mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
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] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66640 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
fd7d4a871c
commit
94f4a0e91e
3 changed files with 54 additions and 4 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
16
vm_trace.c
16
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);
|
||||
|
||||
|
|
Loading…
Reference in a new issue