blob: 8f4119c2c67d65a41b5cafbd6ffd15efdc808699 [file] [log] [blame]
Reid Spencer8b2e1412006-11-17 03:32:33 +00001/*===-- CommonProfiling.c - Profiling support library support -------------===*\
2|*
3|* The LLVM Compiler Infrastructure
4|*
Andrew Trick04317cc2011-01-29 01:09:53 +00005|* This file is distributed under the University of Illinois Open Source
6|* License. See LICENSE.TXT for details.
7|*
Reid Spencer8b2e1412006-11-17 03:32:33 +00008|*===----------------------------------------------------------------------===*|
Andrew Trick04317cc2011-01-29 01:09:53 +00009|*
Reid Spencer8b2e1412006-11-17 03:32:33 +000010|* This file implements functions used by the various different types of
11|* profiling implementations.
12|*
13\*===----------------------------------------------------------------------===*/
14
15#include "Profiling.h"
Andrew Trick04317cc2011-01-29 01:09:53 +000016#include <assert.h>
Reid Spencer8b2e1412006-11-17 03:32:33 +000017#include <sys/types.h>
18#include <sys/stat.h>
19#include <fcntl.h>
20#include <stdio.h>
21#include <string.h>
Francois Pichetb89e6992011-04-29 08:56:07 +000022#if !defined(_MSC_VER) && !defined(__MINGW32__)
Reid Spencer8b2e1412006-11-17 03:32:33 +000023#include <unistd.h>
Francois Pichetb89e6992011-04-29 08:56:07 +000024#else
25#include <io.h>
26#endif
Reid Spencer8b2e1412006-11-17 03:32:33 +000027#include <stdlib.h>
28
29static char *SavedArgs = 0;
30static unsigned SavedArgsLength = 0;
Manman Ren9b25ff62012-11-02 01:10:15 +000031static const char *SavedEnvVar = 0;
Reid Spencer8b2e1412006-11-17 03:32:33 +000032
33static const char *OutputFilename = "llvmprof.out";
34
Manman Ren9b25ff62012-11-02 01:10:15 +000035/* 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 */
38static void check_environment_variable(void) {
NAKAMURA Takumi875618f2012-11-02 01:32:02 +000039 const char *EnvVar;
Manman Ren9b25ff62012-11-02 01:10:15 +000040 if (SavedEnvVar) return; /* Guarantee that we can't leak memory. */
41
NAKAMURA Takumi875618f2012-11-02 01:32:02 +000042 if ((EnvVar = getenv("LLVMPROF_OUTPUT")) != NULL) {
Manman Ren9b25ff62012-11-02 01:10:15 +000043 /* 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
Reid Spencer8b2e1412006-11-17 03:32:33 +000051/* save_arguments - Save argc and argv as passed into the program for the file
52 * we output.
Manman Ren9b25ff62012-11-02 01:10:15 +000053 * 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.
Reid Spencer8b2e1412006-11-17 03:32:33 +000056 */
57int save_arguments(int argc, const char **argv) {
58 unsigned Length, i;
Manman Ren9b25ff62012-11-02 01:10:15 +000059 if (!SavedEnvVar && !SavedArgs) check_environment_variable();
Reid Spencer8b2e1412006-11-17 03:32:33 +000060 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];
Aaron Ballman17da6e72012-02-05 19:43:39 +000070 memmove((char**)&argv[1], &argv[2], (argc-1)*sizeof(char*));
Reid Spencer8b2e1412006-11-17 03:32:33 +000071 --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]);
Manman Ren9b25ff62012-11-02 01:10:15 +000078 if (SavedEnvVar) { free((void *)SavedEnvVar); SavedEnvVar = 0; }
Aaron Ballman17da6e72012-02-05 19:43:39 +000079 memmove((char**)&argv[1], &argv[2], (argc-1)*sizeof(char*));
Reid Spencer8b2e1412006-11-17 03:32:33 +000080 --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
David Blaikieca63acd2012-04-27 19:30:29 +000090 /* 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 */
Ted Kremenek06e950e2012-04-26 20:54:27 +000094 if (Length == 0) {
95 SavedArgs = 0;
96 SavedArgsLength = 0;
97 return argc;
98 }
99
Reid Spencer8b2e1412006-11-17 03:32:33 +0000100 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
Andrew Trick04317cc2011-01-29 01:09:53 +0000114/*
115 * Retrieves the file descriptor for the profile file.
Reid Spencer8b2e1412006-11-17 03:32:33 +0000116 */
Andrew Trick04317cc2011-01-29 01:09:53 +0000117int getOutFile() {
Reid Spencer8b2e1412006-11-17 03:32:33 +0000118 static int OutFile = -1;
Andrew Trick04317cc2011-01-29 01:09:53 +0000119
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.
Reid Spencer8b2e1412006-11-17 03:32:33 +0000122 */
123 if (OutFile == -1) {
Andrew Trick04317cc2011-01-29 01:09:53 +0000124 OutFile = open(OutputFilename, O_CREAT | O_WRONLY, 0666);
125 lseek(OutFile, 0, SEEK_END); /* O_APPEND prevents seeking */
Reid Spencer8b2e1412006-11-17 03:32:33 +0000126 if (OutFile == -1) {
127 fprintf(stderr, "LLVM profiling runtime: while opening '%s': ",
128 OutputFilename);
129 perror("");
Andrew Trick04317cc2011-01-29 01:09:53 +0000130 return(OutFile);
Reid Spencer8b2e1412006-11-17 03:32:33 +0000131 }
132
133 /* Output the command line arguments to the file. */
134 {
135 int PTy = ArgumentInfo;
136 int Zeros = 0;
Galina Kistanova6bbba3c2011-09-22 17:33:24 +0000137 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 }
Reid Spencer8b2e1412006-11-17 03:32:33 +0000143 /* Pad out to a multiple of four bytes */
Galina Kistanova6bbba3c2011-09-22 17:33:24 +0000144 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 }
Reid Spencer8b2e1412006-11-17 03:32:33 +0000150 }
151 }
Andrew Trick04317cc2011-01-29 01:09:53 +0000152 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 */
160void write_profiling_data(enum ProfilingType PT, unsigned *Start,
161 unsigned NumElements) {
162 int PTy;
163 int outFile = getOutFile();
164
Reid Spencer8b2e1412006-11-17 03:32:33 +0000165 /* Write out this record! */
166 PTy = PT;
Andrew Trick04317cc2011-01-29 01:09:53 +0000167 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 }
Reid Spencer8b2e1412006-11-17 03:32:33 +0000173}