Move the smarts of AnalysisGroup registration into PassRegistry.
[oota-llvm.git] / runtime / libprofile / CommonProfiling.c
index ea795b6989e98d33765d09cd4958e738297df008..8b27a257697429d04acfba512bb23cd0454ffa04 100644 (file)
@@ -2,8 +2,8 @@
 |*
 |*                     The LLVM Compiler Infrastructure
 |*
-|* This file was developed by the LLVM research group and is distributed under
-|* the University of Illinois Open Source License. See LICENSE.TXT for details.
+|* This file is distributed under the University of Illinois Open Source      
+|* License. See LICENSE.TXT for details.                                      
 |* 
 |*===----------------------------------------------------------------------===*|
 |* 
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
+#include <stdlib.h>
 
-static int SavedArgc = 0;
-static const char **SavedArgv = 0;
+static char *SavedArgs = 0;
+static unsigned SavedArgsLength = 0;
+
+static const char *OutputFilename = "llvmprof.out";
 
 /* save_arguments - Save argc and argv as passed into the program for the file
  * we output.
  */
-void save_arguments(int argc, const char **argv) {
-  if (SavedArgv) return;  /* This can be called multiple times */
+int save_arguments(int argc, const char **argv) {
+  unsigned Length, i;
+  if (SavedArgs || !argv) return argc;  /* This can be called multiple times */
 
-  /* FIXME: this should copy the arguments out of argv into a string of our own,
-   * because the program might modify the arguments!
+  /* Check to see if there are any arguments passed into the program for the
+   * profiler.  If there are, strip them off and remember their settings.
    */
-  SavedArgc = argc;
-  SavedArgv = argv;
-}
+  while (argc > 1 && !strncmp(argv[1], "-llvmprof-", 10)) {
+    /* Ok, we have an llvmprof argument.  Remove it from the arg list and decide
+     * what to do with it.
+     */
+    const char *Arg = argv[1];
+    memmove(&argv[1], &argv[2], (argc-1)*sizeof(char*));
+    --argc;
+
+    if (!strcmp(Arg, "-llvmprof-output")) {
+      if (argc == 1)
+        puts("-llvmprof-output requires a filename argument!");
+      else {
+        OutputFilename = strdup(argv[1]);
+        memmove(&argv[1], &argv[2], (argc-1)*sizeof(char*));
+        --argc;
+      }
+    } else {
+      printf("Unknown option to the profiler runtime: '%s' - ignored.\n", Arg);
+    }
+  }
 
+  for (Length = 0, i = 0; i != (unsigned)argc; ++i)
+    Length += strlen(argv[i])+1;
 
+  SavedArgs = (char*)malloc(Length);
+  for (Length = 0, i = 0; i != (unsigned)argc; ++i) {
+    unsigned Len = strlen(argv[i]);
+    memcpy(SavedArgs+Length, argv[i], Len);
+    Length += Len;
+    SavedArgs[Length++] = ' ';
+  }
+
+  SavedArgsLength = Length;
+
+  return argc;
+}
 
 
 /* write_profiling_data - Write a raw block of profiling counters out to the
@@ -53,30 +88,30 @@ void write_profiling_data(enum ProfilingType PT, unsigned *Start,
    * appending, creating it if it does not already exist.
    */
   if (OutFile == -1) {
-    off_t Offset;
-    OutFile = open("llvmprof.out", O_CREAT | O_WRONLY | O_APPEND, 0666);
+    OutFile = open(OutputFilename, O_CREAT | O_WRONLY | O_APPEND, 0666);
     if (OutFile == -1) {
-      perror("LLVM profiling: while opening 'llvmprof.out'");
+      fprintf(stderr, "LLVM profiling runtime: while opening '%s': ",
+              OutputFilename);
+      perror("");
       return;
     }
 
     /* Output the command line arguments to the file. */
     {
-      const char *Args = "";
-      int PTy = Arguments;
-      int ArgLength = strlen(Args);
+      int PTy = ArgumentInfo;
       int Zeros = 0;
       write(OutFile, &PTy, sizeof(int));
-      write(OutFile, &ArgLength, sizeof(int));
-      write(OutFile, Args, ArgLength);
+      write(OutFile, &SavedArgsLength, sizeof(unsigned));
+      write(OutFile, SavedArgs, SavedArgsLength);
       /* Pad out to a multiple of four bytes */
-      if (ArgLength & 3)
-        write(OutFile, &Zeros, 4-(ArgLength&3));
+      if (SavedArgsLength & 3)
+        write(OutFile, &Zeros, 4-(SavedArgsLength&3));
     }
   }
  
   /* Write out this record! */
   PTy = PT;
   write(OutFile, &PTy, sizeof(int));
+  write(OutFile, &NumElements, sizeof(unsigned));
   write(OutFile, Start, NumElements*sizeof(unsigned));
 }