Chris Lattner | a9a3028 | 2003-10-28 18:56:51 +0000 | [diff] [blame] | 1 | /*===-- CommonProfiling.c - Profiling support library support -------------===*\ |
| 2 | |* |
| 3 | |* The LLVM Compiler Infrastructure |
| 4 | |* |
| 5 | |* This file was developed by the LLVM research group and is distributed under |
| 6 | |* the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | |* |
| 8 | |*===----------------------------------------------------------------------===*| |
| 9 | |* |
| 10 | |* This file implements functions used by the various different types of |
| 11 | |* profiling implementations. |
| 12 | |* |
| 13 | \*===----------------------------------------------------------------------===*/ |
| 14 | |
| 15 | #include "Profiling.h" |
| 16 | #include <sys/types.h> |
| 17 | #include <sys/stat.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <stdio.h> |
| 20 | #include <string.h> |
| 21 | #include <unistd.h> |
Chris Lattner | d09bef4 | 2003-10-28 22:45:25 +0000 | [diff] [blame] | 22 | #include <stdlib.h> |
Chris Lattner | a9a3028 | 2003-10-28 18:56:51 +0000 | [diff] [blame] | 23 | |
Chris Lattner | d09bef4 | 2003-10-28 22:45:25 +0000 | [diff] [blame] | 24 | static char *SavedArgs = 0; |
| 25 | static unsigned SavedArgsLength = 0; |
Chris Lattner | a9a3028 | 2003-10-28 18:56:51 +0000 | [diff] [blame] | 26 | |
Chris Lattner | ebd02a8 | 2004-02-10 18:01:00 +0000 | [diff] [blame] | 27 | static const char *OutputFilename = "llvmprof.out"; |
| 28 | |
Chris Lattner | a9a3028 | 2003-10-28 18:56:51 +0000 | [diff] [blame] | 29 | /* save_arguments - Save argc and argv as passed into the program for the file |
| 30 | * we output. |
| 31 | */ |
Chris Lattner | affce4f | 2004-02-10 17:36:25 +0000 | [diff] [blame] | 32 | int save_arguments(int argc, const char **argv) { |
Chris Lattner | d09bef4 | 2003-10-28 22:45:25 +0000 | [diff] [blame] | 33 | unsigned Length, i; |
Chris Lattner | affce4f | 2004-02-10 17:36:25 +0000 | [diff] [blame] | 34 | if (SavedArgs || !argv) return argc; /* This can be called multiple times */ |
Chris Lattner | a9a3028 | 2003-10-28 18:56:51 +0000 | [diff] [blame] | 35 | |
Chris Lattner | ebd02a8 | 2004-02-10 18:01:00 +0000 | [diff] [blame] | 36 | /* Check to see if there are any arguments passed into the program for the |
| 37 | * profiler. If there are, strip them off and remember their settings. |
| 38 | */ |
| 39 | while (argc > 1 && !strncmp(argv[1], "-llvmprof-", 10)) { |
| 40 | /* Ok, we have an llvmprof argument. Remove it from the arg list and decide |
| 41 | * what to do with it. |
| 42 | */ |
| 43 | const char *Arg = argv[1]; |
Chris Lattner | f472935 | 2004-02-10 19:14:44 +0000 | [diff] [blame] | 44 | memmove(&argv[1], &argv[2], (argc-1)*sizeof(char*)); |
Chris Lattner | ebd02a8 | 2004-02-10 18:01:00 +0000 | [diff] [blame] | 45 | --argc; |
| 46 | |
| 47 | if (!strcmp(Arg, "-llvmprof-output")) { |
| 48 | if (argc == 1) |
| 49 | puts("-llvmprof-output requires a filename argument!"); |
| 50 | else { |
| 51 | OutputFilename = strdup(argv[1]); |
Chris Lattner | f472935 | 2004-02-10 19:14:44 +0000 | [diff] [blame] | 52 | memmove(&argv[1], &argv[2], (argc-1)*sizeof(char*)); |
Chris Lattner | ebd02a8 | 2004-02-10 18:01:00 +0000 | [diff] [blame] | 53 | --argc; |
| 54 | } |
| 55 | } else { |
| 56 | printf("Unknown option to the profiler runtime: '%s' - ignored.\n", Arg); |
| 57 | } |
| 58 | } |
| 59 | |
Chris Lattner | d09bef4 | 2003-10-28 22:45:25 +0000 | [diff] [blame] | 60 | for (Length = 0, i = 0; i != (unsigned)argc; ++i) |
| 61 | Length += strlen(argv[i])+1; |
| 62 | |
| 63 | SavedArgs = (char*)malloc(Length); |
| 64 | for (Length = 0, i = 0; i != (unsigned)argc; ++i) { |
| 65 | unsigned Len = strlen(argv[i]); |
| 66 | memcpy(SavedArgs+Length, argv[i], Len); |
| 67 | Length += Len; |
| 68 | SavedArgs[Length++] = ' '; |
| 69 | } |
| 70 | |
| 71 | SavedArgsLength = Length; |
Chris Lattner | affce4f | 2004-02-10 17:36:25 +0000 | [diff] [blame] | 72 | |
| 73 | return argc; |
Chris Lattner | a9a3028 | 2003-10-28 18:56:51 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | |
Chris Lattner | a9a3028 | 2003-10-28 18:56:51 +0000 | [diff] [blame] | 77 | /* write_profiling_data - Write a raw block of profiling counters out to the |
| 78 | * llvmprof.out file. Note that we allow programs to be instrumented with |
| 79 | * multiple different kinds of instrumentation. For this reason, this function |
| 80 | * may be called more than once. |
| 81 | */ |
| 82 | void write_profiling_data(enum ProfilingType PT, unsigned *Start, |
| 83 | unsigned NumElements) { |
| 84 | static int OutFile = -1; |
| 85 | int PTy; |
| 86 | |
| 87 | /* If this is the first time this function is called, open the output file for |
| 88 | * appending, creating it if it does not already exist. |
| 89 | */ |
| 90 | if (OutFile == -1) { |
| 91 | off_t Offset; |
Chris Lattner | ebd02a8 | 2004-02-10 18:01:00 +0000 | [diff] [blame] | 92 | OutFile = open(OutputFilename, O_CREAT | O_WRONLY | O_APPEND, 0666); |
Chris Lattner | a9a3028 | 2003-10-28 18:56:51 +0000 | [diff] [blame] | 93 | if (OutFile == -1) { |
Chris Lattner | ebd02a8 | 2004-02-10 18:01:00 +0000 | [diff] [blame] | 94 | fprintf(stderr, "LLVM profiling runtime: while opening '%s': ", |
| 95 | OutputFilename); |
| 96 | perror(""); |
Chris Lattner | a9a3028 | 2003-10-28 18:56:51 +0000 | [diff] [blame] | 97 | return; |
| 98 | } |
| 99 | |
| 100 | /* Output the command line arguments to the file. */ |
| 101 | { |
Brian Gaeke | 9fa49c8 | 2004-05-04 16:51:47 +0000 | [diff] [blame^] | 102 | int PTy = ArgumentInfo; |
Chris Lattner | a9a3028 | 2003-10-28 18:56:51 +0000 | [diff] [blame] | 103 | int Zeros = 0; |
| 104 | write(OutFile, &PTy, sizeof(int)); |
Chris Lattner | d09bef4 | 2003-10-28 22:45:25 +0000 | [diff] [blame] | 105 | write(OutFile, &SavedArgsLength, sizeof(unsigned)); |
| 106 | write(OutFile, SavedArgs, SavedArgsLength); |
Chris Lattner | a9a3028 | 2003-10-28 18:56:51 +0000 | [diff] [blame] | 107 | /* Pad out to a multiple of four bytes */ |
Chris Lattner | d09bef4 | 2003-10-28 22:45:25 +0000 | [diff] [blame] | 108 | if (SavedArgsLength & 3) |
| 109 | write(OutFile, &Zeros, 4-(SavedArgsLength&3)); |
Chris Lattner | a9a3028 | 2003-10-28 18:56:51 +0000 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | /* Write out this record! */ |
| 114 | PTy = PT; |
| 115 | write(OutFile, &PTy, sizeof(int)); |
Chris Lattner | 98b1d81 | 2003-10-28 19:35:56 +0000 | [diff] [blame] | 116 | write(OutFile, &NumElements, sizeof(unsigned)); |
Chris Lattner | a9a3028 | 2003-10-28 18:56:51 +0000 | [diff] [blame] | 117 | write(OutFile, Start, NumElements*sizeof(unsigned)); |
| 118 | } |