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