From 45148bfe8aece6ca319dcc32351e20bba26b2ea7 Mon Sep 17 00:00:00 2001 From: Med Ismail Bennani Date: Tue, 18 Jan 2022 12:52:24 +0100 Subject: [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 --- .../Plugins/Process/scripted/ScriptedThread.cpp | 80 ++++++++++++---------- 1 file changed, 44 insertions(+), 36 deletions(-) (limited to 'lldb/source/Plugins/Process/scripted/ScriptedThread.cpp') diff --git a/lldb/source/Plugins/Process/scripted/ScriptedThread.cpp b/lldb/source/Plugins/Process/scripted/ScriptedThread.cpp index 14f4f99cf9c4..b6cbb62fd6e6 100644 --- a/lldb/source/Plugins/Process/scripted/ScriptedThread.cpp +++ b/lldb/source/Plugins/Process/scripted/ScriptedThread.cpp @@ -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> +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; - } - - llvm::Optional class_name = - process.GetInterface().GetScriptedThreadPluginName(); - if (!class_name || class_name->empty()) { - error.SetErrorString("Failed to get scripted thread class name."); - return; + auto scripted_thread_interface = + process.GetInterface().CreateScriptedThreadInterface(); + if (!scripted_thread_interface) + return llvm::createStringError( + llvm::inconvertibleErrorCode(), + "Failed to create scripted thread interface."); + + llvm::StringRef thread_class_name; + if (!script_object) { + llvm::Optional 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); - - 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; - } + StructuredData::GenericSP owned_script_object_sp = + scripted_thread_interface->CreatePluginObject( + thread_class_name, exe_ctx, + process.m_scripted_process_info.GetArgsSP(), script_object); + + 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(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() { -- cgit v1.2.3