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 char *mangle_filename(const char *orig_filename
) {
52 /* TODO: handle GCOV_PREFIX_STRIP */
56 prefix
= getenv("GCOV_PREFIX");
59 return strdup(orig_filename
);
61 filename
= malloc(strlen(prefix
) + 1 + strlen(orig_filename
) + 1);
62 strcpy(filename
, prefix
);
63 strcat(filename
, "/");
64 strcat(filename
, orig_filename
);
69 static void recursive_mkdir(const char *filename
) {
73 for (i
= 1, e
= strlen(filename
); i
!= e
; ++i
) {
74 if (filename
[i
] == '/') {
75 pathname
= malloc(i
+ 1);
76 strncpy(pathname
, filename
, i
);
78 mkdir(pathname
, 0750); /* some of these will fail, ignore it. */
85 * --- LLVM line counter API ---
88 /* A file in this case is a translation unit. Each .o file built with line
89 * profiling enabled will emit to a different file. Only one file may be
92 void llvm_gcda_start_file(const char *orig_filename
) {
94 filename
= mangle_filename(orig_filename
);
95 recursive_mkdir(filename
);
96 output_file
= fopen(filename
, "wb");
98 /* gcda file, version 404*, stamp LLVM. */
99 fwrite("adcg*404MVLL", 12, 1, output_file
);
101 #ifdef DEBUG_GCDAPROFILING
102 printf("llvmgcda: [%s]\n", orig_filename
);
108 /* Given an array of pointers to counters (counters), increment the n-th one,
109 * where we're also given a pointer to n (predecessor).
111 void llvm_gcda_increment_indirect_counter(uint32_t *predecessor
,
112 uint64_t **counters
) {
117 if (pred
== 0xffffffff)
119 counter
= counters
[pred
];
121 /* Don't crash if the pred# is out of sync. This can happen due to threads,
122 or because of a TODO in GCOVProfiling.cpp buildEdgeLookupTable(). */
125 #ifdef DEBUG_GCDAPROFILING
127 printf("llvmgcda: increment_indirect_counter counters=%x, pred=%u\n",
128 state_table_row
, *predecessor
);
132 void llvm_gcda_emit_function(uint32_t ident
) {
133 #ifdef DEBUG_GCDAPROFILING
134 printf("llvmgcda: function id=%x\n", ident
);
138 fwrite("\0\0\0\1", 4, 1, output_file
);
144 void llvm_gcda_emit_arcs(uint32_t num_counters
, uint64_t *counters
) {
146 /* counter #1 (arcs) tag */
147 fwrite("\0\0\xa1\1", 4, 1, output_file
);
148 write_int32(num_counters
* 2);
149 for (i
= 0; i
< num_counters
; ++i
) {
150 write_int64(counters
[i
]);
153 #ifdef DEBUG_GCDAPROFILING
154 printf("llvmgcda: %u arcs\n", num_counters
);
155 for (i
= 0; i
< num_counters
; ++i
) {
156 printf("llvmgcda: %llu\n", (unsigned long long)counters
[i
]);
161 void llvm_gcda_end_file() {
162 /* Write out EOF record. */
163 fwrite("\0\0\0\0\0\0\0\0", 8, 1, output_file
);
167 #ifdef DEBUG_GCDAPROFILING
168 printf("llvmgcda: -----\n");