Can I get a C++ stack trace when Android app crashes?

trace.txt file give something ? I don't remember if his location is : /data/anr/trace.txt or /data/data/{pkg}/trace.txt


You need to start by trapping the SIGSEGV to execute code when you get a segv. This is posix code, so something similar should work on android:

void abortHandler( int signum, siginfo_t* si, void* unused )
{
   const char* name = NULL;
   switch( signum )
   {
   case SIGABRT: name = "SIGABRT";  break;
   case SIGSEGV: name = "SIGSEGV";  break;
   case SIGBUS:  name = "SIGBUS";   break;
   case SIGILL:  name = "SIGILL";   break;
   case SIGFPE:  name = "SIGFPE";   break;
   case SIGPIPE: name = "SIGPIPE";  break;
   }

   if ( name )
      printf( stderr, "Caught signal %d (%s)\n", signum, name );
   else 
      printf( stderr, "Caught signal %d\n", signum );

   printStackTrace( stderr );

   exit( signum );
}

void handleCrashes()
{
   struct sigaction sa;
   sa.sa_flags = SA_SIGINFO;
   sa.sa_sigaction = abortHandler;
   sigemptyset( &sa.sa_mask );

   sigaction( SIGABRT, &sa, NULL );
   sigaction( SIGSEGV, &sa, NULL );
   sigaction( SIGBUS,  &sa, NULL );
   sigaction( SIGILL,  &sa, NULL );
   sigaction( SIGFPE,  &sa, NULL );
   sigaction( SIGPIPE, &sa, NULL );
}

The next thing is to call that function to register the signal handlers. You can do it as the first thing in main, but then you won't get stack traces until main. If you want them before, you can call this function from the constructor of a global object. But there is no guarantee that it will be the first called constructor. There are ways to make sure it gets called early. For example, overload operator new - in debug builds - to first initialize the stack traces on the first allocation, and then call into the real operator new. This will give you stack traces starting on the first allocation.

To print a stack trace:

void printStackTrace( unsigned int max_frames = 63 )
{
   void* addrlist[max_frames+1];

   // retrieve current stack addresses
   u32 addrlen = backtrace( addrlist, sizeof( addrlist ) / sizeof( void* ));

   if ( addrlen == 0 ) 
   {
      printf( stderr, "  <empty, possibly corrupt>\n" );
      return;
   }

   char** symbollist = backtrace_symbols( addrlist, addrlen );

   for ( u32 i = 3; i < addrlen; i++ )
      printf( stderr, "%s\n", symbollist[i] ):
}

You will need to do more work to demangle the symbols to make them readable. try abi::__cxa_demangle. Of course build with -g and link with -rdynamic.