Skip to content

Add Thread.each_caller_location - #5445

Merged
jeremyevans merged 1 commit into
ruby:masterfrom
jeremyevans:thread_each_caller_location-16663
Feb 17, 2022
Merged

Add Thread.each_caller_location#5445
jeremyevans merged 1 commit into
ruby:masterfrom
jeremyevans:thread_each_caller_location-16663

Conversation

@jeremyevans

Copy link
Copy Markdown
Contributor

This method takes a block and yields Thread::Backtrace::Location
objects to the block. It does not take arguments, and always
starts at the default frame that caller_locations would start at.

Implements [Feature #16663]

This method takes a block and yields Thread::Backtrace::Location
objects to the block.  It does not take arguments, and always
starts at the default frame that caller_locations would start at.

Implements [Feature ruby#16663]
@headius

headius commented Jan 18, 2022

Copy link
Copy Markdown
Contributor

I have done a quick implementation of this in jruby/jruby#7014.

It passes all tests except for the to_enum due to our Enumerator implementation always showing up in the stack. The caller_locations array and an array captured inside the enumerator do not match:

$ jruby -e 'def foo; Enumerator.new {|y| caller_locations.each {|l| y.yield l}}.to_a; end; pp foo'
["-e:1:in `to_a'", "-e:1:in `foo'", "-e:1:in `<main>'"]

$ jruby -e 'def foo; caller_locations; end; pp foo'
["-e:1:in `<main>'"]

This is filtering two elements in both cases: the caller_locations call and an each call.

The enum form makes it difficult to filter out the top N elements that are not relevant for caller since the actual trace capture then happens at a deeper level (under each at a minimum, but also under to_a). I am a bit confused why these elements do not show up in the CRuby version, since it seems to have a similar difference in stack:

$ rvm ruby-3.1 do ruby -e 'def foo; Enumerator.new {|y| caller_locations.each {|l| y.yield l}}.to_a; end; pp foo'
["-e:1:in `each'", "-e:1:in `each'", "-e:1:in `to_a'", "-e:1:in `foo'", "-e:1:in `<main>'"]

$ rvm ruby-3.1 do ruby -e 'def foo; caller_locations; end; pp foo'
["-e:1:in `<main>'"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants