trace.n
来自「tcl是工具命令语言」· N 代码 · 共 362 行 · 第 1/2 页
N
362 行
'\"'\" Copyright (c) 1993 The Regents of the University of California.'\" Copyright (c) 1994-1996 Sun Microsystems, Inc.'\" Copyright (c) 2000 Ajuba Solutions.'\"'\" See the file "license.terms" for information on usage and redistribution'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.'\" '\" RCS: @(#) $Id: trace.n,v 1.13 2003/02/04 00:56:20 hobbs Exp $'\" .so man.macros.TH trace n "8.4" Tcl "Tcl Built-In Commands".BS'\" Note: do not modify the .SH NAME line immediately below!.SH NAMEtrace \- Monitor variable accesses, command usages and command executions.SH SYNOPSIS\fBtrace \fIoption\fR ?\fIarg arg ...\fR?.BE.SH DESCRIPTION.PPThis command causes Tcl commands to be executed whenever certain operations areinvoked. The legal \fIoption\fR's (which may be abbreviated) are:.TP\fBtrace add \fItype name ops ?args?\fRWhere \fItype\fR is \fBcommand\fR, \fBexecution\fR, or \fBvariable\fR..RS.TP\fBtrace add command\fR \fIname ops command\fRArrange for \fIcommand\fR to be executed whenever command \fIname\fRis modified in one of the ways given by the list \fIops\fR. \fIName\fR will beresolved using the usual namespace resolution rules used byprocedures. If the command does not exist, an error will be thrown..RS.PP\fIOps\fR indicates which operations are of interest, and is a list ofone or more of the following items:.TP\fBrename\fRInvoke \fIcommand\fR whenever the command is renamed. Note thatrenaming to the empty string is considered deletion, and will notbe traced with '\fBrename\fR'..TP\fBdelete\fRInvoke \fIcommand\fR when the command is deleted. Commands can bedeleted explicitly by using the \fBrename\fR command to rename thecommand to an empty string. Commands are also deleted when theinterpreter is deleted, but traces will not be invoked because there is nointerpreter in which to execute them..PPWhen the trace triggers, depending on the operations being traced, a number of arguments are appended to \fIcommand\fR so that the actual command is as follows:.CS\fIcommand oldName newName op\fR.CE\fIOldName\fR and \fInewName\fR give the traced command's current(old) name, and the name to which it is being renamed (the emptystring if this is a 'delete' operation).\fIOp\fR indicates what operation is being performed on thecommand, and is one of \fBrename\fR or \fBdelete\fR asdefined above. The trace operation cannot be used to stop a commandfrom being deleted. Tcl will always remove the command once the traceis complete. Recursive renaming or deleting will not cause further traces of the same type to be evaluated, so a delete trace which itselfdeletes the command, or a rename trace which itself renames thecommand will not cause further trace evaluations to occur.Both \fIoldName\fR and \fInewName\fR are fully qualified with any namespace(s)in which they appear..RE.TP\fBtrace add execution\fR \fIname ops command\fRArrange for \fIcommand\fR to be executed whenever command \fIname\fRis modified in one of the ways given by the list \fIops\fR. \fIName\fR will beresolved using the usual namespace resolution rules used byprocedures. If the command does not exist, an error will be thrown..RS.PP\fIOps\fR indicates which operations are of interest, and is a list ofone or more of the following items:.TP\fBenter\fRInvoke \fIcommand\fR whenever the command \fIname\fR is executed,just before the actual execution takes place..TP\fBleave\fRInvoke \fIcommand\fR whenever the command \fIname\fR is executed,just after the actual execution takes place..TP\fBenterstep\fRInvoke \fIcommand\fR for every tcl command which is executed inside the procedure \fIname\fR, just before the actual executiontakes place. For example if we have 'proc foo {} { puts "hello" }',then a \fIenterstep\fR trace would be invoked just before \fIputs "hello"\fR is executed.Setting a \fIenterstep\fR trace on a \fIcommand\fRwill not result in an error and is simply ignored..TP\fBleavestep\fRInvoke \fIcommand\fR for every tcl command which is executed inside the procedure \fIname\fR, just after the actual executiontakes place.Setting a \fIleavestep\fR trace on a \fIcommand\fRwill not result in an error and is simply ignored..PPWhen the trace triggers, depending on the operations being traced, a number of arguments are appended to \fIcommand\fR so that the actual command is as follows:.PPFor \fBenter\fR and \fBenterstep\fR operations:.CS\fIcommand command-string op\fR.CE\fICommand-string\fR gives the complete current command being executed (the traced command for a \fBenter\fR operation, an arbitrary command for a \fBenterstep\fR operation), includingall arguments in their fully expanded form.\fIOp\fR indicates what operation is being performed on thecommand execution, and is one of \fBenter\fR or \fBenterstep\fR asdefined above. The trace operation can be used to stop thecommand from executing, by deleting the command in question. Ofcourse when the command is subsequently executed, an 'invalid command'error will occur..PPFor \fBleave\fR and \fBleavestep\fR operations:.CS\fIcommand command-string code result op\fR.CE\fICommand-string\fR gives the complete current command being executed (the traced command for a \fBenter\fR operation, an arbitrary command for a \fBenterstep\fR operation), includingall arguments in their fully expanded form.\fICode\fR gives the result code of that execution, and \fIresult\fRthe result string.\fIOp\fR indicates what operation is being performed on thecommand execution, and is one of \fBleave\fR or \fBleavestep\fR asdefined above. Note that the creation of many \fBenterstep\fR or\fBleavestep\fR traces can lead to unintuitive results, since theinvoked commands from one trace can themselves lead to furthercommand invocations for other traces.\fICommand\fR executes in the same context as the code that invokedthe traced operation: thus the \fIcommand\fR, if invoked from a procedure,will have access to the same local variables as code in the procedure.This context may be different than the context in which the trace wascreated. If \fIcommand\fR invokes a procedure (which it normally does)then the procedure will have to use upvar or uplevel commands if it wishesto access the local variables of the code which invoked the trace operation.While \fIcommand\fR is executing during an execution trace, traceson \fIname\fR are temporarily disabled. This allows the \fIcommand\fRto execute \fIname\fR in its body without invoking any other traces again.If an error occurs while executing the \fIcommand\fR body, then the\fIcommand\fR name as a whole will return that same error.When multiple traces are set on \fIname\fR, then for \fIenter\fRand \fIenterstep\fR operations, the traced commands are invokedin the reverse order of how the traces were originally created;and for \fIleave\fR and \fIleavestep\fR operations, the tracedcommands are invoked in the original order of creation.The behavior of execution traces is currently undefined for a command \fIname\fR imported into another namespace..RE.TP\fBtrace add variable\fI name ops command\fRArrange for \fIcommand\fR to be executed whenever variable \fIname\fRis accessed in one of the ways given by the list \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. If the variable does not exist, it will be created butwill not be given a value, so it will be visible to \fBnamespace which\fRqueries, but not to \fBinfo exists\fR queries..RS.PP\fIOps\fR indicates which operations are of interest, and is a list ofone or more of the following items:
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?