debug_adapter_protocol.cpp 31 KB

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