Day: May 12, 2021

Debugging ESP32 using OpenOCD and GDB

Run OpenOCD in the first terminal:

$ openocd -s tcl -c 'set ESP_RTOS none' -f interface/ftdi/esp32_devkitj_v1.cfg -f target/esp32.cfg
Open On-Chip Debugger v0.10.0-esp32-20210401-13-g5d011fa5 (2021-05-12-12:41)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
none
adapter speed: 20000 kHz

Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : ftdi: if you experience problems at higher adapter clocks, try the command "ftdi_tdo_sample_edge falling"
Info : clock speed 20000 kHz
Info : JTAG tap: esp32.cpu0 tap/device found: 0x120034e5 (mfg: 0x272 (Tensilica), part: 0x2003, ver: 0x1)
Info : JTAG tap: esp32.cpu1 tap/device found: 0x120034e5 (mfg: 0x272 (Tensilica), part: 0x2003, ver: 0x1)
Info : esp32.cpu0: Debug controller was reset.
Info : esp32.cpu0: Core was reset.
Info : Listening on port 3333 for gdb connections
Info : esp32.cpu0: Debug controller was reset.
Info : esp32.cpu0: Debug controller was reset.
Info : esp32.cpu0: Debug controller was reset.
Info : accepting 'gdb' connection on tcp/3333
Info : esp32.cpu0: Target halted, PC=0x400E692C, debug_reason=00000000
Info : Set GDB target to 'esp32.cpu0'
Info : esp32.cpu0: Target halted, PC=0x4009290A, debug_reason=00000001
Info : Flash mapping 0: 0x10020 -> 0x3f400020, 7 KB
Info : Flash mapping 1: 0x20020 -> 0x400d0020, 96 KB
Info : esp32.cpu0: Target halted, PC=0x4009290A, debug_reason=00000001
Info : Auto-detected flash bank 'esp32.cpu0.flash' size 4096 KB
Info : Using flash bank 'esp32.cpu0.flash' size 4096 KB
Info : esp32.cpu0: Target halted, PC=0x4009290A, debug_reason=00000001
Info : Flash mapping 0: 0x10020 -> 0x3f400020, 7 KB
Info : Flash mapping 1: 0x20020 -> 0x400d0020, 96 KB
Info : Using flash bank 'esp32.cpu0.irom' size 100 KB
Info : esp32.cpu0: Target halted, PC=0x4009290A, debug_reason=00000001
Info : Flash mapping 0: 0x10020 -> 0x3f400020, 7 KB
Info : Flash mapping 1: 0x20020 -> 0x400d0020, 96 KB
Info : Using flash bank 'esp32.cpu0.drom' size 8 KB
Info : New GDB Connection: 1, Target esp32.cpu0, state: halted
Info : JTAG tap: esp32.cpu0 tap/device found: 0x120034e5 (mfg: 0x272 (Tensilica), part: 0x2003, ver: 0x1)
Info : JTAG tap: esp32.cpu1 tap/device found: 0x120034e5 (mfg: 0x272 (Tensilica), part: 0x2003, ver: 0x1)
Info : esp32.cpu0: Debug controller was reset.
Info : esp32.cpu0: Core was reset.
Info : esp32.cpu0: Target halted, PC=0x500000CF, debug_reason=00000000
Info : esp32.cpu0: Core was reset.
Info : esp32.cpu0: Target halted, PC=0x40000400, debug_reason=00000000
Info : esp32.cpu1: Debug controller was reset.
Info : esp32.cpu1: Core was reset.
Info : esp32.cpu1: Target halted, PC=0x40000400, debug_reason=00000000
Info : JTAG tap: esp32.cpu0 tap/device found: 0x120034e5 (mfg: 0x272 (Tensilica), part: 0x2003, ver: 0x1)
Info : JTAG tap: esp32.cpu1 tap/device found: 0x120034e5 (mfg: 0x272 (Tensilica), part: 0x2003, ver: 0x1)
Info : esp32.cpu0: Debug controller was reset.
Info : esp32.cpu0: Core was reset.
Info : esp32.cpu0: Target halted, PC=0x500000CF, debug_reason=00000000
Info : esp32.cpu0: Core was reset.
Info : esp32.cpu0: Target halted, PC=0x40000400, debug_reason=00000000
Info : esp32.cpu1: Debug controller was reset.
Info : esp32.cpu1: Core was reset.
Info : esp32.cpu1: Target halted, PC=0x40000400, debug_reason=00000000
Info : esp32.cpu0: Target halted, PC=0x400D0A59, debug_reason=00000001
Info : Set GDB target to 'esp32.cpu0'

In the second terminal run xtensa gdb:

