Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 1 | /*===- InstrProfilingFile.c - Write instrumentation to a file -------------===*\ |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 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" |
Diego Novillo | eae9514 | 2015-07-09 17:21:52 +0000 | [diff] [blame] | 11 | #include "InstrProfilingUtil.h" |
Joerg Sonnenberger | ef24b41 | 2015-03-09 11:23:29 +0000 | [diff] [blame] | 12 | #include <errno.h> |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 15 | #include <string.h> |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 16 | |
Joerg Sonnenberger | b1cc6d5 | 2014-05-20 16:37:07 +0000 | [diff] [blame] | 17 | #define UNCONST(ptr) ((void *)(uintptr_t)(ptr)) |
| 18 | |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 19 | static int writeFile(FILE *File) { |
Duncan P. N. Exon Smith | 812dcae | 2014-03-21 18:29:24 +0000 | [diff] [blame] | 20 | /* Match logic in __llvm_profile_write_buffer(). */ |
Justin Bogner | cc0d7ee | 2014-09-04 15:45:31 +0000 | [diff] [blame] | 21 | const __llvm_profile_data *DataBegin = __llvm_profile_begin_data(); |
| 22 | const __llvm_profile_data *DataEnd = __llvm_profile_end_data(); |
| 23 | const uint64_t *CountersBegin = __llvm_profile_begin_counters(); |
| 24 | const uint64_t *CountersEnd = __llvm_profile_end_counters(); |
| 25 | const char *NamesBegin = __llvm_profile_begin_names(); |
| 26 | const char *NamesEnd = __llvm_profile_end_names(); |
Betul Buyukkurt | 808385f | 2015-11-18 18:12:35 +0000 | [diff] [blame^] | 27 | uint8_t *ValueDataBegin = NULL; |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 28 | |
| 29 | /* Calculate size of sections. */ |
| 30 | const uint64_t DataSize = DataEnd - DataBegin; |
| 31 | const uint64_t CountersSize = CountersEnd - CountersBegin; |
| 32 | const uint64_t NamesSize = NamesEnd - NamesBegin; |
Betul Buyukkurt | 808385f | 2015-11-18 18:12:35 +0000 | [diff] [blame^] | 33 | const uint8_t Padding = __llvm_profile_get_num_padding_bytes(NamesSize); |
| 34 | const uint64_t ValueDataSize = |
| 35 | __llvm_profile_gather_value_data(&ValueDataBegin); |
Duncan P. N. Exon Smith | 0843988 | 2014-05-16 01:30:24 +0000 | [diff] [blame] | 36 | |
| 37 | /* Enough zeroes for padding. */ |
| 38 | const char Zeroes[sizeof(uint64_t)] = {0}; |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 39 | |
Duncan P. N. Exon Smith | 812dcae | 2014-03-21 18:29:24 +0000 | [diff] [blame] | 40 | /* Create the header. */ |
Xinliang David Li | 4da5de9 | 2015-10-16 22:21:56 +0000 | [diff] [blame] | 41 | __llvm_profile_header Header; |
Xinliang David Li | b6c81d2 | 2015-11-13 22:33:07 +0000 | [diff] [blame] | 42 | |
| 43 | if (!DataSize) |
| 44 | return 0; |
| 45 | |
Xinliang David Li | 4da5de9 | 2015-10-16 22:21:56 +0000 | [diff] [blame] | 46 | Header.Magic = __llvm_profile_get_magic(); |
| 47 | Header.Version = __llvm_profile_get_version(); |
| 48 | Header.DataSize = DataSize; |
| 49 | Header.CountersSize = CountersSize; |
| 50 | Header.NamesSize = NamesSize; |
| 51 | Header.CountersDelta = (uintptr_t)CountersBegin; |
| 52 | Header.NamesDelta = (uintptr_t)NamesBegin; |
Betul Buyukkurt | 808385f | 2015-11-18 18:12:35 +0000 | [diff] [blame^] | 53 | Header.ValueKindLast = VK_LAST; |
| 54 | Header.ValueDataSize = ValueDataSize; |
| 55 | Header.ValueDataDelta = (uintptr_t)ValueDataBegin; |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 56 | |
| 57 | /* Write the data. */ |
Duncan P. N. Exon Smith | 812dcae | 2014-03-21 18:29:24 +0000 | [diff] [blame] | 58 | #define CHECK_fwrite(Data, Size, Length, File) \ |
| 59 | do { if (fwrite(Data, Size, Length, File) != Length) return -1; } while (0) |
Betul Buyukkurt | 808385f | 2015-11-18 18:12:35 +0000 | [diff] [blame^] | 60 | CHECK_fwrite(&Header, sizeof(__llvm_profile_header), 1, File); |
Duncan P. N. Exon Smith | 812dcae | 2014-03-21 18:29:24 +0000 | [diff] [blame] | 61 | CHECK_fwrite(DataBegin, sizeof(__llvm_profile_data), DataSize, File); |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 62 | CHECK_fwrite(CountersBegin, sizeof(uint64_t), CountersSize, File); |
Duncan P. N. Exon Smith | 812dcae | 2014-03-21 18:29:24 +0000 | [diff] [blame] | 63 | CHECK_fwrite(NamesBegin, sizeof(char), NamesSize, File); |
Duncan P. N. Exon Smith | 0843988 | 2014-05-16 01:30:24 +0000 | [diff] [blame] | 64 | CHECK_fwrite(Zeroes, sizeof(char), Padding, File); |
Betul Buyukkurt | 808385f | 2015-11-18 18:12:35 +0000 | [diff] [blame^] | 65 | CHECK_fwrite(ValueDataBegin, |
| 66 | sizeof(__llvm_profile_value_data), ValueDataSize, File); |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 67 | #undef CHECK_fwrite |
Betul Buyukkurt | 808385f | 2015-11-18 18:12:35 +0000 | [diff] [blame^] | 68 | free(ValueDataBegin); |
Duncan P. N. Exon Smith | 0843988 | 2014-05-16 01:30:24 +0000 | [diff] [blame] | 69 | return 0; |
| 70 | } |
| 71 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 72 | static int writeFileWithName(const char *OutputName) { |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 73 | int RetVal; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 74 | FILE *OutputFile; |
| 75 | if (!OutputName || !OutputName[0]) |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 76 | return -1; |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 77 | |
| 78 | /* Append to the file to support profiling multiple shared objects. */ |
| 79 | OutputFile = fopen(OutputName, "a"); |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 80 | if (!OutputFile) |
| 81 | return -1; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 82 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 83 | RetVal = writeFile(OutputFile); |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 84 | |
| 85 | fclose(OutputFile); |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 86 | return RetVal; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 89 | __attribute__((weak)) int __llvm_profile_OwnsFilename = 0; |
Duncan P. N. Exon Smith | 0843988 | 2014-05-16 01:30:24 +0000 | [diff] [blame] | 90 | __attribute__((weak)) const char *__llvm_profile_CurrentFilename = NULL; |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 91 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 92 | static void truncateCurrentFile(void) { |
Vasileios Kalintiris | c9c7a3e | 2015-02-25 13:50:18 +0000 | [diff] [blame] | 93 | const char *Filename; |
| 94 | FILE *File; |
| 95 | |
| 96 | Filename = __llvm_profile_CurrentFilename; |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 97 | if (!Filename || !Filename[0]) |
| 98 | return; |
| 99 | |
Diego Novillo | eae9514 | 2015-07-09 17:21:52 +0000 | [diff] [blame] | 100 | /* Create the directory holding the file, if needed. */ |
| 101 | if (strchr(Filename, '/')) { |
| 102 | char *Copy = malloc(strlen(Filename) + 1); |
| 103 | strcpy(Copy, Filename); |
| 104 | __llvm_profile_recursive_mkdir(Copy); |
Alexey Samsonov | 6585b76 | 2015-07-15 22:50:39 +0000 | [diff] [blame] | 105 | free(Copy); |
Diego Novillo | eae9514 | 2015-07-09 17:21:52 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 108 | /* Truncate the file. Later we'll reopen and append. */ |
Vasileios Kalintiris | c9c7a3e | 2015-02-25 13:50:18 +0000 | [diff] [blame] | 109 | File = fopen(Filename, "w"); |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 110 | if (!File) |
| 111 | return; |
| 112 | fclose(File); |
| 113 | } |
| 114 | |
Eric Christopher | d641b53 | 2015-04-28 22:54:51 +0000 | [diff] [blame] | 115 | static void setFilename(const char *Filename, int OwnsFilename) { |
| 116 | /* Check if this is a new filename and therefore needs truncation. */ |
| 117 | int NewFile = !__llvm_profile_CurrentFilename || |
| 118 | (Filename && strcmp(Filename, __llvm_profile_CurrentFilename)); |
| 119 | if (__llvm_profile_OwnsFilename) |
| 120 | free(UNCONST(__llvm_profile_CurrentFilename)); |
| 121 | |
| 122 | __llvm_profile_CurrentFilename = Filename; |
| 123 | __llvm_profile_OwnsFilename = OwnsFilename; |
| 124 | |
| 125 | /* If not a new file, append to support profiling multiple shared objects. */ |
| 126 | if (NewFile) |
| 127 | truncateCurrentFile(); |
| 128 | } |
| 129 | |
| 130 | static void resetFilenameToDefault(void) { setFilename("default.profraw", 0); } |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 131 | |
| 132 | int getpid(void); |
Eric Christopher | d641b53 | 2015-04-28 22:54:51 +0000 | [diff] [blame] | 133 | static int setFilenamePossiblyWithPid(const char *Filename) { |
Vasileios Kalintiris | c9c7a3e | 2015-02-25 13:50:18 +0000 | [diff] [blame] | 134 | #define MAX_PID_SIZE 16 |
| 135 | char PidChars[MAX_PID_SIZE] = {0}; |
| 136 | int NumPids = 0, PidLength = 0; |
| 137 | char *Allocated; |
| 138 | int I, J; |
| 139 | |
Eric Christopher | d641b53 | 2015-04-28 22:54:51 +0000 | [diff] [blame] | 140 | /* Reset filename on NULL, except with env var which is checked by caller. */ |
| 141 | if (!Filename) { |
| 142 | resetFilenameToDefault(); |
| 143 | return 0; |
| 144 | } |
Duncan P. N. Exon Smith | 54cc0e2 | 2014-03-20 19:23:55 +0000 | [diff] [blame] | 145 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 146 | /* Check the filename for "%p", which indicates a pid-substitution. */ |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 147 | for (I = 0; Filename[I]; ++I) |
| 148 | if (Filename[I] == '%' && Filename[++I] == 'p') |
| 149 | if (!NumPids++) { |
| 150 | PidLength = snprintf(PidChars, MAX_PID_SIZE, "%d", getpid()); |
| 151 | if (PidLength <= 0) |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 152 | return -1; |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 153 | } |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 154 | if (!NumPids) { |
| 155 | setFilename(Filename, 0); |
| 156 | return 0; |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 159 | /* Allocate enough space for the substituted filename. */ |
Vasileios Kalintiris | c9c7a3e | 2015-02-25 13:50:18 +0000 | [diff] [blame] | 160 | Allocated = malloc(I + NumPids*(PidLength - 2) + 1); |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 161 | if (!Allocated) |
| 162 | return -1; |
| 163 | |
| 164 | /* Construct the new filename. */ |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 165 | for (I = 0, J = 0; Filename[I]; ++I) |
| 166 | if (Filename[I] == '%') { |
| 167 | if (Filename[++I] == 'p') { |
| 168 | memcpy(Allocated + J, PidChars, PidLength); |
| 169 | J += PidLength; |
| 170 | } |
| 171 | /* Drop any unknown substitutions. */ |
| 172 | } else |
| 173 | Allocated[J++] = Filename[I]; |
| 174 | Allocated[J] = 0; |
| 175 | |
| 176 | /* Use the computed name. */ |
| 177 | setFilename(Allocated, 1); |
| 178 | return 0; |
| 179 | } |
| 180 | |
Eric Christopher | d641b53 | 2015-04-28 22:54:51 +0000 | [diff] [blame] | 181 | static int setFilenameFromEnvironment(void) { |
| 182 | const char *Filename = getenv("LLVM_PROFILE_FILE"); |
| 183 | |
| 184 | if (!Filename || !Filename[0]) |
| 185 | return -1; |
| 186 | |
| 187 | return setFilenamePossiblyWithPid(Filename); |
| 188 | } |
| 189 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 190 | static void setFilenameAutomatically(void) { |
| 191 | if (!setFilenameFromEnvironment()) |
| 192 | return; |
| 193 | |
Eric Christopher | d641b53 | 2015-04-28 22:54:51 +0000 | [diff] [blame] | 194 | resetFilenameToDefault(); |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | __attribute__((visibility("hidden"))) |
| 198 | void __llvm_profile_initialize_file(void) { |
| 199 | /* Check if the filename has been initialized. */ |
| 200 | if (__llvm_profile_CurrentFilename) |
| 201 | return; |
| 202 | |
| 203 | /* Detect the filename and truncate. */ |
| 204 | setFilenameAutomatically(); |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | __attribute__((visibility("hidden"))) |
| 208 | void __llvm_profile_set_filename(const char *Filename) { |
Eric Christopher | d641b53 | 2015-04-28 22:54:51 +0000 | [diff] [blame] | 209 | setFilenamePossiblyWithPid(Filename); |
| 210 | } |
| 211 | |
| 212 | __attribute__((visibility("hidden"))) |
| 213 | void __llvm_profile_override_default_filename(const char *Filename) { |
| 214 | /* If the env var is set, skip setting filename from argument. */ |
| 215 | const char *Env_Filename = getenv("LLVM_PROFILE_FILE"); |
| 216 | if (Env_Filename && Env_Filename[0]) |
| 217 | return; |
| 218 | setFilenamePossiblyWithPid(Filename); |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | __attribute__((visibility("hidden"))) |
| 222 | int __llvm_profile_write_file(void) { |
Vasileios Kalintiris | c9c7a3e | 2015-02-25 13:50:18 +0000 | [diff] [blame] | 223 | int rc; |
| 224 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 225 | /* Check the filename. */ |
| 226 | if (!__llvm_profile_CurrentFilename) |
| 227 | return -1; |
| 228 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 229 | /* Write the file. */ |
Vasileios Kalintiris | c9c7a3e | 2015-02-25 13:50:18 +0000 | [diff] [blame] | 230 | rc = writeFileWithName(__llvm_profile_CurrentFilename); |
Justin Bogner | 66fd5c9 | 2015-01-16 20:10:56 +0000 | [diff] [blame] | 231 | if (rc && getenv("LLVM_PROFILE_VERBOSE_ERRORS")) |
| 232 | fprintf(stderr, "LLVM Profile: Failed to write file \"%s\": %s\n", |
| 233 | __llvm_profile_CurrentFilename, strerror(errno)); |
| 234 | return rc; |
Duncan P. N. Exon Smith | 54cc0e2 | 2014-03-20 19:23:55 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 237 | static void writeFileWithoutReturn(void) { |
| 238 | __llvm_profile_write_file(); |
| 239 | } |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 240 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 241 | __attribute__((visibility("hidden"))) |
| 242 | int __llvm_profile_register_write_file_atexit(void) { |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 243 | static int HasBeenRegistered = 0; |
| 244 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 245 | if (HasBeenRegistered) |
| 246 | return 0; |
| 247 | |
| 248 | HasBeenRegistered = 1; |
| 249 | return atexit(writeFileWithoutReturn); |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 250 | } |