script_debugger_local.cpp 14 KB

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