There is an API, QWVRCSTK, which will return the job stack.
And it so happens that there was recently a series of three articles using the QWVRCSTK API to demonstrate various techniques that can be used with system APIs. The first article can be found at this site.
Hope this helps,
There are a couple ways to retrieve the call stack, but it’s essentially pointless in this case.
It essentially means that every program would retrieve the call stack before calling any other program to see if a recursion error was possible. Then each time it found that a program was already in the stack, it would need to look at program attributes to see if the program supported recursion. (A program that runs in a *NEW activation group can be called recursively. The same program might not be called recursively in a named AG. CL supports recursion natively; RPG and COBOL do not. Other considerations apply.)
Rather than retrieving the call stack and doing all of the analysis in order to avoid the CALL, simply execute the CALL and handle the error. Done.
It’s far more efficient, far lass code, far more maintainable and much, much easier to understand.