/* * 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