https://github.com/JDevlieghere created https://github.com/llvm/llvm-project/pull/134456
None >From 621a5f78b72232e3f76dc2c6162bffc8051045ae Mon Sep 17 00:00:00 2001 From: Jonas Devlieghere <jo...@devlieghere.com> Date: Fri, 4 Apr 2025 15:07:21 -0700 Subject: [PATCH] [lldb-dap] Stop the process for the threads request --- .../Handler/ThreadsRequestHandler.cpp | 24 +++++++++++++++++-- lldb/tools/lldb-dap/JSONUtils.cpp | 9 +++++++ lldb/tools/lldb-dap/JSONUtils.h | 10 ++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/lldb/tools/lldb-dap/Handler/ThreadsRequestHandler.cpp b/lldb/tools/lldb-dap/Handler/ThreadsRequestHandler.cpp index 2b857f7f6a02b..61d6fb286e261 100644 --- a/lldb/tools/lldb-dap/Handler/ThreadsRequestHandler.cpp +++ b/lldb/tools/lldb-dap/Handler/ThreadsRequestHandler.cpp @@ -10,6 +10,7 @@ #include "EventHelper.h" #include "JSONUtils.h" #include "RequestHandler.h" +#include "lldb/API/SBError.h" namespace lldb_dap { @@ -54,15 +55,34 @@ void ThreadsRequestHandler::operator()( llvm::json::Object response; FillResponse(request, response); + const auto state = process.GetState(); + const bool stop_and_resume = + state != lldb::eStateCrashed && state != lldb::eStateStopped; + + if (stop_and_resume) { + lldb::SBError error = dap.WaitForProcessToStop(1); + if (error.Fail()) { + SetError(response, error); + dap.SendJSON(llvm::json::Value(std::move(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)); } - if (threads.size() == 0) { - response["success"] = llvm::json::Value(false); + + if (stop_and_resume) { + lldb::SBError error = process.Continue(); + if (error.Fail()) { + SetError(response, error); + dap.SendJSON(llvm::json::Value(std::move(response))); + } } + + response["success"] = llvm::json::Value(threads.size() != 0); llvm::json::Object body; body.try_emplace("threads", std::move(threads)); response.try_emplace("body", std::move(body)); diff --git a/lldb/tools/lldb-dap/JSONUtils.cpp b/lldb/tools/lldb-dap/JSONUtils.cpp index 7660403666150..17b9e343a48fa 100644 --- a/lldb/tools/lldb-dap/JSONUtils.cpp +++ b/lldb/tools/lldb-dap/JSONUtils.cpp @@ -268,6 +268,15 @@ void FillResponse(const llvm::json::Object &request, response.try_emplace("success", true); } +void SetError(llvm::json::Object &response, lldb::SBError error) { + assert(error.Fail()); + + response["success"] = llvm::json::Value(error.Success()); + const char *error_cstr = error.GetCString(); + if (error_cstr && error_cstr[0]) + EmplaceSafeString(response, "message", std::string(error_cstr)); +} + // "Scope": { // "type": "object", // "description": "A Scope is a named container for variables. Optionally diff --git a/lldb/tools/lldb-dap/JSONUtils.h b/lldb/tools/lldb-dap/JSONUtils.h index da91797290ff0..e9cd75e884896 100644 --- a/lldb/tools/lldb-dap/JSONUtils.h +++ b/lldb/tools/lldb-dap/JSONUtils.h @@ -12,6 +12,7 @@ #include "DAPForward.h" #include "Protocol/ProtocolTypes.h" #include "lldb/API/SBCompileUnit.h" +#include "lldb/API/SBError.h" #include "lldb/API/SBFileSpec.h" #include "lldb/API/SBFormat.h" #include "lldb/API/SBLineEntry.h" @@ -197,6 +198,15 @@ GetStringMap(const llvm::json::Object &obj, llvm::StringRef key); void FillResponse(const llvm::json::Object &request, llvm::json::Object &response); +/// Set the response to failed and populate the message if there is one. +/// +/// \param[in,out] response +/// An llvm::json::Object object that will be populated. +/// +/// \param[in] error +/// A LLDB error in the failed state. +void SetError(llvm::json::Object &response, lldb::SBError error); + /// Converts \a bp to a JSON value and appends the first valid location to the /// \a breakpoints array. /// _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits