1331722Seadler/* 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: stable/11/usr.bin/gprof/gprof.h 359763 2020-04-10 00:27:19Z kevans $ 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 49177926Simp#if __mips__ 50177926Simp# include "mips.h" 51177926Simp#endif 52107718Sgrehan#if __powerpc__ 53107718Sgrehan# include "powerpc.h" 54107718Sgrehan#endif 55129243Sbde#if __sparc64__ 56129243Sbde# include "sparc64.h" 571590Srgrimes#endif 581590Srgrimes 591590Srgrimes /* 601590Srgrimes * booleans 611590Srgrimes */ 621590Srgrimestypedef int bool; 631590Srgrimes#define FALSE 0 641590Srgrimes#define TRUE 1 651590Srgrimes 661590Srgrimes /* 6791735Sbde * Historical scale factor in profil(2)'s algorithm for converting 6891735Sbde * pc addresses to bucket numbers. This now just complicates the 6991735Sbde * scaling and makes bucket:pc densities of more than 1/2 useless. 7091735Sbde */ 7191735Sbde#define HISTORICAL_SCALE_2 2 7291735Sbde 73359763Skevans#ifndef EXTERN 74359763Skevans#define EXTERN extern 75359763Skevans#endif 76359763Skevans 7791735Sbde /* 781590Srgrimes * ticks per second 791590Srgrimes */ 80359763SkevansEXTERN long hz; 811590Srgrimes 82359763SkevansEXTERN size_t histcounter_size; 83359763SkevansEXTERN int histcounter_type; 8491010Sbde 85359763SkevansEXTERN char *a_outname; 861590Srgrimes#define A_OUTNAME "a.out" 871590Srgrimes 88359763SkevansEXTERN char *gmonname; 891590Srgrimes#define GMONSUM "gmon.sum" 901590Srgrimes 911590Srgrimes /* 921590Srgrimes * a constructed arc, 931590Srgrimes * with pointers to the namelist entry of the parent and the child, 941590Srgrimes * a count of how many times this arc was traversed, 951590Srgrimes * and pointers to the next parent of this child and 961590Srgrimes * the next child of this parent. 971590Srgrimes */ 981590Srgrimesstruct arcstruct { 991590Srgrimes struct nl *arc_parentp; /* pointer to parent's nl entry */ 1001590Srgrimes struct nl *arc_childp; /* pointer to child's nl entry */ 1011590Srgrimes long arc_count; /* num calls from parent to child */ 1021590Srgrimes double arc_time; /* time inherited along arc */ 1031590Srgrimes double arc_childtime; /* childtime inherited along arc */ 1041590Srgrimes struct arcstruct *arc_parentlist; /* parents-of-this-child list */ 1051590Srgrimes struct arcstruct *arc_childlist; /* children-of-this-parent list */ 1061590Srgrimes struct arcstruct *arc_next; /* list of arcs on cycle */ 1071590Srgrimes unsigned short arc_cyclecnt; /* num cycles involved in */ 1081590Srgrimes unsigned short arc_flags; /* see below */ 1091590Srgrimes}; 1101590Srgrimestypedef struct arcstruct arctype; 1111590Srgrimes 1121590Srgrimes /* 1131590Srgrimes * arc flags 1141590Srgrimes */ 1151590Srgrimes#define DEADARC 0x01 /* time should not propagate across the arc */ 1161590Srgrimes#define ONLIST 0x02 /* arc is on list of arcs in cycles */ 1171590Srgrimes 1181590Srgrimes /* 1191590Srgrimes * The symbol table; 1201590Srgrimes * for each external in the specified file we gather 121105243Scharnier * its address, the number of calls and compute its share of CPU time. 1221590Srgrimes */ 1231590Srgrimesstruct nl { 12438928Sjdp const char *name; /* the name */ 1251590Srgrimes unsigned long value; /* the pc entry point */ 1261590Srgrimes unsigned long svalue; /* entry point aligned to histograms */ 1271590Srgrimes double time; /* ticks in this routine */ 1281590Srgrimes double childtime; /* cumulative ticks in children */ 1291590Srgrimes long ncall; /* how many times called */ 1301590Srgrimes long npropcall; /* times called by live arcs */ 1311590Srgrimes long selfcalls; /* how many calls to self */ 1321590Srgrimes double propfraction; /* what % of time propagates */ 1331590Srgrimes double propself; /* how much self time propagates */ 1341590Srgrimes double propchild; /* how much child time propagates */ 1351590Srgrimes short printflag; /* should this be printed? */ 1361590Srgrimes short flags; /* see below */ 1371590Srgrimes int index; /* index in the graph list */ 1381590Srgrimes int toporder; /* graph call chain top-sort order */ 1391590Srgrimes int cycleno; /* internal number of cycle on */ 1401590Srgrimes int parentcnt; /* number of live parent arcs */ 1411590Srgrimes struct nl *cyclehead; /* pointer to head of cycle */ 1421590Srgrimes struct nl *cnext; /* pointer to next member of cycle */ 1431590Srgrimes arctype *parents; /* list of caller arcs */ 1441590Srgrimes arctype *children; /* list of callee arcs */ 1451590Srgrimes}; 1461590Srgrimestypedef struct nl nltype; 1471590Srgrimes 148359763SkevansEXTERN nltype *nl; /* the whole namelist */ 149359763SkevansEXTERN nltype *npe; /* the virtual end of the namelist */ 150359763SkevansEXTERN int nname; /* the number of function names */ 1511590Srgrimes 1521590Srgrimes#define HASCYCLEXIT 0x08 /* node has arc exiting from cycle */ 1531590Srgrimes#define CYCLEHEAD 0x10 /* node marked as head of a cycle */ 1541590Srgrimes#define VISITED 0x20 /* node visited during a cycle */ 1551590Srgrimes 1561590Srgrimes /* 1571590Srgrimes * The cycle list. 1581590Srgrimes * for each subcycle within an identified cycle, we gather 1591590Srgrimes * its size and the list of included arcs. 1601590Srgrimes */ 1611590Srgrimesstruct cl { 1621590Srgrimes int size; /* length of cycle */ 1631590Srgrimes struct cl *next; /* next member of list */ 1641590Srgrimes arctype *list[1]; /* list of arcs in cycle */ 1651590Srgrimes /* actually longer */ 1661590Srgrimes}; 1671590Srgrimestypedef struct cl cltype; 1681590Srgrimes 169359763SkevansEXTERN arctype *archead; /* the head of arcs in current cycle list */ 170359763SkevansEXTERN cltype *cyclehead; /* the head of the list */ 171359763SkevansEXTERN int cyclecnt; /* the number of cycles found */ 1721590Srgrimes#define CYCLEMAX 100 /* maximum cycles before cutting one of them */ 1731590Srgrimes 1741590Srgrimes /* 1751590Srgrimes * flag which marks a nl entry as topologically ``busy'' 1761590Srgrimes * flag which marks a nl entry as topologically ``not_numbered'' 1771590Srgrimes */ 1781590Srgrimes#define DFN_BUSY -1 1791590Srgrimes#define DFN_NAN 0 1801590Srgrimes 1818874Srgrimes /* 1821590Srgrimes * namelist entries for cycle headers. 1831590Srgrimes * the number of discovered cycles. 1841590Srgrimes */ 185359763SkevansEXTERN nltype *cyclenl; /* cycle header namelist */ 186359763SkevansEXTERN int ncycle; /* number of cycles discovered */ 1871590Srgrimes 1881590Srgrimes /* 1891590Srgrimes * The header on the gmon.out file. 1901590Srgrimes * gmon.out consists of a struct phdr (defined in gmon.h) 1911590Srgrimes * and then an array of ncnt samples representing the 1921590Srgrimes * discretized program counter values. 1931590Srgrimes * 1941590Srgrimes * Backward compatible old style header 1951590Srgrimes */ 1961590Srgrimesstruct ophdr { 19791738Sbde u_short *lpc; 19891738Sbde u_short *hpc; 1991590Srgrimes int ncnt; 2001590Srgrimes}; 2011590Srgrimes 202359763SkevansEXTERN int debug; 2031590Srgrimes 2041590Srgrimes /* 2051590Srgrimes * Each discretized pc sample has 2061590Srgrimes * a count of the number of samples in its range 2071590Srgrimes */ 208359763SkevansEXTERN double *samples; 2091590Srgrimes 210359763SkevansEXTERN unsigned long s_lowpc; /* lowpc from the profile file */ 211359763SkevansEXTERN unsigned long s_highpc; /* highpc from the profile file */ 212359763Skevans/* range profiled, in historical units */ 213359763SkevansEXTERN unsigned long lowpc, highpc; 214359763SkevansEXTERN unsigned sampbytes; /* number of bytes of samples */ 215359763SkevansEXTERN int nsamples; /* number of samples */ 216359763Skevans/* accumulated time thus far for putprofline */ 217359763SkevansEXTERN double actime; 218359763SkevansEXTERN double totime; /* total time for all routines */ 219359763SkevansEXTERN double printtime; /* total of time being printed */ 220359763SkevansEXTERN double scale; /* scale factor converting samples to pc 2211590Srgrimes values: each sample covers scale bytes */ 222359763SkevansEXTERN unsigned char *textspace; /* text space of a.out in core */ 223359763Skevans/* with -C, minimum cycle size to ignore */ 224359763SkevansEXTERN int cyclethreshold; 2251590Srgrimes 2261590Srgrimes /* 2271590Srgrimes * option flags, from a to z. 2281590Srgrimes */ 229359763SkevansEXTERN bool aflag; /* suppress static functions */ 230359763SkevansEXTERN bool bflag; /* blurbs, too */ 231359763SkevansEXTERN bool Cflag; /* find cut-set to eliminate cycles */ 232359763SkevansEXTERN bool dflag; /* debugging options */ 233359763SkevansEXTERN bool eflag; /* specific functions excluded */ 234359763SkevansEXTERN bool Eflag; /* functions excluded with time */ 235359763SkevansEXTERN bool fflag; /* specific functions requested */ 236359763SkevansEXTERN bool Fflag; /* functions requested with time */ 237359763SkevansEXTERN bool kflag; /* arcs to be deleted */ 238359763SkevansEXTERN bool Kflag; /* use the running kernel for symbols */ 239359763SkevansEXTERN bool sflag; /* sum multiple gmon.out files */ 240359763SkevansEXTERN bool uflag; /* suppress symbols hidden from C */ 241359763SkevansEXTERN bool 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}; 250359763Skevansextern struct stringlist *elist; 251359763Skevansextern struct stringlist *Elist; 252359763Skevansextern struct stringlist *flist; 253359763Skevansextern struct stringlist *Flist; 254359763Skevansextern struct stringlist *kfromlist; 255359763Skevansextern struct 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 ***); 265246783Scharnierint arccmp(arctype *, arctype *); 266246783Scharnierarctype *arclookup(nltype *, nltype *); 267105243Scharniervoid asgnsamples(void); 268105243Scharniervoid compresslist(void); 269105243Scharnierbool cycleanalyze(void); 270105243Scharniervoid cyclelink(void); 271105243Scharniervoid cycletime(void); 272105243Scharnierbool descend(nltype *, arctype **, arctype **); 273105243Scharniervoid dfn(nltype *); 274246783Scharnierbool dfn_busy(nltype *); 275105243Scharniervoid dfn_findcycle(nltype *); 276105243Scharniervoid dfn_init(void); 277246783Scharnierbool dfn_numbered(nltype *); 278105243Scharniervoid dfn_post_visit(nltype *); 279105243Scharniervoid dfn_pre_visit(nltype *); 280105243Scharniervoid dfn_self_cycle(nltype *); 281246783Scharniernltype **doarcs(void); 282105243Scharniervoid doflags(void); 283105243Scharniervoid dotime(void); 284246783Scharniervoid dumpsum(const char *); 28538928Sjdpint elf_getnfile(const char *, char ***); 286105243Scharniervoid flatprofheader(void); 287105243Scharniervoid flatprofline(nltype *); 288105243Scharniervoid getpfile(char *); 289246783Scharniervoid gprofheader(void); 290246783Scharniervoid gprofline(register nltype *); 291136099Sstefanfint hertz(void); 292105243Scharniervoid inheritflags(nltype *); 29385739Sgreenint kernel_getnfile(const char *, char ***); 29485739Sgreen/* 2951590Srgrimes main(); 2961590Srgrimes*/ 297246783Scharnierunsigned long max(unsigned long, unsigned long); 298246783Scharnierint membercmp(nltype *, nltype *); 299246783Scharnierunsigned long min(unsigned long, unsigned long); 300246783Scharniernltype *nllookup(unsigned long); 301105243Scharnierbool onlist(struct stringlist *, const char *); 302246783ScharnierFILE *openpfile(char *); 303246783Scharniervoid printblurb(const char *); 304105243Scharniervoid printchildren(nltype *); 305105243Scharniervoid printcycle(nltype *); 306105243Scharniervoid printgprof(nltype **); 307105243Scharniervoid printindex(void); 308105243Scharniervoid printmembers(nltype *); 309105243Scharniervoid printname(nltype *); 310105243Scharniervoid printparents(nltype *); 311105243Scharniervoid printprof(void); 312105243Scharniervoid printsubcycle(cltype *); 313105243Scharniervoid readsamples(FILE *); 314105243Scharniervoid sortchildren(nltype *); 315105243Scharniervoid sortmembers(nltype *); 316105243Scharniervoid sortparents(nltype *); 317105243Scharniervoid tally(struct rawarc *); 318105243Scharniervoid timepropagate(nltype *); 319246783Scharnierint totalcmp(const void *, const void *); 3201590Srgrimes 3211590Srgrimes#define LESSTHAN -1 3221590Srgrimes#define EQUALTO 0 3231590Srgrimes#define GREATERTHAN 1 3241590Srgrimes 3251590Srgrimes#define DFNDEBUG 1 3261590Srgrimes#define CYCLEDEBUG 2 3271590Srgrimes#define ARCDEBUG 4 3281590Srgrimes#define TALLYDEBUG 8 3291590Srgrimes#define TIMEDEBUG 16 3301590Srgrimes#define SAMPLEDEBUG 32 3311590Srgrimes#define AOUTDEBUG 64 3321590Srgrimes#define CALLDEBUG 128 3331590Srgrimes#define LOOKUPDEBUG 256 3341590Srgrimes#define PROPDEBUG 512 3351590Srgrimes#define BREAKCYCLE 1024 3361590Srgrimes#define SUBCYCLELIST 2048 3371590Srgrimes#define ANYDEBUG 4096 338