script_debugger_local.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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-2014 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_slice(" ",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_slice(" ",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_slice(" ",1);
  111. String source=bppos.get_slice(":",0).strip_edges();
  112. int line=bppos.get_slice(":",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_slice(" ",1);
  122. String source=bppos.get_slice(":",0).strip_edges();
  123. int line=bppos.get_slice(":",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. void ScriptDebuggerLocal::send_message(const String& p_message, const Array &p_args) {
  147. print_line("MESSAGE: '"+p_message+"' - "+String(Variant(p_args)));
  148. }
  149. ScriptDebuggerLocal::ScriptDebuggerLocal() {
  150. }