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