I need to be able to call a trace against a procedures
execution and check for the existence of several global variables in the handler.
Is this possible, and could somebody demonstrate it?
Yes, you can do.
But, for tracing execution, you need Tcl 8.4, the fresh latest version.
Checking the existence of a variable is matter of [info exists variable].
Tracing procs execution is matter of [trace add execution aproc {ops} command].
trace add execution name ops command
Arrange for command to be executed whenever command name is modified in one of the ways given by the list ops. Name will be resolved using the usual namespace resolution rules used by procedures. If the command does not exist, an error will be thrown.
Ops indicates which operations are of interest, and is a list of one or more of the following items:
enter
Invoke command whenever the command name is executed, just before the actual execution takes place.
leave
Invoke command whenever the command name is executed, just after the actual execution takes place.
enterstep
Invoke command for every tcl command which is executed inside the procedure name, just before the actual execution takes place. For example if we have 'proc foo {} { puts "hello" }', then a enterstep trace would be invoked just before puts "hello" is executed. Setting a enterstep trace on a command will not result in an error and is simply ignored.
leavestep
Invoke command for every tcl command which is executed inside the procedure name, just after the actual execution takes place. Setting a leavestep trace on a command will not result in an error and is simply ignored.
When the trace triggers, depending on the operations being traced, a number of arguments are appended to command so that the actual command is as follows: For enter and enterstep operations:
command command-string op
Command-string gives the complete current command being executed (the traced command for a enter operation, an arbitrary command for a enterstep operation), including all arguments in their fully expanded form. Op indicates what operation is being performed on the command execution, and is one of enter or enterstep as defined above. The trace operation can be used to stop the command from executing, by deleting the command in question. Of course when the command is subsequently executed, an 'invalid command' error will occur.
For leave and leavestep operations:
command command-string code result op
Command-string gives the complete current command being executed (the traced command for a enter operation, an arbitrary command for a enterstep operation), including all arguments in their fully expanded form. Code gives the result code of that execution, and result the result string. Op indicates what operation is being performed on the command execution, and is one of leave or leavestep as defined above. Note that the creation of many enterstep or leavestep traces can lead to unintuitive results, since the invoked commands from one trace can themselves lead to further command invocations for other traces. Command executes in the same context as the code that invoked the traced operation: thus the command, 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 was created. If command invokes a procedure (which it normally does) then the procedure will have to use upvar or uplevel commands if it wishes to access the local variables of the code which invoked the trace operation. While command is executing during an execution trace, traces on name are temporarily disabled. This allows the command to execute name in its body without invoking any other traces again. If an error occurs while executing the command body, then the command name as a whole will return that same error. When multiple traces are set on name, then for enter and enterstep operations, the traced commands are invoked in the reverse order of how the traces were originally created; and for leave and leavestep operations, the traced commands are invoked in the original order of creation. The behavior of execution traces is currently undefined for a command name imported into another namespace.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.