add pic_print_backtrace function

This commit is contained in:
Yuichi Nishiwaki 2014-04-06 14:00:45 +09:00
parent a8de062bcb
commit 920524dc40
3 changed files with 35 additions and 17 deletions

View File

@ -187,6 +187,7 @@ noreturn void pic_abort(pic_state *, const char *);
noreturn void pic_errorf(pic_state *, const char *, ...); noreturn void pic_errorf(pic_state *, const char *, ...);
void pic_warnf(pic_state *, const char *, ...); void pic_warnf(pic_state *, const char *, ...);
pic_str *pic_get_backtrace(pic_state *); pic_str *pic_get_backtrace(pic_state *);
void pic_print_backtrace(pic_state *, struct pic_error *);
/* obsoleted */ /* obsoleted */
noreturn static inline void pic_error(pic_state *pic, const char *msg) noreturn static inline void pic_error(pic_state *pic, const char *msg)

View File

@ -14,11 +14,38 @@ pic_get_backtrace(pic_state *pic)
pic_callinfo *ci; pic_callinfo *ci;
pic_str *trace; pic_str *trace;
trace = pic_str_new(pic, NULL, 0);
for (ci = pic->ci; ci != pic->cibase; --ci) {
struct pic_proc *proc = pic_proc_ptr(ci->fp[0]);
trace = pic_strcat(pic, trace, pic_str_new_cstr(pic, " at "));
trace = pic_strcat(pic, trace, pic_str_new_cstr(pic, pic_symbol_name(pic, pic_proc_name(proc))));
if (pic_proc_func_p(proc)) {
trace = pic_strcat(pic, trace, pic_str_new_cstr(pic, " (native function)\n"));
} else if (pic_proc_irep_p(proc)) {
trace = pic_strcat(pic, trace, pic_str_new_cstr(pic, " (unknown location)\n")); /* TODO */
}
}
pic_gc_arena_restore(pic, ai);
pic_gc_protect(pic, pic_obj_value(trace));
return trace;
}
void
pic_print_backtrace(pic_state *pic, struct pic_error *e)
{
int ai = pic_gc_arena_preserve(pic);
pic_str *trace;
assert(pic->err != NULL); assert(pic->err != NULL);
trace = pic_str_new(pic, NULL, 0); trace = pic_str_new(pic, NULL, 0);
switch (pic->err->type) { switch (e->type) {
case PIC_ERROR_OTHER: case PIC_ERROR_OTHER:
trace = pic_strcat(pic, trace, pic_str_new_cstr(pic, "error: ")); trace = pic_strcat(pic, trace, pic_str_new_cstr(pic, "error: "));
break; break;
@ -33,25 +60,15 @@ pic_get_backtrace(pic_state *pic)
break; break;
} }
trace = pic_strcat(pic, trace, pic->err->msg); trace = pic_strcat(pic, trace, e->msg);
/* TODO: print error irritants */ /* TODO: print error irritants */
for (ci = pic->ci; ci != pic->cibase; --ci) { trace = pic_strcat(pic, trace, pic_str_new(pic, "\n", 1));
struct pic_proc *proc = pic_proc_ptr(ci->fp[0]); trace = pic_strcat(pic, trace, pic_get_backtrace(pic));
trace = pic_strcat(pic, trace, pic_str_new_cstr(pic, "\n at ")); /* print! */
trace = pic_strcat(pic, trace, pic_str_new_cstr(pic, pic_symbol_name(pic, pic_proc_name(proc)))); printf("%s", pic_str_cstr(trace));
if (pic_proc_func_p(proc)) {
trace = pic_strcat(pic, trace, pic_str_new_cstr(pic, " (native function)"));
} else if (pic_proc_irep_p(proc)) {
trace = pic_strcat(pic, trace, pic_str_new_cstr(pic, " (unknown location)")); /* TODO */
}
}
pic_gc_arena_restore(pic, ai); pic_gc_arena_restore(pic, ai);
pic_gc_protect(pic, pic_obj_value(trace));
return trace;
} }

View File

@ -142,7 +142,7 @@ repl(pic_state *pic)
} }
} }
pic_catch { pic_catch {
printf("%s\n", pic_str_cstr(pic_get_backtrace(pic))); pic_print_backtrace(pic, pic->err);
pic->err = NULL; pic->err = NULL;
code[0] = '\0'; code[0] = '\0';
} }