[lldb/Plugins] Fix ScriptedThread IndexID reporting

When listing all the Scripted Threads of a ScriptedProcess, we can see that all
have the thread index set to 1. This is caused by the lldb_private::Thread
constructor, which sets the m_index_id member using the provided thread id `tid`.

Because the call to the super constructor is done before instantiating
the `ScriptedThreadInterface`, lldb can't fetch the thread id from the
script instance, so it uses `LLDB_INVALID_THREAD_ID` instead.

To mitigate this, this patch takes advantage of the `ScriptedThread::Create`
fallible constructor idiom to defer calling the `ScriptedThread` constructor
(and the `Thread` super constructor with it), until we can fetch a valid
thread id `tid` from the `ScriptedThreadInterface`.

rdar://87432065

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

Signed-off-by: Med Ismail Bennani <medismail.bennani@gmail.com>
This commit is contained in:
Med Ismail Bennani committed 2022-01-24 20:25:54 +01:00
1 parent cfa55bfe78
commit 45148bfe8a
5 files changed
+60 -44

No files matched your search

@@ -28,52 +28,60 @@ void ScriptedThread::CheckInterpreterAndScriptObject() const {
lldbassert(GetInterface() && "Invalid Scripted Thread Interface.");
}
ScriptedThread::ScriptedThread(ScriptedProcess &process, Status &error,
StructuredData::Generic *script_object)
: Thread(process, LLDB_INVALID_THREAD_ID), m_scripted_process(process),
m_scripted_thread_interface_sp(
m_scripted_process.GetInterface().CreateScriptedThreadInterface()) {
if (!process.IsValid()) {
error.SetErrorString("Invalid scripted process");
return;
}
llvm::Expected<std::shared_ptr<ScriptedThread>>
ScriptedThread::Create(ScriptedProcess &process,
StructuredData::Generic *script_object) {
if (!process.IsValid())
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"Invalid scripted process.");
process.CheckInterpreterAndScriptObject();
auto scripted_thread_interface = GetInterface();
if (!scripted_thread_interface) {
error.SetErrorString("Failed to get scripted thread interface.");
return;
}
auto scripted_thread_interface =
process.GetInterface().CreateScriptedThreadInterface();
if (!scripted_thread_interface)
return llvm::createStringError(
llvm::inconvertibleErrorCode(),
"Failed to create scripted thread interface.");
llvm::Optional<std::string> class_name =
process.GetInterface().GetScriptedThreadPluginName();
if (!class_name || class_name->empty()) {
error.SetErrorString("Failed to get scripted thread class name.");
return;
llvm::StringRef thread_class_name;
if (!script_object) {
llvm::Optional<std::string> class_name =
process.GetInterface().GetScriptedThreadPluginName();
if (!class_name || class_name->empty())
return llvm::createStringError(
llvm::inconvertibleErrorCode(),
"Failed to get scripted thread class name.");
thread_class_name = *class_name;
}
ExecutionContext exe_ctx(process);
StructuredData::GenericSP owned_script_object_sp =
scripted_thread_interface->CreatePluginObject(
thread_class_name, exe_ctx,
process.m_scripted_process_info.GetArgsSP(), script_object);
m_script_object_sp = scripted_thread_interface->CreatePluginObject(
class_name->c_str(), exe_ctx, process.m_scripted_process_info.GetArgsSP(),
script_object);
if (!m_script_object_sp) {
error.SetErrorString("Failed to create script object");
return;
}
if (!m_script_object_sp->IsValid()) {
m_script_object_sp = nullptr;
error.SetErrorString("Created script object is invalid");
return;
}
if (!owned_script_object_sp)
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"Failed to create script object.");
if (!owned_script_object_sp->IsValid())
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"Created script object is invalid.");
lldb::tid_t tid = scripted_thread_interface->GetThreadID();
SetID(tid);
return std::make_shared<ScriptedThread>(process, scripted_thread_interface,
tid, owned_script_object_sp);
}
ScriptedThread::ScriptedThread(ScriptedProcess &process,
ScriptedThreadInterfaceSP interface_sp,
lldb::tid_t tid,
StructuredData::GenericSP script_object_sp)
: Thread(process, tid), m_scripted_process(process),
m_scripted_thread_interface_sp(interface_sp),
m_script_object_sp(script_object_sp) {}
ScriptedThread::~ScriptedThread() { DestroyThread(); }
const char *ScriptedThread::GetName() {