ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: A minimal debugger
Submitter: Jeff Hobbs (other recipes)
Last Updated: 2001/06/21
Version no: 1.0
Category: Development, Debugging

 

5 stars 1 vote(s)


Approved

Description:

Of course, Tcl's most minimal debugger is puts. But here is a cute
little piece of code that offers some more debugging functionality (if
you have stdin and stdout available - so not for wish on Windows)

Source: Text Source

proc bp {{s {}}} {
    if {![info exists ::bp_skip]} {
	set ::bp_skip [list]
    } elseif {[lsearch -exact $::bp_skip $s]>=0} {
	return
    }
    set who [info level -1]
    while 1 {
	# Display prompt and read command.
	puts -nonewline "$who/$s> "; flush stdout
	gets stdin line

	# Handle shorthands
	if {$line=="c"} {puts "continuing.."; break}
	if {$line=="i"} {set line "info locals"}

	# Handle everything else.
	catch {uplevel 1 $line} res
	puts $res
    }
}

The license for this recipe is available here.

Discussion:

The idea is that you insert breakpoints, calls to bp, in critical
code with an optional string argument (that may be used for
distinguishing), like this:

proc foo {args} {
    set x 1
    bp 1
    string toupper $args
}
foo bar and grill

When execution reaches bp, you get a prompt on stdout, giving the
calling context and the bp string, like this:
 foo bar and grill/1> pwd
 /home/suchenwi/src/roi/test/mr
 foo bar and grill/1> i
 args x
 foo bar and grill/1> set args
 bar and grill
 foo bar and grill/1> set args lowercase
 lowercase
 foo bar and grill/1> c

on which you can issue any Tcl command (especially getting or
setting variable values in the scope of foo), shorthand commands ("i"
for "info locals"; add which others you need), and exit this
micro-debugger with "c"(ontinue). Because you modified a local
variable, foo's result will be
 LOWERCASE

To turn off all breakpoints for an application, just say (maybe
from inside bp):
 proc bp args {}

You can disable single breakpoints labeled e.g. x with the command
 lappend ::bp_skip x

Stepping would be a bigger project, but in some situations a
micro-debugger like this is good enough. See also (http://www.purl.org/thecliff/tcl/wiki/473.html) What debugging
tools are available to a Tcl programmer on the Wiki.



Add comment

No comments.



Highest rated recipes:

1. With busy cursor

2. Get widget info

3. Simplified mega-widiget ...

4. Supporting mouse wheel ...

5. Multi-character split

6. LCD Number Display

7. Check creditcard numbers ...

8. A minimal debugger

9. Socket based communicatio...

10. WSCP - showServerStatus




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.