17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7257d1b4Sraf * Common Development and Distribution License (the "License").
6*7257d1b4Sraf * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21*7257d1b4Sraf
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24*7257d1b4Sraf * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate * Implements the routines that are needed only for internal process
317c478bd9Sstevel@tonic-gate * control.
327c478bd9Sstevel@tonic-gate */
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gate #ifndef DEBUG
357c478bd9Sstevel@tonic-gate #define NDEBUG 1
367c478bd9Sstevel@tonic-gate #endif
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate #include "tnfctl_int.h"
397c478bd9Sstevel@tonic-gate #include "kernel_int.h"
407c478bd9Sstevel@tonic-gate #include "dbg.h"
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate #include <stdio.h>
437c478bd9Sstevel@tonic-gate #include <sys/types.h>
447c478bd9Sstevel@tonic-gate #include <stdlib.h>
457c478bd9Sstevel@tonic-gate #include <unistd.h>
467c478bd9Sstevel@tonic-gate #include <string.h>
477c478bd9Sstevel@tonic-gate #include <link.h>
487c478bd9Sstevel@tonic-gate #include <sys/stat.h>
497c478bd9Sstevel@tonic-gate #include <fcntl.h>
507c478bd9Sstevel@tonic-gate #include <sys/param.h>
517c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
527c478bd9Sstevel@tonic-gate #include <assert.h>
537c478bd9Sstevel@tonic-gate #include <dlfcn.h>
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate static int inprocess_read(void *ignore,
567c478bd9Sstevel@tonic-gate uintptr_t addr, void *buf, size_t size);
577c478bd9Sstevel@tonic-gate static int inprocess_write(void *ignore,
587c478bd9Sstevel@tonic-gate uintptr_t addr, void *buf, size_t size);
597c478bd9Sstevel@tonic-gate static pid_t inprocess_getpid(void *ignore);
607c478bd9Sstevel@tonic-gate static tnfctl_errcode_t inprocess_get_dtdebug(void *hndl, uintptr_t *ret_val);
617c478bd9Sstevel@tonic-gate static int inprocess_loadobj_iter(void *opq, tnfctl_ind_obj_f *obj_func,
627c478bd9Sstevel@tonic-gate void *cd);
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate /*
65*7257d1b4Sraf * Cause interposition on dlclose() and dlopen()
667c478bd9Sstevel@tonic-gate */
677c478bd9Sstevel@tonic-gate #pragma weak dlclose = _tnfctl_dlclose
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate #pragma weak dlopen = _tnfctl_dlopen
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate * The lock used to protect the _tnfctl_internal_tracing_flag variable.
737c478bd9Sstevel@tonic-gate *
747c478bd9Sstevel@tonic-gate */
757c478bd9Sstevel@tonic-gate mutex_t _tnfctl_internalguard_lock = DEFAULTMUTEX;
767c478bd9Sstevel@tonic-gate boolean_t _tnfctl_internal_tracing_flag = 0;
777c478bd9Sstevel@tonic-gate pid_t _tnfctl_externally_traced_pid = NOPID;
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate * Returns a pointer to a tnfctl handle that can do in process probe control.
817c478bd9Sstevel@tonic-gate */
827c478bd9Sstevel@tonic-gate tnfctl_errcode_t
tnfctl_internal_open(tnfctl_handle_t ** ret_val)837c478bd9Sstevel@tonic-gate tnfctl_internal_open(tnfctl_handle_t **ret_val)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate tnfctl_handle_t *hdl;
867c478bd9Sstevel@tonic-gate tnfctl_errcode_t prexstat;
877c478bd9Sstevel@tonic-gate uintptr_t dbgaddr;
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate /* allocate hdl and zero fill */
907c478bd9Sstevel@tonic-gate hdl = calloc(1, sizeof (*hdl));
917c478bd9Sstevel@tonic-gate if (hdl == NULL) {
927c478bd9Sstevel@tonic-gate return (TNFCTL_ERR_ALLOCFAIL);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate hdl->mode = INTERNAL_MODE;
967c478bd9Sstevel@tonic-gate hdl->called_exit = B_FALSE;
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate /* plug in inprocess call back functions */
997c478bd9Sstevel@tonic-gate hdl->p_read = inprocess_read;
1007c478bd9Sstevel@tonic-gate hdl->p_write = inprocess_write;
1017c478bd9Sstevel@tonic-gate hdl->p_obj_iter = inprocess_loadobj_iter;
1027c478bd9Sstevel@tonic-gate hdl->p_getpid = inprocess_getpid;
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate * get the address of DT_DEBUG and store it in proc_p
1067c478bd9Sstevel@tonic-gate * (the handle on the same process is the dbg address)
1077c478bd9Sstevel@tonic-gate */
1087c478bd9Sstevel@tonic-gate prexstat = inprocess_get_dtdebug(hdl, &dbgaddr);
1097c478bd9Sstevel@tonic-gate if (prexstat) {
1107c478bd9Sstevel@tonic-gate free(hdl);
1117c478bd9Sstevel@tonic-gate return (prexstat);
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate hdl->proc_p = (void *) dbgaddr;
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate /* initialize state in handle */
1167c478bd9Sstevel@tonic-gate prexstat = _tnfctl_set_state(hdl);
1177c478bd9Sstevel@tonic-gate if (prexstat) {
1187c478bd9Sstevel@tonic-gate free(hdl);
1197c478bd9Sstevel@tonic-gate return (prexstat);
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate /* see if process is already being traced */
1227c478bd9Sstevel@tonic-gate prexstat = _tnfctl_internal_getlock();
1237c478bd9Sstevel@tonic-gate if (prexstat) {
1247c478bd9Sstevel@tonic-gate free(hdl);
1257c478bd9Sstevel@tonic-gate return (prexstat);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate *ret_val = hdl;
1287c478bd9Sstevel@tonic-gate return (TNFCTL_ERR_NONE);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate /*
1327c478bd9Sstevel@tonic-gate * reads a block of memory from the same address space.
1337c478bd9Sstevel@tonic-gate */
1347c478bd9Sstevel@tonic-gate static int
inprocess_read(void * ignore,uintptr_t addr,void * buf,size_t size)1357c478bd9Sstevel@tonic-gate inprocess_read(void *ignore, uintptr_t addr, void *buf, size_t size)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate DBG_TNF_PROBE_2(inprocess_read_1, "libtnfctl", "sunw%verbosity 3;",
1397c478bd9Sstevel@tonic-gate tnf_long, num_bytes, size,
1407c478bd9Sstevel@tonic-gate tnf_opaque, from_address, addr);
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate (void) memcpy(buf, (void *) addr, size);
1437c478bd9Sstevel@tonic-gate return (0);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate /*
1477c478bd9Sstevel@tonic-gate * writes a block of memory to the same address space.
1487c478bd9Sstevel@tonic-gate */
1497c478bd9Sstevel@tonic-gate static int
inprocess_write(void * ignore,uintptr_t addr,void * buf,size_t size)1507c478bd9Sstevel@tonic-gate inprocess_write(void *ignore, uintptr_t addr, void *buf, size_t size)
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate
1537c478bd9Sstevel@tonic-gate DBG_TNF_PROBE_2(inprocess_write_1, "libtnfctl", "sunw%verbosity 3;",
1547c478bd9Sstevel@tonic-gate tnf_long, num_bytes, size,
1557c478bd9Sstevel@tonic-gate tnf_opaque, to_address, addr);
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate (void) memcpy((void *)addr, buf, size);
1587c478bd9Sstevel@tonic-gate return (0);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate /*
1627c478bd9Sstevel@tonic-gate * returns the pid of the process.
1637c478bd9Sstevel@tonic-gate */
1647c478bd9Sstevel@tonic-gate static pid_t
inprocess_getpid(void * ignore)1657c478bd9Sstevel@tonic-gate inprocess_getpid(void *ignore)
1667c478bd9Sstevel@tonic-gate {
1677c478bd9Sstevel@tonic-gate return (getpid());
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate extern Elf3264_Dyn _DYNAMIC;
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate /*
1727c478bd9Sstevel@tonic-gate * returns the address of the DT_DEBUG field in the _DYNAMIC array
1737c478bd9Sstevel@tonic-gate * of the same address space.
1747c478bd9Sstevel@tonic-gate */
1757c478bd9Sstevel@tonic-gate static tnfctl_errcode_t
inprocess_get_dtdebug(void * hndl,uintptr_t * ret_val)1767c478bd9Sstevel@tonic-gate inprocess_get_dtdebug(void *hndl, uintptr_t *ret_val)
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate Elf3264_Dyn *dyn = &_DYNAMIC;
1797c478bd9Sstevel@tonic-gate Elf3264_Dyn *dp;
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate for (dp = dyn; dp->d_tag != DT_NULL; dp++) {
1827c478bd9Sstevel@tonic-gate if (dp->d_tag == DT_DEBUG) {
1837c478bd9Sstevel@tonic-gate *ret_val = (uintptr_t) dp;
1847c478bd9Sstevel@tonic-gate return (TNFCTL_ERR_NONE);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate return (TNFCTL_ERR_INTERNAL);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate #define PROCFORMAT "/proc/%d"
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate /*
1937c478bd9Sstevel@tonic-gate * iterate over all loadobjects in the same address space calling the
1947c478bd9Sstevel@tonic-gate * callback function "obj_func".
1957c478bd9Sstevel@tonic-gate */
1967c478bd9Sstevel@tonic-gate static int
inprocess_loadobj_iter(void * opq,tnfctl_ind_obj_f * obj_func,void * cd)1977c478bd9Sstevel@tonic-gate inprocess_loadobj_iter(void *opq, tnfctl_ind_obj_f *obj_func, void *cd)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate Elf3264_Dyn *dtdebug = opq;
2007c478bd9Sstevel@tonic-gate struct r_debug *r_dbg;
2017c478bd9Sstevel@tonic-gate struct link_map *lmap;
2027c478bd9Sstevel@tonic-gate char path[MAXPATHLEN];
2037c478bd9Sstevel@tonic-gate int procfd;
2047c478bd9Sstevel@tonic-gate tnfctl_ind_obj_info_t loadobj;
2057c478bd9Sstevel@tonic-gate int retval = 0; /* sucessful return */
2067c478bd9Sstevel@tonic-gate
2077c478bd9Sstevel@tonic-gate DBG_TNF_PROBE_0(inprocess_loadobj_iter_start, "libtnfctl",
2087c478bd9Sstevel@tonic-gate "start inprocess_loadobj_iter; sunw%verbosity 1");
2097c478bd9Sstevel@tonic-gate
2107c478bd9Sstevel@tonic-gate r_dbg = (struct r_debug *)dtdebug->d_un.d_ptr;
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate DBG_TNF_PROBE_1(inprocess_loadobj_iter_1, "libtnfctl",
2137c478bd9Sstevel@tonic-gate "sunw%verbosity 1",
2147c478bd9Sstevel@tonic-gate tnf_string, link_map_state,
2157c478bd9Sstevel@tonic-gate (r_dbg->r_state == RT_CONSISTENT) ? "RT_CONSISTENT" :
2167c478bd9Sstevel@tonic-gate (r_dbg->r_state == RT_ADD) ? "RT_ADD" : "RT_DELETE");
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate /* bail if link map is not consistent */
2197c478bd9Sstevel@tonic-gate if (r_dbg->r_state != RT_CONSISTENT)
2207c478bd9Sstevel@tonic-gate return (1);
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate (void) sprintf(path, PROCFORMAT, (int) getpid());
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate /*
2257c478bd9Sstevel@tonic-gate * opening /proc readonly, so debuggers can still run
2267c478bd9Sstevel@tonic-gate * We use /proc in order to get fd on the object.
2277c478bd9Sstevel@tonic-gate */
2287c478bd9Sstevel@tonic-gate procfd = open(path, O_RDONLY);
2297c478bd9Sstevel@tonic-gate if (procfd == -1)
2307c478bd9Sstevel@tonic-gate return (1);
2317c478bd9Sstevel@tonic-gate
2327c478bd9Sstevel@tonic-gate for (lmap = r_dbg->r_map; lmap; lmap = lmap->l_next) {
2337c478bd9Sstevel@tonic-gate loadobj.text_base = lmap->l_addr;
2347c478bd9Sstevel@tonic-gate loadobj.data_base = lmap->l_addr;
2357c478bd9Sstevel@tonic-gate loadobj.objname = lmap->l_name;
2367c478bd9Sstevel@tonic-gate /*
2377c478bd9Sstevel@tonic-gate * client of this interface should deal with -1 for objfd,
2387c478bd9Sstevel@tonic-gate * so no error checking is needed on this ioctl
2397c478bd9Sstevel@tonic-gate */
2407c478bd9Sstevel@tonic-gate loadobj.objfd = ioctl(procfd, PIOCOPENM, &(lmap->l_addr));
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gate retval = obj_func(opq, &loadobj, cd);
2437c478bd9Sstevel@tonic-gate
2447c478bd9Sstevel@tonic-gate /* close the fd */
2457c478bd9Sstevel@tonic-gate if (loadobj.objfd != -1)
2467c478bd9Sstevel@tonic-gate close(loadobj.objfd);
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate /* check for error */
2497c478bd9Sstevel@tonic-gate if (retval == 1)
2507c478bd9Sstevel@tonic-gate goto end_of_func;
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate end_of_func:
2547c478bd9Sstevel@tonic-gate close(procfd);
2557c478bd9Sstevel@tonic-gate
2567c478bd9Sstevel@tonic-gate DBG_TNF_PROBE_0(inprocess_loadobj_iter_end, "libtnfctl",
2577c478bd9Sstevel@tonic-gate "end inprocess_loadobj_iter; sunw%verbosity 1");
2587c478bd9Sstevel@tonic-gate return (retval);
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate
2617c478bd9Sstevel@tonic-gate /*
2627c478bd9Sstevel@tonic-gate * The lock that prevents a thread from accessing our cached library list
2637c478bd9Sstevel@tonic-gate * and a dlopen or dlclose happening at the same time in another thread.
2647c478bd9Sstevel@tonic-gate */
2657c478bd9Sstevel@tonic-gate mutex_t _tnfctl_lmap_lock = DEFAULTMUTEX;
2667c478bd9Sstevel@tonic-gate
2677c478bd9Sstevel@tonic-gate /*
2687c478bd9Sstevel@tonic-gate * The flag that indicates that the library list has changed via a
2697c478bd9Sstevel@tonic-gate * dlopen or dlclose.
2707c478bd9Sstevel@tonic-gate */
2717c478bd9Sstevel@tonic-gate boolean_t _tnfctl_libs_changed = B_FALSE;
2727c478bd9Sstevel@tonic-gate
2737c478bd9Sstevel@tonic-gate /*
2747c478bd9Sstevel@tonic-gate * Thread id of the owner of the lock in order to implement a
2757c478bd9Sstevel@tonic-gate * recursive lock i.e. no deadlock if the same thread tries to lock
2767c478bd9Sstevel@tonic-gate * a lock it already holds.
2777c478bd9Sstevel@tonic-gate */
2787c478bd9Sstevel@tonic-gate static thread_t lock_holder = 0; /* XXX - no tid with 0 */
2797c478bd9Sstevel@tonic-gate NOTE(MUTEX_PROTECTS_DATA(warlock::lmap_lock, lock_holder))
NOTE(DATA_READABLE_WITHOUT_LOCK (lock_holder))2807c478bd9Sstevel@tonic-gate NOTE(DATA_READABLE_WITHOUT_LOCK(lock_holder))
2817c478bd9Sstevel@tonic-gate
2827c478bd9Sstevel@tonic-gate /*
2837c478bd9Sstevel@tonic-gate * In the routines below, we will appear to use a different lock if we
2847c478bd9Sstevel@tonic-gate * are running lock_lint/warlock. We define a macro to represent whichever
2857c478bd9Sstevel@tonic-gate * lock is appropriate.
2867c478bd9Sstevel@tonic-gate */
2877c478bd9Sstevel@tonic-gate #if defined(__lock_lint)
2887c478bd9Sstevel@tonic-gate #define LMAP_LOCK (&warlock_kludge->lmap_lock)
2897c478bd9Sstevel@tonic-gate #else
2907c478bd9Sstevel@tonic-gate #define LMAP_LOCK (&_tnfctl_lmap_lock)
2917c478bd9Sstevel@tonic-gate #endif
2927c478bd9Sstevel@tonic-gate
2937c478bd9Sstevel@tonic-gate /*
2947c478bd9Sstevel@tonic-gate * dlclose interposition with a recursive lock so that a .fini section
2957c478bd9Sstevel@tonic-gate * can recursively call dlopen or dlclose while holding _tnfctl_lmap_lock
2967c478bd9Sstevel@tonic-gate * This interposition serializes access to rtld's loadobject list and
2977c478bd9Sstevel@tonic-gate * also updates the flag _tnfctl_libs_changed to indicate a change in
2987c478bd9Sstevel@tonic-gate * the library list. This flag is checked by operations that update
2997c478bd9Sstevel@tonic-gate * probes so that it can sync up with the new library list and potential
3007c478bd9Sstevel@tonic-gate * new/deleted probes.
3017c478bd9Sstevel@tonic-gate */
3027c478bd9Sstevel@tonic-gate int
3037c478bd9Sstevel@tonic-gate _tnfctl_dlclose(void *handle)
3047c478bd9Sstevel@tonic-gate {
3057c478bd9Sstevel@tonic-gate static int (*real_dlclose)(void *handle) = NULL;
3067c478bd9Sstevel@tonic-gate int retval;
3077c478bd9Sstevel@tonic-gate thread_t tid;
3087c478bd9Sstevel@tonic-gate
3097c478bd9Sstevel@tonic-gate if (real_dlclose == NULL) {
3107c478bd9Sstevel@tonic-gate real_dlclose = (int (*)(void *)) dlsym(RTLD_NEXT, "dlclose");
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate assert(real_dlclose);
3137c478bd9Sstevel@tonic-gate
3147c478bd9Sstevel@tonic-gate if (mutex_trylock(LMAP_LOCK) != 0) {
3157c478bd9Sstevel@tonic-gate /* don't have lock */
3167c478bd9Sstevel@tonic-gate tid = thr_self();
3177c478bd9Sstevel@tonic-gate if (tid == lock_holder) {
3187c478bd9Sstevel@tonic-gate /* recursive dlopen/dlclose by same thread */
3197c478bd9Sstevel@tonic-gate return ((*real_dlclose)(handle));
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate /* not a recursive dlopen/dlclose - wait on lock */
3227c478bd9Sstevel@tonic-gate mutex_lock(LMAP_LOCK);
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate
3257c478bd9Sstevel@tonic-gate /* lock is held now */
3267c478bd9Sstevel@tonic-gate lock_holder = thr_self();
3277c478bd9Sstevel@tonic-gate retval = (*real_dlclose)(handle);
3287c478bd9Sstevel@tonic-gate
3297c478bd9Sstevel@tonic-gate /*
3307c478bd9Sstevel@tonic-gate * reset lock_holder so that if _tnfctl_lmap_lock is held by some
3317c478bd9Sstevel@tonic-gate * other part of the code, we don't assume it is a recursive
3327c478bd9Sstevel@tonic-gate * dlopen/dlclose
3337c478bd9Sstevel@tonic-gate */
3347c478bd9Sstevel@tonic-gate lock_holder = 0;
3357c478bd9Sstevel@tonic-gate _tnfctl_libs_changed = B_TRUE;
3367c478bd9Sstevel@tonic-gate mutex_unlock(LMAP_LOCK);
3377c478bd9Sstevel@tonic-gate
3387c478bd9Sstevel@tonic-gate return (retval);
3397c478bd9Sstevel@tonic-gate }
3407c478bd9Sstevel@tonic-gate
3417c478bd9Sstevel@tonic-gate /*
3427c478bd9Sstevel@tonic-gate * dlopen interposition with a recursive lock so that a .init section
3437c478bd9Sstevel@tonic-gate * can recursively call dlopen or dlclose while holding _tnfctl_lmap_lock
3447c478bd9Sstevel@tonic-gate * This interposition serializes access to rtld's loadobject list and
3457c478bd9Sstevel@tonic-gate * also updates the flag _tnfctl_libs_changed to indicate a change in
3467c478bd9Sstevel@tonic-gate * the library list. This flag is checked by operations that update
3477c478bd9Sstevel@tonic-gate * probes so that it can sync up with the new library list and potential
3487c478bd9Sstevel@tonic-gate * new/deleted probes.
3497c478bd9Sstevel@tonic-gate */
3507c478bd9Sstevel@tonic-gate void *
_tnfctl_dlopen(const char * pathname,int mode)3517c478bd9Sstevel@tonic-gate _tnfctl_dlopen(const char *pathname, int mode)
3527c478bd9Sstevel@tonic-gate {
3537c478bd9Sstevel@tonic-gate static void * (*real_dlopen)(const char *, int) = NULL;
3547c478bd9Sstevel@tonic-gate void *retval;
3557c478bd9Sstevel@tonic-gate thread_t tid;
3567c478bd9Sstevel@tonic-gate
3577c478bd9Sstevel@tonic-gate if (real_dlopen == NULL) {
3587c478bd9Sstevel@tonic-gate real_dlopen = (void * (*)(const char *, int))
3597c478bd9Sstevel@tonic-gate dlsym(RTLD_NEXT, "dlopen");
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate assert(real_dlopen);
3627c478bd9Sstevel@tonic-gate
3637c478bd9Sstevel@tonic-gate if (mutex_trylock(LMAP_LOCK) != 0) {
3647c478bd9Sstevel@tonic-gate /* don't have lock */
3657c478bd9Sstevel@tonic-gate tid = thr_self();
3667c478bd9Sstevel@tonic-gate if (tid == lock_holder) {
3677c478bd9Sstevel@tonic-gate /* recursive dlopen/dlclose by same thread */
3687c478bd9Sstevel@tonic-gate return ((*real_dlopen)(pathname, mode));
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate /* not a recursive dlopen/dlclose - wait on lock */
3717c478bd9Sstevel@tonic-gate mutex_lock(LMAP_LOCK);
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate
3747c478bd9Sstevel@tonic-gate /* lock is held now */
3757c478bd9Sstevel@tonic-gate lock_holder = thr_self();
3767c478bd9Sstevel@tonic-gate retval = (*real_dlopen)(pathname, mode);
3777c478bd9Sstevel@tonic-gate
3787c478bd9Sstevel@tonic-gate /*
3797c478bd9Sstevel@tonic-gate * reset lock_holder so that if _tnfctl_lmap_lock is held by some
3807c478bd9Sstevel@tonic-gate * other part of the code, we don't assume it is a recursive
3817c478bd9Sstevel@tonic-gate * dlopen/dlclose
3827c478bd9Sstevel@tonic-gate */
3837c478bd9Sstevel@tonic-gate lock_holder = 0;
3847c478bd9Sstevel@tonic-gate _tnfctl_libs_changed = B_TRUE;
3857c478bd9Sstevel@tonic-gate mutex_unlock(LMAP_LOCK);
3867c478bd9Sstevel@tonic-gate
3877c478bd9Sstevel@tonic-gate return (retval);
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate
3907c478bd9Sstevel@tonic-gate tnfctl_errcode_t
_tnfctl_internal_getlock()3917c478bd9Sstevel@tonic-gate _tnfctl_internal_getlock()
3927c478bd9Sstevel@tonic-gate {
3937c478bd9Sstevel@tonic-gate mutex_lock(&_tnfctl_internalguard_lock);
3947c478bd9Sstevel@tonic-gate if (_tnfctl_internal_tracing_flag == 1) {
3957c478bd9Sstevel@tonic-gate /* internal trace control active */
3967c478bd9Sstevel@tonic-gate mutex_unlock(&_tnfctl_internalguard_lock);
3977c478bd9Sstevel@tonic-gate return (TNFCTL_ERR_BUSY);
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate _tnfctl_internal_tracing_flag = 1;
4007c478bd9Sstevel@tonic-gate if (_tnfctl_externally_traced_pid == getpid()) {
4017c478bd9Sstevel@tonic-gate /* external trace control is active */
4027c478bd9Sstevel@tonic-gate _tnfctl_internal_tracing_flag = 0;
4037c478bd9Sstevel@tonic-gate mutex_unlock(&_tnfctl_internalguard_lock);
4047c478bd9Sstevel@tonic-gate return (TNFCTL_ERR_BUSY);
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate DBG((void) fprintf(stderr, "_tnfctl_internal_getlock: ok to trace %d\n",
4077c478bd9Sstevel@tonic-gate getpid()));
4087c478bd9Sstevel@tonic-gate mutex_unlock(&_tnfctl_internalguard_lock);
4097c478bd9Sstevel@tonic-gate return (TNFCTL_ERR_NONE);
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate
4127c478bd9Sstevel@tonic-gate
4137c478bd9Sstevel@tonic-gate #ifdef __lock_lint
4147c478bd9Sstevel@tonic-gate
4157c478bd9Sstevel@tonic-gate /*
4167c478bd9Sstevel@tonic-gate * dummy function for lock_lint (warlock) static lock analysis.
4177c478bd9Sstevel@tonic-gate */
4187c478bd9Sstevel@tonic-gate int
warlock_dummy()4197c478bd9Sstevel@tonic-gate warlock_dummy()
4207c478bd9Sstevel@tonic-gate {
4217c478bd9Sstevel@tonic-gate int (*fp)();
4227c478bd9Sstevel@tonic-gate
4237c478bd9Sstevel@tonic-gate return ((*fp)());
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate
4267c478bd9Sstevel@tonic-gate #endif
427