Version 4 of Breakpoints in Tcl

Updated 2014-05-08 12:49:43 by suchenwi

On facebook someone asked "how do you stop tcl script at a particular line"

The quickest way, if you know the line is (immediately before that line):

error "I have to stop here"

But if you are looking for something a little more elegant, create a proc which tells Tcl to enter the event loop, and stay there until a condition has been satisfied. In this case we will assume our program is running from a command line, and we want to prompt the user to press any key to continue.

proc breakpoint {} { 
  set ::breakpoint 0
  puts stdout "Press <Any Key> to continue"
  fileevent stdin readable {set ::breakpoint 1}
  vwait ::breakpoint

For a tk based application:

proc breakpoint {} { 
  tk_messageBox -type ok -message "Press OK to continue"

RS 2014-05-08 My favorite breakpoint, which allows interacting with a running process like with a debugger, is this:

proc interact args {
    while 1 {
        puts -nonewline "$args% "
        flush stdout
        gets stdin cmd
        if {$cmd=="exit" || $cmd=="c"} break
        catch {uplevel 1 $cmd} res
        if [string length $res] {puts $res}

The zero or more arguments are displayed before the % prompt, so if you have more than one of these breakpoints, you can name them:

 interact here
 interact there

or, to display in which proc the breakpoint is located:

 interact [info level 0]

An earlier attempt is at A minimal debugger.