Back to index

glibc  2.9
bb_exit_func.c
Go to the documentation of this file.
00001 /* Copyright (C) 1996,1997,1998,2000,2001,2002 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by David Mosberger (davidm@cs.arizona.edu).
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 /* __bb_exit_func() dumps all the basic-block statistics linked into
00021    the __bb_head chain to .d files.  */
00022 
00023 #include <sys/gmon.h>
00024 #include <sys/gmon_out.h>
00025 #include <sys/types.h>
00026 
00027 #include <stdio.h>
00028 #include <stdio_ext.h>
00029 #include <string.h>
00030 
00031 #define OUT_NAME     "gmon.out"
00032 
00033 
00034 extern struct __bb *__bb_head attribute_hidden;
00035 
00036 
00037 void
00038 __bb_exit_func (void)
00039 {
00040   const int version = GMON_VERSION;
00041   struct gmon_hdr ghdr;
00042   struct __bb *ptr;
00043   FILE *fp;
00044   fp = fopen (OUT_NAME, "wb");
00045   if (!fp)
00046     {
00047       perror (OUT_NAME);
00048       return;
00049     }
00050   /* No threads use this stream.  */
00051   __fsetlocking (fp, FSETLOCKING_BYCALLER);
00052 
00053   memcpy (&ghdr.cookie[0], GMON_MAGIC, 4);
00054   memcpy (&ghdr.version, &version, sizeof (version));
00055   fwrite_unlocked (&ghdr, sizeof (ghdr), 1, fp);
00056 
00057   for (ptr = __bb_head; ptr != 0; ptr = ptr->next)
00058     {
00059       u_int ncounts = ptr->ncounts;
00060       u_char tag;
00061       u_int i;
00062 
00063       tag = GMON_TAG_BB_COUNT;
00064       fwrite_unlocked (&tag, sizeof (tag), 1, fp);
00065       fwrite_unlocked (&ncounts, sizeof (ncounts), 1, fp);
00066 
00067       for (i = 0; i < ncounts; ++i)
00068        {
00069          fwrite_unlocked (&ptr->addresses[i], sizeof (ptr->addresses[0]), 1,
00070                         fp);
00071          fwrite_unlocked (&ptr->counts[i], sizeof (ptr->counts[0]), 1, fp);
00072        }
00073     }
00074   fclose (fp);
00075 }