Skip to content

Commit 621a5f7

Browse files
committed
[lldb-dap] Stop the process for the threads request
1 parent 12cf6d3 commit 621a5f7

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

lldb/tools/lldb-dap/Handler/ThreadsRequestHandler.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "EventHelper.h"
1111
#include "JSONUtils.h"
1212
#include "RequestHandler.h"
13+
#include "lldb/API/SBError.h"
1314

1415
namespace lldb_dap {
1516

@@ -54,15 +55,34 @@ void ThreadsRequestHandler::operator()(
5455
llvm::json::Object response;
5556
FillResponse(request, response);
5657

58+
const auto state = process.GetState();
59+
const bool stop_and_resume =
60+
state != lldb::eStateCrashed && state != lldb::eStateStopped;
61+
62+
if (stop_and_resume) {
63+
lldb::SBError error = dap.WaitForProcessToStop(1);
64+
if (error.Fail()) {
65+
SetError(response, error);
66+
dap.SendJSON(llvm::json::Value(std::move(response)));
67+
}
68+
}
69+
5770
const uint32_t num_threads = process.GetNumThreads();
5871
llvm::json::Array threads;
5972
for (uint32_t thread_idx = 0; thread_idx < num_threads; ++thread_idx) {
6073
lldb::SBThread thread = process.GetThreadAtIndex(thread_idx);
6174
threads.emplace_back(CreateThread(thread, dap.thread_format));
6275
}
63-
if (threads.size() == 0) {
64-
response["success"] = llvm::json::Value(false);
76+
77+
if (stop_and_resume) {
78+
lldb::SBError error = process.Continue();
79+
if (error.Fail()) {
80+
SetError(response, error);
81+
dap.SendJSON(llvm::json::Value(std::move(response)));
82+
}
6583
}
84+
85+
response["success"] = llvm::json::Value(threads.size() != 0);
6686
llvm::json::Object body;
6787
body.try_emplace("threads", std::move(threads));
6888
response.try_emplace("body", std::move(body));

lldb/tools/lldb-dap/JSONUtils.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,15 @@ void FillResponse(const llvm::json::Object &request,
268268
response.try_emplace("success", true);
269269
}
270270

271+
void SetError(llvm::json::Object &response, lldb::SBError error) {
272+
assert(error.Fail());
273+
274+
response["success"] = llvm::json::Value(error.Success());
275+
const char *error_cstr = error.GetCString();
276+
if (error_cstr && error_cstr[0])
277+
EmplaceSafeString(response, "message", std::string(error_cstr));
278+
}
279+
271280
// "Scope": {
272281
// "type": "object",
273282
// "description": "A Scope is a named container for variables. Optionally

lldb/tools/lldb-dap/JSONUtils.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "DAPForward.h"
1313
#include "Protocol/ProtocolTypes.h"
1414
#include "lldb/API/SBCompileUnit.h"
15+
#include "lldb/API/SBError.h"
1516
#include "lldb/API/SBFileSpec.h"
1617
#include "lldb/API/SBFormat.h"
1718
#include "lldb/API/SBLineEntry.h"
@@ -197,6 +198,15 @@ GetStringMap(const llvm::json::Object &obj, llvm::StringRef key);
197198
void FillResponse(const llvm::json::Object &request,
198199
llvm::json::Object &response);
199200

201+
/// Set the response to failed and populate the message if there is one.
202+
///
203+
/// \param[in,out] response
204+
/// An llvm::json::Object object that will be populated.
205+
///
206+
/// \param[in] error
207+
/// A LLDB error in the failed state.
208+
void SetError(llvm::json::Object &response, lldb::SBError error);
209+
200210
/// Converts \a bp to a JSON value and appends the first valid location to the
201211
/// \a breakpoints array.
202212
///

0 commit comments

Comments
 (0)