If a proc that is currently executing adds an execution leave trace on itself,
the trace only fires when that call returns if the proc already had an (unrelated)
execution trace registered for it when it started executing. Otherwise the trace fires
only on the next call to the command:
proc _finish {target cmd code result op} {
trace remove execution $target leave [list ::_finish $target]
# fire finish breakpoint
puts "would fire fini break"
}
proc foo {} {
puts "in foo"
set target [namespace which -command [lindex [info level 0] 0]]
trace add execution $target leave [list ::_finish $target]
}
# Works if this line is uncommented
#trace add execution foo enter {apply {args {}}}
puts "first foo: [foo]"
puts "second foo: [foo]"
@aspect on the Tcl chat found this: "the block in EvalObjvCore that sets up
TEOV_RunLeaveTraces is under an if whose condition includes (cmdPtr->flags & CMD_HAS_EXEC_TRACES)"
I understand that the performance overhead may be prohibitive for rechecking each time a
proc finishes, but I'm raising this ticket in case it can be made feasible, or at least
be documented that registering a leave trace on a proc on the call stack is undefined
(depends on other trace state)
|