📄 trace.n
字号:
'\"'\" Copyright (c) 1993 The Regents of the University of California.'\" Copyright (c) 1994-1996 Sun Microsystems, Inc.'\"'\" See the file "license.terms" for information on usage and redistribution'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.'\" '\" SCCS: @(#) trace.n 1.12 96/08/26 13:00:18'\" .so man.macros.TH trace n "" Tcl "Tcl Built-In Commands".BS'\" Note: do not modify the .SH NAME line immediately below!.SH NAMEtrace \- Monitor variable accesses.SH SYNOPSIS\fBtrace \fIoption\fR ?\fIarg arg ...\fR?.BE.SH DESCRIPTION.PPThis command causes Tcl commands to be executed whenever certain operations areinvoked. At present, only variable tracing is implemented. Thelegal \fIoption\fR's (which may be abbreviated) are:.TP\fBtrace variable \fIname ops command\fRArrange for \fIcommand\fR to be executed whenever variable \fIname\fRis accessed in one of the ways given by \fIops\fR. \fIName\fR mayrefer to a normal variable, an element of an array, or to an arrayas a whole (i.e. \fIname\fR may be just the name of an array, with noparenthesized index). If \fIname\fR refers to a whole array, then\fIcommand\fR is invoked whenever any element of the array ismanipulated..RS.PP\fIOps\fR indicates which operations are of interest, and consists ofone or more of the following letters:.TP\fBr\fRInvoke \fIcommand\fR whenever the variable is read..TP\fBw\fRInvoke \fIcommand\fR whenever the variable is written..TP\fBu\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 \fBr\fR, \fBw\fR, or \fBu\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 aTcl procedure, then \fIcommand\fR will have access to the samelocal variables as code in the procedure. This context may bedifferent than the context in which the trace was created.If \fIcommand\fR invokes a procedure (which it normally does) thenthe procedure will have to use \fBupvar\fR or \fBuplevel\fR if itwishes to access the traced variable.Note also that \fIname1\fR may not necessarily be the same as the nameused to set the trace on the variable; differences can occur ifthe access is made through a variable defined with the \fBupvar\fRcommand..PPFor read and write traces, \fIcommand\fR can modifythe variable to affect the result of the traced operation.If \fIcommand\fR modifies the value of a variable during aread or write trace, then the new value will be returned as theresult of the traced operation.The return value from \fIcommand\fR is ignored except thatif it returns an error of any sort then the traced operationalso returns an error withthe same error message returned by the trace command(this mechanism can be used to implement read-only variables, forexample).For write traces, \fIcommand\fR is invoked after the variable'svalue has been changed; it can write a new value into the variableto override the original value specified in the write operation.To implement read-only variables, \fIcommand\fR will have to restorethe old 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 and writes invoked by\fIcommand\fR will occur directly, without invoking \fIcommand\fR(or any other traces) again.However, if \fIcommand\fR unsets the variable then unset traceswill be invoked..PPWhen an unset trace is invoked, the variable has already beendeleted: it will appear to be undefined with no traces.If an unset occurs because of a procedure return, then thetrace will be invoked in the variable context of the procedurebeing returned to: the stack frame of the returning procedurewill no longer exist.Traces are not disabled during unset traces, so if an unset tracecommand creates a new trace and accesses the variable, thetrace will be invoked.Any errors in unset traces are ignored..PPIf there are multiple traces on a variable they are invokedin order of creation, most-recent first.If one trace returns an error, then no further traces areinvoked for the variable.If an array element has a trace set, and there is also a traceset on the array as a whole, the trace on the overall arrayis invoked before the one on the element..PPOnce created, the trace remains in effect either until thetrace is removed with the \fBtrace vdelete\fR command describedbelow, until the variable is unset, or until the interpreteris deleted.Unsetting an element of array will remove any traces on thatelement, but will not remove traces on the overall array..PPThis command returns an empty string..RE.TP\fBtrace vdelete \fIname ops command\fRIf there is a trace set on variable \fIname\fR with theoperations and command given by \fIops\fR and \fIcommand\fR,then the trace is removed, so that \fIcommand\fR will neveragain be invoked.Returns an empty string..TP\fBtrace vinfo \fIname\fRReturns a list containing one element for each tracecurrently set on variable \fIname\fR.Each element of the list is itself a list containing twoelements, which are the \fIops\fR and \fIcommand\fR associatedwith the trace.If \fIname\fR doesn't exist or doesn't have any traces set, thenthe result of the command will be an empty string..SH KEYWORDSread, variable, write, trace, unset
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -