Segment errors and how to debug

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.