script_debugger_local.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*************************************************************************/
  2. /* script_debugger_local.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "script_debugger_local.h"
  30. #include "os/os.h"
  31. void ScriptDebuggerLocal::debug(ScriptLanguage *p_script,bool p_can_continue) {
  32. print_line("Debugger Break, Reason: '"+p_script->debug_get_error()+"'");
  33. 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)+"'");
  34. print_line("Enter \"help\" for assistance.");
  35. int current_frame=0;
  36. int total_frames=p_script->debug_get_stack_level_count();
  37. while(true) {
  38. OS::get_singleton()->print("debug> ");
  39. String line = OS::get_singleton()->get_stdin_string().strip_edges();
  40. if (line=="") {
  41. print_line("Debugger Break, Reason: '"+p_script->debug_get_error()+"'");
  42. 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)+"'");
  43. print_line("Enter \"help\" for assistance.");
  44. } else if (line=="c" || line=="continue")
  45. break;
  46. else if (line=="bt" || line=="breakpoint") {
  47. for(int i=0;i<total_frames;i++) {
  48. String cfi=(current_frame==i)?"*":" "; //current frame indicator
  49. 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)+"'");
  50. }
  51. } else if (line.begins_with("fr") || line.begins_with("frame")) {
  52. if (line.get_slice_count(" ")==1) {
  53. 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)+"'");
  54. } else {
  55. int frame = line.get_slicec(' ',1).to_int();
  56. if (frame<0 || frame >=total_frames) {
  57. print_line("Error: Invalid frame.");
  58. } else {
  59. current_frame=frame;
  60. 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)+"'");
  61. }
  62. }
  63. } else if (line=="lv" || line=="locals") {
  64. List<String> locals;
  65. List<Variant> values;
  66. p_script->debug_get_stack_level_locals(current_frame,&locals, &values);
  67. List<Variant>::Element* V = values.front();
  68. for (List<String>::Element *E=locals.front();E;E=E->next()) {
  69. print_line(E->get() + ": " + String(V->get()));
  70. V = V->next();
  71. }
  72. } else if (line=="gv" || line=="globals") {
  73. List<String> locals;
  74. List<Variant> values;
  75. p_script->debug_get_globals(&locals, &values);
  76. List<Variant>::Element* V = values.front();
  77. for (List<String>::Element *E=locals.front();E;E=E->next()) {
  78. print_line(E->get() + ": " + String(V->get()));
  79. V = V->next();
  80. }
  81. } else if (line=="mv" || line=="members") {
  82. List<String> locals;
  83. List<Variant> values;
  84. p_script->debug_get_stack_level_members(current_frame,&locals, &values);
  85. List<Variant>::Element* V = values.front();
  86. for (List<String>::Element *E=locals.front();E;E=E->next()) {
  87. print_line(E->get() + ": " + String(V->get()));
  88. V = V->next();
  89. }
  90. } else if (line.begins_with("p") || line.begins_with("print")) {
  91. if (line.get_slice_count(" ")<=1) {
  92. print_line("Usage: print <expre>");
  93. } else {
  94. String expr = line.get_slicec(' ',2);
  95. String res = p_script->debug_parse_stack_level_expression(current_frame,expr);
  96. print_line(res);
  97. }
  98. } else if (line=="s" || line=="step") {
  99. set_depth(-1);
  100. set_lines_left(1);
  101. break;
  102. } else if (line.begins_with("n") || line.begins_with("next")) {
  103. set_depth(0);
  104. set_lines_left(1);
  105. break;
  106. } else if (line.begins_with("br") || line.begins_with("break")) {
  107. if (line.get_slice_count(" ")<=1) {
  108. //show breakpoints
  109. } else {
  110. String bppos=line.get_slicec(' ',1);
  111. String source=bppos.get_slicec(':',0).strip_edges();
  112. int line=bppos.get_slicec(':',1).strip_edges().to_int();
  113. source = breakpoint_find_source(source);
  114. insert_breakpoint(line,source);
  115. print_line("BreakPoint at "+source+":"+itos(line));
  116. }
  117. } else if (line.begins_with("delete")) {
  118. if (line.get_slice_count(" ")<=1) {
  119. clear_breakpoints();
  120. } else {
  121. String bppos=line.get_slicec(' ',1);
  122. String source=bppos.get_slicec(':',0).strip_edges();
  123. int line=bppos.get_slicec(':',1).strip_edges().to_int();
  124. source = breakpoint_find_source(source);
  125. remove_breakpoint(line,source);
  126. print_line("Removed BreakPoint at "+source+":"+itos(line));
  127. }
  128. } else if (line=="h" || line=="help") {
  129. print_line("Built-In Debugger command list:\n");
  130. print_line("\tc,continue :\t\t Continue execution.");
  131. print_line("\tbt,backtrace :\t\t Show stack trace (frames).");
  132. print_line("\tfr,frame <frame>:\t Change current frame.");
  133. print_line("\tlv,locals :\t\t Show local variables for current frame.");
  134. print_line("\tmv,members :\t\t Show member variables for \"this\" in frame.");
  135. print_line("\tgv,globals :\t\t Show global variables.");
  136. print_line("\tp,print <expr> :\t Execute and print variable in expression.");
  137. print_line("\ts,step :\t\t Step to next line.");
  138. print_line("\tn,next :\t\t Next line.");
  139. print_line("\tbr,break source:line :\t Place a breakpoint.");
  140. print_line("\tdelete [source:line]:\t\t Delete one/all breakpoints.");
  141. } else {
  142. print_line("Error: Invalid command, enter \"help\" for assistance.");
  143. }
  144. }
  145. }
  146. struct _ScriptDebuggerLocalProfileInfoSort {
  147. bool operator()(const ScriptLanguage::ProfilingInfo &A,const ScriptLanguage::ProfilingInfo &B) const {
  148. return A.total_time > B.total_time;
  149. }
  150. };
  151. void ScriptDebuggerLocal::profiling_set_frame_times(float p_frame_time,float p_idle_time,float p_fixed_time,float p_fixed_frame_time) {
  152. frame_time=p_frame_time;
  153. idle_time=p_idle_time;
  154. fixed_time=p_fixed_time;
  155. fixed_frame_time=p_fixed_frame_time;
  156. }
  157. void ScriptDebuggerLocal::idle_poll() {
  158. if (!profiling)
  159. return;
  160. uint64_t diff = OS::get_singleton()->get_ticks_usec() - idle_accum;
  161. if (diff<1000000) //show every one second
  162. return;
  163. idle_accum = OS::get_singleton()->get_ticks_usec();
  164. int ofs=0;
  165. for(int i=0;i<ScriptServer::get_language_count();i++) {
  166. ofs+=ScriptServer::get_language(i)->profiling_get_frame_data(&pinfo[ofs],pinfo.size()-ofs);
  167. }
  168. SortArray<ScriptLanguage::ProfilingInfo,_ScriptDebuggerLocalProfileInfoSort> sort;
  169. sort.sort(pinfo.ptr(),ofs);
  170. //falta el frame time
  171. uint64_t script_time_us=0;
  172. for(int i=0;i<ofs;i++) {
  173. script_time_us+=pinfo[i].self_time;
  174. }
  175. float script_time=USEC_TO_SEC(script_time_us);
  176. float total_time=frame_time;
  177. //print script total
  178. print_line("FRAME: total: "+rtos(frame_time)+" script: "+rtos(script_time)+"/"+itos(script_time*100/total_time)+" %");
  179. for(int i=0;i<ofs;i++) {
  180. print_line(itos(i)+":"+pinfo[i].signature);
  181. float tt=USEC_TO_SEC(pinfo[i].total_time);
  182. float st=USEC_TO_SEC(pinfo[i].self_time);
  183. print_line("\ttotal: "+rtos(tt)+"/"+itos(tt*100/total_time)+" % \tself: "+rtos(st)+"/"+itos(st*100/total_time)+" % tcalls: "+itos(pinfo[i].call_count));
  184. }
  185. }
  186. void ScriptDebuggerLocal::profiling_start() {
  187. for(int i=0;i<ScriptServer::get_language_count();i++) {
  188. ScriptServer::get_language(i)->profiling_start();
  189. }
  190. print_line("BEGIN PROFILING");
  191. profiling=true;
  192. pinfo.resize(32768);
  193. frame_time=0;
  194. fixed_time=0;
  195. idle_time=0;
  196. fixed_frame_time=0;
  197. }
  198. void ScriptDebuggerLocal::profiling_end() {
  199. int ofs=0;
  200. for(int i=0;i<ScriptServer::get_language_count();i++) {
  201. ofs+=ScriptServer::get_language(i)->profiling_get_accumulated_data(&pinfo[ofs],pinfo.size()-ofs);
  202. }
  203. SortArray<ScriptLanguage::ProfilingInfo,_ScriptDebuggerLocalProfileInfoSort> sort;
  204. sort.sort(pinfo.ptr(),ofs);
  205. uint64_t total_us=0;
  206. for(int i=0;i<ofs;i++) {
  207. total_us+=pinfo[i].self_time;
  208. }
  209. float total_time=total_us/1000000.0;
  210. for(int i=0;i<ofs;i++) {
  211. print_line(itos(i)+":"+pinfo[i].signature);
  212. float tt=USEC_TO_SEC(pinfo[i].total_time);
  213. float st=USEC_TO_SEC(pinfo[i].self_time);
  214. 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));
  215. }
  216. for(int i=0;i<ScriptServer::get_language_count();i++) {
  217. ScriptServer::get_language(i)->profiling_stop();
  218. }
  219. profiling=false;
  220. }
  221. void ScriptDebuggerLocal::send_message(const String& p_message, const Array &p_args) {
  222. print_line("MESSAGE: '"+p_message+"' - "+String(Variant(p_args)));
  223. }
  224. ScriptDebuggerLocal::ScriptDebuggerLocal() {
  225. profiling=false;
  226. idle_accum=OS::get_singleton()->get_ticks_usec();
  227. }