RE: Build src tree in Develop/Debug mode

From: Mark Millard <marklmi_at_yahoo.com>
Date: Thu, 07 Dec 2023 03:39:22 UTC
Farhan Khan <farhan_at_farhan.codes> wrote on
Date: Wed, 06 Dec 2023 17:09:59 UTC :

> Is there a build-in-debug-mode flag I can run when building the kernel (or world)?
> 
> I was tinkering through the build system and saw that the optimization is hardcoded to -O2 and objcopy strips debugging symbols. This means even with "CFLAGS+=-g -O0 -fno-inline-functions" in my /etc/make.conf I am still losing symbols and sometimes getting unwanted optimizations. The result is that `kgdb` often does not know what line a crash took place in.
> 
> Is there an option to disable all optimizations, optimizations and symbol stripping? If not, IMO, this would be a good candidate for an option to add.
> 
> I briefly wrote about this a few years back: https://blog.farhan.codes/2018/08/16/including-optimized-out-kernel-symbols-in-dtrace-on-freebsd/


References to get you started for where to look about
various controls relative to how debug builds are
handled and where(/if) debug information is placed . . .




man src.conf reports :

     WITHOUT_ASSERT_DEBUG
             Compile programs and libraries without the assert(3) checks.
. . .
     WITHOUT_DEBUG_FILES
             Avoid building or installing standalone debug files for each
             executable binary and shared library.
. . .
     WITHOUT_SPLIT_KERNEL_DEBUG
             Do not build standalone kernel debug files.  Debug data (if
             enabled by the kernel configuration file) will be included in the
             kernel and modules.  When set, it enforces these options:

             WITHOUT_KERNEL_SYMBOLS
. . .

Also :

     WITHOUT_KERNEL_SYMBOLS
             Do not install standalone kernel debug symbol files.  This option
             has no effect at build time.
. . .




man make.conf reports :

   BUILDING THE KERNEL
. . .
     COPTFLAGS     (str) Controls the compiler settings when building the
                   kernel.  Optimization levels above [-O (-O2, ...)] are not
                   guaranteed to work.
. . .

   BUILDING THE WORLD
. . .
     WANT_FORCE_OPTIMIZATION_DOWNGRADE
                   (int) Causes the system compiler to be built such that it
                   forces high optimization levels to a lower one.  cc(1) -O2
                   and above is known to trigger known optimizer bugs at
                   various times.  The value assigned is the highest
                   optimization value used.
. . .


share/mk/bsd.README :

STRIP           The flag passed to the install program to cause the binary
                to be stripped.  This is to be used when building your
                own install script so that the entire system can be made
                stripped/not-stripped using a single nob.



sys/conf/NOTES :

. . .
#
# The `makeoptions' parameter allows variables to be passed to the
# generated Makefile in the build area.
#
# CONF_CFLAGS gives some extra compiler flags that are added to ${CFLAGS}
# after most other flags.  Here we use it to inhibit use of non-optimal
# gcc built-in functions (e.g., memcmp).
#
# DEBUG happens to be magic.
# The following is equivalent to 'config -g KERNELNAME' and creates
# 'kernel.debug' compiled with -g debugging as well as a normal
# 'kernel'.  Use 'make install.debug' to install the debug kernel
# but that isn't normally necessary as the debug symbols are not loaded
# by the kernel and are not useful there anyway.
#
# KERNEL can be overridden so that you can change the default name of your
# kernel.
#
# MODULES_OVERRIDE can be used to limit modules built to a specific list.
#
makeoptions     CONF_CFLAGS=-fno-builtin  #Don't allow use of memcmp, etc.
#makeoptions    DEBUG=-g                #Build kernel with gdb(1) debug symbols
. . .




share/mk/bsd.sys.mk :

# Additional flags passed in CFLAGS and CXXFLAGS when MK_DEBUG_FILES is
# enabled.
DEBUG_FILES_CFLAGS?= -g -gz=zlib





share/mk/bsd.lib.mk :

.if defined(DEBUG_FLAGS)
CFLAGS+= ${DEBUG_FLAGS}

.if ${MK_CTF} != "no" && ${DEBUG_FLAGS:M-g} != ""
CTFFLAGS+= -g
.endif
.else
STRIP?= -s
.endif

. . .

.if ${MK_DEBUG_FILES} != "no" && empty(DEBUG_FLAGS:M-g) && \
    empty(DEBUG_FLAGS:M-gdwarf*)
.if !${COMPILER_FEATURES:Mcompressed-debug}
CFLAGS+= ${DEBUG_FILES_CFLAGS:N-gz*}
CXXFLAGS+= ${DEBUG_FILES_CFLAGS:N-gz*}
.else
CFLAGS+= ${DEBUG_FILES_CFLAGS}
CXXFLAGS+= ${DEBUG_FILES_CFLAGS}
.endif
CTFFLAGS+= -g
.endif



share/mk/bsd.prog.mk :

.if defined(DEBUG_FLAGS)
CFLAGS+=${DEBUG_FLAGS}
CXXFLAGS+=${DEBUG_FLAGS}

.if ${MK_CTF} != "no" && ${DEBUG_FLAGS:M-g} != ""
CTFFLAGS+= -g
.endif
.endif      

. . .

.if ${MK_DEBUG_FILES} != "no" && empty(DEBUG_FLAGS:M-g) && \
    empty(DEBUG_FLAGS:M-gdwarf-*)
.if !${COMPILER_FEATURES:Mcompressed-debug}
CFLAGS+= ${DEBUG_FILES_CFLAGS:N-gz*}
.else
CFLAGS+= ${DEBUG_FILES_CFLAGS}
.endif
CTFFLAGS+= -g
.endif

. . .

.if !defined(DEBUG_FLAGS)
STRIP?= -s
.endif

. . .



share/mk/bsd.own.mk :

# Common variables
.if !defined(DEBUG_FLAGS)
STRIP?=         -s
.endif



It may be that all this is familar. If so: Sorry for the noise.

===
Mark Millard
marklmi at yahoo.com