static void ac_sigsegv_handler(int sig, siginfo_t *info, void *ctx)
{
ucontext_t *context = (ucontext_t *)ctx;
int i = 0;
/* dump registers, ARM CPU specific */
XAG_LOG_E( "Signal = %d Memory location = %p\n"
"R0 = %016X R1 = %016X R2 = %016X\n"
"R3 = %016X R4 = %016X R5 = %016X\n"
"R6 = %016X R7 = %016X\n"
"R8 = %016X R9 = %016X R10 = %016X\n"
"FP = %016X IP = %016X SP = %016X\n"
"LR = %016X PC = %016X \n\n",
sig, info->si_addr,
context->uc_mcontext.arm_r0,
context->uc_mcontext.arm_r1,
context->uc_mcontext.arm_r2,
context->uc_mcontext.arm_r3,
context->uc_mcontext.arm_r4,
context->uc_mcontext.arm_r5,
context->uc_mcontext.arm_r6,
context->uc_mcontext.arm_r7,
context->uc_mcontext.arm_r8,
context->uc_mcontext.arm_r9,
context->uc_mcontext.arm_r10,
context->uc_mcontext.arm_fp,
context->uc_mcontext.arm_ip,
context->uc_mcontext.arm_sp,
context->uc_mcontext.arm_lr,
context->uc_mcontext.arm_pc);
/* get call stack and write to stdout */
void *buf[256] = {0};
int n = backtrace(buf, 256);
XAG_LOG_E("backtrace() returned %d addresses", n);
char **symbols = backtrace_symbols(buf, n);
if(NULL == symbols){
XAG_LOG_E("backtrace symbols");
exit(EXIT_FAILURE);
}
for (i = 0; i < n; i++) {
XAG_LOG_E("%d: %s\n", i, symbols[i]);
}
free(symbols);
}
void test_3(int *p)
{
*p = 1;
}
void test_2(int *p)
{
test_3(p);
}
void test_1(int *p)
{
test_2(p);
}
void main(XAG_S32 argc, XAG_CHAR** argv)
{
int *p = (int *)0x12345678;
/* SIGSEGV */
struct sigaction action;
sigemptyset(&action.sa_mask);
action.sa_sigaction = ac_sigsegv_handler;
action.sa_flags = SA_SIGINFO;
sigaction(SIGSEGV, &action, NULL);
test_1(p);
}
#if 0
15fe68: b480 push {r7}
15fe6a: b083 sub sp, #12
15fe6c: af00 add r7, sp, #0
15fe6e: 6078 str r0, [r7, #4]
15fe70: 687b ldr r3, [r7, #4]
15fe72: 2201 movs r2, #1
15fe74: 601a str r2, [r3, #0] // 由R15(PC)寄存器来判断此处触发 SIGSEGV 信号
15fe76: bf00 nop
15fe78: 370c adds r7, #12
15fe7a: 46bd mov sp, r7
15fe7c: f85d 7b04 ldr.w r7, [sp], #4
15fe80: 4770 bx lr
[ac_sigsegv_handler ] Signal = 11 Memory location = 0x12345678
R0 = 0000000012345678 R1 = 000000007EA189F8 R2 = 0000000000000001
R3 = 0000000012345678 R4 = 000000007EA18CA8 R5 = 0000000000000000
R6 = 0000000000000000 R7 = 000000007EA18AF8
R8 = 0000000000000000 R9 = 0000000000000000 R10 = 0000000076F41000
FP = 0000000000000000 IP = 00000000000000AE SP = 000000007EA18AF8
LR = 000000000015FE91 PC = 000000000015FE74
[ac_sigsegv_handler ] backtrace() returned 2 addresses
[ac_sigsegv_handler ] 0: ./sc2000_app() [0x15f82a] // backtrace
[ac_sigsegv_handler ] 1: /lib/libc.so.6(+0x25030) [0x76c6a030] // __default_rt_sa_restorer
#endif