180814287SRaphael Isemann //===-- SBInstruction.cpp -------------------------------------------------===//
230fdc8d8SChris Lattner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
630fdc8d8SChris Lattner //
730fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
830fdc8d8SChris Lattner
930fdc8d8SChris Lattner #include "lldb/API/SBInstruction.h"
101755f5b1SJonas Devlieghere #include "lldb/Utility/Instrumentation.h"
1130fdc8d8SChris Lattner
121d273166SGreg Clayton #include "lldb/API/SBAddress.h"
137c9dd3ceSCaroline Tice #include "lldb/API/SBFrame.h"
14e7a91156SLawrence D'Anna #include "lldb/API/SBFile.h"
15796ac80bSJonas Devlieghere
161d273166SGreg Clayton #include "lldb/API/SBInstruction.h"
171d273166SGreg Clayton #include "lldb/API/SBStream.h"
187c9dd3ceSCaroline Tice #include "lldb/API/SBTarget.h"
1930fdc8d8SChris Lattner #include "lldb/Core/Disassembler.h"
207c9dd3ceSCaroline Tice #include "lldb/Core/EmulateInstruction.h"
21aff1b357SJason Molenda #include "lldb/Core/Module.h"
221d273166SGreg Clayton #include "lldb/Core/StreamFile.h"
237263f1bdSPavel Labath #include "lldb/Host/HostInfo.h"
247c9dd3ceSCaroline Tice #include "lldb/Target/ExecutionContext.h"
25b57e4a1bSJason Molenda #include "lldb/Target/StackFrame.h"
267c9dd3ceSCaroline Tice #include "lldb/Target/Target.h"
275f19b907SPavel Labath #include "lldb/Utility/ArchSpec.h"
28666cc0b2SZachary Turner #include "lldb/Utility/DataBufferHeap.h"
29666cc0b2SZachary Turner #include "lldb/Utility/DataExtractor.h"
3030fdc8d8SChris Lattner
31796ac80bSJonas Devlieghere #include <memory>
32796ac80bSJonas Devlieghere
3305097246SAdrian Prantl // We recently fixed a leak in one of the Instruction subclasses where the
3405097246SAdrian Prantl // instruction will only hold a weak reference to the disassembler to avoid a
3505097246SAdrian Prantl // cycle that was keeping both objects alive (leak) and we need the
3605097246SAdrian Prantl // InstructionImpl class to make sure our public API behaves as users would
3705097246SAdrian Prantl // expect. Calls in our public API allow clients to do things like:
384a9d83a5SGreg Clayton //
394a9d83a5SGreg Clayton // 1 lldb::SBInstruction inst;
404a9d83a5SGreg Clayton // 2 inst = target.ReadInstructions(pc, 1).GetInstructionAtIndex(0)
414a9d83a5SGreg Clayton // 3 if (inst.DoesBranch())
424a9d83a5SGreg Clayton // 4 ...
434a9d83a5SGreg Clayton //
444a9d83a5SGreg Clayton // There was a temporary lldb::DisassemblerSP object created in the
4505097246SAdrian Prantl // SBInstructionList that was returned by lldb.target.ReadInstructions() that
4605097246SAdrian Prantl // will go away after line 2 but the "inst" object should be able to still
4705097246SAdrian Prantl // answer questions about itself. So we make sure that any SBInstruction
4805097246SAdrian Prantl // objects that are given out have a strong reference to the disassembler and
4905097246SAdrian Prantl // the instruction so that the object can live and successfully respond to all
5005097246SAdrian Prantl // queries.
51b9c1b51eSKate Stone class InstructionImpl {
524a9d83a5SGreg Clayton public:
InstructionImpl(const lldb::DisassemblerSP & disasm_sp,const lldb::InstructionSP & inst_sp)53b9c1b51eSKate Stone InstructionImpl(const lldb::DisassemblerSP &disasm_sp,
54b9c1b51eSKate Stone const lldb::InstructionSP &inst_sp)
55b9c1b51eSKate Stone : m_disasm_sp(disasm_sp), m_inst_sp(inst_sp) {}
5630fdc8d8SChris Lattner
GetSP() const57b9c1b51eSKate Stone lldb::InstructionSP GetSP() const { return m_inst_sp; }
584a9d83a5SGreg Clayton
IsValid() const59b9c1b51eSKate Stone bool IsValid() const { return (bool)m_inst_sp; }
604a9d83a5SGreg Clayton
614a9d83a5SGreg Clayton protected:
624a9d83a5SGreg Clayton lldb::DisassemblerSP m_disasm_sp; // Can be empty/invalid
634a9d83a5SGreg Clayton lldb::InstructionSP m_inst_sp;
644a9d83a5SGreg Clayton };
654a9d83a5SGreg Clayton
664a9d83a5SGreg Clayton using namespace lldb;
674a9d83a5SGreg Clayton using namespace lldb_private;
684a9d83a5SGreg Clayton
SBInstruction()691755f5b1SJonas Devlieghere SBInstruction::SBInstruction() { LLDB_INSTRUMENT_VA(this); }
704a9d83a5SGreg Clayton
SBInstruction(const lldb::DisassemblerSP & disasm_sp,const lldb::InstructionSP & inst_sp)71b9c1b51eSKate Stone SBInstruction::SBInstruction(const lldb::DisassemblerSP &disasm_sp,
72b9c1b51eSKate Stone const lldb::InstructionSP &inst_sp)
73b9c1b51eSKate Stone : m_opaque_sp(new InstructionImpl(disasm_sp, inst_sp)) {}
741d273166SGreg Clayton
SBInstruction(const SBInstruction & rhs)75b9c1b51eSKate Stone SBInstruction::SBInstruction(const SBInstruction &rhs)
76baf5664fSJonas Devlieghere : m_opaque_sp(rhs.m_opaque_sp) {
771755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, rhs);
78baf5664fSJonas Devlieghere }
79efabb123SGreg Clayton
operator =(const SBInstruction & rhs)80b9c1b51eSKate Stone const SBInstruction &SBInstruction::operator=(const SBInstruction &rhs) {
811755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, rhs);
82baf5664fSJonas Devlieghere
83efabb123SGreg Clayton if (this != &rhs)
84efabb123SGreg Clayton m_opaque_sp = rhs.m_opaque_sp;
85d232abc3SJonas Devlieghere return *this;
86efabb123SGreg Clayton }
87efabb123SGreg Clayton
88866b7a65SJonas Devlieghere SBInstruction::~SBInstruction() = default;
8930fdc8d8SChris Lattner
IsValid()90baf5664fSJonas Devlieghere bool SBInstruction::IsValid() {
911755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this);
927f5237bcSPavel Labath return this->operator bool();
937f5237bcSPavel Labath }
operator bool() const947f5237bcSPavel Labath SBInstruction::operator bool() const {
951755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this);
96baf5664fSJonas Devlieghere
97baf5664fSJonas Devlieghere return m_opaque_sp && m_opaque_sp->IsValid();
98baf5664fSJonas Devlieghere }
9930fdc8d8SChris Lattner
GetAddress()100b9c1b51eSKate Stone SBAddress SBInstruction::GetAddress() {
1011755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this);
102baf5664fSJonas Devlieghere
1031d273166SGreg Clayton SBAddress sb_addr;
1044a9d83a5SGreg Clayton lldb::InstructionSP inst_sp(GetOpaque());
1054a9d83a5SGreg Clayton if (inst_sp && inst_sp->GetAddress().IsValid())
1066cd4a4cdSJonas Devlieghere sb_addr.SetAddress(inst_sp->GetAddress());
107d232abc3SJonas Devlieghere return sb_addr;
1081d273166SGreg Clayton }
10930fdc8d8SChris Lattner
GetMnemonic(SBTarget target)110b9c1b51eSKate Stone const char *SBInstruction::GetMnemonic(SBTarget target) {
1111755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, target);
112baf5664fSJonas Devlieghere
1134a9d83a5SGreg Clayton lldb::InstructionSP inst_sp(GetOpaque());
114b9c1b51eSKate Stone if (inst_sp) {
1158f7180b1SGreg Clayton ExecutionContext exe_ctx;
116b9556accSGreg Clayton TargetSP target_sp(target.GetSP());
117bb19a13cSSaleem Abdulrasool std::unique_lock<std::recursive_mutex> lock;
118b9c1b51eSKate Stone if (target_sp) {
119bb19a13cSSaleem Abdulrasool lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
120bb19a13cSSaleem Abdulrasool
121b9556accSGreg Clayton target_sp->CalculateExecutionContext(exe_ctx);
122b9556accSGreg Clayton exe_ctx.SetProcessSP(target_sp->GetProcessSP());
1238f7180b1SGreg Clayton }
1244a9d83a5SGreg Clayton return inst_sp->GetMnemonic(&exe_ctx);
1258f7180b1SGreg Clayton }
126248a1305SKonrad Kleine return nullptr;
1278f7180b1SGreg Clayton }
1288f7180b1SGreg Clayton
GetOperands(SBTarget target)129b9c1b51eSKate Stone const char *SBInstruction::GetOperands(SBTarget target) {
1301755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, target);
131baf5664fSJonas Devlieghere
1324a9d83a5SGreg Clayton lldb::InstructionSP inst_sp(GetOpaque());
133b9c1b51eSKate Stone if (inst_sp) {
1348f7180b1SGreg Clayton ExecutionContext exe_ctx;
135b9556accSGreg Clayton TargetSP target_sp(target.GetSP());
136bb19a13cSSaleem Abdulrasool std::unique_lock<std::recursive_mutex> lock;
137b9c1b51eSKate Stone if (target_sp) {
138bb19a13cSSaleem Abdulrasool lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
139bb19a13cSSaleem Abdulrasool
140b9556accSGreg Clayton target_sp->CalculateExecutionContext(exe_ctx);
141b9556accSGreg Clayton exe_ctx.SetProcessSP(target_sp->GetProcessSP());
1428f7180b1SGreg Clayton }
1434a9d83a5SGreg Clayton return inst_sp->GetOperands(&exe_ctx);
1448f7180b1SGreg Clayton }
145248a1305SKonrad Kleine return nullptr;
1468f7180b1SGreg Clayton }
1478f7180b1SGreg Clayton
GetComment(SBTarget target)148b9c1b51eSKate Stone const char *SBInstruction::GetComment(SBTarget target) {
1491755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, target);
150baf5664fSJonas Devlieghere
1514a9d83a5SGreg Clayton lldb::InstructionSP inst_sp(GetOpaque());
152b9c1b51eSKate Stone if (inst_sp) {
1538f7180b1SGreg Clayton ExecutionContext exe_ctx;
154b9556accSGreg Clayton TargetSP target_sp(target.GetSP());
155bb19a13cSSaleem Abdulrasool std::unique_lock<std::recursive_mutex> lock;
156b9c1b51eSKate Stone if (target_sp) {
157bb19a13cSSaleem Abdulrasool lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
158bb19a13cSSaleem Abdulrasool
159b9556accSGreg Clayton target_sp->CalculateExecutionContext(exe_ctx);
160b9556accSGreg Clayton exe_ctx.SetProcessSP(target_sp->GetProcessSP());
1618f7180b1SGreg Clayton }
1624a9d83a5SGreg Clayton return inst_sp->GetComment(&exe_ctx);
1638f7180b1SGreg Clayton }
164248a1305SKonrad Kleine return nullptr;
1658f7180b1SGreg Clayton }
1668f7180b1SGreg Clayton
GetByteSize()167b9c1b51eSKate Stone size_t SBInstruction::GetByteSize() {
1681755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this);
169baf5664fSJonas Devlieghere
1704a9d83a5SGreg Clayton lldb::InstructionSP inst_sp(GetOpaque());
1714a9d83a5SGreg Clayton if (inst_sp)
1724a9d83a5SGreg Clayton return inst_sp->GetOpcode().GetByteSize();
1731d273166SGreg Clayton return 0;
1741d273166SGreg Clayton }
17530fdc8d8SChris Lattner
GetData(SBTarget target)176b9c1b51eSKate Stone SBData SBInstruction::GetData(SBTarget target) {
1771755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, target);
178baf5664fSJonas Devlieghere
1798f7180b1SGreg Clayton lldb::SBData sb_data;
1804a9d83a5SGreg Clayton lldb::InstructionSP inst_sp(GetOpaque());
181b9c1b51eSKate Stone if (inst_sp) {
182d1411e1aSGreg Clayton DataExtractorSP data_extractor_sp(new DataExtractor());
183b9c1b51eSKate Stone if (inst_sp->GetData(*data_extractor_sp)) {
1848f7180b1SGreg Clayton sb_data.SetOpaque(data_extractor_sp);
1858f7180b1SGreg Clayton }
1868f7180b1SGreg Clayton }
187d232abc3SJonas Devlieghere return sb_data;
1888f7180b1SGreg Clayton }
1898f7180b1SGreg Clayton
DoesBranch()190b9c1b51eSKate Stone bool SBInstruction::DoesBranch() {
1911755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this);
192baf5664fSJonas Devlieghere
1934a9d83a5SGreg Clayton lldb::InstructionSP inst_sp(GetOpaque());
1944a9d83a5SGreg Clayton if (inst_sp)
1954a9d83a5SGreg Clayton return inst_sp->DoesBranch();
1961d273166SGreg Clayton return false;
1971d273166SGreg Clayton }
1981d273166SGreg Clayton
HasDelaySlot()199b9c1b51eSKate Stone bool SBInstruction::HasDelaySlot() {
2001755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this);
201baf5664fSJonas Devlieghere
2024a9d83a5SGreg Clayton lldb::InstructionSP inst_sp(GetOpaque());
2034a9d83a5SGreg Clayton if (inst_sp)
2044a9d83a5SGreg Clayton return inst_sp->HasDelaySlot();
205df5f0b44SBhushan D. Attarde return false;
206df5f0b44SBhushan D. Attarde }
207df5f0b44SBhushan D. Attarde
CanSetBreakpoint()208dd125943SNitesh Jain bool SBInstruction::CanSetBreakpoint() {
2091755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this);
210baf5664fSJonas Devlieghere
211dd125943SNitesh Jain lldb::InstructionSP inst_sp(GetOpaque());
212dd125943SNitesh Jain if (inst_sp)
213dd125943SNitesh Jain return inst_sp->CanSetBreakpoint();
214dd125943SNitesh Jain return false;
215dd125943SNitesh Jain }
216dd125943SNitesh Jain
GetOpaque()217b9c1b51eSKate Stone lldb::InstructionSP SBInstruction::GetOpaque() {
2184a9d83a5SGreg Clayton if (m_opaque_sp)
2194a9d83a5SGreg Clayton return m_opaque_sp->GetSP();
2204a9d83a5SGreg Clayton else
2214a9d83a5SGreg Clayton return lldb::InstructionSP();
2224a9d83a5SGreg Clayton }
2234a9d83a5SGreg Clayton
SetOpaque(const lldb::DisassemblerSP & disasm_sp,const lldb::InstructionSP & inst_sp)224b9c1b51eSKate Stone void SBInstruction::SetOpaque(const lldb::DisassemblerSP &disasm_sp,
225b9c1b51eSKate Stone const lldb::InstructionSP &inst_sp) {
226796ac80bSJonas Devlieghere m_opaque_sp = std::make_shared<InstructionImpl>(disasm_sp, inst_sp);
2271d273166SGreg Clayton }
2281d273166SGreg Clayton
GetDescription(lldb::SBStream & s)229b9c1b51eSKate Stone bool SBInstruction::GetDescription(lldb::SBStream &s) {
2301755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, s);
231baf5664fSJonas Devlieghere
2324a9d83a5SGreg Clayton lldb::InstructionSP inst_sp(GetOpaque());
233b9c1b51eSKate Stone if (inst_sp) {
234aff1b357SJason Molenda SymbolContext sc;
2354a9d83a5SGreg Clayton const Address &addr = inst_sp->GetAddress();
236aff1b357SJason Molenda ModuleSP module_sp(addr.GetModule());
237aff1b357SJason Molenda if (module_sp)
238b9c1b51eSKate Stone module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything,
239b9c1b51eSKate Stone sc);
2401d273166SGreg Clayton // Use the "ref()" instead of the "get()" accessor in case the SBStream
2411d273166SGreg Clayton // didn't have a stream already created, one will get created...
242554f68d3SGreg Clayton FormatEntity::Entry format;
243554f68d3SGreg Clayton FormatEntity::Parse("${addr}: ", format);
244*ad7bcda9SWalter Erquinigo inst_sp->Dump(&s.ref(), 0, true, false, /*show_control_flow_kind=*/false,
245*ad7bcda9SWalter Erquinigo nullptr, &sc, nullptr, &format, 0);
2461d273166SGreg Clayton return true;
2471d273166SGreg Clayton }
2481d273166SGreg Clayton return false;
2491d273166SGreg Clayton }
25030fdc8d8SChris Lattner
Print(FILE * outp)251e7a91156SLawrence D'Anna void SBInstruction::Print(FILE *outp) {
2521755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, outp);
253e7a91156SLawrence D'Anna FileSP out = std::make_shared<NativeFile>(outp, /*take_ownership=*/false);
254e7a91156SLawrence D'Anna Print(out);
255e7a91156SLawrence D'Anna }
256baf5664fSJonas Devlieghere
Print(SBFile out)257e7a91156SLawrence D'Anna void SBInstruction::Print(SBFile out) {
2581755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, out);
2599efbc564SLawrence D'Anna Print(out.m_opaque_sp);
260e7a91156SLawrence D'Anna }
261e7a91156SLawrence D'Anna
Print(FileSP out_sp)262e7a91156SLawrence D'Anna void SBInstruction::Print(FileSP out_sp) {
2631755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, out_sp);
264e7a91156SLawrence D'Anna
265e7a91156SLawrence D'Anna if (!out_sp || !out_sp->IsValid())
26630fdc8d8SChris Lattner return;
26730fdc8d8SChris Lattner
2684a9d83a5SGreg Clayton lldb::InstructionSP inst_sp(GetOpaque());
269b9c1b51eSKate Stone if (inst_sp) {
270aff1b357SJason Molenda SymbolContext sc;
2714a9d83a5SGreg Clayton const Address &addr = inst_sp->GetAddress();
272aff1b357SJason Molenda ModuleSP module_sp(addr.GetModule());
273aff1b357SJason Molenda if (module_sp)
274b9c1b51eSKate Stone module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything,
275b9c1b51eSKate Stone sc);
276e7a91156SLawrence D'Anna StreamFile out_stream(out_sp);
277554f68d3SGreg Clayton FormatEntity::Entry format;
278554f68d3SGreg Clayton FormatEntity::Parse("${addr}: ", format);
279*ad7bcda9SWalter Erquinigo inst_sp->Dump(&out_stream, 0, true, false, /*show_control_flow_kind=*/false,
280*ad7bcda9SWalter Erquinigo nullptr, &sc, nullptr, &format, 0);
2811d273166SGreg Clayton }
28230fdc8d8SChris Lattner }
2837c9dd3ceSCaroline Tice
EmulateWithFrame(lldb::SBFrame & frame,uint32_t evaluate_options)284b9c1b51eSKate Stone bool SBInstruction::EmulateWithFrame(lldb::SBFrame &frame,
285b9c1b51eSKate Stone uint32_t evaluate_options) {
2861755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, frame, evaluate_options);
287baf5664fSJonas Devlieghere
2884a9d83a5SGreg Clayton lldb::InstructionSP inst_sp(GetOpaque());
289b9c1b51eSKate Stone if (inst_sp) {
290b57e4a1bSJason Molenda lldb::StackFrameSP frame_sp(frame.GetFrameSP());
291b9556accSGreg Clayton
292b9c1b51eSKate Stone if (frame_sp) {
2937c9dd3ceSCaroline Tice lldb_private::ExecutionContext exe_ctx;
294b9556accSGreg Clayton frame_sp->CalculateExecutionContext(exe_ctx);
295c14ee32dSGreg Clayton lldb_private::Target *target = exe_ctx.GetTargetPtr();
2967c9dd3ceSCaroline Tice lldb_private::ArchSpec arch = target->GetArchitecture();
2977c9dd3ceSCaroline Tice
298b9c1b51eSKate Stone return inst_sp->Emulate(
299b9c1b51eSKate Stone arch, evaluate_options, (void *)frame_sp.get(),
3007c9dd3ceSCaroline Tice &lldb_private::EmulateInstruction::ReadMemoryFrame,
3017c9dd3ceSCaroline Tice &lldb_private::EmulateInstruction::WriteMemoryFrame,
3027c9dd3ceSCaroline Tice &lldb_private::EmulateInstruction::ReadRegisterFrame,
3037c9dd3ceSCaroline Tice &lldb_private::EmulateInstruction::WriteRegisterFrame);
3047c9dd3ceSCaroline Tice }
305b9556accSGreg Clayton }
3067c9dd3ceSCaroline Tice return false;
3077c9dd3ceSCaroline Tice }
3087c9dd3ceSCaroline Tice
DumpEmulation(const char * triple)309b9c1b51eSKate Stone bool SBInstruction::DumpEmulation(const char *triple) {
3101755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, triple);
311baf5664fSJonas Devlieghere
3124a9d83a5SGreg Clayton lldb::InstructionSP inst_sp(GetOpaque());
313b9c1b51eSKate Stone if (inst_sp && triple) {
3147263f1bdSPavel Labath return inst_sp->DumpEmulation(HostInfo::GetAugmentedArchSpec(triple));
3157c9dd3ceSCaroline Tice }
3167c9dd3ceSCaroline Tice return false;
3177c9dd3ceSCaroline Tice }
3187c9dd3ceSCaroline Tice
TestEmulation(lldb::SBStream & output_stream,const char * test_file)319b9c1b51eSKate Stone bool SBInstruction::TestEmulation(lldb::SBStream &output_stream,
320b9c1b51eSKate Stone const char *test_file) {
3211755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, output_stream, test_file);
322baf5664fSJonas Devlieghere
3234a9d83a5SGreg Clayton if (!m_opaque_sp)
324b9c1b51eSKate Stone SetOpaque(lldb::DisassemblerSP(),
325b9c1b51eSKate Stone lldb::InstructionSP(new PseudoInstruction()));
3263ac6711aSCaroline Tice
3274a9d83a5SGreg Clayton lldb::InstructionSP inst_sp(GetOpaque());
3284a9d83a5SGreg Clayton if (inst_sp)
3294a9d83a5SGreg Clayton return inst_sp->TestEmulation(output_stream.get(), test_file);
3304a9d83a5SGreg Clayton return false;
3313ac6711aSCaroline Tice }
332