Skip to content

Commit b71343f

Browse files
committed
[lldb][lldb-dap] replicate existing functionality verbatim
1 parent 437c69f commit b71343f

File tree

9 files changed

+97
-19
lines changed

9 files changed

+97
-19
lines changed

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -514,9 +514,7 @@ lldb::SBThread DAP::GetLLDBThread(const llvm::json::Object &arguments) {
514514
return target.GetProcess().GetThreadByID(tid);
515515
}
516516

517-
lldb::SBFrame DAP::GetLLDBFrame(const llvm::json::Object &arguments) {
518-
const uint64_t frame_id =
519-
GetInteger<uint64_t>(arguments, "frameId").value_or(UINT64_MAX);
517+
lldb::SBFrame DAP::GetLLDBFrame(uint64_t frame_id) {
520518
lldb::SBProcess process = target.GetProcess();
521519
// Upper 32 bits is the thread index ID
522520
lldb::SBThread thread =

lldb/tools/lldb-dap/DAP.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ struct DAP {
275275
lldb::SBThread GetLLDBThread(lldb::tid_t id);
276276
lldb::SBThread GetLLDBThread(const llvm::json::Object &arguments);
277277

278-
lldb::SBFrame GetLLDBFrame(const llvm::json::Object &arguments);
278+
lldb::SBFrame GetLLDBFrame(uint64_t frame_id);
279279

280280
llvm::json::Value CreateTopLevelScopes();
281281

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ void CompletionsRequestHandler::operator()(
136136
const auto *arguments = request.getObject("arguments");
137137

138138
// If we have a frame, try to set the context for variable completions.
139-
lldb::SBFrame frame = dap.GetLLDBFrame(*arguments);
139+
const uint64_t frame_id =
140+
GetInteger<uint64_t>(*arguments, "frameId").value_or(UINT64_MAX);
141+
lldb::SBFrame frame = dap.GetLLDBFrame(frame_id);
140142
if (frame.IsValid()) {
141143
frame.GetThread().GetProcess().SetSelectedThread(frame.GetThread());
142144
frame.GetThread().SetSelectedFrame(frame.GetFrameID());

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ void DataBreakpointInfoRequestHandler::operator()(
118118
const auto variablesReference =
119119
GetInteger<uint64_t>(arguments, "variablesReference").value_or(0);
120120
llvm::StringRef name = GetString(arguments, "name").value_or("");
121-
lldb::SBFrame frame = dap.GetLLDBFrame(*arguments);
121+
122+
const uint64_t frame_id =
123+
GetInteger<uint64_t>(arguments, "frameId").value_or(UINT64_MAX);
124+
lldb::SBFrame frame = dap.GetLLDBFrame(frame_id);
122125
lldb::SBValue variable = dap.variables.FindVariable(variablesReference, name);
123126
std::string addr, size;
124127

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,10 @@ void EvaluateRequestHandler::operator()(
144144
FillResponse(request, response);
145145
llvm::json::Object body;
146146
const auto *arguments = request.getObject("arguments");
147-
lldb::SBFrame frame = dap.GetLLDBFrame(*arguments);
147+
148+
const uint64_t frame_id =
149+
GetInteger<uint64_t>(arguments, "frameId").value_or(UINT64_MAX);
150+
lldb::SBFrame frame = dap.GetLLDBFrame(frame_id);
148151
std::string expression =
149152
GetString(arguments, "expression").value_or("").str();
150153
const llvm::StringRef context = GetString(arguments, "context").value_or("");

lldb/tools/lldb-dap/Handler/RequestHandler.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ class PauseRequestHandler : public LegacyRequestHandler {
423423
void operator()(const llvm::json::Object &request) const override;
424424
};
425425

426-
class ScopesRequestHandler2 final
426+
class ScopesRequestHandler final
427427
: public RequestHandler<protocol::ScopesArguments,
428428
llvm::Expected<protocol::ScopesResponseBody>> {
429429
public:
@@ -434,10 +434,10 @@ class ScopesRequestHandler2 final
434434
Run(const protocol::ScopesArguments &args) const override;
435435
};
436436

437-
class ScopesRequestHandler : public LegacyRequestHandler {
437+
class ScopesRequestHandler2 : public LegacyRequestHandler {
438438
public:
439439
using LegacyRequestHandler::LegacyRequestHandler;
440-
static llvm::StringLiteral GetCommand() { return "scopes"; }
440+
static llvm::StringLiteral GetCommand() { return "scopesr"; }
441441
void operator()(const llvm::json::Object &request) const override;
442442
};
443443

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

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,85 @@ namespace lldb_dap {
6565
// }]
6666
// }
6767

68+
protocol::Scope CreateScope2(const llvm::StringRef name,
69+
int64_t variablesReference, int64_t namedVariables,
70+
bool expensive) {
71+
protocol::Scope scope;
72+
73+
scope.name = name;
74+
75+
// TODO: Support "arguments" scope. At the moment lldb-dap includes the
76+
// arguments into the "locals" scope.
77+
// add presentation hint;
78+
if (variablesReference == VARREF_LOCALS)
79+
scope.presentationHint = protocol::Scope::ePresentationHintLocals;
80+
else if (variablesReference == VARREF_REGS)
81+
scope.presentationHint = protocol::Scope::ePresentationHintRegisters;
82+
83+
scope.variablesReference = variablesReference;
84+
scope.namedVariables = namedVariables;
85+
scope.expensive = expensive;
86+
87+
return scope;
88+
}
89+
90+
static std::vector<protocol::Scope> CreateTopLevelScopes(DAP &dap) {
91+
std::vector<protocol::Scope> scopes;
92+
scopes.reserve(3);
93+
scopes.emplace_back(CreateScope2("Locals", VARREF_LOCALS,
94+
dap.variables.locals.GetSize(), false));
95+
scopes.emplace_back(CreateScope2("Globals", VARREF_GLOBALS,
96+
dap.variables.globals.GetSize(), false));
97+
scopes.emplace_back(CreateScope2("Registers", VARREF_REGS,
98+
dap.variables.registers.GetSize(), false));
99+
100+
return scopes;
101+
}
102+
68103
llvm::Expected<protocol::ScopesResponseBody>
69-
ScopesRequestHandler2::Run(const protocol::ScopesArguments &args) const {
70-
// lldb::SBFrame frame = dap.GetLLDBFrame()
71-
return llvm::createStringError("something");
104+
ScopesRequestHandler::Run(const protocol::ScopesArguments &args) const {
105+
lldb::SBFrame frame = dap.GetLLDBFrame(args.frameId);
106+
107+
// As the user selects different stack frames in the GUI, a "scopes" request
108+
// will be sent to the DAP. This is the only way we know that the user has
109+
// selected a frame in a thread. There are no other notifications that are
110+
// sent and VS code doesn't allow multiple frames to show variables
111+
// concurrently. If we select the thread and frame as the "scopes" requests
112+
// are sent, this allows users to type commands in the debugger console
113+
// with a backtick character to run lldb commands and these lldb commands
114+
// will now have the right context selected as they are run. If the user
115+
// types "`bt" into the debugger console and we had another thread selected
116+
// in the LLDB library, we would show the wrong thing to the user. If the
117+
// users switches threads with a lldb command like "`thread select 14", the
118+
// GUI will not update as there are no "event" notification packets that
119+
// allow us to change the currently selected thread or frame in the GUI that
120+
// I am aware of.
121+
if (frame.IsValid()) {
122+
frame.GetThread().GetProcess().SetSelectedThread(frame.GetThread());
123+
frame.GetThread().SetSelectedFrame(frame.GetFrameID());
124+
}
125+
dap.variables.locals = frame.GetVariables(/*arguments=*/true,
126+
/*locals=*/true,
127+
/*statics=*/false,
128+
/*in_scope_only=*/true);
129+
dap.variables.globals = frame.GetVariables(/*arguments=*/false,
130+
/*locals=*/false,
131+
/*statics=*/true,
132+
/*in_scope_only=*/true);
133+
dap.variables.registers = frame.GetRegisters();
134+
135+
return protocol::ScopesResponseBody{CreateTopLevelScopes(dap)};
72136
};
73-
void ScopesRequestHandler::operator()(const llvm::json::Object &request) const {
137+
void ScopesRequestHandler2::operator()(
138+
const llvm::json::Object &request) const {
74139
llvm::json::Object response;
75140
FillResponse(request, response);
76141
llvm::json::Object body;
77142
const auto *arguments = request.getObject("arguments");
78-
lldb::SBFrame frame = dap.GetLLDBFrame(*arguments);
143+
144+
const uint64_t frame_id =
145+
GetInteger<uint64_t>(arguments, "frameId").value_or(UINT64_MAX);
146+
lldb::SBFrame frame = dap.GetLLDBFrame(frame_id);
79147
// As the user selects different stack frames in the GUI, a "scopes" request
80148
// will be sent to the DAP. This is the only way we know that the user has
81149
// selected a frame in a thread. There are no other notifications that are

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ void StepInTargetsRequestHandler::operator()(
7474
const auto *arguments = request.getObject("arguments");
7575

7676
dap.step_in_targets.clear();
77-
lldb::SBFrame frame = dap.GetLLDBFrame(*arguments);
77+
78+
const uint64_t frame_id =
79+
GetInteger<uint64_t>(arguments, "frameId").value_or(UINT64_MAX);
80+
lldb::SBFrame frame = dap.GetLLDBFrame(frame_id);
7881
if (frame.IsValid()) {
7982
lldb::SBAddress pc_addr = frame.GetPCAddress();
8083
lldb::SBAddress line_end_addr =

lldb/tools/lldb-dap/Protocol/ProtocolRequests.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,12 @@ bool fromJSON(const llvm::json::Value &Params, ScopesArguments &SCA,
267267
}
268268

269269
llvm::json::Value toJSON(const ScopesResponseBody &SCR) {
270-
llvm::json::Array body;
270+
llvm::json::Array scopes;
271271
for (const Scope &scope : SCR.scopes) {
272-
body.emplace_back(toJSON(scope));
272+
scopes.emplace_back(toJSON(scope));
273273
}
274-
return body;
274+
275+
return llvm::json::Object{{"scopes", std::move(scopes)}};
275276
}
276277

277278
bool fromJSON(const json::Value &Params, SourceArguments &SA, json::Path P) {

0 commit comments

Comments
 (0)