Wednesday, September 28, 2016

Quickly retrieving the source for a program

retrieve source of rpg program

Once in a while I find a program in one the libraries I am responsible for that is missing the source it was compiled from. Looking at the object description I can see that the program was compiled from a source member in the right source file, but the member is not there. Other times a program was compiled from a member in a source file or library that no longer exists, but I can find a member of that name elsewhere, and I do not know if this source is the same that was used to create the program. Both scenarios should not happen in a perfect world, but most of us work in places that are not perfect.

I know there are programs various people have written to retrieve the source from a program, but I do not have time to do that today. I need to get to the source quickly to find out what this program is suppose to do.

If the object is either a CLP or CLLE and it was compiled with the "Allow RTVCLSRC" parameter, ALWRTVSRC, set to *YES I can use the Retrieve CL Source command, RTVCLSRC, to recover the source. I go into this in detail in RTVCLSRC makes a comeback.

If it is RPG or RPGLE then there is no IBM command to retrieve the source code. I need to be a bit creative in my thinking.

When I compile a RPG/RPGLE object I have choices in the debug views and these are included in the compiled object. I wrote about the debug views and the advantages of them for RPGLE in Debug views finding your favorite.

With old fashioned RPG/400 (RPG III) I have fewer options. If the program was compiled with the "Source listing options" parameter, OPTION, of *SRCDBG or *LSTDBG then the source was included in the program object.

  CRTRPGPGM PGM(MYLIB/RPG3PGM) 
              SRCFILE(DEVSRC)
              OPTION(*SRCDBG)

As neither of these are the parameters default there is a good chance that if the person who performed the compile used PDM and they just put a "14" next to the member and pressed Enter the source will be missing from the object.

The other debugger that many of us have used with old RPG source is STRISDB. This will display the source for the program if the source member is where it was when the program was compiled. If the member has been deleted, renamed, or moved STRISDB cannot display the source code.

I am going to say that today I am lucky, and the program I need to the retrieve the source for was compiled with the appropriate debug values. I can just start debug using the "Start debug" command, STRDBG, for both RPGLE and RPG:

For RPGLE objects:  STRDBG PGM(PGMSDH/TESTRPG)

For RPG object:  STRDBG PGM(PGMSDH/TESTRPG3) OPMSRC(*YES)

And the source code is displayed:

                             Display Module Source

 Program:   TESTRPG        Library:   MYLIB          Module:   TESTRPG
      1  **free
      2  dcl-s Date1 char(6) ;
      3  dcl-s Date2 char(6) ;
      4  dcl-s Difference packed(10) ;
      5
      6  Date1 = '122516' ;
      7  Date2 = '022195' ;
      8
      9  Difference = %diff(%date(Date1:*mdy0):%date(Date2:*mdy0):*d) ;
     10
     11  *inlr = *on ;


                                                                         Bottom
 Debug . . .                                                                  
                                                                             
 F3=End program   F6=Add/Clear breakpoint   F10=Step   F11=Display variable
 F12=Resume       F17=Watch variable   F18=Work with watch   F24=More keys

In this example it would be easy to copy-n-paste the source into a source member. But if the program was one of those thousand line monsters it would take a long time to copy a screen of the source at a time. Fortunately there is an easier way to do a larger program than the one shown above.

If you do not have RDi, Operations Navigator has a debug feature within the "Run SQL Scripts" function. On your computer Operation Navigator could be known as iSeries Navigator or System i Navigator. When I open the IBM i Access for Windows folder from the start menu I find Operation Navigator within it. On my PC it is called System i Navigator.


Click on image to open a larger version in another browser window

When the Navigator opens in the left panel is a list of all IBM i I can connect to. I find the one with the program on it and click the plus ( + ) next to it. Then I find the "Databases" icon and click on the plus next to it. When that opens immediately under it is the database I want, I right click on it and select "Run SQL scripts", see below.


Click on image to open a larger version in another browser window

On the "Run SQL Scripts" window I select "Run", and then "Debugger" in the drop down.


Click on image to open a larger version in another browser window

When the "Start debugger" window opens I just need to enter the program and the library it is in, and click the "OK" button.


Click on image to open a larger version in another browser window

I can go to the code section part of the "System Debugger" window, select all the code ( Ctrl-a ), copy it ( Ctrl-c), then paste it into Notepad ( Ctrl-v ).


Click on image to open a larger version in another browser window

I save the contents of Notepad as a text file. I can now FTP that to the IBM i and place it in the appropriate source file. I would also compile the source to make ensure the next time I look at that object it refers to the right source member.

As you can imagine this has saved me on many occasions where the source has gone missing. I strongly suggest that you all compile your programs and modules with an appropriate debug view. You never know when you have to hunt for missing source, and be grateful that you did.

 

This article was written for IBM i 7.3, and should work for earlier releases too.

8 comments:

  1. Wow, that's a beauty. Thanks for sharing.
    Ringer

    ReplyDelete
  2. In more than one time I approached IBM looking for this. Thanks a lot!

    ReplyDelete
  3. I wish I had thought of that. My motto is "There is always another way...."

    ReplyDelete
  4. I think it was 2004 when I encountered a 10,000 line BPICS monster. I wrote a VB based CA/400 macro. I never needed since then, so I do not remember anything except that it was a pretty painful program to write.

    Thanks for the tip

    ReplyDelete
  5. I tried to get the source but It ask me about the member of the object. And that's the point I don't have it. I have only the object. :(

    ReplyDelete
    Replies
    1. Please be more specific: Where are you seeing this?

      Delete
  6. in the compiler option:
    OPTION (*SRCDBG)
    I get an error for "*SRCDBG" with valid options as:
    (any of these appropriate to the functionality you refer to?)
    *XREF
    *NOXREF
    *GEN
    *NOGEN
    *SECLVL
    *NOSECLVL
    *SHOWCPY
    *NOSHOWCPY
    *EXPDDS
    *NOEXPDDS
    *EXT
    *NOEXT
    *NOSHOWSKP
    *SHOWSKP
    *NOSRCSTMT
    *SRCSTMT
    *DEBUGIO
    *NODEBUGIO
    *UNREF
    *NOUNREF
    *NOEVENTF
    *EVENTF

    ReplyDelete
    Replies
    1. Those are the OPTIONS for compiling RPGLE. You do not specify the debug view in that parameter, you use the DBGVIEW instead see here for more details.

      Delete

To prevent "comment spam" all comments are moderated.
Learn about this website's comments policy here.

Some people have reported that they cannot post a comment using certain computers and browsers. If this is you feel free to use the Contact Form to send me the comment and I will post it for you, please include the title of the post so I know which one to post the comment to.