2
0

script_editor_debugger.cpp 61 KB

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