Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What does this do? 1

Status
Not open for further replies.

NOOB

Technical User
Sep 17, 2002
7
AU
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].

Here is a demo:

package require Tcl 8.4
proc aproc {args} {
puts "inside aproc"
if {[info exists ::glob1]} { puts "::glob1 exists" } else { puts "::glob1 doesn't exist" }
if {[info exists ::glob2]} { puts "::glob2 exists" } else { puts "::glob2 doesn't exist" }
puts "leaving a proc"
}
trace add execution aproc {enter enterstep} entering
trace add execution aproc {leave leavestep} leaving
proc entering {call op} { puts "$op: $call" }
proc leaving {call code res op} { puts "$op: $call -> ($code) $res" }
set ::glob1 1
aproc first call
set ::glob2 2
aproc second call

Here is the doc:

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.

Good luck

ulis
 
Sorry, I should said: this is the standard documentation from the Tcl 8.4 manual.

ulis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top