Ruby 4.1.0dev (2026-04-04 revision 3b6245536cf55da9e8bfcdb03c845fe9ef931d7f)
vm_trace.c (3b6245536cf55da9e8bfcdb03c845fe9ef931d7f)
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 SIZED_FREE(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 SIZED_FREE(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(cfp);
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)CFP_ISEQ(ec->cfp), 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(cfp), 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(cfp), 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
1120static void
1121check_event_support(rb_trace_arg_t *trace_arg, rb_event_flag_t supported)
1122{
1123 if (!(trace_arg->event & supported)) {
1124 rb_raise(rb_eRuntimeError, "not supported by this event");
1125 }
1126}
1127
1128VALUE
1130{
1131 check_event_support(trace_arg, RUBY_EVENT_RETURN | RUBY_EVENT_C_RETURN | RUBY_EVENT_B_RETURN);
1132 if (UNDEF_P(trace_arg->data)) {
1133 rb_bug("rb_tracearg_return_value: unreachable");
1134 }
1135 return trace_arg->data;
1136}
1137
1138VALUE
1140{
1141 check_event_support(trace_arg, RUBY_EVENT_RAISE | RUBY_EVENT_RESCUE);
1142 if (UNDEF_P(trace_arg->data)) {
1143 rb_bug("rb_tracearg_raised_exception: unreachable");
1144 }
1145 return trace_arg->data;
1146}
1147
1148VALUE
1150{
1151 VALUE data = trace_arg->data;
1152
1153 check_event_support(trace_arg, RUBY_EVENT_SCRIPT_COMPILED);
1154 if (UNDEF_P(data)) {
1155 rb_bug("rb_tracearg_eval_script: unreachable");
1156 }
1157 if (rb_obj_is_iseq(data)) {
1158 return Qnil;
1159 }
1160 else {
1161 VM_ASSERT(RB_TYPE_P(data, T_ARRAY));
1162 /* [src, iseq] */
1163 return RARRAY_AREF(data, 0);
1164 }
1165}
1166
1167VALUE
1169{
1170 VALUE data = trace_arg->data;
1171
1172 check_event_support(trace_arg, RUBY_EVENT_SCRIPT_COMPILED);
1173 if (UNDEF_P(data)) {
1174 rb_bug("rb_tracearg_instruction_sequence: unreachable");
1175 }
1176
1177 if (rb_obj_is_iseq(data)) {
1178 return rb_iseqw_new((const rb_iseq_t *)data);
1179 }
1180 else {
1181 VM_ASSERT(RB_TYPE_P(data, T_ARRAY));
1182 VM_ASSERT(rb_obj_is_iseq(RARRAY_AREF(data, 1)));
1183
1184 /* [src, iseq] */
1185 return rb_iseqw_new((const rb_iseq_t *)RARRAY_AREF(data, 1));
1186 }
1187}
1188
1189VALUE
1191{
1192 check_event_support(trace_arg, RUBY_INTERNAL_EVENT_NEWOBJ | RUBY_INTERNAL_EVENT_FREEOBJ);
1193 if (UNDEF_P(trace_arg->data)) {
1194 rb_bug("rb_tracearg_object: unreachable");
1195 }
1196 return trace_arg->data;
1197}
1198
1199static VALUE
1200tracepoint_attr_event(rb_execution_context_t *ec, VALUE tpval)
1201{
1202 return rb_tracearg_event(get_trace_arg());
1203}
1204
1205static VALUE
1206tracepoint_attr_lineno(rb_execution_context_t *ec, VALUE tpval)
1207{
1208 return rb_tracearg_lineno(get_trace_arg());
1209}
1210static VALUE
1211tracepoint_attr_path(rb_execution_context_t *ec, VALUE tpval)
1212{
1213 return rb_tracearg_path(get_trace_arg());
1214}
1215
1216static VALUE
1217tracepoint_attr_parameters(rb_execution_context_t *ec, VALUE tpval)
1218{
1219 return rb_tracearg_parameters(get_trace_arg());
1220}
1221
1222static VALUE
1223tracepoint_attr_method_id(rb_execution_context_t *ec, VALUE tpval)
1224{
1225 return rb_tracearg_method_id(get_trace_arg());
1226}
1227
1228static VALUE
1229tracepoint_attr_callee_id(rb_execution_context_t *ec, VALUE tpval)
1230{
1231 return rb_tracearg_callee_id(get_trace_arg());
1232}
1233
1234static VALUE
1235tracepoint_attr_defined_class(rb_execution_context_t *ec, VALUE tpval)
1236{
1237 return rb_tracearg_defined_class(get_trace_arg());
1238}
1239
1240static VALUE
1241tracepoint_attr_binding(rb_execution_context_t *ec, VALUE tpval)
1242{
1243 return rb_tracearg_binding(get_trace_arg());
1244}
1245
1246static VALUE
1247tracepoint_attr_self(rb_execution_context_t *ec, VALUE tpval)
1248{
1249 return rb_tracearg_self(get_trace_arg());
1250}
1251
1252static VALUE
1253tracepoint_attr_return_value(rb_execution_context_t *ec, VALUE tpval)
1254{
1255 return rb_tracearg_return_value(get_trace_arg());
1256}
1257
1258static VALUE
1259tracepoint_attr_raised_exception(rb_execution_context_t *ec, VALUE tpval)
1260{
1261 return rb_tracearg_raised_exception(get_trace_arg());
1262}
1263
1264static VALUE
1265tracepoint_attr_eval_script(rb_execution_context_t *ec, VALUE tpval)
1266{
1267 return rb_tracearg_eval_script(get_trace_arg());
1268}
1269
1270static VALUE
1271tracepoint_attr_instruction_sequence(rb_execution_context_t *ec, VALUE tpval)
1272{
1273 return rb_tracearg_instruction_sequence(get_trace_arg());
1274}
1275
1276static void
1277tp_call_trace(VALUE tpval, rb_trace_arg_t *trace_arg)
1278{
1279 rb_tp_t *tp = tpptr(tpval);
1280
1281 if (tp->func) {
1282 (*tp->func)(tpval, tp->data);
1283 }
1284 else {
1285 if (tp->ractor == GET_RACTOR()) {
1286 rb_proc_call_with_block((VALUE)tp->proc, 1, &tpval, Qnil);
1287 }
1288 }
1289}
1290
1291VALUE
1293{
1294 rb_tp_t *tp;
1295 tp = tpptr(tpval);
1296
1297 if (tp->local_target_set != Qfalse) {
1298 rb_raise(rb_eArgError, "can't nest-enable a targeting TracePoint");
1299 }
1300
1301 if (tp->tracing) {
1302 return Qundef;
1303 }
1304
1305 if (tp->target_th) {
1306 rb_thread_add_event_hook2(tp->target_th->self, (rb_event_hook_func_t)tp_call_trace, tp->events, tpval,
1307 RUBY_EVENT_HOOK_FLAG_SAFE | RUBY_EVENT_HOOK_FLAG_RAW_ARG);
1308 }
1309 else {
1310 rb_add_event_hook2((rb_event_hook_func_t)tp_call_trace, tp->events, tpval,
1311 RUBY_EVENT_HOOK_FLAG_SAFE | RUBY_EVENT_HOOK_FLAG_RAW_ARG);
1312 }
1313 tp->tracing = 1;
1314 return Qundef;
1315}
1316
1317static const rb_iseq_t *
1318iseq_of(VALUE target)
1319{
1320 VALUE iseqv = rb_funcall(rb_cISeq, rb_intern("of"), 1, target);
1321 if (NIL_P(iseqv)) {
1322 rb_raise(rb_eArgError, "specified target is not supported");
1323 }
1324 else {
1325 return rb_iseqw_to_iseq(iseqv);
1326 }
1327}
1328
1329const rb_method_definition_t *rb_method_def(VALUE method); /* proc.c */
1330
1332rb_method_def_local_hooks(rb_method_definition_t *def, rb_ractor_t *cr, bool create)
1333{
1334 st_data_t val;
1335 rb_hook_list_t *hook_list = NULL;
1336 if (st_lookup(rb_ractor_targeted_hooks(cr), (st_data_t)def, &val)) {
1337 hook_list = (rb_hook_list_t*)val;
1338 RUBY_ASSERT(hook_list->type == hook_list_type_targeted_def);
1339 }
1340 else if (create) {
1341 hook_list = ZALLOC(rb_hook_list_t);
1342 hook_list->type = hook_list_type_targeted_def;
1343 st_insert(&cr->pub.targeted_hooks, (st_data_t)def, (st_data_t)hook_list);
1344 }
1345 return hook_list;
1346}
1347
1348// Enable "local" (targeted) tracepoint
1349static VALUE
1350rb_tracepoint_enable_for_target(VALUE tpval, VALUE target, VALUE target_line)
1351{
1352 rb_tp_t *tp = tpptr(tpval);
1353 const rb_iseq_t *iseq = iseq_of(target); // takes Proc, Iseq, Method
1354 int n = 0;
1355 unsigned int line = 0;
1356 bool target_bmethod = false;
1357 rb_ractor_t *cr = GET_RACTOR();
1358
1359 if (tp->tracing > 0) {
1360 rb_raise(rb_eArgError, "can't nest-enable a targeting TracePoint");
1361 }
1362
1363 if (!NIL_P(target_line)) {
1364 if ((tp->events & RUBY_EVENT_LINE) == 0) {
1365 rb_raise(rb_eArgError, "target_line is specified, but line event is not specified");
1366 }
1367 else {
1368 line = NUM2UINT(target_line);
1369 }
1370 }
1371
1372 VM_ASSERT(tp->local_target_set == Qfalse);
1373 RB_OBJ_WRITE(tpval, &tp->local_target_set, rb_obj_hide(rb_ident_hash_new()));
1374
1375 RB_VM_LOCKING() {
1376 // Rewriting iseq instructions across ractors is not safe unless they are stopped.
1377 rb_vm_barrier();
1378
1379 /* bmethod */
1380 if (rb_obj_is_method(target)) {
1381 rb_method_definition_t *def = (rb_method_definition_t *)rb_method_def(target);
1382 if (def->type == VM_METHOD_TYPE_BMETHOD && (tp->events & (RUBY_EVENT_CALL | RUBY_EVENT_RETURN))) {
1383 rb_hook_list_t *hook_list = rb_method_def_local_hooks(def, cr, true);
1384 rb_hook_list_connect_local_tracepoint(hook_list, tpval, 0);
1385 rb_hash_aset(tp->local_target_set, target, Qfalse); // Qfalse means not an iseq
1386 rb_method_definition_addref(def); // in case `tp` gets GC'd and didn't disable the hook, `def` needs to stay alive
1387 def->body.bmethod.local_hooks_cnt++;
1388 target_bmethod = true;
1389 n++;
1390 }
1391 }
1392
1393 /* iseq */
1394 n += rb_iseq_add_local_tracepoint_recursively(iseq, tp->events, tpval, line, target_bmethod);
1395 if (n > 0) {
1396 rb_hash_aset(tp->local_target_set, (VALUE)iseq, Qtrue);
1397
1398 if ((tp->events & (RUBY_EVENT_CALL | RUBY_EVENT_RETURN)) &&
1399 iseq->body->builtin_attrs & BUILTIN_ATTR_SINGLE_NOARG_LEAF) {
1400 rb_clear_bf_ccs();
1401 }
1402
1403 rb_yjit_tracing_invalidate_all();
1404 rb_zjit_tracing_invalidate_all();
1405 rb_ractor_targeted_hooks_incr(tp->ractor);
1406 if (tp->events & ISEQ_TRACE_EVENTS) {
1407 ruby_vm_iseq_events_enabled++;
1408 }
1409 if ((tp->events & RUBY_EVENT_C_CALL) || (tp->events & RUBY_EVENT_C_RETURN)) {
1410 ruby_vm_c_events_enabled++;
1411 }
1412 tp->tracing = 1;
1413 }
1414 }
1415
1416 if (n == 0) {
1417 rb_raise(rb_eArgError, "can not enable any hooks");
1418 }
1419
1420 return Qnil;
1421}
1422
1423static int
1424disable_local_tracepoint_i(VALUE target, VALUE iseq_p, VALUE tpval)
1425{
1426 rb_tp_t *tp = tpptr(tpval);
1427 rb_ractor_t *cr;
1429 rb_hook_list_t *hook_list;
1430 ASSERT_vm_locking_with_barrier();
1431
1432 if (iseq_p) {
1433 rb_iseq_remove_local_tracepoint_recursively((rb_iseq_t *)target, tpval, tp->ractor);
1434 }
1435 else {
1436 cr = GET_RACTOR();
1437 /* bmethod */
1438 def = (rb_method_definition_t *)rb_method_def(target);
1439 hook_list = rb_method_def_local_hooks(def, cr, false);
1440 RUBY_ASSERT(hook_list != NULL);
1441 if (rb_hook_list_remove_local_tracepoint(hook_list, tpval)) {
1442 RUBY_ASSERT(def->body.bmethod.local_hooks_cnt > 0);
1443 def->body.bmethod.local_hooks_cnt--;
1444 if (hook_list->events == 0) {
1445 st_delete(rb_ractor_targeted_hooks(cr), (st_data_t*)&def, NULL);
1446 rb_hook_list_free(hook_list);
1447 }
1448 rb_method_definition_release(def);
1449 }
1450 }
1451 return ST_CONTINUE;
1452}
1453
1454VALUE
1456{
1457 rb_tp_t *tp;
1458
1459 tp = tpptr(tpval);
1460
1461 if (RTEST(tp->local_target_set)) {
1462 RUBY_ASSERT(GET_RACTOR() == tp->ractor);
1463 RB_VM_LOCKING() {
1464 rb_vm_barrier();
1465
1466 rb_hash_foreach(tp->local_target_set, disable_local_tracepoint_i, tpval);
1467 RB_OBJ_WRITE(tpval, &tp->local_target_set, Qfalse);
1468 rb_ractor_targeted_hooks_decr(tp->ractor);
1469 if (tp->events & ISEQ_TRACE_EVENTS) {
1470 RUBY_ASSERT(ruby_vm_iseq_events_enabled > 0);
1471 ruby_vm_iseq_events_enabled--;
1472 }
1473 if ((tp->events & RUBY_EVENT_C_CALL) || (tp->events & RUBY_EVENT_C_RETURN)) {
1474 RUBY_ASSERT(ruby_vm_c_events_enabled > 0);
1475 ruby_vm_c_events_enabled--;
1476 }
1477 }
1478 }
1479 else {
1480 if (tp->target_th) {
1481 rb_thread_remove_event_hook_with_data(tp->target_th->self, (rb_event_hook_func_t)tp_call_trace, tpval);
1482 }
1483 else {
1485 }
1486 }
1487 tp->tracing = 0;
1488 tp->target_th = NULL;
1489 return Qundef;
1490}
1491
1492// connect a targeted (ie: "local") tracepoint to the hook list for the method
1493// ex: tp.enable(target: method(:puts))
1494void
1495rb_hook_list_connect_local_tracepoint(rb_hook_list_t *list, VALUE tpval, unsigned int target_line)
1496{
1497 rb_tp_t *tp = tpptr(tpval);
1498 rb_event_hook_t *hook = alloc_event_hook((rb_event_hook_func_t)tp_call_trace, tp->events & ISEQ_TRACE_EVENTS, tpval,
1499 RUBY_EVENT_HOOK_FLAG_SAFE | RUBY_EVENT_HOOK_FLAG_RAW_ARG);
1500 hook->filter.target_line = target_line;
1501 hook_list_connect(list, hook, FALSE);
1502}
1503
1504bool
1505rb_hook_list_remove_local_tracepoint(rb_hook_list_t *list, VALUE tpval)
1506{
1507 rb_event_hook_t *hook = list->hooks;
1508 rb_event_flag_t events = 0;
1509 bool removed = false;
1510
1511 while (hook) {
1512 if (hook->data == tpval) {
1513 hook->hook_flags |= RUBY_EVENT_HOOK_FLAG_DELETED;
1514 list->need_clean = true;
1515 removed = true;
1516 }
1517 else if ((hook->hook_flags & RUBY_EVENT_HOOK_FLAG_DELETED) == 0) {
1518 events |= hook->events;
1519 }
1520 hook = hook->next;
1521 }
1522
1523 list->events = events;
1524 return removed;
1525}
1526
1527static VALUE
1528tracepoint_enable_m(rb_execution_context_t *ec, VALUE tpval, VALUE target, VALUE target_line, VALUE target_thread)
1529{
1530 rb_tp_t *tp = tpptr(tpval);
1531 int previous_tracing = tp->tracing;
1532
1533 if (target_thread == sym_default) {
1534 if (rb_block_given_p() && NIL_P(target) && NIL_P(target_line)) {
1535 target_thread = rb_thread_current();
1536 }
1537 else {
1538 target_thread = Qnil;
1539 }
1540 }
1541
1542 /* check target_thread */
1543 if (RTEST(target_thread)) {
1544 if (tp->target_th) {
1545 rb_raise(rb_eArgError, "can not override target_thread filter");
1546 }
1547 tp->target_th = rb_thread_ptr(target_thread);
1548
1549 RUBY_ASSERT(tp->target_th->self == target_thread);
1550 RB_OBJ_WRITTEN(tpval, Qundef, target_thread);
1551 }
1552 else {
1553 tp->target_th = NULL;
1554 }
1555
1556 if (NIL_P(target)) {
1557 if (!NIL_P(target_line)) {
1558 rb_raise(rb_eArgError, "only target_line is specified");
1559 }
1560 rb_tracepoint_enable(tpval);
1561 }
1562 else {
1563 rb_tracepoint_enable_for_target(tpval, target, target_line);
1564 }
1565
1566 if (rb_block_given_p()) {
1567 return rb_ensure(rb_yield, Qundef,
1568 previous_tracing ? rb_tracepoint_enable : rb_tracepoint_disable,
1569 tpval);
1570 }
1571 else {
1572 return RBOOL(previous_tracing);
1573 }
1574}
1575
1576static VALUE
1577tracepoint_disable_m(rb_execution_context_t *ec, VALUE tpval)
1578{
1579 rb_tp_t *tp = tpptr(tpval);
1580 int previous_tracing = tp->tracing;
1581
1582 if (rb_block_given_p()) {
1583 if (tp->local_target_set != Qfalse) {
1584 rb_raise(rb_eArgError, "can't disable a targeting TracePoint in a block");
1585 }
1586
1587 rb_tracepoint_disable(tpval);
1588 return rb_ensure(rb_yield, Qundef,
1589 previous_tracing ? rb_tracepoint_enable : rb_tracepoint_disable,
1590 tpval);
1591 }
1592 else {
1593 rb_tracepoint_disable(tpval);
1594 return RBOOL(previous_tracing);
1595 }
1596}
1597
1598VALUE
1600{
1601 rb_tp_t *tp = tpptr(tpval);
1602 return RBOOL(tp->tracing);
1603}
1604
1605static VALUE
1606tracepoint_enabled_p(rb_execution_context_t *ec, VALUE tpval)
1607{
1608 return rb_tracepoint_enabled_p(tpval);
1609}
1610
1611static VALUE
1612tracepoint_new(VALUE klass, rb_thread_t *target_th, rb_event_flag_t events, void (func)(VALUE, void*), void *data, VALUE proc)
1613{
1614 VALUE tpval = tp_alloc(klass);
1615 rb_tp_t *tp;
1616 TypedData_Get_Struct(tpval, rb_tp_t, &tp_data_type, tp);
1617
1618 RB_OBJ_WRITE(tpval, &tp->proc, proc);
1619 tp->ractor = GET_RACTOR();
1620 tp->func = func; // for internal events
1621 tp->data = data;
1622 tp->events = events;
1623 tp->self = tpval;
1624
1625 return tpval;
1626}
1627
1628VALUE
1629rb_tracepoint_new(VALUE target_thval, rb_event_flag_t events, void (*func)(VALUE, void *), void *data)
1630{
1631 rb_thread_t *target_th = NULL;
1632
1633 if (RTEST(target_thval)) {
1634 target_th = rb_thread_ptr(target_thval);
1635 }
1636 return tracepoint_new(rb_cTracePoint, target_th, events, func, data, Qundef);
1637}
1638
1639static VALUE
1640tracepoint_new_s(rb_execution_context_t *ec, VALUE self, VALUE args)
1641{
1642 rb_event_flag_t events = 0;
1643 long i;
1644 long argc = RARRAY_LEN(args);
1645
1646 if (argc > 0) {
1647 for (i=0; i<argc; i++) {
1648 events |= symbol2event_flag(RARRAY_AREF(args, i));
1649 }
1650 }
1651 else {
1653 }
1654
1655 if (!rb_block_given_p()) {
1656 rb_raise(rb_eArgError, "must be called with a block");
1657 }
1658
1659 return tracepoint_new(self, 0, events, 0, 0, rb_block_proc());
1660}
1661
1662static VALUE
1663tracepoint_trace_s(rb_execution_context_t *ec, VALUE self, VALUE args)
1664{
1665 VALUE trace = tracepoint_new_s(ec, self, args);
1666 rb_tracepoint_enable(trace);
1667 return trace;
1668}
1669
1670static VALUE
1671tracepoint_inspect(rb_execution_context_t *ec, VALUE self)
1672{
1673 rb_tp_t *tp = tpptr(self);
1674 rb_trace_arg_t *trace_arg = GET_EC()->trace_arg;
1675
1676 if (trace_arg) {
1677 switch (trace_arg->event) {
1678 case RUBY_EVENT_LINE:
1679 {
1680 VALUE sym = rb_tracearg_method_id(trace_arg);
1681 if (NIL_P(sym))
1682 break;
1683 return rb_sprintf("#<TracePoint:%"PRIsVALUE" %"PRIsVALUE":%d in '%"PRIsVALUE"'>",
1684 rb_tracearg_event(trace_arg),
1685 rb_tracearg_path(trace_arg),
1686 FIX2INT(rb_tracearg_lineno(trace_arg)),
1687 sym);
1688 }
1689 case RUBY_EVENT_CALL:
1690 case RUBY_EVENT_C_CALL:
1691 case RUBY_EVENT_RETURN:
1693 return rb_sprintf("#<TracePoint:%"PRIsVALUE" '%"PRIsVALUE"' %"PRIsVALUE":%d>",
1694 rb_tracearg_event(trace_arg),
1695 rb_tracearg_method_id(trace_arg),
1696 rb_tracearg_path(trace_arg),
1697 FIX2INT(rb_tracearg_lineno(trace_arg)));
1700 return rb_sprintf("#<TracePoint:%"PRIsVALUE" %"PRIsVALUE">",
1701 rb_tracearg_event(trace_arg),
1702 rb_tracearg_self(trace_arg));
1703 default:
1704 break;
1705 }
1706 return rb_sprintf("#<TracePoint:%"PRIsVALUE" %"PRIsVALUE":%d>",
1707 rb_tracearg_event(trace_arg),
1708 rb_tracearg_path(trace_arg),
1709 FIX2INT(rb_tracearg_lineno(trace_arg)));
1710 }
1711 else {
1712 return rb_sprintf("#<TracePoint:%s>", tp->tracing ? "enabled" : "disabled");
1713 }
1714}
1715
1716static void
1717tracepoint_stat_event_hooks(VALUE hash, VALUE key, rb_event_hook_t *hook)
1718{
1719 int active = 0, deleted = 0;
1720
1721 while (hook) {
1722 if (hook->hook_flags & RUBY_EVENT_HOOK_FLAG_DELETED) {
1723 deleted++;
1724 }
1725 else {
1726 active++;
1727 }
1728 hook = hook->next;
1729 }
1730
1731 rb_hash_aset(hash, key, rb_ary_new3(2, INT2FIX(active), INT2FIX(deleted)));
1732}
1733
1734static VALUE
1735tracepoint_stat_s(rb_execution_context_t *ec, VALUE self)
1736{
1737 rb_vm_t *vm = GET_VM();
1738 VALUE stat = rb_hash_new();
1739
1740 tracepoint_stat_event_hooks(stat, vm->self, rb_ec_ractor_hooks(ec)->hooks);
1741
1742 return stat;
1743}
1744
1745static VALUE
1746disallow_reentry(VALUE val)
1747{
1748 rb_trace_arg_t *arg = (rb_trace_arg_t *)val;
1749 rb_execution_context_t *ec = GET_EC();
1750 if (ec->trace_arg != NULL) rb_bug("should be NULL, but %p", (void *)ec->trace_arg);
1751 ec->trace_arg = arg;
1752 return Qnil;
1753}
1754
1755static VALUE
1756tracepoint_allow_reentry(rb_execution_context_t *ec, VALUE self)
1757{
1758 const rb_trace_arg_t *arg = ec->trace_arg;
1759 if (arg == NULL) rb_raise(rb_eRuntimeError, "No need to allow reentrance.");
1760 ec->trace_arg = NULL;
1761 return rb_ensure(rb_yield, Qnil, disallow_reentry, (VALUE)arg);
1762}
1763
1764#include "trace_point.rbinc"
1765
1766/* This function is called from inits.c */
1767void
1768Init_vm_trace(void)
1769{
1770 sym_default = ID2SYM(rb_intern_const("default"));
1771
1772 /* trace_func */
1773 rb_define_global_function("set_trace_func", set_trace_func, 1);
1774 rb_define_method(rb_cThread, "set_trace_func", thread_set_trace_func_m, 1);
1775 rb_define_method(rb_cThread, "add_trace_func", thread_add_trace_func_m, 1);
1776
1777 rb_cTracePoint = rb_define_class("TracePoint", rb_cObject);
1778 rb_undef_alloc_func(rb_cTracePoint);
1779}
1780
1781/*
1782 * Ruby actually has two separate mechanisms for enqueueing work from contexts
1783 * where it is not safe to run Ruby code, to run later on when it is safe. One
1784 * is async-signal-safe but more limited, and accessed through the
1785 * `rb_postponed_job_preregister` and `rb_postponed_job_trigger` functions. The
1786 * other is more flexible but cannot be used in signal handlers, and is accessed
1787 * through the `rb_workqueue_register` function.
1788 *
1789 * The postponed job functions form part of Ruby's extension API, but the
1790 * workqueue functions are for internal use only.
1791 */
1792
1794 struct ccan_list_node jnode; /* <=> vm->workqueue */
1796 void *data;
1797};
1798
1799// Used for VM memsize reporting. Returns the size of a list of rb_workqueue_job
1800// structs. Defined here because the struct definition lives here as well.
1801size_t
1802rb_vm_memsize_workqueue(struct ccan_list_head *workqueue)
1803{
1804 struct rb_workqueue_job *work = 0;
1805 size_t size = 0;
1806
1807 ccan_list_for_each(workqueue, work, jnode) {
1808 size += sizeof(struct rb_workqueue_job);
1809 }
1810
1811 return size;
1812}
1813
1814/*
1815 * thread-safe and called from non-Ruby thread
1816 * returns FALSE on failure (ENOMEM), TRUE otherwise
1817 */
1818int
1819rb_workqueue_register(unsigned flags, rb_postponed_job_func_t func, void *data)
1820{
1821 struct rb_workqueue_job *wq_job = malloc(sizeof(*wq_job));
1822 rb_vm_t *vm = GET_VM();
1823
1824 if (!wq_job) return FALSE;
1825 wq_job->func = func;
1826 wq_job->data = data;
1827
1828 rb_nativethread_lock_lock(&vm->workqueue_lock);
1829 ccan_list_add_tail(&vm->workqueue, &wq_job->jnode);
1830 rb_nativethread_lock_unlock(&vm->workqueue_lock);
1831
1832 // TODO: current implementation affects only main ractor
1833 RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(rb_vm_main_ractor_ec(vm));
1834
1835 return TRUE;
1836}
1837
1838#define PJOB_TABLE_SIZE (sizeof(rb_atomic_t) * CHAR_BIT)
1839/* pre-registered jobs table, for async-safe jobs */
1841 struct {
1843 void *data;
1844 } table[PJOB_TABLE_SIZE];
1845 /* Bits in this are set when the corresponding entry in prereg_table has non-zero
1846 * triggered_count; i.e. somebody called rb_postponed_job_trigger */
1847 rb_atomic_t triggered_bitset;
1849
1850static rb_postponed_job_queues_t postponed_job_queue;
1851
1853get_valid_ec(rb_vm_t *vm)
1854{
1855 rb_execution_context_t *ec = rb_current_execution_context(false);
1856 if (ec == NULL) ec = rb_vm_main_ractor_ec(vm);
1857 return ec;
1858}
1859
1860void
1861rb_vm_postponed_job_atfork(void)
1862{
1863 rb_postponed_job_queues_t *pjq = &postponed_job_queue;
1864 /* make sure we set the interrupt flag on _this_ thread if we carried any pjobs over
1865 * from the other side of the fork */
1866 if (pjq->triggered_bitset) {
1867 RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(get_valid_ec(GET_VM()));
1868 }
1869
1870}
1871
1872// Used for VM memsize reporting. Returns the total size of the postponed job
1873// queue infrastructure.
1874size_t
1875rb_vm_memsize_postponed_job_queue(void)
1876{
1877 return sizeof(rb_postponed_job_queues_t);
1878}
1879
1880
1882rb_postponed_job_preregister(unsigned int flags, rb_postponed_job_func_t func, void *data)
1883{
1884 /* The doc comments say that this function should be called under the GVL, because
1885 * that is actually required to get the guarantee that "if a given (func, data) pair
1886 * was already pre-registered, this method will return the same handle instance".
1887 *
1888 * However, the actual implementation here is called without the GVL, from inside
1889 * rb_postponed_job_register, to support that legacy interface. In the presence
1890 * of concurrent calls to both _preregister and _register functions on the same
1891 * func, however, the data may get mixed up between them. */
1892
1893 rb_postponed_job_queues_t *pjq = &postponed_job_queue;
1894 for (unsigned int i = 0; i < PJOB_TABLE_SIZE; i++) {
1895 /* Try and set this slot to equal `func` */
1896 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);
1897 if (existing_func == NULL || existing_func == func) {
1898 /* Either this slot was NULL, and we set it to func, or, this slot was already equal to func.
1899 * In either case, clobber the data with our data. Note that concurrent calls to
1900 * rb_postponed_job_register with the same func & different data will result in either of the
1901 * datas being written */
1902 RUBY_ATOMIC_PTR_EXCHANGE(pjq->table[i].data, data);
1903 return (rb_postponed_job_handle_t)i;
1904 }
1905 else {
1906 /* Try the next slot if this one already has a func in it */
1907 continue;
1908 }
1909 }
1910
1911 /* full */
1912 return POSTPONED_JOB_HANDLE_INVALID;
1913}
1914
1915void
1917{
1918 rb_postponed_job_queues_t *pjq = &postponed_job_queue;
1919
1920 RUBY_ATOMIC_OR(pjq->triggered_bitset, (((rb_atomic_t)1UL) << h));
1921 RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(get_valid_ec(GET_VM()));
1922}
1923
1924
1925static int
1926pjob_register_legacy_impl(unsigned int flags, rb_postponed_job_func_t func, void *data)
1927{
1928 /* We _know_ calling preregister from a signal handler like this is racy; what is
1929 * and is not promised is very exhaustively documented in debug.h */
1931 if (h == POSTPONED_JOB_HANDLE_INVALID) {
1932 return 0;
1933 }
1935 return 1;
1936}
1937
1938int
1939rb_postponed_job_register(unsigned int flags, rb_postponed_job_func_t func, void *data)
1940{
1941 return pjob_register_legacy_impl(flags, func, data);
1942}
1943
1944int
1945rb_postponed_job_register_one(unsigned int flags, rb_postponed_job_func_t func, void *data)
1946{
1947 return pjob_register_legacy_impl(flags, func, data);
1948}
1949
1950
1951void
1952rb_postponed_job_flush(rb_vm_t *vm)
1953{
1954 rb_postponed_job_queues_t *pjq = &postponed_job_queue;
1955 rb_execution_context_t *ec = GET_EC();
1956 const rb_atomic_t block_mask = POSTPONED_JOB_INTERRUPT_MASK | TRAP_INTERRUPT_MASK;
1957 volatile rb_atomic_t saved_mask = ec->interrupt_mask & block_mask;
1958 VALUE volatile saved_errno = ec->errinfo;
1959 struct ccan_list_head tmp;
1960
1961 ccan_list_head_init(&tmp);
1962
1963 rb_nativethread_lock_lock(&vm->workqueue_lock);
1964 ccan_list_append_list(&tmp, &vm->workqueue);
1965 rb_nativethread_lock_unlock(&vm->workqueue_lock);
1966
1967 rb_atomic_t triggered_bits = RUBY_ATOMIC_EXCHANGE(pjq->triggered_bitset, 0);
1968
1969 ec->errinfo = Qnil;
1970 /* mask POSTPONED_JOB dispatch */
1971 ec->interrupt_mask |= block_mask;
1972 {
1973 EC_PUSH_TAG(ec);
1974 if (EC_EXEC_TAG() == TAG_NONE) {
1975 /* execute postponed jobs */
1976 while (triggered_bits) {
1977 unsigned int i = bit_length(triggered_bits) - 1;
1978 triggered_bits ^= ((1UL) << i); /* toggle ith bit off */
1979 rb_postponed_job_func_t func = pjq->table[i].func;
1980 void *data = pjq->table[i].data;
1981 (func)(data);
1982 }
1983
1984 /* execute workqueue jobs */
1985 struct rb_workqueue_job *wq_job;
1986 while ((wq_job = ccan_list_pop(&tmp, struct rb_workqueue_job, jnode))) {
1987 rb_postponed_job_func_t func = wq_job->func;
1988 void *data = wq_job->data;
1989
1990 free(wq_job);
1991 (func)(data);
1992 }
1993 }
1994 EC_POP_TAG();
1995 }
1996 /* restore POSTPONED_JOB mask */
1997 ec->interrupt_mask &= ~(saved_mask ^ block_mask);
1998 ec->errinfo = saved_errno;
1999
2000 /* If we threw an exception, there might be leftover workqueue items; carry them over
2001 * to a subsequent execution of flush */
2002 if (!ccan_list_empty(&tmp)) {
2003 rb_nativethread_lock_lock(&vm->workqueue_lock);
2004 ccan_list_prepend_list(&vm->workqueue, &tmp);
2005 rb_nativethread_lock_unlock(&vm->workqueue_lock);
2006
2007 RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(GET_EC());
2008 }
2009 /* likewise with any remaining-to-be-executed bits of the preregistered postponed
2010 * job table */
2011 if (triggered_bits) {
2012 RUBY_ATOMIC_OR(pjq->triggered_bitset, triggered_bits);
2013 RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(GET_EC());
2014 }
2015}
#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:1168
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:1916
VALUE rb_tracepoint_enabled_p(VALUE tpval)
Queries if the passed TracePoint is up and running.
Definition vm_trace.c:1599
VALUE rb_tracearg_object(rb_trace_arg_t *trace_arg)
Queries the allocated/deallocated object that the trace represents.
Definition vm_trace.c:1190
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:1629
VALUE rb_tracearg_raised_exception(rb_trace_arg_t *trace_arg)
Queries the raised exception that the trace represents.
Definition vm_trace.c:1139
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:1882
VALUE rb_tracepoint_disable(VALUE tpval)
Stops (disables) an already running instance of TracePoint.
Definition vm_trace.c:1455
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:1945
VALUE rb_tracearg_return_value(rb_trace_arg_t *trace_arg)
Queries the return value that the trace represents.
Definition vm_trace.c:1129
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:1149
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:1292
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:1939
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:1523
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition eval.c:1018
#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:1427
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1425
VALUE rb_cObject
Object class.
Definition object.c:61
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
Definition object.c:95
VALUE rb_cThread
Thread class.
Definition vm.c:682
#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:1120
Defines RBIMPL_HAS_BUILTIN.
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
Definition proc.c:988
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:1169
VALUE rb_obj_is_method(VALUE recv)
Queries if the given object is a method.
Definition proc.c:1802
VALUE rb_binding_new(void)
Snapshots the current execution context and turn it into an instance of rb_cBinding.
Definition proc.c:331
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
Definition proc.c:122
VALUE rb_thread_current(void)
Obtains the "current" thread.
Definition thread.c:3194
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
Definition vm_method.c:1731
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:1024
VALUE rb_yield(VALUE val)
Yields the block.
Definition vm_eval.c:1375
#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:81
#define RUBY_TYPED_FREE_IMMEDIATELY
Macros to see if each corresponding flag is defined.
Definition rtypeddata.h:122
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
Definition rtypeddata.h:769
#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:578
#define RTEST
This is an old name of RB_TEST.
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:229
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:308
void rb_nativethread_lock_unlock(rb_nativethread_lock_t *lock)
Releases a lock.
Definition thread.c:314
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