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