Debug a script

This lesson finds two bugs in a small script. The first bug gives a wrong number, and a probe shows why. The second bug stops the script, and ztcl debug explains it. The procs are code from an agent: you add no puts to them, and you change only the line with the bug.

A wrong number

progress gives the percent of the notes that are done, with the helper percent. One of the two notes is done, so the answer must be 50.

proc percent {part whole} {
    expr {$part / $whole * 100}
}
proc progress {notes} {
    set done 0
    foreach note $notes {
        if {[dict get $note done]} { incr done }
    }
    return [percent $done [llength $notes]]
}

progress $notes gives 0.

Look inside with a probe

A probe records values at a line of a proc, with no change to the proc. The line numbers start at the proc line, so line 2 of percent is its expr.

  1. package require ztcl::probe loads the module.
  2. ztcl::probe add -proc percent -line 2 -capture {part whole} adds the probe p1.
  3. progress $notes runs the proc again.
  4. dict get [lindex [ztcl::probe records] 0] values reads the record of that call.

The inputs are right, so the bug is in the expr. $part / $whole is 1 / 2, and the division of two integers gives an integer: 0. Multiply first: proc percent {part whole} {expr {100 * $part / $whole}} puts the fix in the shell, and progress $notes gives 50.

The probe stays on the new proc, because a probe follows the name. ztcl::probe remove all removes it. ztcl::probe has the other kinds: a probe on a command, on a variable, and on each error, and -where to keep only some records.

An error: ask ztcl debug

progress {} stops with an error. On your computer, put the two procs and the call progress {} in progress.tcl, and run ztcl debug --explain progress.tcl. The report has the error, the command that failed, and the stack with the locals of each frame:

status: error
error: divide by zero
code: ARITH DIVZERO {divide by zero}
at: expr {100 * $part / $whole}
stack, innermost first (2 frames):
  percent 0 0
    part = 0
    whole = 0
  progress {}
    notes = 
    done = 0

whole is 0 because notes is empty. The fix goes in progress or in percent: an empty list is 0 percent done, or an error with a code. Errors has the error codes.

Other tools

Debugging has each option, the tape, and the setup of the MCP server and the editor.

Reading code an agent wrote

A prompt for an agent: "Find the bug with ztcl debug --explain or a probe of ztcl::probe. Do not add puts to the code. Show the record that proves the cause."

Try this

The state of the shell at the start of this lesson
set notes [list [dict create text "buy milk" done 1] [dict create text "call Sam" done 0]]