Giovanni's logo
CGI debugging tips
this means Version 2
power search
blue line

What to do when the time comes to debug a CGI program

That time will come soon, just after starting.

Error 500

CGI Program failed
is waiting for you.
So, better be prepared.

Note - In this page it is assumed that you run the Apache HTTP server.

1- Setup

Whenever a program check is met, Apache HTTP server jobs bump out, and all the diagnostics are then in their joblogs. So, first of all, HTTP server jobs must have a readable joblog. This is how you do it:
  1. chgjobd qhttpsvr/qzhbhttp log(4 00 *seclvl) inqmsgrpy(*dft)
  2. endtcpsvr *http
  3. strtcpsvr *http
Second, you must change the "script time-out" value in your http directives, by specifying a time value high enough to let you complete your debugs without having the server job terminating because of an excessive wait time on the server response.
  1. edtf '/www/http_instance_name/conf/httpd.conf'
  2. add or change the following directive
    TimeOut nn
    by specifying af least 1800 (30 minutes) for nn .
  3.   endtcpsvr *http
  4.   strtcpsvr *http
If the HTTP server job does not bump out and you want to debug a CGI program of yours, you must first find out which HTTP job is going to serve your request.
To find out the HTTP job servicing your request
  1. wrkactjob sbs(qhttpsvr) job(instance_name)
  2. press F10 several times to reset counts
  3. run any HTTP request that would not fail
  4. press F5 to refresh the counts
  5. the HTTP job servicing your request is the CGI one displaying some CPU time.

2- HTTP instance configuration

  1. If your browser displays error 404, that may just mean that your HTTP directives are not working
  2. If you have installed library cgidev2, clear the cash of your web browser and try to run the following:
    (replace ... with the TCP address of your AS/400)
    • If you get no response, but an Error 400 message, it may mean that your HTTP server is not serving your request. Some of the causes:
      • Your HTTP server is not active; you can check it by entering command
        wrkactjob sbs(qhttpsvr)
      • Your HTTP server is active, but some other active HTTP server is locking port 80 for its own exclusive use. This happens for instance when you also run DOMINO. You may then try to change the port of your HTTP configuration in the following way:
        1. Enter command
          edtf '/www/http_instance_name/conf/httpd.conf'
          to edit the HTTP server instance directives
        2. Look for a Listen directive.
        3. Change the port number on the Listen directive.
          For instance, instead of port 80, assign port 7777.
        4. Re-start the HTTP server instance
        5. Re-try the URL by entering
  3. If you get Error 500 in running
    do the following:
    • make sure that library CGIDEV2 and program CGIDEV2/HELLO1 are authorized to the *public for *use
    • make sure that the configuration directives of your HTTP server contain the directives needed to run CGIDEV2, and that this HTTP server was re-started after installing such directives (to install CGIDEV2 HTTP directives, run command command CGIDEV2/HTTPCFG)
    • make sure you installed the last available PTF Cumulative for product 57xxDG1 (IBM HTTP Server for iSeries)

3- Error 500 trivial cases

Error 500 is raised in four cases:
  1. The mandatory HTTP header is missing.
    The HTTP headers are statements at the beginning of the output generated from a CGI for the browser.
    The fist mandatory HTTP header must be:
    Content type: ...
    In absense of such HTTP header, you get ERROR 500 from Apache. Another common HTTP header is
    Expires: 0
  2. The mandatory HTTP headers separator is missing.
    The HTTP headers must end with a sequence of two Carriage-Return-Line-Feed (CRLF).
    This is implemented by adding an empty line after the last HTTP header.
    In absense of such a separator, you get ERROR 500 from Apache.
  3. The output buffer is not sent to the browser
    The CGI program returns without sending the output buffer via the statement
    callp wrtsection('*fini')
  4. The CGI program faces a program exception and is terminated. This is the topic discussed hereafter.

4- Error 500 non trivial case

If the HTTP instance job servicing your CGI request was terminated due to a program exception, you should first look at its joblog (output queue QEZJOBLOG).
In many cases, error messages provide enough information to enable program correction.
For more complex cases, you need to debug your CGI program.

To perform debug, you must have compiled your ILE modules with the option
If you have done so,
  1. strsrvjob ..... the http server job which you expect to process your next CGI request
  2. strdbg your_CGI_program updprod(*yes)
    Note- To be quicker, you may use command EDBG (Enhanced Debug) available with CGIDEV2 (use F4 to prompt parameters).
  3. Some basics, when the source of the initial module is displayed
    • on the command line,
      F string
      to find a string
    • F16 to find next
    • move a cursor to a source line and press F6 to add a breakpoint
  4. Rerun your CGI program.
    Hopefully it will stop at some breakpoint of yours, you may tell from your debug session
  5. More hints on debug
    • position the cursor on a variable and press F11 to display its value
    • you can do the same on the command line by entering
      eval variable_name
    • if you want to display the value of a variable in hexadecimal format, on the command line enter
      eval variable_name:x
    • to change the value of a variable, on the command line enter
      • eval variable_name = 'value'
        if it is a character variable
      • eval variable_name = value
        if it is a numeric variable
    • to execute one step of coding, press F10
    • to resume execution till the next breakpoint, press F12
    • if your module calls another module, and you want to debug this latter one
      • add a breakpoint on the call statement
      • when execution stops on this statement, press F22 to display and add breakpoints to this latter module
By sure you will become the most proficient debugger in your neighbourhood!