File: | compiler-rt/lib/profile/InstrProfilingFile.c |
Warning: | line 882, column 9 Null pointer passed to 2nd parameter expecting 'nonnull' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /*===- InstrProfilingFile.c - Write instrumentation to a file -------------===*\ | ||||
2 | |* | ||||
3 | |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||
4 | |* See https://llvm.org/LICENSE.txt for license information. | ||||
5 | |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||
6 | |* | ||||
7 | \*===----------------------------------------------------------------------===*/ | ||||
8 | |||||
9 | #if !defined(__Fuchsia__) | ||||
10 | |||||
11 | #include <assert.h> | ||||
12 | #include <errno(*__errno_location ()).h> | ||||
13 | #include <stdio.h> | ||||
14 | #include <stdlib.h> | ||||
15 | #include <string.h> | ||||
16 | #ifdef _MSC_VER | ||||
17 | /* For _alloca. */ | ||||
18 | #include <malloc.h> | ||||
19 | #endif | ||||
20 | #if defined(_WIN32) | ||||
21 | #include "WindowsMMap.h" | ||||
22 | /* For _chsize_s */ | ||||
23 | #include <io.h> | ||||
24 | #include <process.h> | ||||
25 | #else | ||||
26 | #include <sys/file.h> | ||||
27 | #include <sys/mman.h> | ||||
28 | #include <unistd.h> | ||||
29 | #if defined(__linux__1) | ||||
30 | #include <sys/types.h> | ||||
31 | #endif | ||||
32 | #endif | ||||
33 | |||||
34 | #include "InstrProfiling.h" | ||||
35 | #include "InstrProfilingInternal.h" | ||||
36 | #include "InstrProfilingPort.h" | ||||
37 | #include "InstrProfilingUtil.h" | ||||
38 | |||||
39 | /* From where is profile name specified. | ||||
40 | * The order the enumerators define their | ||||
41 | * precedence. Re-order them may lead to | ||||
42 | * runtime behavior change. */ | ||||
43 | typedef enum ProfileNameSpecifier { | ||||
44 | PNS_unknown = 0, | ||||
45 | PNS_default, | ||||
46 | PNS_command_line, | ||||
47 | PNS_environment, | ||||
48 | PNS_runtime_api | ||||
49 | } ProfileNameSpecifier; | ||||
50 | |||||
51 | static const char *getPNSStr(ProfileNameSpecifier PNS) { | ||||
52 | switch (PNS) { | ||||
53 | case PNS_default: | ||||
54 | return "default setting"; | ||||
55 | case PNS_command_line: | ||||
56 | return "command line"; | ||||
57 | case PNS_environment: | ||||
58 | return "environment variable"; | ||||
59 | case PNS_runtime_api: | ||||
60 | return "runtime API"; | ||||
61 | default: | ||||
62 | return "Unknown"; | ||||
63 | } | ||||
64 | } | ||||
65 | |||||
66 | #define MAX_PID_SIZE16 16 | ||||
67 | /* Data structure holding the result of parsed filename pattern. */ | ||||
68 | typedef struct lprofFilename { | ||||
69 | /* File name string possibly with %p or %h specifiers. */ | ||||
70 | const char *FilenamePat; | ||||
71 | /* A flag indicating if FilenamePat's memory is allocated | ||||
72 | * by runtime. */ | ||||
73 | unsigned OwnsFilenamePat; | ||||
74 | const char *ProfilePathPrefix; | ||||
75 | char PidChars[MAX_PID_SIZE16]; | ||||
76 | char *TmpDir; | ||||
77 | char Hostname[COMPILER_RT_MAX_HOSTLEN128]; | ||||
78 | unsigned NumPids; | ||||
79 | unsigned NumHosts; | ||||
80 | /* When in-process merging is enabled, this parameter specifies | ||||
81 | * the total number of profile data files shared by all the processes | ||||
82 | * spawned from the same binary. By default the value is 1. If merging | ||||
83 | * is not enabled, its value should be 0. This parameter is specified | ||||
84 | * by the %[0-9]m specifier. For instance %2m enables merging using | ||||
85 | * 2 profile data files. %1m is equivalent to %m. Also %m specifier | ||||
86 | * can only appear once at the end of the name pattern. */ | ||||
87 | unsigned MergePoolSize; | ||||
88 | ProfileNameSpecifier PNS; | ||||
89 | } lprofFilename; | ||||
90 | |||||
91 | static lprofFilename lprofCurFilename = {0, 0, 0, {0}, NULL((void*)0), | ||||
92 | {0}, 0, 0, 0, PNS_unknown}; | ||||
93 | |||||
94 | static int ProfileMergeRequested = 0; | ||||
95 | static int getProfileFileSizeForMerging(FILE *ProfileFile, | ||||
96 | uint64_t *ProfileFileSize); | ||||
97 | |||||
98 | #if defined(__APPLE__) | ||||
99 | static const int ContinuousModeSupported = 1; | ||||
100 | static const int UseBiasVar = 0; | ||||
101 | static const char *FileOpenMode = "a+b"; | ||||
102 | static void *BiasAddr = NULL((void*)0); | ||||
103 | static void *BiasDefaultAddr = NULL((void*)0); | ||||
104 | static int mmapForContinuousMode(uint64_t CurrentFileOffset, FILE *File) { | ||||
105 | /* Get the sizes of various profile data sections. Taken from | ||||
106 | * __llvm_profile_get_size_for_buffer(). */ | ||||
107 | const __llvm_profile_data *DataBegin = __llvm_profile_begin_data(); | ||||
108 | const __llvm_profile_data *DataEnd = __llvm_profile_end_data(); | ||||
109 | const uint64_t *CountersBegin = __llvm_profile_begin_counters(); | ||||
110 | const uint64_t *CountersEnd = __llvm_profile_end_counters(); | ||||
111 | const char *NamesBegin = __llvm_profile_begin_names(); | ||||
112 | const char *NamesEnd = __llvm_profile_end_names(); | ||||
113 | const uint64_t NamesSize = (NamesEnd - NamesBegin) * sizeof(char); | ||||
114 | uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd); | ||||
115 | uint64_t CountersSize = CountersEnd - CountersBegin; | ||||
116 | |||||
117 | /* Check that the counter and data sections in this image are | ||||
118 | * page-aligned. */ | ||||
119 | unsigned PageSize = getpagesize(); | ||||
120 | if ((intptr_t)CountersBegin % PageSize != 0) { | ||||
121 | PROF_ERR("Counters section not page-aligned (start = %p, pagesz = %u).\n",fprintf(stderr, "LLVM Profile Error: " "Counters section not page-aligned (start = %p, pagesz = %u).\n" , CountersBegin, PageSize); | ||||
122 | CountersBegin, PageSize)fprintf(stderr, "LLVM Profile Error: " "Counters section not page-aligned (start = %p, pagesz = %u).\n" , CountersBegin, PageSize);; | ||||
123 | return 1; | ||||
124 | } | ||||
125 | if ((intptr_t)DataBegin % PageSize != 0) { | ||||
126 | PROF_ERR("Data section not page-aligned (start = %p, pagesz = %u).\n",fprintf(stderr, "LLVM Profile Error: " "Data section not page-aligned (start = %p, pagesz = %u).\n" , DataBegin, PageSize); | ||||
127 | DataBegin, PageSize)fprintf(stderr, "LLVM Profile Error: " "Data section not page-aligned (start = %p, pagesz = %u).\n" , DataBegin, PageSize);; | ||||
128 | return 1; | ||||
129 | } | ||||
130 | int Fileno = fileno(File); | ||||
131 | /* Determine how much padding is needed before/after the counters and | ||||
132 | * after the names. */ | ||||
133 | uint64_t PaddingBytesBeforeCounters, PaddingBytesAfterCounters, | ||||
134 | PaddingBytesAfterNames; | ||||
135 | __llvm_profile_get_padding_sizes_for_counters( | ||||
136 | DataSize, CountersSize, NamesSize, &PaddingBytesBeforeCounters, | ||||
137 | &PaddingBytesAfterCounters, &PaddingBytesAfterNames); | ||||
138 | |||||
139 | uint64_t PageAlignedCountersLength = | ||||
140 | (CountersSize * sizeof(uint64_t)) + PaddingBytesAfterCounters; | ||||
141 | uint64_t FileOffsetToCounters = | ||||
142 | CurrentFileOffset + sizeof(__llvm_profile_header) + | ||||
143 | (DataSize * sizeof(__llvm_profile_data)) + PaddingBytesBeforeCounters; | ||||
144 | uint64_t *CounterMmap = (uint64_t *)mmap( | ||||
145 | (void *)CountersBegin, PageAlignedCountersLength, PROT_READ0x1 | PROT_WRITE0x2, | ||||
146 | MAP_FIXED0x10 | MAP_SHARED0x01, Fileno, FileOffsetToCounters); | ||||
147 | if (CounterMmap != CountersBegin) { | ||||
148 | PROF_ERR(fprintf(stderr, "LLVM Profile Error: " "Continuous counter sync mode is enabled, but mmap() failed (%s).\n" " - CountersBegin: %p\n" " - PageAlignedCountersLength: %" "ll" "u" "\n" " - Fileno: %d\n" " - FileOffsetToCounters: %" "ll" "u" "\n", strerror((*__errno_location ())), CountersBegin , PageAlignedCountersLength, Fileno, FileOffsetToCounters); | ||||
149 | "Continuous counter sync mode is enabled, but mmap() failed (%s).\n"fprintf(stderr, "LLVM Profile Error: " "Continuous counter sync mode is enabled, but mmap() failed (%s).\n" " - CountersBegin: %p\n" " - PageAlignedCountersLength: %" "ll" "u" "\n" " - Fileno: %d\n" " - FileOffsetToCounters: %" "ll" "u" "\n", strerror((*__errno_location ())), CountersBegin , PageAlignedCountersLength, Fileno, FileOffsetToCounters); | ||||
150 | " - CountersBegin: %p\n"fprintf(stderr, "LLVM Profile Error: " "Continuous counter sync mode is enabled, but mmap() failed (%s).\n" " - CountersBegin: %p\n" " - PageAlignedCountersLength: %" "ll" "u" "\n" " - Fileno: %d\n" " - FileOffsetToCounters: %" "ll" "u" "\n", strerror((*__errno_location ())), CountersBegin , PageAlignedCountersLength, Fileno, FileOffsetToCounters); | ||||
151 | " - PageAlignedCountersLength: %" PRIu64 "\n"fprintf(stderr, "LLVM Profile Error: " "Continuous counter sync mode is enabled, but mmap() failed (%s).\n" " - CountersBegin: %p\n" " - PageAlignedCountersLength: %" "ll" "u" "\n" " - Fileno: %d\n" " - FileOffsetToCounters: %" "ll" "u" "\n", strerror((*__errno_location ())), CountersBegin , PageAlignedCountersLength, Fileno, FileOffsetToCounters); | ||||
152 | " - Fileno: %d\n"fprintf(stderr, "LLVM Profile Error: " "Continuous counter sync mode is enabled, but mmap() failed (%s).\n" " - CountersBegin: %p\n" " - PageAlignedCountersLength: %" "ll" "u" "\n" " - Fileno: %d\n" " - FileOffsetToCounters: %" "ll" "u" "\n", strerror((*__errno_location ())), CountersBegin , PageAlignedCountersLength, Fileno, FileOffsetToCounters); | ||||
153 | " - FileOffsetToCounters: %" PRIu64 "\n",fprintf(stderr, "LLVM Profile Error: " "Continuous counter sync mode is enabled, but mmap() failed (%s).\n" " - CountersBegin: %p\n" " - PageAlignedCountersLength: %" "ll" "u" "\n" " - Fileno: %d\n" " - FileOffsetToCounters: %" "ll" "u" "\n", strerror((*__errno_location ())), CountersBegin , PageAlignedCountersLength, Fileno, FileOffsetToCounters); | ||||
154 | strerror(errno), CountersBegin, PageAlignedCountersLength, Fileno,fprintf(stderr, "LLVM Profile Error: " "Continuous counter sync mode is enabled, but mmap() failed (%s).\n" " - CountersBegin: %p\n" " - PageAlignedCountersLength: %" "ll" "u" "\n" " - Fileno: %d\n" " - FileOffsetToCounters: %" "ll" "u" "\n", strerror((*__errno_location ())), CountersBegin , PageAlignedCountersLength, Fileno, FileOffsetToCounters); | ||||
155 | FileOffsetToCounters)fprintf(stderr, "LLVM Profile Error: " "Continuous counter sync mode is enabled, but mmap() failed (%s).\n" " - CountersBegin: %p\n" " - PageAlignedCountersLength: %" "ll" "u" "\n" " - Fileno: %d\n" " - FileOffsetToCounters: %" "ll" "u" "\n", strerror((*__errno_location ())), CountersBegin , PageAlignedCountersLength, Fileno, FileOffsetToCounters);; | ||||
156 | return 1; | ||||
157 | } | ||||
158 | return 0; | ||||
159 | } | ||||
160 | #elif defined(__ELF__1) || defined(_WIN32) | ||||
161 | |||||
162 | #define INSTR_PROF_PROFILE_COUNTER_BIAS_DEFAULT_VAR__llvm_profile_counter_bias_default \ | ||||
163 | INSTR_PROF_CONCAT(INSTR_PROF_PROFILE_COUNTER_BIAS_VAR, _default)__llvm_profile_counter_bias_default | ||||
164 | intptr_t INSTR_PROF_PROFILE_COUNTER_BIAS_DEFAULT_VAR__llvm_profile_counter_bias_default = 0; | ||||
165 | |||||
166 | /* This variable is a weak external reference which could be used to detect | ||||
167 | * whether or not the compiler defined this symbol. */ | ||||
168 | #if defined(_MSC_VER) | ||||
169 | COMPILER_RT_VISIBILITY__attribute__((visibility("hidden"))) extern intptr_t INSTR_PROF_PROFILE_COUNTER_BIAS_VAR__llvm_profile_counter_bias; | ||||
170 | #if defined(_M_IX86) || defined(__i386__1) | ||||
171 | #define WIN_SYM_PREFIX "_" | ||||
172 | #else | ||||
173 | #define WIN_SYM_PREFIX | ||||
174 | #endif | ||||
175 | #pragma comment( \ | ||||
176 | linker, "/alternatename:" WIN_SYM_PREFIX INSTR_PROF_QUOTE( \"__llvm_profile_counter_bias" | ||||
177 | INSTR_PROF_PROFILE_COUNTER_BIAS_VAR)"__llvm_profile_counter_bias" "=" WIN_SYM_PREFIX \ | ||||
178 | INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_COUNTER_BIAS_DEFAULT_VAR)"__llvm_profile_counter_bias_default") | ||||
179 | #else | ||||
180 | COMPILER_RT_VISIBILITY__attribute__((visibility("hidden"))) extern intptr_t INSTR_PROF_PROFILE_COUNTER_BIAS_VAR__llvm_profile_counter_bias | ||||
181 | __attribute__((weak, alias(INSTR_PROF_QUOTE("__llvm_profile_counter_bias_default" | ||||
182 | INSTR_PROF_PROFILE_COUNTER_BIAS_DEFAULT_VAR)"__llvm_profile_counter_bias_default"))); | ||||
183 | #endif | ||||
184 | static const int ContinuousModeSupported = 1; | ||||
185 | static const int UseBiasVar = 1; | ||||
186 | /* TODO: If there are two DSOs, the second DSO initilization will truncate the | ||||
187 | * first profile file. */ | ||||
188 | static const char *FileOpenMode = "w+b"; | ||||
189 | /* This symbol is defined by the compiler when runtime counter relocation is | ||||
190 | * used and runtime provides a weak alias so we can check if it's defined. */ | ||||
191 | static void *BiasAddr = &INSTR_PROF_PROFILE_COUNTER_BIAS_VAR__llvm_profile_counter_bias; | ||||
192 | static void *BiasDefaultAddr = &INSTR_PROF_PROFILE_COUNTER_BIAS_DEFAULT_VAR__llvm_profile_counter_bias_default; | ||||
193 | static int mmapForContinuousMode(uint64_t CurrentFileOffset, FILE *File) { | ||||
194 | /* Get the sizes of various profile data sections. Taken from | ||||
195 | * __llvm_profile_get_size_for_buffer(). */ | ||||
196 | const __llvm_profile_data *DataBegin = __llvm_profile_begin_data(); | ||||
197 | const __llvm_profile_data *DataEnd = __llvm_profile_end_data(); | ||||
198 | const uint64_t *CountersBegin = __llvm_profile_begin_counters(); | ||||
199 | const uint64_t *CountersEnd = __llvm_profile_end_counters(); | ||||
200 | uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd); | ||||
201 | /* Get the file size. */ | ||||
202 | uint64_t FileSize = 0; | ||||
203 | if (getProfileFileSizeForMerging(File, &FileSize)) | ||||
204 | return 1; | ||||
205 | |||||
206 | /* Map the profile. */ | ||||
207 | char *Profile = (char *)mmap(NULL((void*)0), FileSize, PROT_READ0x1 | PROT_WRITE0x2, | ||||
208 | MAP_SHARED0x01, fileno(File), 0); | ||||
209 | if (Profile == MAP_FAILED((void *) -1)) { | ||||
210 | PROF_ERR("Unable to mmap profile: %s\n", strerror(errno))fprintf(stderr, "LLVM Profile Error: " "Unable to mmap profile: %s\n" , strerror((*__errno_location ())));; | ||||
211 | return 1; | ||||
212 | } | ||||
213 | const uint64_t CountersOffsetInBiasMode = | ||||
214 | sizeof(__llvm_profile_header) + __llvm_write_binary_ids(NULL((void*)0)) + | ||||
215 | (DataSize * sizeof(__llvm_profile_data)); | ||||
216 | /* Update the profile fields based on the current mapping. */ | ||||
217 | INSTR_PROF_PROFILE_COUNTER_BIAS_VAR__llvm_profile_counter_bias = | ||||
218 | (intptr_t)Profile - (uintptr_t)CountersBegin + CountersOffsetInBiasMode; | ||||
219 | |||||
220 | /* Return the memory allocated for counters to OS. */ | ||||
221 | lprofReleaseMemoryPagesToOS((uintptr_t)CountersBegin, (uintptr_t)CountersEnd); | ||||
222 | return 0; | ||||
223 | } | ||||
224 | #else | ||||
225 | static const int ContinuousModeSupported = 0; | ||||
226 | static const int UseBiasVar = 0; | ||||
227 | static const char *FileOpenMode = "a+b"; | ||||
228 | static void *BiasAddr = NULL((void*)0); | ||||
229 | static void *BiasDefaultAddr = NULL((void*)0); | ||||
230 | static int mmapForContinuousMode(uint64_t CurrentFileOffset, FILE *File) { | ||||
231 | return 0; | ||||
232 | } | ||||
233 | #endif | ||||
234 | |||||
235 | static int isProfileMergeRequested() { return ProfileMergeRequested; } | ||||
236 | static void setProfileMergeRequested(int EnableMerge) { | ||||
237 | ProfileMergeRequested = EnableMerge; | ||||
238 | } | ||||
239 | |||||
240 | static FILE *ProfileFile = NULL((void*)0); | ||||
241 | static FILE *getProfileFile() { return ProfileFile; } | ||||
242 | static void setProfileFile(FILE *File) { ProfileFile = File; } | ||||
243 | |||||
244 | static int getCurFilenameLength(); | ||||
245 | static const char *getCurFilename(char *FilenameBuf, int ForceUseBuf); | ||||
246 | static unsigned doMerging() { | ||||
247 | return lprofCurFilename.MergePoolSize || isProfileMergeRequested(); | ||||
248 | } | ||||
249 | |||||
250 | /* Return 1 if there is an error, otherwise return 0. */ | ||||
251 | static uint32_t fileWriter(ProfDataWriter *This, ProfDataIOVec *IOVecs, | ||||
252 | uint32_t NumIOVecs) { | ||||
253 | uint32_t I; | ||||
254 | FILE *File = (FILE *)This->WriterCtx; | ||||
255 | char Zeroes[sizeof(uint64_t)] = {0}; | ||||
256 | for (I = 0; I < NumIOVecs; I++) { | ||||
257 | if (IOVecs[I].Data) { | ||||
258 | if (fwrite(IOVecs[I].Data, IOVecs[I].ElmSize, IOVecs[I].NumElm, File) != | ||||
259 | IOVecs[I].NumElm) | ||||
260 | return 1; | ||||
261 | } else if (IOVecs[I].UseZeroPadding) { | ||||
262 | size_t BytesToWrite = IOVecs[I].ElmSize * IOVecs[I].NumElm; | ||||
263 | while (BytesToWrite > 0) { | ||||
264 | size_t PartialWriteLen = | ||||
265 | (sizeof(uint64_t) > BytesToWrite) ? BytesToWrite : sizeof(uint64_t); | ||||
266 | if (fwrite(Zeroes, sizeof(uint8_t), PartialWriteLen, File) != | ||||
267 | PartialWriteLen) { | ||||
268 | return 1; | ||||
269 | } | ||||
270 | BytesToWrite -= PartialWriteLen; | ||||
271 | } | ||||
272 | } else { | ||||
273 | if (fseek(File, IOVecs[I].ElmSize * IOVecs[I].NumElm, SEEK_CUR1) == -1) | ||||
274 | return 1; | ||||
275 | } | ||||
276 | } | ||||
277 | return 0; | ||||
278 | } | ||||
279 | |||||
280 | /* TODO: make buffer size controllable by an internal option, and compiler can pass the size | ||||
281 | to runtime via a variable. */ | ||||
282 | static uint32_t orderFileWriter(FILE *File, const uint32_t *DataStart) { | ||||
283 | if (fwrite(DataStart, sizeof(uint32_t), INSTR_ORDER_FILE_BUFFER_SIZE131072, File) != | ||||
284 | INSTR_ORDER_FILE_BUFFER_SIZE131072) | ||||
285 | return 1; | ||||
286 | return 0; | ||||
287 | } | ||||
288 | |||||
289 | static void initFileWriter(ProfDataWriter *This, FILE *File) { | ||||
290 | This->Write = fileWriter; | ||||
291 | This->WriterCtx = File; | ||||
292 | } | ||||
293 | |||||
294 | COMPILER_RT_VISIBILITY__attribute__((visibility("hidden"))) ProfBufferIO * | ||||
295 | lprofCreateBufferIOInternal(void *File, uint32_t BufferSz) { | ||||
296 | FreeHook = &free; | ||||
297 | DynamicBufferIOBuffer = (uint8_t *)calloc(BufferSz, 1); | ||||
298 | VPBufferSize = BufferSz; | ||||
299 | ProfDataWriter *fileWriter = | ||||
300 | (ProfDataWriter *)calloc(sizeof(ProfDataWriter), 1); | ||||
301 | initFileWriter(fileWriter, File); | ||||
302 | ProfBufferIO *IO = lprofCreateBufferIO(fileWriter); | ||||
303 | IO->OwnFileWriter = 1; | ||||
304 | return IO; | ||||
305 | } | ||||
306 | |||||
307 | static void setupIOBuffer() { | ||||
308 | const char *BufferSzStr = 0; | ||||
309 | BufferSzStr = getenv("LLVM_VP_BUFFER_SIZE"); | ||||
310 | if (BufferSzStr && BufferSzStr[0]) { | ||||
311 | VPBufferSize = atoi(BufferSzStr); | ||||
312 | DynamicBufferIOBuffer = (uint8_t *)calloc(VPBufferSize, 1); | ||||
313 | } | ||||
314 | } | ||||
315 | |||||
316 | /* Get the size of the profile file. If there are any errors, print the | ||||
317 | * message under the assumption that the profile is being read for merging | ||||
318 | * purposes, and return -1. Otherwise return the file size in the inout param | ||||
319 | * \p ProfileFileSize. */ | ||||
320 | static int getProfileFileSizeForMerging(FILE *ProfileFile, | ||||
321 | uint64_t *ProfileFileSize) { | ||||
322 | if (fseek(ProfileFile, 0L, SEEK_END2) == -1) { | ||||
323 | PROF_ERR("Unable to merge profile data, unable to get size: %s\n",fprintf(stderr, "LLVM Profile Error: " "Unable to merge profile data, unable to get size: %s\n" , strerror((*__errno_location ()))); | ||||
324 | strerror(errno))fprintf(stderr, "LLVM Profile Error: " "Unable to merge profile data, unable to get size: %s\n" , strerror((*__errno_location ())));; | ||||
325 | return -1; | ||||
326 | } | ||||
327 | *ProfileFileSize = ftell(ProfileFile); | ||||
328 | |||||
329 | /* Restore file offset. */ | ||||
330 | if (fseek(ProfileFile, 0L, SEEK_SET0) == -1) { | ||||
331 | PROF_ERR("Unable to merge profile data, unable to rewind: %s\n",fprintf(stderr, "LLVM Profile Error: " "Unable to merge profile data, unable to rewind: %s\n" , strerror((*__errno_location ()))); | ||||
332 | strerror(errno))fprintf(stderr, "LLVM Profile Error: " "Unable to merge profile data, unable to rewind: %s\n" , strerror((*__errno_location ())));; | ||||
333 | return -1; | ||||
334 | } | ||||
335 | |||||
336 | if (*ProfileFileSize > 0 && | ||||
337 | *ProfileFileSize < sizeof(__llvm_profile_header)) { | ||||
338 | PROF_WARN("Unable to merge profile data: %s\n",fprintf(stderr, "LLVM Profile Warning: " "Unable to merge profile data: %s\n" , "source profile file is too small."); | ||||
339 | "source profile file is too small.")fprintf(stderr, "LLVM Profile Warning: " "Unable to merge profile data: %s\n" , "source profile file is too small.");; | ||||
340 | return -1; | ||||
341 | } | ||||
342 | return 0; | ||||
343 | } | ||||
344 | |||||
345 | /* mmap() \p ProfileFile for profile merging purposes, assuming that an | ||||
346 | * exclusive lock is held on the file and that \p ProfileFileSize is the | ||||
347 | * length of the file. Return the mmap'd buffer in the inout variable | ||||
348 | * \p ProfileBuffer. Returns -1 on failure. On success, the caller is | ||||
349 | * responsible for unmapping the mmap'd buffer in \p ProfileBuffer. */ | ||||
350 | static int mmapProfileForMerging(FILE *ProfileFile, uint64_t ProfileFileSize, | ||||
351 | char **ProfileBuffer) { | ||||
352 | *ProfileBuffer = mmap(NULL((void*)0), ProfileFileSize, PROT_READ0x1, MAP_SHARED0x01 | MAP_FILE0, | ||||
353 | fileno(ProfileFile), 0); | ||||
354 | if (*ProfileBuffer == MAP_FAILED((void *) -1)) { | ||||
355 | PROF_ERR("Unable to merge profile data, mmap failed: %s\n",fprintf(stderr, "LLVM Profile Error: " "Unable to merge profile data, mmap failed: %s\n" , strerror((*__errno_location ()))); | ||||
356 | strerror(errno))fprintf(stderr, "LLVM Profile Error: " "Unable to merge profile data, mmap failed: %s\n" , strerror((*__errno_location ())));; | ||||
357 | return -1; | ||||
358 | } | ||||
359 | |||||
360 | if (__llvm_profile_check_compatibility(*ProfileBuffer, ProfileFileSize)) { | ||||
361 | (void)munmap(*ProfileBuffer, ProfileFileSize); | ||||
362 | PROF_WARN("Unable to merge profile data: %s\n",fprintf(stderr, "LLVM Profile Warning: " "Unable to merge profile data: %s\n" , "source profile file is not compatible."); | ||||
363 | "source profile file is not compatible.")fprintf(stderr, "LLVM Profile Warning: " "Unable to merge profile data: %s\n" , "source profile file is not compatible.");; | ||||
364 | return -1; | ||||
365 | } | ||||
366 | return 0; | ||||
367 | } | ||||
368 | |||||
369 | /* Read profile data in \c ProfileFile and merge with in-memory | ||||
370 | profile counters. Returns -1 if there is fatal error, otheriwse | ||||
371 | 0 is returned. Returning 0 does not mean merge is actually | ||||
372 | performed. If merge is actually done, *MergeDone is set to 1. | ||||
373 | */ | ||||
374 | static int doProfileMerging(FILE *ProfileFile, int *MergeDone) { | ||||
375 | uint64_t ProfileFileSize; | ||||
376 | char *ProfileBuffer; | ||||
377 | |||||
378 | /* Get the size of the profile on disk. */ | ||||
379 | if (getProfileFileSizeForMerging(ProfileFile, &ProfileFileSize) == -1) | ||||
380 | return -1; | ||||
381 | |||||
382 | /* Nothing to merge. */ | ||||
383 | if (!ProfileFileSize) | ||||
384 | return 0; | ||||
385 | |||||
386 | /* mmap() the profile and check that it is compatible with the data in | ||||
387 | * the current image. */ | ||||
388 | if (mmapProfileForMerging(ProfileFile, ProfileFileSize, &ProfileBuffer) == -1) | ||||
389 | return -1; | ||||
390 | |||||
391 | /* Now start merging */ | ||||
392 | if (__llvm_profile_merge_from_buffer(ProfileBuffer, ProfileFileSize)) { | ||||
393 | PROF_ERR("%s\n", "Invalid profile data to merge")fprintf(stderr, "LLVM Profile Error: " "%s\n", "Invalid profile data to merge" );; | ||||
394 | (void)munmap(ProfileBuffer, ProfileFileSize); | ||||
395 | return -1; | ||||
396 | } | ||||
397 | |||||
398 | // Truncate the file in case merging of value profile did not happen to | ||||
399 | // prevent from leaving garbage data at the end of the profile file. | ||||
400 | (void)COMPILER_RT_FTRUNCATE(ProfileFile,ftruncate(fileno(ProfileFile), __llvm_profile_get_size_for_buffer ()) | ||||
401 | __llvm_profile_get_size_for_buffer())ftruncate(fileno(ProfileFile), __llvm_profile_get_size_for_buffer ()); | ||||
402 | |||||
403 | (void)munmap(ProfileBuffer, ProfileFileSize); | ||||
404 | *MergeDone = 1; | ||||
405 | |||||
406 | return 0; | ||||
407 | } | ||||
408 | |||||
409 | /* Create the directory holding the file, if needed. */ | ||||
410 | static void createProfileDir(const char *Filename) { | ||||
411 | size_t Length = strlen(Filename); | ||||
412 | if (lprofFindFirstDirSeparator(Filename)) { | ||||
413 | char *Copy = (char *)COMPILER_RT_ALLOCA__builtin_alloca(Length + 1); | ||||
414 | strncpy(Copy, Filename, Length + 1); | ||||
415 | __llvm_profile_recursive_mkdir(Copy); | ||||
416 | } | ||||
417 | } | ||||
418 | |||||
419 | /* Open the profile data for merging. It opens the file in r+b mode with | ||||
420 | * file locking. If the file has content which is compatible with the | ||||
421 | * current process, it also reads in the profile data in the file and merge | ||||
422 | * it with in-memory counters. After the profile data is merged in memory, | ||||
423 | * the original profile data is truncated and gets ready for the profile | ||||
424 | * dumper. With profile merging enabled, each executable as well as any of | ||||
425 | * its instrumented shared libraries dump profile data into their own data file. | ||||
426 | */ | ||||
427 | static FILE *openFileForMerging(const char *ProfileFileName, int *MergeDone) { | ||||
428 | FILE *ProfileFile = NULL((void*)0); | ||||
429 | int rc; | ||||
430 | |||||
431 | ProfileFile = getProfileFile(); | ||||
432 | if (ProfileFile) { | ||||
433 | lprofLockFileHandle(ProfileFile); | ||||
434 | } else { | ||||
435 | createProfileDir(ProfileFileName); | ||||
436 | ProfileFile = lprofOpenFileEx(ProfileFileName); | ||||
437 | } | ||||
438 | if (!ProfileFile) | ||||
439 | return NULL((void*)0); | ||||
440 | |||||
441 | rc = doProfileMerging(ProfileFile, MergeDone); | ||||
442 | if (rc || (!*MergeDone && COMPILER_RT_FTRUNCATE(ProfileFile, 0L)ftruncate(fileno(ProfileFile), 0L)) || | ||||
443 | fseek(ProfileFile, 0L, SEEK_SET0) == -1) { | ||||
444 | PROF_ERR("Profile Merging of file %s failed: %s\n", ProfileFileName,fprintf(stderr, "LLVM Profile Error: " "Profile Merging of file %s failed: %s\n" , ProfileFileName, strerror((*__errno_location ()))); | ||||
445 | strerror(errno))fprintf(stderr, "LLVM Profile Error: " "Profile Merging of file %s failed: %s\n" , ProfileFileName, strerror((*__errno_location ())));; | ||||
446 | fclose(ProfileFile); | ||||
447 | return NULL((void*)0); | ||||
448 | } | ||||
449 | return ProfileFile; | ||||
450 | } | ||||
451 | |||||
452 | static FILE *getFileObject(const char *OutputName) { | ||||
453 | FILE *File; | ||||
454 | File = getProfileFile(); | ||||
455 | if (File != NULL((void*)0)) { | ||||
456 | return File; | ||||
457 | } | ||||
458 | |||||
459 | return fopen(OutputName, "ab"); | ||||
460 | } | ||||
461 | |||||
462 | /* Write profile data to file \c OutputName. */ | ||||
463 | static int writeFile(const char *OutputName) { | ||||
464 | int RetVal; | ||||
465 | FILE *OutputFile; | ||||
466 | |||||
467 | int MergeDone = 0; | ||||
468 | VPMergeHook = &lprofMergeValueProfData; | ||||
469 | if (doMerging()) | ||||
470 | OutputFile = openFileForMerging(OutputName, &MergeDone); | ||||
471 | else | ||||
472 | OutputFile = getFileObject(OutputName); | ||||
473 | |||||
474 | if (!OutputFile) | ||||
475 | return -1; | ||||
476 | |||||
477 | FreeHook = &free; | ||||
478 | setupIOBuffer(); | ||||
479 | ProfDataWriter fileWriter; | ||||
480 | initFileWriter(&fileWriter, OutputFile); | ||||
481 | RetVal = lprofWriteData(&fileWriter, lprofGetVPDataReader(), MergeDone); | ||||
482 | |||||
483 | if (OutputFile == getProfileFile()) { | ||||
484 | fflush(OutputFile); | ||||
485 | if (doMerging()) { | ||||
486 | lprofUnlockFileHandle(OutputFile); | ||||
487 | } | ||||
488 | } else { | ||||
489 | fclose(OutputFile); | ||||
490 | } | ||||
491 | |||||
492 | return RetVal; | ||||
493 | } | ||||
494 | |||||
495 | /* Write order data to file \c OutputName. */ | ||||
496 | static int writeOrderFile(const char *OutputName) { | ||||
497 | int RetVal; | ||||
498 | FILE *OutputFile; | ||||
499 | |||||
500 | OutputFile = fopen(OutputName, "w"); | ||||
501 | |||||
502 | if (!OutputFile) { | ||||
503 | PROF_WARN("can't open file with mode ab: %s\n", OutputName)fprintf(stderr, "LLVM Profile Warning: " "can't open file with mode ab: %s\n" , OutputName);; | ||||
504 | return -1; | ||||
505 | } | ||||
506 | |||||
507 | FreeHook = &free; | ||||
508 | setupIOBuffer(); | ||||
509 | const uint32_t *DataBegin = __llvm_profile_begin_orderfile(); | ||||
510 | RetVal = orderFileWriter(OutputFile, DataBegin); | ||||
511 | |||||
512 | fclose(OutputFile); | ||||
513 | return RetVal; | ||||
514 | } | ||||
515 | |||||
516 | #define LPROF_INIT_ONCE_ENV"__LLVM_PROFILE_RT_INIT_ONCE" "__LLVM_PROFILE_RT_INIT_ONCE" | ||||
517 | |||||
518 | static void truncateCurrentFile(void) { | ||||
519 | const char *Filename; | ||||
520 | char *FilenameBuf; | ||||
521 | FILE *File; | ||||
522 | int Length; | ||||
523 | |||||
524 | Length = getCurFilenameLength(); | ||||
525 | FilenameBuf = (char *)COMPILER_RT_ALLOCA__builtin_alloca(Length + 1); | ||||
526 | Filename = getCurFilename(FilenameBuf, 0); | ||||
527 | if (!Filename) | ||||
528 | return; | ||||
529 | |||||
530 | /* Only create the profile directory and truncate an existing profile once. | ||||
531 | * In continuous mode, this is necessary, as the profile is written-to by the | ||||
532 | * runtime initializer. */ | ||||
533 | int initialized = getenv(LPROF_INIT_ONCE_ENV"__LLVM_PROFILE_RT_INIT_ONCE") != NULL((void*)0); | ||||
534 | if (initialized) | ||||
535 | return; | ||||
536 | #if defined(_WIN32) | ||||
537 | _putenv(LPROF_INIT_ONCE_ENV"__LLVM_PROFILE_RT_INIT_ONCE" "=" LPROF_INIT_ONCE_ENV"__LLVM_PROFILE_RT_INIT_ONCE"); | ||||
538 | #else | ||||
539 | setenv(LPROF_INIT_ONCE_ENV"__LLVM_PROFILE_RT_INIT_ONCE", LPROF_INIT_ONCE_ENV"__LLVM_PROFILE_RT_INIT_ONCE", 1); | ||||
540 | #endif | ||||
541 | |||||
542 | /* Create the profile dir (even if online merging is enabled), so that | ||||
543 | * the profile file can be set up if continuous mode is enabled. */ | ||||
544 | createProfileDir(Filename); | ||||
545 | |||||
546 | /* By pass file truncation to allow online raw profile merging. */ | ||||
547 | if (lprofCurFilename.MergePoolSize) | ||||
548 | return; | ||||
549 | |||||
550 | /* Truncate the file. Later we'll reopen and append. */ | ||||
551 | File = fopen(Filename, "w"); | ||||
552 | if (!File) | ||||
553 | return; | ||||
554 | fclose(File); | ||||
555 | } | ||||
556 | |||||
557 | /* Write a partial profile to \p Filename, which is required to be backed by | ||||
558 | * the open file object \p File. */ | ||||
559 | static int writeProfileWithFileObject(const char *Filename, FILE *File) { | ||||
560 | setProfileFile(File); | ||||
561 | int rc = writeFile(Filename); | ||||
562 | if (rc) | ||||
563 | PROF_ERR("Failed to write file \"%s\": %s\n", Filename, strerror(errno))fprintf(stderr, "LLVM Profile Error: " "Failed to write file \"%s\": %s\n" , Filename, strerror((*__errno_location ())));; | ||||
564 | setProfileFile(NULL((void*)0)); | ||||
565 | return rc; | ||||
566 | } | ||||
567 | |||||
568 | static void initializeProfileForContinuousMode(void) { | ||||
569 | if (!__llvm_profile_is_continuous_mode_enabled()) | ||||
570 | return; | ||||
571 | if (!ContinuousModeSupported) { | ||||
572 | PROF_ERR("%s\n", "continuous mode is unsupported on this platform")fprintf(stderr, "LLVM Profile Error: " "%s\n", "continuous mode is unsupported on this platform" );; | ||||
573 | return; | ||||
574 | } | ||||
575 | if (UseBiasVar && BiasAddr == BiasDefaultAddr) { | ||||
576 | PROF_ERR("%s\n", "__llvm_profile_counter_bias is undefined")fprintf(stderr, "LLVM Profile Error: " "%s\n", "__llvm_profile_counter_bias is undefined" );; | ||||
577 | return; | ||||
578 | } | ||||
579 | |||||
580 | /* Get the sizes of counter section. */ | ||||
581 | const uint64_t *CountersBegin = __llvm_profile_begin_counters(); | ||||
582 | const uint64_t *CountersEnd = __llvm_profile_end_counters(); | ||||
583 | uint64_t CountersSize = CountersEnd - CountersBegin; | ||||
584 | |||||
585 | int Length = getCurFilenameLength(); | ||||
586 | char *FilenameBuf = (char *)COMPILER_RT_ALLOCA__builtin_alloca(Length + 1); | ||||
587 | const char *Filename = getCurFilename(FilenameBuf, 0); | ||||
588 | if (!Filename) | ||||
589 | return; | ||||
590 | |||||
591 | FILE *File = NULL((void*)0); | ||||
592 | uint64_t CurrentFileOffset = 0; | ||||
593 | if (doMerging()) { | ||||
594 | /* We are merging profiles. Map the counter section as shared memory into | ||||
595 | * the profile, i.e. into each participating process. An increment in one | ||||
596 | * process should be visible to every other process with the same counter | ||||
597 | * section mapped. */ | ||||
598 | File = lprofOpenFileEx(Filename); | ||||
599 | if (!File) | ||||
600 | return; | ||||
601 | |||||
602 | uint64_t ProfileFileSize = 0; | ||||
603 | if (getProfileFileSizeForMerging(File, &ProfileFileSize) == -1) { | ||||
604 | lprofUnlockFileHandle(File); | ||||
605 | fclose(File); | ||||
606 | return; | ||||
607 | } | ||||
608 | if (ProfileFileSize == 0) { | ||||
609 | /* Grow the profile so that mmap() can succeed. Leak the file handle, as | ||||
610 | * the file should stay open. */ | ||||
611 | if (writeProfileWithFileObject(Filename, File) != 0) { | ||||
612 | lprofUnlockFileHandle(File); | ||||
613 | fclose(File); | ||||
614 | return; | ||||
615 | } | ||||
616 | } else { | ||||
617 | /* The merged profile has a non-zero length. Check that it is compatible | ||||
618 | * with the data in this process. */ | ||||
619 | char *ProfileBuffer; | ||||
620 | if (mmapProfileForMerging(File, ProfileFileSize, &ProfileBuffer) == -1) { | ||||
621 | lprofUnlockFileHandle(File); | ||||
622 | fclose(File); | ||||
623 | return; | ||||
624 | } | ||||
625 | (void)munmap(ProfileBuffer, ProfileFileSize); | ||||
626 | } | ||||
627 | } else { | ||||
628 | File = fopen(Filename, FileOpenMode); | ||||
629 | if (!File) | ||||
630 | return; | ||||
631 | /* Check that the offset within the file is page-aligned. */ | ||||
632 | CurrentFileOffset = ftell(File); | ||||
633 | unsigned PageSize = getpagesize(); | ||||
634 | if (CurrentFileOffset % PageSize != 0) { | ||||
635 | PROF_ERR("Continuous counter sync mode is enabled, but raw profile is not"fprintf(stderr, "LLVM Profile Error: " "Continuous counter sync mode is enabled, but raw profile is not" "page-aligned. CurrentFileOffset = %" "ll" "u" ", pagesz = %u.\n" , (uint64_t)CurrentFileOffset, PageSize); | ||||
636 | "page-aligned. CurrentFileOffset = %" PRIu64 ", pagesz = %u.\n",fprintf(stderr, "LLVM Profile Error: " "Continuous counter sync mode is enabled, but raw profile is not" "page-aligned. CurrentFileOffset = %" "ll" "u" ", pagesz = %u.\n" , (uint64_t)CurrentFileOffset, PageSize); | ||||
637 | (uint64_t)CurrentFileOffset, PageSize)fprintf(stderr, "LLVM Profile Error: " "Continuous counter sync mode is enabled, but raw profile is not" "page-aligned. CurrentFileOffset = %" "ll" "u" ", pagesz = %u.\n" , (uint64_t)CurrentFileOffset, PageSize);; | ||||
638 | return; | ||||
639 | } | ||||
640 | if (writeProfileWithFileObject(Filename, File) != 0) { | ||||
641 | fclose(File); | ||||
642 | return; | ||||
643 | } | ||||
644 | } | ||||
645 | |||||
646 | /* mmap() the profile counters so long as there is at least one counter. | ||||
647 | * If there aren't any counters, mmap() would fail with EINVAL. */ | ||||
648 | if (CountersSize > 0) | ||||
649 | mmapForContinuousMode(CurrentFileOffset, File); | ||||
650 | |||||
651 | if (doMerging()) { | ||||
652 | lprofUnlockFileHandle(File); | ||||
653 | fclose(File); | ||||
654 | } | ||||
655 | } | ||||
656 | |||||
657 | static const char *DefaultProfileName = "default.profraw"; | ||||
658 | static void resetFilenameToDefault(void) { | ||||
659 | if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) { | ||||
660 | free((void *)lprofCurFilename.FilenamePat); | ||||
661 | } | ||||
662 | memset(&lprofCurFilename, 0, sizeof(lprofCurFilename)); | ||||
663 | lprofCurFilename.FilenamePat = DefaultProfileName; | ||||
664 | lprofCurFilename.PNS = PNS_default; | ||||
665 | } | ||||
666 | |||||
667 | static unsigned getMergePoolSize(const char *FilenamePat, int *I) { | ||||
668 | unsigned J = 0, Num = 0; | ||||
669 | for (;; ++J) { | ||||
670 | char C = FilenamePat[*I + J]; | ||||
671 | if (C == 'm') { | ||||
672 | *I += J; | ||||
673 | return Num ? Num : 1; | ||||
674 | } | ||||
675 | if (C < '0' || C > '9') | ||||
676 | break; | ||||
677 | Num = Num * 10 + C - '0'; | ||||
678 | |||||
679 | /* If FilenamePat[*I+J] is between '0' and '9', the next byte is guaranteed | ||||
680 | * to be in-bound as the string is null terminated. */ | ||||
681 | } | ||||
682 | return 0; | ||||
683 | } | ||||
684 | |||||
685 | /* Assert that Idx does index past a string null terminator. Return the | ||||
686 | * result of the check. */ | ||||
687 | static int checkBounds(int Idx, int Strlen) { | ||||
688 | assert(Idx <= Strlen && "Indexing past string null terminator")((void) sizeof ((Idx <= Strlen && "Indexing past string null terminator" ) ? 1 : 0), __extension__ ({ if (Idx <= Strlen && "Indexing past string null terminator" ) ; else __assert_fail ("Idx <= Strlen && \"Indexing past string null terminator\"" , "/build/llvm-toolchain-snapshot-14~++20211110111138+cffbfd01e37b/compiler-rt/lib/profile/InstrProfilingFile.c" , 688, __extension__ __PRETTY_FUNCTION__); })); | ||||
689 | return Idx <= Strlen; | ||||
690 | } | ||||
691 | |||||
692 | /* Parses the pattern string \p FilenamePat and stores the result to | ||||
693 | * lprofcurFilename structure. */ | ||||
694 | static int parseFilenamePattern(const char *FilenamePat, | ||||
695 | unsigned CopyFilenamePat) { | ||||
696 | int NumPids = 0, NumHosts = 0, I; | ||||
697 | char *PidChars = &lprofCurFilename.PidChars[0]; | ||||
698 | char *Hostname = &lprofCurFilename.Hostname[0]; | ||||
699 | int MergingEnabled = 0; | ||||
700 | int FilenamePatLen = strlen(FilenamePat); | ||||
701 | |||||
702 | /* Clean up cached prefix and filename. */ | ||||
703 | if (lprofCurFilename.ProfilePathPrefix) | ||||
704 | free((void *)lprofCurFilename.ProfilePathPrefix); | ||||
705 | |||||
706 | if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) { | ||||
707 | free((void *)lprofCurFilename.FilenamePat); | ||||
708 | } | ||||
709 | |||||
710 | memset(&lprofCurFilename, 0, sizeof(lprofCurFilename)); | ||||
711 | |||||
712 | if (!CopyFilenamePat) | ||||
713 | lprofCurFilename.FilenamePat = FilenamePat; | ||||
714 | else { | ||||
715 | lprofCurFilename.FilenamePat = strdup(FilenamePat); | ||||
716 | lprofCurFilename.OwnsFilenamePat = 1; | ||||
717 | } | ||||
718 | /* Check the filename for "%p", which indicates a pid-substitution. */ | ||||
719 | for (I = 0; checkBounds(I, FilenamePatLen) && FilenamePat[I]; ++I) { | ||||
720 | if (FilenamePat[I] == '%') { | ||||
721 | ++I; /* Advance to the next character. */ | ||||
722 | if (!checkBounds(I, FilenamePatLen)) | ||||
723 | break; | ||||
724 | if (FilenamePat[I] == 'p') { | ||||
725 | if (!NumPids++) { | ||||
726 | if (snprintf(PidChars, MAX_PID_SIZE16, "%ld", (long)getpid()) <= 0) { | ||||
727 | PROF_WARN("Unable to get pid for filename pattern %s. Using the "fprintf(stderr, "LLVM Profile Warning: " "Unable to get pid for filename pattern %s. Using the " "default name.", FilenamePat); | ||||
728 | "default name.",fprintf(stderr, "LLVM Profile Warning: " "Unable to get pid for filename pattern %s. Using the " "default name.", FilenamePat); | ||||
729 | FilenamePat)fprintf(stderr, "LLVM Profile Warning: " "Unable to get pid for filename pattern %s. Using the " "default name.", FilenamePat);; | ||||
730 | return -1; | ||||
731 | } | ||||
732 | } | ||||
733 | } else if (FilenamePat[I] == 'h') { | ||||
734 | if (!NumHosts++) | ||||
735 | if (COMPILER_RT_GETHOSTNAME(Hostname, COMPILER_RT_MAX_HOSTLEN)lprofGetHostName(Hostname, 128)) { | ||||
736 | PROF_WARN("Unable to get hostname for filename pattern %s. Using "fprintf(stderr, "LLVM Profile Warning: " "Unable to get hostname for filename pattern %s. Using " "the default name.", FilenamePat); | ||||
737 | "the default name.",fprintf(stderr, "LLVM Profile Warning: " "Unable to get hostname for filename pattern %s. Using " "the default name.", FilenamePat); | ||||
738 | FilenamePat)fprintf(stderr, "LLVM Profile Warning: " "Unable to get hostname for filename pattern %s. Using " "the default name.", FilenamePat);; | ||||
739 | return -1; | ||||
740 | } | ||||
741 | } else if (FilenamePat[I] == 't') { | ||||
742 | lprofCurFilename.TmpDir = getenv("TMPDIR"); | ||||
743 | if (!lprofCurFilename.TmpDir) { | ||||
744 | PROF_WARN("Unable to get the TMPDIR environment variable, referenced "fprintf(stderr, "LLVM Profile Warning: " "Unable to get the TMPDIR environment variable, referenced " "in %s. Using the default path.", FilenamePat); | ||||
745 | "in %s. Using the default path.",fprintf(stderr, "LLVM Profile Warning: " "Unable to get the TMPDIR environment variable, referenced " "in %s. Using the default path.", FilenamePat); | ||||
746 | FilenamePat)fprintf(stderr, "LLVM Profile Warning: " "Unable to get the TMPDIR environment variable, referenced " "in %s. Using the default path.", FilenamePat);; | ||||
747 | return -1; | ||||
748 | } | ||||
749 | } else if (FilenamePat[I] == 'c') { | ||||
750 | if (__llvm_profile_is_continuous_mode_enabled()) { | ||||
751 | PROF_WARN("%%c specifier can only be specified once in %s.\n",fprintf(stderr, "LLVM Profile Warning: " "%%c specifier can only be specified once in %s.\n" , FilenamePat); | ||||
752 | FilenamePat)fprintf(stderr, "LLVM Profile Warning: " "%%c specifier can only be specified once in %s.\n" , FilenamePat);; | ||||
753 | return -1; | ||||
754 | } | ||||
755 | #if defined(__APPLE__) || defined(__ELF__1) || defined(_WIN32) | ||||
756 | __llvm_profile_set_page_size(getpagesize()); | ||||
757 | __llvm_profile_enable_continuous_mode(); | ||||
758 | #else | ||||
759 | PROF_WARN("%s", "Continous mode is currently only supported for Mach-O,"fprintf(stderr, "LLVM Profile Warning: " "%s", "Continous mode is currently only supported for Mach-O," " ELF and COFF formats."); | ||||
760 | " ELF and COFF formats.")fprintf(stderr, "LLVM Profile Warning: " "%s", "Continous mode is currently only supported for Mach-O," " ELF and COFF formats.");; | ||||
761 | return -1; | ||||
762 | #endif | ||||
763 | } else { | ||||
764 | unsigned MergePoolSize = getMergePoolSize(FilenamePat, &I); | ||||
765 | if (!MergePoolSize) | ||||
766 | continue; | ||||
767 | if (MergingEnabled) { | ||||
768 | PROF_WARN("%%m specifier can only be specified once in %s.\n",fprintf(stderr, "LLVM Profile Warning: " "%%m specifier can only be specified once in %s.\n" , FilenamePat); | ||||
769 | FilenamePat)fprintf(stderr, "LLVM Profile Warning: " "%%m specifier can only be specified once in %s.\n" , FilenamePat);; | ||||
770 | return -1; | ||||
771 | } | ||||
772 | MergingEnabled = 1; | ||||
773 | lprofCurFilename.MergePoolSize = MergePoolSize; | ||||
774 | } | ||||
775 | } | ||||
776 | } | ||||
777 | |||||
778 | lprofCurFilename.NumPids = NumPids; | ||||
779 | lprofCurFilename.NumHosts = NumHosts; | ||||
780 | return 0; | ||||
781 | } | ||||
782 | |||||
783 | static void parseAndSetFilename(const char *FilenamePat, | ||||
784 | ProfileNameSpecifier PNS, | ||||
785 | unsigned CopyFilenamePat) { | ||||
786 | |||||
787 | const char *OldFilenamePat = lprofCurFilename.FilenamePat; | ||||
788 | ProfileNameSpecifier OldPNS = lprofCurFilename.PNS; | ||||
789 | |||||
790 | /* The old profile name specifier takes precedence over the old one. */ | ||||
791 | if (PNS < OldPNS) | ||||
792 | return; | ||||
793 | |||||
794 | if (!FilenamePat) | ||||
795 | FilenamePat = DefaultProfileName; | ||||
796 | |||||
797 | if (OldFilenamePat && !strcmp(OldFilenamePat, FilenamePat)) { | ||||
798 | lprofCurFilename.PNS = PNS; | ||||
799 | return; | ||||
800 | } | ||||
801 | |||||
802 | /* When PNS >= OldPNS, the last one wins. */ | ||||
803 | if (!FilenamePat || parseFilenamePattern(FilenamePat, CopyFilenamePat)) | ||||
804 | resetFilenameToDefault(); | ||||
805 | lprofCurFilename.PNS = PNS; | ||||
806 | |||||
807 | if (!OldFilenamePat) { | ||||
808 | if (getenv("LLVM_PROFILE_VERBOSE")) | ||||
809 | PROF_NOTE("Set profile file path to \"%s\" via %s.\n",fprintf(stderr, "LLVM Profile Note: " "Set profile file path to \"%s\" via %s.\n" , lprofCurFilename.FilenamePat, getPNSStr(PNS)); | ||||
810 | lprofCurFilename.FilenamePat, getPNSStr(PNS))fprintf(stderr, "LLVM Profile Note: " "Set profile file path to \"%s\" via %s.\n" , lprofCurFilename.FilenamePat, getPNSStr(PNS));; | ||||
811 | } else { | ||||
812 | if (getenv("LLVM_PROFILE_VERBOSE")) | ||||
813 | PROF_NOTE("Override old profile path \"%s\" via %s to \"%s\" via %s.\n",fprintf(stderr, "LLVM Profile Note: " "Override old profile path \"%s\" via %s to \"%s\" via %s.\n" , OldFilenamePat, getPNSStr(OldPNS), lprofCurFilename.FilenamePat , getPNSStr(PNS)); | ||||
814 | OldFilenamePat, getPNSStr(OldPNS), lprofCurFilename.FilenamePat,fprintf(stderr, "LLVM Profile Note: " "Override old profile path \"%s\" via %s to \"%s\" via %s.\n" , OldFilenamePat, getPNSStr(OldPNS), lprofCurFilename.FilenamePat , getPNSStr(PNS)); | ||||
815 | getPNSStr(PNS))fprintf(stderr, "LLVM Profile Note: " "Override old profile path \"%s\" via %s to \"%s\" via %s.\n" , OldFilenamePat, getPNSStr(OldPNS), lprofCurFilename.FilenamePat , getPNSStr(PNS));; | ||||
816 | } | ||||
817 | |||||
818 | truncateCurrentFile(); | ||||
819 | if (__llvm_profile_is_continuous_mode_enabled()) | ||||
820 | initializeProfileForContinuousMode(); | ||||
821 | } | ||||
822 | |||||
823 | /* Return buffer length that is required to store the current profile | ||||
824 | * filename with PID and hostname substitutions. */ | ||||
825 | /* The length to hold uint64_t followed by 3 digits pool id including '_' */ | ||||
826 | #define SIGLEN24 24 | ||||
827 | static int getCurFilenameLength() { | ||||
828 | int Len; | ||||
829 | if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0]) | ||||
830 | return 0; | ||||
831 | |||||
832 | if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts || | ||||
833 | lprofCurFilename.TmpDir || lprofCurFilename.MergePoolSize)) | ||||
834 | return strlen(lprofCurFilename.FilenamePat); | ||||
835 | |||||
836 | Len = strlen(lprofCurFilename.FilenamePat) + | ||||
837 | lprofCurFilename.NumPids * (strlen(lprofCurFilename.PidChars) - 2) + | ||||
838 | lprofCurFilename.NumHosts * (strlen(lprofCurFilename.Hostname) - 2) + | ||||
839 | (lprofCurFilename.TmpDir ? (strlen(lprofCurFilename.TmpDir) - 1) : 0); | ||||
840 | if (lprofCurFilename.MergePoolSize) | ||||
841 | Len += SIGLEN24; | ||||
842 | return Len; | ||||
843 | } | ||||
844 | |||||
845 | /* Return the pointer to the current profile file name (after substituting | ||||
846 | * PIDs and Hostnames in filename pattern. \p FilenameBuf is the buffer | ||||
847 | * to store the resulting filename. If no substitution is needed, the | ||||
848 | * current filename pattern string is directly returned, unless ForceUseBuf | ||||
849 | * is enabled. */ | ||||
850 | static const char *getCurFilename(char *FilenameBuf, int ForceUseBuf) { | ||||
851 | int I, J, PidLength, HostNameLength, TmpDirLength, FilenamePatLength; | ||||
852 | const char *FilenamePat = lprofCurFilename.FilenamePat; | ||||
853 | |||||
854 | if (!lprofCurFilename.FilenamePat
| ||||
855 | return 0; | ||||
856 | |||||
857 | if (!(lprofCurFilename.NumPids
| ||||
858 | lprofCurFilename.TmpDir
| ||||
859 | __llvm_profile_is_continuous_mode_enabled())) { | ||||
860 | if (!ForceUseBuf) | ||||
861 | return lprofCurFilename.FilenamePat; | ||||
862 | |||||
863 | FilenamePatLength = strlen(lprofCurFilename.FilenamePat); | ||||
864 | memcpy(FilenameBuf, lprofCurFilename.FilenamePat, FilenamePatLength); | ||||
865 | FilenameBuf[FilenamePatLength] = '\0'; | ||||
866 | return FilenameBuf; | ||||
867 | } | ||||
868 | |||||
869 | PidLength = strlen(lprofCurFilename.PidChars); | ||||
870 | HostNameLength = strlen(lprofCurFilename.Hostname); | ||||
871 | TmpDirLength = lprofCurFilename.TmpDir ? strlen(lprofCurFilename.TmpDir) : 0; | ||||
872 | /* Construct the new filename. */ | ||||
873 | for (I = 0, J = 0; FilenamePat[I]; ++I) | ||||
874 | if (FilenamePat[I] == '%') { | ||||
875 | if (FilenamePat[++I] == 'p') { | ||||
876 | memcpy(FilenameBuf + J, lprofCurFilename.PidChars, PidLength); | ||||
877 | J += PidLength; | ||||
878 | } else if (FilenamePat[I] == 'h') { | ||||
879 | memcpy(FilenameBuf + J, lprofCurFilename.Hostname, HostNameLength); | ||||
880 | J += HostNameLength; | ||||
881 | } else if (FilenamePat[I] == 't') { | ||||
882 | memcpy(FilenameBuf + J, lprofCurFilename.TmpDir, TmpDirLength); | ||||
| |||||
883 | FilenameBuf[J + TmpDirLength] = DIR_SEPARATOR'/'; | ||||
884 | J += TmpDirLength + 1; | ||||
885 | } else { | ||||
886 | if (!getMergePoolSize(FilenamePat, &I)) | ||||
887 | continue; | ||||
888 | char LoadModuleSignature[SIGLEN24 + 1]; | ||||
889 | int S; | ||||
890 | int ProfilePoolId = getpid() % lprofCurFilename.MergePoolSize; | ||||
891 | S = snprintf(LoadModuleSignature, SIGLEN24 + 1, "%" PRIu64"ll" "u" "_%d", | ||||
892 | lprofGetLoadModuleSignature(), ProfilePoolId); | ||||
893 | if (S == -1 || S > SIGLEN24) | ||||
894 | S = SIGLEN24; | ||||
895 | memcpy(FilenameBuf + J, LoadModuleSignature, S); | ||||
896 | J += S; | ||||
897 | } | ||||
898 | /* Drop any unknown substitutions. */ | ||||
899 | } else | ||||
900 | FilenameBuf[J++] = FilenamePat[I]; | ||||
901 | FilenameBuf[J] = 0; | ||||
902 | |||||
903 | return FilenameBuf; | ||||
904 | } | ||||
905 | |||||
906 | /* Returns the pointer to the environment variable | ||||
907 | * string. Returns null if the env var is not set. */ | ||||
908 | static const char *getFilenamePatFromEnv(void) { | ||||
909 | const char *Filename = getenv("LLVM_PROFILE_FILE"); | ||||
910 | if (!Filename || !Filename[0]) | ||||
911 | return 0; | ||||
912 | return Filename; | ||||
913 | } | ||||
914 | |||||
915 | COMPILER_RT_VISIBILITY__attribute__((visibility("hidden"))) | ||||
916 | const char *__llvm_profile_get_path_prefix(void) { | ||||
917 | int Length; | ||||
918 | char *FilenameBuf, *Prefix; | ||||
919 | const char *Filename, *PrefixEnd; | ||||
920 | |||||
921 | if (lprofCurFilename.ProfilePathPrefix) | ||||
922 | return lprofCurFilename.ProfilePathPrefix; | ||||
923 | |||||
924 | Length = getCurFilenameLength(); | ||||
925 | FilenameBuf = (char *)COMPILER_RT_ALLOCA__builtin_alloca(Length + 1); | ||||
926 | Filename = getCurFilename(FilenameBuf, 0); | ||||
927 | if (!Filename) | ||||
928 | return "\0"; | ||||
929 | |||||
930 | PrefixEnd = lprofFindLastDirSeparator(Filename); | ||||
931 | if (!PrefixEnd) | ||||
932 | return "\0"; | ||||
933 | |||||
934 | Length = PrefixEnd - Filename + 1; | ||||
935 | Prefix = (char *)malloc(Length + 1); | ||||
936 | if (!Prefix) { | ||||
937 | PROF_ERR("Failed to %s\n", "allocate memory.")fprintf(stderr, "LLVM Profile Error: " "Failed to %s\n", "allocate memory." );; | ||||
938 | return "\0"; | ||||
939 | } | ||||
940 | memcpy(Prefix, Filename, Length); | ||||
941 | Prefix[Length] = '\0'; | ||||
942 | lprofCurFilename.ProfilePathPrefix = Prefix; | ||||
943 | return Prefix; | ||||
944 | } | ||||
945 | |||||
946 | COMPILER_RT_VISIBILITY__attribute__((visibility("hidden"))) | ||||
947 | const char *__llvm_profile_get_filename(void) { | ||||
948 | int Length; | ||||
949 | char *FilenameBuf; | ||||
950 | const char *Filename; | ||||
951 | |||||
952 | Length = getCurFilenameLength(); | ||||
953 | FilenameBuf = (char *)malloc(Length + 1); | ||||
954 | if (!FilenameBuf) { | ||||
955 | PROF_ERR("Failed to %s\n", "allocate memory.")fprintf(stderr, "LLVM Profile Error: " "Failed to %s\n", "allocate memory." );; | ||||
956 | return "\0"; | ||||
957 | } | ||||
958 | Filename = getCurFilename(FilenameBuf, 1); | ||||
959 | if (!Filename) | ||||
960 | return "\0"; | ||||
961 | |||||
962 | return FilenameBuf; | ||||
963 | } | ||||
964 | |||||
965 | /* This API initializes the file handling, both user specified | ||||
966 | * profile path via -fprofile-instr-generate= and LLVM_PROFILE_FILE | ||||
967 | * environment variable can override this default value. | ||||
968 | */ | ||||
969 | COMPILER_RT_VISIBILITY__attribute__((visibility("hidden"))) | ||||
970 | void __llvm_profile_initialize_file(void) { | ||||
971 | const char *EnvFilenamePat; | ||||
972 | const char *SelectedPat = NULL((void*)0); | ||||
973 | ProfileNameSpecifier PNS = PNS_unknown; | ||||
974 | int hasCommandLineOverrider = (INSTR_PROF_PROFILE_NAME_VAR__llvm_profile_filename[0] != 0); | ||||
975 | |||||
976 | EnvFilenamePat = getFilenamePatFromEnv(); | ||||
977 | if (EnvFilenamePat) { | ||||
978 | /* Pass CopyFilenamePat = 1, to ensure that the filename would be valid | ||||
979 | at the moment when __llvm_profile_write_file() gets executed. */ | ||||
980 | parseAndSetFilename(EnvFilenamePat, PNS_environment, 1); | ||||
981 | return; | ||||
982 | } else if (hasCommandLineOverrider) { | ||||
983 | SelectedPat = INSTR_PROF_PROFILE_NAME_VAR__llvm_profile_filename; | ||||
984 | PNS = PNS_command_line; | ||||
985 | } else { | ||||
986 | SelectedPat = NULL((void*)0); | ||||
987 | PNS = PNS_default; | ||||
988 | } | ||||
989 | |||||
990 | parseAndSetFilename(SelectedPat, PNS, 0); | ||||
991 | } | ||||
992 | |||||
993 | /* This method is invoked by the runtime initialization hook | ||||
994 | * InstrProfilingRuntime.o if it is linked in. | ||||
995 | */ | ||||
996 | COMPILER_RT_VISIBILITY__attribute__((visibility("hidden"))) | ||||
997 | void __llvm_profile_initialize(void) { | ||||
998 | __llvm_profile_initialize_file(); | ||||
999 | if (!__llvm_profile_is_continuous_mode_enabled()) | ||||
1000 | __llvm_profile_register_write_file_atexit(); | ||||
1001 | } | ||||
1002 | |||||
1003 | /* This API is directly called by the user application code. It has the | ||||
1004 | * highest precedence compared with LLVM_PROFILE_FILE environment variable | ||||
1005 | * and command line option -fprofile-instr-generate=<profile_name>. | ||||
1006 | */ | ||||
1007 | COMPILER_RT_VISIBILITY__attribute__((visibility("hidden"))) | ||||
1008 | void __llvm_profile_set_filename(const char *FilenamePat) { | ||||
1009 | if (__llvm_profile_is_continuous_mode_enabled()) | ||||
1010 | return; | ||||
1011 | parseAndSetFilename(FilenamePat, PNS_runtime_api, 1); | ||||
1012 | } | ||||
1013 | |||||
1014 | /* The public API for writing profile data into the file with name | ||||
1015 | * set by previous calls to __llvm_profile_set_filename or | ||||
1016 | * __llvm_profile_override_default_filename or | ||||
1017 | * __llvm_profile_initialize_file. */ | ||||
1018 | COMPILER_RT_VISIBILITY__attribute__((visibility("hidden"))) | ||||
1019 | int __llvm_profile_write_file(void) { | ||||
1020 | int rc, Length; | ||||
1021 | const char *Filename; | ||||
1022 | char *FilenameBuf; | ||||
1023 | int PDeathSig = 0; | ||||
1024 | |||||
1025 | if (lprofProfileDumped() || __llvm_profile_is_continuous_mode_enabled()) { | ||||
1026 | PROF_NOTE("Profile data not written to file: %s.\n", "already written")fprintf(stderr, "LLVM Profile Note: " "Profile data not written to file: %s.\n" , "already written");; | ||||
1027 | return 0; | ||||
1028 | } | ||||
1029 | |||||
1030 | Length = getCurFilenameLength(); | ||||
1031 | FilenameBuf = (char *)COMPILER_RT_ALLOCA__builtin_alloca(Length + 1); | ||||
1032 | Filename = getCurFilename(FilenameBuf, 0); | ||||
1033 | |||||
1034 | /* Check the filename. */ | ||||
1035 | if (!Filename) { | ||||
1036 | PROF_ERR("Failed to write file : %s\n", "Filename not set")fprintf(stderr, "LLVM Profile Error: " "Failed to write file : %s\n" , "Filename not set");; | ||||
1037 | return -1; | ||||
1038 | } | ||||
1039 | |||||
1040 | /* Check if there is llvm/runtime version mismatch. */ | ||||
1041 | if (GET_VERSION(__llvm_profile_get_version())((__llvm_profile_get_version()) & ~0xff00000000000000ULL) != INSTR_PROF_RAW_VERSION8) { | ||||
1042 | PROF_ERR("Runtime and instrumentation version mismatch : "fprintf(stderr, "LLVM Profile Error: " "Runtime and instrumentation version mismatch : " "expected %d, but get %d\n", 8, (int)((__llvm_profile_get_version ()) & ~0xff00000000000000ULL)); | ||||
1043 | "expected %d, but get %d\n",fprintf(stderr, "LLVM Profile Error: " "Runtime and instrumentation version mismatch : " "expected %d, but get %d\n", 8, (int)((__llvm_profile_get_version ()) & ~0xff00000000000000ULL)); | ||||
1044 | INSTR_PROF_RAW_VERSION,fprintf(stderr, "LLVM Profile Error: " "Runtime and instrumentation version mismatch : " "expected %d, but get %d\n", 8, (int)((__llvm_profile_get_version ()) & ~0xff00000000000000ULL)); | ||||
1045 | (int)GET_VERSION(__llvm_profile_get_version()))fprintf(stderr, "LLVM Profile Error: " "Runtime and instrumentation version mismatch : " "expected %d, but get %d\n", 8, (int)((__llvm_profile_get_version ()) & ~0xff00000000000000ULL));; | ||||
1046 | return -1; | ||||
1047 | } | ||||
1048 | |||||
1049 | // Temporarily suspend getting SIGKILL when the parent exits. | ||||
1050 | PDeathSig = lprofSuspendSigKill(); | ||||
1051 | |||||
1052 | /* Write profile data to the file. */ | ||||
1053 | rc = writeFile(Filename); | ||||
1054 | if (rc) | ||||
1055 | PROF_ERR("Failed to write file \"%s\": %s\n", Filename, strerror(errno))fprintf(stderr, "LLVM Profile Error: " "Failed to write file \"%s\": %s\n" , Filename, strerror((*__errno_location ())));; | ||||
1056 | |||||
1057 | // Restore SIGKILL. | ||||
1058 | if (PDeathSig == 1) | ||||
1059 | lprofRestoreSigKill(); | ||||
1060 | |||||
1061 | return rc; | ||||
1062 | } | ||||
1063 | |||||
1064 | COMPILER_RT_VISIBILITY__attribute__((visibility("hidden"))) | ||||
1065 | int __llvm_profile_dump(void) { | ||||
1066 | if (!doMerging()) | ||||
1067 | PROF_WARN("Later invocation of __llvm_profile_dump can lead to clobbering "fprintf(stderr, "LLVM Profile Warning: " "Later invocation of __llvm_profile_dump can lead to clobbering " " of previously dumped profile data : %s. Either use %%m " "in profile name or change profile name before dumping.\n" , "online profile merging is not on"); | ||||
1068 | " of previously dumped profile data : %s. Either use %%m "fprintf(stderr, "LLVM Profile Warning: " "Later invocation of __llvm_profile_dump can lead to clobbering " " of previously dumped profile data : %s. Either use %%m " "in profile name or change profile name before dumping.\n" , "online profile merging is not on"); | ||||
1069 | "in profile name or change profile name before dumping.\n",fprintf(stderr, "LLVM Profile Warning: " "Later invocation of __llvm_profile_dump can lead to clobbering " " of previously dumped profile data : %s. Either use %%m " "in profile name or change profile name before dumping.\n" , "online profile merging is not on"); | ||||
1070 | "online profile merging is not on")fprintf(stderr, "LLVM Profile Warning: " "Later invocation of __llvm_profile_dump can lead to clobbering " " of previously dumped profile data : %s. Either use %%m " "in profile name or change profile name before dumping.\n" , "online profile merging is not on");; | ||||
1071 | int rc = __llvm_profile_write_file(); | ||||
1072 | lprofSetProfileDumped(1); | ||||
1073 | return rc; | ||||
1074 | } | ||||
1075 | |||||
1076 | /* Order file data will be saved in a file with suffx .order. */ | ||||
1077 | static const char *OrderFileSuffix = ".order"; | ||||
1078 | |||||
1079 | COMPILER_RT_VISIBILITY__attribute__((visibility("hidden"))) | ||||
1080 | int __llvm_orderfile_write_file(void) { | ||||
1081 | int rc, Length, LengthBeforeAppend, SuffixLength; | ||||
1082 | const char *Filename; | ||||
1083 | char *FilenameBuf; | ||||
1084 | int PDeathSig = 0; | ||||
1085 | |||||
1086 | SuffixLength = strlen(OrderFileSuffix); | ||||
1087 | Length = getCurFilenameLength() + SuffixLength; | ||||
1088 | FilenameBuf = (char *)COMPILER_RT_ALLOCA__builtin_alloca(Length + 1); | ||||
1089 | Filename = getCurFilename(FilenameBuf, 1); | ||||
1090 | |||||
1091 | /* Check the filename. */ | ||||
1092 | if (!Filename) { | ||||
1093 | PROF_ERR("Failed to write file : %s\n", "Filename not set")fprintf(stderr, "LLVM Profile Error: " "Failed to write file : %s\n" , "Filename not set");; | ||||
1094 | return -1; | ||||
1095 | } | ||||
1096 | |||||
1097 | /* Append order file suffix */ | ||||
1098 | LengthBeforeAppend = strlen(Filename); | ||||
1099 | memcpy(FilenameBuf + LengthBeforeAppend, OrderFileSuffix, SuffixLength); | ||||
1100 | FilenameBuf[LengthBeforeAppend + SuffixLength] = '\0'; | ||||
1101 | |||||
1102 | /* Check if there is llvm/runtime version mismatch. */ | ||||
1103 | if (GET_VERSION(__llvm_profile_get_version())((__llvm_profile_get_version()) & ~0xff00000000000000ULL) != INSTR_PROF_RAW_VERSION8) { | ||||
1104 | PROF_ERR("Runtime and instrumentation version mismatch : "fprintf(stderr, "LLVM Profile Error: " "Runtime and instrumentation version mismatch : " "expected %d, but get %d\n", 8, (int)((__llvm_profile_get_version ()) & ~0xff00000000000000ULL)); | ||||
1105 | "expected %d, but get %d\n",fprintf(stderr, "LLVM Profile Error: " "Runtime and instrumentation version mismatch : " "expected %d, but get %d\n", 8, (int)((__llvm_profile_get_version ()) & ~0xff00000000000000ULL)); | ||||
1106 | INSTR_PROF_RAW_VERSION,fprintf(stderr, "LLVM Profile Error: " "Runtime and instrumentation version mismatch : " "expected %d, but get %d\n", 8, (int)((__llvm_profile_get_version ()) & ~0xff00000000000000ULL)); | ||||
1107 | (int)GET_VERSION(__llvm_profile_get_version()))fprintf(stderr, "LLVM Profile Error: " "Runtime and instrumentation version mismatch : " "expected %d, but get %d\n", 8, (int)((__llvm_profile_get_version ()) & ~0xff00000000000000ULL));; | ||||
1108 | return -1; | ||||
1109 | } | ||||
1110 | |||||
1111 | // Temporarily suspend getting SIGKILL when the parent exits. | ||||
1112 | PDeathSig = lprofSuspendSigKill(); | ||||
1113 | |||||
1114 | /* Write order data to the file. */ | ||||
1115 | rc = writeOrderFile(Filename); | ||||
1116 | if (rc) | ||||
1117 | PROF_ERR("Failed to write file \"%s\": %s\n", Filename, strerror(errno))fprintf(stderr, "LLVM Profile Error: " "Failed to write file \"%s\": %s\n" , Filename, strerror((*__errno_location ())));; | ||||
1118 | |||||
1119 | // Restore SIGKILL. | ||||
1120 | if (PDeathSig == 1) | ||||
1121 | lprofRestoreSigKill(); | ||||
1122 | |||||
1123 | return rc; | ||||
1124 | } | ||||
1125 | |||||
1126 | COMPILER_RT_VISIBILITY__attribute__((visibility("hidden"))) | ||||
1127 | int __llvm_orderfile_dump(void) { | ||||
1128 | int rc = __llvm_orderfile_write_file(); | ||||
| |||||
1129 | return rc; | ||||
1130 | } | ||||
1131 | |||||
1132 | static void writeFileWithoutReturn(void) { __llvm_profile_write_file(); } | ||||
1133 | |||||
1134 | COMPILER_RT_VISIBILITY__attribute__((visibility("hidden"))) | ||||
1135 | int __llvm_profile_register_write_file_atexit(void) { | ||||
1136 | static int HasBeenRegistered = 0; | ||||
1137 | |||||
1138 | if (HasBeenRegistered) | ||||
1139 | return 0; | ||||
1140 | |||||
1141 | lprofSetupValueProfiler(); | ||||
1142 | |||||
1143 | HasBeenRegistered = 1; | ||||
1144 | return atexit(writeFileWithoutReturn); | ||||
1145 | } | ||||
1146 | |||||
1147 | COMPILER_RT_VISIBILITY__attribute__((visibility("hidden"))) int __llvm_profile_set_file_object(FILE *File, | ||||
1148 | int EnableMerge) { | ||||
1149 | if (__llvm_profile_is_continuous_mode_enabled()) { | ||||
1150 | if (!EnableMerge) { | ||||
1151 | PROF_WARN("__llvm_profile_set_file_object(fd=%d) not supported in "fprintf(stderr, "LLVM Profile Warning: " "__llvm_profile_set_file_object(fd=%d) not supported in " "continuous sync mode when merging is disabled\n", fileno(File )); | ||||
1152 | "continuous sync mode when merging is disabled\n",fprintf(stderr, "LLVM Profile Warning: " "__llvm_profile_set_file_object(fd=%d) not supported in " "continuous sync mode when merging is disabled\n", fileno(File )); | ||||
1153 | fileno(File))fprintf(stderr, "LLVM Profile Warning: " "__llvm_profile_set_file_object(fd=%d) not supported in " "continuous sync mode when merging is disabled\n", fileno(File ));; | ||||
1154 | return 1; | ||||
1155 | } | ||||
1156 | if (lprofLockFileHandle(File) != 0) { | ||||
1157 | PROF_WARN("Data may be corrupted during profile merging : %s\n",fprintf(stderr, "LLVM Profile Warning: " "Data may be corrupted during profile merging : %s\n" , "Fail to obtain file lock due to system limit."); | ||||
1158 | "Fail to obtain file lock due to system limit.")fprintf(stderr, "LLVM Profile Warning: " "Data may be corrupted during profile merging : %s\n" , "Fail to obtain file lock due to system limit.");; | ||||
1159 | } | ||||
1160 | uint64_t ProfileFileSize = 0; | ||||
1161 | if (getProfileFileSizeForMerging(File, &ProfileFileSize) == -1) { | ||||
1162 | lprofUnlockFileHandle(File); | ||||
1163 | return 1; | ||||
1164 | } | ||||
1165 | if (ProfileFileSize == 0) { | ||||
1166 | FreeHook = &free; | ||||
1167 | setupIOBuffer(); | ||||
1168 | ProfDataWriter fileWriter; | ||||
1169 | initFileWriter(&fileWriter, File); | ||||
1170 | if (lprofWriteData(&fileWriter, 0, 0)) { | ||||
1171 | lprofUnlockFileHandle(File); | ||||
1172 | PROF_ERR("Failed to write file \"%d\": %s\n", fileno(File),fprintf(stderr, "LLVM Profile Error: " "Failed to write file \"%d\": %s\n" , fileno(File), strerror((*__errno_location ()))); | ||||
1173 | strerror(errno))fprintf(stderr, "LLVM Profile Error: " "Failed to write file \"%d\": %s\n" , fileno(File), strerror((*__errno_location ())));; | ||||
1174 | return 1; | ||||
1175 | } | ||||
1176 | fflush(File); | ||||
1177 | } else { | ||||
1178 | /* The merged profile has a non-zero length. Check that it is compatible | ||||
1179 | * with the data in this process. */ | ||||
1180 | char *ProfileBuffer; | ||||
1181 | if (mmapProfileForMerging(File, ProfileFileSize, &ProfileBuffer) == -1) { | ||||
1182 | lprofUnlockFileHandle(File); | ||||
1183 | return 1; | ||||
1184 | } | ||||
1185 | (void)munmap(ProfileBuffer, ProfileFileSize); | ||||
1186 | } | ||||
1187 | mmapForContinuousMode(0, File); | ||||
1188 | lprofUnlockFileHandle(File); | ||||
1189 | } else { | ||||
1190 | setProfileFile(File); | ||||
1191 | setProfileMergeRequested(EnableMerge); | ||||
1192 | } | ||||
1193 | return 0; | ||||
1194 | } | ||||
1195 | |||||
1196 | #endif |