"Turn" section error debugging artifact-Core Dump

Source: Internet
Author: User

from:http://www.embeddedlinux.org.cn/html/jishuzixun/201307/08-2594.htmlSection error debugging artifact-Core dump detailed Source:Internet Alex Time:2013-07-08 Tag: Linux Click:11670

First, preface :

Some programs can be compiled, but segment fault (segment errors) occur at run time. This is usually caused by a pointer error. But this is not like a compile error will prompt to a file line, but there is no information, making our debugging become difficult to get up.

GDB: One way to do this is to use GDB's step step to find out. This is possible in a short code, but to get you to step into 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, this is the core file.

Ulimit: If you want the system to produce a core file in case of an error caused by a signal interruption, we need to set it in the shell as follows: #设置core大小为无限 Ulimit-c unlimited #设置文件大小为无限 Ulimit Unlimited These need to have root permission, each time you reopen interrupts under Ubuntu, you need to re-enter the first command above to set the core size to infinity.

View the core file with GDB: Below we can take the core dump in case of an error caused by a runtime signal. After core dump occurs, use GDB to view the contents of the core file to locate the row in the file that raised 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 is running, and to locate the file-and-line of core dump.

1. What is Core

Sam has always thought that core dump core is the meaning of Linux kernel. Just found out here today, the core is another meaning:

In the use of semiconductors as memory material, the human is the use of coils as memory material (inventor of Wang), the coil is called core, the memory of the coil is called the core memories. Now that the semiconductor industry has grown, no one has used the core memory, but in many cases people have called it core.

2. What is Core Dump

When we develop (or use) a program, the most feared is that the program is out of the way. Although the system is fine, we may still encounter the same problem next time. Then the operating system will dump the memory content of the program when it comes out (now usually written in a file called core), 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 is down, but the core file is not generated

Under Linux, there are some settings that indicate the resources available to the shell and to processes. You can use #ulimit-a to see these settings. (Ulimit is bash built-in Command)

-A All current limits is reported

-c The maximum size of core files created

-d The maximum size of a process Echnical'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 that m ay is locked into memory

-M The maximum resident set size (have no effect on Linux)

-N The maximum number of open file descriptors (most systems does not allow this value to be set)

-P The pipe size in 512-byte blocks

-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 can be seen 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 the core file.

If the program generates a core file when an error occurs, segmentation fault (core dumped) is displayed.

5. Core dump file directory and naming rules:
/proc/sys/kernel/core_uses_pid can control whether a PID is added to the file name of the resulting core file, and if it is added, the content is 1, otherwise 0

6. How to use the core file
Under Linux, use:
#gdb-C Core.pid program_name
You can enter GDB mode.
Input where, you can indicate which line is down, which function, who 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. See where the core file output is:
The directory where the coredump is stored is the current directory of the process, which is usually the directory where the command was launched to start the process. However, if you start with a script, the script may modify the current directory, and the actual current directory of the process will be different from the directory where the script was executed. You can then view the target of the/proc/< process PID&GT;/CWD symbolic link to determine the actual current directory address of the process. Processes initiated through system services can also be viewed in this way.

9. How to use core dump under embedded devices:
Linux coredump Configuration and commissioning
Linux

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, this 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 (FileSize is KByte). If Ulimit-c Unlimited, the core file size is not restricted. If the generated information exceeds this size, it will be cropped, resulting in an incomplete core file. GDB will prompt for errors while debugging this core file.

the name and build path of the 2.core file

----------------------------

If the system-generated core file does not have any other extension 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 a PID is added as an extension in the file name of the core file. The file content is 1, which means adding a PID as the extension, the resulting core file format is core.xxxx, and 0 means that the resulting core file is named Core.

You can modify this file by using the following command:
echo "1" >/proc/sys/kernel/core_uses_pid

2) Proc/sys/kernel/core_pattern can control the core file save location and file name format.
You can modify this file by using the following command:
echo "/corefile/core-%e-%p-%t" >core_pattern, the core file can be generated uniformly 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 current UID to filename to add the present UID
%g-insert current GID into filename to add an existing GID
%s-insert signal that caused the coredump into the filename adds a signal that causes the core to be generated
%t-insert Unix time, the coredump occurred into filename when you add a core file when you build Unix
%h-insert hostname where the coredump happened into filename adds host name
%e-insert coredumping executable name into filename add command name

3. View the core file with GDB:

Below we can take the core dump in case of an error caused by a runtime signal.
After core dump occurs, use GDB to view the contents of the core file to locate the row in the file that raised 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 is running to locate the file-and-line of core dump.

