local_debugger.cpp 15 KB

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