1 /*===-- CommonProfiling.c - Profiling support library support -------------===*\
3 |* The LLVM Compiler Infrastructure
5 |* This file is distributed under the University of Illinois Open Source
6 |* 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"
17 #include <sys/types.h>
22 #if !defined(_MSC_VER) && !defined(__MINGW32__)
29 static char *SavedArgs = 0;
30 static unsigned SavedArgsLength = 0;
31 static const char *SavedEnvVar = 0;
33 static const char *OutputFilename = "llvmprof.out";
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.
38 static void check_environment_variable(void) {
40 if (SavedEnvVar) return; /* Guarantee that we can't leak memory. */
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.
46 SavedEnvVar = strdup(EnvVar);
47 OutputFilename = SavedEnvVar;
51 /* save_arguments - Save argc and argv as passed into the program for the file
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.
57 int save_arguments(int argc, const char **argv) {
59 if (!SavedEnvVar && !SavedArgs) check_environment_variable();
60 if (SavedArgs || !argv) return argc; /* This can be called multiple times */
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.
65 while (argc > 1 && !strncmp(argv[1], "-llvmprof-", 10)) {
66 /* Ok, we have an llvmprof argument. Remove it from the arg list and decide
69 const char *Arg = argv[1];
70 memmove((char**)&argv[1], &argv[2], (argc-1)*sizeof(char*));
73 if (!strcmp(Arg, "-llvmprof-output")) {
75 puts("-llvmprof-output requires a filename argument!");
77 OutputFilename = strdup(argv[1]);
78 if (SavedEnvVar) { free((void *)SavedEnvVar); SavedEnvVar = 0; }
79 memmove((char**)&argv[1], &argv[2], (argc-1)*sizeof(char*));
83 printf("Unknown option to the profiler runtime: '%s' - ignored.\n", Arg);
87 for (Length = 0, i = 0; i != (unsigned)argc; ++i)
88 Length += strlen(argv[i])+1;
90 /* Defensively check for a zero length, even though this is unlikely
91 * to happen in practice. This avoids calling malloc() below with a
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);
105 SavedArgs[Length++] = ' ';
108 SavedArgsLength = Length;
115 * Retrieves the file descriptor for the profile file.
118 static int OutFile = -1;
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.
124 OutFile = open(OutputFilename, O_CREAT | O_WRONLY, 0666);
125 lseek(OutFile, 0, SEEK_END); /* O_APPEND prevents seeking */
127 fprintf(stderr, "LLVM profiling runtime: while opening '%s': ",
133 /* Output the command line arguments to the file. */
135 int PTy = ArgumentInfo;
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.");
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.");
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.
160 void write_profiling_data(enum ProfilingType PT, unsigned *Start,
161 unsigned NumElements) {
163 int outFile = getOutFile();
165 /* Write out this record! */
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.");