+3  A: 

With GCC you can try this:

-fstack-protector
Emit extra code to check for buffer overflows, such as stack smashing attacks. This is done by adding a guard variable to functions with vulnerable objects. This includes functions that call alloca, and functions with buffers larger than 8 bytes. The guards are initialized when a function is entered and then checked when the function exits. If a guard check fails, an error message is printed and the program exits.

-fstack-protector-all
Like -fstack-protector except that all functions are protected.

http://gcc.gnu.org/onlinedocs/gcc-4.3.3/gcc/Optimize-Options.html#Optimize-Options

Bastien Léonard
This prevents buffer overflows but no stack overflows :/
Manuel
+3  A: 

When a program dies with SIGSEGV, it normally dumps core on Unix. Could you load that core into debugger and check the state of the stack?

Arkadiy
No core dump is created in this case :/
Manuel
Are you sure your ulimit is right?I think the command is ulimit -c unlimited, but check the man page.
Arkadiy
+8  A: 

If you allow the system to dump core files you can analyze them with gdb:

$ ulimit -c unlimited # bash sentence to allow for infinite sized cores
$ ./stack_overflow
Segmentation fault (core dumped)
$ gdb -c core stack_overflow
gdb> bt
#0  0x0000000000400570 in f ()
#1  0x0000000000400570 in f ()
#2  0x0000000000400570 in f ()
...

Some times I have seen a badly generated core file that had an incorrect stack trace, but in most cases the bt will yield a bunch of recursive calls to the same method.

The core file might have a different name that could include the process id, it depends on the default configuration of the kernel in your current system, but can be controlled with (run as root or with sudo):

$ sysctl kernel.core_uses_pid=1
David Rodríguez - dribeas