TCL (Tool Command Language)
Writing TCL Extensions In C
A good description of TCL dual-ported objects and reference counts is here.
Trace
See the trace man page.
The following is a rather basic little example of putting a "trace" on a variable that will be executed everytime a variable is accessed. It doesn't do much other than print to stdout but could be augmented to write to a file or do something more complicated if required. It also only supports reading and writing to a simple variable: arrays are not supported and unsetting the variable is not supported.
% proc jeh_trace {n1 n2 op} {
upvar $n1 n1ref
if {"$op" == "read"} {
set var_exists [uplevel 1 "info exists $n1"]
if {$var_exists} {
puts "Reading $n1 as $n1ref"
} else {
puts "Trying to read $n1 but it does not exist"
}
} else {
puts "Setting $n1 to $n1ref"
}
}
% trace add variable jehtech {read write} jeh_trace
% set jehtech
Trying to read jehtech but it does not exist
can't read "jehtech": no such variable
% set jehtech "jehtech"
Setting jehtech to jehtech
jehtech
% set jehtech
Reading jehtech as jehtech
jehtech