int rb_sourceline(void) { rb_thread_t *th = GET_THREAD(); rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(th, th->cfp); if (cfp) { return rb_vm_get_sourceline(cfp); } else { return 0; } }
rb_method_entry_t * rb_add_method(VALUE klass, ID mid, rb_method_type_t type, void *opts, rb_method_flag_t noex) { rb_thread_t *th; rb_control_frame_t *cfp; int line; rb_method_entry_t *me = rb_method_entry_make(klass, mid, type, 0, noex); rb_method_definition_t *def = ALLOC(rb_method_definition_t); me->def = def; def->type = type; def->original_id = mid; def->alias_count = 0; switch (type) { case VM_METHOD_TYPE_ISEQ: def->body.iseq = (rb_iseq_t *)opts; break; case VM_METHOD_TYPE_CFUNC: def->body.cfunc = *(rb_method_cfunc_t *)opts; break; case VM_METHOD_TYPE_ATTRSET: case VM_METHOD_TYPE_IVAR: def->body.attr.id = (ID)opts; def->body.attr.location = Qfalse; th = GET_THREAD(); cfp = rb_vm_get_ruby_level_next_cfp(th, th->cfp); if (cfp && (line = rb_vm_get_sourceline(cfp))) { VALUE location = rb_ary_new3(2, cfp->iseq->filename, INT2FIX(line)); def->body.attr.location = rb_ary_freeze(location); } break; case VM_METHOD_TYPE_BMETHOD: def->body.proc = (VALUE)opts; break; case VM_METHOD_TYPE_NOTIMPLEMENTED: def->body.cfunc.func = rb_f_notimplement; def->body.cfunc.argc = -1; break; case VM_METHOD_TYPE_OPTIMIZED: def->body.optimize_type = (enum method_optimized_type)opts; break; case VM_METHOD_TYPE_ZSUPER: case VM_METHOD_TYPE_UNDEF: break; default: rb_bug("rb_add_method: unsupported method type (%d)\n", type); } if (type != VM_METHOD_TYPE_UNDEF) { method_added(klass, mid); } return me; }
static void fill_path_and_lineno(rb_trace_arg_t *trace_arg) { if (trace_arg->path == Qundef) { rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(trace_arg->th, trace_arg->cfp); if (cfp) { trace_arg->path = cfp->iseq->location.path; trace_arg->lineno = rb_vm_get_sourceline(cfp); } else { trace_arg->path = Qnil; trace_arg->lineno = 0; } } }
static int vm_backtrace_each(rb_thread_t *th, int lev, void (*init)(void *), rb_backtrace_iter_func *iter, void *arg) { const rb_control_frame_t *limit_cfp = th->cfp; const rb_control_frame_t *cfp = (void *)(th->stack + th->stack_size); VALUE file = Qnil; int line_no = 0; cfp -= 2; while (lev-- >= 0) { if (++limit_cfp > cfp) { return FALSE; } } if (init) (*init)(arg); limit_cfp = RUBY_VM_NEXT_CONTROL_FRAME(limit_cfp); if (th->vm->progname) file = th->vm->progname; while (cfp > limit_cfp) { if (cfp->iseq != 0) { if (cfp->pc != 0) { rb_iseq_t *iseq = cfp->iseq; line_no = rb_vm_get_sourceline(cfp); file = iseq->filename; if ((*iter)(arg, file, line_no, iseq->name)) break; } } else if (RUBYVM_CFUNC_FRAME_P(cfp)) { ID id; extern VALUE ruby_engine_name; if (NIL_P(file)) file = ruby_engine_name; if (cfp->me->def) id = cfp->me->def->original_id; else id = cfp->me->called_id; if (id != ID_ALLOCATOR && (*iter)(arg, file, line_no, rb_id2str(id))) break; } cfp = RUBY_VM_NEXT_CONTROL_FRAME(cfp); } return TRUE; }
static VALUE binding_of_caller(VALUE self, VALUE rb_level) { rb_thread_t *th; GetThreadPtr(rb_thread_current(), th); rb_control_frame_t *cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp); rb_control_frame_t *limit_cfp = (void *)(th->stack + th->stack_size); int level = FIX2INT(rb_level); // attempt to locate the nth parent control frame for (int i = 0; i < level; i++) { cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp); if (cfp >= limit_cfp) rb_raise(rb_eRuntimeError, "Invalid frame, gone beyond end of stack!"); // skip invalid frames if (!valid_frame_p(cfp, limit_cfp)) cfp = find_valid_frame(cfp, limit_cfp); } VALUE bindval = binding_alloc(rb_cBinding); rb_binding_t *bind; if (cfp == 0) rb_raise(rb_eRuntimeError, "Can't create Binding Object on top of Fiber."); GetBindingPtr(bindval, bind); bind->env = rb_vm_make_env_object(th, cfp); bind->filename = cfp->iseq->filename; bind->line_no = rb_vm_get_sourceline(cfp); rb_iv_set(bindval, "@frame_type", frametype_name(cfp->flag)); rb_iv_set(bindval, "@frame_description", cfp->iseq->name); return bindval; }
VALUE rb_thread_current_status(const rb_thread_t *th) { const rb_control_frame_t *cfp = th->cfp; VALUE str = Qnil; if (cfp->iseq != 0) { if (cfp->pc != 0) { rb_iseq_t *iseq = cfp->iseq; int line_no = rb_vm_get_sourceline(cfp); char *file = RSTRING_PTR(iseq->filename); str = rb_sprintf("%s:%d:in `%s'", file, line_no, RSTRING_PTR(iseq->name)); } } else if (cfp->me->def->original_id) { str = rb_sprintf("`%s#%s' (cfunc)", RSTRING_PTR(rb_class_name(cfp->me->klass)), rb_id2name(cfp->me->def->original_id)); } return str; }
static void control_frame_dump(rb_thread_t *th, rb_control_frame_t *cfp) { ptrdiff_t pc = -1, bp = -1; ptrdiff_t lfp = cfp->lfp - th->stack; ptrdiff_t dfp = cfp->dfp - th->stack; char lfp_in_heap = ' ', dfp_in_heap = ' '; char posbuf[MAX_POSBUF+1]; int line = 0; int nopos = 0; const char *magic, *iseq_name = "-", *selfstr = "-", *biseq_name = "-"; VALUE tmp; if (cfp->block_iseq != 0 && BUILTIN_TYPE(cfp->block_iseq) != T_NODE) { biseq_name = ""; /* RSTRING(cfp->block_iseq->name)->ptr; */ } if (lfp < 0 || (size_t)lfp > th->stack_size) { lfp = (ptrdiff_t)cfp->lfp; lfp_in_heap = 'p'; } if (dfp < 0 || (size_t)dfp > th->stack_size) { dfp = (ptrdiff_t)cfp->dfp; dfp_in_heap = 'p'; } if (cfp->bp) { bp = cfp->bp - th->stack; } switch (VM_FRAME_TYPE(cfp)) { case VM_FRAME_MAGIC_TOP: magic = "TOP"; break; case VM_FRAME_MAGIC_METHOD: magic = "METHOD"; break; case VM_FRAME_MAGIC_CLASS: magic = "CLASS"; break; case VM_FRAME_MAGIC_BLOCK: magic = "BLOCK"; break; case VM_FRAME_MAGIC_FINISH: magic = "FINISH"; nopos = 1; break; case VM_FRAME_MAGIC_CFUNC: magic = "CFUNC"; break; case VM_FRAME_MAGIC_PROC: magic = "PROC"; break; case VM_FRAME_MAGIC_LAMBDA: magic = "LAMBDA"; break; case VM_FRAME_MAGIC_IFUNC: magic = "IFUNC"; break; case VM_FRAME_MAGIC_EVAL: magic = "EVAL"; break; case 0: magic = "------"; break; default: magic = "(none)"; break; } if (0) { tmp = rb_inspect(cfp->self); selfstr = StringValueCStr(tmp); } else { selfstr = ""; } if (nopos) { /* no name */ } else if (cfp->iseq != 0) { if (RUBY_VM_IFUNC_P(cfp->iseq)) { iseq_name = "<ifunc>"; } else { pc = cfp->pc - cfp->iseq->iseq_encoded; iseq_name = RSTRING_PTR(cfp->iseq->name); line = rb_vm_get_sourceline(cfp); if (line) { snprintf(posbuf, MAX_POSBUF, "%s:%d", RSTRING_PTR(cfp->iseq->filename), line); } } } else if (cfp->me) { iseq_name = rb_id2name(cfp->me->def->original_id); snprintf(posbuf, MAX_POSBUF, ":%s", iseq_name); line = -1; } fprintf(stderr, "c:%04"PRIdPTRDIFF" ", ((rb_control_frame_t *)(th->stack + th->stack_size) - cfp)); if (pc == -1) { fprintf(stderr, "p:---- "); } else { fprintf(stderr, "p:%04"PRIdPTRDIFF" ", pc); } fprintf(stderr, "s:%04"PRIdPTRDIFF" b:%04"PRIdPTRDIFF" ", (cfp->sp - th->stack), bp); fprintf(stderr, lfp_in_heap == ' ' ? "l:%06"PRIdPTRDIFF" " : "l:%06"PRIxPTRDIFF" ", lfp % 10000); fprintf(stderr, dfp_in_heap == ' ' ? "d:%06"PRIdPTRDIFF" " : "d:%06"PRIxPTRDIFF" ", dfp % 10000); fprintf(stderr, "%-6s", magic); if (line && !nopos) { fprintf(stderr, " %s", posbuf); } if (0) { fprintf(stderr, " \t"); fprintf(stderr, "iseq: %-24s ", iseq_name); fprintf(stderr, "self: %-24s ", selfstr); fprintf(stderr, "%-1s ", biseq_name); } fprintf(stderr, "\n"); }
static void control_frame_dump(rb_thread_t *th, rb_control_frame_t *cfp) { ptrdiff_t pc = -1; ptrdiff_t ep = cfp->ep - th->stack; char ep_in_heap = ' '; char posbuf[MAX_POSBUF+1]; int line = 0; const char *magic, *iseq_name = "-", *selfstr = "-", *biseq_name = "-"; VALUE tmp; const rb_callable_method_entry_t *me; if (ep < 0 || (size_t)ep > th->stack_size) { ep = (ptrdiff_t)cfp->ep; ep_in_heap = 'p'; } switch (VM_FRAME_TYPE(cfp)) { case VM_FRAME_MAGIC_TOP: magic = "TOP"; break; case VM_FRAME_MAGIC_METHOD: magic = "METHOD"; break; case VM_FRAME_MAGIC_CLASS: magic = "CLASS"; break; case VM_FRAME_MAGIC_BLOCK: magic = "BLOCK"; break; case VM_FRAME_MAGIC_CFUNC: magic = "CFUNC"; break; case VM_FRAME_MAGIC_PROC: magic = "PROC"; break; case VM_FRAME_MAGIC_LAMBDA: magic = "LAMBDA"; break; case VM_FRAME_MAGIC_IFUNC: magic = "IFUNC"; break; case VM_FRAME_MAGIC_EVAL: magic = "EVAL"; break; case VM_FRAME_MAGIC_RESCUE: magic = "RESCUE"; break; case 0: magic = "------"; break; default: magic = "(none)"; break; } if (0) { tmp = rb_inspect(cfp->self); selfstr = StringValueCStr(tmp); } else { selfstr = ""; } if (cfp->iseq != 0) { #define RUBY_VM_IFUNC_P(ptr) (RB_TYPE_P((VALUE)(ptr), T_IMEMO) && imemo_type((VALUE)ptr) == imemo_ifunc) if (RUBY_VM_IFUNC_P(cfp->iseq)) { iseq_name = "<ifunc>"; } else if (SYMBOL_P(cfp->iseq)) { tmp = rb_sym2str((VALUE)cfp->iseq); iseq_name = RSTRING_PTR(tmp); snprintf(posbuf, MAX_POSBUF, ":%s", iseq_name); line = -1; } else { pc = cfp->pc - cfp->iseq->body->iseq_encoded; iseq_name = RSTRING_PTR(cfp->iseq->body->location.label); line = rb_vm_get_sourceline(cfp); if (line) { snprintf(posbuf, MAX_POSBUF, "%s:%d", RSTRING_PTR(cfp->iseq->body->location.path), line); } } } else if ((me = rb_vm_frame_method_entry(cfp)) != NULL) { iseq_name = rb_id2name(me->def->original_id); snprintf(posbuf, MAX_POSBUF, ":%s", iseq_name); line = -1; } fprintf(stderr, "c:%04"PRIdPTRDIFF" ", ((rb_control_frame_t *)(th->stack + th->stack_size) - cfp)); if (pc == -1) { fprintf(stderr, "p:---- "); } else { fprintf(stderr, "p:%04"PRIdPTRDIFF" ", pc); } fprintf(stderr, "s:%04"PRIdPTRDIFF" ", cfp->sp - th->stack); fprintf(stderr, ep_in_heap == ' ' ? "e:%06"PRIdPTRDIFF" " : "E:%06"PRIxPTRDIFF" ", ep % 10000); fprintf(stderr, "%-6s", magic); if (line) { fprintf(stderr, " %s", posbuf); } if (VM_FRAME_FINISHED_P(cfp)) { fprintf(stderr, " [FINISH]"); } if (0) { fprintf(stderr, " \t"); fprintf(stderr, "iseq: %-24s ", iseq_name); fprintf(stderr, "self: %-24s ", selfstr); fprintf(stderr, "%-1s ", biseq_name); } fprintf(stderr, "\n"); }
static VALUE stacktrace(int argc, VALUE* argv, rb_thread_t *th) { VALUE ary = rb_ary_new(); int start = 0; int finish = -1; int stack_size = 0; const rb_control_frame_t *cfp = th->cfp; const rb_control_frame_t *tcfp; const rb_control_frame_t *limit_cfp = (void *)(th->stack + th->stack_size); VALUE file = Qnil; int line = 0; rb_iseq_t *iseq = 0; ID id; VALUE frame; extern VALUE ruby_engine_name; int flags = ST_F_ALL; if (argc > 0) { start = NUM2INT(argv[0]); } if (argc > 1) { finish = NUM2INT(argv[1]); } if (argc > 2) { flags = NUM2INT(argv[2]); } cfp += 1; limit_cfp -= 2; if (finish > 0) { finish--; } if (start < 0 || finish < 0) { tcfp = cfp; while (tcfp < limit_cfp) { if (tcfp->iseq != 0 && cfp->pc != 0) { stack_size++; } else if (RUBYVM_CFUNC_FRAME_P(tcfp)) { stack_size++; } tcfp++; } if (start < 0) { start = stack_size + start; } if (finish < 0) { finish = stack_size + finish; } } // rb_warn("flags: %i", flags & ST_F_KLASS); // rb_warn("test %i %i cfp: %i lcfp %i ss %i", start, finish, cfp, limit_cfp, stack_size); while (cfp < limit_cfp) { VALUE hash = 0; if (cfp->iseq != 0 && cfp->pc != 0) { if (start-- > 0) {cfp++; continue;} if (finish-- < 0) break; iseq = cfp->iseq; frame = rb_class_new_instance(0, 0, c_StackFrame); if (iseq->defined_method_id && ((flags & ST_F_KLASS) == ST_F_KLASS)) { rb_iv_set(frame, "@klass", iseq->klass); } if ((flags & ST_F_METHOD) == ST_F_METHOD) { rb_iv_set(frame, "@method", iseq->name); } if ((flags & ST_F_FILENAME) == ST_F_FILENAME) { rb_iv_set(frame, "@filename", iseq->filename); } if ((flags & ST_F_LINENUMBER) == ST_F_LINENUMBER) { line = rb_vm_get_sourceline(cfp); rb_iv_set(frame, "@line_number", INT2FIX(line)); } rb_ary_push(ary, frame); } else if (RUBYVM_CFUNC_FRAME_P(cfp)) { if (start-- > 0) {cfp++; continue;} if (finish-- < 0) break; if (NIL_P(file)) file = ruby_engine_name; if (cfp->me->def) id = cfp->me->def->original_id; else id = cfp->me->called_id; if (id != ID_ALLOCATOR) { frame = rb_class_new_instance(0, 0, c_StackFrame); if ((flags & ST_F_KLASS) == ST_F_KLASS) { rb_iv_set(frame, "@klass", cfp->me->klass); } if ((flags & ST_F_METHOD) == ST_F_METHOD) { rb_iv_set(frame, "@method", rb_id2str(id)); } rb_ary_push(ary,frame); } } cfp += 1; } return ary; }
rb_method_entry_t * rb_add_method(VALUE klass, ID mid, rb_method_type_t type, void *opts, rb_method_flag_t noex) { rb_thread_t *th; rb_control_frame_t *cfp; int line; rb_method_entry_t *me = rb_method_entry_make(klass, mid, type, 0, noex, klass); rb_method_definition_t *def = ALLOC(rb_method_definition_t); if (me->def && me->def->type == VM_METHOD_TYPE_REFINED) { me->def->body.orig_me->def = def; } else { me->def = def; } def->type = type; def->original_id = mid; def->alias_count = 0; switch (type) { case VM_METHOD_TYPE_ISEQ: { rb_iseq_t *iseq = (rb_iseq_t *)opts; *(rb_iseq_t **)&def->body.iseq = iseq; RB_OBJ_WRITTEN(klass, Qundef, iseq->self); break; } case VM_METHOD_TYPE_CFUNC: { rb_method_cfunc_t *cfunc = (rb_method_cfunc_t *)opts; setup_method_cfunc_struct(&def->body.cfunc, cfunc->func, cfunc->argc); } break; case VM_METHOD_TYPE_ATTRSET: case VM_METHOD_TYPE_IVAR: def->body.attr.id = (ID)(VALUE)opts; RB_OBJ_WRITE(klass, &def->body.attr.location, Qfalse); th = GET_THREAD(); cfp = rb_vm_get_ruby_level_next_cfp(th, th->cfp); if (cfp && (line = rb_vm_get_sourceline(cfp))) { VALUE location = rb_ary_new3(2, cfp->iseq->location.path, INT2FIX(line)); RB_OBJ_WRITE(klass, &def->body.attr.location, rb_ary_freeze(location)); } break; case VM_METHOD_TYPE_BMETHOD: RB_OBJ_WRITE(klass, &def->body.proc, (VALUE)opts); break; case VM_METHOD_TYPE_NOTIMPLEMENTED: setup_method_cfunc_struct(&def->body.cfunc, rb_f_notimplement, -1); break; case VM_METHOD_TYPE_OPTIMIZED: def->body.optimize_type = (enum method_optimized_type)opts; break; case VM_METHOD_TYPE_ZSUPER: case VM_METHOD_TYPE_UNDEF: break; case VM_METHOD_TYPE_REFINED: def->body.orig_me = (rb_method_entry_t *) opts; break; default: rb_bug("rb_add_method: unsupported method type (%d)\n", type); } if (type != VM_METHOD_TYPE_UNDEF && type != VM_METHOD_TYPE_REFINED) { method_added(klass, mid); } return me; }
VALUE rb_RPRuby_Sender_Kernel_internal_backtraceHashForControlFrame( rb_control_frame_t** c_current_frame ) { const char* c_method_name = NULL; int c_sourcefile_line = 0; // create new hash for this frame VALUE rb_frame_hash = rb_hash_new(); VALUE rb_sourcefile_name = Qnil; VALUE rb_sourcefile_line = Qnil; VALUE rb_method_name = Qnil; VALUE rb_object_for_frame = Qnil; if ( ( *c_current_frame )->iseq != 0 ) { if ( ( *c_current_frame )->pc != 0 ) { rb_iseq_t *iseq = ( *c_current_frame )->iseq; // get sourcefile name and set in hash rb_sourcefile_name = iseq->filename; // get sourcefile line and set in hash c_sourcefile_line = rb_vm_get_sourceline( *c_current_frame ); rb_sourcefile_line = INT2FIX( c_sourcefile_line ); // get name of instruction sequence rb_method_name = ID2SYM( rb_intern( StringValuePtr( iseq->name ) ) ); rb_object_for_frame = ( *c_current_frame )->self; } } else if ( RUBYVM_CFUNC_FRAME_P( *c_current_frame ) ) { // get name of method #if RUBY_PATCHLEVEL >= -1 // For 1.9.2: const rb_method_entry_t* c_method_for_frame = ( *c_current_frame )->me; c_method_name = rb_id2name( c_method_for_frame->called_id ); #else // For 1.9.1: c_method_name = rb_id2name( ( *c_current_frame )->method_id ); #endif rb_method_name = ( c_method_name == NULL ? Qnil : ID2SYM( rb_intern( c_method_name ) ) ); rb_object_for_frame = ( *c_current_frame )->self; } // we have to test this case - it works for blocks but there may be other cases too else if ( ( *c_current_frame )->block_iseq != 0 && ( *c_current_frame )->pc == 0) { // If we got here we have a fiber // There doesn't seem to be much that we can tell about a fiber's context VALUE rb_current_fiber = rb_fiber_current(); rb_fiber_t* c_current_fiber = NULL; GetFiberPtr( rb_current_fiber, c_current_fiber); rb_context_t* c_context = & c_current_fiber->cont; // rb_block_t* c_blockptr = RUBY_VM_GET_BLOCK_PTR_IN_CFP( *c_current_frame ); rb_object_for_frame = ( *c_current_frame )->self; // get sourcefile name and set in hash rb_sourcefile_name = Qnil; // get sourcefile line and set in hash rb_sourcefile_line = Qnil; // get name of instruction sequence rb_method_name = rb_str_new2( "<Fiber>" ); // if we have a fiber we also include its ruby reference since we have so little other context rb_hash_aset( rb_frame_hash, ID2SYM( rb_intern( "fiber" ) ), c_context->self ); // The one time that we know a fiber is in use in the Ruby base is with Enumerators // For now we will handle that with a special case VALUE rb_enumerator_class = rb_const_get( rb_cObject, rb_intern( "Enumerator" ) ); VALUE rb_object_for_frame_klass = ( ( TYPE( rb_object_for_frame ) == T_CLASS ) ? rb_object_for_frame : rb_funcall( rb_object_for_frame, rb_intern( "class" ), 0 ) ); VALUE rb_ancestors = rb_funcall( rb_object_for_frame_klass, rb_intern( "ancestors" ), 0 ); if ( rb_ary_includes( rb_ancestors, rb_enumerator_class ) ) { struct enumerator* c_enumerator = enumerator_ptr( rb_object_for_frame ); rb_object_for_frame = c_enumerator->obj; rb_method_name = ID2SYM( c_enumerator->meth ); } } else if ( ( *c_current_frame )->block_iseq == 0 && ( *c_current_frame )->pc == 0) { // this happens after we had a fiber and we try to go up - which doesn't make sense with a fiber // not sure what we want to do here, if anything return Qnil; } else { // The third possibility is that we have an iseq frame with nil params for what we want // In that case we can simply return the next frame *c_current_frame = RUBY_VM_PREVIOUS_CONTROL_FRAME( *c_current_frame ); // in theory this could crash because we are going forward a frame when we don't know what's there // in practice I think we are ok, since we are only jumping forward from nil frames which should never be at the end // at least - I don't think they should... we shall see. // // a fix would be to check the next frame, but that requires access to the thread or the limit cfp, // which requires passing more context; so for now, I'm leaving it there return rb_RPRuby_Sender_Kernel_internal_backtraceHashForControlFrame( c_current_frame ); } // Push values to return hash rb_hash_aset( rb_frame_hash, ID2SYM( rb_intern( "object" ) ), rb_object_for_frame ); rb_hash_aset( rb_frame_hash, ID2SYM( rb_intern( "file" ) ), rb_sourcefile_name ); rb_hash_aset( rb_frame_hash, ID2SYM( rb_intern( "line" ) ), rb_sourcefile_line ); rb_hash_aset( rb_frame_hash, ID2SYM( rb_intern( "method" ) ), rb_method_name ); return rb_frame_hash; }