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"
18 #include "lldb/Core/ArchSpec.h"
19 #include "lldb/Core/DataBufferHeap.h"
20 #include "lldb/Core/DataExtractor.h"
21 #include "lldb/Core/Disassembler.h"
22 #include "lldb/Core/EmulateInstruction.h"
23 #include "lldb/Core/Module.h"
24 #include "lldb/Core/StreamFile.h"
25 #include "lldb/Target/ExecutionContext.h"
26 #include "lldb/Target/StackFrame.h"
27 #include "lldb/Target/Target.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 //----------------------------------------------------------------------
53 InstructionImpl (const lldb::DisassemblerSP &disasm_sp, const lldb::InstructionSP& inst_sp) :
54 m_disasm_sp(disasm_sp),
68 return (bool)m_inst_sp;
72 lldb::DisassemblerSP m_disasm_sp; // Can be empty/invalid
73 lldb::InstructionSP m_inst_sp;
77 using namespace lldb_private;
79 SBInstruction::SBInstruction() :
84 SBInstruction::SBInstruction(const lldb::DisassemblerSP &disasm_sp, const lldb::InstructionSP& inst_sp) :
85 m_opaque_sp(new InstructionImpl(disasm_sp, inst_sp))
89 SBInstruction::SBInstruction(const SBInstruction &rhs) :
90 m_opaque_sp (rhs.m_opaque_sp)
95 SBInstruction::operator = (const SBInstruction &rhs)
98 m_opaque_sp = rhs.m_opaque_sp;
102 SBInstruction::~SBInstruction ()
107 SBInstruction::IsValid()
109 return m_opaque_sp && m_opaque_sp->IsValid();
113 SBInstruction::GetAddress()
116 lldb::InstructionSP inst_sp(GetOpaque());
117 if (inst_sp && inst_sp->GetAddress().IsValid())
118 sb_addr.SetAddress(&inst_sp->GetAddress());
123 SBInstruction::GetMnemonic(SBTarget target)
125 lldb::InstructionSP inst_sp(GetOpaque());
128 ExecutionContext exe_ctx;
129 TargetSP target_sp (target.GetSP());
130 std::unique_lock<std::recursive_mutex> lock;
133 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
135 target_sp->CalculateExecutionContext (exe_ctx);
136 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
138 return inst_sp->GetMnemonic(&exe_ctx);
144 SBInstruction::GetOperands(SBTarget target)
146 lldb::InstructionSP inst_sp(GetOpaque());
149 ExecutionContext exe_ctx;
150 TargetSP target_sp (target.GetSP());
151 std::unique_lock<std::recursive_mutex> lock;
154 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
156 target_sp->CalculateExecutionContext (exe_ctx);
157 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
159 return inst_sp->GetOperands(&exe_ctx);
165 SBInstruction::GetComment(SBTarget target)
167 lldb::InstructionSP inst_sp(GetOpaque());
170 ExecutionContext exe_ctx;
171 TargetSP target_sp (target.GetSP());
172 std::unique_lock<std::recursive_mutex> lock;
175 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
177 target_sp->CalculateExecutionContext (exe_ctx);
178 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
180 return inst_sp->GetComment(&exe_ctx);
186 SBInstruction::GetByteSize ()
188 lldb::InstructionSP inst_sp(GetOpaque());
190 return inst_sp->GetOpcode().GetByteSize();
195 SBInstruction::GetData (SBTarget target)
197 lldb::SBData sb_data;
198 lldb::InstructionSP inst_sp(GetOpaque());
201 DataExtractorSP data_extractor_sp (new DataExtractor());
202 if (inst_sp->GetData (*data_extractor_sp))
204 sb_data.SetOpaque (data_extractor_sp);
213 SBInstruction::DoesBranch ()
215 lldb::InstructionSP inst_sp(GetOpaque());
217 return inst_sp->DoesBranch ();
222 SBInstruction::HasDelaySlot ()
224 lldb::InstructionSP inst_sp(GetOpaque());
226 return inst_sp->HasDelaySlot ();
231 SBInstruction::GetOpaque ()
234 return m_opaque_sp->GetSP();
236 return lldb::InstructionSP();
240 SBInstruction::SetOpaque (const lldb::DisassemblerSP &disasm_sp, const lldb::InstructionSP& inst_sp)
242 m_opaque_sp.reset(new InstructionImpl(disasm_sp, inst_sp));
246 SBInstruction::GetDescription (lldb::SBStream &s)
248 lldb::InstructionSP inst_sp(GetOpaque());
252 const Address &addr = inst_sp->GetAddress();
253 ModuleSP module_sp (addr.GetModule());
255 module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything, sc);
256 // Use the "ref()" instead of the "get()" accessor in case the SBStream
257 // didn't have a stream already created, one will get created...
258 FormatEntity::Entry format;
259 FormatEntity::Parse("${addr}: ", format);
260 inst_sp->Dump (&s.ref(), 0, true, false, NULL, &sc, NULL, &format, 0);
267 SBInstruction::Print (FILE *out)
272 lldb::InstructionSP inst_sp(GetOpaque());
276 const Address &addr = inst_sp->GetAddress();
277 ModuleSP module_sp (addr.GetModule());
279 module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything, sc);
280 StreamFile out_stream (out, false);
281 FormatEntity::Entry format;
282 FormatEntity::Parse("${addr}: ", format);
283 inst_sp->Dump (&out_stream, 0, true, false, NULL, &sc, NULL, &format, 0);
288 SBInstruction::EmulateWithFrame (lldb::SBFrame &frame, uint32_t evaluate_options)
290 lldb::InstructionSP inst_sp(GetOpaque());
293 lldb::StackFrameSP frame_sp (frame.GetFrameSP());
297 lldb_private::ExecutionContext exe_ctx;
298 frame_sp->CalculateExecutionContext (exe_ctx);
299 lldb_private::Target *target = exe_ctx.GetTargetPtr();
300 lldb_private::ArchSpec arch = target->GetArchitecture();
302 return inst_sp->Emulate(arch,
304 (void *) frame_sp.get(),
305 &lldb_private::EmulateInstruction::ReadMemoryFrame,
306 &lldb_private::EmulateInstruction::WriteMemoryFrame,
307 &lldb_private::EmulateInstruction::ReadRegisterFrame,
308 &lldb_private::EmulateInstruction::WriteRegisterFrame);
315 SBInstruction::DumpEmulation (const char *triple)
317 lldb::InstructionSP inst_sp(GetOpaque());
318 if (inst_sp && triple)
320 lldb_private::ArchSpec arch (triple, NULL);
321 return inst_sp->DumpEmulation (arch);
327 SBInstruction::TestEmulation (lldb::SBStream &output_stream, const char *test_file)
330 SetOpaque(lldb::DisassemblerSP(), lldb::InstructionSP(new PseudoInstruction()));
332 lldb::InstructionSP inst_sp(GetOpaque());
334 return inst_sp->TestEmulation (output_stream.get(), test_file);
339 SBInstruction::GetAddressClass ()
341 lldb::InstructionSP inst_sp(GetOpaque());
343 return inst_sp->GetAddressClass();
344 return eAddressClassInvalid;