1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
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 *******************************************************************************/
21 //ToDo: Clean up includes and sync to real rtos
22 #include "rom/ets_sys.h"
24 #include "soc/uart_reg.h"
25 #include "soc/io_mux_reg.h"
27 #include "esp_gdbstub.h"
28 #include "driver/gpio.h"
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.
34 static unsigned char cmd[PBUFLEN]; //GDB command input buffer
35 static char chsum; //Running checksum of the output packet
39 static void ATTR_GDBFN keepWDTalive() {
44 //Receive a char from the uart. Uses polling and feeds the watchdog.
45 static int ATTR_GDBFN gdbRecvChar() {
47 while (((READ_PERI_REG(UART_STATUS_REG(0))>>UART_RXFIFO_CNT_S)&UART_RXFIFO_CNT)==0) {
50 i=READ_PERI_REG(UART_FIFO_REG(0));
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);
60 //Send the start of a packet; reset checksum calculation.
61 static void ATTR_GDBFN gdbPacketStart() {
66 //Send a char as part of a packet
67 static void ATTR_GDBFN gdbPacketChar(char c) {
68 if (c=='#' || c=='$' || c=='}' || c=='*') {
78 //Send a string as part of a packet
79 static void ATTR_GDBFN gdbPacketStr(char *c) {
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";
90 for (i=bits; i>0; i-=4) {
91 gdbPacketChar(hexChars[(val>>(i-4))&0xf]);
95 //Finish sending a packet.
96 static void ATTR_GDBFN gdbPacketEnd() {
98 gdbPacketHex(chsum, 8);
101 //Error states used by the routines that grab stuff from the incoming gdb packet
102 #define ST_ENDPACKET -1
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) {
118 for (i=0; i<no; i++) {
121 if (c>='0' && c<='9') {
124 } else if (c>='A' && c<='F') {
127 } else if (c>='a' && c<='f') {
147 //Swap an int into the form gdb wants it
148 static int ATTR_GDBFN iswap(int i) {
151 r|=((i>>16)&0xff)<<8;
152 r|=((i>>8)&0xff)<<16;
153 r|=((i>>0)&0xff)<<24;
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);
165 //Register file in the format exp108 gdb port expects it.
166 //Inspired by gdb/regformats/reg-xtensa.dat
175 uint32_t windowstart;
188 uint32_t expstate; //I'm going to assume this is exccause...
198 GdbRegFile gdbRegFile;
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)
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)
221 STRUCT_END(XtExcFrame)
225 static void dumpHwToRegfile(XtExcFrame *frame) {
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
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};
262 i=gdbRegFile.expstate&0x7f;
263 if (i<sizeof(exceptionSignal)) return gdbPacketHex(exceptionSignal[i], 8); else gdbPacketHex(11, 8);
267 //Handle a command as received from GDB.
268 static int gdbHandleCommand(unsigned char *cmd, int len) {
271 unsigned char *data=cmd+1;
272 if (cmd[0]=='g') { //send all registers to gdb
273 int *p=(int*)&gdbRegFile;
275 for (i=0; i<sizeof(GdbRegFile)/4; i++) gdbPacketHex(iswap(*p++), 32);
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));;
283 } else if (cmd[0]=='m') { //read memory to gdb
284 i=gdbGetHexVal(&data, -1);
286 j=gdbGetHexVal(&data, -1);
288 for (k=0; k<j; k++) {
289 gdbPacketHex(readbyte(i++), 8);
292 } else if (cmd[0]=='?') { //Reply with stop reason
295 //We don't recognize or support whatever GDB just sent us.
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
309 static int gdbReadCommand() {
311 unsigned char chsum=0, rchsum;
312 unsigned char sentchs[2];
316 if (c!='$') return c;
319 if (c=='#') { //end of packet, checksum follows
325 //Wut, restart packet?
330 if (c=='}') { //escape the next char
336 if (p>=PBUFLEN) return ST_ERR;
338 //A # has been received. Get and check the received chsum.
339 sentchs[0]=gdbRecvChar();
340 sentchs[1]=gdbRecvChar();
342 rchsum=gdbGetHexVal(&ptr, 8);
343 // ets_printf("c %x r %x\n", chsum, rchsum);
349 return gdbHandleCommand(cmd, p);
355 void esp_gdbstub_panic_handler(XtExcFrame *frame) {
356 dumpHwToRegfile(frame);
357 //Make sure txd/rxd are enabled
359 PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_U0RXD_U0RXD);
360 PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD_U0TXD);
363 while(gdbReadCommand()!=ST_CONT);