libgcov-driver.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. /* Routines required for instrumenting a program. */
  2. /* Compile this one with gcc. */
  3. /* Copyright (C) 1989-2022 Free Software Foundation, Inc.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #include "libgcov.h"
  21. #include "gcov-io.h"
  22. /* Return 1, if all counter values are zero, otherwise 0. */
  23. static inline int
  24. are_all_counters_zero (const struct gcov_ctr_info *ci_ptr)
  25. {
  26. for (unsigned i = 0; i < ci_ptr->num; i++)
  27. if (ci_ptr->values[i] != 0)
  28. return 0;
  29. return 1;
  30. }
  31. #if defined(inhibit_libc)
  32. /* If libc and its header files are not available, provide dummy functions. */
  33. #if defined(L_gcov)
  34. void __gcov_init (struct gcov_info *p __attribute__ ((unused))) {}
  35. #endif
  36. #else /* inhibit_libc */
  37. #if GCOV_LOCKED
  38. #include <fcntl.h>
  39. #include <errno.h>
  40. #include <sys/stat.h>
  41. #elif GCOV_LOCKED_WITH_LOCKING
  42. #include <fcntl.h>
  43. #include <sys/locking.h>
  44. #include <sys/stat.h>
  45. #endif
  46. #if HAVE_SYS_MMAN_H
  47. #include <sys/mman.h>
  48. #endif
  49. #endif /* inhibit_libc */
  50. #if defined(L_gcov) && !defined(inhibit_libc)
  51. #define NEED_L_GCOV
  52. #endif
  53. #if defined(L_gcov_info_to_gcda) && !IN_GCOV_TOOL
  54. #define NEED_L_GCOV_INFO_TO_GCDA
  55. #endif
  56. #ifdef NEED_L_GCOV
  57. /* A utility function for outputting errors. */
  58. static int gcov_error (const char *, ...);
  59. #if !IN_GCOV_TOOL
  60. static void gcov_error_exit (void);
  61. #endif
  62. #include "gcov-io.cc"
  63. #define GCOV_PROF_PREFIX "libgcov profiling error:%s:"
  64. struct gcov_fn_buffer
  65. {
  66. struct gcov_fn_buffer *next;
  67. unsigned fn_ix;
  68. struct gcov_fn_info info;
  69. /* note gcov_fn_info ends in a trailing array. */
  70. };
  71. struct gcov_summary_buffer
  72. {
  73. struct gcov_summary_buffer *next;
  74. struct gcov_summary summary;
  75. };
  76. /* A struct that bundles all the related information about the
  77. gcda filename. */
  78. struct gcov_filename
  79. {
  80. char *filename; /* filename buffer */
  81. int strip; /* leading chars to strip from filename */
  82. char *prefix; /* prefix string */
  83. };
  84. static struct gcov_fn_buffer *
  85. free_fn_data (const struct gcov_info *gi_ptr, struct gcov_fn_buffer *buffer,
  86. unsigned limit)
  87. {
  88. struct gcov_fn_buffer *next;
  89. unsigned ix, n_ctr = 0;
  90. if (!buffer)
  91. return 0;
  92. next = buffer->next;
  93. for (ix = 0; ix != limit; ix++)
  94. if (gi_ptr->merge[ix])
  95. free (buffer->info.ctrs[n_ctr++].values);
  96. free (buffer);
  97. return next;
  98. }
  99. static struct gcov_fn_buffer **
  100. buffer_fn_data (const char *filename, const struct gcov_info *gi_ptr,
  101. struct gcov_fn_buffer **end_ptr, unsigned fn_ix)
  102. {
  103. unsigned n_ctrs = 0, ix = 0;
  104. struct gcov_fn_buffer *fn_buffer;
  105. unsigned len;
  106. for (ix = GCOV_COUNTERS; ix--;)
  107. if (gi_ptr->merge[ix])
  108. n_ctrs++;
  109. len = sizeof (*fn_buffer) + sizeof (fn_buffer->info.ctrs[0]) * n_ctrs;
  110. fn_buffer = (struct gcov_fn_buffer *) xmalloc (len);
  111. if (!fn_buffer)
  112. goto fail;
  113. fn_buffer->next = 0;
  114. fn_buffer->fn_ix = fn_ix;
  115. fn_buffer->info.ident = gcov_read_unsigned ();
  116. fn_buffer->info.lineno_checksum = gcov_read_unsigned ();
  117. fn_buffer->info.cfg_checksum = gcov_read_unsigned ();
  118. for (n_ctrs = ix = 0; ix != GCOV_COUNTERS; ix++)
  119. {
  120. gcov_unsigned_t length;
  121. gcov_type *values;
  122. if (!gi_ptr->merge[ix])
  123. continue;
  124. if (gcov_read_unsigned () != GCOV_TAG_FOR_COUNTER (ix))
  125. {
  126. len = 0;
  127. goto fail;
  128. }
  129. length = GCOV_TAG_COUNTER_NUM (gcov_read_unsigned ());
  130. len = length * sizeof (gcov_type);
  131. values = (gcov_type *) xmalloc (len);
  132. if (!values)
  133. goto fail;
  134. fn_buffer->info.ctrs[n_ctrs].num = length;
  135. fn_buffer->info.ctrs[n_ctrs].values = values;
  136. while (length--)
  137. *values++ = gcov_read_counter ();
  138. n_ctrs++;
  139. }
  140. *end_ptr = fn_buffer;
  141. return &fn_buffer->next;
  142. fail:
  143. gcov_error (GCOV_PROF_PREFIX "Function %u %s %u \n", filename, fn_ix,
  144. len ? "cannot allocate" : "counter mismatch", len ? len : ix);
  145. return (struct gcov_fn_buffer **)free_fn_data (gi_ptr, fn_buffer, ix);
  146. }
  147. /* Convert VERSION into a string description and return the it.
  148. BUFFER is used for storage of the string. The code should be
  149. aligned wit gcov-iov.c. */
  150. static char *
  151. gcov_version_string (char *buffer, char version[4])
  152. {
  153. if (version[0] < 'A' || version[0] > 'Z'
  154. || version[1] < '0' || version[1] > '9'
  155. || version[2] < '0' || version[2] > '9')
  156. sprintf (buffer, "(unknown)");
  157. else
  158. {
  159. unsigned major = 10 * (version[0] - 'A') + (version[1] - '0');
  160. unsigned minor = version[2] - '0';
  161. sprintf (buffer, "%u.%u (%s)", major, minor,
  162. version[3] == '*' ? "release" : "experimental");
  163. }
  164. return buffer;
  165. }
  166. /* Check if VERSION of the info block PTR matches libgcov one.
  167. Return 1 on success, or zero in case of versions mismatch.
  168. If FILENAME is not NULL, its value used for reporting purposes
  169. instead of value from the info block. */
  170. static int
  171. gcov_version (struct gcov_info *ptr, gcov_unsigned_t version,
  172. const char *filename)
  173. {
  174. if (version != GCOV_VERSION)
  175. {
  176. char v[4], e[4];
  177. char ver_string[128], expected_string[128];
  178. GCOV_UNSIGNED2STRING (v, version);
  179. GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
  180. gcov_error (GCOV_PROF_PREFIX "Version mismatch - expected %s (%.4s) "
  181. "got %s (%.4s)\n",
  182. filename? filename : ptr->filename,
  183. gcov_version_string (expected_string, e), e,
  184. gcov_version_string (ver_string, v), v);
  185. return 0;
  186. }
  187. return 1;
  188. }
  189. /* buffer for the fn_data from another program. */
  190. static struct gcov_fn_buffer *fn_buffer;
  191. /* Including system dependent components. */
  192. #include "libgcov-driver-system.c"
  193. /* This function merges counters in GI_PTR to an existing gcda file.
  194. Return 0 on success.
  195. Return -1 on error. In this case, caller will goto read_fatal. */
  196. static int
  197. merge_one_data (const char *filename,
  198. struct gcov_info *gi_ptr,
  199. struct gcov_summary *summary)
  200. {
  201. gcov_unsigned_t tag, length;
  202. unsigned t_ix;
  203. int f_ix = -1;
  204. int error = 0;
  205. struct gcov_fn_buffer **fn_tail = &fn_buffer;
  206. length = gcov_read_unsigned ();
  207. if (!gcov_version (gi_ptr, length, filename))
  208. return -1;
  209. /* Skip timestamp. */
  210. gcov_read_unsigned ();
  211. length = gcov_read_unsigned ();
  212. if (length != gi_ptr->checksum)
  213. {
  214. /* Read from a different compilation. Overwrite the file. */
  215. gcov_error (GCOV_PROF_PREFIX "overwriting an existing profile data "
  216. "with a different checksum\n", filename);
  217. return 0;
  218. }
  219. tag = gcov_read_unsigned ();
  220. if (tag != GCOV_TAG_OBJECT_SUMMARY)
  221. goto read_mismatch;
  222. length = gcov_read_unsigned ();
  223. gcc_assert (length > 0);
  224. gcov_read_summary (summary);
  225. tag = gcov_read_unsigned ();
  226. /* Merge execution counts for each function. */
  227. for (f_ix = 0; (unsigned)f_ix != gi_ptr->n_functions;
  228. f_ix++, tag = gcov_read_unsigned ())
  229. {
  230. const struct gcov_ctr_info *ci_ptr;
  231. const struct gcov_fn_info *gfi_ptr = gi_ptr->functions[f_ix];
  232. if (tag != GCOV_TAG_FUNCTION)
  233. goto read_mismatch;
  234. length = gcov_read_unsigned ();
  235. if (!length)
  236. /* This function did not appear in the other program.
  237. We have nothing to merge. */
  238. continue;
  239. if (length != GCOV_TAG_FUNCTION_LENGTH)
  240. goto read_mismatch;
  241. if (!gfi_ptr || gfi_ptr->key != gi_ptr)
  242. {
  243. /* This function appears in the other program. We
  244. need to buffer the information in order to write
  245. it back out -- we'll be inserting data before
  246. this point, so cannot simply keep the data in the
  247. file. */
  248. fn_tail = buffer_fn_data (filename, gi_ptr, fn_tail, f_ix);
  249. if (!fn_tail)
  250. goto read_mismatch;
  251. continue;
  252. }
  253. length = gcov_read_unsigned ();
  254. if (length != gfi_ptr->ident)
  255. goto read_mismatch;
  256. length = gcov_read_unsigned ();
  257. if (length != gfi_ptr->lineno_checksum)
  258. goto read_mismatch;
  259. length = gcov_read_unsigned ();
  260. if (length != gfi_ptr->cfg_checksum)
  261. goto read_mismatch;
  262. ci_ptr = gfi_ptr->ctrs;
  263. for (t_ix = 0; t_ix < GCOV_COUNTERS; t_ix++)
  264. {
  265. gcov_merge_fn merge = gi_ptr->merge[t_ix];
  266. if (!merge)
  267. continue;
  268. tag = gcov_read_unsigned ();
  269. int read_length = (int)gcov_read_unsigned ();
  270. length = abs (read_length);
  271. if (tag != GCOV_TAG_FOR_COUNTER (t_ix)
  272. || (length != GCOV_TAG_COUNTER_LENGTH (ci_ptr->num)
  273. && t_ix != GCOV_COUNTER_V_TOPN
  274. && t_ix != GCOV_COUNTER_V_INDIR))
  275. goto read_mismatch;
  276. /* Merging with all zero counters does not make sense. */
  277. if (read_length > 0)
  278. (*merge) (ci_ptr->values, ci_ptr->num);
  279. ci_ptr++;
  280. }
  281. if ((error = gcov_is_error ()))
  282. goto read_error;
  283. }
  284. if (tag)
  285. {
  286. read_mismatch:;
  287. gcov_error (GCOV_PROF_PREFIX "Merge mismatch for %s %u\n",
  288. filename, f_ix >= 0 ? "function" : "summary",
  289. f_ix < 0 ? -1 - f_ix : f_ix);
  290. return -1;
  291. }
  292. return 0;
  293. read_error:
  294. gcov_error (GCOV_PROF_PREFIX "%s merging\n", filename,
  295. error < 0 ? "Overflow": "Error");
  296. return -1;
  297. }
  298. /* Write the DATA of LENGTH characters to the gcov file. */
  299. static void
  300. gcov_dump_handler (const void *data,
  301. unsigned length,
  302. void *arg ATTRIBUTE_UNUSED)
  303. {
  304. gcov_write (data, length);
  305. }
  306. /* Allocate SIZE characters and return the address of the allocated memory. */
  307. static void *
  308. gcov_allocate_handler (unsigned size, void *arg ATTRIBUTE_UNUSED)
  309. {
  310. return xmalloc (size);
  311. }
  312. #endif /* NEED_L_GCOV */
  313. #if defined(NEED_L_GCOV) || defined(NEED_L_GCOV_INFO_TO_GCDA)
  314. /* Dump the WORD using the DUMP handler called with ARG. */
  315. static inline void
  316. dump_unsigned (gcov_unsigned_t word,
  317. void (*dump_fn) (const void *, unsigned, void *),
  318. void *arg)
  319. {
  320. (*dump_fn) (&word, sizeof (word), arg);
  321. }
  322. /* Dump the COUNTER using the DUMP handler called with ARG. */
  323. static inline void
  324. dump_counter (gcov_type counter,
  325. void (*dump_fn) (const void *, unsigned, void *),
  326. void *arg)
  327. {
  328. dump_unsigned ((gcov_unsigned_t)counter, dump_fn, arg);
  329. if (sizeof (counter) > sizeof (gcov_unsigned_t))
  330. dump_unsigned ((gcov_unsigned_t)(counter >> 32), dump_fn, arg);
  331. else
  332. dump_unsigned (0, dump_fn, arg);
  333. }
  334. #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
  335. /* Store all TOP N counters where each has a dynamic length. */
  336. static void
  337. write_topn_counters (const struct gcov_ctr_info *ci_ptr,
  338. unsigned t_ix,
  339. gcov_unsigned_t n_counts,
  340. void (*dump_fn) (const void *, unsigned, void *),
  341. void *(*allocate_fn)(unsigned, void *),
  342. void *arg)
  343. {
  344. unsigned counters = n_counts / GCOV_TOPN_MEM_COUNTERS;
  345. gcc_assert (n_counts % GCOV_TOPN_MEM_COUNTERS == 0);
  346. /* It can happen in a multi-threaded environment that number of counters is
  347. different from the size of the corresponding linked lists. */
  348. #define LIST_SIZE_MIN_LENGTH 4 * 1024
  349. static unsigned *list_sizes = NULL;
  350. static unsigned list_size_length = 0;
  351. if (list_sizes == NULL || counters > list_size_length)
  352. {
  353. list_size_length = MAX (LIST_SIZE_MIN_LENGTH, 2 * counters);
  354. #if !defined(inhibit_libc) && HAVE_SYS_MMAN_H
  355. list_sizes
  356. = (unsigned *)malloc_mmap (list_size_length * sizeof (unsigned));
  357. #endif
  358. /* Malloc fallback. */
  359. if (list_sizes == NULL)
  360. list_sizes =
  361. (unsigned *)(*allocate_fn) (list_size_length * sizeof (unsigned),
  362. arg);
  363. }
  364. unsigned pair_total = 0;
  365. for (unsigned i = 0; i < counters; i++)
  366. {
  367. gcov_type start = ci_ptr->values[GCOV_TOPN_MEM_COUNTERS * i + 2];
  368. unsigned sizes = 0;
  369. for (struct gcov_kvp *node = (struct gcov_kvp *)(__INTPTR_TYPE__)start;
  370. node != NULL; node = node->next)
  371. ++sizes;
  372. pair_total += sizes;
  373. list_sizes[i] = sizes;
  374. }
  375. unsigned disk_size = GCOV_TOPN_DISK_COUNTERS * counters + 2 * pair_total;
  376. dump_unsigned (GCOV_TAG_FOR_COUNTER (t_ix), dump_fn, arg),
  377. dump_unsigned (GCOV_TAG_COUNTER_LENGTH (disk_size), dump_fn, arg);
  378. for (unsigned i = 0; i < counters; i++)
  379. {
  380. dump_counter (ci_ptr->values[GCOV_TOPN_MEM_COUNTERS * i], dump_fn, arg);
  381. dump_counter (list_sizes[i], dump_fn, arg);
  382. gcov_type start = ci_ptr->values[GCOV_TOPN_MEM_COUNTERS * i + 2];
  383. unsigned j = 0;
  384. for (struct gcov_kvp *node = (struct gcov_kvp *)(__INTPTR_TYPE__)start;
  385. j < list_sizes[i]; node = node->next, j++)
  386. {
  387. dump_counter (node->value, dump_fn, arg);
  388. dump_counter (node->count, dump_fn, arg);
  389. }
  390. }
  391. }
  392. /* Write counters in GI_PTR and the summary in PRG to a gcda file. In
  393. the case of appending to an existing file, SUMMARY_POS will be non-zero.
  394. We will write the file starting from SUMMAY_POS. */
  395. static void
  396. write_one_data (const struct gcov_info *gi_ptr,
  397. const struct gcov_summary *prg_p ATTRIBUTE_UNUSED,
  398. void (*dump_fn) (const void *, unsigned, void *),
  399. void *(*allocate_fn) (unsigned, void *),
  400. void *arg)
  401. {
  402. unsigned f_ix;
  403. dump_unsigned (GCOV_DATA_MAGIC, dump_fn, arg);
  404. dump_unsigned (GCOV_VERSION, dump_fn, arg);
  405. dump_unsigned (gi_ptr->stamp, dump_fn, arg);
  406. dump_unsigned (gi_ptr->checksum, dump_fn, arg);
  407. #ifdef NEED_L_GCOV
  408. /* Generate whole program statistics. */
  409. gcov_write_summary (GCOV_TAG_OBJECT_SUMMARY, prg_p);
  410. #endif
  411. /* Write execution counts for each function. */
  412. for (f_ix = 0; f_ix != gi_ptr->n_functions; f_ix++)
  413. {
  414. #ifdef NEED_L_GCOV
  415. unsigned buffered = 0;
  416. #endif
  417. const struct gcov_fn_info *gfi_ptr;
  418. const struct gcov_ctr_info *ci_ptr;
  419. gcov_unsigned_t length;
  420. unsigned t_ix;
  421. #ifdef NEED_L_GCOV
  422. if (fn_buffer && fn_buffer->fn_ix == f_ix)
  423. {
  424. /* Buffered data from another program. */
  425. buffered = 1;
  426. gfi_ptr = &fn_buffer->info;
  427. length = GCOV_TAG_FUNCTION_LENGTH;
  428. }
  429. else
  430. #endif
  431. {
  432. gfi_ptr = gi_ptr->functions[f_ix];
  433. if (gfi_ptr && gfi_ptr->key == gi_ptr)
  434. length = GCOV_TAG_FUNCTION_LENGTH;
  435. else
  436. length = 0;
  437. }
  438. dump_unsigned (GCOV_TAG_FUNCTION, dump_fn, arg);
  439. dump_unsigned (length, dump_fn, arg);
  440. if (!length)
  441. continue;
  442. dump_unsigned (gfi_ptr->ident, dump_fn, arg);
  443. dump_unsigned (gfi_ptr->lineno_checksum, dump_fn, arg);
  444. dump_unsigned (gfi_ptr->cfg_checksum, dump_fn, arg);
  445. ci_ptr = gfi_ptr->ctrs;
  446. for (t_ix = 0; t_ix < GCOV_COUNTERS; t_ix++)
  447. {
  448. gcov_position_t n_counts;
  449. if (!gi_ptr->merge[t_ix])
  450. continue;
  451. n_counts = ci_ptr->num;
  452. if (t_ix == GCOV_COUNTER_V_TOPN || t_ix == GCOV_COUNTER_V_INDIR)
  453. write_topn_counters (ci_ptr, t_ix, n_counts, dump_fn, allocate_fn,
  454. arg);
  455. else
  456. {
  457. dump_unsigned (GCOV_TAG_FOR_COUNTER (t_ix), dump_fn, arg);
  458. if (are_all_counters_zero (ci_ptr))
  459. /* Do not stream when all counters are zero. */
  460. dump_unsigned (GCOV_TAG_COUNTER_LENGTH (-n_counts),
  461. dump_fn, arg);
  462. else
  463. {
  464. dump_unsigned (GCOV_TAG_COUNTER_LENGTH (n_counts),
  465. dump_fn, arg);
  466. for (unsigned i = 0; i < n_counts; i++)
  467. dump_counter (ci_ptr->values[i], dump_fn, arg);
  468. }
  469. }
  470. ci_ptr++;
  471. }
  472. #ifdef NEED_L_GCOV
  473. if (buffered)
  474. fn_buffer = free_fn_data (gi_ptr, fn_buffer, GCOV_COUNTERS);
  475. #endif
  476. }
  477. dump_unsigned (0, dump_fn, arg);
  478. }
  479. #endif /* NEED_L_GCOV || NEED_L_GCOV_INFO_TO_GCDA */
  480. #ifdef NEED_L_GCOV
  481. /* Dump the coverage counts for one gcov_info object. We merge with existing
  482. counts when possible, to avoid growing the .da files ad infinitum. We use
  483. this program's checksum to make sure we only accumulate whole program
  484. statistics to the correct summary. An object file might be embedded
  485. in two separate programs, and we must keep the two program
  486. summaries separate. */
  487. static void
  488. dump_one_gcov (struct gcov_info *gi_ptr, struct gcov_filename *gf,
  489. unsigned run_counted ATTRIBUTE_UNUSED,
  490. gcov_type run_max ATTRIBUTE_UNUSED)
  491. {
  492. struct gcov_summary summary = {};
  493. int error;
  494. gcov_unsigned_t tag;
  495. fn_buffer = 0;
  496. error = gcov_exit_open_gcda_file (gi_ptr, gf);
  497. if (error == -1)
  498. return;
  499. tag = gcov_read_unsigned ();
  500. if (tag)
  501. {
  502. /* Merge data from file. */
  503. if (tag != GCOV_DATA_MAGIC)
  504. {
  505. gcov_error (GCOV_PROF_PREFIX "Not a gcov data file\n",
  506. gf->filename);
  507. goto read_fatal;
  508. }
  509. error = merge_one_data (gf->filename, gi_ptr, &summary);
  510. if (error == -1)
  511. goto read_fatal;
  512. }
  513. gcov_rewrite ();
  514. #if !IN_GCOV_TOOL
  515. if (!run_counted)
  516. {
  517. summary.runs++;
  518. summary.sum_max += run_max;
  519. }
  520. #else
  521. summary = gi_ptr->summary;
  522. #endif
  523. write_one_data (gi_ptr, &summary, gcov_dump_handler, gcov_allocate_handler,
  524. NULL);
  525. /* fall through */
  526. read_fatal:;
  527. while (fn_buffer)
  528. fn_buffer = free_fn_data (gi_ptr, fn_buffer, GCOV_COUNTERS);
  529. if ((error = gcov_close ()))
  530. gcov_error ((error < 0 ? GCOV_PROF_PREFIX "Overflow writing\n"
  531. : GCOV_PROF_PREFIX "Error writing\n"), gf->filename);
  532. }
  533. /* Dump all the coverage counts for the program. It first computes program
  534. summary and then traverses gcov_list list and dumps the gcov_info
  535. objects one by one. */
  536. #if !IN_GCOV_TOOL
  537. static
  538. #endif
  539. void
  540. gcov_do_dump (struct gcov_info *list, int run_counted)
  541. {
  542. struct gcov_info *gi_ptr;
  543. struct gcov_filename gf;
  544. /* Compute run_max of this program run. */
  545. gcov_type run_max = 0;
  546. for (gi_ptr = list; gi_ptr; gi_ptr = gi_ptr->next)
  547. for (unsigned f_ix = 0; (unsigned)f_ix != gi_ptr->n_functions; f_ix++)
  548. {
  549. const struct gcov_ctr_info *cinfo
  550. = &gi_ptr->functions[f_ix]->ctrs[GCOV_COUNTER_ARCS];
  551. for (unsigned i = 0; i < cinfo->num; i++)
  552. if (run_max < cinfo->values[i])
  553. run_max = cinfo->values[i];
  554. }
  555. allocate_filename_struct (&gf);
  556. /* Now merge each file. */
  557. for (gi_ptr = list; gi_ptr; gi_ptr = gi_ptr->next)
  558. {
  559. dump_one_gcov (gi_ptr, &gf, run_counted, run_max);
  560. free (gf.filename);
  561. }
  562. free (gf.prefix);
  563. }
  564. #if IN_GCOV_TOOL
  565. const char *
  566. __attribute__ ((unused))
  567. gcov_get_filename (struct gcov_info *list)
  568. {
  569. return list->filename;
  570. }
  571. #endif
  572. #if !IN_GCOV_TOOL
  573. void
  574. __gcov_dump_one (struct gcov_root *root)
  575. {
  576. if (root->dumped)
  577. return;
  578. gcov_do_dump (root->list, root->run_counted);
  579. root->dumped = 1;
  580. root->run_counted = 1;
  581. }
  582. /* Per-dynamic-object gcov state. */
  583. struct gcov_root __gcov_root;
  584. /* Exactly one of these will be live in the process image. */
  585. struct gcov_master __gcov_master =
  586. {GCOV_VERSION, 0};
  587. /* Dynamic pool for gcov_kvp structures. */
  588. struct gcov_kvp *__gcov_kvp_dynamic_pool;
  589. /* Index into __gcov_kvp_dynamic_pool array. */
  590. unsigned __gcov_kvp_dynamic_pool_index;
  591. /* Size of _gcov_kvp_dynamic_pool array. */
  592. unsigned __gcov_kvp_dynamic_pool_size;
  593. void
  594. __gcov_exit (void)
  595. {
  596. __gcov_dump_one (&__gcov_root);
  597. if (__gcov_root.next)
  598. __gcov_root.next->prev = __gcov_root.prev;
  599. if (__gcov_root.prev)
  600. __gcov_root.prev->next = __gcov_root.next;
  601. else
  602. __gcov_master.root = __gcov_root.next;
  603. gcov_error_exit ();
  604. }
  605. /* Add a new object file onto the bb chain. Invoked automatically
  606. when running an object file's global ctors. */
  607. void
  608. __gcov_init (struct gcov_info *info)
  609. {
  610. if (!info->version || !info->n_functions)
  611. return;
  612. if (gcov_version (info, info->version, 0))
  613. {
  614. if (!__gcov_root.list)
  615. {
  616. /* Add to master list and at exit function. */
  617. if (gcov_version (NULL, __gcov_master.version, "<master>"))
  618. {
  619. __gcov_root.next = __gcov_master.root;
  620. if (__gcov_master.root)
  621. __gcov_master.root->prev = &__gcov_root;
  622. __gcov_master.root = &__gcov_root;
  623. }
  624. }
  625. info->next = __gcov_root.list;
  626. __gcov_root.list = info;
  627. }
  628. }
  629. #endif /* !IN_GCOV_TOOL */
  630. #endif /* NEED_L_GCOV */
  631. #ifdef NEED_L_GCOV_INFO_TO_GCDA
  632. /* Convert the gcov info to a gcda data stream. It is intended for
  633. free-standing environments which do not support the C library file I/O. */
  634. void
  635. __gcov_info_to_gcda (const struct gcov_info *gi_ptr,
  636. void (*filename_fn) (const char *, void *),
  637. void (*dump_fn) (const void *, unsigned, void *),
  638. void *(*allocate_fn) (unsigned, void *),
  639. void *arg)
  640. {
  641. (*filename_fn) (gi_ptr->filename, arg);
  642. write_one_data (gi_ptr, NULL, dump_fn, allocate_fn, arg);
  643. }
  644. #endif /* NEED_L_GCOV_INFO_TO_GCDA */