gdbstub.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. /******************************************************************************
  2. * Copyright 2015 Espressif Systems
  3. *
  4. * Description: A stub to make the ESP8266 debuggable by GDB over the serial
  5. * port.
  6. *
  7. * License: ESPRESSIF MIT License
  8. *******************************************************************************/
  9. #include "ets_sys.h"
  10. #include "eagle_soc.h"
  11. #include "c_types.h"
  12. #include "gpio.h"
  13. #include "xtensa/corebits.h"
  14. #include "gdbstub/gdbstub.h"
  15. #include "gdbstub/gdbstub-entry.h"
  16. #include "gdbstub/gdbstub-cfg.h"
  17. //From xtruntime-frames.h
  18. struct XTensa_exception_frame_s {
  19. uint32_t pc;
  20. uint32_t ps;
  21. uint32_t sar;
  22. uint32_t vpri;
  23. uint32_t a0;
  24. uint32_t a[14]; //a2..a15
  25. //These are added manually by the exception code; the HAL doesn't set these on an exception.
  26. uint32_t litbase;
  27. uint32_t sr176;
  28. uint32_t sr208;
  29. uint32_t a1;
  30. //'reason' is abused for both the debug and the exception vector: if bit 7 is set,
  31. //this contains an exception reason, otherwise it contains a debug vector bitmap.
  32. uint32_t reason;
  33. };
  34. struct XTensa_rtos_int_frame_s {
  35. uint32_t exitPtr;
  36. uint32_t pc;
  37. uint32_t ps;
  38. uint32_t a[16];
  39. uint32_t sar;
  40. };
  41. #if GDBSTUB_FREERTOS
  42. /*
  43. Definitions for FreeRTOS. This redefines some os_* functions to use their non-os* counterparts. It
  44. also sets up some function pointers for ROM functions that aren't in the FreeRTOS ld files.
  45. */
  46. #include <string.h>
  47. #include <stdio.h>
  48. void _xt_isr_attach(int inum, void *fn);
  49. void _xt_isr_unmask(int inum);
  50. void os_install_putc1(void (*p)(char c));
  51. #define os_printf(...) printf(__VA_ARGS__)
  52. #define os_memcpy(a,b,c) memcpy(a,b,c)
  53. typedef void wdtfntype();
  54. static wdtfntype *ets_wdt_disable=(wdtfntype *)0x400030f0;
  55. static wdtfntype *ets_wdt_enable=(wdtfntype *)0x40002fa0;
  56. #else
  57. /*
  58. OS-less SDK defines. Defines some headers for things that aren't in the include files, plus
  59. the xthal stack frame struct.
  60. */
  61. #include "osapi.h"
  62. #include "user_interface.h"
  63. void _xtos_set_exception_handler(int cause, void (exhandler)(struct XTensa_exception_frame_s *frame));
  64. int os_printf_plus(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
  65. #endif
  66. #define EXCEPTION_GDB_SP_OFFSET 0x100
  67. //We need some UART register defines.
  68. #define ETS_UART_INUM 5
  69. #define REG_UART_BASE( i ) (0x60000000+(i)*0xf00)
  70. #define UART_STATUS( i ) (REG_UART_BASE( i ) + 0x1C)
  71. #define UART_RXFIFO_CNT 0x000000FF
  72. #define UART_RXFIFO_CNT_S 0
  73. #define UART_TXFIFO_CNT 0x000000FF
  74. #define UART_TXFIFO_CNT_S 16
  75. #define UART_FIFO( i ) (REG_UART_BASE( i ) + 0x0)
  76. #define UART_INT_ENA(i) (REG_UART_BASE(i) + 0xC)
  77. #define UART_INT_CLR(i) (REG_UART_BASE(i) + 0x10)
  78. #define UART_RXFIFO_TOUT_INT_ENA (BIT(8))
  79. #define UART_RXFIFO_FULL_INT_ENA (BIT(0))
  80. #define UART_RXFIFO_TOUT_INT_CLR (BIT(8))
  81. #define UART_RXFIFO_FULL_INT_CLR (BIT(0))
  82. //Length of buffer used to reserve GDB commands. Has to be at least able to fit the G command, which
  83. //implies a minimum size of about 190 bytes.
  84. #define PBUFLEN 256
  85. //Length of gdb stdout buffer, for console redirection
  86. #define OBUFLEN 32
  87. //The asm stub saves the Xtensa registers here when a debugging exception happens.
  88. struct XTensa_exception_frame_s gdbstub_savedRegs;
  89. #if GDBSTUB_USE_OWN_STACK
  90. //This is the debugging exception stack.
  91. int exceptionStack[256];
  92. #endif
  93. static unsigned char cmd[PBUFLEN]; //GDB command input buffer
  94. static char chsum; //Running checksum of the output packet
  95. #if GDBSTUB_REDIRECT_CONSOLE_OUTPUT
  96. static unsigned char obuf[OBUFLEN]; //GDB stdout buffer
  97. static int obufpos=0; //Current position in the buffer
  98. #endif
  99. static int32_t singleStepPs=-1; //Stores ps when single-stepping instruction. -1 when not in use.
  100. //Small function to feed the hardware watchdog. Needed to stop the ESP from resetting
  101. //due to a watchdog timeout while reading a command.
  102. static void ATTR_GDBFN keepWDTalive() {
  103. uint64_t *wdtval=(uint64_t*)0x3ff21048;
  104. uint64_t *wdtovf=(uint64_t*)0x3ff210cc;
  105. int *wdtctl=(int*)0x3ff210c8;
  106. *wdtovf=*wdtval+1600000;
  107. *wdtctl|=(1<<31);
  108. }
  109. //Receive a char from the uart. Uses polling and feeds the watchdog.
  110. static int ATTR_GDBFN gdbRecvChar() {
  111. int i;
  112. while (((READ_PERI_REG(UART_STATUS(0))>>UART_RXFIFO_CNT_S)&UART_RXFIFO_CNT)==0) {
  113. keepWDTalive();
  114. }
  115. i=READ_PERI_REG(UART_FIFO(0));
  116. return i;
  117. }
  118. //Send a char to the uart.
  119. static void ATTR_GDBFN gdbSendChar(char c) {
  120. while (((READ_PERI_REG(UART_STATUS(0))>>UART_TXFIFO_CNT_S)&UART_TXFIFO_CNT)>=126) ;
  121. WRITE_PERI_REG(UART_FIFO(0), c);
  122. }
  123. //Send the start of a packet; reset checksum calculation.
  124. static void ATTR_GDBFN gdbPacketStart() {
  125. chsum=0;
  126. gdbSendChar('$');
  127. }
  128. //Send a char as part of a packet
  129. static void ATTR_GDBFN gdbPacketChar(char c) {
  130. if (c=='#' || c=='$' || c=='}' || c=='*') {
  131. gdbSendChar('}');
  132. gdbSendChar(c^0x20);
  133. chsum+=(c^0x20)+'}';
  134. } else {
  135. gdbSendChar(c);
  136. chsum+=c;
  137. }
  138. }
  139. //Send a string as part of a packet
  140. static void ATTR_GDBFN gdbPacketStr(char *c) {
  141. while (*c!=0) {
  142. gdbPacketChar(*c);
  143. c++;
  144. }
  145. }
  146. //Send a hex val as part of a packet. 'bits'/4 dictates the number of hex chars sent.
  147. static void ATTR_GDBFN gdbPacketHex(int val, int bits) {
  148. char hexChars[]="0123456789abcdef";
  149. int i;
  150. for (i=bits; i>0; i-=4) {
  151. gdbPacketChar(hexChars[(val>>(i-4))&0xf]);
  152. }
  153. }
  154. //Finish sending a packet.
  155. static void ATTR_GDBFN gdbPacketEnd() {
  156. gdbSendChar('#');
  157. gdbPacketHex(chsum, 8);
  158. }
  159. //Error states used by the routines that grab stuff from the incoming gdb packet
  160. #define ST_ENDPACKET -1
  161. #define ST_ERR -2
  162. #define ST_OK -3
  163. #define ST_CONT -4
  164. //Grab a hex value from the gdb packet. Ptr will get positioned on the end
  165. //of the hex string, as far as the routine has read into it. Bits/4 indicates
  166. //the max amount of hex chars it gobbles up. Bits can be -1 to eat up as much
  167. //hex chars as possible.
  168. static long ATTR_GDBFN gdbGetHexVal(unsigned char **ptr, int bits) {
  169. int i;
  170. int no;
  171. unsigned int v=0;
  172. char c;
  173. no=bits/4;
  174. if (bits==-1) no=64;
  175. for (i=0; i<no; i++) {
  176. c=**ptr;
  177. (*ptr)++;
  178. if (c>='0' && c<='9') {
  179. v<<=4;
  180. v|=(c-'0');
  181. } else if (c>='A' && c<='F') {
  182. v<<=4;
  183. v|=(c-'A')+10;
  184. } else if (c>='a' && c<='f') {
  185. v<<=4;
  186. v|=(c-'a')+10;
  187. } else if (c=='#') {
  188. if (bits==-1) {
  189. (*ptr)--;
  190. return v;
  191. }
  192. return ST_ENDPACKET;
  193. } else {
  194. if (bits==-1) {
  195. (*ptr)--;
  196. return v;
  197. }
  198. return ST_ERR;
  199. }
  200. }
  201. return v;
  202. }
  203. //Swap an int into the form gdb wants it
  204. static int ATTR_GDBFN iswap(int i) {
  205. int r;
  206. r=((i>>24)&0xff);
  207. r|=((i>>16)&0xff)<<8;
  208. r|=((i>>8)&0xff)<<16;
  209. r|=((i>>0)&0xff)<<24;
  210. return r;
  211. }
  212. //Read a byte from the ESP8266 memory.
  213. static unsigned char ATTR_GDBFN readbyte(unsigned int p) {
  214. int *i=(int*)(p&(~3));
  215. if (p<0x20000000 || p>=0x60000000) return -1;
  216. return *i>>((p&3)*8);
  217. }
  218. //Write a byte to the ESP8266 memory.
  219. static void ATTR_GDBFN writeByte(unsigned int p, unsigned char d) {
  220. int *i=(int*)(p&(~3));
  221. if (p<0x20000000 || p>=0x60000000) return;
  222. if ((p&3)==0) *i=(*i&0xffffff00)|(d<<0);
  223. if ((p&3)==1) *i=(*i&0xffff00ff)|(d<<8);
  224. if ((p&3)==2) *i=(*i&0xff00ffff)|(d<<16);
  225. if ((p&3)==3) *i=(*i&0x00ffffff)|(d<<24);
  226. }
  227. //Returns 1 if it makes sense to write to addr p
  228. static int ATTR_GDBFN validWrAddr(int p) {
  229. if (p>=0x3ff00000 && p<0x40000000) return 1;
  230. if (p>=0x40100000 && p<0x40140000) return 1;
  231. if (p>=0x60000000 && p<0x60002000) return 1;
  232. return 0;
  233. }
  234. /*
  235. Register file in the format lx106 gdb port expects it.
  236. Inspired by gdb/regformats/reg-xtensa.dat from
  237. https://github.com/jcmvbkbc/crosstool-NG/blob/lx106-g%2B%2B/overlays/xtensa_lx106.tar
  238. As decoded by Cesanta.
  239. */
  240. struct regfile {
  241. uint32_t a[16];
  242. uint32_t pc;
  243. uint32_t sar;
  244. uint32_t litbase;
  245. uint32_t sr176;
  246. uint32_t sr208;
  247. uint32_t ps;
  248. };
  249. //Send the reason execution is stopped to GDB.
  250. static void ATTR_GDBFN sendReason() {
  251. #if 0
  252. char *reason=""; //default
  253. #endif
  254. //exception-to-signal mapping
  255. char exceptionSignal[]={4,31,11,11,2,6,8,0,6,7,0,0,7,7,7,7};
  256. int i=0;
  257. gdbPacketStart();
  258. gdbPacketChar('T');
  259. if (gdbstub_savedRegs.reason==0xff) {
  260. gdbPacketHex(2, 8); //sigint
  261. } else if (gdbstub_savedRegs.reason&0x80) {
  262. //We stopped because of an exception. Convert exception code to a signal number and send it.
  263. i=gdbstub_savedRegs.reason&0x7f;
  264. if (i<sizeof(exceptionSignal)) gdbPacketHex(exceptionSignal[i], 8); else gdbPacketHex(11, 8);
  265. } else {
  266. //We stopped because of a debugging exception.
  267. gdbPacketHex(5, 8); //sigtrap
  268. //Current Xtensa GDB versions don't seem to request this, so let's leave it off.
  269. #if 0
  270. if (gdbstub_savedRegs.reason&(1<<0)) reason="break";
  271. if (gdbstub_savedRegs.reason&(1<<1)) reason="hwbreak";
  272. if (gdbstub_savedRegs.reason&(1<<2)) reason="watch";
  273. if (gdbstub_savedRegs.reason&(1<<3)) reason="swbreak";
  274. if (gdbstub_savedRegs.reason&(1<<4)) reason="swbreak";
  275. gdbPacketStr(reason);
  276. gdbPacketChar(':');
  277. //ToDo: watch: send address
  278. #endif
  279. }
  280. gdbPacketEnd();
  281. }
  282. //Handle a command as received from GDB.
  283. static int ATTR_GDBFN gdbHandleCommand(unsigned char *cmd, int len) {
  284. //Handle a command
  285. int i, j, k;
  286. unsigned char *data=cmd+1;
  287. if (cmd[0]=='g') { //send all registers to gdb
  288. gdbPacketStart();
  289. gdbPacketHex(iswap(gdbstub_savedRegs.a0), 32);
  290. gdbPacketHex(iswap(gdbstub_savedRegs.a1), 32);
  291. for (i=2; i<16; i++) gdbPacketHex(iswap(gdbstub_savedRegs.a[i-2]), 32);
  292. gdbPacketHex(iswap(gdbstub_savedRegs.pc), 32);
  293. gdbPacketHex(iswap(gdbstub_savedRegs.sar), 32);
  294. gdbPacketHex(iswap(gdbstub_savedRegs.litbase), 32);
  295. gdbPacketHex(iswap(gdbstub_savedRegs.sr176), 32);
  296. gdbPacketHex(0, 32);
  297. gdbPacketHex(iswap(gdbstub_savedRegs.ps), 32);
  298. gdbPacketEnd();
  299. } else if (cmd[0]=='G') { //receive content for all registers from gdb
  300. gdbstub_savedRegs.a0=iswap(gdbGetHexVal(&data, 32));
  301. gdbstub_savedRegs.a1=iswap(gdbGetHexVal(&data, 32));
  302. for (i=2; i<16; i++) gdbstub_savedRegs.a[i-2]=iswap(gdbGetHexVal(&data, 32));
  303. gdbstub_savedRegs.pc=iswap(gdbGetHexVal(&data, 32));
  304. gdbstub_savedRegs.sar=iswap(gdbGetHexVal(&data, 32));
  305. gdbstub_savedRegs.litbase=iswap(gdbGetHexVal(&data, 32));
  306. gdbstub_savedRegs.sr176=iswap(gdbGetHexVal(&data, 32));
  307. gdbGetHexVal(&data, 32);
  308. gdbstub_savedRegs.ps=iswap(gdbGetHexVal(&data, 32));
  309. gdbPacketStart();
  310. gdbPacketStr("OK");
  311. gdbPacketEnd();
  312. } else if (cmd[0]=='m') { //read memory to gdb
  313. i=gdbGetHexVal(&data, -1);
  314. data++;
  315. j=gdbGetHexVal(&data, -1);
  316. gdbPacketStart();
  317. for (k=0; k<j; k++) {
  318. gdbPacketHex(readbyte(i++), 8);
  319. }
  320. gdbPacketEnd();
  321. } else if (cmd[0]=='M') { //write memory from gdb
  322. i=gdbGetHexVal(&data, -1); //addr
  323. data++; //skip ,
  324. j=gdbGetHexVal(&data, -1); //length
  325. data++; //skip :
  326. if (validWrAddr(i) && validWrAddr(i+j)) {
  327. for (k=0; k<j; k++) {
  328. writeByte(i, gdbGetHexVal(&data, 8));
  329. i++;
  330. }
  331. //Make sure caches are up-to-date. Procedure according to Xtensa ISA document, ISYNC inst desc.
  332. asm volatile("ISYNC\nISYNC\n");
  333. gdbPacketStart();
  334. gdbPacketStr("OK");
  335. gdbPacketEnd();
  336. } else {
  337. //Trying to do a software breakpoint on a flash proc, perhaps?
  338. gdbPacketStart();
  339. gdbPacketStr("E01");
  340. gdbPacketEnd();
  341. }
  342. } else if (cmd[0]=='?') { //Reply with stop reason
  343. sendReason();
  344. // } else if (strncmp(cmd, "vCont?", 6)==0) {
  345. // gdbPacketStart();
  346. // gdbPacketStr("vCont;c;s");
  347. // gdbPacketEnd();
  348. } else if (strncmp((char*)cmd, "vCont;c", 7)==0 || cmd[0]=='c') { //continue execution
  349. return ST_CONT;
  350. } else if (strncmp((char*)cmd, "vCont;s", 7)==0 || cmd[0]=='s') { //single-step instruction
  351. //Single-stepping can go wrong if an interrupt is pending, especially when it is e.g. a task switch:
  352. //the ICOUNT register will overflow in the task switch code. That is why we disable interupts when
  353. //doing single-instruction stepping.
  354. singleStepPs=gdbstub_savedRegs.ps;
  355. gdbstub_savedRegs.ps=(gdbstub_savedRegs.ps & ~0xf)|(XCHAL_DEBUGLEVEL-1);
  356. gdbstub_icount_ena_single_step();
  357. return ST_CONT;
  358. } else if (cmd[0]=='q') { //Extended query
  359. if (strncmp((char*)&cmd[1], "Supported", 9)==0) { //Capabilities query
  360. gdbPacketStart();
  361. gdbPacketStr("swbreak+;hwbreak+;PacketSize=255");
  362. gdbPacketEnd();
  363. } else {
  364. //We don't support other queries.
  365. gdbPacketStart();
  366. gdbPacketEnd();
  367. return ST_ERR;
  368. }
  369. } else if (cmd[0]=='Z') { //Set hardware break/watchpoint.
  370. data+=2; //skip 'x,'
  371. i=gdbGetHexVal(&data, -1);
  372. data++; //skip ','
  373. j=gdbGetHexVal(&data, -1);
  374. gdbPacketStart();
  375. if (cmd[1]=='1') { //Set breakpoint
  376. if (gdbstub_set_hw_breakpoint(i, j)) {
  377. gdbPacketStr("OK");
  378. } else {
  379. gdbPacketStr("E01");
  380. }
  381. } else if (cmd[1]=='2' || cmd[1]=='3' || cmd[1]=='4') { //Set watchpoint
  382. int access=0;
  383. int mask=0;
  384. if (cmd[1]=='2') access=2; //write
  385. if (cmd[1]=='3') access=1; //read
  386. if (cmd[1]=='4') access=3; //access
  387. if (j==1) mask=0x3F;
  388. if (j==2) mask=0x3E;
  389. if (j==4) mask=0x3C;
  390. if (j==8) mask=0x38;
  391. if (j==16) mask=0x30;
  392. if (j==32) mask=0x20;
  393. if (j==64) mask=0x00;
  394. if (mask!=0 && gdbstub_set_hw_watchpoint(i,mask, access)) {
  395. gdbPacketStr("OK");
  396. } else {
  397. gdbPacketStr("E01");
  398. }
  399. }
  400. gdbPacketEnd();
  401. } else if (cmd[0]=='z') { //Clear hardware break/watchpoint
  402. data+=2; //skip 'x,'
  403. i=gdbGetHexVal(&data, -1);
  404. data++; //skip ','
  405. j=gdbGetHexVal(&data, -1);
  406. gdbPacketStart();
  407. if (cmd[1]=='1') { //hardware breakpoint
  408. if (gdbstub_del_hw_breakpoint(i)) {
  409. gdbPacketStr("OK");
  410. } else {
  411. gdbPacketStr("E01");
  412. }
  413. } else if (cmd[1]=='2' || cmd[1]=='3' || cmd[1]=='4') { //hardware watchpoint
  414. if (gdbstub_del_hw_watchpoint(i)) {
  415. gdbPacketStr("OK");
  416. } else {
  417. gdbPacketStr("E01");
  418. }
  419. }
  420. gdbPacketEnd();
  421. } else {
  422. //We don't recognize or support whatever GDB just sent us.
  423. gdbPacketStart();
  424. gdbPacketEnd();
  425. return ST_ERR;
  426. }
  427. return ST_OK;
  428. }
  429. //Lower layer: grab a command packet and check the checksum
  430. //Calls gdbHandleCommand on the packet if the checksum is OK
  431. //Returns ST_OK on success, ST_ERR when checksum fails, a
  432. //character if it is received instead of the GDB packet
  433. //start char.
  434. static int ATTR_GDBFN gdbReadCommand() {
  435. unsigned char c;
  436. unsigned char chsum=0, rchsum;
  437. unsigned char sentchs[2];
  438. int p=0;
  439. unsigned char *ptr;
  440. c=gdbRecvChar();
  441. if (c!='$') return c;
  442. while(1) {
  443. c=gdbRecvChar();
  444. if (c=='#') { //end of packet, checksum follows
  445. cmd[p]=0;
  446. break;
  447. }
  448. chsum+=c;
  449. if (c=='$') {
  450. //Wut, restart packet?
  451. chsum=0;
  452. p=0;
  453. continue;
  454. }
  455. if (c=='}') { //escape the next char
  456. c=gdbRecvChar();
  457. chsum+=c;
  458. c^=0x20;
  459. }
  460. cmd[p++]=c;
  461. if (p>=PBUFLEN) return ST_ERR;
  462. }
  463. //A # has been received. Get and check the received chsum.
  464. sentchs[0]=gdbRecvChar();
  465. sentchs[1]=gdbRecvChar();
  466. ptr=&sentchs[0];
  467. rchsum=gdbGetHexVal(&ptr, 8);
  468. // os_printf("c %x r %x\n", chsum, rchsum);
  469. if (rchsum!=chsum) {
  470. gdbSendChar('-');
  471. return ST_ERR;
  472. } else {
  473. gdbSendChar('+');
  474. return gdbHandleCommand(cmd, p);
  475. }
  476. }
  477. //Get the value of one of the A registers
  478. static unsigned int ATTR_GDBFN getaregval(int reg) {
  479. if (reg==0) return gdbstub_savedRegs.a0;
  480. if (reg==1) return gdbstub_savedRegs.a1;
  481. return gdbstub_savedRegs.a[reg-2];
  482. }
  483. //Set the value of one of the A registers
  484. static void ATTR_GDBFN setaregval(int reg, unsigned int val) {
  485. os_printf("%x -> %x\n", val, reg);
  486. if (reg==0) gdbstub_savedRegs.a0=val;
  487. if (reg==1) gdbstub_savedRegs.a1=val;
  488. gdbstub_savedRegs.a[reg-2]=val;
  489. }
  490. //Emulate the l32i/s32i instruction we're stopped at.
  491. static void ATTR_GDBFN emulLdSt() {
  492. unsigned char i0=readbyte(gdbstub_savedRegs.pc);
  493. unsigned char i1=readbyte(gdbstub_savedRegs.pc+1);
  494. unsigned char i2=readbyte(gdbstub_savedRegs.pc+2);
  495. int *p;
  496. if ((i0&0xf)==2 && (i1&0xf0)==0x20) {
  497. //l32i
  498. p=(int*)getaregval(i1&0xf)+(i2*4);
  499. setaregval(i0>>4, *p);
  500. gdbstub_savedRegs.pc+=3;
  501. } else if ((i0&0xf)==0x8) {
  502. //l32i.n
  503. p=(int*)getaregval(i1&0xf)+((i1>>4)*4);
  504. setaregval(i0>>4, *p);
  505. gdbstub_savedRegs.pc+=2;
  506. } else if ((i0&0xf)==2 && (i1&0xf0)==0x60) {
  507. //s32i
  508. p=(int*)getaregval(i1&0xf)+(i2*4);
  509. *p=getaregval(i0>>4);
  510. gdbstub_savedRegs.pc+=3;
  511. } else if ((i0&0xf)==0x9) {
  512. //s32i.n
  513. p=(int*)getaregval(i1&0xf)+((i1>>4)*4);
  514. *p=getaregval(i0>>4);
  515. gdbstub_savedRegs.pc+=2;
  516. } else {
  517. os_printf("GDBSTUB: No l32i/s32i instruction: %x %x %x. Huh?", i2, i1, i0);
  518. }
  519. }
  520. //We just caught a debug exception and need to handle it. This is called from an assembly
  521. //routine in gdbstub-entry.S
  522. void ATTR_GDBFN gdbstub_handle_debug_exception() {
  523. ets_wdt_disable();
  524. if (singleStepPs!=-1) {
  525. //We come here after single-stepping an instruction. Interrupts are disabled
  526. //for the single step. Re-enable them here.
  527. gdbstub_savedRegs.ps=(gdbstub_savedRegs.ps&~0xf)|(singleStepPs&0xf);
  528. singleStepPs=-1;
  529. }
  530. sendReason();
  531. while(gdbReadCommand()!=ST_CONT);
  532. if ((gdbstub_savedRegs.reason&0x84)==0x4) {
  533. //We stopped due to a watchpoint. We can't re-execute the current instruction
  534. //because it will happily re-trigger the same watchpoint, so we emulate it
  535. //while we're still in debugger space.
  536. emulLdSt();
  537. } else if ((gdbstub_savedRegs.reason&0x88)==0x8) {
  538. //We stopped due to a BREAK instruction. Skip over it.
  539. //Check the instruction first; gdb may have replaced it with the original instruction
  540. //if it's one of the breakpoints it set.
  541. if (readbyte(gdbstub_savedRegs.pc+2)==0 &&
  542. (readbyte(gdbstub_savedRegs.pc+1)&0xf0)==0x40 &&
  543. (readbyte(gdbstub_savedRegs.pc)&0x0f)==0x00) {
  544. gdbstub_savedRegs.pc+=3;
  545. }
  546. } else if ((gdbstub_savedRegs.reason&0x90)==0x10) {
  547. //We stopped due to a BREAK.N instruction. Skip over it, after making sure the instruction
  548. //actually is a BREAK.N
  549. if ((readbyte(gdbstub_savedRegs.pc+1)&0xf0)==0xf0 &&
  550. readbyte(gdbstub_savedRegs.pc)==0x2d) {
  551. gdbstub_savedRegs.pc+=3;
  552. }
  553. }
  554. ets_wdt_enable();
  555. }
  556. #if GDBSTUB_FREERTOS
  557. //Freetos exception. This routine is called by an assembly routine in gdbstub-entry.S
  558. void ATTR_GDBFN gdbstub_handle_user_exception() {
  559. ets_wdt_disable();
  560. gdbstub_savedRegs.reason|=0x80; //mark as an exception reason
  561. sendReason();
  562. while(gdbReadCommand()!=ST_CONT);
  563. ets_wdt_enable();
  564. }
  565. #else
  566. //Non-OS exception handler. Gets called by the Xtensa HAL.
  567. static void ATTR_GDBFN gdb_exception_handler(struct XTensa_exception_frame_s *frame) {
  568. //Save the extra registers the Xtensa HAL doesn't save
  569. gdbstub_save_extra_sfrs_for_exception();
  570. //Copy registers the Xtensa HAL did save to gdbstub_savedRegs
  571. os_memcpy(&gdbstub_savedRegs, frame, 19*4);
  572. //Credits go to Cesanta for this trick. A1 seems to be destroyed, but because it
  573. //has a fixed offset from the address of the passed frame, we can recover it.
  574. gdbstub_savedRegs.a1=(uint32_t)frame+EXCEPTION_GDB_SP_OFFSET;
  575. gdbstub_savedRegs.reason|=0x80; //mark as an exception reason
  576. ets_wdt_disable();
  577. sendReason();
  578. while(gdbReadCommand()!=ST_CONT);
  579. ets_wdt_enable();
  580. //Copy any changed registers back to the frame the Xtensa HAL uses.
  581. os_memcpy(frame, &gdbstub_savedRegs, 19*4);
  582. }
  583. #endif
  584. #if GDBSTUB_REDIRECT_CONSOLE_OUTPUT
  585. //Replacement putchar1 routine. Instead of spitting out the character directly, it will buffer up to
  586. //OBUFLEN characters (or up to a \n, whichever comes earlier) and send it out as a gdb stdout packet.
  587. static void ATTR_GDBFN gdb_semihost_putchar1(char c) {
  588. int i;
  589. obuf[obufpos++]=c;
  590. if (c=='\n' || obufpos==OBUFLEN) {
  591. gdbPacketStart();
  592. gdbPacketChar('O');
  593. for (i=0; i<obufpos; i++) gdbPacketHex(obuf[i], 8);
  594. gdbPacketEnd();
  595. obufpos=0;
  596. }
  597. }
  598. #endif
  599. #if !GDBSTUB_FREERTOS
  600. //The OS-less SDK uses the Xtensa HAL to handle exceptions. We can use those functions to catch any
  601. //fatal exceptions and invoke the debugger when this happens.
  602. static void ATTR_GDBINIT install_exceptions() {
  603. int i;
  604. int exno[]={EXCCAUSE_ILLEGAL, EXCCAUSE_SYSCALL, EXCCAUSE_INSTR_ERROR, EXCCAUSE_LOAD_STORE_ERROR,
  605. EXCCAUSE_DIVIDE_BY_ZERO, EXCCAUSE_UNALIGNED, EXCCAUSE_INSTR_DATA_ERROR, EXCCAUSE_LOAD_STORE_DATA_ERROR,
  606. EXCCAUSE_INSTR_ADDR_ERROR, EXCCAUSE_LOAD_STORE_ADDR_ERROR, EXCCAUSE_INSTR_PROHIBITED,
  607. EXCCAUSE_LOAD_PROHIBITED, EXCCAUSE_STORE_PROHIBITED};
  608. for (i=0; i<(sizeof(exno)/sizeof(exno[0])); i++) {
  609. _xtos_set_exception_handler(exno[i], gdb_exception_handler);
  610. }
  611. }
  612. #else
  613. //FreeRTOS doesn't use the Xtensa HAL for exceptions, but uses its own fatal exception handler.
  614. //We use a small hack to replace that with a jump to our own handler, which then has the task of
  615. //decyphering and re-instating the registers the FreeRTOS code left.
  616. extern void user_fatal_exception_handler();
  617. extern void gdbstub_user_exception_entry();
  618. static void ATTR_GDBINIT install_exceptions() {
  619. //Replace the user_fatal_exception_handler by a jump to our own code
  620. int *ufe=(int*)user_fatal_exception_handler;
  621. //This mess encodes as a relative jump instruction to user_fatal_exception_handler
  622. *ufe=((((int)gdbstub_user_exception_entry-(int)user_fatal_exception_handler)-4)<<6)|6;
  623. }
  624. #endif
  625. #if GDBSTUB_CTRLC_BREAK
  626. #if !GDBSTUB_FREERTOS
  627. static void ATTR_GDBFN uart_hdlr(void *arg, void *frame) {
  628. int doDebug=0, fifolen=0;
  629. //Save the extra registers the Xtensa HAL doesn't save
  630. gdbstub_save_extra_sfrs_for_exception();
  631. fifolen=(READ_PERI_REG(UART_STATUS(0))>>UART_RXFIFO_CNT_S)&UART_RXFIFO_CNT;
  632. while (fifolen!=0) {
  633. if ((READ_PERI_REG(UART_FIFO(0)) & 0xFF)==0x3) doDebug=1; //Check if any of the chars is control-C. Throw away rest.
  634. fifolen--;
  635. }
  636. WRITE_PERI_REG(UART_INT_CLR(0), UART_RXFIFO_FULL_INT_CLR|UART_RXFIFO_TOUT_INT_CLR);
  637. if (doDebug) {
  638. //Copy registers the Xtensa HAL did save to gdbstub_savedRegs
  639. os_memcpy(&gdbstub_savedRegs, frame, 19*4);
  640. gdbstub_savedRegs.a1=(uint32_t)frame+EXCEPTION_GDB_SP_OFFSET;
  641. gdbstub_savedRegs.reason=0xff; //mark as user break reason
  642. ets_wdt_disable();
  643. sendReason();
  644. while(gdbReadCommand()!=ST_CONT);
  645. ets_wdt_enable();
  646. //Copy any changed registers back to the frame the Xtensa HAL uses.
  647. os_memcpy(frame, &gdbstub_savedRegs, 19*4);
  648. }
  649. }
  650. static void ATTR_GDBINIT install_uart_hdlr() {
  651. ets_isr_attach(ETS_UART_INUM, uart_hdlr, NULL);
  652. SET_PERI_REG_MASK(UART_INT_ENA(0), UART_RXFIFO_FULL_INT_ENA|UART_RXFIFO_TOUT_INT_ENA);
  653. ets_isr_unmask((1<<ETS_UART_INUM)); //enable uart interrupt
  654. }
  655. #else
  656. void ATTR_GDBFN gdbstub_handle_uart_int(struct XTensa_rtos_int_frame_s *frame) {
  657. int doDebug=0, fifolen=0, x;
  658. fifolen=(READ_PERI_REG(UART_STATUS(0))>>UART_RXFIFO_CNT_S)&UART_RXFIFO_CNT;
  659. while (fifolen!=0) {
  660. if ((READ_PERI_REG(UART_FIFO(0)) & 0xFF)==0x3) doDebug=1; //Check if any of the chars is control-C. Throw away rest.
  661. fifolen--;
  662. }
  663. WRITE_PERI_REG(UART_INT_CLR(0), UART_RXFIFO_FULL_INT_CLR|UART_RXFIFO_TOUT_INT_CLR);
  664. if (doDebug) {
  665. //Copy registers the Xtensa HAL did save to gdbstub_savedRegs
  666. gdbstub_savedRegs.pc=frame->pc;
  667. gdbstub_savedRegs.ps=frame->ps;
  668. gdbstub_savedRegs.sar=frame->sar;
  669. gdbstub_savedRegs.a0=frame->a[0];
  670. gdbstub_savedRegs.a1=frame->a[1];
  671. for (x=2; x<16; x++) gdbstub_savedRegs.a[x-2]=frame->a[x];
  672. // gdbstub_savedRegs.a1=(uint32_t)frame+EXCEPTION_GDB_SP_OFFSET;
  673. gdbstub_savedRegs.reason=0xff; //mark as user break reason
  674. // ets_wdt_disable();
  675. sendReason();
  676. while(gdbReadCommand()!=ST_CONT);
  677. // ets_wdt_enable();
  678. //Copy any changed registers back to the frame the Xtensa HAL uses.
  679. frame->pc=gdbstub_savedRegs.pc;
  680. frame->ps=gdbstub_savedRegs.ps;
  681. frame->sar=gdbstub_savedRegs.sar;
  682. frame->a[0]=gdbstub_savedRegs.a0;
  683. frame->a[1]=gdbstub_savedRegs.a1;
  684. for (x=2; x<16; x++) frame->a[x]=gdbstub_savedRegs.a[x-2];
  685. }
  686. }
  687. static void ATTR_GDBINIT install_uart_hdlr() {
  688. _xt_isr_attach(ETS_UART_INUM, gdbstub_uart_entry);
  689. SET_PERI_REG_MASK(UART_INT_ENA(0), UART_RXFIFO_FULL_INT_ENA|UART_RXFIFO_TOUT_INT_ENA);
  690. _xt_isr_unmask((1<<ETS_UART_INUM)); //enable uart interrupt
  691. }
  692. #endif
  693. #endif
  694. //gdbstub initialization routine.
  695. void ATTR_GDBINIT gdbstub_init() {
  696. #if GDBSTUB_REDIRECT_CONSOLE_OUTPUT
  697. os_install_putc1(gdb_semihost_putchar1);
  698. #endif
  699. #if GDBSTUB_CTRLC_BREAK
  700. install_uart_hdlr();
  701. #endif
  702. install_exceptions();
  703. gdbstub_init_debug_entry();
  704. #if GDBSTUB_BREAK_ON_INIT
  705. gdbstub_do_break();
  706. #endif
  707. }