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 \*===----------------------------------------------------------------------===*/
30 #include "WindowsMMap.h"
36 #if defined(__FreeBSD__) && defined(__i386__)
37 #define I386_FREEBSD 1
39 #define I386_FREEBSD 0
42 #if !defined(_MSC_VER) && !I386_FREEBSD
47 typedef unsigned char uint8_t;
48 typedef unsigned int uint32_t;
49 typedef unsigned long long uint64_t;
51 /* System headers define 'size_t' incorrectly on x64 FreeBSD (prior to
52 * FreeBSD 10, r232261) when compiled in 32-bit mode.
54 typedef unsigned char uint8_t;
55 typedef unsigned int uint32_t;
56 typedef unsigned long long uint64_t;
59 #include "InstrProfiling.h"
60 #include "InstrProfilingUtil.h"
62 /* #define DEBUG_GCDAPROFILING */
65 * --- GCOV file format I/O primitives ---
69 * The current file name we're outputting. Used primarily for error logging.
71 static char *filename = NULL;
74 * The current file we're outputting.
76 static FILE *output_file = NULL;
79 * Buffer that we write things into.
81 #define WRITE_BUFFER_SIZE (128 * 1024)
82 static char *write_buffer = NULL;
83 static uint64_t cur_buffer_size = 0;
84 static uint64_t cur_pos = 0;
85 static uint64_t file_size = 0;
86 static int new_file = 0;
90 * A list of functions to write out the data.
92 typedef void (*writeout_fn)();
94 struct writeout_fn_node {
96 struct writeout_fn_node *next;
99 static struct writeout_fn_node *writeout_fn_head = NULL;
100 static struct writeout_fn_node *writeout_fn_tail = NULL;
103 * A list of flush functions that our __gcov_flush() function should call.
105 typedef void (*flush_fn)();
107 struct flush_fn_node {
109 struct flush_fn_node *next;
112 static struct flush_fn_node *flush_fn_head = NULL;
113 static struct flush_fn_node *flush_fn_tail = NULL;
115 static void resize_write_buffer(uint64_t size) {
116 if (!new_file) return;
118 if (size <= cur_buffer_size) return;
119 size = (size - 1) / WRITE_BUFFER_SIZE + 1;
120 size *= WRITE_BUFFER_SIZE;
121 write_buffer = realloc(write_buffer, size);
122 cur_buffer_size = size;
125 static void write_bytes(const char *s, size_t len) {
126 resize_write_buffer(len);
127 memcpy(&write_buffer[cur_pos], s, len);
131 static void write_32bit_value(uint32_t i) {
132 write_bytes((char*)&i, 4);
135 static void write_64bit_value(uint64_t i) {
136 write_bytes((char*)&i, 8);
139 static uint32_t length_of_string(const char *s) {
140 return (strlen(s) / 4) + 1;
143 static void write_string(const char *s) {
144 uint32_t len = length_of_string(s);
145 write_32bit_value(len);
146 write_bytes(s, strlen(s));
147 write_bytes("\0\0\0\0", 4 - (strlen(s) % 4));
150 static uint32_t read_32bit_value() {
156 val = *(uint32_t*)&write_buffer[cur_pos];
161 static uint64_t read_64bit_value() {
167 val = *(uint64_t*)&write_buffer[cur_pos];
172 static char *mangle_filename(const char *orig_filename) {
176 const char *prefix = lprofGetPathPrefix(&prefix_strip, &prefix_len);
179 return strdup(orig_filename);
181 new_filename = malloc(prefix_len + 1 + strlen(orig_filename) + 1);
182 lprofApplyPathPrefix(new_filename, orig_filename, prefix, prefix_len,
188 static int map_file() {
189 fseek(output_file, 0L, SEEK_END);
190 file_size = ftell(output_file);
192 /* A size of 0 is invalid to `mmap'. Return a fail here, but don't issue an
193 * error message because it should "just work" for the user. */
197 write_buffer = mmap(0, file_size, PROT_READ | PROT_WRITE,
198 MAP_FILE | MAP_SHARED, fd, 0);
199 if (write_buffer == (void *)-1) {
201 fprintf(stderr, "profiling: %s: cannot map: %s\n", filename,
208 static void unmap_file() {
209 if (msync(write_buffer, file_size, MS_SYNC) == -1) {
211 fprintf(stderr, "profiling: %s: cannot msync: %s\n", filename,
215 /* We explicitly ignore errors from unmapping because at this point the data
216 * is written and we don't care.
218 (void)munmap(write_buffer, file_size);
224 * --- LLVM line counter API ---
227 /* A file in this case is a translation unit. Each .o file built with line
228 * profiling enabled will emit to a different file. Only one file may be
231 COMPILER_RT_VISIBILITY
232 void llvm_gcda_start_file(const char *orig_filename, const char version[4],
234 const char *mode = "r+b";
235 filename = mangle_filename(orig_filename);
237 /* Try just opening the file. */
239 fd = open(filename, O_RDWR | O_BINARY);
242 /* Try opening the file, creating it if necessary. */
245 fd = open(filename, O_RDWR | O_CREAT | O_BINARY, 0644);
247 /* Try creating the directories first then opening the file. */
248 __llvm_profile_recursive_mkdir(filename);
249 fd = open(filename, O_RDWR | O_CREAT | O_BINARY, 0644);
251 /* Bah! It's hopeless. */
253 fprintf(stderr, "profiling: %s: cannot open: %s\n", filename,
260 /* Try to flock the file to serialize concurrent processes writing out to the
261 * same GCDA. This can fail if the filesystem doesn't support it, but in that
262 * case we'll just carry on with the old racy behaviour and hope for the best.
265 output_file = fdopen(fd, mode);
267 /* Initialize the write buffer. */
273 resize_write_buffer(WRITE_BUFFER_SIZE);
274 memset(write_buffer, 0, WRITE_BUFFER_SIZE);
276 if (map_file() == -1) {
277 /* mmap failed, try to recover by clobbering */
281 resize_write_buffer(WRITE_BUFFER_SIZE);
282 memset(write_buffer, 0, WRITE_BUFFER_SIZE);
286 /* gcda file, version, stamp checksum. */
287 write_bytes("adcg", 4);
288 write_bytes(version, 4);
289 write_32bit_value(checksum);
291 #ifdef DEBUG_GCDAPROFILING
292 fprintf(stderr, "llvmgcda: [%s]\n", orig_filename);
296 /* Given an array of pointers to counters (counters), increment the n-th one,
297 * where we're also given a pointer to n (predecessor).
299 COMPILER_RT_VISIBILITY
300 void llvm_gcda_increment_indirect_counter(uint32_t *predecessor,
301 uint64_t **counters) {
306 if (pred == 0xffffffff)
308 counter = counters[pred];
310 /* Don't crash if the pred# is out of sync. This can happen due to threads,
311 or because of a TODO in GCOVProfiling.cpp buildEdgeLookupTable(). */
314 #ifdef DEBUG_GCDAPROFILING
317 "llvmgcda: increment_indirect_counter counters=%08llx, pred=%u\n",
318 *counter, *predecessor);
322 COMPILER_RT_VISIBILITY
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 COMPILER_RT_VISIBILITY
350 void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
352 uint64_t *old_ctrs = NULL;
354 uint64_t save_cur_pos = cur_pos;
356 if (!output_file) return;
358 val = read_32bit_value();
360 if (val != (uint32_t)-1) {
361 /* There are counters present in the file. Merge them. */
362 if (val != 0x01a10000) {
363 fprintf(stderr, "profiling: %s: cannot merge previous GCDA file: "
364 "corrupt arc tag (0x%08x)\n",
369 val = read_32bit_value();
370 if (val == (uint32_t)-1 || val / 2 != num_counters) {
371 fprintf(stderr, "profiling: %s: cannot merge previous GCDA file: "
372 "mismatched number of counters (%d)\n",
377 old_ctrs = malloc(sizeof(uint64_t) * num_counters);
378 for (i = 0; i < num_counters; ++i)
379 old_ctrs[i] = read_64bit_value();
382 cur_pos = save_cur_pos;
384 /* Counter #1 (arcs) tag */
385 write_bytes("\0\0\xa1\1", 4);
386 write_32bit_value(num_counters * 2);
387 for (i = 0; i < num_counters; ++i) {
388 counters[i] += (old_ctrs ? old_ctrs[i] : 0);
389 write_64bit_value(counters[i]);
394 #ifdef DEBUG_GCDAPROFILING
395 fprintf(stderr, "llvmgcda: %u arcs\n", num_counters);
396 for (i = 0; i < num_counters; ++i)
397 fprintf(stderr, "llvmgcda: %llu\n", (unsigned long long)counters[i]);
401 COMPILER_RT_VISIBILITY
402 void llvm_gcda_summary_info() {
403 const uint32_t obj_summary_len = 9; /* Length for gcov compatibility. */
407 uint64_t save_cur_pos = cur_pos;
409 if (!output_file) return;
411 val = read_32bit_value();
413 if (val != (uint32_t)-1) {
414 /* There are counters present in the file. Merge them. */
415 if (val != 0xa1000000) {
416 fprintf(stderr, "profiling: %s: cannot merge previous run count: "
417 "corrupt object tag (0x%08x)\n",
422 val = read_32bit_value(); /* length */
423 if (val != obj_summary_len) {
424 fprintf(stderr, "profiling: %s: cannot merge previous run count: "
425 "mismatched object length (%d)\n",
430 read_32bit_value(); /* checksum, unused */
431 read_32bit_value(); /* num, unused */
432 runs += read_32bit_value(); /* Add previous run count to new counter. */
435 cur_pos = save_cur_pos;
437 /* Object summary tag */
438 write_bytes("\0\0\0\xa1", 4);
439 write_32bit_value(obj_summary_len);
440 write_32bit_value(0); /* checksum, unused */
441 write_32bit_value(0); /* num, unused */
442 write_32bit_value(runs);
443 for (i = 3; i < obj_summary_len; ++i)
444 write_32bit_value(0);
446 /* Program summary tag */
447 write_bytes("\0\0\0\xa3", 4); /* tag indicates 1 program */
448 write_32bit_value(0); /* 0 length */
450 #ifdef DEBUG_GCDAPROFILING
451 fprintf(stderr, "llvmgcda: %u runs\n", runs);
455 COMPILER_RT_VISIBILITY
456 void llvm_gcda_end_file() {
457 /* Write out EOF record. */
459 write_bytes("\0\0\0\0\0\0\0\0", 8);
462 fwrite(write_buffer, cur_pos, 1, output_file);
476 #ifdef DEBUG_GCDAPROFILING
477 fprintf(stderr, "llvmgcda: -----\n");
481 COMPILER_RT_VISIBILITY
482 void llvm_register_writeout_function(writeout_fn fn) {
483 struct writeout_fn_node *new_node = malloc(sizeof(struct writeout_fn_node));
485 new_node->next = NULL;
487 if (!writeout_fn_head) {
488 writeout_fn_head = writeout_fn_tail = new_node;
490 writeout_fn_tail->next = new_node;
491 writeout_fn_tail = new_node;
495 COMPILER_RT_VISIBILITY
496 void llvm_writeout_files(void) {
497 struct writeout_fn_node *curr = writeout_fn_head;
505 COMPILER_RT_VISIBILITY
506 void llvm_delete_writeout_function_list(void) {
507 while (writeout_fn_head) {
508 struct writeout_fn_node *node = writeout_fn_head;
509 writeout_fn_head = writeout_fn_head->next;
513 writeout_fn_head = writeout_fn_tail = NULL;
516 COMPILER_RT_VISIBILITY
517 void llvm_register_flush_function(flush_fn fn) {
518 struct flush_fn_node *new_node = malloc(sizeof(struct flush_fn_node));
520 new_node->next = NULL;
522 if (!flush_fn_head) {
523 flush_fn_head = flush_fn_tail = new_node;
525 flush_fn_tail->next = new_node;
526 flush_fn_tail = new_node;
530 COMPILER_RT_VISIBILITY
531 void __gcov_flush() {
532 struct flush_fn_node *curr = flush_fn_head;
540 COMPILER_RT_VISIBILITY
541 void llvm_delete_flush_function_list(void) {
542 while (flush_fn_head) {
543 struct flush_fn_node *node = flush_fn_head;
544 flush_fn_head = flush_fn_head->next;
548 flush_fn_head = flush_fn_tail = NULL;
551 COMPILER_RT_VISIBILITY
552 void llvm_gcov_init(writeout_fn wfn, flush_fn ffn) {
553 static int atexit_ran = 0;
556 llvm_register_writeout_function(wfn);
559 llvm_register_flush_function(ffn);
561 if (atexit_ran == 0) {
564 /* Make sure we write out the data and delete the data structures. */
565 atexit(llvm_delete_flush_function_list);
566 atexit(llvm_delete_writeout_function_list);
567 atexit(llvm_writeout_files);