Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 1 | /*===- 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 Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 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. |
| 20 | |* |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 21 | \*===----------------------------------------------------------------------===*/ |
| 22 | |
| 23 | #include "llvm/Support/DataTypes.h" |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
Nick Lewycky | 5e436b3 | 2011-05-04 22:34:29 +0000 | [diff] [blame] | 27 | #include <sys/stat.h> |
| 28 | #include <sys/types.h> |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 29 | |
Nick Lewycky | ece78a3 | 2011-04-16 04:25:36 +0000 | [diff] [blame] | 30 | /* #define DEBUG_GCDAPROFILING */ |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 31 | |
| 32 | /* |
| 33 | * --- GCOV file format I/O primitives --- |
| 34 | */ |
| 35 | |
| 36 | static FILE *output_file = NULL; |
| 37 | |
| 38 | static void write_int32(uint32_t i) { |
| 39 | fwrite(&i, 4, 1, output_file); |
| 40 | } |
| 41 | |
| 42 | static void write_int64(uint64_t i) { |
| 43 | uint32_t lo, hi; |
Benjamin Kramer | 571c0e9 | 2011-04-16 10:25:32 +0000 | [diff] [blame] | 44 | lo = i >> 0; |
| 45 | hi = i >> 32; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 46 | |
| 47 | write_int32(lo); |
| 48 | write_int32(hi); |
| 49 | } |
| 50 | |
Nick Lewycky | 5409a18 | 2011-05-05 02:46:38 +0000 | [diff] [blame] | 51 | static uint32_t length_of_string(const char *s) { |
Nick Lewycky | d363ff3 | 2011-05-05 23:52:18 +0000 | [diff] [blame^] | 52 | return (strlen(s) / 4) + 1; |
Nick Lewycky | 5409a18 | 2011-05-05 02:46:38 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static void write_string(const char *s) { |
| 56 | uint32_t len = length_of_string(s); |
| 57 | write_int32(len); |
| 58 | fwrite(s, strlen(s), 1, output_file); |
| 59 | fwrite("\0\0\0\0", 4 - (strlen(s) % 4), 1, output_file); |
| 60 | } |
| 61 | |
Nick Lewycky | 7a2ba2f | 2011-04-28 21:35:49 +0000 | [diff] [blame] | 62 | static char *mangle_filename(const char *orig_filename) { |
| 63 | /* TODO: handle GCOV_PREFIX_STRIP */ |
| 64 | const char *prefix; |
| 65 | char *filename = 0; |
| 66 | |
| 67 | prefix = getenv("GCOV_PREFIX"); |
| 68 | |
| 69 | if (!prefix) |
Nick Lewycky | d006ddc | 2011-05-04 03:58:45 +0000 | [diff] [blame] | 70 | return strdup(orig_filename); |
Nick Lewycky | 7a2ba2f | 2011-04-28 21:35:49 +0000 | [diff] [blame] | 71 | |
| 72 | filename = malloc(strlen(prefix) + 1 + strlen(orig_filename) + 1); |
| 73 | strcpy(filename, prefix); |
| 74 | strcat(filename, "/"); |
| 75 | strcat(filename, orig_filename); |
| 76 | |
| 77 | return filename; |
| 78 | } |
| 79 | |
Nick Lewycky | 5e436b3 | 2011-05-04 22:34:29 +0000 | [diff] [blame] | 80 | static void recursive_mkdir(const char *filename) { |
| 81 | char *pathname; |
| 82 | int i, e; |
| 83 | |
| 84 | for (i = 1, e = strlen(filename); i != e; ++i) { |
| 85 | if (filename[i] == '/') { |
| 86 | pathname = malloc(i + 1); |
| 87 | strncpy(pathname, filename, i); |
| 88 | pathname[i] = '\0'; |
| 89 | mkdir(pathname, 0750); /* some of these will fail, ignore it. */ |
| 90 | free(pathname); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 95 | /* |
| 96 | * --- LLVM line counter API --- |
| 97 | */ |
| 98 | |
| 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 |
| 101 | * started at a time. |
| 102 | */ |
Nick Lewycky | 7a2ba2f | 2011-04-28 21:35:49 +0000 | [diff] [blame] | 103 | void llvm_gcda_start_file(const char *orig_filename) { |
| 104 | char *filename; |
| 105 | filename = mangle_filename(orig_filename); |
Nick Lewycky | 5e436b3 | 2011-05-04 22:34:29 +0000 | [diff] [blame] | 106 | recursive_mkdir(filename); |
Nick Lewycky | 7a2ba2f | 2011-04-28 21:35:49 +0000 | [diff] [blame] | 107 | output_file = fopen(filename, "wb"); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 108 | |
| 109 | /* gcda file, version 404*, stamp LLVM. */ |
| 110 | fwrite("adcg*404MVLL", 12, 1, output_file); |
| 111 | |
| 112 | #ifdef DEBUG_GCDAPROFILING |
Nick Lewycky | 7a2ba2f | 2011-04-28 21:35:49 +0000 | [diff] [blame] | 113 | printf("llvmgcda: [%s]\n", orig_filename); |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 114 | #endif |
Nick Lewycky | 7a2ba2f | 2011-04-28 21:35:49 +0000 | [diff] [blame] | 115 | |
| 116 | free(filename); |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 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). |
| 121 | */ |
| 122 | void llvm_gcda_increment_indirect_counter(uint32_t *predecessor, |
| 123 | uint64_t **counters) { |
| 124 | uint64_t *counter; |
Nick Lewycky | 7a2ba2f | 2011-04-28 21:35:49 +0000 | [diff] [blame] | 125 | uint32_t pred; |
| 126 | |
| 127 | pred = *predecessor; |
| 128 | if (pred == 0xffffffff) |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 129 | return; |
Nick Lewycky | 7a2ba2f | 2011-04-28 21:35:49 +0000 | [diff] [blame] | 130 | counter = counters[pred]; |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 131 | |
| 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(). */ |
Nick Lewycky | 7a2ba2f | 2011-04-28 21:35:49 +0000 | [diff] [blame] | 134 | if (counter) |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 135 | ++*counter; |
| 136 | #ifdef DEBUG_GCDAPROFILING |
| 137 | else |
| 138 | printf("llvmgcda: increment_indirect_counter counters=%x, pred=%u\n", |
| 139 | state_table_row, *predecessor); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 140 | #endif |
| 141 | } |
| 142 | |
Nick Lewycky | 5409a18 | 2011-05-05 02:46:38 +0000 | [diff] [blame] | 143 | void llvm_gcda_emit_function(uint32_t ident, const char *function_name) { |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 144 | #ifdef DEBUG_GCDAPROFILING |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 145 | printf("llvmgcda: function id=%x\n", ident); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 146 | #endif |
| 147 | |
| 148 | /* function tag */ |
| 149 | fwrite("\0\0\0\1", 4, 1, output_file); |
Nick Lewycky | 5409a18 | 2011-05-05 02:46:38 +0000 | [diff] [blame] | 150 | write_int32(3 + 1 + length_of_string(function_name)); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 151 | write_int32(ident); |
| 152 | write_int32(0); |
Nick Lewycky | 5409a18 | 2011-05-05 02:46:38 +0000 | [diff] [blame] | 153 | write_int32(0); |
| 154 | write_string(function_name); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) { |
| 158 | uint32_t i; |
| 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]); |
| 164 | } |
| 165 | |
| 166 | #ifdef DEBUG_GCDAPROFILING |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 167 | printf("llvmgcda: %u arcs\n", num_counters); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 168 | for (i = 0; i < num_counters; ++i) { |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 169 | printf("llvmgcda: %llu\n", (unsigned long long)counters[i]); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 170 | } |
| 171 | #endif |
| 172 | } |
| 173 | |
| 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); |
| 177 | fclose(output_file); |
| 178 | output_file = NULL; |
| 179 | |
| 180 | #ifdef DEBUG_GCDAPROFILING |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 181 | printf("llvmgcda: -----\n"); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 182 | #endif |
| 183 | } |