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