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