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