Debugging Bitcoin Core with gdb - how to turn off compiler optimizations

1

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.

JBaczuk

Posted 2018-11-21T15:38:10.900

Reputation: 6 172

Answers

1

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.

MCCCS

Posted 2018-11-21T15:38:10.900

Reputation: 5 827

Nice workaround, thanks. I'm still getting &lt;optimized out&gt;, though, I must be missing something.JBaczuk 2018-11-21T16:24:24.323

Now it is showing all variables. I must've not recompiled the file correctly.JBaczuk 2018-11-21T16:39:46.033

Glad that I could help! :)MCCCS 2018-11-21T16:40:10.530

If you want to avoid all "<compiled out>"s, you need to compile with "-O0" to disable all optimizations.Pieter Wuille 2018-11-21T17:49:00.193

2Also there is no need to modify configure.ac, you can override things on the command line by using ./configure CXXFLAGS="-O0 -ggdb3" ....Pieter Wuille 2018-11-21T17:51:11.583

@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 2018-11-21T17:51:14.697

1@MCCCS But --enable-debug will pass -Og which overrides the -O0 default.Pieter Wuille 2018-11-21T18:00:13.750

Why doesn't --enable-debug use -O0?JBaczuk 2018-11-21T19:33:51.780

1

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

fjahr

Posted 2018-11-21T15:38:10.900

Reputation: 111