设为首页 加入收藏

TOP

C代码的coredump(一)
2014-03-10 13:04:52 来源: 作者: 【 】 浏览:344
Tags:代码 coredump

  要点:

  程序异常终止之后会生成core代码文件程序添加-g编译选项,从而包含调试信息结合gdb和coredump文件定位异常点

  程序异常终止之后会生成coredump文件,不同的系统、不同的设置,这个自动生成的coredump文件也不一样。

  示例代码:

  #include <STDIO.H>

  int core_dump() {

  int i;

  for (i = 5; i >= 0; i--) {

  printf("(%d, %d)\n", i, 100 / i);

  }

  return 0;

  }

  int main() {

  core_dump();

  return 0;

  }

  以上代码存在除0异常,编译运行:

  gcc main.c

  ./a.out

  运行结果:

  (5, 20)

  (4, 25)

  (3, 33)

  (2, 50)

  (1, 100)

       9662 floating point exception (core dumped)  ./a.out

  为了更清楚地找到代码出错的地方,可以使用gdb来分析coredump文件:

  ~/examples/cpp/core_dump % gdb ./a.out core

  GNU gdb (GDB) 7.7

  Copyright (C) 2014 Free Software Foundation, Inc.

  License GPLv3+: GNU GPL version 3 or later <HTTP: gpl.html licenses gnu.org>

  This is free software: you are free to change and redistribute it.

  There is NO WARRANTY, to the extent permitted by law.  Type "show copying"

  and "show warranty" for details.

  This GDB was configured as "i686-pc-linux-gnu".

  Type "show configuration" for configuration details.

  For bug reporting instructions, please see:

  <HTTP: bugs gdb software www.gnu.org />.

  Find the GDB manual and other documentation resources online at:

  <HTTP: gdb software www.gnu.org documentation />.

  For help, type "help".

  Type "apropos word" to search for commands related to "word"…

  Reading symbols from ./a.out…done.

  [New LWP 9662]

  warning: Could not load shared library symbols for linux-gate.so.1.

  Do you need "set solib-search-path" or "set sysroot"

  Core was generated by `./a.out'.

  Program terminated with signal SIGFPE, Arithmetic exception.

  #0  0x08048415 in core_dump ()

  (gdb) where

  #0  0x08048415 in core_dump ()

  #1  0x0804844b in main ()

  (gdb) q

   

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇typedef 和 #define&nb.. 下一篇C语言排列搜索

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: