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/lldb-private.h"
22 #include "lldb/Core/Address.h"
23 #include "lldb/Core/Mangled.h"
24 #include "lldb/Symbol/LineEntry.h"
25 #include "lldb/Utility/Iterable.h"
27 namespace lldb_private {
29 class SymbolContextScope;
31 //----------------------------------------------------------------------
32 /// @class SymbolContext SymbolContext.h "lldb/Symbol/SymbolContext.h"
33 /// @brief Defines a symbol context baton that can be handed other debug
36 /// Many debugger functions require a context when doing lookups. This
37 /// class provides a common structure that can be used as the result
38 /// of a query that can contain a single result. Examples of such
40 /// @li Looking up a load address.
41 //----------------------------------------------------------------------
45 //------------------------------------------------------------------
46 /// Default constructor.
48 /// Initialize all pointer members to nullptr and all struct members
49 /// to their default state.
50 //------------------------------------------------------------------
53 //------------------------------------------------------------------
54 /// Construct with an object that knows how to reconstruct its
57 /// @param[in] sc_scope
58 /// A symbol context scope object that knows how to reconstruct
60 //------------------------------------------------------------------
62 SymbolContext (SymbolContextScope *sc_scope);
64 //------------------------------------------------------------------
65 /// Construct with module, and optional compile unit, function,
66 /// block, line table, line entry and symbol.
68 /// Initialize all pointer to the specified values.
71 /// A Module pointer to the module for this context.
73 /// @param[in] comp_unit
74 /// A CompileUnit pointer to the compile unit for this context.
76 /// @param[in] function
77 /// A Function pointer to the function for this context.
80 /// A Block pointer to the deepest block for this context.
82 /// @param[in] line_entry
83 /// A LineEntry pointer to the line entry for this context.
86 /// A Symbol pointer to the symbol for this context.
87 //------------------------------------------------------------------
89 SymbolContext(const lldb::TargetSP &target_sp,
90 const lldb::ModuleSP &module_sp,
91 CompileUnit *comp_unit = nullptr,
92 Function *function = nullptr,
93 Block *block = nullptr,
94 LineEntry *line_entry = nullptr,
95 Symbol *symbol = nullptr);
97 // This version sets the target to a NULL TargetSP if you don't know it.
99 SymbolContext(const lldb::ModuleSP &module_sp,
100 CompileUnit *comp_unit = nullptr,
101 Function *function = nullptr,
102 Block *block = nullptr,
103 LineEntry *line_entry = nullptr,
104 Symbol *symbol = nullptr);
106 //------------------------------------------------------------------
109 /// Makes a copy of the another SymbolContext object \a rhs.
112 /// A const SymbolContext object reference to copy.
113 //------------------------------------------------------------------
114 SymbolContext (const SymbolContext& rhs);
118 //------------------------------------------------------------------
119 /// Assignment operator.
121 /// Copies the address value from another SymbolContext object \a
122 /// rhs into \a this object.
125 /// A const SymbolContext object reference to copy.
128 /// A const SymbolContext object reference to \a this.
129 //------------------------------------------------------------------
131 operator= (const SymbolContext& rhs);
133 //------------------------------------------------------------------
134 /// Clear the object's state.
136 /// Resets all pointer members to nullptr, and clears any class objects
137 /// to their default state.
138 //------------------------------------------------------------------
140 Clear (bool clear_target);
142 //------------------------------------------------------------------
143 /// Dump a description of this object to a Stream.
145 /// Dump a description of the contents of this object to the
146 /// supplied stream \a s.
149 /// The stream to which to dump the object description.
150 //------------------------------------------------------------------
152 Dump (Stream *s, Target *target) const;
154 //------------------------------------------------------------------
155 /// Dump the stop context in this object to a Stream.
157 /// Dump the best description of this object to the stream. The
158 /// information displayed depends on the amount and quality of the
159 /// information in this context. If a module, function, file and
160 /// line number are available, they will be dumped. If only a
161 /// module and function or symbol name with offset is available,
162 /// that will be output. Else just the address at which the target
163 /// was stopped will be displayed.
166 /// The stream to which to dump the object description.
168 /// @param[in] so_addr
169 /// The resolved section offset address.
171 /// @param[in] show_fullpaths
172 /// When printing file paths (with the Module), whether the
173 /// base name of the Module should be printed or the full path.
175 /// @param[in] show_module
176 /// Whether the module name should be printed followed by a
177 /// grave accent "`" character.
179 /// @param[in] show_inlined_frames
180 /// If a given pc is in inlined function(s), whether the inlined
181 /// functions should be printed on separate lines in addition to
182 /// the concrete function containing the pc.
184 /// @param[in] show_function_arguments
185 /// If false, this method will try to elide the function argument
186 /// types when printing the function name. This may be ambiguous
187 /// for languages that have function overloading - but it may
188 /// make the "function name" too long to include all the argument
191 /// @param[in] show_function_name
192 /// Normally this should be true - the function/symbol name should
193 /// be printed. In disassembly formatting, where we want a format
194 /// like "<*+36>", this should be false and "*" will be printed
196 //------------------------------------------------------------------
198 DumpStopContext (Stream *s,
199 ExecutionContextScope *exe_scope,
200 const Address &so_addr,
203 bool show_inlined_frames,
204 bool show_function_arguments,
205 bool show_function_name) const;
207 //------------------------------------------------------------------
208 /// Get the address range contained within a symbol context.
210 /// Address range priority is as follows:
211 /// - line_entry address range if line_entry is valid and eSymbolContextLineEntry is set in \a scope
212 /// - block address range if block is not nullptr and eSymbolContextBlock is set in \a scope
213 /// - function address range if function is not nullptr and eSymbolContextFunction is set in \a scope
214 /// - symbol address range if symbol is not nullptr and eSymbolContextSymbol is set in \a scope
217 /// A mask of symbol context bits telling this function which
218 /// address ranges it can use when trying to extract one from
219 /// the valid (non-nullptr) symbol context classes.
221 /// @param[in] range_idx
222 /// The address range index to grab. Since many functions and
223 /// blocks are not always contiguous, they may have more than
224 /// one address range.
226 /// @param[in] use_inline_block_range
227 /// If \a scope has the eSymbolContextBlock bit set, and there
228 /// is a valid block in the symbol context, return the block
229 /// address range for the containing inline function block, not
230 /// the deepest most block. This allows us to extract information
231 /// for the address range of the inlined function block, not
232 /// the deepest lexical block.
234 /// @param[out] range
235 /// An address range object that will be filled in if \b true
239 /// \b True if this symbol context contains items that describe
240 /// an address range, \b false otherwise.
241 //------------------------------------------------------------------
243 GetAddressRange (uint32_t scope,
245 bool use_inline_block_range,
246 AddressRange &range) const;
249 GetAddressRangeFromHereToEndLine(uint32_t end_line, AddressRange &range, Error &error);
252 GetDescription(Stream *s,
253 lldb::DescriptionLevel level,
254 Target *target) const;
257 GetResolvedMask () const;
260 GetLanguage () const;
262 //------------------------------------------------------------------
263 /// Find a block that defines the function represented by this
266 /// If this symbol context points to a block that is an inlined
267 /// function, or is contained within an inlined function, the block
268 /// that defines the inlined function is returned.
270 /// If this symbol context has no block in it, or the block is not
271 /// itself an inlined function block or contained within one, we
272 /// return the top level function block.
274 /// This is a handy function to call when you want to get the block
275 /// whose variable list will include the arguments for the function
276 /// that is represented by this symbol context (whether the function
277 /// is an inline function or not).
280 /// The block object pointer that defines the function that is
281 /// represented by this symbol context object, nullptr otherwise.
282 //------------------------------------------------------------------
286 //------------------------------------------------------------------
287 /// If this symbol context represents a function that is a method,
288 /// return true and provide information about the method.
290 /// @param[out] language
291 /// If \b true is returned, the language for the method.
293 /// @param[out] is_instance_method
294 /// If \b true is returned, \b true if this is a instance method,
295 /// \b false if this is a static/class function.
297 /// @param[out] language_object_name
298 /// If \b true is returned, the name of the artificial variable
299 /// for the language ("this" for C++, "self" for ObjC).
302 /// \b True if this symbol context represents a function that
303 /// is a method of a class, \b false otherwise.
304 //------------------------------------------------------------------
306 GetFunctionMethodInfo (lldb::LanguageType &language,
307 bool &is_instance_method,
308 ConstString &language_object_name);
310 //------------------------------------------------------------------
311 /// Sorts the types in TypeMap according to SymbolContext
314 //------------------------------------------------------------------
316 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,
322 /// it will 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 //------------------------------------------------------------------
333 GetFunctionName (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
339 /// for the start address of the inlined function will be returned,
340 /// otherwise the line entry for the start address of the function
341 /// will be returned. This can be used after doing a
342 /// Module::FindFunctions(...) or ModuleList::FindFunctions(...)
343 /// call in order to get the correct line table information for
344 /// the symbol context.
345 /// it will return the inlined function name.
347 /// @param[in] prefer_mangled
348 /// if \btrue, then the mangled name will be returned if there
349 /// is one. Otherwise the unmangled name will be returned if it
353 /// The name of the function represented by this symbol context.
354 //------------------------------------------------------------------
356 GetFunctionStartLineEntry () const;
358 //------------------------------------------------------------------
359 /// Find the block containing the inlined block that contains this block.
361 /// For instance, if the symbol context contains an inlined block,
362 /// it will return the inlined function name.
364 /// @param[in] curr_frame_pc
365 /// The address within the block of this object.
367 /// @param[out] next_frame_sc
368 /// A new symbol context that does what the title says it does.
370 /// @param[out] next_frame_addr
371 /// This is what you should report as the PC in \a next_frame_sc.
374 /// \b true if this SymbolContext specifies a block contained in an
375 /// inlined block. If this returns \b true, \a next_frame_sc and
376 /// \a next_frame_addr will be filled in correctly.
377 //------------------------------------------------------------------
379 GetParentOfInlinedScope (const Address &curr_frame_pc,
380 SymbolContext &next_frame_sc,
381 Address &inlined_frame_addr) const;
383 //------------------------------------------------------------------
385 //------------------------------------------------------------------
386 lldb::TargetSP target_sp; ///< The Target for a given query
387 lldb::ModuleSP module_sp; ///< The Module for a given query
388 CompileUnit * comp_unit; ///< The CompileUnit for a given query
389 Function * function; ///< The Function for a given query
390 Block * block; ///< The Block for a given query
391 LineEntry line_entry; ///< The LineEntry for a given query
392 Symbol * symbol; ///< The Symbol for a given query
393 Variable * variable; ///< The global variable matching the given query
396 class SymbolContextSpecifier
399 typedef enum SpecificationType
401 eNothingSpecified = 0,
402 eModuleSpecified = 1 << 0,
403 eFileSpecified = 1 << 1,
404 eLineStartSpecified = 1 << 2,
405 eLineEndSpecified = 1 << 3,
406 eFunctionSpecified = 1 << 4,
407 eClassOrNamespaceSpecified = 1 << 5,
408 eAddressRangeSpecified = 1 << 6
411 // This one produces a specifier that matches everything...
412 SymbolContextSpecifier (const lldb::TargetSP& target_sp);
414 ~SymbolContextSpecifier();
417 AddSpecification (const char *spec_string, SpecificationType type);
420 AddLineSpecification (uint32_t line_no, SpecificationType type);
426 SymbolContextMatches(SymbolContext &sc);
429 AddressMatches(lldb::addr_t addr);
432 GetDescription (Stream *s, lldb::DescriptionLevel level) const;
435 lldb::TargetSP m_target_sp;
436 std::string m_module_spec;
437 lldb::ModuleSP m_module_sp;
438 std::unique_ptr<FileSpec> m_file_spec_ap;
441 std::string m_function_spec;
442 std::string m_class_name;
443 std::unique_ptr<AddressRange> m_address_range_ap;
444 uint32_t m_type; // Or'ed bits from SpecificationType
447 //----------------------------------------------------------------------
448 /// @class SymbolContextList SymbolContext.h "lldb/Symbol/SymbolContext.h"
449 /// @brief Defines a list of symbol context objects.
451 /// This class provides a common structure that can be used to contain
452 /// the result of a query that can contain a multiple results. Examples
453 /// of such queries include:
454 /// @li Looking up a function by name.
455 /// @li Finding all addresses for a specified file and line number.
456 //----------------------------------------------------------------------
457 class SymbolContextList
460 //------------------------------------------------------------------
461 /// Default constructor.
463 /// Initialize with an empty list.
464 //------------------------------------------------------------------
465 SymbolContextList ();
467 //------------------------------------------------------------------
469 //------------------------------------------------------------------
470 ~SymbolContextList ();
472 //------------------------------------------------------------------
473 /// Append a new symbol context to the list.
476 /// A symbol context to append to the list.
477 //------------------------------------------------------------------
479 Append (const SymbolContext& sc);
482 Append (const SymbolContextList& sc_list);
485 AppendIfUnique (const SymbolContext& sc,
486 bool merge_symbol_into_function);
489 MergeSymbolContextIntoFunctionContext (const SymbolContext& symbol_sc,
490 uint32_t start_idx = 0,
491 uint32_t stop_idx = UINT32_MAX);
494 AppendIfUnique (const SymbolContextList& sc_list,
495 bool merge_symbol_into_function);
497 //------------------------------------------------------------------
498 /// Clear the object's state.
500 /// Clears the symbol context list.
501 //------------------------------------------------------------------
505 //------------------------------------------------------------------
506 /// Dump a description of this object to a Stream.
508 /// Dump a description of the contents of each symbol context in
509 /// the list to the supplied stream \a s.
512 /// The stream to which to dump the object description.
513 //------------------------------------------------------------------
515 Dump(Stream *s, Target *target) const;
517 //------------------------------------------------------------------
518 /// Get accessor for a symbol context at index \a idx.
520 /// Dump a description of the contents of each symbol context in
521 /// the list to the supplied stream \a s.
524 /// The zero based index into the symbol context list.
527 /// A reference to the symbol context to fill in.
530 /// Returns \b true if \a idx was a valid index into this
531 /// symbol context list and \a sc was filled in, \b false
533 //------------------------------------------------------------------
535 GetContextAtIndex(size_t idx, SymbolContext& sc) const;
537 //------------------------------------------------------------------
538 /// Direct reference accessor for a symbol context at index \a idx.
540 /// The index \a idx must be a valid index, no error checking will
541 /// be done to ensure that it is valid.
544 /// The zero based index into the symbol context list.
547 /// A const reference to the symbol context to fill in.
548 //------------------------------------------------------------------
550 operator [] (size_t idx)
552 return m_symbol_contexts[idx];
556 operator [] (size_t idx) const
558 return m_symbol_contexts[idx];
561 //------------------------------------------------------------------
562 /// Get accessor for the last symbol context in the list.
565 /// A reference to the symbol context to fill in.
568 /// Returns \b true if \a sc was filled in, \b false if the
570 //------------------------------------------------------------------
572 GetLastContext(SymbolContext& sc) const;
575 RemoveContextAtIndex (size_t idx);
577 //------------------------------------------------------------------
578 /// Get accessor for a symbol context list size.
581 /// Returns the number of symbol context objects in the list.
582 //------------------------------------------------------------------
587 NumLineEntriesWithLine (uint32_t line) const;
590 GetDescription(Stream *s,
591 lldb::DescriptionLevel level,
592 Target *target) const;
595 typedef std::vector<SymbolContext> collection; ///< The collection type for the list.
597 //------------------------------------------------------------------
599 //------------------------------------------------------------------
600 collection m_symbol_contexts; ///< The list of symbol contexts.
603 typedef AdaptedIterable<collection, SymbolContext, vector_adapter> SymbolContextIterable;
604 SymbolContextIterable
607 return SymbolContextIterable(m_symbol_contexts);
611 bool operator== (const SymbolContext& lhs, const SymbolContext& rhs);
612 bool operator!= (const SymbolContext& lhs, const SymbolContext& rhs);
614 bool operator== (const SymbolContextList& lhs, const SymbolContextList& rhs);
615 bool operator!= (const SymbolContextList& lhs, const SymbolContextList& rhs);
617 } // namespace lldb_private
619 #endif // liblldb_SymbolContext_h_