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
9 #include "lldb/API/SBInstruction.h"
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"
20 #include "lldb/Core/EmulateInstruction.h"
21 #include "lldb/Core/Module.h"
22 #include "lldb/Core/StreamFile.h"
23 #include "lldb/Host/HostInfo.h"
24 #include "lldb/Target/ExecutionContext.h"
25 #include "lldb/Target/StackFrame.h"
26 #include "lldb/Target/Target.h"
27 #include "lldb/Utility/ArchSpec.h"
28 #include "lldb/Utility/DataBufferHeap.h"
29 #include "lldb/Utility/DataExtractor.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.
51 class InstructionImpl {
52 public:
InstructionImpl(const lldb::DisassemblerSP & disasm_sp,const lldb::InstructionSP & inst_sp)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
GetSP() const57 lldb::InstructionSP GetSP() const { return m_inst_sp; }
58
IsValid() const59 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
SBInstruction()69 SBInstruction::SBInstruction() : m_opaque_sp() {
70 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBInstruction);
71 }
72
SBInstruction(const lldb::DisassemblerSP & disasm_sp,const lldb::InstructionSP & inst_sp)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
SBInstruction(const SBInstruction & rhs)77 SBInstruction::SBInstruction(const SBInstruction &rhs)
78 : m_opaque_sp(rhs.m_opaque_sp) {
79 LLDB_RECORD_CONSTRUCTOR(SBInstruction, (const lldb::SBInstruction &), rhs);
80 }
81
operator =(const SBInstruction & rhs)82 const SBInstruction &SBInstruction::operator=(const SBInstruction &rhs) {
83 LLDB_RECORD_METHOD(const lldb::SBInstruction &,
84 SBInstruction, operator=,(const lldb::SBInstruction &),
85 rhs);
86
87 if (this != &rhs)
88 m_opaque_sp = rhs.m_opaque_sp;
89 return LLDB_RECORD_RESULT(*this);
90 }
91
92 SBInstruction::~SBInstruction() = default;
93
IsValid()94 bool SBInstruction::IsValid() {
95 LLDB_RECORD_METHOD_NO_ARGS(bool, SBInstruction, IsValid);
96 return this->operator bool();
97 }
operator bool() const98 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
GetAddress()104 SBAddress SBInstruction::GetAddress() {
105 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBAddress, SBInstruction, GetAddress);
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
GetMnemonic(SBTarget target)114 const char *SBInstruction::GetMnemonic(SBTarget target) {
115 LLDB_RECORD_METHOD(const char *, SBInstruction, GetMnemonic, (lldb::SBTarget),
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
GetOperands(SBTarget target)134 const char *SBInstruction::GetOperands(SBTarget target) {
135 LLDB_RECORD_METHOD(const char *, SBInstruction, GetOperands, (lldb::SBTarget),
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
GetComment(SBTarget target)154 const char *SBInstruction::GetComment(SBTarget target) {
155 LLDB_RECORD_METHOD(const char *, SBInstruction, GetComment, (lldb::SBTarget),
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
GetByteSize()174 size_t SBInstruction::GetByteSize() {
175 LLDB_RECORD_METHOD_NO_ARGS(size_t, SBInstruction, GetByteSize);
176
177 lldb::InstructionSP inst_sp(GetOpaque());
178 if (inst_sp)
179 return inst_sp->GetOpcode().GetByteSize();
180 return 0;
181 }
182
GetData(SBTarget target)183 SBData SBInstruction::GetData(SBTarget target) {
184 LLDB_RECORD_METHOD(lldb::SBData, SBInstruction, GetData, (lldb::SBTarget),
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
DoesBranch()198 bool SBInstruction::DoesBranch() {
199 LLDB_RECORD_METHOD_NO_ARGS(bool, SBInstruction, DoesBranch);
200
201 lldb::InstructionSP inst_sp(GetOpaque());
202 if (inst_sp)
203 return inst_sp->DoesBranch();
204 return false;
205 }
206
HasDelaySlot()207 bool SBInstruction::HasDelaySlot() {
208 LLDB_RECORD_METHOD_NO_ARGS(bool, SBInstruction, HasDelaySlot);
209
210 lldb::InstructionSP inst_sp(GetOpaque());
211 if (inst_sp)
212 return inst_sp->HasDelaySlot();
213 return false;
214 }
215
CanSetBreakpoint()216 bool SBInstruction::CanSetBreakpoint() {
217 LLDB_RECORD_METHOD_NO_ARGS(bool, SBInstruction, CanSetBreakpoint);
218
219 lldb::InstructionSP inst_sp(GetOpaque());
220 if (inst_sp)
221 return inst_sp->CanSetBreakpoint();
222 return false;
223 }
224
GetOpaque()225 lldb::InstructionSP SBInstruction::GetOpaque() {
226 if (m_opaque_sp)
227 return m_opaque_sp->GetSP();
228 else
229 return lldb::InstructionSP();
230 }
231
SetOpaque(const lldb::DisassemblerSP & disasm_sp,const lldb::InstructionSP & inst_sp)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
GetDescription(lldb::SBStream & s)237 bool SBInstruction::GetDescription(lldb::SBStream &s) {
238 LLDB_RECORD_METHOD(bool, SBInstruction, GetDescription, (lldb::SBStream &),
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
Print(FILE * outp)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
Print(SBFile out)265 void SBInstruction::Print(SBFile out) {
266 LLDB_RECORD_METHOD(void, SBInstruction, Print, (SBFile), out);
267 Print(out.m_opaque_sp);
268 }
269
Print(FileSP out_sp)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
EmulateWithFrame(lldb::SBFrame & frame,uint32_t evaluate_options)292 bool SBInstruction::EmulateWithFrame(lldb::SBFrame &frame,
293 uint32_t evaluate_options) {
294 LLDB_RECORD_METHOD(bool, SBInstruction, EmulateWithFrame,
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) {
302 lldb_private::ExecutionContext exe_ctx;
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(),
309 &lldb_private::EmulateInstruction::ReadMemoryFrame,
310 &lldb_private::EmulateInstruction::WriteMemoryFrame,
311 &lldb_private::EmulateInstruction::ReadRegisterFrame,
312 &lldb_private::EmulateInstruction::WriteRegisterFrame);
313 }
314 }
315 return false;
316 }
317
DumpEmulation(const char * triple)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
TestEmulation(lldb::SBStream & output_stream,const char * test_file)329 bool SBInstruction::TestEmulation(lldb::SBStream &output_stream,
330 const char *test_file) {
331 LLDB_RECORD_METHOD(bool, SBInstruction, TestEmulation,
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 <>
RegisterMethods(Registry & R)349 void RegisterMethods<SBInstruction>(Registry &R) {
350 LLDB_REGISTER_CONSTRUCTOR(SBInstruction, ());
351 LLDB_REGISTER_CONSTRUCTOR(SBInstruction, (const lldb::SBInstruction &));
352 LLDB_REGISTER_METHOD(
353 const lldb::SBInstruction &,
354 SBInstruction, operator=,(const lldb::SBInstruction &));
355 LLDB_REGISTER_METHOD(bool, SBInstruction, IsValid, ());
356 LLDB_REGISTER_METHOD_CONST(bool, SBInstruction, operator bool, ());
357 LLDB_REGISTER_METHOD(lldb::SBAddress, SBInstruction, GetAddress, ());
358 LLDB_REGISTER_METHOD(const char *, SBInstruction, GetMnemonic,
359 (lldb::SBTarget));
360 LLDB_REGISTER_METHOD(const char *, SBInstruction, GetOperands,
361 (lldb::SBTarget));
362 LLDB_REGISTER_METHOD(const char *, SBInstruction, GetComment,
363 (lldb::SBTarget));
364 LLDB_REGISTER_METHOD(size_t, SBInstruction, GetByteSize, ());
365 LLDB_REGISTER_METHOD(lldb::SBData, SBInstruction, GetData,
366 (lldb::SBTarget));
367 LLDB_REGISTER_METHOD(bool, SBInstruction, DoesBranch, ());
368 LLDB_REGISTER_METHOD(bool, SBInstruction, HasDelaySlot, ());
369 LLDB_REGISTER_METHOD(bool, SBInstruction, CanSetBreakpoint, ());
370 LLDB_REGISTER_METHOD(bool, SBInstruction, GetDescription,
371 (lldb::SBStream &));
372 LLDB_REGISTER_METHOD(void, SBInstruction, Print, (FILE *));
373 LLDB_REGISTER_METHOD(void, SBInstruction, Print, (SBFile));
374 LLDB_REGISTER_METHOD(void, SBInstruction, Print, (FileSP));
375 LLDB_REGISTER_METHOD(bool, SBInstruction, EmulateWithFrame,
376 (lldb::SBFrame &, uint32_t));
377 LLDB_REGISTER_METHOD(bool, SBInstruction, DumpEmulation, (const char *));
378 LLDB_REGISTER_METHOD(bool, SBInstruction, TestEmulation,
379 (lldb::SBStream &, const char *));
380 }
381
382 }
383 }
384