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