zoukankan      html  css  js  c++  java
  • 使用 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 ", 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 ", 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 ", 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





  • 相关阅读:
    LeetCode "Super Ugly Number" !
    LeetCode "Count of Smaller Number After Self"
    LeetCode "Binary Tree Vertical Order"
    LeetCode "Sparse Matrix Multiplication"
    LeetCode "Minimum Height Tree" !!
    HackerRank "The Indian Job"
    HackerRank "Poisonous Plants"
    HackerRank "Kundu and Tree" !!
    LeetCode "Best Time to Buy and Sell Stock with Cooldown" !
    HackerRank "AND xor OR"
  • 原文地址:https://www.cnblogs.com/JonnyLulu/p/4485842.html
Copyright © 2011-2022 走看看