picrin/extlib/benz/debug.c

65 lines
1.6 KiB
C
Raw Normal View History

2014-08-25 00:38:09 -04:00
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
2016-02-18 10:56:56 -05:00
#include "picrin/object.h"
2014-08-25 00:38:09 -04:00
2016-02-19 13:26:52 -05:00
pic_value
2014-08-25 00:38:09 -04:00
pic_get_backtrace(pic_state *pic)
{
2016-02-19 02:17:13 -05:00
size_t ai = pic_enter(pic);
2014-08-25 00:38:09 -04:00
pic_callinfo *ci;
2016-02-19 13:26:52 -05:00
pic_value trace;
2014-08-25 00:38:09 -04:00
trace = pic_lit_value(pic, "");
2014-08-25 00:38:09 -04:00
for (ci = pic->ci; ci != pic->cibase; --ci) {
2016-02-19 10:03:16 -05:00
pic_value proc = ci->fp[0];
2014-08-25 00:38:09 -04:00
trace = pic_str_cat(pic, trace, pic_lit_value(pic, " at "));
trace = pic_str_cat(pic, trace, pic_lit_value(pic, "(anonymous lambda)"));
2014-08-25 00:38:09 -04:00
2016-02-19 10:03:16 -05:00
if (pic_proc_func_p(pic_proc_ptr(pic, proc))) {
trace = pic_str_cat(pic, trace, pic_lit_value(pic, " (native function)\n"));
2016-02-19 10:03:16 -05:00
} else {
trace = pic_str_cat(pic, trace, pic_lit_value(pic, " (unknown location)\n")); /* TODO */
2014-08-25 00:38:09 -04:00
}
}
2016-02-19 02:17:13 -05:00
pic_leave(pic, ai);
2016-02-19 13:26:52 -05:00
pic_protect(pic, trace);
2014-08-25 00:38:09 -04:00
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
{
assert(! pic_invalid_p(pic, pic->err));
2014-08-25 00:38:09 -04:00
if (! pic_error_p(pic, 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);
2016-02-20 01:31:14 -05:00
if (! pic_eq_p(pic, pic_obj_value(e->type), pic_intern_lit(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
2016-02-19 13:26:52 -05:00
xfputs(pic, pic_str(pic, pic_obj_value(e->stack)), file);
2014-09-16 11:28:55 -04:00
}
2014-08-25 00:38:09 -04:00
}