remote_debugger.cpp 22 KB

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