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 
14 #include "lldb/Target/Thread.h"
15 #include "lldb/Utility/ArchSpec.h"
16 
17 using namespace lldb_private;
18 using namespace lldb;
19 
21 
22 ConstString ArchitectureArm::GetPluginNameStatic() {
23  return ConstString("arm");
24 }
25 
27  PluginManager::RegisterPlugin(GetPluginNameStatic(),
28  "Arm-specific algorithms",
30 }
31 
34 }
35 
36 std::unique_ptr<Architecture> ArchitectureArm::Create(const ArchSpec &arch) {
37  if (arch.GetMachine() != llvm::Triple::arm)
38  return nullptr;
39  return std::unique_ptr<Architecture>(new ArchitectureArm());
40 }
41 
42 ConstString ArchitectureArm::GetPluginName() { return GetPluginNameStatic(); }
43 
45  // We need to check if we are stopped in Thumb mode in a IT instruction and
46  // detect if the condition doesn't pass. If this is the case it means we
47  // won't actually execute this instruction. If this happens we need to clear
48  // the stop reason to no thread plans think we are stopped for a reason and
49  // the plans should keep going.
50  //
51  // We do this because when single stepping many ARM processes, debuggers
52  // often use the BVR/BCR registers that says "stop when the PC is not equal
53  // to its current value". This method of stepping means we can end up
54  // stopping on instructions inside an if/then block that wouldn't get
55  // executed. By fixing this we can stop the debugger from seeming like you
56  // stepped through both the "if" _and_ the "else" clause when source level
57  // stepping because the debugger stops regardless due to the BVR/BCR
58  // triggering a stop.
59  //
60  // It also means we can set breakpoints on instructions inside an an if/then
61  // block and correctly skip them if we use the BKPT instruction. The ARM and
62  // Thumb BKPT instructions are unconditional even when executed in a Thumb IT
63  // block.
64  //
65  // If your debugger inserts software traps in ARM/Thumb code, it will need to
66  // use 16 and 32 bit instruction for 16 and 32 bit thumb instructions
67  // respectively. If your debugger inserts a 16 bit thumb trap on top of a 32
68  // bit thumb instruction for an opcode that is inside an if/then, it will
69  // change the it/then to conditionally execute your
70  // 16 bit trap and then cause your program to crash if it executes the
71  // trailing 16 bits (the second half of the 32 bit thumb instruction you
72  // partially overwrote).
73 
74  RegisterContextSP reg_ctx_sp(thread.GetRegisterContext());
75  if (!reg_ctx_sp)
76  return;
77 
78  const uint32_t cpsr = reg_ctx_sp->GetFlags(0);
79  if (cpsr == 0)
80  return;
81 
82  // Read the J and T bits to get the ISETSTATE
83  const uint32_t J = Bit32(cpsr, 24);
84  const uint32_t T = Bit32(cpsr, 5);
85  const uint32_t ISETSTATE = J << 1 | T;
86  if (ISETSTATE == 0) {
87 // NOTE: I am pretty sure we want to enable the code below
88 // that detects when we stop on an instruction in ARM mode that is conditional
89 // and the condition doesn't pass. This can happen if you set a breakpoint on
90 // an instruction that is conditional. We currently will _always_ stop on the
91 // instruction which is bad. You can also run into this while single stepping
92 // and you could appear to run code in the "if" and in the "else" clause
93 // because it would stop at all of the conditional instructions in both. In
94 // such cases, we really don't want to stop at this location.
95 // I will check with the lldb-dev list first before I enable this.
96 #if 0
97  // ARM mode: check for condition on instruction
98  const addr_t pc = reg_ctx_sp->GetPC();
99  Status error;
100  // If we fail to read the opcode we will get UINT64_MAX as the result in
101  // "opcode" which we can use to detect if we read a valid opcode.
102  const uint64_t opcode = thread.GetProcess()->ReadUnsignedIntegerFromMemory(pc, 4, UINT64_MAX, error);
103  if (opcode <= UINT32_MAX)
104  {
105  const uint32_t condition = Bits32((uint32_t)opcode, 31, 28);
106  if (!ARMConditionPassed(condition, cpsr))
107  {
108  // We ARE stopped on an ARM instruction whose condition doesn't
109  // pass so this instruction won't get executed. Regardless of why
110  // it stopped, we need to clear the stop info
111  thread.SetStopInfo (StopInfoSP());
112  }
113  }
114 #endif
115  } else if (ISETSTATE == 1) {
116  // Thumb mode
117  const uint32_t ITSTATE = Bits32(cpsr, 15, 10) << 2 | Bits32(cpsr, 26, 25);
118  if (ITSTATE != 0) {
119  const uint32_t condition = Bits32(ITSTATE, 7, 4);
120  if (!ARMConditionPassed(condition, cpsr)) {
121  // We ARE stopped in a Thumb IT instruction on an instruction whose
122  // condition doesn't pass so this instruction won't get executed.
123  // Regardless of why it stopped, we need to clear the stop info
124  thread.SetStopInfo(StopInfoSP());
125  }
126  }
127  }
128 }
129 
131  AddressClass addr_class) const {
132  bool is_alternate_isa = false;
133 
134  switch (addr_class) {
135  case AddressClass::eData:
137  return LLDB_INVALID_ADDRESS;
139  is_alternate_isa = true;
140  break;
141  default: break;
142  }
143 
144  if ((code_addr & 2u) || is_alternate_isa)
145  return code_addr | 1u;
146  return code_addr;
147 }
148 
150  AddressClass addr_class) const {
151  switch (addr_class) {
152  case AddressClass::eData:
154  return LLDB_INVALID_ADDRESS;
155  default: break;
156  }
157  return opcode_addr & ~(1ull);
158 }
lldb_private::AddressClass::eData
@ eData
ArchitectureArm.h
lldb_private::ArchSpec
Definition: ArchSpec.h:33
lldb_private::ArchitectureArm::OverrideStopInfo
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...
Definition: ArchitectureArm.cpp:44
lldb_private::ArchSpec::GetMachine
llvm::Triple::ArchType GetMachine() const
Returns a machine family for the current architecture.
Definition: ArchSpec.cpp:667
lldb_private::AddressClass
AddressClass
Definition: lldb-private-enumerations.h:48
lldb_private::ArchitectureArm::Initialize
static void Initialize()
Definition: ArchitectureArm.cpp:26
lldb_private::ArchitectureArm::GetCallableLoadAddress
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.
Definition: ArchitectureArm.cpp:130
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
pc
@ pc
Definition: CompactUnwindInfo.cpp:1250
lldb_private::PluginManager::RegisterPlugin
static bool RegisterPlugin(ConstString name, const char *description, ABICreateInstance create_callback)
Definition: PluginManager.cpp:288
lldb_private::Thread::GetProcess
lldb::ProcessSP GetProcess() const
Definition: Thread.h:154
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::PluginManager::UnregisterPlugin
static bool UnregisterPlugin(ABICreateInstance create_callback)
Definition: PluginManager.cpp:293
lldb_private::Bit32
static uint32_t Bit32(const uint32_t bits, const uint32_t bit)
Definition: InstructionUtils.h:36
lldb_private::Thread
Definition: Thread.h:62
lldb_private::ArchitectureArm
Definition: ArchitectureArm.h:16
lldb_private::ConstString
Definition: ConstString.h:40
arm64_dwarf::cpsr
@ cpsr
Definition: ARM64_DWARF_Registers.h:53
lldb_private::Thread::SetStopInfo
void SetStopInfo(const lldb::StopInfoSP &stop_info_sp)
Definition: Thread.cpp:434
lldb_private::ArchitectureArm::GetOpcodeLoadAddress
lldb::addr_t GetOpcodeLoadAddress(lldb::addr_t load_addr, AddressClass addr_class) const override
Get load_addr as an opcode for this target.
Definition: ArchitectureArm.cpp:149
ARMDefines.h
Thread.h
InstructionUtils.h
lldb_private::Status
Definition: Status.h:44
lldb_private::ArchitectureArm::Create
static std::unique_ptr< Architecture > Create(const ArchSpec &arch)
Definition: ArchitectureArm.cpp:36
uint32_t
ArchSpec.h
UINT32_MAX
#define UINT32_MAX
Definition: lldb-defines.h:31
PluginManager.h
lldb_private::ARMConditionPassed
static bool ARMConditionPassed(const uint32_t condition, const uint32_t cpsr)
Definition: ARMDefines.h:107
LLDB_INVALID_ADDRESS
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:86
lldb_private::AddressClass::eDebug
@ eDebug
lldb_private::AddressClass::eCodeAlternateISA
@ eCodeAlternateISA
lldb_private::ArchitectureArm::GetPluginName
ConstString GetPluginName() override
Definition: ArchitectureArm.cpp:42
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::ArchitectureArm::Terminate
static void Terminate()
Definition: ArchitectureArm.cpp:32
LLDB_PLUGIN_DEFINE
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:31
lldb_private::Thread::GetRegisterContext
virtual lldb::RegisterContextSP GetRegisterContext()=0
lldb
Definition: SBAddress.h:15
RegisterContext.h
UINT64_MAX
#define UINT64_MAX
Definition: lldb-defines.h:35
lldb_private::Bits32
static uint32_t Bits32(const uint32_t bits, const uint32_t msbit, const uint32_t lsbit)
Definition: InstructionUtils.h:29