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 \*===----------------------------------------------------------------------===*/
14 #include "WindowsMMap.h"
17 #include <sys/types.h>
23 #ifdef COMPILER_RT_HAS_UNAME
24 #include <sys/utsname.h>
30 #if defined(__linux__)
32 #include <sys/prctl.h>
35 #include "InstrProfiling.h"
36 #include "InstrProfilingUtil.h"
38 COMPILER_RT_WEAK unsigned lprofDirMode = 0755;
40 COMPILER_RT_VISIBILITY
41 void __llvm_profile_recursive_mkdir(char *path) {
44 for (i = 1; path[i] != '\0'; ++i) {
46 if (!IS_DIR_SEPARATOR(path[i]))
52 /* Some of these will fail, ignore it. */
53 mkdir(path, __llvm_profile_get_dir_mode());
59 COMPILER_RT_VISIBILITY
60 void __llvm_profile_set_dir_mode(unsigned Mode) { lprofDirMode = Mode; }
62 COMPILER_RT_VISIBILITY
63 unsigned __llvm_profile_get_dir_mode(void) { return lprofDirMode; }
65 #if COMPILER_RT_HAS_ATOMICS != 1
66 COMPILER_RT_VISIBILITY
67 uint32_t lprofBoolCmpXchg(void **Ptr, void *OldV, void *NewV) {
75 COMPILER_RT_VISIBILITY
76 void *lprofPtrFetchAdd(void **Mem, long ByteIncr) {
78 *((char **)Mem) += ByteIncr;
85 COMPILER_RT_VISIBILITY int lprofGetHostName(char *Name, int Len) {
86 WCHAR Buffer[COMPILER_RT_MAX_HOSTLEN];
87 DWORD BufferSize = sizeof(Buffer);
89 GetComputerNameExW(ComputerNameDnsFullyQualified, Buffer, &BufferSize);
92 if (WideCharToMultiByte(CP_UTF8, 0, Buffer, -1, Name, Len, NULL, NULL) == 0)
96 #elif defined(COMPILER_RT_HAS_UNAME)
97 COMPILER_RT_VISIBILITY int lprofGetHostName(char *Name, int Len) {
101 strncpy(Name, N.nodename, Len);
108 COMPILER_RT_VISIBILITY int lprofLockFd(int fd) {
109 #ifdef COMPILER_RT_HAS_FCNTL_LCK
110 struct flock s_flock;
112 s_flock.l_whence = SEEK_SET;
114 s_flock.l_len = 0; /* Until EOF. */
115 s_flock.l_pid = getpid();
116 s_flock.l_type = F_WRLCK;
118 while (fcntl(fd, F_SETLKW, &s_flock) == -1) {
119 if (errno != EINTR) {
120 if (errno == ENOLCK) {
133 COMPILER_RT_VISIBILITY int lprofUnlockFd(int fd) {
134 #ifdef COMPILER_RT_HAS_FCNTL_LCK
135 struct flock s_flock;
137 s_flock.l_whence = SEEK_SET;
139 s_flock.l_len = 0; /* Until EOF. */
140 s_flock.l_pid = getpid();
141 s_flock.l_type = F_UNLCK;
143 while (fcntl(fd, F_SETLKW, &s_flock) == -1) {
144 if (errno != EINTR) {
145 if (errno == ENOLCK) {
158 COMPILER_RT_VISIBILITY FILE *lprofOpenFileEx(const char *ProfileName) {
161 #ifdef COMPILER_RT_HAS_FCNTL_LCK
162 fd = open(ProfileName, O_RDWR | O_CREAT, 0666);
166 if (lprofLockFd(fd) != 0)
167 PROF_WARN("Data may be corrupted during profile merging : %s\n",
168 "Fail to obtain file lock due to system limit.");
170 f = fdopen(fd, "r+b");
171 #elif defined(_WIN32)
172 // FIXME: Use the wide variants to handle Unicode filenames.
173 HANDLE h = CreateFileA(ProfileName, GENERIC_READ | GENERIC_WRITE, 0, 0,
174 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
175 if (h == INVALID_HANDLE_VALUE)
178 fd = _open_osfhandle((intptr_t)h, 0);
184 f = _fdopen(fd, "r+b");
190 /* Worst case no locking applied. */
191 PROF_WARN("Concurrent file access is not supported : %s\n",
192 "lack file locking");
193 fd = open(ProfileName, O_RDWR | O_CREAT, 0666);
196 f = fdopen(fd, "r+b");
202 COMPILER_RT_VISIBILITY const char *lprofGetPathPrefix(int *PrefixStrip,
204 const char *Prefix = getenv("GCOV_PREFIX");
205 const char *PrefixStripStr = getenv("GCOV_PREFIX_STRIP");
209 if (Prefix == NULL || Prefix[0] == '\0')
212 if (PrefixStripStr) {
213 *PrefixStrip = atoi(PrefixStripStr);
215 /* Negative GCOV_PREFIX_STRIP values are ignored */
216 if (*PrefixStrip < 0)
221 *PrefixLen = strlen(Prefix);
226 COMPILER_RT_VISIBILITY void
227 lprofApplyPathPrefix(char *Dest, const char *PathStr, const char *Prefix,
228 size_t PrefixLen, int PrefixStrip) {
232 const char *StrippedPathStr = PathStr;
234 for (Level = 0, Ptr = PathStr + 1; Level < PrefixStrip; ++Ptr) {
238 if (!IS_DIR_SEPARATOR(*Ptr))
241 StrippedPathStr = Ptr;
245 memcpy(Dest, Prefix, PrefixLen);
247 if (!IS_DIR_SEPARATOR(Prefix[PrefixLen - 1]))
248 Dest[PrefixLen++] = DIR_SEPARATOR;
250 memcpy(Dest + PrefixLen, StrippedPathStr, strlen(StrippedPathStr) + 1);
253 COMPILER_RT_VISIBILITY const char *
254 lprofFindFirstDirSeparator(const char *Path) {
255 const char *Sep = strchr(Path, DIR_SEPARATOR);
256 #if defined(DIR_SEPARATOR_2)
257 const char *Sep2 = strchr(Path, DIR_SEPARATOR_2);
258 if (Sep2 && (!Sep || Sep2 < Sep))
264 COMPILER_RT_VISIBILITY const char *lprofFindLastDirSeparator(const char *Path) {
265 const char *Sep = strrchr(Path, DIR_SEPARATOR);
266 #if defined(DIR_SEPARATOR_2)
267 const char *Sep2 = strrchr(Path, DIR_SEPARATOR_2);
268 if (Sep2 && (!Sep || Sep2 > Sep))
274 COMPILER_RT_VISIBILITY int lprofSuspendSigKill() {
275 #if defined(__linux__)
277 /* Temporarily suspend getting SIGKILL upon exit of the parent process. */
278 if (prctl(PR_GET_PDEATHSIG, &PDeachSig) == 0 && PDeachSig == SIGKILL)
279 prctl(PR_SET_PDEATHSIG, 0);
280 return (PDeachSig == SIGKILL);
286 COMPILER_RT_VISIBILITY void lprofRestoreSigKill() {
287 #if defined(__linux__)
288 prctl(PR_SET_PDEATHSIG, SIGKILL);