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(1, BufferSz);
339   VPBufferSize = BufferSz;
340   ProfDataWriter *fileWriter =
341       (ProfDataWriter *)calloc(1, sizeof(ProfDataWriter));
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 #elif defined(__clang__)
706 #pragma clang diagnostic push
707 #pragma clang diagnostic ignored "-Wcast-qual"
708 #endif
709     free((void *)lprofCurFilename.FilenamePat);
710 #ifdef __GNUC__
711 #pragma GCC diagnostic pop
712 #elif defined(__clang__)
713 #pragma clang diagnostic pop
714 #endif
715   }
716   memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
717   lprofCurFilename.FilenamePat = DefaultProfileName;
718   lprofCurFilename.PNS = PNS_default;
719 }
720 
721 static unsigned getMergePoolSize(const char *FilenamePat, int *I) {
722   unsigned J = 0, Num = 0;
723   for (;; ++J) {
724     char C = FilenamePat[*I + J];
725     if (C == 'm') {
726       *I += J;
727       return Num ? Num : 1;
728     }
729     if (C < '0' || C > '9')
730       break;
731     Num = Num * 10 + C - '0';
732 
733     /* If FilenamePat[*I+J] is between '0' and '9', the next byte is guaranteed
734      * to be in-bound as the string is null terminated. */
735   }
736   return 0;
737 }
738 
739 /* Assert that Idx does index past a string null terminator. Return the
740  * result of the check. */
741 static int checkBounds(int Idx, int Strlen) {
742   assert(Idx <= Strlen && "Indexing past string null terminator");
743   return Idx <= Strlen;
744 }
745 
746 /* Parses the pattern string \p FilenamePat and stores the result to
747  * lprofcurFilename structure. */
748 static int parseFilenamePattern(const char *FilenamePat,
749                                 unsigned CopyFilenamePat) {
750   int NumPids = 0, NumHosts = 0, I;
751   char *PidChars = &lprofCurFilename.PidChars[0];
752   char *Hostname = &lprofCurFilename.Hostname[0];
753   int MergingEnabled = 0;
754   int FilenamePatLen = strlen(FilenamePat);
755 
756 #ifdef __GNUC__
757 #pragma GCC diagnostic push
758 #pragma GCC diagnostic ignored "-Wcast-qual"
759 #elif defined(__clang__)
760 #pragma clang diagnostic push
761 #pragma clang diagnostic ignored "-Wcast-qual"
762 #endif
763   /* Clean up cached prefix and filename.  */
764   if (lprofCurFilename.ProfilePathPrefix)
765     free((void *)lprofCurFilename.ProfilePathPrefix);
766 
767   if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) {
768     free((void *)lprofCurFilename.FilenamePat);
769   }
770 #ifdef __GNUC__
771 #pragma GCC diagnostic pop
772 #elif defined(__clang__)
773 #pragma clang diagnostic pop
774 #endif
775 
776   memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
777 
778   if (!CopyFilenamePat)
779     lprofCurFilename.FilenamePat = FilenamePat;
780   else {
781     lprofCurFilename.FilenamePat = strdup(FilenamePat);
782     lprofCurFilename.OwnsFilenamePat = 1;
783   }
784   /* Check the filename for "%p", which indicates a pid-substitution. */
785   for (I = 0; checkBounds(I, FilenamePatLen) && FilenamePat[I]; ++I) {
786     if (FilenamePat[I] == '%') {
787       ++I; /* Advance to the next character. */
788       if (!checkBounds(I, FilenamePatLen))
789         break;
790       if (FilenamePat[I] == 'p') {
791         if (!NumPids++) {
792           if (snprintf(PidChars, MAX_PID_SIZE, "%ld", (long)getpid()) <= 0) {
793             PROF_WARN("Unable to get pid for filename pattern %s. Using the "
794                       "default name.",
795                       FilenamePat);
796             return -1;
797           }
798         }
799       } else if (FilenamePat[I] == 'h') {
800         if (!NumHosts++)
801           if (COMPILER_RT_GETHOSTNAME(Hostname, COMPILER_RT_MAX_HOSTLEN)) {
802             PROF_WARN("Unable to get hostname for filename pattern %s. Using "
803                       "the default name.",
804                       FilenamePat);
805             return -1;
806           }
807       } else if (FilenamePat[I] == 't') {
808         lprofCurFilename.TmpDir = getenv("TMPDIR");
809         if (!lprofCurFilename.TmpDir) {
810           PROF_WARN("Unable to get the TMPDIR environment variable, referenced "
811                     "in %s. Using the default path.",
812                     FilenamePat);
813           return -1;
814         }
815       } else if (FilenamePat[I] == 'c') {
816         if (__llvm_profile_is_continuous_mode_enabled()) {
817           PROF_WARN("%%c specifier can only be specified once in %s.\n",
818                     FilenamePat);
819           __llvm_profile_disable_continuous_mode();
820           return -1;
821         }
822 #if defined(__APPLE__) || defined(__ELF__) || defined(_WIN32)
823         __llvm_profile_set_page_size(getpagesize());
824         __llvm_profile_enable_continuous_mode();
825 #else
826         PROF_WARN("%s", "Continous mode is currently only supported for Mach-O,"
827                         " ELF and COFF formats.");
828         return -1;
829 #endif
830       } else {
831         unsigned MergePoolSize = getMergePoolSize(FilenamePat, &I);
832         if (!MergePoolSize)
833           continue;
834         if (MergingEnabled) {
835           PROF_WARN("%%m specifier can only be specified once in %s.\n",
836                     FilenamePat);
837           return -1;
838         }
839         MergingEnabled = 1;
840         lprofCurFilename.MergePoolSize = MergePoolSize;
841       }
842     }
843   }
844 
845   lprofCurFilename.NumPids = NumPids;
846   lprofCurFilename.NumHosts = NumHosts;
847   return 0;
848 }
849 
850 static void parseAndSetFilename(const char *FilenamePat,
851                                 ProfileNameSpecifier PNS,
852                                 unsigned CopyFilenamePat) {
853 
854   const char *OldFilenamePat = lprofCurFilename.FilenamePat;
855   ProfileNameSpecifier OldPNS = lprofCurFilename.PNS;
856 
857   /* The old profile name specifier takes precedence over the old one. */
858   if (PNS < OldPNS)
859     return;
860 
861   if (!FilenamePat)
862     FilenamePat = DefaultProfileName;
863 
864   if (OldFilenamePat && !strcmp(OldFilenamePat, FilenamePat)) {
865     lprofCurFilename.PNS = PNS;
866     return;
867   }
868 
869   /* When PNS >= OldPNS, the last one wins. */
870   if (!FilenamePat || parseFilenamePattern(FilenamePat, CopyFilenamePat))
871     resetFilenameToDefault();
872   lprofCurFilename.PNS = PNS;
873 
874   if (!OldFilenamePat) {
875     if (getenv("LLVM_PROFILE_VERBOSE"))
876       PROF_NOTE("Set profile file path to \"%s\" via %s.\n",
877                 lprofCurFilename.FilenamePat, getPNSStr(PNS));
878   } else {
879     if (getenv("LLVM_PROFILE_VERBOSE"))
880       PROF_NOTE("Override old profile path \"%s\" via %s to \"%s\" via %s.\n",
881                 OldFilenamePat, getPNSStr(OldPNS), lprofCurFilename.FilenamePat,
882                 getPNSStr(PNS));
883   }
884 
885   truncateCurrentFile();
886   if (__llvm_profile_is_continuous_mode_enabled())
887     initializeProfileForContinuousMode();
888 }
889 
890 /* Return buffer length that is required to store the current profile
891  * filename with PID and hostname substitutions. */
892 /* The length to hold uint64_t followed by 3 digits pool id including '_' */
893 #define SIGLEN 24
894 static int getCurFilenameLength(void) {
895   int Len;
896   if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
897     return 0;
898 
899   if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
900         lprofCurFilename.TmpDir || lprofCurFilename.MergePoolSize))
901     return strlen(lprofCurFilename.FilenamePat);
902 
903   Len = strlen(lprofCurFilename.FilenamePat) +
904         lprofCurFilename.NumPids * (strlen(lprofCurFilename.PidChars) - 2) +
905         lprofCurFilename.NumHosts * (strlen(lprofCurFilename.Hostname) - 2) +
906         (lprofCurFilename.TmpDir ? (strlen(lprofCurFilename.TmpDir) - 1) : 0);
907   if (lprofCurFilename.MergePoolSize)
908     Len += SIGLEN;
909   return Len;
910 }
911 
912 /* Return the pointer to the current profile file name (after substituting
913  * PIDs and Hostnames in filename pattern. \p FilenameBuf is the buffer
914  * to store the resulting filename. If no substitution is needed, the
915  * current filename pattern string is directly returned, unless ForceUseBuf
916  * is enabled. */
917 static const char *getCurFilename(char *FilenameBuf, int ForceUseBuf) {
918   int I, J, PidLength, HostNameLength, TmpDirLength, FilenamePatLength;
919   const char *FilenamePat = lprofCurFilename.FilenamePat;
920 
921   if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
922     return 0;
923 
924   if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
925         lprofCurFilename.TmpDir || lprofCurFilename.MergePoolSize ||
926         __llvm_profile_is_continuous_mode_enabled())) {
927     if (!ForceUseBuf)
928       return lprofCurFilename.FilenamePat;
929 
930     FilenamePatLength = strlen(lprofCurFilename.FilenamePat);
931     memcpy(FilenameBuf, lprofCurFilename.FilenamePat, FilenamePatLength);
932     FilenameBuf[FilenamePatLength] = '\0';
933     return FilenameBuf;
934   }
935 
936   PidLength = strlen(lprofCurFilename.PidChars);
937   HostNameLength = strlen(lprofCurFilename.Hostname);
938   TmpDirLength = lprofCurFilename.TmpDir ? strlen(lprofCurFilename.TmpDir) : 0;
939   /* Construct the new filename. */
940   for (I = 0, J = 0; FilenamePat[I]; ++I)
941     if (FilenamePat[I] == '%') {
942       if (FilenamePat[++I] == 'p') {
943         memcpy(FilenameBuf + J, lprofCurFilename.PidChars, PidLength);
944         J += PidLength;
945       } else if (FilenamePat[I] == 'h') {
946         memcpy(FilenameBuf + J, lprofCurFilename.Hostname, HostNameLength);
947         J += HostNameLength;
948       } else if (FilenamePat[I] == 't') {
949         memcpy(FilenameBuf + J, lprofCurFilename.TmpDir, TmpDirLength);
950         FilenameBuf[J + TmpDirLength] = DIR_SEPARATOR;
951         J += TmpDirLength + 1;
952       } else {
953         if (!getMergePoolSize(FilenamePat, &I))
954           continue;
955         char LoadModuleSignature[SIGLEN + 1];
956         int S;
957         int ProfilePoolId = getpid() % lprofCurFilename.MergePoolSize;
958         S = snprintf(LoadModuleSignature, SIGLEN + 1, "%" PRIu64 "_%d",
959                      lprofGetLoadModuleSignature(), ProfilePoolId);
960         if (S == -1 || S > SIGLEN)
961           S = SIGLEN;
962         memcpy(FilenameBuf + J, LoadModuleSignature, S);
963         J += S;
964       }
965       /* Drop any unknown substitutions. */
966     } else
967       FilenameBuf[J++] = FilenamePat[I];
968   FilenameBuf[J] = 0;
969 
970   return FilenameBuf;
971 }
972 
973 /* Returns the pointer to the environment variable
974  * string. Returns null if the env var is not set. */
975 static const char *getFilenamePatFromEnv(void) {
976   const char *Filename = getenv("LLVM_PROFILE_FILE");
977   if (!Filename || !Filename[0])
978     return 0;
979   return Filename;
980 }
981 
982 COMPILER_RT_VISIBILITY
983 const char *__llvm_profile_get_path_prefix(void) {
984   int Length;
985   char *FilenameBuf, *Prefix;
986   const char *Filename, *PrefixEnd;
987 
988   if (lprofCurFilename.ProfilePathPrefix)
989     return lprofCurFilename.ProfilePathPrefix;
990 
991   Length = getCurFilenameLength();
992   FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
993   Filename = getCurFilename(FilenameBuf, 0);
994   if (!Filename)
995     return "\0";
996 
997   PrefixEnd = lprofFindLastDirSeparator(Filename);
998   if (!PrefixEnd)
999     return "\0";
1000 
1001   Length = PrefixEnd - Filename + 1;
1002   Prefix = (char *)malloc(Length + 1);
1003   if (!Prefix) {
1004     PROF_ERR("Failed to %s\n", "allocate memory.");
1005     return "\0";
1006   }
1007   memcpy(Prefix, Filename, Length);
1008   Prefix[Length] = '\0';
1009   lprofCurFilename.ProfilePathPrefix = Prefix;
1010   return Prefix;
1011 }
1012 
1013 COMPILER_RT_VISIBILITY
1014 const char *__llvm_profile_get_filename(void) {
1015   int Length;
1016   char *FilenameBuf;
1017   const char *Filename;
1018 
1019   Length = getCurFilenameLength();
1020   FilenameBuf = (char *)malloc(Length + 1);
1021   if (!FilenameBuf) {
1022     PROF_ERR("Failed to %s\n", "allocate memory.");
1023     return "\0";
1024   }
1025   Filename = getCurFilename(FilenameBuf, 1);
1026   if (!Filename)
1027     return "\0";
1028 
1029   return FilenameBuf;
1030 }
1031 
1032 /* This API initializes the file handling, both user specified
1033  * profile path via -fprofile-instr-generate= and LLVM_PROFILE_FILE
1034  * environment variable can override this default value.
1035  */
1036 COMPILER_RT_VISIBILITY
1037 void __llvm_profile_initialize_file(void) {
1038   const char *EnvFilenamePat;
1039   const char *SelectedPat = NULL;
1040   ProfileNameSpecifier PNS = PNS_unknown;
1041   int hasCommandLineOverrider = (INSTR_PROF_PROFILE_NAME_VAR[0] != 0);
1042 
1043   EnvFilenamePat = getFilenamePatFromEnv();
1044   if (EnvFilenamePat) {
1045     /* Pass CopyFilenamePat = 1, to ensure that the filename would be valid
1046        at the  moment when __llvm_profile_write_file() gets executed. */
1047     parseAndSetFilename(EnvFilenamePat, PNS_environment, 1);
1048     return;
1049   } else if (hasCommandLineOverrider) {
1050     SelectedPat = INSTR_PROF_PROFILE_NAME_VAR;
1051     PNS = PNS_command_line;
1052   } else {
1053     SelectedPat = NULL;
1054     PNS = PNS_default;
1055   }
1056 
1057   parseAndSetFilename(SelectedPat, PNS, 0);
1058 }
1059 
1060 /* This method is invoked by the runtime initialization hook
1061  * InstrProfilingRuntime.o if it is linked in.
1062  */
1063 COMPILER_RT_VISIBILITY
1064 void __llvm_profile_initialize(void) {
1065   __llvm_profile_initialize_file();
1066   if (!__llvm_profile_is_continuous_mode_enabled())
1067     __llvm_profile_register_write_file_atexit();
1068 }
1069 
1070 /* This API is directly called by the user application code. It has the
1071  * highest precedence compared with LLVM_PROFILE_FILE environment variable
1072  * and command line option -fprofile-instr-generate=<profile_name>.
1073  */
1074 COMPILER_RT_VISIBILITY
1075 void __llvm_profile_set_filename(const char *FilenamePat) {
1076   if (__llvm_profile_is_continuous_mode_enabled())
1077     return;
1078   parseAndSetFilename(FilenamePat, PNS_runtime_api, 1);
1079 }
1080 
1081 /* The public API for writing profile data into the file with name
1082  * set by previous calls to __llvm_profile_set_filename or
1083  * __llvm_profile_override_default_filename or
1084  * __llvm_profile_initialize_file. */
1085 COMPILER_RT_VISIBILITY
1086 int __llvm_profile_write_file(void) {
1087   int rc, Length;
1088   const char *Filename;
1089   char *FilenameBuf;
1090 
1091   // Temporarily suspend getting SIGKILL when the parent exits.
1092   int PDeathSig = lprofSuspendSigKill();
1093 
1094   if (lprofProfileDumped() || __llvm_profile_is_continuous_mode_enabled()) {
1095     PROF_NOTE("Profile data not written to file: %s.\n", "already written");
1096     if (PDeathSig == 1)
1097       lprofRestoreSigKill();
1098     return 0;
1099   }
1100 
1101   Length = getCurFilenameLength();
1102   FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
1103   Filename = getCurFilename(FilenameBuf, 0);
1104 
1105   /* Check the filename. */
1106   if (!Filename) {
1107     PROF_ERR("Failed to write file : %s\n", "Filename not set");
1108     if (PDeathSig == 1)
1109       lprofRestoreSigKill();
1110     return -1;
1111   }
1112 
1113   /* Check if there is llvm/runtime version mismatch.  */
1114   if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) {
1115     PROF_ERR("Runtime and instrumentation version mismatch : "
1116              "expected %d, but get %d\n",
1117              INSTR_PROF_RAW_VERSION,
1118              (int)GET_VERSION(__llvm_profile_get_version()));
1119     if (PDeathSig == 1)
1120       lprofRestoreSigKill();
1121     return -1;
1122   }
1123 
1124   /* Write profile data to the file. */
1125   rc = writeFile(Filename);
1126   if (rc)
1127     PROF_ERR("Failed to write file \"%s\": %s\n", Filename, strerror(errno));
1128 
1129   // Restore SIGKILL.
1130   if (PDeathSig == 1)
1131     lprofRestoreSigKill();
1132 
1133   return rc;
1134 }
1135 
1136 COMPILER_RT_VISIBILITY
1137 int __llvm_profile_dump(void) {
1138   if (!doMerging())
1139     PROF_WARN("Later invocation of __llvm_profile_dump can lead to clobbering "
1140               " of previously dumped profile data : %s. Either use %%m "
1141               "in profile name or change profile name before dumping.\n",
1142               "online profile merging is not on");
1143   int rc = __llvm_profile_write_file();
1144   lprofSetProfileDumped(1);
1145   return rc;
1146 }
1147 
1148 /* Order file data will be saved in a file with suffx .order. */
1149 static const char *OrderFileSuffix = ".order";
1150 
1151 COMPILER_RT_VISIBILITY
1152 int __llvm_orderfile_write_file(void) {
1153   int rc, Length, LengthBeforeAppend, SuffixLength;
1154   const char *Filename;
1155   char *FilenameBuf;
1156 
1157   // Temporarily suspend getting SIGKILL when the parent exits.
1158   int PDeathSig = lprofSuspendSigKill();
1159 
1160   SuffixLength = strlen(OrderFileSuffix);
1161   Length = getCurFilenameLength() + SuffixLength;
1162   FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
1163   Filename = getCurFilename(FilenameBuf, 1);
1164 
1165   /* Check the filename. */
1166   if (!Filename) {
1167     PROF_ERR("Failed to write file : %s\n", "Filename not set");
1168     if (PDeathSig == 1)
1169       lprofRestoreSigKill();
1170     return -1;
1171   }
1172 
1173   /* Append order file suffix */
1174   LengthBeforeAppend = strlen(Filename);
1175   memcpy(FilenameBuf + LengthBeforeAppend, OrderFileSuffix, SuffixLength);
1176   FilenameBuf[LengthBeforeAppend + SuffixLength] = '\0';
1177 
1178   /* Check if there is llvm/runtime version mismatch.  */
1179   if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) {
1180     PROF_ERR("Runtime and instrumentation version mismatch : "
1181              "expected %d, but get %d\n",
1182              INSTR_PROF_RAW_VERSION,
1183              (int)GET_VERSION(__llvm_profile_get_version()));
1184     if (PDeathSig == 1)
1185       lprofRestoreSigKill();
1186     return -1;
1187   }
1188 
1189   /* Write order data to the file. */
1190   rc = writeOrderFile(Filename);
1191   if (rc)
1192     PROF_ERR("Failed to write file \"%s\": %s\n", Filename, strerror(errno));
1193 
1194   // Restore SIGKILL.
1195   if (PDeathSig == 1)
1196     lprofRestoreSigKill();
1197 
1198   return rc;
1199 }
1200 
1201 COMPILER_RT_VISIBILITY
1202 int __llvm_orderfile_dump(void) {
1203   int rc = __llvm_orderfile_write_file();
1204   return rc;
1205 }
1206 
1207 static void writeFileWithoutReturn(void) { __llvm_profile_write_file(); }
1208 
1209 COMPILER_RT_VISIBILITY
1210 int __llvm_profile_register_write_file_atexit(void) {
1211   static int HasBeenRegistered = 0;
1212 
1213   if (HasBeenRegistered)
1214     return 0;
1215 
1216   lprofSetupValueProfiler();
1217 
1218   HasBeenRegistered = 1;
1219   return atexit(writeFileWithoutReturn);
1220 }
1221 
1222 COMPILER_RT_VISIBILITY int __llvm_profile_set_file_object(FILE *File,
1223                                                           int EnableMerge) {
1224   if (__llvm_profile_is_continuous_mode_enabled()) {
1225     if (!EnableMerge) {
1226       PROF_WARN("__llvm_profile_set_file_object(fd=%d) not supported in "
1227                 "continuous sync mode when merging is disabled\n",
1228                 fileno(File));
1229       return 1;
1230     }
1231     if (lprofLockFileHandle(File) != 0) {
1232       PROF_WARN("Data may be corrupted during profile merging : %s\n",
1233                 "Fail to obtain file lock due to system limit.");
1234     }
1235     uint64_t ProfileFileSize = 0;
1236     if (getProfileFileSizeForMerging(File, &ProfileFileSize) == -1) {
1237       lprofUnlockFileHandle(File);
1238       return 1;
1239     }
1240     if (ProfileFileSize == 0) {
1241       FreeHook = &free;
1242       setupIOBuffer();
1243       ProfDataWriter fileWriter;
1244       initFileWriter(&fileWriter, File);
1245       if (lprofWriteData(&fileWriter, 0, 0)) {
1246         lprofUnlockFileHandle(File);
1247         PROF_ERR("Failed to write file \"%d\": %s\n", fileno(File),
1248                  strerror(errno));
1249         return 1;
1250       }
1251       fflush(File);
1252     } else {
1253       /* The merged profile has a non-zero length. Check that it is compatible
1254        * with the data in this process. */
1255       char *ProfileBuffer;
1256       if (mmapProfileForMerging(File, ProfileFileSize, &ProfileBuffer) == -1) {
1257         lprofUnlockFileHandle(File);
1258         return 1;
1259       }
1260       (void)munmap(ProfileBuffer, ProfileFileSize);
1261     }
1262     mmapForContinuousMode(0, File);
1263     lprofUnlockFileHandle(File);
1264   } else {
1265     setProfileFile(File);
1266     setProfileMergeRequested(EnableMerge);
1267   }
1268   return 0;
1269 }
1270 
1271 #endif
1272