Path: blob/master/editor/debugger/debug_adapter/debug_adapter_parser.cpp
9906 views
/**************************************************************************/1/* debug_adapter_parser.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "debug_adapter_parser.h"3132#include "editor/debugger/debug_adapter/debug_adapter_types.h"33#include "editor/debugger/editor_debugger_node.h"34#include "editor/debugger/script_editor_debugger.h"35#include "editor/export/editor_export_platform.h"36#include "editor/run/editor_run_bar.h"37#include "editor/script/script_editor_plugin.h"3839void DebugAdapterParser::_bind_methods() {40// Requests41ClassDB::bind_method(D_METHOD("req_initialize", "params"), &DebugAdapterParser::req_initialize);42ClassDB::bind_method(D_METHOD("req_disconnect", "params"), &DebugAdapterParser::req_disconnect);43ClassDB::bind_method(D_METHOD("req_launch", "params"), &DebugAdapterParser::req_launch);44ClassDB::bind_method(D_METHOD("req_attach", "params"), &DebugAdapterParser::req_attach);45ClassDB::bind_method(D_METHOD("req_restart", "params"), &DebugAdapterParser::req_restart);46ClassDB::bind_method(D_METHOD("req_terminate", "params"), &DebugAdapterParser::req_terminate);47ClassDB::bind_method(D_METHOD("req_configurationDone", "params"), &DebugAdapterParser::req_configurationDone);48ClassDB::bind_method(D_METHOD("req_pause", "params"), &DebugAdapterParser::req_pause);49ClassDB::bind_method(D_METHOD("req_continue", "params"), &DebugAdapterParser::req_continue);50ClassDB::bind_method(D_METHOD("req_threads", "params"), &DebugAdapterParser::req_threads);51ClassDB::bind_method(D_METHOD("req_stackTrace", "params"), &DebugAdapterParser::req_stackTrace);52ClassDB::bind_method(D_METHOD("req_setBreakpoints", "params"), &DebugAdapterParser::req_setBreakpoints);53ClassDB::bind_method(D_METHOD("req_breakpointLocations", "params"), &DebugAdapterParser::req_breakpointLocations);54ClassDB::bind_method(D_METHOD("req_scopes", "params"), &DebugAdapterParser::req_scopes);55ClassDB::bind_method(D_METHOD("req_variables", "params"), &DebugAdapterParser::req_variables);56ClassDB::bind_method(D_METHOD("req_next", "params"), &DebugAdapterParser::req_next);57ClassDB::bind_method(D_METHOD("req_stepIn", "params"), &DebugAdapterParser::req_stepIn);58ClassDB::bind_method(D_METHOD("req_evaluate", "params"), &DebugAdapterParser::req_evaluate);59ClassDB::bind_method(D_METHOD("req_godot/put_msg", "params"), &DebugAdapterParser::req_godot_put_msg);60}6162Dictionary DebugAdapterParser::prepare_base_event() const {63Dictionary event;64event["type"] = "event";6566return event;67}6869Dictionary DebugAdapterParser::prepare_success_response(const Dictionary &p_params) const {70Dictionary response;71response["type"] = "response";72response["request_seq"] = p_params["seq"];73response["command"] = p_params["command"];74response["success"] = true;7576return response;77}7879Dictionary DebugAdapterParser::prepare_error_response(const Dictionary &p_params, DAP::ErrorType err_type, const Dictionary &variables) const {80Dictionary response, body;81response["type"] = "response";82response["request_seq"] = p_params["seq"];83response["command"] = p_params["command"];84response["success"] = false;85response["body"] = body;8687DAP::Message message;88String error, error_desc;89switch (err_type) {90case DAP::ErrorType::WRONG_PATH:91error = "wrong_path";92error_desc = "The editor and client are working on different paths; the client is on \"{clientPath}\", but the editor is on \"{editorPath}\"";93break;94case DAP::ErrorType::NOT_RUNNING:95error = "not_running";96error_desc = "Can't attach to a running session since there isn't one.";97break;98case DAP::ErrorType::TIMEOUT:99error = "timeout";100error_desc = "Timeout reached while processing a request.";101break;102case DAP::ErrorType::UNKNOWN_PLATFORM:103error = "unknown_platform";104error_desc = "The specified platform is unknown.";105break;106case DAP::ErrorType::MISSING_DEVICE:107error = "missing_device";108error_desc = "There's no connected device with specified id.";109break;110case DAP::ErrorType::UNKNOWN:111default:112error = "unknown";113error_desc = "An unknown error has occurred when processing the request.";114break;115}116117message.id = err_type;118message.format = error_desc;119message.variables = variables;120response["message"] = error;121body["error"] = message.to_json();122123return response;124}125126Dictionary DebugAdapterParser::req_initialize(const Dictionary &p_params) const {127Dictionary response = prepare_success_response(p_params);128Dictionary args = p_params["arguments"];129130Ref<DAPeer> peer = DebugAdapterProtocol::get_singleton()->get_current_peer();131132peer->linesStartAt1 = args.get("linesStartAt1", false);133peer->columnsStartAt1 = args.get("columnsStartAt1", false);134peer->supportsVariableType = args.get("supportsVariableType", false);135peer->supportsInvalidatedEvent = args.get("supportsInvalidatedEvent", false);136137DAP::Capabilities caps;138response["body"] = caps.to_json();139140DebugAdapterProtocol::get_singleton()->notify_initialized();141142if (DebugAdapterProtocol::get_singleton()->_sync_breakpoints) {143// Send all current breakpoints144List<String> breakpoints;145ScriptEditor::get_singleton()->get_breakpoints(&breakpoints);146for (const String &breakpoint : breakpoints) {147String path = breakpoint.left(breakpoint.find_char(':', 6)); // Skip initial part of path, aka "res://"148int line = breakpoint.substr(path.size()).to_int();149150DebugAdapterProtocol::get_singleton()->on_debug_breakpoint_toggled(path, line, true);151}152} else {153// Remove all current breakpoints154EditorDebuggerNode::get_singleton()->get_default_debugger()->_clear_breakpoints();155}156157return response;158}159160Dictionary DebugAdapterParser::req_disconnect(const Dictionary &p_params) const {161if (!DebugAdapterProtocol::get_singleton()->get_current_peer()->attached) {162EditorRunBar::get_singleton()->stop_playing();163}164165return prepare_success_response(p_params);166}167168Dictionary DebugAdapterParser::req_launch(const Dictionary &p_params) const {169Dictionary args = p_params["arguments"];170if (args.has("project") && !is_valid_path(args["project"])) {171Dictionary variables;172variables["clientPath"] = args["project"];173variables["editorPath"] = ProjectSettings::get_singleton()->get_resource_path();174return prepare_error_response(p_params, DAP::ErrorType::WRONG_PATH, variables);175}176177if (args.has("godot/custom_data")) {178DebugAdapterProtocol::get_singleton()->get_current_peer()->supportsCustomData = args["godot/custom_data"];179}180181DebugAdapterProtocol::get_singleton()->get_current_peer()->pending_launch = p_params;182183return Dictionary();184}185186Dictionary DebugAdapterParser::_launch_process(const Dictionary &p_params) const {187Dictionary args = p_params["arguments"];188ScriptEditorDebugger *dbg = EditorDebuggerNode::get_singleton()->get_default_debugger();189if ((bool)args["noDebug"] != dbg->is_skip_breakpoints()) {190dbg->debug_skip_breakpoints();191}192193String platform_string = args.get("platform", "host");194if (platform_string == "host") {195EditorRunBar::get_singleton()->play_main_scene();196} else {197int device = args.get("device", -1);198int idx = -1;199if (platform_string == "android") {200for (int i = 0; i < EditorExport::get_singleton()->get_export_platform_count(); i++) {201if (EditorExport::get_singleton()->get_export_platform(i)->get_name() == "Android") {202idx = i;203break;204}205}206} else if (platform_string == "web") {207for (int i = 0; i < EditorExport::get_singleton()->get_export_platform_count(); i++) {208if (EditorExport::get_singleton()->get_export_platform(i)->get_name() == "Web") {209idx = i;210break;211}212}213}214215if (idx == -1) {216return prepare_error_response(p_params, DAP::ErrorType::UNKNOWN_PLATFORM);217}218219EditorRunBar *run_bar = EditorRunBar::get_singleton();220Error err = platform_string == "android" ? run_bar->start_native_device(device * 10000 + idx) : run_bar->start_native_device(idx);221if (err) {222if (err == ERR_INVALID_PARAMETER && platform_string == "android") {223return prepare_error_response(p_params, DAP::ErrorType::MISSING_DEVICE);224} else {225return prepare_error_response(p_params, DAP::ErrorType::UNKNOWN);226}227}228}229230DebugAdapterProtocol::get_singleton()->get_current_peer()->attached = false;231DebugAdapterProtocol::get_singleton()->notify_process();232233return prepare_success_response(p_params);234}235236Dictionary DebugAdapterParser::req_attach(const Dictionary &p_params) const {237ScriptEditorDebugger *dbg = EditorDebuggerNode::get_singleton()->get_default_debugger();238if (!dbg->is_session_active()) {239return prepare_error_response(p_params, DAP::ErrorType::NOT_RUNNING);240}241242DebugAdapterProtocol::get_singleton()->get_current_peer()->attached = true;243DebugAdapterProtocol::get_singleton()->notify_process();244return prepare_success_response(p_params);245}246247Dictionary DebugAdapterParser::req_restart(const Dictionary &p_params) const {248// Extract embedded "arguments" so it can be given to req_launch/req_attach249Dictionary params = p_params, args;250args = params["arguments"];251args = args["arguments"];252params["arguments"] = args;253254Dictionary response = DebugAdapterProtocol::get_singleton()->get_current_peer()->attached ? req_attach(params) : _launch_process(params);255if (!response["success"]) {256response["command"] = p_params["command"];257return response;258}259260return prepare_success_response(p_params);261}262263Dictionary DebugAdapterParser::req_terminate(const Dictionary &p_params) const {264EditorRunBar::get_singleton()->stop_playing();265266return prepare_success_response(p_params);267}268269Dictionary DebugAdapterParser::req_configurationDone(const Dictionary &p_params) const {270Ref<DAPeer> peer = DebugAdapterProtocol::get_singleton()->get_current_peer();271if (!peer->pending_launch.is_empty()) {272peer->res_queue.push_back(_launch_process(peer->pending_launch));273peer->pending_launch.clear();274}275276return prepare_success_response(p_params);277}278279Dictionary DebugAdapterParser::req_pause(const Dictionary &p_params) const {280EditorRunBar::get_singleton()->get_pause_button()->set_pressed(true);281EditorDebuggerNode::get_singleton()->_paused();282283DebugAdapterProtocol::get_singleton()->notify_stopped_paused();284285return prepare_success_response(p_params);286}287288Dictionary DebugAdapterParser::req_continue(const Dictionary &p_params) const {289EditorRunBar::get_singleton()->get_pause_button()->set_pressed(false);290EditorDebuggerNode::get_singleton()->_paused();291292DebugAdapterProtocol::get_singleton()->notify_continued();293294return prepare_success_response(p_params);295}296297Dictionary DebugAdapterParser::req_threads(const Dictionary &p_params) const {298Dictionary response = prepare_success_response(p_params), body;299response["body"] = body;300301DAP::Thread thread;302303thread.id = 1; // Hardcoded because Godot only supports debugging one thread at the moment304thread.name = "Main";305Array arr = { thread.to_json() };306body["threads"] = arr;307308return response;309}310311Dictionary DebugAdapterParser::req_stackTrace(const Dictionary &p_params) const {312if (DebugAdapterProtocol::get_singleton()->_processing_stackdump) {313return Dictionary();314}315316Dictionary response = prepare_success_response(p_params), body;317response["body"] = body;318319bool lines_at_one = DebugAdapterProtocol::get_singleton()->get_current_peer()->linesStartAt1;320bool columns_at_one = DebugAdapterProtocol::get_singleton()->get_current_peer()->columnsStartAt1;321322Array arr;323DebugAdapterProtocol *dap = DebugAdapterProtocol::get_singleton();324for (DAP::StackFrame sf : dap->stackframe_list) {325if (!lines_at_one) {326sf.line--;327}328if (!columns_at_one) {329sf.column--;330}331332arr.push_back(sf.to_json());333}334335body["stackFrames"] = arr;336return response;337}338339Dictionary DebugAdapterParser::req_setBreakpoints(const Dictionary &p_params) const {340Dictionary response = prepare_success_response(p_params), body;341response["body"] = body;342343Dictionary args = p_params["arguments"];344DAP::Source source;345source.from_json(args["source"]);346347bool lines_at_one = DebugAdapterProtocol::get_singleton()->get_current_peer()->linesStartAt1;348349if (!is_valid_path(source.path)) {350Dictionary variables;351variables["clientPath"] = source.path;352variables["editorPath"] = ProjectSettings::get_singleton()->get_resource_path();353return prepare_error_response(p_params, DAP::ErrorType::WRONG_PATH, variables);354}355356// If path contains \, it's a Windows path, so we need to convert it to /, and make the drive letter uppercase357if (source.path.contains_char('\\')) {358source.path = source.path.replace_char('\\', '/');359source.path = source.path.substr(0, 1).to_upper() + source.path.substr(1);360}361362Array breakpoints = args["breakpoints"], lines;363for (int i = 0; i < breakpoints.size(); i++) {364DAP::SourceBreakpoint breakpoint;365breakpoint.from_json(breakpoints[i]);366367lines.push_back(breakpoint.line + !lines_at_one);368}369370// Always update the source checksum for the requested path, as it might have been modified externally.371DebugAdapterProtocol::get_singleton()->update_source(source.path);372Array updated_breakpoints = DebugAdapterProtocol::get_singleton()->update_breakpoints(source.path, lines);373body["breakpoints"] = updated_breakpoints;374375return response;376}377378Dictionary DebugAdapterParser::req_breakpointLocations(const Dictionary &p_params) const {379Dictionary response = prepare_success_response(p_params), body;380response["body"] = body;381Dictionary args = p_params["arguments"];382383DAP::BreakpointLocation location;384location.line = args["line"];385if (args.has("endLine")) {386location.endLine = args["endLine"];387}388Array locations = { location.to_json() };389390body["breakpoints"] = locations;391return response;392}393394Dictionary DebugAdapterParser::req_scopes(const Dictionary &p_params) const {395Dictionary response = prepare_success_response(p_params), body;396response["body"] = body;397398Dictionary args = p_params["arguments"];399int frame_id = args["frameId"];400Array scope_list;401402HashMap<DebugAdapterProtocol::DAPStackFrameID, Vector<int>>::Iterator E = DebugAdapterProtocol::get_singleton()->scope_list.find(frame_id);403if (E) {404const Vector<int> &scope_ids = E->value;405ERR_FAIL_COND_V(scope_ids.size() != 3, prepare_error_response(p_params, DAP::ErrorType::UNKNOWN));406for (int i = 0; i < 3; ++i) {407DAP::Scope scope;408scope.variablesReference = scope_ids[i];409switch (i) {410case 0:411scope.name = "Locals";412scope.presentationHint = "locals";413break;414case 1:415scope.name = "Members";416scope.presentationHint = "members";417break;418case 2:419scope.name = "Globals";420scope.presentationHint = "globals";421}422423scope_list.push_back(scope.to_json());424}425}426427EditorDebuggerNode::get_singleton()->get_default_debugger()->request_stack_dump(frame_id);428DebugAdapterProtocol::get_singleton()->_current_frame = frame_id;429430body["scopes"] = scope_list;431return response;432}433434Dictionary DebugAdapterParser::req_variables(const Dictionary &p_params) const {435// If _remaining_vars > 0, the debuggee is still sending a stack dump to the editor.436if (DebugAdapterProtocol::get_singleton()->_remaining_vars > 0) {437return Dictionary();438}439440Dictionary args = p_params["arguments"];441int variable_id = args["variablesReference"];442443if (HashMap<int, Array>::Iterator E = DebugAdapterProtocol::get_singleton()->variable_list.find(variable_id); E) {444Dictionary response = prepare_success_response(p_params);445Dictionary body;446response["body"] = body;447448if (!DebugAdapterProtocol::get_singleton()->get_current_peer()->supportsVariableType) {449for (int i = 0; i < E->value.size(); i++) {450Dictionary variable = E->value[i];451variable.erase("type");452}453}454455body["variables"] = E ? E->value : Array();456return response;457} else {458// If the requested variable is an object, it needs to be requested from the debuggee.459ObjectID object_id = DebugAdapterProtocol::get_singleton()->search_object_id(variable_id);460461if (object_id.is_null()) {462return prepare_error_response(p_params, DAP::ErrorType::UNKNOWN);463}464465DebugAdapterProtocol::get_singleton()->request_remote_object(object_id);466}467return Dictionary();468}469470Dictionary DebugAdapterParser::req_next(const Dictionary &p_params) const {471EditorDebuggerNode::get_singleton()->get_default_debugger()->debug_next();472DebugAdapterProtocol::get_singleton()->_stepping = true;473474return prepare_success_response(p_params);475}476477Dictionary DebugAdapterParser::req_stepIn(const Dictionary &p_params) const {478EditorDebuggerNode::get_singleton()->get_default_debugger()->debug_step();479DebugAdapterProtocol::get_singleton()->_stepping = true;480481return prepare_success_response(p_params);482}483484Dictionary DebugAdapterParser::req_evaluate(const Dictionary &p_params) const {485Dictionary args = p_params["arguments"];486String expression = args["expression"];487int frame_id = args.has("frameId") ? static_cast<int>(args["frameId"]) : DebugAdapterProtocol::get_singleton()->_current_frame;488489if (HashMap<String, DAP::Variable>::Iterator E = DebugAdapterProtocol::get_singleton()->eval_list.find(expression); E) {490Dictionary response = prepare_success_response(p_params);491Dictionary body;492response["body"] = body;493494DAP::Variable var = E->value;495496body["result"] = var.value;497body["variablesReference"] = var.variablesReference;498499// Since an evaluation can alter the state of the debuggee, they are volatile, and should only be used once500DebugAdapterProtocol::get_singleton()->eval_list.erase(E->key);501return response;502} else {503DebugAdapterProtocol::get_singleton()->request_remote_evaluate(expression, frame_id);504}505return Dictionary();506}507508Dictionary DebugAdapterParser::req_godot_put_msg(const Dictionary &p_params) const {509Dictionary args = p_params["arguments"];510511String msg = args["message"];512Array data = args["data"];513514EditorDebuggerNode::get_singleton()->get_default_debugger()->_put_msg(msg, data);515516return prepare_success_response(p_params);517}518519Dictionary DebugAdapterParser::ev_initialized() const {520Dictionary event = prepare_base_event();521event["event"] = "initialized";522523return event;524}525526Dictionary DebugAdapterParser::ev_process(const String &p_command) const {527Dictionary event = prepare_base_event(), body;528event["event"] = "process";529event["body"] = body;530531body["name"] = OS::get_singleton()->get_executable_path();532body["startMethod"] = p_command;533534return event;535}536537Dictionary DebugAdapterParser::ev_terminated() const {538Dictionary event = prepare_base_event();539event["event"] = "terminated";540541return event;542}543544Dictionary DebugAdapterParser::ev_exited(const int &p_exitcode) const {545Dictionary event = prepare_base_event(), body;546event["event"] = "exited";547event["body"] = body;548549body["exitCode"] = p_exitcode;550551return event;552}553554Dictionary DebugAdapterParser::ev_stopped() const {555Dictionary event = prepare_base_event(), body;556event["event"] = "stopped";557event["body"] = body;558559body["threadId"] = 1;560561return event;562}563564Dictionary DebugAdapterParser::ev_stopped_paused() const {565Dictionary event = ev_stopped();566Dictionary body = event["body"];567568body["reason"] = "paused";569body["description"] = "Paused";570571return event;572}573574Dictionary DebugAdapterParser::ev_stopped_exception(const String &p_error) const {575Dictionary event = ev_stopped();576Dictionary body = event["body"];577578body["reason"] = "exception";579body["description"] = "Exception";580body["text"] = p_error;581582return event;583}584585Dictionary DebugAdapterParser::ev_stopped_breakpoint(const int &p_id) const {586Dictionary event = ev_stopped();587Dictionary body = event["body"];588589body["reason"] = "breakpoint";590body["description"] = "Breakpoint";591592Array breakpoints = { p_id };593body["hitBreakpointIds"] = breakpoints;594595return event;596}597598Dictionary DebugAdapterParser::ev_stopped_step() const {599Dictionary event = ev_stopped();600Dictionary body = event["body"];601602body["reason"] = "step";603body["description"] = "Breakpoint";604605return event;606}607608Dictionary DebugAdapterParser::ev_continued() const {609Dictionary event = prepare_base_event(), body;610event["event"] = "continued";611event["body"] = body;612613body["threadId"] = 1;614615return event;616}617618Dictionary DebugAdapterParser::ev_output(const String &p_message, RemoteDebugger::MessageType p_type) const {619Dictionary event = prepare_base_event(), body;620event["event"] = "output";621event["body"] = body;622623body["category"] = (p_type == RemoteDebugger::MessageType::MESSAGE_TYPE_ERROR) ? "stderr" : "stdout";624body["output"] = p_message + "\r\n";625626return event;627}628629Dictionary DebugAdapterParser::ev_breakpoint(const DAP::Breakpoint &p_breakpoint, const bool &p_enabled) const {630Dictionary event = prepare_base_event(), body;631event["event"] = "breakpoint";632event["body"] = body;633634body["reason"] = p_enabled ? "new" : "removed";635body["breakpoint"] = p_breakpoint.to_json();636637return event;638}639640Dictionary DebugAdapterParser::ev_custom_data(const String &p_msg, const Array &p_data) const {641Dictionary event = prepare_base_event(), body;642event["event"] = "godot/custom_data";643event["body"] = body;644645body["message"] = p_msg;646body["data"] = p_data;647648return event;649}650651652