REBOL 3.0

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:

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

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

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

REFINEMENTS:
     /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?

33 Comments

Comments:

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? ;)

CALL-STACK

STACK-TRACE

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
WOW!

can I say anything more?

Ladislav
3-Apr-2007 2:46:14
I second Max's opinion :-)
Reisacher
3-Apr-2007 3:08:28
How about track or back-track?
Oldes
3-Apr-2007 3:34:54
I like track
Volker
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.

Henrik
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?
Steeve
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.
Sunanda
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.)
Stewart
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
94X0SU
medication
12-Jun-2007 4:33:56
KLNZQD
buy generic
12-Jun-2007 4:44:19
QM8P9S
buy carisoprodol online
12-Jun-2007 8:57:39
CYTMXA
sildenafil citrate dosag
13-Jun-2007 7:15:11
OQIXLQ
buy carisoprodol
13-Jun-2007 7:16:52
U64UU6
order tramadol online
13-Jun-2007 7:30:38
B8Q2IS
cialis medication
13-Jun-2007 12:02:47
IRJ807
buy cialis online
13-Jun-2007 12:05:36
HCU5CS
generic cialis
13-Jun-2007 12:08:20
3PR5MZ
ultram
13-Jun-2007 15:37:48
ysdzh
generic
14-Jun-2007 5:21:52
D8IJ46
herbal phentermine
14-Jun-2007 6:15:28
yjbs7
no prescription
15-Jun-2007 7:45:53
yen71
cheap
18-Jun-2007 4:46:31
EDE7AF
overnight
20-Jun-2007 0:03:18
3JPC98
sdhfthjty
31-Mar-2008 5:34:17
, cheap diazepam, [url="https://www.blogger.com/comment.g?blogID=4319045761552434706&postID=2066281014770147922#1"]cheap diazepam[/url], :-), cheap valium, [url="https://www.blogger.com/comment.g?blogID=4319045761552434706&postID=7700234733208766714#1"]cheap valium[/url], >:DD, cheap vicodin, [url="https://www.blogger.com/comment.g?blogID=8719781394278775210&postID=8692894848146421003#1"]cheap vicodin[/url], >:OOO, order ambien, [url="https://www.blogger.com/comment.g?blogID=8450182720959847494&postID=2192987447046132871#1"]order ambien[/url], cucw, buy hydrocodone, [url="https://www.blogger.com/comment.g?blogID=8719781394278775210&postID=8085828541409879706#1"]buy hydrocodone[/url], =-(, cheap ionamin, [url="https://www.blogger.com/comment.g?blogID=4792629741707035281&postID=387113549343216409#1"]cheap ionamin[/url], 723236, buy zolpidem, [url="https://www.blogger.com/comment.g?blogID=8450182720959847494&postID=5340326364614393799#1"]buy zolpidem[/url], bfsgwz, cheap adipex, [url="https://www.blogger.com/comment.g?blogID=4792629741707035281&postID=2802218961036385409#1"]cheap adipex[/url], 731629,
e575r8
31-Mar-2008 16:31:07
, cheap valium, [url="https://www.blogger.com/comment.g?blogID=4319045761552434706&postID=7700234733208766714#1"]cheap valium[/url], mckya, buy vicodin, [url="https://www.blogger.com/comment.g?blogID=8719781394278775210&postID=8692894848146421003#1"]buy vicodin[/url], 979366, order hydrocodone, [url="https://www.blogger.com/comment.g?blogID=8719781394278775210&postID=8085828541409879706#1"]order hydrocodone[/url], 024, order ionamin, [url="https://www.blogger.com/comment.g?blogID=4792629741707035281&postID=387113549343216409#1"]order ionamin[/url], mxz, cheap phentermine, [url="https://www.blogger.com/comment.g?blogID=4792629741707035281&postID=8441306125943653822#1"]cheap phentermine[/url], 8))), buy cialis, [url="https://www.blogger.com/comment.g?blogID=165084756362055358&postID=5924893240209976299#1"]buy cialis[/url], 663360, buy zolpidem online, [url="https://www.blogger.com/comment.g?blogID=8450182720959847494&postID=5340326364614393799#1"]buy zolpidem online[/url], 79433, cheap adipex, [url="https://www.blogger.com/comment.g?blogID=4792629741707035281&postID=2802218961036385409#1"]cheap adipex[/url], gcl,

Post a Comment:

You can post a comment here. Keep it on-topic.

Name:

Blog id:

R3-0075


Comment:


 Note: HTML tags allowed for: b i u li ol ul font span div a p br pre tt blockquote
 
 

This is a technical blog related to the above topic. We reserve the right to remove comments that are off-topic, irrelevant links, advertisements, spams, personal attacks, politics, religion, etc.

REBOL 3.0
Updated 25-Mar-2017 - Edit - Copyright REBOL Technologies - REBOL.net