callstack

Added a subcommand to ensemble command info to print the callstack.

namespace eval debug {
    # callstack sub command is added to the info command
    #set map [dict create {*}[namespace ensemble configure info -map]];
    if {![dict exists [namespace ensemble configure info -map] callstack]} {
        namespace ensemble configure info \
            -map [dict merge [namespace ensemble configure info -map] {callstack debug::callstack}]
    }
}

proc debug::callstack {args} {
    # prints the callstack of a tcl procedure
    if {![llength $args]} {
        set stack "";
        for {set i 1} {$i < [info level]} {incr i} {
            if {$i eq 1} {
                set stack "================================================================================\n"
            }
            append stack "[info frame -$i]"
            if {$i < [info level] - 1} {append stack \n}
        }
        return $stack;
    }
}

usage:

proc A {} {B}

proc B {} {C}

proc C {} {puts [info callstack]}

% A
================================================================================
type proc line 1 cmd {info callstack} proc ::C level 1
type proc line 1 cmd C proc ::B level 2
type proc line 1 cmd B proc ::A level 3