程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> 關於C >> C代碼的coredump

C代碼的coredump

編輯:關於C

要點:

程序異常終止之後會生成core代碼文件程序添加-g編譯選項,從而包含調試信息結合gdb和coredump文件定位異常點


程序異常終止之後會生成coredump文件,不同的系統、不同的設置,這個自動生成的coredump文件也不一樣。


示例代碼:

#include 

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)
[1]    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 
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:
.
Find the GDB manual and other documentation resources online at:
.
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

可以看到上面並沒有給出具體的代碼&行號,因為需要添加-g編譯選項。為此重新編譯、運行,此時的coredumnp文件就提供了全面的信息:

~/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 
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:
.
Find the GDB manual and other documentation resources online at:
.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.out...done.
[New LWP 9758]

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 () at main.c:7
7                       printf("(%d, %d)\n", i, 100 / i);
(gdb) where
#0  0x08048415 in core_dump () at main.c:7
#1  0x0804844b in main () at main.c:14
(gdb) 

補充:

當然,並不是所有的異常都會生成coredump文件,具體請參考《Unix環境高級編程(2nd)》第十章 信號。有些環境缺省設置coredump文件是0字節,為此需要ulimit -c 1024(文件大小)。


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved