TechTip: Little-Known Source Debugging Tricks

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

One of my coworkers was having a strange problem the other day, and we were using the source debugger to trace down the cause. We figured out the problem eventually, but in the course of our work, I discovered some debugger commands and options that I was previously unaware of. Since I have been using the debugger for...umm...awhile now, I was surprised to discover something new, so I thought I'd share what I learned with you.

%SUBSTR Built-In Function

This function is great when you're working with large strings. You can view a segment of a string as shown below (assume that StringFldA = 'abcdefghijklmnopqrstuvwxyz'):

Debug command: EVAL %SUBSTR(StringFldA 12 5)
Displayed result: %SUBSTR(StringFldA 12 5) = 'lmnop'

This is convenient when working with larger string fields. You can also use the %SUBSTR function to set the value of a specific portion of a string. For me, this is the more useful of these two functions.

Debug command: EVAL %SUBSTR(StringFldA 12 5) = 'xxxxx'
Displayed result: %SUBSTR(StringFldA 12 5) = 'xxxxx'
Debug command: EVAL StringFldA
Displayed result: StringFldA = 'abcdefghijkxxxxxqrstuvwxyz'

You can also use %SUBSTR to set a conditional breakpoint or watch condition. For example, the following code would stop execution only when positions 12 thru 16 of StringFldA are 'xxxxx':

Debug command: BREAK 100 when %SUBSTR(StringFldA 12 5) = 'xxxxx'

Or you could watch for those same positions to change by using this watch condition:

Debug command: WATCH %SUBSTR(StringFldA 12 5)

This way, anytime the contents of positions 12 thru 16 change, program execution stops and you are notified.

%INDEX Built-In Function

The %INDEX function is handy when you're using multiple-occurrence data structures. It's also useful in combination with _QRNU_DSI_xxxx (where xxxx = the name of a multi-occurrence data structure). The command EVAL _QRNU_DSI_xxxx returns the current occurrence of a multiple-occurrence data structure. Using the %INDEX function will change the current occurrence. See the example below:

d WorkDS1         ds                  occurs(3)    
d  StringA                      10a                
d  StringB                      25a                

Debug command: EVAL _QRNU_DSI_WorkDS1
Displayed result: 1 (or whatever the current occurrence of WorkDS1 is)

Debug command: WorkDS1 = %INDEX(3)
Displayed result: WorkDS1 = %INDEX(3) = 3
(Interrogated subfields will now reflect the values the of third occurrence of the data structure.)

Parting Shot

In our shop, many of the field names are really long, so if you don't type well (or just don't like to type), then using the EQUATE debug command can help you. This command allows you to define an "alias" for a field name, expression, or command. For instance...

Debug command: EQUATE SmName This_is_a_really_long_field_name

You can then find the value of "This_is_a_really_long_field_name" by keying the command shown below:

Debug command: EVAL SmName

The EQUATE command can also be used to create a macro of sorts. This is done by assigning an alias to a complete command. Here's an example:

Debug command: EQUATE SmCmd EVAL %substr(StringA,5,5)

By keying SmCmd and pressing Enter, you can display the value of positions 5 thru 9 of the StringA variable.

So the next time you start testing a program or chasing a bug, remember these, and you might save yourself some headaches. Good luck and happy hunting.

Jeff Olen is an analyst in the IS department at Costco Wholesale in Issaquah, Washington (just outside Seattle). He has nearly 20 years of experience on midrange systems and has developed software for a wide range of applications. He may be reached at This email address is being protected from spambots. You need JavaScript enabled to view it..


Jeff Olen

Jeff Olen is a super-spy now but keeps his cover identity intact by working for video game studios on the East Coast. So when he’s not out killing members of ISIS or rescuing refugees, you can find him playing Wolfenstein II or testing the new Fallout 76 releases at his beach house in Costa Rica. In any case, he can’t be reached. You can email his cat at This email address is being protected from spambots. You need JavaScript enabled to view it.. She will pass on your message…if she feels like it.


MC Press books written by Jeff Olen available now on the MC Press Bookstore.

The IBM i Programmer’s Guide to PHP The IBM i Programmer’s Guide to PHP
Get the scoop on how PHP can—and should—be deployed on IBM systems.
List Price $79.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

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

  • 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

  • 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: