[KinoSearch] DEBUG() and ASSERT()

Marvin Humphrey marvin at rectangular.com
Sun Sep 30 20:11:20 PDT 2007



On Sun, Sep 30, 2007 at 02:39:17PM -0600, Nathan Kurz wrote:
> On 9/28/07, Marvin Humphrey <marvin at rectangular.com> wrote:
> > No worries, mate.  Can you show me how you normally like your
> > DEBUG and ASSERT macros set up?
> 
> I'm sorry, but I'm not going to have time to clean up my current
> Debug.h file to your standards before leaving on a trip tomorrow.  But
> I've attached it in its current state.  I may have broken it by
> starting to make changes to it, but at least it shows what I'm aiming
> for.

I'll work on it.  For now, I think it belongs under KinoSearch/Util/Debug.h.
It should probably get included by a new "kino.h" file, which would also
include charmony.h.  

The variadic macros are the tricky part -- but at least Charmonizer already
probes for those, and will define HAS_VARIADIC_MACROS if they're available.
If the compiler doesn't support them, ASSERT, DEBUG, and DEBUG_PRINT will have
to be no-op functions.

> The things I like are ASSERT() statements that are run if compiled
> #ifdef DEBUG, as opposed to the system assert() which is  #ifndef
> NDEBUG.  

Sounds good.  Right now I'm using KINO_DEBUG to turn on a bunch of extra
compiler flags (but only under gcc).

> I subscribe to the "Code Complete" theory that one's DEBUG
> version should be so loaded down as to barely run, but the real
> version should be unencumbered by unnecessary checks.  

"Barely run" sounds a mite excessive.  :)  It makes debugging "heavy" tests
all but impossible.  t/001-build_invindexes.t takes several minutes under
valgrind on a dual Xeon.  

> I find the simple DEBUG defined/undefined dichotomy better for this than the
> 2x2 DEBUG/NDEBUG.

Yar, sounds good.  

Here's the section which adds the extra compiler flags... 

    my $EXTRA_CCFLAGS = '';
    if ( $ENV{LUCY_DEBUG} || $ENV{KINO_DEBUG} ) {
        require version;
        if ( defined $Config{gccversion} ) {
            $Config{gccversion} =~ /^([\d\.]+)/ or die "no match";
            my $gcc_version = version->new("$1")->numify;
            $EXTRA_CCFLAGS = "-DPERL_GCC_PEDANTIC -ansi -pedantic -Wall "
                . "-std=c89 -Wno-long-long ";
            $EXTRA_CCFLAGS .= "-Wextra " if $gcc_version >= 3.4;    # correct
            $EXTRA_CCFLAGS .= "-Wno-variadic-macros "
                if $gcc_version > 3.2;    # at least not on gcc 3.2
        }
    }

I suppose I'll change that up to use plain old "DEBUG" instead, plus figure
out a way to avoid requiring version.pm.

> I also like DEBUG() statements that can be controlled at runtime to
> print debugging information.  I do this based on the DEBUG environment
> variable, and allows control at both the file and function level. It's
> possible that there would be a better fit for KinoSearch.  

I think that will be OK.

> Done right, the DEBUG statements in the code serve dual purpose as comments
> and debugging aids.

I'm down with that.  You may have noticed that the KS test files have very few
comments, but reasonably verbose test messages.  Same principle.

> It's a little tricky to integrate this system with the XS loader and
> builder.  I've done it before by creating a Makefile that creates both
> a normal and debug version and changes the XS bootstrap function to
> use libfoo.so or libfoo_debug.so according to the DEBUG environment
> variable.  For now it may be simpler to just build one at a time,
> switching by hand.

I think it would be worthwhile to work up a scheme where both get installed
and users can switch between them.  There have been a number of times where
people have run up against C code that they can't debug themselves and where
I've struggled to diagnose the problem from a distance.  After just a couple
such sessions, the initial investment will be paid back.

Doubling the compilation time is kind of an annoyance during development, so
we'll probably want to define an environment var that disables that
functionality and any associated tests.

Have a good trip,

Marvin Humphrey
Rectangular Research
http://www.rectangular.com/


_______________________________________________
KinoSearch mailing list
KinoSearch at rectangular.com
http://www.rectangular.com/mailman/listinfo/kinosearch




More information about the kinosearch mailing list