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 std::unique_lock<std::recursive_mutex> lock;
121 if (target_sp) {
122 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
123
124 target_sp->CalculateExecutionContext(exe_ctx);
125 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
126 }
127 return ConstString(inst_sp->GetMnemonic(&exe_ctx)).GetCString();
128}
129
131 LLDB_INSTRUMENT_VA(this, target);
132
134 if (!inst_sp)
135 return nullptr;
136
137 ExecutionContext exe_ctx;
138 TargetSP target_sp(target.GetSP());
139 std::unique_lock<std::recursive_mutex> lock;
140 if (target_sp) {
141 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
142
143 target_sp->CalculateExecutionContext(exe_ctx);
144 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
145 }
146 return ConstString(inst_sp->GetOperands(&exe_ctx)).GetCString();
147}
148
150 LLDB_INSTRUMENT_VA(this, target);
151
153 if (!inst_sp)
154 return nullptr;
155
156 ExecutionContext exe_ctx;
157 TargetSP target_sp(target.GetSP());
158 std::unique_lock<std::recursive_mutex> lock;
159 if (target_sp) {
160 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
161
162 target_sp->CalculateExecutionContext(exe_ctx);
163 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
164 }
165 return ConstString(inst_sp->GetComment(&exe_ctx)).GetCString();
166}
167
170 LLDB_INSTRUMENT_VA(this, target);
171
173 if (inst_sp) {
174 ExecutionContext exe_ctx;
175 TargetSP target_sp(target.GetSP());
176 std::unique_lock<std::recursive_mutex> lock;
177 if (target_sp) {
178 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
179
180 target_sp->CalculateExecutionContext(exe_ctx);
181 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
182 }
183 return inst_sp->GetControlFlowKind(&exe_ctx);
184 }
186}
187
189 LLDB_INSTRUMENT_VA(this);
190
192 if (inst_sp)
193 return inst_sp->GetOpcode().GetByteSize();
194 return 0;
195}
196
198 LLDB_INSTRUMENT_VA(this, target);
199
200 lldb::SBData sb_data;
202 if (inst_sp) {
203 DataExtractorSP data_extractor_sp(new DataExtractor());
204 if (inst_sp->GetData(*data_extractor_sp)) {
205 sb_data.SetOpaque(data_extractor_sp);
206 }
207 }
208 return sb_data;
209}
210
212 LLDB_INSTRUMENT_VA(this);
213
215 if (inst_sp)
216 return inst_sp->DoesBranch();
217 return false;
218}
219
221 LLDB_INSTRUMENT_VA(this);
222
224 if (inst_sp)
225 return inst_sp->HasDelaySlot();
226 return false;
227}
228
230 LLDB_INSTRUMENT_VA(this);
231
233 if (inst_sp)
234 return inst_sp->CanSetBreakpoint();
235 return false;
236}
237
239 if (m_opaque_sp)
240 return m_opaque_sp->GetSP();
241 else
242 return lldb::InstructionSP();
243}
244
246 const lldb::InstructionSP &inst_sp) {
247 m_opaque_sp = std::make_shared<InstructionImpl>(disasm_sp, inst_sp);
248}
249
251 LLDB_INSTRUMENT_VA(this, s);
252
254 if (inst_sp) {
255 SymbolContext sc;
256 const Address &addr = inst_sp->GetAddress();
257 ModuleSP module_sp(addr.GetModule());
258 if (module_sp)
259 module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything,
260 sc);
261 // Use the "ref()" instead of the "get()" accessor in case the SBStream
262 // didn't have a stream already created, one will get created...
263 FormatEntity::Entry format;
264 FormatEntity::Parse("${addr}: ", format);
265 inst_sp->Dump(&s.ref(), 0, true, false, /*show_control_flow_kind=*/false,
266 nullptr, &sc, nullptr, &format, 0);
267 return true;
268 }
269 return false;
270}
271
272void SBInstruction::Print(FILE *outp) {
273 LLDB_INSTRUMENT_VA(this, outp);
274 FileSP out = std::make_shared<NativeFile>(outp, File::eOpenOptionWriteOnly,
275 /*take_ownership=*/false);
276 Print(out);
277}
278
280 LLDB_INSTRUMENT_VA(this, out);
281 Print(out.m_opaque_sp);
282}
283
285 LLDB_INSTRUMENT_VA(this, out_sp);
286
287 if (!out_sp || !out_sp->IsValid())
288 return;
289
291 if (inst_sp) {
292 SymbolContext sc;
293 const Address &addr = inst_sp->GetAddress();
294 ModuleSP module_sp(addr.GetModule());
295 if (module_sp)
296 module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything,
297 sc);
298 StreamFile out_stream(out_sp);
299 FormatEntity::Entry format;
300 FormatEntity::Parse("${addr}: ", format);
301 inst_sp->Dump(&out_stream, 0, true, false, /*show_control_flow_kind=*/false,
302 nullptr, &sc, nullptr, &format, 0);
303 }
304}
305
307 uint32_t evaluate_options) {
308 LLDB_INSTRUMENT_VA(this, frame, evaluate_options);
309
311 if (inst_sp) {
312 lldb::StackFrameSP frame_sp(frame.GetFrameSP());
313
314 if (frame_sp) {
316 frame_sp->CalculateExecutionContext(exe_ctx);
317 lldb_private::Target *target = exe_ctx.GetTargetPtr();
319
320 return inst_sp->Emulate(
321 arch, evaluate_options, (void *)frame_sp.get(),
326 }
327 }
328 return false;
329}
330
331bool SBInstruction::DumpEmulation(const char *triple) {
332 LLDB_INSTRUMENT_VA(this, triple);
333
335 if (inst_sp && triple) {
336 return inst_sp->DumpEmulation(HostInfo::GetAugmentedArchSpec(triple));
337 }
338 return false;
339}
340
342 const char *test_file) {
343 LLDB_INSTRUMENT_VA(this, output_stream, test_file);
344
345 if (!m_opaque_sp)
348
350 if (inst_sp)
351 return inst_sp->TestEmulation(output_stream.ref(), test_file);
352 return false;
353}
354
356 LLDB_INSTRUMENT_VA(this);
357
358 SBStructuredData result;
359
360 if (!m_opaque_sp || !m_opaque_sp->IsValid())
361 return result;
362
363 lldb::InstructionSP inst_sp = m_opaque_sp->GetSP();
364 if (!inst_sp)
365 return result;
366
367 StructuredData::ArraySP array_sp = inst_sp->GetVariableAnnotations();
368 result.m_impl_up->SetObjectSP(array_sp);
369
370 return result;
371}
#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:54
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::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:178
StructuredDataImplUP m_impl_up
lldb::TargetSP GetSP() const
Definition SBTarget.cpp:589
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.
@ eOpenOptionWriteOnly
Definition File.h:52
std::shared_ptr< Array > ArraySP
Defines a symbol context baton that can be handed other debug core functions.
const ArchSpec & GetArchitecture() const
Definition Target.h:1153
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