Comments on: R3 Teaser: the STACK function

Carl Sassenrath, CTO
REBOL Technologies
2-Apr-2007 21:23 GMT

Article #0075
Main page || Index || Prior Article [0074] || Next Article [0076] || 33 Comments || Send feedback

Here is a debugging function that many REBOLers might find useful:

    STACK offset /block /word /func /args /size /depth /limit 

     Returns stack backtrace or other values.
     STACK is a native value.

     offset -- Relative backward offset (Type: integer!)

     /block -- Block evaluation position
     /word -- Function or object name, if known
     /func -- Function value
     /args -- Block of args (may be modified)
     /size -- Current stack size (in value units)
     /depth -- Stack depth (frames)
     /limit -- Stack bounds (auto expanding)

(Currently, it's called stack, but it may be renamed because that word may be used in a number of scripts already.)

The offset is the number of stack frames to go back on the stack.

For, example, take this code:

my-func: does [
    loop 1 [
        if true [
            probe stack 0
            print "there"

The zero offset indicates the top of the stack. When you run the code, you'll see:

[stack if loop my-func]

That's a function invocation backtrace, with the most recent functions shown first.

I you only wanted to see functions prior to the stack call, you would use an offset of 1 and get:

[if loop my-func]

Other Information

The stack function can give you other useful information...

To get the code block where the function was called:

>> stack/block 0
== [0 print "there"]

This is normally used by error functions to show where the error occurred. Notice that it does not show the function name. That's because functions don't strictly require names (can be lambda functions, that is unnamed) and functions can also be applied (with apply).

To get the name of a function in a stack (here it's just the stack function):

>> stack/word 0
== stack

To get a function value from the stack:

>> stack/func 0
== make native! [
    {Returns stack backtrace or other values.}
    offset [integer!] "Relative backward offset"
    /block "Block evaluation position"
    /word "Function or object name, if known"
    /func "Function value"
    /args "Block of args (may be modified)"
    /size "Current stack size (in value units)"
    /depth "Stack depth (frames)"
    /limit "Stack bounds (auto expanding)"

That's a fully qualified function value, so you can use first, second, and even do it.

To find out the arguments to a function (including the refinements):

>> stack/args 0
== [0 none none none true none none none none]

So, you can match that up with the function arguments block above.

To get the stack size (the number of values on the stack):

>> stack/size 0
== 13

To get the stack depth (the number of functions or paths being evaluated):

>> stack/depth 0
== 1

And finally, to get the stack bounds (note that this can change as the stack auto-expands):

>> stack/limit 0
== 4096

So, there's a useful debug tool.

Better Name

As I noted, stack is probably not the final name. So, what would be the best name for such a function?



Gregg Irwin
2-Apr-2007 18:40:31
Now THIS is exciting stuff! And you or some guru will provide an "example" debugger written with it, right? ;)



Carl Sassenrath
2-Apr-2007 21:21:09
Yes, I expect someone like Maxim to put it to good use.
Maxim Olivier-Adlhoch
3-Apr-2007 1:52:47

can I say anything more?

3-Apr-2007 2:46:14
I second Max's opinion :-)
3-Apr-2007 3:08:28
How about track or back-track?
3-Apr-2007 3:34:54
I like track
3-Apr-2007 5:30:37
I like it. Much better error-methods.

For name, system/stack ? If r3 still has something similar.

Will it have also modify-functions, to implement single-step, or restart function?

Also it could be uses like the :arg you proposed, to access the callers code for more flexible arguments.

3-Apr-2007 7:07:27
Is this useful also for telling during normal operation if a specific function was called by a specific other function? Does it slow down anything?
3-Apr-2007 9:42:53
Can we stack the stack ? I mean, what is the type of the stack ? a block, an object ? Can we change/copy the content of the stack ? In that case, we could interrupt the current process (for a debuging purpose for example) by saving and clearing the stack and then, by restoring the stack to continue a step by step.
3-Apr-2007 11:23:22
I like this a lot -- this level of detail for debugging (including function names where they exist) has been an important need for a long time.

However (there's always one, isn't there?) there are some (perhaps minor) security issues here. I don't want lower-level code that I may not trust to have easy access to context information about how I've called it, names of my functions, etc).

Of course, I can turn it off entirely with:

stack: none
when I need to. But could there be something more subtle than that?
Brian Tiffin
3-Apr-2007 11:37:07
How about where?
Christian Ensel
3-Apr-2007 13:30:33
I'd suggest TRACE-STACK (symmetrical with TRACE-NET)
Carl Sassenrath
3-Apr-2007 14:22:55
Interesting name suggestions... thanks, and I am thinking about them. System/stack (or similar approach) is also possible.

Regarding single-step: it would not be part of stack function, but a separate method.

And, yes, in fact :arg is similar, but a more structured way.

On stack as a block: yes, but a special one. It is possible to allow other functions on it. However, there is also the issue of the C stack, so there is a limit to the degree of changes that could be allowed. We will come back and re-examine the possibilities soon enough.

Regarding security: yes, this is the subject of a latter blog (I did not want to complicate the discussion with it yet). And, it is not so minor... because in being able to "sniff around" on the stack, a function can discover passwords, etc. The stack function will likely require a secure enable command.

Anton Rolls
3-Apr-2007 23:17:08
How about TRACEBACK ?
So you TRACEBACK to get a backtrace.

I prefer longer, more descriptive, names for specific functions like this one.

(TRACE-STACK is good too.)
8-May-2007 21:06:29
How about dump, as in core dump? Takes me back to my first year at university :-)

trace-stack or System/trace are what I actually prefer, they are more descriptive.

buy cheap
12-Jun-2007 1:05:37
12-Jun-2007 4:33:56
buy generic
12-Jun-2007 4:44:19
buy carisoprodol online
12-Jun-2007 8:57:39
sildenafil citrate dosag
13-Jun-2007 7:15:11
buy carisoprodol
13-Jun-2007 7:16:52
order tramadol online
13-Jun-2007 7:30:38
cialis medication
13-Jun-2007 12:02:47
buy cialis online
13-Jun-2007 12:05:36
generic cialis
13-Jun-2007 12:08:20
13-Jun-2007 15:37:48
14-Jun-2007 5:21:52
herbal phentermine
14-Jun-2007 6:15:28
no prescription
15-Jun-2007 7:45:53
18-Jun-2007 4:46:31
20-Jun-2007 0:03:18
31-Mar-2008 5:34:17
, cheap diazepam, [url=""]cheap diazepam[/url], :-), cheap valium, [url=""]cheap valium[/url], >:DD, cheap vicodin, [url=""]cheap vicodin[/url], >:OOO, order ambien, [url=""]order ambien[/url], cucw, buy hydrocodone, [url=""]buy hydrocodone[/url], =-(, cheap ionamin, [url=""]cheap ionamin[/url], 723236, buy zolpidem, [url=""]buy zolpidem[/url], bfsgwz, cheap adipex, [url=""]cheap adipex[/url], 731629,
31-Mar-2008 16:31:07
, cheap valium, [url=""]cheap valium[/url], mckya, buy vicodin, [url=""]buy vicodin[/url], 979366, order hydrocodone, [url=""]order hydrocodone[/url], 024, order ionamin, [url=""]order ionamin[/url], mxz, cheap phentermine, [url=""]cheap phentermine[/url], 8))), buy cialis, [url=""]buy cialis[/url], 663360, buy zolpidem online, [url=""]buy zolpidem online[/url], 79433, cheap adipex, [url=""]cheap adipex[/url], gcl,

Post a Comment:



Blog id:





Updated 25-Apr-2018 - Edit - Copyright REBOL Technologies -