Home | History | Annotate | Download | only in libprofile
      1 /*===-- CommonProfiling.c - Profiling support library support -------------===*\
      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 |* This file implements functions used by the various different types of
     11 |* profiling implementations.
     12 |*
     13 \*===----------------------------------------------------------------------===*/
     14 
     15 #include "Profiling.h"
     16 #include <assert.h>
     17 #include <sys/types.h>
     18 #include <sys/stat.h>
     19 #include <fcntl.h>
     20 #include <stdio.h>
     21 #include <string.h>
     22 #if !defined(_MSC_VER) && !defined(__MINGW32__)
     23 #include <unistd.h>
     24 #else
     25 #include <io.h>
     26 #endif
     27 #include <stdlib.h>
     28 
     29 static char *SavedArgs = 0;
     30 static unsigned SavedArgsLength = 0;
     31 static const char *SavedEnvVar = 0;
     32 
     33 static const char *OutputFilename = "llvmprof.out";
     34 
     35 /* check_environment_variable - Check to see if the LLVMPROF_OUTPUT environment
     36  * variable is set.  If it is then save it and set OutputFilename.
     37  */
     38 static void check_environment_variable(void) {
     39   const char *EnvVar;
     40   if (SavedEnvVar) return; /* Guarantee that we can't leak memory. */
     41 
     42   if ((EnvVar = getenv("LLVMPROF_OUTPUT")) != NULL) {
     43     /* The string that getenv returns is allowed to be statically allocated,
     44      * which means it may be changed by future calls to getenv, so copy it.
     45      */
     46     SavedEnvVar = strdup(EnvVar);
     47     OutputFilename = SavedEnvVar;
     48   }
     49 }
     50 
     51 /* save_arguments - Save argc and argv as passed into the program for the file
     52  * we output.
     53  * If either the LLVMPROF_OUTPUT environment variable or the -llvmprof-output
     54  * command line argument are set then change OutputFilename to the provided
     55  * value.  The command line argument value overrides the environment variable.
     56  */
     57 int save_arguments(int argc, const char **argv) {
     58   unsigned Length, i;
     59   if (!SavedEnvVar && !SavedArgs) check_environment_variable();
     60   if (SavedArgs || !argv) return argc;  /* This can be called multiple times */
     61 
     62   /* Check to see if there are any arguments passed into the program for the
     63    * profiler.  If there are, strip them off and remember their settings.
     64    */
     65   while (argc > 1 && !strncmp(argv[1], "-llvmprof-", 10)) {
     66     /* Ok, we have an llvmprof argument.  Remove it from the arg list and decide
     67      * what to do with it.
     68      */
     69     const char *Arg = argv[1];
     70     memmove((char**)&argv[1], &argv[2], (argc-1)*sizeof(char*));
     71     --argc;
     72 
     73     if (!strcmp(Arg, "-llvmprof-output")) {
     74       if (argc == 1)
     75         puts("-llvmprof-output requires a filename argument!");
     76       else {
     77         OutputFilename = strdup(argv[1]);
     78         if (SavedEnvVar) { free((void *)SavedEnvVar); SavedEnvVar = 0; }
     79         memmove((char**)&argv[1], &argv[2], (argc-1)*sizeof(char*));
     80         --argc;
     81       }
     82     } else {
     83       printf("Unknown option to the profiler runtime: '%s' - ignored.\n", Arg);
     84     }
     85   }
     86 
     87   for (Length = 0, i = 0; i != (unsigned)argc; ++i)
     88     Length += strlen(argv[i])+1;
     89 
     90   /* Defensively check for a zero length, even though this is unlikely
     91    * to happen in practice.  This avoids calling malloc() below with a
     92    * size of 0.
     93    */
     94   if (Length == 0) {
     95     SavedArgs = 0;
     96     SavedArgsLength = 0;
     97     return argc;
     98   }
     99 
    100   SavedArgs = (char*)malloc(Length);
    101   for (Length = 0, i = 0; i != (unsigned)argc; ++i) {
    102     unsigned Len = strlen(argv[i]);
    103     memcpy(SavedArgs+Length, argv[i], Len);
    104     Length += Len;
    105     SavedArgs[Length++] = ' ';
    106   }
    107 
    108   SavedArgsLength = Length;
    109 
    110   return argc;
    111 }
    112 
    113 
    114 /*
    115  * Retrieves the file descriptor for the profile file.
    116  */
    117 int getOutFile() {
    118   static int OutFile = -1;
    119 
    120   /* If this is the first time this function is called, open the output file
    121    * for appending, creating it if it does not already exist.
    122    */
    123   if (OutFile == -1) {
    124     OutFile = open(OutputFilename, O_CREAT | O_WRONLY, 0666);
    125     lseek(OutFile, 0, SEEK_END); /* O_APPEND prevents seeking */
    126     if (OutFile == -1) {
    127       fprintf(stderr, "LLVM profiling runtime: while opening '%s': ",
    128               OutputFilename);
    129       perror("");
    130       return(OutFile);
    131     }
    132 
    133     /* Output the command line arguments to the file. */
    134     {
    135       int PTy = ArgumentInfo;
    136       int Zeros = 0;
    137       if (write(OutFile, &PTy, sizeof(int)) < 0 ||
    138           write(OutFile, &SavedArgsLength, sizeof(unsigned)) < 0 ||
    139           write(OutFile, SavedArgs, SavedArgsLength) < 0 ) {
    140         fprintf(stderr,"error: unable to write to output file.");
    141         exit(0);
    142       }
    143       /* Pad out to a multiple of four bytes */
    144       if (SavedArgsLength & 3) {
    145         if (write(OutFile, &Zeros, 4-(SavedArgsLength&3)) < 0) {
    146           fprintf(stderr,"error: unable to write to output file.");
    147           exit(0);
    148         }
    149       }
    150     }
    151   }
    152   return(OutFile);
    153 }
    154 
    155 /* write_profiling_data - Write a raw block of profiling counters out to the
    156  * llvmprof.out file.  Note that we allow programs to be instrumented with
    157  * multiple different kinds of instrumentation.  For this reason, this function
    158  * may be called more than once.
    159  */
    160 void write_profiling_data(enum ProfilingType PT, unsigned *Start,
    161                           unsigned NumElements) {
    162   int PTy;
    163   int outFile = getOutFile();
    164 
    165   /* Write out this record! */
    166   PTy = PT;
    167   if( write(outFile, &PTy, sizeof(int)) < 0 ||
    168       write(outFile, &NumElements, sizeof(unsigned)) < 0 ||
    169       write(outFile, Start, NumElements*sizeof(unsigned)) < 0 ) {
    170     fprintf(stderr,"error: unable to write to output file.");
    171     exit(0);
    172   }
    173 }
    174