user60443: 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 <optimized out> 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.

user83363: As of PR #16435 --enable-debug is actually setting "-O0", so there should be no optimization at all anymore.

user38618: 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.

60443: <p>I ran the configure script with the <code>--enable-debug</code> flag, then ran <code>make</code>. I am running unit tests:</p>

<p><code>gdb --args src/test/test_bitcoin --log_level=all --run_test=script_standard_tests</code></p>

<p>I'm trying to step through the execution of the source files using gdb, but I am still getting <code>&lt;optimized out&gt;</code> for many variables, and it skips lines here and there. I thought that the <code>--enable-debug</code> 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.</p>
 83363: <p>As of <a href="https://github.com/bitcoin/bitcoin/commit/d6ac25bdd96589a71006e3ab3f303b091ffaa9e4" rel="nofollow noreferrer">PR #16435</a> <code>--enable-debug</code> is actually setting "-O0", so there should be no optimization at all anymore.</p>
 38618: <p>Bitcoin Core disables <code>-O2</code> and enables <code>-Og</code> when <code>--enable-debug</code> is set. <code>-Og</code> 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 <a href="https://github.com/bitcoin/bitcoin/blob/master/configure.ac#L256-L278" rel="nofollow noreferrer">between L256 and L278</a> to:</p>

<pre><code>CXXFLAGS="-ggdb3 -ftrapv"
</code></pre>

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

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

<pre><code>./configure CXXFLAGS="-O0 -ggdb3"
</code></pre>

<p>O0 is needed in this case.</p>
