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