Skip to content

Commit ad67315

Browse files
authored
[lldb-dap] Move the command plugins out of the DAP header (#140396)
Move the command plugins out of the DAP header and into their file. This PR also renames the classes from "RequestHandler" to "Command". Although they are implemented in terms of sending requests, they are not "handlers".
1 parent ec7a509 commit ad67315

File tree

6 files changed

+208
-177
lines changed

6 files changed

+208
-177
lines changed

lldb/tools/lldb-dap/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ add_public_tablegen_target(LLDBDAPOptionsTableGen)
88
add_lldb_library(lldbDAP
99
Breakpoint.cpp
1010
BreakpointBase.cpp
11+
CommandPlugins.cpp
1112
DAP.cpp
1213
DAPError.cpp
1314
DAPLog.cpp
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
//===-- CommandPlugins.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 "CommandPlugins.h"
10+
#include "Handler/ResponseHandler.h"
11+
#include "JSONUtils.h"
12+
#include "lldb/API/SBStream.h"
13+
14+
using namespace lldb_dap;
15+
16+
bool StartDebuggingCommand::DoExecute(lldb::SBDebugger debugger, char **command,
17+
lldb::SBCommandReturnObject &result) {
18+
// Command format like: `start-debugging <launch|attach> <configuration>`
19+
if (!command) {
20+
result.SetError("Invalid use of start-debugging, expected format "
21+
"`start-debugging <launch|attach> <configuration>`.");
22+
return false;
23+
}
24+
25+
if (!command[0] || llvm::StringRef(command[0]).empty()) {
26+
result.SetError("start-debugging request type missing.");
27+
return false;
28+
}
29+
30+
if (!command[1] || llvm::StringRef(command[1]).empty()) {
31+
result.SetError("start-debugging debug configuration missing.");
32+
return false;
33+
}
34+
35+
llvm::StringRef request{command[0]};
36+
std::string raw_configuration{command[1]};
37+
38+
llvm::Expected<llvm::json::Value> configuration =
39+
llvm::json::parse(raw_configuration);
40+
41+
if (!configuration) {
42+
llvm::Error err = configuration.takeError();
43+
std::string msg = "Failed to parse json configuration: " +
44+
llvm::toString(std::move(err)) + "\n\n" +
45+
raw_configuration;
46+
result.SetError(msg.c_str());
47+
return false;
48+
}
49+
50+
dap.SendReverseRequest<LogFailureResponseHandler>(
51+
"startDebugging",
52+
llvm::json::Object{{"request", request},
53+
{"configuration", std::move(*configuration)}});
54+
55+
result.SetStatus(lldb::eReturnStatusSuccessFinishNoResult);
56+
57+
return true;
58+
}
59+
60+
bool ReplModeCommand::DoExecute(lldb::SBDebugger debugger, char **command,
61+
lldb::SBCommandReturnObject &result) {
62+
// Command format like: `repl-mode <variable|command|auto>?`
63+
// If a new mode is not specified report the current mode.
64+
if (!command || llvm::StringRef(command[0]).empty()) {
65+
std::string mode;
66+
switch (dap.repl_mode) {
67+
case ReplMode::Variable:
68+
mode = "variable";
69+
break;
70+
case ReplMode::Command:
71+
mode = "command";
72+
break;
73+
case ReplMode::Auto:
74+
mode = "auto";
75+
break;
76+
}
77+
78+
result.Printf("lldb-dap repl-mode %s.\n", mode.c_str());
79+
result.SetStatus(lldb::eReturnStatusSuccessFinishResult);
80+
81+
return true;
82+
}
83+
84+
llvm::StringRef new_mode{command[0]};
85+
86+
if (new_mode == "variable") {
87+
dap.repl_mode = ReplMode::Variable;
88+
} else if (new_mode == "command") {
89+
dap.repl_mode = ReplMode::Command;
90+
} else if (new_mode == "auto") {
91+
dap.repl_mode = ReplMode::Auto;
92+
} else {
93+
lldb::SBStream error_message;
94+
error_message.Printf("Invalid repl-mode '%s'. Expected one of 'variable', "
95+
"'command' or 'auto'.\n",
96+
new_mode.data());
97+
result.SetError(error_message.GetData());
98+
return false;
99+
}
100+
101+
result.Printf("lldb-dap repl-mode %s set.\n", new_mode.data());
102+
result.SetStatus(lldb::eReturnStatusSuccessFinishNoResult);
103+
return true;
104+
}
105+
106+
/// Sends a DAP event with an optional body.
107+
///
108+
/// https://code.visualstudio.com/api/references/vscode-api#debug.onDidReceiveDebugSessionCustomEvent
109+
bool SendEventCommand::DoExecute(lldb::SBDebugger debugger, char **command,
110+
lldb::SBCommandReturnObject &result) {
111+
// Command format like: `send-event <name> <body>?`
112+
if (!command || !command[0] || llvm::StringRef(command[0]).empty()) {
113+
result.SetError("Not enough arguments found, expected format "
114+
"`lldb-dap send-event <name> <body>?`.");
115+
return false;
116+
}
117+
118+
llvm::StringRef name{command[0]};
119+
// Events that are stateful and should be handled by lldb-dap internally.
120+
const std::array internal_events{"breakpoint", "capabilities", "continued",
121+
"exited", "initialize", "loadedSource",
122+
"module", "process", "stopped",
123+
"terminated", "thread"};
124+
if (llvm::is_contained(internal_events, name)) {
125+
std::string msg =
126+
llvm::formatv("Invalid use of lldb-dap send-event, event \"{0}\" "
127+
"should be handled by lldb-dap internally.",
128+
name)
129+
.str();
130+
result.SetError(msg.c_str());
131+
return false;
132+
}
133+
134+
llvm::json::Object event(CreateEventObject(name));
135+
136+
if (command[1] && !llvm::StringRef(command[1]).empty()) {
137+
// See if we have unused arguments.
138+
if (command[2]) {
139+
result.SetError(
140+
"Additional arguments found, expected `lldb-dap send-event "
141+
"<name> <body>?`.");
142+
return false;
143+
}
144+
145+
llvm::StringRef raw_body{command[1]};
146+
147+
llvm::Expected<llvm::json::Value> body = llvm::json::parse(raw_body);
148+
149+
if (!body) {
150+
llvm::Error err = body.takeError();
151+
std::string msg = "Failed to parse custom event body: " +
152+
llvm::toString(std::move(err));
153+
result.SetError(msg.c_str());
154+
return false;
155+
}
156+
157+
event.try_emplace("body", std::move(*body));
158+
}
159+
160+
dap.SendJSON(llvm::json::Value(std::move(event)));
161+
result.SetStatus(lldb::eReturnStatusSuccessFinishNoResult);
162+
return true;
163+
}

lldb/tools/lldb-dap/CommandPlugins.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===-- CommandPlugins.h --------------------------------------------------===//
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+
#ifndef LLDB_TOOLS_LLDB_DAP_COMMANDPLUGINS_H
10+
#define LLDB_TOOLS_LLDB_DAP_COMMANDPLUGINS_H
11+
12+
#include "DAP.h"
13+
#include "lldb/API/SBCommandInterpreter.h"
14+
15+
namespace lldb_dap {
16+
17+
struct StartDebuggingCommand : public lldb::SBCommandPluginInterface {
18+
DAP &dap;
19+
explicit StartDebuggingCommand(DAP &d) : dap(d) {};
20+
bool DoExecute(lldb::SBDebugger debugger, char **command,
21+
lldb::SBCommandReturnObject &result) override;
22+
};
23+
24+
struct ReplModeCommand : public lldb::SBCommandPluginInterface {
25+
DAP &dap;
26+
explicit ReplModeCommand(DAP &d) : dap(d) {};
27+
bool DoExecute(lldb::SBDebugger debugger, char **command,
28+
lldb::SBCommandReturnObject &result) override;
29+
};
30+
31+
struct SendEventCommand : public lldb::SBCommandPluginInterface {
32+
DAP &dap;
33+
explicit SendEventCommand(DAP &d) : dap(d) {};
34+
bool DoExecute(lldb::SBDebugger debugger, char **command,
35+
lldb::SBCommandReturnObject &result) override;
36+
};
37+
38+
} // namespace lldb_dap
39+
40+
#endif

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 0 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,159 +1017,6 @@ lldb::SBError DAP::WaitForProcessToStop(std::chrono::seconds seconds) {
10171017
return error;
10181018
}
10191019

1020-
bool StartDebuggingRequestHandler::DoExecute(
1021-
lldb::SBDebugger debugger, char **command,
1022-
lldb::SBCommandReturnObject &result) {
1023-
// Command format like: `start-debugging <launch|attach> <configuration>`
1024-
if (!command) {
1025-
result.SetError("Invalid use of start-debugging, expected format "
1026-
"`start-debugging <launch|attach> <configuration>`.");
1027-
return false;
1028-
}
1029-
1030-
if (!command[0] || llvm::StringRef(command[0]).empty()) {
1031-
result.SetError("start-debugging request type missing.");
1032-
return false;
1033-
}
1034-
1035-
if (!command[1] || llvm::StringRef(command[1]).empty()) {
1036-
result.SetError("start-debugging debug configuration missing.");
1037-
return false;
1038-
}
1039-
1040-
llvm::StringRef request{command[0]};
1041-
std::string raw_configuration{command[1]};
1042-
1043-
llvm::Expected<llvm::json::Value> configuration =
1044-
llvm::json::parse(raw_configuration);
1045-
1046-
if (!configuration) {
1047-
llvm::Error err = configuration.takeError();
1048-
std::string msg = "Failed to parse json configuration: " +
1049-
llvm::toString(std::move(err)) + "\n\n" +
1050-
raw_configuration;
1051-
result.SetError(msg.c_str());
1052-
return false;
1053-
}
1054-
1055-
dap.SendReverseRequest<LogFailureResponseHandler>(
1056-
"startDebugging",
1057-
llvm::json::Object{{"request", request},
1058-
{"configuration", std::move(*configuration)}});
1059-
1060-
result.SetStatus(lldb::eReturnStatusSuccessFinishNoResult);
1061-
1062-
return true;
1063-
}
1064-
1065-
bool ReplModeRequestHandler::DoExecute(lldb::SBDebugger debugger,
1066-
char **command,
1067-
lldb::SBCommandReturnObject &result) {
1068-
// Command format like: `repl-mode <variable|command|auto>?`
1069-
// If a new mode is not specified report the current mode.
1070-
if (!command || llvm::StringRef(command[0]).empty()) {
1071-
std::string mode;
1072-
switch (dap.repl_mode) {
1073-
case ReplMode::Variable:
1074-
mode = "variable";
1075-
break;
1076-
case ReplMode::Command:
1077-
mode = "command";
1078-
break;
1079-
case ReplMode::Auto:
1080-
mode = "auto";
1081-
break;
1082-
}
1083-
1084-
result.Printf("lldb-dap repl-mode %s.\n", mode.c_str());
1085-
result.SetStatus(lldb::eReturnStatusSuccessFinishResult);
1086-
1087-
return true;
1088-
}
1089-
1090-
llvm::StringRef new_mode{command[0]};
1091-
1092-
if (new_mode == "variable") {
1093-
dap.repl_mode = ReplMode::Variable;
1094-
} else if (new_mode == "command") {
1095-
dap.repl_mode = ReplMode::Command;
1096-
} else if (new_mode == "auto") {
1097-
dap.repl_mode = ReplMode::Auto;
1098-
} else {
1099-
lldb::SBStream error_message;
1100-
error_message.Printf("Invalid repl-mode '%s'. Expected one of 'variable', "
1101-
"'command' or 'auto'.\n",
1102-
new_mode.data());
1103-
result.SetError(error_message.GetData());
1104-
return false;
1105-
}
1106-
1107-
result.Printf("lldb-dap repl-mode %s set.\n", new_mode.data());
1108-
result.SetStatus(lldb::eReturnStatusSuccessFinishNoResult);
1109-
return true;
1110-
}
1111-
1112-
// Sends a DAP event with an optional body.
1113-
//
1114-
// See
1115-
// https://code.visualstudio.com/api/references/vscode-api#debug.onDidReceiveDebugSessionCustomEvent
1116-
bool SendEventRequestHandler::DoExecute(lldb::SBDebugger debugger,
1117-
char **command,
1118-
lldb::SBCommandReturnObject &result) {
1119-
// Command format like: `send-event <name> <body>?`
1120-
if (!command || !command[0] || llvm::StringRef(command[0]).empty()) {
1121-
result.SetError("Not enough arguments found, expected format "
1122-
"`lldb-dap send-event <name> <body>?`.");
1123-
return false;
1124-
}
1125-
1126-
llvm::StringRef name{command[0]};
1127-
// Events that are stateful and should be handled by lldb-dap internally.
1128-
const std::array internal_events{"breakpoint", "capabilities", "continued",
1129-
"exited", "initialize", "loadedSource",
1130-
"module", "process", "stopped",
1131-
"terminated", "thread"};
1132-
if (llvm::is_contained(internal_events, name)) {
1133-
std::string msg =
1134-
llvm::formatv("Invalid use of lldb-dap send-event, event \"{0}\" "
1135-
"should be handled by lldb-dap internally.",
1136-
name)
1137-
.str();
1138-
result.SetError(msg.c_str());
1139-
return false;
1140-
}
1141-
1142-
llvm::json::Object event(CreateEventObject(name));
1143-
1144-
if (command[1] && !llvm::StringRef(command[1]).empty()) {
1145-
// See if we have unused arguments.
1146-
if (command[2]) {
1147-
result.SetError(
1148-
"Additional arguments found, expected `lldb-dap send-event "
1149-
"<name> <body>?`.");
1150-
return false;
1151-
}
1152-
1153-
llvm::StringRef raw_body{command[1]};
1154-
1155-
llvm::Expected<llvm::json::Value> body = llvm::json::parse(raw_body);
1156-
1157-
if (!body) {
1158-
llvm::Error err = body.takeError();
1159-
std::string msg = "Failed to parse custom event body: " +
1160-
llvm::toString(std::move(err));
1161-
result.SetError(msg.c_str());
1162-
return false;
1163-
}
1164-
1165-
event.try_emplace("body", std::move(*body));
1166-
}
1167-
1168-
dap.SendJSON(llvm::json::Value(std::move(event)));
1169-
result.SetStatus(lldb::eReturnStatusSuccessFinishNoResult);
1170-
return true;
1171-
}
1172-
11731020
void DAP::ConfigureSourceMaps() {
11741021
if (configuration.sourceMap.empty() && configuration.sourcePath.empty())
11751022
return;

0 commit comments

Comments
 (0)