diff --git a/lldb/bindings/interface/SBInstruction.i b/lldb/bindings/interface/SBInstruction.i --- a/lldb/bindings/interface/SBInstruction.i +++ b/lldb/bindings/interface/SBInstruction.i @@ -68,6 +68,9 @@ bool GetDescription (lldb::SBStream &description); + bool + GetDescription (lldb::SBStream &description, lldb::SBTarget target); + bool EmulateWithFrame (lldb::SBFrame &frame, uint32_t evaluate_options); diff --git a/lldb/include/lldb/API/SBInstruction.h b/lldb/include/lldb/API/SBInstruction.h --- a/lldb/include/lldb/API/SBInstruction.h +++ b/lldb/include/lldb/API/SBInstruction.h @@ -61,6 +61,8 @@ bool GetDescription(lldb::SBStream &description); + bool GetDescription(lldb::SBStream &s, lldb::SBTarget target); + bool EmulateWithFrame(lldb::SBFrame &frame, uint32_t evaluate_options); bool DumpEmulation(const char *triple); // triple is to specify the diff --git a/lldb/source/API/SBInstruction.cpp b/lldb/source/API/SBInstruction.cpp --- a/lldb/source/API/SBInstruction.cpp +++ b/lldb/source/API/SBInstruction.cpp @@ -256,6 +256,33 @@ return false; } +bool SBInstruction::GetDescription(lldb::SBStream &s, SBTarget target) { + LLDB_RECORD_METHOD(bool, SBInstruction, GetDescription, + (lldb::SBStream &, lldb::SBTarget), s, target); + + lldb::InstructionSP inst_sp(GetOpaque()); + if (inst_sp) { + ExecutionContext exe_ctx; + TargetSP target_sp(target.GetSP()); + std::unique_lock lock; + if (target_sp) { + lock = std::unique_lock(target_sp->GetAPIMutex()); + target_sp->CalculateExecutionContext(exe_ctx); + } + SymbolContext sc; + const Address &addr = inst_sp->GetAddress(); + ModuleSP module_sp(addr.GetModule()); + if (module_sp) + module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything, + sc); + FormatEntity::Entry format; + FormatEntity::Parse("${addr}:", format); + inst_sp->Dump(&s.ref(), 0, true, false, &exe_ctx, &sc, nullptr, &format, 0); + return true; + } + return false; +} + void SBInstruction::Print(FILE *outp) { LLDB_RECORD_METHOD(void, SBInstruction, Print, (FILE *), outp); FileSP out = std::make_shared(outp, /*take_ownership=*/false); @@ -369,6 +396,8 @@ LLDB_REGISTER_METHOD(bool, SBInstruction, CanSetBreakpoint, ()); LLDB_REGISTER_METHOD(bool, SBInstruction, GetDescription, (lldb::SBStream &)); + LLDB_REGISTER_METHOD(bool, SBInstruction, GetDescription, + (lldb::SBStream &, lldb::SBTarget)); LLDB_REGISTER_METHOD(void, SBInstruction, Print, (FILE *)); LLDB_REGISTER_METHOD(void, SBInstruction, Print, (SBFile)); LLDB_REGISTER_METHOD(void, SBInstruction, Print, (FileSP)); diff --git a/lldb/test/API/functionalities/disassemble/Makefile b/lldb/test/API/functionalities/disassemble/Makefile new file mode 100644 --- /dev/null +++ b/lldb/test/API/functionalities/disassemble/Makefile @@ -0,0 +1,2 @@ +CXX_SOURCES := main.cpp +include Makefile.rules diff --git a/lldb/test/API/functionalities/disassemble/TestDisassemble.py b/lldb/test/API/functionalities/disassemble/TestDisassemble.py new file mode 100644 --- /dev/null +++ b/lldb/test/API/functionalities/disassemble/TestDisassemble.py @@ -0,0 +1,26 @@ +""" +Test that description for instruction can print address if target is provided. +""" + +from lldbsuite.test.decorators import * +from lldbsuite.test.lldbtest import * +from lldbsuite.test import lldbutil + +class TestDisassembledInstructionPrint(TestBase): + mydir = TestBase.compute_mydir(__file__) + + def test(self): + + self.build() + exe = self.getBuildArtifact("a.out") + target = self.dbg.CreateTarget(exe) + self.assertTrue(target, VALID_TARGET) + self.runCmd("run", RUN_SUCCEEDED) + + thread = target.GetProcess().GetSelectedThread() + instructions = target.ReadInstructions(thread.GetFrameAtIndex(0).GetPCAddress(),1) + instr = instructions.GetInstructionAtIndex(0) + strm_instr = lldb.SBStream() + instr.GetDescription(strm_instr, target) + disasm_instr = strm_instr.GetData() + self.assertTrue(disasm_instr.startswith("0x")) diff --git a/lldb/test/API/functionalities/disassemble/main.cpp b/lldb/test/API/functionalities/disassemble/main.cpp new file mode 100644 --- /dev/null +++ b/lldb/test/API/functionalities/disassemble/main.cpp @@ -0,0 +1,6 @@ +#include +int main() { + // Break here + raise(SIGSEGV); + return 0; +}