LLDB  mainline
ArchitectureMips.cpp
Go to the documentation of this file.
1 //===-- ArchitectureMips.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 
10 #include "lldb/Core/Address.h"
11 #include "lldb/Core/Disassembler.h"
12 #include "lldb/Core/Module.h"
14 #include "lldb/Symbol/Function.h"
17 #include "lldb/Target/Target.h"
18 #include "lldb/Utility/ArchSpec.h"
19 #include "lldb/Utility/Log.h"
20 
21 using namespace lldb_private;
22 using namespace lldb;
23 
25 
26 ConstString ArchitectureMips::GetPluginNameStatic() {
27  return ConstString("mips");
28 }
29 
31  PluginManager::RegisterPlugin(GetPluginNameStatic(),
32  "Mips-specific algorithms",
34 }
35 
38 }
39 
40 std::unique_ptr<Architecture> ArchitectureMips::Create(const ArchSpec &arch) {
41  return arch.IsMIPS() ?
42  std::unique_ptr<Architecture>(new ArchitectureMips(arch)) : nullptr;
43 }
44 
45 ConstString ArchitectureMips::GetPluginName() { return GetPluginNameStatic(); }
46 
48  AddressClass addr_class) const {
49  bool is_alternate_isa = false;
50 
51  switch (addr_class) {
54  return LLDB_INVALID_ADDRESS;
56  is_alternate_isa = true;
57  break;
58  default: break;
59  }
60 
61  if ((code_addr & 2ull) || is_alternate_isa)
62  return code_addr | 1u;
63  return code_addr;
64 }
65 
67  AddressClass addr_class) const {
68  switch (addr_class) {
71  return LLDB_INVALID_ADDRESS;
72  default: break;
73  }
74  return opcode_addr & ~(1ull);
75 }
76 
78  Target &target) const {
79 
81 
82  Address resolved_addr;
83 
84  SectionLoadList &section_load_list = target.GetSectionLoadList();
85  if (section_load_list.IsEmpty())
86  // No sections are loaded, so we must assume we are not running yet and
87  // need to operate only on file address.
88  target.ResolveFileAddress(addr, resolved_addr);
89  else
90  target.ResolveLoadAddress(addr, resolved_addr);
91 
92  addr_t current_offset = 0;
93 
94  // Get the function boundaries to make sure we don't scan back before the
95  // beginning of the current function.
96  ModuleSP temp_addr_module_sp(resolved_addr.GetModule());
97  if (temp_addr_module_sp) {
98  SymbolContext sc;
99  SymbolContextItem resolve_scope =
100  eSymbolContextFunction | eSymbolContextSymbol;
101  temp_addr_module_sp->ResolveSymbolContextForAddress(resolved_addr,
102  resolve_scope, sc);
103  Address sym_addr;
104  if (sc.function)
105  sym_addr = sc.function->GetAddressRange().GetBaseAddress();
106  else if (sc.symbol)
107  sym_addr = sc.symbol->GetAddress();
108 
109  addr_t function_start = sym_addr.GetLoadAddress(&target);
110  if (function_start == LLDB_INVALID_ADDRESS)
111  function_start = sym_addr.GetFileAddress();
112 
113  if (function_start)
114  current_offset = addr - function_start;
115  }
116 
117  // If breakpoint address is start of function then we dont have to do
118  // anything.
119  if (current_offset == 0)
120  return addr;
121 
122  auto insn = GetInstructionAtAddress(target, current_offset, addr);
123 
124  if (nullptr == insn || !insn->HasDelaySlot())
125  return addr;
126 
127  // Adjust the breakable address
128  uint64_t breakable_addr = addr - insn->GetOpcode().GetByteSize();
129  LLDB_LOGF(log,
130  "Target::%s Breakpoint at 0x%8.8" PRIx64
131  " is adjusted to 0x%8.8" PRIx64 " due to delay slot\n",
132  __FUNCTION__, addr, breakable_addr);
133 
134  return breakable_addr;
135 }
136 
138  Target &target, const Address &resolved_addr, addr_t symbol_offset) const {
139 
140  auto loop_count = symbol_offset / 2;
141 
142  uint32_t arch_flags = m_arch.GetFlags();
143  bool IsMips16 = arch_flags & ArchSpec::eMIPSAse_mips16;
144  bool IsMicromips = arch_flags & ArchSpec::eMIPSAse_micromips;
145 
146  if (loop_count > 3) {
147  // Scan previous 6 bytes
148  if (IsMips16 | IsMicromips)
149  loop_count = 3;
150  // For mips-only, instructions are always 4 bytes, so scan previous 4
151  // bytes only.
152  else
153  loop_count = 2;
154  }
155 
156  // Create Disassembler Instance
157  lldb::DisassemblerSP disasm_sp(
158  Disassembler::FindPlugin(m_arch, nullptr, nullptr));
159 
160  InstructionList instruction_list;
161  InstructionSP prev_insn;
162  uint32_t inst_to_choose = 0;
163 
164  Address addr = resolved_addr;
165 
166  for (uint32_t i = 1; i <= loop_count; i++) {
167  // Adjust the address to read from.
168  addr.Slide(-2);
169  uint32_t insn_size = 0;
170 
171  disasm_sp->ParseInstructions(target, addr,
172  {Disassembler::Limit::Bytes, i * 2}, nullptr);
173 
174  uint32_t num_insns = disasm_sp->GetInstructionList().GetSize();
175  if (num_insns) {
176  prev_insn = disasm_sp->GetInstructionList().GetInstructionAtIndex(0);
177  insn_size = prev_insn->GetOpcode().GetByteSize();
178  if (i == 1 && insn_size == 2) {
179  // This looks like a valid 2-byte instruction (but it could be a part
180  // of upper 4 byte instruction).
181  instruction_list.Append(prev_insn);
182  inst_to_choose = 1;
183  }
184  else if (i == 2) {
185  // Here we may get one 4-byte instruction or two 2-byte instructions.
186  if (num_insns == 2) {
187  // Looks like there are two 2-byte instructions above our
188  // breakpoint target address. Now the upper 2-byte instruction is
189  // either a valid 2-byte instruction or could be a part of it's
190  // upper 4-byte instruction. In both cases we don't care because in
191  // this case lower 2-byte instruction is definitely a valid
192  // instruction and whatever i=1 iteration has found out is true.
193  inst_to_choose = 1;
194  break;
195  }
196  else if (insn_size == 4) {
197  // This instruction claims its a valid 4-byte instruction. But it
198  // could be a part of it's upper 4-byte instruction. Lets try
199  // scanning upper 2 bytes to verify this.
200  instruction_list.Append(prev_insn);
201  inst_to_choose = 2;
202  }
203  }
204  else if (i == 3) {
205  if (insn_size == 4)
206  // FIXME: We reached here that means instruction at [target - 4] has
207  // already claimed to be a 4-byte instruction, and now instruction
208  // at [target - 6] is also claiming that it's a 4-byte instruction.
209  // This can not be true. In this case we can not decide the valid
210  // previous instruction so we let lldb set the breakpoint at the
211  // address given by user.
212  inst_to_choose = 0;
213  else
214  // This is straight-forward
215  inst_to_choose = 2;
216  break;
217  }
218  }
219  else {
220  // Decode failed, bytes do not form a valid instruction. So whatever
221  // previous iteration has found out is true.
222  if (i > 1) {
223  inst_to_choose = i - 1;
224  break;
225  }
226  }
227  }
228 
229  // Check if we are able to find any valid instruction.
230  if (inst_to_choose) {
231  if (inst_to_choose > instruction_list.GetSize())
232  inst_to_choose--;
233  return instruction_list.GetInstructionAtIndex(inst_to_choose - 1).get();
234  }
235 
236  return nullptr;
237 }
lldb_private::AddressClass::eData
@ eData
lldb_private::AddressRange::GetBaseAddress
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:209
lldb_private::ArchitectureMips::GetBreakableLoadAddress
lldb::addr_t GetBreakableLoadAddress(lldb::addr_t addr, Target &) const override
Definition: ArchitectureMips.cpp:77
lldb_private::ArchSpec
Definition: ArchSpec.h:33
lldb_private::ArchSpec::IsMIPS
bool IsMIPS() const
if MIPS architecture return true.
Definition: ArchSpec.cpp:545
lldb_private::Function::GetAddressRange
const AddressRange & GetAddressRange()
Definition: Function.h:461
lldb_private::AddressClass
AddressClass
Definition: lldb-private-enumerations.h:48
lldb_private::Disassembler::FindPlugin
static lldb::DisassemblerSP FindPlugin(const ArchSpec &arch, const char *flavor, const char *plugin_name)
Definition: Disassembler.cpp:58
LLDB_LOGF
#define LLDB_LOGF(log,...)
Definition: Log.h:249
lldb_private::ArchitectureMips::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: ArchitectureMips.cpp:66
lldb_private::ArchitectureMips::Initialize
static void Initialize()
Definition: ArchitectureMips.cpp:30
Module.h
lldb_private::Disassembler::Limit::Bytes
@ Bytes
Definition: Disassembler.h:401
SectionLoadList.h
Disassembler.h
lldb_private::InstructionList::GetInstructionAtIndex
lldb::InstructionSP GetInstructionAtIndex(size_t idx) const
Definition: Disassembler.cpp:946
lldb_private::Instruction
Definition: Disassembler.h:58
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::Target::ResolveLoadAddress
bool ResolveLoadAddress(lldb::addr_t load_addr, Address &so_addr, uint32_t stop_id=SectionLoadHistory::eStopIDNow)
Definition: Target.cpp:2827
ArchitectureMips.h
lldb_private::SymbolContext
Definition: SymbolContext.h:33
lldb_private::Target
Definition: Target.h:454
lldb_private::ArchitectureMips
Definition: ArchitectureMips.h:17
lldb_private::ArchitectureMips::Create
static std::unique_ptr< Architecture > Create(const ArchSpec &arch)
Definition: ArchitectureMips.cpp:40
lldb_private::PluginManager::RegisterPlugin
static bool RegisterPlugin(ConstString name, const char *description, ABICreateInstance create_callback)
Definition: PluginManager.cpp:288
Target.h
lldb_private::SymbolContext::symbol
Symbol * symbol
The Symbol for a given query.
Definition: SymbolContext.h:323
lldb_private::InstructionList
Definition: Disassembler.h:267
lldb_private::PluginManager::UnregisterPlugin
static bool UnregisterPlugin(ABICreateInstance create_callback)
Definition: PluginManager.cpp:293
Log.h
lldb_private::ArchitectureMips::GetInstructionAtAddress
Instruction * GetInstructionAtAddress(Target &target, const Address &resolved_addr, lldb::addr_t symbol_offset) const
Definition: ArchitectureMips.cpp:137
lldb_private::SymbolContext::function
Function * function
The Function for a given query.
Definition: SymbolContext.h:320
lldb_private::Address::GetFileAddress
lldb::addr_t GetFileAddress() const
Get the file address.
Definition: Address.cpp:290
lldb_private::Target::ResolveFileAddress
bool ResolveFileAddress(lldb::addr_t load_addr, Address &so_addr)
Definition: Target.cpp:2832
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::ArchitectureMips::GetPluginName
ConstString GetPluginName() override
Definition: ArchitectureMips.cpp:45
lldb_private::GetLogIfAllCategoriesSet
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:58
lldb_private::SectionLoadList
Definition: SectionLoadList.h:22
Address.h
lldb_private::SectionLoadList::IsEmpty
bool IsEmpty() const
Definition: SectionLoadList.cpp:37
uint32_t
LIBLLDB_LOG_BREAKPOINTS
#define LIBLLDB_LOG_BREAKPOINTS
Definition: Logging.h:19
lldb_private::Address
Definition: Address.h:59
lldb_private::Address::GetLoadAddress
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition: Address.cpp:310
ArchSpec.h
lldb_private::ArchitectureMips::Terminate
static void Terminate()
Definition: ArchitectureMips.cpp:36
PluginManager.h
lldb_private::Target::GetSectionLoadList
SectionLoadList & GetSectionLoadList()
Definition: Target.h:1043
LLDB_INVALID_ADDRESS
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:86
lldb_private::ArchSpec::eMIPSAse_mips16
@ eMIPSAse_mips16
Definition: ArchSpec.h:63
Function.h
lldb_private::AddressClass::eDebug
@ eDebug
lldb_private::InstructionList::GetSize
size_t GetSize() const
Definition: Disassembler.cpp:932
lldb_private::AddressClass::eCodeAlternateISA
@ eCodeAlternateISA
SymbolContext.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::ArchitectureMips::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: ArchitectureMips.cpp:47
lldb_private::ArchSpec::eMIPSAse_micromips
@ eMIPSAse_micromips
Definition: ArchSpec.h:64
lldb_private::Address::GetModule
lldb::ModuleSP GetModule() const
Get accessor for the module for this address.
Definition: Address.cpp:282
lldb_private::InstructionList::Append
void Append(lldb::InstructionSP &inst_sp)
Definition: Disassembler.cpp:986
LLDB_PLUGIN_DEFINE
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:31
lldb_private::Log
Definition: Log.h:49
lldb_private::Address::Slide
bool Slide(int64_t offset)
Definition: Address.h:440
lldb
Definition: SBAddress.h:15
lldb_private::Symbol::GetAddress
Address GetAddress() const
Definition: Symbol.h:73