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 \*===----------------------------------------------------------------------===*/
20 #include "llvm/Support/DataTypes.h"
26 // #define DEBUG_GCDAPROFILING
29 * --- GCOV file format I/O primitives ---
32 static FILE *output_file = NULL;
34 static void write_int32(uint32_t i) {
35 fwrite(&i, 4, 1, output_file);
38 static void write_int64(uint64_t i) {
40 lo = i & 0x00000000ffffffff;
41 hi = i & 0xffffffff00000000;
48 * --- LLVM line counter API ---
51 /* A file in this case is a translation unit. Each .o file built with line
52 * profiling enabled will emit to a different file. Only one file may be
55 void llvm_gcda_start_file(const char *filename) {
56 output_file = fopen(filename, "w+");
58 /* gcda file, version 404*, stamp LLVM. */
59 fwrite("adcg*404MVLL", 12, 1, output_file);
61 #ifdef DEBUG_GCDAPROFILING
62 printf("[%s]\n", filename);
66 void llvm_gcda_emit_function(uint32_t ident) {
67 #ifdef DEBUG_GCDAPROFILING
68 printf("function id=%x\n", ident);
72 fwrite("\0\0\0\1", 4, 1, output_file);
78 void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
80 /* counter #1 (arcs) tag */
81 fwrite("\0\0\xa1\1", 4, 1, output_file);
82 write_int32(num_counters * 2);
83 for (i = 0; i < num_counters; ++i) {
84 write_int64(counters[i]);
87 #ifdef DEBUG_GCDAPROFILING
88 printf(" %u arcs\n", num_counters);
89 for (i = 0; i < num_counters; ++i) {
90 printf(" %llu\n", (unsigned long long)counters[i]);
95 void llvm_gcda_end_file() {
96 /* Write out EOF record. */
97 fwrite("\0\0\0\0\0\0\0\0", 8, 1, output_file);
101 #ifdef DEBUG_GCDAPROFILING