debug_adapter_protocol.cpp 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  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 "core/io/marshalls.h"
  35. #include "editor/debugger/debug_adapter/debug_adapter_parser.h"
  36. #include "editor/debugger/script_editor_debugger.h"
  37. #include "editor/editor_log.h"
  38. #include "editor/editor_node.h"
  39. #include "editor/run/editor_run_bar.h"
  40. #include "editor/settings/editor_settings.h"
  41. DebugAdapterProtocol *DebugAdapterProtocol::singleton = nullptr;
  42. Error DAPeer::handle_data() {
  43. int read = 0;
  44. // Read headers
  45. if (!has_header) {
  46. if (!connection->get_available_bytes()) {
  47. return OK;
  48. }
  49. while (true) {
  50. if (req_pos >= DAP_MAX_BUFFER_SIZE) {
  51. req_pos = 0;
  52. ERR_FAIL_V_MSG(ERR_OUT_OF_MEMORY, "Response header too big");
  53. }
  54. Error err = connection->get_partial_data(&req_buf[req_pos], 1, read);
  55. if (err != OK) {
  56. return FAILED;
  57. } else if (read != 1) { // Busy, wait until next poll
  58. return ERR_BUSY;
  59. }
  60. char *r = (char *)req_buf;
  61. int l = req_pos;
  62. // End of headers
  63. if (l > 3 && r[l] == '\n' && r[l - 1] == '\r' && r[l - 2] == '\n' && r[l - 3] == '\r') {
  64. r[l - 3] = '\0'; // Null terminate to read string
  65. String header = String::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 = String::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. const Vector<uint8_t> &formatted_data = format_output(data);
  112. int data_sent = 0;
  113. while (data_sent < formatted_data.size()) {
  114. int curr_sent = 0;
  115. Error err = connection->put_partial_data(formatted_data.ptr() + data_sent, formatted_data.size() - data_sent, 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. Vector<uint8_t> DAPeer::format_output(const Dictionary &p_params) const {
  126. const Vector<uint8_t> &content = Variant(p_params).to_json_string().to_utf8_buffer();
  127. Vector<uint8_t> response = vformat("Content-Length: %d\r\n\r\n", content.size()).to_utf8_buffer();
  128. response.append_array(content);
  129. return response;
  130. }
  131. Error DebugAdapterProtocol::on_client_connected() {
  132. ERR_FAIL_COND_V_MSG(clients.size() >= DAP_MAX_CLIENTS, FAILED, "Max client limits reached");
  133. Ref<StreamPeerTCP> tcp_peer = server->take_connection();
  134. ERR_FAIL_COND_V_MSG(tcp_peer.is_null(), FAILED, "Failed to take incoming DAP connection.");
  135. tcp_peer->set_no_delay(true);
  136. Ref<DAPeer> peer = memnew(DAPeer);
  137. peer->connection = tcp_peer;
  138. clients.push_back(peer);
  139. EditorDebuggerNode::get_singleton()->get_default_debugger()->set_move_to_foreground(false);
  140. EditorNode::get_log()->add_message("[DAP] Connection Taken", EditorLog::MSG_TYPE_EDITOR);
  141. return OK;
  142. }
  143. void DebugAdapterProtocol::on_client_disconnected(const Ref<DAPeer> &p_peer) {
  144. clients.erase(p_peer);
  145. if (!clients.size()) {
  146. reset_ids();
  147. EditorDebuggerNode::get_singleton()->get_default_debugger()->set_move_to_foreground(true);
  148. }
  149. EditorNode::get_log()->add_message("[DAP] Disconnected", EditorLog::MSG_TYPE_EDITOR);
  150. }
  151. void DebugAdapterProtocol::reset_current_info() {
  152. _current_request = "";
  153. _current_peer.unref();
  154. }
  155. void DebugAdapterProtocol::reset_ids() {
  156. breakpoint_id = 0;
  157. breakpoint_list.clear();
  158. breakpoint_source_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. scope_list.clear();
  166. variable_list.clear();
  167. object_list.clear();
  168. object_pending_set.clear();
  169. }
  170. int DebugAdapterProtocol::parse_variant(const Variant &p_var) {
  171. switch (p_var.get_type()) {
  172. case Variant::VECTOR2:
  173. case Variant::VECTOR2I: {
  174. int id = variable_id++;
  175. Vector2 vec = p_var;
  176. const String type_scalar = Variant::get_type_name(p_var.get_type() == Variant::VECTOR2 ? Variant::FLOAT : Variant::INT);
  177. DAP::Variable x, y;
  178. x.name = "x";
  179. y.name = "y";
  180. x.type = type_scalar;
  181. y.type = type_scalar;
  182. x.value = rtos(vec.x);
  183. y.value = rtos(vec.y);
  184. Array arr = { x.to_json(), 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 = { x.to_json(), y.to_json(), w.to_json(), h.to_json() };
  207. variable_list.insert(id, arr);
  208. return id;
  209. }
  210. case Variant::VECTOR3:
  211. case Variant::VECTOR3I: {
  212. int id = variable_id++;
  213. Vector3 vec = p_var;
  214. const String type_scalar = Variant::get_type_name(p_var.get_type() == Variant::VECTOR3 ? Variant::FLOAT : Variant::INT);
  215. DAP::Variable x, y, z;
  216. x.name = "x";
  217. y.name = "y";
  218. z.name = "z";
  219. x.type = type_scalar;
  220. y.type = type_scalar;
  221. z.type = type_scalar;
  222. x.value = rtos(vec.x);
  223. y.value = rtos(vec.y);
  224. z.value = rtos(vec.z);
  225. Array arr = { x.to_json(), y.to_json(), z.to_json() };
  226. variable_list.insert(id, arr);
  227. return id;
  228. }
  229. case Variant::TRANSFORM2D: {
  230. int id = variable_id++;
  231. Transform2D transform = p_var;
  232. const String type_vec2 = Variant::get_type_name(Variant::VECTOR2);
  233. DAP::Variable x, y, origin;
  234. x.name = "x";
  235. y.name = "y";
  236. origin.name = "origin";
  237. x.type = type_vec2;
  238. y.type = type_vec2;
  239. origin.type = type_vec2;
  240. x.value = String(transform.columns[0]);
  241. y.value = String(transform.columns[1]);
  242. origin.value = String(transform.columns[2]);
  243. x.variablesReference = parse_variant(transform.columns[0]);
  244. y.variablesReference = parse_variant(transform.columns[1]);
  245. origin.variablesReference = parse_variant(transform.columns[2]);
  246. Array arr = { x.to_json(), y.to_json(), origin.to_json() };
  247. variable_list.insert(id, arr);
  248. return id;
  249. }
  250. case Variant::PLANE: {
  251. int id = variable_id++;
  252. Plane plane = p_var;
  253. DAP::Variable d, normal;
  254. d.name = "d";
  255. normal.name = "normal";
  256. d.type = Variant::get_type_name(Variant::FLOAT);
  257. normal.type = Variant::get_type_name(Variant::VECTOR3);
  258. d.value = rtos(plane.d);
  259. normal.value = String(plane.normal);
  260. normal.variablesReference = parse_variant(plane.normal);
  261. Array arr = { d.to_json(), normal.to_json() };
  262. variable_list.insert(id, arr);
  263. return id;
  264. }
  265. case Variant::QUATERNION: {
  266. int id = variable_id++;
  267. Quaternion quat = p_var;
  268. const String type_float = Variant::get_type_name(Variant::FLOAT);
  269. DAP::Variable x, y, z, w;
  270. x.name = "x";
  271. y.name = "y";
  272. z.name = "z";
  273. w.name = "w";
  274. x.type = type_float;
  275. y.type = type_float;
  276. z.type = type_float;
  277. w.type = type_float;
  278. x.value = rtos(quat.x);
  279. y.value = rtos(quat.y);
  280. z.value = rtos(quat.z);
  281. w.value = rtos(quat.w);
  282. Array arr = { x.to_json(), y.to_json(), z.to_json(), w.to_json() };
  283. variable_list.insert(id, arr);
  284. return id;
  285. }
  286. case Variant::AABB: {
  287. int id = variable_id++;
  288. AABB aabb = p_var;
  289. const String type_vec3 = Variant::get_type_name(Variant::VECTOR3);
  290. DAP::Variable position, size;
  291. position.name = "position";
  292. size.name = "size";
  293. position.type = type_vec3;
  294. size.type = type_vec3;
  295. position.value = String(aabb.position);
  296. size.value = String(aabb.size);
  297. position.variablesReference = parse_variant(aabb.position);
  298. size.variablesReference = parse_variant(aabb.size);
  299. Array arr = { position.to_json(), size.to_json() };
  300. variable_list.insert(id, arr);
  301. return id;
  302. }
  303. case Variant::BASIS: {
  304. int id = variable_id++;
  305. Basis basis = p_var;
  306. const String type_vec3 = Variant::get_type_name(Variant::VECTOR3);
  307. DAP::Variable x, y, z;
  308. x.name = "x";
  309. y.name = "y";
  310. z.name = "z";
  311. x.type = type_vec3;
  312. y.type = type_vec3;
  313. z.type = type_vec3;
  314. x.value = String(basis.rows[0]);
  315. y.value = String(basis.rows[1]);
  316. z.value = String(basis.rows[2]);
  317. x.variablesReference = parse_variant(basis.rows[0]);
  318. y.variablesReference = parse_variant(basis.rows[1]);
  319. z.variablesReference = parse_variant(basis.rows[2]);
  320. Array arr = { x.to_json(), y.to_json(), z.to_json() };
  321. variable_list.insert(id, arr);
  322. return id;
  323. }
  324. case Variant::TRANSFORM3D: {
  325. int id = variable_id++;
  326. Transform3D transform = p_var;
  327. DAP::Variable basis, origin;
  328. basis.name = "basis";
  329. origin.name = "origin";
  330. basis.type = Variant::get_type_name(Variant::BASIS);
  331. origin.type = Variant::get_type_name(Variant::VECTOR3);
  332. basis.value = String(transform.basis);
  333. origin.value = String(transform.origin);
  334. basis.variablesReference = parse_variant(transform.basis);
  335. origin.variablesReference = parse_variant(transform.origin);
  336. Array arr = { basis.to_json(), origin.to_json() };
  337. variable_list.insert(id, arr);
  338. return id;
  339. }
  340. case Variant::COLOR: {
  341. int id = variable_id++;
  342. Color color = p_var;
  343. const String type_float = Variant::get_type_name(Variant::FLOAT);
  344. DAP::Variable r, g, b, a;
  345. r.name = "r";
  346. g.name = "g";
  347. b.name = "b";
  348. a.name = "a";
  349. r.type = type_float;
  350. g.type = type_float;
  351. b.type = type_float;
  352. a.type = type_float;
  353. r.value = rtos(color.r);
  354. g.value = rtos(color.g);
  355. b.value = rtos(color.b);
  356. a.value = rtos(color.a);
  357. Array arr = { r.to_json(), g.to_json(), b.to_json(), a.to_json() };
  358. variable_list.insert(id, arr);
  359. return id;
  360. }
  361. case Variant::ARRAY: {
  362. int id = variable_id++;
  363. Array array = p_var;
  364. DAP::Variable size;
  365. size.name = "size";
  366. size.type = Variant::get_type_name(Variant::INT);
  367. size.value = itos(array.size());
  368. Array arr = { size.to_json() };
  369. for (int i = 0; i < array.size(); i++) {
  370. DAP::Variable var;
  371. var.name = itos(i);
  372. var.type = Variant::get_type_name(array[i].get_type());
  373. var.value = array[i];
  374. var.variablesReference = parse_variant(array[i]);
  375. arr.push_back(var.to_json());
  376. }
  377. variable_list.insert(id, arr);
  378. return id;
  379. }
  380. case Variant::DICTIONARY: {
  381. int id = variable_id++;
  382. Dictionary dictionary = p_var;
  383. Array arr;
  384. for (const KeyValue<Variant, Variant> &kv : dictionary) {
  385. DAP::Variable var;
  386. var.name = kv.key;
  387. Variant value = kv.value;
  388. var.type = Variant::get_type_name(value.get_type());
  389. var.value = value;
  390. var.variablesReference = parse_variant(value);
  391. arr.push_back(var.to_json());
  392. }
  393. variable_list.insert(id, arr);
  394. return id;
  395. }
  396. case Variant::PACKED_BYTE_ARRAY: {
  397. int id = variable_id++;
  398. PackedByteArray array = p_var;
  399. DAP::Variable size;
  400. size.name = "size";
  401. size.type = Variant::get_type_name(Variant::INT);
  402. size.value = itos(array.size());
  403. Array arr = { size.to_json() };
  404. for (int i = 0; i < array.size(); i++) {
  405. DAP::Variable var;
  406. var.name = itos(i);
  407. var.type = "byte";
  408. var.value = itos(array[i]);
  409. arr.push_back(var.to_json());
  410. }
  411. variable_list.insert(id, arr);
  412. return id;
  413. }
  414. case Variant::PACKED_INT32_ARRAY: {
  415. int id = variable_id++;
  416. PackedInt32Array array = p_var;
  417. DAP::Variable size;
  418. size.name = "size";
  419. size.type = Variant::get_type_name(Variant::INT);
  420. size.value = itos(array.size());
  421. Array arr = { size.to_json() };
  422. for (int i = 0; i < array.size(); i++) {
  423. DAP::Variable var;
  424. var.name = itos(i);
  425. var.type = "int";
  426. var.value = itos(array[i]);
  427. arr.push_back(var.to_json());
  428. }
  429. variable_list.insert(id, arr);
  430. return id;
  431. }
  432. case Variant::PACKED_INT64_ARRAY: {
  433. int id = variable_id++;
  434. PackedInt64Array array = p_var;
  435. DAP::Variable size;
  436. size.name = "size";
  437. size.type = Variant::get_type_name(Variant::INT);
  438. size.value = itos(array.size());
  439. Array arr = { size.to_json() };
  440. for (int i = 0; i < array.size(); i++) {
  441. DAP::Variable var;
  442. var.name = itos(i);
  443. var.type = "long";
  444. var.value = itos(array[i]);
  445. arr.push_back(var.to_json());
  446. }
  447. variable_list.insert(id, arr);
  448. return id;
  449. }
  450. case Variant::PACKED_FLOAT32_ARRAY: {
  451. int id = variable_id++;
  452. PackedFloat32Array array = p_var;
  453. DAP::Variable size;
  454. size.name = "size";
  455. size.type = Variant::get_type_name(Variant::INT);
  456. size.value = itos(array.size());
  457. Array arr = { size.to_json() };
  458. for (int i = 0; i < array.size(); i++) {
  459. DAP::Variable var;
  460. var.name = itos(i);
  461. var.type = "float";
  462. var.value = rtos(array[i]);
  463. arr.push_back(var.to_json());
  464. }
  465. variable_list.insert(id, arr);
  466. return id;
  467. }
  468. case Variant::PACKED_FLOAT64_ARRAY: {
  469. int id = variable_id++;
  470. PackedFloat64Array array = p_var;
  471. DAP::Variable size;
  472. size.name = "size";
  473. size.type = Variant::get_type_name(Variant::INT);
  474. size.value = itos(array.size());
  475. Array arr = { size.to_json() };
  476. for (int i = 0; i < array.size(); i++) {
  477. DAP::Variable var;
  478. var.name = itos(i);
  479. var.type = "double";
  480. var.value = rtos(array[i]);
  481. arr.push_back(var.to_json());
  482. }
  483. variable_list.insert(id, arr);
  484. return id;
  485. }
  486. case Variant::PACKED_STRING_ARRAY: {
  487. int id = variable_id++;
  488. PackedStringArray array = p_var;
  489. DAP::Variable size;
  490. size.name = "size";
  491. size.type = Variant::get_type_name(Variant::INT);
  492. size.value = itos(array.size());
  493. Array arr = { size.to_json() };
  494. for (int i = 0; i < array.size(); i++) {
  495. DAP::Variable var;
  496. var.name = itos(i);
  497. var.type = Variant::get_type_name(Variant::STRING);
  498. var.value = array[i];
  499. arr.push_back(var.to_json());
  500. }
  501. variable_list.insert(id, arr);
  502. return id;
  503. }
  504. case Variant::PACKED_VECTOR2_ARRAY: {
  505. int id = variable_id++;
  506. PackedVector2Array array = p_var;
  507. DAP::Variable size;
  508. size.name = "size";
  509. size.type = Variant::get_type_name(Variant::INT);
  510. size.value = itos(array.size());
  511. Array arr = { size.to_json() };
  512. for (int i = 0; i < array.size(); i++) {
  513. DAP::Variable var;
  514. var.name = itos(i);
  515. var.type = Variant::get_type_name(Variant::VECTOR2);
  516. var.value = String(array[i]);
  517. var.variablesReference = parse_variant(array[i]);
  518. arr.push_back(var.to_json());
  519. }
  520. variable_list.insert(id, arr);
  521. return id;
  522. }
  523. case Variant::PACKED_VECTOR3_ARRAY: {
  524. int id = variable_id++;
  525. PackedVector3Array array = p_var;
  526. DAP::Variable size;
  527. size.name = "size";
  528. size.type = Variant::get_type_name(Variant::INT);
  529. size.value = itos(array.size());
  530. Array arr = { size.to_json() };
  531. for (int i = 0; i < array.size(); i++) {
  532. DAP::Variable var;
  533. var.name = itos(i);
  534. var.type = Variant::get_type_name(Variant::VECTOR3);
  535. var.value = String(array[i]);
  536. var.variablesReference = parse_variant(array[i]);
  537. arr.push_back(var.to_json());
  538. }
  539. variable_list.insert(id, arr);
  540. return id;
  541. }
  542. case Variant::PACKED_COLOR_ARRAY: {
  543. int id = variable_id++;
  544. PackedColorArray array = p_var;
  545. DAP::Variable size;
  546. size.name = "size";
  547. size.type = Variant::get_type_name(Variant::INT);
  548. size.value = itos(array.size());
  549. Array arr = { size.to_json() };
  550. for (int i = 0; i < array.size(); i++) {
  551. DAP::Variable var;
  552. var.name = itos(i);
  553. var.type = Variant::get_type_name(Variant::COLOR);
  554. var.value = String(array[i]);
  555. var.variablesReference = parse_variant(array[i]);
  556. arr.push_back(var.to_json());
  557. }
  558. variable_list.insert(id, arr);
  559. return id;
  560. }
  561. case Variant::PACKED_VECTOR4_ARRAY: {
  562. int id = variable_id++;
  563. PackedVector4Array array = p_var;
  564. DAP::Variable size;
  565. size.name = "size";
  566. size.type = Variant::get_type_name(Variant::INT);
  567. size.value = itos(array.size());
  568. Array arr;
  569. arr.push_back(size.to_json());
  570. for (int i = 0; i < array.size(); i++) {
  571. DAP::Variable var;
  572. var.name = itos(i);
  573. var.type = Variant::get_type_name(Variant::VECTOR4);
  574. var.value = String(array[i]);
  575. var.variablesReference = parse_variant(array[i]);
  576. arr.push_back(var.to_json());
  577. }
  578. variable_list.insert(id, arr);
  579. return id;
  580. }
  581. case Variant::OBJECT: {
  582. // Objects have to be requested from the debuggee. This has do be done
  583. // in a lazy way, as retrieving object properties takes time.
  584. EncodedObjectAsID *encoded_obj = Object::cast_to<EncodedObjectAsID>(p_var);
  585. // Object may be null; in that case, return early.
  586. if (!encoded_obj) {
  587. return 0;
  588. }
  589. // Object may have been already requested.
  590. ObjectID object_id = encoded_obj->get_object_id();
  591. if (object_list.has(object_id)) {
  592. return object_list[object_id];
  593. }
  594. // Queue requesting the object.
  595. int id = variable_id++;
  596. object_list.insert(object_id, id);
  597. return id;
  598. }
  599. default:
  600. // Simple atomic stuff, or too complex to be manipulated
  601. return 0;
  602. }
  603. }
  604. void DebugAdapterProtocol::parse_object(SceneDebuggerObject &p_obj) {
  605. // If the object is not on the pending list, we weren't expecting it. Ignore it.
  606. ObjectID object_id = p_obj.id;
  607. if (!object_pending_set.erase(object_id)) {
  608. return;
  609. }
  610. // Populate DAP::Variable's with the object's properties. These properties will be divided by categories.
  611. Array properties;
  612. Array script_members;
  613. Array script_constants;
  614. Array script_node;
  615. DAP::Variable node_type;
  616. Array node_properties;
  617. for (SceneDebuggerObject::SceneDebuggerProperty &property : p_obj.properties) {
  618. PropertyInfo &info = property.first;
  619. // Script members ("Members/" prefix)
  620. if (info.name.begins_with("Members/")) {
  621. info.name = info.name.trim_prefix("Members/");
  622. script_members.push_back(parse_object_variable(property));
  623. }
  624. // Script constants ("Constants/" prefix)
  625. else if (info.name.begins_with("Constants/")) {
  626. info.name = info.name.trim_prefix("Constants/");
  627. script_constants.push_back(parse_object_variable(property));
  628. }
  629. // Script node ("Node/" prefix)
  630. else if (info.name.begins_with("Node/")) {
  631. info.name = info.name.trim_prefix("Node/");
  632. script_node.push_back(parse_object_variable(property));
  633. }
  634. // Regular categories (with type Variant::NIL)
  635. else if (info.type == Variant::NIL) {
  636. if (!node_properties.is_empty()) {
  637. node_type.value = itos(node_properties.size());
  638. variable_list.insert(node_type.variablesReference, node_properties.duplicate());
  639. properties.push_back(node_type.to_json());
  640. }
  641. node_type.name = info.name;
  642. node_type.type = "Category";
  643. node_type.variablesReference = variable_id++;
  644. node_properties.clear();
  645. }
  646. // Regular properties.
  647. else {
  648. node_properties.push_back(parse_object_variable(property));
  649. }
  650. }
  651. // Add the last category.
  652. if (!node_properties.is_empty()) {
  653. node_type.value = itos(node_properties.size());
  654. variable_list.insert(node_type.variablesReference, node_properties.duplicate());
  655. properties.push_back(node_type.to_json());
  656. }
  657. // Add the script categories, in reverse order to be at the front of the array:
  658. // ( [members; constants; node; category1; category2; ...] )
  659. if (!script_node.is_empty()) {
  660. DAP::Variable node;
  661. node.name = "Node";
  662. node.type = "Category";
  663. node.value = itos(script_node.size());
  664. node.variablesReference = variable_id++;
  665. variable_list.insert(node.variablesReference, script_node);
  666. properties.push_front(node.to_json());
  667. }
  668. if (!script_constants.is_empty()) {
  669. DAP::Variable constants;
  670. constants.name = "Constants";
  671. constants.type = "Category";
  672. constants.value = itos(script_constants.size());
  673. constants.variablesReference = variable_id++;
  674. variable_list.insert(constants.variablesReference, script_constants);
  675. properties.push_front(constants.to_json());
  676. }
  677. if (!script_members.is_empty()) {
  678. DAP::Variable members;
  679. members.name = "Members";
  680. members.type = "Category";
  681. members.value = itos(script_members.size());
  682. members.variablesReference = variable_id++;
  683. variable_list.insert(members.variablesReference, script_members);
  684. properties.push_front(members.to_json());
  685. }
  686. ERR_FAIL_COND(!object_list.has(object_id));
  687. variable_list.insert(object_list[object_id], properties);
  688. }
  689. void DebugAdapterProtocol::parse_evaluation(DebuggerMarshalls::ScriptStackVariable &p_var) {
  690. // If the eval is not on the pending list, we weren't expecting it. Ignore it.
  691. String eval = p_var.name;
  692. if (!eval_pending_list.erase(eval)) {
  693. return;
  694. }
  695. DAP::Variable variable;
  696. variable.name = p_var.name;
  697. variable.value = p_var.value;
  698. variable.type = Variant::get_type_name(p_var.value.get_type());
  699. variable.variablesReference = parse_variant(p_var.value);
  700. eval_list.insert(variable.name, variable);
  701. }
  702. const Variant DebugAdapterProtocol::parse_object_variable(const SceneDebuggerObject::SceneDebuggerProperty &p_property) {
  703. const PropertyInfo &info = p_property.first;
  704. const Variant &value = p_property.second;
  705. DAP::Variable var;
  706. var.name = info.name;
  707. var.type = Variant::get_type_name(info.type);
  708. var.value = value;
  709. var.variablesReference = parse_variant(value);
  710. return var.to_json();
  711. }
  712. ObjectID DebugAdapterProtocol::search_object_id(DAPVarID p_var_id) {
  713. for (const KeyValue<ObjectID, DAPVarID> &E : object_list) {
  714. if (E.value == p_var_id) {
  715. return E.key;
  716. }
  717. }
  718. return ObjectID();
  719. }
  720. bool DebugAdapterProtocol::request_remote_object(const ObjectID &p_object_id) {
  721. // If the object is already on the pending list, we don't need to request it again.
  722. if (object_pending_set.has(p_object_id)) {
  723. return false;
  724. }
  725. TypedArray<uint64_t> arr;
  726. arr.append(p_object_id);
  727. EditorDebuggerNode::get_singleton()->get_default_debugger()->request_remote_objects(arr);
  728. object_pending_set.insert(p_object_id);
  729. return true;
  730. }
  731. bool DebugAdapterProtocol::request_remote_evaluate(const String &p_eval, int p_stack_frame) {
  732. // If the eval is already on the pending list, we don't need to request it again
  733. if (eval_pending_list.has(p_eval)) {
  734. return false;
  735. }
  736. EditorDebuggerNode::get_singleton()->get_default_debugger()->request_remote_evaluate(p_eval, p_stack_frame);
  737. eval_pending_list.insert(p_eval);
  738. return true;
  739. }
  740. const DAP::Source &DebugAdapterProtocol::fetch_source(const String &p_path) {
  741. const String &global_path = ProjectSettings::get_singleton()->globalize_path(p_path);
  742. HashMap<String, DAP::Source>::Iterator E = breakpoint_source_list.find(global_path);
  743. if (E != breakpoint_source_list.end()) {
  744. return E->value;
  745. }
  746. DAP::Source &added_source = breakpoint_source_list.insert(global_path, DAP::Source())->value;
  747. added_source.name = global_path.get_file();
  748. added_source.path = global_path;
  749. added_source.compute_checksums();
  750. return added_source;
  751. }
  752. void DebugAdapterProtocol::update_source(const String &p_path) {
  753. const String &global_path = ProjectSettings::get_singleton()->globalize_path(p_path);
  754. HashMap<String, DAP::Source>::Iterator E = breakpoint_source_list.find(global_path);
  755. if (E != breakpoint_source_list.end()) {
  756. E->value.compute_checksums();
  757. }
  758. }
  759. bool DebugAdapterProtocol::process_message(const String &p_text) {
  760. JSON json;
  761. ERR_FAIL_COND_V_MSG(json.parse(p_text) != OK, true, "Malformed message!");
  762. Dictionary params = json.get_data();
  763. bool completed = true;
  764. // While JSON does not distinguish floats and ints, "seq" is an integer by specification. See https://github.com/godotengine/godot/issues/108288
  765. if (params.has("seq")) {
  766. params["seq"] = (int)params["seq"];
  767. }
  768. if (OS::get_singleton()->get_ticks_msec() - _current_peer->timestamp > _request_timeout) {
  769. Dictionary response = parser->prepare_error_response(params, DAP::ErrorType::TIMEOUT);
  770. _current_peer->res_queue.push_front(response);
  771. return true;
  772. }
  773. // Append "req_" to any command received; prevents name clash with existing functions, and possibly exploiting
  774. String command = "req_" + (String)params["command"];
  775. if (parser->has_method(command)) {
  776. _current_request = params["command"];
  777. Array args = { params };
  778. Dictionary response = parser->callv(command, args);
  779. if (!response.is_empty()) {
  780. _current_peer->res_queue.push_front(response);
  781. } else {
  782. // Launch request needs to be deferred until we receive a configurationDone request.
  783. if (command != "req_launch") {
  784. completed = false;
  785. }
  786. }
  787. }
  788. reset_current_info();
  789. return completed;
  790. }
  791. void DebugAdapterProtocol::notify_initialized() {
  792. Dictionary event = parser->ev_initialized();
  793. _current_peer->res_queue.push_back(event);
  794. }
  795. void DebugAdapterProtocol::notify_process() {
  796. String launch_mode = _current_peer->attached ? "attach" : "launch";
  797. Dictionary event = parser->ev_process(launch_mode);
  798. for (const Ref<DAPeer> &peer : clients) {
  799. peer->res_queue.push_back(event);
  800. }
  801. }
  802. void DebugAdapterProtocol::notify_terminated() {
  803. Dictionary event = parser->ev_terminated();
  804. for (const Ref<DAPeer> &peer : clients) {
  805. if ((_current_request == "launch" || _current_request == "restart") && _current_peer == peer) {
  806. continue;
  807. }
  808. peer->res_queue.push_back(event);
  809. }
  810. }
  811. void DebugAdapterProtocol::notify_exited(const int &p_exitcode) {
  812. Dictionary event = parser->ev_exited(p_exitcode);
  813. for (const Ref<DAPeer> &peer : clients) {
  814. if ((_current_request == "launch" || _current_request == "restart") && _current_peer == peer) {
  815. continue;
  816. }
  817. peer->res_queue.push_back(event);
  818. }
  819. }
  820. void DebugAdapterProtocol::notify_stopped_paused() {
  821. Dictionary event = parser->ev_stopped_paused();
  822. for (const Ref<DAPeer> &peer : clients) {
  823. peer->res_queue.push_back(event);
  824. }
  825. }
  826. void DebugAdapterProtocol::notify_stopped_exception(const String &p_error) {
  827. Dictionary event = parser->ev_stopped_exception(p_error);
  828. for (const Ref<DAPeer> &peer : clients) {
  829. peer->res_queue.push_back(event);
  830. }
  831. }
  832. void DebugAdapterProtocol::notify_stopped_breakpoint(const int &p_id) {
  833. Dictionary event = parser->ev_stopped_breakpoint(p_id);
  834. for (const Ref<DAPeer> &peer : clients) {
  835. peer->res_queue.push_back(event);
  836. }
  837. }
  838. void DebugAdapterProtocol::notify_stopped_step() {
  839. Dictionary event = parser->ev_stopped_step();
  840. for (const Ref<DAPeer> &peer : clients) {
  841. peer->res_queue.push_back(event);
  842. }
  843. }
  844. void DebugAdapterProtocol::notify_continued() {
  845. Dictionary event = parser->ev_continued();
  846. for (const Ref<DAPeer> &peer : clients) {
  847. if (_current_request == "continue" && peer == _current_peer) {
  848. continue;
  849. }
  850. peer->res_queue.push_back(event);
  851. }
  852. reset_stack_info();
  853. }
  854. void DebugAdapterProtocol::notify_output(const String &p_message, RemoteDebugger::MessageType p_type) {
  855. Dictionary event = parser->ev_output(p_message, p_type);
  856. for (const Ref<DAPeer> &peer : clients) {
  857. peer->res_queue.push_back(event);
  858. }
  859. }
  860. void DebugAdapterProtocol::notify_custom_data(const String &p_msg, const Array &p_data) {
  861. Dictionary event = parser->ev_custom_data(p_msg, p_data);
  862. for (const Ref<DAPeer> &peer : clients) {
  863. if (peer->supportsCustomData) {
  864. peer->res_queue.push_back(event);
  865. }
  866. }
  867. }
  868. void DebugAdapterProtocol::notify_breakpoint(const DAP::Breakpoint &p_breakpoint, const bool &p_enabled) {
  869. Dictionary event = parser->ev_breakpoint(p_breakpoint, p_enabled);
  870. for (const Ref<DAPeer> &peer : clients) {
  871. if (_current_request == "setBreakpoints" && peer == _current_peer) {
  872. continue;
  873. }
  874. peer->res_queue.push_back(event);
  875. }
  876. }
  877. Array DebugAdapterProtocol::update_breakpoints(const String &p_path, const Array &p_lines) {
  878. Array updated_breakpoints;
  879. // Add breakpoints
  880. for (int i = 0; i < p_lines.size(); i++) {
  881. DAP::Breakpoint breakpoint(fetch_source(p_path));
  882. breakpoint.line = p_lines[i];
  883. // Avoid duplicated entries.
  884. List<DAP::Breakpoint>::Element *E = breakpoint_list.find(breakpoint);
  885. if (E) {
  886. updated_breakpoints.push_back(E->get().to_json());
  887. continue;
  888. }
  889. EditorDebuggerNode::get_singleton()->get_default_debugger()->_set_breakpoint(p_path, p_lines[i], true);
  890. // Breakpoints are inserted at the end of the breakpoint list.
  891. List<DAP::Breakpoint>::Element *added_breakpoint = breakpoint_list.back();
  892. ERR_FAIL_NULL_V(added_breakpoint, Array());
  893. ERR_FAIL_COND_V(!(added_breakpoint->get() == breakpoint), Array());
  894. updated_breakpoints.push_back(added_breakpoint->get().to_json());
  895. }
  896. // Remove breakpoints
  897. // Must be deferred because we are iterating the breakpoint list.
  898. Vector<int> to_remove;
  899. for (const DAP::Breakpoint &b : breakpoint_list) {
  900. if (b.source->path == p_path && !p_lines.has(b.line)) {
  901. to_remove.push_back(b.line);
  902. }
  903. }
  904. // Safe to remove queued data now.
  905. for (const int &line : to_remove) {
  906. EditorDebuggerNode::get_singleton()->get_default_debugger()->_set_breakpoint(p_path, line, false);
  907. }
  908. return updated_breakpoints;
  909. }
  910. void DebugAdapterProtocol::on_debug_paused() {
  911. if (EditorRunBar::get_singleton()->get_pause_button()->is_pressed()) {
  912. notify_stopped_paused();
  913. } else {
  914. notify_continued();
  915. }
  916. }
  917. void DebugAdapterProtocol::on_debug_stopped() {
  918. notify_exited();
  919. notify_terminated();
  920. reset_ids();
  921. }
  922. void DebugAdapterProtocol::on_debug_output(const String &p_message, int p_type) {
  923. notify_output(p_message, RemoteDebugger::MessageType(p_type));
  924. }
  925. void DebugAdapterProtocol::on_debug_breaked(const bool &p_reallydid, const bool &p_can_debug, const String &p_reason, const bool &p_has_stackdump) {
  926. if (!p_reallydid) {
  927. notify_continued();
  928. return;
  929. }
  930. if (p_reason == "Breakpoint") {
  931. if (_stepping) {
  932. notify_stopped_step();
  933. _stepping = false;
  934. } else {
  935. _processing_breakpoint = true; // Wait for stack_dump to find where the breakpoint happened
  936. }
  937. } else {
  938. notify_stopped_exception(p_reason);
  939. }
  940. _processing_stackdump = p_has_stackdump;
  941. }
  942. void DebugAdapterProtocol::on_debug_breakpoint_toggled(const String &p_path, const int &p_line, const bool &p_enabled) {
  943. DAP::Breakpoint breakpoint(fetch_source(p_path));
  944. breakpoint.verified = true;
  945. breakpoint.line = p_line;
  946. if (p_enabled) {
  947. // Add the breakpoint
  948. breakpoint.id = breakpoint_id++;
  949. breakpoint_list.push_back(breakpoint);
  950. } else {
  951. // Remove the breakpoint
  952. List<DAP::Breakpoint>::Element *E = breakpoint_list.find(breakpoint);
  953. if (E) {
  954. breakpoint.id = E->get().id;
  955. breakpoint_list.erase(E);
  956. }
  957. }
  958. notify_breakpoint(breakpoint, p_enabled);
  959. }
  960. void DebugAdapterProtocol::on_debug_stack_dump(const Array &p_stack_dump) {
  961. if (_processing_breakpoint && !p_stack_dump.is_empty()) {
  962. // Find existing breakpoint
  963. Dictionary d = p_stack_dump[0];
  964. DAP::Breakpoint breakpoint(fetch_source(d["file"]));
  965. breakpoint.line = d["line"];
  966. List<DAP::Breakpoint>::Element *E = breakpoint_list.find(breakpoint);
  967. if (E) {
  968. notify_stopped_breakpoint(E->get().id);
  969. }
  970. _processing_breakpoint = false;
  971. }
  972. stackframe_id = 0;
  973. stackframe_list.clear();
  974. scope_list.clear();
  975. // Fill in stacktrace information
  976. for (int i = 0; i < p_stack_dump.size(); i++) {
  977. Dictionary stack_info = p_stack_dump[i];
  978. DAP::StackFrame stackframe(fetch_source(stack_info["file"]));
  979. stackframe.id = stackframe_id++;
  980. stackframe.name = stack_info["function"];
  981. stackframe.line = stack_info["line"];
  982. stackframe.column = 0;
  983. // Information for "Locals", "Members" and "Globals" variables respectively
  984. Vector<int> scope_ids;
  985. for (int j = 0; j < 3; j++) {
  986. scope_ids.push_back(variable_id++);
  987. }
  988. stackframe_list.push_back(stackframe);
  989. scope_list.insert(stackframe.id, scope_ids);
  990. }
  991. _current_frame = 0;
  992. _processing_stackdump = false;
  993. }
  994. void DebugAdapterProtocol::on_debug_stack_frame_vars(const int &p_size) {
  995. _remaining_vars = p_size;
  996. ERR_FAIL_COND(!scope_list.has(_current_frame));
  997. Vector<int> scope_ids = scope_list.find(_current_frame)->value;
  998. for (const int &var_id : scope_ids) {
  999. if (variable_list.has(var_id)) {
  1000. variable_list.find(var_id)->value.clear();
  1001. } else {
  1002. variable_list.insert(var_id, Array());
  1003. }
  1004. }
  1005. }
  1006. void DebugAdapterProtocol::on_debug_stack_frame_var(const Array &p_data) {
  1007. DebuggerMarshalls::ScriptStackVariable stack_var;
  1008. stack_var.deserialize(p_data);
  1009. ERR_FAIL_COND(!scope_list.has(_current_frame));
  1010. Vector<int> scope_ids = scope_list.find(_current_frame)->value;
  1011. ERR_FAIL_COND(scope_ids.size() != 3);
  1012. ERR_FAIL_INDEX(stack_var.type, 4);
  1013. int var_id = scope_ids.get(stack_var.type);
  1014. DAP::Variable variable;
  1015. variable.name = stack_var.name;
  1016. variable.value = stack_var.value;
  1017. variable.type = Variant::get_type_name(stack_var.value.get_type());
  1018. variable.variablesReference = parse_variant(stack_var.value);
  1019. variable_list.find(var_id)->value.push_back(variable.to_json());
  1020. _remaining_vars--;
  1021. }
  1022. void DebugAdapterProtocol::on_debug_data(const String &p_msg, const Array &p_data) {
  1023. // Ignore data that is already handled by DAP
  1024. 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") {
  1025. return;
  1026. }
  1027. if (p_msg == "scene:inspect_objects") {
  1028. if (!p_data.is_empty()) {
  1029. // An object was requested from the debuggee; parse it.
  1030. SceneDebuggerObject remote_obj;
  1031. remote_obj.deserialize(p_data[0]);
  1032. parse_object(remote_obj);
  1033. }
  1034. #ifndef DISABLE_DEPRECATED
  1035. } else if (p_msg == "scene:inspect_object") {
  1036. if (!p_data.is_empty()) {
  1037. // Legacy single object response format.
  1038. SceneDebuggerObject remote_obj;
  1039. remote_obj.deserialize(p_data);
  1040. parse_object(remote_obj);
  1041. }
  1042. #endif // DISABLE_DEPRECATED
  1043. } else if (p_msg == "evaluation_return") {
  1044. // An evaluation was requested from the debuggee; parse it.
  1045. DebuggerMarshalls::ScriptStackVariable remote_evaluation;
  1046. remote_evaluation.deserialize(p_data);
  1047. parse_evaluation(remote_evaluation);
  1048. }
  1049. notify_custom_data(p_msg, p_data);
  1050. }
  1051. void DebugAdapterProtocol::poll() {
  1052. if (server->is_connection_available()) {
  1053. on_client_connected();
  1054. }
  1055. List<Ref<DAPeer>> to_delete;
  1056. for (const Ref<DAPeer> &peer : clients) {
  1057. peer->connection->poll();
  1058. StreamPeerTCP::Status status = peer->connection->get_status();
  1059. if (status == StreamPeerTCP::STATUS_NONE || status == StreamPeerTCP::STATUS_ERROR) {
  1060. to_delete.push_back(peer);
  1061. } else {
  1062. _current_peer = peer;
  1063. Error err = peer->handle_data();
  1064. if (err != OK && err != ERR_BUSY) {
  1065. to_delete.push_back(peer);
  1066. }
  1067. err = peer->send_data();
  1068. if (err != OK && err != ERR_BUSY) {
  1069. to_delete.push_back(peer);
  1070. }
  1071. }
  1072. }
  1073. for (const Ref<DAPeer> &peer : to_delete) {
  1074. on_client_disconnected(peer);
  1075. }
  1076. to_delete.clear();
  1077. }
  1078. Error DebugAdapterProtocol::start(int p_port, const IPAddress &p_bind_ip) {
  1079. _request_timeout = (uint64_t)_EDITOR_GET("network/debug_adapter/request_timeout");
  1080. _sync_breakpoints = (bool)_EDITOR_GET("network/debug_adapter/sync_breakpoints");
  1081. _initialized = true;
  1082. return server->listen(p_port, p_bind_ip);
  1083. }
  1084. void DebugAdapterProtocol::stop() {
  1085. for (const Ref<DAPeer> &peer : clients) {
  1086. peer->connection->disconnect_from_host();
  1087. }
  1088. clients.clear();
  1089. server->stop();
  1090. _initialized = false;
  1091. }
  1092. DebugAdapterProtocol::DebugAdapterProtocol() {
  1093. server.instantiate();
  1094. singleton = this;
  1095. parser = memnew(DebugAdapterParser);
  1096. reset_ids();
  1097. EditorRunBar::get_singleton()->get_pause_button()->connect(SceneStringName(pressed), callable_mp(this, &DebugAdapterProtocol::on_debug_paused));
  1098. EditorDebuggerNode *debugger_node = EditorDebuggerNode::get_singleton();
  1099. debugger_node->connect("breakpoint_toggled", callable_mp(this, &DebugAdapterProtocol::on_debug_breakpoint_toggled));
  1100. debugger_node->get_default_debugger()->connect("stopped", callable_mp(this, &DebugAdapterProtocol::on_debug_stopped));
  1101. debugger_node->get_default_debugger()->connect(SceneStringName(output), callable_mp(this, &DebugAdapterProtocol::on_debug_output));
  1102. debugger_node->get_default_debugger()->connect("breaked", callable_mp(this, &DebugAdapterProtocol::on_debug_breaked));
  1103. debugger_node->get_default_debugger()->connect("stack_dump", callable_mp(this, &DebugAdapterProtocol::on_debug_stack_dump));
  1104. debugger_node->get_default_debugger()->connect("stack_frame_vars", callable_mp(this, &DebugAdapterProtocol::on_debug_stack_frame_vars));
  1105. debugger_node->get_default_debugger()->connect("stack_frame_var", callable_mp(this, &DebugAdapterProtocol::on_debug_stack_frame_var));
  1106. debugger_node->get_default_debugger()->connect("debug_data", callable_mp(this, &DebugAdapterProtocol::on_debug_data));
  1107. }
  1108. DebugAdapterProtocol::~DebugAdapterProtocol() {
  1109. memdelete(parser);
  1110. }