使用 core dump 查找程序遇到严重问题退出的原因

不使用 ulimit 命令,在程序中使用 API 开启 core dump。注意:只对当前程序有效。
 
   
  1. #include <sys/resource.h>
  2. int enableCoreDump(void)
  3. {
  4. struct rlimit r_old, r_new;
  5. getrlimit(RLIMIT_CORE, &r_old);
  6. printf("r_old.rlim_cur : %d, r_old.rlim_max : %d\n", r_old.rlim_cur, r_old.rlim_max);
  7. r_new.rlim_cur = RLIM_INFINITY;
  8. r_new.rlim_max = RLIM_INFINITY;
  9. if (setrlimit(RLIMIT_CORE, &r_new) != 0) {
  10. getrlimit(RLIMIT_CORE, &r_old);
  11. printf("r_old.rlim_cur : %d, r_old.rlim_max : %d\n", r_old.rlim_cur, r_old.rlim_max);
  12. }
  13. r_new.rlim_cur = r_old.rlim_max;
  14. r_new.rlim_max = r_old.rlim_max;
  15. setrlimit(RLIMIT_CORE, &r_new);
  16. getrlimit(RLIMIT_CORE, &r_old);
  17. printf("r_old.rlim_cur : %d, r_old.rlim_max : %d\n", r_old.rlim_cur, r_old.rlim_max);
  18. return 0;
  19. }
编译程序时注意使用 -g 标志,加入调试信息,否则调试时只能看到函数地址,看不到函数名。
程序遇到严重错误退出后,会在启动程序的目录下生成 core 文件。将 core 文件复制到主机,用交叉编译器提供的gdb 打开。
arm-linux-gdb ./a.out core
输入 where,即可看到函数在哪退出的,以及函数调用栈。下面是输入 where 后,输出的函数调用栈:
 
   
  1. Program terminated with signal SIGSEGV, Segmentation fault.
  2. #0 0x00008c48 in cJSON_GetArrayItem (array=<optimized out>, item=171880)
  3. at cJSON.c:662
  4. 662 cJSON *cJSON_GetArrayItem(cJSON *array,int item) {cJSON *c=array->child; while (c && item>0) item--,c=c->next; return c;}
  5. (gdb) where
  6. #0 0x00008c48 in cJSON_GetArrayItem (array=<optimized out>, item=171880)
  7. at cJSON.c:662
  8. #1 0x0000b8dc in addItem (cacheArray=0xe1a03000, statusArray=0x29f68,
  9. mode=CACHE_MODE_SYNC) at CacheManager.c:162
  10. #2 0x0000b8dc in addItem (cacheArray=0xe1a03000, statusArray=0x29f68,
  11. mode=CACHE_MODE_SYNC) at CacheManager.c:162
  12. #3 0x00000000 in ?? ()
  13. Backtrace stopped: frame did not save the PC





转载于:https://www.cnblogs.com/JonnyLulu/p/4485842.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值