1 /*===- InstrProfilingFile.c - Write instrumentation to a file -------------===*\
2 |*
3 |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 |* See https://llvm.org/LICENSE.txt for license information.
5 |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 |*
7 \*===----------------------------------------------------------------------===*/
8
9 #if !defined(__Fuchsia__)
10
11 #include <assert.h>
12 #include <errno.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #ifdef _MSC_VER
17 /* For _alloca. */
18 #include <malloc.h>
19 #endif
20 #if defined(_WIN32)
21 #include "WindowsMMap.h"
22 /* For _chsize_s */
23 #include <io.h>
24 #include <process.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
33
34 #include "InstrProfiling.h"
35 #include "InstrProfilingInternal.h"
36 #include "InstrProfilingPort.h"
37 #include "InstrProfilingUtil.h"
38
39 /* From where is profile name specified.
40 * The order the enumerators define their
41 * precedence. Re-order them may lead to
42 * runtime behavior change. */
43 typedef enum ProfileNameSpecifier {
44 PNS_unknown = 0,
45 PNS_default,
46 PNS_command_line,
47 PNS_environment,
48 PNS_runtime_api
49 } ProfileNameSpecifier;
50
getPNSStr(ProfileNameSpecifier PNS)51 static const char *getPNSStr(ProfileNameSpecifier PNS) {
52 switch (PNS) {
53 case PNS_default:
54 return "default setting";
55 case PNS_command_line:
56 return "command line";
57 case PNS_environment:
58 return "environment variable";
59 case PNS_runtime_api:
60 return "runtime API";
61 default:
62 return "Unknown";
63 }
64 }
65
66 #define MAX_PID_SIZE 16
67 /* Data structure holding the result of parsed filename pattern. */
68 typedef struct lprofFilename {
69 /* File name string possibly with %p or %h specifiers. */
70 const char *FilenamePat;
71 /* A flag indicating if FilenamePat's memory is allocated
72 * by runtime. */
73 unsigned OwnsFilenamePat;
74 const char *ProfilePathPrefix;
75 char PidChars[MAX_PID_SIZE];
76 char *TmpDir;
77 char Hostname[COMPILER_RT_MAX_HOSTLEN];
78 unsigned NumPids;
79 unsigned NumHosts;
80 /* When in-process merging is enabled, this parameter specifies
81 * the total number of profile data files shared by all the processes
82 * spawned from the same binary. By default the value is 1. If merging
83 * is not enabled, its value should be 0. This parameter is specified
84 * by the %[0-9]m specifier. For instance %2m enables merging using
85 * 2 profile data files. %1m is equivalent to %m. Also %m specifier
86 * can only appear once at the end of the name pattern. */
87 unsigned MergePoolSize;
88 ProfileNameSpecifier PNS;
89 } lprofFilename;
90
91 static lprofFilename lprofCurFilename = {0, 0, 0, {0}, NULL,
92 {0}, 0, 0, 0, PNS_unknown};
93
94 static int ProfileMergeRequested = 0;
95 static int getProfileFileSizeForMerging(FILE *ProfileFile,
96 uint64_t *ProfileFileSize);
97
98 #if defined(__APPLE__)
99 static const int ContinuousModeSupported = 1;
100 static const int UseBiasVar = 0;
101 static const char *FileOpenMode = "a+b";
102 static void *BiasAddr = NULL;
103 static void *BiasDefaultAddr = NULL;
mmapForContinuousMode(uint64_t CurrentFileOffset,FILE * File)104 static int mmapForContinuousMode(uint64_t CurrentFileOffset, FILE *File) {
105 /* Get the sizes of various profile data sections. Taken from
106 * __llvm_profile_get_size_for_buffer(). */
107 const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
108 const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
109 const char *CountersBegin = __llvm_profile_begin_counters();
110 const char *CountersEnd = __llvm_profile_end_counters();
111 const char *NamesBegin = __llvm_profile_begin_names();
112 const char *NamesEnd = __llvm_profile_end_names();
113 const uint64_t NamesSize = (NamesEnd - NamesBegin) * sizeof(char);
114 uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd);
115 uint64_t CountersSize =
116 __llvm_profile_get_counters_size(CountersBegin, CountersEnd);
117
118 /* Check that the counter and data sections in this image are
119 * page-aligned. */
120 unsigned PageSize = getpagesize();
121 if ((intptr_t)CountersBegin % PageSize != 0) {
122 PROF_ERR("Counters section not page-aligned (start = %p, pagesz = %u).\n",
123 CountersBegin, PageSize);
124 return 1;
125 }
126 if ((intptr_t)DataBegin % PageSize != 0) {
127 PROF_ERR("Data section not page-aligned (start = %p, pagesz = %u).\n",
128 DataBegin, PageSize);
129 return 1;
130 }
131 int Fileno = fileno(File);
132 /* Determine how much padding is needed before/after the counters and
133 * after the names. */
134 uint64_t PaddingBytesBeforeCounters, PaddingBytesAfterCounters,
135 PaddingBytesAfterNames;
136 __llvm_profile_get_padding_sizes_for_counters(
137 DataSize, CountersSize, NamesSize, &PaddingBytesBeforeCounters,
138 &PaddingBytesAfterCounters, &PaddingBytesAfterNames);
139
140 uint64_t PageAlignedCountersLength = CountersSize + PaddingBytesAfterCounters;
141 uint64_t FileOffsetToCounters = CurrentFileOffset +
142 sizeof(__llvm_profile_header) + DataSize +
143 PaddingBytesBeforeCounters;
144 void *CounterMmap = mmap((void *)CountersBegin, PageAlignedCountersLength,
145 PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED,
146 Fileno, FileOffsetToCounters);
147 if (CounterMmap != CountersBegin) {
148 PROF_ERR(
149 "Continuous counter sync mode is enabled, but mmap() failed (%s).\n"
150 " - CountersBegin: %p\n"
151 " - PageAlignedCountersLength: %" PRIu64 "\n"
152 " - Fileno: %d\n"
153 " - FileOffsetToCounters: %" PRIu64 "\n",
154 strerror(errno), CountersBegin, PageAlignedCountersLength, Fileno,
155 FileOffsetToCounters);
156 return 1;
157 }
158 return 0;
159 }
160 #elif defined(__ELF__) || defined(_WIN32)
161
162 #define INSTR_PROF_PROFILE_COUNTER_BIAS_DEFAULT_VAR \
163 INSTR_PROF_CONCAT(INSTR_PROF_PROFILE_COUNTER_BIAS_VAR, _default)
164 COMPILER_RT_VISIBILITY intptr_t INSTR_PROF_PROFILE_COUNTER_BIAS_DEFAULT_VAR = 0;
165
166 /* This variable is a weak external reference which could be used to detect
167 * whether or not the compiler defined this symbol. */
168 #if defined(_MSC_VER)
169 COMPILER_RT_VISIBILITY extern intptr_t INSTR_PROF_PROFILE_COUNTER_BIAS_VAR;
170 #if defined(_M_IX86) || defined(__i386__)
171 #define WIN_SYM_PREFIX "_"
172 #else
173 #define WIN_SYM_PREFIX
174 #endif
175 #pragma comment( \
176 linker, "/alternatename:" WIN_SYM_PREFIX INSTR_PROF_QUOTE( \
177 INSTR_PROF_PROFILE_COUNTER_BIAS_VAR) "=" WIN_SYM_PREFIX \
178 INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_COUNTER_BIAS_DEFAULT_VAR))
179 #else
180 COMPILER_RT_VISIBILITY extern intptr_t INSTR_PROF_PROFILE_COUNTER_BIAS_VAR
181 __attribute__((weak, alias(INSTR_PROF_QUOTE(
182 INSTR_PROF_PROFILE_COUNTER_BIAS_DEFAULT_VAR))));
183 #endif
184 static const int ContinuousModeSupported = 1;
185 static const int UseBiasVar = 1;
186 /* TODO: If there are two DSOs, the second DSO initilization will truncate the
187 * first profile file. */
188 static const char *FileOpenMode = "w+b";
189 /* This symbol is defined by the compiler when runtime counter relocation is
190 * used and runtime provides a weak alias so we can check if it's defined. */
191 static void *BiasAddr = &INSTR_PROF_PROFILE_COUNTER_BIAS_VAR;
192 static void *BiasDefaultAddr = &INSTR_PROF_PROFILE_COUNTER_BIAS_DEFAULT_VAR;
mmapForContinuousMode(uint64_t CurrentFileOffset,FILE * File)193 static int mmapForContinuousMode(uint64_t CurrentFileOffset, FILE *File) {
194 /* Get the sizes of various profile data sections. Taken from
195 * __llvm_profile_get_size_for_buffer(). */
196 const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
197 const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
198 const char *CountersBegin = __llvm_profile_begin_counters();
199 const char *CountersEnd = __llvm_profile_end_counters();
200 uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd);
201 /* Get the file size. */
202 uint64_t FileSize = 0;
203 if (getProfileFileSizeForMerging(File, &FileSize))
204 return 1;
205
206 /* Map the profile. */
207 char *Profile = (char *)mmap(NULL, FileSize, PROT_READ | PROT_WRITE,
208 MAP_SHARED, fileno(File), 0);
209 if (Profile == MAP_FAILED) {
210 PROF_ERR("Unable to mmap profile: %s\n", strerror(errno));
211 return 1;
212 }
213 const uint64_t CountersOffsetInBiasMode =
214 sizeof(__llvm_profile_header) + __llvm_write_binary_ids(NULL) + DataSize;
215 /* Update the profile fields based on the current mapping. */
216 INSTR_PROF_PROFILE_COUNTER_BIAS_VAR =
217 (intptr_t)Profile - (uintptr_t)CountersBegin + CountersOffsetInBiasMode;
218
219 /* Return the memory allocated for counters to OS. */
220 lprofReleaseMemoryPagesToOS((uintptr_t)CountersBegin, (uintptr_t)CountersEnd);
221 return 0;
222 }
223 #else
224 static const int ContinuousModeSupported = 0;
225 static const int UseBiasVar = 0;
226 static const char *FileOpenMode = "a+b";
227 static void *BiasAddr = NULL;
228 static void *BiasDefaultAddr = NULL;
mmapForContinuousMode(uint64_t CurrentFileOffset,FILE * File)229 static int mmapForContinuousMode(uint64_t CurrentFileOffset, FILE *File) {
230 return 0;
231 }
232 #endif
233
isProfileMergeRequested(void)234 static int isProfileMergeRequested(void) { return ProfileMergeRequested; }
setProfileMergeRequested(int EnableMerge)235 static void setProfileMergeRequested(int EnableMerge) {
236 ProfileMergeRequested = EnableMerge;
237 }
238
239 static FILE *ProfileFile = NULL;
getProfileFile(void)240 static FILE *getProfileFile(void) { return ProfileFile; }
setProfileFile(FILE * File)241 static void setProfileFile(FILE *File) { ProfileFile = File; }
242
243 static int getCurFilenameLength(void);
244 static const char *getCurFilename(char *FilenameBuf, int ForceUseBuf);
doMerging(void)245 static unsigned doMerging(void) {
246 return lprofCurFilename.MergePoolSize || isProfileMergeRequested();
247 }
248
249 /* Return 1 if there is an error, otherwise return 0. */
fileWriter(ProfDataWriter * This,ProfDataIOVec * IOVecs,uint32_t NumIOVecs)250 static uint32_t fileWriter(ProfDataWriter *This, ProfDataIOVec *IOVecs,
251 uint32_t NumIOVecs) {
252 uint32_t I;
253 FILE *File = (FILE *)This->WriterCtx;
254 char Zeroes[sizeof(uint64_t)] = {0};
255 for (I = 0; I < NumIOVecs; I++) {
256 if (IOVecs[I].Data) {
257 if (fwrite(IOVecs[I].Data, IOVecs[I].ElmSize, IOVecs[I].NumElm, File) !=
258 IOVecs[I].NumElm)
259 return 1;
260 } else if (IOVecs[I].UseZeroPadding) {
261 size_t BytesToWrite = IOVecs[I].ElmSize * IOVecs[I].NumElm;
262 while (BytesToWrite > 0) {
263 size_t PartialWriteLen =
264 (sizeof(uint64_t) > BytesToWrite) ? BytesToWrite : sizeof(uint64_t);
265 if (fwrite(Zeroes, sizeof(uint8_t), PartialWriteLen, File) !=
266 PartialWriteLen) {
267 return 1;
268 }
269 BytesToWrite -= PartialWriteLen;
270 }
271 } else {
272 if (fseek(File, IOVecs[I].ElmSize * IOVecs[I].NumElm, SEEK_CUR) == -1)
273 return 1;
274 }
275 }
276 return 0;
277 }
278
279 /* TODO: make buffer size controllable by an internal option, and compiler can pass the size
280 to runtime via a variable. */
orderFileWriter(FILE * File,const uint32_t * DataStart)281 static uint32_t orderFileWriter(FILE *File, const uint32_t *DataStart) {
282 if (fwrite(DataStart, sizeof(uint32_t), INSTR_ORDER_FILE_BUFFER_SIZE, File) !=
283 INSTR_ORDER_FILE_BUFFER_SIZE)
284 return 1;
285 return 0;
286 }
287
initFileWriter(ProfDataWriter * This,FILE * File)288 static void initFileWriter(ProfDataWriter *This, FILE *File) {
289 This->Write = fileWriter;
290 This->WriterCtx = File;
291 }
292
293 COMPILER_RT_VISIBILITY ProfBufferIO *
lprofCreateBufferIOInternal(void * File,uint32_t BufferSz)294 lprofCreateBufferIOInternal(void *File, uint32_t BufferSz) {
295 FreeHook = &free;
296 DynamicBufferIOBuffer = (uint8_t *)calloc(BufferSz, 1);
297 VPBufferSize = BufferSz;
298 ProfDataWriter *fileWriter =
299 (ProfDataWriter *)calloc(sizeof(ProfDataWriter), 1);
300 initFileWriter(fileWriter, File);
301 ProfBufferIO *IO = lprofCreateBufferIO(fileWriter);
302 IO->OwnFileWriter = 1;
303 return IO;
304 }
305
setupIOBuffer(void)306 static void setupIOBuffer(void) {
307 const char *BufferSzStr = 0;
308 BufferSzStr = getenv("LLVM_VP_BUFFER_SIZE");
309 if (BufferSzStr && BufferSzStr[0]) {
310 VPBufferSize = atoi(BufferSzStr);
311 DynamicBufferIOBuffer = (uint8_t *)calloc(VPBufferSize, 1);
312 }
313 }
314
315 /* Get the size of the profile file. If there are any errors, print the
316 * message under the assumption that the profile is being read for merging
317 * purposes, and return -1. Otherwise return the file size in the inout param
318 * \p ProfileFileSize. */
getProfileFileSizeForMerging(FILE * ProfileFile,uint64_t * ProfileFileSize)319 static int getProfileFileSizeForMerging(FILE *ProfileFile,
320 uint64_t *ProfileFileSize) {
321 if (fseek(ProfileFile, 0L, SEEK_END) == -1) {
322 PROF_ERR("Unable to merge profile data, unable to get size: %s\n",
323 strerror(errno));
324 return -1;
325 }
326 *ProfileFileSize = ftell(ProfileFile);
327
328 /* Restore file offset. */
329 if (fseek(ProfileFile, 0L, SEEK_SET) == -1) {
330 PROF_ERR("Unable to merge profile data, unable to rewind: %s\n",
331 strerror(errno));
332 return -1;
333 }
334
335 if (*ProfileFileSize > 0 &&
336 *ProfileFileSize < sizeof(__llvm_profile_header)) {
337 PROF_WARN("Unable to merge profile data: %s\n",
338 "source profile file is too small.");
339 return -1;
340 }
341 return 0;
342 }
343
344 /* mmap() \p ProfileFile for profile merging purposes, assuming that an
345 * exclusive lock is held on the file and that \p ProfileFileSize is the
346 * length of the file. Return the mmap'd buffer in the inout variable
347 * \p ProfileBuffer. Returns -1 on failure. On success, the caller is
348 * responsible for unmapping the mmap'd buffer in \p ProfileBuffer. */
mmapProfileForMerging(FILE * ProfileFile,uint64_t ProfileFileSize,char ** ProfileBuffer)349 static int mmapProfileForMerging(FILE *ProfileFile, uint64_t ProfileFileSize,
350 char **ProfileBuffer) {
351 *ProfileBuffer = mmap(NULL, ProfileFileSize, PROT_READ, MAP_SHARED | MAP_FILE,
352 fileno(ProfileFile), 0);
353 if (*ProfileBuffer == MAP_FAILED) {
354 PROF_ERR("Unable to merge profile data, mmap failed: %s\n",
355 strerror(errno));
356 return -1;
357 }
358
359 if (__llvm_profile_check_compatibility(*ProfileBuffer, ProfileFileSize)) {
360 (void)munmap(*ProfileBuffer, ProfileFileSize);
361 PROF_WARN("Unable to merge profile data: %s\n",
362 "source profile file is not compatible.");
363 return -1;
364 }
365 return 0;
366 }
367
368 /* Read profile data in \c ProfileFile and merge with in-memory
369 profile counters. Returns -1 if there is fatal error, otheriwse
370 0 is returned. Returning 0 does not mean merge is actually
371 performed. If merge is actually done, *MergeDone is set to 1.
372 */
doProfileMerging(FILE * ProfileFile,int * MergeDone)373 static int doProfileMerging(FILE *ProfileFile, int *MergeDone) {
374 uint64_t ProfileFileSize;
375 char *ProfileBuffer;
376
377 /* Get the size of the profile on disk. */
378 if (getProfileFileSizeForMerging(ProfileFile, &ProfileFileSize) == -1)
379 return -1;
380
381 /* Nothing to merge. */
382 if (!ProfileFileSize)
383 return 0;
384
385 /* mmap() the profile and check that it is compatible with the data in
386 * the current image. */
387 if (mmapProfileForMerging(ProfileFile, ProfileFileSize, &ProfileBuffer) == -1)
388 return -1;
389
390 /* Now start merging */
391 if (__llvm_profile_merge_from_buffer(ProfileBuffer, ProfileFileSize)) {
392 PROF_ERR("%s\n", "Invalid profile data to merge");
393 (void)munmap(ProfileBuffer, ProfileFileSize);
394 return -1;
395 }
396
397 // Truncate the file in case merging of value profile did not happen to
398 // prevent from leaving garbage data at the end of the profile file.
399 (void)COMPILER_RT_FTRUNCATE(ProfileFile,
400 __llvm_profile_get_size_for_buffer());
401
402 (void)munmap(ProfileBuffer, ProfileFileSize);
403 *MergeDone = 1;
404
405 return 0;
406 }
407
408 /* Create the directory holding the file, if needed. */
createProfileDir(const char * Filename)409 static void createProfileDir(const char *Filename) {
410 size_t Length = strlen(Filename);
411 if (lprofFindFirstDirSeparator(Filename)) {
412 char *Copy = (char *)COMPILER_RT_ALLOCA(Length + 1);
413 strncpy(Copy, Filename, Length + 1);
414 __llvm_profile_recursive_mkdir(Copy);
415 }
416 }
417
418 /* Open the profile data for merging. It opens the file in r+b mode with
419 * file locking. If the file has content which is compatible with the
420 * current process, it also reads in the profile data in the file and merge
421 * it with in-memory counters. After the profile data is merged in memory,
422 * the original profile data is truncated and gets ready for the profile
423 * dumper. With profile merging enabled, each executable as well as any of
424 * its instrumented shared libraries dump profile data into their own data file.
425 */
openFileForMerging(const char * ProfileFileName,int * MergeDone)426 static FILE *openFileForMerging(const char *ProfileFileName, int *MergeDone) {
427 FILE *ProfileFile = NULL;
428 int rc;
429
430 ProfileFile = getProfileFile();
431 if (ProfileFile) {
432 lprofLockFileHandle(ProfileFile);
433 } else {
434 createProfileDir(ProfileFileName);
435 ProfileFile = lprofOpenFileEx(ProfileFileName);
436 }
437 if (!ProfileFile)
438 return NULL;
439
440 rc = doProfileMerging(ProfileFile, MergeDone);
441 if (rc || (!*MergeDone && COMPILER_RT_FTRUNCATE(ProfileFile, 0L)) ||
442 fseek(ProfileFile, 0L, SEEK_SET) == -1) {
443 PROF_ERR("Profile Merging of file %s failed: %s\n", ProfileFileName,
444 strerror(errno));
445 fclose(ProfileFile);
446 return NULL;
447 }
448 return ProfileFile;
449 }
450
getFileObject(const char * OutputName)451 static FILE *getFileObject(const char *OutputName) {
452 FILE *File;
453 File = getProfileFile();
454 if (File != NULL) {
455 return File;
456 }
457
458 return fopen(OutputName, "ab");
459 }
460
461 /* Write profile data to file \c OutputName. */
writeFile(const char * OutputName)462 static int writeFile(const char *OutputName) {
463 int RetVal;
464 FILE *OutputFile;
465
466 int MergeDone = 0;
467 VPMergeHook = &lprofMergeValueProfData;
468 if (doMerging())
469 OutputFile = openFileForMerging(OutputName, &MergeDone);
470 else
471 OutputFile = getFileObject(OutputName);
472
473 if (!OutputFile)
474 return -1;
475
476 FreeHook = &free;
477 setupIOBuffer();
478 ProfDataWriter fileWriter;
479 initFileWriter(&fileWriter, OutputFile);
480 RetVal = lprofWriteData(&fileWriter, lprofGetVPDataReader(), MergeDone);
481
482 if (OutputFile == getProfileFile()) {
483 fflush(OutputFile);
484 if (doMerging()) {
485 lprofUnlockFileHandle(OutputFile);
486 }
487 } else {
488 fclose(OutputFile);
489 }
490
491 return RetVal;
492 }
493
494 /* Write order data to file \c OutputName. */
writeOrderFile(const char * OutputName)495 static int writeOrderFile(const char *OutputName) {
496 int RetVal;
497 FILE *OutputFile;
498
499 OutputFile = fopen(OutputName, "w");
500
501 if (!OutputFile) {
502 PROF_WARN("can't open file with mode ab: %s\n", OutputName);
503 return -1;
504 }
505
506 FreeHook = &free;
507 setupIOBuffer();
508 const uint32_t *DataBegin = __llvm_profile_begin_orderfile();
509 RetVal = orderFileWriter(OutputFile, DataBegin);
510
511 fclose(OutputFile);
512 return RetVal;
513 }
514
515 #define LPROF_INIT_ONCE_ENV "__LLVM_PROFILE_RT_INIT_ONCE"
516
truncateCurrentFile(void)517 static void truncateCurrentFile(void) {
518 const char *Filename;
519 char *FilenameBuf;
520 FILE *File;
521 int Length;
522
523 Length = getCurFilenameLength();
524 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
525 Filename = getCurFilename(FilenameBuf, 0);
526 if (!Filename)
527 return;
528
529 /* Only create the profile directory and truncate an existing profile once.
530 * In continuous mode, this is necessary, as the profile is written-to by the
531 * runtime initializer. */
532 int initialized = getenv(LPROF_INIT_ONCE_ENV) != NULL;
533 if (initialized)
534 return;
535 #if defined(_WIN32)
536 _putenv(LPROF_INIT_ONCE_ENV "=" LPROF_INIT_ONCE_ENV);
537 #else
538 setenv(LPROF_INIT_ONCE_ENV, LPROF_INIT_ONCE_ENV, 1);
539 #endif
540
541 /* Create the profile dir (even if online merging is enabled), so that
542 * the profile file can be set up if continuous mode is enabled. */
543 createProfileDir(Filename);
544
545 /* By pass file truncation to allow online raw profile merging. */
546 if (lprofCurFilename.MergePoolSize)
547 return;
548
549 /* Truncate the file. Later we'll reopen and append. */
550 File = fopen(Filename, "w");
551 if (!File)
552 return;
553 fclose(File);
554 }
555
556 /* Write a partial profile to \p Filename, which is required to be backed by
557 * the open file object \p File. */
writeProfileWithFileObject(const char * Filename,FILE * File)558 static int writeProfileWithFileObject(const char *Filename, FILE *File) {
559 setProfileFile(File);
560 int rc = writeFile(Filename);
561 if (rc)
562 PROF_ERR("Failed to write file \"%s\": %s\n", Filename, strerror(errno));
563 setProfileFile(NULL);
564 return rc;
565 }
566
initializeProfileForContinuousMode(void)567 static void initializeProfileForContinuousMode(void) {
568 if (!__llvm_profile_is_continuous_mode_enabled())
569 return;
570 if (!ContinuousModeSupported) {
571 PROF_ERR("%s\n", "continuous mode is unsupported on this platform");
572 return;
573 }
574 if (UseBiasVar && BiasAddr == BiasDefaultAddr) {
575 PROF_ERR("%s\n", "__llvm_profile_counter_bias is undefined");
576 return;
577 }
578
579 /* Get the sizes of counter section. */
580 uint64_t CountersSize = __llvm_profile_get_counters_size(
581 __llvm_profile_begin_counters(), __llvm_profile_end_counters());
582
583 int Length = getCurFilenameLength();
584 char *FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
585 const char *Filename = getCurFilename(FilenameBuf, 0);
586 if (!Filename)
587 return;
588
589 FILE *File = NULL;
590 uint64_t CurrentFileOffset = 0;
591 if (doMerging()) {
592 /* We are merging profiles. Map the counter section as shared memory into
593 * the profile, i.e. into each participating process. An increment in one
594 * process should be visible to every other process with the same counter
595 * section mapped. */
596 File = lprofOpenFileEx(Filename);
597 if (!File)
598 return;
599
600 uint64_t ProfileFileSize = 0;
601 if (getProfileFileSizeForMerging(File, &ProfileFileSize) == -1) {
602 lprofUnlockFileHandle(File);
603 fclose(File);
604 return;
605 }
606 if (ProfileFileSize == 0) {
607 /* Grow the profile so that mmap() can succeed. Leak the file handle, as
608 * the file should stay open. */
609 if (writeProfileWithFileObject(Filename, File) != 0) {
610 lprofUnlockFileHandle(File);
611 fclose(File);
612 return;
613 }
614 } else {
615 /* The merged profile has a non-zero length. Check that it is compatible
616 * with the data in this process. */
617 char *ProfileBuffer;
618 if (mmapProfileForMerging(File, ProfileFileSize, &ProfileBuffer) == -1) {
619 lprofUnlockFileHandle(File);
620 fclose(File);
621 return;
622 }
623 (void)munmap(ProfileBuffer, ProfileFileSize);
624 }
625 } else {
626 File = fopen(Filename, FileOpenMode);
627 if (!File)
628 return;
629 /* Check that the offset within the file is page-aligned. */
630 CurrentFileOffset = ftell(File);
631 unsigned PageSize = getpagesize();
632 if (CurrentFileOffset % PageSize != 0) {
633 PROF_ERR("Continuous counter sync mode is enabled, but raw profile is not"
634 "page-aligned. CurrentFileOffset = %" PRIu64 ", pagesz = %u.\n",
635 (uint64_t)CurrentFileOffset, PageSize);
636 return;
637 }
638 if (writeProfileWithFileObject(Filename, File) != 0) {
639 fclose(File);
640 return;
641 }
642 }
643
644 /* mmap() the profile counters so long as there is at least one counter.
645 * If there aren't any counters, mmap() would fail with EINVAL. */
646 if (CountersSize > 0)
647 mmapForContinuousMode(CurrentFileOffset, File);
648
649 if (doMerging()) {
650 lprofUnlockFileHandle(File);
651 fclose(File);
652 }
653 }
654
655 static const char *DefaultProfileName = "default.profraw";
resetFilenameToDefault(void)656 static void resetFilenameToDefault(void) {
657 if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) {
658 free((void *)lprofCurFilename.FilenamePat);
659 }
660 memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
661 lprofCurFilename.FilenamePat = DefaultProfileName;
662 lprofCurFilename.PNS = PNS_default;
663 }
664
getMergePoolSize(const char * FilenamePat,int * I)665 static unsigned getMergePoolSize(const char *FilenamePat, int *I) {
666 unsigned J = 0, Num = 0;
667 for (;; ++J) {
668 char C = FilenamePat[*I + J];
669 if (C == 'm') {
670 *I += J;
671 return Num ? Num : 1;
672 }
673 if (C < '0' || C > '9')
674 break;
675 Num = Num * 10 + C - '0';
676
677 /* If FilenamePat[*I+J] is between '0' and '9', the next byte is guaranteed
678 * to be in-bound as the string is null terminated. */
679 }
680 return 0;
681 }
682
683 /* Assert that Idx does index past a string null terminator. Return the
684 * result of the check. */
checkBounds(int Idx,int Strlen)685 static int checkBounds(int Idx, int Strlen) {
686 assert(Idx <= Strlen && "Indexing past string null terminator");
687 return Idx <= Strlen;
688 }
689
690 /* Parses the pattern string \p FilenamePat and stores the result to
691 * lprofcurFilename structure. */
parseFilenamePattern(const char * FilenamePat,unsigned CopyFilenamePat)692 static int parseFilenamePattern(const char *FilenamePat,
693 unsigned CopyFilenamePat) {
694 int NumPids = 0, NumHosts = 0, I;
695 char *PidChars = &lprofCurFilename.PidChars[0];
696 char *Hostname = &lprofCurFilename.Hostname[0];
697 int MergingEnabled = 0;
698 int FilenamePatLen = strlen(FilenamePat);
699
700 /* Clean up cached prefix and filename. */
701 if (lprofCurFilename.ProfilePathPrefix)
702 free((void *)lprofCurFilename.ProfilePathPrefix);
703
704 if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) {
705 free((void *)lprofCurFilename.FilenamePat);
706 }
707
708 memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
709
710 if (!CopyFilenamePat)
711 lprofCurFilename.FilenamePat = FilenamePat;
712 else {
713 lprofCurFilename.FilenamePat = strdup(FilenamePat);
714 lprofCurFilename.OwnsFilenamePat = 1;
715 }
716 /* Check the filename for "%p", which indicates a pid-substitution. */
717 for (I = 0; checkBounds(I, FilenamePatLen) && FilenamePat[I]; ++I) {
718 if (FilenamePat[I] == '%') {
719 ++I; /* Advance to the next character. */
720 if (!checkBounds(I, FilenamePatLen))
721 break;
722 if (FilenamePat[I] == 'p') {
723 if (!NumPids++) {
724 if (snprintf(PidChars, MAX_PID_SIZE, "%ld", (long)getpid()) <= 0) {
725 PROF_WARN("Unable to get pid for filename pattern %s. Using the "
726 "default name.",
727 FilenamePat);
728 return -1;
729 }
730 }
731 } else if (FilenamePat[I] == 'h') {
732 if (!NumHosts++)
733 if (COMPILER_RT_GETHOSTNAME(Hostname, COMPILER_RT_MAX_HOSTLEN)) {
734 PROF_WARN("Unable to get hostname for filename pattern %s. Using "
735 "the default name.",
736 FilenamePat);
737 return -1;
738 }
739 } else if (FilenamePat[I] == 't') {
740 lprofCurFilename.TmpDir = getenv("TMPDIR");
741 if (!lprofCurFilename.TmpDir) {
742 PROF_WARN("Unable to get the TMPDIR environment variable, referenced "
743 "in %s. Using the default path.",
744 FilenamePat);
745 return -1;
746 }
747 } else if (FilenamePat[I] == 'c') {
748 if (__llvm_profile_is_continuous_mode_enabled()) {
749 PROF_WARN("%%c specifier can only be specified once in %s.\n",
750 FilenamePat);
751 return -1;
752 }
753 #if defined(__APPLE__) || defined(__ELF__) || defined(_WIN32)
754 __llvm_profile_set_page_size(getpagesize());
755 __llvm_profile_enable_continuous_mode();
756 #else
757 PROF_WARN("%s", "Continous mode is currently only supported for Mach-O,"
758 " ELF and COFF formats.");
759 return -1;
760 #endif
761 } else {
762 unsigned MergePoolSize = getMergePoolSize(FilenamePat, &I);
763 if (!MergePoolSize)
764 continue;
765 if (MergingEnabled) {
766 PROF_WARN("%%m specifier can only be specified once in %s.\n",
767 FilenamePat);
768 return -1;
769 }
770 MergingEnabled = 1;
771 lprofCurFilename.MergePoolSize = MergePoolSize;
772 }
773 }
774 }
775
776 lprofCurFilename.NumPids = NumPids;
777 lprofCurFilename.NumHosts = NumHosts;
778 return 0;
779 }
780
parseAndSetFilename(const char * FilenamePat,ProfileNameSpecifier PNS,unsigned CopyFilenamePat)781 static void parseAndSetFilename(const char *FilenamePat,
782 ProfileNameSpecifier PNS,
783 unsigned CopyFilenamePat) {
784
785 const char *OldFilenamePat = lprofCurFilename.FilenamePat;
786 ProfileNameSpecifier OldPNS = lprofCurFilename.PNS;
787
788 /* The old profile name specifier takes precedence over the old one. */
789 if (PNS < OldPNS)
790 return;
791
792 if (!FilenamePat)
793 FilenamePat = DefaultProfileName;
794
795 if (OldFilenamePat && !strcmp(OldFilenamePat, FilenamePat)) {
796 lprofCurFilename.PNS = PNS;
797 return;
798 }
799
800 /* When PNS >= OldPNS, the last one wins. */
801 if (!FilenamePat || parseFilenamePattern(FilenamePat, CopyFilenamePat))
802 resetFilenameToDefault();
803 lprofCurFilename.PNS = PNS;
804
805 if (!OldFilenamePat) {
806 if (getenv("LLVM_PROFILE_VERBOSE"))
807 PROF_NOTE("Set profile file path to \"%s\" via %s.\n",
808 lprofCurFilename.FilenamePat, getPNSStr(PNS));
809 } else {
810 if (getenv("LLVM_PROFILE_VERBOSE"))
811 PROF_NOTE("Override old profile path \"%s\" via %s to \"%s\" via %s.\n",
812 OldFilenamePat, getPNSStr(OldPNS), lprofCurFilename.FilenamePat,
813 getPNSStr(PNS));
814 }
815
816 truncateCurrentFile();
817 if (__llvm_profile_is_continuous_mode_enabled())
818 initializeProfileForContinuousMode();
819 }
820
821 /* Return buffer length that is required to store the current profile
822 * filename with PID and hostname substitutions. */
823 /* The length to hold uint64_t followed by 3 digits pool id including '_' */
824 #define SIGLEN 24
getCurFilenameLength(void)825 static int getCurFilenameLength(void) {
826 int Len;
827 if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
828 return 0;
829
830 if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
831 lprofCurFilename.TmpDir || lprofCurFilename.MergePoolSize))
832 return strlen(lprofCurFilename.FilenamePat);
833
834 Len = strlen(lprofCurFilename.FilenamePat) +
835 lprofCurFilename.NumPids * (strlen(lprofCurFilename.PidChars) - 2) +
836 lprofCurFilename.NumHosts * (strlen(lprofCurFilename.Hostname) - 2) +
837 (lprofCurFilename.TmpDir ? (strlen(lprofCurFilename.TmpDir) - 1) : 0);
838 if (lprofCurFilename.MergePoolSize)
839 Len += SIGLEN;
840 return Len;
841 }
842
843 /* Return the pointer to the current profile file name (after substituting
844 * PIDs and Hostnames in filename pattern. \p FilenameBuf is the buffer
845 * to store the resulting filename. If no substitution is needed, the
846 * current filename pattern string is directly returned, unless ForceUseBuf
847 * is enabled. */
getCurFilename(char * FilenameBuf,int ForceUseBuf)848 static const char *getCurFilename(char *FilenameBuf, int ForceUseBuf) {
849 int I, J, PidLength, HostNameLength, TmpDirLength, FilenamePatLength;
850 const char *FilenamePat = lprofCurFilename.FilenamePat;
851
852 if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
853 return 0;
854
855 if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
856 lprofCurFilename.TmpDir || lprofCurFilename.MergePoolSize ||
857 __llvm_profile_is_continuous_mode_enabled())) {
858 if (!ForceUseBuf)
859 return lprofCurFilename.FilenamePat;
860
861 FilenamePatLength = strlen(lprofCurFilename.FilenamePat);
862 memcpy(FilenameBuf, lprofCurFilename.FilenamePat, FilenamePatLength);
863 FilenameBuf[FilenamePatLength] = '\0';
864 return FilenameBuf;
865 }
866
867 PidLength = strlen(lprofCurFilename.PidChars);
868 HostNameLength = strlen(lprofCurFilename.Hostname);
869 TmpDirLength = lprofCurFilename.TmpDir ? strlen(lprofCurFilename.TmpDir) : 0;
870 /* Construct the new filename. */
871 for (I = 0, J = 0; FilenamePat[I]; ++I)
872 if (FilenamePat[I] == '%') {
873 if (FilenamePat[++I] == 'p') {
874 memcpy(FilenameBuf + J, lprofCurFilename.PidChars, PidLength);
875 J += PidLength;
876 } else if (FilenamePat[I] == 'h') {
877 memcpy(FilenameBuf + J, lprofCurFilename.Hostname, HostNameLength);
878 J += HostNameLength;
879 } else if (FilenamePat[I] == 't') {
880 memcpy(FilenameBuf + J, lprofCurFilename.TmpDir, TmpDirLength);
881 FilenameBuf[J + TmpDirLength] = DIR_SEPARATOR;
882 J += TmpDirLength + 1;
883 } else {
884 if (!getMergePoolSize(FilenamePat, &I))
885 continue;
886 char LoadModuleSignature[SIGLEN + 1];
887 int S;
888 int ProfilePoolId = getpid() % lprofCurFilename.MergePoolSize;
889 S = snprintf(LoadModuleSignature, SIGLEN + 1, "%" PRIu64 "_%d",
890 lprofGetLoadModuleSignature(), ProfilePoolId);
891 if (S == -1 || S > SIGLEN)
892 S = SIGLEN;
893 memcpy(FilenameBuf + J, LoadModuleSignature, S);
894 J += S;
895 }
896 /* Drop any unknown substitutions. */
897 } else
898 FilenameBuf[J++] = FilenamePat[I];
899 FilenameBuf[J] = 0;
900
901 return FilenameBuf;
902 }
903
904 /* Returns the pointer to the environment variable
905 * string. Returns null if the env var is not set. */
getFilenamePatFromEnv(void)906 static const char *getFilenamePatFromEnv(void) {
907 const char *Filename = getenv("LLVM_PROFILE_FILE");
908 if (!Filename || !Filename[0])
909 return 0;
910 return Filename;
911 }
912
913 COMPILER_RT_VISIBILITY
__llvm_profile_get_path_prefix(void)914 const char *__llvm_profile_get_path_prefix(void) {
915 int Length;
916 char *FilenameBuf, *Prefix;
917 const char *Filename, *PrefixEnd;
918
919 if (lprofCurFilename.ProfilePathPrefix)
920 return lprofCurFilename.ProfilePathPrefix;
921
922 Length = getCurFilenameLength();
923 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
924 Filename = getCurFilename(FilenameBuf, 0);
925 if (!Filename)
926 return "\0";
927
928 PrefixEnd = lprofFindLastDirSeparator(Filename);
929 if (!PrefixEnd)
930 return "\0";
931
932 Length = PrefixEnd - Filename + 1;
933 Prefix = (char *)malloc(Length + 1);
934 if (!Prefix) {
935 PROF_ERR("Failed to %s\n", "allocate memory.");
936 return "\0";
937 }
938 memcpy(Prefix, Filename, Length);
939 Prefix[Length] = '\0';
940 lprofCurFilename.ProfilePathPrefix = Prefix;
941 return Prefix;
942 }
943
944 COMPILER_RT_VISIBILITY
__llvm_profile_get_filename(void)945 const char *__llvm_profile_get_filename(void) {
946 int Length;
947 char *FilenameBuf;
948 const char *Filename;
949
950 Length = getCurFilenameLength();
951 FilenameBuf = (char *)malloc(Length + 1);
952 if (!FilenameBuf) {
953 PROF_ERR("Failed to %s\n", "allocate memory.");
954 return "\0";
955 }
956 Filename = getCurFilename(FilenameBuf, 1);
957 if (!Filename)
958 return "\0";
959
960 return FilenameBuf;
961 }
962
963 /* This API initializes the file handling, both user specified
964 * profile path via -fprofile-instr-generate= and LLVM_PROFILE_FILE
965 * environment variable can override this default value.
966 */
967 COMPILER_RT_VISIBILITY
__llvm_profile_initialize_file(void)968 void __llvm_profile_initialize_file(void) {
969 const char *EnvFilenamePat;
970 const char *SelectedPat = NULL;
971 ProfileNameSpecifier PNS = PNS_unknown;
972 int hasCommandLineOverrider = (INSTR_PROF_PROFILE_NAME_VAR[0] != 0);
973
974 EnvFilenamePat = getFilenamePatFromEnv();
975 if (EnvFilenamePat) {
976 /* Pass CopyFilenamePat = 1, to ensure that the filename would be valid
977 at the moment when __llvm_profile_write_file() gets executed. */
978 parseAndSetFilename(EnvFilenamePat, PNS_environment, 1);
979 return;
980 } else if (hasCommandLineOverrider) {
981 SelectedPat = INSTR_PROF_PROFILE_NAME_VAR;
982 PNS = PNS_command_line;
983 } else {
984 SelectedPat = NULL;
985 PNS = PNS_default;
986 }
987
988 parseAndSetFilename(SelectedPat, PNS, 0);
989 }
990
991 /* This method is invoked by the runtime initialization hook
992 * InstrProfilingRuntime.o if it is linked in.
993 */
994 COMPILER_RT_VISIBILITY
__llvm_profile_initialize(void)995 void __llvm_profile_initialize(void) {
996 __llvm_profile_initialize_file();
997 if (!__llvm_profile_is_continuous_mode_enabled())
998 __llvm_profile_register_write_file_atexit();
999 }
1000
1001 /* This API is directly called by the user application code. It has the
1002 * highest precedence compared with LLVM_PROFILE_FILE environment variable
1003 * and command line option -fprofile-instr-generate=<profile_name>.
1004 */
1005 COMPILER_RT_VISIBILITY
__llvm_profile_set_filename(const char * FilenamePat)1006 void __llvm_profile_set_filename(const char *FilenamePat) {
1007 if (__llvm_profile_is_continuous_mode_enabled())
1008 return;
1009 parseAndSetFilename(FilenamePat, PNS_runtime_api, 1);
1010 }
1011
1012 /* The public API for writing profile data into the file with name
1013 * set by previous calls to __llvm_profile_set_filename or
1014 * __llvm_profile_override_default_filename or
1015 * __llvm_profile_initialize_file. */
1016 COMPILER_RT_VISIBILITY
__llvm_profile_write_file(void)1017 int __llvm_profile_write_file(void) {
1018 int rc, Length;
1019 const char *Filename;
1020 char *FilenameBuf;
1021 int PDeathSig = 0;
1022
1023 if (lprofProfileDumped() || __llvm_profile_is_continuous_mode_enabled()) {
1024 PROF_NOTE("Profile data not written to file: %s.\n", "already written");
1025 return 0;
1026 }
1027
1028 Length = getCurFilenameLength();
1029 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
1030 Filename = getCurFilename(FilenameBuf, 0);
1031
1032 /* Check the filename. */
1033 if (!Filename) {
1034 PROF_ERR("Failed to write file : %s\n", "Filename not set");
1035 return -1;
1036 }
1037
1038 /* Check if there is llvm/runtime version mismatch. */
1039 if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) {
1040 PROF_ERR("Runtime and instrumentation version mismatch : "
1041 "expected %d, but get %d\n",
1042 INSTR_PROF_RAW_VERSION,
1043 (int)GET_VERSION(__llvm_profile_get_version()));
1044 return -1;
1045 }
1046
1047 // Temporarily suspend getting SIGKILL when the parent exits.
1048 PDeathSig = lprofSuspendSigKill();
1049
1050 /* Write profile data to the file. */
1051 rc = writeFile(Filename);
1052 if (rc)
1053 PROF_ERR("Failed to write file \"%s\": %s\n", Filename, strerror(errno));
1054
1055 // Restore SIGKILL.
1056 if (PDeathSig == 1)
1057 lprofRestoreSigKill();
1058
1059 return rc;
1060 }
1061
1062 COMPILER_RT_VISIBILITY
__llvm_profile_dump(void)1063 int __llvm_profile_dump(void) {
1064 if (!doMerging())
1065 PROF_WARN("Later invocation of __llvm_profile_dump can lead to clobbering "
1066 " of previously dumped profile data : %s. Either use %%m "
1067 "in profile name or change profile name before dumping.\n",
1068 "online profile merging is not on");
1069 int rc = __llvm_profile_write_file();
1070 lprofSetProfileDumped(1);
1071 return rc;
1072 }
1073
1074 /* Order file data will be saved in a file with suffx .order. */
1075 static const char *OrderFileSuffix = ".order";
1076
1077 COMPILER_RT_VISIBILITY
__llvm_orderfile_write_file(void)1078 int __llvm_orderfile_write_file(void) {
1079 int rc, Length, LengthBeforeAppend, SuffixLength;
1080 const char *Filename;
1081 char *FilenameBuf;
1082 int PDeathSig = 0;
1083
1084 SuffixLength = strlen(OrderFileSuffix);
1085 Length = getCurFilenameLength() + SuffixLength;
1086 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
1087 Filename = getCurFilename(FilenameBuf, 1);
1088
1089 /* Check the filename. */
1090 if (!Filename) {
1091 PROF_ERR("Failed to write file : %s\n", "Filename not set");
1092 return -1;
1093 }
1094
1095 /* Append order file suffix */
1096 LengthBeforeAppend = strlen(Filename);
1097 memcpy(FilenameBuf + LengthBeforeAppend, OrderFileSuffix, SuffixLength);
1098 FilenameBuf[LengthBeforeAppend + SuffixLength] = '\0';
1099
1100 /* Check if there is llvm/runtime version mismatch. */
1101 if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) {
1102 PROF_ERR("Runtime and instrumentation version mismatch : "
1103 "expected %d, but get %d\n",
1104 INSTR_PROF_RAW_VERSION,
1105 (int)GET_VERSION(__llvm_profile_get_version()));
1106 return -1;
1107 }
1108
1109 // Temporarily suspend getting SIGKILL when the parent exits.
1110 PDeathSig = lprofSuspendSigKill();
1111
1112 /* Write order data to the file. */
1113 rc = writeOrderFile(Filename);
1114 if (rc)
1115 PROF_ERR("Failed to write file \"%s\": %s\n", Filename, strerror(errno));
1116
1117 // Restore SIGKILL.
1118 if (PDeathSig == 1)
1119 lprofRestoreSigKill();
1120
1121 return rc;
1122 }
1123
1124 COMPILER_RT_VISIBILITY
__llvm_orderfile_dump(void)1125 int __llvm_orderfile_dump(void) {
1126 int rc = __llvm_orderfile_write_file();
1127 return rc;
1128 }
1129
writeFileWithoutReturn(void)1130 static void writeFileWithoutReturn(void) { __llvm_profile_write_file(); }
1131
1132 COMPILER_RT_VISIBILITY
__llvm_profile_register_write_file_atexit(void)1133 int __llvm_profile_register_write_file_atexit(void) {
1134 static int HasBeenRegistered = 0;
1135
1136 if (HasBeenRegistered)
1137 return 0;
1138
1139 lprofSetupValueProfiler();
1140
1141 HasBeenRegistered = 1;
1142 return atexit(writeFileWithoutReturn);
1143 }
1144
__llvm_profile_set_file_object(FILE * File,int EnableMerge)1145 COMPILER_RT_VISIBILITY int __llvm_profile_set_file_object(FILE *File,
1146 int EnableMerge) {
1147 if (__llvm_profile_is_continuous_mode_enabled()) {
1148 if (!EnableMerge) {
1149 PROF_WARN("__llvm_profile_set_file_object(fd=%d) not supported in "
1150 "continuous sync mode when merging is disabled\n",
1151 fileno(File));
1152 return 1;
1153 }
1154 if (lprofLockFileHandle(File) != 0) {
1155 PROF_WARN("Data may be corrupted during profile merging : %s\n",
1156 "Fail to obtain file lock due to system limit.");
1157 }
1158 uint64_t ProfileFileSize = 0;
1159 if (getProfileFileSizeForMerging(File, &ProfileFileSize) == -1) {
1160 lprofUnlockFileHandle(File);
1161 return 1;
1162 }
1163 if (ProfileFileSize == 0) {
1164 FreeHook = &free;
1165 setupIOBuffer();
1166 ProfDataWriter fileWriter;
1167 initFileWriter(&fileWriter, File);
1168 if (lprofWriteData(&fileWriter, 0, 0)) {
1169 lprofUnlockFileHandle(File);
1170 PROF_ERR("Failed to write file \"%d\": %s\n", fileno(File),
1171 strerror(errno));
1172 return 1;
1173 }
1174 fflush(File);
1175 } else {
1176 /* The merged profile has a non-zero length. Check that it is compatible
1177 * with the data in this process. */
1178 char *ProfileBuffer;
1179 if (mmapProfileForMerging(File, ProfileFileSize, &ProfileBuffer) == -1) {
1180 lprofUnlockFileHandle(File);
1181 return 1;
1182 }
1183 (void)munmap(ProfileBuffer, ProfileFileSize);
1184 }
1185 mmapForContinuousMode(0, File);
1186 lprofUnlockFileHandle(File);
1187 } else {
1188 setProfileFile(File);
1189 setProfileMergeRequested(EnableMerge);
1190 }
1191 return 0;
1192 }
1193
1194 #endif
1195