source: trunk/src/binutils/gprof/call_graph.c@ 597

Last change on this file since 597 was 10, checked in by bird, 23 years ago

Initial revision

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