175fd0b74Schristos /* bb_exit_func.c - dumps all the basic-block statistics linked into
275fd0b74Schristos the bb_head chain to .d files.
375fd0b74Schristos
4*e992f068Schristos Copyright (C) 2000-2022 Free Software Foundation, Inc.
575fd0b74Schristos
675fd0b74Schristos This file is part of GNU Binutils.
775fd0b74Schristos
875fd0b74Schristos This program is free software; you can redistribute it and/or modify
975fd0b74Schristos it under the terms of the GNU General Public License as published by
1075fd0b74Schristos the Free Software Foundation; either version 3 of the License, or
1175fd0b74Schristos (at your option) any later version.
1275fd0b74Schristos
1375fd0b74Schristos This program is distributed in the hope that it will be useful,
1475fd0b74Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
1575fd0b74Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1675fd0b74Schristos GNU General Public License for more details.
1775fd0b74Schristos
1875fd0b74Schristos You should have received a copy of the GNU General Public License
1975fd0b74Schristos along with this program; if not, write to the Free Software
2075fd0b74Schristos Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
2175fd0b74Schristos 02110-1301, USA.
2275fd0b74Schristos
2375fd0b74Schristos This code was contributed by:
2475fd0b74Schristos
2575fd0b74Schristos David Mosberger-Tang <David.Mosberger@acm.org> */
2675fd0b74Schristos
2775fd0b74Schristos #include <stdio.h>
2875fd0b74Schristos #include <strings.h>
2975fd0b74Schristos #include "bfd.h"
3075fd0b74Schristos #include "gmon_out.h"
3175fd0b74Schristos
3275fd0b74Schristos /* structure emitted by -a */
3375fd0b74Schristos struct bb
3475fd0b74Schristos {
3575fd0b74Schristos long zero_word;
3675fd0b74Schristos const char *filename;
3775fd0b74Schristos long *counts;
3875fd0b74Schristos long ncounts;
3975fd0b74Schristos struct bb *next;
4075fd0b74Schristos const unsigned long *addresses;
4175fd0b74Schristos };
4275fd0b74Schristos
4375fd0b74Schristos struct bb *__bb_head = (struct bb *) 0;
4475fd0b74Schristos
4575fd0b74Schristos
4675fd0b74Schristos void
__bb_exit_func()4775fd0b74Schristos __bb_exit_func ()
4875fd0b74Schristos {
4975fd0b74Schristos const int version = GMON_VERSION;
5075fd0b74Schristos struct gmon_hdr ghdr;
5175fd0b74Schristos struct bb *ptr;
5275fd0b74Schristos FILE *fp;
5375fd0b74Schristos /* GEN_GMON_CNT_FILE should be defined on systems with mcleanup()
5475fd0b74Schristos functions that do not write basic-block to gmon.out. In such
5575fd0b74Schristos cases profiling with "-pg -a" would result in a gmon.out file
5675fd0b74Schristos without basic-block info (because the file written here would be
5775fd0b74Schristos overwritten. Thus, a separate file is generated instead. The
5875fd0b74Schristos two files can easily be combined by specifying them on gprof's
5975fd0b74Schristos command line (and possibly generating a gmon.sum file with "gprof
6075fd0b74Schristos -s"). */
6175fd0b74Schristos #ifndef GEN_GMON_CNT_FILE
6275fd0b74Schristos # define OUT_NAME "gmon.out"
6375fd0b74Schristos #else
6475fd0b74Schristos # define OUT_NAME "gmon.cnt"
6575fd0b74Schristos #endif
6675fd0b74Schristos fp = fopen (OUT_NAME, "wb");
6775fd0b74Schristos if (!fp)
6875fd0b74Schristos {
6975fd0b74Schristos perror (OUT_NAME);
7075fd0b74Schristos return;
7175fd0b74Schristos }
7275fd0b74Schristos memcpy (&ghdr.cookie[0], GMON_MAGIC, 4);
7375fd0b74Schristos memcpy (&ghdr.version, &version, sizeof (version));
7475fd0b74Schristos fwrite (&ghdr, sizeof (ghdr), 1, fp);
7575fd0b74Schristos
7675fd0b74Schristos for (ptr = __bb_head; ptr != 0; ptr = ptr->next)
7775fd0b74Schristos {
7875fd0b74Schristos u_int ncounts = ptr->ncounts;
7975fd0b74Schristos u_char tag;
8075fd0b74Schristos u_int i;
8175fd0b74Schristos
8275fd0b74Schristos tag = GMON_TAG_BB_COUNT;
8375fd0b74Schristos fwrite (&tag, sizeof (tag), 1, fp);
8475fd0b74Schristos fwrite (&ncounts, sizeof (ncounts), 1, fp);
8575fd0b74Schristos
8675fd0b74Schristos for (i = 0; i < ncounts; ++i)
8775fd0b74Schristos {
8875fd0b74Schristos fwrite (&ptr->addresses[i], sizeof (ptr->addresses[0]), 1, fp);
8975fd0b74Schristos fwrite (&ptr->counts[i], sizeof (ptr->counts[0]), 1, fp);
9075fd0b74Schristos }
9175fd0b74Schristos }
9275fd0b74Schristos fclose (fp);
9375fd0b74Schristos }
94