有的程序可以通过编译, 但在运行时会出现Segment fault(段错误). 这通常都是指针错误引起的.但这不像编译错误一样会提示到文件->行, 而是没有任何信息, 使得我们的调试变得困难起来.

1.在一般Linux系统中,默认是不会产生core dump文件的。通过ulimit -c来查看core dump文件的大小,一般开始是0。
    可以设置core文件大小,ulimit -c 1024(kbytes单位)或者ulimit -c unlimited。(注: 使用-c unlimited不限制core文件大小)
2.生成core文件: $ sudo  sh -c "ulimit -c unlimited; ./main_arm" # main_arm 是可执行文件名。
3.记得在编译时加上 -g 方便调试,注意不要加上优化选项 -Os(或者-O0 -O1 ....)
4.用gdb查看core文件:
    gdb [exec file] [core file]
    如:gdb main_arm core
    在进入gdb后, 用 bt 或 where 命令查看backtrace以检查发生程序运行到哪里, 来定位core dump的文件行.
5.举例:

  1. 一个程序cmm_test_tool在运行的时候发生了错误,并生成了一个core文件,如下:
  2. -rw-r–r– 1 root cmm_test_tool.c
  3. -rw-r–r– 1 root cmm_test_tool.o
  4. -rwxr-xr-x 1 root cmm_test_tool
  5. -rw——- 1 root core.19344
  6. -rw——- 1 root core.19351
  7. -rw-r–r– 1 root cmm_test_tool.cfg
  8. -rw-r–r– 1 root cmm_test_tool.res
  9. -rw-r–r– 1 root cmm_test_tool.log
  10. [root@AUTOTEST_SIM2 mam2cm]#
  11. 就可以利用命令gdb进行查找,参数一是应用程序的名称,参数二是core文件,运行
  12. gdb cmm_test_tool core.19344结果如下:
  13. [root@AUTOTEST_SIM2 mam2cm]# gdb cmm_test_tool core.19344
  14. GNU gdb Red Hat Linux (5.2.1-4)
  15. Copyright 2002 Free Software Foundation, Inc.
  16. GDB is free software, covered by the GNU General Public License, and you are
  17. welcome to change it and/or distribute copies of it under certain conditions.
  18. Type “show copying” to see the conditions.
  19. There is absolutely no warranty for GDB. Type “show warranty” for details.
  20. This GDB was configured as “i386-redhat-linux”…
  21. Core was generated by `./cmm_test_tool’.
  22. Program terminated with signal 11, Segmentation fault.
  23. Reading symbols from /lib/i686/libpthread.so.0…done.
  24. Loaded symbols for /lib/i686/libpthread.so.0
  25. Reading symbols from /lib/i686/libm.so.6…done.
  26. Loaded symbols for /lib/i686/libm.so.6
  27. Reading symbols from /usr/lib/libz.so.1…done.
  28. Loaded symbols for /usr/lib/libz.so.1
  29. Reading symbols from /usr/lib/libstdc++.so.5…done.
  30. Loaded symbols for /usr/lib/libstdc++.so.5
  31. Reading symbols from /lib/i686/libc.so.6…done.
  32. Loaded symbols for /lib/i686/libc.so.6
  33. Reading symbols from /lib/libgcc_s.so.1…done.
  34. Loaded symbols for /lib/libgcc_s.so.1
  35. Reading symbols from /lib/ld-linux.so.2…done.
  36. Loaded symbols for /lib/ld-linux.so.2
  37. Reading symbols from /lib/libnss_files.so.2…done.
  38. Loaded symbols for /lib/libnss_files.so.2
  39. #0 0×4202cec1 in __strtoul_internal () from /lib/i686/libc.so.6
  40. (gdb)
  41. 进入gdb提示符,输入where,找到错误发生的位置和堆栈,如下:
  42. (gdb) where
  43. #0 0×4202cec1 in __strtoul_internal () from /lib/i686/libc.so.6
  44. #1 0×4202d4e7 in strtoul () from /lib/i686/libc.so.6
  45. #2 0×0804b4da in GetMaxIDFromDB (get_type=2, max_id=0×806fd20) at cmm_test_tool.c:788
  46. #3 0×0804b9d7 in ConstrctVODProgram (vod_program=0×40345bdc) at cmm_test_tool.c:946
  47. #4 0×0804a2f4 in TVRequestThread (arg=0×0) at cmm_test_tool.c:372
  48. #5 0×40021941 in pthread_start_thread () from /lib/i686/libpthread.so.0
  49. (gdb)

至此,可以看出文件出错的位置是函数 GetMaxIDFromDB ,两个参数分别是2和0×806fd20,这个函数位于源代码的788行,基于此,我们就可以有针对性的找到问题的根源,并加以解决

最新文章

  1. Reverse Linked List II 单向链表逆序(部分逆序)
  2. jekins构建自动化项目的步骤
  3. 使用IronPython给.Net程序加点料
  4. Linux1:Linux概述
  5. PhantomJS快速入门
  6. gif 图片制作和拆解
  7. HDU 1494 跑跑卡丁车
  8. 跟我学习dubbo-ZooKeeper注册中心安装(2)
  9. 关于main()和_tmain()
  10. Linux - 标准输入转换为标准输出 代码(C)
  11. (原)Apache添加完限速模块后的文件
  12. Netfilter-packet-flow.svg
  13. SpringMVC详解(二)------详细架构
  14. SQL之Left Join 关联条件的探讨
  15. Windows Server 2016-域站点链接及复制时间调整
  16. hadoop故障及其应对
  17. html DOM簡介
  18. JAVA中的Set
  19. 4.动态HTML处理和机器图像识别
  20. Cloud Commander

热门文章

  1. Android 批量上传sd卡图片
  2. JFS 文件系统概述及布局分析
  3. AlgorithmsI PA2: Randomized Queues and Deques Deque
  4. 今天愉快的hack小记
  5. Python操作Excel_输出所有内容(包含中文)
  6. windows下sqlite3静态库和动态库的编译
  7. HDOJ 2014 青年歌手大奖赛_评委会打分
  8. nyoj 222 整数中的1个数以及这类问题
  9. 用Myeclipse 编写struts.xml时,自动提示
  10. VC++ 动态检测串口的热插拔(一)通过注册表实现