Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===-- RegisterContextUnwind.h ---------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_TARGET_REGISTERCONTEXTUNWIND_H
0010 #define LLDB_TARGET_REGISTERCONTEXTUNWIND_H
0011 
0012 #include <vector>
0013 
0014 #include "lldb/Symbol/SymbolContext.h"
0015 #include "lldb/Symbol/UnwindPlan.h"
0016 #include "lldb/Target/RegisterContext.h"
0017 #include "lldb/Target/RegisterNumber.h"
0018 #include "lldb/Target/UnwindLLDB.h"
0019 #include "lldb/lldb-private.h"
0020 
0021 namespace lldb_private {
0022 
0023 class UnwindLLDB;
0024 
0025 class RegisterContextUnwind : public lldb_private::RegisterContext {
0026 public:
0027   typedef std::shared_ptr<RegisterContextUnwind> SharedPtr;
0028 
0029   RegisterContextUnwind(lldb_private::Thread &thread,
0030                         const SharedPtr &next_frame,
0031                         lldb_private::SymbolContext &sym_ctx,
0032                         uint32_t frame_number,
0033                         lldb_private::UnwindLLDB &unwind_lldb);
0034 
0035   ~RegisterContextUnwind() override = default;
0036 
0037   void InvalidateAllRegisters() override;
0038 
0039   size_t GetRegisterCount() override;
0040 
0041   const lldb_private::RegisterInfo *GetRegisterInfoAtIndex(size_t reg) override;
0042 
0043   size_t GetRegisterSetCount() override;
0044 
0045   const lldb_private::RegisterSet *GetRegisterSet(size_t reg_set) override;
0046 
0047   bool ReadRegister(const lldb_private::RegisterInfo *reg_info,
0048                     lldb_private::RegisterValue &value) override;
0049 
0050   bool WriteRegister(const lldb_private::RegisterInfo *reg_info,
0051                      const lldb_private::RegisterValue &value) override;
0052 
0053   bool ReadAllRegisterValues(lldb::WritableDataBufferSP &data_sp) override;
0054 
0055   bool WriteAllRegisterValues(const lldb::DataBufferSP &data_sp) override;
0056 
0057   uint32_t ConvertRegisterKindToRegisterNumber(lldb::RegisterKind kind,
0058                                                uint32_t num) override;
0059 
0060   bool IsValid() const;
0061 
0062   bool IsTrapHandlerFrame() const;
0063 
0064   bool GetCFA(lldb::addr_t &cfa);
0065 
0066   bool GetStartPC(lldb::addr_t &start_pc);
0067 
0068   bool ReadPC(lldb::addr_t &start_pc);
0069 
0070   // Indicates whether this frame *behaves* like frame zero -- the currently
0071   // executing frame -- or not.  This can be true in the middle of the stack
0072   // above asynchronous trap handlers (sigtramp) for instance.
0073   bool BehavesLikeZerothFrame() const override;
0074 
0075 private:
0076   enum FrameType {
0077     eNormalFrame,
0078     eTrapHandlerFrame,
0079     eDebuggerFrame, // a debugger inferior function call frame; we get caller's
0080                     // registers from debugger
0081     eSkipFrame,     // The unwind resulted in a bogus frame but may get back on
0082                     // track so we don't want to give up yet
0083     eNotAValidFrame // this frame is invalid for some reason - most likely it is
0084                     // past the top (end) of the stack
0085   };
0086 
0087   // UnwindLLDB needs to pass around references to ConcreteRegisterLocations
0088   friend class UnwindLLDB;
0089 
0090   // Returns true if we have an unwind loop -- the same stack frame unwinding
0091   // multiple times.
0092   bool CheckIfLoopingStack();
0093 
0094   // Indicates whether this frame is frame zero -- the currently
0095   // executing frame -- or not.
0096   bool IsFrameZero() const;
0097 
0098   void InitializeZerothFrame();
0099 
0100   void InitializeNonZerothFrame();
0101 
0102   SharedPtr GetNextFrame() const;
0103 
0104   SharedPtr GetPrevFrame() const;
0105 
0106   // A SkipFrame occurs when the unwind out of frame 0 didn't go right -- we've
0107   // got one bogus frame at frame #1.
0108   // There is a good chance we'll get back on track if we follow the frame
0109   // pointer chain (or whatever is appropriate
0110   // on this ABI) so we allow one invalid frame to be in the stack.  Ideally
0111   // we'll mark this frame specially at some
0112   // point and indicate to the user that the unwinder had a hiccup.  Often when
0113   // this happens we will miss a frame of
0114   // the program's actual stack in the unwind and we want to flag that for the
0115   // user somehow.
0116   bool IsSkipFrame() const;
0117 
0118   /// Determines if a SymbolContext is a trap handler or not
0119   ///
0120   /// Given a SymbolContext, determines if this is a trap handler function
0121   /// aka asynchronous signal handler.
0122   ///
0123   /// \return
0124   ///     Returns true if the SymbolContext is a trap handler.
0125   bool IsTrapHandlerSymbol(lldb_private::Process *process,
0126                            const lldb_private::SymbolContext &m_sym_ctx) const;
0127 
0128   /// Check if the given unwind plan indicates a signal trap handler, and
0129   /// update frame type and symbol context if so.
0130   void PropagateTrapHandlerFlagFromUnwindPlan(lldb::UnwindPlanSP unwind_plan);
0131 
0132   // Provide a location for where THIS function saved the CALLER's register
0133   // value
0134   // Or a frame "below" this one saved it, i.e. a function called by this one,
0135   // preserved a register that this
0136   // function didn't modify/use.
0137   //
0138   // The ConcreteRegisterLocation type may be set to eRegisterNotAvailable --
0139   // this will happen for a volatile register being queried mid-stack.  Instead
0140   // of floating frame 0's contents of that register up the stack (which may or
0141   // may not be the value of that reg when the function was executing), we won't
0142   // return any value.
0143   //
0144   // If a non-volatile register (a "preserved" register) is requested mid-stack
0145   // and no frames "below" the requested
0146   // stack have saved the register anywhere, it is safe to assume that frame 0's
0147   // register values are still the same
0148   // as the requesting frame's.
0149   lldb_private::UnwindLLDB::RegisterSearchResult SavedLocationForRegister(
0150       uint32_t lldb_regnum,
0151       lldb_private::UnwindLLDB::ConcreteRegisterLocation &regloc);
0152 
0153   bool ReadRegisterValueFromRegisterLocation(
0154       lldb_private::UnwindLLDB::ConcreteRegisterLocation regloc,
0155       const lldb_private::RegisterInfo *reg_info,
0156       lldb_private::RegisterValue &value);
0157 
0158   bool WriteRegisterValueToRegisterLocation(
0159       lldb_private::UnwindLLDB::ConcreteRegisterLocation regloc,
0160       const lldb_private::RegisterInfo *reg_info,
0161       const lldb_private::RegisterValue &value);
0162 
0163   /// If the unwind has to the caller frame has failed, try something else
0164   ///
0165   /// If lldb is using an assembly language based UnwindPlan for a frame and
0166   /// the unwind to the caller frame fails, try falling back to a generic
0167   /// UnwindPlan (architecture default unwindplan) to see if that might work
0168   /// better.  This is mostly helping to work around problems where the
0169   /// assembly language inspection fails on hand-written assembly code.
0170   ///
0171   /// \return
0172   ///     Returns true if a fallback unwindplan was found & was installed.
0173   bool TryFallbackUnwindPlan();
0174 
0175   /// Switch to the fallback unwind plan unconditionally without any safety
0176   /// checks that it is providing better results than the normal unwind plan.
0177   ///
0178   /// The only time it is valid to call this method is if the full unwindplan is
0179   /// found to be fundamentally incorrect/impossible.
0180   ///
0181   /// Returns true if it was able to install the fallback unwind plan.
0182   bool ForceSwitchToFallbackUnwindPlan();
0183 
0184   // Get the contents of a general purpose (address-size) register for this
0185   // frame
0186   // (usually retrieved from the next frame)
0187   bool ReadGPRValue(lldb::RegisterKind register_kind, uint32_t regnum,
0188                     lldb::addr_t &value);
0189 
0190   bool ReadGPRValue(const RegisterNumber &reg_num, lldb::addr_t &value);
0191 
0192   // Get the Frame Address register for a given frame.
0193   bool ReadFrameAddress(lldb::RegisterKind register_kind,
0194                           UnwindPlan::Row::FAValue &fa, lldb::addr_t &address);
0195 
0196   lldb::UnwindPlanSP GetFastUnwindPlanForFrame();
0197 
0198   lldb::UnwindPlanSP GetFullUnwindPlanForFrame();
0199 
0200   void UnwindLogMsg(const char *fmt, ...) __attribute__((format(printf, 2, 3)));
0201 
0202   void UnwindLogMsgVerbose(const char *fmt, ...)
0203       __attribute__((format(printf, 2, 3)));
0204 
0205   bool IsUnwindPlanValidForCurrentPC(lldb::UnwindPlanSP unwind_plan_sp);
0206 
0207   lldb::addr_t GetReturnAddressHint(int32_t plan_offset);
0208 
0209   lldb_private::Thread &m_thread;
0210 
0211   ///
0212   // The following tell us how to retrieve the CALLER's register values (ie the
0213   // "previous" frame, aka the frame above)
0214   // i.e. where THIS frame saved them
0215   ///
0216 
0217   lldb::UnwindPlanSP m_fast_unwind_plan_sp; // may be NULL
0218   lldb::UnwindPlanSP m_full_unwind_plan_sp;
0219   lldb::UnwindPlanSP m_fallback_unwind_plan_sp; // may be NULL
0220 
0221   bool m_all_registers_available; // Can we retrieve all regs or just
0222                                   // nonvolatile regs?
0223   int m_frame_type;               // enum FrameType
0224 
0225   lldb::addr_t m_cfa;
0226   lldb::addr_t m_afa;
0227   lldb_private::Address m_start_pc;
0228   lldb_private::Address m_current_pc;
0229 
0230   int m_current_offset; // how far into the function we've executed; -1 if
0231                         // unknown
0232                         // 0 if no instructions have been executed yet.
0233 
0234   // 0 if no instructions have been executed yet.
0235   // On architectures where the return address on the stack points
0236   // to the instruction after the CALL, this value will have 1
0237   // subtracted from it.  Else a function that ends in a CALL will
0238   // have an offset pointing into the next function's address range.
0239   // m_current_pc has the actual address of the "current" pc.
0240   int m_current_offset_backed_up_one; // how far into the function we've
0241                                       // executed; -1 if unknown
0242 
0243   bool m_behaves_like_zeroth_frame; // this frame behaves like frame zero
0244 
0245   lldb_private::SymbolContext &m_sym_ctx;
0246   bool m_sym_ctx_valid; // if ResolveSymbolContextForAddress fails, don't try to
0247                         // use m_sym_ctx
0248 
0249   uint32_t m_frame_number; // What stack frame this RegisterContext is
0250 
0251   std::map<uint32_t, lldb_private::UnwindLLDB::ConcreteRegisterLocation>
0252       m_registers; // where to find reg values for this frame
0253 
0254   lldb_private::UnwindLLDB &m_parent_unwind; // The UnwindLLDB that is creating
0255                                              // this RegisterContextUnwind
0256 
0257   RegisterContextUnwind(const RegisterContextUnwind &) = delete;
0258   const RegisterContextUnwind &
0259   operator=(const RegisterContextUnwind &) = delete;
0260 };
0261 
0262 } // namespace lldb_private
0263 
0264 #endif // LLDB_TARGET_REGISTERCONTEXTUNWIND_H