In the completion of the company project, the test process, often found that the log to a particular piece of code when the time is gone, the process directly exited, and did not capture any of the exception information, if the log printing more may be easier to find the problem, if the log is less, it is difficult to further check the wrong.
But discovering that a core file was generated in that directory can help us find out why the program crashed.
1. What is a core file
Under a Linux system, a core file may be generated if the process is not functioning properly. The core file is an image of the current memory state, plus some debugging information.
Bugs and operating system or hardware protection mechanisms can cause programs to terminate abnormally, and the operating system will kill those processes and produce a core file.
2. Why my Linux does not generate a core file
Use the ulimit-a command to view some of the throttling information for the current system resource, as shown in:
If the core file size is set to 0, it will not produce a core when the program crashes.
#设置core文件大小为无限
Ulimit-c Unlimited
#阻止系统生成core文件
Ulimit-c 0
Note: This command is only currently in effect, and if you want it to be permanent, you need to add this command to the. Bash_profile.
3. Set the core dump file directory and naming rules
/proc/sys/kernel/core_uses_pid can control whether a PID is added to the file name of the resulting core file, or if it is added, the content is 1, otherwise 0. You need to have superuser privileges to make modifications.
/proc/sys/kernel/core_pattern can set the format of the core file save location or file name, the default is |/usr/libexec/abrt-hook-ccpp%s%c%p%u%g%t E. If you need to change it, you can use the following command:
echo "/corefile/core-%e-%p-%t" >/proc/sys/kernel/core_pattern
Will control the resulting core file will be stored in the/corefile directory, resulting in a file named core-command name-pid-timestamp
The following is a list of parameters:
%p-insert pid into filename add PID
%u-insert current UID to filename to add the present UID
%g-insert current GID into filename to add an existing GID
%s-insert signal that caused the coredump into the filename adds a signal that causes the core to be generated
%t-insert Unix time, the coredump occurred into filename when you add a core file when you build Unix
%h-insert hostname where the coredump happened into filename adds host name
%e-insertcoredumping executable name into filename add command name
4. Using the core file
On Linux, you can use GDB to debug a core file in the following format:
GDB [program name] [core file name]
If you don't know which program is generating this core file, you can use
Gdb-c[core file name] to see the name of the program that generated this core file.
The program name can be seen in the display results, possibly like this
Core wasgenerated by './test '.
After entering the GDB debug state, the input where can see the stack information when the program crashes (the list of all called functions before the current function, including the current function), we can find out which part of the program caused the program to crash. Note: The option-G is added when compiling the program.
5. A simple example
Compile the following program:
#include <iostream>
using namespace Std;
Class A
{
Public
int A;
};
void Fun ()
{
A*t = new A ();
T->a = 1;
cout << t->a << Endl;
Delete T;
Delete T;
}
int main ()
{
Fun ();
return 0;
}
Perform:
./test
The result is:
1
GLIBC detected * * *./test:double freeor Corruption (fasttop): 0x09fd7008 * * *
======= BackTrace: =========
/LIB/LIBC.SO.6[0X3EBE31]
/usr/lib/libstdc++.so.6 (_zdlpv+0x22) [0x43fc552]
./test[0x8048705]
./test[0x8048712]
/lib/libc.so.6 (__libc_start_main+0xe6) [0X391D26]
./test[0x8048611]
======= Memory Map: ========
00327000-00328000 R-xp 00000000 00:000 [VDSO]
00334000-00351000 R-xp 00000000 08:02926955/lib/libgcc_s-4.4.7-20120601.so.1
00351000-00352000 rw-p 0001d000 08:02926955/lib/libgcc_s-4.4.7-20120601.so.1
00355000-00373000 R-xp 00000000 08:02926876/lib/ld-2.12.so
00373000-00374000 r--p 0001d000 08:02926876/lib/ld-2.12.so
00374000-00375000 rw-p 0001e000 08:02926876/lib/ld-2.12.so
0037b000-0050c000 R-xp 00000000 08:02926877/lib/libc-2.12.so
0050c000-0050e000 r--p 00191000 08:02926877/lib/libc-2.12.so
0050e000-0050f000 rw-p 00193000 08:02926877/lib/libc-2.12.so
0050f000-00512000 Rw-p 00000000 00:00 0
00543000-0056b000 R-xp 00000000 08:02926889/lib/libm-2.12.so
0056b000-0056c000 r--p 00027000 08:02926889/lib/libm-2.12.so
0056c000-0056d000 rw-p 00028000 08:02926889/lib/libm-2.12.so
0434d000-0442e000 R-xp 00000000 08:02155001/usr/lib/libstdc++.so.6.0.13
0442e000-04432000 r--p 000e0000 08:02155001/usr/lib/libstdc++.so.6.0.13
04432000-04434000 rw-p 000e4000 08:02155001/usr/lib/libstdc++.so.6.0.13
04434000-0443a000 Rw-p 00000000 00:00 0
08048000-08049000 R-xp 00000000 08:02419326/home/wcl/fate/src/app/test/test
08049000-0804a000 Rw-p 00000000 08:02419326/home/wcl/fate/src/app/test/test
09fd7000-09ff8000 Rw-p 00000000 00:000 [Heap]
b7719000-b771c000 Rw-p 00000000 00:00 0
b7727000-b772a000 Rw-p 00000000 00:00 0
bfd2a000-bfd3f000 Rw-p 00000000 00:000 [Stack]
Aborted (core dumped)
Because we delete a deleted pointer again, so the program is down, you can see in the current directory has generated a core.4377 file, 4377 is the previous program started PID.
To debug a core file:
GDB Test core.4377
After entering GDB debugging, type the WHERE command:
(GDB) where
#0 0x00327424 in __kernel_vsyscall ()
#1 0x003a5b11 in Raise () from/lib/libc.so.6
#2 0x003a73ea in Abort () from/lib/libc.so.6
#3 0x003e59d5 in __libc_message () from/lib/libc.so.6
#4 0x003ebe31in Malloc_printerr () from/lib/libc.so.6
#5 0x043fc552 in operator delete (void*) () from/usr/lib/libstdc++.so.6
#6 0x08048705 in Fun () ()
#7 0x08048712 in Main ()
It is obvious that you call the fun function in the main function, then the delete pointer is wrong, and the subsequent function call stack is the part of the program's output error message, regardless of our user code. At this point, we can infer that it is fun () when you delete a pointer in this function, there is an error, so that you can find the specific problem in a targeted way.
Linux Core file debugging