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"
|
2015-11-19 05:08:03 +08:00
|
|
|
#include "InstrProfilingInternal.h"
|
2015-11-19 05:11:46 +08:00
|
|
|
#include "InstrProfilingUtil.h"
|
2015-03-09 19:23:29 +08:00
|
|
|
#include <errno.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>
|
2016-05-20 12:52:27 +08:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
/* For _alloca */
|
|
|
|
#include <malloc.h>
|
2016-05-20 13:40:07 +08:00
|
|
|
#endif
|
2016-05-20 12:52:27 +08:00
|
|
|
|
2014-03-20 06:10:27 +08:00
|
|
|
|
2014-05-21 00:37:07 +08:00
|
|
|
#define UNCONST(ptr) ((void *)(uintptr_t)(ptr))
|
|
|
|
|
2015-11-21 12:16:42 +08:00
|
|
|
/* Return 1 if there is an error, otherwise return 0. */
|
|
|
|
static uint32_t fileWriter(ProfDataIOVec *IOVecs, uint32_t NumIOVecs,
|
|
|
|
void **WriterCtx) {
|
|
|
|
uint32_t I;
|
|
|
|
FILE *File = (FILE *)*WriterCtx;
|
|
|
|
for (I = 0; I < NumIOVecs; I++) {
|
|
|
|
if (fwrite(IOVecs[I].Data, IOVecs[I].ElmSize, IOVecs[I].NumElm, File) !=
|
|
|
|
IOVecs[I].NumElm)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2015-11-19 05:08:03 +08:00
|
|
|
}
|
2014-03-22 02:29:19 +08:00
|
|
|
|
2015-12-30 07:54:41 +08:00
|
|
|
COMPILER_RT_VISIBILITY ProfBufferIO *
|
2016-03-06 12:18:13 +08:00
|
|
|
lprofCreateBufferIOInternal(void *File, uint32_t BufferSz) {
|
2016-05-14 02:26:26 +08:00
|
|
|
FreeHook = &free;
|
|
|
|
DynamicBufferIOBuffer = (uint8_t *)calloc(BufferSz, 1);
|
|
|
|
VPBufferSize = BufferSz;
|
|
|
|
return lprofCreateBufferIO(fileWriter, File);
|
2015-12-30 07:54:41 +08:00
|
|
|
}
|
|
|
|
|
2016-05-14 02:26:26 +08:00
|
|
|
static void setupIOBuffer() {
|
2015-12-29 15:13:59 +08:00
|
|
|
const char *BufferSzStr = 0;
|
|
|
|
BufferSzStr = getenv("LLVM_VP_BUFFER_SIZE");
|
2016-05-14 02:26:26 +08:00
|
|
|
if (BufferSzStr && BufferSzStr[0]) {
|
2015-12-29 15:13:59 +08:00
|
|
|
VPBufferSize = atoi(BufferSzStr);
|
2016-05-14 02:26:26 +08:00
|
|
|
DynamicBufferIOBuffer = (uint8_t *)calloc(VPBufferSize, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int writeFile(FILE *File) {
|
|
|
|
FreeHook = &free;
|
|
|
|
setupIOBuffer();
|
2016-05-15 04:12:42 +08:00
|
|
|
return lprofWriteData(fileWriter, File, lprofGetVPDataReader());
|
2014-05-16 09:30:24 +08:00
|
|
|
}
|
|
|
|
|
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. */
|
2015-12-16 06:18:11 +08:00
|
|
|
OutputFile = fopen(OutputName, "ab");
|
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
|
|
|
}
|
|
|
|
|
2015-12-16 11:29:15 +08:00
|
|
|
COMPILER_RT_WEAK int __llvm_profile_OwnsFilename = 0;
|
|
|
|
COMPILER_RT_WEAK const char *__llvm_profile_CurrentFilename = NULL;
|
2014-05-17 09:27:30 +08:00
|
|
|
|
|
|
|
static void truncateCurrentFile(void) {
|
2015-02-25 21:50:18 +08:00
|
|
|
const char *Filename;
|
|
|
|
FILE *File;
|
|
|
|
|
|
|
|
Filename = __llvm_profile_CurrentFilename;
|
2014-05-17 09:27:30 +08:00
|
|
|
if (!Filename || !Filename[0])
|
|
|
|
return;
|
2014-03-21 08:27:48 +08:00
|
|
|
|
2015-07-10 01:21:52 +08:00
|
|
|
/* Create the directory holding the file, if needed. */
|
2016-03-29 05:32:46 +08:00
|
|
|
if (strchr(Filename, '/') || strchr(Filename, '\\')) {
|
2016-05-20 12:52:27 +08:00
|
|
|
char *Copy = (char *)COMPILER_RT_ALLOCA(strlen(Filename) + 1);
|
2015-07-10 01:21:52 +08:00
|
|
|
strcpy(Copy, Filename);
|
|
|
|
__llvm_profile_recursive_mkdir(Copy);
|
|
|
|
}
|
|
|
|
|
2014-05-17 09:27:30 +08:00
|
|
|
/* Truncate the file. Later we'll reopen and append. */
|
2015-02-25 21:50:18 +08:00
|
|
|
File = fopen(Filename, "w");
|
2014-05-17 09:27:30 +08:00
|
|
|
if (!File)
|
|
|
|
return;
|
|
|
|
fclose(File);
|
|
|
|
}
|
2014-03-21 03:23:55 +08:00
|
|
|
|
2015-04-29 06:54:51 +08:00
|
|
|
static void setFilename(const char *Filename, int OwnsFilename) {
|
|
|
|
/* Check if this is a new filename and therefore needs truncation. */
|
|
|
|
int NewFile = !__llvm_profile_CurrentFilename ||
|
|
|
|
(Filename && strcmp(Filename, __llvm_profile_CurrentFilename));
|
|
|
|
if (__llvm_profile_OwnsFilename)
|
|
|
|
free(UNCONST(__llvm_profile_CurrentFilename));
|
|
|
|
|
|
|
|
__llvm_profile_CurrentFilename = Filename;
|
|
|
|
__llvm_profile_OwnsFilename = OwnsFilename;
|
|
|
|
|
|
|
|
/* If not a new file, append to support profiling multiple shared objects. */
|
|
|
|
if (NewFile)
|
|
|
|
truncateCurrentFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void resetFilenameToDefault(void) { setFilename("default.profraw", 0); }
|
2014-05-17 09:27:30 +08:00
|
|
|
|
|
|
|
int getpid(void);
|
2015-04-29 06:54:51 +08:00
|
|
|
static int setFilenamePossiblyWithPid(const char *Filename) {
|
2015-02-25 21:50:18 +08:00
|
|
|
#define MAX_PID_SIZE 16
|
|
|
|
char PidChars[MAX_PID_SIZE] = {0};
|
2016-01-30 07:52:11 +08:00
|
|
|
int NumPids = 0, PidLength = 0, NumHosts = 0, HostNameLength = 0;
|
2015-02-25 21:50:18 +08:00
|
|
|
char *Allocated;
|
|
|
|
int I, J;
|
2016-01-30 07:52:11 +08:00
|
|
|
char Hostname[COMPILER_RT_MAX_HOSTLEN];
|
2015-02-25 21:50:18 +08:00
|
|
|
|
2015-04-29 06:54:51 +08:00
|
|
|
/* Reset filename on NULL, except with env var which is checked by caller. */
|
|
|
|
if (!Filename) {
|
|
|
|
resetFilenameToDefault();
|
|
|
|
return 0;
|
|
|
|
}
|
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-03-21 08:27:48 +08:00
|
|
|
for (I = 0; Filename[I]; ++I)
|
2016-01-30 07:52:11 +08:00
|
|
|
if (Filename[I] == '%') {
|
|
|
|
if (Filename[++I] == 'p') {
|
|
|
|
if (!NumPids++) {
|
|
|
|
PidLength = snprintf(PidChars, MAX_PID_SIZE, "%d", getpid());
|
|
|
|
if (PidLength <= 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if (Filename[I] == 'h') {
|
|
|
|
if (!NumHosts++)
|
|
|
|
if (COMPILER_RT_GETHOSTNAME(Hostname, COMPILER_RT_MAX_HOSTLEN))
|
|
|
|
return -1;
|
|
|
|
HostNameLength = strlen(Hostname);
|
2014-03-21 08:27:48 +08:00
|
|
|
}
|
2016-01-30 07:52:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(NumPids || NumHosts)) {
|
2014-05-17 09:27:30 +08:00
|
|
|
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. */
|
2016-01-30 07:52:11 +08:00
|
|
|
Allocated = malloc(I + NumPids*(PidLength - 2) +
|
|
|
|
NumHosts*(HostNameLength - 2) + 1);
|
2014-05-17 09:27:30 +08:00
|
|
|
if (!Allocated)
|
|
|
|
return -1;
|
2014-03-21 08:27:48 +08:00
|
|
|
|
2014-05-17 09:27:30 +08:00
|
|
|
/* Construct the new filename. */
|
|
|
|
for (I = 0, J = 0; Filename[I]; ++I)
|
|
|
|
if (Filename[I] == '%') {
|
|
|
|
if (Filename[++I] == 'p') {
|
|
|
|
memcpy(Allocated + J, PidChars, PidLength);
|
|
|
|
J += PidLength;
|
|
|
|
}
|
2016-01-30 07:52:11 +08:00
|
|
|
else if (Filename[I] == 'h') {
|
|
|
|
memcpy(Allocated + J, Hostname, HostNameLength);
|
|
|
|
J += HostNameLength;
|
|
|
|
}
|
2014-05-17 09:27:30 +08:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2015-04-29 06:54:51 +08:00
|
|
|
static int setFilenameFromEnvironment(void) {
|
|
|
|
const char *Filename = getenv("LLVM_PROFILE_FILE");
|
|
|
|
|
|
|
|
if (!Filename || !Filename[0])
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return setFilenamePossiblyWithPid(Filename);
|
|
|
|
}
|
|
|
|
|
2014-05-17 09:27:30 +08:00
|
|
|
static void setFilenameAutomatically(void) {
|
|
|
|
if (!setFilenameFromEnvironment())
|
|
|
|
return;
|
|
|
|
|
2015-04-29 06:54:51 +08:00
|
|
|
resetFilenameToDefault();
|
2014-05-17 09:27:30 +08:00
|
|
|
}
|
|
|
|
|
2015-12-16 11:29:15 +08:00
|
|
|
COMPILER_RT_VISIBILITY
|
2014-05-17 09:27:30 +08:00
|
|
|
void __llvm_profile_initialize_file(void) {
|
|
|
|
/* Check if the filename has been initialized. */
|
|
|
|
if (__llvm_profile_CurrentFilename)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Detect the filename and truncate. */
|
|
|
|
setFilenameAutomatically();
|
|
|
|
}
|
|
|
|
|
2015-12-16 11:29:15 +08:00
|
|
|
COMPILER_RT_VISIBILITY
|
2014-05-17 09:27:30 +08:00
|
|
|
void __llvm_profile_set_filename(const char *Filename) {
|
2015-04-29 06:54:51 +08:00
|
|
|
setFilenamePossiblyWithPid(Filename);
|
|
|
|
}
|
|
|
|
|
2015-12-16 11:29:15 +08:00
|
|
|
COMPILER_RT_VISIBILITY
|
2015-04-29 06:54:51 +08:00
|
|
|
void __llvm_profile_override_default_filename(const char *Filename) {
|
|
|
|
/* If the env var is set, skip setting filename from argument. */
|
|
|
|
const char *Env_Filename = getenv("LLVM_PROFILE_FILE");
|
|
|
|
if (Env_Filename && Env_Filename[0])
|
|
|
|
return;
|
|
|
|
setFilenamePossiblyWithPid(Filename);
|
2014-05-17 09:27:30 +08:00
|
|
|
}
|
|
|
|
|
2015-12-16 11:29:15 +08:00
|
|
|
COMPILER_RT_VISIBILITY
|
2014-05-17 09:27:30 +08:00
|
|
|
int __llvm_profile_write_file(void) {
|
2015-02-25 21:50:18 +08:00
|
|
|
int rc;
|
|
|
|
|
2015-12-08 05:18:16 +08:00
|
|
|
GetEnvHook = &getenv;
|
2014-05-17 09:27:30 +08:00
|
|
|
/* Check the filename. */
|
2015-12-04 02:31:59 +08:00
|
|
|
if (!__llvm_profile_CurrentFilename) {
|
2016-05-20 13:15:42 +08:00
|
|
|
PROF_ERR("Failed to write file : %s\n", "Filename not set");
|
2014-05-17 09:27:30 +08:00
|
|
|
return -1;
|
2015-12-04 02:31:59 +08:00
|
|
|
}
|
2014-05-17 09:27:30 +08:00
|
|
|
|
2016-01-09 07:42:28 +08:00
|
|
|
/* Check if there is llvm/runtime version mismatch. */
|
2016-01-09 07:31:57 +08:00
|
|
|
if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) {
|
2016-05-20 13:15:42 +08:00
|
|
|
PROF_ERR("Runtime and instrumentation version mismatch : "
|
2016-01-09 07:31:57 +08:00
|
|
|
"expected %d, but get %d\n",
|
|
|
|
INSTR_PROF_RAW_VERSION,
|
|
|
|
(int)GET_VERSION(__llvm_profile_get_version()));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-05-17 09:27:30 +08:00
|
|
|
/* Write the file. */
|
2015-02-25 21:50:18 +08:00
|
|
|
rc = writeFileWithName(__llvm_profile_CurrentFilename);
|
2015-12-04 02:31:59 +08:00
|
|
|
if (rc)
|
2016-05-20 13:15:42 +08:00
|
|
|
PROF_ERR("Failed to write file \"%s\": %s\n",
|
2015-01-17 04:10:56 +08:00
|
|
|
__llvm_profile_CurrentFilename, strerror(errno));
|
|
|
|
return rc;
|
2014-03-21 03:23:55 +08:00
|
|
|
}
|
|
|
|
|
2015-11-23 12:38:17 +08:00
|
|
|
static void writeFileWithoutReturn(void) { __llvm_profile_write_file(); }
|
2014-03-22 02:29:15 +08:00
|
|
|
|
2015-12-16 11:29:15 +08:00
|
|
|
COMPILER_RT_VISIBILITY
|
2014-05-17 09:27:30 +08:00
|
|
|
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;
|
|
|
|
|
2016-05-19 06:34:05 +08:00
|
|
|
lprofSetupValueProfiler();
|
|
|
|
|
2014-03-22 02:29:15 +08:00
|
|
|
HasBeenRegistered = 1;
|
|
|
|
return atexit(writeFileWithoutReturn);
|
2014-03-20 06:10:27 +08:00
|
|
|
}
|