script_editor_debugger.cpp 66 KB

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