diff --git a/compiler-rt/lib/profile/InstrProfilingFile.c b/compiler-rt/lib/profile/InstrProfilingFile.c index 55e11ef3e2db0..fa4328ec9da02 100644 --- a/compiler-rt/lib/profile/InstrProfilingFile.c +++ b/compiler-rt/lib/profile/InstrProfilingFile.c @@ -367,16 +367,9 @@ static void exitSignalHandler(int sig) { static void installExitSignalHandlers(void) { unsigned I; - struct sigaction sigact; - int err; for (I = 0; I < lprofCurFilename.NumExitSignals; ++I) { - memset(&sigact, 0, sizeof(sigact)); - sigact.sa_handler = exitSignalHandler; - err = sigaction(lprofCurFilename.ExitOnSignals[I], &sigact, NULL); - if (err) - PROF_WARN( - "Unable to install an exit signal handler for %d (errno = %d).\n", - lprofCurFilename.ExitOnSignals[I], err); + lprofInstallSignalHandler(lprofCurFilename.ExitOnSignals[I], + exitSignalHandler); } } diff --git a/compiler-rt/lib/profile/InstrProfilingUtil.c b/compiler-rt/lib/profile/InstrProfilingUtil.c index 13301f341fc5a..9a8607c66c110 100644 --- a/compiler-rt/lib/profile/InstrProfilingUtil.c +++ b/compiler-rt/lib/profile/InstrProfilingUtil.c @@ -24,6 +24,7 @@ #include #endif +#include #include #include @@ -308,6 +309,24 @@ COMPILER_RT_VISIBILITY const char *lprofFindLastDirSeparator(const char *Path) { return Sep; } +COMPILER_RT_VISIBILITY void lprofInstallSignalHandler(int sig, + void (*handler)(int)) { +#ifdef _WIN32 + void (*err)(int) = signal(sig, handler); + if (err == SIG_ERR) + PROF_WARN("Unable to install an exit signal handler for %d (errno = %d).\n", + sig, errno); +#else + struct sigaction sigact; + memset(&sigact, 0, sizeof(sigact)); + sigact.sa_handler = handler; + int err = sigaction(sig, &sigact, NULL); + if (err) + PROF_WARN("Unable to install an exit signal handler for %d (errno = %d).\n", + sig, err); +#endif +} + COMPILER_RT_VISIBILITY int lprofSuspendSigKill() { #if defined(__linux__) int PDeachSig = 0; diff --git a/compiler-rt/lib/profile/InstrProfilingUtil.h b/compiler-rt/lib/profile/InstrProfilingUtil.h index efba94ca76396..6f23a98695027 100644 --- a/compiler-rt/lib/profile/InstrProfilingUtil.h +++ b/compiler-rt/lib/profile/InstrProfilingUtil.h @@ -61,6 +61,8 @@ int lprofGetHostName(char *Name, int Len); unsigned lprofBoolCmpXchg(void **Ptr, void *OldV, void *NewV); void *lprofPtrFetchAdd(void **Mem, long ByteIncr); +void lprofInstallSignalHandler(int sig, void(*handler)(int)); + /* Temporarily suspend SIGKILL. Return value of 1 means a restore is needed. * Other return values mean no restore is needed. */