Looking at the Invocation Stack

The $STACK intrinsic special variable and the $STACK() function provide a mechanism to access M stack context information.

Example:

        GTM>WRITE $STACK
        2
        GTM>WRITE $STACK(2,"ecode")
        ,M6,Z150373850,
        GTM>WRITE $STACK(2,"place")
        name+3^DMEX
        GTM>WRITE $STACK(2,"mcode")
        if ln<30,bame?1.a.1"-".a1","1" "1a.ap do print q
        GTM>
          

This example gets the value of $STACK and then uses that value to get various types of information about that stack level using the $STACK() function. The "ecode" value of the error information for level two, "place" is similar to $ZPOSITION, "mcode" is the code for the level.

In addition to the $STACK intrinsic special variable, which provides the current stack level, $STACK(-1) gives the highest level for which $STACK() can return valid information. Until there is an error $STACK and $STACK(-1) are the same, but once $ECODE shows that there is an "current" error, the information returned by $STACK() is frozen to capture the state at the time of the error; it unfreezes after a SET $ECODE="".

Example:

        GTM>WRITE $STACK
        2
        GTM>WRITE $STACK(-1)
        2
        GTM>
          

This example shows that under the conditions created (in the above example), $STACK and $STACK(-1) have the same value.

The $STACK() can return information about lower levels.

Example:

        +1^GTM$DMOD
        GTM>WRITE $STACK(1,"ecode")
        GTM>WRITE $STACK(1,"place")
        beg^DMEX
        GTM>WRITE $STACK(1,"mcode")
        beg for read !,"Name:",namde do name
        GTM>
          

This example shows that there was no error at $STACK level one, as well as the "place" and "mcode" information for that level.