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