Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===-- UnwindLLDB.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_UNWINDLLDB_H
0010 #define LLDB_TARGET_UNWINDLLDB_H
0011 
0012 #include <vector>
0013 
0014 #include "lldb/Symbol/FuncUnwinders.h"
0015 #include "lldb/Symbol/SymbolContext.h"
0016 #include "lldb/Symbol/UnwindPlan.h"
0017 #include "lldb/Target/RegisterContext.h"
0018 #include "lldb/Target/Unwind.h"
0019 #include "lldb/Utility/ConstString.h"
0020 #include "lldb/lldb-public.h"
0021 
0022 namespace lldb_private {
0023 
0024 class RegisterContextUnwind;
0025 
0026 class UnwindLLDB : public lldb_private::Unwind {
0027 public:
0028   UnwindLLDB(lldb_private::Thread &thread);
0029 
0030   ~UnwindLLDB() override = default;
0031 
0032   enum RegisterSearchResult {
0033     eRegisterFound = 0,
0034     eRegisterNotFound,
0035     eRegisterIsVolatile
0036   };
0037 
0038 protected:
0039   friend class lldb_private::RegisterContextUnwind;
0040 
0041   /// An UnwindPlan::Row::AbstractRegisterLocation, combined with the register
0042   /// context and memory for a specific stop point, is used to create a
0043   /// ConcreteRegisterLocation.
0044   struct ConcreteRegisterLocation {
0045     enum RegisterLocationTypes {
0046       eRegisterNotSaved = 0, // register was not preserved by callee.  If
0047                              // volatile reg, is unavailable
0048       eRegisterSavedAtMemoryLocation, // register is saved at a specific word of
0049                                       // target mem (target_memory_location)
0050       eRegisterInRegister, // register is available in a (possible other)
0051                            // register (register_number)
0052       eRegisterSavedAtHostMemoryLocation, // register is saved at a word in
0053                                           // lldb's address space
0054       eRegisterValueInferred,        // register val was computed (and is in
0055                                      // inferred_value)
0056       eRegisterInLiveRegisterContext // register value is in a live (stack frame
0057                                      // #0) register
0058     };
0059     int type;
0060     union {
0061       lldb::addr_t target_memory_location;
0062       uint32_t
0063           register_number; // in eRegisterKindLLDB register numbering system
0064       void *host_memory_location;
0065       uint64_t inferred_value; // eRegisterValueInferred - e.g. stack pointer ==
0066                                // cfa + offset
0067     } location;
0068   };
0069 
0070   void DoClear() override {
0071     m_frames.clear();
0072     m_candidate_frame.reset();
0073     m_unwind_complete = false;
0074   }
0075 
0076   uint32_t DoGetFrameCount() override;
0077 
0078   bool DoGetFrameInfoAtIndex(uint32_t frame_idx, lldb::addr_t &cfa,
0079                              lldb::addr_t &start_pc,
0080                              bool &behaves_like_zeroth_frame) override;
0081 
0082   lldb::RegisterContextSP
0083   DoCreateRegisterContextForFrame(lldb_private::StackFrame *frame) override;
0084 
0085   typedef std::shared_ptr<RegisterContextUnwind> RegisterContextLLDBSP;
0086 
0087   // Needed to retrieve the "next" frame (e.g. frame 2 needs to retrieve frame
0088   // 1's RegisterContextUnwind)
0089   // The RegisterContext for frame_num must already exist or this returns an
0090   // empty shared pointer.
0091   RegisterContextLLDBSP GetRegisterContextForFrameNum(uint32_t frame_num);
0092 
0093   // Iterate over the RegisterContextUnwind's in our m_frames vector, look for
0094   // the first one that has a saved location for this reg.
0095   bool SearchForSavedLocationForRegister(
0096       uint32_t lldb_regnum,
0097       lldb_private::UnwindLLDB::ConcreteRegisterLocation &regloc,
0098       uint32_t starting_frame_num, bool pc_register);
0099 
0100   /// Provide the list of user-specified trap handler functions
0101   ///
0102   /// The Platform is one source of trap handler function names; that
0103   /// may be augmented via a setting.  The setting needs to be converted
0104   /// into an array of ConstStrings before it can be used - we only want
0105   /// to do that once per thread so it's here in the UnwindLLDB object.
0106   ///
0107   /// \return
0108   ///     Vector of ConstStrings of trap handler function names.  May be
0109   ///     empty.
0110   const std::vector<ConstString> &GetUserSpecifiedTrapHandlerFunctionNames() {
0111     return m_user_supplied_trap_handler_functions;
0112   }
0113 
0114 private:
0115   struct Cursor {
0116     lldb::addr_t start_pc =
0117         LLDB_INVALID_ADDRESS; // The start address of the function/symbol for
0118                               // this frame - current pc if unknown
0119     lldb::addr_t cfa = LLDB_INVALID_ADDRESS; // The canonical frame address for
0120                                              // this stack frame
0121     lldb_private::SymbolContext sctx; // A symbol context we'll contribute to &
0122                                       // provide to the StackFrame creation
0123     RegisterContextLLDBSP
0124         reg_ctx_lldb_sp; // These are all RegisterContextUnwind's
0125 
0126     Cursor() = default;
0127 
0128   private:
0129     Cursor(const Cursor &) = delete;
0130     const Cursor &operator=(const Cursor &) = delete;
0131   };
0132 
0133   typedef std::shared_ptr<Cursor> CursorSP;
0134   std::vector<CursorSP> m_frames;
0135   CursorSP m_candidate_frame;
0136   bool m_unwind_complete; // If this is true, we've enumerated all the frames in
0137                           // the stack, and m_frames.size() is the
0138   // number of frames, etc.  Otherwise we've only gone as far as directly asked,
0139   // and m_frames.size()
0140   // is how far we've currently gone.
0141 
0142   std::vector<ConstString> m_user_supplied_trap_handler_functions;
0143 
0144   // Check if Full UnwindPlan of First frame is valid or not.
0145   // If not then try Fallback UnwindPlan of the frame. If Fallback
0146   // UnwindPlan succeeds then update the Full UnwindPlan with the
0147   // Fallback UnwindPlan.
0148   void UpdateUnwindPlanForFirstFrameIfInvalid(ABI *abi);
0149 
0150   CursorSP GetOneMoreFrame(ABI *abi);
0151 
0152   bool AddOneMoreFrame(ABI *abi);
0153 
0154   bool AddFirstFrame();
0155 
0156   // For UnwindLLDB only
0157   UnwindLLDB(const UnwindLLDB &) = delete;
0158   const UnwindLLDB &operator=(const UnwindLLDB &) = delete;
0159 };
0160 
0161 } // namespace lldb_private
0162 
0163 #endif // LLDB_TARGET_UNWINDLLDB_H