trace.n
来自「tcl是工具命令语言」· N 代码 · 共 362 行 · 第 1/2 页
N
362 行
.TP\fBarray\fRInvoke \fIcommand\fR whenever the variable is accessed or modified viathe \fBarray\fR command, provided that \fIname\fR is not a scalarvariable at the time that the \fBarray\fR command is invoked. If\fIname\fR is a scalar variable, the access via the \fBarray\fRcommand will not trigger the trace..TP\fBread\fRInvoke \fIcommand\fR whenever the variable is read..TP\fBwrite\fRInvoke \fIcommand\fR whenever the variable is written..TP\fBunset\fRInvoke \fIcommand\fR whenever the variable is unset. Variablescan be unset explicitly with the \fBunset\fR command, orimplicitly when procedures return (all of their local variablesare unset). Variables are also unset when interpreters aredeleted, but traces will not be invoked because there is nointerpreter in which to execute them..PPWhen the trace triggers, three arguments are appended to\fIcommand\fR so that the actual command is as follows:.CS\fIcommand name1 name2 op\fR.CE\fIName1\fR and \fIname2\fR give the name(s) for the variablebeing accessed: if the variable is a scalar then \fIname1\fRgives the variable's name and \fIname2\fR is an empty string;if the variable is an array element then \fIname1\fR gives thename of the array and name2 gives the index into the array;if an entire array is being deleted and the trace was registeredon the overall array, rather than a single element, then \fIname1\fRgives the array name and \fIname2\fR is an empty string.\fIName1\fR and \fIname2\fR are not necessarily the same as thename used in the \fBtrace variable\fR command: the \fBupvar\fRcommand allows a procedure to reference a variable under adifferent name.\fIOp\fR indicates what operation is being performed on thevariable, and is one of \fBread\fR, \fBwrite\fR, or \fBunset\fR asdefined above..PP\fICommand\fR executes in the same context as the code that invokedthe traced operation: if the variable was accessed as part of a Tclprocedure, then \fIcommand\fR will have access to the same localvariables as code in the procedure. This context may be differentthan the context in which the trace was created. If \fIcommand\fRinvokes a procedure (which it normally does) then the procedure willhave to use \fBupvar\fR or \fBuplevel\fR if it wishes to access thetraced variable. Note also that \fIname1\fR may not necessarily bethe same as the name used to set the trace on the variable;differences can occur if the access is made through a variable definedwith the \fBupvar\fR command..PPFor read and write traces, \fIcommand\fR can modify the variable toaffect the result of the traced operation. If \fIcommand\fR modifiesthe value of a variable during a read or write trace, then the newvalue will be returned as the result of the traced operation. Thereturn value from \fIcommand\fR is ignored except that if it returnsan error of any sort then the traced operation also returns an errorwith the same error message returned by the trace command (thismechanism can be used to implement read-only variables, for example).For write traces, \fIcommand\fR is invoked after the variable's valuehas been changed; it can write a new value into the variable tooverride the original value specified in the write operation. Toimplement read-only variables, \fIcommand\fR will have to restore theold value of the variable..PPWhile \fIcommand\fR is executing during a read or write trace, traceson the variable are temporarily disabled. This means that reads andwrites invoked by \fIcommand\fR will occur directly, without invoking\fIcommand\fR (or any other traces) again. However, if \fIcommand\fRunsets the variable then unset traces will be invoked..PPWhen an unset trace is invoked, the variable has already been deleted:it will appear to be undefined with no traces. If an unset occursbecause of a procedure return, then the trace will be invoked in thevariable context of the procedure being returned to: the stack frameof the returning procedure will no longer exist. Traces are notdisabled during unset traces, so if an unset trace command creates anew trace and accesses the variable, the trace will be invoked. Anyerrors in unset traces are ignored..PPIf there are multiple traces on a variable they are invoked in orderof creation, most-recent first. If one trace returns an error, thenno further traces are invoked for the variable. If an array elementhas a trace set, and there is also a trace set on the array as awhole, the trace on the overall array is invoked before the one on theelement..PPOnce created, the trace remains in effect either until the trace isremoved with the \fBtrace remove variable\fR command described below,until the variable is unset, or until the interpreter is deleted.Unsetting an element of array will remove any traces on that element,but will not remove traces on the overall array..PPThis command returns an empty string..RE.RE.TP\fBtrace remove \fItype name opList command\fRWhere \fItype\fR is either \fBcommand\fR, \fBexecution\fR or \fBvariable\fR..RS.TP\fBtrace remove command\fI name opList command\fRIf there is a trace set on command \fIname\fR with the operations andcommand given by \fIopList\fR and \fIcommand\fR, then the trace isremoved, so that \fIcommand\fR will never again be invoked. Returnsan empty string. If \fIname\fR doesn't exist, the command will throwan error..TP\fBtrace remove execution\fI name opList command\fRIf there is a trace set on command \fIname\fR with the operations andcommand given by \fIopList\fR and \fIcommand\fR, then the trace isremoved, so that \fIcommand\fR will never again be invoked. Returnsan empty string. If \fIname\fR doesn't exist, the command will throwan error..TP\fBtrace remove variable\fI name opList command\fRIf there is a trace set on variable \fIname\fR with the operations andcommand given by \fIopList\fR and \fIcommand\fR, then the trace isremoved, so that \fIcommand\fR will never again be invoked. Returnsan empty string..RE.TP\fBtrace info \fItype name\fRWhere \fItype\fR is either \fBcommand\fR, \fBexecution\fR or \fBvariable\fR..RS.TP\fBtrace info command\fI name\fRReturns a list containing one element for each trace currently set oncommand \fIname\fR. Each element of the list is itself a listcontaining two elements, which are the \fIopList\fR and \fIcommand\fRassociated with the trace. If \fIname\fR doesn't have any traces set,then the result of the command will be an empty string. If \fIname\fRdoesn't exist, the command will throw an error..TP\fBtrace info execution\fI name\fRReturns a list containing one element for each trace currently set oncommand \fIname\fR. Each element of the list is itself a listcontaining two elements, which are the \fIopList\fR and \fIcommand\fRassociated with the trace. If \fIname\fR doesn't have any traces set,then the result of the command will be an empty string. If \fIname\fRdoesn't exist, the command will throw an error..TP\fBtrace info variable\fI name\fRReturns a list containing one element for each trace currently set onvariable \fIname\fR. Each element of the list is itself a listcontaining two elements, which are the \fIopList\fR and \fIcommand\fRassociated with the trace. If \fIname\fR doesn't exist or doesn'thave any traces set, then the result of the command will be an emptystring..RE.PPFor backwards compatibility, three other subcommands are available:.RS.TP\fBtrace variable \fIname ops command\fRThis is equivalent to \fBtrace add variable \fIname ops command\fR..TP\fBtrace vdelete \fIname ops command\fRThis is equivalent to \fBtrace remove variable \fIname ops command\fR.TP \fBtrace vinfo \fIname\fR This is equivalent to \fBtrace info variable \fIname\fR.RE.PPThese subcommands are deprecated and will likely be removed in afuture version of Tcl. They use an older syntax in which \fBarray\fR,\fBread\fR, \fBwrite\fR, \fBunset\fR are replaced by \fBa\fR, \fBr\fR,\fBw\fR and \fBu\fR respectively, and the \fIops\fR argument is not alist, but simply a string concatenation of the operations, such as\fBrwua\fR..SH "SEE ALSO"set(n), unset(n).SH KEYWORDSread, command, rename, variable, write, trace, unset
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?