summaryrefslogtreecommitdiff
path: root/test/ruby/test_backtrace.rb
AgeCommit message (Collapse)Author
2022-02-17Add Thread.each_caller_locationJeremy Evans
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] Notes: Merged: https://github.com/ruby/ruby/pull/5445
2021-08-06Make backtrace generation work outward from current frameJeremy Evans
This fixes multiple bugs found in the partial backtrace optimization added in 3b24b7914c16930bfadc89d6aff6326a51c54295. These bugs occurs when passing a start argument to caller where the start argument lands on a iseq frame without a pc. Before this commit, the following code results in the same line being printed twice, both for the #each method. ```ruby def a; [1].group_by { b } end def b; puts(caller(2, 1).first, caller(3, 1).first) end a ``` After this commit and in Ruby 2.7, the lines are different, with the first line being for each and the second for group_by. Before this commit, the following code can either segfault or result in an infinite loop: ```ruby def foo caller_locations(2, 1).inspect # segfault caller_locations(2, 1)[0].path # infinite loop end 1.times.map { 1.times.map { foo } } ``` After this commit, this code works correctly. This commit completely refactors the backtrace handling. Instead of processing the backtrace from the outermost frame working in, process it from the innermost frame working out. This is much faster for partial backtraces, since you only access the control frames you need to in order to construct the backtrace. To handle cfunc frames in the new design, they start out with no location information. We increment a counter for each cfunc frame added. When an iseq frame with pc is accessed, after adding the iseq backtrace location, we use the location for the iseq backtrace location for all of the directly preceding cfunc backtrace locations. If the last backtrace line is a cfunc frame, we continue scanning for iseq frames until the end control frame, and use the location information from the first one for the trailing cfunc frames in the backtrace. As only rb_ec_partial_backtrace_object uses the new backtrace implementation, remove all of the function pointers and inline the functions. This makes the process easier to understand. Restore the Ruby 2.7 implementation of backtrace_each and use it for all the other functions that called backtrace_each other than rb_ec_partial_backtrace_object. All other cases requested the entire backtrace, so there is no advantage of using the new algorithm for those. Additionally, there are implicit assumptions in the other code that the backtrace processing works inward instead of outward. Remove the cfunc/iseq union in rb_backtrace_location_t, and remove the prev_loc member for cfunc. Both cfunc and iseq types can now have iseq and pc entries, so the location information can be accessed the same way for each. This avoids the need for a extra backtrace location entry to store an iseq backtrace location if the final entry in the backtrace is a cfunc. This is also what fixes the segfault and infinite loop issues in the above bugs. Here's Ruby pseudocode for the new algorithm, where start and length are the arguments to caller or caller_locations: ```ruby end_cf = VM.end_control_frame.next cf = VM.start_control_frame size = VM.num_control_frames - 2 bt = [] cfunc_counter = 0 if length.nil? || length > size length = size end while cf != end_cf && bt.size != length if cf.iseq? if cf.instruction_pointer? if start > 0 start -= 1 else bt << cf.iseq_backtrace_entry cf_counter.times do |i| bt[-1 - i].loc = cf.loc end cfunc_counter = 0 end end elsif cf.cfunc? if start > 0 start -= 1 else bt << cf.cfunc_backtrace_entry cfunc_counter += 1 end end cf = cf.prev end if cfunc_counter > 0 while cf != end_cf if (cf.iseq? && cf.instruction_pointer?) cf_counter.times do |i| bt[-i].loc = cf.loc end end cf = cf.prev end end ``` With the following benchmark, which uses a call depth of around 100 (common in many Ruby applications): ```ruby class T def test(depth, &block) if depth == 0 yield self else test(depth - 1, &block) end end def array Array.new end def first caller_locations(1, 1) end def full caller_locations end end t = T.new t.test((ARGV.first || 100).to_i) do Benchmark.ips do |x| x.report ('caller_loc(1, 1)') {t.first} x.report ('caller_loc') {t.full} x.report ('Array.new') {t.array} x.compare! end end ``` Results before commit: ``` Calculating ------------------------------------- caller_loc(1, 1) 281.159k (_ 0.7%) i/s - 1.426M in 5.073055s caller_loc 15.836k (_ 2.1%) i/s - 79.450k in 5.019426s Array.new 1.852M (_ 2.5%) i/s - 9.296M in 5.022511s Comparison: Array.new: 1852297.5 i/s caller_loc(1, 1): 281159.1 i/s - 6.59x (_ 0.00) slower caller_loc: 15835.9 i/s - 116.97x (_ 0.00) slower ``` Results after commit: ``` Calculating ------------------------------------- caller_loc(1, 1) 562.286k (_ 0.8%) i/s - 2.858M in 5.083249s caller_loc 16.402k (_ 1.0%) i/s - 83.200k in 5.072963s Array.new 1.853M (_ 0.1%) i/s - 9.278M in 5.007523s Comparison: Array.new: 1852776.5 i/s caller_loc(1, 1): 562285.6 i/s - 3.30x (_ 0.00) slower caller_loc: 16402.3 i/s - 112.96x (_ 0.00) slower ``` This shows that the speed of caller_locations(1, 1) has roughly doubled, and the speed of caller_locations with no arguments has improved slightly. So this new algorithm is significant faster, much simpler, and fixes bugs in the previous algorithm. Fixes [Bug #18053] Notes: Merged: https://github.com/ruby/ruby/pull/4671
2021-03-24Ensure that caller respects the start argumentJeremy Evans
Previously, if there were ignored frames (iseq without pc), we could go beyond the requested start frame. This has two changes: 1) Ensure that we don't look beyond the start frame by using last_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(last_cfp) until the desired start frame is reached. 2) To fix the failures caused by change 1), which occur when a limited number of frames is requested, scan the VM stack before allocating backtrace frames, looking for ignored frames. This is complicated if there are ignored frames before and after the start, in which case we need to scan until the start frame, and then scan backwards, decrementing the start value until we get to the point where start will result in the number of requested frames. This fixes a Rails test failure. Jean Boussier was able to to produce a failing test case outside of Rails. Co-authored-by: Jean Boussier <jean.boussier@gmail.com> Notes: Merged: https://github.com/ruby/ruby/pull/4237
2021-02-19Fix backtrace to not skip frames with iseq without pcJeremy Evans
Previously, frames with iseq but no pc were skipped (even before the refactoring in 3b24b7914c16930bfadc89d6aff6326a51c54295). Because the entire backtrace was procesed before the refactoring, this was handled by using later frames instead. However, after the refactoring, we need to handle those frames or they get lost. Keep two iteration counters when iterating, one for the desired backtrace size (so we generate the desired number of frames), and one for the actual backtrace size (so we don't process off the end of the stack). When skipping over an iseq frame with no pc, decrement the counter for the desired backtrace, so it will continue to process the expected number of backtrace frames. Fixes [Bug #17581] Notes: Merged: https://github.com/ruby/ruby/pull/4120
2019-03-21Fix a wrong lineno in backtrace for cfuncmame
lineno is an int, and INT2FIX(0) was assigned. [Bug #15719] [ruby-core:91911] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67326 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-28Revert "Add test for cause on pty"naruse
This reverts commit r65422. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65424 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-28Add test for cause on ptynaruse
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65422 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-27fix testnaruse
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65397 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-27Print exception's cause like Javanaruse
Print `cause` of the exception if the exception is not caught and printed its backtraces and error message [Feature #8257] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65393 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-10-08Remove unnecessary `require 'thread'`kazu
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60139 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-08-30Use qualified namesnobu
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56037 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-02-19test/ruby: suppress parser warningsnobu
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53872 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-12-16Add frozen_string_literal: false for all filesnaruse
When you change this to true, you may need to add more tests. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53141 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-10-05* proc.c: enable optimization of Proc#call.ko1
[Feature #11569] * NEWS: write about this optimization and incompatibilities. * test/ruby/test_backtrace.rb: catch up this fix. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52050 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-02-01test_backtrace.rb: add testsnobu
* test/ruby/test_backtrace.rb (test_caller_locations_base_label): test for Thread::Backtrace::Location#base_label. * test/ruby/test_backtrace.rb (test_caller_locations_label): test for Thread::Backtrace::Location#label. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49466 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-01-31test_backtrace.rb: add testnobu
* test/ruby/test_backtrace.rb (test_caller_locations_lineno): test for Thread::Backtrace::Location#lineno. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49462 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-01-31test_backtrace.rb: split testnobu
* test/ruby/test_backtrace.rb (test_caller_locations_absolute_path): split from test_caller_locations_path. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49461 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-01-31test_backtrace.rb: add testnobu
* test/ruby/test_backtrace.rb (test_caller_locations_path): test for Thread::Backtrace::Location#path and #absolute_path. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49460 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-05-26test/ruby: fix leaked threadsnobu
* test/thread/test_backtrace.rb: join work threads not to leak threads. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46132 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-05-26test/ruby: fix leaked threadsnobu
* test/thread/test_{backtrace,beginendblock,proc,threadgroup}.rb: join work threads not to leak threads. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46131 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-05-21vm.c: rewind cfpnobu
* vm.c (m_core_hash_{from_ary,merge_{ary,ptr,kwd}}): rewind cfp to show proper backtrace. [ruby-dev:35820] [Bug #416] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46037 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-05-21test_backtrace.rb: r18554 testsnobu
* test/ruby/test_backtrace.rb (test_core_backtrace_{alias,undef}): add tests for r18554. [ruby-dev:35820] [Bug #416] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46036 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-03-02* test/ruby/test_backtrace.rb: get rid of warnings. unused variable,usa
shadowing. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45246 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-12-25* vm_insnhelper.c (argument_error): insert dummy frame to makeko1
a backtrace object intead of modify backtrace string array. [Bug #9295] * test/ruby/test_backtrace.rb: add a test for this patch. fix test to compare a result of Exception#backtrace with a result of Exception#backtrace_locations. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44411 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-12-13* error.c: add Exception#backtrace_locations.ko1
Now, there are no setter and independent from Exception#backtrace. [Feature #8960] * eval.c (setup_exception): set backtrace locations for `bt_location' special attribute. * vm_backtrace.c (rb_backtrace_to_location_ary): added. * internal.h: ditto. * test/ruby/test_backtrace.rb: add a test for Exception#backtrace_locations. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44170 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-12-11revert r44133. See https://bugs.ruby-lang.org/issues/8886 detailsko1
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44138 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-12-11* eval.c (rb_raise_jump): pop frame after setup exception.ko1
Patches by deivid (David Rodriguez). [Bug #8886] * test/minitest/test_minitest_unit.rb: catch up this change. * test/ruby/test_backtrace.rb: ditto. * test/ruby/test_settracefunc.rb: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44133 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-09-10* vm_backtrace.c (vm_backtrace_to_ary): Ignore the second argument ifsorah
it is nil. [Bug #8884] [ruby-core:57094] * test/ruby/test_backtrace.rb (test_caller_with_nil_length): Test for above. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42905 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-02-23test_backtrace.rb: test Thread#backtrace_locations with rangezzak
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@39431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-12-31* test/ruby/test_backtrace.rb: Add test for r37957 [Feature #7434]zzak
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38664 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-12-01* test/ruby/test_backtrace.rb: add a test forko1
Thread::Backtrace::Location#inspect. BTW, tests for `caller_locations' are not enough. Any volunteers are welcome. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38122 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-11-19* thread.c: add `Thread#backtrace_locations' method.ko1
This method is similart to `caller_locations' method for specific method. And fix to accept `level' and `n' parameters for `Thread#backtrace' and `Thread#backtrace_locations'. `caller' (and `caller_locations') do not return `caller' method frame. However, `Thread#backtrace' (and `Thread#backtrace_locations') return `Thread#backtrace' method frame itself if `Thread.current.backtrace' was called. * vm_backtrace.c: ditto. * internal.h: ditto. * test/ruby/test_backtrace.rb: add tests. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37716 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-06-22* test/ruby/test_backtrace.rb: decrease recursion depthko1
to reduce consuming stack size. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@36177 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-06-04* test/ruby/test_backtrace.rb: fix test.ko1
Windows path includes `:' character. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35900 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-06-02* vm_backtrace.c: change names.ko1
(1) Class name: RubyVM::FrameInfo -> RubyVM::Backtrace::Location. (2) Method name: RubyVM::FrameInfo.caller -> Kernel.caller_locations. (3) Instance methods of RubyVM::FrameInfo (RubyVM::Backtrace::Location) (3-1) name -> label (3-2) basename -> base_label (basename is confusing with File.basename) (3-3) line_no -> lineno (We have already similar name File#lineno, commented by kou [ruby-dev:45686]). (3-4) filename -> path. (3-5) filepath -> absolute_path. (3-5) iseq -> removed (we will make other APIs to access iseq and other information of frame for debugging). * test/ruby/test_backtrace.rb: apply above changes. And apply comment from kou [ruby-dev:45686]. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35873 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-05-26* test/ruby/test_backtrace.rb (test_caller_lev):ko1
decreaze recursion size. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35804 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-05-26* vm.c (RubyVM::FrameInfo): add a class to access each frameko1
information. You don't need to parse strings from caller(). FrameInfo has the following methods: FrameInfo#name: method name, class name, etc with decorations. FrameInfo#basename: name without decorations. FrameInfo#line_no: line number. FrameInfo#filename: file name. FrameInfo#filepath: full filepath. FrameInfo#iseq: iseq if it is iseq frame (defined by ruby script) FrameInfo#to_s: return caller() method style string. RubyVM::FrameInfoFrameInfo.caller(n, lev) returns array of FrameInfo objects. The name "RubyVM::FrameInfoFrameInfo.caller" is long and ambiguous (same as caller() method), we need to change the name before Ruby 2.0 release. Good names or comments are welcome. * test/ruby/test_backtrace.rb: add a test for above change. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35801 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-05-25* vm_eval.c (rb_f_caller): caller() method accepts second optionalko1
argument `n' which specify how many frames should return. For example, `caller(0, 1)' returns only one frame information which calls caller() method. If there are less than n frame information, then all frame information are returned. If n is 0, then always return []. This fix is part of [ruby-dev:42345] [Ruby 1.9-Feature#3917]. However, performance and features are not enough. RDoc is also not available. * test/ruby/test_backtrace.rb: add a test for above. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35792 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-05-25* properties.nobu
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35788 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-05-25* vm.c: refactoring backtrace related funcitons.ko1
(1) unify similar functions (rb_backtrace_each() and backtrace_object()). backtrace_each() is a unified function. variation: a) backtrace_object(): create backtrace object. b) vm_backtrace_str_ary(): create bt as an array of string. c) vm_backtrace_print(): print backtrace to specified file. d) rb_backtrace_print_as_bugreport(): print backtrace on bugreport style. (2) remove rb_backtrace_each(). Use backtrace_each() instead. (3) chang the type of lev parameter to size_t. a) lev == 0 means current frame (exception, etc use it). b) lev == 1 means upper frame (caller(0) use it). * vm_core.h, vm_dump.c, vm_eval.c: ditto. * vm.c (backtrace_object(), vm_backtrace_str_ary()): fix to return a correct size of caller(lev) array. Let n be a "caller(0).size" then ln as caller(lev).size should be (n - lev). However, the previous implementation returns a wrong size array (ln > n - lev). [ruby-dev:45673] * test/ruby/test_backtrace.rb: add tests for backtrace. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35787 b2dd03c8-39d4-4d8f-98ff-823fe69b080e