Reid Spencer | 8b2e141 | 2006-11-17 03:32:33 +0000 | [diff] [blame] | 1 | /*===-- CommonProfiling.c - Profiling support library support -------------===*\ |
| 2 | |* |
| 3 | |* The LLVM Compiler Infrastructure |
| 4 | |* |
Andrew Trick | 04317cc | 2011-01-29 01:09:53 +0000 | [diff] [blame] | 5 | |* This file is distributed under the University of Illinois Open Source |
| 6 | |* License. See LICENSE.TXT for details. |
| 7 | |* |
Reid Spencer | 8b2e141 | 2006-11-17 03:32:33 +0000 | [diff] [blame] | 8 | |*===----------------------------------------------------------------------===*| |
Andrew Trick | 04317cc | 2011-01-29 01:09:53 +0000 | [diff] [blame] | 9 | |* |
Reid Spencer | 8b2e141 | 2006-11-17 03:32:33 +0000 | [diff] [blame] | 10 | |* This file implements functions used by the various different types of |
| 11 | |* profiling implementations. |
| 12 | |* |
| 13 | \*===----------------------------------------------------------------------===*/ |
| 14 | |
| 15 | #include "Profiling.h" |
Andrew Trick | 04317cc | 2011-01-29 01:09:53 +0000 | [diff] [blame] | 16 | #include <assert.h> |
Reid Spencer | 8b2e141 | 2006-11-17 03:32:33 +0000 | [diff] [blame] | 17 | #include <sys/types.h> |
| 18 | #include <sys/stat.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
Francois Pichet | b89e699 | 2011-04-29 08:56:07 +0000 | [diff] [blame] | 22 | #if !defined(_MSC_VER) && !defined(__MINGW32__) |
Reid Spencer | 8b2e141 | 2006-11-17 03:32:33 +0000 | [diff] [blame] | 23 | #include <unistd.h> |
Francois Pichet | b89e699 | 2011-04-29 08:56:07 +0000 | [diff] [blame] | 24 | #else |
| 25 | #include <io.h> |
| 26 | #endif |
Reid Spencer | 8b2e141 | 2006-11-17 03:32:33 +0000 | [diff] [blame] | 27 | #include <stdlib.h> |
| 28 | |
| 29 | static char *SavedArgs = 0; |
| 30 | static unsigned SavedArgsLength = 0; |
| 31 | |
| 32 | static 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 | */ |
| 37 | int 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 Ballman | 17da6e7 | 2012-02-05 19:43:39 +0000 | [diff] [blame] | 49 | memmove((char**)&argv[1], &argv[2], (argc-1)*sizeof(char*)); |
Reid Spencer | 8b2e141 | 2006-11-17 03:32:33 +0000 | [diff] [blame] | 50 | --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 Ballman | 17da6e7 | 2012-02-05 19:43:39 +0000 | [diff] [blame] | 57 | memmove((char**)&argv[1], &argv[2], (argc-1)*sizeof(char*)); |
Reid Spencer | 8b2e141 | 2006-11-17 03:32:33 +0000 | [diff] [blame] | 58 | --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 | |
David Blaikie | ca63acd | 2012-04-27 19:30:29 +0000 | [diff] [blame] | 68 | /* 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 | */ |
Ted Kremenek | 06e950e | 2012-04-26 20:54:27 +0000 | [diff] [blame] | 72 | if (Length == 0) { |
| 73 | SavedArgs = 0; |
| 74 | SavedArgsLength = 0; |
| 75 | return argc; |
| 76 | } |
| 77 | |
Reid Spencer | 8b2e141 | 2006-11-17 03:32:33 +0000 | [diff] [blame] | 78 | SavedArgs = (char*)malloc(Length); |
| 79 | for (Length = 0, i = 0; i != (unsigned)argc; ++i) { |
| 80 | unsigned Len = strlen(argv[i]); |
| 81 | memcpy(SavedArgs+Length, argv[i], Len); |
| 82 | Length += Len; |
| 83 | SavedArgs[Length++] = ' '; |
| 84 | } |
| 85 | |
| 86 | SavedArgsLength = Length; |
| 87 | |
| 88 | return argc; |
| 89 | } |
| 90 | |
| 91 | |
Andrew Trick | 04317cc | 2011-01-29 01:09:53 +0000 | [diff] [blame] | 92 | /* |
| 93 | * Retrieves the file descriptor for the profile file. |
Reid Spencer | 8b2e141 | 2006-11-17 03:32:33 +0000 | [diff] [blame] | 94 | */ |
Andrew Trick | 04317cc | 2011-01-29 01:09:53 +0000 | [diff] [blame] | 95 | int getOutFile() { |
Reid Spencer | 8b2e141 | 2006-11-17 03:32:33 +0000 | [diff] [blame] | 96 | static int OutFile = -1; |
Andrew Trick | 04317cc | 2011-01-29 01:09:53 +0000 | [diff] [blame] | 97 | |
| 98 | /* If this is the first time this function is called, open the output file |
| 99 | * for appending, creating it if it does not already exist. |
Reid Spencer | 8b2e141 | 2006-11-17 03:32:33 +0000 | [diff] [blame] | 100 | */ |
| 101 | if (OutFile == -1) { |
Andrew Trick | 04317cc | 2011-01-29 01:09:53 +0000 | [diff] [blame] | 102 | OutFile = open(OutputFilename, O_CREAT | O_WRONLY, 0666); |
| 103 | lseek(OutFile, 0, SEEK_END); /* O_APPEND prevents seeking */ |
Reid Spencer | 8b2e141 | 2006-11-17 03:32:33 +0000 | [diff] [blame] | 104 | if (OutFile == -1) { |
| 105 | fprintf(stderr, "LLVM profiling runtime: while opening '%s': ", |
| 106 | OutputFilename); |
| 107 | perror(""); |
Andrew Trick | 04317cc | 2011-01-29 01:09:53 +0000 | [diff] [blame] | 108 | return(OutFile); |
Reid Spencer | 8b2e141 | 2006-11-17 03:32:33 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /* Output the command line arguments to the file. */ |
| 112 | { |
| 113 | int PTy = ArgumentInfo; |
| 114 | int Zeros = 0; |
Galina Kistanova | 6bbba3c | 2011-09-22 17:33:24 +0000 | [diff] [blame] | 115 | if (write(OutFile, &PTy, sizeof(int)) < 0 || |
| 116 | write(OutFile, &SavedArgsLength, sizeof(unsigned)) < 0 || |
| 117 | write(OutFile, SavedArgs, SavedArgsLength) < 0 ) { |
| 118 | fprintf(stderr,"error: unable to write to output file."); |
| 119 | exit(0); |
| 120 | } |
Reid Spencer | 8b2e141 | 2006-11-17 03:32:33 +0000 | [diff] [blame] | 121 | /* Pad out to a multiple of four bytes */ |
Galina Kistanova | 6bbba3c | 2011-09-22 17:33:24 +0000 | [diff] [blame] | 122 | if (SavedArgsLength & 3) { |
| 123 | if (write(OutFile, &Zeros, 4-(SavedArgsLength&3)) < 0) { |
| 124 | fprintf(stderr,"error: unable to write to output file."); |
| 125 | exit(0); |
| 126 | } |
| 127 | } |
Reid Spencer | 8b2e141 | 2006-11-17 03:32:33 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
Andrew Trick | 04317cc | 2011-01-29 01:09:53 +0000 | [diff] [blame] | 130 | return(OutFile); |
| 131 | } |
| 132 | |
| 133 | /* write_profiling_data - Write a raw block of profiling counters out to the |
| 134 | * llvmprof.out file. Note that we allow programs to be instrumented with |
| 135 | * multiple different kinds of instrumentation. For this reason, this function |
| 136 | * may be called more than once. |
| 137 | */ |
| 138 | void write_profiling_data(enum ProfilingType PT, unsigned *Start, |
| 139 | unsigned NumElements) { |
| 140 | int PTy; |
| 141 | int outFile = getOutFile(); |
| 142 | |
Reid Spencer | 8b2e141 | 2006-11-17 03:32:33 +0000 | [diff] [blame] | 143 | /* Write out this record! */ |
| 144 | PTy = PT; |
Andrew Trick | 04317cc | 2011-01-29 01:09:53 +0000 | [diff] [blame] | 145 | if( write(outFile, &PTy, sizeof(int)) < 0 || |
| 146 | write(outFile, &NumElements, sizeof(unsigned)) < 0 || |
| 147 | write(outFile, Start, NumElements*sizeof(unsigned)) < 0 ) { |
| 148 | fprintf(stderr,"error: unable to write to output file."); |
| 149 | exit(0); |
| 150 | } |
Reid Spencer | 8b2e141 | 2006-11-17 03:32:33 +0000 | [diff] [blame] | 151 | } |