Project

General

Profile

Feature #14694 ยป tracepoint-parameters.patch

mame (Yusuke Endoh), 04/18/2018 02:06 AM

View differences:

method.h
218 218

  
219 219
void rb_scope_visibility_set(rb_method_visibility_t);
220 220

  
221
VALUE rb_unnamed_parameters(int arity);
222

  
221 223
#endif /* RUBY_METHOD_H */
proc.c
1156 1156
    return iseq_location(rb_proc_get_iseq(self, 0));
1157 1157
}
1158 1158

  
1159
static VALUE
1160
unnamed_parameters(int arity)
1159
VALUE
1160
rb_unnamed_parameters(int arity)
1161 1161
{
1162 1162
    VALUE a, param = rb_ary_new2((arity < 0) ? -arity : arity);
1163 1163
    int n = (arity < 0) ? ~arity : arity;
......
1191 1191
    int is_proc;
1192 1192
    const rb_iseq_t *iseq = rb_proc_get_iseq(self, &is_proc);
1193 1193
    if (!iseq) {
1194
	return unnamed_parameters(rb_proc_arity(self));
1194
	return rb_unnamed_parameters(rb_proc_arity(self));
1195 1195
    }
1196 1196
    return rb_iseq_parameters(iseq, is_proc);
1197 1197
}
......
2575 2575
{
2576 2576
    const rb_iseq_t *iseq = rb_method_iseq(method);
2577 2577
    if (!iseq) {
2578
	return unnamed_parameters(method_arity(method));
2578
	return rb_unnamed_parameters(method_arity(method));
2579 2579
    }
2580 2580
    return rb_iseq_parameters(iseq, 0);
2581 2581
}
test/ruby/test_settracefunc.rb
703 703
    assert_equal(false, trace.enabled?)
704 704
  end
705 705

  
706
  def parameter_test(a, b, c)
707
    yield
708
  end
709

  
710
  def test_tracepoint_parameters
711
    trace = TracePoint.new(:line, :class, :end, :call, :return, :b_call, :b_return, :c_call, :c_return, :raise){|tp|
712
      next if !target_thread?
713
      next if tp.path != __FILE__
714
      case tp.event
715
      when :call, :return
716
        assert_equal([[:req, :a], [:req, :b], [:req, :c]], tp.parameters)
717
      when :b_call, :b_return
718
        next if tp.parameters == []
719
        assert_equal([[:req, :x], [:req, :y], [:req, :z]], tp.parameters)
720
      when :c_call, :c_return
721
        assert_equal([[:req]], tp.parameters) if tp.method_id == :getbyte
722
      when :line, :class, :end, :raise
723
        assert_raise(RuntimeError) { tp.parameters }
724
      end
725
    }
726
    obj = Object.new
727
    trace.enable{
728
      parameter_test(1, 2, 3) {|x, y, z|
729
      }
730
      "".getbyte(0)
731
      class << obj
732
      end
733
      begin
734
        raise
735
      rescue
736
      end
737
    }
738
  end
739

  
706 740
  def method_test_tracepoint_return_value obj
707 741
    obj
708 742
  end
vm_trace.c
804 804
    }
805 805
}
806 806

  
807
VALUE
808
rb_tracearg_parameters(rb_trace_arg_t *trace_arg)
809
{
810
    switch(trace_arg->event) {
811
      case RUBY_EVENT_CALL:
812
      case RUBY_EVENT_RETURN:
813
      case RUBY_EVENT_B_CALL:
814
      case RUBY_EVENT_B_RETURN: {
815
	const rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(trace_arg->ec, trace_arg->cfp);
816
	if (cfp) {
817
	    return rb_iseq_parameters(cfp->iseq, 0);
818
	}
819
	break;
820
      }
821
      case RUBY_EVENT_C_CALL:
822
      case RUBY_EVENT_C_RETURN: {
823
	fill_id_and_klass(trace_arg);
824
	if (trace_arg->klass && trace_arg->id) {
825
	    const rb_method_entry_t *me;
826
	    VALUE iclass = Qnil;
827
	    me = rb_method_entry_without_refinements(trace_arg->klass, trace_arg->id, &iclass);
828
	    return rb_unnamed_parameters(rb_method_entry_arity(me));
829
	}
830
	break;
831
      }
832
      case RUBY_EVENT_RAISE:
833
      case RUBY_EVENT_LINE:
834
      case RUBY_EVENT_CLASS:
835
      case RUBY_EVENT_END:
836
	rb_raise(rb_eRuntimeError, "not supported by this event");
837
	break;
838
    }
839
    return Qnil;
840
}
841

  
807 842
VALUE
808 843
rb_tracearg_method_id(rb_trace_arg_t *trace_arg)
809 844
{
......
919 954
    return rb_tracearg_path(get_trace_arg());
920 955
}
921 956

  
957
/*
958
 * Return the parameters of the method or block that the current hook belongs to
959
 */
960
static VALUE
961
tracepoint_attr_parameters(VALUE tpval)
962
{
963
    return rb_tracearg_parameters(get_trace_arg());
964
}
965

  
922 966
/*
923 967
 * Return the name at the definition of the method being called
924 968
 */
......
1502 1546
    rb_define_method(rb_cTracePoint, "event", tracepoint_attr_event, 0);
1503 1547
    rb_define_method(rb_cTracePoint, "lineno", tracepoint_attr_lineno, 0);
1504 1548
    rb_define_method(rb_cTracePoint, "path", tracepoint_attr_path, 0);
1549
    rb_define_method(rb_cTracePoint, "parameters", tracepoint_attr_parameters, 0);
1505 1550
    rb_define_method(rb_cTracePoint, "method_id", tracepoint_attr_method_id, 0);
1506 1551
    rb_define_method(rb_cTracePoint, "callee_id", tracepoint_attr_callee_id, 0);
1507 1552
    rb_define_method(rb_cTracePoint, "defined_class", tracepoint_attr_defined_class, 0);