gcov-io.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /* File format for coverage information
  2. Copyright (C) 1996-2019 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 notes file is
  42. file : int32:magic int32:version int32:stamp int32:support_unexecuted_blocks record*
  43. The basic format of the data file is
  44. file : int32:magic int32:version int32:stamp record*
  45. The magic ident is different for the notes and the data files. The
  46. magic ident is used to determine the endianness of the file, when
  47. reading. The version is the same for both files and is derived
  48. from gcc's version number. The stamp value is used to synchronize
  49. note and data files and to synchronize merging within a data
  50. file. It need not be an absolute time stamp, merely a ticker that
  51. increments fast enough and cycles slow enough to distinguish
  52. different compile/run/compile cycles.
  53. Although the ident and version are formally 32 bit numbers, they
  54. are derived from 4 character ASCII strings. The version number
  55. consists of a two character major version number
  56. (first digit starts from 'A' letter to not to clash with the older
  57. numbering scheme), the single character minor version number,
  58. and a single character indicating the status of the release.
  59. That will be 'e' experimental, 'p' prerelease and 'r' for release.
  60. Because, by good fortune, these are in alphabetical order, string
  61. collating can be used to compare version strings. Be aware that
  62. the 'e' designation will (naturally) be unstable and might be
  63. incompatible with itself. For gcc 17.0 experimental, it would be
  64. 'B70e' (0x42373065). As we currently do not release more than 5 minor
  65. releases, the single character should be always fine. Major number
  66. is currently changed roughly every year, which gives us space
  67. for next 250 years (maximum allowed number would be 259.9).
  68. A record has a tag, length and variable amount of data.
  69. record: header data
  70. header: int32:tag int32:length
  71. data: item*
  72. Records are not nested, but there is a record hierarchy. Tag
  73. numbers reflect this hierarchy. Tags are unique across note and
  74. data files. Some record types have a varying amount of data. The
  75. LENGTH is the number of 4bytes that follow and is usually used to
  76. determine how much data. The tag value is split into 4 8-bit
  77. fields, one for each of four possible levels. The most significant
  78. is allocated first. Unused levels are zero. Active levels are
  79. odd-valued, so that the LSB of the level is one. A sub-level
  80. incorporates the values of its superlevels. This formatting allows
  81. you to determine the tag hierarchy, without understanding the tags
  82. themselves, and is similar to the standard section numbering used
  83. in technical documents. Level values [1..3f] are used for common
  84. tags, values [41..9f] for the notes file and [a1..ff] for the data
  85. file.
  86. The notes file contains the following records
  87. note: unit function-graph*
  88. unit: header int32:checksum string:source
  89. function-graph: announce_function basic_blocks {arcs | lines}*
  90. announce_function: header int32:ident
  91. int32:lineno_checksum int32:cfg_checksum
  92. string:name string:source int32:start_lineno int32:start_column int32:end_lineno
  93. basic_block: header int32:flags*
  94. arcs: header int32:block_no arc*
  95. arc: int32:dest_block int32:flags
  96. lines: header int32:block_no line*
  97. int32:0 string:NULL
  98. line: int32:line_no | int32:0 string:filename
  99. The BASIC_BLOCK record holds per-bb flags. The number of blocks
  100. can be inferred from its data length. There is one ARCS record per
  101. basic block. The number of arcs from a bb is implicit from the
  102. data length. It enumerates the destination bb and per-arc flags.
  103. There is one LINES record per basic block, it enumerates the source
  104. lines which belong to that basic block. Source file names are
  105. introduced by a line number of 0, following lines are from the new
  106. source file. The initial source file for the function is NULL, but
  107. the current source file should be remembered from one LINES record
  108. to the next. The end of a block is indicated by an empty filename
  109. - this does not reset the current source file. Note there is no
  110. ordering of the ARCS and LINES records: they may be in any order,
  111. interleaved in any manner. The current filename follows the order
  112. the LINES records are stored in the file, *not* the ordering of the
  113. blocks they are for.
  114. The data file contains the following records.
  115. data: {unit summary:object function-data*}*
  116. unit: header int32:checksum
  117. function-data: announce_function present counts
  118. announce_function: header int32:ident
  119. int32:lineno_checksum int32:cfg_checksum
  120. present: header int32:present
  121. counts: header int64:count*
  122. summary: int32:checksum int32:runs int32:sum_max
  123. The ANNOUNCE_FUNCTION record is the same as that in the note file,
  124. but without the source location. The COUNTS gives the
  125. counter values for instrumented features. The about the whole
  126. program. The checksum is used for whole program summaries, and
  127. disambiguates different programs which include the same
  128. instrumented object file. There may be several program summaries,
  129. each with a unique checksum. The object summary's checksum is
  130. zero. Note that the data file might contain information from
  131. several runs concatenated, or the data might be merged.
  132. This file is included by both the compiler, gcov tools and the
  133. runtime support library libgcov. IN_LIBGCOV and IN_GCOV are used to
  134. distinguish which case is which. If IN_LIBGCOV is nonzero,
  135. libgcov is being built. If IN_GCOV is nonzero, the gcov tools are
  136. being built. Otherwise the compiler is being built. IN_GCOV may be
  137. positive or negative. If positive, we are compiling a tool that
  138. requires additional functions (see the code for knowledge of what
  139. those functions are). */
  140. #ifndef GCC_GCOV_IO_H
  141. #define GCC_GCOV_IO_H
  142. #ifndef IN_LIBGCOV
  143. /* About the host */
  144. typedef unsigned gcov_unsigned_t;
  145. typedef unsigned gcov_position_t;
  146. /* gcov_type is typedef'd elsewhere for the compiler */
  147. #if IN_GCOV
  148. #define GCOV_LINKAGE static
  149. typedef int64_t gcov_type;
  150. typedef uint64_t gcov_type_unsigned;
  151. #if IN_GCOV > 0
  152. #include <sys/types.h>
  153. #endif
  154. #endif
  155. #if defined (HOST_HAS_F_SETLKW)
  156. #define GCOV_LOCKED 1
  157. #else
  158. #define GCOV_LOCKED 0
  159. #endif
  160. #define ATTRIBUTE_HIDDEN
  161. #endif /* !IN_LIBGCOV */
  162. #ifndef GCOV_LINKAGE
  163. #define GCOV_LINKAGE extern
  164. #endif
  165. #if IN_LIBGCOV
  166. #define gcov_nonruntime_assert(EXPR) ((void)(0 && (EXPR)))
  167. #else
  168. #define gcov_nonruntime_assert(EXPR) gcc_assert (EXPR)
  169. #define gcov_error(...) fatal_error (input_location, __VA_ARGS__)
  170. #endif
  171. /* File suffixes. */
  172. #define GCOV_DATA_SUFFIX ".gcda"
  173. #define GCOV_NOTE_SUFFIX ".gcno"
  174. /* File magic. Must not be palindromes. */
  175. #define GCOV_DATA_MAGIC ((gcov_unsigned_t)0x67636461) /* "gcda" */
  176. #define GCOV_NOTE_MAGIC ((gcov_unsigned_t)0x67636e6f) /* "gcno" */
  177. /* gcov-iov.h is automatically generated by the makefile from
  178. version.c, it looks like
  179. #define GCOV_VERSION ((gcov_unsigned_t)0x89abcdef)
  180. */
  181. #include "gcov-iov.h"
  182. /* Convert a magic or version number to a 4 character string. */
  183. #define GCOV_UNSIGNED2STRING(ARRAY,VALUE) \
  184. ((ARRAY)[0] = (char)((VALUE) >> 24), \
  185. (ARRAY)[1] = (char)((VALUE) >> 16), \
  186. (ARRAY)[2] = (char)((VALUE) >> 8), \
  187. (ARRAY)[3] = (char)((VALUE) >> 0))
  188. /* The record tags. Values [1..3f] are for tags which may be in either
  189. file. Values [41..9f] for those in the note file and [a1..ff] for
  190. the data file. The tag value zero is used as an explicit end of
  191. file marker -- it is not required to be present. */
  192. #define GCOV_TAG_FUNCTION ((gcov_unsigned_t)0x01000000)
  193. #define GCOV_TAG_FUNCTION_LENGTH (3)
  194. #define GCOV_TAG_BLOCKS ((gcov_unsigned_t)0x01410000)
  195. #define GCOV_TAG_BLOCKS_LENGTH(NUM) (NUM)
  196. #define GCOV_TAG_ARCS ((gcov_unsigned_t)0x01430000)
  197. #define GCOV_TAG_ARCS_LENGTH(NUM) (1 + (NUM) * 2)
  198. #define GCOV_TAG_ARCS_NUM(LENGTH) (((LENGTH) - 1) / 2)
  199. #define GCOV_TAG_LINES ((gcov_unsigned_t)0x01450000)
  200. #define GCOV_TAG_COUNTER_BASE ((gcov_unsigned_t)0x01a10000)
  201. #define GCOV_TAG_COUNTER_LENGTH(NUM) ((NUM) * 2)
  202. #define GCOV_TAG_COUNTER_NUM(LENGTH) ((LENGTH) / 2)
  203. #define GCOV_TAG_OBJECT_SUMMARY ((gcov_unsigned_t)0xa1000000)
  204. #define GCOV_TAG_PROGRAM_SUMMARY ((gcov_unsigned_t)0xa3000000) /* Obsolete */
  205. #define GCOV_TAG_SUMMARY_LENGTH (2)
  206. #define GCOV_TAG_AFDO_FILE_NAMES ((gcov_unsigned_t)0xaa000000)
  207. #define GCOV_TAG_AFDO_FUNCTION ((gcov_unsigned_t)0xac000000)
  208. #define GCOV_TAG_AFDO_WORKING_SET ((gcov_unsigned_t)0xaf000000)
  209. /* Counters that are collected. */
  210. #define DEF_GCOV_COUNTER(COUNTER, NAME, MERGE_FN) COUNTER,
  211. enum {
  212. #include "gcov-counter.def"
  213. GCOV_COUNTERS
  214. };
  215. #undef DEF_GCOV_COUNTER
  216. /* The first of counters used for value profiling. They must form a
  217. consecutive interval and their order must match the order of
  218. HIST_TYPEs in value-prof.h. */
  219. #define GCOV_FIRST_VALUE_COUNTER GCOV_COUNTER_V_INTERVAL
  220. /* The last of counters used for value profiling. */
  221. #define GCOV_LAST_VALUE_COUNTER (GCOV_COUNTERS - 1)
  222. /* Number of counters used for value profiling. */
  223. #define GCOV_N_VALUE_COUNTERS \
  224. (GCOV_LAST_VALUE_COUNTER - GCOV_FIRST_VALUE_COUNTER + 1)
  225. /* The number of hottest callees to be tracked. */
  226. #define GCOV_ICALL_TOPN_VAL 2
  227. /* The number of counter entries per icall callsite. */
  228. #define GCOV_ICALL_TOPN_NCOUNTS (1 + GCOV_ICALL_TOPN_VAL * 4)
  229. /* Convert a counter index to a tag. */
  230. #define GCOV_TAG_FOR_COUNTER(COUNT) \
  231. (GCOV_TAG_COUNTER_BASE + ((gcov_unsigned_t)(COUNT) << 17))
  232. /* Convert a tag to a counter. */
  233. #define GCOV_COUNTER_FOR_TAG(TAG) \
  234. ((unsigned)(((TAG) - GCOV_TAG_COUNTER_BASE) >> 17))
  235. /* Check whether a tag is a counter tag. */
  236. #define GCOV_TAG_IS_COUNTER(TAG) \
  237. (!((TAG) & 0xFFFF) && GCOV_COUNTER_FOR_TAG (TAG) < GCOV_COUNTERS)
  238. /* The tag level mask has 1's in the position of the inner levels, &
  239. the lsb of the current level, and zero on the current and outer
  240. levels. */
  241. #define GCOV_TAG_MASK(TAG) (((TAG) - 1) ^ (TAG))
  242. /* Return nonzero if SUB is an immediate subtag of TAG. */
  243. #define GCOV_TAG_IS_SUBTAG(TAG,SUB) \
  244. (GCOV_TAG_MASK (TAG) >> 8 == GCOV_TAG_MASK (SUB) \
  245. && !(((SUB) ^ (TAG)) & ~GCOV_TAG_MASK (TAG)))
  246. /* Return nonzero if SUB is at a sublevel to TAG. */
  247. #define GCOV_TAG_IS_SUBLEVEL(TAG,SUB) \
  248. (GCOV_TAG_MASK (TAG) > GCOV_TAG_MASK (SUB))
  249. /* Basic block flags. */
  250. #define GCOV_BLOCK_UNEXPECTED (1 << 1)
  251. /* Arc flags. */
  252. #define GCOV_ARC_ON_TREE (1 << 0)
  253. #define GCOV_ARC_FAKE (1 << 1)
  254. #define GCOV_ARC_FALLTHROUGH (1 << 2)
  255. /* Object & program summary record. */
  256. struct gcov_summary
  257. {
  258. gcov_unsigned_t runs; /* Number of program runs. */
  259. gcov_type sum_max; /* Sum of individual run max values. */
  260. };
  261. #if !defined(inhibit_libc)
  262. /* Functions for reading and writing gcov files. In libgcov you can
  263. open the file for reading then writing. Elsewhere you can open the
  264. file either for reading or for writing. When reading a file you may
  265. use the gcov_read_* functions, gcov_sync, gcov_position, &
  266. gcov_error. When writing a file you may use the gcov_write
  267. functions, gcov_seek & gcov_error. When a file is to be rewritten
  268. you use the functions for reading, then gcov_rewrite then the
  269. functions for writing. Your file may become corrupted if you break
  270. these invariants. */
  271. #if !IN_LIBGCOV
  272. GCOV_LINKAGE int gcov_open (const char */*name*/, int /*direction*/);
  273. GCOV_LINKAGE int gcov_magic (gcov_unsigned_t, gcov_unsigned_t);
  274. #endif
  275. /* Available everywhere. */
  276. GCOV_LINKAGE int gcov_close (void) ATTRIBUTE_HIDDEN;
  277. GCOV_LINKAGE gcov_unsigned_t gcov_read_unsigned (void) ATTRIBUTE_HIDDEN;
  278. GCOV_LINKAGE gcov_type gcov_read_counter (void) ATTRIBUTE_HIDDEN;
  279. GCOV_LINKAGE void gcov_read_summary (struct gcov_summary *) ATTRIBUTE_HIDDEN;
  280. GCOV_LINKAGE const char *gcov_read_string (void);
  281. GCOV_LINKAGE void gcov_sync (gcov_position_t /*base*/,
  282. gcov_unsigned_t /*length */);
  283. char *mangle_path (char const *base);
  284. #if !IN_GCOV
  285. /* Available outside gcov */
  286. GCOV_LINKAGE void gcov_write_unsigned (gcov_unsigned_t) ATTRIBUTE_HIDDEN;
  287. #endif
  288. #if !IN_GCOV && !IN_LIBGCOV
  289. /* Available only in compiler */
  290. GCOV_LINKAGE void gcov_write_string (const char *);
  291. GCOV_LINKAGE void gcov_write_filename (const char *);
  292. GCOV_LINKAGE gcov_position_t gcov_write_tag (gcov_unsigned_t);
  293. GCOV_LINKAGE void gcov_write_length (gcov_position_t /*position*/);
  294. #endif
  295. #if IN_GCOV > 0
  296. /* Available in gcov */
  297. GCOV_LINKAGE time_t gcov_time (void);
  298. #endif
  299. #endif /* !inhibit_libc */
  300. #endif /* GCC_GCOV_IO_H */