Debug Segmentation Segment Error "Go" with GDB

Source: Internet
Author: User

This article was reproduced from: http://blog.csdn.net/learnhard/article/details/4879834

Debugging Linux programs, the appearance of segmentation fault is the most depressing thing, the program code is very large, it may take a lot of time to find the cause of the error.

Here's a way to help you debug segmentation fault, which could quickly help you find the line of code that went wrong.

This approach requires the core dump mechanism provided by Linux: crashes occur when a memory operation error occurs in the program and produces a core file (the kernel file). Using GDB, you can analyze the resulting core files to find out when the program crashed and what the program did before it crashed.

First of all, your segmentation fault error must be reproducible (nonsense ...). )。

Then, follow the steps below to see how to do this:

(1) Whether you are compiling with makefile or manually typing commands directly at the command line, you should add the-G option.

(2) In general, by default, when a program crashes, the core file is not generated (many Linux distributions prohibit the generation of core files by default). Therefore, you must modify this default option to execute at the command line:

Ulimit-c Unlimited

Indicates that the size of the generated core file is not limited.

(3) Run your program, no matter what method, make it reproduce segmentation fault error.

(4) At this point, you will find in your program in the same directory, generated a file named core.*** file, that is, the core file. For example, a file such as "core.15667".

(5) Debug it with GDB. Assuming that your executable program is named Test, execute at the command line:

gdb Test core.15667

A bunch of messages may then be displayed:

GNU gdb Fedora (6.8-27.EL5)

Copyright (C) Free Software Foundation, Inc.

License gplv3+: GNU GPL version 3 or later

This was free software:you was free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law. Type "Show copying"

and "Show warranty" for details.

This GDB is configured as "I386-redhat-linux-gnu" ...

Warning:can ' t read pathname for load Map:input/output error.

..................... (There is a lot of content in the middle, omitted here) ..... ..... ...........

Loaded symbols for/usr/lib/libgpg-error.so.0

Core is generated by './test '.

Program terminated with signal one, segmentation fault.

[New Process 15668]

#0 0x0804c760 in Thread _handler () at test.cpp:707

707 cdev* Cur_dev = *it_d;

then we enter and execute the command bt :

(GDB) bt

You will get a message similar to the following:

#0 0x0804c760 in Thread _handler () at test.cpp:707

#1 0x006b149b in Start_thread () from/lib/libpthread.so.0

#2 0x0060842e in Clone () from/lib/libc.so.6

So, we can see at a glance: The program is in the first 707 The problem occurs when a row uses a pointer.

How's it going, easy?

Debug Segmentation Segment Error "Go" with GDB

Related Article

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.