Positioning and commissioning of segmentation fault

Source: Internet
Author: User
Tags time limit cpu usage

1. Preface: Some programs can be compiled, but there will be segment fault (segment errors) at run time. This is usually caused by a pointer error. But this is not the same as compiling errors prompts the file-> line, but there is no information, so that our debugging becomes difficult.

2. GDB: One way to do this is to use GDB's step to find the steps. This is possible in a short code, but to get you to step a tens of thousands of lines of code, I think you will dislike the name of the programmer and call him a debugger. We have a better idea, and this is the core file.

3. Ulimit: If you want the system to generate core files in the event of a signal interruption, we need to set the following in the shell: #设置core大小为无限 ulimit-c Unlimited #设置文件大小为无限 Ulimit Unlimited these needs With root privileges, the first command that you need to reset each time you reopen an interrupt in Ubuntu sets the core size to infinity.

4. View core files with GDB: Below we can have a core dump in the event of a run-time signal error. 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 the backtrace to check where the program runs to locate the file-> line of core dump.

1. What is core:

Sam had always thought that the core of core dump was the meaning of Linux kernel. It was discovered today that core is another meaning:

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)

-A all current limits are reported
-c The maximum size of core files created
-d The maximum size of a process transmission's data segment
-e The maximum scheduling priority ("nice")
-F The maximum size of files written by the shell and its children
-I the maximum number of pending signals
-L The maximum size is locked into memory
-M The maximum resident set size (has no effect on Linux)
-N The maximum number of open file descriptors (most systems does not allow this value to is set)
-P The pipe size in 512-byte blocks (this May is set)
Q The maximum number of bytes in POSIX message queues
-R The maximum real-time scheduling priority
-s the maximum stack size
-T the maximum amount of CPU time in seconds
-U The maximum number of processes available to a single user
-V The maximum amount of virtual memory available to the shell
-X The maximum number of file locks

As you can see from here, if-C is displayed:core file size (blocks,-c)

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.

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.

9. How to use core dump under embedded devices: Linux coredump configuration and debuggingLinux
Core Dump Configuration and debugging
1.core file generation switch and size limit
---------------------------------
1) using Ulimit
The-C command to view the build switch for the core file. If the result is 0, the feature is turned off and the core file is not generated.
2)
Using Ulimit
-cfilesize command, you can limit the size of the core file (filesize units are kbyte). If Ulimit
-cunlimited, the size of the core file is unrestricted. If the generated information exceeds this size, it will be cropped, resulting in an incomplete core file. In Debugging this
Core file, GdB prompts for an error.
2.core file name and build path
----------------------------
If the system-generated core file does not have any other extended names, it is all named Core. The new core file generation will overwrite the original core file.
1)/proc/sys/kernel/core_uses_pid can control whether the file name of the core file adds PID as an extension. The file content is 1, which means adding PID as the extension, the resulting core file format is core.xxxx, and 0 indicates that the generated core file is named Core.
This file can be modified by the following command:
Echo
"1" >
/proc/sys/kernel/core_uses_pid
2 Proc/sys/kernel/core_pattern can control the location of the core file and 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 unified into the/corefile directory, the resulting file name is 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
Add a signal that causes the core to be generated

%t-insert UNIX time this coredump occurred into filename
Unix time to add 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
3. View core files with GDB:
Below we can occur when the run-time signal caused by the error occurs when the core
Dump out.
Occurs core
After dump,
Using GDB to view the contents of the core file,
To locate a core in a file
The row of the dump.
GDB [exec
File] [core file]
Such as:
Gdb
./test Test.core
After entering GDB,
Check backtrace with the BT command to see where the program is going to run,
To locate the file-> line for core dump.
4. Use core file debugging on Development Board
-----------------------------
If the development Board's operating system is also Linux,core debugging method is still applicable. If GDB is not supported on the Development Board, the Environment (headers, libraries), executables, and core files of the development Board can be copied to the PC's Linux, and the relevant commands run.
Note: The executable file to be debugged, you need to add a-g,core file at compile time to display error messages normally.
Issues to be noted:
In Linux to ensure that the program crashes when generating coredump to pay attention to these issues:

To ensure the existence of the directory where the coredump is stored and the process
Record has write permission. 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 might modify the current mesh
Record, the actual current directory of the process will be different from the directory where the script was originally executed. You can then view the target of the/proc/process PID&GT;/CWD symbolic link to determine the process
The true current directory address. Processes that are started through system services can also be viewed in this way.

Second, if the program calls the Seteuid ()/setegid () change
A valid user or group of processes, the system does not generate coredump for these processes by default. Many service programs will invoke Seteuid (), such as MySQL, whatever you use
The active user who runs Mysqld_safe start Mysql,mysqld is always the MSYQL user. If you ran a program with User A, but you see it in PS
The user of this program is B, then these processes are called seteuid. To enable these processes to generate core
Dump, need to be/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
Out. The core file size that is generated when a program crashes is the amount of memory consumed by the program when it is run. However, the behavior of a program crash cannot be estimated as usual, such as a buffer overflow error that could cause the stack to be
Damage, so it is often possible to change the value of a variable into a mess, and then the program using that size to request memory may cause the program to occupy a lot more memory than usual. So regardless of whether the program is running properly
How little memory is needed to make sure that the core file is generated or that the size limit is set to unlimited.

Ulimit--User resource Constraint command 1. Description :
Ulimit the resources used by the shell startup process.


2. Category :
Shell built in command


