* Fix for PR 18665, from sky branch.
[binutils-gdb.git] / gprof / bb_exit_func.c
blob813321566c72c1b59b3ce5dc652f38b6e008fdf9
1 /*
2 * Copyright (c) 1994 David Mosberger-Tang.
4 * This is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Library General Public License as
6 * published by the Free Software Foundation; either version 2, or (at
7 * your option) any later version.
9 * __bb_exit_func() dumps all the basic-block statistics linked into
10 * the bb_head chain to .d files.
12 #include <stdio.h>
13 #include <strings.h>
14 #include "bfd.h"
15 #include "gmon_out.h"
17 /* structure emitted by -a */
18 struct bb {
19 long zero_word;
20 const char *filename;
21 long *counts;
22 long ncounts;
23 struct bb *next;
24 const unsigned long *addresses;
27 struct bb *__bb_head = (struct bb *)0;
30 void
31 __bb_exit_func (void)
33 const int version = GMON_VERSION;
34 struct gmon_hdr ghdr;
35 struct bb *ptr;
36 FILE *fp;
38 * GEN_GMON_CNT_FILE should be defined on systems with mcleanup()
39 * functions that do not write basic-block to gmon.out. In such
40 * cases profiling with "-pg -a" would result in a gmon.out file
41 * without basic-block info (because the file written here would
42 * be overwritten. Thus, a separate file is generated instead.
43 * The two files can easily be combined by specifying them
44 * on gprof's command line (and possibly generating a gmon.sum
45 * file with "gprof -s").
47 #ifndef GEN_GMON_CNT_FILE
48 # define OUT_NAME "gmon.out"
49 #else
50 # define OUT_NAME "gmon.cnt"
51 #endif
52 fp = fopen(OUT_NAME, "wb");
53 if (!fp) {
54 perror(OUT_NAME);
55 return;
56 } /* if */
57 memcpy(&ghdr.cookie[0], GMON_MAGIC, 4);
58 memcpy(&ghdr.version, &version, sizeof(version));
59 fwrite(&ghdr, sizeof(ghdr), 1, fp);
61 for (ptr = __bb_head; ptr != 0; ptr = ptr->next) {
62 u_int ncounts = ptr->ncounts;
63 u_char tag;
64 u_int i;
66 tag = GMON_TAG_BB_COUNT;
67 fwrite(&tag, sizeof(tag), 1, fp);
68 fwrite(&ncounts, sizeof(ncounts), 1, fp);
70 for (i = 0; i < ncounts; ++i) {
71 fwrite(&ptr->addresses[i], sizeof(ptr->addresses[0]), 1, fp);
72 fwrite(&ptr->counts[i], sizeof(ptr->counts[0]), 1, fp);
73 } /* for */
74 } /* for */
75 fclose (fp);
76 } /* __bb_exit_func */
78 /*** end of __bb_exit_func.c ***/