aboutsummaryrefslogtreecommitdiff
path: root/runtime/libprofile/GCDAProfiling.c
blob: 2dcf22d96472045f70a7d2f8519337efa694fadb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*===- GCDAProfiling.c - Support library for GCDA file emission -----------===*\
|*
|*                     The LLVM Compiler Infrastructure
|*
|* This file is distributed under the University of Illinois Open Source
|* License. See LICENSE.TXT for details.
|* 
|*===----------------------------------------------------------------------===*|
|* 
|* This file implements the call back routines for the gcov profiling
|* instrumentation pass. Link against this library when running code through
|* the -insert-gcov-profiling LLVM pass.
|*
|* We emit files in a corrupt version of GCOV's "gcda" file format. These files
|* are only close enough that LCOV will happily parse them. Anything that lcov
|* ignores is missing.
|*
|* TODO: gcov is multi-process safe by having each exit open the existing file
|* and append to it. We'd like to achieve that and be thread-safe too.
|*
\*===----------------------------------------------------------------------===*/

#include "llvm/Support/DataTypes.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* #define DEBUG_GCDAPROFILING */

/*
 * --- GCOV file format I/O primitives ---
 */

static FILE *output_file = NULL;

static void write_int32(uint32_t i) {
  fwrite(&i, 4, 1, output_file);
}

static void write_int64(uint64_t i) {
  uint32_t lo, hi;
  lo = i >>  0;
  hi = i >> 32;

  write_int32(lo);
  write_int32(hi);
}

static char *mangle_filename(const char *orig_filename) {
  /* TODO: handle GCOV_PREFIX_STRIP */
  const char *prefix;
  char *filename = 0;

  prefix = getenv("GCOV_PREFIX");

  if (!prefix)
    return strdup(filename);

  filename = malloc(strlen(prefix) + 1 + strlen(orig_filename) + 1);
  strcpy(filename, prefix);
  strcat(filename, "/");
  strcat(filename, orig_filename);

  return filename;
}

/*
 * --- LLVM line counter API ---
 */

/* A file in this case is a translation unit. Each .o file built with line
 * profiling enabled will emit to a different file. Only one file may be
 * started at a time.
 */
void llvm_gcda_start_file(const char *orig_filename) {
  char *filename;
  filename = mangle_filename(orig_filename);
  output_file = fopen(filename, "wb");

  /* gcda file, version 404*, stamp LLVM. */
  fwrite("adcg*404MVLL", 12, 1, output_file);

#ifdef DEBUG_GCDAPROFILING
  printf("llvmgcda: [%s]\n", orig_filename);
#endif

  free(filename);
}

/* Given an array of pointers to counters (counters), increment the n-th one,
 * where we're also given a pointer to n (predecessor).
 */
void llvm_gcda_increment_indirect_counter(uint32_t *predecessor,
                                          uint64_t **counters) {
  uint64_t *counter;
  uint32_t pred;

  pred = *predecessor;
  if (pred == 0xffffffff)
    return;
  counter = counters[pred];

  /* Don't crash if the pred# is out of sync. This can happen due to threads,
     or because of a TODO in GCOVProfiling.cpp buildEdgeLookupTable(). */
  if (counter)
    ++*counter;
#ifdef DEBUG_GCDAPROFILING
  else
    printf("llvmgcda: increment_indirect_counter counters=%x, pred=%u\n",
           state_table_row, *predecessor);
#endif
}

void llvm_gcda_emit_function(uint32_t ident) {
#ifdef DEBUG_GCDAPROFILING
  printf("llvmgcda: function id=%x\n", ident);
#endif

  /* function tag */  
  fwrite("\0\0\0\1", 4, 1, output_file);
  write_int32(2);
  write_int32(ident);
  write_int32(0);
}

void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
  uint32_t i;
  /* counter #1 (arcs) tag */
  fwrite("\0\0\xa1\1", 4, 1, output_file);
  write_int32(num_counters * 2);
  for (i = 0; i < num_counters; ++i) {
    write_int64(counters[i]);
  }

#ifdef DEBUG_GCDAPROFILING
  printf("llvmgcda:   %u arcs\n", num_counters);
  for (i = 0; i < num_counters; ++i) {
    printf("llvmgcda:   %llu\n", (unsigned long long)counters[i]);
  }
#endif
}

void llvm_gcda_end_file() {
  /* Write out EOF record. */
  fwrite("\0\0\0\0\0\0\0\0", 8, 1, output_file);
  fclose(output_file);
  output_file = NULL;

#ifdef DEBUG_GCDAPROFILING
  printf("llvmgcda: -----\n");
#endif
}