Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/TraceVar.3 @ 42

Last change on this file since 42 was 25, checked in by landauf, 17 years ago

added tcl to libs

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