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 | |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 15 | static int writeFile(FILE *File) { |
Duncan P. N. Exon Smith | 812dcae | 2014-03-21 18:29:24 +0000 | [diff] [blame] | 16 | /* Match logic in __llvm_profile_write_buffer(). */ |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 17 | const __llvm_profile_data *DataBegin = __llvm_profile_data_begin(); |
| 18 | const __llvm_profile_data *DataEnd = __llvm_profile_data_end(); |
| 19 | const uint64_t *CountersBegin = __llvm_profile_counters_begin(); |
| 20 | const uint64_t *CountersEnd = __llvm_profile_counters_end(); |
| 21 | const char *NamesBegin = __llvm_profile_names_begin(); |
| 22 | const char *NamesEnd = __llvm_profile_names_end(); |
| 23 | |
| 24 | /* Calculate size of sections. */ |
| 25 | const uint64_t DataSize = DataEnd - DataBegin; |
| 26 | const uint64_t CountersSize = CountersEnd - CountersBegin; |
| 27 | const uint64_t NamesSize = NamesEnd - NamesBegin; |
Duncan P. N. Exon Smith | 0843988 | 2014-05-16 01:30:24 +0000 | [diff] [blame^] | 28 | const uint64_t Padding = sizeof(uint64_t) - NamesSize % sizeof(uint64_t); |
| 29 | |
| 30 | /* Enough zeroes for padding. */ |
| 31 | const char Zeroes[sizeof(uint64_t)] = {0}; |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 32 | |
Duncan P. N. Exon Smith | 812dcae | 2014-03-21 18:29:24 +0000 | [diff] [blame] | 33 | /* Create the header. */ |
Reid Kleckner | af6b250 | 2014-05-01 18:52:14 +0000 | [diff] [blame] | 34 | uint64_t Header[PROFILE_HEADER_SIZE]; |
| 35 | Header[0] = __llvm_profile_get_magic(); |
| 36 | Header[1] = __llvm_profile_get_version(); |
| 37 | Header[2] = DataSize; |
| 38 | Header[3] = CountersSize; |
| 39 | Header[4] = NamesSize; |
| 40 | Header[5] = (uintptr_t)CountersBegin; |
| 41 | Header[6] = (uintptr_t)NamesBegin; |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 42 | |
| 43 | /* Write the data. */ |
Duncan P. N. Exon Smith | 812dcae | 2014-03-21 18:29:24 +0000 | [diff] [blame] | 44 | #define CHECK_fwrite(Data, Size, Length, File) \ |
| 45 | do { if (fwrite(Data, Size, Length, File) != Length) return -1; } while (0) |
| 46 | CHECK_fwrite(Header, sizeof(uint64_t), PROFILE_HEADER_SIZE, File); |
| 47 | CHECK_fwrite(DataBegin, sizeof(__llvm_profile_data), DataSize, File); |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 48 | CHECK_fwrite(CountersBegin, sizeof(uint64_t), CountersSize, File); |
Duncan P. N. Exon Smith | 812dcae | 2014-03-21 18:29:24 +0000 | [diff] [blame] | 49 | CHECK_fwrite(NamesBegin, sizeof(char), NamesSize, File); |
Duncan P. N. Exon Smith | 0843988 | 2014-05-16 01:30:24 +0000 | [diff] [blame^] | 50 | CHECK_fwrite(Zeroes, sizeof(char), Padding, File); |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 51 | #undef CHECK_fwrite |
| 52 | |
Duncan P. N. Exon Smith | 0843988 | 2014-05-16 01:30:24 +0000 | [diff] [blame^] | 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | typedef struct __llvm_profile_writer { |
| 57 | struct __llvm_profile_writer *Next; |
| 58 | int (*Data)(FILE *); |
| 59 | } __llvm_profile_writer; |
| 60 | |
| 61 | __attribute__((weak)) __llvm_profile_writer *__llvm_profile_HeadWriter = NULL; |
| 62 | static __llvm_profile_writer Writer = {NULL, writeFile}; |
| 63 | |
| 64 | __attribute__((visibility("hidden"))) |
| 65 | void __llvm_profile_register_write_file(void) { |
| 66 | static int HasBeenRegistered = 0; |
| 67 | |
| 68 | if (HasBeenRegistered) |
| 69 | return; |
| 70 | |
| 71 | HasBeenRegistered = 1; |
| 72 | Writer.Next = __llvm_profile_HeadWriter; |
| 73 | __llvm_profile_HeadWriter = &Writer; |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 76 | static int writeFileWithName(const char *OutputName) { |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 77 | int RetVal; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 78 | FILE *OutputFile; |
| 79 | if (!OutputName || !OutputName[0]) |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 80 | return -1; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 81 | OutputFile = fopen(OutputName, "w"); |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 82 | if (!OutputFile) |
| 83 | return -1; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 84 | |
Duncan P. N. Exon Smith | 0843988 | 2014-05-16 01:30:24 +0000 | [diff] [blame^] | 85 | __llvm_profile_writer *Writer = __llvm_profile_HeadWriter; |
| 86 | if (Writer) |
| 87 | for (; Writer; Writer = Writer->Next) { |
| 88 | RetVal = Writer->Data(OutputFile); |
| 89 | if (RetVal != 0) |
| 90 | break; |
| 91 | } |
| 92 | else |
| 93 | // Default to calling this executable's writeFile. |
| 94 | RetVal = writeFile(OutputFile); |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 95 | |
| 96 | fclose(OutputFile); |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 97 | return RetVal; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Duncan P. N. Exon Smith | 0843988 | 2014-05-16 01:30:24 +0000 | [diff] [blame^] | 100 | __attribute__((weak)) const char *__llvm_profile_CurrentFilename = NULL; |
| 101 | __attribute__((weak)) void __llvm_profile_set_filename(const char *Filename) { |
| 102 | __llvm_profile_CurrentFilename = Filename; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 105 | int getpid(void); |
Duncan P. N. Exon Smith | 0843988 | 2014-05-16 01:30:24 +0000 | [diff] [blame^] | 106 | __attribute__((weak)) int __llvm_profile_write_file(void) { |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 107 | char *AllocatedFilename = NULL; |
| 108 | int I, J; |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 109 | int RetVal; |
Duncan P. N. Exon Smith | 54cc0e2 | 2014-03-20 19:23:55 +0000 | [diff] [blame] | 110 | |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 111 | #define MAX_PID_SIZE 16 |
| 112 | char PidChars[MAX_PID_SIZE] = { 0 }; |
| 113 | int PidLength = 0; |
| 114 | int NumPids = 0; |
| 115 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 116 | /* Get the filename. */ |
Duncan P. N. Exon Smith | 0843988 | 2014-05-16 01:30:24 +0000 | [diff] [blame^] | 117 | const char *Filename = __llvm_profile_CurrentFilename; |
Duncan P. N. Exon Smith | 54cc0e2 | 2014-03-20 19:23:55 +0000 | [diff] [blame] | 118 | #define UPDATE_FILENAME(NextFilename) \ |
| 119 | if (!Filename || !Filename[0]) Filename = NextFilename |
| 120 | UPDATE_FILENAME(getenv("LLVM_PROFILE_FILE")); |
Duncan P. N. Exon Smith | 21b98a6 | 2014-03-24 21:53:42 +0000 | [diff] [blame] | 121 | UPDATE_FILENAME("default.profraw"); |
Duncan P. N. Exon Smith | 54cc0e2 | 2014-03-20 19:23:55 +0000 | [diff] [blame] | 122 | #undef UPDATE_FILENAME |
| 123 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 124 | /* 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] | 125 | for (I = 0; Filename[I]; ++I) |
| 126 | if (Filename[I] == '%' && Filename[++I] == 'p') |
| 127 | if (!NumPids++) { |
| 128 | PidLength = snprintf(PidChars, MAX_PID_SIZE, "%d", getpid()); |
| 129 | if (PidLength <= 0) |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 130 | return -1; |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 131 | } |
| 132 | if (NumPids) { |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 133 | /* Allocate enough space for the substituted filename. */ |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 134 | AllocatedFilename = (char*)malloc(I + NumPids*(PidLength - 2) + 1); |
| 135 | if (!AllocatedFilename) |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 136 | return -1; |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 137 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 138 | /* Construct the new filename. */ |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 139 | for (I = 0, J = 0; Filename[I]; ++I) |
| 140 | if (Filename[I] == '%') { |
| 141 | if (Filename[++I] == 'p') { |
| 142 | memcpy(AllocatedFilename + J, PidChars, PidLength); |
| 143 | J += PidLength; |
| 144 | } |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 145 | /* Drop any unknown substitutions. */ |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 146 | } else |
| 147 | AllocatedFilename[J++] = Filename[I]; |
| 148 | AllocatedFilename[J] = 0; |
| 149 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 150 | /* Actually use the computed name. */ |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 151 | Filename = AllocatedFilename; |
| 152 | } |
| 153 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 154 | /* Write the file. */ |
| 155 | RetVal = writeFileWithName(Filename); |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 156 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 157 | /* Free the filename. */ |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 158 | if (AllocatedFilename) |
| 159 | free(AllocatedFilename); |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 160 | |
| 161 | return RetVal; |
Duncan P. N. Exon Smith | 54cc0e2 | 2014-03-20 19:23:55 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 164 | static void writeFileWithoutReturn(void) { |
| 165 | __llvm_profile_write_file(); |
| 166 | } |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 167 | |
Duncan P. N. Exon Smith | 0843988 | 2014-05-16 01:30:24 +0000 | [diff] [blame^] | 168 | __attribute__((weak)) int __llvm_profile_register_write_file_atexit(void) { |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 169 | static int HasBeenRegistered = 0; |
| 170 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 171 | if (HasBeenRegistered) |
| 172 | return 0; |
| 173 | |
| 174 | HasBeenRegistered = 1; |
| 175 | return atexit(writeFileWithoutReturn); |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 176 | } |