forked from ballerina-platform/nballerina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.c
381 lines (350 loc) · 11.9 KB
/
error.c
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "balrt.h"
#include "third-party/libbacktrace/backtrace.h"
#define INITIAL_PC_COUNT 1
#define MAX_PC_COUNT 256
#define THREAD 0
#define SUCCESS 0
#define FAIL 1
#define NO_DEBUG_INFO (-1)
// The reason to pick these negative numbers is that
// the libstacktrace already has error codes starting from -1
#define NO_ERROR (-2)
#define EXCEEDS_MAX_PC_COUNT (-3)
#define OUT_OF_MEMORY (-4)
enum DemangleResult {
PUBLIC_BALLERINA_NAME,
NON_PUBLIC_BALLERINA_NAME,
NON_BALLERINA_NAME,
BAD_BALLERINA_NAME
};
struct backtrace_state *state = NULL;
typedef struct {
int code;
char *message;
FILE *fp;
} BacktraceError;
// The lineNumber of the first backtrace entry is passed
// via _bal_panic. This lineNumber should be passed to the
// callback function
typedef struct {
BacktraceError error;
int64_t lineNumber;
} BacktraceStartLine;
typedef struct {
// This must be first so we can use same error callback
BacktraceError error;
uint32_t nPCs;
uint32_t szPCs;
PC *pcs;
} SimpleBacktrace;
static void storeBacktraceErrorCB(void *data, const char *msg, int errnum);
static int printBacktraceLineCB(void *data, PC pc, const char *filename, int lineno, const char *function);
static void printMissingSymbolsCB(void *data, uintptr_t pc, const char *symname, uintptr_t symval, uintptr_t symsize);
static int storePCCB(void *data, PC pc);
static void printBacktraceLine(const char *filename, int64_t lineNumber, const char *function, FILE *fp);
static void getSimpleBacktrace(SimpleBacktrace *p);
static char *saveMessage(const char *msg);
static void processPCs(GC PC* pcs, uint32_t nPCs, uint32_t start, int64_t lineNumber, BacktraceError *error);
static void processInitialPC(PC pc, BacktraceStartLine *backtraceStartLine);
static enum DemangleResult demangle(const char *mangledName, const char **localName, FILE *fp);
static bool demangleModules(const char **mangledModules, FILE *fp);
static bool demangleCountedName(const char **pp, const char **name);
static bool isPrefix(const char *prefix, const char *str);
TaggedPtr _bal_error_construct(TaggedPtr message, int64_t lineNumber) {
SimpleBacktrace simpleBacktrace;
getSimpleBacktrace(&simpleBacktrace);
uint32_t nPCs = simpleBacktrace.nPCs;
size_t errorSize = sizeof(struct Error) + sizeof(PC)*nPCs;
if (simpleBacktrace.error.message != NULL) {
// store the error message after the PCs
errorSize += strlen(simpleBacktrace.error.message) + 1;
}
ErrorPtr ep = _bal_alloc(errorSize);
ep->message = message;
ep->lineNumber = lineNumber;
ep->nPCs = nPCs;
PC *pcs = simpleBacktrace.pcs;
if (pcs != NULL) {
memcpy(ep->pcs, pcs, sizeof(PC) * nPCs);
free(pcs);
}
ep->backtraceErrorCode = simpleBacktrace.error.code;
char *errorMessage = simpleBacktrace.error.message;
if (errorMessage != NULL) {
// we allocated extra space for the string above
char *str = (char *)(ep->pcs + nPCs);
strcpy(str, errorMessage);
ep->backtraceErrorMessage = str;
free(errorMessage);
}
else {
ep->backtraceErrorMessage = NULL;
}
return ptrAddFlags(ep, (uint64_t)TAG_ERROR << TAG_SHIFT);
}
// p is uninitialized on entry and will be fully initialized on return
static void getSimpleBacktrace(SimpleBacktrace *p) {
p->error.message = NULL;
p->nPCs = 0;
p->pcs = malloc(sizeof(PC) * INITIAL_PC_COUNT);
if (p->pcs == NULL) {
p->szPCs = 0;
p->error.code = OUT_OF_MEMORY;
return;
}
p->szPCs = INITIAL_PC_COUNT;
p->error.code = NO_ERROR;
if (state == NULL) {
state = backtrace_create_state(NULL, THREAD, storeBacktraceErrorCB, p);
if (state == NULL) {
return;
}
}
backtrace_simple(state, 0, storePCCB, storeBacktraceErrorCB, p);
}
// Implementation of backtrace_simple_callback
static int storePCCB(void *data, PC pc) {
SimpleBacktrace *simpleBacktrace = data;
uint32_t nPCs = simpleBacktrace->nPCs;
uint32_t szPCs = simpleBacktrace->szPCs;
if (nPCs == szPCs) {
if (szPCs == MAX_PC_COUNT) {
simpleBacktrace->error.code = EXCEEDS_MAX_PC_COUNT;
return FAIL;
}
szPCs *= 2;
void *p = realloc(simpleBacktrace->pcs, sizeof(PC) * szPCs);
if (p == NULL) {
simpleBacktrace->error.code = OUT_OF_MEMORY;
return FAIL;
}
simpleBacktrace->szPCs = szPCs;
simpleBacktrace->pcs = p;
}
simpleBacktrace->pcs[nPCs] = pc;
simpleBacktrace->nPCs = nPCs + 1;
return SUCCESS;
}
void _bal_error_backtrace_print(ErrorPtr ep, uint32_t start, FILE *fp) {
uint32_t nPCs = ep->nPCs;
BacktraceError err = { ep->backtraceErrorCode, ep->backtraceErrorMessage, fp };
if (start < nPCs) {
processPCs(ep->pcs, nPCs, start, ep->lineNumber, &err);
}
if (err.code == NO_ERROR) {
return;
}
if (err.code == EXCEEDS_MAX_PC_COUNT) {
fputs("...\n", fp);
}
else if (err.code == OUT_OF_MEMORY) {
fputs("out of memory to store backtrace\n", fp);
}
else {
fputs("libbacktrace", fp);
if (err.message != NULL) {
fprintf(fp, ": %s", err.message);
if (err.message != ep->backtraceErrorMessage) {
free(err.message);
}
}
if (err.code > 0) {
fprintf (fp, ": %s", strerror(err.code));
}
fputc('\n', fp);
}
fflush(fp);
}
static void processPCs(GC PC* pcs, uint32_t nPCs, uint32_t start, int64_t lineNumber, BacktraceError *error) {
// Handle the starting pc seperately
BacktraceStartLine backtraceStartLine = { { error->code, error->message, error->fp }, lineNumber };
processInitialPC(pcs[start], &backtraceStartLine);
if (backtraceStartLine.error.code == NO_DEBUG_INFO) {
for (uint32_t i = start + 1; i < nPCs; i++) {
// We do not need to pass error_callback because, already we have an error.
backtrace_syminfo(state, pcs[i], printMissingSymbolsCB, NULL, &backtraceStartLine);
}
}
else {
for (uint32_t i = start + 1; i < nPCs; i++) {
backtrace_pcinfo(state, pcs[i], printBacktraceLineCB, storeBacktraceErrorCB, &backtraceStartLine);
}
}
error->code = backtraceStartLine.error.code;
error->message = backtraceStartLine.error.message;
}
static void processInitialPC(PC pc, BacktraceStartLine *backtraceStartLine) {
backtrace_pcinfo(state, pc, printBacktraceLineCB, storeBacktraceErrorCB, backtraceStartLine);
if (backtraceStartLine->error.code == NO_DEBUG_INFO) {
// We do not need to pass error_callback because, already we have an error
backtrace_syminfo(state, pc, printMissingSymbolsCB, NULL, backtraceStartLine);
}
}
// Implementation of backtrace_full_callback
static int printBacktraceLineCB(void *data, UNUSED PC pc, const char *filename, int lineno, const char *function) {
BacktraceStartLine *backtraceStartLine = data;
if (function != NULL && isPrefix("_B\0", function)) {
int64_t lineNumber = backtraceStartLine->lineNumber != 0 ? backtraceStartLine->lineNumber : lineno;
if (backtraceStartLine->lineNumber != 0) {
backtraceStartLine->lineNumber = 0;
}
printBacktraceLine(filename, lineNumber, function, backtraceStartLine->error.fp);
}
return SUCCESS;
}
// Implementation of backtrace_syminfo_callback
static void printMissingSymbolsCB(void *data, UNUSED uintptr_t pc, const char *symname, UNUSED uintptr_t symval, UNUSED uintptr_t symsize) {
BacktraceStartLine *backtraceStartLine = data;
printBacktraceLine(NULL, backtraceStartLine->lineNumber, symname, backtraceStartLine->error.fp);
}
static void printBacktraceLine(const char *filename, int64_t lineNumber, const char *function, FILE *fp) {
// API docs say any of filename, lineno and function may be 0 meaning unavailable
const char *sep = " ";
if (function != NULL) {
fputs(sep, fp);
_bal_print_mangled_name(function, fp);
sep = " ";
}
if (filename != NULL) {
fprintf(fp, "%s%s", sep, filename);
sep = ":";
}
if (lineNumber != 0) {
fprintf(fp, "%s%" PRId64 "\n", sep, lineNumber);
}
else if (function != NULL || filename != NULL) {
putc('\n', fp);
}
fflush(fp);
}
static bool isPrefix(const char *prefix, const char *str) {
for (; *prefix == *str && *prefix != '\0'; prefix++, str++) { }
return *prefix == '\0';
}
// Implementation of backtrace_error_callback
static void storeBacktraceErrorCB(void *data, const char *msg, int errnum) {
BacktraceError *err = data;
// the error that is created first is tracked
if (err->code != NO_ERROR) {
return;
}
err->code = errnum;
err->message = saveMessage(msg);
}
static char *saveMessage(const char *msg) {
char *str = malloc(strlen(msg) + 1);
if (str == NULL) {
return NULL;
}
strcpy(str, msg);
return str;
}
TaggedPtr BAL_LANG_ERROR_NAME(message)(TaggedPtr error) {
ErrorPtr ep = taggedToPtr(error);
return ep->message;
}
void _bal_print_mangled_name(const char *mangledName, FILE *fp) {
const char *localName;
enum DemangleResult res = demangle(mangledName, &localName, NULL);
if (res == NON_PUBLIC_BALLERINA_NAME) {
fprintf(fp, "%s", localName);
}
else if (res == PUBLIC_BALLERINA_NAME) {
fprintf(fp, "%s (", localName);
demangle(mangledName, &localName, fp);
fputs(")", fp);
}
else if (res == NON_BALLERINA_NAME || res == BAD_BALLERINA_NAME) {
fprintf(fp, "%s", mangledName);
}
}
static enum DemangleResult demangle(const char *mangledName, const char **localName, FILE *fp) {
if (mangledName[0] != '_' || mangledName[1] != 'B') {
return NON_BALLERINA_NAME;
}
mangledName += 2;
if (mangledName[0] == '_') {
*localName = mangledName + 1;
return NON_PUBLIC_BALLERINA_NAME;
}
bool ballerinaOrg = false;
if (mangledName[0] == 'b') {
ballerinaOrg = true;
mangledName++;
}
const char *org;
if (mangledName[0] == '0') {
mangledName++;
org = NULL;
}
else {
if (!demangleCountedName(&mangledName, &org)) {
return BAD_BALLERINA_NAME;
}
}
if (fp != NULL) {
bool haveOrg = false;
if (ballerinaOrg) {
fputs("ballerina", fp);
haveOrg = true;
}
if (org != NULL) {
fwrite(org, 1, mangledName - org, fp);
haveOrg = true;
}
if (haveOrg) {
putc('/', fp);
}
}
if (!demangleModules(&mangledName, fp)) {
return NON_BALLERINA_NAME;
}
const char *lastModule;
if (!demangleCountedName(&mangledName, &lastModule)) {
return NON_BALLERINA_NAME;
}
*localName = mangledName;
if (fp != NULL) {
fwrite(lastModule, 1, mangledName - lastModule, fp);
}
return PUBLIC_BALLERINA_NAME;
}
static bool demangleModules(const char **mangledModules, FILE *fp) {
if (*mangledModules[0] != 'm') {
return true;
}
*mangledModules = *mangledModules + 1;
const char *module;
if (!demangleCountedName(mangledModules, &module)) {
return false;
}
if (fp != NULL) {
fwrite(module, 1, *mangledModules - module, fp);
fputs(".", fp);
}
return demangleModules(mangledModules, fp);
}
static bool demangleCountedName(const char **pp, const char **name) {
long nChars;
int nRead;
if (sscanf(*pp, "%ld%n", &nChars, &nRead) <= 0) {
return false;
}
*name = *pp + nRead;
if (*name[0] == '_') {
*name = *name + 1;
}
const char *p = *name;
while (nChars > 0) {
p++;
if (*p == '\0') {
return false;
}
nChars--;
}
*pp = p;
return true;
}