From 91959c81373eedf4d8561af22f0052a12585a03d Mon Sep 17 00:00:00 2001 From: ajbt200128 Date: Tue, 12 Aug 2025 14:49:39 -0700 Subject: [PATCH] chore: support apple m1 backtraces --- backward.hpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/backward.hpp b/backward.hpp index 2342b18..a075026 100644 --- a/backward.hpp +++ b/backward.hpp @@ -985,6 +985,29 @@ class StackTraceImpl : public StackTraceImplHolder { } _stacktrace[index] = reinterpret_cast(ctx.regs[UNW_ARM_R15]); ++index; + +#elif defined(__APPLE__) && (defined(__arm__) || defined(__arm64__)) //apple arm m1 + unw_getcontext(&ctx); + + // If the IP is the same as the crash address we have a bad function + // dereference The caller's address is pointed to by the link + // pointer, so we dereference that value and set it to be the next + // frame's IP. + if (uctx->uc_mcontext->__ss.__pc == + reinterpret_cast<__uint64_t>(error_addr())) { + uctx->uc_mcontext->__ss.__pc = uctx->uc_mcontext->__ss.__lr; + } + + // 29 general purpose registers + for (int i = 0; i <= 28; i++) { + ctx.data[i] = uctx->uc_mcontext->__ss.__x[i]; + } + ctx.data[29] = uctx->uc_mcontext->__ss.__fp; + ctx.data[30] = uctx->uc_mcontext->__ss.__lr; + ctx.data[31] = uctx->uc_mcontext->__ss.__sp; + ctx.data[32] = uctx->uc_mcontext->__ss.__pc; + _stacktrace[index] = reinterpret_cast(ctx.data[32]); + ++index; #elif defined(__aarch64__) // gcc libunwind/arm64 unw_getcontext(&ctx); // If the IP is the same as the crash address we have a bad function