2012-08-15 00:39:10 -04:00
|
|
|
/**********************************************************************
|
|
|
|
|
|
|
|
vm_trace.c -
|
|
|
|
|
|
|
|
$Author: ko1 $
|
|
|
|
created at: Tue Aug 14 19:37:09 2012
|
|
|
|
|
|
|
|
Copyright (C) 1993-2012 Yukihiro Matsumoto
|
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file incldue two parts:
|
|
|
|
*
|
|
|
|
* (1) set_trace_func internal mechanisms
|
|
|
|
* and C level API
|
|
|
|
*
|
|
|
|
* (2) Ruby level API
|
|
|
|
* (2-1) set_trace_func API
|
|
|
|
* (2-2) TracePoint API (not yet)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ruby/ruby.h"
|
2012-11-20 07:57:49 -05:00
|
|
|
#include "ruby/debug.h"
|
2012-08-15 00:39:10 -04:00
|
|
|
#include "ruby/encoding.h"
|
|
|
|
|
|
|
|
#include "internal.h"
|
|
|
|
#include "vm_core.h"
|
|
|
|
#include "eval_intern.h"
|
|
|
|
|
|
|
|
/* (1) trace mechanisms */
|
|
|
|
|
2012-08-16 07:41:24 -04:00
|
|
|
typedef struct rb_event_hook_struct {
|
2012-11-20 07:57:49 -05:00
|
|
|
rb_event_hook_flag_t hook_flags;
|
2012-08-16 07:41:24 -04:00
|
|
|
rb_event_flag_t events;
|
|
|
|
rb_event_hook_func_t func;
|
|
|
|
VALUE data;
|
|
|
|
struct rb_event_hook_struct *next;
|
|
|
|
} rb_event_hook_t;
|
2012-08-15 00:39:10 -04:00
|
|
|
|
2012-08-22 01:12:31 -04:00
|
|
|
typedef void (*rb_event_hook_raw_arg_func_t)(VALUE data, const rb_trace_arg_t *arg);
|
|
|
|
|
2012-08-16 07:41:24 -04:00
|
|
|
#define MAX_EVENT_NUM 32
|
2012-08-15 00:39:10 -04:00
|
|
|
|
2012-08-16 07:41:24 -04:00
|
|
|
static int ruby_event_flag_count[MAX_EVENT_NUM] = {0};
|
2012-08-15 00:39:10 -04:00
|
|
|
|
2012-08-16 07:41:24 -04:00
|
|
|
/* called from vm.c */
|
|
|
|
|
|
|
|
void
|
2013-03-06 01:30:03 -05:00
|
|
|
rb_vm_trace_mark_event_hooks(rb_hook_list_t *hooks)
|
2012-08-15 00:39:10 -04:00
|
|
|
{
|
2012-08-16 07:41:24 -04:00
|
|
|
rb_event_hook_t *hook = hooks->hooks;
|
|
|
|
|
|
|
|
while (hook) {
|
|
|
|
rb_gc_mark(hook->data);
|
|
|
|
hook = hook->next;
|
|
|
|
}
|
2012-08-15 00:39:10 -04:00
|
|
|
}
|
|
|
|
|
2012-08-16 07:41:24 -04:00
|
|
|
/* ruby_vm_event_flags management */
|
|
|
|
|
2012-08-15 00:39:10 -04:00
|
|
|
static void
|
2012-08-16 07:41:24 -04:00
|
|
|
recalc_add_ruby_vm_event_flags(rb_event_flag_t events)
|
2012-08-15 00:39:10 -04:00
|
|
|
{
|
2012-08-16 07:41:24 -04:00
|
|
|
int i;
|
|
|
|
ruby_vm_event_flags = 0;
|
2012-08-15 00:39:10 -04:00
|
|
|
|
2012-08-16 07:41:24 -04:00
|
|
|
for (i=0; i<MAX_EVENT_NUM; i++) {
|
|
|
|
if (events & (1 << i)) {
|
|
|
|
ruby_event_flag_count[i]++;
|
|
|
|
}
|
|
|
|
ruby_vm_event_flags |= ruby_event_flag_count[i] ? (1<<i) : 0;
|
2012-08-15 00:39:10 -04:00
|
|
|
}
|
2013-05-26 20:21:02 -04:00
|
|
|
|
|
|
|
rb_objspace_set_event_hook(ruby_vm_event_flags);
|
2012-08-15 00:39:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-08-16 07:41:24 -04:00
|
|
|
recalc_remove_ruby_vm_event_flags(rb_event_flag_t events)
|
2012-08-15 00:39:10 -04:00
|
|
|
{
|
2012-08-16 07:41:24 -04:00
|
|
|
int i;
|
|
|
|
ruby_vm_event_flags = 0;
|
|
|
|
|
|
|
|
for (i=0; i<MAX_EVENT_NUM; i++) {
|
|
|
|
if (events & (1 << i)) {
|
|
|
|
ruby_event_flag_count[i]--;
|
|
|
|
}
|
|
|
|
ruby_vm_event_flags |= ruby_event_flag_count[i] ? (1<<i) : 0;
|
|
|
|
}
|
2013-05-26 20:21:02 -04:00
|
|
|
|
|
|
|
rb_objspace_set_event_hook(ruby_vm_event_flags);
|
2012-08-15 00:39:10 -04:00
|
|
|
}
|
|
|
|
|
2012-08-16 07:41:24 -04:00
|
|
|
/* add/remove hooks */
|
|
|
|
|
2012-08-15 00:39:10 -04:00
|
|
|
static rb_thread_t *
|
|
|
|
thval2thread_t(VALUE thval)
|
|
|
|
{
|
|
|
|
rb_thread_t *th;
|
|
|
|
GetThreadPtr(thval, th);
|
|
|
|
return th;
|
|
|
|
}
|
|
|
|
|
2012-08-16 07:41:24 -04:00
|
|
|
static rb_event_hook_t *
|
2012-11-20 07:57:49 -05:00
|
|
|
alloc_event_hook(rb_event_hook_func_t func, rb_event_flag_t events, VALUE data, rb_event_hook_flag_t hook_flags)
|
2012-08-15 00:39:10 -04:00
|
|
|
{
|
2012-08-16 07:41:24 -04:00
|
|
|
rb_event_hook_t *hook = ALLOC(rb_event_hook_t);
|
|
|
|
hook->hook_flags = hook_flags;
|
|
|
|
hook->events = events;
|
|
|
|
hook->func = func;
|
|
|
|
hook->data = data;
|
|
|
|
return hook;
|
2012-08-15 00:39:10 -04:00
|
|
|
}
|
|
|
|
|
2012-08-16 07:41:24 -04:00
|
|
|
static void
|
|
|
|
connect_event_hook(rb_hook_list_t *list, rb_event_hook_t *hook)
|
2012-08-15 00:39:10 -04:00
|
|
|
{
|
2012-08-16 07:41:24 -04:00
|
|
|
hook->next = list->hooks;
|
|
|
|
list->hooks = hook;
|
|
|
|
recalc_add_ruby_vm_event_flags(hook->events);
|
|
|
|
list->events |= hook->events;
|
2012-08-15 00:39:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-11-20 07:57:49 -05:00
|
|
|
rb_threadptr_add_event_hook(rb_thread_t *th, rb_event_hook_func_t func, rb_event_flag_t events, VALUE data, rb_event_hook_flag_t hook_flags)
|
2012-08-15 00:39:10 -04:00
|
|
|
{
|
2012-08-16 07:41:24 -04:00
|
|
|
rb_event_hook_t *hook = alloc_event_hook(func, events, data, hook_flags);
|
|
|
|
connect_event_hook(&th->event_hooks, hook);
|
2012-08-15 00:39:10 -04:00
|
|
|
}
|
|
|
|
|
2012-08-16 07:41:24 -04:00
|
|
|
void
|
|
|
|
rb_thread_add_event_hook(VALUE thval, rb_event_hook_func_t func, rb_event_flag_t events, VALUE data)
|
2012-08-15 00:39:10 -04:00
|
|
|
{
|
2012-11-20 07:57:49 -05:00
|
|
|
rb_threadptr_add_event_hook(thval2thread_t(thval), func, events, data, RUBY_EVENT_HOOK_FLAG_SAFE);
|
2012-08-15 00:39:10 -04:00
|
|
|
}
|
|
|
|
|
2012-08-16 07:41:24 -04:00
|
|
|
void
|
|
|
|
rb_add_event_hook(rb_event_hook_func_t func, rb_event_flag_t events, VALUE data)
|
2012-08-15 00:39:10 -04:00
|
|
|
{
|
2012-11-20 07:57:49 -05:00
|
|
|
rb_event_hook_t *hook = alloc_event_hook(func, events, data, RUBY_EVENT_HOOK_FLAG_SAFE);
|
2012-08-16 07:41:24 -04:00
|
|
|
connect_event_hook(&GET_VM()->event_hooks, hook);
|
2012-08-15 00:39:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-20 07:57:49 -05:00
|
|
|
rb_thread_add_event_hook2(VALUE thval, rb_event_hook_func_t func, rb_event_flag_t events, VALUE data, rb_event_hook_flag_t hook_flags)
|
2012-08-15 00:39:10 -04:00
|
|
|
{
|
2012-08-22 01:12:31 -04:00
|
|
|
rb_threadptr_add_event_hook(thval2thread_t(thval), func, events, data, hook_flags);
|
2012-08-15 00:39:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-20 07:57:49 -05:00
|
|
|
rb_add_event_hook2(rb_event_hook_func_t func, rb_event_flag_t events, VALUE data, rb_event_hook_flag_t hook_flags)
|
2012-08-15 00:39:10 -04:00
|
|
|
{
|
2012-08-22 01:12:31 -04:00
|
|
|
rb_event_hook_t *hook = alloc_event_hook(func, events, data, hook_flags);
|
2012-08-16 07:41:24 -04:00
|
|
|
connect_event_hook(&GET_VM()->event_hooks, hook);
|
2012-08-15 00:39:10 -04:00
|
|
|
}
|
|
|
|
|
2012-08-16 07:41:24 -04:00
|
|
|
/* if func is 0, then clear all funcs */
|
2012-08-15 00:39:10 -04:00
|
|
|
static int
|
2012-08-22 01:12:31 -04:00
|
|
|
remove_event_hook(rb_hook_list_t *list, rb_event_hook_func_t func, VALUE data)
|
2012-08-15 00:39:10 -04:00
|
|
|
{
|
2012-08-16 07:41:24 -04:00
|
|
|
int ret = 0;
|
|
|
|
rb_event_hook_t *hook = list->hooks;
|
|
|
|
|
2012-08-15 00:39:10 -04:00
|
|
|
while (hook) {
|
|
|
|
if (func == 0 || hook->func == func) {
|
2012-08-22 01:12:31 -04:00
|
|
|
if (data == Qundef || hook->data == data) {
|
2012-11-20 07:57:49 -05:00
|
|
|
hook->hook_flags |= RUBY_EVENT_HOOK_FLAG_DELETED;
|
2012-08-22 01:12:31 -04:00
|
|
|
ret+=1;
|
|
|
|
list->need_clean++;
|
|
|
|
}
|
2012-08-15 00:39:10 -04:00
|
|
|
}
|
|
|
|
hook = hook->next;
|
|
|
|
}
|
|
|
|
|
2012-08-16 07:41:24 -04:00
|
|
|
return ret;
|
2012-08-15 00:39:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2012-08-22 01:12:31 -04:00
|
|
|
rb_threadptr_remove_event_hook(rb_thread_t *th, rb_event_hook_func_t func, VALUE data)
|
2012-08-15 00:39:10 -04:00
|
|
|
{
|
2012-08-22 01:12:31 -04:00
|
|
|
return remove_event_hook(&th->event_hooks, func, data);
|
2012-08-15 00:39:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_thread_remove_event_hook(VALUE thval, rb_event_hook_func_t func)
|
|
|
|
{
|
2012-08-22 01:12:31 -04:00
|
|
|
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);
|
2012-08-15 00:39:10 -04:00
|
|
|
}
|
|
|
|
|
2012-08-16 07:41:24 -04:00
|
|
|
int
|
|
|
|
rb_remove_event_hook(rb_event_hook_func_t func)
|
2012-08-15 00:39:10 -04:00
|
|
|
{
|
2012-08-22 01:12:31 -04:00
|
|
|
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);
|
2012-08-15 00:39:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2012-08-16 07:41:24 -04:00
|
|
|
clear_trace_func_i(st_data_t key, st_data_t val, st_data_t flag)
|
2012-08-15 00:39:10 -04:00
|
|
|
{
|
2012-08-16 07:41:24 -04:00
|
|
|
rb_thread_t *th;
|
|
|
|
GetThreadPtr((VALUE)key, th);
|
2012-08-22 01:12:31 -04:00
|
|
|
rb_threadptr_remove_event_hook(th, 0, Qundef);
|
2012-08-16 07:41:24 -04:00
|
|
|
return ST_CONTINUE;
|
2012-08-15 00:39:10 -04:00
|
|
|
}
|
|
|
|
|
2012-08-16 07:41:24 -04:00
|
|
|
void
|
|
|
|
rb_clear_trace_func(void)
|
2012-08-15 00:39:10 -04:00
|
|
|
{
|
2012-08-16 07:41:24 -04:00
|
|
|
st_foreach(GET_VM()->living_threads, clear_trace_func_i, (st_data_t) 0);
|
|
|
|
rb_remove_event_hook(0);
|
2012-08-15 00:39:10 -04:00
|
|
|
}
|
|
|
|
|
2012-08-16 07:41:24 -04:00
|
|
|
/* invoke hooks */
|
|
|
|
|
|
|
|
static void
|
|
|
|
clean_hooks(rb_hook_list_t *list)
|
2012-08-15 00:39:10 -04:00
|
|
|
{
|
2012-08-31 01:03:47 -04:00
|
|
|
rb_event_hook_t *hook, **nextp = &list->hooks;
|
2012-08-16 07:41:24 -04:00
|
|
|
|
|
|
|
list->events = 0;
|
|
|
|
list->need_clean = 0;
|
|
|
|
|
2012-08-31 01:03:47 -04:00
|
|
|
while ((hook = *nextp) != 0) {
|
2012-11-20 07:57:49 -05:00
|
|
|
if (hook->hook_flags & RUBY_EVENT_HOOK_FLAG_DELETED) {
|
2012-08-31 01:03:47 -04:00
|
|
|
*nextp = hook->next;
|
2012-08-16 07:41:24 -04:00
|
|
|
recalc_remove_ruby_vm_event_flags(hook->events);
|
|
|
|
xfree(hook);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
list->events |= hook->events; /* update active events */
|
2012-08-31 01:03:47 -04:00
|
|
|
nextp = &hook->next;
|
2012-08-16 07:41:24 -04:00
|
|
|
}
|
2012-08-15 00:39:10 -04:00
|
|
|
}
|
2012-08-16 07:41:24 -04:00
|
|
|
}
|
|
|
|
|
2012-08-22 01:12:31 -04:00
|
|
|
static int
|
2012-10-21 06:14:26 -04:00
|
|
|
exec_hooks(rb_thread_t *th, rb_hook_list_t *list, const rb_trace_arg_t *trace_arg, int can_clean_hooks)
|
2012-08-16 07:41:24 -04:00
|
|
|
{
|
2012-12-18 21:25:40 -05:00
|
|
|
int state;
|
2012-08-16 07:41:24 -04:00
|
|
|
volatile int raised;
|
|
|
|
|
2012-10-21 06:14:26 -04:00
|
|
|
if (UNLIKELY(list->need_clean > 0) && can_clean_hooks) {
|
2012-08-16 07:41:24 -04:00
|
|
|
clean_hooks(list);
|
2012-08-15 00:39:10 -04:00
|
|
|
}
|
|
|
|
|
2012-08-16 07:41:24 -04:00
|
|
|
raised = rb_threadptr_reset_raised(th);
|
|
|
|
|
2012-11-20 07:57:49 -05:00
|
|
|
/* TODO: Support !RUBY_EVENT_HOOK_FLAG_SAFE hooks */
|
2012-08-16 07:41:24 -04:00
|
|
|
|
|
|
|
TH_PUSH_TAG(th);
|
|
|
|
if ((state = TH_EXEC_TAG()) == 0) {
|
2012-08-30 03:20:05 -04:00
|
|
|
rb_event_hook_t *hook;
|
|
|
|
|
|
|
|
for (hook = list->hooks; hook; hook = hook->next) {
|
2013-05-26 20:21:02 -04:00
|
|
|
if (!(hook->hook_flags & RUBY_EVENT_HOOK_FLAG_DELETED) && (trace_arg->event & hook->events)) {
|
2012-11-20 07:57:49 -05:00
|
|
|
if (!(hook->hook_flags & RUBY_EVENT_HOOK_FLAG_RAW_ARG)) {
|
2012-08-22 01:12:31 -04:00
|
|
|
(*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);
|
|
|
|
}
|
2012-08-16 07:41:24 -04:00
|
|
|
}
|
|
|
|
}
|
2012-08-15 00:39:10 -04:00
|
|
|
}
|
2012-08-16 07:41:24 -04:00
|
|
|
TH_POP_TAG();
|
2012-08-15 00:39:10 -04:00
|
|
|
|
2012-08-16 07:41:24 -04:00
|
|
|
if (raised) {
|
|
|
|
rb_threadptr_set_raised(th);
|
|
|
|
}
|
2012-08-15 00:39:10 -04:00
|
|
|
|
2012-08-16 07:41:24 -04:00
|
|
|
return state;
|
2012-08-15 00:39:10 -04:00
|
|
|
}
|
|
|
|
|
2012-12-25 08:24:17 -05:00
|
|
|
static void
|
|
|
|
rb_threadptr_exec_event_hooks_orig(rb_trace_arg_t *trace_arg, int pop_p)
|
2012-08-16 07:41:24 -04:00
|
|
|
{
|
2012-12-21 04:33:44 -05:00
|
|
|
rb_thread_t *th = trace_arg->th;
|
|
|
|
if (th->trace_arg == 0 &&
|
|
|
|
trace_arg->self != rb_mRubyVMFrozenCore /* skip special methods. TODO: remove it. */) {
|
2012-10-21 06:14:29 -04:00
|
|
|
const int vm_tracing = th->vm->trace_running;
|
2012-12-10 01:23:44 -05:00
|
|
|
const VALUE errinfo = th->errinfo;
|
|
|
|
const int outer_state = th->state;
|
2012-08-31 01:02:47 -04:00
|
|
|
int state = 0;
|
2012-08-16 07:41:24 -04:00
|
|
|
th->state = 0;
|
|
|
|
|
2012-12-19 03:54:57 -05:00
|
|
|
th->vm->trace_running++;
|
2012-12-21 04:33:44 -05:00
|
|
|
th->trace_arg = trace_arg;
|
2012-08-16 07:41:24 -04:00
|
|
|
{
|
|
|
|
rb_hook_list_t *list;
|
|
|
|
|
|
|
|
/* thread local traces */
|
|
|
|
list = &th->event_hooks;
|
2012-12-21 04:33:44 -05:00
|
|
|
if (list->events & trace_arg->event) {
|
|
|
|
state = exec_hooks(th, list, trace_arg, TRUE);
|
2012-08-16 07:41:24 -04:00
|
|
|
if (state) goto terminate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vm global traces */
|
|
|
|
list = &th->vm->event_hooks;
|
2012-12-21 04:33:44 -05:00
|
|
|
if (list->events & trace_arg->event) {
|
|
|
|
state = exec_hooks(th, list, trace_arg, !vm_tracing);
|
2012-08-16 07:41:24 -04:00
|
|
|
if (state) goto terminate;
|
|
|
|
}
|
2012-12-11 13:45:43 -05:00
|
|
|
th->errinfo = errinfo;
|
2012-08-16 07:41:24 -04:00
|
|
|
}
|
|
|
|
terminate:
|
2012-12-21 04:33:44 -05:00
|
|
|
th->trace_arg = 0;
|
2012-12-19 03:54:57 -05:00
|
|
|
th->vm->trace_running--;
|
2012-12-11 13:45:43 -05:00
|
|
|
|
|
|
|
if (state) {
|
2013-01-07 01:24:46 -05:00
|
|
|
if (pop_p) {
|
|
|
|
if (VM_FRAME_TYPE_FINISH_P(th->cfp)) {
|
|
|
|
th->tag = th->tag->prev;
|
|
|
|
}
|
|
|
|
th->cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp);
|
|
|
|
}
|
2012-12-11 13:45:43 -05:00
|
|
|
TH_JUMP_TAG(th, state);
|
|
|
|
}
|
2012-08-16 07:41:24 -04:00
|
|
|
th->state = outer_state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-25 08:24:17 -05:00
|
|
|
void
|
|
|
|
rb_threadptr_exec_event_hooks_and_pop_frame(rb_trace_arg_t *trace_arg)
|
|
|
|
{
|
|
|
|
rb_threadptr_exec_event_hooks_orig(trace_arg, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_threadptr_exec_event_hooks(rb_trace_arg_t *trace_arg)
|
|
|
|
{
|
|
|
|
rb_threadptr_exec_event_hooks_orig(trace_arg, 0);
|
|
|
|
}
|
|
|
|
|
2012-08-16 07:41:24 -04:00
|
|
|
VALUE
|
|
|
|
rb_suppress_tracing(VALUE (*func)(VALUE), VALUE arg)
|
2012-08-15 00:39:10 -04:00
|
|
|
{
|
2012-08-16 07:41:24 -04:00
|
|
|
volatile int raised;
|
|
|
|
volatile int outer_state;
|
|
|
|
VALUE result = Qnil;
|
|
|
|
rb_thread_t *th = GET_THREAD();
|
|
|
|
int state;
|
2012-12-21 04:33:44 -05:00
|
|
|
const int tracing = th->trace_arg ? 1 : 0;
|
|
|
|
rb_trace_arg_t dummy_trace_arg;
|
|
|
|
|
|
|
|
if (!tracing) th->vm->trace_running++;
|
|
|
|
if (!th->trace_arg) th->trace_arg = &dummy_trace_arg;
|
2012-08-16 07:41:24 -04:00
|
|
|
|
|
|
|
raised = rb_threadptr_reset_raised(th);
|
|
|
|
outer_state = th->state;
|
|
|
|
th->state = 0;
|
|
|
|
|
2012-10-23 16:42:45 -04:00
|
|
|
TH_PUSH_TAG(th);
|
|
|
|
if ((state = TH_EXEC_TAG()) == 0) {
|
2012-08-16 07:41:24 -04:00
|
|
|
result = (*func)(arg);
|
|
|
|
}
|
2012-10-23 16:42:45 -04:00
|
|
|
TH_POP_TAG();
|
2012-08-16 07:41:24 -04:00
|
|
|
|
|
|
|
if (raised) {
|
|
|
|
rb_threadptr_set_raised(th);
|
|
|
|
}
|
2012-12-21 04:33:44 -05:00
|
|
|
|
|
|
|
if (th->trace_arg == &dummy_trace_arg) th->trace_arg = 0;
|
|
|
|
if (!tracing) th->vm->trace_running--;
|
2012-08-16 07:41:24 -04:00
|
|
|
|
|
|
|
if (state) {
|
|
|
|
JUMP_TAG(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
th->state = outer_state;
|
|
|
|
return result;
|
2012-08-15 00:39:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void call_trace_func(rb_event_flag_t, VALUE data, VALUE self, ID id, VALUE klass);
|
|
|
|
|
|
|
|
/* (2-1) set_trace_func (old API) */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* set_trace_func(proc) -> proc
|
|
|
|
* set_trace_func(nil) -> nil
|
|
|
|
*
|
|
|
|
* Establishes _proc_ as the handler for tracing, or disables
|
2012-11-30 01:15:59 -05:00
|
|
|
* tracing if the parameter is +nil+.
|
|
|
|
*
|
2013-02-08 23:14:09 -05:00
|
|
|
* *Note:* this method is obsolete, please use TracePoint instead.
|
|
|
|
*
|
2012-11-30 01:15:59 -05:00
|
|
|
* _proc_ takes up to six parameters:
|
|
|
|
*
|
|
|
|
* * an event name
|
|
|
|
* * a filename
|
|
|
|
* * a line number
|
|
|
|
* * an object id
|
|
|
|
* * a binding
|
|
|
|
* * the name of a class
|
|
|
|
*
|
|
|
|
* _proc_ is invoked whenever an event occurs.
|
|
|
|
*
|
|
|
|
* Events are:
|
|
|
|
*
|
|
|
|
* +c-call+:: call a C-language routine
|
|
|
|
* +c-return+:: return from a C-language routine
|
|
|
|
* +call+:: call a Ruby method
|
|
|
|
* +class+:: start a class or module definition),
|
|
|
|
* +end+:: finish a class or module definition),
|
|
|
|
* +line+:: execute code on a new line
|
|
|
|
* +raise+:: raise an exception
|
|
|
|
* +return+:: return from a Ruby method
|
|
|
|
*
|
|
|
|
* Tracing is disabled within the context of _proc_.
|
2012-08-15 00:39:10 -04:00
|
|
|
*
|
|
|
|
* class Test
|
|
|
|
* def test
|
|
|
|
* a = 1
|
|
|
|
* b = 2
|
|
|
|
* end
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* set_trace_func proc { |event, file, line, id, binding, classname|
|
|
|
|
* printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname
|
|
|
|
* }
|
|
|
|
* t = Test.new
|
|
|
|
* t.test
|
|
|
|
*
|
|
|
|
* line prog.rb:11 false
|
|
|
|
* c-call prog.rb:11 new Class
|
|
|
|
* c-call prog.rb:11 initialize Object
|
|
|
|
* c-return prog.rb:11 initialize Object
|
|
|
|
* c-return prog.rb:11 new Class
|
|
|
|
* line prog.rb:12 false
|
|
|
|
* call prog.rb:2 test Test
|
|
|
|
* line prog.rb:3 test Test
|
|
|
|
* line prog.rb:4 test Test
|
|
|
|
* return prog.rb:4 test Test
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
set_trace_func(VALUE obj, VALUE trace)
|
|
|
|
{
|
2013-01-29 02:50:27 -05:00
|
|
|
|
2012-08-15 00:39:10 -04:00
|
|
|
rb_remove_event_hook(call_trace_func);
|
|
|
|
|
|
|
|
if (NIL_P(trace)) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rb_obj_is_proc(trace)) {
|
|
|
|
rb_raise(rb_eTypeError, "trace_func needs to be Proc");
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_add_event_hook(call_trace_func, RUBY_EVENT_ALL, trace);
|
|
|
|
return trace;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
thread_add_trace_func(rb_thread_t *th, VALUE trace)
|
|
|
|
{
|
|
|
|
if (!rb_obj_is_proc(trace)) {
|
|
|
|
rb_raise(rb_eTypeError, "trace_func needs to be Proc");
|
|
|
|
}
|
|
|
|
|
2012-11-20 07:57:49 -05:00
|
|
|
rb_threadptr_add_event_hook(th, call_trace_func, RUBY_EVENT_ALL, trace, RUBY_EVENT_HOOK_FLAG_SAFE);
|
2012-08-15 00:39:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* thr.add_trace_func(proc) -> proc
|
|
|
|
*
|
|
|
|
* Adds _proc_ as a handler for tracing.
|
2013-02-08 23:14:09 -05:00
|
|
|
*
|
|
|
|
* See Thread#set_trace_func and Kernel#set_trace_func.
|
2012-08-15 00:39:10 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
thread_add_trace_func_m(VALUE obj, VALUE trace)
|
|
|
|
{
|
|
|
|
rb_thread_t *th;
|
2013-01-29 02:50:27 -05:00
|
|
|
|
2012-08-15 00:39:10 -04:00
|
|
|
GetThreadPtr(obj, th);
|
|
|
|
thread_add_trace_func(th, trace);
|
|
|
|
return trace;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* thr.set_trace_func(proc) -> proc
|
|
|
|
* thr.set_trace_func(nil) -> nil
|
|
|
|
*
|
|
|
|
* Establishes _proc_ on _thr_ as the handler for tracing, or
|
|
|
|
* disables tracing if the parameter is +nil+.
|
2013-02-08 23:14:09 -05:00
|
|
|
*
|
|
|
|
* See Kernel#set_trace_func.
|
2012-08-15 00:39:10 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
thread_set_trace_func_m(VALUE obj, VALUE trace)
|
|
|
|
{
|
|
|
|
rb_thread_t *th;
|
2013-01-29 02:50:27 -05:00
|
|
|
|
2012-08-15 00:39:10 -04:00
|
|
|
GetThreadPtr(obj, th);
|
2012-08-22 01:12:31 -04:00
|
|
|
rb_threadptr_remove_event_hook(th, call_trace_func, Qundef);
|
2012-08-15 00:39:10 -04:00
|
|
|
|
|
|
|
if (NIL_P(trace)) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
2012-08-16 07:41:24 -04:00
|
|
|
|
2012-08-15 00:39:10 -04:00
|
|
|
thread_add_trace_func(th, trace);
|
|
|
|
return trace;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
get_event_name(rb_event_flag_t event)
|
|
|
|
{
|
|
|
|
switch (event) {
|
2012-08-22 01:12:31 -04:00
|
|
|
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";
|
2012-08-15 00:39:10 -04:00
|
|
|
default:
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-22 01:12:31 -04:00
|
|
|
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);
|
2012-11-29 17:28:16 -05:00
|
|
|
C(b_call, B_CALL);
|
|
|
|
C(b_return, B_RETURN);
|
|
|
|
C(thread_begin, THREAD_BEGIN);
|
|
|
|
C(thread_end, THREAD_END);
|
2012-11-30 12:00:30 -05:00
|
|
|
C(specified_line, SPECIFIED_LINE);
|
2013-01-07 03:38:25 -05:00
|
|
|
case RUBY_EVENT_LINE | RUBY_EVENT_SPECIFIED_LINE: CONST_ID(id, "line"); return id;
|
2012-08-22 01:12:31 -04:00
|
|
|
#undef C
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-16 07:41:24 -04:00
|
|
|
static void
|
|
|
|
call_trace_func(rb_event_flag_t event, VALUE proc, VALUE self, ID id, VALUE klass)
|
2012-08-15 00:39:10 -04:00
|
|
|
{
|
|
|
|
const char *srcfile = rb_sourcefile();
|
2012-08-16 07:41:24 -04:00
|
|
|
VALUE eventname = rb_str_new2(get_event_name(event));
|
2012-08-15 00:39:10 -04:00
|
|
|
VALUE filename = srcfile ? rb_str_new2(srcfile) : Qnil;
|
|
|
|
VALUE argv[6];
|
|
|
|
int line = rb_sourceline();
|
2012-08-16 07:41:24 -04:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
2012-08-15 00:39:10 -04:00
|
|
|
|
2012-08-16 22:13:31 -04:00
|
|
|
if (!klass) {
|
2012-08-16 07:41:24 -04:00
|
|
|
rb_thread_method_id_and_class(th, &id, &klass);
|
2012-08-15 00:39:10 -04:00
|
|
|
}
|
2012-08-16 07:41:24 -04:00
|
|
|
|
2012-08-15 00:39:10 -04:00
|
|
|
if (klass) {
|
|
|
|
if (RB_TYPE_P(klass, T_ICLASS)) {
|
|
|
|
klass = RBASIC(klass)->klass;
|
|
|
|
}
|
|
|
|
else if (FL_TEST(klass, FL_SINGLETON)) {
|
2013-05-02 03:54:17 -04:00
|
|
|
klass = rb_ivar_get(klass, id__attached__);
|
2012-08-15 00:39:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argv[0] = eventname;
|
|
|
|
argv[1] = filename;
|
|
|
|
argv[2] = INT2FIX(line);
|
|
|
|
argv[3] = id ? ID2SYM(id) : Qnil;
|
2012-08-16 07:41:24 -04:00
|
|
|
argv[4] = (self && srcfile) ? rb_binding_new() : Qnil;
|
2012-08-15 00:39:10 -04:00
|
|
|
argv[5] = klass ? klass : Qnil;
|
|
|
|
|
2012-08-16 07:41:24 -04:00
|
|
|
rb_proc_call_with_block(proc, 6, argv, Qnil);
|
2012-08-15 00:39:10 -04:00
|
|
|
}
|
|
|
|
|
2012-08-22 01:12:31 -04:00
|
|
|
/* (2-2) TracePoint API */
|
2012-08-15 00:39:10 -04:00
|
|
|
|
2012-08-22 01:12:31 -04:00
|
|
|
static VALUE rb_cTracePoint;
|
|
|
|
|
|
|
|
typedef struct rb_tp_struct {
|
|
|
|
rb_event_flag_t events;
|
|
|
|
rb_thread_t *target_th;
|
2012-11-20 07:57:49 -05:00
|
|
|
void (*func)(VALUE tpval, void *data);
|
|
|
|
void *data;
|
2012-08-22 01:12:31 -04:00
|
|
|
VALUE proc;
|
|
|
|
int tracing;
|
2012-11-20 07:57:49 -05:00
|
|
|
VALUE self;
|
2012-08-22 01:12:31 -04:00
|
|
|
} 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);
|
2012-11-29 17:28:16 -05:00
|
|
|
C(b_call, B_CALL);
|
|
|
|
C(b_return, B_RETURN);
|
|
|
|
C(thread_begin, THREAD_BEGIN);
|
|
|
|
C(thread_end, THREAD_END);
|
2012-11-30 12:00:30 -05:00
|
|
|
C(specified_line, SPECIFIED_LINE);
|
2012-08-22 01:12:31 -04:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2012-12-21 04:33:44 -05:00
|
|
|
static rb_trace_arg_t *
|
|
|
|
get_trace_arg(void)
|
2012-08-22 01:12:31 -04:00
|
|
|
{
|
2012-12-21 04:33:44 -05:00
|
|
|
rb_trace_arg_t *trace_arg = GET_THREAD()->trace_arg;
|
|
|
|
if (trace_arg == 0) {
|
2012-08-22 01:12:31 -04:00
|
|
|
rb_raise(rb_eRuntimeError, "access from outside");
|
|
|
|
}
|
2012-12-21 04:33:44 -05:00
|
|
|
return trace_arg;
|
2012-08-22 01:12:31 -04:00
|
|
|
}
|
|
|
|
|
2012-11-29 01:43:31 -05:00
|
|
|
struct rb_trace_arg_struct *
|
|
|
|
rb_tracearg_from_tracepoint(VALUE tpval)
|
2012-08-22 01:12:31 -04:00
|
|
|
{
|
2012-12-21 04:33:44 -05:00
|
|
|
return get_trace_arg();
|
2012-11-29 01:43:31 -05:00
|
|
|
}
|
|
|
|
|
2013-05-26 20:21:02 -04:00
|
|
|
rb_event_flag_t
|
|
|
|
rb_tracearg_event_flag(rb_trace_arg_t *trace_arg)
|
|
|
|
{
|
|
|
|
return trace_arg->event;
|
|
|
|
}
|
|
|
|
|
2012-11-29 01:43:31 -05:00
|
|
|
VALUE
|
|
|
|
rb_tracearg_event(rb_trace_arg_t *trace_arg)
|
|
|
|
{
|
|
|
|
return ID2SYM(get_event_id(trace_arg->event));
|
2012-08-22 01:12:31 -04:00
|
|
|
}
|
|
|
|
|
2012-11-26 18:01:45 -05:00
|
|
|
static void
|
2012-11-26 18:25:21 -05:00
|
|
|
fill_path_and_lineno(rb_trace_arg_t *trace_arg)
|
2012-11-26 18:01:45 -05:00
|
|
|
{
|
2012-11-26 18:25:21 -05:00
|
|
|
if (trace_arg->path == Qundef) {
|
2012-11-26 18:01:45 -05:00
|
|
|
rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(trace_arg->th, trace_arg->cfp);
|
|
|
|
|
|
|
|
if (cfp) {
|
2012-11-26 18:25:21 -05:00
|
|
|
trace_arg->path = cfp->iseq->location.path;
|
|
|
|
trace_arg->lineno = rb_vm_get_sourceline(cfp);
|
2012-11-26 18:01:45 -05:00
|
|
|
}
|
|
|
|
else {
|
2012-11-26 18:25:21 -05:00
|
|
|
trace_arg->path = Qnil;
|
|
|
|
trace_arg->lineno = 0;
|
2012-11-26 18:01:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-20 07:57:49 -05:00
|
|
|
VALUE
|
2012-11-29 01:43:31 -05:00
|
|
|
rb_tracearg_lineno(rb_trace_arg_t *trace_arg)
|
2012-08-22 01:12:31 -04:00
|
|
|
{
|
2012-11-29 01:43:31 -05:00
|
|
|
fill_path_and_lineno(trace_arg);
|
|
|
|
return INT2FIX(trace_arg->lineno);
|
2012-08-22 01:12:31 -04:00
|
|
|
}
|
2012-11-20 07:57:49 -05:00
|
|
|
VALUE
|
2012-11-29 01:43:31 -05:00
|
|
|
rb_tracearg_path(rb_trace_arg_t *trace_arg)
|
2012-08-22 01:12:31 -04:00
|
|
|
{
|
2012-11-29 01:43:31 -05:00
|
|
|
fill_path_and_lineno(trace_arg);
|
|
|
|
return trace_arg->path;
|
2012-08-22 01:12:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fill_id_and_klass(rb_trace_arg_t *trace_arg)
|
|
|
|
{
|
2012-11-26 18:01:45 -05:00
|
|
|
if (!trace_arg->klass_solved) {
|
|
|
|
if (!trace_arg->klass) {
|
|
|
|
rb_vm_control_frame_id_and_class(trace_arg->cfp, &trace_arg->id, &trace_arg->klass);
|
|
|
|
}
|
2012-08-22 01:12:31 -04:00
|
|
|
|
2012-11-26 18:01:45 -05:00
|
|
|
if (trace_arg->klass) {
|
|
|
|
if (RB_TYPE_P(trace_arg->klass, T_ICLASS)) {
|
|
|
|
trace_arg->klass = RBASIC(trace_arg->klass)->klass;
|
|
|
|
}
|
2012-08-22 01:12:31 -04:00
|
|
|
}
|
2012-11-26 18:01:45 -05:00
|
|
|
else {
|
|
|
|
trace_arg->klass = Qnil;
|
2012-08-22 01:12:31 -04:00
|
|
|
}
|
2012-11-26 18:01:45 -05:00
|
|
|
|
|
|
|
trace_arg->klass_solved = 1;
|
2012-08-22 01:12:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-20 07:57:49 -05:00
|
|
|
VALUE
|
2012-11-29 01:43:31 -05:00
|
|
|
rb_tracearg_method_id(rb_trace_arg_t *trace_arg)
|
2012-08-22 01:12:31 -04:00
|
|
|
{
|
2012-11-29 01:43:31 -05:00
|
|
|
fill_id_and_klass(trace_arg);
|
|
|
|
return trace_arg->id ? ID2SYM(trace_arg->id) : Qnil;
|
2012-08-22 01:12:31 -04:00
|
|
|
}
|
|
|
|
|
2012-11-20 07:57:49 -05:00
|
|
|
VALUE
|
2012-11-29 01:43:31 -05:00
|
|
|
rb_tracearg_defined_class(rb_trace_arg_t *trace_arg)
|
2012-08-22 01:12:31 -04:00
|
|
|
{
|
2012-11-29 01:43:31 -05:00
|
|
|
fill_id_and_klass(trace_arg);
|
|
|
|
return trace_arg->klass;
|
2012-08-22 01:12:31 -04:00
|
|
|
}
|
|
|
|
|
2012-11-20 07:57:49 -05:00
|
|
|
VALUE
|
2012-11-29 01:43:31 -05:00
|
|
|
rb_tracearg_binding(rb_trace_arg_t *trace_arg)
|
2012-08-22 01:12:31 -04:00
|
|
|
{
|
|
|
|
rb_control_frame_t *cfp;
|
2013-02-05 01:04:59 -05:00
|
|
|
cfp = rb_vm_get_binding_creatable_next_cfp(trace_arg->th, trace_arg->cfp);
|
|
|
|
|
2012-08-22 01:12:31 -04:00
|
|
|
if (cfp) {
|
2012-11-29 01:43:31 -05:00
|
|
|
return rb_binding_new_with_cfp(trace_arg->th, cfp);
|
2012-08-22 01:12:31 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-20 07:57:49 -05:00
|
|
|
VALUE
|
2012-11-29 01:43:31 -05:00
|
|
|
rb_tracearg_self(rb_trace_arg_t *trace_arg)
|
2012-08-22 01:12:31 -04:00
|
|
|
{
|
2012-11-29 01:43:31 -05:00
|
|
|
return trace_arg->self;
|
2012-08-22 01:12:31 -04:00
|
|
|
}
|
|
|
|
|
2012-11-20 07:57:49 -05:00
|
|
|
VALUE
|
2012-11-29 01:43:31 -05:00
|
|
|
rb_tracearg_return_value(rb_trace_arg_t *trace_arg)
|
2012-11-20 04:48:24 -05:00
|
|
|
{
|
2012-11-29 17:28:16 -05:00
|
|
|
if (trace_arg->event & (RUBY_EVENT_RETURN | RUBY_EVENT_C_RETURN | RUBY_EVENT_B_RETURN)) {
|
2012-11-20 04:48:24 -05:00
|
|
|
/* ok */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_raise(rb_eRuntimeError, "not supported by this event");
|
|
|
|
}
|
2012-11-29 01:43:31 -05:00
|
|
|
if (trace_arg->data == Qundef) {
|
2012-11-23 23:12:30 -05:00
|
|
|
rb_bug("tp_attr_return_value_m: unreachable");
|
|
|
|
}
|
2012-11-29 01:43:31 -05:00
|
|
|
return trace_arg->data;
|
2012-11-20 04:48:24 -05:00
|
|
|
}
|
|
|
|
|
2012-11-20 07:57:49 -05:00
|
|
|
VALUE
|
2012-11-29 01:43:31 -05:00
|
|
|
rb_tracearg_raised_exception(rb_trace_arg_t *trace_arg)
|
2012-11-20 04:48:24 -05:00
|
|
|
{
|
2012-11-29 01:43:31 -05:00
|
|
|
if (trace_arg->event & (RUBY_EVENT_RAISE)) {
|
2012-11-20 04:48:24 -05:00
|
|
|
/* ok */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_raise(rb_eRuntimeError, "not supported by this event");
|
|
|
|
}
|
2012-11-29 01:43:31 -05:00
|
|
|
if (trace_arg->data == Qundef) {
|
2013-05-26 20:21:02 -04:00
|
|
|
rb_bug("tp_attr_raised_exception_m: unreachable");
|
|
|
|
}
|
|
|
|
return trace_arg->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_tracearg_object(rb_trace_arg_t *trace_arg)
|
|
|
|
{
|
2013-05-27 06:28:25 -04:00
|
|
|
if (trace_arg->event & (RUBY_INTERNAL_EVENT_NEWOBJ | RUBY_INTERNAL_EVENT_FREEOBJ)) {
|
2013-05-26 20:21:02 -04:00
|
|
|
/* ok */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_raise(rb_eRuntimeError, "not supported by this event");
|
|
|
|
}
|
|
|
|
if (trace_arg->data == Qundef) {
|
2012-11-23 23:12:30 -05:00
|
|
|
rb_bug("tp_attr_raised_exception_m: unreachable");
|
|
|
|
}
|
2012-11-29 01:43:31 -05:00
|
|
|
return trace_arg->data;
|
|
|
|
}
|
|
|
|
|
2012-11-30 04:25:24 -05:00
|
|
|
/*
|
|
|
|
* Type of event
|
|
|
|
*
|
2013-02-08 23:14:09 -05:00
|
|
|
* See TracePoint@Events for more information.
|
2012-11-30 04:25:24 -05:00
|
|
|
*/
|
2012-11-29 01:43:31 -05:00
|
|
|
static VALUE
|
|
|
|
tracepoint_attr_event(VALUE tpval)
|
|
|
|
{
|
2012-12-21 04:33:44 -05:00
|
|
|
return rb_tracearg_event(get_trace_arg());
|
2012-11-29 01:43:31 -05:00
|
|
|
}
|
|
|
|
|
2012-11-30 04:25:24 -05:00
|
|
|
/*
|
|
|
|
* Line number of the event
|
|
|
|
*/
|
2012-11-29 01:43:31 -05:00
|
|
|
static VALUE
|
|
|
|
tracepoint_attr_lineno(VALUE tpval)
|
|
|
|
{
|
2012-12-21 04:33:44 -05:00
|
|
|
return rb_tracearg_lineno(get_trace_arg());
|
2012-11-29 01:43:31 -05:00
|
|
|
}
|
|
|
|
|
2012-11-30 04:25:24 -05:00
|
|
|
/*
|
|
|
|
* Path of the file being run
|
|
|
|
*/
|
2012-11-29 01:43:31 -05:00
|
|
|
static VALUE
|
|
|
|
tracepoint_attr_path(VALUE tpval)
|
|
|
|
{
|
2012-12-21 04:33:44 -05:00
|
|
|
return rb_tracearg_path(get_trace_arg());
|
2012-11-29 01:43:31 -05:00
|
|
|
}
|
|
|
|
|
2012-11-30 04:25:24 -05:00
|
|
|
/*
|
|
|
|
* Return the name of the method being called
|
|
|
|
*/
|
2012-11-29 01:43:31 -05:00
|
|
|
static VALUE
|
|
|
|
tracepoint_attr_method_id(VALUE tpval)
|
|
|
|
{
|
2012-12-21 04:33:44 -05:00
|
|
|
return rb_tracearg_method_id(get_trace_arg());
|
2012-11-29 01:43:31 -05:00
|
|
|
}
|
|
|
|
|
2012-11-30 04:25:24 -05:00
|
|
|
/*
|
2012-12-17 23:18:06 -05:00
|
|
|
* Return class or module of the method being called.
|
2012-12-17 15:28:51 -05:00
|
|
|
*
|
2012-12-17 23:18:06 -05:00
|
|
|
* class C; def foo; end; end
|
|
|
|
* trace = TracePoint.new(:call) do |tp|
|
|
|
|
* p tp.defined_class #=> C
|
|
|
|
* end.enable do
|
|
|
|
* C.new.foo
|
|
|
|
* end
|
2012-12-17 15:28:51 -05:00
|
|
|
*
|
2012-12-17 23:18:06 -05:00
|
|
|
* If method is defined by a module, then that module is returned.
|
2012-12-17 15:28:51 -05:00
|
|
|
*
|
2012-12-17 23:18:06 -05:00
|
|
|
* module M; def foo; end; end
|
|
|
|
* class C; include M; end;
|
|
|
|
* trace = TracePoint.new(:call) do |tp|
|
|
|
|
* p tp.defined_class #=> M
|
|
|
|
* end.enable do
|
|
|
|
* C.new.foo
|
|
|
|
* end
|
2012-12-17 15:28:51 -05:00
|
|
|
*
|
2012-12-17 23:18:06 -05:00
|
|
|
* <b>Note:</b> #defined_class returns singleton class.
|
|
|
|
*
|
|
|
|
* 6th block parameter of Kernel#set_trace_func passes original class
|
|
|
|
* of attached by singleton class.
|
|
|
|
*
|
|
|
|
* <b>This is a difference between Kernel#set_trace_func and TracePoint.</b>
|
|
|
|
*
|
|
|
|
* class C; def self.foo; end; end
|
|
|
|
* trace = TracePoint.new(:call) do |tp|
|
|
|
|
* p tp.defined_class #=> #<Class:C>
|
|
|
|
* end.enable do
|
|
|
|
* C.foo
|
|
|
|
* end
|
2012-11-30 04:25:24 -05:00
|
|
|
*/
|
2012-11-29 01:43:31 -05:00
|
|
|
static VALUE
|
|
|
|
tracepoint_attr_defined_class(VALUE tpval)
|
|
|
|
{
|
2012-12-21 04:33:44 -05:00
|
|
|
return rb_tracearg_defined_class(get_trace_arg());
|
2012-11-29 01:43:31 -05:00
|
|
|
}
|
|
|
|
|
2012-11-30 04:25:24 -05:00
|
|
|
/*
|
|
|
|
* Return the generated binding object from event
|
|
|
|
*/
|
2012-11-29 01:43:31 -05:00
|
|
|
static VALUE
|
|
|
|
tracepoint_attr_binding(VALUE tpval)
|
|
|
|
{
|
2012-12-21 04:33:44 -05:00
|
|
|
return rb_tracearg_binding(get_trace_arg());
|
2012-11-29 01:43:31 -05:00
|
|
|
}
|
|
|
|
|
2012-11-30 04:25:24 -05:00
|
|
|
/*
|
|
|
|
* Return the trace object during event
|
|
|
|
*
|
|
|
|
* Same as TracePoint#binding:
|
|
|
|
* trace.binding.eval('self')
|
|
|
|
*/
|
2012-11-29 01:43:31 -05:00
|
|
|
static VALUE
|
|
|
|
tracepoint_attr_self(VALUE tpval)
|
|
|
|
{
|
2012-12-21 04:33:44 -05:00
|
|
|
return rb_tracearg_self(get_trace_arg());
|
2012-11-29 01:43:31 -05:00
|
|
|
}
|
|
|
|
|
2012-11-30 04:25:24 -05:00
|
|
|
/*
|
2012-11-30 05:29:49 -05:00
|
|
|
* Return value from +:return+, +c_return+, and +b_return+ event
|
2012-11-30 04:25:24 -05:00
|
|
|
*/
|
2012-11-29 01:43:31 -05:00
|
|
|
static VALUE
|
|
|
|
tracepoint_attr_return_value(VALUE tpval)
|
|
|
|
{
|
2012-12-21 04:33:44 -05:00
|
|
|
return rb_tracearg_return_value(get_trace_arg());
|
2012-11-29 01:43:31 -05:00
|
|
|
}
|
|
|
|
|
2012-11-30 04:25:24 -05:00
|
|
|
/*
|
|
|
|
* Value from exception raised on the +:raise+ event
|
|
|
|
*/
|
2012-11-29 01:43:31 -05:00
|
|
|
static VALUE
|
|
|
|
tracepoint_attr_raised_exception(VALUE tpval)
|
|
|
|
{
|
2012-12-21 04:33:44 -05:00
|
|
|
return rb_tracearg_raised_exception(get_trace_arg());
|
2012-11-20 04:48:24 -05:00
|
|
|
}
|
|
|
|
|
2012-08-22 01:12:31 -04:00
|
|
|
static void
|
|
|
|
tp_call_trace(VALUE tpval, rb_trace_arg_t *trace_arg)
|
|
|
|
{
|
|
|
|
rb_tp_t *tp = tpptr(tpval);
|
|
|
|
|
2012-12-21 04:33:44 -05:00
|
|
|
if (tp->func) {
|
|
|
|
(*tp->func)(tpval, tp->data);
|
2012-08-22 01:12:31 -04:00
|
|
|
}
|
2012-12-21 04:33:44 -05:00
|
|
|
else {
|
|
|
|
rb_proc_call_with_block((VALUE)tp->proc, 1, &tpval, Qnil);
|
2012-08-22 01:12:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-20 07:57:49 -05:00
|
|
|
VALUE
|
|
|
|
rb_tracepoint_enable(VALUE tpval)
|
2012-08-22 01:12:31 -04:00
|
|
|
{
|
2013-01-29 02:51:00 -05:00
|
|
|
rb_tp_t *tp;
|
|
|
|
|
|
|
|
tp = tpptr(tpval);
|
2012-08-22 01:12:31 -04:00
|
|
|
|
2012-11-20 06:05:20 -05:00
|
|
|
if (tp->target_th) {
|
2012-11-29 01:43:31 -05:00
|
|
|
rb_thread_add_event_hook2(tp->target_th->self, (rb_event_hook_func_t)tp_call_trace, tp->events, tpval,
|
|
|
|
RUBY_EVENT_HOOK_FLAG_SAFE | RUBY_EVENT_HOOK_FLAG_RAW_ARG);
|
2012-08-22 01:12:31 -04:00
|
|
|
}
|
|
|
|
else {
|
2012-11-29 01:43:31 -05:00
|
|
|
rb_add_event_hook2((rb_event_hook_func_t)tp_call_trace, tp->events, tpval,
|
|
|
|
RUBY_EVENT_HOOK_FLAG_SAFE | RUBY_EVENT_HOOK_FLAG_RAW_ARG);
|
2012-08-22 01:12:31 -04:00
|
|
|
}
|
2012-11-20 06:05:20 -05:00
|
|
|
tp->tracing = 1;
|
|
|
|
return Qundef;
|
|
|
|
}
|
2012-08-22 01:12:31 -04:00
|
|
|
|
2012-11-20 07:57:49 -05:00
|
|
|
VALUE
|
|
|
|
rb_tracepoint_disable(VALUE tpval)
|
2012-11-20 06:05:20 -05:00
|
|
|
{
|
2013-01-29 02:51:00 -05:00
|
|
|
rb_tp_t *tp;
|
|
|
|
|
|
|
|
tp = tpptr(tpval);
|
2012-11-20 06:05:20 -05:00
|
|
|
|
|
|
|
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 Qundef;
|
2012-08-22 01:12:31 -04:00
|
|
|
}
|
|
|
|
|
2012-11-30 04:25:24 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2012-12-05 22:13:50 -05:00
|
|
|
* trace.enable -> true or false
|
2012-11-30 05:29:49 -05:00
|
|
|
* trace.enable { block } -> obj
|
2012-11-30 04:25:24 -05:00
|
|
|
*
|
|
|
|
* Activates the trace
|
|
|
|
*
|
2012-12-05 22:13:50 -05:00
|
|
|
* Return true if trace was enabled.
|
|
|
|
* Return false if trace was disabled.
|
2012-11-30 04:25:24 -05:00
|
|
|
*
|
|
|
|
* trace.enabled? #=> false
|
2012-12-05 22:13:50 -05:00
|
|
|
* trace.enable #=> false (previous state)
|
|
|
|
* # trace is enabled
|
2012-11-30 04:25:24 -05:00
|
|
|
* trace.enabled? #=> true
|
2012-12-05 22:13:50 -05:00
|
|
|
* trace.enable #=> true (previous state)
|
|
|
|
* # trace is still enabled
|
2012-11-30 04:25:24 -05:00
|
|
|
*
|
2012-11-30 05:29:49 -05:00
|
|
|
* If a block is given, the trace will only be enabled within the scope of the
|
2012-12-05 22:13:50 -05:00
|
|
|
* block.
|
2012-11-30 05:29:49 -05:00
|
|
|
*
|
|
|
|
* trace.enabled?
|
|
|
|
* #=> false
|
|
|
|
*
|
|
|
|
* trace.enable do
|
|
|
|
* trace.enabled?
|
|
|
|
* # only enabled for this block
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* trace.enabled?
|
|
|
|
* #=> false
|
|
|
|
*
|
2012-12-05 22:13:50 -05:00
|
|
|
* Note: You cannot access event hooks within the block.
|
|
|
|
*
|
2012-11-30 05:29:49 -05:00
|
|
|
* trace.enable { p tp.lineno }
|
|
|
|
* #=> RuntimeError: access from outside
|
|
|
|
*
|
2012-11-30 04:25:24 -05:00
|
|
|
*/
|
2012-08-22 01:12:31 -04:00
|
|
|
static VALUE
|
2012-11-20 07:57:49 -05:00
|
|
|
tracepoint_enable_m(VALUE tpval)
|
2012-11-20 06:05:20 -05:00
|
|
|
{
|
|
|
|
rb_tp_t *tp = tpptr(tpval);
|
2012-12-05 22:13:50 -05:00
|
|
|
int previous_tracing = tp->tracing;
|
2012-11-20 07:57:49 -05:00
|
|
|
rb_tracepoint_enable(tpval);
|
2012-12-05 22:13:50 -05:00
|
|
|
|
2012-11-20 06:05:20 -05:00
|
|
|
if (rb_block_given_p()) {
|
2012-12-05 22:13:50 -05:00
|
|
|
return rb_ensure(rb_yield, Qnil,
|
|
|
|
previous_tracing ? rb_tracepoint_enable : rb_tracepoint_disable,
|
|
|
|
tpval);
|
2012-11-20 06:05:20 -05:00
|
|
|
}
|
|
|
|
else {
|
2012-12-05 22:13:50 -05:00
|
|
|
return previous_tracing ? Qtrue : Qfalse;
|
2012-11-20 06:05:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-30 04:25:24 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2012-12-05 23:57:20 -05:00
|
|
|
* trace.disable -> true or false
|
2012-11-30 05:29:49 -05:00
|
|
|
* trace.disable { block } -> obj
|
2012-11-30 04:25:24 -05:00
|
|
|
*
|
|
|
|
* Deactivates the trace
|
|
|
|
*
|
2012-12-05 22:13:50 -05:00
|
|
|
* Return true if trace was enabled.
|
|
|
|
* Return false if trace was disabled.
|
2012-11-30 04:25:24 -05:00
|
|
|
*
|
|
|
|
* trace.enabled? #=> true
|
2012-12-05 22:13:50 -05:00
|
|
|
* trace.disable #=> false (previous status)
|
2012-11-30 04:25:24 -05:00
|
|
|
* trace.enabled? #=> false
|
2012-12-05 22:13:50 -05:00
|
|
|
* trace.disable #=> false
|
2012-11-30 04:25:24 -05:00
|
|
|
*
|
2012-11-30 05:29:49 -05:00
|
|
|
* If a block is given, the trace will only be disable within the scope of the
|
2012-12-05 22:13:50 -05:00
|
|
|
* block.
|
2012-11-30 05:29:49 -05:00
|
|
|
*
|
|
|
|
* trace.enabled?
|
|
|
|
* #=> true
|
|
|
|
*
|
|
|
|
* trace.disable do
|
|
|
|
* trace.enabled?
|
|
|
|
* # only disabled for this block
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* trace.enabled?
|
|
|
|
* #=> true
|
|
|
|
*
|
2012-12-05 22:13:50 -05:00
|
|
|
* Note: You cannot access event hooks within the block.
|
2012-11-30 05:29:49 -05:00
|
|
|
*
|
2012-12-05 22:13:50 -05:00
|
|
|
* trace.disable { p tp.lineno }
|
|
|
|
* #=> RuntimeError: access from outside
|
2012-11-30 04:25:24 -05:00
|
|
|
*/
|
2012-11-20 06:05:20 -05:00
|
|
|
static VALUE
|
2012-11-20 07:57:49 -05:00
|
|
|
tracepoint_disable_m(VALUE tpval)
|
2012-08-22 01:12:31 -04:00
|
|
|
{
|
|
|
|
rb_tp_t *tp = tpptr(tpval);
|
2012-12-05 22:13:50 -05:00
|
|
|
int previous_tracing = tp->tracing;
|
2012-11-20 07:57:49 -05:00
|
|
|
rb_tracepoint_disable(tpval);
|
2012-12-05 22:13:50 -05:00
|
|
|
|
2012-11-20 06:05:20 -05:00
|
|
|
if (rb_block_given_p()) {
|
2012-12-05 22:13:50 -05:00
|
|
|
return rb_ensure(rb_yield, Qnil,
|
|
|
|
previous_tracing ? rb_tracepoint_enable : rb_tracepoint_disable,
|
|
|
|
tpval);
|
2012-08-22 01:12:31 -04:00
|
|
|
}
|
|
|
|
else {
|
2012-12-05 22:13:50 -05:00
|
|
|
return previous_tracing ? Qtrue : Qfalse;
|
2012-08-22 01:12:31 -04:00
|
|
|
}
|
2012-11-20 06:05:20 -05:00
|
|
|
}
|
2012-08-22 01:12:31 -04:00
|
|
|
|
2012-11-30 04:25:24 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* trace.enabled? -> true or false
|
|
|
|
*
|
|
|
|
* The current status of the trace
|
|
|
|
*/
|
2012-11-20 07:57:49 -05:00
|
|
|
VALUE
|
|
|
|
rb_tracepoint_enabled_p(VALUE tpval)
|
2012-11-20 06:05:20 -05:00
|
|
|
{
|
|
|
|
rb_tp_t *tp = tpptr(tpval);
|
|
|
|
return tp->tracing ? Qtrue : Qfalse;
|
2012-08-22 01:12:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2012-11-20 07:57:49 -05:00
|
|
|
tracepoint_new(VALUE klass, rb_thread_t *target_th, rb_event_flag_t events, void (func)(VALUE, void*), void *data, VALUE proc)
|
2012-08-22 01:12:31 -04:00
|
|
|
{
|
2012-11-20 06:05:20 -05:00
|
|
|
VALUE tpval = tp_alloc(klass);
|
2012-08-22 01:12:31 -04:00
|
|
|
rb_tp_t *tp;
|
|
|
|
TypedData_Get_Struct(tpval, rb_tp_t, &tp_data_type, tp);
|
|
|
|
|
|
|
|
tp->proc = proc;
|
2012-11-20 07:57:49 -05:00
|
|
|
tp->func = func;
|
|
|
|
tp->data = data;
|
2012-08-22 01:12:31 -04:00
|
|
|
tp->events = events;
|
2012-11-20 07:57:49 -05:00
|
|
|
tp->self = tpval;
|
2012-08-22 01:12:31 -04:00
|
|
|
|
|
|
|
return tpval;
|
|
|
|
}
|
|
|
|
|
2012-11-20 07:57:49 -05:00
|
|
|
VALUE
|
2012-12-21 06:22:54 -05:00
|
|
|
rb_tracepoint_new(VALUE target_thval, rb_event_flag_t events, void (*func)(VALUE, void *), void *data)
|
2012-11-20 07:57:49 -05:00
|
|
|
{
|
2012-12-01 10:26:52 -05:00
|
|
|
rb_thread_t *target_th = 0;
|
2012-12-21 06:22:54 -05:00
|
|
|
if (RTEST(target_thval)) {
|
|
|
|
GetThreadPtr(target_thval, target_th);
|
|
|
|
/* TODO: Test it!
|
|
|
|
* Warning: This function is not tested.
|
|
|
|
*/
|
2012-11-20 07:57:49 -05:00
|
|
|
}
|
|
|
|
return tracepoint_new(rb_cTracePoint, target_th, events, func, data, Qundef);
|
|
|
|
}
|
|
|
|
|
2012-11-30 04:25:24 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* TracePoint.new(*events) { |obj| block } -> obj
|
|
|
|
*
|
|
|
|
* Returns a new TracePoint object, not enabled by default.
|
|
|
|
*
|
|
|
|
* Next, in order to activate the trace, you must use TracePoint.enable
|
|
|
|
*
|
|
|
|
* trace = TracePoint.new(:call) do |tp|
|
|
|
|
* p [tp.lineno, tp.defined_class, tp.method_id, tp.event]
|
|
|
|
* end
|
|
|
|
* #=> #<TracePoint:0x007f17372cdb20>
|
|
|
|
*
|
|
|
|
* trace.enable
|
|
|
|
* #=> #<TracePoint:0x007f17372cdb20>
|
|
|
|
*
|
|
|
|
* puts "Hello, TracePoint!"
|
|
|
|
* # ...
|
|
|
|
* # [48, IRB::Notifier::AbstractNotifier, :printf, :call]
|
|
|
|
* # ...
|
|
|
|
*
|
|
|
|
* When you want to deactivate the trace, you must use TracePoint.disable
|
|
|
|
*
|
|
|
|
* trace.disable
|
|
|
|
*
|
2013-02-08 23:14:09 -05:00
|
|
|
* See TracePoint@Events for possible events and more information.
|
2012-11-30 04:25:24 -05:00
|
|
|
*
|
|
|
|
* A block must be given, otherwise a ThreadError is raised.
|
|
|
|
*
|
|
|
|
* If the trace method isn't included in the given events filter, a
|
|
|
|
* RuntimeError is raised.
|
|
|
|
*
|
|
|
|
* TracePoint.trace(:line) do |tp|
|
|
|
|
* p tp.raised_exception
|
|
|
|
* end
|
|
|
|
* #=> RuntimeError: 'raised_exception' not supported by this event
|
2012-12-21 04:33:44 -05:00
|
|
|
*
|
|
|
|
* If the trace method is called outside block, a RuntimeError is raised.
|
|
|
|
*
|
|
|
|
* TracePoint.trace(:line) do |tp|
|
|
|
|
* $tp = tp
|
|
|
|
* end
|
|
|
|
* $tp.line #=> access from outside (RuntimeError)
|
|
|
|
*
|
|
|
|
* Access from other threads is also forbidden.
|
|
|
|
*
|
2012-11-30 04:25:24 -05:00
|
|
|
*/
|
2012-08-22 01:12:31 -04:00
|
|
|
static VALUE
|
2012-11-20 07:57:49 -05:00
|
|
|
tracepoint_new_s(int argc, VALUE *argv, VALUE self)
|
2012-08-22 01:12:31 -04:00
|
|
|
{
|
|
|
|
rb_event_flag_t events = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (argc > 0) {
|
|
|
|
for (i=0; i<argc; i++) {
|
|
|
|
events |= symbol2event_flag(argv[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2012-11-29 17:28:16 -05:00
|
|
|
events = RUBY_EVENT_TRACEPOINT_ALL;
|
2012-08-22 01:12:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!rb_block_given_p()) {
|
|
|
|
rb_raise(rb_eThreadError, "must be called with a block");
|
|
|
|
}
|
|
|
|
|
2012-11-20 07:57:49 -05:00
|
|
|
return tracepoint_new(self, 0, events, 0, 0, rb_block_proc());
|
2012-11-20 06:05:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2012-11-20 07:57:49 -05:00
|
|
|
tracepoint_trace_s(int argc, VALUE *argv, VALUE self)
|
2012-11-20 06:05:20 -05:00
|
|
|
{
|
2012-11-20 07:57:49 -05:00
|
|
|
VALUE trace = tracepoint_new_s(argc, argv, self);
|
|
|
|
rb_tracepoint_enable(trace);
|
2012-11-20 06:05:20 -05:00
|
|
|
return trace;
|
2012-08-22 01:12:31 -04:00
|
|
|
}
|
2012-08-15 00:39:10 -04:00
|
|
|
|
2012-12-01 12:47:37 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* trace.inspect -> string
|
|
|
|
*
|
|
|
|
* Return a string containing a human-readable TracePoint
|
|
|
|
* status.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
tracepoint_inspect(VALUE self)
|
|
|
|
{
|
|
|
|
rb_tp_t *tp = tpptr(self);
|
2012-12-21 04:33:44 -05:00
|
|
|
rb_trace_arg_t *trace_arg = GET_THREAD()->trace_arg;
|
2012-12-01 12:47:37 -05:00
|
|
|
|
2012-12-21 04:33:44 -05:00
|
|
|
if (trace_arg) {
|
|
|
|
switch (trace_arg->event) {
|
2012-12-01 12:47:37 -05:00
|
|
|
case RUBY_EVENT_LINE:
|
|
|
|
case RUBY_EVENT_SPECIFIED_LINE:
|
|
|
|
{
|
2012-12-21 04:33:44 -05:00
|
|
|
VALUE sym = rb_tracearg_method_id(trace_arg);
|
2012-12-01 12:47:37 -05:00
|
|
|
if (NIL_P(sym))
|
|
|
|
goto default_inspect;
|
|
|
|
return rb_sprintf("#<TracePoint:%"PRIsVALUE"@%"PRIsVALUE":%d in `%"PRIsVALUE"'>",
|
2012-12-21 04:33:44 -05:00
|
|
|
rb_tracearg_event(trace_arg),
|
|
|
|
rb_tracearg_path(trace_arg),
|
|
|
|
FIX2INT(rb_tracearg_lineno(trace_arg)),
|
2012-12-01 12:47:37 -05:00
|
|
|
sym);
|
|
|
|
}
|
|
|
|
case RUBY_EVENT_CALL:
|
|
|
|
case RUBY_EVENT_C_CALL:
|
|
|
|
case RUBY_EVENT_RETURN:
|
|
|
|
case RUBY_EVENT_C_RETURN:
|
|
|
|
return rb_sprintf("#<TracePoint:%"PRIsVALUE" `%"PRIsVALUE"'@%"PRIsVALUE":%d>",
|
2012-12-21 04:33:44 -05:00
|
|
|
rb_tracearg_event(trace_arg),
|
|
|
|
rb_tracearg_method_id(trace_arg),
|
|
|
|
rb_tracearg_path(trace_arg),
|
|
|
|
FIX2INT(rb_tracearg_lineno(trace_arg)));
|
2012-12-01 12:47:37 -05:00
|
|
|
case RUBY_EVENT_THREAD_BEGIN:
|
|
|
|
case RUBY_EVENT_THREAD_END:
|
|
|
|
return rb_sprintf("#<TracePoint:%"PRIsVALUE" %"PRIsVALUE">",
|
2012-12-21 04:33:44 -05:00
|
|
|
rb_tracearg_event(trace_arg),
|
|
|
|
rb_tracearg_self(trace_arg));
|
2012-12-01 12:47:37 -05:00
|
|
|
default:
|
|
|
|
default_inspect:
|
|
|
|
return rb_sprintf("#<TracePoint:%"PRIsVALUE"@%"PRIsVALUE":%d>",
|
2012-12-21 04:33:44 -05:00
|
|
|
rb_tracearg_event(trace_arg),
|
|
|
|
rb_tracearg_path(trace_arg),
|
|
|
|
FIX2INT(rb_tracearg_lineno(trace_arg)));
|
2012-12-01 12:47:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return rb_sprintf("#<TracePoint:%s>", tp->tracing ? "enabled" : "disabled");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-15 00:39:10 -04:00
|
|
|
/* This function is called from inits.c */
|
|
|
|
void
|
|
|
|
Init_vm_trace(void)
|
|
|
|
{
|
2012-08-22 01:12:31 -04:00
|
|
|
/* trace_func */
|
2012-08-15 00:39:10 -04:00
|
|
|
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);
|
|
|
|
|
2012-11-30 04:25:24 -05:00
|
|
|
/*
|
|
|
|
* Document-class: TracePoint
|
|
|
|
*
|
|
|
|
* A class that provides the functionality of Kernel#set_trace_func in a
|
|
|
|
* nice Object-Oriented API.
|
|
|
|
*
|
2013-02-08 23:14:09 -05:00
|
|
|
* == Example
|
2012-11-30 04:25:24 -05:00
|
|
|
*
|
|
|
|
* We can use TracePoint to gather information specifically for exceptions:
|
|
|
|
*
|
|
|
|
* trace = TracePoint.new(:raise) do |tp|
|
|
|
|
* p [tp.lineno, tp.event, tp.raised_exception]
|
|
|
|
* end
|
|
|
|
* #=> #<TracePoint:0x007f786a452448>
|
|
|
|
*
|
|
|
|
* trace.enable
|
|
|
|
* #=> #<TracePoint:0x007f786a452448>
|
|
|
|
*
|
|
|
|
* 0 / 0
|
|
|
|
* #=> [5, :raise, #<ZeroDivisionError: divided by 0>]
|
|
|
|
*
|
2013-02-08 23:14:09 -05:00
|
|
|
* == Events
|
|
|
|
*
|
|
|
|
* If you don't specify the type of events you want to listen for,
|
|
|
|
* TracePoint will include all available events.
|
|
|
|
*
|
|
|
|
* *Note* do not depend on current event set, as this list is subject to
|
|
|
|
* change. Instead, it is recommended you specify the type of events you
|
|
|
|
* want to use.
|
|
|
|
*
|
|
|
|
* To filter what is traced, you can pass any of the following as +events+:
|
|
|
|
*
|
|
|
|
* +:line+:: execute code on a new line
|
|
|
|
* +:class+:: start a class or module definition
|
|
|
|
* +:end+:: finish a class or module definition
|
|
|
|
* +:call+:: call a Ruby method
|
|
|
|
* +:return+:: return from a Ruby method
|
|
|
|
* +:c_call+:: call a C-language routine
|
|
|
|
* +:c_return+:: return from a C-language routine
|
|
|
|
* +:raise+:: raise an exception
|
|
|
|
* +:b_call+:: event hook at block entry
|
|
|
|
* +:b_return+:: event hook at block ending
|
|
|
|
* +:thread_begin+:: event hook at thread beginning
|
|
|
|
* +:thread_end+:: event hook at thread ending
|
2012-11-30 04:25:24 -05:00
|
|
|
*
|
|
|
|
*/
|
2012-08-22 01:12:31 -04:00
|
|
|
rb_cTracePoint = rb_define_class("TracePoint", rb_cObject);
|
|
|
|
rb_undef_alloc_func(rb_cTracePoint);
|
|
|
|
rb_undef_method(CLASS_OF(rb_cTracePoint), "new");
|
2012-11-20 07:57:49 -05:00
|
|
|
rb_define_singleton_method(rb_cTracePoint, "new", tracepoint_new_s, -1);
|
2012-11-30 04:25:24 -05:00
|
|
|
/*
|
|
|
|
* Document-method: trace
|
|
|
|
*
|
|
|
|
* call-seq:
|
|
|
|
* TracePoint.trace(*events) { |obj| block } -> obj
|
|
|
|
*
|
|
|
|
* A convenience method for TracePoint.new, that activates the trace
|
|
|
|
* automatically.
|
|
|
|
*
|
|
|
|
* trace = TracePoint.trace(:call) { |tp| [tp.lineno, tp.event] }
|
|
|
|
* #=> #<TracePoint:0x007f786a452448>
|
|
|
|
*
|
|
|
|
* trace.enabled? #=> true
|
|
|
|
*/
|
2012-11-20 07:57:49 -05:00
|
|
|
rb_define_singleton_method(rb_cTracePoint, "trace", tracepoint_trace_s, -1);
|
|
|
|
|
|
|
|
rb_define_method(rb_cTracePoint, "enable", tracepoint_enable_m, 0);
|
|
|
|
rb_define_method(rb_cTracePoint, "disable", tracepoint_disable_m, 0);
|
|
|
|
rb_define_method(rb_cTracePoint, "enabled?", rb_tracepoint_enabled_p, 0);
|
|
|
|
|
2012-12-01 12:47:37 -05:00
|
|
|
rb_define_method(rb_cTracePoint, "inspect", tracepoint_inspect, 0);
|
|
|
|
|
2012-11-29 01:43:31 -05:00
|
|
|
rb_define_method(rb_cTracePoint, "event", tracepoint_attr_event, 0);
|
|
|
|
rb_define_method(rb_cTracePoint, "lineno", tracepoint_attr_lineno, 0);
|
|
|
|
rb_define_method(rb_cTracePoint, "path", tracepoint_attr_path, 0);
|
|
|
|
rb_define_method(rb_cTracePoint, "method_id", tracepoint_attr_method_id, 0);
|
|
|
|
rb_define_method(rb_cTracePoint, "defined_class", tracepoint_attr_defined_class, 0);
|
|
|
|
rb_define_method(rb_cTracePoint, "binding", tracepoint_attr_binding, 0);
|
|
|
|
rb_define_method(rb_cTracePoint, "self", tracepoint_attr_self, 0);
|
|
|
|
rb_define_method(rb_cTracePoint, "return_value", tracepoint_attr_return_value, 0);
|
|
|
|
rb_define_method(rb_cTracePoint, "raised_exception", tracepoint_attr_raised_exception, 0);
|
2012-08-22 01:12:31 -04:00
|
|
|
}
|
2012-11-29 01:43:31 -05:00
|
|
|
|
2013-05-26 17:30:44 -04:00
|
|
|
typedef struct rb_postponed_job_struct {
|
|
|
|
unsigned long flags; /* reserve */
|
2013-05-27 03:55:59 -04:00
|
|
|
rb_thread_t *th; /* created thread, reserve */
|
2013-05-26 17:30:44 -04:00
|
|
|
rb_postponed_job_func_t func;
|
|
|
|
void *data;
|
|
|
|
struct rb_postponed_job_struct *next;
|
|
|
|
} rb_postponed_job_t;
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_postponed_job_register(unsigned int flags, rb_postponed_job_func_t func, void *data)
|
|
|
|
{
|
|
|
|
rb_thread_t *th = GET_THREAD();
|
|
|
|
rb_vm_t *vm = th->vm;
|
2013-05-27 03:17:30 -04:00
|
|
|
rb_postponed_job_t *pjob = (rb_postponed_job_t *)ruby_xmalloc(sizeof(rb_postponed_job_t));
|
2013-05-26 17:30:44 -04:00
|
|
|
if (pjob == NULL) return 0; /* failed */
|
|
|
|
|
|
|
|
pjob->flags = flags;
|
|
|
|
pjob->th = th;
|
|
|
|
pjob->func = func;
|
|
|
|
pjob->data = data;
|
|
|
|
|
|
|
|
pjob->next = vm->postponed_job;
|
|
|
|
vm->postponed_job = pjob;
|
|
|
|
|
|
|
|
RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(th);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_postponed_job_register_one(unsigned int flags, rb_postponed_job_func_t func, void *data)
|
|
|
|
{
|
|
|
|
rb_vm_t *vm = GET_VM();
|
|
|
|
rb_postponed_job_t *pjob = vm->postponed_job;
|
|
|
|
|
|
|
|
while (pjob) {
|
|
|
|
if (pjob->func == func) {
|
|
|
|
return 2;
|
|
|
|
}
|
2013-05-27 13:08:25 -04:00
|
|
|
pjob = pjob->next;
|
2013-05-26 17:30:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return rb_postponed_job_register(flags, func, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_postponed_job_flush(rb_vm_t *vm)
|
|
|
|
{
|
|
|
|
rb_postponed_job_t *pjob = vm->postponed_job, *next_pjob;
|
|
|
|
vm->postponed_job = 0;
|
|
|
|
|
|
|
|
while (pjob) {
|
|
|
|
next_pjob = pjob->next;
|
|
|
|
pjob->func(pjob->data);
|
2013-05-27 11:19:36 -04:00
|
|
|
ruby_xfree(pjob);
|
2013-05-26 17:30:44 -04:00
|
|
|
pjob = next_pjob;
|
|
|
|
}
|
|
|
|
}
|