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