Example of GDB debugging core file (how to locate segment fault)

Source: Internet
Author: User

Core dump is also called core dump. When an exception occurs during the program running and the program exits abnormally, the operating system stores the current memory status of the program in a core file, which is called core dump. (in Linux, if the memory is out of bounds, it will receive the SIGSEGV signal and then core dump)

In the process of running the program, sometimes we encounter a segment fault (segment error) error. This seems difficult because there is no stack or trace information output. Errors of this type are often related to pointer operations. It is often possible to locate in this way.

1. possible causes of segment fault and core dump

1. Memory Access out of bounds

A) array access out of bounds due to incorrect subscript

B) when searching for a string, the string Terminator is used to determine whether the string ends. However, the string does not use the terminator normally.

C) use strcpy, strcat, sprintf, strcmp, strcasecmp, and other string operation functions to read/write the target string. Functions such as strncpy, strlcpy, strncat, strlcat, snprintf, strncmp, and strncasecmp should be used to prevent read/write from being out of bounds.

2 multi-threaded programs use functions that are not thread-safe.

3 multi-threaded read/write data is not locked. For global data that will be accessed by multiple threads at the same time, pay attention to lock protection, otherwise it will easily cause core dump

4 Invalid Pointer

A) Use a null pointer.

B) Use Pointer conversion at will. A pointer pointing to a memory segment, unless it is determined that the memory is originally allocated to a structure or type, or an array of this structure or type, otherwise, instead of converting it to a pointer of this structure or type, you should copy this memory to a structure or type and then access this structure or type. This is because if the starting address of this memory segment is not aligned according to this structure or type, it is easy to access it because of Bus Error and core dump.

5. Stack Overflow. do not use large local variables (because all local variables are allocated on the stack). This can easily cause stack overflow, damage the stack and heap structure of the system, and cause inexplicable errors.

2. Configure the operating system to generate core files

First, run the ulimit command to check whether the system supports the dump core function. You can use ulimit-C or ulimit-a to view the Core File Size configuration. If it is 0, dump core is disabled. You can enable it through ulimit-C unlimited. If a segment error occurs but no core dump exists, the system prohibits the generation of core files.

Solution:
$Ulimit-C Unlimited(Only valid for the current Shell Process)
Or in~ /. BashrcTo add:Ulimit-C Unlimited(Once and for all)

# Ulimit-C

0

 

$ Ulimit-

Core File size (blocks,-C) 0

Data seg size (Kbytes,-d) Unlimited

File size (blocks,-f) Unlimited

3. Use GDB to view core files

After a core dump occurs, use GDB to view the content of the core file to locate the line that causes the core dump in the file.

GDB [exec file] [core file]

For example, GDB./test. Core.

 

Four examples

 

1. NULL pointer

Example:

# Include <stdio. h>

Int main (void)

{

Printf ("Hello world! Dump core for set value to NULL pointer/N ");

* (Char *) 0 = 0;

Return 0;

}

# Gcc-g test. C-o Test

#./Test

Hello world! Dump core for set value to NULL pointer

Segmentation fault

/× Get segmentation fault, but there is no core dump. The reason is that the system configure core file size to zero ×/

# Ls

Test test. c

/* Set core file size to unlimited */

# Ulimit-C Unlimited

#./Test

Hello world! Dump core for set value to NULL pointer

Segmentation fault (core dumped)

/* Get core dump after change core file size .*/

# Ls

Core.5581 test. c

/* GDB to debug core dump */

# GDB test core.5581
Gnu gdb Red Hat Linux (6.3.0.0-1.132.el4rh)
Copyright 2004 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 "x86_64-redhat-linux-gnu"... using host libthread_db library "/lib64/tls/libthread_db.so.1 ".

Core was generated by './test '.
Program terminated with signal 11, segmentation fault.
Reading symbols from/lib64/tls/libc. so.6...... done.
Loaded symbols for/lib64/tls/libc. so.6
Reading symbols from/lib64/ld-linux-x86-64.so.2... done.
Loaded symbols for/lib64/ld-linux-x86-64.so.2
#0 0x000000000040048b in main () at test. C: 6

Warning: source file is more recent than executable.

6 * (char *) 0 = 0;
(GDB) BT
#0 0x000000000040048b in main () at test. C: 6
 

2. Stack Overflow.

For programs with Stack Overflow, see: a small program with a test stack size.

Http://blog.163.com/huang_bp/blog/static/12311983720099150746901/edit/

 

 

# Gcc-g test. C-o Test-lpthread

# Ls

Test test. c

#./Test

...

Segmentation fault (core dumped)

# Ls

Core.5616 test. c

# GDB test core.5616
Gnu gdb Red Hat Linux (6.3.0.0-1.132.el4rh)
Copyright 2004 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 "x86_64-redhat-linux-gnu"... using host libthread_db library "/lib64/tls/libthread_db.so.1 ".

Core was generated by './test '.
Program terminated with signal 11, segmentation fault.
Reading symbols from/lib64/tls/libpthread. so.0.. Done.
Loaded symbols for/lib64/tls/libpthread. so.0
Reading symbols from/lib64/tls/libc. so.6...... done.
Loaded symbols for/lib64/tls/libc. so.6
Reading symbols from/lib64/ld-linux-x86-64.so.2... done.
Loaded symbols for/lib64/ld-linux-x86-64.so.2
#0 0x0000002a957c051e in vfprintf () from/lib64/tls/libc. so.6
(GDB) List
13
14 buffer [0] = I;
15 test (s );
16}
17
18 int main ()
19 {
20 pthread_t P;
21
22 pthread_create (& P, null, & test, null );

The segment fault of stack overflow is not easily located. You need to analyze the code to determine the cause.

========================================================== ====================================== Recommendation reference:Cause and debugging method of segment errors in Linux

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.