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