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