script_editor_debugger.cpp 80 KB

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