[Lldb-commits] [lldb] c192803 - [lldb] Add a way to get a scripted process implementation from the SBAPI

Med Ismail Bennani via lldb-commits lldb-commits at lists.llvm.org
Fri Feb 3 12:29:14 PST 2023


Author: Med Ismail Bennani
Date: 2023-02-03T12:29:01-08:00
New Revision: c1928033047409f977b26ffc938d59188f1ced97

URL: https://github.com/llvm/llvm-project/commit/c1928033047409f977b26ffc938d59188f1ced97
DIFF: https://github.com/llvm/llvm-project/commit/c1928033047409f977b26ffc938d59188f1ced97.diff

LOG: [lldb] Add a way to get a scripted process implementation from the SBAPI

This patch introduces a new `GetScriptedImplementation` method to the
SBProcess class in the SBAPI. It will allow users of Scripted Processes to
fetch the scripted implementation object from to script interpreter to be
able to interact with it directly (without having to go through lldb).

This allows to user to perform action that are not specified in the
scripted process interface, like calling un-specified methods, but also
to enrich the implementation, by passing it complex objects.

Differential Revision: https://reviews.llvm.org/D143236

Signed-off-by: Med Ismail Bennani <medismail.bennani at gmail.com>

Added: 
    

Modified: 
    lldb/bindings/interface/SBProcess.i
    lldb/bindings/python/python-typemaps.swig
    lldb/include/lldb/API/SBDefines.h
    lldb/include/lldb/API/SBProcess.h
    lldb/include/lldb/Target/Process.h
    lldb/source/API/SBProcess.cpp
    lldb/source/Plugins/Process/scripted/ScriptedProcess.cpp
    lldb/source/Plugins/Process/scripted/ScriptedProcess.h
    lldb/test/API/functionalities/scripted_process/TestScriptedProcess.py
    lldb/test/API/functionalities/scripted_process/dummy_scripted_process.py
    lldb/test/API/python_api/process/TestProcessAPI.py

Removed: 
    


################################################################################
diff  --git a/lldb/bindings/interface/SBProcess.i b/lldb/bindings/interface/SBProcess.i
index 52f9fc7fa204b..0ef558459e84f 100644
--- a/lldb/bindings/interface/SBProcess.i
+++ b/lldb/bindings/interface/SBProcess.i
@@ -344,6 +344,12 @@ public:
     bool
     GetDescription (lldb::SBStream &description);
 
