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