[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.
This commit is contained in:
Tongze Wang
2026-02-27 13:22:46 +08:00
committed by GitHub
parent 425adf50f1
commit 69fffdb52d
2 changed files with 27 additions and 0 deletions
+10
View File
@@ -40,10 +40,15 @@
#include <string.h>
#include <unistd.h>
#include <openthread/openthread-system.h>
#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 <log/log.h>
@@ -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);
}
@@ -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