blob: 5569a66414f4315490e90fc47c32510564dfa27d [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 */
Chris Lattneraffce4f2004-02-10 17:36:25 +000030int save_arguments(int argc, const char **argv) {
Chris Lattnerd09bef42003-10-28 22:45:25 +000031 unsigned Length, i;
Chris Lattneraffce4f2004-02-10 17:36:25 +000032 if (SavedArgs || !argv) return argc; /* 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 Lattneraffce4f2004-02-10 17:36:25 +000046
47 return argc;
Chris Lattnera9a30282003-10-28 18:56:51 +000048}
49
50
Chris Lattnera9a30282003-10-28 18:56:51 +000051/* write_profiling_data - Write a raw block of profiling counters out to the
52 * llvmprof.out file. Note that we allow programs to be instrumented with
53 * multiple different kinds of instrumentation. For this reason, this function
54 * may be called more than once.
55 */
56void write_profiling_data(enum ProfilingType PT, unsigned *Start,
57 unsigned NumElements) {
58 static int OutFile = -1;
59 int PTy;
60
61 /* If this is the first time this function is called, open the output file for
62 * appending, creating it if it does not already exist.
63 */
64 if (OutFile == -1) {
65 off_t Offset;
66 OutFile = open("llvmprof.out", O_CREAT | O_WRONLY | O_APPEND, 0666);
67 if (OutFile == -1) {
68 perror("LLVM profiling: while opening 'llvmprof.out'");
69 return;
70 }
71
72 /* Output the command line arguments to the file. */
73 {
Chris Lattnera9a30282003-10-28 18:56:51 +000074 int PTy = Arguments;
Chris Lattnera9a30282003-10-28 18:56:51 +000075 int Zeros = 0;
76 write(OutFile, &PTy, sizeof(int));
Chris Lattnerd09bef42003-10-28 22:45:25 +000077 write(OutFile, &SavedArgsLength, sizeof(unsigned));
78 write(OutFile, SavedArgs, SavedArgsLength);
Chris Lattnera9a30282003-10-28 18:56:51 +000079 /* Pad out to a multiple of four bytes */
Chris Lattnerd09bef42003-10-28 22:45:25 +000080 if (SavedArgsLength & 3)
81 write(OutFile, &Zeros, 4-(SavedArgsLength&3));
Chris Lattnera9a30282003-10-28 18:56:51 +000082 }
83 }
84
85 /* Write out this record! */
86 PTy = PT;
87 write(OutFile, &PTy, sizeof(int));
Chris Lattner98b1d812003-10-28 19:35:56 +000088 write(OutFile, &NumElements, sizeof(unsigned));
Chris Lattnera9a30282003-10-28 18:56:51 +000089 write(OutFile, Start, NumElements*sizeof(unsigned));
90}