121965Sdist /* 2*62017Sbostic * Copyright (c) 1983, 1993 3*62017Sbostic * The Regents of the University of California. All rights reserved. 421965Sdist * 542683Sbostic * %sccs.include.redist.c% 634199Sbostic * 7*62017Sbostic * @(#)gprof.h 8.1 (Berkeley) 06/06/93 821965Sdist */ 94511Speter 104511Speter #include <sys/types.h> 114511Speter #include <sys/stat.h> 1254804Storek #include <sys/gmon.h> 1354804Storek 144511Speter #include <a.out.h> 1540604Sbostic #include <stdio.h> 1654804Storek #include <stdlib.h> 174511Speter 1811799Speter #if vax 1911799Speter # include "vax.h" 2011799Speter #endif 2154804Storek #if sparc 2254804Storek # include "sparc.h" 2311799Speter #endif 2425709Ssam #if tahoe 2525709Ssam # include "tahoe.h" 2625709Ssam #endif 2748699Sdonn #if hp300 2848699Sdonn # include "hp300.h" 2948699Sdonn #endif 3056172Sbostic #if luna68k 3156172Sbostic # include "luna68k.h" 3256172Sbostic #endif 3348699Sdonn #if i386 3448699Sdonn # include "i386.h" 3548699Sdonn #endif 3658578Sralph #if mips 3758578Sralph # include "mips.h" 3858578Sralph #endif 3911799Speter 4011799Speter 414511Speter /* 427129Speter * who am i, for error messages. 437129Speter */ 447129Speter char *whoami; 457129Speter 467129Speter /* 477174Speter * booleans 487174Speter */ 497174Speter typedef int bool; 507174Speter #define FALSE 0 517174Speter #define TRUE 1 527174Speter 537174Speter /* 544511Speter * ticks per second 554511Speter */ 5610250Speter long hz; 574511Speter 5833225Sbostic typedef u_short UNIT; /* unit of profiling */ 594511Speter char *a_outname; 604511Speter #define A_OUTNAME "a.out" 614511Speter 624560Speter char *gmonname; 634560Speter #define GMONNAME "gmon.out" 644867Smckusic #define GMONSUM "gmon.sum" 6533225Sbostic 664873Smckusic /* 674511Speter * a constructed arc, 684511Speter * with pointers to the namelist entry of the parent and the child, 694511Speter * a count of how many times this arc was traversed, 704511Speter * and pointers to the next parent of this child and 714511Speter * the next child of this parent. 724511Speter */ 734511Speter struct arcstruct { 744511Speter struct nl *arc_parentp; /* pointer to parent's nl entry */ 754511Speter struct nl *arc_childp; /* pointer to child's nl entry */ 7652648Smckusick long arc_count; /* num calls from parent to child */ 774511Speter double arc_time; /* time inherited along arc */ 784511Speter double arc_childtime; /* childtime inherited along arc */ 794511Speter struct arcstruct *arc_parentlist; /* parents-of-this-child list */ 804511Speter struct arcstruct *arc_childlist; /* children-of-this-parent list */ 8152648Smckusick struct arcstruct *arc_next; /* list of arcs on cycle */ 8252648Smckusick unsigned short arc_cyclecnt; /* num cycles involved in */ 8352648Smckusick unsigned short arc_flags; /* see below */ 844511Speter }; 854511Speter typedef struct arcstruct arctype; 864511Speter 877129Speter /* 8852648Smckusick * arc flags 8952648Smckusick */ 9052648Smckusick #define DEADARC 0x01 /* time should not propagate across the arc */ 9152648Smckusick #define ONLIST 0x02 /* arc is on list of arcs in cycles */ 9252648Smckusick 9352648Smckusick /* 947129Speter * The symbol table; 957129Speter * for each external in the specified file we gather 967129Speter * its address, the number of calls and compute its share of cpu time. 977129Speter */ 984511Speter struct nl { 997129Speter char *name; /* the name */ 1007129Speter unsigned long value; /* the pc entry point */ 10111799Speter unsigned long svalue; /* entry point aligned to histograms */ 1027129Speter double time; /* ticks in this routine */ 1037129Speter double childtime; /* cumulative ticks in children */ 1047129Speter long ncall; /* how many times called */ 10552648Smckusick long npropcall; /* times called by live arcs */ 1067129Speter long selfcalls; /* how many calls to self */ 1077224Speter double propfraction; /* what % of time propagates */ 1087224Speter double propself; /* how much self time propagates */ 1097224Speter double propchild; /* how much child time propagates */ 11052648Smckusick short printflag; /* should this be printed? */ 11152648Smckusick short flags; /* see below */ 1127129Speter int index; /* index in the graph list */ 1137129Speter int toporder; /* graph call chain top-sort order */ 1147129Speter int cycleno; /* internal number of cycle on */ 11552648Smckusick int parentcnt; /* number of live parent arcs */ 1167129Speter struct nl *cyclehead; /* pointer to head of cycle */ 1177129Speter struct nl *cnext; /* pointer to next member of cycle */ 1187129Speter arctype *parents; /* list of caller arcs */ 1197129Speter arctype *children; /* list of callee arcs */ 1204511Speter }; 1214511Speter typedef struct nl nltype; 1224511Speter 1234511Speter nltype *nl; /* the whole namelist */ 1244511Speter nltype *npe; /* the virtual end of the namelist */ 1257129Speter int nname; /* the number of function names */ 1264511Speter 12752648Smckusick #define HASCYCLEXIT 0x08 /* node has arc exiting from cycle */ 12852648Smckusick #define CYCLEHEAD 0x10 /* node marked as head of a cycle */ 12952648Smckusick #define VISITED 0x20 /* node visited during a cycle */ 13052648Smckusick 1314511Speter /* 13252648Smckusick * The cycle list. 13352648Smckusick * for each subcycle within an identified cycle, we gather 13452648Smckusick * its size and the list of included arcs. 13552648Smckusick */ 13652648Smckusick struct cl { 13752648Smckusick int size; /* length of cycle */ 13852648Smckusick struct cl *next; /* next member of list */ 13952648Smckusick arctype *list[1]; /* list of arcs in cycle */ 14052648Smckusick /* actually longer */ 14152648Smckusick }; 14252648Smckusick typedef struct cl cltype; 14352648Smckusick 14452648Smckusick arctype *archead; /* the head of arcs in current cycle list */ 14552648Smckusick cltype *cyclehead; /* the head of the list */ 14652648Smckusick int cyclecnt; /* the number of cycles found */ 14752648Smckusick #define CYCLEMAX 100 /* maximum cycles before cutting one of them */ 14852648Smckusick 14952648Smckusick /* 1504511Speter * flag which marks a nl entry as topologically ``busy'' 15110284Speter * flag which marks a nl entry as topologically ``not_numbered'' 1524511Speter */ 1534511Speter #define DFN_BUSY -1 15410284Speter #define DFN_NAN 0 1554511Speter 1564511Speter /* 1577129Speter * namelist entries for cycle headers. 1587129Speter * the number of discovered cycles. 1594511Speter */ 1607129Speter nltype *cyclenl; /* cycle header namelist */ 1617129Speter int ncycle; /* number of cycles discovered */ 1624511Speter 1637129Speter /* 1647129Speter * The header on the gmon.out file. 16552811Smckusick * gmon.out consists of a struct phdr (defined in gmon.h) 16652811Smckusick * and then an array of ncnt samples representing the 16752811Smckusick * discretized program counter values. 16852811Smckusick * 16952811Smckusick * Backward compatible old style header 1707129Speter */ 17152811Smckusick struct ophdr { 17252811Smckusick UNIT *lpc; 17352811Smckusick UNIT *hpc; 17452811Smckusick int ncnt; 1754511Speter }; 1764511Speter 1774511Speter int debug; 1784511Speter 1797129Speter /* 1807129Speter * Each discretized pc sample has 1817129Speter * a count of the number of samples in its range 1827129Speter */ 18333225Sbostic UNIT *samples; 1844511Speter 1854751Speter unsigned long s_lowpc; /* lowpc from the profile file */ 1864751Speter unsigned long s_highpc; /* highpc from the profile file */ 1874751Speter unsigned lowpc, highpc; /* range profiled, in UNIT's */ 1884511Speter unsigned sampbytes; /* number of bytes of samples */ 1894511Speter int nsamples; /* number of samples */ 1904511Speter double actime; /* accumulated time thus far for putprofline */ 1914511Speter double totime; /* total time for all routines */ 1927174Speter double printtime; /* total of time being printed */ 1934511Speter double scale; /* scale factor converting samples to pc 1944511Speter values: each sample covers scale bytes */ 1954511Speter char *strtab; /* string table in core */ 19654804Storek long ssiz; /* size of the string table */ 1974511Speter struct exec xbuf; /* exec header of a.out */ 19852648Smckusick unsigned char *textspace; /* text space of a.out in core */ 19952648Smckusick int cyclethreshold; /* with -C, minimum cycle size to ignore */ 2004511Speter 2014855Speter /* 2024855Speter * option flags, from a to z. 2034855Speter */ 2047174Speter bool aflag; /* suppress static functions */ 2057174Speter bool bflag; /* blurbs, too */ 2067174Speter bool cflag; /* discovered call graph, too */ 20752648Smckusick bool Cflag; /* find cut-set to eliminate cycles */ 2087174Speter bool dflag; /* debugging options */ 2097174Speter bool eflag; /* specific functions excluded */ 2107224Speter bool Eflag; /* functions excluded with time */ 2117174Speter bool fflag; /* specific functions requested */ 2127224Speter bool Fflag; /* functions requested with time */ 21330963Smckusick bool kflag; /* arcs to be deleted */ 2147174Speter bool sflag; /* sum multiple gmon.out files */ 2157174Speter bool zflag; /* zero time/called functions, too */ 2164511Speter 2174511Speter /* 2187224Speter * structure for various string lists 2197224Speter */ 2207224Speter struct stringlist { 2217224Speter struct stringlist *next; 2227224Speter char *string; 2237224Speter }; 2247224Speter struct stringlist *elist; 2257224Speter struct stringlist *Elist; 2267224Speter struct stringlist *flist; 2277224Speter struct stringlist *Flist; 22830963Smckusick struct stringlist *kfromlist; 22930963Smckusick struct stringlist *ktolist; 2307224Speter 2317224Speter /* 2324840Speter * function declarations 2334840Speter */ 23433225Sbostic /* 2354840Speter addarc(); 23633225Sbostic */ 2374840Speter int arccmp(); 2384511Speter arctype *arclookup(); 23933225Sbostic /* 2404840Speter asgnsamples(); 2414855Speter printblurb(); 2424840Speter cyclelink(); 2434840Speter dfn(); 24433225Sbostic */ 2454511Speter bool dfn_busy(); 24633225Sbostic /* 2474840Speter dfn_findcycle(); 24833225Sbostic */ 2494840Speter bool dfn_numbered(); 25033225Sbostic /* 2514840Speter dfn_post_visit(); 2524840Speter dfn_pre_visit(); 2534840Speter dfn_self_cycle(); 25433225Sbostic */ 25516851Smckusick nltype **doarcs(); 25633225Sbostic /* 2574840Speter done(); 2584840Speter findcalls(); 2594840Speter flatprofheader(); 2604840Speter flatprofline(); 26133225Sbostic */ 2624844Speter bool funcsymbol(); 26333225Sbostic /* 2644840Speter getnfile(); 2654840Speter getpfile(); 2664840Speter getstrtab(); 2674840Speter getsymtab(); 2684840Speter gettextspace(); 2694840Speter gprofheader(); 2704840Speter gprofline(); 2714840Speter main(); 27233225Sbostic */ 2734840Speter unsigned long max(); 2744840Speter int membercmp(); 2754840Speter unsigned long min(); 2764840Speter nltype *nllookup(); 2774840Speter FILE *openpfile(); 2784840Speter long operandlength(); 2794840Speter operandenum operandmode(); 2804840Speter char *operandname(); 28133225Sbostic /* 2824840Speter printchildren(); 2834840Speter printcycle(); 2844840Speter printgprof(); 2854840Speter printmembers(); 2864840Speter printname(); 2874840Speter printparents(); 2884840Speter printprof(); 2894840Speter readsamples(); 29033225Sbostic */ 2914840Speter unsigned long reladdr(); 29233225Sbostic /* 2934840Speter sortchildren(); 2944840Speter sortmembers(); 2954840Speter sortparents(); 2964840Speter tally(); 2974840Speter timecmp(); 2984840Speter topcmp(); 29933225Sbostic */ 3004840Speter int totalcmp(); 30133225Sbostic /* 3024840Speter valcmp(); 30333225Sbostic */ 3044511Speter 3054511Speter #define LESSTHAN -1 3064511Speter #define EQUALTO 0 3074511Speter #define GREATERTHAN 1 3084511Speter 3094511Speter #define DFNDEBUG 1 3104511Speter #define CYCLEDEBUG 2 3114511Speter #define ARCDEBUG 4 3124511Speter #define TALLYDEBUG 8 3134511Speter #define TIMEDEBUG 16 3144511Speter #define SAMPLEDEBUG 32 3154511Speter #define AOUTDEBUG 64 31625709Ssam #define CALLDEBUG 128 3174722Speter #define LOOKUPDEBUG 256 3187224Speter #define PROPDEBUG 512 31952648Smckusick #define BREAKCYCLE 1024 32052648Smckusick #define SUBCYCLELIST 2048 32152648Smckusick #define ANYDEBUG 4096 322