script_editor_debugger.cpp 65 KB

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