summaryrefslogtreecommitdiff
path: root/cont.c
diff options
context:
space:
mode:
authorzverok <zverok.offline@gmail.com>2020-12-21 23:42:01 +0200
committerMarc-André Lafortune <github@marc-andre.ca>2020-12-21 19:22:38 -0500
commit843fd1e8cfccdf1efcbb157315e267e4f380e030 (patch)
tree9453e295b19bcb0dca5b7de6f2346d5589fcfb6d /cont.c
parent816bbfdc87d4a5f600f28cf4b0eaa5161af80645 (diff)
Document Fiber#backtrace and #backtrace_locations
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/3966
Diffstat (limited to 'cont.c')
-rw-r--r--cont.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/cont.c b/cont.c
index bdc29e14c1..129aad956e 100644
--- a/cont.c
+++ b/cont.c
@@ -2369,12 +2369,77 @@ rb_fiber_raise(int argc, VALUE *argv, VALUE fiber_value)
}
}
+/*
+ * call-seq:
+ * fiber.backtrace -> array
+ * fiber.backtrace(start) -> array
+ * fiber.backtrace(start, count) -> array
+ * fiber.backtrace(start..end) -> array
+ *
+ * Returns the current execution stack of the fiber. +start+, +count+ and +end+ allow
+ * to select only parts of the backtrace.
+ *
+ * def level3
+ * Fiber.yield
+ * end
+ *
+ * def level2
+ * level3
+ * end
+ *
+ * def level1
+ * level2
+ * end
+ *
+ * f = Fiber.new { level1 }
+ *
+ * # It is empty before the fiber started
+ * f.backtrace
+ * #=> []
+ *
+ * f.resume
+ *
+ * f.backtrace
+ * #=> ["test.rb:2:in `yield'", "test.rb:2:in `level3'", "test.rb:6:in `level2'", "test.rb:10:in `level1'", "test.rb:13:in `block in <main>'"]
+ * p f.backtrace(1) # start from the item 1
+ * #=> ["test.rb:2:in `level3'", "test.rb:6:in `level2'", "test.rb:10:in `level1'", "test.rb:13:in `block in <main>'"]
+ * p f.backtrace(2, 2) # start from item 2, take 2
+ * #=> ["test.rb:6:in `level2'", "test.rb:10:in `level1'"]
+ * p f.backtrace(1..3) # take items from 1 to 3
+ * #=> ["test.rb:2:in `level3'", "test.rb:6:in `level2'", "test.rb:10:in `level1'"]
+ *
+ * f.resume
+ *
+ * # It is empty after the fiber is finished
+ * f.backtrace
+ * #=> []
+ *
+ */
static VALUE
rb_fiber_backtrace(int argc, VALUE *argv, VALUE fiber)
{
return rb_vm_backtrace(argc, argv, &fiber_ptr(fiber)->cont.saved_ec);
}
+/*
+ * call-seq:
+ * fiber.backtrace_locations -> array
+ * fiber.backtrace_locations(start) -> array
+ * fiber.backtrace_locations(start, count) -> array
+ * fiber.backtrace_locations(start..end) -> array
+ *
+ * Like #backtrace, but returns each line of the execution stack as a
+ * Thread::Backtrace::Location. Accepts the same arguments as #backtrace.
+ *
+ * f = Fiber.new { Fiber.yield }
+ * f.resume
+ * loc = f.backtrace_locations.first
+ * loc.label #=> "yield"
+ * loc.path #=> "test.rb"
+ * loc.lineno #=> 1
+ *
+ *
+ */
static VALUE
rb_fiber_backtrace_locations(int argc, VALUE *argv, VALUE fiber)
{