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