Ruby: vm_backtrace.c Source File

Ruby 4.0.5p0 (2026-05-20 revision 64336ffd0ee9e1f4c05891695a3d7b49cb709721)

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