1 //===-- RegisterContextLLDB.h --------------------------------------------*- C++ 2 //-*-===// 3 // 4 // The LLVM Compiler Infrastructure 5 // 6 // This file is distributed under the University of Illinois Open Source 7 // License. See LICENSE.TXT for details. 8 // 9 //===----------------------------------------------------------------------===// 10 11 #ifndef lldb_RegisterContextLLDB_h_ 12 #define lldb_RegisterContextLLDB_h_ 13 14 #include <vector> 15 16 #include "UnwindLLDB.h" 17 #include "lldb/Symbol/SymbolContext.h" 18 #include "lldb/Symbol/UnwindPlan.h" 19 #include "lldb/Target/RegisterContext.h" 20 #include "lldb/Target/RegisterNumber.h" 21 #include "lldb/lldb-private.h" 22 23 namespace lldb_private { 24 25 class UnwindLLDB; 26 27 class RegisterContextLLDB : public lldb_private::RegisterContext { 28 public: 29 typedef std::shared_ptr<RegisterContextLLDB> SharedPtr; 30 31 RegisterContextLLDB(lldb_private::Thread &thread, const SharedPtr &next_frame, 32 lldb_private::SymbolContext &sym_ctx, 33 uint32_t frame_number, 34 lldb_private::UnwindLLDB &unwind_lldb); 35 36 ~RegisterContextLLDB() override = default; 37 38 void InvalidateAllRegisters() override; 39 40 size_t GetRegisterCount() override; 41 42 const lldb_private::RegisterInfo *GetRegisterInfoAtIndex(size_t reg) override; 43 44 size_t GetRegisterSetCount() override; 45 46 const lldb_private::RegisterSet *GetRegisterSet(size_t reg_set) override; 47 48 bool ReadRegister(const lldb_private::RegisterInfo *reg_info, 49 lldb_private::RegisterValue &value) override; 50 51 bool WriteRegister(const lldb_private::RegisterInfo *reg_info, 52 const lldb_private::RegisterValue &value) override; 53 54 bool ReadAllRegisterValues(lldb::DataBufferSP &data_sp) override; 55 56 bool WriteAllRegisterValues(const lldb::DataBufferSP &data_sp) override; 57 58 uint32_t ConvertRegisterKindToRegisterNumber(lldb::RegisterKind kind, 59 uint32_t num) override; 60 61 bool IsValid() const; 62 63 bool IsTrapHandlerFrame() const; 64 65 bool GetCFA(lldb::addr_t &cfa); 66 67 bool GetStartPC(lldb::addr_t &start_pc); 68 69 bool ReadPC(lldb::addr_t &start_pc); 70 71 private: 72 enum FrameType { 73 eNormalFrame, 74 eTrapHandlerFrame, 75 eDebuggerFrame, // a debugger inferior function call frame; we get caller's 76 // registers from debugger 77 eSkipFrame, // The unwind resulted in a bogus frame but may get back on 78 // track so we don't want to give up yet 79 eNotAValidFrame // this frame is invalid for some reason - most likely it is 80 // past the top (end) of the stack 81 }; 82 83 // UnwindLLDB needs to pass around references to RegisterLocations 84 friend class UnwindLLDB; 85 86 // Returns true if we have an unwind loop -- the same stack frame unwinding 87 // multiple times. 88 bool CheckIfLoopingStack(); 89 90 // Indicates whether this frame is frame zero -- the currently 91 // executing frame -- or not. 92 bool IsFrameZero() const; 93 94 void InitializeZerothFrame(); 95 96 void InitializeNonZerothFrame(); 97 98 SharedPtr GetNextFrame() const; 99 100 SharedPtr GetPrevFrame() const; 101 102 // A SkipFrame occurs when the unwind out of frame 0 didn't go right -- we've 103 // got one bogus frame at frame #1. 104 // There is a good chance we'll get back on track if we follow the frame 105 // pointer chain (or whatever is appropriate 106 // on this ABI) so we allow one invalid frame to be in the stack. Ideally 107 // we'll mark this frame specially at some 108 // point and indicate to the user that the unwinder had a hiccup. Often when 109 // this happens we will miss a frame of 110 // the program's actual stack in the unwind and we want to flag that for the 111 // user somehow. 112 bool IsSkipFrame() const; 113 114 //------------------------------------------------------------------ 115 /// Determines if a SymbolContext is a trap handler or not 116 /// 117 /// Given a SymbolContext, determines if this is a trap handler function 118 /// aka asynchronous signal handler. 119 /// 120 /// @return 121 /// Returns true if the SymbolContext is a trap handler. 122 //------------------------------------------------------------------ 123 bool IsTrapHandlerSymbol(lldb_private::Process *process, 124 const lldb_private::SymbolContext &m_sym_ctx) const; 125 126 // Provide a location for where THIS function saved the CALLER's register 127 // value 128 // Or a frame "below" this one saved it, i.e. a function called by this one, 129 // preserved a register that this 130 // function didn't modify/use. 131 // 132 // The RegisterLocation type may be set to eRegisterNotAvailable -- this will 133 // happen for a volatile register 134 // being queried mid-stack. Instead of floating frame 0's contents of that 135 // register up the stack (which may 136 // or may not be the value of that reg when the function was executing), we 137 // won't return any value. 138 // 139 // If a non-volatile register (a "preserved" register) is requested mid-stack 140 // and no frames "below" the requested 141 // stack have saved the register anywhere, it is safe to assume that frame 0's 142 // register values are still the same 143 // as the requesting frame's. 144 lldb_private::UnwindLLDB::RegisterSearchResult 145 SavedLocationForRegister(uint32_t lldb_regnum, 146 lldb_private::UnwindLLDB::RegisterLocation ®loc); 147 148 bool ReadRegisterValueFromRegisterLocation( 149 lldb_private::UnwindLLDB::RegisterLocation regloc, 150 const lldb_private::RegisterInfo *reg_info, 151 lldb_private::RegisterValue &value); 152 153 bool WriteRegisterValueToRegisterLocation( 154 lldb_private::UnwindLLDB::RegisterLocation regloc, 155 const lldb_private::RegisterInfo *reg_info, 156 const lldb_private::RegisterValue &value); 157 158 //------------------------------------------------------------------ 159 /// If the unwind has to the caller frame has failed, try something else 160 /// 161 /// If lldb is using an assembly language based UnwindPlan for a frame and 162 /// the unwind to the caller frame fails, try falling back to a generic 163 /// UnwindPlan (architecture default unwindplan) to see if that might work 164 /// better. This is mostly helping to work around problems where the 165 /// assembly language inspection fails on hand-written assembly code. 166 /// 167 /// @return 168 /// Returns true if a fallback unwindplan was found & was installed. 169 //------------------------------------------------------------------ 170 bool TryFallbackUnwindPlan(); 171 172 //------------------------------------------------------------------ 173 /// Switch to the fallback unwind plan unconditionally without any safety 174 /// checks that it is providing better results than the normal unwind plan. 175 /// 176 /// The only time it is valid to call this method is if the full unwindplan is 177 /// found to be fundamentally incorrect/impossible. 178 /// 179 /// Returns true if it was able to install the fallback unwind plan. 180 //------------------------------------------------------------------ 181 bool ForceSwitchToFallbackUnwindPlan(); 182 183 // Get the contents of a general purpose (address-size) register for this 184 // frame 185 // (usually retrieved from the next frame) 186 bool ReadGPRValue(lldb::RegisterKind register_kind, uint32_t regnum, 187 lldb::addr_t &value); 188 189 bool ReadGPRValue(const RegisterNumber ®_num, lldb::addr_t &value); 190 191 // Get the Frame Address register for a given frame. 192 bool ReadFrameAddress(lldb::RegisterKind register_kind, 193 UnwindPlan::Row::FAValue &fa, lldb::addr_t &address); 194 195 lldb::UnwindPlanSP GetFastUnwindPlanForFrame(); 196 197 lldb::UnwindPlanSP GetFullUnwindPlanForFrame(); 198 199 void UnwindLogMsg(const char *fmt, ...) __attribute__((format(printf, 2, 3))); 200 201 void UnwindLogMsgVerbose(const char *fmt, ...) 202 __attribute__((format(printf, 2, 3))); 203 204 bool IsUnwindPlanValidForCurrentPC(lldb::UnwindPlanSP unwind_plan_sp, 205 int &valid_pc_offset); 206 207 lldb_private::Thread &m_thread; 208 209 /// 210 // The following tell us how to retrieve the CALLER's register values (ie the 211 // "previous" frame, aka the frame above) 212 // i.e. where THIS frame saved them 213 /// 214 215 lldb::UnwindPlanSP m_fast_unwind_plan_sp; // may be NULL 216 lldb::UnwindPlanSP m_full_unwind_plan_sp; 217 lldb::UnwindPlanSP m_fallback_unwind_plan_sp; // may be NULL 218 219 bool m_all_registers_available; // Can we retrieve all regs or just 220 // nonvolatile regs? 221 int m_frame_type; // enum FrameType 222 223 lldb::addr_t m_cfa; 224 lldb::addr_t m_afa; 225 lldb_private::Address m_start_pc; 226 lldb_private::Address m_current_pc; 227 228 int m_current_offset; // how far into the function we've executed; -1 if 229 // unknown 230 // 0 if no instructions have been executed yet. 231 232 int m_current_offset_backed_up_one; // how far into the function we've 233 // executed; -1 if unknown 234 // 0 if no instructions have been executed yet. 235 // On architectures where the return address on the stack points 236 // to the instruction after the CALL, this value will have 1 237 // subtracted from it. Else a function that ends in a CALL will 238 // have an offset pointing into the next function's address range. 239 // m_current_pc has the actual address of the "current" pc. 240 241 lldb_private::SymbolContext &m_sym_ctx; 242 bool m_sym_ctx_valid; // if ResolveSymbolContextForAddress fails, don't try to 243 // use m_sym_ctx 244 245 uint32_t m_frame_number; // What stack frame this RegisterContext is 246 247 std::map<uint32_t, lldb_private::UnwindLLDB::RegisterLocation> 248 m_registers; // where to find reg values for this frame 249 250 lldb_private::UnwindLLDB &m_parent_unwind; // The UnwindLLDB that is creating 251 // this RegisterContextLLDB 252 253 //------------------------------------------------------------------ 254 // For RegisterContextLLDB only 255 //------------------------------------------------------------------ 256 257 DISALLOW_COPY_AND_ASSIGN(RegisterContextLLDB); 258 }; 259 260 } // namespace lldb_private 261 262 #endif // lldb_RegisterContextLLDB_h_ 263