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 #include "InstrProfilingPort.h"
24 #include "InstrProfilingUtil.h"
33 #include "WindowsMMap.h"
42 #if defined(__FreeBSD__) && defined(__i386__)
43 #define I386_FREEBSD 1
45 #define I386_FREEBSD 0
48 #if !defined(_MSC_VER) && !I386_FREEBSD
53 typedef unsigned char uint8_t;
54 typedef unsigned int uint32_t;
55 typedef unsigned long long uint64_t;
57 /* System headers define 'size_t' incorrectly on x64 FreeBSD (prior to
58 * FreeBSD 10, r232261) when compiled in 32-bit mode.
60 typedef unsigned char uint8_t;
61 typedef unsigned int uint32_t;
62 typedef unsigned long long uint64_t;
65 /* #define DEBUG_GCDAPROFILING */
68 * --- GCOV file format I/O primitives ---
72 * The current file name we're outputting. Used primarily for error logging.
74 static char *filename = NULL;
77 * The current file we're outputting.
79 static FILE *output_file = NULL;
82 * Buffer that we write things into.
84 #define WRITE_BUFFER_SIZE (128 * 1024)
85 static char *write_buffer = NULL;
86 static uint64_t cur_buffer_size = 0;
87 static uint64_t cur_pos = 0;
88 static uint64_t file_size = 0;
89 static int new_file = 0;
93 * A list of functions to write out the data.
95 typedef void (*writeout_fn)();
97 struct writeout_fn_node {
99 struct writeout_fn_node *next;
102 static struct writeout_fn_node *writeout_fn_head = NULL;
103 static struct writeout_fn_node *writeout_fn_tail = NULL;
106 * A list of flush functions that our __gcov_flush() function should call.
108 typedef void (*flush_fn)();
110 struct flush_fn_node {
112 struct flush_fn_node *next;
115 static struct flush_fn_node *flush_fn_head = NULL;
116 static struct flush_fn_node *flush_fn_tail = NULL;
118 static void resize_write_buffer(uint64_t size) {
119 if (!new_file) return;
121 if (size <= cur_buffer_size) return;
122 size = (size - 1) / WRITE_BUFFER_SIZE + 1;
123 size *= WRITE_BUFFER_SIZE;
124 write_buffer = realloc(write_buffer, size);
125 cur_buffer_size = size;
128 static void write_bytes(const char *s, size_t len) {
129 resize_write_buffer(len);
130 memcpy(&write_buffer[cur_pos], s, len);
134 static void write_32bit_value(uint32_t i) {
135 write_bytes((char*)&i, 4);
138 static void write_64bit_value(uint64_t i) {
139 write_bytes((char*)&i, 8);
142 static uint32_t length_of_string(const char *s) {
143 return (strlen(s) / 4) + 1;
146 static void write_string(const char *s) {
147 uint32_t len = length_of_string(s);
148 write_32bit_value(len);
149 write_bytes(s, strlen(s));
150 write_bytes("\0\0\0\0", 4 - (strlen(s) % 4));
153 static uint32_t read_32bit_value() {
159 val = *(uint32_t*)&write_buffer[cur_pos];
164 static uint64_t read_64bit_value() {
170 val = *(uint64_t*)&write_buffer[cur_pos];
175 static char *mangle_filename(const char *orig_filename) {
179 const char *prefix = lprofGetPathPrefix(&prefix_strip, &prefix_len);
182 return strdup(orig_filename);
184 new_filename = malloc(prefix_len + 1 + strlen(orig_filename) + 1);
185 lprofApplyPathPrefix(new_filename, orig_filename, prefix, prefix_len,
191 static int map_file() {
192 fseek(output_file, 0L, SEEK_END);
193 file_size = ftell(output_file);
195 /* A size of 0 is invalid to `mmap'. Return a fail here, but don't issue an
196 * error message because it should "just work" for the user. */
200 write_buffer = mmap(0, file_size, PROT_READ | PROT_WRITE,
201 MAP_FILE | MAP_SHARED, fd, 0);
202 if (write_buffer == (void *)-1) {
204 fprintf(stderr, "profiling: %s: cannot map: %s\n", filename,
211 static void unmap_file() {
212 if (msync(write_buffer, file_size, MS_SYNC) == -1) {
214 fprintf(stderr, "profiling: %s: cannot msync: %s\n", filename,
218 /* We explicitly ignore errors from unmapping because at this point the data
219 * is written and we don't care.
221 (void)munmap(write_buffer, file_size);
227 * --- LLVM line counter API ---
230 /* A file in this case is a translation unit. Each .o file built with line
231 * profiling enabled will emit to a different file. Only one file may be
234 void llvm_gcda_start_file(const char *orig_filename, const char version[4],
236 const char *mode = "r+b";
237 filename = mangle_filename(orig_filename);
239 /* Try just opening the file. */
241 fd = open(filename, O_RDWR);
244 /* Try opening the file, creating it if necessary. */
247 fd = open(filename, O_RDWR | O_CREAT, 0644);
249 /* Try creating the directories first then opening the file. */
250 __llvm_profile_recursive_mkdir(filename);
251 fd = open(filename, O_RDWR | O_CREAT, 0644);
253 /* Bah! It's hopeless. */
255 fprintf(stderr, "profiling: %s: cannot open: %s\n", filename,
262 /* Try to flock the file to serialize concurrent processes writing out to the
263 * same GCDA. This can fail if the filesystem doesn't support it, but in that
264 * case we'll just carry on with the old racy behaviour and hope for the best.
267 output_file = fdopen(fd, mode);
269 /* Initialize the write buffer. */
275 resize_write_buffer(WRITE_BUFFER_SIZE);
276 memset(write_buffer, 0, WRITE_BUFFER_SIZE);
278 if (map_file() == -1) {
279 /* mmap failed, try to recover by clobbering */
283 resize_write_buffer(WRITE_BUFFER_SIZE);
284 memset(write_buffer, 0, WRITE_BUFFER_SIZE);
288 /* gcda file, version, stamp checksum. */
289 write_bytes("adcg", 4);
290 write_bytes(version, 4);
291 write_32bit_value(checksum);
293 #ifdef DEBUG_GCDAPROFILING
294 fprintf(stderr, "llvmgcda: [%s]\n", orig_filename);
298 /* Given an array of pointers to counters (counters), increment the n-th one,
299 * where we're also given a pointer to n (predecessor).
301 void llvm_gcda_increment_indirect_counter(uint32_t *predecessor,
302 uint64_t **counters) {
307 if (pred == 0xffffffff)
309 counter = counters[pred];
311 /* Don't crash if the pred# is out of sync. This can happen due to threads,
312 or because of a TODO in GCOVProfiling.cpp buildEdgeLookupTable(). */
315 #ifdef DEBUG_GCDAPROFILING
318 "llvmgcda: increment_indirect_counter counters=%08llx, pred=%u\n",
319 *counter, *predecessor);
323 void llvm_gcda_emit_function(uint32_t ident, const char *function_name,
324 uint32_t func_checksum, uint8_t use_extra_checksum,
325 uint32_t cfg_checksum) {
328 if (use_extra_checksum)
330 #ifdef DEBUG_GCDAPROFILING
331 fprintf(stderr, "llvmgcda: function id=0x%08x name=%s\n", ident,
332 function_name ? function_name : "NULL");
334 if (!output_file) return;
337 write_bytes("\0\0\0\1", 4);
339 len += 1 + length_of_string(function_name);
340 write_32bit_value(len);
341 write_32bit_value(ident);
342 write_32bit_value(func_checksum);
343 if (use_extra_checksum)
344 write_32bit_value(cfg_checksum);
346 write_string(function_name);
349 void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
351 uint64_t *old_ctrs = NULL;
353 uint64_t save_cur_pos = cur_pos;
355 if (!output_file) return;
357 val = read_32bit_value();
359 if (val != (uint32_t)-1) {
360 /* There are counters present in the file. Merge them. */
361 if (val != 0x01a10000) {
362 fprintf(stderr, "profiling: %s: cannot merge previous GCDA file: "
363 "corrupt arc tag (0x%08x)\n",
368 val = read_32bit_value();
369 if (val == (uint32_t)-1 || val / 2 != num_counters) {
370 fprintf(stderr, "profiling: %s: cannot merge previous GCDA file: "
371 "mismatched number of counters (%d)\n",
376 old_ctrs = malloc(sizeof(uint64_t) * num_counters);
377 for (i = 0; i < num_counters; ++i)
378 old_ctrs[i] = read_64bit_value();
381 cur_pos = save_cur_pos;
383 /* Counter #1 (arcs) tag */
384 write_bytes("\0\0\xa1\1", 4);
385 write_32bit_value(num_counters * 2);
386 for (i = 0; i < num_counters; ++i) {
387 counters[i] += (old_ctrs ? old_ctrs[i] : 0);
388 write_64bit_value(counters[i]);
393 #ifdef DEBUG_GCDAPROFILING
394 fprintf(stderr, "llvmgcda: %u arcs\n", num_counters);
395 for (i = 0; i < num_counters; ++i)
396 fprintf(stderr, "llvmgcda: %llu\n", (unsigned long long)counters[i]);
400 void llvm_gcda_summary_info() {
401 const uint32_t obj_summary_len = 9; /* Length for gcov compatibility. */
405 uint64_t save_cur_pos = cur_pos;
407 if (!output_file) return;
409 val = read_32bit_value();
411 if (val != (uint32_t)-1) {
412 /* There are counters present in the file. Merge them. */
413 if (val != 0xa1000000) {
414 fprintf(stderr, "profiling: %s: cannot merge previous run count: "
415 "corrupt object tag (0x%08x)\n",
420 val = read_32bit_value(); /* length */
421 if (val != obj_summary_len) {
422 fprintf(stderr, "profiling: %s: cannot merge previous run count: "
423 "mismatched object length (%d)\n",
428 read_32bit_value(); /* checksum, unused */
429 read_32bit_value(); /* num, unused */
430 runs += read_32bit_value(); /* Add previous run count to new counter. */
433 cur_pos = save_cur_pos;
435 /* Object summary tag */
436 write_bytes("\0\0\0\xa1", 4);
437 write_32bit_value(obj_summary_len);
438 write_32bit_value(0); /* checksum, unused */
439 write_32bit_value(0); /* num, unused */
440 write_32bit_value(runs);
441 for (i = 3; i < obj_summary_len; ++i)
442 write_32bit_value(0);
444 /* Program summary tag */
445 write_bytes("\0\0\0\xa3", 4); /* tag indicates 1 program */
446 write_32bit_value(0); /* 0 length */
448 #ifdef DEBUG_GCDAPROFILING
449 fprintf(stderr, "llvmgcda: %u runs\n", runs);
453 void llvm_gcda_end_file() {
454 /* Write out EOF record. */
456 write_bytes("\0\0\0\0\0\0\0\0", 8);
459 fwrite(write_buffer, cur_pos, 1, output_file);
472 #ifdef DEBUG_GCDAPROFILING
473 fprintf(stderr, "llvmgcda: -----\n");
477 void llvm_register_writeout_function(writeout_fn fn) {
478 struct writeout_fn_node *new_node = malloc(sizeof(struct writeout_fn_node));
480 new_node->next = NULL;
482 if (!writeout_fn_head) {
483 writeout_fn_head = writeout_fn_tail = new_node;
485 writeout_fn_tail->next = new_node;
486 writeout_fn_tail = new_node;
490 void llvm_writeout_files(void) {
491 struct writeout_fn_node *curr = writeout_fn_head;
499 void llvm_delete_writeout_function_list(void) {
500 while (writeout_fn_head) {
501 struct writeout_fn_node *node = writeout_fn_head;
502 writeout_fn_head = writeout_fn_head->next;
506 writeout_fn_head = writeout_fn_tail = NULL;
509 void llvm_register_flush_function(flush_fn fn) {
510 struct flush_fn_node *new_node = malloc(sizeof(struct flush_fn_node));
512 new_node->next = NULL;
514 if (!flush_fn_head) {
515 flush_fn_head = flush_fn_tail = new_node;
517 flush_fn_tail->next = new_node;
518 flush_fn_tail = new_node;
522 void __gcov_flush() {
523 struct flush_fn_node *curr = flush_fn_head;
531 void llvm_delete_flush_function_list(void) {
532 while (flush_fn_head) {
533 struct flush_fn_node *node = flush_fn_head;
534 flush_fn_head = flush_fn_head->next;
538 flush_fn_head = flush_fn_tail = NULL;
541 void llvm_gcov_init(writeout_fn wfn, flush_fn ffn) {
542 static int atexit_ran = 0;
545 llvm_register_writeout_function(wfn);
548 llvm_register_flush_function(ffn);
550 if (atexit_ran == 0) {
553 /* Make sure we write out the data and delete the data structures. */
554 atexit(llvm_delete_flush_function_list);
555 atexit(llvm_delete_writeout_function_list);
556 atexit(llvm_writeout_files);