blob: ea795b6989e98d33765d09cd4958e738297df008 [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>
22
23static int SavedArgc = 0;
24static const char **SavedArgv = 0;
25
26/* save_arguments - Save argc and argv as passed into the program for the file
27 * we output.
28 */
29void save_arguments(int argc, const char **argv) {
30 if (SavedArgv) return; /* This can be called multiple times */
31
32 /* FIXME: this should copy the arguments out of argv into a string of our own,
33 * because the program might modify the arguments!
34 */
35 SavedArgc = argc;
36 SavedArgv = argv;
37}
38
39
40
41
42/* write_profiling_data - Write a raw block of profiling counters out to the
43 * llvmprof.out file. Note that we allow programs to be instrumented with
44 * multiple different kinds of instrumentation. For this reason, this function
45 * may be called more than once.
46 */
47void write_profiling_data(enum ProfilingType PT, unsigned *Start,
48 unsigned NumElements) {
49 static int OutFile = -1;
50 int PTy;
51
52 /* If this is the first time this function is called, open the output file for
53 * appending, creating it if it does not already exist.
54 */
55 if (OutFile == -1) {
56 off_t Offset;
57 OutFile = open("llvmprof.out", O_CREAT | O_WRONLY | O_APPEND, 0666);
58 if (OutFile == -1) {
59 perror("LLVM profiling: while opening 'llvmprof.out'");
60 return;
61 }
62
63 /* Output the command line arguments to the file. */
64 {
65 const char *Args = "";
66 int PTy = Arguments;
67 int ArgLength = strlen(Args);
68 int Zeros = 0;
69 write(OutFile, &PTy, sizeof(int));
70 write(OutFile, &ArgLength, sizeof(int));
71 write(OutFile, Args, ArgLength);
72 /* Pad out to a multiple of four bytes */
73 if (ArgLength & 3)
74 write(OutFile, &Zeros, 4-(ArgLength&3));
75 }
76 }
77
78 /* Write out this record! */
79 PTy = PT;
80 write(OutFile, &PTy, sizeof(int));
81 write(OutFile, Start, NumElements*sizeof(unsigned));
82}