]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/profile/InstrProfilingUtil.c
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r305575, and update
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / profile / InstrProfilingUtil.c
1 /*===- InstrProfilingUtil.c - Support library for PGO instrumentation -----===*\
2 |*
3 |*                     The LLVM Compiler Infrastructure
4 |*
5 |* This file is distributed under the University of Illinois Open Source
6 |* License. See LICENSE.TXT for details.
7 |*
8 \*===----------------------------------------------------------------------===*/
9
10 #include "InstrProfilingUtil.h"
11 #include "InstrProfiling.h"
12
13 #ifdef _WIN32
14 #include <direct.h>
15 #include <io.h>
16 #include <windows.h>
17 #else
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <errno.h>
23 #endif
24
25 #ifdef COMPILER_RT_HAS_UNAME
26 #include <sys/utsname.h>
27 #endif
28
29 #include <stdlib.h>
30 #include <string.h>
31
32 #if defined(__linux__)
33 #include <signal.h>
34 #include <sys/prctl.h>
35 #endif
36
37 COMPILER_RT_VISIBILITY
38 void __llvm_profile_recursive_mkdir(char *path) {
39   int i;
40
41   for (i = 1; path[i] != '\0'; ++i) {
42     char save = path[i];
43     if (!IS_DIR_SEPARATOR(path[i]))
44       continue;
45     path[i] = '\0';
46 #ifdef _WIN32
47     _mkdir(path);
48 #else
49     mkdir(path, 0755); /* Some of these will fail, ignore it. */
50 #endif
51     path[i] = save;
52   }
53 }
54
55 #if COMPILER_RT_HAS_ATOMICS != 1
56 COMPILER_RT_VISIBILITY
57 uint32_t lprofBoolCmpXchg(void **Ptr, void *OldV, void *NewV) {
58   void *R = *Ptr;
59   if (R == OldV) {
60     *Ptr = NewV;
61     return 1;
62   }
63   return 0;
64 }
65 COMPILER_RT_VISIBILITY
66 void *lprofPtrFetchAdd(void **Mem, long ByteIncr) {
67   void *Old = *Mem;
68   *((char **)Mem) += ByteIncr;
69   return Old;
70 }
71
72 #endif
73
74 #ifdef _MSC_VER
75 COMPILER_RT_VISIBILITY int lprofGetHostName(char *Name, int Len) {
76   WCHAR Buffer[COMPILER_RT_MAX_HOSTLEN];
77   DWORD BufferSize = sizeof(Buffer);
78   BOOL Result =
79       GetComputerNameExW(ComputerNameDnsFullyQualified, Buffer, &BufferSize);
80   if (!Result)
81     return -1;
82   if (WideCharToMultiByte(CP_UTF8, 0, Buffer, -1, Name, Len, NULL, NULL) == 0)
83     return -1;
84   return 0;
85 }
86 #elif defined(COMPILER_RT_HAS_UNAME)
87 COMPILER_RT_VISIBILITY int lprofGetHostName(char *Name, int Len) {
88   struct utsname N;
89   int R;
90   if (!(R = uname(&N)))
91     strncpy(Name, N.nodename, Len);
92   return R;
93 }
94 #endif
95
96 COMPILER_RT_VISIBILITY FILE *lprofOpenFileEx(const char *ProfileName) {
97   FILE *f;
98   int fd;
99 #ifdef COMPILER_RT_HAS_FCNTL_LCK
100   struct flock s_flock;
101
102   s_flock.l_whence = SEEK_SET;
103   s_flock.l_start = 0;
104   s_flock.l_len = 0; /* Until EOF.  */
105   s_flock.l_pid = getpid();
106
107   s_flock.l_type = F_WRLCK;
108   fd = open(ProfileName, O_RDWR | O_CREAT, 0666);
109   if (fd < 0)
110     return NULL;
111
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.");
117       }
118       break;
119     }
120   }
121
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)
128     return NULL;
129
130   fd = _open_osfhandle((intptr_t)h, 0);
131   if (fd == -1) {
132     CloseHandle(h);
133     return NULL;
134   }
135
136   f = _fdopen(fd, "r+b");
137   if (f == 0) {
138     CloseHandle(h);
139     return NULL;
140   }
141 #else
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);
146   if (fd < 0)
147     return NULL;
148   f = fdopen(fd, "r+b");
149 #endif
150
151   return f;
152 }
153
154 COMPILER_RT_VISIBILITY const char *lprofGetPathPrefix(int *PrefixStrip,
155                                                       size_t *PrefixLen) {
156   const char *Prefix = getenv("GCOV_PREFIX");
157   const char *PrefixStripStr = getenv("GCOV_PREFIX_STRIP");
158
159   *PrefixLen = 0;
160   *PrefixStrip = 0;
161   if (Prefix == NULL || Prefix[0] == '\0')
162     return NULL;
163
164   if (PrefixStripStr) {
165     *PrefixStrip = atoi(PrefixStripStr);
166
167     /* Negative GCOV_PREFIX_STRIP values are ignored */
168     if (*PrefixStrip < 0)
169       *PrefixStrip = 0;
170   } else {
171     *PrefixStrip = 0;
172   }
173   *PrefixLen = strlen(Prefix);
174
175   return Prefix;
176 }
177
178 COMPILER_RT_VISIBILITY void
179 lprofApplyPathPrefix(char *Dest, const char *PathStr, const char *Prefix,
180                      size_t PrefixLen, int PrefixStrip) {
181
182   const char *Ptr;
183   int Level;
184   const char *StrippedPathStr = PathStr;
185
186   for (Level = 0, Ptr = PathStr + 1; Level < PrefixStrip; ++Ptr) {
187     if (*Ptr == '\0')
188       break;
189
190     if (!IS_DIR_SEPARATOR(*Ptr))
191       continue;
192
193     StrippedPathStr = Ptr;
194     ++Level;
195   }
196
197   memcpy(Dest, Prefix, PrefixLen);
198
199   if (!IS_DIR_SEPARATOR(Prefix[PrefixLen - 1]))
200     Dest[PrefixLen++] = DIR_SEPARATOR;
201
202   memcpy(Dest + PrefixLen, StrippedPathStr, strlen(StrippedPathStr) + 1);
203 }
204
205 COMPILER_RT_VISIBILITY const char *
206 lprofFindFirstDirSeparator(const char *Path) {
207   const char *Sep;
208   Sep = strchr(Path, DIR_SEPARATOR);
209   if (Sep)
210     return Sep;
211 #if defined(DIR_SEPARATOR_2)
212   Sep = strchr(Path, DIR_SEPARATOR_2);
213 #endif
214   return Sep;
215 }
216
217 COMPILER_RT_VISIBILITY const char *lprofFindLastDirSeparator(const char *Path) {
218   const char *Sep;
219   Sep = strrchr(Path, DIR_SEPARATOR);
220   if (Sep)
221     return Sep;
222 #if defined(DIR_SEPARATOR_2)
223   Sep = strrchr(Path, DIR_SEPARATOR_2);
224 #endif
225   return Sep;
226 }
227
228 COMPILER_RT_VISIBILITY int lprofSuspendSigKill() {
229 #if defined(__linux__)
230   int PDeachSig = 0;
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);
235 #else
236   return 0;
237 #endif
238 }
239
240 COMPILER_RT_VISIBILITY void lprofRestoreSigKill() {
241 #if defined(__linux__)
242   prctl(PR_SET_PDEATHSIG, SIGKILL);
243 #endif
244 }