gcov-io.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /* File format for coverage information
  2. Copyright (C) 1996-2015 Free Software Foundation, Inc.
  3. Contributed by Bob Manson <manson@cygnus.com>.
  4. Completely remangled by Nathan Sidwell <nathan@codesourcery.com>.
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License as published by the Free
  8. Software Foundation; either version 3, or (at your option) any later
  9. version.
  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. Under Section 7 of GPL version 3, you are granted additional
  15. permissions described in the GCC Runtime Library Exception, version
  16. 3.1, as published by the Free Software Foundation.
  17. You should have received a copy of the GNU General Public License and
  18. a copy of the GCC Runtime Library Exception along with this program;
  19. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  20. <http://www.gnu.org/licenses/>. */
  21. /* Coverage information is held in two files. A notes file, which is
  22. generated by the compiler, and a data file, which is generated by
  23. the program under test. Both files use a similar structure. We do
  24. not attempt to make these files backwards compatible with previous
  25. versions, as you only need coverage information when developing a
  26. program. We do hold version information, so that mismatches can be
  27. detected, and we use a format that allows tools to skip information
  28. they do not understand or are not interested in.
  29. Numbers are recorded in the 32 bit unsigned binary form of the
  30. endianness of the machine generating the file. 64 bit numbers are
  31. stored as two 32 bit numbers, the low part first. Strings are
  32. padded with 1 to 4 NUL bytes, to bring the length up to a multiple
  33. of 4. The number of 4 bytes is stored, followed by the padded
  34. string. Zero length and NULL strings are simply stored as a length
  35. of zero (they have no trailing NUL or padding).
  36. int32: byte3 byte2 byte1 byte0 | byte0 byte1 byte2 byte3
  37. int64: int32:low int32:high
  38. string: int32:0 | int32:length char* char:0 padding
  39. padding: | char:0 | char:0 char:0 | char:0 char:0 char:0
  40. item: int32 | int64 | string
  41. The basic format of the files is
  42. file : int32:magic int32:version int32:stamp record*
  43. The magic ident is different for the notes and the data files. The
  44. magic ident is used to determine the endianness of the file, when
  45. reading. The version is the same for both files and is derived
  46. from gcc's version number. The stamp value is used to synchronize
  47. note and data files and to synchronize merging within a data
  48. file. It need not be an absolute time stamp, merely a ticker that
  49. increments fast enough and cycles slow enough to distinguish
  50. different compile/run/compile cycles.
  51. Although the ident and version are formally 32 bit numbers, they
  52. are derived from 4 character ASCII strings. The version number
  53. consists of the single character major version number, a two
  54. character minor version number (leading zero for versions less than
  55. 10), and a single character indicating the status of the release.
  56. That will be 'e' experimental, 'p' prerelease and 'r' for release.
  57. Because, by good fortune, these are in alphabetical order, string
  58. collating can be used to compare version strings. Be aware that
  59. the 'e' designation will (naturally) be unstable and might be
  60. incompatible with itself. For gcc 3.4 experimental, it would be
  61. '304e' (0x33303465). When the major version reaches 10, the
  62. letters A-Z will be used. Assuming minor increments releases every
  63. 6 months, we have to make a major increment every 50 years.
  64. Assuming major increments releases every 5 years, we're ok for the
  65. next 155 years -- good enough for me.
  66. A record has a tag, length and variable amount of data.
  67. record: header data
  68. header: int32:tag int32:length
  69. data: item*
  70. Records are not nested, but there is a record hierarchy. Tag
  71. numbers reflect this hierarchy. Tags are unique across note and
  72. data files. Some record types have a varying amount of data. The
  73. LENGTH is the number of 4bytes that follow and is usually used to
  74. determine how much data. The tag value is split into 4 8-bit
  75. fields, one for each of four possible levels. The most significant
  76. is allocated first. Unused levels are zero. Active levels are
  77. odd-valued, so that the LSB of the level is one. A sub-level
  78. incorporates the values of its superlevels. This formatting allows
  79. you to determine the tag hierarchy, without understanding the tags
  80. themselves, and is similar to the standard section numbering used
  81. in technical documents. Level values [1..3f] are used for common
  82. tags, values [41..9f] for the notes file and [a1..ff] for the data
  83. file.
  84. The notes file contains the following records
  85. note: unit function-graph*
  86. unit: header int32:checksum string:source
  87. function-graph: announce_function basic_blocks {arcs | lines}*
  88. announce_function: header int32:ident
  89. int32:lineno_checksum int32:cfg_checksum
  90. string:name string:source int32:lineno
  91. basic_block: header int32:flags*
  92. arcs: header int32:block_no arc*
  93. arc: int32:dest_block int32:flags
  94. lines: header int32:block_no line*
  95. int32:0 string:NULL
  96. line: int32:line_no | int32:0 string:filename
  97. The BASIC_BLOCK record holds per-bb flags. The number of blocks
  98. can be inferred from its data length. There is one ARCS record per
  99. basic block. The number of arcs from a bb is implicit from the
  100. data length. It enumerates the destination bb and per-arc flags.
  101. There is one LINES record per basic block, it enumerates the source
  102. lines which belong to that basic block. Source file names are
  103. introduced by a line number of 0, following lines are from the new
  104. source file. The initial source file for the function is NULL, but
  105. the current source file should be remembered from one LINES record
  106. to the next. The end of a block is indicated by an empty filename
  107. - this does not reset the current source file. Note there is no
  108. ordering of the ARCS and LINES records: they may be in any order,
  109. interleaved in any manner. The current filename follows the order
  110. the LINES records are stored in the file, *not* the ordering of the
  111. blocks they are for.
  112. The data file contains the following records.
  113. data: {unit summary:object summary:program* function-data*}*
  114. unit: header int32:checksum
  115. function-data: announce_function present counts
  116. announce_function: header int32:ident
  117. int32:lineno_checksum int32:cfg_checksum
  118. present: header int32:present
  119. counts: header int64:count*
  120. summary: int32:checksum {count-summary}GCOV_COUNTERS_SUMMABLE
  121. count-summary: int32:num int32:runs int64:sum
  122. int64:max int64:sum_max histogram
  123. histogram: {int32:bitvector}8 histogram-buckets*
  124. histogram-buckets: int32:num int64:min int64:sum
  125. The ANNOUNCE_FUNCTION record is the same as that in the note file,
  126. but without the source location. The COUNTS gives the
  127. counter values for instrumented features. The about the whole
  128. program. The checksum is used for whole program summaries, and
  129. disambiguates different programs which include the same
  130. instrumented object file. There may be several program summaries,
  131. each with a unique checksum. The object summary's checksum is
  132. zero. Note that the data file might contain information from
  133. several runs concatenated, or the data might be merged.
  134. This file is included by both the compiler, gcov tools and the
  135. runtime support library libgcov. IN_LIBGCOV and IN_GCOV are used to
  136. distinguish which case is which. If IN_LIBGCOV is nonzero,
  137. libgcov is being built. If IN_GCOV is nonzero, the gcov tools are
  138. being built. Otherwise the compiler is being built. IN_GCOV may be
  139. positive or negative. If positive, we are compiling a tool that
  140. requires additional functions (see the code for knowledge of what
  141. those functions are). */
  142. #ifndef GCC_GCOV_IO_H
  143. #define GCC_GCOV_IO_H
  144. #ifndef IN_LIBGCOV
  145. /* About the host */
  146. typedef unsigned gcov_unsigned_t;
  147. typedef unsigned gcov_position_t;
  148. /* gcov_type is typedef'd elsewhere for the compiler */
  149. #if IN_GCOV
  150. #define GCOV_LINKAGE static
  151. typedef int64_t gcov_type;
  152. typedef uint64_t gcov_type_unsigned;
  153. #if IN_GCOV > 0
  154. #include <sys/types.h>
  155. #endif
  156. #else /*!IN_GCOV */
  157. #define GCOV_TYPE_SIZE (LONG_LONG_TYPE_SIZE > 32 ? 64 : 32)
  158. #endif
  159. #if defined (HOST_HAS_F_SETLKW)
  160. #define GCOV_LOCKED 1
  161. #else
  162. #define GCOV_LOCKED 0
  163. #endif
  164. #define ATTRIBUTE_HIDDEN
  165. #endif /* !IN_LIBGOCV */
  166. #ifndef GCOV_LINKAGE
  167. #define GCOV_LINKAGE extern
  168. #endif
  169. #if IN_LIBGCOV
  170. #define gcov_nonruntime_assert(EXPR) ((void)(0 && (EXPR)))
  171. #else
  172. #define gcov_nonruntime_assert(EXPR) gcc_assert (EXPR)
  173. #define gcov_error(...) fatal_error (input_location, __VA_ARGS__)
  174. #endif
  175. /* File suffixes. */
  176. #define GCOV_DATA_SUFFIX ".gcda"
  177. #define GCOV_NOTE_SUFFIX ".gcno"
  178. /* File magic. Must not be palindromes. */
  179. #define GCOV_DATA_MAGIC ((gcov_unsigned_t)0x67636461) /* "gcda" */
  180. #define GCOV_NOTE_MAGIC ((gcov_unsigned_t)0x67636e6f) /* "gcno" */
  181. /* gcov-iov.h is automatically generated by the makefile from
  182. version.c, it looks like
  183. #define GCOV_VERSION ((gcov_unsigned_t)0x89abcdef)
  184. */
  185. #include "gcov-iov.h"
  186. /* Convert a magic or version number to a 4 character string. */
  187. #define GCOV_UNSIGNED2STRING(ARRAY,VALUE) \
  188. ((ARRAY)[0] = (char)((VALUE) >> 24), \
  189. (ARRAY)[1] = (char)((VALUE) >> 16), \
  190. (ARRAY)[2] = (char)((VALUE) >> 8), \
  191. (ARRAY)[3] = (char)((VALUE) >> 0))
  192. /* The record tags. Values [1..3f] are for tags which may be in either
  193. file. Values [41..9f] for those in the note file and [a1..ff] for
  194. the data file. The tag value zero is used as an explicit end of
  195. file marker -- it is not required to be present. */
  196. #define GCOV_TAG_FUNCTION ((gcov_unsigned_t)0x01000000)
  197. #define GCOV_TAG_FUNCTION_LENGTH (3)
  198. #define GCOV_TAG_BLOCKS ((gcov_unsigned_t)0x01410000)
  199. #define GCOV_TAG_BLOCKS_LENGTH(NUM) (NUM)
  200. #define GCOV_TAG_BLOCKS_NUM(LENGTH) (LENGTH)
  201. #define GCOV_TAG_ARCS ((gcov_unsigned_t)0x01430000)
  202. #define GCOV_TAG_ARCS_LENGTH(NUM) (1 + (NUM) * 2)
  203. #define GCOV_TAG_ARCS_NUM(LENGTH) (((LENGTH) - 1) / 2)
  204. #define GCOV_TAG_LINES ((gcov_unsigned_t)0x01450000)
  205. #define GCOV_TAG_COUNTER_BASE ((gcov_unsigned_t)0x01a10000)
  206. #define GCOV_TAG_COUNTER_LENGTH(NUM) ((NUM) * 2)
  207. #define GCOV_TAG_COUNTER_NUM(LENGTH) ((LENGTH) / 2)
  208. #define GCOV_TAG_OBJECT_SUMMARY ((gcov_unsigned_t)0xa1000000) /* Obsolete */
  209. #define GCOV_TAG_PROGRAM_SUMMARY ((gcov_unsigned_t)0xa3000000)
  210. #define GCOV_TAG_SUMMARY_LENGTH(NUM) \
  211. (1 + GCOV_COUNTERS_SUMMABLE * (10 + 3 * 2) + (NUM) * 5)
  212. #define GCOV_TAG_AFDO_FILE_NAMES ((gcov_unsigned_t)0xaa000000)
  213. #define GCOV_TAG_AFDO_FUNCTION ((gcov_unsigned_t)0xac000000)
  214. #define GCOV_TAG_AFDO_WORKING_SET ((gcov_unsigned_t)0xaf000000)
  215. /* Counters that are collected. */
  216. #define DEF_GCOV_COUNTER(COUNTER, NAME, MERGE_FN) COUNTER,
  217. enum {
  218. #include "gcov-counter.def"
  219. GCOV_COUNTERS
  220. };
  221. #undef DEF_GCOV_COUNTER
  222. /* Counters which can be summaried. */
  223. #define GCOV_COUNTERS_SUMMABLE (GCOV_COUNTER_ARCS + 1)
  224. /* The first of counters used for value profiling. They must form a
  225. consecutive interval and their order must match the order of
  226. HIST_TYPEs in value-prof.h. */
  227. #define GCOV_FIRST_VALUE_COUNTER GCOV_COUNTERS_SUMMABLE
  228. /* The last of counters used for value profiling. */
  229. #define GCOV_LAST_VALUE_COUNTER (GCOV_COUNTERS - 1)
  230. /* Number of counters used for value profiling. */
  231. #define GCOV_N_VALUE_COUNTERS \
  232. (GCOV_LAST_VALUE_COUNTER - GCOV_FIRST_VALUE_COUNTER + 1)
  233. /* The number of hottest callees to be tracked. */
  234. #define GCOV_ICALL_TOPN_VAL 2
  235. /* The number of counter entries per icall callsite. */
  236. #define GCOV_ICALL_TOPN_NCOUNTS (1 + GCOV_ICALL_TOPN_VAL * 4)
  237. /* Convert a counter index to a tag. */
  238. #define GCOV_TAG_FOR_COUNTER(COUNT) \
  239. (GCOV_TAG_COUNTER_BASE + ((gcov_unsigned_t)(COUNT) << 17))
  240. /* Convert a tag to a counter. */
  241. #define GCOV_COUNTER_FOR_TAG(TAG) \
  242. ((unsigned)(((TAG) - GCOV_TAG_COUNTER_BASE) >> 17))
  243. /* Check whether a tag is a counter tag. */
  244. #define GCOV_TAG_IS_COUNTER(TAG) \
  245. (!((TAG) & 0xFFFF) && GCOV_COUNTER_FOR_TAG (TAG) < GCOV_COUNTERS)
  246. /* The tag level mask has 1's in the position of the inner levels, &
  247. the lsb of the current level, and zero on the current and outer
  248. levels. */
  249. #define GCOV_TAG_MASK(TAG) (((TAG) - 1) ^ (TAG))
  250. /* Return nonzero if SUB is an immediate subtag of TAG. */
  251. #define GCOV_TAG_IS_SUBTAG(TAG,SUB) \
  252. (GCOV_TAG_MASK (TAG) >> 8 == GCOV_TAG_MASK (SUB) \
  253. && !(((SUB) ^ (TAG)) & ~GCOV_TAG_MASK (TAG)))
  254. /* Return nonzero if SUB is at a sublevel to TAG. */
  255. #define GCOV_TAG_IS_SUBLEVEL(TAG,SUB) \
  256. (GCOV_TAG_MASK (TAG) > GCOV_TAG_MASK (SUB))
  257. /* Basic block flags. */
  258. #define GCOV_BLOCK_UNEXPECTED (1 << 1)
  259. /* Arc flags. */
  260. #define GCOV_ARC_ON_TREE (1 << 0)
  261. #define GCOV_ARC_FAKE (1 << 1)
  262. #define GCOV_ARC_FALLTHROUGH (1 << 2)
  263. /* Structured records. */
  264. /* Structure used for each bucket of the log2 histogram of counter values. */
  265. typedef struct
  266. {
  267. /* Number of counters whose profile count falls within the bucket. */
  268. gcov_unsigned_t num_counters;
  269. /* Smallest profile count included in this bucket. */
  270. gcov_type min_value;
  271. /* Cumulative value of the profile counts in this bucket. */
  272. gcov_type cum_value;
  273. } gcov_bucket_type;
  274. /* For a log2 scale histogram with each range split into 4
  275. linear sub-ranges, there will be at most 64 (max gcov_type bit size) - 1 log2
  276. ranges since the lowest 2 log2 values share the lowest 4 linear
  277. sub-range (values 0 - 3). This is 252 total entries (63*4). */
  278. #define GCOV_HISTOGRAM_SIZE 252
  279. /* How many unsigned ints are required to hold a bit vector of non-zero
  280. histogram entries when the histogram is written to the gcov file.
  281. This is essentially a ceiling divide by 32 bits. */
  282. #define GCOV_HISTOGRAM_BITVECTOR_SIZE (GCOV_HISTOGRAM_SIZE + 31) / 32
  283. /* Cumulative counter data. */
  284. struct gcov_ctr_summary
  285. {
  286. gcov_unsigned_t num; /* number of counters. */
  287. gcov_unsigned_t runs; /* number of program runs */
  288. gcov_type sum_all; /* sum of all counters accumulated. */
  289. gcov_type run_max; /* maximum value on a single run. */
  290. gcov_type sum_max; /* sum of individual run max values. */
  291. gcov_bucket_type histogram[GCOV_HISTOGRAM_SIZE]; /* histogram of
  292. counter values. */
  293. };
  294. /* Object & program summary record. */
  295. struct gcov_summary
  296. {
  297. gcov_unsigned_t checksum; /* checksum of program */
  298. struct gcov_ctr_summary ctrs[GCOV_COUNTERS_SUMMABLE];
  299. };
  300. #if !defined(inhibit_libc)
  301. /* Functions for reading and writing gcov files. In libgcov you can
  302. open the file for reading then writing. Elsewhere you can open the
  303. file either for reading or for writing. When reading a file you may
  304. use the gcov_read_* functions, gcov_sync, gcov_position, &
  305. gcov_error. When writing a file you may use the gcov_write
  306. functions, gcov_seek & gcov_error. When a file is to be rewritten
  307. you use the functions for reading, then gcov_rewrite then the
  308. functions for writing. Your file may become corrupted if you break
  309. these invariants. */
  310. #if !IN_LIBGCOV
  311. GCOV_LINKAGE int gcov_open (const char */*name*/, int /*direction*/);
  312. GCOV_LINKAGE int gcov_magic (gcov_unsigned_t, gcov_unsigned_t);
  313. #endif
  314. /* Available everywhere. */
  315. GCOV_LINKAGE int gcov_close (void) ATTRIBUTE_HIDDEN;
  316. GCOV_LINKAGE gcov_unsigned_t gcov_read_unsigned (void) ATTRIBUTE_HIDDEN;
  317. GCOV_LINKAGE gcov_type gcov_read_counter (void) ATTRIBUTE_HIDDEN;
  318. GCOV_LINKAGE void gcov_read_summary (struct gcov_summary *) ATTRIBUTE_HIDDEN;
  319. GCOV_LINKAGE const char *gcov_read_string (void);
  320. GCOV_LINKAGE void gcov_sync (gcov_position_t /*base*/,
  321. gcov_unsigned_t /*length */);
  322. #if !IN_GCOV
  323. /* Available outside gcov */
  324. GCOV_LINKAGE void gcov_write_unsigned (gcov_unsigned_t) ATTRIBUTE_HIDDEN;
  325. #endif
  326. #if !IN_GCOV && !IN_LIBGCOV
  327. /* Available only in compiler */
  328. GCOV_LINKAGE unsigned gcov_histo_index (gcov_type value);
  329. GCOV_LINKAGE void gcov_write_string (const char *);
  330. GCOV_LINKAGE gcov_position_t gcov_write_tag (gcov_unsigned_t);
  331. GCOV_LINKAGE void gcov_write_length (gcov_position_t /*position*/);
  332. #endif
  333. #if IN_GCOV <= 0 && !IN_LIBGCOV
  334. /* Available in gcov-dump and the compiler. */
  335. /* Number of data points in the working set summary array. Using 128
  336. provides information for at least every 1% increment of the total
  337. profile size. The last entry is hardwired to 99.9% of the total. */
  338. #define NUM_GCOV_WORKING_SETS 128
  339. /* Working set size statistics for a given percentage of the entire
  340. profile (sum_all from the counter summary). */
  341. typedef struct gcov_working_set_info
  342. {
  343. /* Number of hot counters included in this working set. */
  344. unsigned num_counters;
  345. /* Smallest counter included in this working set. */
  346. gcov_type min_counter;
  347. } gcov_working_set_t;
  348. GCOV_LINKAGE void compute_working_sets (const struct gcov_ctr_summary *summary,
  349. gcov_working_set_t *gcov_working_sets);
  350. #endif
  351. #if IN_GCOV > 0
  352. /* Available in gcov */
  353. GCOV_LINKAGE time_t gcov_time (void);
  354. #endif
  355. #endif /* !inhibit_libc */
  356. #endif /* GCC_GCOV_IO_H */