1/**************************************************************************/
2/* engine_debugger.cpp */
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#include "engine_debugger.h"
32
33#include "core/debugger/local_debugger.h"
34#include "core/debugger/remote_debugger.h"
35#include "core/debugger/remote_debugger_peer.h"
36#include "core/debugger/script_debugger.h"
37#include "core/os/os.h"
38
39EngineDebugger *EngineDebugger::singleton = nullptr;
40ScriptDebugger *EngineDebugger::script_debugger = nullptr;
41
42HashMap<StringName, EngineDebugger::Profiler> EngineDebugger::profilers;
43HashMap<StringName, EngineDebugger::Capture> EngineDebugger::captures;
44HashMap<String, EngineDebugger::CreatePeerFunc> EngineDebugger::protocols;
45
46void (*EngineDebugger::allow_focus_steal_fn)();
47
48void EngineDebugger::register_profiler(const StringName &p_name, const Profiler &p_func) {
49 ERR_FAIL_COND_MSG(profilers.has(p_name), "Profiler already registered: " + p_name);
50 profilers.insert(p_name, p_func);
51}
52
53void EngineDebugger::unregister_profiler(const StringName &p_name) {
54 ERR_FAIL_COND_MSG(!profilers.has(p_name), "Profiler not registered: " + p_name);
55 Profiler &p = profilers[p_name];
56 if (p.active && p.toggle) {
57 p.toggle(p.data, false, Array());
58 p.active = false;
59 }
60 profilers.erase(p_name);
61}
62
63void EngineDebugger::register_message_capture(const StringName &p_name, Capture p_func) {
64 ERR_FAIL_COND_MSG(captures.has(p_name), "Capture already registered: " + p_name);
65 captures.insert(p_name, p_func);
66}
67
68void EngineDebugger::unregister_message_capture(const StringName &p_name) {
69 ERR_FAIL_COND_MSG(!captures.has(p_name), "Capture not registered: " + p_name);
70 captures.erase(p_name);
71}
72
73void EngineDebugger::register_uri_handler(const String &p_protocol, CreatePeerFunc p_func) {
74 ERR_FAIL_COND_MSG(protocols.has(p_protocol), "Protocol handler already registered: " + p_protocol);
75 protocols.insert(p_protocol, p_func);
76}
77
78void EngineDebugger::profiler_enable(const StringName &p_name, bool p_enabled, const Array &p_opts) {
79 ERR_FAIL_COND_MSG(!profilers.has(p_name), "Can't change profiler state, no profiler: " + p_name);
80 Profiler &p = profilers[p_name];
81 if (p.toggle) {
82 p.toggle(p.data, p_enabled, p_opts);
83 }
84 p.active = p_enabled;
85}
86
87void EngineDebugger::profiler_add_frame_data(const StringName &p_name, const Array &p_data) {
88 ERR_FAIL_COND_MSG(!profilers.has(p_name), "Can't add frame data, no profiler: " + p_name);
89 Profiler &p = profilers[p_name];
90 if (p.add) {
91 p.add(p.data, p_data);
92 }
93}
94
95bool EngineDebugger::is_profiling(const StringName &p_name) {
96 return profilers.has(p_name) && profilers[p_name].active;
97}
98
99bool EngineDebugger::has_profiler(const StringName &p_name) {
100 return profilers.has(p_name);
101}
102
103bool EngineDebugger::has_capture(const StringName &p_name) {
104 return captures.has(p_name);
105}
106
107Error EngineDebugger::capture_parse(const StringName &p_name, const String &p_msg, const Array &p_args, bool &r_captured) {
108 r_captured = false;
109 ERR_FAIL_COND_V_MSG(!captures.has(p_name), ERR_UNCONFIGURED, "Capture not registered: " + p_name);
110 const Capture &cap = captures[p_name];
111 return cap.capture(cap.data, p_msg, p_args, r_captured);
112}
113
114void EngineDebugger::iteration(uint64_t p_frame_ticks, uint64_t p_process_ticks, uint64_t p_physics_ticks, double p_physics_frame_time) {
115 frame_time = USEC_TO_SEC(p_frame_ticks);
116 process_time = USEC_TO_SEC(p_process_ticks);
117 physics_time = USEC_TO_SEC(p_physics_ticks);
118 physics_frame_time = p_physics_frame_time;
119 // Notify tick to running profilers
120 for (KeyValue<StringName, Profiler> &E : profilers) {
121 Profiler &p = E.value;
122 if (!p.active || !p.tick) {
123 continue;
124 }
125 p.tick(p.data, frame_time, process_time, physics_time, physics_frame_time);
126 }
127 singleton->poll_events(true);
128}
129
130void EngineDebugger::initialize(const String &p_uri, bool p_skip_breakpoints, Vector<String> p_breakpoints, void (*p_allow_focus_steal_fn)()) {
131 register_uri_handler("tcp://", RemoteDebuggerPeerTCP::create); // TCP is the default protocol. Platforms/modules can add more.
132 if (p_uri.is_empty()) {
133 return;
134 }
135 if (p_uri == "local://") {
136 singleton = memnew(LocalDebugger);
137 script_debugger = memnew(ScriptDebugger);
138 // Tell the OS that we want to handle termination signals.
139 OS::get_singleton()->initialize_debugging();
140 } else if (p_uri.find("://") >= 0) {
141 const String proto = p_uri.substr(0, p_uri.find("://") + 3);
142 if (!protocols.has(proto)) {
143 return;
144 }
145 RemoteDebuggerPeer *peer = protocols[proto](p_uri);
146 if (!peer) {
147 return;
148 }
149 singleton = memnew(RemoteDebugger(Ref<RemoteDebuggerPeer>(peer)));
150 script_debugger = memnew(ScriptDebugger);
151 // Notify editor of our pid (to allow focus stealing).
152 Array msg;
153 msg.push_back(OS::get_singleton()->get_process_id());
154 singleton->send_message("set_pid", msg);
155 }
156 if (!singleton) {
157 return;
158 }
159
160 // There is a debugger, parse breakpoints.
161 ScriptDebugger *singleton_script_debugger = singleton->get_script_debugger();
162 singleton_script_debugger->set_skip_breakpoints(p_skip_breakpoints);
163
164 for (int i = 0; i < p_breakpoints.size(); i++) {
165 String bp = p_breakpoints[i];
166 int sp = bp.rfind(":");
167 ERR_CONTINUE_MSG(sp == -1, "Invalid breakpoint: '" + bp + "', expected file:line format.");
168
169 singleton_script_debugger->insert_breakpoint(bp.substr(sp + 1, bp.length()).to_int(), bp.substr(0, sp));
170 }
171
172 allow_focus_steal_fn = p_allow_focus_steal_fn;
173}
174
175void EngineDebugger::deinitialize() {
176 if (singleton) {
177 // Stop all profilers
178 for (const KeyValue<StringName, Profiler> &E : profilers) {
179 if (E.value.active) {
180 singleton->profiler_enable(E.key, false);
181 }
182 }
183
184 // Flush any remaining message
185 singleton->poll_events(false);
186
187 memdelete(singleton);
188 singleton = nullptr;
189 }
190
191 // Clear profilers/captures/protocol handlers.
192 profilers.clear();
193 captures.clear();
194 protocols.clear();
195}
196
197EngineDebugger::~EngineDebugger() {
198 if (script_debugger) {
199 memdelete(script_debugger);
200 }
201 script_debugger = nullptr;
202 singleton = nullptr;
203}
204