https://github.com/kusmour updated https://github.com/llvm/llvm-project/pull/134339
>From 12de149a1d2067e1d942cddfcc8576fb6fe7cee5 Mon Sep 17 00:00:00 2001 From: Wanyi Ye <wa...@meta.com> Date: Thu, 3 Apr 2025 22:29:13 -0700 Subject: [PATCH 1/2] [lldb] Make SBProcess thread related actions listen to StopLocker --- lldb/source/API/SBProcess.cpp | 20 ++++++++++--------- .../tools/lldb-dap/attach/TestDAP_attach.py | 2 +- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lldb/source/API/SBProcess.cpp b/lldb/source/API/SBProcess.cpp index 23ea449b30cca..ba77b2beed5ea 100644 --- a/lldb/source/API/SBProcess.cpp +++ b/lldb/source/API/SBProcess.cpp @@ -193,10 +193,11 @@ uint32_t SBProcess::GetNumThreads() { if (process_sp) { Process::StopLocker stop_locker; - const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock()); - std::lock_guard<std::recursive_mutex> guard( - process_sp->GetTarget().GetAPIMutex()); - num_threads = process_sp->GetThreadList().GetSize(can_update); + if (stop_locker.TryLock(&process_sp->GetRunLock())) { + std::lock_guard<std::recursive_mutex> guard( + process_sp->GetTarget().GetAPIMutex()); + num_threads = process_sp->GetThreadList().GetSize(); + } } return num_threads; @@ -393,11 +394,12 @@ SBThread SBProcess::GetThreadAtIndex(size_t index) { ProcessSP process_sp(GetSP()); if (process_sp) { Process::StopLocker stop_locker; - const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock()); - std::lock_guard<std::recursive_mutex> guard( - process_sp->GetTarget().GetAPIMutex()); - thread_sp = process_sp->GetThreadList().GetThreadAtIndex(index, can_update); - sb_thread.SetThread(thread_sp); + if (stop_locker.TryLock(&process_sp->GetRunLock())) { + std::lock_guard<std::recursive_mutex> guard( + process_sp->GetTarget().GetAPIMutex()); + thread_sp = process_sp->GetThreadList().GetThreadAtIndex(index, false); + sb_thread.SetThread(thread_sp); + } } return sb_thread; diff --git a/lldb/test/API/tools/lldb-dap/attach/TestDAP_attach.py b/lldb/test/API/tools/lldb-dap/attach/TestDAP_attach.py index 9df44cc454d5d..b9fbf2c8d14f9 100644 --- a/lldb/test/API/tools/lldb-dap/attach/TestDAP_attach.py +++ b/lldb/test/API/tools/lldb-dap/attach/TestDAP_attach.py @@ -1,5 +1,5 @@ """ -Test lldb-dap setBreakpoints request +Test lldb-dap attach request """ >From 811b06dc5421a1bc30abfc1a6383dfb40bd0a1f1 Mon Sep 17 00:00:00 2001 From: Wanyi Ye <wa...@meta.com> Date: Tue, 8 Apr 2025 22:52:19 -0700 Subject: [PATCH 2/2] [lldb-dap] Client expects initial threads request not empty --- lldb/tools/lldb-dap/DAP.h | 3 +++ .../Handler/ConfigurationDoneRequestHandler.cpp | 4 +++- .../lldb-dap/Handler/ThreadsRequestHandler.cpp | 8 +------- lldb/tools/lldb-dap/JSONUtils.cpp | 16 ++++++++++++++++ lldb/tools/lldb-dap/JSONUtils.h | 2 ++ 5 files changed, 25 insertions(+), 8 deletions(-) diff --git a/lldb/tools/lldb-dap/DAP.h b/lldb/tools/lldb-dap/DAP.h index fc43d988f3a09..feeb040546c76 100644 --- a/lldb/tools/lldb-dap/DAP.h +++ b/lldb/tools/lldb-dap/DAP.h @@ -207,6 +207,9 @@ struct DAP { /// The set of features supported by the connected client. llvm::DenseSet<ClientFeature> clientFeatures; + /// The initial thread list upon attaching + std::optional<llvm::json::Array> initial_thread_list; + /// Creates a new DAP sessions. /// /// \param[in] log diff --git a/lldb/tools/lldb-dap/Handler/ConfigurationDoneRequestHandler.cpp b/lldb/tools/lldb-dap/Handler/ConfigurationDoneRequestHandler.cpp index cd120e1fdfaba..4d68a3c8d128f 100644 --- a/lldb/tools/lldb-dap/Handler/ConfigurationDoneRequestHandler.cpp +++ b/lldb/tools/lldb-dap/Handler/ConfigurationDoneRequestHandler.cpp @@ -52,8 +52,10 @@ void ConfigurationDoneRequestHandler::operator()( dap.configuration_done_sent = true; if (dap.stop_at_entry) SendThreadStoppedEvent(dap); - else + else { + dap.initial_thread_list = GetThreads(dap); dap.target.GetProcess().Continue(); + } } } // namespace lldb_dap diff --git a/lldb/tools/lldb-dap/Handler/ThreadsRequestHandler.cpp b/lldb/tools/lldb-dap/Handler/ThreadsRequestHandler.cpp index 2b857f7f6a02b..b06b40d9ed2de 100644 --- a/lldb/tools/lldb-dap/Handler/ThreadsRequestHandler.cpp +++ b/lldb/tools/lldb-dap/Handler/ThreadsRequestHandler.cpp @@ -50,16 +50,10 @@ namespace lldb_dap { // } void ThreadsRequestHandler::operator()( const llvm::json::Object &request) const { - lldb::SBProcess process = dap.target.GetProcess(); llvm::json::Object response; FillResponse(request, response); - const uint32_t num_threads = process.GetNumThreads(); - llvm::json::Array threads; - for (uint32_t thread_idx = 0; thread_idx < num_threads; ++thread_idx) { - lldb::SBThread thread = process.GetThreadAtIndex(thread_idx); - threads.emplace_back(CreateThread(thread, dap.thread_format)); - } + llvm::json::Array threads = GetThreads(dap); if (threads.size() == 0) { response["success"] = llvm::json::Value(false); } diff --git a/lldb/tools/lldb-dap/JSONUtils.cpp b/lldb/tools/lldb-dap/JSONUtils.cpp index 7660403666150..cfc0306bb7793 100644 --- a/lldb/tools/lldb-dap/JSONUtils.cpp +++ b/lldb/tools/lldb-dap/JSONUtils.cpp @@ -819,6 +819,22 @@ llvm::json::Value CreateExtendedStackFrameLabel(lldb::SBThread &thread, {"presentationHint", "label"}}); } +llvm::json::Array GetThreads(DAP &dap) { + llvm::json::Array threads; + if (dap.initial_thread_list) { + threads = dap.initial_thread_list.value(); + dap.initial_thread_list.reset(); + return threads; + } + lldb::SBProcess process = dap.target.GetProcess(); + const uint32_t num_threads = process.GetNumThreads(); + for (uint32_t thread_idx = 0; thread_idx < num_threads; ++thread_idx) { + lldb::SBThread thread = process.GetThreadAtIndex(thread_idx); + threads.emplace_back(CreateThread(thread, dap.thread_format)); + } + return threads; +} + // "Thread": { // "type": "object", // "description": "A Thread", diff --git a/lldb/tools/lldb-dap/JSONUtils.h b/lldb/tools/lldb-dap/JSONUtils.h index da91797290ff0..e2e35d8c14586 100644 --- a/lldb/tools/lldb-dap/JSONUtils.h +++ b/lldb/tools/lldb-dap/JSONUtils.h @@ -392,6 +392,8 @@ llvm::json::Value CreateStackFrame(lldb::SBFrame &frame, llvm::json::Value CreateExtendedStackFrameLabel(lldb::SBThread &thread, lldb::SBFormat &format); +llvm::json::Array GetThreads(DAP &dap); + /// Create a "Thread" object for a LLDB thread object. /// /// This function will fill in the following keys in the returned _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits