diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-09-06 14:41:34 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-09-06 14:41:34 -0400 |
| commit | 176205a22de8bc356109edc2aceaaa07ae1d24a4 (patch) | |
| tree | 25d1f9052c940be4fcd8d38a38d23e377d6d7c56 | |
| parent | 817235cfbc3162e136d53ec5bffe234d4d87c79b (diff) | |
Print stack trace if $TOMO_STACKTRACE is set
| -rw-r--r-- | builtins/functions.c | 11 | ||||
| -rw-r--r-- | builtins/functions.h | 1 | ||||
| -rw-r--r-- | environment.c | 8 | ||||
| -rw-r--r-- | parse.c | 5 |
4 files changed, 18 insertions, 7 deletions
diff --git a/builtins/functions.c b/builtins/functions.c index 94dda009..22347afb 100644 --- a/builtins/functions.c +++ b/builtins/functions.c @@ -41,7 +41,7 @@ public void tomo_init(void) errx(1, "Couldn't set printf specifier"); } -static void print_stack_trace(FILE *out) +void print_stack_trace(FILE *out, int start, int stop) { // Print stack trace: fprintf(out, "\x1b[34m"); @@ -49,7 +49,7 @@ static void print_stack_trace(FILE *out) void *array[1024]; int64_t size = (int64_t)backtrace(array, sizeof(array)/sizeof(array[0])); char **strings = strings = backtrace_symbols(array, size); - for (int64_t i = 2; i < size - 4; i++) { + for (int64_t i = start; i < size - stop; i++) { char *filename = strings[i]; const char *cmd = heap_strf("addr2line -e %.*s -fisp | sed 's/\\$/./g;s/ at /() at /' >&2", strcspn(filename, "("), filename); FILE *fp = popen(cmd, "w"); @@ -60,6 +60,7 @@ static void print_stack_trace(FILE *out) pclose(fp); } fprintf(out, "\x1b[m"); + fflush(out); } public void fail(const char *fmt, ...) @@ -72,7 +73,7 @@ public void fail(const char *fmt, ...) if (USE_COLOR) fputs("\x1b[m", stderr); fputs("\n\n", stderr); va_end(args); - print_stack_trace(stderr); + print_stack_trace(stderr, 2, 4); fflush(stderr); raise(SIGABRT); } @@ -95,7 +96,7 @@ public void fail_source(const char *filename, int64_t start, int64_t end, const } if (USE_COLOR) fputs("\x1b[m", stderr); - print_stack_trace(stderr); + print_stack_trace(stderr, 2, 4); fflush(stderr); raise(SIGABRT); } @@ -240,7 +241,7 @@ public void end_test(const void *expr, const TypeInfo *type, const char *expecte : "\n==================== TEST FAILED ====================\nExpected: %s\n\n But got: %k\n\n", expected, &expr_text); - print_stack_trace(stderr); + print_stack_trace(stderr, 2, 4); fflush(stderr); raise(SIGABRT); } diff --git a/builtins/functions.h b/builtins/functions.h index 372c42c4..e41326c2 100644 --- a/builtins/functions.h +++ b/builtins/functions.h @@ -28,5 +28,6 @@ Text_t generic_as_text(const void *obj, bool colorize, const TypeInfo *type); int generic_print(const void *obj, bool colorize, const TypeInfo *type); closure_t spawn(closure_t fn); bool pop_flag(char **argv, int *i, const char *flag, Text_t *result); +void print_stack_trace(FILE *out, int start, int stop); // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/environment.c b/environment.c index 39a3e24a..0c37b266 100644 --- a/environment.c +++ b/environment.c @@ -3,11 +3,12 @@ #include <stdlib.h> #include <signal.h> -#include "environment.h" +#include "builtins/functions.h" #include "builtins/table.h" #include "builtins/text.h" -#include "typecheck.h" #include "builtins/util.h" +#include "environment.h" +#include "typecheck.h" type_t *TEXT_TYPE = NULL; type_t *RANGE_TYPE = NULL; @@ -588,6 +589,9 @@ void compiler_err(file_t *f, const char *start, const char *end, const char *fmt if (f && start && end) highlight_error(f, start, end, "\x1b[31;1m", 2, isatty(STDERR_FILENO) && !getenv("NO_COLOR")); + if (getenv("TOMO_STACKTRACE")) + print_stack_trace(stderr, 1, 3); + raise(SIGABRT); exit(1); } @@ -11,6 +11,7 @@ #include <signal.h> #include "ast.h" +#include "builtins/functions.h" #include "builtins/integers.h" #include "builtins/text.h" #include "builtins/table.h" @@ -134,8 +135,12 @@ static void vparser_err(parse_ctx_t *ctx, const char *start, const char *end, co highlight_error(ctx->file, start, end, "\x1b[31;1;7m", 2, isatty(STDERR_FILENO) && !getenv("NO_COLOR")); fputs("\n", stderr); + if (getenv("TOMO_STACKTRACE")) + print_stack_trace(stderr, 1, 3); + if (ctx->on_err) longjmp(*ctx->on_err, 1); + raise(SIGABRT); exit(1); } |
