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" |
Joerg Sonnenberger | ef24b41 | 2015-03-09 11:23:29 +0000 | [diff] [blame^] | 11 | #include <errno.h> |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 14 | #include <string.h> |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 15 | |
Joerg Sonnenberger | b1cc6d5 | 2014-05-20 16:37:07 +0000 | [diff] [blame] | 16 | #define UNCONST(ptr) ((void *)(uintptr_t)(ptr)) |
| 17 | |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 18 | static int writeFile(FILE *File) { |
Duncan P. N. Exon Smith | 812dcae | 2014-03-21 18:29:24 +0000 | [diff] [blame] | 19 | /* Match logic in __llvm_profile_write_buffer(). */ |
Justin Bogner | cc0d7ee | 2014-09-04 15:45:31 +0000 | [diff] [blame] | 20 | const __llvm_profile_data *DataBegin = __llvm_profile_begin_data(); |
| 21 | const __llvm_profile_data *DataEnd = __llvm_profile_end_data(); |
| 22 | const uint64_t *CountersBegin = __llvm_profile_begin_counters(); |
| 23 | const uint64_t *CountersEnd = __llvm_profile_end_counters(); |
| 24 | const char *NamesBegin = __llvm_profile_begin_names(); |
| 25 | const char *NamesEnd = __llvm_profile_end_names(); |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 26 | |
| 27 | /* Calculate size of sections. */ |
| 28 | const uint64_t DataSize = DataEnd - DataBegin; |
| 29 | const uint64_t CountersSize = CountersEnd - CountersBegin; |
| 30 | const uint64_t NamesSize = NamesEnd - NamesBegin; |
Duncan P. N. Exon Smith | 0843988 | 2014-05-16 01:30:24 +0000 | [diff] [blame] | 31 | const uint64_t Padding = sizeof(uint64_t) - NamesSize % sizeof(uint64_t); |
| 32 | |
| 33 | /* Enough zeroes for padding. */ |
| 34 | const char Zeroes[sizeof(uint64_t)] = {0}; |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 35 | |
Duncan P. N. Exon Smith | 812dcae | 2014-03-21 18:29:24 +0000 | [diff] [blame] | 36 | /* Create the header. */ |
Reid Kleckner | af6b250 | 2014-05-01 18:52:14 +0000 | [diff] [blame] | 37 | uint64_t Header[PROFILE_HEADER_SIZE]; |
| 38 | Header[0] = __llvm_profile_get_magic(); |
| 39 | Header[1] = __llvm_profile_get_version(); |
| 40 | Header[2] = DataSize; |
| 41 | Header[3] = CountersSize; |
| 42 | Header[4] = NamesSize; |
| 43 | Header[5] = (uintptr_t)CountersBegin; |
| 44 | Header[6] = (uintptr_t)NamesBegin; |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 45 | |
| 46 | /* Write the data. */ |
Duncan P. N. Exon Smith | 812dcae | 2014-03-21 18:29:24 +0000 | [diff] [blame] | 47 | #define CHECK_fwrite(Data, Size, Length, File) \ |
| 48 | do { if (fwrite(Data, Size, Length, File) != Length) return -1; } while (0) |
| 49 | CHECK_fwrite(Header, sizeof(uint64_t), PROFILE_HEADER_SIZE, File); |
| 50 | CHECK_fwrite(DataBegin, sizeof(__llvm_profile_data), DataSize, File); |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 51 | CHECK_fwrite(CountersBegin, sizeof(uint64_t), CountersSize, File); |
Duncan P. N. Exon Smith | 812dcae | 2014-03-21 18:29:24 +0000 | [diff] [blame] | 52 | CHECK_fwrite(NamesBegin, sizeof(char), NamesSize, File); |
Duncan P. N. Exon Smith | 0843988 | 2014-05-16 01:30:24 +0000 | [diff] [blame] | 53 | CHECK_fwrite(Zeroes, sizeof(char), Padding, File); |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 54 | #undef CHECK_fwrite |
| 55 | |
Duncan P. N. Exon Smith | 0843988 | 2014-05-16 01:30:24 +0000 | [diff] [blame] | 56 | return 0; |
| 57 | } |
| 58 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 59 | static int writeFileWithName(const char *OutputName) { |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 60 | int RetVal; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 61 | FILE *OutputFile; |
| 62 | if (!OutputName || !OutputName[0]) |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 63 | return -1; |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 64 | |
| 65 | /* Append to the file to support profiling multiple shared objects. */ |
| 66 | OutputFile = fopen(OutputName, "a"); |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 67 | if (!OutputFile) |
| 68 | return -1; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 69 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 70 | RetVal = writeFile(OutputFile); |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 71 | |
| 72 | fclose(OutputFile); |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 73 | return RetVal; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 76 | __attribute__((weak)) int __llvm_profile_OwnsFilename = 0; |
Duncan P. N. Exon Smith | 0843988 | 2014-05-16 01:30:24 +0000 | [diff] [blame] | 77 | __attribute__((weak)) const char *__llvm_profile_CurrentFilename = NULL; |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 78 | |
| 79 | static void setFilename(const char *Filename, int OwnsFilename) { |
| 80 | if (__llvm_profile_OwnsFilename) |
Joerg Sonnenberger | b1cc6d5 | 2014-05-20 16:37:07 +0000 | [diff] [blame] | 81 | free(UNCONST(__llvm_profile_CurrentFilename)); |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 82 | |
Duncan P. N. Exon Smith | 0843988 | 2014-05-16 01:30:24 +0000 | [diff] [blame] | 83 | __llvm_profile_CurrentFilename = Filename; |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 84 | __llvm_profile_OwnsFilename = OwnsFilename; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 87 | static void truncateCurrentFile(void) { |
Vasileios Kalintiris | c9c7a3e | 2015-02-25 13:50:18 +0000 | [diff] [blame] | 88 | const char *Filename; |
| 89 | FILE *File; |
| 90 | |
| 91 | Filename = __llvm_profile_CurrentFilename; |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 92 | if (!Filename || !Filename[0]) |
| 93 | return; |
| 94 | |
| 95 | /* Truncate the file. Later we'll reopen and append. */ |
Vasileios Kalintiris | c9c7a3e | 2015-02-25 13:50:18 +0000 | [diff] [blame] | 96 | File = fopen(Filename, "w"); |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 97 | if (!File) |
| 98 | return; |
| 99 | fclose(File); |
| 100 | } |
| 101 | |
| 102 | static void setDefaultFilename(void) { setFilename("default.profraw", 0); } |
| 103 | |
| 104 | int getpid(void); |
| 105 | static int setFilenameFromEnvironment(void) { |
| 106 | const char *Filename = getenv("LLVM_PROFILE_FILE"); |
Vasileios Kalintiris | c9c7a3e | 2015-02-25 13:50:18 +0000 | [diff] [blame] | 107 | #define MAX_PID_SIZE 16 |
| 108 | char PidChars[MAX_PID_SIZE] = {0}; |
| 109 | int NumPids = 0, PidLength = 0; |
| 110 | char *Allocated; |
| 111 | int I, J; |
| 112 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 113 | if (!Filename || !Filename[0]) |
| 114 | return -1; |
Duncan P. N. Exon Smith | 54cc0e2 | 2014-03-20 19:23:55 +0000 | [diff] [blame] | 115 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 116 | /* 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] | 117 | 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) |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 122 | return -1; |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 123 | } |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 124 | if (!NumPids) { |
| 125 | setFilename(Filename, 0); |
| 126 | return 0; |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 129 | /* Allocate enough space for the substituted filename. */ |
Vasileios Kalintiris | c9c7a3e | 2015-02-25 13:50:18 +0000 | [diff] [blame] | 130 | Allocated = malloc(I + NumPids*(PidLength - 2) + 1); |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 131 | if (!Allocated) |
| 132 | return -1; |
| 133 | |
| 134 | /* Construct the new filename. */ |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 135 | 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 | |
| 151 | static void setFilenameAutomatically(void) { |
| 152 | if (!setFilenameFromEnvironment()) |
| 153 | return; |
| 154 | |
| 155 | setDefaultFilename(); |
| 156 | } |
| 157 | |
| 158 | __attribute__((visibility("hidden"))) |
| 159 | void __llvm_profile_initialize_file(void) { |
| 160 | /* Check if the filename has been initialized. */ |
| 161 | if (__llvm_profile_CurrentFilename) |
| 162 | return; |
| 163 | |
| 164 | /* Detect the filename and truncate. */ |
| 165 | setFilenameAutomatically(); |
| 166 | truncateCurrentFile(); |
| 167 | } |
| 168 | |
| 169 | __attribute__((visibility("hidden"))) |
| 170 | void __llvm_profile_set_filename(const char *Filename) { |
| 171 | setFilename(Filename, 0); |
| 172 | truncateCurrentFile(); |
| 173 | } |
| 174 | |
| 175 | __attribute__((visibility("hidden"))) |
| 176 | int __llvm_profile_write_file(void) { |
Vasileios Kalintiris | c9c7a3e | 2015-02-25 13:50:18 +0000 | [diff] [blame] | 177 | int rc; |
| 178 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 179 | /* Check the filename. */ |
| 180 | if (!__llvm_profile_CurrentFilename) |
| 181 | return -1; |
| 182 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 183 | /* Write the file. */ |
Vasileios Kalintiris | c9c7a3e | 2015-02-25 13:50:18 +0000 | [diff] [blame] | 184 | rc = writeFileWithName(__llvm_profile_CurrentFilename); |
Justin Bogner | 66fd5c9 | 2015-01-16 20:10:56 +0000 | [diff] [blame] | 185 | if (rc && getenv("LLVM_PROFILE_VERBOSE_ERRORS")) |
| 186 | fprintf(stderr, "LLVM Profile: Failed to write file \"%s\": %s\n", |
| 187 | __llvm_profile_CurrentFilename, strerror(errno)); |
| 188 | return rc; |
Duncan P. N. Exon Smith | 54cc0e2 | 2014-03-20 19:23:55 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 191 | static void writeFileWithoutReturn(void) { |
| 192 | __llvm_profile_write_file(); |
| 193 | } |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 194 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 195 | __attribute__((visibility("hidden"))) |
| 196 | int __llvm_profile_register_write_file_atexit(void) { |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 197 | static int HasBeenRegistered = 0; |
| 198 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 199 | if (HasBeenRegistered) |
| 200 | return 0; |
| 201 | |
| 202 | HasBeenRegistered = 1; |
| 203 | return atexit(writeFileWithoutReturn); |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 204 | } |