xref: /freebsd/usr.bin/gprof/gprof.h (revision e17f5b1d)
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 #if __amd64__
43 #   include "amd64.h"
44 #endif
45 #if __arm__
46 #   include "arm.h"
47 #endif
48 #if __i386__
49 #   include "i386.h"
50 #endif
51 #if __mips__
52 #   include "mips.h"
53 #endif
54 #if __powerpc__
55 #   include "powerpc.h"
56 #endif
57 #if __sparc64__
58 #   include "sparc64.h"
59 #endif
60 #if __riscv
61 #    include "riscv.h"
62 #endif
63 
64     /*
65      * booleans
66      */
67 typedef int	bool;
68 #define	FALSE	0
69 #define	TRUE	1
70 
71     /*
72      *	Historical scale factor in profil(2)'s algorithm for converting
73      *	pc addresses to bucket numbers.  This now just complicates the
74      *	scaling and makes bucket:pc densities of more than 1/2 useless.
75      */
76 #define	HISTORICAL_SCALE_2	2
77 
78 #ifndef EXTERN
79 #define	EXTERN	extern
80 #endif
81 
82     /*
83      *	ticks per second
84      */
85 EXTERN long	hz;
86 
87 EXTERN size_t	histcounter_size;
88 EXTERN int	histcounter_type;
89 
90 EXTERN char	*a_outname;
91 #define	A_OUTNAME		"a.out"
92 
93 EXTERN 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 EXTERN nltype	*nl;			/* the whole namelist */
154 EXTERN nltype	*npe;			/* the virtual end of the namelist */
155 EXTERN 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 EXTERN arctype	*archead;	/* the head of arcs in current cycle list */
175 EXTERN cltype	*cyclehead;	/* the head of the list */
176 EXTERN 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 EXTERN nltype	*cyclenl;		/* cycle header namelist */
191 EXTERN 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     u_short	*lpc;
203     u_short	*hpc;
204     int		ncnt;
205 };
206 
207 EXTERN int	debug;
208 
209     /*
210      * Each discretized pc sample has
211      * a count of the number of samples in its range
212      */
213 EXTERN double	*samples;
214 
215 EXTERN unsigned long	s_lowpc;	/* lowpc from the profile file */
216 EXTERN unsigned long	s_highpc;	/* highpc from the profile file */
217 /* range profiled, in historical units  */
218 EXTERN unsigned long	lowpc, highpc;
219 EXTERN unsigned sampbytes;		/* number of bytes of samples */
220 EXTERN int	nsamples;		/* number of samples */
221 /* accumulated time thus far for putprofline */
222 EXTERN double	actime;
223 EXTERN double	totime;			/* total time for all routines */
224 EXTERN double	printtime;		/* total of time being printed */
225 EXTERN double	scale;			/* scale factor converting samples to pc
226 				   values: each sample covers scale bytes */
227 EXTERN unsigned char	*textspace;	/* text space of a.out in core */
228 /* with -C, minimum cycle size to ignore */
229 EXTERN int	cyclethreshold;
230 
231     /*
232      *	option flags, from a to z.
233      */
234 EXTERN bool	aflag;			/* suppress static functions */
235 EXTERN bool	bflag;			/* blurbs, too */
236 EXTERN bool	Cflag;			/* find cut-set to eliminate cycles */
237 EXTERN bool	dflag;			/* debugging options */
238 EXTERN bool	eflag;			/* specific functions excluded */
239 EXTERN bool	Eflag;			/* functions excluded with time */
240 EXTERN bool	fflag;			/* specific functions requested */
241 EXTERN bool	Fflag;			/* functions requested with time */
242 EXTERN bool	kflag;			/* arcs to be deleted */
243 EXTERN bool	Kflag;			/* use the running kernel for symbols */
244 EXTERN bool	sflag;			/* sum multiple gmon.out files */
245 EXTERN bool	uflag;			/* suppress symbols hidden from C */
246 EXTERN bool	zflag;			/* zero time/called functions, too */
247 
248     /*
249      *	structure for various string lists
250      */
251 struct stringlist {
252     struct stringlist	*next;
253     char		*string;
254 };
255 extern struct stringlist	*elist;
256 extern struct stringlist	*Elist;
257 extern struct stringlist	*flist;
258 extern struct stringlist	*Flist;
259 extern struct stringlist	*kfromlist;
260 extern struct stringlist	*ktolist;
261 
262     /*
263      *	function declarations
264      */
265 void		addarc(nltype *, nltype *, long);
266 bool		addcycle(arctype **, arctype **);
267 void		addlist(struct stringlist *, char *);
268 void		alignentries(void);
269 #ifdef WITH_AOUT
270 int		aout_getnfile(const char *, char ***);
271 #endif
272 int		arccmp(arctype *, arctype *);
273 arctype		*arclookup(nltype *, nltype *);
274 void		asgnsamples(void);
275 void		compresslist(void);
276 bool		cycleanalyze(void);
277 void		cyclelink(void);
278 void		cycletime(void);
279 bool		descend(nltype *, arctype **, arctype **);
280 void		dfn(nltype *);
281 bool		dfn_busy(nltype *);
282 void		dfn_findcycle(nltype *);
283 void		dfn_init(void);
284 bool		dfn_numbered(nltype *);
285 void		dfn_post_visit(nltype *);
286 void		dfn_pre_visit(nltype *);
287 void		dfn_self_cycle(nltype *);
288 nltype		**doarcs(void);
289 void		doflags(void);
290 void		dotime(void);
291 void		dumpsum(const char *);
292 int		elf_getnfile(const char *, char ***);
293 void		flatprofheader(void);
294 void		flatprofline(nltype *);
295 void		getpfile(char *);
296 void		gprofheader(void);
297 void		gprofline(register nltype *);
298 int		hertz(void);
299 void		inheritflags(nltype *);
300 int		kernel_getnfile(const char *, char ***);
301 /*
302 		main();
303 */
304 unsigned long	max(unsigned long, unsigned long);
305 int		membercmp(nltype *, nltype *);
306 unsigned long	min(unsigned long, unsigned long);
307 nltype		*nllookup(unsigned long);
308 bool		onlist(struct stringlist *, const char *);
309 FILE		*openpfile(char *);
310 void		printblurb(const char *);
311 void		printchildren(nltype *);
312 void		printcycle(nltype *);
313 void		printgprof(nltype **);
314 void		printindex(void);
315 void		printmembers(nltype *);
316 void		printname(nltype *);
317 void		printparents(nltype *);
318 void		printprof(void);
319 void		printsubcycle(cltype *);
320 void		readsamples(FILE *);
321 void		sortchildren(nltype *);
322 void		sortmembers(nltype *);
323 void		sortparents(nltype *);
324 void		tally(struct rawarc *);
325 void		timepropagate(nltype *);
326 int		totalcmp(const void *, const void *);
327 
328 #define	LESSTHAN	-1
329 #define	EQUALTO		0
330 #define	GREATERTHAN	1
331 
332 #define	DFNDEBUG	1
333 #define	CYCLEDEBUG	2
334 #define	ARCDEBUG	4
335 #define	TALLYDEBUG	8
336 #define	TIMEDEBUG	16
337 #define	SAMPLEDEBUG	32
338 #define	AOUTDEBUG	64
339 #define	CALLDEBUG	128
340 #define	LOOKUPDEBUG	256
341 #define	PROPDEBUG	512
342 #define	BREAKCYCLE	1024
343 #define	SUBCYCLELIST	2048
344 #define	ANYDEBUG	4096
345