script_editor_debugger.cpp 65 KB

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