1 /*===-- CommonProfiling.c - Profiling support library support -------------===*\
3 |* The LLVM Compiler Infrastructure
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.
8 |*===----------------------------------------------------------------------===*|
10 |* This file implements functions used by the various different types of
11 |* profiling implementations.
13 \*===----------------------------------------------------------------------===*/
15 #include "Profiling.h"
16 #include <sys/types.h>
24 static char *SavedArgs = 0;
25 static unsigned SavedArgsLength = 0;
27 /* save_arguments - Save argc and argv as passed into the program for the file
30 void save_arguments(int argc, const char **argv) {
32 if (SavedArgs || !argv) return; /* This can be called multiple times */
34 for (Length = 0, i = 0; i != (unsigned)argc; ++i)
35 Length += strlen(argv[i])+1;
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);
42 SavedArgs[Length++] = ' ';
45 SavedArgsLength = Length;
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.
54 void write_profiling_data(enum ProfilingType PT, unsigned *Start,
55 unsigned NumElements) {
56 static int OutFile = -1;
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.
64 OutFile = open("llvmprof.out", O_CREAT | O_WRONLY | O_APPEND, 0666);
66 perror("LLVM profiling: while opening 'llvmprof.out'");
70 /* Output the command line arguments to the file. */
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));
83 /* Write out this record! */
85 write(OutFile, &PTy, sizeof(int));
86 write(OutFile, &NumElements, sizeof(unsigned));
87 write(OutFile, Start, NumElements*sizeof(unsigned));