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> |
| 27 | |
Nick Lewycky | ece78a3 | 2011-04-16 04:25:36 +0000 | [diff] [blame] | 28 | /* #define DEBUG_GCDAPROFILING */ |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * --- GCOV file format I/O primitives --- |
| 32 | */ |
| 33 | |
| 34 | static FILE *output_file = NULL; |
| 35 | |
| 36 | static void write_int32(uint32_t i) { |
| 37 | fwrite(&i, 4, 1, output_file); |
| 38 | } |
| 39 | |
| 40 | static void write_int64(uint64_t i) { |
| 41 | uint32_t lo, hi; |
Benjamin Kramer | 571c0e9 | 2011-04-16 10:25:32 +0000 | [diff] [blame] | 42 | lo = i >> 0; |
| 43 | hi = i >> 32; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 44 | |
| 45 | write_int32(lo); |
| 46 | write_int32(hi); |
| 47 | } |
| 48 | |
| 49 | /* |
| 50 | * --- LLVM line counter API --- |
| 51 | */ |
| 52 | |
| 53 | /* A file in this case is a translation unit. Each .o file built with line |
| 54 | * profiling enabled will emit to a different file. Only one file may be |
| 55 | * started at a time. |
| 56 | */ |
| 57 | void llvm_gcda_start_file(const char *filename) { |
| 58 | output_file = fopen(filename, "w+"); |
| 59 | |
| 60 | /* gcda file, version 404*, stamp LLVM. */ |
| 61 | fwrite("adcg*404MVLL", 12, 1, output_file); |
| 62 | |
| 63 | #ifdef DEBUG_GCDAPROFILING |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame^] | 64 | printf("llvmgcda: [%s]\n", filename); |
| 65 | #endif |
| 66 | } |
| 67 | |
| 68 | /* Given an array of pointers to counters (counters), increment the n-th one, |
| 69 | * where we're also given a pointer to n (predecessor). |
| 70 | */ |
| 71 | void llvm_gcda_increment_indirect_counter(uint32_t *predecessor, |
| 72 | uint64_t **counters) { |
| 73 | uint64_t *counter; |
| 74 | if (*predecessor == 0xffffffff) |
| 75 | return; |
| 76 | |
| 77 | /* Don't crash if the pred# is out of sync. This can happen due to threads, |
| 78 | or because of a TODO in GCOVProfiling.cpp buildEdgeLookupTable(). */ |
| 79 | if ((counter = counters[*predecessor])) |
| 80 | ++*counter; |
| 81 | #ifdef DEBUG_GCDAPROFILING |
| 82 | else |
| 83 | printf("llvmgcda: increment_indirect_counter counters=%x, pred=%u\n", |
| 84 | state_table_row, *predecessor); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 85 | #endif |
| 86 | } |
| 87 | |
| 88 | void llvm_gcda_emit_function(uint32_t ident) { |
| 89 | #ifdef DEBUG_GCDAPROFILING |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame^] | 90 | printf("llvmgcda: function id=%x\n", ident); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 91 | #endif |
| 92 | |
| 93 | /* function tag */ |
| 94 | fwrite("\0\0\0\1", 4, 1, output_file); |
| 95 | write_int32(2); |
| 96 | write_int32(ident); |
| 97 | write_int32(0); |
| 98 | } |
| 99 | |
| 100 | void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) { |
| 101 | uint32_t i; |
| 102 | /* counter #1 (arcs) tag */ |
| 103 | fwrite("\0\0\xa1\1", 4, 1, output_file); |
| 104 | write_int32(num_counters * 2); |
| 105 | for (i = 0; i < num_counters; ++i) { |
| 106 | write_int64(counters[i]); |
| 107 | } |
| 108 | |
| 109 | #ifdef DEBUG_GCDAPROFILING |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame^] | 110 | printf("llvmgcda: %u arcs\n", num_counters); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 111 | for (i = 0; i < num_counters; ++i) { |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame^] | 112 | printf("llvmgcda: %llu\n", (unsigned long long)counters[i]); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 113 | } |
| 114 | #endif |
| 115 | } |
| 116 | |
| 117 | void llvm_gcda_end_file() { |
| 118 | /* Write out EOF record. */ |
| 119 | fwrite("\0\0\0\0\0\0\0\0", 8, 1, output_file); |
| 120 | fclose(output_file); |
| 121 | output_file = NULL; |
| 122 | |
| 123 | #ifdef DEBUG_GCDAPROFILING |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame^] | 124 | printf("llvmgcda: -----\n"); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 125 | #endif |
| 126 | } |