script_editor_debugger.cpp 66 KB

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