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