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 /builtins | |
| parent | 817235cfbc3162e136d53ec5bffe234d4d87c79b (diff) | |
Print stack trace if $TOMO_STACKTRACE is set
Diffstat (limited to 'builtins')
| -rw-r--r-- | builtins/functions.c | 11 | ||||
| -rw-r--r-- | builtins/functions.h | 1 |
2 files changed, 7 insertions, 5 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 |
