script_debugger_local.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "os/os.h"
  32. void ScriptDebuggerLocal::debug(ScriptLanguage *p_script, bool p_can_continue) {
  33. print_line("Debugger Break, Reason: '" + p_script->debug_get_error() + "'");
  34. 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) + "'");
  35. print_line("Enter \"help\" for assistance.");
  36. int current_frame = 0;
  37. int total_frames = p_script->debug_get_stack_level_count();
  38. while (true) {
  39. OS::get_singleton()->print("debug> ");
  40. String line = OS::get_singleton()->get_stdin_string().strip_edges();
  41. if (line == "") {
  42. print_line("Debugger Break, Reason: '" + p_script->debug_get_error() + "'");
  43. 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) + "'");
  44. print_line("Enter \"help\" for assistance.");
  45. } else if (line == "c" || line == "continue")
  46. break;
  47. else if (line == "bt" || line == "breakpoint") {
  48. for (int i = 0; i < total_frames; i++) {
  49. String cfi = (current_frame == i) ? "*" : " "; //current frame indicator
  50. 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) + "'");
  51. }
  52. } else if (line.begins_with("fr") || line.begins_with("frame")) {
  53. if (line.get_slice_count(" ") == 1) {
  54. 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) + "'");
  55. } else {
  56. int frame = line.get_slicec(' ', 1).to_int();
  57. if (frame < 0 || frame >= total_frames) {
  58. print_line("Error: Invalid frame.");
  59. } else {
  60. current_frame = frame;
  61. 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) + "'");
  62. }
  63. }
  64. } else if (line == "lv" || line == "locals") {
  65. List<String> locals;
  66. List<Variant> values;
  67. p_script->debug_get_stack_level_locals(current_frame, &locals, &values);
  68. List<Variant>::Element *V = values.front();
  69. for (List<String>::Element *E = locals.front(); E; E = E->next()) {
  70. print_line(E->get() + ": " + String(V->get()));
  71. V = V->next();
  72. }
  73. } else if (line == "gv" || line == "globals") {
  74. List<String> locals;
  75. List<Variant> values;
  76. p_script->debug_get_globals(&locals, &values);
  77. List<Variant>::Element *V = values.front();
  78. for (List<String>::Element *E = locals.front(); E; E = E->next()) {
  79. print_line(E->get() + ": " + String(V->get()));
  80. V = V->next();
  81. }
  82. } else if (line == "mv" || line == "members") {
  83. List<String> locals;
  84. List<Variant> values;
  85. p_script->debug_get_stack_level_members(current_frame, &locals, &values);
  86. List<Variant>::Element *V = values.front();
  87. for (List<String>::Element *E = locals.front(); E; E = E->next()) {
  88. print_line(E->get() + ": " + String(V->get()));
  89. V = V->next();
  90. }
  91. } else if (line.begins_with("p") || line.begins_with("print")) {
  92. if (line.get_slice_count(" ") <= 1) {
  93. print_line("Usage: print <expre>");
  94. } else {
  95. String expr = line.get_slicec(' ', 2);
  96. String res = p_script->debug_parse_stack_level_expression(current_frame, expr);
  97. print_line(res);
  98. }
  99. } else if (line == "s" || line == "step") {
  100. set_depth(-1);
  101. set_lines_left(1);
  102. break;
  103. } else if (line.begins_with("n") || line.begins_with("next")) {
  104. set_depth(0);
  105. set_lines_left(1);
  106. break;
  107. } else if (line.begins_with("br") || line.begins_with("break")) {
  108. if (line.get_slice_count(" ") <= 1) {
  109. //show breakpoints
  110. } else {
  111. String bppos = line.get_slicec(' ', 1);
  112. String source = bppos.get_slicec(':', 0).strip_edges();
  113. int line = bppos.get_slicec(':', 1).strip_edges().to_int();
  114. source = breakpoint_find_source(source);
  115. insert_breakpoint(line, source);
  116. print_line("BreakPoint at " + source + ":" + itos(line));
  117. }
  118. } else if (line.begins_with("delete")) {
  119. if (line.get_slice_count(" ") <= 1) {
  120. clear_breakpoints();
  121. } else {
  122. String bppos = line.get_slicec(' ', 1);
  123. String source = bppos.get_slicec(':', 0).strip_edges();
  124. int line = bppos.get_slicec(':', 1).strip_edges().to_int();
  125. source = breakpoint_find_source(source);
  126. remove_breakpoint(line, source);
  127. print_line("Removed BreakPoint at " + source + ":" + itos(line));
  128. }
  129. } else if (line == "h" || line == "help") {
  130. print_line("Built-In Debugger command list:\n");
  131. print_line("\tc,continue :\t\t Continue execution.");
  132. print_line("\tbt,backtrace :\t\t Show stack trace (frames).");
  133. print_line("\tfr,frame <frame>:\t Change current frame.");
  134. print_line("\tlv,locals :\t\t Show local variables for current frame.");
  135. print_line("\tmv,members :\t\t Show member variables for \"this\" in frame.");
  136. print_line("\tgv,globals :\t\t Show global variables.");
  137. print_line("\tp,print <expr> :\t Execute and print variable in expression.");
  138. print_line("\ts,step :\t\t Step to next line.");
  139. print_line("\tn,next :\t\t Next line.");
  140. print_line("\tbr,break source:line :\t Place a breakpoint.");
  141. print_line("\tdelete [source:line]:\t\t Delete one/all breakpoints.");
  142. } else {
  143. print_line("Error: Invalid command, enter \"help\" for assistance.");
  144. }
  145. }
  146. }
  147. struct _ScriptDebuggerLocalProfileInfoSort {
  148. bool operator()(const ScriptLanguage::ProfilingInfo &A, const ScriptLanguage::ProfilingInfo &B) const {
  149. return A.total_time > B.total_time;
  150. }
  151. };
  152. void ScriptDebuggerLocal::profiling_set_frame_times(float p_frame_time, float p_idle_time, float p_physics_time, float p_physics_frame_time) {
  153. frame_time = p_frame_time;
  154. idle_time = p_idle_time;
  155. physics_time = p_physics_time;
  156. physics_frame_time = p_physics_frame_time;
  157. }
  158. void ScriptDebuggerLocal::idle_poll() {
  159. if (!profiling)
  160. return;
  161. uint64_t diff = OS::get_singleton()->get_ticks_usec() - idle_accum;
  162. if (diff < 1000000) //show every one second
  163. return;
  164. idle_accum = OS::get_singleton()->get_ticks_usec();
  165. int ofs = 0;
  166. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  167. ofs += ScriptServer::get_language(i)->profiling_get_frame_data(&pinfo[ofs], pinfo.size() - ofs);
  168. }
  169. SortArray<ScriptLanguage::ProfilingInfo, _ScriptDebuggerLocalProfileInfoSort> sort;
  170. sort.sort(pinfo.ptrw(), ofs);
  171. //falta el frame time
  172. uint64_t script_time_us = 0;
  173. for (int i = 0; i < ofs; i++) {
  174. script_time_us += pinfo[i].self_time;
  175. }
  176. float script_time = USEC_TO_SEC(script_time_us);
  177. float total_time = frame_time;
  178. //print script total
  179. print_line("FRAME: total: " + rtos(frame_time) + " script: " + rtos(script_time) + "/" + itos(script_time * 100 / total_time) + " %");
  180. for (int i = 0; i < ofs; i++) {
  181. print_line(itos(i) + ":" + pinfo[i].signature);
  182. float tt = USEC_TO_SEC(pinfo[i].total_time);
  183. float st = USEC_TO_SEC(pinfo[i].self_time);
  184. print_line("\ttotal: " + rtos(tt) + "/" + itos(tt * 100 / total_time) + " % \tself: " + rtos(st) + "/" + itos(st * 100 / total_time) + " % tcalls: " + itos(pinfo[i].call_count));
  185. }
  186. }
  187. void ScriptDebuggerLocal::profiling_start() {
  188. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  189. ScriptServer::get_language(i)->profiling_start();
  190. }
  191. print_line("BEGIN PROFILING");
  192. profiling = true;
  193. pinfo.resize(32768);
  194. frame_time = 0;
  195. physics_time = 0;
  196. idle_time = 0;
  197. physics_frame_time = 0;
  198. }
  199. void ScriptDebuggerLocal::profiling_end() {
  200. int ofs = 0;
  201. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  202. ofs += ScriptServer::get_language(i)->profiling_get_accumulated_data(&pinfo[ofs], pinfo.size() - ofs);
  203. }
  204. SortArray<ScriptLanguage::ProfilingInfo, _ScriptDebuggerLocalProfileInfoSort> sort;
  205. sort.sort(pinfo.ptrw(), ofs);
  206. uint64_t total_us = 0;
  207. for (int i = 0; i < ofs; i++) {
  208. total_us += pinfo[i].self_time;
  209. }
  210. float total_time = total_us / 1000000.0;
  211. for (int i = 0; i < ofs; i++) {
  212. print_line(itos(i) + ":" + pinfo[i].signature);
  213. float tt = USEC_TO_SEC(pinfo[i].total_time);
  214. float st = USEC_TO_SEC(pinfo[i].self_time);
  215. 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));
  216. }
  217. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  218. ScriptServer::get_language(i)->profiling_stop();
  219. }
  220. profiling = false;
  221. }
  222. void ScriptDebuggerLocal::send_message(const String &p_message, const Array &p_args) {
  223. print_line("MESSAGE: '" + p_message + "' - " + String(Variant(p_args)));
  224. }
  225. 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) {
  226. print_line("ERROR: '" + (p_descr.empty() ? p_err : p_descr) + "'");
  227. }
  228. ScriptDebuggerLocal::ScriptDebuggerLocal() {
  229. profiling = false;
  230. idle_accum = OS::get_singleton()->get_ticks_usec();
  231. }