12a6b7db3Sskrll /* bb_exit_func.c - dumps all the basic-block statistics linked into
22a6b7db3Sskrll the bb_head chain to .d files.
32a6b7db3Sskrll
4*cb63e24eSchristos Copyright (C) 2000-2024 Free Software Foundation, Inc.
52a6b7db3Sskrll
62a6b7db3Sskrll This file is part of GNU Binutils.
72a6b7db3Sskrll
82a6b7db3Sskrll This program is free software; you can redistribute it and/or modify
92a6b7db3Sskrll it under the terms of the GNU General Public License as published by
102a6b7db3Sskrll the Free Software Foundation; either version 3 of the License, or
112a6b7db3Sskrll (at your option) any later version.
122a6b7db3Sskrll
132a6b7db3Sskrll This program is distributed in the hope that it will be useful,
142a6b7db3Sskrll but WITHOUT ANY WARRANTY; without even the implied warranty of
152a6b7db3Sskrll MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
162a6b7db3Sskrll GNU General Public License for more details.
172a6b7db3Sskrll
182a6b7db3Sskrll You should have received a copy of the GNU General Public License
192a6b7db3Sskrll along with this program; if not, write to the Free Software
202a6b7db3Sskrll Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
212a6b7db3Sskrll 02110-1301, USA.
222a6b7db3Sskrll
232a6b7db3Sskrll This code was contributed by:
242a6b7db3Sskrll
252a6b7db3Sskrll David Mosberger-Tang <David.Mosberger@acm.org> */
262a6b7db3Sskrll
272a6b7db3Sskrll #include <stdio.h>
282a6b7db3Sskrll #include <strings.h>
292a6b7db3Sskrll #include "bfd.h"
302a6b7db3Sskrll #include "gmon_out.h"
312a6b7db3Sskrll
322a6b7db3Sskrll /* structure emitted by -a */
332a6b7db3Sskrll struct bb
342a6b7db3Sskrll {
352a6b7db3Sskrll long zero_word;
362a6b7db3Sskrll const char *filename;
372a6b7db3Sskrll long *counts;
382a6b7db3Sskrll long ncounts;
392a6b7db3Sskrll struct bb *next;
402a6b7db3Sskrll const unsigned long *addresses;
412a6b7db3Sskrll };
422a6b7db3Sskrll
432a6b7db3Sskrll struct bb *__bb_head = (struct bb *) 0;
442a6b7db3Sskrll
452a6b7db3Sskrll
462a6b7db3Sskrll void
__bb_exit_func()472a6b7db3Sskrll __bb_exit_func ()
482a6b7db3Sskrll {
492a6b7db3Sskrll const int version = GMON_VERSION;
502a6b7db3Sskrll struct gmon_hdr ghdr;
512a6b7db3Sskrll struct bb *ptr;
522a6b7db3Sskrll FILE *fp;
532a6b7db3Sskrll /* GEN_GMON_CNT_FILE should be defined on systems with mcleanup()
542a6b7db3Sskrll functions that do not write basic-block to gmon.out. In such
552a6b7db3Sskrll cases profiling with "-pg -a" would result in a gmon.out file
562a6b7db3Sskrll without basic-block info (because the file written here would be
572a6b7db3Sskrll overwritten. Thus, a separate file is generated instead. The
582a6b7db3Sskrll two files can easily be combined by specifying them on gprof's
592a6b7db3Sskrll command line (and possibly generating a gmon.sum file with "gprof
602a6b7db3Sskrll -s"). */
612a6b7db3Sskrll #ifndef GEN_GMON_CNT_FILE
622a6b7db3Sskrll # define OUT_NAME "gmon.out"
632a6b7db3Sskrll #else
642a6b7db3Sskrll # define OUT_NAME "gmon.cnt"
652a6b7db3Sskrll #endif
662a6b7db3Sskrll fp = fopen (OUT_NAME, "wb");
672a6b7db3Sskrll if (!fp)
682a6b7db3Sskrll {
692a6b7db3Sskrll perror (OUT_NAME);
702a6b7db3Sskrll return;
712a6b7db3Sskrll }
722a6b7db3Sskrll memcpy (&ghdr.cookie[0], GMON_MAGIC, 4);
732a6b7db3Sskrll memcpy (&ghdr.version, &version, sizeof (version));
742a6b7db3Sskrll fwrite (&ghdr, sizeof (ghdr), 1, fp);
752a6b7db3Sskrll
762a6b7db3Sskrll for (ptr = __bb_head; ptr != 0; ptr = ptr->next)
772a6b7db3Sskrll {
782a6b7db3Sskrll u_int ncounts = ptr->ncounts;
792a6b7db3Sskrll u_char tag;
802a6b7db3Sskrll u_int i;
812a6b7db3Sskrll
822a6b7db3Sskrll tag = GMON_TAG_BB_COUNT;
832a6b7db3Sskrll fwrite (&tag, sizeof (tag), 1, fp);
842a6b7db3Sskrll fwrite (&ncounts, sizeof (ncounts), 1, fp);
852a6b7db3Sskrll
862a6b7db3Sskrll for (i = 0; i < ncounts; ++i)
872a6b7db3Sskrll {
882a6b7db3Sskrll fwrite (&ptr->addresses[i], sizeof (ptr->addresses[0]), 1, fp);
892a6b7db3Sskrll fwrite (&ptr->counts[i], sizeof (ptr->counts[0]), 1, fp);
902a6b7db3Sskrll }
912a6b7db3Sskrll }
922a6b7db3Sskrll fclose (fp);
932a6b7db3Sskrll }
94