script_editor_debugger.cpp 66 KB

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