32d19ad7742b9a59d3c735875c86960d8cba9e27
[oota-llvm.git] / runtime / libprofile / GCDAProfiling.c
1 /*===- GCDAProfiling.c - Support library for GCDA file emission -----------===*\
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 the call back routines for the gcov profiling
11 |* instrumentation pass. Link against this library when running code through
12 |* the -insert-gcov-profiling LLVM pass.
13 |*
14 |* We emit files in a corrupt version of GCOV's "gcda" file format. These files
15 |* are only close enough that LCOV will happily parse them. Anything that lcov
16 |* ignores is missing.
17 |*
18 |* TODO: gcov is multi-process safe by having each exit open the existing file
19 |* and append to it. We'd like to achieve that and be thread-safe too.
20 |*
21 \*===----------------------------------------------------------------------===*/
22
23 #include "llvm/Support/DataTypes.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #ifdef _WIN32
30 #include <direct.h>
31 #endif
32
33 /* #define DEBUG_GCDAPROFILING */
34
35 /*
36  * --- GCOV file format I/O primitives ---
37  */
38
39 static FILE *output_file = NULL;
40
41 static void write_int32(uint32_t i) {
42   fwrite(&i, 4, 1, output_file);
43 }
44
45 static void write_int64(uint64_t i) {
46   uint32_t lo, hi;
47   lo = i >>  0;
48   hi = i >> 32;
49
50   write_int32(lo);
51   write_int32(hi);
52 }
53
54 static uint32_t length_of_string(const char *s) {
55   return (strlen(s) / 4) + 1;
56 }
57
58 static void write_string(const char *s) {
59   uint32_t len = length_of_string(s);
60   write_int32(len);
61   fwrite(s, strlen(s), 1, output_file);
62   fwrite("\0\0\0\0", 4 - (strlen(s) % 4), 1, output_file);
63 }
64
65 static char *mangle_filename(const char *orig_filename) {
66   /* TODO: handle GCOV_PREFIX_STRIP */
67   const char *prefix;
68   char *filename = 0;
69
70   prefix = getenv("GCOV_PREFIX");
71
72   if (!prefix)
73     return strdup(orig_filename);
74
75   filename = malloc(strlen(prefix) + 1 + strlen(orig_filename) + 1);
76   strcpy(filename, prefix);
77   strcat(filename, "/");
78   strcat(filename, orig_filename);
79
80   return filename;
81 }
82
83 static void recursive_mkdir(const char *filename) {
84   char *pathname;
85   int i, e;
86
87   for (i = 1, e = strlen(filename); i != e; ++i) {
88     if (filename[i] != '/') continue;
89     pathname = malloc(i + 1);
90     strncpy(pathname, filename, i);
91     pathname[i] = '\0';
92 #ifdef _WIN32
93     _mkdir(pathname);
94 #else
95     mkdir(pathname, 0750);  /* some of these will fail, ignore it. */
96 #endif
97     free(pathname);
98   }
99 }
100
101 /*
102  * --- LLVM line counter API ---
103  */
104
105 /* A file in this case is a translation unit. Each .o file built with line
106  * profiling enabled will emit to a different file. Only one file may be
107  * started at a time.
108  */
109 void llvm_gcda_start_file(const char *orig_filename) {
110   char *filename;
111   filename = mangle_filename(orig_filename);
112   recursive_mkdir(filename);
113   output_file = fopen(filename, "w+b");
114
115   if (!output_file) {
116     const char *cptr = strrchr(orig_filename, '/');
117     output_file = fopen(cptr ? cptr + 1 : orig_filename, "w+b");
118
119     if (!output_file) {
120       fprintf(stderr, "LLVM profiling runtime: cannot open '%s': ",
121               cptr ? cptr + 1 : orig_filename);
122       perror("");
123       return;
124     }
125   }
126
127   /* gcda file, version 404*, stamp LLVM. */
128 #ifdef __APPLE__
129   fwrite("adcg*204MVLL", 12, 1, output_file);
130 #else
131   fwrite("adcg*404MVLL", 12, 1, output_file);
132 #endif
133
134 #ifdef DEBUG_GCDAPROFILING
135   printf("llvmgcda: [%s]\n", orig_filename);
136 #endif
137
138   free(filename);
139 }
140
141 /* Given an array of pointers to counters (counters), increment the n-th one,
142  * where we're also given a pointer to n (predecessor).
143  */
144 void llvm_gcda_increment_indirect_counter(uint32_t *predecessor,
145                                           uint64_t **counters) {
146   uint64_t *counter;
147   uint32_t pred;
148
149   pred = *predecessor;
150   if (pred == 0xffffffff)
151     return;
152   counter = counters[pred];
153
154   /* Don't crash if the pred# is out of sync. This can happen due to threads,
155      or because of a TODO in GCOVProfiling.cpp buildEdgeLookupTable(). */
156   if (counter)
157     ++*counter;
158 #ifdef DEBUG_GCDAPROFILING
159   else
160     printf("llvmgcda: increment_indirect_counter counters=%x, pred=%u\n",
161            state_table_row, *predecessor);
162 #endif
163 }
164
165 void llvm_gcda_emit_function(uint32_t ident, const char *function_name) {
166 #ifdef DEBUG_GCDAPROFILING
167   printf("llvmgcda: function id=%x\n", ident);
168 #endif
169   if (!output_file) return;
170
171   /* function tag */  
172   fwrite("\0\0\0\1", 4, 1, output_file);
173   write_int32(3 + 1 + length_of_string(function_name));
174   write_int32(ident);
175   write_int32(0);
176   write_int32(0);
177   write_string(function_name);
178 }
179
180 void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
181   uint32_t i;
182
183   /* Counter #1 (arcs) tag */
184   if (!output_file) return;
185   fwrite("\0\0\xa1\1", 4, 1, output_file);
186   write_int32(num_counters * 2);
187   for (i = 0; i < num_counters; ++i) {
188     write_int64(counters[i]);
189   }
190
191 #ifdef DEBUG_GCDAPROFILING
192   printf("llvmgcda:   %u arcs\n", num_counters);
193   for (i = 0; i < num_counters; ++i) {
194     printf("llvmgcda:   %llu\n", (unsigned long long)counters[i]);
195   }
196 #endif
197 }
198
199 void llvm_gcda_end_file() {
200   /* Write out EOF record. */
201   if (!output_file) return;
202   fwrite("\0\0\0\0\0\0\0\0", 8, 1, output_file);
203   fclose(output_file);
204   output_file = NULL;
205
206 #ifdef DEBUG_GCDAPROFILING
207   printf("llvmgcda: -----\n");
208 #endif
209 }