remote_debugger.cpp 22 KB

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