trace(n) | Tcl Built-In Commands | trace(n) |
trace - Monitor variable accesses, command usages and command executions
trace option ?arg arg ...?
This command causes Tcl commands to be executed whenever certain operations are invoked. The legal options (which may be abbreviated) are:
Ops indicates which operations are of interest, and is a list of one or more of the following items:
When the trace triggers, depending on the operations being traced, a number of arguments are appended to commandPrefix so that the actual command is as follows:
commandPrefix oldName newName op
Ops indicates which operations are of interest, and is a list of one or more of the following items:
When the trace triggers, depending on the operations being traced, a number of arguments are appended to commandPrefix so that the actual command is as follows:
For enter and enterstep operations:
commandPrefix command-string op
For leave and leavestep operations:
command command-string code result op
CommandPrefix executes in the same context as the code that invoked the traced operation: thus the commandPrefix, if invoked from a procedure, will have access to the same local variables as code in the procedure. This context may be different than the context in which the trace was created. If commandPrefix invokes a procedure (which it normally does) then the procedure will have to use upvar or uplevel commands if it wishes to access the local variables of the code which invoked the trace operation.
While commandPrefix is executing during an execution trace, traces on name are temporarily disabled. This allows the commandPrefix to execute name in its body without invoking any other traces again. If an error occurs while executing the commandPrefix, then the command name as a whole will return that same error.
When multiple traces are set on name, then for enter and enterstep operations, the traced commands are invoked in the reverse order of how the traces were originally created; and for leave and leavestep operations, the traced commands are invoked in the original order of creation.
The behavior of execution traces is currently undefined for a command name imported into another namespace.
Ops indicates which operations are of interest, and is a list of one or more of the following items:
When the trace triggers, three arguments are appended to commandPrefix so that the actual command is as follows:
commandPrefix name1 name2 op
CommandPrefix executes in the same context as the code that invoked the traced operation: if the variable was accessed as part of a Tcl procedure, then commandPrefix will have access to the same local variables as code in the procedure. This context may be different than the context in which the trace was created. If commandPrefix invokes a procedure (which it normally does) then the procedure will have to use upvar or uplevel if it wishes to access the traced variable. Note also that name1 may not necessarily be the same as the name used to set the trace on the variable; differences can occur if the access is made through a variable defined with the upvar command.
For read and write traces, commandPrefix can modify the variable to affect the result of the traced operation. If commandPrefix modifies the value of a variable during a read or write trace, then the new value will be returned as the result of the traced operation. The return value from commandPrefix is ignored except that if it returns an error of any sort then the traced operation also returns an error with the same error message returned by the trace command (this mechanism can be used to implement read-only variables, for example). For write traces, commandPrefix is invoked after the variable's value has been changed; it can write a new value into the variable to override the original value specified in the write operation. To implement read-only variables, commandPrefix will have to restore the old value of the variable.
While commandPrefix is executing during a read or write trace, traces on the variable are temporarily disabled. This means that reads and writes invoked by commandPrefix will occur directly, without invoking commandPrefix (or any other traces) again. However, if commandPrefix unsets the variable then unset traces will be invoked.
When an unset trace is invoked, the variable has already been deleted: it will appear to be undefined with no traces. If an unset occurs because of a procedure return, then the trace will be invoked in the variable context of the procedure being returned to: the stack frame of the returning procedure will no longer exist. Traces are not disabled during unset traces, so if an unset trace command creates a new trace and accesses the variable, the trace will be invoked. Any errors in unset traces are ignored.
If there are multiple traces on a variable they are invoked in order of creation, most-recent first. If one trace returns an error, then no further traces are invoked for the variable. If an array element has a trace set, and there is also a trace set on the array as a whole, the trace on the overall array is invoked before the one on the element.
Once created, the trace remains in effect either until the trace is removed with the trace remove variable command described below, until the variable is unset, or until the interpreter is deleted. Unsetting an element of array will remove any traces on that element, but will not remove traces on the overall array.
This command returns an empty string.
For backwards compatibility, three other subcommands are available:
These subcommands are deprecated and will likely be removed in a future version of Tcl. They use an older syntax in which array, read, write, unset are replaced by a, r, w and u respectively, and the ops argument is not a list, but simply a string concatenation of the operations, such as rwua.
Print a message whenever either of the global variables foo and bar are updated, even if they have a different local name at the time (which can be done with the upvar command):
proc tracer {varname args} {
upvar #0 $varname var
puts "$varname was updated to be \"$var\"" } trace add variable foo write "tracer foo" trace add variable bar write "tracer bar"
Ensure that the global variable foobar always contains the product of the global variables foo and bar:
proc doMult args {
global foo bar foobar
set foobar [expr {$foo * $bar}] } trace add variable foo write doMult trace add variable bar write doMult
Print a trace of what commands are executed during the processing of a Tcl procedure:
proc x {} { y } proc y {} { z } proc z {} { puts hello } proc report args {puts [info level 0]} trace add execution x enterstep report x
→ report y enterstep
report z enterstep
report {puts hello} enterstep
hello
set(n), unset(n)
read, command, rename, variable, write, trace, unset
8.4 | Tcl |