Core Dump [13:38:30 | Author: yuyun]
In Windows, we are used to debugging dump files with tools such as windbg, so as to analyze and eliminate program running errors. In Linux, we can also do similar work-core dump.
Let's first look at the relevant settings.
$ ulimit -acore file size (blocks, -c) 0data seg size (kbytes, -d) unlimitedscheduling priority (-e) 20file size (blocks, -f) unlimitedpending signals (-i) 16382max locked memory (kbytes, -l) 64max memory size (kbytes, -m) unlimitedopen files (-n) 1024pipe size (512 bytes, -p) 8POSIX message queues (bytes, -q) 819200real-time priority (-r) 0stack size (kbytes, -s) 8192cpu time (seconds, -t) unlimitedmax user processes (-u) unlimitedvirtual memory (kbytes, -v) unlimitedfile locks (-x) unlimited
"Core File size (blocks,-C) 0" means that the core dump file will not be generated when the program crashes. We need to modify the settings. If you are as reluctant to modify the configuration file as you are, enter the following command.
$ Sudo sh-c "ulimit-C unlimited;./test" # test is the executable file name.
Wait ...... We should first prepare a test target.
#include <stdio.h>#include <stdlib.h>void test(){ char* s = "abc"; *s = 'x';}int main(int argc, char** argv){ test(); return (EXIT_SUCCESS);}
Obviously, we have written something in test () that should not be written, which will undoubtedly be very serious. After an executable file is generated, run the preceding command.
$ sudo sh -c "ulimit -c unlimited; ./test"Segmentation fault (core dumped)
$ ls -ltotal 96-rw------- 1 root root 167936 2010-01-06 13:30 core-rwxr-xr-x 1 yuhen yuhen 9166 2010-01-06 13:16 test
This core file is dumped by the system, and our analysis goal is it.
$ sudo gdb test coreGNU gdb (GDB) 7.0-ubuntuCopyright (C) 2009 Free Software Foundation, Inc.Reading symbols from .../dist/Debug/test...done.warning: Can't read pathname for load map: Input/output error.Reading symbols from /lib/tls/i686/cmov/libpthread.so.0... ...done.(no debugging symbols found)...done.Loaded symbols for /lib/tls/i686/cmov/libpthread.so.0Reading symbols from /lib/tls/i686/cmov/libc.so.6... ...done.(no debugging symbols found)...done.Loaded symbols for /lib/tls/i686/cmov/libc.so.6Reading symbols from /lib/ld-linux.so.2... ...done.(no debugging symbols found)...done.Loaded symbols for /lib/ld-linux.so.2Core was generated by `./test'.Program terminated with signal 11, Segmentation fault.#0 0x080483f4 in test () at main.c:16warning: Source file is more recent than executable.16 *s = 'x';
The last few lines of prompts tell us the cause of the error and the code location. Next, how to debug it is the gdb technique. You can enter "where" to see the call stack.
(gdb) where#0 0x080483f4 in test () at main.c:16#1 0x08048401 in main (argc=1, argv=0xbfd53e44) at main.c:22
(gdb) p s$1 = 0x80484d0 "abc"
(gdb) info filesSymbols from ".../dist/Debug/test".Local core dump file:Local exec file: `.../dist/Debug/test', file type elf32-i386. Entry point: 0x8048330 0x08048134 - 0x08048147 is .interp ... ... 0x08048330 - 0x080484ac is .text 0x080484ac - 0x080484c8 is .fini 0x080484c8 - 0x080484d4 is .rodata
Obviously, "ABC" belongs to. rodata. It is strictly prohibited to play.
----------------
Appendix: If you debug the release (-O2) version and delete the symbol table (strip), you should compile the code honestly. It can be seen that the trial run with the debug version is very important !!!