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