$ xtensa-esp32-elf-gdb nuttx
GNU gdb (crosstool-NG esp-2020r2) 8.1.0.20180627-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "--host=x86_64-build_pc-linux-gnu --target=xtensa-esp32-elf".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
http://www.gnu.org/software/gdb/bugs/.
Find the GDB manual and other documentation resources online at:
http://www.gnu.org/software/gdb/documentation/.
For help, type "help".
Type "apropos word" to search for commands related to "word"…
Reading symbols from nuttx…done.

(gdb) target extended-remote :3333
Remote debugging using :3333
up_idle () at chip/esp32_idle.c:206
206 }

(gdb) mon reset halt
JTAG tap: esp32.cpu0 tap/device found: 0x120034e5 (mfg: 0x272 (Tensilica), part: 0x2003, ver: 0x1)
JTAG tap: esp32.cpu1 tap/device found: 0x120034e5 (mfg: 0x272 (Tensilica), part: 0x2003, ver: 0x1)
esp32.cpu0: Debug controller was reset.
esp32.cpu0: Core was reset.
esp32.cpu0: Target halted, PC=0x500000CF, debug_reason=00000000
esp32.cpu0: Core was reset.
esp32.cpu0: Target halted, PC=0x40000400, debug_reason=00000000
esp32.cpu1: Debug controller was reset.
esp32.cpu1: Core was reset.
esp32.cpu1: Target halted, PC=0x40000400, debug_reason=00000000

(gdb) flushregs
Register cache flushed.

(gdb) b nx_start
Breakpoint 1 at 0x400d0a59: file init/nx_start.c, line 354.

(gdb) mon reset init
JTAG tap: esp32.cpu0 tap/device found: 0x120034e5 (mfg: 0x272 (Tensilica), part: 0x2003, ver: 0x1)
JTAG tap: esp32.cpu1 tap/device found: 0x120034e5 (mfg: 0x272 (Tensilica), part: 0x2003, ver: 0x1)
esp32.cpu0: Debug controller was reset.
esp32.cpu0: Core was reset.
esp32.cpu0: Target halted, PC=0x500000CF, debug_reason=00000000
esp32.cpu0: Core was reset.
esp32.cpu0: Target halted, PC=0x40000400, debug_reason=00000000
esp32.cpu1: Debug controller was reset.
esp32.cpu1: Core was reset.
esp32.cpu1: Target halted, PC=0x40000400, debug_reason=00000000

(gdb) c
Continuing.
Note: automatically using hardware breakpoints for read-only addresses.
esp32.cpu0: Target halted, PC=0x400D0A59, debug_reason=00000001
Set GDB target to 'esp32.cpu0'

Breakpoint 1, nx_start () at init/nx_start.c:354
354 int cpu = 0;
(gdb)

Debugging ESP32-S2 with OpenOCD and GDB part 2

This is the OpenOCD command session:

$ esp32s2openocd -s tcl -c 'set ESP_RTOS none' -f esp32s2_saola.cfg -c "init; reset halt"
Open On-Chip Debugger v0.10.0-esp32-20210401-13-g5d011fa5 (2021-05-12-12:41)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
none
adapter speed: 3000 kHz

Info : clock speed 3000 kHz
Info : JTAG tap: esp32s2.cpu tap/device found: 0x120034e5 (mfg: 0x272 (Tensilica), part: 0x2003, ver: 0x1)
Info : esp32s2: Target halted, PC=0x4001AA90, debug_reason=00000000
Info : Detected ESP32-S2 chip
Info : Listening on port 3333 for gdb connections
Info : JTAG tap: esp32s2.cpu tap/device found: 0x120034e5 (mfg: 0x272 (Tensilica), part: 0x2003, ver: 0x1)
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Core was reset.
Info : esp32s2: Target halted, PC=0x4000729B, debug_reason=00000000
Info : esp32s2: Core was reset.
Info : esp32s2: Target halted, PC=0x40000400, debug_reason=00000000
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : accepting 'gdb' connection on tcp/3333
Info : esp32s2: Target halted, PC=0x40031CAA, debug_reason=00000001
Error: Failed to get flash maps (-6)!
Warn : Application image is invalid! Check configured binary flash offset 'appimage_offset'.
Warn : Failed to get flash mappings (-4)!
Info : esp32s2: Target halted, PC=0x40031CAA, debug_reason=00000001
Info : esp32s2: Target halted, PC=0x40031CAA, debug_reason=00000001
Info : Auto-detected flash bank 'esp32s2.flash' size 4096 KB
Info : Using flash bank 'esp32s2.flash' size 4096 KB
Info : esp32s2: Target halted, PC=0x40031CAA, debug_reason=00000001
Error: Failed to get flash maps (-6)!
Warn : Application image is invalid! Check configured binary flash offset 'appimage_offset'.
Warn : Failed to get flash mappings (-4)!
Info : esp32s2: Target halted, PC=0x40031CAA, debug_reason=00000001
Info : Using flash bank 'esp32s2.irom' size 0 KB
Info : esp32s2: Target halted, PC=0x40031CAA, debug_reason=00000001
Error: Failed to get flash maps (-6)!
Warn : Application image is invalid! Check configured binary flash offset 'appimage_offset'.
Warn : Failed to get flash mappings (-4)!
Info : esp32s2: Target halted, PC=0x40031CAA, debug_reason=00000001
Info : Using flash bank 'esp32s2.drom' size 0 KB
Warn : negative reply, retrying
invalid command name "nuttx.pid_offset"
invalid command name "nuttx.xcpreg_offset"
invalid command name "nuttx.state_offset"
invalid command name "nuttx.name_offset"
invalid command name "nuttx.name_size"
Info : JTAG tap: esp32s2.cpu tap/device found: 0x120034e5 (mfg: 0x272 (Tensilica), part: 0x2003, ver: 0x1)
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Core was reset.
Info : esp32s2: Target halted, PC=0x40007286, debug_reason=00000000
Info : esp32s2: Core was reset.
Info : esp32s2: Target halted, PC=0x40000400, debug_reason=00000000
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : The target is not running when halt was requested, stopping GDB.
Warn : target esp32s2 is not halted (gdb fileio)
Warn : xtensa_write_memory: esp32s2: target not halted
Error: esp32s2: Failed to read insn (-304)!
Error: esp32s2: Failed to remove SW breakpoint (-304)!
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Target halted, PC=0x4001126A, debug_reason=00000000
Info : dropped 'gdb' connection

