1. Overview
After the C program crashes, it automatically stores, using this information to quickly locate the wrong place.
However, to correctly use this feature, you need to note the following points:
Code at compile time, add debug Option-G to set the size of the storage (some system defaults to 0) combine gdb and coredump files to locate outliers.
2. Example 1
Sample code:
#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;
}
The code above has 0 exceptions, compiled to run:
GCC main.c
./a.out
Run Result:
(5)
(4)
(3,)
(2)
(1, MB)
[1] 9662 floating point exception (core dumped) ./a.out
To find out more clearly where the code went wrong, you can use GDB to parse the Coredump file:
~/examples/cpp/core_dump% gdb./a.out Core GNU gdb (GDB) 7.7 Copyright (C) 2014 Free Software Foundation, Inc. License GP lv3+: GNU GPL version 3 or later
You can see that no specific code & line number is given, because the-G compilation option needs to be added. Recompiling and running this time, the COREDUMNP file provides comprehensive information:
~/examples/cpp/core_dump% gdb./a.out Core GNU gdb (GDB) 7.7 Copyright (C) 2014 Free Software Foundation, Inc. License GP lv3+: GNU GPL version 3 or later
3. Sample 2:ulimit Set the size of the spin storage
The following example is the first time to use memory-transfer storage after a new installation of Ubuntu 12, during which the core file cannot be found after a transfer is encountered. At this point, you need to use the Ulimit command to set the size of the transfer store.
The code is as follows:
#include <stdio.h>//fopen etc
#include <string.h>//strlen
int main ()
{
Const char* s = " Hello ";
file* fp = NULL;
fp = fopen ("./hello_c.txt", "RB");
Fwrite (S, 1, strlen (s), FP);
Fclose (FP);
return 0;
}
Compile, run:
flying-bird@flying-bird:~/examples/cpp/echo$ gcc-g./hello.c
flying-bird@flying-bird:~/examples/cpp/echo$./ A.out
Segment Error (Core has been dumped)
flying-bird@flying-bird:~/examples/cpp/echo$ ll
total dosage
drwxrwxr-x 2 Flying-bird Flying-bird 4096 May 15:19./
drwxrwxr-x 3 flying-bird flying-bird 4096 May 10 15:19. /
-rwxrwxr-x 1 flying-bird flying-bird 9464 May 15:19 a.out*-rw-rw-r--
1 flying-bird flying-bird 211 May 15:17 hello.c
Although the hint has been stored, the core file is still not found. Query the core size for this and modify:
flying-bird@flying-bird:~/examples/cpp/echo$ ulimit-c
0
flying-bird@flying-bird:~/examples/cpp/echo$ Ulimit-c Unlimited
flying-bird@flying-bird:~/examples/cpp/echo$./a.out
Segment Error (core dumps)
flying-bird@flying-bird:~/examples/cpp/echo$ ll
Total dosage 352
drwxrwxr-x 2 flying-bird flying-bird 4096 May 15:21/
drwxrwxr-x 3 flying-bird flying-bird 4096 May 10 15:19. /
-rwxrwxr-x 1 flying-bird flying-bird 9464 May 15:19 a.out*-rw
-------1 Flying-bird flying-bird 335872 May 15:21 core
-rw-rw-r--1 flying-bird flying-bird 211 May 15:17 hello.c
At this point, the core file is generated and can then be used to locate the problem:
flying-bird@flying-bird:~/examples/cpp/echo$ gdb./a.out core GNU gdb (Ubuntu/linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04 Copyright (C) Free Software Foundation, Inc. License gplv3+: GNU GPL version 3 or later
4. SupplementingNot all exceptions will generate Coredump files, please refer to the Tenth Chapter of UNIX Environment Advanced Programming (2nd) signal.
Sometimes you need to dynamically perform ulimit functions in your code, and you need to invoke the Getrlimit () and Setrlimit () functions. Please refer to "Advanced Linux Programming" section 8.5 specifically.