Ruby: vm_trace.c Source File

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

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