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