Debian Codegraph @

core/debugger/local_debugger.cpp

raw symbols SCIP
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 if (script_debugger->is_ignoring_error_breaks() && p_is_error_breakpoint) {
119 return;
120 }
121
122 ScriptLanguage *script_lang = script_debugger->get_break_language();
123
124 if (!target_function.is_empty()) {
125 String current_function = script_lang->debug_get_stack_level_function(0);
126 if (current_function != target_function) {
127 script_debugger->set_depth(0);
128 script_debugger->set_lines_left(1);
129 return;
130 }
131 target_function = "";
132 }
133
134 print_line("\nDebugger Break, Reason: '" + script_lang->debug_get_error() + "'");
135 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) + "'");
136 print_line("Enter \"help\" for assistance.");
137 int current_frame = 0;
138 int total_frames = script_lang->debug_get_stack_level_count();
139 while (true) {
140 OS::get_singleton()->print("debug> ");
141 String line = OS::get_singleton()->get_stdin_string().strip_edges();
142
143 // Cache options
144 String variable_prefix = options["variable_prefix"];
145
146 if (line.is_empty() && !feof(stdin)) {
147 print_line("\nDebugger Break, Reason: '" + script_lang->debug_get_error() + "'");
148 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) + "'");
149 print_line("Enter \"help\" for assistance.");
150 } else if (line == "c" || line == "continue") {
151 break;
152 } else if (line == "bt" || line == "breakpoint") {
153 for (int i = 0; i < total_frames; i++) {
154 String cfi = (current_frame == i) ? "*" : " "; //current frame indicator
155 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) + "'");
156 }
157
158 } else if (line.begins_with("fr") || line.begins_with("frame")) {
159 if (line.get_slice_count(" ") == 1) {
160 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) + "'");
161 } else {
162 int frame = line.get_slicec(' ', 1).to_int();
163 if (frame < 0 || frame >= total_frames) {
164 print_line("Error: Invalid frame.");
165 } else {
166 current_frame = frame;
167 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) + "'");
168 }
169 }
170
171 } else if (line.begins_with("set")) {
172 if (line.get_slice_count(" ") == 1) {
173 for (const KeyValue<String, String> &E : options) {
174 print_line("\t" + E.key + "=" + E.value);
175 }
176
177 } else {
178 String key_value = line.get_slicec(' ', 1);
179 int value_pos = key_value.find_char('=');
180
181 if (value_pos < 0) {
182 print_line("Error: Invalid set format. Use: set key=value");
183 } else {
184 String key = key_value.left(value_pos);
185
186 if (!options.has(key)) {
187 print_line("Error: Unknown option " + key);
188 } else {
189 // Allow explicit tab character
190 String value = key_value.substr(value_pos + 1).replace("\\t", "\t");
191
192 options[key] = value;
193 }
194 }
195 }
196
197 } else if (line == "lv" || line == "locals") {
198 List<String> locals;
199 List<Variant> values;
200 script_lang->debug_get_stack_level_locals(current_frame, &locals, &values);
201 print_variables(locals, values, variable_prefix);
202
203 } else if (line == "gv" || line == "globals") {
204 List<String> globals;
205 List<Variant> values;
206 script_lang->debug_get_globals(&globals, &values);
207 print_variables(globals, values, variable_prefix);
208
209 } else if (line == "mv" || line == "members") {
210 List<String> members;
211 List<Variant> values;
212 script_lang->debug_get_stack_level_members(current_frame, &members, &values);
213 print_variables(members, values, variable_prefix);
214
215 } else if (line.begins_with("p") || line.begins_with("print")) {
216 if (line.find_char(' ') < 0) {
217 print_line("Usage: print <expression>");
218 } else {
219 String expr = line.split(" ", true, 1)[1];
220 String res = script_lang->debug_parse_stack_level_expression(current_frame, expr);
221 print_line(res);
222 }
223
224 } else if (line == "s" || line == "step") {
225 script_debugger->set_depth(-1);
226 script_debugger->set_lines_left(1);
227 break;
228 } else if (line == "n" || line == "next") {
229 script_debugger->set_depth(0);
230 script_debugger->set_lines_left(1);
231 break;
232 } else if (line == "o" || line == "out") {
233 script_debugger->set_depth(1);
234 script_debugger->set_lines_left(1);
235 break;
236 } else if (line == "fin" || line == "finish") {
237 String current_function = script_lang->debug_get_stack_level_function(0);
238
239 for (int i = 0; i < total_frames; i++) {
240 target_function = script_lang->debug_get_stack_level_function(i);
241 if (target_function != current_function) {
242 script_debugger->set_depth(0);
243 script_debugger->set_lines_left(1);
244 return;
245 }
246 }
247
248 print_line("Error: Reached last frame.");
249 target_function = "";
250
251 } else if (line.begins_with("br") || line.begins_with("break")) {
252 if (line.get_slice_count(" ") <= 1) {
253 const HashMap<int, HashSet<StringName>> &breakpoints = script_debugger->get_breakpoints();
254 if (breakpoints.is_empty()) {
255 print_line("No Breakpoints.");
256 continue;
257 }
258
259 print_line("Breakpoint(s): " + itos(breakpoints.size()));
260 for (const KeyValue<int, HashSet<StringName>> &E : breakpoints) {
261 print_line("\t" + String(*E.value.begin()) + ":" + itos(E.key));
262 }
263
264 } else {
265 Pair<String, int> breakpoint = to_breakpoint(line);
266
267 String source = breakpoint.first;
268 int linenr = breakpoint.second;
269
270 if (source.is_empty()) {
271 continue;
272 }
273
274 script_debugger->insert_breakpoint(linenr, source);
275
276 print_line("Added breakpoint at " + source + ":" + itos(linenr));
277 }
278
279 } else if (line == "q" || line == "quit" ||
280 (line.is_empty() && feof(stdin))) {
281 // Do not stop again on quit
282 script_debugger->clear_breakpoints();
283 script_debugger->set_depth(-1);
284 script_debugger->set_lines_left(-1);
285
286 MainLoop *main_loop = OS::get_singleton()->get_main_loop();
287 if (main_loop->get_class() == "SceneTree") {
288 main_loop->call("quit");
289 }
290 break;
291 } else if (line.begins_with("delete")) {
292 if (line.get_slice_count(" ") <= 1) {
293 script_debugger->clear_breakpoints();
294 } else {
295 Pair<String, int> breakpoint = to_breakpoint(line);
296
297 String source = breakpoint.first;
298 int linenr = breakpoint.second;
299
300 if (source.is_empty()) {
301 continue;
302 }
303
304 script_debugger->remove_breakpoint(linenr, source);
305
306 print_line("Removed breakpoint at " + source + ":" + itos(linenr));
307 }
308
309 } else if (line == "h" || line == "help") {
310 print_line("Built-In Debugger command list:\n");
311 print_line("\tc,continue\t\t Continue execution.");
312 print_line("\tbt,backtrace\t\t Show stack trace (frames).");
313 print_line("\tfr,frame <frame>:\t Change current frame.");
314 print_line("\tlv,locals\t\t Show local variables for current frame.");
315 print_line("\tmv,members\t\t Show member variables for \"this\" in frame.");
316 print_line("\tgv,globals\t\t Show global variables.");
317 print_line("\tp,print <expr>\t\t Execute and print variable in expression.");
318 print_line("\ts,step\t\t\t Step to next line.");
319 print_line("\tn,next\t\t\t Next line.");
320 print_line("\tfin,finish\t\t Step out of current frame.");
321 print_line("\tbr,break [source:line]\t List all breakpoints or place a breakpoint.");
322 print_line("\tdelete [source:line]:\t Delete one/all breakpoints.");
323 print_line("\tset [key=value]:\t List all options, or set one.");
324 print_line("\tq,quit\t\t\t Quit application.");
325 } else {
326 print_line("Error: Invalid command, enter \"help\" for assistance.");
327 }
328 }
329 }
330
331 void LocalDebugger::print_variables(const List<String> &names, const List<Variant> &values, const String &variable_prefix) {
332 String value;
333 Vector<String> value_lines;
334 const List<Variant>::Element *V = values.front();
335 for (const String &E : names) {
336 value = String(V->get());
337
338 if (variable_prefix.is_empty()) {
339 print_line(E + ": " + String(V->get()));
340 } else {
341 print_line(E + ":");
342 value_lines = value.split("\n");
343 for (int i = 0; i < value_lines.size(); ++i) {
344 print_line(variable_prefix + value_lines[i]);
345 }
346 }
347
348 V = V->next();
349 }
350 }
351
352 Pair<String, int> LocalDebugger::to_breakpoint(const String &p_line) {
353 String breakpoint_part = p_line.get_slicec(' ', 1);
354 Pair<String, int> breakpoint;
355
356 int last_colon = breakpoint_part.rfind_char(':');
357 if (last_colon < 0) {
358 print_line("Error: Invalid breakpoint format. Expected [source:line]");
359 return breakpoint;
360 }
361
362 breakpoint.first = script_debugger->breakpoint_find_source(breakpoint_part.left(last_colon).strip_edges());
363 breakpoint.second = breakpoint_part.substr(last_colon).strip_edges().to_int();
364
365 return breakpoint;
366 }
367
368 void LocalDebugger::send_message(const String &p_message, const Array &p_args) {
369 // This needs to be cleaned up entirely.
370 // print_line("MESSAGE: '" + p_message + "' - " + String(Variant(p_args)));
371 }
372
373 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) {
374 _err_print_error(p_func.utf8().get_data(), p_file.utf8().get_data(), p_line, p_err, p_descr, p_editor_notify, p_type);
375 }
376
377 LocalDebugger::LocalDebugger() {
378 options["variable_prefix"] = "";
379
380 // Bind scripts profiler.
381 scripts_profiler = memnew(ScriptsProfiler);
382 Profiler scr_prof(
383 scripts_profiler,
384 [](void *p_user, bool p_enable, const Array &p_opts) {
385 static_cast<ScriptsProfiler *>(p_user)->toggle(p_enable, p_opts);
386 },
387 nullptr,
388 [](void *p_user, double p_frame_time, double p_process_time, double p_physics_time, double p_physics_frame_time) {
389 static_cast<ScriptsProfiler *>(p_user)->tick(p_frame_time, p_process_time, p_physics_time, p_physics_frame_time);
390 });
391 register_profiler("scripts", scr_prof);
392 }
393
394 LocalDebugger::~LocalDebugger() {
395 unregister_profiler("scripts");
396 if (scripts_profiler) {
397 memdelete(scripts_profiler);
398 }
399 }