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