xref: /onnv-gate/usr/src/lib/libtnfctl/internal.c (revision 6812:febeba71273d)
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
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * 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.
24*6812Sraf  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Implements the routines that are needed only for internal process
310Sstevel@tonic-gate  * control.
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #ifndef DEBUG
350Sstevel@tonic-gate #define	NDEBUG	1
360Sstevel@tonic-gate #endif
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include "tnfctl_int.h"
390Sstevel@tonic-gate #include "kernel_int.h"
400Sstevel@tonic-gate #include "dbg.h"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include <stdio.h>
430Sstevel@tonic-gate #include <sys/types.h>
440Sstevel@tonic-gate #include <stdlib.h>
450Sstevel@tonic-gate #include <unistd.h>
460Sstevel@tonic-gate #include <string.h>
470Sstevel@tonic-gate #include <link.h>
480Sstevel@tonic-gate #include <sys/stat.h>
490Sstevel@tonic-gate #include <fcntl.h>
500Sstevel@tonic-gate #include <sys/param.h>
510Sstevel@tonic-gate #include <sys/procfs.h>
520Sstevel@tonic-gate #include <assert.h>
530Sstevel@tonic-gate #include <dlfcn.h>
540Sstevel@tonic-gate 
550Sstevel@tonic-gate static int inprocess_read(void *ignore,
560Sstevel@tonic-gate     uintptr_t addr, void *buf, size_t size);
570Sstevel@tonic-gate static int inprocess_write(void *ignore,
580Sstevel@tonic-gate     uintptr_t addr, void *buf, size_t size);
590Sstevel@tonic-gate static pid_t inprocess_getpid(void *ignore);
600Sstevel@tonic-gate static tnfctl_errcode_t inprocess_get_dtdebug(void *hndl, uintptr_t *ret_val);
610Sstevel@tonic-gate static int inprocess_loadobj_iter(void *opq, tnfctl_ind_obj_f *obj_func,
620Sstevel@tonic-gate 					void *cd);
630Sstevel@tonic-gate 
640Sstevel@tonic-gate /*
65*6812Sraf  * Cause interposition on dlclose() and dlopen()
660Sstevel@tonic-gate  */
670Sstevel@tonic-gate #pragma weak dlclose = _tnfctl_dlclose
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #pragma weak dlopen = _tnfctl_dlopen
700Sstevel@tonic-gate 
710Sstevel@tonic-gate /*
720Sstevel@tonic-gate  * The lock used to protect the _tnfctl_internal_tracing_flag variable.
730Sstevel@tonic-gate  *
740Sstevel@tonic-gate  */
750Sstevel@tonic-gate mutex_t		_tnfctl_internalguard_lock = DEFAULTMUTEX;
760Sstevel@tonic-gate boolean_t	_tnfctl_internal_tracing_flag = 0;
770Sstevel@tonic-gate pid_t		_tnfctl_externally_traced_pid = NOPID;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate  * Returns a pointer to a tnfctl handle that can do in process probe control.
810Sstevel@tonic-gate  */
820Sstevel@tonic-gate tnfctl_errcode_t
tnfctl_internal_open(tnfctl_handle_t ** ret_val)830Sstevel@tonic-gate tnfctl_internal_open(tnfctl_handle_t **ret_val)
840Sstevel@tonic-gate {
850Sstevel@tonic-gate 	tnfctl_handle_t	*hdl;
860Sstevel@tonic-gate 	tnfctl_errcode_t	prexstat;
870Sstevel@tonic-gate 	uintptr_t		dbgaddr;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	/* allocate hdl and zero fill */
900Sstevel@tonic-gate 	hdl = calloc(1, sizeof (*hdl));
910Sstevel@tonic-gate 	if (hdl == NULL) {
920Sstevel@tonic-gate 		return (TNFCTL_ERR_ALLOCFAIL);
930Sstevel@tonic-gate 	}
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	hdl->mode = INTERNAL_MODE;
960Sstevel@tonic-gate 	hdl->called_exit = B_FALSE;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	/* plug in inprocess call back functions */
990Sstevel@tonic-gate 	hdl->p_read = inprocess_read;
1000Sstevel@tonic-gate 	hdl->p_write = inprocess_write;
1010Sstevel@tonic-gate 	hdl->p_obj_iter = inprocess_loadobj_iter;
1020Sstevel@tonic-gate 	hdl->p_getpid = inprocess_getpid;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	/*
1050Sstevel@tonic-gate 	 * get the address of DT_DEBUG and store it in proc_p
1060Sstevel@tonic-gate 	 * (the handle on the same process is the dbg address)
1070Sstevel@tonic-gate 	 */
1080Sstevel@tonic-gate 	prexstat = inprocess_get_dtdebug(hdl, &dbgaddr);
1090Sstevel@tonic-gate 	if (prexstat) {
1100Sstevel@tonic-gate 		free(hdl);
1110Sstevel@tonic-gate 		return (prexstat);
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate 	hdl->proc_p = (void *) dbgaddr;
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	/* initialize state in handle */
1160Sstevel@tonic-gate 	prexstat = _tnfctl_set_state(hdl);
1170Sstevel@tonic-gate 	if (prexstat) {
1180Sstevel@tonic-gate 		free(hdl);
1190Sstevel@tonic-gate 		return (prexstat);
1200Sstevel@tonic-gate 	}
1210Sstevel@tonic-gate 	/* see if process is already being traced */
1220Sstevel@tonic-gate 	prexstat = _tnfctl_internal_getlock();
1230Sstevel@tonic-gate 	if (prexstat) {
1240Sstevel@tonic-gate 		free(hdl);
1250Sstevel@tonic-gate 		return (prexstat);
1260Sstevel@tonic-gate 	}
1270Sstevel@tonic-gate 	*ret_val = hdl;
1280Sstevel@tonic-gate 	return (TNFCTL_ERR_NONE);
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate /*
1320Sstevel@tonic-gate  * reads a block of memory from the same address space.
1330Sstevel@tonic-gate  */
1340Sstevel@tonic-gate static int
inprocess_read(void * ignore,uintptr_t addr,void * buf,size_t size)1350Sstevel@tonic-gate inprocess_read(void *ignore, uintptr_t addr, void *buf, size_t size)
1360Sstevel@tonic-gate {
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	DBG_TNF_PROBE_2(inprocess_read_1, "libtnfctl", "sunw%verbosity 3;",
1390Sstevel@tonic-gate 		tnf_long, num_bytes, size,
1400Sstevel@tonic-gate 		tnf_opaque, from_address, addr);
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	(void) memcpy(buf, (void *) addr, size);
1430Sstevel@tonic-gate 	return (0);
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate /*
1470Sstevel@tonic-gate  * writes a block of memory to the same address space.
1480Sstevel@tonic-gate  */
1490Sstevel@tonic-gate static int
inprocess_write(void * ignore,uintptr_t addr,void * buf,size_t size)1500Sstevel@tonic-gate inprocess_write(void *ignore, uintptr_t addr, void *buf, size_t size)
1510Sstevel@tonic-gate {
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	DBG_TNF_PROBE_2(inprocess_write_1, "libtnfctl", "sunw%verbosity 3;",
1540Sstevel@tonic-gate 		tnf_long, num_bytes, size,
1550Sstevel@tonic-gate 		tnf_opaque, to_address, addr);
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	(void) memcpy((void *)addr, buf, size);
1580Sstevel@tonic-gate 	return (0);
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate /*
1620Sstevel@tonic-gate  * returns the pid of the process.
1630Sstevel@tonic-gate  */
1640Sstevel@tonic-gate static pid_t
inprocess_getpid(void * ignore)1650Sstevel@tonic-gate inprocess_getpid(void *ignore)
1660Sstevel@tonic-gate {
1670Sstevel@tonic-gate 	return (getpid());
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate extern Elf3264_Dyn _DYNAMIC;
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate /*
1720Sstevel@tonic-gate  * returns the address of the DT_DEBUG field in the _DYNAMIC array
1730Sstevel@tonic-gate  * of the same address space.
1740Sstevel@tonic-gate  */
1750Sstevel@tonic-gate static tnfctl_errcode_t
inprocess_get_dtdebug(void * hndl,uintptr_t * ret_val)1760Sstevel@tonic-gate inprocess_get_dtdebug(void *hndl, uintptr_t *ret_val)
1770Sstevel@tonic-gate {
1780Sstevel@tonic-gate 	Elf3264_Dyn 	*dyn = &_DYNAMIC;
1790Sstevel@tonic-gate 	Elf3264_Dyn	*dp;
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	for (dp = dyn; dp->d_tag != DT_NULL; dp++) {
1820Sstevel@tonic-gate 		if (dp->d_tag == DT_DEBUG) {
1830Sstevel@tonic-gate 			*ret_val = (uintptr_t) dp;
1840Sstevel@tonic-gate 			return (TNFCTL_ERR_NONE);
1850Sstevel@tonic-gate 		}
1860Sstevel@tonic-gate 	}
1870Sstevel@tonic-gate 	return (TNFCTL_ERR_INTERNAL);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate #define	PROCFORMAT	"/proc/%d"
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate /*
1930Sstevel@tonic-gate  * iterate over all loadobjects in the same address space calling the
1940Sstevel@tonic-gate  * callback function "obj_func".
1950Sstevel@tonic-gate  */
1960Sstevel@tonic-gate static int
inprocess_loadobj_iter(void * opq,tnfctl_ind_obj_f * obj_func,void * cd)1970Sstevel@tonic-gate inprocess_loadobj_iter(void *opq, tnfctl_ind_obj_f *obj_func, void *cd)
1980Sstevel@tonic-gate {
1990Sstevel@tonic-gate 	Elf3264_Dyn	*dtdebug = opq;
2000Sstevel@tonic-gate 	struct r_debug	*r_dbg;
2010Sstevel@tonic-gate 	struct link_map *lmap;
2020Sstevel@tonic-gate 	char		path[MAXPATHLEN];
2030Sstevel@tonic-gate 	int		procfd;
2040Sstevel@tonic-gate 	tnfctl_ind_obj_info_t	loadobj;
2050Sstevel@tonic-gate 	int		retval = 0;	/* sucessful return */
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	DBG_TNF_PROBE_0(inprocess_loadobj_iter_start, "libtnfctl",
2080Sstevel@tonic-gate 			"start inprocess_loadobj_iter; sunw%verbosity 1");
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	r_dbg = (struct r_debug *)dtdebug->d_un.d_ptr;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	DBG_TNF_PROBE_1(inprocess_loadobj_iter_1, "libtnfctl",
2130Sstevel@tonic-gate 		"sunw%verbosity 1",
2140Sstevel@tonic-gate 		tnf_string, link_map_state,
2150Sstevel@tonic-gate 		(r_dbg->r_state == RT_CONSISTENT) ? "RT_CONSISTENT" :
2160Sstevel@tonic-gate 			(r_dbg->r_state == RT_ADD) ? "RT_ADD" : "RT_DELETE");
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	/* bail if link map is not consistent */
2190Sstevel@tonic-gate 	if (r_dbg->r_state != RT_CONSISTENT)
2200Sstevel@tonic-gate 		return (1);
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	(void) sprintf(path, PROCFORMAT, (int) getpid());
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	/*
2250Sstevel@tonic-gate 	 * opening /proc readonly, so debuggers can still run
2260Sstevel@tonic-gate 	 * We use /proc in order to get fd on the object.
2270Sstevel@tonic-gate 	 */
2280Sstevel@tonic-gate 	procfd = open(path, O_RDONLY);
2290Sstevel@tonic-gate 	if (procfd == -1)
2300Sstevel@tonic-gate 		return (1);
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	for (lmap = r_dbg->r_map; lmap; lmap = lmap->l_next) {
2330Sstevel@tonic-gate 		loadobj.text_base = lmap->l_addr;
2340Sstevel@tonic-gate 		loadobj.data_base = lmap->l_addr;
2350Sstevel@tonic-gate 		loadobj.objname = lmap->l_name;
2360Sstevel@tonic-gate 		/*
2370Sstevel@tonic-gate 		 * client of this interface should deal with -1 for objfd,
2380Sstevel@tonic-gate 		 * so no error checking is needed on this ioctl
2390Sstevel@tonic-gate 		 */
2400Sstevel@tonic-gate 		loadobj.objfd = ioctl(procfd, PIOCOPENM, &(lmap->l_addr));
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 		retval = obj_func(opq, &loadobj, cd);
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 		/* close the fd */
2450Sstevel@tonic-gate 		if (loadobj.objfd != -1)
2460Sstevel@tonic-gate 			close(loadobj.objfd);
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 		/* check for error */
2490Sstevel@tonic-gate 		if (retval == 1)
2500Sstevel@tonic-gate 			goto end_of_func;
2510Sstevel@tonic-gate 	}
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate end_of_func:
2540Sstevel@tonic-gate 	close(procfd);
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	DBG_TNF_PROBE_0(inprocess_loadobj_iter_end, "libtnfctl",
2570Sstevel@tonic-gate 			"end inprocess_loadobj_iter; sunw%verbosity 1");
2580Sstevel@tonic-gate 	return (retval);
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate /*
2620Sstevel@tonic-gate  * The lock that prevents a thread from accessing our cached library list
2630Sstevel@tonic-gate  * and a dlopen or dlclose happening at the same time in another thread.
2640Sstevel@tonic-gate  */
2650Sstevel@tonic-gate mutex_t		_tnfctl_lmap_lock = DEFAULTMUTEX;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate /*
2680Sstevel@tonic-gate  * The flag that indicates that the library list has changed via a
2690Sstevel@tonic-gate  * dlopen or dlclose.
2700Sstevel@tonic-gate  */
2710Sstevel@tonic-gate boolean_t	_tnfctl_libs_changed = B_FALSE;
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate /*
2740Sstevel@tonic-gate  * Thread id of the owner of the lock in order to implement a
2750Sstevel@tonic-gate  * recursive lock i.e. no deadlock if the same thread tries to lock
2760Sstevel@tonic-gate  * a lock it already holds.
2770Sstevel@tonic-gate  */
2780Sstevel@tonic-gate static thread_t	lock_holder = 0;	/* XXX - no tid with 0 */
2790Sstevel@tonic-gate NOTE(MUTEX_PROTECTS_DATA(warlock::lmap_lock, lock_holder))
NOTE(DATA_READABLE_WITHOUT_LOCK (lock_holder))2800Sstevel@tonic-gate NOTE(DATA_READABLE_WITHOUT_LOCK(lock_holder))
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate /*
2830Sstevel@tonic-gate  * In the routines below, we will appear to use a different lock if we
2840Sstevel@tonic-gate  * are running lock_lint/warlock.  We define a macro to represent whichever
2850Sstevel@tonic-gate  * lock is appropriate.
2860Sstevel@tonic-gate  */
2870Sstevel@tonic-gate #if defined(__lock_lint)
2880Sstevel@tonic-gate #define	LMAP_LOCK	(&warlock_kludge->lmap_lock)
2890Sstevel@tonic-gate #else
2900Sstevel@tonic-gate #define	LMAP_LOCK	(&_tnfctl_lmap_lock)
2910Sstevel@tonic-gate #endif
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate /*
2940Sstevel@tonic-gate  * dlclose interposition with a recursive lock so that a .fini section
2950Sstevel@tonic-gate  * can recursively call dlopen or dlclose while holding _tnfctl_lmap_lock
2960Sstevel@tonic-gate  * This interposition serializes access to rtld's loadobject list and
2970Sstevel@tonic-gate  * also updates the flag _tnfctl_libs_changed to indicate a change in
2980Sstevel@tonic-gate  * the library list.  This flag is checked by operations that update
2990Sstevel@tonic-gate  * probes so that it can sync up with the new library list and potential
3000Sstevel@tonic-gate  * new/deleted probes.
3010Sstevel@tonic-gate  */
3020Sstevel@tonic-gate int
3030Sstevel@tonic-gate _tnfctl_dlclose(void *handle)
3040Sstevel@tonic-gate {
3050Sstevel@tonic-gate 	static int (*real_dlclose)(void *handle) = NULL;
3060Sstevel@tonic-gate 	int retval;
3070Sstevel@tonic-gate 	thread_t tid;
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	if (real_dlclose == NULL) {
3100Sstevel@tonic-gate 		real_dlclose = (int (*)(void *)) dlsym(RTLD_NEXT, "dlclose");
3110Sstevel@tonic-gate 	}
3120Sstevel@tonic-gate 	assert(real_dlclose);
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	if (mutex_trylock(LMAP_LOCK) != 0) {
3150Sstevel@tonic-gate 		/* don't have lock */
3160Sstevel@tonic-gate 		tid = thr_self();
3170Sstevel@tonic-gate 		if (tid == lock_holder) {
3180Sstevel@tonic-gate 			/* recursive dlopen/dlclose by same thread */
3190Sstevel@tonic-gate 			return ((*real_dlclose)(handle));
3200Sstevel@tonic-gate 		}
3210Sstevel@tonic-gate 		/* not a recursive dlopen/dlclose - wait on lock */
3220Sstevel@tonic-gate 		mutex_lock(LMAP_LOCK);
3230Sstevel@tonic-gate 	}
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	/* lock is held now */
3260Sstevel@tonic-gate 	lock_holder = thr_self();
3270Sstevel@tonic-gate 	retval = (*real_dlclose)(handle);
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	/*
3300Sstevel@tonic-gate 	 * reset lock_holder so that if _tnfctl_lmap_lock is held by some
3310Sstevel@tonic-gate 	 * other part of the code, we don't assume it is a recursive
3320Sstevel@tonic-gate 	 * dlopen/dlclose
3330Sstevel@tonic-gate 	 */
3340Sstevel@tonic-gate 	lock_holder = 0;
3350Sstevel@tonic-gate 	_tnfctl_libs_changed = B_TRUE;
3360Sstevel@tonic-gate 	mutex_unlock(LMAP_LOCK);
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	return (retval);
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate /*
3420Sstevel@tonic-gate  * dlopen interposition with a recursive lock so that a .init section
3430Sstevel@tonic-gate  * can recursively call dlopen or dlclose while holding _tnfctl_lmap_lock
3440Sstevel@tonic-gate  * This interposition serializes access to rtld's loadobject list and
3450Sstevel@tonic-gate  * also updates the flag _tnfctl_libs_changed to indicate a change in
3460Sstevel@tonic-gate  * the library list.  This flag is checked by operations that update
3470Sstevel@tonic-gate  * probes so that it can sync up with the new library list and potential
3480Sstevel@tonic-gate  * new/deleted probes.
3490Sstevel@tonic-gate  */
3500Sstevel@tonic-gate void *
_tnfctl_dlopen(const char * pathname,int mode)3510Sstevel@tonic-gate _tnfctl_dlopen(const char *pathname, int mode)
3520Sstevel@tonic-gate {
3530Sstevel@tonic-gate 	static void * (*real_dlopen)(const char *, int) = NULL;
3540Sstevel@tonic-gate 	void *retval;
3550Sstevel@tonic-gate 	thread_t tid;
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	if (real_dlopen == NULL) {
3580Sstevel@tonic-gate 		real_dlopen = (void * (*)(const char *, int))
3590Sstevel@tonic-gate 					dlsym(RTLD_NEXT, "dlopen");
3600Sstevel@tonic-gate 	}
3610Sstevel@tonic-gate 	assert(real_dlopen);
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	if (mutex_trylock(LMAP_LOCK) != 0) {
3640Sstevel@tonic-gate 		/* don't have lock */
3650Sstevel@tonic-gate 		tid = thr_self();
3660Sstevel@tonic-gate 		if (tid == lock_holder) {
3670Sstevel@tonic-gate 			/* recursive dlopen/dlclose by same thread */
3680Sstevel@tonic-gate 			return ((*real_dlopen)(pathname, mode));
3690Sstevel@tonic-gate 		}
3700Sstevel@tonic-gate 		/* not a recursive dlopen/dlclose - wait on lock */
3710Sstevel@tonic-gate 		mutex_lock(LMAP_LOCK);
3720Sstevel@tonic-gate 	}
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	/* lock is held now */
3750Sstevel@tonic-gate 	lock_holder = thr_self();
3760Sstevel@tonic-gate 	retval = (*real_dlopen)(pathname, mode);
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	/*
3790Sstevel@tonic-gate 	 * reset lock_holder so that if _tnfctl_lmap_lock is held by some
3800Sstevel@tonic-gate 	 * other part of the code, we don't assume it is a recursive
3810Sstevel@tonic-gate 	 * dlopen/dlclose
3820Sstevel@tonic-gate 	 */
3830Sstevel@tonic-gate 	lock_holder = 0;
3840Sstevel@tonic-gate 	_tnfctl_libs_changed = B_TRUE;
3850Sstevel@tonic-gate 	mutex_unlock(LMAP_LOCK);
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	return (retval);
3880Sstevel@tonic-gate }
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate tnfctl_errcode_t
_tnfctl_internal_getlock()3910Sstevel@tonic-gate _tnfctl_internal_getlock()
3920Sstevel@tonic-gate {
3930Sstevel@tonic-gate 	mutex_lock(&_tnfctl_internalguard_lock);
3940Sstevel@tonic-gate 	if (_tnfctl_internal_tracing_flag == 1) {
3950Sstevel@tonic-gate 	/* internal trace control active */
3960Sstevel@tonic-gate 	mutex_unlock(&_tnfctl_internalguard_lock);
3970Sstevel@tonic-gate 	return (TNFCTL_ERR_BUSY);
3980Sstevel@tonic-gate 	}
3990Sstevel@tonic-gate 	_tnfctl_internal_tracing_flag = 1;
4000Sstevel@tonic-gate 	if (_tnfctl_externally_traced_pid == getpid()) {
4010Sstevel@tonic-gate 	/* external trace control is active */
4020Sstevel@tonic-gate 	_tnfctl_internal_tracing_flag = 0;
4030Sstevel@tonic-gate 	mutex_unlock(&_tnfctl_internalguard_lock);
4040Sstevel@tonic-gate 	return (TNFCTL_ERR_BUSY);
4050Sstevel@tonic-gate 	}
4060Sstevel@tonic-gate 	DBG((void) fprintf(stderr, "_tnfctl_internal_getlock: ok to trace %d\n",
4070Sstevel@tonic-gate 	getpid()));
4080Sstevel@tonic-gate 	mutex_unlock(&_tnfctl_internalguard_lock);
4090Sstevel@tonic-gate 	return (TNFCTL_ERR_NONE);
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate #ifdef __lock_lint
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate /*
4160Sstevel@tonic-gate  * dummy function for lock_lint (warlock) static lock analysis.
4170Sstevel@tonic-gate  */
4180Sstevel@tonic-gate int
warlock_dummy()4190Sstevel@tonic-gate warlock_dummy()
4200Sstevel@tonic-gate {
4210Sstevel@tonic-gate 	int (*fp)();
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	return ((*fp)());
4240Sstevel@tonic-gate }
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate #endif
427