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