LLDB  mainline
SBInstruction.cpp
Go to the documentation of this file.
1 //===-- SBInstruction.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 
11 
12 #include "lldb/API/SBAddress.h"
13 #include "lldb/API/SBFrame.h"
14 #include "lldb/API/SBFile.h"
15 
16 #include "lldb/API/SBInstruction.h"
17 #include "lldb/API/SBStream.h"
18 #include "lldb/API/SBTarget.h"
19 #include "lldb/Core/Disassembler.h"
21 #include "lldb/Core/Module.h"
22 #include "lldb/Core/StreamFile.h"
23 #include "lldb/Host/HostInfo.h"
25 #include "lldb/Target/StackFrame.h"
26 #include "lldb/Target/Target.h"
27 #include "lldb/Utility/ArchSpec.h"
30 
31 #include <memory>
32 
33 // We recently fixed a leak in one of the Instruction subclasses where the
34 // instruction will only hold a weak reference to the disassembler to avoid a
35 // cycle that was keeping both objects alive (leak) and we need the
36 // InstructionImpl class to make sure our public API behaves as users would
37 // expect. Calls in our public API allow clients to do things like:
38 //
39 // 1 lldb::SBInstruction inst;
40 // 2 inst = target.ReadInstructions(pc, 1).GetInstructionAtIndex(0)
41 // 3 if (inst.DoesBranch())
42 // 4 ...
43 //
44 // There was a temporary lldb::DisassemblerSP object created in the
45 // SBInstructionList that was returned by lldb.target.ReadInstructions() that
46 // will go away after line 2 but the "inst" object should be able to still
47 // answer questions about itself. So we make sure that any SBInstruction
48 // objects that are given out have a strong reference to the disassembler and
49 // the instruction so that the object can live and successfully respond to all
50 // queries.
52 public:
53  InstructionImpl(const lldb::DisassemblerSP &disasm_sp,
54  const lldb::InstructionSP &inst_sp)
55  : m_disasm_sp(disasm_sp), m_inst_sp(inst_sp) {}
56 
57  lldb::InstructionSP GetSP() const { return m_inst_sp; }
58 
59  bool IsValid() const { return (bool)m_inst_sp; }
60 
61 protected:
62  lldb::DisassemblerSP m_disasm_sp; // Can be empty/invalid
63  lldb::InstructionSP m_inst_sp;
64 };
65 
66 using namespace lldb;
67 using namespace lldb_private;
68 
70 
71 SBInstruction::SBInstruction(const lldb::DisassemblerSP &disasm_sp,
72  const lldb::InstructionSP &inst_sp)
73  : m_opaque_sp(new InstructionImpl(disasm_sp, inst_sp)) {}
74 
76  : m_opaque_sp(rhs.m_opaque_sp) {
77  LLDB_INSTRUMENT_VA(this, rhs);
78 }
79 
81  LLDB_INSTRUMENT_VA(this, rhs);
82 
83  if (this != &rhs)
85  return *this;
86 }
87 
89 
91  LLDB_INSTRUMENT_VA(this);
92  return this->operator bool();
93 }
94 SBInstruction::operator bool() const {
95  LLDB_INSTRUMENT_VA(this);
96 
97  return m_opaque_sp && m_opaque_sp->IsValid();
98 }
99 
101  LLDB_INSTRUMENT_VA(this);
102 
103  SBAddress sb_addr;
104  lldb::InstructionSP inst_sp(GetOpaque());
105  if (inst_sp && inst_sp->GetAddress().IsValid())
106  sb_addr.SetAddress(inst_sp->GetAddress());
107  return sb_addr;
108 }
109 
111  LLDB_INSTRUMENT_VA(this, target);
112 
113  lldb::InstructionSP inst_sp(GetOpaque());
114  if (inst_sp) {
115  ExecutionContext exe_ctx;
116  TargetSP target_sp(target.GetSP());
117  std::unique_lock<std::recursive_mutex> lock;
118  if (target_sp) {
119  lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
120 
121  target_sp->CalculateExecutionContext(exe_ctx);
122  exe_ctx.SetProcessSP(target_sp->GetProcessSP());
123  }
124  return inst_sp->GetMnemonic(&exe_ctx);
125  }
126  return nullptr;
127 }
128 
130  LLDB_INSTRUMENT_VA(this, target);
131 
132  lldb::InstructionSP inst_sp(GetOpaque());
133  if (inst_sp) {
134  ExecutionContext exe_ctx;
135  TargetSP target_sp(target.GetSP());
136  std::unique_lock<std::recursive_mutex> lock;
137  if (target_sp) {
138  lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
139 
140  target_sp->CalculateExecutionContext(exe_ctx);
141  exe_ctx.SetProcessSP(target_sp->GetProcessSP());
142  }
143  return inst_sp->GetOperands(&exe_ctx);
144  }
145  return nullptr;
146 }
147 
148 const char *SBInstruction::GetComment(SBTarget target) {
149  LLDB_INSTRUMENT_VA(this, target);
150 
151  lldb::InstructionSP inst_sp(GetOpaque());
152  if (inst_sp) {
153  ExecutionContext exe_ctx;
154  TargetSP target_sp(target.GetSP());
155  std::unique_lock<std::recursive_mutex> lock;
156  if (target_sp) {
157  lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
158 
159  target_sp->CalculateExecutionContext(exe_ctx);
160  exe_ctx.SetProcessSP(target_sp->GetProcessSP());
161  }
162  return inst_sp->GetComment(&exe_ctx);
163  }
164  return nullptr;
165 }
166 
168  LLDB_INSTRUMENT_VA(this);
169 
170  lldb::InstructionSP inst_sp(GetOpaque());
171  if (inst_sp)
172  return inst_sp->GetOpcode().GetByteSize();
173  return 0;
174 }
175 
177  LLDB_INSTRUMENT_VA(this, target);
178 
179  lldb::SBData sb_data;
180  lldb::InstructionSP inst_sp(GetOpaque());
181  if (inst_sp) {
182  DataExtractorSP data_extractor_sp(new DataExtractor());
183  if (inst_sp->GetData(*data_extractor_sp)) {
184  sb_data.SetOpaque(data_extractor_sp);
185  }
186  }
187  return sb_data;
188 }
189 
191  LLDB_INSTRUMENT_VA(this);
192 
193  lldb::InstructionSP inst_sp(GetOpaque());
194  if (inst_sp)
195  return inst_sp->DoesBranch();
196  return false;
197 }
198 
200  LLDB_INSTRUMENT_VA(this);
201 
202  lldb::InstructionSP inst_sp(GetOpaque());
203  if (inst_sp)
204  return inst_sp->HasDelaySlot();
205  return false;
206 }
207 
209  LLDB_INSTRUMENT_VA(this);
210 
211  lldb::InstructionSP inst_sp(GetOpaque());
212  if (inst_sp)
213  return inst_sp->CanSetBreakpoint();
214  return false;
215 }
216 
217 lldb::InstructionSP SBInstruction::GetOpaque() {
218  if (m_opaque_sp)
219  return m_opaque_sp->GetSP();
220  else
221  return lldb::InstructionSP();
222 }
223 
224 void SBInstruction::SetOpaque(const lldb::DisassemblerSP &disasm_sp,
225  const lldb::InstructionSP &inst_sp) {
226  m_opaque_sp = std::make_shared<InstructionImpl>(disasm_sp, inst_sp);
227 }
228 
230  LLDB_INSTRUMENT_VA(this, s);
231 
232  lldb::InstructionSP inst_sp(GetOpaque());
233  if (inst_sp) {
234  SymbolContext sc;
235  const Address &addr = inst_sp->GetAddress();
236  ModuleSP module_sp(addr.GetModule());
237  if (module_sp)
238  module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything,
239  sc);
240  // Use the "ref()" instead of the "get()" accessor in case the SBStream
241  // didn't have a stream already created, one will get created...
242  FormatEntity::Entry format;
243  FormatEntity::Parse("${addr}: ", format);
244  inst_sp->Dump(&s.ref(), 0, true, false, /*show_control_flow_kind=*/false,
245  nullptr, &sc, nullptr, &format, 0);
246  return true;
247  }
248  return false;
249 }
250 
251 void SBInstruction::Print(FILE *outp) {
252  LLDB_INSTRUMENT_VA(this, outp);
253  FileSP out = std::make_shared<NativeFile>(outp, /*take_ownership=*/false);
254  Print(out);
255 }
256 
258  LLDB_INSTRUMENT_VA(this, out);
259  Print(out.m_opaque_sp);
260 }
261 
262 void SBInstruction::Print(FileSP out_sp) {
263  LLDB_INSTRUMENT_VA(this, out_sp);
264 
265  if (!out_sp || !out_sp->IsValid())
266  return;
267 
268  lldb::InstructionSP inst_sp(GetOpaque());
269  if (inst_sp) {
270  SymbolContext sc;
271  const Address &addr = inst_sp->GetAddress();
272  ModuleSP module_sp(addr.GetModule());
273  if (module_sp)
274  module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything,
275  sc);
276  StreamFile out_stream(out_sp);
277  FormatEntity::Entry format;
278  FormatEntity::Parse("${addr}: ", format);
279  inst_sp->Dump(&out_stream, 0, true, false, /*show_control_flow_kind=*/false,
280  nullptr, &sc, nullptr, &format, 0);
281  }
282 }
283 
285  uint32_t evaluate_options) {
286  LLDB_INSTRUMENT_VA(this, frame, evaluate_options);
287 
288  lldb::InstructionSP inst_sp(GetOpaque());
289  if (inst_sp) {
290  lldb::StackFrameSP frame_sp(frame.GetFrameSP());
291 
292  if (frame_sp) {
294  frame_sp->CalculateExecutionContext(exe_ctx);
295  lldb_private::Target *target = exe_ctx.GetTargetPtr();
296  lldb_private::ArchSpec arch = target->GetArchitecture();
297 
298  return inst_sp->Emulate(
299  arch, evaluate_options, (void *)frame_sp.get(),
304  }
305  }
306  return false;
307 }
308 
309 bool SBInstruction::DumpEmulation(const char *triple) {
310  LLDB_INSTRUMENT_VA(this, triple);
311 
312  lldb::InstructionSP inst_sp(GetOpaque());
313  if (inst_sp && triple) {
314  return inst_sp->DumpEmulation(HostInfo::GetAugmentedArchSpec(triple));
315  }
316  return false;
317 }
318 
320  const char *test_file) {
321  LLDB_INSTRUMENT_VA(this, output_stream, test_file);
322 
323  if (!m_opaque_sp)
324  SetOpaque(lldb::DisassemblerSP(),
325  lldb::InstructionSP(new PseudoInstruction()));
326 
327  lldb::InstructionSP inst_sp(GetOpaque());
328  if (inst_sp)
329  return inst_sp->TestEmulation(output_stream.get(), test_file);
330  return false;
331 }
Instrumentation.h
lldb::SBInstruction::DumpEmulation
bool DumpEmulation(const char *triple)
Definition: SBInstruction.cpp:309
lldb_private::ExecutionContext
Definition: ExecutionContext.h:292
lldb::SBStream::ref
lldb_private::Stream & ref()
Definition: SBStream.cpp:176
lldb_private::ArchSpec
Definition: ArchSpec.h:33
lldb::SBInstruction::GetData
lldb::SBData GetData(lldb::SBTarget target)
Definition: SBInstruction.cpp:176
lldb::SBFile::m_opaque_sp
FileSP m_opaque_sp
Definition: SBFile.h:45
lldb::SBFrame::GetFrameSP
lldb::StackFrameSP GetFrameSP() const
Definition: SBFrame.cpp:83
lldb_private::EmulateInstruction::WriteRegisterFrame
static bool WriteRegisterFrame(EmulateInstruction *instruction, void *baton, const Context &context, const RegisterInfo *reg_info, const RegisterValue &reg_value)
Definition: EmulateInstruction.cpp:293
lldb::SBInstruction::HasDelaySlot
bool HasDelaySlot()
Definition: SBInstruction.cpp:199
lldb::SBInstruction::GetAddress
SBAddress GetAddress()
Definition: SBInstruction.cpp:100
Module.h
lldb_private::PseudoInstruction
Definition: Disassembler.h:343
lldb::SBInstruction::Print
void Print(FILE *out)
Definition: SBInstruction.cpp:251
lldb::SBInstruction::GetOperands
const char * GetOperands(lldb::SBTarget target)
Definition: SBInstruction.cpp:129
InstructionImpl
Definition: SBInstruction.cpp:51
StackFrame.h
Disassembler.h
lldb_private::EmulateInstruction::ReadRegisterFrame
static bool ReadRegisterFrame(EmulateInstruction *instruction, void *baton, const RegisterInfo *reg_info, RegisterValue &reg_value)
Definition: EmulateInstruction.cpp:282
lldb::SBAddress::SetAddress
void SetAddress(lldb::SBSection section, lldb::addr_t offset)
Definition: SBAddress.cpp:88
InstructionImpl::m_disasm_sp
lldb::DisassemblerSP m_disasm_sp
Definition: SBInstruction.cpp:62
lldb_private::SymbolContext
Definition: SymbolContext.h:33
lldb_private::Target
Definition: Target.h:467
lldb::SBInstruction::SBInstruction
SBInstruction()
Definition: SBInstruction.cpp:69
lldb::SBInstruction::SetOpaque
void SetOpaque(const lldb::DisassemblerSP &disasm_sp, const lldb::InstructionSP &inst_sp)
Definition: SBInstruction.cpp:224
lldb::SBInstruction::operator=
const SBInstruction & operator=(const SBInstruction &rhs)
Definition: SBInstruction.cpp:80
Target.h
lldb::SBTarget
Definition: SBTarget.h:29
SBStream.h
lldb::SBFile
Definition: SBFile.h:16
lldb::SBFrame
Definition: SBFrame.h:17
lldb::SBStream::get
lldb_private::Stream * get()
Definition: SBStream.cpp:174
EmulateInstruction.h
lldb_private::DataExtractor
Definition: DataExtractor.h:48
lldb::SBInstruction::CanSetBreakpoint
bool CanSetBreakpoint()
Definition: SBInstruction.cpp:208
lldb::SBInstruction::~SBInstruction
~SBInstruction()
SBFrame.h
lldb::SBInstruction::DoesBranch
bool DoesBranch()
Definition: SBInstruction.cpp:190
lldb::SBInstruction::IsValid
bool IsValid()
Definition: SBInstruction.cpp:90
lldb::SBInstruction::m_opaque_sp
std::shared_ptr< InstructionImpl > m_opaque_sp
Definition: SBInstruction.h:84
InstructionImpl::InstructionImpl
InstructionImpl(const lldb::DisassemblerSP &disasm_sp, const lldb::InstructionSP &inst_sp)
Definition: SBInstruction.cpp:53
lldb::SBInstruction::GetByteSize
size_t GetByteSize()
Definition: SBInstruction.cpp:167
HostInfo.h
lldb::SBInstruction::GetOpaque
lldb::InstructionSP GetOpaque()
Definition: SBInstruction.cpp:217
SBAddress.h
StreamFile.h
lldb_private::EmulateInstruction::WriteMemoryFrame
static size_t WriteMemoryFrame(EmulateInstruction *instruction, void *baton, const Context &context, lldb::addr_t addr, const void *dst, size_t length)
Definition: EmulateInstruction.cpp:264
SBFile.h
lldb::SBData::SetOpaque
void SetOpaque(const lldb::DataExtractorSP &data_sp)
Definition: SBData.cpp:45
lldb_private::Target::GetArchitecture
const ArchSpec & GetArchitecture() const
Definition: Target.h:983
lldb::SBInstruction::GetDescription
bool GetDescription(lldb::SBStream &description)
Definition: SBInstruction.cpp:229
InstructionImpl::IsValid
bool IsValid() const
Definition: SBInstruction.cpp:59
uint32_t
lldb::SBInstruction::EmulateWithFrame
bool EmulateWithFrame(lldb::SBFrame &frame, uint32_t evaluate_options)
Definition: SBInstruction.cpp:284
lldb_private::Address
Definition: Address.h:59
SBInstruction.h
lldb::SBData
Definition: SBData.h:20
ArchSpec.h
lldb_private::FormatEntity::Entry
Definition: FormatEntity.h:40
lldb::SBInstruction::GetMnemonic
const char * GetMnemonic(lldb::SBTarget target)
Definition: SBInstruction.cpp:110
lldb::SBInstruction
Definition: SBInstruction.h:24
DataExtractor.h
lldb::SBAddress
Definition: SBAddress.h:17
lldb_private::EmulateInstruction::ReadMemoryFrame
static size_t ReadMemoryFrame(EmulateInstruction *instruction, void *baton, const Context &context, lldb::addr_t addr, void *dst, size_t length)
Definition: EmulateInstruction.cpp:247
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb::SBInstruction::TestEmulation
bool TestEmulation(lldb::SBStream &output_stream, const char *test_file)
Definition: SBInstruction.cpp:319
lldb::SBInstruction::GetComment
const char * GetComment(lldb::SBTarget target)
Definition: SBInstruction.cpp:148
InstructionImpl::GetSP
lldb::InstructionSP GetSP() const
Definition: SBInstruction.cpp:57
lldb::SBTarget::GetSP
lldb::TargetSP GetSP() const
Definition: SBTarget.cpp:576
LLDB_INSTRUMENT_VA
#define LLDB_INSTRUMENT_VA(...)
Definition: Instrumentation.h:98
lldb_private::StreamFile
Definition: StreamFile.h:22
lldb_private::Address::GetModule
lldb::ModuleSP GetModule() const
Get accessor for the module for this address.
Definition: Address.cpp:283
InstructionImpl::m_inst_sp
lldb::InstructionSP m_inst_sp
Definition: SBInstruction.cpp:63
lldb::SBStream
Definition: SBStream.h:18
SBTarget.h
lldb
Definition: SBAddress.h:15
lldb_private::ExecutionContext::GetTargetPtr
Target * GetTargetPtr() const
Returns a pointer to the target object.
Definition: ExecutionContext.cpp:198
ExecutionContext.h
DataBufferHeap.h
lldb_private::ExecutionContext::SetProcessSP
void SetProcessSP(const lldb::ProcessSP &process_sp)
Set accessor to set only the process shared pointer.
Definition: ExecutionContext.cpp:248