GCDAProfiling.c revision cf5fb62c0206c6544c464fa1b414bbfa26cb3244
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#ifndef __APPLE__
24#include <errno.h>
25#endif
26
27#include <fcntl.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <sys/stat.h>
32#include <sys/mman.h>
33#include <sys/types.h>
34#ifdef _WIN32
35#include <direct.h>
36#endif
37
38#ifndef _MSC_VER
39#include <stdint.h>
40#else
41typedef unsigned int uint32_t;
42typedef unsigned int uint64_t;
43#endif
44
45/* #define DEBUG_GCDAPROFILING */
46
47/*
48 * --- GCOV file format I/O primitives ---
49 */
50
51/*
52 * The current file name we're outputting. Used primarily for error logging.
53 */
54static char *filename = NULL;
55
56/*
57 * The current file we're outputting.
58 */
59static FILE *output_file = NULL;
60
61/*
62 * Buffer that we write things into.
63 */
64#define WRITE_BUFFER_SIZE (128 * 1024)
65static char *write_buffer = NULL;
66static uint64_t cur_buffer_size = 0;
67static uint64_t cur_pos = 0;
68static uint64_t file_size = 0;
69static int new_file = 0;
70static int fd = -1;
71
72/*
73 * A list of functions to write out the data.
74 */
75typedef void (*writeout_fn)();
76
77struct writeout_fn_node {
78  writeout_fn fn;
79  struct writeout_fn_node *next;
80};
81
82static struct writeout_fn_node *writeout_fn_head = NULL;
83static struct writeout_fn_node *writeout_fn_tail = NULL;
84
85/*
86 *  A list of flush functions that our __gcov_flush() function should call.
87 */
88typedef void (*flush_fn)();
89
90struct flush_fn_node {
91  flush_fn fn;
92  struct flush_fn_node *next;
93};
94
95static struct flush_fn_node *flush_fn_head = NULL;
96static struct flush_fn_node *flush_fn_tail = NULL;
97
98static void resize_write_buffer(uint64_t size) {
99  if (!new_file) return;
100  size += cur_pos;
101  if (size <= cur_buffer_size) return;
102  size = (size - 1) / WRITE_BUFFER_SIZE + 1;
103  size *= WRITE_BUFFER_SIZE;
104  write_buffer = realloc(write_buffer, size);
105  cur_buffer_size = size;
106}
107
108static void write_bytes(const char *s, size_t len) {
109  resize_write_buffer(len);
110  memcpy(&write_buffer[cur_pos], s, len);
111  cur_pos += len;
112}
113
114static void write_32bit_value(uint32_t i) {
115  write_bytes((char*)&i, 4);
116}
117
118static void write_64bit_value(uint64_t i) {
119  write_bytes((char*)&i, 8);
120}
121
122static uint32_t length_of_string(const char *s) {
123  return (strlen(s) / 4) + 1;
124}
125
126static void write_string(const char *s) {
127  uint32_t len = length_of_string(s);
128  write_32bit_value(len);
129  write_bytes(s, strlen(s));
130  write_bytes("\0\0\0\0", 4 - (strlen(s) % 4));
131}
132
133static uint32_t read_32bit_value() {
134  uint32_t val;
135
136  if (new_file)
137    return (uint32_t)-1;
138
139  val = *(uint32_t*)&write_buffer[cur_pos];
140  cur_pos += 4;
141  return val;
142}
143
144static uint64_t read_64bit_value() {
145  uint64_t val;
146
147  if (new_file)
148    return (uint64_t)-1;
149
150  val = *(uint64_t*)&write_buffer[cur_pos];
151  cur_pos += 8;
152  return val;
153}
154
155static char *mangle_filename(const char *orig_filename) {
156  char *filename = 0;
157  int prefix_len = 0;
158  int prefix_strip = 0;
159  int level = 0;
160  const char *fname = orig_filename, *ptr = NULL;
161  const char *prefix = getenv("GCOV_PREFIX");
162  const char *prefix_strip_str = getenv("GCOV_PREFIX_STRIP");
163
164  if (!prefix)
165    return strdup(orig_filename);
166
167  if (prefix_strip_str) {
168    prefix_strip = atoi(prefix_strip_str);
169
170    /* Negative GCOV_PREFIX_STRIP values are ignored */
171    if (prefix_strip < 0)
172      prefix_strip = 0;
173  }
174
175  prefix_len = strlen(prefix);
176  filename = malloc(prefix_len + 1 + strlen(orig_filename) + 1);
177  strcpy(filename, prefix);
178
179  if (prefix[prefix_len - 1] != '/')
180    strcat(filename, "/");
181
182  for (ptr = fname + 1; *ptr != '\0' && level < prefix_strip; ++ptr) {
183    if (*ptr != '/') continue;
184    fname = ptr;
185    ++level;
186  }
187
188  strcat(filename, fname);
189
190  return filename;
191}
192
193static void recursive_mkdir(char *filename) {
194  int i;
195
196  for (i = 1; filename[i] != '\0'; ++i) {
197    if (filename[i] != '/') continue;
198    filename[i] = '\0';
199#ifdef _WIN32
200    _mkdir(filename);
201#else
202    mkdir(filename, 0755);  /* Some of these will fail, ignore it. */
203#endif
204    filename[i] = '/';
205  }
206}
207
208static int map_file() {
209  fseek(output_file, 0L, SEEK_END);
210  file_size = ftell(output_file);
211
212  write_buffer = mmap(0, file_size, PROT_READ | PROT_WRITE,
213                      MAP_FILE | MAP_SHARED, fd, 0);
214  if (write_buffer == (void *)-1) {
215#ifndef __APPLE__
216    int errnum = errno;
217    fprintf(stderr, "profiling: %s: cannot map: %s\n", filename,
218            strerror(errnum));
219#endif
220    return -1;
221  }
222  return 0;
223}
224
225static void unmap_file() {
226  if (msync(write_buffer, file_size, MS_SYNC) == -1) {
227#ifndef __APPLE__
228    int errnum = errno;
229    fprintf(stderr, "profiling: %s: cannot msync: %s\n", filename,
230            strerror(errnum));
231#endif
232  }
233
234  /* We explicitly ignore errors from unmapping because at this point the data
235   * is written and we don't care.
236   */
237  (void)munmap(write_buffer, file_size);
238  write_buffer = NULL;
239  file_size = 0;
240}
241
242/*
243 * --- LLVM line counter API ---
244 */
245
246/* A file in this case is a translation unit. Each .o file built with line
247 * profiling enabled will emit to a different file. Only one file may be
248 * started at a time.
249 */
250void llvm_gcda_start_file(const char *orig_filename, const char version[4]) {
251  const char *mode = "r+b";
252  filename = mangle_filename(orig_filename);
253
254  /* Try just opening the file. */
255  new_file = 0;
256  fd = open(filename, O_RDWR);
257
258  if (fd == -1) {
259    /* Try opening the file, creating it if necessary. */
260    new_file = 1;
261    mode = "w+b";
262    fd = open(filename, O_RDWR | O_CREAT, 0644);
263    if (fd == -1) {
264      /* Try creating the directories first then opening the file. */
265      recursive_mkdir(filename);
266      fd = open(filename, O_RDWR | O_CREAT, 0644);
267      if (fd == -1) {
268        /* Bah! It's hopeless. */
269#ifndef __APPLE__
270        int errnum = errno;
271        fprintf(stderr, "profiling: %s: cannot open: %s\n", filename,
272                strerror(errnum));
273#endif
274        return;
275      }
276    }
277  }
278
279  output_file = fdopen(fd, mode);
280
281  /* Initialize the write buffer. */
282  write_buffer = NULL;
283  cur_buffer_size = 0;
284  cur_pos = 0;
285
286  if (new_file) {
287    resize_write_buffer(WRITE_BUFFER_SIZE);
288    memset(write_buffer, 0, WRITE_BUFFER_SIZE);
289  } else {
290    if (map_file() == -1) {
291      /* mmap failed, try to recover by clobbering */
292      new_file = 1;
293      write_buffer = NULL;
294      cur_buffer_size = 0;
295      resize_write_buffer(WRITE_BUFFER_SIZE);
296      memset(write_buffer, 0, WRITE_BUFFER_SIZE);
297    }
298  }
299
300  /* gcda file, version, stamp LLVM. */
301  write_bytes("adcg", 4);
302  write_bytes(version, 4);
303  write_bytes("MVLL", 4);
304
305#ifdef DEBUG_GCDAPROFILING
306  fprintf(stderr, "llvmgcda: [%s]\n", orig_filename);
307#endif
308}
309
310/* Given an array of pointers to counters (counters), increment the n-th one,
311 * where we're also given a pointer to n (predecessor).
312 */
313void llvm_gcda_increment_indirect_counter(uint32_t *predecessor,
314                                          uint64_t **counters) {
315  uint64_t *counter;
316  uint32_t pred;
317
318  pred = *predecessor;
319  if (pred == 0xffffffff)
320    return;
321  counter = counters[pred];
322
323  /* Don't crash if the pred# is out of sync. This can happen due to threads,
324     or because of a TODO in GCOVProfiling.cpp buildEdgeLookupTable(). */
325  if (counter)
326    ++*counter;
327#ifdef DEBUG_GCDAPROFILING
328  else
329    fprintf(stderr,
330            "llvmgcda: increment_indirect_counter counters=%08llx, pred=%u\n",
331            *counter, *predecessor);
332#endif
333}
334
335void llvm_gcda_emit_function(uint32_t ident, const char *function_name,
336                             uint8_t use_extra_checksum) {
337  uint32_t len = 2;
338
339  if (use_extra_checksum)
340    len++;
341#ifdef DEBUG_GCDAPROFILING
342  fprintf(stderr, "llvmgcda: function id=0x%08x name=%s\n", ident,
343          function_name ? function_name : "NULL");
344#endif
345  if (!output_file) return;
346
347  /* function tag */
348  write_bytes("\0\0\0\1", 4);
349  if (function_name)
350    len += 1 + length_of_string(function_name);
351  write_32bit_value(len);
352  write_32bit_value(ident);
353  write_32bit_value(0);
354  if (use_extra_checksum)
355    write_32bit_value(0);
356  if (function_name)
357    write_string(function_name);
358}
359
360void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
361  uint32_t i;
362  uint64_t *old_ctrs = NULL;
363  uint32_t val = 0;
364  uint64_t save_cur_pos = cur_pos;
365
366  if (!output_file) return;
367
368  val = read_32bit_value();
369
370  if (val != (uint32_t)-1) {
371    /* There are counters present in the file. Merge them. */
372    if (val != 0x01a10000) {
373      fprintf(stderr, "profiling:invalid magic number (0x%08x)\n", val);
374      return;
375    }
376
377    val = read_32bit_value();
378    if (val == (uint32_t)-1 || val / 2 != num_counters) {
379      fprintf(stderr, "profiling:invalid number of counters (%d)\n", val);
380      return;
381    }
382
383    old_ctrs = malloc(sizeof(uint64_t) * num_counters);
384    for (i = 0; i < num_counters; ++i)
385      old_ctrs[i] = read_64bit_value();
386  }
387
388  cur_pos = save_cur_pos;
389
390  /* Counter #1 (arcs) tag */
391  write_bytes("\0\0\xa1\1", 4);
392  write_32bit_value(num_counters * 2);
393  for (i = 0; i < num_counters; ++i) {
394    counters[i] += (old_ctrs ? old_ctrs[i] : 0);
395    write_64bit_value(counters[i]);
396  }
397
398  free(old_ctrs);
399
400#ifdef DEBUG_GCDAPROFILING
401  fprintf(stderr, "llvmgcda:   %u arcs\n", num_counters);
402  for (i = 0; i < num_counters; ++i)
403    fprintf(stderr, "llvmgcda:   %llu\n", (unsigned long long)counters[i]);
404#endif
405}
406
407void llvm_gcda_end_file() {
408  /* Write out EOF record. */
409  if (output_file) {
410    write_bytes("\0\0\0\0\0\0\0\0", 8);
411
412    if (new_file) {
413      fwrite(write_buffer, cur_pos, 1, output_file);
414      free(write_buffer);
415    } else {
416      unmap_file();
417    }
418
419    fclose(output_file);
420    output_file = NULL;
421    write_buffer = NULL;
422  }
423  free(filename);
424
425#ifdef DEBUG_GCDAPROFILING
426  fprintf(stderr, "llvmgcda: -----\n");
427#endif
428}
429
430void llvm_register_writeout_function(writeout_fn fn) {
431  struct writeout_fn_node *new_node = malloc(sizeof(struct writeout_fn_node));
432  new_node->fn = fn;
433  new_node->next = NULL;
434
435  if (!writeout_fn_head) {
436    writeout_fn_head = writeout_fn_tail = new_node;
437  } else {
438    writeout_fn_tail->next = new_node;
439    writeout_fn_tail = new_node;
440  }
441}
442
443void llvm_writeout_files() {
444  struct writeout_fn_node *curr = writeout_fn_head;
445
446  while (curr) {
447    curr->fn();
448    curr = curr->next;
449  }
450}
451
452void llvm_delete_writeout_function_list() {
453  while (writeout_fn_head) {
454    struct writeout_fn_node *node = writeout_fn_head;
455    writeout_fn_head = writeout_fn_head->next;
456    free(node);
457  }
458
459  writeout_fn_head = writeout_fn_tail = NULL;
460}
461
462void llvm_register_flush_function(flush_fn fn) {
463  struct flush_fn_node *new_node = malloc(sizeof(struct flush_fn_node));
464  new_node->fn = fn;
465  new_node->next = NULL;
466
467  if (!flush_fn_head) {
468    flush_fn_head = flush_fn_tail = new_node;
469  } else {
470    flush_fn_tail->next = new_node;
471    flush_fn_tail = new_node;
472  }
473}
474
475void __gcov_flush() {
476  struct flush_fn_node *curr = flush_fn_head;
477
478  while (curr) {
479    curr->fn();
480    curr = curr->next;
481  }
482}
483
484void llvm_delete_flush_function_list() {
485  while (flush_fn_head) {
486    struct flush_fn_node *node = flush_fn_head;
487    flush_fn_head = flush_fn_head->next;
488    free(node);
489  }
490
491  flush_fn_head = flush_fn_tail = NULL;
492}
493
494void llvm_gcov_init(writeout_fn wfn, flush_fn ffn) {
495  static int atexit_ran = 0;
496
497  if (wfn)
498    llvm_register_writeout_function(wfn);
499
500  if (ffn)
501    llvm_register_flush_function(ffn);
502
503  if (atexit_ran == 0) {
504    atexit_ran = 1;
505
506    /* Make sure we write out the data and delete the data structures. */
507    atexit(llvm_delete_flush_function_list);
508    atexit(llvm_delete_writeout_function_list);
509    atexit(llvm_writeout_files);
510  }
511}
512