| 1 |
/* |
| 2 |
** backtrace.c - |
| 3 |
** |
| 4 |
** See Copyright Notice in mruby.h |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <mruby.h> |
| 8 |
#include <mruby/variable.h> |
| 9 |
#include <mruby/proc.h> |
| 10 |
#include <mruby/array.h> |
| 11 |
#include <mruby/string.h> |
| 12 |
#include <mruby/class.h> |
| 13 |
#include <mruby/debug.h> |
| 14 |
#include <mruby/error.h> |
| 15 |
#include <mruby/numeric.h> |
| 16 |
#include <mruby/data.h> |
| 17 |
#include <mruby/internal.h> |
| 18 |
#include <mruby/presym.h> |
| 19 |
|
| 20 |
struct backtrace_location { |
| 21 |
mrb_sym method_id; |
| 22 |
int32_t idx; |
| 23 |
const mrb_irep *irep; |
| 24 |
}; |
| 25 |
|
| 26 |
typedef void (*each_backtrace_func)(mrb_state*, const struct backtrace_location*, void*); |
| 27 |
|
| 28 |
static const mrb_data_type bt_type = { "Backtrace", mrb_free }; |
| 29 |
|
| 30 |
static uint32_t |
| 31 |
each_backtrace(mrb_state *mrb, ptrdiff_t ciidx, each_backtrace_func func, void *data) |
| 32 |
{ |
| 33 |
uint32_t n = 0; |
| 34 |
|
| 35 |
for (ptrdiff_t i=ciidx; i >= 0; i--) { |
| 36 |
struct backtrace_location loc; |
| 37 |
mrb_callinfo *ci; |
| 38 |
const mrb_code *pc; |
| 39 |
|
| 40 |
ci = &mrb->c->cibase[i]; |
| 41 |
|
| 42 |
if (!ci->proc || MRB_PROC_CFUNC_P(ci->proc)) { |
| 43 |
if (!ci->mid) continue; |
| 44 |
loc.irep = NULL; |
| 45 |
} |
| 46 |
else { |
| 47 |
loc.irep = ci->proc->body.irep; |
| 48 |
if (!loc.irep) continue; |
| 49 |
if (!loc.irep->debug_info) continue; |
| 50 |
if (mrb->c->cibase[i].pc) { |
| 51 |
pc = &mrb->c->cibase[i].pc[-1]; |
| 52 |
} |
| 53 |
else { |
| 54 |
continue; |
| 55 |
} |
| 56 |
loc.idx = (uint32_t)(pc - loc.irep->iseq); |
| 57 |
} |
| 58 |
loc.method_id = ci->mid; |
| 59 |
if (loc.irep == NULL) { |
| 60 |
for (ptrdiff_t j=i-1; j >= 0; j--) { |
| 61 |
ci = &mrb->c->cibase[j]; |
| 62 |
|
| 63 |
if (!ci->proc) continue; |
| 64 |
if (MRB_PROC_CFUNC_P(ci->proc)) continue; |
| 65 |
|
| 66 |
const mrb_irep *irep = ci->proc->body.irep; |
| 67 |
if (!irep) continue; |
| 68 |
if (!irep->debug_info) continue; |
| 69 |
|
| 70 |
if (mrb->c->cibase[j].pc) { |
| 71 |
pc = &mrb->c->cibase[j].pc[-1]; |
| 72 |
} |
| 73 |
else { |
| 74 |
continue; |
| 75 |
} |
| 76 |
|
| 77 |
loc.irep = irep; |
| 78 |
loc.idx = (uint32_t)(pc - loc.irep->iseq); |
| 79 |
break; |
| 80 |
} |
| 81 |
} |
| 82 |
if (func) func(mrb, &loc, data); |
| 83 |
n++; |
| 84 |
} |
| 85 |
return n; |
| 86 |
} |
| 87 |
|
| 88 |
static void |
| 89 |
pack_backtrace_i(mrb_state *mrb, |
| 90 |
const struct backtrace_location *loc, |
| 91 |
void *data) |
| 92 |
{ |
| 93 |
struct backtrace_location **pptr = (struct backtrace_location**)data; |
| 94 |
struct backtrace_location *ptr = *pptr; |
| 95 |
|
| 96 |
*ptr = *loc; |
| 97 |
*pptr = ptr+1; |
| 98 |
} |
| 99 |
|
| 100 |
static struct RObject* |
| 101 |
packed_backtrace(mrb_state *mrb) |
| 102 |
{ |
| 103 |
struct RData *backtrace; |
| 104 |
ptrdiff_t ciidx = mrb->c->ci - mrb->c->cibase; |
| 105 |
|
| 106 |
if (ciidx >= mrb->c->ciend - mrb->c->cibase) |
| 107 |
ciidx = mrb->c->ciend - mrb->c->cibase; /* ciidx is broken... */ |
| 108 |
|
| 109 |
/* count the number of backtraces */ |
| 110 |
int len = each_backtrace(mrb, ciidx, NULL, NULL); |
| 111 |
backtrace = mrb_data_object_alloc(mrb, NULL, NULL, &bt_type); |
| 112 |
if (len > 0) { |
| 113 |
void *ptr = mrb_malloc(mrb, len * sizeof(struct backtrace_location)); |
| 114 |
backtrace->data = ptr; |
| 115 |
backtrace->flags = len; |
| 116 |
each_backtrace(mrb, ciidx, pack_backtrace_i, &ptr); |
| 117 |
} |
| 118 |
else { |
| 119 |
backtrace->data = NULL; |
| 120 |
backtrace->flags = 0; |
| 121 |
} |
| 122 |
return (struct RObject*)backtrace; |
| 123 |
} |
| 124 |
|
| 125 |
static void |
| 126 |
store_backtrace(mrb_state *mrb, mrb_value exc, struct RObject *backtrace) |
| 127 |
{ |
| 128 |
struct RException *e = mrb_exc_ptr(exc); |
| 129 |
e->backtrace = backtrace; |
| 130 |
mrb_field_write_barrier(mrb, (struct RBasic*)e, (struct RBasic*)backtrace); |
| 131 |
} |
| 132 |
|
| 133 |
void |
| 134 |
mrb_keep_backtrace(mrb_state *mrb, mrb_value exc) |
| 135 |
{ |
| 136 |
int ai; |
| 137 |
|
| 138 |
if (mrb->c->ci == NULL) return; |
| 139 |
if (mrb_exc_ptr(exc)->backtrace) return; |
| 140 |
ai = mrb_gc_arena_save(mrb); |
| 141 |
struct RObject *backtrace = packed_backtrace(mrb); |
| 142 |
store_backtrace(mrb, exc, backtrace); |
| 143 |
mrb_gc_arena_restore(mrb, ai); |
| 144 |
} |
| 145 |
|
| 146 |
static struct RObject* |
| 147 |
mrb_unpack_backtrace(mrb_state *mrb, struct RObject *backtrace) |
| 148 |
{ |
| 149 |
const struct backtrace_location *bt; |
| 150 |
mrb_int n, i; |
| 151 |
int ai; |
| 152 |
|
| 153 |
if (backtrace == NULL) { |
| 154 |
empty_backtrace: |
| 155 |
return mrb_obj_ptr(mrb_ary_new_capa(mrb, 0)); |
| 156 |
} |
| 157 |
if (backtrace->tt == MRB_TT_ARRAY) return backtrace; |
| 158 |
bt = (struct backtrace_location*)mrb_data_check_get_ptr(mrb, mrb_obj_value(backtrace), &bt_type); |
| 159 |
if (bt == NULL) goto empty_backtrace; |
| 160 |
n = (mrb_int)backtrace->flags; |
| 161 |
if (n == 0) goto empty_backtrace; |
| 162 |
backtrace = mrb_obj_ptr(mrb_ary_new_capa(mrb, n)); |
| 163 |
ai = mrb_gc_arena_save(mrb); |
| 164 |
for (i = 0; i < n; i++) { |
| 165 |
const struct backtrace_location *entry = &bt[i]; |
| 166 |
mrb_value btline; |
| 167 |
int32_t lineno; |
| 168 |
const char *filename; |
| 169 |
|
| 170 |
if (!mrb_debug_get_position(mrb, entry->irep, entry->idx, &lineno, &filename)) { |
| 171 |
btline = mrb_str_new_lit(mrb, "(unknown):0"); |
| 172 |
} |
| 173 |
else if (lineno != -1) {//debug info was available |
| 174 |
btline = mrb_format(mrb, "%s:%d", filename, (int)lineno); |
| 175 |
} |
| 176 |
else { //all that was left was the stack frame |
| 177 |
btline = mrb_format(mrb, "%s:0", filename); |
| 178 |
} |
| 179 |
if (entry->method_id != 0) { |
| 180 |
mrb_str_cat_lit(mrb, btline, ":in "); |
| 181 |
mrb_str_cat_cstr(mrb, btline, mrb_sym_name(mrb, entry->method_id)); |
| 182 |
} |
| 183 |
mrb_ary_push(mrb, mrb_obj_value(backtrace), btline); |
| 184 |
mrb_gc_arena_restore(mrb, ai); |
| 185 |
} |
| 186 |
|
| 187 |
return backtrace; |
| 188 |
} |
| 189 |
|
| 190 |
mrb_value |
| 191 |
mrb_exc_backtrace(mrb_state *mrb, mrb_value exc) |
| 192 |
{ |
| 193 |
struct RObject *backtrace = mrb_exc_ptr(exc)->backtrace; |
| 194 |
if (backtrace == NULL) { |
| 195 |
return mrb_nil_value(); |
| 196 |
} |
| 197 |
if (backtrace->tt == MRB_TT_ARRAY) { |
| 198 |
return mrb_obj_value(backtrace); |
| 199 |
} |
| 200 |
/* unpack packed-backtrace */ |
| 201 |
backtrace = mrb_unpack_backtrace(mrb, backtrace); |
| 202 |
store_backtrace(mrb, exc, backtrace); |
| 203 |
return mrb_obj_value(backtrace); |
| 204 |
} |
| 205 |
|
| 206 |
mrb_value |
| 207 |
mrb_get_backtrace(mrb_state *mrb) |
| 208 |
{ |
| 209 |
return mrb_obj_value(mrb_unpack_backtrace(mrb, packed_backtrace(mrb))); |
| 210 |
} |
| 211 |
|
| 212 |
#ifndef MRB_NO_STDIO |
| 213 |
|
| 214 |
static void |
| 215 |
print_backtrace(mrb_state *mrb, struct RObject *exc, struct RArray *backtrace) |
| 216 |
{ |
| 217 |
mrb_int i; |
| 218 |
mrb_int n = (backtrace ? ARY_LEN(backtrace) : 0); |
| 219 |
mrb_value *loc, mesg; |
| 220 |
|
| 221 |
if (n != 0) { |
| 222 |
if (n > 1) { |
| 223 |
fputs("trace (most recent call last):\n", stderr); |
| 224 |
} |
| 225 |
for (i=n-1,loc=&ARY_PTR(backtrace)[i]; i>0; i--,loc--) { |
| 226 |
if (mrb_string_p(*loc)) { |
| 227 |
fprintf(stderr, "\t[%d] ", (int)i); |
| 228 |
fwrite(RSTRING_PTR(*loc), (int)RSTRING_LEN(*loc), 1, stderr); |
| 229 |
fputc('\n', stderr); |
| 230 |
} |
| 231 |
} |
| 232 |
if (mrb_string_p(*loc)) { |
| 233 |
fwrite(RSTRING_PTR(*loc), (int)RSTRING_LEN(*loc), 1, stderr); |
| 234 |
fputs(": ", stderr); |
| 235 |
} |
| 236 |
} |
| 237 |
else { |
| 238 |
fputs("(unknown):0: ", stderr); |
| 239 |
} |
| 240 |
|
| 241 |
if (exc == mrb->nomem_err) { |
| 242 |
static const char nomem[] = "Out of memory (NoMemoryError)\n"; |
| 243 |
fwrite(nomem, sizeof(nomem)-1, 1, stderr); |
| 244 |
} |
| 245 |
else { |
| 246 |
mesg = mrb_exc_inspect(mrb, mrb_obj_value(exc)); |
| 247 |
fwrite(RSTRING_PTR(mesg), RSTRING_LEN(mesg), 1, stderr); |
| 248 |
fputc('\n', stderr); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
/* mrb_print_backtrace |
| 253 |
|
| 254 |
function to retrieve backtrace information from the last exception. |
| 255 |
*/ |
| 256 |
|
| 257 |
MRB_API void |
| 258 |
mrb_print_backtrace(mrb_state *mrb) |
| 259 |
{ |
| 260 |
if (!mrb->exc || mrb->exc->tt != MRB_TT_EXCEPTION) { |
| 261 |
return; |
| 262 |
} |
| 263 |
|
| 264 |
struct RObject *backtrace = ((struct RException*)mrb->exc)->backtrace; |
| 265 |
if (backtrace && backtrace->tt != MRB_TT_ARRAY) backtrace = mrb_unpack_backtrace(mrb, backtrace); |
| 266 |
print_backtrace(mrb, mrb->exc, (struct RArray*)backtrace); |
| 267 |
} |
| 268 |
#else |
| 269 |
MRB_API void |
| 270 |
mrb_print_backtrace(mrb_state *mrb) |
| 271 |
{ |
| 272 |
} |
| 273 |
#endif |