LLDB mainline
SymbolContext.h
Go to the documentation of this file.
1//===-- SymbolContext.h -----------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_SYMBOL_SYMBOLCONTEXT_H
10#define LLDB_SYMBOL_SYMBOLCONTEXT_H
11
12#include <memory>
13#include <string>
14#include <vector>
15
16#include "lldb/Core/Address.h"
17#include "lldb/Core/Mangled.h"
20#include "lldb/Utility/Stream.h"
21#include "lldb/lldb-private.h"
22
23namespace lldb_private {
24
26
27/// \class SymbolContext SymbolContext.h "lldb/Symbol/SymbolContext.h" Defines
28/// a symbol context baton that can be handed other debug core functions.
29///
30/// Many debugger functions require a context when doing lookups. This class
31/// provides a common structure that can be used as the result of a query that
32/// can contain a single result. Examples of such queries include
33/// \li Looking up a load address.
35public:
36 /// Default constructor.
37 ///
38 /// Initialize all pointer members to nullptr and all struct members to
39 /// their default state.
41
42 /// Construct with an object that knows how to reconstruct its symbol
43 /// context.
44 ///
45 /// \param[in] sc_scope
46 /// A symbol context scope object that knows how to reconstruct
47 /// it's context.
48 explicit SymbolContext(SymbolContextScope *sc_scope);
49
50 /// Construct with module, and optional compile unit, function, block, line
51 /// table, line entry and symbol.
52 ///
53 /// Initialize all pointer to the specified values.
54 ///
55 /// \param[in] module_sp
56 /// A Module pointer to the module for this context.
57 ///
58 /// \param[in] comp_unit
59 /// A CompileUnit pointer to the compile unit for this context.
60 ///
61 /// \param[in] function
62 /// A Function pointer to the function for this context.
63 ///
64 /// \param[in] block
65 /// A Block pointer to the deepest block for this context.
66 ///
67 /// \param[in] line_entry
68 /// A LineEntry pointer to the line entry for this context.
69 ///
70 /// \param[in] symbol
71 /// A Symbol pointer to the symbol for this context.
74 CompileUnit *comp_unit = nullptr,
75 Function *function = nullptr, Block *block = nullptr,
76 LineEntry *line_entry = nullptr,
77 Symbol *symbol = nullptr);
78
79 // This version sets the target to a NULL TargetSP if you don't know it.
81 CompileUnit *comp_unit = nullptr,
82 Function *function = nullptr, Block *block = nullptr,
83 LineEntry *line_entry = nullptr,
84 Symbol *symbol = nullptr);
85
87
88 /// Clear the object's state.
89 ///
90 /// Resets all pointer members to nullptr, and clears any class objects to
91 /// their default state.
92 void Clear(bool clear_target);
93
94 /// Dump the stop context in this object to a Stream.
95 ///
96 /// Dump the best description of this object to the stream. The information
97 /// displayed depends on the amount and quality of the information in this
98 /// context. If a module, function, file and line number are available, they
99 /// will be dumped. If only a module and function or symbol name with offset
100 /// is available, that will be output. Else just the address at which the
101 /// target was stopped will be displayed.
102 ///
103 /// \param[in] s
104 /// The stream to which to dump the object description.
105 ///
106 /// \param[in] so_addr
107 /// The resolved section offset address.
108 ///
109 /// \param[in] show_fullpaths
110 /// When printing file paths (with the Module), whether the
111 /// base name of the Module should be printed or the full path.
112 ///
113 /// \param[in] show_module
114 /// Whether the module name should be printed followed by a
115 /// grave accent "`" character.
116 ///
117 /// \param[in] show_inlined_frames
118 /// If a given pc is in inlined function(s), whether the inlined
119 /// functions should be printed on separate lines in addition to
120 /// the concrete function containing the pc.
121 ///
122 /// \param[in] show_function_arguments
123 /// If false, this method will try to elide the function argument
124 /// types when printing the function name. This may be ambiguous
125 /// for languages that have function overloading - but it may
126 /// make the "function name" too long to include all the argument
127 /// types.
128 ///
129 /// \param[in] show_function_name
130 /// Normally this should be true - the function/symbol name should
131 /// be printed. In disassembly formatting, where we want a format
132 /// like "<*+36>", this should be false and "*" will be printed
133 /// instead.
134 ///
135 /// \param[in] show_inline_callsite_line_info
136 /// When processing an inline block, the line info of the callsite
137 /// is dumped if this flag is \b true, otherwise the line info
138 /// of the actual inlined function is dumped.
139 ///
140 /// \param[in] pattern
141 /// An optional regex pattern to match against the stop context
142 /// description. If specified, parts of the description matching this
143 /// pattern may be highlighted or processed differently. If this parameter
144 /// is an empty string or not provided, no highlighting is applied.
145 ///
146 /// \return
147 /// \b true if some text was dumped, \b false otherwise.
148 bool DumpStopContext(
149 Stream *s, ExecutionContextScope *exe_scope, const Address &so_addr,
150 bool show_fullpaths, bool show_module, bool show_inlined_frames,
151 bool show_function_arguments, bool show_function_name,
152 bool show_function_display_name = false,
153 std::optional<Stream::HighlightSettings> settings = std::nullopt) const;
154
155 /// Get the address range contained within a symbol context.
156 ///
157 /// Address range priority is as follows:
158 /// - line_entry address range if line_entry is valid and
159 /// eSymbolContextLineEntry is set in \a scope
160 /// - block address range if block is not nullptr and eSymbolContextBlock
161 /// is set in \a scope
162 /// - function address range if function is not nullptr and
163 /// eSymbolContextFunction is set in \a scope
164 /// - symbol address range if symbol is not nullptr and
165 /// eSymbolContextSymbol is set in \a scope
166 ///
167 /// \param[in] scope
168 /// A mask bits from the \b SymbolContextItem enum telling this function
169 /// which address ranges it can use when trying to extract one from
170 /// the valid (non-nullptr) symbol context classes.
171 ///
172 /// \param[in] range_idx
173 /// The address range index to grab. Since many functions and
174 /// blocks are not always contiguous, they may have more than
175 /// one address range.
176 ///
177 /// \param[in] use_inline_block_range
178 /// If \a scope has the eSymbolContextBlock bit set, and there
179 /// is a valid block in the symbol context, return the block
180 /// address range for the containing inline function block, not
181 /// the deepest most block. This allows us to extract information
182 /// for the address range of the inlined function block, not
183 /// the deepest lexical block.
184 ///
185 /// \param[out] range
186 /// An address range object that will be filled in if \b true
187 /// is returned.
188 ///
189 /// \return
190 /// \b True if this symbol context contains items that describe
191 /// an address range, \b false otherwise.
192 bool GetAddressRange(uint32_t scope, uint32_t range_idx,
193 bool use_inline_block_range, AddressRange &range) const;
194
195 /// Get the address of the function or symbol represented by this symbol
196 /// context.
197 ///
198 /// If both fields are present, the address of the function is returned. If
199 /// both are empty, the result is an invalid address.
201
202 llvm::Error GetAddressRangeFromHereToEndLine(uint32_t end_line,
203 AddressRange &range);
204
205 /// Find the best global data symbol visible from this context.
206 ///
207 /// Symbol priority is:
208 /// - extern symbol in the current module if there is one
209 /// - non-extern symbol in the current module if there is one
210 /// - extern symbol in the target
211 /// - non-extern symbol in the target
212 /// It is an error if the highest-priority result is ambiguous.
213 ///
214 /// \param[in] name
215 /// The name of the symbol to search for.
216 ///
217 /// \param[out] error
218 /// An error that will be populated with a message if there was an
219 /// ambiguous result. The error will not be populated if no result
220 /// was found.
221 ///
222 /// \return
223 /// The symbol that was found, or \b nullptr if none was found.
225
226 void GetDescription(
227 Stream *s, lldb::DescriptionLevel level, Target *target,
228 std::optional<Stream::HighlightSettings> settings = std::nullopt) const;
229
230 uint32_t GetResolvedMask() const;
231
233
234 /// Compares the two symbol contexts, considering that the symbol may or may
235 /// not be present. If both symbols are present, compare them, if one of the
236 /// symbols is not present, consider the symbol contexts as equal as long as
237 /// the other fields are equal.
238 ///
239 /// This function exists because SymbolContexts are often created without the
240 /// symbol, which is filled in later on, after its creation.
242 const SymbolContext &rhs);
243
244 /// Compares the two symbol contexts, except for the symbol field.
245 static bool CompareWithoutSymbol(const SymbolContext &lhs,
246 const SymbolContext &rhs);
247
248 /// Find a block that defines the function represented by this symbol
249 /// context.
250 ///
251 /// If this symbol context points to a block that is an inlined function, or
252 /// is contained within an inlined function, the block that defines the
253 /// inlined function is returned.
254 ///
255 /// If this symbol context has no block in it, or the block is not itself an
256 /// inlined function block or contained within one, we return the top level
257 /// function block.
258 ///
259 /// This is a handy function to call when you want to get the block whose
260 /// variable list will include the arguments for the function that is
261 /// represented by this symbol context (whether the function is an inline
262 /// function or not).
263 ///
264 /// \return
265 /// The block object pointer that defines the function that is
266 /// represented by this symbol context object, nullptr otherwise.
268
269 /// Determines the name of the instance variable for the this decl context.
270 ///
271 /// For C++ the name is "this", for Objective-C the name is "self".
272 ///
273 /// \return
274 /// Returns a StringRef for the name of the instance variable.
275 llvm::StringRef GetInstanceVariableName();
276
277 /// Sorts the types in TypeMap according to SymbolContext to TypeList
278 ///
279 void SortTypeList(TypeMap &type_map, TypeList &type_list) const;
280
281 /// Find a name of the innermost function for the symbol context.
282 ///
283 /// For instance, if the symbol context contains an inlined block, it will
284 /// return the inlined function name.
285 ///
286 /// \return
287 /// The name of the function represented by this symbol context.
290
291 /// Get the line entry that corresponds to the function.
292 ///
293 /// If the symbol context contains an inlined block, the line entry for the
294 /// start address of the inlined function will be returned, otherwise the
295 /// line entry for the start address of the function will be returned. This
296 /// can be used after doing a Module::FindFunctions(...) or
297 /// ModuleList::FindFunctions(...) call in order to get the correct line
298 /// table information for the symbol context. it will return the inlined
299 /// function name.
301
302 /// Find the block containing the inlined block that contains this block.
303 ///
304 /// For instance, if the symbol context contains an inlined block, it will
305 /// return the inlined function name.
306 ///
307 /// \param[in] curr_frame_pc
308 /// The address within the block of this object.
309 ///
310 /// \param[out] next_frame_sc
311 /// A new symbol context that does what the title says it does.
312 ///
313 /// \param[out] inlined_frame_addr
314 /// This is what you should report as the PC in \a next_frame_sc.
315 ///
316 /// \return
317 /// \b true if this SymbolContext specifies a block contained in an
318 /// inlined block. If this returns \b true, \a next_frame_sc and
319 /// \a inlined_frame_addr will be filled in correctly.
320 bool GetParentOfInlinedScope(const Address &curr_frame_pc,
321 SymbolContext &next_frame_sc,
322 Address &inlined_frame_addr) const;
323
324 /// If available, will return the function name according to the specified
325 /// mangling preference. If this object represents an inlined function,
326 /// returns the name of the inlined function. Returns nullptr if no function
327 /// name could be determined.
329
330 // Member variables
331 lldb::TargetSP target_sp; ///< The Target for a given query
332 lldb::ModuleSP module_sp; ///< The Module for a given query
333 CompileUnit *comp_unit = nullptr; ///< The CompileUnit for a given query
334 Function *function = nullptr; ///< The Function for a given query
335 Block *block = nullptr; ///< The Block for a given query
336 LineEntry line_entry; ///< The LineEntry for a given query
337 Symbol *symbol = nullptr; ///< The Symbol for a given query
339 nullptr; ///< The global variable matching the given query
340};
341
343public:
354
355 // This one produces a specifier that matches everything...
356 SymbolContextSpecifier(const lldb::TargetSP &target_sp);
357
359
360 bool AddSpecification(const char *spec_string, SpecificationType type);
361
362 bool AddLineSpecification(uint32_t line_no, SpecificationType type);
363
364 void Clear();
365
366 bool SymbolContextMatches(const SymbolContext &sc);
367
368 bool AddressMatches(lldb::addr_t addr);
369
370 void GetDescription(Stream *s, lldb::DescriptionLevel level) const;
371
372private:
374 std::string m_module_spec;
376 std::unique_ptr<FileSpec> m_file_spec_up;
379 std::string m_function_spec;
380 std::string m_class_name;
381 std::unique_ptr<AddressRange> m_address_range_up;
382 uint32_t m_type; // Or'ed bits from SpecificationType
383};
384
385/// \class SymbolContextList SymbolContext.h "lldb/Symbol/SymbolContext.h"
386/// Defines a list of symbol context objects.
387///
388/// This class provides a common structure that can be used to contain the
389/// result of a query that can contain a multiple results. Examples of such
390/// queries include:
391/// \li Looking up a function by name.
392/// \li Finding all addresses for a specified file and line number.
394public:
395 /// Default constructor.
396 ///
397 /// Initialize with an empty list.
399
400 /// Destructor.
402
403 /// Append a new symbol context to the list.
404 ///
405 /// \param[in] sc
406 /// A symbol context to append to the list.
407 void Append(const SymbolContext &sc);
408
409 void Append(const SymbolContextList &sc_list);
410
411 bool AppendIfUnique(const SymbolContext &sc, bool merge_symbol_into_function);
412
413 uint32_t AppendIfUnique(const SymbolContextList &sc_list,
414 bool merge_symbol_into_function);
415
416 /// Clear the object's state.
417 ///
418 /// Clears the symbol context list.
419 void Clear();
420
421 /// Dump a description of this object to a Stream.
422 ///
423 /// Dump a description of the contents of each symbol context in the list to
424 /// the supplied stream \a s.
425 ///
426 /// \param[in] s
427 /// The stream to which to dump the object description.
428 void Dump(Stream *s, Target *target) const;
429
430 /// Get accessor for a symbol context at index \a idx.
431 ///
432 /// Dump a description of the contents of each symbol context in the list to
433 /// the supplied stream \a s.
434 ///
435 /// \param[in] idx
436 /// The zero based index into the symbol context list.
437 ///
438 /// \param[out] sc
439 /// A reference to the symbol context to fill in.
440 ///
441 /// \return
442 /// Returns \b true if \a idx was a valid index into this
443 /// symbol context list and \a sc was filled in, \b false
444 /// otherwise.
445 bool GetContextAtIndex(size_t idx, SymbolContext &sc) const;
446
447 /// Direct reference accessor for a symbol context at index \a idx.
448 ///
449 /// The index \a idx must be a valid index, no error checking will be done
450 /// to ensure that it is valid.
451 ///
452 /// \param[in] idx
453 /// The zero based index into the symbol context list.
454 ///
455 /// \return
456 /// A const reference to the symbol context to fill in.
457 SymbolContext &operator[](size_t idx) { return m_symbol_contexts[idx]; }
458
459 const SymbolContext &operator[](size_t idx) const {
460 return m_symbol_contexts[idx];
461 }
462
463 bool RemoveContextAtIndex(size_t idx);
464
465 /// Get accessor for a symbol context list size.
466 ///
467 /// \return
468 /// Returns the number of symbol context objects in the list.
469 uint32_t GetSize() const;
470
471 bool IsEmpty() const;
472
473 uint32_t NumLineEntriesWithLine(uint32_t line) const;
474
476 Target *target) const;
477
478protected:
479 typedef std::vector<SymbolContext>
480 collection; ///< The collection type for the list.
481 typedef collection::const_iterator const_iterator;
482
483 // Member variables.
484 collection m_symbol_contexts; ///< The list of symbol contexts.
485
486public:
487 const_iterator begin() const { return m_symbol_contexts.begin(); }
488 const_iterator end() const { return m_symbol_contexts.end(); }
489
490 typedef llvm::iterator_range<collection::const_iterator>
495};
496
497bool operator==(const SymbolContext &lhs, const SymbolContext &rhs);
498bool operator!=(const SymbolContext &lhs, const SymbolContext &rhs);
499
500bool operator==(const SymbolContextList &lhs, const SymbolContextList &rhs);
501bool operator!=(const SymbolContextList &lhs, const SymbolContextList &rhs);
502
503} // namespace lldb_private
504
505#endif // LLDB_SYMBOL_SYMBOLCONTEXT_H
static llvm::raw_ostream & error(Stream &strm)
A section + offset based address range class.
A section + offset based address class.
Definition Address.h:62
A class that describes a single lexical block.
Definition Block.h:41
A class that describes a compilation unit.
Definition CompileUnit.h:43
A uniqued constant string class.
Definition ConstString.h:40
"lldb/Target/ExecutionContextScope.h" Inherit from this if your object can reconstruct its execution ...
A class that describes a function.
Definition Function.h:400
A class that handles mangled names.
Definition Mangled.h:34
An error handling class.
Definition Status.h:118
A stream class that can stream formatted output to a file.
Definition Stream.h:28
collection m_symbol_contexts
The list of symbol contexts.
llvm::iterator_range< collection::const_iterator > SymbolContextIterable
bool GetContextAtIndex(size_t idx, SymbolContext &sc) const
Get accessor for a symbol context at index idx.
uint32_t GetSize() const
Get accessor for a symbol context list size.
SymbolContext & operator[](size_t idx)
Direct reference accessor for a symbol context at index idx.
const_iterator begin() const
void GetDescription(Stream *s, lldb::DescriptionLevel level, Target *target) const
const_iterator end() const
bool AppendIfUnique(const SymbolContext &sc, bool merge_symbol_into_function)
void Dump(Stream *s, Target *target) const
Dump a description of this object to a Stream.
uint32_t NumLineEntriesWithLine(uint32_t line) const
collection::const_iterator const_iterator
void Append(const SymbolContext &sc)
Append a new symbol context to the list.
const SymbolContext & operator[](size_t idx) const
SymbolContextIterable SymbolContexts()
void Clear()
Clear the object's state.
std::vector< SymbolContext > collection
The collection type for the list.
SymbolContextList()
Default constructor.
"lldb/Symbol/SymbolContextScope.h" Inherit from this if your object is part of a symbol context and c...
void GetDescription(Stream *s, lldb::DescriptionLevel level) const
std::unique_ptr< AddressRange > m_address_range_up
std::unique_ptr< FileSpec > m_file_spec_up
bool AddLineSpecification(uint32_t line_no, SpecificationType type)
bool AddSpecification(const char *spec_string, SpecificationType type)
bool SymbolContextMatches(const SymbolContext &sc)
SymbolContextSpecifier(const lldb::TargetSP &target_sp)
Defines a symbol context baton that can be handed other debug core functions.
static bool CompareWithoutSymbol(const SymbolContext &lhs, const SymbolContext &rhs)
Compares the two symbol contexts, except for the symbol field.
LineEntry GetFunctionStartLineEntry() const
Get the line entry that corresponds to the function.
const Symbol * FindBestGlobalDataSymbol(ConstString name, Status &error)
Find the best global data symbol visible from this context.
lldb::LanguageType GetLanguage() const
void GetDescription(Stream *s, lldb::DescriptionLevel level, Target *target, std::optional< Stream::HighlightSettings > settings=std::nullopt) const
llvm::Error GetAddressRangeFromHereToEndLine(uint32_t end_line, AddressRange &range)
Function * function
The Function for a given query.
Block * GetFunctionBlock()
Find a block that defines the function represented by this symbol context.
llvm::StringRef GetInstanceVariableName()
Determines the name of the instance variable for the this decl context.
bool GetParentOfInlinedScope(const Address &curr_frame_pc, SymbolContext &next_frame_sc, Address &inlined_frame_addr) const
Find the block containing the inlined block that contains this block.
ConstString GetFunctionName(Mangled::NamePreference preference=Mangled::ePreferDemangled) const
Find a name of the innermost function for the symbol context.
void SortTypeList(TypeMap &type_map, TypeList &type_list) const
Sorts the types in TypeMap according to SymbolContext to TypeList.
Block * block
The Block for a given query.
lldb::ModuleSP module_sp
The Module for a given query.
CompileUnit * comp_unit
The CompileUnit for a given query.
bool DumpStopContext(Stream *s, ExecutionContextScope *exe_scope, const Address &so_addr, bool show_fullpaths, bool show_module, bool show_inlined_frames, bool show_function_arguments, bool show_function_name, bool show_function_display_name=false, std::optional< Stream::HighlightSettings > settings=std::nullopt) const
Dump the stop context in this object to a Stream.
void Clear(bool clear_target)
Clear the object's state.
Variable * variable
The global variable matching the given query.
bool GetAddressRange(uint32_t scope, uint32_t range_idx, bool use_inline_block_range, AddressRange &range) const
Get the address range contained within a symbol context.
Symbol * symbol
The Symbol for a given query.
lldb::TargetSP target_sp
The Target for a given query.
LineEntry line_entry
The LineEntry for a given query.
Mangled GetPossiblyInlinedFunctionName() const
If available, will return the function name according to the specified mangling preference.
static bool CompareConsideringPossiblyNullSymbol(const SymbolContext &lhs, const SymbolContext &rhs)
Compares the two symbol contexts, considering that the symbol may or may not be present.
Address GetFunctionOrSymbolAddress() const
Get the address of the function or symbol represented by this symbol context.
SymbolContext()
Default constructor.
A class that represents a running process on the host machine.
bool operator!=(const Address &lhs, const Address &rhs)
Definition Address.cpp:1017
bool operator==(const Address &lhs, const Address &rhs)
Definition Address.cpp:1011
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
LanguageType
Programming language type.
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::Target > TargetSP
std::shared_ptr< lldb_private::Module > ModuleSP
A line table entry class.
Definition LineEntry.h:21