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