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