script_editor_debugger.cpp 76 KB

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