blob: 6670a2361119aeddf0564703b1d6771c1d4d19f7 [file] [log] [blame]
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +00001/*===- InstrProfilingFile.c - Write instrumentation to a file -------------===*\
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +00002|*
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 "InstrProfiling.h"
Xinliang David Li1d8d46a2015-11-18 21:08:03 +000011#include "InstrProfilingInternal.h"
Xinliang David Li6e557162015-11-18 21:11:46 +000012#include "InstrProfilingUtil.h"
Joerg Sonnenbergeref24b412015-03-09 11:23:29 +000013#include <errno.h>
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +000014#include <stdio.h>
15#include <stdlib.h>
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +000016#include <string.h>
Xinliang David Li71eddbf2016-05-20 04:52:27 +000017#ifdef _MSC_VER
Xinliang David Li315e49d2016-05-24 21:29:18 +000018/* For _alloca. */
Xinliang David Li71eddbf2016-05-20 04:52:27 +000019#include <malloc.h>
Xinliang David Lifb320a12016-05-20 05:40:07 +000020#endif
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000021#if defined(_WIN32)
22#include "WindowsMMap.h"
23/* For _chsize_s */
24#include <io.h>
25#else
26#include <sys/file.h>
27#include <sys/mman.h>
28#include <unistd.h>
29#if defined(__linux__)
30#include <sys/types.h>
31#endif
32#endif
Xinliang David Li71eddbf2016-05-20 04:52:27 +000033
Xinliang David Li153e8b62016-06-10 20:35:01 +000034/* From where is profile name specified.
35 * The order the enumerators define their
36 * precedence. Re-order them may lead to
37 * runtime behavior change. */
38typedef enum ProfileNameSpecifier {
39 PNS_unknown = 0,
40 PNS_default,
41 PNS_command_line,
42 PNS_environment,
43 PNS_runtime_api
44} ProfileNameSpecifier;
45
46static const char *getPNSStr(ProfileNameSpecifier PNS) {
47 switch (PNS) {
48 case PNS_default:
49 return "default setting";
50 case PNS_command_line:
51 return "command line";
52 case PNS_environment:
53 return "environment variable";
54 case PNS_runtime_api:
55 return "runtime API";
56 default:
57 return "Unknown";
58 }
59}
60
Xinliang David Li315e49d2016-05-24 21:29:18 +000061#define MAX_PID_SIZE 16
62/* Data structure holding the result of parsed filename pattern. */
63typedef struct lprofFilename {
64 /* File name string possibly with %p or %h specifiers. */
65 const char *FilenamePat;
66 char PidChars[MAX_PID_SIZE];
67 char Hostname[COMPILER_RT_MAX_HOSTLEN];
68 unsigned NumPids;
69 unsigned NumHosts;
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000070 /* When in-process merging is enabled, this parameter specifies
71 * the total number of profile data files shared by all the processes
72 * spawned from the same binary. By default the value is 1. If merging
73 * is not enabled, its value should be 0. This parameter is specified
74 * by the %[0-9]m specifier. For instance %2m enables merging using
75 * 2 profile data files. %1m is equivalent to %m. Also %m specifier
76 * can only appear once at the end of the name pattern. */
77 unsigned MergePoolSize;
Xinliang David Li153e8b62016-06-10 20:35:01 +000078 ProfileNameSpecifier PNS;
Xinliang David Li315e49d2016-05-24 21:29:18 +000079} lprofFilename;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000080
Xinliang David Li153e8b62016-06-10 20:35:01 +000081lprofFilename lprofCurFilename = {0, {0}, {0}, 0, 0, 0, PNS_unknown};
Xinliang David Li315e49d2016-05-24 21:29:18 +000082
83int getpid(void);
84static int getCurFilenameLength();
85static const char *getCurFilename(char *FilenameBuf);
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000086static unsigned doMerging() { return lprofCurFilename.MergePoolSize; }
Joerg Sonnenbergerb1cc6d52014-05-20 16:37:07 +000087
Xinliang David Li2d5bf072015-11-21 04:16:42 +000088/* Return 1 if there is an error, otherwise return 0. */
89static uint32_t fileWriter(ProfDataIOVec *IOVecs, uint32_t NumIOVecs,
90 void **WriterCtx) {
91 uint32_t I;
92 FILE *File = (FILE *)*WriterCtx;
93 for (I = 0; I < NumIOVecs; I++) {
94 if (fwrite(IOVecs[I].Data, IOVecs[I].ElmSize, IOVecs[I].NumElm, File) !=
95 IOVecs[I].NumElm)
96 return 1;
97 }
98 return 0;
Xinliang David Li1d8d46a2015-11-18 21:08:03 +000099}
Duncan P. N. Exon Smithcf4bb962014-03-21 18:29:19 +0000100
Xinliang David Licda3bc22015-12-29 23:54:41 +0000101COMPILER_RT_VISIBILITY ProfBufferIO *
Xinliang David Licf1a8d62016-03-06 04:18:13 +0000102lprofCreateBufferIOInternal(void *File, uint32_t BufferSz) {
Xinliang David Li609fae32016-05-13 18:26:26 +0000103 FreeHook = &free;
104 DynamicBufferIOBuffer = (uint8_t *)calloc(BufferSz, 1);
105 VPBufferSize = BufferSz;
106 return lprofCreateBufferIO(fileWriter, File);
107}
108
109static void setupIOBuffer() {
110 const char *BufferSzStr = 0;
111 BufferSzStr = getenv("LLVM_VP_BUFFER_SIZE");
112 if (BufferSzStr && BufferSzStr[0]) {
113 VPBufferSize = atoi(BufferSzStr);
114 DynamicBufferIOBuffer = (uint8_t *)calloc(VPBufferSize, 1);
115 }
Xinliang David Licda3bc22015-12-29 23:54:41 +0000116}
117
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000118/* Read profile data in \c ProfileFile and merge with in-memory
119 profile counters. Returns -1 if there is fatal error, otheriwse
120 0 is returned.
121*/
122static int doProfileMerging(FILE *ProfileFile) {
123 uint64_t ProfileFileSize;
124 char *ProfileBuffer;
125
126 if (fseek(ProfileFile, 0L, SEEK_END) == -1) {
127 PROF_ERR("Unable to merge profile data, unable to get size: %s\n",
128 strerror(errno));
129 return -1;
130 }
131 ProfileFileSize = ftell(ProfileFile);
132
133 /* Restore file offset. */
134 if (fseek(ProfileFile, 0L, SEEK_SET) == -1) {
135 PROF_ERR("Unable to merge profile data, unable to rewind: %s\n",
136 strerror(errno));
137 return -1;
138 }
139
140 /* Nothing to merge. */
141 if (ProfileFileSize < sizeof(__llvm_profile_header)) {
142 if (ProfileFileSize)
143 PROF_WARN("Unable to merge profile data: %s\n",
144 "source profile file is too small.");
145 return 0;
146 }
147
148 ProfileBuffer = mmap(NULL, ProfileFileSize, PROT_READ, MAP_SHARED | MAP_FILE,
149 fileno(ProfileFile), 0);
150 if (ProfileBuffer == MAP_FAILED) {
151 PROF_ERR("Unable to merge profile data, mmap failed: %s\n",
152 strerror(errno));
153 return -1;
154 }
155
156 if (__llvm_profile_check_compatibility(ProfileBuffer, ProfileFileSize)) {
157 (void)munmap(ProfileBuffer, ProfileFileSize);
158 PROF_WARN("Unable to merge profile data: %s\n",
159 "source profile file is not compatible.");
160 return 0;
161 }
162
163 /* Now start merging */
164 __llvm_profile_merge_from_buffer(ProfileBuffer, ProfileFileSize);
165 (void)munmap(ProfileBuffer, ProfileFileSize);
166
167 return 0;
168}
169
170/* Open the profile data for merging. It opens the file in r+b mode with
171 * file locking. If the file has content which is compatible with the
172 * current process, it also reads in the profile data in the file and merge
173 * it with in-memory counters. After the profile data is merged in memory,
174 * the original profile data is truncated and gets ready for the profile
175 * dumper. With profile merging enabled, each executable as well as any of
176 * its instrumented shared libraries dump profile data into their own data file.
177*/
178static FILE *openFileForMerging(const char *ProfileFileName) {
179 FILE *ProfileFile;
180 int rc;
181
182 ProfileFile = lprofOpenFileEx(ProfileFileName);
183 if (!ProfileFile)
184 return NULL;
185
186 rc = doProfileMerging(ProfileFile);
187 if (rc || COMPILER_RT_FTRUNCATE(ProfileFile, 0L) ||
188 fseek(ProfileFile, 0L, SEEK_SET) == -1) {
189 PROF_ERR("Profile Merging of file %s failed: %s\n", ProfileFileName,
190 strerror(errno));
191 fclose(ProfileFile);
192 return NULL;
193 }
194 fseek(ProfileFile, 0L, SEEK_SET);
195 return ProfileFile;
196}
197
Xinliang David Li315e49d2016-05-24 21:29:18 +0000198/* Write profile data to file \c OutputName. */
199static int writeFile(const char *OutputName) {
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000200 int RetVal;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000201 FILE *OutputFile;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000202
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000203 if (!doMerging())
204 OutputFile = fopen(OutputName, "ab");
205 else
206 OutputFile = openFileForMerging(OutputName);
207
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000208 if (!OutputFile)
209 return -1;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000210
Xinliang David Li315e49d2016-05-24 21:29:18 +0000211 FreeHook = &free;
212 setupIOBuffer();
213 RetVal = lprofWriteData(fileWriter, OutputFile, lprofGetVPDataReader());
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000214
215 fclose(OutputFile);
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000216 return RetVal;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000217}
218
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000219static void truncateCurrentFile(void) {
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000220 const char *Filename;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000221 char *FilenameBuf;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000222 FILE *File;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000223 int Length;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000224
Xinliang David Li315e49d2016-05-24 21:29:18 +0000225 Length = getCurFilenameLength();
226 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
227 Filename = getCurFilename(FilenameBuf);
228 if (!Filename)
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000229 return;
230
Diego Novilloeae95142015-07-09 17:21:52 +0000231 /* Create the directory holding the file, if needed. */
Xinliang David Lib6d43b72016-07-19 20:48:00 +0000232 if (lprofFindFirstDirSeparator(Filename)) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000233 char *Copy = (char *)COMPILER_RT_ALLOCA(Length + 1);
234 strncpy(Copy, Filename, Length + 1);
Diego Novilloeae95142015-07-09 17:21:52 +0000235 __llvm_profile_recursive_mkdir(Copy);
236 }
237
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000238 /* Truncate the file. Later we'll reopen and append. */
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000239 File = fopen(Filename, "w");
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000240 if (!File)
241 return;
242 fclose(File);
243}
244
Xinliang David Li153e8b62016-06-10 20:35:01 +0000245static const char *DefaultProfileName = "default.profraw";
Xinliang David Li315e49d2016-05-24 21:29:18 +0000246static void resetFilenameToDefault(void) {
Xinliang David Li7f08d122016-05-25 17:30:15 +0000247 memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
Xinliang David Li153e8b62016-06-10 20:35:01 +0000248 lprofCurFilename.FilenamePat = DefaultProfileName;
249 lprofCurFilename.PNS = PNS_default;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000250}
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000251
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000252static int containsMergeSpecifier(const char *FilenamePat, int I) {
253 return (FilenamePat[I] == 'm' ||
254 (FilenamePat[I] >= '1' && FilenamePat[I] <= '9' &&
255 /* If FilenamePat[I] is not '\0', the next byte is guaranteed
256 * to be in-bound as the string is null terminated. */
257 FilenamePat[I + 1] == 'm'));
258}
Xinliang David Li7f08d122016-05-25 17:30:15 +0000259
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000260/* Parses the pattern string \p FilenamePat and stores the result to
261 * lprofcurFilename structure. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000262static int parseFilenamePattern(const char *FilenamePat) {
263 int NumPids = 0, NumHosts = 0, I;
Xinliang David Li7f08d122016-05-25 17:30:15 +0000264 char *PidChars = &lprofCurFilename.PidChars[0];
265 char *Hostname = &lprofCurFilename.Hostname[0];
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000266 int MergingEnabled = 0;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000267
Xinliang David Li7f08d122016-05-25 17:30:15 +0000268 lprofCurFilename.FilenamePat = FilenamePat;
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000269 /* Check the filename for "%p", which indicates a pid-substitution. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000270 for (I = 0; FilenamePat[I]; ++I)
271 if (FilenamePat[I] == '%') {
272 if (FilenamePat[++I] == 'p') {
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000273 if (!NumPids++) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000274 if (snprintf(PidChars, MAX_PID_SIZE, "%d", getpid()) <= 0) {
275 PROF_WARN(
276 "Unable to parse filename pattern %s. Using the default name.",
277 FilenamePat);
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000278 return -1;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000279 }
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000280 }
Xinliang David Li315e49d2016-05-24 21:29:18 +0000281 } else if (FilenamePat[I] == 'h') {
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000282 if (!NumHosts++)
Xinliang David Li315e49d2016-05-24 21:29:18 +0000283 if (COMPILER_RT_GETHOSTNAME(Hostname, COMPILER_RT_MAX_HOSTLEN)) {
284 PROF_WARN(
285 "Unable to parse filename pattern %s. Using the default name.",
286 FilenamePat);
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000287 return -1;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000288 }
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000289 } else if (containsMergeSpecifier(FilenamePat, I)) {
290 if (MergingEnabled) {
Vedant Kumar8e2dd512016-06-14 17:23:13 +0000291 PROF_WARN("%%m specifier can only be specified once in %s.\n",
292 FilenamePat);
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000293 return -1;
294 }
295 MergingEnabled = 1;
296 if (FilenamePat[I] == 'm')
297 lprofCurFilename.MergePoolSize = 1;
298 else {
299 lprofCurFilename.MergePoolSize = FilenamePat[I] - '0';
300 I++; /* advance to 'm' */
301 }
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +0000302 }
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000303 }
304
Xinliang David Li7f08d122016-05-25 17:30:15 +0000305 lprofCurFilename.NumPids = NumPids;
306 lprofCurFilename.NumHosts = NumHosts;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000307 return 0;
308}
309
Xinliang David Li153e8b62016-06-10 20:35:01 +0000310static void parseAndSetFilename(const char *FilenamePat,
311 ProfileNameSpecifier PNS) {
Xinliang David Li7f08d122016-05-25 17:30:15 +0000312
Xinliang David Li153e8b62016-06-10 20:35:01 +0000313 const char *OldFilenamePat = lprofCurFilename.FilenamePat;
314 ProfileNameSpecifier OldPNS = lprofCurFilename.PNS;
315
316 if (PNS < OldPNS)
317 return;
318
319 if (!FilenamePat)
320 FilenamePat = DefaultProfileName;
321
322 /* When -fprofile-instr-generate=<path> is specified on the
323 * command line, each module will be instrumented with runtime
324 * init call to __llvm_profile_init function which calls
325 * __llvm_profile_override_default_filename. In most of the cases,
326 * the path will be identical, so bypass the parsing completely.
327 */
328 if (OldFilenamePat && !strcmp(OldFilenamePat, FilenamePat)) {
329 lprofCurFilename.PNS = PNS;
330 return;
331 }
332
333 /* When PNS >= OldPNS, the last one wins. */
Xinliang David Li7f08d122016-05-25 17:30:15 +0000334 if (!FilenamePat || parseFilenamePattern(FilenamePat))
335 resetFilenameToDefault();
Xinliang David Li153e8b62016-06-10 20:35:01 +0000336 lprofCurFilename.PNS = PNS;
Xinliang David Li7f08d122016-05-25 17:30:15 +0000337
Xinliang David Li153e8b62016-06-10 20:35:01 +0000338 if (!OldFilenamePat) {
339 PROF_NOTE("Set profile file path to \"%s\" via %s.\n",
340 lprofCurFilename.FilenamePat, getPNSStr(PNS));
341 } else {
342 PROF_NOTE("Override old profile path \"%s\" via %s to \"%s\" via %s.\n",
343 OldFilenamePat, getPNSStr(OldPNS), lprofCurFilename.FilenamePat,
344 getPNSStr(PNS));
345 }
Xinliang David Li7f08d122016-05-25 17:30:15 +0000346
Xinliang David Li153e8b62016-06-10 20:35:01 +0000347 if (!lprofCurFilename.MergePoolSize)
Xinliang David Li7f08d122016-05-25 17:30:15 +0000348 truncateCurrentFile();
349}
350
Xinliang David Li315e49d2016-05-24 21:29:18 +0000351/* Return buffer length that is required to store the current profile
352 * filename with PID and hostname substitutions. */
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000353/* The length to hold uint64_t followed by 2 digit pool id including '_' */
354#define SIGLEN 24
Xinliang David Li315e49d2016-05-24 21:29:18 +0000355static int getCurFilenameLength() {
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000356 int Len;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000357 if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000358 return 0;
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +0000359
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000360 if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
361 lprofCurFilename.MergePoolSize))
Xinliang David Li315e49d2016-05-24 21:29:18 +0000362 return strlen(lprofCurFilename.FilenamePat);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000363
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000364 Len = strlen(lprofCurFilename.FilenamePat) +
365 lprofCurFilename.NumPids * (strlen(lprofCurFilename.PidChars) - 2) +
366 lprofCurFilename.NumHosts * (strlen(lprofCurFilename.Hostname) - 2);
367 if (lprofCurFilename.MergePoolSize)
368 Len += SIGLEN;
369 return Len;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000370}
371
372/* Return the pointer to the current profile file name (after substituting
373 * PIDs and Hostnames in filename pattern. \p FilenameBuf is the buffer
374 * to store the resulting filename. If no substitution is needed, the
375 * current filename pattern string is directly returned. */
376static const char *getCurFilename(char *FilenameBuf) {
377 int I, J, PidLength, HostNameLength;
378 const char *FilenamePat = lprofCurFilename.FilenamePat;
379
380 if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
381 return 0;
382
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000383 if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
384 lprofCurFilename.MergePoolSize))
Xinliang David Li315e49d2016-05-24 21:29:18 +0000385 return lprofCurFilename.FilenamePat;
386
387 PidLength = strlen(lprofCurFilename.PidChars);
388 HostNameLength = strlen(lprofCurFilename.Hostname);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000389 /* Construct the new filename. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000390 for (I = 0, J = 0; FilenamePat[I]; ++I)
391 if (FilenamePat[I] == '%') {
392 if (FilenamePat[++I] == 'p') {
393 memcpy(FilenameBuf + J, lprofCurFilename.PidChars, PidLength);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000394 J += PidLength;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000395 } else if (FilenamePat[I] == 'h') {
396 memcpy(FilenameBuf + J, lprofCurFilename.Hostname, HostNameLength);
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000397 J += HostNameLength;
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000398 } else if (containsMergeSpecifier(FilenamePat, I)) {
399 char LoadModuleSignature[SIGLEN];
400 int S;
401 int ProfilePoolId = getpid() % lprofCurFilename.MergePoolSize;
402 S = snprintf(LoadModuleSignature, SIGLEN, "%" PRIu64 "_%d",
403 lprofGetLoadModuleSignature(), ProfilePoolId);
404 if (S == -1 || S > SIGLEN)
405 S = SIGLEN;
406 memcpy(FilenameBuf + J, LoadModuleSignature, S);
407 J += S;
408 if (FilenamePat[I] != 'm')
409 I++;
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000410 }
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000411 /* Drop any unknown substitutions. */
412 } else
Xinliang David Li315e49d2016-05-24 21:29:18 +0000413 FilenameBuf[J++] = FilenamePat[I];
414 FilenameBuf[J] = 0;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000415
Xinliang David Li315e49d2016-05-24 21:29:18 +0000416 return FilenameBuf;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000417}
418
Xinliang David Li315e49d2016-05-24 21:29:18 +0000419/* Returns the pointer to the environment variable
420 * string. Returns null if the env var is not set. */
421static const char *getFilenamePatFromEnv(void) {
Eric Christopherd641b532015-04-28 22:54:51 +0000422 const char *Filename = getenv("LLVM_PROFILE_FILE");
Eric Christopherd641b532015-04-28 22:54:51 +0000423 if (!Filename || !Filename[0])
Xinliang David Li51fe0022016-05-24 01:23:09 +0000424 return 0;
425 return Filename;
Eric Christopherd641b532015-04-28 22:54:51 +0000426}
427
Xinliang David Li51fe0022016-05-24 01:23:09 +0000428/* This method is invoked by the runtime initialization hook
429 * InstrProfilingRuntime.o if it is linked in. Both user specified
430 * profile path via -fprofile-instr-generate= and LLVM_PROFILE_FILE
431 * environment variable can override this default value. */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000432COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000433void __llvm_profile_initialize_file(void) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000434 const char *FilenamePat;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000435
Xinliang David Li315e49d2016-05-24 21:29:18 +0000436 FilenamePat = getFilenamePatFromEnv();
Xinliang David Li153e8b62016-06-10 20:35:01 +0000437 parseAndSetFilename(FilenamePat, FilenamePat ? PNS_environment : PNS_default);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000438}
439
Xinliang David Li51fe0022016-05-24 01:23:09 +0000440/* This API is directly called by the user application code. It has the
441 * highest precedence compared with LLVM_PROFILE_FILE environment variable
Xinliang David Li41518942016-05-24 02:37:07 +0000442 * and command line option -fprofile-instr-generate=<profile_name>.
Xinliang David Li51fe0022016-05-24 01:23:09 +0000443 */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000444COMPILER_RT_VISIBILITY
Xinliang David Li315e49d2016-05-24 21:29:18 +0000445void __llvm_profile_set_filename(const char *FilenamePat) {
Xinliang David Li153e8b62016-06-10 20:35:01 +0000446 parseAndSetFilename(FilenamePat, PNS_runtime_api);
Eric Christopherd641b532015-04-28 22:54:51 +0000447}
448
Xinliang David Li315e49d2016-05-24 21:29:18 +0000449/*
Xinliang David Li51fe0022016-05-24 01:23:09 +0000450 * This API is invoked by the global initializers emitted by Clang/LLVM when
451 * -fprofile-instr-generate=<..> is specified (vs -fprofile-instr-generate
452 * without an argument). This option has lower precedence than the
453 * LLVM_PROFILE_FILE environment variable.
454 */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000455COMPILER_RT_VISIBILITY
Xinliang David Li315e49d2016-05-24 21:29:18 +0000456void __llvm_profile_override_default_filename(const char *FilenamePat) {
Xinliang David Li153e8b62016-06-10 20:35:01 +0000457 parseAndSetFilename(FilenamePat, PNS_command_line);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000458}
459
Xinliang David Li315e49d2016-05-24 21:29:18 +0000460/* The public API for writing profile data into the file with name
461 * set by previous calls to __llvm_profile_set_filename or
462 * __llvm_profile_override_default_filename or
463 * __llvm_profile_initialize_file. */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000464COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000465int __llvm_profile_write_file(void) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000466 int rc, Length;
467 const char *Filename;
468 char *FilenameBuf;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000469
Xinliang David Li315e49d2016-05-24 21:29:18 +0000470 Length = getCurFilenameLength();
471 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
472 Filename = getCurFilename(FilenameBuf);
473
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000474 /* Check the filename. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000475 if (!Filename) {
Xinliang David Li690c31f2016-05-20 05:15:42 +0000476 PROF_ERR("Failed to write file : %s\n", "Filename not set");
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000477 return -1;
Xinliang David Li9ea30642015-12-03 18:31:59 +0000478 }
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000479
Xinliang David Lid85c32c2016-01-08 23:42:28 +0000480 /* Check if there is llvm/runtime version mismatch. */
Xinliang David Lia6924212016-01-08 23:31:57 +0000481 if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) {
Xinliang David Li690c31f2016-05-20 05:15:42 +0000482 PROF_ERR("Runtime and instrumentation version mismatch : "
Xinliang David Lia6924212016-01-08 23:31:57 +0000483 "expected %d, but get %d\n",
484 INSTR_PROF_RAW_VERSION,
485 (int)GET_VERSION(__llvm_profile_get_version()));
486 return -1;
487 }
488
Xinliang David Li315e49d2016-05-24 21:29:18 +0000489 /* Write profile data to the file. */
490 rc = writeFile(Filename);
Xinliang David Li9ea30642015-12-03 18:31:59 +0000491 if (rc)
Xinliang David Li315e49d2016-05-24 21:29:18 +0000492 PROF_ERR("Failed to write file \"%s\": %s\n", Filename, strerror(errno));
Justin Bogner66fd5c92015-01-16 20:10:56 +0000493 return rc;
Duncan P. N. Exon Smith54cc0e22014-03-20 19:23:55 +0000494}
495
Xinliang David Li6fe18f42015-11-23 04:38:17 +0000496static void writeFileWithoutReturn(void) { __llvm_profile_write_file(); }
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000497
Xinliang David Liabfd5532015-12-16 03:29:15 +0000498COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000499int __llvm_profile_register_write_file_atexit(void) {
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000500 static int HasBeenRegistered = 0;
501
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000502 if (HasBeenRegistered)
503 return 0;
504
Xinliang David Li4617aa72016-05-18 22:34:05 +0000505 lprofSetupValueProfiler();
506
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000507 HasBeenRegistered = 1;
508 return atexit(writeFileWithoutReturn);
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000509}