Fix typos in ChangeLog; fix dates in copyright notices
[external/binutils.git] / gprof / call_graph.c
1 /* call_graph.c  -  Create call graphs.
2
3    Copyright 2000 Free Software Foundation, Inc.
4
5    This file is part of GNU Binutils.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21 \f
22 #include "cg_arcs.h"
23 #include "call_graph.h"
24 #include "corefile.h"
25 #include "gmon_io.h"
26 #include "gmon_out.h"
27 #include "symtab.h"
28 #include "sym_ids.h"
29
30 extern void
31 DEFUN (cg_tally, (from_pc, self_pc, count),
32        bfd_vma from_pc AND bfd_vma self_pc AND unsigned long count)
33 {
34   Sym *parent;
35   Sym *child;
36
37   parent = sym_lookup (&symtab, from_pc);
38   child = sym_lookup (&symtab, self_pc);
39
40   if (child == NULL || parent == NULL)
41     return;
42
43   /* If we're doing line-by-line profiling, both the parent and the
44      child will probably point to line symbols instead of function
45      symbols.  For the parent this is fine, since this identifies the
46      line number in the calling routing, but the child should always
47      point to a function entry point, so we back up in the symbol
48      table until we find it.
49    
50      For normal profiling, is_func will be set on all symbols, so this
51      code will do nothing.  */
52   while (child >= symtab.base && ! child->is_func)
53     --child;
54
55   if (child < symtab.base)
56     return;
57
58   /* Keep arc if it is on INCL_ARCS table or if the INCL_ARCS table
59      is empty and it is not in the EXCL_ARCS table.  */
60   if (sym_id_arc_is_present (&syms[INCL_ARCS], parent, child)
61       || (syms[INCL_ARCS].len == 0
62           && !sym_id_arc_is_present (&syms[EXCL_ARCS], parent, child)))
63     {
64       child->ncalls += count;
65       DBG (TALLYDEBUG,
66            printf (_("[cg_tally] arc from %s to %s traversed %lu times\n"),
67                    parent->name, child->name, count));
68       arc_add (parent, child, count);
69     }
70 }
71
72 /* Read a record from file IFP describing an arc in the function
73    call-graph and the count of how many times the arc has been
74    traversed.  FILENAME is the name of file IFP and is provided
75    for formatting error-messages only.  */
76
77 void
78 DEFUN (cg_read_rec, (ifp, filename), FILE * ifp AND CONST char *filename)
79 {
80   bfd_vma from_pc, self_pc;
81   struct gmon_cg_arc_record arc;
82   unsigned long count;
83
84   if (fread (&arc, sizeof (arc), 1, ifp) != 1)
85     {
86       fprintf (stderr, _("%s: %s: unexpected end of file\n"),
87                whoami, filename);
88       done (1);
89     }
90   
91   from_pc = get_vma (core_bfd, (bfd_byte *) arc.from_pc);
92   self_pc = get_vma (core_bfd, (bfd_byte *) arc.self_pc);
93   count = bfd_get_32 (core_bfd, (bfd_byte *) arc.count);
94   DBG (SAMPLEDEBUG,
95        printf ("[cg_read_rec] frompc 0x%lx selfpc 0x%lx count %lu\n",
96                (unsigned long) from_pc, (unsigned long) self_pc, count));
97   /* Add this arc:  */
98   cg_tally (from_pc, self_pc, count);
99 }
100
101 /* Write all the arcs in the call-graph to file OFP.  FILENAME is
102    the name of OFP and is provided for formatting error-messages
103    only.  */
104
105 void
106 DEFUN (cg_write_arcs, (ofp, filename), FILE * ofp AND const char *filename)
107 {
108   const unsigned char tag = GMON_TAG_CG_ARC;
109   struct gmon_cg_arc_record raw_arc;
110   Arc *arc;
111   Sym *sym;
112
113   for (sym = symtab.base; sym < symtab.limit; sym++)
114     {
115       for (arc = sym->cg.children; arc; arc = arc->next_child)
116         {
117           put_vma (core_bfd, arc->parent->addr, (bfd_byte *) raw_arc.from_pc);
118           put_vma (core_bfd, arc->child->addr, (bfd_byte *) raw_arc.self_pc);
119           bfd_put_32 (core_bfd, arc->count, (bfd_byte *) raw_arc.count);
120           if (fwrite (&tag, sizeof (tag), 1, ofp) != 1
121               || fwrite (&raw_arc, sizeof (raw_arc), 1, ofp) != 1)
122             {
123               perror (filename);
124               done (1);
125             }
126           DBG (SAMPLEDEBUG,
127              printf ("[cg_write_arcs] frompc 0x%lx selfpc 0x%lx count %lu\n",
128                      (unsigned long) arc->parent->addr,
129                      (unsigned long) arc->child->addr, arc->count));
130         }
131     }
132 }