blob: 5aef3904b56d55d3f10b9ec5be7e0e99a95370cd [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"
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +000011#include <stdio.h>
12#include <stdlib.h>
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +000013#include <string.h>
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000014
Joerg Sonnenbergerb1cc6d52014-05-20 16:37:07 +000015#define UNCONST(ptr) ((void *)(uintptr_t)(ptr))
16
Duncan P. N. Exon Smithcf4bb962014-03-21 18:29:19 +000017static int writeFile(FILE *File) {
Duncan P. N. Exon Smith812dcae2014-03-21 18:29:24 +000018 /* Match logic in __llvm_profile_write_buffer(). */
Justin Bognercc0d7ee2014-09-04 15:45:31 +000019 const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
20 const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
21 const uint64_t *CountersBegin = __llvm_profile_begin_counters();
22 const uint64_t *CountersEnd = __llvm_profile_end_counters();
23 const char *NamesBegin = __llvm_profile_begin_names();
24 const char *NamesEnd = __llvm_profile_end_names();
Duncan P. N. Exon Smithcf4bb962014-03-21 18:29:19 +000025
26 /* Calculate size of sections. */
27 const uint64_t DataSize = DataEnd - DataBegin;
28 const uint64_t CountersSize = CountersEnd - CountersBegin;
29 const uint64_t NamesSize = NamesEnd - NamesBegin;
Duncan P. N. Exon Smith08439882014-05-16 01:30:24 +000030 const uint64_t Padding = sizeof(uint64_t) - NamesSize % sizeof(uint64_t);
31
32 /* Enough zeroes for padding. */
33 const char Zeroes[sizeof(uint64_t)] = {0};
Duncan P. N. Exon Smithcf4bb962014-03-21 18:29:19 +000034
Duncan P. N. Exon Smith812dcae2014-03-21 18:29:24 +000035 /* Create the header. */
Reid Kleckneraf6b2502014-05-01 18:52:14 +000036 uint64_t Header[PROFILE_HEADER_SIZE];
37 Header[0] = __llvm_profile_get_magic();
38 Header[1] = __llvm_profile_get_version();
39 Header[2] = DataSize;
40 Header[3] = CountersSize;
41 Header[4] = NamesSize;
42 Header[5] = (uintptr_t)CountersBegin;
43 Header[6] = (uintptr_t)NamesBegin;
Duncan P. N. Exon Smithcf4bb962014-03-21 18:29:19 +000044
45 /* Write the data. */
Duncan P. N. Exon Smith812dcae2014-03-21 18:29:24 +000046#define CHECK_fwrite(Data, Size, Length, File) \
47 do { if (fwrite(Data, Size, Length, File) != Length) return -1; } while (0)
48 CHECK_fwrite(Header, sizeof(uint64_t), PROFILE_HEADER_SIZE, File);
49 CHECK_fwrite(DataBegin, sizeof(__llvm_profile_data), DataSize, File);
Duncan P. N. Exon Smithcf4bb962014-03-21 18:29:19 +000050 CHECK_fwrite(CountersBegin, sizeof(uint64_t), CountersSize, File);
Duncan P. N. Exon Smith812dcae2014-03-21 18:29:24 +000051 CHECK_fwrite(NamesBegin, sizeof(char), NamesSize, File);
Duncan P. N. Exon Smith08439882014-05-16 01:30:24 +000052 CHECK_fwrite(Zeroes, sizeof(char), Padding, File);
Duncan P. N. Exon Smithcf4bb962014-03-21 18:29:19 +000053#undef CHECK_fwrite
54
Duncan P. N. Exon Smith08439882014-05-16 01:30:24 +000055 return 0;
56}
57
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +000058static int writeFileWithName(const char *OutputName) {
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +000059 int RetVal;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000060 FILE *OutputFile;
61 if (!OutputName || !OutputName[0])
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +000062 return -1;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +000063
64 /* Append to the file to support profiling multiple shared objects. */
65 OutputFile = fopen(OutputName, "a");
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +000066 if (!OutputFile)
67 return -1;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000068
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +000069 RetVal = writeFile(OutputFile);
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000070
71 fclose(OutputFile);
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +000072 return RetVal;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000073}
74
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +000075__attribute__((weak)) int __llvm_profile_OwnsFilename = 0;
Duncan P. N. Exon Smith08439882014-05-16 01:30:24 +000076__attribute__((weak)) const char *__llvm_profile_CurrentFilename = NULL;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +000077
78static void setFilename(const char *Filename, int OwnsFilename) {
79 if (__llvm_profile_OwnsFilename)
Joerg Sonnenbergerb1cc6d52014-05-20 16:37:07 +000080 free(UNCONST(__llvm_profile_CurrentFilename));
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +000081
Duncan P. N. Exon Smith08439882014-05-16 01:30:24 +000082 __llvm_profile_CurrentFilename = Filename;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +000083 __llvm_profile_OwnsFilename = OwnsFilename;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000084}
85
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +000086static void truncateCurrentFile(void) {
Duncan P. N. Exon Smith08439882014-05-16 01:30:24 +000087 const char *Filename = __llvm_profile_CurrentFilename;
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +000088 if (!Filename || !Filename[0])
89 return;
90
91 /* Truncate the file. Later we'll reopen and append. */
92 FILE *File = fopen(Filename, "w");
93 if (!File)
94 return;
95 fclose(File);
96}
97
98static void setDefaultFilename(void) { setFilename("default.profraw", 0); }
99
100int getpid(void);
101static int setFilenameFromEnvironment(void) {
102 const char *Filename = getenv("LLVM_PROFILE_FILE");
103 if (!Filename || !Filename[0])
104 return -1;
Duncan P. N. Exon Smith54cc0e22014-03-20 19:23:55 +0000105
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000106 /* Check the filename for "%p", which indicates a pid-substitution. */
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000107#define MAX_PID_SIZE 16
108 char PidChars[MAX_PID_SIZE] = {0};
109 int NumPids = 0;
110 int PidLength = 0;
111 int I;
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +0000112 for (I = 0; Filename[I]; ++I)
113 if (Filename[I] == '%' && Filename[++I] == 'p')
114 if (!NumPids++) {
115 PidLength = snprintf(PidChars, MAX_PID_SIZE, "%d", getpid());
116 if (PidLength <= 0)
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000117 return -1;
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +0000118 }
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000119 if (!NumPids) {
120 setFilename(Filename, 0);
121 return 0;
Duncan P. N. Exon Smithe5edc882014-03-21 00:27:48 +0000122 }
123
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000124 /* Allocate enough space for the substituted filename. */
125 char *Allocated = (char*)malloc(I + NumPids*(PidLength - 2) + 1);
126 if (!Allocated)
127 return -1;
128
129 /* Construct the new filename. */
130 int J;
131 for (I = 0, J = 0; Filename[I]; ++I)
132 if (Filename[I] == '%') {
133 if (Filename[++I] == 'p') {
134 memcpy(Allocated + J, PidChars, PidLength);
135 J += PidLength;
136 }
137 /* Drop any unknown substitutions. */
138 } else
139 Allocated[J++] = Filename[I];
140 Allocated[J] = 0;
141
142 /* Use the computed name. */
143 setFilename(Allocated, 1);
144 return 0;
145}
146
147static void setFilenameAutomatically(void) {
148 if (!setFilenameFromEnvironment())
149 return;
150
151 setDefaultFilename();
152}
153
154__attribute__((visibility("hidden")))
155void __llvm_profile_initialize_file(void) {
156 /* Check if the filename has been initialized. */
157 if (__llvm_profile_CurrentFilename)
158 return;
159
160 /* Detect the filename and truncate. */
161 setFilenameAutomatically();
162 truncateCurrentFile();
163}
164
165__attribute__((visibility("hidden")))
166void __llvm_profile_set_filename(const char *Filename) {
167 setFilename(Filename, 0);
168 truncateCurrentFile();
169}
170
171__attribute__((visibility("hidden")))
172int __llvm_profile_write_file(void) {
173 /* Check the filename. */
174 if (!__llvm_profile_CurrentFilename)
175 return -1;
176
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000177 /* Write the file. */
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000178 return writeFileWithName(__llvm_profile_CurrentFilename);
Duncan P. N. Exon Smith54cc0e22014-03-20 19:23:55 +0000179}
180
Duncan P. N. Exon Smith4543aac2014-03-21 00:27:50 +0000181static void writeFileWithoutReturn(void) {
182 __llvm_profile_write_file();
183}
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000184
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000185__attribute__((visibility("hidden")))
186int __llvm_profile_register_write_file_atexit(void) {
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000187 static int HasBeenRegistered = 0;
188
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000189 if (HasBeenRegistered)
190 return 0;
191
192 HasBeenRegistered = 1;
193 return atexit(writeFileWithoutReturn);
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +0000194}