script_editor_debugger.cpp 59 KB

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