swissChili | b71e027 | 2020-08-08 15:56:14 -0700 | [diff] [blame] | 1 | #include "common.h" |
| 2 | |
| 3 | #include <signal.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <stdio.h> |
| 6 | |
| 7 | catch_t g_catch[MAX_CATCH_LEN]; |
| 8 | unsigned g_catch_len; |
| 9 | |
swissChili | 4bccd44 | 2020-08-11 13:55:10 -0700 | [diff] [blame] | 10 | void unwind() |
swissChili | b71e027 | 2020-08-08 15:56:14 -0700 | [diff] [blame] | 11 | { |
swissChili | b71e027 | 2020-08-08 15:56:14 -0700 | [diff] [blame] | 12 | for (int i = g_catch_len - 1; i >= 0; i--) |
| 13 | { |
| 14 | g_catch[i].fn(g_catch[i].arg); |
| 15 | } |
swissChili | 4bccd44 | 2020-08-11 13:55:10 -0700 | [diff] [blame] | 16 | } |
swissChili | b71e027 | 2020-08-08 15:56:14 -0700 | [diff] [blame] | 17 | |
swissChili | 4bccd44 | 2020-08-11 13:55:10 -0700 | [diff] [blame] | 18 | void throw_(const char *msg, const char *file, unsigned int line) |
| 19 | { |
| 20 | fprintf(stderr, "\033[31mException thrown:\033[33m %s:%d\033[0m %s\n", file, line, msg); |
| 21 | unwind(); |
swissChili | b71e027 | 2020-08-08 15:56:14 -0700 | [diff] [blame] | 22 | exit(1); |
| 23 | } |
| 24 | |
| 25 | void catch_(handle_t hdl, intptr_t arg) |
| 26 | { |
| 27 | if (g_catch_len > MAX_CATCH_LEN) |
| 28 | { |
| 29 | THROW("Catch overflow"); |
| 30 | } |
| 31 | |
| 32 | g_catch[g_catch_len++] = (catch_t){ .fn = hdl, .arg = arg }; |
| 33 | } |
| 34 | |
| 35 | void catch_signal(int sig) |
| 36 | { |
| 37 | if (sig == SIGSEGV) |
| 38 | throw_("Segmentation fault", "unknown", 0); |
| 39 | } |
| 40 | |
| 41 | __attribute__((constructor)) void init_catch() |
| 42 | { |
| 43 | signal(SIGSEGV, catch_signal); |
| 44 | } |