[前][次][番号順一覧][スレッド一覧]

ruby-changes:62597

From: Samuel <ko1@a...>
Date: Mon, 17 Aug 2020 21:57:00 +0900 (JST)
Subject: [ruby-changes:62597] 0a218a97ad (master): Expose ec -> backtrace (internal) and use it to implement fiber backtrace.

https://git.ruby-lang.org/ruby.git/commit/?id=0a218a97ad

From 0a218a97ad31f06eb7f59ccdd428fd46c4b93982 Mon Sep 17 00:00:00 2001
From: Samuel Williams <samuel.williams@o...>
Date: Sat, 15 Aug 2020 15:36:18 +1200
Subject: Expose ec -> backtrace (internal) and use it to implement fiber
 backtrace.

See <https://bugs.ruby-lang.org/issues/16815> for more details.

diff --git a/cont.c b/cont.c
index 139c12c..654fc55 100644
--- a/cont.c
+++ b/cont.c
@@ -2281,6 +2281,18 @@ rb_fiber_raise(int argc, VALUE *argv, VALUE fiber) https://github.com/ruby/ruby/blob/trunk/cont.c#L2281
     return rb_fiber_resume_kw(fiber, -1, &exc, RB_NO_KEYWORDS);
 }
 
+static VALUE
+rb_fiber_backtrace(int argc, VALUE *argv, VALUE fiber)
+{
+    return rb_vm_backtrace(argc, argv, &fiber_ptr(fiber)->cont.saved_ec);
+}
+
+static VALUE
+rb_fiber_backtrace_locations(int argc, VALUE *argv, VALUE fiber)
+{
+    return rb_vm_backtrace_locations(argc, argv, &fiber_ptr(fiber)->cont.saved_ec);
+}
+
 /*
  *  call-seq:
  *     fiber.transfer(args, ...) -> obj
@@ -2533,6 +2545,8 @@ Init_Cont(void) https://github.com/ruby/ruby/blob/trunk/cont.c#L2545
     rb_define_method(rb_cFiber, "blocking?", rb_fiber_blocking_p, 0);
     rb_define_method(rb_cFiber, "resume", rb_fiber_m_resume, -1);
     rb_define_method(rb_cFiber, "raise", rb_fiber_raise, -1);
+    rb_define_method(rb_cFiber, "backtrace", rb_fiber_backtrace, -1);
+    rb_define_method(rb_cFiber, "backtrace_locations", rb_fiber_backtrace_locations, -1);
     rb_define_method(rb_cFiber, "to_s", fiber_to_s, 0);
     rb_define_alias(rb_cFiber, "inspect", "to_s");
 
diff --git a/internal/vm.h b/internal/vm.h
index 657202b..1ae2ba0 100644
--- a/internal/vm.h
+++ b/internal/vm.h
@@ -107,6 +107,8 @@ void rb_print_backtrace(void); https://github.com/ruby/ruby/blob/trunk/internal/vm.h#L107
 /* vm_backtrace.c */
 VALUE rb_vm_thread_backtrace(int argc, const VALUE *argv, VALUE thval);
 VALUE rb_vm_thread_backtrace_locations(int argc, const VALUE *argv, VALUE thval);
+VALUE rb_vm_backtrace(int argc, const VALUE * argv, struct rb_execution_context_struct * ec);
+VALUE rb_vm_backtrace_locations(int argc, const VALUE * argv, struct rb_execution_context_struct * ec);
 VALUE rb_make_backtrace(void);
 void rb_backtrace_print_as_bugreport(void);
 int rb_backtrace_p(VALUE obj);
diff --git a/test/fiber/test_backtrace.rb b/test/fiber/test_backtrace.rb
new file mode 100644
index 0000000..2d637c0
--- /dev/null
+++ b/test/fiber/test_backtrace.rb
@@ -0,0 +1,22 @@ https://github.com/ruby/ruby/blob/trunk/test/fiber/test_backtrace.rb#L1
+# frozen_string_literal: true
+require 'test/unit'
+require 'fiber'
+
+class TestFiberBacktrace < Test::Unit::TestCase
+  def test_backtrace
+    backtrace = Fiber.current.backtrace
+    assert_kind_of Array, backtrace
+    assert_match /test_backtrace/, backtrace[0]
+  end
+
+  def test_backtrace_locations
+    backtrace = Fiber.current.backtrace_locations
+    assert_kind_of Array, backtrace
+    assert_match /test_backtrace_locations/, backtrace[1].label
+  end
+
+  def test_local_backtrace
+    backtrace = Fiber.current.backtrace(2)
+    assert_equal backtrace, caller
+  end
+end
diff --git a/vm_backtrace.c b/vm_backtrace.c
index 04b696c..ba99527 100644
--- a/vm_backtrace.c
+++ b/vm_backtrace.c
@@ -996,6 +996,16 @@ rb_vm_thread_backtrace_locations(int argc, const VALUE *argv, VALUE thval) https://github.com/ruby/ruby/blob/trunk/vm_backtrace.c#L996
     return thread_backtrace_to_ary(argc, argv, thval, 0);
 }
 
+VALUE rb_vm_backtrace(int argc, const VALUE * argv, struct rb_execution_context_struct * ec)
+{
+    return ec_backtrace_to_ary(ec, argc, argv, 0, 0, 1);
+}
+
+VALUE rb_vm_backtrace_locations(int argc, const VALUE * argv, struct rb_execution_context_struct * ec)
+{
+    return ec_backtrace_to_ary(ec, argc, argv, 0, 0, 0);
+}
+
 /*
  *  call-seq:
  *     caller(start=1, length=nil)  -> array or nil
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]