Probes and ztcl debug
A probe records values at a proc, a line, a command, a variable, or an error, with no change to the script. ztcl debug runs a script with probes and writes a short report: the error, the stack with the locals of each frame, the probes, and the last records. The module page has each option of ztcl::probe.
Probes in a running program
A probe is a puts that you add from outside the code. The shell below has the proc average with a bug: the second call divides by zero. Run the commands of the box, then ztcl::probe records -where {x < 0}.
The state of the shell
package require ztcl::probe
proc average {items} {
set total 0
set n 0
foreach x $items {
incr total $x
if {$x < 0} { set n 0 } else { incr n }
}
return [expr {$total / $n}]
}
ztcl::probe add -proc average -line 6 -capture {x n total}
catch {average {3 -3}}
ztcl::probe records
The records show that n goes back to 0 at the negative item, so the line 6 is the bug.
ztcl debug
Give the options of ztcl debug first, then the options of the runner, then the script.
ztcl debug --explain average.tcl
avg 6
status: error
error: divide by zero
code: ARITH DIVZERO {divide by zero}
at: expr {$total / $n}
stack, innermost first (2 frames):
average {3 -3}
items = 3 -3
total = 0
n = 0
x = -3
report
changes in the last call (second run; each change is between the two lines):
total: <no value> -> 0 (line 2 to 3)
total: 0 -> 3 (line 5 to 6)
total: 3 -> 0 (line 5 to 6)
n: <no value> -> 0 (line 3 to 4)
n: 0 -> 1 (line 6 to 5)
n: 1 -> 0 (line 6 to 8)
x: <no value> -> 3 (line 4 to 5)
x: 3 -> -3 (line 6 to 5)
The change of n from 1 to 0 is between line 6 and line 8, so the command on line 6 made it. The file average.tcl of this example is examples/debug/average.tcl of the repository:
proc average {items} {
set total 0
set n 0
foreach x $items {
incr total $x
if {$x < 0} { set n 0 } else { incr n }
}
return [expr {$total / $n}]
}
proc report {} {
puts "avg [average {4 6 8}]"
puts "avg [average {3 -3}]"
}
report
| Option | Description |
|---|---|
--probe SPEC, -p SPEC |
Adds a probe. SPEC is the words of ztcl::probe add, such as -proc parse -line 4 -capture {i [llength $toks]}. Give the option again for each probe. |
--explain |
After an error, runs the script again with a probe on each line of the proc of the error. The probes capture the variables of the failed command and the locals of that frame. The report gives the last 5 changes of each one, with the line of the command that made the change. |
--json |
Writes the report as one JSON object. |
--budget N |
The most bytes of the report. The default is 4,000. The last line tells how many bytes it left out. |
--detail PART |
summary (the default), stack (each frame), records (each record), or all. |
--last N |
The count of records in the report. The default is 10. |
The output of the script comes first, then the report. The exit code is 0 for a run with no error, 1 for an error, and 2 for a bad probe.
The second run of --explain is equal to the first run when the script has no capability, because such a script is deterministic. With a capability, such as --allow clock, the second run can differ, and the report says so in a note line.
Questions over a run
These options of ztcl debug add probes and give one answer line each. They work in a normal run and in a replay.
| Option | The answer |
|---|---|
--who-set VAR |
The last write of the global or namespace variable: its value, its time in compute units, and the stack. |
--when VAR VALUE |
The first write of VALUE to the variable. |
The report also has a first error line when an error came before the last error, for example an error that catch caught.
For this script, ztcl debug --who-set x --when x 8 steps.tcl gives the lines below.
proc step {k} { global x; set x [expr {$x * 2 + $k}] }
set x 1
foreach k {1 2 3 4} { step $k }
catch {error early}
error late
first error: early (at error early, unit 25)
answer: x: was last set to 42 at unit 23 (record #5), stack: {step 4}
answer: x: got the value 8 at unit 13 (record #2), stack: {step 2}