+    %feature("autodoc", "
+    Returns the implementation object of the process plugin if available. None
+    otherwise.") GetScriptedImplementation;
+    ScriptedObject
+    GetScriptedImplementation();
+
     %feature("autodoc", "
     Returns the process' extended crash information.") GetExtendedCrashInformation;
     lldb::SBStructuredData

diff  --git a/lldb/bindings/python/python-typemaps.swig b/lldb/bindings/python/python-typemaps.swig
index 11f68d59ae7be..b321cc24c9afe 100644
--- a/lldb/bindings/python/python-typemaps.swig
+++ b/lldb/bindings/python/python-typemaps.swig
@@ -54,6 +54,16 @@
   free((char *) $1);
 }
 
+%typemap(out) lldb::ScriptedObject {
+  $result = nullptr;
+  if (const void* impl = $1)
+    $result = (PyObject*) impl;
+  if (!$result) {
+    $result = Py_None;
+    Py_INCREF(Py_None);
+  }
+}
+
 %typemap(out) char** {
   int len;
   int i;

diff  --git a/lldb/include/lldb/API/SBDefines.h b/lldb/include/lldb/API/SBDefines.h
index 6833c0542c168..848480d4f5e01 100644
--- a/lldb/include/lldb/API/SBDefines.h
+++ b/lldb/include/lldb/API/SBDefines.h
@@ -110,6 +110,7 @@ class LLDB_API SBUnixSignals;
 typedef bool (*SBBreakpointHitCallback)(void *baton, SBProcess &process,
                                         SBThread &thread,
                                         lldb::SBBreakpointLocation &location);
+typedef void *ScriptedObject;
 }
 
 #endif // LLDB_API_SBDEFINES_H

diff  --git a/lldb/include/lldb/API/SBProcess.h b/lldb/include/lldb/API/SBProcess.h
index 49b3256869ed6..992f78290c465 100644
--- a/lldb/include/lldb/API/SBProcess.h
+++ b/lldb/include/lldb/API/SBProcess.h
@@ -423,6 +423,8 @@ class LLDB_API SBProcess {
   ///
   lldb::SBError DeallocateMemory(lldb::addr_t ptr);
 
+  lldb::ScriptedObject GetScriptedImplementation();
+
 protected:
   friend class SBAddress;
   friend class SBBreakpoint;

diff  --git a/lldb/include/lldb/Target/Process.h b/lldb/include/lldb/Target/Process.h
index ca7f4b545e311..9862b6e572122 100644
--- a/lldb/include/lldb/Target/Process.h
+++ b/lldb/include/lldb/Target/Process.h
@@ -2526,6 +2526,8 @@ void PruneThreadPlans();
   lldb::StructuredDataPluginSP
   GetStructuredDataPlugin(ConstString type_name) const;
 
+  virtual void *GetImplementation() { return nullptr; }
+
 protected:
   friend class Trace;
 

diff  --git a/lldb/source/API/SBProcess.cpp b/lldb/source/API/SBProcess.cpp
index 27593559bb3dd..1a7881ccb11f2 100644
--- a/lldb/source/API/SBProcess.cpp
+++ b/lldb/source/API/SBProcess.cpp
@@ -1262,3 +1262,9 @@ lldb::SBError SBProcess::DeallocateMemory(lldb::addr_t ptr) {
   }
   return sb_error;
 }
+
+ScriptedObject SBProcess::GetScriptedImplementation() {
+  LLDB_INSTRUMENT_VA(this);
+  ProcessSP process_sp(GetSP());
+  return (process_sp) ? process_sp->GetImplementation() : nullptr;
+}

diff  --git a/lldb/source/Plugins/Process/scripted/ScriptedProcess.cpp b/lldb/source/Plugins/Process/scripted/ScriptedProcess.cpp
index 58629a5406f6b..d7d87c793c4d1 100644
--- a/lldb/source/Plugins/Process/scripted/ScriptedProcess.cpp
+++ b/lldb/source/Plugins/Process/scripted/ScriptedProcess.cpp
@@ -512,3 +512,10 @@ void ScriptedProcess::UpdateQueueListIfNeeded() {
 ScriptedProcessInterface &ScriptedProcess::GetInterface() const {
   return m_interpreter->GetScriptedProcessInterface();
 }
+
+void *ScriptedProcess::GetImplementation() {
+  if (m_script_object_sp &&
+      m_script_object_sp->GetType() == eStructuredDataTypeGeneric)
+    return m_script_object_sp->GetAsGeneric()->GetValue();
+  return nullptr;
+}

diff  --git a/lldb/source/Plugins/Process/scripted/ScriptedProcess.h b/lldb/source/Plugins/Process/scripted/ScriptedProcess.h
index 6e13e68c48289..bf283d5bce5da 100644
--- a/lldb/source/Plugins/Process/scripted/ScriptedProcess.h
+++ b/lldb/source/Plugins/Process/scripted/ScriptedProcess.h
@@ -74,6 +74,8 @@ class ScriptedProcess : public Process {
 
   void UpdateQueueListIfNeeded() override;
 
+  void *GetImplementation() override;
+
 protected:
   ScriptedProcess(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp,
                   const ScriptedMetadata &scripted_metadata, Status &error);

diff  --git a/lldb/test/API/functionalities/scripted_process/TestScriptedProcess.py b/lldb/test/API/functionalities/scripted_process/TestScriptedProcess.py
index 9cb33fb832010..50ef5ffadd81a 100644
--- a/lldb/test/API/functionalities/scripted_process/TestScriptedProcess.py
+++ b/lldb/test/API/functionalities/scripted_process/TestScriptedProcess.py
@@ -10,6 +10,8 @@
 from lldbsuite.test import lldbutil
 from lldbsuite.test import lldbtest
 
+import dummy_scripted_process
+
 class ScriptedProcesTestCase(TestBase):
 
     NO_DEBUG_INFO_TESTCASE = True
@@ -118,6 +120,14 @@ def cleanup():
         self.assertEqual(process.GetProcessID(), 42)
         self.assertEqual(process.GetNumThreads(), 1)
 
+        py_impl = process.GetScriptedImplementation()
+        self.assertTrue(py_impl)
+        self.assertTrue(isinstance(py_impl, dummy_scripted_process.DummyScriptedProcess))
+        self.assertFalse(hasattr(py_impl, 'my_super_secret_member'))
+        py_impl.my_super_secret_member = 42
+        self.assertTrue(hasattr(py_impl, 'my_super_secret_member'))
+        self.assertEqual(py_impl.my_super_secret_method(), 42)
+
         addr = 0x500000000
         message = "Hello, world!"
         buff = process.ReadCStringFromMemory(addr, len(message) + 1, error)

diff  --git a/lldb/test/API/functionalities/scripted_process/dummy_scripted_process.py b/lldb/test/API/functionalities/scripted_process/dummy_scripted_process.py
index 83df1ad8cbbb3..05dfa17f3bbbc 100644
--- a/lldb/test/API/functionalities/scripted_process/dummy_scripted_process.py
+++ b/lldb/test/API/functionalities/scripted_process/dummy_scripted_process.py
@@ -43,6 +43,12 @@ def is_alive(self) -> bool:
     def get_scripted_thread_plugin(self):
         return DummyScriptedThread.__module__ + "." + DummyScriptedThread.__name__
 
+    def my_super_secret_method(self):
+        if hasattr(self, 'my_super_secret_member'):
+            return self.my_super_secret_member
+        else:
+            return None
+
 
 class DummyScriptedThread(ScriptedThread):
     def __init__(self, process, args):

diff  --git a/lldb/test/API/python_api/process/TestProcessAPI.py b/lldb/test/API/python_api/process/TestProcessAPI.py
index 3fad30a78a172..cf05335b23840 100644
--- a/lldb/test/API/python_api/process/TestProcessAPI.py
+++ b/lldb/test/API/python_api/process/TestProcessAPI.py
@@ -18,6 +18,18 @@ def setUp(self):
             "main.cpp",
             "// Set break point at this line and check variable 'my_char'.")
 
+    def test_scripted_implementation(self):
+        self.build()
+        exe = self.getBuildArtifact("a.out")
+
+        (target, process, _, _) = \
+            lldbutil.run_to_source_breakpoint(self, "Set break point",
+                                              lldb.SBFileSpec("main.cpp"))
+
+        self.assertTrue(process, PROCESS_IS_VALID)
+        self.assertEqual(process.GetScriptedImplementation(), None)
+
+
     def test_read_memory(self):
         """Test Python SBProcess.ReadMemory() API."""
         self.build()


        


More information about the lldb-commits mailing list