From 35a3c808c4ad5cc92076c11d31d2b77e9df41b7b Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Tue, 4 Apr 2023 22:50:12 +0200 Subject: [PATCH] Add code to debug errors when initialiting globals --- src/context.c | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/context.c b/src/context.c index e81a805..0b9ba5a 100644 --- a/src/context.c +++ b/src/context.c @@ -847,10 +847,50 @@ init_globals_protected(apfl_ctx ctx, void *opaque) } } +#define DEBUG_INIT_GLOBALS 1 + +#if DEBUG_INIT_GLOBALS +static void +on_init_globals_error(apfl_ctx ctx, void *opaque) +{ + (void)opaque; + + struct apfl_io_writer w = apfl_io_file_writer(stderr); + + apfl_tostring(ctx, -1); + + if (!( + apfl_io_write_string(w, apfl_get_string(ctx, -1)) + && apfl_io_write_string(w, "\n\nBacktrace:") + )) { + apfl_raise_const_error(ctx, "Error in on_init_globals_error"); + } + + size_t depth = apfl_call_stack_depth(ctx); + for (size_t i = 0; i < depth; i++) { + if (!( + apfl_io_write_string(w, "\n") + && apfl_io_write_string(w, "#") + && apfl_format_put_int(w, (int)i+1) + && apfl_io_write_string(w, ": ") + && apfl_call_stack_entry_info_format( + w, + apfl_call_stack_inspect(ctx, i) + ) + )) { + apfl_raise_const_error(ctx, "Error in on_init_globals_error"); + } + } +} +#define INIT_GLOBALS_ERRCALLBACK on_init_globals_error +#else +#define INIT_GLOBALS_ERRCALLBACK NULL +#endif + static bool init_globals(apfl_ctx ctx) { - return apfl_do_protected(ctx, init_globals_protected, NULL, NULL) == APFL_RESULT_OK; + return apfl_do_protected(ctx, init_globals_protected, NULL, INIT_GLOBALS_ERRCALLBACK) == APFL_RESULT_OK; } apfl_ctx