From 5e44fe77ec35c251614839781823fffa385e562a Mon Sep 17 00:00:00 2001 From: theMackabu Date: Mon, 27 Apr 2026 23:30:11 -0700 Subject: [PATCH] musl libunwind --- meson/deps/meson.build | 11 ++++++++++- src/crash.c | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/meson/deps/meson.build b/meson/deps/meson.build index 0bb42db..6e80960 100644 --- a/meson/deps/meson.build +++ b/meson/deps/meson.build @@ -51,6 +51,15 @@ else win_deps = [] endif +crash_deps = [] +if host_machine.system() == 'linux' + libunwind_dep = dependency('libunwind', required: false, static: is_static) + if libunwind_dep.found() and cc.has_header('libunwind.h', dependencies: libunwind_dep) + crash_deps += [libunwind_dep] + add_project_arguments('-DANT_CRASH_HAVE_LIBUNWIND', language: 'c') + endif +endif + deps_info = { 'lmdb': { 'dir': 'openldap-LMDB_0.9.33', @@ -234,7 +243,7 @@ ant_deps = [ uriparser_dep, utf8proc_dep, ssl_dep, crypto_dep, zlib_dep, brotli_common_dep, brotli_dec_dep, brotli_enc_dep, uthash_dep, lmdb_dep, wamr_dep, -] + win_deps +] + win_deps + crash_deps if get_option('jit') mir_dep = subproject('mir').get_variable('mir_dep') diff --git a/src/crash.c b/src/crash.c index 23dee06..4c4af4b 100644 --- a/src/crash.c +++ b/src/crash.c @@ -67,10 +67,15 @@ bool ant_crash_is_internal_report(int argc, char **argv) { #include #endif -#if defined(__APPLE__) || defined(__linux__) || defined(__GLIBC__) +#if defined(__APPLE__) || defined(__GLIBC__) #define ANT_CRASH_HAVE_EXECINFO 1 #include #endif + +#ifdef ANT_CRASH_HAVE_LIBUNWIND +#define UNW_LOCAL_ONLY +#include +#endif #endif typedef struct { @@ -965,6 +970,34 @@ static void crash_handler(int sig, siginfo_t *info, void *ucontext) { int skip = n > 1 ? 1 : 0; for (int i = skip; i < n && frame_count < ANT_CRASH_FRAME_MAX; i++) frames[frame_count++] = (uintptr_t)raw_frames[i]; +#elif defined(ANT_CRASH_HAVE_LIBUNWIND) + unw_cursor_t cursor; + bool cursor_ready = false; + bool include_current_frame = false; + +#ifdef UNW_INIT_SIGNAL_FRAME + if (ucontext && unw_init_local2(&cursor, (unw_context_t *)ucontext, UNW_INIT_SIGNAL_FRAME) == 0) { + cursor_ready = true; + include_current_frame = true; + } +#endif + + unw_context_t context; + if (!cursor_ready && unw_getcontext(&context) == 0 && unw_init_local(&cursor, &context) == 0) + cursor_ready = true; + + if (cursor_ready) { + if (include_current_frame) { + unw_word_t ip = 0; + if (unw_get_reg(&cursor, UNW_REG_IP, &ip) == 0 && ip) + frames[frame_count++] = (uintptr_t)ip; + } + while (frame_count < ANT_CRASH_FRAME_MAX && unw_step(&cursor) > 0) { + unw_word_t ip = 0; + if (unw_get_reg(&cursor, UNW_REG_IP, &ip) == 0 && ip) + frames[frame_count++] = (uintptr_t)ip; + } + } #endif uint64_t fault_addr = info ? (uint64_t)(uintptr_t)info->si_addr : 0; -- 2.51.2