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) 45 typedef unsigned int uint32_t; 46 typedef 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 */ 51 typedef unsigned char uint8_t; 52 typedef unsigned int uint32_t; 53 typedef unsigned long long uint64_t; 54 int 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 */ 66 static char *filename = NULL; 67 68 /* 69 * The current file we're outputting. 70 */ 71 static FILE *output_file = NULL; 72 73 /* 74 * Buffer that we write things into. 75 */ 76 #define WRITE_BUFFER_SIZE (128 * 1024) 77 static char *write_buffer = NULL; 78 static uint64_t cur_buffer_size = 0; 79 static uint64_t cur_pos = 0; 80 static uint64_t file_size = 0; 81 static int new_file = 0; 82 static int fd = -1; 83 84 /* 85 * A list of functions to write out the data. 86 */ 87 typedef void (*writeout_fn)(); 88 89 struct writeout_fn_node { 90 writeout_fn fn; 91 struct writeout_fn_node *next; 92 }; 93 94 static struct writeout_fn_node *writeout_fn_head = NULL; 95 static struct writeout_fn_node *writeout_fn_tail = NULL; 96 97 /* 98 * A list of flush functions that our __gcov_flush() function should call. 99 */ 100 typedef void (*flush_fn)(); 101 102 struct flush_fn_node { 103 flush_fn fn; 104 struct flush_fn_node *next; 105 }; 106 107 static struct flush_fn_node *flush_fn_head = NULL; 108 static struct flush_fn_node *flush_fn_tail = NULL; 109 110 static 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 120 static 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 126 static void write_32bit_value(uint32_t i) { 127 write_bytes((char*)&i, 4); 128 } 129 130 static void write_64bit_value(uint64_t i) { 131 write_bytes((char*)&i, 8); 132 } 133 134 static uint32_t length_of_string(const char *s) { 135 return (strlen(s) / 4) + 1; 136 } 137 138 static 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 145 static 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 156 static 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 167 static 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 211 static 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 226 static 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 246 static 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 */ 269 void 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 */ 331 void 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 353 void 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 379 void 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: %s: cannot merge previous GCDA file: " 393 "corrupt arc tag (0x%08x)\n", 394 filename, val); 395 return; 396 } 397 398 val = read_32bit_value(); 399 if (val == (uint32_t)-1 || val / 2 != num_counters) { 400 fprintf(stderr, "profiling: %s: cannot merge previous GCDA file: " 401 "mismatched number of counters (%d)\n", 402 filename, val); 403 return; 404 } 405 406 old_ctrs = malloc(sizeof(uint64_t) * num_counters); 407 for (i = 0; i < num_counters; ++i) 408 old_ctrs[i] = read_64bit_value(); 409 } 410 411 cur_pos = save_cur_pos; 412 413 /* Counter #1 (arcs) tag */ 414 write_bytes("\0\0\xa1\1", 4); 415 write_32bit_value(num_counters * 2); 416 for (i = 0; i < num_counters; ++i) { 417 counters[i] += (old_ctrs ? old_ctrs[i] : 0); 418 write_64bit_value(counters[i]); 419 } 420 421 free(old_ctrs); 422 423 #ifdef DEBUG_GCDAPROFILING 424 fprintf(stderr, "llvmgcda: %u arcs\n", num_counters); 425 for (i = 0; i < num_counters; ++i) 426 fprintf(stderr, "llvmgcda: %llu\n", (unsigned long long)counters[i]); 427 #endif 428 } 429 430 void llvm_gcda_summary_info() { 431 const uint32_t obj_summary_len = 9; /* Length for gcov compatibility. */ 432 uint32_t i; 433 uint32_t runs = 1; 434 uint32_t val = 0; 435 uint64_t save_cur_pos = cur_pos; 436 437 if (!output_file) return; 438 439 val = read_32bit_value(); 440 441 if (val != (uint32_t)-1) { 442 /* There are counters present in the file. Merge them. */ 443 if (val != 0xa1000000) { 444 fprintf(stderr, "profiling: %s: cannot merge previous run count: " 445 "corrupt object tag (0x%08x)\n", 446 filename, val); 447 return; 448 } 449 450 val = read_32bit_value(); /* length */ 451 if (val != obj_summary_len) { 452 fprintf(stderr, "profiling: %s: cannot merge previous run count: " 453 "mismatched object length (%d)\n", 454 filename, val); 455 return; 456 } 457 458 read_32bit_value(); /* checksum, unused */ 459 read_32bit_value(); /* num, unused */ 460 runs += read_32bit_value(); /* Add previous run count to new counter. */ 461 } 462 463 cur_pos = save_cur_pos; 464 465 /* Object summary tag */ 466 write_bytes("\0\0\0\xa1", 4); 467 write_32bit_value(obj_summary_len); 468 write_32bit_value(0); /* checksum, unused */ 469 write_32bit_value(0); /* num, unused */ 470 write_32bit_value(runs); 471 for (i = 3; i < obj_summary_len; ++i) 472 write_32bit_value(0); 473 474 /* Program summary tag */ 475 write_bytes("\0\0\0\xa3", 4); /* tag indicates 1 program */ 476 write_32bit_value(0); /* 0 length */ 477 478 #ifdef DEBUG_GCDAPROFILING 479 fprintf(stderr, "llvmgcda: %u runs\n", runs); 480 #endif 481 } 482 483 void llvm_gcda_end_file() { 484 /* Write out EOF record. */ 485 if (output_file) { 486 write_bytes("\0\0\0\0\0\0\0\0", 8); 487 488 if (new_file) { 489 fwrite(write_buffer, cur_pos, 1, output_file); 490 free(write_buffer); 491 } else { 492 unmap_file(); 493 } 494 495 fclose(output_file); 496 output_file = NULL; 497 write_buffer = NULL; 498 } 499 free(filename); 500 501 #ifdef DEBUG_GCDAPROFILING 502 fprintf(stderr, "llvmgcda: -----\n"); 503 #endif 504 } 505 506 void llvm_register_writeout_function(writeout_fn fn) { 507 struct writeout_fn_node *new_node = malloc(sizeof(struct writeout_fn_node)); 508 new_node->fn = fn; 509 new_node->next = NULL; 510 511 if (!writeout_fn_head) { 512 writeout_fn_head = writeout_fn_tail = new_node; 513 } else { 514 writeout_fn_tail->next = new_node; 515 writeout_fn_tail = new_node; 516 } 517 } 518 519 void llvm_writeout_files() { 520 struct writeout_fn_node *curr = writeout_fn_head; 521 522 while (curr) { 523 curr->fn(); 524 curr = curr->next; 525 } 526 } 527 528 void llvm_delete_writeout_function_list() { 529 while (writeout_fn_head) { 530 struct writeout_fn_node *node = writeout_fn_head; 531 writeout_fn_head = writeout_fn_head->next; 532 free(node); 533 } 534 535 writeout_fn_head = writeout_fn_tail = NULL; 536 } 537 538 void llvm_register_flush_function(flush_fn fn) { 539 struct flush_fn_node *new_node = malloc(sizeof(struct flush_fn_node)); 540 new_node->fn = fn; 541 new_node->next = NULL; 542 543 if (!flush_fn_head) { 544 flush_fn_head = flush_fn_tail = new_node; 545 } else { 546 flush_fn_tail->next = new_node; 547 flush_fn_tail = new_node; 548 } 549 } 550 551 void __gcov_flush() { 552 struct flush_fn_node *curr = flush_fn_head; 553 554 while (curr) { 555 curr->fn(); 556 curr = curr->next; 557 } 558 } 559 560 void llvm_delete_flush_function_list() { 561 while (flush_fn_head) { 562 struct flush_fn_node *node = flush_fn_head; 563 flush_fn_head = flush_fn_head->next; 564 free(node); 565 } 566 567 flush_fn_head = flush_fn_tail = NULL; 568 } 569 570 void llvm_gcov_init(writeout_fn wfn, flush_fn ffn) { 571 static int atexit_ran = 0; 572 573 if (wfn) 574 llvm_register_writeout_function(wfn); 575 576 if (ffn) 577 llvm_register_flush_function(ffn); 578 579 if (atexit_ran == 0) { 580 atexit_ran = 1; 581 582 /* Make sure we write out the data and delete the data structures. */ 583 atexit(llvm_delete_flush_function_list); 584 atexit(llvm_delete_writeout_function_list); 585 atexit(llvm_writeout_files); 586 } 587 } 588