rdhindsa created this revision.
rdhindsa added reviewers: labath, clayborg.
rdhindsa requested review of this revision.
Herald added a project: LLDB.

Add another Getdescription function for SBInstruction.

When trying to print the instruction, existing GetDescription function doesn't 
have access to Execution Context. Hence, it prints instructions of the 
following form for the added test case:
libc.so.6[0x3bce1]: movq   0x108(%rsp), %rax

When a target is passed with this new API call, it is able to extract execution 
context and hence it prints in the following form:
0x7ffff7af6ce1:movq   0x108(%rsp), %rax


Repository:
  rG LLVM Github Monorepo

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);
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