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 #include <sys/types.h>
30 /* #define DEBUG_GCDAPROFILING */
33 * --- GCOV file format I/O primitives ---
36 static FILE *output_file = NULL;
38 static void write_int32(uint32_t i) {
39 fwrite(&i, 4, 1, output_file);
42 static void write_int64(uint64_t i) {
51 static uint32_t length_of_string(const char *s) {
52 return (strlen(s) + 5) / 4;
55 static void write_string(const char *s) {
56 uint32_t len = length_of_string(s);
58 fwrite(s, strlen(s), 1, output_file);
59 fwrite("\0\0\0\0", 4 - (strlen(s) % 4), 1, output_file);
62 static char *mangle_filename(const char *orig_filename) {
63 /* TODO: handle GCOV_PREFIX_STRIP */
67 prefix = getenv("GCOV_PREFIX");
70 return strdup(orig_filename);
72 filename = malloc(strlen(prefix) + 1 + strlen(orig_filename) + 1);
73 strcpy(filename, prefix);
74 strcat(filename, "/");
75 strcat(filename, orig_filename);
80 static void recursive_mkdir(const char *filename) {
84 for (i = 1, e = strlen(filename); i != e; ++i) {
85 if (filename[i] == '/') {
86 pathname = malloc(i + 1);
87 strncpy(pathname, filename, i);
89 mkdir(pathname, 0750); /* some of these will fail, ignore it. */
96 * --- LLVM line counter API ---
99 /* A file in this case is a translation unit. Each .o file built with line
100 * profiling enabled will emit to a different file. Only one file may be
103 void llvm_gcda_start_file(const char *orig_filename) {
105 filename = mangle_filename(orig_filename);
106 recursive_mkdir(filename);
107 output_file = fopen(filename, "wb");
109 /* gcda file, version 404*, stamp LLVM. */
110 fwrite("adcg*404MVLL", 12, 1, output_file);
112 #ifdef DEBUG_GCDAPROFILING
113 printf("llvmgcda: [%s]\n", orig_filename);
119 /* Given an array of pointers to counters (counters), increment the n-th one,
120 * where we're also given a pointer to n (predecessor).
122 void llvm_gcda_increment_indirect_counter(uint32_t *predecessor,
123 uint64_t **counters) {
128 if (pred == 0xffffffff)
130 counter = counters[pred];
132 /* Don't crash if the pred# is out of sync. This can happen due to threads,
133 or because of a TODO in GCOVProfiling.cpp buildEdgeLookupTable(). */
136 #ifdef DEBUG_GCDAPROFILING
138 printf("llvmgcda: increment_indirect_counter counters=%x, pred=%u\n",
139 state_table_row, *predecessor);
143 void llvm_gcda_emit_function(uint32_t ident, const char *function_name) {
144 #ifdef DEBUG_GCDAPROFILING
145 printf("llvmgcda: function id=%x\n", ident);
149 fwrite("\0\0\0\1", 4, 1, output_file);
150 write_int32(3 + 1 + length_of_string(function_name));
154 write_string(function_name);
157 void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
159 /* counter #1 (arcs) tag */
160 fwrite("\0\0\xa1\1", 4, 1, output_file);
161 write_int32(num_counters * 2);
162 for (i = 0; i < num_counters; ++i) {
163 write_int64(counters[i]);
166 #ifdef DEBUG_GCDAPROFILING
167 printf("llvmgcda: %u arcs\n", num_counters);
168 for (i = 0; i < num_counters; ++i) {
169 printf("llvmgcda: %llu\n", (unsigned long long)counters[i]);
174 void llvm_gcda_end_file() {
175 /* Write out EOF record. */
176 fwrite("\0\0\0\0\0\0\0\0", 8, 1, output_file);
180 #ifdef DEBUG_GCDAPROFILING
181 printf("llvmgcda: -----\n");