URL
https://opencores.org/ocsvn/or1k/or1k/trunk
Subversion Repositories or1k
[/] [or1k/] [trunk/] [insight/] [tcl/] [doc/] [TraceVar.3] - Rev 1767
Go to most recent revision | Compare with Previous | Blame | View Log
'\"'\" Copyright (c) 1989-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.'\"'\" RCS: @(#) $Id: TraceVar.3,v 1.1.1.1 2002-01-16 10:25:24 markom Exp $'\".so man.macros.TH Tcl_TraceVar 3 7.4 Tcl "Tcl Library Procedures".BS.SH NAMETcl_TraceVar, Tcl_TraceVar2, Tcl_UntraceVar, Tcl_UntraceVar2, Tcl_VarTraceInfo, Tcl_VarTraceInfo2 \- monitor accesses to a variable.SH SYNOPSIS.nf\fB#include <tcl.h>\fR.spint\fBTcl_TraceVar(\fIinterp, varName, flags, proc, clientData\fB)\fR.spint\fBTcl_TraceVar2(\fIinterp, name1, name2, flags, proc, clientData\fB)\fR.sp\fBTcl_UntraceVar(\fIinterp, varName, flags, proc, clientData\fB)\fR.sp\fBTcl_UntraceVar2(\fIinterp, name1, name2, flags, proc, clientData\fB)\fR.spClientData\fBTcl_VarTraceInfo(\fIinterp, varName, flags, proc, prevClientData\fB)\fR.spClientData\fBTcl_VarTraceInfo2(\fIinterp, name1, name2, flags, proc, prevClientData\fB)\fR.SH ARGUMENTS.AS Tcl_VarTraceProc prevClientData.AP Tcl_Interp *interp inInterpreter containing variable..AP char *varName inName of variable. May refer to a scalar variable, toan array variable with no index, or to an array variablewith a parenthesized index.If the name references an element of an array, then itmust be in writable memory: Tcl will make temporary modificationsto it while looking up the name..AP int flags inOR-ed combination of the values TCL_TRACE_READS, TCL_TRACE_WRITES, andTCL_TRACE_UNSETS, TCL_PARSE_PART1, and TCL_GLOBAL_ONLY.Not all flags are used by allprocedures. See below for more information..AP Tcl_VarTraceProc *proc inProcedure to invoke whenever one of the traced operations occurs..AP ClientData clientData inArbitrary one-word value to pass to \fIproc\fR..AP char *name1 inName of scalar or array variable (without array index)..AP char *name2 inFor a trace on an element of an array, gives the index of theelement. For traces on scalar variables or on whole arrays,is NULL..AP ClientData prevClientData inIf non-NULL, gives last value returned by \fBTcl_VarTraceInfo\fR or\fBTcl_VarTraceInfo2\fR, so this call will return information aboutnext trace. If NULL, this call will return information about firsttrace..BE.SH DESCRIPTION.PP\fBTcl_TraceVar\fR allows a C procedure to monitor and controlaccess to a Tcl variable, so that the C procedure is invokedwhenever the variable is read or written or unset.If the trace is created successfully then \fBTcl_TraceVar\fR returnsTCL_OK. If an error occurred (e.g. \fIvarName\fR specifies an elementof an array, but the actual variable isn't an array) then TCL_ERRORis returned and an error message is left in \fIinterp->result\fR..PPThe \fIflags\fR argument to \fBTcl_TraceVar\fR indicates when thetrace procedure is to be invoked and provides informationfor setting up the trace. It consists of an OR-ed combinationof any of the following values:.TP\fBTCL_GLOBAL_ONLY\fRNormally, the variable will be looked up at the current level ofprocedure call; if this bit is set then the variable will be lookedup at global level, ignoring any active procedures..TP\fBTCL_TRACE_READS\fRInvoke \fIproc\fR whenever an attempt is made to read the variable..TP\fBTCL_TRACE_WRITES\fRInvoke \fIproc\fR whenever an attempt is made to modify the variable..TP\fBTCL_TRACE_UNSETS\fRInvoke \fIproc\fR whenever the variable is unset.A variable may be unset either explicitly by an \fBunset\fR command,or implicitly when a procedure returns (its local variables areautomatically unset) or when the interpreter is deleted (allvariables are automatically unset)..PPWhenever one of the specified operations occurs on the variable,\fIproc\fR will be invoked.It should have arguments and result that match the type\fBTcl_VarTraceProc\fR:.CStypedef char *Tcl_VarTraceProc(ClientData \fIclientData\fR,Tcl_Interp *\fIinterp\fR,char *\fIname1\fR,char *\fIname2\fR,int \fIflags\fR);.CEThe \fIclientData\fR and \fIinterp\fR parameters willhave the same values as those passed to \fBTcl_TraceVar\fR when thetrace was created.\fIClientData\fR typically points to an application-specificdata structure that describes what to do when \fIproc\fRis invoked.\fIName1\fR and \fIname2\fR give the name of the traced variablein the normal two-part form (see the description of \fBTcl_TraceVar2\fRbelow for details).\fIFlags\fR is an OR-ed combination of bits providing severalpieces of information.One of the bits TCL_TRACE_READS, TCL_TRACE_WRITES, or TCL_TRACE_UNSETSwill be set in \fIflags\fR to indicate which operation is being performedon the variable.The bit TCL_GLOBAL_ONLY will be set whenever the variable beingaccessed is a global one not accessible from the current level ofprocedure call: the trace procedure will need to pass this flagback to variable-related procedures like \fBTcl_GetVar\fR if itattempts to access the variable.The bit TCL_TRACE_DESTROYED will be set in \fIflags\fR if the trace isabout to be destroyed; this information may be useful to \fIproc\fRso that it can clean up its own internal data structures (seethe section TCL_TRACE_DESTROYED below for more details).Lastly, the bit TCL_INTERP_DESTROYED will be set if the entireinterpreter is being destroyed.When this bit is set, \fIproc\fR must be especially careful inthe things it does (see the section TCL_INTERP_DESTROYED below).The trace procedure's return value should normally be NULL; seeERROR RETURNS below for information on other possibilities..PP\fBTcl_UntraceVar\fR may be used to remove a trace.If the variable specified by \fIinterp\fR, \fIvarName\fR, and \fIflags\fRhas a trace set with \fIflags\fR, \fIproc\fR, and\fIclientData\fR, then the corresponding trace is removed.If no such trace exists, then the call to \fBTcl_UntraceVar\fRhas no effect.The same bits are valid for \fIflags\fR as for calls to \fBTcl_TraceVar\fR..PP\fBTcl_VarTraceInfo\fR may be used to retrieve information abouttraces set on a given variable.The return value from \fBTcl_VarTraceInfo\fR is the \fIclientData\fRassociated with a particular trace.The trace must be on the variable specified by the \fIinterp\fR,\fIvarName\fR, and \fIflags\fR arguments (only the TCL_GLOBAL_ONLYbit from \fIflags\fR is used; other bits are ignored) and its trace proceduremust the same as the \fIproc\fR argument.If the \fIprevClientData\fR argument is NULL then the returnvalue corresponds to the first (most recently created) matchingtrace, or NULL if there are no matching traces.If the \fIprevClientData\fR argument isn't NULL, then it shouldbe the return value from a previous call to \fBTcl_VarTraceInfo\fR.In this case, the new return value will correspond to the nextmatching trace after the one whose \fIclientData\fR matches\fIprevClientData\fR, or NULL if no trace matches \fIprevClientData\fRor if there are no more matching traces after it.This mechanism makes it possible to step through all of thetraces for a given variable that have the same \fIproc\fR..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.If \fIname2\fR is NULL it 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).As a special case, if the flag TCL_PARSE_PART1 is specified,\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 the flag TCL_PARSE_PART1 is given,\fIname2\fR should be NULL since the array and element namesare taken from \fIname1\fR..SH "ACCESSING VARIABLES DURING TRACES".PPDuring read and write 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 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.If 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..SH KEYWORDSclientData, trace, variable
Go to most recent revision | Compare with Previous | Blame | View Log
