blob: aec232856e74ef8d0f7d03365cc6faa1a7451db4 [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
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080023#include "InstrProfilingUtil.h"
24
Chandler Carruth254abfa2013-06-25 00:37:32 +000025#include <errno.h>
Bill Wendlingd06f2fc2013-05-23 07:18:59 +000026#include <fcntl.h>
Daniel Dunbar8c881192011-11-17 00:12:10 +000027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
Bill Wendlingd06f2fc2013-05-23 07:18:59 +000030#include <sys/mman.h>
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070031#include <sys/file.h>
Daniel Dunbar8c881192011-11-17 00:12:10 +000032
Stephen Hines2d1fdb22014-05-28 23:58:16 -070033#define I386_FREEBSD (defined(__FreeBSD__) && defined(__i386__))
34
Stephen Hines2d1fdb22014-05-28 23:58:16 -070035#if !defined(_MSC_VER) && !I386_FREEBSD
Daniel Dunbar8c881192011-11-17 00:12:10 +000036#include <stdint.h>
Stephen Hines2d1fdb22014-05-28 23:58:16 -070037#endif
38
39#if defined(_MSC_VER)
Daniel Dunbar8c881192011-11-17 00:12:10 +000040typedef unsigned int uint32_t;
Stephen Hines2d1fdb22014-05-28 23:58:16 -070041typedef unsigned long long uint64_t;
42#elif I386_FREEBSD
43/* System headers define 'size_t' incorrectly on x64 FreeBSD (prior to
44 * FreeBSD 10, r232261) when compiled in 32-bit mode.
45 */
46typedef unsigned char uint8_t;
47typedef unsigned int uint32_t;
48typedef unsigned long long uint64_t;
Daniel Dunbar8c881192011-11-17 00:12:10 +000049#endif
50
51/* #define DEBUG_GCDAPROFILING */
52
53/*
54 * --- GCOV file format I/O primitives ---
55 */
56
Bill Wendlinga5391832013-03-18 22:59:47 +000057/*
Chandler Carruth254abfa2013-06-25 00:37:32 +000058 * The current file name we're outputting. Used primarily for error logging.
59 */
60static char *filename = NULL;
61
62/*
Bill Wendlinga5391832013-03-18 22:59:47 +000063 * The current file we're outputting.
64 */
Daniel Dunbar8c881192011-11-17 00:12:10 +000065static FILE *output_file = NULL;
66
Bill Wendlinga5391832013-03-18 22:59:47 +000067/*
Bill Wendling2a46a602013-05-15 21:31:22 +000068 * Buffer that we write things into.
69 */
Bill Wendling2811a0c2013-05-23 18:18:31 +000070#define WRITE_BUFFER_SIZE (128 * 1024)
71static char *write_buffer = NULL;
Bill Wendlingd06f2fc2013-05-23 07:18:59 +000072static uint64_t cur_buffer_size = 0;
73static uint64_t cur_pos = 0;
74static uint64_t file_size = 0;
Bill Wendling2a46a602013-05-15 21:31:22 +000075static int new_file = 0;
Bill Wendlingd06f2fc2013-05-23 07:18:59 +000076static int fd = -1;
Bill Wendling2a46a602013-05-15 21:31:22 +000077
78/*
Bill Wendlingc5333042013-03-19 21:01:19 +000079 * A list of functions to write out the data.
80 */
81typedef void (*writeout_fn)();
82
83struct writeout_fn_node {
84 writeout_fn fn;
85 struct writeout_fn_node *next;
86};
87
Bill Wendling2a46a602013-05-15 21:31:22 +000088static struct writeout_fn_node *writeout_fn_head = NULL;
89static struct writeout_fn_node *writeout_fn_tail = NULL;
Bill Wendlingc5333042013-03-19 21:01:19 +000090
91/*
Bill Wendlinga5391832013-03-18 22:59:47 +000092 * A list of flush functions that our __gcov_flush() function should call.
93 */
94typedef void (*flush_fn)();
95
96struct flush_fn_node {
97 flush_fn fn;
98 struct flush_fn_node *next;
99};
100
Bill Wendling2a46a602013-05-15 21:31:22 +0000101static struct flush_fn_node *flush_fn_head = NULL;
102static struct flush_fn_node *flush_fn_tail = NULL;
Bill Wendlinga5391832013-03-18 22:59:47 +0000103
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000104static void resize_write_buffer(uint64_t size) {
105 if (!new_file) return;
106 size += cur_pos;
107 if (size <= cur_buffer_size) return;
Bill Wendling2811a0c2013-05-23 18:18:31 +0000108 size = (size - 1) / WRITE_BUFFER_SIZE + 1;
109 size *= WRITE_BUFFER_SIZE;
110 write_buffer = realloc(write_buffer, size);
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000111 cur_buffer_size = size;
Daniel Dunbar8c881192011-11-17 00:12:10 +0000112}
113
Bill Wendling2a46a602013-05-15 21:31:22 +0000114static void write_bytes(const char *s, size_t len) {
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000115 resize_write_buffer(len);
Bill Wendling2811a0c2013-05-23 18:18:31 +0000116 memcpy(&write_buffer[cur_pos], s, len);
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000117 cur_pos += len;
Bill Wendling2a46a602013-05-15 21:31:22 +0000118}
119
120static void write_32bit_value(uint32_t i) {
121 write_bytes((char*)&i, 4);
122}
123
124static void write_64bit_value(uint64_t i) {
125 write_bytes((char*)&i, 8);
Daniel Dunbar8c881192011-11-17 00:12:10 +0000126}
127
128static uint32_t length_of_string(const char *s) {
129 return (strlen(s) / 4) + 1;
130}
131
132static void write_string(const char *s) {
133 uint32_t len = length_of_string(s);
Bill Wendling2a46a602013-05-15 21:31:22 +0000134 write_32bit_value(len);
135 write_bytes(s, strlen(s));
136 write_bytes("\0\0\0\0", 4 - (strlen(s) % 4));
Daniel Dunbar8c881192011-11-17 00:12:10 +0000137}
138
Bill Wendling2a46a602013-05-15 21:31:22 +0000139static uint32_t read_32bit_value() {
140 uint32_t val;
Bill Wendling843f3592012-09-14 18:55:32 +0000141
Bill Wendling2a46a602013-05-15 21:31:22 +0000142 if (new_file)
Bill Wendling843f3592012-09-14 18:55:32 +0000143 return (uint32_t)-1;
144
Bill Wendling2811a0c2013-05-23 18:18:31 +0000145 val = *(uint32_t*)&write_buffer[cur_pos];
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000146 cur_pos += 4;
Bill Wendling2a46a602013-05-15 21:31:22 +0000147 return val;
Bill Wendling843f3592012-09-14 18:55:32 +0000148}
149
Bill Wendling2a46a602013-05-15 21:31:22 +0000150static uint64_t read_64bit_value() {
151 uint64_t val;
Bill Wendling843f3592012-09-14 18:55:32 +0000152
Bill Wendling2a46a602013-05-15 21:31:22 +0000153 if (new_file)
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000154 return (uint64_t)-1;
Bill Wendling2a46a602013-05-15 21:31:22 +0000155
Bill Wendling2811a0c2013-05-23 18:18:31 +0000156 val = *(uint64_t*)&write_buffer[cur_pos];
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000157 cur_pos += 8;
Bill Wendling2a46a602013-05-15 21:31:22 +0000158 return val;
Bill Wendling843f3592012-09-14 18:55:32 +0000159}
160
Daniel Dunbar8c881192011-11-17 00:12:10 +0000161static char *mangle_filename(const char *orig_filename) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700162 char *new_filename;
163 size_t filename_len, prefix_len;
164 int prefix_strip;
Bill Wendlingec63f452012-05-28 10:09:01 +0000165 int level = 0;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700166 const char *fname, *ptr;
Bill Wendling906d5a52012-05-28 09:41:48 +0000167 const char *prefix = getenv("GCOV_PREFIX");
Bill Wendling2a46a602013-05-15 21:31:22 +0000168 const char *prefix_strip_str = getenv("GCOV_PREFIX_STRIP");
Daniel Dunbar8c881192011-11-17 00:12:10 +0000169
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700170 if (prefix == NULL || prefix[0] == '\0')
Daniel Dunbar8c881192011-11-17 00:12:10 +0000171 return strdup(orig_filename);
172
Bill Wendling2a46a602013-05-15 21:31:22 +0000173 if (prefix_strip_str) {
174 prefix_strip = atoi(prefix_strip_str);
Bill Wendlingec63f452012-05-28 10:09:01 +0000175
176 /* Negative GCOV_PREFIX_STRIP values are ignored */
177 if (prefix_strip < 0)
178 prefix_strip = 0;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700179 } else {
180 prefix_strip = 0;
Bill Wendlingec63f452012-05-28 10:09:01 +0000181 }
182
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700183 fname = orig_filename;
184 for (level = 0, ptr = fname + 1; level < prefix_strip; ++ptr) {
185 if (*ptr == '\0')
186 break;
187 if (*ptr != '/')
188 continue;
Bill Wendlingec63f452012-05-28 10:09:01 +0000189 fname = ptr;
190 ++level;
191 }
192
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700193 filename_len = strlen(fname);
194 prefix_len = strlen(prefix);
195 new_filename = malloc(prefix_len + 1 + filename_len + 1);
196 memcpy(new_filename, prefix, prefix_len);
Daniel Dunbar8c881192011-11-17 00:12:10 +0000197
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700198 if (prefix[prefix_len - 1] != '/')
199 new_filename[prefix_len++] = '/';
200 memcpy(new_filename + prefix_len, fname, filename_len + 1);
201
202 return new_filename;
Daniel Dunbar8c881192011-11-17 00:12:10 +0000203}
204
Chandler Carruth254abfa2013-06-25 00:37:32 +0000205static int map_file() {
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000206 fseek(output_file, 0L, SEEK_END);
207 file_size = ftell(output_file);
208
Bill Wendling08a0e082013-09-11 19:35:32 +0000209 /* A size of 0 is invalid to `mmap'. Return a fail here, but don't issue an
Bill Wendling7ab94ea2013-09-09 22:25:46 +0000210 * error message because it should "just work" for the user. */
211 if (file_size == 0)
212 return -1;
213
Bill Wendling2811a0c2013-05-23 18:18:31 +0000214 write_buffer = mmap(0, file_size, PROT_READ | PROT_WRITE,
215 MAP_FILE | MAP_SHARED, fd, 0);
Chandler Carruth254abfa2013-06-25 00:37:32 +0000216 if (write_buffer == (void *)-1) {
217 int errnum = errno;
218 fprintf(stderr, "profiling: %s: cannot map: %s\n", filename,
219 strerror(errnum));
220 return -1;
221 }
222 return 0;
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000223}
224
225static void unmap_file() {
Chandler Carruth254abfa2013-06-25 00:37:32 +0000226 if (msync(write_buffer, file_size, MS_SYNC) == -1) {
227 int errnum = errno;
228 fprintf(stderr, "profiling: %s: cannot msync: %s\n", filename,
229 strerror(errnum));
230 }
231
232 /* We explicitly ignore errors from unmapping because at this point the data
233 * is written and we don't care.
234 */
235 (void)munmap(write_buffer, file_size);
Bill Wendling2811a0c2013-05-23 18:18:31 +0000236 write_buffer = NULL;
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000237 file_size = 0;
238}
239
Daniel Dunbar8c881192011-11-17 00:12:10 +0000240/*
241 * --- LLVM line counter API ---
242 */
243
244/* A file in this case is a translation unit. Each .o file built with line
245 * profiling enabled will emit to a different file. Only one file may be
246 * started at a time.
247 */
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700248void llvm_gcda_start_file(const char *orig_filename, const char version[4],
249 uint32_t checksum) {
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000250 const char *mode = "r+b";
Chandler Carruth254abfa2013-06-25 00:37:32 +0000251 filename = mangle_filename(orig_filename);
Bill Wendling843f3592012-09-14 18:55:32 +0000252
253 /* Try just opening the file. */
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000254 new_file = 0;
255 fd = open(filename, O_RDWR);
Bill Wendling5a240c52012-05-28 02:34:34 +0000256
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000257 if (fd == -1) {
Bill Wendling843f3592012-09-14 18:55:32 +0000258 /* Try opening the file, creating it if necessary. */
Bill Wendling2a46a602013-05-15 21:31:22 +0000259 new_file = 1;
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000260 mode = "w+b";
Bill Wendling2811a0c2013-05-23 18:18:31 +0000261 fd = open(filename, O_RDWR | O_CREAT, 0644);
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000262 if (fd == -1) {
Bill Wendling843f3592012-09-14 18:55:32 +0000263 /* Try creating the directories first then opening the file. */
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800264 __llvm_profile_recursive_mkdir(filename);
Bill Wendling2811a0c2013-05-23 18:18:31 +0000265 fd = open(filename, O_RDWR | O_CREAT, 0644);
Chandler Carruth254abfa2013-06-25 00:37:32 +0000266 if (fd == -1) {
Bill Wendling843f3592012-09-14 18:55:32 +0000267 /* Bah! It's hopeless. */
Chandler Carruth254abfa2013-06-25 00:37:32 +0000268 int errnum = errno;
269 fprintf(stderr, "profiling: %s: cannot open: %s\n", filename,
270 strerror(errnum));
Bill Wendling843f3592012-09-14 18:55:32 +0000271 return;
272 }
Bill Wendling5a240c52012-05-28 02:34:34 +0000273 }
274 }
Daniel Dunbar8c881192011-11-17 00:12:10 +0000275
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700276 /* Try to flock the file to serialize concurrent processes writing out to the
277 * same GCDA. This can fail if the filesystem doesn't support it, but in that
278 * case we'll just carry on with the old racy behaviour and hope for the best.
279 */
280 flock(fd, LOCK_EX);
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000281 output_file = fdopen(fd, mode);
Bill Wendling2a46a602013-05-15 21:31:22 +0000282
283 /* Initialize the write buffer. */
Bill Wendling2811a0c2013-05-23 18:18:31 +0000284 write_buffer = NULL;
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000285 cur_buffer_size = 0;
286 cur_pos = 0;
287
288 if (new_file) {
Bill Wendling2811a0c2013-05-23 18:18:31 +0000289 resize_write_buffer(WRITE_BUFFER_SIZE);
290 memset(write_buffer, 0, WRITE_BUFFER_SIZE);
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000291 } else {
Chandler Carruth254abfa2013-06-25 00:37:32 +0000292 if (map_file() == -1) {
293 /* mmap failed, try to recover by clobbering */
294 new_file = 1;
295 write_buffer = NULL;
296 cur_buffer_size = 0;
297 resize_write_buffer(WRITE_BUFFER_SIZE);
298 memset(write_buffer, 0, WRITE_BUFFER_SIZE);
299 }
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000300 }
Bill Wendling2a46a602013-05-15 21:31:22 +0000301
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700302 /* gcda file, version, stamp checksum. */
Bill Wendling2a46a602013-05-15 21:31:22 +0000303 write_bytes("adcg", 4);
304 write_bytes(version, 4);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700305 write_32bit_value(checksum);
Bill Wendling2a46a602013-05-15 21:31:22 +0000306
Bill Wendling843f3592012-09-14 18:55:32 +0000307#ifdef DEBUG_GCDAPROFILING
308 fprintf(stderr, "llvmgcda: [%s]\n", orig_filename);
309#endif
Daniel Dunbar8c881192011-11-17 00:12:10 +0000310}
311
312/* Given an array of pointers to counters (counters), increment the n-th one,
313 * where we're also given a pointer to n (predecessor).
314 */
315void llvm_gcda_increment_indirect_counter(uint32_t *predecessor,
316 uint64_t **counters) {
317 uint64_t *counter;
318 uint32_t pred;
319
320 pred = *predecessor;
321 if (pred == 0xffffffff)
322 return;
323 counter = counters[pred];
324
325 /* Don't crash if the pred# is out of sync. This can happen due to threads,
326 or because of a TODO in GCOVProfiling.cpp buildEdgeLookupTable(). */
327 if (counter)
328 ++*counter;
329#ifdef DEBUG_GCDAPROFILING
330 else
Bill Wendlingde47cb82012-05-28 02:50:53 +0000331 fprintf(stderr,
Bill Wendling843f3592012-09-14 18:55:32 +0000332 "llvmgcda: increment_indirect_counter counters=%08llx, pred=%u\n",
333 *counter, *predecessor);
Daniel Dunbar8c881192011-11-17 00:12:10 +0000334#endif
335}
336
Nick Lewycky07020822013-03-09 01:33:12 +0000337void llvm_gcda_emit_function(uint32_t ident, const char *function_name,
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700338 uint32_t func_checksum, uint8_t use_extra_checksum,
339 uint32_t cfg_checksum) {
Nick Lewycky07020822013-03-09 01:33:12 +0000340 uint32_t len = 2;
Bill Wendling2a46a602013-05-15 21:31:22 +0000341
Nick Lewycky07020822013-03-09 01:33:12 +0000342 if (use_extra_checksum)
343 len++;
Daniel Dunbar8c881192011-11-17 00:12:10 +0000344#ifdef DEBUG_GCDAPROFILING
Nick Lewycky58c400c2013-02-28 07:00:13 +0000345 fprintf(stderr, "llvmgcda: function id=0x%08x name=%s\n", ident,
346 function_name ? function_name : "NULL");
Daniel Dunbar8c881192011-11-17 00:12:10 +0000347#endif
Bill Wendling5a240c52012-05-28 02:34:34 +0000348 if (!output_file) return;
Daniel Dunbar8c881192011-11-17 00:12:10 +0000349
Nick Lewycky58c400c2013-02-28 07:00:13 +0000350 /* function tag */
Bill Wendling2a46a602013-05-15 21:31:22 +0000351 write_bytes("\0\0\0\1", 4);
Nick Lewycky58c400c2013-02-28 07:00:13 +0000352 if (function_name)
353 len += 1 + length_of_string(function_name);
Bill Wendling2a46a602013-05-15 21:31:22 +0000354 write_32bit_value(len);
355 write_32bit_value(ident);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700356 write_32bit_value(func_checksum);
Nick Lewycky07020822013-03-09 01:33:12 +0000357 if (use_extra_checksum)
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700358 write_32bit_value(cfg_checksum);
Nick Lewycky58c400c2013-02-28 07:00:13 +0000359 if (function_name)
360 write_string(function_name);
Daniel Dunbar8c881192011-11-17 00:12:10 +0000361}
362
363void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
364 uint32_t i;
Bill Wendling843f3592012-09-14 18:55:32 +0000365 uint64_t *old_ctrs = NULL;
366 uint32_t val = 0;
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000367 uint64_t save_cur_pos = cur_pos;
Bill Wendling843f3592012-09-14 18:55:32 +0000368
369 if (!output_file) return;
370
Bill Wendling2a46a602013-05-15 21:31:22 +0000371 val = read_32bit_value();
Bill Wendling843f3592012-09-14 18:55:32 +0000372
Bill Wendling843f3592012-09-14 18:55:32 +0000373 if (val != (uint32_t)-1) {
374 /* There are counters present in the file. Merge them. */
Bill Wendling843f3592012-09-14 18:55:32 +0000375 if (val != 0x01a10000) {
Stephen Hines6d186232014-11-26 17:56:19 -0800376 fprintf(stderr, "profiling: %s: cannot merge previous GCDA file: "
377 "corrupt arc tag (0x%08x)\n",
378 filename, val);
Bill Wendling843f3592012-09-14 18:55:32 +0000379 return;
380 }
381
Bill Wendling2a46a602013-05-15 21:31:22 +0000382 val = read_32bit_value();
Bill Wendling843f3592012-09-14 18:55:32 +0000383 if (val == (uint32_t)-1 || val / 2 != num_counters) {
Stephen Hines6d186232014-11-26 17:56:19 -0800384 fprintf(stderr, "profiling: %s: cannot merge previous GCDA file: "
385 "mismatched number of counters (%d)\n",
386 filename, val);
Bill Wendling843f3592012-09-14 18:55:32 +0000387 return;
388 }
389
390 old_ctrs = malloc(sizeof(uint64_t) * num_counters);
Bill Wendling2a46a602013-05-15 21:31:22 +0000391 for (i = 0; i < num_counters; ++i)
392 old_ctrs[i] = read_64bit_value();
Bill Wendling843f3592012-09-14 18:55:32 +0000393 }
394
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000395 cur_pos = save_cur_pos;
396
Bill Wendling5a240c52012-05-28 02:34:34 +0000397 /* Counter #1 (arcs) tag */
Bill Wendling2a46a602013-05-15 21:31:22 +0000398 write_bytes("\0\0\xa1\1", 4);
399 write_32bit_value(num_counters * 2);
400 for (i = 0; i < num_counters; ++i) {
Bill Wendling4aa08fa2013-04-22 03:36:22 +0000401 counters[i] += (old_ctrs ? old_ctrs[i] : 0);
Bill Wendling2a46a602013-05-15 21:31:22 +0000402 write_64bit_value(counters[i]);
Bill Wendling4aa08fa2013-04-22 03:36:22 +0000403 }
Bill Wendling843f3592012-09-14 18:55:32 +0000404
405 free(old_ctrs);
Daniel Dunbar8c881192011-11-17 00:12:10 +0000406
407#ifdef DEBUG_GCDAPROFILING
Bill Wendling843f3592012-09-14 18:55:32 +0000408 fprintf(stderr, "llvmgcda: %u arcs\n", num_counters);
Bill Wendling5a240c52012-05-28 02:34:34 +0000409 for (i = 0; i < num_counters; ++i)
Bill Wendling843f3592012-09-14 18:55:32 +0000410 fprintf(stderr, "llvmgcda: %llu\n", (unsigned long long)counters[i]);
Daniel Dunbar8c881192011-11-17 00:12:10 +0000411#endif
412}
413
Yuchen Wu7bb5d492013-11-12 18:45:50 +0000414void llvm_gcda_summary_info() {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700415 const uint32_t obj_summary_len = 9; /* Length for gcov compatibility. */
Yuchen Wu7bb5d492013-11-12 18:45:50 +0000416 uint32_t i;
417 uint32_t runs = 1;
418 uint32_t val = 0;
419 uint64_t save_cur_pos = cur_pos;
420
421 if (!output_file) return;
422
423 val = read_32bit_value();
424
425 if (val != (uint32_t)-1) {
426 /* There are counters present in the file. Merge them. */
427 if (val != 0xa1000000) {
Stephen Hines6d186232014-11-26 17:56:19 -0800428 fprintf(stderr, "profiling: %s: cannot merge previous run count: "
429 "corrupt object tag (0x%08x)\n",
430 filename, val);
Yuchen Wu7bb5d492013-11-12 18:45:50 +0000431 return;
432 }
433
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700434 val = read_32bit_value(); /* length */
Yuchen Wu7bb5d492013-11-12 18:45:50 +0000435 if (val != obj_summary_len) {
Stephen Hines6d186232014-11-26 17:56:19 -0800436 fprintf(stderr, "profiling: %s: cannot merge previous run count: "
437 "mismatched object length (%d)\n",
438 filename, val);
Yuchen Wu7bb5d492013-11-12 18:45:50 +0000439 return;
440 }
441
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700442 read_32bit_value(); /* checksum, unused */
443 read_32bit_value(); /* num, unused */
444 runs += read_32bit_value(); /* Add previous run count to new counter. */
Yuchen Wu7bb5d492013-11-12 18:45:50 +0000445 }
446
447 cur_pos = save_cur_pos;
448
449 /* Object summary tag */
450 write_bytes("\0\0\0\xa1", 4);
451 write_32bit_value(obj_summary_len);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700452 write_32bit_value(0); /* checksum, unused */
453 write_32bit_value(0); /* num, unused */
Yuchen Wu7bb5d492013-11-12 18:45:50 +0000454 write_32bit_value(runs);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700455 for (i = 3; i < obj_summary_len; ++i)
Yuchen Wu7bb5d492013-11-12 18:45:50 +0000456 write_32bit_value(0);
457
458 /* Program summary tag */
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700459 write_bytes("\0\0\0\xa3", 4); /* tag indicates 1 program */
460 write_32bit_value(0); /* 0 length */
Yuchen Wu7bb5d492013-11-12 18:45:50 +0000461
462#ifdef DEBUG_GCDAPROFILING
463 fprintf(stderr, "llvmgcda: %u runs\n", runs);
464#endif
465}
466
Daniel Dunbar8c881192011-11-17 00:12:10 +0000467void llvm_gcda_end_file() {
468 /* Write out EOF record. */
Chandler Carruthcf5fb622013-06-26 00:26:16 +0000469 if (output_file) {
470 write_bytes("\0\0\0\0\0\0\0\0", 8);
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000471
Chandler Carruthcf5fb622013-06-26 00:26:16 +0000472 if (new_file) {
473 fwrite(write_buffer, cur_pos, 1, output_file);
474 free(write_buffer);
475 } else {
476 unmap_file();
477 }
478
479 fclose(output_file);
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700480 flock(fd, LOCK_UN);
Chandler Carruthcf5fb622013-06-26 00:26:16 +0000481 output_file = NULL;
482 write_buffer = NULL;
Bill Wendlingd06f2fc2013-05-23 07:18:59 +0000483 }
Chandler Carruthcf5fb622013-06-26 00:26:16 +0000484 free(filename);
Daniel Dunbar8c881192011-11-17 00:12:10 +0000485
486#ifdef DEBUG_GCDAPROFILING
Bill Wendling843f3592012-09-14 18:55:32 +0000487 fprintf(stderr, "llvmgcda: -----\n");
Daniel Dunbar8c881192011-11-17 00:12:10 +0000488#endif
489}
Bill Wendlinga5391832013-03-18 22:59:47 +0000490
Bill Wendlingc5333042013-03-19 21:01:19 +0000491void llvm_register_writeout_function(writeout_fn fn) {
492 struct writeout_fn_node *new_node = malloc(sizeof(struct writeout_fn_node));
493 new_node->fn = fn;
494 new_node->next = NULL;
495
496 if (!writeout_fn_head) {
497 writeout_fn_head = writeout_fn_tail = new_node;
498 } else {
499 writeout_fn_tail->next = new_node;
500 writeout_fn_tail = new_node;
501 }
502}
503
Bill Wendling84b46d32013-03-20 21:11:47 +0000504void llvm_writeout_files() {
Bill Wendlingc5333042013-03-19 21:01:19 +0000505 struct writeout_fn_node *curr = writeout_fn_head;
506
507 while (curr) {
508 curr->fn();
509 curr = curr->next;
510 }
511}
512
513void llvm_delete_writeout_function_list() {
514 while (writeout_fn_head) {
515 struct writeout_fn_node *node = writeout_fn_head;
516 writeout_fn_head = writeout_fn_head->next;
517 free(node);
518 }
519
520 writeout_fn_head = writeout_fn_tail = NULL;
521}
522
Bill Wendlinga5391832013-03-18 22:59:47 +0000523void llvm_register_flush_function(flush_fn fn) {
524 struct flush_fn_node *new_node = malloc(sizeof(struct flush_fn_node));
525 new_node->fn = fn;
526 new_node->next = NULL;
527
528 if (!flush_fn_head) {
529 flush_fn_head = flush_fn_tail = new_node;
530 } else {
531 flush_fn_tail->next = new_node;
532 flush_fn_tail = new_node;
533 }
534}
535
536void __gcov_flush() {
537 struct flush_fn_node *curr = flush_fn_head;
538
539 while (curr) {
540 curr->fn();
541 curr = curr->next;
542 }
543}
544
545void llvm_delete_flush_function_list() {
546 while (flush_fn_head) {
547 struct flush_fn_node *node = flush_fn_head;
548 flush_fn_head = flush_fn_head->next;
549 free(node);
550 }
551
552 flush_fn_head = flush_fn_tail = NULL;
553}
Bill Wendling84b46d32013-03-20 21:11:47 +0000554
555void llvm_gcov_init(writeout_fn wfn, flush_fn ffn) {
556 static int atexit_ran = 0;
557
558 if (wfn)
559 llvm_register_writeout_function(wfn);
560
561 if (ffn)
562 llvm_register_flush_function(ffn);
563
564 if (atexit_ran == 0) {
565 atexit_ran = 1;
566
567 /* Make sure we write out the data and delete the data structures. */
568 atexit(llvm_delete_flush_function_list);
569 atexit(llvm_delete_writeout_function_list);
570 atexit(llvm_writeout_files);
571 }
572}