[Turn]gdb combined with Coredump to locate the crash process

Source: Internet
Author: User
Tags cpu usage

[Turn]gdb combined with Coredump to locate the crash process

Http://blog.sina.com.cn/s/blog_54f82cc201013tk4.html

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 size of the corefile to infinity, but also can use the number to replace the unlimited, the corefile of the upper limit value to do more accurate settings.   where is the generated corefile?The place where the corefile 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, and is identified as follows:Percent: equal to%
%p: Equivalent to
%u: equivalent to
%g: Equivalent to
%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 generated corefile 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_pattern  test the following code?
1 2 3 4 5 6 7 8 9 Ten One A #include intfunc(int *p){        *p= 0;} intmain(){        func(NULL);        return0;}
 build the executable and run itGcc-o maina.c [email protected]:~#./main
Segmentation Fault (coredumped) <-----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 usegdbmain/tmp/core-main-10815View the information and find the function to be locatedProgram terminated with signal one, Segmentationfault.
#0 0x080483ba in func ()  

How do I navigate to a row?

turn on the-G debug switch on compile time.Gcc-omain- g A.Cgdb main/tmp/core-main-10815The final results are as follows, great. Program terminated with signal 11,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-cunlimited echo "/tmp/core-%e-%p" > /proc/sys/kernel/core_pattern gcc-omain-g A.Cgdb main/tmp/core-main-10815it's OK.  Additional notes: related common GDB commands1, (GDB) BackTracetake the example above as an exampleProgram terminated with signal one, Segmentationfault.#0 0x080483ba in func (p=0x0) Atmain.c:55*p = 0;(GDB) backtrace#0 0x080483ba in func (p=0x0) Atmain.c:5#1 0x080483d4 in Main () Atmain.c:10if it is a multithreaded environment (GDB) thread apply all BackTrace 2, (GDB) Print[var](gdb) Print P$1= (int *) 0x0(gdb) print &p$2= (int * *) 0xbf96d4d4 3, (GDB) x/fmt[address]whichFmtis a repeat count followed by a format letter and a sizeletter.Format Letters is O (octal), X (hex), D (decimal), U (unsigned decimal) ,T (binary), F (float), a (address), I (instruction), C (char) ands (string).Size Letters is B (byte), H (Halfword), W (word), G (Giant, 8 bytes).thespecified number of objects of the specified size areprintedaccording to the format. (gdb) x/d 0xbf96d4d40xbf96d4d4:0(gdb) x/c 0xbf96d4d40xbf96d4d4:0 ' \000 '  Additionally, there are 10 types of signals that can cause corefile 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:segmentviolation, Invalid memory reference

SIGXCPU: Exceeded CPU Usage resource limit (SETRLIMIT)

Sigxfsz: File length limit exceeded (setrlimit)

Sigsys: Invalid system call


[Turn]gdb combined with 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.