binutils-gdb/gprof/call_graph.c
Richard Allen c912005af7 gprof: only process line numbers for intersection of vmas and histograms
Some programs like RTOS firmware may have a large number of symbols.
The profile information in the profile data file includes histogram
records, which capture low PC and high PC of program execution.  If all
histogram records come in the profile data file before any call-graph
records and basic-block records, we can look up only the line numbers
within low PC and high PC in histogram records, which reduces processing
time for such a firmware from ~2 minutes to ~2 seconds.

Add symbol table access function, get_symtab, get_symtab_direct and
set_symtab to delay loading the symbol table until its first use.

	* aarch64.c (aarch64_find_call): Call get_symtab to get the
	symbol table pointer
	* alpha.c (alpha_find_call): Likewise.
	* basic_blocks.c (bb_read_rec): Likewise.
	(bb_write_blocks): Likewise.
	(print_exec_counts): Likewise.
	(print_annotated_source): Likewise.
	* call_graph.c (cg_tally): Likewise.
	(cg_write_arcs): Likewise.
	* cg_arcs.c (cycle_link): Likewise.
	(propagate_flags): Likewise.
	(cg_assemble): Likewise.
	* cg_print.c (cg_print): Likewise.
	(cg_print_index): Likewise.
	(cg_print_function_ordering): Likewise.
	* corefile.c: Include "gmon_io.h".
	(core_create_syms_from): Call get_symtab_direct to get the
	symbol table pointer.
	(core_create_function_syms): Likewise.
	(core_create_line_syms): Likewise.  If all histogram records
	come in the profile data file before any call-graph records and
	basic-block records, we can look up only the line numbers within
	low PC and high PC in histogram records.
	* gmon_io.c (gmon_histograms_first): New.
	(gmon_out_read): Set gmon_histograms_first to true if all
	histogram records come first.
	(gmon_out_write): Call get_symtab to get the symbol table
	pointer.
	* hist.c (scale_and_align_entries): Likewise.
	(hist_assign_samples_1): Likewise.
	(hist_print): Likewise.
	* i386.c (i386_find_call): Likewise.
	* mips.c (mips_find_call): Likewise.
	* sparc.c (sparc_find_call): Likewise.
	* sym_ids.c (sym_id_parse): Likewise.
	* vax.c (vax_find_call): Likewise.
	* gmon_io.h (gmon_histograms_first): New.
	* gprof.c (man): Don't create profile info.
	(symtab_init): New.
	* gprof.h (symtab_init): New.
	* symtab.c (symtab): Changed to static.
	(get_symtab_direct): New.
	(get_symtab): Likewise.
	(set_symtab): Likewise.
	* symtab.h (symtab): Removed.
	(get_symtab_direct): New.
	(get_symtab): Likewise.
	(set_symtab): Likewise.

Signed-off-by: Richard Allen <rsaxvc@gmail.com>
Co-Authored-By: H.J. Lu <hjl.tools@gmail.com>
2025-03-14 07:03:52 -07:00

133 lines
4.0 KiB
C

/* call_graph.c - Create call graphs.
Copyright (C) 1999-2025 Free Software Foundation, Inc.
This file is part of GNU Binutils.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
02110-1301, USA. */
#include "gprof.h"
#include "search_list.h"
#include "source.h"
#include "symtab.h"
#include "cg_arcs.h"
#include "call_graph.h"
#include "corefile.h"
#include "gmon_io.h"
#include "gmon_out.h"
#include "sym_ids.h"
void
cg_tally (bfd_vma from_pc, bfd_vma self_pc, unsigned long count)
{
Sym *parent;
Sym *child;
Sym_Table *symtab = get_symtab ();
parent = sym_lookup (symtab, from_pc);
child = sym_lookup (symtab, self_pc);
if (child == NULL || parent == NULL)
return;
/* If we're doing line-by-line profiling, both the parent and the
child will probably point to line symbols instead of function
symbols. For the parent this is fine, since this identifies the
line number in the calling routing, but the child should always
point to a function entry point, so we back up in the symbol
table until we find it.
For normal profiling, is_func will be set on all symbols, so this
code will do nothing. */
while (child >= symtab->base && ! child->is_func)
--child;
if (child < symtab->base)
return;
/* Keep arc if it is on INCL_ARCS table or if the INCL_ARCS table
is empty and it is not in the EXCL_ARCS table. */
if (sym_id_arc_is_present (&syms[INCL_ARCS], parent, child)
|| (syms[INCL_ARCS].len == 0
&& !sym_id_arc_is_present (&syms[EXCL_ARCS], parent, child)))
{
child->ncalls += count;
DBG (TALLYDEBUG,
printf (_("[cg_tally] arc from %s to %s traversed %lu times\n"),
parent->name, child->name, count));
arc_add (parent, child, count);
}
}
/* Read a record from file IFP describing an arc in the function
call-graph and the count of how many times the arc has been
traversed. FILENAME is the name of file IFP and is provided
for formatting error-messages only. */
void
cg_read_rec (FILE *ifp, const char *filename)
{
bfd_vma from_pc, self_pc;
unsigned int count;
if (gmon_io_read_vma (ifp, &from_pc)
|| gmon_io_read_vma (ifp, &self_pc)
|| gmon_io_read_32 (ifp, &count))
{
fprintf (stderr, _("%s: %s: unexpected end of file\n"),
whoami, filename);
done (1);
}
DBG (SAMPLEDEBUG,
printf ("[cg_read_rec] frompc 0x%lx selfpc 0x%lx count %lu\n",
(unsigned long) from_pc, (unsigned long) self_pc,
(unsigned long) count));
/* Add this arc: */
cg_tally (from_pc, self_pc, count);
}
/* Write all the arcs in the call-graph to file OFP. FILENAME is
the name of OFP and is provided for formatting error-messages
only. */
void
cg_write_arcs (FILE *ofp, const char *filename)
{
Arc *arc;
Sym *sym;
Sym_Table *symtab = get_symtab ();
for (sym = symtab->base; sym < symtab->limit; sym++)
{
for (arc = sym->cg.children; arc; arc = arc->next_child)
{
if (gmon_io_write_8 (ofp, GMON_TAG_CG_ARC)
|| gmon_io_write_vma (ofp, arc->parent->addr)
|| gmon_io_write_vma (ofp, arc->child->addr)
|| gmon_io_write_32 (ofp, arc->count))
{
perror (filename);
done (1);
}
DBG (SAMPLEDEBUG,
printf ("[cg_write_arcs] frompc 0x%lx selfpc 0x%lx count %lu\n",
(unsigned long) arc->parent->addr,
(unsigned long) arc->child->addr, arc->count));
}
}
}