How do you retrieve the call stack information in the rpgle program? For example, if we call pgm1, we want to know who called it or look for a particular program in the call stack. Is there any way to check the program call stack? Who is the program’s caller who called this program or whether a particular program is in the call stack so that we can build logic based on call stack program information?
We can retrieve call stack information using the retrieve call stack (
QWVRCSTK
) api. We use this retrieve call stack api whenever we want to find the calling program name. We use this api when we want to execute or skip some logic in the current program in the call stack based upon the calling program means who called this program. The thread’s most recent call is indicated by the first call stack record that is returned. Here is the sample program that retrieves the call stack information and finds the calling program name.Here,
psds
is declared to fetch the current job and program information. Jobname is passed as*
, indicating the current job. You can change the following checkIf (curPgmName Pgmname);
in the program to check the provided program’s call stack entry or the call stack entry preceding it, which is the caller of the supplied program. So we can change conditions likeIf (Pgmname = ‘TESTPGM’);
which means you are checking for the specified program in the call stack entry.We can use retrieve call stack API (QWVRCSTK) to retrieve the call stack information or the presence of a specific program in the call stack or find the calling program.
I was searching for it more, and I just discovered that IBM has launched a new
table function
,STACK_INFO
, that does the same job for us as QWVRCSTK API does. It seems to me that retrieving call stack information using the SQL table function stack_info is quite simple, as it allows us to use the SQL select to retrieve a record for each entry in the call stack for a specific thread in a job.You can use SQL to retrieve program stack information. The
stack_info table
function does the same task for us as done by the Display Job (DSPJOB
) CL command and the Retrieve Call Stack (QWVRCSTK
) API. table function can be used in simple SQL select statements. By the way, it works for all types of call stacks such as OPM, ILE, pase, java, and lic stack.This table function requires 2 input parameters namely
JOB_NAME
which is the qualified name of the job,*
is a current job, andTHREAD_ID
which means all the information from all threads gets returned. The default isINITIAL
.For example, passing qualified job name in table function stack-info and thread as
ALL
to select all rows of this specific job for all threads.or passing the current job (*) in the table function stack-info and thread as ‘ALL’ to select all rows of this specific job for all threads.
sample rpgle program to retrieve call stack information using SQL table function stack_info.