11590Srgrimes/* 21590Srgrimes * Copyright (c) 1983, 1993 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * Redistribution and use in source and binary forms, with or without 61590Srgrimes * modification, are permitted provided that the following conditions 71590Srgrimes * are met: 81590Srgrimes * 1. Redistributions of source code must retain the above copyright 91590Srgrimes * notice, this list of conditions and the following disclaimer. 101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111590Srgrimes * notice, this list of conditions and the following disclaimer in the 121590Srgrimes * documentation and/or other materials provided with the distribution. 131590Srgrimes * 4. Neither the name of the University nor the names of its contributors 141590Srgrimes * may be used to endorse or promote products derived from this software 151590Srgrimes * without specific prior written permission. 161590Srgrimes * 171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271590Srgrimes * SUCH DAMAGE. 281590Srgrimes * 291590Srgrimes * @(#)gprof.h 8.1 (Berkeley) 6/6/93 3085353Speter * $FreeBSD$ 311590Srgrimes */ 321590Srgrimes 331590Srgrimes#include <sys/types.h> 341590Srgrimes#include <sys/stat.h> 351590Srgrimes#include <sys/gmon.h> 361590Srgrimes 371590Srgrimes#include <stdio.h> 381590Srgrimes#include <stdlib.h> 391590Srgrimes 40129398Speter#if __amd64__ 41129398Speter# include "amd64.h" 42129398Speter#endif 43129243Sbde#if __arm__ 44129243Sbde# include "arm.h" 4591976Sjake#endif 46129243Sbde#if __i386__ 47129243Sbde# include "i386.h" 48129243Sbde#endif 49129243Sbde#if __ia64__ 50129243Sbde# include "ia64.h" 51129243Sbde#endif 52177926Simp#if __mips__ 53177926Simp# include "mips.h" 54177926Simp#endif 55107718Sgrehan#if __powerpc__ 56107718Sgrehan# include "powerpc.h" 57107718Sgrehan#endif 58129243Sbde#if __sparc64__ 59129243Sbde# include "sparc64.h" 601590Srgrimes#endif 611590Srgrimes 621590Srgrimes /* 631590Srgrimes * booleans 641590Srgrimes */ 651590Srgrimestypedef int bool; 661590Srgrimes#define FALSE 0 671590Srgrimes#define TRUE 1 681590Srgrimes 691590Srgrimes /* 7091735Sbde * Historical scale factor in profil(2)'s algorithm for converting 7191735Sbde * pc addresses to bucket numbers. This now just complicates the 7291735Sbde * scaling and makes bucket:pc densities of more than 1/2 useless. 7391735Sbde */ 7491735Sbde#define HISTORICAL_SCALE_2 2 7591735Sbde 7691735Sbde /* 771590Srgrimes * ticks per second 781590Srgrimes */ 791590Srgrimeslong hz; 801590Srgrimes 8191738Sbdesize_t histcounter_size; 8291738Sbdeint histcounter_type; 8391010Sbde 841590Srgrimeschar *a_outname; 851590Srgrimes#define A_OUTNAME "a.out" 861590Srgrimes 871590Srgrimeschar *gmonname; 881590Srgrimes#define GMONSUM "gmon.sum" 891590Srgrimes 901590Srgrimes /* 911590Srgrimes * a constructed arc, 921590Srgrimes * with pointers to the namelist entry of the parent and the child, 931590Srgrimes * a count of how many times this arc was traversed, 941590Srgrimes * and pointers to the next parent of this child and 951590Srgrimes * the next child of this parent. 961590Srgrimes */ 971590Srgrimesstruct arcstruct { 981590Srgrimes struct nl *arc_parentp; /* pointer to parent's nl entry */ 991590Srgrimes struct nl *arc_childp; /* pointer to child's nl entry */ 1001590Srgrimes long arc_count; /* num calls from parent to child */ 1011590Srgrimes double arc_time; /* time inherited along arc */ 1021590Srgrimes double arc_childtime; /* childtime inherited along arc */ 1031590Srgrimes struct arcstruct *arc_parentlist; /* parents-of-this-child list */ 1041590Srgrimes struct arcstruct *arc_childlist; /* children-of-this-parent list */ 1051590Srgrimes struct arcstruct *arc_next; /* list of arcs on cycle */ 1061590Srgrimes unsigned short arc_cyclecnt; /* num cycles involved in */ 1071590Srgrimes unsigned short arc_flags; /* see below */ 1081590Srgrimes}; 1091590Srgrimestypedef struct arcstruct arctype; 1101590Srgrimes 1111590Srgrimes /* 1121590Srgrimes * arc flags 1131590Srgrimes */ 1141590Srgrimes#define DEADARC 0x01 /* time should not propagate across the arc */ 1151590Srgrimes#define ONLIST 0x02 /* arc is on list of arcs in cycles */ 1161590Srgrimes 1171590Srgrimes /* 1181590Srgrimes * The symbol table; 1191590Srgrimes * for each external in the specified file we gather 120105243Scharnier * its address, the number of calls and compute its share of CPU time. 1211590Srgrimes */ 1221590Srgrimesstruct nl { 12338928Sjdp const char *name; /* the name */ 1241590Srgrimes unsigned long value; /* the pc entry point */ 1251590Srgrimes unsigned long svalue; /* entry point aligned to histograms */ 1261590Srgrimes double time; /* ticks in this routine */ 1271590Srgrimes double childtime; /* cumulative ticks in children */ 1281590Srgrimes long ncall; /* how many times called */ 1291590Srgrimes long npropcall; /* times called by live arcs */ 1301590Srgrimes long selfcalls; /* how many calls to self */ 1311590Srgrimes double propfraction; /* what % of time propagates */ 1321590Srgrimes double propself; /* how much self time propagates */ 1331590Srgrimes double propchild; /* how much child time propagates */ 1341590Srgrimes short printflag; /* should this be printed? */ 1351590Srgrimes short flags; /* see below */ 1361590Srgrimes int index; /* index in the graph list */ 1371590Srgrimes int toporder; /* graph call chain top-sort order */ 1381590Srgrimes int cycleno; /* internal number of cycle on */ 1391590Srgrimes int parentcnt; /* number of live parent arcs */ 1401590Srgrimes struct nl *cyclehead; /* pointer to head of cycle */ 1411590Srgrimes struct nl *cnext; /* pointer to next member of cycle */ 1421590Srgrimes arctype *parents; /* list of caller arcs */ 1431590Srgrimes arctype *children; /* list of callee arcs */ 1441590Srgrimes}; 1451590Srgrimestypedef struct nl nltype; 1461590Srgrimes 1471590Srgrimesnltype *nl; /* the whole namelist */ 1481590Srgrimesnltype *npe; /* the virtual end of the namelist */ 1491590Srgrimesint nname; /* the number of function names */ 1501590Srgrimes 1511590Srgrimes#define HASCYCLEXIT 0x08 /* node has arc exiting from cycle */ 1521590Srgrimes#define CYCLEHEAD 0x10 /* node marked as head of a cycle */ 1531590Srgrimes#define VISITED 0x20 /* node visited during a cycle */ 1541590Srgrimes 1551590Srgrimes /* 1561590Srgrimes * The cycle list. 1571590Srgrimes * for each subcycle within an identified cycle, we gather 1581590Srgrimes * its size and the list of included arcs. 1591590Srgrimes */ 1601590Srgrimesstruct cl { 1611590Srgrimes int size; /* length of cycle */ 1621590Srgrimes struct cl *next; /* next member of list */ 1631590Srgrimes arctype *list[1]; /* list of arcs in cycle */ 1641590Srgrimes /* actually longer */ 1651590Srgrimes}; 1661590Srgrimestypedef struct cl cltype; 1671590Srgrimes 1681590Srgrimesarctype *archead; /* the head of arcs in current cycle list */ 1691590Srgrimescltype *cyclehead; /* the head of the list */ 1701590Srgrimesint cyclecnt; /* the number of cycles found */ 1711590Srgrimes#define CYCLEMAX 100 /* maximum cycles before cutting one of them */ 1721590Srgrimes 1731590Srgrimes /* 1741590Srgrimes * flag which marks a nl entry as topologically ``busy'' 1751590Srgrimes * flag which marks a nl entry as topologically ``not_numbered'' 1761590Srgrimes */ 1771590Srgrimes#define DFN_BUSY -1 1781590Srgrimes#define DFN_NAN 0 1791590Srgrimes 1808874Srgrimes /* 1811590Srgrimes * namelist entries for cycle headers. 1821590Srgrimes * the number of discovered cycles. 1831590Srgrimes */ 1841590Srgrimesnltype *cyclenl; /* cycle header namelist */ 1851590Srgrimesint ncycle; /* number of cycles discovered */ 1861590Srgrimes 1871590Srgrimes /* 1881590Srgrimes * The header on the gmon.out file. 1891590Srgrimes * gmon.out consists of a struct phdr (defined in gmon.h) 1901590Srgrimes * and then an array of ncnt samples representing the 1911590Srgrimes * discretized program counter values. 1921590Srgrimes * 1931590Srgrimes * Backward compatible old style header 1941590Srgrimes */ 1951590Srgrimesstruct ophdr { 19691738Sbde u_short *lpc; 19791738Sbde u_short *hpc; 1981590Srgrimes int ncnt; 1991590Srgrimes}; 2001590Srgrimes 2011590Srgrimesint debug; 2021590Srgrimes 2031590Srgrimes /* 2041590Srgrimes * Each discretized pc sample has 2051590Srgrimes * a count of the number of samples in its range 2061590Srgrimes */ 20791738Sbdedouble *samples; 2081590Srgrimes 2091590Srgrimesunsigned long s_lowpc; /* lowpc from the profile file */ 2101590Srgrimesunsigned long s_highpc; /* highpc from the profile file */ 21191738Sbdeunsigned long lowpc, highpc; /* range profiled, in historical units */ 2121590Srgrimesunsigned sampbytes; /* number of bytes of samples */ 2131590Srgrimesint nsamples; /* number of samples */ 2141590Srgrimesdouble actime; /* accumulated time thus far for putprofline */ 2151590Srgrimesdouble totime; /* total time for all routines */ 2161590Srgrimesdouble printtime; /* total of time being printed */ 2171590Srgrimesdouble scale; /* scale factor converting samples to pc 2181590Srgrimes values: each sample covers scale bytes */ 2191590Srgrimesunsigned char *textspace; /* text space of a.out in core */ 2201590Srgrimesint cyclethreshold; /* with -C, minimum cycle size to ignore */ 2211590Srgrimes 2221590Srgrimes /* 2231590Srgrimes * option flags, from a to z. 2241590Srgrimes */ 2251590Srgrimesbool aflag; /* suppress static functions */ 2261590Srgrimesbool bflag; /* blurbs, too */ 2271590Srgrimesbool Cflag; /* find cut-set to eliminate cycles */ 2281590Srgrimesbool dflag; /* debugging options */ 2291590Srgrimesbool eflag; /* specific functions excluded */ 2301590Srgrimesbool Eflag; /* functions excluded with time */ 2311590Srgrimesbool fflag; /* specific functions requested */ 2321590Srgrimesbool Fflag; /* functions requested with time */ 2331590Srgrimesbool kflag; /* arcs to be deleted */ 23485739Sgreenbool Kflag; /* use the running kernel for symbols */ 2351590Srgrimesbool sflag; /* sum multiple gmon.out files */ 23638928Sjdpbool uflag; /* suppress symbols hidden from C */ 2371590Srgrimesbool zflag; /* zero time/called functions, too */ 2381590Srgrimes 2391590Srgrimes /* 2401590Srgrimes * structure for various string lists 2411590Srgrimes */ 2421590Srgrimesstruct stringlist { 2431590Srgrimes struct stringlist *next; 2441590Srgrimes char *string; 2451590Srgrimes}; 2461590Srgrimesstruct stringlist *elist; 2471590Srgrimesstruct stringlist *Elist; 2481590Srgrimesstruct stringlist *flist; 2491590Srgrimesstruct stringlist *Flist; 2501590Srgrimesstruct stringlist *kfromlist; 2511590Srgrimesstruct stringlist *ktolist; 2521590Srgrimes 2531590Srgrimes /* 2541590Srgrimes * function declarations 2551590Srgrimes */ 256105243Scharniervoid addarc(nltype *, nltype *, long); 257105243Scharnierbool addcycle(arctype **, arctype **); 258105243Scharniervoid addlist(struct stringlist *, char *); 259105243Scharniervoid alignentries(void); 26038928Sjdpint aout_getnfile(const char *, char ***); 2611590Srgrimesint arccmp(); 2621590Srgrimesarctype *arclookup(); 263105243Scharniervoid asgnsamples(void); 264105243Scharniervoid compresslist(void); 265105243Scharnierbool cycleanalyze(void); 266105243Scharniervoid cyclelink(void); 267105243Scharniervoid cycletime(void); 268105243Scharnierbool descend(nltype *, arctype **, arctype **); 269105243Scharniervoid dfn(nltype *); 2701590Srgrimesbool dfn_busy(); 271105243Scharniervoid dfn_findcycle(nltype *); 272105243Scharniervoid dfn_init(void); 2731590Srgrimesbool dfn_numbered(); 274105243Scharniervoid dfn_post_visit(nltype *); 275105243Scharniervoid dfn_pre_visit(nltype *); 276105243Scharniervoid dfn_self_cycle(nltype *); 2771590Srgrimesnltype **doarcs(); 278105243Scharniervoid doflags(void); 279105243Scharniervoid dotime(void); 280105243Scharniervoid dumpsum(char *); 28138928Sjdpint elf_getnfile(const char *, char ***); 282105243Scharniervoid flatprofheader(void); 283105243Scharniervoid flatprofline(nltype *); 284105243Scharniervoid getpfile(char *); 2851590Srgrimes/* 2861590Srgrimes gprofheader(); 2871590Srgrimes gprofline(); 28885739Sgreen*/ 289136099Sstefanfint hertz(void); 290105243Scharniervoid inheritflags(nltype *); 29185739Sgreenint kernel_getnfile(const char *, char ***); 29285739Sgreen/* 2931590Srgrimes main(); 2941590Srgrimes*/ 2951590Srgrimesunsigned long max(); 2961590Srgrimesint membercmp(); 2971590Srgrimesunsigned long min(); 2981590Srgrimesnltype *nllookup(); 299105243Scharnierbool onlist(struct stringlist *, const char *); 3001590SrgrimesFILE *openpfile(); 3011590Srgrimeslong operandlength(); 3021590Srgrimesoperandenum operandmode(); 3031590Srgrimeschar *operandname(); 304105243Scharniervoid printblurb(char *); 305105243Scharniervoid printchildren(nltype *); 306105243Scharniervoid printcycle(nltype *); 307105243Scharniervoid printgprof(nltype **); 308105243Scharniervoid printindex(void); 309105243Scharniervoid printmembers(nltype *); 310105243Scharniervoid printname(nltype *); 311105243Scharniervoid printparents(nltype *); 312105243Scharniervoid printprof(void); 313105243Scharniervoid printsubcycle(cltype *); 314105243Scharniervoid readsamples(FILE *); 3151590Srgrimesunsigned long reladdr(); 316105243Scharniervoid sortchildren(nltype *); 317105243Scharniervoid sortmembers(nltype *); 318105243Scharniervoid sortparents(nltype *); 319105243Scharniervoid tally(struct rawarc *); 320105243Scharniervoid timepropagate(nltype *); 3211590Srgrimesint totalcmp(); 3221590Srgrimes 3231590Srgrimes#define LESSTHAN -1 3241590Srgrimes#define EQUALTO 0 3251590Srgrimes#define GREATERTHAN 1 3261590Srgrimes 3271590Srgrimes#define DFNDEBUG 1 3281590Srgrimes#define CYCLEDEBUG 2 3291590Srgrimes#define ARCDEBUG 4 3301590Srgrimes#define TALLYDEBUG 8 3311590Srgrimes#define TIMEDEBUG 16 3321590Srgrimes#define SAMPLEDEBUG 32 3331590Srgrimes#define AOUTDEBUG 64 3341590Srgrimes#define CALLDEBUG 128 3351590Srgrimes#define LOOKUPDEBUG 256 3361590Srgrimes#define PROPDEBUG 512 3371590Srgrimes#define BREAKCYCLE 1024 3381590Srgrimes#define SUBCYCLELIST 2048 3391590Srgrimes#define ANYDEBUG 4096 340