2
votes

I have a custom test tool written in tcl and bash (mainly in tcl, some initial config and check were done by bash). It isn't have an exact starting point, the outside bash (and sometimes the application which is tested) call specific functions which they find with a "tclIndex" file, created by auto_mkindex.

This tool create a log file, with many "puts" function, which is directed to the file location.

Most of the functions have a "trackBegin" function call at the beginning, and one "trackEnd" function at the end of it. These two get the functions name as parameter. This help us to track where is the problem.

Sadly, this tracker was forgotten in some modification in the near past, and its not even too reliable because its not going to track if there is any abnormal exit in the function. Now, i tried to remove all of them, and create a renamed _proc to override the original and place this two tracker before and after the execution of the function itself.

But i have a lots of error (some i solved, but i dont know its the best way, some are not solved at all, so i'm stuck), these are the main ones:

  1. Because there is no exact entry point, where should i define and how, this overrided proc, to work on all of the procedures in this execution? Some of my files had to be manually modified to _proc to work (mostly the ones where there are code outside the procedures and these files as scripts were called, not functions through the tclIndex, the function called ones are all in a utils folder, and only there, maybe it can help).
  2. In the tracker line i placed a "clock" with format, and its always cause abnormal exit.
  3. I had problems with the returned values (if there was one, and some time when there isn't). Even when that was a return, or Exit.

So my question is in short: How can i solve an overrided proc function, which will write into a logfile a "begin" and "end" block before and after the procedure itself (The log file location was gained from the bash side of this tool), when there is no clear entry point in this tool for the tcl side, and use an auto_mkindex generated procedure index file?

Thanks, Roland.

2
Might not solve the immediate problem, but have a look at the tcllib logger trace functionality, as it provides a working system for enter/exit tracing of procedures with zero runtime cost when disabled. ( core.tcl.tk/tcllib/doc/trunk/embedded/www/tcllib/files/modules/… )schlenk

2 Answers

0
votes

Untested

Assuming your bash script does something like

tclsh file.tcl

You could do

tclsh instrumented.tcl file.tcl

where instrumented.tcl would contain

proc trackBegin {name} {...}
proc trackEnd {name output info} {...}

rename proc _proc
_proc proc {name args body} {
    set new_body [format {
            trackBegin %s
            catch {%s} output info
            trackEnd %s $output $info
        } $name $body $name]
    _proc $name $args $new_body
}

source [lindex $argv 0]

See the return and catch pages for what to do with the info dictionary.

You'll have to show us some of your code to provide more specific help, particularly for your clock error.

0
votes

I'd be tempted to use execution tracing for this, with the addition of the execution tracing being done in an execution trace on proc (after all, it's just a regular Tcl command). In particular, we can do this:

proc addTracking {cmd args} {
    set procName [lindex $cmd 1]
    uplevel 1 [list trace add execution $procName enter [list trackBegin $procName]]
    uplevel 1 [list trace add execution $procName leave [list trackEnd $procName]]
}
proc trackBegin {name arguments operation} {
    # ignore operation, arguments might be interesting
    ...
}
proc trackEnd {name arguments code output operation} {
    # ignore operation, arguments might be interesting
    ...
}
trace add execution proc leave addTracking

It doesn't give you quite the same information, but it does allow you to staple code around the outside non-invasively.