A few simple questions about segmentation fault (core dumped)

Source: Internet
Author: User
Some programs can be compiled, but segment fault (segment errors) occur at run time. This is usually caused by a pointer error. This does not prompt the file line as a compilation error, but does not have any information. One way to do this is to use GDB's step to find it step-by-step. But to step a tens of thousands of lines of code is hard to imagine. We have a better idea, and this is the core file.

If you want the system to produce a core file in the event of a signal interruption, we need to set the following in the shell:

#设置core大小为无限 Ulimit-c Unlimited

#设置文件大小为无限 Ulimit Unlimited

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 After entering GDB, use the BT command to view BackTrace to check where the program runs to locate the file-> line of core dump.

Also note that if you run a lot of applications on your machine, the core you generate does not know which application is generated, you can view it with the following command: File core

A few questions:

1. What is core:

Before using semiconductors as memory materials, humans use coils as the material for memory (the inventor is Wang), the coils are called core, and the memory made of coils is called core memory. Now that the semiconductor industry is developing, no one has been using the core memory, but in many cases people have called the memory core.

2. What is core Dump:

When we develop (or use) a program, the most fear is that the program is not at all to be lost. Although the system is fine, we may still encounter the same problem next time. Then the operating system will be the program when the memory content dump out (now is usually written in a call core file inside), let us or debugger as a reference. This action is called core dump.

3. What file will be generated when Core dump:

Core dump, a file such as the core. Process number is generated.

4. Why sometimes the program down, but did not generate core files.

Linux, there are some settings that mark the available to the shell and to processes. can use

#ulimit-A to see these settings. (Ulimit is bash built-in Command)

As you can see from here, if-C is displayed: Core file size. If this value is 0, the core file cannot be generated. So you can use: #ulimit-C 1024 or #ulimit-C unlimited to enable core files. If a core file is generated when a program fails, the segmentation fault (core dumped) is displayed.

5. Core dump kernel dump file directory and naming rules:

/proc/sys/kernel/core_uses_pid can control whether to add PID as an extension in the filename of the resulting core file, or 0 if the file content is 1.

This file can be modified by the following command:

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

6. How to use core files:

Under Linux, use:

#gdb-C Core.pid program_name

You can enter GDB mode.

When you enter a where, you can point to which line is down, which function, who calls, and so on.

(GDB) where

or enter BT.

(GDB) bt

7. How to make a normal program down:

#kill-S SIGSEGV pid

8. See where core file output is:

The directory that holds the Coredump is the current directory of the process, typically the directory where the command started the process. But if you start with a script, the script may modify the current directory, and the process's real current directory will be different from the directory where the script was originally executed. You can then view the target of the/proc/< process PID>/CWD symbolic link to determine the true current directory address of the process. Processes that are started through system services can also be viewed in this way.

Proc/sys/kernel/core_pattern can control where the core file is saved and the file name format.

This file can be modified by the following command:

echo "/corefile/core-%e-%p-%t" >core_pattern

The core file can be built uniformly into the/corefile directory, resulting in a file named core-command name-pid-timestamp

The following is a list of parameters:

%p-insert pid into filename add PID

%u-insert present UID into filename add current UID

%g-insert present GID into filename add current GID

%s-insert signal that caused the coredump into the filename adds a signal leading to the core

%t-insert Unix time when the coredump occurred into filename to add a core file generation

%h-insert hostname where the coredump happened into filename add host name

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

In Linux to ensure that the program crashes when generating coredump to pay attention to these issues:

To ensure that the directory in which the Coredump resides exists and that the process has write permission to the directory. The directory that holds the Coredump is the current directory of the process, typically the directory where the command started the process. But if you start with a script, the script may modify the current directory, and the process's real current directory will be different from the directory where the script was originally executed. You can then view the target of the/proc/process PID>/CWD symbolic link to determine the true current directory address of the process. Processes that are started through system services can also be viewed in this way.

If the program calls Seteuid ()/setegid () to change a valid user or group of processes, the system does not generate coredump for these processes by default. Many service programs will call Seteuid (), such as MySQL, no matter what user you use to run Mysqld_safe start mysql,mysqld The active user is always MSYQL user. If you ran a program with User A, but the user of the program you see in PS is B, then these processes are called seteuid. To enable these processes to generate core dumps, you need to/proc/sys/fs

The contents of the/suid_dumpable file are changed to 1 (generally default is 0).

Third, this is generally known, is to set a large enough core file size limit. The core file size that is generated when a program crashes is the amount of memory consumed by the program when it is run. But the behavior of the program crashes cannot be estimated as usual, such as buffer overflow errors can cause the stack to be corrupted, so often the value of a variable is modified to a mess, and then the program to use this size to apply for memory may cause the program to occupy a lot more memory than usual. Therefore, no matter how little memory is occupied when the program is running normally, it is good to make sure that the core file is generated or the size limit is set to unlimited.

Four, abnormal exit will certainly generate core. Is there no exception exit that does not generate a core?

If it is not the normal exit is the signal caused by the program exit, some signals can really cause the program to quit but not generate core.

Sighup Terminate process terminal line hangs up

SIGINT Terminate process Interrupt process

Sigquit establishes core file termination process and generates core files

Sigill establish core file illegal instruction

Sigtrap establishes core file tracking self-trapping

Sigbus Build core File Bus error

SIGSEGV Establish core file segment illegal error

SIGFPE creates a core file floating-point exception

Sigiot build core file to perform I/O self-trapping

SIGKILL Terminate process Kill process

Sigpipe terminates the process to write data to a pipe that does not read the process

Sigalarm Terminate process timer to

Sigterm terminate process software termination signal

SIGSTOP stops the process from terminating the signal.

SIGTSTP stops the process terminal to stop the signal

Sigcont ignores the signal and continues to execute a stopped process

Sigurg Ignore signal I/O emergency signal

Sigio ignores signal descriptors for I/O

SIGCHLD Ignore signal Notify parent process when child process stops or exits

Sigttou Stop process background process write terminal

Sigttin Stop process background process read Terminal

SIGXGPU Terminate process CPU time limit timeout

Sigxfsz Terminate process file length is too long

Sigwinch ignores signal window size changes

SIGPROF Terminate process statistic distribution chart with timer to time

SIGUSR1 Terminate process user-defined signal 1

SIGUSR2 Terminate process user-defined signal 2

SIGVTALRM Terminate process Virtual timer to

Set the possible signal to the handle and look at the situation.

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.