Should --enable-debug set CFLAGS to -O0 instead of -O2?

Started by Chao Li9 months ago5 messageshackers
Jump to latest
#1Chao Li
li.evan.chao@gmail.com

Hi,

I noticed that "./configure --enable-debug" will set "-O2" to CFLAGS. To
better debug/trace the code, I have to manually change "-O2" to "-O0" in
src/Makefile.global.

In the script "configure" line 5254 through 5259, I see this comment:

# C[XX]FLAGS are selected so:
# If the user specifies something in the environment, that is used.
# else: If the template file set something, that is used.
# else: If coverage was enabled, don't set anything.
# else: If the compiler is GCC, then we use -O2.
# else: If the compiler is something else, then we use -O, unless debugging.

The last line, "unless debugging", my first impression is that should apply
to GCC. However, the following code shows the GCC always use "-O2".

elif test "$GCC" = yes; then
CFLAGS="-O2"
else
# if the user selected debug mode, don't use -O
if test "$enable_debug" != yes; then
CFLAGS="-O"
fi
fi

For GCC, we need to use "-O0" for best debugging experience. Why here
always uses "-O2" for GCC regardless debugging?

If confirmed a bug, I'd like a submit a patch to fix that.

Thanks,
Chao Li (Evan)
============
HighGo Inc.

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Chao Li (#1)
Re: Should --enable-debug set CFLAGS to -O0 instead of -O2?

Chao Li <li.evan.chao@gmail.com> writes:

I noticed that "./configure --enable-debug" will set "-O2" to CFLAGS. To
better debug/trace the code, I have to manually change "-O2" to "-O0" in
src/Makefile.global.

This has been intentional for decades. It is not a bug. You're
certainly welcome to use different CFLAGS locally if that suits
your habits better.

In my mind, at least, the rationale is that we don't want to
give up more performance than we have to in debug builds, nor
let the behavior deviate from what it would be in production.

It is true than in some cases it's too hard to trace the behavior of
optimized code. What I usually do if I get frustrated while debugging
is to recompile specific file(s) at -O0. The PROFILE variable is
handy for injecting that switch.

regards, tom lane

#3Chao Li
li.evan.chao@gmail.com
In reply to: Tom Lane (#2)
Re: Should --enable-debug set CFLAGS to -O0 instead of -O2?

Hi Tom,

Thanks for the explanation. W.R.T the PROFILE variable, I tried:

# ./configure --enable-debug
# PROFILE="-O0" make

As a result, both "-O2" and "-O0" presented to gcc. In that case, I believe
"-O0" will be ignored by gcc.

Did you mean to something else?

Chao Li (Evan)
------------------------------
HighGo Software Inc.
https://www.highgo.com/

Tom Lane <tgl@sss.pgh.pa.us> 于2025年8月1日周五 11:30写道:

Show quoted text

Chao Li <li.evan.chao@gmail.com> writes:

I noticed that "./configure --enable-debug" will set "-O2" to CFLAGS. To
better debug/trace the code, I have to manually change "-O2" to "-O0" in
src/Makefile.global.

This has been intentional for decades. It is not a bug. You're
certainly welcome to use different CFLAGS locally if that suits
your habits better.

In my mind, at least, the rationale is that we don't want to
give up more performance than we have to in debug builds, nor
let the behavior deviate from what it would be in production.

It is true than in some cases it's too hard to trace the behavior of
optimized code. What I usually do if I get frustrated while debugging
is to recompile specific file(s) at -O0. The PROFILE variable is
handy for injecting that switch.

regards, tom lane

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Chao Li (#3)
Re: Should --enable-debug set CFLAGS to -O0 instead of -O2?

Chao Li <li.evan.chao@gmail.com> writes:

Thanks for the explanation. W.R.T the PROFILE variable, I tried:

# ./configure --enable-debug
# PROFILE="-O0" make

As a result, both "-O2" and "-O0" presented to gcc. In that case, I believe
"-O0" will be ignored by gcc.

Nope ... read the gcc docs, or experiment. The last -O switch wins.
(This is true for most switches in most Unix tools, and it's common
for scripts such as Makefiles to rely on it.)

regards, tom lane

#5Chao Li
li.evan.chao@gmail.com
In reply to: Tom Lane (#4)
Re: Should --enable-debug set CFLAGS to -O0 instead of -O2?

My bad. You are right, the last -O will work. Then I think I get a solution
to my original problem. Thanks again for your help.

Chao Li (Evan)
------------------------------
HighGo Software Inc.
https://www.highgo.com/

Tom Lane <tgl@sss.pgh.pa.us> 于2025年8月1日周五 12:07写道:

Show quoted text

Chao Li <li.evan.chao@gmail.com> writes:

Thanks for the explanation. W.R.T the PROFILE variable, I tried:

# ./configure --enable-debug
# PROFILE="-O0" make

As a result, both "-O2" and "-O0" presented to gcc. In that case, I

believe

"-O0" will be ignored by gcc.

Nope ... read the gcc docs, or experiment. The last -O switch wins.
(This is true for most switches in most Unix tools, and it's common
for scripts such as Makefiles to rely on it.)

regards, tom lane