Skip to content

Commit fd6b58b

Browse files
committed
[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 <[email protected]>
1 parent 3a083b5 commit fd6b58b

File tree

7 files changed

+36
-32
lines changed

7 files changed

+36
-32
lines changed

lldb/bindings/python/python-wrapper.swig

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -322,16 +322,10 @@ LLDBSwigPythonCreateScriptedProcess
322322

323323
PythonObject result = {};
324324
if (arg_info.get().max_positional_args == 2) {
325-
if (args_impl != nullptr) {
326-
error_string.assign("args passed, but __init__ does not take an args dictionary");
327-
Py_RETURN_NONE;
328-
}
329-
result = pfunc(target_arg, dict);
330-
} else if (arg_info.get().max_positional_args >= 3) {
331325
PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(new lldb::SBStructuredData(args_impl)));
332-
result = pfunc(target_arg, args_arg, dict);
326+
result = pfunc(target_arg, args_arg);
333327
} else {
334-
error_string.assign("wrong number of arguments in __init__, should be 2 or 3 (not including self)");
328+
error_string.assign("wrong number of arguments in __init__, should be 2 (not including self)");
335329
Py_RETURN_NONE;
336330
}
337331

@@ -345,7 +339,8 @@ LLDBSwigPythonCreateScriptedThread
345339
(
346340
const char *python_class_name,
347341
const char *session_dictionary_name,
348-
const lldb::TargetSP& target_sp,
342+
const lldb::ProcessSP& process_sp,
343+
lldb_private::StructuredDataImpl *args_impl,
349344
std::string &error_string
350345
)
351346
{
@@ -363,12 +358,12 @@ LLDBSwigPythonCreateScriptedThread
363358
return nullptr;
364359
}
365360

366-
// I do not want the SBTarget to be deallocated when going out of scope
361+
// I do not want the SBProcess to be deallocated when going out of scope
367362
// because python has ownership of it and will manage memory for this
368363
// object by itself
369-
PythonObject target_arg(PyRefType::Owned, SBTypeToSWIGWrapper(new lldb::SBTarget(target_sp)));
364+
PythonObject process_arg(PyRefType::Owned, SBTypeToSWIGWrapper(new lldb::SBProcess(process_sp)));
370365

371-
if (!target_arg.IsAllocated())
366+
if (!process_arg.IsAllocated())
372367
Py_RETURN_NONE;
373368

374369
llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo();
@@ -385,10 +380,11 @@ LLDBSwigPythonCreateScriptedThread
385380
}
386381

387382
PythonObject result = {};
388-
if (arg_info.get().max_positional_args == 1) {
389-
result = pfunc(target_arg);
383+
if (arg_info.get().max_positional_args == 2) {
384+
PythonObject args_arg(PyRefType::Owned, SBTypeToSWIGWrapper(new lldb::SBStructuredData(args_impl)));
385+
result = pfunc(process_arg, args_arg);
390386
} else {
391-
error_string.assign("wrong number of arguments in __init__, should be 2 or 3 (not including self)");
387+
error_string.assign("wrong number of arguments in __init__, should be 2 (not including self)");
392388
Py_RETURN_NONE;
393389
}
394390

lldb/examples/python/scripted_process/my_scripted_process.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ class MyScriptedThread(ScriptedThread):
9393
"gs":0x0000000000000000,
9494
}
9595

96-
def __init__(self, target):
97-
super().__init__(target)
96+
def __init__(self, process, args):
97+
super().__init__(process, args)
9898

9999
def get_thread_id(self) -> int:
100100
return 0x19

lldb/examples/python/scripted_process/scripted_process.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,18 +190,20 @@ class ScriptedThread:
190190
"""
191191

192192
@abstractmethod
193-
def __init__(self, target):
193+
def __init__(self, process, args):
194194
""" Construct a scripted thread.
195195
196196
Args:
197-
target (lldb.SBTarget): The target launching the scripted process.
197+
process (lldb.SBProcess): The scripted process owning this thread.
198198
args (lldb.SBStructuredData): A Dictionary holding arbitrary
199-
key/value pairs used by the scripted process.
199+
key/value pairs used by the scripted thread.
200200
"""
201201
self.target = None
202+
self.process = None
202203
self.args = None
203-
if isinstance(target, lldb.SBTarget) and target.IsValid():
204-
self.target = target
204+
if isinstance(process, lldb.SBProcess) and process.IsValid():
205+
self.process = process
206+
self.target = process.GetTarget()
205207

206208
self.id = None
207209
self.name = None

lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ extern "C" void *LLDBSwigPythonCreateScriptedProcess(
4848

4949
extern "C" void *LLDBSwigPythonCreateScriptedThread(
5050
const char *python_class_name, const char *session_dictionary_name,
51-
const lldb::TargetSP &target_sp, std::string &error_string);
51+
const lldb::ProcessSP &process_sp, StructuredDataImpl *args_impl,
52+
std::string &error_string);
5253

5354
extern "C" void *LLDBSWIGPython_CastPyObjectToSBData(void *data);
5455
extern "C" void *LLDBSWIGPython_CastPyObjectToSBError(void *data);

lldb/source/Plugins/ScriptInterpreter/Python/ScriptedThreadPythonInterface.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,20 @@ StructuredData::GenericSP ScriptedThreadPythonInterface::CreatePluginObject(
3636
if (class_name.empty())
3737
return {};
3838

39-
Locker py_lock(&m_interpreter, Locker::AcquireLock | Locker::NoSTDIN,
40-
Locker::FreeLock);
41-
39+
ProcessSP process_sp = exe_ctx.GetProcessSP();
40+
StructuredDataImpl *args_impl = nullptr;
41+
if (args_sp) {
42+
args_impl = new StructuredDataImpl();
43+
args_impl->SetObjectSP(args_sp);
44+
}
4245
std::string error_string;
4346

44-
TargetSP target_sp = exe_ctx.GetTargetSP();
47+
Locker py_lock(&m_interpreter, Locker::AcquireLock | Locker::NoSTDIN,
48+
Locker::FreeLock);
4549

4650
void *ret_val = LLDBSwigPythonCreateScriptedThread(
47-
class_name.str().c_str(), m_interpreter.GetDictionaryName(), target_sp,
48-
error_string);
51+
class_name.str().c_str(), m_interpreter.GetDictionaryName(), process_sp,
52+
args_impl, error_string);
4953

5054
if (!ret_val)
5155
return {};

lldb/test/API/functionalities/scripted_process/dummy_scripted_process.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ def get_scripted_thread_plugin(self):
4343

4444

4545
class DummyScriptedThread(ScriptedThread):
46-
def __init__(self, target):
47-
super().__init__(target)
46+
def __init__(self, process, args):
47+
super().__init__(process, args)
4848

4949
def get_thread_id(self) -> int:
5050
return 0x19

lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ extern "C" void *LLDBSwigPythonCreateScriptedProcess(
229229

230230
extern "C" void *LLDBSwigPythonCreateScriptedThread(
231231
const char *python_class_name, const char *session_dictionary_name,
232-
const lldb::TargetSP &target_sp, std::string &error_string) {
232+
const lldb::ProcessSP &process_sp, StructuredDataImpl *args_impl,
233+
std::string &error_string) {
233234
return nullptr;
234235
}
235236

0 commit comments

Comments
 (0)