Use gdb to quickly locate the "segment error" and gdb to locate the segment Error

Source: Internet
Author: User

Use gdb to quickly locate the "segment error" and gdb to locate the segment Error

Sometimes, when we operate on an invalid memory in a section of C/C ++ code, the "Segmentation fault (core dumped) "-- segment error.



I think many people will often encounter this problem. This problem is very speechless, just prompts "segment error", and then there is nothing. If we blindly look at the code to find it too painful, because we all believe that the code we write is okay, the reality is the reality. Next, we may locate the wrong segment through printing, which will cause a problem. If the code is large, we need to print a lot of information to find the "segment error" location (this method is too stupid), and sometimes the "segment error" cannot be found simply by printing.Next, we will introduce how to quickly locate the "segment error" Through gdb..


GDB is a powerful UNIX program debugging tool released by the GNU open-source organization. Maybe you prefer the GUI, such as VC, BCB, and other IDE debugging methods. But if you are running software on a UNIX platform, you will find that the GDB debugging tool has more powerful functions than the visual debugger of VC and BCB. This is the so-called "have an inch, have a small size and have a short size.


First, make sure that the gdb debugging tool is installed.Which gdb"View:



Compile C/C ++ code, and add-GParameter, indicating that the standard debugging information is added to the executable file.



Then,./"Run the executable program. If a" segment error "occurs, a core file (such as redhat) will be generated under the same directory of the program under normal conditions. However, some environments do not generate core files (such as ubuntu). The generation of core files is related to the environment settings of the current system. When a program crashes, the stored images of the process are copied to the core file in the current working directory of the process. The core file is only a memory image (with debugging information added) and is mainly used for debugging.


Test in the redhat 6 (64-bit) Environment:

The C ++ test code is as follows:

#include <iostream>using namespace std;void test(){int *i = NULL;*i = 250;}int main(int argc, char*argv[])  {    test();return 0;  }

1) Compile (add -G), Execute the program to produce Core .*File:



2) Start gdb and useCore .*File to quickly locate the "segment error" location.

# Gdb-q program name [corefile]. In this example:Gdb-q a. out core.6922, EnterWhereView the "segment error" location.




Test in ubuntu 14.04 (64-bit) Environment:

The C test code is as follows:

#include <stdio.h>#include <string.h>void test(){char *p = NULL;strcpy(p, "123");}int main(int argc, char*argv[])  {    printf("111111111111\n");test();printf("2222222222222\n");return 0;  }

1) Compile (add -G), Execute the program, and find that the directory at the same level is not produced CoreFile:


2) the generation of the core file is related to the environment settings of the current system. You can use the following statement settings to generate the core file.CoreFile:Ulimit-c unlimited. The core file is generally generated in the same path as the running program, and the file name in ubuntu is generally core.



3) Use gdb to debug the core file: gdb a. out core



4) on the gdb character interface,Where"To view more details.



References:

Http://jingyan.baidu.com/article/

Http://blog.chinaunix.net/

Copyright Disclaimer: This blog post is mostly prepared by myself or collected on the Internet. For more information, see the source !!

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.