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
17#include "lldb/API/SBStream.h"
18#include "lldb/API/SBTarget.h"
21#include "lldb/Core/Module.h"
23#include "lldb/Host/HostInfo.h"
26#include "lldb/Target/Target.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.
52public:
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
61protected:
62 lldb::DisassemblerSP m_disasm_sp; // Can be empty/invalid
63 lldb::InstructionSP m_inst_sp;
64};
65
66using namespace lldb;
67using namespace lldb_private;
68
70
71SBInstruction::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
92 return this->operator bool();
93}
94SBInstruction::operator bool() const {
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
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, target);
169
170 lldb::InstructionSP inst_sp(GetOpaque());
171 if (inst_sp) {
172 ExecutionContext exe_ctx;
173 TargetSP target_sp(target.GetSP());
174 std::unique_lock<std::recursive_mutex> lock;
175 if (target_sp) {
176 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
177
178 target_sp->CalculateExecutionContext(exe_ctx);
179 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
180 }
181 return inst_sp->GetControlFlowKind(&exe_ctx);
182 }
184}
185
187 LLDB_INSTRUMENT_VA(this);
188
189 lldb::InstructionSP inst_sp(GetOpaque());
190 if (inst_sp)
191 return inst_sp->GetOpcode().GetByteSize();
192 return 0;
193}
194
196 LLDB_INSTRUMENT_VA(this, target);
197
198 lldb::SBData sb_data;
199 lldb::InstructionSP inst_sp(GetOpaque());
200 if (inst_sp) {
201 DataExtractorSP data_extractor_sp(new DataExtractor());
202 if (inst_sp->GetData(*data_extractor_sp)) {
203 sb_data.SetOpaque(data_extractor_sp);
204 }
205 }
206 return sb_data;
207}
208
210 LLDB_INSTRUMENT_VA(this);
211
212 lldb::InstructionSP inst_sp(GetOpaque());
213 if (inst_sp)
214 return inst_sp->DoesBranch();
215 return false;
216}
217
219 LLDB_INSTRUMENT_VA(this);
220
221 lldb::InstructionSP inst_sp(GetOpaque());
222 if (inst_sp)
223 return inst_sp->HasDelaySlot();
224 return false;
225}
226
228 LLDB_INSTRUMENT_VA(this);
229
230 lldb::InstructionSP inst_sp(GetOpaque());
231 if (inst_sp)
232 return inst_sp->CanSetBreakpoint();
233 return false;
234}
235
236lldb::InstructionSP SBInstruction::GetOpaque() {
237 if (m_opaque_sp)
238 return m_opaque_sp->GetSP();
239 else
240 return lldb::InstructionSP();
241}
242
243void 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);
246}
247
249 LLDB_INSTRUMENT_VA(this, s);
250
251 lldb::InstructionSP inst_sp(GetOpaque());
252 if (inst_sp) {
253 SymbolContext sc;
254 const Address &addr = inst_sp->GetAddress();
255 ModuleSP module_sp(addr.GetModule());
256 if (module_sp)
257 module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything,
258 sc);
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);
265 return true;
266 }
267 return false;
268}
269
270void SBInstruction::Print(FILE *outp) {
271 LLDB_INSTRUMENT_VA(this, outp);
272 FileSP out = std::make_shared<NativeFile>(outp, /*take_ownership=*/false);
273 Print(out);
274}
275
277 LLDB_INSTRUMENT_VA(this, out);
278 Print(out.m_opaque_sp);
279}
280
281void SBInstruction::Print(FileSP out_sp) {
282 LLDB_INSTRUMENT_VA(this, out_sp);
283
284 if (!out_sp || !out_sp->IsValid())
285 return;
286
287 lldb::InstructionSP inst_sp(GetOpaque());
288 if (inst_sp) {
289 SymbolContext sc;
290 const Address &addr = inst_sp->GetAddress();
291 ModuleSP module_sp(addr.GetModule());
292 if (module_sp)
293 module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything,
294 sc);
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);
300 }
301}
302
304 uint32_t evaluate_options) {
305 LLDB_INSTRUMENT_VA(this, frame, evaluate_options);
306
307 lldb::InstructionSP inst_sp(GetOpaque());
308 if (inst_sp) {
309 lldb::StackFrameSP frame_sp(frame.GetFrameSP());
310
311 if (frame_sp) {
313 frame_sp->CalculateExecutionContext(exe_ctx);
314 lldb_private::Target *target = exe_ctx.GetTargetPtr();
316
317 return inst_sp->Emulate(
318 arch, evaluate_options, (void *)frame_sp.get(),
323 }
324 }
325 return false;
326}
327
328bool SBInstruction::DumpEmulation(const char *triple) {
329 LLDB_INSTRUMENT_VA(this, triple);
330
331 lldb::InstructionSP inst_sp(GetOpaque());
332 if (inst_sp && triple) {
333 return inst_sp->DumpEmulation(HostInfo::GetAugmentedArchSpec(triple));
334 }
335 return false;
336}
337
339 const char *test_file) {
340 LLDB_INSTRUMENT_VA(this, output_stream, test_file);
341
342 if (!m_opaque_sp)
343 SetOpaque(lldb::DisassemblerSP(),
344 lldb::InstructionSP(new PseudoInstruction()));
345
346 lldb::InstructionSP inst_sp(GetOpaque());
347 if (inst_sp)
348 return inst_sp->TestEmulation(output_stream.get(), test_file);
349 return false;
350}
#define LLDB_INSTRUMENT_VA(...)
lldb::InstructionSP GetSP() const
bool IsValid() const
InstructionImpl(const lldb::DisassemblerSP &disasm_sp, const lldb::InstructionSP &inst_sp)
lldb::InstructionSP m_inst_sp
lldb::DisassemblerSP m_disasm_sp
void SetAddress(lldb::SBSection section, lldb::addr_t offset)
Definition: SBAddress.cpp:88
void SetOpaque(const lldb::DataExtractorSP &data_sp)
Definition: SBData.cpp:45
FileSP m_opaque_sp
Definition: SBFile.h:45
lldb::StackFrameSP GetFrameSP() const
Definition: SBFrame.cpp:83
void SetOpaque(const lldb::DisassemblerSP &disasm_sp, const lldb::InstructionSP &inst_sp)
std::shared_ptr< InstructionImpl > m_opaque_sp
Definition: SBInstruction.h:86
const char * GetOperands(lldb::SBTarget target)
lldb::InstructionSP GetOpaque()
const char * GetComment(lldb::SBTarget target)
bool EmulateWithFrame(lldb::SBFrame &frame, uint32_t evaluate_options)
bool DumpEmulation(const char *triple)
lldb::InstructionControlFlowKind GetControlFlowKind(lldb::SBTarget target)
const SBInstruction & operator=(const SBInstruction &rhs)
bool TestEmulation(lldb::SBStream &output_stream, const char *test_file)
void Print(FILE *out)
lldb::SBData GetData(lldb::SBTarget target)
bool GetDescription(lldb::SBStream &description)
const char * GetMnemonic(lldb::SBTarget target)
lldb_private::Stream & ref()
Definition: SBStream.cpp:176
lldb_private::Stream * get()
Definition: SBStream.cpp:174
lldb::TargetSP GetSP() const
Definition: SBTarget.cpp:576
A section + offset based address class.
Definition: Address.h:59
lldb::ModuleSP GetModule() const
Get accessor for the module for this address.
Definition: Address.cpp:283
An architecture specification class.
Definition: ArchSpec.h:32
An data extractor class.
Definition: DataExtractor.h:48
static size_t WriteMemoryFrame(EmulateInstruction *instruction, void *baton, const Context &context, lldb::addr_t addr, const void *dst, size_t length)
static size_t ReadMemoryFrame(EmulateInstruction *instruction, void *baton, const Context &context, lldb::addr_t addr, void *dst, size_t length)
static bool WriteRegisterFrame(EmulateInstruction *instruction, void *baton, const Context &context, const RegisterInfo *reg_info, const RegisterValue &reg_value)
static bool ReadRegisterFrame(EmulateInstruction *instruction, void *baton, const RegisterInfo *reg_info, RegisterValue &reg_value)
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
void SetProcessSP(const lldb::ProcessSP &process_sp)
Set accessor to set only the process shared pointer.
Target * GetTargetPtr() const
Returns a pointer to the target object.
static Status Parse(const llvm::StringRef &format, Entry &entry)
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:33
const ArchSpec & GetArchitecture() const
Definition: Target.h:988
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
InstructionControlFlowKind
Architecture-agnostic categorization of instructions for traversing the control flow of a trace.
@ eInstructionControlFlowKindUnknown
The instruction could not be classified.