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 "InstrProfilingInternal.h"
24 #include "InstrProfilingPort.h"
25 #include "InstrProfilingUtil.h"
34 #include "WindowsMMap.h"
40 #if defined(__FreeBSD__) && defined(__i386__)
41 #define I386_FREEBSD 1
43 #define I386_FREEBSD 0
46 #if !defined(_MSC_VER) && !I386_FREEBSD
51 typedef unsigned char uint8_t;
52 typedef unsigned int uint32_t;
53 typedef unsigned long long uint64_t;
55 /* System headers define 'size_t' incorrectly on x64 FreeBSD (prior to
56 * FreeBSD 10, r232261) when compiled in 32-bit mode.
58 typedef unsigned char uint8_t;
59 typedef unsigned int uint32_t;
60 typedef unsigned long long uint64_t;
63 /* #define DEBUG_GCDAPROFILING */
66 * --- GCOV file format I/O primitives ---
70 * The current file name we're outputting. Used primarily for error logging.
72 static char *filename = NULL;
75 * The current file we're outputting.
77 static FILE *output_file = NULL;
80 * Buffer that we write things into.
82 #define WRITE_BUFFER_SIZE (128 * 1024)
83 static char *write_buffer = NULL;
84 static uint64_t cur_buffer_size = 0;
85 static uint64_t cur_pos = 0;
86 static uint64_t file_size = 0;
87 static int new_file = 0;
91 * A list of functions to write out the data.
93 typedef void (*writeout_fn)();
95 struct writeout_fn_node {
97 struct writeout_fn_node *next;
100 static struct writeout_fn_node *writeout_fn_head = NULL;
101 static struct writeout_fn_node *writeout_fn_tail = NULL;
104 * A list of flush functions that our __gcov_flush() function should call.
106 typedef void (*flush_fn)();
108 struct flush_fn_node {
110 struct flush_fn_node *next;
113 static struct flush_fn_node *flush_fn_head = NULL;
114 static struct flush_fn_node *flush_fn_tail = NULL;
116 static void resize_write_buffer(uint64_t size) {
117 if (!new_file) return;
119 if (size <= cur_buffer_size) return;
120 size = (size - 1) / WRITE_BUFFER_SIZE + 1;
121 size *= WRITE_BUFFER_SIZE;
122 write_buffer = realloc(write_buffer, size);
123 cur_buffer_size = size;
126 static void write_bytes(const char *s, size_t len) {
127 resize_write_buffer(len);
128 memcpy(&write_buffer[cur_pos], s, len);
132 static void write_32bit_value(uint32_t i) {
133 write_bytes((char*)&i, 4);
136 static void write_64bit_value(uint64_t i) {
137 write_bytes((char*)&i, 8);
140 static uint32_t length_of_string(const char *s) {
141 return (strlen(s) / 4) + 1;
144 static void write_string(const char *s) {
145 uint32_t len = length_of_string(s);
146 write_32bit_value(len);
147 write_bytes(s, strlen(s));
148 write_bytes("\0\0\0\0", 4 - (strlen(s) % 4));
151 static uint32_t read_32bit_value() {
157 val = *(uint32_t*)&write_buffer[cur_pos];
162 static uint64_t read_64bit_value() {
168 val = *(uint64_t*)&write_buffer[cur_pos];
173 static char *mangle_filename(const char *orig_filename) {
177 const char *prefix = lprofGetPathPrefix(&prefix_strip, &prefix_len);
180 return strdup(orig_filename);
182 new_filename = malloc(prefix_len + 1 + strlen(orig_filename) + 1);
183 lprofApplyPathPrefix(new_filename, orig_filename, prefix, prefix_len,
189 static int map_file() {
190 fseek(output_file, 0L, SEEK_END);
191 file_size = ftell(output_file);
193 /* A size of 0 is invalid to `mmap'. Return a fail here, but don't issue an
194 * error message because it should "just work" for the user. */
198 write_buffer = mmap(0, file_size, PROT_READ | PROT_WRITE,
199 MAP_FILE | MAP_SHARED, fd, 0);
200 if (write_buffer == (void *)-1) {
202 fprintf(stderr, "profiling: %s: cannot map: %s\n", filename,
209 static void unmap_file() {
210 if (msync(write_buffer, file_size, MS_SYNC) == -1) {
212 fprintf(stderr, "profiling: %s: cannot msync: %s\n", filename,
216 /* We explicitly ignore errors from unmapping because at this point the data
217 * is written and we don't care.
219 (void)munmap(write_buffer, file_size);
225 * --- LLVM line counter API ---
228 /* A file in this case is a translation unit. Each .o file built with line
229 * profiling enabled will emit to a different file. Only one file may be
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);
242 /* Try opening the file, creating it if necessary. */
245 fd = open(filename, O_RDWR | O_CREAT, 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, 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 void llvm_gcda_increment_indirect_counter(uint32_t *predecessor,
300 uint64_t **counters) {
305 if (pred == 0xffffffff)
307 counter = counters[pred];
309 /* Don't crash if the pred# is out of sync. This can happen due to threads,
310 or because of a TODO in GCOVProfiling.cpp buildEdgeLookupTable(). */
313 #ifdef DEBUG_GCDAPROFILING
316 "llvmgcda: increment_indirect_counter counters=%08llx, pred=%u\n",
317 *counter, *predecessor);
321 void llvm_gcda_emit_function(uint32_t ident, const char *function_name,
322 uint32_t func_checksum, uint8_t use_extra_checksum,
323 uint32_t cfg_checksum) {
326 if (use_extra_checksum)
328 #ifdef DEBUG_GCDAPROFILING
329 fprintf(stderr, "llvmgcda: function id=0x%08x name=%s\n", ident,
330 function_name ? function_name : "NULL");
332 if (!output_file) return;
335 write_bytes("\0\0\0\1", 4);
337 len += 1 + length_of_string(function_name);
338 write_32bit_value(len);
339 write_32bit_value(ident);
340 write_32bit_value(func_checksum);
341 if (use_extra_checksum)
342 write_32bit_value(cfg_checksum);
344 write_string(function_name);
347 void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
349 uint64_t *old_ctrs = NULL;
351 uint64_t save_cur_pos = cur_pos;
353 if (!output_file) return;
355 val = read_32bit_value();
357 if (val != (uint32_t)-1) {
358 /* There are counters present in the file. Merge them. */
359 if (val != 0x01a10000) {
360 fprintf(stderr, "profiling: %s: cannot merge previous GCDA file: "
361 "corrupt arc tag (0x%08x)\n",
366 val = read_32bit_value();
367 if (val == (uint32_t)-1 || val / 2 != num_counters) {
368 fprintf(stderr, "profiling: %s: cannot merge previous GCDA file: "
369 "mismatched number of counters (%d)\n",
374 old_ctrs = malloc(sizeof(uint64_t) * num_counters);
375 for (i = 0; i < num_counters; ++i)
376 old_ctrs[i] = read_64bit_value();
379 cur_pos = save_cur_pos;
381 /* Counter #1 (arcs) tag */
382 write_bytes("\0\0\xa1\1", 4);
383 write_32bit_value(num_counters * 2);
384 for (i = 0; i < num_counters; ++i) {
385 counters[i] += (old_ctrs ? old_ctrs[i] : 0);
386 write_64bit_value(counters[i]);
391 #ifdef DEBUG_GCDAPROFILING
392 fprintf(stderr, "llvmgcda: %u arcs\n", num_counters);
393 for (i = 0; i < num_counters; ++i)
394 fprintf(stderr, "llvmgcda: %llu\n", (unsigned long long)counters[i]);
398 void llvm_gcda_summary_info() {
399 const uint32_t obj_summary_len = 9; /* Length for gcov compatibility. */
403 uint64_t save_cur_pos = cur_pos;
405 if (!output_file) return;
407 val = read_32bit_value();
409 if (val != (uint32_t)-1) {
410 /* There are counters present in the file. Merge them. */
411 if (val != 0xa1000000) {
412 fprintf(stderr, "profiling: %s: cannot merge previous run count: "
413 "corrupt object tag (0x%08x)\n",
418 val = read_32bit_value(); /* length */
419 if (val != obj_summary_len) {
420 fprintf(stderr, "profiling: %s: cannot merge previous run count: "
421 "mismatched object length (%d)\n",
426 read_32bit_value(); /* checksum, unused */
427 read_32bit_value(); /* num, unused */
428 runs += read_32bit_value(); /* Add previous run count to new counter. */
431 cur_pos = save_cur_pos;
433 /* Object summary tag */
434 write_bytes("\0\0\0\xa1", 4);
435 write_32bit_value(obj_summary_len);
436 write_32bit_value(0); /* checksum, unused */
437 write_32bit_value(0); /* num, unused */
438 write_32bit_value(runs);
439 for (i = 3; i < obj_summary_len; ++i)
440 write_32bit_value(0);
442 /* Program summary tag */
443 write_bytes("\0\0\0\xa3", 4); /* tag indicates 1 program */
444 write_32bit_value(0); /* 0 length */
446 #ifdef DEBUG_GCDAPROFILING
447 fprintf(stderr, "llvmgcda: %u runs\n", runs);
451 void llvm_gcda_end_file() {
452 /* Write out EOF record. */
454 write_bytes("\0\0\0\0\0\0\0\0", 8);
457 fwrite(write_buffer, cur_pos, 1, output_file);
470 #ifdef DEBUG_GCDAPROFILING
471 fprintf(stderr, "llvmgcda: -----\n");
475 void llvm_register_writeout_function(writeout_fn fn) {
476 struct writeout_fn_node *new_node = malloc(sizeof(struct writeout_fn_node));
478 new_node->next = NULL;
480 if (!writeout_fn_head) {
481 writeout_fn_head = writeout_fn_tail = new_node;
483 writeout_fn_tail->next = new_node;
484 writeout_fn_tail = new_node;
488 void llvm_writeout_files(void) {
489 struct writeout_fn_node *curr = writeout_fn_head;
497 void llvm_delete_writeout_function_list(void) {
498 while (writeout_fn_head) {
499 struct writeout_fn_node *node = writeout_fn_head;
500 writeout_fn_head = writeout_fn_head->next;
504 writeout_fn_head = writeout_fn_tail = NULL;
507 void llvm_register_flush_function(flush_fn fn) {
508 struct flush_fn_node *new_node = malloc(sizeof(struct flush_fn_node));
510 new_node->next = NULL;
512 if (!flush_fn_head) {
513 flush_fn_head = flush_fn_tail = new_node;
515 flush_fn_tail->next = new_node;
516 flush_fn_tail = new_node;
520 void __gcov_flush() {
521 struct flush_fn_node *curr = flush_fn_head;
529 void llvm_delete_flush_function_list(void) {
530 while (flush_fn_head) {
531 struct flush_fn_node *node = flush_fn_head;
532 flush_fn_head = flush_fn_head->next;
536 flush_fn_head = flush_fn_tail = NULL;
539 void llvm_gcov_init(writeout_fn wfn, flush_fn ffn) {
540 static int atexit_ran = 0;
543 llvm_register_writeout_function(wfn);
546 llvm_register_flush_function(ffn);
548 if (atexit_ran == 0) {
551 /* Make sure we write out the data and delete the data structures. */
552 atexit(llvm_delete_flush_function_list);
553 atexit(llvm_delete_writeout_function_list);
554 atexit(llvm_writeout_files);