This situation is generally caused by array cross-border access, null pointer or wild pointer reading and writing. If the program is small, it is easier to handle, and it can be solved by carefully checking the source code. However, for a program with a large amount of code, which includes n multi-function calls and n multi-array pointer accesses, it is not easy to locate the problem at this time (at this time, Niu can still quickly locate the problem by typing printf and method of bisection in the appropriate position: P). If you are lazy, let's start with GDB. What is a core dump file? Occasionally, I can hear a programmer complain, "Shit, it's out of the core again!" . Simply put, a core dump refers to an action performed by the operating system. When a process crashes unexpectedly for some reason, the operating system will dump the memory information of the process to the disk at that time. The generated file is a core file, usually named in the form of core.xxx. How to generate a doredump usually occurs when a process receives a certain signal. Now there are about 60 signals on Linux, which can be listed with the kill -l command. sagi @ sagi-laptop:~ $ kill-l 1)SIGHUP 2)SIGINT 3)SIGQUIT 4)SIGILL 5)SIGTRAP 6)siga BRT 7)SIG bus 8)SIGFPE 9)SIG kill 1 1 / kloc-0/)SIGSEGV 12)SIG usr 2 13)SIG pipe 14) SIGRTMIN+7 42)SIGRTMIN+8 43)SIGRTMIN+9 44)SIGRTMIN+ 10 45)SIGRTMIN+ / Kloc-0/146) sigrtmin+1247) sigrtmin+1348) sigrtmin+1449) sigrtmin+1550) sigrtmax-655. The default signal processing is as follows: 3) sigquit 4) sigill 6) sigabrt8) sigfpe1) sigsegv 7) sigbus 31) sigsys 5) sigtrap 24) sigxcpu 25) sigxfsz 29) sigiot We see sigsegv in it. In addition, although this is the default, you can also write your own signal processing function to change the default behavior. For more information about signal correlation, please refer to link 33. The above content is only a necessary condition to produce coredump, but not a sufficient condition. The generation of the core file also depends on the shell where the program runs, which can be viewed by ulimit -a command. The output content is roughly as follows: sagi @ sagi-laptop: ~ $ ulimit-a core file size (number of blocks,-c) 0daseg size (kilobytes, -d) unlimited scheduling priority (-e) 20 file size (number of blocks, -f) unlimited hang signal (-i) 16382 maximum. -l) 64 Maximum memory size (kilobytes, -m) Unlimited file opening (-n) 1024 Pipeline size (5 12 bytes, -p) 8 -t) Unlimited maximum user process (-u) Unlimited virtual memory (kbytes, -v) Unlimited file lock. My output here is 0, which means that the core file will not be saved. Even if it is generated, it cannot be saved = =! To change this setting, you can use ulimit -c unlimited. Ok, now everything is ready, except a program that can generate Core. C programmers are too easy to introduce. # contains; # contains; int crash() { char *xxx = "crash! !" ; XXX[ 1]= ' D '; //Write the read-only storage area! return 2; } int foo(){ return crash(); } int main(){ return foo(); } there is one thing to pay attention to when debugging the compilation of the above programs. You need to take the parameter -g, so that the generated executable program will bring enough debugging information. After compiling and running, you should be able to see the long-awaited words like "segment fault (core)" or "segment error (core)". Check whether there is a core or core.xxx file in the current directory. To present the classic debugger GDB under linux, first load the program with the core file: gdb exefile core. It should be noted that this core file must be generated by EXFILE, otherwise the symbol table does not match. After loading, it looks like this: sagi @ sagi-laptop: ~ $ gdb core dump core generated by. /coredump。 Program terminated, signal 1 1, subsection failure. # 0 0x 080483 a7 in crash()at core dump . c:8 8 XXX[ 1]= ' D '; (gdb) We can see that we can directly locate outside the core and write a read-only memory area on line 8, which will trigger a segment fault signal. There is a trick when loading the kernel. If you don't know in advance which program generated this core file, you can find a replacement, such as /usr/bin/w, which is a good choice. For example, if we use this method to load the core generated above, gdb will have similar output: sagi @ sagi-laptop: ~ $ gdb/usr/bin/wcore is generated by. /coredump。 Program terminated, signal 1 1, subsection failure. # 0 0x008483 A7 in? () (gdb) You can see that GDB prompts you which program generated this kernel. The procedures of GDB common operations above are relatively simple, and problems can be found directly without additional operations. But in reality, this is not the case, and it is often necessary to track in one step and set breakpoints to successfully locate the problem. Some common operations of GDB are listed below. Startup program: running
Set breakpoint: b line number | function name
Delete breakpoint: delete breakpoint number.
Disable breakpoint: disable breakpoint number.
Enable breakpoint: Enable breakpoint number.
One-step tracking: next can also be abbreviated as N.
Single-step tracking: step can also be abbreviated as s
Print variable: print variable name.
Set variable: set variable = value.
View variable type: ptype var
Sequential execution to end: continued
Sequential execution to a certain line: util lineno print stack information: bt