static void truncateCurrentFile(void) { const char *Filename; char *FilenameBuf; FILE *File; int Length; Length = getCurFilenameLength(); FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1); Filename = getCurFilename(FilenameBuf); if (!Filename) return; /* Create the directory holding the file, if needed. */ if (strchr(Filename, '/') || strchr(Filename, '\\')) { char *Copy = (char *)COMPILER_RT_ALLOCA(Length + 1); strncpy(Copy, Filename, Length + 1); __llvm_profile_recursive_mkdir(Copy); } /* Truncate the file. Later we'll reopen and append. */ File = fopen(Filename, "w"); if (!File) return; fclose(File); }
static void truncateCurrentFile(void) { const char *Filename; FILE *File; Filename = __llvm_profile_CurrentFilename; if (!Filename || !Filename[0]) return; /* Create the directory holding the file, if needed. */ if (strchr(Filename, '/')) { char *Copy = malloc(strlen(Filename) + 1); strcpy(Copy, Filename); __llvm_profile_recursive_mkdir(Copy); free(Copy); } /* Truncate the file. Later we'll reopen and append. */ File = fopen(Filename, "w"); if (!File) return; fclose(File); }
/* A file in this case is a translation unit. Each .o file built with line * profiling enabled will emit to a different file. Only one file may be * started at a time. */ void llvm_gcda_start_file(const char *orig_filename, const char version[4], uint32_t checksum) { const char *mode = "r+b"; filename = mangle_filename(orig_filename); /* Try just opening the file. */ new_file = 0; fd = open(filename, O_RDWR); if (fd == -1) { /* Try opening the file, creating it if necessary. */ new_file = 1; mode = "w+b"; fd = open(filename, O_RDWR | O_CREAT, 0644); if (fd == -1) { /* Try creating the directories first then opening the file. */ __llvm_profile_recursive_mkdir(filename); fd = open(filename, O_RDWR | O_CREAT, 0644); if (fd == -1) { /* Bah! It's hopeless. */ int errnum = errno; fprintf(stderr, "profiling: %s: cannot open: %s\n", filename, strerror(errnum)); return; } } } /* Try to flock the file to serialize concurrent processes writing out to the * same GCDA. This can fail if the filesystem doesn't support it, but in that * case we'll just carry on with the old racy behaviour and hope for the best. */ flock(fd, LOCK_EX); output_file = fdopen(fd, mode); /* Initialize the write buffer. */ write_buffer = NULL; cur_buffer_size = 0; cur_pos = 0; if (new_file) { resize_write_buffer(WRITE_BUFFER_SIZE); memset(write_buffer, 0, WRITE_BUFFER_SIZE); } else { if (map_file() == -1) { /* mmap failed, try to recover by clobbering */ new_file = 1; write_buffer = NULL; cur_buffer_size = 0; resize_write_buffer(WRITE_BUFFER_SIZE); memset(write_buffer, 0, WRITE_BUFFER_SIZE); } } /* gcda file, version, stamp checksum. */ write_bytes("adcg", 4); write_bytes(version, 4); write_32bit_value(checksum); #ifdef DEBUG_GCDAPROFILING fprintf(stderr, "llvmgcda: [%s]\n", orig_filename); #endif }