24#include "eval_intern.h"
26#include "internal/bits.h"
27#include "internal/class.h"
28#include "internal/gc.h"
29#include "internal/hash.h"
30#include "internal/symbol.h"
31#include "internal/thread.h"
37#include "ractor_core.h"
43static VALUE sym_default;
48 rb_event_hook_flag_t hook_flags;
56 unsigned int target_line;
62#define MAX_EVENT_NUM 32
65rb_hook_list_mark(rb_hook_list_t *hooks)
67 rb_event_hook_t *hook = hooks->hooks;
70 rb_gc_mark(hook->data);
76rb_hook_list_mark_and_move(rb_hook_list_t *hooks)
78 if (!rb_gc_checking_shareable()) {
81 rb_event_hook_t *hook = hooks->hooks;
84 rb_gc_mark_and_move(&hook->data);
90static void clean_hooks(rb_hook_list_t *list);
93rb_hook_list_free(rb_hook_list_t *hooks)
95 for (rb_event_hook_t *hook = hooks->hooks; hook; hook = hook->next) {
96 hook->hook_flags |= RUBY_EVENT_HOOK_FLAG_DELETED;
98 hooks->need_clean =
true;
100 if (hooks->running == 0) {
106rb_hook_list_memsize(
const rb_hook_list_t *hooks)
109 const rb_event_hook_t *hook = hooks->hooks;
111 size +=
sizeof(rb_event_hook_t);
119void rb_clear_attr_ccs(
void);
120void rb_clear_bf_ccs(
void);
125 rb_event_flag_t enabled_iseq_events = ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS;
126 return new_iseq_events & ~enabled_iseq_events;
134 return enable_c_call || enable_c_return;
142update_global_event_hooks(rb_hook_list_t *list,
rb_event_flag_t prev_events,
rb_event_flag_t new_events,
int change_iseq_events,
int change_c_events)
144 rb_execution_context_t *ec = rb_current_execution_context(
false);
151 const bool vm_locked_here = ec && !ruby_vm_during_cleanup && GET_RACTOR() != NULL;
152 if (vm_locked_here) {
153 RB_VM_LOCK_ENTER_LEV(&lev);
158 rb_event_flag_t enabled_iseq_events = ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS;
159 bool new_iseq_events_p = iseq_trace_set_all_needed(new_events);
162 bool clear_attr_ccs_p = clear_attr_ccs_needed(prev_events, new_events);
169 rb_event_flag_t new_events_global = (prev_events_global & ~prev_events) | new_events;
170 ruby_vm_event_flags = new_events_global;
173 if (new_iseq_events_p) {
175 rb_iseq_trace_set_all(new_iseq_events | enabled_iseq_events);
177 else if (clear_attr_ccs_p) {
180 else if (enable_call || enable_return) {
184 if (change_iseq_events < 0) {
185 RUBY_ASSERT(ruby_vm_iseq_events_enabled >= (
unsigned int)(-change_iseq_events));
187 ruby_vm_iseq_events_enabled += change_iseq_events;
188 if (change_c_events < 0) {
189 RUBY_ASSERT(ruby_vm_c_events_enabled >= (
unsigned int)(-change_c_events));
191 ruby_vm_c_events_enabled += change_c_events;
193 ruby_vm_event_enabled_global_flags |= new_events;
195 rb_objspace_set_event_hook(new_events_global);
202 rb_zjit_invalidate_newobj_hook();
206 if (new_iseq_events_p || clear_attr_ccs_p) {
211 rb_yjit_tracing_invalidate_all();
212 rb_zjit_tracing_invalidate_all();
215 if (vm_locked_here) {
216 RB_VM_LOCK_LEAVE_LEV(&lev);
222static rb_event_hook_t *
225 rb_event_hook_t *hook;
228 rb_raise(
rb_eTypeError,
"Can not specify normal event and internal event simultaneously.");
231 hook =
ALLOC(rb_event_hook_t);
232 hook->hook_flags = hook_flags;
233 hook->events = events;
238 hook->filter.th = NULL;
239 hook->filter.target_line = 0;
246hook_list_connect(rb_hook_list_t *list, rb_event_hook_t *hook,
int global_p)
249 int change_iseq_events = 0;
250 int change_c_events = 0;
251 hook->next = list->hooks;
253 list->events |= hook->events;
256 if (hook->events & ISEQ_TRACE_EVENTS) {
257 change_iseq_events++;
262 update_global_event_hooks(list, prev_events, list->events, change_iseq_events, change_c_events);
267connect_non_targeted_event_hook(
const rb_execution_context_t *ec, rb_event_hook_t *hook)
269 rb_hook_list_t *list;
275 list = rb_vm_global_hooks(ec);
278 list = rb_ec_ractor_hooks(ec);
280 hook_list_connect(list, hook, TRUE);
284rb_threadptr_add_event_hook(
const rb_execution_context_t *ec, rb_thread_t *th,
287 rb_event_hook_t *hook = alloc_event_hook(func, events, data, hook_flags);
288 hook->filter.th = th;
289 connect_non_targeted_event_hook(ec, hook);
295 rb_threadptr_add_event_hook(GET_EC(), rb_thread_ptr(thval), func, events, data, RUBY_EVENT_HOOK_FLAG_SAFE);
301 rb_add_event_hook2(func, events, data, RUBY_EVENT_HOOK_FLAG_SAFE);
307 rb_threadptr_add_event_hook(GET_EC(), rb_thread_ptr(thval), func, events, data, hook_flags);
313 rb_event_hook_t *hook = alloc_event_hook(func, events, data, hook_flags);
314 connect_non_targeted_event_hook(GET_EC(), hook);
318hook_list_targeted_p(rb_hook_list_t *list)
320 switch (list->type) {
321 case hook_list_type_targeted_iseq:
322 case hook_list_type_targeted_def:
330rb_hook_list_count(rb_hook_list_t *list)
332 rb_event_hook_t *hook = list->hooks;
333 unsigned int count = 0;
336 if (!(hook->hook_flags & RUBY_EVENT_HOOK_FLAG_DELETED)) {
346clean_hooks(rb_hook_list_t *list)
348 rb_event_hook_t *hook, **nextp = &list->hooks;
350 int change_iseq_events = 0;
351 int change_c_events = 0;
353 VM_ASSERT(list->running == 0);
354 VM_ASSERT(list->need_clean ==
true);
357 list->need_clean =
false;
359 while ((hook = *nextp) != 0) {
360 if (hook->hook_flags & RUBY_EVENT_HOOK_FLAG_DELETED) {
362 if (!hook_list_targeted_p(list)) {
363 if (hook->events & ISEQ_TRACE_EVENTS) {
364 change_iseq_events--;
373 list->events |= hook->events;
378 if (hook_list_targeted_p(list)) {
379 if (list->events == 0) {
384 update_global_event_hooks(list, prev_events, list->events, change_iseq_events, change_c_events);
389clean_hooks_check(rb_hook_list_t *list)
391 if (UNLIKELY(list->need_clean)) {
392 if (list->running == 0) {
398#define MATCH_ANY_FILTER_TH ((rb_thread_t *)1)
404 rb_event_hook_t *hook = list->hooks;
407 if (func == 0 || hook->func == func) {
408 if (hook->filter.th == filter_th || filter_th == MATCH_ANY_FILTER_TH) {
409 if (UNDEF_P(data) || hook->data == data) {
410 hook->hook_flags |= RUBY_EVENT_HOOK_FLAG_DELETED;
411 list->need_clean =
true;
419 clean_hooks_check(list);
429 ret += remove_event_hook_from_list(rb_ec_ractor_hooks(ec), filter_th, func, data);
430 ret += remove_event_hook_from_list(rb_vm_global_hooks(ec), filter_th, func, data);
436rb_threadptr_remove_event_hook(
const rb_execution_context_t *ec,
const rb_thread_t *filter_th,
rb_event_hook_func_t func,
VALUE data)
438 return remove_event_hook(ec, filter_th, func, data);
444 return rb_threadptr_remove_event_hook(GET_EC(), rb_thread_ptr(thval), func,
Qundef);
450 return rb_threadptr_remove_event_hook(GET_EC(), rb_thread_ptr(thval), func, data);
456 return remove_event_hook(GET_EC(), NULL, func,
Qundef);
462 return remove_event_hook(GET_EC(), NULL, func, data);
466rb_ec_clear_current_thread_trace_func(
const rb_execution_context_t *ec)
468 rb_threadptr_remove_event_hook(ec, rb_ec_thread_ptr(ec), 0,
Qundef);
472rb_ec_clear_all_trace_func(
const rb_execution_context_t *ec)
474 rb_threadptr_remove_event_hook(ec, MATCH_ANY_FILTER_TH, 0,
Qundef);
480exec_hooks_body(
const rb_execution_context_t *ec, rb_hook_list_t *list,
const rb_trace_arg_t *trace_arg)
482 rb_event_hook_t *hook;
484 for (hook = list->hooks; hook; hook = hook->next) {
485 if (!(hook->hook_flags & RUBY_EVENT_HOOK_FLAG_DELETED) &&
486 (trace_arg->event & hook->events) &&
487 (LIKELY(hook->filter.th == 0) || hook->filter.th == rb_ec_thread_ptr(ec)) &&
488 (LIKELY(hook->filter.target_line == 0) || (hook->filter.target_line == (
unsigned int)rb_vm_get_sourceline(ec->cfp)))) {
489 if (!(hook->hook_flags & RUBY_EVENT_HOOK_FLAG_RAW_ARG)) {
490 (*hook->func)(trace_arg->event, hook->data, trace_arg->self, trace_arg->id, trace_arg->klass);
493 (*((rb_event_hook_raw_arg_func_t)hook->func))(hook->data, trace_arg);
500exec_hooks_precheck(
const rb_execution_context_t *ec, rb_hook_list_t *list,
const rb_trace_arg_t *trace_arg)
502 if (list->events & trace_arg->event) {
512exec_hooks_postcheck(
const rb_execution_context_t *ec, rb_hook_list_t *list)
515 clean_hooks_check(list);
519exec_hooks_unprotected(
const rb_execution_context_t *ec, rb_hook_list_t *list,
const rb_trace_arg_t *trace_arg)
521 if (exec_hooks_precheck(ec, list, trace_arg) == 0)
return;
522 exec_hooks_body(ec, list, trace_arg);
523 exec_hooks_postcheck(ec, list);
527exec_hooks_protected(rb_execution_context_t *ec_arg, rb_hook_list_t *list_arg,
const rb_trace_arg_t *trace_arg)
529 enum ruby_tag_type state;
531 rb_execution_context_t *
volatile ec = ec_arg;
532 rb_hook_list_t *
volatile list = list_arg;
534 if (exec_hooks_precheck(ec, list, trace_arg) == 0)
return 0;
536 raised = rb_ec_reset_raised(ec);
541 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
542 exec_hooks_body(ec, list, trace_arg);
546 exec_hooks_postcheck(ec, list);
549 rb_ec_set_raised(ec);
557rb_exec_event_hooks(
rb_trace_arg_t *trace_arg, rb_hook_list_t *hooks,
int pop_p)
559 rb_execution_context_t *ec = trace_arg->ec;
562 VM_ASSERT(hooks == rb_vm_global_hooks(ec));
570 ec->trace_arg = trace_arg;
572 exec_hooks_unprotected(ec, hooks, trace_arg);
573 ec->trace_arg = prev_trace_arg;
577 if (ec->trace_arg == NULL &&
578 trace_arg->self != rb_mRubyVMFrozenCore ) {
579 const VALUE errinfo = ec->errinfo;
580 const VALUE old_recursive = ec->local_storage_recursive_hash;
581 enum ruby_tag_type state = 0;
584 ec->local_storage_recursive_hash = ec->local_storage_recursive_hash_for_trace;
586 ec->trace_arg = trace_arg;
589 if ((state = exec_hooks_protected(ec, hooks, trace_arg)) == TAG_NONE) {
590 ec->errinfo = errinfo;
594 ec->trace_arg = NULL;
595 ec->local_storage_recursive_hash_for_trace = ec->local_storage_recursive_hash;
596 ec->local_storage_recursive_hash = old_recursive;
600 if (VM_FRAME_FINISHED_P(ec->cfp)) {
601 rb_vm_tag_jmpbuf_deinit(&ec->tag->buf);
602 ec->tag = ec->tag->prev;
606 EC_JUMP_TAG(ec, state);
617 rb_execution_context_t *
const ec = GET_EC();
618 rb_vm_t *
const vm = rb_ec_vm_ptr(ec);
619 enum ruby_tag_type state;
621 dummy_trace_arg.event = 0;
623 if (!ec->trace_arg) {
624 ec->trace_arg = &dummy_trace_arg;
627 raised = rb_ec_reset_raised(ec);
630 if (LIKELY((state = EC_EXEC_TAG()) == TAG_NONE)) {
631 result = (*func)(arg);
639 rb_ec_reset_raised(ec);
642 if (ec->trace_arg == &dummy_trace_arg) {
643 ec->trace_arg = NULL;
647#if defined RUBY_USE_SETJMPEX && RUBY_USE_SETJMPEX
650 EC_JUMP_TAG(ec, state);
740thread_add_trace_func(rb_execution_context_t *ec, rb_thread_t *filter_th,
VALUE trace)
746 rb_threadptr_add_event_hook(ec, filter_th, call_trace_func,
RUBY_EVENT_ALL, trace, RUBY_EVENT_HOOK_FLAG_SAFE);
759thread_add_trace_func_m(
VALUE obj,
VALUE trace)
761 thread_add_trace_func(GET_EC(), rb_thread_ptr(obj), trace);
777thread_set_trace_func_m(
VALUE target_thread,
VALUE trace)
779 rb_execution_context_t *ec = GET_EC();
780 rb_thread_t *target_th = rb_thread_ptr(target_thread);
782 rb_threadptr_remove_event_hook(ec, target_th, call_trace_func,
Qundef);
788 thread_add_trace_func(ec, target_th, trace);
816#define C(name, NAME) case RUBY_EVENT_##NAME: CONST_ID(id, #name); return id;
823 C(c_return, C_RETURN);
826 C(b_return, B_RETURN);
827 C(thread_begin, THREAD_BEGIN);
828 C(thread_end, THREAD_END);
829 C(fiber_switch, FIBER_SWITCH);
830 C(script_compiled, SCRIPT_COMPILED);
839get_path_and_lineno(
const rb_execution_context_t *ec,
const rb_control_frame_t *cfp,
rb_event_flag_t event,
VALUE *pathp,
int *linep)
841 cfp = rb_vm_get_ruby_level_next_cfp(ec, cfp);
844 const rb_iseq_t *iseq = CFP_ISEQ(cfp);
845 *pathp = rb_iseq_path(iseq);
850 *linep =
FIX2INT(rb_iseq_first_lineno(iseq));
853 *linep = rb_vm_get_sourceline(cfp);
869 const rb_execution_context_t *ec = GET_EC();
871 get_path_and_lineno(ec, ec->cfp, event, &filename, &line);
874 rb_ec_frame_method_id_and_class(ec, &
id, 0, &klass);
879 klass =
RBASIC(klass)->klass;
881 else if (RCLASS_SINGLETON_P(klass)) {
882 klass = RCLASS_ATTACHED_OBJECT(klass);
891 if (self && (filename !=
Qnil) &&
894 (VM_FRAME_RUBYFRAME_P(ec->cfp) && imemo_type_p((
VALUE)CFP_ISEQ(ec->cfp), imemo_iseq))) {
897 argv[5] = klass ? klass :
Qnil;
904static VALUE rb_cTracePoint;
909 rb_thread_t *target_th;
910 VALUE local_target_set;
914 void (*func)(
VALUE tpval,
void *data);
922tp_mark_and_move(
void *ptr)
925 rb_gc_mark_and_move(&tp->proc);
926 rb_gc_mark_and_move(&tp->local_target_set);
927 if (tp->target_th) rb_gc_mark_and_move(&tp->target_th->self);
938 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
949symbol2event_flag(
VALUE v)
952 VALUE sym = rb_to_symbol_type(v);
958#define C(name, NAME) CONST_ID(id, #name); if (sym == ID2SYM(id)) return RUBY_EVENT_##NAME
965 C(c_return, C_RETURN);
968 C(b_return, B_RETURN);
969 C(thread_begin, THREAD_BEGIN);
970 C(thread_end, THREAD_END);
971 C(fiber_switch, FIBER_SWITCH);
972 C(script_compiled, SCRIPT_COMPILED);
977 C(a_return, A_RETURN);
979 rb_raise(rb_eArgError,
"unknown event: %"PRIsVALUE,
rb_sym2str(sym));
994 if (trace_arg == 0) {
1003 return get_trace_arg();
1009 return trace_arg->event;
1015 return ID2SYM(get_event_id(trace_arg->event));
1021 if (UNDEF_P(trace_arg->path)) {
1022 get_path_and_lineno(trace_arg->ec, trace_arg->cfp, trace_arg->event, &trace_arg->path, &trace_arg->lineno);
1029 fill_path_and_lineno(trace_arg);
1030 return INT2NUM(trace_arg->lineno);
1035 fill_path_and_lineno(trace_arg);
1036 return trace_arg->path;
1042 if (!trace_arg->klass_solved) {
1043 if (!trace_arg->klass) {
1044 rb_vm_control_frame_id_and_class(trace_arg->cfp, &trace_arg->id, &trace_arg->called_id, &trace_arg->klass);
1047 if (trace_arg->klass) {
1049 trace_arg->klass =
RBASIC(trace_arg->klass)->klass;
1053 trace_arg->klass =
Qnil;
1056 trace_arg->klass_solved = 1;
1063 switch (trace_arg->event) {
1068 const rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(trace_arg->ec, trace_arg->cfp);
1071 if (VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_BLOCK && !VM_FRAME_LAMBDA_P(cfp)) {
1074 return rb_iseq_parameters(CFP_ISEQ(cfp), is_proc);
1080 fill_id_and_klass(trace_arg);
1081 if (trace_arg->klass && trace_arg->id) {
1082 const rb_method_entry_t *me;
1084 me = rb_method_entry_without_refinements(trace_arg->klass, trace_arg->called_id, &iclass);
1086 me = rb_method_entry_without_refinements(trace_arg->klass, trace_arg->id, &iclass);
1088 return rb_unnamed_parameters(rb_method_entry_arity(me));
1107 fill_id_and_klass(trace_arg);
1108 return trace_arg->id ?
ID2SYM(trace_arg->id) :
Qnil;
1114 fill_id_and_klass(trace_arg);
1115 return trace_arg->called_id ?
ID2SYM(trace_arg->called_id) :
Qnil;
1121 fill_id_and_klass(trace_arg);
1122 return trace_arg->klass;
1128 rb_control_frame_t *cfp;
1129 switch (trace_arg->event) {
1134 cfp = rb_vm_get_binding_creatable_next_cfp(trace_arg->ec, trace_arg->cfp);
1136 if (cfp && imemo_type_p((
VALUE)CFP_ISEQ(cfp), imemo_iseq)) {
1137 return rb_vm_make_binding(trace_arg->ec, cfp);
1147 return trace_arg->self;
1153 if (!(trace_arg->event & supported)) {
1162 if (UNDEF_P(trace_arg->data)) {
1163 rb_bug(
"rb_tracearg_return_value: unreachable");
1165 return trace_arg->data;
1172 if (UNDEF_P(trace_arg->data)) {
1173 rb_bug(
"rb_tracearg_raised_exception: unreachable");
1175 return trace_arg->data;
1181 VALUE data = trace_arg->data;
1184 if (UNDEF_P(data)) {
1185 rb_bug(
"rb_tracearg_eval_script: unreachable");
1187 if (rb_obj_is_iseq(data)) {
1200 VALUE data = trace_arg->data;
1203 if (UNDEF_P(data)) {
1204 rb_bug(
"rb_tracearg_instruction_sequence: unreachable");
1207 if (rb_obj_is_iseq(data)) {
1208 return rb_iseqw_new((
const rb_iseq_t *)data);
1215 return rb_iseqw_new((
const rb_iseq_t *)
RARRAY_AREF(data, 1));
1223 if (UNDEF_P(trace_arg->data)) {
1224 rb_bug(
"rb_tracearg_object: unreachable");
1226 return trace_arg->data;
1230tracepoint_attr_event(rb_execution_context_t *ec,
VALUE tpval)
1236tracepoint_attr_lineno(rb_execution_context_t *ec,
VALUE tpval)
1241tracepoint_attr_path(rb_execution_context_t *ec,
VALUE tpval)
1247tracepoint_attr_parameters(rb_execution_context_t *ec,
VALUE tpval)
1253tracepoint_attr_method_id(rb_execution_context_t *ec,
VALUE tpval)
1259tracepoint_attr_callee_id(rb_execution_context_t *ec,
VALUE tpval)
1265tracepoint_attr_defined_class(rb_execution_context_t *ec,
VALUE tpval)
1271tracepoint_attr_binding(rb_execution_context_t *ec,
VALUE tpval)
1277tracepoint_attr_self(rb_execution_context_t *ec,
VALUE tpval)
1283tracepoint_attr_return_value(rb_execution_context_t *ec,
VALUE tpval)
1289tracepoint_attr_raised_exception(rb_execution_context_t *ec,
VALUE tpval)
1295tracepoint_attr_eval_script(rb_execution_context_t *ec,
VALUE tpval)
1301tracepoint_attr_instruction_sequence(rb_execution_context_t *ec,
VALUE tpval)
1309 rb_tp_t *tp = tpptr(tpval);
1312 (*tp->func)(tpval, tp->data);
1315 if (tp->ractor == GET_RACTOR()) {
1327 if (tp->local_target_set !=
Qfalse) {
1328 rb_raise(rb_eArgError,
"can't nest-enable a targeting TracePoint");
1335 if (tp->target_th) {
1336 rb_thread_add_event_hook2(tp->target_th->self, (
rb_event_hook_func_t)tp_call_trace, tp->events, tpval,
1337 RUBY_EVENT_HOOK_FLAG_SAFE | RUBY_EVENT_HOOK_FLAG_RAW_ARG);
1341 RUBY_EVENT_HOOK_FLAG_SAFE | RUBY_EVENT_HOOK_FLAG_RAW_ARG);
1347static const rb_iseq_t *
1348iseq_of(
VALUE target)
1352 rb_raise(rb_eArgError,
"specified target is not supported");
1355 return rb_iseqw_to_iseq(iseqv);
1359const rb_method_definition_t *rb_method_def(
VALUE method);
1362rb_method_def_local_hooks(rb_method_definition_t *def, rb_ractor_t *cr,
bool create)
1365 rb_hook_list_t *hook_list = NULL;
1366 if (st_lookup(rb_ractor_targeted_hooks(cr), (st_data_t)def, &val)) {
1367 hook_list = (rb_hook_list_t*)val;
1368 RUBY_ASSERT(hook_list->type == hook_list_type_targeted_def);
1371 hook_list =
ZALLOC(rb_hook_list_t);
1372 hook_list->type = hook_list_type_targeted_def;
1373 st_insert(&cr->pub.targeted_hooks, (st_data_t)def, (st_data_t)hook_list);
1380rb_tracepoint_enable_for_target(
VALUE tpval,
VALUE target,
VALUE target_line)
1382 rb_tp_t *tp = tpptr(tpval);
1383 const rb_iseq_t *iseq = iseq_of(target);
1385 unsigned int line = 0;
1386 bool target_bmethod =
false;
1387 rb_ractor_t *cr = GET_RACTOR();
1389 if (tp->tracing > 0) {
1390 rb_raise(rb_eArgError,
"can't nest-enable a targeting TracePoint");
1393 if (!
NIL_P(target_line)) {
1395 rb_raise(rb_eArgError,
"target_line is specified, but line event is not specified");
1402 VM_ASSERT(tp->local_target_set ==
Qfalse);
1411 rb_method_definition_t *def = (rb_method_definition_t *)rb_method_def(target);
1413 rb_hook_list_t *hook_list = rb_method_def_local_hooks(def, cr,
true);
1414 rb_hook_list_connect_local_tracepoint(hook_list, tpval, 0);
1415 rb_hash_aset(tp->local_target_set, target,
Qfalse);
1416 rb_method_definition_addref(def);
1417 def->body.bmethod.local_hooks_cnt++;
1418 target_bmethod =
true;
1424 n += rb_iseq_add_local_tracepoint_recursively(iseq, tp->events & ISEQ_TRACE_EVENTS, tpval, line, target_bmethod);
1426 rb_hash_aset(tp->local_target_set, (
VALUE)iseq,
Qtrue);
1429 ISEQ_BODY(iseq)->builtin_attrs & BUILTIN_ATTR_SINGLE_NOARG_LEAF) {
1433 rb_yjit_tracing_invalidate_all();
1434 rb_zjit_tracing_invalidate_all();
1435 rb_ractor_targeted_hooks_incr(tp->ractor);
1436 if (tp->events & ISEQ_TRACE_EVENTS) {
1437 ruby_vm_iseq_events_enabled++;
1440 ruby_vm_c_events_enabled++;
1447 rb_raise(rb_eArgError,
"can not enable any hooks");
1456 rb_tp_t *tp = tpptr(tpval);
1458 rb_method_definition_t *def;
1459 rb_hook_list_t *hook_list;
1460 ASSERT_vm_locking_with_barrier();
1463 rb_iseq_remove_local_tracepoint_recursively((rb_iseq_t *)target, tpval, tp->ractor);
1468 def = (rb_method_definition_t *)rb_method_def(target);
1469 hook_list = rb_method_def_local_hooks(def, cr,
false);
1471 if (rb_hook_list_remove_local_tracepoint(hook_list, tpval)) {
1472 RUBY_ASSERT(def->body.bmethod.local_hooks_cnt > 0);
1473 def->body.bmethod.local_hooks_cnt--;
1474 if (hook_list->events == 0) {
1475 st_delete(rb_ractor_targeted_hooks(cr), (st_data_t*)&def, NULL);
1476 rb_hook_list_free(hook_list);
1478 rb_method_definition_release(def);
1491 if (
RTEST(tp->local_target_set)) {
1496 rb_hash_foreach(tp->local_target_set, disable_local_tracepoint_i, tpval);
1498 rb_ractor_targeted_hooks_decr(tp->ractor);
1499 if (tp->events & ISEQ_TRACE_EVENTS) {
1501 ruby_vm_iseq_events_enabled--;
1505 ruby_vm_c_events_enabled--;
1510 if (tp->target_th) {
1518 tp->target_th = NULL;
1525rb_hook_list_connect_local_tracepoint(rb_hook_list_t *list,
VALUE tpval,
unsigned int target_line)
1527 rb_tp_t *tp = tpptr(tpval);
1528 rb_event_hook_t *hook = alloc_event_hook((
rb_event_hook_func_t)tp_call_trace, tp->events & ISEQ_TRACE_EVENTS, tpval,
1529 RUBY_EVENT_HOOK_FLAG_SAFE | RUBY_EVENT_HOOK_FLAG_RAW_ARG);
1530 hook->filter.target_line = target_line;
1531 hook_list_connect(list, hook, FALSE);
1535rb_hook_list_remove_local_tracepoint(rb_hook_list_t *list,
VALUE tpval)
1537 rb_event_hook_t *hook = list->hooks;
1539 bool removed =
false;
1542 if (hook->data == tpval) {
1543 hook->hook_flags |= RUBY_EVENT_HOOK_FLAG_DELETED;
1544 list->need_clean =
true;
1547 else if ((hook->hook_flags & RUBY_EVENT_HOOK_FLAG_DELETED) == 0) {
1548 events |= hook->events;
1553 list->events = events;
1558tracepoint_enable_m(rb_execution_context_t *ec,
VALUE tpval,
VALUE target,
VALUE target_line,
VALUE target_thread)
1560 rb_tp_t *tp = tpptr(tpval);
1561 int previous_tracing = tp->tracing;
1563 if (target_thread == sym_default) {
1568 target_thread =
Qnil;
1573 if (
RTEST(target_thread)) {
1574 if (tp->target_th) {
1575 rb_raise(rb_eArgError,
"can not override target_thread filter");
1577 tp->target_th = rb_thread_ptr(target_thread);
1579 RUBY_ASSERT(tp->target_th->self == target_thread);
1583 tp->target_th = NULL;
1586 if (
NIL_P(target)) {
1587 if (!
NIL_P(target_line)) {
1588 rb_raise(rb_eArgError,
"only target_line is specified");
1593 rb_tracepoint_enable_for_target(tpval, target, target_line);
1602 return RBOOL(previous_tracing);
1607tracepoint_disable_m(rb_execution_context_t *ec,
VALUE tpval)
1609 rb_tp_t *tp = tpptr(tpval);
1610 int previous_tracing = tp->tracing;
1613 if (tp->local_target_set !=
Qfalse) {
1614 rb_raise(rb_eArgError,
"can't disable a targeting TracePoint in a block");
1624 return RBOOL(previous_tracing);
1631 rb_tp_t *tp = tpptr(tpval);
1632 return RBOOL(tp->tracing);
1636tracepoint_enabled_p(rb_execution_context_t *ec,
VALUE tpval)
1644 VALUE tpval = tp_alloc(klass);
1649 tp->ractor = GET_RACTOR();
1652 tp->events = events;
1661 rb_thread_t *target_th = NULL;
1663 if (
RTEST(target_thval)) {
1664 target_th = rb_thread_ptr(target_thval);
1666 return tracepoint_new(rb_cTracePoint, target_th, events, func, data,
Qundef);
1670tracepoint_new_s(rb_execution_context_t *ec,
VALUE self,
VALUE args)
1677 for (i=0; i<argc; i++) {
1678 events |= symbol2event_flag(
RARRAY_AREF(args, i));
1682 events = RUBY_EVENT_TRACEPOINT_ALL;
1686 rb_raise(rb_eArgError,
"must be called with a block");
1689 return tracepoint_new(self, 0, events, 0, 0,
rb_block_proc());
1693tracepoint_trace_s(rb_execution_context_t *ec,
VALUE self,
VALUE args)
1695 VALUE trace = tracepoint_new_s(ec, self, args);
1701tracepoint_inspect(rb_execution_context_t *ec,
VALUE self)
1703 rb_tp_t *tp = tpptr(self);
1707 switch (trace_arg->event) {
1713 return rb_sprintf(
"#<TracePoint:%"PRIsVALUE
" %"PRIsVALUE
":%d in '%"PRIsVALUE
"'>",
1723 return rb_sprintf(
"#<TracePoint:%"PRIsVALUE
" '%"PRIsVALUE
"' %"PRIsVALUE
":%d>",
1730 return rb_sprintf(
"#<TracePoint:%"PRIsVALUE
" %"PRIsVALUE
">",
1736 return rb_sprintf(
"#<TracePoint:%"PRIsVALUE
" %"PRIsVALUE
":%d>",
1742 return rb_sprintf(
"#<TracePoint:%s>", tp->tracing ?
"enabled" :
"disabled");
1747tracepoint_stat_event_hooks(
VALUE hash,
VALUE key, rb_event_hook_t *hook)
1749 int active = 0, deleted = 0;
1752 if (hook->hook_flags & RUBY_EVENT_HOOK_FLAG_DELETED) {
1765tracepoint_stat_s(rb_execution_context_t *ec,
VALUE self)
1767 rb_vm_t *vm = GET_VM();
1770 tracepoint_stat_event_hooks(stat, vm->self, rb_ec_ractor_hooks(ec)->hooks);
1776disallow_reentry(
VALUE val)
1779 rb_execution_context_t *ec = GET_EC();
1780 if (ec->trace_arg != NULL) rb_bug(
"should be NULL, but %p", (
void *)ec->trace_arg);
1781 ec->trace_arg = arg;
1786tracepoint_allow_reentry(rb_execution_context_t *ec,
VALUE self)
1789 if (arg == NULL) rb_raise(
rb_eRuntimeError,
"No need to allow reentrance.");
1790 ec->trace_arg = NULL;
1794#include "trace_point.rbinc"
1807 rb_cTracePoint = rb_define_class(
"TracePoint",
rb_cObject);
1818#define PJOB_TABLE_SIZE (sizeof(rb_atomic_t) * CHAR_BIT)
1824 } table[PJOB_TABLE_SIZE];
1828} rb_postponed_job_queues_t;
1830static rb_postponed_job_queues_t postponed_job_queue;
1832static rb_execution_context_t *
1833get_valid_ec(rb_vm_t *vm)
1835 rb_execution_context_t *ec = rb_current_execution_context(
false);
1836 if (ec == NULL) ec = rb_vm_main_ractor_ec(vm);
1841rb_vm_postponed_job_atfork(
void)
1843 rb_postponed_job_queues_t *pjq = &postponed_job_queue;
1848 rb_execution_context_t *ec = get_valid_ec(GET_VM());
1849 if (pjq->triggered_bitset || rb_ec_ractor_ptr(ec)->postponed_job_triggered_bits) {
1850 RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(ec);
1858rb_vm_memsize_postponed_job_queue(
void)
1860 return sizeof(rb_postponed_job_queues_t);
1876 rb_postponed_job_queues_t *pjq = &postponed_job_queue;
1877 for (
unsigned int i = 0; i < PJOB_TABLE_SIZE; i++) {
1880 if (existing_func == NULL || existing_func == func) {
1895 return POSTPONED_JOB_HANDLE_INVALID;
1901 rb_postponed_job_queues_t *pjq = &postponed_job_queue;
1904 RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(get_valid_ec(GET_VM()));
1920rb_postponed_job_trigger_for_ractor(
unsigned int h,
VALUE running_ractor)
1922 VM_ASSERT(rb_ractor_p(running_ractor));
1923 rb_ractor_t *r = (rb_ractor_t *)
DATA_PTR(running_ractor);
1931 if (target_ec == NULL && r->threads.main) {
1932 target_ec = r->threads.main->ec;
1935 RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(target_ec);
1940rb_postponed_job_flush(
void)
1942 rb_postponed_job_queues_t *pjq = &postponed_job_queue;
1943 rb_execution_context_t *ec = GET_EC();
1944 const rb_atomic_t block_mask = POSTPONED_JOB_INTERRUPT_MASK | TRAP_INTERRUPT_MASK;
1945 volatile rb_atomic_t saved_mask = ec->interrupt_mask & block_mask;
1946 VALUE volatile saved_errno = ec->errinfo;
1955 ec->interrupt_mask |= block_mask;
1958 if (EC_EXEC_TAG() == TAG_NONE) {
1960 while (triggered_bits) {
1961 unsigned int i = bit_length(triggered_bits) - 1;
1962 triggered_bits ^= ((1UL) << i);
1973 ec->interrupt_mask &= ~(saved_mask ^ block_mask);
1974 ec->errinfo = saved_errno;
1980 if (triggered_bits) {
1981 RUBY_ATOMIC_OR(rb_ec_ractor_ptr(ec)->postponed_job_triggered_bits, triggered_bits);
1982 RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(GET_EC());
#define RUBY_ATOMIC_OR(var, val)
Atomically replaces the value pointed by var with the result of bitwise OR between val and the old va...
#define RUBY_ATOMIC_PTR_LOAD(var)
Identical to RUBY_ATOMIC_LOAD, except it expects its arguments are void*.
#define RUBY_ATOMIC_PTR_CAS(var, oldval, newval)
Identical to RUBY_ATOMIC_CAS, except it expects its arguments are void*.
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
#define RUBY_ATOMIC_PTR_EXCHANGE(var, val)
Identical to RUBY_ATOMIC_EXCHANGE, except it expects its arguments are void*.
#define RUBY_ATOMIC_EXCHANGE(var, val)
Atomically replaces the value pointed by var with val.
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
unsigned int rb_postponed_job_handle_t
The type of a handle returned from rb_postponed_job_preregister and passed to rb_postponed_job_trigge...
VALUE rb_tracearg_binding(rb_trace_arg_t *trace_arg)
Creates a binding object of the point where the trace is at.
VALUE rb_tracearg_parameters(rb_trace_arg_t *trace_arg)
Queries the parameters passed on a call or return event.
VALUE rb_tracearg_instruction_sequence(rb_trace_arg_t *trace_arg)
Queries the compiled instruction sequence on a 'script_compiled' event.
void rb_postponed_job_trigger(rb_postponed_job_handle_t h)
Triggers a pre-registered job registered with rb_postponed_job_preregister, scheduling it for executi...
VALUE rb_tracepoint_enabled_p(VALUE tpval)
Queries if the passed TracePoint is up and running.
VALUE rb_tracearg_object(rb_trace_arg_t *trace_arg)
Queries the allocated/deallocated object that the trace represents.
VALUE rb_tracearg_callee_id(rb_trace_arg_t *trace_arg)
Identical to rb_tracearg_method_id(), except it returns callee id like rb_frame_callee().
VALUE rb_tracearg_defined_class(rb_trace_arg_t *trace_arg)
Queries the class that defines the method that the passed trace is at.
VALUE rb_tracepoint_new(VALUE target_thread_not_supported_yet, rb_event_flag_t events, void(*func)(VALUE, void *), void *data)
Creates a tracepoint by registering a callback function for one or more tracepoint events.
struct rb_trace_arg_struct rb_trace_arg_t
Type that represents a specific trace event.
VALUE rb_tracearg_raised_exception(rb_trace_arg_t *trace_arg)
Queries the raised exception that the trace represents.
void rb_thread_add_event_hook(VALUE thval, rb_event_hook_func_t func, rb_event_flag_t events, VALUE data)
Identical to rb_add_event_hook(), except its effect is limited to the passed thread.
rb_postponed_job_handle_t rb_postponed_job_preregister(unsigned int flags, rb_postponed_job_func_t func, void *data)
Pre-registers a func in Ruby's postponed job preregistration table, returning an opaque handle which ...
VALUE rb_tracepoint_disable(VALUE tpval)
Stops (disables) an already running instance of TracePoint.
VALUE rb_tracearg_self(rb_trace_arg_t *trace_arg)
Queries the receiver of the point trace is at.
int rb_thread_remove_event_hook(VALUE thval, rb_event_hook_func_t func)
Identical to rb_remove_event_hook(), except it additionally takes a thread argument.
VALUE rb_tracearg_return_value(rb_trace_arg_t *trace_arg)
Queries the return value that the trace represents.
rb_event_flag_t rb_tracearg_event_flag(rb_trace_arg_t *trace_arg)
Queries the event of the passed trace.
VALUE rb_tracearg_path(rb_trace_arg_t *trace_arg)
Queries the file name of the point where the trace is at.
VALUE rb_tracearg_eval_script(rb_trace_arg_t *trace_arg)
Queries the compiled source code of the 'script_compiled' event.
int rb_thread_remove_event_hook_with_data(VALUE thval, rb_event_hook_func_t func, VALUE data)
Identical to rb_thread_remove_event_hook(), except it additionally takes the data argument.
VALUE rb_tracepoint_enable(VALUE tpval)
Starts (enables) trace(s) defined by the passed object.
VALUE rb_tracearg_method_id(rb_trace_arg_t *trace_arg)
Queries the method name of the point where the trace is at.
int rb_remove_event_hook_with_data(rb_event_hook_func_t func, VALUE data)
Identical to rb_remove_event_hook(), except it additionally takes the data argument.
rb_trace_arg_t * rb_tracearg_from_tracepoint(VALUE tpval)
Queries the current event of the passed tracepoint.
VALUE rb_tracearg_lineno(rb_trace_arg_t *trace_arg)
Queries the line of the point where the trace is at.
void(* rb_postponed_job_func_t)(void *arg)
Type of postponed jobs.
VALUE rb_tracearg_event(rb_trace_arg_t *trace_arg)
Identical to rb_tracearg_event_flag(), except it returns the name of the event in Ruby's symbol.
#define RUBY_EVENT_END
Encountered an end of a class clause.
#define RUBY_EVENT_C_CALL
A method, written in C, is called.
void rb_add_event_hook(rb_event_hook_func_t func, rb_event_flag_t events, VALUE data)
Registers an event hook function.
#define RUBY_EVENT_RAISE
Encountered a raise statement.
#define RUBY_EVENT_B_RETURN
Encountered a next statement.
#define RUBY_EVENT_SCRIPT_COMPILED
Encountered an eval.
#define RUBY_INTERNAL_EVENT_MASK
Bitmask of internal events.
int rb_remove_event_hook(rb_event_hook_func_t func)
Removes the passed function from the list of event hooks.
#define RUBY_EVENT_ALL
Bitmask of traditional events.
#define RUBY_EVENT_THREAD_BEGIN
Encountered a new thread.
#define RUBY_EVENT_CLASS
Encountered a new class.
void(* rb_event_hook_func_t)(rb_event_flag_t evflag, VALUE data, VALUE self, ID mid, VALUE klass)
Type of event hooks.
#define RUBY_INTERNAL_EVENT_OBJSPACE_MASK
Bitmask of GC events.
#define RUBY_EVENT_LINE
Encountered a new line.
#define RUBY_EVENT_RETURN
Encountered a return statement.
#define RUBY_EVENT_C_RETURN
Return from a method, written in C.
#define RUBY_EVENT_B_CALL
Encountered an yield statement.
#define RUBY_INTERNAL_EVENT_FREEOBJ
Object swept.
uint32_t rb_event_flag_t
Represents event(s).
#define RUBY_EVENT_CALL
A method, written in Ruby, is called.
#define RUBY_INTERNAL_EVENT_NEWOBJ
Object allocated.
#define RUBY_EVENT_THREAD_END
Encountered an end of a thread.
#define RUBY_EVENT_RESCUE
Encountered a rescue statement.
int rb_block_given_p(void)
Determines if the current method is given a block.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define ALLOC
Old name of RB_ALLOC.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define ID2SYM
Old name of RB_ID2SYM.
#define ZALLOC
Old name of RB_ZALLOC.
#define FIX2INT
Old name of RB_FIX2INT.
#define NUM2UINT
Old name of RB_NUM2UINT.
#define T_ICLASS
Old name of RUBY_T_ICLASS.
#define rb_ary_new3
Old name of rb_ary_new_from_args.
#define Qtrue
Old name of RUBY_Qtrue.
#define INT2NUM
Old name of RB_INT2NUM.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define NIL_P
Old name of RB_NIL_P.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eRuntimeError
RuntimeError exception.
VALUE rb_cObject
Object class.
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
VALUE rb_cThread
Thread class.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_hash_new(void)
Creates a new, empty hash object.
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
VALUE rb_proc_call_with_block(VALUE recv, int argc, const VALUE *argv, VALUE proc)
Identical to rb_proc_call(), except you can additionally pass another proc object,...
VALUE rb_obj_is_method(VALUE recv)
Queries if the given object is a method.
VALUE rb_binding_new(void)
Snapshots the current execution context and turn it into an instance of rb_cBinding.
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
VALUE rb_thread_current(void)
Obtains the "current" thread.
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
VALUE rb_yield(VALUE val)
Yields the block.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
void rb_hash_foreach(VALUE q, int_type *w, VALUE e)
Iteration over the given hash.
VALUE rb_ensure(type *q, VALUE w, type *e, VALUE r)
An equivalent of ensure clause.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
References an object in an array.
#define RBASIC(obj)
Convenient casting macro.
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
#define DATA_PTR(obj)
Convenient casting macro for backward compatibility.
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
#define RTEST
This is an old name of RB_TEST.
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.