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_VISIBILITY
39 void __llvm_profile_recursive_mkdir(char *path) {
42 for (i = 1; path[i] != '\0'; ++i) {
44 if (!IS_DIR_SEPARATOR(path[i]))
50 mkdir(path, 0755); /* Some of these will fail, ignore it. */
56 #if COMPILER_RT_HAS_ATOMICS != 1
57 COMPILER_RT_VISIBILITY
58 uint32_t lprofBoolCmpXchg(void **Ptr, void *OldV, void *NewV) {
66 COMPILER_RT_VISIBILITY
67 void *lprofPtrFetchAdd(void **Mem, long ByteIncr) {
69 *((char **)Mem) += ByteIncr;
76 COMPILER_RT_VISIBILITY int lprofGetHostName(char *Name, int Len) {
77 WCHAR Buffer[COMPILER_RT_MAX_HOSTLEN];
78 DWORD BufferSize = sizeof(Buffer);
80 GetComputerNameExW(ComputerNameDnsFullyQualified, Buffer, &BufferSize);
83 if (WideCharToMultiByte(CP_UTF8, 0, Buffer, -1, Name, Len, NULL, NULL) == 0)
87 #elif defined(COMPILER_RT_HAS_UNAME)
88 COMPILER_RT_VISIBILITY int lprofGetHostName(char *Name, int Len) {
92 strncpy(Name, N.nodename, Len);
99 COMPILER_RT_VISIBILITY int lprofLockFd(int fd) {
100 #ifdef COMPILER_RT_HAS_FCNTL_LCK
101 struct flock s_flock;
103 s_flock.l_whence = SEEK_SET;
105 s_flock.l_len = 0; /* Until EOF. */
106 s_flock.l_pid = getpid();
107 s_flock.l_type = F_WRLCK;
109 while (fcntl(fd, F_SETLKW, &s_flock) == -1) {
110 if (errno != EINTR) {
111 if (errno == ENOLCK) {
124 COMPILER_RT_VISIBILITY int lprofUnlockFd(int fd) {
125 #ifdef COMPILER_RT_HAS_FCNTL_LCK
126 struct flock s_flock;
128 s_flock.l_whence = SEEK_SET;
130 s_flock.l_len = 0; /* Until EOF. */
131 s_flock.l_pid = getpid();
132 s_flock.l_type = F_UNLCK;
134 while (fcntl(fd, F_SETLKW, &s_flock) == -1) {
135 if (errno != EINTR) {
136 if (errno == ENOLCK) {
149 COMPILER_RT_VISIBILITY FILE *lprofOpenFileEx(const char *ProfileName) {
152 #ifdef COMPILER_RT_HAS_FCNTL_LCK
153 fd = open(ProfileName, O_RDWR | O_CREAT, 0666);
157 if (lprofLockFd(fd) != 0)
158 PROF_WARN("Data may be corrupted during profile merging : %s\n",
159 "Fail to obtain file lock due to system limit.");
161 f = fdopen(fd, "r+b");
162 #elif defined(_WIN32)
163 // FIXME: Use the wide variants to handle Unicode filenames.
164 HANDLE h = CreateFileA(ProfileName, GENERIC_READ | GENERIC_WRITE, 0, 0,
165 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
166 if (h == INVALID_HANDLE_VALUE)
169 fd = _open_osfhandle((intptr_t)h, 0);
175 f = _fdopen(fd, "r+b");
181 /* Worst case no locking applied. */
182 PROF_WARN("Concurrent file access is not supported : %s\n",
183 "lack file locking");
184 fd = open(ProfileName, O_RDWR | O_CREAT, 0666);
187 f = fdopen(fd, "r+b");
193 COMPILER_RT_VISIBILITY const char *lprofGetPathPrefix(int *PrefixStrip,
195 const char *Prefix = getenv("GCOV_PREFIX");
196 const char *PrefixStripStr = getenv("GCOV_PREFIX_STRIP");
200 if (Prefix == NULL || Prefix[0] == '\0')
203 if (PrefixStripStr) {
204 *PrefixStrip = atoi(PrefixStripStr);
206 /* Negative GCOV_PREFIX_STRIP values are ignored */
207 if (*PrefixStrip < 0)
212 *PrefixLen = strlen(Prefix);
217 COMPILER_RT_VISIBILITY void
218 lprofApplyPathPrefix(char *Dest, const char *PathStr, const char *Prefix,
219 size_t PrefixLen, int PrefixStrip) {
223 const char *StrippedPathStr = PathStr;
225 for (Level = 0, Ptr = PathStr + 1; Level < PrefixStrip; ++Ptr) {
229 if (!IS_DIR_SEPARATOR(*Ptr))
232 StrippedPathStr = Ptr;
236 memcpy(Dest, Prefix, PrefixLen);
238 if (!IS_DIR_SEPARATOR(Prefix[PrefixLen - 1]))
239 Dest[PrefixLen++] = DIR_SEPARATOR;
241 memcpy(Dest + PrefixLen, StrippedPathStr, strlen(StrippedPathStr) + 1);
244 COMPILER_RT_VISIBILITY const char *
245 lprofFindFirstDirSeparator(const char *Path) {
247 Sep = strchr(Path, DIR_SEPARATOR);
250 #if defined(DIR_SEPARATOR_2)
251 Sep = strchr(Path, DIR_SEPARATOR_2);
256 COMPILER_RT_VISIBILITY const char *lprofFindLastDirSeparator(const char *Path) {
258 Sep = strrchr(Path, DIR_SEPARATOR);
261 #if defined(DIR_SEPARATOR_2)
262 Sep = strrchr(Path, DIR_SEPARATOR_2);
267 COMPILER_RT_VISIBILITY int lprofSuspendSigKill() {
268 #if defined(__linux__)
270 /* Temporarily suspend getting SIGKILL upon exit of the parent process. */
271 if (prctl(PR_GET_PDEATHSIG, &PDeachSig) == 0 && PDeachSig == SIGKILL)
272 prctl(PR_SET_PDEATHSIG, 0);
273 return (PDeachSig == SIGKILL);
279 COMPILER_RT_VISIBILITY void lprofRestoreSigKill() {
280 #if defined(__linux__)
281 prctl(PR_SET_PDEATHSIG, SIGKILL);