This is the GDB terminal session:

$ xtensa-esp32-elf-gdb nuttx
GNU gdb (crosstool-NG esp-2020r2) 8.1.0.20180627-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "--host=x86_64-build_pc-linux-gnu --target=xtensa-esp32-elf".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
http://www.gnu.org/software/gdb/bugs/.
Find the GDB manual and other documentation resources online at:
http://www.gnu.org/software/gdb/documentation/.
For help, type "help".
Type "apropos word" to search for commands related to "word"…
Reading symbols from nuttx…done.
(gdb) target extended-remote :3333
Remote debugging using :3333
0x40000400 in ?? ()
(gdb) symbol-file nuttx
Load new symbol table from "nuttx"? (y or n) y
Reading symbols from nuttx…done.
invalid command name "nuttx.pid_offset"
invalid command name "nuttx.xcpreg_offset"
invalid command name "nuttx.state_offset"
invalid command name "nuttx.name_offset"
invalid command name "nuttx.name_size"
(gdb) mon reset halt
JTAG tap: esp32s2.cpu tap/device found: 0x120034e5 (mfg: 0x272 (Tensilica), part: 0x2003, ver: 0x1)
esp32s2: Debug controller was reset.
esp32s2: Core was reset.
esp32s2: Target halted, PC=0x40007286, debug_reason=00000000
esp32s2: Core was reset.
esp32s2: Target halted, PC=0x40000400, debug_reason=00000000
(gdb) flushregs
Register cache flushed.
(gdb) b __start
Breakpoint 1 at 0x400226b8: file chip/esp32s2_start.c, line 77.
(gdb) c
Continuing.
esp32s2: Debug controller was reset.
esp32s2: Debug controller was reset.
esp32s2: Debug controller was reset.
esp32s2: Debug controller was reset.
esp32s2: Debug controller was reset.
esp32s2: Debug controller was reset.
esp32s2: Debug controller was reset.
esp32s2: Debug controller was reset.
^C
The target is not running when halt was requested, stopping GDB.
Program received signal SIGINT, Interrupt.
0x40000400 in ?? ()
(gdb) quit
A debugging session is active.
Inferior 1 [Remote target] will be detached.
Quit anyway? (y or n) y
Detaching from program: /comum/workspace/Espressif/NuttX/nuttx/nuttx, Remote target

Debugging ESP32-S2 with OpenOCD and GDB

