trace(n)
π NAME π
okay bestie, gather round, because this is trace from the Tcl Built-In Commands manual β trace(n) if you're the type to file things properly. And what does it do? Monitor variable accesses, command usages and command executions. That's the whole vibe. β¨
π¦ SYNOPSIS π¦
trace option ?arg arg ...?
π DESCRIPTION π
So here's the tea: this command makes Tcl commands get executed whenever certain operations are invoked. You're basically installing little spies π on your variables and your commands, and they ping you the literal second something happens.
The legal options (and yes β they may be abbreviated, you do not have to type the whole thing out, we're not monsters) are:
β¨ trace add type name ops ?args? β¨
where type is one of command, execution, or variable. Three flavors, three completely different moods. π
π«Ά trace add command name ops commandPrefix
Arrange for commandPrefix to be executed (with additional arguments) whenever command name is modified in one of the ways given by the list ops. Name gets resolved using the usual namespace resolution rules used by commands. And if the command doesn't exist? an error will be thrown. Immediately. No cap.
Ops tells you which operations you actually care about, and it's a list of one or more of these:
renameβ invoke commandPrefix whenever the traced command is renamed. Heads up though: renaming to the empty string counts as deletion, and it will not be traced withrename. πdeleteβ invoke commandPrefix when the traced command is deleted. Commands can be deleted explicitly by using therenamecommand to rename the command to an empty string. Commands are also deleted when the interpreter is deleted, but traces will not be invoked in that case, because there's no interpreter left to execute them in. π
When the trace triggers, depending on the operations being traced, a bunch of arguments get appended to commandPrefix so that the actual command ends up looking like this:
commandPrefix oldName newName op
OldName and newName give the traced command's current (old) name, and the name to which it is being renamed (the empty string if this is a "delete" operation). Op indicates what operation is being performed on the command, and it's one of rename or delete as defined above.
The trace operation cannot be used to stop a command from being deleted. Tcl will always remove the command once the trace is complete. That's not a suggestion, that's the law. Periodt. π
Recursive renaming or deleting will not cause further traces of the same type to be evaluated, so a delete trace which itself deletes the command, or a rename trace which itself renames the command will not cause further trace evaluations to occur. Both oldName and newName are fully qualified with any namespace(s) in which they appear. π―
π trace add execution name ops commandPrefix
Arrange for commandPrefix to be executed (with additional arguments) whenever command name is executed, with traces occurring at the points indicated by the list ops. Name gets resolved using the usual namespace resolution rules used by commands. If the command does not exist, an error will be thrown. Same energy as before.
Ops tells you which operations are of interest, and it's a list of one or more of these:
enterβ invoke commandPrefix whenever the command name is executed, just before the actual execution takes place. πleaveβ invoke commandPrefix whenever the command name is executed, just after the actual execution takes place. π«‘enterstepβ invoke commandPrefix for every Tcl command which is executed from the start of the execution of the procedure name until that procedure finishes. CommandPrefix is invoked just before the actual execution of the Tcl command being reported takes place. So like, if we haveproc foo {} { puts "hello" }, then anentersteptrace would be invoked just beforeputs "hello"is executed. And plot twist: setting anentersteptrace on a command name that does not refer to a procedure will not result in an error β it's just simply ignored. π«₯leavestepβ invoke commandPrefix for every Tcl command which is executed from the start of the execution of the procedure name until that procedure finishes. CommandPrefix is invoked just after the actual execution of the Tcl command being reported takes place. And same deal: setting aleavesteptrace on a command name that does not refer to a procedure will not result in an error and is simply ignored. π«₯
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
Command-string gives the complete current command being executed (the traced command for an enter operation, an arbitrary command for an enterstep operation), including all arguments in their fully expanded form. Op indicates what operation is being performed on the command execution, and it's one of enter or enterstep as defined above. Oh, and a lil bonus: the trace operation can be used to stop the command from executing, by deleting the command in question. Of course, when the command is subsequently executed, an "invalid command" error will occur. π
For leave and leavestep operations:
commandPrefix command-string code result op
Command-string gives the complete current command being executed (the traced command for an enter operation, an arbitrary command for an enterstep operation), including all arguments in their fully expanded form. Code gives the result code of that execution, and result the result string. Op indicates what operation is being performed on the command execution, and is one of leave or leavestep as defined above.
Note that the creation of manyentersteporleavesteptraces can lead to unintuitive results, since the invoked commands from one trace can themselves lead to further command invocations for other traces. It's giving chaos gremlin energy. π
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. And 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. π¨ Not "weird", not "quirky" β undefined. Proceed with caution, babe.
π§ trace add variable name ops commandPrefix
Arrange for commandPrefix to be executed whenever variable name is accessed in one of the ways given by the list ops. Name may refer to a normal variable, an element of an array, or to an array as a whole (i.e. name may be just the name of an array, with no parenthesized index). If name refers to a whole array, then commandPrefix is invoked whenever any element of the array is manipulated. And if the variable does not exist, it will be created but will not be given a value, so it will be visible to namespace which queries, but not to info exists queries. Sneaky. π»
Ops indicates which operations are of interest, and is a list of one or more of the following items:
arrayβ invoke commandPrefix whenever the variable is accessed or modified via thearraycommand, provided that name is not a scalar variable at the time that thearraycommand is invoked. If name is a scalar variable, the access via thearraycommand will not trigger the trace. πreadβ invoke commandPrefix whenever the variable is read. πwriteβ invoke commandPrefix whenever the variable is written. βοΈunsetβ invoke commandPrefix whenever the variable is unset. Variables can be unset explicitly with theunsetcommand, or implicitly when procedures return (all of their local variables are unset). Variables are also unset when interpreters are deleted, but traces will not be invoked because there is no interpreter in which to execute them. π
When the trace triggers, three arguments are appended to commandPrefix so that the actual command is as follows:
commandPrefix name1 name2 op
Okay so when your variable trace fires, your commandPrefix gets these four args, and they're giving main character energy:
name1β gives the name for the variable being accessed. This is not necessarily the same as the name used in thetrace add variablecommand: theupvarcommand lets a procedure reference a variable under a different name. πname2β if the trace was originally set on an array or array element, this provides which index into the array was affected. This info is present even whenname1refers to a scalar, which may happen if theupvarcommand was used to create a reference to a single array element. If an entire array is being deleted and the trace was registered on the overall array, rather than a single element, thenname1gives the array name andname2is an empty string.opβ indicates what operation is being performed on the variable, and is one ofread,write, orunsetas defined above.
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. iykyk π
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. no cap, that's the whole trick. β¨
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. it's giving "quiet mode" but make it useful. π
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. literally ghosts, but in a Tcl way. π»
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. periodt. π
β¨ trace remove type name opList commandPrefix β¨
Where type is either command, execution or variable.
trace remove command name opList commandPrefixβ If there is a trace set on commandnamewith the operations and command given byopListandcommandPrefix, then the trace is removed, so thatcommandPrefixwill never again be invoked. Returns an empty string. Ifnamedoes not exist, the command will throw an error. bye bestie, no more calls. πtrace remove execution name opList commandPrefixβ If there is a trace set on commandnamewith the operations and command given byopListandcommandPrefix, then the trace is removed, so thatcommandPrefixwill never again be invoked. Returns an empty string. Ifnamedoes not exist, the command will throw an error. same vibe, execution edition. π¦trace remove variable name opList commandPrefixβ If there is a trace set on variablenamewith the operations and command given byopListandcommandPrefix, then the trace is removed, so thatcommandPrefixwill never again be invoked. Returns an empty string. variable said "not today," and the trace said "bet." π
β¨ trace info type name β¨
Where type is either command, execution or variable.
trace info command nameβ Returns a list containing one element for each trace currently set on commandname. Each element of the list is itself a list containing two elements, which are theopListandcommandPrefixassociated with the trace. Ifnamedoes not have any traces set, then the result of the command will be an empty string. Ifnamedoes not exist, the command will throw an error. receipts, but for traces. π§Ύtrace info execution nameβ Returns a list containing one element for each trace currently set on commandname. Each element of the list is itself a list containing two elements, which are theopListandcommandPrefixassociated with the trace. Ifnamedoes not have any traces set, then the result of the command will be an empty string. Ifnamedoes not exist, the command will throw an error. same list, execution flavor. πtrace info variable nameβ Returns a list containing one element for each trace currently set on variablename. Each element of the list is itself a list containing two elements, which are theopListandcommandPrefixassociated with the trace. Ifnamedoes not exist or does not have any traces set, then the result of the command will be an empty string. if there's nothing, you get nothing. it's giving minimalism. β¨
β¨ EXAMPLES β¨
Okay so imagine you wanna 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). obsessed. Here's the code:
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"
Now let's make sure the global variable foobar always contains the product of the global variables foo and bar. It's giving math but make it traced: slay.
proc doMult args {
global foo bar foobar
set foobar [expr {$foo * $bar}]
}
trace add variable foo write doMult
trace add variable bar write doMult
And if you wanna print a trace of what commands are executed during the processing of a Tcl procedure, this is the moment:
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
π SEE ALSO π
π KEYWORDS π
read, command, rename, variable, write, trace, unset
And that's the whole vibe, bestie. This page is from Tcl 8.4, trace(n) β iconic, timeless, and the date is giving "not in this snippet but still legendary." πβ¨π
π¦ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- Copyright (c) 1993 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- Copyright (c) 2000 Ajuba Solutions
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms