2004-11-17 09:48:35 -05:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class TestSetTraceFunc < Test::Unit::TestCase
|
2007-11-12 10:38:10 -05:00
|
|
|
def setup
|
2008-06-29 13:26:16 -04:00
|
|
|
@original_compile_option = RubyVM::InstructionSequence.compile_option
|
|
|
|
RubyVM::InstructionSequence.compile_option = {
|
2007-11-12 10:38:10 -05:00
|
|
|
:trace_instruction => true,
|
|
|
|
:specialized_instruction => false
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2008-06-30 09:58:35 -04:00
|
|
|
set_trace_func(nil)
|
2008-06-29 13:26:16 -04:00
|
|
|
RubyVM::InstructionSequence.compile_option = @original_compile_option
|
2007-11-12 10:38:10 -05:00
|
|
|
end
|
2005-04-07 04:38:05 -04:00
|
|
|
|
2007-11-12 10:38:10 -05:00
|
|
|
def test_c_call
|
2005-04-07 04:38:05 -04:00
|
|
|
events = []
|
2007-11-12 10:38:10 -05:00
|
|
|
eval <<-EOF.gsub(/^.*?: /, "")
|
|
|
|
1: set_trace_func(Proc.new { |event, file, lineno, mid, binding, klass|
|
|
|
|
2: events << [event, lineno, mid, klass]
|
|
|
|
3: })
|
|
|
|
4: x = 1 + 1
|
|
|
|
5: set_trace_func(nil)
|
|
|
|
EOF
|
|
|
|
assert_equal(["c-return", 3, :set_trace_func, Kernel],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 4, __method__, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-call", 4, :+, Fixnum],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-return", 4, :+, Fixnum],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 5, __method__, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-call", 5, :set_trace_func, Kernel],
|
|
|
|
events.shift)
|
|
|
|
assert_equal([], events)
|
2005-04-07 04:38:05 -04:00
|
|
|
end
|
2004-11-17 09:48:35 -05:00
|
|
|
|
2007-11-12 10:38:10 -05:00
|
|
|
def test_call
|
2004-11-17 09:48:35 -05:00
|
|
|
events = []
|
2007-11-12 10:38:10 -05:00
|
|
|
eval <<-EOF.gsub(/^.*?: /, "")
|
|
|
|
1: set_trace_func(Proc.new { |event, file, lineno, mid, binding, klass|
|
|
|
|
2: events << [event, lineno, mid, klass]
|
|
|
|
3: })
|
|
|
|
4: def add(x, y)
|
|
|
|
5: x + y
|
|
|
|
6: end
|
|
|
|
7: x = add(1, 1)
|
|
|
|
8: set_trace_func(nil)
|
|
|
|
EOF
|
|
|
|
assert_equal(["c-return", 3, :set_trace_func, Kernel],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 4, __method__, self.class],
|
|
|
|
events.shift)
|
2011-02-11 18:03:21 -05:00
|
|
|
assert_equal(["c-call", 4, :method_added, self.class],
|
2007-11-12 10:38:10 -05:00
|
|
|
events.shift)
|
2011-02-11 18:03:21 -05:00
|
|
|
assert_equal(["c-return", 4, :method_added, self.class],
|
2007-11-12 10:38:10 -05:00
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 7, __method__, self.class],
|
|
|
|
events.shift)
|
2008-09-30 11:31:06 -04:00
|
|
|
assert_equal(["call", 4, :add, self.class],
|
2007-11-12 10:38:10 -05:00
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 5, :add, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-call", 5, :+, Fixnum],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-return", 5, :+, Fixnum],
|
|
|
|
events.shift)
|
2009-07-22 09:37:26 -04:00
|
|
|
assert_equal(["return", 6, :add, self.class],
|
2007-11-12 10:38:10 -05:00
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 8, __method__, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-call", 8, :set_trace_func, Kernel],
|
|
|
|
events.shift)
|
|
|
|
assert_equal([], events)
|
|
|
|
end
|
2004-11-17 09:48:35 -05:00
|
|
|
|
2007-11-12 10:38:10 -05:00
|
|
|
def test_class
|
|
|
|
events = []
|
|
|
|
eval <<-EOF.gsub(/^.*?: /, "")
|
|
|
|
1: set_trace_func(Proc.new { |event, file, lineno, mid, binding, klass|
|
|
|
|
2: events << [event, lineno, mid, klass]
|
|
|
|
3: })
|
|
|
|
4: class Foo
|
|
|
|
5: def bar
|
|
|
|
6: end
|
|
|
|
7: end
|
|
|
|
8: x = Foo.new.bar
|
|
|
|
9: set_trace_func(nil)
|
|
|
|
EOF
|
|
|
|
assert_equal(["c-return", 3, :set_trace_func, Kernel],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 4, __method__, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-call", 4, :inherited, Class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-return", 4, :inherited, Class],
|
|
|
|
events.shift)
|
2009-07-22 09:37:26 -04:00
|
|
|
assert_equal(["class", 4, nil, nil],
|
2007-11-12 10:38:10 -05:00
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 5, nil, nil],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-call", 5, :method_added, Module],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-return", 5, :method_added, Module],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["end", 7, nil, nil],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 8, __method__, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-call", 8, :new, Class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-call", 8, :initialize, BasicObject],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-return", 8, :initialize, BasicObject],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-return", 8, :new, Class],
|
|
|
|
events.shift)
|
2008-09-30 11:31:06 -04:00
|
|
|
assert_equal(["call", 5, :bar, Foo],
|
2007-11-12 10:38:10 -05:00
|
|
|
events.shift)
|
2009-07-22 09:37:26 -04:00
|
|
|
assert_equal(["return", 6, :bar, Foo],
|
2007-11-12 10:38:10 -05:00
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 9, __method__, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-call", 9, :set_trace_func, Kernel],
|
|
|
|
events.shift)
|
2005-03-22 10:43:27 -05:00
|
|
|
assert_equal([], events)
|
2007-11-12 10:38:10 -05:00
|
|
|
end
|
2005-04-07 04:38:05 -04:00
|
|
|
|
2009-08-18 13:55:26 -04:00
|
|
|
def test_return # [ruby-dev:38701]
|
|
|
|
events = []
|
|
|
|
eval <<-EOF.gsub(/^.*?: /, "")
|
|
|
|
1: set_trace_func(Proc.new { |event, file, lineno, mid, binding, klass|
|
|
|
|
2: events << [event, lineno, mid, klass]
|
|
|
|
3: })
|
|
|
|
4: def foo(a)
|
|
|
|
5: return if a
|
|
|
|
6: return
|
|
|
|
7: end
|
|
|
|
8: foo(true)
|
|
|
|
9: foo(false)
|
|
|
|
10: set_trace_func(nil)
|
|
|
|
EOF
|
|
|
|
assert_equal(["c-return", 3, :set_trace_func, Kernel],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 4, __method__, self.class],
|
|
|
|
events.shift)
|
2011-02-11 18:03:21 -05:00
|
|
|
assert_equal(["c-call", 4, :method_added, self.class],
|
2009-08-18 13:55:26 -04:00
|
|
|
events.shift)
|
2011-02-11 18:03:21 -05:00
|
|
|
assert_equal(["c-return", 4, :method_added, self.class],
|
2009-08-18 13:55:26 -04:00
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 8, __method__, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["call", 4, :foo, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 5, :foo, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["return", 5, :foo, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 9, :test_return, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["call", 4, :foo, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 5, :foo, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["return", 7, :foo, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 10, :test_return, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-call", 10, :set_trace_func, Kernel],
|
|
|
|
events.shift)
|
|
|
|
assert_equal([], events)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_return2 # [ruby-core:24463]
|
|
|
|
events = []
|
|
|
|
eval <<-EOF.gsub(/^.*?: /, "")
|
|
|
|
1: set_trace_func(Proc.new { |event, file, lineno, mid, binding, klass|
|
|
|
|
2: events << [event, lineno, mid, klass]
|
|
|
|
3: })
|
|
|
|
4: def foo
|
|
|
|
5: a = 5
|
|
|
|
6: return a
|
|
|
|
7: end
|
|
|
|
8: foo
|
|
|
|
9: set_trace_func(nil)
|
|
|
|
EOF
|
|
|
|
assert_equal(["c-return", 3, :set_trace_func, Kernel],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 4, __method__, self.class],
|
|
|
|
events.shift)
|
2011-02-11 18:03:21 -05:00
|
|
|
assert_equal(["c-call", 4, :method_added, self.class],
|
2009-08-18 13:55:26 -04:00
|
|
|
events.shift)
|
2011-02-11 18:03:21 -05:00
|
|
|
assert_equal(["c-return", 4, :method_added, self.class],
|
2009-08-18 13:55:26 -04:00
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 8, __method__, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["call", 4, :foo, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 5, :foo, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 6, :foo, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["return", 7, :foo, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 9, :test_return2, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-call", 9, :set_trace_func, Kernel],
|
|
|
|
events.shift)
|
|
|
|
assert_equal([], events)
|
|
|
|
end
|
|
|
|
|
2007-11-12 10:38:10 -05:00
|
|
|
def test_raise
|
|
|
|
events = []
|
|
|
|
eval <<-EOF.gsub(/^.*?: /, "")
|
|
|
|
1: set_trace_func(Proc.new { |event, file, lineno, mid, binding, klass|
|
|
|
|
2: events << [event, lineno, mid, klass]
|
|
|
|
3: })
|
2008-06-30 09:58:35 -04:00
|
|
|
4: begin
|
|
|
|
5: raise TypeError, "error"
|
|
|
|
6: rescue TypeError
|
|
|
|
7: end
|
|
|
|
8: set_trace_func(nil)
|
2007-11-12 10:38:10 -05:00
|
|
|
EOF
|
|
|
|
assert_equal(["c-return", 3, :set_trace_func, Kernel],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["line", 4, __method__, self.class],
|
|
|
|
events.shift)
|
2008-07-01 14:43:25 -04:00
|
|
|
assert_equal(["line", 5, __method__, self.class],
|
|
|
|
events.shift)
|
|
|
|
assert_equal(["c-call", 5, :raise, Kernel],
|
2007-11-12 10:38:10 -05:00
|
|
|
events.shift)
|
2008-07-01 14:43:25 -04:00
|
|
|
assert_equal(["c-call", 5, :exception, Exception],
|
2007-11-12 10:38:10 -05:00
|
|
|
events.shift)
|
2008-07-01 14:43:25 -04:00
|
|
|
assert_equal(["c-call", 5, :initialize, Exception],
|
2007-11-12 10:38:10 -05:00
|
|
|
events.shift)
|
2008-07-01 14:43:25 -04:00
|
|
|
assert_equal(["c-return", 5, :initialize, Exception],
|
2007-11-12 10:38:10 -05:00
|
|
|
events.shift)
|
2008-07-01 14:43:25 -04:00
|
|
|
assert_equal(["c-return", 5, :exception, Exception],
|
2007-11-12 10:38:10 -05:00
|
|
|
events.shift)
|
2008-07-01 14:43:25 -04:00
|
|
|
assert_equal(["c-call", 5, :backtrace, Exception],
|
2007-11-12 10:38:10 -05:00
|
|
|
events.shift)
|
2008-07-01 14:43:25 -04:00
|
|
|
assert_equal(["c-return", 5, :backtrace, Exception],
|
2007-11-12 10:38:10 -05:00
|
|
|
events.shift)
|
2008-07-01 14:43:25 -04:00
|
|
|
assert_equal(["c-call", 5, :set_backtrace, Exception],
|
2007-11-12 10:38:10 -05:00
|
|
|
events.shift)
|
2008-07-01 14:43:25 -04:00
|
|
|
assert_equal(["c-return", 5, :set_backtrace, Exception],
|
2007-11-12 10:38:10 -05:00
|
|
|
events.shift)
|
2008-07-01 14:43:25 -04:00
|
|
|
assert_equal(["raise", 5, :test_raise, TestSetTraceFunc],
|
2007-11-12 10:38:10 -05:00
|
|
|
events.shift)
|
2009-08-17 22:44:10 -04:00
|
|
|
assert_equal(["c-return", 5, :raise, Kernel],
|
|
|
|
events.shift)
|
2008-07-01 14:43:25 -04:00
|
|
|
assert_equal(["c-call", 6, :===, Module],
|
2007-11-12 10:38:10 -05:00
|
|
|
events.shift)
|
2008-07-01 14:43:25 -04:00
|
|
|
assert_equal(["c-return", 6, :===, Module],
|
2007-11-12 10:38:10 -05:00
|
|
|
events.shift)
|
2008-07-01 14:43:25 -04:00
|
|
|
assert_equal(["line", 8, __method__, self.class],
|
2007-11-12 10:38:10 -05:00
|
|
|
events.shift)
|
2008-07-01 14:43:25 -04:00
|
|
|
assert_equal(["c-call", 8, :set_trace_func, Kernel],
|
2007-11-12 10:38:10 -05:00
|
|
|
events.shift)
|
2005-04-07 04:38:05 -04:00
|
|
|
assert_equal([], events)
|
2004-11-17 09:48:35 -05:00
|
|
|
end
|
2008-04-23 11:22:13 -04:00
|
|
|
|
2010-01-24 08:52:32 -05:00
|
|
|
def test_break # [ruby-core:27606] [Bug #2610]
|
|
|
|
events = []
|
|
|
|
eval <<-EOF.gsub(/^.*?: /, "")
|
|
|
|
1: set_trace_func(Proc.new { |event, file, lineno, mid, binding, klass|
|
|
|
|
2: events << [event, lineno, mid, klass]
|
|
|
|
3: })
|
|
|
|
4: [1,2,3].any? {|n| n}
|
|
|
|
8: set_trace_func(nil)
|
|
|
|
EOF
|
|
|
|
|
|
|
|
[["c-return", 3, :set_trace_func, Kernel],
|
|
|
|
["line", 4, __method__, self.class],
|
|
|
|
["c-call", 4, :any?, Enumerable],
|
|
|
|
["c-call", 4, :each, Array],
|
|
|
|
["line", 4, __method__, self.class],
|
|
|
|
["c-return", 4, :each, Array],
|
|
|
|
["c-return", 4, :any?, Enumerable],
|
|
|
|
["line", 5, __method__, self.class],
|
|
|
|
["c-call", 5, :set_trace_func, Kernel]].each{|e|
|
|
|
|
assert_equal(e, events.shift)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2008-04-23 11:22:13 -04:00
|
|
|
def test_invalid_proc
|
2010-01-24 08:52:32 -05:00
|
|
|
assert_raise(TypeError) { set_trace_func(1) }
|
2008-04-23 11:22:13 -04:00
|
|
|
end
|
2009-07-05 01:04:41 -04:00
|
|
|
|
|
|
|
def test_raise_in_trace
|
|
|
|
set_trace_func proc {raise rescue nil}
|
|
|
|
assert_equal(42, (raise rescue 42), '[ruby-core:24118]')
|
|
|
|
end
|
2010-03-26 10:55:00 -04:00
|
|
|
|
|
|
|
def test_thread_trace
|
|
|
|
events = {:set => [], :add => []}
|
|
|
|
prc = Proc.new { |event, file, lineno, mid, binding, klass|
|
|
|
|
events[:set] << [event, lineno, mid, klass, :set]
|
|
|
|
}
|
|
|
|
prc2 = Proc.new { |event, file, lineno, mid, binding, klass|
|
|
|
|
events[:add] << [event, lineno, mid, klass, :add]
|
|
|
|
}
|
|
|
|
|
|
|
|
th = Thread.new do
|
|
|
|
th = Thread.current
|
|
|
|
eval <<-EOF.gsub(/^.*?: /, "")
|
|
|
|
1: th.set_trace_func(prc)
|
|
|
|
2: th.add_trace_func(prc2)
|
|
|
|
3: class ThreadTraceInnerClass
|
|
|
|
4: def foo
|
|
|
|
5: x = 1 + 1
|
|
|
|
6: end
|
|
|
|
7: end
|
|
|
|
8: ThreadTraceInnerClass.new.foo
|
|
|
|
9: th.set_trace_func(nil)
|
|
|
|
EOF
|
|
|
|
end
|
|
|
|
th.join
|
|
|
|
|
|
|
|
[["c-return", 1, :set_trace_func, Thread, :set],
|
|
|
|
["line", 2, __method__, self.class, :set],
|
|
|
|
["c-call", 2, :add_trace_func, Thread, :set]].each do |e|
|
|
|
|
assert_equal(e, events[:set].shift)
|
|
|
|
end
|
|
|
|
|
|
|
|
[["c-return", 2, :add_trace_func, Thread],
|
|
|
|
["line", 3, __method__, self.class],
|
|
|
|
["c-call", 3, :inherited, Class],
|
|
|
|
["c-return", 3, :inherited, Class],
|
|
|
|
["class", 3, nil, nil],
|
|
|
|
["line", 4, nil, nil],
|
|
|
|
["c-call", 4, :method_added, Module],
|
|
|
|
["c-return", 4, :method_added, Module],
|
|
|
|
["end", 7, nil, nil],
|
|
|
|
["line", 8, __method__, self.class],
|
|
|
|
["c-call", 8, :new, Class],
|
|
|
|
["c-call", 8, :initialize, BasicObject],
|
|
|
|
["c-return", 8, :initialize, BasicObject],
|
|
|
|
["c-return", 8, :new, Class],
|
|
|
|
["call", 4, :foo, ThreadTraceInnerClass],
|
|
|
|
["line", 5, :foo, ThreadTraceInnerClass],
|
|
|
|
["c-call", 5, :+, Fixnum],
|
|
|
|
["c-return", 5, :+, Fixnum],
|
|
|
|
["return", 6, :foo, ThreadTraceInnerClass],
|
|
|
|
["line", 9, __method__, self.class],
|
|
|
|
["c-call", 9, :set_trace_func, Thread]].each do |e|
|
|
|
|
[:set, :add].each do |type|
|
|
|
|
assert_equal(e + [type], events[type].shift)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_equal([], events[:set])
|
|
|
|
assert_equal([], events[:add])
|
|
|
|
end
|
2010-10-08 22:00:21 -04:00
|
|
|
|
2011-06-30 09:34:53 -04:00
|
|
|
def test_trace_defined_method
|
|
|
|
events = []
|
|
|
|
eval <<-EOF.gsub(/^.*?: /, "")
|
|
|
|
1: class FooBar; define_method(:foobar){}; end
|
|
|
|
2: fb = FooBar.new
|
|
|
|
3: set_trace_func(Proc.new { |event, file, lineno, mid, binding, klass|
|
|
|
|
4: events << [event, lineno, mid, klass]
|
|
|
|
5: })
|
|
|
|
6: fb.foobar
|
|
|
|
7: set_trace_func(nil)
|
|
|
|
EOF
|
|
|
|
|
|
|
|
[["c-return", 5, :set_trace_func, Kernel],
|
|
|
|
["line", 6, __method__, self.class],
|
|
|
|
["call", 6, :foobar, FooBar],
|
|
|
|
["return", 6, :foobar, FooBar],
|
|
|
|
["line", 7, __method__, self.class],
|
|
|
|
["c-call", 7, :set_trace_func, Kernel]].each{|e|
|
|
|
|
assert_equal(e, events.shift)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-10-08 22:00:21 -04:00
|
|
|
def test_remove_in_trace
|
|
|
|
bug3921 = '[ruby-dev:42350]'
|
|
|
|
ok = false
|
|
|
|
func = lambda{|e, f, l, i, b, k|
|
|
|
|
set_trace_func(nil)
|
|
|
|
ok = eval("self", b)
|
|
|
|
}
|
|
|
|
|
|
|
|
set_trace_func(func)
|
|
|
|
assert_equal(self, ok, bug3921)
|
|
|
|
end
|
2011-02-11 18:03:21 -05:00
|
|
|
|
|
|
|
class << self
|
|
|
|
define_method(:method_added, Module.method(:method_added))
|
|
|
|
end
|
2004-11-17 09:48:35 -05:00
|
|
|
end
|