4 ** See Copyright Notice in 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>
18 struct backtrace_location {
24 typedef void (*each_backtrace_func)(mrb_state*, const struct backtrace_location*, void*);
26 static const mrb_data_type bt_type = { "Backtrace", mrb_free };
28 mrb_value mrb_exc_inspect(mrb_state *mrb, mrb_value exc);
29 mrb_value mrb_unpack_backtrace(mrb_state *mrb, mrb_value backtrace);
32 each_backtrace(mrb_state *mrb, ptrdiff_t ciidx, const mrb_code *pc0, each_backtrace_func func, void *data)
36 if (ciidx >= mrb->c->ciend - mrb->c->cibase)
37 ciidx = 10; /* ciidx is broken... */
39 for (i=ciidx; i >= 0; i--) {
40 struct backtrace_location loc;
45 ci = &mrb->c->cibase[i];
47 if (!ci->proc) continue;
48 if (MRB_PROC_CFUNC_P(ci->proc)) continue;
50 irep = ci->proc->body.irep;
53 if (mrb->c->cibase[i].err) {
54 pc = mrb->c->cibase[i].err;
56 else if (i+1 <= ciidx) {
57 if (!mrb->c->cibase[i + 1].pc) continue;
58 pc = &mrb->c->cibase[i+1].pc[-1];
64 loc.lineno = mrb_debug_get_line(mrb, irep, pc - irep->iseq);
65 if (loc.lineno == -1) continue;
67 loc.filename = mrb_debug_get_filename(mrb, irep, pc - irep->iseq);
69 loc.filename = "(unknown)";
72 loc.method_id = ci->mid;
73 func(mrb, &loc, data);
77 #ifndef MRB_DISABLE_STDIO
80 print_backtrace(mrb_state *mrb, struct RObject *exc, mrb_value backtrace)
83 mrb_int n = RARRAY_LEN(backtrace);
85 FILE *stream = stderr;
88 fprintf(stream, "trace (most recent call last):\n");
89 for (i=n-1,loc=&RARRAY_PTR(backtrace)[i]; i>0; i--,loc--) {
90 if (mrb_string_p(*loc)) {
91 fprintf(stream, "\t[%d] %.*s\n",
92 (int)i, (int)RSTRING_LEN(*loc), RSTRING_PTR(*loc));
95 if (mrb_string_p(*loc)) {
96 fprintf(stream, "%.*s: ", (int)RSTRING_LEN(*loc), RSTRING_PTR(*loc));
99 mesg = mrb_exc_inspect(mrb, mrb_obj_value(exc));
100 fprintf(stream, "%.*s\n", (int)RSTRING_LEN(mesg), RSTRING_PTR(mesg));
103 /* mrb_print_backtrace
105 function to retrieve backtrace information from the last exception.
109 mrb_print_backtrace(mrb_state *mrb)
117 backtrace = mrb_obj_iv_get(mrb, mrb->exc, mrb_intern_lit(mrb, "backtrace"));
118 if (mrb_nil_p(backtrace)) return;
119 if (!mrb_array_p(backtrace)) backtrace = mrb_unpack_backtrace(mrb, backtrace);
120 print_backtrace(mrb, mrb->exc, backtrace);
125 mrb_print_backtrace(mrb_state *mrb)
132 count_backtrace_i(mrb_state *mrb,
133 const struct backtrace_location *loc,
136 int *lenp = (int*)data;
142 pack_backtrace_i(mrb_state *mrb,
143 const struct backtrace_location *loc,
146 struct backtrace_location **pptr = (struct backtrace_location**)data;
147 struct backtrace_location *ptr = *pptr;
154 packed_backtrace(mrb_state *mrb)
156 struct RData *backtrace;
157 ptrdiff_t ciidx = mrb->c->ci - mrb->c->cibase;
162 each_backtrace(mrb, ciidx, mrb->c->ci->pc, count_backtrace_i, &len);
163 size = len * sizeof(struct backtrace_location);
164 ptr = mrb_malloc(mrb, size);
165 backtrace = mrb_data_object_alloc(mrb, NULL, ptr, &bt_type);
166 backtrace->flags = (uint32_t)len;
167 each_backtrace(mrb, ciidx, mrb->c->ci->pc, pack_backtrace_i, &ptr);
168 return mrb_obj_value(backtrace);
172 mrb_keep_backtrace(mrb_state *mrb, mrb_value exc)
174 mrb_sym sym = mrb_intern_lit(mrb, "backtrace");
178 if (mrb_iv_defined(mrb, exc, sym)) return;
179 ai = mrb_gc_arena_save(mrb);
180 backtrace = packed_backtrace(mrb);
181 mrb_iv_set(mrb, exc, sym, backtrace);
182 mrb_gc_arena_restore(mrb, ai);
186 mrb_unpack_backtrace(mrb_state *mrb, mrb_value backtrace)
188 const struct backtrace_location *bt;
192 if (mrb_nil_p(backtrace)) {
194 return mrb_ary_new_capa(mrb, 0);
196 if (mrb_array_p(backtrace)) return backtrace;
197 bt = (struct backtrace_location*)mrb_data_check_get_ptr(mrb, backtrace, &bt_type);
198 if (bt == NULL) goto empty_backtrace;
199 n = (mrb_int)RDATA(backtrace)->flags;
200 backtrace = mrb_ary_new_capa(mrb, n);
201 ai = mrb_gc_arena_save(mrb);
202 for (i = 0; i < n; i++) {
203 const struct backtrace_location *entry = &bt[i];
206 btline = mrb_format(mrb, "%s:%d", entry->filename, (int)entry->lineno);
207 if (entry->method_id != 0) {
208 mrb_str_cat_lit(mrb, btline, ":in ");
209 mrb_str_cat_cstr(mrb, btline, mrb_sym_name(mrb, entry->method_id));
211 mrb_ary_push(mrb, backtrace, btline);
212 mrb_gc_arena_restore(mrb, ai);
219 mrb_exc_backtrace(mrb_state *mrb, mrb_value exc)
224 attr_name = mrb_intern_lit(mrb, "backtrace");
225 backtrace = mrb_iv_get(mrb, exc, attr_name);
226 if (mrb_nil_p(backtrace) || mrb_array_p(backtrace)) {
229 backtrace = mrb_unpack_backtrace(mrb, backtrace);
230 mrb_iv_set(mrb, exc, attr_name, backtrace);
235 mrb_get_backtrace(mrb_state *mrb)
237 return mrb_unpack_backtrace(mrb, packed_backtrace(mrb));