blob: c8161d44b07ffd6aeef22034893fb3c6d31869ad [file] [log] [blame]
Nick Lewyckyb1928702011-04-16 01:20:23 +00001/*===- 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|*
Nick Lewycky1790c9c2011-04-26 03:54:16 +000018|* 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|*
Nick Lewyckyb1928702011-04-16 01:20:23 +000021\*===----------------------------------------------------------------------===*/
22
23#include "llvm/Support/DataTypes.h"
Nick Lewyckyb1928702011-04-16 01:20:23 +000024#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27
Nick Lewyckyece78a32011-04-16 04:25:36 +000028/* #define DEBUG_GCDAPROFILING */
Nick Lewyckyb1928702011-04-16 01:20:23 +000029
30/*
31 * --- GCOV file format I/O primitives ---
32 */
33
34static FILE *output_file = NULL;
35
36static void write_int32(uint32_t i) {
37 fwrite(&i, 4, 1, output_file);
38}
39
40static void write_int64(uint64_t i) {
41 uint32_t lo, hi;
Benjamin Kramer571c0e92011-04-16 10:25:32 +000042 lo = i >> 0;
43 hi = i >> 32;
Nick Lewyckyb1928702011-04-16 01:20:23 +000044
45 write_int32(lo);
46 write_int32(hi);
47}
48
49/*
50 * --- LLVM line counter API ---
51 */
52
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
55 * started at a time.
56 */
57void llvm_gcda_start_file(const char *filename) {
58 output_file = fopen(filename, "w+");
59
60 /* gcda file, version 404*, stamp LLVM. */
61 fwrite("adcg*404MVLL", 12, 1, output_file);
62
63#ifdef DEBUG_GCDAPROFILING
Nick Lewycky1790c9c2011-04-26 03:54:16 +000064 printf("llvmgcda: [%s]\n", filename);
65#endif
66}
67
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).
70 */
71void llvm_gcda_increment_indirect_counter(uint32_t *predecessor,
72 uint64_t **counters) {
73 uint64_t *counter;
74 if (*predecessor == 0xffffffff)
75 return;
76
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]))
80 ++*counter;
81#ifdef DEBUG_GCDAPROFILING
82 else
83 printf("llvmgcda: increment_indirect_counter counters=%x, pred=%u\n",
84 state_table_row, *predecessor);
Nick Lewyckyb1928702011-04-16 01:20:23 +000085#endif
86}
87
88void llvm_gcda_emit_function(uint32_t ident) {
89#ifdef DEBUG_GCDAPROFILING
Nick Lewycky1790c9c2011-04-26 03:54:16 +000090 printf("llvmgcda: function id=%x\n", ident);
Nick Lewyckyb1928702011-04-16 01:20:23 +000091#endif
92
93 /* function tag */
94 fwrite("\0\0\0\1", 4, 1, output_file);
95 write_int32(2);
96 write_int32(ident);
97 write_int32(0);
98}
99
100void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
101 uint32_t i;
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]);
107 }
108
109#ifdef DEBUG_GCDAPROFILING
Nick Lewycky1790c9c2011-04-26 03:54:16 +0000110 printf("llvmgcda: %u arcs\n", num_counters);
Nick Lewyckyb1928702011-04-16 01:20:23 +0000111 for (i = 0; i < num_counters; ++i) {
Nick Lewycky1790c9c2011-04-26 03:54:16 +0000112 printf("llvmgcda: %llu\n", (unsigned long long)counters[i]);
Nick Lewyckyb1928702011-04-16 01:20:23 +0000113 }
114#endif
115}
116
117void llvm_gcda_end_file() {
118 /* Write out EOF record. */
119 fwrite("\0\0\0\0\0\0\0\0", 8, 1, output_file);
120 fclose(output_file);
121 output_file = NULL;
122
123#ifdef DEBUG_GCDAPROFILING
Nick Lewycky1790c9c2011-04-26 03:54:16 +0000124 printf("llvmgcda: -----\n");
Nick Lewyckyb1928702011-04-16 01:20:23 +0000125#endif
126}