It would be great if there were a way to get the subshell level nesting. ksh uses .sh.level and bash BASH_SUBSHELL.
A debugger uses this in two ways. First to indicate the level of subshell nesting on prompts, so that as you are following along you won't be surprised when a variable which had a value get unset or the value restored to its previous value. And second to maintain global state.
The general pattern here is
debug_hook:
if ((subshell_level < old_subshell_level)) ; then
eval journal file
fi
save old subshell level
work, work,
...
something that should set or change global state:
save global variable and value in journal file,
e.g. cmd='x=5'; eval $cmd; echo "$cmd" >> journal_file
It also occurs to me that if there were a variable that automatically got *unset* and stayed that way
until explicitly set, then that could be used to sumulate such a variable.