Make sure TracePoint#binding returns nil for c_call/c_return events

This makes sure the method returns nil for these events, as
described in NEWS, even if the TracePoint could create a binding.
This commit is contained in:
Jeremy Evans 2022-12-21 21:02:43 -08:00 коммит произвёл GitHub
Родитель ae455a129e
Коммит 4213f42555
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 50 добавлений и 25 удалений

Просмотреть файл

@ -50,6 +50,26 @@ class TestSetTraceFunc < Test::Unit::TestCase
assert_equal([], events)
end
def test_c_return_no_binding
binding = :none
TracePoint.new(:c_return){|tp|
binding = tp.binding
}.enable{
1.object_id
}
assert_nil(binding)
end
def test_c_call_no_binding
binding = :none
TracePoint.new(:c_call){|tp|
binding = tp.binding
}.enable{
1.object_id
}
assert_nil(binding)
end
def test_call
events = []
name = "#{self.class}\##{__method__}"
@ -459,7 +479,7 @@ class TestSetTraceFunc < Test::Unit::TestCase
begin
eval <<-EOF.gsub(/^.*?: /, ""), nil, 'xyzzy'
1: trace = TracePoint.trace(*trace_events){|tp| next if !target_thread?
2: events << [tp.event, tp.lineno, tp.path, _defined_class.(tp), tp.method_id, tp.self, tp.binding.eval("_local_var"), _get_data.(tp)] if tp.path == 'xyzzy'
2: events << [tp.event, tp.lineno, tp.path, _defined_class.(tp), tp.method_id, tp.self, tp.binding&.eval("_local_var"), _get_data.(tp)] if tp.path == 'xyzzy'
3: }
4: 1.times{|;_local_var| _local_var = :inner
5: tap{}
@ -488,29 +508,29 @@ class TestSetTraceFunc < Test::Unit::TestCase
answer_events = [
#
[:line, 4, 'xyzzy', self.class, method, self, :outer, :nothing],
[:c_call, 4, 'xyzzy', Integer, :times, 1, :outer, :nothing],
[:c_call, 4, 'xyzzy', Integer, :times, 1, nil, :nothing],
[:line, 4, 'xyzzy', self.class, method, self, nil, :nothing],
[:line, 5, 'xyzzy', self.class, method, self, :inner, :nothing],
[:c_return, 4, "xyzzy", Integer, :times, 1, :outer, 1],
[:c_return, 4, "xyzzy", Integer, :times, 1, nil, 1],
[:line, 7, 'xyzzy', self.class, method, self, :outer, :nothing],
[:c_call, 7, "xyzzy", Module, :const_added, TestSetTraceFunc, :outer, :nothing],
[:c_return, 7, "xyzzy", Module, :const_added, TestSetTraceFunc, :outer, nil],
[:c_call, 7, "xyzzy", Class, :inherited, Object, :outer, :nothing],
[:c_return, 7, "xyzzy", Class, :inherited, Object, :outer, nil],
[:c_call, 7, "xyzzy", Module, :const_added, TestSetTraceFunc, nil, :nothing],
[:c_return, 7, "xyzzy", Module, :const_added, TestSetTraceFunc, nil, nil],
[:c_call, 7, "xyzzy", Class, :inherited, Object, nil, :nothing],
[:c_return, 7, "xyzzy", Class, :inherited, Object, nil, nil],
[:class, 7, "xyzzy", nil, nil, xyzzy.class, nil, :nothing],
[:line, 8, "xyzzy", nil, nil, xyzzy.class, nil, :nothing],
[:line, 9, "xyzzy", nil, nil, xyzzy.class, :XYZZY_outer, :nothing],
[:c_call, 9, "xyzzy", Module, :method_added, xyzzy.class, :XYZZY_outer, :nothing],
[:c_return, 9, "xyzzy", Module, :method_added, xyzzy.class, :XYZZY_outer, nil],
[:c_call, 9, "xyzzy", Module, :method_added, xyzzy.class, nil, :nothing],
[:c_return, 9, "xyzzy", Module, :method_added, xyzzy.class, nil, nil],
[:line, 13, "xyzzy", nil, nil, xyzzy.class, :XYZZY_outer, :nothing],
[:c_call, 13, "xyzzy", Module, :method_added, xyzzy.class, :XYZZY_outer, :nothing],
[:c_return,13, "xyzzy", Module, :method_added, xyzzy.class, :XYZZY_outer, nil],
[:c_call, 13, "xyzzy", Module, :method_added, xyzzy.class, nil, :nothing],
[:c_return,13, "xyzzy", Module, :method_added, xyzzy.class, nil, nil],
[:end, 17, "xyzzy", nil, nil, xyzzy.class, :XYZZY_outer, :nothing],
[:line, 18, "xyzzy", TestSetTraceFunc, method, self, :outer, :nothing],
[:c_call, 18, "xyzzy", Class, :new, xyzzy.class, :outer, :nothing],
[:c_call, 18, "xyzzy", BasicObject, :initialize, xyzzy, :outer, :nothing],
[:c_return,18, "xyzzy", BasicObject, :initialize, xyzzy, :outer, nil],
[:c_return,18, "xyzzy", Class, :new, xyzzy.class, :outer, xyzzy],
[:c_call, 18, "xyzzy", Class, :new, xyzzy.class, nil, :nothing],
[:c_call, 18, "xyzzy", BasicObject, :initialize, xyzzy, nil, :nothing],
[:c_return,18, "xyzzy", BasicObject, :initialize, xyzzy, nil, nil],
[:c_return,18, "xyzzy", Class, :new, xyzzy.class, nil, xyzzy],
[:line, 19, "xyzzy", TestSetTraceFunc, method, self, :outer, :nothing],
[:call, 9, "xyzzy", xyzzy.class, :foo, xyzzy, nil, :nothing],
[:line, 10, "xyzzy", xyzzy.class, :foo, xyzzy, nil, :nothing],
@ -521,17 +541,17 @@ class TestSetTraceFunc < Test::Unit::TestCase
[:return, 16, "xyzzy", xyzzy.class, :bar, xyzzy, :XYZZY_bar, xyzzy],
[:return, 12, "xyzzy", xyzzy.class, :foo, xyzzy, :XYZZY_foo, xyzzy],
[:line, 20, "xyzzy", TestSetTraceFunc, method, self, :outer, :nothing],
[:c_call, 20, "xyzzy", Kernel, :raise, self, :outer, :nothing],
[:c_call, 20, "xyzzy", Exception, :exception, RuntimeError, :outer, :nothing],
[:c_call, 20, "xyzzy", Exception, :initialize, raised_exc, :outer, :nothing],
[:c_return,20, "xyzzy", Exception, :initialize, raised_exc, :outer, raised_exc],
[:c_return,20, "xyzzy", Exception, :exception, RuntimeError, :outer, raised_exc],
[:c_return,20, "xyzzy", Kernel, :raise, self, :outer, nil],
[:c_call, 20, "xyzzy", Exception, :backtrace, raised_exc, :outer, :nothing],
[:c_return,20, "xyzzy", Exception, :backtrace, raised_exc, :outer, nil],
[:c_call, 20, "xyzzy", Kernel, :raise, self, nil, :nothing],
[:c_call, 20, "xyzzy", Exception, :exception, RuntimeError, nil, :nothing],
[:c_call, 20, "xyzzy", Exception, :initialize, raised_exc, nil, :nothing],
[:c_return,20, "xyzzy", Exception, :initialize, raised_exc, nil, raised_exc],
[:c_return,20, "xyzzy", Exception, :exception, RuntimeError, nil, raised_exc],
[:c_return,20, "xyzzy", Kernel, :raise, self, nil, nil],
[:c_call, 20, "xyzzy", Exception, :backtrace, raised_exc, nil, :nothing],
[:c_return,20, "xyzzy", Exception, :backtrace, raised_exc, nil, nil],
[:raise, 20, "xyzzy", TestSetTraceFunc, :trace_by_tracepoint, self, :outer, raised_exc],
[:c_call, 20, "xyzzy", Module, :===, RuntimeError,:outer, :nothing],
[:c_return,20, "xyzzy", Module, :===, RuntimeError,:outer, true],
[:c_call, 20, "xyzzy", Module, :===, RuntimeError, nil, :nothing],
[:c_return,20, "xyzzy", Module, :===, RuntimeError, nil, true],
[:line, 21, "xyzzy", TestSetTraceFunc, method, self, :outer, :nothing],
]

Просмотреть файл

@ -961,6 +961,11 @@ VALUE
rb_tracearg_binding(rb_trace_arg_t *trace_arg)
{
rb_control_frame_t *cfp;
switch (trace_arg->event) {
case RUBY_EVENT_C_CALL:
case RUBY_EVENT_C_RETURN:
return Qnil;
}
cfp = rb_vm_get_binding_creatable_next_cfp(trace_arg->ec, trace_arg->cfp);
if (cfp && imemo_type_p((VALUE)cfp->iseq, imemo_iseq)) {