Debugging Bitcoin Core with gdb - how to turn off compiler optimizations I ran the configure script with the --enable-debug flag, then ran make. I am running unit tests:

gdb --args src/test/test_bitcoin --log_level=all --run_test=script_standard_tests

I'm trying to step through the execution of the source files using gdb, but I am still getting &lt;optimized out&gt; for many variables, and it skips lines here and there. I thought that the --enable-debug flag would turn off optimization, but am I mistaken? I'm wondering if the optimization is always on for the tests or if there is a way to turn them off.
Bitcoin Core disables -O2 and enables -Og when --enable-debug is set. -Og is intended for debugging; however, it removes some of the debugging information and is buggy. To disable it and to improve debugging experience, I suggest changing between L256 and L278 to:

CXXFLAGS="-ggdb3 -ftrapv"


-ggdb3 is more powerful than -g3, it uses a GDB-only, LLDB-incompatible dialect for debugging. This will also get rid of -O2, except in libsecp256k1.

Edit: Sipa suggests a more elegant way without altering autoconf files, which is adding args for configuring.

./configure CXXFLAGS="-O0 -ggdb3"


O0 is needed in this case.