[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@gmail.com>
This commit is contained in:
Med Ismail Bennani 2021-10-19 23:30:22 +00:00
parent ad0f7d3d4a
commit 738621d047
7 changed files with 36 additions and 32 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -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

View File

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

View File

@ -36,16 +36,20 @@ StructuredData::GenericSP ScriptedThreadPythonInterface::CreatePluginObject(
if (class_name.empty())
return {};
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;
Locker py_lock(&m_interpreter, Locker::AcquireLock | Locker::NoSTDIN,
Locker::FreeLock);
std::string error_string;
TargetSP target_sp = exe_ctx.GetTargetSP();
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 {};

View File

@ -43,8 +43,8 @@ class DummyScriptedProcess(ScriptedProcess):
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

View File

@ -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;
}