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