diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2012-11-02 07:38:00 -0700 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2012-11-02 07:38:00 -0700 |
| commit | b06d8e2ab8773324f067c76c6cacfde672bd66b7 (patch) | |
| tree | 0cb124737bb5b8cea98c44ddb795432295639da8 /tools | |
| parent | c73dffb684d7a32bc83997d2a7d5dd61f215e588 (diff) | |
| parent | 43f4934e6d6ef14e38baf1f007f2ccc09d2781dc (diff) | |
| download | mruby-b06d8e2ab8773324f067c76c6cacfde672bd66b7.tar.gz mruby-b06d8e2ab8773324f067c76c6cacfde672bd66b7.zip | |
Merge pull request #524 from iij/pr-backtrace
show backtrace when mruby dies on unhandled exception
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/mruby/mruby.c | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/tools/mruby/mruby.c b/tools/mruby/mruby.c index db26d23a1..acecc7c8e 100644 --- a/tools/mruby/mruby.c +++ b/tools/mruby/mruby.c @@ -4,6 +4,7 @@ #include "mruby/string.h" #include "mruby/compile.h" #include "mruby/dump.h" +#include "mruby/variable.h" #include <stdio.h> #include <string.h> @@ -162,6 +163,54 @@ cleanup(mrb_state *mrb, struct _args *args) mrb_close(mrb); } +static void +showcallinfo(mrb_state *mrb) +{ + mrb_callinfo *ci; + mrb_int ciidx; + const char *filename, *method, *sep; + int i, line; + + printf("trace:\n"); + ciidx = mrb_fixnum(mrb_obj_iv_get(mrb, mrb->exc, mrb_intern(mrb, "ciidx"))); + if (ciidx >= mrb->ciend - mrb->cibase) + ciidx = 10; /* ciidx is broken... */ + + for (i = ciidx; i >= 0; i--) { + ci = &mrb->cibase[i]; + filename = "(unknown)"; + line = -1; + + if (MRB_PROC_CFUNC_P(ci->proc)) { + filename = "(cfunc)"; + } else { + mrb_irep *irep = ci->proc->body.irep; + if (irep->filename != NULL) + filename = irep->filename; + if (irep->lines != NULL && i+1 <= ciidx) { + mrb_code *pc = mrb->cibase[i+1].pc; + if (irep->iseq <= pc && pc < irep->iseq + irep->ilen) { + line = irep->lines[pc - irep->iseq - 1]; + } + } + } + if (ci->target_class == ci->proc->target_class) + sep = "."; + else + sep = "#"; + + method = mrb_sym2name(mrb, ci->mid); + if (method == NULL) + method = "(???)"; + + printf("\t[%d] %s:%d:in %s%s%s\n", + i, filename, line, + mrb_class_name(mrb, ci->proc->target_class), + sep, + method); + } +} + int main(int argc, char **argv) { @@ -198,6 +247,7 @@ main(int argc, char **argv) mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb)); n = 0; if (mrb->exc) { + showcallinfo(mrb); p(mrb, mrb_obj_value(mrb->exc)); n = -1; } @@ -223,6 +273,7 @@ main(int argc, char **argv) mrbc_context_free(mrb, c); if (mrb->exc) { if (!mrb_undef_p(v)) { + showcallinfo(mrb); p(mrb, mrb_obj_value(mrb->exc)); } n = -1; |
