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
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)
201COMPILER_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)
206COMPILER_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
217COMPILER_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
221static const int ContinuousModeSupported = 1;
222static const int UseBiasVar = 1;
223/* TODO: If there are two DSOs, the second DSO initilization will truncate the
224 * first profile file. */
225static 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. */
228static void *BiasAddr = &INSTR_PROF_PROFILE_COUNTER_BIAS_VAR;
229static void *BiasDefaultAddr = &INSTR_PROF_PROFILE_COUNTER_BIAS_DEFAULT_VAR;
230static 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(Begin: DataBegin, End: DataEnd);
240 /* Get the file size. */
241 uint64_t FileSize = 0;
242 if (getProfileFileSizeForMerging(ProfileFile: File, ProfileFileSize: &FileSize))
243 return 1;
244
245 int Fileno = fileno(stream: 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, len: FileSize, PROT_READ | PROT_WRITE,
251 MAP_SHARED, fd: Fileno, offset: 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(Begin: (uintptr_t)CountersBegin, End: (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(Begin: (uintptr_t)BitmapBegin, End: (uintptr_t)BitmapEnd);
267 return 0;
268}
269#else
270static const int ContinuousModeSupported = 0;
271static const int UseBiasVar = 0;
272static const char *FileOpenMode = "a+b";
273static void *BiasAddr = NULL;
274static void *BiasDefaultAddr = NULL;
275static int mmapForContinuousMode(uint64_t CurrentFileOffset, FILE *File) {
276 return 0;
277}
278#endif
279
280static int isProfileMergeRequested(void) { return ProfileMergeRequested; }
281static void setProfileMergeRequested(int EnableMerge) {
282 ProfileMergeRequested = EnableMerge;
283}
284
285static FILE *ProfileFile = NULL;
286static FILE *getProfileFile(void) { return ProfileFile; }
287static void setProfileFile(FILE *File) { ProfileFile = File; }
288
289static int getCurFilenameLength(void);
290static const char *getCurFilename(char *FilenameBuf, int ForceUseBuf);
291static unsigned doMerging(void) {
292 return lprofCurFilename.MergePoolSize || isProfileMergeRequested();
293}
294
295/* Return 1 if there is an error, otherwise return 0. */
296static 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(ptr: IOVecs[I].Data, size: IOVecs[I].ElmSize, n: IOVecs[I].NumElm, s: 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(ptr: Zeroes, size: sizeof(uint8_t), n: PartialWriteLen, s: File) !=
312 PartialWriteLen) {
313 return 1;
314 }
315 BytesToWrite -= PartialWriteLen;
316 }
317 } else {
318 if (fseek(stream: File, off: 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. */
327static uint32_t orderFileWriter(FILE *File, const uint32_t *DataStart) {
328 if (fwrite(ptr: DataStart, size: sizeof(uint32_t), INSTR_ORDER_FILE_BUFFER_SIZE, s: File) !=
329 INSTR_ORDER_FILE_BUFFER_SIZE)
330 return 1;
331 return 0;
332}
333
334static void initFileWriter(ProfDataWriter *This, FILE *File) {
335 This->Write = fileWriter;
336 This->WriterCtx = File;
337}
338
339COMPILER_RT_VISIBILITY ProfBufferIO *
340lprofCreateBufferIOInternal(void *File, uint32_t BufferSz) {
341 FreeHook = &free;
342 DynamicBufferIOBuffer = (uint8_t *)calloc(nmemb: 1, size: BufferSz);
343 VPBufferSize = BufferSz;
344 ProfDataWriter *fileWriter =
345 (ProfDataWriter *)calloc(nmemb: 1, size: sizeof(ProfDataWriter));
346 initFileWriter(This: fileWriter, File);
347 ProfBufferIO *IO = lprofCreateBufferIO(FileWriter: fileWriter);
348 IO->OwnFileWriter = 1;
349 return IO;
350}
351
352static void setupIOBuffer(void) {
353 const char *BufferSzStr = 0;
354 BufferSzStr = getenv(name: "LLVM_VP_BUFFER_SIZE");
355 if (BufferSzStr && BufferSzStr[0]) {
356 VPBufferSize = atoi(nptr: BufferSzStr);
357 DynamicBufferIOBuffer = (uint8_t *)calloc(nmemb: VPBufferSize, size: 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. */
365static int getProfileFileSizeForMerging(FILE *ProfileFile,
366 uint64_t *ProfileFileSize) {
367 if (fseek(stream: ProfileFile, off: 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(stream: ProfileFile);
373
374 /* Restore file offset. */
375 if (fseek(stream: ProfileFile, off: 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. */
395static int mmapProfileForMerging(FILE *ProfileFile, uint64_t ProfileFileSize,
396 char **ProfileBuffer) {
397 *ProfileBuffer = mmap(NULL, len: ProfileFileSize, PROT_READ, MAP_SHARED | MAP_FILE,
398 fd: fileno(stream: ProfileFile), offset: 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(Profile: *ProfileBuffer, Size: ProfileFileSize)) {
406 (void)munmap(addr: *ProfileBuffer, len: 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*/
419static 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: &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: &ProfileBuffer) == -1)
434 return -1;
435
436 /* Now start merging */
437 if (__llvm_profile_merge_from_buffer(Profile: ProfileBuffer, Size: ProfileFileSize)) {
438 PROF_ERR("%s\n", "Invalid profile data to merge");
439 (void)munmap(addr: ProfileBuffer, len: 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(addr: ProfileBuffer, len: ProfileFileSize);
449 *MergeDone = 1;
450
451 return 0;
452}
453
454/* Create the directory holding the file, if needed. */
455static void createProfileDir(const char *Filename) {
456 size_t Length = strlen(s: Filename);
457 if (lprofFindFirstDirSeparator(Path: Filename)) {
458 char *Copy = (char *)COMPILER_RT_ALLOCA(Length + 1);
459 strncpy(dest: Copy, src: Filename, n: Length + 1);
460 __llvm_profile_recursive_mkdir(Pathname: 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*/
472static 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(F: ProfileFile);
480 }
481 if (!ProfileFile) {
482 createProfileDir(Filename: ProfileFileName);
483 ProfileFile = lprofOpenFileEx(Filename: 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(stream: ProfileFile, off: 0L, SEEK_SET) == -1) {
491 PROF_ERR("Profile Merging of file %s failed: %s\n", ProfileFileName,
492 strerror(errno));
493 fclose(stream: ProfileFile);
494 return NULL;
495 }
496 return ProfileFile;
497}
498
499static FILE *getFileObject(const char *OutputName) {
500 FILE *File;
501 File = getProfileFile();
502 if (File != NULL) {
503 return File;
504 }
505
506 return fopen(filename: OutputName, modes: "ab");
507}
508
509/* Write profile data to file \c OutputName. */
510static 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(ProfileFileName: OutputName, MergeDone: &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(This: &fileWriter, File: OutputFile);
528 RetVal = lprofWriteData(Writer: &fileWriter, VPDataReader: lprofGetVPDataReader(), SkipNameDataWrite: MergeDone);
529
530 if (OutputFile == getProfileFile()) {
531 fflush(stream: OutputFile);
532 if (doMerging() && !__llvm_profile_is_continuous_mode_enabled()) {
533 lprofUnlockFileHandle(F: OutputFile);
534 }
535 } else {
536 fclose(stream: OutputFile);
537 }
538
539 return RetVal;
540}
541
542/* Write order data to file \c OutputName. */
543static int writeOrderFile(const char *OutputName) {
544 int RetVal;
545 FILE *OutputFile;
546
547 OutputFile = fopen(filename: OutputName, modes: "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(File: OutputFile, DataStart: DataBegin);
558
559 fclose(stream: OutputFile);
560 return RetVal;
561}
562
563#define LPROF_INIT_ONCE_ENV "__LLVM_PROFILE_RT_INIT_ONCE"
564
565static 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, ForceUseBuf: 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, replace: 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: Filename, modes: "w");
599 if (!File)
600 return;
601 fclose(stream: 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. */
606static int writeProfileWithFileObject(const char *Filename, FILE *File) {
607 setProfileFile(File);
608 int rc = writeFile(OutputName: 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
615static 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 Begin: __llvm_profile_begin_counters(), End: __llvm_profile_end_counters());
630
631 int Length = getCurFilenameLength();
632 char *FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
633 const char *Filename = getCurFilename(FilenameBuf, ForceUseBuf: 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(ProfileFile: File, ProfileFileSize: &ProfileFileSize) == -1) {
650 lprofUnlockFileHandle(F: File);
651 fclose(stream: 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(F: File);
659 fclose(stream: 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(ProfileFile: File, ProfileFileSize, ProfileBuffer: &ProfileBuffer) == -1) {
667 lprofUnlockFileHandle(F: File);
668 fclose(stream: File);
669 return;
670 }
671 (void)munmap(addr: ProfileBuffer, len: ProfileFileSize);
672 }
673 } else {
674 File = fopen(filename: Filename, modes: FileOpenMode);
675 if (!File)
676 return;
677 /* Check that the offset within the file is page-aligned. */
678 CurrentFileOffset = ftell(stream: 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(stream: File);
685 return;
686 }
687 if (writeProfileWithFileObject(Filename, File) != 0) {
688 fclose(stream: 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(F: File);
700 }
701 if (File != NULL) {
702 fclose(stream: File);
703 }
704}
705
706static const char *DefaultProfileName = "default.profraw";
707static 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(ptr: (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(s: &lprofCurFilename, c: 0, n: sizeof(lprofCurFilename));
724 lprofCurFilename.FilenamePat = DefaultProfileName;
725 lprofCurFilename.PNS = PNS_default;
726}
727
728static 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. */
748static 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. */
755static 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(s: 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(ptr: (void *)lprofCurFilename.ProfilePathPrefix);
773
774 if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) {
775 free(ptr: (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(s: &lprofCurFilename, c: 0, n: sizeof(lprofCurFilename));
784
785 if (!CopyFilenamePat)
786 lprofCurFilename.FilenamePat = FilenamePat;
787 else {
788 lprofCurFilename.FilenamePat = strdup(s: FilenamePat);
789 lprofCurFilename.OwnsFilenamePat = 1;
790 }
791 /* Check the filename for "%p", which indicates a pid-substitution. */
792 for (I = 0; checkBounds(Idx: I, Strlen: FilenamePatLen) && FilenamePat[I]; ++I) {
793 if (FilenamePat[I] == '%') {
794 ++I; /* Advance to the next character. */
795 if (!checkBounds(Idx: I, Strlen: FilenamePatLen))
796 break;
797 if (FilenamePat[I] == 'p') {
798 if (!NumPids++) {
799 if (snprintf(s: PidChars, MAX_PID_SIZE, format: "%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(name: "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(PageSize: 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: &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
857static 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(s1: OldFilenamePat, s2: 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(name: "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(name: "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
901static 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(s: lprofCurFilename.FilenamePat);
909
910 Len = strlen(s: lprofCurFilename.FilenamePat) +
911 lprofCurFilename.NumPids * (strlen(s: lprofCurFilename.PidChars) - 2) +
912 lprofCurFilename.NumHosts * (strlen(s: lprofCurFilename.Hostname) - 2) +
913 (lprofCurFilename.TmpDir ? (strlen(s: 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. */
924static 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(s: lprofCurFilename.FilenamePat);
938 memcpy(dest: FilenameBuf, src: lprofCurFilename.FilenamePat, n: FilenamePatLength);
939 FilenameBuf[FilenamePatLength] = '\0';
940 return FilenameBuf;
941 }
942
943 PidLength = strlen(s: lprofCurFilename.PidChars);
944 HostNameLength = strlen(s: lprofCurFilename.Hostname);
945 TmpDirLength = lprofCurFilename.TmpDir ? strlen(s: 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(dest: FilenameBuf + J, src: lprofCurFilename.PidChars, n: PidLength);
951 J += PidLength;
952 } else if (FilenamePat[I] == 'h') {
953 memcpy(dest: FilenameBuf + J, src: lprofCurFilename.Hostname, n: HostNameLength);
954 J += HostNameLength;
955 } else if (FilenamePat[I] == 't') {
956 memcpy(dest: FilenameBuf + J, src: lprofCurFilename.TmpDir, n: TmpDirLength);
957 FilenameBuf[J + TmpDirLength] = DIR_SEPARATOR;
958 J += TmpDirLength + 1;
959 } else {
960 if (!getMergePoolSize(FilenamePat, I: &I))
961 continue;
962 char LoadModuleSignature[SIGLEN + 1];
963 int S;
964 int ProfilePoolId = getpid() % lprofCurFilename.MergePoolSize;
965 S = snprintf(s: LoadModuleSignature, SIGLEN + 1, format: "%" PRIu64 "_%d",
966 lprofGetLoadModuleSignature(), ProfilePoolId);
967 if (S == -1 || S > SIGLEN)
968 S = SIGLEN;
969 memcpy(dest: FilenameBuf + J, src: LoadModuleSignature, n: 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. */
982static const char *getFilenamePatFromEnv(void) {
983 const char *Filename = getenv(name: "LLVM_PROFILE_FILE");
984 if (!Filename || !Filename[0])
985 return 0;
986 return Filename;
987}
988
989COMPILER_RT_VISIBILITY
990const 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, ForceUseBuf: 0);
1001 if (!Filename)
1002 return "\0";
1003
1004 PrefixEnd = lprofFindLastDirSeparator(Path: Filename);
1005 if (!PrefixEnd)
1006 return "\0";
1007
1008 Length = PrefixEnd - Filename + 1;
1009 Prefix = (char *)malloc(size: Length + 1);
1010 if (!Prefix) {
1011 PROF_ERR("Failed to %s\n", "allocate memory.");
1012 return "\0";
1013 }
1014 memcpy(dest: Prefix, src: Filename, n: Length);
1015 Prefix[Length] = '\0';
1016 lprofCurFilename.ProfilePathPrefix = Prefix;
1017 return Prefix;
1018}
1019
1020COMPILER_RT_VISIBILITY
1021const char *__llvm_profile_get_filename(void) {
1022 int Length;
1023 char *FilenameBuf;
1024 const char *Filename;
1025
1026 Length = getCurFilenameLength();
1027 FilenameBuf = (char *)malloc(size: Length + 1);
1028 if (!FilenameBuf) {
1029 PROF_ERR("Failed to %s\n", "allocate memory.");
1030 return "\0";
1031 }
1032 Filename = getCurFilename(FilenameBuf, ForceUseBuf: 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 */
1043COMPILER_RT_VISIBILITY
1044void __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(FilenamePat: EnvFilenamePat, PNS: PNS_environment, CopyFilenamePat: 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(FilenamePat: SelectedPat, PNS, CopyFilenamePat: 0);
1065}
1066
1067/* This method is invoked by the runtime initialization hook
1068 * InstrProfilingRuntime.o if it is linked in.
1069 */
1070COMPILER_RT_VISIBILITY
1071void __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 */
1081COMPILER_RT_VISIBILITY
1082void __llvm_profile_set_filename(const char *FilenamePat) {
1083 if (__llvm_profile_is_continuous_mode_enabled())
1084 return;
1085 parseAndSetFilename(FilenamePat, PNS: PNS_runtime_api, CopyFilenamePat: 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. */
1092COMPILER_RT_VISIBILITY
1093int __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, ForceUseBuf: 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(OutputName: 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
1143COMPILER_RT_VISIBILITY
1144int __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. */
1156static const char *OrderFileSuffix = ".order";
1157
1158COMPILER_RT_VISIBILITY
1159int __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(s: OrderFileSuffix);
1168 Length = getCurFilenameLength() + SuffixLength;
1169 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
1170 Filename = getCurFilename(FilenameBuf, ForceUseBuf: 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(s: Filename);
1182 memcpy(dest: FilenameBuf + LengthBeforeAppend, src: OrderFileSuffix, n: 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(OutputName: 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
1208COMPILER_RT_VISIBILITY
1209int __llvm_orderfile_dump(void) {
1210 int rc = __llvm_orderfile_write_file();
1211 return rc;
1212}
1213
1214static void writeFileWithoutReturn(void) { __llvm_profile_write_file(); }
1215
1216COMPILER_RT_VISIBILITY
1217int __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(func: writeFileWithoutReturn);
1227}
1228
1229COMPILER_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(F: 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(ProfileFile: File, ProfileFileSize: &ProfileFileSize) == -1) {
1244 lprofUnlockFileHandle(F: File);
1245 return 1;
1246 }
1247 if (ProfileFileSize == 0) {
1248 FreeHook = &free;
1249 setupIOBuffer();
1250 ProfDataWriter fileWriter;
1251 initFileWriter(This: &fileWriter, File);
1252 if (lprofWriteData(Writer: &fileWriter, VPDataReader: 0, SkipNameDataWrite: 0)) {
1253 lprofUnlockFileHandle(F: File);
1254 PROF_ERR("Failed to write file \"%d\": %s\n", fileno(File),
1255 strerror(errno));
1256 return 1;
1257 }
1258 fflush(stream: 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(ProfileFile: File, ProfileFileSize, ProfileBuffer: &ProfileBuffer) == -1) {
1264 lprofUnlockFileHandle(F: File);
1265 return 1;
1266 }
1267 (void)munmap(addr: ProfileBuffer, len: ProfileFileSize);
1268 }
1269 mmapForContinuousMode(CurrentFileOffset: 0, File);
1270 lprofUnlockFileHandle(F: File);
1271 } else {
1272 setProfileFile(File);
1273 setProfileMergeRequested(EnableMerge);
1274 }
1275 return 0;
1276}
1277
1278#endif
1279