blob: 13e3eb9cf7bdb953e7a7ca9fd5da976415a2ce52 [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;
31
32static const char *OutputFilename = "llvmprof.out";
33
34/* save_arguments - Save argc and argv as passed into the program for the file
35 * we output.
36 */
37int save_arguments(int argc, const char **argv) {
38 unsigned Length, i;
39 if (SavedArgs || !argv) return argc; /* This can be called multiple times */
40
41 /* Check to see if there are any arguments passed into the program for the
42 * profiler. If there are, strip them off and remember their settings.
43 */
44 while (argc > 1 && !strncmp(argv[1], "-llvmprof-", 10)) {
45 /* Ok, we have an llvmprof argument. Remove it from the arg list and decide
46 * what to do with it.
47 */
48 const char *Arg = argv[1];
Aaron Ballman17da6e72012-02-05 19:43:39 +000049 memmove((char**)&argv[1], &argv[2], (argc-1)*sizeof(char*));
Reid Spencer8b2e1412006-11-17 03:32:33 +000050 --argc;
51
52 if (!strcmp(Arg, "-llvmprof-output")) {
53 if (argc == 1)
54 puts("-llvmprof-output requires a filename argument!");
55 else {
56 OutputFilename = strdup(argv[1]);
Aaron Ballman17da6e72012-02-05 19:43:39 +000057 memmove((char**)&argv[1], &argv[2], (argc-1)*sizeof(char*));
Reid Spencer8b2e1412006-11-17 03:32:33 +000058 --argc;
59 }
60 } else {
61 printf("Unknown option to the profiler runtime: '%s' - ignored.\n", Arg);
62 }
63 }
64
65 for (Length = 0, i = 0; i != (unsigned)argc; ++i)
66 Length += strlen(argv[i])+1;
67
Ted Kremenek06e950e2012-04-26 20:54:27 +000068 // Defensively check for a zero length, even though this is unlikely
69 // to happen in practice. This avoids calling malloc() below with a
70 // size of 0.
71 if (Length == 0) {
72 SavedArgs = 0;
73 SavedArgsLength = 0;
74 return argc;
75 }
76
Reid Spencer8b2e1412006-11-17 03:32:33 +000077 SavedArgs = (char*)malloc(Length);
78 for (Length = 0, i = 0; i != (unsigned)argc; ++i) {
79 unsigned Len = strlen(argv[i]);
80 memcpy(SavedArgs+Length, argv[i], Len);
81 Length += Len;
82 SavedArgs[Length++] = ' ';
83 }
84
85 SavedArgsLength = Length;
86
87 return argc;
88}
89
90
Andrew Trick04317cc2011-01-29 01:09:53 +000091/*
92 * Retrieves the file descriptor for the profile file.
Reid Spencer8b2e1412006-11-17 03:32:33 +000093 */
Andrew Trick04317cc2011-01-29 01:09:53 +000094int getOutFile() {
Reid Spencer8b2e1412006-11-17 03:32:33 +000095 static int OutFile = -1;
Andrew Trick04317cc2011-01-29 01:09:53 +000096
97 /* If this is the first time this function is called, open the output file
98 * for appending, creating it if it does not already exist.
Reid Spencer8b2e1412006-11-17 03:32:33 +000099 */
100 if (OutFile == -1) {
Andrew Trick04317cc2011-01-29 01:09:53 +0000101 OutFile = open(OutputFilename, O_CREAT | O_WRONLY, 0666);
102 lseek(OutFile, 0, SEEK_END); /* O_APPEND prevents seeking */
Reid Spencer8b2e1412006-11-17 03:32:33 +0000103 if (OutFile == -1) {
104 fprintf(stderr, "LLVM profiling runtime: while opening '%s': ",
105 OutputFilename);
106 perror("");
Andrew Trick04317cc2011-01-29 01:09:53 +0000107 return(OutFile);
Reid Spencer8b2e1412006-11-17 03:32:33 +0000108 }
109
110 /* Output the command line arguments to the file. */
111 {
112 int PTy = ArgumentInfo;
113 int Zeros = 0;
Galina Kistanova6bbba3c2011-09-22 17:33:24 +0000114 if (write(OutFile, &PTy, sizeof(int)) < 0 ||
115 write(OutFile, &SavedArgsLength, sizeof(unsigned)) < 0 ||
116 write(OutFile, SavedArgs, SavedArgsLength) < 0 ) {
117 fprintf(stderr,"error: unable to write to output file.");
118 exit(0);
119 }
Reid Spencer8b2e1412006-11-17 03:32:33 +0000120 /* Pad out to a multiple of four bytes */
Galina Kistanova6bbba3c2011-09-22 17:33:24 +0000121 if (SavedArgsLength & 3) {
122 if (write(OutFile, &Zeros, 4-(SavedArgsLength&3)) < 0) {
123 fprintf(stderr,"error: unable to write to output file.");
124 exit(0);
125 }
126 }
Reid Spencer8b2e1412006-11-17 03:32:33 +0000127 }
128 }
Andrew Trick04317cc2011-01-29 01:09:53 +0000129 return(OutFile);
130}
131
132/* write_profiling_data - Write a raw block of profiling counters out to the
133 * llvmprof.out file. Note that we allow programs to be instrumented with
134 * multiple different kinds of instrumentation. For this reason, this function
135 * may be called more than once.
136 */
137void write_profiling_data(enum ProfilingType PT, unsigned *Start,
138 unsigned NumElements) {
139 int PTy;
140 int outFile = getOutFile();
141
Reid Spencer8b2e1412006-11-17 03:32:33 +0000142 /* Write out this record! */
143 PTy = PT;
Andrew Trick04317cc2011-01-29 01:09:53 +0000144 if( write(outFile, &PTy, sizeof(int)) < 0 ||
145 write(outFile, &NumElements, sizeof(unsigned)) < 0 ||
146 write(outFile, Start, NumElements*sizeof(unsigned)) < 0 ) {
147 fprintf(stderr,"error: unable to write to output file.");
148 exit(0);
149 }
Reid Spencer8b2e1412006-11-17 03:32:33 +0000150}