blob: eb8a937f3f26869c9bf4359979237dc75957465e [file] [log] [blame]
Daniel Dunbarfb161142011-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
Xinliang David Liaeff1512016-07-15 18:48:14 +000023#include "InstrProfilingPort.h"
Xinliang David Life9ecc92016-07-18 16:16:12 +000024#include "InstrProfilingUtil.h"
Diego Novilloeae95142015-07-09 17:21:52 +000025
Chandler Carrutha26c8142013-06-25 00:37:32 +000026#include <errno.h>
Bill Wendling353fbd32013-05-23 07:18:59 +000027#include <fcntl.h>
Daniel Dunbarfb161142011-11-17 00:12:10 +000028#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Nathan Slingerlandba86c922016-01-05 17:27:01 +000031
32#if defined(_WIN32)
33#include "WindowsMMap.h"
34#else
Bill Wendling353fbd32013-05-23 07:18:59 +000035#include <sys/mman.h>
Justin Bogner7759f182015-04-03 19:10:35 +000036#include <sys/file.h>
Xinliang David Lic5307c62016-07-25 23:12:53 +000037#ifndef MAP_FILE
38#define MAP_FILE 0
39#endif
Nathan Slingerlandba86c922016-01-05 17:27:01 +000040#endif
Daniel Dunbarfb161142011-11-17 00:12:10 +000041
Nico Weber82210fb2016-01-19 22:02:12 +000042#if defined(__FreeBSD__) && defined(__i386__)
43#define I386_FREEBSD 1
44#else
45#define I386_FREEBSD 0
46#endif
Viktor Kutuzovf31e5362014-03-10 16:50:53 +000047
Viktor Kutuzovf31e5362014-03-10 16:50:53 +000048#if !defined(_MSC_VER) && !I386_FREEBSD
Daniel Dunbarfb161142011-11-17 00:12:10 +000049#include <stdint.h>
Viktor Kutuzovf31e5362014-03-10 16:50:53 +000050#endif
51
52#if defined(_MSC_VER)
Nathan Slingerlandba86c922016-01-05 17:27:01 +000053typedef unsigned char uint8_t;
Daniel Dunbarfb161142011-11-17 00:12:10 +000054typedef unsigned int uint32_t;
Duncan P. N. Exon Smith61c97332014-03-19 22:10:24 +000055typedef unsigned long long uint64_t;
Viktor Kutuzovf31e5362014-03-10 16:50:53 +000056#elif I386_FREEBSD
57/* System headers define 'size_t' incorrectly on x64 FreeBSD (prior to
58 * FreeBSD 10, r232261) when compiled in 32-bit mode.
59 */
60typedef unsigned char uint8_t;
61typedef unsigned int uint32_t;
62typedef unsigned long long uint64_t;
Daniel Dunbarfb161142011-11-17 00:12:10 +000063#endif
64
65/* #define DEBUG_GCDAPROFILING */
66
67/*
68 * --- GCOV file format I/O primitives ---
69 */
70
Bill Wendling2428f162013-03-18 22:59:47 +000071/*
Chandler Carrutha26c8142013-06-25 00:37:32 +000072 * The current file name we're outputting. Used primarily for error logging.
73 */
74static char *filename = NULL;
75
76/*
Bill Wendling2428f162013-03-18 22:59:47 +000077 * The current file we're outputting.
78 */
Daniel Dunbarfb161142011-11-17 00:12:10 +000079static FILE *output_file = NULL;
80
Bill Wendling2428f162013-03-18 22:59:47 +000081/*
Bill Wendlingc0540862013-05-15 21:31:22 +000082 * Buffer that we write things into.
83 */
Bill Wendlingb7e7a382013-05-23 18:18:31 +000084#define WRITE_BUFFER_SIZE (128 * 1024)
85static char *write_buffer = NULL;
Bill Wendling353fbd32013-05-23 07:18:59 +000086static uint64_t cur_buffer_size = 0;
87static uint64_t cur_pos = 0;
88static uint64_t file_size = 0;
Bill Wendlingc0540862013-05-15 21:31:22 +000089static int new_file = 0;
Bill Wendling353fbd32013-05-23 07:18:59 +000090static int fd = -1;
Bill Wendlingc0540862013-05-15 21:31:22 +000091
92/*
Bill Wendlinge6476592013-03-19 21:01:19 +000093 * A list of functions to write out the data.
94 */
Alex Lorenzfbac1ae2017-08-31 13:23:24 +000095typedef void (*writeout_fn)(void);
Bill Wendlinge6476592013-03-19 21:01:19 +000096
97struct writeout_fn_node {
98 writeout_fn fn;
99 struct writeout_fn_node *next;
100};
101
Bill Wendlingc0540862013-05-15 21:31:22 +0000102static struct writeout_fn_node *writeout_fn_head = NULL;
103static struct writeout_fn_node *writeout_fn_tail = NULL;
Bill Wendlinge6476592013-03-19 21:01:19 +0000104
105/*
Bill Wendling2428f162013-03-18 22:59:47 +0000106 * A list of flush functions that our __gcov_flush() function should call.
107 */
Alex Lorenzfbac1ae2017-08-31 13:23:24 +0000108typedef void (*flush_fn)(void);
Bill Wendling2428f162013-03-18 22:59:47 +0000109
110struct flush_fn_node {
111 flush_fn fn;
112 struct flush_fn_node *next;
113};
114
Bill Wendlingc0540862013-05-15 21:31:22 +0000115static struct flush_fn_node *flush_fn_head = NULL;
116static struct flush_fn_node *flush_fn_tail = NULL;
Bill Wendling2428f162013-03-18 22:59:47 +0000117
Bill Wendling353fbd32013-05-23 07:18:59 +0000118static void resize_write_buffer(uint64_t size) {
119 if (!new_file) return;
120 size += cur_pos;
121 if (size <= cur_buffer_size) return;
Bill Wendlingb7e7a382013-05-23 18:18:31 +0000122 size = (size - 1) / WRITE_BUFFER_SIZE + 1;
123 size *= WRITE_BUFFER_SIZE;
124 write_buffer = realloc(write_buffer, size);
Bill Wendling353fbd32013-05-23 07:18:59 +0000125 cur_buffer_size = size;
Daniel Dunbarfb161142011-11-17 00:12:10 +0000126}
127
Bill Wendlingc0540862013-05-15 21:31:22 +0000128static void write_bytes(const char *s, size_t len) {
Bill Wendling353fbd32013-05-23 07:18:59 +0000129 resize_write_buffer(len);
Bill Wendlingb7e7a382013-05-23 18:18:31 +0000130 memcpy(&write_buffer[cur_pos], s, len);
Bill Wendling353fbd32013-05-23 07:18:59 +0000131 cur_pos += len;
Bill Wendlingc0540862013-05-15 21:31:22 +0000132}
133
134static void write_32bit_value(uint32_t i) {
135 write_bytes((char*)&i, 4);
136}
137
138static void write_64bit_value(uint64_t i) {
139 write_bytes((char*)&i, 8);
Daniel Dunbarfb161142011-11-17 00:12:10 +0000140}
141
142static uint32_t length_of_string(const char *s) {
143 return (strlen(s) / 4) + 1;
144}
145
146static void write_string(const char *s) {
147 uint32_t len = length_of_string(s);
Bill Wendlingc0540862013-05-15 21:31:22 +0000148 write_32bit_value(len);
149 write_bytes(s, strlen(s));
150 write_bytes("\0\0\0\0", 4 - (strlen(s) % 4));
Daniel Dunbarfb161142011-11-17 00:12:10 +0000151}
152
Bill Wendlingc0540862013-05-15 21:31:22 +0000153static uint32_t read_32bit_value() {
154 uint32_t val;
Bill Wendling66429fe2012-09-14 18:55:32 +0000155
Bill Wendlingc0540862013-05-15 21:31:22 +0000156 if (new_file)
Bill Wendling66429fe2012-09-14 18:55:32 +0000157 return (uint32_t)-1;
158
Bill Wendlingb7e7a382013-05-23 18:18:31 +0000159 val = *(uint32_t*)&write_buffer[cur_pos];
Bill Wendling353fbd32013-05-23 07:18:59 +0000160 cur_pos += 4;
Bill Wendlingc0540862013-05-15 21:31:22 +0000161 return val;
Bill Wendling66429fe2012-09-14 18:55:32 +0000162}
163
Bill Wendlingc0540862013-05-15 21:31:22 +0000164static uint64_t read_64bit_value() {
165 uint64_t val;
Bill Wendling66429fe2012-09-14 18:55:32 +0000166
Bill Wendlingc0540862013-05-15 21:31:22 +0000167 if (new_file)
Bill Wendling353fbd32013-05-23 07:18:59 +0000168 return (uint64_t)-1;
Bill Wendlingc0540862013-05-15 21:31:22 +0000169
Bill Wendlingb7e7a382013-05-23 18:18:31 +0000170 val = *(uint64_t*)&write_buffer[cur_pos];
Bill Wendling353fbd32013-05-23 07:18:59 +0000171 cur_pos += 8;
Bill Wendlingc0540862013-05-15 21:31:22 +0000172 return val;
Bill Wendling66429fe2012-09-14 18:55:32 +0000173}
174
Daniel Dunbarfb161142011-11-17 00:12:10 +0000175static char *mangle_filename(const char *orig_filename) {
Joerg Sonnenbergerc9894552014-01-15 20:57:10 +0000176 char *new_filename;
Xinliang David Life9ecc92016-07-18 16:16:12 +0000177 size_t prefix_len;
Joerg Sonnenbergerc9894552014-01-15 20:57:10 +0000178 int prefix_strip;
Xinliang David Life9ecc92016-07-18 16:16:12 +0000179 const char *prefix = lprofGetPathPrefix(&prefix_strip, &prefix_len);
Daniel Dunbarfb161142011-11-17 00:12:10 +0000180
Xinliang David Life9ecc92016-07-18 16:16:12 +0000181 if (prefix == NULL)
Daniel Dunbarfb161142011-11-17 00:12:10 +0000182 return strdup(orig_filename);
183
Xinliang David Life9ecc92016-07-18 16:16:12 +0000184 new_filename = malloc(prefix_len + 1 + strlen(orig_filename) + 1);
185 lprofApplyPathPrefix(new_filename, orig_filename, prefix, prefix_len,
186 prefix_strip);
Joerg Sonnenbergerc9894552014-01-15 20:57:10 +0000187
188 return new_filename;
Daniel Dunbarfb161142011-11-17 00:12:10 +0000189}
190
Chandler Carrutha26c8142013-06-25 00:37:32 +0000191static int map_file() {
Bill Wendling353fbd32013-05-23 07:18:59 +0000192 fseek(output_file, 0L, SEEK_END);
193 file_size = ftell(output_file);
194
Bill Wendlinga6ec07c2013-09-11 19:35:32 +0000195 /* A size of 0 is invalid to `mmap'. Return a fail here, but don't issue an
Bill Wendling97c22c32013-09-09 22:25:46 +0000196 * error message because it should "just work" for the user. */
197 if (file_size == 0)
198 return -1;
199
Bill Wendlingb7e7a382013-05-23 18:18:31 +0000200 write_buffer = mmap(0, file_size, PROT_READ | PROT_WRITE,
201 MAP_FILE | MAP_SHARED, fd, 0);
Chandler Carrutha26c8142013-06-25 00:37:32 +0000202 if (write_buffer == (void *)-1) {
203 int errnum = errno;
204 fprintf(stderr, "profiling: %s: cannot map: %s\n", filename,
205 strerror(errnum));
206 return -1;
207 }
208 return 0;
Bill Wendling353fbd32013-05-23 07:18:59 +0000209}
210
211static void unmap_file() {
Chandler Carrutha26c8142013-06-25 00:37:32 +0000212 if (msync(write_buffer, file_size, MS_SYNC) == -1) {
213 int errnum = errno;
214 fprintf(stderr, "profiling: %s: cannot msync: %s\n", filename,
215 strerror(errnum));
216 }
217
218 /* We explicitly ignore errors from unmapping because at this point the data
219 * is written and we don't care.
220 */
221 (void)munmap(write_buffer, file_size);
Bill Wendlingb7e7a382013-05-23 18:18:31 +0000222 write_buffer = NULL;
Bill Wendling353fbd32013-05-23 07:18:59 +0000223 file_size = 0;
224}
225
Daniel Dunbarfb161142011-11-17 00:12:10 +0000226/*
227 * --- LLVM line counter API ---
228 */
229
230/* A file in this case is a translation unit. Each .o file built with line
231 * profiling enabled will emit to a different file. Only one file may be
232 * started at a time.
233 */
Yuchen Wuea7611c2013-11-20 04:14:48 +0000234void llvm_gcda_start_file(const char *orig_filename, const char version[4],
235 uint32_t checksum) {
Bill Wendling353fbd32013-05-23 07:18:59 +0000236 const char *mode = "r+b";
Chandler Carrutha26c8142013-06-25 00:37:32 +0000237 filename = mangle_filename(orig_filename);
Bill Wendling66429fe2012-09-14 18:55:32 +0000238
239 /* Try just opening the file. */
Bill Wendling353fbd32013-05-23 07:18:59 +0000240 new_file = 0;
241 fd = open(filename, O_RDWR);
Bill Wendling74f987f2012-05-28 02:34:34 +0000242
Bill Wendling353fbd32013-05-23 07:18:59 +0000243 if (fd == -1) {
Bill Wendling66429fe2012-09-14 18:55:32 +0000244 /* Try opening the file, creating it if necessary. */
Bill Wendlingc0540862013-05-15 21:31:22 +0000245 new_file = 1;
Bill Wendling353fbd32013-05-23 07:18:59 +0000246 mode = "w+b";
Bill Wendlingb7e7a382013-05-23 18:18:31 +0000247 fd = open(filename, O_RDWR | O_CREAT, 0644);
Bill Wendling353fbd32013-05-23 07:18:59 +0000248 if (fd == -1) {
Bill Wendling66429fe2012-09-14 18:55:32 +0000249 /* Try creating the directories first then opening the file. */
Diego Novilloeae95142015-07-09 17:21:52 +0000250 __llvm_profile_recursive_mkdir(filename);
Bill Wendlingb7e7a382013-05-23 18:18:31 +0000251 fd = open(filename, O_RDWR | O_CREAT, 0644);
Chandler Carrutha26c8142013-06-25 00:37:32 +0000252 if (fd == -1) {
Bill Wendling66429fe2012-09-14 18:55:32 +0000253 /* Bah! It's hopeless. */
Chandler Carrutha26c8142013-06-25 00:37:32 +0000254 int errnum = errno;
255 fprintf(stderr, "profiling: %s: cannot open: %s\n", filename,
256 strerror(errnum));
Bill Wendling66429fe2012-09-14 18:55:32 +0000257 return;
258 }
Bill Wendling74f987f2012-05-28 02:34:34 +0000259 }
260 }
Daniel Dunbarfb161142011-11-17 00:12:10 +0000261
Justin Bogner8e1a2a32015-04-03 18:55:44 +0000262 /* Try to flock the file to serialize concurrent processes writing out to the
263 * same GCDA. This can fail if the filesystem doesn't support it, but in that
264 * case we'll just carry on with the old racy behaviour and hope for the best.
265 */
266 flock(fd, LOCK_EX);
Bill Wendling353fbd32013-05-23 07:18:59 +0000267 output_file = fdopen(fd, mode);
Bill Wendlingc0540862013-05-15 21:31:22 +0000268
269 /* Initialize the write buffer. */
Bill Wendlingb7e7a382013-05-23 18:18:31 +0000270 write_buffer = NULL;
Bill Wendling353fbd32013-05-23 07:18:59 +0000271 cur_buffer_size = 0;
272 cur_pos = 0;
273
274 if (new_file) {
Bill Wendlingb7e7a382013-05-23 18:18:31 +0000275 resize_write_buffer(WRITE_BUFFER_SIZE);
276 memset(write_buffer, 0, WRITE_BUFFER_SIZE);
Bill Wendling353fbd32013-05-23 07:18:59 +0000277 } else {
Chandler Carrutha26c8142013-06-25 00:37:32 +0000278 if (map_file() == -1) {
279 /* mmap failed, try to recover by clobbering */
280 new_file = 1;
281 write_buffer = NULL;
282 cur_buffer_size = 0;
283 resize_write_buffer(WRITE_BUFFER_SIZE);
284 memset(write_buffer, 0, WRITE_BUFFER_SIZE);
285 }
Bill Wendling353fbd32013-05-23 07:18:59 +0000286 }
Bill Wendlingc0540862013-05-15 21:31:22 +0000287
Yuchen Wuea7611c2013-11-20 04:14:48 +0000288 /* gcda file, version, stamp checksum. */
Bill Wendlingc0540862013-05-15 21:31:22 +0000289 write_bytes("adcg", 4);
290 write_bytes(version, 4);
Yuchen Wuea7611c2013-11-20 04:14:48 +0000291 write_32bit_value(checksum);
Bill Wendlingc0540862013-05-15 21:31:22 +0000292
Bill Wendling66429fe2012-09-14 18:55:32 +0000293#ifdef DEBUG_GCDAPROFILING
294 fprintf(stderr, "llvmgcda: [%s]\n", orig_filename);
295#endif
Daniel Dunbarfb161142011-11-17 00:12:10 +0000296}
297
298/* Given an array of pointers to counters (counters), increment the n-th one,
299 * where we're also given a pointer to n (predecessor).
300 */
301void llvm_gcda_increment_indirect_counter(uint32_t *predecessor,
302 uint64_t **counters) {
303 uint64_t *counter;
304 uint32_t pred;
305
306 pred = *predecessor;
307 if (pred == 0xffffffff)
308 return;
309 counter = counters[pred];
310
311 /* Don't crash if the pred# is out of sync. This can happen due to threads,
312 or because of a TODO in GCOVProfiling.cpp buildEdgeLookupTable(). */
313 if (counter)
314 ++*counter;
315#ifdef DEBUG_GCDAPROFILING
316 else
Bill Wendlingf1608472012-05-28 02:50:53 +0000317 fprintf(stderr,
Bill Wendling66429fe2012-09-14 18:55:32 +0000318 "llvmgcda: increment_indirect_counter counters=%08llx, pred=%u\n",
319 *counter, *predecessor);
Daniel Dunbarfb161142011-11-17 00:12:10 +0000320#endif
321}
322
Nick Lewycky593eeb02013-03-09 01:33:12 +0000323void llvm_gcda_emit_function(uint32_t ident, const char *function_name,
Yuchen Wua5de3432013-12-04 06:00:04 +0000324 uint32_t func_checksum, uint8_t use_extra_checksum,
Yuchen Wuea7611c2013-11-20 04:14:48 +0000325 uint32_t cfg_checksum) {
Nick Lewycky593eeb02013-03-09 01:33:12 +0000326 uint32_t len = 2;
Bill Wendlingc0540862013-05-15 21:31:22 +0000327
Nick Lewycky593eeb02013-03-09 01:33:12 +0000328 if (use_extra_checksum)
329 len++;
Daniel Dunbarfb161142011-11-17 00:12:10 +0000330#ifdef DEBUG_GCDAPROFILING
Nick Lewycky1052c992013-02-28 07:00:13 +0000331 fprintf(stderr, "llvmgcda: function id=0x%08x name=%s\n", ident,
332 function_name ? function_name : "NULL");
Daniel Dunbarfb161142011-11-17 00:12:10 +0000333#endif
Bill Wendling74f987f2012-05-28 02:34:34 +0000334 if (!output_file) return;
Daniel Dunbarfb161142011-11-17 00:12:10 +0000335
Nick Lewycky1052c992013-02-28 07:00:13 +0000336 /* function tag */
Bill Wendlingc0540862013-05-15 21:31:22 +0000337 write_bytes("\0\0\0\1", 4);
Nick Lewycky1052c992013-02-28 07:00:13 +0000338 if (function_name)
339 len += 1 + length_of_string(function_name);
Bill Wendlingc0540862013-05-15 21:31:22 +0000340 write_32bit_value(len);
341 write_32bit_value(ident);
Yuchen Wua5de3432013-12-04 06:00:04 +0000342 write_32bit_value(func_checksum);
Nick Lewycky593eeb02013-03-09 01:33:12 +0000343 if (use_extra_checksum)
Yuchen Wuea7611c2013-11-20 04:14:48 +0000344 write_32bit_value(cfg_checksum);
Nick Lewycky1052c992013-02-28 07:00:13 +0000345 if (function_name)
346 write_string(function_name);
Daniel Dunbarfb161142011-11-17 00:12:10 +0000347}
348
349void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
350 uint32_t i;
Bill Wendling66429fe2012-09-14 18:55:32 +0000351 uint64_t *old_ctrs = NULL;
352 uint32_t val = 0;
Bill Wendling353fbd32013-05-23 07:18:59 +0000353 uint64_t save_cur_pos = cur_pos;
Bill Wendling66429fe2012-09-14 18:55:32 +0000354
355 if (!output_file) return;
356
Bill Wendlingc0540862013-05-15 21:31:22 +0000357 val = read_32bit_value();
Bill Wendling66429fe2012-09-14 18:55:32 +0000358
Bill Wendling66429fe2012-09-14 18:55:32 +0000359 if (val != (uint32_t)-1) {
360 /* There are counters present in the file. Merge them. */
Bill Wendling66429fe2012-09-14 18:55:32 +0000361 if (val != 0x01a10000) {
Justin Bogner8da47ac2014-08-18 20:47:32 +0000362 fprintf(stderr, "profiling: %s: cannot merge previous GCDA file: "
363 "corrupt arc tag (0x%08x)\n",
364 filename, val);
Bill Wendling66429fe2012-09-14 18:55:32 +0000365 return;
366 }
367
Bill Wendlingc0540862013-05-15 21:31:22 +0000368 val = read_32bit_value();
Bill Wendling66429fe2012-09-14 18:55:32 +0000369 if (val == (uint32_t)-1 || val / 2 != num_counters) {
Justin Bogner8da47ac2014-08-18 20:47:32 +0000370 fprintf(stderr, "profiling: %s: cannot merge previous GCDA file: "
371 "mismatched number of counters (%d)\n",
372 filename, val);
Bill Wendling66429fe2012-09-14 18:55:32 +0000373 return;
374 }
375
376 old_ctrs = malloc(sizeof(uint64_t) * num_counters);
Bill Wendlingc0540862013-05-15 21:31:22 +0000377 for (i = 0; i < num_counters; ++i)
378 old_ctrs[i] = read_64bit_value();
Bill Wendling66429fe2012-09-14 18:55:32 +0000379 }
380
Bill Wendling353fbd32013-05-23 07:18:59 +0000381 cur_pos = save_cur_pos;
382
Bill Wendling74f987f2012-05-28 02:34:34 +0000383 /* Counter #1 (arcs) tag */
Bill Wendlingc0540862013-05-15 21:31:22 +0000384 write_bytes("\0\0\xa1\1", 4);
385 write_32bit_value(num_counters * 2);
386 for (i = 0; i < num_counters; ++i) {
Bill Wendling9a9141a2013-04-22 03:36:22 +0000387 counters[i] += (old_ctrs ? old_ctrs[i] : 0);
Bill Wendlingc0540862013-05-15 21:31:22 +0000388 write_64bit_value(counters[i]);
Bill Wendling9a9141a2013-04-22 03:36:22 +0000389 }
Bill Wendling66429fe2012-09-14 18:55:32 +0000390
391 free(old_ctrs);
Daniel Dunbarfb161142011-11-17 00:12:10 +0000392
393#ifdef DEBUG_GCDAPROFILING
Bill Wendling66429fe2012-09-14 18:55:32 +0000394 fprintf(stderr, "llvmgcda: %u arcs\n", num_counters);
Bill Wendling74f987f2012-05-28 02:34:34 +0000395 for (i = 0; i < num_counters; ++i)
Bill Wendling66429fe2012-09-14 18:55:32 +0000396 fprintf(stderr, "llvmgcda: %llu\n", (unsigned long long)counters[i]);
Daniel Dunbarfb161142011-11-17 00:12:10 +0000397#endif
398}
399
Yuchen Wu61a1bfc2013-11-12 18:45:50 +0000400void llvm_gcda_summary_info() {
Matt Arsenaultd3262522013-12-10 19:05:43 +0000401 const uint32_t obj_summary_len = 9; /* Length for gcov compatibility. */
Yuchen Wu61a1bfc2013-11-12 18:45:50 +0000402 uint32_t i;
403 uint32_t runs = 1;
404 uint32_t val = 0;
405 uint64_t save_cur_pos = cur_pos;
406
407 if (!output_file) return;
408
409 val = read_32bit_value();
410
411 if (val != (uint32_t)-1) {
412 /* There are counters present in the file. Merge them. */
413 if (val != 0xa1000000) {
Justin Bogner8da47ac2014-08-18 20:47:32 +0000414 fprintf(stderr, "profiling: %s: cannot merge previous run count: "
415 "corrupt object tag (0x%08x)\n",
416 filename, val);
Yuchen Wu61a1bfc2013-11-12 18:45:50 +0000417 return;
418 }
419
Matt Arsenaultd3262522013-12-10 19:05:43 +0000420 val = read_32bit_value(); /* length */
Yuchen Wu61a1bfc2013-11-12 18:45:50 +0000421 if (val != obj_summary_len) {
Justin Bogner8da47ac2014-08-18 20:47:32 +0000422 fprintf(stderr, "profiling: %s: cannot merge previous run count: "
423 "mismatched object length (%d)\n",
424 filename, val);
Yuchen Wu61a1bfc2013-11-12 18:45:50 +0000425 return;
426 }
427
Matt Arsenaultd3262522013-12-10 19:05:43 +0000428 read_32bit_value(); /* checksum, unused */
429 read_32bit_value(); /* num, unused */
430 runs += read_32bit_value(); /* Add previous run count to new counter. */
Yuchen Wu61a1bfc2013-11-12 18:45:50 +0000431 }
432
433 cur_pos = save_cur_pos;
434
435 /* Object summary tag */
436 write_bytes("\0\0\0\xa1", 4);
437 write_32bit_value(obj_summary_len);
Matt Arsenaultd3262522013-12-10 19:05:43 +0000438 write_32bit_value(0); /* checksum, unused */
439 write_32bit_value(0); /* num, unused */
Yuchen Wu61a1bfc2013-11-12 18:45:50 +0000440 write_32bit_value(runs);
Matt Arsenaultd3262522013-12-10 19:05:43 +0000441 for (i = 3; i < obj_summary_len; ++i)
Yuchen Wu61a1bfc2013-11-12 18:45:50 +0000442 write_32bit_value(0);
443
444 /* Program summary tag */
Matt Arsenaultd3262522013-12-10 19:05:43 +0000445 write_bytes("\0\0\0\xa3", 4); /* tag indicates 1 program */
446 write_32bit_value(0); /* 0 length */
Yuchen Wu61a1bfc2013-11-12 18:45:50 +0000447
448#ifdef DEBUG_GCDAPROFILING
449 fprintf(stderr, "llvmgcda: %u runs\n", runs);
450#endif
451}
452
Daniel Dunbarfb161142011-11-17 00:12:10 +0000453void llvm_gcda_end_file() {
454 /* Write out EOF record. */
Chandler Carruthfa95cc92013-06-26 00:26:16 +0000455 if (output_file) {
456 write_bytes("\0\0\0\0\0\0\0\0", 8);
Bill Wendling353fbd32013-05-23 07:18:59 +0000457
Chandler Carruthfa95cc92013-06-26 00:26:16 +0000458 if (new_file) {
459 fwrite(write_buffer, cur_pos, 1, output_file);
460 free(write_buffer);
461 } else {
462 unmap_file();
463 }
464
Justin Bogner8e1a2a32015-04-03 18:55:44 +0000465 flock(fd, LOCK_UN);
Vedant Kumare55855f2016-03-05 20:10:25 +0000466 fclose(output_file);
Chandler Carruthfa95cc92013-06-26 00:26:16 +0000467 output_file = NULL;
468 write_buffer = NULL;
Bill Wendling353fbd32013-05-23 07:18:59 +0000469 }
Chandler Carruthfa95cc92013-06-26 00:26:16 +0000470 free(filename);
Daniel Dunbarfb161142011-11-17 00:12:10 +0000471
472#ifdef DEBUG_GCDAPROFILING
Bill Wendling66429fe2012-09-14 18:55:32 +0000473 fprintf(stderr, "llvmgcda: -----\n");
Daniel Dunbarfb161142011-11-17 00:12:10 +0000474#endif
475}
Bill Wendling2428f162013-03-18 22:59:47 +0000476
Bill Wendlinge6476592013-03-19 21:01:19 +0000477void llvm_register_writeout_function(writeout_fn fn) {
478 struct writeout_fn_node *new_node = malloc(sizeof(struct writeout_fn_node));
479 new_node->fn = fn;
480 new_node->next = NULL;
481
482 if (!writeout_fn_head) {
483 writeout_fn_head = writeout_fn_tail = new_node;
484 } else {
485 writeout_fn_tail->next = new_node;
486 writeout_fn_tail = new_node;
487 }
488}
489
Reid Kleckner5c10fa32016-02-11 00:22:43 +0000490void llvm_writeout_files(void) {
Bill Wendlinge6476592013-03-19 21:01:19 +0000491 struct writeout_fn_node *curr = writeout_fn_head;
492
493 while (curr) {
494 curr->fn();
495 curr = curr->next;
496 }
497}
498
Reid Kleckner5c10fa32016-02-11 00:22:43 +0000499void llvm_delete_writeout_function_list(void) {
Bill Wendlinge6476592013-03-19 21:01:19 +0000500 while (writeout_fn_head) {
501 struct writeout_fn_node *node = writeout_fn_head;
502 writeout_fn_head = writeout_fn_head->next;
503 free(node);
504 }
505
506 writeout_fn_head = writeout_fn_tail = NULL;
507}
508
Bill Wendling2428f162013-03-18 22:59:47 +0000509void llvm_register_flush_function(flush_fn fn) {
510 struct flush_fn_node *new_node = malloc(sizeof(struct flush_fn_node));
511 new_node->fn = fn;
512 new_node->next = NULL;
513
514 if (!flush_fn_head) {
515 flush_fn_head = flush_fn_tail = new_node;
516 } else {
517 flush_fn_tail->next = new_node;
518 flush_fn_tail = new_node;
519 }
520}
521
522void __gcov_flush() {
523 struct flush_fn_node *curr = flush_fn_head;
524
525 while (curr) {
526 curr->fn();
527 curr = curr->next;
528 }
529}
530
Reid Kleckner5c10fa32016-02-11 00:22:43 +0000531void llvm_delete_flush_function_list(void) {
Bill Wendling2428f162013-03-18 22:59:47 +0000532 while (flush_fn_head) {
533 struct flush_fn_node *node = flush_fn_head;
534 flush_fn_head = flush_fn_head->next;
535 free(node);
536 }
537
538 flush_fn_head = flush_fn_tail = NULL;
539}
Bill Wendling51a6ff52013-03-20 21:11:47 +0000540
541void llvm_gcov_init(writeout_fn wfn, flush_fn ffn) {
542 static int atexit_ran = 0;
543
544 if (wfn)
545 llvm_register_writeout_function(wfn);
546
547 if (ffn)
548 llvm_register_flush_function(ffn);
549
550 if (atexit_ran == 0) {
551 atexit_ran = 1;
552
553 /* Make sure we write out the data and delete the data structures. */
554 atexit(llvm_delete_flush_function_list);
555 atexit(llvm_delete_writeout_function_list);
556 atexit(llvm_writeout_files);
557 }
558}