remote_debugger.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /**************************************************************************/
  2. /* remote_debugger.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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::PerformanceProfiler : public EngineProfiler {
  40. Object *performance = nullptr;
  41. int last_perf_time = 0;
  42. uint64_t last_monitor_modification_time = 0;
  43. public:
  44. void toggle(bool p_enable, const Array &p_opts) {}
  45. void add(const Array &p_data) {}
  46. void tick(double p_frame_time, double p_process_time, double p_physics_time, double p_physics_frame_time) {
  47. if (!performance) {
  48. return;
  49. }
  50. uint64_t pt = OS::get_singleton()->get_ticks_msec();
  51. if (pt - last_perf_time < 1000) {
  52. return;
  53. }
  54. last_perf_time = pt;
  55. Array custom_monitor_names = performance->call("get_custom_monitor_names");
  56. uint64_t monitor_modification_time = performance->call("get_monitor_modification_time");
  57. if (monitor_modification_time > last_monitor_modification_time) {
  58. last_monitor_modification_time = monitor_modification_time;
  59. EngineDebugger::get_singleton()->send_message("performance:profile_names", custom_monitor_names);
  60. }
  61. int max = performance->get("MONITOR_MAX");
  62. Array arr;
  63. arr.resize(max + custom_monitor_names.size());
  64. for (int i = 0; i < max; i++) {
  65. arr[i] = performance->call("get_monitor", i);
  66. }
  67. for (int i = 0; i < custom_monitor_names.size(); i++) {
  68. Variant monitor_value = performance->call("get_custom_monitor", custom_monitor_names[i]);
  69. if (!monitor_value.is_num()) {
  70. ERR_PRINT("Value of custom monitor '" + String(custom_monitor_names[i]) + "' is not a number");
  71. arr[i + max] = Variant();
  72. } else {
  73. arr[i + max] = monitor_value;
  74. }
  75. }
  76. EngineDebugger::get_singleton()->send_message("performance:profile_frame", arr);
  77. }
  78. explicit PerformanceProfiler(Object *p_performance) {
  79. performance = p_performance;
  80. }
  81. };
  82. Error RemoteDebugger::_put_msg(String p_message, Array p_data) {
  83. Array msg;
  84. msg.push_back(p_message);
  85. msg.push_back(p_data);
  86. Error err = peer->put_message(msg);
  87. if (err != OK) {
  88. n_messages_dropped++;
  89. }
  90. return err;
  91. }
  92. 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) {
  93. if (p_type == ERR_HANDLER_SCRIPT) {
  94. return; //ignore script errors, those go through debugger
  95. }
  96. RemoteDebugger *rd = static_cast<RemoteDebugger *>(p_this);
  97. if (rd->flushing && Thread::get_caller_id() == rd->flush_thread) { // Can't handle recursive errors during flush.
  98. return;
  99. }
  100. Vector<ScriptLanguage::StackInfo> si;
  101. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  102. si = ScriptServer::get_language(i)->debug_get_current_stack_info();
  103. if (si.size()) {
  104. break;
  105. }
  106. }
  107. // send_error will lock internally.
  108. 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);
  109. }
  110. void RemoteDebugger::_print_handler(void *p_this, const String &p_string, bool p_error, bool p_rich) {
  111. RemoteDebugger *rd = static_cast<RemoteDebugger *>(p_this);
  112. if (rd->flushing && Thread::get_caller_id() == rd->flush_thread) { // Can't handle recursive prints during flush.
  113. return;
  114. }
  115. String s = p_string;
  116. int allowed_chars = MIN(MAX(rd->max_chars_per_second - rd->char_count, 0), s.length());
  117. if (allowed_chars == 0 && s.length() > 0) {
  118. return;
  119. }
  120. if (allowed_chars < s.length()) {
  121. s = s.substr(0, allowed_chars);
  122. }
  123. MutexLock lock(rd->mutex);
  124. rd->char_count += allowed_chars;
  125. bool overflowed = rd->char_count >= rd->max_chars_per_second;
  126. if (rd->is_peer_connected()) {
  127. if (overflowed) {
  128. s += "[...]";
  129. }
  130. OutputString output_string;
  131. output_string.message = s;
  132. if (p_error) {
  133. output_string.type = MESSAGE_TYPE_ERROR;
  134. } else if (p_rich) {
  135. output_string.type = MESSAGE_TYPE_LOG_RICH;
  136. } else {
  137. output_string.type = MESSAGE_TYPE_LOG;
  138. }
  139. rd->output_strings.push_back(output_string);
  140. if (overflowed) {
  141. output_string.message = "[output overflow, print less text!]";
  142. output_string.type = MESSAGE_TYPE_ERROR;
  143. rd->output_strings.push_back(output_string);
  144. }
  145. }
  146. }
  147. RemoteDebugger::ErrorMessage RemoteDebugger::_create_overflow_error(const String &p_what, const String &p_descr) {
  148. ErrorMessage oe;
  149. oe.error = p_what;
  150. oe.error_descr = p_descr;
  151. oe.warning = false;
  152. uint64_t time = OS::get_singleton()->get_ticks_msec();
  153. oe.hr = time / 3600000;
  154. oe.min = (time / 60000) % 60;
  155. oe.sec = (time / 1000) % 60;
  156. oe.msec = time % 1000;
  157. return oe;
  158. }
  159. void RemoteDebugger::flush_output() {
  160. flush_thread = Thread::get_caller_id();
  161. flushing = true;
  162. MutexLock lock(mutex);
  163. if (!is_peer_connected()) {
  164. return;
  165. }
  166. if (n_messages_dropped > 0) {
  167. 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.");
  168. if (_put_msg("error", err_msg.serialize()) == OK) {
  169. n_messages_dropped = 0;
  170. }
  171. }
  172. if (output_strings.size()) {
  173. // Join output strings so we generate less messages.
  174. Vector<String> joined_log_strings;
  175. Vector<String> strings;
  176. Vector<int> types;
  177. for (int i = 0; i < output_strings.size(); i++) {
  178. const OutputString &output_string = output_strings[i];
  179. if (output_string.type == MESSAGE_TYPE_ERROR) {
  180. if (!joined_log_strings.is_empty()) {
  181. strings.push_back(String("\n").join(joined_log_strings));
  182. types.push_back(MESSAGE_TYPE_LOG);
  183. joined_log_strings.clear();
  184. }
  185. strings.push_back(output_string.message);
  186. types.push_back(MESSAGE_TYPE_ERROR);
  187. } else if (output_string.type == MESSAGE_TYPE_LOG_RICH) {
  188. if (!joined_log_strings.is_empty()) {
  189. strings.push_back(String("\n").join(joined_log_strings));
  190. types.push_back(MESSAGE_TYPE_LOG_RICH);
  191. joined_log_strings.clear();
  192. }
  193. strings.push_back(output_string.message);
  194. types.push_back(MESSAGE_TYPE_LOG_RICH);
  195. } else {
  196. joined_log_strings.push_back(output_string.message);
  197. }
  198. }
  199. if (!joined_log_strings.is_empty()) {
  200. strings.push_back(String("\n").join(joined_log_strings));
  201. types.push_back(MESSAGE_TYPE_LOG);
  202. }
  203. Array arr;
  204. arr.push_back(strings);
  205. arr.push_back(types);
  206. _put_msg("output", arr);
  207. output_strings.clear();
  208. }
  209. while (errors.size()) {
  210. ErrorMessage oe = errors.front()->get();
  211. _put_msg("error", oe.serialize());
  212. errors.pop_front();
  213. }
  214. // Update limits
  215. uint64_t ticks = OS::get_singleton()->get_ticks_usec() / 1000;
  216. if (ticks - last_reset > 1000) {
  217. last_reset = ticks;
  218. char_count = 0;
  219. err_count = 0;
  220. n_errors_dropped = 0;
  221. warn_count = 0;
  222. n_warnings_dropped = 0;
  223. }
  224. flushing = false;
  225. }
  226. void RemoteDebugger::send_message(const String &p_message, const Array &p_args) {
  227. MutexLock lock(mutex);
  228. if (is_peer_connected()) {
  229. _put_msg(p_message, p_args);
  230. }
  231. }
  232. 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) {
  233. ErrorMessage oe;
  234. oe.error = p_err;
  235. oe.error_descr = p_descr;
  236. oe.source_file = p_file;
  237. oe.source_line = p_line;
  238. oe.source_func = p_func;
  239. oe.warning = p_type == ERR_HANDLER_WARNING;
  240. uint64_t time = OS::get_singleton()->get_ticks_msec();
  241. oe.hr = time / 3600000;
  242. oe.min = (time / 60000) % 60;
  243. oe.sec = (time / 1000) % 60;
  244. oe.msec = time % 1000;
  245. oe.callstack.append_array(script_debugger->get_error_stack_info());
  246. if (flushing && Thread::get_caller_id() == flush_thread) { // Can't handle recursive errors during flush.
  247. return;
  248. }
  249. MutexLock lock(mutex);
  250. if (oe.warning) {
  251. warn_count++;
  252. } else {
  253. err_count++;
  254. }
  255. if (is_peer_connected()) {
  256. if (oe.warning) {
  257. if (warn_count > max_warnings_per_second) {
  258. n_warnings_dropped++;
  259. if (n_warnings_dropped == 1) {
  260. // Only print one message about dropping per second
  261. ErrorMessage overflow = _create_overflow_error("TOO_MANY_WARNINGS", "Too many warnings! Ignoring warnings for up to 1 second.");
  262. errors.push_back(overflow);
  263. }
  264. } else {
  265. errors.push_back(oe);
  266. }
  267. } else {
  268. if (err_count > max_errors_per_second) {
  269. n_errors_dropped++;
  270. if (n_errors_dropped == 1) {
  271. // Only print one message about dropping per second
  272. ErrorMessage overflow = _create_overflow_error("TOO_MANY_ERRORS", "Too many errors! Ignoring errors for up to 1 second.");
  273. errors.push_back(overflow);
  274. }
  275. } else {
  276. errors.push_back(oe);
  277. }
  278. }
  279. }
  280. }
  281. void RemoteDebugger::_send_stack_vars(List<String> &p_names, List<Variant> &p_vals, int p_type) {
  282. DebuggerMarshalls::ScriptStackVariable stvar;
  283. List<String>::Element *E = p_names.front();
  284. List<Variant>::Element *F = p_vals.front();
  285. while (E) {
  286. stvar.name = E->get();
  287. stvar.value = F->get();
  288. stvar.type = p_type;
  289. send_message("stack_frame_var", stvar.serialize());
  290. E = E->next();
  291. F = F->next();
  292. }
  293. }
  294. Error RemoteDebugger::_try_capture(const String &p_msg, const Array &p_data, bool &r_captured) {
  295. const int idx = p_msg.find(":");
  296. r_captured = false;
  297. if (idx < 0) { // No prefix, unknown message.
  298. return OK;
  299. }
  300. const String cap = p_msg.substr(0, idx);
  301. if (!has_capture(cap)) {
  302. return ERR_UNAVAILABLE; // Unknown message...
  303. }
  304. const String msg = p_msg.substr(idx + 1);
  305. return capture_parse(cap, msg, p_data, r_captured);
  306. }
  307. void RemoteDebugger::debug(bool p_can_continue, bool p_is_error_breakpoint) {
  308. //this function is called when there is a debugger break (bug on script)
  309. //or when execution is paused from editor
  310. if (script_debugger->is_skipping_breakpoints() && !p_is_error_breakpoint) {
  311. return;
  312. }
  313. ERR_FAIL_COND_MSG(!is_peer_connected(), "Script Debugger failed to connect, but being used anyway.");
  314. if (!peer->can_block()) {
  315. return; // Peer does not support blocking IO. We could at least send the error though.
  316. }
  317. ScriptLanguage *script_lang = script_debugger->get_break_language();
  318. const String error_str = script_lang ? script_lang->debug_get_error() : "";
  319. Array msg;
  320. msg.push_back(p_can_continue);
  321. msg.push_back(error_str);
  322. ERR_FAIL_COND(!script_lang);
  323. msg.push_back(script_lang->debug_get_stack_level_count() > 0);
  324. if (allow_focus_steal_fn) {
  325. allow_focus_steal_fn();
  326. }
  327. send_message("debug_enter", msg);
  328. Input::MouseMode mouse_mode = Input::get_singleton()->get_mouse_mode();
  329. if (mouse_mode != Input::MOUSE_MODE_VISIBLE) {
  330. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  331. }
  332. while (is_peer_connected()) {
  333. flush_output();
  334. peer->poll();
  335. if (peer->has_message()) {
  336. Array cmd = peer->get_message();
  337. ERR_CONTINUE(cmd.size() != 2);
  338. ERR_CONTINUE(cmd[0].get_type() != Variant::STRING);
  339. ERR_CONTINUE(cmd[1].get_type() != Variant::ARRAY);
  340. String command = cmd[0];
  341. Array data = cmd[1];
  342. if (command == "step") {
  343. script_debugger->set_depth(-1);
  344. script_debugger->set_lines_left(1);
  345. break;
  346. } else if (command == "next") {
  347. script_debugger->set_depth(0);
  348. script_debugger->set_lines_left(1);
  349. break;
  350. } else if (command == "continue") {
  351. script_debugger->set_depth(-1);
  352. script_debugger->set_lines_left(-1);
  353. break;
  354. } else if (command == "break") {
  355. ERR_PRINT("Got break when already broke!");
  356. break;
  357. } else if (command == "get_stack_dump") {
  358. DebuggerMarshalls::ScriptStackDump dump;
  359. int slc = script_lang->debug_get_stack_level_count();
  360. for (int i = 0; i < slc; i++) {
  361. ScriptLanguage::StackInfo frame;
  362. frame.file = script_lang->debug_get_stack_level_source(i);
  363. frame.line = script_lang->debug_get_stack_level_line(i);
  364. frame.func = script_lang->debug_get_stack_level_function(i);
  365. dump.frames.push_back(frame);
  366. }
  367. send_message("stack_dump", dump.serialize());
  368. } else if (command == "get_stack_frame_vars") {
  369. ERR_FAIL_COND(data.size() != 1);
  370. ERR_FAIL_COND(!script_lang);
  371. int lv = data[0];
  372. List<String> members;
  373. List<Variant> member_vals;
  374. if (ScriptInstance *inst = script_lang->debug_get_stack_level_instance(lv)) {
  375. members.push_back("self");
  376. member_vals.push_back(inst->get_owner());
  377. }
  378. script_lang->debug_get_stack_level_members(lv, &members, &member_vals);
  379. ERR_FAIL_COND(members.size() != member_vals.size());
  380. List<String> locals;
  381. List<Variant> local_vals;
  382. script_lang->debug_get_stack_level_locals(lv, &locals, &local_vals);
  383. ERR_FAIL_COND(locals.size() != local_vals.size());
  384. List<String> globals;
  385. List<Variant> globals_vals;
  386. script_lang->debug_get_globals(&globals, &globals_vals);
  387. ERR_FAIL_COND(globals.size() != globals_vals.size());
  388. Array var_size;
  389. var_size.push_back(local_vals.size() + member_vals.size() + globals_vals.size());
  390. send_message("stack_frame_vars", var_size);
  391. _send_stack_vars(locals, local_vals, 0);
  392. _send_stack_vars(members, member_vals, 1);
  393. _send_stack_vars(globals, globals_vals, 2);
  394. } else if (command == "reload_scripts") {
  395. reload_all_scripts = true;
  396. } else if (command == "breakpoint") {
  397. ERR_FAIL_COND(data.size() < 3);
  398. bool set = data[2];
  399. if (set) {
  400. script_debugger->insert_breakpoint(data[1], data[0]);
  401. } else {
  402. script_debugger->remove_breakpoint(data[1], data[0]);
  403. }
  404. } else if (command == "set_skip_breakpoints") {
  405. ERR_FAIL_COND(data.size() < 1);
  406. script_debugger->set_skip_breakpoints(data[0]);
  407. } else {
  408. bool captured = false;
  409. ERR_CONTINUE(_try_capture(command, data, captured) != OK);
  410. if (!captured) {
  411. WARN_PRINT("Unknown message received from debugger: " + command);
  412. }
  413. }
  414. } else {
  415. OS::get_singleton()->delay_usec(10000);
  416. OS::get_singleton()->process_and_drop_events();
  417. }
  418. }
  419. send_message("debug_exit", Array());
  420. if (mouse_mode != Input::MOUSE_MODE_VISIBLE) {
  421. Input::get_singleton()->set_mouse_mode(mouse_mode);
  422. }
  423. }
  424. void RemoteDebugger::poll_events(bool p_is_idle) {
  425. if (peer.is_null()) {
  426. return;
  427. }
  428. flush_output();
  429. peer->poll();
  430. while (peer->has_message()) {
  431. Array arr = peer->get_message();
  432. ERR_CONTINUE(arr.size() != 2);
  433. ERR_CONTINUE(arr[0].get_type() != Variant::STRING);
  434. ERR_CONTINUE(arr[1].get_type() != Variant::ARRAY);
  435. const String cmd = arr[0];
  436. const int idx = cmd.find(":");
  437. bool parsed = false;
  438. if (idx < 0) { // Not prefix, use scripts capture.
  439. capture_parse("core", cmd, arr[1], parsed);
  440. continue;
  441. }
  442. const String cap = cmd.substr(0, idx);
  443. if (!has_capture(cap)) {
  444. continue; // Unknown message...
  445. }
  446. const String msg = cmd.substr(idx + 1);
  447. capture_parse(cap, msg, arr[1], parsed);
  448. }
  449. // Reload scripts during idle poll only.
  450. if (p_is_idle && reload_all_scripts) {
  451. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  452. ScriptServer::get_language(i)->reload_all_scripts();
  453. }
  454. reload_all_scripts = false;
  455. }
  456. }
  457. Error RemoteDebugger::_core_capture(const String &p_cmd, const Array &p_data, bool &r_captured) {
  458. r_captured = true;
  459. if (p_cmd == "reload_scripts") {
  460. reload_all_scripts = true;
  461. } else if (p_cmd == "breakpoint") {
  462. ERR_FAIL_COND_V(p_data.size() < 3, ERR_INVALID_DATA);
  463. bool set = p_data[2];
  464. if (set) {
  465. script_debugger->insert_breakpoint(p_data[1], p_data[0]);
  466. } else {
  467. script_debugger->remove_breakpoint(p_data[1], p_data[0]);
  468. }
  469. } else if (p_cmd == "set_skip_breakpoints") {
  470. ERR_FAIL_COND_V(p_data.size() < 1, ERR_INVALID_DATA);
  471. script_debugger->set_skip_breakpoints(p_data[0]);
  472. } else if (p_cmd == "break") {
  473. script_debugger->debug(script_debugger->get_break_language());
  474. } else {
  475. r_captured = false;
  476. }
  477. return OK;
  478. }
  479. Error RemoteDebugger::_profiler_capture(const String &p_cmd, const Array &p_data, bool &r_captured) {
  480. r_captured = false;
  481. ERR_FAIL_COND_V(p_data.size() < 1, ERR_INVALID_DATA);
  482. ERR_FAIL_COND_V(p_data[0].get_type() != Variant::BOOL, ERR_INVALID_DATA);
  483. ERR_FAIL_COND_V(!has_profiler(p_cmd), ERR_UNAVAILABLE);
  484. Array opts;
  485. if (p_data.size() > 1) { // Optional profiler parameters.
  486. ERR_FAIL_COND_V(p_data[1].get_type() != Variant::ARRAY, ERR_INVALID_DATA);
  487. opts = p_data[1];
  488. }
  489. r_captured = true;
  490. profiler_enable(p_cmd, p_data[0], opts);
  491. return OK;
  492. }
  493. RemoteDebugger::RemoteDebugger(Ref<RemoteDebuggerPeer> p_peer) {
  494. peer = p_peer;
  495. max_chars_per_second = GLOBAL_GET("network/limits/debugger/max_chars_per_second");
  496. max_errors_per_second = GLOBAL_GET("network/limits/debugger/max_errors_per_second");
  497. max_warnings_per_second = GLOBAL_GET("network/limits/debugger/max_warnings_per_second");
  498. // Performance Profiler
  499. Object *perf = Engine::get_singleton()->get_singleton_object("Performance");
  500. if (perf) {
  501. performance_profiler = Ref<PerformanceProfiler>(memnew(PerformanceProfiler(perf)));
  502. performance_profiler->bind("performance");
  503. profiler_enable("performance", true);
  504. }
  505. // Core and profiler captures.
  506. Capture core_cap(this,
  507. [](void *p_user, const String &p_cmd, const Array &p_data, bool &r_captured) {
  508. return static_cast<RemoteDebugger *>(p_user)->_core_capture(p_cmd, p_data, r_captured);
  509. });
  510. register_message_capture("core", core_cap);
  511. Capture profiler_cap(this,
  512. [](void *p_user, const String &p_cmd, const Array &p_data, bool &r_captured) {
  513. return static_cast<RemoteDebugger *>(p_user)->_profiler_capture(p_cmd, p_data, r_captured);
  514. });
  515. register_message_capture("profiler", profiler_cap);
  516. // Error handlers
  517. phl.printfunc = _print_handler;
  518. phl.userdata = this;
  519. add_print_handler(&phl);
  520. eh.errfunc = _err_handler;
  521. eh.userdata = this;
  522. add_error_handler(&eh);
  523. }
  524. RemoteDebugger::~RemoteDebugger() {
  525. remove_print_handler(&phl);
  526. remove_error_handler(&eh);
  527. }