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 COMPILER_RT_VISIBILITY
33 void __llvm_profile_recursive_mkdir(char *path
) {
36 for (i
= 1; path
[i
] != '\0'; ++i
) {
38 if (!IS_DIR_SEPARATOR(path
[i
]))
44 mkdir(path
, 0755); /* Some of these will fail, ignore it. */
50 #if COMPILER_RT_HAS_ATOMICS != 1
51 COMPILER_RT_VISIBILITY
52 uint32_t lprofBoolCmpXchg(void **Ptr
, void *OldV
, void *NewV
) {
60 COMPILER_RT_VISIBILITY
61 void *lprofPtrFetchAdd(void **Mem
, long ByteIncr
) {
63 *((char **)Mem
) += ByteIncr
;
70 COMPILER_RT_VISIBILITY
int lprofGetHostName(char *Name
, int Len
) {
71 WCHAR Buffer
[COMPILER_RT_MAX_HOSTLEN
];
72 DWORD BufferSize
= sizeof(Buffer
);
74 GetComputerNameExW(ComputerNameDnsFullyQualified
, Buffer
, &BufferSize
);
77 if (WideCharToMultiByte(CP_UTF8
, 0, Buffer
, -1, Name
, Len
, NULL
, NULL
) == 0)
81 #elif defined(COMPILER_RT_HAS_UNAME)
82 COMPILER_RT_VISIBILITY
int lprofGetHostName(char *Name
, int Len
) {
86 strncpy(Name
, N
.nodename
, Len
);
91 COMPILER_RT_VISIBILITY
FILE *lprofOpenFileEx(const char *ProfileName
) {
94 #ifdef COMPILER_RT_HAS_FCNTL_LCK
97 s_flock
.l_whence
= SEEK_SET
;
99 s_flock
.l_len
= 0; /* Until EOF. */
100 s_flock
.l_pid
= getpid();
102 s_flock
.l_type
= F_WRLCK
;
103 fd
= open(ProfileName
, O_RDWR
| O_CREAT
, 0666);
107 while (fcntl(fd
, F_SETLKW
, &s_flock
) == -1) {
108 if (errno
!= EINTR
) {
109 if (errno
== ENOLCK
) {
110 PROF_WARN("Data may be corrupted during profile merging : %s\n",
111 "Fail to obtain file lock due to system limit.");
117 f
= fdopen(fd
, "r+b");
118 #elif defined(_WIN32)
119 // FIXME: Use the wide variants to handle Unicode filenames.
120 HANDLE h
= CreateFileA(ProfileName
, GENERIC_READ
| GENERIC_WRITE
, 0, 0,
121 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, 0);
122 if (h
== INVALID_HANDLE_VALUE
)
125 fd
= _open_osfhandle((intptr_t)h
, 0);
131 f
= _fdopen(fd
, "r+b");
137 /* Worst case no locking applied. */
138 PROF_WARN("Concurrent file access is not supported : %s\n",
139 "lack file locking");
140 fd
= open(ProfileName
, O_RDWR
| O_CREAT
, 0666);
143 f
= fdopen(fd
, "r+b");
149 COMPILER_RT_VISIBILITY
const char *lprofGetPathPrefix(int *PrefixStrip
,
151 const char *Prefix
= getenv("GCOV_PREFIX");
152 const char *PrefixStripStr
= getenv("GCOV_PREFIX_STRIP");
156 if (Prefix
== NULL
|| Prefix
[0] == '\0')
159 if (PrefixStripStr
) {
160 *PrefixStrip
= atoi(PrefixStripStr
);
162 /* Negative GCOV_PREFIX_STRIP values are ignored */
163 if (*PrefixStrip
< 0)
168 *PrefixLen
= strlen(Prefix
);
173 COMPILER_RT_VISIBILITY
void
174 lprofApplyPathPrefix(char *Dest
, const char *PathStr
, const char *Prefix
,
175 size_t PrefixLen
, int PrefixStrip
) {
179 const char *StrippedPathStr
= PathStr
;
181 for (Level
= 0, Ptr
= PathStr
+ 1; Level
< PrefixStrip
; ++Ptr
) {
185 if (!IS_DIR_SEPARATOR(*Ptr
))
188 StrippedPathStr
= Ptr
;
192 memcpy(Dest
, Prefix
, PrefixLen
);
194 if (!IS_DIR_SEPARATOR(Prefix
[PrefixLen
- 1]))
195 Dest
[PrefixLen
++] = DIR_SEPARATOR
;
197 memcpy(Dest
+ PrefixLen
, StrippedPathStr
, strlen(StrippedPathStr
) + 1);
200 COMPILER_RT_VISIBILITY
const char *
201 lprofFindFirstDirSeparator(const char *Path
) {
203 Sep
= strchr(Path
, DIR_SEPARATOR
);
206 #if defined(DIR_SEPARATOR_2)
207 Sep
= strchr(Path
, DIR_SEPARATOR_2
);
212 COMPILER_RT_VISIBILITY
const char *lprofFindLastDirSeparator(const char *Path
) {
214 Sep
= strrchr(Path
, DIR_SEPARATOR
);
217 #if defined(DIR_SEPARATOR_2)
218 Sep
= strrchr(Path
, DIR_SEPARATOR_2
);