script_editor_debugger.cpp 74 KB

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