How to debug a unit test (native)

Well if I take the project you’ve posted at PlatformIO stm32: Intellisense for google mock is broken and follow the docs by adding

debug_test = *

the platformio.ini in that case (test file is in the root of test/), and I go to the debugger sidebar and press the debug button, it throws me in a debug session starting in main().

And I can debug as usual, e.g. placing breakpoints in the unit test function and forwarding execution there.

Works no problems.

If I wanted to do it from the CLI, then again per official documentation I start a debugging session in the native environment

C:\Users\Max\temp\platformiogmockintellisense>pio debug -e native --interface=gdb -x .pioinit
C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\gdb.exe: warning: Couldn't determine a path for the index cache directory.
Reading symbols from C:\Users\Max\temp\platformiogmockintellisense\.pio\build\native\program.exe...
PlatformIO Unified Debugger -> http://bit.ly/pio-debug
PlatformIO: debug_tool = None
PlatformIO: Initializing remote target...
Temporary breakpoint 1 at 0x140001833: file test\test.cpp, line 9.
PlatformIO: Initialization completed
(gdb) PlatformIO: Resume the execution to `debug_init_break = tbreak main`
PlatformIO: More configuration options -> http://bit.ly/pio-debug
Starting program: C:\Users\Max\temp\platformiogmockintellisense\.pio\build\native\program.exe
[New Thread 11908.0x2368]
[New Thread 11908.0x2f2c]
[New Thread 11908.0x3488]

Thread 1 hit Temporary breakpoint 1, main (argc=1, argv=0x165f3b71990)
    at test\test.cpp:9
9       int main(int argc, char **argv) {
(gdb)

And it fires up the debugger and execution is again in the main() function, ready for further debugging.

(gdb) list
4
5       void bla(void) {
6           TEST_ASSERT_EQUAL(32, 32);
7       }
8
9       int main(int argc, char **argv) {
10          UNITY_BEGIN();
11          RUN_TEST(bla);
12          UNITY_END();
13
(gdb) break 6
Breakpoint 2 at 0x7ff7ad0c17c4: file test\test.cpp, line 6.
(gdb) continue
Continuing.

Thread 1 hit Breakpoint 2, bla () at test\test.cpp:6
6           TEST_ASSERT_EQUAL(32, 32);
(gdb) backtrace
#0  bla () at test\test.cpp:6
#1  0x00007ff7ad0c38e2 in UnityDefaultTestRun (Func=0x7ff7ad0c17c0 <bla()>,
    FuncName=<optimized out>, FuncLineNum=<optimized out>)
    at C:\Users\Max\.platformio\packages\tool-unity\unity.c:1817
#2  0x00007ff7ad0c1861 in main (argc=<optimized out>, argv=<optimized out>)
    at test\test.cpp:11
(gdb) 
1 Like