1 /*===- InstrProfilingFile.c - Write instrumentation to a file -------------===*\
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"
11 #include "InstrProfilingInternal.h"
12 #include "InstrProfilingUtil.h"
13 #include <errno.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #ifdef _MSC_VER
18 /* For _alloca. */
19 #include <malloc.h>
20 #endif
21 #if defined(_WIN32)
22 #include "WindowsMMap.h"
23 /* For _chsize_s */
24 #include <io.h>
25 #else
26 #include <sys/file.h>
27 #include <sys/mman.h>
28 #include <unistd.h>
29 #if defined(__linux__)
30 #include <sys/types.h>
31 #endif
32 #endif
33 
34 /* From where is profile name specified.
35  * The order the enumerators define their
36  * precedence. Re-order them may lead to
37  * runtime behavior change. */
38 typedef enum ProfileNameSpecifier {
39   PNS_unknown = 0,
40   PNS_default,
41   PNS_command_line,
42   PNS_environment,
43   PNS_runtime_api
44 } ProfileNameSpecifier;
45 
46 static const char *getPNSStr(ProfileNameSpecifier PNS) {
47   switch (PNS) {
48   case PNS_default:
49     return "default setting";
50   case PNS_command_line:
51     return "command line";
52   case PNS_environment:
53     return "environment variable";
54   case PNS_runtime_api:
55     return "runtime API";
56   default:
57     return "Unknown";
58   }
59 }
60 
61 #define MAX_PID_SIZE 16
62 /* Data structure holding the result of parsed filename pattern. */
63 typedef struct lprofFilename {
64   /* File name string possibly with %p or %h specifiers. */
65   const char *FilenamePat;
66   /* A flag indicating if FilenamePat's memory is allocated
67    * by runtime. */
68   unsigned OwnsFilenamePat;
69   const char *ProfilePathPrefix;
70   char PidChars[MAX_PID_SIZE];
71   char Hostname[COMPILER_RT_MAX_HOSTLEN];
72   unsigned NumPids;
73   unsigned NumHosts;
74   /* When in-process merging is enabled, this parameter specifies
75    * the total number of profile data files shared by all the processes
76    * spawned from the same binary. By default the value is 1. If merging
77    * is not enabled, its value should be 0. This parameter is specified
78    * by the %[0-9]m specifier. For instance %2m enables merging using
79    * 2 profile data files. %1m is equivalent to %m. Also %m specifier
80    * can only appear once at the end of the name pattern. */
81   unsigned MergePoolSize;
82   ProfileNameSpecifier PNS;
83 } lprofFilename;
84 
85 lprofFilename lprofCurFilename = {0, 0, 0, {0}, {0}, 0, 0, 0, PNS_unknown};
86 
87 int getpid(void);
88 static int getCurFilenameLength();
89 static const char *getCurFilename(char *FilenameBuf);
90 static unsigned doMerging() { return lprofCurFilename.MergePoolSize; }
91 
92 /* Return 1 if there is an error, otherwise return  0.  */
93 static uint32_t fileWriter(ProfDataIOVec *IOVecs, uint32_t NumIOVecs,
94                            void **WriterCtx) {
95   uint32_t I;
96   FILE *File = (FILE *)*WriterCtx;
97   for (I = 0; I < NumIOVecs; I++) {
98     if (fwrite(IOVecs[I].Data, IOVecs[I].ElmSize, IOVecs[I].NumElm, File) !=
99         IOVecs[I].NumElm)
100       return 1;
101   }
102   return 0;
103 }
104 
105 COMPILER_RT_VISIBILITY ProfBufferIO *
106 lprofCreateBufferIOInternal(void *File, uint32_t BufferSz) {
107   FreeHook = &free;
108   DynamicBufferIOBuffer = (uint8_t *)calloc(BufferSz, 1);
109   VPBufferSize = BufferSz;
110   return lprofCreateBufferIO(fileWriter, File);
111 }
112 
113 static void setupIOBuffer() {
114   const char *BufferSzStr = 0;
115   BufferSzStr = getenv("LLVM_VP_BUFFER_SIZE");
116   if (BufferSzStr && BufferSzStr[0]) {
117     VPBufferSize = atoi(BufferSzStr);
118     DynamicBufferIOBuffer = (uint8_t *)calloc(VPBufferSize, 1);
119   }
120 }
121 
122 /* Read profile data in \c ProfileFile and merge with in-memory
123    profile counters. Returns -1 if there is fatal error, otheriwse
124    0 is returned.
125 */
126 static int doProfileMerging(FILE *ProfileFile) {
127   uint64_t ProfileFileSize;
128   char *ProfileBuffer;
129 
130   if (fseek(ProfileFile, 0L, SEEK_END) == -1) {
131     PROF_ERR("Unable to merge profile data, unable to get size: %s\n",
132              strerror(errno));
133     return -1;
134   }
135   ProfileFileSize = ftell(ProfileFile);
136 
137   /* Restore file offset.  */
138   if (fseek(ProfileFile, 0L, SEEK_SET) == -1) {
139     PROF_ERR("Unable to merge profile data, unable to rewind: %s\n",
140              strerror(errno));
141     return -1;
142   }
143 
144   /* Nothing to merge.  */
145   if (ProfileFileSize < sizeof(__llvm_profile_header)) {
146     if (ProfileFileSize)
147       PROF_WARN("Unable to merge profile data: %s\n",
148                 "source profile file is too small.");
149     return 0;
150   }
151 
152   ProfileBuffer = mmap(NULL, ProfileFileSize, PROT_READ, MAP_SHARED | MAP_FILE,
153                        fileno(ProfileFile), 0);
154   if (ProfileBuffer == MAP_FAILED) {
155     PROF_ERR("Unable to merge profile data, mmap failed: %s\n",
156              strerror(errno));
157     return -1;
158   }
159 
160   if (__llvm_profile_check_compatibility(ProfileBuffer, ProfileFileSize)) {
161     (void)munmap(ProfileBuffer, ProfileFileSize);
162     PROF_WARN("Unable to merge profile data: %s\n",
163               "source profile file is not compatible.");
164     return 0;
165   }
166 
167   /* Now start merging */
168   __llvm_profile_merge_from_buffer(ProfileBuffer, ProfileFileSize);
169   (void)munmap(ProfileBuffer, ProfileFileSize);
170 
171   return 0;
172 }
173 
174 /* Open the profile data for merging. It opens the file in r+b mode with
175  * file locking.  If the file has content which is compatible with the
176  * current process, it also reads in the profile data in the file and merge
177  * it with in-memory counters. After the profile data is merged in memory,
178  * the original profile data is truncated and gets ready for the profile
179  * dumper. With profile merging enabled, each executable as well as any of
180  * its instrumented shared libraries dump profile data into their own data file.
181 */
182 static FILE *openFileForMerging(const char *ProfileFileName) {
183   FILE *ProfileFile;
184   int rc;
185 
186   ProfileFile = lprofOpenFileEx(ProfileFileName);
187   if (!ProfileFile)
188     return NULL;
189 
190   rc = doProfileMerging(ProfileFile);
191   if (rc || COMPILER_RT_FTRUNCATE(ProfileFile, 0L) ||
192       fseek(ProfileFile, 0L, SEEK_SET) == -1) {
193     PROF_ERR("Profile Merging of file %s failed: %s\n", ProfileFileName,
194              strerror(errno));
195     fclose(ProfileFile);
196     return NULL;
197   }
198   fseek(ProfileFile, 0L, SEEK_SET);
199   return ProfileFile;
200 }
201 
202 /* Write profile data to file \c OutputName.  */
203 static int writeFile(const char *OutputName) {
204   int RetVal;
205   FILE *OutputFile;
206 
207   if (!doMerging())
208     OutputFile = fopen(OutputName, "ab");
209   else
210     OutputFile = openFileForMerging(OutputName);
211 
212   if (!OutputFile)
213     return -1;
214 
215   FreeHook = &free;
216   setupIOBuffer();
217   RetVal = lprofWriteData(fileWriter, OutputFile, lprofGetVPDataReader());
218 
219   fclose(OutputFile);
220   return RetVal;
221 }
222 
223 static void truncateCurrentFile(void) {
224   const char *Filename;
225   char *FilenameBuf;
226   FILE *File;
227   int Length;
228 
229   Length = getCurFilenameLength();
230   FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
231   Filename = getCurFilename(FilenameBuf);
232   if (!Filename)
233     return;
234 
235   /* Create the directory holding the file, if needed. */
236   if (lprofFindFirstDirSeparator(Filename)) {
237     char *Copy = (char *)COMPILER_RT_ALLOCA(Length + 1);
238     strncpy(Copy, Filename, Length + 1);
239     __llvm_profile_recursive_mkdir(Copy);
240   }
241 
242   /* By pass file truncation to allow online raw profile
243    * merging. */
244   if (lprofCurFilename.MergePoolSize)
245     return;
246 
247   /* Truncate the file.  Later we'll reopen and append. */
248   File = fopen(Filename, "w");
249   if (!File)
250     return;
251   fclose(File);
252 }
253 
254 static const char *DefaultProfileName = "default.profraw";
255 static void resetFilenameToDefault(void) {
256   if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) {
257     free((void *)lprofCurFilename.FilenamePat);
258   }
259   memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
260   lprofCurFilename.FilenamePat = DefaultProfileName;
261   lprofCurFilename.PNS = PNS_default;
262 }
263 
264 static int containsMergeSpecifier(const char *FilenamePat, int I) {
265   return (FilenamePat[I] == 'm' ||
266           (FilenamePat[I] >= '1' && FilenamePat[I] <= '9' &&
267            /* If FilenamePat[I] is not '\0', the next byte is guaranteed
268             * to be in-bound as the string is null terminated. */
269            FilenamePat[I + 1] == 'm'));
270 }
271 
272 /* Parses the pattern string \p FilenamePat and stores the result to
273  * lprofcurFilename structure. */
274 static int parseFilenamePattern(const char *FilenamePat,
275                                 unsigned CopyFilenamePat) {
276   int NumPids = 0, NumHosts = 0, I;
277   char *PidChars = &lprofCurFilename.PidChars[0];
278   char *Hostname = &lprofCurFilename.Hostname[0];
279   int MergingEnabled = 0;
280 
281   /* Clean up cached prefix.  */
282   if (lprofCurFilename.ProfilePathPrefix)
283     free((void *)lprofCurFilename.ProfilePathPrefix);
284   memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
285 
286   if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) {
287     free((void *)lprofCurFilename.FilenamePat);
288   }
289 
290   if (!CopyFilenamePat)
291     lprofCurFilename.FilenamePat = FilenamePat;
292   else {
293     lprofCurFilename.FilenamePat = strdup(FilenamePat);
294     lprofCurFilename.OwnsFilenamePat = 1;
295   }
296   /* Check the filename for "%p", which indicates a pid-substitution. */
297   for (I = 0; FilenamePat[I]; ++I)
298     if (FilenamePat[I] == '%') {
299       if (FilenamePat[++I] == 'p') {
300         if (!NumPids++) {
301           if (snprintf(PidChars, MAX_PID_SIZE, "%d", getpid()) <= 0) {
302             PROF_WARN(
303                 "Unable to parse filename pattern %s. Using the default name.",
304                 FilenamePat);
305             return -1;
306           }
307         }
308       } else if (FilenamePat[I] == 'h') {
309         if (!NumHosts++)
310           if (COMPILER_RT_GETHOSTNAME(Hostname, COMPILER_RT_MAX_HOSTLEN)) {
311             PROF_WARN(
312                 "Unable to parse filename pattern %s. Using the default name.",
313                 FilenamePat);
314             return -1;
315           }
316       } else if (containsMergeSpecifier(FilenamePat, I)) {
317         if (MergingEnabled) {
318           PROF_WARN("%%m specifier can only be specified once in %s.\n",
319                     FilenamePat);
320           return -1;
321         }
322         MergingEnabled = 1;
323         if (FilenamePat[I] == 'm')
324           lprofCurFilename.MergePoolSize = 1;
325         else {
326           lprofCurFilename.MergePoolSize = FilenamePat[I] - '0';
327           I++; /* advance to 'm' */
328         }
329       }
330     }
331 
332   lprofCurFilename.NumPids = NumPids;
333   lprofCurFilename.NumHosts = NumHosts;
334   return 0;
335 }
336 
337 static void parseAndSetFilename(const char *FilenamePat,
338                                 ProfileNameSpecifier PNS,
339                                 unsigned CopyFilenamePat) {
340 
341   const char *OldFilenamePat = lprofCurFilename.FilenamePat;
342   ProfileNameSpecifier OldPNS = lprofCurFilename.PNS;
343 
344   if (PNS < OldPNS)
345     return;
346 
347   if (!FilenamePat)
348     FilenamePat = DefaultProfileName;
349 
350   if (OldFilenamePat && !strcmp(OldFilenamePat, FilenamePat)) {
351     lprofCurFilename.PNS = PNS;
352     return;
353   }
354 
355   /* When PNS >= OldPNS, the last one wins. */
356   if (!FilenamePat || parseFilenamePattern(FilenamePat, CopyFilenamePat))
357     resetFilenameToDefault();
358   lprofCurFilename.PNS = PNS;
359 
360   if (!OldFilenamePat) {
361     PROF_NOTE("Set profile file path to \"%s\" via %s.\n",
362               lprofCurFilename.FilenamePat, getPNSStr(PNS));
363   } else {
364     PROF_NOTE("Override old profile path \"%s\" via %s to \"%s\" via %s.\n",
365               OldFilenamePat, getPNSStr(OldPNS), lprofCurFilename.FilenamePat,
366               getPNSStr(PNS));
367   }
368 
369   truncateCurrentFile();
370 }
371 
372 /* Return buffer length that is required to store the current profile
373  * filename with PID and hostname substitutions. */
374 /* The length to hold uint64_t followed by 2 digit pool id including '_' */
375 #define SIGLEN 24
376 static int getCurFilenameLength() {
377   int Len;
378   if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
379     return 0;
380 
381   if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
382         lprofCurFilename.MergePoolSize))
383     return strlen(lprofCurFilename.FilenamePat);
384 
385   Len = strlen(lprofCurFilename.FilenamePat) +
386         lprofCurFilename.NumPids * (strlen(lprofCurFilename.PidChars) - 2) +
387         lprofCurFilename.NumHosts * (strlen(lprofCurFilename.Hostname) - 2);
388   if (lprofCurFilename.MergePoolSize)
389     Len += SIGLEN;
390   return Len;
391 }
392 
393 /* Return the pointer to the current profile file name (after substituting
394  * PIDs and Hostnames in filename pattern. \p FilenameBuf is the buffer
395  * to store the resulting filename. If no substitution is needed, the
396  * current filename pattern string is directly returned. */
397 static const char *getCurFilename(char *FilenameBuf) {
398   int I, J, PidLength, HostNameLength;
399   const char *FilenamePat = lprofCurFilename.FilenamePat;
400 
401   if (!lprofCurFilename.FilenamePat || !lprofCurFilename.FilenamePat[0])
402     return 0;
403 
404   if (!(lprofCurFilename.NumPids || lprofCurFilename.NumHosts ||
405         lprofCurFilename.MergePoolSize))
406     return lprofCurFilename.FilenamePat;
407 
408   PidLength = strlen(lprofCurFilename.PidChars);
409   HostNameLength = strlen(lprofCurFilename.Hostname);
410   /* Construct the new filename. */
411   for (I = 0, J = 0; FilenamePat[I]; ++I)
412     if (FilenamePat[I] == '%') {
413       if (FilenamePat[++I] == 'p') {
414         memcpy(FilenameBuf + J, lprofCurFilename.PidChars, PidLength);
415         J += PidLength;
416       } else if (FilenamePat[I] == 'h') {
417         memcpy(FilenameBuf + J, lprofCurFilename.Hostname, HostNameLength);
418         J += HostNameLength;
419       } else if (containsMergeSpecifier(FilenamePat, I)) {
420         char LoadModuleSignature[SIGLEN];
421         int S;
422         int ProfilePoolId = getpid() % lprofCurFilename.MergePoolSize;
423         S = snprintf(LoadModuleSignature, SIGLEN, "%" PRIu64 "_%d",
424                      lprofGetLoadModuleSignature(), ProfilePoolId);
425         if (S == -1 || S > SIGLEN)
426           S = SIGLEN;
427         memcpy(FilenameBuf + J, LoadModuleSignature, S);
428         J += S;
429         if (FilenamePat[I] != 'm')
430           I++;
431       }
432       /* Drop any unknown substitutions. */
433     } else
434       FilenameBuf[J++] = FilenamePat[I];
435   FilenameBuf[J] = 0;
436 
437   return FilenameBuf;
438 }
439 
440 /* Returns the pointer to the environment variable
441  * string. Returns null if the env var is not set. */
442 static const char *getFilenamePatFromEnv(void) {
443   const char *Filename = getenv("LLVM_PROFILE_FILE");
444   if (!Filename || !Filename[0])
445     return 0;
446   return Filename;
447 }
448 
449 COMPILER_RT_VISIBILITY
450 const char *__llvm_profile_get_path_prefix(void) {
451   int Length;
452   char *FilenameBuf, *Prefix;
453   const char *Filename, *PrefixEnd;
454 
455   if (lprofCurFilename.ProfilePathPrefix)
456     return lprofCurFilename.ProfilePathPrefix;
457 
458   Length = getCurFilenameLength();
459   FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
460   Filename = getCurFilename(FilenameBuf);
461   if (!Filename)
462     return "\0";
463 
464   PrefixEnd = lprofFindLastDirSeparator(Filename);
465   if (!PrefixEnd)
466     return "\0";
467 
468   Length = PrefixEnd - Filename + 1;
469   Prefix = (char *)malloc(Length + 1);
470   if (!Prefix) {
471     PROF_ERR("Failed to %s\n", "allocate memory.");
472     return "\0";
473   }
474   memcpy(Prefix, Filename, Length);
475   Prefix[Length] = '\0';
476   lprofCurFilename.ProfilePathPrefix = Prefix;
477   return Prefix;
478 }
479 
480 /* This method is invoked by the runtime initialization hook
481  * InstrProfilingRuntime.o if it is linked in. Both user specified
482  * profile path via -fprofile-instr-generate= and LLVM_PROFILE_FILE
483  * environment variable can override this default value. */
484 COMPILER_RT_VISIBILITY
485 void __llvm_profile_initialize_file(void) {
486   const char *EnvFilenamePat;
487   const char *SelectedPat = NULL;
488   ProfileNameSpecifier PNS = PNS_unknown;
489   int hasCommandLineOverrider = (INSTR_PROF_PROFILE_NAME_VAR[0] != 0);
490 
491   EnvFilenamePat = getFilenamePatFromEnv();
492   if (EnvFilenamePat) {
493     SelectedPat = EnvFilenamePat;
494     PNS = PNS_environment;
495   } else if (hasCommandLineOverrider) {
496     SelectedPat = INSTR_PROF_PROFILE_NAME_VAR;
497     PNS = PNS_command_line;
498   } else {
499     SelectedPat = NULL;
500     PNS = PNS_default;
501   }
502 
503   parseAndSetFilename(SelectedPat, PNS, 0);
504 }
505 
506 /* This API is directly called by the user application code. It has the
507  * highest precedence compared with LLVM_PROFILE_FILE environment variable
508  * and command line option -fprofile-instr-generate=<profile_name>.
509  */
510 COMPILER_RT_VISIBILITY
511 void __llvm_profile_set_filename(const char *FilenamePat) {
512   parseAndSetFilename(FilenamePat, PNS_runtime_api, 1);
513 }
514 
515 /* The public API for writing profile data into the file with name
516  * set by previous calls to __llvm_profile_set_filename or
517  * __llvm_profile_override_default_filename or
518  * __llvm_profile_initialize_file. */
519 COMPILER_RT_VISIBILITY
520 int __llvm_profile_write_file(void) {
521   int rc, Length;
522   const char *Filename;
523   char *FilenameBuf;
524 
525   if (lprofProfileDumped()) {
526     PROF_NOTE("Profile data not written to file: %s.\n",
527               "already written");
528     return 0;
529   }
530 
531   Length = getCurFilenameLength();
532   FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
533   Filename = getCurFilename(FilenameBuf);
534 
535   /* Check the filename. */
536   if (!Filename) {
537     PROF_ERR("Failed to write file : %s\n", "Filename not set");
538     return -1;
539   }
540 
541   /* Check if there is llvm/runtime version mismatch.  */
542   if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) {
543     PROF_ERR("Runtime and instrumentation version mismatch : "
544              "expected %d, but get %d\n",
545              INSTR_PROF_RAW_VERSION,
546              (int)GET_VERSION(__llvm_profile_get_version()));
547     return -1;
548   }
549 
550   /* Write profile data to the file. */
551   rc = writeFile(Filename);
552   if (rc)
553     PROF_ERR("Failed to write file \"%s\": %s\n", Filename, strerror(errno));
554   return rc;
555 }
556 
557 COMPILER_RT_VISIBILITY
558 int __llvm_profile_dump(void) {
559   if (!doMerging())
560     PROF_WARN("Later invocation of __llvm_profile_dump can lead to clobbering "
561               " of previously dumped profile data : %s. Either use %%m "
562               "in profile name or change profile name before dumping.\n",
563               "online profile merging is not on");
564   int rc = __llvm_profile_write_file();
565   lprofSetProfileDumped();
566   return rc;
567 }
568 
569 static void writeFileWithoutReturn(void) { __llvm_profile_write_file(); }
570 
571 COMPILER_RT_VISIBILITY
572 int __llvm_profile_register_write_file_atexit(void) {
573   static int HasBeenRegistered = 0;
574 
575   if (HasBeenRegistered)
576     return 0;
577 
578   lprofSetupValueProfiler();
579 
580   HasBeenRegistered = 1;
581   return atexit(writeFileWithoutReturn);
582 }
583