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