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>
33 /* #define DEBUG_GCDAPROFILING */
36 * --- GCOV file format I/O primitives ---
39 static FILE *output_file = NULL;
41 static void write_int32(uint32_t i) {
42 fwrite(&i, 4, 1, output_file);
45 static void write_int64(uint64_t i) {
47 uint32_t hi = i >> 32;
52 static uint32_t length_of_string(const char *s) {
53 return (strlen(s) / 4) + 1;
56 static void write_string(const char *s) {
57 uint32_t len = length_of_string(s);
59 fwrite(s, strlen(s), 1, output_file);
60 fwrite("\0\0\0\0", 4 - (strlen(s) % 4), 1, output_file);
63 static char *mangle_filename(const char *orig_filename) {
64 /* TODO: handle GCOV_PREFIX_STRIP */
68 prefix = getenv("GCOV_PREFIX");
71 return strdup(orig_filename);
73 filename = malloc(strlen(prefix) + 1 + strlen(orig_filename) + 1);
74 strcpy(filename, prefix);
75 strcat(filename, "/");
76 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] != '/') continue;
86 pathname = malloc(i + 1);
87 strncpy(pathname, filename, i);
92 mkdir(pathname, 0750); /* some of these will fail, ignore it. */
99 * --- LLVM line counter API ---
102 /* A file in this case is a translation unit. Each .o file built with line
103 * profiling enabled will emit to a different file. Only one file may be
106 void llvm_gcda_start_file(const char *orig_filename) {
108 filename = mangle_filename(orig_filename);
109 recursive_mkdir(filename);
110 output_file = fopen(filename, "w+b");
113 const char *cptr = strrchr(orig_filename, '/');
114 output_file = fopen(cptr ? cptr + 1 : orig_filename, "w+b");
117 fprintf(stderr, "LLVM profiling runtime: cannot open '%s': ",
118 cptr ? cptr + 1 : orig_filename);
125 /* gcda file, version 404*, stamp LLVM. */
127 fwrite("adcg*204MVLL", 12, 1, output_file);
129 fwrite("adcg*404MVLL", 12, 1, output_file);
132 #ifdef DEBUG_GCDAPROFILING
133 printf("llvmgcda: [%s]\n", orig_filename);
139 /* Given an array of pointers to counters (counters), increment the n-th one,
140 * where we're also given a pointer to n (predecessor).
142 void llvm_gcda_increment_indirect_counter(uint32_t *predecessor,
143 uint64_t **counters) {
148 if (pred == 0xffffffff)
150 counter = counters[pred];
152 /* Don't crash if the pred# is out of sync. This can happen due to threads,
153 or because of a TODO in GCOVProfiling.cpp buildEdgeLookupTable(). */
156 #ifdef DEBUG_GCDAPROFILING
158 printf("llvmgcda: increment_indirect_counter counters=%x, pred=%u\n",
159 state_table_row, *predecessor);
163 void llvm_gcda_emit_function(uint32_t ident, const char *function_name) {
164 #ifdef DEBUG_GCDAPROFILING
165 printf("llvmgcda: function id=%x\n", ident);
167 if (!output_file) return;
170 fwrite("\0\0\0\1", 4, 1, output_file);
171 write_int32(3 + 1 + length_of_string(function_name));
175 write_string(function_name);
178 void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
181 /* Counter #1 (arcs) tag */
182 if (!output_file) return;
183 fwrite("\0\0\xa1\1", 4, 1, output_file);
184 write_int32(num_counters * 2);
185 for (i = 0; i < num_counters; ++i)
186 write_int64(counters[i]);
188 #ifdef DEBUG_GCDAPROFILING
189 printf("llvmgcda: %u arcs\n", num_counters);
190 for (i = 0; i < num_counters; ++i)
191 printf("llvmgcda: %llu\n", (unsigned long long)counters[i]);
195 void llvm_gcda_end_file() {
196 /* Write out EOF record. */
197 if (!output_file) return;
198 fwrite("\0\0\0\0\0\0\0\0", 8, 1, output_file);
202 #ifdef DEBUG_GCDAPROFILING
203 printf("llvmgcda: -----\n");