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"
22#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:
54 const lldb::InstructionSP &inst_sp)
55 : m_disasm_sp(disasm_sp), m_inst_sp(inst_sp) {}
56
58
59 bool IsValid() const { return (bool)m_inst_sp; }
60
61protected:
62 lldb::DisassemblerSP m_disasm_sp; // Can be empty/invalid
64};
65
66using namespace lldb;
67using namespace lldb_private;
68
70
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;
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
114 if (!inst_sp)
115 return nullptr;
116
117 ExecutionContext exe_ctx;
118 TargetSP target_sp(target.GetSP());
119 std::unique_lock<std::recursive_mutex> lock;
120 if (target_sp) {
121 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
122
123 target_sp->CalculateExecutionContext(exe_ctx);
124 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
125 }
126 return ConstString(inst_sp->GetMnemonic(&exe_ctx)).GetCString();
127}
128
130 LLDB_INSTRUMENT_VA(this, target);
131
133 if (!inst_sp)
134 return nullptr;
135
136 ExecutionContext exe_ctx;
137 TargetSP target_sp(target.GetSP());
138 std::unique_lock<std::recursive_mutex> lock;
139 if (target_sp) {
140 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
141
142 target_sp->CalculateExecutionContext(exe_ctx);
143 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
144 }
145 return ConstString(inst_sp->GetOperands(&exe_ctx)).GetCString();
146}
147
149 LLDB_INSTRUMENT_VA(this, target);
150
152 if (!inst_sp)
153 return nullptr;
154
155 ExecutionContext exe_ctx;
156 TargetSP target_sp(target.GetSP());
157 std::unique_lock<std::recursive_mutex> lock;
158 if (target_sp) {
159 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
160
161 target_sp->CalculateExecutionContext(exe_ctx);
162 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
163 }
164 return ConstString(inst_sp->GetComment(&exe_ctx)).GetCString();
165}
166
168 LLDB_INSTRUMENT_VA(this, target);
169
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
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;
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
213 if (inst_sp)
214 return inst_sp->DoesBranch();
215 return false;
216}
217
219 LLDB_INSTRUMENT_VA(this);
220
222 if (inst_sp)
223 return inst_sp->HasDelaySlot();
224 return false;
225}
226
228 LLDB_INSTRUMENT_VA(this);
229
231 if (inst_sp)
232 return inst_sp->CanSetBreakpoint();
233 return false;
234}
235
237 if (m_opaque_sp)
238 return m_opaque_sp->GetSP();
239 else
240 return lldb::InstructionSP();
241}
242
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
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
282 LLDB_INSTRUMENT_VA(this, out_sp);
283
284 if (!out_sp || !out_sp->IsValid())
285 return;
286
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
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
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)
345
347 if (inst_sp)
348 return inst_sp->TestEmulation(output_stream.ref(), 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:51
lldb::StackFrameSP GetFrameSP() const
Definition: SBFrame.cpp:85
void SetOpaque(const lldb::DisassemblerSP &disasm_sp, const lldb::InstructionSP &inst_sp)
std::shared_ptr< InstructionImpl > m_opaque_sp
Definition: SBInstruction.h:88
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:177
lldb::TargetSP GetSP() const
Definition: SBTarget.cpp:585
A section + offset based address class.
Definition: Address.h:62
lldb::ModuleSP GetModule() const
Get accessor for the module for this address.
Definition: Address.cpp:285
An architecture specification class.
Definition: ArchSpec.h:31
A uniqued constant string class.
Definition: ConstString.h:40
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:214
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.
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:34
const ArchSpec & GetArchitecture() const
Definition: Target.h:1014
Status Parse(const llvm::StringRef &format, Entry &entry)
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
Definition: lldb-forward.h:412
std::shared_ptr< lldb_private::Instruction > InstructionSP
Definition: lldb-forward.h:350
std::shared_ptr< lldb_private::Disassembler > DisassemblerSP
Definition: lldb-forward.h:333
InstructionControlFlowKind
Architecture-agnostic categorization of instructions for traversing the control flow of a trace.
@ eInstructionControlFlowKindUnknown
The instruction could not be classified.
std::shared_ptr< lldb_private::Target > TargetSP
Definition: lldb-forward.h:436
std::shared_ptr< lldb_private::File > FileSP
Definition: lldb-forward.h:345
std::shared_ptr< lldb_private::DataExtractor > DataExtractorSP
Definition: lldb-forward.h:330
std::shared_ptr< lldb_private::Module > ModuleSP
Definition: lldb-forward.h:365