1Tcl_TraceVar(3) Tcl Library Procedures Tcl_TraceVar(3)
2
3
4
5______________________________________________________________________________
6
8 Tcl_TraceVar, Tcl_TraceVar2, Tcl_UntraceVar, Tcl_UntraceVar2, Tcl_Var‐
9 TraceInfo, Tcl_VarTraceInfo2 - monitor accesses to a variable
10
12 #include <tcl.h>
13
14 int
15 Tcl_TraceVar(interp, varName, flags, proc, clientData)
16
17 int
18 Tcl_TraceVar2(interp, name1, name2, flags, proc, clientData)
19
20 Tcl_UntraceVar(interp, varName, flags, proc, clientData)
21
22 Tcl_UntraceVar2(interp, name1, name2, flags, proc, clientData)
23
24 ClientData
25 Tcl_VarTraceInfo(interp, varName, flags, proc, prevClientData)
26
27 ClientData
28 Tcl_VarTraceInfo2(interp, name1, name2, flags, proc, prevClientData)
29
31 Tcl_Interp *interp (in) Interpreter containing
32 variable.
33
34 const char *varName (in) Name of variable. May
35 refer to a scalar vari‐
36 able, to an array vari‐
37 able with no index, or to
38 an array variable with a
39 parenthesized index.
40
41 int flags (in) OR-ed combination of the
42 values TCL_TRACE_READS,
43 TCL_TRACE_WRITES,
44 TCL_TRACE_UNSETS,
45 TCL_TRACE_ARRAY,
46 TCL_GLOBAL_ONLY,
47 TCL_NAMESPACE_ONLY,
48 TCL_TRACE_RESULT_DYNAMIC
49 and
50 TCL_TRACE_RESULT_OBJECT.
51 Not all flags are used by
52 all procedures. See
53 below for more informa‐
54 tion.
55
56 Tcl_VarTraceProc *proc (in) Procedure to invoke when‐
57 ever one of the traced
58 operations occurs.
59
60 ClientData clientData (in) Arbitrary one-word value
61 to pass to proc.
62
63 const char *name1 (in) Name of scalar or array
64 variable (without array
65 index).
66
67 const char *name2 (in) For a trace on an element
68 of an array, gives the
69 index of the element.
70 For traces on scalar
71 variables or on whole
72 arrays, is NULL.
73
74 ClientData prevClientData (in) If non-NULL, gives last
75 value returned by
76 Tcl_VarTraceInfo or
77 Tcl_VarTraceInfo2, so
78 this call will return
79 information about next
80 trace. If NULL, this
81 call will return informa‐
82 tion about first trace.
83_________________________________________________________________
84
86 Tcl_TraceVar allows a C procedure to monitor and control access to a
87 Tcl variable, so that the C procedure is invoked whenever the variable
88 is read or written or unset. If the trace is created successfully then
89 Tcl_TraceVar returns TCL_OK. If an error occurred (e.g. varName speci‐
90 fies an element of an array, but the actual variable is not an array)
91 then TCL_ERROR is returned and an error message is left in the inter‐
92 preter's result.
93
94 The flags argument to Tcl_TraceVar indicates when the trace procedure
95 is to be invoked and provides information for setting up the trace. It
96 consists of an OR-ed combination of any of the following values:
97
98 TCL_GLOBAL_ONLY
99 Normally, the variable will be looked up at the current level of
100 procedure call; if this bit is set then the variable will be
101 looked up at global level, ignoring any active procedures.
102
103 TCL_NAMESPACE_ONLY
104 Normally, the variable will be looked up at the current level of
105 procedure call; if this bit is set then the variable will be
106 looked up in the current namespace, ignoring any active proce‐
107 dures.
108
109 TCL_TRACE_READS
110 Invoke proc whenever an attempt is made to read the variable.
111
112 TCL_TRACE_WRITES
113 Invoke proc whenever an attempt is made to modify the variable.
114
115 TCL_TRACE_UNSETS
116 Invoke proc whenever the variable is unset. A variable may be
117 unset either explicitly by an unset command, or implicitly when
118 a procedure returns (its local variables are automatically
119 unset) or when the interpreter is deleted (all variables are
120 automatically unset).
121
122 TCL_TRACE_ARRAY
123 Invoke proc whenever the array command is invoked. This gives
124 the trace procedure a chance to update the array before array
125 names or array get is called. Note that this is called before
126 an array set, but that will trigger write traces.
127
128 TCL_TRACE_RESULT_DYNAMIC
129 The result of invoking the proc is a dynamically allocated
130 string that will be released by the Tcl library via a call to
131 ckfree. Must not be specified at the same time as
132 TCL_TRACE_RESULT_OBJECT.
133
134 TCL_TRACE_RESULT_OBJECT
135 The result of invoking the proc is a Tcl_Obj* (cast to a char*)
136 with a reference count of at least one. The ownership of that
137 reference will be transferred to the Tcl core for release (when
138 the core has finished with it) via a call to Tcl_DecrRefCount.
139 Must not be specified at the same time as
140 TCL_TRACE_RESULT_DYNAMIC.
141
142 Whenever one of the specified operations occurs on the variable, proc
143 will be invoked. It should have arguments and result that match the
144 type Tcl_VarTraceProc:
145 typedef char *Tcl_VarTraceProc(
146 ClientData clientData,
147 Tcl_Interp *interp,
148 char *name1,
149 char *name2,
150 int flags);
151 The clientData and interp parameters will have the same values as those
152 passed to Tcl_TraceVar when the trace was created. ClientData typi‐
153 cally points to an application-specific data structure that describes
154 what to do when proc is invoked. Name1 and name2 give the name of the
155 traced variable in the normal two-part form (see the description of
156 Tcl_TraceVar2 below for details). Flags is an OR-ed combination of
157 bits providing several pieces of information. One of the bits
158 TCL_TRACE_READS, TCL_TRACE_WRITES, TCL_TRACE_ARRAY, or TCL_TRACE_UNSETS
159 will be set in flags to indicate which operation is being performed on
160 the variable. The bit TCL_GLOBAL_ONLY will be set whenever the vari‐
161 able being accessed is a global one not accessible from the current
162 level of procedure call: the trace procedure will need to pass this
163 flag back to variable-related procedures like Tcl_GetVar if it attempts
164 to access the variable. The bit TCL_NAMESPACE_ONLY will be set when‐
165 ever the variable being accessed is a namespace one not accessible from
166 the current level of procedure call: the trace procedure will need to
167 pass this flag back to variable-related procedures like Tcl_GetVar if
168 it attempts to access the variable. The bit TCL_TRACE_DESTROYED will
169 be set in flags if the trace is about to be destroyed; this informa‐
170 tion may be useful to proc so that it can clean up its own internal
171 data structures (see the section TCL_TRACE_DESTROYED below for more
172 details). Lastly, the bit TCL_INTERP_DESTROYED will be set if the
173 entire interpreter is being destroyed. When this bit is set, proc must
174 be especially careful in the things it does (see the section
175 TCL_INTERP_DESTROYED below). The trace procedure's return value should
176 normally be NULL; see ERROR RETURNS below for information on other
177 possibilities.
178
179 Tcl_UntraceVar may be used to remove a trace. If the variable speci‐
180 fied by interp, varName, and flags has a trace set with flags, proc,
181 and clientData, then the corresponding trace is removed. If no such
182 trace exists, then the call to Tcl_UntraceVar has no effect. The same
183 bits are valid for flags as for calls to Tcl_TraceVar.
184
185 Tcl_VarTraceInfo may be used to retrieve information about traces set
186 on a given variable. The return value from Tcl_VarTraceInfo is the
187 clientData associated with a particular trace. The trace must be on
188 the variable specified by the interp, varName, and flags arguments
189 (only the TCL_GLOBAL_ONLY and TCL_NAMESPACE_ONLY bits from flags is
190 used; other bits are ignored) and its trace procedure must the same as
191 the proc argument. If the prevClientData argument is NULL then the
192 return value corresponds to the first (most recently created) matching
193 trace, or NULL if there are no matching traces. If the prevClientData
194 argument is not NULL, then it should be the return value from a previ‐
195 ous call to Tcl_VarTraceInfo. In this case, the new return value will
196 correspond to the next matching trace after the one whose clientData
197 matches prevClientData, or NULL if no trace matches prevClientData or
198 if there are no more matching traces after it. This mechanism makes it
199 possible to step through all of the traces for a given variable that
200 have the same proc.
201
203 The procedures Tcl_TraceVar2, Tcl_UntraceVar2, and Tcl_VarTraceInfo2
204 are identical to Tcl_TraceVar, Tcl_UntraceVar, and Tcl_VarTraceInfo,
205 respectively, except that the name of the variable consists of two
206 parts. Name1 gives the name of a scalar variable or array, and name2
207 gives the name of an element within an array. When name2 is NULL,
208 name1 may contain both an array and an element name: if the name con‐
209 tains an open parenthesis and ends with a close parenthesis, then the
210 value between the parentheses is treated as an element name (which can
211 have any string value) and the characters before the first open paren‐
212 thesis are treated as the name of an array variable. If name2 is NULL
213 and name1 does not refer to an array element it means that either the
214 variable is a scalar or the trace is to be set on the entire array
215 rather than an individual element (see WHOLE-ARRAY TRACES below for
216 more information).
217
219 During read, write, and array traces, the trace procedure can read,
220 write, or unset the traced variable using Tcl_GetVar2, Tcl_SetVar2, and
221 other procedures. While proc is executing, traces are temporarily dis‐
222 abled for the variable, so that calls to Tcl_GetVar2 and Tcl_SetVar2
223 will not cause proc or other trace procedures to be invoked again.
224 Disabling only occurs for the variable whose trace procedure is active;
225 accesses to other variables will still be traced. However, if a vari‐
226 able is unset during a read or write trace then unset traces will be
227 invoked.
228
229 During unset traces the variable has already been completely expunged.
230 It is possible for the trace procedure to read or write the variable,
231 but this will be a new version of the variable. Traces are not dis‐
232 abled during unset traces as they are for read and write traces, but
233 existing traces have been removed from the variable before any trace
234 procedures are invoked. If new traces are set by unset trace proce‐
235 dures, these traces will be invoked on accesses to the variable by the
236 trace procedures.
237
239 When read tracing has been specified for a variable, the trace proce‐
240 dure will be invoked whenever the variable's value is read. This
241 includes set Tcl commands, $-notation in Tcl commands, and invocations
242 of the Tcl_GetVar and Tcl_GetVar2 procedures. Proc is invoked just
243 before the variable's value is returned. It may modify the value of
244 the variable to affect what is returned by the traced access. If it
245 unsets the variable then the access will return an error just as if the
246 variable never existed.
247
248 When write tracing has been specified for a variable, the trace proce‐
249 dure will be invoked whenever the variable's value is modified. This
250 includes set commands, commands that modify variables as side effects
251 (such as catch and scan), and calls to the Tcl_SetVar and Tcl_SetVar2
252 procedures). Proc will be invoked after the variable's value has been
253 modified, but before the new value of the variable has been returned.
254 It may modify the value of the variable to override the change and to
255 determine the value actually returned by the traced access. If it
256 deletes the variable then the traced access will return an empty
257 string.
258
259 When array tracing has been specified, the trace procedure will be
260 invoked at the beginning of the array command implementation, before
261 any of the operations like get, set, or names have been invoked. The
262 trace procedure can modify the array elements with Tcl_SetVar and
263 Tcl_SetVar2.
264
265 When unset tracing has been specified, the trace procedure will be
266 invoked whenever the variable is destroyed. The traces will be called
267 after the variable has been completely unset.
268
270 If a call to Tcl_TraceVar or Tcl_TraceVar2 specifies the name of an
271 array variable without an index into the array, then the trace will be
272 set on the array as a whole. This means that proc will be invoked
273 whenever any element of the array is accessed in the ways specified by
274 flags. When an array is unset, a whole-array trace will be invoked
275 just once, with name1 equal to the name of the array and name2 NULL;
276 it will not be invoked once for each element.
277
279 It is possible for multiple traces to exist on the same variable. When
280 this happens, all of the trace procedures will be invoked on each
281 access, in order from most-recently-created to least-recently-created.
282 When there exist whole-array traces for an array as well as traces on
283 individual elements, the whole-array traces are invoked before the
284 individual-element traces. If a read or write trace unsets the vari‐
285 able then all of the unset traces will be invoked but the remainder of
286 the read and write traces will be skipped.
287
289 Under normal conditions trace procedures should return NULL, indicating
290 successful completion. If proc returns a non-NULL value it signifies
291 that an error occurred. The return value must be a pointer to a static
292 character string containing an error message, unless (exactly one of)
293 the TCL_TRACE_RESULT_DYNAMIC and TCL_TRACE_RESULT_OBJECT flags is set,
294 which specify that the result is either a dynamic string (to be
295 released with ckfree) or a Tcl_Obj* (cast to char* and to be released
296 with Tcl_DecrRefCount) containing the error message. If a trace proce‐
297 dure returns an error, no further traces are invoked for the access and
298 the traced access aborts with the given message. Trace procedures can
299 use this facility to make variables read-only, for example (but note
300 that the value of the variable will already have been modified before
301 the trace procedure is called, so the trace procedure will have to
302 restore the correct value).
303
304 The return value from proc is only used during read and write tracing.
305 During unset traces, the return value is ignored and all relevant trace
306 procedures will always be invoked.
307
309 A trace procedure can be called at any time, even when there is a par‐
310 tially formed result in the interpreter's result area. If the trace
311 procedure does anything that could damage this result (such as calling
312 Tcl_Eval) then it must save the original values of the interpreter's
313 result and freeProc fields and restore them before it returns.
314
316 It is legal to set a trace on an undefined variable. The variable will
317 still appear to be undefined until the first time its value is set. If
318 an undefined variable is traced and then unset, the unset will fail
319 with an error (“no such variable”), but the trace procedure will still
320 be invoked.
321
323 In an unset callback to proc, the TCL_TRACE_DESTROYED bit is set in
324 flags if the trace is being removed as part of the deletion. Traces on
325 a variable are always removed whenever the variable is deleted; the
326 only time TCL_TRACE_DESTROYED is not set is for a whole-array trace
327 invoked when only a single element of an array is unset.
328
330 When an interpreter is destroyed, unset traces are called for all of
331 its variables. The TCL_INTERP_DESTROYED bit will be set in the flags
332 argument passed to the trace procedures. Trace procedures must be
333 extremely careful in what they do if the TCL_INTERP_DESTROYED bit is
334 set. It is not safe for the procedures to invoke any Tcl procedures on
335 the interpreter, since its state is partially deleted. All that trace
336 procedures should do under these circumstances is to clean up and free
337 their own internal data structures.
338
340 Tcl does not do any error checking to prevent trace procedures from
341 misusing the interpreter during traces with TCL_INTERP_DESTROYED set.
342
343 Array traces are not yet integrated with the Tcl info exists command,
344 nor is there Tcl-level access to array traces.
345
347 clientData, trace, variable
348
349
350
351Tcl 7.4 Tcl_TraceVar(3)