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
16#include "lldb/API/SBStream.h"
17#include "lldb/API/SBTarget.h"
20#include "lldb/Core/Module.h"
21#include "lldb/Host/HostInfo.h"
25#include "lldb/Target/Target.h"
29
30#include <memory>
31
32// We recently fixed a leak in one of the Instruction subclasses where the
33// instruction will only hold a weak reference to the disassembler to avoid a
34// cycle that was keeping both objects alive (leak) and we need the
35// InstructionImpl class to make sure our public API behaves as users would
36// expect. Calls in our public API allow clients to do things like:
37//
38// 1 lldb::SBInstruction inst;
39// 2 inst = target.ReadInstructions(pc, 1).GetInstructionAtIndex(0)
40// 3 if (inst.DoesBranch())
41// 4 ...
42//
43// There was a temporary lldb::DisassemblerSP object created in the
44// SBInstructionList that was returned by lldb.target.ReadInstructions() that
45// will go away after line 2 but the "inst" object should be able to still
46// answer questions about itself. So we make sure that any SBInstruction
47// objects that are given out have a strong reference to the disassembler and
48// the instruction so that the object can live and successfully respond to all
49// queries.
51public:
53 const lldb::InstructionSP &inst_sp)
54 : m_disasm_sp(disasm_sp), m_inst_sp(inst_sp) {}
55
57
58 bool IsValid() const { return (bool)m_inst_sp; }
59
60protected:
61 lldb::DisassemblerSP m_disasm_sp; // Can be empty/invalid
63};
64
65using namespace lldb;
66using namespace lldb_private;
67
69
71 const lldb::InstructionSP &inst_sp)
72 : m_opaque_sp(new InstructionImpl(disasm_sp, inst_sp)) {}
73
78
80 LLDB_INSTRUMENT_VA(this, rhs);
81
82 if (this != &rhs)
84 return *this;
85}
86
88
91 return this->operator bool();
92}
93SBInstruction::operator bool() const {
95
96 return m_opaque_sp && m_opaque_sp->IsValid();
97}
98
100 LLDB_INSTRUMENT_VA(this);
101
102 SBAddress sb_addr;
104 if (inst_sp && inst_sp->GetAddress().IsValid())
105 sb_addr.SetAddress(inst_sp->GetAddress());
106 return sb_addr;
107}
108
110 LLDB_INSTRUMENT_VA(this, target);
111
113 if (!inst_sp)
114 return nullptr;
115
116 ExecutionContext exe_ctx;
117 TargetSP target_sp(target.GetSP());
118 std::unique_lock<std::recursive_mutex> lock;
119 if (target_sp) {
120 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
121
122 target_sp->CalculateExecutionContext(exe_ctx);
123 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
124 }
125 return ConstString(inst_sp->GetMnemonic(&exe_ctx)).GetCString();
126}
127
129 LLDB_INSTRUMENT_VA(this, target);
130
132 if (!inst_sp)
133 return nullptr;
134
135 ExecutionContext exe_ctx;
136 TargetSP target_sp(target.GetSP());
137 std::unique_lock<std::recursive_mutex> lock;
138 if (target_sp) {
139 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
140
141 target_sp->CalculateExecutionContext(exe_ctx);
142 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
143 }
144 return ConstString(inst_sp->GetOperands(&exe_ctx)).GetCString();
145}
146
148 LLDB_INSTRUMENT_VA(this, target);
149
151 if (!inst_sp)
152 return nullptr;
153
154 ExecutionContext exe_ctx;
155 TargetSP target_sp(target.GetSP());
156 std::unique_lock<std::recursive_mutex> lock;
157 if (target_sp) {
158 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
159
160 target_sp->CalculateExecutionContext(exe_ctx);
161 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
162 }
163 return ConstString(inst_sp->GetComment(&exe_ctx)).GetCString();
164}
165
167 LLDB_INSTRUMENT_VA(this, target);
168
170 if (inst_sp) {
171 ExecutionContext exe_ctx;
172 TargetSP target_sp(target.GetSP());
173 std::unique_lock<std::recursive_mutex> lock;
174 if (target_sp) {
175 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
176
177 target_sp->CalculateExecutionContext(exe_ctx);
178 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
179 }
180 return inst_sp->GetControlFlowKind(&exe_ctx);
181 }
183}
184
186 LLDB_INSTRUMENT_VA(this);
187
189 if (inst_sp)
190 return inst_sp->GetOpcode().GetByteSize();
191 return 0;
192}
193
195 LLDB_INSTRUMENT_VA(this, target);
196
197 lldb::SBData sb_data;
199 if (inst_sp) {
200 DataExtractorSP data_extractor_sp(new DataExtractor());
201 if (inst_sp->GetData(*data_extractor_sp)) {
202 sb_data.SetOpaque(data_extractor_sp);
203 }
204 }
205 return sb_data;
206}
207
209 LLDB_INSTRUMENT_VA(this);
210
212 if (inst_sp)
213 return inst_sp->DoesBranch();
214 return false;
215}
216
218 LLDB_INSTRUMENT_VA(this);
219
221 if (inst_sp)
222 return inst_sp->HasDelaySlot();
223 return false;
224}
225
227 LLDB_INSTRUMENT_VA(this);
228
230 if (inst_sp)
231 return inst_sp->CanSetBreakpoint();
232 return false;
233}
234
236 if (m_opaque_sp)
237 return m_opaque_sp->GetSP();
238 else
239 return lldb::InstructionSP();
240}
241
243 const lldb::InstructionSP &inst_sp) {
244 m_opaque_sp = std::make_shared<InstructionImpl>(disasm_sp, inst_sp);
245}
246
248 LLDB_INSTRUMENT_VA(this, s);
249
251 if (inst_sp) {
252 SymbolContext sc;
253 const Address &addr = inst_sp->GetAddress();
254 ModuleSP module_sp(addr.GetModule());
255 if (module_sp)
256 module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything,
257 sc);
258 // Use the "ref()" instead of the "get()" accessor in case the SBStream
259 // didn't have a stream already created, one will get created...
260 FormatEntity::Entry format;
261 FormatEntity::Parse("${addr}: ", format);
262 inst_sp->Dump(&s.ref(), 0, true, false, /*show_control_flow_kind=*/false,
263 nullptr, &sc, nullptr, &format, 0);
264 return true;
265 }
266 return false;
267}
268
269void SBInstruction::Print(FILE *outp) {
270 LLDB_INSTRUMENT_VA(this, outp);
271 FileSP out = std::make_shared<NativeFile>(outp, /*take_ownership=*/false);
272 Print(out);
273}
274
276 LLDB_INSTRUMENT_VA(this, out);
277 Print(out.m_opaque_sp);
278}
279
281 LLDB_INSTRUMENT_VA(this, out_sp);
282
283 if (!out_sp || !out_sp->IsValid())
284 return;
285
287 if (inst_sp) {
288 SymbolContext sc;
289 const Address &addr = inst_sp->GetAddress();
290 ModuleSP module_sp(addr.GetModule());
291 if (module_sp)
292 module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything,
293 sc);
294 StreamFile out_stream(out_sp);
295 FormatEntity::Entry format;
296 FormatEntity::Parse("${addr}: ", format);
297 inst_sp->Dump(&out_stream, 0, true, false, /*show_control_flow_kind=*/false,
298 nullptr, &sc, nullptr, &format, 0);
299 }
300}
301
303 uint32_t evaluate_options) {
304 LLDB_INSTRUMENT_VA(this, frame, evaluate_options);
305
307 if (inst_sp) {
308 lldb::StackFrameSP frame_sp(frame.GetFrameSP());
309
310 if (frame_sp) {
312 frame_sp->CalculateExecutionContext(exe_ctx);
313 lldb_private::Target *target = exe_ctx.GetTargetPtr();
315
316 return inst_sp->Emulate(
317 arch, evaluate_options, (void *)frame_sp.get(),
322 }
323 }
324 return false;
325}
326
327bool SBInstruction::DumpEmulation(const char *triple) {
328 LLDB_INSTRUMENT_VA(this, triple);
329
331 if (inst_sp && triple) {
332 return inst_sp->DumpEmulation(HostInfo::GetAugmentedArchSpec(triple));
333 }
334 return false;
335}
336
338 const char *test_file) {
339 LLDB_INSTRUMENT_VA(this, output_stream, test_file);
340
341 if (!m_opaque_sp)
344
346 if (inst_sp)
347 return inst_sp->TestEmulation(output_stream.ref(), test_file);
348 return false;
349}
#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:86
void SetOpaque(const lldb::DisassemblerSP &disasm_sp, const lldb::InstructionSP &inst_sp)
std::shared_ptr< InstructionImpl > m_opaque_sp
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:578
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:273
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.
An data extractor class.
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.
const ArchSpec & GetArchitecture() const
Definition Target.h:1056
Status Parse(const llvm::StringRef &format, Entry &entry)
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::Instruction > InstructionSP
std::shared_ptr< lldb_private::Disassembler > DisassemblerSP
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
std::shared_ptr< lldb_private::File > FileSP
std::shared_ptr< lldb_private::DataExtractor > DataExtractorSP
std::shared_ptr< lldb_private::Module > ModuleSP