Ruby  2.0.0p195(2013-05-14revision40734)
vm_backtrace.c
Go to the documentation of this file.
1 /**********************************************************************
2 
3  vm_backtrace.c -
4 
5  $Author: ko1 $
6  created at: Sun Jun 03 00:14:20 2012
7 
8  Copyright (C) 1993-2012 Yukihiro Matsumoto
9 
10 **********************************************************************/
11 
12 #include "ruby/ruby.h"
13 #include "ruby/encoding.h"
14 #include "ruby/debug.h"
15 
16 #include "internal.h"
17 #include "vm_core.h"
18 #include "eval_intern.h"
19 #include "iseq.h"
20 
23 
24 extern VALUE ruby_engine_name;
25 
26 inline static int
27 calc_lineno(const rb_iseq_t *iseq, const VALUE *pc)
28 {
29  return rb_iseq_line_no(iseq, pc - iseq->iseq_encoded);
30 }
31 
32 int
34 {
35  int lineno = 0;
36  const rb_iseq_t *iseq = cfp->iseq;
37 
38  if (RUBY_VM_NORMAL_ISEQ_P(iseq)) {
39  lineno = calc_lineno(cfp->iseq, cfp->pc);
40  }
41  return lineno;
42 }
43 
50  } type;
51 
52  union {
53  struct {
54  const rb_iseq_t *iseq;
55  union {
56  const VALUE *pc;
57  int lineno;
58  } lineno;
59  } iseq;
60  struct {
63  } cfunc;
64  } body;
66 
70 };
71 
72 static void
73 location_mark(void *ptr)
74 {
75  if (ptr) {
76  struct valued_frame_info *vfi = (struct valued_frame_info *)ptr;
77  rb_gc_mark(vfi->btobj);
78  }
79 }
80 
81 static void
83 {
84  switch (fi->type) {
85  case LOCATION_TYPE_ISEQ:
86  case LOCATION_TYPE_ISEQ_CALCED:
87  rb_gc_mark(fi->body.iseq.iseq->self);
88  break;
89  case LOCATION_TYPE_CFUNC:
90  case LOCATION_TYPE_IFUNC:
91  default:
92  break;
93  }
94 }
95 
96 static void
97 location_free(void *ptr)
98 {
99  if (ptr) {
101  ruby_xfree(fi);
102  }
103 }
104 
105 static size_t
106 location_memsize(const void *ptr)
107 {
108  /* rb_backtrace_location_t *fi = (rb_backtrace_location_t *)ptr; */
109  return sizeof(rb_backtrace_location_t);
110 }
111 
113  "frame_info",
115 };
116 
117 static inline rb_backtrace_location_t *
119 {
120  struct valued_frame_info *vloc;
121  GetCoreDataFromValue(locobj, struct valued_frame_info, vloc);
122  return vloc->loc;
123 }
124 
125 static int
127 {
128  switch (loc->type) {
129  case LOCATION_TYPE_ISEQ:
130  loc->type = LOCATION_TYPE_ISEQ_CALCED;
131  return (loc->body.iseq.lineno.lineno = calc_lineno(loc->body.iseq.iseq, loc->body.iseq.lineno.pc));
132  case LOCATION_TYPE_ISEQ_CALCED:
133  return loc->body.iseq.lineno.lineno;
134  case LOCATION_TYPE_CFUNC:
135  if (loc->body.cfunc.prev_loc) {
136  return location_lineno(loc->body.cfunc.prev_loc);
137  }
138  return 0;
139  default:
140  rb_bug("location_lineno: unreachable");
141  UNREACHABLE;
142  }
143 }
144 
145 /*
146  * Returns the line number of this frame.
147  *
148  * For example, using +caller_locations.rb+ from Thread::Backtrace::Location
149  *
150  * loc = c(0..1).first
151  * loc.lineno #=> 2
152  */
153 static VALUE
155 {
156  return INT2FIX(location_lineno(location_ptr(self)));
157 }
158 
159 static VALUE
161 {
162  switch (loc->type) {
163  case LOCATION_TYPE_ISEQ:
164  case LOCATION_TYPE_ISEQ_CALCED:
165  return loc->body.iseq.iseq->location.label;
166  case LOCATION_TYPE_CFUNC:
167  return rb_id2str(loc->body.cfunc.mid);
168  case LOCATION_TYPE_IFUNC:
169  default:
170  rb_bug("location_label: unreachable");
171  UNREACHABLE;
172  }
173 }
174 
175 /*
176  * Returns the label of this frame.
177  *
178  * Usually consists of method, class, module, etc names with decoration.
179  *
180  * For example, using +caller_locations.rb+ from Thread::Backtrace::Location
181  *
182  * loc = c(0..1).first
183  * loc.label #=> a
184  */
185 static VALUE
187 {
188  return location_label(location_ptr(self));
189 }
190 
191 static VALUE
193 {
194  switch (loc->type) {
195  case LOCATION_TYPE_ISEQ:
196  case LOCATION_TYPE_ISEQ_CALCED:
197  return loc->body.iseq.iseq->location.base_label;
198  case LOCATION_TYPE_CFUNC:
199  return rb_sym_to_s(ID2SYM(loc->body.cfunc.mid));
200  case LOCATION_TYPE_IFUNC:
201  default:
202  rb_bug("location_base_label: unreachable");
203  UNREACHABLE;
204  }
205 }
206 
207 /*
208  * Returns the base label of this frame.
209  *
210  * Usually same as #label, without decoration.
211  */
212 static VALUE
214 {
215  return location_base_label(location_ptr(self));
216 }
217 
218 static VALUE
220 {
221  switch (loc->type) {
222  case LOCATION_TYPE_ISEQ:
223  case LOCATION_TYPE_ISEQ_CALCED:
224  return loc->body.iseq.iseq->location.path;
225  case LOCATION_TYPE_CFUNC:
226  if (loc->body.cfunc.prev_loc) {
227  return location_path(loc->body.cfunc.prev_loc);
228  }
229  return Qnil;
230  case LOCATION_TYPE_IFUNC:
231  default:
232  rb_bug("location_path: unreachable");
233  UNREACHABLE;
234  }
235 }
236 
237 /*
238  * Returns the file name of this frame.
239  *
240  * For example, using +caller_locations.rb+ from Thread::Backtrace::Location
241  *
242  * loc = c(0..1).first
243  * loc.path #=> caller_locations.rb
244  */
245 static VALUE
247 {
248  return location_path(location_ptr(self));
249 }
250 
251 static VALUE
253 {
254  switch (loc->type) {
255  case LOCATION_TYPE_ISEQ:
256  case LOCATION_TYPE_ISEQ_CALCED:
257  return loc->body.iseq.iseq->location.absolute_path;
258  case LOCATION_TYPE_CFUNC:
259  if (loc->body.cfunc.prev_loc) {
260  return location_absolute_path(loc->body.cfunc.prev_loc);
261  }
262  return Qnil;
263  case LOCATION_TYPE_IFUNC:
264  default:
265  rb_bug("location_absolute_path: unreachable");
266  UNREACHABLE;
267  }
268 }
269 
270 /*
271  * Returns the full file path of this frame.
272  *
273  * Same as #path, but includes the absolute path.
274  */
275 static VALUE
277 {
278  return location_absolute_path(location_ptr(self));
279 }
280 
281 static VALUE
282 location_format(VALUE file, int lineno, VALUE name)
283 {
284  if (lineno != 0) {
285  return rb_enc_sprintf(rb_enc_compatible(file, name), "%s:%d:in `%s'",
286  RSTRING_PTR(file), lineno, RSTRING_PTR(name));
287  }
288  else {
289  return rb_enc_sprintf(rb_enc_compatible(file, name), "%s:in `%s'",
290  RSTRING_PTR(file), RSTRING_PTR(name));
291  }
292 }
293 
294 static VALUE
296 {
297  VALUE file, name;
298  int lineno;
299 
300  switch (loc->type) {
301  case LOCATION_TYPE_ISEQ:
302  file = loc->body.iseq.iseq->location.path;
303  name = loc->body.iseq.iseq->location.label;
304 
305  lineno = loc->body.iseq.lineno.lineno = calc_lineno(loc->body.iseq.iseq, loc->body.iseq.lineno.pc);
306  loc->type = LOCATION_TYPE_ISEQ_CALCED;
307  break;
308  case LOCATION_TYPE_ISEQ_CALCED:
309  file = loc->body.iseq.iseq->location.path;
310  lineno = loc->body.iseq.lineno.lineno;
311  name = loc->body.iseq.iseq->location.label;
312  break;
313  case LOCATION_TYPE_CFUNC:
314  if (loc->body.cfunc.prev_loc) {
315  file = loc->body.cfunc.prev_loc->body.iseq.iseq->location.path;
316  lineno = location_lineno(loc->body.cfunc.prev_loc);
317  }
318  else {
319  rb_thread_t *th = GET_THREAD();
320  file = th->vm->progname ? th->vm->progname : ruby_engine_name;
321  lineno = INT2FIX(0);
322  }
323  name = rb_id2str(loc->body.cfunc.mid);
324  break;
325  case LOCATION_TYPE_IFUNC:
326  default:
327  rb_bug("location_to_str: unreachable");
328  }
329 
330  return location_format(file, lineno, name);
331 }
332 
333 /*
334  * Returns a Kernel#caller style string representing this frame.
335  */
336 static VALUE
338 {
339  return location_to_str(location_ptr(self));
340 }
341 
342 /*
343  * Returns the same as calling +inspect+ on the string representation of
344  * #to_str
345  */
346 static VALUE
348 {
350 }
351 
352 typedef struct rb_backtrace_struct {
358 
359 static void
360 backtrace_mark(void *ptr)
361 {
362  if (ptr) {
363  rb_backtrace_t *bt = (rb_backtrace_t *)ptr;
364  size_t i, s = bt->backtrace_size;
365 
366  for (i=0; i<s; i++) {
368  rb_gc_mark(bt->strary);
369  }
370  }
371 }
372 
373 static void
374 backtrace_free(void *ptr)
375 {
376  if (ptr) {
377  rb_backtrace_t *bt = (rb_backtrace_t *)ptr;
378  if (bt->backtrace) ruby_xfree(bt->backtrace_base);
379  ruby_xfree(bt);
380  }
381 }
382 
383 static size_t
384 backtrace_memsize(const void *ptr)
385 {
386  rb_backtrace_t *bt = (rb_backtrace_t *)ptr;
387  return sizeof(rb_backtrace_t) + sizeof(rb_backtrace_location_t) * bt->backtrace_size;
388 }
389 
391  "backtrace",
393 };
394 
395 int
397 {
398  return rb_typeddata_is_kind_of(obj, &backtrace_data_type);
399 }
400 
401 static VALUE
403 {
404  rb_backtrace_t *bt;
405  VALUE obj = TypedData_Make_Struct(klass, rb_backtrace_t, &backtrace_data_type, bt);
406  return obj;
407 }
408 
409 static void
411  void (*init)(void *arg, size_t size),
412  void (*iter_iseq)(void *arg, const rb_control_frame_t *cfp),
413  void (*iter_cfunc)(void *arg, const rb_control_frame_t *cfp, ID mid),
414  void *arg)
415 {
416  rb_control_frame_t *last_cfp = th->cfp;
418  rb_control_frame_t *cfp;
419  ptrdiff_t size, i;
420 
421  /* <- start_cfp (end control frame)
422  * top frame (dummy)
423  * top frame (dummy)
424  * top frame <- start_cfp
425  * top frame
426  * ...
427  * 2nd frame <- lev:0
428  * current frame <- th->cfp
429  */
430 
431  start_cfp =
433  RUBY_VM_NEXT_CONTROL_FRAME(start_cfp)); /* skip top frames */
434 
435  if (start_cfp < last_cfp) {
436  size = 0;
437  }
438  else {
439  size = start_cfp - last_cfp + 1;
440  }
441 
442  init(arg, size);
443 
444  /* SDR(); */
445  for (i=0, cfp = start_cfp; i<size; i++, cfp = RUBY_VM_NEXT_CONTROL_FRAME(cfp)) {
446  /* fprintf(stderr, "cfp: %d\n", (rb_control_frame_t *)(th->stack + th->stack_size) - cfp); */
447  if (cfp->iseq) {
448  if (cfp->pc) {
449  iter_iseq(arg, cfp);
450  }
451  }
452  else if (RUBYVM_CFUNC_FRAME_P(cfp)) {
453  ID mid = cfp->me->def ? cfp->me->def->original_id : cfp->me->called_id;
454 
455  iter_cfunc(arg, cfp, mid);
456  }
457  }
458 }
459 
460 struct bt_iter_arg {
464 };
465 
466 static void
467 bt_init(void *ptr, size_t size)
468 {
469  struct bt_iter_arg *arg = (struct bt_iter_arg *)ptr;
472  arg->bt->backtrace_base = arg->bt->backtrace = ruby_xmalloc(sizeof(rb_backtrace_location_t) * size);
473  arg->bt->backtrace_size = 0;
474 }
475 
476 static void
477 bt_iter_iseq(void *ptr, const rb_control_frame_t *cfp)
478 {
479  const rb_iseq_t *iseq = cfp->iseq;
480  const VALUE *pc = cfp->pc;
481  struct bt_iter_arg *arg = (struct bt_iter_arg *)ptr;
482  rb_backtrace_location_t *loc = &arg->bt->backtrace[arg->bt->backtrace_size++];
483  loc->type = LOCATION_TYPE_ISEQ;
484  loc->body.iseq.iseq = iseq;
485  loc->body.iseq.lineno.pc = pc;
486  arg->prev_loc = loc;
487 }
488 
489 static void
490 bt_iter_cfunc(void *ptr, const rb_control_frame_t *cfp, ID mid)
491 {
492  struct bt_iter_arg *arg = (struct bt_iter_arg *)ptr;
493  rb_backtrace_location_t *loc = &arg->bt->backtrace[arg->bt->backtrace_size++];
494  loc->type = LOCATION_TYPE_CFUNC;
495  loc->body.cfunc.mid = mid;
496  loc->body.cfunc.prev_loc = arg->prev_loc;
497 }
498 
499 static VALUE
501 {
502  struct bt_iter_arg arg;
503  arg.prev_loc = 0;
504 
505  backtrace_each(th,
506  bt_init,
507  bt_iter_iseq,
509  &arg);
510 
511  return arg.btobj;
512 }
513 
514 VALUE
516 {
517  return backtrace_object(GET_THREAD());
518 }
519 
520 static VALUE
521 backtrace_collect(rb_backtrace_t *bt, long lev, long n, VALUE (*func)(rb_backtrace_location_t *, void *arg), void *arg)
522 {
523  VALUE btary;
524  int i;
525 
526  if (UNLIKELY(lev < 0 || n < 0)) {
527  rb_bug("backtrace_collect: unreachable");
528  }
529 
530  btary = rb_ary_new();
531 
532  for (i=0; i+lev<bt->backtrace_size && i<n; i++) {
533  rb_backtrace_location_t *loc = &bt->backtrace[bt->backtrace_size - 1 - (lev+i)];
534  rb_ary_push(btary, func(loc, arg));
535  }
536 
537  return btary;
538 }
539 
540 static VALUE
542 {
543  return location_to_str(loc);
544 }
545 
546 static VALUE
547 backtrace_to_str_ary(VALUE self, long lev, long n)
548 {
550  int size;
551  VALUE r;
552 
554  size = bt->backtrace_size;
555 
556  if (n == 0) {
557  n = size;
558  }
559  if (lev > size) {
560  return Qnil;
561  }
562 
563  r = backtrace_collect(bt, lev, n, location_to_str_dmyarg, 0);
564  RB_GC_GUARD(self);
565  return r;
566 }
567 
568 VALUE
570 {
573 
574  if (!bt->strary) {
575  bt->strary = backtrace_to_str_ary(self, 0, bt->backtrace_size);
576  }
577  return bt->strary;
578 }
579 
580 static VALUE
582 {
583  VALUE obj;
584  struct valued_frame_info *vloc;
585  obj = TypedData_Make_Struct(rb_cBacktraceLocation, struct valued_frame_info, &location_data_type, vloc);
586 
587  vloc->loc = srcloc;
588  vloc->btobj = (VALUE)btobj;
589 
590  return obj;
591 }
592 
593 static VALUE
594 backtrace_to_location_ary(VALUE self, long lev, long n)
595 {
596  rb_backtrace_t *bt;
597  int size;
598  VALUE r;
599 
601  size = bt->backtrace_size;
602 
603  if (n == 0) {
604  n = size;
605  }
606  if (lev > size) {
607  return Qnil;
608  }
609 
610  r = backtrace_collect(bt, lev, n, location_create, (void *)self);
611  RB_GC_GUARD(self);
612  return r;
613 }
614 
615 static VALUE
617 {
618  VALUE str = rb_backtrace_to_str_ary(self);
619  return str;
620 }
621 
622 static VALUE
624 {
625  rb_backtrace_t *bt;
627  bt->strary = str;
628  return self;
629 }
630 
631 VALUE
632 vm_backtrace_str_ary(rb_thread_t *th, long lev, long n)
633 {
634  return backtrace_to_str_ary(backtrace_object(th), lev, n);
635 }
636 
637 VALUE
639 {
640  return backtrace_to_location_ary(backtrace_object(th), lev, n);
641 }
642 
643 /* make old style backtrace directly */
644 
645 struct oldbt_arg {
647  int lineno;
648  void (*func)(void *data, VALUE file, int lineno, VALUE name);
649  void *data; /* result */
650 };
651 
652 static void
653 oldbt_init(void *ptr, size_t dmy)
654 {
655  struct oldbt_arg *arg = (struct oldbt_arg *)ptr;
656  rb_thread_t *th = GET_THREAD();
657 
658  arg->filename = th->vm->progname ? th->vm->progname : ruby_engine_name;;
659  arg->lineno = 0;
660 }
661 
662 static void
663 oldbt_iter_iseq(void *ptr, const rb_control_frame_t *cfp)
664 {
665  const rb_iseq_t *iseq = cfp->iseq;
666  const VALUE *pc = cfp->pc;
667  struct oldbt_arg *arg = (struct oldbt_arg *)ptr;
668  VALUE file = arg->filename = iseq->location.path;
669  VALUE name = iseq->location.label;
670  int lineno = arg->lineno = calc_lineno(iseq, pc);
671 
672  (arg->func)(arg->data, file, lineno, name);
673 }
674 
675 static void
676 oldbt_iter_cfunc(void *ptr, const rb_control_frame_t *cfp, ID mid)
677 {
678  struct oldbt_arg *arg = (struct oldbt_arg *)ptr;
679  VALUE file = arg->filename;
680  VALUE name = rb_id2str(mid);
681  int lineno = arg->lineno;
682 
683  (arg->func)(arg->data, file, lineno, name);
684 }
685 
686 static void
687 oldbt_print(void *data, VALUE file, int lineno, VALUE name)
688 {
689  FILE *fp = (FILE *)data;
690 
691  if (NIL_P(name)) {
692  fprintf(fp, "\tfrom %s:%d:in unknown method\n",
693  RSTRING_PTR(file), lineno);
694  }
695  else {
696  fprintf(fp, "\tfrom %s:%d:in `%s'\n",
697  RSTRING_PTR(file), lineno, RSTRING_PTR(name));
698  }
699 }
700 
701 static void
703 {
704  struct oldbt_arg arg;
705 
706  arg.func = oldbt_print;
707  arg.data = (void *)fp;
709  oldbt_init,
712  &arg);
713 }
714 
715 static void
716 oldbt_bugreport(void *arg, VALUE file, int line, VALUE method)
717 {
718  const char *filename = NIL_P(file) ? "ruby" : RSTRING_PTR(file);
719  if (!*(int *)arg) {
720  fprintf(stderr, "-- Ruby level backtrace information "
721  "----------------------------------------\n");
722  *(int *)arg = 1;
723  }
724  if (NIL_P(method)) {
725  fprintf(stderr, "%s:%d:in unknown method\n", filename, line);
726  }
727  else {
728  fprintf(stderr, "%s:%d:in `%s'\n", filename, line, RSTRING_PTR(method));
729  }
730 }
731 
732 void
734 {
735  struct oldbt_arg arg;
736  int i;
737 
738  arg.func = oldbt_bugreport;
739  arg.data = (int *)&i;
740 
742  oldbt_init,
745  &arg);
746 }
747 
748 void
750 {
751  vm_backtrace_print(stderr);
752 }
753 
754 VALUE
756 {
757  return vm_backtrace_str_ary(GET_THREAD(), 0, 0);
758 }
759 
760 static VALUE
761 vm_backtrace_to_ary(rb_thread_t *th, int argc, VALUE *argv, int lev_default, int lev_plus, int to_str)
762 {
763  VALUE level, vn;
764  long lev, n;
765  VALUE btval = backtrace_object(th);
766  VALUE r;
767  rb_backtrace_t *bt;
768 
770 
771  rb_scan_args(argc, argv, "02", &level, &vn);
772 
773  switch (argc) {
774  case 0:
775  lev = lev_default + lev_plus;
776  n = bt->backtrace_size - lev;
777  break;
778  case 1:
779  {
780  long beg, len;
781  switch (rb_range_beg_len(level, &beg, &len, bt->backtrace_size - lev_plus, 0)) {
782  case Qfalse:
783  lev = NUM2LONG(level);
784  if (lev < 0) {
785  rb_raise(rb_eArgError, "negative level (%ld)", lev);
786  }
787  lev += lev_plus;
788  n = bt->backtrace_size - lev;
789  break;
790  case Qnil:
791  return Qnil;
792  default:
793  lev = beg + lev_plus;
794  n = len;
795  break;
796  }
797  break;
798  }
799  case 2:
800  lev = NUM2LONG(level);
801  n = NUM2LONG(vn);
802  if (lev < 0) {
803  rb_raise(rb_eArgError, "negative level (%ld)", lev);
804  }
805  if (n < 0) {
806  rb_raise(rb_eArgError, "negative size (%ld)", n);
807  }
808  lev += lev_plus;
809  break;
810  default:
811  lev = n = 0; /* to avoid warning */
812  break;
813  }
814 
815  if (n == 0) {
816  return rb_ary_new();
817  }
818 
819  if (to_str) {
820  r = backtrace_to_str_ary(btval, lev, n);
821  }
822  else {
823  r = backtrace_to_location_ary(btval, lev, n);
824  }
825  RB_GC_GUARD(btval);
826  return r;
827 }
828 
829 static VALUE
830 thread_backtrace_to_ary(int argc, VALUE *argv, VALUE thval, int to_str)
831 {
832  rb_thread_t *th;
833  GetThreadPtr(thval, th);
834 
835  if (th->to_kill || th->status == THREAD_KILLED)
836  return Qnil;
837 
838  return vm_backtrace_to_ary(th, argc, argv, 0, 0, to_str);
839 }
840 
841 VALUE
843 {
844  return thread_backtrace_to_ary(argc, argv, thval, 1);
845 }
846 
847 VALUE
849 {
850  return thread_backtrace_to_ary(argc, argv, thval, 0);
851 }
852 
853 /*
854  * call-seq:
855  * caller(start=1, length=nil) -> array or nil
856  * caller(range) -> array or nil
857  *
858  * Returns the current execution stack---an array containing strings in
859  * the form <code>file:line</code> or <code>file:line: in
860  * `method'</code>.
861  *
862  * The optional _start_ parameter determines the number of initial stack
863  * entries to omit from the top of the stack.
864  *
865  * A second optional +length+ parameter can be used to limit how many entries
866  * are returned from the stack.
867  *
868  * Returns +nil+ if _start_ is greater than the size of
869  * current execution stack.
870  *
871  * Optionally you can pass a range, which will return an array containing the
872  * entries within the specified range.
873  *
874  * def a(skip)
875  * caller(skip)
876  * end
877  * def b(skip)
878  * a(skip)
879  * end
880  * def c(skip)
881  * b(skip)
882  * end
883  * c(0) #=> ["prog:2:in `a'", "prog:5:in `b'", "prog:8:in `c'", "prog:10:in `<main>'"]
884  * c(1) #=> ["prog:5:in `b'", "prog:8:in `c'", "prog:11:in `<main>'"]
885  * c(2) #=> ["prog:8:in `c'", "prog:12:in `<main>'"]
886  * c(3) #=> ["prog:13:in `<main>'"]
887  * c(4) #=> []
888  * c(5) #=> nil
889  */
890 
891 static VALUE
893 {
894  return vm_backtrace_to_ary(GET_THREAD(), argc, argv, 1, 1, 1);
895 }
896 
897 /*
898  * call-seq:
899  * caller_locations(start=1, length=nil) -> array or nil
900  * caller_locations(range) -> array or nil
901  *
902  * Returns the current execution stack---an array containing
903  * backtrace location objects.
904  *
905  * See Thread::Backtrace::Location for more information.
906  *
907  * The optional _start_ parameter determines the number of initial stack
908  * entries to omit from the top of the stack.
909  *
910  * A second optional +length+ parameter can be used to limit how many entries
911  * are returned from the stack.
912  *
913  * Returns +nil+ if _start_ is greater than the size of
914  * current execution stack.
915  *
916  * Optionally you can pass a range, which will return an array containing the
917  * entries within the specified range.
918  */
919 static VALUE
921 {
922  return vm_backtrace_to_ary(GET_THREAD(), argc, argv, 1, 1, 0);
923 }
924 
925 /* called from Init_vm() in vm.c */
926 void
928 {
929  /* :nodoc: */
934 
935  /*
936  * An object representation of a stack frame, initialized by
937  * Kernel#caller_locations.
938  *
939  * For example:
940  *
941  * # caller_locations.rb
942  * def a(skip)
943  * caller_locations(skip)
944  * end
945  * def b(skip)
946  * a(skip)
947  * end
948  * def c(skip)
949  * b(skip)
950  * end
951  *
952  * c(0..2).map do |call|
953  * puts call.to_s
954  * end
955  *
956  * Running <code>ruby caller_locations.rb</code> will produce:
957  *
958  * caller_locations.rb:2:in `a'
959  * caller_locations.rb:5:in `b'
960  * caller_locations.rb:8:in `c'
961  *
962  * Here's another example with a slightly different result:
963  *
964  * # foo.rb
965  * class Foo
966  * attr_accessor :locations
967  * def initialize(skip)
968  * @locations = caller_locations(skip)
969  * end
970  * end
971  *
972  * Foo.new(0..2).locations.map do |call|
973  * puts call.to_s
974  * end
975  *
976  * Now run <code>ruby foo.rb</code> and you should see:
977  *
978  * init.rb:4:in `initialize'
979  * init.rb:8:in `new'
980  * init.rb:8:in `<main>'
981  */
992 
993  rb_define_global_function("caller", rb_f_caller, -1);
994  rb_define_global_function("caller_locations", rb_f_caller_locations, -1);
995 }
996 
997 /* debugger API */
998 
999 #if defined __GNUC__ && __GNUC__ >= 4
1000 #pragma GCC visibility push(default)
1001 #endif
1002 
1003 #if defined __GNUC__ && __GNUC__ >= 4
1004 #pragma GCC visibility pop
1005 #endif
1006 
1011  VALUE contexts; /* [[klass, binding, iseq, cfp], ...] */
1013 };
1014 
1015 enum {
1021 };
1022 
1025 };
1026 
1027 static void
1028 collect_caller_bindings_init(void *arg, size_t size)
1029 {
1030  /* */
1031 }
1032 
1033 static VALUE
1035 {
1036  VALUE klass;
1037  if (rb_vm_control_frame_id_and_class(cfp, 0, &klass)) {
1038  if (RB_TYPE_P(klass, T_ICLASS)) {
1039  return RBASIC(klass)->klass;
1040  }
1041  else {
1042  return klass;
1043  }
1044  }
1045  else {
1046  return Qnil;
1047  }
1048 }
1049 
1050 static void
1052 {
1053  struct collect_caller_bindings_data *data = (struct collect_caller_bindings_data *)arg;
1054  VALUE frame = rb_ary_new2(5);
1055 
1056  rb_ary_store(frame, CALLER_BINDING_SELF, cfp->self);
1058  rb_ary_store(frame, CALLER_BINDING_BINDING, GC_GUARDED_PTR(cfp)); /* create later */
1059  rb_ary_store(frame, CALLER_BINDING_ISEQ, cfp->iseq ? cfp->iseq->self : Qnil);
1061 
1062  rb_ary_push(data->ary, frame);
1063 }
1064 
1065 static void
1067 {
1068  struct collect_caller_bindings_data *data = (struct collect_caller_bindings_data *)arg;
1069  VALUE frame = rb_ary_new2(5);
1070 
1071  rb_ary_store(frame, CALLER_BINDING_SELF, cfp->self);
1073  rb_ary_store(frame, CALLER_BINDING_BINDING, Qnil); /* not available */
1074  rb_ary_store(frame, CALLER_BINDING_ISEQ, Qnil); /* not available */
1076 
1077  rb_ary_push(data->ary, frame);
1078 }
1079 
1080 static VALUE
1082 {
1083  struct collect_caller_bindings_data data;
1084  VALUE result;
1085  int i;
1086 
1087  data.ary = rb_ary_new();
1088 
1089  backtrace_each(th,
1093  &data);
1094 
1095  result = rb_ary_reverse(data.ary);
1096 
1097  /* bindings should be created from top of frame */
1098  for (i=0; i<RARRAY_LEN(result); i++) {
1099  VALUE entry = rb_ary_entry(result, i);
1100  VALUE cfp_val = rb_ary_entry(entry, CALLER_BINDING_BINDING);
1101 
1102  if (!NIL_P(cfp_val)) {
1103  rb_control_frame_t *cfp = GC_GUARDED_PTR_REF(cfp_val);
1105  }
1106  }
1107 
1108  return result;
1109 }
1110 
1111 /*
1112  * Note that the passed `rb_debug_inspector_t' will be disabled
1113  * after `rb_debug_inspector_open'.
1114  */
1115 
1116 VALUE
1118 {
1119  rb_debug_inspector_t dbg_context;
1120  rb_thread_t *th = GET_THREAD();
1121  int state;
1122  volatile VALUE UNINITIALIZED_VAR(result);
1123 
1124  dbg_context.th = th;
1125  dbg_context.cfp = dbg_context.th->cfp;
1126  dbg_context.backtrace = vm_backtrace_location_ary(th, 0, 0);
1127  dbg_context.backtrace_size = RARRAY_LEN(dbg_context.backtrace);
1128  dbg_context.contexts = collect_caller_bindings(th);
1129 
1130  TH_PUSH_TAG(th);
1131  if ((state = EXEC_TAG()) == 0) {
1132  result = (*func)(&dbg_context, data);
1133  }
1134  TH_POP_TAG();
1135 
1136  /* invalidate bindings? */
1137 
1138  if (state) {
1139  JUMP_TAG(state);
1140  }
1141 
1142  return result;
1143 }
1144 
1145 static VALUE
1146 frame_get(const rb_debug_inspector_t *dc, long index)
1147 {
1148  if (index < 0 || index >= dc->backtrace_size) {
1149  rb_raise(rb_eArgError, "no such frame");
1150  }
1151  return rb_ary_entry(dc->contexts, index);
1152 }
1153 
1154 VALUE
1156 {
1157  VALUE frame = frame_get(dc, index);
1158  return rb_ary_entry(frame, CALLER_BINDING_SELF);
1159 }
1160 
1161 VALUE
1163 {
1164  VALUE frame = frame_get(dc, index);
1165  return rb_ary_entry(frame, CALLER_BINDING_CLASS);
1166 }
1167 
1168 VALUE
1170 {
1171  VALUE frame = frame_get(dc, index);
1172  return rb_ary_entry(frame, CALLER_BINDING_BINDING);
1173 }
1174 
1175 VALUE
1177 {
1178  VALUE frame = frame_get(dc, index);
1179  return rb_ary_entry(frame, CALLER_BINDING_ISEQ);
1180 }
1181 
1182 VALUE
1184 {
1185  return dc->backtrace;
1186 }
1187 
1188