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