libgcov-driver.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. /* Routines required for instrumenting a program. */
  2. /* Compile this one with gcc. */
  3. /* Copyright (C) 1989-2015 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. #if defined(inhibit_libc)
  22. /* If libc and its header files are not available, provide dummy functions. */
  23. #if defined(L_gcov)
  24. void __gcov_init (struct gcov_info *p __attribute__ ((unused))) {}
  25. #endif
  26. #else /* inhibit_libc */
  27. #include <string.h>
  28. #if GCOV_LOCKED
  29. #include <fcntl.h>
  30. #include <errno.h>
  31. #include <sys/stat.h>
  32. #endif
  33. #ifdef L_gcov
  34. /* A utility function for outputing errors. */
  35. static int gcov_error (const char *, ...);
  36. #include "gcov-io.c"
  37. struct gcov_fn_buffer
  38. {
  39. struct gcov_fn_buffer *next;
  40. unsigned fn_ix;
  41. struct gcov_fn_info info;
  42. /* note gcov_fn_info ends in a trailing array. */
  43. };
  44. struct gcov_summary_buffer
  45. {
  46. struct gcov_summary_buffer *next;
  47. struct gcov_summary summary;
  48. };
  49. /* A struct that bundles all the related information about the
  50. gcda filename. */
  51. struct gcov_filename
  52. {
  53. char *filename; /* filename buffer */
  54. size_t max_length; /* maximum filename length */
  55. int strip; /* leading chars to strip from filename */
  56. size_t prefix; /* chars to prepend to filename */
  57. };
  58. static struct gcov_fn_buffer *
  59. free_fn_data (const struct gcov_info *gi_ptr, struct gcov_fn_buffer *buffer,
  60. unsigned limit)
  61. {
  62. struct gcov_fn_buffer *next;
  63. unsigned ix, n_ctr = 0;
  64. if (!buffer)
  65. return 0;
  66. next = buffer->next;
  67. for (ix = 0; ix != limit; ix++)
  68. if (gi_ptr->merge[ix])
  69. free (buffer->info.ctrs[n_ctr++].values);
  70. free (buffer);
  71. return next;
  72. }
  73. static struct gcov_fn_buffer **
  74. buffer_fn_data (const char *filename, const struct gcov_info *gi_ptr,
  75. struct gcov_fn_buffer **end_ptr, unsigned fn_ix)
  76. {
  77. unsigned n_ctrs = 0, ix = 0;
  78. struct gcov_fn_buffer *fn_buffer;
  79. unsigned len;
  80. for (ix = GCOV_COUNTERS; ix--;)
  81. if (gi_ptr->merge[ix])
  82. n_ctrs++;
  83. len = sizeof (*fn_buffer) + sizeof (fn_buffer->info.ctrs[0]) * n_ctrs;
  84. fn_buffer = (struct gcov_fn_buffer *) xmalloc (len);
  85. if (!fn_buffer)
  86. goto fail;
  87. fn_buffer->next = 0;
  88. fn_buffer->fn_ix = fn_ix;
  89. fn_buffer->info.ident = gcov_read_unsigned ();
  90. fn_buffer->info.lineno_checksum = gcov_read_unsigned ();
  91. fn_buffer->info.cfg_checksum = gcov_read_unsigned ();
  92. for (n_ctrs = ix = 0; ix != GCOV_COUNTERS; ix++)
  93. {
  94. gcov_unsigned_t length;
  95. gcov_type *values;
  96. if (!gi_ptr->merge[ix])
  97. continue;
  98. if (gcov_read_unsigned () != GCOV_TAG_FOR_COUNTER (ix))
  99. {
  100. len = 0;
  101. goto fail;
  102. }
  103. length = GCOV_TAG_COUNTER_NUM (gcov_read_unsigned ());
  104. len = length * sizeof (gcov_type);
  105. values = (gcov_type *) xmalloc (len);
  106. if (!values)
  107. goto fail;
  108. fn_buffer->info.ctrs[n_ctrs].num = length;
  109. fn_buffer->info.ctrs[n_ctrs].values = values;
  110. while (length--)
  111. *values++ = gcov_read_counter ();
  112. n_ctrs++;
  113. }
  114. *end_ptr = fn_buffer;
  115. return &fn_buffer->next;
  116. fail:
  117. gcov_error ("profiling:%s:Function %u %s %u \n", filename, fn_ix,
  118. len ? "cannot allocate" : "counter mismatch", len ? len : ix);
  119. return (struct gcov_fn_buffer **)free_fn_data (gi_ptr, fn_buffer, ix);
  120. }
  121. /* Add an unsigned value to the current crc */
  122. static gcov_unsigned_t
  123. crc32_unsigned (gcov_unsigned_t crc32, gcov_unsigned_t value)
  124. {
  125. unsigned ix;
  126. for (ix = 32; ix--; value <<= 1)
  127. {
  128. unsigned feedback;
  129. feedback = (value ^ crc32) & 0x80000000 ? 0x04c11db7 : 0;
  130. crc32 <<= 1;
  131. crc32 ^= feedback;
  132. }
  133. return crc32;
  134. }
  135. /* Check if VERSION of the info block PTR matches libgcov one.
  136. Return 1 on success, or zero in case of versions mismatch.
  137. If FILENAME is not NULL, its value used for reporting purposes
  138. instead of value from the info block. */
  139. static int
  140. gcov_version (struct gcov_info *ptr, gcov_unsigned_t version,
  141. const char *filename)
  142. {
  143. if (version != GCOV_VERSION)
  144. {
  145. char v[4], e[4];
  146. GCOV_UNSIGNED2STRING (v, version);
  147. GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
  148. gcov_error ("profiling:%s:Version mismatch - expected %.4s got %.4s\n",
  149. filename? filename : ptr->filename, e, v);
  150. return 0;
  151. }
  152. return 1;
  153. }
  154. /* Insert counter VALUE into HISTOGRAM. */
  155. static void
  156. gcov_histogram_insert(gcov_bucket_type *histogram, gcov_type value)
  157. {
  158. unsigned i;
  159. i = gcov_histo_index(value);
  160. histogram[i].num_counters++;
  161. histogram[i].cum_value += value;
  162. if (value < histogram[i].min_value)
  163. histogram[i].min_value = value;
  164. }
  165. /* Computes a histogram of the arc counters to place in the summary SUM. */
  166. static void
  167. gcov_compute_histogram (struct gcov_info *list, struct gcov_summary *sum)
  168. {
  169. struct gcov_info *gi_ptr;
  170. const struct gcov_fn_info *gfi_ptr;
  171. const struct gcov_ctr_info *ci_ptr;
  172. struct gcov_ctr_summary *cs_ptr;
  173. unsigned t_ix, f_ix, ctr_info_ix, ix;
  174. int h_ix;
  175. /* This currently only applies to arc counters. */
  176. t_ix = GCOV_COUNTER_ARCS;
  177. /* First check if there are any counts recorded for this counter. */
  178. cs_ptr = &(sum->ctrs[t_ix]);
  179. if (!cs_ptr->num)
  180. return;
  181. for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
  182. {
  183. cs_ptr->histogram[h_ix].num_counters = 0;
  184. cs_ptr->histogram[h_ix].min_value = cs_ptr->run_max;
  185. cs_ptr->histogram[h_ix].cum_value = 0;
  186. }
  187. /* Walk through all the per-object structures and record each of
  188. the count values in histogram. */
  189. for (gi_ptr = list; gi_ptr; gi_ptr = gi_ptr->next)
  190. {
  191. if (!gi_ptr->merge[t_ix])
  192. continue;
  193. /* Find the appropriate index into the gcov_ctr_info array
  194. for the counter we are currently working on based on the
  195. existence of the merge function pointer for this object. */
  196. for (ix = 0, ctr_info_ix = 0; ix < t_ix; ix++)
  197. {
  198. if (gi_ptr->merge[ix])
  199. ctr_info_ix++;
  200. }
  201. for (f_ix = 0; f_ix != gi_ptr->n_functions; f_ix++)
  202. {
  203. gfi_ptr = gi_ptr->functions[f_ix];
  204. if (!gfi_ptr || gfi_ptr->key != gi_ptr)
  205. continue;
  206. ci_ptr = &gfi_ptr->ctrs[ctr_info_ix];
  207. for (ix = 0; ix < ci_ptr->num; ix++)
  208. gcov_histogram_insert (cs_ptr->histogram, ci_ptr->values[ix]);
  209. }
  210. }
  211. }
  212. /* buffer for the fn_data from another program. */
  213. static struct gcov_fn_buffer *fn_buffer;
  214. /* buffer for summary from other programs to be written out. */
  215. static struct gcov_summary_buffer *sum_buffer;
  216. /* This function computes the program level summary and the histo-gram.
  217. It computes and returns CRC32 and stored summary in THIS_PRG.
  218. Also determines the longest filename length of the info files. */
  219. #if !IN_GCOV_TOOL
  220. static
  221. #endif
  222. gcov_unsigned_t
  223. compute_summary (struct gcov_info *list, struct gcov_summary *this_prg,
  224. size_t *max_length)
  225. {
  226. struct gcov_info *gi_ptr;
  227. const struct gcov_fn_info *gfi_ptr;
  228. struct gcov_ctr_summary *cs_ptr;
  229. const struct gcov_ctr_info *ci_ptr;
  230. int f_ix;
  231. unsigned t_ix;
  232. gcov_unsigned_t c_num;
  233. gcov_unsigned_t crc32 = 0;
  234. /* Find the totals for this execution. */
  235. memset (this_prg, 0, sizeof (*this_prg));
  236. *max_length = 0;
  237. for (gi_ptr = list; gi_ptr; gi_ptr = gi_ptr->next)
  238. {
  239. size_t len = strlen (gi_ptr->filename);
  240. if (len > *max_length)
  241. *max_length = len;
  242. crc32 = crc32_unsigned (crc32, gi_ptr->stamp);
  243. crc32 = crc32_unsigned (crc32, gi_ptr->n_functions);
  244. for (f_ix = 0; (unsigned)f_ix != gi_ptr->n_functions; f_ix++)
  245. {
  246. gfi_ptr = gi_ptr->functions[f_ix];
  247. if (gfi_ptr && gfi_ptr->key != gi_ptr)
  248. gfi_ptr = 0;
  249. crc32 = crc32_unsigned (crc32, gfi_ptr ? gfi_ptr->cfg_checksum : 0);
  250. crc32 = crc32_unsigned (crc32,
  251. gfi_ptr ? gfi_ptr->lineno_checksum : 0);
  252. if (!gfi_ptr)
  253. continue;
  254. ci_ptr = gfi_ptr->ctrs;
  255. for (t_ix = 0; t_ix != GCOV_COUNTERS_SUMMABLE; t_ix++)
  256. {
  257. if (!gi_ptr->merge[t_ix])
  258. continue;
  259. cs_ptr = &(this_prg->ctrs[t_ix]);
  260. cs_ptr->num += ci_ptr->num;
  261. crc32 = crc32_unsigned (crc32, ci_ptr->num);
  262. for (c_num = 0; c_num < ci_ptr->num; c_num++)
  263. {
  264. cs_ptr->sum_all += ci_ptr->values[c_num];
  265. if (cs_ptr->run_max < ci_ptr->values[c_num])
  266. cs_ptr->run_max = ci_ptr->values[c_num];
  267. }
  268. ci_ptr++;
  269. }
  270. }
  271. }
  272. gcov_compute_histogram (list, this_prg);
  273. return crc32;
  274. }
  275. /* Including system dependent components. */
  276. #include "libgcov-driver-system.c"
  277. /* This function merges counters in GI_PTR to an existing gcda file.
  278. Return 0 on success.
  279. Return -1 on error. In this case, caller will goto read_fatal. */
  280. static int
  281. merge_one_data (const char *filename,
  282. struct gcov_info *gi_ptr,
  283. struct gcov_summary *prg_p,
  284. struct gcov_summary *this_prg,
  285. gcov_position_t *summary_pos_p,
  286. gcov_position_t *eof_pos_p,
  287. gcov_unsigned_t crc32)
  288. {
  289. gcov_unsigned_t tag, length;
  290. unsigned t_ix;
  291. int f_ix;
  292. int error = 0;
  293. struct gcov_fn_buffer **fn_tail = &fn_buffer;
  294. struct gcov_summary_buffer **sum_tail = &sum_buffer;
  295. length = gcov_read_unsigned ();
  296. if (!gcov_version (gi_ptr, length, filename))
  297. return -1;
  298. length = gcov_read_unsigned ();
  299. if (length != gi_ptr->stamp)
  300. /* Read from a different compilation. Overwrite the file. */
  301. return 0;
  302. /* Look for program summary. */
  303. for (f_ix = 0;;)
  304. {
  305. struct gcov_summary tmp;
  306. *eof_pos_p = gcov_position ();
  307. tag = gcov_read_unsigned ();
  308. if (tag != GCOV_TAG_PROGRAM_SUMMARY)
  309. break;
  310. f_ix--;
  311. length = gcov_read_unsigned ();
  312. gcov_read_summary (&tmp);
  313. if ((error = gcov_is_error ()))
  314. goto read_error;
  315. if (*summary_pos_p)
  316. {
  317. /* Save all summaries after the one that will be
  318. merged into below. These will need to be rewritten
  319. as histogram merging may change the number of non-zero
  320. histogram entries that will be emitted, and thus the
  321. size of the merged summary. */
  322. (*sum_tail) = (struct gcov_summary_buffer *)
  323. xmalloc (sizeof(struct gcov_summary_buffer));
  324. (*sum_tail)->summary = tmp;
  325. (*sum_tail)->next = 0;
  326. sum_tail = &((*sum_tail)->next);
  327. goto next_summary;
  328. }
  329. if (tmp.checksum != crc32)
  330. goto next_summary;
  331. for (t_ix = 0; t_ix != GCOV_COUNTERS_SUMMABLE; t_ix++)
  332. if (tmp.ctrs[t_ix].num != this_prg->ctrs[t_ix].num)
  333. goto next_summary;
  334. *prg_p = tmp;
  335. *summary_pos_p = *eof_pos_p;
  336. next_summary:;
  337. }
  338. /* Merge execution counts for each function. */
  339. for (f_ix = 0; (unsigned)f_ix != gi_ptr->n_functions;
  340. f_ix++, tag = gcov_read_unsigned ())
  341. {
  342. const struct gcov_ctr_info *ci_ptr;
  343. const struct gcov_fn_info *gfi_ptr = gi_ptr->functions[f_ix];
  344. if (tag != GCOV_TAG_FUNCTION)
  345. goto read_mismatch;
  346. length = gcov_read_unsigned ();
  347. if (!length)
  348. /* This function did not appear in the other program.
  349. We have nothing to merge. */
  350. continue;
  351. if (length != GCOV_TAG_FUNCTION_LENGTH)
  352. goto read_mismatch;
  353. if (!gfi_ptr || gfi_ptr->key != gi_ptr)
  354. {
  355. /* This function appears in the other program. We
  356. need to buffer the information in order to write
  357. it back out -- we'll be inserting data before
  358. this point, so cannot simply keep the data in the
  359. file. */
  360. fn_tail = buffer_fn_data (filename, gi_ptr, fn_tail, f_ix);
  361. if (!fn_tail)
  362. goto read_mismatch;
  363. continue;
  364. }
  365. length = gcov_read_unsigned ();
  366. if (length != gfi_ptr->ident)
  367. goto read_mismatch;
  368. length = gcov_read_unsigned ();
  369. if (length != gfi_ptr->lineno_checksum)
  370. goto read_mismatch;
  371. length = gcov_read_unsigned ();
  372. if (length != gfi_ptr->cfg_checksum)
  373. goto read_mismatch;
  374. ci_ptr = gfi_ptr->ctrs;
  375. for (t_ix = 0; t_ix < GCOV_COUNTERS; t_ix++)
  376. {
  377. gcov_merge_fn merge = gi_ptr->merge[t_ix];
  378. if (!merge)
  379. continue;
  380. tag = gcov_read_unsigned ();
  381. length = gcov_read_unsigned ();
  382. if (tag != GCOV_TAG_FOR_COUNTER (t_ix)
  383. || length != GCOV_TAG_COUNTER_LENGTH (ci_ptr->num))
  384. goto read_mismatch;
  385. (*merge) (ci_ptr->values, ci_ptr->num);
  386. ci_ptr++;
  387. }
  388. if ((error = gcov_is_error ()))
  389. goto read_error;
  390. }
  391. if (tag)
  392. {
  393. read_mismatch:;
  394. gcov_error ("profiling:%s:Merge mismatch for %s %u\n",
  395. filename, f_ix >= 0 ? "function" : "summary",
  396. f_ix < 0 ? -1 - f_ix : f_ix);
  397. return -1;
  398. }
  399. return 0;
  400. read_error:
  401. gcov_error ("profiling:%s:%s merging\n", filename,
  402. error < 0 ? "Overflow": "Error");
  403. return -1;
  404. }
  405. /* Write counters in GI_PTR and the summary in PRG to a gcda file. In
  406. the case of appending to an existing file, SUMMARY_POS will be non-zero.
  407. We will write the file starting from SUMMAY_POS. */
  408. static void
  409. write_one_data (const struct gcov_info *gi_ptr,
  410. const struct gcov_summary *prg_p,
  411. const gcov_position_t eof_pos,
  412. const gcov_position_t summary_pos)
  413. {
  414. unsigned f_ix;
  415. struct gcov_summary_buffer *next_sum_buffer;
  416. /* Write out the data. */
  417. if (!eof_pos)
  418. {
  419. gcov_write_tag_length (GCOV_DATA_MAGIC, GCOV_VERSION);
  420. gcov_write_unsigned (gi_ptr->stamp);
  421. }
  422. if (summary_pos)
  423. gcov_seek (summary_pos);
  424. /* Generate whole program statistics. */
  425. gcov_write_summary (GCOV_TAG_PROGRAM_SUMMARY, prg_p);
  426. /* Rewrite all the summaries that were after the summary we merged
  427. into. This is necessary as the merged summary may have a different
  428. size due to the number of non-zero histogram entries changing after
  429. merging. */
  430. while (sum_buffer)
  431. {
  432. gcov_write_summary (GCOV_TAG_PROGRAM_SUMMARY, &sum_buffer->summary);
  433. next_sum_buffer = sum_buffer->next;
  434. free (sum_buffer);
  435. sum_buffer = next_sum_buffer;
  436. }
  437. /* Write execution counts for each function. */
  438. for (f_ix = 0; f_ix != gi_ptr->n_functions; f_ix++)
  439. {
  440. unsigned buffered = 0;
  441. const struct gcov_fn_info *gfi_ptr;
  442. const struct gcov_ctr_info *ci_ptr;
  443. gcov_unsigned_t length;
  444. unsigned t_ix;
  445. if (fn_buffer && fn_buffer->fn_ix == f_ix)
  446. {
  447. /* Buffered data from another program. */
  448. buffered = 1;
  449. gfi_ptr = &fn_buffer->info;
  450. length = GCOV_TAG_FUNCTION_LENGTH;
  451. }
  452. else
  453. {
  454. gfi_ptr = gi_ptr->functions[f_ix];
  455. if (gfi_ptr && gfi_ptr->key == gi_ptr)
  456. length = GCOV_TAG_FUNCTION_LENGTH;
  457. else
  458. length = 0;
  459. }
  460. gcov_write_tag_length (GCOV_TAG_FUNCTION, length);
  461. if (!length)
  462. continue;
  463. gcov_write_unsigned (gfi_ptr->ident);
  464. gcov_write_unsigned (gfi_ptr->lineno_checksum);
  465. gcov_write_unsigned (gfi_ptr->cfg_checksum);
  466. ci_ptr = gfi_ptr->ctrs;
  467. for (t_ix = 0; t_ix < GCOV_COUNTERS; t_ix++)
  468. {
  469. gcov_unsigned_t n_counts;
  470. gcov_type *c_ptr;
  471. if (!gi_ptr->merge[t_ix])
  472. continue;
  473. n_counts = ci_ptr->num;
  474. gcov_write_tag_length (GCOV_TAG_FOR_COUNTER (t_ix),
  475. GCOV_TAG_COUNTER_LENGTH (n_counts));
  476. c_ptr = ci_ptr->values;
  477. while (n_counts--)
  478. gcov_write_counter (*c_ptr++);
  479. ci_ptr++;
  480. }
  481. if (buffered)
  482. fn_buffer = free_fn_data (gi_ptr, fn_buffer, GCOV_COUNTERS);
  483. }
  484. gcov_write_unsigned (0);
  485. }
  486. /* Helper function for merging summary.
  487. Return -1 on error. Return 0 on success. */
  488. static int
  489. merge_summary (const char *filename, int run_counted,
  490. const struct gcov_info *gi_ptr, struct gcov_summary *prg,
  491. struct gcov_summary *this_prg, gcov_unsigned_t crc32,
  492. struct gcov_summary *all_prg __attribute__ ((unused)))
  493. {
  494. struct gcov_ctr_summary *cs_prg, *cs_tprg;
  495. unsigned t_ix;
  496. #if !GCOV_LOCKED
  497. /* summary for all instances of program. */
  498. struct gcov_ctr_summary *cs_all;
  499. #endif
  500. /* Merge the summaries. */
  501. for (t_ix = 0; t_ix < GCOV_COUNTERS_SUMMABLE; t_ix++)
  502. {
  503. cs_prg = &(prg->ctrs[t_ix]);
  504. cs_tprg = &(this_prg->ctrs[t_ix]);
  505. if (gi_ptr->merge[t_ix])
  506. {
  507. int first = !cs_prg->runs;
  508. if (!run_counted)
  509. cs_prg->runs++;
  510. if (first)
  511. cs_prg->num = cs_tprg->num;
  512. cs_prg->sum_all += cs_tprg->sum_all;
  513. if (cs_prg->run_max < cs_tprg->run_max)
  514. cs_prg->run_max = cs_tprg->run_max;
  515. cs_prg->sum_max += cs_tprg->run_max;
  516. if (first)
  517. memcpy (cs_prg->histogram, cs_tprg->histogram,
  518. sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
  519. else
  520. gcov_histogram_merge (cs_prg->histogram, cs_tprg->histogram);
  521. }
  522. else if (cs_prg->runs)
  523. {
  524. gcov_error ("profiling:%s:Merge mismatch for summary.\n",
  525. filename);
  526. return -1;
  527. }
  528. #if !GCOV_LOCKED
  529. cs_all = &all_prg->ctrs[t_ix];
  530. if (!cs_all->runs && cs_prg->runs)
  531. {
  532. cs_all->num = cs_prg->num;
  533. cs_all->runs = cs_prg->runs;
  534. cs_all->sum_all = cs_prg->sum_all;
  535. cs_all->run_max = cs_prg->run_max;
  536. cs_all->sum_max = cs_prg->sum_max;
  537. }
  538. else if (!all_prg->checksum
  539. /* Don't compare the histograms, which may have slight
  540. variations depending on the order they were updated
  541. due to the truncating integer divides used in the
  542. merge. */
  543. && (cs_all->num != cs_prg->num
  544. || cs_all->runs != cs_prg->runs
  545. || cs_all->sum_all != cs_prg->sum_all
  546. || cs_all->run_max != cs_prg->run_max
  547. || cs_all->sum_max != cs_prg->sum_max))
  548. {
  549. gcov_error ("profiling:%s:Data file mismatch - some "
  550. "data files may have been concurrently "
  551. "updated without locking support\n", filename);
  552. all_prg->checksum = ~0u;
  553. }
  554. #endif
  555. }
  556. prg->checksum = crc32;
  557. return 0;
  558. }
  559. /* Sort N entries in VALUE_ARRAY in descending order.
  560. Each entry in VALUE_ARRAY has two values. The sorting
  561. is based on the second value. */
  562. GCOV_LINKAGE void
  563. gcov_sort_n_vals (gcov_type *value_array, int n)
  564. {
  565. int j, k;
  566. for (j = 2; j < n; j += 2)
  567. {
  568. gcov_type cur_ent[2];
  569. cur_ent[0] = value_array[j];
  570. cur_ent[1] = value_array[j + 1];
  571. k = j - 2;
  572. while (k >= 0 && value_array[k + 1] < cur_ent[1])
  573. {
  574. value_array[k + 2] = value_array[k];
  575. value_array[k + 3] = value_array[k+1];
  576. k -= 2;
  577. }
  578. value_array[k + 2] = cur_ent[0];
  579. value_array[k + 3] = cur_ent[1];
  580. }
  581. }
  582. /* Sort the profile counters for all indirect call sites. Counters
  583. for each call site are allocated in array COUNTERS. */
  584. static void
  585. gcov_sort_icall_topn_counter (const struct gcov_ctr_info *counters)
  586. {
  587. int i;
  588. gcov_type *values;
  589. int n = counters->num;
  590. gcc_assert (!(n % GCOV_ICALL_TOPN_NCOUNTS));
  591. values = counters->values;
  592. for (i = 0; i < n; i += GCOV_ICALL_TOPN_NCOUNTS)
  593. {
  594. gcov_type *value_array = &values[i + 1];
  595. gcov_sort_n_vals (value_array, GCOV_ICALL_TOPN_NCOUNTS - 1);
  596. }
  597. }
  598. /* Sort topn indirect_call profile counters in GI_PTR. */
  599. static void
  600. gcov_sort_topn_counter_arrays (const struct gcov_info *gi_ptr)
  601. {
  602. unsigned int i;
  603. int f_ix;
  604. const struct gcov_fn_info *gfi_ptr;
  605. const struct gcov_ctr_info *ci_ptr;
  606. if (!gi_ptr->merge[GCOV_COUNTER_ICALL_TOPNV])
  607. return;
  608. for (f_ix = 0; (unsigned)f_ix != gi_ptr->n_functions; f_ix++)
  609. {
  610. gfi_ptr = gi_ptr->functions[f_ix];
  611. ci_ptr = gfi_ptr->ctrs;
  612. for (i = 0; i < GCOV_COUNTERS; i++)
  613. {
  614. if (!gi_ptr->merge[i])
  615. continue;
  616. if (i == GCOV_COUNTER_ICALL_TOPNV)
  617. {
  618. gcov_sort_icall_topn_counter (ci_ptr);
  619. break;
  620. }
  621. ci_ptr++;
  622. }
  623. }
  624. }
  625. /* Dump the coverage counts for one gcov_info object. We merge with existing
  626. counts when possible, to avoid growing the .da files ad infinitum. We use
  627. this program's checksum to make sure we only accumulate whole program
  628. statistics to the correct summary. An object file might be embedded
  629. in two separate programs, and we must keep the two program
  630. summaries separate. */
  631. static void
  632. dump_one_gcov (struct gcov_info *gi_ptr, struct gcov_filename *gf,
  633. unsigned run_counted,
  634. gcov_unsigned_t crc32, struct gcov_summary *all_prg,
  635. struct gcov_summary *this_prg)
  636. {
  637. struct gcov_summary prg; /* summary for this object over all program. */
  638. int error;
  639. gcov_unsigned_t tag;
  640. gcov_position_t summary_pos = 0;
  641. gcov_position_t eof_pos = 0;
  642. fn_buffer = 0;
  643. sum_buffer = 0;
  644. gcov_sort_topn_counter_arrays (gi_ptr);
  645. error = gcov_exit_open_gcda_file (gi_ptr, gf);
  646. if (error == -1)
  647. return;
  648. tag = gcov_read_unsigned ();
  649. if (tag)
  650. {
  651. /* Merge data from file. */
  652. if (tag != GCOV_DATA_MAGIC)
  653. {
  654. gcov_error ("profiling:%s:Not a gcov data file\n", gf->filename);
  655. goto read_fatal;
  656. }
  657. error = merge_one_data (gf->filename, gi_ptr, &prg, this_prg,
  658. &summary_pos, &eof_pos, crc32);
  659. if (error == -1)
  660. goto read_fatal;
  661. }
  662. gcov_rewrite ();
  663. if (!summary_pos)
  664. {
  665. memset (&prg, 0, sizeof (prg));
  666. summary_pos = eof_pos;
  667. }
  668. error = merge_summary (gf->filename, run_counted, gi_ptr, &prg, this_prg,
  669. crc32, all_prg);
  670. if (error == -1)
  671. goto read_fatal;
  672. write_one_data (gi_ptr, &prg, eof_pos, summary_pos);
  673. /* fall through */
  674. read_fatal:;
  675. while (fn_buffer)
  676. fn_buffer = free_fn_data (gi_ptr, fn_buffer, GCOV_COUNTERS);
  677. if ((error = gcov_close ()))
  678. gcov_error (error < 0 ?
  679. "profiling:%s:Overflow writing\n" :
  680. "profiling:%s:Error writing\n",
  681. gf->filename);
  682. }
  683. /* Dump all the coverage counts for the program. It first computes program
  684. summary and then traverses gcov_list list and dumps the gcov_info
  685. objects one by one. */
  686. #if !IN_GCOV_TOOL
  687. static
  688. #endif
  689. void
  690. gcov_do_dump (struct gcov_info *list, int run_counted)
  691. {
  692. struct gcov_info *gi_ptr;
  693. struct gcov_filename gf;
  694. gcov_unsigned_t crc32;
  695. struct gcov_summary all_prg;
  696. struct gcov_summary this_prg;
  697. crc32 = compute_summary (list, &this_prg, &gf.max_length);
  698. allocate_filename_struct (&gf);
  699. #if !GCOV_LOCKED
  700. memset (&all_prg, 0, sizeof (all_prg));
  701. #endif
  702. /* Now merge each file. */
  703. for (gi_ptr = list; gi_ptr; gi_ptr = gi_ptr->next)
  704. dump_one_gcov (gi_ptr, &gf, run_counted, crc32, &all_prg, &this_prg);
  705. free (gf.filename);
  706. }
  707. #if !IN_GCOV_TOOL
  708. void
  709. __gcov_dump_one (struct gcov_root *root)
  710. {
  711. if (root->dumped)
  712. return;
  713. gcov_do_dump (root->list, root->run_counted);
  714. root->dumped = 1;
  715. root->run_counted = 1;
  716. }
  717. /* Per-dynamic-object gcov state. */
  718. struct gcov_root __gcov_root;
  719. /* Exactly one of these will be live in the process image. */
  720. struct gcov_master __gcov_master =
  721. {GCOV_VERSION, 0};
  722. static void
  723. gcov_exit (void)
  724. {
  725. __gcov_dump_one (&__gcov_root);
  726. if (__gcov_root.next)
  727. __gcov_root.next->prev = __gcov_root.prev;
  728. if (__gcov_root.prev)
  729. __gcov_root.prev->next = __gcov_root.next;
  730. else
  731. __gcov_master.root = __gcov_root.next;
  732. }
  733. /* Add a new object file onto the bb chain. Invoked automatically
  734. when running an object file's global ctors. */
  735. void
  736. __gcov_init (struct gcov_info *info)
  737. {
  738. if (!info->version || !info->n_functions)
  739. return;
  740. if (gcov_version (info, info->version, 0))
  741. {
  742. if (!__gcov_root.list)
  743. {
  744. /* Add to master list and at exit function. */
  745. if (gcov_version (NULL, __gcov_master.version, "<master>"))
  746. {
  747. __gcov_root.next = __gcov_master.root;
  748. if (__gcov_master.root)
  749. __gcov_master.root->prev = &__gcov_root;
  750. __gcov_master.root = &__gcov_root;
  751. }
  752. atexit (gcov_exit);
  753. }
  754. info->next = __gcov_root.list;
  755. __gcov_root.list = info;
  756. }
  757. }
  758. #endif /* !IN_GCOV_TOOL */
  759. #endif /* L_gcov */
  760. #endif /* inhibit_libc */