Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/debugger/engine_debugger.h
10277 views
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
#pragma once
32
33
#include "core/string/string_name.h"
34
#include "core/string/ustring.h"
35
#include "core/templates/hash_map.h"
36
#include "core/templates/vector.h"
37
#include "core/variant/array.h"
38
39
class RemoteDebuggerPeer;
40
class ScriptDebugger;
41
42
class EngineDebugger {
43
public:
44
typedef void (*ProfilingToggle)(void *p_user, bool p_enable, const Array &p_opts);
45
typedef void (*ProfilingTick)(void *p_user, double p_frame_time, double p_process_time, double p_physics_time, double p_physics_frame_time);
46
typedef void (*ProfilingAdd)(void *p_user, const Array &p_arr);
47
48
typedef Error (*CaptureFunc)(void *p_user, const String &p_msg, const Array &p_args, bool &r_captured);
49
50
typedef RemoteDebuggerPeer *(*CreatePeerFunc)(const String &p_uri);
51
52
class Profiler {
53
friend class EngineDebugger;
54
55
ProfilingToggle toggle = nullptr;
56
ProfilingAdd add = nullptr;
57
ProfilingTick tick = nullptr;
58
void *data = nullptr;
59
bool active = false;
60
61
public:
62
Profiler() {}
63
Profiler(void *p_data, ProfilingToggle p_toggle, ProfilingAdd p_add, ProfilingTick p_tick) {
64
data = p_data;
65
toggle = p_toggle;
66
add = p_add;
67
tick = p_tick;
68
}
69
};
70
71
class Capture {
72
friend class EngineDebugger;
73
74
CaptureFunc capture = nullptr;
75
void *data = nullptr;
76
77
public:
78
Capture() {}
79
Capture(void *p_data, CaptureFunc p_capture) {
80
data = p_data;
81
capture = p_capture;
82
}
83
};
84
85
private:
86
double frame_time = 0.0;
87
double process_time = 0.0;
88
double physics_time = 0.0;
89
double physics_frame_time = 0.0;
90
91
uint32_t poll_every = 0;
92
93
protected:
94
static inline EngineDebugger *singleton = nullptr;
95
static inline ScriptDebugger *script_debugger = nullptr;
96
97
static inline HashMap<StringName, Profiler> profilers;
98
static inline HashMap<StringName, Capture> captures;
99
static inline HashMap<String, CreatePeerFunc> protocols;
100
101
static void (*allow_focus_steal_fn)();
102
103
public:
104
_FORCE_INLINE_ static EngineDebugger *get_singleton() { return singleton; }
105
_FORCE_INLINE_ static bool is_active() { return singleton != nullptr && script_debugger != nullptr; }
106
107
_FORCE_INLINE_ static ScriptDebugger *get_script_debugger() { return script_debugger; }
108
109
static void initialize(const String &p_uri, bool p_skip_breakpoints, bool p_ignore_error_breaks, const Vector<String> &p_breakpoints, void (*p_allow_focus_steal_fn)());
110
static void deinitialize();
111
static void register_profiler(const StringName &p_name, const Profiler &p_profiler);
112
static void unregister_profiler(const StringName &p_name);
113
static bool is_profiling(const StringName &p_name);
114
static bool has_profiler(const StringName &p_name);
115
static void profiler_add_frame_data(const StringName &p_name, const Array &p_data);
116
117
static void register_message_capture(const StringName &p_name, Capture p_func);
118
static void unregister_message_capture(const StringName &p_name);
119
static bool has_capture(const StringName &p_name);
120
121
static void register_uri_handler(const String &p_protocol, CreatePeerFunc p_func);
122
123
void iteration(uint64_t p_frame_ticks, uint64_t p_process_ticks, uint64_t p_physics_ticks, double p_physics_frame_time);
124
void profiler_enable(const StringName &p_name, bool p_enabled, const Array &p_opts = Array());
125
Error capture_parse(const StringName &p_name, const String &p_msg, const Array &p_args, bool &r_captured);
126
127
void line_poll() {
128
// 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.
129
if (unlikely(poll_every % 2048) == 0) {
130
poll_events(false);
131
}
132
poll_every++;
133
}
134
135
virtual void poll_events(bool p_is_idle) {}
136
virtual void send_message(const String &p_msg, const Array &p_data) = 0;
137
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;
138
virtual void debug(bool p_can_continue = true, bool p_is_error_breakpoint = false) = 0;
139
140
virtual ~EngineDebugger();
141
};
142
143