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