blob: 1c1771c3063e1410bad28a16a5ccbf6918fcfcd8 [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>
22#include <unistd.h>
23#include <stdlib.h>
24
25static char *SavedArgs = 0;
26static unsigned SavedArgsLength = 0;
27
28static const char *OutputFilename = "llvmprof.out";
29
30/* save_arguments - Save argc and argv as passed into the program for the file
31 * we output.
32 */
33int save_arguments(int argc, const char **argv) {
34 unsigned Length, i;
35 if (SavedArgs || !argv) return argc; /* This can be called multiple times */
36
37 /* Check to see if there are any arguments passed into the program for the
38 * profiler. If there are, strip them off and remember their settings.
39 */
40 while (argc > 1 && !strncmp(argv[1], "-llvmprof-", 10)) {
41 /* Ok, we have an llvmprof argument. Remove it from the arg list and decide
42 * what to do with it.
43 */
44 const char *Arg = argv[1];
45 memmove(&argv[1], &argv[2], (argc-1)*sizeof(char*));
46 --argc;
47
48 if (!strcmp(Arg, "-llvmprof-output")) {
49 if (argc == 1)
50 puts("-llvmprof-output requires a filename argument!");
51 else {
52 OutputFilename = strdup(argv[1]);
53 memmove(&argv[1], &argv[2], (argc-1)*sizeof(char*));
54 --argc;
55 }
56 } else {
57 printf("Unknown option to the profiler runtime: '%s' - ignored.\n", Arg);
58 }
59 }
60
61 for (Length = 0, i = 0; i != (unsigned)argc; ++i)
62 Length += strlen(argv[i])+1;
63
64 SavedArgs = (char*)malloc(Length);
65 for (Length = 0, i = 0; i != (unsigned)argc; ++i) {
66 unsigned Len = strlen(argv[i]);
67 memcpy(SavedArgs+Length, argv[i], Len);
68 Length += Len;
69 SavedArgs[Length++] = ' ';
70 }
71
72 SavedArgsLength = Length;
73
74 return argc;
75}
76
77
Andrew Trick04317cc2011-01-29 01:09:53 +000078/*
79 * Retrieves the file descriptor for the profile file.
Reid Spencer8b2e1412006-11-17 03:32:33 +000080 */
Andrew Trick04317cc2011-01-29 01:09:53 +000081int getOutFile() {
Reid Spencer8b2e1412006-11-17 03:32:33 +000082 static int OutFile = -1;
Andrew Trick04317cc2011-01-29 01:09:53 +000083
84 /* If this is the first time this function is called, open the output file
85 * for appending, creating it if it does not already exist.
Reid Spencer8b2e1412006-11-17 03:32:33 +000086 */
87 if (OutFile == -1) {
Andrew Trick04317cc2011-01-29 01:09:53 +000088 OutFile = open(OutputFilename, O_CREAT | O_WRONLY, 0666);
89 lseek(OutFile, 0, SEEK_END); /* O_APPEND prevents seeking */
Reid Spencer8b2e1412006-11-17 03:32:33 +000090 if (OutFile == -1) {
91 fprintf(stderr, "LLVM profiling runtime: while opening '%s': ",
92 OutputFilename);
93 perror("");
Andrew Trick04317cc2011-01-29 01:09:53 +000094 return(OutFile);
Reid Spencer8b2e1412006-11-17 03:32:33 +000095 }
96
97 /* Output the command line arguments to the file. */
98 {
99 int PTy = ArgumentInfo;
100 int Zeros = 0;
101 write(OutFile, &PTy, sizeof(int));
102 write(OutFile, &SavedArgsLength, sizeof(unsigned));
103 write(OutFile, SavedArgs, SavedArgsLength);
104 /* Pad out to a multiple of four bytes */
105 if (SavedArgsLength & 3)
106 write(OutFile, &Zeros, 4-(SavedArgsLength&3));
107 }
108 }
Andrew Trick04317cc2011-01-29 01:09:53 +0000109 return(OutFile);
110}
111
112/* write_profiling_data - Write a raw block of profiling counters out to the
113 * llvmprof.out file. Note that we allow programs to be instrumented with
114 * multiple different kinds of instrumentation. For this reason, this function
115 * may be called more than once.
116 */
117void write_profiling_data(enum ProfilingType PT, unsigned *Start,
118 unsigned NumElements) {
119 int PTy;
120 int outFile = getOutFile();
121
Reid Spencer8b2e1412006-11-17 03:32:33 +0000122 /* Write out this record! */
123 PTy = PT;
Andrew Trick04317cc2011-01-29 01:09:53 +0000124 if( write(outFile, &PTy, sizeof(int)) < 0 ||
125 write(outFile, &NumElements, sizeof(unsigned)) < 0 ||
126 write(outFile, Start, NumElements*sizeof(unsigned)) < 0 ) {
127 fprintf(stderr,"error: unable to write to output file.");
128 exit(0);
129 }
Reid Spencer8b2e1412006-11-17 03:32:33 +0000130}