script_editor_debugger.cpp 76 KB

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