<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>

CC BY-SA 4.0
2018-11-21T16:24:24.323Z
Nice workaround, thanks. I'm still getting `<optimized out>`, though, I must be missing something.
JBaczuk
CC BY-SA 4.0
2018-11-21T16:39:46.033Z
Now it is showing all variables. I must've not recompiled the file correctly.
JBaczuk
CC BY-SA 4.0
2018-11-21T16:40:10.530Z
Glad that I could help! :)
MCCCS
CC BY-SA 4.0
2018-11-21T17:49:00.193Z
If you want to avoid all "<compiled out>"s, you need to compile with "-O0" to disable all optimizations.
Pieter Wuille
CC BY-SA 4.0
2018-11-21T17:51:11.583Z
Also there is no need to modify configure.ac, you can override things on the command line by using `./configure CXXFLAGS="-O0 -ggdb3" ...`.
Pieter Wuille
CC BY-SA 4.0
2018-11-21T17:51:14.697Z
@PieterWuille the manual (https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html) says O0 is default in GCC, but agree about the second, will edit soon.
MCCCS
CC BY-SA 4.0
2018-11-21T18:00:13.750Z
@MCCCS But `--enable-debug` will pass `-Og` which overrides the `-O0` default.
Pieter Wuille
CC BY-SA 4.0
2018-11-21T19:33:51.780Z
Why doesn't `--enable-debug` use `-O0`?
JBaczuk
CC BY-SA 4.0
2018-11-21T16:07:14.877Z
2018-11-21T19:03:36.870Z
2018-11-21T19:03:36.870Z
MCCCS
<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>

CC BY-SA 4.0
2019-09-24T13:18:01.867Z
2019-09-24T13:18:01.867Z
fjahr
<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>

<bitcoin-core-development><testing><debugging>
Debugging Bitcoin Core with gdb - how to turn off compiler optimizations
JBaczuk
# Debugging Bitcoin Core with gdb - how to turn off compiler optimizations

(Asked by: username_0 on 2018-11-21)

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

**Tags:** bitcoin-core-development, testing, debugging


---

## 2 answers

### Answer by username_1 on 2018-11-21. Score: 2 | Accepted answer

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.

> **Comments:**
> **username_0**: Nice workaround, thanks. I'm still getting `<optimized out>`, though, I must be missing something.  
> **username_0**: Now it is showing all variables. I must've not recompiled the file correctly.  
> **username_1**: Glad that I could help! :)  
> **username_2**: If you want to avoid all "<compiled out>"s, you need to compile with "-O0" to disable all optimizations.  
> **username_2**: Also there is no need to modify configure.ac, you can override things on the command line by using `./configure CXXFLAGS="-O0 -ggdb3" ...`.  
> **username_1**: @PieterWuille the manual (https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html) says O0 is default in GCC, but agree about the second, will edit soon.  
> **username_2**: @username_1 But `--enable-debug` will pass `-Og` which overrides the `-O0` default.  
> **username_0**: Why doesn't `--enable-debug` use `-O0`?  

---

### Answer by username_3 on 2019-09-24. Score: 1

As of [PR #16435](https://github.com/bitcoin/bitcoin/commit/d6ac25bdd96589a71006e3ab3f303b091ffaa9e4) `--enable-debug` is actually setting "-O0", so there should be no optimization at all anymore.


---

