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