Debugging of Linux C language errors and bugs

Source: Internet
Author: User

I saw it once. Later, when I had a paragraph error, I remembered that this was very useful. If I didn't use his method, It would be disgusting and hard to find it.

The following is the original article, but the source is unknown.

======================================

There is a problem with the replication. The format is incorrect... give the Link first.

Http://blog.sina.com.cn/s/blog_606c49090100eohs.html

Core Documents: http://blog.sina.com.cn/s/blog_489c2413010080ml.html

Introduction to multi-thread programming under the Linux system http://blog.sina.com.cn/s/blog_489c241301008nco.html THIS SHOULD BE IBMArticleI have read it before.

 

1) to access the system data zone, especially to write data to the memory address protected by the system, it is most common to give a pointer

0 address

2) memory out of bounds (array out of bounds, variable types inconsistent, etc.) access to areas not in your memory
Solution

We are writing in C/C ++ProgramMost of the work of memory management needs to be done. In fact, memory management is a tedious task. No matter how clever and experienced you are, it is inevitable that you will make some small errors here. These errors are usually so simple and easy to eliminate. However, manual debugging is often inefficient and annoying, this article will talk about how to quickly locate these "segment errors" statements about memory access out-of-bounds errors.

The following describes several debugging methods for a program with a segment error:

 
Dummy_function (Void)
{
UnsignedChar* PTR =0x00;
* PTR =0x00;
}

IntMain (Void)
{
Dummy_function ();

Return 0;
}
 
 
As a skilled C/C ++ programmerCodeThe bug should be very clear, because it tries to operate on the memory area with the address 0, and this memory area is usually inaccessible restricted area, of course, there will be errors. We tried to compile and run it: The xiaosuo @ gentux test $./A. Out segment error is as expected. It went wrong and exited. 1. step-by-Step Block Error search using GDB: This method is also widely known and widely used. First, we need an executable program with debugging information, therefore, we add the "-g-rdynamic" parameter to compile the program, and then use GDB to debug and run the newly compiled program. The specific steps are as follows:
Xiaosuo @ gentux test $ gcc-g-rdynamic d. c
Xiaosuo @ gentux test $ GDB./. Out
GNU GDB 6.5
Copyright (c) 2006 Free Software Foundation, Inc.
GDB Is Free Software, covered by the GNU General Public License, and you are
Welcome to change it and/or distribute copies of it under certain conditions.
Type " Show copying " To see the conditions.
There Is Absolutely no warranty For GDB. Type " Show warranty " For Details.
This GDB was configured As " I686-pc-linux-gnu " ... Using host libthread_db Library " /Lib/libthread_db.so.1 " .

(GDB) r
Starting program:/home/xiaosuo/test/. Out

Program received signal SIGSEGV, segmentation fault.
Zero X 08048524 In Dummy_function () at D. C: 4
4 * PTR =0x00 ;
(GDB)
 
 
 
Oh ?! It seems that we did not need to debug step by step to find the Error Path line 4th of the D. c file, which is actually so simple. We also found that the process ended with the SIGSEGV signal. After further reading the document (MAN 7 signal), we know that the default handler action of SIGSEGV is to print the error message of "segment error" and generate a core file, therefore, method 2 is generated. 2. Analyze the core file: What is the core file? The default action of certain signals is to cause a process to terminate and produce a core dump file, a disk file containing an image of the process's memory at the time of termination. A list of the signals which cause a process to dump core can be found in signal (7 ). this document is taken from man page (MAN 5 core ). But it's strange that the core file is not found on my system. Later, I recalled that in order to gradually reduce the number of pull files on the system (I am somewhat clean, which is one of the reasons I like Gentoo), and disabled the generation of core files, check that the following is true, limit the size of the system core file to kb, and try again:
Xiaosuo @ gentux test $ ulimit-C
0
Xiaosuo @ gentux test $ ulimit-C1000
Xiaosuo @ gentux test $ ulimit-C
1000
Xiaosuo @ gentux test $./.Out
Segment error (core dumped)
Xiaosuo @ gentux test $ ls
A.OutCore D. c f. c g. C pango. c test_iconv.c test_regex.c
 
 
 
The core file is finally generated. Use GDB to debug it:
Xiaosuo @ gentux test $ GDB./. Out Core
GNU GDB 6.5
Copyright (c) 2006 Free Software Foundation, Inc.
GDB Is Free Software, covered by the GNU General Public License, and you are
Welcome to change it and/or distribute copies of it under certain conditions.
Type " Show copying " To see the conditions.
There Is Absolutely no warranty For GDB. Type " Show warranty " For Details.
This GDB was configuredAs " I686-pc-linux-gnu " ... Using host libthread_db Library " /Lib/libthread_db.so.1 " .


