GCDAProfiling.c revision ec63f456de18b9ac75d7546f8a53beb6ce048e67
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 char *mangle_filename(const char *orig_filename) {
70  char *filename = 0;
71  int prefix_len = 0;
72  int prefix_strip = 0;
73  int level = 0;
74  const char *fname = orig_filename, *ptr = NULL;
75  const char *prefix = getenv("GCOV_PREFIX");
76  const char *tmp = getenv("GCOV_PREFIX_STRIP");
77
78  if (!prefix)
79    return strdup(orig_filename);
80
81  if (tmp) {
82    prefix_strip = atoi(tmp);
83
84    /* Negative GCOV_PREFIX_STRIP values are ignored */
85    if (prefix_strip < 0)
86      prefix_strip = 0;
87  }
88
89  prefix_len = strlen(prefix);
90  filename = malloc(prefix_len + 1 + strlen(orig_filename) + 1);
91  strcpy(filename, prefix);
92
93  if (prefix[prefix_len - 1] != '/')
94    strcat(filename, "/");
95
96  for (ptr = fname + 1; *ptr != '\0' && level < prefix_strip; ++ptr) {
97    if (*ptr != '/') continue;
98    fname = ptr;
99    ++level;
100  }
101
102  strcat(filename, fname);
103
104  return filename;
105}
106
107static void recursive_mkdir(char *filename) {
108  int i;
109
110  for (i = 1; filename[i] != '\0'; ++i) {
111    if (filename[i] != '/') continue;
112    filename[i] = '\0';
113#ifdef _WIN32
114    _mkdir(filename);
115#else
116    mkdir(filename, 0755);  /* Some of these will fail, ignore it. */
117#endif
118    filename[i] = '/';
119  }
120}
121
122/*
123 * --- LLVM line counter API ---
124 */
125
126/* A file in this case is a translation unit. Each .o file built with line
127 * profiling enabled will emit to a different file. Only one file may be
128 * started at a time.
129 */
130void llvm_gcda_start_file(const char *orig_filename) {
131  char *filename = mangle_filename(orig_filename);
132  output_file = fopen(filename, "w+b");
133
134  if (!output_file) {
135    recursive_mkdir(filename);
136    output_file = fopen(filename, "w+b");
137    if (!output_file) {
138      fprintf(stderr, "profiling:%s: cannot open\n", filename);
139      return;
140    }
141  }
142
143  /* gcda file, version 404*, stamp LLVM. */
144#ifdef __APPLE__
145  fwrite("adcg*204MVLL", 12, 1, output_file);
146#else
147  fwrite("adcg*404MVLL", 12, 1, output_file);
148#endif
149
150#ifdef DEBUG_GCDAPROFILING
151  printf("llvmgcda: [%s]\n", orig_filename);
152#endif
153
154  free(filename);
155}
156
157/* Given an array of pointers to counters (counters), increment the n-th one,
158 * where we're also given a pointer to n (predecessor).
159 */
160void llvm_gcda_increment_indirect_counter(uint32_t *predecessor,
161                                          uint64_t **counters) {
162  uint64_t *counter;
163  uint32_t pred;
164
165  pred = *predecessor;
166  if (pred == 0xffffffff)
167    return;
168  counter = counters[pred];
169
170  /* Don't crash if the pred# is out of sync. This can happen due to threads,
171     or because of a TODO in GCOVProfiling.cpp buildEdgeLookupTable(). */
172  if (counter)
173    ++*counter;
174#ifdef DEBUG_GCDAPROFILING
175  else
176    fprintf(stderr,
177            "llvmgcda: increment_indirect_counter counters=%x, pred=%u\n",
178            state_table_row, *predecessor);
179#endif
180}
181
182void llvm_gcda_emit_function(uint32_t ident, const char *function_name) {
183#ifdef DEBUG_GCDAPROFILING
184  printf("llvmgcda: function id=%x\n", ident);
185#endif
186  if (!output_file) return;
187
188  /* function tag */
189  fwrite("\0\0\0\1", 4, 1, output_file);
190  write_int32(3 + 1 + length_of_string(function_name));
191  write_int32(ident);
192  write_int32(0);
193  write_int32(0);
194  write_string(function_name);
195}
196
197void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
198  uint32_t i;
199
200  /* Counter #1 (arcs) tag */
201  if (!output_file) return;
202  fwrite("\0\0\xa1\1", 4, 1, output_file);
203  write_int32(num_counters * 2);
204  for (i = 0; i < num_counters; ++i)
205    write_int64(counters[i]);
206
207#ifdef DEBUG_GCDAPROFILING
208  printf("llvmgcda:   %u arcs\n", num_counters);
209  for (i = 0; i < num_counters; ++i)
210    printf("llvmgcda:   %llu\n", (unsigned long long)counters[i]);
211#endif
212}
213
214void llvm_gcda_end_file() {
215  /* Write out EOF record. */
216  if (!output_file) return;
217  fwrite("\0\0\0\0\0\0\0\0", 8, 1, output_file);
218  fclose(output_file);
219  output_file = NULL;
220
221#ifdef DEBUG_GCDAPROFILING
222  printf("llvmgcda: -----\n");
223#endif
224}
225