blob: dcf57ab9edcd5c627d66fe7f5a43d7a3e192994e [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>
Nick Lewycky5e436b32011-05-04 22:34:29 +000027#include <sys/stat.h>
28#include <sys/types.h>
Nick Lewyckyb1928702011-04-16 01:20:23 +000029
Nick Lewyckyece78a32011-04-16 04:25:36 +000030/* #define DEBUG_GCDAPROFILING */
Nick Lewyckyb1928702011-04-16 01:20:23 +000031
32/*
33 * --- GCOV file format I/O primitives ---
34 */
35
36static FILE *output_file = NULL;
37
38static void write_int32(uint32_t i) {
39 fwrite(&i, 4, 1, output_file);
40}
41
42static void write_int64(uint64_t i) {
43 uint32_t lo, hi;
Benjamin Kramer571c0e92011-04-16 10:25:32 +000044 lo = i >> 0;
45 hi = i >> 32;
Nick Lewyckyb1928702011-04-16 01:20:23 +000046
47 write_int32(lo);
48 write_int32(hi);
49}
50
Nick Lewycky7a2ba2f2011-04-28 21:35:49 +000051static char *mangle_filename(const char *orig_filename) {
52 /* TODO: handle GCOV_PREFIX_STRIP */
53 const char *prefix;
54 char *filename = 0;
55
56 prefix = getenv("GCOV_PREFIX");
57
58 if (!prefix)
Nick Lewyckyd006ddc2011-05-04 03:58:45 +000059 return strdup(orig_filename);
Nick Lewycky7a2ba2f2011-04-28 21:35:49 +000060
61 filename = malloc(strlen(prefix) + 1 + strlen(orig_filename) + 1);
62 strcpy(filename, prefix);
63 strcat(filename, "/");
64 strcat(filename, orig_filename);
65
66 return filename;
67}
68
Nick Lewycky5e436b32011-05-04 22:34:29 +000069static void recursive_mkdir(const char *filename) {
70 char *pathname;
71 int i, e;
72
73 for (i = 1, e = strlen(filename); i != e; ++i) {
74 if (filename[i] == '/') {
75 pathname = malloc(i + 1);
76 strncpy(pathname, filename, i);
77 pathname[i] = '\0';
78 mkdir(pathname, 0750); /* some of these will fail, ignore it. */
79 free(pathname);
80 }
81 }
82}
83
Nick Lewyckyb1928702011-04-16 01:20:23 +000084/*
85 * --- LLVM line counter API ---
86 */
87
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
90 * started at a time.
91 */
Nick Lewycky7a2ba2f2011-04-28 21:35:49 +000092void llvm_gcda_start_file(const char *orig_filename) {
93 char *filename;
94 filename = mangle_filename(orig_filename);
Nick Lewycky5e436b32011-05-04 22:34:29 +000095 recursive_mkdir(filename);
Nick Lewycky7a2ba2f2011-04-28 21:35:49 +000096 output_file = fopen(filename, "wb");
Nick Lewyckyb1928702011-04-16 01:20:23 +000097
98 /* gcda file, version 404*, stamp LLVM. */
99 fwrite("adcg*404MVLL", 12, 1, output_file);
100
101#ifdef DEBUG_GCDAPROFILING
Nick Lewycky7a2ba2f2011-04-28 21:35:49 +0000102 printf("llvmgcda: [%s]\n", orig_filename);
Nick Lewycky1790c9c2011-04-26 03:54:16 +0000103#endif
Nick Lewycky7a2ba2f2011-04-28 21:35:49 +0000104
105 free(filename);
Nick Lewycky1790c9c2011-04-26 03:54:16 +0000106}
107
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).
110 */
111void llvm_gcda_increment_indirect_counter(uint32_t *predecessor,
112 uint64_t **counters) {
113 uint64_t *counter;
Nick Lewycky7a2ba2f2011-04-28 21:35:49 +0000114 uint32_t pred;
115
116 pred = *predecessor;
117 if (pred == 0xffffffff)
Nick Lewycky1790c9c2011-04-26 03:54:16 +0000118 return;
Nick Lewycky7a2ba2f2011-04-28 21:35:49 +0000119 counter = counters[pred];
Nick Lewycky1790c9c2011-04-26 03:54:16 +0000120
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(). */
Nick Lewycky7a2ba2f2011-04-28 21:35:49 +0000123 if (counter)
Nick Lewycky1790c9c2011-04-26 03:54:16 +0000124 ++*counter;
125#ifdef DEBUG_GCDAPROFILING
126 else
127 printf("llvmgcda: increment_indirect_counter counters=%x, pred=%u\n",
128 state_table_row, *predecessor);
Nick Lewyckyb1928702011-04-16 01:20:23 +0000129#endif
130}
131
132void llvm_gcda_emit_function(uint32_t ident) {
133#ifdef DEBUG_GCDAPROFILING
Nick Lewycky1790c9c2011-04-26 03:54:16 +0000134 printf("llvmgcda: function id=%x\n", ident);
Nick Lewyckyb1928702011-04-16 01:20:23 +0000135#endif
136
137 /* function tag */
138 fwrite("\0\0\0\1", 4, 1, output_file);
139 write_int32(2);
140 write_int32(ident);
141 write_int32(0);
142}
143
144void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
145 uint32_t i;
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]);
151 }
152
153#ifdef DEBUG_GCDAPROFILING
Nick Lewycky1790c9c2011-04-26 03:54:16 +0000154 printf("llvmgcda: %u arcs\n", num_counters);
Nick Lewyckyb1928702011-04-16 01:20:23 +0000155 for (i = 0; i < num_counters; ++i) {
Nick Lewycky1790c9c2011-04-26 03:54:16 +0000156 printf("llvmgcda: %llu\n", (unsigned long long)counters[i]);
Nick Lewyckyb1928702011-04-16 01:20:23 +0000157 }
158#endif
159}
160
161void llvm_gcda_end_file() {
162 /* Write out EOF record. */
163 fwrite("\0\0\0\0\0\0\0\0", 8, 1, output_file);
164 fclose(output_file);
165 output_file = NULL;
166
167#ifdef DEBUG_GCDAPROFILING
Nick Lewycky1790c9c2011-04-26 03:54:16 +0000168 printf("llvmgcda: -----\n");
Nick Lewyckyb1928702011-04-16 01:20:23 +0000169#endif
170}