blob: 76d81d0644de422a1a744f3891ae414e963f29a5 [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;
Xinliang David Lieaf238d2016-07-20 04:26:09 +000066 const char *ProfilePathPrefix;
Xinliang David Li315e49d2016-05-24 21:29:18 +000067 char PidChars[MAX_PID_SIZE];
68 char Hostname[COMPILER_RT_MAX_HOSTLEN];
69 unsigned NumPids;
70 unsigned NumHosts;
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000071 /* When in-process merging is enabled, this parameter specifies
72 * the total number of profile data files shared by all the processes
73 * spawned from the same binary. By default the value is 1. If merging
74 * is not enabled, its value should be 0. This parameter is specified
75 * by the %[0-9]m specifier. For instance %2m enables merging using
76 * 2 profile data files. %1m is equivalent to %m. Also %m specifier
77 * can only appear once at the end of the name pattern. */
78 unsigned MergePoolSize;
Xinliang David Li153e8b62016-06-10 20:35:01 +000079 ProfileNameSpecifier PNS;
Xinliang David Li315e49d2016-05-24 21:29:18 +000080} lprofFilename;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000081
Xinliang David Lieaf238d2016-07-20 04:26:09 +000082lprofFilename lprofCurFilename = {0, 0, {0}, {0}, 0, 0, 0, PNS_unknown};
Xinliang David Li315e49d2016-05-24 21:29:18 +000083
84int getpid(void);
85static int getCurFilenameLength();
86static const char *getCurFilename(char *FilenameBuf);
Xinliang David Lie2ce2e02016-06-08 23:43:56 +000087static unsigned doMerging() { return lprofCurFilename.MergePoolSize; }
Joerg Sonnenbergerb1cc6d52014-05-20 16:37:07 +000088
Xinliang David Li2d5bf072015-11-21 04:16:42 +000089/* Return 1 if there is an error, otherwise return 0. */
90static uint32_t fileWriter(ProfDataIOVec *IOVecs, uint32_t NumIOVecs,
91 void **WriterCtx) {
92 uint32_t I;
93 FILE *File = (FILE *)*WriterCtx;
94 for (I = 0; I < NumIOVecs; I++) {
95 if (fwrite(IOVecs[I].Data, IOVecs[I].ElmSize, IOVecs[I].NumElm, File) !=
96 IOVecs[I].NumElm)
97 return 1;
98 }
99 return 0;
Xinliang David Li1d8d46a2015-11-18 21:08:03 +0000100}
Duncan P. N. Exon Smithcf4bb962014-03-21 18:29:19 +0000101
Xinliang David Licda3bc22015-12-29 23:54:41 +0000102COMPILER_RT_VISIBILITY ProfBufferIO *
Xinliang David Licf1a8d62016-03-06 04:18:13 +0000103lprofCreateBufferIOInternal(void *File, uint32_t BufferSz) {
Xinliang David Li609fae32016-05-13 18:26:26 +0000104 FreeHook = &free;
105 DynamicBufferIOBuffer = (uint8_t *)calloc(BufferSz, 1);
106 VPBufferSize = BufferSz;
107 return lprofCreateBufferIO(fileWriter, File);
108}
109
110static void setupIOBuffer() {
111 const char *BufferSzStr = 0;
112 BufferSzStr = getenv("LLVM_VP_BUFFER_SIZE");
113 if (BufferSzStr && BufferSzStr[0]) {
114 VPBufferSize = atoi(BufferSzStr);
115 DynamicBufferIOBuffer = (uint8_t *)calloc(VPBufferSize, 1);
116 }
Xinliang David Licda3bc22015-12-29 23:54:41 +0000117}
118
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000119/* Read profile data in \c ProfileFile and merge with in-memory
120 profile counters. Returns -1 if there is fatal error, otheriwse
121 0 is returned.
122*/
123static int doProfileMerging(FILE *ProfileFile) {
124 uint64_t ProfileFileSize;
125 char *ProfileBuffer;
126
127 if (fseek(ProfileFile, 0L, SEEK_END) == -1) {
128 PROF_ERR("Unable to merge profile data, unable to get size: %s\n",
129 strerror(errno));
130 return -1;
131 }
132 ProfileFileSize = ftell(ProfileFile);
133
134 /* Restore file offset. */
135 if (fseek(ProfileFile, 0L, SEEK_SET) == -1) {
136 PROF_ERR("Unable to merge profile data, unable to rewind: %s\n",
137 strerror(errno));
138 return -1;
139 }
140
141 /* Nothing to merge. */
142 if (ProfileFileSize < sizeof(__llvm_profile_header)) {
143 if (ProfileFileSize)
144 PROF_WARN("Unable to merge profile data: %s\n",
145 "source profile file is too small.");
146 return 0;
147 }
148
149 ProfileBuffer = mmap(NULL, ProfileFileSize, PROT_READ, MAP_SHARED | MAP_FILE,
150 fileno(ProfileFile), 0);
151 if (ProfileBuffer == MAP_FAILED) {
152 PROF_ERR("Unable to merge profile data, mmap failed: %s\n",
153 strerror(errno));
154 return -1;
155 }
156
157 if (__llvm_profile_check_compatibility(ProfileBuffer, ProfileFileSize)) {
158 (void)munmap(ProfileBuffer, ProfileFileSize);
159 PROF_WARN("Unable to merge profile data: %s\n",
160 "source profile file is not compatible.");
161 return 0;
162 }
163
164 /* Now start merging */
165 __llvm_profile_merge_from_buffer(ProfileBuffer, ProfileFileSize);
166 (void)munmap(ProfileBuffer, ProfileFileSize);
167
168 return 0;
169}
170
171/* Open the profile data for merging. It opens the file in r+b mode with
172 * file locking. If the file has content which is compatible with the
173 * current process, it also reads in the profile data in the file and merge
174 * it with in-memory counters. After the profile data is merged in memory,
175 * the original profile data is truncated and gets ready for the profile
176 * dumper. With profile merging enabled, each executable as well as any of
177 * its instrumented shared libraries dump profile data into their own data file.
178*/
179static FILE *openFileForMerging(const char *ProfileFileName) {
180 FILE *ProfileFile;
181 int rc;
182
183 ProfileFile = lprofOpenFileEx(ProfileFileName);
184 if (!ProfileFile)
185 return NULL;
186
187 rc = doProfileMerging(ProfileFile);
188 if (rc || COMPILER_RT_FTRUNCATE(ProfileFile, 0L) ||
189 fseek(ProfileFile, 0L, SEEK_SET) == -1) {
190 PROF_ERR("Profile Merging of file %s failed: %s\n", ProfileFileName,
191 strerror(errno));
192 fclose(ProfileFile);
193 return NULL;
194 }
195 fseek(ProfileFile, 0L, SEEK_SET);
196 return ProfileFile;
197}
198
Xinliang David Li315e49d2016-05-24 21:29:18 +0000199/* Write profile data to file \c OutputName. */
200static int writeFile(const char *OutputName) {
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000201 int RetVal;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000202 FILE *OutputFile;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000203
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000204 if (!doMerging())
205 OutputFile = fopen(OutputName, "ab");
206 else
207 OutputFile = openFileForMerging(OutputName);
208
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000209 if (!OutputFile)
210 return -1;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000211
Xinliang David Li315e49d2016-05-24 21:29:18 +0000212 FreeHook = &free;
213 setupIOBuffer();
214 RetVal = lprofWriteData(fileWriter, OutputFile, lprofGetVPDataReader());
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000215
216 fclose(OutputFile);
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000217 return RetVal;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000218}
219
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000220static void truncateCurrentFile(void) {
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000221 const char *Filename;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000222 char *FilenameBuf;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000223 FILE *File;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000224 int Length;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000225
Xinliang David Li315e49d2016-05-24 21:29:18 +0000226 Length = getCurFilenameLength();
227 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
228 Filename = getCurFilename(FilenameBuf);
229 if (!Filename)
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000230 return;
231
Diego Novilloeae95142015-07-09 17:21:52 +0000232 /* Create the directory holding the file, if needed. */
Xinliang David Lib6d43b72016-07-19 20:48:00 +0000233 if (lprofFindFirstDirSeparator(Filename)) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000234 char *Copy = (char *)COMPILER_RT_ALLOCA(Length + 1);
235 strncpy(Copy, Filename, Length + 1);
Diego Novilloeae95142015-07-09 17:21:52 +0000236 __llvm_profile_recursive_mkdir(Copy);
237 }
238
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000239 /* Truncate the file. Later we'll reopen and append. */
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000240 File = fopen(Filename, "w");
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000241 if (!File)
242 return;
243 fclose(File);
244}
245
Xinliang David Li153e8b62016-06-10 20:35:01 +0000246static const char *DefaultProfileName = "default.profraw";
Xinliang David Li315e49d2016-05-24 21:29:18 +0000247static void resetFilenameToDefault(void) {
Xinliang David Li7f08d122016-05-25 17:30:15 +0000248 memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
Xinliang David Li153e8b62016-06-10 20:35:01 +0000249 lprofCurFilename.FilenamePat = DefaultProfileName;
250 lprofCurFilename.PNS = PNS_default;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000251}
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000252
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000253static int containsMergeSpecifier(const char *FilenamePat, int I) {
254 return (FilenamePat[I] == 'm' ||
255 (FilenamePat[I] >= '1' && FilenamePat[I] <= '9' &&
256 /* If FilenamePat[I] is not '\0', the next byte is guaranteed
257 * to be in-bound as the string is null terminated. */
258 FilenamePat[I + 1] == 'm'));
259}
Xinliang David Li7f08d122016-05-25 17:30:15 +0000260
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000261/* Parses the pattern string \p FilenamePat and stores the result to
262 * lprofcurFilename structure. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000263static int parseFilenamePattern(const char *FilenamePat) {
264 int NumPids = 0, NumHosts = 0, I;
Xinliang David Li7f08d122016-05-25 17:30:15 +0000265 char *PidChars = &lprofCurFilename.PidChars[0];
266 char *Hostname = &lprofCurFilename.Hostname[0];
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000267 int MergingEnabled = 0;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000268
Xinliang David Lib061cdb2016-07-20 05:10:56 +0000269 /* Clean up cached prefix. */
270 if (lprofCurFilename.ProfilePathPrefix)
271 free((void *)lprofCurFilename.ProfilePathPrefix);
272 memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
273
Xinliang David Li7f08d122016-05-25 17:30:15 +0000274 lprofCurFilename.FilenamePat = FilenamePat;
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000275 /* Check the filename for "%p", which indicates a pid-substitution. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000276 for (I = 0; FilenamePat[I]; ++I)
277 if (FilenamePat[I] == '%') {
278 if (FilenamePat[++I] == 'p') {
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000279 if (!NumPids++) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000280 if (snprintf(PidChars, MAX_PID_SIZE, "%d", getpid()) <= 0) {
281 PROF_WARN(
282 "Unable to parse filename pattern %s. Using the default name.",
283 FilenamePat);
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000284 return -1;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000285 }
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000286 }
Xinliang David Li315e49d2016-05-24 21:29:18 +0000287 } else if (FilenamePat[I] == 'h') {
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000288 if (!NumHosts++)
Xinliang David Li315e49d2016-05-24 21:29:18 +0000289 if (COMPILER_RT_GETHOSTNAME(Hostname, COMPILER_RT_MAX_HOSTLEN)) {
290 PROF_WARN(
291 "Unable to parse filename pattern %s. Using the default name.",
292 FilenamePat);
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000293 return -1;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000294 }
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000295 } else if (containsMergeSpecifier(FilenamePat, I)) {
296 if (MergingEnabled) {
Vedant Kumar8e2dd512016-06-14 17:23:13 +0000297 PROF_WARN("%%m specifier can only be specified once in %s.\n",
298 FilenamePat);
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000299 return -1;
300 }
301 MergingEnabled = 1;
302 if (FilenamePat[I] == 'm')
303 lprofCurFilename.MergePoolSize = 1;
304 else {
305 lprofCurFilename.MergePoolSize = FilenamePat[I] - '0';
306 I++; /* advance to 'm' */
307 }
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +0000308 }
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000309 }
310
Xinliang David Li7f08d122016-05-25 17:30:15 +0000311 lprofCurFilename.NumPids = NumPids;
312 lprofCurFilename.NumHosts = NumHosts;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000313 return 0;
314}
315
Xinliang David Li153e8b62016-06-10 20:35:01 +0000316static void parseAndSetFilename(const char *FilenamePat,
317 ProfileNameSpecifier PNS) {
Xinliang David Li7f08d122016-05-25 17:30:15 +0000318
Xinliang David Li153e8b62016-06-10 20:35:01 +0000319 const char *OldFilenamePat = lprofCurFilename.FilenamePat;
320 ProfileNameSpecifier OldPNS = lprofCurFilename.PNS;
321
322 if (PNS < OldPNS)
323 return;
324
325 if (!FilenamePat)
326 FilenamePat = DefaultProfileName;
327
328 /* When -fprofile-instr-generate=<path> is specified on the
329 * command line, each module will be instrumented with runtime
330 * init call to __llvm_profile_init function which calls
331 * __llvm_profile_override_default_filename. In most of the cases,
332 * the path will be identical, so bypass the parsing completely.
333 */
334 if (OldFilenamePat && !strcmp(OldFilenamePat, FilenamePat)) {
335 lprofCurFilename.PNS = PNS;
336 return;
337 }
338
339 /* When PNS >= OldPNS, the last one wins. */
Xinliang David Li7f08d122016-05-25 17:30:15 +0000340 if (!FilenamePat || parseFilenamePattern(FilenamePat))
341 resetFilenameToDefault();
Xinliang David Li153e8b62016-06-10 20:35:01 +0000342 lprofCurFilename.PNS = PNS;
Xinliang David Li7f08d122016-05-25 17:30:15 +0000343
Xinliang David Li153e8b62016-06-10 20:35:01 +0000344 if (!OldFilenamePat) {
345 PROF_NOTE("Set profile file path to \"%s\" via %s.\n",
346 lprofCurFilename.FilenamePat, getPNSStr(PNS));
347 } else {
348 PROF_NOTE("Override old profile path \"%s\" via %s to \"%s\" via %s.\n",
349 OldFilenamePat, getPNSStr(OldPNS), lprofCurFilename.FilenamePat,
350 getPNSStr(PNS));
351 }
Xinliang David Li7f08d122016-05-25 17:30:15 +0000352
Xinliang David Li153e8b62016-06-10 20:35:01 +0000353 if (!lprofCurFilename.MergePoolSize)
Xinliang David Li7f08d122016-05-25 17:30:15 +0000354 truncateCurrentFile();
355}
356
Xinliang David Li315e49d2016-05-24 21:29:18 +0000357/* Return buffer length that is required to store the current profile
358 * filename with PID and hostname substitutions. */
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000359/* The length to hold uint64_t followed by 2 digit pool id including '_' */
360#define SIGLEN 24
Xinliang David Li315e49d2016-05-24 21:29:18 +0000361static int getCurFilenameLength() {
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000362 int Len;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000363 if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000364 return 0;
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +0000365
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000366 if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
367 lprofCurFilename.MergePoolSize))
Xinliang David Li315e49d2016-05-24 21:29:18 +0000368 return strlen(lprofCurFilename.FilenamePat);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000369
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000370 Len = strlen(lprofCurFilename.FilenamePat) +
371 lprofCurFilename.NumPids * (strlen(lprofCurFilename.PidChars) - 2) +
372 lprofCurFilename.NumHosts * (strlen(lprofCurFilename.Hostname) - 2);
373 if (lprofCurFilename.MergePoolSize)
374 Len += SIGLEN;
375 return Len;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000376}
377
378/* Return the pointer to the current profile file name (after substituting
379 * PIDs and Hostnames in filename pattern. \p FilenameBuf is the buffer
380 * to store the resulting filename. If no substitution is needed, the
381 * current filename pattern string is directly returned. */
382static const char *getCurFilename(char *FilenameBuf) {
383 int I, J, PidLength, HostNameLength;
384 const char *FilenamePat = lprofCurFilename.FilenamePat;
385
386 if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
387 return 0;
388
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000389 if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
390 lprofCurFilename.MergePoolSize))
Xinliang David Li315e49d2016-05-24 21:29:18 +0000391 return lprofCurFilename.FilenamePat;
392
393 PidLength = strlen(lprofCurFilename.PidChars);
394 HostNameLength = strlen(lprofCurFilename.Hostname);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000395 /* Construct the new filename. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000396 for (I = 0, J = 0; FilenamePat[I]; ++I)
397 if (FilenamePat[I] == '%') {
398 if (FilenamePat[++I] == 'p') {
399 memcpy(FilenameBuf + J, lprofCurFilename.PidChars, PidLength);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000400 J += PidLength;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000401 } else if (FilenamePat[I] == 'h') {
402 memcpy(FilenameBuf + J, lprofCurFilename.Hostname, HostNameLength);
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000403 J += HostNameLength;
Xinliang David Lie2ce2e02016-06-08 23:43:56 +0000404 } else if (containsMergeSpecifier(FilenamePat, I)) {
405 char LoadModuleSignature[SIGLEN];
406 int S;
407 int ProfilePoolId = getpid() % lprofCurFilename.MergePoolSize;
408 S = snprintf(LoadModuleSignature, SIGLEN, "%" PRIu64 "_%d",
409 lprofGetLoadModuleSignature(), ProfilePoolId);
410 if (S == -1 || S > SIGLEN)
411 S = SIGLEN;
412 memcpy(FilenameBuf + J, LoadModuleSignature, S);
413 J += S;
414 if (FilenamePat[I] != 'm')
415 I++;
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000416 }
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000417 /* Drop any unknown substitutions. */
418 } else
Xinliang David Li315e49d2016-05-24 21:29:18 +0000419 FilenameBuf[J++] = FilenamePat[I];
420 FilenameBuf[J] = 0;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000421
Xinliang David Li315e49d2016-05-24 21:29:18 +0000422 return FilenameBuf;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000423}
424
Xinliang David Li315e49d2016-05-24 21:29:18 +0000425/* Returns the pointer to the environment variable
426 * string. Returns null if the env var is not set. */
427static const char *getFilenamePatFromEnv(void) {
Eric Christopherd641b532015-04-28 22:54:51 +0000428 const char *Filename = getenv("LLVM_PROFILE_FILE");
Eric Christopherd641b532015-04-28 22:54:51 +0000429 if (!Filename || !Filename[0])
Xinliang David Li51fe0022016-05-24 01:23:09 +0000430 return 0;
431 return Filename;
Eric Christopherd641b532015-04-28 22:54:51 +0000432}
433
Xinliang David Lieaf238d2016-07-20 04:26:09 +0000434COMPILER_RT_VISIBILITY
435const char *__llvm_profile_get_path_prefix(void) {
436 int Length;
437 char *FilenameBuf, *Prefix;
438 const char *Filename, *PrefixEnd;
439
440 if (lprofCurFilename.ProfilePathPrefix)
441 return lprofCurFilename.ProfilePathPrefix;
442
443 Length = getCurFilenameLength();
444 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
445 Filename = getCurFilename(FilenameBuf);
446 if (!Filename)
447 return "\0";
448
449 PrefixEnd = lprofFindLastDirSeparator(Filename);
450 if (!PrefixEnd)
451 return "\0";
452
453 Length = PrefixEnd - Filename + 1;
454 Prefix = (char *)malloc(Length + 1);
455 if (!Prefix) {
456 PROF_ERR("Failed to %s\n", "allocate memory.");
457 return "\0";
458 }
459 memcpy(Prefix, Filename, Length);
460 Prefix[Length] = '\0';
461 lprofCurFilename.ProfilePathPrefix = Prefix;
462 return Prefix;
463}
464
Xinliang David Li51fe0022016-05-24 01:23:09 +0000465/* This method is invoked by the runtime initialization hook
466 * InstrProfilingRuntime.o if it is linked in. Both user specified
467 * profile path via -fprofile-instr-generate= and LLVM_PROFILE_FILE
468 * environment variable can override this default value. */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000469COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000470void __llvm_profile_initialize_file(void) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000471 const char *FilenamePat;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000472
Xinliang David Li315e49d2016-05-24 21:29:18 +0000473 FilenamePat = getFilenamePatFromEnv();
Xinliang David Li153e8b62016-06-10 20:35:01 +0000474 parseAndSetFilename(FilenamePat, FilenamePat ? PNS_environment : PNS_default);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000475}
476
Xinliang David Li51fe0022016-05-24 01:23:09 +0000477/* This API is directly called by the user application code. It has the
478 * highest precedence compared with LLVM_PROFILE_FILE environment variable
Xinliang David Li41518942016-05-24 02:37:07 +0000479 * and command line option -fprofile-instr-generate=<profile_name>.
Xinliang David Li51fe0022016-05-24 01:23:09 +0000480 */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000481COMPILER_RT_VISIBILITY
Xinliang David Li315e49d2016-05-24 21:29:18 +0000482void __llvm_profile_set_filename(const char *FilenamePat) {
Xinliang David Li153e8b62016-06-10 20:35:01 +0000483 parseAndSetFilename(FilenamePat, PNS_runtime_api);
Eric Christopherd641b532015-04-28 22:54:51 +0000484}
485
Xinliang David Li315e49d2016-05-24 21:29:18 +0000486/*
Xinliang David Li51fe0022016-05-24 01:23:09 +0000487 * This API is invoked by the global initializers emitted by Clang/LLVM when
488 * -fprofile-instr-generate=<..> is specified (vs -fprofile-instr-generate
489 * without an argument). This option has lower precedence than the
490 * LLVM_PROFILE_FILE environment variable.
491 */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000492COMPILER_RT_VISIBILITY
Xinliang David Li315e49d2016-05-24 21:29:18 +0000493void __llvm_profile_override_default_filename(const char *FilenamePat) {
Xinliang David Li153e8b62016-06-10 20:35:01 +0000494 parseAndSetFilename(FilenamePat, PNS_command_line);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000495}
496
Xinliang David Li315e49d2016-05-24 21:29:18 +0000497/* The public API for writing profile data into the file with name
498 * set by previous calls to __llvm_profile_set_filename or
499 * __llvm_profile_override_default_filename or
500 * __llvm_profile_initialize_file. */
Xinliang David Liabfd5532015-12-16 03:29:15 +0000501COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000502int __llvm_profile_write_file(void) {
Xinliang David Li315e49d2016-05-24 21:29:18 +0000503 int rc, Length;
504 const char *Filename;
505 char *FilenameBuf;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000506
Xinliang David Li315e49d2016-05-24 21:29:18 +0000507 Length = getCurFilenameLength();
508 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
509 Filename = getCurFilename(FilenameBuf);
510
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000511 /* Check the filename. */
Xinliang David Li315e49d2016-05-24 21:29:18 +0000512 if (!Filename) {
Xinliang David Li690c31f2016-05-20 05:15:42 +0000513 PROF_ERR("Failed to write file : %s\n", "Filename not set");
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000514 return -1;
Xinliang David Li9ea30642015-12-03 18:31:59 +0000515 }
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000516
Xinliang David Lid85c32c2016-01-08 23:42:28 +0000517 /* Check if there is llvm/runtime version mismatch. */
Xinliang David Lia6924212016-01-08 23:31:57 +0000518 if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) {
Xinliang David Li690c31f2016-05-20 05:15:42 +0000519 PROF_ERR("Runtime and instrumentation version mismatch : "
Xinliang David Lia6924212016-01-08 23:31:57 +0000520 "expected %d, but get %d\n",
521 INSTR_PROF_RAW_VERSION,
522 (int)GET_VERSION(__llvm_profile_get_version()));
523 return -1;
524 }
525
Xinliang David Li315e49d2016-05-24 21:29:18 +0000526 /* Write profile data to the file. */
527 rc = writeFile(Filename);
Xinliang David Li9ea30642015-12-03 18:31:59 +0000528 if (rc)
Xinliang David Li315e49d2016-05-24 21:29:18 +0000529 PROF_ERR("Failed to write file \"%s\": %s\n", Filename, strerror(errno));
Justin Bogner66fd5c92015-01-16 20:10:56 +0000530 return rc;
Duncan P. N. Exon Smith54cc0e22014-03-20 19:23:55 +0000531}
532
Xinliang David Li6fe18f42015-11-23 04:38:17 +0000533static void writeFileWithoutReturn(void) { __llvm_profile_write_file(); }
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000534
Xinliang David Liabfd5532015-12-16 03:29:15 +0000535COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000536int __llvm_profile_register_write_file_atexit(void) {
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000537 static int HasBeenRegistered = 0;
538
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000539 if (HasBeenRegistered)
540 return 0;
541
Xinliang David Li4617aa72016-05-18 22:34:05 +0000542 lprofSetupValueProfiler();
543
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000544 HasBeenRegistered = 1;
545 return atexit(writeFileWithoutReturn);
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000546}