[Lldb-commits] [lldb] 738621d - [lldb/bindings] Change ScriptedThread initializer parameters

Med Ismail Bennani via lldb-commits lldb-commits at lists.llvm.org
Wed Nov 10 08:43:51 PST 2021


Author: Med Ismail Bennani
Date: 2021-11-10T17:43:28+01:00
New Revision: 738621d047f2c14482509b39f8307967a91e7586

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

LOG: [lldb/bindings] Change ScriptedThread initializer parameters

This patch changes the `ScriptedThread` initializer in couple of ways:
- It replaces the `SBTarget` parameter by a `SBProcess` (pointing to the
  `ScriptedProcess` that "owns" the `ScriptedThread`).
- It adds a reference to the `ScriptedProcessInfo` Dictionary, to pass
  arbitrary user-input to the `ScriptedThread`.

This patch also fixes the SWIG bindings methods that call the
`ScriptedProcess` and `ScriptedThread` initializers by passing all the
arguments to the appropriate `PythonCallable` object.

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

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

Added: 
    

Modified: 
    lldb/bindings/python/python-wrapper.swig
    lldb/examples/python/scripted_process/my_scripted_process.py
    lldb/examples/python/scripted_process/scripted_process.py
    lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
    lldb/source/Plugins/ScriptInterpreter/Python/ScriptedThreadPythonInterface.cpp
    lldb/test/API/functionalities/scripted_process/dummy_scripted_process.py
    lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/bindings/python/python-wrapper.swig b/lldb/bindings/python/python-wrapper.swig
index 5a950e259e993..698d7d12cebca 100644
--- a/lldb/bindings/python/python-wrapper.swig
+++ b/lldb/bindings/python/python-wrapper.swig
@@ -322,16 +322,10 @@ LLDBSwigPythonCreateScriptedProcess
 
     PythonObject result = {};
     if (arg_info.get().max_positional_args == 2) {
-        if (args_impl != nullptr) {
-           error_string.assign("args passed, but __init__ does not take an args dictionary");
-           Py_RETURN_NONE;
-        }
-        result = pfunc(target_arg, dict);
-    } else if (arg_info.get().max_positional_args >= 3) {
         PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(new lldb::SBStructuredData(args_impl)));
-        result = pfunc(target_arg, args_arg, dict);
+        result = pfunc(target_arg, args_arg);
     } else {
-        error_string.assign("wrong number of arguments in __init__, should be 2 or 3 (not including self)");
+        error_string.assign("wrong number of arguments in __init__, should be 2 (not including self)");
         Py_RETURN_NONE;
     }
 
@@ -345,7 +339,8 @@ LLDBSwigPythonCreateScriptedThread
 (
     const char *python_class_name,
     const char *session_dictionary_name,
-    const lldb::TargetSP& target_sp,
+    const lldb::ProcessSP& process_sp,
+    lldb_private::StructuredDataImpl *args_impl,
     std::string &error_string
 )
 {
@@ -363,12 +358,12 @@ LLDBSwigPythonCreateScriptedThread
         return nullptr;
     }
 
-    // I do not want the SBTarget to be deallocated when going out of scope
+    // I do not want the SBProcess to be deallocated when going out of scope
     // because python has ownership of it and will manage memory for this
     // object by itself
-    PythonObject target_arg(PyRefType::Owned, SBTypeToSWIGWrapper(new lldb::SBTarget(target_sp)));
+    PythonObject process_arg(PyRefType::Owned, SBTypeToSWIGWrapper(new lldb::SBProcess(process_sp)));
 
-    if (!target_arg.IsAllocated())
+    if (!process_arg.IsAllocated())
         Py_RETURN_NONE;
 
     llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo();
@@ -385,10 +380,11 @@ LLDBSwigPythonCreateScriptedThread
     }
 
     PythonObject result = {};
-    if (arg_info.get().max_positional_args == 1) {
-        result = pfunc(target_arg);
+    if (arg_info.get().max_positional_args == 2) {
+        PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(new lldb::SBStructuredData(args_impl)));
+        result = pfunc(process_arg, args_arg);
     } else {
-        error_string.assign("wrong number of arguments in __init__, should be 2 or 3 (not including self)");
+        error_string.assign("wrong number of arguments in __init__, should be 2 (not including self)");
         Py_RETURN_NONE;
     }
 

diff  --git a/lldb/examples/python/scripted_process/my_scripted_process.py b/lldb/examples/python/scripted_process/my_scripted_process.py
index 224b772845a7f..5e7c10b91533d 100644
--- a/lldb/examples/python/scripted_process/my_scripted_process.py
+++ b/lldb/examples/python/scripted_process/my_scripted_process.py
@@ -93,8 +93,8 @@ class MyScriptedThread(ScriptedThread):
         "gs":0x0000000000000000,
     }
 
-    def __init__(self, target):
-        super().__init__(target)
+    def __init__(self, process, args):
+        super().__init__(process, args)
 
     def get_thread_id(self) -> int:
         return 0x19

diff  --git a/lldb/examples/python/scripted_process/scripted_process.py b/lldb/examples/python/scripted_process/scripted_process.py
index 5869ca03e20bd..43ee2d6fffb27 100644
--- a/lldb/examples/python/scripted_process/scripted_process.py
+++ b/lldb/examples/python/scripted_process/scripted_process.py
@@ -190,18 +190,20 @@ class ScriptedThread:
     """
 
     @abstractmethod
-    def __init__(self, target):
+    def __init__(self, process, args):
         """ Construct a scripted thread.
 
         Args:
-            target (lldb.SBTarget): The target launching the scripted process.
+            process (lldb.SBProcess): The scripted process owning this thread.
             args (lldb.SBStructuredData): A Dictionary holding arbitrary
-                key/value pairs used by the scripted process.
+                key/value pairs used by the scripted thread.
         """
         self.target = None
+        self.process = None
         self.args = None
-        if isinstance(target, lldb.SBTarget) and target.IsValid():
-            self.target = target
+        if isinstance(process, lldb.SBProcess) and process.IsValid():
+            self.process = process
+            self.target = process.GetTarget()
 
         self.id = None
         self.name = None

diff  --git a/lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h b/lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
index 81e25128b2c7d..798d947a0a7dc 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
@@ -48,7 +48,8 @@ extern "C" void *LLDBSwigPythonCreateScriptedProcess(
 
 extern "C" void *LLDBSwigPythonCreateScriptedThread(
     const char *python_class_name, const char *session_dictionary_name,
-    const lldb::TargetSP &target_sp, std::string &error_string);
+    const lldb::ProcessSP &process_sp, StructuredDataImpl *args_impl,
+    std::string &error_string);
 
 extern "C" void *LLDBSWIGPython_CastPyObjectToSBData(void *data);
 extern "C" void *LLDBSWIGPython_CastPyObjectToSBError(void *data);

diff  --git a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptedThreadPythonInterface.cpp b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptedThreadPythonInterface.cpp
index 4d6903d567c52..d2c28bc426ee6 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptedThreadPythonInterface.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptedThreadPythonInterface.cpp
@@ -36,16 +36,20 @@ StructuredData::GenericSP ScriptedThreadPythonInterface::CreatePluginObject(
   if (class_name.empty())
     return {};
 
-  Locker py_lock(&m_interpreter, Locker::AcquireLock | Locker::NoSTDIN,
-                 Locker::FreeLock);
-
+  ProcessSP process_sp = exe_ctx.GetProcessSP();
+  StructuredDataImpl *args_impl = nullptr;
+  if (args_sp) {
+    args_impl = new StructuredDataImpl();
+    args_impl->SetObjectSP(args_sp);
+  }
   std::string error_string;
 
-  TargetSP target_sp = exe_ctx.GetTargetSP();
+  Locker py_lock(&m_interpreter, Locker::AcquireLock | Locker::NoSTDIN,
+                 Locker::FreeLock);
 
   void *ret_val = LLDBSwigPythonCreateScriptedThread(
-      class_name.str().c_str(), m_interpreter.GetDictionaryName(), target_sp,
-      error_string);
+      class_name.str().c_str(), m_interpreter.GetDictionaryName(), process_sp,
+      args_impl, error_string);
 
   if (!ret_val)
     return {};

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 31a52f28c6c00..d7f428d408457 100644
--- a/lldb/test/API/functionalities/scripted_process/dummy_scripted_process.py
+++ b/lldb/test/API/functionalities/scripted_process/dummy_scripted_process.py
@@ -43,8 +43,8 @@ def get_scripted_thread_plugin(self):
 
 
 class DummyScriptedThread(ScriptedThread):
-    def __init__(self, target):
-        super().__init__(target)
+    def __init__(self, process, args):
+        super().__init__(process, args)
 
     def get_thread_id(self) -> int:
         return 0x19

diff  --git a/lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp b/lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp
index ea6d6be7dbd5e..7a4b6328a2f54 100644
--- a/lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp
+++ b/lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp
@@ -229,7 +229,8 @@ extern "C" void *LLDBSwigPythonCreateScriptedProcess(
 
 extern "C" void *LLDBSwigPythonCreateScriptedThread(
     const char *python_class_name, const char *session_dictionary_name,
-    const lldb::TargetSP &target_sp, std::string &error_string) {
+    const lldb::ProcessSP &process_sp, StructuredDataImpl *args_impl,
+    std::string &error_string) {
   return nullptr;
 }
 


        


More information about the lldb-commits mailing list