Index: lib/profile/InstrProfilingFile.c =================================================================== --- lib/profile/InstrProfilingFile.c +++ lib/profile/InstrProfilingFile.c @@ -524,6 +524,7 @@ int rc, Length; const char *Filename; char *FilenameBuf; + int PDeathSig = 0; if (lprofProfileDumped()) { PROF_NOTE("Profile data not written to file: %s.\n", @@ -550,10 +551,18 @@ return -1; } + // Temporarily suspend getting SIGKILL when the parent exits. + PDeathSig = lprofSuspendSigKill(); + /* Write profile data to the file. */ rc = writeFile(Filename); if (rc) PROF_ERR("Failed to write file \"%s\": %s\n", Filename, strerror(errno)); + + // Restore SIGKILL. + if (PDeathSig == 1) + lprofRestoreSigKill(); + return rc; } Index: lib/profile/InstrProfilingUtil.h =================================================================== --- lib/profile/InstrProfilingUtil.h +++ lib/profile/InstrProfilingUtil.h @@ -51,4 +51,12 @@ unsigned lprofBoolCmpXchg(void **Ptr, void *OldV, void *NewV); void *lprofPtrFetchAdd(void **Mem, long ByteIncr); +/* Temporarily suspend SIGKILL. Return value of 1 means a restore is needed. + * Other return values mean no restore is needed. + */ +int lprofSuspendSigKill(); + +/* Restore previously suspended SIGKILL. */ +void lprofRestoreSigKill(); + #endif /* PROFILE_INSTRPROFILINGUTIL_H */ Index: lib/profile/InstrProfilingUtil.c =================================================================== --- lib/profile/InstrProfilingUtil.c +++ lib/profile/InstrProfilingUtil.c @@ -29,6 +29,11 @@ #include #include +#if defined(__linux__) +#include +#include +#endif + COMPILER_RT_VISIBILITY void __llvm_profile_recursive_mkdir(char *path) { int i; @@ -219,3 +224,24 @@ #endif return Sep; } + +COMPILER_RT_VISIBILITY int lprofSuspendSigKill() { +#if defined(__linux__) + int PDeachSig = 0; + /* Temporarily suspend getting SIGKILL upon exit of the parent process. */ + if (prctl(PR_GET_PDEATHSIG, &PDeachSig) == 0 && PDeachSig == SIGKILL) { + fprintf(stderr, "set\n"); + prctl(PR_SET_PDEATHSIG, 0); + } + return (PDeachSig == SIGKILL); +#else + return 0; +#endif +} + +COMPILER_RT_VISIBILITY void lprofRestoreSigKill() { +#if defined(__linux__) + fprintf(stderr, "restore \n"); + prctl(PR_SET_PDEATHSIG, SIGKILL); +#endif +} Index: test/profile/Linux/prctl.c =================================================================== --- /dev/null +++ test/profile/Linux/prctl.c @@ -0,0 +1,36 @@ +// RUN: %clang_pgogen -O2 -o %t %s +// RUN: rm -rf default_*.profraw +// RUN: %run %t && sleep 5 +// RUN: llvm-profdata show default_*.profraw 2>&1 | FileCheck %s + +#include +#include +#include +#include +#include + +#define FAKE_COUNT_SZ 10000000 +/* fake counts to increse the profile size. */ +unsigned long long __attribute__((section("__llvm_prf_cnts"))) +counts[FAKE_COUNT_SZ]; + +int main(int argc, char **argv) { + pid_t pid = fork(); + if (pid == 0) { + int i; + int sum = 0; + /* child process: sleep 500us and get to runtime before the + * main process exits. */ + prctl(PR_SET_PDEATHSIG, SIGKILL); + usleep(500); + for (i = 0; i < 5000; ++i) + sum += i * i * i; + printf("child process (%d): sum=%d\n", getpid(), sum); + } else if (pid > 0) { + /* parent process: sleep 100us to get into profile runtime first. */ + usleep(100); + } + return 0; +} + +// CHECK-NOT: Empty raw profile file