1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
|
/**************************************************************************/
/* debug_adapter_parser.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "debug_adapter_parser.h"
#include "editor/debugger/editor_debugger_node.h"
#include "editor/debugger/script_editor_debugger.h"
#include "editor/editor_node.h"
#include "editor/editor_run_native.h"
#include "editor/export/editor_export_platform.h"
#include "editor/plugins/script_editor_plugin.h"
void DebugAdapterParser::_bind_methods() {
// Requests
ClassDB::bind_method(D_METHOD("req_initialize", "params"), &DebugAdapterParser::req_initialize);
ClassDB::bind_method(D_METHOD("req_disconnect", "params"), &DebugAdapterParser::req_disconnect);
ClassDB::bind_method(D_METHOD("req_launch", "params"), &DebugAdapterParser::req_launch);
ClassDB::bind_method(D_METHOD("req_attach", "params"), &DebugAdapterParser::req_attach);
ClassDB::bind_method(D_METHOD("req_restart", "params"), &DebugAdapterParser::req_restart);
ClassDB::bind_method(D_METHOD("req_terminate", "params"), &DebugAdapterParser::req_terminate);
ClassDB::bind_method(D_METHOD("req_configurationDone", "params"), &DebugAdapterParser::prepare_success_response);
ClassDB::bind_method(D_METHOD("req_pause", "params"), &DebugAdapterParser::req_pause);
ClassDB::bind_method(D_METHOD("req_continue", "params"), &DebugAdapterParser::req_continue);
ClassDB::bind_method(D_METHOD("req_threads", "params"), &DebugAdapterParser::req_threads);
ClassDB::bind_method(D_METHOD("req_stackTrace", "params"), &DebugAdapterParser::req_stackTrace);
ClassDB::bind_method(D_METHOD("req_setBreakpoints", "params"), &DebugAdapterParser::req_setBreakpoints);
ClassDB::bind_method(D_METHOD("req_breakpointLocations", "params"), &DebugAdapterParser::req_breakpointLocations);
ClassDB::bind_method(D_METHOD("req_scopes", "params"), &DebugAdapterParser::req_scopes);
ClassDB::bind_method(D_METHOD("req_variables", "params"), &DebugAdapterParser::req_variables);
ClassDB::bind_method(D_METHOD("req_next", "params"), &DebugAdapterParser::req_next);
ClassDB::bind_method(D_METHOD("req_stepIn", "params"), &DebugAdapterParser::req_stepIn);
ClassDB::bind_method(D_METHOD("req_evaluate", "params"), &DebugAdapterParser::req_evaluate);
ClassDB::bind_method(D_METHOD("req_godot/put_msg", "params"), &DebugAdapterParser::req_godot_put_msg);
}
Dictionary DebugAdapterParser::prepare_base_event() const {
Dictionary event;
event["type"] = "event";
return event;
}
Dictionary DebugAdapterParser::prepare_success_response(const Dictionary &p_params) const {
Dictionary response;
response["type"] = "response";
response["request_seq"] = p_params["seq"];
response["command"] = p_params["command"];
response["success"] = true;
return response;
}
Dictionary DebugAdapterParser::prepare_error_response(const Dictionary &p_params, DAP::ErrorType err_type, const Dictionary &variables) const {
Dictionary response, body;
response["type"] = "response";
response["request_seq"] = p_params["seq"];
response["command"] = p_params["command"];
response["success"] = false;
response["body"] = body;
DAP::Message message;
String error, error_desc;
switch (err_type) {
case DAP::ErrorType::WRONG_PATH:
error = "wrong_path";
error_desc = "The editor and client are working on different paths; the client is on \"{clientPath}\", but the editor is on \"{editorPath}\"";
break;
case DAP::ErrorType::NOT_RUNNING:
error = "not_running";
error_desc = "Can't attach to a running session since there isn't one.";
break;
case DAP::ErrorType::TIMEOUT:
error = "timeout";
error_desc = "Timeout reached while processing a request.";
break;
case DAP::ErrorType::UNKNOWN_PLATFORM:
error = "unknown_platform";
error_desc = "The specified platform is unknown.";
break;
case DAP::ErrorType::MISSING_DEVICE:
error = "missing_device";
error_desc = "There's no connected device with specified id.";
break;
case DAP::ErrorType::UNKNOWN:
default:
error = "unknown";
error_desc = "An unknown error has occurred when processing the request.";
break;
}
message.id = err_type;
message.format = error_desc;
message.variables = variables;
response["message"] = error;
body["error"] = message.to_json();
return response;
}
Dictionary DebugAdapterParser::req_initialize(const Dictionary &p_params) const {
Dictionary response = prepare_success_response(p_params);
Dictionary args = p_params["arguments"];
Ref<DAPeer> peer = DebugAdapterProtocol::get_singleton()->get_current_peer();
peer->linesStartAt1 = args.get("linesStartAt1", false);
peer->columnsStartAt1 = args.get("columnsStartAt1", false);
peer->supportsVariableType = args.get("supportsVariableType", false);
peer->supportsInvalidatedEvent = args.get("supportsInvalidatedEvent", false);
DAP::Capabilities caps;
response["body"] = caps.to_json();
DebugAdapterProtocol::get_singleton()->notify_initialized();
if (DebugAdapterProtocol::get_singleton()->_sync_breakpoints) {
// Send all current breakpoints
List<String> breakpoints;
ScriptEditor::get_singleton()->get_breakpoints(&breakpoints);
for (List<String>::Element *E = breakpoints.front(); E; E = E->next()) {
String breakpoint = E->get();
String path = breakpoint.left(breakpoint.find(":", 6)); // Skip initial part of path, aka "res://"
int line = breakpoint.substr(path.size()).to_int();
DebugAdapterProtocol::get_singleton()->on_debug_breakpoint_toggled(path, line, true);
}
} else {
// Remove all current breakpoints
EditorDebuggerNode::get_singleton()->get_default_debugger()->_clear_breakpoints();
}
return response;
}
Dictionary DebugAdapterParser::req_disconnect(const Dictionary &p_params) const {
if (!DebugAdapterProtocol::get_singleton()->get_current_peer()->attached) {
EditorNode::get_singleton()->run_stop();
}
return prepare_success_response(p_params);
}
Dictionary DebugAdapterParser::req_launch(const Dictionary &p_params) const {
Dictionary args = p_params["arguments"];
if (args.has("project") && !is_valid_path(args["project"])) {
Dictionary variables;
variables["clientPath"] = args["project"];
variables["editorPath"] = ProjectSettings::get_singleton()->get_resource_path();
return prepare_error_response(p_params, DAP::ErrorType::WRONG_PATH, variables);
}
if (args.has("godot/custom_data")) {
DebugAdapterProtocol::get_singleton()->get_current_peer()->supportsCustomData = args["godot/custom_data"];
}
ScriptEditorDebugger *dbg = EditorDebuggerNode::get_singleton()->get_default_debugger();
if ((bool)args["noDebug"] != dbg->is_skip_breakpoints()) {
dbg->debug_skip_breakpoints();
}
String platform_string = args.get("platform", "host");
if (platform_string == "host") {
EditorNode::get_singleton()->run_play();
} else {
int device = args.get("device", -1);
int idx = -1;
if (platform_string == "android") {
for (int i = 0; i < EditorExport::get_singleton()->get_export_platform_count(); i++) {
if (EditorExport::get_singleton()->get_export_platform(i)->get_name() == "Android") {
idx = i;
break;
}
}
} else if (platform_string == "web") {
for (int i = 0; i < EditorExport::get_singleton()->get_export_platform_count(); i++) {
if (EditorExport::get_singleton()->get_export_platform(i)->get_name() == "Web") {
idx = i;
break;
}
}
}
if (idx == -1) {
return prepare_error_response(p_params, DAP::ErrorType::UNKNOWN_PLATFORM);
}
EditorNode *editor = EditorNode::get_singleton();
Error err = platform_string == "android" ? editor->run_play_native(device, idx) : editor->run_play_native(-1, idx);
if (err) {
if (err == ERR_INVALID_PARAMETER && platform_string == "android") {
return prepare_error_response(p_params, DAP::ErrorType::MISSING_DEVICE);
} else {
return prepare_error_response(p_params, DAP::ErrorType::UNKNOWN);
}
}
}
DebugAdapterProtocol::get_singleton()->get_current_peer()->attached = false;
DebugAdapterProtocol::get_singleton()->notify_process();
return prepare_success_response(p_params);
}
Dictionary DebugAdapterParser::req_attach(const Dictionary &p_params) const {
ScriptEditorDebugger *dbg = EditorDebuggerNode::get_singleton()->get_default_debugger();
if (!dbg->is_session_active()) {
return prepare_error_response(p_params, DAP::ErrorType::NOT_RUNNING);
}
DebugAdapterProtocol::get_singleton()->get_current_peer()->attached = true;
DebugAdapterProtocol::get_singleton()->notify_process();
return prepare_success_response(p_params);
}
Dictionary DebugAdapterParser::req_restart(const Dictionary &p_params) const {
// Extract embedded "arguments" so it can be given to req_launch/req_attach
Dictionary params = p_params, args;
args = params["arguments"];
args = args["arguments"];
params["arguments"] = args;
Dictionary response = DebugAdapterProtocol::get_singleton()->get_current_peer()->attached ? req_attach(params) : req_launch(params);
if (!response["success"]) {
response["command"] = p_params["command"];
return response;
}
return prepare_success_response(p_params);
}
Dictionary DebugAdapterParser::req_terminate(const Dictionary &p_params) const {
EditorNode::get_singleton()->run_stop();
return prepare_success_response(p_params);
}
Dictionary DebugAdapterParser::req_pause(const Dictionary &p_params) const {
EditorNode::get_singleton()->get_pause_button()->set_pressed(true);
EditorDebuggerNode::get_singleton()->_paused();
DebugAdapterProtocol::get_singleton()->notify_stopped_paused();
return prepare_success_response(p_params);
}
Dictionary DebugAdapterParser::req_continue(const Dictionary &p_params) const {
EditorNode::get_singleton()->get_pause_button()->set_pressed(false);
EditorDebuggerNode::get_singleton()->_paused();
DebugAdapterProtocol::get_singleton()->notify_continued();
return prepare_success_response(p_params);
}
Dictionary DebugAdapterParser::req_threads(const Dictionary &p_params) const {
Dictionary response = prepare_success_response(p_params), body;
response["body"] = body;
Array arr;
DAP::Thread thread;
thread.id = 1; // Hardcoded because Godot only supports debugging one thread at the moment
thread.name = "Main";
arr.push_back(thread.to_json());
body["threads"] = arr;
return response;
}
Dictionary DebugAdapterParser::req_stackTrace(const Dictionary &p_params) const {
if (DebugAdapterProtocol::get_singleton()->_processing_stackdump) {
return Dictionary();
}
Dictionary response = prepare_success_response(p_params), body;
response["body"] = body;
bool lines_at_one = DebugAdapterProtocol::get_singleton()->get_current_peer()->linesStartAt1;
bool columns_at_one = DebugAdapterProtocol::get_singleton()->get_current_peer()->columnsStartAt1;
Array arr;
DebugAdapterProtocol *dap = DebugAdapterProtocol::get_singleton();
for (const KeyValue<DAP::StackFrame, List<int>> &E : dap->stackframe_list) {
DAP::StackFrame sf = E.key;
if (!lines_at_one) {
sf.line--;
}
if (!columns_at_one) {
sf.column--;
}
arr.push_back(sf.to_json());
}
body["stackFrames"] = arr;
return response;
}
Dictionary DebugAdapterParser::req_setBreakpoints(const Dictionary &p_params) const {
Dictionary response = prepare_success_response(p_params), body;
response["body"] = body;
Dictionary args = p_params["arguments"];
DAP::Source source;
source.from_json(args["source"]);
bool lines_at_one = DebugAdapterProtocol::get_singleton()->get_current_peer()->linesStartAt1;
if (!is_valid_path(source.path)) {
Dictionary variables;
variables["clientPath"] = source.path;
variables["editorPath"] = ProjectSettings::get_singleton()->get_resource_path();
return prepare_error_response(p_params, DAP::ErrorType::WRONG_PATH, variables);
}
Array breakpoints = args["breakpoints"], lines;
for (int i = 0; i < breakpoints.size(); i++) {
DAP::SourceBreakpoint breakpoint;
breakpoint.from_json(breakpoints[i]);
lines.push_back(breakpoint.line + !lines_at_one);
}
Array updated_breakpoints = DebugAdapterProtocol::get_singleton()->update_breakpoints(source.path, lines);
body["breakpoints"] = updated_breakpoints;
return response;
}
Dictionary DebugAdapterParser::req_breakpointLocations(const Dictionary &p_params) const {
Dictionary response = prepare_success_response(p_params), body;
response["body"] = body;
Dictionary args = p_params["arguments"];
Array locations;
DAP::BreakpointLocation location;
location.line = args["line"];
if (args.has("endLine")) {
location.endLine = args["endLine"];
}
locations.push_back(location.to_json());
body["breakpoints"] = locations;
return response;
}
Dictionary DebugAdapterParser::req_scopes(const Dictionary &p_params) const {
Dictionary response = prepare_success_response(p_params), body;
response["body"] = body;
Dictionary args = p_params["arguments"];
int frame_id = args["frameId"];
Array scope_list;
DAP::StackFrame frame;
frame.id = frame_id;
HashMap<DAP::StackFrame, List<int>, DAP::StackFrame>::Iterator E = DebugAdapterProtocol::get_singleton()->stackframe_list.find(frame);
if (E) {
ERR_FAIL_COND_V(E->value.size() != 3, prepare_error_response(p_params, DAP::ErrorType::UNKNOWN));
for (int i = 0; i < 3; i++) {
DAP::Scope scope;
scope.variablesReference = E->value[i];
switch (i) {
case 0:
scope.name = "Locals";
scope.presentationHint = "locals";
break;
case 1:
scope.name = "Members";
scope.presentationHint = "members";
break;
case 2:
scope.name = "Globals";
scope.presentationHint = "globals";
}
scope_list.push_back(scope.to_json());
}
}
EditorDebuggerNode::get_singleton()->get_default_debugger()->request_stack_dump(frame_id);
DebugAdapterProtocol::get_singleton()->_current_frame = frame_id;
body["scopes"] = scope_list;
return response;
}
Dictionary DebugAdapterParser::req_variables(const Dictionary &p_params) const {
// If _remaining_vars > 0, the debuggee is still sending a stack dump to the editor.
if (DebugAdapterProtocol::get_singleton()->_remaining_vars > 0) {
return Dictionary();
}
Dictionary response = prepare_success_response(p_params), body;
response["body"] = body;
Dictionary args = p_params["arguments"];
int variable_id = args["variablesReference"];
HashMap<int, Array>::Iterator E = DebugAdapterProtocol::get_singleton()->variable_list.find(variable_id);
if (E) {
if (!DebugAdapterProtocol::get_singleton()->get_current_peer()->supportsVariableType) {
for (int i = 0; i < E->value.size(); i++) {
Dictionary variable = E->value[i];
variable.erase("type");
}
}
body["variables"] = E ? E->value : Array();
return response;
} else {
return Dictionary();
}
}
Dictionary DebugAdapterParser::req_next(const Dictionary &p_params) const {
EditorDebuggerNode::get_singleton()->get_default_debugger()->debug_next();
DebugAdapterProtocol::get_singleton()->_stepping = true;
return prepare_success_response(p_params);
}
Dictionary DebugAdapterParser::req_stepIn(const Dictionary &p_params) const {
EditorDebuggerNode::get_singleton()->get_default_debugger()->debug_step();
DebugAdapterProtocol::get_singleton()->_stepping = true;
return prepare_success_response(p_params);
}
Dictionary DebugAdapterParser::req_evaluate(const Dictionary &p_params) const {
Dictionary response = prepare_success_response(p_params), body;
response["body"] = body;
Dictionary args = p_params["arguments"];
String value = EditorDebuggerNode::get_singleton()->get_var_value(args["expression"]);
body["result"] = value;
return response;
}
Dictionary DebugAdapterParser::req_godot_put_msg(const Dictionary &p_params) const {
Dictionary args = p_params["arguments"];
String msg = args["message"];
Array data = args["data"];
EditorDebuggerNode::get_singleton()->get_default_debugger()->_put_msg(msg, data);
return prepare_success_response(p_params);
}
Dictionary DebugAdapterParser::ev_initialized() const {
Dictionary event = prepare_base_event();
event["event"] = "initialized";
return event;
}
Dictionary DebugAdapterParser::ev_process(const String &p_command) const {
Dictionary event = prepare_base_event(), body;
event["event"] = "process";
event["body"] = body;
body["name"] = OS::get_singleton()->get_executable_path();
body["startMethod"] = p_command;
return event;
}
Dictionary DebugAdapterParser::ev_terminated() const {
Dictionary event = prepare_base_event();
event["event"] = "terminated";
return event;
}
Dictionary DebugAdapterParser::ev_exited(const int &p_exitcode) const {
Dictionary event = prepare_base_event(), body;
event["event"] = "exited";
event["body"] = body;
body["exitCode"] = p_exitcode;
return event;
}
Dictionary DebugAdapterParser::ev_stopped() const {
Dictionary event = prepare_base_event(), body;
event["event"] = "stopped";
event["body"] = body;
body["threadId"] = 1;
return event;
}
Dictionary DebugAdapterParser::ev_stopped_paused() const {
Dictionary event = ev_stopped();
Dictionary body = event["body"];
body["reason"] = "paused";
body["description"] = "Paused";
return event;
}
Dictionary DebugAdapterParser::ev_stopped_exception(const String &p_error) const {
Dictionary event = ev_stopped();
Dictionary body = event["body"];
body["reason"] = "exception";
body["description"] = "Exception";
body["text"] = p_error;
return event;
}
Dictionary DebugAdapterParser::ev_stopped_breakpoint(const int &p_id) const {
Dictionary event = ev_stopped();
Dictionary body = event["body"];
body["reason"] = "breakpoint";
body["description"] = "Breakpoint";
Array breakpoints;
breakpoints.push_back(p_id);
body["hitBreakpointIds"] = breakpoints;
return event;
}
Dictionary DebugAdapterParser::ev_stopped_step() const {
Dictionary event = ev_stopped();
Dictionary body = event["body"];
body["reason"] = "step";
body["description"] = "Breakpoint";
return event;
}
Dictionary DebugAdapterParser::ev_continued() const {
Dictionary event = prepare_base_event(), body;
event["event"] = "continued";
event["body"] = body;
body["threadId"] = 1;
return event;
}
Dictionary DebugAdapterParser::ev_output(const String &p_message) const {
Dictionary event = prepare_base_event(), body;
event["event"] = "output";
event["body"] = body;
body["category"] = "stdout";
body["output"] = p_message + "\r\n";
return event;
}
Dictionary DebugAdapterParser::ev_breakpoint(const DAP::Breakpoint &p_breakpoint, const bool &p_enabled) const {
Dictionary event = prepare_base_event(), body;
event["event"] = "breakpoint";
event["body"] = body;
body["reason"] = p_enabled ? "new" : "removed";
body["breakpoint"] = p_breakpoint.to_json();
return event;
}
Dictionary DebugAdapterParser::ev_custom_data(const String &p_msg, const Array &p_data) const {
Dictionary event = prepare_base_event(), body;
event["event"] = "godot/custom_data";
event["body"] = body;
body["message"] = p_msg;
body["data"] = p_data;
return event;
}
|