REBOL Programming/Debugging Techniques

Debugging TechniquesEdit

There are various debugging tools available in REBOL

ProbeEdit

PROBE is a highly useful tool. What it does, is read the return values of functions and output them on the console. The return value is passed through PROBE, so you can insert it anywhere in your program.

>> wallet: $25
== $25.00
>> probe wallet
$25.00
== $25.00

The value is both printed and returned as a value, because PROBE, lets the return value pass through itself, as opposed to PRINT, which doesn't return a useful value.

A more useful example:

>> add 3 probe add 2 2
4
== 7

NOTE: Be careful with probing large objects as they can take a very long time to print on the console. Large amounts of output can be stopped by pressing Escape.

TraceEdit

TRACE can help you to see function calls or TCP/IP network statistics on the console.

>> trace/func on

TRACE/FUNC returns a large amount of feedback, so it's wise only to activate it around the code you need to examine.

trace/func on
... some problematic code ...
trace/func off
>> trace/net on

protect-systemEdit

it is easy in REBOL to accidentally redefine a system word, as *all* words can be redefined. This code will cause strange things to happen a long way away from the original error:

gt: func [a b] [
    return: a < b
]

That stray ":" on the return redefines return rather than returning the expected result. Any later code that uses return may misbehave.

An answer may be to protect all existing words from change:

 >> protect-system

PrintEdit

Adding print statements is a time-honored way of debugging code. REBOL has a neat way of letting you leave the statements in unactivated until needed

The trick is to define another word as a synonym of print, eg:

 >> dprint: :print

And use that to add debugging print statements:

 >> dprint ["my count is " my-count]
 >> dprint ["file info:" mold info? my-text-file.txt]

In production code, or when you want to turn the debugging statements off, simply add:

 >> dprint: func [data] []

??Edit

REBOL provides specific functions to help with debugging. The function ?? prints a variable name followed by it's molded value. Unlike the print function, it also returns the value it receives.

>> test: "Hello"
== "Hello"
>> value: ?? test
test: "Hello"
== "Hello"
>> ?? value
value: "Hello"
== "Hello"

Once you have finished debugging, you can either remove all the ?? from the source, or just redefine ?? so that it just passes the value as though it were no longer present.

??: func [val] [val]


Adding a debugging option to GUI programsEdit

While a VID program is running, you can't use the console to type PRINT or ?? or any other debugging functions. One way round that is to add a console input field to your VID program, and execute whatever is typed into that:

view layout [
    button "Add"
           [field1/text: 1 + to-integer field1/text show field1]
    label "number"
    field1: field 100 "1"
    
    label "console:"
    console: field 100 [attempt [do console/text]]
]

Try typing "help view" into the field labeled console.

Quickly restarting a GUI programEdit

You can shave off a tiny bit of debugging time, by adding a simple button to your program that quits the program and starts it again.

unview/all
do %<programname>

Stopping and continuing programsEdit

Stopping a program to the console can be done with Escape.

Continuing GUI programs can be done with DO-EVENTS.

Last modified on 3 June 2009, at 03:54