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