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 | |
| 27 | /* save_arguments - Save argc and argv as passed into the program for the file |
| 28 | * we output. |
| 29 | */ |
| 30 | void save_arguments(int argc, const char **argv) { |
Chris Lattner | d09bef4 | 2003-10-28 22:45:25 +0000 | [diff] [blame^] | 31 | unsigned Length, i; |
| 32 | if (SavedArgs || !argv) return; /* This can be called multiple times */ |
Chris Lattner | a9a3028 | 2003-10-28 18:56:51 +0000 | [diff] [blame] | 33 | |
Chris Lattner | d09bef4 | 2003-10-28 22:45:25 +0000 | [diff] [blame^] | 34 | for (Length = 0, i = 0; i != (unsigned)argc; ++i) |
| 35 | Length += strlen(argv[i])+1; |
| 36 | |
| 37 | SavedArgs = (char*)malloc(Length); |
| 38 | for (Length = 0, i = 0; i != (unsigned)argc; ++i) { |
| 39 | unsigned Len = strlen(argv[i]); |
| 40 | memcpy(SavedArgs+Length, argv[i], Len); |
| 41 | Length += Len; |
| 42 | SavedArgs[Length++] = ' '; |
| 43 | } |
| 44 | |
| 45 | SavedArgsLength = Length; |
Chris Lattner | a9a3028 | 2003-10-28 18:56:51 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | |
Chris Lattner | a9a3028 | 2003-10-28 18:56:51 +0000 | [diff] [blame] | 49 | /* write_profiling_data - Write a raw block of profiling counters out to the |
| 50 | * llvmprof.out file. Note that we allow programs to be instrumented with |
| 51 | * multiple different kinds of instrumentation. For this reason, this function |
| 52 | * may be called more than once. |
| 53 | */ |
| 54 | void write_profiling_data(enum ProfilingType PT, unsigned *Start, |
| 55 | unsigned NumElements) { |
| 56 | static int OutFile = -1; |
| 57 | int PTy; |
| 58 | |
| 59 | /* If this is the first time this function is called, open the output file for |
| 60 | * appending, creating it if it does not already exist. |
| 61 | */ |
| 62 | if (OutFile == -1) { |
| 63 | off_t Offset; |
| 64 | OutFile = open("llvmprof.out", O_CREAT | O_WRONLY | O_APPEND, 0666); |
| 65 | if (OutFile == -1) { |
| 66 | perror("LLVM profiling: while opening 'llvmprof.out'"); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | /* Output the command line arguments to the file. */ |
| 71 | { |
Chris Lattner | a9a3028 | 2003-10-28 18:56:51 +0000 | [diff] [blame] | 72 | int PTy = Arguments; |
Chris Lattner | a9a3028 | 2003-10-28 18:56:51 +0000 | [diff] [blame] | 73 | int Zeros = 0; |
| 74 | write(OutFile, &PTy, sizeof(int)); |
Chris Lattner | d09bef4 | 2003-10-28 22:45:25 +0000 | [diff] [blame^] | 75 | write(OutFile, &SavedArgsLength, sizeof(unsigned)); |
| 76 | write(OutFile, SavedArgs, SavedArgsLength); |
Chris Lattner | a9a3028 | 2003-10-28 18:56:51 +0000 | [diff] [blame] | 77 | /* Pad out to a multiple of four bytes */ |
Chris Lattner | d09bef4 | 2003-10-28 22:45:25 +0000 | [diff] [blame^] | 78 | if (SavedArgsLength & 3) |
| 79 | write(OutFile, &Zeros, 4-(SavedArgsLength&3)); |
Chris Lattner | a9a3028 | 2003-10-28 18:56:51 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | /* Write out this record! */ |
| 84 | PTy = PT; |
| 85 | write(OutFile, &PTy, sizeof(int)); |
Chris Lattner | 98b1d81 | 2003-10-28 19:35:56 +0000 | [diff] [blame] | 86 | write(OutFile, &NumElements, sizeof(unsigned)); |
Chris Lattner | a9a3028 | 2003-10-28 18:56:51 +0000 | [diff] [blame] | 87 | write(OutFile, Start, NumElements*sizeof(unsigned)); |
| 88 | } |