LLDB mainline
RegisterContextUnwind.h
Go to the documentation of this file.
1//===-- RegisterContextUnwind.h ---------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_TARGET_REGISTERCONTEXTUNWIND_H
10#define LLDB_TARGET_REGISTERCONTEXTUNWIND_H
11
12#include <vector>
13
19#include "lldb/lldb-private.h"
20
21namespace lldb_private {
22
23class UnwindLLDB;
24class ArchitectureArm;
25
27public:
28 typedef std::shared_ptr<RegisterContextUnwind> SharedPtr;
29
31 const SharedPtr &next_frame,
33 uint32_t frame_number,
34 lldb_private::UnwindLLDB &unwind_lldb);
35
36 ~RegisterContextUnwind() 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
55
56 bool WriteAllRegisterValues(const lldb::DataBufferSP &data_sp) override;
57
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 // Indicates whether this frame *behaves* like frame zero -- the currently
72 // executing frame -- or not. This can be true in the middle of the stack
73 // above asynchronous trap handlers (sigtramp) for instance.
74 bool BehavesLikeZerothFrame() const override;
75
76protected:
77 // Provide a location for where THIS function saved the CALLER's register
78 // value, or a frame "below" this one saved it. That is, this function doesn't
79 // modify the register, it may call a function that does & saved it to stack.
80 //
81 // The ConcreteRegisterLocation type may be set to eRegisterNotAvailable --
82 // this will happen for a volatile register being queried mid-stack. Instead
83 // of floating frame 0's contents of that register up the stack (which may or
84 // may not be the value of that reg when the function was executing), we won't
85 // return any value.
86 //
87 // If a non-volatile register (a "preserved" register, a callee-preserved
88 // register) is requested mid-stack, and no frames "below" the requested stack
89 // have saved the register anywhere, it is safe to assume that frame 0's
90 // register value is the same.
92 uint32_t lldb_regnum,
94
95private:
96 enum FrameType {
99 eDebuggerFrame, // a debugger inferior function call frame; we get caller's
100 // registers from debugger
101 eSkipFrame, // The unwind resulted in a bogus frame but may get back on
102 // track so we don't want to give up yet
103 eNotAValidFrame // this frame is invalid for some reason - most likely it is
104 // past the top (end) of the stack
105 };
106
107 // UnwindLLDB needs to pass around references to ConcreteRegisterLocations
108 friend class UnwindLLDB;
109 // Architecture may need to retrieve caller register values from this frame
110 friend class ArchitectureArm;
111
112 // Returns true if we have an unwind loop -- the same stack frame unwinding
113 // multiple times.
114 bool CheckIfLoopingStack();
115
116 // Indicates whether this frame is frame zero -- the currently
117 // executing frame -- or not.
118 bool IsFrameZero() const;
119
121
123
124 SharedPtr GetNextFrame() const;
125
126 SharedPtr GetPrevFrame() const;
127
128 // A SkipFrame occurs when the unwind out of frame 0 didn't go right -- we've
129 // got one bogus frame at frame #1.
130 // There is a good chance we'll get back on track if we follow the frame
131 // pointer chain (or whatever is appropriate
132 // on this ABI) so we allow one invalid frame to be in the stack. Ideally
133 // we'll mark this frame specially at some
134 // point and indicate to the user that the unwinder had a hiccup. Often when
135 // this happens we will miss a frame of
136 // the program's actual stack in the unwind and we want to flag that for the
137 // user somehow.
138 bool IsSkipFrame() const;
139
140 /// Determines if a SymbolContext is a trap handler or not
141 ///
142 /// Given a SymbolContext, determines if this is a trap handler function
143 /// aka asynchronous signal handler.
144 ///
145 /// \return
146 /// Returns true if the SymbolContext is a trap handler.
149
150 /// Check if the given unwind plan indicates a signal trap handler, and
151 /// update frame type and symbol context if so.
153 std::shared_ptr<const UnwindPlan> unwind_plan);
154
155 std::optional<UnwindPlan::Row::AbstractRegisterLocation>
156 GetAbstractRegisterLocation(uint32_t lldb_regnum, lldb::RegisterKind &kind);
157
160 const lldb_private::RegisterInfo *reg_info,
162
165 const lldb_private::RegisterInfo *reg_info,
166 const lldb_private::RegisterValue &value);
167
168 /// If the unwind has to the caller frame has failed, try something else
169 ///
170 /// If lldb is using an assembly language based UnwindPlan for a frame and
171 /// the unwind to the caller frame fails, try falling back to a generic
172 /// UnwindPlan (architecture default unwindplan) to see if that might work
173 /// better. This is mostly helping to work around problems where the
174 /// assembly language inspection fails on hand-written assembly code.
175 ///
176 /// \return
177 /// Returns true if a fallback unwindplan was found & was installed.
179
180 /// Switch to the fallback unwind plan unconditionally without any safety
181 /// checks that it is providing better results than the normal unwind plan.
182 ///
183 /// The only time it is valid to call this method is if the full unwindplan is
184 /// found to be fundamentally incorrect/impossible.
185 ///
186 /// Returns true if it was able to install the fallback unwind plan.
188
189 // Get the contents of a general purpose (address-size) register for this
190 // frame
191 // (usually retrieved from the next frame)
192 bool ReadGPRValue(lldb::RegisterKind register_kind, uint32_t regnum,
193 lldb::addr_t &value);
194
195 bool ReadGPRValue(const RegisterNumber &reg_num, lldb::addr_t &value);
196
197 // Get the Frame Address register for a given frame.
198 bool ReadFrameAddress(lldb::RegisterKind register_kind,
199 const UnwindPlan::Row::FAValue &fa,
200 lldb::addr_t &address);
201
202 std::shared_ptr<const UnwindPlan> GetFastUnwindPlanForFrame();
203
204 std::shared_ptr<const UnwindPlan> GetFullUnwindPlanForFrame();
205
207
208 void UnwindLogMsg(const char *fmt, ...) __attribute__((format(printf, 2, 3)));
209
210 void UnwindLogMsgVerbose(const char *fmt, ...)
211 __attribute__((format(printf, 2, 3)));
212
214 std::shared_ptr<const UnwindPlan> unwind_plan_sp);
215
216 lldb::addr_t GetReturnAddressHint(int32_t plan_offset);
217
219
220 ///
221 // The following tell us how to retrieve the CALLER's register values (ie the
222 // "previous" frame, aka the frame above)
223 // i.e. where THIS frame saved them
224 ///
225
226 std::shared_ptr<const UnwindPlan> m_fast_unwind_plan_sp; // may be NULL
227 std::shared_ptr<const UnwindPlan> m_full_unwind_plan_sp;
228 std::shared_ptr<const UnwindPlan> m_fallback_unwind_plan_sp; // may be NULL
229
230 bool m_all_registers_available; // Can we retrieve all regs or just
231 // nonvolatile regs?
232 int m_frame_type; // enum FrameType
233
234 lldb::addr_t m_cfa;
235 lldb::addr_t m_afa;
238
239 /// How far into the function we've executed. 0 if no instructions have been
240 /// executed yet, std::nullopt if unknown.
241 std::optional<int> m_current_offset;
242
243 // How far into the function we've executed. 0 if no instructions have been
244 // executed yet, std::nullopt if unknown. On architectures where the return
245 // address on the stack points to the instruction after the CALL, this value
246 // will have 1 subtracted from it. Otherwise, a function that ends in a CALL
247 // will have an offset pointing into the next function's address range.
248 // m_current_pc has the actual address of the "current" pc.
250
251 bool m_behaves_like_zeroth_frame; // this frame behaves like frame zero
252
254 bool m_sym_ctx_valid; // if ResolveSymbolContextForAddress fails, don't try to
255 // use m_sym_ctx
256
257 uint32_t m_frame_number; // What stack frame this RegisterContext is
258
259 std::map<uint32_t, lldb_private::UnwindLLDB::ConcreteRegisterLocation>
260 m_registers; // where to find reg values for this frame
261
262 lldb_private::UnwindLLDB &m_parent_unwind; // The UnwindLLDB that is creating
263 // this RegisterContextUnwind
264
267 operator=(const RegisterContextUnwind &) = delete;
268};
269
270} // namespace lldb_private
271
272#endif // LLDB_TARGET_REGISTERCONTEXTUNWIND_H
A class to represent register numbers, and able to convert between different register numbering schem...
A section + offset based address class.
Definition Address.h:62
A plug-in interface definition class for debugging a process.
Definition Process.h:357
std::optional< UnwindPlan::Row::AbstractRegisterLocation > GetAbstractRegisterLocation(uint32_t lldb_regnum, lldb::RegisterKind &kind)
bool WriteAllRegisterValues(const lldb::DataBufferSP &data_sp) override
void UnwindLogMsg(const char *fmt,...) __attribute__((format(printf
void PropagateTrapHandlerFlagFromUnwindPlan(std::shared_ptr< const UnwindPlan > unwind_plan)
Check if the given unwind plan indicates a signal trap handler, and update frame type and symbol cont...
void void UnwindLogMsgVerbose(const char *fmt,...) __attribute__((format(printf
const lldb_private::RegisterInfo * GetRegisterInfoAtIndex(size_t reg) override
bool ReadRegisterValueFromRegisterLocation(lldb_private::UnwindLLDB::ConcreteRegisterLocation regloc, const lldb_private::RegisterInfo *reg_info, lldb_private::RegisterValue &value)
const lldb_private::RegisterSet * GetRegisterSet(size_t reg_set) override
std::shared_ptr< RegisterContextUnwind > SharedPtr
bool ReadFrameAddress(lldb::RegisterKind register_kind, const UnwindPlan::Row::FAValue &fa, lldb::addr_t &address)
bool ReadAllRegisterValues(lldb::WritableDataBufferSP &data_sp) override
bool WriteRegister(const lldb_private::RegisterInfo *reg_info, const lldb_private::RegisterValue &value) override
RegisterContextUnwind(lldb_private::Thread &thread, const SharedPtr &next_frame, lldb_private::SymbolContext &sym_ctx, uint32_t frame_number, lldb_private::UnwindLLDB &unwind_lldb)
std::shared_ptr< const UnwindPlan > GetFastUnwindPlanForFrame()
std::shared_ptr< const UnwindPlan > m_fast_unwind_plan_sp
std::map< uint32_t, lldb_private::UnwindLLDB::ConcreteRegisterLocation > m_registers
std::shared_ptr< const UnwindPlan > GetFullUnwindPlanForFrame()
bool ForceSwitchToFallbackUnwindPlan()
Switch to the fallback unwind plan unconditionally without any safety checks that it is providing bet...
lldb_private::UnwindLLDB::RegisterSearchResult SavedLocationForRegister(uint32_t lldb_regnum, lldb_private::UnwindLLDB::ConcreteRegisterLocation &regloc)
bool ReadGPRValue(lldb::RegisterKind register_kind, uint32_t regnum, lldb::addr_t &value)
lldb_private::UnwindLLDB & m_parent_unwind
bool WriteRegisterValueToRegisterLocation(lldb_private::UnwindLLDB::ConcreteRegisterLocation regloc, const lldb_private::RegisterInfo *reg_info, const lldb_private::RegisterValue &value)
bool TryFallbackUnwindPlan()
If the unwind has to the caller frame has failed, try something else.
std::optional< int > m_current_offset
How far into the function we've executed.
bool ReadRegister(const lldb_private::RegisterInfo *reg_info, lldb_private::RegisterValue &value) override
lldb::addr_t GetReturnAddressHint(int32_t plan_offset)
~RegisterContextUnwind() override=default
bool IsTrapHandlerSymbol(lldb_private::Process *process, const lldb_private::SymbolContext &m_sym_ctx) const
Determines if a SymbolContext is a trap handler or not.
uint32_t ConvertRegisterKindToRegisterNumber(lldb::RegisterKind kind, uint32_t num) override
Convert from a given register numbering scheme to the lldb register numbering scheme.
void void bool IsUnwindPlanValidForCurrentPC(std::shared_ptr< const UnwindPlan > unwind_plan_sp)
std::shared_ptr< const UnwindPlan > m_fallback_unwind_plan_sp
lldb_private::SymbolContext & m_sym_ctx
bool BehavesLikeZerothFrame() const override
Indicates that this frame is currently executing code, that the PC value is not a return-pc but an ac...
std::shared_ptr< const UnwindPlan > m_full_unwind_plan_sp
Defines a symbol context baton that can be handed other debug core functions.
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::UnwindPlan > UnwindPlanSP
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
std::shared_ptr< lldb_private::WritableDataBuffer > WritableDataBufferSP
uint64_t addr_t
Definition lldb-types.h:80
RegisterKind
Register numbering types.
Every register is described in detail including its name, alternate name (optional),...
Registers are grouped into register sets.
An UnwindPlan::Row::AbstractRegisterLocation, combined with the register context and memory for a spe...
Definition UnwindLLDB.h:46