Printing Stack method when Linux application exception

Source: Internet
Author: User
Tags stack trace

/* * Program abnormal termination When printing exception program call stack * gcc-g-rdynamic backtracetest.c-o backtracetest * * Run Program error: * System error, Stack trace: * 0./ Backtracetest (systemerrorhandler+0x77) [0x40095b] * 1/lib64/libc.so.6 () [0x3a4fe326b0] * 2./backtracetest (Fun1+0x10) [0X400A10] * 3./backtracetest (FUN+0XE) [0x400a23] * 4./backtracetest (MAIN+0X37) [0x400a5c] * 5/LIB64/LIBC.SO.6 (__libc_ START_MAIN+0XFD) [0x3a4fe1ed5d] * 6./backtracetest () [0x400829] * segmentation fault (core dumped) * GDB print error line information * GDB B Acktracetest * (GDB) Info Line *0x400a10 * line all in "backtracetest.c" starts at address 0x400a0c <Fun1+12> and end s at 0x400a13 <fun1+19>. * (GDB) List *0x400a10 * 0x400a10 is in Fun1 (backtracetest.c:66). * Warning:source file is more recent than executable. * "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * FUN1] UN () * * * * * * * * * * * * addr2line locator address corresponding source code location * ADDR2LINE-E backtracetest-i 0x400a10 */home/cyf/workspace/backtraceTEST/BACKTRACETEST.C:66 *  */#include<stdio.h>#include<stdlib.h>#include<signal.h>#include<string.h>#include<execinfo.h>voidSystemerrorhandler (intSignum) {    Const intlen=1024x768; void*Func[len];    size_t size; inti; Char**funs;    Signal (SIGNUM,SIG_DFL); Size=BackTrace (Func,len); Funs=(Char**) Backtrace_symbols (func,size); fprintf (stderr,"System error, Stack trace:\n");  for(i=0; i<size;++i) fprintf (stderr,"%d%s \ n", I,funs[i]);  Free(funs); //exit (1);}voidFun1 () {Char*p=NULL; *p ='A';}voidFun () {Fun1 ();}intMainConst intargcConst Char*argv[]) {signal (Sigsegv,systemerrorhandler);//Invaild memory AddressSignal (Sigabrt,systemerrorhandler);//Abort SignalFun (); return 0;}

Printing Stack method when Linux application exception

Related Article

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.