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