2014-03-22 02:29:15 +08:00
|
|
|
/*===- InstrProfilingFile.c - Write instrumentation to a file -------------===*\
|
2014-03-20 06:10:27 +08:00
|
|
|
|*
|
|
|
|
|* The LLVM Compiler Infrastructure
|
|
|
|
|*
|
|
|
|
|* This file is distributed under the University of Illinois Open Source
|
|
|
|
|* License. See LICENSE.TXT for details.
|
|
|
|
|*
|
|
|
|
\*===----------------------------------------------------------------------===*/
|
|
|
|
|
|
|
|
#include "InstrProfiling.h"
|
2014-03-22 02:29:15 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-03-21 08:27:48 +08:00
|
|
|
#include <string.h>
|
2014-03-20 06:10:27 +08:00
|
|
|
|
2014-05-21 00:37:07 +08:00
|
|
|
#define UNCONST(ptr) ((void *)(uintptr_t)(ptr))
|
|
|
|
|
2014-03-22 02:29:19 +08:00
|
|
|
static int writeFile(FILE *File) {
|
2014-03-22 02:29:24 +08:00
|
|
|
/* Match logic in __llvm_profile_write_buffer(). */
|
2014-09-04 23:45:31 +08:00
|
|
|
const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
|
|
|
|
const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
|
|
|
|
const uint64_t *CountersBegin = __llvm_profile_begin_counters();
|
|
|
|
const uint64_t *CountersEnd = __llvm_profile_end_counters();
|
|
|
|
const char *NamesBegin = __llvm_profile_begin_names();
|
|
|
|
const char *NamesEnd = __llvm_profile_end_names();
|
2014-03-22 02:29:19 +08:00
|
|
|
|
|
|
|
/* Calculate size of sections. */
|
|
|
|
const uint64_t DataSize = DataEnd - DataBegin;
|
|
|
|
const uint64_t CountersSize = CountersEnd - CountersBegin;
|
|
|
|
const uint64_t NamesSize = NamesEnd - NamesBegin;
|
2014-05-16 09:30:24 +08:00
|
|
|
const uint64_t Padding = sizeof(uint64_t) - NamesSize % sizeof(uint64_t);
|
|
|
|
|
|
|
|
/* Enough zeroes for padding. */
|
|
|
|
const char Zeroes[sizeof(uint64_t)] = {0};
|
2014-03-22 02:29:19 +08:00
|
|
|
|
2014-03-22 02:29:24 +08:00
|
|
|
/* Create the header. */
|
2014-05-02 02:52:14 +08:00
|
|
|
uint64_t Header[PROFILE_HEADER_SIZE];
|
|
|
|
Header[0] = __llvm_profile_get_magic();
|
|
|
|
Header[1] = __llvm_profile_get_version();
|
|
|
|
Header[2] = DataSize;
|
|
|
|
Header[3] = CountersSize;
|
|
|
|
Header[4] = NamesSize;
|
|
|
|
Header[5] = (uintptr_t)CountersBegin;
|
|
|
|
Header[6] = (uintptr_t)NamesBegin;
|
2014-03-22 02:29:19 +08:00
|
|
|
|
2014-03-22 02:29:24 +08:00
|
|
|
/* Write the data. */
|
2014-03-22 02:29:19 +08:00
|
|
|
#define CHECK_fwrite(Data, Size, Length, File) \
|
|
|
|
do { if (fwrite(Data, Size, Length, File) != Length) return -1; } while (0)
|
2014-03-22 02:29:24 +08:00
|
|
|
CHECK_fwrite(Header, sizeof(uint64_t), PROFILE_HEADER_SIZE, File);
|
|
|
|
CHECK_fwrite(DataBegin, sizeof(__llvm_profile_data), DataSize, File);
|
2014-03-22 02:29:19 +08:00
|
|
|
CHECK_fwrite(CountersBegin, sizeof(uint64_t), CountersSize, File);
|
2014-03-22 02:29:24 +08:00
|
|
|
CHECK_fwrite(NamesBegin, sizeof(char), NamesSize, File);
|
2014-05-16 09:30:24 +08:00
|
|
|
CHECK_fwrite(Zeroes, sizeof(char), Padding, File);
|
2014-03-22 02:29:19 +08:00
|
|
|
#undef CHECK_fwrite
|
|
|
|
|
2014-05-16 09:30:24 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-22 02:29:15 +08:00
|
|
|
static int writeFileWithName(const char *OutputName) {
|
2014-03-21 08:27:50 +08:00
|
|
|
int RetVal;
|
2014-03-20 06:10:27 +08:00
|
|
|
FILE *OutputFile;
|
|
|
|
if (!OutputName || !OutputName[0])
|
2014-03-21 08:27:50 +08:00
|
|
|
return -1;
|
2014-05-17 09:27:30 +08:00
|
|
|
|
|
|
|
/* Append to the file to support profiling multiple shared objects. */
|
|
|
|
OutputFile = fopen(OutputName, "a");
|
2014-03-21 08:27:50 +08:00
|
|
|
if (!OutputFile)
|
|
|
|
return -1;
|
2014-03-20 06:10:27 +08:00
|
|
|
|
2014-05-17 09:27:30 +08:00
|
|
|
RetVal = writeFile(OutputFile);
|
2014-03-20 06:10:27 +08:00
|
|
|
|
|
|
|
fclose(OutputFile);
|
2014-03-21 08:27:50 +08:00
|
|
|
return RetVal;
|
2014-03-20 06:10:27 +08:00
|
|
|
}
|
|
|
|
|
2014-05-17 09:27:30 +08:00
|
|
|
__attribute__((weak)) int __llvm_profile_OwnsFilename = 0;
|
2014-05-16 09:30:24 +08:00
|
|
|
__attribute__((weak)) const char *__llvm_profile_CurrentFilename = NULL;
|
2014-05-17 09:27:30 +08:00
|
|
|
|
|
|
|
static void setFilename(const char *Filename, int OwnsFilename) {
|
|
|
|
if (__llvm_profile_OwnsFilename)
|
2014-05-21 00:37:07 +08:00
|
|
|
free(UNCONST(__llvm_profile_CurrentFilename));
|
2014-05-17 09:27:30 +08:00
|
|
|
|
2014-05-16 09:30:24 +08:00
|
|
|
__llvm_profile_CurrentFilename = Filename;
|
2014-05-17 09:27:30 +08:00
|
|
|
__llvm_profile_OwnsFilename = OwnsFilename;
|
2014-03-20 06:10:27 +08:00
|
|
|
}
|
|
|
|
|
2014-05-17 09:27:30 +08:00
|
|
|
static void truncateCurrentFile(void) {
|
|
|
|
const char *Filename = __llvm_profile_CurrentFilename;
|
|
|
|
if (!Filename || !Filename[0])
|
|
|
|
return;
|
2014-03-21 08:27:48 +08:00
|
|
|
|
2014-05-17 09:27:30 +08:00
|
|
|
/* Truncate the file. Later we'll reopen and append. */
|
|
|
|
FILE *File = fopen(Filename, "w");
|
|
|
|
if (!File)
|
|
|
|
return;
|
|
|
|
fclose(File);
|
|
|
|
}
|
2014-03-21 03:23:55 +08:00
|
|
|
|
2014-05-17 09:27:30 +08:00
|
|
|
static void setDefaultFilename(void) { setFilename("default.profraw", 0); }
|
|
|
|
|
|
|
|
int getpid(void);
|
|
|
|
static int setFilenameFromEnvironment(void) {
|
|
|
|
const char *Filename = getenv("LLVM_PROFILE_FILE");
|
|
|
|
if (!Filename || !Filename[0])
|
|
|
|
return -1;
|
2014-03-21 03:23:55 +08:00
|
|
|
|
2014-03-22 02:29:15 +08:00
|
|
|
/* Check the filename for "%p", which indicates a pid-substitution. */
|
2014-05-17 09:27:30 +08:00
|
|
|
#define MAX_PID_SIZE 16
|
|
|
|
char PidChars[MAX_PID_SIZE] = {0};
|
|
|
|
int NumPids = 0;
|
|
|
|
int PidLength = 0;
|
|
|
|
int I;
|
2014-03-21 08:27:48 +08:00
|
|
|
for (I = 0; Filename[I]; ++I)
|
|
|
|
if (Filename[I] == '%' && Filename[++I] == 'p')
|
|
|
|
if (!NumPids++) {
|
|
|
|
PidLength = snprintf(PidChars, MAX_PID_SIZE, "%d", getpid());
|
|
|
|
if (PidLength <= 0)
|
2014-03-21 08:27:50 +08:00
|
|
|
return -1;
|
2014-03-21 08:27:48 +08:00
|
|
|
}
|
2014-05-17 09:27:30 +08:00
|
|
|
if (!NumPids) {
|
|
|
|
setFilename(Filename, 0);
|
|
|
|
return 0;
|
2014-03-21 08:27:48 +08:00
|
|
|
}
|
|
|
|
|
2014-05-17 09:27:30 +08:00
|
|
|
/* Allocate enough space for the substituted filename. */
|
|
|
|
char *Allocated = (char*)malloc(I + NumPids*(PidLength - 2) + 1);
|
|
|
|
if (!Allocated)
|
|
|
|
return -1;
|
2014-03-21 08:27:48 +08:00
|
|
|
|
2014-05-17 09:27:30 +08:00
|
|
|
/* Construct the new filename. */
|
|
|
|
int J;
|
|
|
|
for (I = 0, J = 0; Filename[I]; ++I)
|
|
|
|
if (Filename[I] == '%') {
|
|
|
|
if (Filename[++I] == 'p') {
|
|
|
|
memcpy(Allocated + J, PidChars, PidLength);
|
|
|
|
J += PidLength;
|
|
|
|
}
|
|
|
|
/* Drop any unknown substitutions. */
|
|
|
|
} else
|
|
|
|
Allocated[J++] = Filename[I];
|
|
|
|
Allocated[J] = 0;
|
2014-03-21 08:27:50 +08:00
|
|
|
|
2014-05-17 09:27:30 +08:00
|
|
|
/* Use the computed name. */
|
|
|
|
setFilename(Allocated, 1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setFilenameAutomatically(void) {
|
|
|
|
if (!setFilenameFromEnvironment())
|
|
|
|
return;
|
|
|
|
|
|
|
|
setDefaultFilename();
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__((visibility("hidden")))
|
|
|
|
void __llvm_profile_initialize_file(void) {
|
|
|
|
/* Check if the filename has been initialized. */
|
|
|
|
if (__llvm_profile_CurrentFilename)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Detect the filename and truncate. */
|
|
|
|
setFilenameAutomatically();
|
|
|
|
truncateCurrentFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__((visibility("hidden")))
|
|
|
|
void __llvm_profile_set_filename(const char *Filename) {
|
|
|
|
setFilename(Filename, 0);
|
|
|
|
truncateCurrentFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__((visibility("hidden")))
|
|
|
|
int __llvm_profile_write_file(void) {
|
|
|
|
/* Check the filename. */
|
|
|
|
if (!__llvm_profile_CurrentFilename)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Write the file. */
|
|
|
|
return writeFileWithName(__llvm_profile_CurrentFilename);
|
2014-03-21 03:23:55 +08:00
|
|
|
}
|
|
|
|
|
2014-03-21 08:27:50 +08:00
|
|
|
static void writeFileWithoutReturn(void) {
|
|
|
|
__llvm_profile_write_file();
|
|
|
|
}
|
2014-03-22 02:29:15 +08:00
|
|
|
|
2014-05-17 09:27:30 +08:00
|
|
|
__attribute__((visibility("hidden")))
|
|
|
|
int __llvm_profile_register_write_file_atexit(void) {
|
2014-03-20 06:10:27 +08:00
|
|
|
static int HasBeenRegistered = 0;
|
|
|
|
|
2014-03-22 02:29:15 +08:00
|
|
|
if (HasBeenRegistered)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
HasBeenRegistered = 1;
|
|
|
|
return atexit(writeFileWithoutReturn);
|
2014-03-20 06:10:27 +08:00
|
|
|
}
|