Updated 2007-09-27 14:09:01 by tomk

The Pstack package prints the sequence of procedure calls that lead to the execution of a user selected procedure. Argument values for all procedures are included in the printed output. To print the call stack for a particular procedure place $Pstack::print somewhere in the procedure. Printing of the call stack can be turned on and off from anywhere in your program using the Pstack::printON and Pstack::printOFF commands.

Tom Krehbiel

note - If you're looking at this page you may also want to look at traceback ... tjk.

Example
 package require Pstack

 proc A { } {
     B -x
     E
 }
 proc B { x } {
     C -x -y
 }
 proc C { x y {z -z} } {
     $Pstack::print
     D -x -y
 }
 proc D { x y} {
     E
 }
 proc E { } {
     $Pstack::print
 }

 puts "# trace calls"
 Pstack::printON
 A
 puts "# don't trace calls"
 Pstack::printOFF
 A

Example Output

Notice that only passed arguments are printed, not arguments that are defaulted.
 # trace calls
 --- pstack
 A {}
   B {-x}
     C {-x -y}
 --- pstack
 A {}
   B {-x}
     C {-x -y}
       D {-x -y}
         E {}
 --- pstack
 A {}
   E {}

 # don't trace calls

 package provide Pstack 1.0

 # --------------------------------------------------
 # This package prints the sequence of calls that
 # preceeded the execution of a selected procedure.
 # Arguments are included in the printed output.
 #
 # Place $Pstack::print some where in the
 # procedure you are interested in tracing.
 #
 # Tracr printing is turned on and off with the
 # Pstack::printON and Pstack::printOFF commands.
 # --------------------------------------------------

 namespace eval Pstack {
     variable print
     set print Pstack::OFF
 }

 # ------------------------------
 # turn traccing on/off
 proc Pstack::printON { } {
     variable print
     set print Pstack::ON
 }
 proc Pstack::printOFF { } {
     variable print
     set print Pstack::OFF
 }
 # ------------------------------
 # no output
 proc Pstack::OFF { } { }

 # ------------------------------
 # print proc name and arguments
 proc Pstack::ON { } {
     puts stderr "--- pstack"
     set indent ""
     set up_level [expr [info level]-1]
     for {set i 1} {$i<=[expr [info level]-1]} {incr i} {
         set argList [info level ${i}]
         puts stderr "${indent}[lindex $argList 0] {[lrange $argList 1 end]}"
         append indent "  "
     }
 }

escargo 29 Nov 2005 - I changed the first puts in Pstack::ON to output to stderr like to puts in the following loop does. It seems like output should be going to only one place, not a label to stdout and data to stderr.

Category Debugging | Category Example | Category Package