Ficl Debugger

Forth Inspired Command Language  Key Resource
Author: John Sadler (john_sadler@alum.mit.edu)
Created: 19 July 1997 
Revision 2.05: August 2000

 
Ficl release 2.05 includes a simple step debugger for colon definitions and does> words. If you use it and can suggest improvements (or better yet if you write some), please let me know.

Using the debugger

To debug a word, set up the stack with any parameters the word requires, then type:
debug <your word here>
If the word is unnamed, or all you have is an xt, you can instead use:
debug-xt  ( xt -- )

The debugger invokes see on the word, printing a crude source listing, then stops at the first instruction of the definition. There are four (case insensitive) commands you can use from here onwards:

I (step in)
If the next instruction is a colon defintion or does> word, steps into that word's code. If the word is a primitive, simply executes the word.
O (step over)
Executes the next instruction in its entirety
G  (go)
Run the word to completion and exit the debugger
Q (quit)
Abort the word and exit the debugger, clearing the stack

The on-step event 

If there is a defined word named on-step when the debugger starts, that word will be executed before every step. As a guideline, this word should have no side effects. Its intended use is to display the stack, but you may have some better ideas. Please let me know. 
: on-step  ." Stack: " .s cr ;
 

Debugger internals

The debugger words are mostly located in source file tools.c. There is one supporting word (debug) in softcore.fr as well. There are two main words that make the debugger go: debug-xt and step-break. Debug-xt takes the xt of a word to debug (as returned by ', for example) checks to see if it is debuggable (not a primitive), sets a breakpoint at its first instruction, and runs see on it. To set a breakpoint, debug-xt replaces the instruction at the breakpoint with the xt of step-break, and stores the original instruction and its address in a static breakpoint record. To clear the breakpoint, step-break simply replaces the original instruction and adjusts the target virtual machine's instruction pointer to run it. 
Step-break is responsible for processing debugger commands and setting breakpoints at subsequent instructions.