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 
10 #include "SBReproducerPrivate.h"
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 
69 SBInstruction::SBInstruction() : m_opaque_sp() {
71 }
72 
73 SBInstruction::SBInstruction(const lldb::DisassemblerSP &disasm_sp,
74  const lldb::InstructionSP &inst_sp)
75  : m_opaque_sp(new InstructionImpl(disasm_sp, inst_sp)) {}
76 
78  : m_opaque_sp(rhs.m_opaque_sp) {
80 }
81 
84  SBInstruction, operator=,(const lldb::SBInstruction &),
85  rhs);
86 
87  if (this != &rhs)
89  return LLDB_RECORD_RESULT(*this);
90 }
91 
93 
96  return this->operator bool();
97 }
98 SBInstruction::operator bool() const {
99  LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBInstruction, operator bool);
100 
101  return m_opaque_sp && m_opaque_sp->IsValid();
102 }
103 
106 
107  SBAddress sb_addr;
108  lldb::InstructionSP inst_sp(GetOpaque());
109  if (inst_sp && inst_sp->GetAddress().IsValid())
110  sb_addr.SetAddress(&inst_sp->GetAddress());
111  return LLDB_RECORD_RESULT(sb_addr);
112 }
113 
116  target);
117 
118  lldb::InstructionSP inst_sp(GetOpaque());
119  if (inst_sp) {
120  ExecutionContext exe_ctx;
121  TargetSP target_sp(target.GetSP());
122  std::unique_lock<std::recursive_mutex> lock;
123  if (target_sp) {
124  lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
125 
126  target_sp->CalculateExecutionContext(exe_ctx);
127  exe_ctx.SetProcessSP(target_sp->GetProcessSP());
128  }
129  return inst_sp->GetMnemonic(&exe_ctx);
130  }
131  return nullptr;
132 }
133 
136  target);
137 
138  lldb::InstructionSP inst_sp(GetOpaque());
139  if (inst_sp) {
140  ExecutionContext exe_ctx;
141  TargetSP target_sp(target.GetSP());
142  std::unique_lock<std::recursive_mutex> lock;
143  if (target_sp) {
144  lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
145 
146  target_sp->CalculateExecutionContext(exe_ctx);
147  exe_ctx.SetProcessSP(target_sp->GetProcessSP());
148  }
149  return inst_sp->GetOperands(&exe_ctx);
150  }
151  return nullptr;
152 }
153 
154 const char *SBInstruction::GetComment(SBTarget target) {
156  target);
157 
158  lldb::InstructionSP inst_sp(GetOpaque());
159  if (inst_sp) {
160  ExecutionContext exe_ctx;
161  TargetSP target_sp(target.GetSP());
162  std::unique_lock<std::recursive_mutex> lock;
163  if (target_sp) {
164  lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
165 
166  target_sp->CalculateExecutionContext(exe_ctx);
167  exe_ctx.SetProcessSP(target_sp->GetProcessSP());
168  }
169  return inst_sp->GetComment(&exe_ctx);
170  }
171  return nullptr;
172 }
173 
176 
177  lldb::InstructionSP inst_sp(GetOpaque());
178  if (inst_sp)
179  return inst_sp->GetOpcode().GetByteSize();
180  return 0;
181 }
182 
185  target);
186 
187  lldb::SBData sb_data;
188  lldb::InstructionSP inst_sp(GetOpaque());
189  if (inst_sp) {
190  DataExtractorSP data_extractor_sp(new DataExtractor());
191  if (inst_sp->GetData(*data_extractor_sp)) {
192  sb_data.SetOpaque(data_extractor_sp);
193  }
194  }
195  return LLDB_RECORD_RESULT(sb_data);
196 }
197 
200 
201  lldb::InstructionSP inst_sp(GetOpaque());
202  if (inst_sp)
203  return inst_sp->DoesBranch();
204  return false;
205 }
206 
209 
210  lldb::InstructionSP inst_sp(GetOpaque());
211  if (inst_sp)
212  return inst_sp->HasDelaySlot();
213  return false;
214 }
215 
218 
219  lldb::InstructionSP inst_sp(GetOpaque());
220  if (inst_sp)
221  return inst_sp->CanSetBreakpoint();
222  return false;
223 }
224 
225 lldb::InstructionSP SBInstruction::GetOpaque() {
226  if (m_opaque_sp)
227  return m_opaque_sp->GetSP();
228  else
229  return lldb::InstructionSP();
230 }
231 
232 void SBInstruction::SetOpaque(const lldb::DisassemblerSP &disasm_sp,
233  const lldb::InstructionSP &inst_sp) {
234  m_opaque_sp = std::make_shared<InstructionImpl>(disasm_sp, inst_sp);
235 }
236 
239  s);
240 
241  lldb::InstructionSP inst_sp(GetOpaque());
242  if (inst_sp) {
243  SymbolContext sc;
244  const Address &addr = inst_sp->GetAddress();
245  ModuleSP module_sp(addr.GetModule());
246  if (module_sp)
247  module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything,
248  sc);
249  // Use the "ref()" instead of the "get()" accessor in case the SBStream
250  // didn't have a stream already created, one will get created...
251  FormatEntity::Entry format;
252  FormatEntity::Parse("${addr}: ", format);
253  inst_sp->Dump(&s.ref(), 0, true, false, nullptr, &sc, nullptr, &format, 0);
254  return true;
255  }
256  return false;
257 }
258 
259 void SBInstruction::Print(FILE *outp) {
260  LLDB_RECORD_METHOD(void, SBInstruction, Print, (FILE *), outp);
261  FileSP out = std::make_shared<NativeFile>(outp, /*take_ownership=*/false);
262  Print(out);
263 }
264 
267  Print(out.m_opaque_sp);
268 }
269 
270 void SBInstruction::Print(FileSP out_sp) {
271  LLDB_RECORD_METHOD(void, SBInstruction, Print, (FileSP), out_sp);
272 
273  if (!out_sp || !out_sp->IsValid())
274  return;
275 
276  lldb::InstructionSP inst_sp(GetOpaque());
277  if (inst_sp) {
278  SymbolContext sc;
279  const Address &addr = inst_sp->GetAddress();
280  ModuleSP module_sp(addr.GetModule());
281  if (module_sp)
282  module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything,
283  sc);
284  StreamFile out_stream(out_sp);
285  FormatEntity::Entry format;
286  FormatEntity::Parse("${addr}: ", format);
287  inst_sp->Dump(&out_stream, 0, true, false, nullptr, &sc, nullptr, &format,
288  0);
289  }
290 }
291 
293  uint32_t evaluate_options) {
295  (lldb::SBFrame &, uint32_t), frame, evaluate_options);
296 
297  lldb::InstructionSP inst_sp(GetOpaque());
298  if (inst_sp) {
299  lldb::StackFrameSP frame_sp(frame.GetFrameSP());
300 
301  if (frame_sp) {
303  frame_sp->CalculateExecutionContext(exe_ctx);
304  lldb_private::Target *target = exe_ctx.GetTargetPtr();
305  lldb_private::ArchSpec arch = target->GetArchitecture();
306 
307  return inst_sp->Emulate(
308  arch, evaluate_options, (void *)frame_sp.get(),
313  }
314  }
315  return false;
316 }
317 
318 bool SBInstruction::DumpEmulation(const char *triple) {
319  LLDB_RECORD_METHOD(bool, SBInstruction, DumpEmulation, (const char *),
320  triple);
321 
322  lldb::InstructionSP inst_sp(GetOpaque());
323  if (inst_sp && triple) {
324  return inst_sp->DumpEmulation(HostInfo::GetAugmentedArchSpec(triple));
325  }
326  return false;
327 }
328 
330  const char *test_file) {
332  (lldb::SBStream &, const char *), output_stream,
333  test_file);
334 
335  if (!m_opaque_sp)
336  SetOpaque(lldb::DisassemblerSP(),
337  lldb::InstructionSP(new PseudoInstruction()));
338 
339  lldb::InstructionSP inst_sp(GetOpaque());
340  if (inst_sp)
341  return inst_sp->TestEmulation(output_stream.get(), test_file);
342  return false;
343 }
344 
345 namespace lldb_private {
346 namespace repro {
347 
348 template <>
353  const lldb::SBInstruction &,
354  SBInstruction, operator=,(const lldb::SBInstruction &));
356  LLDB_REGISTER_METHOD_CONST(bool, SBInstruction, operator bool, ());
359  (lldb::SBTarget));
361  (lldb::SBTarget));
363  (lldb::SBTarget));
366  (lldb::SBTarget));
371  (lldb::SBStream &));
372  LLDB_REGISTER_METHOD(void, SBInstruction, Print, (FILE *));
374  LLDB_REGISTER_METHOD(void, SBInstruction, Print, (FileSP));
376  (lldb::SBFrame &, uint32_t));
377  LLDB_REGISTER_METHOD(bool, SBInstruction, DumpEmulation, (const char *));
379  (lldb::SBStream &, const char *));
380 }
381 
382 }
383 }
The registry contains a unique mapping between functions and their ID.
An data extractor class.
Definition: DataExtractor.h:46
void Print(FILE *out)
A class that represents a running process on the host machine.
#define LLDB_RECORD_CONSTRUCTOR_NO_ARGS(Class)
void SetOpaque(const lldb::DisassemblerSP &disasm_sp, const lldb::InstructionSP &inst_sp)
const ArchSpec & GetArchitecture() const
Definition: Target.h:942
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:33
bool TestEmulation(lldb::SBStream &output_stream, const char *test_file)
An architecture specification class.
Definition: ArchSpec.h:33
void SetOpaque(const lldb::DataExtractorSP &data_sp)
Definition: SBData.cpp:46
InstructionImpl(const lldb::DisassemblerSP &disasm_sp, const lldb::InstructionSP &inst_sp)
void SetAddress(lldb::SBSection section, lldb::addr_t offset)
Definition: SBAddress.cpp:95
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
const char * GetOperands(lldb::SBTarget target)
const SBInstruction & operator=(const SBInstruction &rhs)
bool IsValid() const
Target * GetTargetPtr() const
Returns a pointer to the target object.
#define LLDB_REGISTER_CONSTRUCTOR(Class, Signature)
#define LLDB_RECORD_METHOD_NO_ARGS(Result, Class, Method)
lldb::TargetSP GetSP() const
Definition: SBTarget.cpp:608
lldb::SBData GetData(lldb::SBTarget target)
const char * GetMnemonic(lldb::SBTarget target)
#define LLDB_RECORD_CONSTRUCTOR(Class, Signature,...)
lldb::InstructionSP GetOpaque()
static bool WriteRegisterFrame(EmulateInstruction *instruction, void *baton, const Context &context, const RegisterInfo *reg_info, const RegisterValue &reg_value)
#define LLDB_RECORD_METHOD(Result, Class, Method, Signature,...)
static size_t WriteMemoryFrame(EmulateInstruction *instruction, void *baton, const Context &context, lldb::addr_t addr, const void *dst, size_t length)
lldb::InstructionSP GetSP() const
A section + offset based address class.
Definition: Address.h:59
void SetProcessSP(const lldb::ProcessSP &process_sp)
Set accessor to set only the process shared pointer.
static bool ReadRegisterFrame(EmulateInstruction *instruction, void *baton, const RegisterInfo *reg_info, RegisterValue &reg_value)
lldb_private::Stream & ref()
Definition: SBStream.cpp:178
lldb::InstructionSP m_inst_sp
std::shared_ptr< InstructionImpl > m_opaque_sp
Definition: SBInstruction.h:84
void RegisterMethods< SBInstruction >(Registry &R)
static size_t ReadMemoryFrame(EmulateInstruction *instruction, void *baton, const Context &context, lldb::addr_t addr, void *dst, size_t length)
lldb::DisassemblerSP m_disasm_sp
lldb::StackFrameSP GetFrameSP() const
Definition: SBFrame.cpp:84
FileSP m_opaque_sp
Definition: SBFile.h:45
lldb::ModuleSP GetModule() const
Get accessor for the module for this address.
Definition: Address.cpp:282
bool DumpEmulation(const char *triple)
Definition: SBAddress.h:15
lldb_private::Stream * get()
Definition: SBStream.cpp:176
bool GetDescription(lldb::SBStream &description)
#define LLDB_REGISTER_METHOD(Result, Class, Method, Signature)
#define LLDB_RECORD_METHOD_CONST_NO_ARGS(Result, Class, Method)
const char * GetComment(lldb::SBTarget target)
#define LLDB_RECORD_RESULT(Result)
#define LLDB_REGISTER_METHOD_CONST(Result, Class, Method, Signature)
class LLDB_API SBInstruction
Definition: SBDefines.h:51
bool EmulateWithFrame(lldb::SBFrame &frame, uint32_t evaluate_options)