1

I have an linux(ARM) multithread application. How can I get the backtraces (for all threads) of the app on exit (not a crash). Note: I can’t use gdb with catch syscall because of not supported architecture for this feature.

I tried to set breakpoints with gdb on exit and _exit, but no success.

  • 1
    If your process exits with threads still running, then that's not very good. A process should make sure that it ends all its started threads and that they are joined, before exiting. And if you detach the threads and they are supposed to continue working on *program* (not process) exit, then you should only exit the main thread, not the process itself (as that will kill all threads, detached or not). – Some programmer dude Nov 02 '22 at 08:05

1 Answers1

1

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.

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56