blob: e32e97fdb8c4777deeb1feba10e685838e4ee04b [file] [log] [blame]
Daniel Dunbar8c881192011-11-17 00:12:10 +00001/*===- GCDAProfiling.c - Support library for GCDA file emission -----------===*\
2|*
3|* The LLVM Compiler Infrastructure
4|*
5|* This file is distributed under the University of Illinois Open Source
6|* License. See LICENSE.TXT for details.
7|*
8|*===----------------------------------------------------------------------===*|
9|*
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.
13|*
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.
17|*
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.
20|*
21\*===----------------------------------------------------------------------===*/
22
Chandler Carruth254abfa2013-06-25 00:37:32 +000023#include <errno.h>
Bill Wendlingd06f2fc2013-05-23 07:18:59 +000024#include <fcntl.h>
Daniel Dunbar8c881192011-11-17 00:12:10 +000025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Bill Wendlingd06f2fc2013-05-23 07:18:59 +000028#include <sys/mman.h>
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070029#include <sys/file.h>
Daniel Dunbar8c881192011-11-17 00:12:10 +000030#ifdef _WIN32
31#include <direct.h>
32#endif
33
Stephen Hines2d1fdb22014-05-28 23:58:16 -070034#define I386_FREEBSD (defined(__FreeBSD__) && defined(__i386__))
35
36#if !I386_FREEBSD
37#include <sys/stat.h>
38#include <sys/types.h>
39#endif
40
41#if !defined(_MSC_VER) && !I386_FREEBSD
Daniel Dunbar8c881192011-11-17 00:12:10 +000042#include <stdint.h>
Stephen Hines2d1fdb22014-05-28 23:58:16 -070043#endif
44
45#if defined(_MSC_VER)
Daniel Dunbar8c881192011-11-17 00:12:10 +000046typedef unsigned int uint32_t;
Stephen Hines2d1fdb22014-05-28 23:58:16 -070047typedef unsigned long long uint64_t;
48#elif I386_FREEBSD
49/* System headers define 'size_t' incorrectly on x64 FreeBSD (prior to
50 * FreeBSD 10, r232261) when compiled in 32-bit mode.
51 */
52typedef unsigned char uint8_t;
53typedef unsigned int uint32_t;
54typedef unsigned long long uint64_t;
55int mkdir(const char*, unsigned short);
Daniel Dunbar8c881192011-11-17 00:12:10 +000056#endif
57
58/* #define DEBUG_GCDAPROFILING */
59
60/*
61 * --- GCOV file format I/O primitives ---
62 */
63
Bill Wendlinga5391832013-03-18 22:59:47 +000064/*
Chandler Carruth254abfa2013-06-25 00:37:32 +000065 * The current file name we're outputting. Used primarily for error logging.
66 */
67static char *filename = NULL;
68
69/*
Bill Wendlinga5391832013-03-18 22:59:47 +000070 * The current file we're outputting.
71 */
Daniel Dunbar8c881192011-11-17 00:12:10 +000072static FILE *output_file = NULL;
73
Bill Wendlinga5391832013-03-18 22:59:47 +000074/*
Bill Wendling2a46a602013-05-15 21:31:22 +000075 * Buffer that we write things into.
76 */
Bill Wendling2811a0c2013-05-23 18:18:31 +000077#define WRITE_BUFFER_SIZE (128 * 1024)
78static char *write_buffer = NULL;
Bill Wendlingd06f2fc2013-05-23 07:18:59 +000079static uint64_t cur_buffer_size = 0;
80static uint64_t cur_pos = 0;
81static uint64_t file_size = 0;
Bill Wendling2a46a602013-05-15 21:31:22 +000082static int new_file = 0;
Bill Wendlingd06f2fc2013-05-23 07:18:59 +000083static int fd = -1;
Bill Wendling2a46a602013-05-15 21:31:22 +000084
85/*
Bill Wendlingc5333042013-03-19 21:01:19 +000086 * A list of functions to write out the data.
87 */
88typedef void (*writeout_fn)();
89
90struct writeout_fn_node {
91 writeout_fn fn;
92 struct writeout_fn_node *next;
93};
94
Bill Wendling2a46a602013-05-15 21:31:22 +000095static struct writeout_fn_node *writeout_fn_head = NULL;
96static struct writeout_fn_node *writeout_fn_tail = NULL;
Bill Wendlingc5333042013-03-19 21:01:19 +000097
98/*
Bill Wendlinga5391832013-03-18 22:59:47 +000099 * A list of flush functions that our __gcov_flush() function should call.
100 */
101typedef void (*flush_fn)();
102
103struct flush_fn_node {
104 flush_fn fn;
105 struct flush_fn_node *next;
106};
107
Bill Wendling2a46a602013-05-15 21:31:22 +0000108static struct flush_fn_node *flush_fn_head = NULL;
109static struct flush_fn_node *flush_fn_tail = NULL;
Bill Wendlinga5391832013-03-18 22:59:47 +0000110
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000111static void resize_write_buffer(uint64_t size) {
112 if (!new_file) return;
113 size += cur_pos;
114 if (size <= cur_buffer_size) return;
Bill Wendling2811a0c2013-05-23 18:18:31 +0000115 size = (size - 1) / WRITE_BUFFER_SIZE + 1;
116 size *= WRITE_BUFFER_SIZE;
117 write_buffer = realloc(write_buffer, size);
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000118 cur_buffer_size = size;
Daniel Dunbar8c881192011-11-17 00:12:10 +0000119}
120
Bill Wendling2a46a602013-05-15 21:31:22 +0000121static void write_bytes(const char *s, size_t len) {
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000122 resize_write_buffer(len);
Bill Wendling2811a0c2013-05-23 18:18:31 +0000123 memcpy(&write_buffer[cur_pos], s, len);
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000124 cur_pos += len;
Bill Wendling2a46a602013-05-15 21:31:22 +0000125}
126
127static void write_32bit_value(uint32_t i) {
128 write_bytes((char*)&i, 4);
129}
130
131static void write_64bit_value(uint64_t i) {
132 write_bytes((char*)&i, 8);
Daniel Dunbar8c881192011-11-17 00:12:10 +0000133}
134
135static uint32_t length_of_string(const char *s) {
136 return (strlen(s) / 4) + 1;
137}
138
139static void write_string(const char *s) {
140 uint32_t len = length_of_string(s);
Bill Wendling2a46a602013-05-15 21:31:22 +0000141 write_32bit_value(len);
142 write_bytes(s, strlen(s));
143 write_bytes("\0\0\0\0", 4 - (strlen(s) % 4));
Daniel Dunbar8c881192011-11-17 00:12:10 +0000144}
145
Bill Wendling2a46a602013-05-15 21:31:22 +0000146static uint32_t read_32bit_value() {
147 uint32_t val;
Bill Wendling843f3592012-09-14 18:55:32 +0000148
Bill Wendling2a46a602013-05-15 21:31:22 +0000149 if (new_file)
Bill Wendling843f3592012-09-14 18:55:32 +0000150 return (uint32_t)-1;
151
Bill Wendling2811a0c2013-05-23 18:18:31 +0000152 val = *(uint32_t*)&write_buffer[cur_pos];
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000153 cur_pos += 4;
Bill Wendling2a46a602013-05-15 21:31:22 +0000154 return val;
Bill Wendling843f3592012-09-14 18:55:32 +0000155}
156
Bill Wendling2a46a602013-05-15 21:31:22 +0000157static uint64_t read_64bit_value() {
158 uint64_t val;
Bill Wendling843f3592012-09-14 18:55:32 +0000159
Bill Wendling2a46a602013-05-15 21:31:22 +0000160 if (new_file)
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000161 return (uint64_t)-1;
Bill Wendling2a46a602013-05-15 21:31:22 +0000162
Bill Wendling2811a0c2013-05-23 18:18:31 +0000163 val = *(uint64_t*)&write_buffer[cur_pos];
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000164 cur_pos += 8;
Bill Wendling2a46a602013-05-15 21:31:22 +0000165 return val;
Bill Wendling843f3592012-09-14 18:55:32 +0000166}
167
Daniel Dunbar8c881192011-11-17 00:12:10 +0000168static char *mangle_filename(const char *orig_filename) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700169 char *new_filename;
170 size_t filename_len, prefix_len;
171 int prefix_strip;
Bill Wendlingec63f452012-05-28 10:09:01 +0000172 int level = 0;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700173 const char *fname, *ptr;
Bill Wendling906d5a52012-05-28 09:41:48 +0000174 const char *prefix = getenv("GCOV_PREFIX");
Bill Wendling2a46a602013-05-15 21:31:22 +0000175 const char *prefix_strip_str = getenv("GCOV_PREFIX_STRIP");
Daniel Dunbar8c881192011-11-17 00:12:10 +0000176
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700177 if (prefix == NULL || prefix[0] == '\0')
Daniel Dunbar8c881192011-11-17 00:12:10 +0000178 return strdup(orig_filename);
179
Bill Wendling2a46a602013-05-15 21:31:22 +0000180 if (prefix_strip_str) {
181 prefix_strip = atoi(prefix_strip_str);
Bill Wendlingec63f452012-05-28 10:09:01 +0000182
183 /* Negative GCOV_PREFIX_STRIP values are ignored */
184 if (prefix_strip < 0)
185 prefix_strip = 0;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700186 } else {
187 prefix_strip = 0;
Bill Wendlingec63f452012-05-28 10:09:01 +0000188 }
189
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700190 fname = orig_filename;
191 for (level = 0, ptr = fname + 1; level < prefix_strip; ++ptr) {
192 if (*ptr == '\0')
193 break;
194 if (*ptr != '/')
195 continue;
Bill Wendlingec63f452012-05-28 10:09:01 +0000196 fname = ptr;
197 ++level;
198 }
199
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700200 filename_len = strlen(fname);
201 prefix_len = strlen(prefix);
202 new_filename = malloc(prefix_len + 1 + filename_len + 1);
203 memcpy(new_filename, prefix, prefix_len);
Daniel Dunbar8c881192011-11-17 00:12:10 +0000204
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700205 if (prefix[prefix_len - 1] != '/')
206 new_filename[prefix_len++] = '/';
207 memcpy(new_filename + prefix_len, fname, filename_len + 1);
208
209 return new_filename;
Daniel Dunbar8c881192011-11-17 00:12:10 +0000210}
211
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700212static void recursive_mkdir(char *path) {
Bill Wendling906d5a52012-05-28 09:41:48 +0000213 int i;
Daniel Dunbar8c881192011-11-17 00:12:10 +0000214
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700215 for (i = 1; path[i] != '\0'; ++i) {
216 if (path[i] != '/') continue;
217 path[i] = '\0';
Daniel Dunbar8c881192011-11-17 00:12:10 +0000218#ifdef _WIN32
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700219 _mkdir(path);
Daniel Dunbar8c881192011-11-17 00:12:10 +0000220#else
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700221 mkdir(path, 0755); /* Some of these will fail, ignore it. */
Daniel Dunbar8c881192011-11-17 00:12:10 +0000222#endif
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700223 path[i] = '/';
Daniel Dunbar8c881192011-11-17 00:12:10 +0000224 }
225}
226
Chandler Carruth254abfa2013-06-25 00:37:32 +0000227static int map_file() {
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000228 fseek(output_file, 0L, SEEK_END);
229 file_size = ftell(output_file);
230
Bill Wendling08a0e082013-09-11 19:35:32 +0000231 /* A size of 0 is invalid to `mmap'. Return a fail here, but don't issue an
Bill Wendling7ab94ea2013-09-09 22:25:46 +0000232 * error message because it should "just work" for the user. */
233 if (file_size == 0)
234 return -1;
235
Bill Wendling2811a0c2013-05-23 18:18:31 +0000236 write_buffer = mmap(0, file_size, PROT_READ | PROT_WRITE,
237 MAP_FILE | MAP_SHARED, fd, 0);
Chandler Carruth254abfa2013-06-25 00:37:32 +0000238 if (write_buffer == (void *)-1) {
239 int errnum = errno;
240 fprintf(stderr, "profiling: %s: cannot map: %s\n", filename,
241 strerror(errnum));
242 return -1;
243 }
244 return 0;
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000245}
246
247static void unmap_file() {
Chandler Carruth254abfa2013-06-25 00:37:32 +0000248 if (msync(write_buffer, file_size, MS_SYNC) == -1) {
249 int errnum = errno;
250 fprintf(stderr, "profiling: %s: cannot msync: %s\n", filename,
251 strerror(errnum));
252 }
253
254 /* We explicitly ignore errors from unmapping because at this point the data
255 * is written and we don't care.
256 */
257 (void)munmap(write_buffer, file_size);
Bill Wendling2811a0c2013-05-23 18:18:31 +0000258 write_buffer = NULL;
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000259 file_size = 0;
260}
261
Daniel Dunbar8c881192011-11-17 00:12:10 +0000262/*
263 * --- LLVM line counter API ---
264 */
265
266/* A file in this case is a translation unit. Each .o file built with line
267 * profiling enabled will emit to a different file. Only one file may be
268 * started at a time.
269 */
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700270void llvm_gcda_start_file(const char *orig_filename, const char version[4],
271 uint32_t checksum) {
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000272 const char *mode = "r+b";
Chandler Carruth254abfa2013-06-25 00:37:32 +0000273 filename = mangle_filename(orig_filename);
Bill Wendling843f3592012-09-14 18:55:32 +0000274
275 /* Try just opening the file. */
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000276 new_file = 0;
277 fd = open(filename, O_RDWR);
Bill Wendling5a240c52012-05-28 02:34:34 +0000278
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000279 if (fd == -1) {
Bill Wendling843f3592012-09-14 18:55:32 +0000280 /* Try opening the file, creating it if necessary. */
Bill Wendling2a46a602013-05-15 21:31:22 +0000281 new_file = 1;
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000282 mode = "w+b";
Bill Wendling2811a0c2013-05-23 18:18:31 +0000283 fd = open(filename, O_RDWR | O_CREAT, 0644);
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000284 if (fd == -1) {
Bill Wendling843f3592012-09-14 18:55:32 +0000285 /* Try creating the directories first then opening the file. */
286 recursive_mkdir(filename);
Bill Wendling2811a0c2013-05-23 18:18:31 +0000287 fd = open(filename, O_RDWR | O_CREAT, 0644);
Chandler Carruth254abfa2013-06-25 00:37:32 +0000288 if (fd == -1) {
Bill Wendling843f3592012-09-14 18:55:32 +0000289 /* Bah! It's hopeless. */
Chandler Carruth254abfa2013-06-25 00:37:32 +0000290 int errnum = errno;
291 fprintf(stderr, "profiling: %s: cannot open: %s\n", filename,
292 strerror(errnum));
Bill Wendling843f3592012-09-14 18:55:32 +0000293 return;
294 }
Bill Wendling5a240c52012-05-28 02:34:34 +0000295 }
296 }
Daniel Dunbar8c881192011-11-17 00:12:10 +0000297
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700298 /* Try to flock the file to serialize concurrent processes writing out to the
299 * same GCDA. This can fail if the filesystem doesn't support it, but in that
300 * case we'll just carry on with the old racy behaviour and hope for the best.
301 */
302 flock(fd, LOCK_EX);
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000303 output_file = fdopen(fd, mode);
Bill Wendling2a46a602013-05-15 21:31:22 +0000304
305 /* Initialize the write buffer. */
Bill Wendling2811a0c2013-05-23 18:18:31 +0000306 write_buffer = NULL;
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000307 cur_buffer_size = 0;
308 cur_pos = 0;
309
310 if (new_file) {
Bill Wendling2811a0c2013-05-23 18:18:31 +0000311 resize_write_buffer(WRITE_BUFFER_SIZE);
312 memset(write_buffer, 0, WRITE_BUFFER_SIZE);
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000313 } else {
Chandler Carruth254abfa2013-06-25 00:37:32 +0000314 if (map_file() == -1) {
315 /* mmap failed, try to recover by clobbering */
316 new_file = 1;
317 write_buffer = NULL;
318 cur_buffer_size = 0;
319 resize_write_buffer(WRITE_BUFFER_SIZE);
320 memset(write_buffer, 0, WRITE_BUFFER_SIZE);
321 }
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000322 }
Bill Wendling2a46a602013-05-15 21:31:22 +0000323
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700324 /* gcda file, version, stamp checksum. */
Bill Wendling2a46a602013-05-15 21:31:22 +0000325 write_bytes("adcg", 4);
326 write_bytes(version, 4);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700327 write_32bit_value(checksum);
Bill Wendling2a46a602013-05-15 21:31:22 +0000328
Bill Wendling843f3592012-09-14 18:55:32 +0000329#ifdef DEBUG_GCDAPROFILING
330 fprintf(stderr, "llvmgcda: [%s]\n", orig_filename);
331#endif
Daniel Dunbar8c881192011-11-17 00:12:10 +0000332}
333
334/* Given an array of pointers to counters (counters), increment the n-th one,
335 * where we're also given a pointer to n (predecessor).
336 */
337void llvm_gcda_increment_indirect_counter(uint32_t *predecessor,
338 uint64_t **counters) {
339 uint64_t *counter;
340 uint32_t pred;
341
342 pred = *predecessor;
343 if (pred == 0xffffffff)
344 return;
345 counter = counters[pred];
346
347 /* Don't crash if the pred# is out of sync. This can happen due to threads,
348 or because of a TODO in GCOVProfiling.cpp buildEdgeLookupTable(). */
349 if (counter)
350 ++*counter;
351#ifdef DEBUG_GCDAPROFILING
352 else
Bill Wendlingde47cb82012-05-28 02:50:53 +0000353 fprintf(stderr,
Bill Wendling843f3592012-09-14 18:55:32 +0000354 "llvmgcda: increment_indirect_counter counters=%08llx, pred=%u\n",
355 *counter, *predecessor);
Daniel Dunbar8c881192011-11-17 00:12:10 +0000356#endif
357}
358
Nick Lewycky07020822013-03-09 01:33:12 +0000359void llvm_gcda_emit_function(uint32_t ident, const char *function_name,
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700360 uint32_t func_checksum, uint8_t use_extra_checksum,
361 uint32_t cfg_checksum) {
Nick Lewycky07020822013-03-09 01:33:12 +0000362 uint32_t len = 2;
Bill Wendling2a46a602013-05-15 21:31:22 +0000363
Nick Lewycky07020822013-03-09 01:33:12 +0000364 if (use_extra_checksum)
365 len++;
Daniel Dunbar8c881192011-11-17 00:12:10 +0000366#ifdef DEBUG_GCDAPROFILING
Nick Lewycky58c400c2013-02-28 07:00:13 +0000367 fprintf(stderr, "llvmgcda: function id=0x%08x name=%s\n", ident,
368 function_name ? function_name : "NULL");
Daniel Dunbar8c881192011-11-17 00:12:10 +0000369#endif
Bill Wendling5a240c52012-05-28 02:34:34 +0000370 if (!output_file) return;
Daniel Dunbar8c881192011-11-17 00:12:10 +0000371
Nick Lewycky58c400c2013-02-28 07:00:13 +0000372 /* function tag */
Bill Wendling2a46a602013-05-15 21:31:22 +0000373 write_bytes("\0\0\0\1", 4);
Nick Lewycky58c400c2013-02-28 07:00:13 +0000374 if (function_name)
375 len += 1 + length_of_string(function_name);
Bill Wendling2a46a602013-05-15 21:31:22 +0000376 write_32bit_value(len);
377 write_32bit_value(ident);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700378 write_32bit_value(func_checksum);
Nick Lewycky07020822013-03-09 01:33:12 +0000379 if (use_extra_checksum)
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700380 write_32bit_value(cfg_checksum);
Nick Lewycky58c400c2013-02-28 07:00:13 +0000381 if (function_name)
382 write_string(function_name);
Daniel Dunbar8c881192011-11-17 00:12:10 +0000383}
384
385void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
386 uint32_t i;
Bill Wendling843f3592012-09-14 18:55:32 +0000387 uint64_t *old_ctrs = NULL;
388 uint32_t val = 0;
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000389 uint64_t save_cur_pos = cur_pos;
Bill Wendling843f3592012-09-14 18:55:32 +0000390
391 if (!output_file) return;
392
Bill Wendling2a46a602013-05-15 21:31:22 +0000393 val = read_32bit_value();
Bill Wendling843f3592012-09-14 18:55:32 +0000394
Bill Wendling843f3592012-09-14 18:55:32 +0000395 if (val != (uint32_t)-1) {
396 /* There are counters present in the file. Merge them. */
Bill Wendling843f3592012-09-14 18:55:32 +0000397 if (val != 0x01a10000) {
Stephen Hines6d186232014-11-26 17:56:19 -0800398 fprintf(stderr, "profiling: %s: cannot merge previous GCDA file: "
399 "corrupt arc tag (0x%08x)\n",
400 filename, val);
Bill Wendling843f3592012-09-14 18:55:32 +0000401 return;
402 }
403
Bill Wendling2a46a602013-05-15 21:31:22 +0000404 val = read_32bit_value();
Bill Wendling843f3592012-09-14 18:55:32 +0000405 if (val == (uint32_t)-1 || val / 2 != num_counters) {
Stephen Hines6d186232014-11-26 17:56:19 -0800406 fprintf(stderr, "profiling: %s: cannot merge previous GCDA file: "
407 "mismatched number of counters (%d)\n",
408 filename, val);
Bill Wendling843f3592012-09-14 18:55:32 +0000409 return;
410 }
411
412 old_ctrs = malloc(sizeof(uint64_t) * num_counters);
Bill Wendling2a46a602013-05-15 21:31:22 +0000413 for (i = 0; i < num_counters; ++i)
414 old_ctrs[i] = read_64bit_value();
Bill Wendling843f3592012-09-14 18:55:32 +0000415 }
416
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000417 cur_pos = save_cur_pos;
418
Bill Wendling5a240c52012-05-28 02:34:34 +0000419 /* Counter #1 (arcs) tag */
Bill Wendling2a46a602013-05-15 21:31:22 +0000420 write_bytes("\0\0\xa1\1", 4);
421 write_32bit_value(num_counters * 2);
422 for (i = 0; i < num_counters; ++i) {
Bill Wendling4aa08fa2013-04-22 03:36:22 +0000423 counters[i] += (old_ctrs ? old_ctrs[i] : 0);
Bill Wendling2a46a602013-05-15 21:31:22 +0000424 write_64bit_value(counters[i]);
Bill Wendling4aa08fa2013-04-22 03:36:22 +0000425 }
Bill Wendling843f3592012-09-14 18:55:32 +0000426
427 free(old_ctrs);
Daniel Dunbar8c881192011-11-17 00:12:10 +0000428
429#ifdef DEBUG_GCDAPROFILING
Bill Wendling843f3592012-09-14 18:55:32 +0000430 fprintf(stderr, "llvmgcda: %u arcs\n", num_counters);
Bill Wendling5a240c52012-05-28 02:34:34 +0000431 for (i = 0; i < num_counters; ++i)
Bill Wendling843f3592012-09-14 18:55:32 +0000432 fprintf(stderr, "llvmgcda: %llu\n", (unsigned long long)counters[i]);
Daniel Dunbar8c881192011-11-17 00:12:10 +0000433#endif
434}
435
Yuchen Wu7bb5d492013-11-12 18:45:50 +0000436void llvm_gcda_summary_info() {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700437 const uint32_t obj_summary_len = 9; /* Length for gcov compatibility. */
Yuchen Wu7bb5d492013-11-12 18:45:50 +0000438 uint32_t i;
439 uint32_t runs = 1;
440 uint32_t val = 0;
441 uint64_t save_cur_pos = cur_pos;
442
443 if (!output_file) return;
444
445 val = read_32bit_value();
446
447 if (val != (uint32_t)-1) {
448 /* There are counters present in the file. Merge them. */
449 if (val != 0xa1000000) {
Stephen Hines6d186232014-11-26 17:56:19 -0800450 fprintf(stderr, "profiling: %s: cannot merge previous run count: "
451 "corrupt object tag (0x%08x)\n",
452 filename, val);
Yuchen Wu7bb5d492013-11-12 18:45:50 +0000453 return;
454 }
455
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700456 val = read_32bit_value(); /* length */
Yuchen Wu7bb5d492013-11-12 18:45:50 +0000457 if (val != obj_summary_len) {
Stephen Hines6d186232014-11-26 17:56:19 -0800458 fprintf(stderr, "profiling: %s: cannot merge previous run count: "
459 "mismatched object length (%d)\n",
460 filename, val);
Yuchen Wu7bb5d492013-11-12 18:45:50 +0000461 return;
462 }
463
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700464 read_32bit_value(); /* checksum, unused */
465 read_32bit_value(); /* num, unused */
466 runs += read_32bit_value(); /* Add previous run count to new counter. */
Yuchen Wu7bb5d492013-11-12 18:45:50 +0000467 }
468
469 cur_pos = save_cur_pos;
470
471 /* Object summary tag */
472 write_bytes("\0\0\0\xa1", 4);
473 write_32bit_value(obj_summary_len);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700474 write_32bit_value(0); /* checksum, unused */
475 write_32bit_value(0); /* num, unused */
Yuchen Wu7bb5d492013-11-12 18:45:50 +0000476 write_32bit_value(runs);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700477 for (i = 3; i < obj_summary_len; ++i)
Yuchen Wu7bb5d492013-11-12 18:45:50 +0000478 write_32bit_value(0);
479
480 /* Program summary tag */
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700481 write_bytes("\0\0\0\xa3", 4); /* tag indicates 1 program */
482 write_32bit_value(0); /* 0 length */
Yuchen Wu7bb5d492013-11-12 18:45:50 +0000483
484#ifdef DEBUG_GCDAPROFILING
485 fprintf(stderr, "llvmgcda: %u runs\n", runs);
486#endif
487}
488
Daniel Dunbar8c881192011-11-17 00:12:10 +0000489void llvm_gcda_end_file() {
490 /* Write out EOF record. */
Chandler Carruthcf5fb622013-06-26 00:26:16 +0000491 if (output_file) {
492 write_bytes("\0\0\0\0\0\0\0\0", 8);
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000493
Chandler Carruthcf5fb622013-06-26 00:26:16 +0000494 if (new_file) {
495 fwrite(write_buffer, cur_pos, 1, output_file);
496 free(write_buffer);
497 } else {
498 unmap_file();
499 }
500
501 fclose(output_file);
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700502 flock(fd, LOCK_UN);
Chandler Carruthcf5fb622013-06-26 00:26:16 +0000503 output_file = NULL;
504 write_buffer = NULL;
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000505 }
Chandler Carruthcf5fb622013-06-26 00:26:16 +0000506 free(filename);
Daniel Dunbar8c881192011-11-17 00:12:10 +0000507
508#ifdef DEBUG_GCDAPROFILING
Bill Wendling843f3592012-09-14 18:55:32 +0000509 fprintf(stderr, "llvmgcda: -----\n");
Daniel Dunbar8c881192011-11-17 00:12:10 +0000510#endif
511}
Bill Wendlinga5391832013-03-18 22:59:47 +0000512
Bill Wendlingc5333042013-03-19 21:01:19 +0000513void llvm_register_writeout_function(writeout_fn fn) {
514 struct writeout_fn_node *new_node = malloc(sizeof(struct writeout_fn_node));
515 new_node->fn = fn;
516 new_node->next = NULL;
517
518 if (!writeout_fn_head) {
519 writeout_fn_head = writeout_fn_tail = new_node;
520 } else {
521 writeout_fn_tail->next = new_node;
522 writeout_fn_tail = new_node;
523 }
524}
525
Bill Wendling84b46d32013-03-20 21:11:47 +0000526void llvm_writeout_files() {
Bill Wendlingc5333042013-03-19 21:01:19 +0000527 struct writeout_fn_node *curr = writeout_fn_head;
528
529 while (curr) {
530 curr->fn();
531 curr = curr->next;
532 }
533}
534
535void llvm_delete_writeout_function_list() {
536 while (writeout_fn_head) {
537 struct writeout_fn_node *node = writeout_fn_head;
538 writeout_fn_head = writeout_fn_head->next;
539 free(node);
540 }
541
542 writeout_fn_head = writeout_fn_tail = NULL;
543}
544
Bill Wendlinga5391832013-03-18 22:59:47 +0000545void llvm_register_flush_function(flush_fn fn) {
546 struct flush_fn_node *new_node = malloc(sizeof(struct flush_fn_node));
547 new_node->fn = fn;
548 new_node->next = NULL;
549
550 if (!flush_fn_head) {
551 flush_fn_head = flush_fn_tail = new_node;
552 } else {
553 flush_fn_tail->next = new_node;
554 flush_fn_tail = new_node;
555 }
556}
557
558void __gcov_flush() {
559 struct flush_fn_node *curr = flush_fn_head;
560
561 while (curr) {
562 curr->fn();
563 curr = curr->next;
564 }
565}
566
567void llvm_delete_flush_function_list() {
568 while (flush_fn_head) {
569 struct flush_fn_node *node = flush_fn_head;
570 flush_fn_head = flush_fn_head->next;
571 free(node);
572 }
573
574 flush_fn_head = flush_fn_tail = NULL;
575}
Bill Wendling84b46d32013-03-20 21:11:47 +0000576
577void llvm_gcov_init(writeout_fn wfn, flush_fn ffn) {
578 static int atexit_ran = 0;
579
580 if (wfn)
581 llvm_register_writeout_function(wfn);
582
583 if (ffn)
584 llvm_register_flush_function(ffn);
585
586 if (atexit_ran == 0) {
587 atexit_ran = 1;
588
589 /* Make sure we write out the data and delete the data structures. */
590 atexit(llvm_delete_flush_function_list);
591 atexit(llvm_delete_writeout_function_list);
592 atexit(llvm_writeout_files);
593 }
594}