1 /*===- GCDAProfiling.c - Support library for GCDA file emission -----------===*\
3 |* The LLVM Compiler Infrastructure
5 |* This file is distributed under the University of Illinois Open Source
6 |* License. See LICENSE.TXT for details.
8 |*===----------------------------------------------------------------------===*|
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.
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.
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.
21 \*===----------------------------------------------------------------------===*/
23 #include "llvm/Support/DataTypes.h"
28 /* #define DEBUG_GCDAPROFILING */
31 * --- GCOV file format I/O primitives ---
34 static FILE *output_file = NULL;
36 static void write_int32(uint32_t i) {
37 fwrite(&i, 4, 1, output_file);
40 static void write_int64(uint64_t i) {
49 static char *mangle_filename(const char *orig_filename) {
50 /* TODO: handle GCOV_PREFIX_STRIP */
54 prefix = getenv("GCOV_PREFIX");
57 return strdup(orig_filename);
59 filename = malloc(strlen(prefix) + 1 + strlen(orig_filename) + 1);
60 strcpy(filename, prefix);
61 strcat(filename, "/");
62 strcat(filename, orig_filename);
68 * --- LLVM line counter API ---
71 /* A file in this case is a translation unit. Each .o file built with line
72 * profiling enabled will emit to a different file. Only one file may be
75 void llvm_gcda_start_file(const char *orig_filename) {
77 filename = mangle_filename(orig_filename);
78 output_file = fopen(filename, "wb");
80 /* gcda file, version 404*, stamp LLVM. */
81 fwrite("adcg*404MVLL", 12, 1, output_file);
83 #ifdef DEBUG_GCDAPROFILING
84 printf("llvmgcda: [%s]\n", orig_filename);
90 /* Given an array of pointers to counters (counters), increment the n-th one,
91 * where we're also given a pointer to n (predecessor).
93 void llvm_gcda_increment_indirect_counter(uint32_t *predecessor,
94 uint64_t **counters) {
99 if (pred == 0xffffffff)
101 counter = counters[pred];
103 /* Don't crash if the pred# is out of sync. This can happen due to threads,
104 or because of a TODO in GCOVProfiling.cpp buildEdgeLookupTable(). */
107 #ifdef DEBUG_GCDAPROFILING
109 printf("llvmgcda: increment_indirect_counter counters=%x, pred=%u\n",
110 state_table_row, *predecessor);
114 void llvm_gcda_emit_function(uint32_t ident) {
115 #ifdef DEBUG_GCDAPROFILING
116 printf("llvmgcda: function id=%x\n", ident);
120 fwrite("\0\0\0\1", 4, 1, output_file);
126 void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
128 /* counter #1 (arcs) tag */
129 fwrite("\0\0\xa1\1", 4, 1, output_file);
130 write_int32(num_counters * 2);
131 for (i = 0; i < num_counters; ++i) {
132 write_int64(counters[i]);
135 #ifdef DEBUG_GCDAPROFILING
136 printf("llvmgcda: %u arcs\n", num_counters);
137 for (i = 0; i < num_counters; ++i) {
138 printf("llvmgcda: %llu\n", (unsigned long long)counters[i]);
143 void llvm_gcda_end_file() {
144 /* Write out EOF record. */
145 fwrite("\0\0\0\0\0\0\0\0", 8, 1, output_file);
149 #ifdef DEBUG_GCDAPROFILING
150 printf("llvmgcda: -----\n");