script_editor_debugger.cpp 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131
  1. /**************************************************************************/
  2. /* script_editor_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 "script_editor_debugger.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/debugger/debugger_marshalls.h"
  33. #include "core/debugger/remote_debugger.h"
  34. #include "core/io/marshalls.h"
  35. #include "core/string/ustring.h"
  36. #include "core/version.h"
  37. #include "editor/debugger/debug_adapter/debug_adapter_protocol.h"
  38. #include "editor/debugger/editor_performance_profiler.h"
  39. #include "editor/debugger/editor_profiler.h"
  40. #include "editor/debugger/editor_visual_profiler.h"
  41. #include "editor/editor_file_system.h"
  42. #include "editor/editor_log.h"
  43. #include "editor/editor_node.h"
  44. #include "editor/editor_property_name_processor.h"
  45. #include "editor/editor_scale.h"
  46. #include "editor/editor_settings.h"
  47. #include "editor/editor_string_names.h"
  48. #include "editor/gui/editor_file_dialog.h"
  49. #include "editor/inspector_dock.h"
  50. #include "editor/plugins/canvas_item_editor_plugin.h"
  51. #include "editor/plugins/editor_debugger_plugin.h"
  52. #include "editor/plugins/node_3d_editor_plugin.h"
  53. #include "main/performance.h"
  54. #include "scene/3d/camera_3d.h"
  55. #include "scene/debugger/scene_debugger.h"
  56. #include "scene/gui/dialogs.h"
  57. #include "scene/gui/grid_container.h"
  58. #include "scene/gui/label.h"
  59. #include "scene/gui/line_edit.h"
  60. #include "scene/gui/margin_container.h"
  61. #include "scene/gui/rich_text_label.h"
  62. #include "scene/gui/separator.h"
  63. #include "scene/gui/split_container.h"
  64. #include "scene/gui/tab_container.h"
  65. #include "scene/gui/texture_button.h"
  66. #include "scene/gui/tree.h"
  67. #include "scene/resources/packed_scene.h"
  68. #include "servers/debugger/servers_debugger.h"
  69. #include "servers/display_server.h"
  70. using CameraOverride = EditorDebuggerNode::CameraOverride;
  71. void ScriptEditorDebugger::_put_msg(String p_message, Array p_data, uint64_t p_thread_id) {
  72. ERR_FAIL_COND(p_thread_id == Thread::UNASSIGNED_ID);
  73. if (is_session_active()) {
  74. Array msg;
  75. msg.push_back(p_message);
  76. msg.push_back(p_thread_id);
  77. msg.push_back(p_data);
  78. peer->put_message(msg);
  79. }
  80. }
  81. void ScriptEditorDebugger::debug_copy() {
  82. String msg = reason->get_text();
  83. if (msg.is_empty()) {
  84. return;
  85. }
  86. DisplayServer::get_singleton()->clipboard_set(msg);
  87. }
  88. void ScriptEditorDebugger::debug_skip_breakpoints() {
  89. skip_breakpoints_value = !skip_breakpoints_value;
  90. if (skip_breakpoints_value) {
  91. skip_breakpoints->set_icon(get_editor_theme_icon(SNAME("DebugSkipBreakpointsOn")));
  92. } else {
  93. skip_breakpoints->set_icon(get_editor_theme_icon(SNAME("DebugSkipBreakpointsOff")));
  94. }
  95. Array msg;
  96. msg.push_back(skip_breakpoints_value);
  97. _put_msg("set_skip_breakpoints", msg, debugging_thread_id != Thread::UNASSIGNED_ID ? debugging_thread_id : Thread::MAIN_ID);
  98. }
  99. void ScriptEditorDebugger::debug_next() {
  100. ERR_FAIL_COND(!is_breaked());
  101. _put_msg("next", Array(), debugging_thread_id);
  102. _clear_execution();
  103. }
  104. void ScriptEditorDebugger::debug_step() {
  105. ERR_FAIL_COND(!is_breaked());
  106. _put_msg("step", Array(), debugging_thread_id);
  107. _clear_execution();
  108. }
  109. void ScriptEditorDebugger::debug_break() {
  110. ERR_FAIL_COND(is_breaked());
  111. _put_msg("break", Array());
  112. }
  113. void ScriptEditorDebugger::debug_continue() {
  114. ERR_FAIL_COND(!is_breaked());
  115. // Allow focus stealing only if we actually run this client for security.
  116. if (remote_pid && EditorNode::get_singleton()->has_child_process(remote_pid)) {
  117. DisplayServer::get_singleton()->enable_for_stealing_focus(remote_pid);
  118. }
  119. _clear_execution();
  120. _put_msg("continue", Array(), debugging_thread_id);
  121. _put_msg("servers:foreground", Array());
  122. }
  123. void ScriptEditorDebugger::update_tabs() {
  124. if (error_count == 0 && warning_count == 0) {
  125. errors_tab->set_name(TTR("Errors"));
  126. tabs->set_tab_icon(tabs->get_tab_idx_from_control(errors_tab), Ref<Texture2D>());
  127. } else {
  128. errors_tab->set_name(TTR("Errors") + " (" + itos(error_count + warning_count) + ")");
  129. if (error_count >= 1 && warning_count >= 1) {
  130. tabs->set_tab_icon(tabs->get_tab_idx_from_control(errors_tab), get_editor_theme_icon(SNAME("ErrorWarning")));
  131. } else if (error_count >= 1) {
  132. tabs->set_tab_icon(tabs->get_tab_idx_from_control(errors_tab), get_editor_theme_icon(SNAME("Error")));
  133. } else {
  134. tabs->set_tab_icon(tabs->get_tab_idx_from_control(errors_tab), get_editor_theme_icon(SNAME("Warning")));
  135. }
  136. }
  137. }
  138. void ScriptEditorDebugger::clear_style() {
  139. tabs->remove_theme_style_override("panel");
  140. }
  141. void ScriptEditorDebugger::save_node(ObjectID p_id, const String &p_file) {
  142. Array msg;
  143. msg.push_back(p_id);
  144. msg.push_back(p_file);
  145. _put_msg("scene:save_node", msg);
  146. }
  147. void ScriptEditorDebugger::_file_selected(const String &p_file) {
  148. switch (file_dialog_purpose) {
  149. case SAVE_MONITORS_CSV: {
  150. Error err;
  151. Ref<FileAccess> file = FileAccess::open(p_file, FileAccess::WRITE, &err);
  152. if (err != OK) {
  153. ERR_PRINT("Failed to open " + p_file);
  154. return;
  155. }
  156. Vector<String> line;
  157. line.resize(Performance::MONITOR_MAX);
  158. // signatures
  159. for (int i = 0; i < Performance::MONITOR_MAX; i++) {
  160. line.write[i] = Performance::get_singleton()->get_monitor_name(Performance::Monitor(i));
  161. }
  162. file->store_csv_line(line);
  163. // values
  164. Vector<List<float>::Element *> iterators;
  165. iterators.resize(Performance::MONITOR_MAX);
  166. bool continue_iteration = false;
  167. for (int i = 0; i < Performance::MONITOR_MAX; i++) {
  168. iterators.write[i] = performance_profiler->get_monitor_data(Performance::get_singleton()->get_monitor_name(Performance::Monitor(i)))->back();
  169. continue_iteration = continue_iteration || iterators[i];
  170. }
  171. while (continue_iteration) {
  172. continue_iteration = false;
  173. for (int i = 0; i < Performance::MONITOR_MAX; i++) {
  174. if (iterators[i]) {
  175. line.write[i] = String::num_real(iterators[i]->get());
  176. iterators.write[i] = iterators[i]->prev();
  177. } else {
  178. line.write[i] = "";
  179. }
  180. continue_iteration = continue_iteration || iterators[i];
  181. }
  182. file->store_csv_line(line);
  183. }
  184. file->store_string("\n");
  185. Vector<Vector<String>> profiler_data = profiler->get_data_as_csv();
  186. for (int i = 0; i < profiler_data.size(); i++) {
  187. file->store_csv_line(profiler_data[i]);
  188. }
  189. } break;
  190. case SAVE_VRAM_CSV: {
  191. Error err;
  192. Ref<FileAccess> file = FileAccess::open(p_file, FileAccess::WRITE, &err);
  193. if (err != OK) {
  194. ERR_PRINT("Failed to open " + p_file);
  195. return;
  196. }
  197. Vector<String> headers;
  198. headers.resize(vmem_tree->get_columns());
  199. for (int i = 0; i < vmem_tree->get_columns(); ++i) {
  200. headers.write[i] = vmem_tree->get_column_title(i);
  201. }
  202. file->store_csv_line(headers);
  203. if (vmem_tree->get_root()) {
  204. TreeItem *ti = vmem_tree->get_root()->get_first_child();
  205. while (ti) {
  206. Vector<String> values;
  207. values.resize(vmem_tree->get_columns());
  208. for (int i = 0; i < vmem_tree->get_columns(); ++i) {
  209. values.write[i] = ti->get_text(i);
  210. }
  211. file->store_csv_line(values);
  212. ti = ti->get_next();
  213. }
  214. }
  215. } break;
  216. }
  217. }
  218. void ScriptEditorDebugger::request_remote_tree() {
  219. _put_msg("scene:request_scene_tree", Array());
  220. }
  221. const SceneDebuggerTree *ScriptEditorDebugger::get_remote_tree() {
  222. return scene_tree;
  223. }
  224. void ScriptEditorDebugger::update_remote_object(ObjectID p_obj_id, const String &p_prop, const Variant &p_value) {
  225. Array msg;
  226. msg.push_back(p_obj_id);
  227. msg.push_back(p_prop);
  228. msg.push_back(p_value);
  229. _put_msg("scene:set_object_property", msg);
  230. }
  231. void ScriptEditorDebugger::request_remote_object(ObjectID p_obj_id) {
  232. ERR_FAIL_COND(p_obj_id.is_null());
  233. Array msg;
  234. msg.push_back(p_obj_id);
  235. _put_msg("scene:inspect_object", msg);
  236. }
  237. Object *ScriptEditorDebugger::get_remote_object(ObjectID p_id) {
  238. return inspector->get_object(p_id);
  239. }
  240. void ScriptEditorDebugger::_remote_object_selected(ObjectID p_id) {
  241. emit_signal(SNAME("remote_object_requested"), p_id);
  242. }
  243. void ScriptEditorDebugger::_remote_object_edited(ObjectID p_id, const String &p_prop, const Variant &p_value) {
  244. update_remote_object(p_id, p_prop, p_value);
  245. request_remote_object(p_id);
  246. }
  247. void ScriptEditorDebugger::_remote_object_property_updated(ObjectID p_id, const String &p_property) {
  248. emit_signal(SNAME("remote_object_property_updated"), p_id, p_property);
  249. }
  250. void ScriptEditorDebugger::_video_mem_request() {
  251. _put_msg("servers:memory", Array());
  252. }
  253. void ScriptEditorDebugger::_video_mem_export() {
  254. file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  255. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  256. file_dialog->clear_filters();
  257. file_dialog_purpose = SAVE_VRAM_CSV;
  258. file_dialog->popup_file_dialog();
  259. }
  260. Size2 ScriptEditorDebugger::get_minimum_size() const {
  261. Size2 ms = MarginContainer::get_minimum_size();
  262. ms.y = MAX(ms.y, 250 * EDSCALE);
  263. return ms;
  264. }
  265. void ScriptEditorDebugger::_thread_debug_enter(uint64_t p_thread_id) {
  266. ERR_FAIL_COND(!threads_debugged.has(p_thread_id));
  267. ThreadDebugged &td = threads_debugged[p_thread_id];
  268. _set_reason_text(td.error, MESSAGE_ERROR);
  269. emit_signal(SNAME("breaked"), true, td.can_debug, td.error, td.has_stackdump);
  270. if (!td.error.is_empty()) {
  271. tabs->set_current_tab(0);
  272. }
  273. inspector->clear_cache(); // Take a chance to force remote objects update.
  274. _put_msg("get_stack_dump", Array(), p_thread_id);
  275. }
  276. void ScriptEditorDebugger::_select_thread(int p_index) {
  277. debugging_thread_id = threads->get_item_metadata(threads->get_selected());
  278. _thread_debug_enter(debugging_thread_id);
  279. }
  280. void ScriptEditorDebugger::_parse_message(const String &p_msg, uint64_t p_thread_id, const Array &p_data) {
  281. emit_signal(SNAME("debug_data"), p_msg, p_data);
  282. if (p_msg == "debug_enter") {
  283. ERR_FAIL_COND(p_data.size() != 4);
  284. ThreadDebugged td;
  285. td.name = p_data[3];
  286. td.error = p_data[1];
  287. td.can_debug = p_data[0];
  288. td.has_stackdump = p_data[2];
  289. td.thread_id = p_thread_id;
  290. static uint32_t order_inc = 0;
  291. td.debug_order = order_inc++;
  292. threads_debugged.insert(p_thread_id, td);
  293. if (threads_debugged.size() == 1) {
  294. // First thread that requests debug
  295. debugging_thread_id = p_thread_id;
  296. _thread_debug_enter(p_thread_id);
  297. can_request_idle_draw = true;
  298. if (is_move_to_foreground()) {
  299. DisplayServer::get_singleton()->window_move_to_foreground();
  300. }
  301. profiler->set_enabled(false, false);
  302. visual_profiler->set_enabled(false);
  303. }
  304. _update_buttons_state();
  305. } else if (p_msg == "debug_exit") {
  306. threads_debugged.erase(p_thread_id);
  307. if (p_thread_id == debugging_thread_id) {
  308. _clear_execution();
  309. if (threads_debugged.size() == 0) {
  310. debugging_thread_id = Thread::UNASSIGNED_ID;
  311. } else {
  312. // Find next thread to debug.
  313. uint32_t min_order = 0xFFFFFFFF;
  314. uint64_t next_thread = Thread::UNASSIGNED_ID;
  315. for (KeyValue<uint64_t, ThreadDebugged> T : threads_debugged) {
  316. if (T.value.debug_order < min_order) {
  317. min_order = T.value.debug_order;
  318. next_thread = T.key;
  319. }
  320. }
  321. debugging_thread_id = next_thread;
  322. }
  323. if (debugging_thread_id == Thread::UNASSIGNED_ID) {
  324. // Nothing else to debug.
  325. profiler->set_enabled(true, false);
  326. profiler->disable_seeking();
  327. visual_profiler->set_enabled(true);
  328. _set_reason_text(TTR("Execution resumed."), MESSAGE_SUCCESS);
  329. emit_signal(SNAME("breaked"), false, false, "", false);
  330. _update_buttons_state();
  331. } else {
  332. _thread_debug_enter(debugging_thread_id);
  333. }
  334. } else {
  335. _update_buttons_state();
  336. }
  337. } else if (p_msg == "set_pid") {
  338. ERR_FAIL_COND(p_data.size() < 1);
  339. remote_pid = p_data[0];
  340. } else if (p_msg == "scene:click_ctrl") {
  341. ERR_FAIL_COND(p_data.size() < 2);
  342. clicked_ctrl->set_text(p_data[0]);
  343. clicked_ctrl_type->set_text(p_data[1]);
  344. } else if (p_msg == "scene:scene_tree") {
  345. scene_tree->nodes.clear();
  346. scene_tree->deserialize(p_data);
  347. emit_signal(SNAME("remote_tree_updated"));
  348. _update_buttons_state();
  349. } else if (p_msg == "scene:inspect_object") {
  350. ObjectID id = inspector->add_object(p_data);
  351. if (id.is_valid()) {
  352. emit_signal(SNAME("remote_object_updated"), id);
  353. }
  354. } else if (p_msg == "servers:memory_usage") {
  355. vmem_tree->clear();
  356. TreeItem *root = vmem_tree->create_item();
  357. ServersDebugger::ResourceUsage usage;
  358. usage.deserialize(p_data);
  359. uint64_t total = 0;
  360. for (const ServersDebugger::ResourceInfo &E : usage.infos) {
  361. TreeItem *it = vmem_tree->create_item(root);
  362. String type = E.type;
  363. int bytes = E.vram;
  364. it->set_text(0, E.path);
  365. it->set_text(1, type);
  366. it->set_text(2, E.format);
  367. it->set_text(3, String::humanize_size(bytes));
  368. total += bytes;
  369. if (has_theme_icon(type, EditorStringName(EditorIcons))) {
  370. it->set_icon(0, get_editor_theme_icon(type));
  371. }
  372. }
  373. vmem_total->set_tooltip_text(TTR("Bytes:") + " " + itos(total));
  374. vmem_total->set_text(String::humanize_size(total));
  375. } else if (p_msg == "servers:drawn") {
  376. can_request_idle_draw = true;
  377. } else if (p_msg == "stack_dump") {
  378. DebuggerMarshalls::ScriptStackDump stack;
  379. stack.deserialize(p_data);
  380. stack_dump->clear();
  381. inspector->clear_stack_variables();
  382. TreeItem *r = stack_dump->create_item();
  383. Array stack_dump_info;
  384. for (int i = 0; i < stack.frames.size(); i++) {
  385. TreeItem *s = stack_dump->create_item(r);
  386. Dictionary d;
  387. d["frame"] = i;
  388. d["file"] = stack.frames[i].file;
  389. d["function"] = stack.frames[i].func;
  390. d["line"] = stack.frames[i].line;
  391. stack_dump_info.push_back(d);
  392. s->set_metadata(0, d);
  393. String line = itos(i) + " - " + String(d["file"]) + ":" + itos(d["line"]) + " - at function: " + String(d["function"]);
  394. s->set_text(0, line);
  395. if (i == 0) {
  396. s->select(0);
  397. }
  398. }
  399. emit_signal(SNAME("stack_dump"), stack_dump_info);
  400. } else if (p_msg == "stack_frame_vars") {
  401. inspector->clear_stack_variables();
  402. ERR_FAIL_COND(p_data.size() != 1);
  403. emit_signal(SNAME("stack_frame_vars"), p_data[0]);
  404. } else if (p_msg == "stack_frame_var") {
  405. inspector->add_stack_variable(p_data);
  406. emit_signal(SNAME("stack_frame_var"), p_data);
  407. } else if (p_msg == "output") {
  408. ERR_FAIL_COND(p_data.size() != 2);
  409. ERR_FAIL_COND(p_data[0].get_type() != Variant::PACKED_STRING_ARRAY);
  410. Vector<String> output_strings = p_data[0];
  411. ERR_FAIL_COND(p_data[1].get_type() != Variant::PACKED_INT32_ARRAY);
  412. Vector<int> output_types = p_data[1];
  413. ERR_FAIL_COND(output_strings.size() != output_types.size());
  414. for (int i = 0; i < output_strings.size(); i++) {
  415. RemoteDebugger::MessageType type = (RemoteDebugger::MessageType)(int)(output_types[i]);
  416. EditorLog::MessageType msg_type;
  417. switch (type) {
  418. case RemoteDebugger::MESSAGE_TYPE_LOG: {
  419. msg_type = EditorLog::MSG_TYPE_STD;
  420. } break;
  421. case RemoteDebugger::MESSAGE_TYPE_LOG_RICH: {
  422. msg_type = EditorLog::MSG_TYPE_STD_RICH;
  423. } break;
  424. case RemoteDebugger::MESSAGE_TYPE_ERROR: {
  425. msg_type = EditorLog::MSG_TYPE_ERROR;
  426. } break;
  427. default: {
  428. WARN_PRINT("Unhandled script debugger message type: " + itos(type));
  429. msg_type = EditorLog::MSG_TYPE_STD;
  430. } break;
  431. }
  432. EditorNode::get_log()->add_message(output_strings[i], msg_type);
  433. emit_signal(SNAME("output"), output_strings[i]);
  434. }
  435. } else if (p_msg == "performance:profile_frame") {
  436. Vector<float> frame_data;
  437. frame_data.resize(p_data.size());
  438. for (int i = 0; i < p_data.size(); i++) {
  439. frame_data.write[i] = p_data[i];
  440. }
  441. performance_profiler->add_profile_frame(frame_data);
  442. } else if (p_msg == "visual:profile_frame") {
  443. ServersDebugger::VisualProfilerFrame frame;
  444. frame.deserialize(p_data);
  445. EditorVisualProfiler::Metric metric;
  446. metric.areas.resize(frame.areas.size());
  447. metric.frame_number = frame.frame_number;
  448. metric.valid = true;
  449. {
  450. EditorVisualProfiler::Metric::Area *areas_ptr = metric.areas.ptrw();
  451. for (int i = 0; i < frame.areas.size(); i++) {
  452. areas_ptr[i].name = frame.areas[i].name;
  453. areas_ptr[i].cpu_time = frame.areas[i].cpu_msec;
  454. areas_ptr[i].gpu_time = frame.areas[i].gpu_msec;
  455. }
  456. }
  457. visual_profiler->add_frame_metric(metric);
  458. } else if (p_msg == "error") {
  459. DebuggerMarshalls::OutputError oe;
  460. ERR_FAIL_COND_MSG(oe.deserialize(p_data) == false, "Failed to deserialize error message");
  461. // Format time.
  462. Array time_vals;
  463. time_vals.push_back(oe.hr);
  464. time_vals.push_back(oe.min);
  465. time_vals.push_back(oe.sec);
  466. time_vals.push_back(oe.msec);
  467. bool e;
  468. String time = String("%d:%02d:%02d:%04d").sprintf(time_vals, &e);
  469. // Rest of the error data.
  470. bool source_is_project_file = oe.source_file.begins_with("res://");
  471. // Metadata to highlight error line in scripts.
  472. Array source_meta;
  473. source_meta.push_back(oe.source_file);
  474. source_meta.push_back(oe.source_line);
  475. // Create error tree to display above error or warning details.
  476. TreeItem *r = error_tree->get_root();
  477. if (!r) {
  478. r = error_tree->create_item();
  479. }
  480. // Also provide the relevant details as tooltip to quickly check without
  481. // uncollapsing the tree.
  482. String tooltip = oe.warning ? TTR("Warning:") : TTR("Error:");
  483. TreeItem *error = error_tree->create_item(r);
  484. if (oe.warning) {
  485. error->set_meta("_is_warning", true);
  486. } else {
  487. error->set_meta("_is_error", true);
  488. }
  489. error->set_collapsed(true);
  490. error->set_icon(0, get_editor_theme_icon(oe.warning ? SNAME("Warning") : SNAME("Error")));
  491. error->set_text(0, time);
  492. error->set_text_alignment(0, HORIZONTAL_ALIGNMENT_LEFT);
  493. const Color color = get_theme_color(oe.warning ? SNAME("warning_color") : SNAME("error_color"), EditorStringName(Editor));
  494. error->set_custom_color(0, color);
  495. error->set_custom_color(1, color);
  496. String error_title;
  497. if (!oe.source_func.is_empty() && source_is_project_file) {
  498. // If source function is inside the project file.
  499. error_title += oe.source_func + ": ";
  500. } else if (oe.callstack.size() > 0) {
  501. // Otherwise, if available, use the script's stack in the error title.
  502. error_title = _format_frame_text(&oe.callstack[0]) + ": ";
  503. } else if (!oe.source_func.is_empty()) {
  504. // Otherwise try to use the C++ source function.
  505. error_title += oe.source_func + ": ";
  506. }
  507. // If we have a (custom) error message, use it as title, and add a C++ Error
  508. // item with the original error condition.
  509. error_title += oe.error_descr.is_empty() ? oe.error : oe.error_descr;
  510. error->set_text(1, error_title);
  511. tooltip += " " + error_title + "\n";
  512. // Find the language of the error's source file.
  513. String source_language_name = "C++"; // Default value is the old hard-coded one.
  514. const String source_file_extension = oe.source_file.get_extension();
  515. for (int i = 0; i < ScriptServer::get_language_count(); ++i) {
  516. ScriptLanguage *script_language = ScriptServer::get_language(i);
  517. if (source_file_extension == script_language->get_extension()) {
  518. source_language_name = script_language->get_name();
  519. break;
  520. }
  521. }
  522. if (!oe.error_descr.is_empty()) {
  523. // Add item for C++ error condition.
  524. TreeItem *cpp_cond = error_tree->create_item(error);
  525. // TRANSLATORS: %s is the name of a language, e.g. C++.
  526. cpp_cond->set_text(0, "<" + vformat(TTR("%s Error"), source_language_name) + ">");
  527. cpp_cond->set_text(1, oe.error);
  528. cpp_cond->set_text_alignment(0, HORIZONTAL_ALIGNMENT_LEFT);
  529. tooltip += vformat(TTR("%s Error:"), source_language_name) + " " + oe.error + "\n";
  530. if (source_is_project_file) {
  531. cpp_cond->set_metadata(0, source_meta);
  532. }
  533. }
  534. Vector<uint8_t> v;
  535. v.resize(100);
  536. // Source of the error.
  537. String source_txt = (source_is_project_file ? oe.source_file.get_file() : oe.source_file) + ":" + itos(oe.source_line);
  538. if (!oe.source_func.is_empty()) {
  539. source_txt += " @ " + oe.source_func;
  540. if (!oe.source_func.ends_with(")")) {
  541. source_txt += "()";
  542. }
  543. }
  544. TreeItem *cpp_source = error_tree->create_item(error);
  545. // TRANSLATORS: %s is the name of a language, e.g. C++.
  546. cpp_source->set_text(0, "<" + vformat(TTR("%s Source"), source_language_name) + ">");
  547. cpp_source->set_text(1, source_txt);
  548. cpp_source->set_text_alignment(0, HORIZONTAL_ALIGNMENT_LEFT);
  549. tooltip += vformat(TTR("%s Source:"), source_language_name) + " " + source_txt + "\n";
  550. // Set metadata to highlight error line in scripts.
  551. if (source_is_project_file) {
  552. error->set_metadata(0, source_meta);
  553. cpp_source->set_metadata(0, source_meta);
  554. }
  555. // Format stack trace.
  556. // stack_items_count is the number of elements to parse, with 3 items per frame
  557. // of the stack trace (script, method, line).
  558. const ScriptLanguage::StackInfo *infos = oe.callstack.ptr();
  559. for (unsigned int i = 0; i < (unsigned int)oe.callstack.size(); i++) {
  560. TreeItem *stack_trace = error_tree->create_item(error);
  561. Array meta;
  562. meta.push_back(infos[i].file);
  563. meta.push_back(infos[i].line);
  564. stack_trace->set_metadata(0, meta);
  565. if (i == 0) {
  566. stack_trace->set_text(0, "<" + TTR("Stack Trace") + ">");
  567. stack_trace->set_text_alignment(0, HORIZONTAL_ALIGNMENT_LEFT);
  568. if (!source_is_project_file) {
  569. // Only override metadata if the source is not inside the project.
  570. error->set_metadata(0, meta);
  571. }
  572. tooltip += TTR("Stack Trace:") + "\n";
  573. }
  574. String frame_txt = _format_frame_text(&infos[i]);
  575. tooltip += frame_txt + "\n";
  576. stack_trace->set_text(1, frame_txt);
  577. }
  578. error->set_tooltip_text(0, tooltip);
  579. error->set_tooltip_text(1, tooltip);
  580. if (warning_count == 0 && error_count == 0) {
  581. expand_all_button->set_disabled(false);
  582. collapse_all_button->set_disabled(false);
  583. clear_button->set_disabled(false);
  584. }
  585. if (oe.warning) {
  586. warning_count++;
  587. } else {
  588. error_count++;
  589. }
  590. } else if (p_msg == "servers:function_signature") {
  591. // Cache a profiler signature.
  592. ServersDebugger::ScriptFunctionSignature sig;
  593. sig.deserialize(p_data);
  594. profiler_signature[sig.id] = sig.name;
  595. } else if (p_msg == "servers:profile_frame" || p_msg == "servers:profile_total") {
  596. EditorProfiler::Metric metric;
  597. ServersDebugger::ServersProfilerFrame frame;
  598. frame.deserialize(p_data);
  599. metric.valid = true;
  600. metric.frame_number = frame.frame_number;
  601. metric.frame_time = frame.frame_time;
  602. metric.process_time = frame.process_time;
  603. metric.physics_time = frame.physics_time;
  604. metric.physics_frame_time = frame.physics_frame_time;
  605. if (frame.servers.size()) {
  606. EditorProfiler::Metric::Category frame_time;
  607. frame_time.signature = "category_frame_time";
  608. frame_time.name = "Frame Time";
  609. frame_time.total_time = metric.frame_time;
  610. EditorProfiler::Metric::Category::Item item;
  611. item.calls = 1;
  612. item.line = 0;
  613. item.name = "Physics Time";
  614. item.total = metric.physics_time;
  615. item.self = item.total;
  616. item.signature = "physics_time";
  617. frame_time.items.push_back(item);
  618. item.name = "Process Time";
  619. item.total = metric.process_time;
  620. item.self = item.total;
  621. item.signature = "process_time";
  622. frame_time.items.push_back(item);
  623. item.name = "Physics Frame Time";
  624. item.total = metric.physics_frame_time;
  625. item.self = item.total;
  626. item.signature = "physics_frame_time";
  627. frame_time.items.push_back(item);
  628. metric.categories.push_back(frame_time);
  629. }
  630. for (int i = 0; i < frame.servers.size(); i++) {
  631. const ServersDebugger::ServerInfo &srv = frame.servers[i];
  632. EditorProfiler::Metric::Category c;
  633. const String name = srv.name;
  634. c.name = EditorPropertyNameProcessor::get_singleton()->process_name(name, EditorPropertyNameProcessor::STYLE_CAPITALIZED);
  635. c.items.resize(srv.functions.size());
  636. c.total_time = 0;
  637. c.signature = "categ::" + name;
  638. for (int j = 0; j < srv.functions.size(); j++) {
  639. EditorProfiler::Metric::Category::Item item;
  640. item.calls = 1;
  641. item.line = 0;
  642. item.name = srv.functions[j].name;
  643. item.self = srv.functions[j].time;
  644. item.total = item.self;
  645. item.signature = "categ::" + name + "::" + item.name;
  646. item.name = EditorPropertyNameProcessor::get_singleton()->process_name(item.name, EditorPropertyNameProcessor::STYLE_CAPITALIZED);
  647. c.total_time += item.total;
  648. c.items.write[j] = item;
  649. }
  650. metric.categories.push_back(c);
  651. }
  652. EditorProfiler::Metric::Category funcs;
  653. funcs.total_time = frame.script_time;
  654. funcs.items.resize(frame.script_functions.size());
  655. funcs.name = "Script Functions";
  656. funcs.signature = "script_functions";
  657. for (int i = 0; i < frame.script_functions.size(); i++) {
  658. int signature = frame.script_functions[i].sig_id;
  659. int calls = frame.script_functions[i].call_count;
  660. float total = frame.script_functions[i].total_time;
  661. float self = frame.script_functions[i].self_time;
  662. EditorProfiler::Metric::Category::Item item;
  663. if (profiler_signature.has(signature)) {
  664. item.signature = profiler_signature[signature];
  665. String name = profiler_signature[signature];
  666. Vector<String> strings = name.split("::");
  667. if (strings.size() == 3) {
  668. item.name = strings[2];
  669. item.script = strings[0];
  670. item.line = strings[1].to_int();
  671. } else if (strings.size() == 4) { //Built-in scripts have an :: in their name
  672. item.name = strings[3];
  673. item.script = strings[0] + "::" + strings[1];
  674. item.line = strings[2].to_int();
  675. }
  676. } else {
  677. item.name = "SigErr " + itos(signature);
  678. }
  679. item.calls = calls;
  680. item.self = self;
  681. item.total = total;
  682. funcs.items.write[i] = item;
  683. }
  684. metric.categories.push_back(funcs);
  685. if (p_msg == "servers:profile_frame") {
  686. profiler->add_frame_metric(metric, false);
  687. } else {
  688. profiler->add_frame_metric(metric, true);
  689. }
  690. } else if (p_msg == "request_quit") {
  691. emit_signal(SNAME("stop_requested"));
  692. _stop_and_notify();
  693. } else if (p_msg == "performance:profile_names") {
  694. Vector<StringName> monitors;
  695. monitors.resize(p_data.size());
  696. for (int i = 0; i < p_data.size(); i++) {
  697. ERR_FAIL_COND(p_data[i].get_type() != Variant::STRING_NAME);
  698. monitors.set(i, p_data[i]);
  699. }
  700. performance_profiler->update_monitors(monitors);
  701. } else if (p_msg == "filesystem:update_file") {
  702. ERR_FAIL_COND(p_data.size() < 1);
  703. if (EditorFileSystem::get_singleton()) {
  704. EditorFileSystem::get_singleton()->update_file(p_data[0]);
  705. }
  706. } else {
  707. int colon_index = p_msg.find_char(':');
  708. ERR_FAIL_COND_MSG(colon_index < 1, "Invalid message received");
  709. bool parsed = EditorDebuggerNode::get_singleton()->plugins_capture(this, p_msg, p_data);
  710. if (!parsed) {
  711. WARN_PRINT("unknown message " + p_msg);
  712. }
  713. }
  714. }
  715. void ScriptEditorDebugger::_set_reason_text(const String &p_reason, MessageType p_type) {
  716. switch (p_type) {
  717. case MESSAGE_ERROR:
  718. reason->add_theme_color_override("font_color", get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  719. break;
  720. case MESSAGE_WARNING:
  721. reason->add_theme_color_override("font_color", get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
  722. break;
  723. default:
  724. reason->add_theme_color_override("font_color", get_theme_color(SNAME("success_color"), EditorStringName(Editor)));
  725. }
  726. reason->set_text(p_reason);
  727. const PackedInt32Array boundaries = TS->string_get_word_breaks(p_reason, "", 80);
  728. PackedStringArray lines;
  729. for (int i = 0; i < boundaries.size(); i += 2) {
  730. const int start = boundaries[i];
  731. const int end = boundaries[i + 1];
  732. lines.append(p_reason.substr(start, end - start + 1));
  733. }
  734. reason->set_tooltip_text(String("\n").join(lines));
  735. }
  736. void ScriptEditorDebugger::_notification(int p_what) {
  737. switch (p_what) {
  738. case NOTIFICATION_ENTER_TREE: {
  739. le_set->connect("pressed", callable_mp(this, &ScriptEditorDebugger::_live_edit_set));
  740. le_clear->connect("pressed", callable_mp(this, &ScriptEditorDebugger::_live_edit_clear));
  741. error_tree->connect("item_selected", callable_mp(this, &ScriptEditorDebugger::_error_selected));
  742. error_tree->connect("item_activated", callable_mp(this, &ScriptEditorDebugger::_error_activated));
  743. breakpoints_tree->connect("item_activated", callable_mp(this, &ScriptEditorDebugger::_breakpoint_tree_clicked));
  744. [[fallthrough]];
  745. }
  746. case NOTIFICATION_THEME_CHANGED: {
  747. tabs->add_theme_style_override("panel", get_theme_stylebox(SNAME("DebuggerPanel"), EditorStringName(EditorStyles)));
  748. skip_breakpoints->set_icon(get_editor_theme_icon(skip_breakpoints_value ? SNAME("DebugSkipBreakpointsOn") : SNAME("DebugSkipBreakpointsOff")));
  749. copy->set_icon(get_editor_theme_icon(SNAME("ActionCopy")));
  750. step->set_icon(get_editor_theme_icon(SNAME("DebugStep")));
  751. next->set_icon(get_editor_theme_icon(SNAME("DebugNext")));
  752. dobreak->set_icon(get_editor_theme_icon(SNAME("Pause")));
  753. docontinue->set_icon(get_editor_theme_icon(SNAME("DebugContinue")));
  754. vmem_refresh->set_icon(get_editor_theme_icon(SNAME("Reload")));
  755. vmem_export->set_icon(get_editor_theme_icon(SNAME("Save")));
  756. search->set_right_icon(get_editor_theme_icon(SNAME("Search")));
  757. reason->add_theme_color_override("font_color", get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  758. TreeItem *error_root = error_tree->get_root();
  759. if (error_root) {
  760. TreeItem *error = error_root->get_first_child();
  761. while (error) {
  762. if (error->has_meta("_is_warning")) {
  763. error->set_icon(0, get_editor_theme_icon(SNAME("Warning")));
  764. error->set_custom_color(0, get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
  765. error->set_custom_color(1, get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
  766. } else if (error->has_meta("_is_error")) {
  767. error->set_icon(0, get_editor_theme_icon(SNAME("Error")));
  768. error->set_custom_color(0, get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  769. error->set_custom_color(1, get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  770. }
  771. error = error->get_next();
  772. }
  773. }
  774. } break;
  775. case NOTIFICATION_PROCESS: {
  776. if (is_session_active()) {
  777. peer->poll();
  778. if (camera_override == CameraOverride::OVERRIDE_2D) {
  779. Dictionary state = CanvasItemEditor::get_singleton()->get_state();
  780. float zoom = state["zoom"];
  781. Point2 offset = state["ofs"];
  782. Transform2D transform;
  783. transform.scale_basis(Size2(zoom, zoom));
  784. transform.columns[2] = -offset * zoom;
  785. Array msg;
  786. msg.push_back(transform);
  787. _put_msg("scene:override_camera_2D:transform", msg);
  788. } else if (camera_override >= CameraOverride::OVERRIDE_3D_1) {
  789. int viewport_idx = camera_override - CameraOverride::OVERRIDE_3D_1;
  790. Node3DEditorViewport *viewport = Node3DEditor::get_singleton()->get_editor_viewport(viewport_idx);
  791. Camera3D *const cam = viewport->get_camera_3d();
  792. Array msg;
  793. msg.push_back(cam->get_camera_transform());
  794. if (cam->get_projection() == Camera3D::PROJECTION_ORTHOGONAL) {
  795. msg.push_back(false);
  796. msg.push_back(cam->get_size());
  797. } else {
  798. msg.push_back(true);
  799. msg.push_back(cam->get_fov());
  800. }
  801. msg.push_back(cam->get_near());
  802. msg.push_back(cam->get_far());
  803. _put_msg("scene:override_camera_3D:transform", msg);
  804. }
  805. if (is_breaked() && can_request_idle_draw) {
  806. _put_msg("servers:draw", Array());
  807. can_request_idle_draw = false;
  808. }
  809. }
  810. const uint64_t until = OS::get_singleton()->get_ticks_msec() + 20;
  811. while (peer.is_valid() && peer->has_message()) {
  812. Array arr = peer->get_message();
  813. if (arr.size() != 3 || arr[0].get_type() != Variant::STRING || arr[1].get_type() != Variant::INT || arr[2].get_type() != Variant::ARRAY) {
  814. _stop_and_notify();
  815. ERR_FAIL_MSG("Invalid message format received from peer");
  816. }
  817. _parse_message(arr[0], arr[1], arr[2]);
  818. if (OS::get_singleton()->get_ticks_msec() > until) {
  819. break;
  820. }
  821. }
  822. if (!is_session_active()) {
  823. _stop_and_notify();
  824. break;
  825. };
  826. } break;
  827. }
  828. }
  829. void ScriptEditorDebugger::_clear_execution() {
  830. TreeItem *ti = stack_dump->get_selected();
  831. if (!ti) {
  832. return;
  833. }
  834. Dictionary d = ti->get_metadata(0);
  835. stack_script = ResourceLoader::load(d["file"]);
  836. emit_signal(SNAME("clear_execution"), stack_script);
  837. stack_script.unref();
  838. stack_dump->clear();
  839. inspector->clear_stack_variables();
  840. }
  841. void ScriptEditorDebugger::_set_breakpoint(const String &p_file, const int &p_line, const bool &p_enabled) {
  842. Ref<Script> scr = ResourceLoader::load(p_file);
  843. emit_signal(SNAME("set_breakpoint"), scr, p_line - 1, p_enabled);
  844. scr.unref();
  845. }
  846. void ScriptEditorDebugger::_clear_breakpoints() {
  847. emit_signal(SNAME("clear_breakpoints"));
  848. }
  849. void ScriptEditorDebugger::_breakpoint_tree_clicked() {
  850. TreeItem *selected = breakpoints_tree->get_selected();
  851. if (selected->has_meta("line")) {
  852. emit_signal(SNAME("breakpoint_selected"), selected->get_parent()->get_text(0), int(selected->get_meta("line")));
  853. }
  854. }
  855. String ScriptEditorDebugger::_format_frame_text(const ScriptLanguage::StackInfo *info) {
  856. String text = info->file.get_file() + ":" + itos(info->line) + " @ " + info->func;
  857. if (!text.ends_with(")")) {
  858. text += "()";
  859. }
  860. return text;
  861. }
  862. void ScriptEditorDebugger::start(Ref<RemoteDebuggerPeer> p_peer) {
  863. _clear_errors_list();
  864. stop();
  865. profiler->set_enabled(true, true);
  866. visual_profiler->set_enabled(true);
  867. peer = p_peer;
  868. ERR_FAIL_COND(p_peer.is_null());
  869. performance_profiler->reset();
  870. set_process(true);
  871. camera_override = CameraOverride::OVERRIDE_NONE;
  872. tabs->set_current_tab(0);
  873. _set_reason_text(TTR("Debug session started."), MESSAGE_SUCCESS);
  874. _update_buttons_state();
  875. emit_signal(SNAME("started"));
  876. }
  877. void ScriptEditorDebugger::_update_buttons_state() {
  878. const bool active = is_session_active();
  879. const bool has_editor_tree = active && editor_remote_tree && editor_remote_tree->get_selected();
  880. vmem_refresh->set_disabled(!active);
  881. step->set_disabled(!active || !is_breaked() || !is_debuggable());
  882. next->set_disabled(!active || !is_breaked() || !is_debuggable());
  883. copy->set_disabled(!active || !is_breaked());
  884. docontinue->set_disabled(!active || !is_breaked());
  885. dobreak->set_disabled(!active || is_breaked());
  886. le_clear->set_disabled(!active);
  887. le_set->set_disabled(!has_editor_tree);
  888. thread_list_updating = true;
  889. LocalVector<ThreadDebugged *> threadss;
  890. for (KeyValue<uint64_t, ThreadDebugged> &I : threads_debugged) {
  891. threadss.push_back(&I.value);
  892. }
  893. threadss.sort_custom<ThreadSort>();
  894. threads->clear();
  895. int32_t selected_index = -1;
  896. for (uint32_t i = 0; i < threadss.size(); i++) {
  897. if (debugging_thread_id == threadss[i]->thread_id) {
  898. selected_index = i;
  899. }
  900. threads->add_item(threadss[i]->name);
  901. threads->set_item_metadata(threads->get_item_count() - 1, threadss[i]->thread_id);
  902. }
  903. if (selected_index != -1) {
  904. threads->select(selected_index);
  905. }
  906. thread_list_updating = false;
  907. }
  908. void ScriptEditorDebugger::_stop_and_notify() {
  909. stop();
  910. emit_signal(SNAME("stopped"));
  911. _set_reason_text(TTR("Debug session closed."), MESSAGE_WARNING);
  912. }
  913. void ScriptEditorDebugger::stop() {
  914. set_process(false);
  915. threads_debugged.clear();
  916. debugging_thread_id = Thread::UNASSIGNED_ID;
  917. remote_pid = 0;
  918. _clear_execution();
  919. inspector->clear_cache();
  920. if (peer.is_valid()) {
  921. peer->close();
  922. peer.unref();
  923. reason->set_text("");
  924. reason->set_tooltip_text("");
  925. }
  926. node_path_cache.clear();
  927. res_path_cache.clear();
  928. profiler_signature.clear();
  929. profiler->set_enabled(false, false);
  930. profiler->set_pressed(false);
  931. visual_profiler->set_enabled(false);
  932. visual_profiler->set_pressed(false);
  933. inspector->edit(nullptr);
  934. _update_buttons_state();
  935. }
  936. void ScriptEditorDebugger::_profiler_activate(bool p_enable, int p_type) {
  937. Array msg_data;
  938. msg_data.push_back(p_enable);
  939. switch (p_type) {
  940. case PROFILER_VISUAL:
  941. _put_msg("profiler:visual", msg_data);
  942. break;
  943. case PROFILER_SCRIPTS_SERVERS:
  944. if (p_enable) {
  945. // Clear old script signatures. (should we move all this into the profiler?)
  946. profiler_signature.clear();
  947. // Add max funcs options to request.
  948. Array opts;
  949. int max_funcs = EDITOR_GET("debugger/profiler_frame_max_functions");
  950. opts.push_back(CLAMP(max_funcs, 16, 512));
  951. msg_data.push_back(opts);
  952. }
  953. _put_msg("profiler:servers", msg_data);
  954. break;
  955. default:
  956. ERR_FAIL_MSG("Invalid profiler type");
  957. }
  958. }
  959. void ScriptEditorDebugger::_profiler_seeked() {
  960. if (is_breaked()) {
  961. return;
  962. }
  963. debug_break();
  964. }
  965. void ScriptEditorDebugger::_stack_dump_frame_selected() {
  966. emit_signal(SNAME("stack_frame_selected"));
  967. int frame = get_stack_script_frame();
  968. if (!request_stack_dump(frame)) {
  969. inspector->edit(nullptr);
  970. }
  971. }
  972. void ScriptEditorDebugger::_export_csv() {
  973. file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  974. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  975. file_dialog_purpose = SAVE_MONITORS_CSV;
  976. file_dialog->popup_file_dialog();
  977. }
  978. String ScriptEditorDebugger::get_var_value(const String &p_var) const {
  979. if (!is_breaked()) {
  980. return String();
  981. }
  982. return inspector->get_stack_variable(p_var);
  983. }
  984. int ScriptEditorDebugger::_get_node_path_cache(const NodePath &p_path) {
  985. const int *r = node_path_cache.getptr(p_path);
  986. if (r) {
  987. return *r;
  988. }
  989. last_path_id++;
  990. node_path_cache[p_path] = last_path_id;
  991. Array msg;
  992. msg.push_back(p_path);
  993. msg.push_back(last_path_id);
  994. _put_msg("scene:live_node_path", msg);
  995. return last_path_id;
  996. }
  997. int ScriptEditorDebugger::_get_res_path_cache(const String &p_path) {
  998. HashMap<String, int>::Iterator E = res_path_cache.find(p_path);
  999. if (E) {
  1000. return E->value;
  1001. }
  1002. last_path_id++;
  1003. res_path_cache[p_path] = last_path_id;
  1004. Array msg;
  1005. msg.push_back(p_path);
  1006. msg.push_back(last_path_id);
  1007. _put_msg("scene:live_res_path", msg);
  1008. return last_path_id;
  1009. }
  1010. void ScriptEditorDebugger::_method_changed(Object *p_base, const StringName &p_name, const Variant **p_args, int p_argcount) {
  1011. if (!p_base || !live_debug || !is_session_active() || !EditorNode::get_singleton()->get_edited_scene()) {
  1012. return;
  1013. }
  1014. Node *node = Object::cast_to<Node>(p_base);
  1015. for (int i = 0; i < p_argcount; i++) {
  1016. //no pointers, sorry
  1017. if (p_args[i]->get_type() == Variant::OBJECT || p_args[i]->get_type() == Variant::RID) {
  1018. return;
  1019. }
  1020. }
  1021. if (node) {
  1022. NodePath path = EditorNode::get_singleton()->get_edited_scene()->get_path_to(node);
  1023. int pathid = _get_node_path_cache(path);
  1024. Array msg;
  1025. msg.push_back(pathid);
  1026. msg.push_back(p_name);
  1027. for (int i = 0; i < p_argcount; i++) {
  1028. //no pointers, sorry
  1029. msg.push_back(*p_args[i]);
  1030. }
  1031. _put_msg("scene:live_node_call", msg);
  1032. return;
  1033. }
  1034. Resource *res = Object::cast_to<Resource>(p_base);
  1035. if (res && !res->get_path().is_empty()) {
  1036. String respath = res->get_path();
  1037. int pathid = _get_res_path_cache(respath);
  1038. Array msg;
  1039. msg.push_back(pathid);
  1040. msg.push_back(p_name);
  1041. for (int i = 0; i < p_argcount; i++) {
  1042. //no pointers, sorry
  1043. msg.push_back(*p_args[i]);
  1044. }
  1045. _put_msg("scene:live_res_call", msg);
  1046. return;
  1047. }
  1048. }
  1049. void ScriptEditorDebugger::_property_changed(Object *p_base, const StringName &p_property, const Variant &p_value) {
  1050. if (!p_base || !live_debug || !EditorNode::get_singleton()->get_edited_scene()) {
  1051. return;
  1052. }
  1053. Node *node = Object::cast_to<Node>(p_base);
  1054. if (node) {
  1055. NodePath path = EditorNode::get_singleton()->get_edited_scene()->get_path_to(node);
  1056. int pathid = _get_node_path_cache(path);
  1057. if (p_value.is_ref_counted()) {
  1058. Ref<Resource> res = p_value;
  1059. if (res.is_valid() && !res->get_path().is_empty()) {
  1060. Array msg;
  1061. msg.push_back(pathid);
  1062. msg.push_back(p_property);
  1063. msg.push_back(res->get_path());
  1064. _put_msg("scene:live_node_prop_res", msg);
  1065. }
  1066. } else {
  1067. Array msg;
  1068. msg.push_back(pathid);
  1069. msg.push_back(p_property);
  1070. msg.push_back(p_value);
  1071. _put_msg("scene:live_node_prop", msg);
  1072. }
  1073. return;
  1074. }
  1075. Resource *res = Object::cast_to<Resource>(p_base);
  1076. if (res && !res->get_path().is_empty()) {
  1077. String respath = res->get_path();
  1078. int pathid = _get_res_path_cache(respath);
  1079. if (p_value.is_ref_counted()) {
  1080. Ref<Resource> res2 = p_value;
  1081. if (res2.is_valid() && !res2->get_path().is_empty()) {
  1082. Array msg;
  1083. msg.push_back(pathid);
  1084. msg.push_back(p_property);
  1085. msg.push_back(res2->get_path());
  1086. _put_msg("scene:live_res_prop_res", msg);
  1087. }
  1088. } else {
  1089. Array msg;
  1090. msg.push_back(pathid);
  1091. msg.push_back(p_property);
  1092. msg.push_back(p_value);
  1093. _put_msg("scene:live_res_prop", msg);
  1094. }
  1095. return;
  1096. }
  1097. }
  1098. bool ScriptEditorDebugger::is_move_to_foreground() const {
  1099. return move_to_foreground;
  1100. }
  1101. void ScriptEditorDebugger::set_move_to_foreground(const bool &p_move_to_foreground) {
  1102. move_to_foreground = p_move_to_foreground;
  1103. }
  1104. String ScriptEditorDebugger::get_stack_script_file() const {
  1105. TreeItem *ti = stack_dump->get_selected();
  1106. if (!ti) {
  1107. return "";
  1108. }
  1109. Dictionary d = ti->get_metadata(0);
  1110. return d["file"];
  1111. }
  1112. int ScriptEditorDebugger::get_stack_script_line() const {
  1113. TreeItem *ti = stack_dump->get_selected();
  1114. if (!ti) {
  1115. return -1;
  1116. }
  1117. Dictionary d = ti->get_metadata(0);
  1118. return d["line"];
  1119. }
  1120. int ScriptEditorDebugger::get_stack_script_frame() const {
  1121. TreeItem *ti = stack_dump->get_selected();
  1122. if (!ti) {
  1123. return -1;
  1124. }
  1125. Dictionary d = ti->get_metadata(0);
  1126. return d["frame"];
  1127. }
  1128. bool ScriptEditorDebugger::request_stack_dump(const int &p_frame) {
  1129. ERR_FAIL_COND_V(!is_session_active() || p_frame < 0, false);
  1130. Array msg;
  1131. msg.push_back(p_frame);
  1132. _put_msg("get_stack_frame_vars", msg, debugging_thread_id);
  1133. return true;
  1134. }
  1135. void ScriptEditorDebugger::set_live_debugging(bool p_enable) {
  1136. live_debug = p_enable;
  1137. }
  1138. void ScriptEditorDebugger::_live_edit_set() {
  1139. if (!is_session_active() || !editor_remote_tree) {
  1140. return;
  1141. }
  1142. TreeItem *ti = editor_remote_tree->get_selected();
  1143. if (!ti) {
  1144. return;
  1145. }
  1146. String path;
  1147. while (ti) {
  1148. String lp = ti->get_text(0);
  1149. path = "/" + lp + path;
  1150. ti = ti->get_parent();
  1151. }
  1152. NodePath np = path;
  1153. EditorNode::get_editor_data().set_edited_scene_live_edit_root(np);
  1154. update_live_edit_root();
  1155. }
  1156. void ScriptEditorDebugger::_live_edit_clear() {
  1157. NodePath np = NodePath("/root");
  1158. EditorNode::get_editor_data().set_edited_scene_live_edit_root(np);
  1159. update_live_edit_root();
  1160. }
  1161. void ScriptEditorDebugger::update_live_edit_root() {
  1162. NodePath np = EditorNode::get_editor_data().get_edited_scene_live_edit_root();
  1163. Array msg;
  1164. msg.push_back(np);
  1165. if (EditorNode::get_singleton()->get_edited_scene()) {
  1166. msg.push_back(EditorNode::get_singleton()->get_edited_scene()->get_scene_file_path());
  1167. } else {
  1168. msg.push_back("");
  1169. }
  1170. _put_msg("scene:live_set_root", msg);
  1171. live_edit_root->set_text(np);
  1172. }
  1173. void ScriptEditorDebugger::live_debug_create_node(const NodePath &p_parent, const String &p_type, const String &p_name) {
  1174. if (live_debug) {
  1175. Array msg;
  1176. msg.push_back(p_parent);
  1177. msg.push_back(p_type);
  1178. msg.push_back(p_name);
  1179. _put_msg("scene:live_create_node", msg);
  1180. }
  1181. }
  1182. void ScriptEditorDebugger::live_debug_instantiate_node(const NodePath &p_parent, const String &p_path, const String &p_name) {
  1183. if (live_debug) {
  1184. Array msg;
  1185. msg.push_back(p_parent);
  1186. msg.push_back(p_path);
  1187. msg.push_back(p_name);
  1188. _put_msg("scene:live_instantiate_node", msg);
  1189. }
  1190. }
  1191. void ScriptEditorDebugger::live_debug_remove_node(const NodePath &p_at) {
  1192. if (live_debug) {
  1193. Array msg;
  1194. msg.push_back(p_at);
  1195. _put_msg("scene:live_remove_node", msg);
  1196. }
  1197. }
  1198. void ScriptEditorDebugger::live_debug_remove_and_keep_node(const NodePath &p_at, ObjectID p_keep_id) {
  1199. if (live_debug) {
  1200. Array msg;
  1201. msg.push_back(p_at);
  1202. msg.push_back(p_keep_id);
  1203. _put_msg("scene:live_remove_and_keep_node", msg);
  1204. }
  1205. }
  1206. void ScriptEditorDebugger::live_debug_restore_node(ObjectID p_id, const NodePath &p_at, int p_at_pos) {
  1207. if (live_debug) {
  1208. Array msg;
  1209. msg.push_back(p_id);
  1210. msg.push_back(p_at);
  1211. msg.push_back(p_at_pos);
  1212. _put_msg("scene:live_restore_node", msg);
  1213. }
  1214. }
  1215. void ScriptEditorDebugger::live_debug_duplicate_node(const NodePath &p_at, const String &p_new_name) {
  1216. if (live_debug) {
  1217. Array msg;
  1218. msg.push_back(p_at);
  1219. msg.push_back(p_new_name);
  1220. _put_msg("scene:live_duplicate_node", msg);
  1221. }
  1222. }
  1223. void ScriptEditorDebugger::live_debug_reparent_node(const NodePath &p_at, const NodePath &p_new_place, const String &p_new_name, int p_at_pos) {
  1224. if (live_debug) {
  1225. Array msg;
  1226. msg.push_back(p_at);
  1227. msg.push_back(p_new_place);
  1228. msg.push_back(p_new_name);
  1229. msg.push_back(p_at_pos);
  1230. _put_msg("scene:live_reparent_node", msg);
  1231. }
  1232. }
  1233. CameraOverride ScriptEditorDebugger::get_camera_override() const {
  1234. return camera_override;
  1235. }
  1236. void ScriptEditorDebugger::set_camera_override(CameraOverride p_override) {
  1237. if (p_override == CameraOverride::OVERRIDE_2D && camera_override != CameraOverride::OVERRIDE_2D) {
  1238. Array msg;
  1239. msg.push_back(true);
  1240. _put_msg("scene:override_camera_2D:set", msg);
  1241. } else if (p_override != CameraOverride::OVERRIDE_2D && camera_override == CameraOverride::OVERRIDE_2D) {
  1242. Array msg;
  1243. msg.push_back(false);
  1244. _put_msg("scene:override_camera_2D:set", msg);
  1245. } else if (p_override >= CameraOverride::OVERRIDE_3D_1 && camera_override < CameraOverride::OVERRIDE_3D_1) {
  1246. Array msg;
  1247. msg.push_back(true);
  1248. _put_msg("scene:override_camera_3D:set", msg);
  1249. } else if (p_override < CameraOverride::OVERRIDE_3D_1 && camera_override >= CameraOverride::OVERRIDE_3D_1) {
  1250. Array msg;
  1251. msg.push_back(false);
  1252. _put_msg("scene:override_camera_3D:set", msg);
  1253. }
  1254. camera_override = p_override;
  1255. }
  1256. void ScriptEditorDebugger::set_breakpoint(const String &p_path, int p_line, bool p_enabled) {
  1257. Array msg;
  1258. msg.push_back(p_path);
  1259. msg.push_back(p_line);
  1260. msg.push_back(p_enabled);
  1261. _put_msg("breakpoint", msg, debugging_thread_id != Thread::UNASSIGNED_ID ? debugging_thread_id : Thread::MAIN_ID);
  1262. TreeItem *path_item = breakpoints_tree->search_item_text(p_path);
  1263. if (path_item == nullptr) {
  1264. if (!p_enabled) {
  1265. return;
  1266. }
  1267. path_item = breakpoints_tree->create_item();
  1268. path_item->set_text(0, p_path);
  1269. }
  1270. int idx = 0;
  1271. TreeItem *breakpoint_item;
  1272. for (breakpoint_item = path_item->get_first_child(); breakpoint_item; breakpoint_item = breakpoint_item->get_next()) {
  1273. if ((int)breakpoint_item->get_meta("line") < p_line) {
  1274. idx++;
  1275. continue;
  1276. }
  1277. if ((int)breakpoint_item->get_meta("line") == p_line) {
  1278. break;
  1279. }
  1280. }
  1281. if (breakpoint_item == nullptr) {
  1282. if (!p_enabled) {
  1283. return;
  1284. }
  1285. breakpoint_item = breakpoints_tree->create_item(path_item, idx);
  1286. breakpoint_item->set_meta("line", p_line);
  1287. breakpoint_item->set_text(0, vformat(TTR("Line %d"), p_line));
  1288. return;
  1289. }
  1290. if (!p_enabled) {
  1291. path_item->remove_child(breakpoint_item);
  1292. if (path_item->get_first_child() == nullptr) {
  1293. breakpoints_tree->get_root()->remove_child(path_item);
  1294. }
  1295. }
  1296. }
  1297. void ScriptEditorDebugger::reload_scripts() {
  1298. _put_msg("reload_scripts", Array(), debugging_thread_id != Thread::UNASSIGNED_ID ? debugging_thread_id : Thread::MAIN_ID);
  1299. }
  1300. bool ScriptEditorDebugger::is_skip_breakpoints() {
  1301. return skip_breakpoints_value;
  1302. }
  1303. void ScriptEditorDebugger::_error_activated() {
  1304. TreeItem *selected = error_tree->get_selected();
  1305. if (!selected) {
  1306. return;
  1307. }
  1308. TreeItem *ci = selected->get_first_child();
  1309. if (ci) {
  1310. selected->set_collapsed(!selected->is_collapsed());
  1311. }
  1312. }
  1313. void ScriptEditorDebugger::_error_selected() {
  1314. TreeItem *selected = error_tree->get_selected();
  1315. if (!selected) {
  1316. return;
  1317. }
  1318. Array meta = selected->get_metadata(0);
  1319. if (meta.size() == 0) {
  1320. return;
  1321. }
  1322. emit_signal(SNAME("error_selected"), String(meta[0]), int(meta[1]));
  1323. }
  1324. void ScriptEditorDebugger::_expand_errors_list() {
  1325. TreeItem *root = error_tree->get_root();
  1326. if (!root) {
  1327. return;
  1328. }
  1329. TreeItem *item = root->get_first_child();
  1330. while (item) {
  1331. item->set_collapsed(false);
  1332. item = item->get_next();
  1333. }
  1334. }
  1335. void ScriptEditorDebugger::_collapse_errors_list() {
  1336. TreeItem *root = error_tree->get_root();
  1337. if (!root) {
  1338. return;
  1339. }
  1340. TreeItem *item = root->get_first_child();
  1341. while (item) {
  1342. item->set_collapsed(true);
  1343. item = item->get_next();
  1344. }
  1345. }
  1346. void ScriptEditorDebugger::_clear_errors_list() {
  1347. error_tree->clear();
  1348. error_count = 0;
  1349. warning_count = 0;
  1350. emit_signal(SNAME("errors_cleared"));
  1351. update_tabs();
  1352. expand_all_button->set_disabled(true);
  1353. collapse_all_button->set_disabled(true);
  1354. clear_button->set_disabled(true);
  1355. }
  1356. void ScriptEditorDebugger::_breakpoints_item_rmb_selected(const Vector2 &p_pos, MouseButton p_button) {
  1357. if (p_button != MouseButton::RIGHT) {
  1358. return;
  1359. }
  1360. breakpoints_menu->clear();
  1361. breakpoints_menu->set_size(Size2(1, 1));
  1362. const TreeItem *selected = breakpoints_tree->get_selected();
  1363. String file = selected->get_text(0);
  1364. if (selected->has_meta("line")) {
  1365. breakpoints_menu->add_icon_item(get_editor_theme_icon(SNAME("Remove")), TTR("Delete Breakpoint"), ACTION_DELETE_BREAKPOINT);
  1366. file = selected->get_parent()->get_text(0);
  1367. }
  1368. breakpoints_menu->add_icon_item(get_editor_theme_icon(SNAME("Remove")), TTR("Delete All Breakpoints in:") + " " + file, ACTION_DELETE_BREAKPOINTS_IN_FILE);
  1369. breakpoints_menu->add_icon_item(get_editor_theme_icon(SNAME("Remove")), TTR("Delete All Breakpoints"), ACTION_DELETE_ALL_BREAKPOINTS);
  1370. breakpoints_menu->set_position(get_screen_position() + get_local_mouse_position());
  1371. breakpoints_menu->popup();
  1372. }
  1373. // Right click on specific file(s) or folder(s).
  1374. void ScriptEditorDebugger::_error_tree_item_rmb_selected(const Vector2 &p_pos, MouseButton p_button) {
  1375. if (p_button != MouseButton::RIGHT) {
  1376. return;
  1377. }
  1378. item_menu->clear();
  1379. item_menu->reset_size();
  1380. if (error_tree->is_anything_selected()) {
  1381. item_menu->add_icon_item(get_editor_theme_icon(SNAME("ActionCopy")), TTR("Copy Error"), ACTION_COPY_ERROR);
  1382. item_menu->add_icon_item(get_editor_theme_icon(SNAME("ExternalLink")), TTR("Open C++ Source on GitHub"), ACTION_OPEN_SOURCE);
  1383. }
  1384. if (item_menu->get_item_count() > 0) {
  1385. item_menu->set_position(error_tree->get_screen_position() + p_pos);
  1386. item_menu->popup();
  1387. }
  1388. }
  1389. void ScriptEditorDebugger::_item_menu_id_pressed(int p_option) {
  1390. switch (p_option) {
  1391. case ACTION_COPY_ERROR: {
  1392. TreeItem *ti = error_tree->get_selected();
  1393. while (ti->get_parent() != error_tree->get_root()) {
  1394. ti = ti->get_parent();
  1395. }
  1396. String type;
  1397. if (ti->has_meta("_is_warning")) {
  1398. type = "W ";
  1399. } else if (ti->has_meta("_is_error")) {
  1400. type = "E ";
  1401. }
  1402. String text = ti->get_text(0) + " ";
  1403. int rpad_len = text.length();
  1404. text = type + text + ti->get_text(1) + "\n";
  1405. TreeItem *ci = ti->get_first_child();
  1406. while (ci) {
  1407. text += " " + ci->get_text(0).rpad(rpad_len) + ci->get_text(1) + "\n";
  1408. ci = ci->get_next();
  1409. }
  1410. DisplayServer::get_singleton()->clipboard_set(text);
  1411. } break;
  1412. case ACTION_OPEN_SOURCE: {
  1413. TreeItem *ti = error_tree->get_selected();
  1414. while (ti->get_parent() != error_tree->get_root()) {
  1415. ti = ti->get_parent();
  1416. }
  1417. // Find the child with the "C++ Source".
  1418. // It's not at a fixed position as "C++ Error" may come first.
  1419. TreeItem *ci = ti->get_first_child();
  1420. const String cpp_source = "<" + TTR("C++ Source") + ">";
  1421. while (ci) {
  1422. if (ci->get_text(0) == cpp_source) {
  1423. break;
  1424. }
  1425. ci = ci->get_next();
  1426. }
  1427. if (!ci) {
  1428. WARN_PRINT_ED("No C++ source reference is available for this error.");
  1429. return;
  1430. }
  1431. // Parse back the `file:line @ method()` string.
  1432. const Vector<String> file_line_number = ci->get_text(1).split("@")[0].strip_edges().split(":");
  1433. ERR_FAIL_COND_MSG(file_line_number.size() < 2, "Incorrect C++ source stack trace file:line format (please report).");
  1434. const String &file = file_line_number[0];
  1435. const int line_number = file_line_number[1].to_int();
  1436. // Construct a GitHub repository URL and open it in the user's default web browser.
  1437. // If the commit hash is available, use it for greater accuracy. Otherwise fall back to tagged release.
  1438. String git_ref = String(VERSION_HASH).is_empty() ? String(VERSION_NUMBER) + "-stable" : String(VERSION_HASH);
  1439. OS::get_singleton()->shell_open(vformat("https://github.com/godotengine/godot/blob/%s/%s#L%d",
  1440. git_ref, file, line_number));
  1441. } break;
  1442. case ACTION_DELETE_BREAKPOINT: {
  1443. const TreeItem *selected = breakpoints_tree->get_selected();
  1444. _set_breakpoint(selected->get_parent()->get_text(0), selected->get_meta("line"), false);
  1445. } break;
  1446. case ACTION_DELETE_BREAKPOINTS_IN_FILE: {
  1447. TreeItem *file_item = breakpoints_tree->get_selected();
  1448. if (file_item->has_meta("line")) {
  1449. file_item = file_item->get_parent();
  1450. }
  1451. // Store first else we will be removing as we loop.
  1452. List<int> lines;
  1453. for (TreeItem *breakpoint_item = file_item->get_first_child(); breakpoint_item; breakpoint_item = breakpoint_item->get_next()) {
  1454. lines.push_back(breakpoint_item->get_meta("line"));
  1455. }
  1456. for (const int &line : lines) {
  1457. _set_breakpoint(file_item->get_text(0), line, false);
  1458. }
  1459. } break;
  1460. case ACTION_DELETE_ALL_BREAKPOINTS: {
  1461. _clear_breakpoints();
  1462. } break;
  1463. }
  1464. }
  1465. void ScriptEditorDebugger::_tab_changed(int p_tab) {
  1466. if (tabs->get_tab_title(p_tab) == TTR("Video RAM")) {
  1467. // "Video RAM" tab was clicked, refresh the data it's displaying when entering the tab.
  1468. _video_mem_request();
  1469. }
  1470. }
  1471. void ScriptEditorDebugger::_bind_methods() {
  1472. ClassDB::bind_method(D_METHOD("live_debug_create_node"), &ScriptEditorDebugger::live_debug_create_node);
  1473. ClassDB::bind_method(D_METHOD("live_debug_instantiate_node"), &ScriptEditorDebugger::live_debug_instantiate_node);
  1474. ClassDB::bind_method(D_METHOD("live_debug_remove_node"), &ScriptEditorDebugger::live_debug_remove_node);
  1475. ClassDB::bind_method(D_METHOD("live_debug_remove_and_keep_node"), &ScriptEditorDebugger::live_debug_remove_and_keep_node);
  1476. ClassDB::bind_method(D_METHOD("live_debug_restore_node"), &ScriptEditorDebugger::live_debug_restore_node);
  1477. ClassDB::bind_method(D_METHOD("live_debug_duplicate_node"), &ScriptEditorDebugger::live_debug_duplicate_node);
  1478. ClassDB::bind_method(D_METHOD("live_debug_reparent_node"), &ScriptEditorDebugger::live_debug_reparent_node);
  1479. ClassDB::bind_method(D_METHOD("request_remote_object", "id"), &ScriptEditorDebugger::request_remote_object);
  1480. ClassDB::bind_method(D_METHOD("update_remote_object", "id", "property", "value"), &ScriptEditorDebugger::update_remote_object);
  1481. ADD_SIGNAL(MethodInfo("started"));
  1482. ADD_SIGNAL(MethodInfo("stopped"));
  1483. ADD_SIGNAL(MethodInfo("stop_requested"));
  1484. ADD_SIGNAL(MethodInfo("stack_frame_selected", PropertyInfo(Variant::INT, "frame")));
  1485. ADD_SIGNAL(MethodInfo("error_selected", PropertyInfo(Variant::INT, "error")));
  1486. ADD_SIGNAL(MethodInfo("breakpoint_selected", PropertyInfo("script"), PropertyInfo(Variant::INT, "line")));
  1487. ADD_SIGNAL(MethodInfo("set_execution", PropertyInfo("script"), PropertyInfo(Variant::INT, "line")));
  1488. ADD_SIGNAL(MethodInfo("clear_execution", PropertyInfo("script")));
  1489. ADD_SIGNAL(MethodInfo("breaked", PropertyInfo(Variant::BOOL, "reallydid"), PropertyInfo(Variant::BOOL, "can_debug"), PropertyInfo(Variant::STRING, "reason"), PropertyInfo(Variant::BOOL, "has_stackdump")));
  1490. ADD_SIGNAL(MethodInfo("remote_object_requested", PropertyInfo(Variant::INT, "id")));
  1491. ADD_SIGNAL(MethodInfo("remote_object_updated", PropertyInfo(Variant::INT, "id")));
  1492. ADD_SIGNAL(MethodInfo("remote_object_property_updated", PropertyInfo(Variant::INT, "id"), PropertyInfo(Variant::STRING, "property")));
  1493. ADD_SIGNAL(MethodInfo("remote_tree_updated"));
  1494. ADD_SIGNAL(MethodInfo("output"));
  1495. ADD_SIGNAL(MethodInfo("stack_dump", PropertyInfo(Variant::ARRAY, "stack_dump")));
  1496. ADD_SIGNAL(MethodInfo("stack_frame_vars", PropertyInfo(Variant::INT, "num_vars")));
  1497. ADD_SIGNAL(MethodInfo("stack_frame_var", PropertyInfo(Variant::ARRAY, "data")));
  1498. ADD_SIGNAL(MethodInfo("debug_data", PropertyInfo(Variant::STRING, "msg"), PropertyInfo(Variant::ARRAY, "data")));
  1499. ADD_SIGNAL(MethodInfo("set_breakpoint", PropertyInfo("script"), PropertyInfo(Variant::INT, "line"), PropertyInfo(Variant::BOOL, "enabled")));
  1500. ADD_SIGNAL(MethodInfo("clear_breakpoints"));
  1501. ADD_SIGNAL(MethodInfo("errors_cleared"));
  1502. }
  1503. void ScriptEditorDebugger::add_debugger_tab(Control *p_control) {
  1504. tabs->add_child(p_control);
  1505. }
  1506. void ScriptEditorDebugger::remove_debugger_tab(Control *p_control) {
  1507. int idx = tabs->get_tab_idx_from_control(p_control);
  1508. ERR_FAIL_COND(idx < 0);
  1509. p_control->queue_free();
  1510. }
  1511. int ScriptEditorDebugger::get_current_debugger_tab() const {
  1512. return tabs->get_current_tab();
  1513. }
  1514. void ScriptEditorDebugger::switch_to_debugger(int p_debugger_tab_idx) {
  1515. tabs->set_current_tab(p_debugger_tab_idx);
  1516. }
  1517. void ScriptEditorDebugger::send_message(const String &p_message, const Array &p_args) {
  1518. _put_msg(p_message, p_args);
  1519. }
  1520. void ScriptEditorDebugger::toggle_profiler(const String &p_profiler, bool p_enable, const Array &p_data) {
  1521. Array msg_data;
  1522. msg_data.push_back(p_enable);
  1523. msg_data.append_array(p_data);
  1524. _put_msg("profiler:" + p_profiler, msg_data);
  1525. }
  1526. ScriptEditorDebugger::ScriptEditorDebugger() {
  1527. tabs = memnew(TabContainer);
  1528. add_child(tabs);
  1529. tabs->connect("tab_changed", callable_mp(this, &ScriptEditorDebugger::_tab_changed));
  1530. InspectorDock::get_inspector_singleton()->connect("object_id_selected", callable_mp(this, &ScriptEditorDebugger::_remote_object_selected));
  1531. { //debugger
  1532. VBoxContainer *vbc = memnew(VBoxContainer);
  1533. vbc->set_name(TTR("Stack Trace"));
  1534. Control *dbg = vbc;
  1535. HBoxContainer *hbc = memnew(HBoxContainer);
  1536. vbc->add_child(hbc);
  1537. reason = memnew(Label);
  1538. reason->set_text("");
  1539. hbc->add_child(reason);
  1540. reason->set_h_size_flags(SIZE_EXPAND_FILL);
  1541. reason->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
  1542. reason->set_max_lines_visible(3);
  1543. reason->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  1544. hbc->add_child(memnew(VSeparator));
  1545. skip_breakpoints = memnew(Button);
  1546. skip_breakpoints->set_theme_type_variation("FlatButton");
  1547. hbc->add_child(skip_breakpoints);
  1548. skip_breakpoints->set_tooltip_text(TTR("Skip Breakpoints"));
  1549. skip_breakpoints->connect("pressed", callable_mp(this, &ScriptEditorDebugger::debug_skip_breakpoints));
  1550. hbc->add_child(memnew(VSeparator));
  1551. copy = memnew(Button);
  1552. copy->set_theme_type_variation("FlatButton");
  1553. hbc->add_child(copy);
  1554. copy->set_tooltip_text(TTR("Copy Error"));
  1555. copy->connect("pressed", callable_mp(this, &ScriptEditorDebugger::debug_copy));
  1556. hbc->add_child(memnew(VSeparator));
  1557. step = memnew(Button);
  1558. step->set_theme_type_variation("FlatButton");
  1559. hbc->add_child(step);
  1560. step->set_tooltip_text(TTR("Step Into"));
  1561. step->set_shortcut(ED_GET_SHORTCUT("debugger/step_into"));
  1562. step->connect("pressed", callable_mp(this, &ScriptEditorDebugger::debug_step));
  1563. next = memnew(Button);
  1564. next->set_theme_type_variation("FlatButton");
  1565. hbc->add_child(next);
  1566. next->set_tooltip_text(TTR("Step Over"));
  1567. next->set_shortcut(ED_GET_SHORTCUT("debugger/step_over"));
  1568. next->connect("pressed", callable_mp(this, &ScriptEditorDebugger::debug_next));
  1569. hbc->add_child(memnew(VSeparator));
  1570. dobreak = memnew(Button);
  1571. dobreak->set_theme_type_variation("FlatButton");
  1572. hbc->add_child(dobreak);
  1573. dobreak->set_tooltip_text(TTR("Break"));
  1574. dobreak->set_shortcut(ED_GET_SHORTCUT("debugger/break"));
  1575. dobreak->connect("pressed", callable_mp(this, &ScriptEditorDebugger::debug_break));
  1576. docontinue = memnew(Button);
  1577. docontinue->set_theme_type_variation("FlatButton");
  1578. hbc->add_child(docontinue);
  1579. docontinue->set_tooltip_text(TTR("Continue"));
  1580. docontinue->set_shortcut(ED_GET_SHORTCUT("debugger/continue"));
  1581. docontinue->connect("pressed", callable_mp(this, &ScriptEditorDebugger::debug_continue));
  1582. HSplitContainer *parent_sc = memnew(HSplitContainer);
  1583. vbc->add_child(parent_sc);
  1584. parent_sc->set_v_size_flags(SIZE_EXPAND_FILL);
  1585. parent_sc->set_split_offset(500 * EDSCALE);
  1586. HSplitContainer *sc = memnew(HSplitContainer);
  1587. sc->set_v_size_flags(SIZE_EXPAND_FILL);
  1588. sc->set_h_size_flags(SIZE_EXPAND_FILL);
  1589. parent_sc->add_child(sc);
  1590. VBoxContainer *stack_vb = memnew(VBoxContainer);
  1591. stack_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  1592. sc->add_child(stack_vb);
  1593. HBoxContainer *thread_hb = memnew(HBoxContainer);
  1594. stack_vb->add_child(thread_hb);
  1595. thread_hb->add_child(memnew(Label(TTR("Thread:"))));
  1596. threads = memnew(OptionButton);
  1597. thread_hb->add_child(threads);
  1598. threads->set_h_size_flags(SIZE_EXPAND_FILL);
  1599. threads->connect("item_selected", callable_mp(this, &ScriptEditorDebugger::_select_thread));
  1600. stack_dump = memnew(Tree);
  1601. stack_dump->set_allow_reselect(true);
  1602. stack_dump->set_columns(1);
  1603. stack_dump->set_column_titles_visible(true);
  1604. stack_dump->set_column_title(0, TTR("Stack Frames"));
  1605. stack_dump->set_hide_root(true);
  1606. stack_dump->set_v_size_flags(SIZE_EXPAND_FILL);
  1607. stack_dump->connect("cell_selected", callable_mp(this, &ScriptEditorDebugger::_stack_dump_frame_selected));
  1608. stack_vb->add_child(stack_dump);
  1609. VBoxContainer *inspector_vbox = memnew(VBoxContainer);
  1610. inspector_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  1611. sc->add_child(inspector_vbox);
  1612. HBoxContainer *tools_hb = memnew(HBoxContainer);
  1613. inspector_vbox->add_child(tools_hb);
  1614. search = memnew(LineEdit);
  1615. search->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1616. search->set_placeholder(TTR("Filter Stack Variables"));
  1617. search->set_clear_button_enabled(true);
  1618. tools_hb->add_child(search);
  1619. inspector = memnew(EditorDebuggerInspector);
  1620. inspector->set_h_size_flags(SIZE_EXPAND_FILL);
  1621. inspector->set_v_size_flags(SIZE_EXPAND_FILL);
  1622. inspector->set_property_name_style(EditorPropertyNameProcessor::STYLE_RAW);
  1623. inspector->set_read_only(true);
  1624. inspector->connect("object_selected", callable_mp(this, &ScriptEditorDebugger::_remote_object_selected));
  1625. inspector->connect("object_edited", callable_mp(this, &ScriptEditorDebugger::_remote_object_edited));
  1626. inspector->connect("object_property_updated", callable_mp(this, &ScriptEditorDebugger::_remote_object_property_updated));
  1627. inspector->register_text_enter(search);
  1628. inspector->set_use_filter(true);
  1629. inspector_vbox->add_child(inspector);
  1630. breakpoints_tree = memnew(Tree);
  1631. breakpoints_tree->set_h_size_flags(SIZE_EXPAND_FILL);
  1632. breakpoints_tree->set_column_titles_visible(true);
  1633. breakpoints_tree->set_column_title(0, TTR("Breakpoints"));
  1634. breakpoints_tree->set_allow_reselect(true);
  1635. breakpoints_tree->set_allow_rmb_select(true);
  1636. breakpoints_tree->set_hide_root(true);
  1637. breakpoints_tree->connect("item_mouse_selected", callable_mp(this, &ScriptEditorDebugger::_breakpoints_item_rmb_selected));
  1638. breakpoints_tree->create_item();
  1639. parent_sc->add_child(breakpoints_tree);
  1640. tabs->add_child(dbg);
  1641. breakpoints_menu = memnew(PopupMenu);
  1642. breakpoints_menu->connect("id_pressed", callable_mp(this, &ScriptEditorDebugger::_item_menu_id_pressed));
  1643. breakpoints_tree->add_child(breakpoints_menu);
  1644. }
  1645. { //errors
  1646. errors_tab = memnew(VBoxContainer);
  1647. errors_tab->set_name(TTR("Errors"));
  1648. HBoxContainer *error_hbox = memnew(HBoxContainer);
  1649. errors_tab->add_child(error_hbox);
  1650. expand_all_button = memnew(Button);
  1651. expand_all_button->set_text(TTR("Expand All"));
  1652. expand_all_button->set_disabled(true);
  1653. expand_all_button->connect("pressed", callable_mp(this, &ScriptEditorDebugger::_expand_errors_list));
  1654. error_hbox->add_child(expand_all_button);
  1655. collapse_all_button = memnew(Button);
  1656. collapse_all_button->set_text(TTR("Collapse All"));
  1657. collapse_all_button->set_disabled(true);
  1658. collapse_all_button->connect("pressed", callable_mp(this, &ScriptEditorDebugger::_collapse_errors_list));
  1659. error_hbox->add_child(collapse_all_button);
  1660. Control *space = memnew(Control);
  1661. space->set_h_size_flags(SIZE_EXPAND_FILL);
  1662. error_hbox->add_child(space);
  1663. clear_button = memnew(Button);
  1664. clear_button->set_text(TTR("Clear"));
  1665. clear_button->set_h_size_flags(0);
  1666. clear_button->set_disabled(true);
  1667. clear_button->connect("pressed", callable_mp(this, &ScriptEditorDebugger::_clear_errors_list));
  1668. error_hbox->add_child(clear_button);
  1669. error_tree = memnew(Tree);
  1670. error_tree->set_columns(2);
  1671. error_tree->set_column_expand(0, false);
  1672. error_tree->set_column_custom_minimum_width(0, 140);
  1673. error_tree->set_column_clip_content(0, true);
  1674. error_tree->set_column_expand(1, true);
  1675. error_tree->set_column_clip_content(1, true);
  1676. error_tree->set_select_mode(Tree::SELECT_ROW);
  1677. error_tree->set_hide_root(true);
  1678. error_tree->set_v_size_flags(SIZE_EXPAND_FILL);
  1679. error_tree->set_allow_rmb_select(true);
  1680. error_tree->set_allow_reselect(true);
  1681. error_tree->connect("item_mouse_selected", callable_mp(this, &ScriptEditorDebugger::_error_tree_item_rmb_selected));
  1682. errors_tab->add_child(error_tree);
  1683. item_menu = memnew(PopupMenu);
  1684. item_menu->connect("id_pressed", callable_mp(this, &ScriptEditorDebugger::_item_menu_id_pressed));
  1685. error_tree->add_child(item_menu);
  1686. tabs->add_child(errors_tab);
  1687. }
  1688. { // File dialog
  1689. file_dialog = memnew(EditorFileDialog);
  1690. file_dialog->connect("file_selected", callable_mp(this, &ScriptEditorDebugger::_file_selected));
  1691. add_child(file_dialog);
  1692. }
  1693. { //profiler
  1694. profiler = memnew(EditorProfiler);
  1695. profiler->set_name(TTR("Profiler"));
  1696. tabs->add_child(profiler);
  1697. profiler->connect("enable_profiling", callable_mp(this, &ScriptEditorDebugger::_profiler_activate).bind(PROFILER_SCRIPTS_SERVERS));
  1698. profiler->connect("break_request", callable_mp(this, &ScriptEditorDebugger::_profiler_seeked));
  1699. }
  1700. { //frame profiler
  1701. visual_profiler = memnew(EditorVisualProfiler);
  1702. visual_profiler->set_name(TTR("Visual Profiler"));
  1703. tabs->add_child(visual_profiler);
  1704. visual_profiler->connect("enable_profiling", callable_mp(this, &ScriptEditorDebugger::_profiler_activate).bind(PROFILER_VISUAL));
  1705. }
  1706. { //monitors
  1707. performance_profiler = memnew(EditorPerformanceProfiler);
  1708. tabs->add_child(performance_profiler);
  1709. }
  1710. { //vmem inspect
  1711. VBoxContainer *vmem_vb = memnew(VBoxContainer);
  1712. HBoxContainer *vmem_hb = memnew(HBoxContainer);
  1713. Label *vmlb = memnew(Label(TTR("List of Video Memory Usage by Resource:") + " "));
  1714. vmlb->set_theme_type_variation("HeaderSmall");
  1715. vmlb->set_h_size_flags(SIZE_EXPAND_FILL);
  1716. vmem_hb->add_child(vmlb);
  1717. vmem_hb->add_child(memnew(Label(TTR("Total:") + " ")));
  1718. vmem_total = memnew(LineEdit);
  1719. vmem_total->set_editable(false);
  1720. vmem_total->set_custom_minimum_size(Size2(100, 0) * EDSCALE);
  1721. vmem_hb->add_child(vmem_total);
  1722. vmem_refresh = memnew(Button);
  1723. vmem_refresh->set_theme_type_variation("FlatButton");
  1724. vmem_hb->add_child(vmem_refresh);
  1725. vmem_export = memnew(Button);
  1726. vmem_export->set_theme_type_variation("FlatButton");
  1727. vmem_export->set_tooltip_text(TTR("Export list to a CSV file"));
  1728. vmem_hb->add_child(vmem_export);
  1729. vmem_vb->add_child(vmem_hb);
  1730. vmem_refresh->connect("pressed", callable_mp(this, &ScriptEditorDebugger::_video_mem_request));
  1731. vmem_export->connect("pressed", callable_mp(this, &ScriptEditorDebugger::_video_mem_export));
  1732. VBoxContainer *vmmc = memnew(VBoxContainer);
  1733. vmem_tree = memnew(Tree);
  1734. vmem_tree->set_v_size_flags(SIZE_EXPAND_FILL);
  1735. vmem_tree->set_h_size_flags(SIZE_EXPAND_FILL);
  1736. vmmc->add_child(vmem_tree);
  1737. vmmc->set_v_size_flags(SIZE_EXPAND_FILL);
  1738. vmem_vb->add_child(vmmc);
  1739. vmem_vb->set_name(TTR("Video RAM"));
  1740. vmem_tree->set_columns(4);
  1741. vmem_tree->set_column_titles_visible(true);
  1742. vmem_tree->set_column_title(0, TTR("Resource Path"));
  1743. vmem_tree->set_column_expand(0, true);
  1744. vmem_tree->set_column_expand(1, false);
  1745. vmem_tree->set_column_title(1, TTR("Type"));
  1746. vmem_tree->set_column_custom_minimum_width(1, 100 * EDSCALE);
  1747. vmem_tree->set_column_expand(2, false);
  1748. vmem_tree->set_column_title(2, TTR("Format"));
  1749. vmem_tree->set_column_custom_minimum_width(2, 150 * EDSCALE);
  1750. vmem_tree->set_column_expand(3, false);
  1751. vmem_tree->set_column_title(3, TTR("Usage"));
  1752. vmem_tree->set_column_custom_minimum_width(3, 80 * EDSCALE);
  1753. vmem_tree->set_hide_root(true);
  1754. tabs->add_child(vmem_vb);
  1755. }
  1756. { // misc
  1757. VBoxContainer *misc = memnew(VBoxContainer);
  1758. misc->set_name(TTR("Misc"));
  1759. tabs->add_child(misc);
  1760. GridContainer *info_left = memnew(GridContainer);
  1761. info_left->set_columns(2);
  1762. misc->add_child(info_left);
  1763. clicked_ctrl = memnew(LineEdit);
  1764. clicked_ctrl->set_editable(false);
  1765. clicked_ctrl->set_h_size_flags(SIZE_EXPAND_FILL);
  1766. info_left->add_child(memnew(Label(TTR("Clicked Control:"))));
  1767. info_left->add_child(clicked_ctrl);
  1768. clicked_ctrl_type = memnew(LineEdit);
  1769. clicked_ctrl_type->set_editable(false);
  1770. info_left->add_child(memnew(Label(TTR("Clicked Control Type:"))));
  1771. info_left->add_child(clicked_ctrl_type);
  1772. scene_tree = memnew(SceneDebuggerTree);
  1773. live_edit_root = memnew(LineEdit);
  1774. live_edit_root->set_editable(false);
  1775. live_edit_root->set_h_size_flags(SIZE_EXPAND_FILL);
  1776. {
  1777. HBoxContainer *lehb = memnew(HBoxContainer);
  1778. Label *l = memnew(Label(TTR("Live Edit Root:")));
  1779. info_left->add_child(l);
  1780. lehb->add_child(live_edit_root);
  1781. le_set = memnew(Button(TTR("Set From Tree")));
  1782. lehb->add_child(le_set);
  1783. le_clear = memnew(Button(TTR("Clear")));
  1784. lehb->add_child(le_clear);
  1785. info_left->add_child(lehb);
  1786. }
  1787. misc->add_child(memnew(VSeparator));
  1788. HBoxContainer *buttons = memnew(HBoxContainer);
  1789. export_csv = memnew(Button(TTR("Export measures as CSV")));
  1790. export_csv->connect("pressed", callable_mp(this, &ScriptEditorDebugger::_export_csv));
  1791. buttons->add_child(export_csv);
  1792. misc->add_child(buttons);
  1793. }
  1794. msgdialog = memnew(AcceptDialog);
  1795. add_child(msgdialog);
  1796. live_debug = true;
  1797. camera_override = CameraOverride::OVERRIDE_NONE;
  1798. last_path_id = false;
  1799. error_count = 0;
  1800. warning_count = 0;
  1801. _update_buttons_state();
  1802. }
  1803. ScriptEditorDebugger::~ScriptEditorDebugger() {
  1804. if (peer.is_valid()) {
  1805. peer->close();
  1806. peer.unref();
  1807. }
  1808. memdelete(scene_tree);
  1809. }