| Daniel Dunbar | fb16114 | 2011-11-17 00:12:10 +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 | |* |
| 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 | |* |
| 21 | \*===----------------------------------------------------------------------===*/ |
| 22 | |
| Xinliang David Li | aeff151 | 2016-07-15 18:48:14 +0000 | [diff] [blame] | 23 | #include "InstrProfilingPort.h" |
| Xinliang David Li | fe9ecc9 | 2016-07-18 16:16:12 +0000 | [diff] [blame] | 24 | #include "InstrProfilingUtil.h" |
| Diego Novillo | eae9514 | 2015-07-09 17:21:52 +0000 | [diff] [blame] | 25 | |
| Chandler Carruth | a26c814 | 2013-06-25 00:37:32 +0000 | [diff] [blame] | 26 | #include <errno.h> |
| Bill Wendling | 353fbd3 | 2013-05-23 07:18:59 +0000 | [diff] [blame] | 27 | #include <fcntl.h> |
| Daniel Dunbar | fb16114 | 2011-11-17 00:12:10 +0000 | [diff] [blame] | 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| Nathan Slingerland | ba86c92 | 2016-01-05 17:27:01 +0000 | [diff] [blame] | 31 | |
| 32 | #if defined(_WIN32) |
| 33 | #include "WindowsMMap.h" |
| 34 | #else |
| Bill Wendling | 353fbd3 | 2013-05-23 07:18:59 +0000 | [diff] [blame] | 35 | #include <sys/mman.h> |
| Justin Bogner | 7759f18 | 2015-04-03 19:10:35 +0000 | [diff] [blame] | 36 | #include <sys/file.h> |
| Xinliang David Li | c5307c6 | 2016-07-25 23:12:53 +0000 | [diff] [blame] | 37 | #ifndef MAP_FILE |
| 38 | #define MAP_FILE 0 |
| 39 | #endif |
| Nathan Slingerland | ba86c92 | 2016-01-05 17:27:01 +0000 | [diff] [blame] | 40 | #endif |
| Daniel Dunbar | fb16114 | 2011-11-17 00:12:10 +0000 | [diff] [blame] | 41 | |
| Nico Weber | 82210fb | 2016-01-19 22:02:12 +0000 | [diff] [blame] | 42 | #if defined(__FreeBSD__) && defined(__i386__) |
| 43 | #define I386_FREEBSD 1 |
| 44 | #else |
| 45 | #define I386_FREEBSD 0 |
| 46 | #endif |
| Viktor Kutuzov | f31e536 | 2014-03-10 16:50:53 +0000 | [diff] [blame] | 47 | |
| Viktor Kutuzov | f31e536 | 2014-03-10 16:50:53 +0000 | [diff] [blame] | 48 | #if !defined(_MSC_VER) && !I386_FREEBSD |
| Daniel Dunbar | fb16114 | 2011-11-17 00:12:10 +0000 | [diff] [blame] | 49 | #include <stdint.h> |
| Viktor Kutuzov | f31e536 | 2014-03-10 16:50:53 +0000 | [diff] [blame] | 50 | #endif |
| 51 | |
| 52 | #if defined(_MSC_VER) |
| Nathan Slingerland | ba86c92 | 2016-01-05 17:27:01 +0000 | [diff] [blame] | 53 | typedef unsigned char uint8_t; |
| Daniel Dunbar | fb16114 | 2011-11-17 00:12:10 +0000 | [diff] [blame] | 54 | typedef unsigned int uint32_t; |
| Duncan P. N. Exon Smith | 61c9733 | 2014-03-19 22:10:24 +0000 | [diff] [blame] | 55 | typedef unsigned long long uint64_t; |
| Viktor Kutuzov | f31e536 | 2014-03-10 16:50:53 +0000 | [diff] [blame] | 56 | #elif I386_FREEBSD |
| 57 | /* System headers define 'size_t' incorrectly on x64 FreeBSD (prior to |
| 58 | * FreeBSD 10, r232261) when compiled in 32-bit mode. |
| 59 | */ |
| 60 | typedef unsigned char uint8_t; |
| 61 | typedef unsigned int uint32_t; |
| 62 | typedef unsigned long long uint64_t; |
| Daniel Dunbar | fb16114 | 2011-11-17 00:12:10 +0000 | [diff] [blame] | 63 | #endif |
| 64 | |
| 65 | /* #define DEBUG_GCDAPROFILING */ |
| 66 | |
| 67 | /* |
| 68 | * --- GCOV file format I/O primitives --- |
| 69 | */ |
| 70 | |
| Bill Wendling | 2428f16 | 2013-03-18 22:59:47 +0000 | [diff] [blame] | 71 | /* |
| Chandler Carruth | a26c814 | 2013-06-25 00:37:32 +0000 | [diff] [blame] | 72 | * The current file name we're outputting. Used primarily for error logging. |
| 73 | */ |
| 74 | static char *filename = NULL; |
| 75 | |
| 76 | /* |
| Bill Wendling | 2428f16 | 2013-03-18 22:59:47 +0000 | [diff] [blame] | 77 | * The current file we're outputting. |
| 78 | */ |
| Daniel Dunbar | fb16114 | 2011-11-17 00:12:10 +0000 | [diff] [blame] | 79 | static FILE *output_file = NULL; |
| 80 | |
| Bill Wendling | 2428f16 | 2013-03-18 22:59:47 +0000 | [diff] [blame] | 81 | /* |
| Bill Wendling | c054086 | 2013-05-15 21:31:22 +0000 | [diff] [blame] | 82 | * Buffer that we write things into. |
| 83 | */ |
| Bill Wendling | b7e7a38 | 2013-05-23 18:18:31 +0000 | [diff] [blame] | 84 | #define WRITE_BUFFER_SIZE (128 * 1024) |
| 85 | static char *write_buffer = NULL; |
| Bill Wendling | 353fbd3 | 2013-05-23 07:18:59 +0000 | [diff] [blame] | 86 | static uint64_t cur_buffer_size = 0; |
| 87 | static uint64_t cur_pos = 0; |
| 88 | static uint64_t file_size = 0; |
| Bill Wendling | c054086 | 2013-05-15 21:31:22 +0000 | [diff] [blame] | 89 | static int new_file = 0; |
| Bill Wendling | 353fbd3 | 2013-05-23 07:18:59 +0000 | [diff] [blame] | 90 | static int fd = -1; |
| Bill Wendling | c054086 | 2013-05-15 21:31:22 +0000 | [diff] [blame] | 91 | |
| 92 | /* |
| Bill Wendling | e647659 | 2013-03-19 21:01:19 +0000 | [diff] [blame] | 93 | * A list of functions to write out the data. |
| 94 | */ |
| Alex Lorenz | fbac1ae | 2017-08-31 13:23:24 +0000 | [diff] [blame^] | 95 | typedef void (*writeout_fn)(void); |
| Bill Wendling | e647659 | 2013-03-19 21:01:19 +0000 | [diff] [blame] | 96 | |
| 97 | struct writeout_fn_node { |
| 98 | writeout_fn fn; |
| 99 | struct writeout_fn_node *next; |
| 100 | }; |
| 101 | |
| Bill Wendling | c054086 | 2013-05-15 21:31:22 +0000 | [diff] [blame] | 102 | static struct writeout_fn_node *writeout_fn_head = NULL; |
| 103 | static struct writeout_fn_node *writeout_fn_tail = NULL; |
| Bill Wendling | e647659 | 2013-03-19 21:01:19 +0000 | [diff] [blame] | 104 | |
| 105 | /* |
| Bill Wendling | 2428f16 | 2013-03-18 22:59:47 +0000 | [diff] [blame] | 106 | * A list of flush functions that our __gcov_flush() function should call. |
| 107 | */ |
| Alex Lorenz | fbac1ae | 2017-08-31 13:23:24 +0000 | [diff] [blame^] | 108 | typedef void (*flush_fn)(void); |
| Bill Wendling | 2428f16 | 2013-03-18 22:59:47 +0000 | [diff] [blame] | 109 | |
| 110 | struct flush_fn_node { |
| 111 | flush_fn fn; |
| 112 | struct flush_fn_node *next; |
| 113 | }; |
| 114 | |
| Bill Wendling | c054086 | 2013-05-15 21:31:22 +0000 | [diff] [blame] | 115 | static struct flush_fn_node *flush_fn_head = NULL; |
| 116 | static struct flush_fn_node *flush_fn_tail = NULL; |
| Bill Wendling | 2428f16 | 2013-03-18 22:59:47 +0000 | [diff] [blame] | 117 | |
| Bill Wendling | 353fbd3 | 2013-05-23 07:18:59 +0000 | [diff] [blame] | 118 | static void resize_write_buffer(uint64_t size) { |
| 119 | if (!new_file) return; |
| 120 | size += cur_pos; |
| 121 | if (size <= cur_buffer_size) return; |
| Bill Wendling | b7e7a38 | 2013-05-23 18:18:31 +0000 | [diff] [blame] | 122 | size = (size - 1) / WRITE_BUFFER_SIZE + 1; |
| 123 | size *= WRITE_BUFFER_SIZE; |
| 124 | write_buffer = realloc(write_buffer, size); |
| Bill Wendling | 353fbd3 | 2013-05-23 07:18:59 +0000 | [diff] [blame] | 125 | cur_buffer_size = size; |
| Daniel Dunbar | fb16114 | 2011-11-17 00:12:10 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| Bill Wendling | c054086 | 2013-05-15 21:31:22 +0000 | [diff] [blame] | 128 | static void write_bytes(const char *s, size_t len) { |
| Bill Wendling | 353fbd3 | 2013-05-23 07:18:59 +0000 | [diff] [blame] | 129 | resize_write_buffer(len); |
| Bill Wendling | b7e7a38 | 2013-05-23 18:18:31 +0000 | [diff] [blame] | 130 | memcpy(&write_buffer[cur_pos], s, len); |
| Bill Wendling | 353fbd3 | 2013-05-23 07:18:59 +0000 | [diff] [blame] | 131 | cur_pos += len; |
| Bill Wendling | c054086 | 2013-05-15 21:31:22 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static void write_32bit_value(uint32_t i) { |
| 135 | write_bytes((char*)&i, 4); |
| 136 | } |
| 137 | |
| 138 | static void write_64bit_value(uint64_t i) { |
| 139 | write_bytes((char*)&i, 8); |
| Daniel Dunbar | fb16114 | 2011-11-17 00:12:10 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static uint32_t length_of_string(const char *s) { |
| 143 | return (strlen(s) / 4) + 1; |
| 144 | } |
| 145 | |
| 146 | static void write_string(const char *s) { |
| 147 | uint32_t len = length_of_string(s); |
| Bill Wendling | c054086 | 2013-05-15 21:31:22 +0000 | [diff] [blame] | 148 | write_32bit_value(len); |
| 149 | write_bytes(s, strlen(s)); |
| 150 | write_bytes("\0\0\0\0", 4 - (strlen(s) % 4)); |
| Daniel Dunbar | fb16114 | 2011-11-17 00:12:10 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| Bill Wendling | c054086 | 2013-05-15 21:31:22 +0000 | [diff] [blame] | 153 | static uint32_t read_32bit_value() { |
| 154 | uint32_t val; |
| Bill Wendling | 66429fe | 2012-09-14 18:55:32 +0000 | [diff] [blame] | 155 | |
| Bill Wendling | c054086 | 2013-05-15 21:31:22 +0000 | [diff] [blame] | 156 | if (new_file) |
| Bill Wendling | 66429fe | 2012-09-14 18:55:32 +0000 | [diff] [blame] | 157 | return (uint32_t)-1; |
| 158 | |
| Bill Wendling | b7e7a38 | 2013-05-23 18:18:31 +0000 | [diff] [blame] | 159 | val = *(uint32_t*)&write_buffer[cur_pos]; |
| Bill Wendling | 353fbd3 | 2013-05-23 07:18:59 +0000 | [diff] [blame] | 160 | cur_pos += 4; |
| Bill Wendling | c054086 | 2013-05-15 21:31:22 +0000 | [diff] [blame] | 161 | return val; |
| Bill Wendling | 66429fe | 2012-09-14 18:55:32 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| Bill Wendling | c054086 | 2013-05-15 21:31:22 +0000 | [diff] [blame] | 164 | static uint64_t read_64bit_value() { |
| 165 | uint64_t val; |
| Bill Wendling | 66429fe | 2012-09-14 18:55:32 +0000 | [diff] [blame] | 166 | |
| Bill Wendling | c054086 | 2013-05-15 21:31:22 +0000 | [diff] [blame] | 167 | if (new_file) |
| Bill Wendling | 353fbd3 | 2013-05-23 07:18:59 +0000 | [diff] [blame] | 168 | return (uint64_t)-1; |
| Bill Wendling | c054086 | 2013-05-15 21:31:22 +0000 | [diff] [blame] | 169 | |
| Bill Wendling | b7e7a38 | 2013-05-23 18:18:31 +0000 | [diff] [blame] | 170 | val = *(uint64_t*)&write_buffer[cur_pos]; |
| Bill Wendling | 353fbd3 | 2013-05-23 07:18:59 +0000 | [diff] [blame] | 171 | cur_pos += 8; |
| Bill Wendling | c054086 | 2013-05-15 21:31:22 +0000 | [diff] [blame] | 172 | return val; |
| Bill Wendling | 66429fe | 2012-09-14 18:55:32 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| Daniel Dunbar | fb16114 | 2011-11-17 00:12:10 +0000 | [diff] [blame] | 175 | static char *mangle_filename(const char *orig_filename) { |
| Joerg Sonnenberger | c989455 | 2014-01-15 20:57:10 +0000 | [diff] [blame] | 176 | char *new_filename; |
| Xinliang David Li | fe9ecc9 | 2016-07-18 16:16:12 +0000 | [diff] [blame] | 177 | size_t prefix_len; |
| Joerg Sonnenberger | c989455 | 2014-01-15 20:57:10 +0000 | [diff] [blame] | 178 | int prefix_strip; |
| Xinliang David Li | fe9ecc9 | 2016-07-18 16:16:12 +0000 | [diff] [blame] | 179 | const char *prefix = lprofGetPathPrefix(&prefix_strip, &prefix_len); |
| Daniel Dunbar | fb16114 | 2011-11-17 00:12:10 +0000 | [diff] [blame] | 180 | |
| Xinliang David Li | fe9ecc9 | 2016-07-18 16:16:12 +0000 | [diff] [blame] | 181 | if (prefix == NULL) |
| Daniel Dunbar | fb16114 | 2011-11-17 00:12:10 +0000 | [diff] [blame] | 182 | return strdup(orig_filename); |
| 183 | |
| Xinliang David Li | fe9ecc9 | 2016-07-18 16:16:12 +0000 | [diff] [blame] | 184 | new_filename = malloc(prefix_len + 1 + strlen(orig_filename) + 1); |
| 185 | lprofApplyPathPrefix(new_filename, orig_filename, prefix, prefix_len, |
| 186 | prefix_strip); |
| Joerg Sonnenberger | c989455 | 2014-01-15 20:57:10 +0000 | [diff] [blame] | 187 | |
| 188 | return new_filename; |
| Daniel Dunbar | fb16114 | 2011-11-17 00:12:10 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| Chandler Carruth | a26c814 | 2013-06-25 00:37:32 +0000 | [diff] [blame] | 191 | static int map_file() { |
| Bill Wendling | 353fbd3 | 2013-05-23 07:18:59 +0000 | [diff] [blame] | 192 | fseek(output_file, 0L, SEEK_END); |
| 193 | file_size = ftell(output_file); |
| 194 | |
| Bill Wendling | a6ec07c | 2013-09-11 19:35:32 +0000 | [diff] [blame] | 195 | /* A size of 0 is invalid to `mmap'. Return a fail here, but don't issue an |
| Bill Wendling | 97c22c3 | 2013-09-09 22:25:46 +0000 | [diff] [blame] | 196 | * error message because it should "just work" for the user. */ |
| 197 | if (file_size == 0) |
| 198 | return -1; |
| 199 | |
| Bill Wendling | b7e7a38 | 2013-05-23 18:18:31 +0000 | [diff] [blame] | 200 | write_buffer = mmap(0, file_size, PROT_READ | PROT_WRITE, |
| 201 | MAP_FILE | MAP_SHARED, fd, 0); |
| Chandler Carruth | a26c814 | 2013-06-25 00:37:32 +0000 | [diff] [blame] | 202 | if (write_buffer == (void *)-1) { |
| 203 | int errnum = errno; |
| 204 | fprintf(stderr, "profiling: %s: cannot map: %s\n", filename, |
| 205 | strerror(errnum)); |
| 206 | return -1; |
| 207 | } |
| 208 | return 0; |
| Bill Wendling | 353fbd3 | 2013-05-23 07:18:59 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | static void unmap_file() { |
| Chandler Carruth | a26c814 | 2013-06-25 00:37:32 +0000 | [diff] [blame] | 212 | if (msync(write_buffer, file_size, MS_SYNC) == -1) { |
| 213 | int errnum = errno; |
| 214 | fprintf(stderr, "profiling: %s: cannot msync: %s\n", filename, |
| 215 | strerror(errnum)); |
| 216 | } |
| 217 | |
| 218 | /* We explicitly ignore errors from unmapping because at this point the data |
| 219 | * is written and we don't care. |
| 220 | */ |
| 221 | (void)munmap(write_buffer, file_size); |
| Bill Wendling | b7e7a38 | 2013-05-23 18:18:31 +0000 | [diff] [blame] | 222 | write_buffer = NULL; |
| Bill Wendling | 353fbd3 | 2013-05-23 07:18:59 +0000 | [diff] [blame] | 223 | file_size = 0; |
| 224 | } |
| 225 | |
| Daniel Dunbar | fb16114 | 2011-11-17 00:12:10 +0000 | [diff] [blame] | 226 | /* |
| 227 | * --- LLVM line counter API --- |
| 228 | */ |
| 229 | |
| 230 | /* A file in this case is a translation unit. Each .o file built with line |
| 231 | * profiling enabled will emit to a different file. Only one file may be |
| 232 | * started at a time. |
| 233 | */ |
| Yuchen Wu | ea7611c | 2013-11-20 04:14:48 +0000 | [diff] [blame] | 234 | void llvm_gcda_start_file(const char *orig_filename, const char version[4], |
| 235 | uint32_t checksum) { |
| Bill Wendling | 353fbd3 | 2013-05-23 07:18:59 +0000 | [diff] [blame] | 236 | const char *mode = "r+b"; |
| Chandler Carruth | a26c814 | 2013-06-25 00:37:32 +0000 | [diff] [blame] | 237 | filename = mangle_filename(orig_filename); |
| Bill Wendling | 66429fe | 2012-09-14 18:55:32 +0000 | [diff] [blame] | 238 | |
| 239 | /* Try just opening the file. */ |
| Bill Wendling | 353fbd3 | 2013-05-23 07:18:59 +0000 | [diff] [blame] | 240 | new_file = 0; |
| 241 | fd = open(filename, O_RDWR); |
| Bill Wendling | 74f987f | 2012-05-28 02:34:34 +0000 | [diff] [blame] | 242 | |
| Bill Wendling | 353fbd3 | 2013-05-23 07:18:59 +0000 | [diff] [blame] | 243 | if (fd == -1) { |
| Bill Wendling | 66429fe | 2012-09-14 18:55:32 +0000 | [diff] [blame] | 244 | /* Try opening the file, creating it if necessary. */ |
| Bill Wendling | c054086 | 2013-05-15 21:31:22 +0000 | [diff] [blame] | 245 | new_file = 1; |
| Bill Wendling | 353fbd3 | 2013-05-23 07:18:59 +0000 | [diff] [blame] | 246 | mode = "w+b"; |
| Bill Wendling | b7e7a38 | 2013-05-23 18:18:31 +0000 | [diff] [blame] | 247 | fd = open(filename, O_RDWR | O_CREAT, 0644); |
| Bill Wendling | 353fbd3 | 2013-05-23 07:18:59 +0000 | [diff] [blame] | 248 | if (fd == -1) { |
| Bill Wendling | 66429fe | 2012-09-14 18:55:32 +0000 | [diff] [blame] | 249 | /* Try creating the directories first then opening the file. */ |
| Diego Novillo | eae9514 | 2015-07-09 17:21:52 +0000 | [diff] [blame] | 250 | __llvm_profile_recursive_mkdir(filename); |
| Bill Wendling | b7e7a38 | 2013-05-23 18:18:31 +0000 | [diff] [blame] | 251 | fd = open(filename, O_RDWR | O_CREAT, 0644); |
| Chandler Carruth | a26c814 | 2013-06-25 00:37:32 +0000 | [diff] [blame] | 252 | if (fd == -1) { |
| Bill Wendling | 66429fe | 2012-09-14 18:55:32 +0000 | [diff] [blame] | 253 | /* Bah! It's hopeless. */ |
| Chandler Carruth | a26c814 | 2013-06-25 00:37:32 +0000 | [diff] [blame] | 254 | int errnum = errno; |
| 255 | fprintf(stderr, "profiling: %s: cannot open: %s\n", filename, |
| 256 | strerror(errnum)); |
| Bill Wendling | 66429fe | 2012-09-14 18:55:32 +0000 | [diff] [blame] | 257 | return; |
| 258 | } |
| Bill Wendling | 74f987f | 2012-05-28 02:34:34 +0000 | [diff] [blame] | 259 | } |
| 260 | } |
| Daniel Dunbar | fb16114 | 2011-11-17 00:12:10 +0000 | [diff] [blame] | 261 | |
| Justin Bogner | 8e1a2a3 | 2015-04-03 18:55:44 +0000 | [diff] [blame] | 262 | /* Try to flock the file to serialize concurrent processes writing out to the |
| 263 | * same GCDA. This can fail if the filesystem doesn't support it, but in that |
| 264 | * case we'll just carry on with the old racy behaviour and hope for the best. |
| 265 | */ |
| 266 | flock(fd, LOCK_EX); |
| Bill Wendling | 353fbd3 | 2013-05-23 07:18:59 +0000 | [diff] [blame] | 267 | output_file = fdopen(fd, mode); |
| Bill Wendling | c054086 | 2013-05-15 21:31:22 +0000 | [diff] [blame] | 268 | |
| 269 | /* Initialize the write buffer. */ |
| Bill Wendling | b7e7a38 | 2013-05-23 18:18:31 +0000 | [diff] [blame] | 270 | write_buffer = NULL; |
| Bill Wendling | 353fbd3 | 2013-05-23 07:18:59 +0000 | [diff] [blame] | 271 | cur_buffer_size = 0; |
| 272 | cur_pos = 0; |
| 273 | |
| 274 | if (new_file) { |
| Bill Wendling | b7e7a38 | 2013-05-23 18:18:31 +0000 | [diff] [blame] | 275 | resize_write_buffer(WRITE_BUFFER_SIZE); |
| 276 | memset(write_buffer, 0, WRITE_BUFFER_SIZE); |
| Bill Wendling | 353fbd3 | 2013-05-23 07:18:59 +0000 | [diff] [blame] | 277 | } else { |
| Chandler Carruth | a26c814 | 2013-06-25 00:37:32 +0000 | [diff] [blame] | 278 | if (map_file() == -1) { |
| 279 | /* mmap failed, try to recover by clobbering */ |
| 280 | new_file = 1; |
| 281 | write_buffer = NULL; |
| 282 | cur_buffer_size = 0; |
| 283 | resize_write_buffer(WRITE_BUFFER_SIZE); |
| 284 | memset(write_buffer, 0, WRITE_BUFFER_SIZE); |
| 285 | } |
| Bill Wendling | 353fbd3 | 2013-05-23 07:18:59 +0000 | [diff] [blame] | 286 | } |
| Bill Wendling | c054086 | 2013-05-15 21:31:22 +0000 | [diff] [blame] | 287 | |
| Yuchen Wu | ea7611c | 2013-11-20 04:14:48 +0000 | [diff] [blame] | 288 | /* gcda file, version, stamp checksum. */ |
| Bill Wendling | c054086 | 2013-05-15 21:31:22 +0000 | [diff] [blame] | 289 | write_bytes("adcg", 4); |
| 290 | write_bytes(version, 4); |
| Yuchen Wu | ea7611c | 2013-11-20 04:14:48 +0000 | [diff] [blame] | 291 | write_32bit_value(checksum); |
| Bill Wendling | c054086 | 2013-05-15 21:31:22 +0000 | [diff] [blame] | 292 | |
| Bill Wendling | 66429fe | 2012-09-14 18:55:32 +0000 | [diff] [blame] | 293 | #ifdef DEBUG_GCDAPROFILING |
| 294 | fprintf(stderr, "llvmgcda: [%s]\n", orig_filename); |
| 295 | #endif |
| Daniel Dunbar | fb16114 | 2011-11-17 00:12:10 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /* Given an array of pointers to counters (counters), increment the n-th one, |
| 299 | * where we're also given a pointer to n (predecessor). |
| 300 | */ |
| 301 | void llvm_gcda_increment_indirect_counter(uint32_t *predecessor, |
| 302 | uint64_t **counters) { |
| 303 | uint64_t *counter; |
| 304 | uint32_t pred; |
| 305 | |
| 306 | pred = *predecessor; |
| 307 | if (pred == 0xffffffff) |
| 308 | return; |
| 309 | counter = counters[pred]; |
| 310 | |
| 311 | /* Don't crash if the pred# is out of sync. This can happen due to threads, |
| 312 | or because of a TODO in GCOVProfiling.cpp buildEdgeLookupTable(). */ |
| 313 | if (counter) |
| 314 | ++*counter; |
| 315 | #ifdef DEBUG_GCDAPROFILING |
| 316 | else |
| Bill Wendling | f160847 | 2012-05-28 02:50:53 +0000 | [diff] [blame] | 317 | fprintf(stderr, |
| Bill Wendling | 66429fe | 2012-09-14 18:55:32 +0000 | [diff] [blame] | 318 | "llvmgcda: increment_indirect_counter counters=%08llx, pred=%u\n", |
| 319 | *counter, *predecessor); |
| Daniel Dunbar | fb16114 | 2011-11-17 00:12:10 +0000 | [diff] [blame] | 320 | #endif |
| 321 | } |
| 322 | |
| Nick Lewycky | 593eeb0 | 2013-03-09 01:33:12 +0000 | [diff] [blame] | 323 | void llvm_gcda_emit_function(uint32_t ident, const char *function_name, |
| Yuchen Wu | a5de343 | 2013-12-04 06:00:04 +0000 | [diff] [blame] | 324 | uint32_t func_checksum, uint8_t use_extra_checksum, |
| Yuchen Wu | ea7611c | 2013-11-20 04:14:48 +0000 | [diff] [blame] | 325 | uint32_t cfg_checksum) { |
| Nick Lewycky | 593eeb0 | 2013-03-09 01:33:12 +0000 | [diff] [blame] | 326 | uint32_t len = 2; |
| Bill Wendling | c054086 | 2013-05-15 21:31:22 +0000 | [diff] [blame] | 327 | |
| Nick Lewycky | 593eeb0 | 2013-03-09 01:33:12 +0000 | [diff] [blame] | 328 | if (use_extra_checksum) |
| 329 | len++; |
| Daniel Dunbar | fb16114 | 2011-11-17 00:12:10 +0000 | [diff] [blame] | 330 | #ifdef DEBUG_GCDAPROFILING |
| Nick Lewycky | 1052c99 | 2013-02-28 07:00:13 +0000 | [diff] [blame] | 331 | fprintf(stderr, "llvmgcda: function id=0x%08x name=%s\n", ident, |
| 332 | function_name ? function_name : "NULL"); |
| Daniel Dunbar | fb16114 | 2011-11-17 00:12:10 +0000 | [diff] [blame] | 333 | #endif |
| Bill Wendling | 74f987f | 2012-05-28 02:34:34 +0000 | [diff] [blame] | 334 | if (!output_file) return; |
| Daniel Dunbar | fb16114 | 2011-11-17 00:12:10 +0000 | [diff] [blame] | 335 | |
| Nick Lewycky | 1052c99 | 2013-02-28 07:00:13 +0000 | [diff] [blame] | 336 | /* function tag */ |
| Bill Wendling | c054086 | 2013-05-15 21:31:22 +0000 | [diff] [blame] | 337 | write_bytes("\0\0\0\1", 4); |
| Nick Lewycky | 1052c99 | 2013-02-28 07:00:13 +0000 | [diff] [blame] | 338 | if (function_name) |
| 339 | len += 1 + length_of_string(function_name); |
| Bill Wendling | c054086 | 2013-05-15 21:31:22 +0000 | [diff] [blame] | 340 | write_32bit_value(len); |
| 341 | write_32bit_value(ident); |
| Yuchen Wu | a5de343 | 2013-12-04 06:00:04 +0000 | [diff] [blame] | 342 | write_32bit_value(func_checksum); |
| Nick Lewycky | 593eeb0 | 2013-03-09 01:33:12 +0000 | [diff] [blame] | 343 | if (use_extra_checksum) |
| Yuchen Wu | ea7611c | 2013-11-20 04:14:48 +0000 | [diff] [blame] | 344 | write_32bit_value(cfg_checksum); |
| Nick Lewycky | 1052c99 | 2013-02-28 07:00:13 +0000 | [diff] [blame] | 345 | if (function_name) |
| 346 | write_string(function_name); |
| Daniel Dunbar | fb16114 | 2011-11-17 00:12:10 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) { |
| 350 | uint32_t i; |
| Bill Wendling | 66429fe | 2012-09-14 18:55:32 +0000 | [diff] [blame] | 351 | uint64_t *old_ctrs = NULL; |
| 352 | uint32_t val = 0; |
| Bill Wendling | 353fbd3 | 2013-05-23 07:18:59 +0000 | [diff] [blame] | 353 | uint64_t save_cur_pos = cur_pos; |
| Bill Wendling | 66429fe | 2012-09-14 18:55:32 +0000 | [diff] [blame] | 354 | |
| 355 | if (!output_file) return; |
| 356 | |
| Bill Wendling | c054086 | 2013-05-15 21:31:22 +0000 | [diff] [blame] | 357 | val = read_32bit_value(); |
| Bill Wendling | 66429fe | 2012-09-14 18:55:32 +0000 | [diff] [blame] | 358 | |
| Bill Wendling | 66429fe | 2012-09-14 18:55:32 +0000 | [diff] [blame] | 359 | if (val != (uint32_t)-1) { |
| 360 | /* There are counters present in the file. Merge them. */ |
| Bill Wendling | 66429fe | 2012-09-14 18:55:32 +0000 | [diff] [blame] | 361 | if (val != 0x01a10000) { |
| Justin Bogner | 8da47ac | 2014-08-18 20:47:32 +0000 | [diff] [blame] | 362 | fprintf(stderr, "profiling: %s: cannot merge previous GCDA file: " |
| 363 | "corrupt arc tag (0x%08x)\n", |
| 364 | filename, val); |
| Bill Wendling | 66429fe | 2012-09-14 18:55:32 +0000 | [diff] [blame] | 365 | return; |
| 366 | } |
| 367 | |
| Bill Wendling | c054086 | 2013-05-15 21:31:22 +0000 | [diff] [blame] | 368 | val = read_32bit_value(); |
| Bill Wendling | 66429fe | 2012-09-14 18:55:32 +0000 | [diff] [blame] | 369 | if (val == (uint32_t)-1 || val / 2 != num_counters) { |
| Justin Bogner | 8da47ac | 2014-08-18 20:47:32 +0000 | [diff] [blame] | 370 | fprintf(stderr, "profiling: %s: cannot merge previous GCDA file: " |
| 371 | "mismatched number of counters (%d)\n", |
| 372 | filename, val); |
| Bill Wendling | 66429fe | 2012-09-14 18:55:32 +0000 | [diff] [blame] | 373 | return; |
| 374 | } |
| 375 | |
| 376 | old_ctrs = malloc(sizeof(uint64_t) * num_counters); |
| Bill Wendling | c054086 | 2013-05-15 21:31:22 +0000 | [diff] [blame] | 377 | for (i = 0; i < num_counters; ++i) |
| 378 | old_ctrs[i] = read_64bit_value(); |
| Bill Wendling | 66429fe | 2012-09-14 18:55:32 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| Bill Wendling | 353fbd3 | 2013-05-23 07:18:59 +0000 | [diff] [blame] | 381 | cur_pos = save_cur_pos; |
| 382 | |
| Bill Wendling | 74f987f | 2012-05-28 02:34:34 +0000 | [diff] [blame] | 383 | /* Counter #1 (arcs) tag */ |
| Bill Wendling | c054086 | 2013-05-15 21:31:22 +0000 | [diff] [blame] | 384 | write_bytes("\0\0\xa1\1", 4); |
| 385 | write_32bit_value(num_counters * 2); |
| 386 | for (i = 0; i < num_counters; ++i) { |
| Bill Wendling | 9a9141a | 2013-04-22 03:36:22 +0000 | [diff] [blame] | 387 | counters[i] += (old_ctrs ? old_ctrs[i] : 0); |
| Bill Wendling | c054086 | 2013-05-15 21:31:22 +0000 | [diff] [blame] | 388 | write_64bit_value(counters[i]); |
| Bill Wendling | 9a9141a | 2013-04-22 03:36:22 +0000 | [diff] [blame] | 389 | } |
| Bill Wendling | 66429fe | 2012-09-14 18:55:32 +0000 | [diff] [blame] | 390 | |
| 391 | free(old_ctrs); |
| Daniel Dunbar | fb16114 | 2011-11-17 00:12:10 +0000 | [diff] [blame] | 392 | |
| 393 | #ifdef DEBUG_GCDAPROFILING |
| Bill Wendling | 66429fe | 2012-09-14 18:55:32 +0000 | [diff] [blame] | 394 | fprintf(stderr, "llvmgcda: %u arcs\n", num_counters); |
| Bill Wendling | 74f987f | 2012-05-28 02:34:34 +0000 | [diff] [blame] | 395 | for (i = 0; i < num_counters; ++i) |
| Bill Wendling | 66429fe | 2012-09-14 18:55:32 +0000 | [diff] [blame] | 396 | fprintf(stderr, "llvmgcda: %llu\n", (unsigned long long)counters[i]); |
| Daniel Dunbar | fb16114 | 2011-11-17 00:12:10 +0000 | [diff] [blame] | 397 | #endif |
| 398 | } |
| 399 | |
| Yuchen Wu | 61a1bfc | 2013-11-12 18:45:50 +0000 | [diff] [blame] | 400 | void llvm_gcda_summary_info() { |
| Matt Arsenault | d326252 | 2013-12-10 19:05:43 +0000 | [diff] [blame] | 401 | const uint32_t obj_summary_len = 9; /* Length for gcov compatibility. */ |
| Yuchen Wu | 61a1bfc | 2013-11-12 18:45:50 +0000 | [diff] [blame] | 402 | uint32_t i; |
| 403 | uint32_t runs = 1; |
| 404 | uint32_t val = 0; |
| 405 | uint64_t save_cur_pos = cur_pos; |
| 406 | |
| 407 | if (!output_file) return; |
| 408 | |
| 409 | val = read_32bit_value(); |
| 410 | |
| 411 | if (val != (uint32_t)-1) { |
| 412 | /* There are counters present in the file. Merge them. */ |
| 413 | if (val != 0xa1000000) { |
| Justin Bogner | 8da47ac | 2014-08-18 20:47:32 +0000 | [diff] [blame] | 414 | fprintf(stderr, "profiling: %s: cannot merge previous run count: " |
| 415 | "corrupt object tag (0x%08x)\n", |
| 416 | filename, val); |
| Yuchen Wu | 61a1bfc | 2013-11-12 18:45:50 +0000 | [diff] [blame] | 417 | return; |
| 418 | } |
| 419 | |
| Matt Arsenault | d326252 | 2013-12-10 19:05:43 +0000 | [diff] [blame] | 420 | val = read_32bit_value(); /* length */ |
| Yuchen Wu | 61a1bfc | 2013-11-12 18:45:50 +0000 | [diff] [blame] | 421 | if (val != obj_summary_len) { |
| Justin Bogner | 8da47ac | 2014-08-18 20:47:32 +0000 | [diff] [blame] | 422 | fprintf(stderr, "profiling: %s: cannot merge previous run count: " |
| 423 | "mismatched object length (%d)\n", |
| 424 | filename, val); |
| Yuchen Wu | 61a1bfc | 2013-11-12 18:45:50 +0000 | [diff] [blame] | 425 | return; |
| 426 | } |
| 427 | |
| Matt Arsenault | d326252 | 2013-12-10 19:05:43 +0000 | [diff] [blame] | 428 | read_32bit_value(); /* checksum, unused */ |
| 429 | read_32bit_value(); /* num, unused */ |
| 430 | runs += read_32bit_value(); /* Add previous run count to new counter. */ |
| Yuchen Wu | 61a1bfc | 2013-11-12 18:45:50 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | cur_pos = save_cur_pos; |
| 434 | |
| 435 | /* Object summary tag */ |
| 436 | write_bytes("\0\0\0\xa1", 4); |
| 437 | write_32bit_value(obj_summary_len); |
| Matt Arsenault | d326252 | 2013-12-10 19:05:43 +0000 | [diff] [blame] | 438 | write_32bit_value(0); /* checksum, unused */ |
| 439 | write_32bit_value(0); /* num, unused */ |
| Yuchen Wu | 61a1bfc | 2013-11-12 18:45:50 +0000 | [diff] [blame] | 440 | write_32bit_value(runs); |
| Matt Arsenault | d326252 | 2013-12-10 19:05:43 +0000 | [diff] [blame] | 441 | for (i = 3; i < obj_summary_len; ++i) |
| Yuchen Wu | 61a1bfc | 2013-11-12 18:45:50 +0000 | [diff] [blame] | 442 | write_32bit_value(0); |
| 443 | |
| 444 | /* Program summary tag */ |
| Matt Arsenault | d326252 | 2013-12-10 19:05:43 +0000 | [diff] [blame] | 445 | write_bytes("\0\0\0\xa3", 4); /* tag indicates 1 program */ |
| 446 | write_32bit_value(0); /* 0 length */ |
| Yuchen Wu | 61a1bfc | 2013-11-12 18:45:50 +0000 | [diff] [blame] | 447 | |
| 448 | #ifdef DEBUG_GCDAPROFILING |
| 449 | fprintf(stderr, "llvmgcda: %u runs\n", runs); |
| 450 | #endif |
| 451 | } |
| 452 | |
| Daniel Dunbar | fb16114 | 2011-11-17 00:12:10 +0000 | [diff] [blame] | 453 | void llvm_gcda_end_file() { |
| 454 | /* Write out EOF record. */ |
| Chandler Carruth | fa95cc9 | 2013-06-26 00:26:16 +0000 | [diff] [blame] | 455 | if (output_file) { |
| 456 | write_bytes("\0\0\0\0\0\0\0\0", 8); |
| Bill Wendling | 353fbd3 | 2013-05-23 07:18:59 +0000 | [diff] [blame] | 457 | |
| Chandler Carruth | fa95cc9 | 2013-06-26 00:26:16 +0000 | [diff] [blame] | 458 | if (new_file) { |
| 459 | fwrite(write_buffer, cur_pos, 1, output_file); |
| 460 | free(write_buffer); |
| 461 | } else { |
| 462 | unmap_file(); |
| 463 | } |
| 464 | |
| Justin Bogner | 8e1a2a3 | 2015-04-03 18:55:44 +0000 | [diff] [blame] | 465 | flock(fd, LOCK_UN); |
| Vedant Kumar | e55855f | 2016-03-05 20:10:25 +0000 | [diff] [blame] | 466 | fclose(output_file); |
| Chandler Carruth | fa95cc9 | 2013-06-26 00:26:16 +0000 | [diff] [blame] | 467 | output_file = NULL; |
| 468 | write_buffer = NULL; |
| Bill Wendling | 353fbd3 | 2013-05-23 07:18:59 +0000 | [diff] [blame] | 469 | } |
| Chandler Carruth | fa95cc9 | 2013-06-26 00:26:16 +0000 | [diff] [blame] | 470 | free(filename); |
| Daniel Dunbar | fb16114 | 2011-11-17 00:12:10 +0000 | [diff] [blame] | 471 | |
| 472 | #ifdef DEBUG_GCDAPROFILING |
| Bill Wendling | 66429fe | 2012-09-14 18:55:32 +0000 | [diff] [blame] | 473 | fprintf(stderr, "llvmgcda: -----\n"); |
| Daniel Dunbar | fb16114 | 2011-11-17 00:12:10 +0000 | [diff] [blame] | 474 | #endif |
| 475 | } |
| Bill Wendling | 2428f16 | 2013-03-18 22:59:47 +0000 | [diff] [blame] | 476 | |
| Bill Wendling | e647659 | 2013-03-19 21:01:19 +0000 | [diff] [blame] | 477 | void llvm_register_writeout_function(writeout_fn fn) { |
| 478 | struct writeout_fn_node *new_node = malloc(sizeof(struct writeout_fn_node)); |
| 479 | new_node->fn = fn; |
| 480 | new_node->next = NULL; |
| 481 | |
| 482 | if (!writeout_fn_head) { |
| 483 | writeout_fn_head = writeout_fn_tail = new_node; |
| 484 | } else { |
| 485 | writeout_fn_tail->next = new_node; |
| 486 | writeout_fn_tail = new_node; |
| 487 | } |
| 488 | } |
| 489 | |
| Reid Kleckner | 5c10fa3 | 2016-02-11 00:22:43 +0000 | [diff] [blame] | 490 | void llvm_writeout_files(void) { |
| Bill Wendling | e647659 | 2013-03-19 21:01:19 +0000 | [diff] [blame] | 491 | struct writeout_fn_node *curr = writeout_fn_head; |
| 492 | |
| 493 | while (curr) { |
| 494 | curr->fn(); |
| 495 | curr = curr->next; |
| 496 | } |
| 497 | } |
| 498 | |
| Reid Kleckner | 5c10fa3 | 2016-02-11 00:22:43 +0000 | [diff] [blame] | 499 | void llvm_delete_writeout_function_list(void) { |
| Bill Wendling | e647659 | 2013-03-19 21:01:19 +0000 | [diff] [blame] | 500 | while (writeout_fn_head) { |
| 501 | struct writeout_fn_node *node = writeout_fn_head; |
| 502 | writeout_fn_head = writeout_fn_head->next; |
| 503 | free(node); |
| 504 | } |
| 505 | |
| 506 | writeout_fn_head = writeout_fn_tail = NULL; |
| 507 | } |
| 508 | |
| Bill Wendling | 2428f16 | 2013-03-18 22:59:47 +0000 | [diff] [blame] | 509 | void llvm_register_flush_function(flush_fn fn) { |
| 510 | struct flush_fn_node *new_node = malloc(sizeof(struct flush_fn_node)); |
| 511 | new_node->fn = fn; |
| 512 | new_node->next = NULL; |
| 513 | |
| 514 | if (!flush_fn_head) { |
| 515 | flush_fn_head = flush_fn_tail = new_node; |
| 516 | } else { |
| 517 | flush_fn_tail->next = new_node; |
| 518 | flush_fn_tail = new_node; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | void __gcov_flush() { |
| 523 | struct flush_fn_node *curr = flush_fn_head; |
| 524 | |
| 525 | while (curr) { |
| 526 | curr->fn(); |
| 527 | curr = curr->next; |
| 528 | } |
| 529 | } |
| 530 | |
| Reid Kleckner | 5c10fa3 | 2016-02-11 00:22:43 +0000 | [diff] [blame] | 531 | void llvm_delete_flush_function_list(void) { |
| Bill Wendling | 2428f16 | 2013-03-18 22:59:47 +0000 | [diff] [blame] | 532 | while (flush_fn_head) { |
| 533 | struct flush_fn_node *node = flush_fn_head; |
| 534 | flush_fn_head = flush_fn_head->next; |
| 535 | free(node); |
| 536 | } |
| 537 | |
| 538 | flush_fn_head = flush_fn_tail = NULL; |
| 539 | } |
| Bill Wendling | 51a6ff5 | 2013-03-20 21:11:47 +0000 | [diff] [blame] | 540 | |
| 541 | void llvm_gcov_init(writeout_fn wfn, flush_fn ffn) { |
| 542 | static int atexit_ran = 0; |
| 543 | |
| 544 | if (wfn) |
| 545 | llvm_register_writeout_function(wfn); |
| 546 | |
| 547 | if (ffn) |
| 548 | llvm_register_flush_function(ffn); |
| 549 | |
| 550 | if (atexit_ran == 0) { |
| 551 | atexit_ran = 1; |
| 552 | |
| 553 | /* Make sure we write out the data and delete the data structures. */ |
| 554 | atexit(llvm_delete_flush_function_list); |
| 555 | atexit(llvm_delete_writeout_function_list); |
| 556 | atexit(llvm_writeout_files); |
| 557 | } |
| 558 | } |