1 /*===- GCDAProfiling.c - Support library for GCDA file emission -----------===*\
3 |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 |* See https://llvm.org/LICENSE.txt for license information.
5 |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 |*===----------------------------------------------------------------------===*|
9 |* This file implements the call back routines for the gcov profiling
10 |* instrumentation pass. Link against this library when running code through
11 |* the -insert-gcov-profiling LLVM pass.
13 |* We emit files in a corrupt version of GCOV's "gcda" file format. These files
14 |* are only close enough that LCOV will happily parse them. Anything that lcov
15 |* ignores is missing.
17 |* TODO: gcov is multi-process safe by having each exit open the existing file
18 |* and append to it. We'd like to achieve that and be thread-safe too.
20 \*===----------------------------------------------------------------------===*/
22 #if !defined(__Fuchsia__)
31 #define WIN32_LEAN_AND_MEAN
33 #include "WindowsMMap.h"
37 #include <sys/types.h>
41 #if defined(__FreeBSD__) && defined(__i386__)
42 #define I386_FREEBSD 1
44 #define I386_FREEBSD 0
47 #if !defined(_MSC_VER) && !I386_FREEBSD
52 typedef unsigned char uint8_t;
53 typedef unsigned int uint32_t;
54 typedef unsigned long long uint64_t;
56 /* System headers define 'size_t' incorrectly on x64 FreeBSD (prior to
57 * FreeBSD 10, r232261) when compiled in 32-bit mode.
59 typedef unsigned char uint8_t;
60 typedef unsigned int uint32_t;
61 typedef unsigned long long uint64_t;
64 #include "InstrProfiling.h"
65 #include "InstrProfilingUtil.h"
67 /* #define DEBUG_GCDAPROFILING */
70 * --- GCOV file format I/O primitives ---
74 * The current file name we're outputting. Used primarily for error logging.
76 static char *filename = NULL;
79 * The current file we're outputting.
81 static FILE *output_file = NULL;
84 * Buffer that we write things into.
86 #define WRITE_BUFFER_SIZE (128 * 1024)
87 static unsigned char *write_buffer = NULL;
88 static uint64_t cur_buffer_size = 0;
89 static uint64_t cur_pos = 0;
90 static uint64_t file_size = 0;
91 static int new_file = 0;
93 static HANDLE mmap_handle = NULL;
97 typedef void (*fn_ptr)();
99 typedef void* dynamic_object_id;
100 // The address of this variable identifies a given dynamic object.
101 static dynamic_object_id current_id;
102 #define CURRENT_ID (¤t_id)
105 dynamic_object_id id;
107 struct fn_node* next;
111 struct fn_node *head, *tail;
115 * A list of functions to write out the data, shared between all dynamic objects.
117 struct fn_list writeout_fn_list;
120 * A list of flush functions that our __gcov_flush() function should call, shared between all dynamic objects.
122 struct fn_list flush_fn_list;
125 * A list of reset functions, shared between all dynamic objects.
127 struct fn_list reset_fn_list;
129 static void fn_list_insert(struct fn_list* list, fn_ptr fn) {
130 struct fn_node* new_node = malloc(sizeof(struct fn_node));
132 new_node->next = NULL;
133 new_node->id = CURRENT_ID;
136 list->head = list->tail = new_node;
138 list->tail->next = new_node;
139 list->tail = new_node;
143 static void fn_list_remove(struct fn_list* list) {
144 struct fn_node* curr = list->head;
145 struct fn_node* prev = NULL;
146 struct fn_node* next = NULL;
151 if (curr->id == CURRENT_ID) {
152 if (curr == list->head) {
156 if (curr == list->tail) {
173 static void resize_write_buffer(uint64_t size) {
174 if (!new_file) return;
176 if (size <= cur_buffer_size) return;
177 size = (size - 1) / WRITE_BUFFER_SIZE + 1;
178 size *= WRITE_BUFFER_SIZE;
179 write_buffer = realloc(write_buffer, size);
180 cur_buffer_size = size;
183 static void write_bytes(const char *s, size_t len) {
184 resize_write_buffer(len);
185 memcpy(&write_buffer[cur_pos], s, len);
189 static void write_32bit_value(uint32_t i) {
190 write_bytes((char*)&i, 4);
193 static void write_64bit_value(uint64_t i) {
194 // GCOV uses a lo-/hi-word format even on big-endian systems.
195 // See also GCOVBuffer::readInt64 in LLVM.
196 uint32_t lo = (uint32_t) i;
197 uint32_t hi = (uint32_t) (i >> 32);
198 write_32bit_value(lo);
199 write_32bit_value(hi);
202 static uint32_t length_of_string(const char *s) {
203 return (strlen(s) / 4) + 1;
206 static void write_string(const char *s) {
207 uint32_t len = length_of_string(s);
208 write_32bit_value(len);
209 write_bytes(s, strlen(s));
210 write_bytes("\0\0\0\0", 4 - (strlen(s) % 4));
213 static uint32_t read_32bit_value() {
219 val = *(uint32_t*)&write_buffer[cur_pos];
224 static uint32_t read_le_32bit_value() {
231 for (i = 0; i < 4; i++)
232 val |= write_buffer[cur_pos++] << (8*i);
236 static uint64_t read_64bit_value() {
237 // GCOV uses a lo-/hi-word format even on big-endian systems.
238 // See also GCOVBuffer::readInt64 in LLVM.
239 uint32_t lo = read_32bit_value();
240 uint32_t hi = read_32bit_value();
241 return ((uint64_t)hi << 32) | ((uint64_t)lo);
244 static char *mangle_filename(const char *orig_filename) {
248 const char *prefix = lprofGetPathPrefix(&prefix_strip, &prefix_len);
251 return strdup(orig_filename);
253 new_filename = malloc(prefix_len + 1 + strlen(orig_filename) + 1);
254 lprofApplyPathPrefix(new_filename, orig_filename, prefix, prefix_len,
260 static int map_file() {
261 fseek(output_file, 0L, SEEK_END);
262 file_size = ftell(output_file);
264 /* A size of 0 is invalid to `mmap'. Return a fail here, but don't issue an
265 * error message because it should "just work" for the user. */
272 mmap_fd = INVALID_HANDLE_VALUE;
274 mmap_fd = (HANDLE)_get_osfhandle(fd);
276 mmap_handle = CreateFileMapping(mmap_fd, NULL, PAGE_READWRITE, DWORD_HI(file_size), DWORD_LO(file_size), NULL);
277 if (mmap_handle == NULL) {
278 fprintf(stderr, "profiling: %s: cannot create file mapping: %lu\n",
279 filename, GetLastError());
283 write_buffer = MapViewOfFile(mmap_handle, FILE_MAP_WRITE, 0, 0, file_size);
284 if (write_buffer == NULL) {
285 fprintf(stderr, "profiling: %s: cannot map: %lu\n", filename,
287 CloseHandle(mmap_handle);
291 write_buffer = mmap(0, file_size, PROT_READ | PROT_WRITE,
292 MAP_FILE | MAP_SHARED, fd, 0);
293 if (write_buffer == (void *)-1) {
295 fprintf(stderr, "profiling: %s: cannot map: %s\n", filename,
304 static void unmap_file() {
306 if (!FlushViewOfFile(write_buffer, file_size)) {
307 fprintf(stderr, "profiling: %s: cannot flush mapped view: %lu\n", filename,
311 if (!UnmapViewOfFile(write_buffer)) {
312 fprintf(stderr, "profiling: %s: cannot unmap mapped view: %lu\n", filename,
316 if (!CloseHandle(mmap_handle)) {
317 fprintf(stderr, "profiling: %s: cannot close file mapping handle: %lu\n",
318 filename, GetLastError());
323 if (msync(write_buffer, file_size, MS_SYNC) == -1) {
325 fprintf(stderr, "profiling: %s: cannot msync: %s\n", filename,
329 /* We explicitly ignore errors from unmapping because at this point the data
330 * is written and we don't care.
332 (void)munmap(write_buffer, file_size);
340 * --- LLVM line counter API ---
343 /* A file in this case is a translation unit. Each .o file built with line
344 * profiling enabled will emit to a different file. Only one file may be
347 COMPILER_RT_VISIBILITY
348 void llvm_gcda_start_file(const char *orig_filename, const char version[4],
350 const char *mode = "r+b";
351 filename = mangle_filename(orig_filename);
353 /* Try just opening the file. */
355 fd = open(filename, O_RDWR | O_BINARY);
358 /* Try opening the file, creating it if necessary. */
361 fd = open(filename, O_RDWR | O_CREAT | O_BINARY, 0644);
363 /* Try creating the directories first then opening the file. */
364 __llvm_profile_recursive_mkdir(filename);
365 fd = open(filename, O_RDWR | O_CREAT | O_BINARY, 0644);
367 /* Bah! It's hopeless. */
369 fprintf(stderr, "profiling: %s: cannot open: %s\n", filename,
376 /* Try to flock the file to serialize concurrent processes writing out to the
377 * same GCDA. This can fail if the filesystem doesn't support it, but in that
378 * case we'll just carry on with the old racy behaviour and hope for the best.
381 output_file = fdopen(fd, mode);
383 /* Initialize the write buffer. */
389 resize_write_buffer(WRITE_BUFFER_SIZE);
390 memset(write_buffer, 0, WRITE_BUFFER_SIZE);
392 if (map_file() == -1) {
393 /* mmap failed, try to recover by clobbering */
397 resize_write_buffer(WRITE_BUFFER_SIZE);
398 memset(write_buffer, 0, WRITE_BUFFER_SIZE);
402 /* gcda file, version, stamp checksum. */
403 write_bytes("adcg", 4);
404 write_bytes(version, 4);
405 write_32bit_value(checksum);
407 #ifdef DEBUG_GCDAPROFILING
408 fprintf(stderr, "llvmgcda: [%s]\n", orig_filename);
412 /* Given an array of pointers to counters (counters), increment the n-th one,
413 * where we're also given a pointer to n (predecessor).
415 COMPILER_RT_VISIBILITY
416 void llvm_gcda_increment_indirect_counter(uint32_t *predecessor,
417 uint64_t **counters) {
422 if (pred == 0xffffffff)
424 counter = counters[pred];
426 /* Don't crash if the pred# is out of sync. This can happen due to threads,
427 or because of a TODO in GCOVProfiling.cpp buildEdgeLookupTable(). */
430 #ifdef DEBUG_GCDAPROFILING
433 "llvmgcda: increment_indirect_counter counters=%08llx, pred=%u\n",
434 *counter, *predecessor);
438 COMPILER_RT_VISIBILITY
439 void llvm_gcda_emit_function(uint32_t ident, const char *function_name,
440 uint32_t func_checksum, uint8_t use_extra_checksum,
441 uint32_t cfg_checksum) {
444 if (use_extra_checksum)
446 #ifdef DEBUG_GCDAPROFILING
447 fprintf(stderr, "llvmgcda: function id=0x%08x name=%s\n", ident,
448 function_name ? function_name : "NULL");
450 if (!output_file) return;
453 write_bytes("\0\0\0\1", 4);
455 len += 1 + length_of_string(function_name);
456 write_32bit_value(len);
457 write_32bit_value(ident);
458 write_32bit_value(func_checksum);
459 if (use_extra_checksum)
460 write_32bit_value(cfg_checksum);
462 write_string(function_name);
465 COMPILER_RT_VISIBILITY
466 void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
468 uint64_t *old_ctrs = NULL;
470 uint64_t save_cur_pos = cur_pos;
472 if (!output_file) return;
474 val = read_le_32bit_value();
476 if (val != (uint32_t)-1) {
477 /* There are counters present in the file. Merge them. */
478 if (val != 0x01a10000) {
479 fprintf(stderr, "profiling: %s: cannot merge previous GCDA file: "
480 "corrupt arc tag (0x%08x)\n",
485 val = read_32bit_value();
486 if (val == (uint32_t)-1 || val / 2 != num_counters) {
487 fprintf(stderr, "profiling: %s: cannot merge previous GCDA file: "
488 "mismatched number of counters (%d)\n",
493 old_ctrs = malloc(sizeof(uint64_t) * num_counters);
494 for (i = 0; i < num_counters; ++i)
495 old_ctrs[i] = read_64bit_value();
498 cur_pos = save_cur_pos;
500 /* Counter #1 (arcs) tag */
501 write_bytes("\0\0\xa1\1", 4);
502 write_32bit_value(num_counters * 2);
503 for (i = 0; i < num_counters; ++i) {
504 counters[i] += (old_ctrs ? old_ctrs[i] : 0);
505 write_64bit_value(counters[i]);
510 #ifdef DEBUG_GCDAPROFILING
511 fprintf(stderr, "llvmgcda: %u arcs\n", num_counters);
512 for (i = 0; i < num_counters; ++i)
513 fprintf(stderr, "llvmgcda: %llu\n", (unsigned long long)counters[i]);
517 COMPILER_RT_VISIBILITY
518 void llvm_gcda_summary_info() {
519 const uint32_t obj_summary_len = 9; /* Length for gcov compatibility. */
522 static uint32_t run_counted = 0; // We only want to increase the run count once.
524 uint64_t save_cur_pos = cur_pos;
526 if (!output_file) return;
528 val = read_le_32bit_value();
530 if (val != (uint32_t)-1) {
531 /* There are counters present in the file. Merge them. */
532 if (val != 0xa1000000) {
533 fprintf(stderr, "profiling: %s: cannot merge previous run count: "
534 "corrupt object tag (0x%08x)\n",
539 val = read_32bit_value(); /* length */
540 if (val != obj_summary_len) {
541 fprintf(stderr, "profiling: %s: cannot merge previous run count: "
542 "mismatched object length (%d)\n",
547 read_32bit_value(); /* checksum, unused */
548 read_32bit_value(); /* num, unused */
549 uint32_t prev_runs = read_32bit_value();
550 /* Add previous run count to new counter, if not already counted before. */
551 runs = run_counted ? prev_runs : prev_runs + 1;
554 cur_pos = save_cur_pos;
556 /* Object summary tag */
557 write_bytes("\0\0\0\xa1", 4);
558 write_32bit_value(obj_summary_len);
559 write_32bit_value(0); /* checksum, unused */
560 write_32bit_value(0); /* num, unused */
561 write_32bit_value(runs);
562 for (i = 3; i < obj_summary_len; ++i)
563 write_32bit_value(0);
565 /* Program summary tag */
566 write_bytes("\0\0\0\xa3", 4); /* tag indicates 1 program */
567 write_32bit_value(0); /* 0 length */
571 #ifdef DEBUG_GCDAPROFILING
572 fprintf(stderr, "llvmgcda: %u runs\n", runs);
576 COMPILER_RT_VISIBILITY
577 void llvm_gcda_end_file() {
578 /* Write out EOF record. */
580 write_bytes("\0\0\0\0\0\0\0\0", 8);
583 fwrite(write_buffer, cur_pos, 1, output_file);
597 #ifdef DEBUG_GCDAPROFILING
598 fprintf(stderr, "llvmgcda: -----\n");
602 COMPILER_RT_VISIBILITY
603 void llvm_register_writeout_function(fn_ptr fn) {
604 fn_list_insert(&writeout_fn_list, fn);
607 COMPILER_RT_VISIBILITY
608 void llvm_writeout_files(void) {
609 struct fn_node *curr = writeout_fn_list.head;
612 if (curr->id == CURRENT_ID) {
619 COMPILER_RT_VISIBILITY
620 void llvm_delete_writeout_function_list(void) {
621 fn_list_remove(&writeout_fn_list);
624 COMPILER_RT_VISIBILITY
625 void llvm_register_flush_function(fn_ptr fn) {
626 fn_list_insert(&flush_fn_list, fn);
629 void __gcov_flush() {
630 struct fn_node* curr = flush_fn_list.head;
638 COMPILER_RT_VISIBILITY
639 void llvm_delete_flush_function_list(void) {
640 fn_list_remove(&flush_fn_list);
643 COMPILER_RT_VISIBILITY
644 void llvm_register_reset_function(fn_ptr fn) {
645 fn_list_insert(&reset_fn_list, fn);
648 COMPILER_RT_VISIBILITY
649 void llvm_delete_reset_function_list(void) { fn_list_remove(&reset_fn_list); }
651 COMPILER_RT_VISIBILITY
652 void llvm_reset_counters(void) {
653 struct fn_node *curr = reset_fn_list.head;
656 if (curr->id == CURRENT_ID) {
664 COMPILER_RT_VISIBILITY
665 pid_t __gcov_fork() {
666 pid_t parent_pid = getpid();
670 pid_t child_pid = getpid();
671 if (child_pid != parent_pid) {
672 // The pid changed so we've a fork (one could have its own fork function)
673 // Just reset the counters for this child process
675 llvm_reset_counters();
682 COMPILER_RT_VISIBILITY
683 void llvm_gcov_init(fn_ptr wfn, fn_ptr ffn, fn_ptr rfn) {
684 static int atexit_ran = 0;
687 llvm_register_writeout_function(wfn);
690 llvm_register_flush_function(ffn);
693 llvm_register_reset_function(rfn);
695 if (atexit_ran == 0) {
698 /* Make sure we write out the data and delete the data structures. */
699 atexit(llvm_delete_reset_function_list);
700 atexit(llvm_delete_flush_function_list);
701 atexit(llvm_delete_writeout_function_list);
702 atexit(llvm_writeout_files);