xref: /onnv-gate/usr/src/cmd/sgs/ldprof/common/profile.c (revision 12877:69001e4756ae)
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
53731Srie  * Common Development and Distribution License (the "License").
63731Srie  * 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  */
216812Sraf 
220Sstevel@tonic-gate /*
23*12877SRod.Evans@Sun.COM  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
256812Sraf 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Routines to provide profiling of shared libraries required by the called
280Sstevel@tonic-gate  * executable.
290Sstevel@tonic-gate  */
306812Sraf #include <stdio.h>
316812Sraf #include <fcntl.h>
326812Sraf #include <sys/mman.h>
336812Sraf #include <unistd.h>
346812Sraf #include <stdlib.h>
356812Sraf #include <string.h>
366812Sraf #include <sys/types.h>
376812Sraf #include <sys/stat.h>
386812Sraf #include <synch.h>
396812Sraf #include <signal.h>
406812Sraf #include <synch.h>
416812Sraf #include <link.h>
426812Sraf #include <libintl.h>
436812Sraf #include <sys/param.h>
446812Sraf #include <procfs.h>
456812Sraf #include "msg.h"
466812Sraf #include "sgs.h"
476812Sraf #include "profile.h"
486812Sraf #include "_rtld.h"
490Sstevel@tonic-gate 
500Sstevel@tonic-gate 
510Sstevel@tonic-gate static char	Profile[MAXPATHLEN];	/* Profile buffer pathname */
520Sstevel@tonic-gate static char	*pname = 0;		/* name of object to profile */
530Sstevel@tonic-gate static L_hdr	*Hptr;			/* profile buffer header pointer */
540Sstevel@tonic-gate static L_cgarc	*Cptr;			/* profile buffer call graph pointer */
550Sstevel@tonic-gate static caddr_t	Hpc, Lpc;		/* Range of addresses being monitored */
560Sstevel@tonic-gate static size_t	Fsize;			/* Size of mapped in profile buffer */
570Sstevel@tonic-gate uintptr_t	profcookie = 0;
580Sstevel@tonic-gate 
590Sstevel@tonic-gate /*
600Sstevel@tonic-gate  * When handling mutex's locally we need to mask signals.  The signal
610Sstevel@tonic-gate  * mask is for everything except SIGWAITING.
620Sstevel@tonic-gate  */
630Sstevel@tonic-gate static const sigset_t	iset = { ~0U, ~0U, ~0U, ~0U };
640Sstevel@tonic-gate 
650Sstevel@tonic-gate static lwp_mutex_t sharedmutex = SHAREDMUTEX;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate static int
prof_mutex_init(lwp_mutex_t * mp)680Sstevel@tonic-gate prof_mutex_init(lwp_mutex_t *mp)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate 	(void) memcpy(mp, &sharedmutex, sizeof (lwp_mutex_t));
710Sstevel@tonic-gate 	return (0);
720Sstevel@tonic-gate }
730Sstevel@tonic-gate 
740Sstevel@tonic-gate static int
prof_mutex_lock(lwp_mutex_t * mp,sigset_t * oset)750Sstevel@tonic-gate prof_mutex_lock(lwp_mutex_t *mp, sigset_t *oset)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate 	if (oset)
780Sstevel@tonic-gate 		(void) sigprocmask(SIG_BLOCK, &iset, oset);
790Sstevel@tonic-gate 	(void) _lwp_mutex_lock(mp);
800Sstevel@tonic-gate 	return (0);
810Sstevel@tonic-gate }
820Sstevel@tonic-gate 
830Sstevel@tonic-gate static int
prof_mutex_unlock(mutex_t * mp,sigset_t * oset)840Sstevel@tonic-gate prof_mutex_unlock(mutex_t *mp, sigset_t *oset)
850Sstevel@tonic-gate {
860Sstevel@tonic-gate 	(void) _lwp_mutex_unlock(mp);
870Sstevel@tonic-gate 	if (oset)
880Sstevel@tonic-gate 		(void) sigprocmask(SIG_SETMASK, oset, NULL);
890Sstevel@tonic-gate 	return (0);
900Sstevel@tonic-gate }
910Sstevel@tonic-gate 
920Sstevel@tonic-gate const char *
_ldprof_msg(Msg mid)930Sstevel@tonic-gate _ldprof_msg(Msg mid)
940Sstevel@tonic-gate {
956812Sraf 	return (dgettext(MSG_ORIG(MSG_SUNW_OST_SGS), MSG_ORIG(mid)));
960Sstevel@tonic-gate }
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate  * Determine whether a set (of arbitrary size) is in use - used to analyze proc
1000Sstevel@tonic-gate  * status information.
1010Sstevel@tonic-gate  */
1020Sstevel@tonic-gate static int
setisinuse(uint32_t * sp,uint_t n)1030Sstevel@tonic-gate setisinuse(uint32_t *sp, uint_t n)
1040Sstevel@tonic-gate {
1050Sstevel@tonic-gate 	while (n--)
1060Sstevel@tonic-gate 		if (*sp++)
1070Sstevel@tonic-gate 			return (1);
1080Sstevel@tonic-gate 	return (0);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate #define	prisinuse(sp) \
1120Sstevel@tonic-gate 		setisinuse((uint32_t *)(sp), \
1130Sstevel@tonic-gate 		    (uint_t)(sizeof (*(sp)) / sizeof (uint32_t)))
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate uint_t
la_version(uint_t version)1160Sstevel@tonic-gate la_version(uint_t version)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate 	int		fd;
1190Sstevel@tonic-gate 	ssize_t		num;
1200Sstevel@tonic-gate 	pstatus_t	status;
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	if (version < LAV_CURRENT) {
1230Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_GEN_AUDITVERSION),
1246812Sraf 		    LAV_CURRENT, version);
1250Sstevel@tonic-gate 		return (LAV_CURRENT);
1260Sstevel@tonic-gate 	}
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	/*
1290Sstevel@tonic-gate 	 * To reduce the potential for deadlock conditions that can arise from
1300Sstevel@tonic-gate 	 * being monitored (say by truss(1)) while setting a lock in the profile
1310Sstevel@tonic-gate 	 * buffer, determine if someone is monitoring us.  If so silently
1320Sstevel@tonic-gate 	 * disable profiling.
1330Sstevel@tonic-gate 	 */
1340Sstevel@tonic-gate 	if ((fd = open(MSG_ORIG(MSG_FMT_PROCSELF), O_RDONLY)) < 0)
1350Sstevel@tonic-gate 		return (LAV_CURRENT);
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	num = read(fd, &status, sizeof (status));
1380Sstevel@tonic-gate 	(void) close(fd);
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	if ((num != sizeof (status)) ||
1410Sstevel@tonic-gate 	    prisinuse(&status.pr_sigtrace) || prisinuse(&status.pr_flttrace) ||
1420Sstevel@tonic-gate 	    prisinuse(&status.pr_sysentry) || prisinuse(&status.pr_sysexit)) {
1430Sstevel@tonic-gate 		return (LAV_CURRENT);
1440Sstevel@tonic-gate 	}
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	/*
1470Sstevel@tonic-gate 	 * We're presently not being monitored (although there's no control of
1480Sstevel@tonic-gate 	 * someone attaching to us later), so retrieve the profile target name.
1490Sstevel@tonic-gate 	 */
1500Sstevel@tonic-gate 	if (dlinfo((void *)NULL, RTLD_DI_PROFILENAME, &pname) == -1)
1510Sstevel@tonic-gate 		(void) fprintf(stderr,  MSG_INTL(MSG_GEN_PROFNOTSET));
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	return (LAV_CURRENT);
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate int
profile_open(const char * fname,Link_map * lmp)1580Sstevel@tonic-gate profile_open(const char *fname, Link_map *lmp)
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate 	size_t		hsize;		/* struct hdr size */
1610Sstevel@tonic-gate 	size_t		psize;		/* profile histogram size */
1620Sstevel@tonic-gate 	size_t		csize;		/* call graph array size */
1630Sstevel@tonic-gate 	size_t		msize;		/* size of memory being profiled */
1640Sstevel@tonic-gate 	int		i, fd, fixed = 0;
1650Sstevel@tonic-gate 	caddr_t		lpc;
1660Sstevel@tonic-gate 	caddr_t		hpc;
1670Sstevel@tonic-gate 	caddr_t		addr;
1680Sstevel@tonic-gate 	struct stat	status;
1690Sstevel@tonic-gate 	int		new_buffer = 0;
1700Sstevel@tonic-gate 	sigset_t	mask;
1710Sstevel@tonic-gate 	int		err;
1720Sstevel@tonic-gate 	Ehdr *		ehdr;		/* ELF header for file */
1730Sstevel@tonic-gate 	Phdr *		phdr;		/* program headers for file */
1740Sstevel@tonic-gate 	Dyn *		dynp = 0;	/* Dynamic section */
1750Sstevel@tonic-gate 	Word		nsym = 0;	/* no. of symtab ntries */
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	if (*Profile == '\0') {
1780Sstevel@tonic-gate 		const char	*dir, *suf;
1790Sstevel@tonic-gate 		char		*tmp;
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 		/*
1820Sstevel@tonic-gate 		 * From the basename of the specified filename generate the
1830Sstevel@tonic-gate 		 * appropriate profile buffer name.  The profile file is created
1840Sstevel@tonic-gate 		 * if it does not already exist.
1850Sstevel@tonic-gate 		 */
1860Sstevel@tonic-gate 		if (((tmp = strrchr(fname, '/')) != 0) && (*(++tmp)))
1870Sstevel@tonic-gate 			fname = tmp;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate #if	defined(_ELF64)
1900Sstevel@tonic-gate 		suf = MSG_ORIG(MSG_SUF_PROFILE_64);
1910Sstevel@tonic-gate #else
1920Sstevel@tonic-gate 		suf = MSG_ORIG(MSG_SUF_PROFILE);
1930Sstevel@tonic-gate #endif
1940Sstevel@tonic-gate 		if (dlinfo((void *)NULL, RTLD_DI_PROFILEOUT, &dir) == -1)
1950Sstevel@tonic-gate 			dir = MSG_ORIG(MSG_PTH_VARTMP);
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 		(void) snprintf(Profile, MAXPATHLEN, MSG_ORIG(MSG_FMT_PROFILE),
1980Sstevel@tonic-gate 		    dir, fname, suf);
1990Sstevel@tonic-gate 	}
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	if ((fd = open(Profile, (O_RDWR | O_CREAT), 0666)) == -1) {
2020Sstevel@tonic-gate 		err = errno;
2030Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_SYS_OPEN), Profile,
2040Sstevel@tonic-gate 		    strerror(err));
2050Sstevel@tonic-gate 		return (0);
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	/*
2090Sstevel@tonic-gate 	 * Now we determine the valid pc range for this object.  The lpc is easy
2100Sstevel@tonic-gate 	 * (lmp->l_addr), to determine the hpc we must examine the Phdrs.
2110Sstevel@tonic-gate 	 */
2120Sstevel@tonic-gate 	lpc = hpc = (caddr_t)lmp->l_addr;
2130Sstevel@tonic-gate 	/* LINTED */
2140Sstevel@tonic-gate 	ehdr = (Ehdr *)lpc;
2150Sstevel@tonic-gate 	if (ehdr->e_phnum == 0) {
2160Sstevel@tonic-gate 		(void) close(fd);
2170Sstevel@tonic-gate 		return (0);
2180Sstevel@tonic-gate 	}
2190Sstevel@tonic-gate 	if (ehdr->e_type == ET_EXEC)
2200Sstevel@tonic-gate 		fixed = 1;
2210Sstevel@tonic-gate 	/* LINTED */
2220Sstevel@tonic-gate 	phdr = (Phdr *)(ehdr->e_phoff + lpc);
2230Sstevel@tonic-gate 	for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
2240Sstevel@tonic-gate 		caddr_t	_hpc;
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 		if (phdr->p_type == PT_DYNAMIC) {
2270Sstevel@tonic-gate 			dynp = (Dyn *)phdr->p_vaddr;
2280Sstevel@tonic-gate 			if (fixed == 0) {
2290Sstevel@tonic-gate 				dynp = (Dyn *)((unsigned long)dynp +
2306812Sraf 				    (unsigned long)lpc);
2310Sstevel@tonic-gate 			}
2320Sstevel@tonic-gate 			continue;
2330Sstevel@tonic-gate 		}
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 		if (phdr->p_type != PT_LOAD)
2360Sstevel@tonic-gate 			continue;
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 		_hpc = (caddr_t)(phdr->p_vaddr + phdr->p_memsz);
2390Sstevel@tonic-gate 		if (fixed == 0) {
2400Sstevel@tonic-gate 			_hpc = (caddr_t)((unsigned long)_hpc +
2416812Sraf 			    (unsigned long)lpc);
2420Sstevel@tonic-gate 		}
2430Sstevel@tonic-gate 		if (_hpc > hpc)
2440Sstevel@tonic-gate 			hpc = _hpc;
2450Sstevel@tonic-gate 	}
2460Sstevel@tonic-gate 	if (lpc == hpc) {
2470Sstevel@tonic-gate 		(void) close(fd);
2480Sstevel@tonic-gate 		return (0);
2490Sstevel@tonic-gate 	}
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	/*
2520Sstevel@tonic-gate 	 * In order to determine the number of symbols in the object scan the
2530Sstevel@tonic-gate 	 * dynamic section until we find the DT_HASH entry (hash[1] == symcnt).
2540Sstevel@tonic-gate 	 */
2550Sstevel@tonic-gate 	if (dynp) {
2560Sstevel@tonic-gate 		for (; dynp->d_tag != DT_NULL; dynp++) {
2570Sstevel@tonic-gate 			unsigned int	*hashp;
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 			if (dynp->d_tag != DT_HASH)
2600Sstevel@tonic-gate 				continue;
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 			hashp = (unsigned int *)dynp->d_un.d_ptr;
2630Sstevel@tonic-gate 			if (fixed == 0) {
2640Sstevel@tonic-gate 				hashp = (unsigned int *)((unsigned long)hashp +
2656812Sraf 				    (unsigned long)lpc);
2660Sstevel@tonic-gate 			}
2670Sstevel@tonic-gate 			nsym = hashp[1];
2680Sstevel@tonic-gate 			break;
2690Sstevel@tonic-gate 		}
2700Sstevel@tonic-gate 	}
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	/*
2730Sstevel@tonic-gate 	 * Determine the (minimum) size of the buffer to allocate
2740Sstevel@tonic-gate 	 */
2750Sstevel@tonic-gate 	Lpc = lpc = (caddr_t)PRF_ROUNDWN((long)lpc, sizeof (long));
2760Sstevel@tonic-gate 	Hpc = hpc = (caddr_t)PRF_ROUNDUP((long)hpc, sizeof (long));
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	hsize = sizeof (L_hdr);
2790Sstevel@tonic-gate 	msize = (size_t)(hpc - lpc);
2800Sstevel@tonic-gate 	psize = (size_t)PRF_ROUNDUP((msize / PRF_BARSIZE), sizeof (long));
2810Sstevel@tonic-gate 	csize = (nsym + 1) * PRF_CGINIT * sizeof (L_cgarc);
2820Sstevel@tonic-gate 	Fsize = (hsize + psize + csize);
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	/*
2850Sstevel@tonic-gate 	 * If the file size is zero (ie. we just created it), truncate it
2860Sstevel@tonic-gate 	 * to the minimum size.
2870Sstevel@tonic-gate 	 */
2880Sstevel@tonic-gate 	(void) fstat(fd, &status);
2890Sstevel@tonic-gate 	if (status.st_size == 0) {
2900Sstevel@tonic-gate 		if (ftruncate(fd, Fsize) == -1) {
2910Sstevel@tonic-gate 			err = errno;
2920Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_FTRUNC),
2930Sstevel@tonic-gate 			    Profile, strerror(err));
2940Sstevel@tonic-gate 			(void) close(fd);
2950Sstevel@tonic-gate 			return (0);
2960Sstevel@tonic-gate 		}
2970Sstevel@tonic-gate 		new_buffer++;
2980Sstevel@tonic-gate 	} else
2990Sstevel@tonic-gate 		Fsize = status.st_size;
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	/*
3020Sstevel@tonic-gate 	 * Map the file in.
3030Sstevel@tonic-gate 	 */
3040Sstevel@tonic-gate 	if ((addr = (caddr_t)mmap(0, Fsize, (PROT_READ | PROT_WRITE),
3050Sstevel@tonic-gate 	    MAP_SHARED, fd, 0)) == (char *)-1) {
3060Sstevel@tonic-gate 		err = errno;
3070Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_SYS_MMAP), Profile,
3080Sstevel@tonic-gate 		    strerror(err));
3090Sstevel@tonic-gate 		(void) close(fd);
3100Sstevel@tonic-gate 		return (0);
3110Sstevel@tonic-gate 	}
3120Sstevel@tonic-gate 	(void) close(fd);
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	/*
3150Sstevel@tonic-gate 	 * Initialize the remaining elements of the header.  All pc addresses
3160Sstevel@tonic-gate 	 * that are recorded are relative to zero thus allowing the recorded
3170Sstevel@tonic-gate 	 * entries to be correlated with the symbols in the original file,
3180Sstevel@tonic-gate 	 * and to compensate for any differences in where the file is mapped.
3190Sstevel@tonic-gate 	 * If the high pc address has been initialized from a previous run,
3200Sstevel@tonic-gate 	 * and the new entry is different from the original then a new library
3210Sstevel@tonic-gate 	 * must have been installed.  In this case bale out.
3220Sstevel@tonic-gate 	 */
3230Sstevel@tonic-gate 	/* LINTED */
3240Sstevel@tonic-gate 	Hptr = (L_hdr *)addr;
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	if (new_buffer)
3270Sstevel@tonic-gate 		(void) prof_mutex_init((lwp_mutex_t *)&Hptr->hd_mutex);
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	(void) prof_mutex_lock((mutex_t *)&Hptr->hd_mutex, &mask);
3300Sstevel@tonic-gate 	if (Hptr->hd_hpc) {
3310Sstevel@tonic-gate 		if (Hptr->hd_hpc != (caddr_t)(hpc - lpc)) {
3320Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_GEN_PROFSZCHG),
3330Sstevel@tonic-gate 			    Profile);
3340Sstevel@tonic-gate 			(void) prof_mutex_unlock((mutex_t *)&Hptr->
3350Sstevel@tonic-gate 			    hd_mutex, &mask);
3360Sstevel@tonic-gate 			(void) munmap((caddr_t)Hptr, Fsize);
3370Sstevel@tonic-gate 			return (0);
3380Sstevel@tonic-gate 		}
3390Sstevel@tonic-gate 	} else {
3400Sstevel@tonic-gate 		/*
3410Sstevel@tonic-gate 		 * Initialize the header information as we must have just
3420Sstevel@tonic-gate 		 * created the output file.
3430Sstevel@tonic-gate 		 */
3440Sstevel@tonic-gate 		Hptr->hd_magic = (unsigned int)PRF_MAGIC;
3450Sstevel@tonic-gate #if	defined(_ELF64)
3460Sstevel@tonic-gate 		Hptr->hd_version = (unsigned int)PRF_VERSION_64;
3470Sstevel@tonic-gate #else
3480Sstevel@tonic-gate 		Hptr->hd_version = (unsigned int)PRF_VERSION;
3490Sstevel@tonic-gate #endif
3500Sstevel@tonic-gate 		Hptr->hd_hpc = (caddr_t)(hpc - lpc);
3510Sstevel@tonic-gate 		/* LINTED */
3520Sstevel@tonic-gate 		Hptr->hd_psize = (unsigned int)psize;
3530Sstevel@tonic-gate 		/* LINTED */
3540Sstevel@tonic-gate 		Hptr->hd_fsize = (unsigned int)Fsize;
3550Sstevel@tonic-gate 		Hptr->hd_ncndx = nsym;
3560Sstevel@tonic-gate 		Hptr->hd_lcndx = (nsym + 1) * PRF_CGINIT;
3570Sstevel@tonic-gate 	}
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	(void) prof_mutex_unlock((mutex_t *)&Hptr->hd_mutex, &mask);
3600Sstevel@tonic-gate 	/* LINTED */
3610Sstevel@tonic-gate 	Cptr = (L_cgarc *)(addr + hsize + psize);
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	/*
3640Sstevel@tonic-gate 	 * Turn on profiling
3650Sstevel@tonic-gate 	 */
3660Sstevel@tonic-gate 	/* LINTED */
3670Sstevel@tonic-gate 	profil((unsigned short *)(addr + hsize),
3686812Sraf 	    psize, (unsigned long)lpc, (unsigned int) PRF_SCALE);
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	return (1);
3710Sstevel@tonic-gate }
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate uint_t
3750Sstevel@tonic-gate /* ARGSUSED1 */
la_objopen(Link_map * lmp,Lmid_t lmid,uintptr_t * cookie)3760Sstevel@tonic-gate la_objopen(Link_map *lmp, Lmid_t lmid, uintptr_t *cookie)
3770Sstevel@tonic-gate {
3780Sstevel@tonic-gate 	char	*objname;
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	/*
3810Sstevel@tonic-gate 	 * This would only occur if the getenv() in la_version() failed.
3820Sstevel@tonic-gate 	 * at this point there is nothing for us to do.
3830Sstevel@tonic-gate 	 */
3840Sstevel@tonic-gate 	if (pname == 0)
3850Sstevel@tonic-gate 		return (0);
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	/*
3880Sstevel@tonic-gate 	 * Just grab the 'basename' of the object current object for
3890Sstevel@tonic-gate 	 * comparing against the 'profiled object name'
3900Sstevel@tonic-gate 	 */
3910Sstevel@tonic-gate 	if (((objname = strrchr(lmp->l_name, '/')) == 0) ||
3920Sstevel@tonic-gate 	    (*(++objname) == 0))
3930Sstevel@tonic-gate 		objname = lmp->l_name;
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	/*
3960Sstevel@tonic-gate 	 * Is this the object we are going to profile.  If not
3970Sstevel@tonic-gate 	 * just set the 'BINDFROM' flag for this object.
3980Sstevel@tonic-gate 	 */
3990Sstevel@tonic-gate 	if ((strcmp(pname, objname) != 0) &&
4000Sstevel@tonic-gate 	    (strcmp(pname, lmp->l_name) != 0))
4010Sstevel@tonic-gate 		return (LA_FLG_BINDFROM);
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	/*
4040Sstevel@tonic-gate 	 * Don't even try to profile an object that does not have
4050Sstevel@tonic-gate 	 * auditing enabled on it's link-map.  This catches 'ld.so.1'.
4060Sstevel@tonic-gate 	 */
407*12877SRod.Evans@Sun.COM 	if (LIST(LINKMAP_TO_RTMAP(lmp))->lm_tflags & LML_TFLG_NOAUDIT)
4080Sstevel@tonic-gate 		return (LA_FLG_BINDFROM);
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	if (profile_open(pname, lmp) == 0)
4110Sstevel@tonic-gate 		return (0);
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	profcookie = *cookie;
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	return (LA_FLG_BINDFROM | LA_FLG_BINDTO);
4160Sstevel@tonic-gate }
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate uint_t
la_objclose(uintptr_t * cookie)4210Sstevel@tonic-gate la_objclose(uintptr_t *cookie)
4220Sstevel@tonic-gate {
4230Sstevel@tonic-gate 	if (*cookie != profcookie)
4240Sstevel@tonic-gate 		return (0);
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	profcookie = 0;
4270Sstevel@tonic-gate 	/*
4280Sstevel@tonic-gate 	 * Turn profil() off.
4290Sstevel@tonic-gate 	 */
4300Sstevel@tonic-gate 	profil(0, 0, 0, 0);
4310Sstevel@tonic-gate 	(void) munmap((caddr_t)Hptr, Fsize);
4320Sstevel@tonic-gate 	return (0);
4330Sstevel@tonic-gate }
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate static int
remap_profile(int fd)4370Sstevel@tonic-gate remap_profile(int fd)
4380Sstevel@tonic-gate {
4390Sstevel@tonic-gate 	caddr_t		addr;
4400Sstevel@tonic-gate 	size_t		l_fsize;
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	l_fsize = Hptr->hd_fsize;
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	if ((addr = (caddr_t)mmap(0, l_fsize, (PROT_READ | PROT_WRITE),
4450Sstevel@tonic-gate 	    MAP_SHARED, fd, 0)) == (char *)-1) {
4460Sstevel@tonic-gate 		int	err = errno;
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_SYS_MMAP), Profile,
4490Sstevel@tonic-gate 		    strerror(err));
4500Sstevel@tonic-gate 		return (0);
4510Sstevel@tonic-gate 	}
4520Sstevel@tonic-gate 	(void) munmap((caddr_t)Hptr, Fsize);
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	Fsize = l_fsize;
4550Sstevel@tonic-gate 	/* LINTED */
4560Sstevel@tonic-gate 	Hptr = (L_hdr*) addr;
4570Sstevel@tonic-gate 	/* LINTED */
4580Sstevel@tonic-gate 	Cptr = (L_cgarc *)(addr + sizeof (L_hdr) + Hptr->hd_psize);
4590Sstevel@tonic-gate 	return (1);
4600Sstevel@tonic-gate }
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate /*
4640Sstevel@tonic-gate  * Update a call graph arc entry.  This routine can be called three ways;
4650Sstevel@tonic-gate  * 	o	On initialization from one of the bndr() functions.
4660Sstevel@tonic-gate  *		In this case the `to' address is known, and may be used to
4670Sstevel@tonic-gate  *		initialize the call graph entry if this function has not
4680Sstevel@tonic-gate  *		been entered before.
4690Sstevel@tonic-gate  *	o	On initial relocation (ie. LD_BIND_NOW). In this case the `to'
4700Sstevel@tonic-gate  *		address is known but the `from' isn't.  The call graph entry
4710Sstevel@tonic-gate  *		is initialized to hold this dummy `to' address, but will be
4720Sstevel@tonic-gate  *		re-initialized later when a function is first called.
4730Sstevel@tonic-gate  *	o	From an initialized plt entry.  When profiling, the plt entries
4740Sstevel@tonic-gate  *		are filled in with the calling functions symbol index and
4750Sstevel@tonic-gate  *		the plt_cg_elf interface function.  This interface function
4760Sstevel@tonic-gate  *		calls here to determine the `to' functions address, and in so
4770Sstevel@tonic-gate  *		doing increments the call count.
4780Sstevel@tonic-gate  */
4790Sstevel@tonic-gate uintptr_t
plt_cg_interp(uint_t ndx,caddr_t from,caddr_t to)4800Sstevel@tonic-gate plt_cg_interp(uint_t ndx, caddr_t from, caddr_t to)
4810Sstevel@tonic-gate {
4820Sstevel@tonic-gate 	L_cgarc *	cptr, cbucket;
4830Sstevel@tonic-gate 	sigset_t	mask;
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	/*
4860Sstevel@tonic-gate 	 * If the from address is outside of the address range being profiled,
4870Sstevel@tonic-gate 	 * simply assign it to the `outside' address.
4880Sstevel@tonic-gate 	 */
4890Sstevel@tonic-gate 	if (from != PRF_UNKNOWN) {
4900Sstevel@tonic-gate 		if ((from > Hpc) || (from < Lpc))
4910Sstevel@tonic-gate 			from = PRF_OUTADDR;
4920Sstevel@tonic-gate 		else
4930Sstevel@tonic-gate 			from = (caddr_t)(from - Lpc);
4940Sstevel@tonic-gate 	}
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	(void) prof_mutex_lock((mutex_t *)&Hptr->hd_mutex, &mask);
4970Sstevel@tonic-gate 	/*
4980Sstevel@tonic-gate 	 * Has the buffer grown since last we looked at it (another processes
4990Sstevel@tonic-gate 	 * could have grown it...).
5000Sstevel@tonic-gate 	 */
5010Sstevel@tonic-gate 	/* LINTED */
5020Sstevel@tonic-gate 	if (Hptr->hd_fsize != (unsigned int)Fsize) {
5030Sstevel@tonic-gate 		int fd;
5040Sstevel@tonic-gate 		fd = open(Profile, O_RDWR, 0);
5050Sstevel@tonic-gate 		if (remap_profile(fd) == 0) {
5060Sstevel@tonic-gate 			(void) prof_mutex_unlock((mutex_t *)&Hptr->hd_mutex,
5076812Sraf 			    &mask);
5080Sstevel@tonic-gate 			exit(1);
5090Sstevel@tonic-gate 		}
5100Sstevel@tonic-gate 		(void) close(fd);
5110Sstevel@tonic-gate 	}
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	cptr = &Cptr[ndx];
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	if (cptr->cg_to == 0) {
5160Sstevel@tonic-gate 		/*
5170Sstevel@tonic-gate 		 * If this is the first time this function has been called we
5180Sstevel@tonic-gate 		 * got here from one of the binders or an initial relocation
5190Sstevel@tonic-gate 		 * (ie. LD_BIND_NOW).  In this case the `to' address is
5200Sstevel@tonic-gate 		 * provided.  Initialize this functions call graph entry with
5210Sstevel@tonic-gate 		 * the functions address (retained as a relative offset).
5220Sstevel@tonic-gate 		 * If we know where the function call originated from
5230Sstevel@tonic-gate 		 * initialize the count field.
5240Sstevel@tonic-gate 		 */
5250Sstevel@tonic-gate 		cptr->cg_to = (caddr_t)(to - Lpc);
5260Sstevel@tonic-gate 		cptr->cg_from = from;
5270Sstevel@tonic-gate 		if (from != PRF_UNKNOWN)
5280Sstevel@tonic-gate 			cptr->cg_count = 1;
5290Sstevel@tonic-gate 	} else {
5300Sstevel@tonic-gate 		/*
5310Sstevel@tonic-gate 		 * If a function has been called from a previous run, but we
5320Sstevel@tonic-gate 		 * don't know where we came from (ie. LD_BIND_NOW), then later
5330Sstevel@tonic-gate 		 * calls through the plt will be able to obtain the required
5340Sstevel@tonic-gate 		 * functions address, thus there is no need to proceed further.
5350Sstevel@tonic-gate 		 */
5360Sstevel@tonic-gate 		if (from != PRF_UNKNOWN) {
5370Sstevel@tonic-gate 			/*
5380Sstevel@tonic-gate 			 * If the from addresses match simply bump the count.
5390Sstevel@tonic-gate 			 * If not scan the link list to find a match for this
5400Sstevel@tonic-gate 			 * `from' address.  If one doesn't exit create a new
5410Sstevel@tonic-gate 			 * entry and link it in.
5420Sstevel@tonic-gate 			 */
5430Sstevel@tonic-gate 			while ((cptr->cg_from != from) &&
5446812Sraf 			    (cptr->cg_from != PRF_UNKNOWN)) {
5450Sstevel@tonic-gate 				if (cptr->cg_next != 0)
5460Sstevel@tonic-gate 					cptr = &Cptr[cptr->cg_next];
5470Sstevel@tonic-gate 				else {
5480Sstevel@tonic-gate 					to = cptr->cg_to;
5490Sstevel@tonic-gate 					cptr->cg_next = Hptr->hd_ncndx++;
5500Sstevel@tonic-gate 					cptr = &Cptr[cptr->cg_next];
5510Sstevel@tonic-gate 					/*
5520Sstevel@tonic-gate 					 * If we've run out of file, extend it.
5530Sstevel@tonic-gate 					 */
5540Sstevel@tonic-gate 					if (Hptr->hd_ncndx == Hptr->hd_lcndx) {
5550Sstevel@tonic-gate 						caddr_t	addr;
5560Sstevel@tonic-gate 						int	fd;
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 						/* LINTED */
5590Sstevel@tonic-gate 						Hptr->hd_fsize += (unsigned int)
5600Sstevel@tonic-gate 						    PRF_CGNUMB *
5610Sstevel@tonic-gate 						    sizeof (L_cgarc);
5620Sstevel@tonic-gate 						fd = open(Profile, O_RDWR, 0);
5630Sstevel@tonic-gate 						if (ftruncate(fd,
5640Sstevel@tonic-gate 						    Hptr->hd_fsize) == -1) {
5650Sstevel@tonic-gate 							int	err = errno;
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 							(void) fprintf(stderr,
5680Sstevel@tonic-gate 							    MSG_INTL(
5690Sstevel@tonic-gate 							    MSG_SYS_FTRUNC),
5700Sstevel@tonic-gate 							    Profile,
5710Sstevel@tonic-gate 							    strerror(err));
5720Sstevel@tonic-gate 							(void) close(fd);
5730Sstevel@tonic-gate 							cptr = &cbucket;
5740Sstevel@tonic-gate 						}
5750Sstevel@tonic-gate 						/*
5760Sstevel@tonic-gate 						 * Since the buffer will be
5770Sstevel@tonic-gate 						 * remapped, we need to be
5780Sstevel@tonic-gate 						 * prepared to adjust cptr.
5790Sstevel@tonic-gate 						 */
5800Sstevel@tonic-gate 						addr = (caddr_t)((Addr)cptr -
5810Sstevel@tonic-gate 						    (Addr)Cptr);
5820Sstevel@tonic-gate 						if (remap_profile(fd) == 0) {
5836812Sraf 						    /* CSTYLED */
5840Sstevel@tonic-gate 						    (void) prof_mutex_unlock(
5850Sstevel@tonic-gate 							(mutex_t *)&Hptr->
5860Sstevel@tonic-gate 							hd_mutex, &mask);
5876812Sraf 							exit(1);
5880Sstevel@tonic-gate 						}
5890Sstevel@tonic-gate 						cptr = (L_cgarc *)((Addr)addr +
5900Sstevel@tonic-gate 						    (Addr)Cptr);
5910Sstevel@tonic-gate 						(void) close(fd);
5920Sstevel@tonic-gate 						Hptr->hd_lcndx += PRF_CGNUMB;
5930Sstevel@tonic-gate 					}
5940Sstevel@tonic-gate 					cptr->cg_from = from;
5950Sstevel@tonic-gate 					cptr->cg_to = to;
5960Sstevel@tonic-gate 				}
5970Sstevel@tonic-gate 			}
5980Sstevel@tonic-gate 			/*
5990Sstevel@tonic-gate 			 * If we're updating an entry from an unknown call
6000Sstevel@tonic-gate 			 * address initialize this element, otherwise
6010Sstevel@tonic-gate 			 * increment the call count.
6020Sstevel@tonic-gate 			 */
6030Sstevel@tonic-gate 			if (cptr->cg_from == PRF_UNKNOWN) {
6040Sstevel@tonic-gate 				cptr->cg_from = from;
6050Sstevel@tonic-gate 				cptr->cg_count = 1;
6060Sstevel@tonic-gate 			} else
6070Sstevel@tonic-gate 				cptr->cg_count++;
6080Sstevel@tonic-gate 		}
6090Sstevel@tonic-gate 	}
6100Sstevel@tonic-gate 	/*
6110Sstevel@tonic-gate 	 * Return the real address of the function.
6120Sstevel@tonic-gate 	 */
6130Sstevel@tonic-gate 	(void) prof_mutex_unlock((mutex_t *)&Hptr->hd_mutex, &mask);
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	return ((uintptr_t)((Addr)cptr->cg_to + (Addr)Lpc));
6160Sstevel@tonic-gate }
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate /* ARGSUSED2 */
6190Sstevel@tonic-gate #if	defined(__sparcv9)
6200Sstevel@tonic-gate uintptr_t
la_sparcv9_pltenter(Elf64_Sym * symp,uint_t symndx,uintptr_t * refcookie,uintptr_t * defcookie,La_sparcv9_regs * regset,uint_t * sbflags,const char * sym_name)6210Sstevel@tonic-gate la_sparcv9_pltenter(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie,
6220Sstevel@tonic-gate 	uintptr_t *defcookie, La_sparcv9_regs *regset, uint_t *sbflags,
6230Sstevel@tonic-gate 	const char *sym_name)
6240Sstevel@tonic-gate #elif	defined(__sparc)
6250Sstevel@tonic-gate uintptr_t
6260Sstevel@tonic-gate la_sparcv8_pltenter(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcookie,
6270Sstevel@tonic-gate 	uintptr_t *defcookie, La_sparcv8_regs *regset, uint_t *sbflags)
6280Sstevel@tonic-gate #elif	defined(__amd64)
6290Sstevel@tonic-gate uintptr_t
6300Sstevel@tonic-gate la_amd64_pltenter(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie,
6310Sstevel@tonic-gate 	uintptr_t *defcookie, La_amd64_regs *regset, uint_t *sbflags,
6320Sstevel@tonic-gate 	const char *sym_name)
6330Sstevel@tonic-gate #elif	defined(__i386)
6340Sstevel@tonic-gate uintptr_t
6350Sstevel@tonic-gate la_i86_pltenter(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcookie,
6360Sstevel@tonic-gate 	uintptr_t *defcookie, La_i86_regs *regset, uint_t *sbflags)
6370Sstevel@tonic-gate #else
6380Sstevel@tonic-gate #error unexpected architecture!
6390Sstevel@tonic-gate #endif
6400Sstevel@tonic-gate {
6410Sstevel@tonic-gate 	caddr_t		from;
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	/*
6440Sstevel@tonic-gate 	 * profiling has been disabled.
6450Sstevel@tonic-gate 	 */
6460Sstevel@tonic-gate 	if (profcookie == 0)
6470Sstevel@tonic-gate 		return (symp->st_value);
6483731Srie #if defined(__sparc)
6490Sstevel@tonic-gate 	/*
6500Sstevel@tonic-gate 	 * The callers return address is currently stored in O7 (which
6510Sstevel@tonic-gate 	 * will become I7 when the window shift occurs).
6520Sstevel@tonic-gate 	 */
6530Sstevel@tonic-gate 	from = (caddr_t)regset->lr_rego7;
6540Sstevel@tonic-gate #elif defined(__amd64)
6550Sstevel@tonic-gate 	/*
6560Sstevel@tonic-gate 	 * The callers return address is on the top of the stack for amd64
6570Sstevel@tonic-gate 	 */
6580Sstevel@tonic-gate 	from = *(caddr_t *)(regset->lr_rsp);
6590Sstevel@tonic-gate #elif defined(__i386)
6600Sstevel@tonic-gate 	/*
6610Sstevel@tonic-gate 	 * The callers return address is on the top of the stack for i386
6620Sstevel@tonic-gate 	 */
6630Sstevel@tonic-gate 	from = *(caddr_t *)(regset->lr_esp);
6640Sstevel@tonic-gate #else
6650Sstevel@tonic-gate #error unexpected architecture!
6660Sstevel@tonic-gate #endif
6670Sstevel@tonic-gate 	return (plt_cg_interp(symndx, (caddr_t)from, (caddr_t)symp->st_value));
6680Sstevel@tonic-gate }
669