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