script_editor_debugger.cpp 81 KB

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