local_debugger.cpp 15 KB

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