diff options
author | ko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-05-25 04:50:10 +0000 |
---|---|---|
committer | ko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-05-25 04:50:10 +0000 |
commit | 1f3142a4477a10bfe566c17837c2a0b9f6cba00f (patch) | |
tree | aaa2ab6627c58640564493cf8a3d3c5a2703b082 /vm_eval.c | |
parent | 26f96fe1ce6178a83a0b3f0dbb9b4b9c7692103e (diff) |
* vm.c: refactoring backtrace related funcitons.
(1) unify similar functions (rb_backtrace_each() and
backtrace_object()). backtrace_each() is a unified function.
variation:
a) backtrace_object(): create backtrace object.
b) vm_backtrace_str_ary(): create bt as an array of string.
c) vm_backtrace_print(): print backtrace to specified file.
d) rb_backtrace_print_as_bugreport(): print backtrace on
bugreport style.
(2) remove rb_backtrace_each(). Use backtrace_each() instead.
(3) chang the type of lev parameter to size_t.
a) lev == 0 means current frame (exception, etc use it).
b) lev == 1 means upper frame (caller(0) use it).
* vm_core.h, vm_dump.c, vm_eval.c: ditto.
* vm.c (backtrace_object(), vm_backtrace_str_ary()): fix to return a
correct size of caller(lev) array.
Let n be a "caller(0).size" then ln as caller(lev).size should be
(n - lev). However, the previous implementation returns a wrong
size array (ln > n - lev). [ruby-dev:45673]
* test/ruby/test_backtrace.rb: add tests for backtrace.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35787 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'vm_eval.c')
-rw-r--r-- | vm_eval.c | 41 |
1 files changed, 6 insertions, 35 deletions
@@ -20,9 +20,8 @@ static VALUE vm_exec(rb_thread_t *th); static void vm_set_eval_stack(rb_thread_t * th, VALUE iseqval, const NODE *cref); static int vm_collect_local_variables_in_heap(rb_thread_t *th, VALUE *dfp, VALUE ary); -static int vm_backtrace_each(rb_thread_t *th, int lev, ptrdiff_t n, void (*init)(void *), rb_backtrace_iter_func *iter, void *arg); -static VALUE backtrace_object(rb_thread_t *th, int lev, ptrdiff_t n); -static VALUE vm_backtrace_str_ary(rb_thread_t *th, int lev, int n); +static VALUE vm_backtrace_str_ary(rb_thread_t *th, size_t lev, size_t n); +static void vm_backtrace_print(FILE *fp); typedef enum call_type { CALL_PUBLIC, @@ -1087,7 +1086,7 @@ eval_string_with_cref(VALUE self, VALUE src, VALUE scope, NODE *cref, const char errat = rb_get_backtrace(errinfo); mesg = rb_attr_get(errinfo, id_mesg); if (!NIL_P(errat) && RB_TYPE_P(errat, T_ARRAY) && - (bt2 = vm_backtrace_str_ary(th, -2, 0), RARRAY_LEN(bt2) > 0)) { + (bt2 = vm_backtrace_str_ary(th, 0, 0), RARRAY_LEN(bt2) > 0)) { if (!NIL_P(mesg) && RB_TYPE_P(mesg, T_STRING) && !RSTRING_LEN(mesg)) { if (OBJ_FROZEN(mesg)) { VALUE m = rb_str_cat(rb_str_dup(RARRAY_PTR(errat)[0]), ": ", 2); @@ -1622,41 +1621,19 @@ rb_f_caller(int argc, VALUE *argv) if (lev < 0) rb_raise(rb_eArgError, "negative level (%d)", lev); - return vm_backtrace_str_ary(GET_THREAD(), lev, 0); -} - -static int -print_backtrace(void *arg, VALUE file, int line, VALUE method) -{ - FILE *fp = arg; - const char *filename = NIL_P(file) ? "ruby" : RSTRING_PTR(file); - if (NIL_P(method)) { - fprintf(fp, "\tfrom %s:%d:in unknown method\n", - filename, line); - } - else { - fprintf(fp, "\tfrom %s:%d:in `%s'\n", - filename, line, RSTRING_PTR(method)); - } - return FALSE; + return vm_backtrace_str_ary(GET_THREAD(), lev+1, 0); } void rb_backtrace(void) { - vm_backtrace_each(GET_THREAD(), -1, 0, NULL, print_backtrace, stderr); + vm_backtrace_print(stderr); } VALUE rb_make_backtrace(void) { - return vm_backtrace_str_ary(GET_THREAD(), -1, 0); -} - -VALUE -rb_vm_backtrace_object() -{ - return backtrace_object(GET_THREAD(), -1, 0); + return vm_backtrace_str_ary(GET_THREAD(), 0, 0); } VALUE @@ -1678,12 +1655,6 @@ rb_thread_backtrace(VALUE thval) return vm_backtrace_str_ary(th, 0, 0); } -int -rb_backtrace_each(rb_backtrace_iter_func *iter, void *arg) -{ - return vm_backtrace_each(GET_THREAD(), -1, 0, NULL, iter, arg); -} - /* * call-seq: * local_variables -> array |