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