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