diff options
-rw-r--r-- | spec/ruby/core/tracepoint/enable_spec.rb | 55 | ||||
-rw-r--r-- | spec/ruby/core/tracepoint/inspect_spec.rb | 4 | ||||
-rw-r--r-- | test/ruby/test_settracefunc.rb | 37 | ||||
-rw-r--r-- | trace_point.rb | 9 |
4 files changed, 68 insertions, 37 deletions
diff --git a/spec/ruby/core/tracepoint/enable_spec.rb b/spec/ruby/core/tracepoint/enable_spec.rb index ab392c7583..4d9cfa1448 100644 --- a/spec/ruby/core/tracepoint/enable_spec.rb +++ b/spec/ruby/core/tracepoint/enable_spec.rb @@ -57,25 +57,50 @@ describe 'TracePoint#enable' do end.enable { event_name.should equal(:line) } end - it 'enables the trace object for any thread' do - threads = [] - trace = TracePoint.new(:line) do |tp| - # Runs on purpose on any Thread - threads << Thread.current - end + ruby_version_is '3.2' do + it 'enables the trace object for any thread' do + threads = [] + trace = TracePoint.new(:line) do |tp| + # Runs on purpose on any Thread + threads << Thread.current + end - thread = nil - trace.enable do - line_event = true - thread = Thread.new do - event_in_other_thread = true + thread = nil + trace.enable do + line_event = true + thread = Thread.new do + event_in_other_thread = true + end + thread.join end - thread.join + + threads = threads.uniq + threads.should.include?(Thread.current) + threads.should_not.include?(thread) end + end - threads = threads.uniq - threads.should.include?(Thread.current) - threads.should.include?(thread) + ruby_version_is ''...'3.2' do + it 'enables the trace object for any thread' do + threads = [] + trace = TracePoint.new(:line) do |tp| + # Runs on purpose on any Thread + threads << Thread.current + end + + thread = nil + trace.enable do + line_event = true + thread = Thread.new do + event_in_other_thread = true + end + thread.join + end + + threads = threads.uniq + threads.should.include?(Thread.current) + threads.should.include?(thread) + end end it 'can accept arguments within a block but it should not yield arguments' do diff --git a/spec/ruby/core/tracepoint/inspect_spec.rb b/spec/ruby/core/tracepoint/inspect_spec.rb index b9d7f35c32..a07b626212 100644 --- a/spec/ruby/core/tracepoint/inspect_spec.rb +++ b/spec/ruby/core/tracepoint/inspect_spec.rb @@ -98,7 +98,7 @@ describe 'TracePoint#inspect' do TracePoint.new(:thread_begin) { |tp| next unless Thread.current == thread inspect ||= tp.inspect - }.enable do + }.enable(target_thread: nil) do thread = Thread.new {} thread_inspection = thread.inspect thread.join @@ -114,7 +114,7 @@ describe 'TracePoint#inspect' do TracePoint.new(:thread_end) { |tp| next unless Thread.current == thread inspect ||= tp.inspect - }.enable do + }.enable(target_thread: nil) do thread = Thread.new {} thread_inspection = thread.inspect thread.join diff --git a/test/ruby/test_settracefunc.rb b/test/ruby/test_settracefunc.rb index 6d9652e3fd..1b595ef1d7 100644 --- a/test/ruby/test_settracefunc.rb +++ b/test/ruby/test_settracefunc.rb @@ -727,25 +727,30 @@ CODE def test_tracepoint_enable ary = [] args = nil - trace = TracePoint.new(:call){|tp| - next if !target_thread? - ary << tp.method_id - } - foo - trace.enable{|*a| - args = a + begin + trace = TracePoint.new(:call){|tp| + next if !target_thread? + ary << tp.method_id + } foo - } - foo - assert_equal([:foo], ary) - assert_equal([], args) + trace.enable(target_thread: nil){|*a| + args = a + foo + } + foo + assert_equal([:foo], ary) + assert_equal([], args) + ensure + trace&.disable + end trace = TracePoint.new{} begin assert_equal(false, trace.enable) assert_equal(true, trace.enable) - trace.enable{} - assert_equal(true, trace.enable) + trace.enable(target_thread: nil){} + trace.disable + assert_equal(false, trace.enable) ensure trace.disable end @@ -977,7 +982,7 @@ CODE tp.defined_class, #=> nil, tp.self.class # tp.self return creating/ending thread ] - }.enable{ + }.enable(target_thread: nil){ created_thread = Thread.new{thread_self = self} created_thread.join } @@ -2239,7 +2244,7 @@ CODE # global TP and targeted TP ex = assert_raise(ArgumentError) do tp = TracePoint.new(:line){} - tp.enable{ + tp.enable(target_thread: nil){ tp.enable(target: code2){} } end @@ -2285,7 +2290,7 @@ CODE events << :___ end end - assert_equal [:tp1, :tp1, :tp1, :tp1, :tp2, :tp1, :___], events + assert_equal [:tp1, :tp1, :tp1, :tp1, :tp1, :tp2, :tp1, :___], events # success with two tracepoints (targeting/global) events = [] diff --git a/trace_point.rb b/trace_point.rb index 85ebac9aa7..866c807821 100644 --- a/trace_point.rb +++ b/trace_point.rb @@ -153,7 +153,7 @@ class TracePoint # call-seq: # trace.enable(target: nil, target_line: nil, target_thread: nil) -> true or false - # trace.enable(target: nil, target_line: nil, target_thread: nil) { block } -> obj + # trace.enable(target: nil, target_line: nil, target_thread: Thread.current) { block } -> obj # # Activates the trace. # @@ -168,14 +168,15 @@ class TracePoint # # trace is still enabled # # If a block is given, the trace will only be enabled within the scope of the - # block. + # block. If target and target_line are both nil, then target_thread will default + # to the current thread if a block is given. # # trace.enabled? # #=> false # # trace.enable do # trace.enabled? - # # only enabled for this block + # # only enabled for this block and thread # end # # trace.enabled? @@ -208,7 +209,7 @@ class TracePoint # trace.enable { p tp.lineno } # #=> RuntimeError: access from outside # - def enable(target: nil, target_line: nil, target_thread: nil) + def enable(target: nil, target_line: nil, target_thread: (Thread.current if target.nil? && target_line.nil? && defined?(yield))) Primitive.tracepoint_enable_m(target, target_line, target_thread) end |