Ruby  2.5.0dev(2017-10-22revision60238)
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 "eval_intern.h"
29 
30 /* (1) trace mechanisms */
31 
32 typedef struct rb_event_hook_struct {
39 
41 
42 #define MAX_EVENT_NUM 32
43 
44 static int ruby_event_flag_count[MAX_EVENT_NUM] = {0};
45 
46 /* called from vm.c */
47 
48 void
50 {
51  rb_event_hook_t *hook = hooks->hooks;
52 
53  while (hook) {
54  rb_gc_mark(hook->data);
55  hook = hook->next;
56  }
57 }
58 
59 /* ruby_vm_event_flags management */
60 
61 static void
62 recalc_add_ruby_vm_event_flags(rb_event_flag_t events)
63 {
64  int i;
66 
67  for (i=0; i<MAX_EVENT_NUM; i++) {
68  if (events & ((rb_event_flag_t)1 << i)) {
69  ruby_event_flag_count[i]++;
70  }
71  ruby_vm_event_flags |= ruby_event_flag_count[i] ? (1<<i) : 0;
72  }
73 
75 }
76 
77 static void
78 recalc_remove_ruby_vm_event_flags(rb_event_flag_t events)
79 {
80  int i;
82 
83  for (i=0; i<MAX_EVENT_NUM; i++) {
84  if (events & ((rb_event_flag_t)1 << i)) {
85  ruby_event_flag_count[i]--;
86  }
87  ruby_vm_event_flags |= ruby_event_flag_count[i] ? (1<<i) : 0;
88  }
89 
91 }
92 
93 /* add/remove hooks */
94 
95 static rb_event_hook_t *
97 {
98  rb_event_hook_t *hook;
99 
100  if ((events & RUBY_INTERNAL_EVENT_MASK) && (events & ~RUBY_INTERNAL_EVENT_MASK)) {
101  rb_raise(rb_eTypeError, "Can not specify normal event and internal event simultaneously.");
102  }
103 
104  hook = ALLOC(rb_event_hook_t);
105  hook->hook_flags = hook_flags;
106  hook->events = events;
107  hook->func = func;
108  hook->data = data;
109  return hook;
110 }
111 
112 static void
113 connect_event_hook(rb_hook_list_t *list, rb_event_hook_t *hook)
114 {
115  hook->next = list->hooks;
116  list->hooks = hook;
117  recalc_add_ruby_vm_event_flags(hook->events);
118  list->events |= hook->events;
119 }
120 
121 static void
123 {
124  rb_event_hook_t *hook = alloc_event_hook(func, events, data, hook_flags);
125  connect_event_hook(&th->event_hooks, hook);
126 }
127 
128 void
130 {
131  rb_threadptr_add_event_hook(rb_thread_ptr(thval), func, events, data, RUBY_EVENT_HOOK_FLAG_SAFE);
132 }
133 
134 void
136 {
137  rb_event_hook_t *hook = alloc_event_hook(func, events, data, RUBY_EVENT_HOOK_FLAG_SAFE);
138  connect_event_hook(&GET_VM()->event_hooks, hook);
139 }
140 
141 void
143 {
144  rb_threadptr_add_event_hook(rb_thread_ptr(thval), func, events, data, hook_flags);
145 }
146 
147 void
149 {
150  rb_event_hook_t *hook = alloc_event_hook(func, events, data, hook_flags);
151  connect_event_hook(&GET_VM()->event_hooks, hook);
152 }
153 
154 /* if func is 0, then clear all funcs */
155 static int
156 remove_event_hook(rb_hook_list_t *list, rb_event_hook_func_t func, VALUE data)
157 {
158  int ret = 0;
159  rb_event_hook_t *hook = list->hooks;
160 
161  while (hook) {
162  if (func == 0 || hook->func == func) {
163  if (data == Qundef || hook->data == data) {
165  ret+=1;
166  list->need_clean = TRUE;
167  }
168  }
169  hook = hook->next;
170  }
171 
172  return ret;
173 }
174 
175 static int
176 rb_threadptr_remove_event_hook(rb_thread_t *th, rb_event_hook_func_t func, VALUE data)
177 {
178  return remove_event_hook(&th->event_hooks, func, data);
179 }
180 
181 int
183 {
184  return rb_threadptr_remove_event_hook(rb_thread_ptr(thval), func, Qundef);
185 }
186 
187 int
189 {
190  return rb_threadptr_remove_event_hook(rb_thread_ptr(thval), func, data);
191 }
192 
193 int
195 {
196  return remove_event_hook(&GET_VM()->event_hooks, func, Qundef);
197 }
198 
199 int
201 {
202  return remove_event_hook(&GET_VM()->event_hooks, func, data);
203 }
204 
205 void
207 {
208  rb_vm_t *vm = GET_VM();
209  rb_thread_t *th = 0;
210 
211  list_for_each(&vm->living_threads, th, vmlt_node) {
212  rb_threadptr_remove_event_hook(th, 0, Qundef);
213  }
215 }
216 
217 /* invoke hooks */
218 
219 static void
220 clean_hooks(rb_hook_list_t *list)
221 {
222  rb_event_hook_t *hook, **nextp = &list->hooks;
223 
224  list->events = 0;
225  list->need_clean = FALSE;
226 
227  while ((hook = *nextp) != 0) {
229  *nextp = hook->next;
230  recalc_remove_ruby_vm_event_flags(hook->events);
231  xfree(hook);
232  }
233  else {
234  list->events |= hook->events; /* update active events */
235  nextp = &hook->next;
236  }
237  }
238 }
239 
240 static void
241 exec_hooks_body(rb_thread_t *th, rb_hook_list_t *list, const rb_trace_arg_t *trace_arg)
242 {
243  rb_event_hook_t *hook;
244 
245  for (hook = list->hooks; hook; hook = hook->next) {
246  if (!(hook->hook_flags & RUBY_EVENT_HOOK_FLAG_DELETED) && (trace_arg->event & hook->events)) {
247  if (!(hook->hook_flags & RUBY_EVENT_HOOK_FLAG_RAW_ARG)) {
248  (*hook->func)(trace_arg->event, hook->data, trace_arg->self, trace_arg->id, trace_arg->klass);
249  }
250  else {
251  (*((rb_event_hook_raw_arg_func_t)hook->func))(hook->data, trace_arg);
252  }
253  }
254  }
255 }
256 
257 static int
258 exec_hooks_precheck(rb_thread_t *th, rb_hook_list_t *list, const rb_trace_arg_t *trace_arg)
259 {
260  if (UNLIKELY(list->need_clean != FALSE)) {
261  if (th->vm->trace_running <= 1) { /* only running this hooks */
262  clean_hooks(list);
263  }
264  }
265 
266  return (list->events & trace_arg->event) != 0;
267 }
268 
269 static void
270 exec_hooks_unprotected(rb_thread_t *th, rb_hook_list_t *list, const rb_trace_arg_t *trace_arg)
271 {
272  if (exec_hooks_precheck(th, list, trace_arg) == 0) return;
273  exec_hooks_body(th, list, trace_arg);
274 }
275 
276 static int
277 exec_hooks_protected(rb_thread_t *th, rb_hook_list_t *list, const rb_trace_arg_t *trace_arg)
278 {
279  enum ruby_tag_type state;
280  volatile int raised;
281 
282  if (exec_hooks_precheck(th, list, trace_arg) == 0) return 0;
283 
284  raised = rb_threadptr_reset_raised(th);
285 
286  /* TODO: Support !RUBY_EVENT_HOOK_FLAG_SAFE hooks */
287 
288  TH_PUSH_TAG(th);
289  if ((state = TH_EXEC_TAG()) == TAG_NONE) {
290  exec_hooks_body(th, list, trace_arg);
291  }
292  TH_POP_TAG();
293 
294  if (raised) {
296  }
297 
298  return state;
299 }
300 
301 static void
302 rb_threadptr_exec_event_hooks_orig(rb_trace_arg_t *trace_arg, int pop_p)
303 {
304  rb_thread_t *th = trace_arg->th;
305 
306  if (trace_arg->event & RUBY_INTERNAL_EVENT_MASK) {
307  if (th->ec.trace_arg && (th->ec.trace_arg->event & RUBY_INTERNAL_EVENT_MASK)) {
308  /* skip hooks because this thread doing INTERNAL_EVENT */
309  }
310  else {
311  rb_trace_arg_t *prev_trace_arg = th->ec.trace_arg;
312  th->vm->trace_running++;
313  th->ec.trace_arg = trace_arg;
314  exec_hooks_unprotected(th, &th->event_hooks, trace_arg);
315  exec_hooks_unprotected(th, &th->vm->event_hooks, trace_arg);
316  th->ec.trace_arg = prev_trace_arg;
317  th->vm->trace_running--;
318  }
319  }
320  else {
321  if (th->ec.trace_arg == NULL && /* check reentrant */
322  trace_arg->self != rb_mRubyVMFrozenCore /* skip special methods. TODO: remove it. */) {
323  const VALUE errinfo = th->ec.errinfo;
324  const VALUE old_recursive = th->ec.local_storage_recursive_hash;
325  int state = 0;
326 
328  th->ec.errinfo = Qnil;
329 
330  th->vm->trace_running++;
331  th->ec.trace_arg = trace_arg;
332  {
333  /* thread local traces */
334  state = exec_hooks_protected(th, &th->event_hooks, trace_arg);
335  if (state) goto terminate;
336 
337  /* vm global traces */
338  state = exec_hooks_protected(th, &th->vm->event_hooks, trace_arg);
339  if (state) goto terminate;
340 
341  th->ec.errinfo = errinfo;
342  }
343  terminate:
344  th->ec.trace_arg = NULL;
345  th->vm->trace_running--;
346 
348  th->ec.local_storage_recursive_hash = old_recursive;
349 
350  if (state) {
351  if (pop_p) {
352  if (VM_FRAME_FINISHED_P(th->ec.cfp)) {
353  th->ec.tag = th->ec.tag->prev;
354  }
355  rb_vm_pop_frame(th);
356  }
357  TH_JUMP_TAG(th, state);
358  }
359  }
360  }
361 }
362 
363 void
365 {
366  rb_threadptr_exec_event_hooks_orig(trace_arg, 1);
367 }
368 
369 void
371 {
372  rb_threadptr_exec_event_hooks_orig(trace_arg, 0);
373 }
374 
375 VALUE
377 {
378  volatile int raised;
379  VALUE result = Qnil;
380  rb_thread_t *volatile th = GET_THREAD();
381  enum ruby_tag_type state;
382  const int tracing = th->ec.trace_arg ? 1 : 0;
383  rb_trace_arg_t dummy_trace_arg;
384  dummy_trace_arg.event = 0;
385 
386  if (!tracing) th->vm->trace_running++;
387  if (!th->ec.trace_arg) th->ec.trace_arg = &dummy_trace_arg;
388 
389  raised = rb_threadptr_reset_raised(th);
390 
391  TH_PUSH_TAG(th);
392  if ((state = TH_EXEC_TAG()) == TAG_NONE) {
393  result = (*func)(arg);
394  }
395  TH_POP_TAG();
396 
397  if (raised) {
399  }
400 
401  if (th->ec.trace_arg == &dummy_trace_arg) th->ec.trace_arg = 0;
402  if (!tracing) th->vm->trace_running--;
403 
404  if (state) {
405 #if defined RUBY_USE_SETJMPEX && RUBY_USE_SETJMPEX
406  RB_GC_GUARD(result);
407 #endif
408  TH_JUMP_TAG(th, state);
409  }
410 
411  return result;
412 }
413 
414 static void call_trace_func(rb_event_flag_t, VALUE data, VALUE self, ID id, VALUE klass);
415 
416 /* (2-1) set_trace_func (old API) */
417 
418 /*
419  * call-seq:
420  * set_trace_func(proc) -> proc
421  * set_trace_func(nil) -> nil
422  *
423  * Establishes _proc_ as the handler for tracing, or disables
424  * tracing if the parameter is +nil+.
425  *
426  * *Note:* this method is obsolete, please use TracePoint instead.
427  *
428  * _proc_ takes up to six parameters:
429  *
430  * * an event name
431  * * a filename
432  * * a line number
433  * * an object id
434  * * a binding
435  * * the name of a class
436  *
437  * _proc_ is invoked whenever an event occurs.
438  *
439  * Events are:
440  *
441  * +c-call+:: call a C-language routine
442  * +c-return+:: return from a C-language routine
443  * +call+:: call a Ruby method
444  * +class+:: start a class or module definition
445  * +end+:: finish a class or module definition
446  * +line+:: execute code on a new line
447  * +raise+:: raise an exception
448  * +return+:: return from a Ruby method
449  *
450  * Tracing is disabled within the context of _proc_.
451  *
452  * class Test
453  * def test
454  * a = 1
455  * b = 2
456  * end
457  * end
458  *
459  * set_trace_func proc { |event, file, line, id, binding, classname|
460  * printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname
461  * }
462  * t = Test.new
463  * t.test
464  *
465  * line prog.rb:11 false
466  * c-call prog.rb:11 new Class
467  * c-call prog.rb:11 initialize Object
468  * c-return prog.rb:11 initialize Object
469  * c-return prog.rb:11 new Class
470  * line prog.rb:12 false
471  * call prog.rb:2 test Test
472  * line prog.rb:3 test Test
473  * line prog.rb:4 test Test
474  * return prog.rb:4 test Test
475  */
476 
477 static VALUE
478 set_trace_func(VALUE obj, VALUE trace)
479 {
480 
481  rb_remove_event_hook(call_trace_func);
482 
483  if (NIL_P(trace)) {
484  return Qnil;
485  }
486 
487  if (!rb_obj_is_proc(trace)) {
488  rb_raise(rb_eTypeError, "trace_func needs to be Proc");
489  }
490 
491  rb_add_event_hook(call_trace_func, RUBY_EVENT_ALL, trace);
492  return trace;
493 }
494 
495 static void
496 thread_add_trace_func(rb_thread_t *th, VALUE trace)
497 {
498  if (!rb_obj_is_proc(trace)) {
499  rb_raise(rb_eTypeError, "trace_func needs to be Proc");
500  }
501 
502  rb_threadptr_add_event_hook(th, call_trace_func, RUBY_EVENT_ALL, trace, RUBY_EVENT_HOOK_FLAG_SAFE);
503 }
504 
505 /*
506  * call-seq:
507  * thr.add_trace_func(proc) -> proc
508  *
509  * Adds _proc_ as a handler for tracing.
510  *
511  * See Thread#set_trace_func and Kernel#set_trace_func.
512  */
513 
514 static VALUE
515 thread_add_trace_func_m(VALUE obj, VALUE trace)
516 {
517  thread_add_trace_func(rb_thread_ptr(obj), trace);
518  return trace;
519 }
520 
521 /*
522  * call-seq:
523  * thr.set_trace_func(proc) -> proc
524  * thr.set_trace_func(nil) -> nil
525  *
526  * Establishes _proc_ on _thr_ as the handler for tracing, or
527  * disables tracing if the parameter is +nil+.
528  *
529  * See Kernel#set_trace_func.
530  */
531 
532 static VALUE
533 thread_set_trace_func_m(VALUE target_thread, VALUE trace)
534 {
535  rb_thread_t *target_th = rb_thread_ptr(target_thread);
536 
537  rb_threadptr_remove_event_hook(target_th, call_trace_func, Qundef);
538 
539  if (NIL_P(trace)) {
540  return Qnil;
541  }
542  else {
543  thread_add_trace_func(target_th, trace);
544  return trace;
545  }
546 }
547 
548 static const char *
549 get_event_name(rb_event_flag_t event)
550 {
551  switch (event) {
552  case RUBY_EVENT_LINE: return "line";
553  case RUBY_EVENT_CLASS: return "class";
554  case RUBY_EVENT_END: return "end";
555  case RUBY_EVENT_CALL: return "call";
556  case RUBY_EVENT_RETURN: return "return";
557  case RUBY_EVENT_C_CALL: return "c-call";
558  case RUBY_EVENT_C_RETURN: return "c-return";
559  case RUBY_EVENT_RAISE: return "raise";
560  default:
561  return "unknown";
562  }
563 }
564 
565 static ID
566 get_event_id(rb_event_flag_t event)
567 {
568  ID id;
569 
570  switch (event) {
571 #define C(name, NAME) case RUBY_EVENT_##NAME: CONST_ID(id, #name); return id;
572  C(line, LINE);
573  C(class, CLASS);
574  C(end, END);
575  C(call, CALL);
576  C(return, RETURN);
577  C(c_call, C_CALL);
578  C(c_return, C_RETURN);
579  C(raise, RAISE);
580  C(b_call, B_CALL);
581  C(b_return, B_RETURN);
582  C(thread_begin, THREAD_BEGIN);
583  C(thread_end, THREAD_END);
584  C(fiber_switch, FIBER_SWITCH);
585  C(specified_line, SPECIFIED_LINE);
586  case RUBY_EVENT_LINE | RUBY_EVENT_SPECIFIED_LINE: CONST_ID(id, "line"); return id;
587 #undef C
588  default:
589  return 0;
590  }
591 }
592 
593 static void
594 call_trace_func(rb_event_flag_t event, VALUE proc, VALUE self, ID id, VALUE klass)
595 {
596  int line;
597  const char *srcfile = rb_source_loc(&line);
598  VALUE eventname = rb_str_new2(get_event_name(event));
599  VALUE filename = srcfile ? rb_str_new2(srcfile) : Qnil;
600  VALUE argv[6];
601  rb_thread_t *th = GET_THREAD();
602 
603  if (!klass) {
604  rb_thread_method_id_and_class(th, &id, 0, &klass);
605  }
606 
607  if (klass) {
608  if (RB_TYPE_P(klass, T_ICLASS)) {
609  klass = RBASIC(klass)->klass;
610  }
611  else if (FL_TEST(klass, FL_SINGLETON)) {
612  klass = rb_ivar_get(klass, id__attached__);
613  }
614  }
615 
616  argv[0] = eventname;
617  argv[1] = filename;
618  argv[2] = INT2FIX(line);
619  argv[3] = id ? ID2SYM(id) : Qnil;
620  argv[4] = (self && srcfile) ? rb_binding_new() : Qnil;
621  argv[5] = klass ? klass : Qnil;
622 
623  rb_proc_call_with_block(proc, 6, argv, Qnil);
624 }
625 
626 /* (2-2) TracePoint API */
627 
628 static VALUE rb_cTracePoint;
629 
630 typedef struct rb_tp_struct {
632  int tracing; /* bool */
634  void (*func)(VALUE tpval, void *data);
635  void *data;
637  VALUE self;
638 } rb_tp_t;
639 
640 static void
641 tp_mark(void *ptr)
642 {
643  rb_tp_t *tp = ptr;
644  rb_gc_mark(tp->proc);
645  if (tp->target_th) rb_gc_mark(tp->target_th->self);
646 }
647 
648 static size_t
649 tp_memsize(const void *ptr)
650 {
651  return sizeof(rb_tp_t);
652 }
653 
654 static const rb_data_type_t tp_data_type = {
655  "tracepoint",
656  {tp_mark, RUBY_TYPED_NEVER_FREE, tp_memsize,},
658 };
659 
660 static VALUE
661 tp_alloc(VALUE klass)
662 {
663  rb_tp_t *tp;
664  return TypedData_Make_Struct(klass, rb_tp_t, &tp_data_type, tp);
665 }
666 
667 static rb_event_flag_t
668 symbol2event_flag(VALUE v)
669 {
670  ID id;
671  VALUE sym = rb_convert_type_with_id(v, T_SYMBOL, "Symbol", idTo_sym);
672  const rb_event_flag_t RUBY_EVENT_A_CALL =
674  const rb_event_flag_t RUBY_EVENT_A_RETURN =
676 
677 #define C(name, NAME) CONST_ID(id, #name); if (sym == ID2SYM(id)) return RUBY_EVENT_##NAME
678  C(line, LINE);
679  C(class, CLASS);
680  C(end, END);
681  C(call, CALL);
682  C(return, RETURN);
683  C(c_call, C_CALL);
684  C(c_return, C_RETURN);
685  C(raise, RAISE);
686  C(b_call, B_CALL);
687  C(b_return, B_RETURN);
688  C(thread_begin, THREAD_BEGIN);
689  C(thread_end, THREAD_END);
690  C(fiber_switch, FIBER_SWITCH);
691  C(specified_line, SPECIFIED_LINE);
692  C(a_call, A_CALL);
693  C(a_return, A_RETURN);
694 #undef C
695  rb_raise(rb_eArgError, "unknown event: %"PRIsVALUE, rb_sym2str(sym));
696 }
697 
698 static rb_tp_t *
699 tpptr(VALUE tpval)
700 {
701  rb_tp_t *tp;
702  TypedData_Get_Struct(tpval, rb_tp_t, &tp_data_type, tp);
703  return tp;
704 }
705 
706 static rb_trace_arg_t *
707 get_trace_arg(void)
708 {
709  rb_trace_arg_t *trace_arg = GET_THREAD()->ec.trace_arg;
710  if (trace_arg == 0) {
711  rb_raise(rb_eRuntimeError, "access from outside");
712  }
713  return trace_arg;
714 }
715 
716 struct rb_trace_arg_struct *
718 {
719  return get_trace_arg();
720 }
721 
724 {
725  return trace_arg->event;
726 }
727 
728 VALUE
730 {
731  return ID2SYM(get_event_id(trace_arg->event));
732 }
733 
734 static void
735 fill_path_and_lineno(rb_trace_arg_t *trace_arg)
736 {
737  if (trace_arg->path == Qundef) {
738  rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(trace_arg->th, trace_arg->cfp);
739 
740  if (cfp) {
741  trace_arg->path = rb_iseq_path(cfp->iseq);
742  trace_arg->lineno = rb_vm_get_sourceline(cfp);
743  }
744  else {
745  trace_arg->path = Qnil;
746  trace_arg->lineno = 0;
747  }
748  }
749 }
750 
751 VALUE
753 {
754  fill_path_and_lineno(trace_arg);
755  return INT2FIX(trace_arg->lineno);
756 }
757 VALUE
759 {
760  fill_path_and_lineno(trace_arg);
761  return trace_arg->path;
762 }
763 
764 static void
765 fill_id_and_klass(rb_trace_arg_t *trace_arg)
766 {
767  if (!trace_arg->klass_solved) {
768  if (!trace_arg->klass) {
769  rb_vm_control_frame_id_and_class(trace_arg->cfp, &trace_arg->id, &trace_arg->called_id, &trace_arg->klass);
770  }
771 
772  if (trace_arg->klass) {
773  if (RB_TYPE_P(trace_arg->klass, T_ICLASS)) {
774  trace_arg->klass = RBASIC(trace_arg->klass)->klass;
775  }
776  }
777  else {
778  trace_arg->klass = Qnil;
779  }
780 
781  trace_arg->klass_solved = 1;
782  }
783 }
784 
785 VALUE
787 {
788  fill_id_and_klass(trace_arg);
789  return trace_arg->id ? ID2SYM(trace_arg->id) : Qnil;
790 }
791 
792 VALUE
794 {
795  fill_id_and_klass(trace_arg);
796  return trace_arg->called_id ? ID2SYM(trace_arg->called_id) : Qnil;
797 }
798 
799 VALUE
801 {
802  fill_id_and_klass(trace_arg);
803  return trace_arg->klass;
804 }
805 
806 VALUE
808 {
810  cfp = rb_vm_get_binding_creatable_next_cfp(trace_arg->th, trace_arg->cfp);
811 
812  if (cfp) {
813  return rb_vm_make_binding(trace_arg->th, cfp);
814  }
815  else {
816  return Qnil;
817  }
818 }
819 
820 VALUE
822 {
823  return trace_arg->self;
824 }
825 
826 VALUE
828 {
830  /* ok */
831  }
832  else {
833  rb_raise(rb_eRuntimeError, "not supported by this event");
834  }
835  if (trace_arg->data == Qundef) {
836  rb_bug("tp_attr_return_value_m: unreachable");
837  }
838  return trace_arg->data;
839 }
840 
841 VALUE
843 {
844  if (trace_arg->event & (RUBY_EVENT_RAISE)) {
845  /* ok */
846  }
847  else {
848  rb_raise(rb_eRuntimeError, "not supported by this event");
849  }
850  if (trace_arg->data == Qundef) {
851  rb_bug("tp_attr_raised_exception_m: unreachable");
852  }
853  return trace_arg->data;
854 }
855 
856 VALUE
858 {
860  /* ok */
861  }
862  else {
863  rb_raise(rb_eRuntimeError, "not supported by this event");
864  }
865  if (trace_arg->data == Qundef) {
866  rb_bug("tp_attr_raised_exception_m: unreachable");
867  }
868  return trace_arg->data;
869 }
870 
871 /*
872  * Type of event
873  *
874  * See TracePoint@Events for more information.
875  */
876 static VALUE
877 tracepoint_attr_event(VALUE tpval)
878 {
879  return rb_tracearg_event(get_trace_arg());
880 }
881 
882 /*
883  * Line number of the event
884  */
885 static VALUE
886 tracepoint_attr_lineno(VALUE tpval)
887 {
888  return rb_tracearg_lineno(get_trace_arg());
889 }
890 
891 /*
892  * Path of the file being run
893  */
894 static VALUE
895 tracepoint_attr_path(VALUE tpval)
896 {
897  return rb_tracearg_path(get_trace_arg());
898 }
899 
900 /*
901  * Return the name at the definition of the method being called
902  */
903 static VALUE
904 tracepoint_attr_method_id(VALUE tpval)
905 {
906  return rb_tracearg_method_id(get_trace_arg());
907 }
908 
909 /*
910  * Return the called name of the method being called
911  */
912 static VALUE
913 tracepoint_attr_callee_id(VALUE tpval)
914 {
915  return rb_tracearg_callee_id(get_trace_arg());
916 }
917 
918 /*
919  * Return class or module of the method being called.
920  *
921  * class C; def foo; end; end
922  * trace = TracePoint.new(:call) do |tp|
923  * p tp.defined_class #=> C
924  * end.enable do
925  * C.new.foo
926  * end
927  *
928  * If method is defined by a module, then that module is returned.
929  *
930  * module M; def foo; end; end
931  * class C; include M; end;
932  * trace = TracePoint.new(:call) do |tp|
933  * p tp.defined_class #=> M
934  * end.enable do
935  * C.new.foo
936  * end
937  *
938  * <b>Note:</b> #defined_class returns singleton class.
939  *
940  * 6th block parameter of Kernel#set_trace_func passes original class
941  * of attached by singleton class.
942  *
943  * <b>This is a difference between Kernel#set_trace_func and TracePoint.</b>
944  *
945  * class C; def self.foo; end; end
946  * trace = TracePoint.new(:call) do |tp|
947  * p tp.defined_class #=> #<Class:C>
948  * end.enable do
949  * C.foo
950  * end
951  */
952 static VALUE
953 tracepoint_attr_defined_class(VALUE tpval)
954 {
955  return rb_tracearg_defined_class(get_trace_arg());
956 }
957 
958 /*
959  * Return the generated binding object from event
960  */
961 static VALUE
962 tracepoint_attr_binding(VALUE tpval)
963 {
964  return rb_tracearg_binding(get_trace_arg());
965 }
966 
967 /*
968  * Return the trace object during event
969  *
970  * Same as TracePoint#binding:
971  * trace.binding.eval('self')
972  */
973 static VALUE
974 tracepoint_attr_self(VALUE tpval)
975 {
976  return rb_tracearg_self(get_trace_arg());
977 }
978 
979 /*
980  * Return value from +:return+, +c_return+, and +b_return+ event
981  */
982 static VALUE
983 tracepoint_attr_return_value(VALUE tpval)
984 {
985  return rb_tracearg_return_value(get_trace_arg());
986 }
987 
988 /*
989  * Value from exception raised on the +:raise+ event
990  */
991 static VALUE
992 tracepoint_attr_raised_exception(VALUE tpval)
993 {
994  return rb_tracearg_raised_exception(get_trace_arg());
995 }
996 
997 static void
998 tp_call_trace(VALUE tpval, rb_trace_arg_t *trace_arg)
999 {
1000  rb_tp_t *tp = tpptr(tpval);
1001 
1002  if (tp->func) {
1003  (*tp->func)(tpval, tp->data);
1004  }
1005  else {
1006  rb_proc_call_with_block((VALUE)tp->proc, 1, &tpval, Qnil);
1007  }
1008 }
1009 
1010 VALUE
1012 {
1013  rb_tp_t *tp;
1014 
1015  tp = tpptr(tpval);
1016 
1017  if (tp->target_th) {
1018  rb_thread_add_event_hook2(tp->target_th->self, (rb_event_hook_func_t)tp_call_trace, tp->events, tpval,
1020  }
1021  else {
1022  rb_add_event_hook2((rb_event_hook_func_t)tp_call_trace, tp->events, tpval,
1024  }
1025  tp->tracing = 1;
1026  return Qundef;
1027 }
1028 
1029 VALUE
1031 {
1032  rb_tp_t *tp;
1033 
1034  tp = tpptr(tpval);
1035 
1036  if (tp->target_th) {
1038  }
1039  else {
1041  }
1042  tp->tracing = 0;
1043  return Qundef;
1044 }
1045 
1046 /*
1047  * call-seq:
1048  * trace.enable -> true or false
1049  * trace.enable { block } -> obj
1050  *
1051  * Activates the trace
1052  *
1053  * Return true if trace was enabled.
1054  * Return false if trace was disabled.
1055  *
1056  * trace.enabled? #=> false
1057  * trace.enable #=> false (previous state)
1058  * # trace is enabled
1059  * trace.enabled? #=> true
1060  * trace.enable #=> true (previous state)
1061  * # trace is still enabled
1062  *
1063  * If a block is given, the trace will only be enabled within the scope of the
1064  * block.
1065  *
1066  * trace.enabled?
1067  * #=> false
1068  *
1069  * trace.enable do
1070  * trace.enabled?
1071  * # only enabled for this block
1072  * end
1073  *
1074  * trace.enabled?
1075  * #=> false
1076  *
1077  * Note: You cannot access event hooks within the block.
1078  *
1079  * trace.enable { p tp.lineno }
1080  * #=> RuntimeError: access from outside
1081  *
1082  */
1083 static VALUE
1084 tracepoint_enable_m(VALUE tpval)
1085 {
1086  rb_tp_t *tp = tpptr(tpval);
1087  int previous_tracing = tp->tracing;
1088  rb_tracepoint_enable(tpval);
1089 
1090  if (rb_block_given_p()) {
1091  return rb_ensure(rb_yield, Qnil,
1092  previous_tracing ? rb_tracepoint_enable : rb_tracepoint_disable,
1093  tpval);
1094  }
1095  else {
1096  return previous_tracing ? Qtrue : Qfalse;
1097  }
1098 }
1099 
1100 /*
1101  * call-seq:
1102  * trace.disable -> true or false
1103  * trace.disable { block } -> obj
1104  *
1105  * Deactivates the trace
1106  *
1107  * Return true if trace was enabled.
1108  * Return false if trace was disabled.
1109  *
1110  * trace.enabled? #=> true
1111  * trace.disable #=> false (previous status)
1112  * trace.enabled? #=> false
1113  * trace.disable #=> false
1114  *
1115  * If a block is given, the trace will only be disable within the scope of the
1116  * block.
1117  *
1118  * trace.enabled?
1119  * #=> true
1120  *
1121  * trace.disable do
1122  * trace.enabled?
1123  * # only disabled for this block
1124  * end
1125  *
1126  * trace.enabled?
1127  * #=> true
1128  *
1129  * Note: You cannot access event hooks within the block.
1130  *
1131  * trace.disable { p tp.lineno }
1132  * #=> RuntimeError: access from outside
1133  */
1134 static VALUE
1135 tracepoint_disable_m(VALUE tpval)
1136 {
1137  rb_tp_t *tp = tpptr(tpval);
1138  int previous_tracing = tp->tracing;
1139  rb_tracepoint_disable(tpval);
1140 
1141  if (rb_block_given_p()) {
1142  return rb_ensure(rb_yield, Qnil,
1143  previous_tracing ? rb_tracepoint_enable : rb_tracepoint_disable,
1144  tpval);
1145  }
1146  else {
1147  return previous_tracing ? Qtrue : Qfalse;
1148  }
1149 }
1150 
1151 /*
1152  * call-seq:
1153  * trace.enabled? -> true or false
1154  *
1155  * The current status of the trace
1156  */
1157 VALUE
1159 {
1160  rb_tp_t *tp = tpptr(tpval);
1161  return tp->tracing ? Qtrue : Qfalse;
1162 }
1163 
1164 static VALUE
1165 tracepoint_new(VALUE klass, rb_thread_t *target_th, rb_event_flag_t events, void (func)(VALUE, void*), void *data, VALUE proc)
1166 {
1167  VALUE tpval = tp_alloc(klass);
1168  rb_tp_t *tp;
1169  TypedData_Get_Struct(tpval, rb_tp_t, &tp_data_type, tp);
1170 
1171  tp->proc = proc;
1172  tp->func = func;
1173  tp->data = data;
1174  tp->events = events;
1175  tp->self = tpval;
1176 
1177  return tpval;
1178 }
1179 
1180 /*
1181  * Creates a tracepoint by registering a callback function for one or more
1182  * tracepoint events. Once the tracepoint is created, you can use
1183  * rb_tracepoint_enable to enable the tracepoint.
1184  *
1185  * Parameters:
1186  * 1. VALUE target_thval - Meant for picking the thread in which the tracepoint
1187  * is to be created. However, current implementation ignore this parameter,
1188  * tracepoint is created for all threads. Simply specify Qnil.
1189  * 2. rb_event_flag_t events - Event(s) to listen to.
1190  * 3. void (*func)(VALUE, void *) - A callback function.
1191  * 4. void *data - Void pointer that will be passed to the callback function.
1192  *
1193  * When the callback function is called, it will be passed 2 parameters:
1194  * 1)VALUE tpval - the TracePoint object from which trace args can be extracted.
1195  * 2)void *data - A void pointer which helps to share scope with the callback function.
1196  *
1197  * It is important to note that you cannot register callbacks for normal events and internal events
1198  * simultaneously because they are different purpose.
1199  * You can use any Ruby APIs (calling methods and so on) on normal event hooks.
1200  * However, in internal events, you can not use any Ruby APIs (even object creations).
1201  * This is why we can't specify internal events by TracePoint directly.
1202  * Limitations are MRI version specific.
1203  *
1204  * Example:
1205  * rb_tracepoint_new(Qnil, RUBY_INTERNAL_EVENT_NEWOBJ | RUBY_INTERNAL_EVENT_FREEOBJ, obj_event_i, data);
1206  *
1207  * In this example, a callback function obj_event_i will be registered for
1208  * internal events RUBY_INTERNAL_EVENT_NEWOBJ and RUBY_INTERNAL_EVENT_FREEOBJ.
1209  */
1210 VALUE
1211 rb_tracepoint_new(VALUE target_thval, rb_event_flag_t events, void (*func)(VALUE, void *), void *data)
1212 {
1213  rb_thread_t *target_th = NULL;
1214 
1215  if (RTEST(target_thval)) {
1216  target_th = rb_thread_ptr(target_thval);
1217  /* TODO: Test it!
1218  * Warning: This function is not tested.
1219  */
1220  }
1221  return tracepoint_new(rb_cTracePoint, target_th, events, func, data, Qundef);
1222 }
1223 
1224 /*
1225  * call-seq:
1226  * TracePoint.new(*events) { |obj| block } -> obj
1227  *
1228  * Returns a new TracePoint object, not enabled by default.
1229  *
1230  * Next, in order to activate the trace, you must use TracePoint.enable
1231  *
1232  * trace = TracePoint.new(:call) do |tp|
1233  * p [tp.lineno, tp.defined_class, tp.method_id, tp.event]
1234  * end
1235  * #=> #<TracePoint:disabled>
1236  *
1237  * trace.enable
1238  * #=> false
1239  *
1240  * puts "Hello, TracePoint!"
1241  * # ...
1242  * # [48, IRB::Notifier::AbstractNotifier, :printf, :call]
1243  * # ...
1244  *
1245  * When you want to deactivate the trace, you must use TracePoint.disable
1246  *
1247  * trace.disable
1248  *
1249  * See TracePoint@Events for possible events and more information.
1250  *
1251  * A block must be given, otherwise a ThreadError is raised.
1252  *
1253  * If the trace method isn't included in the given events filter, a
1254  * RuntimeError is raised.
1255  *
1256  * TracePoint.trace(:line) do |tp|
1257  * p tp.raised_exception
1258  * end
1259  * #=> RuntimeError: 'raised_exception' not supported by this event
1260  *
1261  * If the trace method is called outside block, a RuntimeError is raised.
1262  *
1263  * TracePoint.trace(:line) do |tp|
1264  * $tp = tp
1265  * end
1266  * $tp.line #=> access from outside (RuntimeError)
1267  *
1268  * Access from other threads is also forbidden.
1269  *
1270  */
1271 static VALUE
1272 tracepoint_new_s(int argc, VALUE *argv, VALUE self)
1273 {
1274  rb_event_flag_t events = 0;
1275  int i;
1276 
1277  if (argc > 0) {
1278  for (i=0; i<argc; i++) {
1279  events |= symbol2event_flag(argv[i]);
1280  }
1281  }
1282  else {
1283  events = RUBY_EVENT_TRACEPOINT_ALL;
1284  }
1285 
1286  if (!rb_block_given_p()) {
1287  rb_raise(rb_eThreadError, "must be called with a block");
1288  }
1289 
1290  return tracepoint_new(self, 0, events, 0, 0, rb_block_proc());
1291 }
1292 
1293 static VALUE
1294 tracepoint_trace_s(int argc, VALUE *argv, VALUE self)
1295 {
1296  VALUE trace = tracepoint_new_s(argc, argv, self);
1297  rb_tracepoint_enable(trace);
1298  return trace;
1299 }
1300 
1301 /*
1302  * call-seq:
1303  * trace.inspect -> string
1304  *
1305  * Return a string containing a human-readable TracePoint
1306  * status.
1307  */
1308 
1309 static VALUE
1310 tracepoint_inspect(VALUE self)
1311 {
1312  rb_tp_t *tp = tpptr(self);
1313  rb_trace_arg_t *trace_arg = GET_THREAD()->ec.trace_arg;
1314 
1315  if (trace_arg) {
1316  switch (trace_arg->event) {
1317  case RUBY_EVENT_LINE:
1319  {
1320  VALUE sym = rb_tracearg_method_id(trace_arg);
1321  if (NIL_P(sym))
1322  goto default_inspect;
1323  return rb_sprintf("#<TracePoint:%"PRIsVALUE"@%"PRIsVALUE":%d in `%"PRIsVALUE"'>",
1324  rb_tracearg_event(trace_arg),
1325  rb_tracearg_path(trace_arg),
1326  FIX2INT(rb_tracearg_lineno(trace_arg)),
1327  sym);
1328  }
1329  case RUBY_EVENT_CALL:
1330  case RUBY_EVENT_C_CALL:
1331  case RUBY_EVENT_RETURN:
1332  case RUBY_EVENT_C_RETURN:
1333  return rb_sprintf("#<TracePoint:%"PRIsVALUE" `%"PRIsVALUE"'@%"PRIsVALUE":%d>",
1334  rb_tracearg_event(trace_arg),
1335  rb_tracearg_method_id(trace_arg),
1336  rb_tracearg_path(trace_arg),
1337  FIX2INT(rb_tracearg_lineno(trace_arg)));
1339  case RUBY_EVENT_THREAD_END:
1340  return rb_sprintf("#<TracePoint:%"PRIsVALUE" %"PRIsVALUE">",
1341  rb_tracearg_event(trace_arg),
1342  rb_tracearg_self(trace_arg));
1343  default:
1345  return rb_sprintf("#<TracePoint:%"PRIsVALUE"@%"PRIsVALUE":%d>",
1346  rb_tracearg_event(trace_arg),
1347  rb_tracearg_path(trace_arg),
1348  FIX2INT(rb_tracearg_lineno(trace_arg)));
1349  }
1350  }
1351  else {
1352  return rb_sprintf("#<TracePoint:%s>", tp->tracing ? "enabled" : "disabled");
1353  }
1354 }
1355 
1356 static void
1357 tracepoint_stat_event_hooks(VALUE hash, VALUE key, rb_event_hook_t *hook)
1358 {
1359  int active = 0, deleted = 0;
1360 
1361  while (hook) {
1363  deleted++;
1364  }
1365  else {
1366  active++;
1367  }
1368  hook = hook->next;
1369  }
1370 
1371  rb_hash_aset(hash, key, rb_ary_new3(2, INT2FIX(active), INT2FIX(deleted)));
1372 }
1373 
1374 /*
1375  * call-seq:
1376  * TracePoint.stat -> obj
1377  *
1378  * Returns internal information of TracePoint.
1379  *
1380  * The contents of the returned value are implementation specific.
1381  * It may be changed in future.
1382  *
1383  * This method is only for debugging TracePoint itself.
1384  */
1385 
1386 static VALUE
1387 tracepoint_stat_s(VALUE self)
1388 {
1389  rb_vm_t *vm = GET_VM();
1390  VALUE stat = rb_hash_new();
1391 
1392  tracepoint_stat_event_hooks(stat, vm->self, vm->event_hooks.hooks);
1393  /* TODO: thread local hooks */
1394 
1395  return stat;
1396 }
1397 
1398 static void Init_postponed_job(void);
1399 
1400 /* This function is called from inits.c */
1401 void
1403 {
1404  /* trace_func */
1405  rb_define_global_function("set_trace_func", set_trace_func, 1);
1406  rb_define_method(rb_cThread, "set_trace_func", thread_set_trace_func_m, 1);
1407  rb_define_method(rb_cThread, "add_trace_func", thread_add_trace_func_m, 1);
1408 
1409  /*
1410  * Document-class: TracePoint
1411  *
1412  * A class that provides the functionality of Kernel#set_trace_func in a
1413  * nice Object-Oriented API.
1414  *
1415  * == Example
1416  *
1417  * We can use TracePoint to gather information specifically for exceptions:
1418  *
1419  * trace = TracePoint.new(:raise) do |tp|
1420  * p [tp.lineno, tp.event, tp.raised_exception]
1421  * end
1422  * #=> #<TracePoint:disabled>
1423  *
1424  * trace.enable
1425  * #=> false
1426  *
1427  * 0 / 0
1428  * #=> [5, :raise, #<ZeroDivisionError: divided by 0>]
1429  *
1430  * == Events
1431  *
1432  * If you don't specify the type of events you want to listen for,
1433  * TracePoint will include all available events.
1434  *
1435  * *Note* do not depend on current event set, as this list is subject to
1436  * change. Instead, it is recommended you specify the type of events you
1437  * want to use.
1438  *
1439  * To filter what is traced, you can pass any of the following as +events+:
1440  *
1441  * +:line+:: execute code on a new line
1442  * +:class+:: start a class or module definition
1443  * +:end+:: finish a class or module definition
1444  * +:call+:: call a Ruby method
1445  * +:return+:: return from a Ruby method
1446  * +:c_call+:: call a C-language routine
1447  * +:c_return+:: return from a C-language routine
1448  * +:raise+:: raise an exception
1449  * +:b_call+:: event hook at block entry
1450  * +:b_return+:: event hook at block ending
1451  * +:thread_begin+:: event hook at thread beginning
1452  * +:thread_end+:: event hook at thread ending
1453  * +:fiber_switch+:: event hook at fiber switch
1454  *
1455  */
1456  rb_cTracePoint = rb_define_class("TracePoint", rb_cObject);
1457  rb_undef_alloc_func(rb_cTracePoint);
1458  rb_define_singleton_method(rb_cTracePoint, "new", tracepoint_new_s, -1);
1459  /*
1460  * Document-method: trace
1461  *
1462  * call-seq:
1463  * TracePoint.trace(*events) { |obj| block } -> obj
1464  *
1465  * A convenience method for TracePoint.new, that activates the trace
1466  * automatically.
1467  *
1468  * trace = TracePoint.trace(:call) { |tp| [tp.lineno, tp.event] }
1469  * #=> #<TracePoint:enabled>
1470  *
1471  * trace.enabled? #=> true
1472  */
1473  rb_define_singleton_method(rb_cTracePoint, "trace", tracepoint_trace_s, -1);
1474 
1475  rb_define_method(rb_cTracePoint, "enable", tracepoint_enable_m, 0);
1476  rb_define_method(rb_cTracePoint, "disable", tracepoint_disable_m, 0);
1477  rb_define_method(rb_cTracePoint, "enabled?", rb_tracepoint_enabled_p, 0);
1478 
1479  rb_define_method(rb_cTracePoint, "inspect", tracepoint_inspect, 0);
1480 
1481  rb_define_method(rb_cTracePoint, "event", tracepoint_attr_event, 0);
1482  rb_define_method(rb_cTracePoint, "lineno", tracepoint_attr_lineno, 0);
1483  rb_define_method(rb_cTracePoint, "path", tracepoint_attr_path, 0);
1484  rb_define_method(rb_cTracePoint, "method_id", tracepoint_attr_method_id, 0);
1485  rb_define_method(rb_cTracePoint, "callee_id", tracepoint_attr_callee_id, 0);
1486  rb_define_method(rb_cTracePoint, "defined_class", tracepoint_attr_defined_class, 0);
1487  rb_define_method(rb_cTracePoint, "binding", tracepoint_attr_binding, 0);
1488  rb_define_method(rb_cTracePoint, "self", tracepoint_attr_self, 0);
1489  rb_define_method(rb_cTracePoint, "return_value", tracepoint_attr_return_value, 0);
1490  rb_define_method(rb_cTracePoint, "raised_exception", tracepoint_attr_raised_exception, 0);
1491 
1492  rb_define_singleton_method(rb_cTracePoint, "stat", tracepoint_stat_s, 0);
1493 
1494  /* initialized for postponed job */
1495 
1496  Init_postponed_job();
1497 }
1498 
1499 typedef struct rb_postponed_job_struct {
1500  unsigned long flags; /* reserved */
1501  struct rb_thread_struct *th; /* created thread, reserved */
1503  void *data;
1505 
1506 #define MAX_POSTPONED_JOB 1000
1507 #define MAX_POSTPONED_JOB_SPECIAL_ADDITION 24
1508 
1509 static void
1510 Init_postponed_job(void)
1511 {
1512  rb_vm_t *vm = GET_VM();
1514  vm->postponed_job_index = 0;
1515 }
1516 
1521 };
1522 
1524 postponed_job_register(rb_thread_t *th, rb_vm_t *vm,
1525  unsigned int flags, rb_postponed_job_func_t func, void *data, int max, int expected_index)
1526 {
1527  rb_postponed_job_t *pjob;
1528 
1529  if (expected_index >= max) return PJRR_FULL; /* failed */
1530 
1531  if (ATOMIC_CAS(vm->postponed_job_index, expected_index, expected_index+1) == expected_index) {
1532  pjob = &vm->postponed_job_buffer[expected_index];
1533  }
1534  else {
1535  return PJRR_INTERRUPTED;
1536  }
1537 
1538  pjob->flags = flags;
1539  pjob->th = th;
1540  pjob->func = func;
1541  pjob->data = data;
1542 
1544 
1545  return PJRR_SUCESS;
1546 }
1547 
1548 
1549 /* return 0 if job buffer is full */
1550 int
1552 {
1553  rb_thread_t *th = GET_THREAD();
1554  rb_vm_t *vm = th->vm;
1555 
1556  begin:
1557  switch (postponed_job_register(th, vm, flags, func, data, MAX_POSTPONED_JOB, vm->postponed_job_index)) {
1558  case PJRR_SUCESS : return 1;
1559  case PJRR_FULL : return 0;
1560  case PJRR_INTERRUPTED: goto begin;
1561  default: rb_bug("unreachable\n");
1562  }
1563 }
1564 
1565 /* return 0 if job buffer is full */
1566 int
1568 {
1569  rb_thread_t *th = GET_THREAD();
1570  rb_vm_t *vm = th->vm;
1571  rb_postponed_job_t *pjob;
1572  int i, index;
1573 
1574  begin:
1575  index = vm->postponed_job_index;
1576  for (i=0; i<index; i++) {
1577  pjob = &vm->postponed_job_buffer[i];
1578  if (pjob->func == func) {
1580  return 2;
1581  }
1582  }
1583  switch (postponed_job_register(th, vm, flags, func, data, MAX_POSTPONED_JOB + MAX_POSTPONED_JOB_SPECIAL_ADDITION, index)) {
1584  case PJRR_SUCESS : return 1;
1585  case PJRR_FULL : return 0;
1586  case PJRR_INTERRUPTED: goto begin;
1587  default: rb_bug("unreachable\n");
1588  }
1589 }
1590 
1591 void
1593 {
1594  rb_thread_t *th = GET_THREAD();
1595  const unsigned long block_mask = POSTPONED_JOB_INTERRUPT_MASK|TRAP_INTERRUPT_MASK;
1596  unsigned long saved_mask = th->interrupt_mask & block_mask;
1597  VALUE saved_errno = th->ec.errinfo;
1598 
1599  th->ec.errinfo = Qnil;
1600  /* mask POSTPONED_JOB dispatch */
1601  th->interrupt_mask |= block_mask;
1602  {
1603  TH_PUSH_TAG(th);
1604  if (EXEC_TAG() == TAG_NONE) {
1605  int index;
1606  while ((index = vm->postponed_job_index) > 0) {
1607  if (ATOMIC_CAS(vm->postponed_job_index, index, index-1) == index) {
1608  rb_postponed_job_t *pjob = &vm->postponed_job_buffer[index-1];
1609  (*pjob->func)(pjob->data);
1610  }
1611  }
1612  }
1613  TH_POP_TAG();
1614  }
1615  /* restore POSTPONED_JOB mask */
1616  th->interrupt_mask &= ~(saved_mask ^ block_mask);
1617  th->ec.errinfo = saved_errno;
1618 }
#define RUBY_EVENT_B_RETURN
Definition: ruby.h:2092
VALUE self
Definition: vm_trace.c:637
#define T_SYMBOL
Definition: ruby.h:508
rb_control_frame_t * rb_vm_get_ruby_level_next_cfp(const rb_thread_t *th, const rb_control_frame_t *cfp)
Definition: vm.c:498
#define RUBY_EVENT_THREAD_END
Definition: ruby.h:2094
rb_vm_t * vm
Definition: vm_core.h:788
VALUE rb_tracearg_lineno(rb_trace_arg_t *trace_arg)
Definition: vm_trace.c:752
void rb_bug(const char *fmt,...)
Definition: error.c:521
#define RUBY_EVENT_C_RETURN
Definition: ruby.h:2086
VALUE rb_proc_call_with_block(VALUE, int argc, const VALUE *argv, VALUE)
Definition: proc.c:892
#define FALSE
Definition: nkf.h:174
ruby_tag_type
Definition: vm_core.h:151
#define RUBY_TYPED_FREE_IMMEDIATELY
Definition: ruby.h:1138
int rb_vm_get_sourceline(const rb_control_frame_t *cfp)
Definition: vm_backtrace.c:38
#define RUBY_EVENT_RETURN
Definition: ruby.h:2084
struct rb_trace_arg_struct * trace_arg
Definition: vm_core.h:764
rb_control_frame_t * cfp
Definition: vm_core.h:744
void rb_undef_alloc_func(VALUE)
Definition: vm_method.c:675
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1716
#define TAG_NONE
Definition: vm_core.h:164
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition: eval.c:835
VALUE local_storage_recursive_hash_for_trace
Definition: vm_core.h:757
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2284
#define RUBY_EVENT_RAISE
Definition: ruby.h:2087
#define Qtrue
Definition: ruby.h:437
VALUE rb_tracearg_object(rb_trace_arg_t *trace_arg)
Definition: vm_trace.c:857
#define RUBY_EVENT_ALL
Definition: ruby.h:2088
#define TypedData_Get_Struct(obj, type, data_type, sval)
Definition: ruby.h:1183
const int id
Definition: nkf.c:209
VALUE rb_tracearg_return_value(rb_trace_arg_t *trace_arg)
Definition: vm_trace.c:827
rb_thread_t * th
Definition: vm_core.h:1639
#define RUBY_EVENT_CALL
Definition: ruby.h:2083
#define TH_JUMP_TAG(th, st)
Definition: eval_intern.h:204
void rb_threadptr_exec_event_hooks(rb_trace_arg_t *trace_arg)
Definition: vm_trace.c:370
VALUE rb_ivar_get(VALUE, ID)
Definition: variable.c:1210
unsigned long flags
Definition: vm_trace.c:1500
rb_thread_t * target_th
Definition: vm_trace.c:633
#define TH_EXEC_TAG()
Definition: eval_intern.h:198
#define RB_GC_GUARD(v)
Definition: ruby.h:552
VALUE rb_tracearg_path(rb_trace_arg_t *trace_arg)
Definition: vm_trace.c:758
VALUE rb_tracearg_method_id(rb_trace_arg_t *trace_arg)
Definition: vm_trace.c:786
void rb_gc_mark(VALUE ptr)
Definition: gc.c:4464
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:142
void rb_clear_trace_func(void)
Definition: vm_trace.c:206
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
Definition: class.c:1745
VALUE rb_ensure(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*e_proc)(ANYARGS), VALUE data2)
An equivalent to ensure clause.
Definition: eval.c:1035
rb_event_flag_t events
Definition: vm_core.h:510
VALUE rb_tracearg_event(rb_trace_arg_t *trace_arg)
Definition: vm_trace.c:729
const char * rb_source_loc(int *pline)
Definition: vm.c:1313
int rb_remove_event_hook(rb_event_hook_func_t func)
Definition: vm_trace.c:194
#define FL_TEST(x, f)
Definition: ruby.h:1282
VALUE rb_tracearg_callee_id(rb_trace_arg_t *trace_arg)
Definition: vm_trace.c:793
struct rb_tp_struct rb_tp_t
void rb_threadptr_exec_event_hooks_and_pop_frame(rb_trace_arg_t *trace_arg)
Definition: vm_trace.c:364
#define GET_THREAD()
Definition: vm_core.h:1583
VALUE rb_eArgError
Definition: error.c:802
#define sym(x)
Definition: date_core.c:3721
void Init_vm_trace(void)
Definition: vm_trace.c:1402
#define RUBY_EVENT_CLASS
Definition: ruby.h:2081
void rb_objspace_set_event_hook(const rb_event_flag_t event)
Definition: gc.c:1811
#define FL_SINGLETON
Definition: ruby.h:1208
struct rb_trace_arg_struct * rb_tracearg_from_tracepoint(VALUE tpval)
Definition: vm_trace.c:717
#define RB_TYPE_P(obj, type)
Definition: ruby.h:527
VALUE rb_binding_new(void)
Definition: proc.c:333
#define TH_POP_TAG()
Definition: eval_intern.h:138
struct rb_thread_struct * th
Definition: vm_trace.c:1501
VALUE rb_tracearg_self(rb_trace_arg_t *trace_arg)
Definition: vm_trace.c:821
#define UNLIKELY(x)
Definition: internal.h:43
int rb_vm_control_frame_id_and_class(const rb_control_frame_t *cfp, ID *idp, ID *called_idp, VALUE *klassp)
Definition: vm.c:2048
const rb_iseq_t * iseq
Definition: vm_core.h:665
VALUE rb_tracepoint_disable(VALUE tpval)
Definition: vm_trace.c:1030
VALUE default_inspect(VALUE self, const char *class_name)
Definition: win32ole.c:1344
#define ALLOC_N(type, n)
Definition: ruby.h:1587
int trace_running
Definition: vm_core.h:531
#define EXEC_TAG()
Definition: eval_intern.h:201
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Definition: hash.c:1616
int rb_threadptr_set_raised(rb_thread_t *th)
Definition: thread.c:2173
VALUE rb_convert_type_with_id(VALUE, int, const char *, ID)
Definition: object.c:2979
struct rb_event_hook_struct * next
Definition: vm_trace.c:37
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1893
VALUE rb_tracearg_raised_exception(rb_trace_arg_t *trace_arg)
Definition: vm_trace.c:842
VALUE rb_tracepoint_enabled_p(VALUE tpval)
Definition: vm_trace.c:1158
#define NIL_P(v)
Definition: ruby.h:451
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:646
rb_control_frame_t * cfp
Definition: vm_core.h:1640
VALUE rb_tracearg_defined_class(rb_trace_arg_t *trace_arg)
Definition: vm_trace.c:800
int postponed_job_index
Definition: vm_core.h:563
int argc
Definition: ruby.c:187
VALUE proc
Definition: vm_trace.c:636
void(* rb_event_hook_func_t)(rb_event_flag_t evflag, VALUE data, VALUE self, ID mid, VALUE klass)
Definition: ruby.h:2117
#define Qfalse
Definition: ruby.h:436
rb_event_flag_t rb_tracearg_event_flag(rb_trace_arg_t *trace_arg)
Definition: vm_trace.c:723
VALUE rb_tracearg_binding(rb_trace_arg_t *trace_arg)
Definition: vm_trace.c:807
#define rb_str_new2
Definition: intern.h:835
#define RUBY_EVENT_C_CALL
Definition: ruby.h:2085
struct rb_event_hook_struct * hooks
Definition: vm_core.h:509
#define ATOMIC_CAS(var, oldval, newval)
Definition: ruby_atomic.h:132
#define ALLOC(type)
Definition: ruby.h:1588
#define END(no)
Definition: re.c:25
#define RETURN(val)
Definition: dir.c:293
rb_hook_list_t event_hooks
Definition: vm_core.h:849
void rb_vm_pop_frame(rb_thread_t *th)
rb_event_hook_func_t func
Definition: vm_trace.c:35
#define RUBY_EVENT_SPECIFIED_LINE
Definition: ruby.h:2099
VALUE rb_yield(VALUE)
Definition: vm_eval.c:973
#define TRUE
Definition: nkf.h:175
VALUE rb_obj_is_proc(VALUE)
Definition: proc.c:116
int rb_thread_remove_event_hook_with_data(VALUE thval, rb_event_hook_func_t func, VALUE data)
Definition: vm_trace.c:188
#define C(name, NAME)
void * data
Definition: vm_trace.c:635
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1452
VALUE rb_hash_new(void)
Definition: hash.c:424
#define RUBY_EVENT_LINE
Definition: ruby.h:2080
#define PRIsVALUE
Definition: ruby.h:135
unsigned long ID
Definition: ruby.h:86
int rb_thread_remove_event_hook(VALUE thval, rb_event_hook_func_t func)
Definition: vm_trace.c:182
VALUE rb_vm_make_binding(rb_thread_t *th, const rb_control_frame_t *src_cfp)
Definition: vm.c:895
#define Qnil
Definition: ruby.h:438
unsigned long VALUE
Definition: ruby.h:85
RUBY_EXTERN VALUE rb_cThread
Definition: ruby.h:1930
#define RBASIC(obj)
Definition: ruby.h:1197
rb_event_hook_flag_t
Definition: debug.h:92
VALUE rb_eTypeError
Definition: error.c:801
int rb_threadptr_reset_raised(rb_thread_t *th)
Definition: thread.c:2183
#define FIX2INT(x)
Definition: ruby.h:686
#define RUBY_EVENT_THREAD_BEGIN
Definition: ruby.h:2093
#define rb_ary_new3
Definition: intern.h:91
#define TH_PUSH_TAG(th)
Definition: eval_intern.h:131
#define RUBY_EVENT_TRACEPOINT_ALL
Definition: ruby.h:2096
rb_event_hook_flag_t hook_flags
Definition: vm_trace.c:33
#define RUBY_INTERNAL_EVENT_MASK
Definition: ruby.h:2114
#define MAX_POSTPONED_JOB
Definition: vm_trace.c:1506
int rb_postponed_job_register_one(unsigned int flags, rb_postponed_job_func_t func, void *data)
Definition: vm_trace.c:1567
VALUE rb_mRubyVMFrozenCore
Definition: vm.c:316
int rb_postponed_job_register(unsigned int flags, rb_postponed_job_func_t func, void *data)
Definition: vm_trace.c:1551
struct rb_event_hook_struct rb_event_hook_t
#define MAX_EVENT_NUM
Definition: vm_trace.c:42
#define INT2FIX(i)
Definition: ruby.h:232
#define RUBY_TYPED_NEVER_FREE
Definition: ruby.h:1135
unsigned long interrupt_mask
Definition: vm_core.h:833
VALUE rb_block_proc(void)
Definition: proc.c:780
#define RUBY_INTERNAL_EVENT_NEWOBJ
Definition: ruby.h:2106
#define RUBY_INTERNAL_EVENT_FREEOBJ
Definition: ruby.h:2107
VALUE rb_tracepoint_enable(VALUE tpval)
Definition: vm_trace.c:1011
VALUE rb_eRuntimeError
Definition: error.c:800
rb_hook_list_t event_hooks
Definition: vm_core.h:556
struct rb_vm_tag * tag
Definition: vm_core.h:746
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:129
#define RTEST(v)
Definition: ruby.h:450
VALUE rb_suppress_tracing(VALUE(*func)(VALUE), VALUE arg)
Definition: vm_trace.c:376
rb_event_flag_t ruby_vm_event_flags
Definition: vm.c:322
struct rb_encoding_entry * list
Definition: encoding.c:55
rb_postponed_job_func_t func
Definition: vm_trace.c:1502
#define RUBY_EVENT_END
Definition: ruby.h:2082
#define TypedData_Make_Struct(klass, type, data_type, sval)
Definition: ruby.h:1175
VALUE rb_iseq_path(const rb_iseq_t *iseq)
Definition: iseq.c:692
void rb_vm_trace_mark_event_hooks(rb_hook_list_t *hooks)
Definition: vm_trace.c:49
VALUE rb_tracepoint_new(VALUE target_thval, rb_event_flag_t events, void(*func)(VALUE, void *), void *data)
Definition: vm_trace.c:1211
struct rb_postponed_job_struct rb_postponed_job_t
VALUE self
Definition: vm_core.h:515
rb_execution_context_t ec
Definition: vm_core.h:790
void rb_postponed_job_flush(rb_vm_t *vm)
Definition: vm_trace.c:1592
#define ID2SYM(x)
Definition: ruby.h:383
rb_event_flag_t event
Definition: vm_core.h:1638
postponed_job_register_result
Definition: vm_trace.c:1517
#define MAX_POSTPONED_JOB_SPECIAL_ADDITION
Definition: vm_trace.c:1507
uint32_t rb_event_flag_t
Definition: ruby.h:2116
struct rb_vm_tag * prev
Definition: vm_core.h:700
struct list_head living_threads
Definition: vm_core.h:524
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:148
#define CONST_ID(var, str)
Definition: ruby.h:1743
struct rb_postponed_job_struct * postponed_job_buffer
Definition: vm_core.h:562
int rb_thread_method_id_and_class(rb_thread_t *th, ID *idp, ID *called_idp, VALUE *klassp)
Definition: vm.c:2064
void void xfree(void *)
void(* rb_postponed_job_func_t)(void *arg)
Definition: debug.h:86
#define RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(th)
Definition: vm_core.h:1607
int rb_remove_event_hook_with_data(rb_event_hook_func_t func, VALUE data)
Definition: vm_trace.c:200
#define stat(path, st)
Definition: win32.h:183
#define NULL
Definition: _sdbm.c:102
#define Qundef
Definition: ruby.h:439
#define T_ICLASS
Definition: ruby.h:493
#define CALL(n)
Definition: inits.c:14
void(* rb_event_hook_raw_arg_func_t)(VALUE data, const rb_trace_arg_t *arg)
Definition: vm_trace.c:40
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1515
rb_event_flag_t events
Definition: vm_trace.c:34
VALUE rb_eThreadError
Definition: eval.c:857
rb_event_flag_t events
Definition: vm_trace.c:631
rb_control_frame_t * rb_vm_get_binding_creatable_next_cfp(const rb_thread_t *th, const rb_control_frame_t *cfp)
Definition: vm.c:486
#define RUBY_EVENT_B_CALL
Definition: ruby.h:2091
void(* func)(VALUE tpval, void *data)
Definition: vm_trace.c:634
char ** argv
Definition: ruby.c:188
void rb_add_event_hook(rb_event_hook_func_t func, rb_event_flag_t events, VALUE data)
Definition: vm_trace.c:135
#define rb_sym2str(sym)
Definition: console.c:107
#define GET_VM()
Definition: vm_core.h:1582