Back to index

cell-binutils  2.17cvs20070401
bb_exit_func.c
Go to the documentation of this file.
00001 /*  bb_exit_func.c - dumps all the basic-block statistics linked into
00002     the bb_head chain to .d files.
00003 
00004    Copyright 2000, 2001, 2004 Free Software Foundation, Inc.
00005 
00006    This file is part of GNU Binutils.
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2 of the License, or
00011    (at your option) any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
00021    02110-1301, USA.
00022 
00023    This code was contributed by:
00024 
00025      David Mosberger-Tang <David.Mosberger@acm.org>  */
00026 
00027 #include <stdio.h>
00028 #include <strings.h>
00029 #include "bfd.h"
00030 #include "gmon_out.h"
00031 
00032 /* structure emitted by -a */
00033 struct bb
00034 {
00035   long zero_word;
00036   const char *filename;
00037   long *counts;
00038   long ncounts;
00039   struct bb *next;
00040   const unsigned long *addresses;
00041 };
00042 
00043 struct bb *__bb_head = (struct bb *) 0;
00044 
00045 
00046 void
00047 __bb_exit_func ()
00048 {
00049   const int version = GMON_VERSION;
00050   struct gmon_hdr ghdr;
00051   struct bb *ptr;
00052   FILE *fp;
00053   /* GEN_GMON_CNT_FILE should be defined on systems with mcleanup()
00054      functions that do not write basic-block to gmon.out.  In such
00055      cases profiling with "-pg -a" would result in a gmon.out file
00056      without basic-block info (because the file written here would be
00057      overwritten.  Thus, a separate file is generated instead.  The
00058      two files can easily be combined by specifying them on gprof's
00059      command line (and possibly generating a gmon.sum file with "gprof
00060      -s"). */
00061 #ifndef GEN_GMON_CNT_FILE
00062 #   define OUT_NAME  "gmon.out"
00063 #else
00064 #   define OUT_NAME  "gmon.cnt"
00065 #endif
00066   fp = fopen (OUT_NAME, "wb");
00067   if (!fp)
00068     {
00069       perror (OUT_NAME);
00070       return;
00071     }
00072   memcpy (&ghdr.cookie[0], GMON_MAGIC, 4);
00073   memcpy (&ghdr.version, &version, sizeof (version));
00074   fwrite (&ghdr, sizeof (ghdr), 1, fp);
00075 
00076   for (ptr = __bb_head; ptr != 0; ptr = ptr->next)
00077     {
00078       u_int ncounts = ptr->ncounts;
00079       u_char tag;
00080       u_int i;
00081 
00082       tag = GMON_TAG_BB_COUNT;
00083       fwrite (&tag, sizeof (tag), 1, fp);
00084       fwrite (&ncounts, sizeof (ncounts), 1, fp);
00085 
00086       for (i = 0; i < ncounts; ++i)
00087        {
00088          fwrite (&ptr->addresses[i], sizeof (ptr->addresses[0]), 1, fp);
00089          fwrite (&ptr->counts[i], sizeof (ptr->counts[0]), 1, fp);
00090        }
00091     }
00092   fclose (fp);
00093 }