xref: /titanic_52/usr/src/cmd/sgs/gprof/common/gprof.h (revision 3f9207dc30b6a3a60ed15183a8c6de02cafe3073)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #ifndef	_SGS_GPROF_H
29 #define	_SGS_GPROF_H
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #ifdef	__cplusplus
34 extern "C" {
35 #endif
36 
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <fcntl.h>
43 #include <sys/mman.h>
44 #include <elf.h>
45 
46 #include "sparc.h"
47 #include "gelf.h"
48 #include "monv.h"
49 #include "sgs.h"
50 
51 
52 /*
53  * who am i, for error messages.
54  */
55 extern char	*whoami;
56 
57 /*
58  * booleans
59  */
60 typedef Boolean	bool;
61 
62 /*
63  * Alignment related constants
64  */
65 #define	PGSZ		4096
66 #define	STRUCT_ALIGN	8
67 
68 /*
69  * Macros related to structure alignment
70  */
71 #define	FLOOR(x, align)	(((Address) x) & ~((align) - 1l))
72 #define	CEIL(x, align)	FLOOR(((Address) x) + (align) - 1l, align)
73 
74 #define	PROFHDR_SZ	(CEIL(sizeof (ProfHeader), STRUCT_ALIGN))
75 #define	PROFMODLIST_SZ	(CEIL(sizeof (ProfModuleList), STRUCT_ALIGN))
76 #define	PROFMOD_SZ	(CEIL(sizeof (ProfModule), STRUCT_ALIGN))
77 #define	PROFBUF_SZ	(CEIL(sizeof (ProfBuffer), STRUCT_ALIGN))
78 #define	PROFCGRAPH_SZ	(CEIL(sizeof (ProfCallGraph), STRUCT_ALIGN))
79 #define	PROFFUNC_SZ	(CEIL(sizeof (ProfFunction), STRUCT_ALIGN))
80 
81 #define	HDR_FILLER	(PROFHDR_SZ - sizeof (ProfHeader))
82 #define	MODLIST_FILLER	(PROFMODLIST_SZ - sizeof (ProfModuleList))
83 #define	MOD_FILLER	(PROFMOD_SZ - sizeof (ProfModule))
84 #define	BUF_FILLER	(PROFBUF_SZ - sizeof (ProfBuffer))
85 #define	CGRAPH_FILLER	(PROFCGRAPH_SZ - sizeof (ProfCallGraph))
86 #define	FUNC_FILLER	(PROFFUNC_SZ - sizeof (ProfFunction))
87 
88 /*
89  *	ticks per second
90  */
91 long	hz;
92 
93 typedef	short UNIT;		/* unit of profiling */
94 typedef unsigned short	unsigned_UNIT; /* to remove warnings from gprof.c */
95 char	*a_outname;
96 char	*prog_name;	/* keep the program name for error messages */
97 #define	A_OUTNAME		"a.out"
98 
99 typedef unsigned long long pctype;
100 typedef uint32_t pctype32;
101 typedef size_t sztype;
102 
103 /*
104  * Type definition for the arc count.
105  */
106 typedef long long actype;
107 typedef int32_t actype32;
108 
109 char	*gmonname;
110 #define	GMONNAME		"gmon.out"
111 #define	GMONSUM			"gmon.sum"
112 
113 /*
114  * Special symbols used for profiling of shared libraries through
115  * the run-time linker.
116  */
117 #define	PRF_ETEXT		"_etext"
118 #define	PRF_EXTSYM		"<external>"
119 #define	PRF_MEMTERM		"_END_OF_VIRTUAL_MEMORY"
120 #define	PRF_SYMCNT		3
121 
122 /*
123  * Special symbol needed to determine the program exec's end addr.
124  * Note that since this symbol doesn't get added to the nameslist,
125  * it doesn't have to be counted in PRF_SYMCNT
126  */
127 #define	PRF_END			"_end"
128 
129 /*
130  *	blurbs on the flat and graph profiles.
131  */
132 #define	FLAT_BLURB	"/gprof.flat.blurb"
133 #define	CALLG_BLURB	"/gprof.callg.blurb"
134 
135 /*
136  *	a raw arc,
137  *	    with pointers to the calling site and the called site
138  *          and a count.
139  */
140 struct rawarc {
141 	pctype		raw_frompc;
142 	pctype		raw_selfpc;
143 	actype		raw_count;
144 };
145 
146 struct rawarc32 {
147 	pctype32	raw_frompc;
148 	pctype32	raw_selfpc;
149 	actype32	raw_count;
150 };
151 
152 /*
153  *	a constructed arc,
154  *	    with pointers to the namelist entry of the parent and the child,
155  *	    a count of how many times this arc was traversed,
156  *	    and pointers to the next parent of this child and
157  *	    the next child of this parent.
158  */
159 struct arcstruct {
160     struct nl		*arc_parentp;	/* pointer to parent's nl entry */
161     struct nl		*arc_childp;	/* pointer to child's nl entry */
162     actype		arc_count;	/* how calls from parent to child */
163     double		arc_time;	/* time inherited along arc */
164     double		arc_childtime;	/* childtime inherited along arc */
165     struct arcstruct	*arc_parentlist; /* parents-of-this-child list */
166     struct arcstruct	*arc_childlist;	/* children-of-this-parent list */
167 };
168 typedef struct arcstruct	arctype;
169 
170 
171 /*
172  * Additions for new-style gmon.out
173  */
174 bool	old_style;			/* gmon.out versioned/non-versioned ? */
175 
176 /*
177  * Executable file info.
178  *
179  * All info that is required to identify a file or see if it has changed
180  * relative to another file.
181  */
182 struct fl_info {
183 	dev_t	dev;			/* device associated with this file */
184 	ino_t	ino;			/* i-number of this file */
185 	time_t	mtime;			/* last modified time of this file */
186 	off_t	size;			/* size of file */
187 };
188 typedef struct fl_info	fl_info_t;
189 
190 /*
191  * Saved file info.
192  */
193 fl_info_t	aout_info;		/* saved file info for program exec */
194 fl_info_t	gmonout_info;		/* current gmonout's info */
195 
196 
197 /*
198  * Module info.
199  */
200 struct mod_info {
201 	struct mod_info	*next;		/* ptr to next in the modules list */
202 	char		*name;		/* name of this module */
203 	int		id;		/* id, used while printing */
204 	bool		active;		/* is this module active or not ? */
205 	struct nl	*nl;		/* ptr to nameslist for this module */
206 	struct nl	*npe;		/* virtual end of module's namelist */
207 	sztype		nname;		/* number of funcs in this module */
208 	GElf_Addr	txt_origin;	/* module's start as given in file */
209 	GElf_Addr	data_end;	/* module's end addr as in file */
210 	Address		load_base;	/* actual pcaddr where modl's loaded */
211 	Address		load_end;	/* actual pcaddr where modl ends */
212 };
213 typedef struct mod_info	mod_info_t;
214 
215 sztype		total_names;	/* from all modules */
216 
217 /*
218  * List of shared object modules. Note that this always includes the
219  * program executable as the first element.
220  */
221 mod_info_t	modules;
222 sztype		n_modules;
223 
224 
225 
226 /*
227  * The symbol table;
228  * for each external in the specified file we gather
229  * its address, the number of calls and compute its share of cpu time.
230  */
231 struct nl {
232     char		*name;		/* the name */
233     mod_info_t		*module;	/* module to which this belongs */
234     pctype		value;		/* the pc entry point */
235     pctype		svalue;		/* entry point aligned to histograms */
236     unsigned long	sz;		/* function size */
237     unsigned char	syminfo;	/* sym info */
238     size_t		nticks;		/* ticks in this routine */
239     double		time;		/* ticks in this routine as double */
240     double		childtime;	/* cumulative ticks in children */
241     actype		ncall;		/* how many times called */
242     actype		selfcalls;	/* how many calls to self */
243     double		propfraction;	/* what % of time propagates */
244     double		propself;	/* how much self time propagates */
245     double		propchild;	/* how much child time propagates */
246     bool		printflag;	/* should this be printed? */
247     int			index;		/* index in the graph list */
248     int			toporder;	/* graph call chain top-sort order */
249     int			cycleno;	/* internal number of cycle on */
250     struct nl		*cyclehead;	/* pointer to head of cycle */
251     struct nl		*cnext;		/* pointer to next member of cycle */
252     arctype		*parents;	/* list of caller arcs */
253     arctype		*children;	/* list of callee arcs */
254     unsigned long	ncallers;	/* no. of callers - dumpsum use only */
255 };
256 typedef struct nl	nltype;
257 
258 /*
259  *	flag which marks a nl entry as topologically ``busy''
260  *	flag which marks a nl entry as topologically ``not_numbered''
261  */
262 #define	DFN_BUSY	-1
263 #define	DFN_NAN		0
264 
265 /*
266  *	namelist entries for cycle headers.
267  *	the number of discovered cycles.
268  */
269 nltype	*cyclenl;		/* cycle header namelist */
270 int	ncycle;			/* number of cycles discovered */
271 
272 /*
273  * The header on the gmon.out file.
274  * old-style gmon.out consists of one of these headers,
275  * and then an array of ncnt samples
276  * representing the discretized program counter values.
277  *	this should be a struct phdr, but since everything is done
278  *	as UNITs, this is in UNITs too.
279  */
280 struct hdr {
281 	pctype		lowpc;
282 	pctype		highpc;
283 	pctype		ncnt;
284 };
285 
286 struct hdr32 {
287 	pctype32	lowpc;
288 	pctype32	highpc;
289 	pctype32	ncnt;
290 };
291 
292 struct hdr	h;		/* header of profiled data */
293 
294 int	debug;
295 int 	number_funcs_toprint;
296 
297 /*
298  * Each discretized pc sample has
299  * a count of the number of samples in its range
300  */
301 unsigned short	*samples;
302 
303 pctype	s_lowpc;		/* lowpc from profile file in o-s gmon.out */
304 pctype	s_highpc;		/* highpc from profile file in o-s gmon.out */
305 sztype	sampbytes;		/* number of bytes of samples in o-s gmon.out */
306 sztype	nsamples;		/* number of samples for old-style gmon.out */
307 
308 double	actime;			/* accumulated time thus far for putprofline */
309 double	totime;			/* total time for all routines */
310 double	printtime;		/* total of time being printed */
311 double	scale;			/* scale factor converting samples to pc */
312 				/* values: each sample covers scale bytes */
313 				/* -- all this is for old-style gmon.out only */
314 
315 unsigned char	*textspace;		/* text space of a.out in core */
316 bool	first_file;			/* for difference option */
317 
318 /*
319  * Total number of pcsamples read so far (across gmon.out's)
320  */
321 Size	n_pcsamples;
322 
323 /*
324  *	option flags, from a to z.
325  */
326 bool	aflag;				/* suppress static functions */
327 bool	bflag;				/* blurbs, too */
328 bool	Bflag;				/* big pc's (i.e. 64 bits) */
329 bool	cflag;				/* discovered call graph, too */
330 bool	Cflag;				/* gprofing c++ -- need demangling */
331 bool	dflag;				/* debugging options */
332 bool	Dflag;				/* difference option */
333 bool	eflag;				/* specific functions excluded */
334 bool	Eflag;				/* functions excluded with time */
335 bool	fflag;				/* specific functions requested */
336 bool	Fflag;				/* functions requested with time */
337 bool	lflag;				/* exclude LOCAL syms in output */
338 bool	sflag;				/* sum multiple gmon.out files */
339 bool	zflag;				/* zero time/called functions, too */
340 bool 	nflag;				/* print only n functions in report */
341 bool	rflag;				/* profiling input generated by */
342 					/* run-time linker */
343 
344 
345 /*
346  *	structure for various string lists
347  */
348 struct stringlist {
349     struct stringlist	*next;
350     char		*string;
351 };
352 extern struct stringlist	*elist;
353 extern struct stringlist	*Elist;
354 extern struct stringlist	*flist;
355 extern struct stringlist	*Flist;
356 
357 /*
358  *	function declarations
359  */
360 void	addlist(struct stringlist *, char *);
361 void	addarc(nltype *, nltype *, actype);
362 int	arccmp(arctype *, arctype *);
363 arctype	*arclookup(nltype *, nltype *);
364 void	printblurb(char *);
365 void	dfn(nltype *);
366 bool	dfn_busy(nltype *);
367 void	dfn_findcycle(nltype *);
368 bool	dfn_numbered(nltype *);
369 void	dfn_post_visit(nltype *);
370 void	dfn_pre_visit(nltype *);
371 void	dfn_self_cycle(nltype *);
372 nltype	**doarcs(void);
373 void	done(void);
374 void	findcalls(nltype *, pctype, pctype);
375 void	flatprofheader(void);
376 void	flatprofline(nltype *);
377 bool	is_shared_obj(char *);
378 void	getnfile(char *);
379 void	process_namelist(mod_info_t *);
380 void	gprofheader(void);
381 void	gprofline(nltype *);
382 int	pc_cmp(const void *arg1, const void *arg2);
383 int	membercmp(nltype *, nltype *);
384 nltype	*nllookup(mod_info_t *, pctype, pctype *);
385 bool	onlist(struct stringlist *, char *);
386 void	printchildren(nltype *);
387 void	printcycle(nltype *);
388 void	printgprof(nltype **);
389 void	printindex(void);
390 void	printmembers(nltype *);
391 void	printmodules(void);
392 void	printname(nltype *);
393 void	printparents(nltype *);
394 void	printprof(void);
395 void	sortchildren(nltype *);
396 void	sortmembers(nltype *);
397 void	sortparents(nltype *);
398 int	timecmp(const void *arg1, const void *arg2);
399 int	totalcmp(const void *arg1, const void *arg2);
400 
401 #define	LESSTHAN	-1
402 #define	EQUALTO		0
403 #define	GREATERTHAN	1
404 
405 /*
406  * Macros related to debug messages.
407  */
408 #define	DFNDEBUG	0x0001
409 #define	CYCLEDEBUG	0x0002
410 #define	ARCDEBUG	0x0004
411 #define	TALLYDEBUG	0x0008
412 #define	TIMEDEBUG	0x0010
413 #define	SAMPLEDEBUG	0x0020
414 #define	ELFDEBUG	0x0040
415 #define	CALLSDEBUG	0x0080
416 #define	LOOKUPDEBUG	0x0100
417 #define	PROPDEBUG	0x0200
418 #define	ANYDEBUG	0x0400
419 
420 #define	MONOUTDEBUG	0x0800
421 #define	MODULEDEBUG	0x1000
422 #define	CGRAPHDEBUG	0x2000
423 #define	PCSMPLDEBUG	0x4000
424 
425 #ifdef	__cplusplus
426 }
427 #endif
428 
429 #endif	/* _SGS_GPROF_H */
430