Debugging Perl CGI Programs

Overview

Debugging CGI programs can be challenging. A CGI program runs in a very specific environment. There are several environment variables that affect its behavior, and even the standard input stream has a special use (post parameters).

The dbcgi script is a CGI program which captures all the environment variables and standarad input, then creates a shell script which will run the Perl debugger in the same environment. You can use this shell script to deubg your program.

The dbcgi script can also be run at the command prompt. In this setting, the dbcgi program is used to replace another CGI program with a copy of itself. It makes a backup copy of the CGI program so that it can be restored. You restore the backup copy by running the dbcgi program.

Instructions

  1. Download the dbcgi program; save it in the same directory as your CGI program
  2. From the command prompt, change to the directory containing your CGI program
  3. Run the command
    ./dbcgi mycgi
    where mycgi is the name of your CGI program. dbcgi prints lines describing the commands it is running to backup your file and replace it with a copy of itself.
  4. From the web broswer, access your CGI program in the way you want to debug it. This could be by typing in a URL, or by submitting a form. dbcgi should respond with a web page telling you the name of the shell script it created. (It will save it in the /tmp directory.)
  5. Back at the command prompt, run the command
    ./dbcgi mycgi
    This time dbcgi notices the backup copy of your program and restores it. It also copies the shell script that was made to the current directory. Again, it prints lines describing the commands it is running.
  6. Open your CGI program using emacs
  7. Tell emacs to run the Perl debugger by pressing Alt-x and entering the perldb command.
  8. Emacs asks how you would normally run your program. Erase what Emacs has suggested and replace it with
    ./mycgi.env mycgi
    where mycgi is the name of your CGI progmram. (Note this assumes you currently have your source code open in Emacs. If you did not, you would have to give the full path names of the files.)

You are now debugging your program in a simulation of the web environment.

Notes

This script should work for all types of input parameters, including text file uploads. It will break if you upload a binary file because a shell script cannot contain binary characters.