local_debugger.cpp 15 KB

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