xref: /openbsd/usr.bin/gprof/gprof.h (revision d6966193)
1*d6966193Sderaadt /*	$OpenBSD: gprof.h,v 1.14 2013/10/19 13:51:40 deraadt Exp $	*/
2f3bae140Sderaadt /*	$NetBSD: gprof.h,v 1.13 1996/04/01 21:54:06 mark Exp $	*/
3df930be7Sderaadt 
4df930be7Sderaadt /*
5df930be7Sderaadt  * Copyright (c) 1983, 1993
6df930be7Sderaadt  *	The Regents of the University of California.  All rights reserved.
7df930be7Sderaadt  *
8df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
9df930be7Sderaadt  * modification, are permitted provided that the following conditions
10df930be7Sderaadt  * are met:
11df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
12df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
13df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
14df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
15df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
16f75387cbSmillert  * 3. Neither the name of the University nor the names of its contributors
17df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
18df930be7Sderaadt  *    without specific prior written permission.
19df930be7Sderaadt  *
20df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df930be7Sderaadt  * SUCH DAMAGE.
31df930be7Sderaadt  *
32df930be7Sderaadt  *	@(#)gprof.h	8.1 (Berkeley) 6/6/93
33df930be7Sderaadt  */
34df930be7Sderaadt 
35df930be7Sderaadt #include <sys/types.h>
36df930be7Sderaadt #include <sys/stat.h>
37df930be7Sderaadt #include <sys/gmon.h>
38df930be7Sderaadt 
39df930be7Sderaadt #include <a.out.h>
40df930be7Sderaadt #include <stdio.h>
41df930be7Sderaadt #include <stdlib.h>
42d2559c65Smickey #include <err.h>
43df930be7Sderaadt 
4473e984b5Sespie #include MD_INCLUDE
45df930be7Sderaadt 
46df930be7Sderaadt     /*
47df930be7Sderaadt      * booleans
48df930be7Sderaadt      */
49df930be7Sderaadt typedef int	bool;
50df930be7Sderaadt #define	FALSE	0
51df930be7Sderaadt #define	TRUE	1
52df930be7Sderaadt 
53df930be7Sderaadt     /*
54df930be7Sderaadt      *	ticks per second
55df930be7Sderaadt      */
56df930be7Sderaadt long	hz;
57df930be7Sderaadt 
58df930be7Sderaadt typedef	u_short UNIT;		/* unit of profiling */
59df930be7Sderaadt char	*a_outname;
60df930be7Sderaadt #define	A_OUTNAME		"a.out"
61df930be7Sderaadt 
62df930be7Sderaadt char	*gmonname;
63df930be7Sderaadt #define	GMONNAME		"gmon.out"
64df930be7Sderaadt #define	GMONSUM			"gmon.sum"
65df930be7Sderaadt 
66df930be7Sderaadt     /*
67df930be7Sderaadt      *	a constructed arc,
68df930be7Sderaadt      *	    with pointers to the namelist entry of the parent and the child,
69df930be7Sderaadt      *	    a count of how many times this arc was traversed,
70df930be7Sderaadt      *	    and pointers to the next parent of this child and
71df930be7Sderaadt      *		the next child of this parent.
72df930be7Sderaadt      */
73df930be7Sderaadt struct arcstruct {
74df930be7Sderaadt     struct nl		*arc_parentp;	/* pointer to parent's nl entry */
75df930be7Sderaadt     struct nl		*arc_childp;	/* pointer to child's nl entry */
76df930be7Sderaadt     long		arc_count;	/* num calls from parent to child */
77df930be7Sderaadt     double		arc_time;	/* time inherited along arc */
78df930be7Sderaadt     double		arc_childtime;	/* childtime inherited along arc */
79df930be7Sderaadt     struct arcstruct	*arc_parentlist; /* parents-of-this-child list */
80df930be7Sderaadt     struct arcstruct	*arc_childlist;	/* children-of-this-parent list */
81df930be7Sderaadt     struct arcstruct	*arc_next;	/* list of arcs on cycle */
82df930be7Sderaadt     unsigned short	arc_cyclecnt;	/* num cycles involved in */
83df930be7Sderaadt     unsigned short	arc_flags;	/* see below */
84df930be7Sderaadt };
85df930be7Sderaadt typedef struct arcstruct	arctype;
86df930be7Sderaadt 
87df930be7Sderaadt     /*
88df930be7Sderaadt      * arc flags
89df930be7Sderaadt      */
90df930be7Sderaadt #define	DEADARC	0x01	/* time should not propagate across the arc */
91df930be7Sderaadt #define	ONLIST	0x02	/* arc is on list of arcs in cycles */
92df930be7Sderaadt 
93df930be7Sderaadt     /*
94df930be7Sderaadt      * The symbol table;
95df930be7Sderaadt      * for each external in the specified file we gather
96df930be7Sderaadt      * its address, the number of calls and compute its share of cpu time.
97df930be7Sderaadt      */
98df930be7Sderaadt struct nl {
9990a99070Sart     const char		*name;		/* the name */
100df930be7Sderaadt     unsigned long	value;		/* the pc entry point */
101df930be7Sderaadt     unsigned long	svalue;		/* entry point aligned to histograms */
102df930be7Sderaadt     double		time;		/* ticks in this routine */
103df930be7Sderaadt     double		childtime;	/* cumulative ticks in children */
104df930be7Sderaadt     long		ncall;		/* how many times called */
105df930be7Sderaadt     long		npropcall;	/* times called by live arcs */
106df930be7Sderaadt     long		selfcalls;	/* how many calls to self */
107df930be7Sderaadt     double		propfraction;	/* what % of time propagates */
108df930be7Sderaadt     double		propself;	/* how much self time propagates */
109df930be7Sderaadt     double		propchild;	/* how much child time propagates */
110df930be7Sderaadt     short		printflag;	/* should this be printed? */
111df930be7Sderaadt     short		flags;		/* see below */
112df930be7Sderaadt     int			index;		/* index in the graph list */
113df930be7Sderaadt     int			toporder;	/* graph call chain top-sort order */
114df930be7Sderaadt     int			cycleno;	/* internal number of cycle on */
115df930be7Sderaadt     int			parentcnt;	/* number of live parent arcs */
116df930be7Sderaadt     struct nl		*cyclehead;	/* pointer to head of cycle */
117df930be7Sderaadt     struct nl		*cnext;		/* pointer to next member of cycle */
118df930be7Sderaadt     arctype		*parents;	/* list of caller arcs */
119df930be7Sderaadt     arctype		*children;	/* list of callee arcs */
120df930be7Sderaadt };
121df930be7Sderaadt typedef struct nl	nltype;
122df930be7Sderaadt 
123df930be7Sderaadt nltype	*nl;			/* the whole namelist */
124df930be7Sderaadt nltype	*npe;			/* the virtual end of the namelist */
125df930be7Sderaadt int	nname;			/* the number of function names */
126df930be7Sderaadt 
127df930be7Sderaadt #define	HASCYCLEXIT	0x08	/* node has arc exiting from cycle */
128df930be7Sderaadt #define	CYCLEHEAD	0x10	/* node marked as head of a cycle */
129df930be7Sderaadt #define	VISITED		0x20	/* node visited during a cycle */
130df930be7Sderaadt 
131df930be7Sderaadt     /*
132df930be7Sderaadt      * The cycle list.
133df930be7Sderaadt      * for each subcycle within an identified cycle, we gather
134df930be7Sderaadt      * its size and the list of included arcs.
135df930be7Sderaadt      */
136df930be7Sderaadt struct cl {
137df930be7Sderaadt     int		size;		/* length of cycle */
138df930be7Sderaadt     struct cl	*next;		/* next member of list */
139df930be7Sderaadt     arctype	*list[1];	/* list of arcs in cycle */
140df930be7Sderaadt     /* actually longer */
141df930be7Sderaadt };
142df930be7Sderaadt typedef struct cl cltype;
143df930be7Sderaadt 
144df930be7Sderaadt arctype	*archead;		/* the head of arcs in current cycle list */
145df930be7Sderaadt cltype	*cyclehead;		/* the head of the list */
146df930be7Sderaadt int	cyclecnt;		/* the number of cycles found */
147df930be7Sderaadt #define	CYCLEMAX	100	/* maximum cycles before cutting one of them */
148df930be7Sderaadt 
149df930be7Sderaadt     /*
150df930be7Sderaadt      *	flag which marks a nl entry as topologically ``busy''
151df930be7Sderaadt      *	flag which marks a nl entry as topologically ``not_numbered''
152df930be7Sderaadt      */
153df930be7Sderaadt #define	DFN_BUSY	-1
154df930be7Sderaadt #define	DFN_NAN		0
155df930be7Sderaadt 
156df930be7Sderaadt     /*
157df930be7Sderaadt      *	namelist entries for cycle headers.
158df930be7Sderaadt      *	the number of discovered cycles.
159df930be7Sderaadt      */
160df930be7Sderaadt nltype	*cyclenl;		/* cycle header namelist */
161df930be7Sderaadt int	ncycle;			/* number of cycles discovered */
162df930be7Sderaadt 
163df930be7Sderaadt     /*
164df930be7Sderaadt      * The header on the gmon.out file.
165df930be7Sderaadt      * gmon.out consists of a struct phdr (defined in gmon.h)
166df930be7Sderaadt      * and then an array of ncnt samples representing the
167df930be7Sderaadt      * discretized program counter values.
168df930be7Sderaadt      *
169df930be7Sderaadt      *	Backward compatible old style header
170df930be7Sderaadt      */
171df930be7Sderaadt struct ophdr {
172df930be7Sderaadt     UNIT	*lpc;
173df930be7Sderaadt     UNIT	*hpc;
174df930be7Sderaadt     int		ncnt;
175df930be7Sderaadt };
176df930be7Sderaadt 
177df930be7Sderaadt int	debug;
178df930be7Sderaadt 
179df930be7Sderaadt     /*
180df930be7Sderaadt      * Each discretized pc sample has
181df930be7Sderaadt      * a count of the number of samples in its range
182df930be7Sderaadt      */
183df930be7Sderaadt UNIT	*samples;
184df930be7Sderaadt 
185df930be7Sderaadt unsigned long	s_lowpc;	/* lowpc from the profile file */
186df930be7Sderaadt unsigned long	s_highpc;	/* highpc from the profile file */
187a5f4a2ceSart unsigned long	lowpc, highpc;	/* range profiled, in UNIT's */
188df930be7Sderaadt unsigned sampbytes;		/* number of bytes of samples */
189df930be7Sderaadt int	nsamples;		/* number of samples */
190df930be7Sderaadt double	actime;			/* accumulated time thus far for putprofline */
191df930be7Sderaadt double	totime;			/* total time for all routines */
192df930be7Sderaadt double	printtime;		/* total of time being printed */
193df930be7Sderaadt double	scale;			/* scale factor converting samples to pc
194df930be7Sderaadt 				   values: each sample covers scale bytes */
195df930be7Sderaadt unsigned char	*textspace;	/* text space of a.out in core */
196df930be7Sderaadt int	cyclethreshold;		/* with -C, minimum cycle size to ignore */
197df930be7Sderaadt 
198df930be7Sderaadt     /*
199df930be7Sderaadt      *	option flags, from a to z.
200df930be7Sderaadt      */
201df930be7Sderaadt bool	aflag;				/* suppress static functions */
202df930be7Sderaadt bool	bflag;				/* blurbs, too */
203df930be7Sderaadt bool	cflag;				/* discovered call graph, too */
204df930be7Sderaadt bool	Cflag;				/* find cut-set to eliminate cycles */
205df930be7Sderaadt bool	dflag;				/* debugging options */
206df930be7Sderaadt bool	eflag;				/* specific functions excluded */
207df930be7Sderaadt bool	Eflag;				/* functions excluded with time */
208df930be7Sderaadt bool	fflag;				/* specific functions requested */
209df930be7Sderaadt bool	Fflag;				/* functions requested with time */
210df930be7Sderaadt bool	kflag;				/* arcs to be deleted */
211df930be7Sderaadt bool	sflag;				/* sum multiple gmon.out files */
212df930be7Sderaadt bool	zflag;				/* zero time/called functions, too */
213df930be7Sderaadt 
214df930be7Sderaadt     /*
215df930be7Sderaadt      *	structure for various string lists
216df930be7Sderaadt      */
217df930be7Sderaadt struct stringlist {
218df930be7Sderaadt     struct stringlist	*next;
219df930be7Sderaadt     char		*string;
220df930be7Sderaadt };
221df930be7Sderaadt struct stringlist	*elist;
222df930be7Sderaadt struct stringlist	*Elist;
223df930be7Sderaadt struct stringlist	*flist;
224df930be7Sderaadt struct stringlist	*Flist;
225df930be7Sderaadt struct stringlist	*kfromlist;
226df930be7Sderaadt struct stringlist	*ktolist;
227df930be7Sderaadt 
228df930be7Sderaadt     /*
229df930be7Sderaadt      *	function declarations
230df930be7Sderaadt      */
2319bd74cdeSespie void		addarc(nltype *, nltype *, long);
232c72b5b24Smillert int		addcycle(arctype **, arctype **);
2339bd74cdeSespie void		addlist(struct stringlist *, char *);
2349bd74cdeSespie int		arccmp(arctype *, arctype *);
2359bd74cdeSespie arctype		*arclookup(nltype *, nltype *);
2369bd74cdeSespie void		asgnsamples(void);
2379bd74cdeSespie void		alignentries(void);
2389bd74cdeSespie void		printblurb(const char *);
239c72b5b24Smillert int		cycleanalyze(void);
240c72b5b24Smillert void		cyclelink(void);
241c72b5b24Smillert void		cycletime(void);
242c72b5b24Smillert void		compresslist(void);
2439bd74cdeSespie int		descend(nltype *, arctype **, arctype **);
2449bd74cdeSespie void		dfn(nltype *);
2459bd74cdeSespie bool		dfn_busy(nltype *);
2469bd74cdeSespie void		dfn_findcycle(nltype *);
2479bd74cdeSespie void		dfn_init(void);
2489bd74cdeSespie bool		dfn_numbered(nltype *);
2499bd74cdeSespie void		dfn_post_visit(nltype *);
2509bd74cdeSespie void		dfn_pre_visit(nltype *);
2519bd74cdeSespie void		dfn_self_cycle(nltype *);
2529bd74cdeSespie nltype		**doarcs(void);
253c72b5b24Smillert void		doflags(void);
254c72b5b24Smillert void		dotime(void);
2559bd74cdeSespie void		dumpsum(const char *);
2569bd74cdeSespie void		findcall(nltype *, unsigned long, unsigned long);
2579bd74cdeSespie void		flatprofheader(void);
2589bd74cdeSespie void		flatprofline(nltype *);
25990a99070Sart int		getnfile(const char *, char ***);
2609bd74cdeSespie void		getpfile(const char *);
2619bd74cdeSespie void		gprofheader(void);
2629bd74cdeSespie void		gprofline(nltype *);
2639bd74cdeSespie int		hertz(void);
2649bd74cdeSespie void		inheritflags(nltype *);
2659bd74cdeSespie unsigned long	max(unsigned long, unsigned long);
2669bd74cdeSespie int		membercmp(nltype *, nltype *);
2679bd74cdeSespie unsigned long	min(unsigned long, unsigned long);
2689bd74cdeSespie nltype		*nllookup(unsigned long);
2699bd74cdeSespie bool		onlist(struct stringlist *, const char *);
2709bd74cdeSespie FILE		*openpfile(const char *);
2719bd74cdeSespie void		printchildren(nltype *);
2729bd74cdeSespie void		printcycle(nltype *);
2739bd74cdeSespie void		printgprof(nltype **);
2749bd74cdeSespie void		printindex(void);
2759bd74cdeSespie void		printmembers(nltype *);
2769bd74cdeSespie void		printname(nltype *);
2779bd74cdeSespie void		printparents(nltype *);
2789bd74cdeSespie void		printprof(void);
279d2559c65Smickey void		readsamples(FILE *);
2809bd74cdeSespie void		sortchildren(nltype *);
2819bd74cdeSespie void		sortmembers(nltype *);
2829bd74cdeSespie void		sortparents(nltype *);
2839bd74cdeSespie void		tally(struct rawarc *);
2849bd74cdeSespie int		timecmp(nltype **, nltype **);
285c72b5b24Smillert void		timepropagate(nltype *);
2869bd74cdeSespie int		topcmp(nltype **, nltype **);
2879bd74cdeSespie int		totalcmp(nltype **, nltype **);
288df930be7Sderaadt 
289df930be7Sderaadt #define	LESSTHAN	-1
290df930be7Sderaadt #define	EQUALTO		0
291df930be7Sderaadt #define	GREATERTHAN	1
292df930be7Sderaadt 
293df930be7Sderaadt #define	DFNDEBUG	1
294df930be7Sderaadt #define	CYCLEDEBUG	2
295df930be7Sderaadt #define	ARCDEBUG	4
296df930be7Sderaadt #define	TALLYDEBUG	8
297df930be7Sderaadt #define	TIMEDEBUG	16
298df930be7Sderaadt #define	SAMPLEDEBUG	32
299*d6966193Sderaadt #define	ELFDEBUG	64
300df930be7Sderaadt #define	CALLDEBUG	128
301df930be7Sderaadt #define	LOOKUPDEBUG	256
302df930be7Sderaadt #define	PROPDEBUG	512
303df930be7Sderaadt #define	BREAKCYCLE	1024
304df930be7Sderaadt #define	SUBCYCLELIST	2048
305df930be7Sderaadt #define	ANYDEBUG	4096
306