Can you get any program in Linux to print a stack trace if it segfaults?
Solution 1:
There might be a better way, but this kind of automates it.
Put the following in ~/backtrace
:
backtrace
quit
Put this in a script called seg_wrapper.sh
in a directory in your path:
#!/bin/bash
ulimit -c unlimited
"$@"
if [[ $? -eq 139 ]]; then
gdb -q $1 core -x ~/backtrace
fi
The ulimit
command makes it so the core is dumped. "$@"
are the arguments given to the script, so it would be your program and its arguments. $?
holds the exit status, 139 seems to be the default exit status for my machine for a segfault.
For gdb
, -q
means quiet (no intro message), and -x
tells gdb
to execute commands in the file given to it.
Usage
So to use it you would just:
seg_wrapper.sh ./mycommand and its arguments
Update
You could also write a signal handler that does this, see this link.
Solution 2:
Sorry to come in here 2 years later ... stumbled upon while looking for something else. Adding this for completeness.
1) While I think the accepted answer is great, it requires gdb. The method I am familiar with uses libSegFault.so.
If you run your app with
LD_PRELOAD=...path-to.../libSegFault.so myapp
You would get a report with backtrace, loaded libs, etc
2) A wrapper script catchsegv
is also available that would attempt to use addr2line
to translate addresses to filename + line number.
These are much lighter solutions than core files or gdb (good for embedded systems for example)
Solution 3:
You need everyone's friend GDB
gdb <program> [core file]
Once you've loaded your corefile, the command 'backtrace' (can be abbreviated to bt) will give you the current call stack. If you run your program from inside gdb, you can set arbitrary breakpoints and examine the memory contents, etc.
Solution 4:
catchsegv
It was mentioned in another answer (but in no way focused on). It's a handy tool bundled with the glibc project. It will provide a backtrace (and other useful debug information) only if a program does indeed segfault.
A good write up exists here.
You can include it in your own scripts as you see fit.
Solution 5:
Ubuntu (as a project) uses Apport to do this. You can look how they did it.
https://wiki.ubuntu.com/Apport