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