| 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 | '\" RCS: @(#) $Id: trace.n,v 1.13.2.2 2004/10/27 14:43:14 dkf Exp $ |
| 10 | '\" |
| 11 | '\" The definitions below are for supplemental macros used in Tcl/Tk |
| 12 | '\" manual entries. |
| 13 | '\" |
| 14 | '\" .AP type name in/out ?indent? |
| 15 | '\" Start paragraph describing an argument to a library procedure. |
| 16 | '\" type is type of argument (int, etc.), in/out is either "in", "out", |
| 17 | '\" or "in/out" to describe whether procedure reads or modifies arg, |
| 18 | '\" and indent is equivalent to second arg of .IP (shouldn't ever be |
| 19 | '\" needed; use .AS below instead) |
| 20 | '\" |
| 21 | '\" .AS ?type? ?name? |
| 22 | '\" Give maximum sizes of arguments for setting tab stops. Type and |
| 23 | '\" name are examples of largest possible arguments that will be passed |
| 24 | '\" to .AP later. If args are omitted, default tab stops are used. |
| 25 | '\" |
| 26 | '\" .BS |
| 27 | '\" Start box enclosure. From here until next .BE, everything will be |
| 28 | '\" enclosed in one large box. |
| 29 | '\" |
| 30 | '\" .BE |
| 31 | '\" End of box enclosure. |
| 32 | '\" |
| 33 | '\" .CS |
| 34 | '\" Begin code excerpt. |
| 35 | '\" |
| 36 | '\" .CE |
| 37 | '\" End code excerpt. |
| 38 | '\" |
| 39 | '\" .VS ?version? ?br? |
| 40 | '\" Begin vertical sidebar, for use in marking newly-changed parts |
| 41 | '\" of man pages. The first argument is ignored and used for recording |
| 42 | '\" the version when the .VS was added, so that the sidebars can be |
| 43 | '\" found and removed when they reach a certain age. If another argument |
| 44 | '\" is present, then a line break is forced before starting the sidebar. |
| 45 | '\" |
| 46 | '\" .VE |
| 47 | '\" End of vertical sidebar. |
| 48 | '\" |
| 49 | '\" .DS |
| 50 | '\" Begin an indented unfilled display. |
| 51 | '\" |
| 52 | '\" .DE |
| 53 | '\" End of indented unfilled display. |
| 54 | '\" |
| 55 | '\" .SO |
| 56 | '\" Start of list of standard options for a Tk widget. The |
| 57 | '\" options follow on successive lines, in four columns separated |
| 58 | '\" by tabs. |
| 59 | '\" |
| 60 | '\" .SE |
| 61 | '\" End of list of standard options for a Tk widget. |
| 62 | '\" |
| 63 | '\" .OP cmdName dbName dbClass |
| 64 | '\" Start of description of a specific option. cmdName gives the |
| 65 | '\" option's name as specified in the class command, dbName gives |
| 66 | '\" the option's name in the option database, and dbClass gives |
| 67 | '\" the option's class in the option database. |
| 68 | '\" |
| 69 | '\" .UL arg1 arg2 |
| 70 | '\" Print arg1 underlined, then print arg2 normally. |
| 71 | '\" |
| 72 | '\" RCS: @(#) $Id: man.macros,v 1.4 2000/08/25 06:18:32 ericm Exp $ |
| 73 | '\" |
| 74 | '\" # Set up traps and other miscellaneous stuff for Tcl/Tk man pages. |
| 75 | .if t .wh -1.3i ^B |
| 76 | .nr ^l \n(.l |
| 77 | .ad b |
| 78 | '\" # Start an argument description |
| 79 | .de AP |
| 80 | .ie !"\\$4"" .TP \\$4 |
| 81 | .el \{\ |
| 82 | . ie !"\\$2"" .TP \\n()Cu |
| 83 | . el .TP 15 |
| 84 | .\} |
| 85 | .ta \\n()Au \\n()Bu |
| 86 | .ie !"\\$3"" \{\ |
| 87 | \&\\$1 \\fI\\$2\\fP (\\$3) |
| 88 | .\".b |
| 89 | .\} |
| 90 | .el \{\ |
| 91 | .br |
| 92 | .ie !"\\$2"" \{\ |
| 93 | \&\\$1 \\fI\\$2\\fP |
| 94 | .\} |
| 95 | .el \{\ |
| 96 | \&\\fI\\$1\\fP |
| 97 | .\} |
| 98 | .\} |
| 99 | .. |
| 100 | '\" # define tabbing values for .AP |
| 101 | .de AS |
| 102 | .nr )A 10n |
| 103 | .if !"\\$1"" .nr )A \\w'\\$1'u+3n |
| 104 | .nr )B \\n()Au+15n |
| 105 | .\" |
| 106 | .if !"\\$2"" .nr )B \\w'\\$2'u+\\n()Au+3n |
| 107 | .nr )C \\n()Bu+\\w'(in/out)'u+2n |
| 108 | .. |
| 109 | .AS Tcl_Interp Tcl_CreateInterp in/out |
| 110 | '\" # BS - start boxed text |
| 111 | '\" # ^y = starting y location |
| 112 | '\" # ^b = 1 |
| 113 | .de BS |
| 114 | .br |
| 115 | .mk ^y |
| 116 | .nr ^b 1u |
| 117 | .if n .nf |
| 118 | .if n .ti 0 |
| 119 | .if n \l'\\n(.lu\(ul' |
| 120 | .if n .fi |
| 121 | .. |
| 122 | '\" # BE - end boxed text (draw box now) |
| 123 | .de BE |
| 124 | .nf |
| 125 | .ti 0 |
| 126 | .mk ^t |
| 127 | .ie n \l'\\n(^lu\(ul' |
| 128 | .el \{\ |
| 129 | .\" Draw four-sided box normally, but don't draw top of |
| 130 | .\" box if the box started on an earlier page. |
| 131 | .ie !\\n(^b-1 \{\ |
| 132 | \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul' |
| 133 | .\} |
| 134 | .el \}\ |
| 135 | \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul' |
| 136 | .\} |
| 137 | .\} |
| 138 | .fi |
| 139 | .br |
| 140 | .nr ^b 0 |
| 141 | .. |
| 142 | '\" # VS - start vertical sidebar |
| 143 | '\" # ^Y = starting y location |
| 144 | '\" # ^v = 1 (for troff; for nroff this doesn't matter) |
| 145 | .de VS |
| 146 | .if !"\\$2"" .br |
| 147 | .mk ^Y |
| 148 | .ie n 'mc \s12\(br\s0 |
| 149 | .el .nr ^v 1u |
| 150 | .. |
| 151 | '\" # VE - end of vertical sidebar |
| 152 | .de VE |
| 153 | .ie n 'mc |
| 154 | .el \{\ |
| 155 | .ev 2 |
| 156 | .nf |
| 157 | .ti 0 |
| 158 | .mk ^t |
| 159 | \h'|\\n(^lu+3n'\L'|\\n(^Yu-1v\(bv'\v'\\n(^tu+1v-\\n(^Yu'\h'-|\\n(^lu+3n' |
| 160 | .sp -1 |
| 161 | .fi |
| 162 | .ev |
| 163 | .\} |
| 164 | .nr ^v 0 |
| 165 | .. |
| 166 | '\" # Special macro to handle page bottom: finish off current |
| 167 | '\" # box/sidebar if in box/sidebar mode, then invoked standard |
| 168 | '\" # page bottom macro. |
| 169 | .de ^B |
| 170 | .ev 2 |
| 171 | 'ti 0 |
| 172 | 'nf |
| 173 | .mk ^t |
| 174 | .if \\n(^b \{\ |
| 175 | .\" Draw three-sided box if this is the box's first page, |
| 176 | .\" draw two sides but no top otherwise. |
| 177 | .ie !\\n(^b-1 \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c |
| 178 | .el \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c |
| 179 | .\} |
| 180 | .if \\n(^v \{\ |
| 181 | .nr ^x \\n(^tu+1v-\\n(^Yu |
| 182 | \kx\h'-\\nxu'\h'|\\n(^lu+3n'\ky\L'-\\n(^xu'\v'\\n(^xu'\h'|0u'\c |
| 183 | .\} |
| 184 | .bp |
| 185 | 'fi |
| 186 | .ev |
| 187 | .if \\n(^b \{\ |
| 188 | .mk ^y |
| 189 | .nr ^b 2 |
| 190 | .\} |
| 191 | .if \\n(^v \{\ |
| 192 | .mk ^Y |
| 193 | .\} |
| 194 | .. |
| 195 | '\" # DS - begin display |
| 196 | .de DS |
| 197 | .RS |
| 198 | .nf |
| 199 | .sp |
| 200 | .. |
| 201 | '\" # DE - end display |
| 202 | .de DE |
| 203 | .fi |
| 204 | .RE |
| 205 | .sp |
| 206 | .. |
| 207 | '\" # SO - start of list of standard options |
| 208 | .de SO |
| 209 | .SH "STANDARD OPTIONS" |
| 210 | .LP |
| 211 | .nf |
| 212 | .ta 5.5c 11c |
| 213 | .ft B |
| 214 | .. |
| 215 | '\" # SE - end of list of standard options |
| 216 | .de SE |
| 217 | .fi |
| 218 | .ft R |
| 219 | .LP |
| 220 | See the \\fBoptions\\fR manual entry for details on the standard options. |
| 221 | .. |
| 222 | '\" # OP - start of full description for a single option |
| 223 | .de OP |
| 224 | .LP |
| 225 | .nf |
| 226 | .ta 4c |
| 227 | Command-Line Name: \\fB\\$1\\fR |
| 228 | Database Name: \\fB\\$2\\fR |
| 229 | Database Class: \\fB\\$3\\fR |
| 230 | .fi |
| 231 | .IP |
| 232 | .. |
| 233 | '\" # CS - begin code excerpt |
| 234 | .de CS |
| 235 | .RS |
| 236 | .nf |
| 237 | .ta .25i .5i .75i 1i |
| 238 | .. |
| 239 | '\" # CE - end code excerpt |
| 240 | .de CE |
| 241 | .fi |
| 242 | .RE |
| 243 | .. |
| 244 | .de UL |
| 245 | \\$1\l'|0\(ul'\\$2 |
| 246 | .. |
| 247 | .TH trace n "8.4" Tcl "Tcl Built-In Commands" |
| 248 | .BS |
| 249 | '\" Note: do not modify the .SH NAME line immediately below! |
| 250 | .SH NAME |
| 251 | trace \- Monitor variable accesses, command usages and command executions |
| 252 | .SH SYNOPSIS |
| 253 | \fBtrace \fIoption\fR ?\fIarg arg ...\fR? |
| 254 | .BE |
| 255 | |
| 256 | .SH DESCRIPTION |
| 257 | .PP |
| 258 | This command causes Tcl commands to be executed whenever certain operations are |
| 259 | invoked. The legal \fIoption\fR's (which may be abbreviated) are: |
| 260 | .TP |
| 261 | \fBtrace add \fItype name ops ?args?\fR |
| 262 | Where \fItype\fR is \fBcommand\fR, \fBexecution\fR, or \fBvariable\fR. |
| 263 | .RS |
| 264 | .TP |
| 265 | \fBtrace add command\fR \fIname ops command\fR |
| 266 | Arrange for \fIcommand\fR to be executed whenever command \fIname\fR |
| 267 | is modified in one of the ways given by the list \fIops\fR. \fIName\fR will be |
| 268 | resolved using the usual namespace resolution rules used by |
| 269 | procedures. If the command does not exist, an error will be thrown. |
| 270 | .RS |
| 271 | .PP |
| 272 | \fIOps\fR indicates which operations are of interest, and is a list of |
| 273 | one or more of the following items: |
| 274 | .TP |
| 275 | \fBrename\fR |
| 276 | Invoke \fIcommand\fR whenever the command is renamed. Note that |
| 277 | renaming to the empty string is considered deletion, and will not |
| 278 | be traced with '\fBrename\fR'. |
| 279 | .TP |
| 280 | \fBdelete\fR |
| 281 | Invoke \fIcommand\fR when the command is deleted. Commands can be |
| 282 | deleted explicitly by using the \fBrename\fR command to rename the |
| 283 | command to an empty string. Commands are also deleted when the |
| 284 | interpreter is deleted, but traces will not be invoked because there is no |
| 285 | interpreter in which to execute them. |
| 286 | .PP |
| 287 | When the trace triggers, depending on the operations being traced, a |
| 288 | number of arguments are appended to \fIcommand\fR so that the actual |
| 289 | command is as follows: |
| 290 | .CS |
| 291 | \fIcommand oldName newName op\fR |
| 292 | .CE |
| 293 | \fIOldName\fR and \fInewName\fR give the traced command's current |
| 294 | (old) name, and the name to which it is being renamed (the empty |
| 295 | string if this is a 'delete' operation). |
| 296 | \fIOp\fR indicates what operation is being performed on the |
| 297 | command, and is one of \fBrename\fR or \fBdelete\fR as |
| 298 | defined above. The trace operation cannot be used to stop a command |
| 299 | from being deleted. Tcl will always remove the command once the trace |
| 300 | is complete. Recursive renaming or deleting will not cause further traces |
| 301 | of the same type to be evaluated, so a delete trace which itself |
| 302 | deletes the command, or a rename trace which itself renames the |
| 303 | command will not cause further trace evaluations to occur. |
| 304 | Both \fIoldName\fR and \fInewName\fR are fully qualified with any namespace(s) |
| 305 | in which they appear. |
| 306 | .RE |
| 307 | .TP |
| 308 | \fBtrace add execution\fR \fIname ops command\fR |
| 309 | Arrange for \fIcommand\fR to be executed whenever command \fIname\fR |
| 310 | is executed, with traces occurring at the points indicated by the list |
| 311 | \fIops\fR. \fIName\fR will be |
| 312 | resolved using the usual namespace resolution rules used by |
| 313 | procedures. If the command does not exist, an error will be thrown. |
| 314 | .RS |
| 315 | .PP |
| 316 | \fIOps\fR indicates which operations are of interest, and is a list of |
| 317 | one or more of the following items: |
| 318 | .TP |
| 319 | \fBenter\fR |
| 320 | Invoke \fIcommand\fR whenever the command \fIname\fR is executed, |
| 321 | just before the actual execution takes place. |
| 322 | .TP |
| 323 | \fBleave\fR |
| 324 | Invoke \fIcommand\fR whenever the command \fIname\fR is executed, |
| 325 | just after the actual execution takes place. |
| 326 | .TP |
| 327 | \fBenterstep\fR |
| 328 | Invoke \fIcommand\fR for every Tcl command which is executed |
| 329 | inside the procedure \fIname\fR, just before the actual execution |
| 330 | takes place. For example if we have 'proc foo {} { puts "hello" }', |
| 331 | then an \fIenterstep\fR trace would be |
| 332 | invoked just before \fIputs "hello"\fR is executed. |
| 333 | Setting an \fIenterstep\fR trace on a \fIcommand\fR |
| 334 | will not result in an error and is simply ignored. |
| 335 | .TP |
| 336 | \fBleavestep\fR |
| 337 | Invoke \fIcommand\fR for every Tcl command which is executed |
| 338 | inside the procedure \fIname\fR, just after the actual execution |
| 339 | takes place. |
| 340 | Setting a \fIleavestep\fR trace on a \fIcommand\fR |
| 341 | will not result in an error and is simply ignored. |
| 342 | .PP |
| 343 | When the trace triggers, depending on the operations being traced, a |
| 344 | number of arguments are appended to \fIcommand\fR so that the actual |
| 345 | command is as follows: |
| 346 | .PP |
| 347 | For \fBenter\fR and \fBenterstep\fR operations: |
| 348 | .CS |
| 349 | \fIcommand command-string op\fR |
| 350 | .CE |
| 351 | \fICommand-string\fR gives the complete current command being |
| 352 | executed (the traced command for a \fBenter\fR operation, an |
| 353 | arbitrary command for a \fBenterstep\fR operation), including |
| 354 | all arguments in their fully expanded form. |
| 355 | \fIOp\fR indicates what operation is being performed on the |
| 356 | command execution, and is one of \fBenter\fR or \fBenterstep\fR as |
| 357 | defined above. The trace operation can be used to stop the |
| 358 | command from executing, by deleting the command in question. Of |
| 359 | course when the command is subsequently executed, an 'invalid command' |
| 360 | error will occur. |
| 361 | .PP |
| 362 | For \fBleave\fR and \fBleavestep\fR operations: |
| 363 | .CS |
| 364 | \fIcommand command-string code result op\fR |
| 365 | .CE |
| 366 | \fICommand-string\fR gives the complete current command being |
| 367 | executed (the traced command for a \fBenter\fR operation, an |
| 368 | arbitrary command for a \fBenterstep\fR operation), including |
| 369 | all arguments in their fully expanded form. |
| 370 | \fICode\fR gives the result code of that execution, and \fIresult\fR |
| 371 | the result string. |
| 372 | \fIOp\fR indicates what operation is being performed on the |
| 373 | command execution, and is one of \fBleave\fR or \fBleavestep\fR as |
| 374 | defined above. |
| 375 | Note that the creation of many \fBenterstep\fR or |
| 376 | \fBleavestep\fR traces can lead to unintuitive results, since the |
| 377 | invoked commands from one trace can themselves lead to further |
| 378 | command invocations for other traces. |
| 379 | .PP |
| 380 | \fICommand\fR executes in the same context as the code that invoked |
| 381 | the traced operation: thus the \fIcommand\fR, if invoked from a procedure, |
| 382 | will have access to the same local variables as code in the procedure. |
| 383 | This context may be different than the context in which the trace was |
| 384 | created. If \fIcommand\fR invokes a procedure (which it normally does) |
| 385 | then the procedure will have to use upvar or uplevel commands if it wishes |
| 386 | to access the local variables of the code which invoked the trace operation. |
| 387 | .PP |
| 388 | While \fIcommand\fR is executing during an execution trace, traces |
| 389 | on \fIname\fR are temporarily disabled. This allows the \fIcommand\fR |
| 390 | to execute \fIname\fR in its body without invoking any other traces again. |
| 391 | If an error occurs while executing the \fIcommand\fR body, then the |
| 392 | command \fIname\fR as a whole will return that same error. |
| 393 | .PP |
| 394 | When multiple traces are set on \fIname\fR, then for \fIenter\fR |
| 395 | and \fIenterstep\fR operations, the traced commands are invoked |
| 396 | in the reverse order of how the traces were originally created; |
| 397 | and for \fIleave\fR and \fIleavestep\fR operations, the traced |
| 398 | commands are invoked in the original order of creation. |
| 399 | .PP |
| 400 | The behavior of execution traces is currently undefined for a command |
| 401 | \fIname\fR imported into another namespace. |
| 402 | .RE |
| 403 | .TP |
| 404 | \fBtrace add variable\fI name ops command\fR |
| 405 | Arrange for \fIcommand\fR to be executed whenever variable \fIname\fR |
| 406 | is accessed in one of the ways given by the list \fIops\fR. \fIName\fR may |
| 407 | refer to a normal variable, an element of an array, or to an array |
| 408 | as a whole (i.e. \fIname\fR may be just the name of an array, with no |
| 409 | parenthesized index). If \fIname\fR refers to a whole array, then |
| 410 | \fIcommand\fR is invoked whenever any element of the array is |
| 411 | manipulated. If the variable does not exist, it will be created but |
| 412 | will not be given a value, so it will be visible to \fBnamespace which\fR |
| 413 | queries, but not to \fBinfo exists\fR queries. |
| 414 | .RS |
| 415 | .PP |
| 416 | \fIOps\fR indicates which operations are of interest, and is a list of |
| 417 | one or more of the following items: |
| 418 | .TP |
| 419 | \fBarray\fR |
| 420 | Invoke \fIcommand\fR whenever the variable is accessed or modified via |
| 421 | the \fBarray\fR command, provided that \fIname\fR is not a scalar |
| 422 | variable at the time that the \fBarray\fR command is invoked. If |
| 423 | \fIname\fR is a scalar variable, the access via the \fBarray\fR |
| 424 | command will not trigger the trace. |
| 425 | .TP |
| 426 | \fBread\fR |
| 427 | Invoke \fIcommand\fR whenever the variable is read. |
| 428 | .TP |
| 429 | \fBwrite\fR |
| 430 | Invoke \fIcommand\fR whenever the variable is written. |
| 431 | .TP |
| 432 | \fBunset\fR |
| 433 | Invoke \fIcommand\fR whenever the variable is unset. Variables |
| 434 | can be unset explicitly with the \fBunset\fR command, or |
| 435 | implicitly when procedures return (all of their local variables |
| 436 | are unset). Variables are also unset when interpreters are |
| 437 | deleted, but traces will not be invoked because there is no |
| 438 | interpreter in which to execute them. |
| 439 | .PP |
| 440 | When the trace triggers, three arguments are appended to |
| 441 | \fIcommand\fR so that the actual command is as follows: |
| 442 | .CS |
| 443 | \fIcommand name1 name2 op\fR |
| 444 | .CE |
| 445 | \fIName1\fR and \fIname2\fR give the name(s) for the variable |
| 446 | being accessed: if the variable is a scalar then \fIname1\fR |
| 447 | gives the variable's name and \fIname2\fR is an empty string; |
| 448 | if the variable is an array element then \fIname1\fR gives the |
| 449 | name of the array and name2 gives the index into the array; |
| 450 | if an entire array is being deleted and the trace was registered |
| 451 | on the overall array, rather than a single element, then \fIname1\fR |
| 452 | gives the array name and \fIname2\fR is an empty string. |
| 453 | \fIName1\fR and \fIname2\fR are not necessarily the same as the |
| 454 | name used in the \fBtrace variable\fR command: the \fBupvar\fR |
| 455 | command allows a procedure to reference a variable under a |
| 456 | different name. |
| 457 | \fIOp\fR indicates what operation is being performed on the |
| 458 | variable, and is one of \fBread\fR, \fBwrite\fR, or \fBunset\fR as |
| 459 | defined above. |
| 460 | .PP |
| 461 | \fICommand\fR executes in the same context as the code that invoked |
| 462 | the traced operation: if the variable was accessed as part of a Tcl |
| 463 | procedure, then \fIcommand\fR will have access to the same local |
| 464 | variables as code in the procedure. This context may be different |
| 465 | than the context in which the trace was created. If \fIcommand\fR |
| 466 | invokes a procedure (which it normally does) then the procedure will |
| 467 | have to use \fBupvar\fR or \fBuplevel\fR if it wishes to access the |
| 468 | traced variable. Note also that \fIname1\fR may not necessarily be |
| 469 | the same as the name used to set the trace on the variable; |
| 470 | differences can occur if the access is made through a variable defined |
| 471 | with the \fBupvar\fR command. |
| 472 | .PP |
| 473 | For read and write traces, \fIcommand\fR can modify the variable to |
| 474 | affect the result of the traced operation. If \fIcommand\fR modifies |
| 475 | the value of a variable during a read or write trace, then the new |
| 476 | value will be returned as the result of the traced operation. The |
| 477 | return value from \fIcommand\fR is ignored except that if it returns |
| 478 | an error of any sort then the traced operation also returns an error |
| 479 | with the same error message returned by the trace command (this |
| 480 | mechanism can be used to implement read-only variables, for example). |
| 481 | For write traces, \fIcommand\fR is invoked after the variable's value |
| 482 | has been changed; it can write a new value into the variable to |
| 483 | override the original value specified in the write operation. To |
| 484 | implement read-only variables, \fIcommand\fR will have to restore the |
| 485 | old value of the variable. |
| 486 | .PP |
| 487 | While \fIcommand\fR is executing during a read or write trace, traces |
| 488 | on the variable are temporarily disabled. This means that reads and |
| 489 | writes invoked by \fIcommand\fR will occur directly, without invoking |
| 490 | \fIcommand\fR (or any other traces) again. However, if \fIcommand\fR |
| 491 | unsets the variable then unset traces will be invoked. |
| 492 | .PP |
| 493 | When an unset trace is invoked, the variable has already been deleted: |
| 494 | it will appear to be undefined with no traces. If an unset occurs |
| 495 | because of a procedure return, then the trace will be invoked in the |
| 496 | variable context of the procedure being returned to: the stack frame |
| 497 | of the returning procedure will no longer exist. Traces are not |
| 498 | disabled during unset traces, so if an unset trace command creates a |
| 499 | new trace and accesses the variable, the trace will be invoked. Any |
| 500 | errors in unset traces are ignored. |
| 501 | .PP |
| 502 | If there are multiple traces on a variable they are invoked in order |
| 503 | of creation, most-recent first. If one trace returns an error, then |
| 504 | no further traces are invoked for the variable. If an array element |
| 505 | has a trace set, and there is also a trace set on the array as a |
| 506 | whole, the trace on the overall array is invoked before the one on the |
| 507 | element. |
| 508 | .PP |
| 509 | Once created, the trace remains in effect either until the trace is |
| 510 | removed with the \fBtrace remove variable\fR command described below, |
| 511 | until the variable is unset, or until the interpreter is deleted. |
| 512 | Unsetting an element of array will remove any traces on that element, |
| 513 | but will not remove traces on the overall array. |
| 514 | .PP |
| 515 | This command returns an empty string. |
| 516 | .RE |
| 517 | .RE |
| 518 | .TP |
| 519 | \fBtrace remove \fItype name opList command\fR |
| 520 | Where \fItype\fR is either \fBcommand\fR, \fBexecution\fR or \fBvariable\fR. |
| 521 | .RS |
| 522 | .TP |
| 523 | \fBtrace remove command\fI name opList command\fR |
| 524 | If there is a trace set on command \fIname\fR with the operations and |
| 525 | command given by \fIopList\fR and \fIcommand\fR, then the trace is |
| 526 | removed, so that \fIcommand\fR will never again be invoked. Returns |
| 527 | an empty string. If \fIname\fR doesn't exist, the command will throw |
| 528 | an error. |
| 529 | .TP |
| 530 | \fBtrace remove execution\fI name opList command\fR |
| 531 | If there is a trace set on command \fIname\fR with the operations and |
| 532 | command given by \fIopList\fR and \fIcommand\fR, then the trace is |
| 533 | removed, so that \fIcommand\fR will never again be invoked. Returns |
| 534 | an empty string. If \fIname\fR doesn't exist, the command will throw |
| 535 | an error. |
| 536 | .TP |
| 537 | \fBtrace remove variable\fI name opList command\fR |
| 538 | If there is a trace set on variable \fIname\fR with the operations and |
| 539 | command given by \fIopList\fR and \fIcommand\fR, then the trace is |
| 540 | removed, so that \fIcommand\fR will never again be invoked. Returns |
| 541 | an empty string. |
| 542 | .RE |
| 543 | .TP |
| 544 | \fBtrace info \fItype name\fR |
| 545 | Where \fItype\fR is either \fBcommand\fR, \fBexecution\fR or \fBvariable\fR. |
| 546 | .RS |
| 547 | .TP |
| 548 | \fBtrace info command\fI name\fR |
| 549 | Returns a list containing one element for each trace currently set on |
| 550 | command \fIname\fR. Each element of the list is itself a list |
| 551 | containing two elements, which are the \fIopList\fR and \fIcommand\fR |
| 552 | associated with the trace. If \fIname\fR doesn't have any traces set, |
| 553 | then the result of the command will be an empty string. If \fIname\fR |
| 554 | doesn't exist, the command will throw an error. |
| 555 | .TP |
| 556 | \fBtrace info execution\fI name\fR |
| 557 | Returns a list containing one element for each trace currently set on |
| 558 | command \fIname\fR. Each element of the list is itself a list |
| 559 | containing two elements, which are the \fIopList\fR and \fIcommand\fR |
| 560 | associated with the trace. If \fIname\fR doesn't have any traces set, |
| 561 | then the result of the command will be an empty string. If \fIname\fR |
| 562 | doesn't exist, the command will throw an error. |
| 563 | .TP |
| 564 | \fBtrace info variable\fI name\fR |
| 565 | Returns a list containing one element for each trace currently set on |
| 566 | variable \fIname\fR. Each element of the list is itself a list |
| 567 | containing two elements, which are the \fIopList\fR and \fIcommand\fR |
| 568 | associated with the trace. If \fIname\fR doesn't exist or doesn't |
| 569 | have any traces set, then the result of the command will be an empty |
| 570 | string. |
| 571 | .RE |
| 572 | .PP |
| 573 | For backwards compatibility, three other subcommands are available: |
| 574 | .RS |
| 575 | .TP |
| 576 | \fBtrace variable \fIname ops command\fR |
| 577 | This is equivalent to \fBtrace add variable \fIname ops command\fR. |
| 578 | .TP |
| 579 | \fBtrace vdelete \fIname ops command\fR |
| 580 | This is equivalent to \fBtrace remove variable \fIname ops command\fR |
| 581 | .TP |
| 582 | \fBtrace vinfo \fIname\fR |
| 583 | This is equivalent to \fBtrace info variable \fIname\fR |
| 584 | .RE |
| 585 | .PP |
| 586 | These subcommands are deprecated and will likely be removed in a |
| 587 | future version of Tcl. They use an older syntax in which \fBarray\fR, |
| 588 | \fBread\fR, \fBwrite\fR, \fBunset\fR are replaced by \fBa\fR, \fBr\fR, |
| 589 | \fBw\fR and \fBu\fR respectively, and the \fIops\fR argument is not a |
| 590 | list, but simply a string concatenation of the operations, such as |
| 591 | \fBrwua\fR. |
| 592 | .SH EXAMPLES |
| 593 | Print a message whenever either of the global variables \fBfoo\fR and |
| 594 | \fBbar\fR are updated, even if they have a different local name at the |
| 595 | time (which can be done with the \fBupvar\fR command): |
| 596 | .CS |
| 597 | proc tracer {varname args} { |
| 598 | upvar #0 $varname var |
| 599 | puts "$varname was updated to be \e"$var\e"" |
| 600 | } |
| 601 | \fBtrace add\fR variable foo write "tracer foo" |
| 602 | \fBtrace add\fR variable bar write "tracer bar" |
| 603 | .CE |
| 604 | .PP |
| 605 | Ensure that the global variable \fBfoobar\fR always contains the |
| 606 | product of the global variables \fBfoo\fR and \fBbar\fR: |
| 607 | .CS |
| 608 | proc doMult args { |
| 609 | global foo bar foobar |
| 610 | set foobar [expr {$foo * $bar}] |
| 611 | } |
| 612 | \fBtrace add\fR variable foo write doMult |
| 613 | \fBtrace add\fR variable bar write doMult |
| 614 | .CE |
| 615 | |
| 616 | .SH "SEE ALSO" |
| 617 | set(n), unset(n) |
| 618 | |
| 619 | .SH KEYWORDS |
| 620 | read, command, rename, variable, write, trace, unset |