remote_debugger.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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. uint64_t loop_begin_usec = 0;
  387. uint64_t loop_time_sec = 0;
  388. while (is_peer_connected()) {
  389. loop_begin_usec = OS::get_singleton()->get_ticks_usec();
  390. flush_output();
  391. peer->poll();
  392. if (peer->has_message()) {
  393. Array cmd = peer->get_message();
  394. ERR_CONTINUE(cmd.size() != 2);
  395. ERR_CONTINUE(cmd[0].get_type() != Variant::STRING);
  396. ERR_CONTINUE(cmd[1].get_type() != Variant::ARRAY);
  397. String command = cmd[0];
  398. Array data = cmd[1];
  399. if (command == "step") {
  400. script_debugger->set_depth(-1);
  401. script_debugger->set_lines_left(1);
  402. break;
  403. } else if (command == "next") {
  404. script_debugger->set_depth(0);
  405. script_debugger->set_lines_left(1);
  406. break;
  407. } else if (command == "continue") {
  408. script_debugger->set_depth(-1);
  409. script_debugger->set_lines_left(-1);
  410. DisplayServer::get_singleton()->window_move_to_foreground();
  411. break;
  412. } else if (command == "break") {
  413. ERR_PRINT("Got break when already broke!");
  414. break;
  415. } else if (command == "get_stack_dump") {
  416. DebuggerMarshalls::ScriptStackDump dump;
  417. int slc = script_lang->debug_get_stack_level_count();
  418. for (int i = 0; i < slc; i++) {
  419. ScriptLanguage::StackInfo frame;
  420. frame.file = script_lang->debug_get_stack_level_source(i);
  421. frame.line = script_lang->debug_get_stack_level_line(i);
  422. frame.func = script_lang->debug_get_stack_level_function(i);
  423. dump.frames.push_back(frame);
  424. }
  425. send_message("stack_dump", dump.serialize());
  426. } else if (command == "get_stack_frame_vars") {
  427. ERR_FAIL_COND(data.size() != 1);
  428. ERR_FAIL_COND(!script_lang);
  429. int lv = data[0];
  430. List<String> members;
  431. List<Variant> member_vals;
  432. if (ScriptInstance *inst = script_lang->debug_get_stack_level_instance(lv)) {
  433. members.push_back("self");
  434. member_vals.push_back(inst->get_owner());
  435. }
  436. script_lang->debug_get_stack_level_members(lv, &members, &member_vals);
  437. ERR_FAIL_COND(members.size() != member_vals.size());
  438. List<String> locals;
  439. List<Variant> local_vals;
  440. script_lang->debug_get_stack_level_locals(lv, &locals, &local_vals);
  441. ERR_FAIL_COND(locals.size() != local_vals.size());
  442. List<String> globals;
  443. List<Variant> globals_vals;
  444. script_lang->debug_get_globals(&globals, &globals_vals);
  445. ERR_FAIL_COND(globals.size() != globals_vals.size());
  446. Array var_size;
  447. var_size.push_back(local_vals.size() + member_vals.size() + globals_vals.size());
  448. send_message("stack_frame_vars", var_size);
  449. _send_stack_vars(locals, local_vals, 0);
  450. _send_stack_vars(members, member_vals, 1);
  451. _send_stack_vars(globals, globals_vals, 2);
  452. } else if (command == "reload_scripts") {
  453. reload_all_scripts = true;
  454. } else if (command == "breakpoint") {
  455. ERR_FAIL_COND(data.size() < 3);
  456. bool set = data[2];
  457. if (set) {
  458. script_debugger->insert_breakpoint(data[1], data[0]);
  459. } else {
  460. script_debugger->remove_breakpoint(data[1], data[0]);
  461. }
  462. } else if (command == "set_skip_breakpoints") {
  463. ERR_FAIL_COND(data.size() < 1);
  464. script_debugger->set_skip_breakpoints(data[0]);
  465. } else {
  466. bool captured = false;
  467. ERR_CONTINUE(_try_capture(command, data, captured) != OK);
  468. if (!captured) {
  469. WARN_PRINT("Unknown message received from debugger: " + command);
  470. }
  471. }
  472. } else {
  473. OS::get_singleton()->delay_usec(10000);
  474. OS::get_singleton()->process_and_drop_events();
  475. }
  476. // This is for the camera override to stay live even when the game is paused from the editor
  477. loop_time_sec = (OS::get_singleton()->get_ticks_usec() - loop_begin_usec) / 1000000.0f;
  478. RenderingServer::get_singleton()->sync();
  479. if (RenderingServer::get_singleton()->has_changed()) {
  480. RenderingServer::get_singleton()->draw(true, loop_time_sec * Engine::get_singleton()->get_time_scale());
  481. }
  482. }
  483. send_message("debug_exit", Array());
  484. if (mouse_mode != Input::MOUSE_MODE_VISIBLE) {
  485. Input::get_singleton()->set_mouse_mode(mouse_mode);
  486. }
  487. }
  488. void RemoteDebugger::poll_events(bool p_is_idle) {
  489. if (peer.is_null()) {
  490. return;
  491. }
  492. flush_output();
  493. peer->poll();
  494. while (peer->has_message()) {
  495. Array arr = peer->get_message();
  496. ERR_CONTINUE(arr.size() != 2);
  497. ERR_CONTINUE(arr[0].get_type() != Variant::STRING);
  498. ERR_CONTINUE(arr[1].get_type() != Variant::ARRAY);
  499. const String cmd = arr[0];
  500. const int idx = cmd.find(":");
  501. bool parsed = false;
  502. if (idx < 0) { // Not prefix, use scripts capture.
  503. capture_parse("core", cmd, arr[1], parsed);
  504. continue;
  505. }
  506. const String cap = cmd.substr(0, idx);
  507. if (!has_capture(cap)) {
  508. continue; // Unknown message...
  509. }
  510. const String msg = cmd.substr(idx + 1);
  511. capture_parse(cap, msg, arr[1], parsed);
  512. }
  513. // Reload scripts during idle poll only.
  514. if (p_is_idle && reload_all_scripts) {
  515. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  516. ScriptServer::get_language(i)->reload_all_scripts();
  517. }
  518. reload_all_scripts = false;
  519. }
  520. }
  521. Error RemoteDebugger::_core_capture(const String &p_cmd, const Array &p_data, bool &r_captured) {
  522. r_captured = true;
  523. if (p_cmd == "reload_scripts") {
  524. reload_all_scripts = true;
  525. } else if (p_cmd == "breakpoint") {
  526. ERR_FAIL_COND_V(p_data.size() < 3, ERR_INVALID_DATA);
  527. bool set = p_data[2];
  528. if (set) {
  529. script_debugger->insert_breakpoint(p_data[1], p_data[0]);
  530. } else {
  531. script_debugger->remove_breakpoint(p_data[1], p_data[0]);
  532. }
  533. } else if (p_cmd == "set_skip_breakpoints") {
  534. ERR_FAIL_COND_V(p_data.size() < 1, ERR_INVALID_DATA);
  535. script_debugger->set_skip_breakpoints(p_data[0]);
  536. } else if (p_cmd == "break") {
  537. script_debugger->debug(script_debugger->get_break_language());
  538. } else {
  539. r_captured = false;
  540. }
  541. return OK;
  542. }
  543. Error RemoteDebugger::_profiler_capture(const String &p_cmd, const Array &p_data, bool &r_captured) {
  544. r_captured = false;
  545. ERR_FAIL_COND_V(p_data.size() < 1, ERR_INVALID_DATA);
  546. ERR_FAIL_COND_V(p_data[0].get_type() != Variant::BOOL, ERR_INVALID_DATA);
  547. ERR_FAIL_COND_V(!has_profiler(p_cmd), ERR_UNAVAILABLE);
  548. Array opts;
  549. if (p_data.size() > 1) { // Optional profiler parameters.
  550. ERR_FAIL_COND_V(p_data[1].get_type() != Variant::ARRAY, ERR_INVALID_DATA);
  551. opts = p_data[1];
  552. }
  553. r_captured = true;
  554. profiler_enable(p_cmd, p_data[0], opts);
  555. return OK;
  556. }
  557. RemoteDebugger::RemoteDebugger(Ref<RemoteDebuggerPeer> p_peer) {
  558. peer = p_peer;
  559. max_chars_per_second = GLOBAL_GET("network/limits/debugger/max_chars_per_second");
  560. max_errors_per_second = GLOBAL_GET("network/limits/debugger/max_errors_per_second");
  561. max_warnings_per_second = GLOBAL_GET("network/limits/debugger/max_warnings_per_second");
  562. // Multiplayer Profiler
  563. multiplayer_profiler.instantiate();
  564. multiplayer_profiler->bind("multiplayer");
  565. // Performance Profiler
  566. Object *perf = Engine::get_singleton()->get_singleton_object("Performance");
  567. if (perf) {
  568. performance_profiler = Ref<PerformanceProfiler>(memnew(PerformanceProfiler(perf)));
  569. performance_profiler->bind("performance");
  570. profiler_enable("performance", true);
  571. }
  572. // Core and profiler captures.
  573. Capture core_cap(this,
  574. [](void *p_user, const String &p_cmd, const Array &p_data, bool &r_captured) {
  575. return ((RemoteDebugger *)p_user)->_core_capture(p_cmd, p_data, r_captured);
  576. });
  577. register_message_capture("core", core_cap);
  578. Capture profiler_cap(this,
  579. [](void *p_user, const String &p_cmd, const Array &p_data, bool &r_captured) {
  580. return ((RemoteDebugger *)p_user)->_profiler_capture(p_cmd, p_data, r_captured);
  581. });
  582. register_message_capture("profiler", profiler_cap);
  583. // Error handlers
  584. phl.printfunc = _print_handler;
  585. phl.userdata = this;
  586. add_print_handler(&phl);
  587. eh.errfunc = _err_handler;
  588. eh.userdata = this;
  589. add_error_handler(&eh);
  590. }
  591. RemoteDebugger::~RemoteDebugger() {
  592. remove_print_handler(&phl);
  593. remove_error_handler(&eh);
  594. }