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" |
Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 11 | #include "InstrProfilingInternal.h" |
Xinliang David Li | 6e55716 | 2015-11-18 21:11:46 +0000 | [diff] [blame] | 12 | #include "InstrProfilingUtil.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> |
Xinliang David Li | 71eddbf | 2016-05-20 04:52:27 +0000 | [diff] [blame] | 17 | #ifdef _MSC_VER |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 18 | /* For _alloca. */ |
Xinliang David Li | 71eddbf | 2016-05-20 04:52:27 +0000 | [diff] [blame] | 19 | #include <malloc.h> |
Xinliang David Li | fb320a1 | 2016-05-20 05:40:07 +0000 | [diff] [blame] | 20 | #endif |
Xinliang David Li | 71eddbf | 2016-05-20 04:52:27 +0000 | [diff] [blame] | 21 | |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 22 | #define MAX_PID_SIZE 16 |
| 23 | /* Data structure holding the result of parsed filename pattern. */ |
| 24 | typedef struct lprofFilename { |
| 25 | /* File name string possibly with %p or %h specifiers. */ |
| 26 | const char *FilenamePat; |
| 27 | char PidChars[MAX_PID_SIZE]; |
| 28 | char Hostname[COMPILER_RT_MAX_HOSTLEN]; |
| 29 | unsigned NumPids; |
| 30 | unsigned NumHosts; |
| 31 | } lprofFilename; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 32 | |
Vedant Kumar | 33b8b64 | 2016-06-08 01:33:15 +0000 | [diff] [blame] | 33 | lprofFilename lprofCurFilename = {0, {0}, {0}, 0, 0}; |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 34 | |
| 35 | int getpid(void); |
| 36 | static int getCurFilenameLength(); |
| 37 | static const char *getCurFilename(char *FilenameBuf); |
Joerg Sonnenberger | b1cc6d5 | 2014-05-20 16:37:07 +0000 | [diff] [blame] | 38 | |
Xinliang David Li | 2d5bf07 | 2015-11-21 04:16:42 +0000 | [diff] [blame] | 39 | /* Return 1 if there is an error, otherwise return 0. */ |
| 40 | static uint32_t fileWriter(ProfDataIOVec *IOVecs, uint32_t NumIOVecs, |
| 41 | void **WriterCtx) { |
| 42 | uint32_t I; |
| 43 | FILE *File = (FILE *)*WriterCtx; |
| 44 | for (I = 0; I < NumIOVecs; I++) { |
| 45 | if (fwrite(IOVecs[I].Data, IOVecs[I].ElmSize, IOVecs[I].NumElm, File) != |
| 46 | IOVecs[I].NumElm) |
| 47 | return 1; |
| 48 | } |
| 49 | return 0; |
Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 50 | } |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 51 | |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 52 | COMPILER_RT_VISIBILITY ProfBufferIO * |
Xinliang David Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 53 | lprofCreateBufferIOInternal(void *File, uint32_t BufferSz) { |
Xinliang David Li | 609fae3 | 2016-05-13 18:26:26 +0000 | [diff] [blame] | 54 | FreeHook = &free; |
| 55 | DynamicBufferIOBuffer = (uint8_t *)calloc(BufferSz, 1); |
| 56 | VPBufferSize = BufferSz; |
| 57 | return lprofCreateBufferIO(fileWriter, File); |
| 58 | } |
| 59 | |
| 60 | static void setupIOBuffer() { |
| 61 | const char *BufferSzStr = 0; |
| 62 | BufferSzStr = getenv("LLVM_VP_BUFFER_SIZE"); |
| 63 | if (BufferSzStr && BufferSzStr[0]) { |
| 64 | VPBufferSize = atoi(BufferSzStr); |
| 65 | DynamicBufferIOBuffer = (uint8_t *)calloc(VPBufferSize, 1); |
| 66 | } |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 69 | /* Write profile data to file \c OutputName. */ |
| 70 | static int writeFile(const char *OutputName) { |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 71 | int RetVal; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 72 | FILE *OutputFile; |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 73 | |
| 74 | /* Append to the file to support profiling multiple shared objects. */ |
Xinliang David Li | be49271 | 2015-12-15 22:18:11 +0000 | [diff] [blame] | 75 | OutputFile = fopen(OutputName, "ab"); |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 76 | if (!OutputFile) |
| 77 | return -1; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 78 | |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 79 | FreeHook = &free; |
| 80 | setupIOBuffer(); |
| 81 | RetVal = lprofWriteData(fileWriter, OutputFile, lprofGetVPDataReader()); |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 82 | |
| 83 | fclose(OutputFile); |
Duncan P. N. Exon Smith | 4543aac | 2014-03-21 00:27:50 +0000 | [diff] [blame] | 84 | return RetVal; |
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; |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 89 | char *FilenameBuf; |
Vasileios Kalintiris | c9c7a3e | 2015-02-25 13:50:18 +0000 | [diff] [blame] | 90 | FILE *File; |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 91 | int Length; |
Vasileios Kalintiris | c9c7a3e | 2015-02-25 13:50:18 +0000 | [diff] [blame] | 92 | |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 93 | Length = getCurFilenameLength(); |
| 94 | FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1); |
| 95 | Filename = getCurFilename(FilenameBuf); |
| 96 | if (!Filename) |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 97 | return; |
| 98 | |
Diego Novillo | eae9514 | 2015-07-09 17:21:52 +0000 | [diff] [blame] | 99 | /* Create the directory holding the file, if needed. */ |
Sean Silva | c2feac7 | 2016-03-28 21:32:46 +0000 | [diff] [blame] | 100 | if (strchr(Filename, '/') || strchr(Filename, '\\')) { |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 101 | char *Copy = (char *)COMPILER_RT_ALLOCA(Length + 1); |
| 102 | strncpy(Copy, Filename, Length + 1); |
Diego Novillo | eae9514 | 2015-07-09 17:21:52 +0000 | [diff] [blame] | 103 | __llvm_profile_recursive_mkdir(Copy); |
| 104 | } |
| 105 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 106 | /* Truncate the file. Later we'll reopen and append. */ |
Vasileios Kalintiris | c9c7a3e | 2015-02-25 13:50:18 +0000 | [diff] [blame] | 107 | File = fopen(Filename, "w"); |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 108 | if (!File) |
| 109 | return; |
| 110 | fclose(File); |
| 111 | } |
| 112 | |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 113 | static void resetFilenameToDefault(void) { |
Xinliang David Li | 7f08d12 | 2016-05-25 17:30:15 +0000 | [diff] [blame] | 114 | memset(&lprofCurFilename, 0, sizeof(lprofCurFilename)); |
| 115 | lprofCurFilename.FilenamePat = "default.profraw"; |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 116 | } |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 117 | |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 118 | /* Parses the pattern string \p FilenamePat and store the result to |
| 119 | * lprofcurFilename structure. */ |
Xinliang David Li | 7f08d12 | 2016-05-25 17:30:15 +0000 | [diff] [blame] | 120 | |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 121 | static int parseFilenamePattern(const char *FilenamePat) { |
| 122 | int NumPids = 0, NumHosts = 0, I; |
Xinliang David Li | 7f08d12 | 2016-05-25 17:30:15 +0000 | [diff] [blame] | 123 | char *PidChars = &lprofCurFilename.PidChars[0]; |
| 124 | char *Hostname = &lprofCurFilename.Hostname[0]; |
Vasileios Kalintiris | c9c7a3e | 2015-02-25 13:50:18 +0000 | [diff] [blame] | 125 | |
Xinliang David Li | 7f08d12 | 2016-05-25 17:30:15 +0000 | [diff] [blame] | 126 | lprofCurFilename.FilenamePat = FilenamePat; |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 127 | /* Check the filename for "%p", which indicates a pid-substitution. */ |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 128 | for (I = 0; FilenamePat[I]; ++I) |
| 129 | if (FilenamePat[I] == '%') { |
| 130 | if (FilenamePat[++I] == 'p') { |
Vedant Kumar | a06e8ca | 2016-01-29 23:52:11 +0000 | [diff] [blame] | 131 | if (!NumPids++) { |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 132 | if (snprintf(PidChars, MAX_PID_SIZE, "%d", getpid()) <= 0) { |
| 133 | PROF_WARN( |
| 134 | "Unable to parse filename pattern %s. Using the default name.", |
| 135 | FilenamePat); |
Vedant Kumar | a06e8ca | 2016-01-29 23:52:11 +0000 | [diff] [blame] | 136 | return -1; |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 137 | } |
Vedant Kumar | a06e8ca | 2016-01-29 23:52:11 +0000 | [diff] [blame] | 138 | } |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 139 | } else if (FilenamePat[I] == 'h') { |
Vedant Kumar | a06e8ca | 2016-01-29 23:52:11 +0000 | [diff] [blame] | 140 | if (!NumHosts++) |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 141 | if (COMPILER_RT_GETHOSTNAME(Hostname, COMPILER_RT_MAX_HOSTLEN)) { |
| 142 | PROF_WARN( |
| 143 | "Unable to parse filename pattern %s. Using the default name.", |
| 144 | FilenamePat); |
Vedant Kumar | a06e8ca | 2016-01-29 23:52:11 +0000 | [diff] [blame] | 145 | return -1; |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 146 | } |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 147 | } |
Vedant Kumar | a06e8ca | 2016-01-29 23:52:11 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Xinliang David Li | 7f08d12 | 2016-05-25 17:30:15 +0000 | [diff] [blame] | 150 | lprofCurFilename.NumPids = NumPids; |
| 151 | lprofCurFilename.NumHosts = NumHosts; |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 152 | return 0; |
| 153 | } |
| 154 | |
Xinliang David Li | 7f08d12 | 2016-05-25 17:30:15 +0000 | [diff] [blame] | 155 | static void parseAndSetFilename(const char *FilenamePat) { |
| 156 | int NewFile; |
| 157 | const char *OldFilenamePat = lprofCurFilename.FilenamePat; |
| 158 | |
| 159 | if (!FilenamePat || parseFilenamePattern(FilenamePat)) |
| 160 | resetFilenameToDefault(); |
| 161 | |
| 162 | NewFile = |
| 163 | !OldFilenamePat || (strcmp(OldFilenamePat, lprofCurFilename.FilenamePat)); |
| 164 | |
| 165 | if (NewFile) |
| 166 | truncateCurrentFile(); |
| 167 | } |
| 168 | |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 169 | /* Return buffer length that is required to store the current profile |
| 170 | * filename with PID and hostname substitutions. */ |
| 171 | static int getCurFilenameLength() { |
| 172 | if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0]) |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 173 | return 0; |
Duncan P. N. Exon Smith | e5edc88 | 2014-03-21 00:27:48 +0000 | [diff] [blame] | 174 | |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 175 | if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts)) |
| 176 | return strlen(lprofCurFilename.FilenamePat); |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 177 | |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 178 | return strlen(lprofCurFilename.FilenamePat) + |
| 179 | lprofCurFilename.NumPids * (strlen(lprofCurFilename.PidChars) - 2) + |
| 180 | lprofCurFilename.NumHosts * (strlen(lprofCurFilename.Hostname) - 2); |
| 181 | } |
| 182 | |
| 183 | /* Return the pointer to the current profile file name (after substituting |
| 184 | * PIDs and Hostnames in filename pattern. \p FilenameBuf is the buffer |
| 185 | * to store the resulting filename. If no substitution is needed, the |
| 186 | * current filename pattern string is directly returned. */ |
| 187 | static const char *getCurFilename(char *FilenameBuf) { |
| 188 | int I, J, PidLength, HostNameLength; |
| 189 | const char *FilenamePat = lprofCurFilename.FilenamePat; |
| 190 | |
| 191 | if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0]) |
| 192 | return 0; |
| 193 | |
| 194 | if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts)) |
| 195 | return lprofCurFilename.FilenamePat; |
| 196 | |
| 197 | PidLength = strlen(lprofCurFilename.PidChars); |
| 198 | HostNameLength = strlen(lprofCurFilename.Hostname); |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 199 | /* Construct the new filename. */ |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 200 | for (I = 0, J = 0; FilenamePat[I]; ++I) |
| 201 | if (FilenamePat[I] == '%') { |
| 202 | if (FilenamePat[++I] == 'p') { |
| 203 | memcpy(FilenameBuf + J, lprofCurFilename.PidChars, PidLength); |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 204 | J += PidLength; |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 205 | } else if (FilenamePat[I] == 'h') { |
| 206 | memcpy(FilenameBuf + J, lprofCurFilename.Hostname, HostNameLength); |
Vedant Kumar | a06e8ca | 2016-01-29 23:52:11 +0000 | [diff] [blame] | 207 | J += HostNameLength; |
| 208 | } |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 209 | /* Drop any unknown substitutions. */ |
| 210 | } else |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 211 | FilenameBuf[J++] = FilenamePat[I]; |
| 212 | FilenameBuf[J] = 0; |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 213 | |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 214 | return FilenameBuf; |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 217 | /* Returns the pointer to the environment variable |
| 218 | * string. Returns null if the env var is not set. */ |
| 219 | static const char *getFilenamePatFromEnv(void) { |
Eric Christopher | d641b53 | 2015-04-28 22:54:51 +0000 | [diff] [blame] | 220 | const char *Filename = getenv("LLVM_PROFILE_FILE"); |
Eric Christopher | d641b53 | 2015-04-28 22:54:51 +0000 | [diff] [blame] | 221 | if (!Filename || !Filename[0]) |
Xinliang David Li | 51fe002 | 2016-05-24 01:23:09 +0000 | [diff] [blame] | 222 | return 0; |
| 223 | return Filename; |
Eric Christopher | d641b53 | 2015-04-28 22:54:51 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Xinliang David Li | 51fe002 | 2016-05-24 01:23:09 +0000 | [diff] [blame] | 226 | /* This method is invoked by the runtime initialization hook |
| 227 | * InstrProfilingRuntime.o if it is linked in. Both user specified |
| 228 | * profile path via -fprofile-instr-generate= and LLVM_PROFILE_FILE |
| 229 | * environment variable can override this default value. */ |
Xinliang David Li | abfd553 | 2015-12-16 03:29:15 +0000 | [diff] [blame] | 230 | COMPILER_RT_VISIBILITY |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 231 | void __llvm_profile_initialize_file(void) { |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 232 | const char *FilenamePat; |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 233 | /* Check if the filename has been initialized. */ |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 234 | if (lprofCurFilename.FilenamePat) |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 235 | return; |
| 236 | |
| 237 | /* Detect the filename and truncate. */ |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 238 | FilenamePat = getFilenamePatFromEnv(); |
Xinliang David Li | 7f08d12 | 2016-05-25 17:30:15 +0000 | [diff] [blame] | 239 | parseAndSetFilename(FilenamePat); |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Xinliang David Li | 51fe002 | 2016-05-24 01:23:09 +0000 | [diff] [blame] | 242 | /* This API is directly called by the user application code. It has the |
| 243 | * highest precedence compared with LLVM_PROFILE_FILE environment variable |
Xinliang David Li | 4151894 | 2016-05-24 02:37:07 +0000 | [diff] [blame] | 244 | * and command line option -fprofile-instr-generate=<profile_name>. |
Xinliang David Li | 51fe002 | 2016-05-24 01:23:09 +0000 | [diff] [blame] | 245 | */ |
Xinliang David Li | abfd553 | 2015-12-16 03:29:15 +0000 | [diff] [blame] | 246 | COMPILER_RT_VISIBILITY |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 247 | void __llvm_profile_set_filename(const char *FilenamePat) { |
Xinliang David Li | 7f08d12 | 2016-05-25 17:30:15 +0000 | [diff] [blame] | 248 | parseAndSetFilename(FilenamePat); |
Eric Christopher | d641b53 | 2015-04-28 22:54:51 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 251 | /* |
Xinliang David Li | 51fe002 | 2016-05-24 01:23:09 +0000 | [diff] [blame] | 252 | * This API is invoked by the global initializers emitted by Clang/LLVM when |
| 253 | * -fprofile-instr-generate=<..> is specified (vs -fprofile-instr-generate |
| 254 | * without an argument). This option has lower precedence than the |
| 255 | * LLVM_PROFILE_FILE environment variable. |
| 256 | */ |
Xinliang David Li | abfd553 | 2015-12-16 03:29:15 +0000 | [diff] [blame] | 257 | COMPILER_RT_VISIBILITY |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 258 | void __llvm_profile_override_default_filename(const char *FilenamePat) { |
Eric Christopher | d641b53 | 2015-04-28 22:54:51 +0000 | [diff] [blame] | 259 | /* If the env var is set, skip setting filename from argument. */ |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 260 | const char *Env_Filename = getFilenamePatFromEnv(); |
Xinliang David Li | 51fe002 | 2016-05-24 01:23:09 +0000 | [diff] [blame] | 261 | if (Env_Filename) |
Eric Christopher | d641b53 | 2015-04-28 22:54:51 +0000 | [diff] [blame] | 262 | return; |
Xinliang David Li | 7f08d12 | 2016-05-25 17:30:15 +0000 | [diff] [blame] | 263 | |
| 264 | parseAndSetFilename(FilenamePat); |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 267 | /* The public API for writing profile data into the file with name |
| 268 | * set by previous calls to __llvm_profile_set_filename or |
| 269 | * __llvm_profile_override_default_filename or |
| 270 | * __llvm_profile_initialize_file. */ |
Xinliang David Li | abfd553 | 2015-12-16 03:29:15 +0000 | [diff] [blame] | 271 | COMPILER_RT_VISIBILITY |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 272 | int __llvm_profile_write_file(void) { |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 273 | int rc, Length; |
| 274 | const char *Filename; |
| 275 | char *FilenameBuf; |
Vasileios Kalintiris | c9c7a3e | 2015-02-25 13:50:18 +0000 | [diff] [blame] | 276 | |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 277 | Length = getCurFilenameLength(); |
| 278 | FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1); |
| 279 | Filename = getCurFilename(FilenameBuf); |
| 280 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 281 | /* Check the filename. */ |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 282 | if (!Filename) { |
Xinliang David Li | 690c31f | 2016-05-20 05:15:42 +0000 | [diff] [blame] | 283 | PROF_ERR("Failed to write file : %s\n", "Filename not set"); |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 284 | return -1; |
Xinliang David Li | 9ea3064 | 2015-12-03 18:31:59 +0000 | [diff] [blame] | 285 | } |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 286 | |
Xinliang David Li | d85c32c | 2016-01-08 23:42:28 +0000 | [diff] [blame] | 287 | /* Check if there is llvm/runtime version mismatch. */ |
Xinliang David Li | a692421 | 2016-01-08 23:31:57 +0000 | [diff] [blame] | 288 | if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) { |
Xinliang David Li | 690c31f | 2016-05-20 05:15:42 +0000 | [diff] [blame] | 289 | PROF_ERR("Runtime and instrumentation version mismatch : " |
Xinliang David Li | a692421 | 2016-01-08 23:31:57 +0000 | [diff] [blame] | 290 | "expected %d, but get %d\n", |
| 291 | INSTR_PROF_RAW_VERSION, |
| 292 | (int)GET_VERSION(__llvm_profile_get_version())); |
| 293 | return -1; |
| 294 | } |
| 295 | |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 296 | /* Write profile data to the file. */ |
| 297 | rc = writeFile(Filename); |
Xinliang David Li | 9ea3064 | 2015-12-03 18:31:59 +0000 | [diff] [blame] | 298 | if (rc) |
Xinliang David Li | 315e49d | 2016-05-24 21:29:18 +0000 | [diff] [blame] | 299 | PROF_ERR("Failed to write file \"%s\": %s\n", Filename, strerror(errno)); |
Justin Bogner | 66fd5c9 | 2015-01-16 20:10:56 +0000 | [diff] [blame] | 300 | return rc; |
Duncan P. N. Exon Smith | 54cc0e2 | 2014-03-20 19:23:55 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Xinliang David Li | 6fe18f4 | 2015-11-23 04:38:17 +0000 | [diff] [blame] | 303 | static void writeFileWithoutReturn(void) { __llvm_profile_write_file(); } |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 304 | |
Xinliang David Li | abfd553 | 2015-12-16 03:29:15 +0000 | [diff] [blame] | 305 | COMPILER_RT_VISIBILITY |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 306 | int __llvm_profile_register_write_file_atexit(void) { |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 307 | static int HasBeenRegistered = 0; |
| 308 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 309 | if (HasBeenRegistered) |
| 310 | return 0; |
| 311 | |
Xinliang David Li | 4617aa7 | 2016-05-18 22:34:05 +0000 | [diff] [blame] | 312 | lprofSetupValueProfiler(); |
| 313 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 314 | HasBeenRegistered = 1; |
| 315 | return atexit(writeFileWithoutReturn); |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 316 | } |