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