GCDAProfiling.c revision 7caabbd43ef867fa93317bef61924197b97ea1c7
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 23#ifndef __APPLE__ 24#include <errno.h> 25#endif 26 27#include <fcntl.h> 28#include <stdio.h> 29#include <stdlib.h> 30#include <string.h> 31#include <sys/stat.h> 32#include <sys/mman.h> 33#include <sys/types.h> 34#ifdef _WIN32 35#include <direct.h> 36#endif 37 38#ifndef _MSC_VER 39#include <stdint.h> 40#else 41typedef unsigned int uint32_t; 42typedef unsigned int uint64_t; 43#endif 44 45/* #define DEBUG_GCDAPROFILING */ 46 47/* 48 * --- GCOV file format I/O primitives --- 49 */ 50 51/* 52 * The current file name we're outputting. Used primarily for error logging. 53 */ 54static char *filename = NULL; 55 56/* 57 * The current file we're outputting. 58 */ 59static FILE *output_file = NULL; 60 61/* 62 * Buffer that we write things into. 63 */ 64#define WRITE_BUFFER_SIZE (128 * 1024) 65static char *write_buffer = NULL; 66static uint64_t cur_buffer_size = 0; 67static uint64_t cur_pos = 0; 68static uint64_t file_size = 0; 69static int new_file = 0; 70static int fd = -1; 71 72/* 73 * A list of functions to write out the data. 74 */ 75typedef void (*writeout_fn)(); 76 77struct writeout_fn_node { 78 writeout_fn fn; 79 struct writeout_fn_node *next; 80}; 81 82static struct writeout_fn_node *writeout_fn_head = NULL; 83static struct writeout_fn_node *writeout_fn_tail = NULL; 84 85/* 86 * A list of flush functions that our __gcov_flush() function should call. 87 */ 88typedef void (*flush_fn)(); 89 90struct flush_fn_node { 91 flush_fn fn; 92 struct flush_fn_node *next; 93}; 94 95static struct flush_fn_node *flush_fn_head = NULL; 96static struct flush_fn_node *flush_fn_tail = NULL; 97 98static void resize_write_buffer(uint64_t size) { 99 if (!new_file) return; 100 size += cur_pos; 101 if (size <= cur_buffer_size) return; 102 size = (size - 1) / WRITE_BUFFER_SIZE + 1; 103 size *= WRITE_BUFFER_SIZE; 104 write_buffer = realloc(write_buffer, size); 105 cur_buffer_size = size; 106} 107 108static void write_bytes(const char *s, size_t len) { 109 resize_write_buffer(len); 110 memcpy(&write_buffer[cur_pos], s, len); 111 cur_pos += len; 112} 113 114static void write_32bit_value(uint32_t i) { 115 write_bytes((char*)&i, 4); 116} 117 118static void write_64bit_value(uint64_t i) { 119 write_bytes((char*)&i, 8); 120} 121 122static uint32_t length_of_string(const char *s) { 123 return (strlen(s) / 4) + 1; 124} 125 126static void write_string(const char *s) { 127 uint32_t len = length_of_string(s); 128 write_32bit_value(len); 129 write_bytes(s, strlen(s)); 130 write_bytes("\0\0\0\0", 4 - (strlen(s) % 4)); 131} 132 133static uint32_t read_32bit_value() { 134 uint32_t val; 135 136 if (new_file) 137 return (uint32_t)-1; 138 139 val = *(uint32_t*)&write_buffer[cur_pos]; 140 cur_pos += 4; 141 return val; 142} 143 144static uint64_t read_64bit_value() { 145 uint64_t val; 146 147 if (new_file) 148 return (uint64_t)-1; 149 150 val = *(uint64_t*)&write_buffer[cur_pos]; 151 cur_pos += 8; 152 return val; 153} 154 155static char *mangle_filename(const char *orig_filename) { 156 char *filename = 0; 157 int prefix_len = 0; 158 int prefix_strip = 0; 159 int level = 0; 160 const char *fname = orig_filename, *ptr = NULL; 161 const char *prefix = getenv("GCOV_PREFIX"); 162 const char *prefix_strip_str = getenv("GCOV_PREFIX_STRIP"); 163 164 if (!prefix) 165 return strdup(orig_filename); 166 167 if (prefix_strip_str) { 168 prefix_strip = atoi(prefix_strip_str); 169 170 /* Negative GCOV_PREFIX_STRIP values are ignored */ 171 if (prefix_strip < 0) 172 prefix_strip = 0; 173 } 174 175 prefix_len = strlen(prefix); 176 filename = malloc(prefix_len + 1 + strlen(orig_filename) + 1); 177 strcpy(filename, prefix); 178 179 if (prefix[prefix_len - 1] != '/') 180 strcat(filename, "/"); 181 182 for (ptr = fname + 1; *ptr != '\0' && level < prefix_strip; ++ptr) { 183 if (*ptr != '/') continue; 184 fname = ptr; 185 ++level; 186 } 187 188 strcat(filename, fname); 189 190 return filename; 191} 192 193static void recursive_mkdir(char *filename) { 194 int i; 195 196 for (i = 1; filename[i] != '\0'; ++i) { 197 if (filename[i] != '/') continue; 198 filename[i] = '\0'; 199#ifdef _WIN32 200 _mkdir(filename); 201#else 202 mkdir(filename, 0755); /* Some of these will fail, ignore it. */ 203#endif 204 filename[i] = '/'; 205 } 206} 207 208static int map_file() { 209 fseek(output_file, 0L, SEEK_END); 210 file_size = ftell(output_file); 211 212 write_buffer = mmap(0, file_size, PROT_READ | PROT_WRITE, 213 MAP_FILE | MAP_SHARED, fd, 0); 214 if (write_buffer == (void *)-1) { 215#ifndef __APPLE__ 216 int errnum = errno; 217 fprintf(stderr, "profiling: %s: cannot map: %s\n", filename, 218 strerror(errnum)); 219#endif 220 return -1; 221 } 222 return 0; 223} 224 225static void unmap_file() { 226 if (msync(write_buffer, file_size, MS_SYNC) == -1) { 227#ifndef __APPLE__ 228 int errnum = errno; 229 fprintf(stderr, "profiling: %s: cannot msync: %s\n", filename, 230 strerror(errnum)); 231#endif 232 } 233 234 /* We explicitly ignore errors from unmapping because at this point the data 235 * is written and we don't care. 236 */ 237 (void)munmap(write_buffer, file_size); 238 write_buffer = NULL; 239 file_size = 0; 240} 241 242/* 243 * --- LLVM line counter API --- 244 */ 245 246/* A file in this case is a translation unit. Each .o file built with line 247 * profiling enabled will emit to a different file. Only one file may be 248 * started at a time. 249 */ 250void llvm_gcda_start_file(const char *orig_filename, const char version[4]) { 251 const char *mode = "r+b"; 252 filename = mangle_filename(orig_filename); 253 254 /* Try just opening the file. */ 255 new_file = 0; 256 fd = open(filename, O_RDWR); 257 258 if (fd == -1) { 259 /* Try opening the file, creating it if necessary. */ 260 new_file = 1; 261 mode = "w+b"; 262 fd = open(filename, O_RDWR | O_CREAT, 0644); 263 if (fd == -1) { 264 /* Try creating the directories first then opening the file. */ 265 recursive_mkdir(filename); 266 fd = open(filename, O_RDWR | O_CREAT, 0644); 267 if (fd == -1) { 268 /* Bah! It's hopeless. */ 269#ifndef __APPLE__ 270 int errnum = errno; 271 fprintf(stderr, "profiling: %s: cannot open: %s\n", filename, 272 strerror(errnum)); 273#endif 274 free(filename); 275 return; 276 } 277 } 278 } 279 280 output_file = fdopen(fd, mode); 281 282 /* Initialize the write buffer. */ 283 write_buffer = NULL; 284 cur_buffer_size = 0; 285 cur_pos = 0; 286 287 if (new_file) { 288 resize_write_buffer(WRITE_BUFFER_SIZE); 289 memset(write_buffer, 0, WRITE_BUFFER_SIZE); 290 } else { 291 if (map_file() == -1) { 292 /* mmap failed, try to recover by clobbering */ 293 new_file = 1; 294 write_buffer = NULL; 295 cur_buffer_size = 0; 296 resize_write_buffer(WRITE_BUFFER_SIZE); 297 memset(write_buffer, 0, WRITE_BUFFER_SIZE); 298 } 299 } 300 301 /* gcda file, version, stamp LLVM. */ 302 write_bytes("adcg", 4); 303 write_bytes(version, 4); 304 write_bytes("MVLL", 4); 305 306 free(filename); 307 308#ifdef DEBUG_GCDAPROFILING 309 fprintf(stderr, "llvmgcda: [%s]\n", orig_filename); 310#endif 311} 312 313/* Given an array of pointers to counters (counters), increment the n-th one, 314 * where we're also given a pointer to n (predecessor). 315 */ 316void llvm_gcda_increment_indirect_counter(uint32_t *predecessor, 317 uint64_t **counters) { 318 uint64_t *counter; 319 uint32_t pred; 320 321 pred = *predecessor; 322 if (pred == 0xffffffff) 323 return; 324 counter = counters[pred]; 325 326 /* Don't crash if the pred# is out of sync. This can happen due to threads, 327 or because of a TODO in GCOVProfiling.cpp buildEdgeLookupTable(). */ 328 if (counter) 329 ++*counter; 330#ifdef DEBUG_GCDAPROFILING 331 else 332 fprintf(stderr, 333 "llvmgcda: increment_indirect_counter counters=%08llx, pred=%u\n", 334 *counter, *predecessor); 335#endif 336} 337 338void llvm_gcda_emit_function(uint32_t ident, const char *function_name, 339 uint8_t use_extra_checksum) { 340 uint32_t len = 2; 341 342 if (use_extra_checksum) 343 len++; 344#ifdef DEBUG_GCDAPROFILING 345 fprintf(stderr, "llvmgcda: function id=0x%08x name=%s\n", ident, 346 function_name ? function_name : "NULL"); 347#endif 348 if (!output_file) return; 349 350 /* function tag */ 351 write_bytes("\0\0\0\1", 4); 352 if (function_name) 353 len += 1 + length_of_string(function_name); 354 write_32bit_value(len); 355 write_32bit_value(ident); 356 write_32bit_value(0); 357 if (use_extra_checksum) 358 write_32bit_value(0); 359 if (function_name) 360 write_string(function_name); 361} 362 363void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) { 364 uint32_t i; 365 uint64_t *old_ctrs = NULL; 366 uint32_t val = 0; 367 uint64_t save_cur_pos = cur_pos; 368 369 if (!output_file) return; 370 371 val = read_32bit_value(); 372 373 if (val != (uint32_t)-1) { 374 /* There are counters present in the file. Merge them. */ 375 if (val != 0x01a10000) { 376 fprintf(stderr, "profiling:invalid magic number (0x%08x)\n", val); 377 return; 378 } 379 380 val = read_32bit_value(); 381 if (val == (uint32_t)-1 || val / 2 != num_counters) { 382 fprintf(stderr, "profiling:invalid number of counters (%d)\n", val); 383 return; 384 } 385 386 old_ctrs = malloc(sizeof(uint64_t) * num_counters); 387 for (i = 0; i < num_counters; ++i) 388 old_ctrs[i] = read_64bit_value(); 389 } 390 391 cur_pos = save_cur_pos; 392 393 /* Counter #1 (arcs) tag */ 394 write_bytes("\0\0\xa1\1", 4); 395 write_32bit_value(num_counters * 2); 396 for (i = 0; i < num_counters; ++i) { 397 counters[i] += (old_ctrs ? old_ctrs[i] : 0); 398 write_64bit_value(counters[i]); 399 } 400 401 free(old_ctrs); 402 403#ifdef DEBUG_GCDAPROFILING 404 fprintf(stderr, "llvmgcda: %u arcs\n", num_counters); 405 for (i = 0; i < num_counters; ++i) 406 fprintf(stderr, "llvmgcda: %llu\n", (unsigned long long)counters[i]); 407#endif 408} 409 410void llvm_gcda_end_file() { 411 /* Write out EOF record. */ 412 if (!output_file) return; 413 write_bytes("\0\0\0\0\0\0\0\0", 8); 414 415 if (new_file) { 416 fwrite(write_buffer, cur_pos, 1, output_file); 417 free(write_buffer); 418 } else { 419 unmap_file(); 420 } 421 422 fclose(output_file); 423 output_file = NULL; 424 write_buffer = NULL; 425 426#ifdef DEBUG_GCDAPROFILING 427 fprintf(stderr, "llvmgcda: -----\n"); 428#endif 429} 430 431void llvm_register_writeout_function(writeout_fn fn) { 432 struct writeout_fn_node *new_node = malloc(sizeof(struct writeout_fn_node)); 433 new_node->fn = fn; 434 new_node->next = NULL; 435 436 if (!writeout_fn_head) { 437 writeout_fn_head = writeout_fn_tail = new_node; 438 } else { 439 writeout_fn_tail->next = new_node; 440 writeout_fn_tail = new_node; 441 } 442} 443 444void llvm_writeout_files() { 445 struct writeout_fn_node *curr = writeout_fn_head; 446 447 while (curr) { 448 curr->fn(); 449 curr = curr->next; 450 } 451} 452 453void llvm_delete_writeout_function_list() { 454 while (writeout_fn_head) { 455 struct writeout_fn_node *node = writeout_fn_head; 456 writeout_fn_head = writeout_fn_head->next; 457 free(node); 458 } 459 460 writeout_fn_head = writeout_fn_tail = NULL; 461} 462 463void llvm_register_flush_function(flush_fn fn) { 464 struct flush_fn_node *new_node = malloc(sizeof(struct flush_fn_node)); 465 new_node->fn = fn; 466 new_node->next = NULL; 467 468 if (!flush_fn_head) { 469 flush_fn_head = flush_fn_tail = new_node; 470 } else { 471 flush_fn_tail->next = new_node; 472 flush_fn_tail = new_node; 473 } 474} 475 476void __gcov_flush() { 477 struct flush_fn_node *curr = flush_fn_head; 478 479 while (curr) { 480 curr->fn(); 481 curr = curr->next; 482 } 483} 484 485void llvm_delete_flush_function_list() { 486 while (flush_fn_head) { 487 struct flush_fn_node *node = flush_fn_head; 488 flush_fn_head = flush_fn_head->next; 489 free(node); 490 } 491 492 flush_fn_head = flush_fn_tail = NULL; 493} 494 495void llvm_gcov_init(writeout_fn wfn, flush_fn ffn) { 496 static int atexit_ran = 0; 497 498 if (wfn) 499 llvm_register_writeout_function(wfn); 500 501 if (ffn) 502 llvm_register_flush_function(ffn); 503 504 if (atexit_ran == 0) { 505 atexit_ran = 1; 506 507 /* Make sure we write out the data and delete the data structures. */ 508 atexit(llvm_delete_flush_function_list); 509 atexit(llvm_delete_writeout_function_list); 510 atexit(llvm_writeout_files); 511 } 512} 513