Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | '\" |
2 | '\" Copyright (c) 1989-1993 The Regents of the University of California. | |
3 | '\" Copyright (c) 1994-1996 Sun Microsystems, Inc. | |
4 | '\" | |
5 | '\" See the file "license.terms" for information on usage and redistribution | |
6 | '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. | |
7 | '\" | |
8 | '\" RCS: @(#) $Id: TraceVar.3,v 1.8 2002/08/05 03:24:39 dgp Exp $ | |
9 | '\" | |
10 | '\" The definitions below are for supplemental macros used in Tcl/Tk | |
11 | '\" manual entries. | |
12 | '\" | |
13 | '\" .AP type name in/out ?indent? | |
14 | '\" Start paragraph describing an argument to a library procedure. | |
15 | '\" type is type of argument (int, etc.), in/out is either "in", "out", | |
16 | '\" or "in/out" to describe whether procedure reads or modifies arg, | |
17 | '\" and indent is equivalent to second arg of .IP (shouldn't ever be | |
18 | '\" needed; use .AS below instead) | |
19 | '\" | |
20 | '\" .AS ?type? ?name? | |
21 | '\" Give maximum sizes of arguments for setting tab stops. Type and | |
22 | '\" name are examples of largest possible arguments that will be passed | |
23 | '\" to .AP later. If args are omitted, default tab stops are used. | |
24 | '\" | |
25 | '\" .BS | |
26 | '\" Start box enclosure. From here until next .BE, everything will be | |
27 | '\" enclosed in one large box. | |
28 | '\" | |
29 | '\" .BE | |
30 | '\" End of box enclosure. | |
31 | '\" | |
32 | '\" .CS | |
33 | '\" Begin code excerpt. | |
34 | '\" | |
35 | '\" .CE | |
36 | '\" End code excerpt. | |
37 | '\" | |
38 | '\" .VS ?version? ?br? | |
39 | '\" Begin vertical sidebar, for use in marking newly-changed parts | |
40 | '\" of man pages. The first argument is ignored and used for recording | |
41 | '\" the version when the .VS was added, so that the sidebars can be | |
42 | '\" found and removed when they reach a certain age. If another argument | |
43 | '\" is present, then a line break is forced before starting the sidebar. | |
44 | '\" | |
45 | '\" .VE | |
46 | '\" End of vertical sidebar. | |
47 | '\" | |
48 | '\" .DS | |
49 | '\" Begin an indented unfilled display. | |
50 | '\" | |
51 | '\" .DE | |
52 | '\" End of indented unfilled display. | |
53 | '\" | |
54 | '\" .SO | |
55 | '\" Start of list of standard options for a Tk widget. The | |
56 | '\" options follow on successive lines, in four columns separated | |
57 | '\" by tabs. | |
58 | '\" | |
59 | '\" .SE | |
60 | '\" End of list of standard options for a Tk widget. | |
61 | '\" | |
62 | '\" .OP cmdName dbName dbClass | |
63 | '\" Start of description of a specific option. cmdName gives the | |
64 | '\" option's name as specified in the class command, dbName gives | |
65 | '\" the option's name in the option database, and dbClass gives | |
66 | '\" the option's class in the option database. | |
67 | '\" | |
68 | '\" .UL arg1 arg2 | |
69 | '\" Print arg1 underlined, then print arg2 normally. | |
70 | '\" | |
71 | '\" RCS: @(#) $Id: man.macros,v 1.4 2000/08/25 06:18:32 ericm Exp $ | |
72 | '\" | |
73 | '\" # Set up traps and other miscellaneous stuff for Tcl/Tk man pages. | |
74 | .if t .wh -1.3i ^B | |
75 | .nr ^l \n(.l | |
76 | .ad b | |
77 | '\" # Start an argument description | |
78 | .de AP | |
79 | .ie !"\\$4"" .TP \\$4 | |
80 | .el \{\ | |
81 | . ie !"\\$2"" .TP \\n()Cu | |
82 | . el .TP 15 | |
83 | .\} | |
84 | .ta \\n()Au \\n()Bu | |
85 | .ie !"\\$3"" \{\ | |
86 | \&\\$1 \\fI\\$2\\fP (\\$3) | |
87 | .\".b | |
88 | .\} | |
89 | .el \{\ | |
90 | .br | |
91 | .ie !"\\$2"" \{\ | |
92 | \&\\$1 \\fI\\$2\\fP | |
93 | .\} | |
94 | .el \{\ | |
95 | \&\\fI\\$1\\fP | |
96 | .\} | |
97 | .\} | |
98 | .. | |
99 | '\" # define tabbing values for .AP | |
100 | .de AS | |
101 | .nr )A 10n | |
102 | .if !"\\$1"" .nr )A \\w'\\$1'u+3n | |
103 | .nr )B \\n()Au+15n | |
104 | .\" | |
105 | .if !"\\$2"" .nr )B \\w'\\$2'u+\\n()Au+3n | |
106 | .nr )C \\n()Bu+\\w'(in/out)'u+2n | |
107 | .. | |
108 | .AS Tcl_Interp Tcl_CreateInterp in/out | |
109 | '\" # BS - start boxed text | |
110 | '\" # ^y = starting y location | |
111 | '\" # ^b = 1 | |
112 | .de BS | |
113 | .br | |
114 | .mk ^y | |
115 | .nr ^b 1u | |
116 | .if n .nf | |
117 | .if n .ti 0 | |
118 | .if n \l'\\n(.lu\(ul' | |
119 | .if n .fi | |
120 | .. | |
121 | '\" # BE - end boxed text (draw box now) | |
122 | .de BE | |
123 | .nf | |
124 | .ti 0 | |
125 | .mk ^t | |
126 | .ie n \l'\\n(^lu\(ul' | |
127 | .el \{\ | |
128 | .\" Draw four-sided box normally, but don't draw top of | |
129 | .\" box if the box started on an earlier page. | |
130 | .ie !\\n(^b-1 \{\ | |
131 | \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul' | |
132 | .\} | |
133 | .el \}\ | |
134 | \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul' | |
135 | .\} | |
136 | .\} | |
137 | .fi | |
138 | .br | |
139 | .nr ^b 0 | |
140 | .. | |
141 | '\" # VS - start vertical sidebar | |
142 | '\" # ^Y = starting y location | |
143 | '\" # ^v = 1 (for troff; for nroff this doesn't matter) | |
144 | .de VS | |
145 | .if !"\\$2"" .br | |
146 | .mk ^Y | |
147 | .ie n 'mc \s12\(br\s0 | |
148 | .el .nr ^v 1u | |
149 | .. | |
150 | '\" # VE - end of vertical sidebar | |
151 | .de VE | |
152 | .ie n 'mc | |
153 | .el \{\ | |
154 | .ev 2 | |
155 | .nf | |
156 | .ti 0 | |
157 | .mk ^t | |
158 | \h'|\\n(^lu+3n'\L'|\\n(^Yu-1v\(bv'\v'\\n(^tu+1v-\\n(^Yu'\h'-|\\n(^lu+3n' | |
159 | .sp -1 | |
160 | .fi | |
161 | .ev | |
162 | .\} | |
163 | .nr ^v 0 | |
164 | .. | |
165 | '\" # Special macro to handle page bottom: finish off current | |
166 | '\" # box/sidebar if in box/sidebar mode, then invoked standard | |
167 | '\" # page bottom macro. | |
168 | .de ^B | |
169 | .ev 2 | |
170 | 'ti 0 | |
171 | 'nf | |
172 | .mk ^t | |
173 | .if \\n(^b \{\ | |
174 | .\" Draw three-sided box if this is the box's first page, | |
175 | .\" draw two sides but no top otherwise. | |
176 | .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 | |
177 | .el \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c | |
178 | .\} | |
179 | .if \\n(^v \{\ | |
180 | .nr ^x \\n(^tu+1v-\\n(^Yu | |
181 | \kx\h'-\\nxu'\h'|\\n(^lu+3n'\ky\L'-\\n(^xu'\v'\\n(^xu'\h'|0u'\c | |
182 | .\} | |
183 | .bp | |
184 | 'fi | |
185 | .ev | |
186 | .if \\n(^b \{\ | |
187 | .mk ^y | |
188 | .nr ^b 2 | |
189 | .\} | |
190 | .if \\n(^v \{\ | |
191 | .mk ^Y | |
192 | .\} | |
193 | .. | |
194 | '\" # DS - begin display | |
195 | .de DS | |
196 | .RS | |
197 | .nf | |
198 | .sp | |
199 | .. | |
200 | '\" # DE - end display | |
201 | .de DE | |
202 | .fi | |
203 | .RE | |
204 | .sp | |
205 | .. | |
206 | '\" # SO - start of list of standard options | |
207 | .de SO | |
208 | .SH "STANDARD OPTIONS" | |
209 | .LP | |
210 | .nf | |
211 | .ta 5.5c 11c | |
212 | .ft B | |
213 | .. | |
214 | '\" # SE - end of list of standard options | |
215 | .de SE | |
216 | .fi | |
217 | .ft R | |
218 | .LP | |
219 | See the \\fBoptions\\fR manual entry for details on the standard options. | |
220 | .. | |
221 | '\" # OP - start of full description for a single option | |
222 | .de OP | |
223 | .LP | |
224 | .nf | |
225 | .ta 4c | |
226 | Command-Line Name: \\fB\\$1\\fR | |
227 | Database Name: \\fB\\$2\\fR | |
228 | Database Class: \\fB\\$3\\fR | |
229 | .fi | |
230 | .IP | |
231 | .. | |
232 | '\" # CS - begin code excerpt | |
233 | .de CS | |
234 | .RS | |
235 | .nf | |
236 | .ta .25i .5i .75i 1i | |
237 | .. | |
238 | '\" # CE - end code excerpt | |
239 | .de CE | |
240 | .fi | |
241 | .RE | |
242 | .. | |
243 | .de UL | |
244 | \\$1\l'|0\(ul'\\$2 | |
245 | .. | |
246 | .TH Tcl_TraceVar 3 7.4 Tcl "Tcl Library Procedures" | |
247 | .BS | |
248 | .SH NAME | |
249 | Tcl_TraceVar, Tcl_TraceVar2, Tcl_UntraceVar, Tcl_UntraceVar2, Tcl_VarTraceInfo, Tcl_VarTraceInfo2 \- monitor accesses to a variable | |
250 | .SH SYNOPSIS | |
251 | .nf | |
252 | \fB#include <tcl.h>\fR | |
253 | .sp | |
254 | int | |
255 | \fBTcl_TraceVar(\fIinterp, varName, flags, proc, clientData\fB)\fR | |
256 | .sp | |
257 | int | |
258 | \fBTcl_TraceVar2(\fIinterp, name1, name2, flags, proc, clientData\fB)\fR | |
259 | .sp | |
260 | \fBTcl_UntraceVar(\fIinterp, varName, flags, proc, clientData\fB)\fR | |
261 | .sp | |
262 | \fBTcl_UntraceVar2(\fIinterp, name1, name2, flags, proc, clientData\fB)\fR | |
263 | .sp | |
264 | ClientData | |
265 | \fBTcl_VarTraceInfo(\fIinterp, varName, flags, proc, prevClientData\fB)\fR | |
266 | .sp | |
267 | ClientData | |
268 | \fBTcl_VarTraceInfo2(\fIinterp, name1, name2, flags, proc, prevClientData\fB)\fR | |
269 | .SH ARGUMENTS | |
270 | .AS Tcl_VarTraceProc prevClientData | |
271 | .AP Tcl_Interp *interp in | |
272 | Interpreter containing variable. | |
273 | .AP "CONST char" *varName in | |
274 | Name of variable. May refer to a scalar variable, to | |
275 | an array variable with no index, or to an array variable | |
276 | with a parenthesized index. | |
277 | .AP int flags in | |
278 | OR-ed combination of the values TCL_TRACE_READS, TCL_TRACE_WRITES, | |
279 | TCL_TRACE_UNSETS, TCL_TRACE_ARRAY, TCL_GLOBAL_ONLY, TCL_NAMESPACE_ONLY, | |
280 | TCL_TRACE_RESULT_DYNAMIC and TCL_TRACE_RESULT_OBJECT. | |
281 | Not all flags are used by all | |
282 | procedures. See below for more information. | |
283 | .AP Tcl_VarTraceProc *proc in | |
284 | Procedure to invoke whenever one of the traced operations occurs. | |
285 | .AP ClientData clientData in | |
286 | Arbitrary one-word value to pass to \fIproc\fR. | |
287 | .AP "CONST char" *name1 in | |
288 | Name of scalar or array variable (without array index). | |
289 | .AP "CONST char" *name2 in | |
290 | For a trace on an element of an array, gives the index of the | |
291 | element. For traces on scalar variables or on whole arrays, | |
292 | is NULL. | |
293 | .AP ClientData prevClientData in | |
294 | If non-NULL, gives last value returned by \fBTcl_VarTraceInfo\fR or | |
295 | \fBTcl_VarTraceInfo2\fR, so this call will return information about | |
296 | next trace. If NULL, this call will return information about first | |
297 | trace. | |
298 | .BE | |
299 | ||
300 | .SH DESCRIPTION | |
301 | .PP | |
302 | \fBTcl_TraceVar\fR allows a C procedure to monitor and control | |
303 | access to a Tcl variable, so that the C procedure is invoked | |
304 | whenever the variable is read or written or unset. | |
305 | If the trace is created successfully then \fBTcl_TraceVar\fR returns | |
306 | TCL_OK. If an error occurred (e.g. \fIvarName\fR specifies an element | |
307 | of an array, but the actual variable isn't an array) then TCL_ERROR | |
308 | is returned and an error message is left in the interpreter's result. | |
309 | .PP | |
310 | The \fIflags\fR argument to \fBTcl_TraceVar\fR indicates when the | |
311 | trace procedure is to be invoked and provides information | |
312 | for setting up the trace. It consists of an OR-ed combination | |
313 | of any of the following values: | |
314 | .TP | |
315 | \fBTCL_GLOBAL_ONLY\fR | |
316 | Normally, the variable will be looked up at the current level of | |
317 | procedure call; if this bit is set then the variable will be looked | |
318 | up at global level, ignoring any active procedures. | |
319 | .TP | |
320 | \fBTCL_NAMESPACE_ONLY\fR | |
321 | Normally, the variable will be looked up at the current level of | |
322 | procedure call; if this bit is set then the variable will be looked | |
323 | up in the current namespace, ignoring any active procedures. | |
324 | .TP | |
325 | \fBTCL_TRACE_READS\fR | |
326 | Invoke \fIproc\fR whenever an attempt is made to read the variable. | |
327 | .TP | |
328 | \fBTCL_TRACE_WRITES\fR | |
329 | Invoke \fIproc\fR whenever an attempt is made to modify the variable. | |
330 | .TP | |
331 | \fBTCL_TRACE_UNSETS\fR | |
332 | Invoke \fIproc\fR whenever the variable is unset. | |
333 | A variable may be unset either explicitly by an \fBunset\fR command, | |
334 | or implicitly when a procedure returns (its local variables are | |
335 | automatically unset) or when the interpreter is deleted (all | |
336 | variables are automatically unset). | |
337 | .TP | |
338 | \fBTCL_TRACE_ARRAY\fR | |
339 | Invoke \fIproc\fR whenever the array command is invoked. | |
340 | This gives the trace procedure a chance to update the array before | |
341 | array names or array get is called. Note that this is called | |
342 | before an array set, but that will trigger write traces. | |
343 | .VS 8.4 | |
344 | .TP | |
345 | \fBTCL_TRACE_RESULT_DYNAMIC\fR | |
346 | The result of invoking the \fIproc\fR is a dynamically allocated | |
347 | string that will be released by the Tcl library via a call to | |
348 | \fBckfree\fR. Must not be specified at the same time as | |
349 | TCL_TRACE_RESULT_OBJECT. | |
350 | .TP | |
351 | \fBTCL_TRACE_RESULT_OBJECT\fR | |
352 | The result of invoking the \fIproc\fR is a Tcl_Obj* (cast to a char*) | |
353 | with a reference count of at least one. The ownership of that | |
354 | reference will be transferred to the Tcl core for release (when the | |
355 | core has finished with it) via a call to \fBTcl_DecrRefCount\fR. Must | |
356 | not be specified at the same time as TCL_TRACE_RESULT_DYNAMIC. | |
357 | .VE 8.4 | |
358 | .PP | |
359 | Whenever one of the specified operations occurs on the variable, | |
360 | \fIproc\fR will be invoked. | |
361 | It should have arguments and result that match the type | |
362 | \fBTcl_VarTraceProc\fR: | |
363 | .CS | |
364 | typedef char *Tcl_VarTraceProc( | |
365 | ClientData \fIclientData\fR, | |
366 | Tcl_Interp *\fIinterp\fR, | |
367 | char *\fIname1\fR, | |
368 | char *\fIname2\fR, | |
369 | int \fIflags\fR); | |
370 | .CE | |
371 | The \fIclientData\fR and \fIinterp\fR parameters will | |
372 | have the same values as those passed to \fBTcl_TraceVar\fR when the | |
373 | trace was created. | |
374 | \fIClientData\fR typically points to an application-specific | |
375 | data structure that describes what to do when \fIproc\fR | |
376 | is invoked. | |
377 | \fIName1\fR and \fIname2\fR give the name of the traced variable | |
378 | in the normal two-part form (see the description of \fBTcl_TraceVar2\fR | |
379 | below for details). | |
380 | \fIFlags\fR is an OR-ed combination of bits providing several | |
381 | pieces of information. | |
382 | One of the bits TCL_TRACE_READS, TCL_TRACE_WRITES, TCL_TRACE_ARRAY, | |
383 | or TCL_TRACE_UNSETS | |
384 | will be set in \fIflags\fR to indicate which operation is being performed | |
385 | on the variable. | |
386 | The bit TCL_GLOBAL_ONLY will be set whenever the variable being | |
387 | accessed is a global one not accessible from the current level of | |
388 | procedure call: the trace procedure will need to pass this flag | |
389 | back to variable-related procedures like \fBTcl_GetVar\fR if it | |
390 | attempts to access the variable. | |
391 | The bit TCL_NAMESPACE_ONLY will be set whenever the variable being | |
392 | accessed is a namespace one not accessible from the current level of | |
393 | procedure call: the trace procedure will need to pass this flag | |
394 | back to variable-related procedures like \fBTcl_GetVar\fR if it | |
395 | attempts to access the variable. | |
396 | The bit TCL_TRACE_DESTROYED will be set in \fIflags\fR if the trace is | |
397 | about to be destroyed; this information may be useful to \fIproc\fR | |
398 | so that it can clean up its own internal data structures (see | |
399 | the section TCL_TRACE_DESTROYED below for more details). | |
400 | Lastly, the bit TCL_INTERP_DESTROYED will be set if the entire | |
401 | interpreter is being destroyed. | |
402 | When this bit is set, \fIproc\fR must be especially careful in | |
403 | the things it does (see the section TCL_INTERP_DESTROYED below). | |
404 | The trace procedure's return value should normally be NULL; see | |
405 | ERROR RETURNS below for information on other possibilities. | |
406 | .PP | |
407 | \fBTcl_UntraceVar\fR may be used to remove a trace. | |
408 | If the variable specified by \fIinterp\fR, \fIvarName\fR, and \fIflags\fR | |
409 | has a trace set with \fIflags\fR, \fIproc\fR, and | |
410 | \fIclientData\fR, then the corresponding trace is removed. | |
411 | If no such trace exists, then the call to \fBTcl_UntraceVar\fR | |
412 | has no effect. | |
413 | The same bits are valid for \fIflags\fR as for calls to \fBTcl_TraceVar\fR. | |
414 | .PP | |
415 | \fBTcl_VarTraceInfo\fR may be used to retrieve information about | |
416 | traces set on a given variable. | |
417 | The return value from \fBTcl_VarTraceInfo\fR is the \fIclientData\fR | |
418 | associated with a particular trace. | |
419 | The trace must be on the variable specified by the \fIinterp\fR, | |
420 | \fIvarName\fR, and \fIflags\fR arguments (only the TCL_GLOBAL_ONLY and | |
421 | TCL_NAMESPACE_ONLY bits from \fIflags\fR is used; other bits are | |
422 | ignored) and its trace procedure must the same as the \fIproc\fR | |
423 | argument. | |
424 | If the \fIprevClientData\fR argument is NULL then the return | |
425 | value corresponds to the first (most recently created) matching | |
426 | trace, or NULL if there are no matching traces. | |
427 | If the \fIprevClientData\fR argument isn't NULL, then it should | |
428 | be the return value from a previous call to \fBTcl_VarTraceInfo\fR. | |
429 | In this case, the new return value will correspond to the next | |
430 | matching trace after the one whose \fIclientData\fR matches | |
431 | \fIprevClientData\fR, or NULL if no trace matches \fIprevClientData\fR | |
432 | or if there are no more matching traces after it. | |
433 | This mechanism makes it possible to step through all of the | |
434 | traces for a given variable that have the same \fIproc\fR. | |
435 | ||
436 | .SH "TWO-PART NAMES" | |
437 | .PP | |
438 | The procedures \fBTcl_TraceVar2\fR, \fBTcl_UntraceVar2\fR, and | |
439 | \fBTcl_VarTraceInfo2\fR are identical to \fBTcl_TraceVar\fR, | |
440 | \fBTcl_UntraceVar\fR, and \fBTcl_VarTraceInfo\fR, respectively, | |
441 | except that the name of the variable consists of two parts. | |
442 | \fIName1\fR gives the name of a scalar variable or array, | |
443 | and \fIname2\fR gives the name of an element within an array. | |
444 | .VS 8.1 | |
445 | When \fIname2\fR is NULL, | |
446 | \fIname1\fR may contain both an array and an element name: | |
447 | if the name contains an open parenthesis and ends with a | |
448 | close parenthesis, then the value between the parentheses is | |
449 | treated as an element name (which can have any string value) and | |
450 | the characters before the first open | |
451 | parenthesis are treated as the name of an array variable. | |
452 | If \fIname2\fR is NULL and \fIname1\fR does not refer | |
453 | to an array element | |
454 | .VE | |
455 | it means that either the variable is | |
456 | a scalar or the trace is to be set on the entire array rather | |
457 | than an individual element (see WHOLE-ARRAY TRACES below for | |
458 | more information). | |
459 | ||
460 | ||
461 | .SH "ACCESSING VARIABLES DURING TRACES" | |
462 | .PP | |
463 | During read, write, and array traces, the | |
464 | trace procedure can read, write, or unset the traced | |
465 | variable using \fBTcl_GetVar2\fR, \fBTcl_SetVar2\fR, and | |
466 | other procedures. | |
467 | While \fIproc\fR is executing, traces are temporarily disabled | |
468 | for the variable, so that calls to \fBTcl_GetVar2\fR and | |
469 | \fBTcl_SetVar2\fR will not cause \fIproc\fR or other trace procedures | |
470 | to be invoked again. | |
471 | Disabling only occurs for the variable whose trace procedure | |
472 | is active; accesses to other variables will still be traced. | |
473 | However, if a variable is unset during a read or write trace then unset | |
474 | traces will be invoked. | |
475 | .PP | |
476 | During unset traces the variable has already been completely | |
477 | expunged. | |
478 | It is possible for the trace procedure to read or write the | |
479 | variable, but this will be a new version of the variable. | |
480 | Traces are not disabled during unset traces as they are for | |
481 | read and write traces, but existing traces have been removed | |
482 | from the variable before any trace procedures are invoked. | |
483 | If new traces are set by unset trace procedures, these traces | |
484 | will be invoked on accesses to the variable by the trace | |
485 | procedures. | |
486 | ||
487 | .SH "CALLBACK TIMING" | |
488 | .PP | |
489 | When read tracing has been specified for a variable, the trace | |
490 | procedure will be invoked whenever the variable's value is | |
491 | read. This includes \fBset\fR Tcl commands, \fB$\fR-notation | |
492 | in Tcl commands, and invocations of the \fBTcl_GetVar\fR | |
493 | and \fBTcl_GetVar2\fR procedures. | |
494 | \fIProc\fR is invoked just before the variable's value is | |
495 | returned. | |
496 | It may modify the value of the variable to affect what | |
497 | is returned by the traced access. | |
498 | If it unsets the variable then the access will return an error | |
499 | just as if the variable never existed. | |
500 | .PP | |
501 | When write tracing has been specified for a variable, the | |
502 | trace procedure will be invoked whenever the variable's value | |
503 | is modified. This includes \fBset\fR commands, | |
504 | commands that modify variables as side effects (such as | |
505 | \fBcatch\fR and \fBscan\fR), and calls to the \fBTcl_SetVar\fR | |
506 | and \fBTcl_SetVar2\fR procedures). | |
507 | \fIProc\fR will be invoked after the variable's value has been | |
508 | modified, but before the new value of the variable has been | |
509 | returned. | |
510 | It may modify the value of the variable to override the change | |
511 | and to determine the value actually returned by the traced | |
512 | access. | |
513 | If it deletes the variable then the traced access will return | |
514 | an empty string. | |
515 | .PP | |
516 | When array tracing has been specified, the trace procedure | |
517 | will be invoked at the beginning of the array command implementation, | |
518 | before any of the operations like get, set, or names have been invoked. | |
519 | The trace procedure can modify the array elements with \fBTcl_SetVar\fR | |
520 | and \fBTcl_SetVar2\fR. | |
521 | .PP | |
522 | When unset tracing has been specified, the trace procedure | |
523 | will be invoked whenever the variable is destroyed. | |
524 | The traces will be called after the variable has been | |
525 | completely unset. | |
526 | ||
527 | .SH "WHOLE-ARRAY TRACES" | |
528 | .PP | |
529 | If a call to \fBTcl_TraceVar\fR or \fBTcl_TraceVar2\fR specifies | |
530 | the name of an array variable without an index into the array, | |
531 | then the trace will be set on the array as a whole. | |
532 | This means that \fIproc\fR will be invoked whenever any | |
533 | element of the array is accessed in the ways specified by | |
534 | \fIflags\fR. | |
535 | When an array is unset, a whole-array trace will be invoked | |
536 | just once, with \fIname1\fR equal to the name of the array | |
537 | and \fIname2\fR NULL; it will not be invoked once for each | |
538 | element. | |
539 | ||
540 | .SH "MULTIPLE TRACES" | |
541 | .PP | |
542 | It is possible for multiple traces to exist on the same variable. | |
543 | When this happens, all of the trace procedures will be invoked on each | |
544 | access, in order from most-recently-created to least-recently-created. | |
545 | When there exist whole-array traces for an array as well as | |
546 | traces on individual elements, the whole-array traces are invoked | |
547 | before the individual-element traces. | |
548 | If a read or write trace unsets the variable then all of the unset | |
549 | traces will be invoked but the remainder of the read and write traces | |
550 | will be skipped. | |
551 | ||
552 | .SH "ERROR RETURNS" | |
553 | .PP | |
554 | Under normal conditions trace procedures should return NULL, indicating | |
555 | successful completion. | |
556 | If \fIproc\fR returns a non-NULL value it signifies that an | |
557 | error occurred. | |
558 | The return value must be a pointer to a static character string | |
559 | containing an error message, | |
560 | .VS 8.4 | |
561 | unless (\fIexactly\fR one of) the TCL_TRACE_RESULT_DYNAMIC and | |
562 | TCL_TRACE_RESULT_OBJECT flags is set, which specify that the result is | |
563 | either a dynamic string (to be released with \fBckfree\fR) or a | |
564 | Tcl_Obj* (cast to char* and to be released with | |
565 | \fBTcl_DecrRefCount\fR) containing the error message. | |
566 | .VE 8.4 | |
567 | If a trace procedure returns an error, no further traces are | |
568 | invoked for the access and the traced access aborts with the | |
569 | given message. | |
570 | Trace procedures can use this facility to make variables | |
571 | read-only, for example (but note that the value of the variable | |
572 | will already have been modified before the trace procedure is | |
573 | called, so the trace procedure will have to restore the correct | |
574 | value). | |
575 | .PP | |
576 | The return value from \fIproc\fR is only used during read and | |
577 | write tracing. | |
578 | During unset traces, the return value is ignored and all relevant | |
579 | trace procedures will always be invoked. | |
580 | ||
581 | .SH "RESTRICTIONS" | |
582 | .PP | |
583 | A trace procedure can be called at any time, even when there | |
584 | is a partially-formed result in the interpreter's result area. If | |
585 | the trace procedure does anything that could damage this result (such | |
586 | as calling \fBTcl_Eval\fR) then it must save the original values of | |
587 | the interpreter's \fBresult\fR and \fBfreeProc\fR fields and restore | |
588 | them before it returns. | |
589 | ||
590 | .SH "UNDEFINED VARIABLES" | |
591 | .PP | |
592 | It is legal to set a trace on an undefined variable. | |
593 | The variable will still appear to be undefined until the | |
594 | first time its value is set. | |
595 | If an undefined variable is traced and then unset, the unset will fail | |
596 | with an error (``no such variable''), but the trace | |
597 | procedure will still be invoked. | |
598 | ||
599 | .SH "TCL_TRACE_DESTROYED FLAG" | |
600 | .PP | |
601 | In an unset callback to \fIproc\fR, the TCL_TRACE_DESTROYED bit | |
602 | is set in \fIflags\fR if the trace is being removed as part | |
603 | of the deletion. | |
604 | Traces on a variable are always removed whenever the variable | |
605 | is deleted; the only time TCL_TRACE_DESTROYED isn't set is for | |
606 | a whole-array trace invoked when only a single element of an | |
607 | array is unset. | |
608 | ||
609 | .SH "TCL_INTERP_DESTROYED" | |
610 | .PP | |
611 | When an interpreter is destroyed, unset traces are called for | |
612 | all of its variables. | |
613 | The TCL_INTERP_DESTROYED bit will be set in the \fIflags\fR | |
614 | argument passed to the trace procedures. | |
615 | Trace procedures must be extremely careful in what they do if | |
616 | the TCL_INTERP_DESTROYED bit is set. | |
617 | It is not safe for the procedures to invoke any Tcl procedures | |
618 | on the interpreter, since its state is partially deleted. | |
619 | All that trace procedures should do under these circumstances is | |
620 | to clean up and free their own internal data structures. | |
621 | ||
622 | .SH BUGS | |
623 | .PP | |
624 | Tcl doesn't do any error checking to prevent trace procedures | |
625 | from misusing the interpreter during traces with TCL_INTERP_DESTROYED | |
626 | set. | |
627 | .PP | |
628 | Array traces are not yet integrated with the Tcl "info exists" command, | |
629 | nor is there Tcl-level access to array traces. | |
630 | ||
631 | .SH KEYWORDS | |
632 | clientData, trace, variable |