blob: b01b948142ab3813c70caa7d01412fc43916896a [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>
Benjamin Kramer70b582a2011-10-08 15:49:19 +000029#ifdef _WIN32
Francois Pichet97938082011-05-26 04:55:20 +000030#include <direct.h>
31#endif
Nick Lewyckyb1928702011-04-16 01:20:23 +000032
Nick Lewyckyece78a32011-04-16 04:25:36 +000033/* #define DEBUG_GCDAPROFILING */
Nick Lewyckyb1928702011-04-16 01:20:23 +000034
35/*
36 * --- GCOV file format I/O primitives ---
37 */
38
39static FILE *output_file = NULL;
40
41static void write_int32(uint32_t i) {
42 fwrite(&i, 4, 1, output_file);
43}
44
45static void write_int64(uint64_t i) {
Bill Wendling53caba62012-05-25 21:57:59 +000046 uint32_t lo = i >> 0;
47 uint32_t hi = i >> 32;
Nick Lewyckyb1928702011-04-16 01:20:23 +000048 write_int32(lo);
49 write_int32(hi);
50}
51
Nick Lewycky5409a182011-05-05 02:46:38 +000052static uint32_t length_of_string(const char *s) {
Nick Lewyckyd363ff32011-05-05 23:52:18 +000053 return (strlen(s) / 4) + 1;
Nick Lewycky5409a182011-05-05 02:46:38 +000054}
55
56static void write_string(const char *s) {
57 uint32_t len = length_of_string(s);
58 write_int32(len);
59 fwrite(s, strlen(s), 1, output_file);
60 fwrite("\0\0\0\0", 4 - (strlen(s) % 4), 1, output_file);
61}
62
Nick Lewycky7a2ba2f2011-04-28 21:35:49 +000063static char *mangle_filename(const char *orig_filename) {
64 /* TODO: handle GCOV_PREFIX_STRIP */
65 const char *prefix;
66 char *filename = 0;
67
68 prefix = getenv("GCOV_PREFIX");
69
70 if (!prefix)
Nick Lewyckyd006ddc2011-05-04 03:58:45 +000071 return strdup(orig_filename);
Nick Lewycky7a2ba2f2011-04-28 21:35:49 +000072
73 filename = malloc(strlen(prefix) + 1 + strlen(orig_filename) + 1);
74 strcpy(filename, prefix);
75 strcat(filename, "/");
76 strcat(filename, orig_filename);
Nick Lewycky7a2ba2f2011-04-28 21:35:49 +000077 return filename;
78}
79
Nick Lewycky5e436b32011-05-04 22:34:29 +000080static void recursive_mkdir(const char *filename) {
81 char *pathname;
82 int i, e;
83
84 for (i = 1, e = strlen(filename); i != e; ++i) {
Bill Wendling43a699a2012-05-25 00:57:21 +000085 if (filename[i] != '/') continue;
86 pathname = malloc(i + 1);
87 strncpy(pathname, filename, i);
88 pathname[i] = '\0';
Eli Friedmanaeebc352011-06-23 18:24:27 +000089#ifdef _WIN32
Bill Wendling43a699a2012-05-25 00:57:21 +000090 _mkdir(pathname);
Francois Pichet97938082011-05-26 04:55:20 +000091#else
Bill Wendling43a699a2012-05-25 00:57:21 +000092 mkdir(pathname, 0750); /* some of these will fail, ignore it. */
Francois Pichet97938082011-05-26 04:55:20 +000093#endif
Bill Wendling43a699a2012-05-25 00:57:21 +000094 free(pathname);
Nick Lewycky5e436b32011-05-04 22:34:29 +000095 }
96}
97
Nick Lewyckyb1928702011-04-16 01:20:23 +000098/*
99 * --- LLVM line counter API ---
100 */
101
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
104 * started at a time.
105 */
Nick Lewycky7a2ba2f2011-04-28 21:35:49 +0000106void llvm_gcda_start_file(const char *orig_filename) {
107 char *filename;
108 filename = mangle_filename(orig_filename);
Nick Lewycky5e436b32011-05-04 22:34:29 +0000109 recursive_mkdir(filename);
Bill Wendlinge00c8fd2012-05-25 21:55:06 +0000110 output_file = fopen(filename, "w+b");
Nick Lewyckyb1928702011-04-16 01:20:23 +0000111
Bill Wendling66e30f82012-03-27 21:17:04 +0000112 if (!output_file) {
Bill Wendling7242a4f2012-03-28 01:30:51 +0000113 const char *cptr = strrchr(orig_filename, '/');
Bill Wendlinge00c8fd2012-05-25 21:55:06 +0000114 output_file = fopen(cptr ? cptr + 1 : orig_filename, "w+b");
Bill Wendling66e30f82012-03-27 21:17:04 +0000115
116 if (!output_file) {
Bill Wendlinge00c8fd2012-05-25 21:55:06 +0000117 fprintf(stderr, "LLVM profiling runtime: cannot open '%s': ",
Bill Wendlingd4ec0542012-03-28 02:39:06 +0000118 cptr ? cptr + 1 : orig_filename);
Bill Wendling66e30f82012-03-27 21:17:04 +0000119 perror("");
Bill Wendling717f7fa2012-05-25 00:55:38 +0000120 return;
Bill Wendling66e30f82012-03-27 21:17:04 +0000121 }
122 }
123
Nick Lewyckyb1928702011-04-16 01:20:23 +0000124 /* gcda file, version 404*, stamp LLVM. */
Bill Wendlingad759c52011-07-28 18:12:20 +0000125#ifdef __APPLE__
126 fwrite("adcg*204MVLL", 12, 1, output_file);
127#else
Nick Lewyckyb1928702011-04-16 01:20:23 +0000128 fwrite("adcg*404MVLL", 12, 1, output_file);
Bill Wendlingad759c52011-07-28 18:12:20 +0000129#endif
Nick Lewyckyb1928702011-04-16 01:20:23 +0000130
131#ifdef DEBUG_GCDAPROFILING
Nick Lewycky7a2ba2f2011-04-28 21:35:49 +0000132 printf("llvmgcda: [%s]\n", orig_filename);
Nick Lewycky1790c9c2011-04-26 03:54:16 +0000133#endif
Nick Lewycky7a2ba2f2011-04-28 21:35:49 +0000134
135 free(filename);
Nick Lewycky1790c9c2011-04-26 03:54:16 +0000136}
137
138/* Given an array of pointers to counters (counters), increment the n-th one,
139 * where we're also given a pointer to n (predecessor).
140 */
141void llvm_gcda_increment_indirect_counter(uint32_t *predecessor,
142 uint64_t **counters) {
143 uint64_t *counter;
Nick Lewycky7a2ba2f2011-04-28 21:35:49 +0000144 uint32_t pred;
145
146 pred = *predecessor;
147 if (pred == 0xffffffff)
Nick Lewycky1790c9c2011-04-26 03:54:16 +0000148 return;
Nick Lewycky7a2ba2f2011-04-28 21:35:49 +0000149 counter = counters[pred];
Nick Lewycky1790c9c2011-04-26 03:54:16 +0000150
151 /* Don't crash if the pred# is out of sync. This can happen due to threads,
152 or because of a TODO in GCOVProfiling.cpp buildEdgeLookupTable(). */
Nick Lewycky7a2ba2f2011-04-28 21:35:49 +0000153 if (counter)
Nick Lewycky1790c9c2011-04-26 03:54:16 +0000154 ++*counter;
155#ifdef DEBUG_GCDAPROFILING
156 else
157 printf("llvmgcda: increment_indirect_counter counters=%x, pred=%u\n",
158 state_table_row, *predecessor);
Nick Lewyckyb1928702011-04-16 01:20:23 +0000159#endif
160}
161
Nick Lewycky5409a182011-05-05 02:46:38 +0000162void llvm_gcda_emit_function(uint32_t ident, const char *function_name) {
Nick Lewyckyb1928702011-04-16 01:20:23 +0000163#ifdef DEBUG_GCDAPROFILING
Nick Lewycky1790c9c2011-04-26 03:54:16 +0000164 printf("llvmgcda: function id=%x\n", ident);
Nick Lewyckyb1928702011-04-16 01:20:23 +0000165#endif
Bill Wendling717f7fa2012-05-25 00:55:38 +0000166 if (!output_file) return;
Nick Lewyckyb1928702011-04-16 01:20:23 +0000167
168 /* function tag */
169 fwrite("\0\0\0\1", 4, 1, output_file);
Nick Lewycky5409a182011-05-05 02:46:38 +0000170 write_int32(3 + 1 + length_of_string(function_name));
Nick Lewyckyb1928702011-04-16 01:20:23 +0000171 write_int32(ident);
172 write_int32(0);
Nick Lewycky5409a182011-05-05 02:46:38 +0000173 write_int32(0);
174 write_string(function_name);
Nick Lewyckyb1928702011-04-16 01:20:23 +0000175}
176
177void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
178 uint32_t i;
Bill Wendling717f7fa2012-05-25 00:55:38 +0000179
180 /* Counter #1 (arcs) tag */
181 if (!output_file) return;
Nick Lewyckyb1928702011-04-16 01:20:23 +0000182 fwrite("\0\0\xa1\1", 4, 1, output_file);
183 write_int32(num_counters * 2);
Bill Wendling53caba62012-05-25 21:57:59 +0000184 for (i = 0; i < num_counters; ++i)
Nick Lewyckyb1928702011-04-16 01:20:23 +0000185 write_int64(counters[i]);
Nick Lewyckyb1928702011-04-16 01:20:23 +0000186
187#ifdef DEBUG_GCDAPROFILING
Nick Lewycky1790c9c2011-04-26 03:54:16 +0000188 printf("llvmgcda: %u arcs\n", num_counters);
Bill Wendling53caba62012-05-25 21:57:59 +0000189 for (i = 0; i < num_counters; ++i)
Nick Lewycky1790c9c2011-04-26 03:54:16 +0000190 printf("llvmgcda: %llu\n", (unsigned long long)counters[i]);
Nick Lewyckyb1928702011-04-16 01:20:23 +0000191#endif
192}
193
194void llvm_gcda_end_file() {
195 /* Write out EOF record. */
Bill Wendling717f7fa2012-05-25 00:55:38 +0000196 if (!output_file) return;
Nick Lewyckyb1928702011-04-16 01:20:23 +0000197 fwrite("\0\0\0\0\0\0\0\0", 8, 1, output_file);
198 fclose(output_file);
199 output_file = NULL;
200
201#ifdef DEBUG_GCDAPROFILING
Nick Lewycky1790c9c2011-04-26 03:54:16 +0000202 printf("llvmgcda: -----\n");
Nick Lewyckyb1928702011-04-16 01:20:23 +0000203#endif
204}