Carl Sassenrath, CTO
REBOL Technologies
17-Jul-2009 1:00 GMT

Article #0224
Main page || Index || Prior Article [0223] || Next Article [0225] || 5 Comments || Send feedback

Note: the trace function document has been updated.

Have you ever had a script run fine for a moment then crash? You decide to add a trace to see what happened in the area of the crash... but you don't want to see 24000 lines of output it's just the last 20 lines you really need to know?

As we test R3, and as we port R2 scripts to it, we'll have that situation happen. So, in A75, there is a solution: TRACE/back. This is a simple refinement to trace that redirects its output to an internal buffer that you can examine later.

To enable backtrace:

>> trace/back on

Then, run your code. When your crash occurs, type:

>> trace/back 20

to see the last 20 lines (or however many lines you want to see.)

The internal backtrace buffer is 100KB, so it can hold quite a few lines of history.

You can also modify your trace depth as you would normally. For example:

>> trace/back on
>> trace 5

will only trace down five levels of code.

Here is an example session:

>> trace/back on
>> test: func [a] [if integer? a [loop a [bug]]]
>> test 10
** Script error: bug has no value
** Where: loop if test
** Near: loop a [bug]

>> trace/back 20
 1: test : function! [a]
 2: 10
--> test
     1: if : native! [condition then-block /else else-block]
     2: integer? : action! [value]
     3: a : 10
    --> integer?
    <-- integer? => true
     4: [loop a [bug]]
    --> if
         1: loop : native! [count block]
         2: a : 10
         3: [bug]
        --> loop
             1: bug : unset!
            **: error : Script no-value
 1: trace/back
 2: 20
--> trace

So, it's not hard to see what was going on when the script crashed. Backtrace can be quite handy when you need it.

I wrote a short document on how to read the trace output. It's pretty easy, once you "get the format". (Although, I should probably revisit and refresh that doc a bit.)

There are probably a few improvements that could be made. For example, it's often enough just to see the function call history, skipping all the evaluation details (as in R2). So, that can be added as a refinement.

Anyway, give it a try... and I should note: it's not going to handle Unicode as of yet. More work to do on that.


Updated 14-Jun-2024 - Edit - Copyright REBOL Technologies -