From 69fffdb52d5d73417996eccb48ae61f66dff2681 Mon Sep 17 00:00:00 2001 From: Tongze Wang Date: Fri, 27 Feb 2026 13:22:46 +0800 Subject: [PATCH] [backtrace] add `otSysRegisterCrashCallback` to register crash callback function (#12552) This commit adds `otSysRegisterCrashCallback` function which can be used to register the callback function to be called when openthread crashes. --- src/posix/platform/backtrace.cpp | 10 ++++++++++ .../include/openthread/openthread-system.h | 17 +++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/posix/platform/backtrace.cpp b/src/posix/platform/backtrace.cpp index 988718d7a..fb05dcbc3 100644 --- a/src/posix/platform/backtrace.cpp +++ b/src/posix/platform/backtrace.cpp @@ -40,10 +40,15 @@ #include #include +#include + #include "common/code_utils.hpp" #include "common/logging.hpp" #if OPENTHREAD_POSIX_CONFIG_BACKTRACE_ENABLE +static otSysCrashCallback sCrashCallback = nullptr; + +void otSysRegisterCrashCallback(otSysCrashCallback aCallback) { sCrashCallback = aCallback; } #if OPENTHREAD_POSIX_CONFIG_ANDROID_ENABLE || defined(__GLIBC__) #if OPENTHREAD_POSIX_CONFIG_ANDROID_ENABLE #include @@ -156,6 +161,11 @@ static void signalCritical(int sig, siginfo_t *info, void *ucontext) otLogCritPlat("------------------ END OF CRASH ------------------"); + if (sCrashCallback != nullptr) + { + sCrashCallback(); + } + resetSignalActions(); raise(sig); } diff --git a/src/posix/platform/include/openthread/openthread-system.h b/src/posix/platform/include/openthread/openthread-system.h index fc4d51d8d..adc7caa33 100644 --- a/src/posix/platform/include/openthread/openthread-system.h +++ b/src/posix/platform/include/openthread/openthread-system.h @@ -330,6 +330,23 @@ void otSysTrelDeinit(void); */ void otSysSetRcpRestorationEnabled(bool aEnabled); +/** + * Represents a callback function to be called when openthread crashes. + * + * @note This callback is invoked from a signal handler context. The callback implementation must only use + * async-signal-safe functions. + */ +typedef void (*otSysCrashCallback)(void); + +/** + * Registers the callback function to be called when openthread crashes. + * + * Requires `OPENTHREAD_POSIX_CONFIG_BACKTRACE_ENABLE`. + * + * @param[in] aCallback The callback function. + */ +void otSysRegisterCrashCallback(otSysCrashCallback aCallback); + #ifdef __cplusplus } // end of extern "C" #endif