Recalcitrant CGI problems

One of the most useful tools in my box of diagnstics is strace (aka ktrace on FreeBSD or truss on Solaris). Right now, I’ve got a Perl CGI script that’s failing and I don’t know why. It would be useful to see what it’s doing in terms of system calls that’s making it fail.

Unfortunately, that could potentially be problematic with strace. It needs to attach to a new process by starting the command line, or by attaching with the -p flag. But I don’t want to attach to Apache, it will have far too many child processes and I don’t know which one will handle my request. And there’s no convenient point for me to tell apache to run my CGI script with strace in front.

Thankfully, a small amount of Perl can make it all happen. Stick this as the first thing in the file.

    local $ENV{PATH} = '/bin:/usr/bin:/usr/local/bin';
    system "strace -o strace.out.$$ -p $$ &";
    sleep 1;

Now, any time that I run the script, I’ll get a log of exactly what it did.