GCDAProfiling.c revision 9c994aae092ddbc786ceb27668f38571de926e5b
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 <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28#ifdef _WIN32
29#include <direct.h>
30#endif
31
32#ifndef _MSC_VER
33#include <stdint.h>
34#else
35typedef unsigned int uint32_t;
36typedef unsigned int uint64_t;
37#endif
38
39/* #define DEBUG_GCDAPROFILING */
40
41/*
42 * --- GCOV file format I/O primitives ---
43 */
44
45static FILE *output_file = NULL;
46
47static void write_int32(uint32_t i) {
48  fwrite(&i, 4, 1, output_file);
49}
50
51static void write_int64(uint64_t i) {
52  uint32_t lo = i >>  0;
53  uint32_t hi = i >> 32;
54  write_int32(lo);
55  write_int32(hi);
56}
57
58static uint32_t length_of_string(const char *s) {
59  return (strlen(s) / 4) + 1;
60}
61
62static void write_string(const char *s) {
63  uint32_t len = length_of_string(s);
64  write_int32(len);
65  fwrite(s, strlen(s), 1, output_file);
66  fwrite("\0\0\0\0", 4 - (strlen(s) % 4), 1, output_file);
67}
68
69static uint32_t read_int32() {
70  uint32_t tmp;
71
72  if (fread(&tmp, 1, 4, output_file) != 4)
73    return (uint32_t)-1;
74
75  return tmp;
76}
77
78static uint64_t read_int64() {
79  uint64_t tmp;
80
81  if (fread(&tmp, 1, 8, output_file) != 8)
82    return (uint64_t)-1;
83
84  return tmp;
85}
86
87static char *mangle_filename(const char *orig_filename) {
88  char *filename = 0;
89  int prefix_len = 0;
90  int prefix_strip = 0;
91  int level = 0;
92  const char *fname = orig_filename, *ptr = NULL;
93  const char *prefix = getenv("GCOV_PREFIX");
94  const char *tmp = getenv("GCOV_PREFIX_STRIP");
95
96  if (!prefix)
97    return strdup(orig_filename);
98
99  if (tmp) {
100    prefix_strip = atoi(tmp);
101
102    /* Negative GCOV_PREFIX_STRIP values are ignored */
103    if (prefix_strip < 0)
104      prefix_strip = 0;
105  }
106
107  prefix_len = strlen(prefix);
108  filename = malloc(prefix_len + 1 + strlen(orig_filename) + 1);
109  strcpy(filename, prefix);
110
111  if (prefix[prefix_len - 1] != '/')
112    strcat(filename, "/");
113
114  for (ptr = fname + 1; *ptr != '\0' && level < prefix_strip; ++ptr) {
115    if (*ptr != '/') continue;
116    fname = ptr;
117    ++level;
118  }
119
120  strcat(filename, fname);
121
122  return filename;
123}
124
125static void recursive_mkdir(char *filename) {
126  int i;
127
128  for (i = 1; filename[i] != '\0'; ++i) {
129    if (filename[i] != '/') continue;
130    filename[i] = '\0';
131#ifdef _WIN32
132    _mkdir(filename);
133#else
134    mkdir(filename, 0755);  /* Some of these will fail, ignore it. */
135#endif
136    filename[i] = '/';
137  }
138}
139
140/*
141 * --- LLVM line counter API ---
142 */
143
144/* A file in this case is a translation unit. Each .o file built with line
145 * profiling enabled will emit to a different file. Only one file may be
146 * started at a time.
147 */
148void llvm_gcda_start_file(const char *orig_filename) {
149  char *filename = mangle_filename(orig_filename);
150
151  /* Try just opening the file. */
152  output_file = fopen(filename, "r+b");
153
154  if (!output_file) {
155    /* Try opening the file, creating it if necessary. */
156    output_file = fopen(filename, "w+b");
157    if (!output_file) {
158      /* Try creating the directories first then opening the file. */
159      recursive_mkdir(filename);
160      output_file = fopen(filename, "w+b");
161      if (!output_file) {
162        /* Bah! It's hopeless. */
163        fprintf(stderr, "profiling:%s: cannot open\n", filename);
164        free(filename);
165        return;
166      }
167    }
168  }
169
170  /* gcda file, version 404*, stamp LLVM. */
171#ifdef __APPLE__
172  fwrite("adcg*204MVLL", 12, 1, output_file);
173#else
174  fwrite("adcg*404MVLL", 12, 1, output_file);
175#endif
176
177  free(filename);
178
179#ifdef DEBUG_GCDAPROFILING
180  fprintf(stderr, "llvmgcda: [%s]\n", orig_filename);
181#endif
182}
183
184/* Given an array of pointers to counters (counters), increment the n-th one,
185 * where we're also given a pointer to n (predecessor).
186 */
187void llvm_gcda_increment_indirect_counter(uint32_t *predecessor,
188                                          uint64_t **counters) {
189  uint64_t *counter;
190  uint32_t pred;
191
192  pred = *predecessor;
193  if (pred == 0xffffffff)
194    return;
195  counter = counters[pred];
196
197  /* Don't crash if the pred# is out of sync. This can happen due to threads,
198     or because of a TODO in GCOVProfiling.cpp buildEdgeLookupTable(). */
199  if (counter)
200    ++*counter;
201#ifdef DEBUG_GCDAPROFILING
202  else
203    fprintf(stderr,
204            "llvmgcda: increment_indirect_counter counters=%08llx, pred=%u\n",
205            *counter, *predecessor);
206#endif
207}
208
209void llvm_gcda_emit_function(uint32_t ident, const char *function_name) {
210#ifdef DEBUG_GCDAPROFILING
211  fprintf(stderr, "llvmgcda: function id=0x%08x\n", ident);
212#endif
213  if (!output_file) return;
214
215  /* function tag */
216  fwrite("\0\0\0\1", 4, 1, output_file);
217  write_int32(3 + 1 + length_of_string(function_name));
218  write_int32(ident);
219  write_int32(0);
220  write_int32(0);
221  write_string(function_name);
222}
223
224void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
225  uint32_t i;
226  uint64_t *old_ctrs = NULL;
227  uint32_t val = 0;
228  long pos = 0;
229
230  if (!output_file) return;
231
232  pos = ftell(output_file);
233  val = read_int32();
234
235  if (val != (uint32_t)-1) {
236    /* There are counters present in the file. Merge them. */
237    uint32_t j;
238
239    if (val != 0x01a10000) {
240      fprintf(stderr, "profiling: invalid magic number (0x%08x)\n", val);
241      return;
242    }
243
244    val = read_int32();
245    if (val == (uint32_t)-1 || val / 2 != num_counters) {
246      fprintf(stderr, "profiling: invalid number of counters (%d)\n", val);
247      return;
248    }
249
250    old_ctrs = malloc(sizeof(uint64_t) * num_counters);
251
252    for (j = 0; j < num_counters; ++j)
253      old_ctrs[j] = read_int64();
254  }
255
256  /* Reset for writing. */
257  fseek(output_file, pos, SEEK_SET);
258
259  /* Counter #1 (arcs) tag */
260  fwrite("\0\0\xa1\1", 4, 1, output_file);
261  write_int32(num_counters * 2);
262  for (i = 0; i < num_counters; ++i)
263    write_int64(counters[i] + (old_ctrs ? old_ctrs[i] : 0));
264
265  free(old_ctrs);
266
267#ifdef DEBUG_GCDAPROFILING
268  fprintf(stderr, "llvmgcda:   %u arcs\n", num_counters);
269  for (i = 0; i < num_counters; ++i)
270    fprintf(stderr, "llvmgcda:   %llu\n", (unsigned long long)counters[i]);
271#endif
272}
273
274void llvm_gcda_end_file() {
275  /* Write out EOF record. */
276  if (!output_file) return;
277  fwrite("\0\0\0\0\0\0\0\0", 8, 1, output_file);
278  fclose(output_file);
279  output_file = NULL;
280
281#ifdef DEBUG_GCDAPROFILING
282  fprintf(stderr, "llvmgcda: -----\n");
283#endif
284}
285