script_editor_debugger.cpp 66 KB

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