How to debug the core dump file in Linux

Source: Internet
Author: User

When developing and using Linux programs, the engine is sometimes inexplicable.
The core of the slave is deleted. I checked it online and sorted out a simple
Single debugging core file method.
1. What is core dump?
Core, that is, core memory, and dump is stacked.
Meaning. Core dump is also called a core dump.
When an exception occurs in the process and the program exits abnormally
The current memory status of the program is stored in a core file,
It is called core dump.
2. How to enable core dump support?
Some operating systems do not enable core dump by default.
Yes, you need to use the ulimit-C unlimited statement to set,
The location where the core file is generated is generally in the current project where the program is running.
Directory, the file name is core. Process number (of course, different systems
The path and file name of the corresponding Manual may be different.
).
3. How to use core dump
Select the-G option when compiling with GCC,
So that debug support can be started. When an executable file is generated, ex ,./
Ex run the executable file. If the program is dropped, it will generate
A core file, assuming core.1568, GDB ex
Core.1568 go to GDB, and then run the where command.
View it.
First, let's see what machine I use:
$ Uname-
Take a look at some default parameters. Pay attention to the core file size.
It is 0, and the core file will not be generated when a program error occurs.
$ Ulimit-
Core File size (blocks,-C) 0
......
Write a simple program to see if the core file will be
.
(Code omitted)
$ Gcc-wall-G Foo. c
$./A. Out
Segmentation fault
$ LS-L core .*
Ls: core. *: no such file or directory
The core file is not found. Modify the ulimit settings,
Make it generate. 1024 is random. If the core file is large
If the number is less than 1024, it cannot be generated.
$ Ulimit-C 1024
$ Ulimit-
Core File size (blocks,-C) 1024
......
$./A. Out
Segmentation fault (core dumped)
$ LS-L core .*
-RW ------- 1 uniware 53248.
Jun 30 :10 core.9128
Pay attention to the output information above. Multiple (Core
Dumped ). A core file is generated. 9128 is
PID of the process. Let's use GDB to look at this core.
$ GDB -- core = core.9128
(Output information omitted)
(GDB) BT
#0 0x08048373 in ?? ()
#1 0xbfffd8f8 in ?? ()
#2 0x0804839e in ?? ()
#3 0xb74cc6b3 in ?? ()
#4 0x00000000 in ?? ()
At this time, you cannot see backtrace with BT, that is, the call stack,
It turns out that GDB does not know where the symbol information is. We tell
It:

(GDB) file./A. Out

(GDB) BT
#0 0x08048373 in sub () at Foo. C: 17
#1 0x08048359 in main () at Foo. C: 8
Now the backtrace is available.
When a program unexpectedly exits, the kernel will work in the current job
Generate a core file (a memory image
Add debugging information ). Use GDB to view core files.
To indicate the file and number of lines of code that causes program errors.
Other Summary
1. Core File generation switch and size limit
1) run the ulimit-C command to view the raw data of the core file.
Switch. If the result is 0, this function is disabled.
The core file is generated.
2) use the ulimit-C filesize command to limit
Core File size (The unit of filesize is Kbyte ). If
Ulimit-C unlimited indicates that the core file size is not
Restricted. If the size of the generated information exceeds this value, the information will be deleted.
To generate an incomplete core file. In debugging
When this core file is used, GDB will prompt an error.
2. Name and path of the Core File
Core File generation path:
Enter the same path for the executable file to run the command.
If the core file generated by the system does not contain any extension
Name, then all are named core. The new core file will
Overwrite the original core file.
1)/proc/sys/kernel/core_uses_pid controllable
Whether to add a PID to the file name of the core file as an extension.
The file content is 1, indicating that the PID is added as the extension, and the generated
The format of the core file is core. xxxx; 0 indicates the generated
The same core file is named core.
Run the following command to modify the file:
Echo "1">/proc/sys/kernel/core_uses _
PID
2) proc/sys/kernel/core_pattern can be controlled
Core File storage location and file name format.
Run the following command to modify the file:
E c h o "/c o r e f I L E/C o r e-% E-% P-% t">
Core_pattern, which can generate core files/
In the corefile directory, the generated file name is core-command
Name-PID-Timestamp
3. view the Core File
Use GDB to view the core file.
GDB./A. Out
Core-file core. xxxx
Use the BT command to see where the program went wrong.
The following two command methods have the same effect,
In some environments, the preceding command is recommended.
1) GDB-core = core. xxxx
File./A. Out
BT
2) GDB-C Core. xxxx
File./A. Out
BT
4. Use the core file for debugging ON THE DEVELOPMENT BOARD
Debug the core file generated on the development board on the PC.
To use the gdb that comes with the cross compiler
Specify solib-absolute-prefix and solib-searchpath
Two variables to ensure that GDB can find executable programs
. There is a way to create a configuration file, not
You need to configure the above variables every time you start GDB, that is, when running
Create. gdbinit in the path of row GDB.
Configuration File Content:
Set solib-Abso lute-Prefix your_cross _
Compile_path
S e t s o l I B-S E A R C H-P A t h y O U R _ C R o s _
Compile_path
Set solib-search-path your_developer _
Tools_lib_path
Handle sig32 nostop noprint pass
Note: The executable files to be debugged must be compiled.
You must add-G to the core file to Display Error information normally! Yes
When the core information is large, you can mount it to the PC.
To avoid this.

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.