/**************************************************************************/ /* script_editor_debugger.cpp */ /**************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /**************************************************************************/ /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /**************************************************************************/ #include "script_editor_debugger.h" #include "core/config/project_settings.h" #include "core/debugger/debugger_marshalls.h" #include "core/debugger/remote_debugger.h" #include "core/io/marshalls.h" #include "core/string/ustring.h" #include "core/version.h" #include "editor/debugger/debug_adapter/debug_adapter_protocol.h" #include "editor/debugger/editor_performance_profiler.h" #include "editor/debugger/editor_profiler.h" #include "editor/debugger/editor_visual_profiler.h" #include "editor/editor_file_system.h" #include "editor/editor_log.h" #include "editor/editor_node.h" #include "editor/editor_property_name_processor.h" #include "editor/editor_settings.h" #include "editor/editor_string_names.h" #include "editor/gui/editor_file_dialog.h" #include "editor/inspector_dock.h" #include "editor/plugins/canvas_item_editor_plugin.h" #include "editor/plugins/editor_debugger_plugin.h" #include "editor/plugins/node_3d_editor_plugin.h" #include "editor/themes/editor_scale.h" #include "main/performance.h" #include "scene/3d/camera_3d.h" #include "scene/debugger/scene_debugger.h" #include "scene/gui/dialogs.h" #include "scene/gui/grid_container.h" #include "scene/gui/label.h" #include "scene/gui/line_edit.h" #include "scene/gui/margin_container.h" #include "scene/gui/rich_text_label.h" #include "scene/gui/separator.h" #include "scene/gui/split_container.h" #include "scene/gui/tab_container.h" #include "scene/gui/texture_button.h" #include "scene/gui/tree.h" #include "scene/resources/packed_scene.h" #include "servers/debugger/servers_debugger.h" #include "servers/display_server.h" using CameraOverride = EditorDebuggerNode::CameraOverride; void ScriptEditorDebugger::_put_msg(const String &p_message, const Array &p_data, uint64_t p_thread_id) { ERR_FAIL_COND(p_thread_id == Thread::UNASSIGNED_ID); if (is_session_active()) { Array msg; msg.push_back(p_message); msg.push_back(p_thread_id); msg.push_back(p_data); peer->put_message(msg); } } void ScriptEditorDebugger::debug_copy() { String msg = reason->get_text(); if (msg.is_empty()) { return; } DisplayServer::get_singleton()->clipboard_set(msg); } void ScriptEditorDebugger::debug_skip_breakpoints() { skip_breakpoints_value = !skip_breakpoints_value; if (skip_breakpoints_value) { skip_breakpoints->set_icon(get_editor_theme_icon(SNAME("DebugSkipBreakpointsOn"))); } else { skip_breakpoints->set_icon(get_editor_theme_icon(SNAME("DebugSkipBreakpointsOff"))); } Array msg; msg.push_back(skip_breakpoints_value); _put_msg("set_skip_breakpoints", msg, debugging_thread_id != Thread::UNASSIGNED_ID ? debugging_thread_id : Thread::MAIN_ID); } void ScriptEditorDebugger::debug_next() { ERR_FAIL_COND(!is_breaked()); _put_msg("next", Array(), debugging_thread_id); _clear_execution(); } void ScriptEditorDebugger::debug_step() { ERR_FAIL_COND(!is_breaked()); _put_msg("step", Array(), debugging_thread_id); _clear_execution(); } void ScriptEditorDebugger::debug_break() { ERR_FAIL_COND(is_breaked()); _put_msg("break", Array()); } void ScriptEditorDebugger::debug_continue() { ERR_FAIL_COND(!is_breaked()); // Allow focus stealing only if we actually run this client for security. if (remote_pid && EditorNode::get_singleton()->has_child_process(remote_pid)) { DisplayServer::get_singleton()->enable_for_stealing_focus(remote_pid); } _clear_execution(); _put_msg("continue", Array(), debugging_thread_id); _put_msg("servers:foreground", Array()); } void ScriptEditorDebugger::update_tabs() { if (error_count == 0 && warning_count == 0) { errors_tab->set_name(TTR("Errors")); tabs->set_tab_icon(tabs->get_tab_idx_from_control(errors_tab), Ref()); } else { errors_tab->set_name(TTR("Errors") + " (" + itos(error_count + warning_count) + ")"); if (error_count >= 1 && warning_count >= 1) { tabs->set_tab_icon(tabs->get_tab_idx_from_control(errors_tab), get_editor_theme_icon(SNAME("ErrorWarning"))); } else if (error_count >= 1) { tabs->set_tab_icon(tabs->get_tab_idx_from_control(errors_tab), get_editor_theme_icon(SNAME("Error"))); } else { tabs->set_tab_icon(tabs->get_tab_idx_from_control(errors_tab), get_editor_theme_icon(SNAME("Warning"))); } } } void ScriptEditorDebugger::clear_style() { tabs->remove_theme_style_override(SceneStringName(panel)); } void ScriptEditorDebugger::save_node(ObjectID p_id, const String &p_file) { Array msg; msg.push_back(p_id); msg.push_back(p_file); _put_msg("scene:save_node", msg); } void ScriptEditorDebugger::_file_selected(const String &p_file) { switch (file_dialog_purpose) { case SAVE_MONITORS_CSV: { Error err; Ref file = FileAccess::open(p_file, FileAccess::WRITE, &err); if (err != OK) { ERR_PRINT("Failed to open " + p_file); return; } Vector line; line.resize(Performance::MONITOR_MAX); // signatures for (int i = 0; i < Performance::MONITOR_MAX; i++) { line.write[i] = Performance::get_singleton()->get_monitor_name(Performance::Monitor(i)); } file->store_csv_line(line); // values Vector::Element *> iterators; iterators.resize(Performance::MONITOR_MAX); bool continue_iteration = false; for (int i = 0; i < Performance::MONITOR_MAX; i++) { iterators.write[i] = performance_profiler->get_monitor_data(Performance::get_singleton()->get_monitor_name(Performance::Monitor(i)))->back(); continue_iteration = continue_iteration || iterators[i]; } while (continue_iteration) { continue_iteration = false; for (int i = 0; i < Performance::MONITOR_MAX; i++) { if (iterators[i]) { line.write[i] = String::num_real(iterators[i]->get()); iterators.write[i] = iterators[i]->prev(); } else { line.write[i] = ""; } continue_iteration = continue_iteration || iterators[i]; } file->store_csv_line(line); } file->store_string("\n"); Vector> profiler_data = profiler->get_data_as_csv(); for (int i = 0; i < profiler_data.size(); i++) { file->store_csv_line(profiler_data[i]); } } break; case SAVE_VRAM_CSV: { Error err; Ref file = FileAccess::open(p_file, FileAccess::WRITE, &err); if (err != OK) { ERR_PRINT("Failed to open " + p_file); return; } Vector headers; headers.resize(vmem_tree->get_columns()); for (int i = 0; i < vmem_tree->get_columns(); ++i) { headers.write[i] = vmem_tree->get_column_title(i); } file->store_csv_line(headers); if (vmem_tree->get_root()) { TreeItem *ti = vmem_tree->get_root()->get_first_child(); while (ti) { Vector values; values.resize(vmem_tree->get_columns()); for (int i = 0; i < vmem_tree->get_columns(); ++i) { values.write[i] = ti->get_text(i); } file->store_csv_line(values); ti = ti->get_next(); } } } break; } } void ScriptEditorDebugger::request_remote_tree() { _put_msg("scene:request_scene_tree", Array()); } const SceneDebuggerTree *ScriptEditorDebugger::get_remote_tree() { return scene_tree; } void ScriptEditorDebugger::update_remote_object(ObjectID p_obj_id, const String &p_prop, const Variant &p_value) { Array msg; msg.push_back(p_obj_id); msg.push_back(p_prop); msg.push_back(p_value); _put_msg("scene:set_object_property", msg); } void ScriptEditorDebugger::request_remote_object(ObjectID p_obj_id) { ERR_FAIL_COND(p_obj_id.is_null()); Array msg; msg.push_back(p_obj_id); _put_msg("scene:inspect_object", msg); } Object *ScriptEditorDebugger::get_remote_object(ObjectID p_id) { return inspector->get_object(p_id); } void ScriptEditorDebugger::_remote_object_selected(ObjectID p_id) { emit_signal(SNAME("remote_object_requested"), p_id); } void ScriptEditorDebugger::_remote_object_edited(ObjectID p_id, const String &p_prop, const Variant &p_value) { update_remote_object(p_id, p_prop, p_value); request_remote_object(p_id); } void ScriptEditorDebugger::_remote_object_property_updated(ObjectID p_id, const String &p_property) { emit_signal(SNAME("remote_object_property_updated"), p_id, p_property); } void ScriptEditorDebugger::_video_mem_request() { _put_msg("servers:memory", Array()); } void ScriptEditorDebugger::_video_mem_export() { file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE); file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM); file_dialog->clear_filters(); file_dialog_purpose = SAVE_VRAM_CSV; file_dialog->popup_file_dialog(); } Size2 ScriptEditorDebugger::get_minimum_size() const { Size2 ms = MarginContainer::get_minimum_size(); ms.y = MAX(ms.y, 250 * EDSCALE); return ms; } void ScriptEditorDebugger::_thread_debug_enter(uint64_t p_thread_id) { ERR_FAIL_COND(!threads_debugged.has(p_thread_id)); ThreadDebugged &td = threads_debugged[p_thread_id]; _set_reason_text(td.error, MESSAGE_ERROR); emit_signal(SNAME("breaked"), true, td.can_debug, td.error, td.has_stackdump); if (!td.error.is_empty()) { tabs->set_current_tab(0); } inspector->clear_cache(); // Take a chance to force remote objects update. _put_msg("get_stack_dump", Array(), p_thread_id); } void ScriptEditorDebugger::_select_thread(int p_index) { debugging_thread_id = threads->get_item_metadata(threads->get_selected()); _thread_debug_enter(debugging_thread_id); } void ScriptEditorDebugger::_parse_message(const String &p_msg, uint64_t p_thread_id, const Array &p_data) { emit_signal(SNAME("debug_data"), p_msg, p_data); if (p_msg == "debug_enter") { ERR_FAIL_COND(p_data.size() != 4); ThreadDebugged td; td.name = p_data[3]; td.error = p_data[1]; td.can_debug = p_data[0]; td.has_stackdump = p_data[2]; td.thread_id = p_thread_id; static uint32_t order_inc = 0; td.debug_order = order_inc++; threads_debugged.insert(p_thread_id, td); if (threads_debugged.size() == 1) { // First thread that requests debug debugging_thread_id = p_thread_id; _thread_debug_enter(p_thread_id); can_request_idle_draw = true; if (is_move_to_foreground()) { DisplayServer::get_singleton()->window_move_to_foreground(); } profiler->set_enabled(false, false); visual_profiler->set_enabled(false); } _update_buttons_state(); } else if (p_msg == "debug_exit") { threads_debugged.erase(p_thread_id); if (p_thread_id == debugging_thread_id) { _clear_execution(); if (threads_debugged.size() == 0) { debugging_thread_id = Thread::UNASSIGNED_ID; } else { // Find next thread to debug. uint32_t min_order = 0xFFFFFFFF; uint64_t next_thread = Thread::UNASSIGNED_ID; for (KeyValue T : threads_debugged) { if (T.value.debug_order < min_order) { min_order = T.value.debug_order; next_thread = T.key; } } debugging_thread_id = next_thread; } if (debugging_thread_id == Thread::UNASSIGNED_ID) { // Nothing else to debug. profiler->set_enabled(true, false); profiler->disable_seeking(); visual_profiler->set_enabled(true); _set_reason_text(TTR("Execution resumed."), MESSAGE_SUCCESS); emit_signal(SNAME("breaked"), false, false, "", false); _update_buttons_state(); } else { _thread_debug_enter(debugging_thread_id); } } else { _update_buttons_state(); } } else if (p_msg == "set_pid") { ERR_FAIL_COND(p_data.is_empty()); remote_pid = p_data[0]; } else if (p_msg == "scene:click_ctrl") { ERR_FAIL_COND(p_data.size() < 2); clicked_ctrl->set_text(p_data[0]); clicked_ctrl_type->set_text(p_data[1]); } else if (p_msg == "scene:scene_tree") { scene_tree->nodes.clear(); scene_tree->deserialize(p_data); emit_signal(SNAME("remote_tree_updated")); _update_buttons_state(); } else if (p_msg == "scene:inspect_object") { ObjectID id = inspector->add_object(p_data); if (id.is_valid()) { emit_signal(SNAME("remote_object_updated"), id); } } else if (p_msg == "servers:memory_usage") { vmem_tree->clear(); TreeItem *root = vmem_tree->create_item(); ServersDebugger::ResourceUsage usage; usage.deserialize(p_data); uint64_t total = 0; for (const ServersDebugger::ResourceInfo &E : usage.infos) { TreeItem *it = vmem_tree->create_item(root); String type = E.type; int bytes = E.vram; it->set_text(0, E.path); it->set_text(1, type); it->set_text(2, E.format); it->set_text(3, String::humanize_size(bytes)); total += bytes; if (has_theme_icon(type, EditorStringName(EditorIcons))) { it->set_icon(0, get_editor_theme_icon(type)); } } vmem_total->set_tooltip_text(TTR("Bytes:") + " " + itos(total)); vmem_total->set_text(String::humanize_size(total)); } else if (p_msg == "servers:drawn") { can_request_idle_draw = true; } else if (p_msg == "stack_dump") { DebuggerMarshalls::ScriptStackDump stack; stack.deserialize(p_data); stack_dump->clear(); inspector->clear_stack_variables(); TreeItem *r = stack_dump->create_item(); Array stack_dump_info; int i = 0; for (List::Iterator itr = stack.frames.begin(); itr != stack.frames.end(); ++itr, ++i) { TreeItem *s = stack_dump->create_item(r); Dictionary d; d["frame"] = i; d["file"] = itr->file; d["function"] = itr->func; d["line"] = itr->line; stack_dump_info.push_back(d); s->set_metadata(0, d); String line = itos(i) + " - " + String(d["file"]) + ":" + itos(d["line"]) + " - at function: " + String(d["function"]); s->set_text(0, line); if (i == 0) { s->select(0); } } emit_signal(SNAME("stack_dump"), stack_dump_info); } else if (p_msg == "stack_frame_vars") { inspector->clear_stack_variables(); ERR_FAIL_COND(p_data.size() != 1); emit_signal(SNAME("stack_frame_vars"), p_data[0]); } else if (p_msg == "stack_frame_var") { inspector->add_stack_variable(p_data); emit_signal(SNAME("stack_frame_var"), p_data); } else if (p_msg == "output") { ERR_FAIL_COND(p_data.size() != 2); ERR_FAIL_COND(p_data[0].get_type() != Variant::PACKED_STRING_ARRAY); Vector output_strings = p_data[0]; ERR_FAIL_COND(p_data[1].get_type() != Variant::PACKED_INT32_ARRAY); Vector output_types = p_data[1]; ERR_FAIL_COND(output_strings.size() != output_types.size()); for (int i = 0; i < output_strings.size(); i++) { RemoteDebugger::MessageType type = (RemoteDebugger::MessageType)(int)(output_types[i]); EditorLog::MessageType msg_type; switch (type) { case RemoteDebugger::MESSAGE_TYPE_LOG: { msg_type = EditorLog::MSG_TYPE_STD; } break; case RemoteDebugger::MESSAGE_TYPE_LOG_RICH: { msg_type = EditorLog::MSG_TYPE_STD_RICH; } break; case RemoteDebugger::MESSAGE_TYPE_ERROR: { msg_type = EditorLog::MSG_TYPE_ERROR; } break; default: { WARN_PRINT("Unhandled script debugger message type: " + itos(type)); msg_type = EditorLog::MSG_TYPE_STD; } break; } EditorNode::get_log()->add_message(output_strings[i], msg_type); emit_signal(SNAME("output"), output_strings[i], msg_type); } } else if (p_msg == "performance:profile_frame") { Vector frame_data; frame_data.resize(p_data.size()); for (int i = 0; i < p_data.size(); i++) { frame_data.write[i] = p_data[i]; } performance_profiler->add_profile_frame(frame_data); } else if (p_msg == "visual:profile_frame") { ServersDebugger::VisualProfilerFrame frame; frame.deserialize(p_data); EditorVisualProfiler::Metric metric; metric.areas.resize(frame.areas.size()); metric.frame_number = frame.frame_number; metric.valid = true; { EditorVisualProfiler::Metric::Area *areas_ptr = metric.areas.ptrw(); for (int i = 0; i < frame.areas.size(); i++) { areas_ptr[i].name = frame.areas[i].name; areas_ptr[i].cpu_time = frame.areas[i].cpu_msec; areas_ptr[i].gpu_time = frame.areas[i].gpu_msec; } } visual_profiler->add_frame_metric(metric); } else if (p_msg == "error") { DebuggerMarshalls::OutputError oe; ERR_FAIL_COND_MSG(oe.deserialize(p_data) == false, "Failed to deserialize error message"); // Format time. Array time_vals; time_vals.push_back(oe.hr); time_vals.push_back(oe.min); time_vals.push_back(oe.sec); time_vals.push_back(oe.msec); bool e; String time = String("%d:%02d:%02d:%04d").sprintf(time_vals, &e); // Rest of the error data. bool source_is_project_file = oe.source_file.begins_with("res://"); // Metadata to highlight error line in scripts. Array source_meta; source_meta.push_back(oe.source_file); source_meta.push_back(oe.source_line); // Create error tree to display above error or warning details. TreeItem *r = error_tree->get_root(); if (!r) { r = error_tree->create_item(); } // Also provide the relevant details as tooltip to quickly check without // uncollapsing the tree. String tooltip = oe.warning ? TTR("Warning:") : TTR("Error:"); TreeItem *error = error_tree->create_item(r); if (oe.warning) { error->set_meta("_is_warning", true); } else { error->set_meta("_is_error", true); } error->set_collapsed(true); error->set_icon(0, get_editor_theme_icon(oe.warning ? SNAME("Warning") : SNAME("Error"))); error->set_text(0, time); error->set_text_alignment(0, HORIZONTAL_ALIGNMENT_LEFT); const Color color = get_theme_color(oe.warning ? SNAME("warning_color") : SNAME("error_color"), EditorStringName(Editor)); error->set_custom_color(0, color); error->set_custom_color(1, color); String error_title; if (!oe.source_func.is_empty() && source_is_project_file) { // If source function is inside the project file. error_title += oe.source_func + ": "; } else if (oe.callstack.size() > 0) { // Otherwise, if available, use the script's stack in the error title. error_title = _format_frame_text(&oe.callstack[0]) + ": "; } else if (!oe.source_func.is_empty()) { // Otherwise try to use the C++ source function. error_title += oe.source_func + ": "; } // If we have a (custom) error message, use it as title, and add a C++ Error // item with the original error condition. error_title += oe.error_descr.is_empty() ? oe.error : oe.error_descr; error->set_text(1, error_title); tooltip += " " + error_title + "\n"; // Find the language of the error's source file. String source_language_name = "C++"; // Default value is the old hard-coded one. const String source_file_extension = oe.source_file.get_extension(); for (int i = 0; i < ScriptServer::get_language_count(); ++i) { ScriptLanguage *script_language = ScriptServer::get_language(i); if (source_file_extension == script_language->get_extension()) { source_language_name = script_language->get_name(); break; } } if (!oe.error_descr.is_empty()) { // Add item for C++ error condition. TreeItem *cpp_cond = error_tree->create_item(error); // TRANSLATORS: %s is the name of a language, e.g. C++. cpp_cond->set_text(0, "<" + vformat(TTR("%s Error"), source_language_name) + ">"); cpp_cond->set_text(1, oe.error); cpp_cond->set_text_alignment(0, HORIZONTAL_ALIGNMENT_LEFT); tooltip += vformat(TTR("%s Error:"), source_language_name) + " " + oe.error + "\n"; if (source_is_project_file) { cpp_cond->set_metadata(0, source_meta); } } Vector v; v.resize(100); // Source of the error. String source_txt = (source_is_project_file ? oe.source_file.get_file() : oe.source_file) + ":" + itos(oe.source_line); if (!oe.source_func.is_empty()) { source_txt += " @ " + oe.source_func; if (!oe.source_func.ends_with(")")) { source_txt += "()"; } } TreeItem *cpp_source = error_tree->create_item(error); // TRANSLATORS: %s is the name of a language, e.g. C++. cpp_source->set_text(0, "<" + vformat(TTR("%s Source"), source_language_name) + ">"); cpp_source->set_text(1, source_txt); cpp_source->set_text_alignment(0, HORIZONTAL_ALIGNMENT_LEFT); tooltip += vformat(TTR("%s Source:"), source_language_name) + " " + source_txt + "\n"; // Set metadata to highlight error line in scripts. if (source_is_project_file) { error->set_metadata(0, source_meta); cpp_source->set_metadata(0, source_meta); } // Format stack trace. // stack_items_count is the number of elements to parse, with 3 items per frame // of the stack trace (script, method, line). const ScriptLanguage::StackInfo *infos = oe.callstack.ptr(); for (unsigned int i = 0; i < (unsigned int)oe.callstack.size(); i++) { TreeItem *stack_trace = error_tree->create_item(error); Array meta; meta.push_back(infos[i].file); meta.push_back(infos[i].line); stack_trace->set_metadata(0, meta); if (i == 0) { stack_trace->set_text(0, "<" + TTR("Stack Trace") + ">"); stack_trace->set_text_alignment(0, HORIZONTAL_ALIGNMENT_LEFT); if (!source_is_project_file) { // Only override metadata if the source is not inside the project. error->set_metadata(0, meta); } tooltip += TTR("Stack Trace:") + "\n"; } String frame_txt = _format_frame_text(&infos[i]); tooltip += frame_txt + "\n"; stack_trace->set_text(1, frame_txt); } error->set_tooltip_text(0, tooltip); error->set_tooltip_text(1, tooltip); if (warning_count == 0 && error_count == 0) { expand_all_button->set_disabled(false); collapse_all_button->set_disabled(false); clear_button->set_disabled(false); } if (oe.warning) { warning_count++; } else { error_count++; } } else if (p_msg == "servers:function_signature") { // Cache a profiler signature. ServersDebugger::ScriptFunctionSignature sig; sig.deserialize(p_data); profiler_signature[sig.id] = sig.name; } else if (p_msg == "servers:profile_frame" || p_msg == "servers:profile_total") { EditorProfiler::Metric metric; ServersDebugger::ServersProfilerFrame frame; frame.deserialize(p_data); metric.valid = true; metric.frame_number = frame.frame_number; metric.frame_time = frame.frame_time; metric.process_time = frame.process_time; metric.physics_time = frame.physics_time; metric.physics_frame_time = frame.physics_frame_time; if (frame.servers.size()) { EditorProfiler::Metric::Category frame_time; frame_time.signature = "category_frame_time"; frame_time.name = "Frame Time"; frame_time.total_time = metric.frame_time; EditorProfiler::Metric::Category::Item item; item.calls = 1; item.line = 0; item.name = "Physics Time"; item.total = metric.physics_time; item.self = item.total; item.signature = "physics_time"; frame_time.items.push_back(item); item.name = "Process Time"; item.total = metric.process_time; item.self = item.total; item.signature = "process_time"; frame_time.items.push_back(item); item.name = "Physics Frame Time"; item.total = metric.physics_frame_time; item.self = item.total; item.signature = "physics_frame_time"; frame_time.items.push_back(item); metric.categories.push_back(frame_time); } for (const ServersDebugger::ServerInfo &srv : frame.servers) { EditorProfiler::Metric::Category c; const String name = srv.name; c.name = EditorPropertyNameProcessor::get_singleton()->process_name(name, EditorPropertyNameProcessor::STYLE_CAPITALIZED); c.items.resize(srv.functions.size()); c.total_time = 0; c.signature = "categ::" + name; int j = 0; for (List::ConstIterator itr = srv.functions.begin(); itr != srv.functions.end(); ++itr, ++j) { EditorProfiler::Metric::Category::Item item; item.calls = 1; item.line = 0; item.name = itr->name; item.self = itr->time; item.total = item.self; item.signature = "categ::" + name + "::" + item.name; item.name = EditorPropertyNameProcessor::get_singleton()->process_name(item.name, EditorPropertyNameProcessor::STYLE_CAPITALIZED); c.total_time += item.total; c.items.write[j] = item; } metric.categories.push_back(c); } EditorProfiler::Metric::Category funcs; funcs.total_time = frame.script_time; funcs.items.resize(frame.script_functions.size()); funcs.name = "Script Functions"; funcs.signature = "script_functions"; for (int i = 0; i < frame.script_functions.size(); i++) { int signature = frame.script_functions[i].sig_id; int calls = frame.script_functions[i].call_count; float total = frame.script_functions[i].total_time; float self = frame.script_functions[i].self_time; float internal = frame.script_functions[i].internal_time; EditorProfiler::Metric::Category::Item item; if (profiler_signature.has(signature)) { item.signature = profiler_signature[signature]; String name = profiler_signature[signature]; Vector strings = name.split("::"); if (strings.size() == 3) { item.name = strings[2]; item.script = strings[0]; item.line = strings[1].to_int(); } else if (strings.size() == 4) { //Built-in scripts have an :: in their name item.name = strings[3]; item.script = strings[0] + "::" + strings[1]; item.line = strings[2].to_int(); } } else { item.name = "SigErr " + itos(signature); } item.calls = calls; item.self = self; item.total = total; item.internal = internal; funcs.items.write[i] = item; } metric.categories.push_back(funcs); if (p_msg == "servers:profile_frame") { profiler->add_frame_metric(metric, false); } else { profiler->add_frame_metric(metric, true); } } else if (p_msg == "request_quit") { emit_signal(SNAME("stop_requested")); _stop_and_notify(); } else if (p_msg == "performance:profile_names") { Vector monitors; monitors.resize(p_data.size()); for (int i = 0; i < p_data.size(); i++) { ERR_FAIL_COND(p_data[i].get_type() != Variant::STRING_NAME); monitors.set(i, p_data[i]); } performance_profiler->update_monitors(monitors); } else if (p_msg == "filesystem:update_file") { ERR_FAIL_COND(p_data.is_empty()); if (EditorFileSystem::get_singleton()) { EditorFileSystem::get_singleton()->update_file(p_data[0]); } } else { int colon_index = p_msg.find_char(':'); ERR_FAIL_COND_MSG(colon_index < 1, "Invalid message received"); bool parsed = EditorDebuggerNode::get_singleton()->plugins_capture(this, p_msg, p_data); if (!parsed) { WARN_PRINT("unknown message " + p_msg); } } } void ScriptEditorDebugger::_set_reason_text(const String &p_reason, MessageType p_type) { switch (p_type) { case MESSAGE_ERROR: reason->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("error_color"), EditorStringName(Editor))); break; case MESSAGE_WARNING: reason->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("warning_color"), EditorStringName(Editor))); break; default: reason->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("success_color"), EditorStringName(Editor))); } reason->set_text(p_reason); const PackedInt32Array boundaries = TS->string_get_word_breaks(p_reason, "", 80); PackedStringArray lines; for (int i = 0; i < boundaries.size(); i += 2) { const int start = boundaries[i]; const int end = boundaries[i + 1]; lines.append(p_reason.substr(start, end - start)); } reason->set_tooltip_text(String("\n").join(lines)); } void ScriptEditorDebugger::_notification(int p_what) { switch (p_what) { case NOTIFICATION_ENTER_TREE: { le_set->connect(SceneStringName(pressed), callable_mp(this, &ScriptEditorDebugger::_live_edit_set)); le_clear->connect(SceneStringName(pressed), callable_mp(this, &ScriptEditorDebugger::_live_edit_clear)); error_tree->connect(SceneStringName(item_selected), callable_mp(this, &ScriptEditorDebugger::_error_selected)); error_tree->connect("item_activated", callable_mp(this, &ScriptEditorDebugger::_error_activated)); breakpoints_tree->connect("item_activated", callable_mp(this, &ScriptEditorDebugger::_breakpoint_tree_clicked)); [[fallthrough]]; } case NOTIFICATION_THEME_CHANGED: { tabs->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("DebuggerPanel"), EditorStringName(EditorStyles))); skip_breakpoints->set_icon(get_editor_theme_icon(skip_breakpoints_value ? SNAME("DebugSkipBreakpointsOn") : SNAME("DebugSkipBreakpointsOff"))); copy->set_icon(get_editor_theme_icon(SNAME("ActionCopy"))); step->set_icon(get_editor_theme_icon(SNAME("DebugStep"))); next->set_icon(get_editor_theme_icon(SNAME("DebugNext"))); dobreak->set_icon(get_editor_theme_icon(SNAME("Pause"))); docontinue->set_icon(get_editor_theme_icon(SNAME("DebugContinue"))); vmem_refresh->set_icon(get_editor_theme_icon(SNAME("Reload"))); vmem_export->set_icon(get_editor_theme_icon(SNAME("Save"))); search->set_right_icon(get_editor_theme_icon(SNAME("Search"))); reason->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("error_color"), EditorStringName(Editor))); TreeItem *error_root = error_tree->get_root(); if (error_root) { TreeItem *error = error_root->get_first_child(); while (error) { if (error->has_meta("_is_warning")) { error->set_icon(0, get_editor_theme_icon(SNAME("Warning"))); error->set_custom_color(0, get_theme_color(SNAME("warning_color"), EditorStringName(Editor))); error->set_custom_color(1, get_theme_color(SNAME("warning_color"), EditorStringName(Editor))); } else if (error->has_meta("_is_error")) { error->set_icon(0, get_editor_theme_icon(SNAME("Error"))); error->set_custom_color(0, get_theme_color(SNAME("error_color"), EditorStringName(Editor))); error->set_custom_color(1, get_theme_color(SNAME("error_color"), EditorStringName(Editor))); } error = error->get_next(); } } } break; case NOTIFICATION_PROCESS: { if (is_session_active()) { peer->poll(); if (camera_override == CameraOverride::OVERRIDE_2D) { Dictionary state = CanvasItemEditor::get_singleton()->get_state(); float zoom = state["zoom"]; Point2 offset = state["ofs"]; Transform2D transform; transform.scale_basis(Size2(zoom, zoom)); transform.columns[2] = -offset * zoom; Array msg; msg.push_back(transform); _put_msg("scene:override_camera_2D:transform", msg); } else if (camera_override >= CameraOverride::OVERRIDE_3D_1) { int viewport_idx = camera_override - CameraOverride::OVERRIDE_3D_1; Node3DEditorViewport *viewport = Node3DEditor::get_singleton()->get_editor_viewport(viewport_idx); Camera3D *const cam = viewport->get_camera_3d(); Array msg; msg.push_back(cam->get_camera_transform()); if (cam->get_projection() == Camera3D::PROJECTION_ORTHOGONAL) { msg.push_back(false); msg.push_back(cam->get_size()); } else { msg.push_back(true); msg.push_back(cam->get_fov()); } msg.push_back(cam->get_near()); msg.push_back(cam->get_far()); _put_msg("scene:override_camera_3D:transform", msg); } if (is_breaked() && can_request_idle_draw) { _put_msg("servers:draw", Array()); can_request_idle_draw = false; } } const uint64_t until = OS::get_singleton()->get_ticks_msec() + 20; while (peer.is_valid() && peer->has_message()) { Array arr = peer->get_message(); if (arr.size() != 3 || arr[0].get_type() != Variant::STRING || arr[1].get_type() != Variant::INT || arr[2].get_type() != Variant::ARRAY) { _stop_and_notify(); ERR_FAIL_MSG("Invalid message format received from peer"); } _parse_message(arr[0], arr[1], arr[2]); if (OS::get_singleton()->get_ticks_msec() > until) { break; } } if (!is_session_active()) { _stop_and_notify(); break; }; } break; } } void ScriptEditorDebugger::_clear_execution() { TreeItem *ti = stack_dump->get_selected(); if (!ti) { return; } Dictionary d = ti->get_metadata(0); stack_script = ResourceLoader::load(d["file"]); emit_signal(SNAME("clear_execution"), stack_script); stack_script.unref(); stack_dump->clear(); inspector->clear_stack_variables(); } void ScriptEditorDebugger::_set_breakpoint(const String &p_file, const int &p_line, const bool &p_enabled) { Ref