gprof.h revision 107718
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 107718 2002-12-10 02:22:01Z grehan $
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
4485353Speter#if __ia64__
4585353Speter#   include "ia64.h"
4685353Speter#endif
4748839Ssimokawa#if __alpha__
4848839Ssimokawa#   include "alpha.h"
4948839Ssimokawa#endif
5091976Sjake#if __sparc64__
5191976Sjake#   include "sparc64.h"
5291976Sjake#endif
53107718Sgrehan#if __powerpc__
54107718Sgrehan#   include "powerpc.h"
55107718Sgrehan#endif
561590Srgrimes#if vax
571590Srgrimes#   include "vax.h"
581590Srgrimes#endif
591590Srgrimes#if sparc
601590Srgrimes#   include "sparc.h"
611590Srgrimes#endif
621590Srgrimes#if tahoe
631590Srgrimes#   include "tahoe.h"
641590Srgrimes#endif
651590Srgrimes#if hp300
661590Srgrimes#   include "hp300.h"
671590Srgrimes#endif
681590Srgrimes#if luna68k
691590Srgrimes#   include "luna68k.h"
701590Srgrimes#endif
7197551Salfred#if __i386__
721590Srgrimes#   include "i386.h"
731590Srgrimes#endif
741590Srgrimes#if mips
751590Srgrimes#   include "mips.h"
761590Srgrimes#endif
771590Srgrimes
781590Srgrimes
791590Srgrimes    /*
801590Srgrimes     * booleans
811590Srgrimes     */
821590Srgrimestypedef int	bool;
831590Srgrimes#define	FALSE	0
841590Srgrimes#define	TRUE	1
851590Srgrimes
861590Srgrimes    /*
8791735Sbde     *	Historical scale factor in profil(2)'s algorithm for converting
8891735Sbde     *	pc addresses to bucket numbers.  This now just complicates the
8991735Sbde     *	scaling and makes bucket:pc densities of more than 1/2 useless.
9091735Sbde     */
9191735Sbde#define	HISTORICAL_SCALE_2	2
9291735Sbde
9391735Sbde    /*
941590Srgrimes     *	ticks per second
951590Srgrimes     */
961590Srgrimeslong	hz;
971590Srgrimes
9891738Sbdesize_t	histcounter_size;
9991738Sbdeint	histcounter_type;
10091010Sbde
1011590Srgrimeschar	*a_outname;
1021590Srgrimes#define	A_OUTNAME		"a.out"
1031590Srgrimes
1041590Srgrimeschar	*gmonname;
1051590Srgrimes#define	GMONSUM			"gmon.sum"
1061590Srgrimes
1071590Srgrimes    /*
1081590Srgrimes     *	a constructed arc,
1091590Srgrimes     *	    with pointers to the namelist entry of the parent and the child,
1101590Srgrimes     *	    a count of how many times this arc was traversed,
1111590Srgrimes     *	    and pointers to the next parent of this child and
1121590Srgrimes     *		the next child of this parent.
1131590Srgrimes     */
1141590Srgrimesstruct arcstruct {
1151590Srgrimes    struct nl		*arc_parentp;	/* pointer to parent's nl entry */
1161590Srgrimes    struct nl		*arc_childp;	/* pointer to child's nl entry */
1171590Srgrimes    long		arc_count;	/* num calls from parent to child */
1181590Srgrimes    double		arc_time;	/* time inherited along arc */
1191590Srgrimes    double		arc_childtime;	/* childtime inherited along arc */
1201590Srgrimes    struct arcstruct	*arc_parentlist; /* parents-of-this-child list */
1211590Srgrimes    struct arcstruct	*arc_childlist;	/* children-of-this-parent list */
1221590Srgrimes    struct arcstruct	*arc_next;	/* list of arcs on cycle */
1231590Srgrimes    unsigned short	arc_cyclecnt;	/* num cycles involved in */
1241590Srgrimes    unsigned short	arc_flags;	/* see below */
1251590Srgrimes};
1261590Srgrimestypedef struct arcstruct	arctype;
1271590Srgrimes
1281590Srgrimes    /*
1291590Srgrimes     * arc flags
1301590Srgrimes     */
1311590Srgrimes#define	DEADARC	0x01	/* time should not propagate across the arc */
1321590Srgrimes#define	ONLIST	0x02	/* arc is on list of arcs in cycles */
1331590Srgrimes
1341590Srgrimes    /*
1351590Srgrimes     * The symbol table;
1361590Srgrimes     * for each external in the specified file we gather
137105243Scharnier     * its address, the number of calls and compute its share of CPU time.
1381590Srgrimes     */
1391590Srgrimesstruct nl {
14038928Sjdp    const char		*name;		/* the name */
1411590Srgrimes    unsigned long	value;		/* the pc entry point */
1421590Srgrimes    unsigned long	svalue;		/* entry point aligned to histograms */
1431590Srgrimes    double		time;		/* ticks in this routine */
1441590Srgrimes    double		childtime;	/* cumulative ticks in children */
1451590Srgrimes    long		ncall;		/* how many times called */
1461590Srgrimes    long		npropcall;	/* times called by live arcs */
1471590Srgrimes    long		selfcalls;	/* how many calls to self */
1481590Srgrimes    double		propfraction;	/* what % of time propagates */
1491590Srgrimes    double		propself;	/* how much self time propagates */
1501590Srgrimes    double		propchild;	/* how much child time propagates */
1511590Srgrimes    short		printflag;	/* should this be printed? */
1521590Srgrimes    short		flags;		/* see below */
1531590Srgrimes    int			index;		/* index in the graph list */
1541590Srgrimes    int			toporder;	/* graph call chain top-sort order */
1551590Srgrimes    int			cycleno;	/* internal number of cycle on */
1561590Srgrimes    int			parentcnt;	/* number of live parent arcs */
1571590Srgrimes    struct nl		*cyclehead;	/* pointer to head of cycle */
1581590Srgrimes    struct nl		*cnext;		/* pointer to next member of cycle */
1591590Srgrimes    arctype		*parents;	/* list of caller arcs */
1601590Srgrimes    arctype		*children;	/* list of callee arcs */
1611590Srgrimes};
1621590Srgrimestypedef struct nl	nltype;
1631590Srgrimes
1641590Srgrimesnltype	*nl;			/* the whole namelist */
1651590Srgrimesnltype	*npe;			/* the virtual end of the namelist */
1661590Srgrimesint	nname;			/* the number of function names */
1671590Srgrimes
1681590Srgrimes#define	HASCYCLEXIT	0x08	/* node has arc exiting from cycle */
1691590Srgrimes#define	CYCLEHEAD	0x10	/* node marked as head of a cycle */
1701590Srgrimes#define	VISITED		0x20	/* node visited during a cycle */
1711590Srgrimes
1721590Srgrimes    /*
1731590Srgrimes     * The cycle list.
1741590Srgrimes     * for each subcycle within an identified cycle, we gather
1751590Srgrimes     * its size and the list of included arcs.
1761590Srgrimes     */
1771590Srgrimesstruct cl {
1781590Srgrimes    int		size;		/* length of cycle */
1791590Srgrimes    struct cl	*next;		/* next member of list */
1801590Srgrimes    arctype	*list[1];	/* list of arcs in cycle */
1811590Srgrimes    /* actually longer */
1821590Srgrimes};
1831590Srgrimestypedef struct cl cltype;
1841590Srgrimes
1851590Srgrimesarctype	*archead;		/* the head of arcs in current cycle list */
1861590Srgrimescltype	*cyclehead;		/* the head of the list */
1871590Srgrimesint	cyclecnt;		/* the number of cycles found */
1881590Srgrimes#define	CYCLEMAX	100	/* maximum cycles before cutting one of them */
1891590Srgrimes
1901590Srgrimes    /*
1911590Srgrimes     *	flag which marks a nl entry as topologically ``busy''
1921590Srgrimes     *	flag which marks a nl entry as topologically ``not_numbered''
1931590Srgrimes     */
1941590Srgrimes#define	DFN_BUSY	-1
1951590Srgrimes#define	DFN_NAN		0
1961590Srgrimes
1978874Srgrimes    /*
1981590Srgrimes     *	namelist entries for cycle headers.
1991590Srgrimes     *	the number of discovered cycles.
2001590Srgrimes     */
2011590Srgrimesnltype	*cyclenl;		/* cycle header namelist */
2021590Srgrimesint	ncycle;			/* number of cycles discovered */
2031590Srgrimes
2041590Srgrimes    /*
2051590Srgrimes     * The header on the gmon.out file.
2061590Srgrimes     * gmon.out consists of a struct phdr (defined in gmon.h)
2071590Srgrimes     * and then an array of ncnt samples representing the
2081590Srgrimes     * discretized program counter values.
2091590Srgrimes     *
2101590Srgrimes     *	Backward compatible old style header
2111590Srgrimes     */
2121590Srgrimesstruct ophdr {
21391738Sbde    u_short	*lpc;
21491738Sbde    u_short	*hpc;
2151590Srgrimes    int		ncnt;
2161590Srgrimes};
2171590Srgrimes
2181590Srgrimesint	debug;
2191590Srgrimes
2201590Srgrimes    /*
2211590Srgrimes     * Each discretized pc sample has
2221590Srgrimes     * a count of the number of samples in its range
2231590Srgrimes     */
22491738Sbdedouble	*samples;
2251590Srgrimes
2261590Srgrimesunsigned long	s_lowpc;	/* lowpc from the profile file */
2271590Srgrimesunsigned long	s_highpc;	/* highpc from the profile file */
22891738Sbdeunsigned long	lowpc, highpc;	/* range profiled, in historical units  */
2291590Srgrimesunsigned sampbytes;		/* number of bytes of samples */
2301590Srgrimesint	nsamples;		/* number of samples */
2311590Srgrimesdouble	actime;			/* accumulated time thus far for putprofline */
2321590Srgrimesdouble	totime;			/* total time for all routines */
2331590Srgrimesdouble	printtime;		/* total of time being printed */
2341590Srgrimesdouble	scale;			/* scale factor converting samples to pc
2351590Srgrimes				   values: each sample covers scale bytes */
2361590Srgrimesunsigned char	*textspace;	/* text space of a.out in core */
2371590Srgrimesint	cyclethreshold;		/* with -C, minimum cycle size to ignore */
2381590Srgrimes
2391590Srgrimes    /*
2401590Srgrimes     *	option flags, from a to z.
2411590Srgrimes     */
2421590Srgrimesbool	aflag;				/* suppress static functions */
2431590Srgrimesbool	bflag;				/* blurbs, too */
2441590Srgrimesbool	cflag;				/* discovered call graph, too */
2451590Srgrimesbool	Cflag;				/* find cut-set to eliminate cycles */
2461590Srgrimesbool	dflag;				/* debugging options */
2471590Srgrimesbool	eflag;				/* specific functions excluded */
2481590Srgrimesbool	Eflag;				/* functions excluded with time */
2491590Srgrimesbool	fflag;				/* specific functions requested */
2501590Srgrimesbool	Fflag;				/* functions requested with time */
2511590Srgrimesbool	kflag;				/* arcs to be deleted */
25285739Sgreenbool	Kflag;				/* use the running kernel for symbols */
2531590Srgrimesbool	sflag;				/* sum multiple gmon.out files */
25438928Sjdpbool	uflag;				/* suppress symbols hidden from C */
2551590Srgrimesbool	zflag;				/* zero time/called functions, too */
2561590Srgrimes
2571590Srgrimes    /*
2581590Srgrimes     *	structure for various string lists
2591590Srgrimes     */
2601590Srgrimesstruct stringlist {
2611590Srgrimes    struct stringlist	*next;
2621590Srgrimes    char		*string;
2631590Srgrimes};
2641590Srgrimesstruct stringlist	*elist;
2651590Srgrimesstruct stringlist	*Elist;
2661590Srgrimesstruct stringlist	*flist;
2671590Srgrimesstruct stringlist	*Flist;
2681590Srgrimesstruct stringlist	*kfromlist;
2691590Srgrimesstruct stringlist	*ktolist;
2701590Srgrimes
2711590Srgrimes    /*
2721590Srgrimes     *	function declarations
2731590Srgrimes     */
274105243Scharniervoid		addarc(nltype *, nltype *, long);
275105243Scharnierbool		addcycle(arctype **, arctype **);
276105243Scharniervoid		addlist(struct stringlist *, char *);
277105243Scharniervoid		alignentries(void);
27838928Sjdpint		aout_getnfile(const char *, char ***);
2791590Srgrimesint		arccmp();
2801590Srgrimesarctype		*arclookup();
281105243Scharniervoid		asgnsamples(void);
282105243Scharniervoid		compresslist(void);
283105243Scharnierbool		cycleanalyze(void);
284105243Scharniervoid		cyclelink(void);
285105243Scharniervoid		cycletime(void);
286105243Scharnierbool		descend(nltype *, arctype **, arctype **);
287105243Scharniervoid		dfn(nltype *);
2881590Srgrimesbool		dfn_busy();
289105243Scharniervoid		dfn_findcycle(nltype *);
290105243Scharniervoid		dfn_init(void);
2911590Srgrimesbool		dfn_numbered();
292105243Scharniervoid		dfn_post_visit(nltype *);
293105243Scharniervoid		dfn_pre_visit(nltype *);
294105243Scharniervoid		dfn_self_cycle(nltype *);
2951590Srgrimesnltype		**doarcs();
296105243Scharniervoid		doflags(void);
297105243Scharniervoid		dotime(void);
298105243Scharniervoid		dumpsum(char *);
29938928Sjdpint		elf_getnfile(const char *, char ***);
30038928Sjdp/*
3011590Srgrimes		findcalls();
3021590Srgrimes*/
303105243Scharniervoid		flatprofheader(void);
304105243Scharniervoid		flatprofline(nltype *);
305105243Scharniervoid		getpfile(char *);
3061590Srgrimes/*
3071590Srgrimes		gprofheader();
3081590Srgrimes		gprofline();
30985739Sgreen*/
310105243Scharniervoid		inheritflags(nltype *);
31185739Sgreenint		kernel_getnfile(const char *, char ***);
31285739Sgreen/*
3131590Srgrimes		main();
3141590Srgrimes*/
3151590Srgrimesunsigned long	max();
3161590Srgrimesint		membercmp();
3171590Srgrimesunsigned long	min();
3181590Srgrimesnltype		*nllookup();
319105243Scharnierbool		onlist(struct stringlist *, const char *);
3201590SrgrimesFILE		*openpfile();
3211590Srgrimeslong		operandlength();
3221590Srgrimesoperandenum	operandmode();
3231590Srgrimeschar		*operandname();
324105243Scharniervoid		printblurb(char *);
325105243Scharniervoid		printchildren(nltype *);
326105243Scharniervoid		printcycle(nltype *);
327105243Scharniervoid		printgprof(nltype **);
328105243Scharniervoid		printindex(void);
329105243Scharniervoid		printmembers(nltype *);
330105243Scharniervoid		printname(nltype *);
331105243Scharniervoid		printparents(nltype *);
332105243Scharniervoid		printprof(void);
333105243Scharniervoid		printsubcycle(cltype *);
334105243Scharniervoid		readsamples(FILE *);
3351590Srgrimesunsigned long	reladdr();
336105243Scharniervoid		sortchildren(nltype *);
337105243Scharniervoid		sortmembers(nltype *);
338105243Scharniervoid		sortparents(nltype *);
339105243Scharniervoid		tally(struct rawarc *);
340105243Scharniervoid		timepropagate(nltype *);
3411590Srgrimesint		totalcmp();
3421590Srgrimes
3431590Srgrimes#define	LESSTHAN	-1
3441590Srgrimes#define	EQUALTO		0
3451590Srgrimes#define	GREATERTHAN	1
3461590Srgrimes
3471590Srgrimes#define	DFNDEBUG	1
3481590Srgrimes#define	CYCLEDEBUG	2
3491590Srgrimes#define	ARCDEBUG	4
3501590Srgrimes#define	TALLYDEBUG	8
3511590Srgrimes#define	TIMEDEBUG	16
3521590Srgrimes#define	SAMPLEDEBUG	32
3531590Srgrimes#define	AOUTDEBUG	64
3541590Srgrimes#define	CALLDEBUG	128
3551590Srgrimes#define	LOOKUPDEBUG	256
3561590Srgrimes#define	PROPDEBUG	512
3571590Srgrimes#define	BREAKCYCLE	1024
3581590Srgrimes#define	SUBCYCLELIST	2048
3591590Srgrimes#define	ANYDEBUG	4096
360