13
Wed, Nov
5 New Articles

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..

Jan Jorgensen

Jan Jorgensen is one of the owners of www.reeft.dk, which specializes in mobile and i5 solutions. He works with RPG, HTML, JavaScript, Perl, and PHP. 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

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: