Ruby 3.4.1p0 (2024-12-25 revision 48d4efcb85000e1ebae42004e963b5d0cedddcf2)
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;
57} rb_event_hook_t;
58
59typedef void (*rb_event_hook_raw_arg_func_t)(VALUE data, const rb_trace_arg_t *arg);
60
61#define MAX_EVENT_NUM 32
62
63void
64rb_hook_list_mark(rb_hook_list_t *hooks)
65{
66 rb_event_hook_t *hook = hooks->hooks;
67
68 while (hook) {
69 rb_gc_mark(hook->data);
70 hook = hook->next;
71 }
72}
73
74void
75rb_hook_list_mark_and_update(rb_hook_list_t *hooks)
76{
77 rb_event_hook_t *hook = hooks->hooks;
78
79 while (hook) {
80 rb_gc_mark_and_move(&hook->data);
81 hook = hook->next;
82 }
83}
84
85static void clean_hooks(rb_hook_list_t *list);
86
87void
88rb_hook_list_free(rb_hook_list_t *hooks)
89{
90 hooks->need_clean = true;
91
92 if (hooks->running == 0) {
93 clean_hooks(hooks);
94 }
95}
96
97/* ruby_vm_event_flags management */
98
99void rb_clear_attr_ccs(void);
100void rb_clear_bf_ccs(void);
101
102static void
103update_global_event_hook(rb_event_flag_t prev_events, rb_event_flag_t new_events)
104{
105 rb_event_flag_t new_iseq_events = new_events & ISEQ_TRACE_EVENTS;
106 rb_event_flag_t enabled_iseq_events = ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS;
107 bool first_time_iseq_events_p = new_iseq_events & ~enabled_iseq_events;
108 bool enable_c_call = (prev_events & RUBY_EVENT_C_CALL) == 0 && (new_events & RUBY_EVENT_C_CALL);
109 bool enable_c_return = (prev_events & RUBY_EVENT_C_RETURN) == 0 && (new_events & RUBY_EVENT_C_RETURN);
110 bool enable_call = (prev_events & RUBY_EVENT_CALL) == 0 && (new_events & RUBY_EVENT_CALL);
111 bool enable_return = (prev_events & RUBY_EVENT_RETURN) == 0 && (new_events & RUBY_EVENT_RETURN);
112
113 // Modify ISEQs or CCs to enable tracing
114 if (first_time_iseq_events_p) {
115 // write all ISeqs only when new events are added for the first time
116 rb_iseq_trace_set_all(new_iseq_events | enabled_iseq_events);
117 }
118 // if c_call or c_return is activated
119 else if (enable_c_call || enable_c_return) {
120 rb_clear_attr_ccs();
121 }
122 else if (enable_call || enable_return) {
123 rb_clear_bf_ccs();
124 }
125
126 ruby_vm_event_flags = new_events;
127 ruby_vm_event_enabled_global_flags |= new_events;
128 rb_objspace_set_event_hook(new_events);
129
130 // Invalidate JIT code as needed
131 if (first_time_iseq_events_p || enable_c_call || enable_c_return) {
132 // Invalidate all code when ISEQs are modified to use trace_* insns above.
133 // Also invalidate when enabling c_call or c_return because generated code
134 // never fires these events.
135 // Internal events fire inside C routines so don't need special handling.
136 // Do this after event flags updates so other ractors see updated vm events
137 // when they wake up.
138 rb_yjit_tracing_invalidate_all();
139 rb_rjit_tracing_invalidate_all(new_iseq_events);
140 }
141}
142
143/* add/remove hooks */
144
145static rb_event_hook_t *
146alloc_event_hook(rb_event_hook_func_t func, rb_event_flag_t events, VALUE data, rb_event_hook_flag_t hook_flags)
147{
148 rb_event_hook_t *hook;
149
150 if ((events & RUBY_INTERNAL_EVENT_MASK) && (events & ~RUBY_INTERNAL_EVENT_MASK)) {
151 rb_raise(rb_eTypeError, "Can not specify normal event and internal event simultaneously.");
152 }
153
154 hook = ALLOC(rb_event_hook_t);
155 hook->hook_flags = hook_flags;
156 hook->events = events;
157 hook->func = func;
158 hook->data = data;
159
160 /* no filters */
161 hook->filter.th = NULL;
162 hook->filter.target_line = 0;
163
164 return hook;
165}
166
167static void
168hook_list_connect(VALUE list_owner, rb_hook_list_t *list, rb_event_hook_t *hook, int global_p)
169{
170 rb_event_flag_t prev_events = list->events;
171 hook->next = list->hooks;
172 list->hooks = hook;
173 list->events |= hook->events;
174
175 if (global_p) {
176 /* global hooks are root objects at GC mark. */
177 update_global_event_hook(prev_events, list->events);
178 }
179 else {
180 RB_OBJ_WRITTEN(list_owner, Qundef, hook->data);
181 }
182}
183
184static void
185connect_event_hook(const rb_execution_context_t *ec, rb_event_hook_t *hook)
186{
187 rb_hook_list_t *list = rb_ec_ractor_hooks(ec);
188 hook_list_connect(Qundef, list, hook, TRUE);
189}
190
191static void
192rb_threadptr_add_event_hook(const rb_execution_context_t *ec, rb_thread_t *th,
193 rb_event_hook_func_t func, rb_event_flag_t events, VALUE data, rb_event_hook_flag_t hook_flags)
194{
195 rb_event_hook_t *hook = alloc_event_hook(func, events, data, hook_flags);
196 hook->filter.th = th;
197 connect_event_hook(ec, hook);
198}
199
200void
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 enum ruby_tag_type state = 0;
439
440 /* setup */
441 ec->local_storage_recursive_hash = ec->local_storage_recursive_hash_for_trace;
442 ec->errinfo = Qnil;
443 ec->trace_arg = trace_arg;
444
445 /* kick hooks */
446 if ((state = exec_hooks_protected(ec, hooks, trace_arg)) == TAG_NONE) {
447 ec->errinfo = errinfo;
448 }
449
450 /* cleanup */
451 ec->trace_arg = NULL;
452 ec->local_storage_recursive_hash_for_trace = ec->local_storage_recursive_hash;
453 ec->local_storage_recursive_hash = old_recursive;
454
455 if (state) {
456 if (pop_p) {
457 if (VM_FRAME_FINISHED_P(ec->cfp)) {
458 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 (RCLASS_SINGLETON_P(klass)) {
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 if (!me) {
941 me = rb_method_entry_without_refinements(trace_arg->klass, trace_arg->id, &iclass);
942 }
943 return rb_unnamed_parameters(rb_method_entry_arity(me));
944 }
945 break;
946 }
947 case RUBY_EVENT_RAISE:
948 case RUBY_EVENT_LINE:
949 case RUBY_EVENT_CLASS:
950 case RUBY_EVENT_END:
953 rb_raise(rb_eRuntimeError, "not supported by this event");
954 break;
955 }
956 return Qnil;
957}
958
959VALUE
961{
962 fill_id_and_klass(trace_arg);
963 return trace_arg->id ? ID2SYM(trace_arg->id) : Qnil;
964}
965
966VALUE
968{
969 fill_id_and_klass(trace_arg);
970 return trace_arg->called_id ? ID2SYM(trace_arg->called_id) : Qnil;
971}
972
973VALUE
975{
976 fill_id_and_klass(trace_arg);
977 return trace_arg->klass;
978}
979
980VALUE
982{
983 rb_control_frame_t *cfp;
984 switch (trace_arg->event) {
987 return Qnil;
988 }
989 cfp = rb_vm_get_binding_creatable_next_cfp(trace_arg->ec, trace_arg->cfp);
990
991 if (cfp && imemo_type_p((VALUE)cfp->iseq, imemo_iseq)) {
992 return rb_vm_make_binding(trace_arg->ec, cfp);
993 }
994 else {
995 return Qnil;
996 }
997}
998
999VALUE
1001{
1002 return trace_arg->self;
1003}
1004
1005VALUE
1007{
1008 if (trace_arg->event & (RUBY_EVENT_RETURN | RUBY_EVENT_C_RETURN | RUBY_EVENT_B_RETURN)) {
1009 /* ok */
1010 }
1011 else {
1012 rb_raise(rb_eRuntimeError, "not supported by this event");
1013 }
1014 if (UNDEF_P(trace_arg->data)) {
1015 rb_bug("rb_tracearg_return_value: unreachable");
1016 }
1017 return trace_arg->data;
1018}
1019
1020VALUE
1022{
1023 if (trace_arg->event & (RUBY_EVENT_RAISE | RUBY_EVENT_RESCUE)) {
1024 /* ok */
1025 }
1026 else {
1027 rb_raise(rb_eRuntimeError, "not supported by this event");
1028 }
1029 if (UNDEF_P(trace_arg->data)) {
1030 rb_bug("rb_tracearg_raised_exception: unreachable");
1031 }
1032 return trace_arg->data;
1033}
1034
1035VALUE
1036rb_tracearg_eval_script(rb_trace_arg_t *trace_arg)
1037{
1038 VALUE data = trace_arg->data;
1039
1040 if (trace_arg->event & (RUBY_EVENT_SCRIPT_COMPILED)) {
1041 /* ok */
1042 }
1043 else {
1044 rb_raise(rb_eRuntimeError, "not supported by this event");
1045 }
1046 if (UNDEF_P(data)) {
1047 rb_bug("rb_tracearg_raised_exception: unreachable");
1048 }
1049 if (rb_obj_is_iseq(data)) {
1050 return Qnil;
1051 }
1052 else {
1053 VM_ASSERT(RB_TYPE_P(data, T_ARRAY));
1054 /* [src, iseq] */
1055 return RARRAY_AREF(data, 0);
1056 }
1057}
1058
1059VALUE
1060rb_tracearg_instruction_sequence(rb_trace_arg_t *trace_arg)
1061{
1062 VALUE data = trace_arg->data;
1063
1064 if (trace_arg->event & (RUBY_EVENT_SCRIPT_COMPILED)) {
1065 /* ok */
1066 }
1067 else {
1068 rb_raise(rb_eRuntimeError, "not supported by this event");
1069 }
1070 if (UNDEF_P(data)) {
1071 rb_bug("rb_tracearg_raised_exception: unreachable");
1072 }
1073
1074 if (rb_obj_is_iseq(data)) {
1075 return rb_iseqw_new((const rb_iseq_t *)data);
1076 }
1077 else {
1078 VM_ASSERT(RB_TYPE_P(data, T_ARRAY));
1079 VM_ASSERT(rb_obj_is_iseq(RARRAY_AREF(data, 1)));
1080
1081 /* [src, iseq] */
1082 return rb_iseqw_new((const rb_iseq_t *)RARRAY_AREF(data, 1));
1083 }
1084}
1085
1086VALUE
1088{
1089 if (trace_arg->event & (RUBY_INTERNAL_EVENT_NEWOBJ | RUBY_INTERNAL_EVENT_FREEOBJ)) {
1090 /* ok */
1091 }
1092 else {
1093 rb_raise(rb_eRuntimeError, "not supported by this event");
1094 }
1095 if (UNDEF_P(trace_arg->data)) {
1096 rb_bug("rb_tracearg_object: unreachable");
1097 }
1098 return trace_arg->data;
1099}
1100
1101static VALUE
1102tracepoint_attr_event(rb_execution_context_t *ec, VALUE tpval)
1103{
1104 return rb_tracearg_event(get_trace_arg());
1105}
1106
1107static VALUE
1108tracepoint_attr_lineno(rb_execution_context_t *ec, VALUE tpval)
1109{
1110 return rb_tracearg_lineno(get_trace_arg());
1111}
1112static VALUE
1113tracepoint_attr_path(rb_execution_context_t *ec, VALUE tpval)
1114{
1115 return rb_tracearg_path(get_trace_arg());
1116}
1117
1118static VALUE
1119tracepoint_attr_parameters(rb_execution_context_t *ec, VALUE tpval)
1120{
1121 return rb_tracearg_parameters(get_trace_arg());
1122}
1123
1124static VALUE
1125tracepoint_attr_method_id(rb_execution_context_t *ec, VALUE tpval)
1126{
1127 return rb_tracearg_method_id(get_trace_arg());
1128}
1129
1130static VALUE
1131tracepoint_attr_callee_id(rb_execution_context_t *ec, VALUE tpval)
1132{
1133 return rb_tracearg_callee_id(get_trace_arg());
1134}
1135
1136static VALUE
1137tracepoint_attr_defined_class(rb_execution_context_t *ec, VALUE tpval)
1138{
1139 return rb_tracearg_defined_class(get_trace_arg());
1140}
1141
1142static VALUE
1143tracepoint_attr_binding(rb_execution_context_t *ec, VALUE tpval)
1144{
1145 return rb_tracearg_binding(get_trace_arg());
1146}
1147
1148static VALUE
1149tracepoint_attr_self(rb_execution_context_t *ec, VALUE tpval)
1150{
1151 return rb_tracearg_self(get_trace_arg());
1152}
1153
1154static VALUE
1155tracepoint_attr_return_value(rb_execution_context_t *ec, VALUE tpval)
1156{
1157 return rb_tracearg_return_value(get_trace_arg());
1158}
1159
1160static VALUE
1161tracepoint_attr_raised_exception(rb_execution_context_t *ec, VALUE tpval)
1162{
1163 return rb_tracearg_raised_exception(get_trace_arg());
1164}
1165
1166static VALUE
1167tracepoint_attr_eval_script(rb_execution_context_t *ec, VALUE tpval)
1168{
1169 return rb_tracearg_eval_script(get_trace_arg());
1170}
1171
1172static VALUE
1173tracepoint_attr_instruction_sequence(rb_execution_context_t *ec, VALUE tpval)
1174{
1175 return rb_tracearg_instruction_sequence(get_trace_arg());
1176}
1177
1178static void
1179tp_call_trace(VALUE tpval, rb_trace_arg_t *trace_arg)
1180{
1181 rb_tp_t *tp = tpptr(tpval);
1182
1183 if (tp->func) {
1184 (*tp->func)(tpval, tp->data);
1185 }
1186 else {
1187 if (tp->ractor == NULL || tp->ractor == GET_RACTOR()) {
1188 rb_proc_call_with_block((VALUE)tp->proc, 1, &tpval, Qnil);
1189 }
1190 }
1191}
1192
1193VALUE
1195{
1196 rb_tp_t *tp;
1197 tp = tpptr(tpval);
1198
1199 if (tp->local_target_set != Qfalse) {
1200 rb_raise(rb_eArgError, "can't nest-enable a targeting TracePoint");
1201 }
1202
1203 if (tp->tracing) {
1204 return Qundef;
1205 }
1206
1207 if (tp->target_th) {
1208 rb_thread_add_event_hook2(tp->target_th->self, (rb_event_hook_func_t)tp_call_trace, tp->events, tpval,
1209 RUBY_EVENT_HOOK_FLAG_SAFE | RUBY_EVENT_HOOK_FLAG_RAW_ARG);
1210 }
1211 else {
1212 rb_add_event_hook2((rb_event_hook_func_t)tp_call_trace, tp->events, tpval,
1213 RUBY_EVENT_HOOK_FLAG_SAFE | RUBY_EVENT_HOOK_FLAG_RAW_ARG);
1214 }
1215 tp->tracing = 1;
1216 return Qundef;
1217}
1218
1219static const rb_iseq_t *
1220iseq_of(VALUE target)
1221{
1222 VALUE iseqv = rb_funcall(rb_cISeq, rb_intern("of"), 1, target);
1223 if (NIL_P(iseqv)) {
1224 rb_raise(rb_eArgError, "specified target is not supported");
1225 }
1226 else {
1227 return rb_iseqw_to_iseq(iseqv);
1228 }
1229}
1230
1231const rb_method_definition_t *rb_method_def(VALUE method); /* proc.c */
1232
1233static VALUE
1234rb_tracepoint_enable_for_target(VALUE tpval, VALUE target, VALUE target_line)
1235{
1236 rb_tp_t *tp = tpptr(tpval);
1237 const rb_iseq_t *iseq = iseq_of(target);
1238 int n = 0;
1239 unsigned int line = 0;
1240 bool target_bmethod = false;
1241
1242 if (tp->tracing > 0) {
1243 rb_raise(rb_eArgError, "can't nest-enable a targeting TracePoint");
1244 }
1245
1246 if (!NIL_P(target_line)) {
1247 if ((tp->events & RUBY_EVENT_LINE) == 0) {
1248 rb_raise(rb_eArgError, "target_line is specified, but line event is not specified");
1249 }
1250 else {
1251 line = NUM2UINT(target_line);
1252 }
1253 }
1254
1255 VM_ASSERT(tp->local_target_set == Qfalse);
1256 RB_OBJ_WRITE(tpval, &tp->local_target_set, rb_obj_hide(rb_ident_hash_new()));
1257
1258 /* bmethod */
1259 if (rb_obj_is_method(target)) {
1260 rb_method_definition_t *def = (rb_method_definition_t *)rb_method_def(target);
1261 if (def->type == VM_METHOD_TYPE_BMETHOD &&
1262 (tp->events & (RUBY_EVENT_CALL | RUBY_EVENT_RETURN))) {
1263 if (def->body.bmethod.hooks == NULL) {
1264 def->body.bmethod.hooks = ZALLOC(rb_hook_list_t);
1265 def->body.bmethod.hooks->is_local = true;
1266 }
1267 rb_hook_list_connect_tracepoint(target, def->body.bmethod.hooks, tpval, 0);
1268 rb_hash_aset(tp->local_target_set, target, Qfalse);
1269 target_bmethod = true;
1270
1271 n++;
1272 }
1273 }
1274
1275 /* iseq */
1276 n += rb_iseq_add_local_tracepoint_recursively(iseq, tp->events, tpval, line, target_bmethod);
1277 rb_hash_aset(tp->local_target_set, (VALUE)iseq, Qtrue);
1278
1279 if ((tp->events & (RUBY_EVENT_CALL | RUBY_EVENT_RETURN)) &&
1280 iseq->body->builtin_attrs & BUILTIN_ATTR_SINGLE_NOARG_LEAF) {
1281 rb_clear_bf_ccs();
1282 }
1283
1284 if (n == 0) {
1285 rb_raise(rb_eArgError, "can not enable any hooks");
1286 }
1287
1288 rb_yjit_tracing_invalidate_all();
1289 rb_rjit_tracing_invalidate_all(tp->events);
1290
1291 ruby_vm_event_local_num++;
1292
1293 tp->tracing = 1;
1294
1295 return Qnil;
1296}
1297
1298static int
1299disable_local_event_iseq_i(VALUE target, VALUE iseq_p, VALUE tpval)
1300{
1301 if (iseq_p) {
1302 rb_iseq_remove_local_tracepoint_recursively((rb_iseq_t *)target, tpval);
1303 }
1304 else {
1305 /* bmethod */
1306 rb_method_definition_t *def = (rb_method_definition_t *)rb_method_def(target);
1307 rb_hook_list_t *hooks = def->body.bmethod.hooks;
1308 VM_ASSERT(hooks != NULL);
1309 rb_hook_list_remove_tracepoint(hooks, tpval);
1310
1311 if (hooks->events == 0) {
1312 rb_hook_list_free(def->body.bmethod.hooks);
1313 def->body.bmethod.hooks = NULL;
1314 }
1315 }
1316 return ST_CONTINUE;
1317}
1318
1319VALUE
1321{
1322 rb_tp_t *tp;
1323
1324 tp = tpptr(tpval);
1325
1326 if (tp->local_target_set) {
1327 rb_hash_foreach(tp->local_target_set, disable_local_event_iseq_i, tpval);
1328 RB_OBJ_WRITE(tpval, &tp->local_target_set, Qfalse);
1329 ruby_vm_event_local_num--;
1330 }
1331 else {
1332 if (tp->target_th) {
1333 rb_thread_remove_event_hook_with_data(tp->target_th->self, (rb_event_hook_func_t)tp_call_trace, tpval);
1334 }
1335 else {
1337 }
1338 }
1339 tp->tracing = 0;
1340 tp->target_th = NULL;
1341 return Qundef;
1342}
1343
1344void
1345rb_hook_list_connect_tracepoint(VALUE target, rb_hook_list_t *list, VALUE tpval, unsigned int target_line)
1346{
1347 rb_tp_t *tp = tpptr(tpval);
1348 rb_event_hook_t *hook = alloc_event_hook((rb_event_hook_func_t)tp_call_trace, tp->events & ISEQ_TRACE_EVENTS, tpval,
1349 RUBY_EVENT_HOOK_FLAG_SAFE | RUBY_EVENT_HOOK_FLAG_RAW_ARG);
1350 hook->filter.target_line = target_line;
1351 hook_list_connect(target, list, hook, FALSE);
1352}
1353
1354void
1355rb_hook_list_remove_tracepoint(rb_hook_list_t *list, VALUE tpval)
1356{
1357 rb_event_hook_t *hook = list->hooks;
1358 rb_event_flag_t events = 0;
1359
1360 while (hook) {
1361 if (hook->data == tpval) {
1362 hook->hook_flags |= RUBY_EVENT_HOOK_FLAG_DELETED;
1363 list->need_clean = true;
1364 }
1365 else if ((hook->hook_flags & RUBY_EVENT_HOOK_FLAG_DELETED) == 0) {
1366 events |= hook->events;
1367 }
1368 hook = hook->next;
1369 }
1370
1371 list->events = events;
1372}
1373
1374static VALUE
1375tracepoint_enable_m(rb_execution_context_t *ec, VALUE tpval, VALUE target, VALUE target_line, VALUE target_thread)
1376{
1377 rb_tp_t *tp = tpptr(tpval);
1378 int previous_tracing = tp->tracing;
1379
1380 if (target_thread == sym_default) {
1381 if (rb_block_given_p() && NIL_P(target) && NIL_P(target_line)) {
1382 target_thread = rb_thread_current();
1383 }
1384 else {
1385 target_thread = Qnil;
1386 }
1387 }
1388
1389 /* check target_thread */
1390 if (RTEST(target_thread)) {
1391 if (tp->target_th) {
1392 rb_raise(rb_eArgError, "can not override target_thread filter");
1393 }
1394 tp->target_th = rb_thread_ptr(target_thread);
1395
1396 RUBY_ASSERT(tp->target_th->self == target_thread);
1397 RB_OBJ_WRITTEN(tpval, Qundef, target_thread);
1398 }
1399 else {
1400 tp->target_th = NULL;
1401 }
1402
1403 if (NIL_P(target)) {
1404 if (!NIL_P(target_line)) {
1405 rb_raise(rb_eArgError, "only target_line is specified");
1406 }
1407 rb_tracepoint_enable(tpval);
1408 }
1409 else {
1410 rb_tracepoint_enable_for_target(tpval, target, target_line);
1411 }
1412
1413 if (rb_block_given_p()) {
1414 return rb_ensure(rb_yield, Qundef,
1415 previous_tracing ? rb_tracepoint_enable : rb_tracepoint_disable,
1416 tpval);
1417 }
1418 else {
1419 return RBOOL(previous_tracing);
1420 }
1421}
1422
1423static VALUE
1424tracepoint_disable_m(rb_execution_context_t *ec, VALUE tpval)
1425{
1426 rb_tp_t *tp = tpptr(tpval);
1427 int previous_tracing = tp->tracing;
1428
1429 if (rb_block_given_p()) {
1430 if (tp->local_target_set != Qfalse) {
1431 rb_raise(rb_eArgError, "can't disable a targeting TracePoint in a block");
1432 }
1433
1434 rb_tracepoint_disable(tpval);
1435 return rb_ensure(rb_yield, Qundef,
1436 previous_tracing ? rb_tracepoint_enable : rb_tracepoint_disable,
1437 tpval);
1438 }
1439 else {
1440 rb_tracepoint_disable(tpval);
1441 return RBOOL(previous_tracing);
1442 }
1443}
1444
1445VALUE
1447{
1448 rb_tp_t *tp = tpptr(tpval);
1449 return RBOOL(tp->tracing);
1450}
1451
1452static VALUE
1453tracepoint_enabled_p(rb_execution_context_t *ec, VALUE tpval)
1454{
1455 return rb_tracepoint_enabled_p(tpval);
1456}
1457
1458static VALUE
1459tracepoint_new(VALUE klass, rb_thread_t *target_th, rb_event_flag_t events, void (func)(VALUE, void*), void *data, VALUE proc)
1460{
1461 VALUE tpval = tp_alloc(klass);
1462 rb_tp_t *tp;
1463 TypedData_Get_Struct(tpval, rb_tp_t, &tp_data_type, tp);
1464
1465 RB_OBJ_WRITE(tpval, &tp->proc, proc);
1466 tp->ractor = rb_ractor_shareable_p(proc) ? NULL : GET_RACTOR();
1467 tp->func = func;
1468 tp->data = data;
1469 tp->events = events;
1470 tp->self = tpval;
1471
1472 return tpval;
1473}
1474
1475VALUE
1476rb_tracepoint_new(VALUE target_thval, rb_event_flag_t events, void (*func)(VALUE, void *), void *data)
1477{
1478 rb_thread_t *target_th = NULL;
1479
1480 if (RTEST(target_thval)) {
1481 target_th = rb_thread_ptr(target_thval);
1482 /* TODO: Test it!
1483 * Warning: This function is not tested.
1484 */
1485 }
1486 return tracepoint_new(rb_cTracePoint, target_th, events, func, data, Qundef);
1487}
1488
1489static VALUE
1490tracepoint_new_s(rb_execution_context_t *ec, VALUE self, VALUE args)
1491{
1492 rb_event_flag_t events = 0;
1493 long i;
1494 long argc = RARRAY_LEN(args);
1495
1496 if (argc > 0) {
1497 for (i=0; i<argc; i++) {
1498 events |= symbol2event_flag(RARRAY_AREF(args, i));
1499 }
1500 }
1501 else {
1503 }
1504
1505 if (!rb_block_given_p()) {
1506 rb_raise(rb_eArgError, "must be called with a block");
1507 }
1508
1509 return tracepoint_new(self, 0, events, 0, 0, rb_block_proc());
1510}
1511
1512static VALUE
1513tracepoint_trace_s(rb_execution_context_t *ec, VALUE self, VALUE args)
1514{
1515 VALUE trace = tracepoint_new_s(ec, self, args);
1516 rb_tracepoint_enable(trace);
1517 return trace;
1518}
1519
1520static VALUE
1521tracepoint_inspect(rb_execution_context_t *ec, VALUE self)
1522{
1523 rb_tp_t *tp = tpptr(self);
1524 rb_trace_arg_t *trace_arg = GET_EC()->trace_arg;
1525
1526 if (trace_arg) {
1527 switch (trace_arg->event) {
1528 case RUBY_EVENT_LINE:
1529 {
1530 VALUE sym = rb_tracearg_method_id(trace_arg);
1531 if (NIL_P(sym))
1532 break;
1533 return rb_sprintf("#<TracePoint:%"PRIsVALUE" %"PRIsVALUE":%d in '%"PRIsVALUE"'>",
1534 rb_tracearg_event(trace_arg),
1535 rb_tracearg_path(trace_arg),
1536 FIX2INT(rb_tracearg_lineno(trace_arg)),
1537 sym);
1538 }
1539 case RUBY_EVENT_CALL:
1540 case RUBY_EVENT_C_CALL:
1541 case RUBY_EVENT_RETURN:
1543 return rb_sprintf("#<TracePoint:%"PRIsVALUE" '%"PRIsVALUE"' %"PRIsVALUE":%d>",
1544 rb_tracearg_event(trace_arg),
1545 rb_tracearg_method_id(trace_arg),
1546 rb_tracearg_path(trace_arg),
1547 FIX2INT(rb_tracearg_lineno(trace_arg)));
1550 return rb_sprintf("#<TracePoint:%"PRIsVALUE" %"PRIsVALUE">",
1551 rb_tracearg_event(trace_arg),
1552 rb_tracearg_self(trace_arg));
1553 default:
1554 break;
1555 }
1556 return rb_sprintf("#<TracePoint:%"PRIsVALUE" %"PRIsVALUE":%d>",
1557 rb_tracearg_event(trace_arg),
1558 rb_tracearg_path(trace_arg),
1559 FIX2INT(rb_tracearg_lineno(trace_arg)));
1560 }
1561 else {
1562 return rb_sprintf("#<TracePoint:%s>", tp->tracing ? "enabled" : "disabled");
1563 }
1564}
1565
1566static void
1567tracepoint_stat_event_hooks(VALUE hash, VALUE key, rb_event_hook_t *hook)
1568{
1569 int active = 0, deleted = 0;
1570
1571 while (hook) {
1572 if (hook->hook_flags & RUBY_EVENT_HOOK_FLAG_DELETED) {
1573 deleted++;
1574 }
1575 else {
1576 active++;
1577 }
1578 hook = hook->next;
1579 }
1580
1581 rb_hash_aset(hash, key, rb_ary_new3(2, INT2FIX(active), INT2FIX(deleted)));
1582}
1583
1584static VALUE
1585tracepoint_stat_s(rb_execution_context_t *ec, VALUE self)
1586{
1587 rb_vm_t *vm = GET_VM();
1588 VALUE stat = rb_hash_new();
1589
1590 tracepoint_stat_event_hooks(stat, vm->self, rb_ec_ractor_hooks(ec)->hooks);
1591 /* TODO: thread local hooks */
1592
1593 return stat;
1594}
1595
1596static VALUE
1597disallow_reentry(VALUE val)
1598{
1599 rb_trace_arg_t *arg = (rb_trace_arg_t *)val;
1600 rb_execution_context_t *ec = GET_EC();
1601 if (ec->trace_arg != NULL) rb_bug("should be NULL, but %p", (void *)ec->trace_arg);
1602 ec->trace_arg = arg;
1603 return Qnil;
1604}
1605
1606static VALUE
1607tracepoint_allow_reentry(rb_execution_context_t *ec, VALUE self)
1608{
1609 const rb_trace_arg_t *arg = ec->trace_arg;
1610 if (arg == NULL) rb_raise(rb_eRuntimeError, "No need to allow reentrance.");
1611 ec->trace_arg = NULL;
1612 return rb_ensure(rb_yield, Qnil, disallow_reentry, (VALUE)arg);
1613}
1614
1615#include "trace_point.rbinc"
1616
1617/* This function is called from inits.c */
1618void
1619Init_vm_trace(void)
1620{
1621 sym_default = ID2SYM(rb_intern_const("default"));
1622
1623 /* trace_func */
1624 rb_define_global_function("set_trace_func", set_trace_func, 1);
1625 rb_define_method(rb_cThread, "set_trace_func", thread_set_trace_func_m, 1);
1626 rb_define_method(rb_cThread, "add_trace_func", thread_add_trace_func_m, 1);
1627
1628 rb_cTracePoint = rb_define_class("TracePoint", rb_cObject);
1629 rb_undef_alloc_func(rb_cTracePoint);
1630}
1631
1632/*
1633 * Ruby actually has two separate mechanisms for enqueueing work from contexts
1634 * where it is not safe to run Ruby code, to run later on when it is safe. One
1635 * is async-signal-safe but more limited, and accessed through the
1636 * `rb_postponed_job_preregister` and `rb_postponed_job_trigger` functions. The
1637 * other is more flexible but cannot be used in signal handlers, and is accessed
1638 * through the `rb_workqueue_register` function.
1639 *
1640 * The postponed job functions form part of Ruby's extension API, but the
1641 * workqueue functions are for internal use only.
1642 */
1643
1645 struct ccan_list_node jnode; /* <=> vm->workqueue */
1647 void *data;
1648};
1649
1650// Used for VM memsize reporting. Returns the size of a list of rb_workqueue_job
1651// structs. Defined here because the struct definition lives here as well.
1652size_t
1653rb_vm_memsize_workqueue(struct ccan_list_head *workqueue)
1654{
1655 struct rb_workqueue_job *work = 0;
1656 size_t size = 0;
1657
1658 ccan_list_for_each(workqueue, work, jnode) {
1659 size += sizeof(struct rb_workqueue_job);
1660 }
1661
1662 return size;
1663}
1664
1665/*
1666 * thread-safe and called from non-Ruby thread
1667 * returns FALSE on failure (ENOMEM), TRUE otherwise
1668 */
1669int
1670rb_workqueue_register(unsigned flags, rb_postponed_job_func_t func, void *data)
1671{
1672 struct rb_workqueue_job *wq_job = malloc(sizeof(*wq_job));
1673 rb_vm_t *vm = GET_VM();
1674
1675 if (!wq_job) return FALSE;
1676 wq_job->func = func;
1677 wq_job->data = data;
1678
1679 rb_nativethread_lock_lock(&vm->workqueue_lock);
1680 ccan_list_add_tail(&vm->workqueue, &wq_job->jnode);
1681 rb_nativethread_lock_unlock(&vm->workqueue_lock);
1682
1683 // TODO: current implementation affects only main ractor
1684 RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(rb_vm_main_ractor_ec(vm));
1685
1686 return TRUE;
1687}
1688
1689#define PJOB_TABLE_SIZE (sizeof(rb_atomic_t) * CHAR_BIT)
1690/* pre-registered jobs table, for async-safe jobs */
1692 struct {
1694 void *data;
1695 } table[PJOB_TABLE_SIZE];
1696 /* Bits in this are set when the corresponding entry in prereg_table has non-zero
1697 * triggered_count; i.e. somebody called rb_postponed_job_trigger */
1698 rb_atomic_t triggered_bitset;
1699} rb_postponed_job_queues_t;
1700
1701void
1702rb_vm_postponed_job_queue_init(rb_vm_t *vm)
1703{
1704 /* use mimmalloc; postponed job registration is a dependency of objspace, so this gets
1705 * called _VERY_ early inside Init_BareVM */
1706 rb_postponed_job_queues_t *pjq = ruby_mimmalloc(sizeof(rb_postponed_job_queues_t));
1707 pjq->triggered_bitset = 0;
1708 memset(pjq->table, 0, sizeof(pjq->table));
1709 vm->postponed_job_queue = pjq;
1710}
1711
1712static rb_execution_context_t *
1713get_valid_ec(rb_vm_t *vm)
1714{
1715 rb_execution_context_t *ec = rb_current_execution_context(false);
1716 if (ec == NULL) ec = rb_vm_main_ractor_ec(vm);
1717 return ec;
1718}
1719
1720void
1721rb_vm_postponed_job_atfork(void)
1722{
1723 rb_vm_t *vm = GET_VM();
1724 rb_postponed_job_queues_t *pjq = vm->postponed_job_queue;
1725 /* make sure we set the interrupt flag on _this_ thread if we carried any pjobs over
1726 * from the other side of the fork */
1727 if (pjq->triggered_bitset) {
1728 RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(get_valid_ec(vm));
1729 }
1730
1731}
1732
1733/* Frees the memory managed by the postponed job infrastructure at shutdown */
1734void
1735rb_vm_postponed_job_free(void)
1736{
1737 rb_vm_t *vm = GET_VM();
1738 ruby_xfree(vm->postponed_job_queue);
1739 vm->postponed_job_queue = NULL;
1740}
1741
1742// Used for VM memsize reporting. Returns the total size of the postponed job
1743// queue infrastructure.
1744size_t
1745rb_vm_memsize_postponed_job_queue(void)
1746{
1747 return sizeof(rb_postponed_job_queues_t);
1748}
1749
1750
1752rb_postponed_job_preregister(unsigned int flags, rb_postponed_job_func_t func, void *data)
1753{
1754 /* The doc comments say that this function should be called under the GVL, because
1755 * that is actually required to get the guarantee that "if a given (func, data) pair
1756 * was already pre-registered, this method will return the same handle instance".
1757 *
1758 * However, the actual implementation here is called without the GVL, from inside
1759 * rb_postponed_job_register, to support that legacy interface. In the presence
1760 * of concurrent calls to both _preregister and _register functions on the same
1761 * func, however, the data may get mixed up between them. */
1762
1763 rb_postponed_job_queues_t *pjq = GET_VM()->postponed_job_queue;
1764 for (unsigned int i = 0; i < PJOB_TABLE_SIZE; i++) {
1765 /* Try and set this slot to equal `func` */
1766 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);
1767 if (existing_func == NULL || existing_func == func) {
1768 /* Either this slot was NULL, and we set it to func, or, this slot was already equal to func.
1769 * In either case, clobber the data with our data. Note that concurrent calls to
1770 * rb_postponed_job_register with the same func & different data will result in either of the
1771 * datas being written */
1772 RUBY_ATOMIC_PTR_EXCHANGE(pjq->table[i].data, data);
1773 return (rb_postponed_job_handle_t)i;
1774 }
1775 else {
1776 /* Try the next slot if this one already has a func in it */
1777 continue;
1778 }
1779 }
1780
1781 /* full */
1782 return POSTPONED_JOB_HANDLE_INVALID;
1783}
1784
1785void
1787{
1788 rb_vm_t *vm = GET_VM();
1789 rb_postponed_job_queues_t *pjq = vm->postponed_job_queue;
1790
1791 RUBY_ATOMIC_OR(pjq->triggered_bitset, (((rb_atomic_t)1UL) << h));
1792 RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(get_valid_ec(vm));
1793}
1794
1795
1796static int
1797pjob_register_legacy_impl(unsigned int flags, rb_postponed_job_func_t func, void *data)
1798{
1799 /* We _know_ calling preregister from a signal handler like this is racy; what is
1800 * and is not promised is very exhaustively documented in debug.h */
1802 if (h == POSTPONED_JOB_HANDLE_INVALID) {
1803 return 0;
1804 }
1806 return 1;
1807}
1808
1809int
1810rb_postponed_job_register(unsigned int flags, rb_postponed_job_func_t func, void *data)
1811{
1812 return pjob_register_legacy_impl(flags, func, data);
1813}
1814
1815int
1816rb_postponed_job_register_one(unsigned int flags, rb_postponed_job_func_t func, void *data)
1817{
1818 return pjob_register_legacy_impl(flags, func, data);
1819}
1820
1821
1822void
1823rb_postponed_job_flush(rb_vm_t *vm)
1824{
1825 rb_postponed_job_queues_t *pjq = GET_VM()->postponed_job_queue;
1826 rb_execution_context_t *ec = GET_EC();
1827 const rb_atomic_t block_mask = POSTPONED_JOB_INTERRUPT_MASK | TRAP_INTERRUPT_MASK;
1828 volatile rb_atomic_t saved_mask = ec->interrupt_mask & block_mask;
1829 VALUE volatile saved_errno = ec->errinfo;
1830 struct ccan_list_head tmp;
1831
1832 ccan_list_head_init(&tmp);
1833
1834 rb_nativethread_lock_lock(&vm->workqueue_lock);
1835 ccan_list_append_list(&tmp, &vm->workqueue);
1836 rb_nativethread_lock_unlock(&vm->workqueue_lock);
1837
1838 rb_atomic_t triggered_bits = RUBY_ATOMIC_EXCHANGE(pjq->triggered_bitset, 0);
1839
1840 ec->errinfo = Qnil;
1841 /* mask POSTPONED_JOB dispatch */
1842 ec->interrupt_mask |= block_mask;
1843 {
1844 EC_PUSH_TAG(ec);
1845 if (EC_EXEC_TAG() == TAG_NONE) {
1846 /* execute postponed jobs */
1847 while (triggered_bits) {
1848 unsigned int i = bit_length(triggered_bits) - 1;
1849 triggered_bits ^= ((1UL) << i); /* toggle ith bit off */
1850 rb_postponed_job_func_t func = pjq->table[i].func;
1851 void *data = pjq->table[i].data;
1852 (func)(data);
1853 }
1854
1855 /* execute workqueue jobs */
1856 struct rb_workqueue_job *wq_job;
1857 while ((wq_job = ccan_list_pop(&tmp, struct rb_workqueue_job, jnode))) {
1858 rb_postponed_job_func_t func = wq_job->func;
1859 void *data = wq_job->data;
1860
1861 free(wq_job);
1862 (func)(data);
1863 }
1864 }
1865 EC_POP_TAG();
1866 }
1867 /* restore POSTPONED_JOB mask */
1868 ec->interrupt_mask &= ~(saved_mask ^ block_mask);
1869 ec->errinfo = saved_errno;
1870
1871 /* If we threw an exception, there might be leftover workqueue items; carry them over
1872 * to a subsequent execution of flush */
1873 if (!ccan_list_empty(&tmp)) {
1874 rb_nativethread_lock_lock(&vm->workqueue_lock);
1875 ccan_list_prepend_list(&vm->workqueue, &tmp);
1876 rb_nativethread_lock_unlock(&vm->workqueue_lock);
1877
1878 RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(GET_EC());
1879 }
1880 /* likewise with any remaining-to-be-executed bits of the preregistered postponed
1881 * job table */
1882 if (triggered_bits) {
1883 RUBY_ATOMIC_OR(pjq->triggered_bitset, triggered_bits);
1884 RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(GET_EC());
1885 }
1886}
#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:315
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:290
#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: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:981
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:1786
VALUE rb_tracepoint_enabled_p(VALUE tpval)
Queries if the passed TracePoint is up and running.
Definition vm_trace.c:1446
VALUE rb_tracearg_object(rb_trace_arg_t *trace_arg)
Queries the allocated/deallocated object that the trace represents.
Definition vm_trace.c:1087
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:967
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:974
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:1476
struct rb_trace_arg_struct rb_trace_arg_t
Type that represents a specific trace event.
Definition debug.h:465
VALUE rb_tracearg_raised_exception(rb_trace_arg_t *trace_arg)
Queries the raised exception that the trace represents.
Definition vm_trace.c:1021
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:1752
VALUE rb_tracepoint_disable(VALUE tpval)
Stops (disables) an already running instance of TracePoint.
Definition vm_trace.c:1320
VALUE rb_tracearg_self(rb_trace_arg_t *trace_arg)
Queries the receiver of the point trace is at.
Definition vm_trace.c:1000
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:1816
VALUE rb_tracearg_return_value(rb_trace_arg_t *trace_arg)
Queries the return value that the trace represents.
Definition vm_trace.c:1006
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:1194
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:1810
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:960
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:980
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition eval.c:937
#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:544
#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:1099
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
Definition proc.c:836
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:1018
VALUE rb_obj_is_method(VALUE recv)
Queries if the given object is a method.
Definition proc.c:1646
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:2984
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
Definition vm_method.c:1291
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:970
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:1354
#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:515
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:197
#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.
void rb_nativethread_lock_lock(rb_nativethread_lock_t *lock)
Blocks until the current thread obtains a lock.
Definition thread.c:300
void rb_nativethread_lock_unlock(rb_nativethread_lock_t *lock)
Releases a lock.
Definition thread.c:306
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