xref: /freebsd/usr.bin/gprof/gprof.h (revision 2357939bc239bd5334a169b62313806178dd8f30)
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  * $FreeBSD$
35  */
36 
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <sys/gmon.h>
40 
41 #include <stdio.h>
42 #include <stdlib.h>
43 
44 #if __ia64__
45 #   include "ia64.h"
46 #endif
47 #if __alpha__
48 #   include "alpha.h"
49 #endif
50 #if __sparc64__
51 #   include "sparc64.h"
52 #endif
53 #if __powerpc__
54 #   include "powerpc.h"
55 #endif
56 #if __i386__
57 #   include "i386.h"
58 #endif
59 
60     /*
61      * booleans
62      */
63 typedef int	bool;
64 #define	FALSE	0
65 #define	TRUE	1
66 
67     /*
68      *	Historical scale factor in profil(2)'s algorithm for converting
69      *	pc addresses to bucket numbers.  This now just complicates the
70      *	scaling and makes bucket:pc densities of more than 1/2 useless.
71      */
72 #define	HISTORICAL_SCALE_2	2
73 
74     /*
75      *	ticks per second
76      */
77 long	hz;
78 
79 size_t	histcounter_size;
80 int	histcounter_type;
81 
82 char	*a_outname;
83 #define	A_OUTNAME		"a.out"
84 
85 char	*gmonname;
86 #define	GMONSUM			"gmon.sum"
87 
88     /*
89      *	a constructed arc,
90      *	    with pointers to the namelist entry of the parent and the child,
91      *	    a count of how many times this arc was traversed,
92      *	    and pointers to the next parent of this child and
93      *		the next child of this parent.
94      */
95 struct arcstruct {
96     struct nl		*arc_parentp;	/* pointer to parent's nl entry */
97     struct nl		*arc_childp;	/* pointer to child's nl entry */
98     long		arc_count;	/* num calls from parent to child */
99     double		arc_time;	/* time inherited along arc */
100     double		arc_childtime;	/* childtime inherited along arc */
101     struct arcstruct	*arc_parentlist; /* parents-of-this-child list */
102     struct arcstruct	*arc_childlist;	/* children-of-this-parent list */
103     struct arcstruct	*arc_next;	/* list of arcs on cycle */
104     unsigned short	arc_cyclecnt;	/* num cycles involved in */
105     unsigned short	arc_flags;	/* see below */
106 };
107 typedef struct arcstruct	arctype;
108 
109     /*
110      * arc flags
111      */
112 #define	DEADARC	0x01	/* time should not propagate across the arc */
113 #define	ONLIST	0x02	/* arc is on list of arcs in cycles */
114 
115     /*
116      * The symbol table;
117      * for each external in the specified file we gather
118      * its address, the number of calls and compute its share of CPU time.
119      */
120 struct nl {
121     const char		*name;		/* the name */
122     unsigned long	value;		/* the pc entry point */
123     unsigned long	svalue;		/* entry point aligned to histograms */
124     double		time;		/* ticks in this routine */
125     double		childtime;	/* cumulative ticks in children */
126     long		ncall;		/* how many times called */
127     long		npropcall;	/* times called by live arcs */
128     long		selfcalls;	/* how many calls to self */
129     double		propfraction;	/* what % of time propagates */
130     double		propself;	/* how much self time propagates */
131     double		propchild;	/* how much child time propagates */
132     short		printflag;	/* should this be printed? */
133     short		flags;		/* see below */
134     int			index;		/* index in the graph list */
135     int			toporder;	/* graph call chain top-sort order */
136     int			cycleno;	/* internal number of cycle on */
137     int			parentcnt;	/* number of live parent arcs */
138     struct nl		*cyclehead;	/* pointer to head of cycle */
139     struct nl		*cnext;		/* pointer to next member of cycle */
140     arctype		*parents;	/* list of caller arcs */
141     arctype		*children;	/* list of callee arcs */
142 };
143 typedef struct nl	nltype;
144 
145 nltype	*nl;			/* the whole namelist */
146 nltype	*npe;			/* the virtual end of the namelist */
147 int	nname;			/* the number of function names */
148 
149 #define	HASCYCLEXIT	0x08	/* node has arc exiting from cycle */
150 #define	CYCLEHEAD	0x10	/* node marked as head of a cycle */
151 #define	VISITED		0x20	/* node visited during a cycle */
152 
153     /*
154      * The cycle list.
155      * for each subcycle within an identified cycle, we gather
156      * its size and the list of included arcs.
157      */
158 struct cl {
159     int		size;		/* length of cycle */
160     struct cl	*next;		/* next member of list */
161     arctype	*list[1];	/* list of arcs in cycle */
162     /* actually longer */
163 };
164 typedef struct cl cltype;
165 
166 arctype	*archead;		/* the head of arcs in current cycle list */
167 cltype	*cyclehead;		/* the head of the list */
168 int	cyclecnt;		/* the number of cycles found */
169 #define	CYCLEMAX	100	/* maximum cycles before cutting one of them */
170 
171     /*
172      *	flag which marks a nl entry as topologically ``busy''
173      *	flag which marks a nl entry as topologically ``not_numbered''
174      */
175 #define	DFN_BUSY	-1
176 #define	DFN_NAN		0
177 
178     /*
179      *	namelist entries for cycle headers.
180      *	the number of discovered cycles.
181      */
182 nltype	*cyclenl;		/* cycle header namelist */
183 int	ncycle;			/* number of cycles discovered */
184 
185     /*
186      * The header on the gmon.out file.
187      * gmon.out consists of a struct phdr (defined in gmon.h)
188      * and then an array of ncnt samples representing the
189      * discretized program counter values.
190      *
191      *	Backward compatible old style header
192      */
193 struct ophdr {
194     u_short	*lpc;
195     u_short	*hpc;
196     int		ncnt;
197 };
198 
199 int	debug;
200 
201     /*
202      * Each discretized pc sample has
203      * a count of the number of samples in its range
204      */
205 double	*samples;
206 
207 unsigned long	s_lowpc;	/* lowpc from the profile file */
208 unsigned long	s_highpc;	/* highpc from the profile file */
209 unsigned long	lowpc, highpc;	/* range profiled, in historical units  */
210 unsigned sampbytes;		/* number of bytes of samples */
211 int	nsamples;		/* number of samples */
212 double	actime;			/* accumulated time thus far for putprofline */
213 double	totime;			/* total time for all routines */
214 double	printtime;		/* total of time being printed */
215 double	scale;			/* scale factor converting samples to pc
216 				   values: each sample covers scale bytes */
217 unsigned char	*textspace;	/* text space of a.out in core */
218 int	cyclethreshold;		/* with -C, minimum cycle size to ignore */
219 
220     /*
221      *	option flags, from a to z.
222      */
223 bool	aflag;				/* suppress static functions */
224 bool	bflag;				/* blurbs, too */
225 bool	cflag;				/* discovered call graph, too */
226 bool	Cflag;				/* find cut-set to eliminate cycles */
227 bool	dflag;				/* debugging options */
228 bool	eflag;				/* specific functions excluded */
229 bool	Eflag;				/* functions excluded with time */
230 bool	fflag;				/* specific functions requested */
231 bool	Fflag;				/* functions requested with time */
232 bool	kflag;				/* arcs to be deleted */
233 bool	Kflag;				/* use the running kernel for symbols */
234 bool	sflag;				/* sum multiple gmon.out files */
235 bool	uflag;				/* suppress symbols hidden from C */
236 bool	zflag;				/* zero time/called functions, too */
237 
238     /*
239      *	structure for various string lists
240      */
241 struct stringlist {
242     struct stringlist	*next;
243     char		*string;
244 };
245 struct stringlist	*elist;
246 struct stringlist	*Elist;
247 struct stringlist	*flist;
248 struct stringlist	*Flist;
249 struct stringlist	*kfromlist;
250 struct stringlist	*ktolist;
251 
252     /*
253      *	function declarations
254      */
255 void		addarc(nltype *, nltype *, long);
256 bool		addcycle(arctype **, arctype **);
257 void		addlist(struct stringlist *, char *);
258 void		alignentries(void);
259 int		aout_getnfile(const char *, char ***);
260 int		arccmp();
261 arctype		*arclookup();
262 void		asgnsamples(void);
263 void		compresslist(void);
264 bool		cycleanalyze(void);
265 void		cyclelink(void);
266 void		cycletime(void);
267 bool		descend(nltype *, arctype **, arctype **);
268 void		dfn(nltype *);
269 bool		dfn_busy();
270 void		dfn_findcycle(nltype *);
271 void		dfn_init(void);
272 bool		dfn_numbered();
273 void		dfn_post_visit(nltype *);
274 void		dfn_pre_visit(nltype *);
275 void		dfn_self_cycle(nltype *);
276 nltype		**doarcs();
277 void		doflags(void);
278 void		dotime(void);
279 void		dumpsum(char *);
280 int		elf_getnfile(const char *, char ***);
281 /*
282 		findcalls();
283 */
284 void		flatprofheader(void);
285 void		flatprofline(nltype *);
286 void		getpfile(char *);
287 /*
288 		gprofheader();
289 		gprofline();
290 */
291 void		inheritflags(nltype *);
292 int		kernel_getnfile(const char *, char ***);
293 /*
294 		main();
295 */
296 unsigned long	max();
297 int		membercmp();
298 unsigned long	min();
299 nltype		*nllookup();
300 bool		onlist(struct stringlist *, const char *);
301 FILE		*openpfile();
302 long		operandlength();
303 operandenum	operandmode();
304 char		*operandname();
305 void		printblurb(char *);
306 void		printchildren(nltype *);
307 void		printcycle(nltype *);
308 void		printgprof(nltype **);
309 void		printindex(void);
310 void		printmembers(nltype *);
311 void		printname(nltype *);
312 void		printparents(nltype *);
313 void		printprof(void);
314 void		printsubcycle(cltype *);
315 void		readsamples(FILE *);
316 unsigned long	reladdr();
317 void		sortchildren(nltype *);
318 void		sortmembers(nltype *);
319 void		sortparents(nltype *);
320 void		tally(struct rawarc *);
321 void		timepropagate(nltype *);
322 int		totalcmp();
323 
324 #define	LESSTHAN	-1
325 #define	EQUALTO		0
326 #define	GREATERTHAN	1
327 
328 #define	DFNDEBUG	1
329 #define	CYCLEDEBUG	2
330 #define	ARCDEBUG	4
331 #define	TALLYDEBUG	8
332 #define	TIMEDEBUG	16
333 #define	SAMPLEDEBUG	32
334 #define	AOUTDEBUG	64
335 #define	CALLDEBUG	128
336 #define	LOOKUPDEBUG	256
337 #define	PROPDEBUG	512
338 #define	BREAKCYCLE	1024
339 #define	SUBCYCLELIST	2048
340 #define	ANYDEBUG	4096
341