Segmentation fault (core dumped)

Source: Internet
Author: User

From: http://blog.chinaunix.net/uid-26748613-id-3182113.html

Reference: http://manpages.ubuntu.com/manpages/lucid/man5/core.5.html

Some programs can be compiled, but segment fault (segment error) may occur during runtime ). This is usually caused by pointer errors. But this is not like a compilation error, but will prompt a line of the file, but there is no information. One way is to use the step of GDB to find it step by step. However, it is hard to imagine that tens of thousands of lines of code are required. We still have a better solution, which is core file.

If you want the system to generate a core file when an error occurs due to signal interruption, you need to set it in shell as follows:

# Set the core size to unlimited ulimit-C Unlimited

# Set the file size to unlimited ulimit Unlimited

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

GDB [exec file] [core file]

For example, after GDB./test. Core enters GDB, use the BT command to view backtrace to check where the program is running and locate the File> line of core dump.

In addition, if you run a lot of applications on your machine and you do not know which application the core is generated, you can run the following command to check the core: file Core

 

Several questions:

1. What is core:

Before using semiconductors as memory materials, humans use coils as memory materials (inventor Wang An), and coils are called core, and the memory made by coils is called core memory. Today, the semiconductor industry is booming and no one has used core memory. However, in many cases, memory is still called core.

2. What is core dump:

When developing (or using) a program, what we fear most is that the program can be ignored. Although the system is okay, we may encounter the same problem next time. Then, the operating system will dump out the memory content when the program is down (usually written in a file called core). Let's use it as a reference or debugger. This action is called core dump.

3. What files will be generated when core dump:

Core Dump generates files such as core. Process numbers.

4. Why sometimes the program is down, but the core file is not generated.

In Linux, some settings indicate resources available to the shell and to processes. Available

# Ulimit-. (Ulimit is Bash built-in command)

It can be seen from this that if-C is displayed: core file size. If the value is 0, the core file cannot be generated. Therefore, you can use # ulimit-C 1024 or # ulimit-C unlimited to enable core files. If the program generates a core file when an error occurs, segmentation fault (core dumped) is displayed ).

5. Core Dump's core dump file directory and naming rules:

/Proc/sys/kernel/core_uses_pid can be used to control whether the PID is added as an extension to the file name of the generated core file. If it is added, the file content is 1; otherwise, it is 0.

Run the following command to modify the file:

Echo "1">/proc/sys/kernel/core_uses_pid

6. How to use core files:

In Linux, use:

# GDB-C Core. PID program_name

You can enter the gdb mode.

Enter the WHERE clause to indicate the row in which the data is down, the function in which the data is called, and so on.

(GDB) Where

Or enter BT.

(GDB) BT

7. How to make a normal program down:

# Kill-s SIGSEGV PID

8. Check where the core file is output:

The directory that stores coredump is the current directory of the process, which is generally the directory where the command was issued to start the process. However, if the script is started, the script may modify the current directory, and the current directory of the process will be different from the directory where the script was originally executed. In this case, you can view the target of the "/proc/<process pid>/CWD" symbolic link to determine the real current directory address of the process. You can also view the processes started by the system service in this way.

Proc/sys/kernel/core_pattern can control the core file storage location and file name format.

Run the following command to modify the file:

Echo "/corefile/Core-% E-% P-% t"> core_pattern

You can generate the core file to the/corefile directory, and the generated file name is core-command name-PID-timestamp.

The following is a list of parameters:

% P-insert PID into filename add PID

% U-insert current uid into filename add current uid

% G-insert current GID into filename add current GID

% S-insert signal that caused the coredump into the filename added to generate core Signal

% T-insert UNIX time that the coredump occurred into filename UNIX time when the core file is generated

% H-insert hostname where the coredump happened into filename Add the Host Name

% E-insert coredumping executable name into filename add command name

 

In Linux, pay attention to the following issues to ensure coredump is generated when the program crashes:

1. Ensure that the directory where coredump is stored exists and the process has write permission on the directory. The directory that stores coredump is the current directory of the process, which is generally the directory where the command was issued to start the process. However, if the script is started, the script may modify the current directory, and the current directory of the process will be different from the directory where the script was originally executed. In this case, you can view the target of the "/proc/process pid>/CWD" symbolic link to determine the real current directory address of the process. You can also view the processes started by the system service in this way.

2. If the program calls seteuid ()/setegid () to change the valid user or group of the process, the system will not generate coredump for these processes by default. Many service programs call seteuid (), such as MySQL. No matter which user you use to run mysqld_safe to start MySQL, the effective user of mysqld is always the msyql user. If you run a program with user A, but the user who sees the program in PS is B, then these processes call seteuid. To allow these processes to generate Core
Dump,/proc/sys/fs

The content of the/suid_dumpable file is changed to 1 (usually 0 by default ).

3. This is generally known as setting a large enough core file size limit. The core file size generated when the program crashes is the memory size occupied by the program running. However, when a program crashes, the behavior cannot be estimated as usual. For example, errors such as buffer overflow may cause the stack to be destroyed. Therefore, the value of a variable is often changed to a mess, then the program uses this size to apply for memory, which may cause the program to occupy more memory than usual. Therefore, no matter how little memory is occupied during normal running of the program, make sure to generate the core file or set the size limit to unlimited.

4. Will a core be generated when an exception exits? Is there any abnormal exit without generating a core?

If the program Exits normally, the program exits due to a signal. Some signals can indeed cause the program to exit but do not generate a core.

Sighup

SIGINT terminates process interruption

Sigquit creates a core file to terminate the process and generates a Core File

Invalid Command for creating core file in sigill

Sigtrap creates core file tracking self-traps

Sigbus Core File bus creation error

Invalid SIGSEGV Core File segment creation error

An error occurred while creating the core file floating point in sigfpe.

Sigiot creates Core File Execution I/O self-traps

Sigkill terminate the process and kill the process

Sigpipe terminates a process and writes data to an unread process pipeline.

When sigalarm terminates the process Timer

Sigterm termination process software termination signal

Sigstop stop signal of non-terminal stop Process

Sigtstp stop signal from the process Terminal

Sigcont ignores the signal and continues a stopped process.

Sigurg ignores signal I/O emergency signals

Sigio can ignore the I/O on the signal Descriptor

Sigchld ignores the signal and notifies the parent process when the child process stops or exits.

Sigttou stops the process Background Process Writing Terminal

Sigttin stops the process Background process read Terminal

Sigxgpu termination process CPU timeout

The length of the sigxfsz termination process file is too long.

Sigwinch ignores changing signal window size

Sigprof termination process statistical distribution when timer is used

SIGUSR1 termination process user-defined signal 1

Sigusr2 termination process user-defined Signal 2

Sigvtalrm terminates the virtual timer of the process

Set the handle for all possible signals.

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.