Interesting experience with execution traces

From: Helmut Giese (hgiese_at_ratiosoft.com)
Date: 11/21/03


Date: Fri, 21 Nov 2003 17:54:26 GMT

Hello out there,
I just played around a bit with execution traces and was really
surprised about the behaviour of the enterstep / leavestep commands.
As I understood the docs

enterstep
Invoke command for every tcl command which is executed inside the
procedure name, just before the actual execution takes place ...

"inside the procedure" it says, but what it does is to _recurse_ into
any command which is not built-in.

Take this script (please correct the line breaks):
        ---
set lineCnt 1

# this proc will be called during the trace
proc showTrace {args} {
    puts "[format %2d $::lineCnt]) showTrace: $args"
    incr ::lineCnt
}

proc show {str} {
    puts "$str"
}

# this proc is to be traced
proc sum {args} {
    set sum [expr [join $args +]]
    show "sum of $args: $sum"
}

# enable traces
trace add execution sum [list enter leave enterstep leavestep]
showTrace
sum 1 2 3
        ---

This is the output it produces:
 1) showTrace: {sum 1 2 3} enter
 2) showTrace: {join {1 2 3} +} enterstep
 3) showTrace: {join {1 2 3} +} 0 1+2+3 leavestep
 4) showTrace: {expr 1+2+3} enterstep
 5) showTrace: {expr 1+2+3} 0 6 leavestep
 6) showTrace: {set sum 6} enterstep
 7) showTrace: {set sum 6} 0 6 leavestep
 8) showTrace: {show {sum of 1 2 3: 6}} enterstep
 9) showTrace: {puts {sum of 1 2 3: 6}} enterstep
sum of 1 2 3: 6
10) showTrace: {puts {sum of 1 2 3: 6}} 0 {} leavestep
11) showTrace: {show {sum of 1 2 3: 6}} 0 {} leavestep
12) showTrace: {sum 1 2 3} 0 {} leave

As you can see in lines 9 and 10, also the 'puts' inside 'show' gets
traced, not only the commands inside 'sum' as I expected.

IMHO, this behaviour makes this feature a lot less useful: A simple
'puts' gave me screens full of output when running in TkCon - amazing,
what TkCon has to do to write out a simple line, but not necessarily
what I was interested in seeing at this moment.

At least the docs should get updated to mention something about
recursive descent, but I wonder if a step wise trace which _does not
recurse_ would be of use. Any comments ?

On a side note: This can be a very useful feature explaining the inner
workings of Tcl to a beginner (which incidentally made me look at it):
Look at the 'leavestep' lines in the sample above to see all the
transformations which take place - just don't throw in a 'puts' when
running under TkCon :)

Best regards
Helmut Giese



Relevant Pages

  • Re: trace doc confusion
    ... Invoke command for every Tcl command which is executed inside the ... just before the actual execution takes place. ... an enterstep trace on a command will not result in an error and is ...
    (comp.lang.tcl)
  • Re: trace doc confusion
    ... So we have two competing meanings for the word: ... ...setting an enterstep trace on a command will not ... ... trace add execution name ops *script* ...
    (comp.lang.tcl)
  • trace doc confusion
    ... I'm confused when reading the trace documentation for trace add ... Invoke command for every Tcl command which is executed inside the ... just before the actual execution takes place. ...
    (comp.lang.tcl)
  • Re: trace doc confusion
    ... trace add execution name ops command ... ...setting an enterstep trace on a command will not ... ... trace add execution name ops *script* ...
    (comp.lang.tcl)
  • Re: trace doc confusion
    ... Invoke command for every Tcl command which is executed inside the ... just before the actual execution takes place. ... an enterstep trace on a command will not result in an error and is ...
    (comp.lang.tcl)