mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Merge pull request #524 from iij/pr-backtrace
show backtrace when mruby dies on unhandled exception
This commit is contained in:
@@ -189,6 +189,7 @@ exc_debug_info(mrb_state *mrb, struct RObject *exc)
|
||||
mrb_callinfo *ci = mrb->ci;
|
||||
mrb_code *pc = ci->pc;
|
||||
|
||||
mrb_obj_iv_set(mrb, exc, mrb_intern(mrb, "ciidx"), mrb_fixnum_value(ci - mrb->cibase));
|
||||
ci--;
|
||||
while (ci >= mrb->cibase) {
|
||||
if (ci->proc && !MRB_PROC_CFUNC_P(ci->proc)) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user