Backtrace without skips needed

T

Tobias Peters

Is there a way to tell ruby that it must never skip levels in the
backtrace like this:

/lib/ruby/1.8/net/ftp.rb:192:in `readline': End of file reached (EOFError)
from /lib/ruby/1.8/net/ftp.rb:192:in `getline'
from /lib/ruby/1.8/net/ftp.rb:202:in `getmultiline'
from /lib/ruby/1.8/net/ftp.rb:216:in `getresp'
from /lib/ruby/1.8/net/ftp.rb:232:in `voidresp'
from /lib/ruby/1.8/net/ftp.rb:157:in `connect'
from /lib/ruby/1.8/net/ftp.rb:155:in `synchronize'
from /lib/ruby/1.8/net/ftp.rb:158:in `connect'
from /lib/ruby/1.8/net/ftp.rb:119:in `initialize'
... 7 levels...
from getp.rb:85:in `get_from'
from getp.rb:85:in `open'
from getp.rb:85:in `get_from'
from getp.rb:120

This is the backtrace the ruby interpreter prints out when it terminates
due to an unhandled exception.

Thanks,
Tobias
 
Y

Yukihiro Matsumoto

Hi,

In message "Backtrace without skips needed"

|Is there a way to tell ruby that it must never skip levels in the
|backtrace like this:

begin
foo
rescue => e
cb = e.backtrace
print cb.shift, ":", e.message, "\n"
cb.each{|c| print "\tfrom ", c, "\n"}
exit 1
end

matz.
 
H

Hugh Sasse Staff Elec Eng

|Is there a way to tell ruby that it must never skip levels in the
|backtrace like this:

begin
foo
rescue => e
cb = e.backtrace
print cb.shift, ":", e.message, "\n"
cb.each{|c| print "\tfrom ", c, "\n"}
exit 1
end

matz.

I, for one, won't remember this when I need it, so may I suggest
that this be wrapped for convenience in a standard method, so we can
do
begin
foo
rescue => e
e.full_backtrace # or some other name
exit 1
end
in some near-future ruby, please?

Thank you,
Hugh
 
Z

Zoran Lazarevic

Is there a way to tell ruby that it must never skip levels in the

You might want to enclose your 'main' into begin/rescue/end and print
the stacktrace for yourself

begin
main # main raises an exception, e.g. 1/0
rescue
caller.each{|x| p x}
end
 
T

Tobias Peters

Thank you for your replies.

What I was really hoping for a is a command line switch to the ruby
interpreter, i.e. something that I do not need to modify my source for.

Maybe I can require some file on the command line with
-r complete_backtrace that implements $!.backtrace printing
in an atexit handler? Otherwise, I plan to modify my ruby interpreter to
make the maximum number of stack frames printed configurable via
environment variable.

Will post when I have something.

Tobias
 
R

Robert Klemme

Zoran Lazarevic said:
You might want to enclose your 'main' into begin/rescue/end and print
the stacktrace for yourself

begin
main # main raises an exception, e.g. 1/0
rescue
caller.each{|x| p x}
end

I'm afraid, that won't output the interesting part, namely the stack
levels *below* this method that make it possible to find the source of the
exception.

Regards

robert
 
R

Robert Klemme

Tobias Peters said:
Thank you for your replies.

What I was really hoping for a is a command line switch to the ruby
interpreter, i.e. something that I do not need to modify my source for.

Maybe I can require some file on the command line with
-r complete_backtrace that implements $!.backtrace printing
in an atexit handler? Otherwise, I plan to modify my ruby interpreter to
make the maximum number of stack frames printed configurable via
environment variable.

Before you start, maybe Matz could commment on the idea of having a
~/.rubyrc that controls interpreter bahavior (of course together with a
switch that allows for ignoring this file). Then maybe he / we / you
implement this feature for the complete runtime and all / several command
line switches.

Kind regards

robert
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Backtrace without skips needed"

|Before you start, maybe Matz could commment on the idea of having a
|~/.rubyrc that controls interpreter bahavior (of course together with a
|switch that allows for ignoring this file). Then maybe he / we / you
|implement this feature for the complete runtime and all / several command
|line switches.

I'd say "no" to the rc idea. I believe it's application matter, not
language matter, i.e. there can be no common configuration among all
applications written in Ruby.

matz.
 
G

Gavin Sinclair

Is there a way to tell ruby that it must never skip levels in the
backtrace like this:
/lib/ruby/1.8/net/ftp.rb:192:in `readline': End of file reached (EOFError)
from /lib/ruby/1.8/net/ftp.rb:192:in `getline'
from /lib/ruby/1.8/net/ftp.rb:202:in `getmultiline'
from /lib/ruby/1.8/net/ftp.rb:216:in `getresp'
from /lib/ruby/1.8/net/ftp.rb:232:in `voidresp'
from /lib/ruby/1.8/net/ftp.rb:157:in `connect'
from /lib/ruby/1.8/net/ftp.rb:155:in `synchronize'
from /lib/ruby/1.8/net/ftp.rb:158:in `connect'
from /lib/ruby/1.8/net/ftp.rb:119:in `initialize'
... 7 levels...
from getp.rb:85:in `get_from'
from getp.rb:85:in `open'
from getp.rb:85:in `get_from'
from getp.rb:120


If you catch the exception ('err') and 'puts err.backtrace', you'll
get the full story in the following form:

...
e.rb:7:in `b'
e.rb:3:in `a'
e.rb:7:in `b'
e.rb:3:in `a'
e.rb:7:in `b'
...

If you do a more complicated printing routine as Matz suggested, you
get this:

...
from e.rb:7:in `b'
from e.rb:3:in `a'
from e.rb:7:in `b'
from e.rb:3:in `a'
from e.rb:7:in `b'
from e.rb:3:in `a'
...

For information purposes, I'd just 'puts err.backtrace' and be done
with it. Note in both cases, the full trace is printed, not "..."!

Gavin
 
R

Robert Klemme

Yukihiro Matsumoto said:
Hi,

In message "Re: Backtrace without skips needed"

|Before you start, maybe Matz could commment on the idea of having a
|~/.rubyrc that controls interpreter bahavior (of course together with a
|switch that allows for ignoring this file). Then maybe he / we / you
|implement this feature for the complete runtime and all / several command
|line switches.

I'd say "no" to the rc idea. I believe it's application matter, not
language matter, i.e. there can be no common configuration among all
applications written in Ruby.

Sounds reasonable considering that a change to the rc file could have
suprising side effects on other applications. Thanks for pointing my nose
on that!

Regards

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,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top