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
10#include "Utils.h"
12
13#include "lldb/API/SBAddress.h"
14#include "lldb/API/SBFile.h"
15#include "lldb/API/SBFrame.h"
16
17#include "lldb/API/SBStream.h"
19#include "lldb/API/SBTarget.h"
22#include "lldb/Core/Module.h"
23#include "lldb/Host/HostInfo.h"
27#include "lldb/Target/Target.h"
33
34#include <memory>
35
36// We recently fixed a leak in one of the Instruction subclasses where the
37// instruction will only hold a weak reference to the disassembler to avoid a
38// cycle that was keeping both objects alive (leak) and we need the
39// InstructionImpl class to make sure our public API behaves as users would
40// expect. Calls in our public API allow clients to do things like:
41//
42// 1 lldb::SBInstruction inst;
43// 2 inst = target.ReadInstructions(pc, 1).GetInstructionAtIndex(0)
44// 3 if (inst.DoesBranch())
45// 4 ...
46//
47// There was a temporary lldb::DisassemblerSP object created in the
48// SBInstructionList that was returned by lldb.target.ReadInstructions() that
49// will go away after line 2 but the "inst" object should be able to still
50// answer questions about itself. So we make sure that any SBInstruction
51// objects that are given out have a strong reference to the disassembler and
52// the instruction so that the object can live and successfully respond to all
53// queries.
55public:
57 const lldb::InstructionSP &inst_sp)
58 : m_disasm_sp(disasm_sp), m_inst_sp(inst_sp) {}
59
61
62 bool IsValid() const { return (bool)m_inst_sp; }
63
64protected:
65 lldb::DisassemblerSP m_disasm_sp; // Can be empty/invalid
67};
68
69using namespace lldb;
70using namespace lldb_private;
71
73
75 const lldb::InstructionSP &inst_sp)
76 : m_opaque_sp(new InstructionImpl(disasm_sp, inst_sp)) {}
77
82
84 LLDB_INSTRUMENT_VA(this, rhs);
85
86 if (this != &rhs)
88 return *this;
89}
90
92
95 return this->operator bool();
96}
97SBInstruction::operator bool() const {
99
100 return m_opaque_sp && m_opaque_sp->IsValid();
101}
102
104 LLDB_INSTRUMENT_VA(this);
105
106 SBAddress sb_addr;
108 if (inst_sp && inst_sp->GetAddress().IsValid())
109 sb_addr.SetAddress(inst_sp->GetAddress());
110 return sb_addr;
111}
112
114 LLDB_INSTRUMENT_VA(this, target);
115
117 if (!inst_sp)
118 return nullptr;
119
120 ExecutionContext exe_ctx;
121 TargetSP target_sp(target.GetSP());
122 TargetAPIMutex api_lock;
123 std::unique_lock<TargetAPIMutex> guard;
124 if (target_sp) {
125 api_lock = TargetAPIMutex(target_sp->GetAPIMutex());
126 guard = std::unique_lock<TargetAPIMutex>(api_lock);
127
128 target_sp->CalculateExecutionContext(exe_ctx);
129 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
130 }
131 StringPoolRef pool = target_sp ? GetDebuggerStringPool(*target_sp)
133 return pool.Intern(inst_sp->GetMnemonic(&exe_ctx));
134}
135
137 LLDB_INSTRUMENT_VA(this, target);
138
140 if (!inst_sp)
141 return nullptr;
142
143 ExecutionContext exe_ctx;
144 TargetSP target_sp(target.GetSP());
145 TargetAPIMutex api_lock;
146 std::unique_lock<TargetAPIMutex> guard;
147 if (target_sp) {
148 api_lock = TargetAPIMutex(target_sp->GetAPIMutex());
149 guard = std::unique_lock<TargetAPIMutex>(api_lock);
150
151 target_sp->CalculateExecutionContext(exe_ctx);
152 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
153 }
154 StringPoolRef pool = target_sp ? GetDebuggerStringPool(*target_sp)
156 return pool.Intern(inst_sp->GetOperands(&exe_ctx));
157}
158
160 LLDB_INSTRUMENT_VA(this, target);
161
163 if (!inst_sp)
164 return nullptr;
165
166 ExecutionContext exe_ctx;
167 TargetSP target_sp(target.GetSP());
168 TargetAPIMutex api_lock;
169 std::unique_lock<TargetAPIMutex> guard;
170 if (target_sp) {
171 api_lock = TargetAPIMutex(target_sp->GetAPIMutex());
172 guard = std::unique_lock<TargetAPIMutex>(api_lock);
173
174 target_sp->CalculateExecutionContext(exe_ctx);
175 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
176 }
177 StringPoolRef pool = target_sp ? GetDebuggerStringPool(*target_sp)
179 return pool.Intern(inst_sp->GetComment(&exe_ctx));
180}
181
184 LLDB_INSTRUMENT_VA(this, target);
185
187 if (inst_sp) {
188 ExecutionContext exe_ctx;
189 TargetSP target_sp(target.GetSP());
190 TargetAPIMutex api_lock;
191 std::unique_lock<TargetAPIMutex> guard;
192 if (target_sp) {
193 api_lock = TargetAPIMutex(target_sp->GetAPIMutex());
194 guard = std::unique_lock<TargetAPIMutex>(api_lock);
195
196 target_sp->CalculateExecutionContext(exe_ctx);
197 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
198 }
199 return inst_sp->GetControlFlowKind(&exe_ctx);
200 }
202}
203
205 LLDB_INSTRUMENT_VA(this);
206
208 if (inst_sp)
209 return inst_sp->GetOpcode().GetByteSize();
210 return 0;
211}
212
214 LLDB_INSTRUMENT_VA(this, target);
215
216 lldb::SBData sb_data;
218 if (inst_sp) {
219 DataExtractorSP data_extractor_sp(new DataExtractor());
220 if (inst_sp->GetData(*data_extractor_sp)) {
221 sb_data.SetOpaque(data_extractor_sp);
222 }
223 }
224 return sb_data;
225}
226
228 LLDB_INSTRUMENT_VA(this);
229
231 if (inst_sp)
232 return inst_sp->DoesBranch();
233 return false;
234}
235
237 LLDB_INSTRUMENT_VA(this);
238
240 if (inst_sp)
241 return inst_sp->HasDelaySlot();
242 return false;
243}
244
246 LLDB_INSTRUMENT_VA(this);
247
249 if (inst_sp)
250 return inst_sp->CanSetBreakpoint();
251 return false;
252}
253
255 if (m_opaque_sp)
256 return m_opaque_sp->GetSP();
257 else
258 return lldb::InstructionSP();
259}
260
262 const lldb::InstructionSP &inst_sp) {
263 m_opaque_sp = std::make_shared<InstructionImpl>(disasm_sp, inst_sp);
264}
265
267 LLDB_INSTRUMENT_VA(this, s);
268
270 if (inst_sp) {
271 SymbolContext sc;
272 const Address &addr = inst_sp->GetAddress();
273 ModuleSP module_sp(addr.GetModule());
274 if (module_sp)
275 module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything,
276 sc);
277 // Use the "ref()" instead of the "get()" accessor in case the SBStream
278 // didn't have a stream already created, one will get created...
279 FormatEntity::Entry format;
280 FormatEntity::Parse("${addr}: ", format);
281 inst_sp->Dump(&s.ref(), 0, true, false, /*show_control_flow_kind=*/false,
282 nullptr, &sc, nullptr, &format, 0);
283 return true;
284 }
285 return false;
286}
287
288void SBInstruction::Print(FILE *outp) {
289 LLDB_INSTRUMENT_VA(this, outp);
290 FileSP out = std::make_shared<NativeFile>(outp, File::eOpenOptionWriteOnly,
291 /*take_ownership=*/false);
292 Print(out);
293}
294
296 LLDB_INSTRUMENT_VA(this, out);
297 Print(out.m_opaque_sp);
298}
299
301 LLDB_INSTRUMENT_VA(this, out_sp);
302
303 if (!out_sp || !out_sp->IsValid())
304 return;
305
307 if (inst_sp) {
308 SymbolContext sc;
309 const Address &addr = inst_sp->GetAddress();
310 ModuleSP module_sp(addr.GetModule());
311 if (module_sp)
312 module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything,
313 sc);
314 StreamFile out_stream(out_sp);
315 FormatEntity::Entry format;
316 FormatEntity::Parse("${addr}: ", format);
317 inst_sp->Dump(&out_stream, 0, true, false, /*show_control_flow_kind=*/false,
318 nullptr, &sc, nullptr, &format, 0);
319 }
320}
321
323 uint32_t evaluate_options) {
324 LLDB_INSTRUMENT_VA(this, frame, evaluate_options);
325
327 if (inst_sp) {
328 lldb::StackFrameSP frame_sp(frame.GetFrameSP());
329
330 if (frame_sp) {
332 frame_sp->CalculateExecutionContext(exe_ctx);
333 lldb_private::Target *target = exe_ctx.GetTargetPtr();
335
336 return inst_sp->Emulate(
337 arch, evaluate_options, (void *)frame_sp.get(),
342 }
343 }
344 return false;
345}
346
347bool SBInstruction::DumpEmulation(const char *triple) {
348 LLDB_INSTRUMENT_VA(this, triple);
349
351 if (inst_sp && triple) {
352 return inst_sp->DumpEmulation(HostInfo::GetAugmentedArchSpec(triple));
353 }
354 return false;
355}
356
358 const char *test_file) {
359 LLDB_INSTRUMENT_VA(this, output_stream, test_file);
360
361 if (!m_opaque_sp)
364
366 if (inst_sp)
367 return inst_sp->TestEmulation(output_stream.ref(), test_file);
368 return false;
369}
370
372 LLDB_INSTRUMENT_VA(this);
373
374 SBStructuredData result;
375
376 if (!m_opaque_sp || !m_opaque_sp->IsValid())
377 return result;
378
379 lldb::InstructionSP inst_sp = m_opaque_sp->GetSP();
380 if (!inst_sp)
381 return result;
382
383 StructuredData::ArraySP array_sp = inst_sp->GetVariableAnnotations();
384 result.m_impl_up->SetObjectSP(array_sp);
385
386 return result;
387}
#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:90
void SetOpaque(const lldb::DataExtractorSP &data_sp)
Definition SBData.cpp:47
FileSP m_opaque_sp
Definition SBFile.h:64
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:275
An architecture specification class.
Definition ArchSpec.h:32
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.
A handle to a StringPool that does not own it.
Definition StringPool.h:72
const char * Intern(llvm::StringRef str) const
Definition StringPool.h:78
static StringPoolRef GetSystemPool()
The system pool. Only valid between Initialize and Terminate.
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:1337
Status Parse(const llvm::StringRef &format, Entry &entry)
A class that represents a running process on the host machine.
StringPoolRef GetDebuggerStringPool(const Target &target)
Strings interned here live as long as target's debugger.
Definition Utils.h:21
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