editor_debugger_node.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /*************************************************************************/
  2. /* editor_debugger_node.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "editor_debugger_node.h"
  31. #include "editor/debugger/editor_debugger_tree.h"
  32. #include "editor/debugger/script_editor_debugger.h"
  33. #include "editor/editor_log.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/plugins/editor_debugger_plugin.h"
  36. #include "editor/plugins/script_editor_plugin.h"
  37. #include "scene/gui/menu_button.h"
  38. #include "scene/gui/tab_container.h"
  39. template <typename Func>
  40. void _for_all(TabContainer *p_node, const Func &p_func) {
  41. for (int i = 0; i < p_node->get_tab_count(); i++) {
  42. ScriptEditorDebugger *dbg = Object::cast_to<ScriptEditorDebugger>(p_node->get_tab_control(i));
  43. ERR_FAIL_COND(!dbg);
  44. p_func(dbg);
  45. }
  46. }
  47. EditorDebuggerNode *EditorDebuggerNode::singleton = nullptr;
  48. EditorDebuggerNode::EditorDebuggerNode() {
  49. if (!singleton) {
  50. singleton = this;
  51. }
  52. add_theme_constant_override("margin_left", -EditorNode::get_singleton()->get_gui_base()->get_theme_stylebox("BottomPanelDebuggerOverride", "EditorStyles")->get_margin(SIDE_LEFT));
  53. add_theme_constant_override("margin_right", -EditorNode::get_singleton()->get_gui_base()->get_theme_stylebox("BottomPanelDebuggerOverride", "EditorStyles")->get_margin(SIDE_RIGHT));
  54. tabs = memnew(TabContainer);
  55. tabs->set_tab_align(TabContainer::ALIGN_LEFT);
  56. tabs->set_tabs_visible(false);
  57. tabs->connect("tab_changed", callable_mp(this, &EditorDebuggerNode::_debugger_changed));
  58. add_child(tabs);
  59. Ref<StyleBoxEmpty> empty;
  60. empty.instance();
  61. tabs->add_theme_style_override("panel", empty);
  62. auto_switch_remote_scene_tree = EDITOR_DEF("debugger/auto_switch_to_remote_scene_tree", false);
  63. _add_debugger();
  64. // Remote scene tree
  65. remote_scene_tree = memnew(EditorDebuggerTree);
  66. remote_scene_tree->connect("object_selected", callable_mp(this, &EditorDebuggerNode::_remote_object_requested));
  67. remote_scene_tree->connect("save_node", callable_mp(this, &EditorDebuggerNode::_save_node_requested));
  68. EditorNode::get_singleton()->get_scene_tree_dock()->add_remote_tree_editor(remote_scene_tree);
  69. EditorNode::get_singleton()->get_scene_tree_dock()->connect("remote_tree_selected", callable_mp(this, &EditorDebuggerNode::request_remote_tree));
  70. remote_scene_tree_timeout = EDITOR_DEF("debugger/remote_scene_tree_refresh_interval", 1.0);
  71. inspect_edited_object_timeout = EDITOR_DEF("debugger/remote_inspect_refresh_interval", 0.2);
  72. EditorNode *editor = EditorNode::get_singleton();
  73. editor->get_undo_redo()->set_method_notify_callback(_method_changeds, this);
  74. editor->get_undo_redo()->set_property_notify_callback(_property_changeds, this);
  75. editor->get_pause_button()->connect("pressed", callable_mp(this, &EditorDebuggerNode::_paused));
  76. }
  77. ScriptEditorDebugger *EditorDebuggerNode::_add_debugger() {
  78. ScriptEditorDebugger *node = memnew(ScriptEditorDebugger(EditorNode::get_singleton()));
  79. int id = tabs->get_tab_count();
  80. node->connect("stop_requested", callable_mp(this, &EditorDebuggerNode::_debugger_wants_stop), varray(id));
  81. node->connect("stopped", callable_mp(this, &EditorDebuggerNode::_debugger_stopped), varray(id));
  82. node->connect("stack_frame_selected", callable_mp(this, &EditorDebuggerNode::_stack_frame_selected), varray(id));
  83. node->connect("error_selected", callable_mp(this, &EditorDebuggerNode::_error_selected), varray(id));
  84. node->connect("clear_execution", callable_mp(this, &EditorDebuggerNode::_clear_execution));
  85. node->connect("breaked", callable_mp(this, &EditorDebuggerNode::_breaked), varray(id));
  86. node->connect("remote_tree_updated", callable_mp(this, &EditorDebuggerNode::_remote_tree_updated), varray(id));
  87. node->connect("remote_object_updated", callable_mp(this, &EditorDebuggerNode::_remote_object_updated), varray(id));
  88. node->connect("remote_object_property_updated", callable_mp(this, &EditorDebuggerNode::_remote_object_property_updated), varray(id));
  89. node->connect("remote_object_requested", callable_mp(this, &EditorDebuggerNode::_remote_object_requested), varray(id));
  90. if (tabs->get_tab_count() > 0) {
  91. get_debugger(0)->clear_style();
  92. }
  93. tabs->add_child(node);
  94. node->set_name("Session " + itos(tabs->get_tab_count()));
  95. if (tabs->get_tab_count() > 1) {
  96. node->clear_style();
  97. tabs->set_tabs_visible(true);
  98. tabs->add_theme_style_override("panel", EditorNode::get_singleton()->get_gui_base()->get_theme_stylebox("DebuggerPanel", "EditorStyles"));
  99. }
  100. if (!debugger_plugins.empty()) {
  101. for (Set<Ref<Script>>::Element *i = debugger_plugins.front(); i; i = i->next()) {
  102. node->add_debugger_plugin(i->get());
  103. }
  104. }
  105. return node;
  106. }
  107. void EditorDebuggerNode::_stack_frame_selected(int p_debugger) {
  108. const ScriptEditorDebugger *dbg = get_debugger(p_debugger);
  109. ERR_FAIL_COND(!dbg);
  110. if (dbg != get_current_debugger()) {
  111. return;
  112. }
  113. _text_editor_stack_goto(dbg);
  114. }
  115. void EditorDebuggerNode::_error_selected(const String &p_file, int p_line, int p_debugger) {
  116. Ref<Script> s = ResourceLoader::load(p_file);
  117. emit_signal("goto_script_line", s, p_line - 1);
  118. }
  119. void EditorDebuggerNode::_text_editor_stack_goto(const ScriptEditorDebugger *p_debugger) {
  120. const String file = p_debugger->get_stack_script_file();
  121. if (file.empty()) {
  122. return;
  123. }
  124. stack_script = ResourceLoader::load(file);
  125. const int line = p_debugger->get_stack_script_line() - 1;
  126. emit_signal("goto_script_line", stack_script, line);
  127. emit_signal("set_execution", stack_script, line);
  128. stack_script.unref(); // Why?!?
  129. }
  130. void EditorDebuggerNode::_bind_methods() {
  131. // LiveDebug.
  132. ClassDB::bind_method("live_debug_create_node", &EditorDebuggerNode::live_debug_create_node);
  133. ClassDB::bind_method("live_debug_instance_node", &EditorDebuggerNode::live_debug_instance_node);
  134. ClassDB::bind_method("live_debug_remove_node", &EditorDebuggerNode::live_debug_remove_node);
  135. ClassDB::bind_method("live_debug_remove_and_keep_node", &EditorDebuggerNode::live_debug_remove_and_keep_node);
  136. ClassDB::bind_method("live_debug_restore_node", &EditorDebuggerNode::live_debug_restore_node);
  137. ClassDB::bind_method("live_debug_duplicate_node", &EditorDebuggerNode::live_debug_duplicate_node);
  138. ClassDB::bind_method("live_debug_reparent_node", &EditorDebuggerNode::live_debug_reparent_node);
  139. ADD_SIGNAL(MethodInfo("goto_script_line"));
  140. ADD_SIGNAL(MethodInfo("set_execution", PropertyInfo("script"), PropertyInfo(Variant::INT, "line")));
  141. ADD_SIGNAL(MethodInfo("clear_execution", PropertyInfo("script")));
  142. ADD_SIGNAL(MethodInfo("breaked", PropertyInfo(Variant::BOOL, "reallydid"), PropertyInfo(Variant::BOOL, "can_debug")));
  143. }
  144. EditorDebuggerRemoteObject *EditorDebuggerNode::get_inspected_remote_object() {
  145. return Object::cast_to<EditorDebuggerRemoteObject>(ObjectDB::get_instance(EditorNode::get_singleton()->get_editor_history()->get_current()));
  146. }
  147. ScriptEditorDebugger *EditorDebuggerNode::get_debugger(int p_id) const {
  148. return Object::cast_to<ScriptEditorDebugger>(tabs->get_tab_control(p_id));
  149. }
  150. ScriptEditorDebugger *EditorDebuggerNode::get_current_debugger() const {
  151. return Object::cast_to<ScriptEditorDebugger>(tabs->get_tab_control(tabs->get_current_tab()));
  152. }
  153. ScriptEditorDebugger *EditorDebuggerNode::get_default_debugger() const {
  154. return Object::cast_to<ScriptEditorDebugger>(tabs->get_tab_control(0));
  155. }
  156. Error EditorDebuggerNode::start(const String &p_protocol) {
  157. stop();
  158. if (EDITOR_GET("run/output/always_open_output_on_play")) {
  159. EditorNode::get_singleton()->make_bottom_panel_item_visible(EditorNode::get_log());
  160. } else {
  161. EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
  162. }
  163. server = Ref<EditorDebuggerServer>(EditorDebuggerServer::create(p_protocol));
  164. const Error err = server->start();
  165. if (err != OK) {
  166. return err;
  167. }
  168. set_process(true);
  169. EditorNode::get_log()->add_message("--- Debugging process started ---", EditorLog::MSG_TYPE_EDITOR);
  170. return OK;
  171. }
  172. void EditorDebuggerNode::stop() {
  173. if (server.is_valid()) {
  174. server->stop();
  175. EditorNode::get_log()->add_message("--- Debugging process stopped ---", EditorLog::MSG_TYPE_EDITOR);
  176. server.unref();
  177. }
  178. // Also close all debugging sessions.
  179. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  180. if (dbg->is_session_active()) {
  181. dbg->stop();
  182. }
  183. });
  184. _break_state_changed();
  185. if (hide_on_stop) {
  186. if (is_visible_in_tree()) {
  187. EditorNode::get_singleton()->hide_bottom_panel();
  188. }
  189. }
  190. breakpoints.clear();
  191. set_process(false);
  192. }
  193. void EditorDebuggerNode::_notification(int p_what) {
  194. switch (p_what) {
  195. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  196. if (tabs->get_tab_count() > 1) {
  197. add_theme_constant_override("margin_left", -EditorNode::get_singleton()->get_gui_base()->get_theme_stylebox("BottomPanelDebuggerOverride", "EditorStyles")->get_margin(SIDE_LEFT));
  198. add_theme_constant_override("margin_right", -EditorNode::get_singleton()->get_gui_base()->get_theme_stylebox("BottomPanelDebuggerOverride", "EditorStyles")->get_margin(SIDE_RIGHT));
  199. tabs->add_theme_style_override("panel", EditorNode::get_singleton()->get_gui_base()->get_theme_stylebox("DebuggerPanel", "EditorStyles"));
  200. }
  201. } break;
  202. case NOTIFICATION_READY: {
  203. _update_debug_options();
  204. } break;
  205. default:
  206. break;
  207. }
  208. if (p_what != NOTIFICATION_PROCESS || !server.is_valid()) {
  209. return;
  210. }
  211. if (!server.is_valid() || !server->is_active()) {
  212. stop();
  213. return;
  214. }
  215. server->poll();
  216. // Errors and warnings
  217. int error_count = 0;
  218. int warning_count = 0;
  219. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  220. error_count += dbg->get_error_count();
  221. warning_count += dbg->get_warning_count();
  222. });
  223. if (error_count != last_error_count || warning_count != last_warning_count) {
  224. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  225. dbg->update_tabs();
  226. });
  227. if (error_count == 0 && warning_count == 0) {
  228. debugger_button->set_text(TTR("Debugger"));
  229. debugger_button->set_icon(Ref<Texture2D>());
  230. } else {
  231. debugger_button->set_text(TTR("Debugger") + " (" + itos(error_count + warning_count) + ")");
  232. if (error_count >= 1 && warning_count >= 1) {
  233. debugger_button->set_icon(get_theme_icon("ErrorWarning", "EditorIcons"));
  234. } else if (error_count >= 1) {
  235. debugger_button->set_icon(get_theme_icon("Error", "EditorIcons"));
  236. } else {
  237. debugger_button->set_icon(get_theme_icon("Warning", "EditorIcons"));
  238. }
  239. }
  240. last_error_count = error_count;
  241. last_warning_count = warning_count;
  242. }
  243. // Remote scene tree update
  244. remote_scene_tree_timeout -= get_process_delta_time();
  245. if (remote_scene_tree_timeout < 0) {
  246. remote_scene_tree_timeout = EditorSettings::get_singleton()->get("debugger/remote_scene_tree_refresh_interval");
  247. if (remote_scene_tree->is_visible_in_tree()) {
  248. get_current_debugger()->request_remote_tree();
  249. }
  250. }
  251. // Remote inspector update
  252. inspect_edited_object_timeout -= get_process_delta_time();
  253. if (inspect_edited_object_timeout < 0) {
  254. inspect_edited_object_timeout = EditorSettings::get_singleton()->get("debugger/remote_inspect_refresh_interval");
  255. if (EditorDebuggerRemoteObject *obj = get_inspected_remote_object()) {
  256. get_current_debugger()->request_remote_object(obj->remote_object_id);
  257. }
  258. }
  259. // Take connections.
  260. if (server->is_connection_available()) {
  261. ScriptEditorDebugger *debugger = nullptr;
  262. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  263. if (debugger || dbg->is_session_active()) {
  264. return;
  265. }
  266. debugger = dbg;
  267. });
  268. if (debugger == nullptr) {
  269. if (tabs->get_tab_count() <= 4) { // Max 4 debugging sessions active.
  270. debugger = _add_debugger();
  271. } else {
  272. // We already have too many sessions, disconnecting new clients to prevent them from hanging.
  273. server->take_connection()->close();
  274. return; // Can't add, stop here.
  275. }
  276. }
  277. EditorNode::get_singleton()->get_pause_button()->set_disabled(false);
  278. // Switch to remote tree view if so desired.
  279. auto_switch_remote_scene_tree = (bool)EditorSettings::get_singleton()->get("debugger/auto_switch_to_remote_scene_tree");
  280. if (auto_switch_remote_scene_tree) {
  281. EditorNode::get_singleton()->get_scene_tree_dock()->show_remote_tree();
  282. }
  283. // Good to go.
  284. EditorNode::get_singleton()->get_scene_tree_dock()->show_tab_buttons();
  285. debugger->set_editor_remote_tree(remote_scene_tree);
  286. debugger->start(server->take_connection());
  287. // Send breakpoints.
  288. for (Map<Breakpoint, bool>::Element *E = breakpoints.front(); E; E = E->next()) {
  289. const Breakpoint &bp = E->key();
  290. debugger->set_breakpoint(bp.source, bp.line, E->get());
  291. } // Will arrive too late, how does the regular run work?
  292. debugger->update_live_edit_root();
  293. }
  294. }
  295. void EditorDebuggerNode::_debugger_stopped(int p_id) {
  296. ScriptEditorDebugger *dbg = get_debugger(p_id);
  297. ERR_FAIL_COND(!dbg);
  298. bool found = false;
  299. _for_all(tabs, [&](ScriptEditorDebugger *p_debugger) {
  300. if (p_debugger->is_session_active()) {
  301. found = true;
  302. }
  303. });
  304. if (!found) {
  305. EditorNode::get_singleton()->get_pause_button()->set_pressed(false);
  306. EditorNode::get_singleton()->get_pause_button()->set_disabled(true);
  307. EditorNode::get_singleton()->get_scene_tree_dock()->hide_remote_tree();
  308. EditorNode::get_singleton()->get_scene_tree_dock()->hide_tab_buttons();
  309. EditorNode::get_singleton()->notify_all_debug_sessions_exited();
  310. }
  311. }
  312. void EditorDebuggerNode::_debugger_wants_stop(int p_id) {
  313. // Ask editor to kill PID.
  314. int pid = get_debugger(p_id)->get_remote_pid();
  315. if (pid) {
  316. EditorNode::get_singleton()->call_deferred("stop_child_process", pid);
  317. }
  318. }
  319. void EditorDebuggerNode::_debugger_changed(int p_tab) {
  320. if (get_inspected_remote_object()) {
  321. // Clear inspected object, you can only inspect objects in selected debugger.
  322. // Hopefully, in the future, we will have one inspector per debugger.
  323. EditorNode::get_singleton()->push_item(nullptr);
  324. }
  325. if (remote_scene_tree->is_visible_in_tree()) {
  326. get_current_debugger()->request_remote_tree();
  327. }
  328. if (get_current_debugger()->is_breaked()) {
  329. _text_editor_stack_goto(get_current_debugger());
  330. }
  331. }
  332. void EditorDebuggerNode::set_script_debug_button(MenuButton *p_button) {
  333. script_menu = p_button;
  334. script_menu->set_text(TTR("Debug"));
  335. script_menu->set_switch_on_hover(true);
  336. PopupMenu *p = script_menu->get_popup();
  337. p->add_shortcut(ED_GET_SHORTCUT("debugger/step_into"), DEBUG_STEP);
  338. p->add_shortcut(ED_GET_SHORTCUT("debugger/step_over"), DEBUG_NEXT);
  339. p->add_separator();
  340. p->add_shortcut(ED_GET_SHORTCUT("debugger/break"), DEBUG_BREAK);
  341. p->add_shortcut(ED_GET_SHORTCUT("debugger/continue"), DEBUG_CONTINUE);
  342. p->add_separator();
  343. p->add_check_shortcut(ED_GET_SHORTCUT("debugger/keep_debugger_open"), DEBUG_KEEP_DEBUGGER_OPEN);
  344. p->add_check_shortcut(ED_GET_SHORTCUT("debugger/debug_with_external_editor"), DEBUG_WITH_EXTERNAL_EDITOR);
  345. p->connect("id_pressed", callable_mp(this, &EditorDebuggerNode::_menu_option));
  346. _break_state_changed();
  347. script_menu->show();
  348. }
  349. void EditorDebuggerNode::_break_state_changed() {
  350. const bool breaked = get_current_debugger()->is_breaked();
  351. const bool can_debug = get_current_debugger()->is_debuggable();
  352. if (breaked) { // Show debugger.
  353. EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
  354. }
  355. // Update script menu.
  356. if (!script_menu) {
  357. return;
  358. }
  359. PopupMenu *p = script_menu->get_popup();
  360. p->set_item_disabled(p->get_item_index(DEBUG_NEXT), !(breaked && can_debug));
  361. p->set_item_disabled(p->get_item_index(DEBUG_STEP), !(breaked && can_debug));
  362. p->set_item_disabled(p->get_item_index(DEBUG_BREAK), breaked);
  363. p->set_item_disabled(p->get_item_index(DEBUG_CONTINUE), !breaked);
  364. }
  365. void EditorDebuggerNode::_menu_option(int p_id) {
  366. switch (p_id) {
  367. case DEBUG_NEXT: {
  368. debug_next();
  369. } break;
  370. case DEBUG_STEP: {
  371. debug_step();
  372. } break;
  373. case DEBUG_BREAK: {
  374. debug_break();
  375. } break;
  376. case DEBUG_CONTINUE: {
  377. debug_continue();
  378. } break;
  379. case DEBUG_KEEP_DEBUGGER_OPEN: {
  380. bool ischecked = script_menu->get_popup()->is_item_checked(script_menu->get_popup()->get_item_index(DEBUG_KEEP_DEBUGGER_OPEN));
  381. hide_on_stop = ischecked;
  382. script_menu->get_popup()->set_item_checked(script_menu->get_popup()->get_item_index(DEBUG_KEEP_DEBUGGER_OPEN), !ischecked);
  383. EditorSettings::get_singleton()->set_project_metadata("debug_options", "keep_debugger_open", !ischecked);
  384. } break;
  385. case DEBUG_WITH_EXTERNAL_EDITOR: {
  386. bool ischecked = script_menu->get_popup()->is_item_checked(script_menu->get_popup()->get_item_index(DEBUG_WITH_EXTERNAL_EDITOR));
  387. debug_with_external_editor = !ischecked;
  388. script_menu->get_popup()->set_item_checked(script_menu->get_popup()->get_item_index(DEBUG_WITH_EXTERNAL_EDITOR), !ischecked);
  389. EditorSettings::get_singleton()->set_project_metadata("debug_options", "debug_with_external_editor", !ischecked);
  390. } break;
  391. }
  392. }
  393. void EditorDebuggerNode::_update_debug_options() {
  394. bool keep_debugger_open = EditorSettings::get_singleton()->get_project_metadata("debug_options", "keep_debugger_open", false);
  395. bool debug_with_external_editor = EditorSettings::get_singleton()->get_project_metadata("debug_options", "debug_with_external_editor", false);
  396. if (keep_debugger_open) {
  397. _menu_option(DEBUG_KEEP_DEBUGGER_OPEN);
  398. }
  399. if (debug_with_external_editor) {
  400. _menu_option(DEBUG_WITH_EXTERNAL_EDITOR);
  401. }
  402. }
  403. void EditorDebuggerNode::_paused() {
  404. const bool paused = EditorNode::get_singleton()->get_pause_button()->is_pressed();
  405. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  406. if (paused && !dbg->is_breaked()) {
  407. dbg->debug_break();
  408. } else if (!paused && dbg->is_breaked()) {
  409. dbg->debug_continue();
  410. }
  411. });
  412. }
  413. void EditorDebuggerNode::_breaked(bool p_breaked, bool p_can_debug, int p_debugger) {
  414. if (get_current_debugger() != get_debugger(p_debugger)) {
  415. if (!p_breaked) {
  416. return;
  417. }
  418. tabs->set_current_tab(p_debugger);
  419. }
  420. _break_state_changed();
  421. EditorNode::get_singleton()->get_pause_button()->set_pressed(p_breaked);
  422. emit_signal("breaked", p_breaked, p_can_debug);
  423. }
  424. bool EditorDebuggerNode::is_skip_breakpoints() const {
  425. return get_default_debugger()->is_skip_breakpoints();
  426. }
  427. void EditorDebuggerNode::set_breakpoint(const String &p_path, int p_line, bool p_enabled) {
  428. breakpoints[Breakpoint(p_path, p_line)] = p_enabled;
  429. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  430. dbg->set_breakpoint(p_path, p_line, p_enabled);
  431. });
  432. }
  433. void EditorDebuggerNode::reload_scripts() {
  434. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  435. dbg->reload_scripts();
  436. });
  437. }
  438. void EditorDebuggerNode::debug_next() {
  439. get_default_debugger()->debug_next();
  440. }
  441. void EditorDebuggerNode::debug_step() {
  442. get_default_debugger()->debug_step();
  443. }
  444. void EditorDebuggerNode::debug_break() {
  445. get_default_debugger()->debug_break();
  446. }
  447. void EditorDebuggerNode::debug_continue() {
  448. get_default_debugger()->debug_continue();
  449. }
  450. String EditorDebuggerNode::get_var_value(const String &p_var) const {
  451. return get_default_debugger()->get_var_value(p_var);
  452. }
  453. // LiveEdit/Inspector
  454. void EditorDebuggerNode::request_remote_tree() {
  455. get_current_debugger()->request_remote_tree();
  456. }
  457. void EditorDebuggerNode::_remote_tree_updated(int p_debugger) {
  458. if (p_debugger != tabs->get_current_tab()) {
  459. return;
  460. }
  461. remote_scene_tree->clear();
  462. remote_scene_tree->update_scene_tree(get_current_debugger()->get_remote_tree(), p_debugger);
  463. }
  464. void EditorDebuggerNode::_remote_object_updated(ObjectID p_id, int p_debugger) {
  465. if (p_debugger != tabs->get_current_tab()) {
  466. return;
  467. }
  468. if (EditorDebuggerRemoteObject *obj = get_inspected_remote_object()) {
  469. if (obj->remote_object_id == p_id) {
  470. return; // Already being edited
  471. }
  472. }
  473. EditorNode::get_singleton()->push_item(get_current_debugger()->get_remote_object(p_id));
  474. }
  475. void EditorDebuggerNode::_remote_object_property_updated(ObjectID p_id, const String &p_property, int p_debugger) {
  476. if (p_debugger != tabs->get_current_tab()) {
  477. return;
  478. }
  479. if (EditorDebuggerRemoteObject *obj = get_inspected_remote_object()) {
  480. if (obj->remote_object_id != p_id) {
  481. return;
  482. }
  483. EditorNode::get_singleton()->get_inspector()->update_property(p_property);
  484. }
  485. }
  486. void EditorDebuggerNode::_remote_object_requested(ObjectID p_id, int p_debugger) {
  487. if (p_debugger != tabs->get_current_tab()) {
  488. return;
  489. }
  490. inspect_edited_object_timeout = 0.7; // Temporarily disable timeout to avoid multiple requests.
  491. get_current_debugger()->request_remote_object(p_id);
  492. }
  493. void EditorDebuggerNode::_save_node_requested(ObjectID p_id, const String &p_file, int p_debugger) {
  494. if (p_debugger != tabs->get_current_tab()) {
  495. return;
  496. }
  497. get_current_debugger()->save_node(p_id, p_file);
  498. }
  499. // Remote inspector/edit.
  500. void EditorDebuggerNode::_method_changeds(void *p_ud, Object *p_base, const StringName &p_name, VARIANT_ARG_DECLARE) {
  501. if (!singleton) {
  502. return;
  503. }
  504. _for_all(singleton->tabs, [&](ScriptEditorDebugger *dbg) {
  505. dbg->_method_changed(p_base, p_name, VARIANT_ARG_PASS);
  506. });
  507. }
  508. void EditorDebuggerNode::_property_changeds(void *p_ud, Object *p_base, const StringName &p_property, const Variant &p_value) {
  509. if (!singleton) {
  510. return;
  511. }
  512. _for_all(singleton->tabs, [&](ScriptEditorDebugger *dbg) {
  513. dbg->_property_changed(p_base, p_property, p_value);
  514. });
  515. }
  516. // LiveDebug
  517. void EditorDebuggerNode::set_live_debugging(bool p_enabled) {
  518. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  519. dbg->set_live_debugging(p_enabled);
  520. });
  521. }
  522. void EditorDebuggerNode::update_live_edit_root() {
  523. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  524. dbg->update_live_edit_root();
  525. });
  526. }
  527. void EditorDebuggerNode::live_debug_create_node(const NodePath &p_parent, const String &p_type, const String &p_name) {
  528. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  529. dbg->live_debug_create_node(p_parent, p_type, p_name);
  530. });
  531. }
  532. void EditorDebuggerNode::live_debug_instance_node(const NodePath &p_parent, const String &p_path, const String &p_name) {
  533. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  534. dbg->live_debug_instance_node(p_parent, p_path, p_name);
  535. });
  536. }
  537. void EditorDebuggerNode::live_debug_remove_node(const NodePath &p_at) {
  538. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  539. dbg->live_debug_remove_node(p_at);
  540. });
  541. }
  542. void EditorDebuggerNode::live_debug_remove_and_keep_node(const NodePath &p_at, ObjectID p_keep_id) {
  543. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  544. dbg->live_debug_remove_and_keep_node(p_at, p_keep_id);
  545. });
  546. }
  547. void EditorDebuggerNode::live_debug_restore_node(ObjectID p_id, const NodePath &p_at, int p_at_pos) {
  548. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  549. dbg->live_debug_restore_node(p_id, p_at, p_at_pos);
  550. });
  551. }
  552. void EditorDebuggerNode::live_debug_duplicate_node(const NodePath &p_at, const String &p_new_name) {
  553. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  554. dbg->live_debug_duplicate_node(p_at, p_new_name);
  555. });
  556. }
  557. void EditorDebuggerNode::live_debug_reparent_node(const NodePath &p_at, const NodePath &p_new_place, const String &p_new_name, int p_at_pos) {
  558. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  559. dbg->live_debug_reparent_node(p_at, p_new_place, p_new_name, p_at_pos);
  560. });
  561. }
  562. void EditorDebuggerNode::add_debugger_plugin(const Ref<Script> &p_script) {
  563. ERR_FAIL_COND_MSG(debugger_plugins.has(p_script), "Debugger plugin already exists.");
  564. ERR_FAIL_COND_MSG(p_script.is_null(), "Debugger plugin script is null");
  565. ERR_FAIL_COND_MSG(String(p_script->get_instance_base_type()) == "", "Debugger plugin script has error.");
  566. ERR_FAIL_COND_MSG(String(p_script->get_instance_base_type()) != "EditorDebuggerPlugin", "Base type of debugger plugin is not 'EditorDebuggerPlugin'.");
  567. ERR_FAIL_COND_MSG(!p_script->is_tool(), "Debugger plugin script is not in tool mode.");
  568. debugger_plugins.insert(p_script);
  569. for (int i = 0; get_debugger(i); i++) {
  570. get_debugger(i)->add_debugger_plugin(p_script);
  571. }
  572. }
  573. void EditorDebuggerNode::remove_debugger_plugin(const Ref<Script> &p_script) {
  574. ERR_FAIL_COND_MSG(!debugger_plugins.has(p_script), "Debugger plugin doesn't exists.");
  575. debugger_plugins.erase(p_script);
  576. for (int i = 0; get_debugger(i); i++) {
  577. get_debugger(i)->remove_debugger_plugin(p_script);
  578. }
  579. }