Warning: Can ' T read pathname for load map: input/output error.
Reading symbols From /Lib/libc. So. 6 ... Done.
Loaded symbolsFor /Lib/libc. So. 6
Reading symbols From /Lib/ld-linux.so. 2 ... Done.
Loaded symbols For /Lib/ld-linux.so. 2
Core was generated by './. Out ' .
Program terminated with Signal 11 , Segmentation fault.
#0 Zero X 08048524 In Dummy_function () at D. C: 4
4 * PTR = 0x00 ;
 
 
 
Wow, it's a good experience. I just got to the wrong location and admire the design of Linux/Unix systems. Next, when I used Internet Explorer in windows, sometimes some web pages may encounter "runtime errors ", at this time, if a Windows compiler is installed on your machine, a dialog box will pop up asking you if you want to debug it. If you choose yes, the compiler will be opened, and enter the debugging status to start debugging. How can we achieve this in Linux? My brain is spinning fast. Now, let it call GDB in the handler of SIGSEGV, so the third method is born again: 3. Start debugging when a segment error occurs:
# Include
# Include
# Include
# Include

Void Dump (Int Signo)
{
Char Buf [ 1024 ];
Char CMD [ 1024 ];
File * FH;

Snprintf (BUF, Sizeof (BUF ), " /Proc/% d/cmdline " , Getpid ());
If (! (FH = fopen (BUF, " R " )))
Exit ( 0 );
If (! Fgets (BUF, Sizeof (BUF), FH ))
Exit ( 0 );
Fclose (FH );
If (BUF [strlen (BUF )- 1 ] = ' \ N ' )
Buf [strlen (BUF )- 1 ] = ' \ 0 ' ;
Snprintf (CMD, Sizeof (CMD ), " GDB % S % d " , Buf, getpid ());
System (CMD );

Exit ( 0 );
}

Void
Dummy_function (Void )
{
Unsigned Char * PTR = 0x00 ;
* PTR = 0x00 ;
}

Int
Main ( Void )
{
Signal (SIGSEGV, & dump );
Dummy_function ();

Return 0 ;
}
 
 
 
The compilation and running effect is as follows:
Xiaosuo @ gentux test $ gcc-g-rdynamic F. C
Xiaosuo @ gentux test $./. Out
GNU GDB 6.5
Copyright (c) 2006 Free Software Foundation, Inc.
GDB Is Free Software, covered by the GNU General Public License, and you are
Welcome to change it and/or distribute copies of it under certain conditions.
Type " Show copying " To see the conditions.
There Is Absolutely no warranty For GDB. Type " Show warranty " For Details.
This GDB was configured As " I686-pc-linux-gnu " ... Using host libthread_db Library " /Lib/libthread_db.so.1 " .

Attaching to program:/home/xiaosuo/test/. Out , Process 9563
Reading symbols From /Lib/libc. So. 6 ... Done.
Loaded symbols For /Lib/libc. So. 6
Reading symbols From /Lib/ld-linux.so. 2 ... Done.
Loaded symbolsFor /Lib/ld-linux.so. 2
0xffffe410 In _ Kernel_vsyscall ()
(GDB) BT
# 0 0xffffe410 In _ Kernel_vsyscall ()
# 1 0xb7ee4b53 In Waitpid () From /Lib/libc. So. 6
#2 0xb7e925c9 In Strtold_l () From /Lib/libc. So. 6
# 3 Zero X 08048830 In Dump (signo = 11 ) At F. C: 22
# 4
# 5 0x0804884c In Dummy_function () at F. C: 31
# 6 Zero X 08048886 In Main () at F. C: 38
 
 
 
How is it? Is it still cool? The above methods are implemented on the premise that GDB is available on the system. If not, what should I do? Actually, glibc provides us with such function clusters that can dump stack content. For details, see/usr/include/execinfo. H (no man page is provided for these functions, so we can't find them). You can also learn from the GNU manual. 4. Use backtrace and objdump for analysis: The rewritten code is as follows:
 
 
The compilation result is as follows: xiaosuo @ gentux test $ gcc-g-rdynamic G. cxiaosuo @ gentux test $. /. outobtained 5 stack frames .. /. out (dump + 0x19) [0x80486c2] [0xffffe420]. /. out (main + 0x35) [0x8000006f]/lib/libc. so.6 (_ libc_start_main + 0xe6) [0xb7e02866]. /. out [0x8048601] You may be a little disappointed this time. It seems that you have not provided enough information to indicate errors. Don't worry. Let's first look at what can be analyzed. Use the objdump disassembly program, find the Code Location corresponding to address 0x803856f: xiaosuo @ gentux test $ objdump-d. out 8048765: E8 02 Fe FF call 804856c 8 0100006a: E8 25 FF call 8048694 803666f: B8 00 00 00 00 mov $0x0, % eax8048774: C9 leave we still find the function (dummy_function) the information is not complete, but it is always better! Postscript: This article provides several methods for analyzing "segment errors". Do not think this is the same as the four methods of writing "back" by Mr. Kong Yiji, because each method has its own applicability and applicable environment, please use it as appropriate or follow the doctor's advice.

 

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.