$ openocd -f esp32s2_saola.cfg
Open On-Chip Debugger v0.10.0-esp32-20210401-13-g5d011fa5 (2021-05-12-12:41)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
adapter speed: 3000 kHz

Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : clock speed 3000 kHz
Info : JTAG tap: esp32s2.cpu tap/device found: 0x120034e5 (mfg: 0x272 (Tensilica), part: 0x2003, ver: 0x1)
Info : esp32s2: Target halted, PC=0x40000400, debug_reason=00000000
Info : Detected ESP32-S2 chip
Info : Listening on port 3333 for gdb connections
Info : accepting 'gdb' connection on tcp/3333
Warn : No symbols for FreeRTOS!
Info : esp32s2: Target halted, PC=0x40031CAA, debug_reason=00000001
Error: Failed to get flash maps (-6)!
Warn : Application image is invalid! Check configured binary flash offset 'appimage_offset'.
Warn : Failed to get flash mappings (-4)!
Info : esp32s2: Target halted, PC=0x40031CAA, debug_reason=00000001
Info : esp32s2: Target halted, PC=0x40031CAA, debug_reason=00000001
Info : Auto-detected flash bank 'esp32s2.flash' size 4096 KB
Info : Using flash bank 'esp32s2.flash' size 4096 KB
Info : esp32s2: Target halted, PC=0x40031CAA, debug_reason=00000001
Error: Failed to get flash maps (-6)!
Warn : Application image is invalid! Check configured binary flash offset 'appimage_offset'.
Warn : Failed to get flash mappings (-4)!
Info : esp32s2: Target halted, PC=0x40031CAA, debug_reason=00000001
Info : Using flash bank 'esp32s2.irom' size 0 KB
Info : esp32s2: Target halted, PC=0x40031CAA, debug_reason=00000001
Error: Failed to get flash maps (-6)!
Warn : Application image is invalid! Check configured binary flash offset 'appimage_offset'.
Warn : Failed to get flash mappings (-4)!
Info : esp32s2: Target halted, PC=0x40031CAA, debug_reason=00000001
Info : Using flash bank 'esp32s2.drom' size 0 KB
Warn : negative reply, retrying
Warn : RTOS FreeRTOS not detected. (GDB could not find symbol 'pxCurrentTCB')
Warn : RTOS FreeRTOS not detected. (GDB could not find symbol 'pxCurrentTCB')
Warn : RTOS FreeRTOS not detected. (GDB could not find symbol 'pxCurrentTCB')
invalid command name "nuttx.pid_offset"
invalid command name "nuttx.xcpreg_offset"
invalid command name "nuttx.state_offset"
invalid command name "nuttx.name_offset"
invalid command name "nuttx.name_size"
Info : JTAG tap: esp32s2.cpu tap/device found: 0x120034e5 (mfg: 0x272 (Tensilica), part: 0x2003, ver: 0x1)
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Core was reset.
Info : esp32s2: Target halted, PC=0x40007299, debug_reason=00000000
Info : esp32s2: Core was reset.
Info : esp32s2: Target halted, PC=0x40000400, debug_reason=00000000
Info : JTAG tap: esp32s2.cpu tap/device found: 0x120034e5 (mfg: 0x272 (Tensilica), part: 0x2003, ver: 0x1)
Info : esp32s2: Debug controller was reset.
Info : esp32s2: Core was reset.
Info : esp32s2: Target halted, PC=0x4000F99B, debug_reason=00000000
Info : esp32s2: Core was reset.
Info : esp32s2: Target halted, PC=0x40000400, debug_reason=00000000
Warn : address 0x400804b0 not writable
Error: esp32s2: Failed to write breakpoint instruction (-4)!
Error: esp32s2: Failed to add SW breakpoint!
Error: can't add breakpoint: unknown reason

In another terminal execute:

$ xtensa-esp32-elf-gdb nuttx
GNU gdb (crosstool-NG esp-2020r2) 8.1.0.20180627-git
Copyright (C) 2018 Free Software Foundation, Inc.
…
Reading symbols from nuttx…done.

(gdb) target extended-remote :3333
Remote debugging using :3333
0x4001276f in ?? ()

(gdb) symbol-file nuttx
Load new symbol table from "nuttx"? (y or n) y
Reading symbols from nuttx…done.
invalid command name "nuttx.pid_offset"
invalid command name "nuttx.xcpreg_offset"
invalid command name "nuttx.state_offset"
invalid command name "nuttx.name_offset"
invalid command name "nuttx.name_size"

(gdb) set remote hardware-watchpoint-limit 2

(gdb) mon reset halt
JTAG tap: esp32s2.cpu tap/device found: 0x120034e5 (mfg: 0x272 (Tensilica), part: 0x2003, ver: 0x1)
esp32s2: Debug controller was reset.
esp32s2: Core was reset.
esp32s2: Target halted, PC=0x40007299, debug_reason=00000000
esp32s2: Core was reset.
esp32s2: Target halted, PC=0x40000400, debug_reason=00000000

(gdb) flushregs
Register cache flushed.

(gdb) b __start
Breakpoint 1 at 0x400226b8: file chip/esp32s2_start.c, line 77.

(gdb) b nx_start
Breakpoint 2 at 0x400804b0: file init/nx_start.c, line 353.

(gdb) run
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /comum/workspace/Espressif/NuttX/nuttx/nuttx
Warning:
Cannot insert breakpoint 2.
Cannot access memory at address 0x400804b0

Command aborted.

(gdb) c
Continuing.
Warning:
Cannot insert breakpoint 2.
Cannot access memory at address 0x400804b0

Command aborted.
(gdb)