http://qs321.pair.com?node_id=109815

CGI programs have earned the reputation for being problematic to debug. The X Window System that comes with linux includes features that can make CGI debugging much easier.

The ptkdb debugger can be invoked on the shebang line of a perl program like this:
#!/usr/bin/perl -d:ptkdb
As long as the web server machine can open the debugger window, this ptkdb debugger option works fine in a CGI program. In order to allow a any machine to open a window on your X-Windows display, execute xhost +.

The DISPLAY environment variable needs to be set in the BEGIN block of the perl code.

#!/usr/bin/perl -d:ptkdb BEGIN { # Set the DISPLAY variable to the name of the local machine # where the debugger window and web browser appear. $ENV{DISPLAY} = "mymachine:0" ; } $|++; print "Content-type: text/html\n\n"; print "<PRE>"; for (my $i=0; $i<10; $i++) { print "Count $i\n"; } print "</PRE>\n";
This technique is complementary to the more common techniques of sending the error messages to the browser, examining the error logs, and diagnostic print statements.

The nice part about this technique is that you can single step through the CGI program, set breakpoints, examine and change variables, and watch the CGI output being rendered in the browser one step at a time. All these features are available in the easy-to-use graphical user interface of ptkdb.

Note: tilly asked me to post this here after I wrote a much shorter version in the snippets section. I fixed a few spelling errors, also.

It should work perfectly the first time! - toma

Replies are listed 'Best First'.
Re: Debugging a CGI
by tachyon (Chancellor) on Sep 03, 2001 at 17:10 UTC

    Excellent post ++ toma. I have added this link to the CGI Help Guide which covers most of the common CGI headaches from the ground up.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Debugging a CGI
by stefan k (Curate) on Sep 03, 2001 at 20:03 UTC
    Hey,
    having spent mere loads of hours in apaches error logs this sounds really like a good idea! (though fortunately I haven't had to write CGIs for a good while now :)

    Just two little remarks:

    • #!/usr/perl/bin -d:ptkdb This probably should be #!/usr/bin/perl -d:ptkdb in your first call.
    • Does ptkdb come with perl? Which version? Or do we need to get from CPAN? I can't find it on my system: perl 5.6.0 running on SuSE Linux 7.1 out of the box

    Regards... Stefan
    you begin bashing the string with a +42 regexp of confusion

      In my version of Linux I needed to install:

        Tk
        This has a name like Tk800.023. It is a CPAN module by Nick Ing-Simmons. On my very slow linux machine it took quite a while to compile the included C code, but there were no problems.

        ptkdb
        This is a pure perl module by Andrew E. Page. This excellent module makes it worthwhile to go the trouble of building Tk!

      Both are available from CPAN.

      It should work perfectly the first time! - toma

      You don't need ptkdb nor X. I've debugged CGIs using the standard Perl debugger while telnet'd into the web server. Sorry, I don't have the details handy. I had to chmod the pseudo tty that telnet gave me so that the world could read/write it, then modify the CGI script to invoke the Perl debugger and tell it to use that tty as the debugging console.

      I don't recall exactly what method I used to tell the Perl debugger to use an alternate tty. You can use the PERLDB_OPTS environment variables and/or the .perldb (or perldb.ini) config file but I suspect either of these will require a trick similar to:

      #!/usr/bin/perl -w use strict; BEGIN { if( @ARGV && $ARGV[0] eq "-debugging" ) { shift @ARGV; } else { $ENV{PERLDB_OPTS}= 'TTY=/dev/pty06'; chdir('/home/tye/cgibin') or die "Can't chdir to /home/tye/cgibin: $!\n"; exec( $^X, "-d", $0, "-debugging", @ARGV ); } }
      (though I'm not sure $^X will always be sufficient so you might have to hard-code "/usr/bin/perl" there to match your #! line.)

              - tye (but my friends call me "Tye")
        I think I can see where you are headed with this, but I didn't get your example to work yet. Your idea would be especially useful on systems with firewall constraints that do not allow X Window traffic to pass from the web server to the developer's X server.

        This is what I tried:
        I used telnet to log onto the web server, and made a little cgi program that used your code. I set the TTY variable to be the same as the device used by my telnet window. I used chmod to open up read and write priviledges on the TTY device for the telnet window. I invoked the CGI from my web browser and I saw the prompt for the debugger come up in my telnet window. But when I typed a debugger command, the command went to the shell instead of going to the debugger. So the debugger commands weren't executed, and the CGI program hung.

        Suggestions?

        It should work perfectly the first time! - toma