sighandler.cpp
#include <asm/sigcontext.h>
struct ucontext {
unsigned long uc_flags;
struct ucontext *uc_link;
stack_t uc_stack;
struct sigcontext uc_mcontext;
sigset_t uc_sigmask; /* mask last for extensibility */
};
static void throwCppException() {
throw 1;
}
static void sig_handler(int sig, siginfo_t *si, void *unused) {
ucontext* uc = (ucontext*)unused;
#ifdef __thumb__
uc->uc_mcontext.arm_lr = (uc->uc_mcontext.arm_pc + 2) | 1;
uc->uc_mcontext.arm_pc = (int)(void*)throwCppException;
#elif defined __arm__
uc->uc_mcontext.arm_lr = (uc->uc_mcontext.arm_pc + 4);
uc->uc_mcontext.arm_pc = (int)(void*)throwCppException;
#elif defined(__i386__) || defined(__x86_64__)
(!!! Not Implemented !!!)
#endif
// just return. do not throw in sig_handler directly;
return;
}
void catch_signal_test() {
struct sigaction sa;
struct sigaction newAction, orgAction;
sigemptyset(&newAction.sa_mask);
newAction.sa_sigaction = sig_handler;
newAction.sa_flags = SA_RESTART | SA_SIGINFO;
sigaction(SIGSEGV, &newAction, &orgAction);
// for Test
try {
volatile int* null_p = 0;
*null_p = 1;
}
catch (int a) {
ALOGD("Exception catched!!");
}
}