Walker News

How To Debug DB2 UDF Written In C Language Using Log File?

After learning how to build DB2 external UDF in the previous post, now let’s see how to make the user-defined function writing debugging info to your own application log file (not the DB2 system log file).

It’s pretty simple – just call the system() function to execute operating system command (e.g. Linux echo command) or program (e.g. console-based SMTP email client, if you want to receive email alert when it hits an exception).

Adds debugging log file feature to UDF
Now, let’s modify the sample code used in previous post (the udftest.c), to include 3 additional lines that achieve this purpose – writing the input value passed by caller (the SQL statement) to a disk file in /tmp directory.
Apparently, this tutorial is based on Linux platform. So, change that log file path to Windows folder convention, if you’re working on the Microsoft operating system.

The excerpt of udftest.c with debugging log file feature:
    time_t ep = *eT;
    struct tm *tms;

    // Uses Linux echo command to log *eT value to file
    char cmd[50];
    sprintf(cmd, "echo 'eT =%ld' >>/tmp/udflog", *eT);

    tms = *uG == 0 ? localtime(&ep) : gmtime(&ep);
    strftime(tS, 20, "%F %T", tms);

    // Indicates a value is returned to caller
    *tSI = 0;

Drop function, disconnect, reconnect, create function!
After modifying a user-defined function, you must go through these 4 steps in that particular order, unless you register the modified UDF to DB using different function name:
drop function > disconnect DB > reconnect to DB > create function

Custom Search

  1. Walker 16-02-14@22:53

    The system() is not perfect for dumping debugging info.

    A better solution is by using the fprintf() for writing/appending data to disk file.

2016  •  Privacy Policy