script_editor_debugger.h 14 KB

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