1/* File format for coverage information
2   Copyright (C) 1996, 1997, 1998, 2000, 2002,
3   2003, 2004, 2005, 2008, 2009 Free Software Foundation, Inc.
4   Contributed by Bob Manson <manson@cygnus.com>.
5   Completely remangled by Nathan Sidwell <nathan@codesourcery.com>.
6
7This file is part of GCC.
8
9GCC is free software; you can redistribute it and/or modify it under
10the terms of the GNU General Public License as published by the Free
11Software Foundation; either version 3, or (at your option) any later
12version.
13
14GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15WARRANTY; without even the implied warranty of MERCHANTABILITY or
16FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17for more details.
18
19Under Section 7 of GPL version 3, you are granted additional
20permissions described in the GCC Runtime Library Exception, version
213.1, as published by the Free Software Foundation.
22
23You should have received a copy of the GNU General Public License and
24a copy of the GCC Runtime Library Exception along with this program;
25see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
26<http://www.gnu.org/licenses/>.  */
27
28
29/* Coverage information is held in two files.  A notes file, which is
30   generated by the compiler, and a data file, which is generated by
31   the program under test.  Both files use a similar structure.  We do
32   not attempt to make these files backwards compatible with previous
33   versions, as you only need coverage information when developing a
34   program.  We do hold version information, so that mismatches can be
35   detected, and we use a format that allows tools to skip information
36   they do not understand or are not interested in.
37
38   Numbers are recorded in the 32 bit unsigned binary form of the
39   endianness of the machine generating the file. 64 bit numbers are
40   stored as two 32 bit numbers, the low part first.  Strings are
41   padded with 1 to 4 NUL bytes, to bring the length up to a multiple
42   of 4. The number of 4 bytes is stored, followed by the padded
43   string. Zero length and NULL strings are simply stored as a length
44   of zero (they have no trailing NUL or padding).
45
46   	int32:  byte3 byte2 byte1 byte0 | byte0 byte1 byte2 byte3
47	int64:  int32:low int32:high
48	string: int32:0 | int32:length char* char:0 padding
49	padding: | char:0 | char:0 char:0 | char:0 char:0 char:0
50	item: int32 | int64 | string
51
52   The basic format of the files is
53
54   	file : int32:magic int32:version int32:stamp record*
55
56   The magic ident is different for the notes and the data files.  The
57   magic ident is used to determine the endianness of the file, when
58   reading.  The version is the same for both files and is derived
59   from gcc's version number. The stamp value is used to synchronize
60   note and data files and to synchronize merging within a data
61   file. It need not be an absolute time stamp, merely a ticker that
62   increments fast enough and cycles slow enough to distinguish
63   different compile/run/compile cycles.
64
65   Although the ident and version are formally 32 bit numbers, they
66   are derived from 4 character ASCII strings.  The version number
67   consists of the single character major version number, a two
68   character minor version number (leading zero for versions less than
69   10), and a single character indicating the status of the release.
70   That will be 'e' experimental, 'p' prerelease and 'r' for release.
71   Because, by good fortune, these are in alphabetical order, string
72   collating can be used to compare version strings.  Be aware that
73   the 'e' designation will (naturally) be unstable and might be
74   incompatible with itself.  For gcc 3.4 experimental, it would be
75   '304e' (0x33303465).  When the major version reaches 10, the
76   letters A-Z will be used.  Assuming minor increments releases every
77   6 months, we have to make a major increment every 50 years.
78   Assuming major increments releases every 5 years, we're ok for the
79   next 155 years -- good enough for me.
80
81   A record has a tag, length and variable amount of data.
82
83   	record: header data
84	header: int32:tag int32:length
85	data: item*
86
87   Records are not nested, but there is a record hierarchy.  Tag
88   numbers reflect this hierarchy.  Tags are unique across note and
89   data files.  Some record types have a varying amount of data.  The
90   LENGTH is the number of 4bytes that follow and is usually used to
91   determine how much data.  The tag value is split into 4 8-bit
92   fields, one for each of four possible levels.  The most significant
93   is allocated first.  Unused levels are zero.  Active levels are
94   odd-valued, so that the LSB of the level is one.  A sub-level
95   incorporates the values of its superlevels.  This formatting allows
96   you to determine the tag hierarchy, without understanding the tags
97   themselves, and is similar to the standard section numbering used
98   in technical documents.  Level values [1..3f] are used for common
99   tags, values [41..9f] for the notes file and [a1..ff] for the data
100   file.
101
102   The basic block graph file contains the following records
103   	note: unit function-graph*
104	unit: header int32:checksum string:source
105	function-graph: announce_function basic_blocks {arcs | lines}*
106	announce_function: header int32:ident
107		int32:lineno_checksum int32:cfg_checksum
108		string:name string:source int32:lineno
109	basic_block: header int32:flags*
110	arcs: header int32:block_no arc*
111	arc:  int32:dest_block int32:flags
112        lines: header int32:block_no line*
113               int32:0 string:NULL
114	line:  int32:line_no | int32:0 string:filename
115
116   The BASIC_BLOCK record holds per-bb flags.  The number of blocks
117   can be inferred from its data length.  There is one ARCS record per
118   basic block.  The number of arcs from a bb is implicit from the
119   data length.  It enumerates the destination bb and per-arc flags.
120   There is one LINES record per basic block, it enumerates the source
121   lines which belong to that basic block.  Source file names are
122   introduced by a line number of 0, following lines are from the new
123   source file.  The initial source file for the function is NULL, but
124   the current source file should be remembered from one LINES record
125   to the next.  The end of a block is indicated by an empty filename
126   - this does not reset the current source file.  Note there is no
127   ordering of the ARCS and LINES records: they may be in any order,
128   interleaved in any manner.  The current filename follows the order
129   the LINES records are stored in the file, *not* the ordering of the
130   blocks they are for.
131
132   The data file contains the following records.
133        data: {unit function-data* summary:object summary:program*}*
134	unit: header int32:checksum
135        function-data:	announce_function arc_counts
136	announce_function: header int32:ident
137		int32:lineno_checksum int32:cfg_checksum
138	arc_counts: header int64:count*
139	summary: int32:checksum {count-summary}GCOV_COUNTERS
140	count-summary:	int32:num int32:runs int64:sum
141			int64:max int64:sum_max
142
143   The ANNOUNCE_FUNCTION record is the same as that in the note file,
144   but without the source location.  The ARC_COUNTS gives the counter
145   values for those arcs that are instrumented.  The SUMMARY records
146   give information about the whole object file and about the whole
147   program.  The checksum is used for whole program summaries, and
148   disambiguates different programs which include the same
149   instrumented object file.  There may be several program summaries,
150   each with a unique checksum.  The object summary's checksum is zero.
151   Note that the data file might contain information from several runs
152   concatenated, or the data might be merged.
153
154   This file is included by both the compiler, gcov tools and the
155   runtime support library libgcov. IN_LIBGCOV and IN_GCOV are used to
156   distinguish which case is which.  If IN_LIBGCOV is nonzero,
157   libgcov is being built. If IN_GCOV is nonzero, the gcov tools are
158   being built. Otherwise the compiler is being built. IN_GCOV may be
159   positive or negative. If positive, we are compiling a tool that
160   requires additional functions (see the code for knowledge of what
161   those functions are).  */
162
163#ifndef GCC_GCOV_IO_H
164#define GCC_GCOV_IO_H
165
166#ifdef __KERNEL__
167#ifndef __GCOV_KERNEL__
168#define __GCOV_KERNEL__
169#endif /* __GCOV_KERNEL__ */
170#endif /* __KERNEL__ */
171
172#ifdef __GCOV_KERNEL__
173#define GCOV_LINKAGE /* nothing */
174
175/* We need the definitions for
176    BITS_PER_UNIT and
177    LONG_LONG_TYPE_SIZE
178  They are defined in gcc/defaults.h and gcc/config/<arch_depend_files>
179  (like, gcc/config/i386/i386.h). And it can be overridden by setting
180  in build scripts. Here I hardcoded the value for x86.
181  Todo: using a program to auto-generate the vaules in build time.  */
182#define BITS_PER_UNIT 8
183#define LONG_LONG_TYPE_SIZE 64
184
185/* There are many gcc_assertions. Set the vaule to 1 if we want a warning
186   message if the assertion fails.  */
187#ifndef ENABLE_ASSERT_CHECKING
188#define ENABLE_ASSERT_CHECKING 1
189#endif
190
191#include <linux/fs.h>
192#endif /* __GCOV_KERNEL__ */
193
194/* Wrappers to the file operations.  */
195#ifndef __GCOV_KERNEL__
196# define _GCOV_FILE      FILE
197# define _GCOV_fclose    fclose
198# define _GCOV_ftell     ftell
199# define _GCOV_fseek     fseek
200# define _GCOV_ftruncate ftruncate
201# define _GCOV_fread     fread
202# define _GCOV_fwrite    fwrite
203# define _GCOV_fread     fread
204# define _GCOV_fileno    fileno
205#else /* __GCOV_KERNEL__ */
206/* In Linux kernel mode, a virtual file is used for file operations.  */
207struct gcov_info;
208typedef struct {
209  long size; /* size of buf */
210  long count; /* element written into buf */
211  struct gcov_info *info;
212  char buf[0];
213} gcov_kernel_vfile;
214
215# define _GCOV_FILE gcov_kernel_vfile
216
217/* gcc_assert() prints out a warning if the check fails. It
218   will not abort.  */
219#if ENABLE_ASSERT_CHECKING
220# define gcc_assert(EXPR) \
221    ((void)(!(EXPR) ? printk (KERN_WARNING \
222      "GCOV assertion fails: func=%s line=%d\n", \
223      __FUNCTION__, __LINE__), 0 : 0))
224#else
225# define gcc_assert(EXPR) ((void)(0 && (EXPR)))
226#endif
227
228/* Wrappers to the file operations.  */
229# define _GCOV_fclose     kernel_file_fclose
230# define _GCOV_ftell      kernel_file_ftell
231# define _GCOV_fseek      kernel_file_fseek
232# define _GCOV_ftruncate  kernel_file_ftruncate
233# define _GCOV_fread      kernel_file_fread
234# define _GCOV_fwrite     kernel_file_fwrite
235# define _GCOV_fileno     kernel_file_fileno
236
237/* Declarations for virtual files operations.  */
238extern int kernel_file_fclose (gcov_kernel_vfile *);
239extern long kernel_file_ftell (gcov_kernel_vfile *);
240extern int kernel_file_fseek (gcov_kernel_vfile *, long, int);
241extern int kernel_file_ftruncate (gcov_kernel_vfile *, off_t);
242extern int kernel_file_fread (void *, size_t, size_t,
243    gcov_kernel_vfile *);
244extern int kernel_file_fwrite (const void *, size_t, size_t,
245    gcov_kernel_vfile *);
246extern int kernel_file_fileno(gcov_kernel_vfile *);
247#endif /* GCOV_KERNEL */
248#if IN_LIBGCOV
249
250#undef FUNC_ID_WIDTH
251#undef FUNC_ID_MASK
252/* About the target */
253
254#if BITS_PER_UNIT == 8
255typedef unsigned gcov_unsigned_t __attribute__ ((mode (SI)));
256typedef unsigned gcov_position_t __attribute__ ((mode (SI)));
257#if LONG_LONG_TYPE_SIZE > 32
258typedef signed gcov_type __attribute__ ((mode (DI)));
259#define FUNC_ID_WIDTH 32
260#define FUNC_ID_MASK ((1ll << FUNC_ID_WIDTH) - 1)
261#else
262typedef signed gcov_type __attribute__ ((mode (SI)));
263#define FUNC_ID_WIDTH 16
264#define FUNC_ID_MASK ((1 << FUNC_ID_WIDTH) - 1)
265#endif
266#else   /* BITS_PER_UNIT != 8  */
267#if BITS_PER_UNIT == 16
268typedef unsigned gcov_unsigned_t __attribute__ ((mode (HI)));
269typedef unsigned gcov_position_t __attribute__ ((mode (HI)));
270#if LONG_LONG_TYPE_SIZE > 32
271typedef signed gcov_type __attribute__ ((mode (SI)));
272#define FUNC_ID_WIDTH 32
273#define FUNC_ID_MASK ((1ll << FUNC_ID_WIDTH) - 1)
274#else
275typedef signed gcov_type __attribute__ ((mode (HI)));
276#define FUNC_ID_WIDTH 16
277#define FUNC_ID_MASK ((1 << FUNC_ID_WIDTH) - 1)
278#endif
279#else  /* BITS_PER_UNIT != 16  */
280typedef unsigned gcov_unsigned_t __attribute__ ((mode (QI)));
281typedef unsigned gcov_position_t __attribute__ ((mode (QI)));
282#if LONG_LONG_TYPE_SIZE > 32
283typedef signed gcov_type __attribute__ ((mode (HI)));
284#define FUNC_ID_WIDTH 32
285#define FUNC_ID_MASK ((1ll << FUNC_ID_WIDTH) - 1)
286#else
287typedef signed gcov_type __attribute__ ((mode (QI)));
288#define FUNC_ID_WIDTH 16
289#define FUNC_ID_MASK ((1 << FUNC_ID_WIDTH) - 1)
290#endif
291#endif /* BITS_PER_UNIT == 16  */
292
293#endif  /* BITS_PER_UNIT == 8  */
294
295#undef EXTRACT_MODULE_ID_FROM_GLOBAL_ID
296#undef EXTRACT_FUNC_ID_FROM_GLOBAL_ID
297#undef GEN_FUNC_GLOBAL_ID
298#define EXTRACT_MODULE_ID_FROM_GLOBAL_ID(gid) \
299                (gcov_unsigned_t)(((gid) >> FUNC_ID_WIDTH) & FUNC_ID_MASK)
300#define EXTRACT_FUNC_ID_FROM_GLOBAL_ID(gid) \
301                (gcov_unsigned_t)((gid) & FUNC_ID_MASK)
302#define GEN_FUNC_GLOBAL_ID(m,f) ((((gcov_type) (m)) << FUNC_ID_WIDTH) | (f))
303
304
305#if defined (TARGET_POSIX_IO)
306#define GCOV_LOCKED 1
307#else
308#define GCOV_LOCKED 0
309#endif
310
311#else /* !IN_LIBGCOV */
312/* About the host */
313
314typedef unsigned gcov_unsigned_t;
315typedef unsigned gcov_position_t;
316
317/* gcov_type is typedef'd elsewhere for the compiler */
318#if IN_GCOV
319#define GCOV_LINKAGE static
320typedef HOST_WIDEST_INT gcov_type;
321#if IN_GCOV > 0
322#include <sys/types.h>
323#endif
324
325#define FUNC_ID_WIDTH HOST_BITS_PER_WIDE_INT/2
326#define FUNC_ID_MASK ((1L << FUNC_ID_WIDTH) - 1)
327#define EXTRACT_MODULE_ID_FROM_GLOBAL_ID(gid) (unsigned)(((gid) >> FUNC_ID_WIDTH) & FUNC_ID_MASK)
328#define EXTRACT_FUNC_ID_FROM_GLOBAL_ID(gid) (unsigned)((gid) & FUNC_ID_MASK)
329#define FUNC_GLOBAL_ID(m,f) ((((HOST_WIDE_INT) (m)) << FUNC_ID_WIDTH) | (f)
330
331#else /*!IN_GCOV */
332#define GCOV_TYPE_SIZE (LONG_LONG_TYPE_SIZE > 32 ? 64 : 32)
333#endif
334
335#if defined (HOST_HAS_F_SETLKW)
336#define GCOV_LOCKED 1
337#else
338#define GCOV_LOCKED 0
339#endif
340
341#endif /* !IN_LIBGCOV */
342
343/* In gcov we want function linkage to be static.  In the compiler we want
344   it extern, so that they can be accessed from elsewhere.  In libgcov we
345   need these functions to be extern, so prefix them with __gcov.  In
346   libgcov they must also be hidden so that the instance in the executable
347   is not also used in a DSO.  */
348#if IN_LIBGCOV
349
350#ifndef __GCOV_KERNEL__
351#include "tconfig.h"
352#endif /* __GCOV_KERNEL__ */
353
354#define gcov_var __gcov_var
355#define gcov_open __gcov_open
356#define gcov_close __gcov_close
357#define gcov_write_tag_length __gcov_write_tag_length
358#define gcov_position __gcov_position
359#define gcov_seek __gcov_seek
360#define gcov_rewrite __gcov_rewrite
361#define gcov_truncate __gcov_truncate
362#define gcov_is_error __gcov_is_error
363#define gcov_write_unsigned __gcov_write_unsigned
364#define gcov_write_counter __gcov_write_counter
365#define gcov_write_summary __gcov_write_summary
366#define gcov_write_module_info __gcov_write_module_info
367#define gcov_write_string __gcov_write_string
368#define gcov_string_length __gcov_string_length
369#define gcov_read_unsigned __gcov_read_unsigned
370#define gcov_read_counter __gcov_read_counter
371#define gcov_read_string __gcov_read_string
372#define gcov_read_summary __gcov_read_summary
373#define gcov_read_module_info __gcov_read_module_info
374#define gcov_sort_n_vals __gcov_sort_n_vals
375#define gcov_canonical_filename _gcov_canonical_filename
376#define gcov_read_pmu_load_latency_info __gcov_read_pmu_load_latency_info
377#define gcov_read_pmu_branch_mispredict_info __gcov_read_pmu_branch_mispredict_info
378#define gcov_read_pmu_tool_header __gcov_read_pmu_tool_header
379#define destroy_pmu_tool_header __destroy_pmu_tool_header
380
381
382/* Poison these, so they don't accidentally slip in.  */
383#pragma GCC poison gcov_write_tag gcov_write_length
384#pragma GCC poison gcov_sync gcov_time gcov_magic
385
386#ifdef HAVE_GAS_HIDDEN
387#define ATTRIBUTE_HIDDEN  __attribute__ ((__visibility__ ("hidden")))
388#else
389#define ATTRIBUTE_HIDDEN
390#endif
391
392#else
393
394#define ATTRIBUTE_HIDDEN
395
396#endif
397
398#ifndef GCOV_LINKAGE
399#define GCOV_LINKAGE extern
400#endif
401
402/* File suffixes.  */
403#define GCOV_DATA_SUFFIX ".gcda"
404#define GCOV_NOTE_SUFFIX ".gcno"
405
406/* File magic. Must not be palindromes.  */
407#define GCOV_DATA_MAGIC ((gcov_unsigned_t)0x67636461) /* "gcda" */
408#define GCOV_NOTE_MAGIC ((gcov_unsigned_t)0x67636e6f) /* "gcno" */
409
410/* gcov-iov.h is automatically generated by the makefile from
411   version.c, it looks like
412   	#define GCOV_VERSION ((gcov_unsigned_t)0x89abcdef)
413*/
414#include "gcov-iov.h"
415
416/* Convert a magic or version number to a 4 character string.  */
417#define GCOV_UNSIGNED2STRING(ARRAY,VALUE)	\
418  ((ARRAY)[0] = (char)((VALUE) >> 24),		\
419   (ARRAY)[1] = (char)((VALUE) >> 16),		\
420   (ARRAY)[2] = (char)((VALUE) >> 8),		\
421   (ARRAY)[3] = (char)((VALUE) >> 0))
422
423/* The record tags.  Values [1..3f] are for tags which may be in either
424   file.  Values [41..9f] for those in the note file and [a1..ff] for
425   the data file.  The tag value zero is used as an explicit end of
426   file marker -- it is not required to be present.  */
427
428#define GCOV_TAG_FUNCTION	 ((gcov_unsigned_t)0x01000000)
429#define GCOV_TAG_FUNCTION_LENGTH (3)
430#define GCOV_TAG_BLOCKS		 ((gcov_unsigned_t)0x01410000)
431#define GCOV_TAG_BLOCKS_LENGTH(NUM) (NUM)
432#define GCOV_TAG_BLOCKS_NUM(LENGTH) (LENGTH)
433#define GCOV_TAG_ARCS		 ((gcov_unsigned_t)0x01430000)
434#define GCOV_TAG_ARCS_LENGTH(NUM)  (1 + (NUM) * 2)
435#define GCOV_TAG_ARCS_NUM(LENGTH)  (((LENGTH) - 1) / 2)
436#define GCOV_TAG_LINES		 ((gcov_unsigned_t)0x01450000)
437#define GCOV_TAG_COUNTER_BASE 	 ((gcov_unsigned_t)0x01a10000)
438#define GCOV_TAG_COUNTER_LENGTH(NUM) ((NUM) * 2)
439#define GCOV_TAG_COUNTER_NUM(LENGTH) ((LENGTH) / 2)
440#define GCOV_TAG_OBJECT_SUMMARY  ((gcov_unsigned_t)0xa1000000)
441#define GCOV_TAG_PROGRAM_SUMMARY ((gcov_unsigned_t)0xa3000000)
442#define GCOV_TAG_SUMMARY_LENGTH  \
443	(1 + GCOV_COUNTERS_SUMMABLE * (2 + 3 * 2))
444#define GCOV_TAG_MODULE_INFO ((gcov_unsigned_t)0xa4000000)
445#define GCOV_TAG_PMU_LOAD_LATENCY_INFO ((gcov_unsigned_t)0xa5000000)
446#define GCOV_TAG_PMU_LOAD_LATENCY_LENGTH(filename)  \
447  (gcov_string_length (filename) + 12 + 2)
448#define GCOV_TAG_PMU_BRANCH_MISPREDICT_INFO ((gcov_unsigned_t)0xa7000000)
449#define GCOV_TAG_PMU_BRANCH_MISPREDICT_LENGTH(filename)  \
450  (gcov_string_length (filename) + 5 + 2)
451#define GCOV_TAG_PMU_TOOL_HEADER ((gcov_unsigned_t)0xa9000000)
452
453/* Counters that are collected.  */
454#define GCOV_COUNTER_ARCS 	0  /* Arc transitions.  */
455#define GCOV_COUNTERS_SUMMABLE	1  /* Counters which can be
456				      summaried.  */
457#define GCOV_FIRST_VALUE_COUNTER 1 /* The first of counters used for value
458				      profiling.  They must form a consecutive
459				      interval and their order must match
460				      the order of HIST_TYPEs in
461				      value-prof.h.  */
462#define GCOV_COUNTER_V_INTERVAL	1  /* Histogram of value inside an interval.  */
463#define GCOV_COUNTER_V_POW2	2  /* Histogram of exact power2 logarithm
464				      of a value.  */
465#define GCOV_COUNTER_V_SINGLE	3  /* The most common value of expression.  */
466#define GCOV_COUNTER_V_DELTA	4  /* The most common difference between
467				      consecutive values of expression.  */
468
469#define GCOV_COUNTER_V_INDIR	5  /* The most common indirect address */
470#define GCOV_COUNTER_AVERAGE	6  /* Compute average value passed to the
471				      counter.  */
472#define GCOV_COUNTER_IOR	7  /* IOR of the all values passed to
473				      counter.  */
474#define GCOV_COUNTER_ICALL_TOPNV 8 /* Top N value tracking for indirect calls */
475#define GCOV_LAST_VALUE_COUNTER 8  /* The last of counters used for value
476				      profiling.  */
477#define GCOV_COUNTER_DIRECT_CALL 9 /* Direct call counts.  */
478#define GCOV_COUNTER_REUSE_DIST 10 /* Reuse distance measure.  */
479#define GCOV_COUNTERS		11
480
481/* Number of counters used for value profiling.  */
482#define GCOV_N_VALUE_COUNTERS \
483  (GCOV_LAST_VALUE_COUNTER - GCOV_FIRST_VALUE_COUNTER + 1)
484
485  /* A list of human readable names of the counters */
486#define GCOV_COUNTER_NAMES	{"arcs", "interval", "pow2", "single", \
487				 "delta","indirect_call", "average", "ior", \
488				 "indirect_call_topn", "direct_call", \
489                                 "reuse_distance"}
490
491#define GCOV_ICALL_TOPN_VAL  2   /* Track two hottest callees */
492#define GCOV_ICALL_TOPN_NCOUNTS  9 /* The number of counter entries per icall callsite */
493  /* Names of merge functions for counters.  */
494#define GCOV_MERGE_FUNCTIONS	{"__gcov_merge_add",	\
495				 "__gcov_merge_add",	\
496				 "__gcov_merge_add",	\
497				 "__gcov_merge_single",	\
498				 "__gcov_merge_delta",  \
499				 "__gcov_merge_single", \
500				 "__gcov_merge_add",	\
501				 "__gcov_merge_ior",	\
502				 "__gcov_merge_icall_topn",\
503                                 "__gcov_merge_dc",\
504                                 "__gcov_merge_reusedist" }
505
506/* Convert a counter index to a tag.  */
507#define GCOV_TAG_FOR_COUNTER(COUNT)				\
508	(GCOV_TAG_COUNTER_BASE + ((gcov_unsigned_t)(COUNT) << 17))
509/* Convert a tag to a counter.  */
510#define GCOV_COUNTER_FOR_TAG(TAG)					\
511	((unsigned)(((TAG) - GCOV_TAG_COUNTER_BASE) >> 17))
512/* Check whether a tag is a counter tag.  */
513#define GCOV_TAG_IS_COUNTER(TAG)				\
514	(!((TAG) & 0xFFFF) && GCOV_COUNTER_FOR_TAG (TAG) < GCOV_COUNTERS)
515
516/* The tag level mask has 1's in the position of the inner levels, &
517   the lsb of the current level, and zero on the current and outer
518   levels.  */
519#define GCOV_TAG_MASK(TAG) (((TAG) - 1) ^ (TAG))
520
521/* Return nonzero if SUB is an immediate subtag of TAG.  */
522#define GCOV_TAG_IS_SUBTAG(TAG,SUB)				\
523	(GCOV_TAG_MASK (TAG) >> 8 == GCOV_TAG_MASK (SUB) 	\
524	 && !(((SUB) ^ (TAG)) & ~GCOV_TAG_MASK(TAG)))
525
526/* Return nonzero if SUB is at a sublevel to TAG.  */
527#define GCOV_TAG_IS_SUBLEVEL(TAG,SUB)				\
528     	(GCOV_TAG_MASK (TAG) > GCOV_TAG_MASK (SUB))
529
530/* Basic block flags.  */
531#define GCOV_BLOCK_UNEXPECTED	(1 << 1)
532
533/* Arc flags.  */
534#define GCOV_ARC_ON_TREE 	(1 << 0)
535#define GCOV_ARC_FAKE		(1 << 1)
536#define GCOV_ARC_FALLTHROUGH	(1 << 2)
537
538/* Structured records.  */
539
540/* Cumulative counter data.  */
541struct gcov_ctr_summary
542{
543  gcov_unsigned_t num;		/* number of counters.  */
544  gcov_unsigned_t runs;		/* number of program runs */
545  gcov_type sum_all;		/* sum of all counters accumulated.  */
546  gcov_type run_max;		/* maximum value on a single run.  */
547  gcov_type sum_max;    	/* sum of individual run max values.  */
548};
549
550/* Object & program summary record.  */
551struct gcov_summary
552{
553  gcov_unsigned_t checksum;	/* checksum of program */
554  struct gcov_ctr_summary ctrs[GCOV_COUNTERS_SUMMABLE];
555};
556
557#define GCOV_MODULE_UNKNOWN_LANG  0
558#define GCOV_MODULE_C_LANG    1
559#define GCOV_MODULE_CPP_LANG  2
560#define GCOV_MODULE_FORT_LANG 3
561
562#define GCOV_MODULE_ASM_STMTS (1 << 16)
563#define GCOV_MODULE_LANG_MASK 0xffff
564
565enum print_newline {no_newline, add_newline};
566
567/* Source module info. The data structure is used in
568   both runtime and profile-use phase. Make sure to allocate
569   enough space for the variable length member.  */
570struct gcov_module_info
571{
572  gcov_unsigned_t ident;
573  gcov_unsigned_t is_primary; /* this is overloaded to mean two things:
574				 (1) means FDO/LIPO in instrumented binary.
575				 (2) means IS_PRIMARY in persistent file or
576				     memory copy used in profile-use.  */
577  gcov_unsigned_t is_exported;
578  gcov_unsigned_t lang; /* lower 16 bits encode the language, and the upper
579			   16 bits enocde other attributes, such as whether
580			   any assembler is present in the source, etc.  */
581  char *da_filename;
582  char *source_filename;
583  gcov_unsigned_t num_quote_paths;
584  gcov_unsigned_t num_bracket_paths;
585  gcov_unsigned_t num_cpp_defines;
586  gcov_unsigned_t num_cpp_includes;
587  gcov_unsigned_t num_cl_args;
588  char *string_array[1];
589};
590
591extern struct gcov_module_info **module_infos;
592extern unsigned primary_module_id;
593#define PRIMARY_MODULE_EXPORTED                                         \
594  (module_infos[0]->is_exported						\
595   && !((module_infos[0]->lang & GCOV_MODULE_ASM_STMTS)			\
596	&& flag_ripa_disallow_asm_modules))
597
598/* Information about the hardware performance monitoring unit.  */
599struct gcov_pmu_info
600{
601  const char *pmu_profile_filename;	/* pmu profile filename  */
602  const char *pmu_tool;  	/* canonical pmu tool options  */
603  gcov_unsigned_t pmu_top_n_address;  /* how many top addresses to symbolize */
604};
605
606/* Information about the PMU tool header.  */
607typedef struct gcov_pmu_tool_header {
608  char *host_cpu;
609  char *hostname;
610  char *kernel_version;
611  char *column_header;
612  char *column_description;
613  char *full_header;
614} gcov_pmu_tool_header_t;
615
616/* Available only for PMUs which support PEBS or IBS using pfmon
617   tool. If any field here is changed, the length computation in
618   GCOV_TAG_PMU_LOAD_LATENCY_LENGTH must be updated as well. All
619   percentages are multiplied by 100 to make them out of 10000 and
620   only integer part is kept.  */
621typedef struct gcov_pmu_load_latency_info
622{
623  gcov_unsigned_t counts;     /* raw count of samples */
624  gcov_unsigned_t self;       /* per 10k of total samples */
625  gcov_unsigned_t cum;        /* per 10k cumulative weight */
626  gcov_unsigned_t lt_10;      /* per 10k with latency <= 10 cycles */
627  gcov_unsigned_t lt_32;      /* per 10k with latency <= 32 cycles */
628  gcov_unsigned_t lt_64;      /* per 10k with latency <= 64 cycles */
629  gcov_unsigned_t lt_256;     /* per 10k with latency <= 256 cycles */
630  gcov_unsigned_t lt_1024;    /* per 10k with latency <= 1024 cycles */
631  gcov_unsigned_t gt_1024;    /* per 10k with latency > 1024 cycles */
632  gcov_unsigned_t wself;      /* weighted average cost of this miss in cycles */
633  gcov_type code_addr;        /* the actual miss address (pc+1 for Intel) */
634  gcov_unsigned_t line;       /* line number corresponding to this miss */
635  gcov_unsigned_t discriminator;   /* discriminator information for this miss */
636  char *filename;       /* filename corresponding to this miss */
637} gcov_pmu_ll_info_t;
638
639/* This structure is used during runtime as well as in gcov.  */
640typedef struct load_latency_infos
641{
642  /* An array describing the total number of load latency fields.  */
643  gcov_pmu_ll_info_t **ll_array;
644  /* The total number of entries in the load latency array.  */
645  unsigned ll_count;
646  /* The total number of entries currently allocated in the array.
647     Used for bookkeeping.  */
648  unsigned alloc_ll_count;
649  /* PMU tool header */
650  gcov_pmu_tool_header_t *pmu_tool_header;
651} ll_infos_t;
652
653/* Available only for PMUs which support PEBS or IBS using pfmon
654   tool. If any field here is changed, the length computation in
655   GCOV_TAG_PMU_BR_MISPREDICT_LENGTH must be updated as well. All
656   percentages are multiplied by 100 to make them out of 10000 and
657   only integer part is kept.  */
658typedef struct gcov_pmu_branch_mispredict_info
659{
660  gcov_unsigned_t counts;     /* raw count of samples */
661  gcov_unsigned_t self;       /* per 10k of total samples */
662  gcov_unsigned_t cum;        /* per 10k cumulative weight */
663  gcov_type code_addr;        /* the actual mispredict address */
664  gcov_unsigned_t line;       /* line number corresponding to this event */
665  gcov_unsigned_t discriminator;   /* discriminator for this event */
666  char *filename;       /* filename corresponding to this event */
667} gcov_pmu_brm_info_t;
668
669/* This structure is used during runtime as well as in gcov.  */
670typedef struct branch_mispredict_infos
671{
672  /* An array describing the total number of mispredict entries.  */
673  gcov_pmu_brm_info_t **brm_array;
674  /* The total number of entries in the above array.  */
675  unsigned brm_count;
676  /* The total number of entries currently allocated in the array.
677     Used for bookkeeping.  */
678  unsigned alloc_brm_count;
679  /* PMU tool header */
680  gcov_pmu_tool_header_t *pmu_tool_header;
681} brm_infos_t;
682
683/* Structures embedded in coveraged program.  The structures generated
684   by write_profile must match these.  */
685
686#if IN_LIBGCOV
687/* Information about a single function.  This uses the trailing array
688   idiom. The number of counters is determined from the counter_mask
689   in gcov_info.  We hold an array of function info, so have to
690   explicitly calculate the correct array stride.  */
691
692struct gcov_fn_info
693{
694  gcov_unsigned_t ident;	/* unique ident of function */
695  gcov_unsigned_t lineno_checksum;	/* function lineo_checksum */
696  gcov_unsigned_t cfg_checksum;	/* function cfg checksum */
697  gcov_unsigned_t dc_offset;    /* direct call offset */
698  unsigned n_ctrs[0];		/* instrumented counters */
699};
700
701/* Type of function used to merge counters.  */
702typedef void (*gcov_merge_fn) (gcov_type *, gcov_unsigned_t);
703
704/* Information about counters.  */
705struct gcov_ctr_info
706{
707  gcov_unsigned_t num;		/* number of counters.  */
708  gcov_type *values;		/* their values.  */
709  gcov_merge_fn merge;  	/* The function used to merge them.  */
710};
711
712/* Information about a single object file.  */
713struct gcov_info
714{
715  gcov_unsigned_t version;	/* expected version number */
716  struct gcov_module_info *mod_info; /* addtional module info.  */
717  struct gcov_info *next;	/* link to next, used by libgcov */
718
719  gcov_unsigned_t stamp;	/* uniquifying time stamp */
720  const char *filename;		/* output file name */
721  gcov_unsigned_t eof_pos;      /* end position of profile data */
722  unsigned n_functions;		/* number of functions */
723  const struct gcov_fn_info *functions; /* table of functions */
724
725  unsigned ctr_mask;		/* mask of counters instrumented.  */
726  struct gcov_ctr_info counts[0]; /* count data. The number of bits
727				     set in the ctr_mask field
728				     determines how big this array
729				     is.  */
730};
731
732/* Information about a single imported module.  */
733struct dyn_imp_mod
734{
735  const struct gcov_info *imp_mod;
736  double weight;
737};
738
739/* Register a new object file module.  */
740extern void __gcov_init (struct gcov_info *) ATTRIBUTE_HIDDEN;
741
742/* Called before fork, to avoid double counting.  */
743extern void __gcov_flush (void) ATTRIBUTE_HIDDEN;
744
745/* The merge function that just sums the counters.  */
746extern void __gcov_merge_add (gcov_type *, unsigned) ATTRIBUTE_HIDDEN;
747
748/* The merge function to choose the most common value.  */
749extern void __gcov_merge_single (gcov_type *, unsigned) ATTRIBUTE_HIDDEN;
750
751/* The merge function to choose the most common difference between
752   consecutive values.  */
753extern void __gcov_merge_delta (gcov_type *, unsigned) ATTRIBUTE_HIDDEN;
754
755/* The merge function that just ors the counters together.  */
756extern void __gcov_merge_ior (gcov_type *, unsigned) ATTRIBUTE_HIDDEN;
757
758/* The merge function used for direct call counters.  */
759extern void __gcov_merge_dc (gcov_type *, unsigned) ATTRIBUTE_HIDDEN;
760
761/* The merge function used for reuse distance counters.  */
762extern void __gcov_merge_reusedist (gcov_type *, unsigned) ATTRIBUTE_HIDDEN;
763
764/* The merge function used for indirect call counters.  */
765extern void __gcov_merge_icall_topn (gcov_type *, unsigned) ATTRIBUTE_HIDDEN;
766
767/* The profiler functions.  */
768extern void __gcov_interval_profiler (gcov_type *, gcov_type, int, unsigned);
769extern void __gcov_pow2_profiler (gcov_type *, gcov_type);
770extern void __gcov_one_value_profiler (gcov_type *, gcov_type);
771extern void __gcov_indirect_call_profiler (gcov_type *, gcov_type, void *, void *);
772extern void __gcov_indirect_call_topn_profiler (void *, void *, gcov_unsigned_t) ATTRIBUTE_HIDDEN;
773extern void __gcov_direct_call_profiler (void *, void *, gcov_unsigned_t) ATTRIBUTE_HIDDEN;
774extern void __gcov_average_profiler (gcov_type *, gcov_type);
775extern void __gcov_ior_profiler (gcov_type *, gcov_type);
776extern void __gcov_sort_n_vals (gcov_type *value_array, int n);
777
778/* Initialize/start/stop/dump performance monitoring unit (PMU) profile */
779void __gcov_init_pmu_profiler (struct gcov_pmu_info *) ATTRIBUTE_HIDDEN;
780void __gcov_start_pmu_profiler (void) ATTRIBUTE_HIDDEN;
781void __gcov_stop_pmu_profiler (void) ATTRIBUTE_HIDDEN;
782void __gcov_end_pmu_profiler (int gcda_error) ATTRIBUTE_HIDDEN;
783
784#ifndef inhibit_libc
785/* The wrappers around some library functions..  */
786extern pid_t __gcov_fork (void) ATTRIBUTE_HIDDEN;
787extern int __gcov_execl (const char *, char *, ...) ATTRIBUTE_HIDDEN;
788extern int __gcov_execlp (const char *, char *, ...) ATTRIBUTE_HIDDEN;
789extern int __gcov_execle (const char *, char *, ...) ATTRIBUTE_HIDDEN;
790extern int __gcov_execv (const char *, char *const []) ATTRIBUTE_HIDDEN;
791extern int __gcov_execvp (const char *, char *const []) ATTRIBUTE_HIDDEN;
792extern int __gcov_execve (const char *, char  *const [], char *const [])
793  ATTRIBUTE_HIDDEN;
794#endif
795
796#endif /* IN_LIBGCOV */
797
798#if IN_LIBGCOV >= 0
799
800/* Optimum number of gcov_unsigned_t's read from or written to disk.  */
801#define GCOV_BLOCK_SIZE (1 << 10)
802
803struct gcov_var
804{
805  _GCOV_FILE *file;
806  gcov_position_t start;	/* Position of first byte of block */
807  unsigned offset;		/* Read/write position within the block.  */
808  unsigned length;		/* Read limit in the block.  */
809  unsigned overread;		/* Number of words overread.  */
810  int error;			/* < 0 overflow, > 0 disk error.  */
811  int mode;	                /* < 0 writing, > 0 reading */
812#if IN_LIBGCOV
813  /* Holds one block plus 4 bytes, thus all coverage reads & writes
814     fit within this buffer and we always can transfer GCOV_BLOCK_SIZE
815     to and from the disk. libgcov never backtracks and only writes 4
816     or 8 byte objects.  */
817  gcov_unsigned_t buffer[GCOV_BLOCK_SIZE + 1];
818#else
819  int endian;			/* Swap endianness.  */
820  /* Holds a variable length block, as the compiler can write
821     strings and needs to backtrack.  */
822  size_t alloc;
823  gcov_unsigned_t *buffer;
824#endif
825};
826
827/* In kernel mode, move gcov_var definition to gcov-io.c
828   to avoid dulipcate definitions.  */
829#ifndef __GCOV_KERNEL__
830GCOV_LINKAGE struct gcov_var gcov_var ATTRIBUTE_HIDDEN;
831#else
832extern struct gcov_var gcov_var;
833#endif
834
835/* Functions for reading and writing gcov files. In libgcov you can
836   open the file for reading then writing. Elsewhere you can open the
837   file either for reading or for writing. When reading a file you may
838   use the gcov_read_* functions, gcov_sync, gcov_position, &
839   gcov_error. When writing a file you may use the gcov_write
840   functions, gcov_seek & gcov_error. When a file is to be rewritten
841   you use the functions for reading, then gcov_rewrite then the
842   functions for writing.  Your file may become corrupted if you break
843   these invariants.  */
844#if IN_LIBGCOV
845GCOV_LINKAGE int gcov_open (const char */*name*/) ATTRIBUTE_HIDDEN;
846#else
847GCOV_LINKAGE int gcov_open (const char */*name*/, int /*direction*/);
848GCOV_LINKAGE int gcov_magic (gcov_unsigned_t, gcov_unsigned_t);
849#endif
850GCOV_LINKAGE int gcov_close (void) ATTRIBUTE_HIDDEN;
851
852/* Available everywhere.  */
853static gcov_position_t gcov_position (void);
854static int gcov_is_error (void);
855
856GCOV_LINKAGE const char *gcov_read_string (void) ATTRIBUTE_HIDDEN;
857GCOV_LINKAGE gcov_unsigned_t gcov_read_unsigned (void) ATTRIBUTE_HIDDEN;
858GCOV_LINKAGE gcov_type gcov_read_counter (void) ATTRIBUTE_HIDDEN;
859GCOV_LINKAGE void gcov_read_summary (struct gcov_summary *) ATTRIBUTE_HIDDEN;
860GCOV_LINKAGE char *gcov_canonical_filename (char *filename) ATTRIBUTE_HIDDEN;
861GCOV_LINKAGE void
862gcov_read_pmu_load_latency_info (gcov_pmu_ll_info_t *ll_info,
863                                 gcov_unsigned_t len) ATTRIBUTE_HIDDEN;
864GCOV_LINKAGE void
865gcov_read_pmu_branch_mispredict_info (gcov_pmu_brm_info_t *brm_info,
866                                      gcov_unsigned_t len) ATTRIBUTE_HIDDEN;
867GCOV_LINKAGE void
868gcov_read_pmu_tool_header (gcov_pmu_tool_header_t *tool_header,
869                           gcov_unsigned_t len) ATTRIBUTE_HIDDEN;
870#ifndef __GCOV_KERNEL__
871GCOV_LINKAGE float convert_unsigned_to_pct (
872    const unsigned number) ATTRIBUTE_HIDDEN;
873#endif /* __GCOV_KERNEL__ */
874
875#if !IN_LIBGCOV && IN_GCOV != 1
876GCOV_LINKAGE void gcov_read_module_info (struct gcov_module_info *mod_info,
877					 gcov_unsigned_t len) ATTRIBUTE_HIDDEN;
878GCOV_LINKAGE void print_load_latency_line (FILE *fp,
879                                           const gcov_pmu_ll_info_t *ll_info,
880                                           const enum print_newline);
881GCOV_LINKAGE void
882print_branch_mispredict_line (FILE *fp, const gcov_pmu_brm_info_t *brm_info,
883                              const enum print_newline);
884GCOV_LINKAGE void print_pmu_tool_header (FILE *fp,
885                                         gcov_pmu_tool_header_t *tool_header,
886                                         const enum print_newline);
887#endif
888
889#if IN_GCOV != 1
890GCOV_LINKAGE void destroy_pmu_tool_header (gcov_pmu_tool_header_t *tool_header)
891  ATTRIBUTE_HIDDEN;
892#endif
893
894#if IN_LIBGCOV
895/* Available only in libgcov */
896GCOV_LINKAGE void gcov_write_counter (gcov_type) ATTRIBUTE_HIDDEN;
897GCOV_LINKAGE void gcov_write_tag_length (gcov_unsigned_t, gcov_unsigned_t)
898    ATTRIBUTE_HIDDEN;
899GCOV_LINKAGE void gcov_write_summary (gcov_unsigned_t /*tag*/,
900				      const struct gcov_summary *)
901    ATTRIBUTE_HIDDEN;
902
903GCOV_LINKAGE void gcov_write_module_infos (struct gcov_info *mod_info)
904    ATTRIBUTE_HIDDEN;
905GCOV_LINKAGE const struct dyn_imp_mod **
906gcov_get_sorted_import_module_array (struct gcov_info *mod_info, unsigned *len)
907    ATTRIBUTE_HIDDEN;
908static void gcov_rewrite (void);
909GCOV_LINKAGE void gcov_seek (gcov_position_t /*position*/) ATTRIBUTE_HIDDEN;
910GCOV_LINKAGE void gcov_truncate (void) ATTRIBUTE_HIDDEN;
911GCOV_LINKAGE gcov_unsigned_t gcov_string_length (const char *) ATTRIBUTE_HIDDEN;
912GCOV_LINKAGE unsigned gcov_gcda_file_size (struct gcov_info *);
913#else
914/* Available outside libgcov */
915GCOV_LINKAGE void gcov_sync (gcov_position_t /*base*/,
916			     gcov_unsigned_t /*length */);
917#endif
918
919#if !IN_GCOV
920/* Available outside gcov */
921GCOV_LINKAGE void gcov_write_unsigned (gcov_unsigned_t) ATTRIBUTE_HIDDEN;
922GCOV_LINKAGE void gcov_write_string (const char *) ATTRIBUTE_HIDDEN;
923#endif
924
925#if !IN_GCOV && !IN_LIBGCOV
926/* Available only in compiler */
927GCOV_LINKAGE gcov_position_t gcov_write_tag (gcov_unsigned_t);
928GCOV_LINKAGE void gcov_write_length (gcov_position_t /*position*/);
929#endif
930
931#if IN_GCOV > 0
932/* Available in gcov */
933GCOV_LINKAGE time_t gcov_time (void);
934#endif
935
936/* Save the current position in the gcov file.  */
937
938static inline gcov_position_t
939gcov_position (void)
940{
941  return gcov_var.start + gcov_var.offset;
942}
943
944/* Return nonzero if the error flag is set.  */
945
946static inline int
947gcov_is_error (void)
948{
949  return gcov_var.file ? gcov_var.error : 1;
950}
951
952#if IN_LIBGCOV
953/* Move to beginning of file and initialize for writing.  */
954
955static inline void
956gcov_rewrite (void)
957{
958  gcc_assert (gcov_var.mode > 0);
959  gcov_var.mode = -1;
960  gcov_var.start = 0;
961  gcov_var.offset = 0;
962  _GCOV_fseek (gcov_var.file, 0L, SEEK_SET);
963}
964#endif
965
966#endif /* IN_LIBGCOV >= 0 */
967
968#endif /* GCC_GCOV_IO_H */
969