| 1 |
'\" |
| 2 |
'\" Copyright (c) 1993 The Regents of the University of California. |
| 3 |
'\" Copyright (c) 1994-1996 Sun Microsystems, Inc. |
| 4 |
'\" Copyright (c) 2000 Ajuba Solutions. |
| 5 |
'\" |
| 6 |
'\" See the file "license.terms" for information on usage and redistribution |
| 7 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 8 |
'\" |
| 9 |
.TH trace n "8.4" Tcl "Tcl Built-In Commands" |
| 10 |
.so man.macros |
| 11 |
.BS |
| 12 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 13 |
.SH NAME |
| 14 |
trace \- Monitor variable accesses, command usages and command executions |
| 15 |
.SH SYNOPSIS |
| 16 |
\fBtrace \fIoption\fR ?\fIarg arg ...\fR? |
| 17 |
.BE |
| 18 |
.SH DESCRIPTION |
| 19 |
.PP |
| 20 |
This command causes Tcl commands to be executed whenever certain operations are |
| 21 |
invoked. The legal \fIoption\fRs (which may be abbreviated) are: |
| 22 |
.TP |
| 23 |
\fBtrace add \fItype name ops ?args?\fR |
| 24 |
Where \fItype\fR is \fBcommand\fR, \fBexecution\fR, or \fBvariable\fR. |
| 25 |
.RS |
| 26 |
.TP |
| 27 |
\fBtrace add command\fR \fIname ops commandPrefix\fR |
| 28 |
. |
| 29 |
Arrange for \fIcommandPrefix\fR to be executed (with additional arguments) |
| 30 |
whenever command \fIname\fR is modified in one of the ways given by the list |
| 31 |
\fIops\fR. \fIName\fR will be resolved using the usual namespace resolution |
| 32 |
rules used by commands. If the command does not exist, an error will be |
| 33 |
thrown. |
| 34 |
.RS |
| 35 |
.PP |
| 36 |
\fIOps\fR indicates which operations are of interest, and is a list of |
| 37 |
one or more of the following items: |
| 38 |
.TP |
| 39 |
\fBrename\fR |
| 40 |
. |
| 41 |
Invoke \fIcommandPrefix\fR whenever the traced command is renamed. Note that |
| 42 |
renaming to the empty string is considered deletion, and will not be traced |
| 43 |
with |
| 44 |
.QW \fBrename\fR . |
| 45 |
.TP |
| 46 |
\fBdelete\fR |
| 47 |
. |
| 48 |
Invoke \fIcommandPrefix\fR when the traced command is deleted. Commands can be |
| 49 |
deleted explicitly by using the \fBrename\fR command to rename the command to |
| 50 |
an empty string. Commands are also deleted when the interpreter is deleted, |
| 51 |
but traces will not be invoked because there is no interpreter in which to |
| 52 |
execute them. |
| 53 |
.PP |
| 54 |
When the trace triggers, depending on the operations being traced, a number of |
| 55 |
arguments are appended to \fIcommandPrefix\fR so that the actual command is as |
| 56 |
follows: |
| 57 |
.PP |
| 58 |
.CS |
| 59 |
\fIcommandPrefix oldName newName op\fR |
| 60 |
.CE |
| 61 |
.PP |
| 62 |
\fIOldName\fR and \fInewName\fR give the traced command's current (old) name, |
| 63 |
and the name to which it is being renamed (the empty string if this is a |
| 64 |
.QW delete |
| 65 |
operation). |
| 66 |
\fIOp\fR indicates what operation is being performed on the |
| 67 |
command, and is one of \fBrename\fR or \fBdelete\fR as |
| 68 |
defined above. The trace operation cannot be used to stop a command |
| 69 |
from being deleted. Tcl will always remove the command once the trace |
| 70 |
is complete. Recursive renaming or deleting will not cause further traces |
| 71 |
of the same type to be evaluated, so a delete trace which itself |
| 72 |
deletes the command, or a rename trace which itself renames the |
| 73 |
command will not cause further trace evaluations to occur. |
| 74 |
Both \fIoldName\fR and \fInewName\fR are fully qualified with any namespace(s) |
| 75 |
in which they appear. |
| 76 |
.RE |
| 77 |
.TP |
| 78 |
\fBtrace add execution\fR \fIname ops commandPrefix\fR |
| 79 |
. |
| 80 |
Arrange for \fIcommandPrefix\fR to be executed (with additional arguments) |
| 81 |
whenever command \fIname\fR is executed, with traces occurring at the points |
| 82 |
indicated by the list \fIops\fR. \fIName\fR will be resolved using the usual |
| 83 |
namespace resolution rules used by commands. If the command does not exist, |
| 84 |
an error will be thrown. |
| 85 |
.RS |
| 86 |
.PP |
| 87 |
\fIOps\fR indicates which operations are of interest, and is a list of |
| 88 |
one or more of the following items: |
| 89 |
.TP |
| 90 |
\fBenter\fR |
| 91 |
Invoke \fIcommandPrefix\fR whenever the command \fIname\fR is executed, |
| 92 |
just before the actual execution takes place. |
| 93 |
.TP |
| 94 |
\fBleave\fR |
| 95 |
Invoke \fIcommandPrefix\fR whenever the command \fIname\fR is executed, |
| 96 |
just after the actual execution takes place. |
| 97 |
.TP |
| 98 |
\fBenterstep\fR |
| 99 |
. |
| 100 |
Invoke \fIcommandPrefix\fR for every Tcl command which is executed from the |
| 101 |
start of the execution of the procedure \fIname\fR until that |
| 102 |
procedure finishes. \fICommandPrefix\fR is invoked just before the actual |
| 103 |
execution of the Tcl command being reported takes place. For example |
| 104 |
if we have |
| 105 |
.QW "proc foo {} { puts \N'34'hello\N'34' }" , |
| 106 |
then an \fIenterstep\fR trace would be invoked just before |
| 107 |
.QW "\fIputs \N'34'hello\N'34'\fR" |
| 108 |
is executed. |
| 109 |
Setting an \fIenterstep\fR trace on a command \fIname\fR that does not refer |
| 110 |
to a procedure will not result in an error and is simply ignored. |
| 111 |
.TP |
| 112 |
\fBleavestep\fR |
| 113 |
. |
| 114 |
Invoke \fIcommandPrefix\fR for every Tcl command which is executed from the |
| 115 |
start of the execution of the procedure \fIname\fR until that |
| 116 |
procedure finishes. \fICommandPrefix\fR is invoked just after the actual |
| 117 |
execution of the Tcl command being reported takes place. |
| 118 |
Setting a \fIleavestep\fR trace on a command \fIname\fR that does not refer to |
| 119 |
a procedure will not result in an error and is simply ignored. |
| 120 |
.PP |
| 121 |
When the trace triggers, depending on the operations being traced, a |
| 122 |
number of arguments are appended to \fIcommandPrefix\fR so that the actual |
| 123 |
command is as follows: |
| 124 |
.PP |
| 125 |
For \fBenter\fR and \fBenterstep\fR operations: |
| 126 |
.PP |
| 127 |
.CS |
| 128 |
\fIcommandPrefix command-string op\fR |
| 129 |
.CE |
| 130 |
.PP |
| 131 |
\fICommand-string\fR gives the complete current command being |
| 132 |
executed (the traced command for a \fBenter\fR operation, an |
| 133 |
arbitrary command for a \fBenterstep\fR operation), including |
| 134 |
all arguments in their fully expanded form. |
| 135 |
\fIOp\fR indicates what operation is being performed on the |
| 136 |
command execution, and is one of \fBenter\fR or \fBenterstep\fR as |
| 137 |
defined above. The trace operation can be used to stop the |
| 138 |
command from executing, by deleting the command in question. Of |
| 139 |
course when the command is subsequently executed, an |
| 140 |
.QW "invalid command" |
| 141 |
error will occur. |
| 142 |
.PP |
| 143 |
For \fBleave\fR and \fBleavestep\fR operations: |
| 144 |
.PP |
| 145 |
.CS |
| 146 |
\fIcommandPrefix command-string code result op\fR |
| 147 |
.CE |
| 148 |
.PP |
| 149 |
\fICommand-string\fR gives the complete current command being |
| 150 |
executed (the traced command for a \fBenter\fR operation, an |
| 151 |
arbitrary command for a \fBenterstep\fR operation), including |
| 152 |
all arguments in their fully expanded form. |
| 153 |
\fICode\fR gives the result code of that execution, and \fIresult\fR |
| 154 |
the result string. |
| 155 |
\fIOp\fR indicates what operation is being performed on the |
| 156 |
command execution, and is one of \fBleave\fR or \fBleavestep\fR as |
| 157 |
defined above. |
| 158 |
Note that the creation of many \fBenterstep\fR or |
| 159 |
\fBleavestep\fR traces can lead to unintuitive results, since the |
| 160 |
invoked commands from one trace can themselves lead to further |
| 161 |
command invocations for other traces. |
| 162 |
.PP |
| 163 |
\fICommandPrefix\fR executes in the same context as the code that invoked |
| 164 |
the traced operation: thus the \fIcommandPrefix\fR, if invoked from a |
| 165 |
procedure, will have access to the same local variables as code in the |
| 166 |
procedure. This context may be different than the context in which the trace |
| 167 |
was created. If \fIcommandPrefix\fR invokes a procedure (which it normally |
| 168 |
does) then the procedure will have to use \fBupvar\fR or \fBuplevel\fR |
| 169 |
commands if it wishes to access the local variables of the code which invoked |
| 170 |
the trace operation. |
| 171 |
.PP |
| 172 |
While \fIcommandPrefix\fR is executing during an execution trace, traces |
| 173 |
on \fIname\fR are temporarily disabled. This allows the \fIcommandPrefix\fR |
| 174 |
to execute \fIname\fR in its body without invoking any other traces again. |
| 175 |
If an error occurs while executing the \fIcommandPrefix\fR, then the |
| 176 |
command \fIname\fR as a whole will return that same error. |
| 177 |
.PP |
| 178 |
When multiple traces are set on \fIname\fR, then for \fIenter\fR |
| 179 |
and \fIenterstep\fR operations, the traced commands are invoked |
| 180 |
in the reverse order of how the traces were originally created; |
| 181 |
and for \fIleave\fR and \fIleavestep\fR operations, the traced |
| 182 |
commands are invoked in the original order of creation. |
| 183 |
.PP |
| 184 |
The behavior of execution traces is currently undefined for a command |
| 185 |
\fIname\fR imported into another namespace. |
| 186 |
.RE |
| 187 |
.TP |
| 188 |
\fBtrace add variable\fI name ops commandPrefix\fR |
| 189 |
Arrange for \fIcommandPrefix\fR to be executed whenever variable \fIname\fR |
| 190 |
is accessed in one of the ways given by the list \fIops\fR. \fIName\fR may |
| 191 |
refer to a normal variable, an element of an array, or to an array |
| 192 |
as a whole (i.e. \fIname\fR may be just the name of an array, with no |
| 193 |
parenthesized index). If \fIname\fR refers to a whole array, then |
| 194 |
\fIcommandPrefix\fR is invoked whenever any element of the array is |
| 195 |
manipulated. If the variable does not exist, it will be created but |
| 196 |
will not be given a value, so it will be visible to \fBnamespace which\fR |
| 197 |
queries, but not to \fBinfo exists\fR queries. |
| 198 |
.RS |
| 199 |
.PP |
| 200 |
\fIOps\fR indicates which operations are of interest, and is a list of |
| 201 |
one or more of the following items: |
| 202 |
.TP |
| 203 |
\fBarray\fR |
| 204 |
Invoke \fIcommandPrefix\fR whenever the variable is accessed or modified via |
| 205 |
the \fBarray\fR command, provided that \fIname\fR is not a scalar |
| 206 |
variable at the time that the \fBarray\fR command is invoked. If |
| 207 |
\fIname\fR is a scalar variable, the access via the \fBarray\fR |
| 208 |
command will not trigger the trace. |
| 209 |
.TP |
| 210 |
\fBread\fR |
| 211 |
Invoke \fIcommandPrefix\fR whenever the variable is read. |
| 212 |
.TP |
| 213 |
\fBwrite\fR |
| 214 |
Invoke \fIcommandPrefix\fR whenever the variable is written. |
| 215 |
.TP |
| 216 |
\fBunset\fR |
| 217 |
Invoke \fIcommandPrefix\fR whenever the variable is unset. Variables |
| 218 |
can be unset explicitly with the \fBunset\fR command, or |
| 219 |
implicitly when procedures return (all of their local variables |
| 220 |
are unset). Variables are also unset when interpreters are |
| 221 |
deleted, but traces will not be invoked because there is no |
| 222 |
interpreter in which to execute them. |
| 223 |
.PP |
| 224 |
When the trace triggers, three arguments are appended to |
| 225 |
\fIcommandPrefix\fR so that the actual command is as follows: |
| 226 |
.PP |
| 227 |
.CS |
| 228 |
\fIcommandPrefix name1 name2 op\fR |
| 229 |
.CE |
| 230 |
.PP |
| 231 |
\fIName1\fR and \fIname2\fR give the name(s) for the variable |
| 232 |
being accessed: if the variable is a scalar then \fIname1\fR |
| 233 |
gives the variable's name and \fIname2\fR is an empty string; |
| 234 |
if the variable is an array element then \fIname1\fR gives the |
| 235 |
name of the array and name2 gives the index into the array; |
| 236 |
if an entire array is being deleted and the trace was registered |
| 237 |
on the overall array, rather than a single element, then \fIname1\fR |
| 238 |
gives the array name and \fIname2\fR is an empty string. |
| 239 |
\fIName1\fR and \fIname2\fR are not necessarily the same as the |
| 240 |
name used in the \fBtrace variable\fR command: the \fBupvar\fR |
| 241 |
command allows a procedure to reference a variable under a |
| 242 |
different name. |
| 243 |
\fIOp\fR indicates what operation is being performed on the |
| 244 |
variable, and is one of \fBread\fR, \fBwrite\fR, or \fBunset\fR as |
| 245 |
defined above. |
| 246 |
.PP |
| 247 |
\fICommandPrefix\fR executes in the same context as the code that invoked |
| 248 |
the traced operation: if the variable was accessed as part of a Tcl |
| 249 |
procedure, then \fIcommandPrefix\fR will have access to the same local |
| 250 |
variables as code in the procedure. This context may be different |
| 251 |
than the context in which the trace was created. If \fIcommandPrefix\fR |
| 252 |
invokes a procedure (which it normally does) then the procedure will |
| 253 |
have to use \fBupvar\fR or \fBuplevel\fR if it wishes to access the |
| 254 |
traced variable. Note also that \fIname1\fR may not necessarily be |
| 255 |
the same as the name used to set the trace on the variable; |
| 256 |
differences can occur if the access is made through a variable defined |
| 257 |
with the \fBupvar\fR command. |
| 258 |
.PP |
| 259 |
For read and write traces, \fIcommandPrefix\fR can modify the variable to |
| 260 |
affect the result of the traced operation. If \fIcommandPrefix\fR modifies |
| 261 |
the value of a variable during a read or write trace, then the new |
| 262 |
value will be returned as the result of the traced operation. The |
| 263 |
return value from \fIcommandPrefix\fR is ignored except that if it returns |
| 264 |
an error of any sort then the traced operation also returns an error |
| 265 |
with the same error message returned by the trace command (this |
| 266 |
mechanism can be used to implement read-only variables, for example). |
| 267 |
For write traces, \fIcommandPrefix\fR is invoked after the variable's value |
| 268 |
has been changed; it can write a new value into the variable to |
| 269 |
override the original value specified in the write operation. To |
| 270 |
implement read-only variables, \fIcommandPrefix\fR will have to restore the |
| 271 |
old value of the variable. |
| 272 |
.PP |
| 273 |
While \fIcommandPrefix\fR is executing during a read or write trace, traces |
| 274 |
on the variable are temporarily disabled. This means that reads and |
| 275 |
writes invoked by \fIcommandPrefix\fR will occur directly, without invoking |
| 276 |
\fIcommandPrefix\fR (or any other traces) again. However, if |
| 277 |
\fIcommandPrefix\fR unsets the variable then unset traces will be invoked. |
| 278 |
.PP |
| 279 |
When an unset trace is invoked, the variable has already been deleted: |
| 280 |
it will appear to be undefined with no traces. If an unset occurs |
| 281 |
because of a procedure return, then the trace will be invoked in the |
| 282 |
variable context of the procedure being returned to: the stack frame |
| 283 |
of the returning procedure will no longer exist. Traces are not |
| 284 |
disabled during unset traces, so if an unset trace command creates a |
| 285 |
new trace and accesses the variable, the trace will be invoked. Any |
| 286 |
errors in unset traces are ignored. |
| 287 |
.PP |
| 288 |
If there are multiple traces on a variable they are invoked in order |
| 289 |
of creation, most-recent first. If one trace returns an error, then |
| 290 |
no further traces are invoked for the variable. If an array element |
| 291 |
has a trace set, and there is also a trace set on the array as a |
| 292 |
whole, the trace on the overall array is invoked before the one on the |
| 293 |
element. |
| 294 |
.PP |
| 295 |
Once created, the trace remains in effect either until the trace is |
| 296 |
removed with the \fBtrace remove variable\fR command described below, |
| 297 |
until the variable is unset, or until the interpreter is deleted. |
| 298 |
Unsetting an element of array will remove any traces on that element, |
| 299 |
but will not remove traces on the overall array. |
| 300 |
.PP |
| 301 |
This command returns an empty string. |
| 302 |
.RE |
| 303 |
.RE |
| 304 |
.TP |
| 305 |
\fBtrace remove \fItype name opList commandPrefix\fR |
| 306 |
Where \fItype\fR is either \fBcommand\fR, \fBexecution\fR or \fBvariable\fR. |
| 307 |
.RS |
| 308 |
.TP |
| 309 |
\fBtrace remove command\fI name opList commandPrefix\fR |
| 310 |
If there is a trace set on command \fIname\fR with the operations and |
| 311 |
command given by \fIopList\fR and \fIcommandPrefix\fR, then the trace is |
| 312 |
removed, so that \fIcommandPrefix\fR will never again be invoked. Returns |
| 313 |
an empty string. If \fIname\fR does not exist, the command will throw |
| 314 |
an error. |
| 315 |
.TP |
| 316 |
\fBtrace remove execution\fI name opList commandPrefix\fR |
| 317 |
If there is a trace set on command \fIname\fR with the operations and |
| 318 |
command given by \fIopList\fR and \fIcommandPrefix\fR, then the trace is |
| 319 |
removed, so that \fIcommandPrefix\fR will never again be invoked. Returns |
| 320 |
an empty string. If \fIname\fR does not exist, the command will throw |
| 321 |
an error. |
| 322 |
.TP |
| 323 |
\fBtrace remove variable\fI name opList commandPrefix\fR |
| 324 |
If there is a trace set on variable \fIname\fR with the operations and |
| 325 |
command given by \fIopList\fR and \fIcommandPrefix\fR, then the trace is |
| 326 |
removed, so that \fIcommandPrefix\fR will never again be invoked. Returns |
| 327 |
an empty string. |
| 328 |
.RE |
| 329 |
.TP |
| 330 |
\fBtrace info \fItype name\fR |
| 331 |
Where \fItype\fR is either \fBcommand\fR, \fBexecution\fR or \fBvariable\fR. |
| 332 |
.RS |
| 333 |
.TP |
| 334 |
\fBtrace info command\fI name\fR |
| 335 |
Returns a list containing one element for each trace currently set on |
| 336 |
command \fIname\fR. Each element of the list is itself a list |
| 337 |
containing two elements, which are the \fIopList\fR and \fIcommandPrefix\fR |
| 338 |
associated with the trace. If \fIname\fR does not have any traces set, |
| 339 |
then the result of the command will be an empty string. If \fIname\fR |
| 340 |
does not exist, the command will throw an error. |
| 341 |
.TP |
| 342 |
\fBtrace info execution\fI name\fR |
| 343 |
Returns a list containing one element for each trace currently set on |
| 344 |
command \fIname\fR. Each element of the list is itself a list |
| 345 |
containing two elements, which are the \fIopList\fR and \fIcommandPrefix\fR |
| 346 |
associated with the trace. If \fIname\fR does not have any traces set, |
| 347 |
then the result of the command will be an empty string. If \fIname\fR |
| 348 |
does not exist, the command will throw an error. |
| 349 |
.TP |
| 350 |
\fBtrace info variable\fI name\fR |
| 351 |
Returns a list containing one element for each trace currently set on |
| 352 |
variable \fIname\fR. Each element of the list is itself a list |
| 353 |
containing two elements, which are the \fIopList\fR and \fIcommandPrefix\fR |
| 354 |
associated with the trace. If \fIname\fR does not exist or does not |
| 355 |
have any traces set, then the result of the command will be an empty |
| 356 |
string. |
| 357 |
.RE |
| 358 |
.PP |
| 359 |
For backwards compatibility, three other subcommands are available: |
| 360 |
.RS |
| 361 |
.TP |
| 362 |
\fBtrace variable \fIname ops command\fR |
| 363 |
This is equivalent to \fBtrace add variable \fIname ops command\fR. |
| 364 |
.TP |
| 365 |
\fBtrace vdelete \fIname ops command\fR |
| 366 |
This is equivalent to \fBtrace remove variable \fIname ops command\fR |
| 367 |
.TP |
| 368 |
\fBtrace vinfo \fIname\fR |
| 369 |
This is equivalent to \fBtrace info variable \fIname\fR |
| 370 |
.RE |
| 371 |
.PP |
| 372 |
These subcommands are deprecated and will likely be removed in a |
| 373 |
future version of Tcl. They use an older syntax in which \fBarray\fR, |
| 374 |
\fBread\fR, \fBwrite\fR, \fBunset\fR are replaced by \fBa\fR, \fBr\fR, |
| 375 |
\fBw\fR and \fBu\fR respectively, and the \fIops\fR argument is not a |
| 376 |
list, but simply a string concatenation of the operations, such as |
| 377 |
\fBrwua\fR. |
| 378 |
.SH EXAMPLES |
| 379 |
.PP |
| 380 |
Print a message whenever either of the global variables \fBfoo\fR and |
| 381 |
\fBbar\fR are updated, even if they have a different local name at the |
| 382 |
time (which can be done with the \fBupvar\fR command): |
| 383 |
.PP |
| 384 |
.CS |
| 385 |
proc tracer {varname args} { |
| 386 |
upvar #0 $varname var |
| 387 |
puts "$varname was updated to be \e"$var\e"" |
| 388 |
} |
| 389 |
\fBtrace add\fR variable foo write "tracer foo" |
| 390 |
\fBtrace add\fR variable bar write "tracer bar" |
| 391 |
.CE |
| 392 |
.PP |
| 393 |
Ensure that the global variable \fBfoobar\fR always contains the |
| 394 |
product of the global variables \fBfoo\fR and \fBbar\fR: |
| 395 |
.PP |
| 396 |
.CS |
| 397 |
proc doMult args { |
| 398 |
global foo bar foobar |
| 399 |
set foobar [expr {$foo * $bar}] |
| 400 |
} |
| 401 |
\fBtrace add\fR variable foo write doMult |
| 402 |
\fBtrace add\fR variable bar write doMult |
| 403 |
.CE |
| 404 |
.PP |
| 405 |
Print a trace of what commands are executed during the processing of a Tcl |
| 406 |
procedure: |
| 407 |
.PP |
| 408 |
.CS |
| 409 |
proc x {} { y } |
| 410 |
proc y {} { z } |
| 411 |
proc z {} { puts hello } |
| 412 |
proc report args {puts [info level 0]} |
| 413 |
\fBtrace add\fR execution x enterstep report |
| 414 |
x |
| 415 |
\(-> \fIreport y enterstep\fR |
| 416 |
\fIreport z enterstep\fR |
| 417 |
\fIreport {puts hello} enterstep\fR |
| 418 |
\fIhello\fR |
| 419 |
.CE |
| 420 |
.SH "SEE ALSO" |
| 421 |
set(n), unset(n) |
| 422 |
.SH KEYWORDS |
| 423 |
read, command, rename, variable, write, trace, unset |
| 424 |
.\" Local Variables: |
| 425 |
.\" mode: nroff |
| 426 |
.\" End: |