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

ruby-changes:54425

From: ko1 <ko1@a...>
Date: Sun, 30 Dec 2018 01:44:14 +0900 (JST)
Subject: [ruby-changes:54425] ko1:r66640 (trunk): `TracePoint#enable(target_thraed:)` [Feature #15473]

ko1	2018-12-30 01:44:09 +0900 (Sun, 30 Dec 2018)

  New Revision: 66640

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=66640

  Log:
    `TracePoint#enable(target_thraed:)` [Feature #15473]
    
    * vm_trace.c (tracepoint_enable_m): `TracePoint#enable` supports
      `target_thread:` keyword to filter a target thread.
      [Feature #15473]

  Modified files:
    trunk/prelude.rb
    trunk/test/ruby/test_settracefunc.rb
    trunk/vm_trace.c
Index: vm_trace.c
===================================================================
--- vm_trace.c	(revision 66639)
+++ vm_trace.c	(revision 66640)
@@ -1327,6 +1327,7 @@ rb_tracepoint_disable(VALUE tpval) https://github.com/ruby/ruby/blob/trunk/vm_trace.c#L1327
         }
     }
     tp->tracing = 0;
+    tp->target_th = NULL;
     return Qundef;
 }
 
@@ -1398,11 +1399,22 @@ rb_hook_list_remove_tracepoint(rb_hook_l https://github.com/ruby/ruby/blob/trunk/vm_trace.c#L1399
  *
  */
 static VALUE
-tracepoint_enable_m(VALUE tpval, VALUE target, VALUE target_line)
+tracepoint_enable_m(VALUE tpval, VALUE target, VALUE target_line, VALUE target_thread)
 {
     rb_tp_t *tp = tpptr(tpval);
     int previous_tracing = tp->tracing;
 
+    /* check target_thread */
+    if (RTEST(target_thread)) {
+        if (tp->target_th) {
+            rb_raise(rb_eArgError, "can not override target_thread filter");
+        }
+        tp->target_th = rb_thread_ptr(target_thread);
+    }
+    else {
+        tp->target_th = NULL;
+    }
+
     if (NIL_P(target)) {
         if (!NIL_P(target_line)) {
             rb_raise(rb_eArgError, "only target_line is specified");
@@ -1801,7 +1813,7 @@ Init_vm_trace(void) https://github.com/ruby/ruby/blob/trunk/vm_trace.c#L1813
      */
     rb_define_singleton_method(rb_cTracePoint, "trace", tracepoint_trace_s, -1);
 
-    rb_define_method(rb_cTracePoint, "__enable", tracepoint_enable_m, 2);
+    rb_define_method(rb_cTracePoint, "__enable", tracepoint_enable_m, 3);
     rb_define_method(rb_cTracePoint, "disable", tracepoint_disable_m, 0);
     rb_define_method(rb_cTracePoint, "enabled?", rb_tracepoint_enabled_p, 0);
 
Index: test/ruby/test_settracefunc.rb
===================================================================
--- test/ruby/test_settracefunc.rb	(revision 66639)
+++ test/ruby/test_settracefunc.rb	(revision 66640)
@@ -2115,4 +2115,42 @@ class TestSetTraceFunc < Test::Unit::Tes https://github.com/ruby/ruby/blob/trunk/test/ruby/test_settracefunc.rb#L2115
     }
     assert_equal [], events
   end
+
+  def test_enable_target_thread
+    events = []
+    TracePoint.new(:line) do |tp|
+      events << Thread.current
+    end.enable(target_thread: Thread.current) do
+      a = 1
+      Thread.new{
+        b = 2
+        c = 3
+      }.join
+      d = 4
+    end
+    assert_equal Array.new(3){Thread.current}, events
+
+    events = []
+    tp = TracePoint.new(:line) do |tp|
+      events << Thread.current
+    end
+
+    q1 = Queue.new
+    q2 = Queue.new
+
+    th = Thread.new{
+      q1 << :ok; q2.pop
+      t1 = 1
+      t2 = 2
+    }
+    q1.pop
+    tp.enable(target_thread: th) do
+      q2 << 1
+      a = 1
+      b = 2
+      th.join
+    end
+
+    assert_equal Array.new(2){th}, events
+  end
 end
Index: prelude.rb
===================================================================
--- prelude.rb	(revision 66639)
+++ prelude.rb	(revision 66640)
@@ -133,8 +133,8 @@ class IO https://github.com/ruby/ruby/blob/trunk/prelude.rb#L133
 end
 
 class TracePoint
-  def enable target: nil, target_line: nil, &blk
-    self.__enable target, target_line, &blk
+  def enable target: nil, target_line: nil, target_thread: nil, &blk
+    self.__enable target, target_line, target_thread, &blk
   end
 end
 

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

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