debug_adapter_parser.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*************************************************************************/
  2. /* debug_adapter_parser.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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/editor_debugger_node.h"
  32. #include "editor/debugger/script_editor_debugger.h"
  33. #include "editor/editor_node.h"
  34. void DebugAdapterParser::_bind_methods() {
  35. // Requests
  36. ClassDB::bind_method(D_METHOD("req_initialize", "params"), &DebugAdapterParser::req_initialize);
  37. ClassDB::bind_method(D_METHOD("req_disconnect", "params"), &DebugAdapterParser::prepare_success_response);
  38. ClassDB::bind_method(D_METHOD("req_launch", "params"), &DebugAdapterParser::req_launch);
  39. ClassDB::bind_method(D_METHOD("req_terminate", "params"), &DebugAdapterParser::req_terminate);
  40. ClassDB::bind_method(D_METHOD("req_configurationDone", "params"), &DebugAdapterParser::prepare_success_response);
  41. ClassDB::bind_method(D_METHOD("req_pause", "params"), &DebugAdapterParser::req_pause);
  42. ClassDB::bind_method(D_METHOD("req_continue", "params"), &DebugAdapterParser::req_continue);
  43. ClassDB::bind_method(D_METHOD("req_threads", "params"), &DebugAdapterParser::req_threads);
  44. ClassDB::bind_method(D_METHOD("req_stackTrace", "params"), &DebugAdapterParser::req_stackTrace);
  45. ClassDB::bind_method(D_METHOD("req_setBreakpoints", "params"), &DebugAdapterParser::req_setBreakpoints);
  46. ClassDB::bind_method(D_METHOD("req_scopes", "params"), &DebugAdapterParser::req_scopes);
  47. ClassDB::bind_method(D_METHOD("req_variables", "params"), &DebugAdapterParser::req_variables);
  48. ClassDB::bind_method(D_METHOD("req_next", "params"), &DebugAdapterParser::req_next);
  49. ClassDB::bind_method(D_METHOD("req_stepIn", "params"), &DebugAdapterParser::req_stepIn);
  50. }
  51. Dictionary DebugAdapterParser::prepare_base_event() const {
  52. Dictionary event;
  53. event["type"] = "event";
  54. return event;
  55. }
  56. Dictionary DebugAdapterParser::prepare_success_response(const Dictionary &p_params) const {
  57. Dictionary response;
  58. response["type"] = "response";
  59. response["request_seq"] = p_params["seq"];
  60. response["command"] = p_params["command"];
  61. response["success"] = true;
  62. return response;
  63. }
  64. Dictionary DebugAdapterParser::prepare_error_response(const Dictionary &p_params, DAP::ErrorType err_type, const Dictionary &variables) const {
  65. Dictionary response, body;
  66. response["type"] = "response";
  67. response["request_seq"] = p_params["seq"];
  68. response["command"] = p_params["command"];
  69. response["success"] = false;
  70. response["body"] = body;
  71. DAP::Message message;
  72. String error, error_desc;
  73. switch (err_type) {
  74. case DAP::ErrorType::UNKNOWN:
  75. error = "unknown";
  76. error_desc = "An unknown error has ocurred when processing the request.";
  77. break;
  78. case DAP::ErrorType::WRONG_PATH:
  79. error = "wrong_path";
  80. error_desc = "The editor and client are working on different paths; the client is on \"{clientPath}\", but the editor is on \"{editorPath}\"";
  81. }
  82. message.id = err_type;
  83. message.format = error_desc;
  84. message.variables = variables;
  85. response["message"] = error;
  86. body["error"] = message.to_json();
  87. return response;
  88. }
  89. Dictionary DebugAdapterParser::req_initialize(const Dictionary &p_params) const {
  90. Dictionary response = prepare_success_response(p_params);
  91. Dictionary args = p_params["arguments"];
  92. Ref<DAPeer> peer = DebugAdapterProtocol::get_singleton()->get_current_peer();
  93. peer->linesStartAt1 = args.get("linesStartAt1", false);
  94. peer->columnsStartAt1 = args.get("columnsStartAt1", false);
  95. peer->supportsVariableType = args.get("supportsVariableType", false);
  96. peer->supportsInvalidatedEvent = args.get("supportsInvalidatedEvent", false);
  97. DAP::Capabilities caps;
  98. response["body"] = caps.to_json();
  99. DebugAdapterProtocol::get_singleton()->notify_initialized();
  100. return response;
  101. }
  102. Dictionary DebugAdapterParser::req_launch(const Dictionary &p_params) {
  103. Dictionary args = p_params["arguments"];
  104. if (args.has("project") && !is_valid_path(args["project"])) {
  105. Dictionary variables;
  106. variables["clientPath"] = args["project"];
  107. variables["editorPath"] = ProjectSettings::get_singleton()->get_resource_path();
  108. return prepare_error_response(p_params, DAP::ErrorType::WRONG_PATH, variables);
  109. }
  110. ScriptEditorDebugger *dbg = EditorDebuggerNode::get_singleton()->get_default_debugger();
  111. if ((bool)args["noDebug"] != dbg->is_skip_breakpoints()) {
  112. dbg->debug_skip_breakpoints();
  113. }
  114. EditorNode::get_singleton()->run_play();
  115. DebugAdapterProtocol::get_singleton()->notify_process();
  116. return prepare_success_response(p_params);
  117. }
  118. Dictionary DebugAdapterParser::req_terminate(const Dictionary &p_params) const {
  119. EditorNode::get_singleton()->run_stop();
  120. return prepare_success_response(p_params);
  121. }
  122. Dictionary DebugAdapterParser::req_pause(const Dictionary &p_params) const {
  123. EditorNode::get_singleton()->get_pause_button()->set_pressed(true);
  124. EditorDebuggerNode::get_singleton()->_paused();
  125. DebugAdapterProtocol::get_singleton()->notify_stopped_paused();
  126. return prepare_success_response(p_params);
  127. }
  128. Dictionary DebugAdapterParser::req_continue(const Dictionary &p_params) const {
  129. EditorNode::get_singleton()->get_pause_button()->set_pressed(false);
  130. EditorDebuggerNode::get_singleton()->_paused();
  131. DebugAdapterProtocol::get_singleton()->notify_continued();
  132. return prepare_success_response(p_params);
  133. }
  134. Dictionary DebugAdapterParser::req_threads(const Dictionary &p_params) const {
  135. Dictionary response = prepare_success_response(p_params), body;
  136. response["body"] = body;
  137. Array arr;
  138. DAP::Thread thread;
  139. thread.id = 1; // Hardcoded because Godot only supports debugging one thread at the moment
  140. thread.name = "Main";
  141. arr.push_back(thread.to_json());
  142. body["threads"] = arr;
  143. return response;
  144. }
  145. Dictionary DebugAdapterParser::req_stackTrace(const Dictionary &p_params) const {
  146. if (DebugAdapterProtocol::get_singleton()->_processing_stackdump) {
  147. return Dictionary();
  148. }
  149. Dictionary response = prepare_success_response(p_params), body;
  150. response["body"] = body;
  151. bool lines_at_one = DebugAdapterProtocol::get_singleton()->get_current_peer()->linesStartAt1;
  152. bool columns_at_one = DebugAdapterProtocol::get_singleton()->get_current_peer()->columnsStartAt1;
  153. Array arr;
  154. DebugAdapterProtocol *dap = DebugAdapterProtocol::get_singleton();
  155. for (Map<DAP::StackFrame, List<int>>::Element *E = dap->stackframe_list.front(); E; E = E->next()) {
  156. DAP::StackFrame sf = E->key();
  157. if (!lines_at_one) {
  158. sf.line--;
  159. }
  160. if (!columns_at_one) {
  161. sf.column--;
  162. }
  163. arr.push_back(sf.to_json());
  164. }
  165. body["stackFrames"] = arr;
  166. return response;
  167. }
  168. Dictionary DebugAdapterParser::req_setBreakpoints(const Dictionary &p_params) {
  169. Dictionary response = prepare_success_response(p_params), body;
  170. response["body"] = body;
  171. Dictionary args = p_params["arguments"];
  172. DAP::Source source;
  173. source.from_json(args["source"]);
  174. bool lines_at_one = DebugAdapterProtocol::get_singleton()->get_current_peer()->linesStartAt1;
  175. if (!is_valid_path(source.path)) {
  176. Dictionary variables;
  177. variables["clientPath"] = source.path;
  178. variables["editorPath"] = ProjectSettings::get_singleton()->get_resource_path();
  179. return prepare_error_response(p_params, DAP::ErrorType::WRONG_PATH, variables);
  180. }
  181. Array breakpoints = args["breakpoints"], lines;
  182. for (int i = 0; i < breakpoints.size(); i++) {
  183. DAP::SourceBreakpoint breakpoint;
  184. breakpoint.from_json(breakpoints[i]);
  185. lines.push_back(breakpoint.line + !lines_at_one);
  186. }
  187. EditorDebuggerNode::get_singleton()->set_breakpoints(ProjectSettings::get_singleton()->localize_path(source.path), lines);
  188. Array updated_breakpoints = DebugAdapterProtocol::get_singleton()->update_breakpoints(source.path, lines);
  189. body["breakpoints"] = updated_breakpoints;
  190. return response;
  191. }
  192. Dictionary DebugAdapterParser::req_scopes(const Dictionary &p_params) {
  193. Dictionary response = prepare_success_response(p_params), body;
  194. response["body"] = body;
  195. Dictionary args = p_params["arguments"];
  196. int frame_id = args["frameId"];
  197. Array scope_list;
  198. DAP::StackFrame frame;
  199. frame.id = frame_id;
  200. Map<DAP::StackFrame, List<int>>::Element *E = DebugAdapterProtocol::get_singleton()->stackframe_list.find(frame);
  201. if (E) {
  202. ERR_FAIL_COND_V(E->value().size() != 3, prepare_error_response(p_params, DAP::ErrorType::UNKNOWN));
  203. for (int i = 0; i < 3; i++) {
  204. DAP::Scope scope;
  205. scope.variablesReference = E->value()[i];
  206. switch (i) {
  207. case 0:
  208. scope.name = "Locals";
  209. scope.presentationHint = "locals";
  210. break;
  211. case 1:
  212. scope.name = "Members";
  213. scope.presentationHint = "members";
  214. break;
  215. case 2:
  216. scope.name = "Globals";
  217. scope.presentationHint = "globals";
  218. }
  219. scope_list.push_back(scope.to_json());
  220. }
  221. }
  222. EditorDebuggerNode::get_singleton()->get_default_debugger()->request_stack_dump(frame_id);
  223. DebugAdapterProtocol::get_singleton()->_current_frame = frame_id;
  224. body["scopes"] = scope_list;
  225. return response;
  226. }
  227. Dictionary DebugAdapterParser::req_variables(const Dictionary &p_params) const {
  228. // If _remaining_vars > 0, the debugee is still sending a stack dump to the editor.
  229. if (DebugAdapterProtocol::get_singleton()->_remaining_vars > 0) {
  230. return Dictionary();
  231. }
  232. Dictionary response = prepare_success_response(p_params), body;
  233. response["body"] = body;
  234. Dictionary args = p_params["arguments"];
  235. int variable_id = args["variablesReference"];
  236. Map<int, Array>::Element *E = DebugAdapterProtocol::get_singleton()->variable_list.find(variable_id);
  237. if (E) {
  238. body["variables"] = E ? E->value() : Array();
  239. return response;
  240. } else {
  241. return Dictionary();
  242. }
  243. }
  244. Dictionary DebugAdapterParser::req_next(const Dictionary &p_params) const {
  245. EditorDebuggerNode::get_singleton()->get_default_debugger()->debug_next();
  246. DebugAdapterProtocol::get_singleton()->_stepping = true;
  247. return prepare_success_response(p_params);
  248. }
  249. Dictionary DebugAdapterParser::req_stepIn(const Dictionary &p_params) const {
  250. EditorDebuggerNode::get_singleton()->get_default_debugger()->debug_step();
  251. DebugAdapterProtocol::get_singleton()->_stepping = true;
  252. return prepare_success_response(p_params);
  253. }
  254. Dictionary DebugAdapterParser::ev_initialized() const {
  255. Dictionary event = prepare_base_event();
  256. event["event"] = "initialized";
  257. return event;
  258. }
  259. Dictionary DebugAdapterParser::ev_process(const String &p_command) const {
  260. Dictionary event = prepare_base_event(), body;
  261. event["event"] = "process";
  262. event["body"] = body;
  263. body["name"] = OS::get_singleton()->get_executable_path();
  264. body["startMethod"] = p_command;
  265. return event;
  266. }
  267. Dictionary DebugAdapterParser::ev_terminated() const {
  268. Dictionary event = prepare_base_event();
  269. event["event"] = "terminated";
  270. return event;
  271. }
  272. Dictionary DebugAdapterParser::ev_exited(const int &p_exitcode) const {
  273. Dictionary event = prepare_base_event(), body;
  274. event["event"] = "exited";
  275. event["body"] = body;
  276. body["exitCode"] = p_exitcode;
  277. return event;
  278. }
  279. Dictionary DebugAdapterParser::ev_stopped() const {
  280. Dictionary event = prepare_base_event(), body;
  281. event["event"] = "stopped";
  282. event["body"] = body;
  283. body["threadId"] = 1;
  284. return event;
  285. }
  286. Dictionary DebugAdapterParser::ev_stopped_paused() const {
  287. Dictionary event = ev_stopped();
  288. Dictionary body = event["body"];
  289. body["reason"] = "paused";
  290. body["description"] = "Paused";
  291. return event;
  292. }
  293. Dictionary DebugAdapterParser::ev_stopped_exception(const String &p_error) const {
  294. Dictionary event = ev_stopped();
  295. Dictionary body = event["body"];
  296. body["reason"] = "exception";
  297. body["description"] = "Exception";
  298. body["text"] = p_error;
  299. return event;
  300. }
  301. Dictionary DebugAdapterParser::ev_stopped_breakpoint(const int &p_id) const {
  302. Dictionary event = ev_stopped();
  303. Dictionary body = event["body"];
  304. body["reason"] = "breakpoint";
  305. body["description"] = "Breakpoint";
  306. Array breakpoints;
  307. breakpoints.push_back(p_id);
  308. body["hitBreakpointIds"] = breakpoints;
  309. return event;
  310. }
  311. Dictionary DebugAdapterParser::ev_stopped_step() const {
  312. Dictionary event = ev_stopped();
  313. Dictionary body = event["body"];
  314. body["reason"] = "step";
  315. body["description"] = "Breakpoint";
  316. return event;
  317. }
  318. Dictionary DebugAdapterParser::ev_continued() const {
  319. Dictionary event = prepare_base_event(), body;
  320. event["event"] = "continued";
  321. event["body"] = body;
  322. body["threadId"] = 1;
  323. return event;
  324. }
  325. Dictionary DebugAdapterParser::ev_output(const String &p_message) const {
  326. Dictionary event = prepare_base_event(), body;
  327. event["event"] = "output";
  328. event["body"] = body;
  329. body["category"] = "stdout";
  330. body["output"] = p_message + "\r\n";
  331. return event;
  332. }