X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=common%2Fos_posix.cpp;h=7d39d8ada5e0ffafc0f730bcdd3e913744f734d2;hb=56ad11c7849c7e6ca0ad66558cb1a99c58d4cd3d;hp=7dc2bb46a0d54400aee1a297e1064d10a01043f0;hpb=447b3d5c7402b0a9e780a26dfed76376e723ac94;p=apitrace diff --git a/common/os_posix.cpp b/common/os_posix.cpp index 7dc2bb4..7d39d8a 100644 --- a/common/os_posix.cpp +++ b/common/os_posix.cpp @@ -30,9 +30,7 @@ #include #include -#include #include -#include #include #include #include @@ -46,6 +44,10 @@ #include #endif +#ifdef ANDROID +#include +#endif + #ifndef PATH_MAX #warning PATH_MAX undefined #define PATH_MAX 4096 @@ -58,24 +60,6 @@ namespace os { -static pthread_mutex_t -mutex = PTHREAD_MUTEX_INITIALIZER; - - -void -acquireMutex(void) -{ - pthread_mutex_lock(&mutex); -} - - -void -releaseMutex(void) -{ - pthread_mutex_unlock(&mutex); -} - - String getProcessName(void) { @@ -159,28 +143,41 @@ int execute(char * const * args) return -1; } int status = -1; + int ret; waitpid(pid, &status, 0); - return status; + if (WIFEXITED(status)) { + ret = WEXITSTATUS(status); + } else if (WIFSIGNALED(status)) { + // match shell return code + ret = WTERMSIG(status) + 128; + } else { + ret = 128; + } + return ret; } } +static volatile bool logging = false; + void log(const char *format, ...) { + logging = true; va_list ap; va_start(ap, format); fflush(stdout); +#ifdef ANDROID + __android_log_vprint(ANDROID_LOG_DEBUG, "apitrace", format, ap); +#else vfprintf(stderr, format, ap); +#endif va_end(ap); + logging = false; } -long long -getTime(void) -{ - struct timeval tv; - gettimeofday(&tv, NULL); - return tv.tv_usec + tv.tv_sec*1000000LL; -} +#if defined(__APPLE__) +long long timeFrequency = 0LL; +#endif void abort(void) @@ -204,12 +201,22 @@ struct sigaction old_actions[NUM_SIGNALS]; static void signalHandler(int sig, siginfo_t *info, void *context) { + /* + * There are several signals that can happen when logging to stdout/stderr. + * For example, SIGPIPE will be emitted if stderr is a pipe with no + * readers. Therefore ignore any signal while logging by returning + * immediately, to prevent deadlocks. + */ + if (logging) { + return; + } + static int recursion_count = 0; - fprintf(stderr, "apitrace: warning: caught signal %i\n", sig); + log("apitrace: warning: caught signal %i\n", sig); if (recursion_count) { - fprintf(stderr, "apitrace: warning: recursion handling signal %i\n", sig); + log("apitrace: warning: recursion handling signal %i\n", sig); } else { if (gCallback) { ++recursion_count; @@ -221,7 +228,7 @@ signalHandler(int sig, siginfo_t *info, void *context) struct sigaction *old_action; if (sig >= NUM_SIGNALS) { /* This should never happen */ - fprintf(stderr, "error: unexpected signal %i\n", sig); + log("error: unexpected signal %i\n", sig); raise(SIGKILL); } old_action = &old_actions[sig]; @@ -231,7 +238,7 @@ signalHandler(int sig, siginfo_t *info, void *context) old_action->sa_sigaction(sig, info, context); } else { if (old_action->sa_handler == SIG_DFL) { - fprintf(stderr, "apitrace: info: taking default action for signal %i\n", sig); + log("apitrace: info: taking default action for signal %i\n", sig); #if 1 struct sigaction dfl_action; @@ -267,11 +274,26 @@ setExceptionCallback(void (*callback)(void)) for (int sig = 1; sig < NUM_SIGNALS; ++sig) { - // SIGKILL and SIGSTOP can't be handled - if (sig != SIGKILL && sig != SIGSTOP) { - if (sigaction(sig, NULL, &old_actions[sig]) >= 0) { - sigaction(sig, &new_action, NULL); - } + // SIGKILL and SIGSTOP can't be handled. + if (sig == SIGKILL || sig == SIGSTOP) { + continue; + } + + /* + * SIGPIPE can be emitted when writing to stderr that is redirected + * to a pipe without readers. It is also very unlikely to ocurr + * inside graphics APIs, and most applications where it can occur + * normally already ignore it. In summary, it is unlikely that a + * SIGPIPE will cause abnormal termination, which it is likely that + * intercepting here will cause problems, so simple don't intercept + * it here. + */ + if (sig == SIGPIPE) { + continue; + } + + if (sigaction(sig, NULL, &old_actions[sig]) >= 0) { + sigaction(sig, &new_action, NULL); } } }