Ruby 3.5.0dev (2025-02-22 revision 412997300569c1853c09813e4924b6df3d7e8669)
vm_backtrace.c (412997300569c1853c09813e4924b6df3d7e8669)
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 = -1;
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
121 const rb_iseq_t *iseq;
122 const VALUE *pc;
124
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,
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
199VALUE rb_mod_name0(VALUE klass, bool *permanent);
200
201VALUE
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
265// Return true if a given location is a C method or supposed to behave like one.
266static inline bool
267location_cfunc_p(rb_backtrace_location_t *loc)
268{
269 if (!loc->cme) return false;
270
271 switch (loc->cme->def->type) {
272 case VM_METHOD_TYPE_CFUNC:
273 return true;
274 case VM_METHOD_TYPE_ISEQ:
275 return rb_iseq_attr_p(loc->cme->def->body.iseq.iseqptr, BUILTIN_ATTR_C_TRACE);
276 default:
277 return false;
278 }
279}
280
281static VALUE
282location_label(rb_backtrace_location_t *loc)
283{
284 if (location_cfunc_p(loc)) {
285 return rb_gen_method_name(loc->cme->owner, rb_id2str(loc->cme->def->original_id));
286 }
287 else {
288 VALUE owner = Qnil;
289 if (loc->cme) {
290 owner = loc->cme->owner;
291 }
292 return calculate_iseq_label(owner, loc->iseq);
293 }
294}
295/*
296 * Returns the label of this frame.
297 *
298 * Usually consists of method, class, module, etc names with decoration.
299 *
300 * Consider the following example:
301 *
302 * def foo
303 * puts caller_locations(0).first.label
304 *
305 * 1.times do
306 * puts caller_locations(0).first.label
307 *
308 * 1.times do
309 * puts caller_locations(0).first.label
310 * end
311 * end
312 * end
313 *
314 * The result of calling +foo+ is this:
315 *
316 * foo
317 * block in foo
318 * block (2 levels) in foo
319 *
320 */
321static VALUE
322location_label_m(VALUE self)
323{
324 return location_label(location_ptr(self));
325}
326
327static VALUE
328location_base_label(rb_backtrace_location_t *loc)
329{
330 if (location_cfunc_p(loc)) {
331 return rb_id2str(loc->cme->def->original_id);
332 }
333
334 return ISEQ_BODY(loc->iseq)->location.base_label;
335}
336
337/*
338 * Returns the base label of this frame, which is usually equal to the label,
339 * without decoration.
340 *
341 * Consider the following example:
342 *
343 * def foo
344 * puts caller_locations(0).first.base_label
345 *
346 * 1.times do
347 * puts caller_locations(0).first.base_label
348 *
349 * 1.times do
350 * puts caller_locations(0).first.base_label
351 * end
352 * end
353 * end
354 *
355 * The result of calling +foo+ is this:
356 *
357 * foo
358 * foo
359 * foo
360 */
361static VALUE
362location_base_label_m(VALUE self)
363{
364 return location_base_label(location_ptr(self));
365}
366
367static const rb_iseq_t *
368location_iseq(rb_backtrace_location_t *loc)
369{
370 return loc->iseq;
371}
372
373/*
374 * Returns the file name of this frame. This will generally be an absolute
375 * path, unless the frame is in the main script, in which case it will be the
376 * script location passed on the command line.
377 *
378 * For example, using +caller_locations.rb+ from Thread::Backtrace::Location
379 *
380 * loc = c(0..1).first
381 * loc.path #=> caller_locations.rb
382 */
383static VALUE
384location_path_m(VALUE self)
385{
386 const rb_iseq_t *iseq = location_iseq(location_ptr(self));
387 return iseq ? rb_iseq_path(iseq) : Qnil;
388}
389
390#ifdef USE_ISEQ_NODE_ID
391static int
392location_node_id(rb_backtrace_location_t *loc)
393{
394 if (loc->iseq && loc->pc) {
395 return calc_node_id(loc->iseq, loc->pc);
396 }
397 return -1;
398}
399#endif
400
401int
402rb_get_node_id_from_frame_info(VALUE obj)
403{
404#ifdef USE_ISEQ_NODE_ID
405 rb_backtrace_location_t *loc = location_ptr(obj);
406 return location_node_id(loc);
407#else
408 return -1;
409#endif
410}
411
412const rb_iseq_t *
413rb_get_iseq_from_frame_info(VALUE obj)
414{
415 rb_backtrace_location_t *loc = location_ptr(obj);
416 const rb_iseq_t *iseq = location_iseq(loc);
417 return iseq;
418}
419
420static VALUE
421location_realpath(rb_backtrace_location_t *loc)
422{
423 if (loc->iseq) {
424 return rb_iseq_realpath(loc->iseq);
425 }
426 return Qnil;
427}
428
429/*
430 * Returns the full file path of this frame.
431 *
432 * Same as #path, except that it will return absolute path
433 * even if the frame is in the main script.
434 */
435static VALUE
436location_absolute_path_m(VALUE self)
437{
438 return location_realpath(location_ptr(self));
439}
440
441static VALUE
442location_format(VALUE file, int lineno, VALUE name)
443{
444 VALUE s = rb_enc_sprintf(rb_enc_compatible(file, name), "%s", RSTRING_PTR(file));
445 if (lineno != 0) {
446 rb_str_catf(s, ":%d", lineno);
447 }
448 rb_str_cat_cstr(s, ":in ");
449 if (NIL_P(name)) {
450 rb_str_cat_cstr(s, "unknown method");
451 }
452 else {
453 rb_str_catf(s, "'%s'", RSTRING_PTR(name));
454 }
455 return s;
456}
457
458static VALUE
459location_to_str(rb_backtrace_location_t *loc)
460{
461 VALUE file, owner = Qnil, name;
462 int lineno;
463
464 if (location_cfunc_p(loc)) {
465 if (loc->iseq && loc->pc) {
466 file = rb_iseq_path(loc->iseq);
467 lineno = calc_lineno(loc->iseq, loc->pc);
468 }
469 else {
470 file = GET_VM()->progname;
471 lineno = 0;
472 }
473 name = rb_gen_method_name(loc->cme->owner, rb_id2str(loc->cme->def->original_id));
474 }
475 else {
476 file = rb_iseq_path(loc->iseq);
477 lineno = calc_lineno(loc->iseq, loc->pc);
478 if (loc->cme) {
479 owner = loc->cme->owner;
480 }
481 name = calculate_iseq_label(owner, loc->iseq);
482 }
483
484 return location_format(file, lineno, name);
485}
486
487/*
488 * Returns a Kernel#caller style string representing this frame.
489 */
490static VALUE
491location_to_str_m(VALUE self)
492{
493 return location_to_str(location_ptr(self));
494}
495
496/*
497 * Returns the same as calling +inspect+ on the string representation of
498 * #to_str
499 */
500static VALUE
501location_inspect_m(VALUE self)
502{
503 return rb_str_inspect(location_to_str(location_ptr(self)));
504}
505
506typedef struct rb_backtrace_struct {
507 int backtrace_size;
508 VALUE strary;
509 VALUE locary;
510 rb_backtrace_location_t backtrace[1];
512
513static void
514backtrace_mark(void *ptr)
515{
516 rb_backtrace_t *bt = (rb_backtrace_t *)ptr;
517 size_t i, s = bt->backtrace_size;
518
519 for (i=0; i<s; i++) {
520 location_mark_entry(&bt->backtrace[i]);
521 }
522 rb_gc_mark_movable(bt->strary);
523 rb_gc_mark_movable(bt->locary);
524}
525
526static void
527location_update_entry(rb_backtrace_location_t *fi)
528{
529 fi->cme = (rb_callable_method_entry_t *)rb_gc_location((VALUE)fi->cme);
530 if (fi->iseq) {
531 fi->iseq = (rb_iseq_t *)rb_gc_location((VALUE)fi->iseq);
532 }
533}
534
535static void
536backtrace_update(void *ptr)
537{
538 rb_backtrace_t *bt = (rb_backtrace_t *)ptr;
539 size_t i, s = bt->backtrace_size;
540
541 for (i=0; i<s; i++) {
542 location_update_entry(&bt->backtrace[i]);
543 }
544 bt->strary = rb_gc_location(bt->strary);
545 bt->locary = rb_gc_location(bt->locary);
546}
547
548static const rb_data_type_t backtrace_data_type = {
549 "backtrace",
550 {
551 backtrace_mark,
553 NULL, // No external memory to report,
554 backtrace_update,
555 },
556 /* Cannot set the RUBY_TYPED_EMBEDDABLE flag because the loc of frame_info
557 * points elements in the backtrace array. This can cause the loc to become
558 * incorrect if this backtrace object is moved by compaction. */
559 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
560};
561
562int
563rb_backtrace_p(VALUE obj)
564{
565 return rb_typeddata_is_kind_of(obj, &backtrace_data_type);
566}
567
568static VALUE
569backtrace_alloc(VALUE klass)
570{
571 rb_backtrace_t *bt;
572 VALUE obj = TypedData_Make_Struct(klass, rb_backtrace_t, &backtrace_data_type, bt);
573 return obj;
574}
575
576static VALUE
577backtrace_alloc_capa(long num_frames, rb_backtrace_t **backtrace)
578{
579 size_t memsize = offsetof(rb_backtrace_t, backtrace) + num_frames * sizeof(rb_backtrace_location_t);
580 VALUE btobj = rb_data_typed_object_zalloc(rb_cBacktrace, memsize, &backtrace_data_type);
581 TypedData_Get_Struct(btobj, rb_backtrace_t, &backtrace_data_type, *backtrace);
582 return btobj;
583}
584
585
586static long
587backtrace_size(const rb_execution_context_t *ec)
588{
589 const rb_control_frame_t *last_cfp = ec->cfp;
590 const rb_control_frame_t *start_cfp = RUBY_VM_END_CONTROL_FRAME(ec);
591
592 if (start_cfp == NULL) {
593 return -1;
594 }
595
596 start_cfp =
597 RUBY_VM_NEXT_CONTROL_FRAME(
598 RUBY_VM_NEXT_CONTROL_FRAME(start_cfp)); /* skip top frames */
599
600 if (start_cfp < last_cfp) {
601 return 0;
602 }
603
604 return start_cfp - last_cfp + 1;
605}
606
607static bool
608is_internal_location(const rb_control_frame_t *cfp)
609{
610 static const char prefix[] = "<internal:";
611 const size_t prefix_len = sizeof(prefix) - 1;
612 VALUE file = rb_iseq_path(cfp->iseq);
613 return strncmp(prefix, RSTRING_PTR(file), prefix_len) == 0;
614}
615
616static bool
617is_rescue_or_ensure_frame(const rb_control_frame_t *cfp)
618{
619 enum rb_iseq_type type = ISEQ_BODY(cfp->iseq)->type;
620 return type == ISEQ_TYPE_RESCUE || type == ISEQ_TYPE_ENSURE;
621}
622
623static void
624bt_update_cfunc_loc(unsigned long cfunc_counter, rb_backtrace_location_t *cfunc_loc, const rb_iseq_t *iseq, const VALUE *pc)
625{
626 for (; cfunc_counter > 0; cfunc_counter--, cfunc_loc--) {
627 cfunc_loc->iseq = iseq;
628 cfunc_loc->pc = pc;
629 }
630}
631
632static VALUE location_create(rb_backtrace_location_t *srcloc, void *btobj);
633
634static void
635bt_yield_loc(rb_backtrace_location_t *loc, long num_frames, VALUE btobj)
636{
637 for (; num_frames > 0; num_frames--, loc++) {
638 rb_yield(location_create(loc, (void *)btobj));
639 }
640}
641
642static VALUE
643rb_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)
644{
645 const rb_control_frame_t *cfp = ec->cfp;
646 const rb_control_frame_t *end_cfp = RUBY_VM_END_CONTROL_FRAME(ec);
647 ptrdiff_t size;
648 rb_backtrace_t *bt = NULL;
649 VALUE btobj = Qnil;
650 rb_backtrace_location_t *loc = NULL;
651 unsigned long cfunc_counter = 0;
652 bool skip_next_frame = FALSE;
653
654 // In the case the thread vm_stack or cfp is not initialized, there is no backtrace.
655 if (end_cfp == NULL) {
656 num_frames = 0;
657 }
658 else {
659 end_cfp = RUBY_VM_NEXT_CONTROL_FRAME(end_cfp);
660
661 /*
662 * top frame (dummy) <- RUBY_VM_END_CONTROL_FRAME
663 * top frame (dummy) <- end_cfp
664 * top frame <- main script
665 * top frame
666 * ...
667 * 2nd frame <- lev:0
668 * current frame <- ec->cfp
669 */
670
671 size = end_cfp - cfp + 1;
672 if (size < 0) {
673 num_frames = 0;
674 }
675 else if (num_frames < 0 || num_frames > size) {
676 num_frames = size;
677 }
678 }
679
680 btobj = backtrace_alloc_capa(num_frames, &bt);
681
682 bt->backtrace_size = 0;
683 if (num_frames == 0) {
684 if (start_too_large) *start_too_large = 0;
685 return btobj;
686 }
687
688 for (; cfp != end_cfp && (bt->backtrace_size < num_frames); cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp)) {
689 if (cfp->iseq) {
690 if (cfp->pc) {
691 if (start_frame > 0) {
692 start_frame--;
693 }
694 else if (!(skip_internal && is_internal_location(cfp))) {
695 if (!skip_next_frame) {
696 const rb_iseq_t *iseq = cfp->iseq;
697 const VALUE *pc = cfp->pc;
698 loc = &bt->backtrace[bt->backtrace_size++];
699 RB_OBJ_WRITE(btobj, &loc->cme, rb_vm_frame_method_entry(cfp));
700 // Ruby methods with `Primitive.attr! :c_trace` should behave like C methods
701 if (rb_iseq_attr_p(cfp->iseq, BUILTIN_ATTR_C_TRACE)) {
702 loc->iseq = NULL;
703 loc->pc = NULL;
704 cfunc_counter++;
705 }
706 else {
707 RB_OBJ_WRITE(btobj, &loc->iseq, iseq);
708 loc->pc = pc;
709 bt_update_cfunc_loc(cfunc_counter, loc-1, iseq, pc);
710 if (do_yield) {
711 bt_yield_loc(loc - cfunc_counter, cfunc_counter+1, btobj);
712 }
713 cfunc_counter = 0;
714 }
715 }
716 skip_next_frame = is_rescue_or_ensure_frame(cfp);
717 }
718 }
719 }
720 else {
721 VM_ASSERT(RUBYVM_CFUNC_FRAME_P(cfp));
722 if (start_frame > 0) {
723 start_frame--;
724 }
725 else {
726 loc = &bt->backtrace[bt->backtrace_size++];
727 RB_OBJ_WRITE(btobj, &loc->cme, rb_vm_frame_method_entry(cfp));
728 loc->iseq = NULL;
729 loc->pc = NULL;
730 cfunc_counter++;
731 }
732 }
733 }
734
735 // When a backtrace entry corresponds to a method defined in C (e.g. rb_define_method), the reported file:line
736 // is the one of the caller Ruby frame, so if the last entry is a C frame we find the caller Ruby frame here.
737 if (cfunc_counter > 0) {
738 for (; cfp != end_cfp; cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp)) {
739 if (cfp->iseq && cfp->pc && !(skip_internal && is_internal_location(cfp))) {
740 VM_ASSERT(!skip_next_frame); // ISEQ_TYPE_RESCUE/ISEQ_TYPE_ENSURE should have a caller Ruby ISEQ, not a cfunc
741 bt_update_cfunc_loc(cfunc_counter, loc, cfp->iseq, cfp->pc);
742 RB_OBJ_WRITTEN(btobj, Qundef, cfp->iseq);
743 if (do_yield) {
744 bt_yield_loc(loc - cfunc_counter, cfunc_counter, btobj);
745 }
746 break;
747 }
748 }
749 }
750
751 if (start_too_large) *start_too_large = (start_frame > 0 ? -1 : 0);
752 return btobj;
753}
754
755VALUE
756rb_ec_backtrace_object(const rb_execution_context_t *ec)
757{
758 return rb_ec_partial_backtrace_object(ec, RUBY_BACKTRACE_START, RUBY_ALL_BACKTRACE_LINES, NULL, FALSE, FALSE);
759}
760
761static VALUE
762backtrace_collect(rb_backtrace_t *bt, VALUE (*func)(rb_backtrace_location_t *, void *arg), void *arg)
763{
764 VALUE btary;
765 int i;
766
767 btary = rb_ary_new2(bt->backtrace_size);
768
769 for (i=0; i<bt->backtrace_size; i++) {
770 rb_backtrace_location_t *loc = &bt->backtrace[i];
771 rb_ary_push(btary, func(loc, arg));
772 }
773
774 return btary;
775}
776
777static VALUE
778location_to_str_dmyarg(rb_backtrace_location_t *loc, void *dmy)
779{
780 return location_to_str(loc);
781}
782
783static VALUE
784backtrace_to_str_ary(VALUE self)
785{
786 VALUE r;
787 rb_backtrace_t *bt;
788 TypedData_Get_Struct(self, rb_backtrace_t, &backtrace_data_type, bt);
789 r = backtrace_collect(bt, location_to_str_dmyarg, 0);
790 RB_GC_GUARD(self);
791 return r;
792}
793
794VALUE
795rb_backtrace_to_str_ary(VALUE self)
796{
797 rb_backtrace_t *bt;
798 TypedData_Get_Struct(self, rb_backtrace_t, &backtrace_data_type, bt);
799
800 if (!bt->strary) {
801 RB_OBJ_WRITE(self, &bt->strary, backtrace_to_str_ary(self));
802 }
803 return bt->strary;
804}
805
806void
807rb_backtrace_use_iseq_first_lineno_for_last_location(VALUE self)
808{
809 rb_backtrace_t *bt;
811
812 TypedData_Get_Struct(self, rb_backtrace_t, &backtrace_data_type, bt);
813 VM_ASSERT(bt->backtrace_size > 0);
814
815 loc = &bt->backtrace[0];
816
817 VM_ASSERT(!loc->cme || loc->cme->def->type == VM_METHOD_TYPE_ISEQ);
818
819 loc->pc = NULL; // means location.first_lineno
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
846VALUE
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
858VALUE
859rb_location_ary_to_backtrace(VALUE ary)
860{
861 if (!RB_TYPE_P(ary, T_ARRAY) || !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
964VALUE
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
970VALUE
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
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
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
1147{
1148 vm_backtrace_print(stderr);
1149}
1150
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
1188VALUE
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
1294VALUE
1295rb_vm_thread_backtrace(int argc, const VALUE *argv, VALUE thval)
1296{
1297 return thread_backtrace_to_ary(argc, argv, thval, 1);
1298}
1299
1300VALUE
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
1306VALUE
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
1312VALUE
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
1495 rb_control_frame_t *cfp;
1496 VALUE backtrace;
1497 VALUE contexts; /* [[klass, binding, iseq, cfp], ...] */
1498 long backtrace_size;
1499};
1500
1501enum {
1502 CALLER_BINDING_SELF,
1503 CALLER_BINDING_CLASS,
1504 CALLER_BINDING_BINDING,
1505 CALLER_BINDING_ISEQ,
1506 CALLER_BINDING_CFP,
1507 CALLER_BINDING_DEPTH,
1508};
1509
1511 VALUE ary;
1512 const rb_execution_context_t *ec;
1513};
1514
1515static void
1516collect_caller_bindings_init(void *arg, size_t size)
1517{
1518 /* */
1519}
1520
1521static VALUE
1522get_klass(const rb_control_frame_t *cfp)
1523{
1524 VALUE klass;
1525 if (rb_vm_control_frame_id_and_class(cfp, 0, 0, &klass)) {
1526 if (RB_TYPE_P(klass, T_ICLASS)) {
1527 return RBASIC(klass)->klass;
1528 }
1529 else {
1530 return klass;
1531 }
1532 }
1533 else {
1534 return Qnil;
1535 }
1536}
1537
1538static int
1539frame_depth(const rb_execution_context_t *ec, const rb_control_frame_t *cfp)
1540{
1541 VM_ASSERT(RUBY_VM_END_CONTROL_FRAME(ec) >= cfp);
1542 return (int)(RUBY_VM_END_CONTROL_FRAME(ec) - cfp);
1543}
1544
1545static void
1546collect_caller_bindings_iseq(void *arg, const rb_control_frame_t *cfp)
1547{
1549 VALUE frame = rb_ary_new2(6);
1550
1551 rb_ary_store(frame, CALLER_BINDING_SELF, cfp->self);
1552 rb_ary_store(frame, CALLER_BINDING_CLASS, get_klass(cfp));
1553 rb_ary_store(frame, CALLER_BINDING_BINDING, GC_GUARDED_PTR(cfp)); /* create later */
1554 rb_ary_store(frame, CALLER_BINDING_ISEQ, cfp->iseq ? (VALUE)cfp->iseq : Qnil);
1555 rb_ary_store(frame, CALLER_BINDING_CFP, GC_GUARDED_PTR(cfp));
1556 rb_ary_store(frame, CALLER_BINDING_DEPTH, INT2FIX(frame_depth(data->ec, cfp)));
1557
1558 rb_ary_push(data->ary, frame);
1559}
1560
1561static void
1562collect_caller_bindings_cfunc(void *arg, const rb_control_frame_t *cfp, ID mid)
1563{
1565 VALUE frame = rb_ary_new2(6);
1566
1567 rb_ary_store(frame, CALLER_BINDING_SELF, cfp->self);
1568 rb_ary_store(frame, CALLER_BINDING_CLASS, get_klass(cfp));
1569 rb_ary_store(frame, CALLER_BINDING_BINDING, Qnil); /* not available */
1570 rb_ary_store(frame, CALLER_BINDING_ISEQ, Qnil); /* not available */
1571 rb_ary_store(frame, CALLER_BINDING_CFP, GC_GUARDED_PTR(cfp));
1572 rb_ary_store(frame, CALLER_BINDING_DEPTH, INT2FIX(frame_depth(data->ec, cfp)));
1573
1574 rb_ary_push(data->ary, frame);
1575}
1576
1577static VALUE
1578collect_caller_bindings(const rb_execution_context_t *ec)
1579{
1580 int i;
1581 VALUE result;
1582 struct collect_caller_bindings_data data = {
1583 rb_ary_new(), ec
1584 };
1585
1586 backtrace_each(ec,
1587 collect_caller_bindings_init,
1588 collect_caller_bindings_iseq,
1589 collect_caller_bindings_cfunc,
1590 &data);
1591
1592 result = rb_ary_reverse(data.ary);
1593
1594 /* bindings should be created from top of frame */
1595 for (i=0; i<RARRAY_LEN(result); i++) {
1596 VALUE entry = rb_ary_entry(result, i);
1597 VALUE cfp_val = rb_ary_entry(entry, CALLER_BINDING_BINDING);
1598
1599 if (!NIL_P(cfp_val)) {
1600 rb_control_frame_t *cfp = GC_GUARDED_PTR_REF(cfp_val);
1601 rb_ary_store(entry, CALLER_BINDING_BINDING, rb_vm_make_binding(ec, cfp));
1602 }
1603 }
1604
1605 return result;
1606}
1607
1608/*
1609 * Note that the passed `rb_debug_inspector_t' will be disabled
1610 * after `rb_debug_inspector_open'.
1611 */
1612
1613VALUE
1615{
1616 rb_debug_inspector_t dbg_context;
1617 rb_execution_context_t *ec = GET_EC();
1618 enum ruby_tag_type state;
1619 volatile VALUE MAYBE_UNUSED(result);
1620
1621 /* escape all env to heap */
1622 rb_vm_stack_to_heap(ec);
1623
1624 dbg_context.ec = ec;
1625 dbg_context.cfp = dbg_context.ec->cfp;
1626 dbg_context.backtrace = rb_ec_backtrace_location_ary(ec, RUBY_BACKTRACE_START, RUBY_ALL_BACKTRACE_LINES, FALSE);
1627 dbg_context.backtrace_size = RARRAY_LEN(dbg_context.backtrace);
1628 dbg_context.contexts = collect_caller_bindings(ec);
1629
1630 EC_PUSH_TAG(ec);
1631 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1632 result = (*func)(&dbg_context, data);
1633 }
1634 EC_POP_TAG();
1635
1636 /* invalidate bindings? */
1637
1638 if (state) {
1639 EC_JUMP_TAG(ec, state);
1640 }
1641
1642 return result;
1643}
1644
1645static VALUE
1646frame_get(const rb_debug_inspector_t *dc, long index)
1647{
1648 if (index < 0 || index >= dc->backtrace_size) {
1649 rb_raise(rb_eArgError, "no such frame");
1650 }
1651 return rb_ary_entry(dc->contexts, index);
1652}
1653
1654VALUE
1656{
1657 VALUE frame = frame_get(dc, index);
1658 return rb_ary_entry(frame, CALLER_BINDING_SELF);
1659}
1660
1661VALUE
1663{
1664 VALUE frame = frame_get(dc, index);
1665 return rb_ary_entry(frame, CALLER_BINDING_CLASS);
1666}
1667
1668VALUE
1670{
1671 VALUE frame = frame_get(dc, index);
1672 return rb_ary_entry(frame, CALLER_BINDING_BINDING);
1673}
1674
1675VALUE
1677{
1678 VALUE frame = frame_get(dc, index);
1679 VALUE iseq = rb_ary_entry(frame, CALLER_BINDING_ISEQ);
1680
1681 return RTEST(iseq) ? rb_iseqw_new((rb_iseq_t *)iseq) : Qnil;
1682}
1683
1684VALUE
1686{
1687 VALUE frame = frame_get(dc, index);
1688 return rb_ary_entry(frame, CALLER_BINDING_DEPTH);
1689}
1690
1691VALUE
1693{
1694 rb_execution_context_t *ec = GET_EC();
1695 return INT2FIX(frame_depth(ec, ec->cfp));
1696}
1697
1698VALUE
1700{
1701 return dc->backtrace;
1702}
1703
1704static int
1705thread_profile_frames(rb_execution_context_t *ec, int start, int limit, VALUE *buff, int *lines)
1706{
1707 int i;
1708 const rb_control_frame_t *cfp = ec->cfp, *end_cfp = RUBY_VM_END_CONTROL_FRAME(ec);
1709 const rb_control_frame_t *top = cfp;
1710 const rb_callable_method_entry_t *cme;
1711
1712 // If this function is called inside a thread after thread creation, but
1713 // before the CFP has been created, just return 0. This can happen when
1714 // sampling via signals. Threads can be interrupted randomly by the
1715 // signal, including during the time after the thread has been created, but
1716 // before the CFP has been allocated
1717 if (!cfp) {
1718 return 0;
1719 }
1720
1721 // Skip dummy frame; see `rb_ec_partial_backtrace_object` for details
1722 end_cfp = RUBY_VM_NEXT_CONTROL_FRAME(end_cfp);
1723
1724 for (i=0; i<limit && cfp != end_cfp; cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp)) {
1725 if (VM_FRAME_RUBYFRAME_P(cfp) && cfp->pc != 0) {
1726 if (start > 0) {
1727 start--;
1728 continue;
1729 }
1730
1731 /* record frame info */
1732 cme = rb_vm_frame_method_entry(cfp);
1733 if (cme && cme->def->type == VM_METHOD_TYPE_ISEQ) {
1734 buff[i] = (VALUE)cme;
1735 }
1736 else {
1737 buff[i] = (VALUE)cfp->iseq;
1738 }
1739
1740 if (lines) {
1741 // The topmost frame may not have an updated PC because the JIT
1742 // may not have set one. The JIT compiler will update the PC
1743 // before entering a new function (so that `caller` will work),
1744 // so only the topmost frame could possibly have an out of date PC
1745 if (cfp == top && cfp->jit_return) {
1746 lines[i] = 0;
1747 }
1748 else {
1749 lines[i] = calc_lineno(cfp->iseq, cfp->pc);
1750 }
1751 }
1752
1753 i++;
1754 }
1755 else {
1756 cme = rb_vm_frame_method_entry(cfp);
1757 if (cme && cme->def->type == VM_METHOD_TYPE_CFUNC) {
1758 if (start > 0) {
1759 start--;
1760 continue;
1761 }
1762 buff[i] = (VALUE)cme;
1763 if (lines) lines[i] = 0;
1764 i++;
1765 }
1766 }
1767 }
1768
1769 return i;
1770}
1771
1772int
1773rb_profile_frames(int start, int limit, VALUE *buff, int *lines)
1774{
1775 rb_execution_context_t *ec = rb_current_execution_context(false);
1776
1777 // If there is no EC, we may be attempting to profile a non-Ruby thread or a
1778 // M:N shared native thread which has no active Ruby thread.
1779 if (!ec) {
1780 return 0;
1781 }
1782
1783 return thread_profile_frames(ec, start, limit, buff, lines);
1784}
1785
1786int
1787rb_profile_thread_frames(VALUE thread, int start, int limit, VALUE *buff, int *lines)
1788{
1789 rb_thread_t *th = rb_thread_ptr(thread);
1790 return thread_profile_frames(th->ec, start, limit, buff, lines);
1791}
1792
1793static const rb_iseq_t *
1794frame2iseq(VALUE frame)
1795{
1796 if (NIL_P(frame)) return NULL;
1797
1798 if (RB_TYPE_P(frame, T_IMEMO)) {
1799 switch (imemo_type(frame)) {
1800 case imemo_iseq:
1801 return (const rb_iseq_t *)frame;
1802 case imemo_ment:
1803 {
1805 switch (cme->def->type) {
1806 case VM_METHOD_TYPE_ISEQ:
1807 return cme->def->body.iseq.iseqptr;
1808 default:
1809 return NULL;
1810 }
1811 }
1812 default:
1813 break;
1814 }
1815 }
1816 rb_bug("frame2iseq: unreachable");
1817}
1818
1819VALUE
1821{
1822 const rb_iseq_t *iseq = frame2iseq(frame);
1823 return iseq ? rb_iseq_path(iseq) : Qnil;
1824}
1825
1826static const rb_callable_method_entry_t *
1827cframe(VALUE frame)
1828{
1829 if (NIL_P(frame)) return NULL;
1830
1831 if (RB_TYPE_P(frame, T_IMEMO)) {
1832 switch (imemo_type(frame)) {
1833 case imemo_ment:
1834 {
1836 switch (cme->def->type) {
1837 case VM_METHOD_TYPE_CFUNC:
1838 return cme;
1839 default:
1840 return NULL;
1841 }
1842 }
1843 default:
1844 return NULL;
1845 }
1846 }
1847
1848 return NULL;
1849}
1850
1851VALUE
1853{
1854 if (cframe(frame)) {
1855 static VALUE cfunc_str = Qfalse;
1856 if (!cfunc_str) {
1857 cfunc_str = rb_str_new_literal("<cfunc>");
1858 rb_vm_register_global_object(cfunc_str);
1859 }
1860 return cfunc_str;
1861 }
1862 const rb_iseq_t *iseq = frame2iseq(frame);
1863 return iseq ? rb_iseq_realpath(iseq) : Qnil;
1864}
1865
1866VALUE
1868{
1869 const rb_iseq_t *iseq = frame2iseq(frame);
1870 return iseq ? rb_iseq_label(iseq) : Qnil;
1871}
1872
1873VALUE
1875{
1876 const rb_iseq_t *iseq = frame2iseq(frame);
1877 return iseq ? rb_iseq_base_label(iseq) : Qnil;
1878}
1879
1880VALUE
1882{
1883 const rb_iseq_t *iseq = frame2iseq(frame);
1884 return iseq ? rb_iseq_first_lineno(iseq) : Qnil;
1885}
1886
1887static VALUE
1888frame2klass(VALUE frame)
1889{
1890 if (NIL_P(frame)) return Qnil;
1891
1892 if (RB_TYPE_P(frame, T_IMEMO)) {
1894
1895 if (imemo_type(frame) == imemo_ment) {
1896 return cme->defined_class;
1897 }
1898 }
1899 return Qnil;
1900}
1901
1902VALUE
1904{
1905 VALUE klass = frame2klass(frame);
1906
1907 if (klass && !NIL_P(klass)) {
1908 if (RB_TYPE_P(klass, T_ICLASS)) {
1909 klass = RBASIC(klass)->klass;
1910 }
1911 else if (RCLASS_SINGLETON_P(klass)) {
1912 klass = RCLASS_ATTACHED_OBJECT(klass);
1913 if (!RB_TYPE_P(klass, T_CLASS) && !RB_TYPE_P(klass, T_MODULE))
1914 return rb_sprintf("#<%s:%p>", rb_class2name(rb_obj_class(klass)), (void*)klass);
1915 }
1916 return rb_class_path(klass);
1917 }
1918 else {
1919 return Qnil;
1920 }
1921}
1922
1923VALUE
1925{
1926 VALUE klass = frame2klass(frame);
1927
1928 return RBOOL(klass && !NIL_P(klass) && RCLASS_SINGLETON_P(klass));
1929}
1930
1931VALUE
1933{
1934 const rb_callable_method_entry_t *cme = cframe(frame);
1935 if (cme) {
1936 ID mid = cme->def->original_id;
1937 return id2str(mid);
1938 }
1939 const rb_iseq_t *iseq = frame2iseq(frame);
1940 return iseq ? rb_iseq_method_name(iseq) : Qnil;
1941}
1942
1943static VALUE
1944qualified_method_name(VALUE frame, VALUE method_name)
1945{
1946 if (method_name != Qnil) {
1947 VALUE classpath = rb_profile_frame_classpath(frame);
1948 VALUE singleton_p = rb_profile_frame_singleton_method_p(frame);
1949
1950 if (classpath != Qnil) {
1951 return rb_sprintf("%"PRIsVALUE"%s%"PRIsVALUE,
1952 classpath, singleton_p == Qtrue ? "." : "#", method_name);
1953 }
1954 else {
1955 return method_name;
1956 }
1957 }
1958 else {
1959 return Qnil;
1960 }
1961}
1962
1963VALUE
1965{
1966 VALUE method_name = rb_profile_frame_method_name(frame);
1967
1968 return qualified_method_name(frame, method_name);
1969}
1970
1971VALUE
1973{
1974 const rb_callable_method_entry_t *cme = cframe(frame);
1975 if (cme) {
1976 ID mid = cme->def->original_id;
1977 VALUE method_name = id2str(mid);
1978 return qualified_method_name(frame, method_name);
1979 }
1980
1981 VALUE label = rb_profile_frame_label(frame);
1982 VALUE base_label = rb_profile_frame_base_label(frame);
1983 VALUE qualified_method_name = rb_profile_frame_qualified_method_name(frame);
1984
1985 if (NIL_P(qualified_method_name) || base_label == qualified_method_name) {
1986 return label;
1987 }
1988 else {
1989 long label_length = RSTRING_LEN(label);
1990 long base_label_length = RSTRING_LEN(base_label);
1991 int prefix_len = rb_long2int(label_length - base_label_length);
1992
1993 return rb_sprintf("%.*s%"PRIsVALUE, prefix_len, RSTRING_PTR(label), qualified_method_name);
1994 }
1995}
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
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.
int rb_profile_thread_frames(VALUE thread, int start, int limit, VALUE *buff, int *lines)
Queries mysterious "frame"s of the given range.
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
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:1198
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition class.c:1012
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
Definition class.c:2166
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:2635
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
Definition class.c:2424
#define Qundef
Old name of RUBY_Qundef.
#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 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 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 T_ARRAY
Old name of RUBY_T_ARRAY.
Definition value_type.h:56
#define NIL_P
Old name of RB_NIL_P.
#define T_CLASS
Old name of RUBY_T_CLASS.
Definition value_type.h:58
#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:657
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:1380
VALUE rb_cArray
Array class.
Definition array.c:40
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:247
VALUE rb_cThread
Thread class.
Definition vm.c:530
#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
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:603
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:1892
#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:7212
#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_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
Definition variable.c:293
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
Definition vm_method.c:1284
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
void rb_backtrace(void)
Prints the backtrace out to the standard error.
int len
Length of the buffer.
Definition io.h:8
VALUE rb_yield(VALUE val)
Yields the block.
Definition vm_eval.c:1354
#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:134
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:167
VALUE type(ANYARGS)
ANYARGS-ed function type.
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:51
#define RARRAY_AREF(a, i)
Definition rarray.h:403
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
#define RUBY_DEFAULT_FREE
This is a value you can set to RData::dfree.
Definition rdata.h:78
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
Definition rtypeddata.h:79
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
Definition rtypeddata.h:515
#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:497
const char * rb_class2name(VALUE klass)
Queries the name of the passed class.
Definition variable.c:418
#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
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:200
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
Definition method.h:135
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
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