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