]> granicus.if.org Git - esp-idf/blob - components/esp32/gdbstub.c
Merge remote-tracking branch 'origin/master' into feature/better_idle_tick_hook_mechanism
[esp-idf] / components / esp32 / gdbstub.c
1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /******************************************************************************
16  * Description: A stub to make the ESP32 debuggable by GDB over the serial 
17  * port, at least enough to do a backtrace on panic. This gdbstub is read-only:
18  * it allows inspecting the ESP32 state 
19  *******************************************************************************/
20
21 //ToDo: Clean up includes and sync to real rtos
22 #include "rom/ets_sys.h"
23
24 #include "soc/uart_reg.h"
25 #include "soc/io_mux_reg.h"
26
27 #include "esp_gdbstub.h"
28 #include "driver/gpio.h"
29
30 //Length of buffer used to reserve GDB commands. Has to be at least able to fit the G command, which
31 //implies a minimum size of about 320 bytes.
32 #define PBUFLEN 512
33
34 static unsigned char cmd[PBUFLEN];              //GDB command input buffer
35 static char chsum;                                              //Running checksum of the output packet
36
37 #define ATTR_GDBFN
38
39 static void ATTR_GDBFN keepWDTalive() {
40         //ToDo for esp31/32
41 }
42
43
44 //Receive a char from the uart. Uses polling and feeds the watchdog.
45 static int ATTR_GDBFN gdbRecvChar() {
46         int i;
47         while (((READ_PERI_REG(UART_STATUS_REG(0))>>UART_RXFIFO_CNT_S)&UART_RXFIFO_CNT)==0) {
48                 keepWDTalive();
49         }
50         i=READ_PERI_REG(UART_FIFO_REG(0));
51         return i;
52 }
53
54 //Send a char to the uart.
55 static void ATTR_GDBFN gdbSendChar(char c) {
56         while (((READ_PERI_REG(UART_STATUS_REG(0))>>UART_TXFIFO_CNT_S)&UART_TXFIFO_CNT)>=126) ;
57         WRITE_PERI_REG(UART_FIFO_REG(0), c);
58 }
59
60 //Send the start of a packet; reset checksum calculation.
61 static void ATTR_GDBFN gdbPacketStart() {
62         chsum=0;
63         gdbSendChar('$');
64 }
65
66 //Send a char as part of a packet
67 static void ATTR_GDBFN gdbPacketChar(char c) {
68         if (c=='#' || c=='$' || c=='}' || c=='*') {
69                 gdbSendChar('}');
70                 gdbSendChar(c^0x20);
71                 chsum+=(c^0x20)+'}';
72         } else {
73                 gdbSendChar(c);
74                 chsum+=c;
75         }
76 }
77
78 //Send a string as part of a packet
79 static void ATTR_GDBFN gdbPacketStr(char *c) {
80         while (*c!=0) {
81                 gdbPacketChar(*c);
82                 c++;
83         }
84 }
85
86 //Send a hex val as part of a packet. 'bits'/4 dictates the number of hex chars sent.
87 static void ATTR_GDBFN gdbPacketHex(int val, int bits) {
88         char hexChars[]="0123456789abcdef";
89         int i;
90         for (i=bits; i>0; i-=4) {
91                 gdbPacketChar(hexChars[(val>>(i-4))&0xf]);
92         }
93 }
94
95 //Finish sending a packet.
96 static void ATTR_GDBFN gdbPacketEnd() {
97         gdbSendChar('#');
98         gdbPacketHex(chsum, 8);
99 }
100
101 //Error states used by the routines that grab stuff from the incoming gdb packet
102 #define ST_ENDPACKET -1
103 #define ST_ERR -2
104 #define ST_OK -3
105 #define ST_CONT -4
106
107 //Grab a hex value from the gdb packet. Ptr will get positioned on the end
108 //of the hex string, as far as the routine has read into it. Bits/4 indicates
109 //the max amount of hex chars it gobbles up. Bits can be -1 to eat up as much
110 //hex chars as possible.
111 static long ATTR_GDBFN gdbGetHexVal(unsigned char **ptr, int bits) {
112         int i;
113         int no;
114         unsigned int v=0;
115         char c;
116         no=bits/4;
117         if (bits==-1) no=64;
118         for (i=0; i<no; i++) {
119                 c=**ptr;
120                 (*ptr)++;
121                 if (c>='0' && c<='9') {
122                         v<<=4;
123                         v|=(c-'0');
124                 } else if (c>='A' && c<='F') {
125                         v<<=4;
126                         v|=(c-'A')+10;
127                 } else if (c>='a' && c<='f') {
128                         v<<=4;
129                         v|=(c-'a')+10;
130                 } else if (c=='#') {
131                         if (bits==-1) {
132                                 (*ptr)--;
133                                 return v;
134                         }
135                         return ST_ENDPACKET;
136                 } else {
137                         if (bits==-1) {
138                                 (*ptr)--;
139                                 return v;
140                         }
141                         return ST_ERR;
142                 }
143         }
144         return v;
145 }
146
147 //Swap an int into the form gdb wants it
148 static int ATTR_GDBFN iswap(int i) {
149         int r;
150         r=((i>>24)&0xff);
151         r|=((i>>16)&0xff)<<8;
152         r|=((i>>8)&0xff)<<16;
153         r|=((i>>0)&0xff)<<24;
154         return r;
155 }
156
157 //Read a byte from ESP32 memory.
158 static unsigned char ATTR_GDBFN readbyte(unsigned int p) {
159         int *i=(int*)(p&(~3));
160         if (p<0x20000000 || p>=0x80000000) return -1;
161         return *i>>((p&3)*8);
162 }
163
164
165 //Register file in the format exp108 gdb port expects it.
166 //Inspired by gdb/regformats/reg-xtensa.dat
167 typedef struct {
168         uint32_t pc;
169         uint32_t a[64];
170         uint32_t lbeg;
171         uint32_t lend;
172         uint32_t lcount;
173         uint32_t sar;
174         uint32_t windowbase;
175         uint32_t windowstart;
176         uint32_t configid0;
177         uint32_t configid1;
178         uint32_t ps;
179         uint32_t threadptr;
180         uint32_t br;
181         uint32_t scompare1;
182         uint32_t acclo;
183         uint32_t acchi;
184         uint32_t m0;
185         uint32_t m1;
186         uint32_t m2;
187         uint32_t m3;
188         uint32_t expstate;  //I'm going to assume this is exccause...
189         uint32_t f64r_lo;
190         uint32_t f64r_hi;
191         uint32_t f64s;
192         uint32_t f[16];
193         uint32_t fcr;
194         uint32_t fsr;
195 } GdbRegFile;
196
197
198 GdbRegFile gdbRegFile;
199
200 /* 
201 //Register format as the Xtensa HAL has it:
202 STRUCT_FIELD (long, 4, XT_STK_EXIT,     exit)
203 STRUCT_FIELD (long, 4, XT_STK_PC,       pc)
204 STRUCT_FIELD (long, 4, XT_STK_PS,       ps)
205 STRUCT_FIELD (long, 4, XT_STK_A0,       a0)
206 [..]
207 STRUCT_FIELD (long, 4, XT_STK_A15,      a15)
208 STRUCT_FIELD (long, 4, XT_STK_SAR,      sar)
209 STRUCT_FIELD (long, 4, XT_STK_EXCCAUSE, exccause)
210 STRUCT_FIELD (long, 4, XT_STK_EXCVADDR, excvaddr)
211 STRUCT_FIELD (long, 4, XT_STK_LBEG,   lbeg)
212 STRUCT_FIELD (long, 4, XT_STK_LEND,   lend)
213 STRUCT_FIELD (long, 4, XT_STK_LCOUNT, lcount)
214 // Temporary space for saving stuff during window spill 
215 STRUCT_FIELD (long, 4, XT_STK_TMP0,   tmp0)
216 STRUCT_FIELD (long, 4, XT_STK_TMP1,   tmp1)
217 STRUCT_FIELD (long, 4, XT_STK_TMP2,   tmp2)
218 STRUCT_FIELD (long, 4, XT_STK_VPRI,   vpri)
219 STRUCT_FIELD (long, 4, XT_STK_OVLY,   ovly)
220 #endif
221 STRUCT_END(XtExcFrame)
222 */
223
224
225 static void dumpHwToRegfile(XtExcFrame *frame) {
226         int i;
227         long *frameAregs=&frame->a0;
228         gdbRegFile.pc=frame->pc;
229         for (i=0; i<16; i++) gdbRegFile.a[i]=frameAregs[i];
230         for (i=16; i<64; i++) gdbRegFile.a[i]=0xDEADBEEF;
231         gdbRegFile.lbeg=frame->lbeg;
232         gdbRegFile.lend=frame->lend;
233         gdbRegFile.lcount=frame->lcount;
234         gdbRegFile.sar=frame->sar;
235         //All windows have been spilled to the stack by the ISR routines. The following values should indicate that.
236         gdbRegFile.sar=frame->sar;
237         gdbRegFile.windowbase=0; //0
238         gdbRegFile.windowstart=0x1; //1
239         gdbRegFile.configid0=0xdeadbeef; //ToDo
240         gdbRegFile.configid1=0xdeadbeef; //ToDo
241         gdbRegFile.ps=frame->ps-PS_EXCM_MASK;
242         gdbRegFile.threadptr=0xdeadbeef; //ToDo
243         gdbRegFile.br=0xdeadbeef; //ToDo
244         gdbRegFile.scompare1=0xdeadbeef; //ToDo
245         gdbRegFile.acclo=0xdeadbeef; //ToDo
246         gdbRegFile.acchi=0xdeadbeef; //ToDo
247         gdbRegFile.m0=0xdeadbeef; //ToDo
248         gdbRegFile.m1=0xdeadbeef; //ToDo
249         gdbRegFile.m2=0xdeadbeef; //ToDo
250         gdbRegFile.m3=0xdeadbeef; //ToDo
251         gdbRegFile.expstate=frame->exccause; //ToDo
252 }
253
254
255 //Send the reason execution is stopped to GDB.
256 static void sendReason() {
257         //exception-to-signal mapping
258         char exceptionSignal[]={4,31,11,11,2,6,8,0,6,7,0,0,7,7,7,7};
259         int i=0;
260         gdbPacketStart();
261         gdbPacketChar('T');
262         i=gdbRegFile.expstate&0x7f;
263         if (i<sizeof(exceptionSignal)) return gdbPacketHex(exceptionSignal[i], 8); else gdbPacketHex(11, 8);
264         gdbPacketEnd();
265 }
266
267 //Handle a command as received from GDB.
268 static int gdbHandleCommand(unsigned char *cmd, int len) {
269         //Handle a command
270         int i, j, k;
271         unsigned char *data=cmd+1;
272         if (cmd[0]=='g') {              //send all registers to gdb
273                 int *p=(int*)&gdbRegFile;
274                 gdbPacketStart();
275                 for (i=0; i<sizeof(GdbRegFile)/4; i++) gdbPacketHex(iswap(*p++), 32);
276                 gdbPacketEnd();
277         } else if (cmd[0]=='G') {       //receive content for all registers from gdb
278                 int *p=(int*)&gdbRegFile;
279                 for (i=0; i<sizeof(GdbRegFile)/4; i++) *p++=iswap(gdbGetHexVal(&data, 32));;
280                 gdbPacketStart();
281                 gdbPacketStr("OK");
282                 gdbPacketEnd();
283         } else if (cmd[0]=='m') {       //read memory to gdb
284                 i=gdbGetHexVal(&data, -1);
285                 data++;
286                 j=gdbGetHexVal(&data, -1);
287                 gdbPacketStart();
288                 for (k=0; k<j; k++) {
289                         gdbPacketHex(readbyte(i++), 8);
290                 }
291                 gdbPacketEnd();
292         } else if (cmd[0]=='?') {       //Reply with stop reason
293                 sendReason();
294         } else {
295                 //We don't recognize or support whatever GDB just sent us.
296                 gdbPacketStart();
297                 gdbPacketEnd();
298                 return ST_ERR;
299         }
300         return ST_OK;
301 }
302
303
304 //Lower layer: grab a command packet and check the checksum
305 //Calls gdbHandleCommand on the packet if the checksum is OK
306 //Returns ST_OK on success, ST_ERR when checksum fails, a 
307 //character if it is received instead of the GDB packet
308 //start char.
309 static int gdbReadCommand() {
310         unsigned char c;
311         unsigned char chsum=0, rchsum;
312         unsigned char sentchs[2];
313         int p=0;
314         unsigned char *ptr;
315         c=gdbRecvChar();
316         if (c!='$') return c;
317         while(1) {
318                 c=gdbRecvChar();
319                 if (c=='#') {   //end of packet, checksum follows
320                         cmd[p]=0;
321                         break;
322                 }
323                 chsum+=c;
324                 if (c=='$') {
325                         //Wut, restart packet?
326                         chsum=0;
327                         p=0;
328                         continue;
329                 }
330                 if (c=='}') {           //escape the next char
331                         c=gdbRecvChar();
332                         chsum+=c;
333                         c^=0x20;
334                 }
335                 cmd[p++]=c;
336                 if (p>=PBUFLEN) return ST_ERR;
337         }
338         //A # has been received. Get and check the received chsum.
339         sentchs[0]=gdbRecvChar();
340         sentchs[1]=gdbRecvChar();
341         ptr=&sentchs[0];
342         rchsum=gdbGetHexVal(&ptr, 8);
343 //      ets_printf("c %x r %x\n", chsum, rchsum);
344         if (rchsum!=chsum) {
345                 gdbSendChar('-');
346                 return ST_ERR;
347         } else {
348                 gdbSendChar('+');
349                 return gdbHandleCommand(cmd, p);
350         }
351 }
352
353
354
355 void esp_gdbstub_panic_handler(XtExcFrame *frame) {
356         dumpHwToRegfile(frame);
357         //Make sure txd/rxd are enabled
358         gpio_pullup_dis(1);
359         PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_U0RXD_U0RXD);
360         PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD_U0TXD);
361
362         sendReason();
363         while(gdbReadCommand()!=ST_CONT);
364         while(1);
365 }
366