1 |
578 |
markom |
'\"
|
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.1.1.1 2002-01-16 10:25:24 markom Exp $
|
9 |
|
|
'\"
|
10 |
|
|
.so man.macros
|
11 |
|
|
.TH Tcl_TraceVar 3 7.4 Tcl "Tcl Library Procedures"
|
12 |
|
|
.BS
|
13 |
|
|
.SH NAME
|
14 |
|
|
Tcl_TraceVar, Tcl_TraceVar2, Tcl_UntraceVar, Tcl_UntraceVar2, Tcl_VarTraceInfo, Tcl_VarTraceInfo2 \- monitor accesses to a variable
|
15 |
|
|
.SH SYNOPSIS
|
16 |
|
|
.nf
|
17 |
|
|
\fB#include \fR
|
18 |
|
|
.sp
|
19 |
|
|
int
|
20 |
|
|
\fBTcl_TraceVar(\fIinterp, varName, flags, proc, clientData\fB)\fR
|
21 |
|
|
.sp
|
22 |
|
|
int
|
23 |
|
|
\fBTcl_TraceVar2(\fIinterp, name1, name2, flags, proc, clientData\fB)\fR
|
24 |
|
|
.sp
|
25 |
|
|
\fBTcl_UntraceVar(\fIinterp, varName, flags, proc, clientData\fB)\fR
|
26 |
|
|
.sp
|
27 |
|
|
\fBTcl_UntraceVar2(\fIinterp, name1, name2, flags, proc, clientData\fB)\fR
|
28 |
|
|
.sp
|
29 |
|
|
ClientData
|
30 |
|
|
\fBTcl_VarTraceInfo(\fIinterp, varName, flags, proc, prevClientData\fB)\fR
|
31 |
|
|
.sp
|
32 |
|
|
ClientData
|
33 |
|
|
\fBTcl_VarTraceInfo2(\fIinterp, name1, name2, flags, proc, prevClientData\fB)\fR
|
34 |
|
|
.SH ARGUMENTS
|
35 |
|
|
.AS Tcl_VarTraceProc prevClientData
|
36 |
|
|
.AP Tcl_Interp *interp in
|
37 |
|
|
Interpreter containing variable.
|
38 |
|
|
.AP char *varName in
|
39 |
|
|
Name of variable. May refer to a scalar variable, to
|
40 |
|
|
an array variable with no index, or to an array variable
|
41 |
|
|
with a parenthesized index.
|
42 |
|
|
If the name references an element of an array, then it
|
43 |
|
|
must be in writable memory: Tcl will make temporary modifications
|
44 |
|
|
to it while looking up the name.
|
45 |
|
|
.AP int flags in
|
46 |
|
|
OR-ed combination of the values TCL_TRACE_READS, TCL_TRACE_WRITES, and
|
47 |
|
|
TCL_TRACE_UNSETS, TCL_PARSE_PART1, and TCL_GLOBAL_ONLY.
|
48 |
|
|
Not all flags are used by all
|
49 |
|
|
procedures. See below for more information.
|
50 |
|
|
.AP Tcl_VarTraceProc *proc in
|
51 |
|
|
Procedure to invoke whenever one of the traced operations occurs.
|
52 |
|
|
.AP ClientData clientData in
|
53 |
|
|
Arbitrary one-word value to pass to \fIproc\fR.
|
54 |
|
|
.AP char *name1 in
|
55 |
|
|
Name of scalar or array variable (without array index).
|
56 |
|
|
.AP char *name2 in
|
57 |
|
|
For a trace on an element of an array, gives the index of the
|
58 |
|
|
element. For traces on scalar variables or on whole arrays,
|
59 |
|
|
is NULL.
|
60 |
|
|
.AP ClientData prevClientData in
|
61 |
|
|
If non-NULL, gives last value returned by \fBTcl_VarTraceInfo\fR or
|
62 |
|
|
\fBTcl_VarTraceInfo2\fR, so this call will return information about
|
63 |
|
|
next trace. If NULL, this call will return information about first
|
64 |
|
|
trace.
|
65 |
|
|
.BE
|
66 |
|
|
|
67 |
|
|
.SH DESCRIPTION
|
68 |
|
|
.PP
|
69 |
|
|
\fBTcl_TraceVar\fR allows a C procedure to monitor and control
|
70 |
|
|
access to a Tcl variable, so that the C procedure is invoked
|
71 |
|
|
whenever the variable is read or written or unset.
|
72 |
|
|
If the trace is created successfully then \fBTcl_TraceVar\fR returns
|
73 |
|
|
TCL_OK. If an error occurred (e.g. \fIvarName\fR specifies an element
|
74 |
|
|
of an array, but the actual variable isn't an array) then TCL_ERROR
|
75 |
|
|
is returned and an error message is left in \fIinterp->result\fR.
|
76 |
|
|
.PP
|
77 |
|
|
The \fIflags\fR argument to \fBTcl_TraceVar\fR indicates when the
|
78 |
|
|
trace procedure is to be invoked and provides information
|
79 |
|
|
for setting up the trace. It consists of an OR-ed combination
|
80 |
|
|
of any of the following values:
|
81 |
|
|
.TP
|
82 |
|
|
\fBTCL_GLOBAL_ONLY\fR
|
83 |
|
|
Normally, the variable will be looked up at the current level of
|
84 |
|
|
procedure call; if this bit is set then the variable will be looked
|
85 |
|
|
up at global level, ignoring any active procedures.
|
86 |
|
|
.TP
|
87 |
|
|
\fBTCL_TRACE_READS\fR
|
88 |
|
|
Invoke \fIproc\fR whenever an attempt is made to read the variable.
|
89 |
|
|
.TP
|
90 |
|
|
\fBTCL_TRACE_WRITES\fR
|
91 |
|
|
Invoke \fIproc\fR whenever an attempt is made to modify the variable.
|
92 |
|
|
.TP
|
93 |
|
|
\fBTCL_TRACE_UNSETS\fR
|
94 |
|
|
Invoke \fIproc\fR whenever the variable is unset.
|
95 |
|
|
A variable may be unset either explicitly by an \fBunset\fR command,
|
96 |
|
|
or implicitly when a procedure returns (its local variables are
|
97 |
|
|
automatically unset) or when the interpreter is deleted (all
|
98 |
|
|
variables are automatically unset).
|
99 |
|
|
.PP
|
100 |
|
|
Whenever one of the specified operations occurs on the variable,
|
101 |
|
|
\fIproc\fR will be invoked.
|
102 |
|
|
It should have arguments and result that match the type
|
103 |
|
|
\fBTcl_VarTraceProc\fR:
|
104 |
|
|
.CS
|
105 |
|
|
typedef char *Tcl_VarTraceProc(
|
106 |
|
|
ClientData \fIclientData\fR,
|
107 |
|
|
Tcl_Interp *\fIinterp\fR,
|
108 |
|
|
char *\fIname1\fR,
|
109 |
|
|
char *\fIname2\fR,
|
110 |
|
|
int \fIflags\fR);
|
111 |
|
|
.CE
|
112 |
|
|
The \fIclientData\fR and \fIinterp\fR parameters will
|
113 |
|
|
have the same values as those passed to \fBTcl_TraceVar\fR when the
|
114 |
|
|
trace was created.
|
115 |
|
|
\fIClientData\fR typically points to an application-specific
|
116 |
|
|
data structure that describes what to do when \fIproc\fR
|
117 |
|
|
is invoked.
|
118 |
|
|
\fIName1\fR and \fIname2\fR give the name of the traced variable
|
119 |
|
|
in the normal two-part form (see the description of \fBTcl_TraceVar2\fR
|
120 |
|
|
below for details).
|
121 |
|
|
\fIFlags\fR is an OR-ed combination of bits providing several
|
122 |
|
|
pieces of information.
|
123 |
|
|
One of the bits TCL_TRACE_READS, TCL_TRACE_WRITES, or TCL_TRACE_UNSETS
|
124 |
|
|
will be set in \fIflags\fR to indicate which operation is being performed
|
125 |
|
|
on the variable.
|
126 |
|
|
The bit TCL_GLOBAL_ONLY will be set whenever the variable being
|
127 |
|
|
accessed is a global one not accessible from the current level of
|
128 |
|
|
procedure call: the trace procedure will need to pass this flag
|
129 |
|
|
back to variable-related procedures like \fBTcl_GetVar\fR if it
|
130 |
|
|
attempts to access the variable.
|
131 |
|
|
The bit TCL_TRACE_DESTROYED will be set in \fIflags\fR if the trace is
|
132 |
|
|
about to be destroyed; this information may be useful to \fIproc\fR
|
133 |
|
|
so that it can clean up its own internal data structures (see
|
134 |
|
|
the section TCL_TRACE_DESTROYED below for more details).
|
135 |
|
|
Lastly, the bit TCL_INTERP_DESTROYED will be set if the entire
|
136 |
|
|
interpreter is being destroyed.
|
137 |
|
|
When this bit is set, \fIproc\fR must be especially careful in
|
138 |
|
|
the things it does (see the section TCL_INTERP_DESTROYED below).
|
139 |
|
|
The trace procedure's return value should normally be NULL; see
|
140 |
|
|
ERROR RETURNS below for information on other possibilities.
|
141 |
|
|
.PP
|
142 |
|
|
\fBTcl_UntraceVar\fR may be used to remove a trace.
|
143 |
|
|
If the variable specified by \fIinterp\fR, \fIvarName\fR, and \fIflags\fR
|
144 |
|
|
has a trace set with \fIflags\fR, \fIproc\fR, and
|
145 |
|
|
\fIclientData\fR, then the corresponding trace is removed.
|
146 |
|
|
If no such trace exists, then the call to \fBTcl_UntraceVar\fR
|
147 |
|
|
has no effect.
|
148 |
|
|
The same bits are valid for \fIflags\fR as for calls to \fBTcl_TraceVar\fR.
|
149 |
|
|
.PP
|
150 |
|
|
\fBTcl_VarTraceInfo\fR may be used to retrieve information about
|
151 |
|
|
traces set on a given variable.
|
152 |
|
|
The return value from \fBTcl_VarTraceInfo\fR is the \fIclientData\fR
|
153 |
|
|
associated with a particular trace.
|
154 |
|
|
The trace must be on the variable specified by the \fIinterp\fR,
|
155 |
|
|
\fIvarName\fR, and \fIflags\fR arguments (only the TCL_GLOBAL_ONLY
|
156 |
|
|
bit from \fIflags\fR is used; other bits are ignored) and its trace procedure
|
157 |
|
|
must the same as the \fIproc\fR argument.
|
158 |
|
|
If the \fIprevClientData\fR argument is NULL then the return
|
159 |
|
|
value corresponds to the first (most recently created) matching
|
160 |
|
|
trace, or NULL if there are no matching traces.
|
161 |
|
|
If the \fIprevClientData\fR argument isn't NULL, then it should
|
162 |
|
|
be the return value from a previous call to \fBTcl_VarTraceInfo\fR.
|
163 |
|
|
In this case, the new return value will correspond to the next
|
164 |
|
|
matching trace after the one whose \fIclientData\fR matches
|
165 |
|
|
\fIprevClientData\fR, or NULL if no trace matches \fIprevClientData\fR
|
166 |
|
|
or if there are no more matching traces after it.
|
167 |
|
|
This mechanism makes it possible to step through all of the
|
168 |
|
|
traces for a given variable that have the same \fIproc\fR.
|
169 |
|
|
|
170 |
|
|
.SH "TWO-PART NAMES"
|
171 |
|
|
.PP
|
172 |
|
|
The procedures \fBTcl_TraceVar2\fR, \fBTcl_UntraceVar2\fR, and
|
173 |
|
|
\fBTcl_VarTraceInfo2\fR are identical to \fBTcl_TraceVar\fR,
|
174 |
|
|
\fBTcl_UntraceVar\fR, and \fBTcl_VarTraceInfo\fR, respectively,
|
175 |
|
|
except that the name of the variable consists of two parts.
|
176 |
|
|
\fIName1\fR gives the name of a scalar variable or array,
|
177 |
|
|
and \fIname2\fR gives the name of an element within an array.
|
178 |
|
|
If \fIname2\fR is NULL it means that either the variable is
|
179 |
|
|
a scalar or the trace is to be set on the entire array rather
|
180 |
|
|
than an individual element (see WHOLE-ARRAY TRACES below for
|
181 |
|
|
more information).
|
182 |
|
|
As a special case, if the flag TCL_PARSE_PART1 is specified,
|
183 |
|
|
\fIname1\fR may contain both an array and an element name:
|
184 |
|
|
if the name contains an open parenthesis and ends with a
|
185 |
|
|
close parenthesis, then the value between the parentheses is
|
186 |
|
|
treated as an element name (which can have any string value) and
|
187 |
|
|
the characters before the first open
|
188 |
|
|
parenthesis are treated as the name of an array variable.
|
189 |
|
|
If the flag TCL_PARSE_PART1 is given,
|
190 |
|
|
\fIname2\fR should be NULL since the array and element names
|
191 |
|
|
are taken from \fIname1\fR.
|
192 |
|
|
|
193 |
|
|
.SH "ACCESSING VARIABLES DURING TRACES"
|
194 |
|
|
.PP
|
195 |
|
|
During read and write traces, the
|
196 |
|
|
trace procedure can read, write, or unset the traced
|
197 |
|
|
variable using \fBTcl_GetVar2\fR, \fBTcl_SetVar2\fR, and
|
198 |
|
|
other procedures.
|
199 |
|
|
While \fIproc\fR is executing, traces are temporarily disabled
|
200 |
|
|
for the variable, so that calls to \fBTcl_GetVar2\fR and
|
201 |
|
|
\fBTcl_SetVar2\fR will not cause \fIproc\fR or other trace procedures
|
202 |
|
|
to be invoked again.
|
203 |
|
|
Disabling only occurs for the variable whose trace procedure
|
204 |
|
|
is active; accesses to other variables will still be traced.
|
205 |
|
|
However, if a variable is unset during a read or write trace then unset
|
206 |
|
|
traces will be invoked.
|
207 |
|
|
.PP
|
208 |
|
|
During unset traces the variable has already been completely
|
209 |
|
|
expunged.
|
210 |
|
|
It is possible for the trace procedure to read or write the
|
211 |
|
|
variable, but this will be a new version of the variable.
|
212 |
|
|
Traces are not disabled during unset traces as they are for
|
213 |
|
|
read and write traces, but existing traces have been removed
|
214 |
|
|
from the variable before any trace procedures are invoked.
|
215 |
|
|
If new traces are set by unset trace procedures, these traces
|
216 |
|
|
will be invoked on accesses to the variable by the trace
|
217 |
|
|
procedures.
|
218 |
|
|
|
219 |
|
|
.SH "CALLBACK TIMING"
|
220 |
|
|
.PP
|
221 |
|
|
When read tracing has been specified for a variable, the trace
|
222 |
|
|
procedure will be invoked whenever the variable's value is
|
223 |
|
|
read. This includes \fBset\fR Tcl commands, \fB$\fR-notation
|
224 |
|
|
in Tcl commands, and invocations of the \fBTcl_GetVar\fR
|
225 |
|
|
and \fBTcl_GetVar2\fR procedures.
|
226 |
|
|
\fIProc\fR is invoked just before the variable's value is
|
227 |
|
|
returned.
|
228 |
|
|
It may modify the value of the variable to affect what
|
229 |
|
|
is returned by the traced access.
|
230 |
|
|
If it unsets the variable then the access will return an error
|
231 |
|
|
just as if the variable never existed.
|
232 |
|
|
.PP
|
233 |
|
|
When write tracing has been specified for a variable, the
|
234 |
|
|
trace procedure will be invoked whenever the variable's value
|
235 |
|
|
is modified. This includes \fBset\fR commands,
|
236 |
|
|
commands that modify variables as side effects (such as
|
237 |
|
|
\fBcatch\fR and \fBscan\fR), and calls to the \fBTcl_SetVar\fR
|
238 |
|
|
and \fBTcl_SetVar2\fR procedures).
|
239 |
|
|
\fIProc\fR will be invoked after the variable's value has been
|
240 |
|
|
modified, but before the new value of the variable has been
|
241 |
|
|
returned.
|
242 |
|
|
It may modify the value of the variable to override the change
|
243 |
|
|
and to determine the value actually returned by the traced
|
244 |
|
|
access.
|
245 |
|
|
If it deletes the variable then the traced access will return
|
246 |
|
|
an empty string.
|
247 |
|
|
.PP
|
248 |
|
|
When unset tracing has been specified, the trace procedure
|
249 |
|
|
will be invoked whenever the variable is destroyed.
|
250 |
|
|
The traces will be called after the variable has been
|
251 |
|
|
completely unset.
|
252 |
|
|
|
253 |
|
|
.SH "WHOLE-ARRAY TRACES"
|
254 |
|
|
.PP
|
255 |
|
|
If a call to \fBTcl_TraceVar\fR or \fBTcl_TraceVar2\fR specifies
|
256 |
|
|
the name of an array variable without an index into the array,
|
257 |
|
|
then the trace will be set on the array as a whole.
|
258 |
|
|
This means that \fIproc\fR will be invoked whenever any
|
259 |
|
|
element of the array is accessed in the ways specified by
|
260 |
|
|
\fIflags\fR.
|
261 |
|
|
When an array is unset, a whole-array trace will be invoked
|
262 |
|
|
just once, with \fIname1\fR equal to the name of the array
|
263 |
|
|
and \fIname2\fR NULL; it will not be invoked once for each
|
264 |
|
|
element.
|
265 |
|
|
|
266 |
|
|
.SH "MULTIPLE TRACES"
|
267 |
|
|
.PP
|
268 |
|
|
It is possible for multiple traces to exist on the same variable.
|
269 |
|
|
When this happens, all of the trace procedures will be invoked on each
|
270 |
|
|
access, in order from most-recently-created to least-recently-created.
|
271 |
|
|
When there exist whole-array traces for an array as well as
|
272 |
|
|
traces on individual elements, the whole-array traces are invoked
|
273 |
|
|
before the individual-element traces.
|
274 |
|
|
If a read or write trace unsets the variable then all of the unset
|
275 |
|
|
traces will be invoked but the remainder of the read and write traces
|
276 |
|
|
will be skipped.
|
277 |
|
|
|
278 |
|
|
.SH "ERROR RETURNS"
|
279 |
|
|
.PP
|
280 |
|
|
Under normal conditions trace procedures should return NULL, indicating
|
281 |
|
|
successful completion.
|
282 |
|
|
If \fIproc\fR returns a non-NULL value it signifies that an
|
283 |
|
|
error occurred.
|
284 |
|
|
The return value must be a pointer to a static character string
|
285 |
|
|
containing an error message.
|
286 |
|
|
If a trace procedure returns an error, no further traces are
|
287 |
|
|
invoked for the access and the traced access aborts with the
|
288 |
|
|
given message.
|
289 |
|
|
Trace procedures can use this facility to make variables
|
290 |
|
|
read-only, for example (but note that the value of the variable
|
291 |
|
|
will already have been modified before the trace procedure is
|
292 |
|
|
called, so the trace procedure will have to restore the correct
|
293 |
|
|
value).
|
294 |
|
|
.PP
|
295 |
|
|
The return value from \fIproc\fR is only used during read and
|
296 |
|
|
write tracing.
|
297 |
|
|
During unset traces, the return value is ignored and all relevant
|
298 |
|
|
trace procedures will always be invoked.
|
299 |
|
|
|
300 |
|
|
.SH "RESTRICTIONS"
|
301 |
|
|
.PP
|
302 |
|
|
A trace procedure can be called at any time, even when there
|
303 |
|
|
is a partially-formed result in the interpreter's result area. If
|
304 |
|
|
the trace procedure does anything that could damage this result (such
|
305 |
|
|
as calling \fBTcl_Eval\fR) then it must save the original values of
|
306 |
|
|
the interpreter's \fBresult\fR and \fBfreeProc\fR fields and restore
|
307 |
|
|
them before it returns.
|
308 |
|
|
|
309 |
|
|
.SH "UNDEFINED VARIABLES"
|
310 |
|
|
.PP
|
311 |
|
|
It is legal to set a trace on an undefined variable.
|
312 |
|
|
The variable will still appear to be undefined until the
|
313 |
|
|
first time its value is set.
|
314 |
|
|
If an undefined variable is traced and then unset, the unset will fail
|
315 |
|
|
with an error (``no such variable''), but the trace
|
316 |
|
|
procedure will still be invoked.
|
317 |
|
|
|
318 |
|
|
.SH "TCL_TRACE_DESTROYED FLAG"
|
319 |
|
|
.PP
|
320 |
|
|
In an unset callback to \fIproc\fR, the TCL_TRACE_DESTROYED bit
|
321 |
|
|
is set in \fIflags\fR if the trace is being removed as part
|
322 |
|
|
of the deletion.
|
323 |
|
|
Traces on a variable are always removed whenever the variable
|
324 |
|
|
is deleted; the only time TCL_TRACE_DESTROYED isn't set is for
|
325 |
|
|
a whole-array trace invoked when only a single element of an
|
326 |
|
|
array is unset.
|
327 |
|
|
|
328 |
|
|
.SH "TCL_INTERP_DESTROYED"
|
329 |
|
|
.PP
|
330 |
|
|
When an interpreter is destroyed, unset traces are called for
|
331 |
|
|
all of its variables.
|
332 |
|
|
The TCL_INTERP_DESTROYED bit will be set in the \fIflags\fR
|
333 |
|
|
argument passed to the trace procedures.
|
334 |
|
|
Trace procedures must be extremely careful in what they do if
|
335 |
|
|
the TCL_INTERP_DESTROYED bit is set.
|
336 |
|
|
It is not safe for the procedures to invoke any Tcl procedures
|
337 |
|
|
on the interpreter, since its state is partially deleted.
|
338 |
|
|
All that trace procedures should do under these circumstances is
|
339 |
|
|
to clean up and free their own internal data structures.
|
340 |
|
|
|
341 |
|
|
.SH BUGS
|
342 |
|
|
.PP
|
343 |
|
|
Tcl doesn't do any error checking to prevent trace procedures
|
344 |
|
|
from misusing the interpreter during traces with TCL_INTERP_DESTROYED
|
345 |
|
|
set.
|
346 |
|
|
|
347 |
|
|
.SH KEYWORDS
|
348 |
|
|
clientData, trace, variable
|