Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/debugger/local_debugger.cpp
11351 views
1
/**************************************************************************/
2
/* local_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 "local_debugger.h"
32
33
#include "core/debugger/script_debugger.h"
34
#include "core/os/main_loop.h"
35
#include "core/os/os.h"
36
37
struct LocalDebugger::ScriptsProfiler {
38
struct ProfileInfoSort {
39
bool operator()(const ScriptLanguage::ProfilingInfo &A, const ScriptLanguage::ProfilingInfo &B) const {
40
return A.total_time > B.total_time;
41
}
42
};
43
44
double frame_time = 0;
45
uint64_t idle_accum = 0;
46
Vector<ScriptLanguage::ProfilingInfo> pinfo;
47
48
void toggle(bool p_enable, const Array &p_opts) {
49
if (p_enable) {
50
for (int i = 0; i < ScriptServer::get_language_count(); i++) {
51
ScriptServer::get_language(i)->profiling_start();
52
}
53
54
print_line("BEGIN PROFILING");
55
pinfo.resize(32768);
56
} else {
57
_print_frame_data(true);
58
for (int i = 0; i < ScriptServer::get_language_count(); i++) {
59
ScriptServer::get_language(i)->profiling_stop();
60
}
61
}
62
}
63
64
void tick(double p_frame_time, double p_process_time, double p_physics_time, double p_physics_frame_time) {
65
frame_time = p_frame_time;
66
_print_frame_data(false);
67
}
68
69
void _print_frame_data(bool p_accumulated) {
70
uint64_t diff = OS::get_singleton()->get_ticks_usec() - idle_accum;
71
72
if (!p_accumulated && diff < 1000000) { //show every one second
73
return;
74
}
75
76
idle_accum = OS::get_singleton()->get_ticks_usec();
77
78
int ofs = 0;
79
for (int i = 0; i < ScriptServer::get_language_count(); i++) {
80
if (p_accumulated) {
81
ofs += ScriptServer::get_language(i)->profiling_get_accumulated_data(&pinfo.write[ofs], pinfo.size() - ofs);
82
} else {
83
ofs += ScriptServer::get_language(i)->profiling_get_frame_data(&pinfo.write[ofs], pinfo.size() - ofs);
84
}
85
}
86
87
SortArray<ScriptLanguage::ProfilingInfo, ProfileInfoSort> sort;
88
sort.sort(pinfo.ptrw(), ofs);
89
90
// compute total script frame time
91
uint64_t script_time_us = 0;
92
for (int i = 0; i < ofs; i++) {
93
script_time_us += pinfo[i].self_time;
94
}
95
double script_time = USEC_TO_SEC(script_time_us);
96
double total_time = p_accumulated ? script_time : frame_time;
97
98
if (!p_accumulated) {
99
print_line("FRAME: total: " + rtos(total_time) + " script: " + rtos(script_time) + "/" + itos(script_time * 100 / total_time) + " %");
100
} else {
101
print_line("ACCUMULATED: total: " + rtos(total_time));
102
}
103
104
for (int i = 0; i < ofs; i++) {
105
print_line(itos(i) + ":" + pinfo[i].signature);
106
double tt = USEC_TO_SEC(pinfo[i].total_time);
107
double st = USEC_TO_SEC(pinfo[i].self_time);
108
print_line("\ttotal: " + rtos(tt) + "/" + itos(tt * 100 / total_time) + " % \tself: " + rtos(st) + "/" + itos(st * 100 / total_time) + " % tcalls: " + itos(pinfo[i].call_count));
109
}
110
}
111
112
ScriptsProfiler() {
113
idle_accum = OS::get_singleton()->get_ticks_usec();
114
}
115
};
116
117
void LocalDebugger::debug(bool p_can_continue, bool p_is_error_breakpoint) {
118
ScriptLanguage *script_lang = script_debugger->get_break_language();
119
120
if (!target_function.is_empty()) {
121
String current_function = script_lang->debug_get_stack_level_function(0);
122
if (current_function != target_function) {
123
script_debugger->set_depth(0);
124
script_debugger->set_lines_left(1);
125
return;
126
}
127
target_function = "";
128
}
129
130
print_line("\nDebugger Break, Reason: '" + script_lang->debug_get_error() + "'");
131
print_line("*Frame " + itos(0) + " - " + script_lang->debug_get_stack_level_source(0) + ":" + itos(script_lang->debug_get_stack_level_line(0)) + " in function '" + script_lang->debug_get_stack_level_function(0) + "'");
132
print_line("Enter \"help\" for assistance.");
133
int current_frame = 0;
134
int total_frames = script_lang->debug_get_stack_level_count();
135
while (true) {
136
OS::get_singleton()->print("debug> ");
137
String line = OS::get_singleton()->get_stdin_string().strip_edges();
138
139
// Cache options
140
String variable_prefix = options["variable_prefix"];
141
142
if (line.is_empty() && !feof(stdin)) {
143
print_line("\nDebugger Break, Reason: '" + script_lang->debug_get_error() + "'");
144
print_line("*Frame " + itos(current_frame) + " - " + script_lang->debug_get_stack_level_source(current_frame) + ":" + itos(script_lang->debug_get_stack_level_line(current_frame)) + " in function '" + script_lang->debug_get_stack_level_function(current_frame) + "'");
145
print_line("Enter \"help\" for assistance.");
146
} else if (line == "c" || line == "continue") {
147
break;
148
} else if (line == "bt" || line == "breakpoint") {
149
for (int i = 0; i < total_frames; i++) {
150
String cfi = (current_frame == i) ? "*" : " "; //current frame indicator
151
print_line(cfi + "Frame " + itos(i) + " - " + script_lang->debug_get_stack_level_source(i) + ":" + itos(script_lang->debug_get_stack_level_line(i)) + " in function '" + script_lang->debug_get_stack_level_function(i) + "'");
152
}
153
154
} else if (line.begins_with("fr") || line.begins_with("frame")) {
155
if (line.get_slice_count(" ") == 1) {
156
print_line("*Frame " + itos(current_frame) + " - " + script_lang->debug_get_stack_level_source(current_frame) + ":" + itos(script_lang->debug_get_stack_level_line(current_frame)) + " in function '" + script_lang->debug_get_stack_level_function(current_frame) + "'");
157
} else {
158
int frame = line.get_slicec(' ', 1).to_int();
159
if (frame < 0 || frame >= total_frames) {
160
print_line("Error: Invalid frame.");
161
} else {
162
current_frame = frame;
163
print_line("*Frame " + itos(frame) + " - " + script_lang->debug_get_stack_level_source(frame) + ":" + itos(script_lang->debug_get_stack_level_line(frame)) + " in function '" + script_lang->debug_get_stack_level_function(frame) + "'");
164
}
165
}
166
167
} else if (line.begins_with("set")) {
168
if (line.get_slice_count(" ") == 1) {
169
for (const KeyValue<String, String> &E : options) {
170
print_line("\t" + E.key + "=" + E.value);
171
}
172
173
} else {
174
String key_value = line.get_slicec(' ', 1);
175
int value_pos = key_value.find_char('=');
176
177
if (value_pos < 0) {
178
print_line("Error: Invalid set format. Use: set key=value");
179
} else {
180
String key = key_value.left(value_pos);
181
182
if (!options.has(key)) {
183
print_line("Error: Unknown option " + key);
184
} else {
185
// Allow explicit tab character
186
String value = key_value.substr(value_pos + 1).replace("\\t", "\t");
187
188
options[key] = value;
189
}
190
}
191
}
192
193
} else if (line == "lv" || line == "locals") {
194
List<String> locals;
195
List<Variant> values;
196
script_lang->debug_get_stack_level_locals(current_frame, &locals, &values);
197
print_variables(locals, values, variable_prefix);
198
199
} else if (line == "gv" || line == "globals") {
200
List<String> globals;
201
List<Variant> values;
202
script_lang->debug_get_globals(&globals, &values);
203
print_variables(globals, values, variable_prefix);
204
205
} else if (line == "mv" || line == "members") {
206
List<String> members;
207
List<Variant> values;
208
script_lang->debug_get_stack_level_members(current_frame, &members, &values);
209
print_variables(members, values, variable_prefix);
210
211
} else if (line.begins_with("p") || line.begins_with("print")) {
212
if (line.find_char(' ') < 0) {
213
print_line("Usage: print <expression>");
214
} else {
215
String expr = line.split(" ", true, 1)[1];
216
String res = script_lang->debug_parse_stack_level_expression(current_frame, expr);
217
print_line(res);
218
}
219
220
} else if (line == "s" || line == "step") {
221
script_debugger->set_depth(-1);
222
script_debugger->set_lines_left(1);
223
break;
224
} else if (line == "n" || line == "next") {
225
script_debugger->set_depth(0);
226
script_debugger->set_lines_left(1);
227
break;
228
} else if (line == "fin" || line == "finish") {
229
String current_function = script_lang->debug_get_stack_level_function(0);
230
231
for (int i = 0; i < total_frames; i++) {
232
target_function = script_lang->debug_get_stack_level_function(i);
233
if (target_function != current_function) {
234
script_debugger->set_depth(0);
235
script_debugger->set_lines_left(1);
236
return;
237
}
238
}
239
240
print_line("Error: Reached last frame.");
241
target_function = "";
242
243
} else if (line.begins_with("br") || line.begins_with("break")) {
244
if (line.get_slice_count(" ") <= 1) {
245
const HashMap<int, HashSet<StringName>> &breakpoints = script_debugger->get_breakpoints();
246
if (breakpoints.is_empty()) {
247
print_line("No Breakpoints.");
248
continue;
249
}
250
251
print_line("Breakpoint(s): " + itos(breakpoints.size()));
252
for (const KeyValue<int, HashSet<StringName>> &E : breakpoints) {
253
print_line("\t" + String(*E.value.begin()) + ":" + itos(E.key));
254
}
255
256
} else {
257
Pair<String, int> breakpoint = to_breakpoint(line);
258
259
String source = breakpoint.first;
260
int linenr = breakpoint.second;
261
262
if (source.is_empty()) {
263
continue;
264
}
265
266
script_debugger->insert_breakpoint(linenr, source);
267
268
print_line("Added breakpoint at " + source + ":" + itos(linenr));
269
}
270
271
} else if (line == "q" || line == "quit" ||
272
(line.is_empty() && feof(stdin))) {
273
// Do not stop again on quit
274
script_debugger->clear_breakpoints();
275
script_debugger->set_depth(-1);
276
script_debugger->set_lines_left(-1);
277
278
MainLoop *main_loop = OS::get_singleton()->get_main_loop();
279
if (main_loop->get_class() == "SceneTree") {
280
main_loop->call("quit");
281
}
282
break;
283
} else if (line.begins_with("delete")) {
284
if (line.get_slice_count(" ") <= 1) {
285
script_debugger->clear_breakpoints();
286
} else {
287
Pair<String, int> breakpoint = to_breakpoint(line);
288
289
String source = breakpoint.first;
290
int linenr = breakpoint.second;
291
292
if (source.is_empty()) {
293
continue;
294
}
295
296
script_debugger->remove_breakpoint(linenr, source);
297
298
print_line("Removed breakpoint at " + source + ":" + itos(linenr));
299
}
300
301
} else if (line == "h" || line == "help") {
302
print_line("Built-In Debugger command list:\n");
303
print_line("\tc,continue\t\t Continue execution.");
304
print_line("\tbt,backtrace\t\t Show stack trace (frames).");
305
print_line("\tfr,frame <frame>:\t Change current frame.");
306
print_line("\tlv,locals\t\t Show local variables for current frame.");
307
print_line("\tmv,members\t\t Show member variables for \"this\" in frame.");
308
print_line("\tgv,globals\t\t Show global variables.");
309
print_line("\tp,print <expr>\t\t Execute and print variable in expression.");
310
print_line("\ts,step\t\t\t Step to next line.");
311
print_line("\tn,next\t\t\t Next line.");
312
print_line("\tfin,finish\t\t Step out of current frame.");
313
print_line("\tbr,break [source:line]\t List all breakpoints or place a breakpoint.");
314
print_line("\tdelete [source:line]:\t Delete one/all breakpoints.");
315
print_line("\tset [key=value]:\t List all options, or set one.");
316
print_line("\tq,quit\t\t\t Quit application.");
317
} else {
318
print_line("Error: Invalid command, enter \"help\" for assistance.");
319
}
320
}
321
}
322
323
void LocalDebugger::print_variables(const List<String> &names, const List<Variant> &values, const String &variable_prefix) {
324
String value;
325
Vector<String> value_lines;
326
const List<Variant>::Element *V = values.front();
327
for (const String &E : names) {
328
value = String(V->get());
329
330
if (variable_prefix.is_empty()) {
331
print_line(E + ": " + String(V->get()));
332
} else {
333
print_line(E + ":");
334
value_lines = value.split("\n");
335
for (int i = 0; i < value_lines.size(); ++i) {
336
print_line(variable_prefix + value_lines[i]);
337
}
338
}
339
340
V = V->next();
341
}
342
}
343
344
Pair<String, int> LocalDebugger::to_breakpoint(const String &p_line) {
345
String breakpoint_part = p_line.get_slicec(' ', 1);
346
Pair<String, int> breakpoint;
347
348
int last_colon = breakpoint_part.rfind_char(':');
349
if (last_colon < 0) {
350
print_line("Error: Invalid breakpoint format. Expected [source:line]");
351
return breakpoint;
352
}
353
354
breakpoint.first = script_debugger->breakpoint_find_source(breakpoint_part.left(last_colon).strip_edges());
355
breakpoint.second = breakpoint_part.substr(last_colon).strip_edges().to_int();
356
357
return breakpoint;
358
}
359
360
void LocalDebugger::send_message(const String &p_message, const Array &p_args) {
361
// This needs to be cleaned up entirely.
362
// print_line("MESSAGE: '" + p_message + "' - " + String(Variant(p_args)));
363
}
364
365
void LocalDebugger::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) {
366
_err_print_error(p_func.utf8().get_data(), p_file.utf8().get_data(), p_line, p_err, p_descr, p_editor_notify, p_type);
367
}
368
369
LocalDebugger::LocalDebugger() {
370
options["variable_prefix"] = "";
371
372
// Bind scripts profiler.
373
scripts_profiler = memnew(ScriptsProfiler);
374
Profiler scr_prof(
375
scripts_profiler,
376
[](void *p_user, bool p_enable, const Array &p_opts) {
377
static_cast<ScriptsProfiler *>(p_user)->toggle(p_enable, p_opts);
378
},
379
nullptr,
380
[](void *p_user, double p_frame_time, double p_process_time, double p_physics_time, double p_physics_frame_time) {
381
static_cast<ScriptsProfiler *>(p_user)->tick(p_frame_time, p_process_time, p_physics_time, p_physics_frame_time);
382
});
383
register_profiler("scripts", scr_prof);
384
}
385
386
LocalDebugger::~LocalDebugger() {
387
unregister_profiler("scripts");
388
if (scripts_profiler) {
389
memdelete(scripts_profiler);
390
}
391
}
392
393