script_editor_debugger.cpp 67 KB

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