-
Notifications
You must be signed in to change notification settings - Fork 838
Expand file tree
/
Copy pathbacktrace.c
More file actions
293 lines (252 loc) · 7.32 KB
/
Copy pathbacktrace.c
File metadata and controls
293 lines (252 loc) · 7.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
** backtrace.c -
**
** See Copyright Notice in mruby.h
*/
#include <mruby.h>
#include <mruby/variable.h>
#include <mruby/proc.h>
#include <mruby/array.h>
#include <mruby/string.h>
#include <mruby/class.h>
#include <mruby/debug.h>
#include <mruby/error.h>
#include <mruby/numeric.h>
#include <mruby/data.h>
#include <mruby/internal.h>
#define MAX_IREP_REFCNT UINT16_MAX
#define UNKNOWN_LINENO -1
#define UNKNOWN_LOCATION "(unknown):0"
static void
copy_backtrace(mrb_state *mrb,
const struct mrb_backtrace_location *loc,
struct mrb_backtrace_location *ptr,
size_t n)
{
ptr[n] = *loc;
if (loc->irep) {
if (loc->irep->refcnt == MAX_IREP_REFCNT) {
ptr[n].irep = NULL;
}
else {
mrb_irep_incref(mrb, (mrb_irep*)loc->irep);
}
}
}
/**
* Creates a packed backtrace from the current call stack
*
* @param mrb The mruby state
* @param ciidx The current callinfo index
* @param ptr Pointer to store the backtrace locations
* @return Number of backtrace entries
* @note This function handles both proc and non-proc cases, managing IREP references
* and building location entries for each stack frame.
*/
static size_t
pack_backtrace(mrb_state *mrb, ptrdiff_t ciidx, struct mrb_backtrace_location *ptr)
{
size_t n = 0;
for (ptrdiff_t i=ciidx; i >= 0; i--) {
struct mrb_backtrace_location loc;
mrb_callinfo *ci;
const mrb_code *pc;
ci = &mrb->c->cibase[i];
loc.method_id = ci->mid;
if (ci->proc && !MRB_PROC_CFUNC_P(ci->proc)) {
mrb_assert(!MRB_PROC_ALIAS_P(ci->proc));
loc.irep = ci->proc->body.irep;
if (!loc.irep) continue;
if (!loc.irep->debug_info) continue;
if (!ci->pc) continue;
pc = &ci->pc[-1];
loc.idx = (uint32_t)(pc - loc.irep->iseq);
}
else {
if (!loc.method_id) continue;
loc.irep = NULL;
for (ptrdiff_t j=i-1; j >= 0; j--) {
ci = &mrb->c->cibase[j];
if (!ci->proc) continue;
if (MRB_PROC_CFUNC_P(ci->proc)) continue;
mrb_assert(!MRB_PROC_ALIAS_P(ci->proc));
const mrb_irep *irep = ci->proc->body.irep;
if (!irep) continue;
if (!irep->debug_info) continue;
if (!ci->pc) continue;
pc = &ci->pc[-1];
loc.irep = irep;
loc.idx = (uint32_t)(pc - irep->iseq);
break;
}
}
copy_backtrace(mrb, &loc, ptr, n);
n++;
}
return n;
}
static struct RBasic*
packed_backtrace(mrb_state *mrb)
{
ptrdiff_t ciidx = mrb->c->ci - mrb->c->cibase;
if (ciidx >= mrb->c->ciend - mrb->c->cibase)
ciidx = mrb->c->ciend - mrb->c->cibase; /* ciidx is broken... */
ptrdiff_t len = ciidx + 1;
struct RBacktrace *backtrace = MRB_OBJ_ALLOC(mrb, MRB_TT_BACKTRACE, NULL);
void *ptr = mrb_malloc(mrb, len * sizeof(struct mrb_backtrace_location));
backtrace->locations = (struct mrb_backtrace_location*)ptr;
backtrace->len = pack_backtrace(mrb, ciidx, backtrace->locations);
return (struct RBasic*)backtrace;
}
static void
store_backtrace(mrb_state *mrb, mrb_value exc, struct RBasic *backtrace)
{
struct RException *e = mrb_exc_ptr(exc);
e->backtrace = backtrace;
mrb_field_write_barrier(mrb, (struct RBasic*)e, backtrace);
}
void
mrb_keep_backtrace(mrb_state *mrb, mrb_value exc)
{
int ai;
if (mrb->c->ci == NULL) return;
if (mrb_exc_ptr(exc)->backtrace) return;
ai = mrb_gc_arena_save(mrb);
struct RBasic *backtrace = packed_backtrace(mrb);
store_backtrace(mrb, exc, backtrace);
mrb_gc_arena_restore(mrb, ai);
}
static mrb_value
decode_location(mrb_state *mrb, const struct mrb_backtrace_location *entry)
{
mrb_value btline;
int32_t lineno;
const char *filename;
// Case 1: No IREP or debug info available
if (!entry->irep) {
return mrb_str_new_lit(mrb, UNKNOWN_LOCATION);
}
// Case 2: Debug info lookup failed
if (!mrb_debug_get_position(mrb, entry->irep, entry->idx, &lineno, &filename)) {
return mrb_str_new_lit(mrb, UNKNOWN_LOCATION);
}
// Case 3: Valid debug info
if (lineno != UNKNOWN_LINENO) {
btline = mrb_format(mrb, "%s:%d", filename, (int)lineno);
}
else { //all that was left was the stack frame
btline = mrb_format(mrb, "%s:0", filename);
}
// Add method name if available
if (entry->method_id != 0) {
mrb_str_cat_lit(mrb, btline, ":in ");
mrb_str_cat_cstr(mrb, btline, mrb_sym_name(mrb, entry->method_id));
}
return btline;
}
static struct RBasic*
mrb_unpack_backtrace(mrb_state *mrb, struct RBasic *backtrace)
{
if (backtrace == NULL) {
return mrb_basic_ptr(mrb_ary_new_capa(mrb, 0));
}
if (backtrace->tt == MRB_TT_ARRAY) return backtrace;
mrb_assert(backtrace->tt == MRB_TT_BACKTRACE);
struct RBacktrace *bt = (struct RBacktrace*)backtrace;
mrb_int n = (mrb_int)bt->len;
const struct mrb_backtrace_location *loc = bt->locations;
backtrace = mrb_basic_ptr(mrb_ary_new_capa(mrb, n));
int ai = mrb_gc_arena_save(mrb);
for (mrb_int i = 0; i < n; i++) {
mrb_value btline = decode_location(mrb, &loc[i]);
mrb_ary_push(mrb, mrb_obj_value(backtrace), btline);
mrb_gc_arena_restore(mrb, ai);
}
return backtrace;
}
mrb_value
mrb_exc_backtrace(mrb_state *mrb, mrb_value exc)
{
struct RBasic *backtrace = mrb_exc_ptr(exc)->backtrace;
if (backtrace == NULL) {
return mrb_nil_value();
}
if (backtrace->tt == MRB_TT_ARRAY) {
return mrb_obj_value(backtrace);
}
/* unpack packed-backtrace */
backtrace = mrb_unpack_backtrace(mrb, backtrace);
store_backtrace(mrb, exc, backtrace);
return mrb_obj_value(backtrace);
}
mrb_value
mrb_get_backtrace(mrb_state *mrb)
{
return mrb_obj_value(mrb_unpack_backtrace(mrb, packed_backtrace(mrb)));
}
#ifndef MRB_NO_STDIO
static void
print_backtrace(mrb_state *mrb, struct RObject *exc, struct RBasic *ptr)
{
struct RArray *ary = NULL;
struct RBacktrace *bt = NULL;
mrb_int n = 0;
if (ptr) {
if (ptr->tt == MRB_TT_ARRAY) {
ary = (struct RArray*)ptr;
n = ARY_LEN(ary);
}
else {
bt = (struct RBacktrace*)ptr;
n = (mrb_int)bt->len;
}
}
if (n != 0) {
mrb_value btline;
fputs("trace (most recent call last):\n", stderr);
for (mrb_int i=n-1; i>0; i--) {
if (ary) btline = ARY_PTR(ary)[i];
else btline = decode_location(mrb, &bt->locations[i]);
if (mrb_string_p(btline)) {
fprintf(stderr, "\t[%d] ", (int)i);
fwrite(RSTRING_PTR(btline), (int)RSTRING_LEN(btline), 1, stderr);
fputc('\n', stderr);
}
}
if (ary) btline = ARY_PTR(ary)[0];
else btline = decode_location(mrb, &bt->locations[0]);
if (mrb_string_p(btline)) {
fwrite(RSTRING_PTR(btline), (int)RSTRING_LEN(btline), 1, stderr);
fputs(": ", stderr);
}
}
else {
fputs(UNKNOWN_LOCATION ": ", stderr);
}
if (exc == mrb->nomem_err) {
static const char nomem[] = "Out of memory (NoMemoryError)\n";
fwrite(nomem, sizeof(nomem)-1, 1, stderr);
}
else {
mrb_value output = mrb_exc_get_output(mrb, exc);
fwrite(RSTRING_PTR(output), RSTRING_LEN(output), 1, stderr);
fputc('\n', stderr);
}
}
/* mrb_print_backtrace
function to retrieve backtrace information from the last exception.
*/
MRB_API void
mrb_print_backtrace(mrb_state *mrb)
{
if (!mrb->exc || mrb->exc->tt != MRB_TT_EXCEPTION) {
return;
}
print_backtrace(mrb, mrb->exc, ((struct RException*)mrb->exc)->backtrace);
}
#else
MRB_API void
mrb_print_backtrace(mrb_state *mrb)
{
}
#endif