xref: /onnv-gate/usr/src/cmd/sgs/prof/common/prof.c (revision 7008:8f7bd4ba8aeb)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53621Sab196087  * Common Development and Distribution License (the "License").
63621Sab196087  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21211Smike_s 
22211Smike_s /*
236951Sab196087  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24211Smike_s  * Use is subject to license terms.
25211Smike_s  */
26211Smike_s 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate 
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate  *	Program profiling report generator.
350Sstevel@tonic-gate  *
360Sstevel@tonic-gate  *	Usage:
370Sstevel@tonic-gate  *
38211Smike_s  * 	prof [-ChsVz] [-a | c | n | t]  [-o  |  x]   [-g  |  l]
39211Smike_s  *	    [-m mdata] [prog]
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  *	Where "prog" is the program that was profiled; "a.out" by default.
420Sstevel@tonic-gate  *	Options are:
430Sstevel@tonic-gate  *
440Sstevel@tonic-gate  *	-n	Sort by symbol name.
450Sstevel@tonic-gate  *	-t	Sort by decreasing time.
460Sstevel@tonic-gate  *	-c	Sort by decreasing number of calls.
470Sstevel@tonic-gate  *	-a	Sort by increasing symbol address.
480Sstevel@tonic-gate  *
490Sstevel@tonic-gate  *	The options that determine the type of sorting are mutually exclusive.
500Sstevel@tonic-gate  *	Additional options are:
510Sstevel@tonic-gate  *
520Sstevel@tonic-gate  *	-o	Include symbol addresses in output (in octal).
530Sstevel@tonic-gate  *	-x	Include symbol addresses in output (in hexadecimal).
540Sstevel@tonic-gate  *	-g	Include non-global T-type symbols in output.
55211Smike_s  *	-l	Do NOT include local T-type symbols in output (default).
560Sstevel@tonic-gate  *	-z	Include all symbols in profiling range, even if zero
570Sstevel@tonic-gate  *			number of calls or time.
580Sstevel@tonic-gate  *	-h	Suppress table header.
590Sstevel@tonic-gate  *	-s	Follow report with additional statistical information.
600Sstevel@tonic-gate  *	-m mdata Use file "mdata" instead of MON_OUT for profiling data.
610Sstevel@tonic-gate  *	-V	print version information for prof (and exit, if only V spec'd)
620Sstevel@tonic-gate  *	-C	call C++ demangle routine to demangle names before printing.
630Sstevel@tonic-gate  */
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #include <stdio.h>
660Sstevel@tonic-gate #include <string.h>
670Sstevel@tonic-gate #include <errno.h>
680Sstevel@tonic-gate #include <dlfcn.h>
69211Smike_s #include <ctype.h>
70*7008Sab196087 #include "conv.h"
710Sstevel@tonic-gate #include "symint.h"
720Sstevel@tonic-gate #include "sys/param.h"			/* for HZ */
730Sstevel@tonic-gate #include "mon.h"
740Sstevel@tonic-gate #include "sys/stat.h"
750Sstevel@tonic-gate #include "debug.h"
760Sstevel@tonic-gate 
770Sstevel@tonic-gate #define	OLD_DEBUG(x)
780Sstevel@tonic-gate 
790Sstevel@tonic-gate #define	Print	(void) printf
800Sstevel@tonic-gate #define	Fprint	(void) fprintf
810Sstevel@tonic-gate 
820Sstevel@tonic-gate #if vax
830Sstevel@tonic-gate 	/* Max positive difference between a fnpc and sl_addr for match */
840Sstevel@tonic-gate #define	CCADIFF	22
850Sstevel@tonic-gate 	/* Type if n_type field in file symbol table entry. */
860Sstevel@tonic-gate #endif
870Sstevel@tonic-gate 
880Sstevel@tonic-gate #if (u3b || u3b15 || u3b2 || i386)
890Sstevel@tonic-gate 	/* Max positive difference between a fnpc and sl_addr for match */
900Sstevel@tonic-gate #define	CCADIFF	20	/*  ?? (16 would probably do) */
910Sstevel@tonic-gate 	/* For u3b, the "type" is storage class + section number (no type_t) */
920Sstevel@tonic-gate #endif
930Sstevel@tonic-gate 
940Sstevel@tonic-gate #if (sparc)
950Sstevel@tonic-gate #define	CCADIFF 24	/* PIC prologue length=20 + 4 */
960Sstevel@tonic-gate #endif
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 
990Sstevel@tonic-gate #define	PROFSEC(ticks) ((double)(ticks)/HZ) /* Convert clock ticks to seconds */
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	/* Title fragment used if symbol addresses in output ("-o" or "-x"). */
1020Sstevel@tonic-gate char *atitle = " Address ";
1030Sstevel@tonic-gate 	/* Format for addresses in output */
1040Sstevel@tonic-gate char *aformat = "%8o ";
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate #if !(vax || u3b || u3b15 || u3b2 || i386 || sparc)
1070Sstevel@tonic-gate 	/* Make sure something we are set up for.  Else lay egg. */
1080Sstevel@tonic-gate #include "### No code for processor type ###"
1090Sstevel@tonic-gate #endif
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	/* Shorthand to gimme the Precise #of addresses per cells */
1130Sstevel@tonic-gate #define	DBL_ADDRPERCELL		(((double)bias)/sf)
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	/* Used for unsigned fixed-point fraction with binary scale at */
1170Sstevel@tonic-gate 	/* the left of 15'th bit (0 as least significant bit) . */
1180Sstevel@tonic-gate #define	BIAS		((long)0200000L)
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate /*
1210Sstevel@tonic-gate  *	TS1 insures that the symbols section is executable.
1220Sstevel@tonic-gate  */
1230Sstevel@tonic-gate #define	TS1(s) (((s) > 0) && (scnhdrp[(s)-1].sh_flags & SHF_EXECINSTR))
1240Sstevel@tonic-gate /*
1250Sstevel@tonic-gate  *	TS2 insures that the symbol should be reported.  We want
1260Sstevel@tonic-gate  *	to report only those symbols that are functions (STT_FUNC)
1270Sstevel@tonic-gate  *	or "notype" (STT_NOTYPE... "printf", for example).  Also,
1280Sstevel@tonic-gate  *	unless the gflag is set, the symbol must be global.
1290Sstevel@tonic-gate  */
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate #define	TS2(i)	\
1320Sstevel@tonic-gate 	(((ELF32_ST_TYPE(i) == STT_FUNC) ||		\
1330Sstevel@tonic-gate 			(ELF32_ST_TYPE(i) == STT_NOTYPE)) &&	\
1340Sstevel@tonic-gate 		((ELF32_ST_BIND(i) == STB_GLOBAL) ||		\
1350Sstevel@tonic-gate 			(gflag && (ELF32_ST_BIND(i) == STB_LOCAL))))
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate #define	TXTSYM(s, i)	(TS1(s) && TS2(i))
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate int gflag = 0;			/*  replaces gmatch and gmask */
1400Sstevel@tonic-gate int Cflag = 0;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate PROF_FILE	*ldptr; 		/* For program ("a.out") file. */
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate FILE	*mon_iop;		/* For profile (MON_OUT) file. */
1450Sstevel@tonic-gate char	*sym_fn = "a.out";	/* Default program file name. */
1460Sstevel@tonic-gate char	*mon_fn = MON_OUT;	/* Default profile file name. */
1470Sstevel@tonic-gate 				/* May be changed by "-m file". */
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate long bias;	/* adjusted bias */
1500Sstevel@tonic-gate long temp;	/* for bias adjust */
1510Sstevel@tonic-gate 
152211Smike_s extern void profver(void);
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	/* For symbol table entries read from program file. */
1550Sstevel@tonic-gate PROF_SYMBOL nl;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate /* Compare routines called from qsort() */
1580Sstevel@tonic-gate 
159211Smike_s int c_ccaddr(const void *arg1, const void *arg2);
160211Smike_s int c_sladdr(const void *arg1, const void *arg2);
161211Smike_s int c_time(const void *arg1, const void *arg2);
162211Smike_s int c_ncalls(const void *arg1, const void *arg2);
163211Smike_s int c_name(const void *arg1, const void *arg2);
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate /* Other stuff. */
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate /* Return size of open file (arg is file descriptor) */
168211Smike_s static off_t fsize(int fd);
169211Smike_s 
170211Smike_s static void snh(void);
171211Smike_s static void Perror(char *s);
172211Smike_s static void eofon(FILE *iop, char *fn);
173211Smike_s static void usage(void);
174211Smike_s static char *getname(PROF_FILE *ldpter, PROF_SYMBOL symbol);
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	/* Memory allocation. Like malloc(), but no return if error. */
177211Smike_s static void *_prof_Malloc(int item_count, int item_size);
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	/* Scan past path part (if any) in the ... */
180211Smike_s static char *basename(char *s);
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	/* command name, for error messages. */
1830Sstevel@tonic-gate char	*cmdname;
1840Sstevel@tonic-gate /* Structure of subroutine call counters (cnt) is defined in mon.h. */
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate /* Structure for header of mon.out (hdr) is defined in mon.h. */
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	/* Local representation of symbols and call/time information. */
1890Sstevel@tonic-gate struct slist {
1900Sstevel@tonic-gate 	char *sl_name;		/* Symbol name. */
1910Sstevel@tonic-gate 	char *sl_addr;		/* Address. */
1920Sstevel@tonic-gate 	long sl_size;		/* size of symbol */
1930Sstevel@tonic-gate 	long sl_count;		/* Count of subroutine calls */
1940Sstevel@tonic-gate 	float sl_time;		/* Count of clock ticks in this routine, */
1950Sstevel@tonic-gate 				/*		converted to secs. */
1960Sstevel@tonic-gate };
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	/* local structure for tracking synonyms in our symbol list */
199211Smike_s struct snymEntry {
2000Sstevel@tonic-gate 	char	*sym_addr;	/* address which has a synonym */
2010Sstevel@tonic-gate 	int	howMany;	/* # of synonyms for this symbol */
2020Sstevel@tonic-gate 	int	snymReported;	/* 'was printed in a report line already'  */
2030Sstevel@tonic-gate 				/* 	flag, */
2040Sstevel@tonic-gate 				/*   > 0 report line printed for these syns. */
2050Sstevel@tonic-gate 				/*  == 0 not printed yet. */
2060Sstevel@tonic-gate 	long	tot_sl_count;	/* total subr calls for these snyms */
2070Sstevel@tonic-gate 	float	tot_sl_time;	/* total clock ticks (a la sl_time) */
2080Sstevel@tonic-gate };
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate #define	AOUTHSZ		(filhdr.f_opthdr)
2120Sstevel@tonic-gate PROF_FILE	filhdr;			/* profile file descriptor */
2130Sstevel@tonic-gate Elf32_Shdr	*scnhdrp;	/* pointer to first section header */
2140Sstevel@tonic-gate 					/* (space by _prof_Malloc) */
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate struct hdr head;	/* Profile file (MON_OUT) header. */
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate int	(*sort)() = NULL;	/* Compare routine for sorting output */
2190Sstevel@tonic-gate 				/*	symbols.  Set by "-[acnt]". */
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate int	flags;		/* Various flag bits. */
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate char	*pc_l;		/* From head.lpc. */
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate char	*pc_h;		/*   "  head.hpc. */
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate short	VwasSpecified = 0;	/* 1 if -V was specified */
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate /*
2300Sstevel@tonic-gate  * Bit macro and flag bit definitions. These need to be identical to the
2310Sstevel@tonic-gate  * set in profv.h. Any change here should be reflected in profv.c also.
2320Sstevel@tonic-gate  */
2330Sstevel@tonic-gate #define	FBIT(pos)	(01 << (pos))	/* Returns value with bit pos set. */
2340Sstevel@tonic-gate #define	F_SORT		FBIT(0)		/* Set if "-[acnt]" seen. */
2350Sstevel@tonic-gate #define	F_VERBOSE	FBIT(1)		/* Set if "-s" seen. */
2360Sstevel@tonic-gate #define	F_ZSYMS		FBIT(2)		/* Set if "-z" seen. */
2370Sstevel@tonic-gate #define	F_PADDR		FBIT(3)		/* Set if "-o" or "-x" seen. */
2380Sstevel@tonic-gate #define	F_NHEAD		FBIT(4)		/* Set if "-h" seen. */
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate struct snymEntry *snymList;	/* Pointer to allocated list of */
2420Sstevel@tonic-gate 				/* synonym entries.  */
2430Sstevel@tonic-gate struct snymEntry *snymp;
2440Sstevel@tonic-gate 				/* for scanning entries. */
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate int snymCapacity;		/* #slots in snymList */
2470Sstevel@tonic-gate int n_snyms;			/* #used slots in snymList */
2480Sstevel@tonic-gate 
249211Smike_s static int readnl(int symindex);
250211Smike_s static int fprecision(long count);
251211Smike_s 
2520Sstevel@tonic-gate /*
2530Sstevel@tonic-gate  * Sort flags. Mutually exclusive. These need to be identical to the ones
2540Sstevel@tonic-gate  * defined in profv.h
2550Sstevel@tonic-gate  */
2560Sstevel@tonic-gate #define	BY_ADDRESS	0x1
2570Sstevel@tonic-gate #define	BY_NCALLS	0x2
2580Sstevel@tonic-gate #define	BY_NAME		0x4
2590Sstevel@tonic-gate #define	BY_TIME		0x8
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate extern unsigned char sort_flag;	/* what type of sort ? */
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	/*
2640Sstevel@tonic-gate 	 * printSnymNames - print a comma-seperated list of snym names.
2650Sstevel@tonic-gate 	 * This routine hunts down all the synonyms for the given
2660Sstevel@tonic-gate 	 * symbol, and prints them as a comma-seperated list.
2670Sstevel@tonic-gate 	 * NB we assume that all the synonyms _Follow_ this one,
2680Sstevel@tonic-gate 	 * since they are only printed when the First one
2690Sstevel@tonic-gate 	 * is seen.
2700Sstevel@tonic-gate 	 */
2710Sstevel@tonic-gate void
printSnymNames(struct slist * slp,struct snymEntry * snymp)272211Smike_s printSnymNames(struct slist *slp, struct snymEntry *snymp)
2730Sstevel@tonic-gate {
2740Sstevel@tonic-gate 	/* how many snyms for this addr, total, and their shared address */
2750Sstevel@tonic-gate 	int i = snymp->howMany;
2760Sstevel@tonic-gate 	char *sharedaddr = snymp->sym_addr;
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	/* put out first name - it counts as one, so decr count */
2790Sstevel@tonic-gate 	(void) fputs(slp->sl_name, stdout);
2800Sstevel@tonic-gate 	i--;
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	/* for the others: find each, print each. */
2830Sstevel@tonic-gate 	while (--i >= 0) {
2846951Sab196087 		while ((++slp)->sl_addr != sharedaddr)
2856951Sab196087 			;
2860Sstevel@tonic-gate 		Print(", %s", slp->sl_name);
2870Sstevel@tonic-gate 	}
2880Sstevel@tonic-gate 	/* finally.. the trailing newline */
289211Smike_s 	(void) putchar('\n');
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	/*
2940Sstevel@tonic-gate 	 * getSnymEntry - see if addr was noted as a aliased address
2950Sstevel@tonic-gate 	 * (i.e. a synonym symbol) and return the address of the
2960Sstevel@tonic-gate 	 * snym entry if it was.
2970Sstevel@tonic-gate 	 */
298211Smike_s struct snymEntry *
getSnymEntry(char * sl_addr)299211Smike_s getSnymEntry(char *sl_addr)
3000Sstevel@tonic-gate {
3010Sstevel@tonic-gate 	struct snymEntry *p;
3020Sstevel@tonic-gate 	int i;
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	for (p = snymList, i = n_snyms; --i >= 0; p++)
3050Sstevel@tonic-gate 		if (sl_addr == p->sym_addr)
3060Sstevel@tonic-gate 			return (p);
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	return ((struct snymEntry *)0);
3090Sstevel@tonic-gate }
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 
312211Smike_s int
main(int argc,char ** argv)313211Smike_s main(int argc, char **argv)
3140Sstevel@tonic-gate {
3150Sstevel@tonic-gate 	char buffer[BUFSIZ];	/* buffer for printf */
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	WORD *pcounts;	/* Pointer to allocated area for */
3180Sstevel@tonic-gate 			/*	pcounts: PC clock hit counts */
3190Sstevel@tonic-gate 
320211Smike_s 	WORD *pcp;	/* For scanning pcounts. */
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	struct cnt *ccounts;	/* Pointer to allocated area for cnt */
3230Sstevel@tonic-gate 				/* structures: subr PC-call counts. */
3240Sstevel@tonic-gate 
325211Smike_s 	struct cnt *ccp;	/* For scanning ccounts. */
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	struct slist *slist;	/* Pointer to allocated slist structures: */
3280Sstevel@tonic-gate 				/* symbol name/address/time/call counts */
3290Sstevel@tonic-gate 
330211Smike_s 	struct slist *slp;	/* For scanning slist */
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	int vn_cc, n_cc;	/* Number of cnt structures in profile data */
3330Sstevel@tonic-gate 				/*	file (later # ones used). */
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	int n_pc;	/* Number of pcounts in profile data file. */
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	int n_syms;	/* Number of text symbols (of proper type) */
3380Sstevel@tonic-gate 			/*	that fill in range of profiling. */
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	int n_nonzero;	/* Number of (above symbols) actually printed */
3410Sstevel@tonic-gate 			/*	because nonzero time or # calls. */
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	int symttl;	/* Total # symbols in program file sym-table */
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	int i;
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	int fdigits = 0; /* # of digits of precision for print msecs/call */
3480Sstevel@tonic-gate 
349211Smike_s 	int n, symct;
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	long sf;	/* Scale for index into pcounts: */
3520Sstevel@tonic-gate 			/*	i(pc) = ((pc - pc_l) * sf)/bias. */
3530Sstevel@tonic-gate 
354211Smike_s 	/* LINTED: set but not used */
3550Sstevel@tonic-gate 	long s_inv;	/* Inverse: i_inv(i) = */
3560Sstevel@tonic-gate 			/*		{pc00, pc00+1, ... pc00+s_inv-1}. */
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	unsigned pc_m;	/* Range of PCs profiled: pc_m = pc_h - pc_l */
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	float	t, t0;
3610Sstevel@tonic-gate 	float	t_tot;	/* Total time: PROFSEC(sum of all pcounts[i]) */
3620Sstevel@tonic-gate 	int	callTotal = 0;
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	DEBUG_LOC("main: top");
3650Sstevel@tonic-gate 	setbuf(stdout, buffer);
3660Sstevel@tonic-gate 	cmdname = basename(*argv);	/* command name. */
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	while ((n = getopt(argc, argv, "canthsglzoxT:m:VC")) != EOF) {
3690Sstevel@tonic-gate 		switch (n) {
3700Sstevel@tonic-gate 		int (*fcn)();	/* For function to sort results. */
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 		case 'm':	/* Specify data file:	-m file */
3730Sstevel@tonic-gate 			mon_fn = optarg;
3740Sstevel@tonic-gate 			break;
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate #ifdef ddt
3770Sstevel@tonic-gate 		case 'T':	/* Set trace flags: -T(octnum) */
3780Sstevel@tonic-gate 			debug_value = (int)strtol(optarg, 0, 8);
3790Sstevel@tonic-gate 			break;
3800Sstevel@tonic-gate #endif
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 		case 'n':	/* Sort by symbol name. */
3830Sstevel@tonic-gate 			fcn = c_name;
3840Sstevel@tonic-gate 			sort_flag |= BY_NAME;
3850Sstevel@tonic-gate 			goto check;
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 		case 't':	/* Sort by decreasing time. */
3880Sstevel@tonic-gate 			fcn = c_time;
3890Sstevel@tonic-gate 			sort_flag |= BY_TIME;
3900Sstevel@tonic-gate 			goto check;
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 		case 'c':	/* Sort by decreasing # calls. */
3930Sstevel@tonic-gate 			fcn = c_ncalls;
3940Sstevel@tonic-gate 			sort_flag |= BY_NCALLS;
3950Sstevel@tonic-gate 			goto check;
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 		case 'a':	/* Sort by increasing symbol address */
3980Sstevel@tonic-gate 				/*		(don't have to -- it will be) */
3990Sstevel@tonic-gate 			fcn = NULL;
4000Sstevel@tonic-gate 			sort_flag |= BY_ADDRESS;
4010Sstevel@tonic-gate 		check:		/* Here to check sort option conflicts. */
4020Sstevel@tonic-gate 			if (sort != NULL && sort != fcn) {
4030Sstevel@tonic-gate 				Fprint(stderr, "%s: Warning: %c overrides"
4040Sstevel@tonic-gate 				" previous specification\n", cmdname, n);
4050Sstevel@tonic-gate 			}
4060Sstevel@tonic-gate 			sort = fcn;	/* Store sort routine */
4070Sstevel@tonic-gate 			flags |= F_SORT; /* Note have done so */
4080Sstevel@tonic-gate 			break;
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 		case 'o':	/* Include symbol addresses in output. */
4110Sstevel@tonic-gate 		case 'x':	/* Include symbol addresses in output. */
4120Sstevel@tonic-gate 			aformat[2] = n;	/* 'o' or 'x' in format */
4130Sstevel@tonic-gate 			flags |= F_PADDR;	/* Set flag. */
4140Sstevel@tonic-gate 			break;
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 		case 'g':	/* Include local T symbols as well as global */
4170Sstevel@tonic-gate 			gflag = 1;
4180Sstevel@tonic-gate 			break;
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 		case 'l':	/* Do NOT include local T symbols */
4210Sstevel@tonic-gate 			gflag = 0;
4220Sstevel@tonic-gate 			break;
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 		case 'z':	/* Print all symbols in profiling range, */
4250Sstevel@tonic-gate 				/*	 even if no time or # calls. */
4260Sstevel@tonic-gate 			flags |= F_ZSYMS;	/* Set flag. */
4270Sstevel@tonic-gate 			break;
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 		case 'h':	/* Suppress table header. */
4300Sstevel@tonic-gate 			flags |= F_NHEAD;
4310Sstevel@tonic-gate 			break;
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 		case 's':	/* Follow normal output with extra summary. */
4340Sstevel@tonic-gate 			flags |= F_VERBOSE;	/* Set flag (...) */
4350Sstevel@tonic-gate 			break;
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 		case 'V':
4380Sstevel@tonic-gate 			(void) fprintf(stderr, "prof: %s %s\n",
4390Sstevel@tonic-gate 			    (const char *)SGU_PKG, (const char *)SGU_REL);
4400Sstevel@tonic-gate 			VwasSpecified = 1;
4410Sstevel@tonic-gate 			break;
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 		case 'C':	/* demangle C++ names before printing. */
4440Sstevel@tonic-gate 			Cflag = 1;
4450Sstevel@tonic-gate 			break;
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 		case '?':	/* But no good. */
448211Smike_s 			usage();
4490Sstevel@tonic-gate 		}	/* End switch (n) */
4500Sstevel@tonic-gate 	}	/* End while (getopt) */
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	DEBUG_LOC("main: following getopt");
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	/* if -V the only argument, just exit. */
4550Sstevel@tonic-gate 	if (VwasSpecified && argc == 2 && !flags)
4560Sstevel@tonic-gate 		exit(0);
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	if (optind < argc)
4590Sstevel@tonic-gate 		sym_fn = argv[optind];	/* name other than `a.out' */
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 	if (sort == NULL && !(flags & F_SORT))
4620Sstevel@tonic-gate 				/* If have not specified sort mode ... */
4630Sstevel@tonic-gate 		sort = c_time;		/* then sort by decreasing time. */
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	/*
4660Sstevel@tonic-gate 	 * profver() checks to see if the mon.out was "versioned" and if
4670Sstevel@tonic-gate 	 * yes, processes it and exits; otherwise, we have an *old-style*
4680Sstevel@tonic-gate 	 * mon.out and we process it the old way.
4690Sstevel@tonic-gate 	 */
4700Sstevel@tonic-gate 	profver();
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 		/* Open monitor data file (has counts). */
4730Sstevel@tonic-gate 	if ((mon_iop = fopen(mon_fn, "r")) == NULL)
4740Sstevel@tonic-gate 		Perror(mon_fn);
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	DEBUG_LOC("main: before _symintOpen");
4770Sstevel@tonic-gate 	if ((ldptr = _symintOpen(sym_fn)) == NULL) {
4780Sstevel@tonic-gate 		Perror("_symintOpen failed");
4790Sstevel@tonic-gate 	}
4800Sstevel@tonic-gate 	DEBUG_LOC("main: after _symintOpen");
4810Sstevel@tonic-gate 	filhdr = *ldptr;
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	scnhdrp = ldptr->pf_shdarr_p;
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	{
4860Sstevel@tonic-gate 	Elf_Kind k = elf_kind(filhdr.pf_elf_p);
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	DEBUG_EXP(printf("elf_kind = %d\n", k));
4890Sstevel@tonic-gate 	DEBUG_EXP(printf("elf_type = %d\n", filhdr.pf_elfhd_p->e_type));
4900Sstevel@tonic-gate 	if ((k != ELF_K_ELF) || (filhdr.pf_elfhd_p->e_type != ET_EXEC)) {
4910Sstevel@tonic-gate 		Fprint(stderr, "%s: %s: improper format\n", cmdname, sym_fn);
4920Sstevel@tonic-gate 		exit(1);
4930Sstevel@tonic-gate 	}
4940Sstevel@tonic-gate 	}
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	/* Compute the file address of symbol table. Machine-dependent. */
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	DEBUG_EXP(printf("number of symbols (pf_nsyms) = %d\n",
4996951Sab196087 	    filhdr.pf_nsyms));
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 		/* Number of symbols in file symbol table. */
5020Sstevel@tonic-gate 	symttl = filhdr.pf_nsyms;
5030Sstevel@tonic-gate 	if (symttl == 0) {		/* This is possible. */
5040Sstevel@tonic-gate 		Fprint(stderr, "%s: %s: no symbols\n", cmdname, sym_fn);
5050Sstevel@tonic-gate 		exit(0);		/* Note zero exit code. */
5060Sstevel@tonic-gate 	}
5070Sstevel@tonic-gate 	/* Get size of file containing profiling data. Read header part. */
5080Sstevel@tonic-gate 	n = fsize(fileno(mon_iop));
5090Sstevel@tonic-gate 	if (fread((char *)&head, sizeof (struct hdr), 1, mon_iop) != 1)
5100Sstevel@tonic-gate 		eofon(mon_iop, mon_fn);		/* Probably junk file. */
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	/* Get # cnt structures (they follow header), */
5130Sstevel@tonic-gate 	/*		and allocate space for them. */
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	n_cc = head.nfns;
516211Smike_s 	ccounts = _prof_Malloc(n_cc, sizeof (struct cnt));
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 		/* Read the call addr-count pairs. */
5190Sstevel@tonic-gate 	if (fread((char *)ccounts, sizeof (struct cnt), n_cc, mon_iop) != n_cc)
5200Sstevel@tonic-gate 		eofon(mon_iop, mon_fn);
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	/*
5230Sstevel@tonic-gate 	 * Compute # PC counters (pcounts), which occupy whatever is left
5240Sstevel@tonic-gate 	 * of the file after the header and call counts.
5250Sstevel@tonic-gate 	 */
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	n_pc = (n - sizeof (head) - n_cc * sizeof (struct cnt))/sizeof (WORD);
5280Sstevel@tonic-gate 	ccp = &ccounts[n_cc];	/* Point to last (+1) of call counters ... */
5290Sstevel@tonic-gate 	do {		/* and scan backward until find highest one used. */
5300Sstevel@tonic-gate 		if ((--ccp)->mcnt)
5310Sstevel@tonic-gate 			break;		/* Stop when find nonzero count. */
5320Sstevel@tonic-gate 	} while (--n_cc > 0);		/* Or all are zero. */
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	if (n_cc > 0) {
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	/* If less than all cnt entries are used, return unused space. */
5370Sstevel@tonic-gate 	if (n_cc < head.nfns) {
5380Sstevel@tonic-gate 		if ((ccounts = (struct cnt *)realloc((char *)ccounts,
5390Sstevel@tonic-gate 		    (unsigned)n_cc * sizeof (struct cnt))) == NULL)
5400Sstevel@tonic-gate 			snh();	/* Should not fail when reducing size. */
5410Sstevel@tonic-gate 	}
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	/* If more than 250 cnt entries used set verbose for warning */
5440Sstevel@tonic-gate 	if (n_cc > (MPROGS0 * 5)/6)
5450Sstevel@tonic-gate 		flags |= F_VERBOSE;
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 		/* Space for PC counts. */
5480Sstevel@tonic-gate 	pcounts = (WORD *)_prof_Malloc(n_pc, sizeof (WORD));
5490Sstevel@tonic-gate 		/* Read the PC counts from rest of MON_OUT file. */
5500Sstevel@tonic-gate 	if (fread((char *)pcounts, sizeof (WORD), n_pc, mon_iop) != n_pc)
5510Sstevel@tonic-gate 		eofon(mon_iop, mon_fn);
5520Sstevel@tonic-gate 	/*
5530Sstevel@tonic-gate 	 *
5540Sstevel@tonic-gate 	 * Having gotten preliminaries out of the way, get down to business.
5550Sstevel@tonic-gate 	 * The range pc_m of addresses over which profiling was done is
5560Sstevel@tonic-gate 	 * computed from the low (pc_l) and high (pc_h) addresses, gotten
5570Sstevel@tonic-gate 	 * from the MON_OUT header.  From this and the number of clock
5580Sstevel@tonic-gate 	 * tick counters, n_pc, is computed the so-called "scale", sf, used
5590Sstevel@tonic-gate 	 * in the mapping of addresses to indices, as follows:
5600Sstevel@tonic-gate 	 *
5610Sstevel@tonic-gate 	 *		(pc - pc_l) * sf
5620Sstevel@tonic-gate 	 *	i(pc) = ----------------
5630Sstevel@tonic-gate 	 *		  0200000
5640Sstevel@tonic-gate 	 *
5650Sstevel@tonic-gate 	 * Also, the N-to-one value, s_inv, such that
5660Sstevel@tonic-gate 	 *
5670Sstevel@tonic-gate 	 *	i(pc_l + K * s_inv + d) = K, for 0 <= d < s_inv
5680Sstevel@tonic-gate 	 *
5690Sstevel@tonic-gate 	 * Following this, the symbol table is scanned, and those symbols
5700Sstevel@tonic-gate 	 * that qualify are counted.  These  are T-type symbols, excluding
5710Sstevel@tonic-gate 	 * local (nonglobal) unless the "-g" option was given. Having thus
5720Sstevel@tonic-gate 	 * determined the space requirements, space for symbols/times etc.
5730Sstevel@tonic-gate 	 * is allocated, and the symbol table re-read, this time keeping
5740Sstevel@tonic-gate 	 * qualified symbols.
5750Sstevel@tonic-gate 	 *
5760Sstevel@tonic-gate 	 * NB s_inv, as actually computed, is not sufficiently accurate
5770Sstevel@tonic-gate 	 * (since it is truncated) for many calculations.  Since it is
5780Sstevel@tonic-gate 	 * logically equivalent to 1/(sf/bias), and the latter is much
5790Sstevel@tonic-gate 	 * more accurate, therefore the latter will often appear in
5800Sstevel@tonic-gate 	 * the code when 's_inv' is mentioned.  dween
5810Sstevel@tonic-gate 	 *
5820Sstevel@tonic-gate 	 */
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	pc_l = head.lpc;	/* Low PC of range that was profiled. */
5860Sstevel@tonic-gate 	pc_h = head.hpc;	/* First address past range of profiling. */
5870Sstevel@tonic-gate 	pc_m = pc_h - pc_l;	/* Range of profiled addresses. */
5880Sstevel@tonic-gate 
5896951Sab196087 	/* BEGIN CSTYLED */
5900Sstevel@tonic-gate OLD_DEBUG(if (debug_value) Fprint(stderr,
5910Sstevel@tonic-gate "low pc = %#o, high pc = %#o, range = %#o = %u\n\
5920Sstevel@tonic-gate call counts: %u, %u used; pc counters: %u\n",
5930Sstevel@tonic-gate pc_l, pc_h, pc_m, pc_m, head.nfns, n_cc, n_pc));
5946951Sab196087 	/* END CSTYLED */
5950Sstevel@tonic-gate 
5966951Sab196087 	/*LINTED: E_ASSIGMENT_CAUSE_LOSS_PREC*/
5970Sstevel@tonic-gate 	sf = (BIAS * (double)n_pc)/pc_m;
5980Sstevel@tonic-gate 	/*
5990Sstevel@tonic-gate 	 * Now adjust bias and sf so that there is no overflow
6000Sstevel@tonic-gate 	 * when calculating indices.
6010Sstevel@tonic-gate 	 */
6020Sstevel@tonic-gate 	bias = BIAS;
6030Sstevel@tonic-gate 	temp = pc_m;
6040Sstevel@tonic-gate 	while ((temp >>= 1) > 0x7fff) {
6050Sstevel@tonic-gate 		sf >>= 1;
6060Sstevel@tonic-gate 		bias >>= 1;
6070Sstevel@tonic-gate 	}
6080Sstevel@tonic-gate 	s_inv = pc_m/n_pc;	/* Range of PCs mapped into one index. */
6090Sstevel@tonic-gate 
6106951Sab196087 	/* BEGIN CSTYLED */
6110Sstevel@tonic-gate OLD_DEBUG(
6120Sstevel@tonic-gate 	if (debug_value) {
6130Sstevel@tonic-gate 		Fprint(
6140Sstevel@tonic-gate 			stderr,
6150Sstevel@tonic-gate 			"sf = %d, s_inv = %d bias = %d\n",
6160Sstevel@tonic-gate 			(long)sf, s_inv, bias);
6170Sstevel@tonic-gate 	}
6180Sstevel@tonic-gate );
6196951Sab196087 	/* END CSTYLED */
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 		/* Prepare to read symbols from "a.out" (or whatever). */
6220Sstevel@tonic-gate 	n_syms = 0;			/* Init count of qualified symbols. */
6230Sstevel@tonic-gate 	n = symttl;			/* Total symbols. */
6240Sstevel@tonic-gate 	while (--n >= 0)			/* Scan symbol table. */
6250Sstevel@tonic-gate 		if (readnl(n))	/* Read and examine symbol, count qualifiers */
6260Sstevel@tonic-gate 			n_syms++;
6270Sstevel@tonic-gate 
6286951Sab196087 	/* BEGIN CSTYLED */
6290Sstevel@tonic-gate OLD_DEBUG(
6300Sstevel@tonic-gate 	if (debug_value) {
6310Sstevel@tonic-gate 		Fprint(stderr, "%u symbols, %u qualify\n", symttl, n_syms);
6320Sstevel@tonic-gate 	}
6330Sstevel@tonic-gate );
6346951Sab196087 	/* END CSTYLED */
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 		/* Allocate space for qualified symbols. */
6370Sstevel@tonic-gate 
638211Smike_s 	slist = slp = _prof_Malloc(n_syms, sizeof (struct slist));
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 		/*
6410Sstevel@tonic-gate 		 * Allocate space for synonym symbols
6420Sstevel@tonic-gate 		 * (i.e. symbols that refer to the same address).
6430Sstevel@tonic-gate 		 * NB there can be no more than n_syms/2 addresses
6440Sstevel@tonic-gate 		 * with symbols, That Have Aliases, that refer to them!
6450Sstevel@tonic-gate 		 */
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	snymCapacity = n_syms/2;
6486951Sab196087 	snymList = snymp =
6496951Sab196087 	    _prof_Malloc(snymCapacity, sizeof (struct snymEntry));
6500Sstevel@tonic-gate 	n_snyms = 0;
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate /* OLD_DEBUG(debug_value &= ~020); */
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	/* Loop on number of qualified symbols. */
6550Sstevel@tonic-gate 	for (n = n_syms, symct = 0; n > 0; symct++) {
6560Sstevel@tonic-gate 		if (readnl(symct)) {	/* Get one. Check again. */
6570Sstevel@tonic-gate 				/* Is qualified. Move name ... */
6580Sstevel@tonic-gate 			slp->sl_name = getname(ldptr, nl);
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 				/* and address into slist structure. */
6610Sstevel@tonic-gate 			slp->sl_addr = (char *)nl.ps_sym.st_value;
6620Sstevel@tonic-gate 			slp->sl_size = nl.ps_sym.st_size;
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 				/* set other slist fields to zero. */
6650Sstevel@tonic-gate 			slp->sl_time = 0.0;
6660Sstevel@tonic-gate 			slp->sl_count = 0;
6676951Sab196087 	/* BEGIN CSTYLED */
6680Sstevel@tonic-gate OLD_DEBUG(
6690Sstevel@tonic-gate 	if (debug_value & 02)
6700Sstevel@tonic-gate 		Fprint(stderr, "%-8.8s: %#8o\n", slp->sl_name, slp->sl_addr)
6710Sstevel@tonic-gate );
6726951Sab196087 	/* END CSTYLED */
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 			slp++;
6750Sstevel@tonic-gate 			--n;
6760Sstevel@tonic-gate 		}
6770Sstevel@tonic-gate 	}
6780Sstevel@tonic-gate 	/*
6790Sstevel@tonic-gate 	 *
6800Sstevel@tonic-gate 	 * Now attempt to match call counts with symbols.  To do this, it
6810Sstevel@tonic-gate 	 * helps to first sort both the symbols and the call address/count
6820Sstevel@tonic-gate 	 * pairs by ascending address, since they are generally not, to
6830Sstevel@tonic-gate 	 * begin with.  The addresses associated with the counts are not,
6840Sstevel@tonic-gate 	 * of course, the subroutine addresses associated with the symbols,
6850Sstevel@tonic-gate 	 * but some address slightly past these. Therefore a given count
6860Sstevel@tonic-gate 	 * address (in the fnpc field) is matched with the closest symbol
6870Sstevel@tonic-gate 	 * address (sl_addr) that is:
6880Sstevel@tonic-gate 	 *	(1) less than the fnpc value but,
6890Sstevel@tonic-gate 	 *	(2) not more than the length of the function
6900Sstevel@tonic-gate 	 * In other words, unreasonable matchups are avoided.
6910Sstevel@tonic-gate 	 * Situations such as this could arise when static procedures are
6920Sstevel@tonic-gate 	 * counted but the "-g" option was not given to this program,
6930Sstevel@tonic-gate 	 * causing the symbol to fail to qualify.  Without this limitation,
6940Sstevel@tonic-gate 	 * unmatched counts could be erroneously charged.
6950Sstevel@tonic-gate 	 *
6960Sstevel@tonic-gate 	 */
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	ccp = ccounts;			/* Point to first call counter. */
7000Sstevel@tonic-gate 	slp = slist;			/*   "		"   "   symbol. */
7010Sstevel@tonic-gate 		/* Sort call counters and ... */
7020Sstevel@tonic-gate 	qsort((char *)ccp, (unsigned)n_cc, sizeof (struct cnt), c_ccaddr);
7030Sstevel@tonic-gate 		/* symbols by increasing address. */
7040Sstevel@tonic-gate 	qsort((char *)slp, (unsigned)n_syms, sizeof (struct slist), c_sladdr);
7050Sstevel@tonic-gate 	vn_cc = n_cc;			/* save this for verbose option */
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 		/* Loop to match up call counts & symbols. */
7090Sstevel@tonic-gate 	for (n = n_syms; n > 0 && vn_cc > 0; ) {
7100Sstevel@tonic-gate 		int	sz = slp->sl_size;
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 		if (sz == 0)
7136951Sab196087 			sz = slp[ 1 ].sl_addr - slp->sl_addr;
7140Sstevel@tonic-gate 		if (slp->sl_addr < ccp->fnpc &&
7150Sstevel@tonic-gate 		    ccp->fnpc <= slp->sl_addr + sz) {
7160Sstevel@tonic-gate 					/* got a candidate: find Closest. */
7170Sstevel@tonic-gate 			struct slist *closest_symp;
7180Sstevel@tonic-gate 			do {
7190Sstevel@tonic-gate 				closest_symp = slp;
7200Sstevel@tonic-gate 				slp++;
7210Sstevel@tonic-gate 				--n;
7220Sstevel@tonic-gate 			} while (n > 0 && slp->sl_addr < ccp->fnpc);
7230Sstevel@tonic-gate 
7246951Sab196087 	/* BEGIN CSTYLED */
7250Sstevel@tonic-gate OLD_DEBUG(
7260Sstevel@tonic-gate if (debug_value & 04) {
7270Sstevel@tonic-gate 	Fprint(stderr,
7280Sstevel@tonic-gate 		"Routine %-8.8s @ %#8x+%-2d matches count address %#8x\n",
7290Sstevel@tonic-gate 		closest_symp->sl_name,
7300Sstevel@tonic-gate 		closest_symp->sl_addr,
7310Sstevel@tonic-gate 		ccp->fnpc-slp->sl_addr,
7320Sstevel@tonic-gate 		ccp->fnpc);
7330Sstevel@tonic-gate }
7340Sstevel@tonic-gate );
7356951Sab196087 	/* END CSTYLED */
7360Sstevel@tonic-gate 			closest_symp->sl_count = ccp->mcnt;  /* Copy count. */
7370Sstevel@tonic-gate 			++ccp;
7380Sstevel@tonic-gate 			--vn_cc;
7390Sstevel@tonic-gate 		} else if (ccp->fnpc < slp->sl_addr) {
7400Sstevel@tonic-gate 			++ccp;
7410Sstevel@tonic-gate 			--vn_cc;
7420Sstevel@tonic-gate 		} else {
7430Sstevel@tonic-gate 			++slp;
7440Sstevel@tonic-gate 			--n;
7450Sstevel@tonic-gate 		}
7460Sstevel@tonic-gate 	}
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	/*
7490Sstevel@tonic-gate 	 *
7500Sstevel@tonic-gate 	 * The distribution of times to addresses is done on a proportional
7510Sstevel@tonic-gate 	 * basis as follows: The t counts in pcounts[i] correspond to clock
7520Sstevel@tonic-gate 	 * ticks for values of pc in the range pc, pc+1, ..., pc+s_inv-1
7530Sstevel@tonic-gate 	 * (odd addresses excluded for PDP11s). Without more detailed info,
7540Sstevel@tonic-gate 	 * it must be assumed that there is no greater probability
7550Sstevel@tonic-gate 	 * of the clock ticking for any particular pc in this range than for
7560Sstevel@tonic-gate 	 * any other.  Thus the t counts are considered to be equally
7570Sstevel@tonic-gate 	 * distributed over the addresses in the range, and that the time for
7580Sstevel@tonic-gate 	 * any given address in the range is pcounts[i]/s_inv.
7590Sstevel@tonic-gate 	 *
7600Sstevel@tonic-gate 	 * The values of the symbols that qualify, bounded below and above
7610Sstevel@tonic-gate 	 * by pc_l and pc_h, respectively, partition the profiling range into
7620Sstevel@tonic-gate 	 * regions to which are assigned the total times associated with the
7630Sstevel@tonic-gate 	 * addresses they contain in the following way:
7640Sstevel@tonic-gate 	 *
7650Sstevel@tonic-gate 	 * The sum of all pcounts[i] for which the corresponding addresses are
7660Sstevel@tonic-gate 	 * wholly within the partition are charged to the partition (the
7670Sstevel@tonic-gate 	 * subroutine whose address is the lower bound of the partition).
7680Sstevel@tonic-gate 	 *
7690Sstevel@tonic-gate 	 * If the range of addresses corresponding to a given t = pcounts[i]
7700Sstevel@tonic-gate 	 * lies astraddle the boundary of a partition, e.g., for some k such
7710Sstevel@tonic-gate 	 * that 0 < k < s_inv-1, the addresses pc, pc+1, ..., pc+k-1 are in
7720Sstevel@tonic-gate 	 * the lower partition, and the addresses pc+k, pc+k+1, ..., pc+s_inv-1
7730Sstevel@tonic-gate 	 * are in the next partition, then k*pcounts[i]/s_inv time is charged
7740Sstevel@tonic-gate 	 * to the lower partition, and (s_inv-k) * pcounts[i]/s_inv time to the
7750Sstevel@tonic-gate 	 * upper.  It is conceivable, in cases of large granularity or small
7760Sstevel@tonic-gate 	 * subroutines, for a range corresponding to a given pcounts[i] to
7770Sstevel@tonic-gate 	 * overlap three regions, completely containing the (small) middle one.
7780Sstevel@tonic-gate 	 * The algorithm is adjusted appropriately in this case.
7790Sstevel@tonic-gate 	 *
7800Sstevel@tonic-gate 	 */
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	pcp = pcounts;				/* Reset to base. */
7840Sstevel@tonic-gate 	slp = slist;				/* Ditto. */
7850Sstevel@tonic-gate 	t0 = 0.0;				/* Time accumulator. */
7860Sstevel@tonic-gate 	for (n = 0; n < n_syms; n++) {		/* Loop on symbols. */
7870Sstevel@tonic-gate 			/* Start addr of region, low addr of overlap. */
7880Sstevel@tonic-gate 		char *pc0, *pc00;
7890Sstevel@tonic-gate 			/* Start addr of next region, low addr of overlap. */
7900Sstevel@tonic-gate 		char *pc1, *pc10;
7910Sstevel@tonic-gate 		/* First index into pcounts for this region and next region. */
792211Smike_s 		int i0, i1;
7930Sstevel@tonic-gate 		long ticks;
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 			/* Address of symbol (subroutine). */
7960Sstevel@tonic-gate 		pc0 = slp[n].sl_addr;
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 			/* Address of next symbol, if any or top */
7990Sstevel@tonic-gate 			/* of profile range, if not */
8000Sstevel@tonic-gate 		pc1 = (n < n_syms - 1) ? slp[n+1].sl_addr : pc_h;
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 			/* Lower bound of indices into pcounts for this range */
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 		i0 = (((unsigned)pc0 - (unsigned)pc_l) * sf)/bias;
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 			/* Upper bound (least or least + 1) of indices. */
8070Sstevel@tonic-gate 		i1 = (((unsigned)pc1 - (unsigned)pc_l) * sf)/bias;
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 		if (i1 >= n_pc)				/* If past top, */
8100Sstevel@tonic-gate 			i1 = n_pc - 1;				/* adjust. */
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 			/* Lowest addr for which count maps to pcounts[i0]; */
8130Sstevel@tonic-gate 		pc00 =  pc_l + (unsigned long)((bias * i0)/sf);
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 			/* Lowest addr for which count maps to pcounts[i1]. */
8160Sstevel@tonic-gate 		pc10 =  pc_l + (unsigned long)((bias * i1)/sf);
8170Sstevel@tonic-gate 
8186951Sab196087 	/* BEGIN CSTYLED */
8190Sstevel@tonic-gate OLD_DEBUG(if (debug_value & 010) Fprint(stderr,
8200Sstevel@tonic-gate "%-8.8s\ti0 = %4d, pc00 = %#6o, pc0 = %#6o\n\
8210Sstevel@tonic-gate \t\ti1 = %4d, pc10 = %#6o, pc1 = %#6o\n\t\t",
8220Sstevel@tonic-gate slp[n].sl_name, i0, pc00, pc0, i1, pc10, pc1));
8236951Sab196087 	/* END CSTYLED */
8240Sstevel@tonic-gate 		t = 0;			/* Init time for this symbol. */
8250Sstevel@tonic-gate 		if (i0 == i1) {
8260Sstevel@tonic-gate 			/* Counter overlaps two areas? (unlikely */
8270Sstevel@tonic-gate 			/* unless large granularity). */
8280Sstevel@tonic-gate 			ticks = pcp[i0];	/* # Times (clock ticks). */
8290Sstevel@tonic-gate OLD_DEBUG(if (debug_value & 010) fprintf(stderr, "ticks = %d\n", ticks));
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 			    /* Time less that which overlaps adjacent areas */
8320Sstevel@tonic-gate 			t += PROFSEC(ticks * ((double)(pc1 - pc0) * sf)/bias);
8330Sstevel@tonic-gate 
8346951Sab196087 	/* BEGIN CSTYLED */
8350Sstevel@tonic-gate OLD_DEBUG(if (debug_value & 010)
8360Sstevel@tonic-gate 	Fprint(stderr, "%ld/(%.1f)", (pc1 - pc0) * ticks, DBL_ADDRPERCELL)
8370Sstevel@tonic-gate );
8386951Sab196087 	/* END CSTYLED */
8390Sstevel@tonic-gate 		} else {
8400Sstevel@tonic-gate 				/* Overlap with previous region? */
8410Sstevel@tonic-gate 			if (pc00 < pc0) {
8420Sstevel@tonic-gate 				ticks = pcp[i0];
8436951Sab196087 	/* BEGIN CSTYLED */
8440Sstevel@tonic-gate OLD_DEBUG(if (debug_value & 010)
8450Sstevel@tonic-gate 	fprintf(stderr, "pc00 < pc0 ticks = %d\n", ticks));
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 				/* Get time of overlapping area and */
8480Sstevel@tonic-gate 				/* subtract proportion for lower region. */
8490Sstevel@tonic-gate 				t += PROFSEC(
8500Sstevel@tonic-gate 				ticks*(1-((double)(pc0-pc00) *sf)/bias));
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 				/* Do not count this time when summing times */
8530Sstevel@tonic-gate 				/*		wholly within the region. */
8540Sstevel@tonic-gate 				i0++;
8556951Sab196087 	/* BEGIN CSTYLED */
8560Sstevel@tonic-gate OLD_DEBUG(if (debug_value & 010)
8570Sstevel@tonic-gate 	Fprint(stderr, "%ld/(%.1f) + ", (pc0 - pc00) * ticks,
8580Sstevel@tonic-gate 		DBL_ADDRPERCELL));
8596951Sab196087 	/* END CSTYLED */
8600Sstevel@tonic-gate 			}
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 			/* Init sum of counts for PCs not shared w/other */
8630Sstevel@tonic-gate 			/*	routines. */
8640Sstevel@tonic-gate 			ticks = 0;
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 			/* Stop at first count that overlaps following */
8670Sstevel@tonic-gate 			/*	routine. */
8680Sstevel@tonic-gate 			for (i = i0; i < i1; i++)
8690Sstevel@tonic-gate 				ticks += pcp[i];
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 			t += PROFSEC(ticks); /* Convert to secs, add to total */
8720Sstevel@tonic-gate OLD_DEBUG(if (debug_value & 010) Fprint(stderr, "%ld", ticks));
8730Sstevel@tonic-gate 			/* Some overlap with low addresses of next routine? */
8740Sstevel@tonic-gate 			if (pc10 < pc1) {
8750Sstevel@tonic-gate 					/* Yes. Get total count ... */
8760Sstevel@tonic-gate 				ticks = pcp[i1];
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 				/* and accumulate proportion for addresses in */
8790Sstevel@tonic-gate 				/*		range of this routine */
8800Sstevel@tonic-gate 				t += PROFSEC(((double)ticks *
8816951Sab196087 				    (pc1 - pc10)*sf)/bias);
8826951Sab196087 	/* BEGIN CSTYLED */
8830Sstevel@tonic-gate OLD_DEBUG(if (debug_value & 010) fprintf(stderr, "ticks = %d\n", ticks));
8840Sstevel@tonic-gate OLD_DEBUG(if (debug_value & 010)
8850Sstevel@tonic-gate 	Fprint(stderr, " + %ld/(%.1f)", (pc1 - pc10) * ticks, DBL_ADDRPERCELL)
8860Sstevel@tonic-gate );
8876951Sab196087 	/* END CSTYLED */
8880Sstevel@tonic-gate 			}
8890Sstevel@tonic-gate 		}		/* End if (i0 == i1) ... else ... */
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 		slp[n].sl_time = t;	/* Store time for this routine. */
8920Sstevel@tonic-gate 		t0 += t;		/* Accumulate total time. */
8930Sstevel@tonic-gate OLD_DEBUG(if (debug_value & 010) Fprint(stderr, " ticks = %.2f msec\n", t));
8940Sstevel@tonic-gate 	}	/* End for (n = 0; n < n_syms; n++) */
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate 	/* Final pass to total up time. */
8970Sstevel@tonic-gate 	/* Sum ticks, then convert to seconds. */
8980Sstevel@tonic-gate 
8996951Sab196087 	for (n = n_pc, temp = 0; --n >= 0; temp += *(pcp++))
9006951Sab196087 		;
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 	t_tot = PROFSEC(temp);
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	/*
9050Sstevel@tonic-gate 	 * Now, whilst we still have the symbols sorted
9060Sstevel@tonic-gate 	 * in address order..
9070Sstevel@tonic-gate 	 * Loop to record duplicates, so we can display
9080Sstevel@tonic-gate 	 * synonym symbols correctly.
9090Sstevel@tonic-gate 	 * Synonym symbols, or symbols with the same address,
9100Sstevel@tonic-gate 	 * are to be displayed by prof on the same line, with
9110Sstevel@tonic-gate 	 * one statistics line, as below:
9120Sstevel@tonic-gate 	 *			... 255  ldaopen, ldaopen
9130Sstevel@tonic-gate 	 * The way this will be implemented, is as follows:
9140Sstevel@tonic-gate 	 *
9150Sstevel@tonic-gate 	 * Pass 1 - while the symbols are in address order, we
9160Sstevel@tonic-gate 	 *  do a pre-pass through them, to determine for which
9170Sstevel@tonic-gate 	 *  addresses there are more than one symbol (i.e. synonyms).
9180Sstevel@tonic-gate 	 *  During this prepass we collect summary statistics in
9190Sstevel@tonic-gate 	 *  the synonym entry, for all the synonyms.
9200Sstevel@tonic-gate 	 *
9210Sstevel@tonic-gate 	 * 'Pass' 2 - while printing a report,  for each report line,
9220Sstevel@tonic-gate 	 *  if the current symbol is a synonym symbol (i.e. in the
9230Sstevel@tonic-gate 	 *  snymList) then we scan forward and pick up all the names
9240Sstevel@tonic-gate 	 *  which map to this address, and print them too.
9250Sstevel@tonic-gate 	 *  If the address' synonyms have already been printed, then
9260Sstevel@tonic-gate 	 *  we just skip this symbol and go on to process the next.
9270Sstevel@tonic-gate 	 *
9280Sstevel@tonic-gate 	 */
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate 	{
9310Sstevel@tonic-gate 	/* pass 1 */
9320Sstevel@tonic-gate 	char *thisaddr;
9330Sstevel@tonic-gate 	char *lastaddr = slist->sl_addr; /* use 1st sym as */
9340Sstevel@tonic-gate 					/* 'last/prior symbol' */
9350Sstevel@tonic-gate 	int lastWasSnym = 0;	/* 1st can't be snym yet-no aliases seen! */
9360Sstevel@tonic-gate 	int thisIsSnym;
9370Sstevel@tonic-gate 
9386951Sab196087 	/* BEGIN CSTYLED */
9390Sstevel@tonic-gate OLD_DEBUG(
9400Sstevel@tonic-gate int totsnyms = 0; int totseries = 0; struct slist *lastslp = slist;
9410Sstevel@tonic-gate );
9426951Sab196087 	/* END CSTYLED */
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	/* NB loop starts with 2nd symbol, loops over n_syms-1 symbols! */
9450Sstevel@tonic-gate 	for (n = n_syms-1, slp = slist+1; --n >= 0; slp++) {
9460Sstevel@tonic-gate 		thisaddr = slp->sl_addr;
9470Sstevel@tonic-gate 		thisIsSnym = (thisaddr == lastaddr);
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 		if (thisIsSnym) {
9500Sstevel@tonic-gate 			/* gotta synonym */
9510Sstevel@tonic-gate 			if (!lastWasSnym) {
9526951Sab196087 	/* BEGIN CSTYLED */
9530Sstevel@tonic-gate OLD_DEBUG(
9540Sstevel@tonic-gate if (debug_value)  {
9550Sstevel@tonic-gate 	Fprint(stderr,
9560Sstevel@tonic-gate 		"Synonym series:\n1st->\t%s at address %x, ct=%ld, time=%f\n",
9570Sstevel@tonic-gate 		lastslp->sl_name, lastaddr, lastslp->sl_count,
9580Sstevel@tonic-gate 		lastslp->sl_time);
9590Sstevel@tonic-gate 	totseries++;
9600Sstevel@tonic-gate 	totsnyms++;
9610Sstevel@tonic-gate }
9620Sstevel@tonic-gate );
9636951Sab196087 	/* END CSTYLED */
9640Sstevel@tonic-gate 				/* this is the Second! of a series */
9650Sstevel@tonic-gate 				snymp = (n_snyms++ == 0 ? snymList : snymp+1);
9660Sstevel@tonic-gate 				snymp->howMany = 1; /* gotta count 1st one!! */
9670Sstevel@tonic-gate 				snymp->sym_addr = slp->sl_addr;
9680Sstevel@tonic-gate 				/* zero summary statistics */
9690Sstevel@tonic-gate 				snymp->tot_sl_count = 0;
9700Sstevel@tonic-gate 				snymp->tot_sl_time = 0.0;
9710Sstevel@tonic-gate 				/* Offen the Reported flag */
9720Sstevel@tonic-gate 				snymp->snymReported = 0;
9730Sstevel@tonic-gate 			}
9746951Sab196087 	/* BEGIN CSTYLED */
9750Sstevel@tonic-gate OLD_DEBUG(
9760Sstevel@tonic-gate if (debug_value)  {
9770Sstevel@tonic-gate 	Fprint(stderr,
9780Sstevel@tonic-gate 		"\t%s at address %x, ct=%ld, time=%f\n",
9790Sstevel@tonic-gate 		slp->sl_name,
9800Sstevel@tonic-gate 		thisaddr,
9810Sstevel@tonic-gate 		slp->sl_count,
9820Sstevel@tonic-gate 		slp->sl_time);
9830Sstevel@tonic-gate 	totsnyms++;
9840Sstevel@tonic-gate }
9850Sstevel@tonic-gate );
9866951Sab196087 	/* END CSTYLED */
9870Sstevel@tonic-gate 			/* ok - bump count for snym, and note its Finding */
9880Sstevel@tonic-gate 			snymp->howMany++;
9890Sstevel@tonic-gate 			/* and update the summary statistics */
9900Sstevel@tonic-gate 			snymp->tot_sl_count += slp->sl_count;
9910Sstevel@tonic-gate 			snymp->tot_sl_time += slp->sl_time;
9920Sstevel@tonic-gate 		}
9930Sstevel@tonic-gate 		callTotal += slp->sl_count;
9940Sstevel@tonic-gate 		lastaddr = thisaddr;
9950Sstevel@tonic-gate 		lastWasSnym = thisIsSnym;
9966951Sab196087 	/* BEGIN CSTYLED */
9970Sstevel@tonic-gate OLD_DEBUG(
9980Sstevel@tonic-gate if (debug_value) lastslp = slp;
9990Sstevel@tonic-gate );
10006951Sab196087 	/* END CSTYLED */
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 	}
10036951Sab196087 	/* BEGIN CSTYLED */
10040Sstevel@tonic-gate OLD_DEBUG(
10050Sstevel@tonic-gate if (debug_value)  {
10060Sstevel@tonic-gate 	Fprint(stderr, "Total #series %d, #synonyms %d\n", totseries, totsnyms);
10070Sstevel@tonic-gate }
10080Sstevel@tonic-gate );
10096951Sab196087 	/* END CSTYLED */
10100Sstevel@tonic-gate 	}
10110Sstevel@tonic-gate 	/*
10120Sstevel@tonic-gate 	 * Most of the heavy work is done now.  Only minor stuff remains.
10130Sstevel@tonic-gate 	 * The symbols are currently in address order and must be re-sorted
10140Sstevel@tonic-gate 	 * if desired in a different order.  Report generating options
10150Sstevel@tonic-gate 	 * include "-o" or "-x": Include symbol address, which causes
10160Sstevel@tonic-gate 	 * another column
10170Sstevel@tonic-gate 	 * in the output; and "-z": Include symbols in report even if zero
10180Sstevel@tonic-gate 	 * time and call count.  Symbols not in profiling range are excluded
10190Sstevel@tonic-gate 	 * in any case.  Following the main body of the report, the "-s"
10200Sstevel@tonic-gate 	 * option causes certain additional information to be printed.
10210Sstevel@tonic-gate 	 */
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate OLD_DEBUG(if (debug_value) Fprint(stderr,
10240Sstevel@tonic-gate "Time unaccounted for: %.7G\n", t_tot - t0));
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 	if (sort)	/* If comparison routine given then use it. */
10270Sstevel@tonic-gate 		qsort((char *)slist, (unsigned)n_syms,
10286951Sab196087 		    sizeof (struct slist), sort);
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate 	if (!(flags & F_NHEAD)) {
10310Sstevel@tonic-gate 		if (flags & F_PADDR)
1032211Smike_s 			Print("%s", atitle);	/* Title for addresses. */
10330Sstevel@tonic-gate 		(void) puts(" %Time Seconds Cumsecs  #Calls   msec/call  Name");
10340Sstevel@tonic-gate 	}
10350Sstevel@tonic-gate 	t = 0.0;			/* Init cumulative time. */
10360Sstevel@tonic-gate 	if (t_tot != 0.0)		/* Convert to percent. */
10370Sstevel@tonic-gate 		t_tot = 100.0/t_tot;	/* Prevent divide-by-zero fault */
10380Sstevel@tonic-gate 	n_nonzero = 0;	/* Number of symbols with nonzero time or # calls. */
10390Sstevel@tonic-gate 	for (n = n_syms, slp = slist; --n >= 0; slp++) {
10400Sstevel@tonic-gate 		long count;	 /* # Calls. */
10410Sstevel@tonic-gate 		/* t0, time in seconds. */
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 		/* if a snym symbol, use summarized stats, else use indiv. */
10440Sstevel@tonic-gate 		if ((snymp = getSnymEntry(slp->sl_addr)) != 0) {
10450Sstevel@tonic-gate 			count = snymp->tot_sl_count;
10460Sstevel@tonic-gate 			t0 = snymp->tot_sl_time;
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 		} else {
10490Sstevel@tonic-gate 			count = slp->sl_count;
10500Sstevel@tonic-gate 			t0 = slp->sl_time;
10510Sstevel@tonic-gate 		}
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 		/* if a snym and already reported, skip this entry */
10540Sstevel@tonic-gate 		if (snymp && snymp->snymReported)
10550Sstevel@tonic-gate 			continue;
10560Sstevel@tonic-gate 		/* Don't do entries with no action. */
10570Sstevel@tonic-gate 		if (t0 == 0.0 && count == 0 && !(flags & F_ZSYMS))
10580Sstevel@tonic-gate 			continue;
10590Sstevel@tonic-gate 		if ((strcmp(slp->sl_name, "_mcount") == 0) ||
10600Sstevel@tonic-gate 		    (strcmp(slp->sl_name, "mcount") == 0)) {
10616951Sab196087 			count = callTotal;
10620Sstevel@tonic-gate 		}
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 		/* count number of entries (i.e. symbols) printed */
10650Sstevel@tonic-gate 		if (snymp)
10660Sstevel@tonic-gate 			n_nonzero += snymp->howMany; /* add for each snym */
10670Sstevel@tonic-gate 		else
10680Sstevel@tonic-gate 			n_nonzero++;
10690Sstevel@tonic-gate 
1070211Smike_s 		if (flags & F_PADDR) { /* Printing address of symbol? */
1071211Smike_s 			/* LINTED: variable format */
10720Sstevel@tonic-gate 			Print(aformat, slp->sl_addr);
1073211Smike_s 		}
10740Sstevel@tonic-gate 		t += t0;	/*  move here; compiler bug  !! */
10750Sstevel@tonic-gate 		Print("%6.1f%8.2f%8.2f", t0 * t_tot, t0, t);
10760Sstevel@tonic-gate 		fdigits = 0;
10770Sstevel@tonic-gate 		if (count) {		/* Any calls recorded? */
10780Sstevel@tonic-gate 		/* Get reasonable number of fractional digits to print. */
10790Sstevel@tonic-gate 			fdigits = fprecision(count);
10800Sstevel@tonic-gate 			Print("%8ld%#*.*f", count, fdigits+8, fdigits,
10810Sstevel@tonic-gate 			    1000.0*t0/count);
10820Sstevel@tonic-gate 			Print("%*s", 6-fdigits, " ");
10830Sstevel@tonic-gate 		} else {
10840Sstevel@tonic-gate 			Print("%22s", " ");
10850Sstevel@tonic-gate 		}
10860Sstevel@tonic-gate 		/*
10870Sstevel@tonic-gate 		 * now print the name (or comma-seperate list of names,
10880Sstevel@tonic-gate 		 * for synonym symbols).
10890Sstevel@tonic-gate 		 */
10900Sstevel@tonic-gate 		if (snymp) {
10910Sstevel@tonic-gate 			printSnymNames(slp, snymp);	/* print it, then */
10920Sstevel@tonic-gate 			snymp->snymReported = 1;	/* mark it Done */
10930Sstevel@tonic-gate 		}
10940Sstevel@tonic-gate 		else
10950Sstevel@tonic-gate 			(void) puts(slp->sl_name);	/* print the one name */
10960Sstevel@tonic-gate 	}
10970Sstevel@tonic-gate 	if (flags & F_VERBOSE) {		/* Extra info? */
10980Sstevel@tonic-gate 		Fprint(stderr, "%5d/%d call counts used\n", n_cc, head.nfns);
10990Sstevel@tonic-gate 		Fprint(stderr, "%5d/%d symbols qualified", n_syms, symttl);
11000Sstevel@tonic-gate 		if (n_nonzero < n_syms)
11010Sstevel@tonic-gate 			Fprint(stderr,
11020Sstevel@tonic-gate 			    ", %d had zero time and zero call-counts\n",
11030Sstevel@tonic-gate 			    n_syms - n_nonzero);
11040Sstevel@tonic-gate 		else
11050Sstevel@tonic-gate 			(void) putc('\n', stderr);
1106211Smike_s 		Fprint(stderr, "%#lx scale factor\n", (long)sf);
11070Sstevel@tonic-gate 	}
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 	_symintClose(ldptr);
11100Sstevel@tonic-gate 	} else {
11116951Sab196087 		Fprint(stderr, "prof: no call counts captured\n");
11120Sstevel@tonic-gate 	}
1113211Smike_s 	return (0);
11140Sstevel@tonic-gate }
11150Sstevel@tonic-gate /* Return size of file associated with file descriptor fd. */
11160Sstevel@tonic-gate 
1117211Smike_s static off_t
fsize(int fd)1118211Smike_s fsize(int fd)
11190Sstevel@tonic-gate {
11200Sstevel@tonic-gate 	struct stat sbuf;
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 	if (fstat(fd, &sbuf) < 0)  /* Status of open file. */
11230Sstevel@tonic-gate 		Perror("stat");
11240Sstevel@tonic-gate 	return (sbuf.st_size);			/* This is a long. */
11250Sstevel@tonic-gate }
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate /* Read symbol entry. Return TRUE if satisfies conditions. */
11280Sstevel@tonic-gate 
1129211Smike_s static int
readnl(int symindex)1130211Smike_s readnl(int symindex)
11310Sstevel@tonic-gate {
11320Sstevel@tonic-gate 	nl = ldptr->pf_symarr_p[symindex];
11330Sstevel@tonic-gate 
11346951Sab196087 	/* BEGIN CSTYLED */
11350Sstevel@tonic-gate OLD_DEBUG(
11360Sstevel@tonic-gate 	if (debug_value & 020) {
11370Sstevel@tonic-gate 		Fprint(stderr,
11380Sstevel@tonic-gate 			"`%-8.8s'\tst_info=%#4o, value=%#8.6o\n",
11390Sstevel@tonic-gate 			ldptr->pf_symstr_p[nl.ps_sym.st_name],
11400Sstevel@tonic-gate 			(unsigned char) nl.ps_sym.st_info,
11410Sstevel@tonic-gate 			nl.ps_sym.st_value);
11420Sstevel@tonic-gate 	}
11430Sstevel@tonic-gate );
11446951Sab196087 	/* END CSTYLED */
11456951Sab196087 
11460Sstevel@tonic-gate 	/*
11470Sstevel@tonic-gate 	 * TXTSYM accepts global (and local, if "-g" given) T-type symbols.
11480Sstevel@tonic-gate 	 * Only those in the profiling range are useful.
11490Sstevel@tonic-gate 	 */
11500Sstevel@tonic-gate 	return (nl.ps_sym.st_shndx < SHN_LORESERVE &&
11516951Sab196087 	    TXTSYM(nl.ps_sym.st_shndx, nl.ps_sym.st_info) &&
11526951Sab196087 	    (pc_l <= (char *)nl.ps_sym.st_value) &&
11536951Sab196087 	    ((char *)nl.ps_sym.st_value < pc_h));
11540Sstevel@tonic-gate }
11550Sstevel@tonic-gate /*
11560Sstevel@tonic-gate  * Error-checking memory allocators -
11570Sstevel@tonic-gate  * Guarantees good return (else none at all).
11580Sstevel@tonic-gate  */
11590Sstevel@tonic-gate 
1160211Smike_s static void *
_prof_Malloc(int item_count,int item_size)1161211Smike_s _prof_Malloc(int item_count, int item_size)
11620Sstevel@tonic-gate {
1163211Smike_s 	void *p;
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 	if ((p = malloc((unsigned)item_count * (unsigned)item_size)) == NULL)  {
11660Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: Out of space\n", cmdname);
11670Sstevel@tonic-gate 		exit(1);
11680Sstevel@tonic-gate 	}
11690Sstevel@tonic-gate 	return (p);
11700Sstevel@tonic-gate }
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate /*
11750Sstevel@tonic-gate  *	Given the quotient Q = N/D, where entier(N) == N and D > 0, an
11760Sstevel@tonic-gate  *	approximation of the "best" number of fractional digits to use
11770Sstevel@tonic-gate  *	in printing Q is f = entier(log10(D)), which is crudely produced
11780Sstevel@tonic-gate  *	by the following routine.
11790Sstevel@tonic-gate  */
11800Sstevel@tonic-gate 
1181211Smike_s static int
fprecision(long count)1182211Smike_s fprecision(long count)
11830Sstevel@tonic-gate {
11840Sstevel@tonic-gate 	return (count < 10 ? 0 : count < 100 ? 1 : count < 1000 ? 2 :
11850Sstevel@tonic-gate 	    count < 10000 ? 3 : 4);
11860Sstevel@tonic-gate }
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate /*
11890Sstevel@tonic-gate  *	Return pointer to base name(name less path) of string s.
11900Sstevel@tonic-gate  *	Handles case of superfluous trailing '/'s, and unlikely
11910Sstevel@tonic-gate  *	case of s == "/".
11920Sstevel@tonic-gate  */
11930Sstevel@tonic-gate 
1194211Smike_s static char *
basename(char * s)1195211Smike_s basename(char *s)
11960Sstevel@tonic-gate {
1197211Smike_s 	char *p;
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 	p = &s[strlen(s)];			/* End (+1) of string. */
12000Sstevel@tonic-gate 	while (p > s && *--p == '/')		/* Trim trailing '/'s. */
12010Sstevel@tonic-gate 		*p = '\0';
12020Sstevel@tonic-gate 	p++;					/* New end (+1) of string. */
12036951Sab196087 	while (p > s && *--p != '/')		/* Break backward on '/'. */
12046951Sab196087 		;
12050Sstevel@tonic-gate 	if (*p == '/')		/* If found '/', point to 1st following. */
12060Sstevel@tonic-gate 		p++;
12070Sstevel@tonic-gate 	if (*p == '\0')
12080Sstevel@tonic-gate 		p = "/";			/* If NULL, must be "/". (?) */
12090Sstevel@tonic-gate 	return (p);
12100Sstevel@tonic-gate }
12110Sstevel@tonic-gate /* Here if unexpected read problem. */
12120Sstevel@tonic-gate 
1213211Smike_s static void
eofon(FILE * iop,char * fn)1214211Smike_s eofon(FILE *iop, char *fn)
12150Sstevel@tonic-gate {
12160Sstevel@tonic-gate 	if (ferror(iop))		/* Real error? */
12170Sstevel@tonic-gate 		Perror(fn);		/* Yes. */
12180Sstevel@tonic-gate 	Fprint(stderr, "%s: %s: Premature EOF\n", cmdname, fn);
12190Sstevel@tonic-gate 	exit(1);
12200Sstevel@tonic-gate }
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate /* Version of perror() that prints cmdname first. */
12230Sstevel@tonic-gate 
1224211Smike_s static void
Perror(char * s)1225211Smike_s Perror(char *s)
12260Sstevel@tonic-gate {				/* Print system error message & exit. */
1227211Smike_s 	int err = errno;	/* Save current errno in case */
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate 	Fprint(stderr, "%s: ", cmdname);
12300Sstevel@tonic-gate 	errno = err;			/* Put real error back. */
12310Sstevel@tonic-gate 	perror(s);			/* Print message. */
12320Sstevel@tonic-gate 	_symintClose(ldptr);		/* cleanup symbol information */
12330Sstevel@tonic-gate 	exit(1);			/* Exit w/nonzero status. */
12340Sstevel@tonic-gate }
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate /* Here for things that "Should Never Happen". */
12370Sstevel@tonic-gate 
1238211Smike_s static void
snh(void)1239211Smike_s snh(void)
12400Sstevel@tonic-gate {
12410Sstevel@tonic-gate 	Fprint(stderr, "%s: Internal error\n", cmdname);
12420Sstevel@tonic-gate 	(void) abort();
12430Sstevel@tonic-gate }
1244211Smike_s 
12450Sstevel@tonic-gate /*
12460Sstevel@tonic-gate  *	Various comparison routines for qsort. Uses:
12470Sstevel@tonic-gate  *
12480Sstevel@tonic-gate  *	c_ccaddr	- Compare fnpc fields of cnt structs to put
12490Sstevel@tonic-gate  *				call counters in increasing address order.
12500Sstevel@tonic-gate  *	c_sladdr	- Sort slist structures on increasing address.
12510Sstevel@tonic-gate  *	c_time		-  "	 "	  "      " decreasing time.
12520Sstevel@tonic-gate  *	c_ncalls	-  "	 "	  "      " decreasing # calls.
12530Sstevel@tonic-gate  *	c_name		-  "	 "	  "      " increasing symbol name
12540Sstevel@tonic-gate  */
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate #define	CMP2(v1, v2)	((v1) < (v2) ? -1 : (v1) == (v2) ? 0 : 1)
12570Sstevel@tonic-gate #define	CMP1(v)		CMP2(v, 0)
12580Sstevel@tonic-gate 
1259211Smike_s int
c_ccaddr(const void * arg1,const void * arg2)1260211Smike_s c_ccaddr(const void *arg1, const void *arg2)
12610Sstevel@tonic-gate {
1262211Smike_s 	struct cnt *p1 = (struct cnt *)arg1;
1263211Smike_s 	struct cnt *p2 = (struct cnt *)arg2;
1264211Smike_s 
12650Sstevel@tonic-gate 	return (CMP2(p1->fnpc, p2->fnpc));
12660Sstevel@tonic-gate }
12670Sstevel@tonic-gate 
1268211Smike_s int
c_sladdr(const void * arg1,const void * arg2)1269211Smike_s c_sladdr(const void *arg1, const void *arg2)
12700Sstevel@tonic-gate {
1271211Smike_s 	struct slist *p1 = (struct slist *)arg1;
1272211Smike_s 	struct slist *p2 = (struct slist *)arg2;
1273211Smike_s 
12740Sstevel@tonic-gate 	return (CMP2(p1->sl_addr, p2->sl_addr));
12750Sstevel@tonic-gate }
12760Sstevel@tonic-gate 
1277211Smike_s int
c_time(const void * arg1,const void * arg2)1278211Smike_s c_time(const void *arg1, const void *arg2)
12790Sstevel@tonic-gate {
1280211Smike_s 	struct slist *p1 = (struct slist *)arg1;
1281211Smike_s 	struct slist *p2 = (struct slist *)arg2;
1282211Smike_s 	float dtime = p2->sl_time - p1->sl_time; /* Decreasing time. */
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate 	return (CMP1(dtime));
12850Sstevel@tonic-gate }
12860Sstevel@tonic-gate 
1287211Smike_s int
c_ncalls(const void * arg1,const void * arg2)1288211Smike_s c_ncalls(const void *arg1, const void *arg2)
12890Sstevel@tonic-gate {
1290211Smike_s 	struct slist *p1 = (struct slist *)arg1;
1291211Smike_s 	struct slist *p2 = (struct slist *)arg2;
1292211Smike_s 	int diff = p2->sl_count - p1->sl_count;
12930Sstevel@tonic-gate 		/* Decreasing # calls. */
12940Sstevel@tonic-gate 	return (CMP1(diff));
12950Sstevel@tonic-gate }
12960Sstevel@tonic-gate 
1297211Smike_s int
c_name(const void * arg1,const void * arg2)1298211Smike_s c_name(const void *arg1, const void *arg2)
12990Sstevel@tonic-gate {
1300211Smike_s 	struct slist *p1 = (struct slist *)arg1;
1301211Smike_s 	struct slist *p2 = (struct slist *)arg2;
1302211Smike_s 	int diff;
13030Sstevel@tonic-gate 
13040Sstevel@tonic-gate 		/* flex names has variable length strings for names */
13050Sstevel@tonic-gate 	diff = strcmp(p1->sl_name, p2->sl_name);
13060Sstevel@tonic-gate 	return (CMP1(diff));
13070Sstevel@tonic-gate }
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate #define	STRSPACE 2400		/* guess at amount of string space */
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate char *format_buf;
13120Sstevel@tonic-gate #define	FORMAT_BUF	"%s\n\t\t\t\t\t    [%s]"
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate static char *
demangled_name(char * s)1315211Smike_s demangled_name(char *s)
13160Sstevel@tonic-gate {
1317*7008Sab196087 	const char *name;
1318211Smike_s 	size_t	len;
13190Sstevel@tonic-gate 
1320*7008Sab196087 	name = conv_demangle_name(s);
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 	if (strcmp(name, s) == 0)
13230Sstevel@tonic-gate 		return (s);
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 	if (format_buf != NULL)
13260Sstevel@tonic-gate 		free(format_buf);
13270Sstevel@tonic-gate 
1328211Smike_s 	len = strlen(name) + strlen(FORMAT_BUF) + strlen(s) + 1;
1329211Smike_s 	format_buf = malloc(len);
13300Sstevel@tonic-gate 	if (format_buf == NULL)
13310Sstevel@tonic-gate 		return (s);
1332211Smike_s 	(void) snprintf(format_buf, len, FORMAT_BUF, name, s);
13330Sstevel@tonic-gate 	return (format_buf);
13340Sstevel@tonic-gate }
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate /* getname - get the name of a symbol in a permanent fashion */
1337211Smike_s static char *
getname(PROF_FILE * ldpter,PROF_SYMBOL symbol)1338211Smike_s getname(PROF_FILE *ldpter, PROF_SYMBOL symbol)
13390Sstevel@tonic-gate {
13400Sstevel@tonic-gate 	static char *strtable = NULL;	/* space for names */
13410Sstevel@tonic-gate 	static int sp_used = 0;		/* space used so far */
13420Sstevel@tonic-gate 	static int size = 0;		/* size of string table */
1343211Smike_s 	char *name;			/* name to store */
13440Sstevel@tonic-gate 	int lth;			/* space needed for name */
13450Sstevel@tonic-gate 	int get;			/* amount of space to get */
13460Sstevel@tonic-gate 
13473621Sab196087 	name = elf_strptr(ldpter->pf_elf_p, ldpter->pf_symstr_ndx,
13483621Sab196087 	    symbol.ps_sym.st_name);
13493621Sab196087 	if (name == NULL)
13500Sstevel@tonic-gate 		return ("<<bad symbol name>>");
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate 	if (Cflag)
1353211Smike_s 		name = demangled_name(name);
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate 	lth = strlen(name) + 1;
13560Sstevel@tonic-gate 	if ((sp_used + lth) > size)  {	 /* if need more space */
13570Sstevel@tonic-gate 		/* just in case very long name */
13580Sstevel@tonic-gate 		get = lth > STRSPACE ? lth : STRSPACE;
13590Sstevel@tonic-gate 		strtable = _prof_Malloc(1, get);
13600Sstevel@tonic-gate 		size = get;
13610Sstevel@tonic-gate 		sp_used = 0;
13620Sstevel@tonic-gate 	}
13630Sstevel@tonic-gate 	(void) strcpy(&(strtable[sp_used]), name);
13640Sstevel@tonic-gate 	name = &(strtable[sp_used]);
13650Sstevel@tonic-gate 	sp_used += lth;
13660Sstevel@tonic-gate 	return (name);
13670Sstevel@tonic-gate }
13680Sstevel@tonic-gate 
1369211Smike_s static void
usage(void)1370211Smike_s usage(void)
13710Sstevel@tonic-gate {
1372211Smike_s 	(void) fprintf(stderr,
1373211Smike_s 	    "usage: %s [-ChsVz] [-a | c | n | t]  [-o  |  x]   [-g  |  l]\n"
1374211Smike_s 	    "\t[-m mdata] [prog]\n",
1375211Smike_s 	    cmdname);
1376211Smike_s 	exit(1);
13770Sstevel@tonic-gate }
1378