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_
15 #include "lldb/lldb-private.h"
16 #include "lldb/Target/RegisterContext.h"
17 #include "lldb/Symbol/UnwindPlan.h"
18 #include "lldb/Symbol/SymbolContext.h"
19 #include "lldb/Utility/RegisterNumber.h"
20 #include "UnwindLLDB.h"
22 namespace lldb_private {
26 class RegisterContextLLDB : public lldb_private::RegisterContext
29 typedef std::shared_ptr<RegisterContextLLDB> SharedPtr;
31 RegisterContextLLDB (lldb_private::Thread &thread,
32 const SharedPtr& next_frame,
33 lldb_private::SymbolContext& sym_ctx,
34 uint32_t frame_number, lldb_private::UnwindLLDB& unwind_lldb);
37 // pure virtual functions from the base class that we must implement
41 ~RegisterContextLLDB () { }
44 InvalidateAllRegisters ();
49 virtual const lldb_private::RegisterInfo *
50 GetRegisterInfoAtIndex (size_t reg);
53 GetRegisterSetCount ();
55 virtual const lldb_private::RegisterSet *
56 GetRegisterSet (size_t reg_set);
59 ReadRegister (const lldb_private::RegisterInfo *reg_info, lldb_private::RegisterValue &value);
62 WriteRegister (const lldb_private::RegisterInfo *reg_info, const lldb_private::RegisterValue &value);
65 ReadAllRegisterValues (lldb::DataBufferSP &data_sp);
68 WriteAllRegisterValues (const lldb::DataBufferSP &data_sp);
71 ConvertRegisterKindToRegisterNumber (lldb::RegisterKind kind, uint32_t num);
77 IsTrapHandlerFrame () const;
80 GetCFA (lldb::addr_t& cfa);
83 GetStartPC (lldb::addr_t& start_pc);
86 ReadPC (lldb::addr_t& start_pc);
94 eDebuggerFrame, // a debugger inferior function call frame; we get caller's registers from debugger
95 eSkipFrame, // The unwind resulted in a bogus frame but may get back on track so we don't want to give up yet
96 eNotAValidFrame // this frame is invalid for some reason - most likely it is past the top (end) of the stack
99 // UnwindLLDB needs to pass around references to RegisterLocations
100 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;
134 //------------------------------------------------------------------
135 /// Determines if a SymbolContext is a trap handler or not
137 /// Given a SymbolContext, determines if this is a trap handler function
138 /// aka asynchronous signal handler.
141 /// Returns true if the SymbolContext is a trap handler.
142 //------------------------------------------------------------------
144 IsTrapHandlerSymbol (lldb_private::Process *process, const lldb_private::SymbolContext &m_sym_ctx) const;
146 // Provide a location for where THIS function saved the CALLER's register value
147 // Or a frame "below" this one saved it, i.e. a function called by this one, preserved a register that this
148 // function didn't modify/use.
150 // The RegisterLocation type may be set to eRegisterNotAvailable -- this will happen for a volatile register
151 // being queried mid-stack. Instead of floating frame 0's contents of that register up the stack (which may
152 // or may not be the value of that reg when the function was executing), we won't return any value.
154 // If a non-volatile register (a "preserved" register) is requested mid-stack and no frames "below" the requested
155 // stack have saved the register anywhere, it is safe to assume that frame 0's register values are still the same
156 // as the requesting frame's.
157 lldb_private::UnwindLLDB::RegisterSearchResult
158 SavedLocationForRegister (uint32_t lldb_regnum, lldb_private::UnwindLLDB::RegisterLocation ®loc);
161 ReadRegisterValueFromRegisterLocation (lldb_private::UnwindLLDB::RegisterLocation regloc,
162 const lldb_private::RegisterInfo *reg_info,
163 lldb_private::RegisterValue &value);
166 WriteRegisterValueToRegisterLocation (lldb_private::UnwindLLDB::RegisterLocation regloc,
167 const lldb_private::RegisterInfo *reg_info,
168 const lldb_private::RegisterValue &value);
170 //------------------------------------------------------------------
171 /// If the unwind has to the caller frame has failed, try something else
173 /// If lldb is using an assembly language based UnwindPlan for a frame and
174 /// the unwind to the caller frame fails, try falling back to a generic
175 /// UnwindPlan (architecture default unwindplan) to see if that might work
176 /// better. This is mostly helping to work around problems where the
177 /// assembly language inspection fails on hand-written assembly code.
180 /// Returns true if a fallback unwindplan was found & was installed.
181 //------------------------------------------------------------------
183 TryFallbackUnwindPlan ();
185 //------------------------------------------------------------------
186 /// Switch to the fallback unwind plan unconditionally without any safety
187 /// checks that it is providing better results than the normal unwind plan.
189 /// The only time it is valid to call this method is if the full unwindplan is
190 /// found to be fundamentally incorrect/impossible.
192 /// Returns true if it was able to install the fallback unwind plan.
193 //------------------------------------------------------------------
195 ForceSwitchToFallbackUnwindPlan ();
197 // Get the contents of a general purpose (address-size) register for this frame
198 // (usually retrieved from the next frame)
200 ReadGPRValue (lldb::RegisterKind register_kind, uint32_t regnum, lldb::addr_t &value);
203 ReadGPRValue (const RegisterNumber ®_num, lldb::addr_t &value);
205 // Get the CFA register for a given frame.
207 ReadCFAValueForRow (lldb::RegisterKind register_kind, const UnwindPlan::RowSP &row, lldb::addr_t &value);
210 GetFastUnwindPlanForFrame ();
213 GetFullUnwindPlanForFrame ();
216 UnwindLogMsg (const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
219 UnwindLogMsgVerbose (const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
222 IsUnwindPlanValidForCurrentPC(lldb::UnwindPlanSP unwind_plan_sp, int &valid_pc_offset);
225 lldb_private::Thread& m_thread;
228 // The following tell us how to retrieve the CALLER's register values (ie the "previous" frame, aka the frame above)
229 // i.e. where THIS frame saved them
232 lldb::UnwindPlanSP m_fast_unwind_plan_sp; // may be NULL
233 lldb::UnwindPlanSP m_full_unwind_plan_sp;
234 lldb::UnwindPlanSP m_fallback_unwind_plan_sp; // may be NULL
236 bool m_all_registers_available; // Can we retrieve all regs or just nonvolatile regs?
237 int m_frame_type; // enum FrameType
240 lldb_private::Address m_start_pc;
241 lldb_private::Address m_current_pc;
243 int m_current_offset; // how far into the function we've executed; -1 if unknown
244 // 0 if no instructions have been executed yet.
246 int m_current_offset_backed_up_one; // how far into the function we've executed; -1 if unknown
247 // 0 if no instructions have been executed yet.
248 // On architectures where the return address on the stack points
249 // to the instruction after the CALL, this value will have 1
250 // subtracted from it. Else a function that ends in a CALL will
251 // have an offset pointing into the next function's address range.
252 // m_current_pc has the actual address of the "current" pc.
254 lldb_private::SymbolContext& m_sym_ctx;
255 bool m_sym_ctx_valid; // if ResolveSymbolContextForAddress fails, don't try to use m_sym_ctx
257 uint32_t m_frame_number; // What stack frame this RegisterContext is
259 std::map<uint32_t, lldb_private::UnwindLLDB::RegisterLocation> m_registers; // where to find reg values for this frame
261 lldb_private::UnwindLLDB& m_parent_unwind; // The UnwindLLDB that is creating this RegisterContextLLDB
263 //------------------------------------------------------------------
264 // For RegisterContextLLDB only
265 //------------------------------------------------------------------
267 DISALLOW_COPY_AND_ASSIGN (RegisterContextLLDB);
270 } // namespace lldb_private
272 #endif // lldb_RegisterContextLLDB_h_