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