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