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