debug_adapter_protocol.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  1. /**************************************************************************/
  2. /* debug_adapter_protocol.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_protocol.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/debugger/debugger_marshalls.h"
  33. #include "core/io/json.h"
  34. #include "editor/debugger/script_editor_debugger.h"
  35. #include "editor/doc_tools.h"
  36. #include "editor/editor_log.h"
  37. #include "editor/editor_node.h"
  38. #include "editor/editor_settings.h"
  39. #include "editor/gui/editor_run_bar.h"
  40. DebugAdapterProtocol *DebugAdapterProtocol::singleton = nullptr;
  41. Error DAPeer::handle_data() {
  42. int read = 0;
  43. // Read headers
  44. if (!has_header) {
  45. if (!connection->get_available_bytes()) {
  46. return OK;
  47. }
  48. while (true) {
  49. if (req_pos >= DAP_MAX_BUFFER_SIZE) {
  50. req_pos = 0;
  51. ERR_FAIL_V_MSG(ERR_OUT_OF_MEMORY, "Response header too big");
  52. }
  53. Error err = connection->get_partial_data(&req_buf[req_pos], 1, read);
  54. if (err != OK) {
  55. return FAILED;
  56. } else if (read != 1) { // Busy, wait until next poll
  57. return ERR_BUSY;
  58. }
  59. char *r = (char *)req_buf;
  60. int l = req_pos;
  61. // End of headers
  62. if (l > 3 && r[l] == '\n' && r[l - 1] == '\r' && r[l - 2] == '\n' && r[l - 3] == '\r') {
  63. r[l - 3] = '\0'; // Null terminate to read string
  64. String header;
  65. header.parse_utf8(r);
  66. content_length = header.substr(16).to_int();
  67. has_header = true;
  68. req_pos = 0;
  69. break;
  70. }
  71. req_pos++;
  72. }
  73. }
  74. if (has_header) {
  75. while (req_pos < content_length) {
  76. if (content_length >= DAP_MAX_BUFFER_SIZE) {
  77. req_pos = 0;
  78. has_header = false;
  79. ERR_FAIL_COND_V_MSG(req_pos >= DAP_MAX_BUFFER_SIZE, ERR_OUT_OF_MEMORY, "Response content too big");
  80. }
  81. Error err = connection->get_partial_data(&req_buf[req_pos], content_length - req_pos, read);
  82. if (err != OK) {
  83. return FAILED;
  84. } else if (read < content_length - req_pos) {
  85. return ERR_BUSY;
  86. }
  87. req_pos += read;
  88. }
  89. // Parse data
  90. String msg;
  91. msg.parse_utf8((const char *)req_buf, req_pos);
  92. // Apply a timestamp if it there's none yet
  93. if (!timestamp) {
  94. timestamp = OS::get_singleton()->get_ticks_msec();
  95. }
  96. // Response
  97. if (DebugAdapterProtocol::get_singleton()->process_message(msg)) {
  98. // Reset to read again
  99. req_pos = 0;
  100. has_header = false;
  101. timestamp = 0;
  102. }
  103. }
  104. return OK;
  105. }
  106. Error DAPeer::send_data() {
  107. while (res_queue.size()) {
  108. Dictionary data = res_queue.front()->get();
  109. if (!data.has("seq")) {
  110. data["seq"] = ++seq;
  111. }
  112. String formatted_data = format_output(data);
  113. int data_sent = 0;
  114. while (data_sent < formatted_data.length()) {
  115. int curr_sent = 0;
  116. Error err = connection->put_partial_data((const uint8_t *)formatted_data.utf8().get_data(), formatted_data.size() - data_sent - 1, curr_sent);
  117. if (err != OK) {
  118. return err;
  119. }
  120. data_sent += curr_sent;
  121. }
  122. res_queue.pop_front();
  123. }
  124. return OK;
  125. }
  126. String DAPeer::format_output(const Dictionary &p_params) const {
  127. String response = Variant(p_params).to_json_string();
  128. String header = "Content-Length: ";
  129. CharString charstr = response.utf8();
  130. size_t len = charstr.length();
  131. header += itos(len);
  132. header += "\r\n\r\n";
  133. return header + response;
  134. }
  135. Error DebugAdapterProtocol::on_client_connected() {
  136. ERR_FAIL_COND_V_MSG(clients.size() >= DAP_MAX_CLIENTS, FAILED, "Max client limits reached");
  137. Ref<StreamPeerTCP> tcp_peer = server->take_connection();
  138. tcp_peer->set_no_delay(true);
  139. Ref<DAPeer> peer = memnew(DAPeer);
  140. peer->connection = tcp_peer;
  141. clients.push_back(peer);
  142. EditorDebuggerNode::get_singleton()->get_default_debugger()->set_move_to_foreground(false);
  143. EditorNode::get_log()->add_message("[DAP] Connection Taken", EditorLog::MSG_TYPE_EDITOR);
  144. return OK;
  145. }
  146. void DebugAdapterProtocol::on_client_disconnected(const Ref<DAPeer> &p_peer) {
  147. clients.erase(p_peer);
  148. if (!clients.size()) {
  149. reset_ids();
  150. EditorDebuggerNode::get_singleton()->get_default_debugger()->set_move_to_foreground(true);
  151. }
  152. EditorNode::get_log()->add_message("[DAP] Disconnected", EditorLog::MSG_TYPE_EDITOR);
  153. }
  154. void DebugAdapterProtocol::reset_current_info() {
  155. _current_request = "";
  156. _current_peer.unref();
  157. }
  158. void DebugAdapterProtocol::reset_ids() {
  159. breakpoint_id = 0;
  160. breakpoint_list.clear();
  161. reset_stack_info();
  162. }
  163. void DebugAdapterProtocol::reset_stack_info() {
  164. stackframe_id = 0;
  165. variable_id = 1;
  166. stackframe_list.clear();
  167. variable_list.clear();
  168. }
  169. int DebugAdapterProtocol::parse_variant(const Variant &p_var) {
  170. switch (p_var.get_type()) {
  171. case Variant::VECTOR2:
  172. case Variant::VECTOR2I: {
  173. int id = variable_id++;
  174. Vector2 vec = p_var;
  175. const String type_scalar = Variant::get_type_name(p_var.get_type() == Variant::VECTOR2 ? Variant::FLOAT : Variant::INT);
  176. DAP::Variable x, y;
  177. x.name = "x";
  178. y.name = "y";
  179. x.type = type_scalar;
  180. y.type = type_scalar;
  181. x.value = rtos(vec.x);
  182. y.value = rtos(vec.y);
  183. Array arr;
  184. arr.push_back(x.to_json());
  185. arr.push_back(y.to_json());
  186. variable_list.insert(id, arr);
  187. return id;
  188. }
  189. case Variant::RECT2:
  190. case Variant::RECT2I: {
  191. int id = variable_id++;
  192. Rect2 rect = p_var;
  193. const String type_scalar = Variant::get_type_name(p_var.get_type() == Variant::RECT2 ? Variant::FLOAT : Variant::INT);
  194. DAP::Variable x, y, w, h;
  195. x.name = "x";
  196. y.name = "y";
  197. w.name = "w";
  198. h.name = "h";
  199. x.type = type_scalar;
  200. y.type = type_scalar;
  201. w.type = type_scalar;
  202. h.type = type_scalar;
  203. x.value = rtos(rect.position.x);
  204. y.value = rtos(rect.position.y);
  205. w.value = rtos(rect.size.x);
  206. h.value = rtos(rect.size.y);
  207. Array arr;
  208. arr.push_back(x.to_json());
  209. arr.push_back(y.to_json());
  210. arr.push_back(w.to_json());
  211. arr.push_back(h.to_json());
  212. variable_list.insert(id, arr);
  213. return id;
  214. }
  215. case Variant::VECTOR3:
  216. case Variant::VECTOR3I: {
  217. int id = variable_id++;
  218. Vector3 vec = p_var;
  219. const String type_scalar = Variant::get_type_name(p_var.get_type() == Variant::VECTOR3 ? Variant::FLOAT : Variant::INT);
  220. DAP::Variable x, y, z;
  221. x.name = "x";
  222. y.name = "y";
  223. z.name = "z";
  224. x.type = type_scalar;
  225. y.type = type_scalar;
  226. z.type = type_scalar;
  227. x.value = rtos(vec.x);
  228. y.value = rtos(vec.y);
  229. z.value = rtos(vec.z);
  230. Array arr;
  231. arr.push_back(x.to_json());
  232. arr.push_back(y.to_json());
  233. arr.push_back(z.to_json());
  234. variable_list.insert(id, arr);
  235. return id;
  236. }
  237. case Variant::TRANSFORM2D: {
  238. int id = variable_id++;
  239. Transform2D transform = p_var;
  240. const String type_vec2 = Variant::get_type_name(Variant::VECTOR2);
  241. DAP::Variable x, y, origin;
  242. x.name = "x";
  243. y.name = "y";
  244. origin.name = "origin";
  245. x.type = type_vec2;
  246. y.type = type_vec2;
  247. origin.type = type_vec2;
  248. x.value = transform.columns[0];
  249. y.value = transform.columns[1];
  250. origin.value = transform.columns[2];
  251. x.variablesReference = parse_variant(transform.columns[0]);
  252. y.variablesReference = parse_variant(transform.columns[1]);
  253. origin.variablesReference = parse_variant(transform.columns[2]);
  254. Array arr;
  255. arr.push_back(x.to_json());
  256. arr.push_back(y.to_json());
  257. arr.push_back(origin.to_json());
  258. variable_list.insert(id, arr);
  259. return id;
  260. }
  261. case Variant::PLANE: {
  262. int id = variable_id++;
  263. Plane plane = p_var;
  264. DAP::Variable d, normal;
  265. d.name = "d";
  266. normal.name = "normal";
  267. d.type = Variant::get_type_name(Variant::FLOAT);
  268. normal.type = Variant::get_type_name(Variant::VECTOR3);
  269. d.value = rtos(plane.d);
  270. normal.value = plane.normal;
  271. normal.variablesReference = parse_variant(plane.normal);
  272. Array arr;
  273. arr.push_back(d.to_json());
  274. arr.push_back(normal.to_json());
  275. variable_list.insert(id, arr);
  276. return id;
  277. }
  278. case Variant::QUATERNION: {
  279. int id = variable_id++;
  280. Quaternion quat = p_var;
  281. const String type_float = Variant::get_type_name(Variant::FLOAT);
  282. DAP::Variable x, y, z, w;
  283. x.name = "x";
  284. y.name = "y";
  285. z.name = "z";
  286. w.name = "w";
  287. x.type = type_float;
  288. y.type = type_float;
  289. z.type = type_float;
  290. w.type = type_float;
  291. x.value = rtos(quat.x);
  292. y.value = rtos(quat.y);
  293. z.value = rtos(quat.z);
  294. w.value = rtos(quat.w);
  295. Array arr;
  296. arr.push_back(x.to_json());
  297. arr.push_back(y.to_json());
  298. arr.push_back(z.to_json());
  299. arr.push_back(w.to_json());
  300. variable_list.insert(id, arr);
  301. return id;
  302. }
  303. case Variant::AABB: {
  304. int id = variable_id++;
  305. AABB aabb = p_var;
  306. const String type_vec3 = Variant::get_type_name(Variant::VECTOR3);
  307. DAP::Variable position, size;
  308. position.name = "position";
  309. size.name = "size";
  310. position.type = type_vec3;
  311. size.type = type_vec3;
  312. position.value = aabb.position;
  313. size.value = aabb.size;
  314. position.variablesReference = parse_variant(aabb.position);
  315. size.variablesReference = parse_variant(aabb.size);
  316. Array arr;
  317. arr.push_back(position.to_json());
  318. arr.push_back(size.to_json());
  319. variable_list.insert(id, arr);
  320. return id;
  321. }
  322. case Variant::BASIS: {
  323. int id = variable_id++;
  324. Basis basis = p_var;
  325. const String type_vec3 = Variant::get_type_name(Variant::VECTOR3);
  326. DAP::Variable x, y, z;
  327. x.name = "x";
  328. y.name = "y";
  329. z.name = "z";
  330. x.type = type_vec3;
  331. y.type = type_vec3;
  332. z.type = type_vec3;
  333. x.value = basis.rows[0];
  334. y.value = basis.rows[1];
  335. z.value = basis.rows[2];
  336. x.variablesReference = parse_variant(basis.rows[0]);
  337. y.variablesReference = parse_variant(basis.rows[1]);
  338. z.variablesReference = parse_variant(basis.rows[2]);
  339. Array arr;
  340. arr.push_back(x.to_json());
  341. arr.push_back(y.to_json());
  342. arr.push_back(z.to_json());
  343. variable_list.insert(id, arr);
  344. return id;
  345. }
  346. case Variant::TRANSFORM3D: {
  347. int id = variable_id++;
  348. Transform3D transform = p_var;
  349. DAP::Variable basis, origin;
  350. basis.name = "basis";
  351. origin.name = "origin";
  352. basis.type = Variant::get_type_name(Variant::BASIS);
  353. origin.type = Variant::get_type_name(Variant::VECTOR3);
  354. basis.value = transform.basis;
  355. origin.value = transform.origin;
  356. basis.variablesReference = parse_variant(transform.basis);
  357. origin.variablesReference = parse_variant(transform.origin);
  358. Array arr;
  359. arr.push_back(basis.to_json());
  360. arr.push_back(origin.to_json());
  361. variable_list.insert(id, arr);
  362. return id;
  363. }
  364. case Variant::COLOR: {
  365. int id = variable_id++;
  366. Color color = p_var;
  367. const String type_float = Variant::get_type_name(Variant::FLOAT);
  368. DAP::Variable r, g, b, a;
  369. r.name = "r";
  370. g.name = "g";
  371. b.name = "b";
  372. a.name = "a";
  373. r.type = type_float;
  374. g.type = type_float;
  375. b.type = type_float;
  376. a.type = type_float;
  377. r.value = rtos(color.r);
  378. g.value = rtos(color.g);
  379. b.value = rtos(color.b);
  380. a.value = rtos(color.a);
  381. Array arr;
  382. arr.push_back(r.to_json());
  383. arr.push_back(g.to_json());
  384. arr.push_back(b.to_json());
  385. arr.push_back(a.to_json());
  386. variable_list.insert(id, arr);
  387. return id;
  388. }
  389. case Variant::ARRAY: {
  390. int id = variable_id++;
  391. Array array = p_var;
  392. DAP::Variable size;
  393. size.name = "size";
  394. size.type = Variant::get_type_name(Variant::INT);
  395. size.value = itos(array.size());
  396. Array arr;
  397. arr.push_back(size.to_json());
  398. for (int i = 0; i < array.size(); i++) {
  399. DAP::Variable var;
  400. var.name = itos(i);
  401. var.type = Variant::get_type_name(array[i].get_type());
  402. var.value = array[i];
  403. var.variablesReference = parse_variant(array[i]);
  404. arr.push_back(var.to_json());
  405. }
  406. variable_list.insert(id, arr);
  407. return id;
  408. }
  409. case Variant::DICTIONARY: {
  410. int id = variable_id++;
  411. Dictionary dictionary = p_var;
  412. Array arr;
  413. for (int i = 0; i < dictionary.size(); i++) {
  414. DAP::Variable var;
  415. var.name = dictionary.get_key_at_index(i);
  416. Variant value = dictionary.get_value_at_index(i);
  417. var.type = Variant::get_type_name(value.get_type());
  418. var.value = value;
  419. var.variablesReference = parse_variant(value);
  420. arr.push_back(var.to_json());
  421. }
  422. variable_list.insert(id, arr);
  423. return id;
  424. }
  425. case Variant::PACKED_BYTE_ARRAY: {
  426. int id = variable_id++;
  427. PackedByteArray array = p_var;
  428. DAP::Variable size;
  429. size.name = "size";
  430. size.type = Variant::get_type_name(Variant::INT);
  431. size.value = itos(array.size());
  432. Array arr;
  433. arr.push_back(size.to_json());
  434. for (int i = 0; i < array.size(); i++) {
  435. DAP::Variable var;
  436. var.name = itos(i);
  437. var.type = "byte";
  438. var.value = itos(array[i]);
  439. arr.push_back(var.to_json());
  440. }
  441. variable_list.insert(id, arr);
  442. return id;
  443. }
  444. case Variant::PACKED_INT32_ARRAY: {
  445. int id = variable_id++;
  446. PackedInt32Array array = p_var;
  447. DAP::Variable size;
  448. size.name = "size";
  449. size.type = Variant::get_type_name(Variant::INT);
  450. size.value = itos(array.size());
  451. Array arr;
  452. arr.push_back(size.to_json());
  453. for (int i = 0; i < array.size(); i++) {
  454. DAP::Variable var;
  455. var.name = itos(i);
  456. var.type = "int";
  457. var.value = itos(array[i]);
  458. arr.push_back(var.to_json());
  459. }
  460. variable_list.insert(id, arr);
  461. return id;
  462. }
  463. case Variant::PACKED_INT64_ARRAY: {
  464. int id = variable_id++;
  465. PackedInt64Array array = p_var;
  466. DAP::Variable size;
  467. size.name = "size";
  468. size.type = Variant::get_type_name(Variant::INT);
  469. size.value = itos(array.size());
  470. Array arr;
  471. arr.push_back(size.to_json());
  472. for (int i = 0; i < array.size(); i++) {
  473. DAP::Variable var;
  474. var.name = itos(i);
  475. var.type = "long";
  476. var.value = itos(array[i]);
  477. arr.push_back(var.to_json());
  478. }
  479. variable_list.insert(id, arr);
  480. return id;
  481. }
  482. case Variant::PACKED_FLOAT32_ARRAY: {
  483. int id = variable_id++;
  484. PackedFloat32Array array = p_var;
  485. DAP::Variable size;
  486. size.name = "size";
  487. size.type = Variant::get_type_name(Variant::INT);
  488. size.value = itos(array.size());
  489. Array arr;
  490. arr.push_back(size.to_json());
  491. for (int i = 0; i < array.size(); i++) {
  492. DAP::Variable var;
  493. var.name = itos(i);
  494. var.type = "float";
  495. var.value = rtos(array[i]);
  496. arr.push_back(var.to_json());
  497. }
  498. variable_list.insert(id, arr);
  499. return id;
  500. }
  501. case Variant::PACKED_FLOAT64_ARRAY: {
  502. int id = variable_id++;
  503. PackedFloat64Array array = p_var;
  504. DAP::Variable size;
  505. size.name = "size";
  506. size.type = Variant::get_type_name(Variant::INT);
  507. size.value = itos(array.size());
  508. Array arr;
  509. arr.push_back(size.to_json());
  510. for (int i = 0; i < array.size(); i++) {
  511. DAP::Variable var;
  512. var.name = itos(i);
  513. var.type = "double";
  514. var.value = rtos(array[i]);
  515. arr.push_back(var.to_json());
  516. }
  517. variable_list.insert(id, arr);
  518. return id;
  519. }
  520. case Variant::PACKED_STRING_ARRAY: {
  521. int id = variable_id++;
  522. PackedStringArray array = p_var;
  523. DAP::Variable size;
  524. size.name = "size";
  525. size.type = Variant::get_type_name(Variant::INT);
  526. size.value = itos(array.size());
  527. Array arr;
  528. arr.push_back(size.to_json());
  529. for (int i = 0; i < array.size(); i++) {
  530. DAP::Variable var;
  531. var.name = itos(i);
  532. var.type = Variant::get_type_name(Variant::STRING);
  533. var.value = array[i];
  534. arr.push_back(var.to_json());
  535. }
  536. variable_list.insert(id, arr);
  537. return id;
  538. }
  539. case Variant::PACKED_VECTOR2_ARRAY: {
  540. int id = variable_id++;
  541. PackedVector2Array array = p_var;
  542. DAP::Variable size;
  543. size.name = "size";
  544. size.type = Variant::get_type_name(Variant::INT);
  545. size.value = itos(array.size());
  546. Array arr;
  547. arr.push_back(size.to_json());
  548. for (int i = 0; i < array.size(); i++) {
  549. DAP::Variable var;
  550. var.name = itos(i);
  551. var.type = Variant::get_type_name(Variant::VECTOR2);
  552. var.value = array[i];
  553. var.variablesReference = parse_variant(array[i]);
  554. arr.push_back(var.to_json());
  555. }
  556. variable_list.insert(id, arr);
  557. return id;
  558. }
  559. case Variant::PACKED_VECTOR3_ARRAY: {
  560. int id = variable_id++;
  561. PackedVector3Array array = p_var;
  562. DAP::Variable size;
  563. size.name = "size";
  564. size.type = Variant::get_type_name(Variant::INT);
  565. size.value = itos(array.size());
  566. Array arr;
  567. arr.push_back(size.to_json());
  568. for (int i = 0; i < array.size(); i++) {
  569. DAP::Variable var;
  570. var.name = itos(i);
  571. var.type = Variant::get_type_name(Variant::VECTOR3);
  572. var.value = array[i];
  573. var.variablesReference = parse_variant(array[i]);
  574. arr.push_back(var.to_json());
  575. }
  576. variable_list.insert(id, arr);
  577. return id;
  578. }
  579. case Variant::PACKED_COLOR_ARRAY: {
  580. int id = variable_id++;
  581. PackedColorArray array = p_var;
  582. DAP::Variable size;
  583. size.name = "size";
  584. size.type = Variant::get_type_name(Variant::INT);
  585. size.value = itos(array.size());
  586. Array arr;
  587. arr.push_back(size.to_json());
  588. for (int i = 0; i < array.size(); i++) {
  589. DAP::Variable var;
  590. var.name = itos(i);
  591. var.type = Variant::get_type_name(Variant::COLOR);
  592. var.value = array[i];
  593. var.variablesReference = parse_variant(array[i]);
  594. arr.push_back(var.to_json());
  595. }
  596. variable_list.insert(id, arr);
  597. return id;
  598. }
  599. case Variant::PACKED_VECTOR4_ARRAY: {
  600. int id = variable_id++;
  601. PackedVector4Array array = p_var;
  602. DAP::Variable size;
  603. size.name = "size";
  604. size.type = Variant::get_type_name(Variant::INT);
  605. size.value = itos(array.size());
  606. Array arr;
  607. arr.push_back(size.to_json());
  608. for (int i = 0; i < array.size(); i++) {
  609. DAP::Variable var;
  610. var.name = itos(i);
  611. var.type = Variant::get_type_name(Variant::VECTOR4);
  612. var.value = array[i];
  613. var.variablesReference = parse_variant(array[i]);
  614. arr.push_back(var.to_json());
  615. }
  616. variable_list.insert(id, arr);
  617. return id;
  618. }
  619. default:
  620. // Simple atomic stuff, or too complex to be manipulated
  621. return 0;
  622. }
  623. }
  624. bool DebugAdapterProtocol::process_message(const String &p_text) {
  625. JSON json;
  626. ERR_FAIL_COND_V_MSG(json.parse(p_text) != OK, true, "Malformed message!");
  627. Dictionary params = json.get_data();
  628. bool completed = true;
  629. if (OS::get_singleton()->get_ticks_msec() - _current_peer->timestamp > _request_timeout) {
  630. Dictionary response = parser->prepare_error_response(params, DAP::ErrorType::TIMEOUT);
  631. _current_peer->res_queue.push_front(response);
  632. return true;
  633. }
  634. // Append "req_" to any command received; prevents name clash with existing functions, and possibly exploiting
  635. String command = "req_" + (String)params["command"];
  636. if (parser->has_method(command)) {
  637. _current_request = params["command"];
  638. Array args;
  639. args.push_back(params);
  640. Dictionary response = parser->callv(command, args);
  641. if (!response.is_empty()) {
  642. _current_peer->res_queue.push_front(response);
  643. } else {
  644. // Launch request needs to be deferred until we receive a configurationDone request.
  645. if (command != "req_launch") {
  646. completed = false;
  647. }
  648. }
  649. }
  650. reset_current_info();
  651. return completed;
  652. }
  653. void DebugAdapterProtocol::notify_initialized() {
  654. Dictionary event = parser->ev_initialized();
  655. _current_peer->res_queue.push_back(event);
  656. }
  657. void DebugAdapterProtocol::notify_process() {
  658. String launch_mode = _current_peer->attached ? "attach" : "launch";
  659. Dictionary event = parser->ev_process(launch_mode);
  660. for (List<Ref<DAPeer>>::Element *E = clients.front(); E; E = E->next()) {
  661. E->get()->res_queue.push_back(event);
  662. }
  663. }
  664. void DebugAdapterProtocol::notify_terminated() {
  665. Dictionary event = parser->ev_terminated();
  666. for (List<Ref<DAPeer>>::Element *E = clients.front(); E; E = E->next()) {
  667. if ((_current_request == "launch" || _current_request == "restart") && _current_peer == E->get()) {
  668. continue;
  669. }
  670. E->get()->res_queue.push_back(event);
  671. }
  672. }
  673. void DebugAdapterProtocol::notify_exited(const int &p_exitcode) {
  674. Dictionary event = parser->ev_exited(p_exitcode);
  675. for (List<Ref<DAPeer>>::Element *E = clients.front(); E; E = E->next()) {
  676. if ((_current_request == "launch" || _current_request == "restart") && _current_peer == E->get()) {
  677. continue;
  678. }
  679. E->get()->res_queue.push_back(event);
  680. }
  681. }
  682. void DebugAdapterProtocol::notify_stopped_paused() {
  683. Dictionary event = parser->ev_stopped_paused();
  684. for (List<Ref<DAPeer>>::Element *E = clients.front(); E; E = E->next()) {
  685. E->get()->res_queue.push_back(event);
  686. }
  687. }
  688. void DebugAdapterProtocol::notify_stopped_exception(const String &p_error) {
  689. Dictionary event = parser->ev_stopped_exception(p_error);
  690. for (List<Ref<DAPeer>>::Element *E = clients.front(); E; E = E->next()) {
  691. E->get()->res_queue.push_back(event);
  692. }
  693. }
  694. void DebugAdapterProtocol::notify_stopped_breakpoint(const int &p_id) {
  695. Dictionary event = parser->ev_stopped_breakpoint(p_id);
  696. for (List<Ref<DAPeer>>::Element *E = clients.front(); E; E = E->next()) {
  697. E->get()->res_queue.push_back(event);
  698. }
  699. }
  700. void DebugAdapterProtocol::notify_stopped_step() {
  701. Dictionary event = parser->ev_stopped_step();
  702. for (List<Ref<DAPeer>>::Element *E = clients.front(); E; E = E->next()) {
  703. E->get()->res_queue.push_back(event);
  704. }
  705. }
  706. void DebugAdapterProtocol::notify_continued() {
  707. Dictionary event = parser->ev_continued();
  708. for (List<Ref<DAPeer>>::Element *E = clients.front(); E; E = E->next()) {
  709. if (_current_request == "continue" && E->get() == _current_peer) {
  710. continue;
  711. }
  712. E->get()->res_queue.push_back(event);
  713. }
  714. reset_stack_info();
  715. }
  716. void DebugAdapterProtocol::notify_output(const String &p_message, RemoteDebugger::MessageType p_type) {
  717. Dictionary event = parser->ev_output(p_message, p_type);
  718. for (List<Ref<DAPeer>>::Element *E = clients.front(); E; E = E->next()) {
  719. E->get()->res_queue.push_back(event);
  720. }
  721. }
  722. void DebugAdapterProtocol::notify_custom_data(const String &p_msg, const Array &p_data) {
  723. Dictionary event = parser->ev_custom_data(p_msg, p_data);
  724. for (List<Ref<DAPeer>>::Element *E = clients.front(); E; E = E->next()) {
  725. Ref<DAPeer> peer = E->get();
  726. if (peer->supportsCustomData) {
  727. peer->res_queue.push_back(event);
  728. }
  729. }
  730. }
  731. void DebugAdapterProtocol::notify_breakpoint(const DAP::Breakpoint &p_breakpoint, const bool &p_enabled) {
  732. Dictionary event = parser->ev_breakpoint(p_breakpoint, p_enabled);
  733. for (List<Ref<DAPeer>>::Element *E = clients.front(); E; E = E->next()) {
  734. if (_current_request == "setBreakpoints" && E->get() == _current_peer) {
  735. continue;
  736. }
  737. E->get()->res_queue.push_back(event);
  738. }
  739. }
  740. Array DebugAdapterProtocol::update_breakpoints(const String &p_path, const Array &p_lines) {
  741. Array updated_breakpoints;
  742. // Add breakpoints
  743. for (int i = 0; i < p_lines.size(); i++) {
  744. EditorDebuggerNode::get_singleton()->get_default_debugger()->_set_breakpoint(p_path, p_lines[i], true);
  745. DAP::Breakpoint breakpoint;
  746. breakpoint.line = p_lines[i];
  747. breakpoint.source.path = p_path;
  748. ERR_FAIL_COND_V(!breakpoint_list.find(breakpoint), Array());
  749. updated_breakpoints.push_back(breakpoint_list.find(breakpoint)->get().to_json());
  750. }
  751. // Remove breakpoints
  752. for (List<DAP::Breakpoint>::Element *E = breakpoint_list.front(); E; E = E->next()) {
  753. DAP::Breakpoint b = E->get();
  754. if (b.source.path == p_path && !p_lines.has(b.line)) {
  755. EditorDebuggerNode::get_singleton()->get_default_debugger()->_set_breakpoint(p_path, b.line, false);
  756. }
  757. }
  758. return updated_breakpoints;
  759. }
  760. void DebugAdapterProtocol::on_debug_paused() {
  761. if (EditorRunBar::get_singleton()->get_pause_button()->is_pressed()) {
  762. notify_stopped_paused();
  763. } else {
  764. notify_continued();
  765. }
  766. }
  767. void DebugAdapterProtocol::on_debug_stopped() {
  768. notify_exited();
  769. notify_terminated();
  770. }
  771. void DebugAdapterProtocol::on_debug_output(const String &p_message, int p_type) {
  772. notify_output(p_message, RemoteDebugger::MessageType(p_type));
  773. }
  774. void DebugAdapterProtocol::on_debug_breaked(const bool &p_reallydid, const bool &p_can_debug, const String &p_reason, const bool &p_has_stackdump) {
  775. if (!p_reallydid) {
  776. notify_continued();
  777. return;
  778. }
  779. if (p_reason == "Breakpoint") {
  780. if (_stepping) {
  781. notify_stopped_step();
  782. _stepping = false;
  783. } else {
  784. _processing_breakpoint = true; // Wait for stack_dump to find where the breakpoint happened
  785. }
  786. } else {
  787. notify_stopped_exception(p_reason);
  788. }
  789. _processing_stackdump = p_has_stackdump;
  790. }
  791. void DebugAdapterProtocol::on_debug_breakpoint_toggled(const String &p_path, const int &p_line, const bool &p_enabled) {
  792. DAP::Breakpoint breakpoint;
  793. breakpoint.verified = true;
  794. breakpoint.source.path = ProjectSettings::get_singleton()->globalize_path(p_path);
  795. breakpoint.source.compute_checksums();
  796. breakpoint.line = p_line;
  797. if (p_enabled) {
  798. // Add the breakpoint
  799. breakpoint.id = breakpoint_id++;
  800. breakpoint_list.push_back(breakpoint);
  801. } else {
  802. // Remove the breakpoint
  803. List<DAP::Breakpoint>::Element *E = breakpoint_list.find(breakpoint);
  804. if (E) {
  805. breakpoint.id = E->get().id;
  806. breakpoint_list.erase(E);
  807. }
  808. }
  809. notify_breakpoint(breakpoint, p_enabled);
  810. }
  811. void DebugAdapterProtocol::on_debug_stack_dump(const Array &p_stack_dump) {
  812. if (_processing_breakpoint && !p_stack_dump.is_empty()) {
  813. // Find existing breakpoint
  814. Dictionary d = p_stack_dump[0];
  815. DAP::Breakpoint breakpoint;
  816. breakpoint.source.path = ProjectSettings::get_singleton()->globalize_path(d["file"]);
  817. breakpoint.line = d["line"];
  818. List<DAP::Breakpoint>::Element *E = breakpoint_list.find(breakpoint);
  819. if (E) {
  820. notify_stopped_breakpoint(E->get().id);
  821. }
  822. _processing_breakpoint = false;
  823. }
  824. stackframe_id = 0;
  825. stackframe_list.clear();
  826. // Fill in stacktrace information
  827. for (int i = 0; i < p_stack_dump.size(); i++) {
  828. Dictionary stack_info = p_stack_dump[i];
  829. DAP::StackFrame stackframe;
  830. stackframe.id = stackframe_id++;
  831. stackframe.name = stack_info["function"];
  832. stackframe.line = stack_info["line"];
  833. stackframe.column = 0;
  834. stackframe.source.path = ProjectSettings::get_singleton()->globalize_path(stack_info["file"]);
  835. stackframe.source.compute_checksums();
  836. // Information for "Locals", "Members" and "Globals" variables respectively
  837. List<int> scope_ids;
  838. for (int j = 0; j < 3; j++) {
  839. scope_ids.push_back(variable_id++);
  840. }
  841. stackframe_list.insert(stackframe, scope_ids);
  842. }
  843. _current_frame = 0;
  844. _processing_stackdump = false;
  845. }
  846. void DebugAdapterProtocol::on_debug_stack_frame_vars(const int &p_size) {
  847. _remaining_vars = p_size;
  848. DAP::StackFrame frame;
  849. frame.id = _current_frame;
  850. ERR_FAIL_COND(!stackframe_list.has(frame));
  851. List<int> scope_ids = stackframe_list.find(frame)->value;
  852. for (List<int>::Element *E = scope_ids.front(); E; E = E->next()) {
  853. int var_id = E->get();
  854. if (variable_list.has(var_id)) {
  855. variable_list.find(var_id)->value.clear();
  856. } else {
  857. variable_list.insert(var_id, Array());
  858. }
  859. }
  860. }
  861. void DebugAdapterProtocol::on_debug_stack_frame_var(const Array &p_data) {
  862. DebuggerMarshalls::ScriptStackVariable stack_var;
  863. stack_var.deserialize(p_data);
  864. ERR_FAIL_COND(stackframe_list.is_empty());
  865. DAP::StackFrame frame;
  866. frame.id = _current_frame;
  867. List<int> scope_ids = stackframe_list.find(frame)->value;
  868. ERR_FAIL_COND(scope_ids.size() != 3);
  869. ERR_FAIL_INDEX(stack_var.type, 3);
  870. int var_id = scope_ids.get(stack_var.type);
  871. DAP::Variable variable;
  872. variable.name = stack_var.name;
  873. variable.value = stack_var.value;
  874. variable.type = Variant::get_type_name(stack_var.value.get_type());
  875. variable.variablesReference = parse_variant(stack_var.value);
  876. variable_list.find(var_id)->value.push_back(variable.to_json());
  877. _remaining_vars--;
  878. }
  879. void DebugAdapterProtocol::on_debug_data(const String &p_msg, const Array &p_data) {
  880. // Ignore data that is already handled by DAP
  881. if (p_msg == "debug_enter" || p_msg == "debug_exit" || p_msg == "stack_dump" || p_msg == "stack_frame_vars" || p_msg == "stack_frame_var" || p_msg == "output" || p_msg == "request_quit") {
  882. return;
  883. }
  884. notify_custom_data(p_msg, p_data);
  885. }
  886. void DebugAdapterProtocol::poll() {
  887. if (server->is_connection_available()) {
  888. on_client_connected();
  889. }
  890. List<Ref<DAPeer>> to_delete;
  891. for (List<Ref<DAPeer>>::Element *E = clients.front(); E; E = E->next()) {
  892. Ref<DAPeer> peer = E->get();
  893. peer->connection->poll();
  894. StreamPeerTCP::Status status = peer->connection->get_status();
  895. if (status == StreamPeerTCP::STATUS_NONE || status == StreamPeerTCP::STATUS_ERROR) {
  896. to_delete.push_back(peer);
  897. } else {
  898. _current_peer = peer;
  899. Error err = peer->handle_data();
  900. if (err != OK && err != ERR_BUSY) {
  901. to_delete.push_back(peer);
  902. }
  903. err = peer->send_data();
  904. if (err != OK && err != ERR_BUSY) {
  905. to_delete.push_back(peer);
  906. }
  907. }
  908. }
  909. for (List<Ref<DAPeer>>::Element *E = to_delete.front(); E; E = E->next()) {
  910. on_client_disconnected(E->get());
  911. }
  912. to_delete.clear();
  913. }
  914. Error DebugAdapterProtocol::start(int p_port, const IPAddress &p_bind_ip) {
  915. _request_timeout = (uint64_t)_EDITOR_GET("network/debug_adapter/request_timeout");
  916. _sync_breakpoints = (bool)_EDITOR_GET("network/debug_adapter/sync_breakpoints");
  917. _initialized = true;
  918. return server->listen(p_port, p_bind_ip);
  919. }
  920. void DebugAdapterProtocol::stop() {
  921. for (List<Ref<DAPeer>>::Element *E = clients.front(); E; E = E->next()) {
  922. E->get()->connection->disconnect_from_host();
  923. }
  924. clients.clear();
  925. server->stop();
  926. _initialized = false;
  927. }
  928. DebugAdapterProtocol::DebugAdapterProtocol() {
  929. server.instantiate();
  930. singleton = this;
  931. parser = memnew(DebugAdapterParser);
  932. reset_ids();
  933. EditorRunBar::get_singleton()->get_pause_button()->connect(SceneStringName(pressed), callable_mp(this, &DebugAdapterProtocol::on_debug_paused));
  934. EditorDebuggerNode *debugger_node = EditorDebuggerNode::get_singleton();
  935. debugger_node->connect("breakpoint_toggled", callable_mp(this, &DebugAdapterProtocol::on_debug_breakpoint_toggled));
  936. debugger_node->get_default_debugger()->connect("stopped", callable_mp(this, &DebugAdapterProtocol::on_debug_stopped));
  937. debugger_node->get_default_debugger()->connect("output", callable_mp(this, &DebugAdapterProtocol::on_debug_output));
  938. debugger_node->get_default_debugger()->connect("breaked", callable_mp(this, &DebugAdapterProtocol::on_debug_breaked));
  939. debugger_node->get_default_debugger()->connect("stack_dump", callable_mp(this, &DebugAdapterProtocol::on_debug_stack_dump));
  940. debugger_node->get_default_debugger()->connect("stack_frame_vars", callable_mp(this, &DebugAdapterProtocol::on_debug_stack_frame_vars));
  941. debugger_node->get_default_debugger()->connect("stack_frame_var", callable_mp(this, &DebugAdapterProtocol::on_debug_stack_frame_var));
  942. debugger_node->get_default_debugger()->connect("debug_data", callable_mp(this, &DebugAdapterProtocol::on_debug_data));
  943. }
  944. DebugAdapterProtocol::~DebugAdapterProtocol() {
  945. memdelete(parser);
  946. }