Actually save and pass in argument information
[oota-llvm.git] / runtime / libprofile / CommonProfiling.c
1 /*===-- CommonProfiling.c - Profiling support library support -------------===*\
2 |*
3 |*                     The LLVM Compiler Infrastructure
4 |*
5 |* This file was developed by the LLVM research group and is distributed under
6 |* the University of Illinois Open Source 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 <sys/types.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <stdlib.h>
23
24 static char *SavedArgs = 0;
25 static unsigned SavedArgsLength = 0;
26
27 /* save_arguments - Save argc and argv as passed into the program for the file
28  * we output.
29  */
30 void save_arguments(int argc, const char **argv) {
31   unsigned Length, i;
32   if (SavedArgs || !argv) return;  /* This can be called multiple times */
33
34   for (Length = 0, i = 0; i != (unsigned)argc; ++i)
35     Length += strlen(argv[i])+1;
36
37   SavedArgs = (char*)malloc(Length);
38   for (Length = 0, i = 0; i != (unsigned)argc; ++i) {
39     unsigned Len = strlen(argv[i]);
40     memcpy(SavedArgs+Length, argv[i], Len);
41     Length += Len;
42     SavedArgs[Length++] = ' ';
43   }
44
45   SavedArgsLength = Length;
46 }
47
48
49 /* write_profiling_data - Write a raw block of profiling counters out to the
50  * llvmprof.out file.  Note that we allow programs to be instrumented with
51  * multiple different kinds of instrumentation.  For this reason, this function
52  * may be called more than once.
53  */
54 void write_profiling_data(enum ProfilingType PT, unsigned *Start,
55                           unsigned NumElements) {
56   static int OutFile = -1;
57   int PTy;
58   
59   /* If this is the first time this function is called, open the output file for
60    * appending, creating it if it does not already exist.
61    */
62   if (OutFile == -1) {
63     off_t Offset;
64     OutFile = open("llvmprof.out", O_CREAT | O_WRONLY | O_APPEND, 0666);
65     if (OutFile == -1) {
66       perror("LLVM profiling: while opening 'llvmprof.out'");
67       return;
68     }
69
70     /* Output the command line arguments to the file. */
71     {
72       int PTy = Arguments;
73       int Zeros = 0;
74       write(OutFile, &PTy, sizeof(int));
75       write(OutFile, &SavedArgsLength, sizeof(unsigned));
76       write(OutFile, SavedArgs, SavedArgsLength);
77       /* Pad out to a multiple of four bytes */
78       if (SavedArgsLength & 3)
79         write(OutFile, &Zeros, 4-(SavedArgsLength&3));
80     }
81   }
82  
83   /* Write out this record! */
84   PTy = PT;
85   write(OutFile, &PTy, sizeof(int));
86   write(OutFile, &NumElements, sizeof(unsigned));
87   write(OutFile, Start, NumElements*sizeof(unsigned));
88 }