LLDB mainline
ArchitectureArm.cpp
Go to the documentation of this file.
1//===-- ArchitectureArm.cpp -----------------------------------------------===//
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
15#include "lldb/Target/Process.h"
18#include "lldb/Target/Thread.h"
22#include "lldb/Utility/Log.h"
24
25#include "llvm/Support/Endian.h"
26
27using namespace lldb_private;
28using namespace lldb;
29
31
37
41
42std::unique_ptr<Architecture> ArchitectureArm::Create(const ArchSpec &arch) {
43 if (arch.GetMachine() != llvm::Triple::arm)
44 return nullptr;
45 return std::unique_ptr<Architecture>(new ArchitectureArm());
46}
47
49 // We need to check if we are stopped in Thumb mode in a IT instruction and
50 // detect if the condition doesn't pass. If this is the case it means we
51 // won't actually execute this instruction. If this happens we need to clear
52 // the stop reason to no thread plans think we are stopped for a reason and
53 // the plans should keep going.
54 //
55 // We do this because when single stepping many ARM processes, debuggers
56 // often use the BVR/BCR registers that says "stop when the PC is not equal
57 // to its current value". This method of stepping means we can end up
58 // stopping on instructions inside an if/then block that wouldn't get
59 // executed. By fixing this we can stop the debugger from seeming like you
60 // stepped through both the "if" _and_ the "else" clause when source level
61 // stepping because the debugger stops regardless due to the BVR/BCR
62 // triggering a stop.
63 //
64 // It also means we can set breakpoints on instructions inside an if/then
65 // block and correctly skip them if we use the BKPT instruction. The ARM and
66 // Thumb BKPT instructions are unconditional even when executed in a Thumb IT
67 // block.
68 //
69 // If your debugger inserts software traps in ARM/Thumb code, it will need to
70 // use 16 and 32 bit instruction for 16 and 32 bit thumb instructions
71 // respectively. If your debugger inserts a 16 bit thumb trap on top of a 32
72 // bit thumb instruction for an opcode that is inside an if/then, it will
73 // change the it/then to conditionally execute your
74 // 16 bit trap and then cause your program to crash if it executes the
75 // trailing 16 bits (the second half of the 32 bit thumb instruction you
76 // partially overwrote).
77
78 RegisterContextSP reg_ctx_sp(thread.GetRegisterContext());
79 if (!reg_ctx_sp)
80 return;
81
82 const uint32_t cpsr = reg_ctx_sp->GetFlags(0);
83 if (cpsr == 0)
84 return;
85
86 // Read the J and T bits to get the ISETSTATE
87 const uint32_t J = Bit32(cpsr, 24);
88 const uint32_t T = Bit32(cpsr, 5);
89 const uint32_t ISETSTATE = J << 1 | T;
90 if (ISETSTATE == 0) {
91// NOTE: I am pretty sure we want to enable the code below
92// that detects when we stop on an instruction in ARM mode that is conditional
93// and the condition doesn't pass. This can happen if you set a breakpoint on
94// an instruction that is conditional. We currently will _always_ stop on the
95// instruction which is bad. You can also run into this while single stepping
96// and you could appear to run code in the "if" and in the "else" clause
97// because it would stop at all of the conditional instructions in both. In
98// such cases, we really don't want to stop at this location.
99// I will check with the lldb-dev list first before I enable this.
100#if 0
101 // ARM mode: check for condition on instruction
102 const addr_t pc = reg_ctx_sp->GetPC();
104 // If we fail to read the opcode we will get UINT64_MAX as the result in
105 // "opcode" which we can use to detect if we read a valid opcode.
106 const uint64_t opcode = thread.GetProcess()->ReadUnsignedIntegerFromMemory(pc, 4, UINT64_MAX, error);
107 if (opcode <= UINT32_MAX)
108 {
109 const uint32_t condition = Bits32((uint32_t)opcode, 31, 28);
110 if (!ARMConditionPassed(condition, cpsr))
111 {
112 // We ARE stopped on an ARM instruction whose condition doesn't
113 // pass so this instruction won't get executed. Regardless of why
114 // it stopped, we need to clear the stop info
115 thread.SetStopInfo (StopInfoSP());
116 }
117 }
118#endif
119 } else if (ISETSTATE == 1) {
120 // Thumb mode
121 const uint32_t ITSTATE = Bits32(cpsr, 15, 10) << 2 | Bits32(cpsr, 26, 25);
122 if (ITSTATE != 0) {
123 const uint32_t condition = Bits32(ITSTATE, 7, 4);
124 if (!ARMConditionPassed(condition, cpsr)) {
125 // We ARE stopped in a Thumb IT instruction on an instruction whose
126 // condition doesn't pass so this instruction won't get executed.
127 // Regardless of why it stopped, we need to clear the stop info
128 thread.SetStopInfo(StopInfoSP());
129 }
130 }
131 }
132}
133
135 AddressClass addr_class) const {
136 bool is_alternate_isa = false;
137
138 switch (addr_class) {
143 is_alternate_isa = true;
144 break;
145 default: break;
146 }
147
148 if ((code_addr & 2u) || is_alternate_isa)
149 return code_addr | 1u;
150 return code_addr;
151}
152
154 AddressClass addr_class) const {
155 switch (addr_class) {
159 default: break;
160 }
161 return opcode_addr & ~(1ull);
162}
163
164// The ARM M-Profile Armv7-M Architecture Reference Manual,
165// subsection "B1.5 Armv7-M exception model", see the parts
166// describing "Exception entry behavior" and "Exception
167// return behavior".
168// When an exception happens on this processor, certain registers are
169// saved below the stack pointer, the stack pointer is decremented,
170// a special value is put in the link register to indicate the
171// exception has been taken, and an exception handler function
172// is invoked.
173//
174// Detect that special value in $lr, and if present, add
175// unwind rules for the registers that were saved above this
176// stack frame's CFA. Overwrite any register locations that
177// the current_unwindplan has for these registers; they are
178// not correct when we're invoked this way.
180 Thread &thread, RegisterContextUnwind *regctx,
181 std::shared_ptr<const UnwindPlan> current_unwindplan) {
182
183 ProcessSP process_sp = thread.GetProcess();
184 if (!process_sp)
185 return {};
186
187 const ArchSpec arch = process_sp->GetTarget().GetArchitecture();
188 if (!arch.GetTriple().isArmMClass() || arch.GetAddressByteSize() != 4)
189 return {};
190
191 // Get the caller's LR value from regctx (the LR value
192 // at function entry to this function).
193 RegisterNumber ra_regnum(thread, eRegisterKindGeneric,
195 uint32_t ra_regnum_lldb = ra_regnum.GetAsKind(eRegisterKindLLDB);
196
197 if (ra_regnum_lldb == LLDB_INVALID_REGNUM)
198 return {};
199
201 bool got_concrete_location = false;
202 if (regctx->SavedLocationForRegister(ra_regnum_lldb, regloc) ==
204 got_concrete_location = true;
205 } else {
206 RegisterNumber pc_regnum(thread, eRegisterKindGeneric,
208 uint32_t pc_regnum_lldb = pc_regnum.GetAsKind(eRegisterKindLLDB);
209 if (regctx->SavedLocationForRegister(pc_regnum_lldb, regloc) ==
211 got_concrete_location = true;
212 }
213
214 if (!got_concrete_location)
215 return {};
216
217 addr_t callers_return_address = LLDB_INVALID_ADDRESS;
218 const RegisterInfo *reg_info = regctx->GetRegisterInfoAtIndex(ra_regnum_lldb);
219 if (reg_info) {
220 RegisterValue reg_value;
221 if (regctx->ReadRegisterValueFromRegisterLocation(regloc, reg_info,
222 reg_value)) {
223 callers_return_address = reg_value.GetAsUInt32();
224 }
225 }
226
227 if (callers_return_address == LLDB_INVALID_ADDRESS)
228 return {};
229
230 // ARMv7-M ARM says that the LR will be set to
231 // one of these values when an exception has taken
232 // place:
233 // if HaveFPExt() then
234 // if CurrentMode==Mode_Handler then
235 // LR = Ones(27):NOT(CONTROL.FPCA):'0001';
236 // else
237 // LR = Ones(27):NOT(CONTROL.FPCA):'1':CONTROL.SPSEL:'01';
238 // else
239 // if CurrentMode==Mode_Handler then
240 // LR = Ones(28):'0001';
241 // else
242 // LR = Ones(29):CONTROL.SPSEL:'01';
243
244 // Top 27 bits are set for an exception return.
245 const uint32_t exception_return = -1U & ~0b11111U;
246 // Bit4 is 1 if only GPRs were saved.
247 const uint32_t gprs_only = 0b10000;
248 // Bit<1:0> are '01'.
249 const uint32_t lowbits = 0b01;
250
251 if ((callers_return_address & exception_return) != exception_return)
252 return {};
253 if ((callers_return_address & lowbits) != lowbits)
254 return {};
255
256 const bool fp_regs_saved = !(callers_return_address & gprs_only);
257
258 const RegisterKind plan_regkind = current_unwindplan->GetRegisterKind();
259 UnwindPlanSP new_plan = std::make_shared<UnwindPlan>(plan_regkind);
260 new_plan->SetSourceName("Arm Cortex-M exception return UnwindPlan");
261 new_plan->SetSourcedFromCompiler(eLazyBoolNo);
262 new_plan->SetUnwindPlanValidAtAllInstructions(eLazyBoolYes);
263 new_plan->SetUnwindPlanForSignalTrap(eLazyBoolYes);
264
265 int stored_regs_size = fp_regs_saved ? 0x68 : 0x20;
266
267 uint32_t gpr_regs[] = {dwarf_r0, dwarf_r1, dwarf_r2, dwarf_r3,
269 const int gpr_reg_count = std::size(gpr_regs);
270 uint32_t fpr_regs[] = {dwarf_s0, dwarf_s1, dwarf_s2, dwarf_s3,
274 const int fpr_reg_count = std::size(fpr_regs);
275
276 RegisterContextSP reg_ctx_sp = thread.GetRegisterContext();
277 std::vector<uint32_t> saved_regs;
278 for (int i = 0; i < gpr_reg_count; i++) {
279 uint32_t regno = gpr_regs[i];
280 reg_ctx_sp->ConvertBetweenRegisterKinds(eRegisterKindDWARF, gpr_regs[i],
281 plan_regkind, regno);
282 saved_regs.push_back(regno);
283 }
284 if (fp_regs_saved) {
285 for (int i = 0; i < fpr_reg_count; i++) {
286 uint32_t regno = fpr_regs[i];
287 reg_ctx_sp->ConvertBetweenRegisterKinds(eRegisterKindDWARF, fpr_regs[i],
288 plan_regkind, regno);
289 saved_regs.push_back(regno);
290 }
291 }
292
293 addr_t cfa;
294 if (!regctx->GetCFA(cfa))
295 return {};
296
297 // The CPSR value saved to stack is actually (from Armv7-M ARM)
298 // "XPSR<31:10>:frameptralign:XPSR<8:0>"
299 // Bit 9 indicates that the stack pointer was aligned (to
300 // an 8-byte alignment) when the exception happened, and we must
301 // account for that when restoring the original stack pointer value.
303 uint32_t callers_xPSR =
304 process_sp->ReadUnsignedIntegerFromMemory(cfa + 0x1c, 4, 0, error);
305 const bool align_stack = callers_xPSR & (1U << 9);
306 uint32_t callers_sp = cfa + stored_regs_size;
307 if (align_stack)
308 callers_sp |= 4;
309
310 Log *log = GetLog(LLDBLog::Unwind);
311 LLDB_LOGF(log,
312 "ArchitectureArm::GetArchitectureUnwindPlan found caller return "
313 "addr of 0x%" PRIx64 ", for frame with CFA 0x%" PRIx64
314 ", fp_regs_saved %d, stored_regs_size 0x%x, align stack %d",
315 callers_return_address, cfa, fp_regs_saved, stored_regs_size,
316 align_stack);
317
318 uint32_t sp_regnum = dwarf_sp;
319 reg_ctx_sp->ConvertBetweenRegisterKinds(eRegisterKindDWARF, dwarf_sp,
320 plan_regkind, sp_regnum);
321
322 const int row_count = current_unwindplan->GetRowCount();
323 for (int i = 0; i < row_count; i++) {
324 UnwindPlan::Row row = *current_unwindplan->GetRowAtIndex(i);
325 uint32_t offset = 0;
326 const size_t saved_reg_count = saved_regs.size();
327 for (size_t j = 0; j < saved_reg_count; j++) {
328 // The locations could be set with
329 // SetRegisterLocationToIsConstant(regno, cfa+offset)
330 // expressing it in terms of CFA addr+offset - this UnwindPlan
331 // is only used once, with this specific CFA. I'm not sure
332 // which will be clearer for someone reading the unwind log.
333 row.SetRegisterLocationToAtCFAPlusOffset(saved_regs[j], offset, true);
334 offset += 4;
335 }
336 row.SetRegisterLocationToIsCFAPlusOffset(sp_regnum, callers_sp - cfa, true);
337 new_plan->AppendRow(row);
338 }
339 return new_plan;
340}
341
343 llvm::ArrayRef<uint8_t> reference, llvm::ArrayRef<uint8_t> observed) const {
344 if (reference.size() > observed.size())
345 return false;
346
347 if (reference.size() == 2) {
348 auto ref_bytes = llvm::support::endian::read16le(reference.data());
349 auto obs_bytes = llvm::support::endian::read16le(observed.data());
350 if (ref_bytes == obs_bytes)
351 return true;
352 // LLDB uses an undef instruction encoding for breakpoints -
353 // perhaps we have an actual BKPT in the inferior.
354 uint16_t mask = 0xFF00;
355 if ((obs_bytes & mask) == 0xBE00)
356 return true;
357 } else if (reference.size() == 4) {
358 auto ref_bytes = llvm::support::endian::read32le(reference.data());
359 auto obs_bytes = llvm::support::endian::read32le(observed.data());
360 if (ref_bytes == obs_bytes)
361 return true;
362 uint32_t mask = 0xFFF000F0;
363 uint32_t bkpt_pattern = 0xE1200070;
364 if ((obs_bytes & mask) == bkpt_pattern)
365 return true;
366 }
367 return false;
368}
@ dwarf_r12
@ dwarf_r3
@ dwarf_r2
@ dwarf_r1
@ dwarf_pc
@ dwarf_r0
@ dwarf_sp
@ dwarf_lr
@ dwarf_s15
@ dwarf_s12
@ dwarf_s14
@ dwarf_s10
@ dwarf_s13
@ dwarf_cpsr
@ dwarf_s11
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
Definition Log.h:378
#define LLDB_PLUGIN_DEFINE(PluginName)
A class to represent register numbers, and able to convert between different register numbering schem...
uint32_t GetAsKind(lldb::RegisterKind kind)
An architecture specification class.
Definition ArchSpec.h:32
uint32_t GetAddressByteSize() const
Returns the size in bytes of an address of the current architecture.
Definition ArchSpec.cpp:690
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition ArchSpec.h:460
llvm::Triple::ArchType GetMachine() const
Returns a machine family for the current architecture.
Definition ArchSpec.cpp:682
bool IsValidTrapInstruction(llvm::ArrayRef< uint8_t > reference, llvm::ArrayRef< uint8_t > observed) const override
Returns whether a given byte sequence is a valid trap instruction for the architecture.
static std::unique_ptr< Architecture > Create(const ArchSpec &arch)
lldb::addr_t GetCallableLoadAddress(lldb::addr_t load_addr, AddressClass addr_class) const override
Get load_addr as a callable code load address for this target.
static llvm::StringRef GetPluginNameStatic()
void OverrideStopInfo(Thread &thread) const override
This is currently intended to handle cases where a program stops at an instruction that won't get exe...
lldb::addr_t GetOpcodeLoadAddress(lldb::addr_t load_addr, AddressClass addr_class) const override
Get load_addr as an opcode for this target.
lldb::UnwindPlanSP GetArchitectureUnwindPlan(lldb_private::Thread &thread, lldb_private::RegisterContextUnwind *regctx, std::shared_ptr< const UnwindPlan > current_unwindplan) override
Return an UnwindPlan that allows architecture-defined rules for finding saved registers,...
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
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)
lldb_private::UnwindLLDB::RegisterSearchResult SavedLocationForRegister(uint32_t lldb_regnum, lldb_private::UnwindLLDB::ConcreteRegisterLocation &regloc)
uint32_t GetAsUInt32(uint32_t fail_value=UINT32_MAX, bool *success_ptr=nullptr) const
An error handling class.
Definition Status.h:118
bool SetRegisterLocationToIsCFAPlusOffset(uint32_t reg_num, int32_t offset, bool can_replace)
bool SetRegisterLocationToAtCFAPlusOffset(uint32_t reg_num, int32_t offset, bool can_replace)
#define LLDB_REGNUM_GENERIC_RA
#define UINT64_MAX
#define LLDB_INVALID_ADDRESS
#define UINT32_MAX
#define LLDB_INVALID_REGNUM
#define LLDB_REGNUM_GENERIC_PC
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:327
static bool ARMConditionPassed(const uint32_t condition, const uint32_t cpsr)
Definition ARMDefines.h:107
static uint32_t Bits32(const uint32_t bits, const uint32_t msbit, const uint32_t lsbit)
static uint32_t Bit32(const uint32_t bits, const uint32_t bit)
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::UnwindPlan > UnwindPlanSP
std::shared_ptr< lldb_private::StopInfo > StopInfoSP
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::RegisterContext > RegisterContextSP
RegisterKind
Register numbering types.
@ eRegisterKindGeneric
insn ptr reg, stack ptr reg, etc not specific to any particular target
@ eRegisterKindLLDB
lldb's internal register numbers
@ eRegisterKindDWARF
the register numbers seen DWARF
Every register is described in detail including its name, alternate name (optional),...
An UnwindPlan::Row::AbstractRegisterLocation, combined with the register context and memory for a spe...
Definition UnwindLLDB.h:46