blob: 327f4a2bc6f4c99c4a78c8e7ba2bf26479627446 [file] [log] [blame]
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +00001/*===- InstrProfilingFile.c - Write instrumentation to a file -------------===*\
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +00002|*
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"
Xinliang David Li1d8d46a2015-11-18 21:08:03 +000011#include "InstrProfilingInternal.h"
Xinliang David Li6e557162015-11-18 21:11:46 +000012#include "InstrProfilingUtil.h"
Joerg Sonnenbergeref24b412015-03-09 11:23:29 +000013#include <errno.h>
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +000014#include <stdio.h>
15#include <stdlib.h>
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +000016#include <string.h>
Vedant Kumara06e8ca2016-01-29 23:52:11 +000017#ifdef COMPILER_RT_HAS_UNAME
18#include <sys/utsname.h>
19#endif
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000020
Joerg Sonnenbergerb1cc6d52014-05-20 16:37:07 +000021#define UNCONST(ptr) ((void *)(uintptr_t)(ptr))
22
Vedant Kumara06e8ca2016-01-29 23:52:11 +000023#ifdef COMPILER_RT_HAS_UNAME
Xinliang David Licf1a8d62016-03-06 04:18:13 +000024int lprofGetHostName(char *Name, int Len) {
25 struct utsname N;
26 int R;
27 if (!(R = uname(&N)))
28 strncpy(Name, N.nodename, Len);
29 return R;
Vedant Kumara06e8ca2016-01-29 23:52:11 +000030}
31#endif
32
Xinliang David Li2d5bf072015-11-21 04:16:42 +000033/* Return 1 if there is an error, otherwise return 0. */
34static uint32_t fileWriter(ProfDataIOVec *IOVecs, uint32_t NumIOVecs,
35 void **WriterCtx) {
36 uint32_t I;
37 FILE *File = (FILE *)*WriterCtx;
38 for (I = 0; I < NumIOVecs; I++) {
39 if (fwrite(IOVecs[I].Data, IOVecs[I].ElmSize, IOVecs[I].NumElm, File) !=
40 IOVecs[I].NumElm)
41 return 1;
42 }
43 return 0;
Xinliang David Li1d8d46a2015-11-18 21:08:03 +000044}
Duncan P. N. Exon Smithcf4bb962014-03-21 18:29:19 +000045
Xinliang David Licda3bc22015-12-29 23:54:41 +000046COMPILER_RT_VISIBILITY ProfBufferIO *
Xinliang David Licf1a8d62016-03-06 04:18:13 +000047lprofCreateBufferIOInternal(void *File, uint32_t BufferSz) {
Xinliang David Licda3bc22015-12-29 23:54:41 +000048 CallocHook = calloc;
49 FreeHook = free;
Xinliang David Licf1a8d62016-03-06 04:18:13 +000050 return lprofCreateBufferIO(fileWriter, File, BufferSz);
Xinliang David Licda3bc22015-12-29 23:54:41 +000051}
52
Xinliang David Li1d8d46a2015-11-18 21:08:03 +000053static int writeFile(FILE *File) {
Xinliang David Li54dd6832015-12-29 07:13:59 +000054 const char *BufferSzStr = 0;
55 uint64_t ValueDataSize = 0;
56 struct ValueProfData **ValueDataArray =
57 __llvm_profile_gather_value_data(&ValueDataSize);
58 FreeHook = &free;
59 CallocHook = &calloc;
60 BufferSzStr = getenv("LLVM_VP_BUFFER_SIZE");
61 if (BufferSzStr && BufferSzStr[0])
62 VPBufferSize = atoi(BufferSzStr);
Xinliang David Licf1a8d62016-03-06 04:18:13 +000063 return lprofWriteData(fileWriter, File, ValueDataArray, ValueDataSize);
Duncan P. N. Exon Smith08439882014-05-16 01:30:24 +000064}
65
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +000066static int writeFileWithName(const char *OutputName) {
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +000067 int RetVal;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000068 FILE *OutputFile;
69 if (!OutputName || !OutputName[0])
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +000070 return -1;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +000071
72 /* Append to the file to support profiling multiple shared objects. */
Xinliang David Libe492712015-12-15 22:18:11 +000073 OutputFile = fopen(OutputName, "ab");
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +000074 if (!OutputFile)
75 return -1;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000076
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +000077 RetVal = writeFile(OutputFile);
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000078
79 fclose(OutputFile);
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +000080 return RetVal;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000081}
82
Xinliang David Liabfd5532015-12-16 03:29:15 +000083COMPILER_RT_WEAK int __llvm_profile_OwnsFilename = 0;
84COMPILER_RT_WEAK const char *__llvm_profile_CurrentFilename = NULL;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +000085
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +000086static void truncateCurrentFile(void) {
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +000087 const char *Filename;
88 FILE *File;
89
90 Filename = __llvm_profile_CurrentFilename;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +000091 if (!Filename || !Filename[0])
92 return;
93
Diego Novilloeae95142015-07-09 17:21:52 +000094 /* Create the directory holding the file, if needed. */
95 if (strchr(Filename, '/')) {
96 char *Copy = malloc(strlen(Filename) + 1);
97 strcpy(Copy, Filename);
98 __llvm_profile_recursive_mkdir(Copy);
Alexey Samsonov6585b762015-07-15 22:50:39 +000099 free(Copy);
Diego Novilloeae95142015-07-09 17:21:52 +0000100 }
101
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000102 /* Truncate the file. Later we'll reopen and append. */
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000103 File = fopen(Filename, "w");
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000104 if (!File)
105 return;
106 fclose(File);
107}
108
Eric Christopherd641b532015-04-28 22:54:51 +0000109static void setFilename(const char *Filename, int OwnsFilename) {
110 /* Check if this is a new filename and therefore needs truncation. */
111 int NewFile = !__llvm_profile_CurrentFilename ||
112 (Filename && strcmp(Filename, __llvm_profile_CurrentFilename));
113 if (__llvm_profile_OwnsFilename)
114 free(UNCONST(__llvm_profile_CurrentFilename));
115
116 __llvm_profile_CurrentFilename = Filename;
117 __llvm_profile_OwnsFilename = OwnsFilename;
118
119 /* If not a new file, append to support profiling multiple shared objects. */
120 if (NewFile)
121 truncateCurrentFile();
122}
123
124static void resetFilenameToDefault(void) { setFilename("default.profraw", 0); }
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000125
126int getpid(void);
Eric Christopherd641b532015-04-28 22:54:51 +0000127static int setFilenamePossiblyWithPid(const char *Filename) {
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000128#define MAX_PID_SIZE 16
129 char PidChars[MAX_PID_SIZE] = {0};
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000130 int NumPids = 0, PidLength = 0, NumHosts = 0, HostNameLength = 0;
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000131 char *Allocated;
132 int I, J;
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000133 char Hostname[COMPILER_RT_MAX_HOSTLEN];
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000134
Eric Christopherd641b532015-04-28 22:54:51 +0000135 /* Reset filename on NULL, except with env var which is checked by caller. */
136 if (!Filename) {
137 resetFilenameToDefault();
138 return 0;
139 }
Duncan P. N. Exon Smith54cc0e22014-03-20 19:23:55 +0000140
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000141 /* Check the filename for "%p", which indicates a pid-substitution. */
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +0000142 for (I = 0; Filename[I]; ++I)
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000143 if (Filename[I] == '%') {
144 if (Filename[++I] == 'p') {
145 if (!NumPids++) {
146 PidLength = snprintf(PidChars, MAX_PID_SIZE, "%d", getpid());
147 if (PidLength <= 0)
148 return -1;
149 }
150 } else if (Filename[I] == 'h') {
151 if (!NumHosts++)
152 if (COMPILER_RT_GETHOSTNAME(Hostname, COMPILER_RT_MAX_HOSTLEN))
153 return -1;
154 HostNameLength = strlen(Hostname);
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +0000155 }
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000156 }
157
158 if (!(NumPids || NumHosts)) {
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000159 setFilename(Filename, 0);
160 return 0;
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +0000161 }
162
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000163 /* Allocate enough space for the substituted filename. */
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000164 Allocated = malloc(I + NumPids*(PidLength - 2) +
165 NumHosts*(HostNameLength - 2) + 1);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000166 if (!Allocated)
167 return -1;
168
169 /* Construct the new filename. */
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000170 for (I = 0, J = 0; Filename[I]; ++I)
171 if (Filename[I] == '%') {
172 if (Filename[++I] == 'p') {
173 memcpy(Allocated + J, PidChars, PidLength);
174 J += PidLength;
175 }
Vedant Kumara06e8ca2016-01-29 23:52:11 +0000176 else if (Filename[I] == 'h') {
177 memcpy(Allocated + J, Hostname, HostNameLength);
178 J += HostNameLength;
179 }
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000180 /* Drop any unknown substitutions. */
181 } else
182 Allocated[J++] = Filename[I];
183 Allocated[J] = 0;
184
185 /* Use the computed name. */
186 setFilename(Allocated, 1);
187 return 0;
188}
189
Eric Christopherd641b532015-04-28 22:54:51 +0000190static int setFilenameFromEnvironment(void) {
191 const char *Filename = getenv("LLVM_PROFILE_FILE");
192
193 if (!Filename || !Filename[0])
194 return -1;
195
196 return setFilenamePossiblyWithPid(Filename);
197}
198
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000199static void setFilenameAutomatically(void) {
200 if (!setFilenameFromEnvironment())
201 return;
202
Eric Christopherd641b532015-04-28 22:54:51 +0000203 resetFilenameToDefault();
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000204}
205
Xinliang David Liabfd5532015-12-16 03:29:15 +0000206COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000207void __llvm_profile_initialize_file(void) {
208 /* Check if the filename has been initialized. */
209 if (__llvm_profile_CurrentFilename)
210 return;
211
212 /* Detect the filename and truncate. */
213 setFilenameAutomatically();
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000214}
215
Xinliang David Liabfd5532015-12-16 03:29:15 +0000216COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000217void __llvm_profile_set_filename(const char *Filename) {
Eric Christopherd641b532015-04-28 22:54:51 +0000218 setFilenamePossiblyWithPid(Filename);
219}
220
Xinliang David Liabfd5532015-12-16 03:29:15 +0000221COMPILER_RT_VISIBILITY
Eric Christopherd641b532015-04-28 22:54:51 +0000222void __llvm_profile_override_default_filename(const char *Filename) {
223 /* If the env var is set, skip setting filename from argument. */
224 const char *Env_Filename = getenv("LLVM_PROFILE_FILE");
225 if (Env_Filename && Env_Filename[0])
226 return;
227 setFilenamePossiblyWithPid(Filename);
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000228}
229
Xinliang David Liabfd5532015-12-16 03:29:15 +0000230COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000231int __llvm_profile_write_file(void) {
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000232 int rc;
233
Xinliang David Li55d927a2015-12-07 21:18:16 +0000234 GetEnvHook = &getenv;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000235 /* Check the filename. */
Xinliang David Li9ea30642015-12-03 18:31:59 +0000236 if (!__llvm_profile_CurrentFilename) {
237 PROF_ERR("LLVM Profile: Failed to write file : %s\n", "Filename not set");
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000238 return -1;
Xinliang David Li9ea30642015-12-03 18:31:59 +0000239 }
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000240
Xinliang David Lid85c32c2016-01-08 23:42:28 +0000241 /* Check if there is llvm/runtime version mismatch. */
Xinliang David Lia6924212016-01-08 23:31:57 +0000242 if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) {
243 PROF_ERR("LLVM Profile: runtime and instrumentation version mismatch : "
244 "expected %d, but get %d\n",
245 INSTR_PROF_RAW_VERSION,
246 (int)GET_VERSION(__llvm_profile_get_version()));
247 return -1;
248 }
249
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000250 /* Write the file. */
Vasileios Kalintirisc9c7a3e2015-02-25 13:50:18 +0000251 rc = writeFileWithName(__llvm_profile_CurrentFilename);
Xinliang David Li9ea30642015-12-03 18:31:59 +0000252 if (rc)
253 PROF_ERR("LLVM Profile: Failed to write file \"%s\": %s\n",
Justin Bogner66fd5c92015-01-16 20:10:56 +0000254 __llvm_profile_CurrentFilename, strerror(errno));
255 return rc;
Duncan P. N. Exon Smith54cc0e22014-03-20 19:23:55 +0000256}
257
Xinliang David Li6fe18f42015-11-23 04:38:17 +0000258static void writeFileWithoutReturn(void) { __llvm_profile_write_file(); }
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000259
Xinliang David Liabfd5532015-12-16 03:29:15 +0000260COMPILER_RT_VISIBILITY
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000261int __llvm_profile_register_write_file_atexit(void) {
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000262 static int HasBeenRegistered = 0;
263
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000264 if (HasBeenRegistered)
265 return 0;
266
267 HasBeenRegistered = 1;
268 return atexit(writeFileWithoutReturn);
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000269}