rdhindsa updated this revision to Diff 370660.
rdhindsa marked an inline comment as done.
rdhindsa added a comment.

Added LLDB_REGISTER_METHOD for GetDescription.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D109249/new/

https://reviews.llvm.org/D109249

Files:
  lldb/bindings/interface/SBInstruction.i
  lldb/include/lldb/API/SBInstruction.h
  lldb/source/API/SBInstruction.cpp
  lldb/test/API/functionalities/disassemble/Makefile
  lldb/test/API/functionalities/disassemble/TestDisassemble.py
  lldb/test/API/functionalities/disassemble/main.cpp

Index: lldb/test/API/functionalities/disassemble/main.cpp
===================================================================
--- /dev/null
+++ lldb/test/API/functionalities/disassemble/main.cpp
@@ -0,0 +1,6 @@
+#include <signal.h>
+int main() {
+  // Break here
+  raise(SIGSEGV);
+  return 0;
+}
Index: lldb/test/API/functionalities/disassemble/TestDisassemble.py
===================================================================
--- /dev/null
+++ 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"))
Index: lldb/test/API/functionalities/disassemble/Makefile
===================================================================
--- /dev/null
+++ lldb/test/API/functionalities/disassemble/Makefile
@@ -0,0 +1,2 @@
+CXX_SOURCES := main.cpp
+include Makefile.rules
Index: lldb/source/API/SBInstruction.cpp
===================================================================
--- lldb/source/API/SBInstruction.cpp
+++ 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<std::recursive_mutex> lock;
+    if (target_sp) {
+      lock = std::unique_lock<std::recursive_mutex>(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<NativeFile>(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));
Index: lldb/include/lldb/API/SBInstruction.h
===================================================================
--- lldb/include/lldb/API/SBInstruction.h
+++ 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
Index: lldb/bindings/interface/SBInstruction.i
===================================================================
--- lldb/bindings/interface/SBInstruction.i
+++ 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);
 
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to