Debugging hampered by optimisation?

I’m trying to do some native (MingGW) unit tests, and one isn’t behaving as I’d expect so I’m trying to debug it.

As I debug through the unit tests, the instruction pointer/yellow current line in VSC isn’t always where I think it should be.

As I start debugging, it first breaks at the start of main (as per my understanding of how
the default for debug_init_break works. OK
Then it breaks inside setUp(), not an issue, but unexpected - ive no breakpoint set
Then it breaks inside my code where I’ve no breakpoint set
From then on it sort of follows the execution I’d expect, but the call stack keeps jumping about, it “returns” earlier than the source files define - so I’m assuming some sort of optimisation has occurred here.

Setting -O in debug_build_flags to anything other than -Og seems to prevent the debugger from attaching :confused: Is that to be expected? Incidentally, sometimes the debugger doesnt attach using the default build flags - i can’t determine why.

platform = native
build_type = debug
debug_test = mytest
; debug_build_flags = -O0 -ggdb3 -g3

Any ideas? Many thanks, loving PIO otherwise!

Yeah, exactly.

But should I expect that -O0 means I cant even attach the debugger?

Do you use a development version of PlatformIO Core? A debugging for the unit test has not been officially released yet. See platformio-core/HISTORY.rst at develop · platformio/platformio-core · GitHub

If you want to try PlatformIO Core 5.2, please open PlatformIO Core CLI in VSCode and type pio upgrade --dev. Restart VSCode.

Thanks for the response

Yeah I’m on the latest dev build already

Can you upload the exact project you’re having problems with?

No but I’ll try and make a minimum repro and upload that