Index: lib/profile/InstrProfilingFile.c =================================================================== --- lib/profile/InstrProfilingFile.c +++ lib/profile/InstrProfilingFile.c @@ -549,6 +549,8 @@ parseAndSetFilename(FilenamePat, PNS_runtime_api, 1); } +// int __sanitizer_print_stack_trace(void); + /* The public API for writing profile data into the file with name * set by previous calls to __llvm_profile_set_filename or * __llvm_profile_override_default_filename or @@ -559,8 +561,9 @@ const char *Filename; char *FilenameBuf; int PDeathSig = 0; - +// __sanitizer_print_stack_trace(); if (lprofProfileDumped()) { + myLog("WRITE FILE CALLED FROM pid: %lu\tfilename: %s\n", getpid(), "CALLED TWICE, BAIL OUT!"); PROF_NOTE("Profile data not written to file: %s.\n", "already written"); return 0; @@ -569,7 +572,7 @@ Length = getCurFilenameLength(); FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1); Filename = getCurFilename(FilenameBuf); - + myLog("WRITE FILE CALLED FROM pid: %lu\tfilename: %s\n", getpid(), Filename); /* Check the filename. */ if (!Filename) { PROF_ERR("Failed to write file : %s\n", "Filename not set"); @@ -588,6 +591,20 @@ // Temporarily suspend getting SIGKILL when the parent exits. PDeathSig = lprofSuspendSigKill(); + // Let's inspect counts in memory before writing the file. + __llvm_profile_data *DataBegin = (__llvm_profile_data *)__llvm_profile_begin_data(); + __llvm_profile_data *DataEnd = (__llvm_profile_data *)__llvm_profile_end_data(); + for (__llvm_profile_data *DataPtr = DataBegin; DataPtr < DataEnd; ++DataPtr) { + if (DataPtr->NameRef == 0x321408bd58d4c346 || DataPtr->NameRef == 0xf96b1fa6f8205ca0) { + uint64_t *Counters = (uint64_t *)DataPtr->CounterPtr; + unsigned NC = DataPtr->NumCounters; + for (unsigned I = 0; I < NC; ++I) { + myLog2("Index %lu of %lu\n", I, NC); + myLog3("In-Memory Counter [pid: %lu] @ %llx holding Value: %lu\n", getpid(), &Counters[I], Counters[I]); + } + } + } + /* Write profile data to the file. */ rc = writeFile(Filename); if (rc) @@ -602,6 +619,7 @@ COMPILER_RT_VISIBILITY int __llvm_profile_dump(void) { + myLog2("DUMP FILE CALLED FROM pid: %lu\tisProfileDumped: %lu\n", getpid(), lprofProfileDumped()); if (!doMerging()) PROF_WARN("Later invocation of __llvm_profile_dump can lead to clobbering " " of previously dumped profile data : %s. Either use %%m " Index: lib/profile/InstrProfilingMerge.c =================================================================== --- lib/profile/InstrProfilingMerge.c +++ lib/profile/InstrProfilingMerge.c @@ -69,6 +69,8 @@ Header->NamesSize + Header->CountersSize) return 1; + uint64_t *SrcCountersStart = (uint64_t*)SrcDataEnd; + for (SrcData = SrcDataStart, DstData = (__llvm_profile_data *)__llvm_profile_begin_data(); SrcData < SrcDataEnd; ++SrcData, ++DstData) { @@ -76,6 +78,26 @@ SrcData->FuncHash != DstData->FuncHash || SrcData->NumCounters != DstData->NumCounters) return 1; + // Let's inspect counts in file before merging. +// __llvm_profile_data *DataBegin = (__llvm_profile_data *)__llvm_profile_begin_data(); +// __llvm_profile_data *DataEnd = (__llvm_profile_data *)__llvm_profile_end_data(); +// for (__llvm_profile_data *DataPtr = DataBegin; DataPtr < DataEnd; ++DataPtr) { + if (SrcData->NameRef == 0x321408bd58d4c346 || SrcData->NameRef == 0xf96b1fa6f8205ca0) { + // uint64_t *Counters = (uint64_t *)SrcData->CounterPtr; + uint64_t *Counters = SrcCountersStart + + ((size_t)SrcData->CounterPtr - Header->CountersDelta) / + sizeof(uint64_t); + myLog3("In-File SrcCountersStart: %llx, SrcData->CounterPtr: %llx, Header->CountersDelta: %llx\n", + SrcCountersStart, SrcData->CounterPtr, Header->CountersDelta); + myLog2("In-File Calculation [pid: %lu], Counters: %llx\n", getpid(), Counters); + myLog3("In-Sanity-File SrcData: %llx, DstData: %llx, SrcData - SrcDataStart: %lld\n", + SrcData, DstData, SrcData - SrcDataStart); + unsigned NC = SrcData->NumCounters; + for (unsigned I = 0; I < NC; ++I) { + myLog3("In-File Counter [pid: %lu] @ %llx holding Value: %lu\n", getpid(), &Counters[I], Counters[I]); + } + } +// } } /* Matched! */ Index: lib/profile/InstrProfilingUtil.h =================================================================== --- lib/profile/InstrProfilingUtil.h +++ lib/profile/InstrProfilingUtil.h @@ -62,4 +62,12 @@ /* Restore previously suspended SIGKILL. */ void lprofRestoreSigKill(); +int myLog(const char *format, const uint64_t pid, const char* name); + +int myLog2(const char *format, uint64_t pid, uint64_t name); + +int myLog3(const char *format, uint64_t pid, uint64_t name, uint64_t value); + +int myLog4(const char *format, const uint64_t pid, const uint64_t sign, const char* name); + #endif /* PROFILE_INSTRPROFILINGUTIL_H */ Index: lib/profile/InstrProfilingUtil.c =================================================================== --- lib/profile/InstrProfilingUtil.c +++ lib/profile/InstrProfilingUtil.c @@ -268,9 +268,14 @@ #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) + if (prctl(PR_GET_PDEATHSIG, &PDeachSig) == 0 && (PDeachSig == SIGKILL || PDeachSig == SIGTERM)) prctl(PR_SET_PDEATHSIG, 0); - return (PDeachSig == SIGKILL); + if (PDeachSig == SIGKILL) + myLog("GOT SIGNAL IN pid: %lu\t SIGNAL: %s\n", getpid(), "SIGKILL"); + else if (PDeachSig == SIGTERM) + myLog("GOT SIGNAL IN pid: %lu\t SIGNAL: %s\n", getpid(), "SIGTERM"); + + return (PDeachSig == SIGKILL || PDeachSig == SIGTERM); #else return 0; #endif @@ -281,3 +286,55 @@ prctl(PR_SET_PDEATHSIG, SIGKILL); #endif } + +COMPILER_RT_VISIBILITY int myLog(const char *format, const uint64_t pid, const char* name) { + // va_list args; + // va_start(args, format); + + FILE *f = lprofOpenFileEx("mylog.txt"); + if (!f) + return 123 / ((uint64_t)format - (uint64_t)format); + + fseek(f, 0, SEEK_END); + + fprintf(f, format, pid, name); +// va_end(args); + + return fclose(f); +} + +COMPILER_RT_VISIBILITY int myLog2(const char *format, uint64_t pid, uint64_t name) { + FILE *f = lprofOpenFileEx("mylog.txt"); + if (!f) + return 123 / ((uint64_t)format - (uint64_t)format); + + fseek(f, 0, SEEK_END); + + fprintf(f, format, pid, name); + + return fclose(f); +} + +COMPILER_RT_VISIBILITY int myLog3(const char *format, uint64_t pid, uint64_t name, uint64_t value) { + FILE *f = lprofOpenFileEx("mylog.txt"); + if (!f) + return 123 / ((uint64_t)format - (uint64_t)format); + + fseek(f, 0, SEEK_END); + + fprintf(f, format, pid, name, value); + + return fclose(f); +} + +COMPILER_RT_VISIBILITY int myLog4(const char *format, const uint64_t pid, const uint64_t sign, const char* name) { + FILE *f = lprofOpenFileEx("mylog.txt"); + if (!f) + return 123 / ((uint64_t)format - (uint64_t)format); + + fseek(f, 0, SEEK_END); + + fprintf(f, format, pid, sign, name); + + return fclose(f); +}