12.1.1 Debug Guile Scheme code

Sometimes code in Scheme may fail and output a backtrace looking out something like this:

Backtrace:
In unknown file:
?: 19 [apply-smob/1 #<catch-closure 55e36cf0a020>]
In ice-9/boot-9.scm:
66: 18 [call-with-prompt prompt0 ...]
...
many more lines
...
In ice-9/boot-9.scm:
105: 1 [#<procedure 55e36e0eee80 at ice-9/boot-9.scm:100:6
(thrown-k . args)> wrong-type-arg ...]
In unknown file:
?: 0 [apply-smob/1 #<catch-closure 55e36e0edae0> wrong-type-arg ...]

ERROR: In procedure apply-smob/1:
ERROR: In procedure struct-vtable: Wrong type argument in position 1
(expecting struct): #f

The output is incomprehensible, isn’t it? The issue gets worse due to abbreviation of the output, look at the ellipses in some of the lines.

The length of the output lines depends on the environment variable COLUMNS set in your environment. Sometimes you can improve readability of such errors by setting the variable to a bigger value. For instance, if you launch your program in terminal, do:

echo $COLUMNS
-| 106
export COLUMNS=1000
lepton-schematic

You can do it in your Scheme code as well:

(setenv "COLUMNS" "1000")

Sometimes you have to find the code you could insert such a line in. For example, the code of Lepton Scheme unit tests contains this line in unit-test.scm, though it is commented out as this is unnecessary in most cases.

Another approach is using of interactive mode of Guile or one of Lepton tools to debug your code. Load your modules or files with code in this mode one by one and see what’s wrong with them. lepton-netlist and lepton-symcheck support this.