babongo edited this page Oct 5, 2012 · 1 revision

Debugging Tips

First of all set KLONE_CONF_ARGS += --enable_debug in the top level Makefile. Then you have at least two options, depending on which debug style you are used to.

Using a debugger, e.g. GDB

Place a “hook” function inside the declaration block of the page that you need to debug and let the code block recall it as soon as possible, e.g.:

    #include <u/libu.h>
    /* debug bookmark */
    void this_pg_debug(void) { dbg("this_pg.kl1"); }
    const char *this_pg_param0 = NULL;
    /* debug hook */
    /* pg IMPL */
    vars_t *args = request_get_args(request);

Force the kloned not to detach from the controlling terminal, and to use the iterative serve model (one request at a time):

(gdb) set args -Fd

Then tell the GDB to stop each time this_pg.kl1 is requested:

(gdb) break this_pg_debug

at this point you can proceed the usual way.

Using printf-like strategy

It’s convenient to use libu debug features (which are builtin in the KLone) coupled with syslog. Enable syslog logging in etc/kloned.conf:

log.type syslog

Choose a suitable syslog facility (one that is not polluted by other daemons):

log.syslog.facility LOG_LOCAL5

Tell syslogd how to handle local5 messages and let the running instance re-read its configuration:

# echo 'local5.* /var/log/mykloneapp.debug' >> /etc/syslog.conf
# killall -HUP syslogd

from now on you can use dbg(), info(), warn(), err() printf-like macros wherever you want inside your .kl1 pages:

    char b[2];
    dbg("before hazardous op");
    strcpy(b, "0123456789abcdef"); 
    dbg("after hazardous op");

Use a different terminal to display log messages as soon as they are generated:

$ tail -f /var/log/mykloneapp.debug
Nov 17 06:58:53 pb : [dbg] before hazardous op

I’d also suggest you to use libu’s carpal macros throughout your code to achieve effective flow control with less key-strokes (see libu::carpal for details).