| |||
| Home > Working with the CLI > Using variables in the debugger > Stack references | |||
When a function is invoked in C/C++, space is allocated on the stack for most local variables. Typically, space is also allocated for a return address for returning to the calling routine. If a function calls another function, all information is saved on the stack to continue execution when the called function returns. The function is now nested.
You can reference variables and functions nested on the stack implicitly or explicitly.
Within the debugger, you can implicitly reference variables on the stack as follows:
To refer
to variables on the stack in the current function, specify the name
of the variable, for example x.
To refer to a local variable in a nested function,
specify the function name followed by a backslash and the name of
the local variable (main\i for example). If the
nested function is recursive, the last occurrence of that function
is used. An explicit reference enables any occurrence to be selected.
A function is allocated storage for its variables on the stack
when it is currently executing. To refer to variables on the stack
explicitly, you must specify the nesting level of the function preceded
by an at sign @. The Call Stack window in source-level
mode displays nesting level information. The current function is @0,
its caller is @1.
You can reference functions on the stack as follows:
To refer to the address where some function on the
stack returns, specify the function nesting level preceded by an
at sign @. For example, GO @1 executes the program
until the debugger reaches the address that corresponds to the location
where the current function returns to its caller (the instruction
after the call). The LIST and DISASSEMBLE commands can
be used to show the code at the return address (LI @2 for
example).
In nonrecursive programs, the command GO @1 corresponds
to setting a breakpoint when the current function returns to its
caller. In recursive programs, the address alone might not be enough
to specify the instance that you want. A command such as GO@1; until(depth
== 4) can be used to specify the instance of the address
that you want (assuming depth is a local variable
in your recursive function that determines the instance you are
executing).
To explicitly refer to a local variable in a nested
function, specify the function nesting level followed by a backslash
and the name of the variable. For example, PRINTVALUE @3\str references
the local variable str of the function at nesting
level 3.
To see all available information about a function, specify the EXPAND command followed by the function nesting level. For example, EXPAND @7 displays all information about the function at the specified level for that particular invocation. This information includes the name of the function, the address that is returned to, and all local variables in the function and their values.