2015-07-10 01:21:52 +08:00
|
|
|
/*===- InstrProfilingUtil.c - Support library for PGO instrumentation -----===*\
|
|
|
|
|*
|
2019-01-19 16:50:56 +08:00
|
|
|
|* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
|* See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-07-10 01:21:52 +08:00
|
|
|
|*
|
|
|
|
\*===----------------------------------------------------------------------===*/
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <direct.h>
|
2017-12-22 05:48:55 +08:00
|
|
|
#include <process.h>
|
2016-06-06 11:17:58 +08:00
|
|
|
#include <windows.h>
|
2017-12-15 03:01:04 +08:00
|
|
|
#include "WindowsMMap.h"
|
2015-07-10 01:21:52 +08:00
|
|
|
#else
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2016-06-06 11:17:58 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2015-07-10 01:21:52 +08:00
|
|
|
#endif
|
|
|
|
|
2016-03-06 12:52:45 +08:00
|
|
|
#ifdef COMPILER_RT_HAS_UNAME
|
|
|
|
#include <sys/utsname.h>
|
|
|
|
#endif
|
|
|
|
|
2016-07-19 00:16:12 +08:00
|
|
|
#include <stdlib.h>
|
2016-03-06 12:52:45 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2017-03-18 02:41:33 +08:00
|
|
|
#if defined(__linux__)
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
#endif
|
|
|
|
|
2017-12-15 03:01:04 +08:00
|
|
|
#include "InstrProfiling.h"
|
|
|
|
#include "InstrProfilingUtil.h"
|
|
|
|
|
2018-08-01 07:37:24 +08:00
|
|
|
COMPILER_RT_WEAK unsigned lprofDirMode = 0755;
|
|
|
|
|
2015-12-16 11:29:15 +08:00
|
|
|
COMPILER_RT_VISIBILITY
|
2015-07-10 01:21:52 +08:00
|
|
|
void __llvm_profile_recursive_mkdir(char *path) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 1; path[i] != '\0'; ++i) {
|
2016-03-29 05:32:46 +08:00
|
|
|
char save = path[i];
|
2016-07-20 04:20:20 +08:00
|
|
|
if (!IS_DIR_SEPARATOR(path[i]))
|
2016-03-29 05:32:46 +08:00
|
|
|
continue;
|
2015-07-10 01:21:52 +08:00
|
|
|
path[i] = '\0';
|
|
|
|
#ifdef _WIN32
|
|
|
|
_mkdir(path);
|
|
|
|
#else
|
2018-08-01 07:37:24 +08:00
|
|
|
/* Some of these will fail, ignore it. */
|
|
|
|
mkdir(path, __llvm_profile_get_dir_mode());
|
2015-07-10 01:21:52 +08:00
|
|
|
#endif
|
2016-03-29 05:32:46 +08:00
|
|
|
path[i] = save;
|
2015-07-10 01:21:52 +08:00
|
|
|
}
|
|
|
|
}
|
2016-03-06 08:55:20 +08:00
|
|
|
|
2018-08-01 07:37:24 +08:00
|
|
|
COMPILER_RT_VISIBILITY
|
|
|
|
void __llvm_profile_set_dir_mode(unsigned Mode) { lprofDirMode = Mode; }
|
|
|
|
|
|
|
|
COMPILER_RT_VISIBILITY
|
|
|
|
unsigned __llvm_profile_get_dir_mode(void) { return lprofDirMode; }
|
|
|
|
|
2016-03-06 08:55:20 +08:00
|
|
|
#if COMPILER_RT_HAS_ATOMICS != 1
|
|
|
|
COMPILER_RT_VISIBILITY
|
2016-03-06 12:18:13 +08:00
|
|
|
uint32_t lprofBoolCmpXchg(void **Ptr, void *OldV, void *NewV) {
|
2016-03-06 08:55:20 +08:00
|
|
|
void *R = *Ptr;
|
|
|
|
if (R == OldV) {
|
|
|
|
*Ptr = NewV;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2016-05-17 07:01:03 +08:00
|
|
|
COMPILER_RT_VISIBILITY
|
|
|
|
void *lprofPtrFetchAdd(void **Mem, long ByteIncr) {
|
|
|
|
void *Old = *Mem;
|
|
|
|
*((char **)Mem) += ByteIncr;
|
|
|
|
return Old;
|
|
|
|
}
|
|
|
|
|
2016-03-06 08:55:20 +08:00
|
|
|
#endif
|
|
|
|
|
2016-11-29 23:24:00 +08:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
COMPILER_RT_VISIBILITY int lprofGetHostName(char *Name, int Len) {
|
|
|
|
WCHAR Buffer[COMPILER_RT_MAX_HOSTLEN];
|
|
|
|
DWORD BufferSize = sizeof(Buffer);
|
|
|
|
BOOL Result =
|
|
|
|
GetComputerNameExW(ComputerNameDnsFullyQualified, Buffer, &BufferSize);
|
|
|
|
if (!Result)
|
|
|
|
return -1;
|
|
|
|
if (WideCharToMultiByte(CP_UTF8, 0, Buffer, -1, Name, Len, NULL, NULL) == 0)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#elif defined(COMPILER_RT_HAS_UNAME)
|
2016-06-08 08:44:38 +08:00
|
|
|
COMPILER_RT_VISIBILITY int lprofGetHostName(char *Name, int Len) {
|
2016-03-06 12:52:45 +08:00
|
|
|
struct utsname N;
|
2017-12-15 03:01:04 +08:00
|
|
|
int R = uname(&N);
|
|
|
|
if (R >= 0) {
|
2016-03-06 12:52:45 +08:00
|
|
|
strncpy(Name, N.nodename, Len);
|
2017-12-15 03:01:04 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2016-03-06 12:52:45 +08:00
|
|
|
return R;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-12-15 03:01:04 +08:00
|
|
|
COMPILER_RT_VISIBILITY int lprofLockFd(int fd) {
|
2016-06-06 11:17:58 +08:00
|
|
|
#ifdef COMPILER_RT_HAS_FCNTL_LCK
|
|
|
|
struct flock s_flock;
|
|
|
|
|
|
|
|
s_flock.l_whence = SEEK_SET;
|
|
|
|
s_flock.l_start = 0;
|
|
|
|
s_flock.l_len = 0; /* Until EOF. */
|
|
|
|
s_flock.l_pid = getpid();
|
2017-12-15 02:50:13 +08:00
|
|
|
s_flock.l_type = F_WRLCK;
|
2017-12-15 02:43:14 +08:00
|
|
|
|
|
|
|
while (fcntl(fd, F_SETLKW, &s_flock) == -1) {
|
|
|
|
if (errno != EINTR) {
|
|
|
|
if (errno == ENOLCK) {
|
2017-12-15 03:01:04 +08:00
|
|
|
return -1;
|
2017-12-15 02:43:14 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-12-15 03:01:04 +08:00
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
flock(fd, LOCK_EX);
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
COMPILER_RT_VISIBILITY int lprofUnlockFd(int fd) {
|
|
|
|
#ifdef COMPILER_RT_HAS_FCNTL_LCK
|
|
|
|
struct flock s_flock;
|
|
|
|
|
|
|
|
s_flock.l_whence = SEEK_SET;
|
|
|
|
s_flock.l_start = 0;
|
|
|
|
s_flock.l_len = 0; /* Until EOF. */
|
|
|
|
s_flock.l_pid = getpid();
|
|
|
|
s_flock.l_type = F_UNLCK;
|
|
|
|
|
|
|
|
while (fcntl(fd, F_SETLKW, &s_flock) == -1) {
|
|
|
|
if (errno != EINTR) {
|
|
|
|
if (errno == ENOLCK) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
flock(fd, LOCK_UN);
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
COMPILER_RT_VISIBILITY FILE *lprofOpenFileEx(const char *ProfileName) {
|
|
|
|
FILE *f;
|
|
|
|
int fd;
|
|
|
|
#ifdef COMPILER_RT_HAS_FCNTL_LCK
|
|
|
|
fd = open(ProfileName, O_RDWR | O_CREAT, 0666);
|
|
|
|
if (fd < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (lprofLockFd(fd) != 0)
|
|
|
|
PROF_WARN("Data may be corrupted during profile merging : %s\n",
|
|
|
|
"Fail to obtain file lock due to system limit.");
|
2016-06-06 11:17:58 +08:00
|
|
|
|
|
|
|
f = fdopen(fd, "r+b");
|
|
|
|
#elif defined(_WIN32)
|
2016-06-23 23:40:42 +08:00
|
|
|
// FIXME: Use the wide variants to handle Unicode filenames.
|
|
|
|
HANDLE h = CreateFileA(ProfileName, GENERIC_READ | GENERIC_WRITE, 0, 0,
|
|
|
|
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
|
2016-06-06 11:17:58 +08:00
|
|
|
if (h == INVALID_HANDLE_VALUE)
|
2016-06-07 02:31:29 +08:00
|
|
|
return NULL;
|
2016-06-06 11:17:58 +08:00
|
|
|
|
|
|
|
fd = _open_osfhandle((intptr_t)h, 0);
|
|
|
|
if (fd == -1) {
|
|
|
|
CloseHandle(h);
|
2016-06-07 02:31:29 +08:00
|
|
|
return NULL;
|
2016-06-06 11:17:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
f = _fdopen(fd, "r+b");
|
|
|
|
if (f == 0) {
|
|
|
|
CloseHandle(h);
|
2016-06-07 02:31:29 +08:00
|
|
|
return NULL;
|
2016-06-06 11:17:58 +08:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* Worst case no locking applied. */
|
2016-06-07 02:31:29 +08:00
|
|
|
PROF_WARN("Concurrent file access is not supported : %s\n",
|
|
|
|
"lack file locking");
|
2016-06-06 11:17:58 +08:00
|
|
|
fd = open(ProfileName, O_RDWR | O_CREAT, 0666);
|
|
|
|
if (fd < 0)
|
2016-06-07 02:31:29 +08:00
|
|
|
return NULL;
|
2016-06-06 11:17:58 +08:00
|
|
|
f = fdopen(fd, "r+b");
|
|
|
|
#endif
|
2016-03-06 12:52:45 +08:00
|
|
|
|
2016-06-06 11:17:58 +08:00
|
|
|
return f;
|
|
|
|
}
|
2016-07-19 00:16:12 +08:00
|
|
|
|
|
|
|
COMPILER_RT_VISIBILITY const char *lprofGetPathPrefix(int *PrefixStrip,
|
|
|
|
size_t *PrefixLen) {
|
|
|
|
const char *Prefix = getenv("GCOV_PREFIX");
|
|
|
|
const char *PrefixStripStr = getenv("GCOV_PREFIX_STRIP");
|
|
|
|
|
|
|
|
*PrefixLen = 0;
|
|
|
|
*PrefixStrip = 0;
|
|
|
|
if (Prefix == NULL || Prefix[0] == '\0')
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (PrefixStripStr) {
|
|
|
|
*PrefixStrip = atoi(PrefixStripStr);
|
|
|
|
|
|
|
|
/* Negative GCOV_PREFIX_STRIP values are ignored */
|
|
|
|
if (*PrefixStrip < 0)
|
|
|
|
*PrefixStrip = 0;
|
|
|
|
} else {
|
|
|
|
*PrefixStrip = 0;
|
|
|
|
}
|
|
|
|
*PrefixLen = strlen(Prefix);
|
|
|
|
|
|
|
|
return Prefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
COMPILER_RT_VISIBILITY void
|
|
|
|
lprofApplyPathPrefix(char *Dest, const char *PathStr, const char *Prefix,
|
|
|
|
size_t PrefixLen, int PrefixStrip) {
|
|
|
|
|
|
|
|
const char *Ptr;
|
|
|
|
int Level;
|
|
|
|
const char *StrippedPathStr = PathStr;
|
|
|
|
|
|
|
|
for (Level = 0, Ptr = PathStr + 1; Level < PrefixStrip; ++Ptr) {
|
|
|
|
if (*Ptr == '\0')
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!IS_DIR_SEPARATOR(*Ptr))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
StrippedPathStr = Ptr;
|
|
|
|
++Level;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(Dest, Prefix, PrefixLen);
|
|
|
|
|
2017-08-15 11:13:01 +08:00
|
|
|
if (!IS_DIR_SEPARATOR(Prefix[PrefixLen - 1]))
|
2016-07-19 00:16:12 +08:00
|
|
|
Dest[PrefixLen++] = DIR_SEPARATOR;
|
|
|
|
|
|
|
|
memcpy(Dest + PrefixLen, StrippedPathStr, strlen(StrippedPathStr) + 1);
|
|
|
|
}
|
2016-07-20 04:48:00 +08:00
|
|
|
|
|
|
|
COMPILER_RT_VISIBILITY const char *
|
|
|
|
lprofFindFirstDirSeparator(const char *Path) {
|
2018-07-24 21:06:19 +08:00
|
|
|
const char *Sep = strchr(Path, DIR_SEPARATOR);
|
2016-07-20 04:48:00 +08:00
|
|
|
#if defined(DIR_SEPARATOR_2)
|
2018-07-24 21:06:19 +08:00
|
|
|
const char *Sep2 = strchr(Path, DIR_SEPARATOR_2);
|
|
|
|
if (Sep2 && (!Sep || Sep2 < Sep))
|
|
|
|
Sep = Sep2;
|
2016-07-20 04:48:00 +08:00
|
|
|
#endif
|
|
|
|
return Sep;
|
|
|
|
}
|
|
|
|
|
|
|
|
COMPILER_RT_VISIBILITY const char *lprofFindLastDirSeparator(const char *Path) {
|
2018-07-24 21:06:19 +08:00
|
|
|
const char *Sep = strrchr(Path, DIR_SEPARATOR);
|
2016-07-20 04:48:00 +08:00
|
|
|
#if defined(DIR_SEPARATOR_2)
|
2018-07-24 21:06:19 +08:00
|
|
|
const char *Sep2 = strrchr(Path, DIR_SEPARATOR_2);
|
|
|
|
if (Sep2 && (!Sep || Sep2 > Sep))
|
|
|
|
Sep = Sep2;
|
2016-07-20 04:48:00 +08:00
|
|
|
#endif
|
|
|
|
return Sep;
|
|
|
|
}
|
2017-03-18 02:41:33 +08:00
|
|
|
|
|
|
|
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)
|
|
|
|
prctl(PR_SET_PDEATHSIG, 0);
|
|
|
|
return (PDeachSig == SIGKILL);
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
COMPILER_RT_VISIBILITY void lprofRestoreSigKill() {
|
|
|
|
#if defined(__linux__)
|
|
|
|
prctl(PR_SET_PDEATHSIG, SIGKILL);
|
|
|
|
#endif
|
|
|
|
}
|