How to capture and analyze SIGSEGV's site

Source: Internet
Author: User

Linux under the program to SIGSEGV signal Default processing method is to generate Coredump and terminate the program, you can refer to man 7 signal

       Signal Value Action comment────────────────────────────────────────────────────────────────────── SIGHUP 1 Term hangup detected on controlling terminal or death O  F Controlling Process SIGINT 2 term Interrupt from keyboard sigquit 3 Core Quit From keyboard Sigill 4 core illegal instruction SIGABRT 6 core Abort Signal FR       Om abort (3) SIGFPE 8 Core floating point exception SIGKILL 9 term Kill signal  SIGSEGV one Core Invalid memory reference sigpipe broken pipe:write to pipe       With no readers SIGALRM-term Timer signal from alarm (2)   SIGTERM termination signal SIGUSR1 30,10,16 term user-defined signal 1 SIGUSR2 31,12,17 term User-defined Signal 2 SIGCHLD 20,17,18 IGN Child stopped or terminated Sigcont 19,18,25 Cont Con Tinue if stopped SIGSTOP 17,19,23 stop stop process SIGTSTP 18,20,24 stop stop typed at Termin Al sigttin 21,21,26 stop Terminal input for background process Sigttou 22,22,27 stop Terminal Output for background process

Description of Action

       The entries in the ' Action ' column of the tables below specify       the default disposition for each signal, as Follows:
   term   Default Action is to terminate the process.       IGN    Default Action is to ignore the signal.       Core   Default action is to terminate the process and dump core (see              Core (5)).       The Stop   Default action is to stop the process.       Cont   Default Action is to continue the process if it is currently              stopped.

You can see that the signal that produces the core is more than SIGSEGV this one. Usually in the program has the memory invalid reference will produce the SIGSEGV, the concrete description see http://www.cnblogs.com/thammer/p/4737371.html.

  Methods for parsing segment errors :

1. Use GDB directly

If it is easy to reproduce the SIGSEGV direct gdb hangs to run, when the SIGSEGV is generated when GDB stops and prints the hint, the direct-typing command of the BT Viewer at this point in the function call stack frame can be located to which function in what kind of invocation situation in which the segment error occurs.

2. Using the core file +gdb

The Coredump,core file that is generated when the program receives SIGSEGV is the process memory context and the CPU register information for the exception process at the moment the exception occurred.

First, set the core file size Ulimit-c xxxx,xxxx is allowed to produce the core file size, usually set to unlimited, not limited to size

Then, run the program until the core file is generated, the name is generally core.xxx,xxx for the program process number, different distributions may have different naming rules

Then, run gdb, typing the command core-file corefile-name, then BT can

3. Register the SIGSEGV signal processing function and use some stack backtracking functions inside the processing function to print the stack frame information.

A. Using the glibc band function BackTrace backtrace_symbols backtrace_symbols_fd printing

       voidSigsegv_handler (int Signo)       {           intJ, Nptrs; void*Buffer[bt_buf_size]; Char**strings; Nptrs=backtrace (buffer, bt_buf_size); printf ("BackTrace () returned%d addresses\n", Nptrs); /*The call backtrace_symbols_fd (buffer, nptrs, Stdout_fileno) would produce similar output to the Followin G:*/Strings=backtrace_symbols (buffer, nptrs); if(Strings = =NULL) {Perror ("Backtrace_symbols");           Exit (Exit_failure); }            for(j =0; J < Nptrs; J + +) printf ("%s\n", Strings[j]);  Free(strings);
       exit ( -1); }

Backtrace_symbols and BACKTRACE_SYMBOLS_FD differ in that the latter will print input into a file specified by FD.

It has certain limitations:

     

       These functions make some assumptions about how a function ' s return       address was stored on the stack.  Note the following:       * Omission of the frame pointers (as implied by any of the  gcc (1) ' s          nonzero optimization level s) may cause these assumptions to be          violated.       * inlined functions does not have a  stack frames.       *  tail-call optimization causes one stack frame to replace another.       The symbol names may is unavailable without the use of the special linker       options.  For systems using the GNU linker, it's necessary to use       the-rdynamic linker option.  Note that names of "static" functions       is not exposed, and won ' t is available in the backtrace.

Programs that are optimized may fail

Failure of the inline function

Only the function address can be printed on the static function

function invalidation for tail-call optimization

Need to join-rdynamic at compile time

B. There are other ways or interfaces to do something like BackTrace, later to add

        

How to capture and analyze SIGSEGV's site

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.