1 //===-- SBInstruction.cpp ---------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/API/SBInstruction.h"
12 #include "lldb/API/SBAddress.h"
13 #include "lldb/API/SBFrame.h"
14 #include "lldb/API/SBInstruction.h"
15 #include "lldb/API/SBStream.h"
16 #include "lldb/API/SBTarget.h"
17 #include "lldb/Core/Disassembler.h"
18 #include "lldb/Core/EmulateInstruction.h"
19 #include "lldb/Core/Module.h"
20 #include "lldb/Core/StreamFile.h"
21 #include "lldb/Host/HostInfo.h"
22 #include "lldb/Target/ExecutionContext.h"
23 #include "lldb/Target/StackFrame.h"
24 #include "lldb/Target/Target.h"
25 #include "lldb/Utility/ArchSpec.h"
26 #include "lldb/Utility/DataBufferHeap.h"
27 #include "lldb/Utility/DataExtractor.h"
29 //----------------------------------------------------------------------
30 // We recently fixed a leak in one of the Instruction subclasses where the
31 // instruction will only hold a weak reference to the disassembler to avoid a
32 // cycle that was keeping both objects alive (leak) and we need the
33 // InstructionImpl class to make sure our public API behaves as users would
34 // expect. Calls in our public API allow clients to do things like:
36 // 1 lldb::SBInstruction inst;
37 // 2 inst = target.ReadInstructions(pc, 1).GetInstructionAtIndex(0)
38 // 3 if (inst.DoesBranch())
41 // There was a temporary lldb::DisassemblerSP object created in the
42 // SBInstructionList that was returned by lldb.target.ReadInstructions() that
43 // will go away after line 2 but the "inst" object should be able to still
44 // answer questions about itself. So we make sure that any SBInstruction
45 // objects that are given out have a strong reference to the disassembler and
46 // the instruction so that the object can live and successfully respond to all
48 //----------------------------------------------------------------------
49 class InstructionImpl {
51 InstructionImpl(const lldb::DisassemblerSP &disasm_sp,
52 const lldb::InstructionSP &inst_sp)
53 : m_disasm_sp(disasm_sp), m_inst_sp(inst_sp) {}
55 lldb::InstructionSP GetSP() const { return m_inst_sp; }
57 bool IsValid() const { return (bool)m_inst_sp; }
60 lldb::DisassemblerSP m_disasm_sp; // Can be empty/invalid
61 lldb::InstructionSP m_inst_sp;
65 using namespace lldb_private;
67 SBInstruction::SBInstruction() : m_opaque_sp() {}
69 SBInstruction::SBInstruction(const lldb::DisassemblerSP &disasm_sp,
70 const lldb::InstructionSP &inst_sp)
71 : m_opaque_sp(new InstructionImpl(disasm_sp, inst_sp)) {}
73 SBInstruction::SBInstruction(const SBInstruction &rhs)
74 : m_opaque_sp(rhs.m_opaque_sp) {}
76 const SBInstruction &SBInstruction::operator=(const SBInstruction &rhs) {
78 m_opaque_sp = rhs.m_opaque_sp;
82 SBInstruction::~SBInstruction() {}
84 bool SBInstruction::IsValid() { return m_opaque_sp && m_opaque_sp->IsValid(); }
86 SBAddress SBInstruction::GetAddress() {
88 lldb::InstructionSP inst_sp(GetOpaque());
89 if (inst_sp && inst_sp->GetAddress().IsValid())
90 sb_addr.SetAddress(&inst_sp->GetAddress());
94 const char *SBInstruction::GetMnemonic(SBTarget target) {
95 lldb::InstructionSP inst_sp(GetOpaque());
97 ExecutionContext exe_ctx;
98 TargetSP target_sp(target.GetSP());
99 std::unique_lock<std::recursive_mutex> lock;
101 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
103 target_sp->CalculateExecutionContext(exe_ctx);
104 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
106 return inst_sp->GetMnemonic(&exe_ctx);
111 const char *SBInstruction::GetOperands(SBTarget target) {
112 lldb::InstructionSP inst_sp(GetOpaque());
114 ExecutionContext exe_ctx;
115 TargetSP target_sp(target.GetSP());
116 std::unique_lock<std::recursive_mutex> lock;
118 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
120 target_sp->CalculateExecutionContext(exe_ctx);
121 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
123 return inst_sp->GetOperands(&exe_ctx);
128 const char *SBInstruction::GetComment(SBTarget target) {
129 lldb::InstructionSP inst_sp(GetOpaque());
131 ExecutionContext exe_ctx;
132 TargetSP target_sp(target.GetSP());
133 std::unique_lock<std::recursive_mutex> lock;
135 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
137 target_sp->CalculateExecutionContext(exe_ctx);
138 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
140 return inst_sp->GetComment(&exe_ctx);
145 size_t SBInstruction::GetByteSize() {
146 lldb::InstructionSP inst_sp(GetOpaque());
148 return inst_sp->GetOpcode().GetByteSize();
152 SBData SBInstruction::GetData(SBTarget target) {
153 lldb::SBData sb_data;
154 lldb::InstructionSP inst_sp(GetOpaque());
156 DataExtractorSP data_extractor_sp(new DataExtractor());
157 if (inst_sp->GetData(*data_extractor_sp)) {
158 sb_data.SetOpaque(data_extractor_sp);
164 bool SBInstruction::DoesBranch() {
165 lldb::InstructionSP inst_sp(GetOpaque());
167 return inst_sp->DoesBranch();
171 bool SBInstruction::HasDelaySlot() {
172 lldb::InstructionSP inst_sp(GetOpaque());
174 return inst_sp->HasDelaySlot();
178 bool SBInstruction::CanSetBreakpoint () {
179 lldb::InstructionSP inst_sp(GetOpaque());
181 return inst_sp->CanSetBreakpoint();
185 lldb::InstructionSP SBInstruction::GetOpaque() {
187 return m_opaque_sp->GetSP();
189 return lldb::InstructionSP();
192 void SBInstruction::SetOpaque(const lldb::DisassemblerSP &disasm_sp,
193 const lldb::InstructionSP &inst_sp) {
194 m_opaque_sp.reset(new InstructionImpl(disasm_sp, inst_sp));
197 bool SBInstruction::GetDescription(lldb::SBStream &s) {
198 lldb::InstructionSP inst_sp(GetOpaque());
201 const Address &addr = inst_sp->GetAddress();
202 ModuleSP module_sp(addr.GetModule());
204 module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything,
206 // Use the "ref()" instead of the "get()" accessor in case the SBStream
207 // didn't have a stream already created, one will get created...
208 FormatEntity::Entry format;
209 FormatEntity::Parse("${addr}: ", format);
210 inst_sp->Dump(&s.ref(), 0, true, false, NULL, &sc, NULL, &format, 0);
216 void SBInstruction::Print(FILE *out) {
220 lldb::InstructionSP inst_sp(GetOpaque());
223 const Address &addr = inst_sp->GetAddress();
224 ModuleSP module_sp(addr.GetModule());
226 module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything,
228 StreamFile out_stream(out, false);
229 FormatEntity::Entry format;
230 FormatEntity::Parse("${addr}: ", format);
231 inst_sp->Dump(&out_stream, 0, true, false, NULL, &sc, NULL, &format, 0);
235 bool SBInstruction::EmulateWithFrame(lldb::SBFrame &frame,
236 uint32_t evaluate_options) {
237 lldb::InstructionSP inst_sp(GetOpaque());
239 lldb::StackFrameSP frame_sp(frame.GetFrameSP());
242 lldb_private::ExecutionContext exe_ctx;
243 frame_sp->CalculateExecutionContext(exe_ctx);
244 lldb_private::Target *target = exe_ctx.GetTargetPtr();
245 lldb_private::ArchSpec arch = target->GetArchitecture();
247 return inst_sp->Emulate(
248 arch, evaluate_options, (void *)frame_sp.get(),
249 &lldb_private::EmulateInstruction::ReadMemoryFrame,
250 &lldb_private::EmulateInstruction::WriteMemoryFrame,
251 &lldb_private::EmulateInstruction::ReadRegisterFrame,
252 &lldb_private::EmulateInstruction::WriteRegisterFrame);
258 bool SBInstruction::DumpEmulation(const char *triple) {
259 lldb::InstructionSP inst_sp(GetOpaque());
260 if (inst_sp && triple) {
261 return inst_sp->DumpEmulation(HostInfo::GetAugmentedArchSpec(triple));
266 bool SBInstruction::TestEmulation(lldb::SBStream &output_stream,
267 const char *test_file) {
269 SetOpaque(lldb::DisassemblerSP(),
270 lldb::InstructionSP(new PseudoInstruction()));
272 lldb::InstructionSP inst_sp(GetOpaque());
274 return inst_sp->TestEmulation(output_stream.get(), test_file);