Ruby: vm_trace.c Source File

Ruby 3.4.9p82 (2026-03-11 revision 76cca827ab52ab1d346a728f068d5b8da3e2952b)

vm_trace.c
1/**********************************************************************
2
3 vm_trace.c -
4
5 $Author: ko1 $
6 created at: Tue Aug 14 19:37:09 2012
7
8 Copyright (C) 1993-2012 Yukihiro Matsumoto
9
10**********************************************************************/
11
12/*
13 * This file include two parts:
14 *
15 * (1) set_trace_func internal mechanisms
16 * and C level API
17 *
18 * (2) Ruby level API
19 * (2-1) set_trace_func API
20 * (2-2) TracePoint API (not yet)
21 *
22 */
23
24#include "eval_intern.h"
25#include "internal.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"
32#include "iseq.h"
33#include "rjit.h"
34#include "ruby/atomic.h"
35#include "ruby/debug.h"
36#include "vm_core.h"
37#include "ruby/ractor.h"
38#include "yjit.h"
39
40#include "builtin.h"
41
42static VALUE sym_default;
43
44/* (1) trace mechanisms */
45
46 typedef struct rb_event_hook_struct {
47 rb_event_hook_flag_t hook_flags;
48 rb_event_flag_t events;
49 rb_event_hook_func_t func;
50 VALUE data;
51 struct rb_event_hook_struct *next;
52
53 struct {
54 rb_thread_t *th;
55 unsigned int target_line;
56 } filter;
57} rb_event_hook_t;
58
59typedef void (*rb_event_hook_raw_arg_func_t)(VALUE data, const rb_trace_arg_t *arg);
60
61#define MAX_EVENT_NUM 32
62
63void
64rb_hook_list_mark(rb_hook_list_t *hooks)
65{
66 rb_event_hook_t *hook = hooks->hooks;
67
68 while (hook) {
69 rb_gc_mark(hook->data);
70 hook = hook->next;
71 }
72}
73
74void
75rb_hook_list_mark_and_update(rb_hook_list_t *hooks)
76{
77 rb_event_hook_t *hook = hooks->hooks;
78
79 while (hook) {
80 rb_gc_mark_and_move(&hook->data);
81 hook = hook->next;
82 }
83}
84
85static void clean_hooks(rb_hook_list_t *list);
86
87void
88rb_hook_list_free(rb_hook_list_t *hooks)
89{
90 hooks->need_clean = true;
91
92 if (hooks->running == 0) {
93 clean_hooks(hooks);
94 }
95}
96
97/* ruby_vm_event_flags management */
98
99void rb_clear_attr_ccs(void);
100void rb_clear_bf_ccs(void);
101
102static void
103update_global_event_hook(rb_event_flag_t prev_events, rb_event_flag_t new_events)
104{
105 rb_event_flag_t new_iseq_events = new_events & ISEQ_TRACE_EVENTS;
106 rb_event_flag_t enabled_iseq_events = ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS;
107 bool first_time_iseq_events_p = new_iseq_events & ~enabled_iseq_events;
108 bool enable_c_call = (prev_events & RUBY_EVENT_C_CALL) == 0 && (new_events & RUBY_EVENT_C_CALL);
109 bool enable_c_return = (prev_events & RUBY_EVENT_C_RETURN) == 0 && (new_events & RUBY_EVENT_C_RETURN);
110 bool enable_call = (prev_events & RUBY_EVENT_CALL) == 0 && (new_events & RUBY_EVENT_CALL);
111 bool enable_return = (prev_events & RUBY_EVENT_RETURN) == 0 && (new_events & RUBY_EVENT_RETURN);
112
113 // Modify ISEQs or CCs to enable tracing
114 if (first_time_iseq_events_p) {
115 // write all ISeqs only when new events are added for the first time
116 rb_iseq_trace_set_all(new_iseq_events | enabled_iseq_events);
117 }
118 // if c_call or c_return is activated
119 else if (enable_c_call || enable_c_return) {
120 rb_clear_attr_ccs();
121 }
122 else if (enable_call || enable_return) {
123 rb_clear_bf_ccs();
124 }
125
126 ruby_vm_event_flags = new_events;
127 ruby_vm_event_enabled_global_flags |= new_events;
128 rb_objspace_set_event_hook(new_events);
129
130 // Invalidate JIT code as needed
131 if (first_time_iseq_events_p || enable_c_call || enable_c_return) {
132 // Invalidate all code when ISEQs are modified to use trace_* insns above.
133 // Also invalidate when enabling c_call or c_return because generated code
134 // never fires these events.
135 // Internal events fire inside C routines so don't need special handling.
136 // Do this after event flags updates so other ractors see updated vm events
137 // when they wake up.
138 rb_yjit_tracing_invalidate_all();
139 rb_rjit_tracing_invalidate_all(new_iseq_events);
140 }
141}
142
143/* add/remove hooks */
144
145static rb_event_hook_t *
146alloc_event_hook(rb_event_hook_func_t func, rb_event_flag_t events, VALUE data, rb_event_hook_flag_t hook_flags)
147{
148 rb_event_hook_t *hook;
149
150 if ((events & RUBY_INTERNAL_EVENT_MASK) && (events & ~RUBY_INTERNAL_EVENT_MASK)) {
151 rb_raise(rb_eTypeError, "Can not specify normal event and internal event simultaneously.");
152 }
153
154 hook = ALLOC(rb_event_hook_t);
155 hook->hook_flags = hook_flags;
156 hook->events = events;
157 hook->func = func;
158 hook->data = data;
159
160 /* no filters */
161 hook->filter.th = NULL;
162 hook->filter.target_line = 0;
163
164 return hook;
165}
166
167static void
168hook_list_connect(VALUE list_owner, rb_hook_list_t *list, rb_event_hook_t *hook, int global_p)
169{
170 rb_event_flag_t prev_events = list->events;
171 hook->next = list->hooks;
172 list->hooks = hook;
173 list->events |= hook->events;
174
175 if (global_p) {
176 /* global hooks are root objects at GC mark. */
177 update_global_event_hook(prev_events, list->events);
178 }
179 else {
180 RB_OBJ_WRITTEN(list_owner, Qundef, hook->data);
181 }
182}
183
184static void
185connect_event_hook(const rb_execution_context_t *ec, rb_event_hook_t *hook)
186{
187 rb_hook_list_t *list = rb_ec_ractor_hooks(ec);
188 hook_list_connect(Qundef, list, hook, TRUE);
189}
190
191static void
192rb_threadptr_add_event_hook(const rb_execution_context_t *ec, rb_thread_t *th,
193 rb_event_hook_func_t func, rb_event_flag_t events, VALUE data, rb_event_hook_flag_t hook_flags)
194{
195 rb_event_hook_t *hook = alloc_event_hook(func, events, data, hook_flags);
196 hook->filter.th = th;
197 connect_event_hook(ec, hook);
198}
199
200void
201rb_thread_add_event_hook(VALUE thval, rb_event_hook_func_t func, rb_event_flag_t events, VALUE data)
202{
203 rb_threadptr_add_event_hook(GET_EC(), rb_thread_ptr(thval), func, events, data, RUBY_EVENT_HOOK_FLAG_SAFE);
204}
205
206void
207rb_add_event_hook(rb_event_hook_func_t func, rb_event_flag_t events, VALUE data)
208{
209 rb_add_event_hook2(func, events, data, RUBY_EVENT_HOOK_FLAG_SAFE);
210}
211
212void
213rb_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)
214{
215 rb_threadptr_add_event_hook(GET_EC(), rb_thread_ptr(thval), func, events, data, hook_flags);
216}
217
218void
219rb_add_event_hook2(rb_event_hook_func_t func, rb_event_flag_t events, VALUE data, rb_event_hook_flag_t hook_flags)
220{
221 rb_event_hook_t *hook = alloc_event_hook(func, events, data, hook_flags);
222 connect_event_hook(GET_EC(), hook);
223}
224
225static void
226clean_hooks(rb_hook_list_t *list)
227{
228 rb_event_hook_t *hook, **nextp = &list->hooks;
229 rb_event_flag_t prev_events = list->events;
230
231 VM_ASSERT(list->running == 0);
232 VM_ASSERT(list->need_clean == true);
233
234 list->events = 0;
235 list->need_clean = false;
236
237 while ((hook = *nextp) != 0) {
238 if (hook->hook_flags & RUBY_EVENT_HOOK_FLAG_DELETED) {
239 *nextp = hook->next;
240 xfree(hook);
241 }
242 else {
243 list->events |= hook->events; /* update active events */
244 nextp = &hook->next;
245 }
246 }
247
248 if (list->is_local) {
249 if (list->events == 0) {
250 /* local events */
251 ruby_xfree(list);
252 }
253 }
254 else {
255 update_global_event_hook(prev_events, list->events);
256 }
257}
258
259static void
260clean_hooks_check(rb_hook_list_t *list)
261{
262 if (UNLIKELY(list->need_clean)) {
263 if (list->running == 0) {
264 clean_hooks(list);
265 }
266 }
267}
268
269#define MATCH_ANY_FILTER_TH ((rb_thread_t *)1)
270
271/* if func is 0, then clear all funcs */
272static int
273remove_event_hook(const rb_execution_context_t *ec, const rb_thread_t *filter_th, rb_event_hook_func_t func, VALUE data)
274{
275 rb_hook_list_t *list = rb_ec_ractor_hooks(ec);
276 int ret = 0;
277 rb_event_hook_t *hook = list->hooks;
278
279 while (hook) {
280 if (func == 0 || hook->func == func) {
281 if (hook->filter.th == filter_th || filter_th == MATCH_ANY_FILTER_TH) {
282 if (UNDEF_P(data) || hook->data == data) {
283 hook->hook_flags |= RUBY_EVENT_HOOK_FLAG_DELETED;
284 ret+=1;
285 list->need_clean = true;
286 }
287 }
288 }
289 hook = hook->next;
290 }
291
292 clean_hooks_check(list);
293 return ret;
294}
295
296static int
297rb_threadptr_remove_event_hook(const rb_execution_context_t *ec, const rb_thread_t *filter_th, rb_event_hook_func_t func, VALUE data)
298{
299 return remove_event_hook(ec, filter_th, func, data);
300}
301
302int
303rb_thread_remove_event_hook(VALUE thval, rb_event_hook_func_t func)
304{
305 return rb_threadptr_remove_event_hook(GET_EC(), rb_thread_ptr(thval), func, Qundef);
306}
307
308int
309rb_thread_remove_event_hook_with_data(VALUE thval, rb_event_hook_func_t func, VALUE data)
310{
311 return rb_threadptr_remove_event_hook(GET_EC(), rb_thread_ptr(thval), func, data);
312}
313
314int
315rb_remove_event_hook(rb_event_hook_func_t func)
316{
317 return remove_event_hook(GET_EC(), NULL, func, Qundef);
318}
319
320int
321rb_remove_event_hook_with_data(rb_event_hook_func_t func, VALUE data)
322{
323 return remove_event_hook(GET_EC(), NULL, func, data);
324}
325
326void
327rb_ec_clear_current_thread_trace_func(const rb_execution_context_t *ec)
328{
329 rb_threadptr_remove_event_hook(ec, rb_ec_thread_ptr(ec), 0, Qundef);
330}
331
332void
333rb_ec_clear_all_trace_func(const rb_execution_context_t *ec)
334{
335 rb_threadptr_remove_event_hook(ec, MATCH_ANY_FILTER_TH, 0, Qundef);
336}
337
338/* invoke hooks */
339
340static void
341exec_hooks_body(const rb_execution_context_t *ec, rb_hook_list_t *list, const rb_trace_arg_t *trace_arg)
342{
343 rb_event_hook_t *hook;
344
345 for (hook = list->hooks; hook; hook = hook->next) {
346 if (!(hook->hook_flags & RUBY_EVENT_HOOK_FLAG_DELETED) &&
347 (trace_arg->event & hook->events) &&
348 (LIKELY(hook->filter.th == 0) || hook->filter.th == rb_ec_thread_ptr(ec)) &&
349 (LIKELY(hook->filter.target_line == 0) || (hook->filter.target_line == (unsigned int)rb_vm_get_sourceline(ec->cfp)))) {
350 if (!(hook->hook_flags & RUBY_EVENT_HOOK_FLAG_RAW_ARG)) {
351 (*hook->func)(trace_arg->event, hook->data, trace_arg->self, trace_arg->id, trace_arg->klass);
352 }
353 else {
354 (*((rb_event_hook_raw_arg_func_t)hook->func))(hook->data, trace_arg);
355 }
356 }
357 }
358}
359
360static int
361exec_hooks_precheck(const rb_execution_context_t *ec, rb_hook_list_t *list, const rb_trace_arg_t *trace_arg)
362{
363 if (list->events & trace_arg->event) {
364 list->running++;
365 return TRUE;
366 }
367 else {
368 return FALSE;
369 }
370}
371
372static void
373exec_hooks_postcheck(const rb_execution_context_t *ec, rb_hook_list_t *list)
374{
375 list->running--;
376 clean_hooks_check(list);
377}
378
379static void
380exec_hooks_unprotected(const rb_execution_context_t *ec, rb_hook_list_t *list, const rb_trace_arg_t *trace_arg)
381{
382 if (exec_hooks_precheck(ec, list, trace_arg) == 0) return;
383 exec_hooks_body(ec, list, trace_arg);
384 exec_hooks_postcheck(ec, list);
385}
386
387static int
388exec_hooks_protected(rb_execution_context_t *ec, rb_hook_list_t *list, const rb_trace_arg_t *trace_arg)
389{
390 enum ruby_tag_type state;
391 volatile int raised;
392
393 if (exec_hooks_precheck(ec, list, trace_arg) == 0) return 0;
394
395 raised = rb_ec_reset_raised(ec);
396
397 /* TODO: Support !RUBY_EVENT_HOOK_FLAG_SAFE hooks */
398
399 EC_PUSH_TAG(ec);
400 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
401 exec_hooks_body(ec, list, trace_arg);
402 }
403 EC_POP_TAG();
404
405 exec_hooks_postcheck(ec, list);
406
407 if (raised) {
408 rb_ec_set_raised(ec);
409 }
410
411 return state;
412}
413
414// pop_p: Whether to pop the frame for the TracePoint when it throws.
415void
416rb_exec_event_hooks(rb_trace_arg_t *trace_arg, rb_hook_list_t *hooks, int pop_p)
417{
418 rb_execution_context_t *ec = trace_arg->ec;
419
420 if (UNLIKELY(trace_arg->event & RUBY_INTERNAL_EVENT_MASK)) {
421 if (ec->trace_arg && (ec->trace_arg->event & RUBY_INTERNAL_EVENT_MASK)) {
422 /* skip hooks because this thread doing INTERNAL_EVENT */
423 }
424 else {
425 rb_trace_arg_t *prev_trace_arg = ec->trace_arg;
426
427 ec->trace_arg = trace_arg;
428 /* only global hooks */
429 exec_hooks_unprotected(ec, rb_ec_ractor_hooks(ec), trace_arg);
430 ec->trace_arg = prev_trace_arg;
431 }
432 }
433 else {
434 if (ec->trace_arg == NULL && /* check reentrant */
435 trace_arg->self != rb_mRubyVMFrozenCore /* skip special methods. TODO: remove it. */) {
436 const VALUE errinfo = ec->errinfo;
437 const VALUE old_recursive = ec->local_storage_recursive_hash;
438 enum ruby_tag_type state = 0;
439
440 /* setup */
441 ec->local_storage_recursive_hash = ec->local_storage_recursive_hash_for_trace;
442 ec->errinfo = Qnil;
443 ec->trace_arg = trace_arg;
444
445 /* kick hooks */
446 if ((state = exec_hooks_protected(ec, hooks, trace_arg)) == TAG_NONE) {
447 ec->errinfo = errinfo;
448 }
449
450 /* cleanup */
451 ec->trace_arg = NULL;
452 ec->local_storage_recursive_hash_for_trace = ec->local_storage_recursive_hash;
453 ec->local_storage_recursive_hash = old_recursive;
454
455 if (state) {
456 if (pop_p) {
457 if (VM_FRAME_FINISHED_P(ec->cfp)) {
458 rb_vm_tag_jmpbuf_deinit(&ec->tag->buf);
459 ec->tag = ec->tag->prev;
460 }
461 rb_vm_pop_frame(ec);
462 }
463 EC_JUMP_TAG(ec, state);
464 }
465 }
466 }
467}
468
469 VALUE
470rb_suppress_tracing(VALUE (*func)(VALUE), VALUE arg)
471{
472 volatile int raised;
473 volatile VALUE result = Qnil;
474 rb_execution_context_t *const ec = GET_EC();
475 rb_vm_t *const vm = rb_ec_vm_ptr(ec);
476 enum ruby_tag_type state;
477 rb_trace_arg_t dummy_trace_arg;
478 dummy_trace_arg.event = 0;
479
480 if (!ec->trace_arg) {
481 ec->trace_arg = &dummy_trace_arg;
482 }
483
484 raised = rb_ec_reset_raised(ec);
485
486 EC_PUSH_TAG(ec);
487 if (LIKELY((state = EC_EXEC_TAG()) == TAG_NONE)) {
488 result = (*func)(arg);
489 }
490 else {
491 (void)*&vm; /* suppress "clobbered" warning */
492 }
493 EC_POP_TAG();
494
495 if (raised) {
496 rb_ec_reset_raised(ec);
497 }
498
499 if (ec->trace_arg == &dummy_trace_arg) {
500 ec->trace_arg = NULL;
501 }
502
503 if (state) {
504#if defined RUBY_USE_SETJMPEX && RUBY_USE_SETJMPEX
505 RB_GC_GUARD(result);
506#endif
507 EC_JUMP_TAG(ec, state);
508 }
509
510 return result;
511}
512
513static void call_trace_func(rb_event_flag_t, VALUE data, VALUE self, ID id, VALUE klass);
514
515/* (2-1) set_trace_func (old API) */
516
517/*
518 * call-seq:
519 * set_trace_func(proc) -> proc
520 * set_trace_func(nil) -> nil
521 *
522 * Establishes _proc_ as the handler for tracing, or disables
523 * tracing if the parameter is +nil+.
524 *
525 * *Note:* this method is obsolete, please use TracePoint instead.
526 *
527 * _proc_ takes up to six parameters:
528 *
529 * * an event name string
530 * * a filename string
531 * * a line number
532 * * a method name symbol, or nil
533 * * a binding, or nil
534 * * the class, module, or nil
535 *
536 * _proc_ is invoked whenever an event occurs.
537 *
538 * Events are:
539 *
540 * <code>"c-call"</code>:: call a C-language routine
541 * <code>"c-return"</code>:: return from a C-language routine
542 * <code>"call"</code>:: call a Ruby method
543 * <code>"class"</code>:: start a class or module definition
544 * <code>"end"</code>:: finish a class or module definition
545 * <code>"line"</code>:: execute code on a new line
546 * <code>"raise"</code>:: raise an exception
547 * <code>"return"</code>:: return from a Ruby method
548 *
549 * Tracing is disabled within the context of _proc_.
550 *
551 * class Test
552 * def test
553 * a = 1
554 * b = 2
555 * end
556 * end
557 *
558 * set_trace_func proc { |event, file, line, id, binding, class_or_module|
559 * printf "%8s %s:%-2d %16p %14p\n", event, file, line, id, class_or_module
560 * }
561 * t = Test.new
562 * t.test
563 *
564 * Produces:
565 *
566 * c-return prog.rb:8 :set_trace_func Kernel
567 * line prog.rb:11 nil nil
568 * c-call prog.rb:11 :new Class
569 * c-call prog.rb:11 :initialize BasicObject
570 * c-return prog.rb:11 :initialize BasicObject
571 * c-return prog.rb:11 :new Class
572 * line prog.rb:12 nil nil
573 * call prog.rb:2 :test Test
574 * line prog.rb:3 :test Test
575 * line prog.rb:4 :test Test
576 * return prog.rb:5 :test Test
577 */
578
579static VALUE
580set_trace_func(VALUE obj, VALUE trace)
581{
582 rb_remove_event_hook(call_trace_func);
583
584 if (NIL_P(trace)) {
585 return Qnil;
586 }
587
588 if (!rb_obj_is_proc(trace)) {
589 rb_raise(rb_eTypeError, "trace_func needs to be Proc");
590 }
591
592 rb_add_event_hook(call_trace_func, RUBY_EVENT_ALL, trace);
593 return trace;
594}
595
596static void
597thread_add_trace_func(rb_execution_context_t *ec, rb_thread_t *filter_th, VALUE trace)
598{
599 if (!rb_obj_is_proc(trace)) {
600 rb_raise(rb_eTypeError, "trace_func needs to be Proc");
601 }
602
603 rb_threadptr_add_event_hook(ec, filter_th, call_trace_func, RUBY_EVENT_ALL, trace, RUBY_EVENT_HOOK_FLAG_SAFE);
604}
605
606/*
607 * call-seq:
608 * thr.add_trace_func(proc) -> proc
609 *
610 * Adds _proc_ as a handler for tracing.
611 *
612 * See Thread#set_trace_func and Kernel#set_trace_func.
613 */
614
615static VALUE
616thread_add_trace_func_m(VALUE obj, VALUE trace)
617{
618 thread_add_trace_func(GET_EC(), rb_thread_ptr(obj), trace);
619 return trace;
620}
621
622/*
623 * call-seq:
624 * thr.set_trace_func(proc) -> proc
625 * thr.set_trace_func(nil) -> nil
626 *
627 * Establishes _proc_ on _thr_ as the handler for tracing, or
628 * disables tracing if the parameter is +nil+.
629 *
630 * See Kernel#set_trace_func.
631 */
632
633static VALUE
634thread_set_trace_func_m(VALUE target_thread, VALUE trace)
635{
636 rb_execution_context_t *ec = GET_EC();
637 rb_thread_t *target_th = rb_thread_ptr(target_thread);
638
639 rb_threadptr_remove_event_hook(ec, target_th, call_trace_func, Qundef);
640
641 if (NIL_P(trace)) {
642 return Qnil;
643 }
644 else {
645 thread_add_trace_func(ec, target_th, trace);
646 return trace;
647 }
648}
649
650static const char *
651get_event_name(rb_event_flag_t event)
652{
653 switch (event) {
654 case RUBY_EVENT_LINE: return "line";
655 case RUBY_EVENT_CLASS: return "class";
656 case RUBY_EVENT_END: return "end";
657 case RUBY_EVENT_CALL: return "call";
658 case RUBY_EVENT_RETURN: return "return";
659 case RUBY_EVENT_C_CALL: return "c-call";
660 case RUBY_EVENT_C_RETURN: return "c-return";
661 case RUBY_EVENT_RAISE: return "raise";
662 default:
663 return "unknown";
664 }
665}
666
667static ID
668get_event_id(rb_event_flag_t event)
669{
670 ID id;
671
672 switch (event) {
673#define C(name, NAME) case RUBY_EVENT_##NAME: CONST_ID(id, #name); return id;
674 C(line, LINE);
675 C(class, CLASS);
676 C(end, END);
677 C(call, CALL);
678 C(return, RETURN);
679 C(c_call, C_CALL);
680 C(c_return, C_RETURN);
681 C(raise, RAISE);
682 C(b_call, B_CALL);
683 C(b_return, B_RETURN);
684 C(thread_begin, THREAD_BEGIN);
685 C(thread_end, THREAD_END);
686 C(fiber_switch, FIBER_SWITCH);
687 C(script_compiled, SCRIPT_COMPILED);
688 C(rescue, RESCUE);
689#undef C
690 default:
691 return 0;
692 }
693}
694
695static void
696get_path_and_lineno(const rb_execution_context_t *ec, const rb_control_frame_t *cfp, rb_event_flag_t event, VALUE *pathp, int *linep)
697{
698 cfp = rb_vm_get_ruby_level_next_cfp(ec, cfp);
699
700 if (cfp) {
701 const rb_iseq_t *iseq = cfp->iseq;
702 *pathp = rb_iseq_path(iseq);
703
704 if (event & (RUBY_EVENT_CLASS |
705 RUBY_EVENT_CALL |
706 RUBY_EVENT_B_CALL)) {
707 *linep = FIX2INT(rb_iseq_first_lineno(iseq));
708 }
709 else {
710 *linep = rb_vm_get_sourceline(cfp);
711 }
712 }
713 else {
714 *pathp = Qnil;
715 *linep = 0;
716 }
717}
718
719static void
720call_trace_func(rb_event_flag_t event, VALUE proc, VALUE self, ID id, VALUE klass)
721{
722 int line;
723 VALUE filename;
724 VALUE eventname = rb_str_new2(get_event_name(event));
725 VALUE argv[6];
726 const rb_execution_context_t *ec = GET_EC();
727
728 get_path_and_lineno(ec, ec->cfp, event, &filename, &line);
729
730 if (!klass) {
731 rb_ec_frame_method_id_and_class(ec, &id, 0, &klass);
732 }
733
734 if (klass) {
735 if (RB_TYPE_P(klass, T_ICLASS)) {
736 klass = RBASIC(klass)->klass;
737 }
738 else if (RCLASS_SINGLETON_P(klass)) {
739 klass = RCLASS_ATTACHED_OBJECT(klass);
740 }
741 }
742
743 argv[0] = eventname;
744 argv[1] = filename;
745 argv[2] = INT2FIX(line);
746 argv[3] = id ? ID2SYM(id) : Qnil;
747 argv[4] = Qnil;
748 if (self && (filename != Qnil) &&
749 event != RUBY_EVENT_C_CALL &&
750 event != RUBY_EVENT_C_RETURN &&
751 (VM_FRAME_RUBYFRAME_P(ec->cfp) && imemo_type_p((VALUE)ec->cfp->iseq, imemo_iseq))) {
752 argv[4] = rb_binding_new();
753 }
754 argv[5] = klass ? klass : Qnil;
755
756 rb_proc_call_with_block(proc, 6, argv, Qnil);
757}
758
759/* (2-2) TracePoint API */
760
761static VALUE rb_cTracePoint;
762
763 typedef struct rb_tp_struct {
764 rb_event_flag_t events;
765 int tracing; /* bool */
766 rb_thread_t *target_th;
767 VALUE local_target_set; /* Hash: target ->
768 * Qtrue (if target is iseq) or
769 * Qfalse (if target is bmethod)
770 */
771 void (*func)(VALUE tpval, void *data);
772 void *data;
773 VALUE proc;
774 rb_ractor_t *ractor;
775 VALUE self;
776} rb_tp_t;
777
778static void
779tp_mark(void *ptr)
780{
781 rb_tp_t *tp = ptr;
782 rb_gc_mark(tp->proc);
783 rb_gc_mark(tp->local_target_set);
784 if (tp->target_th) rb_gc_mark(tp->target_th->self);
785}
786
787static const rb_data_type_t tp_data_type = {
788 "tracepoint",
789 {
790 tp_mark,
791 RUBY_TYPED_DEFAULT_FREE,
792 NULL, // Nothing allocated externally, so don't need a memsize function
793 },
794 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
795};
796
797static VALUE
798tp_alloc(VALUE klass)
799{
800 rb_tp_t *tp;
801 return TypedData_Make_Struct(klass, rb_tp_t, &tp_data_type, tp);
802}
803
804static rb_event_flag_t
805symbol2event_flag(VALUE v)
806{
807 ID id;
808 VALUE sym = rb_to_symbol_type(v);
809 const rb_event_flag_t RUBY_EVENT_A_CALL =
810 RUBY_EVENT_CALL | RUBY_EVENT_B_CALL | RUBY_EVENT_C_CALL;
811 const rb_event_flag_t RUBY_EVENT_A_RETURN =
812 RUBY_EVENT_RETURN | RUBY_EVENT_B_RETURN | RUBY_EVENT_C_RETURN;
813
814#define C(name, NAME) CONST_ID(id, #name); if (sym == ID2SYM(id)) return RUBY_EVENT_##NAME
815 C(line, LINE);
816 C(class, CLASS);
817 C(end, END);
818 C(call, CALL);
819 C(return, RETURN);
820 C(c_call, C_CALL);
821 C(c_return, C_RETURN);
822 C(raise, RAISE);
823 C(b_call, B_CALL);
824 C(b_return, B_RETURN);
825 C(thread_begin, THREAD_BEGIN);
826 C(thread_end, THREAD_END);
827 C(fiber_switch, FIBER_SWITCH);
828 C(script_compiled, SCRIPT_COMPILED);
829 C(rescue, RESCUE);
830
831 /* joke */
832 C(a_call, A_CALL);
833 C(a_return, A_RETURN);
834#undef C
835 rb_raise(rb_eArgError, "unknown event: %"PRIsVALUE, rb_sym2str(sym));
836}
837
838static rb_tp_t *
839tpptr(VALUE tpval)
840{
841 rb_tp_t *tp;
842 TypedData_Get_Struct(tpval, rb_tp_t, &tp_data_type, tp);
843 return tp;
844}
845
846static rb_trace_arg_t *
847get_trace_arg(void)
848{
849 rb_trace_arg_t *trace_arg = GET_EC()->trace_arg;
850 if (trace_arg == 0) {
851 rb_raise(rb_eRuntimeError, "access from outside");
852 }
853 return trace_arg;
854}
855
856struct rb_trace_arg_struct *
857rb_tracearg_from_tracepoint(VALUE tpval)
858{
859 return get_trace_arg();
860}
861
862 rb_event_flag_t
863rb_tracearg_event_flag(rb_trace_arg_t *trace_arg)
864{
865 return trace_arg->event;
866}
867
868 VALUE
869rb_tracearg_event(rb_trace_arg_t *trace_arg)
870{
871 return ID2SYM(get_event_id(trace_arg->event));
872}
873
874static void
875fill_path_and_lineno(rb_trace_arg_t *trace_arg)
876{
877 if (UNDEF_P(trace_arg->path)) {
878 get_path_and_lineno(trace_arg->ec, trace_arg->cfp, trace_arg->event, &trace_arg->path, &trace_arg->lineno);
879 }
880}
881
882 VALUE
883rb_tracearg_lineno(rb_trace_arg_t *trace_arg)
884{
885 fill_path_and_lineno(trace_arg);
886 return INT2FIX(trace_arg->lineno);
887}
888 VALUE
889rb_tracearg_path(rb_trace_arg_t *trace_arg)
890{
891 fill_path_and_lineno(trace_arg);
892 return trace_arg->path;
893}
894
895static void
896fill_id_and_klass(rb_trace_arg_t *trace_arg)
897{
898 if (!trace_arg->klass_solved) {
899 if (!trace_arg->klass) {
900 rb_vm_control_frame_id_and_class(trace_arg->cfp, &trace_arg->id, &trace_arg->called_id, &trace_arg->klass);
901 }
902
903 if (trace_arg->klass) {
904 if (RB_TYPE_P(trace_arg->klass, T_ICLASS)) {
905 trace_arg->klass = RBASIC(trace_arg->klass)->klass;
906 }
907 }
908 else {
909 trace_arg->klass = Qnil;
910 }
911
912 trace_arg->klass_solved = 1;
913 }
914}
915
916 VALUE
917rb_tracearg_parameters(rb_trace_arg_t *trace_arg)
918{
919 switch (trace_arg->event) {
920 case RUBY_EVENT_CALL:
921 case RUBY_EVENT_RETURN:
922 case RUBY_EVENT_B_CALL:
923 case RUBY_EVENT_B_RETURN: {
924 const rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(trace_arg->ec, trace_arg->cfp);
925 if (cfp) {
926 int is_proc = 0;
927 if (VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_BLOCK && !VM_FRAME_LAMBDA_P(cfp)) {
928 is_proc = 1;
929 }
930 return rb_iseq_parameters(cfp->iseq, is_proc);
931 }
932 break;
933 }
934 case RUBY_EVENT_C_CALL:
935 case RUBY_EVENT_C_RETURN: {
936 fill_id_and_klass(trace_arg);
937 if (trace_arg->klass && trace_arg->id) {
938 const rb_method_entry_t *me;
939 VALUE iclass = Qnil;
940 me = rb_method_entry_without_refinements(trace_arg->klass, trace_arg->called_id, &iclass);
941 if (!me) {
942 me = rb_method_entry_without_refinements(trace_arg->klass, trace_arg->id, &iclass);
943 }
944 return rb_unnamed_parameters(rb_method_entry_arity(me));
945 }
946 break;
947 }
948 case RUBY_EVENT_RAISE:
949 case RUBY_EVENT_LINE:
950 case RUBY_EVENT_CLASS:
951 case RUBY_EVENT_END:
952 case RUBY_EVENT_SCRIPT_COMPILED:
953 case RUBY_EVENT_RESCUE:
954 rb_raise(rb_eRuntimeError, "not supported by this event");
955 break;
956 }
957 return Qnil;
958}
959
960 VALUE
961rb_tracearg_method_id(rb_trace_arg_t *trace_arg)
962{
963 fill_id_and_klass(trace_arg);
964 return trace_arg->id ? ID2SYM(trace_arg->id) : Qnil;
965}
966
967 VALUE
968rb_tracearg_callee_id(rb_trace_arg_t *trace_arg)
969{
970 fill_id_and_klass(trace_arg);
971 return trace_arg->called_id ? ID2SYM(trace_arg->called_id) : Qnil;
972}
973
974 VALUE
975rb_tracearg_defined_class(rb_trace_arg_t *trace_arg)
976{
977 fill_id_and_klass(trace_arg);
978 return trace_arg->klass;
979}
980
981 VALUE
982rb_tracearg_binding(rb_trace_arg_t *trace_arg)
983{
984 rb_control_frame_t *cfp;
985 switch (trace_arg->event) {
986 case RUBY_EVENT_C_CALL:
987 case RUBY_EVENT_C_RETURN:
988 return Qnil;
989 }
990 cfp = rb_vm_get_binding_creatable_next_cfp(trace_arg->ec, trace_arg->cfp);
991
992 if (cfp && imemo_type_p((VALUE)cfp->iseq, imemo_iseq)) {
993 return rb_vm_make_binding(trace_arg->ec, cfp);
994 }
995 else {
996 return Qnil;
997 }
998}
999
1000 VALUE
1001rb_tracearg_self(rb_trace_arg_t *trace_arg)
1002{
1003 return trace_arg->self;
1004}
1005
1006 VALUE
1007rb_tracearg_return_value(rb_trace_arg_t *trace_arg)
1008{
1009 if (trace_arg->event & (RUBY_EVENT_RETURN | RUBY_EVENT_C_RETURN | RUBY_EVENT_B_RETURN)) {
1010 /* ok */
1011 }
1012 else {
1013 rb_raise(rb_eRuntimeError, "not supported by this event");
1014 }
1015 if (UNDEF_P(trace_arg->data)) {
1016 rb_bug("rb_tracearg_return_value: unreachable");
1017 }
1018 return trace_arg->data;
1019}
1020
1021 VALUE
1022rb_tracearg_raised_exception(rb_trace_arg_t *trace_arg)
1023{
1024 if (trace_arg->event & (RUBY_EVENT_RAISE | RUBY_EVENT_RESCUE)) {
1025 /* ok */
1026 }
1027 else {
1028 rb_raise(rb_eRuntimeError, "not supported by this event");
1029 }
1030 if (UNDEF_P(trace_arg->data)) {
1031 rb_bug("rb_tracearg_raised_exception: unreachable");
1032 }
1033 return trace_arg->data;
1034}
1035
1036 VALUE
1037rb_tracearg_eval_script(rb_trace_arg_t *trace_arg)
1038{
1039 VALUE data = trace_arg->data;
1040
1041 if (trace_arg->event & (RUBY_EVENT_SCRIPT_COMPILED)) {
1042 /* ok */
1043 }
1044 else {
1045 rb_raise(rb_eRuntimeError, "not supported by this event");
1046 }
1047 if (UNDEF_P(data)) {
1048 rb_bug("rb_tracearg_raised_exception: unreachable");
1049 }
1050 if (rb_obj_is_iseq(data)) {
1051 return Qnil;
1052 }
1053 else {
1054 VM_ASSERT(RB_TYPE_P(data, T_ARRAY));
1055 /* [src, iseq] */
1056 return RARRAY_AREF(data, 0);
1057 }
1058}
1059
1060 VALUE
1061rb_tracearg_instruction_sequence(rb_trace_arg_t *trace_arg)
1062{
1063 VALUE data = trace_arg->data;
1064
1065 if (trace_arg->event & (RUBY_EVENT_SCRIPT_COMPILED)) {
1066 /* ok */
1067 }
1068 else {
1069 rb_raise(rb_eRuntimeError, "not supported by this event");
1070 }
1071 if (UNDEF_P(data)) {
1072 rb_bug("rb_tracearg_raised_exception: unreachable");
1073 }
1074
1075 if (rb_obj_is_iseq(data)) {
1076 return rb_iseqw_new((const rb_iseq_t *)data);
1077 }
1078 else {
1079 VM_ASSERT(RB_TYPE_P(data, T_ARRAY));
1080 VM_ASSERT(rb_obj_is_iseq(RARRAY_AREF(data, 1)));
1081
1082 /* [src, iseq] */
1083 return rb_iseqw_new((const rb_iseq_t *)RARRAY_AREF(data, 1));
1084 }
1085}
1086
1087 VALUE
1088rb_tracearg_object(rb_trace_arg_t *trace_arg)
1089{
1090 if (trace_arg->event & (RUBY_INTERNAL_EVENT_NEWOBJ | RUBY_INTERNAL_EVENT_FREEOBJ)) {
1091 /* ok */
1092 }
1093 else {
1094 rb_raise(rb_eRuntimeError, "not supported by this event");
1095 }
1096 if (UNDEF_P(trace_arg->data)) {
1097 rb_bug("rb_tracearg_object: unreachable");
1098 }
1099 return trace_arg->data;
1100}
1101
1102static VALUE
1103tracepoint_attr_event(rb_execution_context_t *ec, VALUE tpval)
1104{
1105 return rb_tracearg_event(get_trace_arg());
1106}
1107
1108static VALUE
1109tracepoint_attr_lineno(rb_execution_context_t *ec, VALUE tpval)
1110{
1111 return rb_tracearg_lineno(get_trace_arg());
1112}
1113static VALUE
1114tracepoint_attr_path(rb_execution_context_t *ec, VALUE tpval)
1115{
1116 return rb_tracearg_path(get_trace_arg());
1117}
1118
1119static VALUE
1120tracepoint_attr_parameters(rb_execution_context_t *ec, VALUE tpval)
1121{
1122 return rb_tracearg_parameters(get_trace_arg());
1123}
1124
1125static VALUE
1126tracepoint_attr_method_id(rb_execution_context_t *ec, VALUE tpval)
1127{
1128 return rb_tracearg_method_id(get_trace_arg());
1129}
1130
1131static VALUE
1132tracepoint_attr_callee_id(rb_execution_context_t *ec, VALUE tpval)
1133{
1134 return rb_tracearg_callee_id(get_trace_arg());
1135}
1136
1137static VALUE
1138tracepoint_attr_defined_class(rb_execution_context_t *ec, VALUE tpval)
1139{
1140 return rb_tracearg_defined_class(get_trace_arg());
1141}
1142
1143static VALUE
1144tracepoint_attr_binding(rb_execution_context_t *ec, VALUE tpval)
1145{
1146 return rb_tracearg_binding(get_trace_arg());
1147}
1148
1149static VALUE
1150tracepoint_attr_self(rb_execution_context_t *ec, VALUE tpval)
1151{
1152 return rb_tracearg_self(get_trace_arg());
1153}
1154
1155static VALUE
1156tracepoint_attr_return_value(rb_execution_context_t *ec, VALUE tpval)
1157{
1158 return rb_tracearg_return_value(get_trace_arg());
1159}
1160
1161static VALUE
1162tracepoint_attr_raised_exception(rb_execution_context_t *ec, VALUE tpval)
1163{
1164 return rb_tracearg_raised_exception(get_trace_arg());
1165}
1166
1167static VALUE
1168tracepoint_attr_eval_script(rb_execution_context_t *ec, VALUE tpval)
1169{
1170 return rb_tracearg_eval_script(get_trace_arg());
1171}
1172
1173static VALUE
1174tracepoint_attr_instruction_sequence(rb_execution_context_t *ec, VALUE tpval)
1175{
1176 return rb_tracearg_instruction_sequence(get_trace_arg());
1177}
1178
1179static void
1180tp_call_trace(VALUE tpval, rb_trace_arg_t *trace_arg)
1181{
1182 rb_tp_t *tp = tpptr(tpval);
1183
1184 if (tp->func) {
1185 (*tp->func)(tpval, tp->data);
1186 }
1187 else {
1188 if (tp->ractor == NULL || tp->ractor == GET_RACTOR()) {
1189 rb_proc_call_with_block((VALUE)tp->proc, 1, &tpval, Qnil);
1190 }
1191 }
1192}
1193
1194 VALUE
1195rb_tracepoint_enable(VALUE tpval)
1196{
1197 rb_tp_t *tp;
1198 tp = tpptr(tpval);
1199
1200 if (tp->local_target_set != Qfalse) {
1201 rb_raise(rb_eArgError, "can't nest-enable a targeting TracePoint");
1202 }
1203
1204 if (tp->tracing) {
1205 return Qundef;
1206 }
1207
1208 if (tp->target_th) {
1209 rb_thread_add_event_hook2(tp->target_th->self, (rb_event_hook_func_t)tp_call_trace, tp->events, tpval,
1210 RUBY_EVENT_HOOK_FLAG_SAFE | RUBY_EVENT_HOOK_FLAG_RAW_ARG);
1211 }
1212 else {
1213 rb_add_event_hook2((rb_event_hook_func_t)tp_call_trace, tp->events, tpval,
1214 RUBY_EVENT_HOOK_FLAG_SAFE | RUBY_EVENT_HOOK_FLAG_RAW_ARG);
1215 }
1216 tp->tracing = 1;
1217 return Qundef;
1218}
1219
1220static const rb_iseq_t *
1221iseq_of(VALUE target)
1222{
1223 VALUE iseqv = rb_funcall(rb_cISeq, rb_intern("of"), 1, target);
1224 if (NIL_P(iseqv)) {
1225 rb_raise(rb_eArgError, "specified target is not supported");
1226 }
1227 else {
1228 return rb_iseqw_to_iseq(iseqv);
1229 }
1230}
1231
1232const rb_method_definition_t *rb_method_def(VALUE method); /* proc.c */
1233
1234static VALUE
1235rb_tracepoint_enable_for_target(VALUE tpval, VALUE target, VALUE target_line)
1236{
1237 rb_tp_t *tp = tpptr(tpval);
1238 const rb_iseq_t *iseq = iseq_of(target);
1239 int n = 0;
1240 unsigned int line = 0;
1241 bool target_bmethod = false;
1242
1243 if (tp->tracing > 0) {
1244 rb_raise(rb_eArgError, "can't nest-enable a targeting TracePoint");
1245 }
1246
1247 if (!NIL_P(target_line)) {
1248 if ((tp->events & RUBY_EVENT_LINE) == 0) {
1249 rb_raise(rb_eArgError, "target_line is specified, but line event is not specified");
1250 }
1251 else {
1252 line = NUM2UINT(target_line);
1253 }
1254 }
1255
1256 VM_ASSERT(tp->local_target_set == Qfalse);
1257 RB_OBJ_WRITE(tpval, &tp->local_target_set, rb_obj_hide(rb_ident_hash_new()));
1258
1259 /* bmethod */
1260 if (rb_obj_is_method(target)) {
1261 rb_method_definition_t *def = (rb_method_definition_t *)rb_method_def(target);
1262 if (def->type == VM_METHOD_TYPE_BMETHOD &&
1263 (tp->events & (RUBY_EVENT_CALL | RUBY_EVENT_RETURN))) {
1264 if (def->body.bmethod.hooks == NULL) {
1265 def->body.bmethod.hooks = ZALLOC(rb_hook_list_t);
1266 def->body.bmethod.hooks->is_local = true;
1267 }
1268 rb_hook_list_connect_tracepoint(target, def->body.bmethod.hooks, tpval, 0);
1269 rb_hash_aset(tp->local_target_set, target, Qfalse);
1270 target_bmethod = true;
1271
1272 n++;
1273 }
1274 }
1275
1276 /* iseq */
1277 n += rb_iseq_add_local_tracepoint_recursively(iseq, tp->events, tpval, line, target_bmethod);
1278 rb_hash_aset(tp->local_target_set, (VALUE)iseq, Qtrue);
1279
1280 if ((tp->events & (RUBY_EVENT_CALL | RUBY_EVENT_RETURN)) &&
1281 iseq->body->builtin_attrs & BUILTIN_ATTR_SINGLE_NOARG_LEAF) {
1282 rb_clear_bf_ccs();
1283 }
1284
1285 if (n == 0) {
1286 rb_raise(rb_eArgError, "can not enable any hooks");
1287 }
1288
1289 rb_yjit_tracing_invalidate_all();
1290 rb_rjit_tracing_invalidate_all(tp->events);
1291
1292 ruby_vm_event_local_num++;
1293
1294 tp->tracing = 1;
1295
1296 return Qnil;
1297}
1298
1299static int
1300disable_local_event_iseq_i(VALUE target, VALUE iseq_p, VALUE tpval)
1301{
1302 if (iseq_p) {
1303 rb_iseq_remove_local_tracepoint_recursively((rb_iseq_t *)target, tpval);
1304 }
1305 else {
1306 /* bmethod */
1307 rb_method_definition_t *def = (rb_method_definition_t *)rb_method_def(target);
1308 rb_hook_list_t *hooks = def->body.bmethod.hooks;
1309 VM_ASSERT(hooks != NULL);
1310 rb_hook_list_remove_tracepoint(hooks, tpval);
1311
1312 if (hooks->events == 0) {
1313 rb_hook_list_free(def->body.bmethod.hooks);
1314 def->body.bmethod.hooks = NULL;
1315 }
1316 }
1317 return ST_CONTINUE;
1318}
1319
1320 VALUE
1321rb_tracepoint_disable(VALUE tpval)
1322{
1323 rb_tp_t *tp;
1324
1325 tp = tpptr(tpval);
1326
1327 if (tp->local_target_set) {
1328 rb_hash_foreach(tp->local_target_set, disable_local_event_iseq_i, tpval);
1329 RB_OBJ_WRITE(tpval, &tp->local_target_set, Qfalse);
1330 ruby_vm_event_local_num--;
1331 }
1332 else {
1333 if (tp->target_th) {
1334 rb_thread_remove_event_hook_with_data(tp->target_th->self, (rb_event_hook_func_t)tp_call_trace, tpval);
1335 }
1336 else {
1337 rb_remove_event_hook_with_data((rb_event_hook_func_t)tp_call_trace, tpval);
1338 }
1339 }
1340 tp->tracing = 0;
1341 tp->target_th = NULL;
1342 return Qundef;
1343}
1344
1345void
1346rb_hook_list_connect_tracepoint(VALUE target, rb_hook_list_t *list, VALUE tpval, unsigned int target_line)
1347{
1348 rb_tp_t *tp = tpptr(tpval);
1349 rb_event_hook_t *hook = alloc_event_hook((rb_event_hook_func_t)tp_call_trace, tp->events & ISEQ_TRACE_EVENTS, tpval,
1350 RUBY_EVENT_HOOK_FLAG_SAFE | RUBY_EVENT_HOOK_FLAG_RAW_ARG);
1351 hook->filter.target_line = target_line;
1352 hook_list_connect(target, list, hook, FALSE);
1353}
1354
1355void
1356rb_hook_list_remove_tracepoint(rb_hook_list_t *list, VALUE tpval)
1357{
1358 rb_event_hook_t *hook = list->hooks;
1359 rb_event_flag_t events = 0;
1360
1361 while (hook) {
1362 if (hook->data == tpval) {
1363 hook->hook_flags |= RUBY_EVENT_HOOK_FLAG_DELETED;
1364 list->need_clean = true;
1365 }
1366 else if ((hook->hook_flags & RUBY_EVENT_HOOK_FLAG_DELETED) == 0) {
1367 events |= hook->events;
1368 }
1369 hook = hook->next;
1370 }
1371
1372 list->events = events;
1373}
1374
1375static VALUE
1376tracepoint_enable_m(rb_execution_context_t *ec, VALUE tpval, VALUE target, VALUE target_line, VALUE target_thread)
1377{
1378 rb_tp_t *tp = tpptr(tpval);
1379 int previous_tracing = tp->tracing;
1380
1381 if (target_thread == sym_default) {
1382 if (rb_block_given_p() && NIL_P(target) && NIL_P(target_line)) {
1383 target_thread = rb_thread_current();
1384 }
1385 else {
1386 target_thread = Qnil;
1387 }
1388 }
1389
1390 /* check target_thread */
1391 if (RTEST(target_thread)) {
1392 if (tp->target_th) {
1393 rb_raise(rb_eArgError, "can not override target_thread filter");
1394 }
1395 tp->target_th = rb_thread_ptr(target_thread);
1396
1397 RUBY_ASSERT(tp->target_th->self == target_thread);
1398 RB_OBJ_WRITTEN(tpval, Qundef, target_thread);
1399 }
1400 else {
1401 tp->target_th = NULL;
1402 }
1403
1404 if (NIL_P(target)) {
1405 if (!NIL_P(target_line)) {
1406 rb_raise(rb_eArgError, "only target_line is specified");
1407 }
1408 rb_tracepoint_enable(tpval);
1409 }
1410 else {
1411 rb_tracepoint_enable_for_target(tpval, target, target_line);
1412 }
1413
1414 if (rb_block_given_p()) {
1415 return rb_ensure(rb_yield, Qundef,
1416 previous_tracing ? rb_tracepoint_enable : rb_tracepoint_disable,
1417 tpval);
1418 }
1419 else {
1420 return RBOOL(previous_tracing);
1421 }
1422}
1423
1424static VALUE
1425tracepoint_disable_m(rb_execution_context_t *ec, VALUE tpval)
1426{
1427 rb_tp_t *tp = tpptr(tpval);
1428 int previous_tracing = tp->tracing;
1429
1430 if (rb_block_given_p()) {
1431 if (tp->local_target_set != Qfalse) {
1432 rb_raise(rb_eArgError, "can't disable a targeting TracePoint in a block");
1433 }
1434
1435 rb_tracepoint_disable(tpval);
1436 return rb_ensure(rb_yield, Qundef,
1437 previous_tracing ? rb_tracepoint_enable : rb_tracepoint_disable,
1438 tpval);
1439 }
1440 else {
1441 rb_tracepoint_disable(tpval);
1442 return RBOOL(previous_tracing);
1443 }
1444}
1445
1446 VALUE
1447rb_tracepoint_enabled_p(VALUE tpval)
1448{
1449 rb_tp_t *tp = tpptr(tpval);
1450 return RBOOL(tp->tracing);
1451}
1452
1453static VALUE
1454tracepoint_enabled_p(rb_execution_context_t *ec, VALUE tpval)
1455{
1456 return rb_tracepoint_enabled_p(tpval);
1457}
1458
1459static VALUE
1460tracepoint_new(VALUE klass, rb_thread_t *target_th, rb_event_flag_t events, void (func)(VALUE, void*), void *data, VALUE proc)
1461{
1462 VALUE tpval = tp_alloc(klass);
1463 rb_tp_t *tp;
1464 TypedData_Get_Struct(tpval, rb_tp_t, &tp_data_type, tp);
1465
1466 RB_OBJ_WRITE(tpval, &tp->proc, proc);
1467 tp->ractor = rb_ractor_shareable_p(proc) ? NULL : GET_RACTOR();
1468 tp->func = func;
1469 tp->data = data;
1470 tp->events = events;
1471 tp->self = tpval;
1472
1473 return tpval;
1474}
1475
1476 VALUE
1477rb_tracepoint_new(VALUE target_thval, rb_event_flag_t events, void (*func)(VALUE, void *), void *data)
1478{
1479 rb_thread_t *target_th = NULL;
1480
1481 if (RTEST(target_thval)) {
1482 target_th = rb_thread_ptr(target_thval);
1483 /* TODO: Test it!
1484 * Warning: This function is not tested.
1485 */
1486 }
1487 return tracepoint_new(rb_cTracePoint, target_th, events, func, data, Qundef);
1488}
1489
1490static VALUE
1491tracepoint_new_s(rb_execution_context_t *ec, VALUE self, VALUE args)
1492{
1493 rb_event_flag_t events = 0;
1494 long i;
1495 long argc = RARRAY_LEN(args);
1496
1497 if (argc > 0) {
1498 for (i=0; i<argc; i++) {
1499 events |= symbol2event_flag(RARRAY_AREF(args, i));
1500 }
1501 }
1502 else {
1503 events = RUBY_EVENT_TRACEPOINT_ALL;
1504 }
1505
1506 if (!rb_block_given_p()) {
1507 rb_raise(rb_eArgError, "must be called with a block");
1508 }
1509
1510 return tracepoint_new(self, 0, events, 0, 0, rb_block_proc());
1511}
1512
1513static VALUE
1514tracepoint_trace_s(rb_execution_context_t *ec, VALUE self, VALUE args)
1515{
1516 VALUE trace = tracepoint_new_s(ec, self, args);
1517 rb_tracepoint_enable(trace);
1518 return trace;
1519}
1520
1521static VALUE
1522tracepoint_inspect(rb_execution_context_t *ec, VALUE self)
1523{
1524 rb_tp_t *tp = tpptr(self);
1525 rb_trace_arg_t *trace_arg = GET_EC()->trace_arg;
1526
1527 if (trace_arg) {
1528 switch (trace_arg->event) {
1529 case RUBY_EVENT_LINE:
1530 {
1531 VALUE sym = rb_tracearg_method_id(trace_arg);
1532 if (NIL_P(sym))
1533 break;
1534 return rb_sprintf("#<TracePoint:%"PRIsVALUE" %"PRIsVALUE":%d in '%"PRIsVALUE"'>",
1535 rb_tracearg_event(trace_arg),
1536 rb_tracearg_path(trace_arg),
1537 FIX2INT(rb_tracearg_lineno(trace_arg)),
1538 sym);
1539 }
1540 case RUBY_EVENT_CALL:
1541 case RUBY_EVENT_C_CALL:
1542 case RUBY_EVENT_RETURN:
1543 case RUBY_EVENT_C_RETURN:
1544 return rb_sprintf("#<TracePoint:%"PRIsVALUE" '%"PRIsVALUE"' %"PRIsVALUE":%d>",
1545 rb_tracearg_event(trace_arg),
1546 rb_tracearg_method_id(trace_arg),
1547 rb_tracearg_path(trace_arg),
1548 FIX2INT(rb_tracearg_lineno(trace_arg)));
1549 case RUBY_EVENT_THREAD_BEGIN:
1550 case RUBY_EVENT_THREAD_END:
1551 return rb_sprintf("#<TracePoint:%"PRIsVALUE" %"PRIsVALUE">",
1552 rb_tracearg_event(trace_arg),
1553 rb_tracearg_self(trace_arg));
1554 default:
1555 break;
1556 }
1557 return rb_sprintf("#<TracePoint:%"PRIsVALUE" %"PRIsVALUE":%d>",
1558 rb_tracearg_event(trace_arg),
1559 rb_tracearg_path(trace_arg),
1560 FIX2INT(rb_tracearg_lineno(trace_arg)));
1561 }
1562 else {
1563 return rb_sprintf("#<TracePoint:%s>", tp->tracing ? "enabled" : "disabled");
1564 }
1565}
1566
1567static void
1568tracepoint_stat_event_hooks(VALUE hash, VALUE key, rb_event_hook_t *hook)
1569{
1570 int active = 0, deleted = 0;
1571
1572 while (hook) {
1573 if (hook->hook_flags & RUBY_EVENT_HOOK_FLAG_DELETED) {
1574 deleted++;
1575 }
1576 else {
1577 active++;
1578 }
1579 hook = hook->next;
1580 }
1581
1582 rb_hash_aset(hash, key, rb_ary_new3(2, INT2FIX(active), INT2FIX(deleted)));
1583}
1584
1585static VALUE
1586tracepoint_stat_s(rb_execution_context_t *ec, VALUE self)
1587{
1588 rb_vm_t *vm = GET_VM();
1589 VALUE stat = rb_hash_new();
1590
1591 tracepoint_stat_event_hooks(stat, vm->self, rb_ec_ractor_hooks(ec)->hooks);
1592 /* TODO: thread local hooks */
1593
1594 return stat;
1595}
1596
1597static VALUE
1598disallow_reentry(VALUE val)
1599{
1600 rb_trace_arg_t *arg = (rb_trace_arg_t *)val;
1601 rb_execution_context_t *ec = GET_EC();
1602 if (ec->trace_arg != NULL) rb_bug("should be NULL, but %p", (void *)ec->trace_arg);
1603 ec->trace_arg = arg;
1604 return Qnil;
1605}
1606
1607static VALUE
1608tracepoint_allow_reentry(rb_execution_context_t *ec, VALUE self)
1609{
1610 const rb_trace_arg_t *arg = ec->trace_arg;
1611 if (arg == NULL) rb_raise(rb_eRuntimeError, "No need to allow reentrance.");
1612 ec->trace_arg = NULL;
1613 return rb_ensure(rb_yield, Qnil, disallow_reentry, (VALUE)arg);
1614}
1615
1616#include "trace_point.rbinc"
1617
1618/* This function is called from inits.c */
1619void
1620Init_vm_trace(void)
1621{
1622 sym_default = ID2SYM(rb_intern_const("default"));
1623
1624 /* trace_func */
1625 rb_define_global_function("set_trace_func", set_trace_func, 1);
1626 rb_define_method(rb_cThread, "set_trace_func", thread_set_trace_func_m, 1);
1627 rb_define_method(rb_cThread, "add_trace_func", thread_add_trace_func_m, 1);
1628
1629 rb_cTracePoint = rb_define_class("TracePoint", rb_cObject);
1630 rb_undef_alloc_func(rb_cTracePoint);
1631}
1632
1633/*
1634 * Ruby actually has two separate mechanisms for enqueueing work from contexts
1635 * where it is not safe to run Ruby code, to run later on when it is safe. One
1636 * is async-signal-safe but more limited, and accessed through the
1637 * `rb_postponed_job_preregister` and `rb_postponed_job_trigger` functions. The
1638 * other is more flexible but cannot be used in signal handlers, and is accessed
1639 * through the `rb_workqueue_register` function.
1640 *
1641 * The postponed job functions form part of Ruby's extension API, but the
1642 * workqueue functions are for internal use only.
1643 */
1644
1645 struct rb_workqueue_job {
1646 struct ccan_list_node jnode; /* <=> vm->workqueue */
1647 rb_postponed_job_func_t func;
1648 void *data;
1649};
1650
1651// Used for VM memsize reporting. Returns the size of a list of rb_workqueue_job
1652// structs. Defined here because the struct definition lives here as well.
1653size_t
1654rb_vm_memsize_workqueue(struct ccan_list_head *workqueue)
1655{
1656 struct rb_workqueue_job *work = 0;
1657 size_t size = 0;
1658
1659 ccan_list_for_each(workqueue, work, jnode) {
1660 size += sizeof(struct rb_workqueue_job);
1661 }
1662
1663 return size;
1664}
1665
1666/*
1667 * thread-safe and called from non-Ruby thread
1668 * returns FALSE on failure (ENOMEM), TRUE otherwise
1669 */
1670int
1671rb_workqueue_register(unsigned flags, rb_postponed_job_func_t func, void *data)
1672{
1673 struct rb_workqueue_job *wq_job = malloc(sizeof(*wq_job));
1674 rb_vm_t *vm = GET_VM();
1675
1676 if (!wq_job) return FALSE;
1677 wq_job->func = func;
1678 wq_job->data = data;
1679
1680 rb_nativethread_lock_lock(&vm->workqueue_lock);
1681 ccan_list_add_tail(&vm->workqueue, &wq_job->jnode);
1682 rb_nativethread_lock_unlock(&vm->workqueue_lock);
1683
1684 // TODO: current implementation affects only main ractor
1685 RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(rb_vm_main_ractor_ec(vm));
1686
1687 return TRUE;
1688}
1689
1690#define PJOB_TABLE_SIZE (sizeof(rb_atomic_t) * CHAR_BIT)
1691/* pre-registered jobs table, for async-safe jobs */
1692 typedef struct rb_postponed_job_queue {
1693 struct {
1694 rb_postponed_job_func_t func;
1695 void *data;
1696 } table[PJOB_TABLE_SIZE];
1697 /* Bits in this are set when the corresponding entry in prereg_table has non-zero
1698 * triggered_count; i.e. somebody called rb_postponed_job_trigger */
1699 rb_atomic_t triggered_bitset;
1700} rb_postponed_job_queues_t;
1701
1702void
1703rb_vm_postponed_job_queue_init(rb_vm_t *vm)
1704{
1705 /* use mimmalloc; postponed job registration is a dependency of objspace, so this gets
1706 * called _VERY_ early inside Init_BareVM */
1707 rb_postponed_job_queues_t *pjq = ruby_mimmalloc(sizeof(rb_postponed_job_queues_t));
1708 pjq->triggered_bitset = 0;
1709 memset(pjq->table, 0, sizeof(pjq->table));
1710 vm->postponed_job_queue = pjq;
1711}
1712
1713static rb_execution_context_t *
1714get_valid_ec(rb_vm_t *vm)
1715{
1716 rb_execution_context_t *ec = rb_current_execution_context(false);
1717 if (ec == NULL) ec = rb_vm_main_ractor_ec(vm);
1718 return ec;
1719}
1720
1721void
1722rb_vm_postponed_job_atfork(void)
1723{
1724 rb_vm_t *vm = GET_VM();
1725 rb_postponed_job_queues_t *pjq = vm->postponed_job_queue;
1726 /* make sure we set the interrupt flag on _this_ thread if we carried any pjobs over
1727 * from the other side of the fork */
1728 if (pjq->triggered_bitset) {
1729 RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(get_valid_ec(vm));
1730 }
1731
1732}
1733
1734/* Frees the memory managed by the postponed job infrastructure at shutdown */
1735void
1736rb_vm_postponed_job_free(void)
1737{
1738 rb_vm_t *vm = GET_VM();
1739 ruby_xfree(vm->postponed_job_queue);
1740 vm->postponed_job_queue = NULL;
1741}
1742
1743// Used for VM memsize reporting. Returns the total size of the postponed job
1744// queue infrastructure.
1745size_t
1746rb_vm_memsize_postponed_job_queue(void)
1747{
1748 return sizeof(rb_postponed_job_queues_t);
1749}
1750
1751
1752 rb_postponed_job_handle_t
1753rb_postponed_job_preregister(unsigned int flags, rb_postponed_job_func_t func, void *data)
1754{
1755 /* The doc comments say that this function should be called under the GVL, because
1756 * that is actually required to get the guarantee that "if a given (func, data) pair
1757 * was already pre-registered, this method will return the same handle instance".
1758 *
1759 * However, the actual implementation here is called without the GVL, from inside
1760 * rb_postponed_job_register, to support that legacy interface. In the presence
1761 * of concurrent calls to both _preregister and _register functions on the same
1762 * func, however, the data may get mixed up between them. */
1763
1764 rb_postponed_job_queues_t *pjq = GET_VM()->postponed_job_queue;
1765 for (unsigned int i = 0; i < PJOB_TABLE_SIZE; i++) {
1766 /* Try and set this slot to equal `func` */
1767 rb_postponed_job_func_t existing_func = (rb_postponed_job_func_t)(uintptr_t)RUBY_ATOMIC_PTR_CAS(pjq->table[i].func, NULL, (void *)(uintptr_t)func);
1768 if (existing_func == NULL || existing_func == func) {
1769 /* Either this slot was NULL, and we set it to func, or, this slot was already equal to func.
1770 * In either case, clobber the data with our data. Note that concurrent calls to
1771 * rb_postponed_job_register with the same func & different data will result in either of the
1772 * datas being written */
1773 RUBY_ATOMIC_PTR_EXCHANGE(pjq->table[i].data, data);
1774 return (rb_postponed_job_handle_t)i;
1775 }
1776 else {
1777 /* Try the next slot if this one already has a func in it */
1778 continue;
1779 }
1780 }
1781
1782 /* full */
1783 return POSTPONED_JOB_HANDLE_INVALID;
1784}
1785
1786void
1787rb_postponed_job_trigger(rb_postponed_job_handle_t h)
1788{
1789 rb_vm_t *vm = GET_VM();
1790 rb_postponed_job_queues_t *pjq = vm->postponed_job_queue;
1791
1792 RUBY_ATOMIC_OR(pjq->triggered_bitset, (((rb_atomic_t)1UL) << h));
1793 RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(get_valid_ec(vm));
1794}
1795
1796
1797static int
1798pjob_register_legacy_impl(unsigned int flags, rb_postponed_job_func_t func, void *data)
1799{
1800 /* We _know_ calling preregister from a signal handler like this is racy; what is
1801 * and is not promised is very exhaustively documented in debug.h */
1802 rb_postponed_job_handle_t h = rb_postponed_job_preregister(0, func, data);
1803 if (h == POSTPONED_JOB_HANDLE_INVALID) {
1804 return 0;
1805 }
1806 rb_postponed_job_trigger(h);
1807 return 1;
1808}
1809
1810int
1811rb_postponed_job_register(unsigned int flags, rb_postponed_job_func_t func, void *data)
1812{
1813 return pjob_register_legacy_impl(flags, func, data);
1814}
1815
1816int
1817rb_postponed_job_register_one(unsigned int flags, rb_postponed_job_func_t func, void *data)
1818{
1819 return pjob_register_legacy_impl(flags, func, data);
1820}
1821
1822
1823void
1824rb_postponed_job_flush(rb_vm_t *vm)
1825{
1826 rb_postponed_job_queues_t *pjq = GET_VM()->postponed_job_queue;
1827 rb_execution_context_t *ec = GET_EC();
1828 const rb_atomic_t block_mask = POSTPONED_JOB_INTERRUPT_MASK | TRAP_INTERRUPT_MASK;
1829 volatile rb_atomic_t saved_mask = ec->interrupt_mask & block_mask;
1830 VALUE volatile saved_errno = ec->errinfo;
1831 struct ccan_list_head tmp;
1832
1833 ccan_list_head_init(&tmp);
1834
1835 rb_nativethread_lock_lock(&vm->workqueue_lock);
1836 ccan_list_append_list(&tmp, &vm->workqueue);
1837 rb_nativethread_lock_unlock(&vm->workqueue_lock);
1838
1839 rb_atomic_t triggered_bits = RUBY_ATOMIC_EXCHANGE(pjq->triggered_bitset, 0);
1840
1841 ec->errinfo = Qnil;
1842 /* mask POSTPONED_JOB dispatch */
1843 ec->interrupt_mask |= block_mask;
1844 {
1845 EC_PUSH_TAG(ec);
1846 if (EC_EXEC_TAG() == TAG_NONE) {
1847 /* execute postponed jobs */
1848 while (triggered_bits) {
1849 unsigned int i = bit_length(triggered_bits) - 1;
1850 triggered_bits ^= ((1UL) << i); /* toggle ith bit off */
1851 rb_postponed_job_func_t func = pjq->table[i].func;
1852 void *data = pjq->table[i].data;
1853 (func)(data);
1854 }
1855
1856 /* execute workqueue jobs */
1857 struct rb_workqueue_job *wq_job;
1858 while ((wq_job = ccan_list_pop(&tmp, struct rb_workqueue_job, jnode))) {
1859 rb_postponed_job_func_t func = wq_job->func;
1860 void *data = wq_job->data;
1861
1862 free(wq_job);
1863 (func)(data);
1864 }
1865 }
1866 EC_POP_TAG();
1867 }
1868 /* restore POSTPONED_JOB mask */
1869 ec->interrupt_mask &= ~(saved_mask ^ block_mask);
1870 ec->errinfo = saved_errno;
1871
1872 /* If we threw an exception, there might be leftover workqueue items; carry them over
1873 * to a subsequent execution of flush */
1874 if (!ccan_list_empty(&tmp)) {
1875 rb_nativethread_lock_lock(&vm->workqueue_lock);
1876 ccan_list_prepend_list(&vm->workqueue, &tmp);
1877 rb_nativethread_lock_unlock(&vm->workqueue_lock);
1878
1879 RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(GET_EC());
1880 }
1881 /* likewise with any remaining-to-be-executed bits of the preregistered postponed
1882 * job table */
1883 if (triggered_bits) {
1884 RUBY_ATOMIC_OR(pjq->triggered_bitset, triggered_bits);
1885 RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(GET_EC());
1886 }
1887}
RUBY_ASSERT
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
atomic.h
Atomic operations.
RUBY_ATOMIC_OR
#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...
Definition atomic.h:116
RUBY_ATOMIC_PTR_CAS
#define RUBY_ATOMIC_PTR_CAS(var, oldval, newval)
Identical to RUBY_ATOMIC_CAS, except it expects its arguments are void*.
Definition atomic.h:315
rb_atomic_t
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
Definition atomic.h:69
RUBY_ATOMIC_PTR_EXCHANGE
#define RUBY_ATOMIC_PTR_EXCHANGE(var, val)
Identical to RUBY_ATOMIC_EXCHANGE, except it expects its arguments are void*.
Definition atomic.h:290
RUBY_ATOMIC_EXCHANGE
#define RUBY_ATOMIC_EXCHANGE(var, val)
Atomically replaces the value pointed by var with val.
Definition atomic.h:127
rb_define_method
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
Definition cxxanyargs.hpp:670
rb_define_global_function
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
Definition cxxanyargs.hpp:695
debug.h
rb_postponed_job_handle_t
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...
Definition debug.h:665
rb_tracearg_binding
VALUE rb_tracearg_binding(rb_trace_arg_t *trace_arg)
Creates a binding object of the point where the trace is at.
Definition vm_trace.c:982
rb_postponed_job_trigger
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...
Definition vm_trace.c:1787
rb_tracepoint_enabled_p
VALUE rb_tracepoint_enabled_p(VALUE tpval)
Queries if the passed TracePoint is up and running.
Definition vm_trace.c:1447
rb_tracearg_object
VALUE rb_tracearg_object(rb_trace_arg_t *trace_arg)
Queries the allocated/deallocated object that the trace represents.
Definition vm_trace.c:1088
rb_tracearg_callee_id
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().
Definition vm_trace.c:968
rb_tracearg_defined_class
VALUE rb_tracearg_defined_class(rb_trace_arg_t *trace_arg)
Queries the class that defines the method that the passed trace is at.
Definition vm_trace.c:975
rb_tracepoint_new
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.
Definition vm_trace.c:1477
rb_trace_arg_t
struct rb_trace_arg_struct rb_trace_arg_t
Type that represents a specific trace event.
Definition debug.h:465
rb_tracearg_raised_exception
VALUE rb_tracearg_raised_exception(rb_trace_arg_t *trace_arg)
Queries the raised exception that the trace represents.
Definition vm_trace.c:1022
rb_thread_add_event_hook
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.
Definition vm_trace.c:201
rb_postponed_job_preregister
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 ...
Definition vm_trace.c:1753
rb_tracepoint_disable
VALUE rb_tracepoint_disable(VALUE tpval)
Stops (disables) an already running instance of TracePoint.
Definition vm_trace.c:1321
rb_tracearg_self
VALUE rb_tracearg_self(rb_trace_arg_t *trace_arg)
Queries the receiver of the point trace is at.
Definition vm_trace.c:1001
rb_thread_remove_event_hook
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.
Definition vm_trace.c:303
rb_postponed_job_register_one
int rb_postponed_job_register_one(unsigned int flags, rb_postponed_job_func_t func, void *data)
Identical to rb_postponed_job_register.
Definition vm_trace.c:1817
rb_tracearg_return_value
VALUE rb_tracearg_return_value(rb_trace_arg_t *trace_arg)
Queries the return value that the trace represents.
Definition vm_trace.c:1007
rb_tracearg_event_flag
rb_event_flag_t rb_tracearg_event_flag(rb_trace_arg_t *trace_arg)
Queries the event of the passed trace.
Definition vm_trace.c:863
rb_tracearg_path
VALUE rb_tracearg_path(rb_trace_arg_t *trace_arg)
Queries the file name of the point where the trace is at.
Definition vm_trace.c:889
rb_thread_remove_event_hook_with_data
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.
Definition vm_trace.c:309
rb_tracepoint_enable
VALUE rb_tracepoint_enable(VALUE tpval)
Starts (enables) trace(s) defined by the passed object.
Definition vm_trace.c:1195
rb_postponed_job_register
int rb_postponed_job_register(unsigned int flags, rb_postponed_job_func_t func, void *data)
Schedules the given func to be called with data when Ruby next checks for interrupts.
Definition vm_trace.c:1811
rb_tracearg_method_id
VALUE rb_tracearg_method_id(rb_trace_arg_t *trace_arg)
Queries the method name of the point where the trace is at.
Definition vm_trace.c:961
rb_remove_event_hook_with_data
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.
Definition vm_trace.c:321
rb_tracearg_from_tracepoint
rb_trace_arg_t * rb_tracearg_from_tracepoint(VALUE tpval)
Queries the current event of the passed tracepoint.
Definition vm_trace.c:857
rb_tracearg_lineno
VALUE rb_tracearg_lineno(rb_trace_arg_t *trace_arg)
Queries the line of the point where the trace is at.
Definition vm_trace.c:883
rb_postponed_job_func_t
void(* rb_postponed_job_func_t)(void *arg)
Type of postponed jobs.
Definition debug.h:659
rb_tracearg_event
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.
Definition vm_trace.c:869
RUBY_EVENT_END
#define RUBY_EVENT_END
Encountered an end of a class clause.
Definition event.h:40
RUBY_EVENT_C_CALL
#define RUBY_EVENT_C_CALL
A method, written in C, is called.
Definition event.h:43
RUBY_EVENT_TRACEPOINT_ALL
#define RUBY_EVENT_TRACEPOINT_ALL
Bitmask of extended events.
Definition event.h:62
rb_add_event_hook
void rb_add_event_hook(rb_event_hook_func_t func, rb_event_flag_t events, VALUE data)
Registers an event hook function.
Definition vm_trace.c:207
RUBY_EVENT_RAISE
#define RUBY_EVENT_RAISE
Encountered a raise statement.
Definition event.h:45
RUBY_EVENT_B_RETURN
#define RUBY_EVENT_B_RETURN
Encountered a next statement.
Definition event.h:56
RUBY_EVENT_SCRIPT_COMPILED
#define RUBY_EVENT_SCRIPT_COMPILED
Encountered an eval.
Definition event.h:60
RUBY_INTERNAL_EVENT_MASK
#define RUBY_INTERNAL_EVENT_MASK
Bitmask of internal events.
Definition event.h:101
rb_remove_event_hook
int rb_remove_event_hook(rb_event_hook_func_t func)
Removes the passed function from the list of event hooks.
Definition vm_trace.c:315
RUBY_EVENT_ALL
#define RUBY_EVENT_ALL
Bitmask of traditional events.
Definition event.h:46
RUBY_EVENT_THREAD_BEGIN
#define RUBY_EVENT_THREAD_BEGIN
Encountered a new thread.
Definition event.h:57
RUBY_EVENT_CLASS
#define RUBY_EVENT_CLASS
Encountered a new class.
Definition event.h:39
rb_event_hook_func_t
void(* rb_event_hook_func_t)(rb_event_flag_t evflag, VALUE data, VALUE self, ID mid, VALUE klass)
Type of event hooks.
Definition event.h:120
RUBY_EVENT_LINE
#define RUBY_EVENT_LINE
Encountered a new line.
Definition event.h:38
RUBY_EVENT_RETURN
#define RUBY_EVENT_RETURN
Encountered a return statement.
Definition event.h:42
RUBY_EVENT_C_RETURN
#define RUBY_EVENT_C_RETURN
Return from a method, written in C.
Definition event.h:44
RUBY_EVENT_B_CALL
#define RUBY_EVENT_B_CALL
Encountered an yield statement.
Definition event.h:55
RUBY_INTERNAL_EVENT_FREEOBJ
#define RUBY_INTERNAL_EVENT_FREEOBJ
Object swept.
Definition event.h:94
rb_event_flag_t
uint32_t rb_event_flag_t
Represents event(s).
Definition event.h:108
RUBY_EVENT_CALL
#define RUBY_EVENT_CALL
A method, written in Ruby, is called.
Definition event.h:41
RUBY_INTERNAL_EVENT_NEWOBJ
#define RUBY_INTERNAL_EVENT_NEWOBJ
Object allocated.
Definition event.h:93
RUBY_EVENT_THREAD_END
#define RUBY_EVENT_THREAD_END
Encountered an end of a thread.
Definition event.h:58
RUBY_EVENT_RESCUE
#define RUBY_EVENT_RESCUE
Encountered a rescue statement.
Definition event.h:61
rb_define_class
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition class.c:980
rb_block_given_p
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition eval.c:937
rb_str_new2
#define rb_str_new2
Old name of rb_str_new_cstr.
Definition string.h:1675
ALLOC
#define ALLOC
Old name of RB_ALLOC.
Definition memory.h:400
xfree
#define xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
Qundef
#define Qundef
Old name of RUBY_Qundef.
Definition special_consts.h:62
INT2FIX
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
ID2SYM
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
ZALLOC
#define ZALLOC
Old name of RB_ZALLOC.
Definition memory.h:402
FIX2INT
#define FIX2INT
Old name of RB_FIX2INT.
Definition int.h:41
NUM2UINT
#define NUM2UINT
Old name of RB_NUM2UINT.
Definition int.h:45
T_ICLASS
#define T_ICLASS
Old name of RUBY_T_ICLASS.
Definition value_type.h:66
rb_ary_new3
#define rb_ary_new3
Old name of rb_ary_new_from_args.
Definition array.h:658
Qtrue
#define Qtrue
Old name of RUBY_Qtrue.
Definition special_consts.h:61
Qnil
#define Qnil
Old name of RUBY_Qnil.
Definition special_consts.h:60
Qfalse
#define Qfalse
Old name of RUBY_Qfalse.
Definition special_consts.h:59
T_ARRAY
#define T_ARRAY
Old name of RUBY_T_ARRAY.
Definition value_type.h:56
NIL_P
#define NIL_P
Old name of RB_NIL_P.
Definition special_consts.h:55
rb_eTypeError
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1430
rb_eRuntimeError
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1428
rb_obj_hide
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
Definition object.c:104
rb_cThread
VALUE rb_cThread
Thread class.
Definition vm.c:544
RB_OBJ_WRITTEN
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
Definition gc.h:615
RB_OBJ_WRITE
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:603
rb_funcall
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1099
rb_hash_new
VALUE rb_hash_new(void)
Creates a new, empty hash object.
Definition hash.c:1477
rb_block_proc
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
Definition proc.c:839
rb_proc_call_with_block
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,...
Definition proc.c:1021
rb_obj_is_method
VALUE rb_obj_is_method(VALUE recv)
Queries if the given object is a method.
Definition proc.c:1652
rb_binding_new
VALUE rb_binding_new(void)
Snapshots the current execution context and turn it into an instance of rb_cBinding.
Definition proc.c:325
rb_obj_is_proc
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
Definition proc.c:120
rb_thread_current
VALUE rb_thread_current(void)
Obtains the "current" thread.
Definition thread.c:2984
rb_undef_alloc_func
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
Definition vm_method.c:1297
rb_intern_const
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
Definition symbol.h:284
rb_sym2str
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
Definition symbol.c:971
ractor.h
rb_ractor_shareable_p
static bool rb_ractor_shareable_p(VALUE obj)
Queries if multiple Ractors can share the passed object or not.
Definition ractor.h:249
rb_yield
VALUE rb_yield(VALUE val)
Yields the block.
Definition vm_eval.c:1354
RB_GC_GUARD
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:167
ruby::backward::cxxanyargs::rb_hash_foreach
void rb_hash_foreach(VALUE q, int_type *w, VALUE e)
Iteration over the given hash.
Definition cxxanyargs.hpp:482
ruby::backward::cxxanyargs::rb_ensure
VALUE rb_ensure(type *q, VALUE w, type *e, VALUE r)
An equivalent of ensure clause.
Definition cxxanyargs.hpp:310
RARRAY_LEN
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:51
RARRAY_AREF
#define RARRAY_AREF(a, i)
Definition rarray.h:403
RBASIC
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
RUBY_TYPED_DEFAULT_FREE
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
Definition rtypeddata.h:79
TypedData_Get_Struct
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
Definition rtypeddata.h:515
rb_data_type_t
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:197
TypedData_Make_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...
Definition rtypeddata.h:497
RTEST
#define RTEST
This is an old name of RB_TEST.
Definition special_consts.h:51
rb_event_hook_struct
Definition vm_trace.c:46
rb_postponed_job_queue
Definition vm_trace.c:1692
rb_tp_struct
Definition vm_trace.c:763
rb_trace_arg_struct
Definition vm_core.h:2138
rb_workqueue_job
Definition vm_trace.c:1645
rb_nativethread_lock_lock
void rb_nativethread_lock_lock(rb_nativethread_lock_t *lock)
Blocks until the current thread obtains a lock.
Definition thread.c:300
rb_nativethread_lock_unlock
void rb_nativethread_lock_unlock(rb_nativethread_lock_t *lock)
Releases a lock.
Definition thread.c:306
ID
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
RB_TYPE_P
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376