📄 tracevar.3
字号:
.SH "TWO-PART NAMES".PPThe procedures \fBTcl_TraceVar2\fR, \fBTcl_UntraceVar2\fR, and\fBTcl_VarTraceInfo2\fR are identical to \fBTcl_TraceVar\fR,\fBTcl_UntraceVar\fR, and \fBTcl_VarTraceInfo\fR, respectively,except that the name of the variable consists of two parts.\fIName1\fR gives the name of a scalar variable or array,and \fIname2\fR gives the name of an element within an array..VS 8.1When \fIname2\fR is NULL, \fIname1\fR may contain both an array and an element name:if the name contains an open parenthesis and ends with aclose parenthesis, then the value between the parentheses istreated as an element name (which can have any string value) andthe characters before the first openparenthesis are treated as the name of an array variable.If \fIname2\fR is NULL and \fIname1\fR does not referto an array element .VEit means that either the variable isa scalar or the trace is to be set on the entire array ratherthan an individual element (see WHOLE-ARRAY TRACES below formore information). .SH "ACCESSING VARIABLES DURING TRACES".PPDuring read, write, and array traces, thetrace procedure can read, write, or unset the tracedvariable using \fBTcl_GetVar2\fR, \fBTcl_SetVar2\fR, andother procedures.While \fIproc\fR is executing, traces are temporarily disabledfor the variable, so that calls to \fBTcl_GetVar2\fR and\fBTcl_SetVar2\fR will not cause \fIproc\fR or other trace proceduresto be invoked again.Disabling only occurs for the variable whose trace procedureis active; accesses to other variables will still be traced.However, if a variable is unset during a read or write trace then unsettraces will be invoked..PPDuring unset traces the variable has already been completelyexpunged.It is possible for the trace procedure to read or write thevariable, but this will be a new version of the variable.Traces are not disabled during unset traces as they are forread and write traces, but existing traces have been removedfrom the variable before any trace procedures are invoked.If new traces are set by unset trace procedures, these traceswill be invoked on accesses to the variable by the traceprocedures..SH "CALLBACK TIMING".PPWhen read tracing has been specified for a variable, the traceprocedure will be invoked whenever the variable's value isread. This includes \fBset\fR Tcl commands, \fB$\fR-notationin Tcl commands, and invocations of the \fBTcl_GetVar\fRand \fBTcl_GetVar2\fR procedures.\fIProc\fR is invoked just before the variable's value isreturned.It may modify the value of the variable to affect whatis returned by the traced access.If it unsets the variable then the access will return an errorjust as if the variable never existed..PPWhen write tracing has been specified for a variable, thetrace procedure will be invoked whenever the variable's valueis modified. This includes \fBset\fR commands,commands that modify variables as side effects (such as\fBcatch\fR and \fBscan\fR), and calls to the \fBTcl_SetVar\fRand \fBTcl_SetVar2\fR procedures).\fIProc\fR will be invoked after the variable's value has beenmodified, but before the new value of the variable has beenreturned.It may modify the value of the variable to override the changeand to determine the value actually returned by the tracedaccess.If it deletes the variable then the traced access will returnan empty string..PPWhen array tracing has been specified, the trace procedurewill be invoked at the beginning of the array command implementation,before any of the operations like get, set, or names have been invoked.The trace procedure can modify the array elements with \fBTcl_SetVar\fRand \fBTcl_SetVar2\fR..PPWhen unset tracing has been specified, the trace procedurewill be invoked whenever the variable is destroyed.The traces will be called after the variable has beencompletely unset..SH "WHOLE-ARRAY TRACES".PPIf a call to \fBTcl_TraceVar\fR or \fBTcl_TraceVar2\fR specifiesthe name of an array variable without an index into the array,then the trace will be set on the array as a whole.This means that \fIproc\fR will be invoked whenever anyelement of the array is accessed in the ways specified by\fIflags\fR.When an array is unset, a whole-array trace will be invokedjust once, with \fIname1\fR equal to the name of the arrayand \fIname2\fR NULL; it will not be invoked once for eachelement..SH "MULTIPLE TRACES".PPIt is possible for multiple traces to exist on the same variable.When this happens, all of the trace procedures will be invoked on eachaccess, in order from most-recently-created to least-recently-created.When there exist whole-array traces for an array as well astraces on individual elements, the whole-array traces are invokedbefore the individual-element traces.If a read or write trace unsets the variable then all of the unsettraces will be invoked but the remainder of the read and write traceswill be skipped..SH "ERROR RETURNS".PPUnder normal conditions trace procedures should return NULL, indicatingsuccessful completion.If \fIproc\fR returns a non-NULL value it signifies that anerror occurred.The return value must be a pointer to a static character stringcontaining an error message,.VS 8.4unless (\fIexactly\fR one of) the TCL_TRACE_RESULT_DYNAMIC andTCL_TRACE_RESULT_OBJECT flags is set, which specify that the result iseither a dynamic string (to be released with \fBckfree\fR) or aTcl_Obj* (cast to char* and to be released with\fBTcl_DecrRefCount\fR) containing the error message..VE 8.4If a trace procedure returns an error, no further traces areinvoked for the access and the traced access aborts with thegiven message.Trace procedures can use this facility to make variablesread-only, for example (but note that the value of the variablewill already have been modified before the trace procedure iscalled, so the trace procedure will have to restore the correctvalue)..PPThe return value from \fIproc\fR is only used during read andwrite tracing.During unset traces, the return value is ignored and all relevanttrace procedures will always be invoked..SH "RESTRICTIONS".PPA trace procedure can be called at any time, even when thereis a partially-formed result in the interpreter's result area. Ifthe trace procedure does anything that could damage this result (suchas calling \fBTcl_Eval\fR) then it must save the original values ofthe interpreter's \fBresult\fR and \fBfreeProc\fR fields and restorethem before it returns..SH "UNDEFINED VARIABLES".PPIt is legal to set a trace on an undefined variable.The variable will still appear to be undefined until thefirst time its value is set.If an undefined variable is traced and then unset, the unset will failwith an error (``no such variable''), but the traceprocedure will still be invoked..SH "TCL_TRACE_DESTROYED FLAG".PPIn an unset callback to \fIproc\fR, the TCL_TRACE_DESTROYED bitis set in \fIflags\fR if the trace is being removed as partof the deletion.Traces on a variable are always removed whenever the variableis deleted; the only time TCL_TRACE_DESTROYED isn't set is fora whole-array trace invoked when only a single element of anarray is unset..SH "TCL_INTERP_DESTROYED".PPWhen an interpreter is destroyed, unset traces are called forall of its variables.The TCL_INTERP_DESTROYED bit will be set in the \fIflags\fRargument passed to the trace procedures.Trace procedures must be extremely careful in what they do ifthe TCL_INTERP_DESTROYED bit is set.It is not safe for the procedures to invoke any Tcl procedureson the interpreter, since its state is partially deleted.All that trace procedures should do under these circumstances isto clean up and free their own internal data structures..SH BUGS.PPTcl doesn't do any error checking to prevent trace proceduresfrom misusing the interpreter during traces with TCL_INTERP_DESTROYEDset..PPArray traces are not yet integrated with the Tcl "info exists" command,nor is there Tcl-level access to array traces..SH KEYWORDSclientData, trace, variable
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -