Skip to content

Commit 506b00f

Browse files
JDevlieghereSquallATF
authored andcommitted
[lldb-dap] Refactor custom & testing related request handlers (NFC) (llvm#128549)
Continuation of the work started in llvm#128262. Builds on top of llvm#128453.
1 parent 8a92015 commit 506b00f

File tree

6 files changed

+206
-131
lines changed

6 files changed

+206
-131
lines changed

lldb/tools/lldb-dap/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ add_lldb_tool(lldb-dap
3939

4040
Handler/AttachRequestHandler.cpp
4141
Handler/BreakpointLocationsHandler.cpp
42+
Handler/CompileUnitsRequestHandler.cpp
4243
Handler/CompletionsHandler.cpp
4344
Handler/ConfigurationDoneRequestHandler.cpp
4445
Handler/ContinueRequestHandler.cpp
@@ -47,11 +48,13 @@ add_lldb_tool(lldb-dap
4748
Handler/ExceptionInfoRequestHandler.cpp
4849
Handler/InitializeRequestHandler.cpp
4950
Handler/LaunchRequestHandler.cpp
51+
Handler/ModulesRequestHandler.cpp
5052
Handler/NextRequestHandler.cpp
5153
Handler/RequestHandler.cpp
5254
Handler/RestartRequestHandler.cpp
5355
Handler/StepInRequestHandler.cpp
5456
Handler/StepInTargetsRequestHandler.cpp
57+
Handler/TestGetTargetBreakpointsRequestHandler.cpp
5558
Handler/StepOutRequestHandler.cpp
5659

5760
LINK_LIBS
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//===-- CompileUnitsRequestHandler.cpp ------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "DAP.h"
10+
#include "EventHelper.h"
11+
#include "JSONUtils.h"
12+
#include "RequestHandler.h"
13+
14+
namespace lldb_dap {
15+
16+
// "compileUnitsRequest": {
17+
// "allOf": [ { "$ref": "#/definitions/Request" }, {
18+
// "type": "object",
19+
// "description": "Compile Unit request; value of command field is
20+
// 'compileUnits'.",
21+
// "properties": {
22+
// "command": {
23+
// "type": "string",
24+
// "enum": [ "compileUnits" ]
25+
// },
26+
// "arguments": {
27+
// "$ref": "#/definitions/compileUnitRequestArguments"
28+
// }
29+
// },
30+
// "required": [ "command", "arguments" ]
31+
// }]
32+
// },
33+
// "compileUnitsRequestArguments": {
34+
// "type": "object",
35+
// "description": "Arguments for 'compileUnits' request.",
36+
// "properties": {
37+
// "moduleId": {
38+
// "type": "string",
39+
// "description": "The ID of the module."
40+
// }
41+
// },
42+
// "required": [ "moduleId" ]
43+
// },
44+
// "compileUnitsResponse": {
45+
// "allOf": [ { "$ref": "#/definitions/Response" }, {
46+
// "type": "object",
47+
// "description": "Response to 'compileUnits' request.",
48+
// "properties": {
49+
// "body": {
50+
// "description": "Response to 'compileUnits' request. Array of
51+
// paths of compile units."
52+
// }
53+
// }
54+
// }]
55+
// }
56+
void CompileUnitsRequestHandler::operator()(const llvm::json::Object &request) {
57+
llvm::json::Object response;
58+
FillResponse(request, response);
59+
llvm::json::Object body;
60+
llvm::json::Array units;
61+
const auto *arguments = request.getObject("arguments");
62+
std::string module_id = std::string(GetString(arguments, "moduleId"));
63+
int num_modules = dap.target.GetNumModules();
64+
for (int i = 0; i < num_modules; i++) {
65+
auto curr_module = dap.target.GetModuleAtIndex(i);
66+
if (module_id == curr_module.GetUUIDString()) {
67+
int num_units = curr_module.GetNumCompileUnits();
68+
for (int j = 0; j < num_units; j++) {
69+
auto curr_unit = curr_module.GetCompileUnitAtIndex(j);
70+
units.emplace_back(CreateCompileUnit(curr_unit));
71+
}
72+
body.try_emplace("compileUnits", std::move(units));
73+
break;
74+
}
75+
}
76+
response.try_emplace("body", std::move(body));
77+
dap.SendJSON(llvm::json::Value(std::move(response)));
78+
}
79+
80+
} // namespace lldb_dap
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//===-- ModulesRequestHandler.cpp -----------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "DAP.h"
10+
#include "EventHelper.h"
11+
#include "JSONUtils.h"
12+
#include "RequestHandler.h"
13+
14+
namespace lldb_dap {
15+
16+
// "modulesRequest": {
17+
// "allOf": [ { "$ref": "#/definitions/Request" }, {
18+
// "type": "object",
19+
// "description": "Modules request; value of command field is
20+
// 'modules'.",
21+
// "properties": {
22+
// "command": {
23+
// "type": "string",
24+
// "enum": [ "modules" ]
25+
// },
26+
// },
27+
// "required": [ "command" ]
28+
// }]
29+
// },
30+
// "modulesResponse": {
31+
// "allOf": [ { "$ref": "#/definitions/Response" }, {
32+
// "type": "object",
33+
// "description": "Response to 'modules' request.",
34+
// "properties": {
35+
// "body": {
36+
// "description": "Response to 'modules' request. Array of
37+
// module objects."
38+
// }
39+
// }
40+
// }]
41+
// }
42+
void ModulesRequestHandler::operator()(const llvm::json::Object &request) {
43+
llvm::json::Object response;
44+
FillResponse(request, response);
45+
46+
llvm::json::Array modules;
47+
for (size_t i = 0; i < dap.target.GetNumModules(); i++) {
48+
lldb::SBModule module = dap.target.GetModuleAtIndex(i);
49+
modules.emplace_back(CreateModule(dap.target, module));
50+
}
51+
52+
llvm::json::Object body;
53+
body.try_emplace("modules", std::move(modules));
54+
response.try_emplace("body", std::move(body));
55+
dap.SendJSON(llvm::json::Value(std::move(response)));
56+
}
57+
58+
} // namespace lldb_dap

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,33 @@ class StepOutRequestHandler : public RequestHandler {
162162
void operator()(const llvm::json::Object &request) override;
163163
};
164164

165+
class CompileUnitsRequestHandler : public RequestHandler {
166+
public:
167+
using RequestHandler::RequestHandler;
168+
static llvm::StringLiteral getCommand() { return "compileUnits"; }
169+
void operator()(const llvm::json::Object &request) override;
170+
};
171+
172+
class ModulesRequestHandler : public RequestHandler {
173+
public:
174+
using RequestHandler::RequestHandler;
175+
static llvm::StringLiteral getCommand() { return "modules"; }
176+
void operator()(const llvm::json::Object &request) override;
177+
};
178+
179+
/// A request used in testing to get the details on all breakpoints that are
180+
/// currently set in the target. This helps us to test "setBreakpoints" and
181+
/// "setFunctionBreakpoints" requests to verify we have the correct set of
182+
/// breakpoints currently set in LLDB.
183+
class TestGetTargetBreakpointsRequestHandler : public RequestHandler {
184+
public:
185+
using RequestHandler::RequestHandler;
186+
static llvm::StringLiteral getCommand() {
187+
return "_testGetTargetBreakpoints";
188+
}
189+
void operator()(const llvm::json::Object &request) override;
190+
};
191+
165192
} // namespace lldb_dap
166193

167194
#endif
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===-- TestGetTargetBreakpointsRequestHandler.cpp ------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "DAP.h"
10+
#include "EventHelper.h"
11+
#include "JSONUtils.h"
12+
#include "RequestHandler.h"
13+
14+
namespace lldb_dap {
15+
16+
void TestGetTargetBreakpointsRequestHandler::operator()(
17+
const llvm::json::Object &request) {
18+
llvm::json::Object response;
19+
FillResponse(request, response);
20+
llvm::json::Array response_breakpoints;
21+
for (uint32_t i = 0; dap.target.GetBreakpointAtIndex(i).IsValid(); ++i) {
22+
auto bp = Breakpoint(dap, dap.target.GetBreakpointAtIndex(i));
23+
AppendBreakpoint(&bp, response_breakpoints);
24+
}
25+
llvm::json::Object body;
26+
body.try_emplace("breakpoints", std::move(response_breakpoints));
27+
response.try_emplace("body", std::move(body));
28+
dap.SendJSON(llvm::json::Value(std::move(response)));
29+
}
30+
31+
} // namespace lldb_dap

lldb/tools/lldb-dap/lldb-dap.cpp

Lines changed: 7 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -261,112 +261,6 @@ bool FillStackFrames(DAP &dap, lldb::SBThread &thread,
261261
return reached_end_of_stack;
262262
}
263263

264-
// "compileUnitsRequest": {
265-
// "allOf": [ { "$ref": "#/definitions/Request" }, {
266-
// "type": "object",
267-
// "description": "Compile Unit request; value of command field is
268-
// 'compileUnits'.",
269-
// "properties": {
270-
// "command": {
271-
// "type": "string",
272-
// "enum": [ "compileUnits" ]
273-
// },
274-
// "arguments": {
275-
// "$ref": "#/definitions/compileUnitRequestArguments"
276-
// }
277-
// },
278-
// "required": [ "command", "arguments" ]
279-
// }]
280-
// },
281-
// "compileUnitsRequestArguments": {
282-
// "type": "object",
283-
// "description": "Arguments for 'compileUnits' request.",
284-
// "properties": {
285-
// "moduleId": {
286-
// "type": "string",
287-
// "description": "The ID of the module."
288-
// }
289-
// },
290-
// "required": [ "moduleId" ]
291-
// },
292-
// "compileUnitsResponse": {
293-
// "allOf": [ { "$ref": "#/definitions/Response" }, {
294-
// "type": "object",
295-
// "description": "Response to 'compileUnits' request.",
296-
// "properties": {
297-
// "body": {
298-
// "description": "Response to 'compileUnits' request. Array of
299-
// paths of compile units."
300-
// }
301-
// }
302-
// }]
303-
// }
304-
void request_compileUnits(DAP &dap, const llvm::json::Object &request) {
305-
llvm::json::Object response;
306-
FillResponse(request, response);
307-
llvm::json::Object body;
308-
llvm::json::Array units;
309-
const auto *arguments = request.getObject("arguments");
310-
std::string module_id = std::string(GetString(arguments, "moduleId"));
311-
int num_modules = dap.target.GetNumModules();
312-
for (int i = 0; i < num_modules; i++) {
313-
auto curr_module = dap.target.GetModuleAtIndex(i);
314-
if (module_id == curr_module.GetUUIDString()) {
315-
int num_units = curr_module.GetNumCompileUnits();
316-
for (int j = 0; j < num_units; j++) {
317-
auto curr_unit = curr_module.GetCompileUnitAtIndex(j);
318-
units.emplace_back(CreateCompileUnit(curr_unit));
319-
}
320-
body.try_emplace("compileUnits", std::move(units));
321-
break;
322-
}
323-
}
324-
response.try_emplace("body", std::move(body));
325-
dap.SendJSON(llvm::json::Value(std::move(response)));
326-
}
327-
328-
// "modulesRequest": {
329-
// "allOf": [ { "$ref": "#/definitions/Request" }, {
330-
// "type": "object",
331-
// "description": "Modules request; value of command field is
332-
// 'modules'.",
333-
// "properties": {
334-
// "command": {
335-
// "type": "string",
336-
// "enum": [ "modules" ]
337-
// },
338-
// },
339-
// "required": [ "command" ]
340-
// }]
341-
// },
342-
// "modulesResponse": {
343-
// "allOf": [ { "$ref": "#/definitions/Response" }, {
344-
// "type": "object",
345-
// "description": "Response to 'modules' request.",
346-
// "properties": {
347-
// "body": {
348-
// "description": "Response to 'modules' request. Array of
349-
// module objects."
350-
// }
351-
// }
352-
// }]
353-
// }
354-
void request_modules(DAP &dap, const llvm::json::Object &request) {
355-
llvm::json::Object response;
356-
FillResponse(request, response);
357-
358-
llvm::json::Array modules;
359-
for (size_t i = 0; i < dap.target.GetNumModules(); i++) {
360-
lldb::SBModule module = dap.target.GetModuleAtIndex(i);
361-
modules.emplace_back(CreateModule(dap.target, module));
362-
}
363-
364-
llvm::json::Object body;
365-
body.try_emplace("modules", std::move(modules));
366-
response.try_emplace("body", std::move(body));
367-
dap.SendJSON(llvm::json::Value(std::move(response)));
368-
}
369-
370264
// "PauseRequest": {
371265
// "allOf": [ { "$ref": "#/definitions/Request" }, {
372266
// "type": "object",
@@ -2187,25 +2081,6 @@ void request_readMemory(DAP &dap, const llvm::json::Object &request) {
21872081
dap.SendJSON(llvm::json::Value(std::move(response)));
21882082
}
21892083

2190-
// A request used in testing to get the details on all breakpoints that are
2191-
// currently set in the target. This helps us to test "setBreakpoints" and
2192-
// "setFunctionBreakpoints" requests to verify we have the correct set of
2193-
// breakpoints currently set in LLDB.
2194-
void request__testGetTargetBreakpoints(DAP &dap,
2195-
const llvm::json::Object &request) {
2196-
llvm::json::Object response;
2197-
FillResponse(request, response);
2198-
llvm::json::Array response_breakpoints;
2199-
for (uint32_t i = 0; dap.target.GetBreakpointAtIndex(i).IsValid(); ++i) {
2200-
auto bp = Breakpoint(dap, dap.target.GetBreakpointAtIndex(i));
2201-
AppendBreakpoint(&bp, response_breakpoints);
2202-
}
2203-
llvm::json::Object body;
2204-
body.try_emplace("breakpoints", std::move(response_breakpoints));
2205-
response.try_emplace("body", std::move(body));
2206-
dap.SendJSON(llvm::json::Value(std::move(response)));
2207-
}
2208-
22092084
// "SetInstructionBreakpointsRequest": {
22102085
// "allOf": [
22112086
// {"$ref": "#/definitions/Request"},
@@ -2456,6 +2331,13 @@ void RegisterRequestCallbacks(DAP &dap) {
24562331
dap.RegisterRequest<StepInTargetsRequestHandler>();
24572332
dap.RegisterRequest<StepOutRequestHandler>();
24582333

2334+
// Custom requests
2335+
dap.RegisterRequest<CompileUnitsRequestHandler>();
2336+
dap.RegisterRequest<ModulesRequestHandler>();
2337+
2338+
// Testing requests
2339+
dap.RegisterRequest<TestGetTargetBreakpointsRequestHandler>();
2340+
24592341
dap.RegisterRequestCallback("pause", request_pause);
24602342
dap.RegisterRequestCallback("scopes", request_scopes);
24612343
dap.RegisterRequestCallback("setBreakpoints", request_setBreakpoints);
@@ -2475,12 +2357,6 @@ void RegisterRequestCallbacks(DAP &dap) {
24752357
dap.RegisterRequestCallback("readMemory", request_readMemory);
24762358
dap.RegisterRequestCallback("setInstructionBreakpoints",
24772359
request_setInstructionBreakpoints);
2478-
// Custom requests
2479-
dap.RegisterRequestCallback("compileUnits", request_compileUnits);
2480-
dap.RegisterRequestCallback("modules", request_modules);
2481-
// Testing requests
2482-
dap.RegisterRequestCallback("_testGetTargetBreakpoints",
2483-
request__testGetTargetBreakpoints);
24842360
}
24852361

24862362
} // anonymous namespace

0 commit comments

Comments
 (0)