3. Syntax format :
Ulimit [-ACDFHLMNPSSTVW] [size]


4, Parameter introduction :
-H Set hardware resource limits.
-S sets the software resource limit.
-a displays all current resource restrictions.
-C Size: Sets the maximum value for the core file. Unit: Blocks
-D Size: Sets the maximum value for the data segment. Unit: Kbytes
-F Size: Sets the maximum value for creating a file. Unit: Blocks
-L Size: Sets the maximum value to lock a process in memory. Unit: Kbytes
-M Size: Sets the maximum amount of resident memory that can be used. Unit: Kbytes
-N Size: Sets the maximum number of file descriptors that the kernel can open at the same time. Unit: N
-P Size: Sets the maximum value of the pipe buffer. Unit: Kbytes
-S size: Sets the maximum value for the stack. Unit: Kbytes
-T size: Sets the maximum CPU usage time limit. Unit: Seconds
-V Size: Sets the maximum value for virtual memory. Unit: Kbytes 5, simple example:


5. Examples
When writing a program under Linux, if the program is larger, often encounter "segment error" (Segmentationfault) Such a problem, this is mainly due to the Linux system's initial stack size (stack sizes) is too small, generally 10M. I usually set the stacksize to 256M, so there is no error. The command is:
Ulimit-s 262140
If you want the system to automatically remember this configuration, edit the/etc/profile file, under the "Ulimit-s-C 0 >/dev/null 2>&1" line, add "Ulimit-s 262140" to save the reboot system.
1] In RH8 's environmental file/etc/profile, we can see how the system configures Ulimit:
#grep Ulimit/etc/profile
Ulimit-s-C 0 >/dev/null 2>&1
This statement sets the size of the software resources and the core file
2] If we want to make some restrictions on the size of the file created by the shell, such as:
#ll h
-rw-r--r--1 Lee Lee 150062 July 02:39 H
#ulimit-F #设置创建文件的最大块 (one = 512 bytes)
#cat H&GT;NEWH
File size limit exceeded
#ll NEWH
-rw-r--r--1 Lee Lee 51200 November 8 11:47 NEWH
The size of file h is 150062 bytes, and the size of the creation file we set is 512 bytes x100 block = 51200 bytes
The system will, of course, generate 51200-byte NEWH files based on your settings.
3] You can put the ulimit you want to set in the/etc/profile environment file, like instance 1].
Use
Set or report user resource limits.
Grammar
Ulimit [-h] [s] [A] [-c] [-d] [-f] [-m] [-n] [-S] [-t] [Limit]
Describe
The Ulimit command sets or reports user process resource limits, as defined by the/etc/security/limits file. The file contains the following default value limits:
Fsize = 2097151
Core = 2097151
CPU =-1
data = 262144
RSS = 65536
stack = 65536
Nofiles = 2000
These values are used as default values when new users are added to the system. When you add a user to the system, the above values are set by the Mkuser command or changed by the Chuser command.
The limit is divided into soft or rigid. The Ulimit command allows the user to change the soft limit to the maximum set value of the hard limit. To change the resource hard limit, you must have root user rights.
Many systems do not include one or more of these limits. The limit of a particular resource is set when the Limit parameter is specified. The value of the Limit parameter can be a number in a specified cell in each resource, or a value of unlimited. To set a specific ulimit to unlimited, you can use the word Unlimited.
Note: Setting the default limit in the/etc/security/limits file sets the system width limit, not just the user's required limit when creating the user.
When the Limit parameter is omitted, the current resource limit is printed. The soft limit is printed unless the user specifies the-H flag. When a user specifies more than one resource, the limit name and cell are printed before the value. If an option is not given, the-f flag is assumed.
Because the Ulimit command affects the current shell environment, it is provided as a Shell general built-in command. If the command is invoked in a stand-alone command execution environment, it does not affect the file size limit of the caller's environment. This is the case in the following example:
Nohup ulimit-f 10000
ENV Ulimit 10000
Once the hard limit is reduced through the process, it cannot be increased without root privilege, even if it is returned to the original value.
For more information on user and system resource limits, see Volume, set in AIX 5L Version 5.3 Technical reference:baseoperating System and Extensions getrlimit 1 Rlimit or Vlimit subroutine.
Sign
-A lists all current resource limits.
-c Specifies the size of the core dump in 512-byte blocks.
-d Specifies the size of the data region in K-byte units.
-F Sets the file size limit (in blocks) when using the Limit parameter, or reports the file size limit when no parameters are specified. The default value is the-f flag.
-h Specifies the hard limit for setting a given resource. You can increase the hard limit if the user has root user rights. Any user can reduce the hard limit.
-m specifies the size of the physical storage in K-byte units.
-n Specifies the limit of the number of file descriptors that a process can hold.
-s Specifies the size of the stack in K-byte units.
-s specifies that the soft limit is set for the given resource. The soft limit can be increased to the value of the hard limit. If the-H and-s flags are not specified, the limit applies to both.
-t specifies the number of seconds per process to use.
Exit status
Returns the following exit values:
0 completed successfully.
>0 rejects a request for a higher limit, or an error occurs.
Example
To set the file size limit to 51,200 bytes, enter:
Ulimit-f 100


Does an exception exit have to be core generated? Is there no exception that does not generate a core exit??

If it's not a normal exit, it's a signal-triggered program exit. Some signals do cause the program to quit but not generate the 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. Turn from: http://blog.csdn.net/yangzhu1982/article/details/6318600

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.