Dummy_function (void) { char * ptr=0x00; *ptr=0x00;} int Main () { dummy_function (); return 0 ;}
The above code bug should be clear, as a skilled C + + programmer, because it tries to manipulate an area of memory where the address is 0, which is usually an inaccessible area, and of course it will go wrong.
Method 1: Use GDB to step through a segment error This method is also well known and widely used by the public, first we need an executable program with debug information, so we add "-g-rdynamic" The parameters are compiled and then called gdb Debug to run the newly compiled program.
This version of GDB does not prompt for errors, the previous version hints except for errors.
Not only does it suggest that the first few lines have errors, but it also indicates that the process ended because the SIGSEGV signal was received. Through further access to the documentation (man 7 signal),
We know that the SIGSEGV default handler action is to print "segment error" error message, and produce a core file, thus we have a method two.
Method 2: Analyze the core file
But it's weird. The core file is not generated on my system and the Linux system disables the creation of the core file by default, and we tested it with the following command.
We will limit the size of the core file to 1000, this seems to be set every time or not to lose, estimated to modify the file, regardless of, know on the line.
The core file was finally generated after our setup above.
Wow, good, or a step to the wrong location, admire the Linux system of this kind of design,
Method 3: Start debugging When a segment error occurs (tried unsuccessfully)
1#include <signal.h>2#include <stdlib.h>3 voidDumpintSigno)4 { 5 //omitted here .... System"gdb"); 6 7 } 8Dummy_function (void) 9 { TenUnsignedChar* ptr=0x00; One*ptr=0x00; ASleepTen); - } - intMain () the { -Signal (sigsegv,&dump); -dummy_function (); - return 0; +The idea is this, then enter the "BT" after entering the debug interface"
Method 4: Use BackTrace and objdump for analysis.
Segment errors and how to debug