libgcov.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /* Header file for libgcov-*.c.
  2. Copyright (C) 1996-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #ifndef GCC_LIBGCOV_H
  20. #define GCC_LIBGCOV_H
  21. /* work around the poisoned malloc/calloc in system.h. */
  22. #ifndef xmalloc
  23. #define xmalloc malloc
  24. #endif
  25. #ifndef xcalloc
  26. #define xcalloc calloc
  27. #endif
  28. #ifndef IN_GCOV_TOOL
  29. /* About the target. */
  30. /* This path will be used by libgcov runtime. */
  31. #include "tconfig.h"
  32. #include "tsystem.h"
  33. #include "coretypes.h"
  34. #include "tm.h"
  35. #include "libgcc_tm.h"
  36. #if BITS_PER_UNIT == 8
  37. typedef unsigned gcov_unsigned_t __attribute__ ((mode (SI)));
  38. typedef unsigned gcov_position_t __attribute__ ((mode (SI)));
  39. #if LONG_LONG_TYPE_SIZE > 32
  40. typedef signed gcov_type __attribute__ ((mode (DI)));
  41. typedef unsigned gcov_type_unsigned __attribute__ ((mode (DI)));
  42. #else
  43. typedef signed gcov_type __attribute__ ((mode (SI)));
  44. typedef unsigned gcov_type_unsigned __attribute__ ((mode (SI)));
  45. #endif
  46. #else
  47. #if BITS_PER_UNIT == 16
  48. typedef unsigned gcov_unsigned_t __attribute__ ((mode (HI)));
  49. typedef unsigned gcov_position_t __attribute__ ((mode (HI)));
  50. #if LONG_LONG_TYPE_SIZE > 32
  51. typedef signed gcov_type __attribute__ ((mode (SI)));
  52. typedef unsigned gcov_type_unsigned __attribute__ ((mode (SI)));
  53. #else
  54. typedef signed gcov_type __attribute__ ((mode (HI)));
  55. typedef unsigned gcov_type_unsigned __attribute__ ((mode (HI)));
  56. #endif
  57. #else
  58. typedef unsigned gcov_unsigned_t __attribute__ ((mode (QI)));
  59. typedef unsigned gcov_position_t __attribute__ ((mode (QI)));
  60. #if LONG_LONG_TYPE_SIZE > 32
  61. typedef signed gcov_type __attribute__ ((mode (HI)));
  62. typedef unsigned gcov_type_unsigned __attribute__ ((mode (HI)));
  63. #else
  64. typedef signed gcov_type __attribute__ ((mode (QI)));
  65. typedef unsigned gcov_type_unsigned __attribute__ ((mode (QI)));
  66. #endif
  67. #endif
  68. #endif
  69. #if defined (TARGET_POSIX_IO)
  70. #define GCOV_LOCKED 1
  71. #else
  72. #define GCOV_LOCKED 0
  73. #endif
  74. /* In libgcov we need these functions to be extern, so prefix them with
  75. __gcov. In libgcov they must also be hidden so that the instance in
  76. the executable is not also used in a DSO. */
  77. #define gcov_var __gcov_var
  78. #define gcov_open __gcov_open
  79. #define gcov_close __gcov_close
  80. #define gcov_write_tag_length __gcov_write_tag_length
  81. #define gcov_position __gcov_position
  82. #define gcov_seek __gcov_seek
  83. #define gcov_rewrite __gcov_rewrite
  84. #define gcov_is_error __gcov_is_error
  85. #define gcov_write_unsigned __gcov_write_unsigned
  86. #define gcov_write_counter __gcov_write_counter
  87. #define gcov_write_summary __gcov_write_summary
  88. #define gcov_read_unsigned __gcov_read_unsigned
  89. #define gcov_read_counter __gcov_read_counter
  90. #define gcov_read_summary __gcov_read_summary
  91. #define gcov_sort_n_vals __gcov_sort_n_vals
  92. #else /* IN_GCOV_TOOL */
  93. /* About the host. */
  94. /* This path will be compiled for the host and linked into
  95. gcov-tool binary. */
  96. #include "config.h"
  97. #include "system.h"
  98. #include "coretypes.h"
  99. #include "tm.h"
  100. typedef unsigned gcov_unsigned_t;
  101. typedef unsigned gcov_position_t;
  102. /* gcov_type is typedef'd elsewhere for the compiler */
  103. #if defined (HOST_HAS_F_SETLKW)
  104. #define GCOV_LOCKED 1
  105. #else
  106. #define GCOV_LOCKED 0
  107. #endif
  108. /* Some Macros specific to gcov-tool. */
  109. #define L_gcov 1
  110. #define L_gcov_merge_add 1
  111. #define L_gcov_merge_single 1
  112. #define L_gcov_merge_delta 1
  113. #define L_gcov_merge_ior 1
  114. #define L_gcov_merge_time_profile 1
  115. #define L_gcov_merge_icall_topn 1
  116. extern gcov_type gcov_read_counter_mem ();
  117. extern unsigned gcov_get_merge_weight ();
  118. extern struct gcov_info *gcov_list;
  119. #endif /* !IN_GCOV_TOOL */
  120. #if defined(inhibit_libc)
  121. #define IN_LIBGCOV (-1)
  122. #else
  123. #define IN_LIBGCOV 1
  124. #if defined(L_gcov)
  125. #define GCOV_LINKAGE /* nothing */
  126. #endif
  127. #endif
  128. /* Poison these, so they don't accidentally slip in. */
  129. #pragma GCC poison gcov_write_string gcov_write_tag gcov_write_length
  130. #pragma GCC poison gcov_time gcov_magic
  131. #ifdef HAVE_GAS_HIDDEN
  132. #define ATTRIBUTE_HIDDEN __attribute__ ((__visibility__ ("hidden")))
  133. #else
  134. #define ATTRIBUTE_HIDDEN
  135. #endif
  136. #include "gcov-io.h"
  137. /* Structures embedded in coveraged program. The structures generated
  138. by write_profile must match these. */
  139. /* Information about counters for a single function. */
  140. struct gcov_ctr_info
  141. {
  142. gcov_unsigned_t num; /* number of counters. */
  143. gcov_type *values; /* their values. */
  144. };
  145. /* Information about a single function. This uses the trailing array
  146. idiom. The number of counters is determined from the merge pointer
  147. array in gcov_info. The key is used to detect which of a set of
  148. comdat functions was selected -- it points to the gcov_info object
  149. of the object file containing the selected comdat function. */
  150. struct gcov_fn_info
  151. {
  152. const struct gcov_info *key; /* comdat key */
  153. gcov_unsigned_t ident; /* unique ident of function */
  154. gcov_unsigned_t lineno_checksum; /* function lineo_checksum */
  155. gcov_unsigned_t cfg_checksum; /* function cfg checksum */
  156. struct gcov_ctr_info ctrs[1]; /* instrumented counters */
  157. };
  158. /* Type of function used to merge counters. */
  159. typedef void (*gcov_merge_fn) (gcov_type *, gcov_unsigned_t);
  160. /* Information about a single object file. */
  161. struct gcov_info
  162. {
  163. gcov_unsigned_t version; /* expected version number */
  164. struct gcov_info *next; /* link to next, used by libgcov */
  165. gcov_unsigned_t stamp; /* uniquifying time stamp */
  166. const char *filename; /* output file name */
  167. gcov_merge_fn merge[GCOV_COUNTERS]; /* merge functions (null for
  168. unused) */
  169. unsigned n_functions; /* number of functions */
  170. #ifndef IN_GCOV_TOOL
  171. const struct gcov_fn_info *const *functions; /* pointer to pointers
  172. to function information */
  173. #else
  174. const struct gcov_fn_info **functions;
  175. #endif /* !IN_GCOV_TOOL */
  176. };
  177. /* Root of a program/shared-object state */
  178. struct gcov_root
  179. {
  180. struct gcov_info *list;
  181. unsigned dumped : 1; /* counts have been dumped. */
  182. unsigned run_counted : 1; /* run has been accounted for. */
  183. struct gcov_root *next;
  184. struct gcov_root *prev;
  185. };
  186. extern struct gcov_root __gcov_root ATTRIBUTE_HIDDEN;
  187. struct gcov_master
  188. {
  189. gcov_unsigned_t version;
  190. struct gcov_root *root;
  191. };
  192. /* Exactly one of these will be active in the process. */
  193. extern struct gcov_master __gcov_master;
  194. /* Dump a set of gcov objects. */
  195. extern void __gcov_dump_one (struct gcov_root *) ATTRIBUTE_HIDDEN;
  196. /* Register a new object file module. */
  197. extern void __gcov_init (struct gcov_info *) ATTRIBUTE_HIDDEN;
  198. /* Called before fork, to avoid double counting. */
  199. extern void __gcov_flush (void) ATTRIBUTE_HIDDEN;
  200. /* Function to reset all counters to 0. Both externally visible (and
  201. overridable) and internal version. */
  202. extern void __gcov_reset (void);
  203. extern void __gcov_reset_int (void) ATTRIBUTE_HIDDEN;
  204. /* User function to enable early write of profile information so far. */
  205. extern void __gcov_dump (void);
  206. extern void __gcov_dump_int (void) ATTRIBUTE_HIDDEN;
  207. /* The merge function that just sums the counters. */
  208. extern void __gcov_merge_add (gcov_type *, unsigned) ATTRIBUTE_HIDDEN;
  209. /* The merge function to select the minimum valid counter value. */
  210. extern void __gcov_merge_time_profile (gcov_type *, unsigned) ATTRIBUTE_HIDDEN;
  211. /* The merge function to choose the most common value. */
  212. extern void __gcov_merge_single (gcov_type *, unsigned) ATTRIBUTE_HIDDEN;
  213. /* The merge function to choose the most common difference between
  214. consecutive values. */
  215. extern void __gcov_merge_delta (gcov_type *, unsigned) ATTRIBUTE_HIDDEN;
  216. /* The merge function that just ors the counters together. */
  217. extern void __gcov_merge_ior (gcov_type *, unsigned) ATTRIBUTE_HIDDEN;
  218. /* The merge function is used for topn indirect call counters. */
  219. extern void __gcov_merge_icall_topn (gcov_type *, unsigned) ATTRIBUTE_HIDDEN;
  220. /* The profiler functions. */
  221. extern void __gcov_interval_profiler (gcov_type *, gcov_type, int, unsigned);
  222. extern void __gcov_pow2_profiler (gcov_type *, gcov_type);
  223. extern void __gcov_one_value_profiler (gcov_type *, gcov_type);
  224. extern void __gcov_indirect_call_profiler (gcov_type*, gcov_type,
  225. void*, void*);
  226. extern void __gcov_indirect_call_profiler_v2 (gcov_type, void *);
  227. extern void __gcov_time_profiler (gcov_type *);
  228. extern void __gcov_average_profiler (gcov_type *, gcov_type);
  229. extern void __gcov_ior_profiler (gcov_type *, gcov_type);
  230. extern void __gcov_indirect_call_topn_profiler (gcov_type, void *);
  231. extern void gcov_sort_n_vals (gcov_type *, int);
  232. #ifndef inhibit_libc
  233. /* The wrappers around some library functions.. */
  234. extern pid_t __gcov_fork (void) ATTRIBUTE_HIDDEN;
  235. extern int __gcov_execl (const char *, char *, ...) ATTRIBUTE_HIDDEN;
  236. extern int __gcov_execlp (const char *, char *, ...) ATTRIBUTE_HIDDEN;
  237. extern int __gcov_execle (const char *, char *, ...) ATTRIBUTE_HIDDEN;
  238. extern int __gcov_execv (const char *, char *const []) ATTRIBUTE_HIDDEN;
  239. extern int __gcov_execvp (const char *, char *const []) ATTRIBUTE_HIDDEN;
  240. extern int __gcov_execve (const char *, char *const [], char *const [])
  241. ATTRIBUTE_HIDDEN;
  242. /* Functions that only available in libgcov. */
  243. GCOV_LINKAGE int gcov_open (const char */*name*/) ATTRIBUTE_HIDDEN;
  244. GCOV_LINKAGE void gcov_write_counter (gcov_type) ATTRIBUTE_HIDDEN;
  245. GCOV_LINKAGE void gcov_write_tag_length (gcov_unsigned_t, gcov_unsigned_t)
  246. ATTRIBUTE_HIDDEN;
  247. GCOV_LINKAGE void gcov_write_summary (gcov_unsigned_t /*tag*/,
  248. const struct gcov_summary *)
  249. ATTRIBUTE_HIDDEN;
  250. GCOV_LINKAGE void gcov_seek (gcov_position_t /*position*/) ATTRIBUTE_HIDDEN;
  251. GCOV_LINKAGE void gcov_rewrite (void) ATTRIBUTE_HIDDEN;
  252. /* "Counts" stored in gcda files can be a real counter value, or
  253. an target address. When differentiate these two types because
  254. when manipulating counts, we should only change real counter values,
  255. rather target addresses. */
  256. static inline gcov_type
  257. gcov_get_counter (void)
  258. {
  259. #ifndef IN_GCOV_TOOL
  260. /* This version is for reading count values in libgcov runtime:
  261. we read from gcda files. */
  262. return gcov_read_counter ();
  263. #else
  264. /* This version is for gcov-tool. We read the value from memory and
  265. multiply it by the merge weight. */
  266. return gcov_read_counter_mem () * gcov_get_merge_weight ();
  267. #endif
  268. }
  269. /* Similar function as gcov_get_counter(), but handles target address
  270. counters. */
  271. static inline gcov_type
  272. gcov_get_counter_target (void)
  273. {
  274. #ifndef IN_GCOV_TOOL
  275. /* This version is for reading count target values in libgcov runtime:
  276. we read from gcda files. */
  277. return gcov_read_counter ();
  278. #else
  279. /* This version is for gcov-tool. We read the value from memory and we do NOT
  280. multiply it by the merge weight. */
  281. return gcov_read_counter_mem ();
  282. #endif
  283. }
  284. #endif /* !inhibit_libc */
  285. #endif /* GCC_LIBGCOV_H */