Debuggers
If you are programming
in a high-level language and want to run
a debugger, it is usually impossible to do so directly. However, it
is possible to simulate the environment in which an Apache script
runs. The first thing to do is to become the user that Apache runs
as. Then, remember that Apache always runs a script in the
script’s own directory, so go to that directory.
Next, Apache passes most of the information a script needs in
environment variables. Determine what those environment variables
should be (either by thinking about it or, more reliably, by
temporarily replacing your CGI with one that executes
env, as illustrated earlier), and write a little
script that sets them then runs your CGI (possibly under a debugger).
Since Apache sets a vast number of environment variables, it is worth
knowing that most CGI scripts use relatively few of
them — usually only QUERY_STRING
(or
PATH_INFO
, less often). Of course, if you wrote
the script and all its libraries, you’ll know what
it used, but that isn’t always the case. So, to give
a concrete example, suppose we wanted to debug some script written in
C. We’d go into .../cgi-bin and
write a script called, say, debug.cgi, that
looked something like this:
#!/bin/sh QUERY_STRING='2315_order=20&2316_order=10&card_type=Amex' export QUERY_STRING gdb mycgi
We’d run it by typing:
chmod +x debug.cgi ./debug.cgi
Once gdb came up, we’d hit
r<CR>
, and the script would run.[5]
A couple of things may trip you up here. The first is that if ...
Get Apache: The Definitive Guide, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.