This post originated from an RSS feed registered with Ruby Buzz
by Eigen Class.
Original Post: Line numbers, error reporting and backtraces
Feed Title: Eigenclass
Feed URL: http://feeds.feedburner.com/eigenclass
Feed Description: Ruby stuff --- trying to stay away from triviality.
The AST ruby creates when parsing your code doesn't
contain that much information regarding its format. In particular, it
only stores one line number per node. Why care? It turns out that this is what Ruby
needs to generate meaningful backtrace info and report events to the
trace_func and/or event_hook*1.
When you have chained method calls with multi-line blocks, the line numbers
reported by Ruby will often be wrong:
class Object;def foo;callerendenda=foodo# line 3, this is where Ruby tells us the last #foo call# # is being performed from#end.foodo#end.foo# <--- when it's actually here (line 8)pa# >> ["-:3"]
I patched ruby to set the NODE line number to the last line (the one with
the end block delimiter), which helps with chained calls, but might be
a bit confusing regarding LINE events.
I used this program to show the
events reported by Ruby to the trace_func and print the call stack at each point:
class Objectdef foo;1endendset_trace_funclambda{|event,file,line,id,binding,classname|puts"="*40puts"%8s :%-2d %5s %8s"%[event,line,id,classname]putscaller.map{|x|x.inspect}}foodo# line 111end.foodo# line 132end[1,# line 182,3+4.abs,4].foo# line 23
These are the events and backtrace information generated with the above script, for
both the unpatched interpreter and the modified one, side by side: