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