ESP GDB stub not working with platformIO project

Hey I am having issues with the gbdstub but only on certain projects…

I have set up an example project (blinky) to try and get the esp gdbstub working. To do this I set the ESP_SYSTEM_GDBSTUB_RUNTIME in the menuconfig for the project and also set up CONFIG_ESP_SYSTEM_PANIC, then flashed and ran idf.py with monitor and did the Ctrl C trick. It opened up gdb and I was able to debug just fine which was very exciting!

I then went back to an old project using platformIO over the top of esp, copied over its builds to the expected places (since it needs - unfortunately - to be built with platformIO’s wrapping build system).

I ran:

cp .\.pio\build\dev\firmware.elf .\build\<PROJECT_NAME>.elf

so that it is as if it were build idf.py build.

When I do this and use the same trick with Ctrl - C and the serial monitor, it halts the processor and allows me to set breakpoints, but these breakpoints are never ever hit. I know for a fact that these lines of code are being run frequently- they are toggling LED over an MCP once per second which is visible. It also seems kinda glitchy using the gdb prompt. For example, when continuing, the prompt hangs at “continuing” in gdb. You cannot enter any further commands until the prompt returns when you hit Ctrl C again.

I thought that perhaps there might be issues with code inlining and optimisation but in my ini file:

`[build_type_flags] 
debug_flags = -D${compile_option_flags.debug} -O0 ; Turn off 
optimisations -g3 ; All debug information 
... 
[env:dev] 
build_type = debug 
build_flags = ${common.build_flags} ${build_type_flags.debug_flags}

so I can’t imagine that there is inlining etc going on since we are working in an extension of debug without any optimisations.

Edit: I have now run things like this:

  (penv) PS C:<MY_PATH_TO_MY_PROJECT>> pio debug --interface=gdb
C:\Users\<MY_USER>\.platformio\packages\toolchain-xtensa-esp32\bin\xtensa-esp32-elf-gdb.exe: warning: Couldn't determine a path for the index cache directory.
Reading symbols from C:<MY_PATH_TO_MY_PROJECT>\.pio\build\dev\firmware.elf...
(gdb) target remote COM3
Remote debugging using COM3
0x4019480e in esp_pm_impl_waiti ()
    at C:\Users\<MY_USER>\.platformio\packages\framework-espidf\components\hal\esp32\include/hal/cpu_ll.h:183
183         asm volatile ("waiti 0\n");
(gdb) continue
Continuing.

When I do this and set a breakpoint I get the same behaviour, the breakpoint is never hit at all.
Any advice to get this working?

Perhaps there is some documentation here…?

Your ELF must match the ESP-IDF linker script exactly

https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/linker-script-generation.html PlatformIO may change configs and memory layout https://docs.platformio.org/en/latest/platforms/espressif32.html

GDB sets breakpoints using ELF symbol addresses https://sourceware.org/gdb/current/onlinedocs/gdb.html/Breakpoints.html

If addresses differ, breakpoints go to wrong memory

https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/memory-types.html

CPU never executes those locations, so breakpoints never trigger.