Returning from main()
is not guaranteed to call either exit()
or _exit()
. That's an internal implementation detail that isn't covered by the C standard.
You can add an exit handler function using atexit()
and then set a gdb
breakpoint in that function.
#include <stdlib.h>
.
.
.
static void myExitHandler( void )
{
write( STDERR_FILENO, "in exit handler\n",
strlen( "in exit handler\n" ) );
}
And in your main()
:
atexit( myExitHandler );
Then you should be able to set a breakpoint in myExitHandler()
and it should be triggered when your process exits.
If you need to get backtraces for all threads programmatically without using gdb
, if your program keeps track of its threads (it sure should...) you can pass the thread ids to your exit handler and use something like libunwind
to get the backtraces. See Getting a backtrace of other thread for some ways to do that.
But as noted in the comments, if your threads are actually working, simply exiting the process entirely can cause problems such as data corruption.