2011-04-16 01:20:23 +00: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.
|
|
|
|
|*
|
2011-04-26 03:54:16 +00:00
|
|
|
|* 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.
|
|
|
|
|*
|
2011-04-16 01:20:23 +00:00
|
|
|
\*===----------------------------------------------------------------------===*/
|
|
|
|
|
|
|
|
#include "llvm/Support/DataTypes.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2011-05-04 22:34:29 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2011-10-08 15:49:19 +00:00
|
|
|
#ifdef _WIN32
|
2011-05-26 04:55:20 +00:00
|
|
|
#include <direct.h>
|
|
|
|
#endif
|
2011-04-16 01:20:23 +00:00
|
|
|
|
2011-04-16 04:25:36 +00:00
|
|
|
/* #define DEBUG_GCDAPROFILING */
|
2011-04-16 01:20:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* --- GCOV file format I/O primitives ---
|
|
|
|
*/
|
|
|
|
|
|
|
|
static FILE *output_file = NULL;
|
|
|
|
|
|
|
|
static void write_int32(uint32_t i) {
|
|
|
|
fwrite(&i, 4, 1, output_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void write_int64(uint64_t i) {
|
2012-05-25 21:57:59 +00:00
|
|
|
uint32_t lo = i >> 0;
|
|
|
|
uint32_t hi = i >> 32;
|
2011-04-16 01:20:23 +00:00
|
|
|
write_int32(lo);
|
|
|
|
write_int32(hi);
|
|
|
|
}
|
|
|
|
|
2011-05-05 02:46:38 +00:00
|
|
|
static uint32_t length_of_string(const char *s) {
|
2011-05-05 23:52:18 +00:00
|
|
|
return (strlen(s) / 4) + 1;
|
2011-05-05 02:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void write_string(const char *s) {
|
|
|
|
uint32_t len = length_of_string(s);
|
|
|
|
write_int32(len);
|
|
|
|
fwrite(s, strlen(s), 1, output_file);
|
|
|
|
fwrite("\0\0\0\0", 4 - (strlen(s) % 4), 1, output_file);
|
|
|
|
}
|
|
|
|
|
2011-04-28 21:35:49 +00:00
|
|
|
static char *mangle_filename(const char *orig_filename) {
|
|
|
|
/* TODO: handle GCOV_PREFIX_STRIP */
|
|
|
|
const char *prefix;
|
|
|
|
char *filename = 0;
|
|
|
|
|
|
|
|
prefix = getenv("GCOV_PREFIX");
|
|
|
|
|
|
|
|
if (!prefix)
|
2011-05-04 03:58:45 +00:00
|
|
|
return strdup(orig_filename);
|
2011-04-28 21:35:49 +00:00
|
|
|
|
|
|
|
filename = malloc(strlen(prefix) + 1 + strlen(orig_filename) + 1);
|
|
|
|
strcpy(filename, prefix);
|
|
|
|
strcat(filename, "/");
|
|
|
|
strcat(filename, orig_filename);
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2011-05-04 22:34:29 +00:00
|
|
|
static void recursive_mkdir(const char *filename) {
|
|
|
|
char *pathname;
|
|
|
|
int i, e;
|
|
|
|
|
|
|
|
for (i = 1, e = strlen(filename); i != e; ++i) {
|
2012-05-25 00:57:21 +00:00
|
|
|
if (filename[i] != '/') continue;
|
|
|
|
pathname = malloc(i + 1);
|
|
|
|
strncpy(pathname, filename, i);
|
|
|
|
pathname[i] = '\0';
|
2011-06-23 18:24:27 +00:00
|
|
|
#ifdef _WIN32
|
2012-05-25 00:57:21 +00:00
|
|
|
_mkdir(pathname);
|
2011-05-26 04:55:20 +00:00
|
|
|
#else
|
2012-05-25 00:57:21 +00:00
|
|
|
mkdir(pathname, 0750); /* some of these will fail, ignore it. */
|
2011-05-26 04:55:20 +00:00
|
|
|
#endif
|
2012-05-25 00:57:21 +00:00
|
|
|
free(pathname);
|
2011-05-04 22:34:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-16 01:20:23 +00: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.
|
|
|
|
*/
|
2011-04-28 21:35:49 +00:00
|
|
|
void llvm_gcda_start_file(const char *orig_filename) {
|
|
|
|
char *filename;
|
|
|
|
filename = mangle_filename(orig_filename);
|
2011-05-04 22:34:29 +00:00
|
|
|
recursive_mkdir(filename);
|
2012-05-25 21:55:06 +00:00
|
|
|
output_file = fopen(filename, "w+b");
|
2011-04-16 01:20:23 +00:00
|
|
|
|
2012-03-27 21:17:04 +00:00
|
|
|
if (!output_file) {
|
2012-03-28 01:30:51 +00:00
|
|
|
const char *cptr = strrchr(orig_filename, '/');
|
2012-05-25 21:55:06 +00:00
|
|
|
output_file = fopen(cptr ? cptr + 1 : orig_filename, "w+b");
|
2012-03-27 21:17:04 +00:00
|
|
|
|
|
|
|
if (!output_file) {
|
2012-05-25 21:55:06 +00:00
|
|
|
fprintf(stderr, "LLVM profiling runtime: cannot open '%s': ",
|
2012-03-28 02:39:06 +00:00
|
|
|
cptr ? cptr + 1 : orig_filename);
|
2012-03-27 21:17:04 +00:00
|
|
|
perror("");
|
2012-06-15 09:11:47 +00:00
|
|
|
free(filename);
|
2012-05-25 00:55:38 +00:00
|
|
|
return;
|
2012-03-27 21:17:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-16 01:20:23 +00:00
|
|
|
/* gcda file, version 404*, stamp LLVM. */
|
2011-07-28 18:12:20 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
fwrite("adcg*204MVLL", 12, 1, output_file);
|
|
|
|
#else
|
2011-04-16 01:20:23 +00:00
|
|
|
fwrite("adcg*404MVLL", 12, 1, output_file);
|
2011-07-28 18:12:20 +00:00
|
|
|
#endif
|
2011-04-16 01:20:23 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_GCDAPROFILING
|
2011-04-28 21:35:49 +00:00
|
|
|
printf("llvmgcda: [%s]\n", orig_filename);
|
2011-04-26 03:54:16 +00:00
|
|
|
#endif
|
2011-04-28 21:35:49 +00:00
|
|
|
|
|
|
|
free(filename);
|
2011-04-26 03:54:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
2011-04-28 21:35:49 +00:00
|
|
|
uint32_t pred;
|
|
|
|
|
|
|
|
pred = *predecessor;
|
|
|
|
if (pred == 0xffffffff)
|
2011-04-26 03:54:16 +00:00
|
|
|
return;
|
2011-04-28 21:35:49 +00:00
|
|
|
counter = counters[pred];
|
2011-04-26 03:54:16 +00:00
|
|
|
|
|
|
|
/* 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(). */
|
2011-04-28 21:35:49 +00:00
|
|
|
if (counter)
|
2011-04-26 03:54:16 +00:00
|
|
|
++*counter;
|
|
|
|
#ifdef DEBUG_GCDAPROFILING
|
|
|
|
else
|
|
|
|
printf("llvmgcda: increment_indirect_counter counters=%x, pred=%u\n",
|
|
|
|
state_table_row, *predecessor);
|
2011-04-16 01:20:23 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-05-05 02:46:38 +00:00
|
|
|
void llvm_gcda_emit_function(uint32_t ident, const char *function_name) {
|
2011-04-16 01:20:23 +00:00
|
|
|
#ifdef DEBUG_GCDAPROFILING
|
2011-04-26 03:54:16 +00:00
|
|
|
printf("llvmgcda: function id=%x\n", ident);
|
2011-04-16 01:20:23 +00:00
|
|
|
#endif
|
2012-05-25 00:55:38 +00:00
|
|
|
if (!output_file) return;
|
2011-04-16 01:20:23 +00:00
|
|
|
|
|
|
|
/* function tag */
|
|
|
|
fwrite("\0\0\0\1", 4, 1, output_file);
|
2011-05-05 02:46:38 +00:00
|
|
|
write_int32(3 + 1 + length_of_string(function_name));
|
2011-04-16 01:20:23 +00:00
|
|
|
write_int32(ident);
|
|
|
|
write_int32(0);
|
2011-05-05 02:46:38 +00:00
|
|
|
write_int32(0);
|
|
|
|
write_string(function_name);
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
|
|
|
|
uint32_t i;
|
2012-05-25 00:55:38 +00:00
|
|
|
|
|
|
|
/* Counter #1 (arcs) tag */
|
|
|
|
if (!output_file) return;
|
2011-04-16 01:20:23 +00:00
|
|
|
fwrite("\0\0\xa1\1", 4, 1, output_file);
|
|
|
|
write_int32(num_counters * 2);
|
2012-05-25 21:57:59 +00:00
|
|
|
for (i = 0; i < num_counters; ++i)
|
2011-04-16 01:20:23 +00:00
|
|
|
write_int64(counters[i]);
|
|
|
|
|
|
|
|
#ifdef DEBUG_GCDAPROFILING
|
2011-04-26 03:54:16 +00:00
|
|
|
printf("llvmgcda: %u arcs\n", num_counters);
|
2012-05-25 21:57:59 +00:00
|
|
|
for (i = 0; i < num_counters; ++i)
|
2011-04-26 03:54:16 +00:00
|
|
|
printf("llvmgcda: %llu\n", (unsigned long long)counters[i]);
|
2011-04-16 01:20:23 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void llvm_gcda_end_file() {
|
|
|
|
/* Write out EOF record. */
|
2012-05-25 00:55:38 +00:00
|
|
|
if (!output_file) return;
|
2011-04-16 01:20:23 +00:00
|
|
|
fwrite("\0\0\0\0\0\0\0\0", 8, 1, output_file);
|
|
|
|
fclose(output_file);
|
|
|
|
output_file = NULL;
|
|
|
|
|
|
|
|
#ifdef DEBUG_GCDAPROFILING
|
2011-04-26 03:54:16 +00:00
|
|
|
printf("llvmgcda: -----\n");
|
2011-04-16 01:20:23 +00:00
|
|
|
#endif
|
|
|
|
}
|