picrin/debug.c

75 lines
1.8 KiB
C
Raw Normal View History

2014-08-25 00:38:09 -04:00
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
#include "picrin/string.h"
#include "picrin/error.h"
#include "picrin/proc.h"
pic_str *
pic_get_backtrace(pic_state *pic)
{
size_t ai = pic_gc_arena_preserve(pic);
pic_callinfo *ci;
pic_str *trace;
2014-09-12 06:52:49 -04:00
trace = pic_make_str(pic, NULL, 0);
2014-08-25 00:38:09 -04:00
for (ci = pic->ci; ci != pic->cibase; --ci) {
struct pic_proc *proc = pic_proc_ptr(ci->fp[0]);
2014-09-12 06:52:49 -04:00
trace = pic_strcat(pic, trace, pic_make_str_cstr(pic, " at "));
trace = pic_strcat(pic, trace, pic_make_str_cstr(pic, pic_symbol_name(pic, pic_proc_name(proc))));
2014-08-25 00:38:09 -04:00
if (pic_proc_func_p(proc)) {
2014-09-12 06:52:49 -04:00
trace = pic_strcat(pic, trace, pic_make_str_cstr(pic, " (native function)\n"));
2014-08-25 00:38:09 -04:00
} else if (pic_proc_irep_p(proc)) {
2014-09-12 06:52:49 -04:00
trace = pic_strcat(pic, trace, pic_make_str_cstr(pic, " (unknown location)\n")); /* TODO */
2014-08-25 00:38:09 -04:00
}
}
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)
{
size_t ai = pic_gc_arena_preserve(pic);
pic_str *trace;
assert(pic->err != NULL);
2014-09-12 06:52:49 -04:00
trace = pic_make_str(pic, NULL, 0);
2014-08-25 00:38:09 -04:00
switch (e->type) {
case PIC_ERROR_OTHER:
2014-09-12 06:52:49 -04:00
trace = pic_strcat(pic, trace, pic_make_str_cstr(pic, "error: "));
2014-08-25 00:38:09 -04:00
break;
case PIC_ERROR_FILE:
2014-09-12 06:52:49 -04:00
trace = pic_strcat(pic, trace, pic_make_str_cstr(pic, "file error: "));
2014-08-25 00:38:09 -04:00
break;
case PIC_ERROR_READ:
2014-09-12 06:52:49 -04:00
trace = pic_strcat(pic, trace, pic_make_str_cstr(pic, "read error: "));
2014-08-25 00:38:09 -04:00
break;
case PIC_ERROR_RAISED:
2014-09-12 06:52:49 -04:00
trace = pic_strcat(pic, trace, pic_make_str_cstr(pic, "raised: "));
2014-08-25 00:38:09 -04:00
break;
}
trace = pic_strcat(pic, trace, e->msg);
/* TODO: print error irritants */
2014-09-12 06:52:49 -04:00
trace = pic_strcat(pic, trace, pic_make_str(pic, "\n", 1));
2014-08-25 00:38:09 -04:00
trace = pic_strcat(pic, trace, e->stack);
/* print! */
printf("%s", pic_str_cstr(trace));
pic_gc_arena_restore(pic, ai);
}