libgcov.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /* Header file for libgcov-*.c.
  2. Copyright (C) 1996-2022 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 "auto-target.h"
  33. #include "tsystem.h"
  34. #include "coretypes.h"
  35. #include "tm.h"
  36. #include "libgcc_tm.h"
  37. #include "gcov.h"
  38. #if HAVE_SYS_MMAN_H
  39. #include <sys/mman.h>
  40. #endif
  41. #if __CHAR_BIT__ == 8
  42. typedef unsigned gcov_unsigned_t __attribute__ ((mode (SI)));
  43. typedef unsigned gcov_position_t __attribute__ ((mode (SI)));
  44. #if __LIBGCC_GCOV_TYPE_SIZE > 32
  45. typedef signed gcov_type __attribute__ ((mode (DI)));
  46. typedef unsigned gcov_type_unsigned __attribute__ ((mode (DI)));
  47. #else
  48. typedef signed gcov_type __attribute__ ((mode (SI)));
  49. typedef unsigned gcov_type_unsigned __attribute__ ((mode (SI)));
  50. #endif
  51. #else
  52. #if __CHAR_BIT__ == 16
  53. typedef unsigned gcov_unsigned_t __attribute__ ((mode (HI)));
  54. typedef unsigned gcov_position_t __attribute__ ((mode (HI)));
  55. #if __LIBGCC_GCOV_TYPE_SIZE > 32
  56. typedef signed gcov_type __attribute__ ((mode (SI)));
  57. typedef unsigned gcov_type_unsigned __attribute__ ((mode (SI)));
  58. #else
  59. typedef signed gcov_type __attribute__ ((mode (HI)));
  60. typedef unsigned gcov_type_unsigned __attribute__ ((mode (HI)));
  61. #endif
  62. #else
  63. typedef unsigned gcov_unsigned_t __attribute__ ((mode (QI)));
  64. typedef unsigned gcov_position_t __attribute__ ((mode (QI)));
  65. #if __LIBGCC_GCOV_TYPE_SIZE > 32
  66. typedef signed gcov_type __attribute__ ((mode (HI)));
  67. typedef unsigned gcov_type_unsigned __attribute__ ((mode (HI)));
  68. #else
  69. typedef signed gcov_type __attribute__ ((mode (QI)));
  70. typedef unsigned gcov_type_unsigned __attribute__ ((mode (QI)));
  71. #endif
  72. #endif
  73. #endif
  74. #if defined (TARGET_POSIX_IO)
  75. #define GCOV_LOCKED 1
  76. #else
  77. #define GCOV_LOCKED 0
  78. #endif
  79. #if defined (__MSVCRT__)
  80. #define GCOV_LOCKED_WITH_LOCKING 1
  81. #else
  82. #define GCOV_LOCKED_WITH_LOCKING 0
  83. #endif
  84. #ifndef GCOV_SUPPORTS_ATOMIC
  85. /* Detect whether target can support atomic update of profilers. */
  86. #if __SIZEOF_LONG_LONG__ == 4 && __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
  87. #define GCOV_SUPPORTS_ATOMIC 1
  88. #else
  89. #if __SIZEOF_LONG_LONG__ == 8 && __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8
  90. #define GCOV_SUPPORTS_ATOMIC 1
  91. #else
  92. #define GCOV_SUPPORTS_ATOMIC 0
  93. #endif
  94. #endif
  95. #endif
  96. /* In libgcov we need these functions to be extern, so prefix them with
  97. __gcov. In libgcov they must also be hidden so that the instance in
  98. the executable is not also used in a DSO. */
  99. #define gcov_var __gcov_var
  100. #define gcov_open __gcov_open
  101. #define gcov_close __gcov_close
  102. #define gcov_position __gcov_position
  103. #define gcov_seek __gcov_seek
  104. #define gcov_rewrite __gcov_rewrite
  105. #define gcov_is_error __gcov_is_error
  106. #define gcov_write_unsigned __gcov_write_unsigned
  107. #define gcov_write_summary __gcov_write_summary
  108. #define gcov_read_unsigned __gcov_read_unsigned
  109. #define gcov_read_counter __gcov_read_counter
  110. #define gcov_read_summary __gcov_read_summary
  111. #else /* IN_GCOV_TOOL */
  112. /* About the host. */
  113. /* This path will be compiled for the host and linked into
  114. gcov-tool binary. */
  115. #include "config.h"
  116. #include "system.h"
  117. #include "coretypes.h"
  118. #include "tm.h"
  119. typedef unsigned gcov_unsigned_t;
  120. typedef unsigned gcov_position_t;
  121. /* gcov_type is typedef'd elsewhere for the compiler */
  122. #if defined (HOST_HAS_F_SETLKW)
  123. #define GCOV_LOCKED 1
  124. #else
  125. #define GCOV_LOCKED 0
  126. #endif
  127. #if defined (HOST_HAS_LK_LOCK)
  128. #define GCOV_LOCKED_WITH_LOCKING 1
  129. #else
  130. #define GCOV_LOCKED_WITH_LOCKING 0
  131. #endif
  132. /* Some Macros specific to gcov-tool. */
  133. #define L_gcov 1
  134. #define L_gcov_merge_add 1
  135. #define L_gcov_merge_topn 1
  136. #define L_gcov_merge_ior 1
  137. #define L_gcov_merge_time_profile 1
  138. extern gcov_type gcov_read_counter_mem ();
  139. extern unsigned gcov_get_merge_weight ();
  140. extern struct gcov_info *gcov_list;
  141. #endif /* !IN_GCOV_TOOL */
  142. #if defined(inhibit_libc)
  143. #define IN_LIBGCOV (-1)
  144. #else
  145. #define IN_LIBGCOV 1
  146. #if defined(L_gcov)
  147. #define GCOV_LINKAGE /* nothing */
  148. #endif
  149. #endif
  150. /* Poison these, so they don't accidentally slip in. */
  151. #pragma GCC poison gcov_write_string gcov_write_tag gcov_write_length
  152. #pragma GCC poison gcov_time
  153. #ifdef HAVE_GAS_HIDDEN
  154. #define ATTRIBUTE_HIDDEN __attribute__ ((__visibility__ ("hidden")))
  155. #else
  156. #define ATTRIBUTE_HIDDEN
  157. #endif
  158. #if HAVE_SYS_MMAN_H
  159. #ifndef MAP_FAILED
  160. #define MAP_FAILED ((void *)-1)
  161. #endif
  162. #if !defined (MAP_ANONYMOUS) && defined (MAP_ANON)
  163. #define MAP_ANONYMOUS MAP_ANON
  164. #endif
  165. #endif
  166. #include "gcov-io.h"
  167. /* Structures embedded in coveraged program. The structures generated
  168. by write_profile must match these. */
  169. /* Information about counters for a single function. */
  170. struct gcov_ctr_info
  171. {
  172. gcov_unsigned_t num; /* number of counters. */
  173. gcov_type *values; /* their values. */
  174. };
  175. /* Information about a single function. This uses the trailing array
  176. idiom. The number of counters is determined from the merge pointer
  177. array in gcov_info. The key is used to detect which of a set of
  178. comdat functions was selected -- it points to the gcov_info object
  179. of the object file containing the selected comdat function. */
  180. struct gcov_fn_info
  181. {
  182. const struct gcov_info *key; /* comdat key */
  183. gcov_unsigned_t ident; /* unique ident of function */
  184. gcov_unsigned_t lineno_checksum; /* function lineo_checksum */
  185. gcov_unsigned_t cfg_checksum; /* function cfg checksum */
  186. struct gcov_ctr_info ctrs[1]; /* instrumented counters */
  187. };
  188. /* Type of function used to merge counters. */
  189. typedef void (*gcov_merge_fn) (gcov_type *, gcov_unsigned_t);
  190. /* Information about a single object file. */
  191. struct gcov_info
  192. {
  193. gcov_unsigned_t version; /* expected version number */
  194. struct gcov_info *next; /* link to next, used by libgcov */
  195. gcov_unsigned_t stamp; /* uniquifying time stamp */
  196. gcov_unsigned_t checksum; /* unique object checksum */
  197. const char *filename; /* output file name */
  198. gcov_merge_fn merge[GCOV_COUNTERS]; /* merge functions (null for
  199. unused) */
  200. unsigned n_functions; /* number of functions */
  201. #ifndef IN_GCOV_TOOL
  202. const struct gcov_fn_info *const *functions; /* pointer to pointers
  203. to function information */
  204. #else
  205. struct gcov_fn_info **functions;
  206. struct gcov_summary summary;
  207. #endif /* !IN_GCOV_TOOL */
  208. };
  209. /* Root of a program/shared-object state */
  210. struct gcov_root
  211. {
  212. struct gcov_info *list;
  213. unsigned dumped : 1; /* counts have been dumped. */
  214. unsigned run_counted : 1; /* run has been accounted for. */
  215. struct gcov_root *next;
  216. struct gcov_root *prev;
  217. };
  218. extern struct gcov_root __gcov_root ATTRIBUTE_HIDDEN;
  219. struct gcov_master
  220. {
  221. gcov_unsigned_t version;
  222. struct gcov_root *root;
  223. };
  224. struct indirect_call_tuple
  225. {
  226. /* Callee function. */
  227. void *callee;
  228. /* Pointer to counters. */
  229. gcov_type *counters;
  230. };
  231. /* Exactly one of these will be active in the process. */
  232. extern struct gcov_master __gcov_master;
  233. extern struct gcov_kvp *__gcov_kvp_dynamic_pool;
  234. extern unsigned __gcov_kvp_dynamic_pool_index;
  235. extern unsigned __gcov_kvp_dynamic_pool_size;
  236. /* Dump a set of gcov objects. */
  237. extern void __gcov_dump_one (struct gcov_root *) ATTRIBUTE_HIDDEN;
  238. /* Register a new object file module. */
  239. extern void __gcov_init (struct gcov_info *) ATTRIBUTE_HIDDEN;
  240. /* GCOV exit function registered via a static destructor. */
  241. extern void __gcov_exit (void) ATTRIBUTE_HIDDEN;
  242. /* Function to reset all counters to 0. Both externally visible (and
  243. overridable) and internal version. */
  244. extern void __gcov_reset_int (void) ATTRIBUTE_HIDDEN;
  245. /* User function to enable early write of profile information so far. */
  246. extern void __gcov_dump_int (void) ATTRIBUTE_HIDDEN;
  247. /* Lock critical section for __gcov_dump and __gcov_reset functions. */
  248. extern void __gcov_lock (void) ATTRIBUTE_HIDDEN;
  249. /* Unlock critical section for __gcov_dump and __gcov_reset functions. */
  250. extern void __gcov_unlock (void) ATTRIBUTE_HIDDEN;
  251. /* The merge function that just sums the counters. */
  252. extern void __gcov_merge_add (gcov_type *, unsigned) ATTRIBUTE_HIDDEN;
  253. /* The merge function to select the minimum valid counter value. */
  254. extern void __gcov_merge_time_profile (gcov_type *, unsigned) ATTRIBUTE_HIDDEN;
  255. /* The merge function to choose the most common N values. */
  256. extern void __gcov_merge_topn (gcov_type *, unsigned) ATTRIBUTE_HIDDEN;
  257. /* The merge function that just ors the counters together. */
  258. extern void __gcov_merge_ior (gcov_type *, unsigned) ATTRIBUTE_HIDDEN;
  259. /* The profiler functions. */
  260. extern void __gcov_interval_profiler (gcov_type *, gcov_type, int, unsigned);
  261. extern void __gcov_interval_profiler_atomic (gcov_type *, gcov_type, int,
  262. unsigned);
  263. extern void __gcov_pow2_profiler (gcov_type *, gcov_type);
  264. extern void __gcov_pow2_profiler_atomic (gcov_type *, gcov_type);
  265. extern void __gcov_topn_values_profiler (gcov_type *, gcov_type);
  266. extern void __gcov_topn_values_profiler_atomic (gcov_type *, gcov_type);
  267. extern void __gcov_indirect_call_profiler_v4 (gcov_type, void *);
  268. extern void __gcov_indirect_call_profiler_v4_atomic (gcov_type, void *);
  269. extern void __gcov_time_profiler (gcov_type *);
  270. extern void __gcov_time_profiler_atomic (gcov_type *);
  271. extern void __gcov_average_profiler (gcov_type *, gcov_type);
  272. extern void __gcov_average_profiler_atomic (gcov_type *, gcov_type);
  273. extern void __gcov_ior_profiler (gcov_type *, gcov_type);
  274. extern void __gcov_ior_profiler_atomic (gcov_type *, gcov_type);
  275. #ifndef inhibit_libc
  276. /* The wrappers around some library functions.. */
  277. extern pid_t __gcov_fork (void) ATTRIBUTE_HIDDEN;
  278. extern int __gcov_execl (const char *, char *, ...) ATTRIBUTE_HIDDEN;
  279. extern int __gcov_execlp (const char *, char *, ...) ATTRIBUTE_HIDDEN;
  280. extern int __gcov_execle (const char *, char *, ...) ATTRIBUTE_HIDDEN;
  281. extern int __gcov_execv (const char *, char *const []) ATTRIBUTE_HIDDEN;
  282. extern int __gcov_execvp (const char *, char *const []) ATTRIBUTE_HIDDEN;
  283. extern int __gcov_execve (const char *, char *const [], char *const [])
  284. ATTRIBUTE_HIDDEN;
  285. /* Functions that only available in libgcov. */
  286. GCOV_LINKAGE int gcov_open (const char */*name*/) ATTRIBUTE_HIDDEN;
  287. GCOV_LINKAGE void gcov_write_summary (gcov_unsigned_t /*tag*/,
  288. const struct gcov_summary *)
  289. ATTRIBUTE_HIDDEN;
  290. GCOV_LINKAGE void gcov_seek (gcov_position_t /*position*/) ATTRIBUTE_HIDDEN;
  291. GCOV_LINKAGE void gcov_rewrite (void) ATTRIBUTE_HIDDEN;
  292. /* "Counts" stored in gcda files can be a real counter value, or
  293. an target address. When differentiate these two types because
  294. when manipulating counts, we should only change real counter values,
  295. rather target addresses. */
  296. static inline gcov_type
  297. gcov_get_counter (void)
  298. {
  299. #ifndef IN_GCOV_TOOL
  300. /* This version is for reading count values in libgcov runtime:
  301. we read from gcda files. */
  302. return gcov_read_counter ();
  303. #else
  304. /* This version is for gcov-tool. We read the value from memory and
  305. multiply it by the merge weight. */
  306. return gcov_read_counter_mem () * gcov_get_merge_weight ();
  307. #endif
  308. }
  309. /* Similar function as gcov_get_counter(), but do not scale
  310. when read value is equal to IGNORE_SCALING. */
  311. static inline gcov_type
  312. gcov_get_counter_ignore_scaling (gcov_type ignore_scaling ATTRIBUTE_UNUSED)
  313. {
  314. #ifndef IN_GCOV_TOOL
  315. /* This version is for reading count values in libgcov runtime:
  316. we read from gcda files. */
  317. return gcov_read_counter ();
  318. #else
  319. /* This version is for gcov-tool. We read the value from memory and
  320. multiply it by the merge weight. */
  321. gcov_type v = gcov_read_counter_mem ();
  322. if (v != ignore_scaling)
  323. v *= gcov_get_merge_weight ();
  324. return v;
  325. #endif
  326. }
  327. /* Similar function as gcov_get_counter(), but handles target address
  328. counters. */
  329. static inline gcov_type
  330. gcov_get_counter_target (void)
  331. {
  332. #ifndef IN_GCOV_TOOL
  333. /* This version is for reading count target values in libgcov runtime:
  334. we read from gcda files. */
  335. return gcov_read_counter ();
  336. #else
  337. /* This version is for gcov-tool. We read the value from memory and we do NOT
  338. multiply it by the merge weight. */
  339. return gcov_read_counter_mem ();
  340. #endif
  341. }
  342. /* Add VALUE to *COUNTER and make it with atomic operation
  343. if USE_ATOMIC is true. */
  344. static inline void
  345. gcov_counter_add (gcov_type *counter, gcov_type value,
  346. int use_atomic ATTRIBUTE_UNUSED)
  347. {
  348. #if GCOV_SUPPORTS_ATOMIC
  349. if (use_atomic)
  350. __atomic_fetch_add (counter, value, __ATOMIC_RELAXED);
  351. else
  352. #endif
  353. *counter += value;
  354. }
  355. #if HAVE_SYS_MMAN_H
  356. /* Allocate LENGTH with mmap function. */
  357. static inline void *
  358. malloc_mmap (size_t length)
  359. {
  360. return mmap (NULL, length, PROT_READ | PROT_WRITE,
  361. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  362. }
  363. #endif
  364. /* Allocate gcov_kvp from statically pre-allocated pool,
  365. or use heap otherwise. */
  366. static inline struct gcov_kvp *
  367. allocate_gcov_kvp (void)
  368. {
  369. #define MMAP_CHUNK_SIZE (128 * 1024)
  370. struct gcov_kvp *new_node = NULL;
  371. unsigned kvp_sizeof = sizeof(struct gcov_kvp);
  372. /* Try mmaped pool if available. */
  373. #if !defined(IN_GCOV_TOOL) && !defined(L_gcov_merge_topn) && HAVE_SYS_MMAN_H
  374. if (__gcov_kvp_dynamic_pool == NULL
  375. || __gcov_kvp_dynamic_pool_index >= __gcov_kvp_dynamic_pool_size)
  376. {
  377. void *ptr = malloc_mmap (MMAP_CHUNK_SIZE);
  378. if (ptr != MAP_FAILED)
  379. {
  380. __gcov_kvp_dynamic_pool = ptr;
  381. __gcov_kvp_dynamic_pool_size = MMAP_CHUNK_SIZE / kvp_sizeof;
  382. __gcov_kvp_dynamic_pool_index = 0;
  383. }
  384. }
  385. if (__gcov_kvp_dynamic_pool != NULL)
  386. {
  387. unsigned index;
  388. #if GCOV_SUPPORTS_ATOMIC
  389. index
  390. = __atomic_fetch_add (&__gcov_kvp_dynamic_pool_index, 1,
  391. __ATOMIC_RELAXED);
  392. #else
  393. index = __gcov_kvp_dynamic_pool_index++;
  394. #endif
  395. if (index < __gcov_kvp_dynamic_pool_size)
  396. new_node = __gcov_kvp_dynamic_pool + index;
  397. }
  398. #endif
  399. /* Fallback to malloc. */
  400. if (new_node == NULL)
  401. new_node = (struct gcov_kvp *)xcalloc (1, kvp_sizeof);
  402. return new_node;
  403. }
  404. /* Add key value pair VALUE:COUNT to a top N COUNTERS. When INCREMENT_TOTAL
  405. is true, add COUNT to total of the TOP counter. If USE_ATOMIC is true,
  406. do it in atomic way. Return true when the counter is full, otherwise
  407. return false. */
  408. static inline unsigned
  409. gcov_topn_add_value (gcov_type *counters, gcov_type value, gcov_type count,
  410. int use_atomic, int increment_total)
  411. {
  412. if (increment_total)
  413. {
  414. /* In the multi-threaded mode, we can have an already merged profile
  415. with a negative total value. In that case, we should bail out. */
  416. if (counters[0] < 0)
  417. return 0;
  418. gcov_counter_add (&counters[0], 1, use_atomic);
  419. }
  420. struct gcov_kvp *prev_node = NULL;
  421. struct gcov_kvp *minimal_node = NULL;
  422. struct gcov_kvp *current_node = (struct gcov_kvp *)(intptr_t)counters[2];
  423. while (current_node)
  424. {
  425. if (current_node->value == value)
  426. {
  427. gcov_counter_add (&current_node->count, count, use_atomic);
  428. return 0;
  429. }
  430. if (minimal_node == NULL
  431. || current_node->count < minimal_node->count)
  432. minimal_node = current_node;
  433. prev_node = current_node;
  434. current_node = current_node->next;
  435. }
  436. if (counters[1] == GCOV_TOPN_MAXIMUM_TRACKED_VALUES)
  437. {
  438. if (--minimal_node->count < count)
  439. {
  440. minimal_node->value = value;
  441. minimal_node->count = count;
  442. }
  443. return 1;
  444. }
  445. else
  446. {
  447. struct gcov_kvp *new_node = allocate_gcov_kvp ();
  448. if (new_node == NULL)
  449. return 0;
  450. new_node->value = value;
  451. new_node->count = count;
  452. int success = 0;
  453. if (!counters[2])
  454. {
  455. #if GCOV_SUPPORTS_ATOMIC
  456. if (use_atomic)
  457. {
  458. struct gcov_kvp **ptr = (struct gcov_kvp **)(intptr_t)&counters[2];
  459. success = !__sync_val_compare_and_swap (ptr, 0, new_node);
  460. }
  461. else
  462. #endif
  463. {
  464. counters[2] = (intptr_t)new_node;
  465. success = 1;
  466. }
  467. }
  468. else if (prev_node && !prev_node->next)
  469. {
  470. #if GCOV_SUPPORTS_ATOMIC
  471. if (use_atomic)
  472. success = !__sync_val_compare_and_swap (&prev_node->next, 0,
  473. new_node);
  474. else
  475. #endif
  476. {
  477. prev_node->next = new_node;
  478. success = 1;
  479. }
  480. }
  481. /* Increment number of nodes. */
  482. if (success)
  483. gcov_counter_add (&counters[1], 1, use_atomic);
  484. }
  485. return 0;
  486. }
  487. #endif /* !inhibit_libc */
  488. #endif /* GCC_LIBGCOV_H */