StackFrame.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. //===-- StackFrame.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_TARGET_STACKFRAME_H
  9. #define LLDB_TARGET_STACKFRAME_H
  10. #include <memory>
  11. #include <mutex>
  12. #include "lldb/Utility/Flags.h"
  13. #include "lldb/Core/ValueObjectList.h"
  14. #include "lldb/Symbol/SymbolContext.h"
  15. #include "lldb/Target/ExecutionContextScope.h"
  16. #include "lldb/Target/StackID.h"
  17. #include "lldb/Utility/Scalar.h"
  18. #include "lldb/Utility/Status.h"
  19. #include "lldb/Utility/StreamString.h"
  20. #include "lldb/Utility/UserID.h"
  21. namespace lldb_private {
  22. /// \class StackFrame StackFrame.h "lldb/Target/StackFrame.h"
  23. ///
  24. /// This base class provides an interface to stack frames.
  25. ///
  26. /// StackFrames may have a Canonical Frame Address (CFA) or not.
  27. /// A frame may have a plain pc value or it may indicate a specific point in
  28. /// the debug session so the correct section load list is used for
  29. /// symbolication.
  30. ///
  31. /// Local variables may be available, or not. A register context may be
  32. /// available, or not.
  33. class StackFrame : public ExecutionContextScope,
  34. public std::enable_shared_from_this<StackFrame> {
  35. public:
  36. enum ExpressionPathOption {
  37. eExpressionPathOptionCheckPtrVsMember = (1u << 0),
  38. eExpressionPathOptionsNoFragileObjcIvar = (1u << 1),
  39. eExpressionPathOptionsNoSyntheticChildren = (1u << 2),
  40. eExpressionPathOptionsNoSyntheticArrayRange = (1u << 3),
  41. eExpressionPathOptionsAllowDirectIVarAccess = (1u << 4),
  42. eExpressionPathOptionsInspectAnonymousUnions = (1u << 5)
  43. };
  44. enum class Kind {
  45. /// A regular stack frame with access to registers and local variables.
  46. Regular,
  47. /// A historical stack frame -- possibly without CFA or registers or
  48. /// local variables.
  49. History,
  50. /// An artificial stack frame (e.g. a synthesized result of inferring
  51. /// missing tail call frames from a backtrace) with limited support for
  52. /// local variables.
  53. Artificial
  54. };
  55. /// Construct a StackFrame object without supplying a RegisterContextSP.
  56. ///
  57. /// This is the one constructor that doesn't take a RegisterContext
  58. /// parameter. This ctor may be called when creating a history StackFrame;
  59. /// these are used if we've collected a stack trace of pc addresses at some
  60. /// point in the past. We may only have pc values. We may have a CFA,
  61. /// or more likely, we won't.
  62. ///
  63. /// \param [in] thread_sp
  64. /// The Thread that this frame belongs to.
  65. ///
  66. /// \param [in] frame_idx
  67. /// This StackFrame's frame index number in the Thread. If inlined stack
  68. /// frames are being created, this may differ from the concrete_frame_idx
  69. /// which is the frame index without any inlined stack frames.
  70. ///
  71. /// \param [in] concrete_frame_idx
  72. /// The StackFrame's frame index number in the Thread without any inlined
  73. /// stack frames being included in the index.
  74. ///
  75. /// \param [in] cfa
  76. /// The Canonical Frame Address (this terminology from DWARF) for this
  77. /// stack frame. The CFA for a stack frame does not change over the
  78. /// span of the stack frame's existence. It is often the value of the
  79. /// caller's stack pointer before the call instruction into this frame's
  80. /// function. It is usually not the same as the frame pointer register's
  81. /// value.
  82. ///
  83. /// \param [in] cfa_is_valid
  84. /// A history stack frame may not have a CFA value collected. We want to
  85. /// distinguish between "no CFA available" and a CFA of
  86. /// LLDB_INVALID_ADDRESS.
  87. ///
  88. /// \param [in] pc
  89. /// The current pc value of this stack frame.
  90. ///
  91. /// \param [in] sc_ptr
  92. /// Optionally seed the StackFrame with the SymbolContext information that
  93. /// has
  94. /// already been discovered.
  95. StackFrame(const lldb::ThreadSP &thread_sp, lldb::user_id_t frame_idx,
  96. lldb::user_id_t concrete_frame_idx, lldb::addr_t cfa,
  97. bool cfa_is_valid, lldb::addr_t pc, Kind frame_kind,
  98. bool behaves_like_zeroth_frame, const SymbolContext *sc_ptr);
  99. StackFrame(const lldb::ThreadSP &thread_sp, lldb::user_id_t frame_idx,
  100. lldb::user_id_t concrete_frame_idx,
  101. const lldb::RegisterContextSP &reg_context_sp, lldb::addr_t cfa,
  102. lldb::addr_t pc, bool behaves_like_zeroth_frame,
  103. const SymbolContext *sc_ptr);
  104. StackFrame(const lldb::ThreadSP &thread_sp, lldb::user_id_t frame_idx,
  105. lldb::user_id_t concrete_frame_idx,
  106. const lldb::RegisterContextSP &reg_context_sp, lldb::addr_t cfa,
  107. const Address &pc, bool behaves_like_zeroth_frame,
  108. const SymbolContext *sc_ptr);
  109. ~StackFrame() override;
  110. lldb::ThreadSP GetThread() const { return m_thread_wp.lock(); }
  111. StackID &GetStackID();
  112. /// Get an Address for the current pc value in this StackFrame.
  113. ///
  114. /// May not be the same as the actual PC value for inlined stack frames.
  115. ///
  116. /// \return
  117. /// The Address object set to the current PC value.
  118. const Address &GetFrameCodeAddress();
  119. /// Get the current code Address suitable for symbolication,
  120. /// may not be the same as GetFrameCodeAddress().
  121. ///
  122. /// For a frame in the middle of the stack, the return-pc is the
  123. /// current code address, but for symbolication purposes the
  124. /// return address after a noreturn call may point to the next
  125. /// function, a DWARF location list entry that is a completely
  126. /// different code path, or the wrong source line.
  127. ///
  128. /// The address returned should be used for symbolication (source line,
  129. /// block, function, DWARF location entry selection) but should NOT
  130. /// be shown to the user. It may not point to an actual instruction
  131. /// boundary.
  132. ///
  133. /// \return
  134. /// The Address object set to the current PC value.
  135. Address GetFrameCodeAddressForSymbolication();
  136. /// Change the pc value for a given thread.
  137. ///
  138. /// Change the current pc value for the frame on this thread.
  139. ///
  140. /// \param[in] pc
  141. /// The load address that the pc will be set to.
  142. ///
  143. /// \return
  144. /// true if the pc was changed. false if this failed -- possibly
  145. /// because this frame is not a live StackFrame.
  146. bool ChangePC(lldb::addr_t pc);
  147. /// Provide a SymbolContext for this StackFrame's current pc value.
  148. ///
  149. /// The StackFrame maintains this SymbolContext and adds additional
  150. /// information to it on an as-needed basis. This helps to avoid different
  151. /// functions looking up symbolic information for a given pc value multiple
  152. /// times.
  153. ///
  154. /// \params [in] resolve_scope
  155. /// Flags from the SymbolContextItem enumerated type which specify what
  156. /// type of symbol context is needed by this caller.
  157. ///
  158. /// \return
  159. /// A SymbolContext reference which includes the types of information
  160. /// requested by resolve_scope, if they are available.
  161. const SymbolContext &GetSymbolContext(lldb::SymbolContextItem resolve_scope);
  162. /// Return the Canonical Frame Address (DWARF term) for this frame.
  163. ///
  164. /// The CFA is typically the value of the stack pointer register before the
  165. /// call invocation is made. It will not change during the lifetime of a
  166. /// stack frame. It is often not the same thing as the frame pointer
  167. /// register value.
  168. ///
  169. /// Live StackFrames will always have a CFA but other types of frames may
  170. /// not be able to supply one.
  171. ///
  172. /// \param [out] value
  173. /// The address of the CFA for this frame, if available.
  174. ///
  175. /// \param [out] error_ptr
  176. /// If there is an error determining the CFA address, this may contain a
  177. /// string explaining the failure.
  178. ///
  179. /// \return
  180. /// Returns true if the CFA value was successfully set in value. Some
  181. /// frames may be unable to provide this value; they will return false.
  182. bool GetFrameBaseValue(Scalar &value, Status *error_ptr);
  183. /// Get the DWARFExpression corresponding to the Canonical Frame Address.
  184. ///
  185. /// Often a register (bp), but sometimes a register + offset.
  186. ///
  187. /// \param [out] error_ptr
  188. /// If there is an error determining the CFA address, this may contain a
  189. /// string explaining the failure.
  190. ///
  191. /// \return
  192. /// Returns the corresponding DWARF expression, or NULL.
  193. DWARFExpression *GetFrameBaseExpression(Status *error_ptr);
  194. /// Get the current lexical scope block for this StackFrame, if possible.
  195. ///
  196. /// If debug information is available for this stack frame, return a pointer
  197. /// to the innermost lexical Block that the frame is currently executing.
  198. ///
  199. /// \return
  200. /// A pointer to the current Block. nullptr is returned if this can
  201. /// not be provided.
  202. Block *GetFrameBlock();
  203. /// Get the RegisterContext for this frame, if possible.
  204. ///
  205. /// Returns a shared pointer to the RegisterContext for this stack frame.
  206. /// Only a live StackFrame object will be able to return a RegisterContext -
  207. /// callers must be prepared for an empty shared pointer being returned.
  208. ///
  209. /// Even a live StackFrame RegisterContext may not be able to provide all
  210. /// registers. Only the currently executing frame (frame 0) can reliably
  211. /// provide every register in the register context.
  212. ///
  213. /// \return
  214. /// The RegisterContext shared point for this frame.
  215. lldb::RegisterContextSP GetRegisterContext();
  216. const lldb::RegisterContextSP &GetRegisterContextSP() const {
  217. return m_reg_context_sp;
  218. }
  219. /// Retrieve the list of variables that are in scope at this StackFrame's
  220. /// pc.
  221. ///
  222. /// A frame that is not live may return an empty VariableList for a given
  223. /// pc value even though variables would be available at this point if it
  224. /// were a live stack frame.
  225. ///
  226. /// \param[in] get_file_globals
  227. /// Whether to also retrieve compilation-unit scoped variables
  228. /// that are visible to the entire compilation unit (e.g. file
  229. /// static in C, globals that are homed in this CU).
  230. ///
  231. /// \return
  232. /// A pointer to a list of variables.
  233. VariableList *GetVariableList(bool get_file_globals);
  234. /// Retrieve the list of variables that are in scope at this StackFrame's
  235. /// pc.
  236. ///
  237. /// A frame that is not live may return an empty VariableListSP for a
  238. /// given pc value even though variables would be available at this point if
  239. /// it were a live stack frame.
  240. ///
  241. /// \param[in] get_file_globals
  242. /// Whether to also retrieve compilation-unit scoped variables
  243. /// that are visible to the entire compilation unit (e.g. file
  244. /// static in C, globals that are homed in this CU).
  245. ///
  246. /// \return
  247. /// A pointer to a list of variables.
  248. lldb::VariableListSP
  249. GetInScopeVariableList(bool get_file_globals,
  250. bool must_have_valid_location = false);
  251. /// Create a ValueObject for a variable name / pathname, possibly including
  252. /// simple dereference/child selection syntax.
  253. ///
  254. /// \param[in] var_expr
  255. /// The string specifying a variable to base the VariableObject off
  256. /// of.
  257. ///
  258. /// \param[in] use_dynamic
  259. /// Whether the correct dynamic type of an object pointer should be
  260. /// determined before creating the object, or if the static type is
  261. /// sufficient. One of the DynamicValueType enumerated values.
  262. ///
  263. /// \param[in] options
  264. /// An unsigned integer of flags, values from
  265. /// StackFrame::ExpressionPathOption
  266. /// enum.
  267. /// \param[in] var_sp
  268. /// A VariableSP that will be set to the variable described in the
  269. /// var_expr path.
  270. ///
  271. /// \param[in] error
  272. /// Record any errors encountered while evaluating var_expr.
  273. ///
  274. /// \return
  275. /// A shared pointer to the ValueObject described by var_expr.
  276. lldb::ValueObjectSP GetValueForVariableExpressionPath(
  277. llvm::StringRef var_expr, lldb::DynamicValueType use_dynamic,
  278. uint32_t options, lldb::VariableSP &var_sp, Status &error);
  279. /// Determine whether this StackFrame has debug information available or not.
  280. ///
  281. /// \return
  282. /// true if debug information is available for this frame (function,
  283. /// compilation unit, block, etc.)
  284. bool HasDebugInformation();
  285. /// Return the disassembly for the instructions of this StackFrame's
  286. /// function as a single C string.
  287. ///
  288. /// \return
  289. /// C string with the assembly instructions for this function.
  290. const char *Disassemble();
  291. /// Print a description for this frame using the frame-format formatter
  292. /// settings.
  293. ///
  294. /// \param [in] strm
  295. /// The Stream to print the description to.
  296. ///
  297. /// \param [in] show_unique
  298. /// Whether to print the function arguments or not for backtrace unique.
  299. ///
  300. /// \param [in] frame_marker
  301. /// Optional string that will be prepended to the frame output description.
  302. void DumpUsingSettingsFormat(Stream *strm, bool show_unique = false,
  303. const char *frame_marker = nullptr);
  304. /// Print a description for this frame using a default format.
  305. ///
  306. /// \param [in] strm
  307. /// The Stream to print the description to.
  308. ///
  309. /// \param [in] show_frame_index
  310. /// Whether to print the frame number or not.
  311. ///
  312. /// \param [in] show_fullpaths
  313. /// Whether to print the full source paths or just the file base name.
  314. void Dump(Stream *strm, bool show_frame_index, bool show_fullpaths);
  315. /// Print a description of this stack frame and/or the source
  316. /// context/assembly for this stack frame.
  317. ///
  318. /// \param[in] strm
  319. /// The Stream to send the output to.
  320. ///
  321. /// \param[in] show_frame_info
  322. /// If true, print the frame info by calling DumpUsingSettingsFormat().
  323. ///
  324. /// \param[in] show_source
  325. /// If true, print source or disassembly as per the user's settings.
  326. ///
  327. /// \param[in] show_unique
  328. /// If true, print using backtrace unique style, without function
  329. /// arguments as per the user's settings.
  330. ///
  331. /// \param[in] frame_marker
  332. /// Passed to DumpUsingSettingsFormat() for the frame info printing.
  333. ///
  334. /// \return
  335. /// Returns true if successful.
  336. bool GetStatus(Stream &strm, bool show_frame_info, bool show_source,
  337. bool show_unique = false, const char *frame_marker = nullptr);
  338. /// Query whether this frame is a concrete frame on the call stack, or if it
  339. /// is an inlined frame derived from the debug information and presented by
  340. /// the debugger.
  341. ///
  342. /// \return
  343. /// true if this is an inlined frame.
  344. bool IsInlined();
  345. /// Query whether this frame is part of a historical backtrace.
  346. bool IsHistorical() const;
  347. /// Query whether this frame is artificial (e.g a synthesized result of
  348. /// inferring missing tail call frames from a backtrace). Artificial frames
  349. /// may have limited support for inspecting variables.
  350. bool IsArtificial() const;
  351. /// Query this frame to find what frame it is in this Thread's
  352. /// StackFrameList.
  353. ///
  354. /// \return
  355. /// StackFrame index 0 indicates the currently-executing function. Inline
  356. /// frames are included in this frame index count.
  357. uint32_t GetFrameIndex() const;
  358. /// Set this frame's synthetic frame index.
  359. void SetFrameIndex(uint32_t index) { m_frame_index = index; }
  360. /// Query this frame to find what frame it is in this Thread's
  361. /// StackFrameList, not counting inlined frames.
  362. ///
  363. /// \return
  364. /// StackFrame index 0 indicates the currently-executing function. Inline
  365. /// frames are not included in this frame index count; their concrete
  366. /// frame index will be the same as the concrete frame that they are
  367. /// derived from.
  368. uint32_t GetConcreteFrameIndex() const { return m_concrete_frame_index; }
  369. /// Create a ValueObject for a given Variable in this StackFrame.
  370. ///
  371. /// \params [in] variable_sp
  372. /// The Variable to base this ValueObject on
  373. ///
  374. /// \params [in] use_dynamic
  375. /// Whether the correct dynamic type of the variable should be
  376. /// determined before creating the ValueObject, or if the static type
  377. /// is sufficient. One of the DynamicValueType enumerated values.
  378. ///
  379. /// \return
  380. /// A ValueObject for this variable.
  381. lldb::ValueObjectSP
  382. GetValueObjectForFrameVariable(const lldb::VariableSP &variable_sp,
  383. lldb::DynamicValueType use_dynamic);
  384. /// Query this frame to determine what the default language should be when
  385. /// parsing expressions given the execution context.
  386. ///
  387. /// \return
  388. /// The language of the frame if known, else lldb::eLanguageTypeUnknown.
  389. lldb::LanguageType GetLanguage();
  390. // similar to GetLanguage(), but is allowed to take a potentially incorrect
  391. // guess if exact information is not available
  392. lldb::LanguageType GuessLanguage();
  393. /// Attempt to econstruct the ValueObject for a given raw address touched by
  394. /// the current instruction. The ExpressionPath should indicate how to get
  395. /// to this value using "frame variable."
  396. ///
  397. /// \params [in] addr
  398. /// The raw address.
  399. ///
  400. /// \return
  401. /// The ValueObject if found. If valid, it has a valid ExpressionPath.
  402. lldb::ValueObjectSP GuessValueForAddress(lldb::addr_t addr);
  403. /// Attempt to reconstruct the ValueObject for the address contained in a
  404. /// given register plus an offset. The ExpressionPath should indicate how
  405. /// to get to this value using "frame variable."
  406. ///
  407. /// \params [in] reg
  408. /// The name of the register.
  409. ///
  410. /// \params [in] offset
  411. /// The offset from the register. Particularly important for sp...
  412. ///
  413. /// \return
  414. /// The ValueObject if found. If valid, it has a valid ExpressionPath.
  415. lldb::ValueObjectSP GuessValueForRegisterAndOffset(ConstString reg,
  416. int64_t offset);
  417. /// Attempt to reconstruct the ValueObject for a variable with a given \a name
  418. /// from within the current StackFrame, within the current block. The search
  419. /// for the variable starts in the deepest block corresponding to the current
  420. /// PC in the stack frame and traverse through all parent blocks stopping at
  421. /// inlined function boundaries.
  422. ///
  423. /// \params [in] name
  424. /// The name of the variable.
  425. ///
  426. /// \return
  427. /// The ValueObject if found.
  428. lldb::ValueObjectSP FindVariable(ConstString name);
  429. // lldb::ExecutionContextScope pure virtual functions
  430. lldb::TargetSP CalculateTarget() override;
  431. lldb::ProcessSP CalculateProcess() override;
  432. lldb::ThreadSP CalculateThread() override;
  433. lldb::StackFrameSP CalculateStackFrame() override;
  434. void CalculateExecutionContext(ExecutionContext &exe_ctx) override;
  435. lldb::RecognizedStackFrameSP GetRecognizedFrame();
  436. protected:
  437. friend class StackFrameList;
  438. void SetSymbolContextScope(SymbolContextScope *symbol_scope);
  439. void UpdateCurrentFrameFromPreviousFrame(StackFrame &prev_frame);
  440. void UpdatePreviousFrameFromCurrentFrame(StackFrame &curr_frame);
  441. bool HasCachedData() const;
  442. private:
  443. // For StackFrame only
  444. lldb::ThreadWP m_thread_wp;
  445. uint32_t m_frame_index;
  446. uint32_t m_concrete_frame_index;
  447. lldb::RegisterContextSP m_reg_context_sp;
  448. StackID m_id;
  449. Address m_frame_code_addr; // The frame code address (might not be the same as
  450. // the actual PC for inlined frames) as a
  451. // section/offset address
  452. SymbolContext m_sc;
  453. Flags m_flags;
  454. Scalar m_frame_base;
  455. Status m_frame_base_error;
  456. bool m_cfa_is_valid; // Does this frame have a CFA? Different from CFA ==
  457. // LLDB_INVALID_ADDRESS
  458. Kind m_stack_frame_kind;
  459. // Whether this frame behaves like the zeroth frame, in the sense
  460. // that its pc value might not immediately follow a call (and thus might
  461. // be the first address of its function). True for actual frame zero as
  462. // well as any other frame with the same trait.
  463. bool m_behaves_like_zeroth_frame;
  464. lldb::VariableListSP m_variable_list_sp;
  465. ValueObjectList m_variable_list_value_objects; // Value objects for each
  466. // variable in
  467. // m_variable_list_sp
  468. lldb::RecognizedStackFrameSP m_recognized_frame_sp;
  469. StreamString m_disassembly;
  470. std::recursive_mutex m_mutex;
  471. StackFrame(const StackFrame &) = delete;
  472. const StackFrame &operator=(const StackFrame &) = delete;
  473. };
  474. } // namespace lldb_private
  475. #endif // LLDB_TARGET_STACKFRAME_H