From 176205a22de8bc356109edc2aceaaa07ae1d24a4 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 6 Sep 2024 14:41:34 -0400 Subject: Print stack trace if $TOMO_STACKTRACE is set --- builtins/functions.c | 11 ++++++----- builtins/functions.h | 1 + 2 files changed, 7 insertions(+), 5 deletions(-) (limited to 'builtins') 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 -- cgit v1.2.3