2011-11-17 08:12:10 +08:00
|
|
|
/*===- GCDAProfiling.c - Support library for GCDA file emission -----------===*\
|
|
|
|
|*
|
|
|
|
|* The LLVM Compiler Infrastructure
|
|
|
|
|*
|
|
|
|
|* This file is distributed under the University of Illinois Open Source
|
|
|
|
|* License. See LICENSE.TXT for details.
|
|
|
|
|*
|
|
|
|
|*===----------------------------------------------------------------------===*|
|
|
|
|
|*
|
|
|
|
|* This file implements the call back routines for the gcov profiling
|
|
|
|
|* instrumentation pass. Link against this library when running code through
|
|
|
|
|* the -insert-gcov-profiling LLVM pass.
|
|
|
|
|*
|
|
|
|
|* We emit files in a corrupt version of GCOV's "gcda" file format. These files
|
|
|
|
|* are only close enough that LCOV will happily parse them. Anything that lcov
|
|
|
|
|* ignores is missing.
|
|
|
|
|*
|
|
|
|
|* TODO: gcov is multi-process safe by having each exit open the existing file
|
|
|
|
|* and append to it. We'd like to achieve that and be thread-safe too.
|
|
|
|
|*
|
|
|
|
\*===----------------------------------------------------------------------===*/
|
|
|
|
|
2016-07-16 02:48:14 +08:00
|
|
|
#include "InstrProfilingPort.h"
|
2016-07-19 00:16:12 +08:00
|
|
|
#include "InstrProfilingUtil.h"
|
2015-07-10 01:21:52 +08:00
|
|
|
|
2013-06-25 08:37:32 +08:00
|
|
|
#include <errno.h>
|
2013-05-23 15:18:59 +08:00
|
|
|
#include <fcntl.h>
|
2011-11-17 08:12:10 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2016-01-06 01:27:01 +08:00
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
#include "WindowsMMap.h"
|
|
|
|
#else
|
2013-05-23 15:18:59 +08:00
|
|
|
#include <sys/mman.h>
|
2015-04-04 03:10:35 +08:00
|
|
|
#include <sys/file.h>
|
2016-07-26 07:12:53 +08:00
|
|
|
#ifndef MAP_FILE
|
|
|
|
#define MAP_FILE 0
|
|
|
|
#endif
|
2016-01-06 01:27:01 +08:00
|
|
|
#endif
|
2011-11-17 08:12:10 +08:00
|
|
|
|
2016-01-20 06:02:12 +08:00
|
|
|
#if defined(__FreeBSD__) && defined(__i386__)
|
|
|
|
#define I386_FREEBSD 1
|
|
|
|
#else
|
|
|
|
#define I386_FREEBSD 0
|
|
|
|
#endif
|
2014-03-11 00:50:53 +08:00
|
|
|
|
|
|
|
#if !defined(_MSC_VER) && !I386_FREEBSD
|
2011-11-17 08:12:10 +08:00
|
|
|
#include <stdint.h>
|
2014-03-11 00:50:53 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(_MSC_VER)
|
2016-01-06 01:27:01 +08:00
|
|
|
typedef unsigned char uint8_t;
|
2011-11-17 08:12:10 +08:00
|
|
|
typedef unsigned int uint32_t;
|
2014-03-20 06:10:24 +08:00
|
|
|
typedef unsigned long long uint64_t;
|
2014-03-11 00:50:53 +08:00
|
|
|
#elif I386_FREEBSD
|
|
|
|
/* System headers define 'size_t' incorrectly on x64 FreeBSD (prior to
|
|
|
|
* FreeBSD 10, r232261) when compiled in 32-bit mode.
|
|
|
|
*/
|
|
|
|
typedef unsigned char uint8_t;
|
|
|
|
typedef unsigned int uint32_t;
|
|
|
|
typedef unsigned long long uint64_t;
|
2011-11-17 08:12:10 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* #define DEBUG_GCDAPROFILING */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* --- GCOV file format I/O primitives ---
|
|
|
|
*/
|
|
|
|
|
2013-06-25 08:37:32 +08:00
|
|
|
/*
|
|
|
|
* The current file name we're outputting. Used primarily for error logging.
|
|
|
|
*/
|
|
|
|
static char *filename = NULL;
|
|
|
|
|
2013-03-19 06:59:47 +08:00
|
|
|
/*
|
|
|
|
* The current file we're outputting.
|
|
|
|
*/
|
2011-11-17 08:12:10 +08:00
|
|
|
static FILE *output_file = NULL;
|
|
|
|
|
2013-05-16 05:31:22 +08:00
|
|
|
/*
|
|
|
|
* Buffer that we write things into.
|
|
|
|
*/
|
2013-05-24 02:18:31 +08:00
|
|
|
#define WRITE_BUFFER_SIZE (128 * 1024)
|
|
|
|
static char *write_buffer = NULL;
|
2013-05-23 15:18:59 +08:00
|
|
|
static uint64_t cur_buffer_size = 0;
|
|
|
|
static uint64_t cur_pos = 0;
|
|
|
|
static uint64_t file_size = 0;
|
2013-05-16 05:31:22 +08:00
|
|
|
static int new_file = 0;
|
2013-05-23 15:18:59 +08:00
|
|
|
static int fd = -1;
|
2013-05-16 05:31:22 +08:00
|
|
|
|
2013-03-20 05:01:19 +08:00
|
|
|
/*
|
|
|
|
* A list of functions to write out the data.
|
|
|
|
*/
|
|
|
|
typedef void (*writeout_fn)();
|
|
|
|
|
|
|
|
struct writeout_fn_node {
|
|
|
|
writeout_fn fn;
|
|
|
|
struct writeout_fn_node *next;
|
|
|
|
};
|
|
|
|
|
2013-05-16 05:31:22 +08:00
|
|
|
static struct writeout_fn_node *writeout_fn_head = NULL;
|
|
|
|
static struct writeout_fn_node *writeout_fn_tail = NULL;
|
2013-03-20 05:01:19 +08:00
|
|
|
|
2013-03-19 06:59:47 +08:00
|
|
|
/*
|
|
|
|
* A list of flush functions that our __gcov_flush() function should call.
|
|
|
|
*/
|
|
|
|
typedef void (*flush_fn)();
|
|
|
|
|
|
|
|
struct flush_fn_node {
|
|
|
|
flush_fn fn;
|
|
|
|
struct flush_fn_node *next;
|
|
|
|
};
|
|
|
|
|
2013-05-16 05:31:22 +08:00
|
|
|
static struct flush_fn_node *flush_fn_head = NULL;
|
|
|
|
static struct flush_fn_node *flush_fn_tail = NULL;
|
|
|
|
|
2013-05-23 15:18:59 +08:00
|
|
|
static void resize_write_buffer(uint64_t size) {
|
|
|
|
if (!new_file) return;
|
|
|
|
size += cur_pos;
|
|
|
|
if (size <= cur_buffer_size) return;
|
2013-05-24 02:18:31 +08:00
|
|
|
size = (size - 1) / WRITE_BUFFER_SIZE + 1;
|
|
|
|
size *= WRITE_BUFFER_SIZE;
|
|
|
|
write_buffer = realloc(write_buffer, size);
|
2013-05-23 15:18:59 +08:00
|
|
|
cur_buffer_size = size;
|
2011-11-17 08:12:10 +08:00
|
|
|
}
|
|
|
|
|
2013-05-16 05:31:22 +08:00
|
|
|
static void write_bytes(const char *s, size_t len) {
|
2013-05-23 15:18:59 +08:00
|
|
|
resize_write_buffer(len);
|
2013-05-24 02:18:31 +08:00
|
|
|
memcpy(&write_buffer[cur_pos], s, len);
|
2013-05-23 15:18:59 +08:00
|
|
|
cur_pos += len;
|
2013-05-16 05:31:22 +08:00
|
|
|
}
|
2013-04-22 11:36:22 +08:00
|
|
|
|
2013-05-16 05:31:22 +08:00
|
|
|
static void write_32bit_value(uint32_t i) {
|
|
|
|
write_bytes((char*)&i, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void write_64bit_value(uint64_t i) {
|
|
|
|
write_bytes((char*)&i, 8);
|
2011-11-17 08:12:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t length_of_string(const char *s) {
|
|
|
|
return (strlen(s) / 4) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void write_string(const char *s) {
|
|
|
|
uint32_t len = length_of_string(s);
|
2013-05-16 05:31:22 +08:00
|
|
|
write_32bit_value(len);
|
|
|
|
write_bytes(s, strlen(s));
|
|
|
|
write_bytes("\0\0\0\0", 4 - (strlen(s) % 4));
|
2011-11-17 08:12:10 +08:00
|
|
|
}
|
|
|
|
|
2013-05-16 05:31:22 +08:00
|
|
|
static uint32_t read_32bit_value() {
|
|
|
|
uint32_t val;
|
|
|
|
|
|
|
|
if (new_file)
|
2012-09-15 02:55:32 +08:00
|
|
|
return (uint32_t)-1;
|
|
|
|
|
2013-05-24 02:18:31 +08:00
|
|
|
val = *(uint32_t*)&write_buffer[cur_pos];
|
2013-05-23 15:18:59 +08:00
|
|
|
cur_pos += 4;
|
2013-05-16 05:31:22 +08:00
|
|
|
return val;
|
2012-09-15 02:55:32 +08:00
|
|
|
}
|
|
|
|
|
2013-05-16 05:31:22 +08:00
|
|
|
static uint64_t read_64bit_value() {
|
|
|
|
uint64_t val;
|
2012-09-15 02:55:32 +08:00
|
|
|
|
2013-05-16 05:31:22 +08:00
|
|
|
if (new_file)
|
2013-05-23 15:18:59 +08:00
|
|
|
return (uint64_t)-1;
|
2013-05-16 05:31:22 +08:00
|
|
|
|
2013-05-24 02:18:31 +08:00
|
|
|
val = *(uint64_t*)&write_buffer[cur_pos];
|
2013-05-23 15:18:59 +08:00
|
|
|
cur_pos += 8;
|
2013-05-16 05:31:22 +08:00
|
|
|
return val;
|
2012-09-15 02:55:32 +08:00
|
|
|
}
|
|
|
|
|
2011-11-17 08:12:10 +08:00
|
|
|
static char *mangle_filename(const char *orig_filename) {
|
2014-01-16 04:57:10 +08:00
|
|
|
char *new_filename;
|
2016-07-19 00:16:12 +08:00
|
|
|
size_t prefix_len;
|
2014-01-16 04:57:10 +08:00
|
|
|
int prefix_strip;
|
2016-07-19 00:16:12 +08:00
|
|
|
const char *prefix = lprofGetPathPrefix(&prefix_strip, &prefix_len);
|
2011-11-17 08:12:10 +08:00
|
|
|
|
2016-07-19 00:16:12 +08:00
|
|
|
if (prefix == NULL)
|
2011-11-17 08:12:10 +08:00
|
|
|
return strdup(orig_filename);
|
|
|
|
|
2016-07-19 00:16:12 +08:00
|
|
|
new_filename = malloc(prefix_len + 1 + strlen(orig_filename) + 1);
|
|
|
|
lprofApplyPathPrefix(new_filename, orig_filename, prefix, prefix_len,
|
|
|
|
prefix_strip);
|
2011-11-17 08:12:10 +08:00
|
|
|
|
2014-01-16 04:57:10 +08:00
|
|
|
return new_filename;
|
2011-11-17 08:12:10 +08:00
|
|
|
}
|
|
|
|
|
2013-06-25 08:37:32 +08:00
|
|
|
static int map_file() {
|
2013-05-23 15:18:59 +08:00
|
|
|
fseek(output_file, 0L, SEEK_END);
|
|
|
|
file_size = ftell(output_file);
|
|
|
|
|
2013-09-12 03:35:32 +08:00
|
|
|
/* A size of 0 is invalid to `mmap'. Return a fail here, but don't issue an
|
2013-09-10 06:25:46 +08:00
|
|
|
* error message because it should "just work" for the user. */
|
|
|
|
if (file_size == 0)
|
|
|
|
return -1;
|
|
|
|
|
2013-05-24 02:18:31 +08:00
|
|
|
write_buffer = mmap(0, file_size, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_FILE | MAP_SHARED, fd, 0);
|
2013-06-25 08:37:32 +08:00
|
|
|
if (write_buffer == (void *)-1) {
|
|
|
|
int errnum = errno;
|
|
|
|
fprintf(stderr, "profiling: %s: cannot map: %s\n", filename,
|
|
|
|
strerror(errnum));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2013-05-23 15:18:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void unmap_file() {
|
2013-06-25 08:37:32 +08:00
|
|
|
if (msync(write_buffer, file_size, MS_SYNC) == -1) {
|
|
|
|
int errnum = errno;
|
|
|
|
fprintf(stderr, "profiling: %s: cannot msync: %s\n", filename,
|
|
|
|
strerror(errnum));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We explicitly ignore errors from unmapping because at this point the data
|
|
|
|
* is written and we don't care.
|
|
|
|
*/
|
|
|
|
(void)munmap(write_buffer, file_size);
|
2013-05-24 02:18:31 +08:00
|
|
|
write_buffer = NULL;
|
2013-05-23 15:18:59 +08:00
|
|
|
file_size = 0;
|
|
|
|
}
|
|
|
|
|
2011-11-17 08:12:10 +08:00
|
|
|
/*
|
|
|
|
* --- LLVM line counter API ---
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* A file in this case is a translation unit. Each .o file built with line
|
|
|
|
* profiling enabled will emit to a different file. Only one file may be
|
|
|
|
* started at a time.
|
|
|
|
*/
|
2013-11-20 12:14:48 +08:00
|
|
|
void llvm_gcda_start_file(const char *orig_filename, const char version[4],
|
|
|
|
uint32_t checksum) {
|
2013-05-23 15:18:59 +08:00
|
|
|
const char *mode = "r+b";
|
2013-06-25 08:37:32 +08:00
|
|
|
filename = mangle_filename(orig_filename);
|
2012-09-15 02:55:32 +08:00
|
|
|
|
|
|
|
/* Try just opening the file. */
|
2013-05-23 15:18:59 +08:00
|
|
|
new_file = 0;
|
|
|
|
fd = open(filename, O_RDWR);
|
2012-05-28 10:34:34 +08:00
|
|
|
|
2013-05-23 15:18:59 +08:00
|
|
|
if (fd == -1) {
|
2012-09-15 02:55:32 +08:00
|
|
|
/* Try opening the file, creating it if necessary. */
|
2013-05-16 05:31:22 +08:00
|
|
|
new_file = 1;
|
2013-05-23 15:18:59 +08:00
|
|
|
mode = "w+b";
|
2013-05-24 02:18:31 +08:00
|
|
|
fd = open(filename, O_RDWR | O_CREAT, 0644);
|
2013-05-23 15:18:59 +08:00
|
|
|
if (fd == -1) {
|
2012-09-15 02:55:32 +08:00
|
|
|
/* Try creating the directories first then opening the file. */
|
2015-07-10 01:21:52 +08:00
|
|
|
__llvm_profile_recursive_mkdir(filename);
|
2013-05-24 02:18:31 +08:00
|
|
|
fd = open(filename, O_RDWR | O_CREAT, 0644);
|
2013-06-25 08:37:32 +08:00
|
|
|
if (fd == -1) {
|
2012-09-15 02:55:32 +08:00
|
|
|
/* Bah! It's hopeless. */
|
2013-06-25 08:37:32 +08:00
|
|
|
int errnum = errno;
|
|
|
|
fprintf(stderr, "profiling: %s: cannot open: %s\n", filename,
|
|
|
|
strerror(errnum));
|
2012-09-15 02:55:32 +08:00
|
|
|
return;
|
|
|
|
}
|
2012-05-28 10:34:34 +08:00
|
|
|
}
|
|
|
|
}
|
2011-11-17 08:12:10 +08:00
|
|
|
|
2015-04-04 02:55:44 +08:00
|
|
|
/* Try to flock the file to serialize concurrent processes writing out to the
|
|
|
|
* same GCDA. This can fail if the filesystem doesn't support it, but in that
|
|
|
|
* case we'll just carry on with the old racy behaviour and hope for the best.
|
|
|
|
*/
|
|
|
|
flock(fd, LOCK_EX);
|
2013-05-23 15:18:59 +08:00
|
|
|
output_file = fdopen(fd, mode);
|
2013-05-16 05:31:22 +08:00
|
|
|
|
|
|
|
/* Initialize the write buffer. */
|
2013-05-24 02:18:31 +08:00
|
|
|
write_buffer = NULL;
|
2013-05-23 15:18:59 +08:00
|
|
|
cur_buffer_size = 0;
|
|
|
|
cur_pos = 0;
|
|
|
|
|
|
|
|
if (new_file) {
|
2013-05-24 02:18:31 +08:00
|
|
|
resize_write_buffer(WRITE_BUFFER_SIZE);
|
|
|
|
memset(write_buffer, 0, WRITE_BUFFER_SIZE);
|
2013-05-23 15:18:59 +08:00
|
|
|
} else {
|
2013-06-25 08:37:32 +08:00
|
|
|
if (map_file() == -1) {
|
|
|
|
/* mmap failed, try to recover by clobbering */
|
|
|
|
new_file = 1;
|
|
|
|
write_buffer = NULL;
|
|
|
|
cur_buffer_size = 0;
|
|
|
|
resize_write_buffer(WRITE_BUFFER_SIZE);
|
|
|
|
memset(write_buffer, 0, WRITE_BUFFER_SIZE);
|
|
|
|
}
|
2013-05-23 15:18:59 +08:00
|
|
|
}
|
2013-05-16 05:31:22 +08:00
|
|
|
|
2013-11-20 12:14:48 +08:00
|
|
|
/* gcda file, version, stamp checksum. */
|
2013-05-16 05:31:22 +08:00
|
|
|
write_bytes("adcg", 4);
|
|
|
|
write_bytes(version, 4);
|
2013-11-20 12:14:48 +08:00
|
|
|
write_32bit_value(checksum);
|
2013-05-16 05:31:22 +08:00
|
|
|
|
2011-11-17 08:12:10 +08:00
|
|
|
#ifdef DEBUG_GCDAPROFILING
|
2012-09-15 02:55:32 +08:00
|
|
|
fprintf(stderr, "llvmgcda: [%s]\n", orig_filename);
|
2011-11-17 08:12:10 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Given an array of pointers to counters (counters), increment the n-th one,
|
|
|
|
* where we're also given a pointer to n (predecessor).
|
|
|
|
*/
|
|
|
|
void llvm_gcda_increment_indirect_counter(uint32_t *predecessor,
|
|
|
|
uint64_t **counters) {
|
|
|
|
uint64_t *counter;
|
|
|
|
uint32_t pred;
|
|
|
|
|
|
|
|
pred = *predecessor;
|
|
|
|
if (pred == 0xffffffff)
|
|
|
|
return;
|
|
|
|
counter = counters[pred];
|
|
|
|
|
|
|
|
/* Don't crash if the pred# is out of sync. This can happen due to threads,
|
|
|
|
or because of a TODO in GCOVProfiling.cpp buildEdgeLookupTable(). */
|
|
|
|
if (counter)
|
|
|
|
++*counter;
|
|
|
|
#ifdef DEBUG_GCDAPROFILING
|
|
|
|
else
|
2012-05-28 10:50:53 +08:00
|
|
|
fprintf(stderr,
|
2012-09-15 02:55:32 +08:00
|
|
|
"llvmgcda: increment_indirect_counter counters=%08llx, pred=%u\n",
|
|
|
|
*counter, *predecessor);
|
2011-11-17 08:12:10 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-03-09 09:33:12 +08:00
|
|
|
void llvm_gcda_emit_function(uint32_t ident, const char *function_name,
|
2013-12-04 14:00:04 +08:00
|
|
|
uint32_t func_checksum, uint8_t use_extra_checksum,
|
2013-11-20 12:14:48 +08:00
|
|
|
uint32_t cfg_checksum) {
|
2013-03-09 09:33:12 +08:00
|
|
|
uint32_t len = 2;
|
2013-05-16 05:31:22 +08:00
|
|
|
|
2013-03-09 09:33:12 +08:00
|
|
|
if (use_extra_checksum)
|
|
|
|
len++;
|
2011-11-17 08:12:10 +08:00
|
|
|
#ifdef DEBUG_GCDAPROFILING
|
2013-02-28 15:00:13 +08:00
|
|
|
fprintf(stderr, "llvmgcda: function id=0x%08x name=%s\n", ident,
|
|
|
|
function_name ? function_name : "NULL");
|
2011-11-17 08:12:10 +08:00
|
|
|
#endif
|
2012-05-28 10:34:34 +08:00
|
|
|
if (!output_file) return;
|
2011-11-17 08:12:10 +08:00
|
|
|
|
2013-02-28 15:00:13 +08:00
|
|
|
/* function tag */
|
2013-05-16 05:31:22 +08:00
|
|
|
write_bytes("\0\0\0\1", 4);
|
2013-02-28 15:00:13 +08:00
|
|
|
if (function_name)
|
|
|
|
len += 1 + length_of_string(function_name);
|
2013-05-16 05:31:22 +08:00
|
|
|
write_32bit_value(len);
|
|
|
|
write_32bit_value(ident);
|
2013-12-04 14:00:04 +08:00
|
|
|
write_32bit_value(func_checksum);
|
2013-03-09 09:33:12 +08:00
|
|
|
if (use_extra_checksum)
|
2013-11-20 12:14:48 +08:00
|
|
|
write_32bit_value(cfg_checksum);
|
2013-02-28 15:00:13 +08:00
|
|
|
if (function_name)
|
|
|
|
write_string(function_name);
|
2011-11-17 08:12:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
|
|
|
|
uint32_t i;
|
2012-09-15 02:55:32 +08:00
|
|
|
uint64_t *old_ctrs = NULL;
|
|
|
|
uint32_t val = 0;
|
2013-05-23 15:18:59 +08:00
|
|
|
uint64_t save_cur_pos = cur_pos;
|
2012-05-28 10:34:34 +08:00
|
|
|
|
|
|
|
if (!output_file) return;
|
2012-09-15 02:55:32 +08:00
|
|
|
|
2013-05-16 05:31:22 +08:00
|
|
|
val = read_32bit_value();
|
2012-09-15 02:55:32 +08:00
|
|
|
|
|
|
|
if (val != (uint32_t)-1) {
|
|
|
|
/* There are counters present in the file. Merge them. */
|
|
|
|
if (val != 0x01a10000) {
|
2014-08-19 04:47:32 +08:00
|
|
|
fprintf(stderr, "profiling: %s: cannot merge previous GCDA file: "
|
|
|
|
"corrupt arc tag (0x%08x)\n",
|
|
|
|
filename, val);
|
2012-09-15 02:55:32 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-16 05:31:22 +08:00
|
|
|
val = read_32bit_value();
|
2012-09-15 02:55:32 +08:00
|
|
|
if (val == (uint32_t)-1 || val / 2 != num_counters) {
|
2014-08-19 04:47:32 +08:00
|
|
|
fprintf(stderr, "profiling: %s: cannot merge previous GCDA file: "
|
|
|
|
"mismatched number of counters (%d)\n",
|
|
|
|
filename, val);
|
2012-09-15 02:55:32 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
old_ctrs = malloc(sizeof(uint64_t) * num_counters);
|
2013-05-16 05:31:22 +08:00
|
|
|
for (i = 0; i < num_counters; ++i)
|
|
|
|
old_ctrs[i] = read_64bit_value();
|
|
|
|
}
|
2012-09-15 02:55:32 +08:00
|
|
|
|
2013-05-23 15:18:59 +08:00
|
|
|
cur_pos = save_cur_pos;
|
|
|
|
|
2012-09-15 02:55:32 +08:00
|
|
|
/* Counter #1 (arcs) tag */
|
2013-05-16 05:31:22 +08:00
|
|
|
write_bytes("\0\0\xa1\1", 4);
|
|
|
|
write_32bit_value(num_counters * 2);
|
|
|
|
for (i = 0; i < num_counters; ++i) {
|
2013-04-22 11:36:22 +08:00
|
|
|
counters[i] += (old_ctrs ? old_ctrs[i] : 0);
|
2013-05-16 05:31:22 +08:00
|
|
|
write_64bit_value(counters[i]);
|
2013-04-22 11:36:22 +08:00
|
|
|
}
|
2012-09-15 02:55:32 +08:00
|
|
|
|
|
|
|
free(old_ctrs);
|
2011-11-17 08:12:10 +08:00
|
|
|
|
|
|
|
#ifdef DEBUG_GCDAPROFILING
|
2012-09-15 02:55:32 +08:00
|
|
|
fprintf(stderr, "llvmgcda: %u arcs\n", num_counters);
|
2012-05-28 10:34:34 +08:00
|
|
|
for (i = 0; i < num_counters; ++i)
|
2012-09-15 02:55:32 +08:00
|
|
|
fprintf(stderr, "llvmgcda: %llu\n", (unsigned long long)counters[i]);
|
2011-11-17 08:12:10 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-11-13 02:45:50 +08:00
|
|
|
void llvm_gcda_summary_info() {
|
2013-12-11 03:05:43 +08:00
|
|
|
const uint32_t obj_summary_len = 9; /* Length for gcov compatibility. */
|
2013-11-13 02:45:50 +08:00
|
|
|
uint32_t i;
|
|
|
|
uint32_t runs = 1;
|
|
|
|
uint32_t val = 0;
|
|
|
|
uint64_t save_cur_pos = cur_pos;
|
|
|
|
|
|
|
|
if (!output_file) return;
|
|
|
|
|
|
|
|
val = read_32bit_value();
|
|
|
|
|
|
|
|
if (val != (uint32_t)-1) {
|
|
|
|
/* There are counters present in the file. Merge them. */
|
|
|
|
if (val != 0xa1000000) {
|
2014-08-19 04:47:32 +08:00
|
|
|
fprintf(stderr, "profiling: %s: cannot merge previous run count: "
|
|
|
|
"corrupt object tag (0x%08x)\n",
|
|
|
|
filename, val);
|
2013-11-13 02:45:50 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-11 03:05:43 +08:00
|
|
|
val = read_32bit_value(); /* length */
|
2013-11-13 02:45:50 +08:00
|
|
|
if (val != obj_summary_len) {
|
2014-08-19 04:47:32 +08:00
|
|
|
fprintf(stderr, "profiling: %s: cannot merge previous run count: "
|
|
|
|
"mismatched object length (%d)\n",
|
|
|
|
filename, val);
|
2013-11-13 02:45:50 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-11 03:05:43 +08:00
|
|
|
read_32bit_value(); /* checksum, unused */
|
|
|
|
read_32bit_value(); /* num, unused */
|
|
|
|
runs += read_32bit_value(); /* Add previous run count to new counter. */
|
2013-11-13 02:45:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cur_pos = save_cur_pos;
|
|
|
|
|
|
|
|
/* Object summary tag */
|
|
|
|
write_bytes("\0\0\0\xa1", 4);
|
|
|
|
write_32bit_value(obj_summary_len);
|
2013-12-11 03:05:43 +08:00
|
|
|
write_32bit_value(0); /* checksum, unused */
|
|
|
|
write_32bit_value(0); /* num, unused */
|
2013-11-13 02:45:50 +08:00
|
|
|
write_32bit_value(runs);
|
2013-12-11 03:05:43 +08:00
|
|
|
for (i = 3; i < obj_summary_len; ++i)
|
2013-11-13 02:45:50 +08:00
|
|
|
write_32bit_value(0);
|
|
|
|
|
|
|
|
/* Program summary tag */
|
2013-12-11 03:05:43 +08:00
|
|
|
write_bytes("\0\0\0\xa3", 4); /* tag indicates 1 program */
|
|
|
|
write_32bit_value(0); /* 0 length */
|
2013-11-13 02:45:50 +08:00
|
|
|
|
|
|
|
#ifdef DEBUG_GCDAPROFILING
|
|
|
|
fprintf(stderr, "llvmgcda: %u runs\n", runs);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-11-17 08:12:10 +08:00
|
|
|
void llvm_gcda_end_file() {
|
|
|
|
/* Write out EOF record. */
|
2013-06-26 08:26:16 +08:00
|
|
|
if (output_file) {
|
|
|
|
write_bytes("\0\0\0\0\0\0\0\0", 8);
|
|
|
|
|
|
|
|
if (new_file) {
|
|
|
|
fwrite(write_buffer, cur_pos, 1, output_file);
|
|
|
|
free(write_buffer);
|
|
|
|
} else {
|
|
|
|
unmap_file();
|
|
|
|
}
|
2013-05-23 15:18:59 +08:00
|
|
|
|
2015-04-04 02:55:44 +08:00
|
|
|
flock(fd, LOCK_UN);
|
2016-03-06 04:10:25 +08:00
|
|
|
fclose(output_file);
|
2013-06-26 08:26:16 +08:00
|
|
|
output_file = NULL;
|
|
|
|
write_buffer = NULL;
|
2013-05-23 15:18:59 +08:00
|
|
|
}
|
2013-06-26 08:26:16 +08:00
|
|
|
free(filename);
|
2011-11-17 08:12:10 +08:00
|
|
|
|
|
|
|
#ifdef DEBUG_GCDAPROFILING
|
2012-09-15 02:55:32 +08:00
|
|
|
fprintf(stderr, "llvmgcda: -----\n");
|
2011-11-17 08:12:10 +08:00
|
|
|
#endif
|
|
|
|
}
|
2013-03-19 06:59:47 +08:00
|
|
|
|
2013-03-20 05:01:19 +08:00
|
|
|
void llvm_register_writeout_function(writeout_fn fn) {
|
|
|
|
struct writeout_fn_node *new_node = malloc(sizeof(struct writeout_fn_node));
|
|
|
|
new_node->fn = fn;
|
|
|
|
new_node->next = NULL;
|
|
|
|
|
|
|
|
if (!writeout_fn_head) {
|
|
|
|
writeout_fn_head = writeout_fn_tail = new_node;
|
|
|
|
} else {
|
|
|
|
writeout_fn_tail->next = new_node;
|
|
|
|
writeout_fn_tail = new_node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-11 08:22:43 +08:00
|
|
|
void llvm_writeout_files(void) {
|
2013-03-20 05:01:19 +08:00
|
|
|
struct writeout_fn_node *curr = writeout_fn_head;
|
|
|
|
|
|
|
|
while (curr) {
|
|
|
|
curr->fn();
|
|
|
|
curr = curr->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-11 08:22:43 +08:00
|
|
|
void llvm_delete_writeout_function_list(void) {
|
2013-03-20 05:01:19 +08:00
|
|
|
while (writeout_fn_head) {
|
|
|
|
struct writeout_fn_node *node = writeout_fn_head;
|
|
|
|
writeout_fn_head = writeout_fn_head->next;
|
|
|
|
free(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeout_fn_head = writeout_fn_tail = NULL;
|
|
|
|
}
|
|
|
|
|
2013-03-19 06:59:47 +08:00
|
|
|
void llvm_register_flush_function(flush_fn fn) {
|
|
|
|
struct flush_fn_node *new_node = malloc(sizeof(struct flush_fn_node));
|
|
|
|
new_node->fn = fn;
|
|
|
|
new_node->next = NULL;
|
|
|
|
|
|
|
|
if (!flush_fn_head) {
|
|
|
|
flush_fn_head = flush_fn_tail = new_node;
|
|
|
|
} else {
|
|
|
|
flush_fn_tail->next = new_node;
|
|
|
|
flush_fn_tail = new_node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void __gcov_flush() {
|
|
|
|
struct flush_fn_node *curr = flush_fn_head;
|
|
|
|
|
|
|
|
while (curr) {
|
|
|
|
curr->fn();
|
|
|
|
curr = curr->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-11 08:22:43 +08:00
|
|
|
void llvm_delete_flush_function_list(void) {
|
2013-03-19 06:59:47 +08:00
|
|
|
while (flush_fn_head) {
|
|
|
|
struct flush_fn_node *node = flush_fn_head;
|
|
|
|
flush_fn_head = flush_fn_head->next;
|
|
|
|
free(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
flush_fn_head = flush_fn_tail = NULL;
|
|
|
|
}
|
2013-03-21 05:11:47 +08:00
|
|
|
|
|
|
|
void llvm_gcov_init(writeout_fn wfn, flush_fn ffn) {
|
|
|
|
static int atexit_ran = 0;
|
|
|
|
|
|
|
|
if (wfn)
|
|
|
|
llvm_register_writeout_function(wfn);
|
|
|
|
|
|
|
|
if (ffn)
|
|
|
|
llvm_register_flush_function(ffn);
|
|
|
|
|
|
|
|
if (atexit_ran == 0) {
|
|
|
|
atexit_ran = 1;
|
|
|
|
|
|
|
|
/* Make sure we write out the data and delete the data structures. */
|
|
|
|
atexit(llvm_delete_flush_function_list);
|
|
|
|
atexit(llvm_delete_writeout_function_list);
|
|
|
|
atexit(llvm_writeout_files);
|
|
|
|
}
|
|
|
|
}
|