2001-05-28 Philip Blundell <philb@gnu.org>
[binutils.git] / gprof / bb_exit_func.c
blobeb580fa5b0124d50afa6bd787f5b7784a5d2632c
1 /* bb_exit_func.c - dumps all the basic-block statistics linked into
2 the bb_head chain to .d files.
4 Copyright (C) 2000 Free Software Foundation, Inc.
6 This file is part of GNU Binutils.
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 2 of the License, or
11 (at your option) any later version.
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.
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., 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA.
23 This code was contributed by:
25 David Mosberger-Tang <David.Mosberger@acm.org> */
27 #include <stdio.h>
28 #include <strings.h>
29 #include "bfd.h"
30 #include "gmon_out.h"
32 /* structure emitted by -a */
33 struct bb {
34 long zero_word;
35 const char *filename;
36 long *counts;
37 long ncounts;
38 struct bb *next;
39 const unsigned long *addresses;
42 struct bb *__bb_head = (struct bb *)0;
45 void
46 __bb_exit_func (void)
48 const int version = GMON_VERSION;
49 struct gmon_hdr ghdr;
50 struct bb *ptr;
51 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
57 * be overwritten. Thus, a separate file is generated instead.
58 * The two files can easily be combined by specifying them
59 * on gprof's command line (and possibly generating a gmon.sum
60 * file with "gprof -s").
62 #ifndef GEN_GMON_CNT_FILE
63 # define OUT_NAME "gmon.out"
64 #else
65 # define OUT_NAME "gmon.cnt"
66 #endif
67 fp = fopen(OUT_NAME, "wb");
68 if (!fp) {
69 perror(OUT_NAME);
70 return;
71 } /* if */
72 memcpy(&ghdr.cookie[0], GMON_MAGIC, 4);
73 memcpy(&ghdr.version, &version, sizeof(version));
74 fwrite(&ghdr, sizeof(ghdr), 1, fp);
76 for (ptr = __bb_head; ptr != 0; ptr = ptr->next) {
77 u_int ncounts = ptr->ncounts;
78 u_char tag;
79 u_int i;
81 tag = GMON_TAG_BB_COUNT;
82 fwrite(&tag, sizeof(tag), 1, fp);
83 fwrite(&ncounts, sizeof(ncounts), 1, fp);
85 for (i = 0; i < ncounts; ++i) {
86 fwrite(&ptr->addresses[i], sizeof(ptr->addresses[0]), 1, fp);
87 fwrite(&ptr->counts[i], sizeof(ptr->counts[0]), 1, fp);
88 } /* for */
89 } /* for */
90 fclose (fp);
91 } /* __bb_exit_func */
93 /*** end of __bb_exit_func.c ***/