1mNAME0m
       trace - Monitor variable accesses

1mSYNOPSIS0m
       1mtrace 4m22moption24m ?4marg24m 4marg24m 4m...24m?


1mDESCRIPTION0m
       This command causes Tcl commands to be executed whenever certain opera-
       tions are invoked.  At present, only variable tracing  is  implemented.
       The legal 4moption24m's (which may be abbreviated) are:

       1mtrace variable 4m22mname24m 4mops24m 4mcommand0m
              Arrange  for  4mcommand24m  to  be executed whenever variable 4mname24m is
              accessed in one of the ways given by 4mops24m.  4mName24m may refer  to  a
              normal  variable,  an  element  of an array, or to an array as a
              whole (i.e. 4mname24m may be just the  name  of  an  array,  with  no
              parenthesized  index).   If  4mname24m  refers to a whole array, then
              4mcommand24m is invoked whenever any element of the array is  manipu-
              lated.   If  the variable does not exist, it will be created but
              will not be given a value, so it will be  visible  to  1mnamespace0m
              1mwhich 22mqueries, but not to 1minfo exists 22mqueries.

              4mOps24m  indicates which operations are of interest, and consists of
              one or more of the following letters:

              1mr      22mInvoke 4mcommand24m whenever the variable is read.

              1mw      22mInvoke 4mcommand24m whenever the variable is written.

              1mu      22mInvoke 4mcommand24m whenever the variable is unset.  Variables
                     can  be  unset  explicitly  with  the  1munset  22mcommand, or
                     implicitly when procedures return  (all  of  their  local
                     variables  are  unset).   Variables  are  also unset when
                     interpreters are deleted, but traces will not be  invoked
                     because there is no interpreter in which to execute them.

              When the trace triggers, three arguments are appended to 4mcommand0m
              so that the actual command is as follows:
                     4mcommand24m 4mname124m 4mname224m 4mop0m
              4mName124m  and  4mname224m  give  the  name(s)  for  the  variable  being
              accessed:  if the variable is a  scalar  then  4mname124m  gives  the
              variable's name and 4mname224m is an empty string; if the variable is
              an array element then 4mname124m gives the  name  of  the  array  and
              name2  gives  the  index  into  the array; if an entire array is
              being deleted and the trace was registered on the overall array,
              rather  than  a  single element, then 4mname124m gives the array name
              and 4mname224m is an empty string.  4mName124m and 4mname224m are not necessar-
              ily  the  same  as  the name used in the 1mtrace variable 22mcommand:
              the 1mupvar 22mcommand allows a procedure  to  reference  a  variable
              under  a  different  name.  4mOp24m indicates what operation is being
              performed on the variable, and is one of 1mr22m, 1mw22m, or 1mu  22mas  defined
              above.

              4mCommand24m  executes  in  the same context as the code that invoked
              the traced operation:  if the variable was accessed as part of a
              Tcl  procedure,  then 4mcommand24m will have access to the same local
              variables as code in the procedure.  This context may be differ-
              ent than the context in which the trace was created.  If 4mcommand0m
              invokes a procedure (which it normally does) then the  procedure
              will  have  to  use  1mupvar 22mor 1muplevel 22mif it wishes to access the
              traced variable.  Note also that 4mname124m may  not  necessarily  be
              the  same  as  the  name  used to set the trace on the variable;
              differences can occur if the access is made through  a  variable
              defined with the 1mupvar 22mcommand.

              For  read  and  write traces, 4mcommand24m can modify the variable to
              affect the result of the traced operation.  If 4mcommand24m  modifies
              the  value  of a variable during a read or write trace, then the
              new value will be returned as the result of  the  traced  opera-
              tion.   The return value from  4mcommand24m is ignored except that if
              it returns an error of any sort then the traced  operation  also
              returns  an  error  with  the same error message returned by the
              trace command (this mechanism can be used to implement read-only
              variables,  for  example).  For write traces, 4mcommand24m is invoked
              after the variable's value has been changed; it can write a  new
              value into the variable to override the original value specified
              in the write operation.  To implement read-only variables,  4mcom-0m
              4mmand24m will have to restore the old value of the variable.

              While  4mcommand24m is executing during a read or write trace, traces
              on the variable are temporarily disabled.  This means that reads
              and  writes  invoked  by  4mcommand24m  will  occur directly, without
              invoking 4mcommand24m (or any other traces) again.  However, if  4mcom-0m
              4mmand24m unsets the variable then unset traces will be invoked.

              When  an  unset  trace is invoked, the variable has already been
              deleted:  it will appear to be undefined with no traces.  If  an
              unset  occurs because of a procedure return, then the trace will
              be invoked in  the  variable  context  of  the  procedure  being
              returned to:  the stack frame of the returning procedure will no
              longer exist.  Traces are not disabled during unset  traces,  so
              if  an  unset trace command creates a new trace and accesses the
              variable, the trace will be invoked.  Any errors in unset traces
              are ignored.

              If  there  are multiple traces on a variable they are invoked in
              order of creation, most-recent first.  If one trace  returns  an
              error,  then no further traces are invoked for the variable.  If
              an array element has a trace set, and there is also a trace  set
              on  the  array  as  a  whole,  the trace on the overall array is
              invoked before the one on the element.

              Once created, the trace remains in effect either until the trace
              is removed with the 1mtrace vdelete 22mcommand 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.

              This command returns an empty string.

       1mtrace vdelete 4m22mname24m 4mops24m 4mcommand0m
              If there is a trace set on variable 4mname24m with the operations and
              command  given by 4mops24m and 4mcommand24m, then the trace is removed, so
              that 4mcommand24m will never again  be  invoked.   Returns  an  empty
              string.

       1mtrace vinfo 4m22mname0m
              Returns  a  list containing one element for each trace currently
              set on variable 4mname24m.  Each element of the list is itself a list
              containing  two  elements, which are the 4mops24m and 4mcommand24m associ-
              ated with the trace.  If 4mname24m doesn't exist or doesn't have  any
              traces  set,  then  the  result  of the command will be an empty
              string.


1mKEYWORDS0m
