xref: /openbsd/usr.bin/gprof/gprof.h (revision 5d07573c)
1*5d07573cSderaadt /*	$OpenBSD: gprof.h,v 1.17 2021/01/27 07:18:41 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>
4377620a5cSpascal #include <unistd.h>
44df930be7Sderaadt 
4573e984b5Sespie #include MD_INCLUDE
46df930be7Sderaadt 
47df930be7Sderaadt     /*
48df930be7Sderaadt      * booleans
49df930be7Sderaadt      */
50df930be7Sderaadt typedef int	bool;
51df930be7Sderaadt #define	FALSE	0
52df930be7Sderaadt #define	TRUE	1
53df930be7Sderaadt 
54df930be7Sderaadt     /*
55df930be7Sderaadt      *	ticks per second
56df930be7Sderaadt      */
57*5d07573cSderaadt extern long	hz;
58df930be7Sderaadt 
59df930be7Sderaadt typedef	u_short UNIT;		/* unit of profiling */
60*5d07573cSderaadt extern char	*a_outname;
61df930be7Sderaadt #define	A_OUTNAME		"a.out"
62df930be7Sderaadt 
63*5d07573cSderaadt extern char	*gmonname;
64df930be7Sderaadt #define	GMONNAME		"gmon.out"
65df930be7Sderaadt #define	GMONSUM			"gmon.sum"
66df930be7Sderaadt 
67df930be7Sderaadt     /*
68df930be7Sderaadt      *	a constructed arc,
69df930be7Sderaadt      *	    with pointers to the namelist entry of the parent and the child,
70df930be7Sderaadt      *	    a count of how many times this arc was traversed,
71df930be7Sderaadt      *	    and pointers to the next parent of this child and
72df930be7Sderaadt      *		the next child of this parent.
73df930be7Sderaadt      */
74df930be7Sderaadt struct arcstruct {
75df930be7Sderaadt     struct nl		*arc_parentp;	/* pointer to parent's nl entry */
76df930be7Sderaadt     struct nl		*arc_childp;	/* pointer to child's nl entry */
77df930be7Sderaadt     long		arc_count;	/* num calls from parent to child */
78df930be7Sderaadt     double		arc_time;	/* time inherited along arc */
79df930be7Sderaadt     double		arc_childtime;	/* childtime inherited along arc */
80df930be7Sderaadt     struct arcstruct	*arc_parentlist; /* parents-of-this-child list */
81df930be7Sderaadt     struct arcstruct	*arc_childlist;	/* children-of-this-parent list */
82df930be7Sderaadt     struct arcstruct	*arc_next;	/* list of arcs on cycle */
83df930be7Sderaadt     unsigned short	arc_cyclecnt;	/* num cycles involved in */
84df930be7Sderaadt     unsigned short	arc_flags;	/* see below */
85df930be7Sderaadt };
86df930be7Sderaadt typedef struct arcstruct	arctype;
87df930be7Sderaadt 
88df930be7Sderaadt     /*
89df930be7Sderaadt      * arc flags
90df930be7Sderaadt      */
91df930be7Sderaadt #define	DEADARC	0x01	/* time should not propagate across the arc */
92df930be7Sderaadt #define	ONLIST	0x02	/* arc is on list of arcs in cycles */
93df930be7Sderaadt 
94df930be7Sderaadt     /*
95df930be7Sderaadt      * The symbol table;
96df930be7Sderaadt      * for each external in the specified file we gather
97df930be7Sderaadt      * its address, the number of calls and compute its share of cpu time.
98df930be7Sderaadt      */
99df930be7Sderaadt struct nl {
10090a99070Sart     const char		*name;		/* the name */
101df930be7Sderaadt     unsigned long	value;		/* the pc entry point */
102df930be7Sderaadt     unsigned long	svalue;		/* entry point aligned to histograms */
103df930be7Sderaadt     double		time;		/* ticks in this routine */
104df930be7Sderaadt     double		childtime;	/* cumulative ticks in children */
105df930be7Sderaadt     long		ncall;		/* how many times called */
106df930be7Sderaadt     long		npropcall;	/* times called by live arcs */
107df930be7Sderaadt     long		selfcalls;	/* how many calls to self */
108df930be7Sderaadt     double		propfraction;	/* what % of time propagates */
109df930be7Sderaadt     double		propself;	/* how much self time propagates */
110df930be7Sderaadt     double		propchild;	/* how much child time propagates */
111df930be7Sderaadt     short		printflag;	/* should this be printed? */
112df930be7Sderaadt     short		flags;		/* see below */
113df930be7Sderaadt     int			index;		/* index in the graph list */
114df930be7Sderaadt     int			toporder;	/* graph call chain top-sort order */
115df930be7Sderaadt     int			cycleno;	/* internal number of cycle on */
116df930be7Sderaadt     int			parentcnt;	/* number of live parent arcs */
117df930be7Sderaadt     struct nl		*cyclehead;	/* pointer to head of cycle */
118df930be7Sderaadt     struct nl		*cnext;		/* pointer to next member of cycle */
119df930be7Sderaadt     arctype		*parents;	/* list of caller arcs */
120df930be7Sderaadt     arctype		*children;	/* list of callee arcs */
121df930be7Sderaadt };
122df930be7Sderaadt typedef struct nl	nltype;
123df930be7Sderaadt 
124*5d07573cSderaadt extern nltype	*nl;			/* the whole namelist */
125*5d07573cSderaadt extern nltype	*npe;			/* the virtual end of the namelist */
126*5d07573cSderaadt extern int	nname;			/* the number of function names */
127df930be7Sderaadt 
128df930be7Sderaadt #define	HASCYCLEXIT	0x08	/* node has arc exiting from cycle */
129df930be7Sderaadt #define	CYCLEHEAD	0x10	/* node marked as head of a cycle */
130df930be7Sderaadt #define	VISITED		0x20	/* node visited during a cycle */
131df930be7Sderaadt 
132df930be7Sderaadt     /*
133df930be7Sderaadt      * The cycle list.
134df930be7Sderaadt      * for each subcycle within an identified cycle, we gather
135df930be7Sderaadt      * its size and the list of included arcs.
136df930be7Sderaadt      */
137df930be7Sderaadt struct cl {
138df930be7Sderaadt     int		size;		/* length of cycle */
139df930be7Sderaadt     struct cl	*next;		/* next member of list */
140df930be7Sderaadt     arctype	*list[1];	/* list of arcs in cycle */
141df930be7Sderaadt     /* actually longer */
142df930be7Sderaadt };
143df930be7Sderaadt typedef struct cl cltype;
144df930be7Sderaadt 
145*5d07573cSderaadt extern arctype	*archead;		/* the head of arcs in current cycle list */
146*5d07573cSderaadt extern cltype	*cyclehead;		/* the head of the list */
147*5d07573cSderaadt extern int	cyclecnt;		/* the number of cycles found */
148df930be7Sderaadt #define	CYCLEMAX	100	/* maximum cycles before cutting one of them */
149df930be7Sderaadt 
150df930be7Sderaadt     /*
151df930be7Sderaadt      *	flag which marks a nl entry as topologically ``busy''
152df930be7Sderaadt      *	flag which marks a nl entry as topologically ``not_numbered''
153df930be7Sderaadt      */
154df930be7Sderaadt #define	DFN_BUSY	-1
155df930be7Sderaadt #define	DFN_NAN		0
156df930be7Sderaadt 
157df930be7Sderaadt     /*
158df930be7Sderaadt      *	namelist entries for cycle headers.
159df930be7Sderaadt      *	the number of discovered cycles.
160df930be7Sderaadt      */
161*5d07573cSderaadt extern nltype	*cyclenl;		/* cycle header namelist */
162*5d07573cSderaadt extern int	ncycle;			/* number of cycles discovered */
163df930be7Sderaadt 
164df930be7Sderaadt     /*
165df930be7Sderaadt      * The header on the gmon.out file.
166df930be7Sderaadt      * gmon.out consists of a struct phdr (defined in gmon.h)
167df930be7Sderaadt      * and then an array of ncnt samples representing the
168df930be7Sderaadt      * discretized program counter values.
169df930be7Sderaadt      *
170df930be7Sderaadt      *	Backward compatible old style header
171df930be7Sderaadt      */
172df930be7Sderaadt struct ophdr {
173df930be7Sderaadt     UNIT	*lpc;
174df930be7Sderaadt     UNIT	*hpc;
175df930be7Sderaadt     int		ncnt;
176df930be7Sderaadt };
177df930be7Sderaadt 
178*5d07573cSderaadt extern int	debug;
179df930be7Sderaadt 
180df930be7Sderaadt     /*
181df930be7Sderaadt      * Each discretized pc sample has
182df930be7Sderaadt      * a count of the number of samples in its range
183df930be7Sderaadt      */
184*5d07573cSderaadt extern UNIT	*samples;
185df930be7Sderaadt 
186*5d07573cSderaadt extern unsigned long	s_lowpc;	/* lowpc from the profile file */
187*5d07573cSderaadt extern unsigned long	s_highpc;	/* highpc from the profile file */
188*5d07573cSderaadt extern unsigned long	lowpc, highpc;	/* range profiled, in UNIT's */
189*5d07573cSderaadt extern unsigned sampbytes;		/* number of bytes of samples */
190*5d07573cSderaadt extern int	nsamples;		/* number of samples */
191*5d07573cSderaadt extern double	actime;			/* accumulated time thus far for putprofline */
192*5d07573cSderaadt extern double	totime;			/* total time for all routines */
193*5d07573cSderaadt extern double	printtime;		/* total of time being printed */
194*5d07573cSderaadt extern double	scale;			/* scale factor converting samples to pc
195df930be7Sderaadt 				   values: each sample covers scale bytes */
196*5d07573cSderaadt extern unsigned char	*textspace;	/* text space of a.out in core */
197*5d07573cSderaadt extern int	cyclethreshold;		/* with -C, minimum cycle size to ignore */
198df930be7Sderaadt 
199df930be7Sderaadt     /*
200df930be7Sderaadt      *	option flags, from a to z.
201df930be7Sderaadt      */
202*5d07573cSderaadt extern bool	aflag;				/* suppress static functions */
203*5d07573cSderaadt extern bool	bflag;				/* blurbs, too */
204*5d07573cSderaadt extern bool	cflag;				/* discovered call graph, too */
205*5d07573cSderaadt extern bool	Cflag;				/* find cut-set to eliminate cycles */
206*5d07573cSderaadt extern bool	dflag;				/* debugging options */
207*5d07573cSderaadt extern bool	eflag;				/* specific functions excluded */
208*5d07573cSderaadt extern bool	Eflag;				/* functions excluded with time */
209*5d07573cSderaadt extern bool	fflag;				/* specific functions requested */
210*5d07573cSderaadt extern bool	Fflag;				/* functions requested with time */
211*5d07573cSderaadt extern bool	kflag;				/* arcs to be deleted */
212*5d07573cSderaadt extern bool	sflag;				/* sum multiple gmon.out files */
213*5d07573cSderaadt extern bool	zflag;				/* zero time/called functions, too */
214df930be7Sderaadt 
215df930be7Sderaadt     /*
216df930be7Sderaadt      *	structure for various string lists
217df930be7Sderaadt      */
218df930be7Sderaadt struct stringlist {
219df930be7Sderaadt     struct stringlist	*next;
220df930be7Sderaadt     char		*string;
221df930be7Sderaadt };
222*5d07573cSderaadt extern struct stringlist	*elist;
223*5d07573cSderaadt extern struct stringlist	*Elist;
224*5d07573cSderaadt extern struct stringlist	*flist;
225*5d07573cSderaadt extern struct stringlist	*Flist;
226*5d07573cSderaadt extern struct stringlist	*kfromlist;
227*5d07573cSderaadt extern struct stringlist	*ktolist;
228df930be7Sderaadt 
229df930be7Sderaadt     /*
230df930be7Sderaadt      *	function declarations
231df930be7Sderaadt      */
2329bd74cdeSespie void		addarc(nltype *, nltype *, long);
233c72b5b24Smillert int		addcycle(arctype **, arctype **);
2349bd74cdeSespie void		addlist(struct stringlist *, char *);
2359bd74cdeSespie int		arccmp(arctype *, arctype *);
2369bd74cdeSespie arctype		*arclookup(nltype *, nltype *);
2379bd74cdeSespie void		asgnsamples(void);
2389bd74cdeSespie void		alignentries(void);
2399bd74cdeSespie void		printblurb(const char *);
240c72b5b24Smillert int		cycleanalyze(void);
241c72b5b24Smillert void		cyclelink(void);
242c72b5b24Smillert void		cycletime(void);
243c72b5b24Smillert void		compresslist(void);
2449bd74cdeSespie int		descend(nltype *, arctype **, arctype **);
2459bd74cdeSespie void		dfn(nltype *);
2469bd74cdeSespie bool		dfn_busy(nltype *);
2479bd74cdeSespie void		dfn_findcycle(nltype *);
2489bd74cdeSespie void		dfn_init(void);
2499bd74cdeSespie bool		dfn_numbered(nltype *);
2509bd74cdeSespie void		dfn_post_visit(nltype *);
2519bd74cdeSespie void		dfn_pre_visit(nltype *);
2529bd74cdeSespie void		dfn_self_cycle(nltype *);
2539bd74cdeSespie nltype		**doarcs(void);
254c72b5b24Smillert void		doflags(void);
255c72b5b24Smillert void		dotime(void);
2569bd74cdeSespie void		dumpsum(const char *);
2579bd74cdeSespie void		findcall(nltype *, unsigned long, unsigned long);
2589bd74cdeSespie void		flatprofheader(void);
2599bd74cdeSespie void		flatprofline(nltype *);
26090a99070Sart int		getnfile(const char *, char ***);
2619bd74cdeSespie void		getpfile(const char *);
2629bd74cdeSespie void		gprofheader(void);
2639bd74cdeSespie void		gprofline(nltype *);
2649bd74cdeSespie int		hertz(void);
2659bd74cdeSespie void		inheritflags(nltype *);
2669bd74cdeSespie unsigned long	max(unsigned long, unsigned long);
2679bd74cdeSespie int		membercmp(nltype *, nltype *);
2689bd74cdeSespie unsigned long	min(unsigned long, unsigned long);
2699bd74cdeSespie nltype		*nllookup(unsigned long);
2709bd74cdeSespie bool		onlist(struct stringlist *, const char *);
2719bd74cdeSespie FILE		*openpfile(const char *);
2729bd74cdeSespie void		printchildren(nltype *);
2739bd74cdeSespie void		printcycle(nltype *);
2749bd74cdeSespie void		printgprof(nltype **);
2759bd74cdeSespie void		printindex(void);
2769bd74cdeSespie void		printmembers(nltype *);
2779bd74cdeSespie void		printname(nltype *);
2789bd74cdeSespie void		printparents(nltype *);
2799bd74cdeSespie void		printprof(void);
280d2559c65Smickey void		readsamples(FILE *);
2819bd74cdeSespie void		sortchildren(nltype *);
2829bd74cdeSespie void		sortmembers(nltype *);
2839bd74cdeSespie void		sortparents(nltype *);
2849bd74cdeSespie void		tally(struct rawarc *);
28590e0690eSguenther int		timecmp(const void *, const void *);
286c72b5b24Smillert void		timepropagate(nltype *);
28790e0690eSguenther int		topcmp(const void *, const void *);
28890e0690eSguenther int		totalcmp(const void *, const void *);
289df930be7Sderaadt 
290df930be7Sderaadt #define	LESSTHAN	-1
291df930be7Sderaadt #define	EQUALTO		0
292df930be7Sderaadt #define	GREATERTHAN	1
293df930be7Sderaadt 
294df930be7Sderaadt #define	DFNDEBUG	1
295df930be7Sderaadt #define	CYCLEDEBUG	2
296df930be7Sderaadt #define	ARCDEBUG	4
297df930be7Sderaadt #define	TALLYDEBUG	8
298df930be7Sderaadt #define	TIMEDEBUG	16
299df930be7Sderaadt #define	SAMPLEDEBUG	32
300d6966193Sderaadt #define	ELFDEBUG	64
301df930be7Sderaadt #define	CALLDEBUG	128
302df930be7Sderaadt #define	LOOKUPDEBUG	256
303df930be7Sderaadt #define	PROPDEBUG	512
304df930be7Sderaadt #define	BREAKCYCLE	1024
305df930be7Sderaadt #define	SUBCYCLELIST	2048
306df930be7Sderaadt #define	ANYDEBUG	4096
307