Debian Codegraph @

editor/debugger/script_editor_debugger.cpp

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