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
) {
50 * --- LLVM line counter API ---
53 /* A file in this case is a translation unit. Each .o file built with line
54 * profiling enabled will emit to a different file. Only one file may be
57 void llvm_gcda_start_file(const char *filename
) {
58 output_file
= fopen(filename
, "w+");
60 /* gcda file, version 404*, stamp LLVM. */
61 fwrite("adcg*404MVLL", 12, 1, output_file
);
63 #ifdef DEBUG_GCDAPROFILING
64 printf("llvmgcda: [%s]\n", filename
);
68 /* Given an array of pointers to counters (counters), increment the n-th one,
69 * where we're also given a pointer to n (predecessor).
71 void llvm_gcda_increment_indirect_counter(uint32_t *predecessor
,
72 uint64_t **counters
) {
74 if (*predecessor
== 0xffffffff)
77 /* Don't crash if the pred# is out of sync. This can happen due to threads,
78 or because of a TODO in GCOVProfiling.cpp buildEdgeLookupTable(). */
79 if ((counter
= counters
[*predecessor
]))
81 #ifdef DEBUG_GCDAPROFILING
83 printf("llvmgcda: increment_indirect_counter counters=%x, pred=%u\n",
84 state_table_row
, *predecessor
);
88 void llvm_gcda_emit_function(uint32_t ident
) {
89 #ifdef DEBUG_GCDAPROFILING
90 printf("llvmgcda: function id=%x\n", ident
);
94 fwrite("\0\0\0\1", 4, 1, output_file
);
100 void llvm_gcda_emit_arcs(uint32_t num_counters
, uint64_t *counters
) {
102 /* counter #1 (arcs) tag */
103 fwrite("\0\0\xa1\1", 4, 1, output_file
);
104 write_int32(num_counters
* 2);
105 for (i
= 0; i
< num_counters
; ++i
) {
106 write_int64(counters
[i
]);
109 #ifdef DEBUG_GCDAPROFILING
110 printf("llvmgcda: %u arcs\n", num_counters
);
111 for (i
= 0; i
< num_counters
; ++i
) {
112 printf("llvmgcda: %llu\n", (unsigned long long)counters
[i
]);
117 void llvm_gcda_end_file() {
118 /* Write out EOF record. */
119 fwrite("\0\0\0\0\0\0\0\0", 8, 1, output_file
);
123 #ifdef DEBUG_GCDAPROFILING
124 printf("llvmgcda: -----\n");