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