Files
itgmania212121/stepmania/src/archutils/Unix/CrashHandler.cpp
T

481 lines
13 KiB
C++
Raw Normal View History

2003-07-27 07:07:45 +00:00
#include "global.h"
2004-04-05 05:22:32 +00:00
#include <cstdio>
#include <cstdlib>
#include <cstdarg>
2003-07-27 07:07:45 +00:00
#include <unistd.h>
2004-04-05 05:22:32 +00:00
#include <cerrno>
2003-07-27 07:07:45 +00:00
#include <limits.h>
#include <fcntl.h>
2004-04-05 05:22:32 +00:00
#include <csignal>
2003-07-27 07:07:45 +00:00
#include "RageLog.h" /* for RageLog::GetAdditionalLog, etc, only */
2004-01-13 00:01:07 +00:00
#include "RageThreads.h"
2004-03-12 05:15:32 +00:00
#include "Backtrace.h"
2003-07-27 07:07:45 +00:00
#include <sys/types.h>
#include <sys/wait.h>
#include "CrashHandler.h"
#include "CrashHandlerInternal.h"
#if defined(MACOSX)
2004-09-07 12:40:44 +00:00
# include "../Darwin/DarwinThreadHelpers.h"
#endif
extern uint64_t GetInvalidThreadId();
extern const char *g_pCrashHandlerArgv0;
2005-12-20 03:27:17 +00:00
static void safe_print( int fd, ... )
2003-07-27 07:07:45 +00:00
{
va_list ap;
2005-12-20 03:27:17 +00:00
va_start( ap, fd );
2003-07-27 07:07:45 +00:00
2005-12-20 03:27:17 +00:00
while( true )
2003-07-27 07:07:45 +00:00
{
2005-12-20 03:27:17 +00:00
const char *p = va_arg( ap, const char * );
if( p == NULL )
2003-07-27 07:07:45 +00:00
break;
2005-12-20 03:27:17 +00:00
write( fd, p, strlen(p) );
2003-07-27 07:07:45 +00:00
}
2005-12-20 03:27:17 +00:00
va_end( ap );
2003-07-27 07:07:45 +00:00
}
2005-12-20 03:27:17 +00:00
static const char *itoa( unsigned n )
{
static char ret[32];
char *p = ret;
for( int div = 1000000000; div > 0; div /= 10 )
{
*p++ = (n / div) + '0';
n %= div;
}
*p = 0;
p = ret;
while( p[0] == '0' && p[1] )
++p;
return p;
}
#if defined(LINUX)
static void GetExecutableName( char *buf, int bufsize )
{
/* readlink(/proc/pid/exe). This is more reliable than argv[0]. */
char proc_fn[1024] = "/proc/";
strcat( proc_fn, itoa( getpid() ) );
strcat( proc_fn, "/exe" );
int got = readlink( proc_fn, buf, bufsize-1 );
if( got == -1 )
{
safe_print( fileno(stderr), "Crash handler readlink ", proc_fn, " failed: ", strerror(errno), "\n", NULL );
strncpy( buf, g_pCrashHandlerArgv0, bufsize );
buf[bufsize-1] = 0;
}
buf[got] = 0;
}
#else
static void GetExecutableName( char *buf, int bufsize )
{
strncpy( buf, g_pCrashHandlerArgv0, bufsize );
buf[bufsize-1] = 0;
}
#endif
2003-07-27 07:07:45 +00:00
2004-02-15 06:32:07 +00:00
static void NORETURN spawn_child_process( int from_parent )
2003-07-27 07:07:45 +00:00
{
/* We need to re-exec ourself, to get a clean process. Close all
* FDs except for 0-2 and to_child, and then assign to_child to 3. */
2005-12-20 03:27:17 +00:00
for( int fd = 3; fd < 1024; ++fd )
if( fd != from_parent ) close(fd);
2003-07-27 07:07:45 +00:00
2005-12-20 03:27:17 +00:00
if( from_parent != 3 )
2003-07-27 07:07:45 +00:00
{
2005-12-20 03:27:17 +00:00
dup2( from_parent, 3 );
close( from_parent );
2003-07-27 07:07:45 +00:00
}
char path[1024];
GetExecutableName( path, sizeof(path) );
2003-07-27 07:07:45 +00:00
2004-09-30 12:35:13 +00:00
/* Use execve; it's the lowest-level of the exec calls. The others may allocate. */
char *argv[3] = { path, CHILD_MAGIC_PARAMETER, NULL };
char *envp[1] = { NULL };
execve( path, argv, envp );
/* If we got here, the exec failed. We can't call strerror. */
// safe_print(fileno(stderr), "Crash handler execl(", path, ") failed: ", strerror(errno), "\n", NULL);
2005-12-20 03:27:17 +00:00
safe_print( fileno(stderr), "Crash handler execl(", path, ") failed: ", itoa( errno ), "\n", NULL );
2003-07-27 07:07:45 +00:00
_exit(1);
}
2004-01-12 23:54:13 +00:00
/* write(), but retry a couple times on EINTR. */
2004-01-13 00:01:07 +00:00
static int retried_write( int fd, const void *buf, size_t count )
2004-01-12 23:54:13 +00:00
{
int tries = 3, ret;
do
{
ret = write( fd, buf, count );
}
2004-01-13 00:01:07 +00:00
while( ret == -1 && errno == EINTR && tries-- );
2004-01-12 23:54:13 +00:00
return ret;
}
2003-07-27 07:07:45 +00:00
2005-12-20 03:27:17 +00:00
static bool parent_write( int to_child, const void *p, size_t size )
2003-07-27 07:07:45 +00:00
{
2005-12-20 03:27:17 +00:00
int ret = retried_write( to_child, p, size );
2004-03-24 08:22:38 +00:00
if( ret == -1 )
2003-07-27 07:07:45 +00:00
{
2005-12-20 03:27:17 +00:00
safe_print( fileno(stderr), "Unexpected write() result (", strerror(errno), ")\n", NULL );
2004-03-24 07:22:02 +00:00
return false;
2003-07-27 07:07:45 +00:00
}
2004-03-24 07:22:02 +00:00
2005-12-20 03:27:17 +00:00
if( size_t(ret) != size )
2004-03-24 08:22:38 +00:00
{
2005-12-20 03:27:17 +00:00
safe_print( fileno(stderr), "Unexpected write() result (", itoa(ret), ")\n", NULL );
2004-03-24 08:22:38 +00:00
return false;
}
2004-03-24 07:22:02 +00:00
return true;
2003-07-27 07:07:45 +00:00
}
2004-03-20 21:36:10 +00:00
static void parent_process( int to_child, const CrashData *crash )
2003-07-27 07:07:45 +00:00
{
2004-03-20 21:36:10 +00:00
/* 1. Write the CrashData. */
2004-03-24 07:22:02 +00:00
if( !parent_write(to_child, crash, sizeof(CrashData)) )
return;
2004-03-19 06:56:18 +00:00
2004-03-20 21:36:10 +00:00
/* 2. Write info. */
2003-07-27 07:07:45 +00:00
const char *p = RageLog::GetInfo();
2005-12-20 03:27:17 +00:00
int size = strlen( p )+1;
2004-03-24 07:22:02 +00:00
if( !parent_write(to_child, &size, sizeof(size)) )
return;
if( !parent_write(to_child, p, size) )
return;
2003-07-27 07:07:45 +00:00
2004-03-20 21:36:10 +00:00
/* 3. Write AdditionalLog. */
2003-07-27 07:07:45 +00:00
p = RageLog::GetAdditionalLog();
2005-12-20 03:27:17 +00:00
size = strlen( p )+1;
2004-03-24 07:22:02 +00:00
if( !parent_write(to_child, &size, sizeof(size)) )
return;
if( !parent_write(to_child, p, size) )
return;
2003-07-27 07:07:45 +00:00
2004-03-20 21:36:10 +00:00
/* 4. Write RecentLogs. */
2003-07-27 07:07:45 +00:00
int cnt = 0;
const char *ps[1024];
while( cnt < 1024 && (ps[cnt] = RageLog::GetRecentLog( cnt )) != NULL )
++cnt;
parent_write(to_child, &cnt, sizeof(cnt));
for( int i = 0; i < cnt; ++i )
{
size = strlen(ps[i])+1;
2004-03-24 08:22:38 +00:00
if( !parent_write(to_child, &size, sizeof(size)) )
2004-03-24 07:22:02 +00:00
return;
2004-03-24 08:22:38 +00:00
if( !parent_write(to_child, ps[i], size) )
2004-03-24 07:22:02 +00:00
return;
2003-07-27 07:07:45 +00:00
}
2003-08-07 19:58:15 +00:00
2004-03-20 21:36:10 +00:00
/* 5. Write CHECKPOINTs. */
2004-10-27 19:52:51 +00:00
static char buf[1024*32];
Checkpoints::GetLogs( buf, sizeof(buf), "\n" );
2005-12-20 03:27:17 +00:00
size = strlen( buf )+1;
2004-03-24 07:22:02 +00:00
if( !parent_write(to_child, &size, sizeof(size)) )
2005-12-20 03:27:17 +00:00
return;
2004-10-27 19:52:51 +00:00
if( !parent_write(to_child, buf, size) )
2005-12-20 03:27:17 +00:00
return;
2004-03-20 21:36:10 +00:00
/* 6. Write the crashed thread's name. */
p = RageThread::GetCurThreadName();
2005-12-20 03:27:17 +00:00
size = strlen( p )+1;
2004-03-24 07:22:02 +00:00
if( !parent_write(to_child, &size, sizeof(size)) )
2005-12-20 03:27:17 +00:00
return;
2004-03-24 07:22:02 +00:00
if( !parent_write(to_child, p, size) )
2005-12-20 03:27:17 +00:00
return;
2003-07-27 07:07:45 +00:00
}
/* The parent process is the crashed process. It'll send data to the
* child, who will do stuff with it. The parent then waits for the
* child to quit, and exits.
*
* We can do whatever fancy things we want in the child process. However,
* let's not open any windows until we at least try to shut down OpenGL,
* since it may cause problems. We don't want to try to shut down OpenGL
* until we've sent all of our data, since it might explode.
*
* So, first fork off the error reporting child, send data to it, shut down
* OpenGL, close the socket and wait for the child to shut down.
*
* The child reads the data from the parent, waits for the socket to close
* (EOF), and it's then free to open windows and stuff.
*
* XXX: make sure the parent dying doesn't take out the child
*/
/* The x86 backtrace() in glibc doesn't make any effort at all to decode
* signal trampolines. The result is that it doesn't properly show the
* function that actually caused the signal--which is the most important
* one! So, we have to do it all ourself. */
2004-03-19 08:17:44 +00:00
const char *SignalName( int signo )
{
#define X(a) case a: return #a;
switch( signo )
{
case SIGALRM: return "Alarm";
case SIGBUS: return "Bus error";
case SIGFPE: return "Floating point exception";
X(SIGHUP)
case SIGILL: return "Illegal instruction";
X(SIGINT)
case SIGPIPE: return "Broken pipe";
case SIGABRT: return "Aborted";
X(SIGQUIT)
case SIGSEGV: return "Segmentation fault";
X(SIGTRAP) X(SIGTERM) X(SIGVTALRM) X(SIGXCPU) X(SIGXFSZ)
#if defined(HAVE_DECL_SIGPWR)
2004-03-19 08:17:44 +00:00
X(SIGPWR)
#endif
#if defined(HAVE_DECL_SIGUSR1)
case SIGUSR1: return "Forced crash";
2004-03-19 08:17:44 +00:00
#endif
default:
{
static char buf[128];
strcpy( buf, "Unknown signal " );
strcat( buf, itoa(signo) );
return buf;
}
}
#undef X
2004-03-19 03:13:27 +00:00
}
2004-03-19 06:56:18 +00:00
static void RunCrashHandler( const CrashData *crash )
2003-07-27 07:07:45 +00:00
{
2004-03-12 03:03:09 +00:00
if( g_pCrashHandlerArgv0 == NULL )
2004-02-20 04:11:01 +00:00
{
2005-12-20 03:27:17 +00:00
safe_print( fileno(stderr), "Crash handler failed: CrashHandlerHandleArgs was not called\n", NULL );
_exit( 1 );
2004-02-20 04:11:01 +00:00
}
2004-03-19 03:23:42 +00:00
/* Block SIGPIPE, so we get EPIPE. */
struct sigaction sa;
2004-03-19 05:03:19 +00:00
memset( &sa, 0, sizeof(sa) );
2004-03-19 03:23:42 +00:00
sa.sa_handler = SIG_IGN;
if( sigaction( SIGPIPE, &sa, NULL ) != 0 )
{
2005-12-20 03:27:17 +00:00
safe_print( fileno(stderr), "sigaction() failed: %s", strerror(errno), NULL );
2004-03-19 03:23:42 +00:00
/* non-fatal */
}
static int received = 0;
2004-09-30 12:35:13 +00:00
static int active = 0;
2003-07-27 07:07:45 +00:00
2004-09-30 12:35:13 +00:00
if( active )
2003-07-27 07:07:45 +00:00
{
/* We've received a second signal. This may mean that another thread
* crashed before we stopped it, or it may mean that the crash handler
* crashed. */
2004-03-19 08:41:46 +00:00
switch( crash->type )
{
case CrashData::SIGNAL:
safe_print( fileno(stderr), "Fatal signal (", SignalName(crash->signal), ")", NULL );
break;
2004-09-08 07:34:38 +00:00
case CrashData::FORCE_CRASH:
safe_print( fileno(stderr), "Crash handler failed: \"", crash->reason, "\"", NULL );
break;
2004-03-19 08:41:46 +00:00
default:
safe_print( fileno(stderr), "Unexpected RunCrashHandler call (", itoa(crash->type), ")", NULL );
break;
}
2004-09-30 12:35:13 +00:00
if( active == 1 )
2004-03-19 08:41:46 +00:00
safe_print( fileno(stderr), " while still in the crash handler\n", NULL);
2004-09-30 12:35:13 +00:00
else if( active == 2 )
2004-03-19 08:41:46 +00:00
safe_print( fileno(stderr), " while in the crash handler child\n", NULL);
2005-12-20 03:27:17 +00:00
_exit( 1 );
2003-07-27 07:07:45 +00:00
}
2004-09-30 12:35:13 +00:00
active = 1;
received = getpid();
2003-07-27 07:07:45 +00:00
2004-04-07 07:41:58 +00:00
/* Stop other threads. XXX: This prints a spurious ptrace error if any threads
* are already suspended, which happens in ForceCrashHandlerDeadlock(). */
2004-03-28 06:06:10 +00:00
RageThread::HaltAllThreads();
2003-07-27 07:07:45 +00:00
/* We need to be very careful, since we're under crash conditions. Let's fork
* a process and exec ourself to get a clean environment to work in. */
int fds[2];
2005-12-20 03:27:17 +00:00
if( pipe(fds) != 0 )
2003-07-27 07:07:45 +00:00
{
2005-12-20 03:27:17 +00:00
safe_print( fileno(stderr), "Crash handler pipe() failed: ", strerror(errno), "\n", NULL );
exit( 1 );
2003-07-27 07:07:45 +00:00
}
2004-09-30 12:35:13 +00:00
pid_t childpid = fork();
if( childpid == -1 )
2003-07-27 07:07:45 +00:00
{
2005-12-20 03:27:17 +00:00
safe_print( fileno(stderr), "Crash handler fork() failed: ", strerror(errno), "\n", NULL );
_exit( 1 );
2003-07-27 07:07:45 +00:00
}
if( childpid == 0 )
2003-07-27 07:07:45 +00:00
{
2004-09-30 12:35:13 +00:00
active = 2;
2005-12-20 03:27:17 +00:00
close( fds[1] );
spawn_child_process( fds[0] );
2003-07-27 07:07:45 +00:00
}
else
{
2005-12-20 03:27:17 +00:00
close( fds[0] );
2004-03-20 21:36:10 +00:00
parent_process( fds[1], crash );
2004-03-24 07:22:02 +00:00
close( fds[1] );
int status = 0;
waitpid( childpid, &status, 0 );
2004-04-07 07:40:02 +00:00
/* We need to resume threads before continuing, or we may deadlock on _exit(). */
2004-06-11 07:09:43 +00:00
/* XXX: race condition: If two threads are deadlocked on a pair of mutexes, there's
* a chance that they'll both try to lock the other's mutex at the same time. If
* that happens, then they'll both time out the lock at about the same time. One
* will trigger the deadlock crash first, and the other will be suspended. However,
* once we resume it here, it'll continue, and * trigger the deadlock crash again.
* It can result in unrecoverable deadlocks. */
2004-04-07 07:40:02 +00:00
RageThread::ResumeAllThreads();
if( WIFSIGNALED(status) )
2005-12-20 03:27:17 +00:00
safe_print( fileno(stderr), "Crash handler child exited with signal ", itoa(WTERMSIG(status)), "\n", NULL );
2003-07-27 07:07:45 +00:00
}
}
static void BacktraceAllThreads( CrashData& crash )
{
int iCnt = 1;
uint64_t iID;
for( int i = 0; RageThread::EnumThreadIDs(i, iID); ++i )
{
if( iID == GetInvalidThreadId() || iID == RageThread::GetCurrentThreadID() )
continue;
BacktraceContext ctx;
if( GetThreadBacktraceContext( iID, &ctx ) )
GetBacktrace( crash.BacktracePointers[iCnt], BACKTRACE_MAX_SIZE, &ctx );
strncpy( crash.m_ThreadName[iCnt], RageThread::GetThreadNameByID(iID), sizeof(crash.m_ThreadName[0])-1 );
++iCnt;
if( iCnt == CrashData::MAX_BACKTRACE_THREADS )
break;
}
}
2005-12-31 04:00:41 +00:00
void CrashHandler::ForceCrash( const char *reason )
2004-03-19 06:56:18 +00:00
{
CrashData crash;
2004-03-20 21:36:10 +00:00
memset( &crash, 0, sizeof(crash) );
2004-09-08 07:34:38 +00:00
crash.type = CrashData::FORCE_CRASH;
2004-03-19 06:56:18 +00:00
strncpy( crash.reason, reason, sizeof(crash.reason) );
crash.reason[ sizeof(crash.reason)-1 ] = 0;
2004-09-08 07:34:38 +00:00
GetBacktrace( crash.BacktracePointers[0], BACKTRACE_MAX_SIZE, NULL );
2004-03-20 21:36:10 +00:00
2004-03-19 06:56:18 +00:00
RunCrashHandler( &crash );
}
2005-12-31 04:00:41 +00:00
void CrashHandler::ForceDeadlock( CString reason, uint64_t iID )
2004-06-11 07:09:43 +00:00
{
2004-09-08 07:34:38 +00:00
CrashData crash;
memset( &crash, 0, sizeof(crash) );
crash.type = CrashData::FORCE_CRASH;
GetBacktrace( crash.BacktracePointers[0], BACKTRACE_MAX_SIZE, NULL );
if( iID == GetInvalidThreadId() )
{
/* Backtrace all threads. */
BacktraceAllThreads( crash );
}
2004-09-07 11:12:29 +00:00
else
{
BacktraceContext ctx;
if( !GetThreadBacktraceContext( iID, &ctx ) )
reason += "; GetThreadBacktraceContext failed";
else
GetBacktrace( crash.BacktracePointers[1], BACKTRACE_MAX_SIZE, &ctx );
strncpy( crash.m_ThreadName[1], RageThread::GetThreadNameByID(iID), sizeof(crash.m_ThreadName[0])-1 );
}
2004-09-08 07:34:38 +00:00
strncpy( crash.m_ThreadName[0], RageThread::GetCurThreadName(), sizeof(crash.m_ThreadName[0])-1 );
strncpy( crash.reason, reason, min(sizeof(crash.reason) - 1, reason.length()) );
crash.reason[ sizeof(crash.reason)-1 ] = 0;
RunCrashHandler( &crash );
2004-06-11 07:09:43 +00:00
2005-12-20 03:27:17 +00:00
_exit( 1 );
2004-06-11 07:09:43 +00:00
}
2004-03-20 21:36:10 +00:00
/* XXX test for recursive crashes here (eg. GetBacktrace crashing) */
2005-12-31 04:00:41 +00:00
void CrashHandler::CrashSignalHandler( int signal, siginfo_t *si, const ucontext_t *uc )
2004-03-19 06:56:18 +00:00
{
CrashData crash;
2004-03-20 21:36:10 +00:00
memset( &crash, 0, sizeof(crash) );
2004-03-19 06:56:18 +00:00
crash.type = CrashData::SIGNAL;
crash.signal = signal;
crash.si = *si;
2004-03-20 21:36:10 +00:00
BacktraceContext ctx;
GetSignalBacktraceContext( &ctx, uc );
2004-09-08 07:34:38 +00:00
GetBacktrace( crash.BacktracePointers[0], BACKTRACE_MAX_SIZE, &ctx );
#if defined(HAVE_DECL_SIGUSR1)
if( signal == SIGUSR1 )
BacktraceAllThreads( crash );
#endif
2004-09-08 07:34:38 +00:00
strncpy( crash.m_ThreadName[0], RageThread::GetCurThreadName(), sizeof(crash.m_ThreadName[0])-1 );
2004-03-20 21:36:10 +00:00
2004-03-19 06:56:18 +00:00
RunCrashHandler( &crash );
}
2005-12-31 04:00:41 +00:00
void CrashHandler::InitializeCrashHandler()
2003-11-11 22:21:33 +00:00
{
2004-03-12 05:15:32 +00:00
InitializeBacktrace();
2003-11-11 22:21:33 +00:00
}
2003-07-27 07:07:45 +00:00
2004-05-15 08:23:53 +00:00
/*
* (c) 2003-2004 Glenn Maynard
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/