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
45enum opermodes { dummy };
46typedef enum opermodes	operandenum;
47
48    /*
49     * booleans
50     */
51typedef 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     */
69EXTERN long	hz;
70
71EXTERN size_t	histcounter_size;
72EXTERN int	histcounter_type;
73
74EXTERN char	*a_outname;
75#define	A_OUTNAME		"a.out"
76
77EXTERN 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     */
87struct 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};
99typedef 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     */
112struct 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};
135typedef struct nl	nltype;
136
137EXTERN nltype	*nl;			/* the whole namelist */
138EXTERN nltype	*npe;			/* the virtual end of the namelist */
139EXTERN 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     */
150struct 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};
156typedef struct cl cltype;
157
158EXTERN arctype	*archead;	/* the head of arcs in current cycle list */
159EXTERN cltype	*cyclehead;	/* the head of the list */
160EXTERN 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     */
174EXTERN nltype	*cyclenl;		/* cycle header namelist */
175EXTERN 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     */
185struct ophdr {
186    u_short	*lpc;
187    u_short	*hpc;
188    int		ncnt;
189};
190
191EXTERN int	debug;
192
193    /*
194     * Each discretized pc sample has
195     * a count of the number of samples in its range
196     */
197EXTERN double	*samples;
198
199EXTERN unsigned long	s_lowpc;	/* lowpc from the profile file */
200EXTERN unsigned long	s_highpc;	/* highpc from the profile file */
201/* range profiled, in historical units  */
202EXTERN unsigned long	lowpc, highpc;
203EXTERN unsigned sampbytes;		/* number of bytes of samples */
204EXTERN int	nsamples;		/* number of samples */
205/* accumulated time thus far for putprofline */
206EXTERN double	actime;
207EXTERN double	totime;			/* total time for all routines */
208EXTERN double	printtime;		/* total of time being printed */
209EXTERN double	scale;			/* scale factor converting samples to pc
210				   values: each sample covers scale bytes */
211EXTERN unsigned char	*textspace;	/* text space of a.out in core */
212/* with -C, minimum cycle size to ignore */
213EXTERN int	cyclethreshold;
214
215    /*
216     *	option flags, from a to z.
217     */
218EXTERN bool	aflag;			/* suppress static functions */
219EXTERN bool	bflag;			/* blurbs, too */
220EXTERN bool	Cflag;			/* find cut-set to eliminate cycles */
221EXTERN bool	dflag;			/* debugging options */
222EXTERN bool	eflag;			/* specific functions excluded */
223EXTERN bool	Eflag;			/* functions excluded with time */
224EXTERN bool	fflag;			/* specific functions requested */
225EXTERN bool	Fflag;			/* functions requested with time */
226EXTERN bool	kflag;			/* arcs to be deleted */
227EXTERN bool	Kflag;			/* use the running kernel for symbols */
228EXTERN bool	sflag;			/* sum multiple gmon.out files */
229EXTERN bool	uflag;			/* suppress symbols hidden from C */
230EXTERN bool	zflag;			/* zero time/called functions, too */
231
232    /*
233     *	structure for various string lists
234     */
235struct stringlist {
236    struct stringlist	*next;
237    char		*string;
238};
239extern struct stringlist	*elist;
240extern struct stringlist	*Elist;
241extern struct stringlist	*flist;
242extern struct stringlist	*Flist;
243extern struct stringlist	*kfromlist;
244extern struct stringlist	*ktolist;
245
246    /*
247     *	function declarations
248     */
249void		addarc(nltype *, nltype *, long);
250bool		addcycle(arctype **, arctype **);
251void		addlist(struct stringlist *, char *);
252void		alignentries(void);
253int		arccmp(arctype *, arctype *);
254arctype		*arclookup(nltype *, nltype *);
255void		asgnsamples(void);
256void		compresslist(void);
257bool		cycleanalyze(void);
258void		cyclelink(void);
259void		cycletime(void);
260bool		descend(nltype *, arctype **, arctype **);
261void		dfn(nltype *);
262bool		dfn_busy(nltype *);
263void		dfn_findcycle(nltype *);
264void		dfn_init(void);
265bool		dfn_numbered(nltype *);
266void		dfn_post_visit(nltype *);
267void		dfn_pre_visit(nltype *);
268void		dfn_self_cycle(nltype *);
269nltype		**doarcs(void);
270void		doflags(void);
271void		dotime(void);
272void		dumpsum(const char *);
273int		elf_getnfile(const char *, char ***);
274void		flatprofheader(void);
275void		flatprofline(nltype *);
276void		getpfile(char *);
277void		gprofheader(void);
278void		gprofline(register nltype *);
279int		hertz(void);
280void		inheritflags(nltype *);
281int		kernel_getnfile(const char *, char ***);
282/*
283		main();
284*/
285unsigned long	max(unsigned long, unsigned long);
286int		membercmp(nltype *, nltype *);
287unsigned long	min(unsigned long, unsigned long);
288nltype		*nllookup(unsigned long);
289bool		onlist(struct stringlist *, const char *);
290FILE		*openpfile(char *);
291void		printblurb(const char *);
292void		printchildren(nltype *);
293void		printcycle(nltype *);
294void		printgprof(nltype **);
295void		printindex(void);
296void		printmembers(nltype *);
297void		printname(nltype *);
298void		printparents(nltype *);
299void		printprof(void);
300void		printsubcycle(cltype *);
301void		readsamples(FILE *);
302void		sortchildren(nltype *);
303void		sortmembers(nltype *);
304void		sortparents(nltype *);
305void		tally(struct rawarc *);
306void		timepropagate(nltype *);
307int		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