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/SBFile.h"
14#include "lldb/API/SBFrame.h"
15
16#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"
31
32#include <memory>
33
34// We recently fixed a leak in one of the Instruction subclasses where the
35// instruction will only hold a weak reference to the disassembler to avoid a
36// cycle that was keeping both objects alive (leak) and we need the
37// InstructionImpl class to make sure our public API behaves as users would
38// expect. Calls in our public API allow clients to do things like:
39//
40// 1 lldb::SBInstruction inst;
41// 2 inst = target.ReadInstructions(pc, 1).GetInstructionAtIndex(0)
42// 3 if (inst.DoesBranch())
43// 4 ...
44//
45// There was a temporary lldb::DisassemblerSP object created in the
46// SBInstructionList that was returned by lldb.target.ReadInstructions() that
47// will go away after line 2 but the "inst" object should be able to still
48// answer questions about itself. So we make sure that any SBInstruction
49// objects that are given out have a strong reference to the disassembler and
50// the instruction so that the object can live and successfully respond to all
51// queries.
53public:
55 const lldb::InstructionSP &inst_sp)
56 : m_disasm_sp(disasm_sp), m_inst_sp(inst_sp) {}
57
59
60 bool IsValid() const { return (bool)m_inst_sp; }
61
62protected:
63 lldb::DisassemblerSP m_disasm_sp; // Can be empty/invalid
65};
66
67using namespace lldb;
68using namespace lldb_private;
69
71
73 const lldb::InstructionSP &inst_sp)
74 : m_opaque_sp(new InstructionImpl(disasm_sp, inst_sp)) {}
75
80
82 LLDB_INSTRUMENT_VA(this, rhs);
83
84 if (this != &rhs)
86 return *this;
87}
88
90
93 return this->operator bool();
94}
95SBInstruction::operator bool() const {
97
98 return m_opaque_sp && m_opaque_sp->IsValid();
99}
100
102 LLDB_INSTRUMENT_VA(this);
103
104 SBAddress sb_addr;
106 if (inst_sp && inst_sp->GetAddress().IsValid())
107 sb_addr.SetAddress(inst_sp->GetAddress());
108 return sb_addr;
109}
110
112 LLDB_INSTRUMENT_VA(this, target);
113
115 if (!inst_sp)
116 return nullptr;
117
118 ExecutionContext exe_ctx;
119 TargetSP target_sp(target.GetSP());
120 TargetAPIMutex api_lock;
121 std::unique_lock<TargetAPIMutex> guard;
122 if (target_sp) {
123 api_lock = TargetAPIMutex(target_sp->GetAPIMutex());
124 guard = std::unique_lock<TargetAPIMutex>(api_lock);
125
126 target_sp->CalculateExecutionContext(exe_ctx);
127 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
128 }
129 return ConstString(inst_sp->GetMnemonic(&exe_ctx)).GetCString();
130}
131
133 LLDB_INSTRUMENT_VA(this, target);
134
136 if (!inst_sp)
137 return nullptr;
138
139 ExecutionContext exe_ctx;
140 TargetSP target_sp(target.GetSP());
141 TargetAPIMutex api_lock;
142 std::unique_lock<TargetAPIMutex> guard;
143 if (target_sp) {
144 api_lock = TargetAPIMutex(target_sp->GetAPIMutex());
145 guard = std::unique_lock<TargetAPIMutex>(api_lock);
146
147 target_sp->CalculateExecutionContext(exe_ctx);
148 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
149 }
150 return ConstString(inst_sp->GetOperands(&exe_ctx)).GetCString();
151}
152
154 LLDB_INSTRUMENT_VA(this, target);
155
157 if (!inst_sp)
158 return nullptr;
159
160 ExecutionContext exe_ctx;
161 TargetSP target_sp(target.GetSP());
162 TargetAPIMutex api_lock;
163 std::unique_lock<TargetAPIMutex> guard;
164 if (target_sp) {
165 api_lock = TargetAPIMutex(target_sp->GetAPIMutex());
166 guard = std::unique_lock<TargetAPIMutex>(api_lock);
167
168 target_sp->CalculateExecutionContext(exe_ctx);
169 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
170 }
171 return ConstString(inst_sp->GetComment(&exe_ctx)).GetCString();
172}
173
176 LLDB_INSTRUMENT_VA(this, target);
177
179 if (inst_sp) {
180 ExecutionContext exe_ctx;
181 TargetSP target_sp(target.GetSP());
182 TargetAPIMutex api_lock;
183 std::unique_lock<TargetAPIMutex> guard;
184 if (target_sp) {
185 api_lock = TargetAPIMutex(target_sp->GetAPIMutex());
186 guard = std::unique_lock<TargetAPIMutex>(api_lock);
187
188 target_sp->CalculateExecutionContext(exe_ctx);
189 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
190 }
191 return inst_sp->GetControlFlowKind(&exe_ctx);
192 }
194}
195
197 LLDB_INSTRUMENT_VA(this);
198
200 if (inst_sp)
201 return inst_sp->GetOpcode().GetByteSize();
202 return 0;
203}
204
206 LLDB_INSTRUMENT_VA(this, target);
207
208 lldb::SBData sb_data;
210 if (inst_sp) {
211 DataExtractorSP data_extractor_sp(new DataExtractor());
212 if (inst_sp->GetData(*data_extractor_sp)) {
213 sb_data.SetOpaque(data_extractor_sp);
214 }
215 }
216 return sb_data;
217}
218
220 LLDB_INSTRUMENT_VA(this);
221
223 if (inst_sp)
224 return inst_sp->DoesBranch();
225 return false;
226}
227
229 LLDB_INSTRUMENT_VA(this);
230
232 if (inst_sp)
233 return inst_sp->HasDelaySlot();
234 return false;
235}
236
238 LLDB_INSTRUMENT_VA(this);
239
241 if (inst_sp)
242 return inst_sp->CanSetBreakpoint();
243 return false;
244}
245
247 if (m_opaque_sp)
248 return m_opaque_sp->GetSP();
249 else
250 return lldb::InstructionSP();
251}
252
254 const lldb::InstructionSP &inst_sp) {
255 m_opaque_sp = std::make_shared<InstructionImpl>(disasm_sp, inst_sp);
256}
257
259 LLDB_INSTRUMENT_VA(this, s);
260
262 if (inst_sp) {
263 SymbolContext sc;
264 const Address &addr = inst_sp->GetAddress();
265 ModuleSP module_sp(addr.GetModule());
266 if (module_sp)
267 module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything,
268 sc);
269 // Use the "ref()" instead of the "get()" accessor in case the SBStream
270 // didn't have a stream already created, one will get created...
271 FormatEntity::Entry format;
272 FormatEntity::Parse("${addr}: ", format);
273 inst_sp->Dump(&s.ref(), 0, true, false, /*show_control_flow_kind=*/false,
274 nullptr, &sc, nullptr, &format, 0);
275 return true;
276 }
277 return false;
278}
279
280void SBInstruction::Print(FILE *outp) {
281 LLDB_INSTRUMENT_VA(this, outp);
282 FileSP out = std::make_shared<NativeFile>(outp, File::eOpenOptionWriteOnly,
283 /*take_ownership=*/false);
284 Print(out);
285}
286
288 LLDB_INSTRUMENT_VA(this, out);
289 Print(out.m_opaque_sp);
290}
291
293 LLDB_INSTRUMENT_VA(this, out_sp);
294
295 if (!out_sp || !out_sp->IsValid())
296 return;
297
299 if (inst_sp) {
300 SymbolContext sc;
301 const Address &addr = inst_sp->GetAddress();
302 ModuleSP module_sp(addr.GetModule());
303 if (module_sp)
304 module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything,
305 sc);
306 StreamFile out_stream(out_sp);
307 FormatEntity::Entry format;
308 FormatEntity::Parse("${addr}: ", format);
309 inst_sp->Dump(&out_stream, 0, true, false, /*show_control_flow_kind=*/false,
310 nullptr, &sc, nullptr, &format, 0);
311 }
312}
313
315 uint32_t evaluate_options) {
316 LLDB_INSTRUMENT_VA(this, frame, evaluate_options);
317
319 if (inst_sp) {
320 lldb::StackFrameSP frame_sp(frame.GetFrameSP());
321
322 if (frame_sp) {
324 frame_sp->CalculateExecutionContext(exe_ctx);
325 lldb_private::Target *target = exe_ctx.GetTargetPtr();
327
328 return inst_sp->Emulate(
329 arch, evaluate_options, (void *)frame_sp.get(),
334 }
335 }
336 return false;
337}
338
339bool SBInstruction::DumpEmulation(const char *triple) {
340 LLDB_INSTRUMENT_VA(this, triple);
341
343 if (inst_sp && triple) {
344 return inst_sp->DumpEmulation(HostInfo::GetAugmentedArchSpec(triple));
345 }
346 return false;
347}
348
350 const char *test_file) {
351 LLDB_INSTRUMENT_VA(this, output_stream, test_file);
352
353 if (!m_opaque_sp)
356
358 if (inst_sp)
359 return inst_sp->TestEmulation(output_stream.ref(), test_file);
360 return false;
361}
362
364 LLDB_INSTRUMENT_VA(this);
365
366 SBStructuredData result;
367
368 if (!m_opaque_sp || !m_opaque_sp->IsValid())
369 return result;
370
371 lldb::InstructionSP inst_sp = m_opaque_sp->GetSP();
372 if (!inst_sp)
373 return result;
374
375 StructuredData::ArraySP array_sp = inst_sp->GetVariableAnnotations();
376 result.m_impl_up->SetObjectSP(array_sp);
377
378 return result;
379}
#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:46
FileSP m_opaque_sp
Definition SBFile.h:54
lldb::StackFrameSP GetFrameSP() const
Definition SBFrame.cpp:88
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::SBStructuredData GetVariableAnnotations()
Get variable annotations for this instruction as structured data.
lldb::SBData GetData(lldb::SBTarget target)
bool GetDescription(lldb::SBStream &description)
const char * GetMnemonic(lldb::SBTarget target)
lldb_private::Stream & ref()
Definition SBStream.cpp:179
StructuredDataImplUP m_impl_up
lldb::TargetSP GetSP() const
Definition SBTarget.cpp:600
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:32
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.
std::shared_ptr< Array > ArraySP
Defines a symbol context baton that can be handed other debug core functions.
A Lockable handle over a Target's API mutex, returned by Target::GetAPIMutex() and backing the public...
const ArchSpec & GetArchitecture() const
Definition Target.h:1296
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