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.

BEGIN {
    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.

Comments 2

  1. Aristotle Pagaltzis wrote:

    Damn clever hack!

    Post it on Perlmonks. :-)

    Posted 16 Nov 2005 at 02:32
  2. Dominic Mitchell wrote:

    It would be even better if it actually showed me the root cause of the problem. Unfortunately, it turned out that I had suexec configured in to my apache without realising. That was stopping me dead in my tracks. :-(

    Posted 16 Nov 2005 at 08:46