remote_debugger.cpp 22 KB

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