gprof.h revision 177926
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 * 3. All advertising materials mentioning features or use of this software 141590Srgrimes * must display the following acknowledgement: 151590Srgrimes * This product includes software developed by the University of 161590Srgrimes * California, Berkeley and its contributors. 171590Srgrimes * 4. Neither the name of the University nor the names of its contributors 181590Srgrimes * may be used to endorse or promote products derived from this software 191590Srgrimes * without specific prior written permission. 201590Srgrimes * 211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311590Srgrimes * SUCH DAMAGE. 321590Srgrimes * 331590Srgrimes * @(#)gprof.h 8.1 (Berkeley) 6/6/93 3485353Speter * $FreeBSD: head/usr.bin/gprof/gprof.h 177926 2008-04-04 21:33:41Z imp $ 351590Srgrimes */ 361590Srgrimes 371590Srgrimes#include <sys/types.h> 381590Srgrimes#include <sys/stat.h> 391590Srgrimes#include <sys/gmon.h> 401590Srgrimes 411590Srgrimes#include <stdio.h> 421590Srgrimes#include <stdlib.h> 431590Srgrimes 44129398Speter#if __amd64__ 45129398Speter# include "amd64.h" 46129398Speter#endif 47129243Sbde#if __arm__ 48129243Sbde# include "arm.h" 4991976Sjake#endif 50129243Sbde#if __i386__ 51129243Sbde# include "i386.h" 52129243Sbde#endif 53129243Sbde#if __ia64__ 54129243Sbde# include "ia64.h" 55129243Sbde#endif 56177926Simp#if __mips__ 57177926Simp# include "mips.h" 58177926Simp#endif 59107718Sgrehan#if __powerpc__ 60107718Sgrehan# include "powerpc.h" 61107718Sgrehan#endif 62129243Sbde#if __sparc64__ 63129243Sbde# include "sparc64.h" 641590Srgrimes#endif 651590Srgrimes 661590Srgrimes /* 671590Srgrimes * booleans 681590Srgrimes */ 691590Srgrimestypedef int bool; 701590Srgrimes#define FALSE 0 711590Srgrimes#define TRUE 1 721590Srgrimes 731590Srgrimes /* 7491735Sbde * Historical scale factor in profil(2)'s algorithm for converting 7591735Sbde * pc addresses to bucket numbers. This now just complicates the 7691735Sbde * scaling and makes bucket:pc densities of more than 1/2 useless. 7791735Sbde */ 7891735Sbde#define HISTORICAL_SCALE_2 2 7991735Sbde 8091735Sbde /* 811590Srgrimes * ticks per second 821590Srgrimes */ 831590Srgrimeslong hz; 841590Srgrimes 8591738Sbdesize_t histcounter_size; 8691738Sbdeint histcounter_type; 8791010Sbde 881590Srgrimeschar *a_outname; 891590Srgrimes#define A_OUTNAME "a.out" 901590Srgrimes 911590Srgrimeschar *gmonname; 921590Srgrimes#define GMONSUM "gmon.sum" 931590Srgrimes 941590Srgrimes /* 951590Srgrimes * a constructed arc, 961590Srgrimes * with pointers to the namelist entry of the parent and the child, 971590Srgrimes * a count of how many times this arc was traversed, 981590Srgrimes * and pointers to the next parent of this child and 991590Srgrimes * the next child of this parent. 1001590Srgrimes */ 1011590Srgrimesstruct arcstruct { 1021590Srgrimes struct nl *arc_parentp; /* pointer to parent's nl entry */ 1031590Srgrimes struct nl *arc_childp; /* pointer to child's nl entry */ 1041590Srgrimes long arc_count; /* num calls from parent to child */ 1051590Srgrimes double arc_time; /* time inherited along arc */ 1061590Srgrimes double arc_childtime; /* childtime inherited along arc */ 1071590Srgrimes struct arcstruct *arc_parentlist; /* parents-of-this-child list */ 1081590Srgrimes struct arcstruct *arc_childlist; /* children-of-this-parent list */ 1091590Srgrimes struct arcstruct *arc_next; /* list of arcs on cycle */ 1101590Srgrimes unsigned short arc_cyclecnt; /* num cycles involved in */ 1111590Srgrimes unsigned short arc_flags; /* see below */ 1121590Srgrimes}; 1131590Srgrimestypedef struct arcstruct arctype; 1141590Srgrimes 1151590Srgrimes /* 1161590Srgrimes * arc flags 1171590Srgrimes */ 1181590Srgrimes#define DEADARC 0x01 /* time should not propagate across the arc */ 1191590Srgrimes#define ONLIST 0x02 /* arc is on list of arcs in cycles */ 1201590Srgrimes 1211590Srgrimes /* 1221590Srgrimes * The symbol table; 1231590Srgrimes * for each external in the specified file we gather 124105243Scharnier * its address, the number of calls and compute its share of CPU time. 1251590Srgrimes */ 1261590Srgrimesstruct nl { 12738928Sjdp const char *name; /* the name */ 1281590Srgrimes unsigned long value; /* the pc entry point */ 1291590Srgrimes unsigned long svalue; /* entry point aligned to histograms */ 1301590Srgrimes double time; /* ticks in this routine */ 1311590Srgrimes double childtime; /* cumulative ticks in children */ 1321590Srgrimes long ncall; /* how many times called */ 1331590Srgrimes long npropcall; /* times called by live arcs */ 1341590Srgrimes long selfcalls; /* how many calls to self */ 1351590Srgrimes double propfraction; /* what % of time propagates */ 1361590Srgrimes double propself; /* how much self time propagates */ 1371590Srgrimes double propchild; /* how much child time propagates */ 1381590Srgrimes short printflag; /* should this be printed? */ 1391590Srgrimes short flags; /* see below */ 1401590Srgrimes int index; /* index in the graph list */ 1411590Srgrimes int toporder; /* graph call chain top-sort order */ 1421590Srgrimes int cycleno; /* internal number of cycle on */ 1431590Srgrimes int parentcnt; /* number of live parent arcs */ 1441590Srgrimes struct nl *cyclehead; /* pointer to head of cycle */ 1451590Srgrimes struct nl *cnext; /* pointer to next member of cycle */ 1461590Srgrimes arctype *parents; /* list of caller arcs */ 1471590Srgrimes arctype *children; /* list of callee arcs */ 1481590Srgrimes}; 1491590Srgrimestypedef struct nl nltype; 1501590Srgrimes 1511590Srgrimesnltype *nl; /* the whole namelist */ 1521590Srgrimesnltype *npe; /* the virtual end of the namelist */ 1531590Srgrimesint nname; /* the number of function names */ 1541590Srgrimes 1551590Srgrimes#define HASCYCLEXIT 0x08 /* node has arc exiting from cycle */ 1561590Srgrimes#define CYCLEHEAD 0x10 /* node marked as head of a cycle */ 1571590Srgrimes#define VISITED 0x20 /* node visited during a cycle */ 1581590Srgrimes 1591590Srgrimes /* 1601590Srgrimes * The cycle list. 1611590Srgrimes * for each subcycle within an identified cycle, we gather 1621590Srgrimes * its size and the list of included arcs. 1631590Srgrimes */ 1641590Srgrimesstruct cl { 1651590Srgrimes int size; /* length of cycle */ 1661590Srgrimes struct cl *next; /* next member of list */ 1671590Srgrimes arctype *list[1]; /* list of arcs in cycle */ 1681590Srgrimes /* actually longer */ 1691590Srgrimes}; 1701590Srgrimestypedef struct cl cltype; 1711590Srgrimes 1721590Srgrimesarctype *archead; /* the head of arcs in current cycle list */ 1731590Srgrimescltype *cyclehead; /* the head of the list */ 1741590Srgrimesint cyclecnt; /* the number of cycles found */ 1751590Srgrimes#define CYCLEMAX 100 /* maximum cycles before cutting one of them */ 1761590Srgrimes 1771590Srgrimes /* 1781590Srgrimes * flag which marks a nl entry as topologically ``busy'' 1791590Srgrimes * flag which marks a nl entry as topologically ``not_numbered'' 1801590Srgrimes */ 1811590Srgrimes#define DFN_BUSY -1 1821590Srgrimes#define DFN_NAN 0 1831590Srgrimes 1848874Srgrimes /* 1851590Srgrimes * namelist entries for cycle headers. 1861590Srgrimes * the number of discovered cycles. 1871590Srgrimes */ 1881590Srgrimesnltype *cyclenl; /* cycle header namelist */ 1891590Srgrimesint ncycle; /* number of cycles discovered */ 1901590Srgrimes 1911590Srgrimes /* 1921590Srgrimes * The header on the gmon.out file. 1931590Srgrimes * gmon.out consists of a struct phdr (defined in gmon.h) 1941590Srgrimes * and then an array of ncnt samples representing the 1951590Srgrimes * discretized program counter values. 1961590Srgrimes * 1971590Srgrimes * Backward compatible old style header 1981590Srgrimes */ 1991590Srgrimesstruct ophdr { 20091738Sbde u_short *lpc; 20191738Sbde u_short *hpc; 2021590Srgrimes int ncnt; 2031590Srgrimes}; 2041590Srgrimes 2051590Srgrimesint debug; 2061590Srgrimes 2071590Srgrimes /* 2081590Srgrimes * Each discretized pc sample has 2091590Srgrimes * a count of the number of samples in its range 2101590Srgrimes */ 21191738Sbdedouble *samples; 2121590Srgrimes 2131590Srgrimesunsigned long s_lowpc; /* lowpc from the profile file */ 2141590Srgrimesunsigned long s_highpc; /* highpc from the profile file */ 21591738Sbdeunsigned long lowpc, highpc; /* range profiled, in historical units */ 2161590Srgrimesunsigned sampbytes; /* number of bytes of samples */ 2171590Srgrimesint nsamples; /* number of samples */ 2181590Srgrimesdouble actime; /* accumulated time thus far for putprofline */ 2191590Srgrimesdouble totime; /* total time for all routines */ 2201590Srgrimesdouble printtime; /* total of time being printed */ 2211590Srgrimesdouble scale; /* scale factor converting samples to pc 2221590Srgrimes values: each sample covers scale bytes */ 2231590Srgrimesunsigned char *textspace; /* text space of a.out in core */ 2241590Srgrimesint cyclethreshold; /* with -C, minimum cycle size to ignore */ 2251590Srgrimes 2261590Srgrimes /* 2271590Srgrimes * option flags, from a to z. 2281590Srgrimes */ 2291590Srgrimesbool aflag; /* suppress static functions */ 2301590Srgrimesbool bflag; /* blurbs, too */ 2311590Srgrimesbool Cflag; /* find cut-set to eliminate cycles */ 2321590Srgrimesbool dflag; /* debugging options */ 2331590Srgrimesbool eflag; /* specific functions excluded */ 2341590Srgrimesbool Eflag; /* functions excluded with time */ 2351590Srgrimesbool fflag; /* specific functions requested */ 2361590Srgrimesbool Fflag; /* functions requested with time */ 2371590Srgrimesbool kflag; /* arcs to be deleted */ 23885739Sgreenbool Kflag; /* use the running kernel for symbols */ 2391590Srgrimesbool sflag; /* sum multiple gmon.out files */ 24038928Sjdpbool uflag; /* suppress symbols hidden from C */ 2411590Srgrimesbool zflag; /* zero time/called functions, too */ 2421590Srgrimes 2431590Srgrimes /* 2441590Srgrimes * structure for various string lists 2451590Srgrimes */ 2461590Srgrimesstruct stringlist { 2471590Srgrimes struct stringlist *next; 2481590Srgrimes char *string; 2491590Srgrimes}; 2501590Srgrimesstruct stringlist *elist; 2511590Srgrimesstruct stringlist *Elist; 2521590Srgrimesstruct stringlist *flist; 2531590Srgrimesstruct stringlist *Flist; 2541590Srgrimesstruct stringlist *kfromlist; 2551590Srgrimesstruct stringlist *ktolist; 2561590Srgrimes 2571590Srgrimes /* 2581590Srgrimes * function declarations 2591590Srgrimes */ 260105243Scharniervoid addarc(nltype *, nltype *, long); 261105243Scharnierbool addcycle(arctype **, arctype **); 262105243Scharniervoid addlist(struct stringlist *, char *); 263105243Scharniervoid alignentries(void); 26438928Sjdpint aout_getnfile(const char *, char ***); 2651590Srgrimesint arccmp(); 2661590Srgrimesarctype *arclookup(); 267105243Scharniervoid asgnsamples(void); 268105243Scharniervoid compresslist(void); 269105243Scharnierbool cycleanalyze(void); 270105243Scharniervoid cyclelink(void); 271105243Scharniervoid cycletime(void); 272105243Scharnierbool descend(nltype *, arctype **, arctype **); 273105243Scharniervoid dfn(nltype *); 2741590Srgrimesbool dfn_busy(); 275105243Scharniervoid dfn_findcycle(nltype *); 276105243Scharniervoid dfn_init(void); 2771590Srgrimesbool dfn_numbered(); 278105243Scharniervoid dfn_post_visit(nltype *); 279105243Scharniervoid dfn_pre_visit(nltype *); 280105243Scharniervoid dfn_self_cycle(nltype *); 2811590Srgrimesnltype **doarcs(); 282105243Scharniervoid doflags(void); 283105243Scharniervoid dotime(void); 284105243Scharniervoid dumpsum(char *); 28538928Sjdpint elf_getnfile(const char *, char ***); 286105243Scharniervoid flatprofheader(void); 287105243Scharniervoid flatprofline(nltype *); 288105243Scharniervoid getpfile(char *); 2891590Srgrimes/* 2901590Srgrimes gprofheader(); 2911590Srgrimes gprofline(); 29285739Sgreen*/ 293136099Sstefanfint hertz(void); 294105243Scharniervoid inheritflags(nltype *); 29585739Sgreenint kernel_getnfile(const char *, char ***); 29685739Sgreen/* 2971590Srgrimes main(); 2981590Srgrimes*/ 2991590Srgrimesunsigned long max(); 3001590Srgrimesint membercmp(); 3011590Srgrimesunsigned long min(); 3021590Srgrimesnltype *nllookup(); 303105243Scharnierbool onlist(struct stringlist *, const char *); 3041590SrgrimesFILE *openpfile(); 3051590Srgrimeslong operandlength(); 3061590Srgrimesoperandenum operandmode(); 3071590Srgrimeschar *operandname(); 308105243Scharniervoid printblurb(char *); 309105243Scharniervoid printchildren(nltype *); 310105243Scharniervoid printcycle(nltype *); 311105243Scharniervoid printgprof(nltype **); 312105243Scharniervoid printindex(void); 313105243Scharniervoid printmembers(nltype *); 314105243Scharniervoid printname(nltype *); 315105243Scharniervoid printparents(nltype *); 316105243Scharniervoid printprof(void); 317105243Scharniervoid printsubcycle(cltype *); 318105243Scharniervoid readsamples(FILE *); 3191590Srgrimesunsigned long reladdr(); 320105243Scharniervoid sortchildren(nltype *); 321105243Scharniervoid sortmembers(nltype *); 322105243Scharniervoid sortparents(nltype *); 323105243Scharniervoid tally(struct rawarc *); 324105243Scharniervoid timepropagate(nltype *); 3251590Srgrimesint totalcmp(); 3261590Srgrimes 3271590Srgrimes#define LESSTHAN -1 3281590Srgrimes#define EQUALTO 0 3291590Srgrimes#define GREATERTHAN 1 3301590Srgrimes 3311590Srgrimes#define DFNDEBUG 1 3321590Srgrimes#define CYCLEDEBUG 2 3331590Srgrimes#define ARCDEBUG 4 3341590Srgrimes#define TALLYDEBUG 8 3351590Srgrimes#define TIMEDEBUG 16 3361590Srgrimes#define SAMPLEDEBUG 32 3371590Srgrimes#define AOUTDEBUG 64 3381590Srgrimes#define CALLDEBUG 128 3391590Srgrimes#define LOOKUPDEBUG 256 3401590Srgrimes#define PROPDEBUG 512 3411590Srgrimes#define BREAKCYCLE 1024 3421590Srgrimes#define SUBCYCLELIST 2048 3431590Srgrimes#define ANYDEBUG 4096 344