remote_debugger.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. /*************************************************************************/
  2. /* remote_debugger.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "remote_debugger.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/debugger/debugger_marshalls.h"
  33. #include "core/debugger/engine_debugger.h"
  34. #include "core/debugger/engine_profiler.h"
  35. #include "core/debugger/script_debugger.h"
  36. #include "core/input/input.h"
  37. #include "core/object/script_language.h"
  38. #include "core/os/os.h"
  39. class RemoteDebugger::MultiplayerProfiler : public EngineProfiler {
  40. struct BandwidthFrame {
  41. uint32_t timestamp;
  42. int packet_size;
  43. };
  44. int bandwidth_in_ptr = 0;
  45. Vector<BandwidthFrame> bandwidth_in;
  46. int bandwidth_out_ptr = 0;
  47. Vector<BandwidthFrame> bandwidth_out;
  48. uint64_t last_bandwidth_time = 0;
  49. int bandwidth_usage(const Vector<BandwidthFrame> &p_buffer, int p_pointer) {
  50. ERR_FAIL_COND_V(p_buffer.size() == 0, 0);
  51. int total_bandwidth = 0;
  52. uint64_t timestamp = OS::get_singleton()->get_ticks_msec();
  53. uint64_t final_timestamp = timestamp - 1000;
  54. int i = (p_pointer + p_buffer.size() - 1) % p_buffer.size();
  55. while (i != p_pointer && p_buffer[i].packet_size > 0) {
  56. if (p_buffer[i].timestamp < final_timestamp) {
  57. return total_bandwidth;
  58. }
  59. total_bandwidth += p_buffer[i].packet_size;
  60. i = (i + p_buffer.size() - 1) % p_buffer.size();
  61. }
  62. ERR_FAIL_COND_V_MSG(i == p_pointer, total_bandwidth, "Reached the end of the bandwidth profiler buffer, values might be inaccurate.");
  63. return total_bandwidth;
  64. }
  65. public:
  66. void toggle(bool p_enable, const Array &p_opts) {
  67. if (!p_enable) {
  68. bandwidth_in.clear();
  69. bandwidth_out.clear();
  70. } else {
  71. bandwidth_in_ptr = 0;
  72. bandwidth_in.resize(16384); // ~128kB
  73. for (int i = 0; i < bandwidth_in.size(); ++i) {
  74. bandwidth_in.write[i].packet_size = -1;
  75. }
  76. bandwidth_out_ptr = 0;
  77. bandwidth_out.resize(16384); // ~128kB
  78. for (int i = 0; i < bandwidth_out.size(); ++i) {
  79. bandwidth_out.write[i].packet_size = -1;
  80. }
  81. }
  82. }
  83. void add(const Array &p_data) {
  84. ERR_FAIL_COND(p_data.size() < 3);
  85. const String inout = p_data[0];
  86. int time = p_data[1];
  87. int size = p_data[2];
  88. if (inout == "in") {
  89. bandwidth_in.write[bandwidth_in_ptr].timestamp = time;
  90. bandwidth_in.write[bandwidth_in_ptr].packet_size = size;
  91. bandwidth_in_ptr = (bandwidth_in_ptr + 1) % bandwidth_in.size();
  92. } else if (inout == "out") {
  93. bandwidth_out.write[bandwidth_out_ptr].timestamp = time;
  94. bandwidth_out.write[bandwidth_out_ptr].packet_size = size;
  95. bandwidth_out_ptr = (bandwidth_out_ptr + 1) % bandwidth_out.size();
  96. }
  97. }
  98. void tick(double p_frame_time, double p_idle_time, double p_physics_time, double p_physics_frame_time) {
  99. uint64_t pt = OS::get_singleton()->get_ticks_msec();
  100. if (pt - last_bandwidth_time > 200) {
  101. last_bandwidth_time = pt;
  102. int incoming_bandwidth = bandwidth_usage(bandwidth_in, bandwidth_in_ptr);
  103. int outgoing_bandwidth = bandwidth_usage(bandwidth_out, bandwidth_out_ptr);
  104. Array arr;
  105. arr.push_back(incoming_bandwidth);
  106. arr.push_back(outgoing_bandwidth);
  107. EngineDebugger::get_singleton()->send_message("multiplayer:bandwidth", arr);
  108. }
  109. }
  110. };
  111. class RemoteDebugger::PerformanceProfiler : public EngineProfiler {
  112. Object *performance = nullptr;
  113. int last_perf_time = 0;
  114. uint64_t last_monitor_modification_time = 0;
  115. public:
  116. void toggle(bool p_enable, const Array &p_opts) {}
  117. void add(const Array &p_data) {}
  118. void tick(double p_frame_time, double p_idle_time, double p_physics_time, double p_physics_frame_time) {
  119. if (!performance) {
  120. return;
  121. }
  122. uint64_t pt = OS::get_singleton()->get_ticks_msec();
  123. if (pt - last_perf_time < 1000) {
  124. return;
  125. }
  126. last_perf_time = pt;
  127. Array custom_monitor_names = performance->call("get_custom_monitor_names");
  128. uint64_t monitor_modification_time = performance->call("get_monitor_modification_time");
  129. if (monitor_modification_time > last_monitor_modification_time) {
  130. last_monitor_modification_time = monitor_modification_time;
  131. EngineDebugger::get_singleton()->send_message("performance:profile_names", custom_monitor_names);
  132. }
  133. int max = performance->get("MONITOR_MAX");
  134. Array arr;
  135. arr.resize(max + custom_monitor_names.size());
  136. for (int i = 0; i < max; i++) {
  137. arr[i] = performance->call("get_monitor", i);
  138. }
  139. for (int i = 0; i < custom_monitor_names.size(); i++) {
  140. Variant monitor_value = performance->call("get_custom_monitor", custom_monitor_names[i]);
  141. if (!monitor_value.is_num()) {
  142. ERR_PRINT("Value of custom monitor '" + String(custom_monitor_names[i]) + "' is not a number");
  143. arr[i + max] = Variant();
  144. }
  145. arr[i + max] = monitor_value;
  146. }
  147. EngineDebugger::get_singleton()->send_message("performance:profile_frame", arr);
  148. }
  149. PerformanceProfiler(Object *p_performance) {
  150. performance = p_performance;
  151. }
  152. };
  153. Error RemoteDebugger::_put_msg(String p_message, Array p_data) {
  154. Array msg;
  155. msg.push_back(p_message);
  156. msg.push_back(p_data);
  157. Error err = peer->put_message(msg);
  158. if (err != OK) {
  159. n_messages_dropped++;
  160. }
  161. return err;
  162. }
  163. void RemoteDebugger::_err_handler(void *p_this, const char *p_func, const char *p_file, int p_line, const char *p_err, const char *p_descr, bool p_editor_notify, ErrorHandlerType p_type) {
  164. if (p_type == ERR_HANDLER_SCRIPT) {
  165. return; //ignore script errors, those go through debugger
  166. }
  167. RemoteDebugger *rd = (RemoteDebugger *)p_this;
  168. if (rd->flushing && Thread::get_caller_id() == rd->flush_thread) { // Can't handle recursive errors during flush.
  169. return;
  170. }
  171. Vector<ScriptLanguage::StackInfo> si;
  172. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  173. si = ScriptServer::get_language(i)->debug_get_current_stack_info();
  174. if (si.size()) {
  175. break;
  176. }
  177. }
  178. // send_error will lock internally.
  179. rd->script_debugger->send_error(String::utf8(p_func), String::utf8(p_file), p_line, String::utf8(p_err), String::utf8(p_descr), p_editor_notify, p_type, si);
  180. }
  181. void RemoteDebugger::_print_handler(void *p_this, const String &p_string, bool p_error) {
  182. RemoteDebugger *rd = (RemoteDebugger *)p_this;
  183. if (rd->flushing && Thread::get_caller_id() == rd->flush_thread) { // Can't handle recursive prints during flush.
  184. return;
  185. }
  186. String s = p_string;
  187. int allowed_chars = MIN(MAX(rd->max_chars_per_second - rd->char_count, 0), s.length());
  188. if (allowed_chars == 0 && s.length() > 0) {
  189. return;
  190. }
  191. if (allowed_chars < s.length()) {
  192. s = s.substr(0, allowed_chars);
  193. }
  194. MutexLock lock(rd->mutex);
  195. rd->char_count += allowed_chars;
  196. bool overflowed = rd->char_count >= rd->max_chars_per_second;
  197. if (rd->is_peer_connected()) {
  198. if (overflowed) {
  199. s += "[...]";
  200. }
  201. OutputString output_string;
  202. output_string.message = s;
  203. output_string.type = p_error ? MESSAGE_TYPE_ERROR : MESSAGE_TYPE_LOG;
  204. rd->output_strings.push_back(output_string);
  205. if (overflowed) {
  206. output_string.message = "[output overflow, print less text!]";
  207. output_string.type = MESSAGE_TYPE_ERROR;
  208. rd->output_strings.push_back(output_string);
  209. }
  210. }
  211. }
  212. RemoteDebugger::ErrorMessage RemoteDebugger::_create_overflow_error(const String &p_what, const String &p_descr) {
  213. ErrorMessage oe;
  214. oe.error = p_what;
  215. oe.error_descr = p_descr;
  216. oe.warning = false;
  217. uint64_t time = OS::get_singleton()->get_ticks_msec();
  218. oe.hr = time / 3600000;
  219. oe.min = (time / 60000) % 60;
  220. oe.sec = (time / 1000) % 60;
  221. oe.msec = time % 1000;
  222. return oe;
  223. }
  224. void RemoteDebugger::flush_output() {
  225. flush_thread = Thread::get_caller_id();
  226. flushing = true;
  227. MutexLock lock(mutex);
  228. if (!is_peer_connected()) {
  229. return;
  230. }
  231. if (n_messages_dropped > 0) {
  232. ErrorMessage err_msg = _create_overflow_error("TOO_MANY_MESSAGES", "Too many messages! " + String::num_int64(n_messages_dropped) + " messages were dropped. Profiling might misbheave, try raising 'network/limits/debugger/max_queued_messages' in project setting.");
  233. if (_put_msg("error", err_msg.serialize()) == OK) {
  234. n_messages_dropped = 0;
  235. }
  236. }
  237. if (output_strings.size()) {
  238. // Join output strings so we generate less messages.
  239. Vector<String> joined_log_strings;
  240. Vector<String> strings;
  241. Vector<int> types;
  242. for (int i = 0; i < output_strings.size(); i++) {
  243. const OutputString &output_string = output_strings[i];
  244. if (output_string.type == MESSAGE_TYPE_ERROR) {
  245. if (!joined_log_strings.is_empty()) {
  246. strings.push_back(String("\n").join(joined_log_strings));
  247. types.push_back(MESSAGE_TYPE_LOG);
  248. joined_log_strings.clear();
  249. }
  250. strings.push_back(output_string.message);
  251. types.push_back(MESSAGE_TYPE_ERROR);
  252. } else {
  253. joined_log_strings.push_back(output_string.message);
  254. }
  255. }
  256. if (!joined_log_strings.is_empty()) {
  257. strings.push_back(String("\n").join(joined_log_strings));
  258. types.push_back(MESSAGE_TYPE_LOG);
  259. }
  260. Array arr;
  261. arr.push_back(strings);
  262. arr.push_back(types);
  263. _put_msg("output", arr);
  264. output_strings.clear();
  265. }
  266. while (errors.size()) {
  267. ErrorMessage oe = errors.front()->get();
  268. _put_msg("error", oe.serialize());
  269. errors.pop_front();
  270. }
  271. // Update limits
  272. uint64_t ticks = OS::get_singleton()->get_ticks_usec() / 1000;
  273. if (ticks - last_reset > 1000) {
  274. last_reset = ticks;
  275. char_count = 0;
  276. err_count = 0;
  277. n_errors_dropped = 0;
  278. warn_count = 0;
  279. n_warnings_dropped = 0;
  280. }
  281. flushing = false;
  282. }
  283. void RemoteDebugger::send_message(const String &p_message, const Array &p_args) {
  284. MutexLock lock(mutex);
  285. if (is_peer_connected()) {
  286. _put_msg(p_message, p_args);
  287. }
  288. }
  289. void RemoteDebugger::send_error(const String &p_func, const String &p_file, int p_line, const String &p_err, const String &p_descr, bool p_editor_notify, ErrorHandlerType p_type) {
  290. ErrorMessage oe;
  291. oe.error = p_err;
  292. oe.error_descr = p_descr;
  293. oe.source_file = p_file;
  294. oe.source_line = p_line;
  295. oe.source_func = p_func;
  296. oe.warning = p_type == ERR_HANDLER_WARNING;
  297. uint64_t time = OS::get_singleton()->get_ticks_msec();
  298. oe.hr = time / 3600000;
  299. oe.min = (time / 60000) % 60;
  300. oe.sec = (time / 1000) % 60;
  301. oe.msec = time % 1000;
  302. oe.callstack.append_array(script_debugger->get_error_stack_info());
  303. if (flushing && Thread::get_caller_id() == flush_thread) { // Can't handle recursive errors during flush.
  304. return;
  305. }
  306. MutexLock lock(mutex);
  307. if (oe.warning) {
  308. warn_count++;
  309. } else {
  310. err_count++;
  311. }
  312. if (is_peer_connected()) {
  313. if (oe.warning) {
  314. if (warn_count > max_warnings_per_second) {
  315. n_warnings_dropped++;
  316. if (n_warnings_dropped == 1) {
  317. // Only print one message about dropping per second
  318. ErrorMessage overflow = _create_overflow_error("TOO_MANY_WARNINGS", "Too many warnings! Ignoring warnings for up to 1 second.");
  319. errors.push_back(overflow);
  320. }
  321. } else {
  322. errors.push_back(oe);
  323. }
  324. } else {
  325. if (err_count > max_errors_per_second) {
  326. n_errors_dropped++;
  327. if (n_errors_dropped == 1) {
  328. // Only print one message about dropping per second
  329. ErrorMessage overflow = _create_overflow_error("TOO_MANY_ERRORS", "Too many errors! Ignoring errors for up to 1 second.");
  330. errors.push_back(overflow);
  331. }
  332. } else {
  333. errors.push_back(oe);
  334. }
  335. }
  336. }
  337. }
  338. void RemoteDebugger::_send_stack_vars(List<String> &p_names, List<Variant> &p_vals, int p_type) {
  339. DebuggerMarshalls::ScriptStackVariable stvar;
  340. List<String>::Element *E = p_names.front();
  341. List<Variant>::Element *F = p_vals.front();
  342. while (E) {
  343. stvar.name = E->get();
  344. stvar.value = F->get();
  345. stvar.type = p_type;
  346. send_message("stack_frame_var", stvar.serialize());
  347. E = E->next();
  348. F = F->next();
  349. }
  350. }
  351. Error RemoteDebugger::_try_capture(const String &p_msg, const Array &p_data, bool &r_captured) {
  352. const int idx = p_msg.find(":");
  353. r_captured = false;
  354. if (idx < 0) { // No prefix, unknown message.
  355. return OK;
  356. }
  357. const String cap = p_msg.substr(0, idx);
  358. if (!has_capture(cap)) {
  359. return ERR_UNAVAILABLE; // Unknown message...
  360. }
  361. const String msg = p_msg.substr(idx + 1);
  362. return capture_parse(cap, msg, p_data, r_captured);
  363. }
  364. void RemoteDebugger::debug(bool p_can_continue, bool p_is_error_breakpoint) {
  365. //this function is called when there is a debugger break (bug on script)
  366. //or when execution is paused from editor
  367. if (script_debugger->is_skipping_breakpoints() && !p_is_error_breakpoint) {
  368. return;
  369. }
  370. ERR_FAIL_COND_MSG(!is_peer_connected(), "Script Debugger failed to connect, but being used anyway.");
  371. if (!peer->can_block()) {
  372. return; // Peer does not support blocking IO. We could at least send the error though.
  373. }
  374. ScriptLanguage *script_lang = script_debugger->get_break_language();
  375. const String error_str = script_lang ? script_lang->debug_get_error() : "";
  376. Array msg;
  377. msg.push_back(p_can_continue);
  378. msg.push_back(error_str);
  379. ERR_FAIL_COND(!script_lang);
  380. msg.push_back(script_lang->debug_get_stack_level_count() > 0);
  381. send_message("debug_enter", msg);
  382. Input::MouseMode mouse_mode = Input::get_singleton()->get_mouse_mode();
  383. if (mouse_mode != Input::MOUSE_MODE_VISIBLE) {
  384. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  385. }
  386. while (is_peer_connected()) {
  387. flush_output();
  388. peer->poll();
  389. if (peer->has_message()) {
  390. Array cmd = peer->get_message();
  391. ERR_CONTINUE(cmd.size() != 2);
  392. ERR_CONTINUE(cmd[0].get_type() != Variant::STRING);
  393. ERR_CONTINUE(cmd[1].get_type() != Variant::ARRAY);
  394. String command = cmd[0];
  395. Array data = cmd[1];
  396. if (command == "step") {
  397. script_debugger->set_depth(-1);
  398. script_debugger->set_lines_left(1);
  399. break;
  400. } else if (command == "next") {
  401. script_debugger->set_depth(0);
  402. script_debugger->set_lines_left(1);
  403. break;
  404. } else if (command == "continue") {
  405. script_debugger->set_depth(-1);
  406. script_debugger->set_lines_left(-1);
  407. break;
  408. } else if (command == "break") {
  409. ERR_PRINT("Got break when already broke!");
  410. break;
  411. } else if (command == "get_stack_dump") {
  412. DebuggerMarshalls::ScriptStackDump dump;
  413. int slc = script_lang->debug_get_stack_level_count();
  414. for (int i = 0; i < slc; i++) {
  415. ScriptLanguage::StackInfo frame;
  416. frame.file = script_lang->debug_get_stack_level_source(i);
  417. frame.line = script_lang->debug_get_stack_level_line(i);
  418. frame.func = script_lang->debug_get_stack_level_function(i);
  419. dump.frames.push_back(frame);
  420. }
  421. send_message("stack_dump", dump.serialize());
  422. } else if (command == "get_stack_frame_vars") {
  423. ERR_FAIL_COND(data.size() != 1);
  424. ERR_FAIL_COND(!script_lang);
  425. int lv = data[0];
  426. List<String> members;
  427. List<Variant> member_vals;
  428. if (ScriptInstance *inst = script_lang->debug_get_stack_level_instance(lv)) {
  429. members.push_back("self");
  430. member_vals.push_back(inst->get_owner());
  431. }
  432. script_lang->debug_get_stack_level_members(lv, &members, &member_vals);
  433. ERR_FAIL_COND(members.size() != member_vals.size());
  434. List<String> locals;
  435. List<Variant> local_vals;
  436. script_lang->debug_get_stack_level_locals(lv, &locals, &local_vals);
  437. ERR_FAIL_COND(locals.size() != local_vals.size());
  438. List<String> globals;
  439. List<Variant> globals_vals;
  440. script_lang->debug_get_globals(&globals, &globals_vals);
  441. ERR_FAIL_COND(globals.size() != globals_vals.size());
  442. Array var_size;
  443. var_size.push_back(local_vals.size() + member_vals.size() + globals_vals.size());
  444. send_message("stack_frame_vars", var_size);
  445. _send_stack_vars(locals, local_vals, 0);
  446. _send_stack_vars(members, member_vals, 1);
  447. _send_stack_vars(globals, globals_vals, 2);
  448. } else if (command == "reload_scripts") {
  449. reload_all_scripts = true;
  450. } else if (command == "breakpoint") {
  451. ERR_FAIL_COND(data.size() < 3);
  452. bool set = data[2];
  453. if (set) {
  454. script_debugger->insert_breakpoint(data[1], data[0]);
  455. } else {
  456. script_debugger->remove_breakpoint(data[1], data[0]);
  457. }
  458. } else if (command == "set_skip_breakpoints") {
  459. ERR_FAIL_COND(data.size() < 1);
  460. script_debugger->set_skip_breakpoints(data[0]);
  461. } else {
  462. bool captured = false;
  463. ERR_CONTINUE(_try_capture(command, data, captured) != OK);
  464. if (!captured) {
  465. WARN_PRINT("Unknown message received from debugger: " + command);
  466. }
  467. }
  468. } else {
  469. OS::get_singleton()->delay_usec(10000);
  470. OS::get_singleton()->process_and_drop_events();
  471. }
  472. }
  473. send_message("debug_exit", Array());
  474. if (mouse_mode != Input::MOUSE_MODE_VISIBLE) {
  475. Input::get_singleton()->set_mouse_mode(mouse_mode);
  476. }
  477. }
  478. void RemoteDebugger::poll_events(bool p_is_idle) {
  479. if (peer.is_null()) {
  480. return;
  481. }
  482. flush_output();
  483. peer->poll();
  484. while (peer->has_message()) {
  485. Array arr = peer->get_message();
  486. ERR_CONTINUE(arr.size() != 2);
  487. ERR_CONTINUE(arr[0].get_type() != Variant::STRING);
  488. ERR_CONTINUE(arr[1].get_type() != Variant::ARRAY);
  489. const String cmd = arr[0];
  490. const int idx = cmd.find(":");
  491. bool parsed = false;
  492. if (idx < 0) { // Not prefix, use scripts capture.
  493. capture_parse("core", cmd, arr[1], parsed);
  494. continue;
  495. }
  496. const String cap = cmd.substr(0, idx);
  497. if (!has_capture(cap)) {
  498. continue; // Unknown message...
  499. }
  500. const String msg = cmd.substr(idx + 1);
  501. capture_parse(cap, msg, arr[1], parsed);
  502. }
  503. // Reload scripts during idle poll only.
  504. if (p_is_idle && reload_all_scripts) {
  505. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  506. ScriptServer::get_language(i)->reload_all_scripts();
  507. }
  508. reload_all_scripts = false;
  509. }
  510. }
  511. Error RemoteDebugger::_core_capture(const String &p_cmd, const Array &p_data, bool &r_captured) {
  512. r_captured = true;
  513. if (p_cmd == "reload_scripts") {
  514. reload_all_scripts = true;
  515. } else if (p_cmd == "breakpoint") {
  516. ERR_FAIL_COND_V(p_data.size() < 3, ERR_INVALID_DATA);
  517. bool set = p_data[2];
  518. if (set) {
  519. script_debugger->insert_breakpoint(p_data[1], p_data[0]);
  520. } else {
  521. script_debugger->remove_breakpoint(p_data[1], p_data[0]);
  522. }
  523. } else if (p_cmd == "set_skip_breakpoints") {
  524. ERR_FAIL_COND_V(p_data.size() < 1, ERR_INVALID_DATA);
  525. script_debugger->set_skip_breakpoints(p_data[0]);
  526. } else if (p_cmd == "break") {
  527. script_debugger->debug(script_debugger->get_break_language());
  528. } else {
  529. r_captured = false;
  530. }
  531. return OK;
  532. }
  533. Error RemoteDebugger::_profiler_capture(const String &p_cmd, const Array &p_data, bool &r_captured) {
  534. r_captured = false;
  535. ERR_FAIL_COND_V(p_data.size() < 1, ERR_INVALID_DATA);
  536. ERR_FAIL_COND_V(p_data[0].get_type() != Variant::BOOL, ERR_INVALID_DATA);
  537. ERR_FAIL_COND_V(!has_profiler(p_cmd), ERR_UNAVAILABLE);
  538. Array opts;
  539. if (p_data.size() > 1) { // Optional profiler parameters.
  540. ERR_FAIL_COND_V(p_data[1].get_type() != Variant::ARRAY, ERR_INVALID_DATA);
  541. opts = p_data[1];
  542. }
  543. r_captured = true;
  544. profiler_enable(p_cmd, p_data[0], opts);
  545. return OK;
  546. }
  547. RemoteDebugger::RemoteDebugger(Ref<RemoteDebuggerPeer> p_peer) {
  548. peer = p_peer;
  549. max_chars_per_second = GLOBAL_GET("network/limits/debugger/max_chars_per_second");
  550. max_errors_per_second = GLOBAL_GET("network/limits/debugger/max_errors_per_second");
  551. max_warnings_per_second = GLOBAL_GET("network/limits/debugger/max_warnings_per_second");
  552. // Multiplayer Profiler
  553. multiplayer_profiler.instantiate();
  554. multiplayer_profiler->bind("multiplayer");
  555. // Performance Profiler
  556. Object *perf = Engine::get_singleton()->get_singleton_object("Performance");
  557. if (perf) {
  558. performance_profiler = Ref<PerformanceProfiler>(memnew(PerformanceProfiler(perf)));
  559. performance_profiler->bind("performance");
  560. profiler_enable("performance", true);
  561. }
  562. // Core and profiler captures.
  563. Capture core_cap(this,
  564. [](void *p_user, const String &p_cmd, const Array &p_data, bool &r_captured) {
  565. return ((RemoteDebugger *)p_user)->_core_capture(p_cmd, p_data, r_captured);
  566. });
  567. register_message_capture("core", core_cap);
  568. Capture profiler_cap(this,
  569. [](void *p_user, const String &p_cmd, const Array &p_data, bool &r_captured) {
  570. return ((RemoteDebugger *)p_user)->_profiler_capture(p_cmd, p_data, r_captured);
  571. });
  572. register_message_capture("profiler", profiler_cap);
  573. // Error handlers
  574. phl.printfunc = _print_handler;
  575. phl.userdata = this;
  576. add_print_handler(&phl);
  577. eh.errfunc = _err_handler;
  578. eh.userdata = this;
  579. add_error_handler(&eh);
  580. }
  581. RemoteDebugger::~RemoteDebugger() {
  582. remove_print_handler(&phl);
  583. remove_error_handler(&eh);
  584. }