| 1 | /**************************************************************************/ |
| 2 | /* script_editor_debugger.h */ |
| 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 | #ifndef SCRIPT_EDITOR_DEBUGGER_H |
| 32 | #define SCRIPT_EDITOR_DEBUGGER_H |
| 33 | |
| 34 | #include "core/object/script_language.h" |
| 35 | #include "core/os/os.h" |
| 36 | #include "editor/debugger/editor_debugger_inspector.h" |
| 37 | #include "editor/debugger/editor_debugger_node.h" |
| 38 | #include "editor/debugger/editor_debugger_server.h" |
| 39 | #include "scene/gui/button.h" |
| 40 | #include "scene/gui/margin_container.h" |
| 41 | |
| 42 | class Tree; |
| 43 | class LineEdit; |
| 44 | class TabContainer; |
| 45 | class RichTextLabel; |
| 46 | class TextureButton; |
| 47 | class AcceptDialog; |
| 48 | class TreeItem; |
| 49 | class HSplitContainer; |
| 50 | class ItemList; |
| 51 | class EditorProfiler; |
| 52 | class EditorFileDialog; |
| 53 | class EditorVisualProfiler; |
| 54 | class EditorPerformanceProfiler; |
| 55 | class SceneDebuggerTree; |
| 56 | class EditorDebuggerPlugin; |
| 57 | class DebugAdapterProtocol; |
| 58 | class DebugAdapterParser; |
| 59 | |
| 60 | class ScriptEditorDebugger : public MarginContainer { |
| 61 | GDCLASS(ScriptEditorDebugger, MarginContainer); |
| 62 | |
| 63 | friend class EditorDebuggerNode; |
| 64 | friend class DebugAdapterProtocol; |
| 65 | friend class DebugAdapterParser; |
| 66 | |
| 67 | private: |
| 68 | enum MessageType { |
| 69 | MESSAGE_ERROR, |
| 70 | MESSAGE_WARNING, |
| 71 | MESSAGE_SUCCESS, |
| 72 | }; |
| 73 | |
| 74 | enum ProfilerType { |
| 75 | PROFILER_VISUAL, |
| 76 | PROFILER_SCRIPTS_SERVERS |
| 77 | }; |
| 78 | |
| 79 | enum Actions { |
| 80 | ACTION_COPY_ERROR, |
| 81 | ACTION_OPEN_SOURCE, |
| 82 | ACTION_DELETE_BREAKPOINT, |
| 83 | ACTION_DELETE_BREAKPOINTS_IN_FILE, |
| 84 | ACTION_DELETE_ALL_BREAKPOINTS, |
| 85 | }; |
| 86 | |
| 87 | AcceptDialog *msgdialog = nullptr; |
| 88 | |
| 89 | LineEdit *clicked_ctrl = nullptr; |
| 90 | LineEdit *clicked_ctrl_type = nullptr; |
| 91 | LineEdit *live_edit_root = nullptr; |
| 92 | Button *le_set = nullptr; |
| 93 | Button *le_clear = nullptr; |
| 94 | Button *export_csv = nullptr; |
| 95 | |
| 96 | VBoxContainer *errors_tab = nullptr; |
| 97 | Tree *error_tree = nullptr; |
| 98 | Button *expand_all_button = nullptr; |
| 99 | Button *collapse_all_button = nullptr; |
| 100 | Button *clear_button = nullptr; |
| 101 | PopupMenu * = nullptr; |
| 102 | |
| 103 | Tree *breakpoints_tree = nullptr; |
| 104 | PopupMenu * = nullptr; |
| 105 | |
| 106 | EditorFileDialog *file_dialog = nullptr; |
| 107 | enum FileDialogPurpose { |
| 108 | SAVE_MONITORS_CSV, |
| 109 | SAVE_VRAM_CSV, |
| 110 | }; |
| 111 | FileDialogPurpose file_dialog_purpose; |
| 112 | |
| 113 | int error_count; |
| 114 | int warning_count; |
| 115 | |
| 116 | bool skip_breakpoints_value = false; |
| 117 | Ref<Script> stack_script; |
| 118 | |
| 119 | TabContainer *tabs = nullptr; |
| 120 | |
| 121 | Label *reason = nullptr; |
| 122 | |
| 123 | Button *skip_breakpoints = nullptr; |
| 124 | Button *copy = nullptr; |
| 125 | Button *step = nullptr; |
| 126 | Button *next = nullptr; |
| 127 | Button *dobreak = nullptr; |
| 128 | Button *docontinue = nullptr; |
| 129 | // Reference to "Remote" tab in scene tree. Needed by _live_edit_set and buttons state. |
| 130 | // Each debugger should have it's tree in the future I guess. |
| 131 | const Tree *editor_remote_tree = nullptr; |
| 132 | |
| 133 | HashMap<int, String> profiler_signature; |
| 134 | |
| 135 | Tree *vmem_tree = nullptr; |
| 136 | Button *vmem_refresh = nullptr; |
| 137 | Button *vmem_export = nullptr; |
| 138 | LineEdit *vmem_total = nullptr; |
| 139 | |
| 140 | Tree *stack_dump = nullptr; |
| 141 | LineEdit *search = nullptr; |
| 142 | OptionButton *threads = nullptr; |
| 143 | EditorDebuggerInspector *inspector = nullptr; |
| 144 | SceneDebuggerTree *scene_tree = nullptr; |
| 145 | |
| 146 | Ref<RemoteDebuggerPeer> peer; |
| 147 | |
| 148 | HashMap<NodePath, int> node_path_cache; |
| 149 | int last_path_id; |
| 150 | HashMap<String, int> res_path_cache; |
| 151 | |
| 152 | EditorProfiler *profiler = nullptr; |
| 153 | EditorVisualProfiler *visual_profiler = nullptr; |
| 154 | EditorPerformanceProfiler *performance_profiler = nullptr; |
| 155 | |
| 156 | OS::ProcessID remote_pid = 0; |
| 157 | bool move_to_foreground = true; |
| 158 | bool can_request_idle_draw = false; |
| 159 | |
| 160 | bool live_debug; |
| 161 | |
| 162 | uint64_t debugging_thread_id = Thread::UNASSIGNED_ID; |
| 163 | |
| 164 | struct ThreadDebugged { |
| 165 | String name; |
| 166 | String error; |
| 167 | bool can_debug = false; |
| 168 | bool has_stackdump = false; |
| 169 | uint32_t debug_order = 0; |
| 170 | uint64_t thread_id = Thread::UNASSIGNED_ID; // for order |
| 171 | }; |
| 172 | |
| 173 | struct ThreadSort { |
| 174 | bool operator()(const ThreadDebugged *a, const ThreadDebugged *b) const { |
| 175 | return a->debug_order < b->debug_order; |
| 176 | } |
| 177 | }; |
| 178 | |
| 179 | HashMap<uint64_t, ThreadDebugged> threads_debugged; |
| 180 | bool thread_list_updating = false; |
| 181 | |
| 182 | void _select_thread(int p_index); |
| 183 | |
| 184 | EditorDebuggerNode::CameraOverride camera_override; |
| 185 | |
| 186 | void _stack_dump_frame_selected(); |
| 187 | |
| 188 | void _file_selected(const String &p_file); |
| 189 | void _parse_message(const String &p_msg, uint64_t p_thread_id, const Array &p_data); |
| 190 | void _set_reason_text(const String &p_reason, MessageType p_type); |
| 191 | void _update_buttons_state(); |
| 192 | void _remote_object_selected(ObjectID p_object); |
| 193 | void _remote_object_edited(ObjectID, const String &p_prop, const Variant &p_value); |
| 194 | void _remote_object_property_updated(ObjectID p_id, const String &p_property); |
| 195 | |
| 196 | void _video_mem_request(); |
| 197 | void _video_mem_export(); |
| 198 | |
| 199 | int _get_node_path_cache(const NodePath &p_path); |
| 200 | |
| 201 | int _get_res_path_cache(const String &p_path); |
| 202 | |
| 203 | void _live_edit_set(); |
| 204 | void _live_edit_clear(); |
| 205 | |
| 206 | void _method_changed(Object *p_base, const StringName &p_name, const Variant **p_args, int p_argcount); |
| 207 | void _property_changed(Object *p_base, const StringName &p_property, const Variant &p_value); |
| 208 | |
| 209 | void _error_activated(); |
| 210 | void _error_selected(); |
| 211 | |
| 212 | void _expand_errors_list(); |
| 213 | void _collapse_errors_list(); |
| 214 | |
| 215 | void _profiler_activate(bool p_enable, int p_profiler); |
| 216 | void _profiler_seeked(); |
| 217 | |
| 218 | void _clear_errors_list(); |
| 219 | |
| 220 | void _breakpoints_item_rmb_selected(const Vector2 &p_pos, MouseButton p_button); |
| 221 | void _error_tree_item_rmb_selected(const Vector2 &p_pos, MouseButton p_button); |
| 222 | void (int p_option); |
| 223 | void _tab_changed(int p_tab); |
| 224 | |
| 225 | void _put_msg(String p_message, Array p_data, uint64_t p_thread_id = Thread::MAIN_ID); |
| 226 | void _export_csv(); |
| 227 | |
| 228 | void _clear_execution(); |
| 229 | void _stop_and_notify(); |
| 230 | |
| 231 | void _set_breakpoint(const String &p_path, const int &p_line, const bool &p_enabled); |
| 232 | void _clear_breakpoints(); |
| 233 | |
| 234 | void _breakpoint_tree_clicked(); |
| 235 | |
| 236 | String _format_frame_text(const ScriptLanguage::StackInfo *info); |
| 237 | |
| 238 | void _thread_debug_enter(uint64_t p_thread_id); |
| 239 | |
| 240 | protected: |
| 241 | void _notification(int p_what); |
| 242 | static void _bind_methods(); |
| 243 | |
| 244 | public: |
| 245 | void request_remote_object(ObjectID p_obj_id); |
| 246 | void update_remote_object(ObjectID p_obj_id, const String &p_prop, const Variant &p_value); |
| 247 | Object *get_remote_object(ObjectID p_id); |
| 248 | |
| 249 | // Needed by _live_edit_set, buttons state. |
| 250 | void set_editor_remote_tree(const Tree *p_tree) { editor_remote_tree = p_tree; } |
| 251 | |
| 252 | void request_remote_tree(); |
| 253 | const SceneDebuggerTree *get_remote_tree(); |
| 254 | |
| 255 | void start(Ref<RemoteDebuggerPeer> p_peer); |
| 256 | void stop(); |
| 257 | |
| 258 | void debug_skip_breakpoints(); |
| 259 | void debug_copy(); |
| 260 | |
| 261 | void debug_next(); |
| 262 | void debug_step(); |
| 263 | void debug_break(); |
| 264 | void debug_continue(); |
| 265 | bool is_breaked() const { return threads_debugged.size() > 0; } |
| 266 | bool is_debuggable() const { return threads_debugged.size() > 0 && threads_debugged[debugging_thread_id].can_debug; } |
| 267 | bool is_session_active() { return peer.is_valid() && peer->is_peer_connected(); } |
| 268 | int get_remote_pid() const { return remote_pid; } |
| 269 | |
| 270 | bool is_move_to_foreground() const; |
| 271 | void set_move_to_foreground(const bool &p_move_to_foreground); |
| 272 | |
| 273 | int get_error_count() const { return error_count; } |
| 274 | int get_warning_count() const { return warning_count; } |
| 275 | String get_stack_script_file() const; |
| 276 | int get_stack_script_line() const; |
| 277 | int get_stack_script_frame() const; |
| 278 | |
| 279 | bool request_stack_dump(const int &p_frame); |
| 280 | |
| 281 | void update_tabs(); |
| 282 | void clear_style(); |
| 283 | String get_var_value(const String &p_var) const; |
| 284 | |
| 285 | void save_node(ObjectID p_id, const String &p_file); |
| 286 | void set_live_debugging(bool p_enable); |
| 287 | |
| 288 | void live_debug_create_node(const NodePath &p_parent, const String &p_type, const String &p_name); |
| 289 | void live_debug_instantiate_node(const NodePath &p_parent, const String &p_path, const String &p_name); |
| 290 | void live_debug_remove_node(const NodePath &p_at); |
| 291 | void live_debug_remove_and_keep_node(const NodePath &p_at, ObjectID p_keep_id); |
| 292 | void live_debug_restore_node(ObjectID p_id, const NodePath &p_at, int p_at_pos); |
| 293 | void live_debug_duplicate_node(const NodePath &p_at, const String &p_new_name); |
| 294 | void live_debug_reparent_node(const NodePath &p_at, const NodePath &p_new_place, const String &p_new_name, int p_at_pos); |
| 295 | |
| 296 | EditorDebuggerNode::CameraOverride get_camera_override() const; |
| 297 | void set_camera_override(EditorDebuggerNode::CameraOverride p_override); |
| 298 | |
| 299 | void set_breakpoint(const String &p_path, int p_line, bool p_enabled); |
| 300 | |
| 301 | void update_live_edit_root(); |
| 302 | |
| 303 | void reload_scripts(); |
| 304 | |
| 305 | bool is_skip_breakpoints(); |
| 306 | |
| 307 | virtual Size2 get_minimum_size() const override; |
| 308 | |
| 309 | void add_debugger_tab(Control *p_control); |
| 310 | void remove_debugger_tab(Control *p_control); |
| 311 | int get_current_debugger_tab() const; |
| 312 | void switch_to_debugger(int p_debugger_tab_idx); |
| 313 | |
| 314 | void send_message(const String &p_message, const Array &p_args); |
| 315 | void toggle_profiler(const String &p_profiler, bool p_enable, const Array &p_data); |
| 316 | |
| 317 | ScriptEditorDebugger(); |
| 318 | ~ScriptEditorDebugger(); |
| 319 | }; |
| 320 | |
| 321 | #endif // SCRIPT_EDITOR_DEBUGGER_H |
| 322 | |