xref: /netbsd-src/sys/external/bsd/compiler_rt/dist/lib/profile/GCDAProfiling.c (revision b3babee32b19400ea8087c80671b57d22e19670e)
1b9829059Sjoerg /*===- GCDAProfiling.c - Support library for GCDA file emission -----------===*\
2b9829059Sjoerg |*
3b9829059Sjoerg |*                     The LLVM Compiler Infrastructure
4b9829059Sjoerg |*
5b9829059Sjoerg |* This file is distributed under the University of Illinois Open Source
6b9829059Sjoerg |* License. See LICENSE.TXT for details.
7b9829059Sjoerg |*
8b9829059Sjoerg |*===----------------------------------------------------------------------===*|
9b9829059Sjoerg |*
10b9829059Sjoerg |* This file implements the call back routines for the gcov profiling
11b9829059Sjoerg |* instrumentation pass. Link against this library when running code through
12b9829059Sjoerg |* the -insert-gcov-profiling LLVM pass.
13b9829059Sjoerg |*
14b9829059Sjoerg |* We emit files in a corrupt version of GCOV's "gcda" file format. These files
15b9829059Sjoerg |* are only close enough that LCOV will happily parse them. Anything that lcov
16b9829059Sjoerg |* ignores is missing.
17b9829059Sjoerg |*
18b9829059Sjoerg |* TODO: gcov is multi-process safe by having each exit open the existing file
19b9829059Sjoerg |* and append to it. We'd like to achieve that and be thread-safe too.
20b9829059Sjoerg |*
21b9829059Sjoerg \*===----------------------------------------------------------------------===*/
22b9829059Sjoerg 
23*b3babee3Skamil #ifdef _LIBC
24*b3babee3Skamil #include "namespace.h"
25*b3babee3Skamil #endif
26*b3babee3Skamil 
27ef84fd3bSjoerg #include "InstrProfilingUtil.h"
28ef84fd3bSjoerg 
29b9829059Sjoerg #include <errno.h>
30b9829059Sjoerg #include <fcntl.h>
31b9829059Sjoerg #include <stdio.h>
32b9829059Sjoerg #include <stdlib.h>
33b9829059Sjoerg #include <string.h>
34ef84fd3bSjoerg 
35ef84fd3bSjoerg #if defined(_WIN32)
36ef84fd3bSjoerg #include "WindowsMMap.h"
37ef84fd3bSjoerg #else
38b9829059Sjoerg #include <sys/mman.h>
39ef84fd3bSjoerg #include <sys/file.h>
40b9829059Sjoerg #endif
41b9829059Sjoerg 
42ef84fd3bSjoerg #if defined(__FreeBSD__) && defined(__i386__)
43ef84fd3bSjoerg #define I386_FREEBSD 1
44ef84fd3bSjoerg #else
45ef84fd3bSjoerg #define I386_FREEBSD 0
4661f2f256Sjoerg #endif
4761f2f256Sjoerg 
4861f2f256Sjoerg #if !defined(_MSC_VER) && !I386_FREEBSD
49b9829059Sjoerg #include <stdint.h>
5061f2f256Sjoerg #endif
5161f2f256Sjoerg 
5261f2f256Sjoerg #if defined(_MSC_VER)
53ef84fd3bSjoerg typedef unsigned char uint8_t;
54b9829059Sjoerg typedef unsigned int uint32_t;
5561f2f256Sjoerg typedef unsigned long long uint64_t;
5661f2f256Sjoerg #elif I386_FREEBSD
5761f2f256Sjoerg /* System headers define 'size_t' incorrectly on x64 FreeBSD (prior to
5861f2f256Sjoerg  * FreeBSD 10, r232261) when compiled in 32-bit mode.
5961f2f256Sjoerg  */
6061f2f256Sjoerg typedef unsigned char uint8_t;
6161f2f256Sjoerg typedef unsigned int uint32_t;
6261f2f256Sjoerg typedef unsigned long long uint64_t;
63b9829059Sjoerg #endif
64b9829059Sjoerg 
65b9829059Sjoerg /* #define DEBUG_GCDAPROFILING */
66b9829059Sjoerg 
67b9829059Sjoerg /*
68b9829059Sjoerg  * --- GCOV file format I/O primitives ---
69b9829059Sjoerg  */
70b9829059Sjoerg 
71b9829059Sjoerg /*
72b9829059Sjoerg  * The current file name we're outputting. Used primarily for error logging.
73b9829059Sjoerg  */
74b9829059Sjoerg static char *filename = NULL;
75b9829059Sjoerg 
76b9829059Sjoerg /*
77b9829059Sjoerg  * The current file we're outputting.
78b9829059Sjoerg  */
79b9829059Sjoerg static FILE *output_file = NULL;
80b9829059Sjoerg 
81b9829059Sjoerg /*
82b9829059Sjoerg  * Buffer that we write things into.
83b9829059Sjoerg  */
84b9829059Sjoerg #define WRITE_BUFFER_SIZE (128 * 1024)
85b9829059Sjoerg static char *write_buffer = NULL;
86b9829059Sjoerg static uint64_t cur_buffer_size = 0;
87b9829059Sjoerg static uint64_t cur_pos = 0;
88b9829059Sjoerg static uint64_t file_size = 0;
89b9829059Sjoerg static int new_file = 0;
90b9829059Sjoerg static int fd = -1;
91b9829059Sjoerg 
92b9829059Sjoerg /*
93b9829059Sjoerg  * A list of functions to write out the data.
94b9829059Sjoerg  */
95b9829059Sjoerg typedef void (*writeout_fn)();
96b9829059Sjoerg 
97b9829059Sjoerg struct writeout_fn_node {
98b9829059Sjoerg   writeout_fn fn;
99b9829059Sjoerg   struct writeout_fn_node *next;
100b9829059Sjoerg };
101b9829059Sjoerg 
102b9829059Sjoerg static struct writeout_fn_node *writeout_fn_head = NULL;
103b9829059Sjoerg static struct writeout_fn_node *writeout_fn_tail = NULL;
104b9829059Sjoerg 
105b9829059Sjoerg /*
106b9829059Sjoerg  *  A list of flush functions that our __gcov_flush() function should call.
107b9829059Sjoerg  */
108b9829059Sjoerg typedef void (*flush_fn)();
109b9829059Sjoerg 
110b9829059Sjoerg struct flush_fn_node {
111b9829059Sjoerg   flush_fn fn;
112b9829059Sjoerg   struct flush_fn_node *next;
113b9829059Sjoerg };
114b9829059Sjoerg 
115b9829059Sjoerg static struct flush_fn_node *flush_fn_head = NULL;
116b9829059Sjoerg static struct flush_fn_node *flush_fn_tail = NULL;
117b9829059Sjoerg 
resize_write_buffer(uint64_t size)118b9829059Sjoerg static void resize_write_buffer(uint64_t size) {
119b9829059Sjoerg   if (!new_file) return;
120b9829059Sjoerg   size += cur_pos;
121b9829059Sjoerg   if (size <= cur_buffer_size) return;
122b9829059Sjoerg   size = (size - 1) / WRITE_BUFFER_SIZE + 1;
123b9829059Sjoerg   size *= WRITE_BUFFER_SIZE;
124b9829059Sjoerg   write_buffer = realloc(write_buffer, size);
125b9829059Sjoerg   cur_buffer_size = size;
126b9829059Sjoerg }
127b9829059Sjoerg 
write_bytes(const char * s,size_t len)128b9829059Sjoerg static void write_bytes(const char *s, size_t len) {
129b9829059Sjoerg   resize_write_buffer(len);
130b9829059Sjoerg   memcpy(&write_buffer[cur_pos], s, len);
131b9829059Sjoerg   cur_pos += len;
132b9829059Sjoerg }
133b9829059Sjoerg 
write_32bit_value(uint32_t i)134b9829059Sjoerg static void write_32bit_value(uint32_t i) {
135b9829059Sjoerg   write_bytes((char*)&i, 4);
136b9829059Sjoerg }
137b9829059Sjoerg 
write_64bit_value(uint64_t i)138b9829059Sjoerg static void write_64bit_value(uint64_t i) {
139b9829059Sjoerg   write_bytes((char*)&i, 8);
140b9829059Sjoerg }
141b9829059Sjoerg 
length_of_string(const char * s)142b9829059Sjoerg static uint32_t length_of_string(const char *s) {
143b9829059Sjoerg   return (strlen(s) / 4) + 1;
144b9829059Sjoerg }
145b9829059Sjoerg 
write_string(const char * s)146b9829059Sjoerg static void write_string(const char *s) {
147b9829059Sjoerg   uint32_t len = length_of_string(s);
148b9829059Sjoerg   write_32bit_value(len);
149b9829059Sjoerg   write_bytes(s, strlen(s));
150b9829059Sjoerg   write_bytes("\0\0\0\0", 4 - (strlen(s) % 4));
151b9829059Sjoerg }
152b9829059Sjoerg 
read_32bit_value()153b9829059Sjoerg static uint32_t read_32bit_value() {
154b9829059Sjoerg   uint32_t val;
155b9829059Sjoerg 
156b9829059Sjoerg   if (new_file)
157b9829059Sjoerg     return (uint32_t)-1;
158b9829059Sjoerg 
159b9829059Sjoerg   val = *(uint32_t*)&write_buffer[cur_pos];
160b9829059Sjoerg   cur_pos += 4;
161b9829059Sjoerg   return val;
162b9829059Sjoerg }
163b9829059Sjoerg 
read_64bit_value()164b9829059Sjoerg static uint64_t read_64bit_value() {
165b9829059Sjoerg   uint64_t val;
166b9829059Sjoerg 
167b9829059Sjoerg   if (new_file)
168b9829059Sjoerg     return (uint64_t)-1;
169b9829059Sjoerg 
170b9829059Sjoerg   val = *(uint64_t*)&write_buffer[cur_pos];
171b9829059Sjoerg   cur_pos += 8;
172b9829059Sjoerg   return val;
173b9829059Sjoerg }
174b9829059Sjoerg 
mangle_filename(const char * orig_filename)175b9829059Sjoerg static char *mangle_filename(const char *orig_filename) {
176b4b49684Sjoerg   char *new_filename;
177b4b49684Sjoerg   size_t filename_len, prefix_len;
178b4b49684Sjoerg   int prefix_strip;
179b9829059Sjoerg   int level = 0;
180b4b49684Sjoerg   const char *fname, *ptr;
181b9829059Sjoerg   const char *prefix = getenv("GCOV_PREFIX");
182b9829059Sjoerg   const char *prefix_strip_str = getenv("GCOV_PREFIX_STRIP");
183b9829059Sjoerg 
184b4b49684Sjoerg   if (prefix == NULL || prefix[0] == '\0')
185b9829059Sjoerg     return strdup(orig_filename);
186b9829059Sjoerg 
187b9829059Sjoerg   if (prefix_strip_str) {
188b9829059Sjoerg     prefix_strip = atoi(prefix_strip_str);
189b9829059Sjoerg 
190b9829059Sjoerg     /* Negative GCOV_PREFIX_STRIP values are ignored */
191b9829059Sjoerg     if (prefix_strip < 0)
192b9829059Sjoerg       prefix_strip = 0;
193b4b49684Sjoerg   } else {
194b4b49684Sjoerg     prefix_strip = 0;
195b9829059Sjoerg   }
196b9829059Sjoerg 
197b4b49684Sjoerg   fname = orig_filename;
198b4b49684Sjoerg   for (level = 0, ptr = fname + 1; level < prefix_strip; ++ptr) {
199b4b49684Sjoerg     if (*ptr == '\0')
200b4b49684Sjoerg       break;
201b4b49684Sjoerg     if (*ptr != '/')
202b4b49684Sjoerg       continue;
203b9829059Sjoerg     fname = ptr;
204b9829059Sjoerg     ++level;
205b9829059Sjoerg   }
206b9829059Sjoerg 
207b4b49684Sjoerg   filename_len = strlen(fname);
208b4b49684Sjoerg   prefix_len = strlen(prefix);
209b4b49684Sjoerg   new_filename = malloc(prefix_len + 1 + filename_len + 1);
210b4b49684Sjoerg   memcpy(new_filename, prefix, prefix_len);
211b9829059Sjoerg 
212b4b49684Sjoerg   if (prefix[prefix_len - 1] != '/')
213f1ec592dSjoerg     new_filename[prefix_len++] = '/';
214b4b49684Sjoerg   memcpy(new_filename + prefix_len, fname, filename_len + 1);
215b4b49684Sjoerg 
216b4b49684Sjoerg   return new_filename;
217b9829059Sjoerg }
218b9829059Sjoerg 
map_file()219b9829059Sjoerg static int map_file() {
220b9829059Sjoerg   fseek(output_file, 0L, SEEK_END);
221b9829059Sjoerg   file_size = ftell(output_file);
222b9829059Sjoerg 
223b4b49684Sjoerg   /* A size of 0 is invalid to `mmap'. Return a fail here, but don't issue an
224b4b49684Sjoerg    * error message because it should "just work" for the user. */
225b4b49684Sjoerg   if (file_size == 0)
226b4b49684Sjoerg     return -1;
227b4b49684Sjoerg 
228b9829059Sjoerg   write_buffer = mmap(0, file_size, PROT_READ | PROT_WRITE,
229b9829059Sjoerg                       MAP_FILE | MAP_SHARED, fd, 0);
230b9829059Sjoerg   if (write_buffer == (void *)-1) {
231b9829059Sjoerg     int errnum = errno;
232b9829059Sjoerg     fprintf(stderr, "profiling: %s: cannot map: %s\n", filename,
233b9829059Sjoerg             strerror(errnum));
234b9829059Sjoerg     return -1;
235b9829059Sjoerg   }
236b9829059Sjoerg   return 0;
237b9829059Sjoerg }
238b9829059Sjoerg 
unmap_file()239b9829059Sjoerg static void unmap_file() {
240b9829059Sjoerg   if (msync(write_buffer, file_size, MS_SYNC) == -1) {
241b9829059Sjoerg     int errnum = errno;
242b9829059Sjoerg     fprintf(stderr, "profiling: %s: cannot msync: %s\n", filename,
243b9829059Sjoerg             strerror(errnum));
244b9829059Sjoerg   }
245b9829059Sjoerg 
246b9829059Sjoerg   /* We explicitly ignore errors from unmapping because at this point the data
247b9829059Sjoerg    * is written and we don't care.
248b9829059Sjoerg    */
249b9829059Sjoerg   (void)munmap(write_buffer, file_size);
250b9829059Sjoerg   write_buffer = NULL;
251b9829059Sjoerg   file_size = 0;
252b9829059Sjoerg }
253b9829059Sjoerg 
254b9829059Sjoerg /*
255b9829059Sjoerg  * --- LLVM line counter API ---
256b9829059Sjoerg  */
257b9829059Sjoerg 
258b9829059Sjoerg /* A file in this case is a translation unit. Each .o file built with line
259b9829059Sjoerg  * profiling enabled will emit to a different file. Only one file may be
260b9829059Sjoerg  * started at a time.
261b9829059Sjoerg  */
llvm_gcda_start_file(const char * orig_filename,const char version[4],uint32_t checksum)262b4b49684Sjoerg void llvm_gcda_start_file(const char *orig_filename, const char version[4],
263b4b49684Sjoerg                           uint32_t checksum) {
264b9829059Sjoerg   const char *mode = "r+b";
265b9829059Sjoerg   filename = mangle_filename(orig_filename);
266b9829059Sjoerg 
267b9829059Sjoerg   /* Try just opening the file. */
268b9829059Sjoerg   new_file = 0;
269b9829059Sjoerg   fd = open(filename, O_RDWR);
270b9829059Sjoerg 
271b9829059Sjoerg   if (fd == -1) {
272b9829059Sjoerg     /* Try opening the file, creating it if necessary. */
273b9829059Sjoerg     new_file = 1;
274b9829059Sjoerg     mode = "w+b";
275b9829059Sjoerg     fd = open(filename, O_RDWR | O_CREAT, 0644);
276b9829059Sjoerg     if (fd == -1) {
277b9829059Sjoerg       /* Try creating the directories first then opening the file. */
278ef84fd3bSjoerg       __llvm_profile_recursive_mkdir(filename);
279b9829059Sjoerg       fd = open(filename, O_RDWR | O_CREAT, 0644);
280b9829059Sjoerg       if (fd == -1) {
281b9829059Sjoerg         /* Bah! It's hopeless. */
282b9829059Sjoerg         int errnum = errno;
283b9829059Sjoerg         fprintf(stderr, "profiling: %s: cannot open: %s\n", filename,
284b9829059Sjoerg                 strerror(errnum));
285b9829059Sjoerg         return;
286b9829059Sjoerg       }
287b9829059Sjoerg     }
288b9829059Sjoerg   }
289b9829059Sjoerg 
290ef84fd3bSjoerg   /* Try to flock the file to serialize concurrent processes writing out to the
291ef84fd3bSjoerg    * same GCDA. This can fail if the filesystem doesn't support it, but in that
292ef84fd3bSjoerg    * case we'll just carry on with the old racy behaviour and hope for the best.
293ef84fd3bSjoerg    */
294ef84fd3bSjoerg   flock(fd, LOCK_EX);
295b9829059Sjoerg   output_file = fdopen(fd, mode);
296b9829059Sjoerg 
297b9829059Sjoerg   /* Initialize the write buffer. */
298b9829059Sjoerg   write_buffer = NULL;
299b9829059Sjoerg   cur_buffer_size = 0;
300b9829059Sjoerg   cur_pos = 0;
301b9829059Sjoerg 
302b9829059Sjoerg   if (new_file) {
303b9829059Sjoerg     resize_write_buffer(WRITE_BUFFER_SIZE);
304b9829059Sjoerg     memset(write_buffer, 0, WRITE_BUFFER_SIZE);
305b9829059Sjoerg   } else {
306b9829059Sjoerg     if (map_file() == -1) {
307b9829059Sjoerg       /* mmap failed, try to recover by clobbering */
308b9829059Sjoerg       new_file = 1;
309b9829059Sjoerg       write_buffer = NULL;
310b9829059Sjoerg       cur_buffer_size = 0;
311b9829059Sjoerg       resize_write_buffer(WRITE_BUFFER_SIZE);
312b9829059Sjoerg       memset(write_buffer, 0, WRITE_BUFFER_SIZE);
313b9829059Sjoerg     }
314b9829059Sjoerg   }
315b9829059Sjoerg 
316b4b49684Sjoerg   /* gcda file, version, stamp checksum. */
317b9829059Sjoerg   write_bytes("adcg", 4);
318b9829059Sjoerg   write_bytes(version, 4);
319b4b49684Sjoerg   write_32bit_value(checksum);
320b9829059Sjoerg 
321b9829059Sjoerg #ifdef DEBUG_GCDAPROFILING
322b9829059Sjoerg   fprintf(stderr, "llvmgcda: [%s]\n", orig_filename);
323b9829059Sjoerg #endif
324b9829059Sjoerg }
325b9829059Sjoerg 
326b9829059Sjoerg /* Given an array of pointers to counters (counters), increment the n-th one,
327b9829059Sjoerg  * where we're also given a pointer to n (predecessor).
328b9829059Sjoerg  */
llvm_gcda_increment_indirect_counter(uint32_t * predecessor,uint64_t ** counters)329b9829059Sjoerg void llvm_gcda_increment_indirect_counter(uint32_t *predecessor,
330b9829059Sjoerg                                           uint64_t **counters) {
331b9829059Sjoerg   uint64_t *counter;
332b9829059Sjoerg   uint32_t pred;
333b9829059Sjoerg 
334b9829059Sjoerg   pred = *predecessor;
335b9829059Sjoerg   if (pred == 0xffffffff)
336b9829059Sjoerg     return;
337b9829059Sjoerg   counter = counters[pred];
338b9829059Sjoerg 
339b9829059Sjoerg   /* Don't crash if the pred# is out of sync. This can happen due to threads,
340b9829059Sjoerg      or because of a TODO in GCOVProfiling.cpp buildEdgeLookupTable(). */
341b9829059Sjoerg   if (counter)
342b9829059Sjoerg     ++*counter;
343b9829059Sjoerg #ifdef DEBUG_GCDAPROFILING
344b9829059Sjoerg   else
345b9829059Sjoerg     fprintf(stderr,
346b9829059Sjoerg             "llvmgcda: increment_indirect_counter counters=%08llx, pred=%u\n",
347b9829059Sjoerg             *counter, *predecessor);
348b9829059Sjoerg #endif
349b9829059Sjoerg }
350b9829059Sjoerg 
llvm_gcda_emit_function(uint32_t ident,const char * function_name,uint32_t func_checksum,uint8_t use_extra_checksum,uint32_t cfg_checksum)351b9829059Sjoerg void llvm_gcda_emit_function(uint32_t ident, const char *function_name,
352b4b49684Sjoerg                              uint32_t func_checksum, uint8_t use_extra_checksum,
353b4b49684Sjoerg                              uint32_t cfg_checksum) {
354b9829059Sjoerg   uint32_t len = 2;
355b9829059Sjoerg 
356b9829059Sjoerg   if (use_extra_checksum)
357b9829059Sjoerg     len++;
358b9829059Sjoerg #ifdef DEBUG_GCDAPROFILING
359b9829059Sjoerg   fprintf(stderr, "llvmgcda: function id=0x%08x name=%s\n", ident,
360b9829059Sjoerg           function_name ? function_name : "NULL");
361b9829059Sjoerg #endif
362b9829059Sjoerg   if (!output_file) return;
363b9829059Sjoerg 
364b9829059Sjoerg   /* function tag */
365b9829059Sjoerg   write_bytes("\0\0\0\1", 4);
366b9829059Sjoerg   if (function_name)
367b9829059Sjoerg     len += 1 + length_of_string(function_name);
368b9829059Sjoerg   write_32bit_value(len);
369b9829059Sjoerg   write_32bit_value(ident);
370b4b49684Sjoerg   write_32bit_value(func_checksum);
371b9829059Sjoerg   if (use_extra_checksum)
372b4b49684Sjoerg     write_32bit_value(cfg_checksum);
373b9829059Sjoerg   if (function_name)
374b9829059Sjoerg     write_string(function_name);
375b9829059Sjoerg }
376b9829059Sjoerg 
llvm_gcda_emit_arcs(uint32_t num_counters,uint64_t * counters)377b9829059Sjoerg void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
378b9829059Sjoerg   uint32_t i;
379b9829059Sjoerg   uint64_t *old_ctrs = NULL;
380b9829059Sjoerg   uint32_t val = 0;
381b9829059Sjoerg   uint64_t save_cur_pos = cur_pos;
382b9829059Sjoerg 
383b9829059Sjoerg   if (!output_file) return;
384b9829059Sjoerg 
385b9829059Sjoerg   val = read_32bit_value();
386b9829059Sjoerg 
387b9829059Sjoerg   if (val != (uint32_t)-1) {
388b9829059Sjoerg     /* There are counters present in the file. Merge them. */
389b9829059Sjoerg     if (val != 0x01a10000) {
390ef84fd3bSjoerg       fprintf(stderr, "profiling: %s: cannot merge previous GCDA file: "
391ef84fd3bSjoerg                       "corrupt arc tag (0x%08x)\n",
392ef84fd3bSjoerg               filename, val);
393b9829059Sjoerg       return;
394b9829059Sjoerg     }
395b9829059Sjoerg 
396b9829059Sjoerg     val = read_32bit_value();
397b9829059Sjoerg     if (val == (uint32_t)-1 || val / 2 != num_counters) {
398ef84fd3bSjoerg       fprintf(stderr, "profiling: %s: cannot merge previous GCDA file: "
399ef84fd3bSjoerg                       "mismatched number of counters (%d)\n",
400ef84fd3bSjoerg               filename, val);
401b9829059Sjoerg       return;
402b9829059Sjoerg     }
403b9829059Sjoerg 
404b9829059Sjoerg     old_ctrs = malloc(sizeof(uint64_t) * num_counters);
405b9829059Sjoerg     for (i = 0; i < num_counters; ++i)
406b9829059Sjoerg       old_ctrs[i] = read_64bit_value();
407b9829059Sjoerg   }
408b9829059Sjoerg 
409b9829059Sjoerg   cur_pos = save_cur_pos;
410b9829059Sjoerg 
411b9829059Sjoerg   /* Counter #1 (arcs) tag */
412b9829059Sjoerg   write_bytes("\0\0\xa1\1", 4);
413b9829059Sjoerg   write_32bit_value(num_counters * 2);
414b9829059Sjoerg   for (i = 0; i < num_counters; ++i) {
415b9829059Sjoerg     counters[i] += (old_ctrs ? old_ctrs[i] : 0);
416b9829059Sjoerg     write_64bit_value(counters[i]);
417b9829059Sjoerg   }
418b9829059Sjoerg 
419b9829059Sjoerg   free(old_ctrs);
420b9829059Sjoerg 
421b9829059Sjoerg #ifdef DEBUG_GCDAPROFILING
422b9829059Sjoerg   fprintf(stderr, "llvmgcda:   %u arcs\n", num_counters);
423b9829059Sjoerg   for (i = 0; i < num_counters; ++i)
424b9829059Sjoerg     fprintf(stderr, "llvmgcda:   %llu\n", (unsigned long long)counters[i]);
425b9829059Sjoerg #endif
426b9829059Sjoerg }
427b9829059Sjoerg 
llvm_gcda_summary_info()428b4b49684Sjoerg void llvm_gcda_summary_info() {
429b4b49684Sjoerg   const uint32_t obj_summary_len = 9; /* Length for gcov compatibility. */
430b4b49684Sjoerg   uint32_t i;
431b4b49684Sjoerg   uint32_t runs = 1;
432b4b49684Sjoerg   uint32_t val = 0;
433b4b49684Sjoerg   uint64_t save_cur_pos = cur_pos;
434b4b49684Sjoerg 
435b4b49684Sjoerg   if (!output_file) return;
436b4b49684Sjoerg 
437b4b49684Sjoerg   val = read_32bit_value();
438b4b49684Sjoerg 
439b4b49684Sjoerg   if (val != (uint32_t)-1) {
440b4b49684Sjoerg     /* There are counters present in the file. Merge them. */
441b4b49684Sjoerg     if (val != 0xa1000000) {
442ef84fd3bSjoerg       fprintf(stderr, "profiling: %s: cannot merge previous run count: "
443ef84fd3bSjoerg                       "corrupt object tag (0x%08x)\n",
444ef84fd3bSjoerg               filename, val);
445b4b49684Sjoerg       return;
446b4b49684Sjoerg     }
447b4b49684Sjoerg 
448b4b49684Sjoerg     val = read_32bit_value(); /* length */
449b4b49684Sjoerg     if (val != obj_summary_len) {
450ef84fd3bSjoerg       fprintf(stderr, "profiling: %s: cannot merge previous run count: "
451ef84fd3bSjoerg                       "mismatched object length (%d)\n",
452ef84fd3bSjoerg               filename, val);
453b4b49684Sjoerg       return;
454b4b49684Sjoerg     }
455b4b49684Sjoerg 
456b4b49684Sjoerg     read_32bit_value(); /* checksum, unused */
457b4b49684Sjoerg     read_32bit_value(); /* num, unused */
458b4b49684Sjoerg     runs += read_32bit_value(); /* Add previous run count to new counter. */
459b4b49684Sjoerg   }
460b4b49684Sjoerg 
461b4b49684Sjoerg   cur_pos = save_cur_pos;
462b4b49684Sjoerg 
463b4b49684Sjoerg   /* Object summary tag */
464b4b49684Sjoerg   write_bytes("\0\0\0\xa1", 4);
465b4b49684Sjoerg   write_32bit_value(obj_summary_len);
466b4b49684Sjoerg   write_32bit_value(0); /* checksum, unused */
467b4b49684Sjoerg   write_32bit_value(0); /* num, unused */
468b4b49684Sjoerg   write_32bit_value(runs);
469b4b49684Sjoerg   for (i = 3; i < obj_summary_len; ++i)
470b4b49684Sjoerg     write_32bit_value(0);
471b4b49684Sjoerg 
472b4b49684Sjoerg   /* Program summary tag */
473b4b49684Sjoerg   write_bytes("\0\0\0\xa3", 4); /* tag indicates 1 program */
474b4b49684Sjoerg   write_32bit_value(0); /* 0 length */
475b4b49684Sjoerg 
476b4b49684Sjoerg #ifdef DEBUG_GCDAPROFILING
477b4b49684Sjoerg   fprintf(stderr, "llvmgcda:   %u runs\n", runs);
478b4b49684Sjoerg #endif
479b4b49684Sjoerg }
480b4b49684Sjoerg 
llvm_gcda_end_file()481b9829059Sjoerg void llvm_gcda_end_file() {
482b9829059Sjoerg   /* Write out EOF record. */
483b9829059Sjoerg   if (output_file) {
484b9829059Sjoerg     write_bytes("\0\0\0\0\0\0\0\0", 8);
485b9829059Sjoerg 
486b9829059Sjoerg     if (new_file) {
487b9829059Sjoerg       fwrite(write_buffer, cur_pos, 1, output_file);
488b9829059Sjoerg       free(write_buffer);
489b9829059Sjoerg     } else {
490b9829059Sjoerg       unmap_file();
491b9829059Sjoerg     }
492b9829059Sjoerg 
493b9829059Sjoerg     fclose(output_file);
494ef84fd3bSjoerg     flock(fd, LOCK_UN);
495b9829059Sjoerg     output_file = NULL;
496b9829059Sjoerg     write_buffer = NULL;
497b9829059Sjoerg   }
498b9829059Sjoerg   free(filename);
499b9829059Sjoerg 
500b9829059Sjoerg #ifdef DEBUG_GCDAPROFILING
501b9829059Sjoerg   fprintf(stderr, "llvmgcda: -----\n");
502b9829059Sjoerg #endif
503b9829059Sjoerg }
504b9829059Sjoerg 
llvm_register_writeout_function(writeout_fn fn)505b9829059Sjoerg void llvm_register_writeout_function(writeout_fn fn) {
506b9829059Sjoerg   struct writeout_fn_node *new_node = malloc(sizeof(struct writeout_fn_node));
507b9829059Sjoerg   new_node->fn = fn;
508b9829059Sjoerg   new_node->next = NULL;
509b9829059Sjoerg 
510b9829059Sjoerg   if (!writeout_fn_head) {
511b9829059Sjoerg     writeout_fn_head = writeout_fn_tail = new_node;
512b9829059Sjoerg   } else {
513b9829059Sjoerg     writeout_fn_tail->next = new_node;
514b9829059Sjoerg     writeout_fn_tail = new_node;
515b9829059Sjoerg   }
516b9829059Sjoerg }
517b9829059Sjoerg 
llvm_writeout_files()518b9829059Sjoerg void llvm_writeout_files() {
519b9829059Sjoerg   struct writeout_fn_node *curr = writeout_fn_head;
520b9829059Sjoerg 
521b9829059Sjoerg   while (curr) {
522b9829059Sjoerg     curr->fn();
523b9829059Sjoerg     curr = curr->next;
524b9829059Sjoerg   }
525b9829059Sjoerg }
526b9829059Sjoerg 
llvm_delete_writeout_function_list()527b9829059Sjoerg void llvm_delete_writeout_function_list() {
528b9829059Sjoerg   while (writeout_fn_head) {
529b9829059Sjoerg     struct writeout_fn_node *node = writeout_fn_head;
530b9829059Sjoerg     writeout_fn_head = writeout_fn_head->next;
531b9829059Sjoerg     free(node);
532b9829059Sjoerg   }
533b9829059Sjoerg 
534b9829059Sjoerg   writeout_fn_head = writeout_fn_tail = NULL;
535b9829059Sjoerg }
536b9829059Sjoerg 
llvm_register_flush_function(flush_fn fn)537b9829059Sjoerg void llvm_register_flush_function(flush_fn fn) {
538b9829059Sjoerg   struct flush_fn_node *new_node = malloc(sizeof(struct flush_fn_node));
539b9829059Sjoerg   new_node->fn = fn;
540b9829059Sjoerg   new_node->next = NULL;
541b9829059Sjoerg 
542b9829059Sjoerg   if (!flush_fn_head) {
543b9829059Sjoerg     flush_fn_head = flush_fn_tail = new_node;
544b9829059Sjoerg   } else {
545b9829059Sjoerg     flush_fn_tail->next = new_node;
546b9829059Sjoerg     flush_fn_tail = new_node;
547b9829059Sjoerg   }
548b9829059Sjoerg }
549b9829059Sjoerg 
__gcov_flush()550b9829059Sjoerg void __gcov_flush() {
551b9829059Sjoerg   struct flush_fn_node *curr = flush_fn_head;
552b9829059Sjoerg 
553b9829059Sjoerg   while (curr) {
554b9829059Sjoerg     curr->fn();
555b9829059Sjoerg     curr = curr->next;
556b9829059Sjoerg   }
557b9829059Sjoerg }
558b9829059Sjoerg 
llvm_delete_flush_function_list()559b9829059Sjoerg void llvm_delete_flush_function_list() {
560b9829059Sjoerg   while (flush_fn_head) {
561b9829059Sjoerg     struct flush_fn_node *node = flush_fn_head;
562b9829059Sjoerg     flush_fn_head = flush_fn_head->next;
563b9829059Sjoerg     free(node);
564b9829059Sjoerg   }
565b9829059Sjoerg 
566b9829059Sjoerg   flush_fn_head = flush_fn_tail = NULL;
567b9829059Sjoerg }
568b9829059Sjoerg 
llvm_gcov_init(writeout_fn wfn,flush_fn ffn)569b9829059Sjoerg void llvm_gcov_init(writeout_fn wfn, flush_fn ffn) {
570b9829059Sjoerg   static int atexit_ran = 0;
571b9829059Sjoerg 
572b9829059Sjoerg   if (wfn)
573b9829059Sjoerg     llvm_register_writeout_function(wfn);
574b9829059Sjoerg 
575b9829059Sjoerg   if (ffn)
576b9829059Sjoerg     llvm_register_flush_function(ffn);
577b9829059Sjoerg 
578b9829059Sjoerg   if (atexit_ran == 0) {
579b9829059Sjoerg     atexit_ran = 1;
580b9829059Sjoerg 
581b9829059Sjoerg     /* Make sure we write out the data and delete the data structures. */
582b9829059Sjoerg     atexit(llvm_delete_flush_function_list);
583b9829059Sjoerg     atexit(llvm_delete_writeout_function_list);
584b9829059Sjoerg     atexit(llvm_writeout_files);
585b9829059Sjoerg   }
586b9829059Sjoerg }
587