有时候, 在特殊需要时, 我们可以让程序主动core掉, 来查看当时的内容。 本文来玩玩这个:

 

#include <iostream>
using namespace std;

class Point
{
public:
	int x;
	int y;	
};

int main()
{
	int a = 1;
	int b = 2;

	Point M;
	M.x = 3;
	M.y = 4;

	int *p = 0;
	*p = 0;
	
	return 0;
}

      看看吧:

 

 

taoge@localhost Desktop> rm core*; g++ -g main.cpp && ./a.out 
Segmentation fault (core dumped)
taoge@localhost Desktop> gdb a.out co*
GNU gdb (GDB) Red Hat Enterprise Linux (7.1-29.el6)
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
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-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/taoge/Desktop/a.out...done.
[New Thread 2937]
Missing separate debuginfo for 
Try: yum --disablerepo='*' --enablerepo='*-debuginfo' install /usr/lib/debug/.build-id/74/d23352fd770753e375bd0caecf375bd77bded5
Reading symbols from /usr/lib/libstdc++.so.6...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/libstdc++.so.6
Reading symbols from /lib/libm.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib/libm.so.6
Reading symbols from /lib/libgcc_s.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib/libgcc_s.so.1
Reading symbols from /lib/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib/ld-linux.so.2
Core was generated by `./a.out'.
Program terminated with signal 11, Segmentation fault.
#0  0x08048570 in main () at main.cpp:21
21		*p = 0;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.7.el6.i686 libgcc-4.4.4-13.el6.i686 libstdc++-4.4.4-13.el6.i686
(gdb) 
(gdb) 
(gdb) 
(gdb) bt
#0  0x08048570 in main () at main.cpp:21
(gdb) i locals
a = 1
b = 2
M = {x = 3, y = 4}
p = 0x0
(gdb) p a
$1 = 1
(gdb) p b
$2 = 2
(gdb) p M
$3 = {x = 3, y = 4}
(gdb) p p
$4 = (int *) 0x0
(gdb) p *p
Cannot access memory at address 0x0
(gdb) 

      总之, 获取了变量的值, 也知道core的原因了。

 

 

 

 


本文转载:CSDN博客