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