Ruby 2.7.0p0(2019-12-25revision647ee6f091eafcce70ffb75ddf7e121e192ab217)

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