debug_adapter_protocol.cpp 31 KB

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