1 /* Copyright (C) 1996-2018 Free Software Foundation, Inc.
2 
3    This file is part of GCC.
4 
5    GCC is free software; you can redistribute it and/or modify it under
6    the terms of the GNU General Public License as published by the Free
7    Software Foundation; either version 3, or (at your option) any later
8    version.
9 
10    GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11    WARRANTY; without even the implied warranty of MERCHANTABILITY or
12    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13    for more details.
14 
15    Under Section 7 of GPL version 3, you are granted additional
16    permissions described in the GCC Runtime Library Exception, version
17    3.1, as published by the Free Software Foundation.
18 
19    You should have received a copy of the GNU General Public License and
20    a copy of the GCC Runtime Library Exception along with this program;
21    see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
22    <http://www.gnu.org/licenses/>.  */
23 
24 /**
25  * This file has excerpts from gcc libgcc/libgcov.h and gcc/gcov-io.h.
26  * Which is governed by section 7 of additional permissions.
27  */
28 
29 
30 #ifndef _COVERAGE_H_
31 #define _COVERAGE_H_
32 
33 #if (__GNUC__ >= 14)
34 #define GCOV_COUNTERS 9U
35 #elif (__GNUC__ >= 10)
36 #define GCOV_COUNTERS 8U
37 #elif (__GNUC__ >= 8)
38 #define GCOV_COUNTERS 9U
39 #else
40 #define GCOV_COUNTERS 10U
41 #endif
42 
43 /* The GCOV 12 gcno/gcda format has slight change,
44  * Please refer to gcov-io.h in the GCC 12 for
45  * more details.
46  *
47  * Following GCC commits introduced these changes:
48  * gcc-mirror/gcc@23eb66d
49  * gcc-mirror/gcc@72e0c74
50  */
51 #if (__GNUC__ >= 12)
52 #define GCOV_12_FORMAT
53 #endif
54 
55 typedef uint64_t gcov_type;
56 
57 #ifdef GCOV_12_FORMAT
58 #define GCOV_TAG_FUNCTION_LENGTH  12
59 #else
60 #define GCOV_TAG_FUNCTION_LENGTH  3
61 #endif
62 
63 #define GCOV_DATA_MAGIC   (0x67636461)
64 #define GCOV_TAG_FUNCTION (0x01000000)
65 #define GCOV_TAG_COUNTER_BASE (0x01a10000)
66 #define GCOV_TAG_FOR_COUNTER(count)         \
67 (GCOV_TAG_COUNTER_BASE + ((uint32_t) (count) << 17))
68 
69 #define FILE_START_INDICATOR	'*'
70 #define GCOV_DUMP_SEPARATOR	'<'
71 
72 /**Information about counters for a single function
73  *
74  * This data is generated by gcc during compilation and doesn't change
75  *  at run-time with the exception of the values array.
76  */
77 struct gcov_ctr_info {
78 	unsigned int num;    /* number of counter values for this type */
79 	gcov_type *values;   /* array of counter values for this type */
80 };
81 
82 /**
83  * Profiling meta data per function
84  *
85  * This data is generated by gcc during compilation and doesn't change
86  * at run-time.
87  *
88  * Information about a single function.  This uses the trailing array
89  * idiom. The number of counters is determined from the merge pointer
90  * array in gcov_info.  The key is used to detect which of a set of
91  * comdat functions was selected -- it points to the gcov_info object
92  * of the object file containing the selected comdat function.
93  */
94 struct gcov_fn_info {
95 	const struct gcov_info *key;     /* comdat key */
96 	unsigned int ident;              /* unique ident of function */
97 	unsigned int lineno_checksum;    /* function lineno_checksum */
98 	unsigned int cfg_checksum;       /* function cfg checksum */
99 	struct gcov_ctr_info ctrs[1];    /* instrumented counters */
100 };
101 
102 /** Profiling data per object file
103  *
104  * This data is generated by gcc during compilation and doesn't change
105  * at run-time with the exception of the next pointer.
106  */
107 struct gcov_info {
108 	unsigned int version;        /* Gcov version (same as GCC version) */
109 	struct gcov_info *next;      /* List head for a singly-linked list */
110 	unsigned int stamp;          /* Uniquifying time stamp */
111 #ifdef GCOV_12_FORMAT
112 	unsigned int checksum;		/* unique object checksum */
113 #endif
114 	const char *filename;        /* Name of the associated gcda data file */
115 	/* merge functions, null for unused*/
116 	void (*merge[GCOV_COUNTERS])(gcov_type *, unsigned int);
117 	unsigned int n_functions;         /* number of instrumented functions */
118 	struct gcov_fn_info **functions;  /* function information */
119 
120 };
121 
122 /*
123  * These functions are in the header for easy access for external interface
124  * reporting since they aren't useful without the structs in this header.
125  */
126 struct gcov_info *gcov_get_list_head(void);
127 size_t gcov_populate_buffer(uint8_t *buffer, struct gcov_info *info);
128 size_t gcov_calculate_buff_size(struct gcov_info *info);
129 void gcov_reset_all_counts(void);
130 
131 #endif /* _COVERAGE_H_ */
132