blob: 7f2923c3065816c09c8cfac4bcd61b3ae483c16c [file] [log] [blame]
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001/*===- 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#include "InstrProfiling.h"
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080011#include "InstrProfilingInternal.h"
12#include "InstrProfilingUtil.h"
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -070013#include <errno.h>
Stephen Hines2d1fdb22014-05-28 23:58:16 -070014#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17
18#define UNCONST(ptr) ((void *)(uintptr_t)(ptr))
19
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080020/* Return 1 if there is an error, otherwise return 0. */
21static uint32_t fileWriter(ProfDataIOVec *IOVecs, uint32_t NumIOVecs,
22 void **WriterCtx) {
23 uint32_t I;
24 FILE *File = (FILE *)*WriterCtx;
25 for (I = 0; I < NumIOVecs; I++) {
26 if (fwrite(IOVecs[I].Data, IOVecs[I].ElmSize, IOVecs[I].NumElm, File) !=
27 IOVecs[I].NumElm)
28 return 1;
29 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -070030 return 0;
31}
32
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080033static int writeFile(FILE *File) {
34 uint8_t *ValueDataBegin = NULL;
35 const uint64_t ValueDataSize =
36 __llvm_profile_gather_value_data(&ValueDataBegin);
37 int r = llvmWriteProfData(fileWriter, File, ValueDataBegin, ValueDataSize);
38 free(ValueDataBegin);
39 return r;
40}
41
Stephen Hines2d1fdb22014-05-28 23:58:16 -070042static int writeFileWithName(const char *OutputName) {
43 int RetVal;
44 FILE *OutputFile;
45 if (!OutputName || !OutputName[0])
46 return -1;
47
48 /* Append to the file to support profiling multiple shared objects. */
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080049 OutputFile = fopen(OutputName, "ab");
Stephen Hines2d1fdb22014-05-28 23:58:16 -070050 if (!OutputFile)
51 return -1;
52
53 RetVal = writeFile(OutputFile);
54
55 fclose(OutputFile);
56 return RetVal;
57}
58
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080059COMPILER_RT_WEAK int __llvm_profile_OwnsFilename = 0;
60COMPILER_RT_WEAK const char *__llvm_profile_CurrentFilename = NULL;
Stephen Hines2d1fdb22014-05-28 23:58:16 -070061
Stephen Hines2d1fdb22014-05-28 23:58:16 -070062static void truncateCurrentFile(void) {
Stephen Hines86277eb2015-03-23 12:06:32 -070063 const char *Filename;
64 FILE *File;
65
66 Filename = __llvm_profile_CurrentFilename;
Stephen Hines2d1fdb22014-05-28 23:58:16 -070067 if (!Filename || !Filename[0])
68 return;
69
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080070 /* Create the directory holding the file, if needed. */
71 if (strchr(Filename, '/')) {
72 char *Copy = malloc(strlen(Filename) + 1);
73 strcpy(Copy, Filename);
74 __llvm_profile_recursive_mkdir(Copy);
75 free(Copy);
76 }
77
Stephen Hines2d1fdb22014-05-28 23:58:16 -070078 /* Truncate the file. Later we'll reopen and append. */
Stephen Hines86277eb2015-03-23 12:06:32 -070079 File = fopen(Filename, "w");
Stephen Hines2d1fdb22014-05-28 23:58:16 -070080 if (!File)
81 return;
82 fclose(File);
83}
84
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070085static void setFilename(const char *Filename, int OwnsFilename) {
86 /* Check if this is a new filename and therefore needs truncation. */
87 int NewFile = !__llvm_profile_CurrentFilename ||
88 (Filename && strcmp(Filename, __llvm_profile_CurrentFilename));
89 if (__llvm_profile_OwnsFilename)
90 free(UNCONST(__llvm_profile_CurrentFilename));
91
92 __llvm_profile_CurrentFilename = Filename;
93 __llvm_profile_OwnsFilename = OwnsFilename;
94
95 /* If not a new file, append to support profiling multiple shared objects. */
96 if (NewFile)
97 truncateCurrentFile();
98}
99
100static void resetFilenameToDefault(void) { setFilename("default.profraw", 0); }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700101
102int getpid(void);
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700103static int setFilenamePossiblyWithPid(const char *Filename) {
Stephen Hines86277eb2015-03-23 12:06:32 -0700104#define MAX_PID_SIZE 16
105 char PidChars[MAX_PID_SIZE] = {0};
106 int NumPids = 0, PidLength = 0;
107 char *Allocated;
108 int I, J;
109
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700110 /* Reset filename on NULL, except with env var which is checked by caller. */
111 if (!Filename) {
112 resetFilenameToDefault();
113 return 0;
114 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700115
116 /* Check the filename for "%p", which indicates a pid-substitution. */
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700117 for (I = 0; Filename[I]; ++I)
118 if (Filename[I] == '%' && Filename[++I] == 'p')
119 if (!NumPids++) {
120 PidLength = snprintf(PidChars, MAX_PID_SIZE, "%d", getpid());
121 if (PidLength <= 0)
122 return -1;
123 }
124 if (!NumPids) {
125 setFilename(Filename, 0);
126 return 0;
127 }
128
129 /* Allocate enough space for the substituted filename. */
Stephen Hines86277eb2015-03-23 12:06:32 -0700130 Allocated = malloc(I + NumPids*(PidLength - 2) + 1);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700131 if (!Allocated)
132 return -1;
133
134 /* Construct the new filename. */
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700135 for (I = 0, J = 0; Filename[I]; ++I)
136 if (Filename[I] == '%') {
137 if (Filename[++I] == 'p') {
138 memcpy(Allocated + J, PidChars, PidLength);
139 J += PidLength;
140 }
141 /* Drop any unknown substitutions. */
142 } else
143 Allocated[J++] = Filename[I];
144 Allocated[J] = 0;
145
146 /* Use the computed name. */
147 setFilename(Allocated, 1);
148 return 0;
149}
150
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700151static int setFilenameFromEnvironment(void) {
152 const char *Filename = getenv("LLVM_PROFILE_FILE");
153
154 if (!Filename || !Filename[0])
155 return -1;
156
157 return setFilenamePossiblyWithPid(Filename);
158}
159
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700160static void setFilenameAutomatically(void) {
161 if (!setFilenameFromEnvironment())
162 return;
163
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700164 resetFilenameToDefault();
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700165}
166
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800167COMPILER_RT_VISIBILITY
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700168void __llvm_profile_initialize_file(void) {
169 /* Check if the filename has been initialized. */
170 if (__llvm_profile_CurrentFilename)
171 return;
172
173 /* Detect the filename and truncate. */
174 setFilenameAutomatically();
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700175}
176
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800177COMPILER_RT_VISIBILITY
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700178void __llvm_profile_set_filename(const char *Filename) {
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700179 setFilenamePossiblyWithPid(Filename);
180}
181
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800182COMPILER_RT_VISIBILITY
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700183void __llvm_profile_override_default_filename(const char *Filename) {
184 /* If the env var is set, skip setting filename from argument. */
185 const char *Env_Filename = getenv("LLVM_PROFILE_FILE");
186 if (Env_Filename && Env_Filename[0])
187 return;
188 setFilenamePossiblyWithPid(Filename);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700189}
190
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800191COMPILER_RT_VISIBILITY
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700192int __llvm_profile_write_file(void) {
Stephen Hines86277eb2015-03-23 12:06:32 -0700193 int rc;
194
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800195 GetEnvHook = &getenv;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700196 /* Check the filename. */
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800197 if (!__llvm_profile_CurrentFilename) {
198 PROF_ERR("LLVM Profile: Failed to write file : %s\n", "Filename not set");
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700199 return -1;
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800200 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700201
202 /* Write the file. */
Stephen Hines86277eb2015-03-23 12:06:32 -0700203 rc = writeFileWithName(__llvm_profile_CurrentFilename);
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800204 if (rc)
205 PROF_ERR("LLVM Profile: Failed to write file \"%s\": %s\n",
Stephen Hines86277eb2015-03-23 12:06:32 -0700206 __llvm_profile_CurrentFilename, strerror(errno));
207 return rc;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700208}
209
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800210static void writeFileWithoutReturn(void) { __llvm_profile_write_file(); }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700211
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800212COMPILER_RT_VISIBILITY
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700213int __llvm_profile_register_write_file_atexit(void) {
214 static int HasBeenRegistered = 0;
215
216 if (HasBeenRegistered)
217 return 0;
218
219 HasBeenRegistered = 1;
220 return atexit(writeFileWithoutReturn);
221}