1 //===-- SBInstruction.cpp -------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/API/SBInstruction.h"
10 #include "lldb/Utility/Instrumentation.h"
12 #include "lldb/API/SBAddress.h"
13 #include "lldb/API/SBFrame.h"
14 #include "lldb/API/SBFile.h"
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/Host/HostInfo.h"
23 #include "lldb/Host/StreamFile.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"
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:
39 // 1 lldb::SBInstruction inst;
40 // 2 inst = target.ReadInstructions(pc, 1).GetInstructionAtIndex(0)
41 // 3 if (inst.DoesBranch())
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
51 class InstructionImpl
{
53 InstructionImpl(const lldb::DisassemblerSP
&disasm_sp
,
54 const lldb::InstructionSP
&inst_sp
)
55 : m_disasm_sp(disasm_sp
), m_inst_sp(inst_sp
) {}
57 lldb::InstructionSP
GetSP() const { return m_inst_sp
; }
59 bool IsValid() const { return (bool)m_inst_sp
; }
62 lldb::DisassemblerSP m_disasm_sp
; // Can be empty/invalid
63 lldb::InstructionSP m_inst_sp
;
67 using namespace lldb_private
;
69 SBInstruction::SBInstruction() { LLDB_INSTRUMENT_VA(this); }
71 SBInstruction::SBInstruction(const lldb::DisassemblerSP
&disasm_sp
,
72 const lldb::InstructionSP
&inst_sp
)
73 : m_opaque_sp(new InstructionImpl(disasm_sp
, inst_sp
)) {}
75 SBInstruction::SBInstruction(const SBInstruction
&rhs
)
76 : m_opaque_sp(rhs
.m_opaque_sp
) {
77 LLDB_INSTRUMENT_VA(this, rhs
);
80 const SBInstruction
&SBInstruction::operator=(const SBInstruction
&rhs
) {
81 LLDB_INSTRUMENT_VA(this, rhs
);
84 m_opaque_sp
= rhs
.m_opaque_sp
;
88 SBInstruction::~SBInstruction() = default;
90 bool SBInstruction::IsValid() {
91 LLDB_INSTRUMENT_VA(this);
92 return this->operator bool();
94 SBInstruction::operator bool() const {
95 LLDB_INSTRUMENT_VA(this);
97 return m_opaque_sp
&& m_opaque_sp
->IsValid();
100 SBAddress
SBInstruction::GetAddress() {
101 LLDB_INSTRUMENT_VA(this);
104 lldb::InstructionSP
inst_sp(GetOpaque());
105 if (inst_sp
&& inst_sp
->GetAddress().IsValid())
106 sb_addr
.SetAddress(inst_sp
->GetAddress());
110 const char *SBInstruction::GetMnemonic(SBTarget target
) {
111 LLDB_INSTRUMENT_VA(this, target
);
113 lldb::InstructionSP
inst_sp(GetOpaque());
117 ExecutionContext exe_ctx
;
118 TargetSP
target_sp(target
.GetSP());
119 std::unique_lock
<std::recursive_mutex
> lock
;
121 lock
= std::unique_lock
<std::recursive_mutex
>(target_sp
->GetAPIMutex());
123 target_sp
->CalculateExecutionContext(exe_ctx
);
124 exe_ctx
.SetProcessSP(target_sp
->GetProcessSP());
126 return ConstString(inst_sp
->GetMnemonic(&exe_ctx
)).GetCString();
129 const char *SBInstruction::GetOperands(SBTarget target
) {
130 LLDB_INSTRUMENT_VA(this, target
);
132 lldb::InstructionSP
inst_sp(GetOpaque());
136 ExecutionContext exe_ctx
;
137 TargetSP
target_sp(target
.GetSP());
138 std::unique_lock
<std::recursive_mutex
> lock
;
140 lock
= std::unique_lock
<std::recursive_mutex
>(target_sp
->GetAPIMutex());
142 target_sp
->CalculateExecutionContext(exe_ctx
);
143 exe_ctx
.SetProcessSP(target_sp
->GetProcessSP());
145 return ConstString(inst_sp
->GetOperands(&exe_ctx
)).GetCString();
148 const char *SBInstruction::GetComment(SBTarget target
) {
149 LLDB_INSTRUMENT_VA(this, target
);
151 lldb::InstructionSP
inst_sp(GetOpaque());
155 ExecutionContext exe_ctx
;
156 TargetSP
target_sp(target
.GetSP());
157 std::unique_lock
<std::recursive_mutex
> lock
;
159 lock
= std::unique_lock
<std::recursive_mutex
>(target_sp
->GetAPIMutex());
161 target_sp
->CalculateExecutionContext(exe_ctx
);
162 exe_ctx
.SetProcessSP(target_sp
->GetProcessSP());
164 return ConstString(inst_sp
->GetComment(&exe_ctx
)).GetCString();
167 lldb::InstructionControlFlowKind
SBInstruction::GetControlFlowKind(lldb::SBTarget target
) {
168 LLDB_INSTRUMENT_VA(this, target
);
170 lldb::InstructionSP
inst_sp(GetOpaque());
172 ExecutionContext exe_ctx
;
173 TargetSP
target_sp(target
.GetSP());
174 std::unique_lock
<std::recursive_mutex
> lock
;
176 lock
= std::unique_lock
<std::recursive_mutex
>(target_sp
->GetAPIMutex());
178 target_sp
->CalculateExecutionContext(exe_ctx
);
179 exe_ctx
.SetProcessSP(target_sp
->GetProcessSP());
181 return inst_sp
->GetControlFlowKind(&exe_ctx
);
183 return lldb::eInstructionControlFlowKindUnknown
;
186 size_t SBInstruction::GetByteSize() {
187 LLDB_INSTRUMENT_VA(this);
189 lldb::InstructionSP
inst_sp(GetOpaque());
191 return inst_sp
->GetOpcode().GetByteSize();
195 SBData
SBInstruction::GetData(SBTarget target
) {
196 LLDB_INSTRUMENT_VA(this, target
);
198 lldb::SBData sb_data
;
199 lldb::InstructionSP
inst_sp(GetOpaque());
201 DataExtractorSP
data_extractor_sp(new DataExtractor());
202 if (inst_sp
->GetData(*data_extractor_sp
)) {
203 sb_data
.SetOpaque(data_extractor_sp
);
209 bool SBInstruction::DoesBranch() {
210 LLDB_INSTRUMENT_VA(this);
212 lldb::InstructionSP
inst_sp(GetOpaque());
214 return inst_sp
->DoesBranch();
218 bool SBInstruction::HasDelaySlot() {
219 LLDB_INSTRUMENT_VA(this);
221 lldb::InstructionSP
inst_sp(GetOpaque());
223 return inst_sp
->HasDelaySlot();
227 bool SBInstruction::CanSetBreakpoint() {
228 LLDB_INSTRUMENT_VA(this);
230 lldb::InstructionSP
inst_sp(GetOpaque());
232 return inst_sp
->CanSetBreakpoint();
236 lldb::InstructionSP
SBInstruction::GetOpaque() {
238 return m_opaque_sp
->GetSP();
240 return lldb::InstructionSP();
243 void SBInstruction::SetOpaque(const lldb::DisassemblerSP
&disasm_sp
,
244 const lldb::InstructionSP
&inst_sp
) {
245 m_opaque_sp
= std::make_shared
<InstructionImpl
>(disasm_sp
, inst_sp
);
248 bool SBInstruction::GetDescription(lldb::SBStream
&s
) {
249 LLDB_INSTRUMENT_VA(this, s
);
251 lldb::InstructionSP
inst_sp(GetOpaque());
254 const Address
&addr
= inst_sp
->GetAddress();
255 ModuleSP
module_sp(addr
.GetModule());
257 module_sp
->ResolveSymbolContextForAddress(addr
, eSymbolContextEverything
,
259 // Use the "ref()" instead of the "get()" accessor in case the SBStream
260 // didn't have a stream already created, one will get created...
261 FormatEntity::Entry format
;
262 FormatEntity::Parse("${addr}: ", format
);
263 inst_sp
->Dump(&s
.ref(), 0, true, false, /*show_control_flow_kind=*/false,
264 nullptr, &sc
, nullptr, &format
, 0);
270 void SBInstruction::Print(FILE *outp
) {
271 LLDB_INSTRUMENT_VA(this, outp
);
272 FileSP out
= std::make_shared
<NativeFile
>(outp
, /*take_ownership=*/false);
276 void SBInstruction::Print(SBFile out
) {
277 LLDB_INSTRUMENT_VA(this, out
);
278 Print(out
.m_opaque_sp
);
281 void SBInstruction::Print(FileSP out_sp
) {
282 LLDB_INSTRUMENT_VA(this, out_sp
);
284 if (!out_sp
|| !out_sp
->IsValid())
287 lldb::InstructionSP
inst_sp(GetOpaque());
290 const Address
&addr
= inst_sp
->GetAddress();
291 ModuleSP
module_sp(addr
.GetModule());
293 module_sp
->ResolveSymbolContextForAddress(addr
, eSymbolContextEverything
,
295 StreamFile
out_stream(out_sp
);
296 FormatEntity::Entry format
;
297 FormatEntity::Parse("${addr}: ", format
);
298 inst_sp
->Dump(&out_stream
, 0, true, false, /*show_control_flow_kind=*/false,
299 nullptr, &sc
, nullptr, &format
, 0);
303 bool SBInstruction::EmulateWithFrame(lldb::SBFrame
&frame
,
304 uint32_t evaluate_options
) {
305 LLDB_INSTRUMENT_VA(this, frame
, evaluate_options
);
307 lldb::InstructionSP
inst_sp(GetOpaque());
309 lldb::StackFrameSP
frame_sp(frame
.GetFrameSP());
312 lldb_private::ExecutionContext exe_ctx
;
313 frame_sp
->CalculateExecutionContext(exe_ctx
);
314 lldb_private::Target
*target
= exe_ctx
.GetTargetPtr();
315 lldb_private::ArchSpec arch
= target
->GetArchitecture();
317 return inst_sp
->Emulate(
318 arch
, evaluate_options
, (void *)frame_sp
.get(),
319 &lldb_private::EmulateInstruction::ReadMemoryFrame
,
320 &lldb_private::EmulateInstruction::WriteMemoryFrame
,
321 &lldb_private::EmulateInstruction::ReadRegisterFrame
,
322 &lldb_private::EmulateInstruction::WriteRegisterFrame
);
328 bool SBInstruction::DumpEmulation(const char *triple
) {
329 LLDB_INSTRUMENT_VA(this, triple
);
331 lldb::InstructionSP
inst_sp(GetOpaque());
332 if (inst_sp
&& triple
) {
333 return inst_sp
->DumpEmulation(HostInfo::GetAugmentedArchSpec(triple
));
338 bool SBInstruction::TestEmulation(lldb::SBStream
&output_stream
,
339 const char *test_file
) {
340 LLDB_INSTRUMENT_VA(this, output_stream
, test_file
);
343 SetOpaque(lldb::DisassemblerSP(),
344 lldb::InstructionSP(new PseudoInstruction()));
346 lldb::InstructionSP
inst_sp(GetOpaque());
348 return inst_sp
->TestEmulation(output_stream
.ref(), test_file
);