1/*===- GCDAProfiling.c - Support library for GCDA file emission -----------===*\
2|*
3|* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4|* See https://llvm.org/LICENSE.txt for license information.
5|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6|*
7|*===----------------------------------------------------------------------===*|
8|*
9|* This file implements the call back routines for the gcov profiling
10|* instrumentation pass. Link against this library when running code through
11|* the -insert-gcov-profiling LLVM pass.
12|*
13|* We emit files in a corrupt version of GCOV's "gcda" file format. These files
14|* are only close enough that LCOV will happily parse them. Anything that lcov
15|* ignores is missing.
16|*
17|* TODO: gcov is multi-process safe by having each exit open the existing file
18|* and append to it. We'd like to achieve that and be thread-safe too.
19|*
20\*===----------------------------------------------------------------------===*/
21
22#if !defined(__Fuchsia__)
23
24#if defined(__linux__)
25// For fdopen()
26#define _DEFAULT_SOURCE
27#endif
28
29#include <errno.h>
30#include <fcntl.h>
31#include <stdint.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35
36#if defined(_WIN32)
37#define WIN32_LEAN_AND_MEAN
38#include <windows.h>
39#include "WindowsMMap.h"
40#else
41#include <sys/file.h>
42#include <sys/mman.h>
43#include <sys/types.h>
44#include <unistd.h>
45#endif
46
47#include "InstrProfiling.h"
48#include "InstrProfilingUtil.h"
49
50/* #define DEBUG_GCDAPROFILING */
51
52enum {
53 GCOV_DATA_MAGIC = 0x67636461, // "gcda"
54
55 GCOV_TAG_FUNCTION = 0x01000000,
56 GCOV_TAG_COUNTER_ARCS = 0x01a10000,
57 // GCOV_TAG_OBJECT_SUMMARY superseded GCOV_TAG_PROGRAM_SUMMARY in GCC 9.
58 GCOV_TAG_OBJECT_SUMMARY = 0xa1000000,
59 GCOV_TAG_PROGRAM_SUMMARY = 0xa3000000,
60};
61
62/*
63 * --- GCOV file format I/O primitives ---
64 */
65
66/*
67 * The current file name we're outputting. Used primarily for error logging.
68 */
69static char *filename = NULL;
70
71/*
72 * The current file we're outputting.
73 */
74static FILE *output_file = NULL;
75
76/*
77 * Buffer that we write things into.
78 */
79#define WRITE_BUFFER_SIZE (128 * 1024)
80static unsigned char *write_buffer = NULL;
81static uint64_t cur_buffer_size = 0;
82static uint64_t cur_pos = 0;
83static uint64_t file_size = 0;
84static int new_file = 0;
85static int gcov_version;
86#if defined(_WIN32)
87static HANDLE mmap_handle = NULL;
88#endif
89static int fd = -1;
90
91typedef void (*fn_ptr)(void);
92
93typedef void* dynamic_object_id;
94// The address of this variable identifies a given dynamic object.
95static dynamic_object_id current_id;
96#define CURRENT_ID (&current_id)
97
98struct fn_node {
99 dynamic_object_id id;
100 fn_ptr fn;
101 struct fn_node* next;
102};
103
104struct fn_list {
105 struct fn_node *head, *tail;
106};
107
108/*
109 * A list of functions to write out the data, shared between all dynamic objects.
110 */
111struct fn_list writeout_fn_list;
112
113/*
114 * A list of reset functions, shared between all dynamic objects.
115 */
116struct fn_list reset_fn_list;
117
118static void fn_list_insert(struct fn_list* list, fn_ptr fn) {
119 struct fn_node* new_node = malloc(size: sizeof(struct fn_node));
120 new_node->fn = fn;
121 new_node->next = NULL;
122 new_node->id = CURRENT_ID;
123
124 if (!list->head) {
125 list->head = list->tail = new_node;
126 } else {
127 list->tail->next = new_node;
128 list->tail = new_node;
129 }
130}
131
132static void fn_list_remove(struct fn_list* list) {
133 struct fn_node* curr = list->head;
134 struct fn_node* prev = NULL;
135 struct fn_node* next = NULL;
136
137 while (curr) {
138 next = curr->next;
139
140 if (curr->id == CURRENT_ID) {
141 if (curr == list->head) {
142 list->head = next;
143 }
144
145 if (curr == list->tail) {
146 list->tail = prev;
147 }
148
149 if (prev) {
150 prev->next = next;
151 }
152
153 free(ptr: curr);
154 } else {
155 prev = curr;
156 }
157
158 curr = next;
159 }
160}
161
162static void resize_write_buffer(uint64_t size) {
163 if (!new_file) return;
164 size += cur_pos;
165 if (size <= cur_buffer_size) return;
166 size = (size - 1) / WRITE_BUFFER_SIZE + 1;
167 size *= WRITE_BUFFER_SIZE;
168 write_buffer = realloc(ptr: write_buffer, size: size);
169 cur_buffer_size = size;
170}
171
172static void write_bytes(const char *s, size_t len) {
173 resize_write_buffer(size: len);
174 memcpy(dest: &write_buffer[cur_pos], src: s, n: len);
175 cur_pos += len;
176}
177
178static void write_32bit_value(uint32_t i) {
179 write_bytes(s: (char*)&i, len: 4);
180}
181
182static void write_64bit_value(uint64_t i) {
183 // GCOV uses a lo-/hi-word format even on big-endian systems.
184 // See also GCOVBuffer::readInt64 in LLVM.
185 uint32_t lo = (uint32_t) i;
186 uint32_t hi = (uint32_t) (i >> 32);
187 write_32bit_value(i: lo);
188 write_32bit_value(i: hi);
189}
190
191static uint32_t read_32bit_value(void) {
192 uint32_t val;
193
194 if (new_file)
195 return (uint32_t)-1;
196
197 val = *(uint32_t*)&write_buffer[cur_pos];
198 cur_pos += 4;
199 return val;
200}
201
202static uint64_t read_64bit_value(void) {
203 // GCOV uses a lo-/hi-word format even on big-endian systems.
204 // See also GCOVBuffer::readInt64 in LLVM.
205 uint32_t lo = read_32bit_value();
206 uint32_t hi = read_32bit_value();
207 return ((uint64_t)hi << 32) | ((uint64_t)lo);
208}
209
210static char *mangle_filename(const char *orig_filename) {
211 char *new_filename;
212 size_t prefix_len;
213 int prefix_strip;
214 const char *prefix = lprofGetPathPrefix(PrefixStrip: &prefix_strip, PrefixLen: &prefix_len);
215
216 if (prefix == NULL)
217 return strdup(s: orig_filename);
218
219 new_filename = malloc(size: prefix_len + 1 + strlen(s: orig_filename) + 1);
220 lprofApplyPathPrefix(Dest: new_filename, PathStr: orig_filename, Prefix: prefix, PrefixLen: prefix_len,
221 PrefixStrip: prefix_strip);
222
223 return new_filename;
224}
225
226static int map_file(void) {
227 fseek(stream: output_file, off: 0L, SEEK_END);
228 file_size = ftell(stream: output_file);
229
230 /* A size of 0 means the file has been created just now (possibly by another
231 * process in lock-after-open race condition). No need to mmap. */
232 if (file_size == 0)
233 return -1;
234
235#if defined(_WIN32)
236 HANDLE mmap_fd;
237 if (fd == -1)
238 mmap_fd = INVALID_HANDLE_VALUE;
239 else
240 mmap_fd = (HANDLE)_get_osfhandle(fd);
241
242 mmap_handle = CreateFileMapping(mmap_fd, NULL, PAGE_READWRITE, DWORD_HI(file_size), DWORD_LO(file_size), NULL);
243 if (mmap_handle == NULL) {
244 fprintf(stderr, "profiling: %s: cannot create file mapping: %lu\n",
245 filename, GetLastError());
246 return -1;
247 }
248
249 write_buffer = MapViewOfFile(mmap_handle, FILE_MAP_WRITE, 0, 0, file_size);
250 if (write_buffer == NULL) {
251 fprintf(stderr, "profiling: %s: cannot map: %lu\n", filename,
252 GetLastError());
253 CloseHandle(mmap_handle);
254 return -1;
255 }
256#else
257 write_buffer = mmap(addr: 0, len: file_size, PROT_READ | PROT_WRITE,
258 MAP_FILE | MAP_SHARED, fd: fd, offset: 0);
259 if (write_buffer == (void *)-1) {
260 int errnum = errno;
261 fprintf(stderr, format: "profiling: %s: cannot map: %s\n", filename,
262 strerror(errnum: errnum));
263 return -1;
264 }
265#endif
266
267 return 0;
268}
269
270static void unmap_file(void) {
271#if defined(_WIN32)
272 if (!UnmapViewOfFile(write_buffer)) {
273 fprintf(stderr, "profiling: %s: cannot unmap mapped view: %lu\n", filename,
274 GetLastError());
275 }
276
277 if (!CloseHandle(mmap_handle)) {
278 fprintf(stderr, "profiling: %s: cannot close file mapping handle: %lu\n",
279 filename, GetLastError());
280 }
281
282 mmap_handle = NULL;
283#else
284 if (munmap(addr: write_buffer, len: file_size) == -1) {
285 int errnum = errno;
286 fprintf(stderr, format: "profiling: %s: cannot munmap: %s\n", filename,
287 strerror(errnum: errnum));
288 }
289#endif
290
291 write_buffer = NULL;
292 file_size = 0;
293}
294
295/*
296 * --- LLVM line counter API ---
297 */
298
299/* A file in this case is a translation unit. Each .o file built with line
300 * profiling enabled will emit to a different file. Only one file may be
301 * started at a time.
302 */
303COMPILER_RT_VISIBILITY
304void llvm_gcda_start_file(const char *orig_filename, uint32_t version,
305 uint32_t checksum) {
306 const char *mode = "r+b";
307 filename = mangle_filename(orig_filename);
308
309 /* Try just opening the file. */
310 fd = open(file: filename, O_RDWR | O_BINARY);
311
312 if (fd == -1) {
313 /* Try creating the file. */
314 fd = open(file: filename, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0644);
315 if (fd != -1) {
316 mode = "w+b";
317 } else {
318 /* Try creating the directories first then opening the file. */
319 __llvm_profile_recursive_mkdir(Pathname: filename);
320 fd = open(file: filename, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0644);
321 if (fd != -1) {
322 mode = "w+b";
323 } else {
324 /* Another process may have created the file just now.
325 * Try opening it without O_CREAT and O_EXCL. */
326 fd = open(file: filename, O_RDWR | O_BINARY);
327 if (fd == -1) {
328 /* Bah! It's hopeless. */
329 int errnum = errno;
330 fprintf(stderr, format: "profiling: %s: cannot open: %s\n", filename,
331 strerror(errnum: errnum));
332 return;
333 }
334 }
335 }
336 }
337
338 /* Try to flock the file to serialize concurrent processes writing out to the
339 * same GCDA. This can fail if the filesystem doesn't support it, but in that
340 * case we'll just carry on with the old racy behaviour and hope for the best.
341 */
342 lprofLockFd(fd);
343 output_file = fdopen(fd: fd, modes: mode);
344
345 /* Initialize the write buffer. */
346 new_file = 0;
347 write_buffer = NULL;
348 cur_buffer_size = 0;
349 cur_pos = 0;
350
351 if (map_file() == -1) {
352 /* The file has been created just now (file_size == 0) or mmap failed
353 * unexpectedly. In the latter case, try to recover by clobbering. */
354 new_file = 1;
355 write_buffer = NULL;
356 resize_write_buffer(WRITE_BUFFER_SIZE);
357 memset(s: write_buffer, c: 0, WRITE_BUFFER_SIZE);
358 }
359
360 /* gcda file, version, stamp checksum. */
361 {
362 uint8_t c3 = version >> 24;
363 uint8_t c2 = (version >> 16) & 255;
364 uint8_t c1 = (version >> 8) & 255;
365 gcov_version = c3 >= 'A' ? (c3 - 'A') * 100 + (c2 - '0') * 10 + c1 - '0'
366 : (c3 - '0') * 10 + c1 - '0';
367 }
368 write_32bit_value(i: GCOV_DATA_MAGIC);
369 write_32bit_value(i: version);
370 write_32bit_value(i: checksum);
371
372#ifdef DEBUG_GCDAPROFILING
373 fprintf(stderr, "llvmgcda: [%s]\n", orig_filename);
374#endif
375}
376
377COMPILER_RT_VISIBILITY
378void llvm_gcda_emit_function(uint32_t ident, uint32_t func_checksum,
379 uint32_t cfg_checksum) {
380 uint32_t len = 2;
381 int use_extra_checksum = gcov_version >= 47;
382
383 if (use_extra_checksum)
384 len++;
385#ifdef DEBUG_GCDAPROFILING
386 fprintf(stderr, "llvmgcda: function id=0x%08x\n", ident);
387#endif
388 if (!output_file) return;
389
390 /* function tag */
391 write_32bit_value(i: GCOV_TAG_FUNCTION);
392 write_32bit_value(i: len);
393 write_32bit_value(i: ident);
394 write_32bit_value(i: func_checksum);
395 if (use_extra_checksum)
396 write_32bit_value(i: cfg_checksum);
397}
398
399COMPILER_RT_VISIBILITY
400void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
401 uint32_t i;
402 uint64_t *old_ctrs = NULL;
403 uint32_t val = 0;
404 uint64_t save_cur_pos = cur_pos;
405
406 if (!output_file) return;
407
408 val = read_32bit_value();
409
410 if (val != (uint32_t)-1) {
411 /* There are counters present in the file. Merge them. */
412 if (val != GCOV_TAG_COUNTER_ARCS) {
413 fprintf(stderr, format: "profiling: %s: cannot merge previous GCDA file: "
414 "corrupt arc tag (0x%08x)\n",
415 filename, val);
416 return;
417 }
418
419 val = read_32bit_value();
420 if (val == (uint32_t)-1 || val / 2 != num_counters) {
421 fprintf(stderr, format: "profiling: %s: cannot merge previous GCDA file: "
422 "mismatched number of counters (%d)\n",
423 filename, val);
424 return;
425 }
426
427 old_ctrs = malloc(size: sizeof(uint64_t) * num_counters);
428 for (i = 0; i < num_counters; ++i)
429 old_ctrs[i] = read_64bit_value();
430 }
431
432 cur_pos = save_cur_pos;
433
434 /* Counter #1 (arcs) tag */
435 write_32bit_value(i: GCOV_TAG_COUNTER_ARCS);
436 write_32bit_value(i: num_counters * 2);
437 for (i = 0; i < num_counters; ++i) {
438 counters[i] += (old_ctrs ? old_ctrs[i] : 0);
439 write_64bit_value(i: counters[i]);
440 }
441
442 free(ptr: old_ctrs);
443
444#ifdef DEBUG_GCDAPROFILING
445 fprintf(stderr, "llvmgcda: %u arcs\n", num_counters);
446 for (i = 0; i < num_counters; ++i)
447 fprintf(stderr, "llvmgcda: %llu\n", (unsigned long long)counters[i]);
448#endif
449}
450
451COMPILER_RT_VISIBILITY
452void llvm_gcda_summary_info(void) {
453 uint32_t runs = 1;
454 static uint32_t run_counted = 0; // We only want to increase the run count once.
455 uint32_t val = 0;
456 uint64_t save_cur_pos = cur_pos;
457
458 if (!output_file) return;
459
460 val = read_32bit_value();
461
462 if (val != (uint32_t)-1) {
463 /* There are counters present in the file. Merge them. */
464 uint32_t gcov_tag =
465 gcov_version >= 90 ? GCOV_TAG_OBJECT_SUMMARY : GCOV_TAG_PROGRAM_SUMMARY;
466 if (val != gcov_tag) {
467 fprintf(stderr,
468 format: "profiling: %s: cannot merge previous run count: "
469 "corrupt object tag (0x%08x)\n",
470 filename, val);
471 return;
472 }
473
474 val = read_32bit_value(); /* length */
475 uint32_t prev_runs;
476 if (gcov_version < 90) {
477 read_32bit_value();
478 read_32bit_value();
479 prev_runs = read_32bit_value();
480 } else {
481 prev_runs = read_32bit_value();
482 read_32bit_value();
483 }
484 for (uint32_t i = gcov_version < 90 ? 3 : 2; i < val; ++i)
485 read_32bit_value();
486 /* Add previous run count to new counter, if not already counted before. */
487 runs = run_counted ? prev_runs : prev_runs + 1;
488 }
489
490 cur_pos = save_cur_pos;
491
492 if (gcov_version >= 90) {
493 write_32bit_value(i: GCOV_TAG_OBJECT_SUMMARY);
494 write_32bit_value(i: 2);
495 write_32bit_value(i: runs);
496 write_32bit_value(i: 0); // sum_max
497 } else {
498 // Before gcov 4.8 (r190952), GCOV_TAG_SUMMARY_LENGTH was 9. r190952 set
499 // GCOV_TAG_SUMMARY_LENGTH to 22. We simply use the smallest length which
500 // can make gcov read "Runs:".
501 write_32bit_value(i: GCOV_TAG_PROGRAM_SUMMARY);
502 write_32bit_value(i: 3);
503 write_32bit_value(i: 0);
504 write_32bit_value(i: 0);
505 write_32bit_value(i: runs);
506 }
507
508 run_counted = 1;
509
510#ifdef DEBUG_GCDAPROFILING
511 fprintf(stderr, "llvmgcda: %u runs\n", runs);
512#endif
513}
514
515COMPILER_RT_VISIBILITY
516void llvm_gcda_end_file(void) {
517 /* Write out EOF record. */
518 if (output_file) {
519 write_bytes(s: "\0\0\0\0\0\0\0\0", len: 8);
520
521 if (new_file) {
522 fwrite(ptr: write_buffer, size: cur_pos, n: 1, s: output_file);
523 free(ptr: write_buffer);
524 } else {
525 unmap_file();
526 }
527
528 fflush(stream: output_file);
529 lprofUnlockFd(fd);
530 fclose(stream: output_file);
531 output_file = NULL;
532 write_buffer = NULL;
533 }
534 free(ptr: filename);
535
536#ifdef DEBUG_GCDAPROFILING
537 fprintf(stderr, "llvmgcda: -----\n");
538#endif
539}
540
541COMPILER_RT_VISIBILITY
542void llvm_register_writeout_function(fn_ptr fn) {
543 fn_list_insert(list: &writeout_fn_list, fn);
544}
545
546COMPILER_RT_VISIBILITY
547void llvm_writeout_files(void) {
548 struct fn_node *curr = writeout_fn_list.head;
549
550 while (curr) {
551 if (curr->id == CURRENT_ID) {
552 curr->fn();
553 }
554 curr = curr->next;
555 }
556}
557
558#ifndef _WIN32
559// __attribute__((destructor)) and destructors whose priorities are greater than
560// 100 run before this function and can thus be tracked. The priority is
561// compatible with GCC 7 onwards.
562#if __GNUC__ >= 9
563#pragma GCC diagnostic ignored "-Wprio-ctor-dtor"
564#endif
565__attribute__((destructor(100)))
566#endif
567static void llvm_writeout_and_clear(void) {
568 llvm_writeout_files();
569 fn_list_remove(list: &writeout_fn_list);
570}
571
572COMPILER_RT_VISIBILITY
573void llvm_register_reset_function(fn_ptr fn) {
574 fn_list_insert(list: &reset_fn_list, fn);
575}
576
577COMPILER_RT_VISIBILITY
578void llvm_delete_reset_function_list(void) { fn_list_remove(list: &reset_fn_list); }
579
580COMPILER_RT_VISIBILITY
581void llvm_reset_counters(void) {
582 struct fn_node *curr = reset_fn_list.head;
583
584 while (curr) {
585 if (curr->id == CURRENT_ID) {
586 curr->fn();
587 }
588 curr = curr->next;
589 }
590}
591
592#if !defined(_WIN32) && !defined(__wasm__)
593COMPILER_RT_VISIBILITY
594pid_t __gcov_fork() {
595 pid_t parent_pid = getpid();
596 pid_t pid = fork();
597
598 if (pid == 0) {
599 pid_t child_pid = getpid();
600 if (child_pid != parent_pid) {
601 // The pid changed so we've a fork (one could have its own fork function)
602 // Just reset the counters for this child process
603 // threads.
604 llvm_reset_counters();
605 }
606 }
607 return pid;
608}
609#endif
610
611COMPILER_RT_VISIBILITY
612void llvm_gcov_init(fn_ptr wfn, fn_ptr rfn) {
613 static int atexit_ran = 0;
614
615 if (wfn)
616 llvm_register_writeout_function(fn: wfn);
617
618 if (rfn)
619 llvm_register_reset_function(fn: rfn);
620
621 if (atexit_ran == 0) {
622 atexit_ran = 1;
623
624 /* Make sure we write out the data and delete the data structures. */
625 lprofAtExit(llvm_delete_reset_function_list);
626#ifdef _WIN32
627 lprofAtExit(llvm_writeout_and_clear);
628#endif
629 }
630}
631
632#if defined(_AIX)
633COMPILER_RT_VISIBILITY __attribute__((constructor)) void
634__llvm_profile_gcov_initialize() {
635 const __llvm_gcov_init_func_struct *InitFuncStart =
636 __llvm_profile_begin_covinit();
637 const __llvm_gcov_init_func_struct *InitFuncEnd =
638 __llvm_profile_end_covinit();
639
640 for (const __llvm_gcov_init_func_struct *Ptr = InitFuncStart;
641 Ptr != InitFuncEnd; ++Ptr) {
642 fn_ptr wfn = (fn_ptr)Ptr->WriteoutFunction;
643 fn_ptr rfn = (fn_ptr)Ptr->ResetFunction;
644 if (!(wfn && rfn))
645 continue;
646 llvm_gcov_init(wfn, rfn);
647 }
648}
649#endif
650
651void __gcov_dump(void) {
652 for (struct fn_node *f = writeout_fn_list.head; f; f = f->next)
653 f->fn();
654}
655
656void __gcov_reset(void) {
657 for (struct fn_node *f = reset_fn_list.head; f; f = f->next)
658 f->fn();
659}
660
661#endif
662