Print stack trace if $TOMO_STACKTRACE is set
This commit is contained in:
parent
817235cfbc
commit
176205a22d
@ -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);
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
|
5
parse.c
5
parse.c
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user