Binding & Exception, how to get more introspection ?

J

Jean-Hugues ROBERT

Hi,

I am using some tracing to debug my code. When an exception bombs,
I display it. Together with the call stack (thanks to caller()). Fine.

def something_broken()
some_bug
rescue Exception; debug
end

def debug
p $!
p caller
end

Yet, more information is welcome about the context when the
exception bombed. Things like local variables and "self" for example:

# empty.rb

def something_broken()
myvar = "hello"
some_bug
rescue Exception; debug binding
end

def debug( binding )
p $!
p caller
eval( "p self; local_variables.each do |v| p v, '==', eval( v) end",
binding)
end

something_broken()

Result:
#<NameError: undefined local variable or method `some_bug' for main:Object>
["empty.rb:11:in `something_broken'", "empty.rb:20"]
main
"myvar"
"=="
"hello"

Neat ! I can display both "self" and the local variables.

Yet I want more. Things like the local variables and self from
the broken method's caller and so on up to the top of the stack.

Questions:

1 - Is there a more direct way to get the binding ?
I would rather write:
rescue Exception; debug
then:
rescue Exception; debug binding

2 - Looking for some Exception##binding() that would return
the Binding where the exception bombed, I found a private
Exception##binding(). It does return a Binding, yet...
In the returned Binding, self is the exception itself, not the
self when the exception bombed. I have no clue what this is for,
any idea ?

3 - Is there a way to get the binding not only of the caller
but also the caller's caller and so on ?

4 - Is there a way to avoid using eval() to get "self" and the
local variables ?

5 - Is there a variant of caller() that would provide Binding objects ?
Or a variant of binding(), say: binding( 0), to walk the stack ?

6 - I think that I could get by with set_trace_proc() handling
"call" and "return" events, but this is way to slow for me. Does
anything faster exists ?

Thanks in advance.

Jean-Hugues Robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top