Skip to content

Wrong line numbers in Kernel#caller inside set_trace_func (with code to reproduce) #4051

@ivoanjo

Description

@ivoanjo

I was trying to see if I could get pry-nav to work with JRuby. It doesn't have a lot of features, but it's a straightforward use of set_trace_func and I wanted to experiment with it a bit.

I hit some issues, and investigating them led me to discover that when running code inside set_trace_func and then calling Kernel#caller (such as when stepping) leads to line numbers being wrong in the stack trace, which confuses pry.

Environment

Running jruby 9.1.2.0 (2.3.0) 2016-05-26 7357c8f OpenJDK 64-Bit Server VM 25.91-b14 on 1.8.0_91-8u91-b14-3ubuntu1~15.10.1-b14 [linux-x86_64] on Ubuntu 15.10.

Expected Behavior

Example code:

def sum(arg1, arg2)
  arg1 + arg2
end

def crazy_sums
  sum(1, 2)
end

set_trace_func proc { |type, _, line, *stuff|
  unless ['c-return', 'c-call'].include? type
    puts "Event #{type} @ #{line}, caller: #{caller.first}"
  end
}

crazy_sums

Output in MRI 2.3.1:

Event line @ 15, caller: test.rb:15:in `<main>'
Event call @ 5, caller: test.rb:5:in `crazy_sums'
Event line @ 6, caller: test.rb:6:in `crazy_sums'
Event call @ 1, caller: test.rb:1:in `sum'
Event line @ 2, caller: test.rb:2:in `sum'
Event return @ 3, caller: test.rb:3:in `sum'
Event return @ 7, caller: test.rb:7:in `crazy_sums'

With MRI, caller.first matches the line we get in set_trace_func.

Actual Behavior

Output in JRuby:

Event line @ 15, caller: test.rb:9:in `<top>'
Event call @ 5, caller: test.rb:15:in `crazy_sums'
Event line @ 6, caller: test.rb:15:in `crazy_sums'
Event call @ 1, caller: test.rb:6:in `sum'
Event line @ 2, caller: test.rb:6:in `sum'
Event return @ 3, caller: test.rb:2:in `sum'
Event return @ 7, caller: test.rb:6:in `crazy_sums'

With JRuby, the line numbers don't match the line we get in set_trace_func.

There is also a minor difference where MRI calls the top-level <main> while JRuby uses <top>. This seems minor, but I can report it as a separate issue if needed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions