diff --git a/lldb/bindings/python/python-wrapper.swig b/lldb/bindings/python/python-wrapper.swig --- a/lldb/bindings/python/python-wrapper.swig +++ b/lldb/bindings/python/python-wrapper.swig @@ -322,16 +322,10 @@ 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 @@ ( 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 @@ 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 arg_info = pfunc.GetArgInfo(); @@ -385,10 +380,11 @@ } 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 --- a/lldb/examples/python/scripted_process/my_scripted_process.py +++ b/lldb/examples/python/scripted_process/my_scripted_process.py @@ -93,8 +93,8 @@ "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 --- a/lldb/examples/python/scripted_process/scripted_process.py +++ b/lldb/examples/python/scripted_process/scripted_process.py @@ -190,18 +190,20 @@ """ @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 --- a/lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h +++ b/lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h @@ -48,7 +48,8 @@ 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 --- a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptedThreadPythonInterface.cpp +++ b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptedThreadPythonInterface.cpp @@ -36,16 +36,20 @@ 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 --- 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 @@ 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 --- a/lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp +++ b/lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp @@ -229,7 +229,8 @@ 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; }