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