editor_debugger_node.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. /**************************************************************************/
  2. /* editor_debugger_node.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 "editor_debugger_node.h"
  31. #include "core/object/undo_redo.h"
  32. #include "editor/debugger/editor_debugger_tree.h"
  33. #include "editor/debugger/script_editor_debugger.h"
  34. #include "editor/editor_log.h"
  35. #include "editor/editor_node.h"
  36. #include "editor/editor_settings.h"
  37. #include "editor/editor_string_names.h"
  38. #include "editor/editor_undo_redo_manager.h"
  39. #include "editor/gui/editor_bottom_panel.h"
  40. #include "editor/gui/editor_run_bar.h"
  41. #include "editor/inspector_dock.h"
  42. #include "editor/plugins/editor_debugger_plugin.h"
  43. #include "editor/plugins/script_editor_plugin.h"
  44. #include "editor/scene_tree_dock.h"
  45. #include "editor/themes/editor_theme_manager.h"
  46. #include "scene/gui/menu_button.h"
  47. #include "scene/gui/tab_container.h"
  48. #include "scene/resources/packed_scene.h"
  49. template <typename Func>
  50. void _for_all(TabContainer *p_node, const Func &p_func) {
  51. for (int i = 0; i < p_node->get_tab_count(); i++) {
  52. ScriptEditorDebugger *dbg = Object::cast_to<ScriptEditorDebugger>(p_node->get_tab_control(i));
  53. ERR_FAIL_NULL(dbg);
  54. p_func(dbg);
  55. }
  56. }
  57. EditorDebuggerNode *EditorDebuggerNode::singleton = nullptr;
  58. EditorDebuggerNode::EditorDebuggerNode() {
  59. if (!singleton) {
  60. singleton = this;
  61. }
  62. add_theme_constant_override("margin_left", -EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("BottomPanelDebuggerOverride"), EditorStringName(EditorStyles))->get_margin(SIDE_LEFT));
  63. add_theme_constant_override("margin_right", -EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("BottomPanelDebuggerOverride"), EditorStringName(EditorStyles))->get_margin(SIDE_RIGHT));
  64. tabs = memnew(TabContainer);
  65. tabs->set_tabs_visible(false);
  66. tabs->connect("tab_changed", callable_mp(this, &EditorDebuggerNode::_debugger_changed));
  67. add_child(tabs);
  68. Ref<StyleBoxEmpty> empty;
  69. empty.instantiate();
  70. tabs->add_theme_style_override("panel", empty);
  71. auto_switch_remote_scene_tree = EDITOR_GET("debugger/auto_switch_to_remote_scene_tree");
  72. _add_debugger();
  73. // Remote scene tree
  74. remote_scene_tree = memnew(EditorDebuggerTree);
  75. remote_scene_tree->connect("object_selected", callable_mp(this, &EditorDebuggerNode::_remote_object_requested));
  76. remote_scene_tree->connect("save_node", callable_mp(this, &EditorDebuggerNode::_save_node_requested));
  77. remote_scene_tree->connect("button_clicked", callable_mp(this, &EditorDebuggerNode::_remote_tree_button_pressed));
  78. SceneTreeDock::get_singleton()->add_remote_tree_editor(remote_scene_tree);
  79. SceneTreeDock::get_singleton()->connect("remote_tree_selected", callable_mp(this, &EditorDebuggerNode::request_remote_tree));
  80. remote_scene_tree_timeout = EDITOR_GET("debugger/remote_scene_tree_refresh_interval");
  81. inspect_edited_object_timeout = EDITOR_GET("debugger/remote_inspect_refresh_interval");
  82. EditorRunBar::get_singleton()->get_pause_button()->connect(SceneStringName(pressed), callable_mp(this, &EditorDebuggerNode::_paused));
  83. }
  84. ScriptEditorDebugger *EditorDebuggerNode::_add_debugger() {
  85. ScriptEditorDebugger *node = memnew(ScriptEditorDebugger);
  86. int id = tabs->get_tab_count();
  87. node->connect("stop_requested", callable_mp(this, &EditorDebuggerNode::_debugger_wants_stop).bind(id));
  88. node->connect("stopped", callable_mp(this, &EditorDebuggerNode::_debugger_stopped).bind(id));
  89. node->connect("stack_frame_selected", callable_mp(this, &EditorDebuggerNode::_stack_frame_selected).bind(id));
  90. node->connect("error_selected", callable_mp(this, &EditorDebuggerNode::_error_selected).bind(id));
  91. node->connect("breakpoint_selected", callable_mp(this, &EditorDebuggerNode::_error_selected).bind(id));
  92. node->connect("clear_execution", callable_mp(this, &EditorDebuggerNode::_clear_execution));
  93. node->connect("breaked", callable_mp(this, &EditorDebuggerNode::_breaked).bind(id));
  94. node->connect("remote_tree_updated", callable_mp(this, &EditorDebuggerNode::_remote_tree_updated).bind(id));
  95. node->connect("remote_object_updated", callable_mp(this, &EditorDebuggerNode::_remote_object_updated).bind(id));
  96. node->connect("remote_object_property_updated", callable_mp(this, &EditorDebuggerNode::_remote_object_property_updated).bind(id));
  97. node->connect("remote_object_requested", callable_mp(this, &EditorDebuggerNode::_remote_object_requested).bind(id));
  98. node->connect("set_breakpoint", callable_mp(this, &EditorDebuggerNode::_breakpoint_set_in_tree).bind(id));
  99. node->connect("clear_breakpoints", callable_mp(this, &EditorDebuggerNode::_breakpoints_cleared_in_tree).bind(id));
  100. node->connect("errors_cleared", callable_mp(this, &EditorDebuggerNode::_update_errors));
  101. if (tabs->get_tab_count() > 0) {
  102. get_debugger(0)->clear_style();
  103. }
  104. tabs->add_child(node);
  105. node->set_name("Session " + itos(tabs->get_tab_count()));
  106. if (tabs->get_tab_count() > 1) {
  107. node->clear_style();
  108. tabs->set_tabs_visible(true);
  109. tabs->add_theme_style_override("panel", EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("DebuggerPanel"), EditorStringName(EditorStyles)));
  110. }
  111. if (!debugger_plugins.is_empty()) {
  112. for (Ref<EditorDebuggerPlugin> plugin : debugger_plugins) {
  113. plugin->create_session(node);
  114. }
  115. }
  116. return node;
  117. }
  118. void EditorDebuggerNode::_stack_frame_selected(int p_debugger) {
  119. const ScriptEditorDebugger *dbg = get_debugger(p_debugger);
  120. ERR_FAIL_NULL(dbg);
  121. if (dbg != get_current_debugger()) {
  122. return;
  123. }
  124. _text_editor_stack_goto(dbg);
  125. }
  126. void EditorDebuggerNode::_error_selected(const String &p_file, int p_line, int p_debugger) {
  127. Ref<Script> s = ResourceLoader::load(p_file);
  128. emit_signal(SNAME("goto_script_line"), s, p_line - 1);
  129. }
  130. void EditorDebuggerNode::_text_editor_stack_goto(const ScriptEditorDebugger *p_debugger) {
  131. String file = p_debugger->get_stack_script_file();
  132. if (file.is_empty()) {
  133. return;
  134. }
  135. if (file.is_resource_file()) {
  136. stack_script = ResourceLoader::load(file);
  137. } else {
  138. // If the script is built-in, it can be opened only if the scene is loaded in memory.
  139. int i = file.find("::");
  140. int j = file.rfind("(", i);
  141. if (j > -1) { // If the script is named, the string is "name (file)", so we need to extract the path.
  142. file = file.substr(j + 1, file.find(")", i) - j - 1);
  143. }
  144. Ref<PackedScene> ps = ResourceLoader::load(file.get_slice("::", 0));
  145. stack_script = ResourceLoader::load(file);
  146. }
  147. const int line = p_debugger->get_stack_script_line() - 1;
  148. emit_signal(SNAME("goto_script_line"), stack_script, line);
  149. emit_signal(SNAME("set_execution"), stack_script, line);
  150. stack_script.unref(); // Why?!?
  151. }
  152. void EditorDebuggerNode::_text_editor_stack_clear(const ScriptEditorDebugger *p_debugger) {
  153. String file = p_debugger->get_stack_script_file();
  154. if (file.is_empty()) {
  155. return;
  156. }
  157. if (file.is_resource_file()) {
  158. stack_script = ResourceLoader::load(file);
  159. } else {
  160. // If the script is built-in, it can be opened only if the scene is loaded in memory.
  161. int i = file.find("::");
  162. int j = file.rfind("(", i);
  163. if (j > -1) { // If the script is named, the string is "name (file)", so we need to extract the path.
  164. file = file.substr(j + 1, file.find(")", i) - j - 1);
  165. }
  166. Ref<PackedScene> ps = ResourceLoader::load(file.get_slice("::", 0));
  167. stack_script = ResourceLoader::load(file);
  168. }
  169. emit_signal(SNAME("clear_execution"), stack_script);
  170. stack_script.unref(); // Why?!?
  171. }
  172. void EditorDebuggerNode::_bind_methods() {
  173. // LiveDebug.
  174. ClassDB::bind_method("live_debug_create_node", &EditorDebuggerNode::live_debug_create_node);
  175. ClassDB::bind_method("live_debug_instantiate_node", &EditorDebuggerNode::live_debug_instantiate_node);
  176. ClassDB::bind_method("live_debug_remove_node", &EditorDebuggerNode::live_debug_remove_node);
  177. ClassDB::bind_method("live_debug_remove_and_keep_node", &EditorDebuggerNode::live_debug_remove_and_keep_node);
  178. ClassDB::bind_method("live_debug_restore_node", &EditorDebuggerNode::live_debug_restore_node);
  179. ClassDB::bind_method("live_debug_duplicate_node", &EditorDebuggerNode::live_debug_duplicate_node);
  180. ClassDB::bind_method("live_debug_reparent_node", &EditorDebuggerNode::live_debug_reparent_node);
  181. ADD_SIGNAL(MethodInfo("goto_script_line"));
  182. ADD_SIGNAL(MethodInfo("set_execution", PropertyInfo("script"), PropertyInfo(Variant::INT, "line")));
  183. ADD_SIGNAL(MethodInfo("clear_execution", PropertyInfo("script")));
  184. ADD_SIGNAL(MethodInfo("breaked", PropertyInfo(Variant::BOOL, "reallydid"), PropertyInfo(Variant::BOOL, "can_debug")));
  185. ADD_SIGNAL(MethodInfo("breakpoint_toggled", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::INT, "line"), PropertyInfo(Variant::BOOL, "enabled")));
  186. ADD_SIGNAL(MethodInfo("breakpoint_set_in_tree", PropertyInfo("script"), PropertyInfo(Variant::INT, "line"), PropertyInfo(Variant::BOOL, "enabled"), PropertyInfo(Variant::INT, "debugger")));
  187. ADD_SIGNAL(MethodInfo("breakpoints_cleared_in_tree", PropertyInfo(Variant::INT, "debugger")));
  188. }
  189. void EditorDebuggerNode::register_undo_redo(UndoRedo *p_undo_redo) {
  190. p_undo_redo->set_method_notify_callback(_method_changeds, this);
  191. p_undo_redo->set_property_notify_callback(_property_changeds, this);
  192. }
  193. EditorDebuggerRemoteObject *EditorDebuggerNode::get_inspected_remote_object() {
  194. return Object::cast_to<EditorDebuggerRemoteObject>(ObjectDB::get_instance(EditorNode::get_singleton()->get_editor_selection_history()->get_current()));
  195. }
  196. ScriptEditorDebugger *EditorDebuggerNode::get_debugger(int p_id) const {
  197. return Object::cast_to<ScriptEditorDebugger>(tabs->get_tab_control(p_id));
  198. }
  199. ScriptEditorDebugger *EditorDebuggerNode::get_previous_debugger() const {
  200. return Object::cast_to<ScriptEditorDebugger>(tabs->get_tab_control(tabs->get_previous_tab()));
  201. }
  202. ScriptEditorDebugger *EditorDebuggerNode::get_current_debugger() const {
  203. return Object::cast_to<ScriptEditorDebugger>(tabs->get_tab_control(tabs->get_current_tab()));
  204. }
  205. ScriptEditorDebugger *EditorDebuggerNode::get_default_debugger() const {
  206. return Object::cast_to<ScriptEditorDebugger>(tabs->get_tab_control(0));
  207. }
  208. String EditorDebuggerNode::get_server_uri() const {
  209. ERR_FAIL_COND_V(server.is_null(), "");
  210. return server->get_uri();
  211. }
  212. void EditorDebuggerNode::set_keep_open(bool p_keep_open) {
  213. keep_open = p_keep_open;
  214. if (keep_open) {
  215. if (server.is_null() || !server->is_active()) {
  216. start();
  217. }
  218. } else {
  219. bool found = false;
  220. _for_all(tabs, [&](ScriptEditorDebugger *p_debugger) {
  221. if (p_debugger->is_session_active()) {
  222. found = true;
  223. }
  224. });
  225. if (!found) {
  226. stop();
  227. }
  228. }
  229. }
  230. Error EditorDebuggerNode::start(const String &p_uri) {
  231. ERR_FAIL_COND_V(!p_uri.contains("://"), ERR_INVALID_PARAMETER);
  232. if (keep_open && current_uri == p_uri && server.is_valid()) {
  233. return OK;
  234. }
  235. stop(true);
  236. current_uri = p_uri;
  237. server = Ref<EditorDebuggerServer>(EditorDebuggerServer::create(p_uri.substr(0, p_uri.find("://") + 3)));
  238. const Error err = server->start(p_uri);
  239. if (err != OK) {
  240. return err;
  241. }
  242. set_process(true);
  243. EditorNode::get_log()->add_message("--- Debugging process started ---", EditorLog::MSG_TYPE_EDITOR);
  244. return OK;
  245. }
  246. void EditorDebuggerNode::stop(bool p_force) {
  247. if (keep_open && !p_force) {
  248. return;
  249. }
  250. current_uri.clear();
  251. if (server.is_valid()) {
  252. server->stop();
  253. EditorNode::get_log()->add_message("--- Debugging process stopped ---", EditorLog::MSG_TYPE_EDITOR);
  254. if (EditorRunBar::get_singleton()->is_movie_maker_enabled()) {
  255. // Request attention in case the user was doing something else when movie recording is finished.
  256. DisplayServer::get_singleton()->window_request_attention();
  257. }
  258. server.unref();
  259. }
  260. // Also close all debugging sessions.
  261. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  262. if (dbg->is_session_active()) {
  263. dbg->_stop_and_notify();
  264. }
  265. });
  266. _break_state_changed();
  267. breakpoints.clear();
  268. EditorUndoRedoManager::get_singleton()->clear_history(false, EditorUndoRedoManager::REMOTE_HISTORY);
  269. set_process(false);
  270. }
  271. void EditorDebuggerNode::_notification(int p_what) {
  272. switch (p_what) {
  273. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  274. if (!EditorThemeManager::is_generated_theme_outdated()) {
  275. return;
  276. }
  277. if (tabs->get_tab_count() > 1) {
  278. add_theme_constant_override("margin_left", -EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("BottomPanelDebuggerOverride"), EditorStringName(EditorStyles))->get_margin(SIDE_LEFT));
  279. add_theme_constant_override("margin_right", -EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("BottomPanelDebuggerOverride"), EditorStringName(EditorStyles))->get_margin(SIDE_RIGHT));
  280. tabs->add_theme_style_override("panel", EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("DebuggerPanel"), EditorStringName(EditorStyles)));
  281. }
  282. remote_scene_tree->update_icon_max_width();
  283. } break;
  284. case NOTIFICATION_READY: {
  285. _update_debug_options();
  286. initializing = false;
  287. } break;
  288. case NOTIFICATION_PROCESS: {
  289. if (!server.is_valid()) {
  290. return;
  291. }
  292. if (!server->is_active()) {
  293. stop();
  294. return;
  295. }
  296. server->poll();
  297. _update_errors();
  298. // Remote scene tree update
  299. remote_scene_tree_timeout -= get_process_delta_time();
  300. if (remote_scene_tree_timeout < 0) {
  301. remote_scene_tree_timeout = EDITOR_GET("debugger/remote_scene_tree_refresh_interval");
  302. if (remote_scene_tree->is_visible_in_tree()) {
  303. get_current_debugger()->request_remote_tree();
  304. }
  305. }
  306. // Remote inspector update
  307. inspect_edited_object_timeout -= get_process_delta_time();
  308. if (inspect_edited_object_timeout < 0) {
  309. inspect_edited_object_timeout = EDITOR_GET("debugger/remote_inspect_refresh_interval");
  310. if (EditorDebuggerRemoteObject *obj = get_inspected_remote_object()) {
  311. get_current_debugger()->request_remote_object(obj->remote_object_id);
  312. }
  313. }
  314. // Take connections.
  315. if (server->is_connection_available()) {
  316. ScriptEditorDebugger *debugger = nullptr;
  317. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  318. if (debugger || dbg->is_session_active()) {
  319. return;
  320. }
  321. debugger = dbg;
  322. });
  323. if (debugger == nullptr) {
  324. if (tabs->get_tab_count() <= 4) { // Max 4 debugging sessions active.
  325. debugger = _add_debugger();
  326. } else {
  327. // We already have too many sessions, disconnecting new clients to prevent them from hanging.
  328. server->take_connection()->close();
  329. return; // Can't add, stop here.
  330. }
  331. }
  332. EditorRunBar::get_singleton()->get_pause_button()->set_disabled(false);
  333. // Switch to remote tree view if so desired.
  334. auto_switch_remote_scene_tree = (bool)EDITOR_GET("debugger/auto_switch_to_remote_scene_tree");
  335. if (auto_switch_remote_scene_tree) {
  336. SceneTreeDock::get_singleton()->show_remote_tree();
  337. }
  338. // Good to go.
  339. SceneTreeDock::get_singleton()->show_tab_buttons();
  340. debugger->set_editor_remote_tree(remote_scene_tree);
  341. debugger->start(server->take_connection());
  342. // Send breakpoints.
  343. for (const KeyValue<Breakpoint, bool> &E : breakpoints) {
  344. const Breakpoint &bp = E.key;
  345. debugger->set_breakpoint(bp.source, bp.line, E.value);
  346. } // Will arrive too late, how does the regular run work?
  347. debugger->update_live_edit_root();
  348. }
  349. } break;
  350. }
  351. }
  352. void EditorDebuggerNode::_update_errors() {
  353. int error_count = 0;
  354. int warning_count = 0;
  355. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  356. error_count += dbg->get_error_count();
  357. warning_count += dbg->get_warning_count();
  358. });
  359. if (error_count != last_error_count || warning_count != last_warning_count) {
  360. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  361. dbg->update_tabs();
  362. });
  363. if (error_count == 0 && warning_count == 0) {
  364. debugger_button->set_text(TTR("Debugger"));
  365. debugger_button->remove_theme_color_override("font_color");
  366. debugger_button->set_icon(Ref<Texture2D>());
  367. } else {
  368. debugger_button->set_text(TTR("Debugger") + " (" + itos(error_count + warning_count) + ")");
  369. if (error_count >= 1 && warning_count >= 1) {
  370. debugger_button->set_icon(get_editor_theme_icon(SNAME("ErrorWarning")));
  371. // Use error color to represent the highest level of severity reported.
  372. debugger_button->add_theme_color_override("font_color", get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  373. } else if (error_count >= 1) {
  374. debugger_button->set_icon(get_editor_theme_icon(SNAME("Error")));
  375. debugger_button->add_theme_color_override("font_color", get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  376. } else {
  377. debugger_button->set_icon(get_editor_theme_icon(SNAME("Warning")));
  378. debugger_button->add_theme_color_override("font_color", get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
  379. }
  380. }
  381. last_error_count = error_count;
  382. last_warning_count = warning_count;
  383. }
  384. }
  385. void EditorDebuggerNode::_debugger_stopped(int p_id) {
  386. ScriptEditorDebugger *dbg = get_debugger(p_id);
  387. ERR_FAIL_NULL(dbg);
  388. bool found = false;
  389. _for_all(tabs, [&](ScriptEditorDebugger *p_debugger) {
  390. if (p_debugger->is_session_active()) {
  391. found = true;
  392. }
  393. });
  394. if (!found) {
  395. EditorRunBar::get_singleton()->get_pause_button()->set_pressed(false);
  396. EditorRunBar::get_singleton()->get_pause_button()->set_disabled(true);
  397. SceneTreeDock::get_singleton()->hide_remote_tree();
  398. SceneTreeDock::get_singleton()->hide_tab_buttons();
  399. EditorNode::get_singleton()->notify_all_debug_sessions_exited();
  400. }
  401. }
  402. void EditorDebuggerNode::_debugger_wants_stop(int p_id) {
  403. // Ask editor to kill PID.
  404. int pid = get_debugger(p_id)->get_remote_pid();
  405. if (pid) {
  406. callable_mp(EditorNode::get_singleton(), &EditorNode::stop_child_process).call_deferred(pid);
  407. }
  408. }
  409. void EditorDebuggerNode::_debugger_changed(int p_tab) {
  410. if (get_inspected_remote_object()) {
  411. // Clear inspected object, you can only inspect objects in selected debugger.
  412. // Hopefully, in the future, we will have one inspector per debugger.
  413. EditorNode::get_singleton()->push_item(nullptr);
  414. }
  415. if (get_previous_debugger()) {
  416. _text_editor_stack_clear(get_previous_debugger());
  417. }
  418. if (remote_scene_tree->is_visible_in_tree()) {
  419. get_current_debugger()->request_remote_tree();
  420. }
  421. if (get_current_debugger()->is_breaked()) {
  422. _text_editor_stack_goto(get_current_debugger());
  423. }
  424. _break_state_changed();
  425. }
  426. void EditorDebuggerNode::set_script_debug_button(MenuButton *p_button) {
  427. script_menu = p_button;
  428. script_menu->set_text(TTR("Debug"));
  429. script_menu->set_switch_on_hover(true);
  430. PopupMenu *p = script_menu->get_popup();
  431. p->add_shortcut(ED_GET_SHORTCUT("debugger/step_into"), DEBUG_STEP);
  432. p->add_shortcut(ED_GET_SHORTCUT("debugger/step_over"), DEBUG_NEXT);
  433. p->add_separator();
  434. p->add_shortcut(ED_GET_SHORTCUT("debugger/break"), DEBUG_BREAK);
  435. p->add_shortcut(ED_GET_SHORTCUT("debugger/continue"), DEBUG_CONTINUE);
  436. p->add_separator();
  437. p->add_check_shortcut(ED_GET_SHORTCUT("debugger/debug_with_external_editor"), DEBUG_WITH_EXTERNAL_EDITOR);
  438. p->connect(SceneStringName(id_pressed), callable_mp(this, &EditorDebuggerNode::_menu_option));
  439. _break_state_changed();
  440. script_menu->show();
  441. }
  442. void EditorDebuggerNode::_break_state_changed() {
  443. const bool breaked = get_current_debugger()->is_breaked();
  444. const bool can_debug = get_current_debugger()->is_debuggable();
  445. if (breaked) { // Show debugger.
  446. EditorNode::get_bottom_panel()->make_item_visible(this);
  447. }
  448. // Update script menu.
  449. if (!script_menu) {
  450. return;
  451. }
  452. PopupMenu *p = script_menu->get_popup();
  453. p->set_item_disabled(p->get_item_index(DEBUG_NEXT), !(breaked && can_debug));
  454. p->set_item_disabled(p->get_item_index(DEBUG_STEP), !(breaked && can_debug));
  455. p->set_item_disabled(p->get_item_index(DEBUG_BREAK), breaked);
  456. p->set_item_disabled(p->get_item_index(DEBUG_CONTINUE), !breaked);
  457. }
  458. void EditorDebuggerNode::_menu_option(int p_id) {
  459. switch (p_id) {
  460. case DEBUG_NEXT: {
  461. debug_next();
  462. } break;
  463. case DEBUG_STEP: {
  464. debug_step();
  465. } break;
  466. case DEBUG_BREAK: {
  467. debug_break();
  468. } break;
  469. case DEBUG_CONTINUE: {
  470. debug_continue();
  471. } break;
  472. case DEBUG_WITH_EXTERNAL_EDITOR: {
  473. bool ischecked = script_menu->get_popup()->is_item_checked(script_menu->get_popup()->get_item_index(DEBUG_WITH_EXTERNAL_EDITOR));
  474. debug_with_external_editor = !ischecked;
  475. script_menu->get_popup()->set_item_checked(script_menu->get_popup()->get_item_index(DEBUG_WITH_EXTERNAL_EDITOR), !ischecked);
  476. if (!initializing) {
  477. EditorSettings::get_singleton()->set_project_metadata("debug_options", "debug_with_external_editor", !ischecked);
  478. }
  479. } break;
  480. }
  481. }
  482. void EditorDebuggerNode::_update_debug_options() {
  483. if (EditorSettings::get_singleton()->get_project_metadata("debug_options", "debug_with_external_editor", false).operator bool()) {
  484. _menu_option(DEBUG_WITH_EXTERNAL_EDITOR);
  485. }
  486. }
  487. void EditorDebuggerNode::_paused() {
  488. const bool paused = EditorRunBar::get_singleton()->get_pause_button()->is_pressed();
  489. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  490. if (paused && !dbg->is_breaked()) {
  491. dbg->debug_break();
  492. } else if (!paused && dbg->is_breaked()) {
  493. dbg->debug_continue();
  494. }
  495. });
  496. }
  497. void EditorDebuggerNode::_breaked(bool p_breaked, bool p_can_debug, const String &p_message, bool p_has_stackdump, int p_debugger) {
  498. if (get_current_debugger() != get_debugger(p_debugger)) {
  499. if (!p_breaked) {
  500. return;
  501. }
  502. tabs->set_current_tab(p_debugger);
  503. }
  504. _break_state_changed();
  505. EditorRunBar::get_singleton()->get_pause_button()->set_pressed(p_breaked);
  506. emit_signal(SNAME("breaked"), p_breaked, p_can_debug);
  507. }
  508. bool EditorDebuggerNode::is_skip_breakpoints() const {
  509. return get_current_debugger()->is_skip_breakpoints();
  510. }
  511. void EditorDebuggerNode::set_breakpoint(const String &p_path, int p_line, bool p_enabled) {
  512. breakpoints[Breakpoint(p_path, p_line)] = p_enabled;
  513. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  514. dbg->set_breakpoint(p_path, p_line, p_enabled);
  515. });
  516. emit_signal(SNAME("breakpoint_toggled"), p_path, p_line, p_enabled);
  517. }
  518. void EditorDebuggerNode::set_breakpoints(const String &p_path, const Array &p_lines) {
  519. for (int i = 0; i < p_lines.size(); i++) {
  520. set_breakpoint(p_path, p_lines[i], true);
  521. }
  522. for (const KeyValue<Breakpoint, bool> &E : breakpoints) {
  523. Breakpoint b = E.key;
  524. if (b.source == p_path && !p_lines.has(b.line)) {
  525. set_breakpoint(p_path, b.line, false);
  526. }
  527. }
  528. }
  529. void EditorDebuggerNode::reload_all_scripts() {
  530. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  531. dbg->reload_all_scripts();
  532. });
  533. }
  534. void EditorDebuggerNode::reload_scripts(const Vector<String> &p_script_paths) {
  535. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  536. dbg->reload_scripts(p_script_paths);
  537. });
  538. }
  539. void EditorDebuggerNode::debug_next() {
  540. get_current_debugger()->debug_next();
  541. }
  542. void EditorDebuggerNode::debug_step() {
  543. get_current_debugger()->debug_step();
  544. }
  545. void EditorDebuggerNode::debug_break() {
  546. get_current_debugger()->debug_break();
  547. }
  548. void EditorDebuggerNode::debug_continue() {
  549. get_current_debugger()->debug_continue();
  550. }
  551. String EditorDebuggerNode::get_var_value(const String &p_var) const {
  552. return get_current_debugger()->get_var_value(p_var);
  553. }
  554. // LiveEdit/Inspector
  555. void EditorDebuggerNode::request_remote_tree() {
  556. get_current_debugger()->request_remote_tree();
  557. }
  558. void EditorDebuggerNode::_remote_tree_updated(int p_debugger) {
  559. if (p_debugger != tabs->get_current_tab()) {
  560. return;
  561. }
  562. remote_scene_tree->clear();
  563. remote_scene_tree->update_scene_tree(get_current_debugger()->get_remote_tree(), p_debugger);
  564. }
  565. void EditorDebuggerNode::_remote_tree_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) {
  566. if (p_button != MouseButton::LEFT) {
  567. return;
  568. }
  569. TreeItem *item = Object::cast_to<TreeItem>(p_item);
  570. ERR_FAIL_NULL(item);
  571. if (p_id == EditorDebuggerTree::BUTTON_SUBSCENE) {
  572. remote_scene_tree->emit_signal(SNAME("open"), item->get_meta("scene_file_path"));
  573. } else if (p_id == EditorDebuggerTree::BUTTON_VISIBILITY) {
  574. ObjectID obj_id = item->get_metadata(0);
  575. ERR_FAIL_COND(obj_id.is_null());
  576. get_current_debugger()->update_remote_object(obj_id, "visible", !item->get_meta("visible"));
  577. get_current_debugger()->request_remote_tree();
  578. }
  579. }
  580. void EditorDebuggerNode::_remote_object_updated(ObjectID p_id, int p_debugger) {
  581. if (p_debugger != tabs->get_current_tab()) {
  582. return;
  583. }
  584. if (EditorDebuggerRemoteObject *obj = get_inspected_remote_object()) {
  585. if (obj->remote_object_id == p_id) {
  586. return; // Already being edited
  587. }
  588. }
  589. EditorNode::get_singleton()->push_item(get_current_debugger()->get_remote_object(p_id));
  590. }
  591. void EditorDebuggerNode::_remote_object_property_updated(ObjectID p_id, const String &p_property, int p_debugger) {
  592. if (p_debugger != tabs->get_current_tab()) {
  593. return;
  594. }
  595. if (EditorDebuggerRemoteObject *obj = get_inspected_remote_object()) {
  596. if (obj->remote_object_id != p_id) {
  597. return;
  598. }
  599. InspectorDock::get_inspector_singleton()->update_property(p_property);
  600. }
  601. }
  602. void EditorDebuggerNode::_remote_object_requested(ObjectID p_id, int p_debugger) {
  603. if (p_debugger != tabs->get_current_tab()) {
  604. return;
  605. }
  606. inspect_edited_object_timeout = 0.7; // Temporarily disable timeout to avoid multiple requests.
  607. get_current_debugger()->request_remote_object(p_id);
  608. }
  609. void EditorDebuggerNode::_save_node_requested(ObjectID p_id, const String &p_file, int p_debugger) {
  610. if (p_debugger != tabs->get_current_tab()) {
  611. return;
  612. }
  613. get_current_debugger()->save_node(p_id, p_file);
  614. }
  615. void EditorDebuggerNode::_breakpoint_set_in_tree(Ref<RefCounted> p_script, int p_line, bool p_enabled, int p_debugger) {
  616. if (p_debugger != tabs->get_current_tab()) {
  617. return;
  618. }
  619. emit_signal(SNAME("breakpoint_set_in_tree"), p_script, p_line, p_enabled);
  620. }
  621. void EditorDebuggerNode::_breakpoints_cleared_in_tree(int p_debugger) {
  622. if (p_debugger != tabs->get_current_tab()) {
  623. return;
  624. }
  625. emit_signal(SNAME("breakpoints_cleared_in_tree"));
  626. }
  627. // Remote inspector/edit.
  628. void EditorDebuggerNode::_method_changeds(void *p_ud, Object *p_base, const StringName &p_name, const Variant **p_args, int p_argcount) {
  629. if (!singleton) {
  630. return;
  631. }
  632. _for_all(singleton->tabs, [&](ScriptEditorDebugger *dbg) {
  633. dbg->_method_changed(p_base, p_name, p_args, p_argcount);
  634. });
  635. }
  636. void EditorDebuggerNode::_property_changeds(void *p_ud, Object *p_base, const StringName &p_property, const Variant &p_value) {
  637. if (!singleton) {
  638. return;
  639. }
  640. _for_all(singleton->tabs, [&](ScriptEditorDebugger *dbg) {
  641. dbg->_property_changed(p_base, p_property, p_value);
  642. });
  643. }
  644. // LiveDebug
  645. void EditorDebuggerNode::set_live_debugging(bool p_enabled) {
  646. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  647. dbg->set_live_debugging(p_enabled);
  648. });
  649. }
  650. void EditorDebuggerNode::update_live_edit_root() {
  651. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  652. dbg->update_live_edit_root();
  653. });
  654. }
  655. void EditorDebuggerNode::live_debug_create_node(const NodePath &p_parent, const String &p_type, const String &p_name) {
  656. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  657. dbg->live_debug_create_node(p_parent, p_type, p_name);
  658. });
  659. }
  660. void EditorDebuggerNode::live_debug_instantiate_node(const NodePath &p_parent, const String &p_path, const String &p_name) {
  661. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  662. dbg->live_debug_instantiate_node(p_parent, p_path, p_name);
  663. });
  664. }
  665. void EditorDebuggerNode::live_debug_remove_node(const NodePath &p_at) {
  666. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  667. dbg->live_debug_remove_node(p_at);
  668. });
  669. }
  670. void EditorDebuggerNode::live_debug_remove_and_keep_node(const NodePath &p_at, ObjectID p_keep_id) {
  671. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  672. dbg->live_debug_remove_and_keep_node(p_at, p_keep_id);
  673. });
  674. }
  675. void EditorDebuggerNode::live_debug_restore_node(ObjectID p_id, const NodePath &p_at, int p_at_pos) {
  676. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  677. dbg->live_debug_restore_node(p_id, p_at, p_at_pos);
  678. });
  679. }
  680. void EditorDebuggerNode::live_debug_duplicate_node(const NodePath &p_at, const String &p_new_name) {
  681. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  682. dbg->live_debug_duplicate_node(p_at, p_new_name);
  683. });
  684. }
  685. void EditorDebuggerNode::live_debug_reparent_node(const NodePath &p_at, const NodePath &p_new_place, const String &p_new_name, int p_at_pos) {
  686. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  687. dbg->live_debug_reparent_node(p_at, p_new_place, p_new_name, p_at_pos);
  688. });
  689. }
  690. void EditorDebuggerNode::set_camera_override(CameraOverride p_override) {
  691. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  692. dbg->set_camera_override(p_override);
  693. });
  694. camera_override = p_override;
  695. }
  696. EditorDebuggerNode::CameraOverride EditorDebuggerNode::get_camera_override() {
  697. return camera_override;
  698. }
  699. void EditorDebuggerNode::add_debugger_plugin(const Ref<EditorDebuggerPlugin> &p_plugin) {
  700. ERR_FAIL_COND_MSG(p_plugin.is_null(), "Debugger plugin is null.");
  701. ERR_FAIL_COND_MSG(debugger_plugins.has(p_plugin), "Debugger plugin already exists.");
  702. debugger_plugins.insert(p_plugin);
  703. Ref<EditorDebuggerPlugin> plugin = p_plugin;
  704. for (int i = 0; get_debugger(i); i++) {
  705. plugin->create_session(get_debugger(i));
  706. }
  707. }
  708. void EditorDebuggerNode::remove_debugger_plugin(const Ref<EditorDebuggerPlugin> &p_plugin) {
  709. ERR_FAIL_COND_MSG(p_plugin.is_null(), "Debugger plugin is null.");
  710. ERR_FAIL_COND_MSG(!debugger_plugins.has(p_plugin), "Debugger plugin doesn't exists.");
  711. debugger_plugins.erase(p_plugin);
  712. Ref<EditorDebuggerPlugin>(p_plugin)->clear();
  713. }
  714. bool EditorDebuggerNode::plugins_capture(ScriptEditorDebugger *p_debugger, const String &p_message, const Array &p_data) {
  715. int session_index = tabs->get_tab_idx_from_control(p_debugger);
  716. ERR_FAIL_COND_V(session_index < 0, false);
  717. int colon_index = p_message.find_char(':');
  718. ERR_FAIL_COND_V_MSG(colon_index < 1, false, "Invalid message received.");
  719. const String cap = p_message.substr(0, colon_index);
  720. bool parsed = false;
  721. for (Ref<EditorDebuggerPlugin> plugin : debugger_plugins) {
  722. if (plugin->has_capture(cap)) {
  723. parsed |= plugin->capture(p_message, p_data, session_index);
  724. }
  725. }
  726. return parsed;
  727. }