script_editor_debugger.cpp 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998
  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 "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.is_empty()) {
  78. return;
  79. }
  80. DisplayServer::get_singleton()->clipboard_set(msg);
  81. }
  82. void ScriptEditorDebugger::debug_skip_breakpoints() {
  83. skip_breakpoints_value = !skip_breakpoints_value;
  84. if (skip_breakpoints_value) {
  85. skip_breakpoints->set_icon(get_theme_icon(SNAME("DebugSkipBreakpointsOn"), SNAME("EditorIcons")));
  86. } else {
  87. skip_breakpoints->set_icon(get_theme_icon(SNAME("DebugSkipBreakpointsOff"), SNAME("EditorIcons")));
  88. }
  89. Array msg;
  90. msg.push_back(skip_breakpoints_value);
  91. _put_msg("set_skip_breakpoints", msg);
  92. }
  93. void ScriptEditorDebugger::debug_next() {
  94. ERR_FAIL_COND(!breaked);
  95. _put_msg("next", Array());
  96. _clear_execution();
  97. }
  98. void ScriptEditorDebugger::debug_step() {
  99. ERR_FAIL_COND(!breaked);
  100. _put_msg("step", Array());
  101. _clear_execution();
  102. }
  103. void ScriptEditorDebugger::debug_break() {
  104. ERR_FAIL_COND(breaked);
  105. _put_msg("break", Array());
  106. }
  107. void ScriptEditorDebugger::debug_continue() {
  108. ERR_FAIL_COND(!breaked);
  109. // Allow focus stealing only if we actually run this client for security.
  110. if (remote_pid && EditorNode::get_singleton()->has_child_process(remote_pid)) {
  111. DisplayServer::get_singleton()->enable_for_stealing_focus(remote_pid);
  112. }
  113. _clear_execution();
  114. _put_msg("continue", Array());
  115. }
  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->remove_theme_style_override("panel");
  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.is_empty()) {
  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(SNAME("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(SNAME("stack_frame_vars"), p_data[0]);
  353. } else if (p_msg == "stack_frame_var") {
  354. inspector->add_stack_variable(p_data);
  355. emit_signal(SNAME("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(SNAME("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_alignment(0, HORIZONTAL_ALIGNMENT_LEFT);
  434. const Color color = get_theme_color(oe.warning ? SNAME("warning_color") : SNAME("error_color"), SNAME("Editor"));
  435. error->set_custom_color(0, color);
  436. error->set_custom_color(1, color);
  437. String error_title;
  438. if (oe.callstack.size() > 0) {
  439. // If available, use the script's stack in the error title.
  440. error_title = oe.callstack[oe.callstack.size() - 1].func + ": ";
  441. } else if (!oe.source_func.is_empty()) {
  442. // Otherwise try to use the C++ source function.
  443. error_title += oe.source_func + ": ";
  444. }
  445. // If we have a (custom) error message, use it as title, and add a C++ Error
  446. // item with the original error condition.
  447. error_title += oe.error_descr.is_empty() ? oe.error : oe.error_descr;
  448. error->set_text(1, error_title);
  449. tooltip += " " + error_title + "\n";
  450. if (!oe.error_descr.is_empty()) {
  451. // Add item for C++ error condition.
  452. TreeItem *cpp_cond = error_tree->create_item(error);
  453. cpp_cond->set_text(0, "<" + TTR("C++ Error") + ">");
  454. cpp_cond->set_text(1, oe.error);
  455. cpp_cond->set_text_alignment(0, HORIZONTAL_ALIGNMENT_LEFT);
  456. tooltip += TTR("C++ Error:") + " " + oe.error + "\n";
  457. if (source_is_project_file) {
  458. cpp_cond->set_metadata(0, source_meta);
  459. }
  460. }
  461. Vector<uint8_t> v;
  462. v.resize(100);
  463. // Source of the error.
  464. String source_txt = (source_is_project_file ? oe.source_file.get_file() : oe.source_file) + ":" + itos(oe.source_line);
  465. if (!oe.source_func.is_empty()) {
  466. source_txt += " @ " + oe.source_func + "()";
  467. }
  468. TreeItem *cpp_source = error_tree->create_item(error);
  469. cpp_source->set_text(0, "<" + (source_is_project_file ? TTR("Source") : TTR("C++ Source")) + ">");
  470. cpp_source->set_text(1, source_txt);
  471. cpp_source->set_text_alignment(0, HORIZONTAL_ALIGNMENT_LEFT);
  472. tooltip += (source_is_project_file ? TTR("Source:") : TTR("C++ Source:")) + " " + source_txt + "\n";
  473. // Set metadata to highlight error line in scripts.
  474. if (source_is_project_file) {
  475. error->set_metadata(0, source_meta);
  476. cpp_source->set_metadata(0, source_meta);
  477. }
  478. // Format stack trace.
  479. // stack_items_count is the number of elements to parse, with 3 items per frame
  480. // of the stack trace (script, method, line).
  481. const ScriptLanguage::StackInfo *infos = oe.callstack.ptr();
  482. for (unsigned int i = 0; i < (unsigned int)oe.callstack.size(); i++) {
  483. TreeItem *stack_trace = error_tree->create_item(error);
  484. Array meta;
  485. meta.push_back(infos[i].file);
  486. meta.push_back(infos[i].line);
  487. stack_trace->set_metadata(0, meta);
  488. if (i == 0) {
  489. stack_trace->set_text(0, "<" + TTR("Stack Trace") + ">");
  490. stack_trace->set_text_alignment(0, HORIZONTAL_ALIGNMENT_LEFT);
  491. error->set_metadata(0, meta);
  492. tooltip += TTR("Stack Trace:") + "\n";
  493. }
  494. String frame_txt = infos[i].file.get_file() + ":" + itos(infos[i].line) + " @ " + infos[i].func + "()";
  495. tooltip += frame_txt + "\n";
  496. stack_trace->set_text(1, frame_txt);
  497. }
  498. error->set_tooltip(0, tooltip);
  499. error->set_tooltip(1, tooltip);
  500. if (warning_count == 0 && error_count == 0) {
  501. expand_all_button->set_disabled(false);
  502. collapse_all_button->set_disabled(false);
  503. clear_button->set_disabled(false);
  504. }
  505. if (oe.warning) {
  506. warning_count++;
  507. } else {
  508. error_count++;
  509. }
  510. } else if (p_msg == "servers:function_signature") {
  511. // Cache a profiler signature.
  512. DebuggerMarshalls::ScriptFunctionSignature sig;
  513. sig.deserialize(p_data);
  514. profiler_signature[sig.id] = sig.name;
  515. } else if (p_msg == "servers:profile_frame" || p_msg == "servers:profile_total") {
  516. EditorProfiler::Metric metric;
  517. DebuggerMarshalls::ServersProfilerFrame frame;
  518. frame.deserialize(p_data);
  519. metric.valid = true;
  520. metric.frame_number = frame.frame_number;
  521. metric.frame_time = frame.frame_time;
  522. metric.idle_time = frame.idle_time;
  523. metric.physics_time = frame.physics_time;
  524. metric.physics_frame_time = frame.physics_frame_time;
  525. if (frame.servers.size()) {
  526. EditorProfiler::Metric::Category frame_time;
  527. frame_time.signature = "category_frame_time";
  528. frame_time.name = "Frame Time";
  529. frame_time.total_time = metric.frame_time;
  530. EditorProfiler::Metric::Category::Item item;
  531. item.calls = 1;
  532. item.line = 0;
  533. item.name = "Physics Time";
  534. item.total = metric.physics_time;
  535. item.self = item.total;
  536. item.signature = "physics_time";
  537. frame_time.items.push_back(item);
  538. item.name = "Idle Time";
  539. item.total = metric.idle_time;
  540. item.self = item.total;
  541. item.signature = "idle_time";
  542. frame_time.items.push_back(item);
  543. item.name = "Physics Frame Time";
  544. item.total = metric.physics_frame_time;
  545. item.self = item.total;
  546. item.signature = "physics_frame_time";
  547. frame_time.items.push_back(item);
  548. metric.categories.push_back(frame_time);
  549. }
  550. for (int i = 0; i < frame.servers.size(); i++) {
  551. const DebuggerMarshalls::ServerInfo &srv = frame.servers[i];
  552. EditorProfiler::Metric::Category c;
  553. const String name = srv.name;
  554. c.name = name.capitalize();
  555. c.items.resize(srv.functions.size());
  556. c.total_time = 0;
  557. c.signature = "categ::" + name;
  558. for (int j = 0; j < srv.functions.size(); j++) {
  559. EditorProfiler::Metric::Category::Item item;
  560. item.calls = 1;
  561. item.line = 0;
  562. item.name = srv.functions[j].name;
  563. item.self = srv.functions[j].time;
  564. item.total = item.self;
  565. item.signature = "categ::" + name + "::" + item.name;
  566. item.name = item.name.capitalize();
  567. c.total_time += item.total;
  568. c.items.write[j] = item;
  569. }
  570. metric.categories.push_back(c);
  571. }
  572. EditorProfiler::Metric::Category funcs;
  573. funcs.total_time = frame.script_time;
  574. funcs.items.resize(frame.script_functions.size());
  575. funcs.name = "Script Functions";
  576. funcs.signature = "script_functions";
  577. for (int i = 0; i < frame.script_functions.size(); i++) {
  578. int signature = frame.script_functions[i].sig_id;
  579. int calls = frame.script_functions[i].call_count;
  580. float total = frame.script_functions[i].total_time;
  581. float self = frame.script_functions[i].self_time;
  582. EditorProfiler::Metric::Category::Item item;
  583. if (profiler_signature.has(signature)) {
  584. item.signature = profiler_signature[signature];
  585. String name = profiler_signature[signature];
  586. Vector<String> strings = name.split("::");
  587. if (strings.size() == 3) {
  588. item.name = strings[2];
  589. item.script = strings[0];
  590. item.line = strings[1].to_int();
  591. } else if (strings.size() == 4) { //Built-in scripts have an :: in their name
  592. item.name = strings[3];
  593. item.script = strings[0] + "::" + strings[1];
  594. item.line = strings[2].to_int();
  595. }
  596. } else {
  597. item.name = "SigErr " + itos(signature);
  598. }
  599. item.calls = calls;
  600. item.self = self;
  601. item.total = total;
  602. funcs.items.write[i] = item;
  603. }
  604. metric.categories.push_back(funcs);
  605. if (p_msg == "servers:profile_frame") {
  606. profiler->add_frame_metric(metric, false);
  607. } else {
  608. profiler->add_frame_metric(metric, true);
  609. }
  610. } else if (p_msg == "network:profile_frame") {
  611. DebuggerMarshalls::NetworkProfilerFrame frame;
  612. frame.deserialize(p_data);
  613. for (int i = 0; i < frame.infos.size(); i++) {
  614. network_profiler->add_node_frame_data(frame.infos[i]);
  615. }
  616. } else if (p_msg == "network:bandwidth") {
  617. ERR_FAIL_COND(p_data.size() < 2);
  618. network_profiler->set_bandwidth(p_data[0], p_data[1]);
  619. } else if (p_msg == "request_quit") {
  620. emit_signal(SNAME("stop_requested"));
  621. _stop_and_notify();
  622. } else if (p_msg == "performance:profile_names") {
  623. Vector<StringName> monitors;
  624. monitors.resize(p_data.size());
  625. for (int i = 0; i < p_data.size(); i++) {
  626. ERR_FAIL_COND(p_data[i].get_type() != Variant::STRING_NAME);
  627. monitors.set(i, p_data[i]);
  628. }
  629. performance_profiler->update_monitors(monitors);
  630. } else {
  631. int colon_index = p_msg.find_char(':');
  632. ERR_FAIL_COND_MSG(colon_index < 1, "Invalid message received");
  633. bool parsed = false;
  634. const String cap = p_msg.substr(0, colon_index);
  635. Map<StringName, Callable>::Element *element = captures.find(cap);
  636. if (element) {
  637. Callable &c = element->value();
  638. ERR_FAIL_COND_MSG(c.is_null(), "Invalid callable registered: " + cap);
  639. Variant cmd = p_msg.substr(colon_index + 1), data = p_data;
  640. const Variant *args[2] = { &cmd, &data };
  641. Variant retval;
  642. Callable::CallError err;
  643. c.call(args, 2, retval, err);
  644. ERR_FAIL_COND_MSG(err.error != Callable::CallError::CALL_OK, "Error calling 'capture' to callable: " + Variant::get_callable_error_text(c, args, 2, err));
  645. ERR_FAIL_COND_MSG(retval.get_type() != Variant::BOOL, "Error calling 'capture' to callable: " + String(c) + ". Return type is not bool.");
  646. parsed = retval;
  647. }
  648. if (!parsed) {
  649. WARN_PRINT("unknown message " + p_msg);
  650. }
  651. }
  652. }
  653. void ScriptEditorDebugger::_set_reason_text(const String &p_reason, MessageType p_type) {
  654. switch (p_type) {
  655. case MESSAGE_ERROR:
  656. reason->add_theme_color_override("font_color", get_theme_color(SNAME("error_color"), SNAME("Editor")));
  657. break;
  658. case MESSAGE_WARNING:
  659. reason->add_theme_color_override("font_color", get_theme_color(SNAME("warning_color"), SNAME("Editor")));
  660. break;
  661. default:
  662. reason->add_theme_color_override("font_color", get_theme_color(SNAME("success_color"), SNAME("Editor")));
  663. }
  664. reason->set_text(p_reason);
  665. reason->set_tooltip(p_reason.word_wrap(80));
  666. }
  667. void ScriptEditorDebugger::_notification(int p_what) {
  668. switch (p_what) {
  669. case NOTIFICATION_ENTER_TREE: {
  670. skip_breakpoints->set_icon(get_theme_icon(SNAME("DebugSkipBreakpointsOff"), SNAME("EditorIcons")));
  671. copy->set_icon(get_theme_icon(SNAME("ActionCopy"), SNAME("EditorIcons")));
  672. step->set_icon(get_theme_icon(SNAME("DebugStep"), SNAME("EditorIcons")));
  673. next->set_icon(get_theme_icon(SNAME("DebugNext"), SNAME("EditorIcons")));
  674. dobreak->set_icon(get_theme_icon(SNAME("Pause"), SNAME("EditorIcons")));
  675. docontinue->set_icon(get_theme_icon(SNAME("DebugContinue"), SNAME("EditorIcons")));
  676. le_set->connect("pressed", callable_mp(this, &ScriptEditorDebugger::_live_edit_set));
  677. le_clear->connect("pressed", callable_mp(this, &ScriptEditorDebugger::_live_edit_clear));
  678. error_tree->connect("item_selected", callable_mp(this, &ScriptEditorDebugger::_error_selected));
  679. error_tree->connect("item_activated", callable_mp(this, &ScriptEditorDebugger::_error_activated));
  680. breakpoints_tree->connect("item_activated", callable_mp(this, &ScriptEditorDebugger::_breakpoint_tree_clicked));
  681. vmem_refresh->set_icon(get_theme_icon(SNAME("Reload"), SNAME("EditorIcons")));
  682. vmem_export->set_icon(get_theme_icon(SNAME("Save"), SNAME("EditorIcons")));
  683. search->set_right_icon(get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  684. reason->add_theme_color_override("font_color", get_theme_color(SNAME("error_color"), SNAME("Editor")));
  685. } break;
  686. case NOTIFICATION_PROCESS: {
  687. if (is_session_active()) {
  688. peer->poll();
  689. if (camera_override == CameraOverride::OVERRIDE_2D) {
  690. CanvasItemEditor *editor = CanvasItemEditor::get_singleton();
  691. Dictionary state = editor->get_state();
  692. float zoom = state["zoom"];
  693. Point2 offset = state["ofs"];
  694. Transform2D transform;
  695. transform.scale_basis(Size2(zoom, zoom));
  696. transform.elements[2] = -offset * zoom;
  697. Array msg;
  698. msg.push_back(transform);
  699. _put_msg("scene:override_camera_2D:transform", msg);
  700. } else if (camera_override >= CameraOverride::OVERRIDE_3D_1) {
  701. int viewport_idx = camera_override - CameraOverride::OVERRIDE_3D_1;
  702. Node3DEditorViewport *viewport = Node3DEditor::get_singleton()->get_editor_viewport(viewport_idx);
  703. Camera3D *const cam = viewport->get_camera_3d();
  704. Array msg;
  705. msg.push_back(cam->get_camera_transform());
  706. if (cam->get_projection() == Camera3D::PROJECTION_ORTHOGONAL) {
  707. msg.push_back(false);
  708. msg.push_back(cam->get_size());
  709. } else {
  710. msg.push_back(true);
  711. msg.push_back(cam->get_fov());
  712. }
  713. msg.push_back(cam->get_near());
  714. msg.push_back(cam->get_far());
  715. _put_msg("scene:override_camera_3D:transform", msg);
  716. }
  717. }
  718. const uint64_t until = OS::get_singleton()->get_ticks_msec() + 20;
  719. while (peer.is_valid() && peer->has_message()) {
  720. Array arr = peer->get_message();
  721. if (arr.size() != 2 || arr[0].get_type() != Variant::STRING || arr[1].get_type() != Variant::ARRAY) {
  722. _stop_and_notify();
  723. ERR_FAIL_MSG("Invalid message format received from peer");
  724. }
  725. _parse_message(arr[0], arr[1]);
  726. if (OS::get_singleton()->get_ticks_msec() > until) {
  727. break;
  728. }
  729. }
  730. if (!is_session_active()) {
  731. _stop_and_notify();
  732. break;
  733. };
  734. } break;
  735. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  736. if (tabs->has_theme_stylebox_override("panel")) {
  737. tabs->add_theme_style_override("panel", editor->get_gui_base()->get_theme_stylebox(SNAME("DebuggerPanel"), SNAME("EditorStyles")));
  738. }
  739. copy->set_icon(get_theme_icon(SNAME("ActionCopy"), SNAME("EditorIcons")));
  740. step->set_icon(get_theme_icon(SNAME("DebugStep"), SNAME("EditorIcons")));
  741. next->set_icon(get_theme_icon(SNAME("DebugNext"), SNAME("EditorIcons")));
  742. dobreak->set_icon(get_theme_icon(SNAME("Pause"), SNAME("EditorIcons")));
  743. docontinue->set_icon(get_theme_icon(SNAME("DebugContinue"), SNAME("EditorIcons")));
  744. vmem_refresh->set_icon(get_theme_icon(SNAME("Reload"), SNAME("EditorIcons")));
  745. vmem_export->set_icon(get_theme_icon(SNAME("Save"), SNAME("EditorIcons")));
  746. search->set_right_icon(get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  747. } break;
  748. }
  749. }
  750. void ScriptEditorDebugger::_clear_execution() {
  751. TreeItem *ti = stack_dump->get_selected();
  752. if (!ti) {
  753. return;
  754. }
  755. Dictionary d = ti->get_metadata(0);
  756. stack_script = ResourceLoader::load(d["file"]);
  757. emit_signal(SNAME("clear_execution"), stack_script);
  758. stack_script.unref();
  759. stack_dump->clear();
  760. inspector->clear_stack_variables();
  761. }
  762. void ScriptEditorDebugger::_set_breakpoint(const String &p_file, const int &p_line, const bool &p_enabled) {
  763. Ref<Script> script = ResourceLoader::load(p_file);
  764. emit_signal("set_breakpoint", script, p_line - 1, p_enabled);
  765. script.unref();
  766. }
  767. void ScriptEditorDebugger::_clear_breakpoints() {
  768. emit_signal("clear_breakpoints");
  769. }
  770. void ScriptEditorDebugger::_breakpoint_tree_clicked() {
  771. TreeItem *selected = breakpoints_tree->get_selected();
  772. if (selected->has_meta("line")) {
  773. emit_signal(SNAME("breakpoint_selected"), selected->get_parent()->get_text(0), int(selected->get_meta("line")));
  774. }
  775. }
  776. void ScriptEditorDebugger::start(Ref<RemoteDebuggerPeer> p_peer) {
  777. _clear_errors_list();
  778. stop();
  779. peer = p_peer;
  780. ERR_FAIL_COND(p_peer.is_null());
  781. performance_profiler->reset();
  782. set_process(true);
  783. breaked = false;
  784. can_debug = true;
  785. camera_override = CameraOverride::OVERRIDE_NONE;
  786. tabs->set_current_tab(0);
  787. _set_reason_text(TTR("Debug session started."), MESSAGE_SUCCESS);
  788. _update_buttons_state();
  789. emit_signal(SNAME("started"));
  790. }
  791. void ScriptEditorDebugger::_update_buttons_state() {
  792. const bool active = is_session_active();
  793. const bool has_editor_tree = active && editor_remote_tree && editor_remote_tree->get_selected();
  794. vmem_refresh->set_disabled(!active);
  795. step->set_disabled(!active || !breaked || !can_debug);
  796. next->set_disabled(!active || !breaked || !can_debug);
  797. copy->set_disabled(!active || !breaked);
  798. docontinue->set_disabled(!active || !breaked);
  799. dobreak->set_disabled(!active || breaked);
  800. le_clear->set_disabled(!active);
  801. le_set->set_disabled(!has_editor_tree);
  802. }
  803. void ScriptEditorDebugger::_stop_and_notify() {
  804. stop();
  805. emit_signal(SNAME("stopped"));
  806. _set_reason_text(TTR("Debug session closed."), MESSAGE_WARNING);
  807. }
  808. void ScriptEditorDebugger::stop() {
  809. set_process(false);
  810. breaked = false;
  811. can_debug = false;
  812. remote_pid = 0;
  813. _clear_execution();
  814. inspector->clear_cache();
  815. if (peer.is_valid()) {
  816. peer->close();
  817. peer.unref();
  818. reason->set_text("");
  819. reason->set_tooltip("");
  820. }
  821. node_path_cache.clear();
  822. res_path_cache.clear();
  823. profiler_signature.clear();
  824. inspector->edit(nullptr);
  825. _update_buttons_state();
  826. }
  827. void ScriptEditorDebugger::_profiler_activate(bool p_enable, int p_type) {
  828. Array data;
  829. data.push_back(p_enable);
  830. switch (p_type) {
  831. case PROFILER_NETWORK:
  832. _put_msg("profiler:network", data);
  833. break;
  834. case PROFILER_VISUAL:
  835. _put_msg("profiler:visual", data);
  836. break;
  837. case PROFILER_SCRIPTS_SERVERS:
  838. if (p_enable) {
  839. // Clear old script signatures. (should we move all this into the profiler?)
  840. profiler_signature.clear();
  841. // Add max funcs options to request.
  842. Array opts;
  843. int max_funcs = EditorSettings::get_singleton()->get("debugger/profiler_frame_max_functions");
  844. opts.push_back(CLAMP(max_funcs, 16, 512));
  845. data.push_back(opts);
  846. }
  847. _put_msg("profiler:servers", data);
  848. break;
  849. default:
  850. ERR_FAIL_MSG("Invalid profiler type");
  851. }
  852. }
  853. void ScriptEditorDebugger::_profiler_seeked() {
  854. if (breaked) {
  855. return;
  856. }
  857. debug_break();
  858. }
  859. void ScriptEditorDebugger::_stack_dump_frame_selected() {
  860. emit_signal(SNAME("stack_frame_selected"));
  861. int frame = get_stack_script_frame();
  862. if (!request_stack_dump(frame)) {
  863. inspector->edit(nullptr);
  864. }
  865. }
  866. void ScriptEditorDebugger::_export_csv() {
  867. file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  868. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  869. file_dialog_purpose = SAVE_MONITORS_CSV;
  870. file_dialog->popup_file_dialog();
  871. }
  872. String ScriptEditorDebugger::get_var_value(const String &p_var) const {
  873. if (!breaked) {
  874. return String();
  875. }
  876. return inspector->get_stack_variable(p_var);
  877. }
  878. int ScriptEditorDebugger::_get_node_path_cache(const NodePath &p_path) {
  879. const int *r = node_path_cache.getptr(p_path);
  880. if (r) {
  881. return *r;
  882. }
  883. last_path_id++;
  884. node_path_cache[p_path] = last_path_id;
  885. Array msg;
  886. msg.push_back(p_path);
  887. msg.push_back(last_path_id);
  888. _put_msg("scene:live_node_path", msg);
  889. return last_path_id;
  890. }
  891. int ScriptEditorDebugger::_get_res_path_cache(const String &p_path) {
  892. Map<String, int>::Element *E = res_path_cache.find(p_path);
  893. if (E) {
  894. return E->get();
  895. }
  896. last_path_id++;
  897. res_path_cache[p_path] = last_path_id;
  898. Array msg;
  899. msg.push_back(p_path);
  900. msg.push_back(last_path_id);
  901. _put_msg("scene:live_res_path", msg);
  902. return last_path_id;
  903. }
  904. void ScriptEditorDebugger::_method_changed(Object *p_base, const StringName &p_name, VARIANT_ARG_DECLARE) {
  905. if (!p_base || !live_debug || !is_session_active() || !editor->get_edited_scene()) {
  906. return;
  907. }
  908. Node *node = Object::cast_to<Node>(p_base);
  909. VARIANT_ARGPTRS
  910. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  911. //no pointers, sorry
  912. if (argptr[i] && (argptr[i]->get_type() == Variant::OBJECT || argptr[i]->get_type() == Variant::RID)) {
  913. return;
  914. }
  915. }
  916. if (node) {
  917. NodePath path = editor->get_edited_scene()->get_path_to(node);
  918. int pathid = _get_node_path_cache(path);
  919. Array msg;
  920. msg.push_back(pathid);
  921. msg.push_back(p_name);
  922. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  923. //no pointers, sorry
  924. msg.push_back(*argptr[i]);
  925. }
  926. _put_msg("scene:live_node_call", msg);
  927. return;
  928. }
  929. Resource *res = Object::cast_to<Resource>(p_base);
  930. if (res && !res->get_path().is_empty()) {
  931. String respath = res->get_path();
  932. int pathid = _get_res_path_cache(respath);
  933. Array msg;
  934. msg.push_back(pathid);
  935. msg.push_back(p_name);
  936. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  937. //no pointers, sorry
  938. msg.push_back(*argptr[i]);
  939. }
  940. _put_msg("scene:live_res_call", msg);
  941. return;
  942. }
  943. }
  944. void ScriptEditorDebugger::_property_changed(Object *p_base, const StringName &p_property, const Variant &p_value) {
  945. if (!p_base || !live_debug || !editor->get_edited_scene()) {
  946. return;
  947. }
  948. Node *node = Object::cast_to<Node>(p_base);
  949. if (node) {
  950. NodePath path = editor->get_edited_scene()->get_path_to(node);
  951. int pathid = _get_node_path_cache(path);
  952. if (p_value.is_ref()) {
  953. Ref<Resource> res = p_value;
  954. if (res.is_valid() && !res->get_path().is_empty()) {
  955. Array msg;
  956. msg.push_back(pathid);
  957. msg.push_back(p_property);
  958. msg.push_back(res->get_path());
  959. _put_msg("scene:live_node_prop_res", msg);
  960. }
  961. } else {
  962. Array msg;
  963. msg.push_back(pathid);
  964. msg.push_back(p_property);
  965. msg.push_back(p_value);
  966. _put_msg("scene:live_node_prop", msg);
  967. }
  968. return;
  969. }
  970. Resource *res = Object::cast_to<Resource>(p_base);
  971. if (res && !res->get_path().is_empty()) {
  972. String respath = res->get_path();
  973. int pathid = _get_res_path_cache(respath);
  974. if (p_value.is_ref()) {
  975. Ref<Resource> res2 = p_value;
  976. if (res2.is_valid() && !res2->get_path().is_empty()) {
  977. Array msg;
  978. msg.push_back(pathid);
  979. msg.push_back(p_property);
  980. msg.push_back(res2->get_path());
  981. _put_msg("scene:live_res_prop_res", msg);
  982. }
  983. } else {
  984. Array msg;
  985. msg.push_back(pathid);
  986. msg.push_back(p_property);
  987. msg.push_back(p_value);
  988. _put_msg("scene:live_res_prop", msg);
  989. }
  990. return;
  991. }
  992. }
  993. bool ScriptEditorDebugger::is_move_to_foreground() const {
  994. return move_to_foreground;
  995. }
  996. void ScriptEditorDebugger::set_move_to_foreground(const bool &p_move_to_foreground) {
  997. move_to_foreground = p_move_to_foreground;
  998. }
  999. String ScriptEditorDebugger::get_stack_script_file() const {
  1000. TreeItem *ti = stack_dump->get_selected();
  1001. if (!ti) {
  1002. return "";
  1003. }
  1004. Dictionary d = ti->get_metadata(0);
  1005. return d["file"];
  1006. }
  1007. int ScriptEditorDebugger::get_stack_script_line() const {
  1008. TreeItem *ti = stack_dump->get_selected();
  1009. if (!ti) {
  1010. return -1;
  1011. }
  1012. Dictionary d = ti->get_metadata(0);
  1013. return d["line"];
  1014. }
  1015. int ScriptEditorDebugger::get_stack_script_frame() const {
  1016. TreeItem *ti = stack_dump->get_selected();
  1017. if (!ti) {
  1018. return -1;
  1019. }
  1020. Dictionary d = ti->get_metadata(0);
  1021. return d["frame"];
  1022. }
  1023. bool ScriptEditorDebugger::request_stack_dump(const int &p_frame) {
  1024. ERR_FAIL_COND_V(!is_session_active() || p_frame < 0, false);
  1025. Array msg;
  1026. msg.push_back(p_frame);
  1027. _put_msg("get_stack_frame_vars", msg);
  1028. return true;
  1029. }
  1030. void ScriptEditorDebugger::set_live_debugging(bool p_enable) {
  1031. live_debug = p_enable;
  1032. }
  1033. void ScriptEditorDebugger::_live_edit_set() {
  1034. if (!is_session_active() || !editor_remote_tree) {
  1035. return;
  1036. }
  1037. TreeItem *ti = editor_remote_tree->get_selected();
  1038. if (!ti) {
  1039. return;
  1040. }
  1041. String path;
  1042. while (ti) {
  1043. String lp = ti->get_text(0);
  1044. path = "/" + lp + path;
  1045. ti = ti->get_parent();
  1046. }
  1047. NodePath np = path;
  1048. editor->get_editor_data().set_edited_scene_live_edit_root(np);
  1049. update_live_edit_root();
  1050. }
  1051. void ScriptEditorDebugger::_live_edit_clear() {
  1052. NodePath np = NodePath("/root");
  1053. editor->get_editor_data().set_edited_scene_live_edit_root(np);
  1054. update_live_edit_root();
  1055. }
  1056. void ScriptEditorDebugger::update_live_edit_root() {
  1057. NodePath np = editor->get_editor_data().get_edited_scene_live_edit_root();
  1058. Array msg;
  1059. msg.push_back(np);
  1060. if (editor->get_edited_scene()) {
  1061. msg.push_back(editor->get_edited_scene()->get_scene_file_path());
  1062. } else {
  1063. msg.push_back("");
  1064. }
  1065. _put_msg("scene:live_set_root", msg);
  1066. live_edit_root->set_text(np);
  1067. }
  1068. void ScriptEditorDebugger::live_debug_create_node(const NodePath &p_parent, const String &p_type, const String &p_name) {
  1069. if (live_debug) {
  1070. Array msg;
  1071. msg.push_back(p_parent);
  1072. msg.push_back(p_type);
  1073. msg.push_back(p_name);
  1074. _put_msg("scene:live_create_node", msg);
  1075. }
  1076. }
  1077. void ScriptEditorDebugger::live_debug_instance_node(const NodePath &p_parent, const String &p_path, const String &p_name) {
  1078. if (live_debug) {
  1079. Array msg;
  1080. msg.push_back(p_parent);
  1081. msg.push_back(p_path);
  1082. msg.push_back(p_name);
  1083. _put_msg("scene:live_instance_node", msg);
  1084. }
  1085. }
  1086. void ScriptEditorDebugger::live_debug_remove_node(const NodePath &p_at) {
  1087. if (live_debug) {
  1088. Array msg;
  1089. msg.push_back(p_at);
  1090. _put_msg("scene:live_remove_node", msg);
  1091. }
  1092. }
  1093. void ScriptEditorDebugger::live_debug_remove_and_keep_node(const NodePath &p_at, ObjectID p_keep_id) {
  1094. if (live_debug) {
  1095. Array msg;
  1096. msg.push_back(p_at);
  1097. msg.push_back(p_keep_id);
  1098. _put_msg("scene:live_remove_and_keep_node", msg);
  1099. }
  1100. }
  1101. void ScriptEditorDebugger::live_debug_restore_node(ObjectID p_id, const NodePath &p_at, int p_at_pos) {
  1102. if (live_debug) {
  1103. Array msg;
  1104. msg.push_back(p_id);
  1105. msg.push_back(p_at);
  1106. msg.push_back(p_at_pos);
  1107. _put_msg("scene:live_restore_node", msg);
  1108. }
  1109. }
  1110. void ScriptEditorDebugger::live_debug_duplicate_node(const NodePath &p_at, const String &p_new_name) {
  1111. if (live_debug) {
  1112. Array msg;
  1113. msg.push_back(p_at);
  1114. msg.push_back(p_new_name);
  1115. _put_msg("scene:live_duplicate_node", msg);
  1116. }
  1117. }
  1118. void ScriptEditorDebugger::live_debug_reparent_node(const NodePath &p_at, const NodePath &p_new_place, const String &p_new_name, int p_at_pos) {
  1119. if (live_debug) {
  1120. Array msg;
  1121. msg.push_back(p_at);
  1122. msg.push_back(p_new_place);
  1123. msg.push_back(p_new_name);
  1124. msg.push_back(p_at_pos);
  1125. _put_msg("scene:live_reparent_node", msg);
  1126. }
  1127. }
  1128. CameraOverride ScriptEditorDebugger::get_camera_override() const {
  1129. return camera_override;
  1130. }
  1131. void ScriptEditorDebugger::set_camera_override(CameraOverride p_override) {
  1132. if (p_override == CameraOverride::OVERRIDE_2D && camera_override != CameraOverride::OVERRIDE_2D) {
  1133. Array msg;
  1134. msg.push_back(true);
  1135. _put_msg("scene:override_camera_2D:set", msg);
  1136. } else if (p_override != CameraOverride::OVERRIDE_2D && camera_override == CameraOverride::OVERRIDE_2D) {
  1137. Array msg;
  1138. msg.push_back(false);
  1139. _put_msg("scene:override_camera_2D:set", msg);
  1140. } else if (p_override >= CameraOverride::OVERRIDE_3D_1 && camera_override < CameraOverride::OVERRIDE_3D_1) {
  1141. Array msg;
  1142. msg.push_back(true);
  1143. _put_msg("scene:override_camera_3D:set", msg);
  1144. } else if (p_override < CameraOverride::OVERRIDE_3D_1 && camera_override >= CameraOverride::OVERRIDE_3D_1) {
  1145. Array msg;
  1146. msg.push_back(false);
  1147. _put_msg("scene:override_camera_3D:set", msg);
  1148. }
  1149. camera_override = p_override;
  1150. }
  1151. void ScriptEditorDebugger::set_breakpoint(const String &p_path, int p_line, bool p_enabled) {
  1152. Array msg;
  1153. msg.push_back(p_path);
  1154. msg.push_back(p_line);
  1155. msg.push_back(p_enabled);
  1156. _put_msg("breakpoint", msg);
  1157. TreeItem *path_item = breakpoints_tree->search_item_text(p_path);
  1158. if (path_item == nullptr) {
  1159. if (!p_enabled) {
  1160. return;
  1161. }
  1162. path_item = breakpoints_tree->create_item();
  1163. path_item->set_text(0, p_path);
  1164. }
  1165. int idx = 0;
  1166. TreeItem *breakpoint_item;
  1167. for (breakpoint_item = path_item->get_first_child(); breakpoint_item; breakpoint_item = breakpoint_item->get_next()) {
  1168. if ((int)breakpoint_item->get_meta("line") < p_line) {
  1169. idx++;
  1170. continue;
  1171. }
  1172. if ((int)breakpoint_item->get_meta("line") == p_line) {
  1173. break;
  1174. }
  1175. }
  1176. if (breakpoint_item == nullptr) {
  1177. if (!p_enabled) {
  1178. return;
  1179. }
  1180. breakpoint_item = breakpoints_tree->create_item(path_item, idx);
  1181. breakpoint_item->set_meta("line", p_line);
  1182. breakpoint_item->set_text(0, vformat(TTR("Line %d"), p_line));
  1183. return;
  1184. }
  1185. if (!p_enabled) {
  1186. path_item->remove_child(breakpoint_item);
  1187. if (path_item->get_first_child() == nullptr) {
  1188. breakpoints_tree->get_root()->remove_child(path_item);
  1189. }
  1190. }
  1191. }
  1192. void ScriptEditorDebugger::reload_scripts() {
  1193. _put_msg("reload_scripts", Array());
  1194. }
  1195. bool ScriptEditorDebugger::is_skip_breakpoints() {
  1196. return skip_breakpoints_value;
  1197. }
  1198. void ScriptEditorDebugger::_error_activated() {
  1199. TreeItem *selected = error_tree->get_selected();
  1200. TreeItem *ci = selected->get_first_child();
  1201. if (ci) {
  1202. selected->set_collapsed(!selected->is_collapsed());
  1203. }
  1204. }
  1205. void ScriptEditorDebugger::_error_selected() {
  1206. TreeItem *selected = error_tree->get_selected();
  1207. Array meta = selected->get_metadata(0);
  1208. if (meta.size() == 0) {
  1209. return;
  1210. }
  1211. emit_signal(SNAME("error_selected"), String(meta[0]), int(meta[1]));
  1212. }
  1213. void ScriptEditorDebugger::_expand_errors_list() {
  1214. TreeItem *root = error_tree->get_root();
  1215. if (!root) {
  1216. return;
  1217. }
  1218. TreeItem *item = root->get_first_child();
  1219. while (item) {
  1220. item->set_collapsed(false);
  1221. item = item->get_next();
  1222. }
  1223. }
  1224. void ScriptEditorDebugger::_collapse_errors_list() {
  1225. TreeItem *root = error_tree->get_root();
  1226. if (!root) {
  1227. return;
  1228. }
  1229. TreeItem *item = root->get_first_child();
  1230. while (item) {
  1231. item->set_collapsed(true);
  1232. item = item->get_next();
  1233. }
  1234. }
  1235. void ScriptEditorDebugger::_clear_errors_list() {
  1236. error_tree->clear();
  1237. error_count = 0;
  1238. warning_count = 0;
  1239. update_tabs();
  1240. expand_all_button->set_disabled(true);
  1241. collapse_all_button->set_disabled(true);
  1242. clear_button->set_disabled(true);
  1243. }
  1244. void ScriptEditorDebugger::_breakpoints_item_rmb_selected(const Vector2 &p_pos) {
  1245. breakpoints_menu->clear();
  1246. breakpoints_menu->set_size(Size2(1, 1));
  1247. const TreeItem *selected = breakpoints_tree->get_selected();
  1248. String file = selected->get_text(0);
  1249. if (selected->has_meta("line")) {
  1250. breakpoints_menu->add_icon_item(get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")), TTR("Delete Breakpoint"), ACTION_DELETE_BREAKPOINT);
  1251. file = selected->get_parent()->get_text(0);
  1252. }
  1253. breakpoints_menu->add_icon_item(get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")), TTR("Delete All Breakpoints in: ") + file, ACTION_DELETE_BREAKPOINTS_IN_FILE);
  1254. breakpoints_menu->add_icon_item(get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")), TTR("Delete All Breakpoints"), ACTION_DELETE_ALL_BREAKPOINTS);
  1255. breakpoints_menu->set_position(breakpoints_tree->get_global_position() + p_pos);
  1256. breakpoints_menu->popup();
  1257. }
  1258. // Right click on specific file(s) or folder(s).
  1259. void ScriptEditorDebugger::_error_tree_item_rmb_selected(const Vector2 &p_pos) {
  1260. item_menu->clear();
  1261. item_menu->reset_size();
  1262. if (error_tree->is_anything_selected()) {
  1263. item_menu->add_icon_item(get_theme_icon(SNAME("ActionCopy"), SNAME("EditorIcons")), TTR("Copy Error"), ACTION_COPY_ERROR);
  1264. item_menu->add_icon_item(get_theme_icon(SNAME("Instance"), SNAME("EditorIcons")), TTR("Open C++ Source on GitHub"), ACTION_OPEN_SOURCE);
  1265. }
  1266. if (item_menu->get_item_count() > 0) {
  1267. item_menu->set_position(error_tree->get_screen_position() + p_pos);
  1268. item_menu->popup();
  1269. }
  1270. }
  1271. void ScriptEditorDebugger::_item_menu_id_pressed(int p_option) {
  1272. switch (p_option) {
  1273. case ACTION_COPY_ERROR: {
  1274. TreeItem *ti = error_tree->get_selected();
  1275. while (ti->get_parent() != error_tree->get_root()) {
  1276. ti = ti->get_parent();
  1277. }
  1278. String type;
  1279. if (ti->get_icon(0) == get_theme_icon(SNAME("Warning"), SNAME("EditorIcons"))) {
  1280. type = "W ";
  1281. } else if (ti->get_icon(0) == get_theme_icon(SNAME("Error"), SNAME("EditorIcons"))) {
  1282. type = "E ";
  1283. }
  1284. String text = ti->get_text(0) + " ";
  1285. int rpad_len = text.length();
  1286. text = type + text + ti->get_text(1) + "\n";
  1287. TreeItem *ci = ti->get_first_child();
  1288. while (ci) {
  1289. text += " " + ci->get_text(0).rpad(rpad_len) + ci->get_text(1) + "\n";
  1290. ci = ci->get_next();
  1291. }
  1292. DisplayServer::get_singleton()->clipboard_set(text);
  1293. } break;
  1294. case ACTION_OPEN_SOURCE: {
  1295. TreeItem *ti = error_tree->get_selected();
  1296. while (ti->get_parent() != error_tree->get_root()) {
  1297. ti = ti->get_parent();
  1298. }
  1299. // We only need the first child here (C++ source stack trace).
  1300. TreeItem *ci = ti->get_first_child();
  1301. // Parse back the `file:line @ method()` string.
  1302. const Vector<String> file_line_number = ci->get_text(1).split("@")[0].strip_edges().split(":");
  1303. ERR_FAIL_COND_MSG(file_line_number.size() < 2, "Incorrect C++ source stack trace file:line format (please report).");
  1304. const String file = file_line_number[0];
  1305. const int line_number = file_line_number[1].to_int();
  1306. // Construct a GitHub repository URL and open it in the user's default web browser.
  1307. if (String(VERSION_HASH).length() >= 1) {
  1308. // Git commit hash information available; use it for greater accuracy, including for development versions.
  1309. OS::get_singleton()->shell_open(vformat("https://github.com/godotengine/godot/blob/%s/%s#L%d",
  1310. VERSION_HASH,
  1311. file,
  1312. line_number));
  1313. } else {
  1314. // Git commit hash information unavailable; fall back to tagged releases.
  1315. OS::get_singleton()->shell_open(vformat("https://github.com/godotengine/godot/blob/%s-stable/%s#L%d",
  1316. VERSION_NUMBER,
  1317. file,
  1318. line_number));
  1319. }
  1320. } break;
  1321. case ACTION_DELETE_BREAKPOINT: {
  1322. const TreeItem *selected = breakpoints_tree->get_selected();
  1323. _set_breakpoint(selected->get_parent()->get_text(0), selected->get_meta("line"), false);
  1324. } break;
  1325. case ACTION_DELETE_BREAKPOINTS_IN_FILE: {
  1326. TreeItem *file_item = breakpoints_tree->get_selected();
  1327. if (file_item->has_meta("line")) {
  1328. file_item = file_item->get_parent();
  1329. }
  1330. // Store first else we will be removing as we loop.
  1331. List<int> lines;
  1332. for (TreeItem *breakpoint_item = file_item->get_first_child(); breakpoint_item; breakpoint_item = breakpoint_item->get_next()) {
  1333. lines.push_back(breakpoint_item->get_meta("line"));
  1334. }
  1335. for (const int &line : lines) {
  1336. _set_breakpoint(file_item->get_text(0), line, false);
  1337. }
  1338. } break;
  1339. case ACTION_DELETE_ALL_BREAKPOINTS: {
  1340. _clear_breakpoints();
  1341. } break;
  1342. }
  1343. }
  1344. void ScriptEditorDebugger::_tab_changed(int p_tab) {
  1345. if (tabs->get_tab_title(p_tab) == TTR("Video RAM")) {
  1346. // "Video RAM" tab was clicked, refresh the data it's displaying when entering the tab.
  1347. _video_mem_request();
  1348. }
  1349. }
  1350. void ScriptEditorDebugger::_bind_methods() {
  1351. ClassDB::bind_method(D_METHOD("live_debug_create_node"), &ScriptEditorDebugger::live_debug_create_node);
  1352. ClassDB::bind_method(D_METHOD("live_debug_instance_node"), &ScriptEditorDebugger::live_debug_instance_node);
  1353. ClassDB::bind_method(D_METHOD("live_debug_remove_node"), &ScriptEditorDebugger::live_debug_remove_node);
  1354. ClassDB::bind_method(D_METHOD("live_debug_remove_and_keep_node"), &ScriptEditorDebugger::live_debug_remove_and_keep_node);
  1355. ClassDB::bind_method(D_METHOD("live_debug_restore_node"), &ScriptEditorDebugger::live_debug_restore_node);
  1356. ClassDB::bind_method(D_METHOD("live_debug_duplicate_node"), &ScriptEditorDebugger::live_debug_duplicate_node);
  1357. ClassDB::bind_method(D_METHOD("live_debug_reparent_node"), &ScriptEditorDebugger::live_debug_reparent_node);
  1358. ClassDB::bind_method(D_METHOD("request_remote_object", "id"), &ScriptEditorDebugger::request_remote_object);
  1359. ClassDB::bind_method(D_METHOD("update_remote_object", "id", "property", "value"), &ScriptEditorDebugger::update_remote_object);
  1360. ADD_SIGNAL(MethodInfo("started"));
  1361. ADD_SIGNAL(MethodInfo("stopped"));
  1362. ADD_SIGNAL(MethodInfo("stop_requested"));
  1363. ADD_SIGNAL(MethodInfo("stack_frame_selected", PropertyInfo(Variant::INT, "frame")));
  1364. ADD_SIGNAL(MethodInfo("error_selected", PropertyInfo(Variant::INT, "error")));
  1365. ADD_SIGNAL(MethodInfo("breakpoint_selected", PropertyInfo("script"), PropertyInfo(Variant::INT, "line")));
  1366. ADD_SIGNAL(MethodInfo("set_execution", PropertyInfo("script"), PropertyInfo(Variant::INT, "line")));
  1367. ADD_SIGNAL(MethodInfo("clear_execution", PropertyInfo("script")));
  1368. ADD_SIGNAL(MethodInfo("breaked", PropertyInfo(Variant::BOOL, "reallydid"), PropertyInfo(Variant::BOOL, "can_debug"), PropertyInfo(Variant::STRING, "reason"), PropertyInfo(Variant::BOOL, "has_stackdump")));
  1369. ADD_SIGNAL(MethodInfo("remote_object_requested", PropertyInfo(Variant::INT, "id")));
  1370. ADD_SIGNAL(MethodInfo("remote_object_updated", PropertyInfo(Variant::INT, "id")));
  1371. ADD_SIGNAL(MethodInfo("remote_object_property_updated", PropertyInfo(Variant::INT, "id"), PropertyInfo(Variant::STRING, "property")));
  1372. ADD_SIGNAL(MethodInfo("remote_tree_updated"));
  1373. ADD_SIGNAL(MethodInfo("output"));
  1374. ADD_SIGNAL(MethodInfo("stack_dump", PropertyInfo(Variant::ARRAY, "stack_dump")));
  1375. ADD_SIGNAL(MethodInfo("stack_frame_vars", PropertyInfo(Variant::INT, "num_vars")));
  1376. ADD_SIGNAL(MethodInfo("stack_frame_var", PropertyInfo(Variant::ARRAY, "data")));
  1377. ADD_SIGNAL(MethodInfo("debug_data", PropertyInfo(Variant::STRING, "msg"), PropertyInfo(Variant::ARRAY, "data")));
  1378. ADD_SIGNAL(MethodInfo("set_breakpoint", PropertyInfo("script"), PropertyInfo(Variant::INT, "line"), PropertyInfo(Variant::BOOL, "enabled")));
  1379. ADD_SIGNAL(MethodInfo("clear_breakpoints"));
  1380. }
  1381. void ScriptEditorDebugger::add_debugger_plugin(const Ref<Script> &p_script) {
  1382. if (!debugger_plugins.has(p_script)) {
  1383. EditorDebuggerPlugin *plugin = memnew(EditorDebuggerPlugin());
  1384. plugin->attach_debugger(this);
  1385. plugin->set_script(p_script);
  1386. tabs->add_child(plugin);
  1387. debugger_plugins.insert(p_script, plugin);
  1388. }
  1389. }
  1390. void ScriptEditorDebugger::remove_debugger_plugin(const Ref<Script> &p_script) {
  1391. if (debugger_plugins.has(p_script)) {
  1392. tabs->remove_child(debugger_plugins[p_script]);
  1393. debugger_plugins[p_script]->detach_debugger(false);
  1394. memdelete(debugger_plugins[p_script]);
  1395. debugger_plugins.erase(p_script);
  1396. }
  1397. }
  1398. void ScriptEditorDebugger::send_message(const String &p_message, const Array &p_args) {
  1399. _put_msg(p_message, p_args);
  1400. }
  1401. void ScriptEditorDebugger::register_message_capture(const StringName &p_name, const Callable &p_callable) {
  1402. ERR_FAIL_COND_MSG(has_capture(p_name), "Capture already registered: " + p_name);
  1403. captures.insert(p_name, p_callable);
  1404. }
  1405. void ScriptEditorDebugger::unregister_message_capture(const StringName &p_name) {
  1406. ERR_FAIL_COND_MSG(!has_capture(p_name), "Capture not registered: " + p_name);
  1407. captures.erase(p_name);
  1408. }
  1409. bool ScriptEditorDebugger::has_capture(const StringName &p_name) {
  1410. return captures.has(p_name);
  1411. }
  1412. ScriptEditorDebugger::ScriptEditorDebugger(EditorNode *p_editor) {
  1413. editor = p_editor;
  1414. tabs = memnew(TabContainer);
  1415. tabs->set_tab_alignment(TabContainer::ALIGNMENT_LEFT);
  1416. tabs->add_theme_style_override("panel", editor->get_gui_base()->get_theme_stylebox(SNAME("DebuggerPanel"), SNAME("EditorStyles")));
  1417. tabs->connect("tab_changed", callable_mp(this, &ScriptEditorDebugger::_tab_changed));
  1418. add_child(tabs);
  1419. { //debugger
  1420. VBoxContainer *vbc = memnew(VBoxContainer);
  1421. vbc->set_name(TTR("Debugger"));
  1422. Control *dbg = vbc;
  1423. HBoxContainer *hbc = memnew(HBoxContainer);
  1424. vbc->add_child(hbc);
  1425. reason = memnew(Label);
  1426. reason->set_text("");
  1427. hbc->add_child(reason);
  1428. reason->set_h_size_flags(SIZE_EXPAND_FILL);
  1429. reason->set_autowrap_mode(Label::AUTOWRAP_WORD_SMART);
  1430. reason->set_max_lines_visible(3);
  1431. reason->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  1432. hbc->add_child(memnew(VSeparator));
  1433. skip_breakpoints = memnew(Button);
  1434. skip_breakpoints->set_flat(true);
  1435. hbc->add_child(skip_breakpoints);
  1436. skip_breakpoints->set_tooltip(TTR("Skip Breakpoints"));
  1437. skip_breakpoints->connect("pressed", callable_mp(this, &ScriptEditorDebugger::debug_skip_breakpoints));
  1438. hbc->add_child(memnew(VSeparator));
  1439. copy = memnew(Button);
  1440. copy->set_flat(true);
  1441. hbc->add_child(copy);
  1442. copy->set_tooltip(TTR("Copy Error"));
  1443. copy->connect("pressed", callable_mp(this, &ScriptEditorDebugger::debug_copy));
  1444. hbc->add_child(memnew(VSeparator));
  1445. step = memnew(Button);
  1446. step->set_flat(true);
  1447. hbc->add_child(step);
  1448. step->set_tooltip(TTR("Step Into"));
  1449. step->set_shortcut(ED_GET_SHORTCUT("debugger/step_into"));
  1450. step->connect("pressed", callable_mp(this, &ScriptEditorDebugger::debug_step));
  1451. next = memnew(Button);
  1452. next->set_flat(true);
  1453. hbc->add_child(next);
  1454. next->set_tooltip(TTR("Step Over"));
  1455. next->set_shortcut(ED_GET_SHORTCUT("debugger/step_over"));
  1456. next->connect("pressed", callable_mp(this, &ScriptEditorDebugger::debug_next));
  1457. hbc->add_child(memnew(VSeparator));
  1458. dobreak = memnew(Button);
  1459. dobreak->set_flat(true);
  1460. hbc->add_child(dobreak);
  1461. dobreak->set_tooltip(TTR("Break"));
  1462. dobreak->set_shortcut(ED_GET_SHORTCUT("debugger/break"));
  1463. dobreak->connect("pressed", callable_mp(this, &ScriptEditorDebugger::debug_break));
  1464. docontinue = memnew(Button);
  1465. docontinue->set_flat(true);
  1466. hbc->add_child(docontinue);
  1467. docontinue->set_tooltip(TTR("Continue"));
  1468. docontinue->set_shortcut(ED_GET_SHORTCUT("debugger/continue"));
  1469. docontinue->connect("pressed", callable_mp(this, &ScriptEditorDebugger::debug_continue));
  1470. HSplitContainer *parent_sc = memnew(HSplitContainer);
  1471. vbc->add_child(parent_sc);
  1472. parent_sc->set_v_size_flags(SIZE_EXPAND_FILL);
  1473. parent_sc->set_split_offset(500 * EDSCALE);
  1474. HSplitContainer *sc = memnew(HSplitContainer);
  1475. sc->set_v_size_flags(SIZE_EXPAND_FILL);
  1476. sc->set_h_size_flags(SIZE_EXPAND_FILL);
  1477. parent_sc->add_child(sc);
  1478. stack_dump = memnew(Tree);
  1479. stack_dump->set_allow_reselect(true);
  1480. stack_dump->set_columns(1);
  1481. stack_dump->set_column_titles_visible(true);
  1482. stack_dump->set_column_title(0, TTR("Stack Frames"));
  1483. stack_dump->set_h_size_flags(SIZE_EXPAND_FILL);
  1484. stack_dump->set_hide_root(true);
  1485. stack_dump->connect("cell_selected", callable_mp(this, &ScriptEditorDebugger::_stack_dump_frame_selected));
  1486. sc->add_child(stack_dump);
  1487. VBoxContainer *inspector_vbox = memnew(VBoxContainer);
  1488. inspector_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  1489. sc->add_child(inspector_vbox);
  1490. HBoxContainer *tools_hb = memnew(HBoxContainer);
  1491. inspector_vbox->add_child(tools_hb);
  1492. search = memnew(LineEdit);
  1493. search->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1494. search->set_placeholder(TTR("Filter stack variables"));
  1495. search->set_clear_button_enabled(true);
  1496. tools_hb->add_child(search);
  1497. inspector = memnew(EditorDebuggerInspector);
  1498. inspector->set_h_size_flags(SIZE_EXPAND_FILL);
  1499. inspector->set_v_size_flags(SIZE_EXPAND_FILL);
  1500. inspector->set_enable_capitalize_paths(false);
  1501. inspector->set_read_only(true);
  1502. inspector->connect("object_selected", callable_mp(this, &ScriptEditorDebugger::_remote_object_selected));
  1503. inspector->connect("object_edited", callable_mp(this, &ScriptEditorDebugger::_remote_object_edited));
  1504. inspector->connect("object_property_updated", callable_mp(this, &ScriptEditorDebugger::_remote_object_property_updated));
  1505. inspector->register_text_enter(search);
  1506. inspector->set_use_filter(true);
  1507. inspector_vbox->add_child(inspector);
  1508. breakpoints_tree = memnew(Tree);
  1509. breakpoints_tree->set_h_size_flags(SIZE_EXPAND_FILL);
  1510. breakpoints_tree->set_column_titles_visible(true);
  1511. breakpoints_tree->set_column_title(0, TTR("Breakpoints"));
  1512. breakpoints_tree->set_allow_reselect(true);
  1513. breakpoints_tree->set_allow_rmb_select(true);
  1514. breakpoints_tree->set_hide_root(true);
  1515. breakpoints_tree->connect("item_rmb_selected", callable_mp(this, &ScriptEditorDebugger::_breakpoints_item_rmb_selected));
  1516. breakpoints_tree->create_item();
  1517. parent_sc->add_child(breakpoints_tree);
  1518. tabs->add_child(dbg);
  1519. breakpoints_menu = memnew(PopupMenu);
  1520. breakpoints_menu->connect("id_pressed", callable_mp(this, &ScriptEditorDebugger::_item_menu_id_pressed));
  1521. breakpoints_tree->add_child(breakpoints_menu);
  1522. }
  1523. { //errors
  1524. errors_tab = memnew(VBoxContainer);
  1525. errors_tab->set_name(TTR("Errors"));
  1526. HBoxContainer *error_hbox = memnew(HBoxContainer);
  1527. errors_tab->add_child(error_hbox);
  1528. expand_all_button = memnew(Button);
  1529. expand_all_button->set_text(TTR("Expand All"));
  1530. expand_all_button->set_disabled(true);
  1531. expand_all_button->connect("pressed", callable_mp(this, &ScriptEditorDebugger::_expand_errors_list));
  1532. error_hbox->add_child(expand_all_button);
  1533. collapse_all_button = memnew(Button);
  1534. collapse_all_button->set_text(TTR("Collapse All"));
  1535. collapse_all_button->set_disabled(true);
  1536. collapse_all_button->connect("pressed", callable_mp(this, &ScriptEditorDebugger::_collapse_errors_list));
  1537. error_hbox->add_child(collapse_all_button);
  1538. Control *space = memnew(Control);
  1539. space->set_h_size_flags(SIZE_EXPAND_FILL);
  1540. error_hbox->add_child(space);
  1541. clear_button = memnew(Button);
  1542. clear_button->set_text(TTR("Clear"));
  1543. clear_button->set_h_size_flags(0);
  1544. clear_button->set_disabled(true);
  1545. clear_button->connect("pressed", callable_mp(this, &ScriptEditorDebugger::_clear_errors_list));
  1546. error_hbox->add_child(clear_button);
  1547. error_tree = memnew(Tree);
  1548. error_tree->set_columns(2);
  1549. error_tree->set_column_expand(0, false);
  1550. error_tree->set_column_custom_minimum_width(0, 140);
  1551. error_tree->set_column_clip_content(0, true);
  1552. error_tree->set_column_expand(1, true);
  1553. error_tree->set_column_clip_content(1, true);
  1554. error_tree->set_select_mode(Tree::SELECT_ROW);
  1555. error_tree->set_hide_root(true);
  1556. error_tree->set_v_size_flags(SIZE_EXPAND_FILL);
  1557. error_tree->set_allow_rmb_select(true);
  1558. error_tree->connect("item_rmb_selected", callable_mp(this, &ScriptEditorDebugger::_error_tree_item_rmb_selected));
  1559. errors_tab->add_child(error_tree);
  1560. item_menu = memnew(PopupMenu);
  1561. item_menu->connect("id_pressed", callable_mp(this, &ScriptEditorDebugger::_item_menu_id_pressed));
  1562. error_tree->add_child(item_menu);
  1563. tabs->add_child(errors_tab);
  1564. }
  1565. { // File dialog
  1566. file_dialog = memnew(EditorFileDialog);
  1567. file_dialog->connect("file_selected", callable_mp(this, &ScriptEditorDebugger::_file_selected));
  1568. add_child(file_dialog);
  1569. }
  1570. { //profiler
  1571. profiler = memnew(EditorProfiler);
  1572. profiler->set_name(TTR("Profiler"));
  1573. tabs->add_child(profiler);
  1574. profiler->connect("enable_profiling", callable_mp(this, &ScriptEditorDebugger::_profiler_activate), varray(PROFILER_SCRIPTS_SERVERS));
  1575. profiler->connect("break_request", callable_mp(this, &ScriptEditorDebugger::_profiler_seeked));
  1576. }
  1577. { //frame profiler
  1578. visual_profiler = memnew(EditorVisualProfiler);
  1579. visual_profiler->set_name(TTR("Visual Profiler"));
  1580. tabs->add_child(visual_profiler);
  1581. visual_profiler->connect("enable_profiling", callable_mp(this, &ScriptEditorDebugger::_profiler_activate), varray(PROFILER_VISUAL));
  1582. }
  1583. { //network profiler
  1584. network_profiler = memnew(EditorNetworkProfiler);
  1585. network_profiler->set_name(TTR("Network Profiler"));
  1586. tabs->add_child(network_profiler);
  1587. network_profiler->connect("enable_profiling", callable_mp(this, &ScriptEditorDebugger::_profiler_activate), varray(PROFILER_NETWORK));
  1588. }
  1589. { //monitors
  1590. performance_profiler = memnew(EditorPerformanceProfiler);
  1591. tabs->add_child(performance_profiler);
  1592. }
  1593. { //vmem inspect
  1594. VBoxContainer *vmem_vb = memnew(VBoxContainer);
  1595. HBoxContainer *vmem_hb = memnew(HBoxContainer);
  1596. Label *vmlb = memnew(Label(TTR("List of Video Memory Usage by Resource:") + " "));
  1597. vmlb->set_theme_type_variation("HeaderSmall");
  1598. vmlb->set_h_size_flags(SIZE_EXPAND_FILL);
  1599. vmem_hb->add_child(vmlb);
  1600. vmem_hb->add_child(memnew(Label(TTR("Total:") + " ")));
  1601. vmem_total = memnew(LineEdit);
  1602. vmem_total->set_editable(false);
  1603. vmem_total->set_custom_minimum_size(Size2(100, 0) * EDSCALE);
  1604. vmem_hb->add_child(vmem_total);
  1605. vmem_refresh = memnew(Button);
  1606. vmem_refresh->set_flat(true);
  1607. vmem_hb->add_child(vmem_refresh);
  1608. vmem_export = memnew(Button);
  1609. vmem_export->set_flat(true);
  1610. vmem_export->set_tooltip(TTR("Export list to a CSV file"));
  1611. vmem_hb->add_child(vmem_export);
  1612. vmem_vb->add_child(vmem_hb);
  1613. vmem_refresh->connect("pressed", callable_mp(this, &ScriptEditorDebugger::_video_mem_request));
  1614. vmem_export->connect("pressed", callable_mp(this, &ScriptEditorDebugger::_video_mem_export));
  1615. VBoxContainer *vmmc = memnew(VBoxContainer);
  1616. vmem_tree = memnew(Tree);
  1617. vmem_tree->set_v_size_flags(SIZE_EXPAND_FILL);
  1618. vmem_tree->set_h_size_flags(SIZE_EXPAND_FILL);
  1619. vmmc->add_child(vmem_tree);
  1620. vmmc->set_v_size_flags(SIZE_EXPAND_FILL);
  1621. vmem_vb->add_child(vmmc);
  1622. vmem_vb->set_name(TTR("Video RAM"));
  1623. vmem_tree->set_columns(4);
  1624. vmem_tree->set_column_titles_visible(true);
  1625. vmem_tree->set_column_title(0, TTR("Resource Path"));
  1626. vmem_tree->set_column_expand(0, true);
  1627. vmem_tree->set_column_expand(1, false);
  1628. vmem_tree->set_column_title(1, TTR("Type"));
  1629. vmem_tree->set_column_custom_minimum_width(1, 100 * EDSCALE);
  1630. vmem_tree->set_column_expand(2, false);
  1631. vmem_tree->set_column_title(2, TTR("Format"));
  1632. vmem_tree->set_column_custom_minimum_width(2, 150 * EDSCALE);
  1633. vmem_tree->set_column_expand(3, false);
  1634. vmem_tree->set_column_title(3, TTR("Usage"));
  1635. vmem_tree->set_column_custom_minimum_width(3, 80 * EDSCALE);
  1636. vmem_tree->set_hide_root(true);
  1637. tabs->add_child(vmem_vb);
  1638. }
  1639. { // misc
  1640. VBoxContainer *misc = memnew(VBoxContainer);
  1641. misc->set_name(TTR("Misc"));
  1642. tabs->add_child(misc);
  1643. GridContainer *info_left = memnew(GridContainer);
  1644. info_left->set_columns(2);
  1645. misc->add_child(info_left);
  1646. clicked_ctrl = memnew(LineEdit);
  1647. clicked_ctrl->set_h_size_flags(SIZE_EXPAND_FILL);
  1648. info_left->add_child(memnew(Label(TTR("Clicked Control:"))));
  1649. info_left->add_child(clicked_ctrl);
  1650. clicked_ctrl_type = memnew(LineEdit);
  1651. info_left->add_child(memnew(Label(TTR("Clicked Control Type:"))));
  1652. info_left->add_child(clicked_ctrl_type);
  1653. scene_tree = memnew(SceneDebuggerTree);
  1654. live_edit_root = memnew(LineEdit);
  1655. live_edit_root->set_h_size_flags(SIZE_EXPAND_FILL);
  1656. {
  1657. HBoxContainer *lehb = memnew(HBoxContainer);
  1658. Label *l = memnew(Label(TTR("Live Edit Root:")));
  1659. info_left->add_child(l);
  1660. lehb->add_child(live_edit_root);
  1661. le_set = memnew(Button(TTR("Set From Tree")));
  1662. lehb->add_child(le_set);
  1663. le_clear = memnew(Button(TTR("Clear")));
  1664. lehb->add_child(le_clear);
  1665. info_left->add_child(lehb);
  1666. }
  1667. misc->add_child(memnew(VSeparator));
  1668. HBoxContainer *buttons = memnew(HBoxContainer);
  1669. export_csv = memnew(Button(TTR("Export measures as CSV")));
  1670. export_csv->connect("pressed", callable_mp(this, &ScriptEditorDebugger::_export_csv));
  1671. buttons->add_child(export_csv);
  1672. misc->add_child(buttons);
  1673. }
  1674. msgdialog = memnew(AcceptDialog);
  1675. add_child(msgdialog);
  1676. live_debug = true;
  1677. camera_override = CameraOverride::OVERRIDE_NONE;
  1678. last_path_id = false;
  1679. error_count = 0;
  1680. warning_count = 0;
  1681. _update_buttons_state();
  1682. }
  1683. ScriptEditorDebugger::~ScriptEditorDebugger() {
  1684. if (peer.is_valid()) {
  1685. peer->close();
  1686. peer.unref();
  1687. }
  1688. memdelete(scene_tree);
  1689. }