1 /*===- GCDAProfiling.c - Support library for GCDA file emission -----------===*\
3 |* The LLVM Compiler Infrastructure
5 |* This file is distributed under the University of Illinois Open Source
6 |* License. See LICENSE.TXT for details.
8 |*===----------------------------------------------------------------------===*|
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.
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.
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.
21 \*===----------------------------------------------------------------------===*/
23 #if !defined(__Fuchsia__)
32 #define WIN32_LEAN_AND_MEAN
34 #include "WindowsMMap.h"
40 #if defined(__FreeBSD__) && defined(__i386__)
41 #define I386_FREEBSD 1
43 #define I386_FREEBSD 0
46 #if !defined(_MSC_VER) && !I386_FREEBSD
51 typedef unsigned char uint8_t;
52 typedef unsigned int uint32_t;
53 typedef unsigned long long uint64_t;
55 /* System headers define 'size_t' incorrectly on x64 FreeBSD (prior to
56 * FreeBSD 10, r232261) when compiled in 32-bit mode.
58 typedef unsigned char uint8_t;
59 typedef unsigned int uint32_t;
60 typedef unsigned long long uint64_t;
63 #include "InstrProfiling.h"
64 #include "InstrProfilingUtil.h"
66 /* #define DEBUG_GCDAPROFILING */
69 * --- GCOV file format I/O primitives ---
73 * The current file name we're outputting. Used primarily for error logging.
75 static char *filename = NULL;
78 * The current file we're outputting.
80 static FILE *output_file = NULL;
83 * Buffer that we write things into.
85 #define WRITE_BUFFER_SIZE (128 * 1024)
86 static unsigned char *write_buffer = NULL;
87 static uint64_t cur_buffer_size = 0;
88 static uint64_t cur_pos = 0;
89 static uint64_t file_size = 0;
90 static int new_file = 0;
92 static HANDLE mmap_handle = NULL;
96 typedef void (*fn_ptr)();
98 typedef void* dynamic_object_id;
99 // The address of this variable identifies a given dynamic object.
100 static dynamic_object_id current_id;
101 #define CURRENT_ID (¤t_id)
104 dynamic_object_id id;
106 struct fn_node* next;
110 struct fn_node *head, *tail;
114 * A list of functions to write out the data, shared between all dynamic objects.
116 struct fn_list writeout_fn_list;
119 * A list of flush functions that our __gcov_flush() function should call, shared between all dynamic objects.
121 struct fn_list flush_fn_list;
123 static void fn_list_insert(struct fn_list* list, fn_ptr fn) {
124 struct fn_node* new_node = malloc(sizeof(struct fn_node));
126 new_node->next = NULL;
127 new_node->id = CURRENT_ID;
130 list->head = list->tail = new_node;
132 list->tail->next = new_node;
133 list->tail = new_node;
137 static void fn_list_remove(struct fn_list* list) {
138 struct fn_node* curr = list->head;
139 struct fn_node* prev = NULL;
140 struct fn_node* next = NULL;
145 if (curr->id == CURRENT_ID) {
146 if (curr == list->head) {
150 if (curr == list->tail) {
167 static void resize_write_buffer(uint64_t size) {
168 if (!new_file) return;
170 if (size <= cur_buffer_size) return;
171 size = (size - 1) / WRITE_BUFFER_SIZE + 1;
172 size *= WRITE_BUFFER_SIZE;
173 write_buffer = realloc(write_buffer, size);
174 cur_buffer_size = size;
177 static void write_bytes(const char *s, size_t len) {
178 resize_write_buffer(len);
179 memcpy(&write_buffer[cur_pos], s, len);
183 static void write_32bit_value(uint32_t i) {
184 write_bytes((char*)&i, 4);
187 static void write_64bit_value(uint64_t i) {
188 // GCOV uses a lo-/hi-word format even on big-endian systems.
189 // See also GCOVBuffer::readInt64 in LLVM.
190 uint32_t lo = (uint32_t) i;
191 uint32_t hi = (uint32_t) (i >> 32);
192 write_32bit_value(lo);
193 write_32bit_value(hi);
196 static uint32_t length_of_string(const char *s) {
197 return (strlen(s) / 4) + 1;
200 static void write_string(const char *s) {
201 uint32_t len = length_of_string(s);
202 write_32bit_value(len);
203 write_bytes(s, strlen(s));
204 write_bytes("\0\0\0\0", 4 - (strlen(s) % 4));
207 static uint32_t read_32bit_value() {
213 val = *(uint32_t*)&write_buffer[cur_pos];
218 static uint32_t read_le_32bit_value() {
225 for (i = 0; i < 4; i++)
226 val |= write_buffer[cur_pos++] << (8*i);
230 static uint64_t read_64bit_value() {
231 // GCOV uses a lo-/hi-word format even on big-endian systems.
232 // See also GCOVBuffer::readInt64 in LLVM.
233 uint32_t lo = read_32bit_value();
234 uint32_t hi = read_32bit_value();
235 return ((uint64_t)hi << 32) | ((uint64_t)lo);
238 static char *mangle_filename(const char *orig_filename) {
242 const char *prefix = lprofGetPathPrefix(&prefix_strip, &prefix_len);
245 return strdup(orig_filename);
247 new_filename = malloc(prefix_len + 1 + strlen(orig_filename) + 1);
248 lprofApplyPathPrefix(new_filename, orig_filename, prefix, prefix_len,
254 static int map_file() {
255 fseek(output_file, 0L, SEEK_END);
256 file_size = ftell(output_file);
258 /* A size of 0 is invalid to `mmap'. Return a fail here, but don't issue an
259 * error message because it should "just work" for the user. */
266 mmap_fd = INVALID_HANDLE_VALUE;
268 mmap_fd = (HANDLE)_get_osfhandle(fd);
270 mmap_handle = CreateFileMapping(mmap_fd, NULL, PAGE_READWRITE, DWORD_HI(file_size), DWORD_LO(file_size), NULL);
271 if (mmap_handle == NULL) {
272 fprintf(stderr, "profiling: %s: cannot create file mapping: %d\n", filename,
277 write_buffer = MapViewOfFile(mmap_handle, FILE_MAP_WRITE, 0, 0, file_size);
278 if (write_buffer == NULL) {
279 fprintf(stderr, "profiling: %s: cannot map: %d\n", filename,
281 CloseHandle(mmap_handle);
285 write_buffer = mmap(0, file_size, PROT_READ | PROT_WRITE,
286 MAP_FILE | MAP_SHARED, fd, 0);
287 if (write_buffer == (void *)-1) {
289 fprintf(stderr, "profiling: %s: cannot map: %s\n", filename,
298 static void unmap_file() {
300 if (!FlushViewOfFile(write_buffer, file_size)) {
301 fprintf(stderr, "profiling: %s: cannot flush mapped view: %d\n", filename,
305 if (!UnmapViewOfFile(write_buffer)) {
306 fprintf(stderr, "profiling: %s: cannot unmap mapped view: %d\n", filename,
310 if (!CloseHandle(mmap_handle)) {
311 fprintf(stderr, "profiling: %s: cannot close file mapping handle: %d\n", filename,
317 if (msync(write_buffer, file_size, MS_SYNC) == -1) {
319 fprintf(stderr, "profiling: %s: cannot msync: %s\n", filename,
323 /* We explicitly ignore errors from unmapping because at this point the data
324 * is written and we don't care.
326 (void)munmap(write_buffer, file_size);
334 * --- LLVM line counter API ---
337 /* A file in this case is a translation unit. Each .o file built with line
338 * profiling enabled will emit to a different file. Only one file may be
341 COMPILER_RT_VISIBILITY
342 void llvm_gcda_start_file(const char *orig_filename, const char version[4],
344 const char *mode = "r+b";
345 filename = mangle_filename(orig_filename);
347 /* Try just opening the file. */
349 fd = open(filename, O_RDWR | O_BINARY);
352 /* Try opening the file, creating it if necessary. */
355 fd = open(filename, O_RDWR | O_CREAT | O_BINARY, 0644);
357 /* Try creating the directories first then opening the file. */
358 __llvm_profile_recursive_mkdir(filename);
359 fd = open(filename, O_RDWR | O_CREAT | O_BINARY, 0644);
361 /* Bah! It's hopeless. */
363 fprintf(stderr, "profiling: %s: cannot open: %s\n", filename,
370 /* Try to flock the file to serialize concurrent processes writing out to the
371 * same GCDA. This can fail if the filesystem doesn't support it, but in that
372 * case we'll just carry on with the old racy behaviour and hope for the best.
375 output_file = fdopen(fd, mode);
377 /* Initialize the write buffer. */
383 resize_write_buffer(WRITE_BUFFER_SIZE);
384 memset(write_buffer, 0, WRITE_BUFFER_SIZE);
386 if (map_file() == -1) {
387 /* mmap failed, try to recover by clobbering */
391 resize_write_buffer(WRITE_BUFFER_SIZE);
392 memset(write_buffer, 0, WRITE_BUFFER_SIZE);
396 /* gcda file, version, stamp checksum. */
397 write_bytes("adcg", 4);
398 write_bytes(version, 4);
399 write_32bit_value(checksum);
401 #ifdef DEBUG_GCDAPROFILING
402 fprintf(stderr, "llvmgcda: [%s]\n", orig_filename);
406 /* Given an array of pointers to counters (counters), increment the n-th one,
407 * where we're also given a pointer to n (predecessor).
409 COMPILER_RT_VISIBILITY
410 void llvm_gcda_increment_indirect_counter(uint32_t *predecessor,
411 uint64_t **counters) {
416 if (pred == 0xffffffff)
418 counter = counters[pred];
420 /* Don't crash if the pred# is out of sync. This can happen due to threads,
421 or because of a TODO in GCOVProfiling.cpp buildEdgeLookupTable(). */
424 #ifdef DEBUG_GCDAPROFILING
427 "llvmgcda: increment_indirect_counter counters=%08llx, pred=%u\n",
428 *counter, *predecessor);
432 COMPILER_RT_VISIBILITY
433 void llvm_gcda_emit_function(uint32_t ident, const char *function_name,
434 uint32_t func_checksum, uint8_t use_extra_checksum,
435 uint32_t cfg_checksum) {
438 if (use_extra_checksum)
440 #ifdef DEBUG_GCDAPROFILING
441 fprintf(stderr, "llvmgcda: function id=0x%08x name=%s\n", ident,
442 function_name ? function_name : "NULL");
444 if (!output_file) return;
447 write_bytes("\0\0\0\1", 4);
449 len += 1 + length_of_string(function_name);
450 write_32bit_value(len);
451 write_32bit_value(ident);
452 write_32bit_value(func_checksum);
453 if (use_extra_checksum)
454 write_32bit_value(cfg_checksum);
456 write_string(function_name);
459 COMPILER_RT_VISIBILITY
460 void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
462 uint64_t *old_ctrs = NULL;
464 uint64_t save_cur_pos = cur_pos;
466 if (!output_file) return;
468 val = read_le_32bit_value();
470 if (val != (uint32_t)-1) {
471 /* There are counters present in the file. Merge them. */
472 if (val != 0x01a10000) {
473 fprintf(stderr, "profiling: %s: cannot merge previous GCDA file: "
474 "corrupt arc tag (0x%08x)\n",
479 val = read_32bit_value();
480 if (val == (uint32_t)-1 || val / 2 != num_counters) {
481 fprintf(stderr, "profiling: %s: cannot merge previous GCDA file: "
482 "mismatched number of counters (%d)\n",
487 old_ctrs = malloc(sizeof(uint64_t) * num_counters);
488 for (i = 0; i < num_counters; ++i)
489 old_ctrs[i] = read_64bit_value();
492 cur_pos = save_cur_pos;
494 /* Counter #1 (arcs) tag */
495 write_bytes("\0\0\xa1\1", 4);
496 write_32bit_value(num_counters * 2);
497 for (i = 0; i < num_counters; ++i) {
498 counters[i] += (old_ctrs ? old_ctrs[i] : 0);
499 write_64bit_value(counters[i]);
504 #ifdef DEBUG_GCDAPROFILING
505 fprintf(stderr, "llvmgcda: %u arcs\n", num_counters);
506 for (i = 0; i < num_counters; ++i)
507 fprintf(stderr, "llvmgcda: %llu\n", (unsigned long long)counters[i]);
511 COMPILER_RT_VISIBILITY
512 void llvm_gcda_summary_info() {
513 const uint32_t obj_summary_len = 9; /* Length for gcov compatibility. */
516 static uint32_t run_counted = 0; // We only want to increase the run count once.
518 uint64_t save_cur_pos = cur_pos;
520 if (!output_file) return;
522 val = read_le_32bit_value();
524 if (val != (uint32_t)-1) {
525 /* There are counters present in the file. Merge them. */
526 if (val != 0xa1000000) {
527 fprintf(stderr, "profiling: %s: cannot merge previous run count: "
528 "corrupt object tag (0x%08x)\n",
533 val = read_32bit_value(); /* length */
534 if (val != obj_summary_len) {
535 fprintf(stderr, "profiling: %s: cannot merge previous run count: "
536 "mismatched object length (%d)\n",
541 read_32bit_value(); /* checksum, unused */
542 read_32bit_value(); /* num, unused */
543 uint32_t prev_runs = read_32bit_value();
544 /* Add previous run count to new counter, if not already counted before. */
545 runs = run_counted ? prev_runs : prev_runs + 1;
548 cur_pos = save_cur_pos;
550 /* Object summary tag */
551 write_bytes("\0\0\0\xa1", 4);
552 write_32bit_value(obj_summary_len);
553 write_32bit_value(0); /* checksum, unused */
554 write_32bit_value(0); /* num, unused */
555 write_32bit_value(runs);
556 for (i = 3; i < obj_summary_len; ++i)
557 write_32bit_value(0);
559 /* Program summary tag */
560 write_bytes("\0\0\0\xa3", 4); /* tag indicates 1 program */
561 write_32bit_value(0); /* 0 length */
565 #ifdef DEBUG_GCDAPROFILING
566 fprintf(stderr, "llvmgcda: %u runs\n", runs);
570 COMPILER_RT_VISIBILITY
571 void llvm_gcda_end_file() {
572 /* Write out EOF record. */
574 write_bytes("\0\0\0\0\0\0\0\0", 8);
577 fwrite(write_buffer, cur_pos, 1, output_file);
591 #ifdef DEBUG_GCDAPROFILING
592 fprintf(stderr, "llvmgcda: -----\n");
596 COMPILER_RT_VISIBILITY
597 void llvm_register_writeout_function(fn_ptr fn) {
598 fn_list_insert(&writeout_fn_list, fn);
601 COMPILER_RT_VISIBILITY
602 void llvm_writeout_files(void) {
603 struct fn_node *curr = writeout_fn_list.head;
606 if (curr->id == CURRENT_ID) {
613 COMPILER_RT_VISIBILITY
614 void llvm_delete_writeout_function_list(void) {
615 fn_list_remove(&writeout_fn_list);
618 COMPILER_RT_VISIBILITY
619 void llvm_register_flush_function(fn_ptr fn) {
620 fn_list_insert(&flush_fn_list, fn);
623 void __gcov_flush() {
624 struct fn_node* curr = flush_fn_list.head;
632 COMPILER_RT_VISIBILITY
633 void llvm_delete_flush_function_list(void) {
634 fn_list_remove(&flush_fn_list);
637 COMPILER_RT_VISIBILITY
638 void llvm_gcov_init(fn_ptr wfn, fn_ptr ffn) {
639 static int atexit_ran = 0;
642 llvm_register_writeout_function(wfn);
645 llvm_register_flush_function(ffn);
647 if (atexit_ran == 0) {
650 /* Make sure we write out the data and delete the data structures. */
651 atexit(llvm_delete_flush_function_list);
652 atexit(llvm_delete_writeout_function_list);
653 atexit(llvm_writeout_files);