SymbolContext.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. #ifndef LLDB_SYMBOL_SYMBOLCONTEXT_H
  9. #define LLDB_SYMBOL_SYMBOLCONTEXT_H
  10. #include <memory>
  11. #include <string>
  12. #include <vector>
  13. #include "lldb/Core/Address.h"
  14. #include "lldb/Core/Mangled.h"
  15. #include "lldb/Symbol/LineEntry.h"
  16. #include "lldb/Utility/Iterable.h"
  17. #include "lldb/lldb-private.h"
  18. namespace lldb_private {
  19. class SymbolContextScope;
  20. /// \class SymbolContext SymbolContext.h "lldb/Symbol/SymbolContext.h" Defines
  21. /// a symbol context baton that can be handed other debug core functions.
  22. ///
  23. /// Many debugger functions require a context when doing lookups. This class
  24. /// provides a common structure that can be used as the result of a query that
  25. /// can contain a single result. Examples of such queries include
  26. /// \li Looking up a load address.
  27. class SymbolContext {
  28. public:
  29. /// Default constructor.
  30. ///
  31. /// Initialize all pointer members to nullptr and all struct members to
  32. /// their default state.
  33. SymbolContext();
  34. /// Construct with an object that knows how to reconstruct its symbol
  35. /// context.
  36. ///
  37. /// \param[in] sc_scope
  38. /// A symbol context scope object that knows how to reconstruct
  39. /// it's context.
  40. explicit SymbolContext(SymbolContextScope *sc_scope);
  41. /// Construct with module, and optional compile unit, function, block, line
  42. /// table, line entry and symbol.
  43. ///
  44. /// Initialize all pointer to the specified values.
  45. ///
  46. /// \param[in] module_sp
  47. /// A Module pointer to the module for this context.
  48. ///
  49. /// \param[in] comp_unit
  50. /// A CompileUnit pointer to the compile unit for this context.
  51. ///
  52. /// \param[in] function
  53. /// A Function pointer to the function for this context.
  54. ///
  55. /// \param[in] block
  56. /// A Block pointer to the deepest block for this context.
  57. ///
  58. /// \param[in] line_entry
  59. /// A LineEntry pointer to the line entry for this context.
  60. ///
  61. /// \param[in] symbol
  62. /// A Symbol pointer to the symbol for this context.
  63. explicit SymbolContext(const lldb::TargetSP &target_sp,
  64. const lldb::ModuleSP &module_sp,
  65. CompileUnit *comp_unit = nullptr,
  66. Function *function = nullptr, Block *block = nullptr,
  67. LineEntry *line_entry = nullptr,
  68. Symbol *symbol = nullptr);
  69. // This version sets the target to a NULL TargetSP if you don't know it.
  70. explicit SymbolContext(const lldb::ModuleSP &module_sp,
  71. CompileUnit *comp_unit = nullptr,
  72. Function *function = nullptr, Block *block = nullptr,
  73. LineEntry *line_entry = nullptr,
  74. Symbol *symbol = nullptr);
  75. ~SymbolContext();
  76. /// Clear the object's state.
  77. ///
  78. /// Resets all pointer members to nullptr, and clears any class objects to
  79. /// their default state.
  80. void Clear(bool clear_target);
  81. /// Dump a description of this object to a Stream.
  82. ///
  83. /// Dump a description of the contents of this object to the supplied stream
  84. /// \a s.
  85. ///
  86. /// \param[in] s
  87. /// The stream to which to dump the object description.
  88. void Dump(Stream *s, Target *target) const;
  89. /// Dump the stop context in this object to a Stream.
  90. ///
  91. /// Dump the best description of this object to the stream. The information
  92. /// displayed depends on the amount and quality of the information in this
  93. /// context. If a module, function, file and line number are available, they
  94. /// will be dumped. If only a module and function or symbol name with offset
  95. /// is available, that will be output. Else just the address at which the
  96. /// target was stopped will be displayed.
  97. ///
  98. /// \param[in] s
  99. /// The stream to which to dump the object description.
  100. ///
  101. /// \param[in] so_addr
  102. /// The resolved section offset address.
  103. ///
  104. /// \param[in] show_fullpaths
  105. /// When printing file paths (with the Module), whether the
  106. /// base name of the Module should be printed or the full path.
  107. ///
  108. /// \param[in] show_module
  109. /// Whether the module name should be printed followed by a
  110. /// grave accent "`" character.
  111. ///
  112. /// \param[in] show_inlined_frames
  113. /// If a given pc is in inlined function(s), whether the inlined
  114. /// functions should be printed on separate lines in addition to
  115. /// the concrete function containing the pc.
  116. ///
  117. /// \param[in] show_function_arguments
  118. /// If false, this method will try to elide the function argument
  119. /// types when printing the function name. This may be ambiguous
  120. /// for languages that have function overloading - but it may
  121. /// make the "function name" too long to include all the argument
  122. /// types.
  123. ///
  124. /// \param[in] show_function_name
  125. /// Normally this should be true - the function/symbol name should
  126. /// be printed. In disassembly formatting, where we want a format
  127. /// like "<*+36>", this should be false and "*" will be printed
  128. /// instead.
  129. ///
  130. /// \param[in] show_inline_callsite_line_info
  131. /// When processing an inline block, the line info of the callsite
  132. /// is dumped if this flag is \b true, otherwise the line info
  133. /// of the actual inlined function is dumped.
  134. ///
  135. /// \return
  136. /// \b true if some text was dumped, \b false otherwise.
  137. bool DumpStopContext(Stream *s, ExecutionContextScope *exe_scope,
  138. const Address &so_addr, bool show_fullpaths,
  139. bool show_module, bool show_inlined_frames,
  140. bool show_function_arguments,
  141. bool show_function_name) const;
  142. /// Get the address range contained within a symbol context.
  143. ///
  144. /// Address range priority is as follows:
  145. /// - line_entry address range if line_entry is valid and
  146. /// eSymbolContextLineEntry is set in \a scope
  147. /// - block address range if block is not nullptr and eSymbolContextBlock
  148. /// is set in \a scope
  149. /// - function address range if function is not nullptr and
  150. /// eSymbolContextFunction is set in \a scope
  151. /// - symbol address range if symbol is not nullptr and
  152. /// eSymbolContextSymbol is set in \a scope
  153. ///
  154. /// \param[in] scope
  155. /// A mask of symbol context bits telling this function which
  156. /// address ranges it can use when trying to extract one from
  157. /// the valid (non-nullptr) symbol context classes.
  158. ///
  159. /// \param[in] range_idx
  160. /// The address range index to grab. Since many functions and
  161. /// blocks are not always contiguous, they may have more than
  162. /// one address range.
  163. ///
  164. /// \param[in] use_inline_block_range
  165. /// If \a scope has the eSymbolContextBlock bit set, and there
  166. /// is a valid block in the symbol context, return the block
  167. /// address range for the containing inline function block, not
  168. /// the deepest most block. This allows us to extract information
  169. /// for the address range of the inlined function block, not
  170. /// the deepest lexical block.
  171. ///
  172. /// \param[out] range
  173. /// An address range object that will be filled in if \b true
  174. /// is returned.
  175. ///
  176. /// \return
  177. /// \b True if this symbol context contains items that describe
  178. /// an address range, \b false otherwise.
  179. bool GetAddressRange(uint32_t scope, uint32_t range_idx,
  180. bool use_inline_block_range, AddressRange &range) const;
  181. bool GetAddressRangeFromHereToEndLine(uint32_t end_line, AddressRange &range,
  182. Status &error);
  183. /// Find the best global data symbol visible from this context.
  184. ///
  185. /// Symbol priority is:
  186. /// - extern symbol in the current module if there is one
  187. /// - non-extern symbol in the current module if there is one
  188. /// - extern symbol in the target
  189. /// - non-extern symbol in the target
  190. /// It is an error if the highest-priority result is ambiguous.
  191. ///
  192. /// \param[in] name
  193. /// The name of the symbol to search for.
  194. ///
  195. /// \param[out] error
  196. /// An error that will be populated with a message if there was an
  197. /// ambiguous result. The error will not be populated if no result
  198. /// was found.
  199. ///
  200. /// \return
  201. /// The symbol that was found, or \b nullptr if none was found.
  202. const Symbol *FindBestGlobalDataSymbol(ConstString name, Status &error);
  203. void GetDescription(Stream *s, lldb::DescriptionLevel level,
  204. Target *target) const;
  205. uint32_t GetResolvedMask() const;
  206. lldb::LanguageType GetLanguage() const;
  207. /// Find a block that defines the function represented by this symbol
  208. /// context.
  209. ///
  210. /// If this symbol context points to a block that is an inlined function, or
  211. /// is contained within an inlined function, the block that defines the
  212. /// inlined function is returned.
  213. ///
  214. /// If this symbol context has no block in it, or the block is not itself an
  215. /// inlined function block or contained within one, we return the top level
  216. /// function block.
  217. ///
  218. /// This is a handy function to call when you want to get the block whose
  219. /// variable list will include the arguments for the function that is
  220. /// represented by this symbol context (whether the function is an inline
  221. /// function or not).
  222. ///
  223. /// \return
  224. /// The block object pointer that defines the function that is
  225. /// represented by this symbol context object, nullptr otherwise.
  226. Block *GetFunctionBlock();
  227. /// If this symbol context represents a function that is a method, return
  228. /// true and provide information about the method.
  229. ///
  230. /// \param[out] language
  231. /// If \b true is returned, the language for the method.
  232. ///
  233. /// \param[out] is_instance_method
  234. /// If \b true is returned, \b true if this is a instance method,
  235. /// \b false if this is a static/class function.
  236. ///
  237. /// \param[out] language_object_name
  238. /// If \b true is returned, the name of the artificial variable
  239. /// for the language ("this" for C++, "self" for ObjC).
  240. ///
  241. /// \return
  242. /// \b True if this symbol context represents a function that
  243. /// is a method of a class, \b false otherwise.
  244. bool GetFunctionMethodInfo(lldb::LanguageType &language,
  245. bool &is_instance_method,
  246. ConstString &language_object_name);
  247. /// Sorts the types in TypeMap according to SymbolContext to TypeList
  248. ///
  249. void SortTypeList(TypeMap &type_map, TypeList &type_list) const;
  250. /// Find a name of the innermost function for the symbol context.
  251. ///
  252. /// For instance, if the symbol context contains an inlined block, it will
  253. /// return the inlined function name.
  254. ///
  255. /// \return
  256. /// The name of the function represented by this symbol context.
  257. ConstString GetFunctionName(
  258. Mangled::NamePreference preference = Mangled::ePreferDemangled) const;
  259. /// Get the line entry that corresponds to the function.
  260. ///
  261. /// If the symbol context contains an inlined block, the line entry for the
  262. /// start address of the inlined function will be returned, otherwise the
  263. /// line entry for the start address of the function will be returned. This
  264. /// can be used after doing a Module::FindFunctions(...) or
  265. /// ModuleList::FindFunctions(...) call in order to get the correct line
  266. /// table information for the symbol context. it will return the inlined
  267. /// function name.
  268. LineEntry GetFunctionStartLineEntry() const;
  269. /// Find the block containing the inlined block that contains this block.
  270. ///
  271. /// For instance, if the symbol context contains an inlined block, it will
  272. /// return the inlined function name.
  273. ///
  274. /// \param[in] curr_frame_pc
  275. /// The address within the block of this object.
  276. ///
  277. /// \param[out] next_frame_sc
  278. /// A new symbol context that does what the title says it does.
  279. ///
  280. /// \param[out] inlined_frame_addr
  281. /// This is what you should report as the PC in \a next_frame_sc.
  282. ///
  283. /// \return
  284. /// \b true if this SymbolContext specifies a block contained in an
  285. /// inlined block. If this returns \b true, \a next_frame_sc and
  286. /// \a inlined_frame_addr will be filled in correctly.
  287. bool GetParentOfInlinedScope(const Address &curr_frame_pc,
  288. SymbolContext &next_frame_sc,
  289. Address &inlined_frame_addr) const;
  290. // Member variables
  291. lldb::TargetSP target_sp; ///< The Target for a given query
  292. lldb::ModuleSP module_sp; ///< The Module for a given query
  293. CompileUnit *comp_unit; ///< The CompileUnit for a given query
  294. Function *function; ///< The Function for a given query
  295. Block *block; ///< The Block for a given query
  296. LineEntry line_entry; ///< The LineEntry for a given query
  297. Symbol *symbol; ///< The Symbol for a given query
  298. Variable *variable; ///< The global variable matching the given query
  299. };
  300. class SymbolContextSpecifier {
  301. public:
  302. enum SpecificationType {
  303. eNothingSpecified = 0,
  304. eModuleSpecified = 1 << 0,
  305. eFileSpecified = 1 << 1,
  306. eLineStartSpecified = 1 << 2,
  307. eLineEndSpecified = 1 << 3,
  308. eFunctionSpecified = 1 << 4,
  309. eClassOrNamespaceSpecified = 1 << 5,
  310. eAddressRangeSpecified = 1 << 6
  311. };
  312. // This one produces a specifier that matches everything...
  313. SymbolContextSpecifier(const lldb::TargetSP &target_sp);
  314. ~SymbolContextSpecifier();
  315. bool AddSpecification(const char *spec_string, SpecificationType type);
  316. bool AddLineSpecification(uint32_t line_no, SpecificationType type);
  317. void Clear();
  318. bool SymbolContextMatches(const SymbolContext &sc);
  319. bool AddressMatches(lldb::addr_t addr);
  320. void GetDescription(Stream *s, lldb::DescriptionLevel level) const;
  321. private:
  322. lldb::TargetSP m_target_sp;
  323. std::string m_module_spec;
  324. lldb::ModuleSP m_module_sp;
  325. std::unique_ptr<FileSpec> m_file_spec_up;
  326. size_t m_start_line;
  327. size_t m_end_line;
  328. std::string m_function_spec;
  329. std::string m_class_name;
  330. std::unique_ptr<AddressRange> m_address_range_up;
  331. uint32_t m_type; // Or'ed bits from SpecificationType
  332. };
  333. /// \class SymbolContextList SymbolContext.h "lldb/Symbol/SymbolContext.h"
  334. /// Defines a list of symbol context objects.
  335. ///
  336. /// This class provides a common structure that can be used to contain the
  337. /// result of a query that can contain a multiple results. Examples of such
  338. /// queries include:
  339. /// \li Looking up a function by name.
  340. /// \li Finding all addresses for a specified file and line number.
  341. class SymbolContextList {
  342. public:
  343. /// Default constructor.
  344. ///
  345. /// Initialize with an empty list.
  346. SymbolContextList();
  347. /// Destructor.
  348. ~SymbolContextList();
  349. /// Append a new symbol context to the list.
  350. ///
  351. /// \param[in] sc
  352. /// A symbol context to append to the list.
  353. void Append(const SymbolContext &sc);
  354. void Append(const SymbolContextList &sc_list);
  355. bool AppendIfUnique(const SymbolContext &sc, bool merge_symbol_into_function);
  356. uint32_t AppendIfUnique(const SymbolContextList &sc_list,
  357. bool merge_symbol_into_function);
  358. /// Clear the object's state.
  359. ///
  360. /// Clears the symbol context list.
  361. void Clear();
  362. /// Dump a description of this object to a Stream.
  363. ///
  364. /// Dump a description of the contents of each symbol context in the list to
  365. /// the supplied stream \a s.
  366. ///
  367. /// \param[in] s
  368. /// The stream to which to dump the object description.
  369. void Dump(Stream *s, Target *target) const;
  370. /// Get accessor for a symbol context at index \a idx.
  371. ///
  372. /// Dump a description of the contents of each symbol context in the list to
  373. /// the supplied stream \a s.
  374. ///
  375. /// \param[in] idx
  376. /// The zero based index into the symbol context list.
  377. ///
  378. /// \param[out] sc
  379. /// A reference to the symbol context to fill in.
  380. ///
  381. /// \return
  382. /// Returns \b true if \a idx was a valid index into this
  383. /// symbol context list and \a sc was filled in, \b false
  384. /// otherwise.
  385. bool GetContextAtIndex(size_t idx, SymbolContext &sc) const;
  386. /// Direct reference accessor for a symbol context at index \a idx.
  387. ///
  388. /// The index \a idx must be a valid index, no error checking will be done
  389. /// to ensure that it is valid.
  390. ///
  391. /// \param[in] idx
  392. /// The zero based index into the symbol context list.
  393. ///
  394. /// \return
  395. /// A const reference to the symbol context to fill in.
  396. SymbolContext &operator[](size_t idx) { return m_symbol_contexts[idx]; }
  397. const SymbolContext &operator[](size_t idx) const {
  398. return m_symbol_contexts[idx];
  399. }
  400. bool RemoveContextAtIndex(size_t idx);
  401. /// Get accessor for a symbol context list size.
  402. ///
  403. /// \return
  404. /// Returns the number of symbol context objects in the list.
  405. uint32_t GetSize() const;
  406. bool IsEmpty() const;
  407. uint32_t NumLineEntriesWithLine(uint32_t line) const;
  408. void GetDescription(Stream *s, lldb::DescriptionLevel level,
  409. Target *target) const;
  410. protected:
  411. typedef std::vector<SymbolContext>
  412. collection; ///< The collection type for the list.
  413. // Member variables.
  414. collection m_symbol_contexts; ///< The list of symbol contexts.
  415. public:
  416. typedef AdaptedIterable<collection, SymbolContext, vector_adapter>
  417. SymbolContextIterable;
  418. SymbolContextIterable SymbolContexts() {
  419. return SymbolContextIterable(m_symbol_contexts);
  420. }
  421. };
  422. bool operator==(const SymbolContext &lhs, const SymbolContext &rhs);
  423. bool operator!=(const SymbolContext &lhs, const SymbolContext &rhs);
  424. bool operator==(const SymbolContextList &lhs, const SymbolContextList &rhs);
  425. bool operator!=(const SymbolContextList &lhs, const SymbolContextList &rhs);
  426. } // namespace lldb_private
  427. #endif // LLDB_SYMBOL_SYMBOLCONTEXT_H