Segmentation fault (core dumped) solution

Source: Internet
Author: User

Core dump is also called the central Dump, when the program runs abnormal, the program abnormal exit, the program's current memory state of the system stored in a core file, called Core dump. (In Linux If the memory is out of bounds, you will receive a SIGSEGV signal, and then core dump)

In the process of running the program, sometimes we will encounter segment fault (segment error) Such a mistake. This looks more difficult because there is no stack, trace information output. This type of error is often associated with pointer manipulation. Can often be positioned in such a way.

a possible cause of core dump caused by Segment fault

1. Memory Access out of bounds

A) array access is out of bounds due to the use of incorrect subscript

b When searching a string, the string terminator is used to determine whether the string ends, but the string does not use the end character properly

c) Use string manipulation functions such as strcpy, Strcat, sprintf, strcmp, strcasecmp to read/write the target string to the burst. Functions such as strncpy, strlcpy, Strncat, Strlcat, snprintf, strncmp, strncasecmp, etc. should be used to prevent reading and writing from crossing boundaries.

The 2 multithreaded application uses a thread-unsafe function.

3 read-write data for multiple threads is not protected by lock. For global data that will be accessed by multiple threads at the same time, you should pay attention to lock protection, otherwise it can easily cause core dump

4 illegal pointers

A) using null pointers

b free use of pointer conversions. A pointer to a piece of memory, you should not convert this memory into a pointer to this structure or type unless you determine that it was originally assigned to a struct or type, or an array of that structure or type, and you should copy that memory into one of these structures or types, and then access that structure or type. This is because if the beginning address of this memory is not aligned according to this structure or type, it is easy to access it with the core dump because of bus error.

5 Stack overflow. Do not use large local variables (because local variables are all allocated on the stack), which can easily cause stack overflow, damage the system stack and heap structure, resulting in inexplicable errors.

two configuring the operating system to produce core files

First, check with the Ulimit command to see if the system is configured to support the dump core feature. With Ulimit-c or ulimit-a, you can view the configuration of the core file size, or 0, which means the system shuts down the dump core. Can be opened by Ulimit-c Unlimited. If a segment error occurs, but there is no core dump, it is because the system prohibits core file generation.

Workaround:
$ulimit-c Unlimited (valid only for current shell processes)
Or at the end of ~/.BASHRC add: ulimit-c unlimited (once and for all)

# ulimit-c

0

$ ulimit-a

Core file size (blocks,-c) 0

Data seg Size (Kbytes,-D) Unlimited

File size (blocks,-f) Unlimited

three use GDB to view core files

After core dump occurs, use GDB to view the contents of the core file to locate the row in the file that is causing the core dump.

GDB [exec file] [core file]

such as: GDB./test Test.core

Four examples

1. Null pointer

Sample Example:

#include

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

/xget segmentation Fault, but there is no core dump. The reason is this system configure core file size to zerox/

# ls

Test test.c

# ulimit-c Unlimited

#./test

Hello world! Dump core for set value to NULL pointer

Segmentation fault (core dumped)

# ls

core.5581 Test test.c

# GDB Test core.5581
GNU gdb Red Hat Linux (6.3.0.0-1.132.EL4RH)
Copyright 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 "X86_64-redhat-linux-gnu" ... The Using host libthread_db the Library "/lib64/tls/libthread_db.so.1".

Core is generated by './test '.
Program terminated with signal, 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 () Test.c:6

Warning:source file is more recent than executable.

6 * (char *) 0 = 0;
(GDB) bt
#0 0x000000000040048b in Main () Test.c:6

2. Stack overflow.

For stack overflow programs, see: A small program that tests the size of a stack

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 test.c

# GDB Test core.5616
GNU gdb Red Hat Linux (6.3.0.0-1.132.EL4RH)
Copyright 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 "X86_64-redhat-linux-gnu" ... The Using host libthread_db the Library "/lib64/tls/libthread_db.so.1".

Core is generated by './test '.
Program terminated with signal, 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
Buffer[0]=i;
Test (s);
16}
17
int main ()
19 {
pthread_t p;
21st
Pthread_create (&p, NULL, &test, NULL);

For stack overflow segment fault without first locating convenience, it is necessary to analyze the code to determine the reason.

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.