1 //===-- SymbolContext.h -----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_SymbolContext_h_
11 #define liblldb_SymbolContext_h_
19 // Other libraries and framework includes
21 #include "lldb/Core/Address.h"
22 #include "lldb/Core/Mangled.h"
23 #include "lldb/Symbol/LineEntry.h"
24 #include "lldb/Utility/Iterable.h"
25 #include "lldb/lldb-private.h"
27 namespace lldb_private {
29 class SymbolContextScope;
31 //----------------------------------------------------------------------
32 /// @class SymbolContext SymbolContext.h "lldb/Symbol/SymbolContext.h" Defines
33 /// a symbol context baton that can be handed other debug core functions.
35 /// Many debugger functions require a context when doing lookups. This class
36 /// provides a common structure that can be used as the result of a query that
37 /// can contain a single result. Examples of such queries include
38 /// @li Looking up a load address.
39 //----------------------------------------------------------------------
42 //------------------------------------------------------------------
43 /// Default constructor.
45 /// Initialize all pointer members to nullptr and all struct members to
46 /// their default state.
47 //------------------------------------------------------------------
50 //------------------------------------------------------------------
51 /// Construct with an object that knows how to reconstruct its symbol
54 /// @param[in] sc_scope
55 /// A symbol context scope object that knows how to reconstruct
57 //------------------------------------------------------------------
58 explicit SymbolContext(SymbolContextScope *sc_scope);
60 //------------------------------------------------------------------
61 /// Construct with module, and optional compile unit, function, block, line
62 /// table, line entry and symbol.
64 /// Initialize all pointer to the specified values.
67 /// A Module pointer to the module for this context.
69 /// @param[in] comp_unit
70 /// A CompileUnit pointer to the compile unit for this context.
72 /// @param[in] function
73 /// A Function pointer to the function for this context.
76 /// A Block pointer to the deepest block for this context.
78 /// @param[in] line_entry
79 /// A LineEntry pointer to the line entry for this context.
82 /// A Symbol pointer to the symbol for this context.
83 //------------------------------------------------------------------
84 explicit SymbolContext(const lldb::TargetSP &target_sp,
85 const lldb::ModuleSP &module_sp,
86 CompileUnit *comp_unit = nullptr,
87 Function *function = nullptr, Block *block = nullptr,
88 LineEntry *line_entry = nullptr,
89 Symbol *symbol = nullptr);
91 // This version sets the target to a NULL TargetSP if you don't know it.
92 explicit SymbolContext(const lldb::ModuleSP &module_sp,
93 CompileUnit *comp_unit = nullptr,
94 Function *function = nullptr, Block *block = nullptr,
95 LineEntry *line_entry = nullptr,
96 Symbol *symbol = nullptr);
98 //------------------------------------------------------------------
101 /// Makes a copy of the another SymbolContext object \a rhs.
104 /// A const SymbolContext object reference to copy.
105 //------------------------------------------------------------------
106 SymbolContext(const SymbolContext &rhs);
110 //------------------------------------------------------------------
111 /// Assignment operator.
113 /// Copies the address value from another SymbolContext object \a rhs into
117 /// A const SymbolContext object reference to copy.
120 /// A const SymbolContext object reference to \a this.
121 //------------------------------------------------------------------
122 const SymbolContext &operator=(const SymbolContext &rhs);
124 //------------------------------------------------------------------
125 /// Clear the object's state.
127 /// Resets all pointer members to nullptr, and clears any class objects to
128 /// their default state.
129 //------------------------------------------------------------------
130 void Clear(bool clear_target);
132 //------------------------------------------------------------------
133 /// Dump a description of this object to a Stream.
135 /// Dump a description of the contents of this object to the supplied stream
139 /// The stream to which to dump the object description.
140 //------------------------------------------------------------------
141 void Dump(Stream *s, Target *target) const;
143 //------------------------------------------------------------------
144 /// Dump the stop context in this object to a Stream.
146 /// Dump the best description of this object to the stream. The information
147 /// displayed depends on the amount and quality of the information in this
148 /// context. If a module, function, file and line number are available, they
149 /// will be dumped. If only a module and function or symbol name with offset
150 /// is available, that will be output. Else just the address at which the
151 /// target was stopped will be displayed.
154 /// The stream to which to dump the object description.
156 /// @param[in] so_addr
157 /// The resolved section offset address.
159 /// @param[in] show_fullpaths
160 /// When printing file paths (with the Module), whether the
161 /// base name of the Module should be printed or the full path.
163 /// @param[in] show_module
164 /// Whether the module name should be printed followed by a
165 /// grave accent "`" character.
167 /// @param[in] show_inlined_frames
168 /// If a given pc is in inlined function(s), whether the inlined
169 /// functions should be printed on separate lines in addition to
170 /// the concrete function containing the pc.
172 /// @param[in] show_function_arguments
173 /// If false, this method will try to elide the function argument
174 /// types when printing the function name. This may be ambiguous
175 /// for languages that have function overloading - but it may
176 /// make the "function name" too long to include all the argument
179 /// @param[in] show_function_name
180 /// Normally this should be true - the function/symbol name should
181 /// be printed. In disassembly formatting, where we want a format
182 /// like "<*+36>", this should be false and "*" will be printed
184 //------------------------------------------------------------------
185 bool DumpStopContext(Stream *s, ExecutionContextScope *exe_scope,
186 const Address &so_addr, bool show_fullpaths,
187 bool show_module, bool show_inlined_frames,
188 bool show_function_arguments,
189 bool show_function_name) const;
191 //------------------------------------------------------------------
192 /// Get the address range contained within a symbol context.
194 /// Address range priority is as follows:
195 /// - line_entry address range if line_entry is valid and
196 /// eSymbolContextLineEntry is set in \a scope
197 /// - block address range if block is not nullptr and eSymbolContextBlock
198 /// is set in \a scope
199 /// - function address range if function is not nullptr and
200 /// eSymbolContextFunction is set in \a scope
201 /// - symbol address range if symbol is not nullptr and
202 /// eSymbolContextSymbol is set in \a scope
205 /// A mask of symbol context bits telling this function which
206 /// address ranges it can use when trying to extract one from
207 /// the valid (non-nullptr) symbol context classes.
209 /// @param[in] range_idx
210 /// The address range index to grab. Since many functions and
211 /// blocks are not always contiguous, they may have more than
212 /// one address range.
214 /// @param[in] use_inline_block_range
215 /// If \a scope has the eSymbolContextBlock bit set, and there
216 /// is a valid block in the symbol context, return the block
217 /// address range for the containing inline function block, not
218 /// the deepest most block. This allows us to extract information
219 /// for the address range of the inlined function block, not
220 /// the deepest lexical block.
222 /// @param[out] range
223 /// An address range object that will be filled in if \b true
227 /// \b True if this symbol context contains items that describe
228 /// an address range, \b false otherwise.
229 //------------------------------------------------------------------
230 bool GetAddressRange(uint32_t scope, uint32_t range_idx,
231 bool use_inline_block_range, AddressRange &range) const;
233 bool GetAddressRangeFromHereToEndLine(uint32_t end_line, AddressRange &range,
236 //------------------------------------------------------------------
237 /// Find the best global data symbol visible from this context.
239 /// Symbol priority is:
240 /// - extern symbol in the current module if there is one
241 /// - non-extern symbol in the current module if there is one
242 /// - extern symbol in the target
243 /// - non-extern symbol in the target
244 /// It is an error if the highest-priority result is ambiguous.
247 /// The name of the symbol to search for.
249 /// @param[out] error
250 /// An error that will be populated with a message if there was an
251 /// ambiguous result. The error will not be populated if no result
255 /// The symbol that was found, or \b nullptr if none was found.
256 //------------------------------------------------------------------
257 const Symbol *FindBestGlobalDataSymbol(const ConstString &name, Status &error);
259 void GetDescription(Stream *s, lldb::DescriptionLevel level,
260 Target *target) const;
262 uint32_t GetResolvedMask() const;
264 lldb::LanguageType GetLanguage() const;
266 //------------------------------------------------------------------
267 /// Find a block that defines the function represented by this symbol
270 /// If this symbol context points to a block that is an inlined function, or
271 /// is contained within an inlined function, the block that defines the
272 /// inlined function is returned.
274 /// If this symbol context has no block in it, or the block is not itself an
275 /// inlined function block or contained within one, we return the top level
278 /// This is a handy function to call when you want to get the block whose
279 /// variable list will include the arguments for the function that is
280 /// represented by this symbol context (whether the function is an inline
281 /// function or not).
284 /// The block object pointer that defines the function that is
285 /// represented by this symbol context object, nullptr otherwise.
286 //------------------------------------------------------------------
287 Block *GetFunctionBlock();
289 //------------------------------------------------------------------
290 /// If this symbol context represents a function that is a method, return
291 /// true and provide information about the method.
293 /// @param[out] language
294 /// If \b true is returned, the language for the method.
296 /// @param[out] is_instance_method
297 /// If \b true is returned, \b true if this is a instance method,
298 /// \b false if this is a static/class function.
300 /// @param[out] language_object_name
301 /// If \b true is returned, the name of the artificial variable
302 /// for the language ("this" for C++, "self" for ObjC).
305 /// \b True if this symbol context represents a function that
306 /// is a method of a class, \b false otherwise.
307 //------------------------------------------------------------------
308 bool GetFunctionMethodInfo(lldb::LanguageType &language,
309 bool &is_instance_method,
310 ConstString &language_object_name);
312 //------------------------------------------------------------------
313 /// Sorts the types in TypeMap according to SymbolContext to TypeList
315 //------------------------------------------------------------------
316 void SortTypeList(TypeMap &type_map, TypeList &type_list) const;
318 //------------------------------------------------------------------
319 /// Find a name of the innermost function for the symbol context.
321 /// For instance, if the symbol context contains an inlined block, it will
322 /// return the inlined function name.
324 /// @param[in] prefer_mangled
325 /// if \btrue, then the mangled name will be returned if there
326 /// is one. Otherwise the unmangled name will be returned if it
330 /// The name of the function represented by this symbol context.
331 //------------------------------------------------------------------
332 ConstString GetFunctionName(
333 Mangled::NamePreference preference = Mangled::ePreferDemangled) const;
335 //------------------------------------------------------------------
336 /// Get the line entry that corresponds to the function.
338 /// If the symbol context contains an inlined block, the line entry for the
339 /// start address of the inlined function will be returned, otherwise the
340 /// line entry for the start address of the function will be returned. This
341 /// can be used after doing a Module::FindFunctions(...) or
342 /// ModuleList::FindFunctions(...) call in order to get the correct line
343 /// table information for the symbol context. it will return the inlined
346 /// @param[in] prefer_mangled
347 /// if \btrue, then the mangled name will be returned if there
348 /// is one. Otherwise the unmangled name will be returned if it
352 /// The name of the function represented by this symbol context.
353 //------------------------------------------------------------------
354 LineEntry GetFunctionStartLineEntry() const;
356 //------------------------------------------------------------------
357 /// Find the block containing the inlined block that contains this block.
359 /// For instance, if the symbol context contains an inlined block, it will
360 /// return the inlined function name.
362 /// @param[in] curr_frame_pc
363 /// The address within the block of this object.
365 /// @param[out] next_frame_sc
366 /// A new symbol context that does what the title says it does.
368 /// @param[out] next_frame_addr
369 /// This is what you should report as the PC in \a next_frame_sc.
372 /// \b true if this SymbolContext specifies a block contained in an
373 /// inlined block. If this returns \b true, \a next_frame_sc and
374 /// \a next_frame_addr will be filled in correctly.
375 //------------------------------------------------------------------
376 bool GetParentOfInlinedScope(const Address &curr_frame_pc,
377 SymbolContext &next_frame_sc,
378 Address &inlined_frame_addr) const;
380 //------------------------------------------------------------------
382 //------------------------------------------------------------------
383 lldb::TargetSP target_sp; ///< The Target for a given query
384 lldb::ModuleSP module_sp; ///< The Module for a given query
385 CompileUnit *comp_unit; ///< The CompileUnit for a given query
386 Function *function; ///< The Function for a given query
387 Block *block; ///< The Block for a given query
388 LineEntry line_entry; ///< The LineEntry for a given query
389 Symbol *symbol; ///< The Symbol for a given query
390 Variable *variable; ///< The global variable matching the given query
393 class SymbolContextSpecifier {
395 typedef enum SpecificationType {
396 eNothingSpecified = 0,
397 eModuleSpecified = 1 << 0,
398 eFileSpecified = 1 << 1,
399 eLineStartSpecified = 1 << 2,
400 eLineEndSpecified = 1 << 3,
401 eFunctionSpecified = 1 << 4,
402 eClassOrNamespaceSpecified = 1 << 5,
403 eAddressRangeSpecified = 1 << 6
406 // This one produces a specifier that matches everything...
407 SymbolContextSpecifier(const lldb::TargetSP &target_sp);
409 ~SymbolContextSpecifier();
411 bool AddSpecification(const char *spec_string, SpecificationType type);
413 bool AddLineSpecification(uint32_t line_no, SpecificationType type);
417 bool SymbolContextMatches(SymbolContext &sc);
419 bool AddressMatches(lldb::addr_t addr);
421 void GetDescription(Stream *s, lldb::DescriptionLevel level) const;
424 lldb::TargetSP m_target_sp;
425 std::string m_module_spec;
426 lldb::ModuleSP m_module_sp;
427 std::unique_ptr<FileSpec> m_file_spec_ap;
430 std::string m_function_spec;
431 std::string m_class_name;
432 std::unique_ptr<AddressRange> m_address_range_ap;
433 uint32_t m_type; // Or'ed bits from SpecificationType
436 //----------------------------------------------------------------------
437 /// @class SymbolContextList SymbolContext.h "lldb/Symbol/SymbolContext.h"
438 /// Defines a list of symbol context objects.
440 /// This class provides a common structure that can be used to contain the
441 /// result of a query that can contain a multiple results. Examples of such
443 /// @li Looking up a function by name.
444 /// @li Finding all addresses for a specified file and line number.
445 //----------------------------------------------------------------------
446 class SymbolContextList {
448 //------------------------------------------------------------------
449 /// Default constructor.
451 /// Initialize with an empty list.
452 //------------------------------------------------------------------
455 //------------------------------------------------------------------
457 //------------------------------------------------------------------
458 ~SymbolContextList();
460 //------------------------------------------------------------------
461 /// Append a new symbol context to the list.
464 /// A symbol context to append to the list.
465 //------------------------------------------------------------------
466 void Append(const SymbolContext &sc);
468 void Append(const SymbolContextList &sc_list);
470 bool AppendIfUnique(const SymbolContext &sc, bool merge_symbol_into_function);
472 bool MergeSymbolContextIntoFunctionContext(const SymbolContext &symbol_sc,
473 uint32_t start_idx = 0,
474 uint32_t stop_idx = UINT32_MAX);
476 uint32_t AppendIfUnique(const SymbolContextList &sc_list,
477 bool merge_symbol_into_function);
479 //------------------------------------------------------------------
480 /// Clear the object's state.
482 /// Clears the symbol context list.
483 //------------------------------------------------------------------
486 //------------------------------------------------------------------
487 /// Dump a description of this object to a Stream.
489 /// Dump a description of the contents of each symbol context in the list to
490 /// the supplied stream \a s.
493 /// The stream to which to dump the object description.
494 //------------------------------------------------------------------
495 void Dump(Stream *s, Target *target) const;
497 //------------------------------------------------------------------
498 /// Get accessor for a symbol context at index \a idx.
500 /// Dump a description of the contents of each symbol context in the list to
501 /// the supplied stream \a s.
504 /// The zero based index into the symbol context list.
507 /// A reference to the symbol context to fill in.
510 /// Returns \b true if \a idx was a valid index into this
511 /// symbol context list and \a sc was filled in, \b false
513 //------------------------------------------------------------------
514 bool GetContextAtIndex(size_t idx, SymbolContext &sc) const;
516 //------------------------------------------------------------------
517 /// Direct reference accessor for a symbol context at index \a idx.
519 /// The index \a idx must be a valid index, no error checking will be done
520 /// to ensure that it is valid.
523 /// The zero based index into the symbol context list.
526 /// A const reference to the symbol context to fill in.
527 //------------------------------------------------------------------
528 SymbolContext &operator[](size_t idx) { return m_symbol_contexts[idx]; }
530 const SymbolContext &operator[](size_t idx) const {
531 return m_symbol_contexts[idx];
534 //------------------------------------------------------------------
535 /// Get accessor for the last symbol context in the list.
538 /// A reference to the symbol context to fill in.
541 /// Returns \b true if \a sc was filled in, \b false if the
543 //------------------------------------------------------------------
544 bool GetLastContext(SymbolContext &sc) const;
546 bool RemoveContextAtIndex(size_t idx);
548 //------------------------------------------------------------------
549 /// Get accessor for a symbol context list size.
552 /// Returns the number of symbol context objects in the list.
553 //------------------------------------------------------------------
554 uint32_t GetSize() const;
556 uint32_t NumLineEntriesWithLine(uint32_t line) const;
558 void GetDescription(Stream *s, lldb::DescriptionLevel level,
559 Target *target) const;
562 typedef std::vector<SymbolContext>
563 collection; ///< The collection type for the list.
565 //------------------------------------------------------------------
567 //------------------------------------------------------------------
568 collection m_symbol_contexts; ///< The list of symbol contexts.
571 typedef AdaptedIterable<collection, SymbolContext, vector_adapter>
572 SymbolContextIterable;
573 SymbolContextIterable SymbolContexts() {
574 return SymbolContextIterable(m_symbol_contexts);
578 bool operator==(const SymbolContext &lhs, const SymbolContext &rhs);
579 bool operator!=(const SymbolContext &lhs, const SymbolContext &rhs);
581 bool operator==(const SymbolContextList &lhs, const SymbolContextList &rhs);
582 bool operator!=(const SymbolContextList &lhs, const SymbolContextList &rhs);
584 } // namespace lldb_private
586 #endif // liblldb_SymbolContext_h_