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