bb_exit_func.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* bb_exit_func.c - dumps all the basic-block statistics linked into
  2. the bb_head chain to .d files.
  3. Copyright (C) 2000-2022 Free Software Foundation, Inc.
  4. This file is part of GNU Binutils.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  16. 02110-1301, USA.
  17. This code was contributed by:
  18. David Mosberger-Tang <David.Mosberger@acm.org> */
  19. #include <stdio.h>
  20. #include <strings.h>
  21. #include "bfd.h"
  22. #include "gmon_out.h"
  23. /* structure emitted by -a */
  24. struct bb
  25. {
  26. long zero_word;
  27. const char *filename;
  28. long *counts;
  29. long ncounts;
  30. struct bb *next;
  31. const unsigned long *addresses;
  32. };
  33. struct bb *__bb_head = (struct bb *) 0;
  34. void
  35. __bb_exit_func ()
  36. {
  37. const int version = GMON_VERSION;
  38. struct gmon_hdr ghdr;
  39. struct bb *ptr;
  40. FILE *fp;
  41. /* GEN_GMON_CNT_FILE should be defined on systems with mcleanup()
  42. functions that do not write basic-block to gmon.out. In such
  43. cases profiling with "-pg -a" would result in a gmon.out file
  44. without basic-block info (because the file written here would be
  45. overwritten. Thus, a separate file is generated instead. The
  46. two files can easily be combined by specifying them on gprof's
  47. command line (and possibly generating a gmon.sum file with "gprof
  48. -s"). */
  49. #ifndef GEN_GMON_CNT_FILE
  50. # define OUT_NAME "gmon.out"
  51. #else
  52. # define OUT_NAME "gmon.cnt"
  53. #endif
  54. fp = fopen (OUT_NAME, "wb");
  55. if (!fp)
  56. {
  57. perror (OUT_NAME);
  58. return;
  59. }
  60. memcpy (&ghdr.cookie[0], GMON_MAGIC, 4);
  61. memcpy (&ghdr.version, &version, sizeof (version));
  62. fwrite (&ghdr, sizeof (ghdr), 1, fp);
  63. for (ptr = __bb_head; ptr != 0; ptr = ptr->next)
  64. {
  65. u_int ncounts = ptr->ncounts;
  66. u_char tag;
  67. u_int i;
  68. tag = GMON_TAG_BB_COUNT;
  69. fwrite (&tag, sizeof (tag), 1, fp);
  70. fwrite (&ncounts, sizeof (ncounts), 1, fp);
  71. for (i = 0; i < ncounts; ++i)
  72. {
  73. fwrite (&ptr->addresses[i], sizeof (ptr->addresses[0]), 1, fp);
  74. fwrite (&ptr->counts[i], sizeof (ptr->counts[0]), 1, fp);
  75. }
  76. }
  77. fclose (fp);
  78. }