1

Is there any way to list out all user defined proc calls in a Tcl file. ?

I have a TCL file, I want to list out all procs that has been called by that file.

Regards keerthan

keerthan kumar
  • 332
  • 7
  • 25
  • Execution traces can do that sort of thing with a little help. The main tricky bit is deciding on which commands to attach the traces to, and that depends on your exact use case. – Donal Fellows Apr 20 '17 at 16:41

1 Answers1

2

To get a message when a specific command is called, we can use an enter execution trace:

trace add execution $theCommand enter {apply {{call op} {
    puts "Called [lindex $call 0]"
}}

To select which command to attach that trace to, we can use various approaches. The easiest is if you can identify a namespace whose procedures you want to attach to:

foreach p [info procs ::theNamespace::*] {
    trace add execution $p enter {apply {{call op} {
        puts "Called [lindex $call 0]"
    }}
}

You can also attach it to every procedure created when a particular file is sourced. This is a bit more complex:

# Need to keep this script fragment so we can remove it again after the source
set tracer {apply {{proccall args} {
    set cmd [uplevel 1 [list namespace origin [lindex $proccall 1]]]
    trace add execution $cmd enter {apply {{call args} {
        puts "Called [lindex $call 0]"
    }}
}}

trace add execution ::proc leave $tracer
source /the/script.tcl
trace remove execution ::proc leave $tracer

You can get pretty complicated this way (and be aware that it also can affect procedures created by the loading of packages by the script; you can stop that, but it's a lot more work). Just attaching to all procedures that are currently in a namespace is simpler.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215