1 /*===- InstrProfilingUtil.c - Support library for PGO instrumentation -----===*\
3 |* The LLVM Compiler Infrastructure
5 |* This file is distributed under the University of Illinois Open Source
6 |* License. See LICENSE.TXT for details.
8 \*===----------------------------------------------------------------------===*/
10 #include "InstrProfilingUtil.h"
11 #include "InstrProfiling.h"
19 #include <sys/types.h>
25 #ifdef COMPILER_RT_HAS_UNAME
26 #include <sys/utsname.h>
32 #if defined(__linux__)
34 #include <sys/prctl.h>
37 COMPILER_RT_VISIBILITY
38 void __llvm_profile_recursive_mkdir(char *path) {
41 for (i = 1; path[i] != '\0'; ++i) {
43 if (!IS_DIR_SEPARATOR(path[i]))
49 mkdir(path, 0755); /* Some of these will fail, ignore it. */
55 #if COMPILER_RT_HAS_ATOMICS != 1
56 COMPILER_RT_VISIBILITY
57 uint32_t lprofBoolCmpXchg(void **Ptr, void *OldV, void *NewV) {
65 COMPILER_RT_VISIBILITY
66 void *lprofPtrFetchAdd(void **Mem, long ByteIncr) {
68 *((char **)Mem) += ByteIncr;
75 COMPILER_RT_VISIBILITY int lprofGetHostName(char *Name, int Len) {
76 WCHAR Buffer[COMPILER_RT_MAX_HOSTLEN];
77 DWORD BufferSize = sizeof(Buffer);
79 GetComputerNameExW(ComputerNameDnsFullyQualified, Buffer, &BufferSize);
82 if (WideCharToMultiByte(CP_UTF8, 0, Buffer, -1, Name, Len, NULL, NULL) == 0)
86 #elif defined(COMPILER_RT_HAS_UNAME)
87 COMPILER_RT_VISIBILITY int lprofGetHostName(char *Name, int Len) {
91 strncpy(Name, N.nodename, Len);
96 COMPILER_RT_VISIBILITY FILE *lprofOpenFileEx(const char *ProfileName) {
99 #ifdef COMPILER_RT_HAS_FCNTL_LCK
100 struct flock s_flock;
102 s_flock.l_whence = SEEK_SET;
104 s_flock.l_len = 0; /* Until EOF. */
105 s_flock.l_pid = getpid();
107 s_flock.l_type = F_WRLCK;
108 fd = open(ProfileName, O_RDWR | O_CREAT, 0666);
112 while (fcntl(fd, F_SETLKW, &s_flock) == -1) {
113 if (errno != EINTR) {
114 if (errno == ENOLCK) {
115 PROF_WARN("Data may be corrupted during profile merging : %s\n",
116 "Fail to obtain file lock due to system limit.");
122 f = fdopen(fd, "r+b");
123 #elif defined(_WIN32)
124 // FIXME: Use the wide variants to handle Unicode filenames.
125 HANDLE h = CreateFileA(ProfileName, GENERIC_READ | GENERIC_WRITE, 0, 0,
126 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
127 if (h == INVALID_HANDLE_VALUE)
130 fd = _open_osfhandle((intptr_t)h, 0);
136 f = _fdopen(fd, "r+b");
142 /* Worst case no locking applied. */
143 PROF_WARN("Concurrent file access is not supported : %s\n",
144 "lack file locking");
145 fd = open(ProfileName, O_RDWR | O_CREAT, 0666);
148 f = fdopen(fd, "r+b");
154 COMPILER_RT_VISIBILITY const char *lprofGetPathPrefix(int *PrefixStrip,
156 const char *Prefix = getenv("GCOV_PREFIX");
157 const char *PrefixStripStr = getenv("GCOV_PREFIX_STRIP");
161 if (Prefix == NULL || Prefix[0] == '\0')
164 if (PrefixStripStr) {
165 *PrefixStrip = atoi(PrefixStripStr);
167 /* Negative GCOV_PREFIX_STRIP values are ignored */
168 if (*PrefixStrip < 0)
173 *PrefixLen = strlen(Prefix);
178 COMPILER_RT_VISIBILITY void
179 lprofApplyPathPrefix(char *Dest, const char *PathStr, const char *Prefix,
180 size_t PrefixLen, int PrefixStrip) {
184 const char *StrippedPathStr = PathStr;
186 for (Level = 0, Ptr = PathStr + 1; Level < PrefixStrip; ++Ptr) {
190 if (!IS_DIR_SEPARATOR(*Ptr))
193 StrippedPathStr = Ptr;
197 memcpy(Dest, Prefix, PrefixLen);
199 if (!IS_DIR_SEPARATOR(Prefix[PrefixLen - 1]))
200 Dest[PrefixLen++] = DIR_SEPARATOR;
202 memcpy(Dest + PrefixLen, StrippedPathStr, strlen(StrippedPathStr) + 1);
205 COMPILER_RT_VISIBILITY const char *
206 lprofFindFirstDirSeparator(const char *Path) {
208 Sep = strchr(Path, DIR_SEPARATOR);
211 #if defined(DIR_SEPARATOR_2)
212 Sep = strchr(Path, DIR_SEPARATOR_2);
217 COMPILER_RT_VISIBILITY const char *lprofFindLastDirSeparator(const char *Path) {
219 Sep = strrchr(Path, DIR_SEPARATOR);
222 #if defined(DIR_SEPARATOR_2)
223 Sep = strrchr(Path, DIR_SEPARATOR_2);
228 COMPILER_RT_VISIBILITY int lprofSuspendSigKill() {
229 #if defined(__linux__)
231 /* Temporarily suspend getting SIGKILL upon exit of the parent process. */
232 if (prctl(PR_GET_PDEATHSIG, &PDeachSig) == 0 && PDeachSig == SIGKILL)
233 prctl(PR_SET_PDEATHSIG, 0);
234 return (PDeachSig == SIGKILL);
240 COMPILER_RT_VISIBILITY void lprofRestoreSigKill() {
241 #if defined(__linux__)
242 prctl(PR_SET_PDEATHSIG, SIGKILL);