script_editor_debugger.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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;
  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 *dobreak = nullptr;
  111. Button *docontinue = nullptr;
  112. // Reference to "Remote" tab in scene tree. Needed by _live_edit_set and buttons state.
  113. // Each debugger should have it's tree in the future I guess.
  114. const Tree *editor_remote_tree = nullptr;
  115. HashMap<int, String> profiler_signature;
  116. Tree *vmem_tree = nullptr;
  117. Button *vmem_refresh = nullptr;
  118. Button *vmem_export = nullptr;
  119. LineEdit *vmem_total = nullptr;
  120. TextureRect *vmem_notice_icon = nullptr;
  121. Tree *stack_dump = nullptr;
  122. LineEdit *search = nullptr;
  123. OptionButton *threads = nullptr;
  124. EditorDebuggerInspector *inspector = nullptr;
  125. SceneDebuggerTree *scene_tree = nullptr;
  126. Ref<RemoteDebuggerPeer> peer;
  127. HashMap<NodePath, int> node_path_cache;
  128. int last_path_id = 0;
  129. HashMap<String, int> res_path_cache;
  130. EditorProfiler *profiler = nullptr;
  131. EditorVisualProfiler *visual_profiler = nullptr;
  132. EditorPerformanceProfiler *performance_profiler = nullptr;
  133. EditorExpressionEvaluator *expression_evaluator = nullptr;
  134. OS::ProcessID remote_pid = 0;
  135. bool move_to_foreground = true;
  136. bool can_request_idle_draw = false;
  137. bool live_debug = true;
  138. uint64_t debugging_thread_id = Thread::UNASSIGNED_ID;
  139. struct ThreadDebugged {
  140. String name;
  141. String error;
  142. bool can_debug = false;
  143. bool has_stackdump = false;
  144. uint32_t debug_order = 0;
  145. uint64_t thread_id = Thread::UNASSIGNED_ID; // for order
  146. };
  147. struct ThreadSort {
  148. bool operator()(const ThreadDebugged *a, const ThreadDebugged *b) const {
  149. return a->debug_order < b->debug_order;
  150. }
  151. };
  152. HashMap<uint64_t, ThreadDebugged> threads_debugged;
  153. bool thread_list_updating = false;
  154. void _select_thread(int p_index);
  155. bool debug_mute_audio = false;
  156. EditorDebuggerNode::CameraOverride camera_override;
  157. void _stack_dump_frame_selected();
  158. void _file_selected(const String &p_file);
  159. /// Message handler function for _parse_message.
  160. typedef void (ScriptEditorDebugger::*ParseMessageFunc)(uint64_t p_thread_id, const Array &p_data);
  161. static HashMap<String, ParseMessageFunc> parse_message_handlers;
  162. static void _init_parse_message_handlers();
  163. void _msg_debug_enter(uint64_t p_thread_id, const Array &p_data);
  164. void _msg_debug_exit(uint64_t p_thread_id, const Array &p_data);
  165. void _msg_set_pid(uint64_t p_thread_id, const Array &p_data);
  166. void _msg_scene_click_ctrl(uint64_t p_thread_id, const Array &p_data);
  167. void _msg_scene_scene_tree(uint64_t p_thread_id, const Array &p_data);
  168. void _msg_scene_inspect_objects(uint64_t p_thread_id, const Array &p_data);
  169. void _msg_servers_memory_usage(uint64_t p_thread_id, const Array &p_data);
  170. void _msg_servers_drawn(uint64_t p_thread_id, const Array &p_data);
  171. void _msg_stack_dump(uint64_t p_thread_id, const Array &p_data);
  172. void _msg_stack_frame_vars(uint64_t p_thread_id, const Array &p_data);
  173. void _msg_stack_frame_var(uint64_t p_thread_id, const Array &p_data);
  174. void _msg_output(uint64_t p_thread_id, const Array &p_data);
  175. void _msg_performance_profile_frame(uint64_t p_thread_id, const Array &p_data);
  176. void _msg_visual_hardware_info(uint64_t p_thread_id, const Array &p_data);
  177. void _msg_visual_profile_frame(uint64_t p_thread_id, const Array &p_data);
  178. void _msg_error(uint64_t p_thread_id, const Array &p_data);
  179. void _msg_servers_function_signature(uint64_t p_thread_id, const Array &p_data);
  180. void _msg_servers_profile_common(const Array &p_data, const bool p_final);
  181. void _msg_servers_profile_frame(uint64_t p_thread_id, const Array &p_data);
  182. void _msg_servers_profile_total(uint64_t p_thread_id, const Array &p_data);
  183. void _msg_request_quit(uint64_t p_thread_id, const Array &p_data);
  184. void _msg_remote_objects_selected(uint64_t p_thread_id, const Array &p_data);
  185. void _msg_remote_nothing_selected(uint64_t p_thread_id, const Array &p_data);
  186. void _msg_remote_selection_invalidated(uint64_t p_thread_id, const Array &p_data);
  187. void _msg_show_selection_limit_warning(uint64_t p_thread_id, const Array &p_data);
  188. void _msg_performance_profile_names(uint64_t p_thread_id, const Array &p_data);
  189. void _msg_filesystem_update_file(uint64_t p_thread_id, const Array &p_data);
  190. void _msg_evaluation_return(uint64_t p_thread_id, const Array &p_data);
  191. void _msg_window_title(uint64_t p_thread_id, const Array &p_data);
  192. void _msg_embed_suspend_toggle(uint64_t p_thread_id, const Array &p_data);
  193. void _msg_embed_next_frame(uint64_t p_thread_id, const Array &p_data);
  194. void _parse_message(const String &p_msg, uint64_t p_thread_id, const Array &p_data);
  195. void _set_reason_text(const String &p_reason, MessageType p_type);
  196. void _update_reason_content_height();
  197. void _update_buttons_state();
  198. void _remote_object_selected(ObjectID p_object);
  199. void _remote_objects_edited(const String &p_prop, const TypedDictionary<uint64_t, Variant> &p_values, const String &p_field);
  200. void _remote_object_property_updated(ObjectID p_id, const String &p_property);
  201. void _video_mem_request();
  202. void _video_mem_export();
  203. void _resources_reimported(const PackedStringArray &p_resources);
  204. int _get_node_path_cache(const NodePath &p_path);
  205. int _get_res_path_cache(const String &p_path);
  206. void _live_edit_set();
  207. void _live_edit_clear();
  208. void _method_changed(Object *p_base, const StringName &p_name, const Variant **p_args, int p_argcount);
  209. void _property_changed(Object *p_base, const StringName &p_property, const Variant &p_value);
  210. void _error_activated();
  211. void _error_selected();
  212. void _expand_errors_list();
  213. void _collapse_errors_list();
  214. void _vmem_item_activated();
  215. void _profiler_activate(bool p_enable, int p_profiler);
  216. void _profiler_seeked();
  217. void _clear_errors_list();
  218. void _breakpoints_item_rmb_selected(const Vector2 &p_pos, MouseButton p_button);
  219. void _error_tree_item_rmb_selected(const Vector2 &p_pos, MouseButton p_button);
  220. void _item_menu_id_pressed(int p_option);
  221. void _tab_changed(int p_tab);
  222. void _put_msg(const String &p_message, const Array &p_data, uint64_t p_thread_id = Thread::MAIN_ID);
  223. void _export_csv();
  224. void _clear_execution();
  225. void _stop_and_notify();
  226. void _set_breakpoint(const String &p_path, const int &p_line, const bool &p_enabled);
  227. void _clear_breakpoints();
  228. void _breakpoint_tree_clicked();
  229. String _format_frame_text(const ScriptLanguage::StackInfo *info);
  230. void _thread_debug_enter(uint64_t p_thread_id);
  231. protected:
  232. void _notification(int p_what);
  233. static void _bind_methods();
  234. public:
  235. enum EmbedShortcutAction {
  236. EMBED_SUSPEND_TOGGLE,
  237. EMBED_NEXT_FRAME,
  238. };
  239. void request_remote_objects(const TypedArray<uint64_t> &p_obj_ids, bool p_update_selection = true);
  240. void update_remote_object(ObjectID p_obj_id, const String &p_prop, const Variant &p_value, const String &p_field = "");
  241. void clear_inspector(bool p_send_msg = true);
  242. // Needed by _live_edit_set, buttons state.
  243. void set_editor_remote_tree(const Tree *p_tree) { editor_remote_tree = p_tree; }
  244. void request_remote_tree();
  245. const SceneDebuggerTree *get_remote_tree();
  246. void request_remote_evaluate(const String &p_expression, int p_stack_frame);
  247. void start(Ref<RemoteDebuggerPeer> p_peer);
  248. void stop();
  249. void debug_skip_breakpoints();
  250. void debug_ignore_error_breaks();
  251. void debug_copy();
  252. void debug_next();
  253. void debug_step();
  254. void debug_break();
  255. void debug_continue();
  256. bool is_breaked() const { return threads_debugged.size() > 0; }
  257. bool is_debuggable() const { return threads_debugged.size() > 0 && threads_debugged[debugging_thread_id].can_debug; }
  258. bool is_session_active() { return peer.is_valid() && peer->is_peer_connected(); }
  259. int get_remote_pid() const { return remote_pid; }
  260. bool is_move_to_foreground() const;
  261. void set_move_to_foreground(const bool &p_move_to_foreground);
  262. int get_error_count() const { return error_count; }
  263. int get_warning_count() const { return warning_count; }
  264. String get_stack_script_file() const;
  265. int get_stack_script_line() const;
  266. int get_stack_script_frame() const;
  267. bool request_stack_dump(const int &p_frame);
  268. void update_tabs();
  269. void clear_style();
  270. String get_var_value(const String &p_var) const;
  271. void save_node(ObjectID p_id, const String &p_file);
  272. void set_live_debugging(bool p_enable);
  273. void live_debug_create_node(const NodePath &p_parent, const String &p_type, const String &p_name);
  274. void live_debug_instantiate_node(const NodePath &p_parent, const String &p_path, const String &p_name);
  275. void live_debug_remove_node(const NodePath &p_at);
  276. void live_debug_remove_and_keep_node(const NodePath &p_at, ObjectID p_keep_id);
  277. void live_debug_restore_node(ObjectID p_id, const NodePath &p_at, int p_at_pos);
  278. void live_debug_duplicate_node(const NodePath &p_at, const String &p_new_name);
  279. void live_debug_reparent_node(const NodePath &p_at, const NodePath &p_new_place, const String &p_new_name, int p_at_pos);
  280. bool get_debug_mute_audio() const;
  281. void set_debug_mute_audio(bool p_mute);
  282. EditorDebuggerNode::CameraOverride get_camera_override() const;
  283. void set_camera_override(EditorDebuggerNode::CameraOverride p_override);
  284. void set_breakpoint(const String &p_path, int p_line, bool p_enabled);
  285. void update_live_edit_root();
  286. void reload_all_scripts();
  287. void reload_scripts(const Vector<String> &p_script_paths);
  288. bool is_skip_breakpoints() const;
  289. bool is_ignore_error_breaks() const;
  290. virtual Size2 get_minimum_size() const override;
  291. void add_debugger_tab(Control *p_control);
  292. void remove_debugger_tab(Control *p_control);
  293. int get_current_debugger_tab() const;
  294. void switch_to_debugger(int p_debugger_tab_idx);
  295. void send_message(const String &p_message, const Array &p_args);
  296. void toggle_profiler(const String &p_profiler, bool p_enable, const Array &p_data);
  297. ScriptEditorDebugger();
  298. ~ScriptEditorDebugger();
  299. };