Add code to debug errors when initialiting globals

This commit is contained in:
Laria 2023-04-04 22:50:12 +02:00
parent 4d01f20d6e
commit 35a3c808c4

View file

@ -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