xref: /openbsd/usr.bin/gprof/gprof.h (revision 90a99070)
1*90a99070Sart /*	$OpenBSD: gprof.h,v 1.8 2002/05/08 16:46:35 art 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.
16df930be7Sderaadt  * 3. All advertising materials mentioning features or use of this software
17df930be7Sderaadt  *    must display the following acknowledgement:
18df930be7Sderaadt  *	This product includes software developed by the University of
19df930be7Sderaadt  *	California, Berkeley and its contributors.
20df930be7Sderaadt  * 4. Neither the name of the University nor the names of its contributors
21df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
22df930be7Sderaadt  *    without specific prior written permission.
23df930be7Sderaadt  *
24df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df930be7Sderaadt  * SUCH DAMAGE.
35df930be7Sderaadt  *
36df930be7Sderaadt  *	@(#)gprof.h	8.1 (Berkeley) 6/6/93
37df930be7Sderaadt  */
38df930be7Sderaadt 
39df930be7Sderaadt #include <sys/types.h>
40df930be7Sderaadt #include <sys/stat.h>
41df930be7Sderaadt #include <sys/gmon.h>
42df930be7Sderaadt 
43df930be7Sderaadt #include <a.out.h>
44df930be7Sderaadt #include <stdio.h>
45df930be7Sderaadt #include <stdlib.h>
46d2559c65Smickey #include <err.h>
47df930be7Sderaadt 
484bc6cf27Smiod #include "machine.h"
49df930be7Sderaadt 
50df930be7Sderaadt     /*
51df930be7Sderaadt      * booleans
52df930be7Sderaadt      */
53df930be7Sderaadt typedef int	bool;
54df930be7Sderaadt #define	FALSE	0
55df930be7Sderaadt #define	TRUE	1
56df930be7Sderaadt 
57df930be7Sderaadt     /*
58df930be7Sderaadt      *	ticks per second
59df930be7Sderaadt      */
60df930be7Sderaadt long	hz;
61df930be7Sderaadt 
62df930be7Sderaadt typedef	u_short UNIT;		/* unit of profiling */
63df930be7Sderaadt char	*a_outname;
64df930be7Sderaadt #define	A_OUTNAME		"a.out"
65df930be7Sderaadt 
66df930be7Sderaadt char	*gmonname;
67df930be7Sderaadt #define	GMONNAME		"gmon.out"
68df930be7Sderaadt #define	GMONSUM			"gmon.sum"
69df930be7Sderaadt 
70df930be7Sderaadt     /*
71df930be7Sderaadt      *	a constructed arc,
72df930be7Sderaadt      *	    with pointers to the namelist entry of the parent and the child,
73df930be7Sderaadt      *	    a count of how many times this arc was traversed,
74df930be7Sderaadt      *	    and pointers to the next parent of this child and
75df930be7Sderaadt      *		the next child of this parent.
76df930be7Sderaadt      */
77df930be7Sderaadt struct arcstruct {
78df930be7Sderaadt     struct nl		*arc_parentp;	/* pointer to parent's nl entry */
79df930be7Sderaadt     struct nl		*arc_childp;	/* pointer to child's nl entry */
80df930be7Sderaadt     long		arc_count;	/* num calls from parent to child */
81df930be7Sderaadt     double		arc_time;	/* time inherited along arc */
82df930be7Sderaadt     double		arc_childtime;	/* childtime inherited along arc */
83df930be7Sderaadt     struct arcstruct	*arc_parentlist; /* parents-of-this-child list */
84df930be7Sderaadt     struct arcstruct	*arc_childlist;	/* children-of-this-parent list */
85df930be7Sderaadt     struct arcstruct	*arc_next;	/* list of arcs on cycle */
86df930be7Sderaadt     unsigned short	arc_cyclecnt;	/* num cycles involved in */
87df930be7Sderaadt     unsigned short	arc_flags;	/* see below */
88df930be7Sderaadt };
89df930be7Sderaadt typedef struct arcstruct	arctype;
90df930be7Sderaadt 
91df930be7Sderaadt     /*
92df930be7Sderaadt      * arc flags
93df930be7Sderaadt      */
94df930be7Sderaadt #define	DEADARC	0x01	/* time should not propagate across the arc */
95df930be7Sderaadt #define	ONLIST	0x02	/* arc is on list of arcs in cycles */
96df930be7Sderaadt 
97df930be7Sderaadt     /*
98df930be7Sderaadt      * The symbol table;
99df930be7Sderaadt      * for each external in the specified file we gather
100df930be7Sderaadt      * its address, the number of calls and compute its share of cpu time.
101df930be7Sderaadt      */
102df930be7Sderaadt struct nl {
103*90a99070Sart     const char		*name;		/* the name */
104df930be7Sderaadt     unsigned long	value;		/* the pc entry point */
105df930be7Sderaadt     unsigned long	svalue;		/* entry point aligned to histograms */
106df930be7Sderaadt     double		time;		/* ticks in this routine */
107df930be7Sderaadt     double		childtime;	/* cumulative ticks in children */
108df930be7Sderaadt     long		ncall;		/* how many times called */
109df930be7Sderaadt     long		npropcall;	/* times called by live arcs */
110df930be7Sderaadt     long		selfcalls;	/* how many calls to self */
111df930be7Sderaadt     double		propfraction;	/* what % of time propagates */
112df930be7Sderaadt     double		propself;	/* how much self time propagates */
113df930be7Sderaadt     double		propchild;	/* how much child time propagates */
114df930be7Sderaadt     short		printflag;	/* should this be printed? */
115df930be7Sderaadt     short		flags;		/* see below */
116df930be7Sderaadt     int			index;		/* index in the graph list */
117df930be7Sderaadt     int			toporder;	/* graph call chain top-sort order */
118df930be7Sderaadt     int			cycleno;	/* internal number of cycle on */
119df930be7Sderaadt     int			parentcnt;	/* number of live parent arcs */
120df930be7Sderaadt     struct nl		*cyclehead;	/* pointer to head of cycle */
121df930be7Sderaadt     struct nl		*cnext;		/* pointer to next member of cycle */
122df930be7Sderaadt     arctype		*parents;	/* list of caller arcs */
123df930be7Sderaadt     arctype		*children;	/* list of callee arcs */
124df930be7Sderaadt };
125df930be7Sderaadt typedef struct nl	nltype;
126df930be7Sderaadt 
127df930be7Sderaadt nltype	*nl;			/* the whole namelist */
128df930be7Sderaadt nltype	*npe;			/* the virtual end of the namelist */
129df930be7Sderaadt int	nname;			/* the number of function names */
130df930be7Sderaadt 
131df930be7Sderaadt #define	HASCYCLEXIT	0x08	/* node has arc exiting from cycle */
132df930be7Sderaadt #define	CYCLEHEAD	0x10	/* node marked as head of a cycle */
133df930be7Sderaadt #define	VISITED		0x20	/* node visited during a cycle */
134df930be7Sderaadt 
135df930be7Sderaadt     /*
136df930be7Sderaadt      * The cycle list.
137df930be7Sderaadt      * for each subcycle within an identified cycle, we gather
138df930be7Sderaadt      * its size and the list of included arcs.
139df930be7Sderaadt      */
140df930be7Sderaadt struct cl {
141df930be7Sderaadt     int		size;		/* length of cycle */
142df930be7Sderaadt     struct cl	*next;		/* next member of list */
143df930be7Sderaadt     arctype	*list[1];	/* list of arcs in cycle */
144df930be7Sderaadt     /* actually longer */
145df930be7Sderaadt };
146df930be7Sderaadt typedef struct cl cltype;
147df930be7Sderaadt 
148df930be7Sderaadt arctype	*archead;		/* the head of arcs in current cycle list */
149df930be7Sderaadt cltype	*cyclehead;		/* the head of the list */
150df930be7Sderaadt int	cyclecnt;		/* the number of cycles found */
151df930be7Sderaadt #define	CYCLEMAX	100	/* maximum cycles before cutting one of them */
152df930be7Sderaadt 
153df930be7Sderaadt     /*
154df930be7Sderaadt      *	flag which marks a nl entry as topologically ``busy''
155df930be7Sderaadt      *	flag which marks a nl entry as topologically ``not_numbered''
156df930be7Sderaadt      */
157df930be7Sderaadt #define	DFN_BUSY	-1
158df930be7Sderaadt #define	DFN_NAN		0
159df930be7Sderaadt 
160df930be7Sderaadt     /*
161df930be7Sderaadt      *	namelist entries for cycle headers.
162df930be7Sderaadt      *	the number of discovered cycles.
163df930be7Sderaadt      */
164df930be7Sderaadt nltype	*cyclenl;		/* cycle header namelist */
165df930be7Sderaadt int	ncycle;			/* number of cycles discovered */
166df930be7Sderaadt 
167df930be7Sderaadt     /*
168df930be7Sderaadt      * The header on the gmon.out file.
169df930be7Sderaadt      * gmon.out consists of a struct phdr (defined in gmon.h)
170df930be7Sderaadt      * and then an array of ncnt samples representing the
171df930be7Sderaadt      * discretized program counter values.
172df930be7Sderaadt      *
173df930be7Sderaadt      *	Backward compatible old style header
174df930be7Sderaadt      */
175df930be7Sderaadt struct ophdr {
176df930be7Sderaadt     UNIT	*lpc;
177df930be7Sderaadt     UNIT	*hpc;
178df930be7Sderaadt     int		ncnt;
179df930be7Sderaadt };
180df930be7Sderaadt 
181df930be7Sderaadt int	debug;
182df930be7Sderaadt 
183df930be7Sderaadt     /*
184df930be7Sderaadt      * Each discretized pc sample has
185df930be7Sderaadt      * a count of the number of samples in its range
186df930be7Sderaadt      */
187df930be7Sderaadt UNIT	*samples;
188df930be7Sderaadt 
189df930be7Sderaadt unsigned long	s_lowpc;	/* lowpc from the profile file */
190df930be7Sderaadt unsigned long	s_highpc;	/* highpc from the profile file */
191df930be7Sderaadt unsigned lowpc, highpc;		/* range profiled, in UNIT's */
192df930be7Sderaadt unsigned sampbytes;		/* number of bytes of samples */
193df930be7Sderaadt int	nsamples;		/* number of samples */
194df930be7Sderaadt double	actime;			/* accumulated time thus far for putprofline */
195df930be7Sderaadt double	totime;			/* total time for all routines */
196df930be7Sderaadt double	printtime;		/* total of time being printed */
197df930be7Sderaadt double	scale;			/* scale factor converting samples to pc
198df930be7Sderaadt 				   values: each sample covers scale bytes */
199df930be7Sderaadt unsigned char	*textspace;	/* text space of a.out in core */
200df930be7Sderaadt int	cyclethreshold;		/* with -C, minimum cycle size to ignore */
201df930be7Sderaadt 
202df930be7Sderaadt     /*
203df930be7Sderaadt      *	option flags, from a to z.
204df930be7Sderaadt      */
205df930be7Sderaadt bool	aflag;				/* suppress static functions */
206df930be7Sderaadt bool	bflag;				/* blurbs, too */
207df930be7Sderaadt bool	cflag;				/* discovered call graph, too */
208df930be7Sderaadt bool	Cflag;				/* find cut-set to eliminate cycles */
209df930be7Sderaadt bool	dflag;				/* debugging options */
210df930be7Sderaadt bool	eflag;				/* specific functions excluded */
211df930be7Sderaadt bool	Eflag;				/* functions excluded with time */
212df930be7Sderaadt bool	fflag;				/* specific functions requested */
213df930be7Sderaadt bool	Fflag;				/* functions requested with time */
214df930be7Sderaadt bool	kflag;				/* arcs to be deleted */
215df930be7Sderaadt bool	sflag;				/* sum multiple gmon.out files */
216df930be7Sderaadt bool	zflag;				/* zero time/called functions, too */
217df930be7Sderaadt 
218df930be7Sderaadt     /*
219df930be7Sderaadt      *	structure for various string lists
220df930be7Sderaadt      */
221df930be7Sderaadt struct stringlist {
222df930be7Sderaadt     struct stringlist	*next;
223df930be7Sderaadt     char		*string;
224df930be7Sderaadt };
225df930be7Sderaadt struct stringlist	*elist;
226df930be7Sderaadt struct stringlist	*Elist;
227df930be7Sderaadt struct stringlist	*flist;
228df930be7Sderaadt struct stringlist	*Flist;
229df930be7Sderaadt struct stringlist	*kfromlist;
230df930be7Sderaadt struct stringlist	*ktolist;
231df930be7Sderaadt 
232df930be7Sderaadt     /*
233df930be7Sderaadt      *	function declarations
234df930be7Sderaadt      */
235d2559c65Smickey void		addarc();
236c72b5b24Smillert int		addcycle(arctype **, arctype **);
237c72b5b24Smillert void		addlist(struct stringlist *listp, char *funcname);
238df930be7Sderaadt int		arccmp();
239df930be7Sderaadt arctype		*arclookup();
240d2559c65Smickey void		asgnsamples();
241d2559c65Smickey void		alignentries();
242d2559c65Smickey void		printblurb();
243c72b5b24Smillert int		cycleanalyze(void);
244c72b5b24Smillert void		cyclelink(void);
245c72b5b24Smillert void		cycletime(void);
246c72b5b24Smillert void		compresslist(void);
247c72b5b24Smillert int		descend(nltype *node, arctype **stkstart, arctype **stkp);
248d2559c65Smickey void		dfn();
249df930be7Sderaadt bool		dfn_busy();
250d2559c65Smickey void		dfn_findcycle();
251d2559c65Smickey void		dfn_init();
252df930be7Sderaadt bool		dfn_numbered();
253d2559c65Smickey void		dfn_post_visit();
254d2559c65Smickey void		dfn_pre_visit();
255d2559c65Smickey void		dfn_self_cycle();
256df930be7Sderaadt nltype		**doarcs();
257c72b5b24Smillert void		doflags(void);
258c72b5b24Smillert void		dotime(void);
259d2559c65Smickey void		dumpsum();
260c72b5b24Smillert void		findcall(nltype *, u_long, u_long);
261d2559c65Smickey void		flatprofheader();
262d2559c65Smickey void		flatprofline();
263*90a99070Sart int		getnfile(const char *, char ***);
264d2559c65Smickey void		getpfile();
265d2559c65Smickey void		gprofheader();
266d2559c65Smickey void		gprofline();
267d2559c65Smickey int		hertz();
268c72b5b24Smillert void		inheritflags(nltype *childp);
269df930be7Sderaadt unsigned long	max();
270df930be7Sderaadt int		membercmp();
271df930be7Sderaadt unsigned long	min();
272df930be7Sderaadt nltype		*nllookup();
273d2559c65Smickey bool		onlist();
274df930be7Sderaadt FILE		*openpfile();
275df930be7Sderaadt long		operandlength();
276df930be7Sderaadt operandenum	operandmode();
277df930be7Sderaadt char		*operandname();
278d2559c65Smickey void		printchildren();
279d2559c65Smickey void		printcycle();
280d2559c65Smickey void		printgprof();
281d2559c65Smickey void		printindex();
282d2559c65Smickey void		printmembers();
283d2559c65Smickey void		printname();
284d2559c65Smickey void		printparents();
285d2559c65Smickey void		printprof();
286d2559c65Smickey void		readsamples(FILE *);
287df930be7Sderaadt unsigned long	reladdr();
288d2559c65Smickey void		sortchildren();
289d2559c65Smickey void		sortmembers();
290d2559c65Smickey void		sortparents();
291d2559c65Smickey void		tally();
292d2559c65Smickey int		timecmp();
293c72b5b24Smillert void		timepropagate(nltype *);
294d2559c65Smickey int		topcmp();
295df930be7Sderaadt int		totalcmp();
296df930be7Sderaadt 
297df930be7Sderaadt #define	LESSTHAN	-1
298df930be7Sderaadt #define	EQUALTO		0
299df930be7Sderaadt #define	GREATERTHAN	1
300df930be7Sderaadt 
301df930be7Sderaadt #define	DFNDEBUG	1
302df930be7Sderaadt #define	CYCLEDEBUG	2
303df930be7Sderaadt #define	ARCDEBUG	4
304df930be7Sderaadt #define	TALLYDEBUG	8
305df930be7Sderaadt #define	TIMEDEBUG	16
306df930be7Sderaadt #define	SAMPLEDEBUG	32
307df930be7Sderaadt #define	AOUTDEBUG	64
308df930be7Sderaadt #define	CALLDEBUG	128
309df930be7Sderaadt #define	LOOKUPDEBUG	256
310df930be7Sderaadt #define	PROPDEBUG	512
311df930be7Sderaadt #define	BREAKCYCLE	1024
312df930be7Sderaadt #define	SUBCYCLELIST	2048
313df930be7Sderaadt #define	ANYDEBUG	4096
314