First, the foreword :
Some programs can be compiled, but segment fault (segment errors) occur at run time. This is usually caused by a pointer error. But it's not like compiling errors that prompt you to a row of files, but without any information, making debugging difficult.
GDB: One way to do this is to use GDB's step to find out. 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.
Ulimit: If you want the system to produce a core file in the event of a signal interruption, we need to set it in the shell as follows: #设置core大小为无限 Ulimit-c unlimited #设置文件大小为无限 Ulimit Unlimited These require root privileges, and each time you reopen the interrupt under Ubuntu you need to re-enter the first command above to set the core size to infinity.
View core files with GDB: Below we can have a core dump when a run-time signal error occurs. 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 files 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. You can use #ulimit-a to look at these settings.
As you can see from here, if-C is displayed: Core file size (blocks,-c), if this value is 0, the core files 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. For example:
5. Core dump kernel dump file directory and naming rules:
int set_coredump_size (void)
struct Rlimit RL;
Rl.rlim_cur = 2<<20;
Rl.rlim_max = 2<<20;
ret = Setrlimit (Rlimit_core, &RL);
if (Ret < 0)
perror ("Setrlimit ()");
int main ()
char * ptr=0;
/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.
or enter 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.
Second, Core Dump configuration and debugging
1.core file generation switch and size limit
1 Use the ULIMIT-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 the ulimit-c filesize command, you can limit the size of the core file (the FileSize unit is KByte). If Ulimit-c Unlimited, 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. When 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 generated 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 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
3. View core files with GDB:
Here we can take a core dump when a run-time signal causes an 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, check backtrace with the BT command to check where the program is running to locate the file-> line of 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 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 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 dumps, you need to change the contents of the/proc/sys/fs/suid_dumpable file to 1 (the 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.
Ulimit--User Resource Constraint command
1, Description: Ulimit for the shell to start the process occupied by resources.
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:
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. 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:
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:
-rw-r--r--1 Lee Lee 150062 July 02:39 H
#ulimit-F #设置创建文件的最大块 (one = 512 bytes)
File size limit exceeded
-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, of course the system will generate 51200 bytes of NEWH files according to your setup.
3] You can put the ulimit you want to set in the/etc/profile environment file, like instance 1].
Purpose: Set or report user resource limits.
Syntax: Ulimit [-h] [s] [-a] [-c] [-d] [-f] [-m] [-n] [-S] [-t] [Limit]
Description: 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
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.
-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.
Returns the following exit values:
0 completed successfully.