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 "InstrProfilingUtil.h"
33 #define I386_FREEBSD (defined(__FreeBSD__) && defined(__i386__))
35 #if !defined(_MSC_VER) && !I386_FREEBSD
40 typedef unsigned int uint32_t;
41 typedef unsigned long long uint64_t;
43 /* System headers define 'size_t' incorrectly on x64 FreeBSD (prior to
44 * FreeBSD 10, r232261) when compiled in 32-bit mode.
46 typedef unsigned char uint8_t;
47 typedef unsigned int uint32_t;
48 typedef unsigned long long uint64_t;
51 /* #define DEBUG_GCDAPROFILING */
54 * --- GCOV file format I/O primitives ---
58 * The current file name we're outputting. Used primarily for error logging.
60 static char *filename = NULL;
63 * The current file we're outputting.
65 static FILE *output_file = NULL;
68 * Buffer that we write things into.
70 #define WRITE_BUFFER_SIZE (128 * 1024)
71 static char *write_buffer = NULL;
72 static uint64_t cur_buffer_size = 0;
73 static uint64_t cur_pos = 0;
74 static uint64_t file_size = 0;
75 static int new_file = 0;
79 * A list of functions to write out the data.
81 typedef void (*writeout_fn)();
83 struct writeout_fn_node {
85 struct writeout_fn_node *next;
88 static struct writeout_fn_node *writeout_fn_head = NULL;
89 static struct writeout_fn_node *writeout_fn_tail = NULL;
92 * A list of flush functions that our __gcov_flush() function should call.
94 typedef void (*flush_fn)();
96 struct flush_fn_node {
98 struct flush_fn_node *next;
101 static struct flush_fn_node *flush_fn_head = NULL;
102 static struct flush_fn_node *flush_fn_tail = NULL;
104 static void resize_write_buffer(uint64_t size) {
105 if (!new_file) return;
107 if (size <= cur_buffer_size) return;
108 size = (size - 1) / WRITE_BUFFER_SIZE + 1;
109 size *= WRITE_BUFFER_SIZE;
110 write_buffer = realloc(write_buffer, size);
111 cur_buffer_size = size;
114 static void write_bytes(const char *s, size_t len) {
115 resize_write_buffer(len);
116 memcpy(&write_buffer[cur_pos], s, len);
120 static void write_32bit_value(uint32_t i) {
121 write_bytes((char*)&i, 4);
124 static void write_64bit_value(uint64_t i) {
125 write_bytes((char*)&i, 8);
128 static uint32_t length_of_string(const char *s) {
129 return (strlen(s) / 4) + 1;
132 static void write_string(const char *s) {
133 uint32_t len = length_of_string(s);
134 write_32bit_value(len);
135 write_bytes(s, strlen(s));
136 write_bytes("\0\0\0\0", 4 - (strlen(s) % 4));
139 static uint32_t read_32bit_value() {
145 val = *(uint32_t*)&write_buffer[cur_pos];
150 static uint64_t read_64bit_value() {
156 val = *(uint64_t*)&write_buffer[cur_pos];
161 static char *mangle_filename(const char *orig_filename) {
163 size_t filename_len, prefix_len;
166 const char *fname, *ptr;
167 const char *prefix = getenv("GCOV_PREFIX");
168 const char *prefix_strip_str = getenv("GCOV_PREFIX_STRIP");
170 if (prefix == NULL || prefix[0] == '\0')
171 return strdup(orig_filename);
173 if (prefix_strip_str) {
174 prefix_strip = atoi(prefix_strip_str);
176 /* Negative GCOV_PREFIX_STRIP values are ignored */
177 if (prefix_strip < 0)
183 fname = orig_filename;
184 for (level = 0, ptr = fname + 1; level < prefix_strip; ++ptr) {
193 filename_len = strlen(fname);
194 prefix_len = strlen(prefix);
195 new_filename = malloc(prefix_len + 1 + filename_len + 1);
196 memcpy(new_filename, prefix, prefix_len);
198 if (prefix[prefix_len - 1] != '/')
199 new_filename[prefix_len++] = '/';
200 memcpy(new_filename + prefix_len, fname, filename_len + 1);
205 static int map_file() {
206 fseek(output_file, 0L, SEEK_END);
207 file_size = ftell(output_file);
209 /* A size of 0 is invalid to `mmap'. Return a fail here, but don't issue an
210 * error message because it should "just work" for the user. */
214 write_buffer = mmap(0, file_size, PROT_READ | PROT_WRITE,
215 MAP_FILE | MAP_SHARED, fd, 0);
216 if (write_buffer == (void *)-1) {
218 fprintf(stderr, "profiling: %s: cannot map: %s\n", filename,
225 static void unmap_file() {
226 if (msync(write_buffer, file_size, MS_SYNC) == -1) {
228 fprintf(stderr, "profiling: %s: cannot msync: %s\n", filename,
232 /* We explicitly ignore errors from unmapping because at this point the data
233 * is written and we don't care.
235 (void)munmap(write_buffer, file_size);
241 * --- LLVM line counter API ---
244 /* A file in this case is a translation unit. Each .o file built with line
245 * profiling enabled will emit to a different file. Only one file may be
248 void llvm_gcda_start_file(const char *orig_filename, const char version[4],
250 const char *mode = "r+b";
251 filename = mangle_filename(orig_filename);
253 /* Try just opening the file. */
255 fd = open(filename, O_RDWR);
258 /* Try opening the file, creating it if necessary. */
261 fd = open(filename, O_RDWR | O_CREAT, 0644);
263 /* Try creating the directories first then opening the file. */
264 __llvm_profile_recursive_mkdir(filename);
265 fd = open(filename, O_RDWR | O_CREAT, 0644);
267 /* Bah! It's hopeless. */
269 fprintf(stderr, "profiling: %s: cannot open: %s\n", filename,
276 /* Try to flock the file to serialize concurrent processes writing out to the
277 * same GCDA. This can fail if the filesystem doesn't support it, but in that
278 * case we'll just carry on with the old racy behaviour and hope for the best.
281 output_file = fdopen(fd, mode);
283 /* Initialize the write buffer. */
289 resize_write_buffer(WRITE_BUFFER_SIZE);
290 memset(write_buffer, 0, WRITE_BUFFER_SIZE);
292 if (map_file() == -1) {
293 /* mmap failed, try to recover by clobbering */
297 resize_write_buffer(WRITE_BUFFER_SIZE);
298 memset(write_buffer, 0, WRITE_BUFFER_SIZE);
302 /* gcda file, version, stamp checksum. */
303 write_bytes("adcg", 4);
304 write_bytes(version, 4);
305 write_32bit_value(checksum);
307 #ifdef DEBUG_GCDAPROFILING
308 fprintf(stderr, "llvmgcda: [%s]\n", orig_filename);
312 /* Given an array of pointers to counters (counters), increment the n-th one,
313 * where we're also given a pointer to n (predecessor).
315 void llvm_gcda_increment_indirect_counter(uint32_t *predecessor,
316 uint64_t **counters) {
321 if (pred == 0xffffffff)
323 counter = counters[pred];
325 /* Don't crash if the pred# is out of sync. This can happen due to threads,
326 or because of a TODO in GCOVProfiling.cpp buildEdgeLookupTable(). */
329 #ifdef DEBUG_GCDAPROFILING
332 "llvmgcda: increment_indirect_counter counters=%08llx, pred=%u\n",
333 *counter, *predecessor);
337 void llvm_gcda_emit_function(uint32_t ident, const char *function_name,
338 uint32_t func_checksum, uint8_t use_extra_checksum,
339 uint32_t cfg_checksum) {
342 if (use_extra_checksum)
344 #ifdef DEBUG_GCDAPROFILING
345 fprintf(stderr, "llvmgcda: function id=0x%08x name=%s\n", ident,
346 function_name ? function_name : "NULL");
348 if (!output_file) return;
351 write_bytes("\0\0\0\1", 4);
353 len += 1 + length_of_string(function_name);
354 write_32bit_value(len);
355 write_32bit_value(ident);
356 write_32bit_value(func_checksum);
357 if (use_extra_checksum)
358 write_32bit_value(cfg_checksum);
360 write_string(function_name);
363 void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
365 uint64_t *old_ctrs = NULL;
367 uint64_t save_cur_pos = cur_pos;
369 if (!output_file) return;
371 val = read_32bit_value();
373 if (val != (uint32_t)-1) {
374 /* There are counters present in the file. Merge them. */
375 if (val != 0x01a10000) {
376 fprintf(stderr, "profiling: %s: cannot merge previous GCDA file: "
377 "corrupt arc tag (0x%08x)\n",
382 val = read_32bit_value();
383 if (val == (uint32_t)-1 || val / 2 != num_counters) {
384 fprintf(stderr, "profiling: %s: cannot merge previous GCDA file: "
385 "mismatched number of counters (%d)\n",
390 old_ctrs = malloc(sizeof(uint64_t) * num_counters);
391 for (i = 0; i < num_counters; ++i)
392 old_ctrs[i] = read_64bit_value();
395 cur_pos = save_cur_pos;
397 /* Counter #1 (arcs) tag */
398 write_bytes("\0\0\xa1\1", 4);
399 write_32bit_value(num_counters * 2);
400 for (i = 0; i < num_counters; ++i) {
401 counters[i] += (old_ctrs ? old_ctrs[i] : 0);
402 write_64bit_value(counters[i]);
407 #ifdef DEBUG_GCDAPROFILING
408 fprintf(stderr, "llvmgcda: %u arcs\n", num_counters);
409 for (i = 0; i < num_counters; ++i)
410 fprintf(stderr, "llvmgcda: %llu\n", (unsigned long long)counters[i]);
414 void llvm_gcda_summary_info() {
415 const uint32_t obj_summary_len = 9; /* Length for gcov compatibility. */
419 uint64_t save_cur_pos = cur_pos;
421 if (!output_file) return;
423 val = read_32bit_value();
425 if (val != (uint32_t)-1) {
426 /* There are counters present in the file. Merge them. */
427 if (val != 0xa1000000) {
428 fprintf(stderr, "profiling: %s: cannot merge previous run count: "
429 "corrupt object tag (0x%08x)\n",
434 val = read_32bit_value(); /* length */
435 if (val != obj_summary_len) {
436 fprintf(stderr, "profiling: %s: cannot merge previous run count: "
437 "mismatched object length (%d)\n",
442 read_32bit_value(); /* checksum, unused */
443 read_32bit_value(); /* num, unused */
444 runs += read_32bit_value(); /* Add previous run count to new counter. */
447 cur_pos = save_cur_pos;
449 /* Object summary tag */
450 write_bytes("\0\0\0\xa1", 4);
451 write_32bit_value(obj_summary_len);
452 write_32bit_value(0); /* checksum, unused */
453 write_32bit_value(0); /* num, unused */
454 write_32bit_value(runs);
455 for (i = 3; i < obj_summary_len; ++i)
456 write_32bit_value(0);
458 /* Program summary tag */
459 write_bytes("\0\0\0\xa3", 4); /* tag indicates 1 program */
460 write_32bit_value(0); /* 0 length */
462 #ifdef DEBUG_GCDAPROFILING
463 fprintf(stderr, "llvmgcda: %u runs\n", runs);
467 void llvm_gcda_end_file() {
468 /* Write out EOF record. */
470 write_bytes("\0\0\0\0\0\0\0\0", 8);
473 fwrite(write_buffer, cur_pos, 1, output_file);
486 #ifdef DEBUG_GCDAPROFILING
487 fprintf(stderr, "llvmgcda: -----\n");
491 void llvm_register_writeout_function(writeout_fn fn) {
492 struct writeout_fn_node *new_node = malloc(sizeof(struct writeout_fn_node));
494 new_node->next = NULL;
496 if (!writeout_fn_head) {
497 writeout_fn_head = writeout_fn_tail = new_node;
499 writeout_fn_tail->next = new_node;
500 writeout_fn_tail = new_node;
504 void llvm_writeout_files() {
505 struct writeout_fn_node *curr = writeout_fn_head;
513 void llvm_delete_writeout_function_list() {
514 while (writeout_fn_head) {
515 struct writeout_fn_node *node = writeout_fn_head;
516 writeout_fn_head = writeout_fn_head->next;
520 writeout_fn_head = writeout_fn_tail = NULL;
523 void llvm_register_flush_function(flush_fn fn) {
524 struct flush_fn_node *new_node = malloc(sizeof(struct flush_fn_node));
526 new_node->next = NULL;
528 if (!flush_fn_head) {
529 flush_fn_head = flush_fn_tail = new_node;
531 flush_fn_tail->next = new_node;
532 flush_fn_tail = new_node;
536 void __gcov_flush() {
537 struct flush_fn_node *curr = flush_fn_head;
545 void llvm_delete_flush_function_list() {
546 while (flush_fn_head) {
547 struct flush_fn_node *node = flush_fn_head;
548 flush_fn_head = flush_fn_head->next;
552 flush_fn_head = flush_fn_tail = NULL;
555 void llvm_gcov_init(writeout_fn wfn, flush_fn ffn) {
556 static int atexit_ran = 0;
559 llvm_register_writeout_function(wfn);
562 llvm_register_flush_function(ffn);
564 if (atexit_ran == 0) {
567 /* Make sure we write out the data and delete the data structures. */
568 atexit(llvm_delete_flush_function_list);
569 atexit(llvm_delete_writeout_function_list);
570 atexit(llvm_writeout_files);