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 "lldb/Utility/Instrumentation.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:
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() { LLDB_INSTRUMENT_VA(this); }
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 
75 SBInstruction::SBInstruction(const SBInstruction &rhs)
76     : m_opaque_sp(rhs.m_opaque_sp) {
77   LLDB_INSTRUMENT_VA(this, rhs);
78 }
79 
80 const SBInstruction &SBInstruction::operator=(const SBInstruction &rhs) {
81   LLDB_INSTRUMENT_VA(this, rhs);
82 
83   if (this != &rhs)
84     m_opaque_sp = rhs.m_opaque_sp;
85   return *this;
86 }
87 
88 SBInstruction::~SBInstruction() = default;
89 
90 bool SBInstruction::IsValid() {
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 
100 SBAddress SBInstruction::GetAddress() {
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 
110 const char *SBInstruction::GetMnemonic(SBTarget target) {
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 
129 const char *SBInstruction::GetOperands(SBTarget target) {
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 
167 size_t SBInstruction::GetByteSize() {
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 
176 SBData SBInstruction::GetData(SBTarget target) {
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 
190 bool SBInstruction::DoesBranch() {
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 
199 bool SBInstruction::HasDelaySlot() {
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 
208 bool SBInstruction::CanSetBreakpoint() {
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 
229 bool SBInstruction::GetDescription(lldb::SBStream &s) {
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 
257 void SBInstruction::Print(SBFile out) {
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 
284 bool SBInstruction::EmulateWithFrame(lldb::SBFrame &frame,
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) {
293       lldb_private::ExecutionContext exe_ctx;
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(),
300           &lldb_private::EmulateInstruction::ReadMemoryFrame,
301           &lldb_private::EmulateInstruction::WriteMemoryFrame,
302           &lldb_private::EmulateInstruction::ReadRegisterFrame,
303           &lldb_private::EmulateInstruction::WriteRegisterFrame);
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 
319 bool SBInstruction::TestEmulation(lldb::SBStream &output_stream,
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 }
332