TechTip: Debugging CGI Programs

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

This TechTip will show you how to debug CGI programs in a green-screen environment.
Before we start, I expect a few things from you:

  • You have already configured a Web server in your iSeries to run CGI programs.
  • You know the interactive debugger (STRDBG).
  • You have access to create/start/stop Web server instances in your iSeries.

Note: This tip applies to the Apache Web server, although I'll show how to do almost the same thing with the original AS/400 HTTP Web server.

First of all, let me explain the "problem" with the Apache Web server running on the iSeries. When the Apache Web server starts, several instances are started for serving HTTP (QZSRHTTP), several for logging (QZSRLOG), and several for serving CGI (QZSRCGI). To debug a CGI program, you have to do it through a service job (STRSRVJOB), but because several instances exist, it is impossible for you to know exactly which instance your CGI request will be processed in and which instance you should run the service job against. Of course, you could open up multiple screens and start a service job on every instance, but that is a cumbersome waste of time.

What we want to do is control in which instance the CGI programs are processed.

In my shop, we have a development machine, so we can do pretty much what we want, but let's say that this is not the case in your shop and you want to debug on the production machine. Naturally, you cannot disturb the production Web server in any way. So we will clone the Web server already running and then modify it a little bit to accomplish our task. Instead of using the *ADMIN browser interface, I'll show you the quick and dirty way to do this, but please don't tell a soul!

In my example, I am using the default Apache www dir, and my Web server instance is called webserver, so you might have to change that a bit if your setup is different.

Here we go:

1. Enter wrklnk '/www/*'.

2. Place your cursor in front of the webserver dir and enter 3=Copy and F4.

3. In parameter TOOBJ, enter /www/webdebug.

4. In parameter SUBTREE, enter *ALL. (See the figure below.)


http://www.mcpressonline.com/articles/images/2002/Debugging%20CGI%20programsV4--05120600.jpg

5. Press Enter. After a little while, a completion message will be shown.

If the Web server you copy from is active and is using logs, you'll get a message saying that some objects copied and two failed. That's OK. Don't panic. They are just locked by the running Web server.

6. Press F5 to refresh and enter 5 in front of webdebug.

7. Enter 5 in front of conf and press Enter.

8. Enter 2 in front of httpd.conf and press Enter.

You are now ready to edit the configuration file. What you want to change is the number of CGI instances being started and the port the server is listening at.

9. Look for the Listen directive. It will most likely look like this: Listen *:80. Change it to Listen *.801, which means that the instance will listen on port 801. (If you have something else running on port 801, you must pick a port not in use.) If the directive is not in the file, add it at the end of the file.

10. Look for the ThreadsPerChild directive. Replace whatever value is after it with 1 (one). If the ThreadsPerChild directive is not there, add it at the end of the file. (See the figure below.)

http://www.mcpressonline.com/articles/images/2002/Debugging%20CGI%20programsV4--05120601.jpg

11. Press F2 to save and F3 to exit the editor.

12. Use PDM to locate file QATMHINSTC in library QUSRSYS.

13. Enter "12=Work with" and press Enter.

 14. Locate member webserver and copy it to member webdebug.

15. Enter "18=Change using DFU" and press Enter.

16. The member contains only one record, which looks like this: apache -d /www/webserver -f conf/httpd.conf. Change it to apache -d /www/webdebug -f conf/httpd.conf.

17. Save and exit DFU.

You are now ready to fire up the new Web server for the first time. Enter the following command:

STRTCPSVR SERVER(*HTTP) HTTPSVR(WEBDEBUG)

To ensure the Web server is running, enter the following:

WRKACTJOB SBS(QHTTPSVR)

Look for the webdebug job. If you cannot find it, you'll have to check the above points once again in case you missed a step.

Start Debugging

You are now ready to debug a CGI program.

I have provided a small and simple program called HELLOWORLD. All it does is write a message to the browser. Download it to your CGI library and compile it as described at the top of the source. Then, start your favorite browser and enter the following address:

http://your-server:801/cgi-bin/helloworld.pgm
(Note the :801. This is the Listen directive in action.)

You should see a message in the browser.

Now it's time to debug the program:

1. Enter WRKACTJOB SBS(QHTTPSVR) and find the job named WEBDEBUG running the function called PGM-QZSRCGI. Write down the user ID (QTMHHTTP) and the job number.

2. Enter STRSRVJOB JOB(job_no/QTMHHTTP/WEBDEBUG) and press Enter to start the service job.

3. Do a STRDBG HELLOWORLD (remember to add your CGI library) and press Enter.

Note: If you do not see any source code, do ENDDBG and recompile the CGI program with DBGVIEW set to *SOURCE or *LIST and then repeat the STRDBG. You do not have to end the service job.

4. Set a breakpoint somewhere in the source and press F12.

5. Go to your browser and press F5 or re-enter the
http://your-server:801/cgi-bin/helloworld.pgm URL.

6. Return to your green-screen, and you'll see that the debugger has stopped at your breakpoint. You can now debug the program as you would any other program.

Note: You do not have all the time in the world to debug the program. The Web server will timeout the job and end the service job. You can control this by using the TimeOut directive in the httpd.conf file.

When you're done, end the debugger and service job by entering ENDDBG and ENDSRVJOB. If you want to end the Web server, enter ENDTCPSVR SERVER(*HTTP) HTTPSVR(WEBDEBUG).

This completes the debugging session, and I hope that this has shed a little light on a task that sometimes can be quite cumbersome.

But wait! There's more! What about the original HTTP server? Simply enter the following command against a configuration created with WRKHTTPCFG or the Web interface:

STRTCPSVR SERVER(*HTTP) HTTPSVR(WEBDEBUG '-p 801 -minat 1 -maxat 1')

You should be well on your way!

Jan Jorgensen is a programmer at Electrolux Laundry Systems Denmark. He works with stuff like RPG, HTML, JavaScript and Perl. You can reach him at This email address is being protected from spambots. You need JavaScript enabled to view it..

BLOG COMMENTS POWERED BY DISQUS