diff --git a/prelude.rb b/prelude.rb index 1d62c13219..17f3f5052a 100644 --- a/prelude.rb +++ b/prelude.rb @@ -133,8 +133,8 @@ class IO 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 diff --git a/test/ruby/test_settracefunc.rb b/test/ruby/test_settracefunc.rb index 2ada093f8d..6c07bf90f6 100644 --- a/test/ruby/test_settracefunc.rb +++ b/test/ruby/test_settracefunc.rb @@ -2115,4 +2115,42 @@ class TestSetTraceFunc < Test::Unit::TestCase } 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 diff --git a/vm_trace.c b/vm_trace.c index 82c5249abc..79b72876c3 100644 --- a/vm_trace.c +++ b/vm_trace.c @@ -1327,6 +1327,7 @@ rb_tracepoint_disable(VALUE tpval) } } tp->tracing = 0; + tp->target_th = NULL; return Qundef; } @@ -1398,11 +1399,22 @@ rb_hook_list_remove_tracepoint(rb_hook_list_t *list, VALUE tpval) * */ 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) */ 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);