Reprint: GDB combines coredump to locate the crash process

Source: Internet
Author: User
Tags cpu usage

The Linux environment often encounters a process that hangs out and cannot find the cause, and we can locate it by creating a core file with GDB.

How do I generate a core file?We can use the Ulimit command to set the size of the core file. By default, the size of the core file is set to 0 so that the system does not dump the core file. This is set with the following command:
Ulimit-c Unlimited
This will set the core file size to infinity, but also can use the number to replace the unlimited, the core file of the upper limit value for more precise settings. where is the generated core file?The place where core file is generated is defined in the/proc/sys/kernel/core_pattern file. The way to change to a directory that you have defined is:
echo "pattern" >/proc/sys/kernel/core_pattern
And only super users can modify these two files. "Pattern" is similar to the format of our C-language print string, related to the following: percent:%
%p: Equivalent to <pid>
%u: equivalent to <uid>
%g: Equivalent to <gid>
%s: number equivalent to the signal that caused the dump
%t: equivalent to dump time
%h: Equivalent to hostname
%e: Equivalent to the name of the execution file at this point, the resulting core file is set to the System/TMP directory with the following command, and the PID and execution file name are recordedecho "/tmp/core-%e-%p" >/proc/sys/kernel/core_patternTest the following code?
123456789101112 #include <stdio.h>  int func ( int *p) {           *p = 0; }  int main () {           func (NULL); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; return 0; }
Build the executable file and run Gcc-o main a.c[email protected]:~#./main
Segmentation fault (core dumped) <-----There was a segment error and a core file was generated. Found files in the/tmp directory core-main-10815 how do I see where the process is hanging?We can use GDB main/tmp/core-main-10815 to view the information, found that can be positioned to function the program terminated with signal one, segmentation fault.
#0 0x080483ba in func ()

How do I navigate to a row?

Turn on the-G debug switch on compile time Gcc-o main- GA.cgdb main/tmp/core-main-10815 finally see the results as follows, great. Program terminated with signal one, segmentation fault.
#0 0x080483ba in func (p=0x0) at A.c:5
5 *p = 0; To sum up, we need only 4 operations to locate the process on which we want to go.Ulimit-c Unlimitedecho "/tmp/core-%e-%p" >/proc/sys/kernel/core_patternGcc-o main-g a.cgdb main/tmp/core-main-10815 is OK. Additional notes: related common GDB commands1, (GDB) backtrace/* View current thread function stack backtracking * * Take the example above for example program terminated with signal one, segmentation fault. #0 0X080483BA in func (p =0x0) at main.c:55*p = 0;(GDB) backtrace#0 0x080483ba in func (p=0x0) at Main.c:5#1 0x080483d4 in Main () at main.c:10 if it is a multithreaded environment (GDB) thread apply All BackTrace/* Show all thread stacks backtracking */2, (gdb) print [var]/* View variable values */(GDB) Print p$1 = (int *) 0x0 (gdb) Print &p$2 = (int * *) 0xbf96d4d4 3, (GDB) x/fmt [address]/* View the value of the address according to the format */where FMT is a repeat count followed by a format letter and a size Lette R.format Letters is O (octal), X (hex), D (decimal), U (unsigned decimal), T (binary), F (float), a (address), I (instruction), C (char) and S (String). Size Letters is B (byte), H (Halfword), W (word), G (Giant, 8 bytes). The specified number of objects of the specified size is printedaccording to the format. (GDB) x/d 0xbf96d4d40xbf96d4d4:0(GDB) x/c 0xbf96d4d40xbf96d4d4:0 ' \000 ' In addition, there are 10 types of signals that can cause core file files to be generated:

Sigquit: Terminal exit character

Sigill: Illegal hardware instructions

SIGTRAP: Platform-related hardware errors, which are now used for breakpoints when debugging

Sigbus: Platform-related hardware errors, typically memory errors

SIGABRT: This signal is generated when the abort function is called, and the process terminates abnormally

SIGFPE: Arithmetic anomalies

Sigsegv:segment violation, invalid memory reference

SIGXCPU: Exceeded CPU Usage resource limit (SETRLIMIT)

Sigxfsz: File length limit exceeded (setrlimit)

Sigsys: Invalid system call

Reprint: GDB combines coredump to locate the crash process

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.