2014-08-25 00:38:09 -04:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "picrin.h"
|
|
|
|
|
|
|
|
pic_str *
|
|
|
|
pic_get_backtrace(pic_state *pic)
|
|
|
|
{
|
|
|
|
size_t ai = pic_gc_arena_preserve(pic);
|
|
|
|
pic_callinfo *ci;
|
|
|
|
pic_str *trace;
|
|
|
|
|
2016-02-07 12:31:45 -05:00
|
|
|
trace = pic_make_lit(pic, "");
|
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]);
|
|
|
|
|
2016-02-07 12:31:45 -05:00
|
|
|
trace = pic_str_cat(pic, trace, pic_make_lit(pic, " at "));
|
|
|
|
trace = pic_str_cat(pic, trace, pic_make_lit(pic, "(anonymous lambda)"));
|
2014-08-25 00:38:09 -04:00
|
|
|
|
|
|
|
if (pic_proc_func_p(proc)) {
|
2016-02-07 12:31:45 -05:00
|
|
|
trace = pic_str_cat(pic, trace, pic_make_lit(pic, " (native function)\n"));
|
2014-08-25 00:38:09 -04:00
|
|
|
} else if (pic_proc_irep_p(proc)) {
|
2016-02-07 12:31:45 -05:00
|
|
|
trace = pic_str_cat(pic, trace, pic_make_lit(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
|
2015-05-27 11:14:10 -04:00
|
|
|
pic_print_backtrace(pic_state *pic, xFILE *file)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2015-06-09 03:02:23 -04:00
|
|
|
assert(! pic_invalid_p(pic->err));
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2014-09-16 11:28:55 -04:00
|
|
|
if (! pic_error_p(pic->err)) {
|
2015-06-18 13:05:56 -04:00
|
|
|
xfprintf(pic, file, "raise: ");
|
2015-05-27 11:14:10 -04:00
|
|
|
pic_fwrite(pic, pic->err, file);
|
2014-09-16 11:28:55 -04:00
|
|
|
} else {
|
|
|
|
struct pic_error *e;
|
2015-06-19 13:34:00 -04:00
|
|
|
pic_value elem, it;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2014-09-16 11:28:55 -04:00
|
|
|
e = pic_error_ptr(pic->err);
|
2015-07-12 19:16:04 -04:00
|
|
|
if (e->type != pic_intern(pic, "")) {
|
2015-05-27 11:14:10 -04:00
|
|
|
pic_fwrite(pic, pic_obj_value(e->type), file);
|
2015-06-18 13:05:56 -04:00
|
|
|
xfprintf(pic, file, " ");
|
2014-09-16 11:28:55 -04:00
|
|
|
}
|
2015-06-18 13:05:56 -04:00
|
|
|
xfprintf(pic, file, "error: ");
|
2015-05-27 11:14:10 -04:00
|
|
|
pic_fwrite(pic, pic_obj_value(e->msg), file);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-06-19 13:34:00 -04:00
|
|
|
pic_for_each (elem, e->irrs, it) { /* print error irritants */
|
|
|
|
xfprintf(pic, file, " ");
|
|
|
|
pic_fwrite(pic, elem, file);
|
|
|
|
}
|
|
|
|
xfprintf(pic, file, "\n");
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-06-18 13:05:56 -04:00
|
|
|
xfputs(pic, pic_str_cstr(pic, e->stack), file);
|
2014-09-16 11:28:55 -04:00
|
|
|
}
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|