summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog13
-rw-r--r--proc.c12
-rw-r--r--test/ruby/test_settracefunc.rb218
-rw-r--r--vm.c10
-rw-r--r--vm_trace.c455
5 files changed, 658 insertions, 50 deletions
diff --git a/ChangeLog b/ChangeLog
index f331f2c12c..b97ec8a623 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+Wed Aug 22 14:05:23 2012 Koichi Sasada <ko1@atdot.net>
+
+ * vm_trace.c: support TracePoint. [ruby-trunk - Feature #6895]
+
+ * test/ruby/test_settracefunc.rb: add tests for above.
+
+ * proc.c (rb_binding_new_with_cfp): add an internal function.
+
+ * vm.c (rb_vm_control_frame_id_and_class): add an internal function.
+
+ * vm_trace.c: add rb_add_event_hook2() and rb_thread_add_event_hook2().
+ Give us the good name for them!
+
Wed Aug 22 11:38:16 2012 URABE Shyouhei <shyouhei@ruby-lang.org>
* .travis.yml (before_script): Turned out that make -j is broken.
diff --git a/proc.c b/proc.c
index 753464bc32..300c6a0b70 100644
--- a/proc.c
+++ b/proc.c
@@ -310,10 +310,9 @@ binding_clone(VALUE self)
}
VALUE
-rb_binding_new(void)
+rb_binding_new_with_cfp(rb_thread_t *th, rb_control_frame_t *src_cfp)
{
- rb_thread_t *th = GET_THREAD();
- rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(th, th->cfp);
+ rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(th, src_cfp);
VALUE bindval = binding_alloc(rb_cBinding);
rb_binding_t *bind;
@@ -328,6 +327,13 @@ rb_binding_new(void)
return bindval;
}
+VALUE
+rb_binding_new(void)
+{
+ rb_thread_t *th = GET_THREAD();
+ return rb_binding_new_with_cfp(th, th->cfp);
+}
+
/*
* call-seq:
* binding -> a_binding
diff --git a/test/ruby/test_settracefunc.rb b/test/ruby/test_settracefunc.rb
index 463923aa26..d24bcccd4d 100644
--- a/test/ruby/test_settracefunc.rb
+++ b/test/ruby/test_settracefunc.rb
@@ -397,4 +397,222 @@ class TestSetTraceFunc < Test::Unit::TestCase
class << self
define_method(:method_added, Module.method(:method_added))
end
+
+ def test_tracepoint
+ events = []
+ trace = nil
+ xyzzy = nil
+ local_var = :outer
+ eval <<-EOF.gsub(/^.*?: /, ""), nil, 'xyzzy'
+ 1: trace = TracePoint.trace(){|tp|
+ 2: events << [tp.event, tp.line, tp.file, tp.klass, tp.id, tp.self, tp.binding.eval("local_var")]
+ 3: }
+ 4: 1.times{|;local_var| local_var = :inner
+ 5: tap{}
+ 6: }
+ 7: class XYZZY
+ 8: local_var = :XYZZY_outer
+ 9: def foo
+ 10: local_var = :XYZZY_foo
+ 11: bar
+ 12: end
+ 13: def bar
+ 14: local_var = :XYZZY_bar
+ 15: tap{}
+ 16: end
+ 17: end
+ 18: xyzzy = XYZZY.new
+ 19: xyzzy.foo
+ 20: trace.untrace
+ EOF
+
+ events.each{|ev|
+ STDERR.puts [ev[0], ev[1]].inspect
+ STDERR.puts ev.inspect
+ }
+
+ [
+ #
+ [:c_return, 1, "xyzzy", self.class, :trace, TracePoint, :outer],
+ [:line, 4, 'xyzzy', self.class, :test_tracepoint, self, :outer],
+ [:c_call, 4, 'xyzzy', Integer, :times, 1, :outer],
+ [:line, 4, 'xyzzy', self.class, :test_tracepoint, self, nil],
+ [:line, 5, 'xyzzy', self.class, :test_tracepoint, self, :inner],
+ [:c_call, 5, 'xyzzy', Kernel, :tap, self, :inner],
+ [:line, 7, 'xyzzy', self.class, :test_tracepoint, self, :outer],
+ [:c_call, 7, "xyzzy", Class, :new, TestSetTraceFunc::XYZZY, :outer],
+ [:c_call, 7, "xyzzy", BasicObject, :initialize, xyzzy, :outer],
+ [:line, 8, 'xyzzy', self.class, :test_tracepoint, self, :outer],
+ [:c_call, 9, 'xyzzy', TracePoint, :untrace, trace,:outer],
+ ].each{|e|
+ assert_equal e, events.shift
+ }
+ assert_equal [], events
+ end
+
+
+ def trace_by_tracepoint *trace_events
+ events = []
+ trace = nil
+ xyzzy = nil
+ local_var = :outer
+ method = :trace_by_tracepoint
+
+ eval <<-EOF.gsub(/^.*?: /, ""), nil, 'xyzzy'
+ 1: trace = TracePoint.trace(*trace_events){|tp|
+ 2: events << [tp.event, tp.line, tp.file, tp.klass, tp.id, tp.self, tp.binding.eval("local_var")]
+ 3: }
+ 4: 1.times{|;local_var| local_var = :inner
+ 5: tap{}
+ 6: }
+ 7: class XYZZY
+ 8: local_var = :XYZZY_outer
+ 9: def foo
+ 10: local_var = :XYZZY_foo
+ 11: bar
+ 12: end
+ 13: def bar
+ 14: local_var = :XYZZY_bar
+ 15: tap{}
+ 16: end
+ 17: end
+ 18: xyzzy = XYZZY.new
+ 19: xyzzy.foo
+ 20: trace.untrace
+ EOF
+ self.class.class_eval{remove_const(:XYZZY)}
+
+ answer_events = [
+ #
+ [:c_return, 1, "xyzzy", TracePoint, :trace, TracePoint, :outer],
+ [:line, 4, 'xyzzy', self.class, method, self, :outer],
+ [:c_call, 4, 'xyzzy', Integer, :times, 1, :outer],
+ [:line, 4, 'xyzzy', self.class, method, self, nil],
+ [:line, 5, 'xyzzy', self.class, method, self, :inner],
+ [:c_call, 5, 'xyzzy', Kernel, :tap, self, :inner],
+ [:c_return, 5, "xyzzy", Kernel, :tap, self, :inner],
+ [:c_return, 4, "xyzzy", Integer, :times, 1, :outer],
+ [:line, 7, 'xyzzy', self.class, method, self, :outer],
+ [:c_call, 7, "xyzzy", Class, :inherited, Object, :outer],
+ [:c_return, 7, "xyzzy", Class, :inherited, Object, :outer],
+ [:class, 7, "xyzzy", nil, nil, xyzzy.class, nil],
+ [:line, 8, "xyzzy", nil, nil, xyzzy.class, nil],
+ [:line, 9, "xyzzy", nil, nil, xyzzy.class, :XYZZY_outer],
+ [:c_call, 9, "xyzzy", Module, :method_added, xyzzy.class, :XYZZY_outer],
+ [:c_return, 9, "xyzzy", Module, :method_added, xyzzy.class, :XYZZY_outer],
+ [:line, 13, "xyzzy", nil, nil, xyzzy.class, :XYZZY_outer],
+ [:c_call, 13, "xyzzy", Module, :method_added, xyzzy.class, :XYZZY_outer],
+ [:c_return,13, "xyzzy", Module, :method_added, xyzzy.class, :XYZZY_outer],
+ [:end, 17, "xyzzy", nil, nil, xyzzy.class, :XYZZY_outer],
+ [:line, 18, "xyzzy", TestSetTraceFunc, method, self, :outer],
+ [:c_call, 18, "xyzzy", Class, :new, xyzzy.class, :outer],
+ [:c_call, 18, "xyzzy", BasicObject, :initialize, xyzzy, :outer],
+ [:c_return,18, "xyzzy", BasicObject, :initialize, xyzzy, :outer],
+ [:c_return,18, "xyzzy", Class, :new, xyzzy.class, :outer],
+ [:line, 19, "xyzzy", TestSetTraceFunc, method, self, :outer],
+ [:call, 9, "xyzzy", xyzzy.class, :foo, xyzzy, nil],
+ [:line, 10, "xyzzy", xyzzy.class, :foo, xyzzy, nil],
+ [:line, 11, "xyzzy", xyzzy.class, :foo, xyzzy, :XYZZY_foo],
+ [:call, 13, "xyzzy", xyzzy.class, :bar, xyzzy, nil],
+ [:line, 14, "xyzzy", xyzzy.class, :bar, xyzzy, nil],
+ [:line, 15, "xyzzy", xyzzy.class, :bar, xyzzy, :XYZZY_bar],
+ [:c_call, 15, "xyzzy", Kernel, :tap, xyzzy, :XYZZY_bar],
+ [:c_return,15, "xyzzy", Kernel, :tap, xyzzy, :XYZZY_bar],
+ [:return, 16, "xyzzy", xyzzy.class, :bar, xyzzy, :XYZZY_bar],
+ [:return, 12, "xyzzy", xyzzy.class, :foo, xyzzy, :XYZZY_foo],
+ [:line, 20, "xyzzy", TestSetTraceFunc, method, self, :outer],
+ [:c_call, 20, "xyzzy", TracePoint, :untrace, trace, :outer],
+ ]
+
+ return events, answer_events
+ end
+
+ def trace_by_set_trace_func
+ events = []
+ trace = nil
+ xyzzy = nil
+ local_var = :outer
+ eval <<-EOF.gsub(/^.*?: /, ""), nil, 'xyzzy'
+ 1: set_trace_func(lambda{|event, file, line, id, binding, klass|
+ 2: events << [event, line, file, klass, id, binding.eval('self'), binding.eval("local_var")]
+ 3: })
+ 4: 1.times{|;local_var| local_var = :inner
+ 5: tap{}
+ 6: }
+ 7: class XYZZY
+ 8: local_var = :XYZZY_outer
+ 9: def foo
+ 10: local_var = :XYZZY_foo
+ 11: bar
+ 12: end
+ 13: def bar
+ 14: local_var = :XYZZY_bar
+ 15: tap{}
+ 16: end
+ 17: end
+ 18: xyzzy = XYZZY.new
+ 19: xyzzy.foo
+ 20: set_trace_func(nil)
+ EOF
+ self.class.class_eval{remove_const(:XYZZY)}
+ return events
+ end
+
+ def test_tracepoint
+ events1, answer_events = *trace_by_tracepoint()
+ answer_events.zip(events1){|answer, event|
+ assert_equal answer, event
+ }
+
+ events2 = trace_by_set_trace_func
+ events1.zip(events2){|ev1, ev2|
+ ev2[0] = ev2[0].sub('-', '_').to_sym
+ assert_equal ev1[0..2], ev2[0..2], ev1.inspect
+
+ # event, line, file, klass, id, binding.eval('self'), binding.eval("local_var")
+ assert_equal ev1[3].nil?, ev2[3].nil? # klass
+ assert_equal ev1[4].nil?, ev2[4].nil? # id
+ assert_equal ev1[6], ev2[6] # local_var
+ }
+
+ [:line, :class, :end, :call, :return, :c_call, :c_return, :raise].each{|event|
+ events1, answer_events = *trace_by_tracepoint(event)
+ answer_events.find_all{|e| e[0] == event}.zip(events1){|answer_line, event_line|
+ assert_equal answer_line, event_line
+ }
+ }
+ end
+
+ def test_tracepoint_object_id
+ tps = []
+ trace = TracePoint.trace(){|tp|
+ tps << tp
+ }
+ tap{}
+ tap{}
+ tap{}
+ trace.untrace
+
+ # passed tp is unique, `trace' object which is genereted by TracePoint.trace
+ tps.each{|tp|
+ assert_equal trace, tp
+ }
+ end
+
+ def test_tracepoint_access_from_outside
+ tp_store = nil
+ trace = TracePoint.trace(){|tp|
+ tp_store = tp
+ }
+ tap{}
+ trace.untrace
+
+ assert_raise(RuntimeError){tp_store.line}
+ assert_raise(RuntimeError){tp_store.event}
+ assert_raise(RuntimeError){tp_store.file}
+ assert_raise(RuntimeError){tp_store.id}
+ assert_raise(RuntimeError){tp_store.klass}
+ assert_raise(RuntimeError){tp_store.binding}
+ assert_raise(RuntimeError){tp_store.self}
+ end
end
diff --git a/vm.c b/vm.c
index 277e33d27a..53a0aba6bc 100644
--- a/vm.c
+++ b/vm.c
@@ -1392,10 +1392,8 @@ rb_iseq_eval_main(VALUE iseqval)
}
int
-rb_thread_method_id_and_class(rb_thread_t *th,
- ID *idp, VALUE *klassp)
+rb_vm_control_frame_id_and_class(rb_control_frame_t *cfp, ID *idp, VALUE *klassp)
{
- rb_control_frame_t *cfp = th->cfp;
rb_iseq_t *iseq = cfp->iseq;
if (!iseq && cfp->me) {
if (idp) *idp = cfp->me->def->original_id;
@@ -1422,6 +1420,12 @@ rb_thread_method_id_and_class(rb_thread_t *th,
}
int
+rb_thread_method_id_and_class(rb_thread_t *th, ID *idp, VALUE *klassp)
+{
+ return rb_vm_control_frame_id_and_class(th->cfp, idp, klassp);
+}
+
+int
rb_frame_method_id_and_class(ID *idp, VALUE *klassp)
{
return rb_thread_method_id_and_class(GET_THREAD(), idp, klassp);
diff --git a/vm_trace.c b/vm_trace.c
index aff71cc876..16317bdc19 100644
--- a/vm_trace.c
+++ b/vm_trace.c
@@ -33,6 +33,7 @@
typedef enum {
RUBY_HOOK_FLAG_SAFE = 0x01,
RUBY_HOOK_FLAG_DELETED = 0x02,
+ RUBY_HOOK_FLAG_RAW_ARG = 0x04
} rb_hook_flag_t;
typedef struct rb_event_hook_struct {
@@ -43,6 +44,17 @@ typedef struct rb_event_hook_struct {
struct rb_event_hook_struct *next;
} rb_event_hook_t;
+typedef struct rb_trace_arg_struct {
+ rb_event_flag_t event;
+ rb_thread_t *th;
+ rb_control_frame_t *cfp;
+ VALUE self;
+ ID id;
+ VALUE klass;
+} rb_trace_arg_t;
+
+typedef void (*rb_event_hook_raw_arg_func_t)(VALUE data, const rb_trace_arg_t *arg);
+
#define MAX_EVENT_NUM 32
static int ruby_event_flag_count[MAX_EVENT_NUM] = {0};
@@ -50,14 +62,14 @@ static int ruby_event_flag_count[MAX_EVENT_NUM] = {0};
/* Safe API. Callback will be called under PUSH_TAG() */
void rb_add_event_hook(rb_event_hook_func_t func, rb_event_flag_t events, VALUE data);
int rb_remove_event_hook(rb_event_hook_func_t func);
+int rb_remove_event_hook_with_data(rb_event_hook_func_t func, VALUE data);
void rb_thread_add_event_hook(VALUE thval, rb_event_hook_func_t func, rb_event_flag_t events, VALUE data);
int rb_thread_remove_event_hook(VALUE thval, rb_event_hook_func_t func);
+int rb_thread_remove_event_hook_with_data(VALUE thval, rb_event_hook_func_t func, VALUE data);
-/* Raw API. Callback will be called without PUSH_TAG() */
-void rb_add_raw_event_hook(rb_event_hook_func_t func, rb_event_flag_t events, VALUE data);
-int rb_remove_raw_event_hook(rb_event_hook_func_t func);
-void rb_thread_add_raw_event_hook(VALUE thval, rb_event_hook_func_t func, rb_event_flag_t events, VALUE data);
-int rb_thread_remove_raw_event_hook(VALUE thval, rb_event_hook_func_t func);
+/* advanced version */
+void rb_add_event_hook2(rb_event_hook_func_t func, rb_event_flag_t events, VALUE data, rb_hook_flag_t hook_flag);
+void rb_thread_add_event_hook2(VALUE thval, rb_event_hook_func_t func, rb_event_flag_t events, VALUE data, rb_hook_flag_t hook_flag);
/* called from vm.c */
@@ -153,30 +165,32 @@ rb_add_event_hook(rb_event_hook_func_t func, rb_event_flag_t events, VALUE data)
}
void
-rb_thread_add_raw_event_hook(VALUE thval, rb_event_hook_func_t func, rb_event_flag_t events, VALUE data)
+rb_thread_add_event_hook2(VALUE thval, rb_event_hook_func_t func, rb_event_flag_t events, VALUE data, rb_hook_flag_t hook_flags)
{
- rb_threadptr_add_event_hook(thval2thread_t(thval), func, events, data, 0);
+ rb_threadptr_add_event_hook(thval2thread_t(thval), func, events, data, hook_flags);
}
void
-rb_add_raw_event_hook(rb_event_hook_func_t func, rb_event_flag_t events, VALUE data)
+rb_add_event_hook2(rb_event_hook_func_t func, rb_event_flag_t events, VALUE data, rb_hook_flag_t hook_flags)
{
- rb_event_hook_t *hook = alloc_event_hook(func, events, data, 0);
+ rb_event_hook_t *hook = alloc_event_hook(func, events, data, hook_flags);
connect_event_hook(&GET_VM()->event_hooks, hook);
}
/* if func is 0, then clear all funcs */
static int
-remove_event_hook_by_func(rb_hook_list_t *list, rb_event_hook_func_t func)
+remove_event_hook(rb_hook_list_t *list, rb_event_hook_func_t func, VALUE data)
{
int ret = 0;
rb_event_hook_t *hook = list->hooks;
while (hook) {
if (func == 0 || hook->func == func) {
- hook->hook_flags |= RUBY_HOOK_FLAG_DELETED;
- ret+=1;
- list->need_clean++;
+ if (data == Qundef || hook->data == data) {
+ hook->hook_flags |= RUBY_HOOK_FLAG_DELETED;
+ ret+=1;
+ list->need_clean++;
+ }
}
hook = hook->next;
}
@@ -185,21 +199,33 @@ remove_event_hook_by_func(rb_hook_list_t *list, rb_event_hook_func_t func)
}
static int
-rb_threadptr_remove_event_hook(rb_thread_t *th, rb_event_hook_func_t func)
+rb_threadptr_remove_event_hook(rb_thread_t *th, rb_event_hook_func_t func, VALUE data)
{
- return remove_event_hook_by_func(&th->event_hooks, func);
+ return remove_event_hook(&th->event_hooks, func, data);
}
int
rb_thread_remove_event_hook(VALUE thval, rb_event_hook_func_t func)
{
- return rb_threadptr_remove_event_hook(thval2thread_t(thval), func);
+ return rb_threadptr_remove_event_hook(thval2thread_t(thval), func, Qundef);
+}
+
+int
+rb_thread_remove_event_hook_with_data(VALUE thval, rb_event_hook_func_t func, VALUE data)
+{
+ return rb_threadptr_remove_event_hook(thval2thread_t(thval), func, data);
}
int
rb_remove_event_hook(rb_event_hook_func_t func)
{
- return remove_event_hook_by_func(&GET_VM()->event_hooks, func);
+ return remove_event_hook(&GET_VM()->event_hooks, func, Qundef);
+}
+
+int
+rb_remove_event_hook_with_data(rb_event_hook_func_t func, VALUE data)
+{
+ return remove_event_hook(&GET_VM()->event_hooks, func, data);
}
static int
@@ -207,7 +233,7 @@ clear_trace_func_i(st_data_t key, st_data_t val, st_data_t flag)
{
rb_thread_t *th;
GetThreadPtr((VALUE)key, th);
- rb_threadptr_remove_event_hook(th, 0);
+ rb_threadptr_remove_event_hook(th, 0, Qundef);
return ST_CONTINUE;
}
@@ -251,8 +277,8 @@ clean_hooks(rb_hook_list_t *list)
}
}
-static inline int
-exec_hooks(rb_thread_t *th, rb_hook_list_t *list, rb_event_flag_t event, VALUE self, ID id, VALUE klass)
+static int
+exec_hooks(rb_thread_t *th, rb_hook_list_t *list, const rb_trace_arg_t *trace_arg)
{
rb_event_hook_t *hook;
int state;
@@ -271,8 +297,13 @@ exec_hooks(rb_thread_t *th, rb_hook_list_t *list, rb_event_flag_t event, VALUE s
TH_PUSH_TAG(th);
if ((state = TH_EXEC_TAG()) == 0) {
while (hook) {
- if (LIKELY(!(hook->hook_flags & RUBY_HOOK_FLAG_DELETED)) && (event & hook->events)) {
- (*hook->func)(event, hook->data, self, id, klass);
+ if (LIKELY(!(hook->hook_flags & RUBY_HOOK_FLAG_DELETED)) && (trace_arg->event & hook->events)) {
+ if (!(hook->hook_flags & RUBY_HOOK_FLAG_RAW_ARG)) {
+ (*hook->func)(trace_arg->event, hook->data, trace_arg->self, trace_arg->id, trace_arg->klass);
+ }
+ else {
+ (*((rb_event_hook_raw_arg_func_t)hook->func))(hook->data, trace_arg);
+ }
}
hook = hook->next;
}
@@ -299,18 +330,26 @@ rb_threadptr_exec_event_hooks(rb_thread_t *th, rb_event_flag_t event, VALUE self
{
const VALUE errinfo = th->errinfo;
rb_hook_list_t *list;
+ rb_trace_arg_t ta;
+
+ ta.event = event;
+ ta.th = th;
+ ta.cfp = th->cfp;
+ ta.self = self;
+ ta.id = id;
+ ta.klass = klass;
/* thread local traces */
list = &th->event_hooks;
if (list->events & event) {
- state = exec_hooks(th, list, event, self, id, klass);
+ state = exec_hooks(th, list, &ta);
if (state) goto terminate;
}
/* vm global traces */
list = &th->vm->event_hooks;
if (list->events & event) {
- state = exec_hooks(th, list, event, self, id, klass);
+ state = exec_hooks(th, list, &ta);
if (state) goto terminate;
}
th->errinfo = errinfo;
@@ -465,7 +504,7 @@ thread_set_trace_func_m(VALUE obj, VALUE trace)
{
rb_thread_t *th;
GetThreadPtr(obj, th);
- rb_threadptr_remove_event_hook(th, call_trace_func);
+ rb_threadptr_remove_event_hook(th, call_trace_func, Qundef);
if (NIL_P(trace)) {
return Qnil;
@@ -479,27 +518,40 @@ static const char *
get_event_name(rb_event_flag_t event)
{
switch (event) {
- case RUBY_EVENT_LINE:
- return "line";
- case RUBY_EVENT_CLASS:
- return "class";
- case RUBY_EVENT_END:
- return "end";
- case RUBY_EVENT_CALL:
- return "call";
- case RUBY_EVENT_RETURN:
- return "return";
- case RUBY_EVENT_C_CALL:
- return "c-call";
- case RUBY_EVENT_C_RETURN:
- return "c-return";
- case RUBY_EVENT_RAISE:
- return "raise";
+ case RUBY_EVENT_LINE: return "line";
+ case RUBY_EVENT_CLASS: return "class";
+ case RUBY_EVENT_END: return "end";
+ case RUBY_EVENT_CALL: return "call";
+ case RUBY_EVENT_RETURN: return "return";
+ case RUBY_EVENT_C_CALL: return "c-call";
+ case RUBY_EVENT_C_RETURN: return "c-return";
+ case RUBY_EVENT_RAISE: return "raise";
default:
return "unknown";
}
}
+static ID
+get_event_id(rb_event_flag_t event)
+{
+ ID id;
+
+ switch (event) {
+#define C(name, NAME) case RUBY_EVENT_##NAME: CONST_ID(id, #name); return id;
+ C(line, LINE);
+ C(class, CLASS);
+ C(end, END);
+ C(call, CALL);
+ C(return, RETURN);
+ C(c_call, C_CALL);
+ C(c_return, C_RETURN);
+ C(raise, RAISE);
+#undef C
+ default:
+ return 0;
+ }
+}
+
static void
call_trace_func(rb_event_flag_t event, VALUE proc, VALUE self, ID id, VALUE klass)
{
@@ -536,16 +588,331 @@ call_trace_func(rb_event_flag_t event, VALUE proc, VALUE self, ID id, VALUE klas
rb_proc_call_with_block(proc, 6, argv, Qnil);
}
-/* (2-2) TracePoint API (not yet) */
+/* (2-2) TracePoint API */
+static VALUE rb_cTracePoint;
+
+typedef struct rb_tp_struct {
+ rb_event_flag_t events;
+ rb_thread_t *target_th;
+ VALUE proc;
+ rb_trace_arg_t *trace_arg;
+ int tracing;
+} rb_tp_t;
+
+static void
+tp_mark(void *ptr)
+{
+ if (ptr) {
+ rb_tp_t *tp = (rb_tp_t *)ptr;
+ rb_gc_mark(tp->proc);
+ if (tp->target_th) rb_gc_mark(tp->target_th->self);
+ }
+}
+
+static void
+tp_free(void *ptr)
+{
+ /* do nothing */
+}
+
+static size_t
+tp_memsize(const void *ptr)
+{
+ return sizeof(rb_tp_t);
+}
+
+static const rb_data_type_t tp_data_type = {
+ "tracepoint",
+ {tp_mark, tp_free, tp_memsize,},
+};
+
+static VALUE
+tp_alloc(VALUE klass)
+{
+ rb_tp_t *tp;
+ return TypedData_Make_Struct(klass, rb_tp_t, &tp_data_type, tp);
+}
+
+static rb_event_flag_t
+symbol2event_flag(VALUE v)
+{
+ static ID id;
+ VALUE sym = rb_convert_type(v, T_SYMBOL, "Symbol", "to_sym");
+
+#define C(name, NAME) CONST_ID(id, #name); if (sym == ID2SYM(id)) return RUBY_EVENT_##NAME
+ C(line, LINE);
+ C(class, CLASS);
+ C(end, END);
+ C(call, CALL);
+ C(return, RETURN);
+ C(c_call, C_CALL);
+ C(c_return, C_RETURN);
+ C(raise, RAISE);
+#undef C
+ rb_raise(rb_eArgError, "unknown event: %s", rb_id2name(SYM2ID(sym)));
+}
+
+static rb_tp_t *
+tpptr(VALUE tpval)
+{
+ rb_tp_t *tp;
+ TypedData_Get_Struct(tpval, rb_tp_t, &tp_data_type, tp);
+ return tp;
+}
+
+static void
+tp_attr_check_active(rb_tp_t *tp)
+{
+ if (tp->trace_arg == 0) {
+ rb_raise(rb_eRuntimeError, "access from outside");
+ }
+}
+
+static VALUE
+tp_attr_event_m(VALUE tpval)
+{
+ rb_tp_t *tp = tpptr(tpval);
+ tp_attr_check_active(tp);
+ return ID2SYM(get_event_id(tp->trace_arg->event));
+}
+
+rb_control_frame_t *rb_vm_get_ruby_level_next_cfp(rb_thread_t *th, rb_control_frame_t *cfp);
+int rb_vm_control_frame_id_and_class(rb_control_frame_t *cfp, ID *idp, VALUE *klassp);
+VALUE rb_binding_new_with_cfp(rb_thread_t *th, rb_control_frame_t *src_cfp);
+
+static VALUE
+tp_attr_line_m(VALUE tpval)
+{
+ rb_tp_t *tp = tpptr(tpval);
+ rb_control_frame_t *cfp;
+ tp_attr_check_active(tp);
+
+ cfp = rb_vm_get_ruby_level_next_cfp(tp->trace_arg->th, tp->trace_arg->cfp);
+ if (cfp) {
+ return INT2FIX(rb_vm_get_sourceline(cfp));
+ }
+ else {
+ return INT2FIX(0);
+ }
+}
+
+static VALUE
+tp_attr_file_m(VALUE tpval)
+{
+ rb_tp_t *tp = tpptr(tpval);
+ rb_control_frame_t *cfp;
+ tp_attr_check_active(tp);
+
+ cfp = rb_vm_get_ruby_level_next_cfp(tp->trace_arg->th, tp->trace_arg->cfp);
+ if (cfp) {
+ return cfp->iseq->location.path;
+ }
+ else {
+ return Qnil;
+ }
+}
+
+static void
+fill_id_and_klass(rb_trace_arg_t *trace_arg)
+{
+ if (!trace_arg->klass)
+ rb_vm_control_frame_id_and_class(trace_arg->cfp, &trace_arg->id, &trace_arg->klass);
+
+ if (trace_arg->klass) {
+ if (RB_TYPE_P(trace_arg->klass, T_ICLASS)) {
+ trace_arg->klass = RBASIC(trace_arg->klass)->klass;
+ }
+ else if (FL_TEST(trace_arg->klass, FL_SINGLETON)) {
+ trace_arg->klass = rb_iv_get(trace_arg->klass, "__attached__");
+ }
+ }
+}
+
+static VALUE
+tp_attr_id_m(VALUE tpval)
+{
+ rb_tp_t *tp = tpptr(tpval);
+ tp_attr_check_active(tp);
+ fill_id_and_klass(tp->trace_arg);
+ if (tp->trace_arg->id) {
+ return ID2SYM(tp->trace_arg->id);
+ }
+ else {
+ return Qnil;
+ }
+}
+
+static VALUE
+tp_attr_klass_m(VALUE tpval)
+{
+ rb_tp_t *tp = tpptr(tpval);
+ tp_attr_check_active(tp);
+ fill_id_and_klass(tp->trace_arg);
+
+ if (tp->trace_arg->klass) {
+ return tp->trace_arg->klass;
+ }
+ else {
+ return Qnil;
+ }
+}
+
+static VALUE
+tp_attr_binding_m(VALUE tpval)
+{
+ rb_tp_t *tp = tpptr(tpval);
+ rb_control_frame_t *cfp;
+ tp_attr_check_active(tp);
+
+ cfp = rb_vm_get_ruby_level_next_cfp(tp->trace_arg->th, tp->trace_arg->cfp);
+ if (cfp) {
+ return rb_binding_new_with_cfp(tp->trace_arg->th, cfp);
+ }
+ else {
+ return Qnil;
+ }
+}
+
+static VALUE
+tp_attr_self_m(VALUE tpval)
+{
+ rb_tp_t *tp = tpptr(tpval);
+ tp_attr_check_active(tp);
+
+ return tp->trace_arg->self;
+}
+
+static void
+tp_call_trace(VALUE tpval, rb_trace_arg_t *trace_arg)
+{
+ rb_tp_t *tp = tpptr(tpval);
+ rb_thread_t *th = GET_THREAD();
+ int state;
+
+ if (UNLIKELY(trace_arg->id == ID_ALLOCATOR)) {
+ return;
+ }
+
+ tp->trace_arg = trace_arg;
+
+ TH_PUSH_TAG(th);
+ if ((state = TH_EXEC_TAG()) == 0) {
+ rb_proc_call_with_block(tp->proc, 1, &tpval, Qnil);
+ }
+ TH_POP_TAG();
+
+ tp->trace_arg = 0;
+
+ if (state) {
+ TH_JUMP_TAG(th, state);
+ }
+}
+
+static VALUE
+tp_set_trace(VALUE tpval)
+{
+ rb_tp_t *tp = tpptr(tpval);
+
+ if (tp->tracing) {
+ /* already tracing */
+ /* TODO: raise error? */
+ }
+ else {
+ if (tp->target_th) {
+ rb_thread_add_event_hook2(tp->target_th->self, (rb_event_hook_func_t)tp_call_trace, tp->events, tpval, RUBY_HOOK_FLAG_SAFE | RUBY_HOOK_FLAG_RAW_ARG);
+ }
+ else {
+ rb_add_event_hook2((rb_event_hook_func_t)tp_call_trace, tp->events, tpval, RUBY_HOOK_FLAG_SAFE | RUBY_HOOK_FLAG_RAW_ARG);
+ }
+ tp->tracing = 1;
+ }
+
+ return tpval;
+}
+
+static VALUE
+tp_unset_trace(VALUE tpval)
+{
+ rb_tp_t *tp = tpptr(tpval);
+
+ if (!tp->tracing) {
+ /* not tracing */
+ /* TODO: raise error? */
+ }
+ else {
+ if (tp->target_th) {
+ rb_thread_remove_event_hook_with_data(tp->target_th->self, (rb_event_hook_func_t)tp_call_trace, tpval);
+ }
+ else {
+ rb_remove_event_hook_with_data((rb_event_hook_func_t)tp_call_trace, tpval);
+ }
+ tp->tracing = 0;
+ }
+
+ return tpval;
+}
+
+static VALUE
+tp_initialize(rb_thread_t *target_th, rb_event_flag_t events, VALUE proc)
+{
+ VALUE tpval = tp_alloc(rb_cTracePoint);
+ rb_tp_t *tp;
+ TypedData_Get_Struct(tpval, rb_tp_t, &tp_data_type, tp);
+
+ tp->proc = proc;
+ tp->events = events;
+
+ tp_set_trace(tpval);
+
+ return tpval;
+}
+
+static VALUE
+tp_trace_s(int argc, VALUE *argv)
+{
+ rb_event_flag_t events = 0;
+ int i;
+
+ if (argc > 0) {
+ for (i=0; i<argc; i++) {
+ events |= symbol2event_flag(argv[i]);
+ }
+ }
+ else {
+ events = RUBY_EVENT_ALL;
+ }
+
+ if (!rb_block_given_p()) {
+ rb_raise(rb_eThreadError, "must be called with a block");
+ }
+
+ return tp_initialize(0, events, rb_block_proc());
+}
/* This function is called from inits.c */
void
Init_vm_trace(void)
{
- /* trace */
+ /* trace_func */
rb_define_global_function("set_trace_func", set_trace_func, 1);
rb_define_method(rb_cThread, "set_trace_func", thread_set_trace_func_m, 1);
rb_define_method(rb_cThread, "add_trace_func", thread_add_trace_func_m, 1);
-}
+ /* TracePoint */
+ rb_cTracePoint = rb_define_class("TracePoint", rb_cObject);
+ rb_undef_alloc_func(rb_cTracePoint);
+ rb_undef_method(CLASS_OF(rb_cTracePoint), "new");
+ rb_define_singleton_method(rb_cTracePoint, "trace", tp_trace_s, -1);
+
+ rb_define_method(rb_cTracePoint, "retrace", tp_set_trace, 0);
+ rb_define_method(rb_cTracePoint, "untrace", tp_unset_trace, 0);
+
+ rb_define_method(rb_cTracePoint, "event", tp_attr_event_m, 0);
+ rb_define_method(rb_cTracePoint, "line", tp_attr_line_m, 0);
+ rb_define_method(rb_cTracePoint, "file", tp_attr_file_m, 0);
+ rb_define_method(rb_cTracePoint, "id", tp_attr_id_m, 0);
+ rb_define_method(rb_cTracePoint, "klass", tp_attr_klass_m, 0);
+ rb_define_method(rb_cTracePoint, "binding", tp_attr_binding_m, 0);
+ rb_define_method(rb_cTracePoint, "self", tp_attr_self_m, 0);
+}