1 /*===- InstrProfilingFile.c - Write instrumentation to a file -------------===*\ 2 |* 3 |* The LLVM Compiler Infrastructure 4 |* 5 |* This file is distributed under the University of Illinois Open Source 6 |* License. See LICENSE.TXT for details. 7 |* 8 \*===----------------------------------------------------------------------===*/ 9 10 #if !defined(__Fuchsia__) 11 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 "InstrProfilingUtil.h" 37 38 /* From where is profile name specified. 39 * The order the enumerators define their 40 * precedence. Re-order them may lead to 41 * runtime behavior change. */ 42 typedef enum ProfileNameSpecifier { 43 PNS_unknown = 0, 44 PNS_default, 45 PNS_command_line, 46 PNS_environment, 47 PNS_runtime_api 48 } ProfileNameSpecifier; 49 50 static const char *getPNSStr(ProfileNameSpecifier PNS) { 51 switch (PNS) { 52 case PNS_default: 53 return "default setting"; 54 case PNS_command_line: 55 return "command line"; 56 case PNS_environment: 57 return "environment variable"; 58 case PNS_runtime_api: 59 return "runtime API"; 60 default: 61 return "Unknown"; 62 } 63 } 64 65 #define MAX_PID_SIZE 16 66 /* Data structure holding the result of parsed filename pattern. */ 67 typedef struct lprofFilename { 68 /* File name string possibly with %p or %h specifiers. */ 69 const char *FilenamePat; 70 /* A flag indicating if FilenamePat's memory is allocated 71 * by runtime. */ 72 unsigned OwnsFilenamePat; 73 const char *ProfilePathPrefix; 74 const char *Filename; 75 char PidChars[MAX_PID_SIZE]; 76 char Hostname[COMPILER_RT_MAX_HOSTLEN]; 77 unsigned NumPids; 78 unsigned NumHosts; 79 /* When in-process merging is enabled, this parameter specifies 80 * the total number of profile data files shared by all the processes 81 * spawned from the same binary. By default the value is 1. If merging 82 * is not enabled, its value should be 0. This parameter is specified 83 * by the %[0-9]m specifier. For instance %2m enables merging using 84 * 2 profile data files. %1m is equivalent to %m. Also %m specifier 85 * can only appear once at the end of the name pattern. */ 86 unsigned MergePoolSize; 87 ProfileNameSpecifier PNS; 88 } lprofFilename; 89 90 COMPILER_RT_WEAK lprofFilename lprofCurFilename = {0, 0, 0, 0, {0}, 91 {0}, 0, 0, 0, PNS_unknown}; 92 93 static int getCurFilenameLength(); 94 static const char *getCurFilename(char *FilenameBuf, int ForceUseBuf); 95 static unsigned doMerging() { return lprofCurFilename.MergePoolSize; } 96 97 /* Return 1 if there is an error, otherwise return 0. */ 98 static uint32_t fileWriter(ProfDataWriter *This, ProfDataIOVec *IOVecs, 99 uint32_t NumIOVecs) { 100 uint32_t I; 101 FILE *File = (FILE *)This->WriterCtx; 102 for (I = 0; I < NumIOVecs; I++) { 103 if (IOVecs[I].Data) { 104 if (fwrite(IOVecs[I].Data, IOVecs[I].ElmSize, IOVecs[I].NumElm, File) != 105 IOVecs[I].NumElm) 106 return 1; 107 } else { 108 if (fseek(File, IOVecs[I].ElmSize * IOVecs[I].NumElm, SEEK_CUR) == -1) 109 return 1; 110 } 111 } 112 return 0; 113 } 114 115 static void initFileWriter(ProfDataWriter *This, FILE *File) { 116 This->Write = fileWriter; 117 This->WriterCtx = File; 118 } 119 120 COMPILER_RT_VISIBILITY ProfBufferIO * 121 lprofCreateBufferIOInternal(void *File, uint32_t BufferSz) { 122 FreeHook = &free; 123 DynamicBufferIOBuffer = (uint8_t *)calloc(BufferSz, 1); 124 VPBufferSize = BufferSz; 125 ProfDataWriter *fileWriter = 126 (ProfDataWriter *)calloc(sizeof(ProfDataWriter), 1); 127 initFileWriter(fileWriter, File); 128 ProfBufferIO *IO = lprofCreateBufferIO(fileWriter); 129 IO->OwnFileWriter = 1; 130 return IO; 131 } 132 133 static void setupIOBuffer() { 134 const char *BufferSzStr = 0; 135 BufferSzStr = getenv("LLVM_VP_BUFFER_SIZE"); 136 if (BufferSzStr && BufferSzStr[0]) { 137 VPBufferSize = atoi(BufferSzStr); 138 DynamicBufferIOBuffer = (uint8_t *)calloc(VPBufferSize, 1); 139 } 140 } 141 142 /* Read profile data in \c ProfileFile and merge with in-memory 143 profile counters. Returns -1 if there is fatal error, otheriwse 144 0 is returned. Returning 0 does not mean merge is actually 145 performed. If merge is actually done, *MergeDone is set to 1. 146 */ 147 static int doProfileMerging(FILE *ProfileFile, int *MergeDone) { 148 uint64_t ProfileFileSize; 149 char *ProfileBuffer; 150 151 if (fseek(ProfileFile, 0L, SEEK_END) == -1) { 152 PROF_ERR("Unable to merge profile data, unable to get size: %s\n", 153 strerror(errno)); 154 return -1; 155 } 156 ProfileFileSize = ftell(ProfileFile); 157 158 /* Restore file offset. */ 159 if (fseek(ProfileFile, 0L, SEEK_SET) == -1) { 160 PROF_ERR("Unable to merge profile data, unable to rewind: %s\n", 161 strerror(errno)); 162 return -1; 163 } 164 165 /* Nothing to merge. */ 166 if (ProfileFileSize < sizeof(__llvm_profile_header)) { 167 if (ProfileFileSize) 168 PROF_WARN("Unable to merge profile data: %s\n", 169 "source profile file is too small."); 170 return 0; 171 } 172 173 ProfileBuffer = mmap(NULL, ProfileFileSize, PROT_READ, MAP_SHARED | MAP_FILE, 174 fileno(ProfileFile), 0); 175 if (ProfileBuffer == MAP_FAILED) { 176 PROF_ERR("Unable to merge profile data, mmap failed: %s\n", 177 strerror(errno)); 178 return -1; 179 } 180 181 if (__llvm_profile_check_compatibility(ProfileBuffer, ProfileFileSize)) { 182 (void)munmap(ProfileBuffer, ProfileFileSize); 183 PROF_WARN("Unable to merge profile data: %s\n", 184 "source profile file is not compatible."); 185 return 0; 186 } 187 188 /* Now start merging */ 189 __llvm_profile_merge_from_buffer(ProfileBuffer, ProfileFileSize); 190 191 // Truncate the file in case merging of value profile did not happend to 192 // prevent from leaving garbage data at the end of the profile file. 193 COMPILER_RT_FTRUNCATE(ProfileFile, __llvm_profile_get_size_for_buffer()); 194 195 (void)munmap(ProfileBuffer, ProfileFileSize); 196 *MergeDone = 1; 197 198 return 0; 199 } 200 201 /* Create the directory holding the file, if needed. */ 202 static void createProfileDir(const char *Filename) { 203 size_t Length = strlen(Filename); 204 if (lprofFindFirstDirSeparator(Filename)) { 205 char *Copy = (char *)COMPILER_RT_ALLOCA(Length + 1); 206 strncpy(Copy, Filename, Length + 1); 207 __llvm_profile_recursive_mkdir(Copy); 208 } 209 } 210 211 /* Open the profile data for merging. It opens the file in r+b mode with 212 * file locking. If the file has content which is compatible with the 213 * current process, it also reads in the profile data in the file and merge 214 * it with in-memory counters. After the profile data is merged in memory, 215 * the original profile data is truncated and gets ready for the profile 216 * dumper. With profile merging enabled, each executable as well as any of 217 * its instrumented shared libraries dump profile data into their own data file. 218 */ 219 static FILE *openFileForMerging(const char *ProfileFileName, int *MergeDone) { 220 FILE *ProfileFile; 221 int rc; 222 223 createProfileDir(ProfileFileName); 224 ProfileFile = lprofOpenFileEx(ProfileFileName); 225 if (!ProfileFile) 226 return NULL; 227 228 rc = doProfileMerging(ProfileFile, MergeDone); 229 if (rc || (!*MergeDone && COMPILER_RT_FTRUNCATE(ProfileFile, 0L)) || 230 fseek(ProfileFile, 0L, SEEK_SET) == -1) { 231 PROF_ERR("Profile Merging of file %s failed: %s\n", ProfileFileName, 232 strerror(errno)); 233 fclose(ProfileFile); 234 return NULL; 235 } 236 return ProfileFile; 237 } 238 239 /* Write profile data to file \c OutputName. */ 240 static int writeFile(const char *OutputName) { 241 int RetVal; 242 FILE *OutputFile; 243 244 int MergeDone = 0; 245 VPMergeHook = &lprofMergeValueProfData; 246 if (!doMerging()) 247 OutputFile = fopen(OutputName, "ab"); 248 else 249 OutputFile = openFileForMerging(OutputName, &MergeDone); 250 251 if (!OutputFile) 252 return -1; 253 254 FreeHook = &free; 255 setupIOBuffer(); 256 ProfDataWriter fileWriter; 257 initFileWriter(&fileWriter, OutputFile); 258 RetVal = lprofWriteData(&fileWriter, lprofGetVPDataReader(), MergeDone); 259 260 fclose(OutputFile); 261 return RetVal; 262 } 263 264 static void truncateCurrentFile(void) { 265 const char *Filename; 266 char *FilenameBuf; 267 FILE *File; 268 int Length; 269 270 Length = getCurFilenameLength(); 271 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1); 272 Filename = getCurFilename(FilenameBuf, 0); 273 if (!Filename) 274 return; 275 276 /* By pass file truncation to allow online raw profile 277 * merging. */ 278 if (lprofCurFilename.MergePoolSize) 279 return; 280 281 createProfileDir(Filename); 282 283 /* Truncate the file. Later we'll reopen and append. */ 284 File = fopen(Filename, "w"); 285 if (!File) 286 return; 287 fclose(File); 288 } 289 290 static const char *DefaultProfileName = "default.profraw"; 291 static void resetFilenameToDefault(void) { 292 if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) { 293 free((void *)lprofCurFilename.FilenamePat); 294 } 295 memset(&lprofCurFilename, 0, sizeof(lprofCurFilename)); 296 lprofCurFilename.FilenamePat = DefaultProfileName; 297 lprofCurFilename.PNS = PNS_default; 298 } 299 300 static int containsMergeSpecifier(const char *FilenamePat, int I) { 301 return (FilenamePat[I] == 'm' || 302 (FilenamePat[I] >= '1' && FilenamePat[I] <= '9' && 303 /* If FilenamePat[I] is not '\0', the next byte is guaranteed 304 * to be in-bound as the string is null terminated. */ 305 FilenamePat[I + 1] == 'm')); 306 } 307 308 /* Parses the pattern string \p FilenamePat and stores the result to 309 * lprofcurFilename structure. */ 310 static int parseFilenamePattern(const char *FilenamePat, 311 unsigned CopyFilenamePat) { 312 int NumPids = 0, NumHosts = 0, I; 313 char *PidChars = &lprofCurFilename.PidChars[0]; 314 char *Hostname = &lprofCurFilename.Hostname[0]; 315 int MergingEnabled = 0; 316 317 /* Clean up cached prefix and filename. */ 318 if (lprofCurFilename.ProfilePathPrefix) 319 free((void *)lprofCurFilename.ProfilePathPrefix); 320 if (lprofCurFilename.Filename) 321 free((void *)lprofCurFilename.Filename); 322 323 if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) { 324 free((void *)lprofCurFilename.FilenamePat); 325 } 326 327 memset(&lprofCurFilename, 0, sizeof(lprofCurFilename)); 328 329 if (!CopyFilenamePat) 330 lprofCurFilename.FilenamePat = FilenamePat; 331 else { 332 lprofCurFilename.FilenamePat = strdup(FilenamePat); 333 lprofCurFilename.OwnsFilenamePat = 1; 334 } 335 /* Check the filename for "%p", which indicates a pid-substitution. */ 336 for (I = 0; FilenamePat[I]; ++I) 337 if (FilenamePat[I] == '%') { 338 if (FilenamePat[++I] == 'p') { 339 if (!NumPids++) { 340 if (snprintf(PidChars, MAX_PID_SIZE, "%ld", (long)getpid()) <= 0) { 341 PROF_WARN("Unable to get pid for filename pattern %s. Using the " 342 "default name.", 343 FilenamePat); 344 return -1; 345 } 346 } 347 } else if (FilenamePat[I] == 'h') { 348 if (!NumHosts++) 349 if (COMPILER_RT_GETHOSTNAME(Hostname, COMPILER_RT_MAX_HOSTLEN)) { 350 PROF_WARN("Unable to get hostname for filename pattern %s. Using " 351 "the default name.", 352 FilenamePat); 353 return -1; 354 } 355 } else if (containsMergeSpecifier(FilenamePat, I)) { 356 if (MergingEnabled) { 357 PROF_WARN("%%m specifier can only be specified once in %s.\n", 358 FilenamePat); 359 return -1; 360 } 361 MergingEnabled = 1; 362 if (FilenamePat[I] == 'm') 363 lprofCurFilename.MergePoolSize = 1; 364 else { 365 lprofCurFilename.MergePoolSize = FilenamePat[I] - '0'; 366 I++; /* advance to 'm' */ 367 } 368 } 369 } 370 371 lprofCurFilename.NumPids = NumPids; 372 lprofCurFilename.NumHosts = NumHosts; 373 return 0; 374 } 375 376 static void parseAndSetFilename(const char *FilenamePat, 377 ProfileNameSpecifier PNS, 378 unsigned CopyFilenamePat) { 379 380 const char *OldFilenamePat = lprofCurFilename.FilenamePat; 381 ProfileNameSpecifier OldPNS = lprofCurFilename.PNS; 382 383 if (PNS < OldPNS) 384 return; 385 386 if (!FilenamePat) 387 FilenamePat = DefaultProfileName; 388 389 if (OldFilenamePat && !strcmp(OldFilenamePat, FilenamePat)) { 390 lprofCurFilename.PNS = PNS; 391 return; 392 } 393 394 /* When PNS >= OldPNS, the last one wins. */ 395 if (!FilenamePat || parseFilenamePattern(FilenamePat, CopyFilenamePat)) 396 resetFilenameToDefault(); 397 lprofCurFilename.PNS = PNS; 398 399 if (!OldFilenamePat) { 400 if (getenv("LLVM_PROFILE_VERBOSE")) 401 PROF_NOTE("Set profile file path to \"%s\" via %s.\n", 402 lprofCurFilename.FilenamePat, getPNSStr(PNS)); 403 } else { 404 if (getenv("LLVM_PROFILE_VERBOSE")) 405 PROF_NOTE("Override old profile path \"%s\" via %s to \"%s\" via %s.\n", 406 OldFilenamePat, getPNSStr(OldPNS), lprofCurFilename.FilenamePat, 407 getPNSStr(PNS)); 408 } 409 410 truncateCurrentFile(); 411 } 412 413 /* Return buffer length that is required to store the current profile 414 * filename with PID and hostname substitutions. */ 415 /* The length to hold uint64_t followed by 2 digit pool id including '_' */ 416 #define SIGLEN 24 417 static int getCurFilenameLength() { 418 int Len; 419 if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0]) 420 return 0; 421 422 if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts || 423 lprofCurFilename.MergePoolSize)) 424 return strlen(lprofCurFilename.FilenamePat); 425 426 Len = strlen(lprofCurFilename.FilenamePat) + 427 lprofCurFilename.NumPids * (strlen(lprofCurFilename.PidChars) - 2) + 428 lprofCurFilename.NumHosts * (strlen(lprofCurFilename.Hostname) - 2); 429 if (lprofCurFilename.MergePoolSize) 430 Len += SIGLEN; 431 return Len; 432 } 433 434 /* Return the pointer to the current profile file name (after substituting 435 * PIDs and Hostnames in filename pattern. \p FilenameBuf is the buffer 436 * to store the resulting filename. If no substitution is needed, the 437 * current filename pattern string is directly returned, unless ForceUseBuf 438 * is enabled. */ 439 static const char *getCurFilename(char *FilenameBuf, int ForceUseBuf) { 440 int I, J, PidLength, HostNameLength, FilenamePatLength; 441 const char *FilenamePat = lprofCurFilename.FilenamePat; 442 443 if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0]) 444 return 0; 445 446 if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts || 447 lprofCurFilename.MergePoolSize)) { 448 if (!ForceUseBuf) 449 return lprofCurFilename.FilenamePat; 450 451 FilenamePatLength = strlen(lprofCurFilename.FilenamePat); 452 memcpy(FilenameBuf, lprofCurFilename.FilenamePat, FilenamePatLength); 453 FilenameBuf[FilenamePatLength] = '\0'; 454 return FilenameBuf; 455 } 456 457 PidLength = strlen(lprofCurFilename.PidChars); 458 HostNameLength = strlen(lprofCurFilename.Hostname); 459 /* Construct the new filename. */ 460 for (I = 0, J = 0; FilenamePat[I]; ++I) 461 if (FilenamePat[I] == '%') { 462 if (FilenamePat[++I] == 'p') { 463 memcpy(FilenameBuf + J, lprofCurFilename.PidChars, PidLength); 464 J += PidLength; 465 } else if (FilenamePat[I] == 'h') { 466 memcpy(FilenameBuf + J, lprofCurFilename.Hostname, HostNameLength); 467 J += HostNameLength; 468 } else if (containsMergeSpecifier(FilenamePat, I)) { 469 char LoadModuleSignature[SIGLEN]; 470 int S; 471 int ProfilePoolId = getpid() % lprofCurFilename.MergePoolSize; 472 S = snprintf(LoadModuleSignature, SIGLEN, "%" PRIu64 "_%d", 473 lprofGetLoadModuleSignature(), ProfilePoolId); 474 if (S == -1 || S > SIGLEN) 475 S = SIGLEN; 476 memcpy(FilenameBuf + J, LoadModuleSignature, S); 477 J += S; 478 if (FilenamePat[I] != 'm') 479 I++; 480 } 481 /* Drop any unknown substitutions. */ 482 } else 483 FilenameBuf[J++] = FilenamePat[I]; 484 FilenameBuf[J] = 0; 485 486 return FilenameBuf; 487 } 488 489 /* Returns the pointer to the environment variable 490 * string. Returns null if the env var is not set. */ 491 static const char *getFilenamePatFromEnv(void) { 492 const char *Filename = getenv("LLVM_PROFILE_FILE"); 493 if (!Filename || !Filename[0]) 494 return 0; 495 return Filename; 496 } 497 498 COMPILER_RT_VISIBILITY 499 const char *__llvm_profile_get_path_prefix(void) { 500 int Length; 501 char *FilenameBuf, *Prefix; 502 const char *Filename, *PrefixEnd; 503 504 if (lprofCurFilename.ProfilePathPrefix) 505 return lprofCurFilename.ProfilePathPrefix; 506 507 Length = getCurFilenameLength(); 508 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1); 509 Filename = getCurFilename(FilenameBuf, 0); 510 if (!Filename) 511 return "\0"; 512 513 PrefixEnd = lprofFindLastDirSeparator(Filename); 514 if (!PrefixEnd) 515 return "\0"; 516 517 Length = PrefixEnd - Filename + 1; 518 Prefix = (char *)malloc(Length + 1); 519 if (!Prefix) { 520 PROF_ERR("Failed to %s\n", "allocate memory."); 521 return "\0"; 522 } 523 memcpy(Prefix, Filename, Length); 524 Prefix[Length] = '\0'; 525 lprofCurFilename.ProfilePathPrefix = Prefix; 526 return Prefix; 527 } 528 529 COMPILER_RT_VISIBILITY 530 const char *__llvm_profile_get_filename(void) { 531 int Length; 532 char *FilenameBuf; 533 const char *Filename; 534 535 if (lprofCurFilename.Filename) 536 return lprofCurFilename.Filename; 537 538 Length = getCurFilenameLength(); 539 FilenameBuf = (char *)malloc(Length + 1); 540 if (!FilenameBuf) { 541 PROF_ERR("Failed to %s\n", "allocate memory."); 542 return "\0"; 543 } 544 Filename = getCurFilename(FilenameBuf, 1); 545 if (!Filename) 546 return "\0"; 547 548 lprofCurFilename.Filename = FilenameBuf; 549 return FilenameBuf; 550 } 551 552 /* This method is invoked by the runtime initialization hook 553 * InstrProfilingRuntime.o if it is linked in. Both user specified 554 * profile path via -fprofile-instr-generate= and LLVM_PROFILE_FILE 555 * environment variable can override this default value. */ 556 COMPILER_RT_VISIBILITY 557 void __llvm_profile_initialize_file(void) { 558 const char *EnvFilenamePat; 559 const char *SelectedPat = NULL; 560 ProfileNameSpecifier PNS = PNS_unknown; 561 int hasCommandLineOverrider = (INSTR_PROF_PROFILE_NAME_VAR[0] != 0); 562 563 EnvFilenamePat = getFilenamePatFromEnv(); 564 if (EnvFilenamePat) { 565 /* Pass CopyFilenamePat = 1, to ensure that the filename would be valid 566 at the moment when __llvm_profile_write_file() gets executed. */ 567 parseAndSetFilename(EnvFilenamePat, PNS_environment, 1); 568 return; 569 } else if (hasCommandLineOverrider) { 570 SelectedPat = INSTR_PROF_PROFILE_NAME_VAR; 571 PNS = PNS_command_line; 572 } else { 573 SelectedPat = NULL; 574 PNS = PNS_default; 575 } 576 577 parseAndSetFilename(SelectedPat, PNS, 0); 578 } 579 580 /* This API is directly called by the user application code. It has the 581 * highest precedence compared with LLVM_PROFILE_FILE environment variable 582 * and command line option -fprofile-instr-generate=<profile_name>. 583 */ 584 COMPILER_RT_VISIBILITY 585 void __llvm_profile_set_filename(const char *FilenamePat) { 586 parseAndSetFilename(FilenamePat, PNS_runtime_api, 1); 587 } 588 589 /* The public API for writing profile data into the file with name 590 * set by previous calls to __llvm_profile_set_filename or 591 * __llvm_profile_override_default_filename or 592 * __llvm_profile_initialize_file. */ 593 COMPILER_RT_VISIBILITY 594 int __llvm_profile_write_file(void) { 595 int rc, Length; 596 const char *Filename; 597 char *FilenameBuf; 598 int PDeathSig = 0; 599 600 if (lprofProfileDumped()) { 601 PROF_NOTE("Profile data not written to file: %s.\n", 602 "already written"); 603 return 0; 604 } 605 606 Length = getCurFilenameLength(); 607 FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1); 608 Filename = getCurFilename(FilenameBuf, 0); 609 610 /* Check the filename. */ 611 if (!Filename) { 612 PROF_ERR("Failed to write file : %s\n", "Filename not set"); 613 return -1; 614 } 615 616 /* Check if there is llvm/runtime version mismatch. */ 617 if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) { 618 PROF_ERR("Runtime and instrumentation version mismatch : " 619 "expected %d, but get %d\n", 620 INSTR_PROF_RAW_VERSION, 621 (int)GET_VERSION(__llvm_profile_get_version())); 622 return -1; 623 } 624 625 // Temporarily suspend getting SIGKILL when the parent exits. 626 PDeathSig = lprofSuspendSigKill(); 627 628 /* Write profile data to the file. */ 629 rc = writeFile(Filename); 630 if (rc) 631 PROF_ERR("Failed to write file \"%s\": %s\n", Filename, strerror(errno)); 632 633 // Restore SIGKILL. 634 if (PDeathSig == 1) 635 lprofRestoreSigKill(); 636 637 return rc; 638 } 639 640 COMPILER_RT_VISIBILITY 641 int __llvm_profile_dump(void) { 642 if (!doMerging()) 643 PROF_WARN("Later invocation of __llvm_profile_dump can lead to clobbering " 644 " of previously dumped profile data : %s. Either use %%m " 645 "in profile name or change profile name before dumping.\n", 646 "online profile merging is not on"); 647 int rc = __llvm_profile_write_file(); 648 lprofSetProfileDumped(); 649 return rc; 650 } 651 652 static void writeFileWithoutReturn(void) { __llvm_profile_write_file(); } 653 654 COMPILER_RT_VISIBILITY 655 int __llvm_profile_register_write_file_atexit(void) { 656 static int HasBeenRegistered = 0; 657 658 if (HasBeenRegistered) 659 return 0; 660 661 lprofSetupValueProfiler(); 662 663 HasBeenRegistered = 1; 664 return atexit(writeFileWithoutReturn); 665 } 666 667 #endif 668