From 9219029d3c0a3b5ceccd7d1606a1fce48d4e012d Mon Sep 17 00:00:00 2001 From: mame Date: Thu, 20 May 2010 13:07:58 +0000 Subject: * vm.c (vm_backtrace_each): now takes an init function to distinguish an empty stack from out of stack. [ruby-dev:41366] * vm_eval.c (print_backtrace, rb_thread_backtrace): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@27924 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- vm.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'vm.c') diff --git a/vm.c b/vm.c index 084c8c853e..ac1d8ed753 100644 --- a/vm.c +++ b/vm.c @@ -706,19 +706,20 @@ rb_vm_get_sourceline(const rb_control_frame_t *cfp) } static int -vm_backtrace_each(rb_thread_t *th, int lev, rb_backtrace_iter_func *iter, void *arg) +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; + VALUE file = Qnil, *aryp = arg; int line_no = 0; cfp -= 2; while (lev-- >= 0) { - if (++limit_cfp >= cfp) { + 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) { @@ -747,15 +748,19 @@ vm_backtrace_each(rb_thread_t *th, int lev, rb_backtrace_iter_func *iter, void * return TRUE; } +static void +vm_backtrace_alloc(void *arg) +{ + VALUE *aryp = arg; + *aryp = rb_ary_new(); +} + static int vm_backtrace_push(void *arg, VALUE file, int line_no, VALUE name) { VALUE *aryp = arg; VALUE bt; - if (!*aryp) { - *aryp = rb_ary_new(); - } bt = rb_enc_sprintf(rb_enc_compatible(file, name), "%s:%d:in `%s'", RSTRING_PTR(file), line_no, RSTRING_PTR(name)); rb_ary_push(*aryp, bt); @@ -770,7 +775,7 @@ vm_backtrace(rb_thread_t *th, int lev) if (lev < 0) { ary = rb_ary_new(); } - vm_backtrace_each(th, lev, vm_backtrace_push, &ary); + vm_backtrace_each(th, lev, vm_backtrace_alloc, vm_backtrace_push, &ary); if (!ary) return Qnil; return rb_ary_reverse(ary); } -- cgit v1.2.3