]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/profile/InstrProfilingUtil.c
Update DTS files from Linux 4.12
[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 COMPILER_RT_VISIBILITY
33 void __llvm_profile_recursive_mkdir(char *path) {
34   int i;
35
36   for (i = 1; path[i] != '\0'; ++i) {
37     char save = path[i];
38     if (!IS_DIR_SEPARATOR(path[i]))
39       continue;
40     path[i] = '\0';
41 #ifdef _WIN32
42     _mkdir(path);
43 #else
44     mkdir(path, 0755); /* Some of these will fail, ignore it. */
45 #endif
46     path[i] = save;
47   }
48 }
49
50 #if COMPILER_RT_HAS_ATOMICS != 1
51 COMPILER_RT_VISIBILITY
52 uint32_t lprofBoolCmpXchg(void **Ptr, void *OldV, void *NewV) {
53   void *R = *Ptr;
54   if (R == OldV) {
55     *Ptr = NewV;
56     return 1;
57   }
58   return 0;
59 }
60 COMPILER_RT_VISIBILITY
61 void *lprofPtrFetchAdd(void **Mem, long ByteIncr) {
62   void *Old = *Mem;
63   *((char **)Mem) += ByteIncr;
64   return Old;
65 }
66
67 #endif
68
69 #ifdef _MSC_VER
70 COMPILER_RT_VISIBILITY int lprofGetHostName(char *Name, int Len) {
71   WCHAR Buffer[COMPILER_RT_MAX_HOSTLEN];
72   DWORD BufferSize = sizeof(Buffer);
73   BOOL Result =
74       GetComputerNameExW(ComputerNameDnsFullyQualified, Buffer, &BufferSize);
75   if (!Result)
76     return -1;
77   if (WideCharToMultiByte(CP_UTF8, 0, Buffer, -1, Name, Len, NULL, NULL) == 0)
78     return -1;
79   return 0;
80 }
81 #elif defined(COMPILER_RT_HAS_UNAME)
82 COMPILER_RT_VISIBILITY int lprofGetHostName(char *Name, int Len) {
83   struct utsname N;
84   int R;
85   if (!(R = uname(&N)))
86     strncpy(Name, N.nodename, Len);
87   return R;
88 }
89 #endif
90
91 COMPILER_RT_VISIBILITY FILE *lprofOpenFileEx(const char *ProfileName) {
92   FILE *f;
93   int fd;
94 #ifdef COMPILER_RT_HAS_FCNTL_LCK
95   struct flock s_flock;
96
97   s_flock.l_whence = SEEK_SET;
98   s_flock.l_start = 0;
99   s_flock.l_len = 0; /* Until EOF.  */
100   s_flock.l_pid = getpid();
101
102   s_flock.l_type = F_WRLCK;
103   fd = open(ProfileName, O_RDWR | O_CREAT, 0666);
104   if (fd < 0)
105     return NULL;
106
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.");
112       }
113       break;
114     }
115   }
116
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)
123     return NULL;
124
125   fd = _open_osfhandle((intptr_t)h, 0);
126   if (fd == -1) {
127     CloseHandle(h);
128     return NULL;
129   }
130
131   f = _fdopen(fd, "r+b");
132   if (f == 0) {
133     CloseHandle(h);
134     return NULL;
135   }
136 #else
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);
141   if (fd < 0)
142     return NULL;
143   f = fdopen(fd, "r+b");
144 #endif
145
146   return f;
147 }
148
149 COMPILER_RT_VISIBILITY const char *lprofGetPathPrefix(int *PrefixStrip,
150                                                       size_t *PrefixLen) {
151   const char *Prefix = getenv("GCOV_PREFIX");
152   const char *PrefixStripStr = getenv("GCOV_PREFIX_STRIP");
153
154   *PrefixLen = 0;
155   *PrefixStrip = 0;
156   if (Prefix == NULL || Prefix[0] == '\0')
157     return NULL;
158
159   if (PrefixStripStr) {
160     *PrefixStrip = atoi(PrefixStripStr);
161
162     /* Negative GCOV_PREFIX_STRIP values are ignored */
163     if (*PrefixStrip < 0)
164       *PrefixStrip = 0;
165   } else {
166     *PrefixStrip = 0;
167   }
168   *PrefixLen = strlen(Prefix);
169
170   return Prefix;
171 }
172
173 COMPILER_RT_VISIBILITY void
174 lprofApplyPathPrefix(char *Dest, const char *PathStr, const char *Prefix,
175                      size_t PrefixLen, int PrefixStrip) {
176
177   const char *Ptr;
178   int Level;
179   const char *StrippedPathStr = PathStr;
180
181   for (Level = 0, Ptr = PathStr + 1; Level < PrefixStrip; ++Ptr) {
182     if (*Ptr == '\0')
183       break;
184
185     if (!IS_DIR_SEPARATOR(*Ptr))
186       continue;
187
188     StrippedPathStr = Ptr;
189     ++Level;
190   }
191
192   memcpy(Dest, Prefix, PrefixLen);
193
194   if (!IS_DIR_SEPARATOR(Prefix[PrefixLen - 1]))
195     Dest[PrefixLen++] = DIR_SEPARATOR;
196
197   memcpy(Dest + PrefixLen, StrippedPathStr, strlen(StrippedPathStr) + 1);
198 }
199
200 COMPILER_RT_VISIBILITY const char *
201 lprofFindFirstDirSeparator(const char *Path) {
202   const char *Sep;
203   Sep = strchr(Path, DIR_SEPARATOR);
204   if (Sep)
205     return Sep;
206 #if defined(DIR_SEPARATOR_2)
207   Sep = strchr(Path, DIR_SEPARATOR_2);
208 #endif
209   return Sep;
210 }
211
212 COMPILER_RT_VISIBILITY const char *lprofFindLastDirSeparator(const char *Path) {
213   const char *Sep;
214   Sep = strrchr(Path, DIR_SEPARATOR);
215   if (Sep)
216     return Sep;
217 #if defined(DIR_SEPARATOR_2)
218   Sep = strrchr(Path, DIR_SEPARATOR_2);
219 #endif
220   return Sep;
221 }