dbg.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Copyright (C) 2018 bzt (bztsrc@github)
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use, copy,
  8. * modify, merge, publish, distribute, sublicense, and/or sell copies
  9. * of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  19. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. *
  24. */
  25. #include "uart.h"
  26. #define DISASSEMBLER 1
  27. // array to store register values (see dbg_saveregs in start.S)
  28. unsigned long dbg_regs[37];
  29. // command line
  30. char cmd[256], dbg_running=0;
  31. #if DISASSEMBLER
  32. /**
  33. * things needed by the disassembler
  34. */
  35. #include "sprintf.h"
  36. #define NULL ((void*)0)
  37. typedef unsigned long uint64_t;
  38. typedef unsigned int uint32_t;
  39. typedef unsigned short uint16_t;
  40. typedef unsigned char uint8_t;
  41. // include the Universal Disassembler Library
  42. #include "disasm.h"
  43. #endif
  44. /**
  45. * Decode exception cause
  46. */
  47. void dbg_decodeexc(unsigned long type)
  48. {
  49. unsigned char cause=dbg_regs[33]>>26;
  50. // print out interruption type
  51. switch(type) {
  52. case 0: printf("Synchronous"); break;
  53. case 1: printf("IRQ"); break;
  54. case 2: printf("FIQ"); break;
  55. case 3: printf("SError"); break;
  56. }
  57. printf(": ");
  58. // decode exception type (some, not all. See ARM DDI0487B_b chapter D10.2.28)
  59. switch(cause) {
  60. case 0b000000: printf("Unknown"); break;
  61. case 0b000001: printf("Trapped WFI/WFE"); break;
  62. case 0b001110: printf("Illegal execution"); break;
  63. case 0b010101: printf("System call"); break;
  64. case 0b100000: printf("Instruction abort, lower EL"); break;
  65. case 0b100001: printf("Instruction abort, same EL"); break;
  66. case 0b100010: printf("Instruction alignment fault"); break;
  67. case 0b100100: printf("Data abort, lower EL"); break;
  68. case 0b100101: printf("Data abort, same EL"); break;
  69. case 0b100110: printf("Stack alignment fault"); break;
  70. case 0b101100: printf("Floating point"); break;
  71. case 0b110000: printf("Breakpoint, lower EL"); break;
  72. case 0b110001: printf("Breakpoint, same EL"); break;
  73. case 0b111100: printf("Breakpoint instruction"); break;
  74. default: printf("Unknown %x", cause); break;
  75. }
  76. // decode data abort cause
  77. if(cause==0b100100 || cause==0b100101) {
  78. printf(", ");
  79. switch((dbg_regs[33]>>2)&0x3) {
  80. case 0: printf("Address size fault"); break;
  81. case 1: printf("Translation fault"); break;
  82. case 2: printf("Access flag fault"); break;
  83. case 3: printf("Permission fault"); break;
  84. }
  85. switch(dbg_regs[33]&0x3) {
  86. case 0: printf(" at level 0"); break;
  87. case 1: printf(" at level 1"); break;
  88. case 2: printf(" at level 2"); break;
  89. case 3: printf(" at level 3"); break;
  90. }
  91. }
  92. printf("\n");
  93. // if the exception happened in the debugger, we stop to avoid infinite loop
  94. if(dbg_running) {
  95. printf("Exception in debugger!\n"
  96. " elr_el1: %x spsr_el1: %x\n esr_el1: %x far_el1: %x\nsctlr_el1: %x tcr_el1: %x\n",
  97. dbg_regs[31],dbg_regs[32],dbg_regs[33],dbg_regs[34],dbg_regs[35],dbg_regs[36]);
  98. while(1);
  99. }
  100. }
  101. /**
  102. * helper to read a line from user. We redefine some control caracters to handle CSI
  103. * \e[3~ = 1, delete
  104. * \e[D = 2, cursor left
  105. * \e[C = 3, cursor right
  106. */
  107. void dbg_getline()
  108. {
  109. int i,cmdidx=0,cmdlast=0;
  110. char c;
  111. cmd[0]=0;
  112. // prompt
  113. printf("\r> ");
  114. // read until Enter pressed
  115. while((c=uart_getc())!='\n') {
  116. // decode CSI key sequences (some, not all)
  117. if(c==27) {
  118. c=uart_getc();
  119. if(c=='[') {
  120. c=uart_getc();
  121. if(c=='C') c=3; else // left
  122. if(c=='D') c=2; else // right
  123. if(c=='3') {
  124. c=uart_getc();
  125. if(c=='~') c=1; // delete
  126. }
  127. }
  128. }
  129. // Backspace
  130. if(c==8 || c==127) {
  131. if(cmdidx>0) {
  132. cmdidx--;
  133. for(i=cmdidx;i<cmdlast;i++) cmd[i]=cmd[i+1];
  134. cmdlast--;
  135. }
  136. } else
  137. // Delete
  138. if(c==1) {
  139. if(cmdidx<cmdlast) {
  140. for(i=cmdidx;i<cmdlast;i++) cmd[i]=cmd[i+1];
  141. cmdlast--;
  142. }
  143. } else
  144. // cursor left
  145. if(c==2) {
  146. if(cmdidx>0) cmdidx--;
  147. } else
  148. // cursor right
  149. if(c==3) {
  150. if(cmdidx<cmdlast) cmdidx++;
  151. } else {
  152. // is there a valid character and space to store it?
  153. if(c<' ' || cmdlast>=sizeof(cmd)-1) {
  154. continue;
  155. }
  156. // if we're not appending, move bytes after cursor
  157. if(cmdidx<cmdlast) {
  158. for(i=cmdlast;i>cmdidx;i--)
  159. cmd[i]=cmd[i-1];
  160. }
  161. cmdlast++;
  162. cmd[cmdidx++]=c;
  163. }
  164. cmd[cmdlast]=0;
  165. // display prompt and command line, place cursor with CSI code
  166. printf("\r> %s \r\e[%dC",cmd,cmdidx+2);
  167. }
  168. printf("\n");
  169. }
  170. /**
  171. * helper function to parse the command line for arguments
  172. */
  173. unsigned long dbg_getoffs(int i)
  174. {
  175. unsigned long base=0,ret=0;
  176. int j=0,sign=0;
  177. // if starts with a register
  178. if(cmd[i]=='x' || cmd[i]=='r') {
  179. i++; if(cmd[i]>='0' && cmd[i]<='9') { j=cmd[i]-'0'; }
  180. i++; if(cmd[i]>='0' && cmd[i]<='9') { j*=10; j+=cmd[i]-'0'; }
  181. if(j>=0 && j<37) base=dbg_regs[j];
  182. i++;
  183. if(cmd[i]=='-') { i++; sign++; }
  184. if(cmd[i]=='+') i++;
  185. }
  186. // offset part
  187. if(cmd[i]=='0' && cmd[i+1]=='x') {
  188. i+=2;
  189. // hex value
  190. while((cmd[i]>='0'&&cmd[i]<='9')||(cmd[i]>='a'&&cmd[i]<='f')||(cmd[i]>='A'&&cmd[i]<='F')) {
  191. ret <<= 4;
  192. if(cmd[i]>='0' && cmd[i]<='9') ret += cmd[i]-'0';
  193. else if(cmd[i] >= 'a' && cmd[i] <= 'f') ret += cmd[i]-'a'+10;
  194. else if(cmd[i] >= 'A' && cmd[i] <= 'F') ret += cmd[i]-'A'+10;
  195. i++;
  196. }
  197. } else {
  198. // decimal value
  199. while(cmd[i]>='0'&&cmd[i]<='9'){
  200. ret *= 10;
  201. ret += cmd[i++]-'0';
  202. }
  203. }
  204. // return base + offset
  205. return sign? base-ret : base+ret;
  206. }
  207. /**
  208. * main loop, get and parse commands
  209. */
  210. void dbg_main()
  211. {
  212. unsigned long os=0, oe=0, a;
  213. char c;
  214. #if DISASSEMBLER
  215. char str[64];
  216. #endif
  217. int i;
  218. dbg_running++;
  219. // main debugger loop
  220. while(1) {
  221. // get command from user
  222. dbg_getline();
  223. // parse commands
  224. if(cmd[0]==0 || cmd[0]=='?' || cmd[0]=='h') {
  225. // print help
  226. printf("Mini debugger commands:\n"
  227. " ?/h\t\tthis help\n"
  228. " r\t\tdump registers\n"
  229. " x [os [oe]]\texamine memory from offset start (os) to offset end (oe)\n"
  230. " i [os [oe]]\tdisassemble instruction from offset start to offset end\n"
  231. " c\t\tcontinue execution\n");
  232. continue;
  233. } else
  234. // continue execution
  235. if(cmd[0]=='c') {
  236. // move instruction pointer, skip over 'brk'
  237. asm volatile ("msr elr_el1, %0" : : "r" (dbg_regs[31]+4));
  238. break;
  239. } else
  240. // dump registers
  241. if(cmd[0]=='r') {
  242. // general purpose registers x0-x30
  243. for(i=0;i<31;i++) {
  244. if(i && i%3==0) printf("\n");
  245. if(i<10) printf(" ");
  246. printf("x%d: %16x ",i,dbg_regs[i]);
  247. }
  248. // some system registers
  249. printf("elr_el1: %x spsr_el1: %x\n esr_el1: %x far_el1: %x\nsctlr_el1: %x tcr_el1: %x\n",
  250. dbg_regs[31],dbg_regs[32],dbg_regs[33],dbg_regs[34],dbg_regs[35],dbg_regs[36]);
  251. continue;
  252. } else
  253. // examine or disassemble, commands with arguments
  254. if(cmd[0]=='x' || cmd[0]=='i') {
  255. i=1;
  256. // get first argument
  257. while(cmd[i]!=0 && cmd[i]!=' ') i++; // skip command
  258. while(cmd[i]!=0 && cmd[i]==' ') i++; // skip separators
  259. if(cmd[i]!=0) {
  260. os=oe=dbg_getoffs(i);
  261. // get second argument
  262. while(cmd[i]!=0 && cmd[i]!=' ') i++; // skip 1st arg
  263. while(cmd[i]!=0 && cmd[i]==' ') i++; // skip separators
  264. if(cmd[i]!=0) {
  265. oe=dbg_getoffs(i);
  266. }
  267. } else {
  268. // no arguments, use defaults
  269. if(cmd[0]=='i') {
  270. // elr or lr (x30)
  271. os=oe=dbg_regs[31]?dbg_regs[31]:dbg_regs[30];
  272. } else {
  273. // sp (x29)
  274. os=oe=dbg_regs[29];
  275. }
  276. }
  277. // do the thing
  278. if(cmd[0]=='i') {
  279. // must be multiple of 4
  280. os=os&~3L;
  281. oe=(oe+3)&~3L;
  282. if(oe<=os) oe=os+4;
  283. // disassemble AArch64 bytecode
  284. while(os<oe) {
  285. // print out address and instruction bytecode
  286. printf("%8x: %8x",os,*((unsigned int*)os));
  287. #if DISASSEMBLER
  288. // disassemble and print out instruction mnemonic
  289. os=disasm(os,str);
  290. printf("\t%s\n",str);
  291. #else
  292. os+=4;
  293. printf("\n");
  294. #endif
  295. }
  296. } else {
  297. // dump memory
  298. if(oe<=os) oe=os+16;
  299. // for each 16 bytes, do
  300. for(a=os;a<oe;a+=16) {
  301. // print out address
  302. printf("%8x: ", a);
  303. // hex representation
  304. for(i=0;i<16;i++) {
  305. printf("%2x%s ",*((unsigned char*)(a+i)),i%4==3?" ":"");
  306. }
  307. // character representation
  308. for(i=0;i<16;i++) {
  309. c=*((unsigned char*)(a+i));
  310. printf("%c",c<32||c>=127?'.':c);
  311. }
  312. printf("\n");
  313. }
  314. }
  315. continue;
  316. } else {
  317. printf("ERROR: unknown command.\n");
  318. }
  319. }
  320. dbg_running--;
  321. }