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