ARM 使用 backtrace 调测 SIGSEGV 信号 ---- 打印寄存器值

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

相关推荐
韦德斯21 小时前
嵌入式Linux的RTC读写操作应用
linux·运维·c语言·arm开发·实时音视频
byte轻骑兵1 天前
嵌入式 ARM Linux 系统构成全解:从硬件到应用层层剖析
linux·arm开发·arm·嵌入式开发
思尔芯S2C1 天前
面向未来的智能视觉参考设计与汽车架构,思尔芯提供基于Arm技术的创新方案
arm开发·架构·汽车·iot·fpga原型验证·prototyping·智慧视觉
Eternal-Student2 天前
【docker了解】如何将x86镜像转换为适用于Jetson的ARM镜像
arm开发·docker·容器
不怕犯错,就怕不做2 天前
修复kernel编译栈帧大小异常问题error: the frame size of 1928 bytes is larger than 1024 bytes
linux·arm开发·驱动开发
憧憬一下3 天前
UART硬件介绍
arm开发·嵌入式硬件·串口·嵌入式·linux驱动开发
Petal9909124 天前
UEFI学习笔记(十八):ARM电源管理之PSCI和SCMI概述
arm开发·笔记·学习·uefi
古月居GYH4 天前
一文了解ARM内部架构
arm开发·架构
白书宇4 天前
13.100ASK_T113-PRO RTC实验
linux·arm开发·驱动开发·嵌入式硬件·物联网·硬件工程
简简单单一天吃六顿5 天前
rootfs根文件系统在Linux下制作动态库
linux·服务器·arm开发·iot