Skip to content

Commit 3f12155

Browse files
ashgtivogelsgesang
andauthored
[lldb-dap] Creating well defined structures for DAP messages. (#129155)
This adds a new `Protocol.{h,cpp}` for defining structured types that represent Debug Adapter Protocol messages. This adds static types to define well structure messages for the protocol. This iteration includes only the basic `Event`, `Request` and `Response` types. These types help simplify and improve the validation of messages and give us additional static type checks on the overall structure of DAP messages, compared to today where we tend to use `llvm::json::Value` directly. In a follow-up patch I plan on adding more types as need to allow for incrementally migrating raw `llvm::json::Value` usage to well defined types. --------- Co-authored-by: Adrian Vogelsgesang <[email protected]>
1 parent 024362f commit 3f12155

File tree

3 files changed

+537
-0
lines changed

3 files changed

+537
-0
lines changed

lldb/tools/lldb-dap/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ add_lldb_tool(lldb-dap
3535
ProgressEvent.cpp
3636
RunInTerminal.cpp
3737
SourceBreakpoint.cpp
38+
Protocol.cpp
3839
Watchpoint.cpp
3940

4041
Handler/ResponseHandler.cpp

lldb/tools/lldb-dap/Protocol.cpp

Lines changed: 291 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,291 @@
1+
//===-- Protocol.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 "Protocol.h"
10+
#include "llvm/ADT/StringRef.h"
11+
#include "llvm/ADT/StringSwitch.h"
12+
#include "llvm/Support/ErrorHandling.h"
13+
#include "llvm/Support/JSON.h"
14+
#include <optional>
15+
#include <utility>
16+
17+
using namespace llvm;
18+
19+
static bool mapRaw(const json::Value &Params, StringLiteral Prop,
20+
std::optional<json::Value> &V, json::Path P) {
21+
const auto *O = Params.getAsObject();
22+
if (!O) {
23+
P.report("expected object");
24+
return false;
25+
}
26+
const json::Value *E = O->get(Prop);
27+
if (E)
28+
V = std::move(*E);
29+
return true;
30+
}
31+
32+
namespace lldb_dap {
33+
namespace protocol {
34+
35+
enum class MessageType { request, response, event };
36+
37+
bool fromJSON(const json::Value &Params, MessageType &M, json::Path P) {
38+
auto rawType = Params.getAsString();
39+
if (!rawType) {
40+
P.report("expected a string");
41+
return false;
42+
}
43+
std::optional<MessageType> type =
44+
StringSwitch<std::optional<MessageType>>(*rawType)
45+
.Case("request", MessageType::request)
46+
.Case("response", MessageType::response)
47+
.Case("event", MessageType::event)
48+
.Default(std::nullopt);
49+
if (!type) {
50+
P.report("unexpected value, expected 'request', 'response' or 'event'");
51+
return false;
52+
}
53+
M = *type;
54+
return true;
55+
}
56+
57+
json::Value toJSON(const Request &R) {
58+
json::Object Result{
59+
{"type", "request"},
60+
{"seq", R.seq},
61+
{"command", R.command},
62+
};
63+
64+
if (R.rawArguments)
65+
Result.insert({"arguments", R.rawArguments});
66+
67+
return std::move(Result);
68+
}
69+
70+
bool fromJSON(json::Value const &Params, Request &R, json::Path P) {
71+
json::ObjectMapper O(Params, P);
72+
if (!O)
73+
return false;
74+
75+
MessageType type;
76+
if (!O.map("type", type) || !O.map("command", R.command) ||
77+
!O.map("seq", R.seq))
78+
return false;
79+
80+
if (type != MessageType::request) {
81+
P.field("type").report("expected to be 'request'");
82+
return false;
83+
}
84+
85+
if (R.command.empty()) {
86+
P.field("command").report("expected to not be ''");
87+
return false;
88+
}
89+
90+
if (!R.seq) {
91+
P.field("seq").report("expected to not be '0'");
92+
return false;
93+
}
94+
95+
return mapRaw(Params, "arguments", R.rawArguments, P);
96+
}
97+
98+
json::Value toJSON(const Response &R) {
99+
json::Object Result{{"type", "response"},
100+
{"seq", 0},
101+
{"command", R.command},
102+
{"request_seq", R.request_seq},
103+
{"success", R.success}};
104+
105+
if (R.message) {
106+
assert(!R.success && "message can only be used if success is false");
107+
if (const auto *messageEnum = std::get_if<Response::Message>(&*R.message)) {
108+
switch (*messageEnum) {
109+
case Response::Message::cancelled:
110+
Result.insert({"message", "cancelled"});
111+
break;
112+
case Response::Message::notStopped:
113+
Result.insert({"message", "notStopped"});
114+
break;
115+
}
116+
} else if (const auto *messageString =
117+
std::get_if<std::string>(&*R.message)) {
118+
Result.insert({"message", *messageString});
119+
}
120+
}
121+
122+
if (R.rawBody)
123+
Result.insert({"body", R.rawBody});
124+
125+
return std::move(Result);
126+
}
127+
128+
bool fromJSON(json::Value const &Params,
129+
std::variant<Response::Message, std::string> &M, json::Path P) {
130+
auto rawMessage = Params.getAsString();
131+
if (!rawMessage) {
132+
P.report("expected a string");
133+
return false;
134+
}
135+
std::optional<Response::Message> message =
136+
StringSwitch<std::optional<Response::Message>>(*rawMessage)
137+
.Case("cancelled", Response::Message::cancelled)
138+
.Case("notStopped", Response::Message::notStopped)
139+
.Default(std::nullopt);
140+
if (message)
141+
M = *message;
142+
else if (!rawMessage->empty())
143+
M = rawMessage->str();
144+
return true;
145+
}
146+
147+
bool fromJSON(json::Value const &Params, Response &R, json::Path P) {
148+
json::ObjectMapper O(Params, P);
149+
if (!O)
150+
return false;
151+
152+
MessageType type;
153+
int64_t seq;
154+
if (!O.map("type", type) || !O.map("seq", seq) ||
155+
!O.map("command", R.command) || !O.map("request_seq", R.request_seq))
156+
return false;
157+
158+
if (type != MessageType::response) {
159+
P.field("type").report("expected to be 'response'");
160+
return false;
161+
}
162+
163+
if (seq != 0) {
164+
P.field("seq").report("expected to be '0'");
165+
return false;
166+
}
167+
168+
if (R.command.empty()) {
169+
P.field("command").report("expected to not be ''");
170+
return false;
171+
}
172+
173+
if (R.request_seq == 0) {
174+
P.field("request_seq").report("expected to not be '0'");
175+
return false;
176+
}
177+
178+
return O.map("success", R.success) && O.mapOptional("message", R.message) &&
179+
mapRaw(Params, "body", R.rawBody, P);
180+
}
181+
182+
json::Value toJSON(const ErrorMessage &EM) {
183+
json::Object Result{{"id", EM.id}, {"format", EM.format}};
184+
185+
if (EM.variables) {
186+
json::Object variables;
187+
for (auto &var : *EM.variables)
188+
variables[var.first] = var.second;
189+
Result.insert({"variables", std::move(variables)});
190+
}
191+
if (EM.sendTelemetry)
192+
Result.insert({"sendTelemetry", EM.sendTelemetry});
193+
if (EM.showUser)
194+
Result.insert({"showUser", EM.showUser});
195+
if (EM.url)
196+
Result.insert({"url", EM.url});
197+
if (EM.urlLabel)
198+
Result.insert({"urlLabel", EM.urlLabel});
199+
200+
return std::move(Result);
201+
}
202+
203+
bool fromJSON(json::Value const &Params, ErrorMessage &EM, json::Path P) {
204+
json::ObjectMapper O(Params, P);
205+
return O && O.map("id", EM.id) && O.map("format", EM.format) &&
206+
O.map("variables", EM.variables) &&
207+
O.map("sendTelemetry", EM.sendTelemetry) &&
208+
O.map("showUser", EM.showUser) && O.map("url", EM.url) &&
209+
O.map("urlLabel", EM.urlLabel);
210+
}
211+
212+
json::Value toJSON(const Event &E) {
213+
json::Object Result{
214+
{"type", "event"},
215+
{"seq", 0},
216+
{"event", E.event},
217+
};
218+
219+
if (E.rawBody)
220+
Result.insert({"body", E.rawBody});
221+
222+
return std::move(Result);
223+
}
224+
225+
bool fromJSON(json::Value const &Params, Event &E, json::Path P) {
226+
json::ObjectMapper O(Params, P);
227+
if (!O)
228+
return false;
229+
230+
MessageType type;
231+
int64_t seq;
232+
if (!O.map("type", type) || !O.map("seq", seq) || !O.map("event", E.event))
233+
return false;
234+
235+
if (type != MessageType::event) {
236+
P.field("type").report("expected to be 'event'");
237+
return false;
238+
}
239+
240+
if (seq != 0) {
241+
P.field("seq").report("expected to be '0'");
242+
return false;
243+
}
244+
245+
if (E.event.empty()) {
246+
P.field("event").report("expected to not be ''");
247+
return false;
248+
}
249+
250+
return mapRaw(Params, "body", E.rawBody, P);
251+
}
252+
253+
bool fromJSON(const json::Value &Params, Message &PM, json::Path P) {
254+
json::ObjectMapper O(Params, P);
255+
if (!O)
256+
return false;
257+
258+
MessageType type;
259+
if (!O.map("type", type))
260+
return false;
261+
262+
switch (type) {
263+
case MessageType::request: {
264+
Request req;
265+
if (!fromJSON(Params, req, P))
266+
return false;
267+
PM = std::move(req);
268+
return true;
269+
}
270+
case MessageType::response: {
271+
Response resp;
272+
if (!fromJSON(Params, resp, P))
273+
return false;
274+
PM = std::move(resp);
275+
return true;
276+
}
277+
case MessageType::event:
278+
Event evt;
279+
if (!fromJSON(Params, evt, P))
280+
return false;
281+
PM = std::move(evt);
282+
return true;
283+
}
284+
}
285+
286+
json::Value toJSON(const Message &M) {
287+
return std::visit([](auto &M) { return toJSON(M); }, M);
288+
}
289+
290+
} // namespace protocol
291+
} // namespace lldb_dap

0 commit comments

Comments
 (0)