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" |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 13 | #include <string.h> |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 14 | |
Joerg Sonnenberger | b1cc6d5 | 2014-05-20 16:37:07 +0000 | [diff] [blame] | 15 | #define UNCONST(ptr) ((void *)(uintptr_t)(ptr)) |
| 16 | |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 17 | static int writeFile(FILE *File) { |
Duncan P. N. Exon Smith | 812dcae | 2014-03-21 18:29:24 +0000 | [diff] [blame] | 18 | /* Match logic in __llvm_profile_write_buffer(). */ |
Justin Bogner | cc0d7ee | 2014-09-04 15:45:31 +0000 | [diff] [blame] | 19 | 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 Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 25 | |
| 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 Smith | 0843988 | 2014-05-16 01:30:24 +0000 | [diff] [blame] | 30 | 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 Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 34 | |
Duncan P. N. Exon Smith | 812dcae | 2014-03-21 18:29:24 +0000 | [diff] [blame] | 35 | /* Create the header. */ |
Reid Kleckner | af6b250 | 2014-05-01 18:52:14 +0000 | [diff] [blame] | 36 | 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 Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 44 | |
| 45 | /* Write the data. */ |
Duncan P. N. Exon Smith | 812dcae | 2014-03-21 18:29:24 +0000 | [diff] [blame] | 46 | #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 Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 50 | CHECK_fwrite(CountersBegin, sizeof(uint64_t), CountersSize, File); |
Duncan P. N. Exon Smith | 812dcae | 2014-03-21 18:29:24 +0000 | [diff] [blame] | 51 | CHECK_fwrite(NamesBegin, sizeof(char), NamesSize, File); |
Duncan P. N. Exon Smith | 0843988 | 2014-05-16 01:30:24 +0000 | [diff] [blame] | 52 | CHECK_fwrite(Zeroes, sizeof(char), Padding, File); |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 53 | #undef CHECK_fwrite |
| 54 | |
Duncan P. N. Exon Smith | 0843988 | 2014-05-16 01:30:24 +0000 | [diff] [blame] | 55 | return 0; |
| 56 | } |
| 57 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 58 | static int writeFileWithName(const char *OutputName) { |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 59 | int RetVal; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 60 | FILE *OutputFile; |
| 61 | if (!OutputName || !OutputName[0]) |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 62 | return -1; |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 63 | |
| 64 | /* Append to the file to support profiling multiple shared objects. */ |
| 65 | OutputFile = fopen(OutputName, "a"); |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 66 | if (!OutputFile) |
| 67 | return -1; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 68 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 69 | RetVal = writeFile(OutputFile); |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 70 | |
| 71 | fclose(OutputFile); |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 72 | return RetVal; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 75 | __attribute__((weak)) int __llvm_profile_OwnsFilename = 0; |
Duncan P. N. Exon Smith | 0843988 | 2014-05-16 01:30:24 +0000 | [diff] [blame] | 76 | __attribute__((weak)) const char *__llvm_profile_CurrentFilename = NULL; |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 77 | |
| 78 | static void setFilename(const char *Filename, int OwnsFilename) { |
| 79 | if (__llvm_profile_OwnsFilename) |
Joerg Sonnenberger | b1cc6d5 | 2014-05-20 16:37:07 +0000 | [diff] [blame] | 80 | free(UNCONST(__llvm_profile_CurrentFilename)); |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 81 | |
Duncan P. N. Exon Smith | 0843988 | 2014-05-16 01:30:24 +0000 | [diff] [blame] | 82 | __llvm_profile_CurrentFilename = Filename; |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 83 | __llvm_profile_OwnsFilename = OwnsFilename; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 86 | static void truncateCurrentFile(void) { |
Duncan P. N. Exon Smith | 0843988 | 2014-05-16 01:30:24 +0000 | [diff] [blame] | 87 | const char *Filename = __llvm_profile_CurrentFilename; |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 88 | 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 | |
| 98 | static void setDefaultFilename(void) { setFilename("default.profraw", 0); } |
| 99 | |
| 100 | int getpid(void); |
| 101 | static int setFilenameFromEnvironment(void) { |
| 102 | const char *Filename = getenv("LLVM_PROFILE_FILE"); |
| 103 | if (!Filename || !Filename[0]) |
| 104 | return -1; |
Duncan P. N. Exon Smith | 54cc0e2 | 2014-03-20 19:23:55 +0000 | [diff] [blame] | 105 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 106 | /* Check the filename for "%p", which indicates a pid-substitution. */ |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 107 | #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 Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 112 | 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 Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 117 | return -1; |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 118 | } |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 119 | if (!NumPids) { |
| 120 | setFilename(Filename, 0); |
| 121 | return 0; |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 124 | /* 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 | |
| 147 | static void setFilenameAutomatically(void) { |
| 148 | if (!setFilenameFromEnvironment()) |
| 149 | return; |
| 150 | |
| 151 | setDefaultFilename(); |
| 152 | } |
| 153 | |
| 154 | __attribute__((visibility("hidden"))) |
| 155 | void __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"))) |
| 166 | void __llvm_profile_set_filename(const char *Filename) { |
| 167 | setFilename(Filename, 0); |
| 168 | truncateCurrentFile(); |
| 169 | } |
| 170 | |
| 171 | __attribute__((visibility("hidden"))) |
| 172 | int __llvm_profile_write_file(void) { |
| 173 | /* Check the filename. */ |
| 174 | if (!__llvm_profile_CurrentFilename) |
| 175 | return -1; |
| 176 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 177 | /* Write the file. */ |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 178 | return writeFileWithName(__llvm_profile_CurrentFilename); |
Duncan P. N. Exon Smith | 54cc0e2 | 2014-03-20 19:23:55 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 181 | static void writeFileWithoutReturn(void) { |
| 182 | __llvm_profile_write_file(); |
| 183 | } |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 184 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 185 | __attribute__((visibility("hidden"))) |
| 186 | int __llvm_profile_register_write_file_atexit(void) { |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 187 | static int HasBeenRegistered = 0; |
| 188 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 189 | if (HasBeenRegistered) |
| 190 | return 0; |
| 191 | |
| 192 | HasBeenRegistered = 1; |
| 193 | return atexit(writeFileWithoutReturn); |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 194 | } |