Linux/C + + program debugging

Source: Internet
Author: User
Tags linux

The cause and debugging method of segment error in Linux

Original address: http://www.upsdn.net/html/2006-11/775.html

Reference Address: http://www.cnblogs.com/khler/archive/2010/09/16/1828349.html

In short, generating a segment error is access to the wrong memory segment, generally you do not have permissions, or there is no corresponding physical memory, especially the access to 0 address.

In general, a segment error means that the memory being accessed exceeds the memory space given to the program by the system. Usually this value is saved by GDTR, a 48-bit register, where 32 bits are saved by the GDT table it points to, and the last 13 bits hold the subscript corresponding to the GDT. The last 3 bits include whether the program is in memory and the program's level of operation on the CPU, pointing to the GDT by a 64-bit-per-unit table, This table contains information about the code snippet that the program runs and the starting address of the data segment, as well as the corresponding segment and page Exchange, the program run level, and the memory granularity. Once a program has a cross-border access, the CPU will produce the corresponding exception protection, so segmentation fault appeared.

In programming, the following types of practices can easily lead to segment errors, basically by using pointers incorrectly

1 access to the system data area, especially to the system protected memory address write data

The most common is to give a pointer to a 0 address

2 Memory out of bounds (array out of bounds, inconsistent variable types, etc.) access to areas that are not part of your memory

Solving method

When we write programs in C/s + + language, most of the work in memory management needs to be done. In fact, memory management is a relatively cumbersome work, no matter how smart you are, experience rich, will inevitably make small mistakes here, and often these errors are so simple and easy to eliminate. But manual "Bug-Removing" (debug) is often inefficient and annoying, and this article talks about how to quickly locate these "segment errors" statements about "segment error", a memory access error that is out of bounds.

Several debugging methods are described below for a program that has a section error:

Dummy_function (void)
{
        unsigned char *ptr = 0x00;
        *ptr = 0x00;
}
    
int main (void)
{
        dummy_function ();
    
        return 0;
}

The bug in the code above should be very clear as a skilled C + + programmer, because it tries to manipulate the memory area with address 0, which is usually an inaccessible restricted area and, of course, makes a mistake. We try to compile it to run it:

Xiaosuo@gentux test $./a.out

Segment Error

As expected, it made a mistake and withdrew.

1. Use GDB to step through a segment error:

This method is well-known and widely used by the public, first of all, we need a executable program with debugging information, so we add "-g-rdynamic" parameters to compile, and then use GDB to debug the new compiled program, the following steps:

Xiaosuo@gentux Test $ gcc-g-rdynamic D.C

Xiaosuo@gentux test $ gdb./a.out

GNU GDB 6.5

Copyright (C) 2006 Free Software Foundation, Inc.

The GDB is free software, covered by the GNU general public License, and your are

Welcome to change it and/or distribute copies of it under certain conditions.

Type ' show copying ' to the conditions.

There is absolutely no warranty for GDB. Type ' show warranty ' for details.

This is GDB was configured as "I686-pc-linux-gnu" ... The Using host libthread_db the Library "/lib/libthread_db.so.1".

(GDB) R

Starting program:/home/xiaosuo/test/a.out

Program received signal SIGSEGV, segmentation fault.

0x08048524 in Dummy_function () at D.c:4

4 *ptr = 0x00;

(GDB)

Oh?! It's like we don't have to step through it. We found the wrong location. The 4th line of the D.C file is actually so simple.

From here we also found that the process was terminated by the SIGSEGV signal received. Through a further lookup document (man 7 signal), we know that the SIGSEGV default handler action is to print "segment error" error messages and produce core files, which leads us to a method of two.

More Wonderful content: http://www.bianceng.cn/Programming/cplus/

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.