Ruby 3.2.1p31 (2023-02-08 revision 31819e82c88c6f8ecfaeb162519bfa26a14b21fd)
vm_backtrace.c
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 "eval_intern.h"
13#include "internal.h"
14#include "internal/error.h"
15#include "internal/vm.h"
16#include "iseq.h"
17#include "ruby/debug.h"
18#include "ruby/encoding.h"
19#include "vm_core.h"
20
21static VALUE rb_cBacktrace;
22static VALUE rb_cBacktraceLocation;
23
24static VALUE
25id2str(ID id)
26{
27 VALUE str = rb_id2str(id);
28 if (!str) return Qnil;
29 return str;
30}
31#define rb_id2str(id) id2str(id)
32
33#define BACKTRACE_START 0
34#define ALL_BACKTRACE_LINES -1
35
36inline static int
37calc_pos(const rb_iseq_t *iseq, const VALUE *pc, int *lineno, int *node_id)
38{
39 VM_ASSERT(iseq);
40
41 if (pc == NULL) {
42 if (ISEQ_BODY(iseq)->type == ISEQ_TYPE_TOP) {
43 VM_ASSERT(! ISEQ_BODY(iseq)->local_table);
44 VM_ASSERT(! ISEQ_BODY(iseq)->local_table_size);
45 return 0;
46 }
47 if (lineno) *lineno = ISEQ_BODY(iseq)->location.first_lineno;
48#ifdef USE_ISEQ_NODE_ID
49 if (node_id) *node_id = -1;
50#endif
51 return 1;
52 }
53 else {
54 VM_ASSERT(ISEQ_BODY(iseq));
55 VM_ASSERT(ISEQ_BODY(iseq)->iseq_encoded);
56 VM_ASSERT(ISEQ_BODY(iseq)->iseq_size);
57
58 ptrdiff_t n = pc - ISEQ_BODY(iseq)->iseq_encoded;
59 VM_ASSERT(n <= ISEQ_BODY(iseq)->iseq_size);
60 VM_ASSERT(n >= 0);
61 ASSUME(n >= 0);
62 size_t pos = n; /* no overflow */
63 if (LIKELY(pos)) {
64 /* use pos-1 because PC points next instruction at the beginning of instruction */
65 pos--;
66 }
67#if VMDEBUG && defined(HAVE_BUILTIN___BUILTIN_TRAP)
68 else {
69 /* SDR() is not possible; that causes infinite loop. */
70 rb_print_backtrace();
71 __builtin_trap();
72 }
73#endif
74 if (lineno) *lineno = rb_iseq_line_no(iseq, pos);
75#ifdef USE_ISEQ_NODE_ID
76 if (node_id) *node_id = rb_iseq_node_id(iseq, pos);
77#endif
78 return 1;
79 }
80}
81
82inline static int
83calc_lineno(const rb_iseq_t *iseq, const VALUE *pc)
84{
85 int lineno;
86 if (calc_pos(iseq, pc, &lineno, NULL)) return lineno;
87 return 0;
88}
89
90#ifdef USE_ISEQ_NODE_ID
91inline static int
92calc_node_id(const rb_iseq_t *iseq, const VALUE *pc)
93{
94 int node_id;
95 if (calc_pos(iseq, pc, NULL, &node_id)) return node_id;
96 return -1;
97}
98#endif
99
100int
101rb_vm_get_sourceline(const rb_control_frame_t *cfp)
102{
103 if (VM_FRAME_RUBYFRAME_P(cfp) && cfp->iseq) {
104 const rb_iseq_t *iseq = cfp->iseq;
105 int line = calc_lineno(iseq, cfp->pc);
106 if (line != 0) {
107 return line;
108 }
109 else {
110 return ISEQ_BODY(iseq)->location.first_lineno;
111 }
112 }
113 else {
114 return 0;
115 }
116}
117
119 enum LOCATION_TYPE {
120 LOCATION_TYPE_ISEQ = 1,
121 LOCATION_TYPE_CFUNC,
122 } type;
123
124 const rb_iseq_t *iseq;
125 const VALUE *pc;
126 ID mid;
128
131 VALUE btobj;
132};
133
134static void
135location_mark(void *ptr)
136{
137 struct valued_frame_info *vfi = (struct valued_frame_info *)ptr;
138 rb_gc_mark(vfi->btobj);
139}
140
141static void
142location_mark_entry(rb_backtrace_location_t *fi)
143{
144 switch (fi->type) {
145 case LOCATION_TYPE_ISEQ:
146 rb_gc_mark_movable((VALUE)fi->iseq);
147 break;
148 case LOCATION_TYPE_CFUNC:
149 if (fi->iseq) {
150 rb_gc_mark_movable((VALUE)fi->iseq);
151 }
152 break;
153 default:
154 break;
155 }
156}
157
158static size_t
159location_memsize(const void *ptr)
160{
161 /* rb_backtrace_location_t *fi = (rb_backtrace_location_t *)ptr; */
162 return sizeof(rb_backtrace_location_t);
163}
164
165static const rb_data_type_t location_data_type = {
166 "frame_info",
167 {location_mark, RUBY_TYPED_DEFAULT_FREE, location_memsize,},
168 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
169};
170
171int
172rb_frame_info_p(VALUE obj)
173{
174 return rb_typeddata_is_kind_of(obj, &location_data_type);
175}
176
177static inline rb_backtrace_location_t *
178location_ptr(VALUE locobj)
179{
180 struct valued_frame_info *vloc;
181 GetCoreDataFromValue(locobj, struct valued_frame_info, vloc);
182 return vloc->loc;
183}
184
185static int
186location_lineno(rb_backtrace_location_t *loc)
187{
188 switch (loc->type) {
189 case LOCATION_TYPE_ISEQ:
190 return calc_lineno(loc->iseq, loc->pc);
191 case LOCATION_TYPE_CFUNC:
192 if (loc->iseq && loc->pc) {
193 return calc_lineno(loc->iseq, loc->pc);
194 }
195 return 0;
196 default:
197 rb_bug("location_lineno: unreachable");
199 }
200}
201
202/*
203 * Returns the line number of this frame.
204 *
205 * For example, using +caller_locations.rb+ from Thread::Backtrace::Location
206 *
207 * loc = c(0..1).first
208 * loc.lineno #=> 2
209 */
210static VALUE
211location_lineno_m(VALUE self)
212{
213 return INT2FIX(location_lineno(location_ptr(self)));
214}
215
216static VALUE
217location_label(rb_backtrace_location_t *loc)
218{
219 switch (loc->type) {
220 case LOCATION_TYPE_ISEQ:
221 return ISEQ_BODY(loc->iseq)->location.label;
222 case LOCATION_TYPE_CFUNC:
223 return rb_id2str(loc->mid);
224 default:
225 rb_bug("location_label: unreachable");
227 }
228}
229
230/*
231 * Returns the label of this frame.
232 *
233 * Usually consists of method, class, module, etc names with decoration.
234 *
235 * Consider the following example:
236 *
237 * def foo
238 * puts caller_locations(0).first.label
239 *
240 * 1.times do
241 * puts caller_locations(0).first.label
242 *
243 * 1.times do
244 * puts caller_locations(0).first.label
245 * end
246 *
247 * end
248 * end
249 *
250 * The result of calling +foo+ is this:
251 *
252 * label: foo
253 * label: block in foo
254 * label: block (2 levels) in foo
255 *
256 */
257static VALUE
258location_label_m(VALUE self)
259{
260 return location_label(location_ptr(self));
261}
262
263static VALUE
264location_base_label(rb_backtrace_location_t *loc)
265{
266 switch (loc->type) {
267 case LOCATION_TYPE_ISEQ:
268 return ISEQ_BODY(loc->iseq)->location.base_label;
269 case LOCATION_TYPE_CFUNC:
270 return rb_id2str(loc->mid);
271 default:
272 rb_bug("location_base_label: unreachable");
274 }
275}
276
277/*
278 * Returns the base label of this frame.
279 *
280 * Usually same as #label, without decoration.
281 */
282static VALUE
283location_base_label_m(VALUE self)
284{
285 return location_base_label(location_ptr(self));
286}
287
288static const rb_iseq_t *
289location_iseq(rb_backtrace_location_t *loc)
290{
291 switch (loc->type) {
292 case LOCATION_TYPE_ISEQ:
293 return loc->iseq;
294 case LOCATION_TYPE_CFUNC:
295 return loc->iseq;
296 default:
297 rb_bug("location_iseq: unreachable");
299 }
300}
301
302/*
303 * Returns the file name of this frame. This will generally be an absolute
304 * path, unless the frame is in the main script, in which case it will be the
305 * script location passed on the command line.
306 *
307 * For example, using +caller_locations.rb+ from Thread::Backtrace::Location
308 *
309 * loc = c(0..1).first
310 * loc.path #=> caller_locations.rb
311 */
312static VALUE
313location_path_m(VALUE self)
314{
315 const rb_iseq_t *iseq = location_iseq(location_ptr(self));
316 return iseq ? rb_iseq_path(iseq) : Qnil;
317}
318
319#ifdef USE_ISEQ_NODE_ID
320static int
321location_node_id(rb_backtrace_location_t *loc)
322{
323 switch (loc->type) {
324 case LOCATION_TYPE_ISEQ:
325 return calc_node_id(loc->iseq, loc->pc);
326 case LOCATION_TYPE_CFUNC:
327 if (loc->iseq && loc->pc) {
328 return calc_node_id(loc->iseq, loc->pc);
329 }
330 return -1;
331 default:
332 rb_bug("location_node_id: unreachable");
334 }
335}
336#endif
337
338int
339rb_get_node_id_from_frame_info(VALUE obj)
340{
341#ifdef USE_ISEQ_NODE_ID
342 rb_backtrace_location_t *loc = location_ptr(obj);
343 return location_node_id(loc);
344#else
345 return -1;
346#endif
347}
348
349const rb_iseq_t *
350rb_get_iseq_from_frame_info(VALUE obj)
351{
352 rb_backtrace_location_t *loc = location_ptr(obj);
353 const rb_iseq_t *iseq = location_iseq(loc);
354 return iseq;
355}
356
357static VALUE
358location_realpath(rb_backtrace_location_t *loc)
359{
360 switch (loc->type) {
361 case LOCATION_TYPE_ISEQ:
362 return rb_iseq_realpath(loc->iseq);
363 case LOCATION_TYPE_CFUNC:
364 if (loc->iseq) {
365 return rb_iseq_realpath(loc->iseq);
366 }
367 return Qnil;
368 default:
369 rb_bug("location_realpath: unreachable");
371 }
372}
373
374/*
375 * Returns the full file path of this frame.
376 *
377 * Same as #path, except that it will return absolute path
378 * even if the frame is in the main script.
379 */
380static VALUE
381location_absolute_path_m(VALUE self)
382{
383 return location_realpath(location_ptr(self));
384}
385
386static VALUE
387location_format(VALUE file, int lineno, VALUE name)
388{
389 VALUE s = rb_enc_sprintf(rb_enc_compatible(file, name), "%s", RSTRING_PTR(file));
390 if (lineno != 0) {
391 rb_str_catf(s, ":%d", lineno);
392 }
393 rb_str_cat_cstr(s, ":in ");
394 if (NIL_P(name)) {
395 rb_str_cat_cstr(s, "unknown method");
396 }
397 else {
398 rb_str_catf(s, "`%s'", RSTRING_PTR(name));
399 }
400 return s;
401}
402
403static VALUE
404location_to_str(rb_backtrace_location_t *loc)
405{
406 VALUE file, name;
407 int lineno;
408
409 switch (loc->type) {
410 case LOCATION_TYPE_ISEQ:
411 file = rb_iseq_path(loc->iseq);
412 name = ISEQ_BODY(loc->iseq)->location.label;
413
414 lineno = calc_lineno(loc->iseq, loc->pc);
415 break;
416 case LOCATION_TYPE_CFUNC:
417 if (loc->iseq && loc->pc) {
418 file = rb_iseq_path(loc->iseq);
419 lineno = calc_lineno(loc->iseq, loc->pc);
420 }
421 else {
422 file = GET_VM()->progname;
423 lineno = 0;
424 }
425 name = rb_id2str(loc->mid);
426 break;
427 default:
428 rb_bug("location_to_str: unreachable");
429 }
430
431 return location_format(file, lineno, name);
432}
433
434/*
435 * Returns a Kernel#caller style string representing this frame.
436 */
437static VALUE
438location_to_str_m(VALUE self)
439{
440 return location_to_str(location_ptr(self));
441}
442
443/*
444 * Returns the same as calling +inspect+ on the string representation of
445 * #to_str
446 */
447static VALUE
448location_inspect_m(VALUE self)
449{
450 return rb_str_inspect(location_to_str(location_ptr(self)));
451}
452
453typedef struct rb_backtrace_struct {
454 rb_backtrace_location_t *backtrace;
455 int backtrace_size;
456 VALUE strary;
457 VALUE locary;
459
460static void
461backtrace_mark(void *ptr)
462{
463 rb_backtrace_t *bt = (rb_backtrace_t *)ptr;
464 size_t i, s = bt->backtrace_size;
465
466 for (i=0; i<s; i++) {
467 location_mark_entry(&bt->backtrace[i]);
468 }
469 rb_gc_mark_movable(bt->strary);
470 rb_gc_mark_movable(bt->locary);
471}
472
473static void
474backtrace_free(void *ptr)
475{
476 rb_backtrace_t *bt = (rb_backtrace_t *)ptr;
477 if (bt->backtrace) ruby_xfree(bt->backtrace);
478 ruby_xfree(bt);
479}
480
481static void
482location_update_entry(rb_backtrace_location_t *fi)
483{
484 switch (fi->type) {
485 case LOCATION_TYPE_ISEQ:
486 fi->iseq = (rb_iseq_t*)rb_gc_location((VALUE)fi->iseq);
487 break;
488 case LOCATION_TYPE_CFUNC:
489 if (fi->iseq) {
490 fi->iseq = (rb_iseq_t*)rb_gc_location((VALUE)fi->iseq);
491 }
492 break;
493 default:
494 break;
495 }
496}
497
498static void
499backtrace_update(void *ptr)
500{
501 rb_backtrace_t *bt = (rb_backtrace_t *)ptr;
502 size_t i, s = bt->backtrace_size;
503
504 for (i=0; i<s; i++) {
505 location_update_entry(&bt->backtrace[i]);
506 }
507 bt->strary = rb_gc_location(bt->strary);
508 bt->locary = rb_gc_location(bt->locary);
509}
510
511static size_t
512backtrace_memsize(const void *ptr)
513{
514 rb_backtrace_t *bt = (rb_backtrace_t *)ptr;
515 return sizeof(rb_backtrace_t) + sizeof(rb_backtrace_location_t) * bt->backtrace_size;
516}
517
518static const rb_data_type_t backtrace_data_type = {
519 "backtrace",
520 {backtrace_mark, backtrace_free, backtrace_memsize, backtrace_update},
521 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
522};
523
524int
525rb_backtrace_p(VALUE obj)
526{
527 return rb_typeddata_is_kind_of(obj, &backtrace_data_type);
528}
529
530static VALUE
531backtrace_alloc(VALUE klass)
532{
533 rb_backtrace_t *bt;
534 VALUE obj = TypedData_Make_Struct(klass, rb_backtrace_t, &backtrace_data_type, bt);
535 return obj;
536}
537
538static long
539backtrace_size(const rb_execution_context_t *ec)
540{
541 const rb_control_frame_t *last_cfp = ec->cfp;
542 const rb_control_frame_t *start_cfp = RUBY_VM_END_CONTROL_FRAME(ec);
543
544 if (start_cfp == NULL) {
545 return -1;
546 }
547
548 start_cfp =
549 RUBY_VM_NEXT_CONTROL_FRAME(
550 RUBY_VM_NEXT_CONTROL_FRAME(start_cfp)); /* skip top frames */
551
552 if (start_cfp < last_cfp) {
553 return 0;
554 }
555
556 return start_cfp - last_cfp + 1;
557}
558
559static bool
560is_internal_location(const rb_control_frame_t *cfp)
561{
562 static const char prefix[] = "<internal:";
563 const size_t prefix_len = sizeof(prefix) - 1;
564 VALUE file = rb_iseq_path(cfp->iseq);
565 return strncmp(prefix, RSTRING_PTR(file), prefix_len) == 0;
566}
567
568static void
569bt_update_cfunc_loc(unsigned long cfunc_counter, rb_backtrace_location_t *cfunc_loc, const rb_iseq_t *iseq, const VALUE *pc)
570{
571 for (; cfunc_counter > 0; cfunc_counter--, cfunc_loc--) {
572 cfunc_loc->iseq = iseq;
573 cfunc_loc->pc = pc;
574 }
575}
576
577static VALUE location_create(rb_backtrace_location_t *srcloc, void *btobj);
578
579static void
580bt_yield_loc(rb_backtrace_location_t *loc, long num_frames, VALUE btobj)
581{
582 for (; num_frames > 0; num_frames--, loc++) {
583 rb_yield(location_create(loc, (void *)btobj));
584 }
585}
586
587static VALUE
588rb_ec_partial_backtrace_object(const rb_execution_context_t *ec, long start_frame, long num_frames, int* start_too_large, bool skip_internal, bool do_yield)
589{
590 const rb_control_frame_t *cfp = ec->cfp;
591 const rb_control_frame_t *end_cfp = RUBY_VM_END_CONTROL_FRAME(ec);
592 ptrdiff_t size;
593 rb_backtrace_t *bt;
594 VALUE btobj = backtrace_alloc(rb_cBacktrace);
595 rb_backtrace_location_t *loc = NULL;
596 unsigned long cfunc_counter = 0;
597 GetCoreDataFromValue(btobj, rb_backtrace_t, bt);
598
599 // In the case the thread vm_stack or cfp is not initialized, there is no backtrace.
600 if (end_cfp == NULL) {
601 num_frames = 0;
602 }
603 else {
604 end_cfp = RUBY_VM_NEXT_CONTROL_FRAME(end_cfp);
605
606 /*
607 * top frame (dummy) <- RUBY_VM_END_CONTROL_FRAME
608 * top frame (dummy) <- end_cfp
609 * top frame <- main script
610 * top frame
611 * ...
612 * 2nd frame <- lev:0
613 * current frame <- ec->cfp
614 */
615
616 size = end_cfp - cfp + 1;
617 if (size < 0) {
618 num_frames = 0;
619 }
620 else if (num_frames < 0 || num_frames > size) {
621 num_frames = size;
622 }
623 }
624
625 bt->backtrace = ZALLOC_N(rb_backtrace_location_t, num_frames);
626 bt->backtrace_size = 0;
627 if (num_frames == 0) {
628 if (start_too_large) *start_too_large = 0;
629 return btobj;
630 }
631
632 for (; cfp != end_cfp && (bt->backtrace_size < num_frames); cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp)) {
633 if (cfp->iseq) {
634 if (cfp->pc) {
635 if (start_frame > 0) {
636 start_frame--;
637 }
638 else if (!skip_internal || !is_internal_location(cfp)) {
639 const rb_iseq_t *iseq = cfp->iseq;
640 const VALUE *pc = cfp->pc;
641 loc = &bt->backtrace[bt->backtrace_size++];
642 loc->type = LOCATION_TYPE_ISEQ;
643 loc->iseq = iseq;
644 loc->pc = pc;
645 bt_update_cfunc_loc(cfunc_counter, loc-1, iseq, pc);
646 if (do_yield) {
647 bt_yield_loc(loc - cfunc_counter, cfunc_counter+1, btobj);
648 }
649 cfunc_counter = 0;
650 }
651 }
652 }
653 else {
654 VM_ASSERT(RUBYVM_CFUNC_FRAME_P(cfp));
655 if (start_frame > 0) {
656 start_frame--;
657 }
658 else {
659 loc = &bt->backtrace[bt->backtrace_size++];
660 loc->type = LOCATION_TYPE_CFUNC;
661 loc->iseq = NULL;
662 loc->pc = NULL;
663 loc->mid = rb_vm_frame_method_entry(cfp)->def->original_id;
664 cfunc_counter++;
665 }
666 }
667 }
668
669 if (cfunc_counter > 0) {
670 for (; cfp != end_cfp; cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp)) {
671 if (cfp->iseq && cfp->pc && (!skip_internal || !is_internal_location(cfp))) {
672 bt_update_cfunc_loc(cfunc_counter, loc, cfp->iseq, cfp->pc);
673 if (do_yield) {
674 bt_yield_loc(loc - cfunc_counter, cfunc_counter, btobj);
675 }
676 break;
677 }
678 }
679 }
680
681 if (start_too_large) *start_too_large = (start_frame > 0 ? -1 : 0);
682 return btobj;
683}
684
685MJIT_FUNC_EXPORTED VALUE
686rb_ec_backtrace_object(const rb_execution_context_t *ec)
687{
688 return rb_ec_partial_backtrace_object(ec, BACKTRACE_START, ALL_BACKTRACE_LINES, NULL, FALSE, FALSE);
689}
690
691static VALUE
692backtrace_collect(rb_backtrace_t *bt, VALUE (*func)(rb_backtrace_location_t *, void *arg), void *arg)
693{
694 VALUE btary;
695 int i;
696
697 btary = rb_ary_new2(bt->backtrace_size);
698
699 for (i=0; i<bt->backtrace_size; i++) {
700 rb_backtrace_location_t *loc = &bt->backtrace[i];
701 rb_ary_push(btary, func(loc, arg));
702 }
703
704 return btary;
705}
706
707static VALUE
708location_to_str_dmyarg(rb_backtrace_location_t *loc, void *dmy)
709{
710 return location_to_str(loc);
711}
712
713static VALUE
714backtrace_to_str_ary(VALUE self)
715{
716 VALUE r;
717 rb_backtrace_t *bt;
718 GetCoreDataFromValue(self, rb_backtrace_t, bt);
719 r = backtrace_collect(bt, location_to_str_dmyarg, 0);
720 RB_GC_GUARD(self);
721 return r;
722}
723
724VALUE
725rb_backtrace_to_str_ary(VALUE self)
726{
727 rb_backtrace_t *bt;
728 GetCoreDataFromValue(self, rb_backtrace_t, bt);
729
730 if (!bt->strary) {
731 bt->strary = backtrace_to_str_ary(self);
732 }
733 return bt->strary;
734}
735
736MJIT_FUNC_EXPORTED void
737rb_backtrace_use_iseq_first_lineno_for_last_location(VALUE self)
738{
739 const rb_backtrace_t *bt;
741
742 GetCoreDataFromValue(self, rb_backtrace_t, bt);
743 VM_ASSERT(bt->backtrace_size > 0);
744
745 loc = &bt->backtrace[0];
746
747 VM_ASSERT(loc->type == LOCATION_TYPE_ISEQ);
748
749 loc->pc = NULL; // means location.first_lineno
750}
751
752static VALUE
753location_create(rb_backtrace_location_t *srcloc, void *btobj)
754{
755 VALUE obj;
756 struct valued_frame_info *vloc;
757 obj = TypedData_Make_Struct(rb_cBacktraceLocation, struct valued_frame_info, &location_data_type, vloc);
758
759 vloc->loc = srcloc;
760 vloc->btobj = (VALUE)btobj;
761
762 return obj;
763}
764
765static VALUE
766backtrace_to_location_ary(VALUE self)
767{
768 VALUE r;
769 rb_backtrace_t *bt;
770 GetCoreDataFromValue(self, rb_backtrace_t, bt);
771 r = backtrace_collect(bt, location_create, (void *)self);
772 RB_GC_GUARD(self);
773 return r;
774}
775
776VALUE
777rb_backtrace_to_location_ary(VALUE self)
778{
779 rb_backtrace_t *bt;
780 GetCoreDataFromValue(self, rb_backtrace_t, bt);
781
782 if (!bt->locary) {
783 bt->locary = backtrace_to_location_ary(self);
784 }
785 return bt->locary;
786}
787
788static VALUE
789backtrace_dump_data(VALUE self)
790{
791 VALUE str = rb_backtrace_to_str_ary(self);
792 return str;
793}
794
795static VALUE
796backtrace_load_data(VALUE self, VALUE str)
797{
798 rb_backtrace_t *bt;
799 GetCoreDataFromValue(self, rb_backtrace_t, bt);
800 bt->strary = str;
801 return self;
802}
803
804/*
805 * call-seq: Thread::Backtrace::limit -> integer
806 *
807 * Returns maximum backtrace length set by <tt>--backtrace-limit</tt>
808 * command-line option. The default is <tt>-1</tt> which means unlimited
809 * backtraces. If the value is zero or positive, the error backtraces,
810 * produced by Exception#full_message, are abbreviated and the extra lines
811 * are replaced by <tt>... 3 levels... </tt>
812 *
813 * $ ruby -r net/http -e "p Thread::Backtrace.limit; Net::HTTP.get(URI('http://wrong.address'))"
814 * - 1
815 * .../lib/ruby/3.1.0/socket.rb:227:in `getaddrinfo': Failed to open TCP connection to wrong.address:80 (getaddrinfo: Name or service not known) (SocketError)
816 * from .../lib/ruby/3.1.0/socket.rb:227:in `foreach'
817 * from .../lib/ruby/3.1.0/socket.rb:632:in `tcp'
818 * from .../lib/ruby/3.1.0/net/http.rb:998:in `connect'
819 * from .../lib/ruby/3.1.0/net/http.rb:976:in `do_start'
820 * from .../lib/ruby/3.1.0/net/http.rb:965:in `start'
821 * from .../lib/ruby/3.1.0/net/http.rb:627:in `start'
822 * from .../lib/ruby/3.1.0/net/http.rb:503:in `get_response'
823 * from .../lib/ruby/3.1.0/net/http.rb:474:in `get'
824 * .../lib/ruby/3.1.0/socket.rb:227:in `getaddrinfo': getaddrinfo: Name or service not known (SocketError)
825 * from .../lib/ruby/3.1.0/socket.rb:227:in `foreach'
826 * from .../lib/ruby/3.1.0/socket.rb:632:in `tcp'
827 * from .../lib/ruby/3.1.0/net/http.rb:998:in `connect'
828 * from .../lib/ruby/3.1.0/net/http.rb:976:in `do_start'
829 * from .../lib/ruby/3.1.0/net/http.rb:965:in `start'
830 * from .../lib/ruby/3.1.0/net/http.rb:627:in `start'
831 * from .../lib/ruby/3.1.0/net/http.rb:503:in `get_response'
832 * from .../lib/ruby/3.1.0/net/http.rb:474:in `get'
833 * from -e:1:in `<main>'
834 *
835 * $ ruby --backtrace-limit 2 -r net/http -e "p Thread::Backtrace.limit; Net::HTTP.get(URI('http://wrong.address'))"
836 * 2
837 * .../lib/ruby/3.1.0/socket.rb:227:in `getaddrinfo': Failed to open TCP connection to wrong.address:80 (getaddrinfo: Name or service not known) (SocketError)
838 * from .../lib/ruby/3.1.0/socket.rb:227:in `foreach'
839 * from .../lib/ruby/3.1.0/socket.rb:632:in `tcp'
840 * ... 7 levels...
841 * .../lib/ruby/3.1.0/socket.rb:227:in `getaddrinfo': getaddrinfo: Name or service not known (SocketError)
842 * from .../lib/ruby/3.1.0/socket.rb:227:in `foreach'
843 * from .../lib/ruby/3.1.0/socket.rb:632:in `tcp'
844 * ... 7 levels...
845 *
846 * $ ruby --backtrace-limit 0 -r net/http -e "p Thread::Backtrace.limit; Net::HTTP.get(URI('http://wrong.address'))"
847 * 0
848 * .../lib/ruby/3.1.0/socket.rb:227:in `getaddrinfo': Failed to open TCP connection to wrong.address:80 (getaddrinfo: Name or service not known) (SocketError)
849 * ... 9 levels...
850 * .../lib/ruby/3.1.0/socket.rb:227:in `getaddrinfo': getaddrinfo: Name or service not known (SocketError)
851 * ... 9 levels...
852 *
853 */
854static VALUE
855backtrace_limit(VALUE self)
856{
857 return LONG2NUM(rb_backtrace_length_limit);
858}
859
860VALUE
861rb_ec_backtrace_str_ary(const rb_execution_context_t *ec, long lev, long n)
862{
863 return rb_backtrace_to_str_ary(rb_ec_partial_backtrace_object(ec, lev, n, NULL, FALSE, FALSE));
864}
865
866VALUE
867rb_ec_backtrace_location_ary(const rb_execution_context_t *ec, long lev, long n, bool skip_internal)
868{
869 return rb_backtrace_to_location_ary(rb_ec_partial_backtrace_object(ec, lev, n, NULL, skip_internal, FALSE));
870}
871
872/* make old style backtrace directly */
873
874static void
875backtrace_each(const rb_execution_context_t *ec,
876 void (*init)(void *arg, size_t size),
877 void (*iter_iseq)(void *arg, const rb_control_frame_t *cfp),
878 void (*iter_cfunc)(void *arg, const rb_control_frame_t *cfp, ID mid),
879 void *arg)
880{
881 const rb_control_frame_t *last_cfp = ec->cfp;
882 const rb_control_frame_t *start_cfp = RUBY_VM_END_CONTROL_FRAME(ec);
883 const rb_control_frame_t *cfp;
884 ptrdiff_t size, i;
885
886 // In the case the thread vm_stack or cfp is not initialized, there is no backtrace.
887 if (start_cfp == NULL) {
888 init(arg, 0);
889 return;
890 }
891
892 /* <- start_cfp (end control frame)
893 * top frame (dummy)
894 * top frame (dummy)
895 * top frame <- start_cfp
896 * top frame
897 * ...
898 * 2nd frame <- lev:0
899 * current frame <- ec->cfp
900 */
901
902 start_cfp =
903 RUBY_VM_NEXT_CONTROL_FRAME(
904 RUBY_VM_NEXT_CONTROL_FRAME(start_cfp)); /* skip top frames */
905
906 if (start_cfp < last_cfp) {
907 size = 0;
908 }
909 else {
910 size = start_cfp - last_cfp + 1;
911 }
912
913 init(arg, size);
914
915 /* SDR(); */
916 for (i=0, cfp = start_cfp; i<size; i++, cfp = RUBY_VM_NEXT_CONTROL_FRAME(cfp)) {
917 /* fprintf(stderr, "cfp: %d\n", (rb_control_frame_t *)(ec->vm_stack + ec->vm_stack_size) - cfp); */
918 if (cfp->iseq) {
919 if (cfp->pc) {
920 iter_iseq(arg, cfp);
921 }
922 }
923 else {
924 VM_ASSERT(RUBYVM_CFUNC_FRAME_P(cfp));
925 const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(cfp);
926 ID mid = me->def->original_id;
927
928 iter_cfunc(arg, cfp, mid);
929 }
930 }
931}
932
933struct oldbt_arg {
934 VALUE filename;
935 int lineno;
936 void (*func)(void *data, VALUE file, int lineno, VALUE name);
937 void *data; /* result */
938};
939
940static void
941oldbt_init(void *ptr, size_t dmy)
942{
943 struct oldbt_arg *arg = (struct oldbt_arg *)ptr;
944 arg->filename = GET_VM()->progname;
945 arg->lineno = 0;
946}
947
948static void
949oldbt_iter_iseq(void *ptr, const rb_control_frame_t *cfp)
950{
951 const rb_iseq_t *iseq = cfp->iseq;
952 const VALUE *pc = cfp->pc;
953 struct oldbt_arg *arg = (struct oldbt_arg *)ptr;
954 VALUE file = arg->filename = rb_iseq_path(iseq);
955 VALUE name = ISEQ_BODY(iseq)->location.label;
956 int lineno = arg->lineno = calc_lineno(iseq, pc);
957
958 (arg->func)(arg->data, file, lineno, name);
959}
960
961static void
962oldbt_iter_cfunc(void *ptr, const rb_control_frame_t *cfp, ID mid)
963{
964 struct oldbt_arg *arg = (struct oldbt_arg *)ptr;
965 VALUE file = arg->filename;
966 VALUE name = rb_id2str(mid);
967 int lineno = arg->lineno;
968
969 (arg->func)(arg->data, file, lineno, name);
970}
971
972static void
973oldbt_print(void *data, VALUE file, int lineno, VALUE name)
974{
975 FILE *fp = (FILE *)data;
976
977 if (NIL_P(name)) {
978 fprintf(fp, "\tfrom %s:%d:in unknown method\n",
979 RSTRING_PTR(file), lineno);
980 }
981 else {
982 fprintf(fp, "\tfrom %s:%d:in `%s'\n",
983 RSTRING_PTR(file), lineno, RSTRING_PTR(name));
984 }
985}
986
987static void
988vm_backtrace_print(FILE *fp)
989{
990 struct oldbt_arg arg;
991
992 arg.func = oldbt_print;
993 arg.data = (void *)fp;
994 backtrace_each(GET_EC(),
995 oldbt_init,
996 oldbt_iter_iseq,
997 oldbt_iter_cfunc,
998 &arg);
999}
1000
1001static void
1002oldbt_bugreport(void *arg, VALUE file, int line, VALUE method)
1003{
1004 const char *filename = NIL_P(file) ? "ruby" : RSTRING_PTR(file);
1005 if (!*(int *)arg) {
1006 fprintf(stderr, "-- Ruby level backtrace information "
1007 "----------------------------------------\n");
1008 *(int *)arg = 1;
1009 }
1010 if (NIL_P(method)) {
1011 fprintf(stderr, "%s:%d:in unknown method\n", filename, line);
1012 }
1013 else {
1014 fprintf(stderr, "%s:%d:in `%s'\n", filename, line, RSTRING_PTR(method));
1015 }
1016}
1017
1018void
1019rb_backtrace_print_as_bugreport(void)
1020{
1021 struct oldbt_arg arg;
1022 int i = 0;
1023
1024 arg.func = oldbt_bugreport;
1025 arg.data = (int *)&i;
1026
1027 backtrace_each(GET_EC(),
1028 oldbt_init,
1029 oldbt_iter_iseq,
1030 oldbt_iter_cfunc,
1031 &arg);
1032}
1033
1034void
1036{
1037 vm_backtrace_print(stderr);
1038}
1039
1041 VALUE (*iter)(VALUE recv, VALUE str);
1042 VALUE output;
1043};
1044
1045static void
1046oldbt_print_to(void *data, VALUE file, int lineno, VALUE name)
1047{
1048 const struct print_to_arg *arg = data;
1049 VALUE str = rb_sprintf("\tfrom %"PRIsVALUE":%d:in ", file, lineno);
1050
1051 if (NIL_P(name)) {
1052 rb_str_cat2(str, "unknown method\n");
1053 }
1054 else {
1055 rb_str_catf(str, " `%"PRIsVALUE"'\n", name);
1056 }
1057 (*arg->iter)(arg->output, str);
1058}
1059
1060void
1061rb_backtrace_each(VALUE (*iter)(VALUE recv, VALUE str), VALUE output)
1062{
1063 struct oldbt_arg arg;
1064 struct print_to_arg parg;
1065
1066 parg.iter = iter;
1067 parg.output = output;
1068 arg.func = oldbt_print_to;
1069 arg.data = &parg;
1070 backtrace_each(GET_EC(),
1071 oldbt_init,
1072 oldbt_iter_iseq,
1073 oldbt_iter_cfunc,
1074 &arg);
1075}
1076
1077VALUE
1079{
1080 return rb_ec_backtrace_str_ary(GET_EC(), BACKTRACE_START, ALL_BACKTRACE_LINES);
1081}
1082
1083static VALUE
1084ec_backtrace_to_ary(const rb_execution_context_t *ec, int argc, const VALUE *argv, int lev_default, int lev_plus, int to_str)
1085{
1086 VALUE level, vn;
1087 long lev, n;
1088 VALUE btval;
1089 VALUE r;
1090 int too_large;
1091
1092 rb_scan_args(argc, argv, "02", &level, &vn);
1093
1094 if (argc == 2 && NIL_P(vn)) argc--;
1095
1096 switch (argc) {
1097 case 0:
1098 lev = lev_default + lev_plus;
1099 n = ALL_BACKTRACE_LINES;
1100 break;
1101 case 1:
1102 {
1103 long beg, len, bt_size = backtrace_size(ec);
1104 switch (rb_range_beg_len(level, &beg, &len, bt_size - lev_plus, 0)) {
1105 case Qfalse:
1106 lev = NUM2LONG(level);
1107 if (lev < 0) {
1108 rb_raise(rb_eArgError, "negative level (%ld)", lev);
1109 }
1110 lev += lev_plus;
1111 n = ALL_BACKTRACE_LINES;
1112 break;
1113 case Qnil:
1114 return Qnil;
1115 default:
1116 lev = beg + lev_plus;
1117 n = len;
1118 break;
1119 }
1120 break;
1121 }
1122 case 2:
1123 lev = NUM2LONG(level);
1124 n = NUM2LONG(vn);
1125 if (lev < 0) {
1126 rb_raise(rb_eArgError, "negative level (%ld)", lev);
1127 }
1128 if (n < 0) {
1129 rb_raise(rb_eArgError, "negative size (%ld)", n);
1130 }
1131 lev += lev_plus;
1132 break;
1133 default:
1134 lev = n = 0; /* to avoid warning */
1135 break;
1136 }
1137
1138 if (n == 0) {
1139 return rb_ary_new();
1140 }
1141
1142 btval = rb_ec_partial_backtrace_object(ec, lev, n, &too_large, FALSE, FALSE);
1143
1144 if (too_large) {
1145 return Qnil;
1146 }
1147
1148 if (to_str) {
1149 r = backtrace_to_str_ary(btval);
1150 }
1151 else {
1152 r = backtrace_to_location_ary(btval);
1153 }
1154 RB_GC_GUARD(btval);
1155 return r;
1156}
1157
1158static VALUE
1159thread_backtrace_to_ary(int argc, const VALUE *argv, VALUE thval, int to_str)
1160{
1161 rb_thread_t *target_th = rb_thread_ptr(thval);
1162
1163 if (target_th->to_kill || target_th->status == THREAD_KILLED)
1164 return Qnil;
1165
1166 return ec_backtrace_to_ary(target_th->ec, argc, argv, 0, 0, to_str);
1167}
1168
1169VALUE
1170rb_vm_thread_backtrace(int argc, const VALUE *argv, VALUE thval)
1171{
1172 return thread_backtrace_to_ary(argc, argv, thval, 1);
1173}
1174
1175VALUE
1176rb_vm_thread_backtrace_locations(int argc, const VALUE *argv, VALUE thval)
1177{
1178 return thread_backtrace_to_ary(argc, argv, thval, 0);
1179}
1180
1181VALUE
1182rb_vm_backtrace(int argc, const VALUE * argv, struct rb_execution_context_struct * ec)
1183{
1184 return ec_backtrace_to_ary(ec, argc, argv, 0, 0, 1);
1185}
1186
1187VALUE
1188rb_vm_backtrace_locations(int argc, const VALUE * argv, struct rb_execution_context_struct * ec)
1189{
1190 return ec_backtrace_to_ary(ec, argc, argv, 0, 0, 0);
1191}
1192
1193/*
1194 * call-seq:
1195 * caller(start=1, length=nil) -> array or nil
1196 * caller(range) -> array or nil
1197 *
1198 * Returns the current execution stack---an array containing strings in
1199 * the form <code>file:line</code> or <code>file:line: in
1200 * `method'</code>.
1201 *
1202 * The optional _start_ parameter determines the number of initial stack
1203 * entries to omit from the top of the stack.
1204 *
1205 * A second optional +length+ parameter can be used to limit how many entries
1206 * are returned from the stack.
1207 *
1208 * Returns +nil+ if _start_ is greater than the size of
1209 * current execution stack.
1210 *
1211 * Optionally you can pass a range, which will return an array containing the
1212 * entries within the specified range.
1213 *
1214 * def a(skip)
1215 * caller(skip)
1216 * end
1217 * def b(skip)
1218 * a(skip)
1219 * end
1220 * def c(skip)
1221 * b(skip)
1222 * end
1223 * c(0) #=> ["prog:2:in `a'", "prog:5:in `b'", "prog:8:in `c'", "prog:10:in `<main>'"]
1224 * c(1) #=> ["prog:5:in `b'", "prog:8:in `c'", "prog:11:in `<main>'"]
1225 * c(2) #=> ["prog:8:in `c'", "prog:12:in `<main>'"]
1226 * c(3) #=> ["prog:13:in `<main>'"]
1227 * c(4) #=> []
1228 * c(5) #=> nil
1229 */
1230
1231static VALUE
1232rb_f_caller(int argc, VALUE *argv, VALUE _)
1233{
1234 return ec_backtrace_to_ary(GET_EC(), argc, argv, 1, 1, 1);
1235}
1236
1237/*
1238 * call-seq:
1239 * caller_locations(start=1, length=nil) -> array or nil
1240 * caller_locations(range) -> array or nil
1241 *
1242 * Returns the current execution stack---an array containing
1243 * backtrace location objects.
1244 *
1245 * See Thread::Backtrace::Location for more information.
1246 *
1247 * The optional _start_ parameter determines the number of initial stack
1248 * entries to omit from the top of the stack.
1249 *
1250 * A second optional +length+ parameter can be used to limit how many entries
1251 * are returned from the stack.
1252 *
1253 * Returns +nil+ if _start_ is greater than the size of
1254 * current execution stack.
1255 *
1256 * Optionally you can pass a range, which will return an array containing the
1257 * entries within the specified range.
1258 */
1259static VALUE
1260rb_f_caller_locations(int argc, VALUE *argv, VALUE _)
1261{
1262 return ec_backtrace_to_ary(GET_EC(), argc, argv, 1, 1, 0);
1263}
1264
1265/*
1266 * call-seq:
1267 * Thread.each_caller_location{ |loc| ... } -> nil
1268 *
1269 * Yields each frame of the current execution stack as a
1270 * backtrace location object.
1271 */
1272static VALUE
1273each_caller_location(VALUE unused)
1274{
1275 rb_ec_partial_backtrace_object(GET_EC(), 2, ALL_BACKTRACE_LINES, NULL, FALSE, TRUE);
1276 return Qnil;
1277}
1278
1279/* called from Init_vm() in vm.c */
1280void
1281Init_vm_backtrace(void)
1282{
1283 /*
1284 * An internal representation of the backtrace. The user will never interact with
1285 * objects of this class directly, but class methods can be used to get backtrace
1286 * settings of the current session.
1287 */
1288 rb_cBacktrace = rb_define_class_under(rb_cThread, "Backtrace", rb_cObject);
1289 rb_define_alloc_func(rb_cBacktrace, backtrace_alloc);
1290 rb_undef_method(CLASS_OF(rb_cBacktrace), "new");
1291 rb_marshal_define_compat(rb_cBacktrace, rb_cArray, backtrace_dump_data, backtrace_load_data);
1292 rb_define_singleton_method(rb_cBacktrace, "limit", backtrace_limit, 0);
1293
1294 /*
1295 * An object representation of a stack frame, initialized by
1296 * Kernel#caller_locations.
1297 *
1298 * For example:
1299 *
1300 * # caller_locations.rb
1301 * def a(skip)
1302 * caller_locations(skip)
1303 * end
1304 * def b(skip)
1305 * a(skip)
1306 * end
1307 * def c(skip)
1308 * b(skip)
1309 * end
1310 *
1311 * c(0..2).map do |call|
1312 * puts call.to_s
1313 * end
1314 *
1315 * Running <code>ruby caller_locations.rb</code> will produce:
1316 *
1317 * caller_locations.rb:2:in `a'
1318 * caller_locations.rb:5:in `b'
1319 * caller_locations.rb:8:in `c'
1320 *
1321 * Here's another example with a slightly different result:
1322 *
1323 * # foo.rb
1324 * class Foo
1325 * attr_accessor :locations
1326 * def initialize(skip)
1327 * @locations = caller_locations(skip)
1328 * end
1329 * end
1330 *
1331 * Foo.new(0..2).locations.map do |call|
1332 * puts call.to_s
1333 * end
1334 *
1335 * Now run <code>ruby foo.rb</code> and you should see:
1336 *
1337 * init.rb:4:in `initialize'
1338 * init.rb:8:in `new'
1339 * init.rb:8:in `<main>'
1340 */
1341 rb_cBacktraceLocation = rb_define_class_under(rb_cBacktrace, "Location", rb_cObject);
1342 rb_undef_alloc_func(rb_cBacktraceLocation);
1343 rb_undef_method(CLASS_OF(rb_cBacktraceLocation), "new");
1344 rb_define_method(rb_cBacktraceLocation, "lineno", location_lineno_m, 0);
1345 rb_define_method(rb_cBacktraceLocation, "label", location_label_m, 0);
1346 rb_define_method(rb_cBacktraceLocation, "base_label", location_base_label_m, 0);
1347 rb_define_method(rb_cBacktraceLocation, "path", location_path_m, 0);
1348 rb_define_method(rb_cBacktraceLocation, "absolute_path", location_absolute_path_m, 0);
1349 rb_define_method(rb_cBacktraceLocation, "to_s", location_to_str_m, 0);
1350 rb_define_method(rb_cBacktraceLocation, "inspect", location_inspect_m, 0);
1351
1352 rb_define_global_function("caller", rb_f_caller, -1);
1353 rb_define_global_function("caller_locations", rb_f_caller_locations, -1);
1354
1355 rb_define_singleton_method(rb_cThread, "each_caller_location", each_caller_location, 0);
1356}
1357
1358/* debugger API */
1359
1360RUBY_SYMBOL_EXPORT_BEGIN
1361
1362RUBY_SYMBOL_EXPORT_END
1363
1366 rb_control_frame_t *cfp;
1367 VALUE backtrace;
1368 VALUE contexts; /* [[klass, binding, iseq, cfp], ...] */
1369 long backtrace_size;
1370};
1371
1372enum {
1373 CALLER_BINDING_SELF,
1374 CALLER_BINDING_CLASS,
1375 CALLER_BINDING_BINDING,
1376 CALLER_BINDING_ISEQ,
1377 CALLER_BINDING_CFP,
1378 CALLER_BINDING_DEPTH,
1379};
1380
1382 VALUE ary;
1383 const rb_execution_context_t *ec;
1384};
1385
1386static void
1387collect_caller_bindings_init(void *arg, size_t size)
1388{
1389 /* */
1390}
1391
1392static VALUE
1393get_klass(const rb_control_frame_t *cfp)
1394{
1395 VALUE klass;
1396 if (rb_vm_control_frame_id_and_class(cfp, 0, 0, &klass)) {
1397 if (RB_TYPE_P(klass, T_ICLASS)) {
1398 return RBASIC(klass)->klass;
1399 }
1400 else {
1401 return klass;
1402 }
1403 }
1404 else {
1405 return Qnil;
1406 }
1407}
1408
1409static int
1410frame_depth(const rb_execution_context_t *ec, const rb_control_frame_t *cfp)
1411{
1412 VM_ASSERT(RUBY_VM_END_CONTROL_FRAME(ec) >= cfp);
1413 return (int)(RUBY_VM_END_CONTROL_FRAME(ec) - cfp);
1414}
1415
1416static void
1417collect_caller_bindings_iseq(void *arg, const rb_control_frame_t *cfp)
1418{
1420 VALUE frame = rb_ary_new2(6);
1421
1422 rb_ary_store(frame, CALLER_BINDING_SELF, cfp->self);
1423 rb_ary_store(frame, CALLER_BINDING_CLASS, get_klass(cfp));
1424 rb_ary_store(frame, CALLER_BINDING_BINDING, GC_GUARDED_PTR(cfp)); /* create later */
1425 rb_ary_store(frame, CALLER_BINDING_ISEQ, cfp->iseq ? (VALUE)cfp->iseq : Qnil);
1426 rb_ary_store(frame, CALLER_BINDING_CFP, GC_GUARDED_PTR(cfp));
1427 rb_ary_store(frame, CALLER_BINDING_DEPTH, INT2FIX(frame_depth(data->ec, cfp)));
1428
1429 rb_ary_push(data->ary, frame);
1430}
1431
1432static void
1433collect_caller_bindings_cfunc(void *arg, const rb_control_frame_t *cfp, ID mid)
1434{
1436 VALUE frame = rb_ary_new2(6);
1437
1438 rb_ary_store(frame, CALLER_BINDING_SELF, cfp->self);
1439 rb_ary_store(frame, CALLER_BINDING_CLASS, get_klass(cfp));
1440 rb_ary_store(frame, CALLER_BINDING_BINDING, Qnil); /* not available */
1441 rb_ary_store(frame, CALLER_BINDING_ISEQ, Qnil); /* not available */
1442 rb_ary_store(frame, CALLER_BINDING_CFP, GC_GUARDED_PTR(cfp));
1443 rb_ary_store(frame, CALLER_BINDING_DEPTH, INT2FIX(frame_depth(data->ec, cfp)));
1444
1445 rb_ary_push(data->ary, frame);
1446}
1447
1448static VALUE
1449collect_caller_bindings(const rb_execution_context_t *ec)
1450{
1451 int i;
1452 VALUE result;
1453 struct collect_caller_bindings_data data = {
1454 rb_ary_new(), ec
1455 };
1456
1457 backtrace_each(ec,
1458 collect_caller_bindings_init,
1459 collect_caller_bindings_iseq,
1460 collect_caller_bindings_cfunc,
1461 &data);
1462
1463 result = rb_ary_reverse(data.ary);
1464
1465 /* bindings should be created from top of frame */
1466 for (i=0; i<RARRAY_LEN(result); i++) {
1467 VALUE entry = rb_ary_entry(result, i);
1468 VALUE cfp_val = rb_ary_entry(entry, CALLER_BINDING_BINDING);
1469
1470 if (!NIL_P(cfp_val)) {
1471 rb_control_frame_t *cfp = GC_GUARDED_PTR_REF(cfp_val);
1472 rb_ary_store(entry, CALLER_BINDING_BINDING, rb_vm_make_binding(ec, cfp));
1473 }
1474 }
1475
1476 return result;
1477}
1478
1479/*
1480 * Note that the passed `rb_debug_inspector_t' will be disabled
1481 * after `rb_debug_inspector_open'.
1482 */
1483
1484VALUE
1486{
1487 rb_debug_inspector_t dbg_context;
1488 rb_execution_context_t *ec = GET_EC();
1489 enum ruby_tag_type state;
1490 volatile VALUE MAYBE_UNUSED(result);
1491
1492 /* escape all env to heap */
1493 rb_vm_stack_to_heap(ec);
1494
1495 dbg_context.ec = ec;
1496 dbg_context.cfp = dbg_context.ec->cfp;
1497 dbg_context.backtrace = rb_ec_backtrace_location_ary(ec, BACKTRACE_START, ALL_BACKTRACE_LINES, FALSE);
1498 dbg_context.backtrace_size = RARRAY_LEN(dbg_context.backtrace);
1499 dbg_context.contexts = collect_caller_bindings(ec);
1500
1501 EC_PUSH_TAG(ec);
1502 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1503 result = (*func)(&dbg_context, data);
1504 }
1505 EC_POP_TAG();
1506
1507 /* invalidate bindings? */
1508
1509 if (state) {
1510 EC_JUMP_TAG(ec, state);
1511 }
1512
1513 return result;
1514}
1515
1516static VALUE
1517frame_get(const rb_debug_inspector_t *dc, long index)
1518{
1519 if (index < 0 || index >= dc->backtrace_size) {
1520 rb_raise(rb_eArgError, "no such frame");
1521 }
1522 return rb_ary_entry(dc->contexts, index);
1523}
1524
1525VALUE
1527{
1528 VALUE frame = frame_get(dc, index);
1529 return rb_ary_entry(frame, CALLER_BINDING_SELF);
1530}
1531
1532VALUE
1534{
1535 VALUE frame = frame_get(dc, index);
1536 return rb_ary_entry(frame, CALLER_BINDING_CLASS);
1537}
1538
1539VALUE
1541{
1542 VALUE frame = frame_get(dc, index);
1543 return rb_ary_entry(frame, CALLER_BINDING_BINDING);
1544}
1545
1546VALUE
1548{
1549 VALUE frame = frame_get(dc, index);
1550 VALUE iseq = rb_ary_entry(frame, CALLER_BINDING_ISEQ);
1551
1552 return RTEST(iseq) ? rb_iseqw_new((rb_iseq_t *)iseq) : Qnil;
1553}
1554
1555VALUE
1557{
1558 VALUE frame = frame_get(dc, index);
1559 return rb_ary_entry(frame, CALLER_BINDING_DEPTH);
1560}
1561
1562VALUE
1564{
1565 rb_execution_context_t *ec = GET_EC();
1566 return INT2FIX(frame_depth(ec, ec->cfp));
1567}
1568
1569VALUE
1571{
1572 return dc->backtrace;
1573}
1574
1575int
1576rb_profile_frames(int start, int limit, VALUE *buff, int *lines)
1577{
1578 int i;
1579 const rb_execution_context_t *ec = GET_EC();
1580 const rb_control_frame_t *cfp = ec->cfp, *end_cfp = RUBY_VM_END_CONTROL_FRAME(ec);
1581 const rb_callable_method_entry_t *cme;
1582
1583 // If this function is called inside a thread after thread creation, but
1584 // before the CFP has been created, just return 0. This can happen when
1585 // sampling via signals. Threads can be interrupted randomly by the
1586 // signal, including during the time after the thread has been created, but
1587 // before the CFP has been allocated
1588 if (!cfp) {
1589 return 0;
1590 }
1591
1592 // Skip dummy frame; see `rb_ec_partial_backtrace_object` for details
1593 end_cfp = RUBY_VM_NEXT_CONTROL_FRAME(end_cfp);
1594
1595 for (i=0; i<limit && cfp != end_cfp;) {
1596 if (VM_FRAME_RUBYFRAME_P(cfp) && cfp->pc != 0) {
1597 if (start > 0) {
1598 start--;
1599 continue;
1600 }
1601
1602 /* record frame info */
1603 cme = rb_vm_frame_method_entry(cfp);
1604 if (cme && cme->def->type == VM_METHOD_TYPE_ISEQ) {
1605 buff[i] = (VALUE)cme;
1606 }
1607 else {
1608 buff[i] = (VALUE)cfp->iseq;
1609 }
1610
1611 if (lines) lines[i] = calc_lineno(cfp->iseq, cfp->pc);
1612
1613 i++;
1614 }
1615 else {
1616 cme = rb_vm_frame_method_entry(cfp);
1617 if (cme && cme->def->type == VM_METHOD_TYPE_CFUNC) {
1618 buff[i] = (VALUE)cme;
1619 if (lines) lines[i] = 0;
1620 i++;
1621 }
1622 }
1623 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1624 }
1625
1626 return i;
1627}
1628
1629static const rb_iseq_t *
1630frame2iseq(VALUE frame)
1631{
1632 if (NIL_P(frame)) return NULL;
1633
1634 if (RB_TYPE_P(frame, T_IMEMO)) {
1635 switch (imemo_type(frame)) {
1636 case imemo_iseq:
1637 return (const rb_iseq_t *)frame;
1638 case imemo_ment:
1639 {
1641 switch (cme->def->type) {
1642 case VM_METHOD_TYPE_ISEQ:
1643 return cme->def->body.iseq.iseqptr;
1644 default:
1645 return NULL;
1646 }
1647 }
1648 default:
1649 break;
1650 }
1651 }
1652 rb_bug("frame2iseq: unreachable");
1653}
1654
1655VALUE
1657{
1658 const rb_iseq_t *iseq = frame2iseq(frame);
1659 return iseq ? rb_iseq_path(iseq) : Qnil;
1660}
1661
1662static const rb_callable_method_entry_t *
1663cframe(VALUE frame)
1664{
1665 if (NIL_P(frame)) return NULL;
1666
1667 if (RB_TYPE_P(frame, T_IMEMO)) {
1668 switch (imemo_type(frame)) {
1669 case imemo_ment:
1670 {
1672 switch (cme->def->type) {
1673 case VM_METHOD_TYPE_CFUNC:
1674 return cme;
1675 default:
1676 return NULL;
1677 }
1678 }
1679 default:
1680 return NULL;
1681 }
1682 }
1683
1684 return NULL;
1685}
1686
1687VALUE
1689{
1690 if (cframe(frame)) {
1691 static VALUE cfunc_str = Qfalse;
1692 if (!cfunc_str) {
1693 cfunc_str = rb_str_new_literal("<cfunc>");
1694 rb_gc_register_mark_object(cfunc_str);
1695 }
1696 return cfunc_str;
1697 }
1698 const rb_iseq_t *iseq = frame2iseq(frame);
1699 return iseq ? rb_iseq_realpath(iseq) : Qnil;
1700}
1701
1702VALUE
1704{
1705 const rb_iseq_t *iseq = frame2iseq(frame);
1706 return iseq ? rb_iseq_label(iseq) : Qnil;
1707}
1708
1709VALUE
1711{
1712 const rb_iseq_t *iseq = frame2iseq(frame);
1713 return iseq ? rb_iseq_base_label(iseq) : Qnil;
1714}
1715
1716VALUE
1718{
1719 const rb_iseq_t *iseq = frame2iseq(frame);
1720 return iseq ? rb_iseq_first_lineno(iseq) : Qnil;
1721}
1722
1723static VALUE
1724frame2klass(VALUE frame)
1725{
1726 if (NIL_P(frame)) return Qnil;
1727
1728 if (RB_TYPE_P(frame, T_IMEMO)) {
1730
1731 if (imemo_type(frame) == imemo_ment) {
1732 return cme->defined_class;
1733 }
1734 }
1735 return Qnil;
1736}
1737
1738VALUE
1740{
1741 VALUE klass = frame2klass(frame);
1742
1743 if (klass && !NIL_P(klass)) {
1744 if (RB_TYPE_P(klass, T_ICLASS)) {
1745 klass = RBASIC(klass)->klass;
1746 }
1747 else if (FL_TEST(klass, FL_SINGLETON)) {
1748 klass = rb_ivar_get(klass, id__attached__);
1749 if (!RB_TYPE_P(klass, T_CLASS) && !RB_TYPE_P(klass, T_MODULE))
1750 return rb_sprintf("#<%s:%p>", rb_class2name(rb_obj_class(klass)), (void*)klass);
1751 }
1752 return rb_class_path(klass);
1753 }
1754 else {
1755 return Qnil;
1756 }
1757}
1758
1759VALUE
1761{
1762 VALUE klass = frame2klass(frame);
1763
1764 return RBOOL(klass && !NIL_P(klass) && FL_TEST(klass, FL_SINGLETON));
1765}
1766
1767VALUE
1769{
1770 const rb_callable_method_entry_t *cme = cframe(frame);
1771 if (cme) {
1772 ID mid = cme->def->original_id;
1773 return id2str(mid);
1774 }
1775 const rb_iseq_t *iseq = frame2iseq(frame);
1776 return iseq ? rb_iseq_method_name(iseq) : Qnil;
1777}
1778
1779static VALUE
1780qualified_method_name(VALUE frame, VALUE method_name)
1781{
1782 if (method_name != Qnil) {
1783 VALUE classpath = rb_profile_frame_classpath(frame);
1784 VALUE singleton_p = rb_profile_frame_singleton_method_p(frame);
1785
1786 if (classpath != Qnil) {
1787 return rb_sprintf("%"PRIsVALUE"%s%"PRIsVALUE,
1788 classpath, singleton_p == Qtrue ? "." : "#", method_name);
1789 }
1790 else {
1791 return method_name;
1792 }
1793 }
1794 else {
1795 return Qnil;
1796 }
1797}
1798
1799VALUE
1801{
1802 VALUE method_name = rb_profile_frame_method_name(frame);
1803
1804 return qualified_method_name(frame, method_name);
1805}
1806
1807VALUE
1809{
1810 const rb_callable_method_entry_t *cme = cframe(frame);
1811 if (cme) {
1812 ID mid = cme->def->original_id;
1813 VALUE method_name = id2str(mid);
1814 return qualified_method_name(frame, method_name);
1815 }
1816
1817 VALUE label = rb_profile_frame_label(frame);
1818 VALUE base_label = rb_profile_frame_base_label(frame);
1819 VALUE qualified_method_name = rb_profile_frame_qualified_method_name(frame);
1820
1821 if (NIL_P(qualified_method_name) || base_label == qualified_method_name) {
1822 return label;
1823 }
1824 else {
1825 long label_length = RSTRING_LEN(label);
1826 long base_label_length = RSTRING_LEN(base_label);
1827 int prefix_len = rb_long2int(label_length - base_label_length);
1828
1829 return rb_sprintf("%.*s%"PRIsVALUE, prefix_len, RSTRING_PTR(label), qualified_method_name);
1830 }
1831}
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
Definition: cxxanyargs.hpp:670
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
Definition: cxxanyargs.hpp:685
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
Definition: cxxanyargs.hpp:695
int rb_profile_frames(int start, int limit, VALUE *buff, int *lines)
Queries mysterious "frame"s of the given range.
VALUE rb_profile_frame_full_label(VALUE frame)
Identical to rb_profile_frame_label(), except it returns a qualified result.
VALUE rb_debug_inspector_frame_iseq_get(const rb_debug_inspector_t *dc, long index)
Queries the instruction sequence of the passed context's upper frame.
VALUE rb_debug_inspector_current_depth(void)
Return current frmae depth.
VALUE rb_profile_frame_method_name(VALUE frame)
Queries the name of the method of the passed frame.
VALUE rb_debug_inspector_frame_depth(const rb_debug_inspector_t *dc, long index)
Queries the depth of the passed context's upper frame.
VALUE rb_profile_frame_qualified_method_name(VALUE frame)
Identical to rb_profile_frame_method_name(), except it "qualifies" the return value with its defining...
VALUE rb_profile_frame_label(VALUE frame)
Queries human-readable "label" string.
VALUE rb_profile_frame_singleton_method_p(VALUE frame)
Queries if the method of the passed frame is a singleton class.
VALUE rb_debug_inspector_backtrace_locations(const rb_debug_inspector_t *dc)
Queries the backtrace object of the context.
VALUE rb_profile_frame_absolute_path(VALUE frame)
Identical to rb_profile_frame_path(), except it tries to expand the returning path.
VALUE rb_debug_inspector_open(rb_debug_inspector_func_t func, void *data)
Prepares, executes, then cleans up a debug session.
VALUE rb_debug_inspector_frame_self_get(const rb_debug_inspector_t *dc, long index)
Queries the current receiver of the passed context's upper frame.
VALUE rb_debug_inspector_frame_binding_get(const rb_debug_inspector_t *dc, long index)
Queries the binding of the passed context's upper frame.
VALUE rb_debug_inspector_frame_class_get(const rb_debug_inspector_t *dc, long index)
Queries the current class of the passed context's upper frame.
VALUE rb_profile_frame_classpath(VALUE frame)
Queries the class path of the method that the passed frame represents.
VALUE rb_profile_frame_path(VALUE frame)
Queries the path of the passed backtrace.
VALUE rb_profile_frame_first_lineno(VALUE frame)
Queries the first line of the method of the passed frame pointer.
VALUE(* rb_debug_inspector_func_t)(const rb_debug_inspector_t *dc, void *data)
Type of the callback function passed to rb_debug_inspector_open().
Definition: debug.h:198
VALUE rb_profile_frame_base_label(VALUE frame)
Identical to rb_profile_frame_label(), except it does not "qualify" the result.
VALUE rb_enc_sprintf(rb_encoding *enc, const char *fmt,...)
Identical to rb_sprintf(), except it additionally takes an encoding.
Definition: sprintf.c:1200
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:920
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
Definition: class.c:2073
#define FL_SINGLETON
Old name of RUBY_FL_SINGLETON.
Definition: fl_type.h:58
#define INT2FIX
Old name of RB_INT2FIX.
Definition: long.h:48
#define rb_str_cat2
Old name of rb_str_cat_cstr.
Definition: string.h:1683
#define UNREACHABLE
Old name of RBIMPL_UNREACHABLE.
Definition: assume.h:28
#define T_IMEMO
Old name of RUBY_T_IMEMO.
Definition: value_type.h:67
#define CLASS_OF
Old name of rb_class_of.
Definition: globals.h:203
#define T_MODULE
Old name of RUBY_T_MODULE.
Definition: value_type.h:70
#define ZALLOC_N
Old name of RB_ZALLOC_N.
Definition: memory.h:395
#define ASSUME
Old name of RBIMPL_ASSUME.
Definition: assume.h:27
#define T_ICLASS
Old name of RUBY_T_ICLASS.
Definition: value_type.h:66
#define LONG2NUM
Old name of RB_LONG2NUM.
Definition: long.h:50
#define Qtrue
Old name of RUBY_Qtrue.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define NIL_P
Old name of RB_NIL_P.
#define T_CLASS
Old name of RUBY_T_CLASS.
Definition: value_type.h:58
#define FL_TEST
Old name of RB_FL_TEST.
Definition: fl_type.h:139
#define NUM2LONG
Old name of RB_NUM2LONG.
Definition: long.h:51
#define rb_ary_new2
Old name of rb_ary_new_capa.
Definition: array.h:651
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
Definition: error.c:3148
int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
Checks if the given object is of given kind.
Definition: error.c:1041
void rb_bug(const char *fmt,...)
Interpreter panic switch.
Definition: error.c:794
VALUE rb_eArgError
ArgumentError exception.
Definition: error.c:1092
VALUE rb_cArray
Array class.
Definition: array.c:40
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition: object.c:190
VALUE rb_cThread
Thread class.
Definition: vm.c:466
Encoding relates APIs.
VALUE rb_range_beg_len(VALUE range, long *begp, long *lenp, long len, int err)
Deconstructs a numerical range.
Definition: range.c:1578
#define rb_str_new_literal(str)
Just another name of rb_str_new_lit.
Definition: string.h:1750
VALUE rb_str_inspect(VALUE str)
Generates a "readable" version of the receiver.
Definition: string.c:6677
#define rb_str_cat_cstr(buf, str)
Identical to rb_str_cat(), except it assumes the passed pointer is a pointer to a C string.
Definition: string.h:1656
VALUE rb_ivar_get(VALUE obj, ID name)
Identical to rb_iv_get(), except it accepts the name as an ID instead of a C string.
Definition: variable.c:1215
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
Definition: variable.c:185
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
Definition: vm_method.c:1159
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
VALUE rb_make_backtrace(void)
Creates the good old fashioned array-of-strings style backtrace info.
void rb_backtrace(void)
Prints the backtrace out to the standard error.
VALUE rb_sprintf(const char *fmt,...)
Ruby's extended sprintf(3).
Definition: sprintf.c:1219
VALUE rb_str_catf(VALUE dst, const char *fmt,...)
Identical to rb_sprintf(), except it renders the output to the specified object rather than creating ...
Definition: sprintf.c:1242
VALUE rb_yield(VALUE val)
Yields the block.
Definition: vm_eval.c:1358
#define rb_long2int
Just another name of rb_long2int_inline.
Definition: long.h:62
void rb_marshal_define_compat(VALUE newclass, VALUE oldclass, VALUE(*dumper)(VALUE), VALUE(*loader)(VALUE, VALUE))
Marshal format compatibility layer.
Definition: marshal.c:150
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition: memory.h:161
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition: cxxanyargs.hpp:56
#define RARRAY_LEN
Just another name of rb_array_len.
Definition: rarray.h:68
#define RBASIC(obj)
Convenient casting macro.
Definition: rbasic.h:40
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
Definition: rtypeddata.h:79
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
Definition: rtypeddata.h:489
const char * rb_class2name(VALUE klass)
Queries the name of the passed class.
Definition: variable.c:313
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
Definition: stdarg.h:35
Definition: method.h:62
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
Definition: method.h:134
uintptr_t VALUE
Type that represents a Ruby object.
Definition: value.h:40
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition: value.h:52