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

相关推荐
qq_459730032 小时前
4-3 MCU中ARM存储器的作用
arm开发·单片机·嵌入式硬件
轩辰~12 小时前
网络协议入门
linux·服务器·开发语言·网络·arm开发·c++·网络协议
ARM&开发(Haidong)12 小时前
ARM 获取cpu个数
arm开发
憧憬一下2 天前
PCIe_Host驱动分析_设备枚举
arm开发·嵌入式硬件·嵌入式·pcie·linux驱动开发
7yewh2 天前
嵌入式Linux QT+OpenCV基于人脸识别的考勤系统 项目
linux·开发语言·arm开发·驱动开发·qt·opencv·嵌入式linux
憧憬一下4 天前
PCIe_Host驱动分析_地址映射
arm开发·嵌入式硬件·嵌入式·linux驱动开发·pci/pcie
楼兰公子4 天前
相机主要调试参数
arm开发·驱动·camera·v4l2
7yewh4 天前
嵌入式驱动RK3566 HDMI eDP MIPI 背光 屏幕选型与调试提升篇-eDP屏
linux·arm开发·驱动开发·嵌入式硬件·嵌入式linux·rk·edp
千千道6 天前
深入理解 Linux 内核启动流程
linux·arm开发·驱动开发
徐某人..6 天前
ARM嵌入式学习--第八天(PWM)
arm开发·学习·arm