debug_adapter_protocol.cpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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. PackedVector2Array 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. default:
  600. // Simple atomic stuff, or too complex to be manipulated
  601. return 0;
  602. }
  603. }
  604. bool DebugAdapterProtocol::process_message(const String &p_text) {
  605. JSON json;
  606. ERR_FAIL_COND_V_MSG(json.parse(p_text) != OK, true, "Malformed message!");
  607. Dictionary params = json.get_data();
  608. bool completed = true;
  609. if (OS::get_singleton()->get_ticks_msec() - _current_peer->timestamp > _request_timeout) {
  610. Dictionary response = parser->prepare_error_response(params, DAP::ErrorType::TIMEOUT);
  611. _current_peer->res_queue.push_front(response);
  612. return true;
  613. }
  614. // Append "req_" to any command received; prevents name clash with existing functions, and possibly exploiting
  615. String command = "req_" + (String)params["command"];
  616. if (parser->has_method(command)) {
  617. _current_request = params["command"];
  618. Array args;
  619. args.push_back(params);
  620. Dictionary response = parser->callv(command, args);
  621. if (!response.is_empty()) {
  622. _current_peer->res_queue.push_front(response);
  623. } else {
  624. // Launch request needs to be deferred until we receive a configurationDone request.
  625. if (command != "req_launch") {
  626. completed = false;
  627. }
  628. }
  629. }
  630. reset_current_info();
  631. return completed;
  632. }
  633. void DebugAdapterProtocol::notify_initialized() {
  634. Dictionary event = parser->ev_initialized();
  635. _current_peer->res_queue.push_back(event);
  636. }
  637. void DebugAdapterProtocol::notify_process() {
  638. String launch_mode = _current_peer->attached ? "attach" : "launch";
  639. Dictionary event = parser->ev_process(launch_mode);
  640. for (List<Ref<DAPeer>>::Element *E = clients.front(); E; E = E->next()) {
  641. E->get()->res_queue.push_back(event);
  642. }
  643. }
  644. void DebugAdapterProtocol::notify_terminated() {
  645. Dictionary event = parser->ev_terminated();
  646. for (List<Ref<DAPeer>>::Element *E = clients.front(); E; E = E->next()) {
  647. if ((_current_request == "launch" || _current_request == "restart") && _current_peer == E->get()) {
  648. continue;
  649. }
  650. E->get()->res_queue.push_back(event);
  651. }
  652. }
  653. void DebugAdapterProtocol::notify_exited(const int &p_exitcode) {
  654. Dictionary event = parser->ev_exited(p_exitcode);
  655. for (List<Ref<DAPeer>>::Element *E = clients.front(); E; E = E->next()) {
  656. if ((_current_request == "launch" || _current_request == "restart") && _current_peer == E->get()) {
  657. continue;
  658. }
  659. E->get()->res_queue.push_back(event);
  660. }
  661. }
  662. void DebugAdapterProtocol::notify_stopped_paused() {
  663. Dictionary event = parser->ev_stopped_paused();
  664. for (List<Ref<DAPeer>>::Element *E = clients.front(); E; E = E->next()) {
  665. E->get()->res_queue.push_back(event);
  666. }
  667. }
  668. void DebugAdapterProtocol::notify_stopped_exception(const String &p_error) {
  669. Dictionary event = parser->ev_stopped_exception(p_error);
  670. for (List<Ref<DAPeer>>::Element *E = clients.front(); E; E = E->next()) {
  671. E->get()->res_queue.push_back(event);
  672. }
  673. }
  674. void DebugAdapterProtocol::notify_stopped_breakpoint(const int &p_id) {
  675. Dictionary event = parser->ev_stopped_breakpoint(p_id);
  676. for (List<Ref<DAPeer>>::Element *E = clients.front(); E; E = E->next()) {
  677. E->get()->res_queue.push_back(event);
  678. }
  679. }
  680. void DebugAdapterProtocol::notify_stopped_step() {
  681. Dictionary event = parser->ev_stopped_step();
  682. for (List<Ref<DAPeer>>::Element *E = clients.front(); E; E = E->next()) {
  683. E->get()->res_queue.push_back(event);
  684. }
  685. }
  686. void DebugAdapterProtocol::notify_continued() {
  687. Dictionary event = parser->ev_continued();
  688. for (List<Ref<DAPeer>>::Element *E = clients.front(); E; E = E->next()) {
  689. if (_current_request == "continue" && E->get() == _current_peer) {
  690. continue;
  691. }
  692. E->get()->res_queue.push_back(event);
  693. }
  694. reset_stack_info();
  695. }
  696. void DebugAdapterProtocol::notify_output(const String &p_message, RemoteDebugger::MessageType p_type) {
  697. Dictionary event = parser->ev_output(p_message, p_type);
  698. for (List<Ref<DAPeer>>::Element *E = clients.front(); E; E = E->next()) {
  699. E->get()->res_queue.push_back(event);
  700. }
  701. }
  702. void DebugAdapterProtocol::notify_custom_data(const String &p_msg, const Array &p_data) {
  703. Dictionary event = parser->ev_custom_data(p_msg, p_data);
  704. for (List<Ref<DAPeer>>::Element *E = clients.front(); E; E = E->next()) {
  705. Ref<DAPeer> peer = E->get();
  706. if (peer->supportsCustomData) {
  707. peer->res_queue.push_back(event);
  708. }
  709. }
  710. }
  711. void DebugAdapterProtocol::notify_breakpoint(const DAP::Breakpoint &p_breakpoint, const bool &p_enabled) {
  712. Dictionary event = parser->ev_breakpoint(p_breakpoint, p_enabled);
  713. for (List<Ref<DAPeer>>::Element *E = clients.front(); E; E = E->next()) {
  714. if (_current_request == "setBreakpoints" && E->get() == _current_peer) {
  715. continue;
  716. }
  717. E->get()->res_queue.push_back(event);
  718. }
  719. }
  720. Array DebugAdapterProtocol::update_breakpoints(const String &p_path, const Array &p_lines) {
  721. Array updated_breakpoints;
  722. // Add breakpoints
  723. for (int i = 0; i < p_lines.size(); i++) {
  724. EditorDebuggerNode::get_singleton()->get_default_debugger()->_set_breakpoint(p_path, p_lines[i], true);
  725. DAP::Breakpoint breakpoint;
  726. breakpoint.line = p_lines[i];
  727. breakpoint.source.path = p_path;
  728. ERR_FAIL_COND_V(!breakpoint_list.find(breakpoint), Array());
  729. updated_breakpoints.push_back(breakpoint_list.find(breakpoint)->get().to_json());
  730. }
  731. // Remove breakpoints
  732. for (List<DAP::Breakpoint>::Element *E = breakpoint_list.front(); E; E = E->next()) {
  733. DAP::Breakpoint b = E->get();
  734. if (b.source.path == p_path && !p_lines.has(b.line)) {
  735. EditorDebuggerNode::get_singleton()->get_default_debugger()->_set_breakpoint(p_path, b.line, false);
  736. }
  737. }
  738. return updated_breakpoints;
  739. }
  740. void DebugAdapterProtocol::on_debug_paused() {
  741. if (EditorRunBar::get_singleton()->get_pause_button()->is_pressed()) {
  742. notify_stopped_paused();
  743. } else {
  744. notify_continued();
  745. }
  746. }
  747. void DebugAdapterProtocol::on_debug_stopped() {
  748. notify_exited();
  749. notify_terminated();
  750. }
  751. void DebugAdapterProtocol::on_debug_output(const String &p_message, int p_type) {
  752. notify_output(p_message, RemoteDebugger::MessageType(p_type));
  753. }
  754. void DebugAdapterProtocol::on_debug_breaked(const bool &p_reallydid, const bool &p_can_debug, const String &p_reason, const bool &p_has_stackdump) {
  755. if (!p_reallydid) {
  756. notify_continued();
  757. return;
  758. }
  759. if (p_reason == "Breakpoint") {
  760. if (_stepping) {
  761. notify_stopped_step();
  762. _stepping = false;
  763. } else {
  764. _processing_breakpoint = true; // Wait for stack_dump to find where the breakpoint happened
  765. }
  766. } else {
  767. notify_stopped_exception(p_reason);
  768. }
  769. _processing_stackdump = p_has_stackdump;
  770. }
  771. void DebugAdapterProtocol::on_debug_breakpoint_toggled(const String &p_path, const int &p_line, const bool &p_enabled) {
  772. DAP::Breakpoint breakpoint;
  773. breakpoint.verified = true;
  774. breakpoint.source.path = ProjectSettings::get_singleton()->globalize_path(p_path);
  775. breakpoint.source.compute_checksums();
  776. breakpoint.line = p_line;
  777. if (p_enabled) {
  778. // Add the breakpoint
  779. breakpoint.id = breakpoint_id++;
  780. breakpoint_list.push_back(breakpoint);
  781. } else {
  782. // Remove the breakpoint
  783. List<DAP::Breakpoint>::Element *E = breakpoint_list.find(breakpoint);
  784. if (E) {
  785. breakpoint.id = E->get().id;
  786. breakpoint_list.erase(E);
  787. }
  788. }
  789. notify_breakpoint(breakpoint, p_enabled);
  790. }
  791. void DebugAdapterProtocol::on_debug_stack_dump(const Array &p_stack_dump) {
  792. if (_processing_breakpoint && !p_stack_dump.is_empty()) {
  793. // Find existing breakpoint
  794. Dictionary d = p_stack_dump[0];
  795. DAP::Breakpoint breakpoint;
  796. breakpoint.source.path = ProjectSettings::get_singleton()->globalize_path(d["file"]);
  797. breakpoint.line = d["line"];
  798. List<DAP::Breakpoint>::Element *E = breakpoint_list.find(breakpoint);
  799. if (E) {
  800. notify_stopped_breakpoint(E->get().id);
  801. }
  802. _processing_breakpoint = false;
  803. }
  804. stackframe_id = 0;
  805. stackframe_list.clear();
  806. // Fill in stacktrace information
  807. for (int i = 0; i < p_stack_dump.size(); i++) {
  808. Dictionary stack_info = p_stack_dump[i];
  809. DAP::StackFrame stackframe;
  810. stackframe.id = stackframe_id++;
  811. stackframe.name = stack_info["function"];
  812. stackframe.line = stack_info["line"];
  813. stackframe.column = 0;
  814. stackframe.source.path = ProjectSettings::get_singleton()->globalize_path(stack_info["file"]);
  815. stackframe.source.compute_checksums();
  816. // Information for "Locals", "Members" and "Globals" variables respectively
  817. List<int> scope_ids;
  818. for (int j = 0; j < 3; j++) {
  819. scope_ids.push_back(variable_id++);
  820. }
  821. stackframe_list.insert(stackframe, scope_ids);
  822. }
  823. _current_frame = 0;
  824. _processing_stackdump = false;
  825. }
  826. void DebugAdapterProtocol::on_debug_stack_frame_vars(const int &p_size) {
  827. _remaining_vars = p_size;
  828. DAP::StackFrame frame;
  829. frame.id = _current_frame;
  830. ERR_FAIL_COND(!stackframe_list.has(frame));
  831. List<int> scope_ids = stackframe_list.find(frame)->value;
  832. for (List<int>::Element *E = scope_ids.front(); E; E = E->next()) {
  833. int var_id = E->get();
  834. if (variable_list.has(var_id)) {
  835. variable_list.find(var_id)->value.clear();
  836. } else {
  837. variable_list.insert(var_id, Array());
  838. }
  839. }
  840. }
  841. void DebugAdapterProtocol::on_debug_stack_frame_var(const Array &p_data) {
  842. DebuggerMarshalls::ScriptStackVariable stack_var;
  843. stack_var.deserialize(p_data);
  844. ERR_FAIL_COND(stackframe_list.is_empty());
  845. DAP::StackFrame frame;
  846. frame.id = _current_frame;
  847. List<int> scope_ids = stackframe_list.find(frame)->value;
  848. ERR_FAIL_COND(scope_ids.size() != 3);
  849. ERR_FAIL_INDEX(stack_var.type, 3);
  850. int var_id = scope_ids[stack_var.type];
  851. DAP::Variable variable;
  852. variable.name = stack_var.name;
  853. variable.value = stack_var.value;
  854. variable.type = Variant::get_type_name(stack_var.value.get_type());
  855. variable.variablesReference = parse_variant(stack_var.value);
  856. variable_list.find(var_id)->value.push_back(variable.to_json());
  857. _remaining_vars--;
  858. }
  859. void DebugAdapterProtocol::on_debug_data(const String &p_msg, const Array &p_data) {
  860. // Ignore data that is already handled by DAP
  861. 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") {
  862. return;
  863. }
  864. notify_custom_data(p_msg, p_data);
  865. }
  866. void DebugAdapterProtocol::poll() {
  867. if (server->is_connection_available()) {
  868. on_client_connected();
  869. }
  870. List<Ref<DAPeer>> to_delete;
  871. for (List<Ref<DAPeer>>::Element *E = clients.front(); E; E = E->next()) {
  872. Ref<DAPeer> peer = E->get();
  873. peer->connection->poll();
  874. StreamPeerTCP::Status status = peer->connection->get_status();
  875. if (status == StreamPeerTCP::STATUS_NONE || status == StreamPeerTCP::STATUS_ERROR) {
  876. to_delete.push_back(peer);
  877. } else {
  878. _current_peer = peer;
  879. Error err = peer->handle_data();
  880. if (err != OK && err != ERR_BUSY) {
  881. to_delete.push_back(peer);
  882. }
  883. err = peer->send_data();
  884. if (err != OK && err != ERR_BUSY) {
  885. to_delete.push_back(peer);
  886. }
  887. }
  888. }
  889. for (List<Ref<DAPeer>>::Element *E = to_delete.front(); E; E = E->next()) {
  890. on_client_disconnected(E->get());
  891. }
  892. to_delete.clear();
  893. }
  894. Error DebugAdapterProtocol::start(int p_port, const IPAddress &p_bind_ip) {
  895. _request_timeout = (uint64_t)_EDITOR_GET("network/debug_adapter/request_timeout");
  896. _sync_breakpoints = (bool)_EDITOR_GET("network/debug_adapter/sync_breakpoints");
  897. _initialized = true;
  898. return server->listen(p_port, p_bind_ip);
  899. }
  900. void DebugAdapterProtocol::stop() {
  901. for (List<Ref<DAPeer>>::Element *E = clients.front(); E; E = E->next()) {
  902. E->get()->connection->disconnect_from_host();
  903. }
  904. clients.clear();
  905. server->stop();
  906. _initialized = false;
  907. }
  908. DebugAdapterProtocol::DebugAdapterProtocol() {
  909. server.instantiate();
  910. singleton = this;
  911. parser = memnew(DebugAdapterParser);
  912. reset_ids();
  913. EditorRunBar::get_singleton()->get_pause_button()->connect("pressed", callable_mp(this, &DebugAdapterProtocol::on_debug_paused));
  914. EditorDebuggerNode *debugger_node = EditorDebuggerNode::get_singleton();
  915. debugger_node->connect("breakpoint_toggled", callable_mp(this, &DebugAdapterProtocol::on_debug_breakpoint_toggled));
  916. debugger_node->get_default_debugger()->connect("stopped", callable_mp(this, &DebugAdapterProtocol::on_debug_stopped));
  917. debugger_node->get_default_debugger()->connect("output", callable_mp(this, &DebugAdapterProtocol::on_debug_output));
  918. debugger_node->get_default_debugger()->connect("breaked", callable_mp(this, &DebugAdapterProtocol::on_debug_breaked));
  919. debugger_node->get_default_debugger()->connect("stack_dump", callable_mp(this, &DebugAdapterProtocol::on_debug_stack_dump));
  920. debugger_node->get_default_debugger()->connect("stack_frame_vars", callable_mp(this, &DebugAdapterProtocol::on_debug_stack_frame_vars));
  921. debugger_node->get_default_debugger()->connect("stack_frame_var", callable_mp(this, &DebugAdapterProtocol::on_debug_stack_frame_var));
  922. debugger_node->get_default_debugger()->connect("debug_data", callable_mp(this, &DebugAdapterProtocol::on_debug_data));
  923. }
  924. DebugAdapterProtocol::~DebugAdapterProtocol() {
  925. memdelete(parser);
  926. }