picrin/src/debug.c

75 lines
1.8 KiB
C
Raw Normal View History

2014-03-29 07:02:11 -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)
{
2014-05-26 03:06:41 -04:00
size_t ai = pic_gc_arena_preserve(pic);
2014-03-29 07:02:11 -04:00
pic_callinfo *ci;
pic_str *trace;
2014-04-06 01:00:45 -04:00
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)
{
2014-05-26 03:06:41 -04:00
size_t ai = pic_gc_arena_preserve(pic);
2014-04-06 01:00:45 -04:00
pic_str *trace;
2014-03-29 07:02:11 -04:00
assert(pic->err != NULL);
trace = pic_str_new(pic, NULL, 0);
2014-04-06 01:00:45 -04:00
switch (e->type) {
2014-03-29 07:02:11 -04:00
case PIC_ERROR_OTHER:
trace = pic_strcat(pic, trace, pic_str_new_cstr(pic, "error: "));
break;
case PIC_ERROR_FILE:
trace = pic_strcat(pic, trace, pic_str_new_cstr(pic, "file error: "));
break;
case PIC_ERROR_READ:
trace = pic_strcat(pic, trace, pic_str_new_cstr(pic, "read error: "));
break;
case PIC_ERROR_RAISED:
trace = pic_strcat(pic, trace, pic_str_new_cstr(pic, "raised: "));
break;
}
2014-04-06 01:00:45 -04:00
trace = pic_strcat(pic, trace, e->msg);
2014-03-29 07:02:11 -04:00
/* TODO: print error irritants */
2014-04-06 01:00:45 -04:00
trace = pic_strcat(pic, trace, pic_str_new(pic, "\n", 1));
2014-04-06 01:02:21 -04:00
trace = pic_strcat(pic, trace, e->stack);
2014-03-29 07:02:11 -04:00
2014-04-06 01:00:45 -04:00
/* print! */
printf("%s", pic_str_cstr(trace));
2014-03-29 07:02:11 -04:00
pic_gc_arena_restore(pic, ai);
}