Skip to content

[lldb-dap] Stop the process for the threads request #134456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions lldb/tools/lldb-dap/Handler/ThreadsRequestHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "EventHelper.h"
#include "JSONUtils.h"
#include "RequestHandler.h"
#include "lldb/API/SBError.h"

namespace lldb_dap {

Expand Down Expand Up @@ -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));
Expand Down
9 changes: 9 additions & 0 deletions lldb/tools/lldb-dap/JSONUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions lldb/tools/lldb-dap/JSONUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
///
Expand Down