Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:55

0001 
0002 //===-- StackFrame.h --------------------------------------------*- C++ -*-===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef LLDB_TARGET_STACKFRAME_H
0011 #define LLDB_TARGET_STACKFRAME_H
0012 
0013 #include <memory>
0014 #include <mutex>
0015 
0016 #include "lldb/Utility/Flags.h"
0017 
0018 #include "lldb/Core/FormatEntity.h"
0019 #include "lldb/Symbol/SymbolContext.h"
0020 #include "lldb/Target/ExecutionContextScope.h"
0021 #include "lldb/Target/StackID.h"
0022 #include "lldb/Utility/Scalar.h"
0023 #include "lldb/Utility/Status.h"
0024 #include "lldb/Utility/StreamString.h"
0025 #include "lldb/Utility/StructuredData.h"
0026 #include "lldb/Utility/UserID.h"
0027 #include "lldb/ValueObject/ValueObjectList.h"
0028 
0029 namespace lldb_private {
0030 
0031 /// \class StackFrame StackFrame.h "lldb/Target/StackFrame.h"
0032 ///
0033 /// This base class provides an interface to stack frames.
0034 ///
0035 /// StackFrames may have a Canonical Frame Address (CFA) or not.
0036 /// A frame may have a plain pc value or it may  indicate a specific point in
0037 /// the debug session so the correct section load list is used for
0038 /// symbolication.
0039 ///
0040 /// Local variables may be available, or not.  A register context may be
0041 /// available, or not.
0042 
0043 class StackFrame : public ExecutionContextScope,
0044                    public std::enable_shared_from_this<StackFrame> {
0045 public:
0046   enum ExpressionPathOption {
0047     eExpressionPathOptionCheckPtrVsMember = (1u << 0),
0048     eExpressionPathOptionsNoFragileObjcIvar = (1u << 1),
0049     eExpressionPathOptionsNoSyntheticChildren = (1u << 2),
0050     eExpressionPathOptionsNoSyntheticArrayRange = (1u << 3),
0051     eExpressionPathOptionsAllowDirectIVarAccess = (1u << 4),
0052     eExpressionPathOptionsInspectAnonymousUnions = (1u << 5)
0053   };
0054 
0055   enum class Kind {
0056     /// A regular stack frame with access to registers and local variables.
0057     Regular,
0058 
0059     /// A historical stack frame -- possibly without CFA or registers or
0060     /// local variables.
0061     History,
0062 
0063     /// An artificial stack frame (e.g. a synthesized result of inferring
0064     /// missing tail call frames from a backtrace) with limited support for
0065     /// local variables.
0066     Artificial
0067   };
0068 
0069   /// Construct a StackFrame object without supplying a RegisterContextSP.
0070   ///
0071   /// This is the one constructor that doesn't take a RegisterContext
0072   /// parameter.  This ctor may be called when creating a history StackFrame;
0073   /// these are used if we've collected a stack trace of pc addresses at some
0074   /// point in the past.  We may only have pc values. We may have a CFA,
0075   /// or more likely, we won't.
0076   ///
0077   /// \param [in] thread_sp
0078   ///   The Thread that this frame belongs to.
0079   ///
0080   /// \param [in] frame_idx
0081   ///   This StackFrame's frame index number in the Thread.  If inlined stack
0082   ///   frames are being created, this may differ from the concrete_frame_idx
0083   ///   which is the frame index without any inlined stack frames.
0084   ///
0085   /// \param [in] concrete_frame_idx
0086   ///   The StackFrame's frame index number in the Thread without any inlined
0087   ///   stack frames being included in the index.
0088   ///
0089   /// \param [in] cfa
0090   ///   The Canonical Frame Address (this terminology from DWARF) for this
0091   ///   stack frame.  The CFA for a stack frame does not change over the
0092   ///   span of the stack frame's existence.  It is often the value of the
0093   ///   caller's stack pointer before the call instruction into this frame's
0094   ///   function.  It is usually not the same as the frame pointer register's
0095   ///   value.
0096   ///
0097   /// \param [in] cfa_is_valid
0098   ///   A history stack frame may not have a CFA value collected.  We want to
0099   ///   distinguish between "no CFA available" and a CFA of
0100   ///   LLDB_INVALID_ADDRESS.
0101   ///
0102   /// \param [in] pc
0103   ///   The current pc value of this stack frame.
0104   ///
0105   /// \param [in] sc_ptr
0106   ///   Optionally seed the StackFrame with the SymbolContext information that
0107   ///   has
0108   ///   already been discovered.
0109   StackFrame(const lldb::ThreadSP &thread_sp, lldb::user_id_t frame_idx,
0110              lldb::user_id_t concrete_frame_idx, lldb::addr_t cfa,
0111              bool cfa_is_valid, lldb::addr_t pc, Kind frame_kind,
0112              bool behaves_like_zeroth_frame, const SymbolContext *sc_ptr);
0113 
0114   StackFrame(const lldb::ThreadSP &thread_sp, lldb::user_id_t frame_idx,
0115              lldb::user_id_t concrete_frame_idx,
0116              const lldb::RegisterContextSP &reg_context_sp, lldb::addr_t cfa,
0117              lldb::addr_t pc, bool behaves_like_zeroth_frame,
0118              const SymbolContext *sc_ptr);
0119 
0120   StackFrame(const lldb::ThreadSP &thread_sp, lldb::user_id_t frame_idx,
0121              lldb::user_id_t concrete_frame_idx,
0122              const lldb::RegisterContextSP &reg_context_sp, lldb::addr_t cfa,
0123              const Address &pc, bool behaves_like_zeroth_frame,
0124              const SymbolContext *sc_ptr);
0125 
0126   ~StackFrame() override;
0127 
0128   lldb::ThreadSP GetThread() const { return m_thread_wp.lock(); }
0129 
0130   StackID &GetStackID();
0131 
0132   /// Get an Address for the current pc value in this StackFrame.
0133   ///
0134   /// May not be the same as the actual PC value for inlined stack frames.
0135   ///
0136   /// \return
0137   ///   The Address object set to the current PC value.
0138   const Address &GetFrameCodeAddress();
0139 
0140   /// Get the current code Address suitable for symbolication,
0141   /// may not be the same as GetFrameCodeAddress().
0142   ///
0143   /// For a frame in the middle of the stack, the return-pc is the
0144   /// current code address, but for symbolication purposes the
0145   /// return address after a noreturn call may point to the next
0146   /// function, a DWARF location list entry that is a completely
0147   /// different code path, or the wrong source line.
0148   ///
0149   /// The address returned should be used for symbolication (source line,
0150   /// block, function, DWARF location entry selection) but should NOT
0151   /// be shown to the user.  It may not point to an actual instruction
0152   /// boundary.
0153   ///
0154   /// \return
0155   ///   The Address object set to the current PC value.
0156   Address GetFrameCodeAddressForSymbolication();
0157 
0158   /// Change the pc value for a given thread.
0159   ///
0160   /// Change the current pc value for the frame on this thread.
0161   ///
0162   /// \param[in] pc
0163   ///     The load address that the pc will be set to.
0164   ///
0165   /// \return
0166   ///     true if the pc was changed.  false if this failed -- possibly
0167   ///     because this frame is not a live StackFrame.
0168   bool ChangePC(lldb::addr_t pc);
0169 
0170   /// Provide a SymbolContext for this StackFrame's current pc value.
0171   ///
0172   /// The StackFrame maintains this SymbolContext and adds additional
0173   /// information to it on an as-needed basis.  This helps to avoid different
0174   /// functions looking up symbolic information for a given pc value multiple
0175   /// times.
0176   ///
0177   /// \param [in] resolve_scope
0178   ///   Flags from the SymbolContextItem enumerated type which specify what
0179   ///   type of symbol context is needed by this caller.
0180   ///
0181   /// \return
0182   ///   A SymbolContext reference which includes the types of information
0183   ///   requested by resolve_scope, if they are available.
0184   const SymbolContext &GetSymbolContext(lldb::SymbolContextItem resolve_scope);
0185 
0186   /// Return the Canonical Frame Address (DWARF term) for this frame.
0187   ///
0188   /// The CFA is typically the value of the stack pointer register before the
0189   /// call invocation is made.  It will not change during the lifetime of a
0190   /// stack frame.  It is often not the same thing as the frame pointer
0191   /// register value.
0192   ///
0193   /// Live StackFrames will always have a CFA but other types of frames may
0194   /// not be able to supply one.
0195   ///
0196   /// \param [out] value
0197   ///   The address of the CFA for this frame, if available.
0198   ///
0199   /// \return
0200   ///   If there is an error determining the CFA address, return an error
0201   ///   explaining the failure. Success otherwise.
0202   llvm::Error GetFrameBaseValue(Scalar &value);
0203 
0204   /// Get the DWARFExpressionList corresponding to the Canonical Frame Address.
0205   ///
0206   /// Often a register (bp), but sometimes a register + offset.
0207   ///
0208   /// \param [out] error_ptr
0209   ///   If there is an error determining the CFA address, this may contain a
0210   ///   string explaining the failure.
0211   ///
0212   /// \return
0213   ///   Returns the corresponding DWARF expression, or NULL.
0214   DWARFExpressionList *GetFrameBaseExpression(Status *error_ptr);
0215 
0216   /// Get the current lexical scope block for this StackFrame, if possible.
0217   ///
0218   /// If debug information is available for this stack frame, return a pointer
0219   /// to the innermost lexical Block that the frame is currently executing.
0220   ///
0221   /// \return
0222   ///   A pointer to the current Block.  nullptr is returned if this can
0223   ///   not be provided.
0224   Block *GetFrameBlock();
0225 
0226   /// Get the RegisterContext for this frame, if possible.
0227   ///
0228   /// Returns a shared pointer to the RegisterContext for this stack frame.
0229   /// Only a live StackFrame object will be able to return a RegisterContext -
0230   /// callers must be prepared for an empty shared pointer being returned.
0231   ///
0232   /// Even a live StackFrame RegisterContext may not be able to provide all
0233   /// registers.  Only the currently executing frame (frame 0) can reliably
0234   /// provide every register in the register context.
0235   ///
0236   /// \return
0237   ///   The RegisterContext shared point for this frame.
0238   lldb::RegisterContextSP GetRegisterContext();
0239 
0240   const lldb::RegisterContextSP &GetRegisterContextSP() const {
0241     return m_reg_context_sp;
0242   }
0243 
0244   /// Retrieve the list of variables that are in scope at this StackFrame's
0245   /// pc.
0246   ///
0247   /// A frame that is not live may return an empty VariableList for a given
0248   /// pc value even though variables would be available at this point if it
0249   /// were a live stack frame.
0250   ///
0251   /// \param[in] get_file_globals
0252   ///     Whether to also retrieve compilation-unit scoped variables
0253   ///     that are visible to the entire compilation unit (e.g. file
0254   ///     static in C, globals that are homed in this CU).
0255   ///
0256   /// \param [out] error_ptr
0257   ///   If there is an error in the debug information that prevents variables
0258   ///   from being fetched. \see SymbolFile::GetFrameVariableError() for full
0259   ///   details.
0260   ///
0261   /// \return
0262   ///     A pointer to a list of variables.
0263   VariableList *GetVariableList(bool get_file_globals, Status *error_ptr);
0264 
0265   /// Retrieve the list of variables that are in scope at this StackFrame's
0266   /// pc.
0267   ///
0268   /// A frame that is not live may return an empty VariableListSP for a
0269   /// given pc value even though variables would be available at this point if
0270   /// it were a live stack frame.
0271   ///
0272   /// \param[in] get_file_globals
0273   ///     Whether to also retrieve compilation-unit scoped variables
0274   ///     that are visible to the entire compilation unit (e.g. file
0275   ///     static in C, globals that are homed in this CU).
0276   ///
0277   /// \return
0278   ///     A pointer to a list of variables.
0279   lldb::VariableListSP
0280   GetInScopeVariableList(bool get_file_globals,
0281                          bool must_have_valid_location = false);
0282 
0283   /// Create a ValueObject for a variable name / pathname, possibly including
0284   /// simple dereference/child selection syntax.
0285   ///
0286   /// \param[in] var_expr
0287   ///     The string specifying a variable to base the VariableObject off
0288   ///     of.
0289   ///
0290   /// \param[in] use_dynamic
0291   ///     Whether the correct dynamic type of an object pointer should be
0292   ///     determined before creating the object, or if the static type is
0293   ///     sufficient.  One of the DynamicValueType enumerated values.
0294   ///
0295   /// \param[in] options
0296   ///     An unsigned integer of flags, values from
0297   ///     StackFrame::ExpressionPathOption
0298   ///     enum.
0299   /// \param[in] var_sp
0300   ///     A VariableSP that will be set to the variable described in the
0301   ///     var_expr path.
0302   ///
0303   /// \param[in] error
0304   ///     Record any errors encountered while evaluating var_expr.
0305   ///
0306   /// \return
0307   ///     A shared pointer to the ValueObject described by var_expr.
0308   lldb::ValueObjectSP GetValueForVariableExpressionPath(
0309       llvm::StringRef var_expr, lldb::DynamicValueType use_dynamic,
0310       uint32_t options, lldb::VariableSP &var_sp, Status &error);
0311 
0312   /// Determine whether this StackFrame has debug information available or not.
0313   ///
0314   /// \return
0315   ///    true if debug information is available for this frame (function,
0316   ///    compilation unit, block, etc.)
0317   bool HasDebugInformation();
0318 
0319   /// Return the disassembly for the instructions of this StackFrame's
0320   /// function as a single C string.
0321   ///
0322   /// \return
0323   ///    C string with the assembly instructions for this function.
0324   const char *Disassemble();
0325 
0326   /// Print a description of this frame using the provided frame format.
0327   ///
0328   /// \param[out] strm
0329   ///   The Stream to print the description to.
0330   ///
0331   /// \param[in] frame_marker
0332   ///   Optional string that will be prepended to the frame output description.
0333   ///
0334   /// \return
0335   ///   \b true if and only if dumping with the given \p format worked.
0336   bool DumpUsingFormat(Stream &strm,
0337                        const lldb_private::FormatEntity::Entry *format,
0338                        llvm::StringRef frame_marker = {});
0339 
0340   /// Print a description for this frame using the frame-format formatter
0341   /// settings. If the current frame-format settings are invalid, then the
0342   /// default formatter will be used (see \a StackFrame::Dump()).
0343   ///
0344   /// \param [in] strm
0345   ///   The Stream to print the description to.
0346   ///
0347   /// \param [in] show_unique
0348   ///   Whether to print the function arguments or not for backtrace unique.
0349   ///
0350   /// \param [in] frame_marker
0351   ///   Optional string that will be prepended to the frame output description.
0352   void DumpUsingSettingsFormat(Stream *strm, bool show_unique = false,
0353                                const char *frame_marker = nullptr);
0354 
0355   /// Print a description for this frame using a default format.
0356   ///
0357   /// \param [in] strm
0358   ///   The Stream to print the description to.
0359   ///
0360   /// \param [in] show_frame_index
0361   ///   Whether to print the frame number or not.
0362   ///
0363   /// \param [in] show_fullpaths
0364   ///   Whether to print the full source paths or just the file base name.
0365   void Dump(Stream *strm, bool show_frame_index, bool show_fullpaths);
0366 
0367   /// Print a description of this stack frame and/or the source
0368   /// context/assembly for this stack frame.
0369   ///
0370   /// \param[in] strm
0371   ///   The Stream to send the output to.
0372   ///
0373   /// \param[in] show_frame_info
0374   ///   If true, print the frame info by calling DumpUsingSettingsFormat().
0375   ///
0376   /// \param[in] show_source
0377   ///   If true, print source or disassembly as per the user's settings.
0378   ///
0379   /// \param[in] show_unique
0380   ///   If true, print using backtrace unique style, without function
0381   ///            arguments as per the user's settings.
0382   ///
0383   /// \param[in] frame_marker
0384   ///   Passed to DumpUsingSettingsFormat() for the frame info printing.
0385   ///
0386   /// \return
0387   ///   Returns true if successful.
0388   bool GetStatus(Stream &strm, bool show_frame_info, bool show_source,
0389                  bool show_unique = false, const char *frame_marker = nullptr);
0390 
0391   /// Query whether this frame is a concrete frame on the call stack, or if it
0392   /// is an inlined frame derived from the debug information and presented by
0393   /// the debugger.
0394   ///
0395   /// \return
0396   ///   true if this is an inlined frame.
0397   bool IsInlined();
0398 
0399   /// Query whether this frame is part of a historical backtrace.
0400   bool IsHistorical() const;
0401 
0402   /// Query whether this frame is artificial (e.g a synthesized result of
0403   /// inferring missing tail call frames from a backtrace). Artificial frames
0404   /// may have limited support for inspecting variables.
0405   bool IsArtificial() const;
0406 
0407   /// Query whether this frame should be hidden from backtraces. Frame
0408   /// recognizers can customize this behavior and hide distracting
0409   /// system implementation details this way.
0410   bool IsHidden();
0411 
0412   /// Language plugins can use this API to report language-specific
0413   /// runtime information about this compile unit, such as additional
0414   /// language version details or feature flags.
0415   StructuredData::ObjectSP GetLanguageSpecificData();
0416 
0417   /// Get the frame's demangled name.
0418   ///
0419   ///  /// \return
0420   ///   A C-String containing the function demangled name. Can be null.
0421   const char *GetFunctionName();
0422 
0423   /// Get the frame's demangled display name.
0424   ///
0425   ///  /// \return
0426   ///   A C-String containing the function demangled display name. Can be null.
0427   const char *GetDisplayFunctionName();
0428 
0429   /// Query this frame to find what frame it is in this Thread's
0430   /// StackFrameList.
0431   ///
0432   /// \return
0433   ///   StackFrame index 0 indicates the currently-executing function.  Inline
0434   ///   frames are included in this frame index count.
0435   uint32_t GetFrameIndex() const;
0436 
0437   /// Set this frame's synthetic frame index.
0438   void SetFrameIndex(uint32_t index) { m_frame_index = index; }
0439 
0440   /// Query this frame to find what frame it is in this Thread's
0441   /// StackFrameList, not counting inlined frames.
0442   ///
0443   /// \return
0444   ///   StackFrame index 0 indicates the currently-executing function.  Inline
0445   ///   frames are not included in this frame index count; their concrete
0446   ///   frame index will be the same as the concrete frame that they are
0447   ///   derived from.
0448   uint32_t GetConcreteFrameIndex() const { return m_concrete_frame_index; }
0449 
0450   /// Create a ValueObject for a given Variable in this StackFrame.
0451   ///
0452   /// \param [in] variable_sp
0453   ///   The Variable to base this ValueObject on
0454   ///
0455   /// \param [in] use_dynamic
0456   ///     Whether the correct dynamic type of the variable should be
0457   ///     determined before creating the ValueObject, or if the static type
0458   ///     is sufficient.  One of the DynamicValueType enumerated values.
0459   ///
0460   /// \return
0461   ///     A ValueObject for this variable.
0462   lldb::ValueObjectSP
0463   GetValueObjectForFrameVariable(const lldb::VariableSP &variable_sp,
0464                                  lldb::DynamicValueType use_dynamic);
0465 
0466   /// Query this frame to determine what the default language should be when
0467   /// parsing expressions given the execution context.
0468   ///
0469   /// \return   The language of the frame if known.
0470   SourceLanguage GetLanguage();
0471 
0472   /// Similar to GetLanguage(), but is allowed to take a potentially incorrect
0473   /// guess if exact information is not available.
0474   SourceLanguage GuessLanguage();
0475 
0476   /// Attempt to econstruct the ValueObject for a given raw address touched by
0477   /// the current instruction.  The ExpressionPath should indicate how to get
0478   /// to this value using "frame variable."
0479   ///
0480   /// \param [in] addr
0481   ///   The raw address.
0482   ///
0483   /// \return
0484   ///   The ValueObject if found.  If valid, it has a valid ExpressionPath.
0485   lldb::ValueObjectSP GuessValueForAddress(lldb::addr_t addr);
0486 
0487   /// Attempt to reconstruct the ValueObject for the address contained in a
0488   /// given register plus an offset.  The ExpressionPath should indicate how
0489   /// to get to this value using "frame variable."
0490   ///
0491   /// \param [in] reg
0492   ///   The name of the register.
0493   ///
0494   /// \param [in] offset
0495   ///   The offset from the register.  Particularly important for sp...
0496   ///
0497   /// \return
0498   ///   The ValueObject if found.  If valid, it has a valid ExpressionPath.
0499   lldb::ValueObjectSP GuessValueForRegisterAndOffset(ConstString reg,
0500                                                      int64_t offset);
0501 
0502   /// Attempt to reconstruct the ValueObject for a variable with a given \a name
0503   /// from within the current StackFrame, within the current block. The search
0504   /// for the variable starts in the deepest block corresponding to the current
0505   /// PC in the stack frame and traverse through all parent blocks stopping at
0506   /// inlined function boundaries.
0507   ///
0508   /// \param [in] name
0509   ///   The name of the variable.
0510   ///
0511   /// \return
0512   ///   The ValueObject if found.
0513   lldb::ValueObjectSP FindVariable(ConstString name);
0514 
0515   // lldb::ExecutionContextScope pure virtual functions
0516   lldb::TargetSP CalculateTarget() override;
0517 
0518   lldb::ProcessSP CalculateProcess() override;
0519 
0520   lldb::ThreadSP CalculateThread() override;
0521 
0522   lldb::StackFrameSP CalculateStackFrame() override;
0523 
0524   void CalculateExecutionContext(ExecutionContext &exe_ctx) override;
0525 
0526   lldb::RecognizedStackFrameSP GetRecognizedFrame();
0527 
0528 protected:
0529   friend class StackFrameList;
0530 
0531   void SetSymbolContextScope(SymbolContextScope *symbol_scope);
0532 
0533   void UpdateCurrentFrameFromPreviousFrame(StackFrame &prev_frame);
0534 
0535   void UpdatePreviousFrameFromCurrentFrame(StackFrame &curr_frame);
0536 
0537   bool HasCachedData() const;
0538 
0539 private:
0540   /// Private methods, called from GetValueForVariableExpressionPath.
0541   /// See that method for documentation of parameters and return value.
0542   lldb::ValueObjectSP LegacyGetValueForVariableExpressionPath(
0543       llvm::StringRef var_expr, lldb::DynamicValueType use_dynamic,
0544       uint32_t options, lldb::VariableSP &var_sp, Status &error);
0545 
0546   lldb::ValueObjectSP DILGetValueForVariableExpressionPath(
0547       llvm::StringRef var_expr, lldb::DynamicValueType use_dynamic,
0548       uint32_t options, lldb::VariableSP &var_sp, Status &error);
0549 
0550   /// For StackFrame only.
0551   /// \{
0552   lldb::ThreadWP m_thread_wp;
0553   uint32_t m_frame_index;
0554   uint32_t m_concrete_frame_index;
0555   lldb::RegisterContextSP m_reg_context_sp;
0556   StackID m_id;
0557   /// \}
0558 
0559   /// The frame code address (might not be the same as the actual PC
0560   /// for inlined frames) as a section/offset address.
0561   Address m_frame_code_addr;
0562   SymbolContext m_sc;
0563   Flags m_flags;
0564   Scalar m_frame_base;
0565   Status m_frame_base_error;
0566   uint16_t m_frame_recognizer_generation = 0;
0567   /// Does this frame have a CFA?  Different from CFA == LLDB_INVALID_ADDRESS.
0568   bool m_cfa_is_valid;
0569   Kind m_stack_frame_kind;
0570 
0571   /// Whether this frame behaves like the zeroth frame, in the sense
0572   /// that its pc value might not immediately follow a call (and thus might
0573   /// be the first address of its function). True for actual frame zero as
0574   /// well as any other frame with the same trait.
0575   bool m_behaves_like_zeroth_frame;
0576   lldb::VariableListSP m_variable_list_sp;
0577   /// Value objects for each variable in m_variable_list_sp.
0578   ValueObjectList m_variable_list_value_objects;
0579   std::optional<lldb::RecognizedStackFrameSP> m_recognized_frame_sp;
0580   StreamString m_disassembly;
0581   std::recursive_mutex m_mutex;
0582 
0583   StackFrame(const StackFrame &) = delete;
0584   const StackFrame &operator=(const StackFrame &) = delete;
0585 };
0586 
0587 } // namespace lldb_private
0588 
0589 #endif // LLDB_TARGET_STACKFRAME_H