blob: b88ea9cf8b0ede0b76500ab4340779caa882c6c0 [file] [log] [blame]
Chris Lattnera9a30282003-10-28 18:56:51 +00001/*===-- 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 Lattnerd09bef42003-10-28 22:45:25 +000022#include <stdlib.h>
Chris Lattnera9a30282003-10-28 18:56:51 +000023
Chris Lattnerd09bef42003-10-28 22:45:25 +000024static char *SavedArgs = 0;
25static unsigned SavedArgsLength = 0;
Chris Lattnera9a30282003-10-28 18:56:51 +000026
27/* save_arguments - Save argc and argv as passed into the program for the file
28 * we output.
29 */
30void save_arguments(int argc, const char **argv) {
Chris Lattnerd09bef42003-10-28 22:45:25 +000031 unsigned Length, i;
32 if (SavedArgs || !argv) return; /* This can be called multiple times */
Chris Lattnera9a30282003-10-28 18:56:51 +000033
Chris Lattnerd09bef42003-10-28 22:45:25 +000034 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 Lattnera9a30282003-10-28 18:56:51 +000046}
47
48
Chris Lattnera9a30282003-10-28 18:56:51 +000049/* 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 */
54void 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 Lattnera9a30282003-10-28 18:56:51 +000072 int PTy = Arguments;
Chris Lattnera9a30282003-10-28 18:56:51 +000073 int Zeros = 0;
74 write(OutFile, &PTy, sizeof(int));
Chris Lattnerd09bef42003-10-28 22:45:25 +000075 write(OutFile, &SavedArgsLength, sizeof(unsigned));
76 write(OutFile, SavedArgs, SavedArgsLength);
Chris Lattnera9a30282003-10-28 18:56:51 +000077 /* Pad out to a multiple of four bytes */
Chris Lattnerd09bef42003-10-28 22:45:25 +000078 if (SavedArgsLength & 3)
79 write(OutFile, &Zeros, 4-(SavedArgsLength&3));
Chris Lattnera9a30282003-10-28 18:56:51 +000080 }
81 }
82
83 /* Write out this record! */
84 PTy = PT;
85 write(OutFile, &PTy, sizeof(int));
Chris Lattner98b1d812003-10-28 19:35:56 +000086 write(OutFile, &NumElements, sizeof(unsigned));
Chris Lattnera9a30282003-10-28 18:56:51 +000087 write(OutFile, Start, NumElements*sizeof(unsigned));
88}