1 | /**************************************************************************/ |
2 | /* engine_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 ENGINE_DEBUGGER_H |
32 | #define ENGINE_DEBUGGER_H |
33 | |
34 | #include "core/string/string_name.h" |
35 | #include "core/string/ustring.h" |
36 | #include "core/templates/hash_map.h" |
37 | #include "core/templates/vector.h" |
38 | #include "core/variant/array.h" |
39 | #include "core/variant/variant.h" |
40 | |
41 | class RemoteDebuggerPeer; |
42 | class ScriptDebugger; |
43 | |
44 | class EngineDebugger { |
45 | public: |
46 | typedef void (*ProfilingToggle)(void *p_user, bool p_enable, const Array &p_opts); |
47 | typedef void (*ProfilingTick)(void *p_user, double p_frame_time, double p_process_time, double p_physics_time, double p_physics_frame_time); |
48 | typedef void (*ProfilingAdd)(void *p_user, const Array &p_arr); |
49 | |
50 | typedef Error (*CaptureFunc)(void *p_user, const String &p_msg, const Array &p_args, bool &r_captured); |
51 | |
52 | typedef RemoteDebuggerPeer *(*CreatePeerFunc)(const String &p_uri); |
53 | |
54 | class Profiler { |
55 | friend class EngineDebugger; |
56 | |
57 | ProfilingToggle toggle = nullptr; |
58 | ProfilingAdd add = nullptr; |
59 | ProfilingTick tick = nullptr; |
60 | void *data = nullptr; |
61 | bool active = false; |
62 | |
63 | public: |
64 | Profiler() {} |
65 | Profiler(void *p_data, ProfilingToggle p_toggle, ProfilingAdd p_add, ProfilingTick p_tick) { |
66 | data = p_data; |
67 | toggle = p_toggle; |
68 | add = p_add; |
69 | tick = p_tick; |
70 | } |
71 | }; |
72 | |
73 | class Capture { |
74 | friend class EngineDebugger; |
75 | |
76 | CaptureFunc capture = nullptr; |
77 | void *data = nullptr; |
78 | |
79 | public: |
80 | Capture() {} |
81 | Capture(void *p_data, CaptureFunc p_capture) { |
82 | data = p_data; |
83 | capture = p_capture; |
84 | } |
85 | }; |
86 | |
87 | private: |
88 | double frame_time = 0.0; |
89 | double process_time = 0.0; |
90 | double physics_time = 0.0; |
91 | double physics_frame_time = 0.0; |
92 | |
93 | uint32_t poll_every = 0; |
94 | |
95 | protected: |
96 | static EngineDebugger *singleton; |
97 | static ScriptDebugger *script_debugger; |
98 | |
99 | static HashMap<StringName, Profiler> profilers; |
100 | static HashMap<StringName, Capture> captures; |
101 | static HashMap<String, CreatePeerFunc> protocols; |
102 | |
103 | static void (*allow_focus_steal_fn)(); |
104 | |
105 | public: |
106 | _FORCE_INLINE_ static EngineDebugger *get_singleton() { return singleton; } |
107 | _FORCE_INLINE_ static bool is_active() { return singleton != nullptr && script_debugger != nullptr; } |
108 | |
109 | _FORCE_INLINE_ static ScriptDebugger *get_script_debugger() { return script_debugger; }; |
110 | |
111 | static void initialize(const String &p_uri, bool p_skip_breakpoints, Vector<String> p_breakpoints, void (*p_allow_focus_steal_fn)()); |
112 | static void deinitialize(); |
113 | static void register_profiler(const StringName &p_name, const Profiler &p_profiler); |
114 | static void unregister_profiler(const StringName &p_name); |
115 | static bool is_profiling(const StringName &p_name); |
116 | static bool has_profiler(const StringName &p_name); |
117 | static void profiler_add_frame_data(const StringName &p_name, const Array &p_data); |
118 | |
119 | static void register_message_capture(const StringName &p_name, Capture p_func); |
120 | static void unregister_message_capture(const StringName &p_name); |
121 | static bool has_capture(const StringName &p_name); |
122 | |
123 | static void register_uri_handler(const String &p_protocol, CreatePeerFunc p_func); |
124 | |
125 | void iteration(uint64_t p_frame_ticks, uint64_t p_process_ticks, uint64_t p_physics_ticks, double p_physics_frame_time); |
126 | void profiler_enable(const StringName &p_name, bool p_enabled, const Array &p_opts = Array()); |
127 | Error capture_parse(const StringName &p_name, const String &p_msg, const Array &p_args, bool &r_captured); |
128 | |
129 | void line_poll() { |
130 | // The purpose of this is just processing events every now and then when the script might get too busy otherwise bugs like infinite loops can't be caught. |
131 | if (unlikely(poll_every % 2048) == 0) { |
132 | poll_events(false); |
133 | } |
134 | poll_every++; |
135 | } |
136 | |
137 | virtual void poll_events(bool p_is_idle) {} |
138 | virtual void send_message(const String &p_msg, const Array &p_data) = 0; |
139 | virtual void send_error(const String &p_func, const String &p_file, int p_line, const String &p_err, const String &p_descr, bool p_editor_notify, ErrorHandlerType p_type) = 0; |
140 | virtual void debug(bool p_can_continue = true, bool p_is_error_breakpoint = false) = 0; |
141 | |
142 | virtual ~EngineDebugger(); |
143 | }; |
144 | |
145 | #endif // ENGINE_DEBUGGER_H |
146 | |