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