* alpha.c (alpha_find_call): Warning fixes.
[platform/upstream/binutils.git] / gprof / call_graph.c
1 /* call_graph.c  -  Create call graphs.
2
3    Copyright 2000, 2001, 2002 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 "gprof.h"
23 #include "search_list.h"
24 #include "source.h"
25 #include "symtab.h"
26 #include "cg_arcs.h"
27 #include "call_graph.h"
28 #include "corefile.h"
29 #include "gmon_io.h"
30 #include "gmon_out.h"
31 #include "sym_ids.h"
32
33 void
34 cg_tally (from_pc, self_pc, count)
35      bfd_vma from_pc;
36      bfd_vma self_pc;
37      unsigned long count;
38 {
39   Sym *parent;
40   Sym *child;
41
42   parent = sym_lookup (&symtab, from_pc);
43   child = sym_lookup (&symtab, self_pc);
44
45   if (child == NULL || parent == NULL)
46     return;
47
48   /* If we're doing line-by-line profiling, both the parent and the
49      child will probably point to line symbols instead of function
50      symbols.  For the parent this is fine, since this identifies the
51      line number in the calling routing, but the child should always
52      point to a function entry point, so we back up in the symbol
53      table until we find it.
54
55      For normal profiling, is_func will be set on all symbols, so this
56      code will do nothing.  */
57   while (child >= symtab.base && ! child->is_func)
58     --child;
59
60   if (child < symtab.base)
61     return;
62
63   /* Keep arc if it is on INCL_ARCS table or if the INCL_ARCS table
64      is empty and it is not in the EXCL_ARCS table.  */
65   if (sym_id_arc_is_present (&syms[INCL_ARCS], parent, child)
66       || (syms[INCL_ARCS].len == 0
67           && !sym_id_arc_is_present (&syms[EXCL_ARCS], parent, child)))
68     {
69       child->ncalls += count;
70       DBG (TALLYDEBUG,
71            printf (_("[cg_tally] arc from %s to %s traversed %lu times\n"),
72                    parent->name, child->name, count));
73       arc_add (parent, child, count);
74     }
75 }
76
77 /* Read a record from file IFP describing an arc in the function
78    call-graph and the count of how many times the arc has been
79    traversed.  FILENAME is the name of file IFP and is provided
80    for formatting error-messages only.  */
81
82 void
83 cg_read_rec (ifp, filename)
84      FILE *ifp;
85      const char *filename;
86 {
87   bfd_vma from_pc, self_pc;
88   unsigned int count;
89
90   if (gmon_io_read_vma (ifp, &from_pc)
91       || gmon_io_read_vma (ifp, &self_pc)
92       || gmon_io_read_32 (ifp, &count))
93     {
94       fprintf (stderr, _("%s: %s: unexpected end of file\n"),
95                whoami, filename);
96       done (1);
97     }
98
99   DBG (SAMPLEDEBUG,
100        printf ("[cg_read_rec] frompc 0x%lx selfpc 0x%lx count %lu\n",
101                (unsigned long) from_pc, (unsigned long) self_pc,
102                (unsigned long) count));
103   /* Add this arc:  */
104   cg_tally (from_pc, self_pc, count);
105 }
106
107 /* Write all the arcs in the call-graph to file OFP.  FILENAME is
108    the name of OFP and is provided for formatting error-messages
109    only.  */
110
111 void
112 cg_write_arcs (ofp, filename)
113      FILE *ofp;
114      const char *filename;
115 {
116   Arc *arc;
117   Sym *sym;
118
119   for (sym = symtab.base; sym < symtab.limit; sym++)
120     {
121       for (arc = sym->cg.children; arc; arc = arc->next_child)
122         {
123           if (gmon_io_write_8 (ofp, GMON_TAG_CG_ARC)
124               || gmon_io_write_vma (ofp, arc->parent->addr)
125               || gmon_io_write_vma (ofp, arc->child->addr)
126               || gmon_io_write_32 (ofp, arc->count))
127             {
128               perror (filename);
129               done (1);
130             }
131           DBG (SAMPLEDEBUG,
132              printf ("[cg_write_arcs] frompc 0x%lx selfpc 0x%lx count %lu\n",
133                      (unsigned long) arc->parent->addr,
134                      (unsigned long) arc->child->addr, arc->count));
135         }
136     }
137 }