don't load element before checking to see if it is valid.
[llvm/stm8.git] / runtime / libprofile / CommonProfiling.c
blob210a5e5ab78ae67920bbd92914e906253ea3d195
1 /*===-- CommonProfiling.c - Profiling support library support -------------===*\
2 |*
3 |* The LLVM Compiler Infrastructure
4 |*
5 |* This file is distributed under the University of Illinois Open Source
6 |* License. See LICENSE.TXT for details.
7 |*
8 |*===----------------------------------------------------------------------===*|
9 |*
10 |* This file implements functions used by the various different types of
11 |* profiling implementations.
13 \*===----------------------------------------------------------------------===*/
15 #include "Profiling.h"
16 #include <assert.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 #include <stdio.h>
21 #include <string.h>
22 #if !defined(_MSC_VER) && !defined(__MINGW32__)
23 #include <unistd.h>
24 #else
25 #include <io.h>
26 #endif
27 #include <stdlib.h>
29 static char *SavedArgs = 0;
30 static unsigned SavedArgsLength = 0;
32 static const char *OutputFilename = "llvmprof.out";
34 /* save_arguments - Save argc and argv as passed into the program for the file
35 * we output.
37 int save_arguments(int argc, const char **argv) {
38 unsigned Length, i;
39 if (SavedArgs || !argv) return argc; /* This can be called multiple times */
41 /* Check to see if there are any arguments passed into the program for the
42 * profiler. If there are, strip them off and remember their settings.
44 while (argc > 1 && !strncmp(argv[1], "-llvmprof-", 10)) {
45 /* Ok, we have an llvmprof argument. Remove it from the arg list and decide
46 * what to do with it.
48 const char *Arg = argv[1];
49 memmove(&argv[1], &argv[2], (argc-1)*sizeof(char*));
50 --argc;
52 if (!strcmp(Arg, "-llvmprof-output")) {
53 if (argc == 1)
54 puts("-llvmprof-output requires a filename argument!");
55 else {
56 OutputFilename = strdup(argv[1]);
57 memmove(&argv[1], &argv[2], (argc-1)*sizeof(char*));
58 --argc;
60 } else {
61 printf("Unknown option to the profiler runtime: '%s' - ignored.\n", Arg);
65 for (Length = 0, i = 0; i != (unsigned)argc; ++i)
66 Length += strlen(argv[i])+1;
68 SavedArgs = (char*)malloc(Length);
69 for (Length = 0, i = 0; i != (unsigned)argc; ++i) {
70 unsigned Len = strlen(argv[i]);
71 memcpy(SavedArgs+Length, argv[i], Len);
72 Length += Len;
73 SavedArgs[Length++] = ' ';
76 SavedArgsLength = Length;
78 return argc;
83 * Retrieves the file descriptor for the profile file.
85 int getOutFile() {
86 static int OutFile = -1;
88 /* If this is the first time this function is called, open the output file
89 * for appending, creating it if it does not already exist.
91 if (OutFile == -1) {
92 OutFile = open(OutputFilename, O_CREAT | O_WRONLY, 0666);
93 lseek(OutFile, 0, SEEK_END); /* O_APPEND prevents seeking */
94 if (OutFile == -1) {
95 fprintf(stderr, "LLVM profiling runtime: while opening '%s': ",
96 OutputFilename);
97 perror("");
98 return(OutFile);
101 /* Output the command line arguments to the file. */
103 int PTy = ArgumentInfo;
104 int Zeros = 0;
105 write(OutFile, &PTy, sizeof(int));
106 write(OutFile, &SavedArgsLength, sizeof(unsigned));
107 write(OutFile, SavedArgs, SavedArgsLength);
108 /* Pad out to a multiple of four bytes */
109 if (SavedArgsLength & 3)
110 write(OutFile, &Zeros, 4-(SavedArgsLength&3));
113 return(OutFile);
116 /* write_profiling_data - Write a raw block of profiling counters out to the
117 * llvmprof.out file. Note that we allow programs to be instrumented with
118 * multiple different kinds of instrumentation. For this reason, this function
119 * may be called more than once.
121 void write_profiling_data(enum ProfilingType PT, unsigned *Start,
122 unsigned NumElements) {
123 int PTy;
124 int outFile = getOutFile();
126 /* Write out this record! */
127 PTy = PT;
128 if( write(outFile, &PTy, sizeof(int)) < 0 ||
129 write(outFile, &NumElements, sizeof(unsigned)) < 0 ||
130 write(outFile, Start, NumElements*sizeof(unsigned)) < 0 ) {
131 fprintf(stderr,"error: unable to write to output file.");
132 exit(0);