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