14684ddb6SLionel Sambuc /*===- GCDAProfiling.c - Support library for GCDA file emission -----------===*\
24684ddb6SLionel Sambuc |*
34684ddb6SLionel Sambuc |* The LLVM Compiler Infrastructure
44684ddb6SLionel Sambuc |*
54684ddb6SLionel Sambuc |* This file is distributed under the University of Illinois Open Source
64684ddb6SLionel Sambuc |* License. See LICENSE.TXT for details.
74684ddb6SLionel Sambuc |*
84684ddb6SLionel Sambuc |*===----------------------------------------------------------------------===*|
94684ddb6SLionel Sambuc |*
104684ddb6SLionel Sambuc |* This file implements the call back routines for the gcov profiling
114684ddb6SLionel Sambuc |* instrumentation pass. Link against this library when running code through
124684ddb6SLionel Sambuc |* the -insert-gcov-profiling LLVM pass.
134684ddb6SLionel Sambuc |*
144684ddb6SLionel Sambuc |* We emit files in a corrupt version of GCOV's "gcda" file format. These files
154684ddb6SLionel Sambuc |* are only close enough that LCOV will happily parse them. Anything that lcov
164684ddb6SLionel Sambuc |* ignores is missing.
174684ddb6SLionel Sambuc |*
184684ddb6SLionel Sambuc |* TODO: gcov is multi-process safe by having each exit open the existing file
194684ddb6SLionel Sambuc |* and append to it. We'd like to achieve that and be thread-safe too.
204684ddb6SLionel Sambuc |*
214684ddb6SLionel Sambuc \*===----------------------------------------------------------------------===*/
224684ddb6SLionel Sambuc
234684ddb6SLionel Sambuc #include <errno.h>
244684ddb6SLionel Sambuc #include <fcntl.h>
254684ddb6SLionel Sambuc #include <stdio.h>
264684ddb6SLionel Sambuc #include <stdlib.h>
274684ddb6SLionel Sambuc #include <string.h>
284684ddb6SLionel Sambuc #include <sys/mman.h>
294684ddb6SLionel Sambuc #ifdef _WIN32
304684ddb6SLionel Sambuc #include <direct.h>
314684ddb6SLionel Sambuc #endif
324684ddb6SLionel Sambuc
33*0a6a1f1dSLionel Sambuc #define I386_FREEBSD (defined(__FreeBSD__) && defined(__i386__))
34*0a6a1f1dSLionel Sambuc
35*0a6a1f1dSLionel Sambuc #if !I386_FREEBSD
36*0a6a1f1dSLionel Sambuc #include <sys/stat.h>
37*0a6a1f1dSLionel Sambuc #include <sys/types.h>
38*0a6a1f1dSLionel Sambuc #endif
39*0a6a1f1dSLionel Sambuc
40*0a6a1f1dSLionel Sambuc #if !defined(_MSC_VER) && !I386_FREEBSD
414684ddb6SLionel Sambuc #include <stdint.h>
42*0a6a1f1dSLionel Sambuc #endif
43*0a6a1f1dSLionel Sambuc
44*0a6a1f1dSLionel Sambuc #if defined(_MSC_VER)
454684ddb6SLionel Sambuc typedef unsigned int uint32_t;
46*0a6a1f1dSLionel Sambuc typedef unsigned long long uint64_t;
47*0a6a1f1dSLionel Sambuc #elif I386_FREEBSD
48*0a6a1f1dSLionel Sambuc /* System headers define 'size_t' incorrectly on x64 FreeBSD (prior to
49*0a6a1f1dSLionel Sambuc * FreeBSD 10, r232261) when compiled in 32-bit mode.
50*0a6a1f1dSLionel Sambuc */
51*0a6a1f1dSLionel Sambuc typedef unsigned char uint8_t;
52*0a6a1f1dSLionel Sambuc typedef unsigned int uint32_t;
53*0a6a1f1dSLionel Sambuc typedef unsigned long long uint64_t;
54*0a6a1f1dSLionel Sambuc int mkdir(const char*, unsigned short);
554684ddb6SLionel Sambuc #endif
564684ddb6SLionel Sambuc
574684ddb6SLionel Sambuc /* #define DEBUG_GCDAPROFILING */
584684ddb6SLionel Sambuc
594684ddb6SLionel Sambuc /*
604684ddb6SLionel Sambuc * --- GCOV file format I/O primitives ---
614684ddb6SLionel Sambuc */
624684ddb6SLionel Sambuc
634684ddb6SLionel Sambuc /*
644684ddb6SLionel Sambuc * The current file name we're outputting. Used primarily for error logging.
654684ddb6SLionel Sambuc */
664684ddb6SLionel Sambuc static char *filename = NULL;
674684ddb6SLionel Sambuc
684684ddb6SLionel Sambuc /*
694684ddb6SLionel Sambuc * The current file we're outputting.
704684ddb6SLionel Sambuc */
714684ddb6SLionel Sambuc static FILE *output_file = NULL;
724684ddb6SLionel Sambuc
734684ddb6SLionel Sambuc /*
744684ddb6SLionel Sambuc * Buffer that we write things into.
754684ddb6SLionel Sambuc */
764684ddb6SLionel Sambuc #define WRITE_BUFFER_SIZE (128 * 1024)
774684ddb6SLionel Sambuc static char *write_buffer = NULL;
784684ddb6SLionel Sambuc static uint64_t cur_buffer_size = 0;
794684ddb6SLionel Sambuc static uint64_t cur_pos = 0;
804684ddb6SLionel Sambuc static uint64_t file_size = 0;
814684ddb6SLionel Sambuc static int new_file = 0;
824684ddb6SLionel Sambuc static int fd = -1;
834684ddb6SLionel Sambuc
844684ddb6SLionel Sambuc /*
854684ddb6SLionel Sambuc * A list of functions to write out the data.
864684ddb6SLionel Sambuc */
874684ddb6SLionel Sambuc typedef void (*writeout_fn)();
884684ddb6SLionel Sambuc
894684ddb6SLionel Sambuc struct writeout_fn_node {
904684ddb6SLionel Sambuc writeout_fn fn;
914684ddb6SLionel Sambuc struct writeout_fn_node *next;
924684ddb6SLionel Sambuc };
934684ddb6SLionel Sambuc
944684ddb6SLionel Sambuc static struct writeout_fn_node *writeout_fn_head = NULL;
954684ddb6SLionel Sambuc static struct writeout_fn_node *writeout_fn_tail = NULL;
964684ddb6SLionel Sambuc
974684ddb6SLionel Sambuc /*
984684ddb6SLionel Sambuc * A list of flush functions that our __gcov_flush() function should call.
994684ddb6SLionel Sambuc */
1004684ddb6SLionel Sambuc typedef void (*flush_fn)();
1014684ddb6SLionel Sambuc
1024684ddb6SLionel Sambuc struct flush_fn_node {
1034684ddb6SLionel Sambuc flush_fn fn;
1044684ddb6SLionel Sambuc struct flush_fn_node *next;
1054684ddb6SLionel Sambuc };
1064684ddb6SLionel Sambuc
1074684ddb6SLionel Sambuc static struct flush_fn_node *flush_fn_head = NULL;
1084684ddb6SLionel Sambuc static struct flush_fn_node *flush_fn_tail = NULL;
1094684ddb6SLionel Sambuc
resize_write_buffer(uint64_t size)1104684ddb6SLionel Sambuc static void resize_write_buffer(uint64_t size) {
1114684ddb6SLionel Sambuc if (!new_file) return;
1124684ddb6SLionel Sambuc size += cur_pos;
1134684ddb6SLionel Sambuc if (size <= cur_buffer_size) return;
1144684ddb6SLionel Sambuc size = (size - 1) / WRITE_BUFFER_SIZE + 1;
1154684ddb6SLionel Sambuc size *= WRITE_BUFFER_SIZE;
1164684ddb6SLionel Sambuc write_buffer = realloc(write_buffer, size);
1174684ddb6SLionel Sambuc cur_buffer_size = size;
1184684ddb6SLionel Sambuc }
1194684ddb6SLionel Sambuc
write_bytes(const char * s,size_t len)1204684ddb6SLionel Sambuc static void write_bytes(const char *s, size_t len) {
1214684ddb6SLionel Sambuc resize_write_buffer(len);
1224684ddb6SLionel Sambuc memcpy(&write_buffer[cur_pos], s, len);
1234684ddb6SLionel Sambuc cur_pos += len;
1244684ddb6SLionel Sambuc }
1254684ddb6SLionel Sambuc
write_32bit_value(uint32_t i)1264684ddb6SLionel Sambuc static void write_32bit_value(uint32_t i) {
1274684ddb6SLionel Sambuc write_bytes((char*)&i, 4);
1284684ddb6SLionel Sambuc }
1294684ddb6SLionel Sambuc
write_64bit_value(uint64_t i)1304684ddb6SLionel Sambuc static void write_64bit_value(uint64_t i) {
1314684ddb6SLionel Sambuc write_bytes((char*)&i, 8);
1324684ddb6SLionel Sambuc }
1334684ddb6SLionel Sambuc
length_of_string(const char * s)1344684ddb6SLionel Sambuc static uint32_t length_of_string(const char *s) {
1354684ddb6SLionel Sambuc return (strlen(s) / 4) + 1;
1364684ddb6SLionel Sambuc }
1374684ddb6SLionel Sambuc
write_string(const char * s)1384684ddb6SLionel Sambuc static void write_string(const char *s) {
1394684ddb6SLionel Sambuc uint32_t len = length_of_string(s);
1404684ddb6SLionel Sambuc write_32bit_value(len);
1414684ddb6SLionel Sambuc write_bytes(s, strlen(s));
1424684ddb6SLionel Sambuc write_bytes("\0\0\0\0", 4 - (strlen(s) % 4));
1434684ddb6SLionel Sambuc }
1444684ddb6SLionel Sambuc
read_32bit_value()1454684ddb6SLionel Sambuc static uint32_t read_32bit_value() {
1464684ddb6SLionel Sambuc uint32_t val;
1474684ddb6SLionel Sambuc
1484684ddb6SLionel Sambuc if (new_file)
1494684ddb6SLionel Sambuc return (uint32_t)-1;
1504684ddb6SLionel Sambuc
1514684ddb6SLionel Sambuc val = *(uint32_t*)&write_buffer[cur_pos];
1524684ddb6SLionel Sambuc cur_pos += 4;
1534684ddb6SLionel Sambuc return val;
1544684ddb6SLionel Sambuc }
1554684ddb6SLionel Sambuc
read_64bit_value()1564684ddb6SLionel Sambuc static uint64_t read_64bit_value() {
1574684ddb6SLionel Sambuc uint64_t val;
1584684ddb6SLionel Sambuc
1594684ddb6SLionel Sambuc if (new_file)
1604684ddb6SLionel Sambuc return (uint64_t)-1;
1614684ddb6SLionel Sambuc
1624684ddb6SLionel Sambuc val = *(uint64_t*)&write_buffer[cur_pos];
1634684ddb6SLionel Sambuc cur_pos += 8;
1644684ddb6SLionel Sambuc return val;
1654684ddb6SLionel Sambuc }
1664684ddb6SLionel Sambuc
mangle_filename(const char * orig_filename)1674684ddb6SLionel Sambuc static char *mangle_filename(const char *orig_filename) {
168*0a6a1f1dSLionel Sambuc char *new_filename;
169*0a6a1f1dSLionel Sambuc size_t filename_len, prefix_len;
170*0a6a1f1dSLionel Sambuc int prefix_strip;
1714684ddb6SLionel Sambuc int level = 0;
172*0a6a1f1dSLionel Sambuc const char *fname, *ptr;
1734684ddb6SLionel Sambuc const char *prefix = getenv("GCOV_PREFIX");
1744684ddb6SLionel Sambuc const char *prefix_strip_str = getenv("GCOV_PREFIX_STRIP");
1754684ddb6SLionel Sambuc
176*0a6a1f1dSLionel Sambuc if (prefix == NULL || prefix[0] == '\0')
1774684ddb6SLionel Sambuc return strdup(orig_filename);
1784684ddb6SLionel Sambuc
1794684ddb6SLionel Sambuc if (prefix_strip_str) {
1804684ddb6SLionel Sambuc prefix_strip = atoi(prefix_strip_str);
1814684ddb6SLionel Sambuc
1824684ddb6SLionel Sambuc /* Negative GCOV_PREFIX_STRIP values are ignored */
1834684ddb6SLionel Sambuc if (prefix_strip < 0)
1844684ddb6SLionel Sambuc prefix_strip = 0;
185*0a6a1f1dSLionel Sambuc } else {
186*0a6a1f1dSLionel Sambuc prefix_strip = 0;
1874684ddb6SLionel Sambuc }
1884684ddb6SLionel Sambuc
189*0a6a1f1dSLionel Sambuc fname = orig_filename;
190*0a6a1f1dSLionel Sambuc for (level = 0, ptr = fname + 1; level < prefix_strip; ++ptr) {
191*0a6a1f1dSLionel Sambuc if (*ptr == '\0')
192*0a6a1f1dSLionel Sambuc break;
193*0a6a1f1dSLionel Sambuc if (*ptr != '/')
194*0a6a1f1dSLionel Sambuc continue;
1954684ddb6SLionel Sambuc fname = ptr;
1964684ddb6SLionel Sambuc ++level;
1974684ddb6SLionel Sambuc }
1984684ddb6SLionel Sambuc
199*0a6a1f1dSLionel Sambuc filename_len = strlen(fname);
200*0a6a1f1dSLionel Sambuc prefix_len = strlen(prefix);
201*0a6a1f1dSLionel Sambuc new_filename = malloc(prefix_len + 1 + filename_len + 1);
202*0a6a1f1dSLionel Sambuc memcpy(new_filename, prefix, prefix_len);
2034684ddb6SLionel Sambuc
204*0a6a1f1dSLionel Sambuc if (prefix[prefix_len - 1] != '/')
205*0a6a1f1dSLionel Sambuc new_filename[prefix_len++] = '/';
206*0a6a1f1dSLionel Sambuc memcpy(new_filename + prefix_len, fname, filename_len + 1);
207*0a6a1f1dSLionel Sambuc
208*0a6a1f1dSLionel Sambuc return new_filename;
2094684ddb6SLionel Sambuc }
2104684ddb6SLionel Sambuc
recursive_mkdir(char * path)211*0a6a1f1dSLionel Sambuc static void recursive_mkdir(char *path) {
2124684ddb6SLionel Sambuc int i;
2134684ddb6SLionel Sambuc
214*0a6a1f1dSLionel Sambuc for (i = 1; path[i] != '\0'; ++i) {
215*0a6a1f1dSLionel Sambuc if (path[i] != '/') continue;
216*0a6a1f1dSLionel Sambuc path[i] = '\0';
2174684ddb6SLionel Sambuc #ifdef _WIN32
218*0a6a1f1dSLionel Sambuc _mkdir(path);
2194684ddb6SLionel Sambuc #else
220*0a6a1f1dSLionel Sambuc mkdir(path, 0755); /* Some of these will fail, ignore it. */
2214684ddb6SLionel Sambuc #endif
222*0a6a1f1dSLionel Sambuc path[i] = '/';
2234684ddb6SLionel Sambuc }
2244684ddb6SLionel Sambuc }
2254684ddb6SLionel Sambuc
map_file()2264684ddb6SLionel Sambuc static int map_file() {
2274684ddb6SLionel Sambuc fseek(output_file, 0L, SEEK_END);
2284684ddb6SLionel Sambuc file_size = ftell(output_file);
2294684ddb6SLionel Sambuc
230*0a6a1f1dSLionel Sambuc /* A size of 0 is invalid to `mmap'. Return a fail here, but don't issue an
231*0a6a1f1dSLionel Sambuc * error message because it should "just work" for the user. */
232*0a6a1f1dSLionel Sambuc if (file_size == 0)
233*0a6a1f1dSLionel Sambuc return -1;
234*0a6a1f1dSLionel Sambuc
2354684ddb6SLionel Sambuc write_buffer = mmap(0, file_size, PROT_READ | PROT_WRITE,
2364684ddb6SLionel Sambuc MAP_FILE | MAP_SHARED, fd, 0);
2374684ddb6SLionel Sambuc if (write_buffer == (void *)-1) {
2384684ddb6SLionel Sambuc int errnum = errno;
2394684ddb6SLionel Sambuc fprintf(stderr, "profiling: %s: cannot map: %s\n", filename,
2404684ddb6SLionel Sambuc strerror(errnum));
2414684ddb6SLionel Sambuc return -1;
2424684ddb6SLionel Sambuc }
2434684ddb6SLionel Sambuc return 0;
2444684ddb6SLionel Sambuc }
2454684ddb6SLionel Sambuc
unmap_file()2464684ddb6SLionel Sambuc static void unmap_file() {
247*0a6a1f1dSLionel Sambuc #if !defined(__minix)
2484684ddb6SLionel Sambuc if (msync(write_buffer, file_size, MS_SYNC) == -1) {
2494684ddb6SLionel Sambuc int errnum = errno;
2504684ddb6SLionel Sambuc fprintf(stderr, "profiling: %s: cannot msync: %s\n", filename,
2514684ddb6SLionel Sambuc strerror(errnum));
2524684ddb6SLionel Sambuc }
253*0a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
2544684ddb6SLionel Sambuc
2554684ddb6SLionel Sambuc /* We explicitly ignore errors from unmapping because at this point the data
2564684ddb6SLionel Sambuc * is written and we don't care.
2574684ddb6SLionel Sambuc */
2584684ddb6SLionel Sambuc (void)munmap(write_buffer, file_size);
2594684ddb6SLionel Sambuc write_buffer = NULL;
2604684ddb6SLionel Sambuc file_size = 0;
2614684ddb6SLionel Sambuc }
2624684ddb6SLionel Sambuc
2634684ddb6SLionel Sambuc /*
2644684ddb6SLionel Sambuc * --- LLVM line counter API ---
2654684ddb6SLionel Sambuc */
2664684ddb6SLionel Sambuc
2674684ddb6SLionel Sambuc /* A file in this case is a translation unit. Each .o file built with line
2684684ddb6SLionel Sambuc * profiling enabled will emit to a different file. Only one file may be
2694684ddb6SLionel Sambuc * started at a time.
2704684ddb6SLionel Sambuc */
llvm_gcda_start_file(const char * orig_filename,const char version[4],uint32_t checksum)271*0a6a1f1dSLionel Sambuc void llvm_gcda_start_file(const char *orig_filename, const char version[4],
272*0a6a1f1dSLionel Sambuc uint32_t checksum) {
2734684ddb6SLionel Sambuc const char *mode = "r+b";
2744684ddb6SLionel Sambuc filename = mangle_filename(orig_filename);
2754684ddb6SLionel Sambuc
2764684ddb6SLionel Sambuc /* Try just opening the file. */
2774684ddb6SLionel Sambuc new_file = 0;
2784684ddb6SLionel Sambuc fd = open(filename, O_RDWR);
2794684ddb6SLionel Sambuc
2804684ddb6SLionel Sambuc if (fd == -1) {
2814684ddb6SLionel Sambuc /* Try opening the file, creating it if necessary. */
2824684ddb6SLionel Sambuc new_file = 1;
2834684ddb6SLionel Sambuc mode = "w+b";
2844684ddb6SLionel Sambuc fd = open(filename, O_RDWR | O_CREAT, 0644);
2854684ddb6SLionel Sambuc if (fd == -1) {
2864684ddb6SLionel Sambuc /* Try creating the directories first then opening the file. */
2874684ddb6SLionel Sambuc recursive_mkdir(filename);
2884684ddb6SLionel Sambuc fd = open(filename, O_RDWR | O_CREAT, 0644);
2894684ddb6SLionel Sambuc if (fd == -1) {
2904684ddb6SLionel Sambuc /* Bah! It's hopeless. */
2914684ddb6SLionel Sambuc int errnum = errno;
2924684ddb6SLionel Sambuc fprintf(stderr, "profiling: %s: cannot open: %s\n", filename,
2934684ddb6SLionel Sambuc strerror(errnum));
2944684ddb6SLionel Sambuc return;
2954684ddb6SLionel Sambuc }
2964684ddb6SLionel Sambuc }
2974684ddb6SLionel Sambuc }
2984684ddb6SLionel Sambuc
2994684ddb6SLionel Sambuc output_file = fdopen(fd, mode);
3004684ddb6SLionel Sambuc
3014684ddb6SLionel Sambuc /* Initialize the write buffer. */
3024684ddb6SLionel Sambuc write_buffer = NULL;
3034684ddb6SLionel Sambuc cur_buffer_size = 0;
3044684ddb6SLionel Sambuc cur_pos = 0;
3054684ddb6SLionel Sambuc
3064684ddb6SLionel Sambuc if (new_file) {
3074684ddb6SLionel Sambuc resize_write_buffer(WRITE_BUFFER_SIZE);
3084684ddb6SLionel Sambuc memset(write_buffer, 0, WRITE_BUFFER_SIZE);
3094684ddb6SLionel Sambuc } else {
3104684ddb6SLionel Sambuc if (map_file() == -1) {
3114684ddb6SLionel Sambuc /* mmap failed, try to recover by clobbering */
3124684ddb6SLionel Sambuc new_file = 1;
3134684ddb6SLionel Sambuc write_buffer = NULL;
3144684ddb6SLionel Sambuc cur_buffer_size = 0;
3154684ddb6SLionel Sambuc resize_write_buffer(WRITE_BUFFER_SIZE);
3164684ddb6SLionel Sambuc memset(write_buffer, 0, WRITE_BUFFER_SIZE);
3174684ddb6SLionel Sambuc }
3184684ddb6SLionel Sambuc }
3194684ddb6SLionel Sambuc
320*0a6a1f1dSLionel Sambuc /* gcda file, version, stamp checksum. */
3214684ddb6SLionel Sambuc write_bytes("adcg", 4);
3224684ddb6SLionel Sambuc write_bytes(version, 4);
323*0a6a1f1dSLionel Sambuc write_32bit_value(checksum);
3244684ddb6SLionel Sambuc
3254684ddb6SLionel Sambuc #ifdef DEBUG_GCDAPROFILING
3264684ddb6SLionel Sambuc fprintf(stderr, "llvmgcda: [%s]\n", orig_filename);
3274684ddb6SLionel Sambuc #endif
3284684ddb6SLionel Sambuc }
3294684ddb6SLionel Sambuc
3304684ddb6SLionel Sambuc /* Given an array of pointers to counters (counters), increment the n-th one,
3314684ddb6SLionel Sambuc * where we're also given a pointer to n (predecessor).
3324684ddb6SLionel Sambuc */
llvm_gcda_increment_indirect_counter(uint32_t * predecessor,uint64_t ** counters)3334684ddb6SLionel Sambuc void llvm_gcda_increment_indirect_counter(uint32_t *predecessor,
3344684ddb6SLionel Sambuc uint64_t **counters) {
3354684ddb6SLionel Sambuc uint64_t *counter;
3364684ddb6SLionel Sambuc uint32_t pred;
3374684ddb6SLionel Sambuc
3384684ddb6SLionel Sambuc pred = *predecessor;
3394684ddb6SLionel Sambuc if (pred == 0xffffffff)
3404684ddb6SLionel Sambuc return;
3414684ddb6SLionel Sambuc counter = counters[pred];
3424684ddb6SLionel Sambuc
3434684ddb6SLionel Sambuc /* Don't crash if the pred# is out of sync. This can happen due to threads,
3444684ddb6SLionel Sambuc or because of a TODO in GCOVProfiling.cpp buildEdgeLookupTable(). */
3454684ddb6SLionel Sambuc if (counter)
3464684ddb6SLionel Sambuc ++*counter;
3474684ddb6SLionel Sambuc #ifdef DEBUG_GCDAPROFILING
3484684ddb6SLionel Sambuc else
3494684ddb6SLionel Sambuc fprintf(stderr,
3504684ddb6SLionel Sambuc "llvmgcda: increment_indirect_counter counters=%08llx, pred=%u\n",
3514684ddb6SLionel Sambuc *counter, *predecessor);
3524684ddb6SLionel Sambuc #endif
3534684ddb6SLionel Sambuc }
3544684ddb6SLionel Sambuc
llvm_gcda_emit_function(uint32_t ident,const char * function_name,uint32_t func_checksum,uint8_t use_extra_checksum,uint32_t cfg_checksum)3554684ddb6SLionel Sambuc void llvm_gcda_emit_function(uint32_t ident, const char *function_name,
356*0a6a1f1dSLionel Sambuc uint32_t func_checksum, uint8_t use_extra_checksum,
357*0a6a1f1dSLionel Sambuc uint32_t cfg_checksum) {
3584684ddb6SLionel Sambuc uint32_t len = 2;
3594684ddb6SLionel Sambuc
3604684ddb6SLionel Sambuc if (use_extra_checksum)
3614684ddb6SLionel Sambuc len++;
3624684ddb6SLionel Sambuc #ifdef DEBUG_GCDAPROFILING
3634684ddb6SLionel Sambuc fprintf(stderr, "llvmgcda: function id=0x%08x name=%s\n", ident,
3644684ddb6SLionel Sambuc function_name ? function_name : "NULL");
3654684ddb6SLionel Sambuc #endif
3664684ddb6SLionel Sambuc if (!output_file) return;
3674684ddb6SLionel Sambuc
3684684ddb6SLionel Sambuc /* function tag */
3694684ddb6SLionel Sambuc write_bytes("\0\0\0\1", 4);
3704684ddb6SLionel Sambuc if (function_name)
3714684ddb6SLionel Sambuc len += 1 + length_of_string(function_name);
3724684ddb6SLionel Sambuc write_32bit_value(len);
3734684ddb6SLionel Sambuc write_32bit_value(ident);
374*0a6a1f1dSLionel Sambuc write_32bit_value(func_checksum);
3754684ddb6SLionel Sambuc if (use_extra_checksum)
376*0a6a1f1dSLionel Sambuc write_32bit_value(cfg_checksum);
3774684ddb6SLionel Sambuc if (function_name)
3784684ddb6SLionel Sambuc write_string(function_name);
3794684ddb6SLionel Sambuc }
3804684ddb6SLionel Sambuc
llvm_gcda_emit_arcs(uint32_t num_counters,uint64_t * counters)3814684ddb6SLionel Sambuc void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
3824684ddb6SLionel Sambuc uint32_t i;
3834684ddb6SLionel Sambuc uint64_t *old_ctrs = NULL;
3844684ddb6SLionel Sambuc uint32_t val = 0;
3854684ddb6SLionel Sambuc uint64_t save_cur_pos = cur_pos;
3864684ddb6SLionel Sambuc
3874684ddb6SLionel Sambuc if (!output_file) return;
3884684ddb6SLionel Sambuc
3894684ddb6SLionel Sambuc val = read_32bit_value();
3904684ddb6SLionel Sambuc
3914684ddb6SLionel Sambuc if (val != (uint32_t)-1) {
3924684ddb6SLionel Sambuc /* There are counters present in the file. Merge them. */
3934684ddb6SLionel Sambuc if (val != 0x01a10000) {
394*0a6a1f1dSLionel Sambuc fprintf(stderr, "profiling:invalid arc tag (0x%08x)\n", val);
3954684ddb6SLionel Sambuc return;
3964684ddb6SLionel Sambuc }
3974684ddb6SLionel Sambuc
3984684ddb6SLionel Sambuc val = read_32bit_value();
3994684ddb6SLionel Sambuc if (val == (uint32_t)-1 || val / 2 != num_counters) {
4004684ddb6SLionel Sambuc fprintf(stderr, "profiling:invalid number of counters (%d)\n", val);
4014684ddb6SLionel Sambuc return;
4024684ddb6SLionel Sambuc }
4034684ddb6SLionel Sambuc
4044684ddb6SLionel Sambuc old_ctrs = malloc(sizeof(uint64_t) * num_counters);
4054684ddb6SLionel Sambuc for (i = 0; i < num_counters; ++i)
4064684ddb6SLionel Sambuc old_ctrs[i] = read_64bit_value();
4074684ddb6SLionel Sambuc }
4084684ddb6SLionel Sambuc
4094684ddb6SLionel Sambuc cur_pos = save_cur_pos;
4104684ddb6SLionel Sambuc
4114684ddb6SLionel Sambuc /* Counter #1 (arcs) tag */
4124684ddb6SLionel Sambuc write_bytes("\0\0\xa1\1", 4);
4134684ddb6SLionel Sambuc write_32bit_value(num_counters * 2);
4144684ddb6SLionel Sambuc for (i = 0; i < num_counters; ++i) {
4154684ddb6SLionel Sambuc counters[i] += (old_ctrs ? old_ctrs[i] : 0);
4164684ddb6SLionel Sambuc write_64bit_value(counters[i]);
4174684ddb6SLionel Sambuc }
4184684ddb6SLionel Sambuc
4194684ddb6SLionel Sambuc free(old_ctrs);
4204684ddb6SLionel Sambuc
4214684ddb6SLionel Sambuc #ifdef DEBUG_GCDAPROFILING
4224684ddb6SLionel Sambuc fprintf(stderr, "llvmgcda: %u arcs\n", num_counters);
4234684ddb6SLionel Sambuc for (i = 0; i < num_counters; ++i)
4244684ddb6SLionel Sambuc fprintf(stderr, "llvmgcda: %llu\n", (unsigned long long)counters[i]);
4254684ddb6SLionel Sambuc #endif
4264684ddb6SLionel Sambuc }
4274684ddb6SLionel Sambuc
llvm_gcda_summary_info()428*0a6a1f1dSLionel Sambuc void llvm_gcda_summary_info() {
429*0a6a1f1dSLionel Sambuc const uint32_t obj_summary_len = 9; /* Length for gcov compatibility. */
430*0a6a1f1dSLionel Sambuc uint32_t i;
431*0a6a1f1dSLionel Sambuc uint32_t runs = 1;
432*0a6a1f1dSLionel Sambuc uint32_t val = 0;
433*0a6a1f1dSLionel Sambuc uint64_t save_cur_pos = cur_pos;
434*0a6a1f1dSLionel Sambuc
435*0a6a1f1dSLionel Sambuc if (!output_file) return;
436*0a6a1f1dSLionel Sambuc
437*0a6a1f1dSLionel Sambuc val = read_32bit_value();
438*0a6a1f1dSLionel Sambuc
439*0a6a1f1dSLionel Sambuc if (val != (uint32_t)-1) {
440*0a6a1f1dSLionel Sambuc /* There are counters present in the file. Merge them. */
441*0a6a1f1dSLionel Sambuc if (val != 0xa1000000) {
442*0a6a1f1dSLionel Sambuc fprintf(stderr, "profiling:invalid object tag (0x%08x)\n", val);
443*0a6a1f1dSLionel Sambuc return;
444*0a6a1f1dSLionel Sambuc }
445*0a6a1f1dSLionel Sambuc
446*0a6a1f1dSLionel Sambuc val = read_32bit_value(); /* length */
447*0a6a1f1dSLionel Sambuc if (val != obj_summary_len) {
448*0a6a1f1dSLionel Sambuc fprintf(stderr, "profiling:invalid object length (%d)\n", val);
449*0a6a1f1dSLionel Sambuc return;
450*0a6a1f1dSLionel Sambuc }
451*0a6a1f1dSLionel Sambuc
452*0a6a1f1dSLionel Sambuc read_32bit_value(); /* checksum, unused */
453*0a6a1f1dSLionel Sambuc read_32bit_value(); /* num, unused */
454*0a6a1f1dSLionel Sambuc runs += read_32bit_value(); /* Add previous run count to new counter. */
455*0a6a1f1dSLionel Sambuc }
456*0a6a1f1dSLionel Sambuc
457*0a6a1f1dSLionel Sambuc cur_pos = save_cur_pos;
458*0a6a1f1dSLionel Sambuc
459*0a6a1f1dSLionel Sambuc /* Object summary tag */
460*0a6a1f1dSLionel Sambuc write_bytes("\0\0\0\xa1", 4);
461*0a6a1f1dSLionel Sambuc write_32bit_value(obj_summary_len);
462*0a6a1f1dSLionel Sambuc write_32bit_value(0); /* checksum, unused */
463*0a6a1f1dSLionel Sambuc write_32bit_value(0); /* num, unused */
464*0a6a1f1dSLionel Sambuc write_32bit_value(runs);
465*0a6a1f1dSLionel Sambuc for (i = 3; i < obj_summary_len; ++i)
466*0a6a1f1dSLionel Sambuc write_32bit_value(0);
467*0a6a1f1dSLionel Sambuc
468*0a6a1f1dSLionel Sambuc /* Program summary tag */
469*0a6a1f1dSLionel Sambuc write_bytes("\0\0\0\xa3", 4); /* tag indicates 1 program */
470*0a6a1f1dSLionel Sambuc write_32bit_value(0); /* 0 length */
471*0a6a1f1dSLionel Sambuc
472*0a6a1f1dSLionel Sambuc #ifdef DEBUG_GCDAPROFILING
473*0a6a1f1dSLionel Sambuc fprintf(stderr, "llvmgcda: %u runs\n", runs);
474*0a6a1f1dSLionel Sambuc #endif
475*0a6a1f1dSLionel Sambuc }
476*0a6a1f1dSLionel Sambuc
llvm_gcda_end_file()4774684ddb6SLionel Sambuc void llvm_gcda_end_file() {
4784684ddb6SLionel Sambuc /* Write out EOF record. */
4794684ddb6SLionel Sambuc if (output_file) {
4804684ddb6SLionel Sambuc write_bytes("\0\0\0\0\0\0\0\0", 8);
4814684ddb6SLionel Sambuc
4824684ddb6SLionel Sambuc if (new_file) {
4834684ddb6SLionel Sambuc fwrite(write_buffer, cur_pos, 1, output_file);
4844684ddb6SLionel Sambuc free(write_buffer);
4854684ddb6SLionel Sambuc } else {
4864684ddb6SLionel Sambuc unmap_file();
4874684ddb6SLionel Sambuc }
4884684ddb6SLionel Sambuc
4894684ddb6SLionel Sambuc fclose(output_file);
4904684ddb6SLionel Sambuc output_file = NULL;
4914684ddb6SLionel Sambuc write_buffer = NULL;
4924684ddb6SLionel Sambuc }
4934684ddb6SLionel Sambuc free(filename);
4944684ddb6SLionel Sambuc
4954684ddb6SLionel Sambuc #ifdef DEBUG_GCDAPROFILING
4964684ddb6SLionel Sambuc fprintf(stderr, "llvmgcda: -----\n");
4974684ddb6SLionel Sambuc #endif
4984684ddb6SLionel Sambuc }
4994684ddb6SLionel Sambuc
llvm_register_writeout_function(writeout_fn fn)5004684ddb6SLionel Sambuc void llvm_register_writeout_function(writeout_fn fn) {
5014684ddb6SLionel Sambuc struct writeout_fn_node *new_node = malloc(sizeof(struct writeout_fn_node));
5024684ddb6SLionel Sambuc new_node->fn = fn;
5034684ddb6SLionel Sambuc new_node->next = NULL;
5044684ddb6SLionel Sambuc
5054684ddb6SLionel Sambuc if (!writeout_fn_head) {
5064684ddb6SLionel Sambuc writeout_fn_head = writeout_fn_tail = new_node;
5074684ddb6SLionel Sambuc } else {
5084684ddb6SLionel Sambuc writeout_fn_tail->next = new_node;
5094684ddb6SLionel Sambuc writeout_fn_tail = new_node;
5104684ddb6SLionel Sambuc }
5114684ddb6SLionel Sambuc }
5124684ddb6SLionel Sambuc
llvm_writeout_files()5134684ddb6SLionel Sambuc void llvm_writeout_files() {
5144684ddb6SLionel Sambuc struct writeout_fn_node *curr = writeout_fn_head;
5154684ddb6SLionel Sambuc
5164684ddb6SLionel Sambuc while (curr) {
5174684ddb6SLionel Sambuc curr->fn();
5184684ddb6SLionel Sambuc curr = curr->next;
5194684ddb6SLionel Sambuc }
5204684ddb6SLionel Sambuc }
5214684ddb6SLionel Sambuc
llvm_delete_writeout_function_list()5224684ddb6SLionel Sambuc void llvm_delete_writeout_function_list() {
5234684ddb6SLionel Sambuc while (writeout_fn_head) {
5244684ddb6SLionel Sambuc struct writeout_fn_node *node = writeout_fn_head;
5254684ddb6SLionel Sambuc writeout_fn_head = writeout_fn_head->next;
5264684ddb6SLionel Sambuc free(node);
5274684ddb6SLionel Sambuc }
5284684ddb6SLionel Sambuc
5294684ddb6SLionel Sambuc writeout_fn_head = writeout_fn_tail = NULL;
5304684ddb6SLionel Sambuc }
5314684ddb6SLionel Sambuc
llvm_register_flush_function(flush_fn fn)5324684ddb6SLionel Sambuc void llvm_register_flush_function(flush_fn fn) {
5334684ddb6SLionel Sambuc struct flush_fn_node *new_node = malloc(sizeof(struct flush_fn_node));
5344684ddb6SLionel Sambuc new_node->fn = fn;
5354684ddb6SLionel Sambuc new_node->next = NULL;
5364684ddb6SLionel Sambuc
5374684ddb6SLionel Sambuc if (!flush_fn_head) {
5384684ddb6SLionel Sambuc flush_fn_head = flush_fn_tail = new_node;
5394684ddb6SLionel Sambuc } else {
5404684ddb6SLionel Sambuc flush_fn_tail->next = new_node;
5414684ddb6SLionel Sambuc flush_fn_tail = new_node;
5424684ddb6SLionel Sambuc }
5434684ddb6SLionel Sambuc }
5444684ddb6SLionel Sambuc
__gcov_flush()5454684ddb6SLionel Sambuc void __gcov_flush() {
5464684ddb6SLionel Sambuc struct flush_fn_node *curr = flush_fn_head;
5474684ddb6SLionel Sambuc
5484684ddb6SLionel Sambuc while (curr) {
5494684ddb6SLionel Sambuc curr->fn();
5504684ddb6SLionel Sambuc curr = curr->next;
5514684ddb6SLionel Sambuc }
5524684ddb6SLionel Sambuc }
5534684ddb6SLionel Sambuc
llvm_delete_flush_function_list()5544684ddb6SLionel Sambuc void llvm_delete_flush_function_list() {
5554684ddb6SLionel Sambuc while (flush_fn_head) {
5564684ddb6SLionel Sambuc struct flush_fn_node *node = flush_fn_head;
5574684ddb6SLionel Sambuc flush_fn_head = flush_fn_head->next;
5584684ddb6SLionel Sambuc free(node);
5594684ddb6SLionel Sambuc }
5604684ddb6SLionel Sambuc
5614684ddb6SLionel Sambuc flush_fn_head = flush_fn_tail = NULL;
5624684ddb6SLionel Sambuc }
5634684ddb6SLionel Sambuc
llvm_gcov_init(writeout_fn wfn,flush_fn ffn)5644684ddb6SLionel Sambuc void llvm_gcov_init(writeout_fn wfn, flush_fn ffn) {
5654684ddb6SLionel Sambuc static int atexit_ran = 0;
5664684ddb6SLionel Sambuc
5674684ddb6SLionel Sambuc if (wfn)
5684684ddb6SLionel Sambuc llvm_register_writeout_function(wfn);
5694684ddb6SLionel Sambuc
5704684ddb6SLionel Sambuc if (ffn)
5714684ddb6SLionel Sambuc llvm_register_flush_function(ffn);
5724684ddb6SLionel Sambuc
5734684ddb6SLionel Sambuc if (atexit_ran == 0) {
5744684ddb6SLionel Sambuc atexit_ran = 1;
5754684ddb6SLionel Sambuc
5764684ddb6SLionel Sambuc /* Make sure we write out the data and delete the data structures. */
5774684ddb6SLionel Sambuc atexit(llvm_delete_flush_function_list);
5784684ddb6SLionel Sambuc atexit(llvm_delete_writeout_function_list);
5794684ddb6SLionel Sambuc atexit(llvm_writeout_files);
5804684ddb6SLionel Sambuc }
5814684ddb6SLionel Sambuc }
582