remote_debugger.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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_process_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_process_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. } else {
  145. arr[i + max] = monitor_value;
  146. }
  147. }
  148. EngineDebugger::get_singleton()->send_message("performance:profile_frame", arr);
  149. }
  150. explicit PerformanceProfiler(Object *p_performance) {
  151. performance = p_performance;
  152. }
  153. };
  154. Error RemoteDebugger::_put_msg(String p_message, Array p_data) {
  155. Array msg;
  156. msg.push_back(p_message);
  157. msg.push_back(p_data);
  158. Error err = peer->put_message(msg);
  159. if (err != OK) {
  160. n_messages_dropped++;
  161. }
  162. return err;
  163. }
  164. 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) {
  165. if (p_type == ERR_HANDLER_SCRIPT) {
  166. return; //ignore script errors, those go through debugger
  167. }
  168. RemoteDebugger *rd = static_cast<RemoteDebugger *>(p_this);
  169. if (rd->flushing && Thread::get_caller_id() == rd->flush_thread) { // Can't handle recursive errors during flush.
  170. return;
  171. }
  172. Vector<ScriptLanguage::StackInfo> si;
  173. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  174. si = ScriptServer::get_language(i)->debug_get_current_stack_info();
  175. if (si.size()) {
  176. break;
  177. }
  178. }
  179. // send_error will lock internally.
  180. 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);
  181. }
  182. void RemoteDebugger::_print_handler(void *p_this, const String &p_string, bool p_error, bool p_rich) {
  183. RemoteDebugger *rd = static_cast<RemoteDebugger *>(p_this);
  184. if (rd->flushing && Thread::get_caller_id() == rd->flush_thread) { // Can't handle recursive prints during flush.
  185. return;
  186. }
  187. String s = p_string;
  188. int allowed_chars = MIN(MAX(rd->max_chars_per_second - rd->char_count, 0), s.length());
  189. if (allowed_chars == 0 && s.length() > 0) {
  190. return;
  191. }
  192. if (allowed_chars < s.length()) {
  193. s = s.substr(0, allowed_chars);
  194. }
  195. MutexLock lock(rd->mutex);
  196. rd->char_count += allowed_chars;
  197. bool overflowed = rd->char_count >= rd->max_chars_per_second;
  198. if (rd->is_peer_connected()) {
  199. if (overflowed) {
  200. s += "[...]";
  201. }
  202. OutputString output_string;
  203. output_string.message = s;
  204. if (p_error) {
  205. output_string.type = MESSAGE_TYPE_ERROR;
  206. } else if (p_rich) {
  207. output_string.type = MESSAGE_TYPE_LOG_RICH;
  208. } else {
  209. output_string.type = MESSAGE_TYPE_LOG;
  210. }
  211. rd->output_strings.push_back(output_string);
  212. if (overflowed) {
  213. output_string.message = "[output overflow, print less text!]";
  214. output_string.type = MESSAGE_TYPE_ERROR;
  215. rd->output_strings.push_back(output_string);
  216. }
  217. }
  218. }
  219. RemoteDebugger::ErrorMessage RemoteDebugger::_create_overflow_error(const String &p_what, const String &p_descr) {
  220. ErrorMessage oe;
  221. oe.error = p_what;
  222. oe.error_descr = p_descr;
  223. oe.warning = false;
  224. uint64_t time = OS::get_singleton()->get_ticks_msec();
  225. oe.hr = time / 3600000;
  226. oe.min = (time / 60000) % 60;
  227. oe.sec = (time / 1000) % 60;
  228. oe.msec = time % 1000;
  229. return oe;
  230. }
  231. void RemoteDebugger::flush_output() {
  232. flush_thread = Thread::get_caller_id();
  233. flushing = true;
  234. MutexLock lock(mutex);
  235. if (!is_peer_connected()) {
  236. return;
  237. }
  238. if (n_messages_dropped > 0) {
  239. 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.");
  240. if (_put_msg("error", err_msg.serialize()) == OK) {
  241. n_messages_dropped = 0;
  242. }
  243. }
  244. if (output_strings.size()) {
  245. // Join output strings so we generate less messages.
  246. Vector<String> joined_log_strings;
  247. Vector<String> strings;
  248. Vector<int> types;
  249. for (int i = 0; i < output_strings.size(); i++) {
  250. const OutputString &output_string = output_strings[i];
  251. if (output_string.type == MESSAGE_TYPE_ERROR) {
  252. if (!joined_log_strings.is_empty()) {
  253. strings.push_back(String("\n").join(joined_log_strings));
  254. types.push_back(MESSAGE_TYPE_LOG);
  255. joined_log_strings.clear();
  256. }
  257. strings.push_back(output_string.message);
  258. types.push_back(MESSAGE_TYPE_ERROR);
  259. } else if (output_string.type == MESSAGE_TYPE_LOG_RICH) {
  260. if (!joined_log_strings.is_empty()) {
  261. strings.push_back(String("\n").join(joined_log_strings));
  262. types.push_back(MESSAGE_TYPE_LOG_RICH);
  263. joined_log_strings.clear();
  264. }
  265. strings.push_back(output_string.message);
  266. types.push_back(MESSAGE_TYPE_LOG_RICH);
  267. } else {
  268. joined_log_strings.push_back(output_string.message);
  269. }
  270. }
  271. if (!joined_log_strings.is_empty()) {
  272. strings.push_back(String("\n").join(joined_log_strings));
  273. types.push_back(MESSAGE_TYPE_LOG);
  274. }
  275. Array arr;
  276. arr.push_back(strings);
  277. arr.push_back(types);
  278. _put_msg("output", arr);
  279. output_strings.clear();
  280. }
  281. while (errors.size()) {
  282. ErrorMessage oe = errors.front()->get();
  283. _put_msg("error", oe.serialize());
  284. errors.pop_front();
  285. }
  286. // Update limits
  287. uint64_t ticks = OS::get_singleton()->get_ticks_usec() / 1000;
  288. if (ticks - last_reset > 1000) {
  289. last_reset = ticks;
  290. char_count = 0;
  291. err_count = 0;
  292. n_errors_dropped = 0;
  293. warn_count = 0;
  294. n_warnings_dropped = 0;
  295. }
  296. flushing = false;
  297. }
  298. void RemoteDebugger::send_message(const String &p_message, const Array &p_args) {
  299. MutexLock lock(mutex);
  300. if (is_peer_connected()) {
  301. _put_msg(p_message, p_args);
  302. }
  303. }
  304. 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) {
  305. ErrorMessage oe;
  306. oe.error = p_err;
  307. oe.error_descr = p_descr;
  308. oe.source_file = p_file;
  309. oe.source_line = p_line;
  310. oe.source_func = p_func;
  311. oe.warning = p_type == ERR_HANDLER_WARNING;
  312. uint64_t time = OS::get_singleton()->get_ticks_msec();
  313. oe.hr = time / 3600000;
  314. oe.min = (time / 60000) % 60;
  315. oe.sec = (time / 1000) % 60;
  316. oe.msec = time % 1000;
  317. oe.callstack.append_array(script_debugger->get_error_stack_info());
  318. if (flushing && Thread::get_caller_id() == flush_thread) { // Can't handle recursive errors during flush.
  319. return;
  320. }
  321. MutexLock lock(mutex);
  322. if (oe.warning) {
  323. warn_count++;
  324. } else {
  325. err_count++;
  326. }
  327. if (is_peer_connected()) {
  328. if (oe.warning) {
  329. if (warn_count > max_warnings_per_second) {
  330. n_warnings_dropped++;
  331. if (n_warnings_dropped == 1) {
  332. // Only print one message about dropping per second
  333. ErrorMessage overflow = _create_overflow_error("TOO_MANY_WARNINGS", "Too many warnings! Ignoring warnings for up to 1 second.");
  334. errors.push_back(overflow);
  335. }
  336. } else {
  337. errors.push_back(oe);
  338. }
  339. } else {
  340. if (err_count > max_errors_per_second) {
  341. n_errors_dropped++;
  342. if (n_errors_dropped == 1) {
  343. // Only print one message about dropping per second
  344. ErrorMessage overflow = _create_overflow_error("TOO_MANY_ERRORS", "Too many errors! Ignoring errors for up to 1 second.");
  345. errors.push_back(overflow);
  346. }
  347. } else {
  348. errors.push_back(oe);
  349. }
  350. }
  351. }
  352. }
  353. void RemoteDebugger::_send_stack_vars(List<String> &p_names, List<Variant> &p_vals, int p_type) {
  354. DebuggerMarshalls::ScriptStackVariable stvar;
  355. List<String>::Element *E = p_names.front();
  356. List<Variant>::Element *F = p_vals.front();
  357. while (E) {
  358. stvar.name = E->get();
  359. stvar.value = F->get();
  360. stvar.type = p_type;
  361. send_message("stack_frame_var", stvar.serialize());
  362. E = E->next();
  363. F = F->next();
  364. }
  365. }
  366. Error RemoteDebugger::_try_capture(const String &p_msg, const Array &p_data, bool &r_captured) {
  367. const int idx = p_msg.find(":");
  368. r_captured = false;
  369. if (idx < 0) { // No prefix, unknown message.
  370. return OK;
  371. }
  372. const String cap = p_msg.substr(0, idx);
  373. if (!has_capture(cap)) {
  374. return ERR_UNAVAILABLE; // Unknown message...
  375. }
  376. const String msg = p_msg.substr(idx + 1);
  377. return capture_parse(cap, msg, p_data, r_captured);
  378. }
  379. void RemoteDebugger::debug(bool p_can_continue, bool p_is_error_breakpoint) {
  380. //this function is called when there is a debugger break (bug on script)
  381. //or when execution is paused from editor
  382. if (script_debugger->is_skipping_breakpoints() && !p_is_error_breakpoint) {
  383. return;
  384. }
  385. ERR_FAIL_COND_MSG(!is_peer_connected(), "Script Debugger failed to connect, but being used anyway.");
  386. if (!peer->can_block()) {
  387. return; // Peer does not support blocking IO. We could at least send the error though.
  388. }
  389. ScriptLanguage *script_lang = script_debugger->get_break_language();
  390. const String error_str = script_lang ? script_lang->debug_get_error() : "";
  391. Array msg;
  392. msg.push_back(p_can_continue);
  393. msg.push_back(error_str);
  394. ERR_FAIL_COND(!script_lang);
  395. msg.push_back(script_lang->debug_get_stack_level_count() > 0);
  396. send_message("debug_enter", msg);
  397. Input::MouseMode mouse_mode = Input::get_singleton()->get_mouse_mode();
  398. if (mouse_mode != Input::MOUSE_MODE_VISIBLE) {
  399. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  400. }
  401. while (is_peer_connected()) {
  402. flush_output();
  403. peer->poll();
  404. if (peer->has_message()) {
  405. Array cmd = peer->get_message();
  406. ERR_CONTINUE(cmd.size() != 2);
  407. ERR_CONTINUE(cmd[0].get_type() != Variant::STRING);
  408. ERR_CONTINUE(cmd[1].get_type() != Variant::ARRAY);
  409. String command = cmd[0];
  410. Array data = cmd[1];
  411. if (command == "step") {
  412. script_debugger->set_depth(-1);
  413. script_debugger->set_lines_left(1);
  414. break;
  415. } else if (command == "next") {
  416. script_debugger->set_depth(0);
  417. script_debugger->set_lines_left(1);
  418. break;
  419. } else if (command == "continue") {
  420. script_debugger->set_depth(-1);
  421. script_debugger->set_lines_left(-1);
  422. break;
  423. } else if (command == "break") {
  424. ERR_PRINT("Got break when already broke!");
  425. break;
  426. } else if (command == "get_stack_dump") {
  427. DebuggerMarshalls::ScriptStackDump dump;
  428. int slc = script_lang->debug_get_stack_level_count();
  429. for (int i = 0; i < slc; i++) {
  430. ScriptLanguage::StackInfo frame;
  431. frame.file = script_lang->debug_get_stack_level_source(i);
  432. frame.line = script_lang->debug_get_stack_level_line(i);
  433. frame.func = script_lang->debug_get_stack_level_function(i);
  434. dump.frames.push_back(frame);
  435. }
  436. send_message("stack_dump", dump.serialize());
  437. } else if (command == "get_stack_frame_vars") {
  438. ERR_FAIL_COND(data.size() != 1);
  439. ERR_FAIL_COND(!script_lang);
  440. int lv = data[0];
  441. List<String> members;
  442. List<Variant> member_vals;
  443. if (ScriptInstance *inst = script_lang->debug_get_stack_level_instance(lv)) {
  444. members.push_back("self");
  445. member_vals.push_back(inst->get_owner());
  446. }
  447. script_lang->debug_get_stack_level_members(lv, &members, &member_vals);
  448. ERR_FAIL_COND(members.size() != member_vals.size());
  449. List<String> locals;
  450. List<Variant> local_vals;
  451. script_lang->debug_get_stack_level_locals(lv, &locals, &local_vals);
  452. ERR_FAIL_COND(locals.size() != local_vals.size());
  453. List<String> globals;
  454. List<Variant> globals_vals;
  455. script_lang->debug_get_globals(&globals, &globals_vals);
  456. ERR_FAIL_COND(globals.size() != globals_vals.size());
  457. Array var_size;
  458. var_size.push_back(local_vals.size() + member_vals.size() + globals_vals.size());
  459. send_message("stack_frame_vars", var_size);
  460. _send_stack_vars(locals, local_vals, 0);
  461. _send_stack_vars(members, member_vals, 1);
  462. _send_stack_vars(globals, globals_vals, 2);
  463. } else if (command == "reload_scripts") {
  464. reload_all_scripts = true;
  465. } else if (command == "breakpoint") {
  466. ERR_FAIL_COND(data.size() < 3);
  467. bool set = data[2];
  468. if (set) {
  469. script_debugger->insert_breakpoint(data[1], data[0]);
  470. } else {
  471. script_debugger->remove_breakpoint(data[1], data[0]);
  472. }
  473. } else if (command == "set_skip_breakpoints") {
  474. ERR_FAIL_COND(data.size() < 1);
  475. script_debugger->set_skip_breakpoints(data[0]);
  476. } else {
  477. bool captured = false;
  478. ERR_CONTINUE(_try_capture(command, data, captured) != OK);
  479. if (!captured) {
  480. WARN_PRINT("Unknown message received from debugger: " + command);
  481. }
  482. }
  483. } else {
  484. OS::get_singleton()->delay_usec(10000);
  485. OS::get_singleton()->process_and_drop_events();
  486. }
  487. }
  488. send_message("debug_exit", Array());
  489. if (mouse_mode != Input::MOUSE_MODE_VISIBLE) {
  490. Input::get_singleton()->set_mouse_mode(mouse_mode);
  491. }
  492. }
  493. void RemoteDebugger::poll_events(bool p_is_idle) {
  494. if (peer.is_null()) {
  495. return;
  496. }
  497. flush_output();
  498. peer->poll();
  499. while (peer->has_message()) {
  500. Array arr = peer->get_message();
  501. ERR_CONTINUE(arr.size() != 2);
  502. ERR_CONTINUE(arr[0].get_type() != Variant::STRING);
  503. ERR_CONTINUE(arr[1].get_type() != Variant::ARRAY);
  504. const String cmd = arr[0];
  505. const int idx = cmd.find(":");
  506. bool parsed = false;
  507. if (idx < 0) { // Not prefix, use scripts capture.
  508. capture_parse("core", cmd, arr[1], parsed);
  509. continue;
  510. }
  511. const String cap = cmd.substr(0, idx);
  512. if (!has_capture(cap)) {
  513. continue; // Unknown message...
  514. }
  515. const String msg = cmd.substr(idx + 1);
  516. capture_parse(cap, msg, arr[1], parsed);
  517. }
  518. // Reload scripts during idle poll only.
  519. if (p_is_idle && reload_all_scripts) {
  520. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  521. ScriptServer::get_language(i)->reload_all_scripts();
  522. }
  523. reload_all_scripts = false;
  524. }
  525. }
  526. Error RemoteDebugger::_core_capture(const String &p_cmd, const Array &p_data, bool &r_captured) {
  527. r_captured = true;
  528. if (p_cmd == "reload_scripts") {
  529. reload_all_scripts = true;
  530. } else if (p_cmd == "breakpoint") {
  531. ERR_FAIL_COND_V(p_data.size() < 3, ERR_INVALID_DATA);
  532. bool set = p_data[2];
  533. if (set) {
  534. script_debugger->insert_breakpoint(p_data[1], p_data[0]);
  535. } else {
  536. script_debugger->remove_breakpoint(p_data[1], p_data[0]);
  537. }
  538. } else if (p_cmd == "set_skip_breakpoints") {
  539. ERR_FAIL_COND_V(p_data.size() < 1, ERR_INVALID_DATA);
  540. script_debugger->set_skip_breakpoints(p_data[0]);
  541. } else if (p_cmd == "break") {
  542. script_debugger->debug(script_debugger->get_break_language());
  543. } else {
  544. r_captured = false;
  545. }
  546. return OK;
  547. }
  548. Error RemoteDebugger::_profiler_capture(const String &p_cmd, const Array &p_data, bool &r_captured) {
  549. r_captured = false;
  550. ERR_FAIL_COND_V(p_data.size() < 1, ERR_INVALID_DATA);
  551. ERR_FAIL_COND_V(p_data[0].get_type() != Variant::BOOL, ERR_INVALID_DATA);
  552. ERR_FAIL_COND_V(!has_profiler(p_cmd), ERR_UNAVAILABLE);
  553. Array opts;
  554. if (p_data.size() > 1) { // Optional profiler parameters.
  555. ERR_FAIL_COND_V(p_data[1].get_type() != Variant::ARRAY, ERR_INVALID_DATA);
  556. opts = p_data[1];
  557. }
  558. r_captured = true;
  559. profiler_enable(p_cmd, p_data[0], opts);
  560. return OK;
  561. }
  562. RemoteDebugger::RemoteDebugger(Ref<RemoteDebuggerPeer> p_peer) {
  563. peer = p_peer;
  564. max_chars_per_second = GLOBAL_GET("network/limits/debugger/max_chars_per_second");
  565. max_errors_per_second = GLOBAL_GET("network/limits/debugger/max_errors_per_second");
  566. max_warnings_per_second = GLOBAL_GET("network/limits/debugger/max_warnings_per_second");
  567. // Multiplayer Profiler
  568. multiplayer_profiler.instantiate();
  569. multiplayer_profiler->bind("multiplayer");
  570. // Performance Profiler
  571. Object *perf = Engine::get_singleton()->get_singleton_object("Performance");
  572. if (perf) {
  573. performance_profiler = Ref<PerformanceProfiler>(memnew(PerformanceProfiler(perf)));
  574. performance_profiler->bind("performance");
  575. profiler_enable("performance", true);
  576. }
  577. // Core and profiler captures.
  578. Capture core_cap(this,
  579. [](void *p_user, const String &p_cmd, const Array &p_data, bool &r_captured) {
  580. return static_cast<RemoteDebugger *>(p_user)->_core_capture(p_cmd, p_data, r_captured);
  581. });
  582. register_message_capture("core", core_cap);
  583. Capture profiler_cap(this,
  584. [](void *p_user, const String &p_cmd, const Array &p_data, bool &r_captured) {
  585. return static_cast<RemoteDebugger *>(p_user)->_profiler_capture(p_cmd, p_data, r_captured);
  586. });
  587. register_message_capture("profiler", profiler_cap);
  588. // Error handlers
  589. phl.printfunc = _print_handler;
  590. phl.userdata = this;
  591. add_print_handler(&phl);
  592. eh.errfunc = _err_handler;
  593. eh.userdata = this;
  594. add_error_handler(&eh);
  595. }
  596. RemoteDebugger::~RemoteDebugger() {
  597. remove_print_handler(&phl);
  598. remove_error_handler(&eh);
  599. }