12#include "eval_intern.h"
14#include "internal/class.h"
15#include "internal/error.h"
16#include "internal/vm.h"
22static VALUE rb_cBacktrace;
23static VALUE rb_cBacktraceLocation;
28 VALUE str = rb_id2str(
id);
29 if (!str)
return Qnil;
32#define rb_id2str(id) id2str(id)
34#define BACKTRACE_START 0
35#define ALL_BACKTRACE_LINES -1
38calc_pos(
const rb_iseq_t *iseq,
const VALUE *pc,
int *lineno,
int *node_id)
43 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_TOP) {
44 VM_ASSERT(! ISEQ_BODY(iseq)->local_table);
45 VM_ASSERT(! ISEQ_BODY(iseq)->local_table_size);
48 if (lineno) *lineno = ISEQ_BODY(iseq)->location.first_lineno;
49#ifdef USE_ISEQ_NODE_ID
50 if (node_id) *node_id = -1;
55 VM_ASSERT(ISEQ_BODY(iseq));
56 VM_ASSERT(ISEQ_BODY(iseq)->iseq_encoded);
57 VM_ASSERT(ISEQ_BODY(iseq)->iseq_size);
59 ptrdiff_t n = pc - ISEQ_BODY(iseq)->iseq_encoded;
61#if SIZEOF_PTRDIFF_T > SIZEOF_INT
62 VM_ASSERT(n <= (ptrdiff_t)UINT_MAX);
64 VM_ASSERT((
unsigned int)n <= ISEQ_BODY(iseq)->iseq_size);
71#if VMDEBUG && defined(HAVE_BUILTIN___BUILTIN_TRAP)
74 rb_print_backtrace(stderr);
78 if (lineno) *lineno = rb_iseq_line_no(iseq, pos);
79#ifdef USE_ISEQ_NODE_ID
80 if (node_id) *node_id = rb_iseq_node_id(iseq, pos);
87calc_lineno(
const rb_iseq_t *iseq,
const VALUE *pc)
90 if (calc_pos(iseq, pc, &lineno, NULL))
return lineno;
94#ifdef USE_ISEQ_NODE_ID
96calc_node_id(
const rb_iseq_t *iseq,
const VALUE *pc)
99 if (calc_pos(iseq, pc, NULL, &node_id))
return node_id;
105rb_vm_get_sourceline(
const rb_control_frame_t *cfp)
107 if (VM_FRAME_RUBYFRAME_P(cfp) && cfp->iseq) {
108 const rb_iseq_t *iseq = cfp->iseq;
109 int line = calc_lineno(iseq, cfp->pc);
114 return ISEQ_BODY(iseq)->location.first_lineno;
123 const rb_callable_method_entry_t *cme;
124 const rb_iseq_t *iseq;
126} rb_backtrace_location_t;
129 rb_backtrace_location_t *loc;
134location_mark(
void *ptr)
137 rb_gc_mark_movable(vfi->btobj);
141location_ref_update(
void *ptr)
144 vfi->btobj = rb_gc_location(vfi->btobj);
148location_mark_entry(rb_backtrace_location_t *fi)
150 rb_gc_mark((
VALUE)fi->cme);
151 if (fi->iseq) rb_gc_mark_movable((
VALUE)fi->iseq);
162 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
166rb_frame_info_p(
VALUE obj)
171static inline rb_backtrace_location_t *
172location_ptr(
VALUE locobj)
180location_lineno(rb_backtrace_location_t *loc)
183 return calc_lineno(loc->iseq, loc->pc);
197location_lineno_m(
VALUE self)
199 return INT2FIX(location_lineno(location_ptr(self)));
202VALUE rb_mod_name0(
VALUE klass,
bool *permanent);
209 if (RCLASS_SINGLETON_P(owner)) {
210 VALUE v = RCLASS_ATTACHED_OBJECT(owner);
212 v = rb_mod_name0(v, &permanent);
213 if (permanent && !
NIL_P(v)) {
214 return rb_sprintf(
"%"PRIsVALUE
".%"PRIsVALUE, v, name);
219 owner = rb_mod_name0(owner, &permanent);
220 if (permanent && !
NIL_P(owner)) {
221 return rb_sprintf(
"%"PRIsVALUE
"#%"PRIsVALUE, owner, name);
229calculate_iseq_label(
VALUE owner,
const rb_iseq_t *iseq)
232 switch (ISEQ_BODY(iseq)->
type) {
234 case ISEQ_TYPE_CLASS:
236 return ISEQ_BODY(iseq)->location.label;
237 case ISEQ_TYPE_METHOD:
238 return rb_gen_method_name(owner, ISEQ_BODY(iseq)->location.label);
239 case ISEQ_TYPE_BLOCK:
240 case ISEQ_TYPE_PLAIN: {
242 const rb_iseq_t *orig_iseq = iseq;
243 if (ISEQ_BODY(orig_iseq)->parent_iseq != 0) {
244 while (ISEQ_BODY(orig_iseq)->local_iseq != iseq) {
245 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_BLOCK) {
248 iseq = ISEQ_BODY(iseq)->parent_iseq;
252 return rb_sprintf(
"block in %"PRIsVALUE, calculate_iseq_label(owner, iseq));
255 return rb_sprintf(
"block (%d levels) in %"PRIsVALUE, level, calculate_iseq_label(owner, iseq));
258 case ISEQ_TYPE_RESCUE:
259 case ISEQ_TYPE_ENSURE:
261 iseq = ISEQ_BODY(iseq)->parent_iseq;
264 rb_bug(
"calculate_iseq_label: unreachable");
270location_cfunc_p(rb_backtrace_location_t *loc)
272 if (!loc->cme)
return false;
274 switch (loc->cme->def->type) {
275 case VM_METHOD_TYPE_CFUNC:
277 case VM_METHOD_TYPE_ISEQ:
278 return rb_iseq_attr_p(loc->cme->def->body.iseq.
iseqptr, BUILTIN_ATTR_C_TRACE);
285location_label(rb_backtrace_location_t *loc)
287 if (location_cfunc_p(loc)) {
288 return rb_gen_method_name(loc->cme->owner, rb_id2str(loc->cme->def->original_id));
293 owner = loc->cme->owner;
295 return calculate_iseq_label(owner, loc->iseq);
325location_label_m(
VALUE self)
327 return location_label(location_ptr(self));
331location_base_label(rb_backtrace_location_t *loc)
333 if (location_cfunc_p(loc)) {
334 return rb_id2str(loc->cme->def->original_id);
337 return ISEQ_BODY(loc->iseq)->location.base_label;
365location_base_label_m(
VALUE self)
367 return location_base_label(location_ptr(self));
370static const rb_iseq_t *
371location_iseq(rb_backtrace_location_t *loc)
387location_path_m(
VALUE self)
389 const rb_iseq_t *iseq = location_iseq(location_ptr(self));
390 return iseq ? rb_iseq_path(iseq) :
Qnil;
393#ifdef USE_ISEQ_NODE_ID
395location_node_id(rb_backtrace_location_t *loc)
397 if (loc->iseq && loc->pc) {
398 return calc_node_id(loc->iseq, loc->pc);
405rb_get_node_id_from_frame_info(
VALUE obj)
407#ifdef USE_ISEQ_NODE_ID
408 rb_backtrace_location_t *loc = location_ptr(obj);
409 return location_node_id(loc);
416rb_get_iseq_from_frame_info(
VALUE obj)
418 rb_backtrace_location_t *loc = location_ptr(obj);
419 const rb_iseq_t *iseq = location_iseq(loc);
424location_realpath(rb_backtrace_location_t *loc)
427 return rb_iseq_realpath(loc->iseq);
439location_absolute_path_m(
VALUE self)
441 return location_realpath(location_ptr(self));
445location_format(
VALUE file,
int lineno,
VALUE name)
449 rb_str_catf(s,
":%d", lineno);
456 rb_str_catf(s,
"'%s'", RSTRING_PTR(name));
462location_to_str(rb_backtrace_location_t *loc)
467 if (location_cfunc_p(loc)) {
468 if (loc->iseq && loc->pc) {
469 file = rb_iseq_path(loc->iseq);
470 lineno = calc_lineno(loc->iseq, loc->pc);
473 file = GET_VM()->progname;
476 name = rb_gen_method_name(loc->cme->owner, rb_id2str(loc->cme->def->original_id));
479 file = rb_iseq_path(loc->iseq);
480 lineno = calc_lineno(loc->iseq, loc->pc);
482 owner = loc->cme->owner;
484 name = calculate_iseq_label(owner, loc->iseq);
487 return location_format(file, lineno, name);
494location_to_str_m(
VALUE self)
496 return location_to_str(location_ptr(self));
504location_inspect_m(
VALUE self)
513 rb_backtrace_location_t backtrace[1];
517backtrace_mark(
void *ptr)
519 rb_backtrace_t *bt = (rb_backtrace_t *)ptr;
520 size_t i, s = bt->backtrace_size;
522 for (i=0; i<s; i++) {
523 location_mark_entry(&bt->backtrace[i]);
525 rb_gc_mark_movable(bt->strary);
526 rb_gc_mark_movable(bt->locary);
530location_update_entry(rb_backtrace_location_t *fi)
532 fi->cme = (rb_callable_method_entry_t *)rb_gc_location((
VALUE)fi->cme);
534 fi->iseq = (rb_iseq_t *)rb_gc_location((
VALUE)fi->iseq);
539backtrace_update(
void *ptr)
541 rb_backtrace_t *bt = (rb_backtrace_t *)ptr;
542 size_t i, s = bt->backtrace_size;
544 for (i=0; i<s; i++) {
545 location_update_entry(&bt->backtrace[i]);
547 bt->strary = rb_gc_location(bt->strary);
548 bt->locary = rb_gc_location(bt->locary);
562 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
566rb_backtrace_p(
VALUE obj)
572backtrace_alloc(
VALUE klass)
580backtrace_alloc_capa(
long num_frames, rb_backtrace_t **backtrace)
582 size_t memsize = offsetof(rb_backtrace_t, backtrace) + num_frames *
sizeof(rb_backtrace_location_t);
583 VALUE btobj = rb_data_typed_object_zalloc(rb_cBacktrace, memsize, &backtrace_data_type);
590backtrace_size(
const rb_execution_context_t *ec)
592 const rb_control_frame_t *last_cfp = ec->cfp;
593 const rb_control_frame_t *start_cfp = RUBY_VM_END_CONTROL_FRAME(ec);
595 if (start_cfp == NULL) {
600 RUBY_VM_NEXT_CONTROL_FRAME(
601 RUBY_VM_NEXT_CONTROL_FRAME(start_cfp));
603 if (start_cfp < last_cfp) {
607 return start_cfp - last_cfp + 1;
611is_internal_location(
const rb_control_frame_t *cfp)
613 static const char prefix[] =
"<internal:";
614 const size_t prefix_len =
sizeof(prefix) - 1;
615 VALUE file = rb_iseq_path(cfp->iseq);
616 return strncmp(prefix, RSTRING_PTR(file), prefix_len) == 0;
620is_rescue_or_ensure_frame(
const rb_control_frame_t *cfp)
622 enum rb_iseq_type
type = ISEQ_BODY(cfp->iseq)->type;
623 return type == ISEQ_TYPE_RESCUE ||
type == ISEQ_TYPE_ENSURE;
627bt_update_cfunc_loc(
unsigned long cfunc_counter, rb_backtrace_location_t *cfunc_loc,
const rb_iseq_t *iseq,
const VALUE *pc)
629 for (; cfunc_counter > 0; cfunc_counter--, cfunc_loc--) {
630 cfunc_loc->iseq = iseq;
635static VALUE location_create(rb_backtrace_location_t *srcloc,
void *btobj);
638bt_yield_loc(rb_backtrace_location_t *loc,
long num_frames,
VALUE btobj)
640 for (; num_frames > 0; num_frames--, loc++) {
641 rb_yield(location_create(loc, (
void *)btobj));
646rb_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)
648 const rb_control_frame_t *cfp = ec->cfp;
649 const rb_control_frame_t *end_cfp = RUBY_VM_END_CONTROL_FRAME(ec);
651 rb_backtrace_t *bt = NULL;
653 rb_backtrace_location_t *loc = NULL;
654 unsigned long cfunc_counter = 0;
655 bool skip_next_frame = FALSE;
658 if (end_cfp == NULL) {
662 end_cfp = RUBY_VM_NEXT_CONTROL_FRAME(end_cfp);
674 size = end_cfp - cfp + 1;
678 else if (num_frames < 0 || num_frames > size) {
683 btobj = backtrace_alloc_capa(num_frames, &bt);
685 bt->backtrace_size = 0;
686 if (num_frames == 0) {
687 if (start_too_large) *start_too_large = 0;
691 for (; cfp != end_cfp && (bt->backtrace_size < num_frames); cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp)) {
694 if (start_frame > 0) {
697 else if (!(skip_internal && is_internal_location(cfp))) {
698 if (!skip_next_frame) {
699 const rb_iseq_t *iseq = cfp->iseq;
700 const VALUE *pc = cfp->pc;
701 loc = &bt->backtrace[bt->backtrace_size++];
702 RB_OBJ_WRITE(btobj, &loc->cme, rb_vm_frame_method_entry(cfp));
704 if (rb_iseq_attr_p(cfp->iseq, BUILTIN_ATTR_C_TRACE)) {
712 bt_update_cfunc_loc(cfunc_counter, loc-1, iseq, pc);
714 bt_yield_loc(loc - cfunc_counter, cfunc_counter+1, btobj);
719 skip_next_frame = is_rescue_or_ensure_frame(cfp);
724 VM_ASSERT(RUBYVM_CFUNC_FRAME_P(cfp));
725 if (start_frame > 0) {
729 loc = &bt->backtrace[bt->backtrace_size++];
730 RB_OBJ_WRITE(btobj, &loc->cme, rb_vm_frame_method_entry(cfp));
740 if (cfunc_counter > 0) {
741 for (; cfp != end_cfp; cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp)) {
742 if (cfp->iseq && cfp->pc && !(skip_internal && is_internal_location(cfp))) {
743 VM_ASSERT(!skip_next_frame);
744 bt_update_cfunc_loc(cfunc_counter, loc, cfp->iseq, cfp->pc);
747 bt_yield_loc(loc - cfunc_counter, cfunc_counter, btobj);
754 if (start_too_large) *start_too_large = (start_frame > 0 ? -1 : 0);
759rb_ec_backtrace_object(
const rb_execution_context_t *ec)
761 return rb_ec_partial_backtrace_object(ec, BACKTRACE_START, ALL_BACKTRACE_LINES, NULL, FALSE, FALSE);
765backtrace_collect(rb_backtrace_t *bt,
VALUE (*func)(rb_backtrace_location_t *,
void *arg),
void *arg)
772 for (i=0; i<bt->backtrace_size; i++) {
773 rb_backtrace_location_t *loc = &bt->backtrace[i];
774 rb_ary_push(btary, func(loc, arg));
781location_to_str_dmyarg(rb_backtrace_location_t *loc,
void *dmy)
783 return location_to_str(loc);
787backtrace_to_str_ary(
VALUE self)
792 r = backtrace_collect(bt, location_to_str_dmyarg, 0);
798rb_backtrace_to_str_ary(
VALUE self)
804 RB_OBJ_WRITE(self, &bt->strary, backtrace_to_str_ary(self));
810rb_backtrace_use_iseq_first_lineno_for_last_location(
VALUE self)
813 rb_backtrace_location_t *loc;
816 VM_ASSERT(bt->backtrace_size > 0);
818 loc = &bt->backtrace[0];
820 VM_ASSERT(!loc->cme || loc->cme->def->type == VM_METHOD_TYPE_ISEQ);
826location_create(rb_backtrace_location_t *srcloc,
void *btobj)
839backtrace_to_location_ary(
VALUE self)
844 r = backtrace_collect(bt, location_create, (
void *)self);
850rb_backtrace_to_location_ary(
VALUE self)
856 RB_OBJ_WRITE(self, &bt->locary, backtrace_to_location_ary(self));
862rb_location_ary_to_backtrace(
VALUE ary)
868 rb_backtrace_t *new_backtrace;
870 VALUE btobj = backtrace_alloc_capa(num_frames, &new_backtrace);
872 for (
long index = 0; index <
RARRAY_LEN(ary); index++) {
875 if (!rb_frame_info_p(locobj)) {
882 rb_backtrace_location_t *dst_location = &new_backtrace->backtrace[index];
883 RB_OBJ_WRITE(btobj, &dst_location->cme, src_vloc->loc->cme);
884 RB_OBJ_WRITE(btobj, &dst_location->iseq, src_vloc->loc->iseq);
885 dst_location->pc = src_vloc->loc->pc;
887 new_backtrace->backtrace_size++;
896backtrace_dump_data(
VALUE self)
898 VALUE str = rb_backtrace_to_str_ary(self);
962backtrace_limit(
VALUE self)
964 return LONG2NUM(rb_backtrace_length_limit);
968rb_ec_backtrace_str_ary(
const rb_execution_context_t *ec,
long lev,
long n)
970 return rb_backtrace_to_str_ary(rb_ec_partial_backtrace_object(ec, lev, n, NULL, FALSE, FALSE));
974rb_ec_backtrace_location_ary(
const rb_execution_context_t *ec,
long lev,
long n,
bool skip_internal)
976 return rb_backtrace_to_location_ary(rb_ec_partial_backtrace_object(ec, lev, n, NULL, skip_internal, FALSE));
982backtrace_each(
const rb_execution_context_t *ec,
983 void (*init)(
void *arg,
size_t size),
984 void (*iter_iseq)(
void *arg,
const rb_control_frame_t *cfp),
985 void (*iter_cfunc)(
void *arg,
const rb_control_frame_t *cfp,
ID mid),
988 const rb_control_frame_t *last_cfp = ec->cfp;
989 const rb_control_frame_t *start_cfp = RUBY_VM_END_CONTROL_FRAME(ec);
990 const rb_control_frame_t *cfp;
994 if (start_cfp == NULL) {
1010 RUBY_VM_NEXT_CONTROL_FRAME(
1011 RUBY_VM_NEXT_CONTROL_FRAME(start_cfp));
1013 if (start_cfp < last_cfp) {
1017 size = start_cfp - last_cfp + 1;
1023 for (i=0, cfp = start_cfp; i<size; i++, cfp = RUBY_VM_NEXT_CONTROL_FRAME(cfp)) {
1027 iter_iseq(arg, cfp);
1031 VM_ASSERT(RUBYVM_CFUNC_FRAME_P(cfp));
1032 const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(cfp);
1033 ID mid = me->def->original_id;
1035 iter_cfunc(arg, cfp, mid);
1043 void (*func)(
void *data,
VALUE file,
int lineno,
VALUE name);
1048oldbt_init(
void *ptr,
size_t dmy)
1051 arg->filename = GET_VM()->progname;
1056oldbt_iter_iseq(
void *ptr,
const rb_control_frame_t *cfp)
1058 const rb_iseq_t *iseq = cfp->iseq;
1059 const VALUE *pc = cfp->pc;
1061 VALUE file = arg->filename = rb_iseq_path(iseq);
1062 VALUE name = ISEQ_BODY(iseq)->location.label;
1063 int lineno = arg->lineno = calc_lineno(iseq, pc);
1065 (arg->func)(arg->data, file, lineno, name);
1069oldbt_iter_cfunc(
void *ptr,
const rb_control_frame_t *cfp,
ID mid)
1072 VALUE file = arg->filename;
1073 VALUE name = rb_id2str(mid);
1074 int lineno = arg->lineno;
1076 (arg->func)(arg->data, file, lineno, name);
1080oldbt_print(
void *data,
VALUE file,
int lineno,
VALUE name)
1082 FILE *fp = (FILE *)data;
1085 fprintf(fp,
"\tfrom %s:%d:in unknown method\n",
1086 RSTRING_PTR(file), lineno);
1089 fprintf(fp,
"\tfrom %s:%d:in '%s'\n",
1090 RSTRING_PTR(file), lineno, RSTRING_PTR(name));
1095vm_backtrace_print(FILE *fp)
1099 arg.func = oldbt_print;
1100 arg.data = (
void *)fp;
1101 backtrace_each(GET_EC(),
1114oldbt_bugreport(
void *arg,
VALUE file,
int line,
VALUE method)
1118 const char *filename =
NIL_P(file) ?
"ruby" : RSTRING_PTR(file);
1120 fprintf(fp,
"-- Ruby level backtrace information "
1121 "----------------------------------------\n");
1124 if (
NIL_P(method)) {
1125 fprintf(fp,
"%s:%d:in unknown method\n", filename, line);
1128 fprintf(fp,
"%s:%d:in '%s'\n", filename, line, RSTRING_PTR(method));
1133rb_backtrace_print_as_bugreport(FILE *fp)
1138 arg.func = oldbt_bugreport;
1141 backtrace_each(GET_EC(),
1151 vm_backtrace_print(stderr);
1160oldbt_print_to(
void *data,
VALUE file,
int lineno,
VALUE name)
1163 VALUE str = rb_sprintf(
"\tfrom %"PRIsVALUE
":%d:in ", file, lineno);
1169 rb_str_catf(str,
" '%"PRIsVALUE
"'\n", name);
1171 (*arg->iter)(arg->output, str);
1181 parg.output = output;
1182 arg.func = oldbt_print_to;
1184 backtrace_each(GET_EC(),
1194 return rb_ec_backtrace_str_ary(GET_EC(), BACKTRACE_START, ALL_BACKTRACE_LINES);
1198ec_backtrace_range(
const rb_execution_context_t *ec,
int argc,
const VALUE *argv,
int lev_default,
int lev_plus,
long *len_ptr)
1200 VALUE level, vn, opts;
1208 if (argc == 2 &&
NIL_P(vn)) argc--;
1212 lev = lev_default + lev_plus;
1213 n = ALL_BACKTRACE_LINES;
1217 long beg,
len, bt_size = backtrace_size(ec);
1222 rb_raise(rb_eArgError,
"negative level (%ld)", lev);
1225 n = ALL_BACKTRACE_LINES;
1230 lev = beg + lev_plus;
1240 rb_raise(rb_eArgError,
"negative level (%ld)", lev);
1243 rb_raise(rb_eArgError,
"negative size (%ld)", n);
1257ec_backtrace_to_ary(
const rb_execution_context_t *ec,
int argc,
const VALUE *argv,
int lev_default,
int lev_plus,
int to_str)
1263 lev = ec_backtrace_range(ec, argc, argv, lev_default, lev_plus, &n);
1264 if (lev < 0)
return Qnil;
1270 btval = rb_ec_partial_backtrace_object(ec, lev, n, &too_large, FALSE, FALSE);
1277 r = backtrace_to_str_ary(btval);
1280 r = backtrace_to_location_ary(btval);
1287thread_backtrace_to_ary(
int argc,
const VALUE *argv,
VALUE thval,
int to_str)
1289 rb_thread_t *target_th = rb_thread_ptr(thval);
1291 if (target_th->to_kill || target_th->status == THREAD_KILLED)
1294 return ec_backtrace_to_ary(target_th->ec, argc, argv, 0, 0, to_str);
1298rb_vm_thread_backtrace(
int argc,
const VALUE *argv,
VALUE thval)
1300 return thread_backtrace_to_ary(argc, argv, thval, 1);
1304rb_vm_thread_backtrace_locations(
int argc,
const VALUE *argv,
VALUE thval)
1306 return thread_backtrace_to_ary(argc, argv, thval, 0);
1312 return ec_backtrace_to_ary(ec, argc, argv, 0, 0, 1);
1318 return ec_backtrace_to_ary(ec, argc, argv, 0, 0, 0);
1362 return ec_backtrace_to_ary(GET_EC(), argc, argv, 1, 1, 1);
1388rb_f_caller_locations(
int argc,
VALUE *argv,
VALUE _)
1390 return ec_backtrace_to_ary(GET_EC(), argc, argv, 1, 1, 0);
1401each_caller_location(
int argc,
VALUE *argv,
VALUE _)
1403 rb_execution_context_t *ec = GET_EC();
1404 long n, lev = ec_backtrace_range(ec, argc, argv, 1, 1, &n);
1405 if (lev >= 0 && n != 0) {
1406 rb_ec_partial_backtrace_object(ec, lev, n, NULL, FALSE, TRUE);
1413Init_vm_backtrace(
void)
1478 rb_define_method(rb_cBacktraceLocation,
"base_label", location_base_label_m, 0);
1480 rb_define_method(rb_cBacktraceLocation,
"absolute_path", location_absolute_path_m, 0);
1492RUBY_SYMBOL_EXPORT_BEGIN
1494RUBY_SYMBOL_EXPORT_END
1497 rb_execution_context_t *ec;
1498 rb_control_frame_t *cfp;
1501 long backtrace_size;
1505 CALLER_BINDING_SELF,
1506 CALLER_BINDING_CLASS,
1507 CALLER_BINDING_BINDING,
1508 CALLER_BINDING_ISEQ,
1510 CALLER_BINDING_DEPTH,
1515 const rb_execution_context_t *ec;
1519collect_caller_bindings_init(
void *arg,
size_t size)
1525get_klass(
const rb_control_frame_t *cfp)
1528 if (rb_vm_control_frame_id_and_class(cfp, 0, 0, &klass)) {
1530 return RBASIC(klass)->klass;
1542frame_depth(
const rb_execution_context_t *ec,
const rb_control_frame_t *cfp)
1544 VM_ASSERT(RUBY_VM_END_CONTROL_FRAME(ec) >= cfp);
1545 return (
int)(RUBY_VM_END_CONTROL_FRAME(ec) - cfp);
1549collect_caller_bindings_iseq(
void *arg,
const rb_control_frame_t *cfp)
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));
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 rb_ary_store(frame, CALLER_BINDING_DEPTH,
INT2FIX(frame_depth(data->ec, cfp)));
1561 rb_ary_push(data->ary, frame);
1565collect_caller_bindings_cfunc(
void *arg,
const rb_control_frame_t *cfp,
ID mid)
1570 rb_ary_store(frame, CALLER_BINDING_SELF, cfp->self);
1571 rb_ary_store(frame, CALLER_BINDING_CLASS, get_klass(cfp));
1572 rb_ary_store(frame, CALLER_BINDING_BINDING,
Qnil);
1573 rb_ary_store(frame, CALLER_BINDING_ISEQ,
Qnil);
1574 rb_ary_store(frame, CALLER_BINDING_CFP, GC_GUARDED_PTR(cfp));
1575 rb_ary_store(frame, CALLER_BINDING_DEPTH,
INT2FIX(frame_depth(data->ec, cfp)));
1577 rb_ary_push(data->ary, frame);
1581collect_caller_bindings(
const rb_execution_context_t *ec)
1590 collect_caller_bindings_init,
1591 collect_caller_bindings_iseq,
1592 collect_caller_bindings_cfunc,
1595 result = rb_ary_reverse(data.ary);
1599 VALUE entry = rb_ary_entry(result, i);
1600 VALUE cfp_val = rb_ary_entry(entry, CALLER_BINDING_BINDING);
1602 if (!
NIL_P(cfp_val)) {
1603 rb_control_frame_t *cfp = GC_GUARDED_PTR_REF(cfp_val);
1604 rb_ary_store(entry, CALLER_BINDING_BINDING, rb_vm_make_binding(ec, cfp));
1620 rb_execution_context_t *ec = GET_EC();
1621 enum ruby_tag_type state;
1622 volatile VALUE MAYBE_UNUSED(result);
1625 rb_vm_stack_to_heap(ec);
1627 dbg_context.ec = ec;
1628 dbg_context.cfp = dbg_context.ec->cfp;
1629 dbg_context.backtrace = rb_ec_backtrace_location_ary(ec, BACKTRACE_START, ALL_BACKTRACE_LINES, FALSE);
1630 dbg_context.backtrace_size =
RARRAY_LEN(dbg_context.backtrace);
1631 dbg_context.contexts = collect_caller_bindings(ec);
1634 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1635 result = (*func)(&dbg_context, data);
1642 EC_JUMP_TAG(ec, state);
1651 if (index < 0 || index >= dc->backtrace_size) {
1652 rb_raise(rb_eArgError,
"no such frame");
1654 return rb_ary_entry(dc->contexts, index);
1660 VALUE frame = frame_get(dc, index);
1661 return rb_ary_entry(frame, CALLER_BINDING_SELF);
1667 VALUE frame = frame_get(dc, index);
1668 return rb_ary_entry(frame, CALLER_BINDING_CLASS);
1674 VALUE frame = frame_get(dc, index);
1675 return rb_ary_entry(frame, CALLER_BINDING_BINDING);
1681 VALUE frame = frame_get(dc, index);
1682 VALUE iseq = rb_ary_entry(frame, CALLER_BINDING_ISEQ);
1684 return RTEST(iseq) ? rb_iseqw_new((rb_iseq_t *)iseq) :
Qnil;
1690 VALUE frame = frame_get(dc, index);
1691 return rb_ary_entry(frame, CALLER_BINDING_DEPTH);
1697 rb_execution_context_t *ec = GET_EC();
1698 return INT2FIX(frame_depth(ec, ec->cfp));
1704 return dc->backtrace;
1708thread_profile_frames(rb_execution_context_t *ec,
int start,
int limit,
VALUE *buff,
int *lines)
1711 const rb_control_frame_t *cfp = ec->cfp, *end_cfp = RUBY_VM_END_CONTROL_FRAME(ec);
1712 const rb_control_frame_t *top = cfp;
1713 const rb_callable_method_entry_t *cme;
1725 end_cfp = RUBY_VM_NEXT_CONTROL_FRAME(end_cfp);
1727 for (i=0; i<limit && cfp != end_cfp; cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp)) {
1728 if (VM_FRAME_RUBYFRAME_P(cfp) && cfp->pc != 0) {
1735 cme = rb_vm_frame_method_entry(cfp);
1736 if (cme && cme->def->type == VM_METHOD_TYPE_ISEQ) {
1737 buff[i] = (
VALUE)cme;
1740 buff[i] = (
VALUE)cfp->iseq;
1748 if (cfp == top && cfp->jit_return) {
1752 lines[i] = calc_lineno(cfp->iseq, cfp->pc);
1759 cme = rb_vm_frame_method_entry(cfp);
1760 if (cme && cme->def->type == VM_METHOD_TYPE_CFUNC) {
1765 buff[i] = (
VALUE)cme;
1766 if (lines) lines[i] = 0;
1778 rb_execution_context_t *ec = rb_current_execution_context(
false);
1786 return thread_profile_frames(ec, start, limit, buff, lines);
1792 rb_thread_t *th = rb_thread_ptr(thread);
1793 return thread_profile_frames(th->ec, start, limit, buff, lines);
1796static const rb_iseq_t *
1797frame2iseq(
VALUE frame)
1799 if (
NIL_P(frame))
return NULL;
1802 switch (imemo_type(frame)) {
1804 return (
const rb_iseq_t *)frame;
1807 const rb_callable_method_entry_t *cme = (rb_callable_method_entry_t *)frame;
1808 switch (cme->def->type) {
1809 case VM_METHOD_TYPE_ISEQ:
1810 return cme->def->body.iseq.
iseqptr;
1819 rb_bug(
"frame2iseq: unreachable");
1825 const rb_iseq_t *iseq = frame2iseq(frame);
1826 return iseq ? rb_iseq_path(iseq) :
Qnil;
1829static const rb_callable_method_entry_t *
1832 if (
NIL_P(frame))
return NULL;
1835 switch (imemo_type(frame)) {
1838 const rb_callable_method_entry_t *cme = (rb_callable_method_entry_t *)frame;
1839 switch (cme->def->type) {
1840 case VM_METHOD_TYPE_CFUNC:
1857 if (cframe(frame)) {
1861 rb_vm_register_global_object(cfunc_str);
1865 const rb_iseq_t *iseq = frame2iseq(frame);
1866 return iseq ? rb_iseq_realpath(iseq) :
Qnil;
1872 const rb_iseq_t *iseq = frame2iseq(frame);
1873 return iseq ? rb_iseq_label(iseq) :
Qnil;
1879 const rb_iseq_t *iseq = frame2iseq(frame);
1880 return iseq ? rb_iseq_base_label(iseq) :
Qnil;
1886 const rb_iseq_t *iseq = frame2iseq(frame);
1887 return iseq ? rb_iseq_first_lineno(iseq) :
Qnil;
1891frame2klass(
VALUE frame)
1896 const rb_callable_method_entry_t *cme = (rb_callable_method_entry_t *)frame;
1898 if (imemo_type(frame) == imemo_ment) {
1899 return cme->defined_class;
1908 VALUE klass = frame2klass(frame);
1910 if (klass && !
NIL_P(klass)) {
1912 klass =
RBASIC(klass)->klass;
1914 else if (RCLASS_SINGLETON_P(klass)) {
1915 klass = RCLASS_ATTACHED_OBJECT(klass);
1929 VALUE klass = frame2klass(frame);
1931 return RBOOL(klass && !
NIL_P(klass) && RCLASS_SINGLETON_P(klass));
1937 const rb_callable_method_entry_t *cme = cframe(frame);
1939 ID mid = cme->def->original_id;
1942 const rb_iseq_t *iseq = frame2iseq(frame);
1943 return iseq ? rb_iseq_method_name(iseq) :
Qnil;
1947qualified_method_name(
VALUE frame,
VALUE method_name)
1949 if (method_name !=
Qnil) {
1953 if (classpath !=
Qnil) {
1954 return rb_sprintf(
"%"PRIsVALUE
"%s%"PRIsVALUE,
1955 classpath, singleton_p ==
Qtrue ?
"." :
"#", method_name);
1971 return qualified_method_name(frame, method_name);
1977 const rb_callable_method_entry_t *cme = cframe(frame);
1979 ID mid = cme->def->original_id;
1980 VALUE method_name = id2str(mid);
1981 return qualified_method_name(frame, method_name);
1988 if (
NIL_P(qualified_method_name) || base_label == qualified_method_name) {
1992 long label_length = RSTRING_LEN(label);
1993 long base_label_length = RSTRING_LEN(base_label);
1994 int prefix_len =
rb_long2int(label_length - base_label_length);
1996 return rb_sprintf(
"%.*s%"PRIsVALUE, prefix_len, RSTRING_PTR(label), qualified_method_name);
#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.
struct rb_debug_inspector_struct rb_debug_inspector_t
Opaque struct representing a debug inspector.
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().
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.
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
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.
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define rb_str_cat2
Old name of rb_str_cat_cstr.
#define T_IMEMO
Old name of RUBY_T_IMEMO.
#define CLASS_OF
Old name of rb_class_of.
#define T_MODULE
Old name of RUBY_T_MODULE.
#define ASSUME
Old name of RBIMPL_ASSUME.
#define T_ICLASS
Old name of RUBY_T_ICLASS.
#define LONG2NUM
Old name of RB_LONG2NUM.
#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.
#define NIL_P
Old name of RB_NIL_P.
#define T_CLASS
Old name of RUBY_T_CLASS.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define rb_ary_new2
Old name of rb_ary_new_capa.
int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
Checks if the given object is of given kind.
VALUE rb_cArray
Array class.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_cThread
Thread class.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_range_beg_len(VALUE range, long *begp, long *lenp, long len, int err)
Deconstructs a numerical range.
#define rb_str_new_literal(str)
Just another name of rb_str_new_lit.
VALUE rb_str_inspect(VALUE str)
Generates a "readable" version of the receiver.
#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.
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
VALUE rb_make_backtrace(void)
Creates the good old fashioned array-of-strings style backtrace info.
void rb_backtrace(void)
Prints the backtrace out to the standard error.
int len
Length of the buffer.
VALUE rb_yield(VALUE val)
Yields the block.
#define rb_long2int
Just another name of rb_long2int_inline.
void rb_marshal_define_compat(VALUE newclass, VALUE oldclass, VALUE(*dumper)(VALUE), VALUE(*loader)(VALUE, VALUE))
Marshal format compatibility layer.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
VALUE type(ANYARGS)
ANYARGS-ed function type.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
#define RBASIC(obj)
Convenient casting macro.
#define RUBY_DEFAULT_FREE
This is a value you can set to RData::dfree.
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a 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...
const char * rb_class2name(VALUE klass)
Queries the name of the passed class.
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.