I am using g++ 4.9.2 on Solaris 10 as well as on Linux. I am using a signal handler. Whenever there is a signal, I would like to show the stack trace. How do I show the stack trace in Linux?
how do I show stack trace in Linux?
3.2k Views Asked by Dr. Debasish Jana AtThere are 2 best solutions below

In principle, a signal handler can only call a (small) set of functions, only the so called async-signal safe functions (which excludes backtrace
from GNU libc, as suggested by Maxim's answer). Read signal(7).
In practice, especially if you only want to show a backtrace and then abort, you might not care. Look also into Ian Taylor's libbacktrace which parses DWARF debug information (so you'll better compile your code with -g
flag to gcc
or g++
, perhaps in addition of optimization flags like -O2
). Inside GCC it is practically used to dump a nice backtrace on bad signals (including segmentation faults, i.e. SIGSEGV
) from their signal handlers and works quite well most of the time. You should then use a recent version of GCC, e.g. GCC 6
There is
backtrace
family of functions that do that. See the man page for examples.