beetlebug▾
/

Traces

A trace records what happens in the app: each event, the bindings that it runs, the -command of a button, each option write, each geometry pass, each widget that gets dirty, each frame, and each change of the focus and the grab. Use it to answer "what ran when I clicked" and "why did this widget draw again".

ztk::debug trace start starts the record. ztk::debug trace records gives the records as dicts, oldest first.

button .save -text Save -command {.status configure -text Saved}
label .status -text Ready
pack .save .status
update
ztk::debug trace start
.save invoke
update
foreach r [ztk::debug trace records -kind {command option frame}] {
    puts $r
}

Each record starts with the same keys:

Key What it has
seq The number of the record. Option sources use the same numbers.
kind The kind of the record.
widget The widget of the record.
cause The number of the event whose bindings ran when the record was made, or 0.
us The time of the clock of the app, in microseconds.

The other keys depend on the kind:

Kind Keys
event event (for example <ButtonRelease-1>), x, y, text.
motion The same keys, for <Motion>, <Enter>, and <Leave>. This kind is off unless -kinds names it.
binding tag, pattern, script.
command script, ran (0 when the command did not run), why (for example the widget is disabled).
option option, value, prev, source, code, line, proc, as in ztk::debug why.
dirty flags (the new flags of style, measure, layout, and paint), by (the number of the option record that made the widget dirty, or 0).
geometry manager, req, box, changed (each content widget whose box changed: the path, the old box, and the new box).
frame frame (the count of frames), us (the time of the frame), dirty (the widgets that got dirty since the last frame of the toplevel).
focus from, to.
grab action (set or release), global.

The subcommands:

Command Description
ztk::debug trace start ?-kinds LIST? ?-size N? Starts the trace. -kinds is a list of kinds, or all. The default is each kind except motion. -size is the count of records that the ring keeps (default 10,000). A new record replaces the oldest one.
ztk::debug trace stop Stops the trace and keeps the records.
ztk::debug trace clear Removes the records.
ztk::debug trace status Gives on, kinds, count, size, and dropped (the count of records that the ring replaced).
ztk::debug trace records ?FILTER ...? Gives the records that pass each filter.

The filters of records:

Filter What it keeps
-kind list The records of these kinds.
-widget pattern The records of the widgets that match the pattern, with * and ?.
-match pattern The records whose fields match the pattern, for example *ButtonRelease*.
-cause n The event n and each record that its bindings made: the answer to "what ran when I clicked".
-since n The records after the number n.
-last n The last n records that pass the other filters.

This script answers "what ran when I clicked Save":

set click [lindex [ztk::debug trace records -kind event -widget .save -match *ButtonRelease* -last 1] 0]
foreach r [ztk::debug trace records -cause [dict get $click seq]] {
    puts "[dict get $r kind] [dict get $r widget]"
}

While the trace is off, each hook tests one mask and makes no record.

Short answers: explain, click, type, and ran

These subcommands give lines of text for a person or an agent. Each answer has at most 24 lines, and a longer answer ends with a line that gives the count of the lines that it left out.

Command The question that it answers
ztk::debug explain PATH ?OPTION? "Why is .f.ok disabled?" With an option: the value, the call that set it with its line and stack, the value before, and the style. With no option: the box, the geometry notes, and each option that a call set.
ztk::debug click PATH ?BUTTON? "What ran when I clicked Save?" It clicks the center of the widget through the hit test and gives each event with its bindings, commands, option changes, and dirty widgets, then the geometry passes and the frames.
ztk::debug type TEXT ?PATH? The same for text: into the widget with the focus, or into path after a click on it.
ztk::debug ran ?seq? What ran for the last event that ran a binding, or for the event with the number seq. It needs the trace.
ztk::debug changes name "Diff the UI before and after": the answer of ztk::debug diff as lines.
ztk::debug tree ?path? One line for each widget: the path, the class, the box, and the map state.

click and type start the trace when it is off, and stop it again after. For example, ztk --headless app.tcl with this app.tcl:

ztk::debug sources on
button .save -text Save -command save
label .status -text Ready
pack .save .status
proc save {} { .status configure -text Saved; lock .save }
proc lock {w} { $w configure -state disabled }
puts [ztk::debug click .save]
puts [ztk::debug explain .save -state]

The second answer is:

.save -state is "disabled": set by $w configure -state disabled at line 1 of proc lock in app.tcl.
Before: "normal". Writes: 1.
Stack: lock .save < save
A click on .save runs no -command while it is disabled.