1 /*===-- CommonProfiling.c - Profiling support library support -------------===*\
3 |* The LLVM Compiler Infrastructure
5 |* This file is distributed under the University of Illinois Open Source
6 |* License. See LICENSE.TXT for details.
8 |*===----------------------------------------------------------------------===*|
10 |* This file implements functions used by the various different types of
11 |* profiling implementations.
13 \*===----------------------------------------------------------------------===*/
15 #include "Profiling.h"
16 #include <sys/types.h>
24 static char *SavedArgs = 0;
25 static unsigned SavedArgsLength = 0;
27 static const char *OutputFilename = "llvmprof.out";
29 /* save_arguments - Save argc and argv as passed into the program for the file
32 int save_arguments(int argc, const char **argv) {
34 if (SavedArgs || !argv) return argc; /* This can be called multiple times */
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.
39 while (argc > 1 && !strncmp(argv[1], "-llvmprof-", 10)) {
40 /* Ok, we have an llvmprof argument. Remove it from the arg list and decide
43 const char *Arg = argv[1];
44 memmove(&argv[1], &argv[2], (argc-1)*sizeof(char*));
47 if (!strcmp(Arg, "-llvmprof-output")) {
49 puts("-llvmprof-output requires a filename argument!");
51 OutputFilename = strdup(argv[1]);
52 memmove(&argv[1], &argv[2], (argc-1)*sizeof(char*));
56 printf("Unknown option to the profiler runtime: '%s' - ignored.\n", Arg);
60 for (Length = 0, i = 0; i != (unsigned)argc; ++i)
61 Length += strlen(argv[i])+1;
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);
68 SavedArgs[Length++] = ' ';
71 SavedArgsLength = Length;
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.
82 void write_profiling_data(enum ProfilingType PT, unsigned *Start,
83 unsigned NumElements) {
84 static int OutFile = -1;
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.
91 OutFile = open(OutputFilename, O_CREAT | O_WRONLY | O_APPEND, 0666);
93 fprintf(stderr, "LLVM profiling runtime: while opening '%s': ",
99 /* Output the command line arguments to the file. */
101 int PTy = ArgumentInfo;
103 write(OutFile, &PTy, sizeof(int));
104 write(OutFile, &SavedArgsLength, sizeof(unsigned));
105 write(OutFile, SavedArgs, SavedArgsLength);
106 /* Pad out to a multiple of four bytes */
107 if (SavedArgsLength & 3)
108 write(OutFile, &Zeros, 4-(SavedArgsLength&3));
112 /* Write out this record! */
114 write(OutFile, &PTy, sizeof(int));
115 write(OutFile, &NumElements, sizeof(unsigned));
116 write(OutFile, Start, NumElements*sizeof(unsigned));