debug_adapter_parser.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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. #include "debug_adapter_parser.h"
  31. #include "editor/debugger/debug_adapter/debug_adapter_protocol.h"
  32. #include "editor/debugger/editor_debugger_node.h"
  33. #include "editor/debugger/script_editor_debugger.h"
  34. #include "editor/export/editor_export.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"
  38. void DebugAdapterParser::_bind_methods() {
  39. // Requests
  40. ClassDB::bind_method(D_METHOD("req_initialize", "params"), &DebugAdapterParser::req_initialize);
  41. ClassDB::bind_method(D_METHOD("req_disconnect", "params"), &DebugAdapterParser::req_disconnect);
  42. ClassDB::bind_method(D_METHOD("req_launch", "params"), &DebugAdapterParser::req_launch);
  43. ClassDB::bind_method(D_METHOD("req_attach", "params"), &DebugAdapterParser::req_attach);
  44. ClassDB::bind_method(D_METHOD("req_restart", "params"), &DebugAdapterParser::req_restart);
  45. ClassDB::bind_method(D_METHOD("req_terminate", "params"), &DebugAdapterParser::req_terminate);
  46. ClassDB::bind_method(D_METHOD("req_configurationDone", "params"), &DebugAdapterParser::req_configurationDone);
  47. ClassDB::bind_method(D_METHOD("req_pause", "params"), &DebugAdapterParser::req_pause);
  48. ClassDB::bind_method(D_METHOD("req_continue", "params"), &DebugAdapterParser::req_continue);
  49. ClassDB::bind_method(D_METHOD("req_threads", "params"), &DebugAdapterParser::req_threads);
  50. ClassDB::bind_method(D_METHOD("req_stackTrace", "params"), &DebugAdapterParser::req_stackTrace);
  51. ClassDB::bind_method(D_METHOD("req_setBreakpoints", "params"), &DebugAdapterParser::req_setBreakpoints);
  52. ClassDB::bind_method(D_METHOD("req_breakpointLocations", "params"), &DebugAdapterParser::req_breakpointLocations);
  53. ClassDB::bind_method(D_METHOD("req_scopes", "params"), &DebugAdapterParser::req_scopes);
  54. ClassDB::bind_method(D_METHOD("req_variables", "params"), &DebugAdapterParser::req_variables);
  55. ClassDB::bind_method(D_METHOD("req_next", "params"), &DebugAdapterParser::req_next);
  56. ClassDB::bind_method(D_METHOD("req_stepIn", "params"), &DebugAdapterParser::req_stepIn);
  57. ClassDB::bind_method(D_METHOD("req_evaluate", "params"), &DebugAdapterParser::req_evaluate);
  58. ClassDB::bind_method(D_METHOD("req_godot/put_msg", "params"), &DebugAdapterParser::req_godot_put_msg);
  59. }
  60. Dictionary DebugAdapterParser::prepare_base_event() const {
  61. Dictionary event;
  62. event["type"] = "event";
  63. return event;
  64. }
  65. Dictionary DebugAdapterParser::prepare_success_response(const Dictionary &p_params) const {
  66. Dictionary response;
  67. response["type"] = "response";
  68. response["request_seq"] = p_params["seq"];
  69. response["command"] = p_params["command"];
  70. response["success"] = true;
  71. return response;
  72. }
  73. Dictionary DebugAdapterParser::prepare_error_response(const Dictionary &p_params, DAP::ErrorType err_type, const Dictionary &variables) const {
  74. Dictionary response, body;
  75. response["type"] = "response";
  76. response["request_seq"] = p_params["seq"];
  77. response["command"] = p_params["command"];
  78. response["success"] = false;
  79. response["body"] = body;
  80. DAP::Message message;
  81. String error, error_desc;
  82. switch (err_type) {
  83. case DAP::ErrorType::WRONG_PATH:
  84. error = "wrong_path";
  85. error_desc = "The editor and client are working on different paths; the client is on \"{clientPath}\", but the editor is on \"{editorPath}\"";
  86. break;
  87. case DAP::ErrorType::NOT_RUNNING:
  88. error = "not_running";
  89. error_desc = "Can't attach to a running session since there isn't one.";
  90. break;
  91. case DAP::ErrorType::TIMEOUT:
  92. error = "timeout";
  93. error_desc = "Timeout reached while processing a request.";
  94. break;
  95. case DAP::ErrorType::UNKNOWN_PLATFORM:
  96. error = "unknown_platform";
  97. error_desc = "The specified platform is unknown.";
  98. break;
  99. case DAP::ErrorType::MISSING_DEVICE:
  100. error = "missing_device";
  101. error_desc = "There's no connected device with specified id.";
  102. break;
  103. case DAP::ErrorType::UNKNOWN:
  104. default:
  105. error = "unknown";
  106. error_desc = "An unknown error has occurred when processing the request.";
  107. break;
  108. }
  109. message.id = err_type;
  110. message.format = error_desc;
  111. message.variables = variables;
  112. response["message"] = error;
  113. body["error"] = message.to_json();
  114. return response;
  115. }
  116. Dictionary DebugAdapterParser::req_initialize(const Dictionary &p_params) const {
  117. Dictionary response = prepare_success_response(p_params);
  118. Dictionary args = p_params["arguments"];
  119. Ref<DAPeer> peer = DebugAdapterProtocol::get_singleton()->get_current_peer();
  120. peer->linesStartAt1 = args.get("linesStartAt1", false);
  121. peer->columnsStartAt1 = args.get("columnsStartAt1", false);
  122. peer->supportsVariableType = args.get("supportsVariableType", false);
  123. peer->supportsInvalidatedEvent = args.get("supportsInvalidatedEvent", false);
  124. DAP::Capabilities caps;
  125. response["body"] = caps.to_json();
  126. DebugAdapterProtocol::get_singleton()->notify_initialized();
  127. if (DebugAdapterProtocol::get_singleton()->_sync_breakpoints) {
  128. // Send all current breakpoints
  129. List<String> breakpoints;
  130. ScriptEditor::get_singleton()->get_breakpoints(&breakpoints);
  131. for (const String &breakpoint : breakpoints) {
  132. String path = breakpoint.left(breakpoint.find_char(':', 6)); // Skip initial part of path, aka "res://"
  133. int line = breakpoint.substr(path.size()).to_int();
  134. DebugAdapterProtocol::get_singleton()->on_debug_breakpoint_toggled(path, line, true);
  135. }
  136. } else {
  137. // Remove all current breakpoints
  138. EditorDebuggerNode::get_singleton()->get_default_debugger()->_clear_breakpoints();
  139. }
  140. return response;
  141. }
  142. Dictionary DebugAdapterParser::req_disconnect(const Dictionary &p_params) const {
  143. if (!DebugAdapterProtocol::get_singleton()->get_current_peer()->attached) {
  144. EditorRunBar::get_singleton()->stop_playing();
  145. }
  146. return prepare_success_response(p_params);
  147. }
  148. Dictionary DebugAdapterParser::req_launch(const Dictionary &p_params) const {
  149. Dictionary args = p_params["arguments"];
  150. if (args.has("project") && !is_valid_path(args["project"])) {
  151. Dictionary variables;
  152. variables["clientPath"] = args["project"];
  153. variables["editorPath"] = ProjectSettings::get_singleton()->get_resource_path();
  154. return prepare_error_response(p_params, DAP::ErrorType::WRONG_PATH, variables);
  155. }
  156. if (args.has("godot/custom_data")) {
  157. DebugAdapterProtocol::get_singleton()->get_current_peer()->supportsCustomData = args["godot/custom_data"];
  158. }
  159. DebugAdapterProtocol::get_singleton()->get_current_peer()->pending_launch = p_params;
  160. return Dictionary();
  161. }
  162. Vector<String> DebugAdapterParser::_extract_play_arguments(const Dictionary &p_args) const {
  163. Vector<String> play_args;
  164. if (p_args.has("playArgs")) {
  165. Variant v = p_args["playArgs"];
  166. if (v.get_type() == Variant::ARRAY) {
  167. Array arr = v;
  168. for (const Variant &arg : arr) {
  169. play_args.push_back(String(arg));
  170. }
  171. }
  172. }
  173. return play_args;
  174. }
  175. Dictionary DebugAdapterParser::_launch_process(const Dictionary &p_params) const {
  176. Dictionary args = p_params["arguments"];
  177. ScriptEditorDebugger *dbg = EditorDebuggerNode::get_singleton()->get_default_debugger();
  178. if ((bool)args["noDebug"] != dbg->is_skip_breakpoints()) {
  179. dbg->debug_skip_breakpoints();
  180. }
  181. String platform_string = args.get("platform", "host");
  182. if (platform_string == "host") {
  183. Vector<String> play_args = _extract_play_arguments(args);
  184. const String scene = args.get("scene", "main");
  185. if (scene == "main") {
  186. EditorRunBar::get_singleton()->play_main_scene(false, play_args);
  187. } else if (scene == "current") {
  188. EditorRunBar::get_singleton()->play_current_scene(false, play_args);
  189. } else {
  190. EditorRunBar::get_singleton()->play_custom_scene(scene, play_args);
  191. }
  192. } else {
  193. // Not limited to Android, iOS, Web.
  194. const int platform_idx = EditorExport::get_singleton()->get_export_platform_index_by_name(platform_string);
  195. if (platform_idx == -1) {
  196. return prepare_error_response(p_params, DAP::ErrorType::UNKNOWN_PLATFORM);
  197. }
  198. // If it is not passed, would mean first device of this platform.
  199. const int device_idx = args.get("device", 0);
  200. const EditorRunBar *run_bar = EditorRunBar::get_singleton();
  201. const int encoded_id = EditorExport::encode_platform_device_id(platform_idx, device_idx);
  202. const Error err = run_bar->start_native_device(encoded_id);
  203. if (err) {
  204. if (err == ERR_INVALID_PARAMETER) {
  205. return prepare_error_response(p_params, DAP::ErrorType::MISSING_DEVICE);
  206. } else {
  207. return prepare_error_response(p_params, DAP::ErrorType::UNKNOWN);
  208. }
  209. }
  210. }
  211. DebugAdapterProtocol::get_singleton()->get_current_peer()->attached = false;
  212. DebugAdapterProtocol::get_singleton()->notify_process();
  213. return prepare_success_response(p_params);
  214. }
  215. Dictionary DebugAdapterParser::req_attach(const Dictionary &p_params) const {
  216. ScriptEditorDebugger *dbg = EditorDebuggerNode::get_singleton()->get_default_debugger();
  217. if (!dbg->is_session_active()) {
  218. return prepare_error_response(p_params, DAP::ErrorType::NOT_RUNNING);
  219. }
  220. DebugAdapterProtocol::get_singleton()->get_current_peer()->attached = true;
  221. DebugAdapterProtocol::get_singleton()->notify_process();
  222. return prepare_success_response(p_params);
  223. }
  224. Dictionary DebugAdapterParser::req_restart(const Dictionary &p_params) const {
  225. // Extract embedded "arguments" so it can be given to req_launch/req_attach
  226. Dictionary params = p_params, args;
  227. args = params["arguments"];
  228. args = args["arguments"];
  229. params["arguments"] = args;
  230. Dictionary response = DebugAdapterProtocol::get_singleton()->get_current_peer()->attached ? req_attach(params) : _launch_process(params);
  231. if (!response["success"]) {
  232. response["command"] = p_params["command"];
  233. return response;
  234. }
  235. return prepare_success_response(p_params);
  236. }
  237. Dictionary DebugAdapterParser::req_terminate(const Dictionary &p_params) const {
  238. EditorRunBar::get_singleton()->stop_playing();
  239. return prepare_success_response(p_params);
  240. }
  241. Dictionary DebugAdapterParser::req_configurationDone(const Dictionary &p_params) const {
  242. Ref<DAPeer> peer = DebugAdapterProtocol::get_singleton()->get_current_peer();
  243. if (!peer->pending_launch.is_empty()) {
  244. peer->res_queue.push_back(_launch_process(peer->pending_launch));
  245. peer->pending_launch.clear();
  246. }
  247. return prepare_success_response(p_params);
  248. }
  249. Dictionary DebugAdapterParser::req_pause(const Dictionary &p_params) const {
  250. EditorRunBar::get_singleton()->get_pause_button()->set_pressed(true);
  251. EditorDebuggerNode::get_singleton()->_paused();
  252. DebugAdapterProtocol::get_singleton()->notify_stopped_paused();
  253. return prepare_success_response(p_params);
  254. }
  255. Dictionary DebugAdapterParser::req_continue(const Dictionary &p_params) const {
  256. EditorRunBar::get_singleton()->get_pause_button()->set_pressed(false);
  257. EditorDebuggerNode::get_singleton()->_paused();
  258. DebugAdapterProtocol::get_singleton()->notify_continued();
  259. return prepare_success_response(p_params);
  260. }
  261. Dictionary DebugAdapterParser::req_threads(const Dictionary &p_params) const {
  262. Dictionary response = prepare_success_response(p_params), body;
  263. response["body"] = body;
  264. DAP::Thread thread;
  265. thread.id = 1; // Hardcoded because Godot only supports debugging one thread at the moment
  266. thread.name = "Main";
  267. Array arr = { thread.to_json() };
  268. body["threads"] = arr;
  269. return response;
  270. }
  271. Dictionary DebugAdapterParser::req_stackTrace(const Dictionary &p_params) const {
  272. if (DebugAdapterProtocol::get_singleton()->_processing_stackdump) {
  273. return Dictionary();
  274. }
  275. Dictionary response = prepare_success_response(p_params), body;
  276. response["body"] = body;
  277. bool lines_at_one = DebugAdapterProtocol::get_singleton()->get_current_peer()->linesStartAt1;
  278. bool columns_at_one = DebugAdapterProtocol::get_singleton()->get_current_peer()->columnsStartAt1;
  279. Array arr;
  280. DebugAdapterProtocol *dap = DebugAdapterProtocol::get_singleton();
  281. for (DAP::StackFrame sf : dap->stackframe_list) {
  282. if (!lines_at_one) {
  283. sf.line--;
  284. }
  285. if (!columns_at_one) {
  286. sf.column--;
  287. }
  288. arr.push_back(sf.to_json());
  289. }
  290. body["stackFrames"] = arr;
  291. return response;
  292. }
  293. Dictionary DebugAdapterParser::req_setBreakpoints(const Dictionary &p_params) const {
  294. Dictionary response = prepare_success_response(p_params), body;
  295. response["body"] = body;
  296. Dictionary args = p_params["arguments"];
  297. DAP::Source source;
  298. source.from_json(args["source"]);
  299. bool lines_at_one = DebugAdapterProtocol::get_singleton()->get_current_peer()->linesStartAt1;
  300. if (!is_valid_path(source.path)) {
  301. Dictionary variables;
  302. variables["clientPath"] = source.path;
  303. variables["editorPath"] = ProjectSettings::get_singleton()->get_resource_path();
  304. return prepare_error_response(p_params, DAP::ErrorType::WRONG_PATH, variables);
  305. }
  306. // If path contains \, it's a Windows path, so we need to convert it to /, and make the drive letter uppercase
  307. if (source.path.contains_char('\\')) {
  308. source.path = source.path.replace_char('\\', '/');
  309. source.path = source.path.substr(0, 1).to_upper() + source.path.substr(1);
  310. }
  311. Array breakpoints = args["breakpoints"], lines;
  312. for (int i = 0; i < breakpoints.size(); i++) {
  313. DAP::SourceBreakpoint breakpoint;
  314. breakpoint.from_json(breakpoints[i]);
  315. lines.push_back(breakpoint.line + !lines_at_one);
  316. }
  317. // Always update the source checksum for the requested path, as it might have been modified externally.
  318. DebugAdapterProtocol::get_singleton()->update_source(source.path);
  319. Array updated_breakpoints = DebugAdapterProtocol::get_singleton()->update_breakpoints(source.path, lines);
  320. body["breakpoints"] = updated_breakpoints;
  321. return response;
  322. }
  323. Dictionary DebugAdapterParser::req_breakpointLocations(const Dictionary &p_params) const {
  324. Dictionary response = prepare_success_response(p_params), body;
  325. response["body"] = body;
  326. Dictionary args = p_params["arguments"];
  327. DAP::BreakpointLocation location;
  328. location.line = args["line"];
  329. if (args.has("endLine")) {
  330. location.endLine = args["endLine"];
  331. }
  332. Array locations = { location.to_json() };
  333. body["breakpoints"] = locations;
  334. return response;
  335. }
  336. Dictionary DebugAdapterParser::req_scopes(const Dictionary &p_params) const {
  337. Dictionary response = prepare_success_response(p_params), body;
  338. response["body"] = body;
  339. Dictionary args = p_params["arguments"];
  340. int frame_id = args["frameId"];
  341. Array scope_list;
  342. HashMap<DebugAdapterProtocol::DAPStackFrameID, Vector<int>>::Iterator E = DebugAdapterProtocol::get_singleton()->scope_list.find(frame_id);
  343. if (E) {
  344. const Vector<int> &scope_ids = E->value;
  345. ERR_FAIL_COND_V(scope_ids.size() != 3, prepare_error_response(p_params, DAP::ErrorType::UNKNOWN));
  346. for (int i = 0; i < 3; ++i) {
  347. DAP::Scope scope;
  348. scope.variablesReference = scope_ids[i];
  349. switch (i) {
  350. case 0:
  351. scope.name = "Locals";
  352. scope.presentationHint = "locals";
  353. break;
  354. case 1:
  355. scope.name = "Members";
  356. scope.presentationHint = "members";
  357. break;
  358. case 2:
  359. scope.name = "Globals";
  360. scope.presentationHint = "globals";
  361. }
  362. scope_list.push_back(scope.to_json());
  363. }
  364. }
  365. EditorDebuggerNode::get_singleton()->get_default_debugger()->request_stack_dump(frame_id);
  366. DebugAdapterProtocol::get_singleton()->_current_frame = frame_id;
  367. body["scopes"] = scope_list;
  368. return response;
  369. }
  370. Dictionary DebugAdapterParser::req_variables(const Dictionary &p_params) const {
  371. // If _remaining_vars > 0, the debuggee is still sending a stack dump to the editor.
  372. if (DebugAdapterProtocol::get_singleton()->_remaining_vars > 0) {
  373. return Dictionary();
  374. }
  375. Dictionary args = p_params["arguments"];
  376. int variable_id = args["variablesReference"];
  377. if (HashMap<int, Array>::Iterator E = DebugAdapterProtocol::get_singleton()->variable_list.find(variable_id); E) {
  378. Dictionary response = prepare_success_response(p_params);
  379. Dictionary body;
  380. response["body"] = body;
  381. if (!DebugAdapterProtocol::get_singleton()->get_current_peer()->supportsVariableType) {
  382. for (int i = 0; i < E->value.size(); i++) {
  383. Dictionary variable = E->value[i];
  384. variable.erase("type");
  385. }
  386. }
  387. body["variables"] = E ? E->value : Array();
  388. return response;
  389. } else {
  390. // If the requested variable is an object, it needs to be requested from the debuggee.
  391. ObjectID object_id = DebugAdapterProtocol::get_singleton()->search_object_id(variable_id);
  392. if (object_id.is_null()) {
  393. return prepare_error_response(p_params, DAP::ErrorType::UNKNOWN);
  394. }
  395. DebugAdapterProtocol::get_singleton()->request_remote_object(object_id);
  396. }
  397. return Dictionary();
  398. }
  399. Dictionary DebugAdapterParser::req_next(const Dictionary &p_params) const {
  400. EditorDebuggerNode::get_singleton()->get_default_debugger()->debug_next();
  401. DebugAdapterProtocol::get_singleton()->_stepping = true;
  402. return prepare_success_response(p_params);
  403. }
  404. Dictionary DebugAdapterParser::req_stepIn(const Dictionary &p_params) const {
  405. EditorDebuggerNode::get_singleton()->get_default_debugger()->debug_step();
  406. DebugAdapterProtocol::get_singleton()->_stepping = true;
  407. return prepare_success_response(p_params);
  408. }
  409. Dictionary DebugAdapterParser::req_evaluate(const Dictionary &p_params) const {
  410. Dictionary args = p_params["arguments"];
  411. String expression = args["expression"];
  412. int frame_id = args.has("frameId") ? static_cast<int>(args["frameId"]) : DebugAdapterProtocol::get_singleton()->_current_frame;
  413. if (HashMap<String, DAP::Variable>::Iterator E = DebugAdapterProtocol::get_singleton()->eval_list.find(expression); E) {
  414. Dictionary response = prepare_success_response(p_params);
  415. Dictionary body;
  416. response["body"] = body;
  417. DAP::Variable var = E->value;
  418. body["result"] = var.value;
  419. body["variablesReference"] = var.variablesReference;
  420. // Since an evaluation can alter the state of the debuggee, they are volatile, and should only be used once
  421. DebugAdapterProtocol::get_singleton()->eval_list.erase(E->key);
  422. return response;
  423. } else {
  424. DebugAdapterProtocol::get_singleton()->request_remote_evaluate(expression, frame_id);
  425. }
  426. return Dictionary();
  427. }
  428. Dictionary DebugAdapterParser::req_godot_put_msg(const Dictionary &p_params) const {
  429. Dictionary args = p_params["arguments"];
  430. String msg = args["message"];
  431. Array data = args["data"];
  432. EditorDebuggerNode::get_singleton()->get_default_debugger()->_put_msg(msg, data);
  433. return prepare_success_response(p_params);
  434. }
  435. Dictionary DebugAdapterParser::ev_initialized() const {
  436. Dictionary event = prepare_base_event();
  437. event["event"] = "initialized";
  438. return event;
  439. }
  440. Dictionary DebugAdapterParser::ev_process(const String &p_command) const {
  441. Dictionary event = prepare_base_event(), body;
  442. event["event"] = "process";
  443. event["body"] = body;
  444. body["name"] = OS::get_singleton()->get_executable_path();
  445. body["startMethod"] = p_command;
  446. return event;
  447. }
  448. Dictionary DebugAdapterParser::ev_terminated() const {
  449. Dictionary event = prepare_base_event();
  450. event["event"] = "terminated";
  451. return event;
  452. }
  453. Dictionary DebugAdapterParser::ev_exited(const int &p_exitcode) const {
  454. Dictionary event = prepare_base_event(), body;
  455. event["event"] = "exited";
  456. event["body"] = body;
  457. body["exitCode"] = p_exitcode;
  458. return event;
  459. }
  460. Dictionary DebugAdapterParser::ev_stopped() const {
  461. Dictionary event = prepare_base_event(), body;
  462. event["event"] = "stopped";
  463. event["body"] = body;
  464. body["threadId"] = 1;
  465. return event;
  466. }
  467. Dictionary DebugAdapterParser::ev_stopped_paused() const {
  468. Dictionary event = ev_stopped();
  469. Dictionary body = event["body"];
  470. body["reason"] = "paused";
  471. body["description"] = "Paused";
  472. return event;
  473. }
  474. Dictionary DebugAdapterParser::ev_stopped_exception(const String &p_error) const {
  475. Dictionary event = ev_stopped();
  476. Dictionary body = event["body"];
  477. body["reason"] = "exception";
  478. body["description"] = "Exception";
  479. body["text"] = p_error;
  480. return event;
  481. }
  482. Dictionary DebugAdapterParser::ev_stopped_breakpoint(const int &p_id) const {
  483. Dictionary event = ev_stopped();
  484. Dictionary body = event["body"];
  485. body["reason"] = "breakpoint";
  486. body["description"] = "Breakpoint";
  487. Array breakpoints = { p_id };
  488. body["hitBreakpointIds"] = breakpoints;
  489. return event;
  490. }
  491. Dictionary DebugAdapterParser::ev_stopped_step() const {
  492. Dictionary event = ev_stopped();
  493. Dictionary body = event["body"];
  494. body["reason"] = "step";
  495. body["description"] = "Breakpoint";
  496. return event;
  497. }
  498. Dictionary DebugAdapterParser::ev_continued() const {
  499. Dictionary event = prepare_base_event(), body;
  500. event["event"] = "continued";
  501. event["body"] = body;
  502. body["threadId"] = 1;
  503. return event;
  504. }
  505. Dictionary DebugAdapterParser::ev_output(const String &p_message, RemoteDebugger::MessageType p_type) const {
  506. Dictionary event = prepare_base_event(), body;
  507. event["event"] = "output";
  508. event["body"] = body;
  509. body["category"] = (p_type == RemoteDebugger::MessageType::MESSAGE_TYPE_ERROR) ? "stderr" : "stdout";
  510. body["output"] = p_message + "\r\n";
  511. return event;
  512. }
  513. Dictionary DebugAdapterParser::ev_breakpoint(const DAP::Breakpoint &p_breakpoint, const bool &p_enabled) const {
  514. Dictionary event = prepare_base_event(), body;
  515. event["event"] = "breakpoint";
  516. event["body"] = body;
  517. body["reason"] = p_enabled ? "new" : "removed";
  518. body["breakpoint"] = p_breakpoint.to_json();
  519. return event;
  520. }
  521. Dictionary DebugAdapterParser::ev_custom_data(const String &p_msg, const Array &p_data) const {
  522. Dictionary event = prepare_base_event(), body;
  523. event["event"] = "godot/custom_data";
  524. event["body"] = body;
  525. body["message"] = p_msg;
  526. body["data"] = p_data;
  527. return event;
  528. }