Author vstinner
Recipients vstinner
Date 2019-10-02.16:35:07
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1570034107.68.0.232468214894.issue38350@roundup.psfhosted.org>
In-reply-to
Content
In Python 2.7, when using ./configure --with-pydebug, Python is built using -O0 compiler optimization level: disable all optimizations. The comment is quite explicit: "Optimization messes up debuggers, so turn it off for debug builds".

if test "$Py_DEBUG" = 'true' ; then
    # Optimization messes up debuggers, so turn it off for
    # debug builds.
    OPT="-g -O0 -Wall $STRICT_PROTO"
else
    OPT="-g $WRAP -O3 -Wall $STRICT_PROTO"
fi

In Python 3, -Og is preferred over -O0 for pydebug, if -Og is available:

if test "$Py_DEBUG" = 'true' ; then
    # Optimization messes up debuggers, so turn it off for
    # debug builds.
    if "$CC" -v --help 2>/dev/null |grep -- -Og > /dev/null; then
        OPT="-g -Og -Wall"
    else
        OPT="-g -O0 -Wall"
    fi
else
    OPT="-g $WRAP -O3 -Wall"
fi

Problem: in my experience, gdb traceback doesn't make sense sometimes, and gdb fails to inspect some function arguments and some variables which are displayed as <optimized out>.

See a very concrete example with a test_gdb failure on x86-64 when Python is compiled using gcc -Og:
https://bugzilla.redhat.com/show_bug.cgi?id=1734327#c22

My colleague who is working on gdb suggests to use -O0:
https://bugzilla.redhat.com/show_bug.cgi?id=1734327#c27

Since I started contributing to Python, I always forced gcc -O0 because any other optimization level caused me many issues in gdb. I'm using -O0 for 10 years with sucess.

The GCC documentation says "It is a better choice than -O0 for producing debuggable code because some compiler passes that collect debug information are disabled at -O0." But my experience says the opposite.

Note: instead of -g, we could use -g3 to include debug information for macros and defines.

--

I'm proposing to change the *default* compiler flags from -Og to -O0. Obviously, Linux distributions and developers are free to override the compiler optimization level. For example: ./configure --with-pydebug CFLAGS="-Og" ensures that Python is always built using -Og.

I propose to modify 3.7, 3.8 and master branches.
History
Date User Action Args
2019-10-02 16:35:07vstinnersetrecipients: + vstinner
2019-10-02 16:35:07vstinnersetmessageid: <1570034107.68.0.232468214894.issue38350@roundup.psfhosted.org>
2019-10-02 16:35:07vstinnerlinkissue38350 messages
2019-10-02 16:35:07vstinnercreate