1 //===-- RegisterContextLLDB.h --------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef lldb_RegisterContextLLDB_h_
11 #define lldb_RegisterContextLLDB_h_
17 // Other libraries and framework includes
19 #include "lldb/lldb-private.h"
20 #include "lldb/Target/RegisterContext.h"
21 #include "lldb/Symbol/UnwindPlan.h"
22 #include "lldb/Symbol/SymbolContext.h"
23 #include "lldb/Utility/RegisterNumber.h"
24 #include "UnwindLLDB.h"
26 namespace lldb_private {
30 class RegisterContextLLDB : public lldb_private::RegisterContext
33 typedef std::shared_ptr<RegisterContextLLDB> SharedPtr;
35 RegisterContextLLDB (lldb_private::Thread &thread,
36 const SharedPtr& next_frame,
37 lldb_private::SymbolContext& sym_ctx,
38 uint32_t frame_number, lldb_private::UnwindLLDB& unwind_lldb);
40 ~RegisterContextLLDB() override = default;
43 InvalidateAllRegisters() override;
46 GetRegisterCount() override;
48 const lldb_private::RegisterInfo *
49 GetRegisterInfoAtIndex(size_t reg) override;
52 GetRegisterSetCount() override;
54 const lldb_private::RegisterSet *
55 GetRegisterSet(size_t reg_set) override;
58 ReadRegister(const lldb_private::RegisterInfo *reg_info,
59 lldb_private::RegisterValue &value) override;
62 WriteRegister(const lldb_private::RegisterInfo *reg_info,
63 const lldb_private::RegisterValue &value) override;
66 ReadAllRegisterValues(lldb::DataBufferSP &data_sp) override;
69 WriteAllRegisterValues(const lldb::DataBufferSP &data_sp) override;
72 ConvertRegisterKindToRegisterNumber(lldb::RegisterKind kind, uint32_t num) override;
78 IsTrapHandlerFrame () const;
81 GetCFA (lldb::addr_t& cfa);
84 GetStartPC (lldb::addr_t& start_pc);
87 ReadPC (lldb::addr_t& start_pc);
95 eDebuggerFrame, // a debugger inferior function call frame; we get caller's registers from debugger
96 eSkipFrame, // The unwind resulted in a bogus frame but may get back on track so we don't want to give up yet
97 eNotAValidFrame // this frame is invalid for some reason - most likely it is past the top (end) of the stack
100 // UnwindLLDB needs to pass around references to RegisterLocations
101 friend class UnwindLLDB;
103 // Returns true if we have an unwind loop -- the same stack frame unwinding
106 CheckIfLoopingStack ();
108 // Indicates whether this frame is frame zero -- the currently
109 // executing frame -- or not.
111 IsFrameZero () const;
114 InitializeZerothFrame ();
117 InitializeNonZerothFrame();
120 GetNextFrame () const;
123 GetPrevFrame () const;
125 // A SkipFrame occurs when the unwind out of frame 0 didn't go right -- we've got one bogus frame at frame #1.
126 // There is a good chance we'll get back on track if we follow the frame pointer chain (or whatever is appropriate
127 // on this ABI) so we allow one invalid frame to be in the stack. Ideally we'll mark this frame specially at some
128 // point and indicate to the user that the unwinder had a hiccup. Often when this happens we will miss a frame of
129 // the program's actual stack in the unwind and we want to flag that for the user somehow.
131 IsSkipFrame () const;
133 //------------------------------------------------------------------
134 /// Determines if a SymbolContext is a trap handler or not
136 /// Given a SymbolContext, determines if this is a trap handler function
137 /// aka asynchronous signal handler.
140 /// Returns true if the SymbolContext is a trap handler.
141 //------------------------------------------------------------------
143 IsTrapHandlerSymbol (lldb_private::Process *process, const lldb_private::SymbolContext &m_sym_ctx) const;
145 // Provide a location for where THIS function saved the CALLER's register value
146 // Or a frame "below" this one saved it, i.e. a function called by this one, preserved a register that this
147 // function didn't modify/use.
149 // The RegisterLocation type may be set to eRegisterNotAvailable -- this will happen for a volatile register
150 // being queried mid-stack. Instead of floating frame 0's contents of that register up the stack (which may
151 // or may not be the value of that reg when the function was executing), we won't return any value.
153 // If a non-volatile register (a "preserved" register) is requested mid-stack and no frames "below" the requested
154 // stack have saved the register anywhere, it is safe to assume that frame 0's register values are still the same
155 // as the requesting frame's.
156 lldb_private::UnwindLLDB::RegisterSearchResult
157 SavedLocationForRegister (uint32_t lldb_regnum, lldb_private::UnwindLLDB::RegisterLocation ®loc);
160 ReadRegisterValueFromRegisterLocation (lldb_private::UnwindLLDB::RegisterLocation regloc,
161 const lldb_private::RegisterInfo *reg_info,
162 lldb_private::RegisterValue &value);
165 WriteRegisterValueToRegisterLocation (lldb_private::UnwindLLDB::RegisterLocation regloc,
166 const lldb_private::RegisterInfo *reg_info,
167 const lldb_private::RegisterValue &value);
169 //------------------------------------------------------------------
170 /// If the unwind has to the caller frame has failed, try something else
172 /// If lldb is using an assembly language based UnwindPlan for a frame and
173 /// the unwind to the caller frame fails, try falling back to a generic
174 /// UnwindPlan (architecture default unwindplan) to see if that might work
175 /// better. This is mostly helping to work around problems where the
176 /// assembly language inspection fails on hand-written assembly code.
179 /// Returns true if a fallback unwindplan was found & was installed.
180 //------------------------------------------------------------------
182 TryFallbackUnwindPlan ();
184 //------------------------------------------------------------------
185 /// Switch to the fallback unwind plan unconditionally without any safety
186 /// checks that it is providing better results than the normal unwind plan.
188 /// The only time it is valid to call this method is if the full unwindplan is
189 /// found to be fundamentally incorrect/impossible.
191 /// Returns true if it was able to install the fallback unwind plan.
192 //------------------------------------------------------------------
194 ForceSwitchToFallbackUnwindPlan ();
196 // Get the contents of a general purpose (address-size) register for this frame
197 // (usually retrieved from the next frame)
199 ReadGPRValue (lldb::RegisterKind register_kind, uint32_t regnum, lldb::addr_t &value);
202 ReadGPRValue (const RegisterNumber ®_num, lldb::addr_t &value);
204 // Get the CFA register for a given frame.
206 ReadCFAValueForRow (lldb::RegisterKind register_kind, const UnwindPlan::RowSP &row, lldb::addr_t &value);
209 GetFastUnwindPlanForFrame ();
212 GetFullUnwindPlanForFrame ();
215 UnwindLogMsg (const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
218 UnwindLogMsgVerbose (const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
221 IsUnwindPlanValidForCurrentPC(lldb::UnwindPlanSP unwind_plan_sp, int &valid_pc_offset);
223 lldb_private::Thread& m_thread;
226 // The following tell us how to retrieve the CALLER's register values (ie the "previous" frame, aka the frame above)
227 // i.e. where THIS frame saved them
230 lldb::UnwindPlanSP m_fast_unwind_plan_sp; // may be NULL
231 lldb::UnwindPlanSP m_full_unwind_plan_sp;
232 lldb::UnwindPlanSP m_fallback_unwind_plan_sp; // may be NULL
234 bool m_all_registers_available; // Can we retrieve all regs or just nonvolatile regs?
235 int m_frame_type; // enum FrameType
238 lldb_private::Address m_start_pc;
239 lldb_private::Address m_current_pc;
241 int m_current_offset; // how far into the function we've executed; -1 if unknown
242 // 0 if no instructions have been executed yet.
244 int m_current_offset_backed_up_one; // how far into the function we've executed; -1 if unknown
245 // 0 if no instructions have been executed yet.
246 // On architectures where the return address on the stack points
247 // to the instruction after the CALL, this value will have 1
248 // subtracted from it. Else a function that ends in a CALL will
249 // have an offset pointing into the next function's address range.
250 // m_current_pc has the actual address of the "current" pc.
252 lldb_private::SymbolContext& m_sym_ctx;
253 bool m_sym_ctx_valid; // if ResolveSymbolContextForAddress fails, don't try to use m_sym_ctx
255 uint32_t m_frame_number; // What stack frame this RegisterContext is
257 std::map<uint32_t, lldb_private::UnwindLLDB::RegisterLocation> m_registers; // where to find reg values for this frame
259 lldb_private::UnwindLLDB& m_parent_unwind; // The UnwindLLDB that is creating this RegisterContextLLDB
261 //------------------------------------------------------------------
262 // For RegisterContextLLDB only
263 //------------------------------------------------------------------
265 DISALLOW_COPY_AND_ASSIGN (RegisterContextLLDB);
268 } // namespace lldb_private
270 #endif // lldb_RegisterContextLLDB_h_