Debian Codegraph @

core/error/error_macros.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* error_macros.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 "error_macros.h"
32
33 #include "core/core_globals.h"
34 #include "core/io/logger.h"
35 #include "core/object/object_id.h"
36 #include "core/object/script_language.h"
37 #include "core/os/os.h"
38 #include "core/string/ustring.h"
39
40 // Optional physics interpolation warnings try to include the path to the relevant node.
41 #if defined(DEBUG_ENABLED) && defined(TOOLS_ENABLED)
42 #include "core/config/project_settings.h"
43 #include "scene/main/node.h"
44 #endif
45
46 static ErrorHandlerList *error_handler_list = nullptr;
47 static thread_local bool is_printing_error = false;
48
49 static void _err_print_fallback(const char *p_function, const char *p_file, int p_line, const char *p_error_details, ErrorHandlerType p_type, bool p_reentrance) {
50 if (p_reentrance) {
51 fprintf(stderr, "While attempting to print an error, another error was printed:\n");
52 }
53
54 fprintf(stderr, "%s: %s\n", _error_handler_type_string(p_type), p_error_details);
55
56 if (p_function && p_file) {
57 fprintf(stderr, " at: %s (%s:%i)\n", p_function, p_file, p_line);
58 }
59 }
60
61 void add_error_handler(ErrorHandlerList *p_handler) {
62 // If p_handler is already in error_handler_list
63 // we'd better remove it first then we can add it.
64 // This prevent cyclic redundancy.
65 remove_error_handler(p_handler);
66
67 _global_lock();
68
69 p_handler->next = error_handler_list;
70 error_handler_list = p_handler;
71
72 _global_unlock();
73 }
74
75 void remove_error_handler(const ErrorHandlerList *p_handler) {
76 _global_lock();
77
78 ErrorHandlerList *prev = nullptr;
79 ErrorHandlerList *l = error_handler_list;
80
81 while (l) {
82 if (l == p_handler) {
83 if (prev) {
84 prev->next = l->next;
85 } else {
86 error_handler_list = l->next;
87 }
88 break;
89 }
90 prev = l;
91 l = l->next;
92 }
93
94 _global_unlock();
95 }
96
97 // Errors without messages.
98 void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, bool p_editor_notify, ErrorHandlerType p_type) {
99 _err_print_error(p_function, p_file, p_line, p_error, "", p_editor_notify, p_type);
100 }
101
102 void _err_print_error(const char *p_function, const char *p_file, int p_line, const String &p_error, bool p_editor_notify, ErrorHandlerType p_type) {
103 _err_print_error(p_function, p_file, p_line, p_error.utf8().get_data(), "", p_editor_notify, p_type);
104 }
105
106 // Main error printing function.
107 void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, const char *p_message, bool p_editor_notify, ErrorHandlerType p_type) {
108 if (!CoreGlobals::print_ready) {
109 const char *err_details = (p_message && *p_message) ? p_message : p_error;
110 _err_print_fallback(p_function, p_file, p_line, err_details, p_type, false);
111 return;
112 }
113
114 if (is_printing_error) {
115 // Fallback if we're already printing an error, to prevent infinite recursion.
116 const char *err_details = (p_message && *p_message) ? p_message : p_error;
117 _err_print_fallback(p_function, p_file, p_line, err_details, p_type, true);
118 return;
119 }
120
121 is_printing_error = true;
122
123 if (OS::get_singleton()) {
124 OS::get_singleton()->print_error(p_function, p_file, p_line, p_error, p_message, p_editor_notify, (Logger::ErrorType)p_type, ScriptServer::capture_script_backtraces(false));
125 } else {
126 // Fallback if errors happen before OS init or after it's destroyed.
127 const char *err_details = (p_message && *p_message) ? p_message : p_error;
128 _err_print_fallback(p_function, p_file, p_line, err_details, p_type, false);
129 }
130
131 _global_lock();
132
133 ErrorHandlerList *l = error_handler_list;
134 while (l) {
135 l->errfunc(l->userdata, p_function, p_file, p_line, p_error, p_message, p_editor_notify, p_type);
136 l = l->next;
137 }
138
139 _global_unlock();
140
141 is_printing_error = false;
142 }
143
144 // For printing errors when we may crash at any point, so we must flush ASAP a lot of lines
145 // but we don't want to make it noisy by printing lots of file & line info (because it's already
146 // been printing by a preceding _err_print_error).
147 void _err_print_error_asap(const String &p_error, ErrorHandlerType p_type) {
148 const char *err_details = p_error.utf8().get_data();
149
150 if (!CoreGlobals::print_ready) {
151 _err_print_fallback(nullptr, nullptr, 0, err_details, p_type, false);
152 return;
153 }
154
155 if (is_printing_error) {
156 // Fallback if we're already printing an error, to prevent infinite recursion.
157 _err_print_fallback(nullptr, nullptr, 0, err_details, p_type, true);
158 return;
159 }
160
161 is_printing_error = true;
162
163 if (OS::get_singleton()) {
164 OS::get_singleton()->printerr("%s: %s\n", _error_handler_type_string(p_type), err_details);
165 } else {
166 // Fallback if errors happen before OS init or after it's destroyed.
167 _err_print_fallback(nullptr, nullptr, 0, err_details, p_type, false);
168 }
169
170 _global_lock();
171
172 ErrorHandlerList *l = error_handler_list;
173 while (l) {
174 l->errfunc(l->userdata, "", "", 0, err_details, "", false, p_type);
175 l = l->next;
176 }
177
178 _global_unlock();
179
180 is_printing_error = false;
181 }
182
183 // Errors with message. (All combinations of p_error and p_message as String or char*.)
184 void _err_print_error(const char *p_function, const char *p_file, int p_line, const String &p_error, const char *p_message, bool p_editor_notify, ErrorHandlerType p_type) {
185 _err_print_error(p_function, p_file, p_line, p_error.utf8().get_data(), p_message, p_editor_notify, p_type);
186 }
187
188 void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, const String &p_message, bool p_editor_notify, ErrorHandlerType p_type) {
189 _err_print_error(p_function, p_file, p_line, p_error, p_message.utf8().get_data(), p_editor_notify, p_type);
190 }
191
192 void _err_print_error(const char *p_function, const char *p_file, int p_line, const String &p_error, const String &p_message, bool p_editor_notify, ErrorHandlerType p_type) {
193 _err_print_error(p_function, p_file, p_line, p_error.utf8().get_data(), p_message.utf8().get_data(), p_editor_notify, p_type);
194 }
195
196 // Index errors. (All combinations of p_message as String or char*.)
197 void _err_print_index_error(const char *p_function, const char *p_file, int p_line, int64_t p_index, int64_t p_size, const char *p_index_str, const char *p_size_str, const char *p_message, bool p_editor_notify, bool p_fatal) {
198 String fstr(p_fatal ? "FATAL: " : "");
199 String err(fstr + "Index " + p_index_str + " = " + itos(p_index) + " is out of bounds (" + p_size_str + " = " + itos(p_size) + ").");
200 _err_print_error(p_function, p_file, p_line, err.utf8().get_data(), p_message, p_editor_notify, ERR_HANDLER_ERROR);
201 }
202
203 void _err_print_index_error(const char *p_function, const char *p_file, int p_line, int64_t p_index, int64_t p_size, const char *p_index_str, const char *p_size_str, const String &p_message, bool p_editor_notify, bool p_fatal) {
204 _err_print_index_error(p_function, p_file, p_line, p_index, p_size, p_index_str, p_size_str, p_message.utf8().get_data(), p_editor_notify, p_fatal);
205 }
206
207 void _err_flush_stdout() {
208 fflush(stdout);
209 }
210
211 // Prevent error spam by limiting the warnings to a certain frequency.
212 void _physics_interpolation_warning(const char *p_function, const char *p_file, int p_line, ObjectID p_id, const char *p_warn_string) {
213 #if defined(DEBUG_ENABLED) && defined(TOOLS_ENABLED)
214 const uint32_t warn_max = 2048;
215 const uint32_t warn_timeout_seconds = 15;
216
217 static uint32_t warn_count = warn_max;
218 static uint32_t warn_timeout = warn_timeout_seconds;
219
220 uint32_t time_now = UINT32_MAX;
221
222 if (warn_count) {
223 warn_count--;
224 }
225
226 if (!warn_count) {
227 time_now = OS::get_singleton()->get_ticks_msec() / 1000;
228 }
229
230 if ((warn_count == 0) && (time_now >= warn_timeout)) {
231 warn_count = warn_max;
232 warn_timeout = time_now + warn_timeout_seconds;
233
234 if (GLOBAL_GET("debug/settings/physics_interpolation/enable_warnings")) {
235 // UINT64_MAX means unused.
236 if (p_id.operator uint64_t() == UINT64_MAX) {
237 _err_print_error(p_function, p_file, p_line, "[Physics interpolation] " + String(p_warn_string) + " (possibly benign).", false, ERR_HANDLER_WARNING);
238 } else {
239 String node_name;
240 if (p_id.is_valid()) {
241 Node *node = ObjectDB::get_instance<Node>(p_id);
242 if (node && node->is_inside_tree()) {
243 node_name = "\"" + String(node->get_path()) + "\"";
244 } else {
245 node_name = "\"unknown\"";
246 }
247 }
248
249 _err_print_error(p_function, p_file, p_line, "[Physics interpolation] " + String(p_warn_string) + ": " + node_name + " (possibly benign).", false, ERR_HANDLER_WARNING);
250 }
251 }
252 }
253 #endif
254 }