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
31 // the instruction will only hold a weak reference to the disassembler
32 // to avoid a cycle that was keeping both objects alive (leak) and we
33 // need the InstructionImpl class to make sure our public API behaves
34 // as users would expect. Calls in our public API allow clients to do
37 // 1 lldb::SBInstruction inst;
38 // 2 inst = target.ReadInstructions(pc, 1).GetInstructionAtIndex(0)
39 // 3 if (inst.DoesBranch())
42 // There was a temporary lldb::DisassemblerSP object created in the
43 // SBInstructionList that was returned by lldb.target.ReadInstructions()
44 // that will go away after line 2 but the "inst" object should be able
45 // to still answer questions about itself. So we make sure that any
46 // SBInstruction objects that are given out have a strong reference to
47 // the disassembler and the instruction so that the object can live and
48 // successfully respond to all queries.
49 //----------------------------------------------------------------------
50 class InstructionImpl {
52 InstructionImpl(const lldb::DisassemblerSP &disasm_sp,
53 const lldb::InstructionSP &inst_sp)
54 : m_disasm_sp(disasm_sp), m_inst_sp(inst_sp) {}
56 lldb::InstructionSP GetSP() const { return m_inst_sp; }
58 bool IsValid() const { return (bool)m_inst_sp; }
61 lldb::DisassemblerSP m_disasm_sp; // Can be empty/invalid
62 lldb::InstructionSP m_inst_sp;
66 using namespace lldb_private;
68 SBInstruction::SBInstruction() : m_opaque_sp() {}
70 SBInstruction::SBInstruction(const lldb::DisassemblerSP &disasm_sp,
71 const lldb::InstructionSP &inst_sp)
72 : m_opaque_sp(new InstructionImpl(disasm_sp, inst_sp)) {}
74 SBInstruction::SBInstruction(const SBInstruction &rhs)
75 : m_opaque_sp(rhs.m_opaque_sp) {}
77 const SBInstruction &SBInstruction::operator=(const SBInstruction &rhs) {
79 m_opaque_sp = rhs.m_opaque_sp;
83 SBInstruction::~SBInstruction() {}
85 bool SBInstruction::IsValid() { return m_opaque_sp && m_opaque_sp->IsValid(); }
87 SBAddress SBInstruction::GetAddress() {
89 lldb::InstructionSP inst_sp(GetOpaque());
90 if (inst_sp && inst_sp->GetAddress().IsValid())
91 sb_addr.SetAddress(&inst_sp->GetAddress());
95 const char *SBInstruction::GetMnemonic(SBTarget target) {
96 lldb::InstructionSP inst_sp(GetOpaque());
98 ExecutionContext exe_ctx;
99 TargetSP target_sp(target.GetSP());
100 std::unique_lock<std::recursive_mutex> lock;
102 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
104 target_sp->CalculateExecutionContext(exe_ctx);
105 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
107 return inst_sp->GetMnemonic(&exe_ctx);
112 const char *SBInstruction::GetOperands(SBTarget target) {
113 lldb::InstructionSP inst_sp(GetOpaque());
115 ExecutionContext exe_ctx;
116 TargetSP target_sp(target.GetSP());
117 std::unique_lock<std::recursive_mutex> lock;
119 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
121 target_sp->CalculateExecutionContext(exe_ctx);
122 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
124 return inst_sp->GetOperands(&exe_ctx);
129 const char *SBInstruction::GetComment(SBTarget target) {
130 lldb::InstructionSP inst_sp(GetOpaque());
132 ExecutionContext exe_ctx;
133 TargetSP target_sp(target.GetSP());
134 std::unique_lock<std::recursive_mutex> lock;
136 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
138 target_sp->CalculateExecutionContext(exe_ctx);
139 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
141 return inst_sp->GetComment(&exe_ctx);
146 size_t SBInstruction::GetByteSize() {
147 lldb::InstructionSP inst_sp(GetOpaque());
149 return inst_sp->GetOpcode().GetByteSize();
153 SBData SBInstruction::GetData(SBTarget target) {
154 lldb::SBData sb_data;
155 lldb::InstructionSP inst_sp(GetOpaque());
157 DataExtractorSP data_extractor_sp(new DataExtractor());
158 if (inst_sp->GetData(*data_extractor_sp)) {
159 sb_data.SetOpaque(data_extractor_sp);
165 bool SBInstruction::DoesBranch() {
166 lldb::InstructionSP inst_sp(GetOpaque());
168 return inst_sp->DoesBranch();
172 bool SBInstruction::HasDelaySlot() {
173 lldb::InstructionSP inst_sp(GetOpaque());
175 return inst_sp->HasDelaySlot();
179 bool SBInstruction::CanSetBreakpoint () {
180 lldb::InstructionSP inst_sp(GetOpaque());
182 return inst_sp->CanSetBreakpoint();
186 lldb::InstructionSP SBInstruction::GetOpaque() {
188 return m_opaque_sp->GetSP();
190 return lldb::InstructionSP();
193 void SBInstruction::SetOpaque(const lldb::DisassemblerSP &disasm_sp,
194 const lldb::InstructionSP &inst_sp) {
195 m_opaque_sp.reset(new InstructionImpl(disasm_sp, inst_sp));
198 bool SBInstruction::GetDescription(lldb::SBStream &s) {
199 lldb::InstructionSP inst_sp(GetOpaque());
202 const Address &addr = inst_sp->GetAddress();
203 ModuleSP module_sp(addr.GetModule());
205 module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything,
207 // Use the "ref()" instead of the "get()" accessor in case the SBStream
208 // didn't have a stream already created, one will get created...
209 FormatEntity::Entry format;
210 FormatEntity::Parse("${addr}: ", format);
211 inst_sp->Dump(&s.ref(), 0, true, false, NULL, &sc, NULL, &format, 0);
217 void SBInstruction::Print(FILE *out) {
221 lldb::InstructionSP inst_sp(GetOpaque());
224 const Address &addr = inst_sp->GetAddress();
225 ModuleSP module_sp(addr.GetModule());
227 module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything,
229 StreamFile out_stream(out, false);
230 FormatEntity::Entry format;
231 FormatEntity::Parse("${addr}: ", format);
232 inst_sp->Dump(&out_stream, 0, true, false, NULL, &sc, NULL, &format, 0);
236 bool SBInstruction::EmulateWithFrame(lldb::SBFrame &frame,
237 uint32_t evaluate_options) {
238 lldb::InstructionSP inst_sp(GetOpaque());
240 lldb::StackFrameSP frame_sp(frame.GetFrameSP());
243 lldb_private::ExecutionContext exe_ctx;
244 frame_sp->CalculateExecutionContext(exe_ctx);
245 lldb_private::Target *target = exe_ctx.GetTargetPtr();
246 lldb_private::ArchSpec arch = target->GetArchitecture();
248 return inst_sp->Emulate(
249 arch, evaluate_options, (void *)frame_sp.get(),
250 &lldb_private::EmulateInstruction::ReadMemoryFrame,
251 &lldb_private::EmulateInstruction::WriteMemoryFrame,
252 &lldb_private::EmulateInstruction::ReadRegisterFrame,
253 &lldb_private::EmulateInstruction::WriteRegisterFrame);
259 bool SBInstruction::DumpEmulation(const char *triple) {
260 lldb::InstructionSP inst_sp(GetOpaque());
261 if (inst_sp && triple) {
262 return inst_sp->DumpEmulation(HostInfo::GetAugmentedArchSpec(triple));
267 bool SBInstruction::TestEmulation(lldb::SBStream &output_stream,
268 const char *test_file) {
270 SetOpaque(lldb::DisassemblerSP(),
271 lldb::InstructionSP(new PseudoInstruction()));
273 lldb::InstructionSP inst_sp(GetOpaque());
275 return inst_sp->TestEmulation(output_stream.get(), test_file);
279 lldb::AddressClass SBInstruction::GetAddressClass() {
280 lldb::InstructionSP inst_sp(GetOpaque());
282 return inst_sp->GetAddressClass();
283 return eAddressClassInvalid;