Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/trace.n @ 37

Last change on this file since 37 was 25, checked in by landauf, 16 years ago

added tcl to libs

File size: 17.6 KB
Line 
1'\"
2'\" Copyright (c) 1993 The Regents of the University of California.
3'\" Copyright (c) 1994-1996 Sun Microsystems, Inc.
4'\" Copyright (c) 2000 Ajuba Solutions.
5'\"
6'\" See the file "license.terms" for information on usage and redistribution
7'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
8'\"
9'\" RCS: @(#) $Id: trace.n,v 1.26 2007/12/13 15:22:33 dgp Exp $
10'\"
11.so man.macros
12.TH trace n "8.4" Tcl "Tcl Built-In Commands"
13.BS
14'\" Note:  do not modify the .SH NAME line immediately below!
15.SH NAME
16trace \- Monitor variable accesses, command usages and command executions
17.SH SYNOPSIS
18\fBtrace \fIoption\fR ?\fIarg arg ...\fR?
19.BE
20.SH DESCRIPTION
21.PP
22This command causes Tcl commands to be executed whenever certain operations are
23invoked.  The legal \fIoption\fRs (which may be abbreviated) are:
24.TP
25\fBtrace add \fItype name ops ?args?\fR
26Where \fItype\fR is \fBcommand\fR, \fBexecution\fR, or \fBvariable\fR.
27.RS
28.TP
29\fBtrace add command\fR \fIname ops commandPrefix\fR
30.
31Arrange for \fIcommandPrefix\fR to be executed (with additional arguments)
32whenever command \fIname\fR is modified in one of the ways given by the list
33\fIops\fR. \fIName\fR will be resolved using the usual namespace resolution
34rules used by commands. If the command does not exist, an error will be
35thrown.
36.RS
37.PP
38\fIOps\fR indicates which operations are of interest, and is a list of
39one or more of the following items:
40.TP
41\fBrename\fR
42.
43Invoke \fIcommandPrefix\fR whenever the traced command is renamed.  Note that
44renaming to the empty string is considered deletion, and will not be traced
45with
46.QW \fBrename\fR .
47.TP
48\fBdelete\fR
49.
50Invoke \fIcommandPrefix\fR when the traced command is deleted. Commands can be
51deleted explicitly by using the \fBrename\fR command to rename the command to
52an empty string. Commands are also deleted when the interpreter is deleted,
53but traces will not be invoked because there is no interpreter in which to
54execute them.
55.PP
56When the trace triggers, depending on the operations being traced, a number of
57arguments are appended to \fIcommandPrefix\fR so that the actual command is as
58follows:
59.CS
60\fIcommandPrefix oldName newName op\fR
61.CE
62\fIOldName\fR and \fInewName\fR give the traced command's current (old) name,
63and the name to which it is being renamed (the empty string if this is a
64.QW delete
65operation).
66\fIOp\fR indicates what operation is being performed on the
67command, and is one of \fBrename\fR or \fBdelete\fR as
68defined above.  The trace operation cannot be used to stop a command
69from being deleted.  Tcl will always remove the command once the trace
70is complete.  Recursive renaming or deleting will not cause further traces
71of the same type to be evaluated, so a delete trace which itself
72deletes the command, or a rename trace which itself renames the
73command will not cause further trace evaluations to occur.
74Both \fIoldName\fR and \fInewName\fR are fully qualified with any namespace(s)
75in which they appear.
76.RE
77.TP
78\fBtrace add execution\fR \fIname ops commandPrefix\fR
79.
80Arrange for \fIcommandPrefix\fR to be executed (with additional arguments)
81whenever command \fIname\fR is executed, with traces occurring at the points
82indicated by the list \fIops\fR.  \fIName\fR will be resolved using the usual
83namespace resolution rules used by commands.  If the command does not exist,
84an error will be thrown.
85.RS
86.PP
87\fIOps\fR indicates which operations are of interest, and is a list of
88one or more of the following items:
89.TP
90\fBenter\fR
91Invoke \fIcommandPrefix\fR whenever the command \fIname\fR is executed,
92just before the actual execution takes place.
93.TP
94\fBleave\fR
95Invoke \fIcommandPrefix\fR whenever the command \fIname\fR is executed,
96just after the actual execution takes place.
97.TP
98\fBenterstep\fR
99.
100Invoke \fIcommandPrefix\fR for every Tcl command which is executed from the
101start of the execution of the procedure \fIname\fR until that
102procedure finishes. \fICommandPrefix\fR is invoked just before the actual
103execution of the Tcl command being reported takes place.  For example
104if we have
105.QW "proc foo {} { puts \N'34'hello\N'34' }" ,
106then an \fIenterstep\fR trace would be invoked just before
107.QW "\fIputs \N'34'hello\N'34'\fR"
108is executed.
109Setting an \fIenterstep\fR trace on a command \fIname\fR that does not refer
110to a procedure will not result in an error and is simply ignored.
111.TP
112\fBleavestep\fR
113.
114Invoke \fIcommandPrefix\fR for every Tcl command which is executed from the
115start of the execution of the procedure \fIname\fR until that
116procedure finishes. \fICommandPrefix\fR is invoked just after the actual
117execution of the Tcl command being reported takes place.
118Setting a \fIleavestep\fR trace on a command \fIname\fR that does not refer to
119a procedure will not result in an error and is simply ignored.
120.PP
121When the trace triggers, depending on the operations being traced, a
122number of arguments are appended to \fIcommandPrefix\fR so that the actual
123command is as follows:
124.PP
125For \fBenter\fR and \fBenterstep\fR operations:
126.CS
127\fIcommandPrefix command-string op\fR
128.CE
129\fICommand-string\fR gives the complete current command being
130executed (the traced command for a \fBenter\fR operation, an
131arbitrary command for a \fBenterstep\fR operation), including
132all arguments in their fully expanded form.
133\fIOp\fR indicates what operation is being performed on the
134command execution, and is one of \fBenter\fR or \fBenterstep\fR as
135defined above.  The trace operation can be used to stop the
136command from executing, by deleting the command in question.  Of
137course when the command is subsequently executed, an
138.QW "invalid command"
139error will occur.
140.PP
141For \fBleave\fR and \fBleavestep\fR operations:
142.CS
143\fIcommand command-string code result op\fR
144.CE
145\fICommand-string\fR gives the complete current command being
146executed (the traced command for a \fBenter\fR operation, an
147arbitrary command for a \fBenterstep\fR operation), including
148all arguments in their fully expanded form.
149\fICode\fR gives the result code of that execution, and \fIresult\fR
150the result string.
151\fIOp\fR indicates what operation is being performed on the
152command execution, and is one of \fBleave\fR or \fBleavestep\fR as
153defined above.
154Note that the creation of many \fBenterstep\fR or
155\fBleavestep\fR traces can lead to unintuitive results, since the
156invoked commands from one trace can themselves lead to further
157command invocations for other traces.
158.PP
159\fICommandPrefix\fR executes in the same context as the code that invoked
160the traced operation: thus the \fIcommandPrefix\fR, if invoked from a
161procedure, will have access to the same local variables as code in the
162procedure. This context may be different than the context in which the trace
163was created. If \fIcommandPrefix\fR invokes a procedure (which it normally
164does) then the procedure will have to use \fBupvar\fR or \fBuplevel\fR
165commands if it wishes to access the local variables of the code which invoked
166the trace operation.
167.PP
168While \fIcommandPrefix\fR is executing during an execution trace, traces
169on \fIname\fR are temporarily disabled. This allows the \fIcommandPrefix\fR
170to execute \fIname\fR in its body without invoking any other traces again.
171If an error occurs while executing the \fIcommandPrefix\fR, then the
172command \fIname\fR as a whole will return that same error.
173.PP
174When multiple traces are set on \fIname\fR, then for \fIenter\fR
175and \fIenterstep\fR operations, the traced commands are invoked
176in the reverse order of how the traces were originally created;
177and for \fIleave\fR and \fIleavestep\fR operations, the traced
178commands are invoked in the original order of creation.
179.PP
180The behavior of execution traces is currently undefined for a command
181\fIname\fR imported into another namespace.
182.RE
183.TP
184\fBtrace add variable\fI name ops commandPrefix\fR
185Arrange for \fIcommandPrefix\fR to be executed whenever variable \fIname\fR
186is accessed in one of the ways given by the list \fIops\fR.  \fIName\fR may
187refer to a normal variable, an element of an array, or to an array
188as a whole (i.e. \fIname\fR may be just the name of an array, with no
189parenthesized index).  If \fIname\fR refers to a whole array, then
190\fIcommandPrefix\fR is invoked whenever any element of the array is
191manipulated.  If the variable does not exist, it will be created but
192will not be given a value, so it will be visible to \fBnamespace which\fR
193queries, but not to \fBinfo exists\fR queries.
194.RS
195.PP
196\fIOps\fR indicates which operations are of interest, and is a list of
197one or more of the following items:
198.TP
199\fBarray\fR
200Invoke \fIcommandPrefix\fR whenever the variable is accessed or modified via
201the \fBarray\fR command, provided that \fIname\fR is not a scalar
202variable at the time that the \fBarray\fR command is invoked.  If
203\fIname\fR is a scalar variable, the access via the \fBarray\fR
204command will not trigger the trace.
205.TP
206\fBread\fR
207Invoke \fIcommandPrefix\fR whenever the variable is read.
208.TP
209\fBwrite\fR
210Invoke \fIcommandPrefix\fR whenever the variable is written.
211.TP
212\fBunset\fR
213Invoke \fIcommandPrefix\fR whenever the variable is unset.  Variables
214can be unset explicitly with the \fBunset\fR command, or
215implicitly when procedures return (all of their local variables
216are unset).  Variables are also unset when interpreters are
217deleted, but traces will not be invoked because there is no
218interpreter in which to execute them.
219.PP
220When the trace triggers, three arguments are appended to
221\fIcommandPrefix\fR so that the actual command is as follows:
222.CS
223\fIcommandPrefix name1 name2 op\fR
224.CE
225\fIName1\fR and \fIname2\fR give the name(s) for the variable
226being accessed:  if the variable is a scalar then \fIname1\fR
227gives the variable's name and \fIname2\fR is an empty string;
228if the variable is an array element then \fIname1\fR gives the
229name of the array and name2 gives the index into the array;
230if an entire array is being deleted and the trace was registered
231on the overall array, rather than a single element, then \fIname1\fR
232gives the array name and \fIname2\fR is an empty string.
233\fIName1\fR and \fIname2\fR are not necessarily the same as the
234name used in the \fBtrace variable\fR command:  the \fBupvar\fR
235command allows a procedure to reference a variable under a
236different name.
237\fIOp\fR indicates what operation is being performed on the
238variable, and is one of \fBread\fR, \fBwrite\fR, or \fBunset\fR as
239defined above.
240.PP
241\fICommandPrefix\fR executes in the same context as the code that invoked
242the traced operation:  if the variable was accessed as part of a Tcl
243procedure, then \fIcommandPrefix\fR will have access to the same local
244variables as code in the procedure.  This context may be different
245than the context in which the trace was created. If \fIcommandPrefix\fR
246invokes a procedure (which it normally does) then the procedure will
247have to use \fBupvar\fR or \fBuplevel\fR if it wishes to access the
248traced variable.  Note also that \fIname1\fR may not necessarily be
249the same as the name used to set the trace on the variable;
250differences can occur if the access is made through a variable defined
251with the \fBupvar\fR command.
252.PP
253For read and write traces, \fIcommandPrefix\fR can modify the variable to
254affect the result of the traced operation.  If \fIcommandPrefix\fR modifies
255the value of a variable during a read or write trace, then the new
256value will be returned as the result of the traced operation.  The
257return value from  \fIcommandPrefix\fR is ignored except that if it returns
258an error of any sort then the traced operation also returns an error
259with the same error message returned by the trace command (this
260mechanism can be used to implement read-only variables, for example).
261For write traces, \fIcommandPrefix\fR is invoked after the variable's value
262has been changed; it can write a new value into the variable to
263override the original value specified in the write operation.  To
264implement read-only variables, \fIcommandPrefix\fR will have to restore the
265old value of the variable.
266.PP
267While \fIcommandPrefix\fR is executing during a read or write trace, traces
268on the variable are temporarily disabled.  This means that reads and
269writes invoked by \fIcommandPrefix\fR will occur directly, without invoking
270\fIcommandPrefix\fR (or any other traces) again.  However, if
271\fIcommandPrefix\fR unsets the variable then unset traces will be invoked.
272.PP
273When an unset trace is invoked, the variable has already been deleted:
274it will appear to be undefined with no traces.  If an unset occurs
275because of a procedure return, then the trace will be invoked in the
276variable context of the procedure being returned to:  the stack frame
277of the returning procedure will no longer exist.  Traces are not
278disabled during unset traces, so if an unset trace command creates a
279new trace and accesses the variable, the trace will be invoked.  Any
280errors in unset traces are ignored.
281.PP
282If there are multiple traces on a variable they are invoked in order
283of creation, most-recent first.  If one trace returns an error, then
284no further traces are invoked for the variable.  If an array element
285has a trace set, and there is also a trace set on the array as a
286whole, the trace on the overall array is invoked before the one on the
287element.
288.PP
289Once created, the trace remains in effect either until the trace is
290removed with the \fBtrace remove variable\fR command described below,
291until the variable is unset, or until the interpreter is deleted.
292Unsetting an element of array will remove any traces on that element,
293but will not remove traces on the overall array.
294.PP
295This command returns an empty string.
296.RE
297.RE
298.TP
299\fBtrace remove \fItype name opList commandPrefix\fR
300Where \fItype\fR is either \fBcommand\fR, \fBexecution\fR or \fBvariable\fR.
301.RS
302.TP
303\fBtrace remove command\fI name opList commandPrefix\fR
304If there is a trace set on command \fIname\fR with the operations and
305command given by \fIopList\fR and \fIcommandPrefix\fR, then the trace is
306removed, so that \fIcommandPrefix\fR will never again be invoked.  Returns
307an empty string.   If \fIname\fR does not exist, the command will throw
308an error.
309.TP
310\fBtrace remove execution\fI name opList commandPrefix\fR
311If there is a trace set on command \fIname\fR with the operations and
312command given by \fIopList\fR and \fIcommandPrefix\fR, then the trace is
313removed, so that \fIcommandPrefix\fR will never again be invoked.  Returns
314an empty string.   If \fIname\fR does not exist, the command will throw
315an error.
316.TP
317\fBtrace remove variable\fI name opList commandPrefix\fR
318If there is a trace set on variable \fIname\fR with the operations and
319command given by \fIopList\fR and \fIcommandPrefix\fR, then the trace is
320removed, so that \fIcommandPrefix\fR will never again be invoked.  Returns
321an empty string.
322.RE
323.TP
324\fBtrace info \fItype name\fR
325Where \fItype\fR is either \fBcommand\fR, \fBexecution\fR or \fBvariable\fR.
326.RS
327.TP
328\fBtrace info command\fI name\fR
329Returns a list containing one element for each trace currently set on
330command \fIname\fR. Each element of the list is itself a list
331containing two elements, which are the \fIopList\fR and \fIcommandPrefix\fR
332associated with the trace.  If \fIname\fR does not have any traces set,
333then the result of the command will be an empty string.  If \fIname\fR
334does not exist, the command will throw an error.
335.TP
336\fBtrace info execution\fI name\fR
337Returns a list containing one element for each trace currently set on
338command \fIname\fR. Each element of the list is itself a list
339containing two elements, which are the \fIopList\fR and \fIcommandPrefix\fR
340associated with the trace.  If \fIname\fR does not have any traces set,
341then the result of the command will be an empty string.  If \fIname\fR
342does not exist, the command will throw an error.
343.TP
344\fBtrace info variable\fI name\fR
345Returns a list containing one element for each trace currently set on
346variable \fIname\fR.  Each element of the list is itself a list
347containing two elements, which are the \fIopList\fR and \fIcommandPrefix\fR
348associated with the trace.  If \fIname\fR does not exist or does not
349have any traces set, then the result of the command will be an empty
350string.
351.RE
352.PP
353For backwards compatibility, three other subcommands are available:
354.RS
355.TP
356\fBtrace variable \fIname ops command\fR
357This is equivalent to \fBtrace add variable \fIname ops command\fR.
358.TP
359\fBtrace vdelete \fIname ops command\fR
360This is equivalent to \fBtrace remove variable \fIname ops command\fR
361.TP
362\fBtrace vinfo \fIname\fR
363This is equivalent to \fBtrace info variable \fIname\fR
364.RE
365.PP
366These subcommands are deprecated and will likely be removed in a
367future version of Tcl.  They use an older syntax in which \fBarray\fR,
368\fBread\fR, \fBwrite\fR, \fBunset\fR are replaced by \fBa\fR, \fBr\fR,
369\fBw\fR and \fBu\fR respectively, and the \fIops\fR argument is not a
370list, but simply a string concatenation of the operations, such as
371\fBrwua\fR.
372.SH EXAMPLES
373Print a message whenever either of the global variables \fBfoo\fR and
374\fBbar\fR are updated, even if they have a different local name at the
375time (which can be done with the \fBupvar\fR command):
376.CS
377proc tracer {varname args} {
378    upvar #0 $varname var
379    puts "$varname was updated to be \e"$var\e""
380}
381\fBtrace add\fR variable foo write "tracer foo"
382\fBtrace add\fR variable bar write "tracer bar"
383.CE
384.PP
385Ensure that the global variable \fBfoobar\fR always contains the
386product of the global variables \fBfoo\fR and \fBbar\fR:
387.CS
388proc doMult args {
389    global foo bar foobar
390    set foobar [expr {$foo * $bar}]
391}
392\fBtrace add\fR variable foo write doMult
393\fBtrace add\fR variable bar write doMult
394.CE
395.PP
396Print a trace of what commands are executed during the processing of a Tcl
397procedure:
398.CS
399proc x {} { y }
400proc y {} { z }
401proc z {} { puts hello }
402proc report args {puts [info level 0]}
403\fBtrace add\fR execution x enterstep report
404x
405  \(-> \fIreport y enterstep\fR
406    \fIreport z enterstep\fR
407    \fIreport {puts hello} enterstep\fR
408    \fIhello\fR
409.CE
410.SH "SEE ALSO"
411set(n), unset(n)
412.SH KEYWORDS
413read, command, rename, variable, write, trace, unset
Note: See TracBrowser for help on using the repository browser.