4. Debugging on the board using core files

-----------------------------

If the operating system of the board is also linux,core the Debug method still applies. If GDB is not supported on the board, you can copy the environment (header files, libraries), executables, and core files of the board to your PC's Linux and run the relevant commands.

Note: The executable file to be debugged, at compile time need to add-g,core file to display error message normally!

Note the issue:

In Linux to ensure that the program crashes when generating coredump to be aware of these issues:

To ensure that the directory in which the coredump is stored exists and that the process has write access to the directory. The directory where the coredump is stored is the current directory of the process, which is usually the directory where the command was launched to start the process. However, if you start with a script, the script may modify the current directory, and the actual current directory of the process will be different from the directory where the script was executed. You can then view the target of the/proc/process PID&GT;/CWD symbolic link to determine the actual current directory address of the process. Processes initiated through system services can also be viewed in this way.

If the program calls Seteuid ()/setegid () changes the active user or group of the process, the system does not generate coredump for these processes by default. Many service programs will call Seteuid (), such as MySQL, regardless of what user you use to run Mysqld_safe startup Mysql,mysqld The active user is always MSYQL user. If you have run 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, you need to change the contents of the/proc/sys/fs/suid_dumpable file to 1 (which is typically 0 by default).

Third, this is generally known, is to set a large enough core file size limit. The size of the core file that is generated when the program crashes is the amount of memory that the program consumes when it runs. However, the behavior of the program crashes can not be estimated as usual behavior, such as buffer overflow and other errors may cause the stack to be destroyed, so often the value of a variable is modified to a mess, and then the program uses this size to apply memory can cause the program more memory than usual. So no matter how little memory is used when the program is running properly, it's good to make sure that you build the core file or set the size limit to unlimited.


Ulimit--User Resource Restriction 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 sets the hardware resource limit.
-S sets the software resource limit.
-a displays all current resource limits.
-C Size: Sets the maximum value of the core file. Unit: Blocks
-D Size: Sets the maximum value for the data segment. Unit: Kbytes
-F Size: Sets the maximum value of the created file. Unit: Blocks
-L Size: Sets the maximum value of the locked process in memory. Unit: Kbytes
-M Size: Sets the maximum number 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 of 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. Example

When writing programs under Linux, if the program is relatively large, often encounter "segment error" (Segmentationfault) Such a problem, mainly because the Linux system initial stack size (stack size) is too small, generally 10M. I generally set the stacksize to 256M, so there is no paragraph 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", Save the restart system is OK!

1] In RH8 's environment file/etc/profile, we can see how the system is configured for Ulimit:
#grep Ulimit/etc/profile
Ulimit-s-C 0 >/dev/null 2>&1
This statement sets the settings for the software resource and the size of 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 #设置创建文件的最大块 (piece = 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 the file h is 150062 bytes, and the size of the created file we set is 512 bytes x100 block = 51200 bytes, and of course the system generates 51200 bytes of newh file according to your settings.

3] Just like example 1], put the ulimit you want to set in/etc/profile this environment file.
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
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 adding 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 hard. With the Ulimit command, the user can change the soft limit to the maximum setting value of the hard limit. To change the hard limit of a resource, you must have the root user right.

Many systems do not include one or more of these limits. The limits of a particular resource are 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 is the setting of the system width limit, not just the limit required by the user 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 unit are printed before the value. If no option is given, the-f flag is assumed.

Because the Ulimit command affects the current shell environment, it is provided as a shell regular built-in command. If the command is called in a stand-alone command execution environment, the file size limit of the caller's environment is not affected. 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 privileges, even if the return to the original value is not possible.

For more information on user and system resource limits, see the Volume, set in AIX 5L Version 5.3 Technical reference:baseoperating system and Extensions Getrlimit 1 Rlimit or Vlimit subroutines.

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 to set the hard limit for a given resource. If the user has root user rights, the hard limit can be increased. Any user can reduce the hard limit.

-m specifies the size of the physical memory in K-byte units.

-n Specifies the limit of the number of file descriptors a process can have.

-s Specifies the size of the stack in K-byte units.

-s specifies to set the soft limit for a given resource. The soft limit can be increased to the value of the hard limit. If both the-H and-s flags are unspecified, the limit applies to both.

-t specifies the number of seconds to use per process.

Exit status

Returns the following exit values:

0 completed successfully.

"Turn" section error debugging artifact-Core Dump

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.