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