17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
5cb62078raf * Common Development and Distribution License (the "License").
6cb62078raf * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
21cb62078raf
227c478bdstevel@tonic-gate/*
23cb62078raf * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bdstevel@tonic-gate
297c478bdstevel@tonic-gate/*
307c478bdstevel@tonic-gate * Includes
317c478bdstevel@tonic-gate */
327c478bdstevel@tonic-gate
337c478bdstevel@tonic-gate#ifndef DEBUG
347c478bdstevel@tonic-gate#define	NDEBUG	1
357c478bdstevel@tonic-gate#endif
367c478bdstevel@tonic-gate
377c478bdstevel@tonic-gate#include <thread.h>
387c478bdstevel@tonic-gate#include <pthread.h>
397c478bdstevel@tonic-gate#include <sys/lwp.h>
407c478bdstevel@tonic-gate#include <synch.h>
417c478bdstevel@tonic-gate#include <sys/types.h>
427c478bdstevel@tonic-gate#include <sys/stat.h>
437c478bdstevel@tonic-gate#include <sys/param.h>
447c478bdstevel@tonic-gate#include <fcntl.h>
457c478bdstevel@tonic-gate#include <dlfcn.h>
467c478bdstevel@tonic-gate#include <string.h>
477c478bdstevel@tonic-gate#include <unistd.h>
487c478bdstevel@tonic-gate#include <stdlib.h>
497c478bdstevel@tonic-gate#include <assert.h>
507c478bdstevel@tonic-gate#include <stdio.h>
517c478bdstevel@tonic-gate#include <errno.h>
527c478bdstevel@tonic-gate#ifdef sparc
537c478bdstevel@tonic-gate#include <setjmp.h>
547c478bdstevel@tonic-gate#endif /* sparc */
557c478bdstevel@tonic-gate
567c478bdstevel@tonic-gate#include "tnf_trace.h"
577c478bdstevel@tonic-gate
587c478bdstevel@tonic-gate/*
597c478bdstevel@tonic-gate * Typedefs
607c478bdstevel@tonic-gate */
617c478bdstevel@tonic-gate
627c478bdstevel@tonic-gatetypedef tnf_ops_t *(*tnf_context_t)(void);
637c478bdstevel@tonic-gate
647c478bdstevel@tonic-gatetypedef void * (*start_func_t)(void *arg);
657c478bdstevel@tonic-gate
667c478bdstevel@tonic-gatetypedef int (*tnf_thr_create_func_t)(void		*stk,
677c478bdstevel@tonic-gate				size_t			stksize,
687c478bdstevel@tonic-gate				start_func_t		startfunc,
697c478bdstevel@tonic-gate				void			*arg,
707c478bdstevel@tonic-gate				long			flags,
717c478bdstevel@tonic-gate				thread_t		*newthread);
727c478bdstevel@tonic-gate
737c478bdstevel@tonic-gatetypedef int (*tnf_pthread_create_func_t)(pthread_t	*thr,
747c478bdstevel@tonic-gate				const pthread_attr_t	*attr,
757c478bdstevel@tonic-gate				start_func_t		startfunc,
767c478bdstevel@tonic-gate				void *			arg);
777c478bdstevel@tonic-gate
787c478bdstevel@tonic-gatetypedef void (*tnf_thr_exit_func_t)(void *) __NORETURN;
797c478bdstevel@tonic-gate
807c478bdstevel@tonic-gatetypedef void (*tnf_pthread_exit_func_t)(void *) __NORETURN;
817c478bdstevel@tonic-gate
827c478bdstevel@tonic-gatetypedef pid_t (*fork_t)(void);
837c478bdstevel@tonic-gate
847c478bdstevel@tonic-gatetypedef int (*tnf_thr_stksegment_func_t)(stack_t *s);
857c478bdstevel@tonic-gate
867c478bdstevel@tonic-gatetypedef struct args {
877c478bdstevel@tonic-gate	start_func_t		real_func;
887c478bdstevel@tonic-gate	void 			*real_arg;
897c478bdstevel@tonic-gate} args_t;
907c478bdstevel@tonic-gate
917c478bdstevel@tonic-gate/*
927c478bdstevel@tonic-gate * Local Declarations
937c478bdstevel@tonic-gate */
947c478bdstevel@tonic-gate
957c478bdstevel@tonic-gatestatic void * tnf_threaded_test(void 			*dummy,
967c478bdstevel@tonic-gate				tnf_probe_control_t 	*probe_p,
977c478bdstevel@tonic-gate				tnf_probe_setup_t 	*set_p);
987c478bdstevel@tonic-gatestatic void * tnf_non_threaded_test(void 			*dummy,
997c478bdstevel@tonic-gate					tnf_probe_control_t	*probe_p,
1007c478bdstevel@tonic-gate					tnf_probe_setup_t	*set_p);
1017c478bdstevel@tonic-gatestatic tnf_ops_t *tnf_probe_getfunc(void);
1027c478bdstevel@tonic-gatestatic void *probestart(void *arg);
1037c478bdstevel@tonic-gatestatic pid_t common_fork(fork_t real_fork);
1047c478bdstevel@tonic-gatestatic void probe_setup(void *data);
1057c478bdstevel@tonic-gatestatic tnf_ops_t *tnf_get_ops();
1067c478bdstevel@tonic-gate
1077c478bdstevel@tonic-gate/*
1087c478bdstevel@tonic-gate * Static Globals
1097c478bdstevel@tonic-gate */
1107c478bdstevel@tonic-gate
1117c478bdstevel@tonic-gateextern tnf_ops_t 	tnf_trace_initial_tpd;
1127c478bdstevel@tonic-gatestatic void 		*tpd = &tnf_trace_initial_tpd;
1137c478bdstevel@tonic-gate#ifdef sparc
1147c478bdstevel@tonic-gatestatic size_t		tnf_probe_dsize = 0;
1157c478bdstevel@tonic-gate#endif /* sparc */
1167c478bdstevel@tonic-gate
1177c478bdstevel@tonic-gate/*
1187c478bdstevel@tonic-gate * Project Private interfaces:
1197c478bdstevel@tonic-gate * 	These are interfaces between prex and libtnfw, or
1207c478bdstevel@tonic-gate * 	between libtnfw and libtthread.
1217c478bdstevel@tonic-gate */
1227c478bdstevel@tonic-gate
1237c478bdstevel@tonic-gate/* variable indicates if libtnfw has sync'ed up with libthread or not */
1247c478bdstevel@tonic-gatelong			__tnf_probe_thr_sync		= 0;
1257c478bdstevel@tonic-gate
1267c478bdstevel@tonic-gate/* head of the list that is used to chain all probes */
1277c478bdstevel@tonic-gatetnf_probe_control_t	*__tnf_probe_list_head		= NULL;
1287c478bdstevel@tonic-gateint			__tnf_probe_list_valid		= 0;
1297c478bdstevel@tonic-gate
1307c478bdstevel@tonic-gate/* notify function that libthread calls after primordial thread is created */
1317c478bdstevel@tonic-gatevoid __tnf_probe_notify(void);
1327c478bdstevel@tonic-gate
1337c478bdstevel@tonic-gatetnf_probe_test_func_t tnf_threaded_test_addr = tnf_threaded_test;
1347c478bdstevel@tonic-gatetnf_probe_test_func_t tnf_non_threaded_test_addr = tnf_non_threaded_test;
1357c478bdstevel@tonic-gate
1367c478bdstevel@tonic-gate
1377c478bdstevel@tonic-gate/*
1387c478bdstevel@tonic-gate * Externs
1397c478bdstevel@tonic-gate */
1407c478bdstevel@tonic-gate#pragma weak thr_probe_getfunc_addr
1417c478bdstevel@tonic-gateextern tnf_context_t	thr_probe_getfunc_addr;
1427c478bdstevel@tonic-gate
1437c478bdstevel@tonic-gate#pragma weak thr_probe_setup
1447c478bdstevel@tonic-gateextern void thr_probe_setup(void *);
1457c478bdstevel@tonic-gate
1467c478bdstevel@tonic-gate/* ---------------------------------------------------------------- */
1477c478bdstevel@tonic-gate/* ----------------------- Public Functions ----------------------- */
1487c478bdstevel@tonic-gate/* ---------------------------------------------------------------- */
1497c478bdstevel@tonic-gate
1507c478bdstevel@tonic-gate/*
1517c478bdstevel@tonic-gate * probe_setup() - the thread probe setup function for the non-threaded
1527c478bdstevel@tonic-gate * case.
1537c478bdstevel@tonic-gate */
1547c478bdstevel@tonic-gatestatic void
1557c478bdstevel@tonic-gateprobe_setup(void *data)
1567c478bdstevel@tonic-gate{
1577c478bdstevel@tonic-gate#ifdef DEBUG
1587c478bdstevel@tonic-gate	/* #### - TEMPORARY */
1597c478bdstevel@tonic-gate	fprintf(stderr, "probe_setup: \n");
1607c478bdstevel@tonic-gate#endif
1617c478bdstevel@tonic-gate	tpd = data;
1627c478bdstevel@tonic-gate
1637c478bdstevel@tonic-gate}   /* end probe_setup */
1647c478bdstevel@tonic-gate
1657c478bdstevel@tonic-gate/*
1667c478bdstevel@tonic-gate * __tnf_probe_notify() - libthread calls this function to notify us
1677c478bdstevel@tonic-gate * that the primordial thread has been created.
1687c478bdstevel@tonic-gate */
1697c478bdstevel@tonic-gate
1707c478bdstevel@tonic-gatevoid
1717c478bdstevel@tonic-gate__tnf_probe_notify(void)
1727c478bdstevel@tonic-gate{
1737c478bdstevel@tonic-gate	tnf_probe_control_t		*prbctl_p;
1747c478bdstevel@tonic-gate	tnf_probe_test_func_t		test_func;
1757c478bdstevel@tonic-gate
1767c478bdstevel@tonic-gate	/* paranoia: thr_probe_setup should be defined */
1777c478bdstevel@tonic-gate	assert(thr_probe_setup != 0);
1787c478bdstevel@tonic-gate	if (thr_probe_setup != 0) thr_probe_setup(tpd);
1797c478bdstevel@tonic-gate
1807c478bdstevel@tonic-gate	/*
1817c478bdstevel@tonic-gate	 * no race with prex if we set flag first
1827c478bdstevel@tonic-gate	 *		- this is an idempotent operation
1837c478bdstevel@tonic-gate	 */
1847c478bdstevel@tonic-gate	__tnf_probe_thr_sync = 1;
1857c478bdstevel@tonic-gate
1867c478bdstevel@tonic-gate#ifdef DEBUG
1877c478bdstevel@tonic-gate	{
1887c478bdstevel@tonic-gate		char tmp_buf[512];
1897c478bdstevel@tonic-gate		(void) sprintf(tmp_buf, "__tnf_probe_notify: \n");
1907c478bdstevel@tonic-gate		(void) write(2, tmp_buf, strlen(tmp_buf));
1917c478bdstevel@tonic-gate	}
1927c478bdstevel@tonic-gate#endif
1937c478bdstevel@tonic-gate	/*
1947c478bdstevel@tonic-gate	 * Use dlsym to test for the present of "thr_probe_getfunc_addr" .
1957c478bdstevel@tonic-gate	 */
1967c478bdstevel@tonic-gate
1977c478bdstevel@tonic-gate	test_func = (((int(*)())dlsym(RTLD_DEFAULT,
1987c478bdstevel@tonic-gate		"thr_probe_getfunc_addr")) != NULL) ? tnf_threaded_test : 0;
1997c478bdstevel@tonic-gate
2007c478bdstevel@tonic-gate	assert(test_func);
2017c478bdstevel@tonic-gate
2027c478bdstevel@tonic-gate	/*
2037c478bdstevel@tonic-gate	 * I think in this case that we do not need to check the
2047c478bdstevel@tonic-gate	 * __tnf_probe_list_valid flag since __tnf_probe_notify is
2057c478bdstevel@tonic-gate	 * called very early.
2067c478bdstevel@tonic-gate	 */
2077c478bdstevel@tonic-gate
2087c478bdstevel@tonic-gate	/* replace all existing test functions with libthread's test func */
2097c478bdstevel@tonic-gate	for (prbctl_p = __tnf_probe_list_head; prbctl_p;
2107c478bdstevel@tonic-gate					prbctl_p = prbctl_p->next)
2117c478bdstevel@tonic-gate		if (prbctl_p->test_func)
2127c478bdstevel@tonic-gate			prbctl_p->test_func = test_func;
2137c478bdstevel@tonic-gate
2147c478bdstevel@tonic-gate	return;
2157c478bdstevel@tonic-gate
2167c478bdstevel@tonic-gate}   /* end __tnf_probe_notify */
2177c478bdstevel@tonic-gate
2187c478bdstevel@tonic-gate/*
2197c478bdstevel@tonic-gate * _tnf_fork_thread_setup - function called by buffering layer
2207c478bdstevel@tonic-gate * whenever it finds a thread in the newly forked process that
2217c478bdstevel@tonic-gate * hasn't been re-initialized in this process.
2227c478bdstevel@tonic-gate */
2237c478bdstevel@tonic-gatevoid
2247c478bdstevel@tonic-gate_tnf_fork_thread_setup(void)
2257c478bdstevel@tonic-gate{
2267c478bdstevel@tonic-gate	tnf_ops_t	*ops;
2277c478bdstevel@tonic-gate
2287c478bdstevel@tonic-gate#ifdef DEBUGFUNCS
2297c478bdstevel@tonic-gate	{
2307c478bdstevel@tonic-gate		char tmp_buf[512];
2317c478bdstevel@tonic-gate		(void) sprintf(tmp_buf, "in _tnf_fork_thread_setup: \n");
2327c478bdstevel@tonic-gate		(void) write(2, tmp_buf, strlen(tmp_buf));
2337c478bdstevel@tonic-gate	}
2347c478bdstevel@tonic-gate#endif
2357c478bdstevel@tonic-gate	/* get the tpd */
2367c478bdstevel@tonic-gate	ops = tnf_get_ops();
2377c478bdstevel@tonic-gate	if (!ops)
2387c478bdstevel@tonic-gate		return;
2397c478bdstevel@tonic-gate	/* null out tag_index, so that a new one is initialized and written */
2407c478bdstevel@tonic-gate	ops->schedule.record_p = 0;
2417c478bdstevel@tonic-gate	return;
2427c478bdstevel@tonic-gate
2437c478bdstevel@tonic-gate}
2447c478bdstevel@tonic-gate
2457c478bdstevel@tonic-gate/* ---------------------------------------------------------------- */
2467c478bdstevel@tonic-gate/* ---------------------- Interposed Functions -------------------- */
2477c478bdstevel@tonic-gate/* ---------------------------------------------------------------- */
2487c478bdstevel@tonic-gate
2497c478bdstevel@tonic-gate/*
2507c478bdstevel@tonic-gate * thr_create() - this function is interposed in front of the
2517c478bdstevel@tonic-gate * actual thread create function in libthread.
2527c478bdstevel@tonic-gate */
2537c478bdstevel@tonic-gate
2547c478bdstevel@tonic-gateint
2557c478bdstevel@tonic-gatethr_create(void 		*stk,
2567c478bdstevel@tonic-gate	size_t		stksize,
2577c478bdstevel@tonic-gate	void *		(*real_func)(void *),
2587c478bdstevel@tonic-gate	void		*real_arg,
2597c478bdstevel@tonic-gate	long		flags,
2607c478bdstevel@tonic-gate	thread_t	*new_thread)
2617c478bdstevel@tonic-gate{
2627c478bdstevel@tonic-gate	static tnf_thr_create_func_t real_thr_create = NULL;
2637c478bdstevel@tonic-gate	args_t *arg_p;
2647c478bdstevel@tonic-gate
2657c478bdstevel@tonic-gate#ifdef VERYVERBOSE
2667c478bdstevel@tonic-gate	fprintf(stderr, "hello from the interposed thr_create parent\n");
2677c478bdstevel@tonic-gate#endif
2687c478bdstevel@tonic-gate
2697c478bdstevel@tonic-gate	/* use dlsym to find the address of the "real" thr_create function */
2707c478bdstevel@tonic-gate	if (real_thr_create == NULL) {
2717c478bdstevel@tonic-gate		real_thr_create = (tnf_thr_create_func_t)
2727c478bdstevel@tonic-gate					dlsym(RTLD_NEXT, "thr_create");
2737c478bdstevel@tonic-gate	}
2747c478bdstevel@tonic-gate	assert(real_thr_create);
2757c478bdstevel@tonic-gate
2767c478bdstevel@tonic-gate	/* set up the interposed argument block */
2777c478bdstevel@tonic-gate	arg_p = (args_t *)malloc(sizeof (args_t));
2787c478bdstevel@tonic-gate	assert(arg_p);
2797c478bdstevel@tonic-gate	arg_p->real_func = real_func;
2807c478bdstevel@tonic-gate	arg_p->real_arg  = real_arg;
2817c478bdstevel@tonic-gate
2827c478bdstevel@tonic-gate	return ((*real_thr_create)(stk, stksize, probestart, (void *) arg_p,
2837c478bdstevel@tonic-gate					flags, new_thread));
2847c478bdstevel@tonic-gate
2857c478bdstevel@tonic-gate}   /* end thr_create */
2867c478bdstevel@tonic-gate
2877c478bdstevel@tonic-gate
2887c478bdstevel@tonic-gateint
2897c478bdstevel@tonic-gatepthread_create(pthread_t *new_thread_id,
2907c478bdstevel@tonic-gate	const pthread_attr_t *attr,
2917c478bdstevel@tonic-gate	void *		(*real_func)(void *),
2927c478bdstevel@tonic-gate	void		*real_arg)
2937c478bdstevel@tonic-gate{
2947c478bdstevel@tonic-gate	static tnf_pthread_create_func_t real_pthread_create = NULL;
2957c478bdstevel@tonic-gate	args_t *arg_p;
2967c478bdstevel@tonic-gate
2977c478bdstevel@tonic-gate#ifdef VERYVERBOSE
2987c478bdstevel@tonic-gate	fprintf(stderr, "hello from the interposed pthread_create parent\n");
2997c478bdstevel@tonic-gate#endif
3007c478bdstevel@tonic-gate
3017c478bdstevel@tonic-gate	/* use dlsym to find the address of the "real" pthread_create func */
3027c478bdstevel@tonic-gate	if (real_pthread_create == NULL) {
3037c478bdstevel@tonic-gate		real_pthread_create = (tnf_pthread_create_func_t)
3047c478bdstevel@tonic-gate					dlsym(RTLD_NEXT, "pthread_create");
3057c478bdstevel@tonic-gate	}
3067c478bdstevel@tonic-gate	assert(real_pthread_create);
3077c478bdstevel@tonic-gate
3087c478bdstevel@tonic-gate	/* set up the interposed argument block */
3097c478bdstevel@tonic-gate	arg_p = (args_t *)malloc(sizeof (args_t));
3107c478bdstevel@tonic-gate	assert(arg_p);
3117c478bdstevel@tonic-gate	arg_p->real_func = real_func;
3127c478bdstevel@tonic-gate	arg_p->real_arg  = real_arg;
3137c478bdstevel@tonic-gate
3147c478bdstevel@tonic-gate	return ((*real_pthread_create)(new_thread_id, attr, probestart,
3157c478bdstevel@tonic-gate			(void *) arg_p));
3167c478bdstevel@tonic-gate
3177c478bdstevel@tonic-gate}   /* end pthread_create */
3187c478bdstevel@tonic-gate
3197c478bdstevel@tonic-gatevoid
3207c478bdstevel@tonic-gatethr_exit(void * status)
3217c478bdstevel@tonic-gate{
3227c478bdstevel@tonic-gate	static tnf_thr_exit_func_t real_thr_exit = NULL;
3237c478bdstevel@tonic-gate	/* use dlsym to find the address of the "real" pthread_create func */
3247c478bdstevel@tonic-gate	if (real_thr_exit == NULL) {
3257c478bdstevel@tonic-gate		real_thr_exit = (tnf_thr_exit_func_t)
3267c478bdstevel@tonic-gate		dlsym(RTLD_NEXT, "thr_exit");
3277c478bdstevel@tonic-gate	}
3287c478bdstevel@tonic-gate	assert(real_thr_exit);
3297c478bdstevel@tonic-gate
3307c478bdstevel@tonic-gate
3317c478bdstevel@tonic-gate	/*
3327c478bdstevel@tonic-gate	 * Calling tnf_thread_disable() whenever a thread exits...
3337c478bdstevel@tonic-gate	 * This has the side-effect of unlocking our currently
3347c478bdstevel@tonic-gate	 * locked block in the trace buffer.  This keeps a dying
3357c478bdstevel@tonic-gate	 * thread from taking a block with it when it dies, but
3367c478bdstevel@tonic-gate	 * it means that we won't be able to trace events from
3377c478bdstevel@tonic-gate	 * the thread-specific data destructors.  We will lose
3387c478bdstevel@tonic-gate	 * out on any events a thread spits out AFTER is calls thr_exit().
3397c478bdstevel@tonic-gate	 * This code was added to fix a bug where tracing breaks when trying
3407c478bdstevel@tonic-gate	 * to trace a program with large numbers of thread-ids.
3417c478bdstevel@tonic-gate	 *
3427c478bdstevel@tonic-gate	 * Addendum:
3437c478bdstevel@tonic-gate	 * Now you can't get events for thr_exit using an interposition library.
3447c478bdstevel@tonic-gate	 * Since thr_exit is a really helpful event, this is a problem.
3457c478bdstevel@tonic-gate	 * Also, breaking this interposition will probably break
3467c478bdstevel@tonic-gate	 * BAT, the DevPro TNF perf tool.
3477c478bdstevel@tonic-gate	 *
3487c478bdstevel@tonic-gate	 * Addendum:
3497c478bdstevel@tonic-gate	 * Correction:  You can get interposition events if the interposition
3507c478bdstevel@tonic-gate	 * library comes BEFORE libtnfprobe.so.  But not, if the interp.
3517c478bdstevel@tonic-gate	 * library comes AFTER libtnfprobe.so.  This is a more difficult
3527c478bdstevel@tonic-gate	 * constraint that it might sound like because of the following:
3537c478bdstevel@tonic-gate	 * The tnfctl functional interface and the prex command line
3547c478bdstevel@tonic-gate	 * interface provide convenience features where you can supply
3557c478bdstevel@tonic-gate	 * a character string argument which will be put into LD_PRELOAD
3567c478bdstevel@tonic-gate	 * for you.  Unfortunately, this string gets appended AFTER
3577c478bdstevel@tonic-gate	 * libtnfprobe.so by the tnfctl library(and also hence by the
3587c478bdstevel@tonic-gate	 * prex -l option).
3597c478bdstevel@tonic-gate	 * Luckily, when libtnfprobe is added by the tnfctl library, it is
3607c478bdstevel@tonic-gate	 * added AFTER an existing contents of the LD_PRELOAD variable.
3617c478bdstevel@tonic-gate	 *
3627c478bdstevel@tonic-gate	 * Therefore, if you are using an interposition library to collect
3637c478bdstevel@tonic-gate	 * thr_exit and pthread_exit events, THEN you should NOT use 'prex -l'
3647c478bdstevel@tonic-gate	 * or the 'ld_preload' argument to tnfctl_exec_open(), instead, you
3657c478bdstevel@tonic-gate	 * should be sure to put the interposition library into the LD_PRELOAD
3667c478bdstevel@tonic-gate	 * variable yourself.
3677c478bdstevel@tonic-gate	 *
3687c478bdstevel@tonic-gate	 */
3697c478bdstevel@tonic-gate
3707c478bdstevel@tonic-gate	tnf_thread_disable();
3717c478bdstevel@tonic-gate
3727c478bdstevel@tonic-gate	((*real_thr_exit)(status));
3737c478bdstevel@tonic-gate}
3747c478bdstevel@tonic-gate
3757c478bdstevel@tonic-gatevoid
3767c478bdstevel@tonic-gatepthread_exit(void * status)
3777c478bdstevel@tonic-gate{
3787c478bdstevel@tonic-gate	static tnf_pthread_exit_func_t real_pthread_exit = NULL;
3797c478bdstevel@tonic-gate	/* use dlsym to find the address of the "real" pthread_create func */
3807c478bdstevel@tonic-gate	if (real_pthread_exit == NULL) {
3817c478bdstevel@tonic-gate		real_pthread_exit = (tnf_pthread_exit_func_t)
3827c478bdstevel@tonic-gate		dlsym(RTLD_NEXT, "pthread_exit");
3837c478bdstevel@tonic-gate	}
3847c478bdstevel@tonic-gate	assert(real_pthread_exit);
3857c478bdstevel@tonic-gate	/* see the comment in thr_exit about tnf_thread_disable() */
3867c478bdstevel@tonic-gate	tnf_thread_disable();
3877c478bdstevel@tonic-gate	((*real_pthread_exit)(status));
3887c478bdstevel@tonic-gate}
3897c478bdstevel@tonic-gate
3907c478bdstevel@tonic-gate/*
3917c478bdstevel@tonic-gate * function to be interposed in front of _resume.  We invalidate the
3927c478bdstevel@tonic-gate * schedule record in case the lwpid changes the next time this
3937c478bdstevel@tonic-gate * thread is scheduled.
3947c478bdstevel@tonic-gate */
3957c478bdstevel@tonic-gate
3967c478bdstevel@tonic-gate#pragma weak _resume_ret = _tnf_resume_ret
3977c478bdstevel@tonic-gatevoid
3987c478bdstevel@tonic-gate_tnf_resume_ret(void *arg1)
3997c478bdstevel@tonic-gate{
4007c478bdstevel@tonic-gate	static void (*real_resume_ret)(void *) = NULL;
4017c478bdstevel@tonic-gate	tnf_ops_t	*ops;
4027c478bdstevel@tonic-gate
4037c478bdstevel@tonic-gate	if (real_resume_ret == NULL) {
4047c478bdstevel@tonic-gate		real_resume_ret = (void (*)(void *)) dlsym(RTLD_NEXT,
4057c478bdstevel@tonic-gate					"_resume_ret");
4067c478bdstevel@tonic-gate	}
4077c478bdstevel@tonic-gate	assert(real_resume_ret);
4087c478bdstevel@tonic-gate
4097c478bdstevel@tonic-gate	ops = tnf_get_ops();
4107c478bdstevel@tonic-gate	if (ops) {
4117c478bdstevel@tonic-gate		/*
4127c478bdstevel@tonic-gate		 * invalidate the schedule record.  This forces it
4137c478bdstevel@tonic-gate		 * to get re-initialized with the new lwpid the next
4147c478bdstevel@tonic-gate		 * time this thread gets scheduled
4157c478bdstevel@tonic-gate		 */
4167c478bdstevel@tonic-gate		if (ops->schedule.lwpid != _lwp_self())
4177c478bdstevel@tonic-gate			ops->schedule.record_p = 0;
4187c478bdstevel@tonic-gate	}
4197c478bdstevel@tonic-gate
4207c478bdstevel@tonic-gate	real_resume_ret(arg1);
4217c478bdstevel@tonic-gate}
4227c478bdstevel@tonic-gate
4237c478bdstevel@tonic-gate/*
4247c478bdstevel@tonic-gate * Functions to be interposed in front of fork and fork1.
4257c478bdstevel@tonic-gate *
4267c478bdstevel@tonic-gate * NOTE: we can't handle vfork, because the child would ruin the parent's
4277c478bdstevel@tonic-gate * data structures.  We therefore don't interpose, letting the child's
4287c478bdstevel@tonic-gate * events appear as though they were the parent's.  A slightly cleaner
4297c478bdstevel@tonic-gate * way to handle vfork would be to interpose on vfork separately to
4307c478bdstevel@tonic-gate * change the pid and anything else needed to show any events caused
4317c478bdstevel@tonic-gate * by the child as its events, and then interpose on the exec's as
4327c478bdstevel@tonic-gate * well to set things back to the way they should be for the parent.
4337c478bdstevel@tonic-gate * But this is a lot of work, and it makes almost no difference, since the
4347c478bdstevel@tonic-gate * child typically exec's very quickly after a vfork.
4357c478bdstevel@tonic-gate */
4367c478bdstevel@tonic-gate
4377c478bdstevel@tonic-gate#pragma weak fork = _tnf_fork
4387c478bdstevel@tonic-gatepid_t
4397c478bdstevel@tonic-gate_tnf_fork(void)
4407c478bdstevel@tonic-gate{
4417c478bdstevel@tonic-gate	static fork_t real_fork = NULL;
4427c478bdstevel@tonic-gate
4437c478bdstevel@tonic-gate	if (real_fork == NULL) {
4447c478bdstevel@tonic-gate		real_fork = (fork_t)dlsym(RTLD_NEXT, "fork");
4457c478bdstevel@tonic-gate	}
4467c478bdstevel@tonic-gate	assert(real_fork);
4477c478bdstevel@tonic-gate	return (common_fork(real_fork));
4487c478bdstevel@tonic-gate}
4497c478bdstevel@tonic-gate
4507c478bdstevel@tonic-gate#pragma weak fork1 = _tnf_fork1
4517c478bdstevel@tonic-gatepid_t
4527c478bdstevel@tonic-gate_tnf_fork1(void)
4537c478bdstevel@tonic-gate{
4547c478bdstevel@tonic-gate	static fork_t real_fork = NULL;
4557c478bdstevel@tonic-gate
4567c478bdstevel@tonic-gate	if (real_fork == NULL) {
4577c478bdstevel@tonic-gate		real_fork = (fork_t)dlsym(RTLD_NEXT, "fork1");
4587c478bdstevel@tonic-gate	}
4597c478bdstevel@tonic-gate	assert(real_fork);
4607c478bdstevel@tonic-gate	return (common_fork(real_fork));
4617c478bdstevel@tonic-gate}
4627c478bdstevel@tonic-gate
4637c478bdstevel@tonic-gate#ifdef sparc
4647c478bdstevel@tonic-gate/*
4657c478bdstevel@tonic-gate * Function to be interposed in front of thr_stksegment
4667c478bdstevel@tonic-gate * _tnf_thr_stksegment() - used to hide the probestart() allocated data
4677c478bdstevel@tonic-gate * on the thread stack, ensuring that the caller receives a pointer to the
4687c478bdstevel@tonic-gate * true bottom (ie, usable) portion of the stack, and the size thereof.
4697c478bdstevel@tonic-gate *
4707c478bdstevel@tonic-gate * NOTE:  On sparc systems, failure to allow for the presense of tnf data
4717c478bdstevel@tonic-gate * on the stack would cause TNF probes to fail across doorfs calls.  The
4727c478bdstevel@tonic-gate * i386 version of door_return decides to "skip over some slop", so no
4737c478bdstevel@tonic-gate * interpose function is required for x86;  if the 512 byte 'slop skip'
4747c478bdstevel@tonic-gate * is ever removed from the i386 door_return, then it will also need
4757c478bdstevel@tonic-gate * interpose function intervention.
476dfb96a4ab *
477dfb96a4ab * Note: Instead of making this function static, we reduce it to local
478dfb96a4ab * scope in the mapfile. That allows the linker to prevent it from
479dfb96a4ab * appearing in the .SUNW_dynsymsort section.
4807c478bdstevel@tonic-gate */
4817c478bdstevel@tonic-gate#pragma weak thr_stksegment = _tnf_thr_stksegment
482dfb96a4abint
4837c478bdstevel@tonic-gate_tnf_thr_stksegment(stack_t *s)
4847c478bdstevel@tonic-gate{
4857c478bdstevel@tonic-gate	static tnf_thr_stksegment_func_t real_thr_stksegment = NULL;
4867c478bdstevel@tonic-gate	int	err;
4877c478bdstevel@tonic-gate
4887c478bdstevel@tonic-gate#ifdef VERYVERBOSE
4897c478bdstevel@tonic-gate	fprintf(stderr, "hello from the interposed thr_stksegment\n");
4907c478bdstevel@tonic-gate#endif
4917c478bdstevel@tonic-gate
4927c478bdstevel@tonic-gate	if (real_thr_stksegment == NULL) {
4937c478bdstevel@tonic-gate		real_thr_stksegment = (tnf_thr_stksegment_func_t)
4947c478bdstevel@tonic-gate		    dlsym(RTLD_NEXT, "thr_stksegment");
4957c478bdstevel@tonic-gate	}
4967c478bdstevel@tonic-gate	assert(real_thr_stksegment);
4977c478bdstevel@tonic-gate
4987c478bdstevel@tonic-gate	err = ((*real_thr_stksegment)(s));
4997c478bdstevel@tonic-gate	if (err == 0) {
5007c478bdstevel@tonic-gate		s->ss_sp = (void *)((caddr_t)s->ss_sp - tnf_probe_dsize);
5017c478bdstevel@tonic-gate		s->ss_size -= tnf_probe_dsize;
5027c478bdstevel@tonic-gate	}
5037c478bdstevel@tonic-gate	return (err);
5047c478bdstevel@tonic-gate}
5057c478bdstevel@tonic-gate#endif /* sparc */
5067c478bdstevel@tonic-gate
5077c478bdstevel@tonic-gate/* ---------------------------------------------------------------- */
5087c478bdstevel@tonic-gate/* ----------------------- Private Functions ---------------------- */
5097c478bdstevel@tonic-gate/* ---------------------------------------------------------------- */
5107c478bdstevel@tonic-gate
5117c478bdstevel@tonic-gate/*
5127c478bdstevel@tonic-gate * tnf_probe_getfunc() - default test function if libthread is not
5137c478bdstevel@tonic-gate * present
5147c478bdstevel@tonic-gate */
5157c478bdstevel@tonic-gatestatic tnf_ops_t *
5167c478bdstevel@tonic-gatetnf_probe_getfunc(void)
5177c478bdstevel@tonic-gate{
5187c478bdstevel@tonic-gate	/* test function to be used if libthread is not linked in */
5197c478bdstevel@tonic-gate#ifdef DEBUGFUNCS
5207c478bdstevel@tonic-gate	{
5217c478bdstevel@tonic-gate		char tmp_buf[512];
5227c478bdstevel@tonic-gate		(void) sprintf(tmp_buf, "tnf_probe_getfunc: \n");
5237c478bdstevel@tonic-gate		(void) write(2, tmp_buf, strlen(tmp_buf));
5247c478bdstevel@tonic-gate	}
5257c478bdstevel@tonic-gate#endif
5267c478bdstevel@tonic-gate	return (tpd);
5277c478bdstevel@tonic-gate}   /* end tnf_probe_getfunc */
5287c478bdstevel@tonic-gate
5297c478bdstevel@tonic-gate
5307c478bdstevel@tonic-gate/*
5317c478bdstevel@tonic-gate * probestart() - this function is called as the start_func by the
5327c478bdstevel@tonic-gate * interposed thr_create() and pthread_create().  It calls the real start
5337c478bdstevel@tonic-gate * function.
5347c478bdstevel@tonic-gate */
5357c478bdstevel@tonic-gate
5367c478bdstevel@tonic-gatestatic void *
5377c478bdstevel@tonic-gateprobestart(void * arg)
5387c478bdstevel@tonic-gate{
5397c478bdstevel@tonic-gate	args_t 		*args_p = (args_t *)arg;
5407c478bdstevel@tonic-gate	start_func_t	real_func;
5417c478bdstevel@tonic-gate	void		*real_arg;
5427c478bdstevel@tonic-gate	tnf_ops_t	ops;		/* allocated on stack */
5437c478bdstevel@tonic-gate	void		*real_retval;
5447c478bdstevel@tonic-gate
5457c478bdstevel@tonic-gate#ifdef VERYVERBOSE
5467c478bdstevel@tonic-gate	fprintf(stderr, "hello from the interposed thr_create child\n");
5477c478bdstevel@tonic-gate#endif
5487c478bdstevel@tonic-gate#ifdef sparc
5497c478bdstevel@tonic-gate	/*
5507c478bdstevel@tonic-gate	 * if the size of the probe data has not yet been calculated,
5517c478bdstevel@tonic-gate	 * initialize a jmpbuffer and calculate the amount of stack space
5527c478bdstevel@tonic-gate	 * used by probestart:  %fp - %sp from jmp_buf
5537c478bdstevel@tonic-gate	 * Not expecting anything to actually longjmp here, so that is
5547c478bdstevel@tonic-gate	 * handled as an error condition.
5557c478bdstevel@tonic-gate	 */
5567c478bdstevel@tonic-gate	if (tnf_probe_dsize == 0) {
5577c478bdstevel@tonic-gate		jmp_buf tnf_jmpbuf;
5587c478bdstevel@tonic-gate		if (setjmp(tnf_jmpbuf) != 0) {
5597c478bdstevel@tonic-gate			(void) write(2,
5607c478bdstevel@tonic-gate			    "probestart:  unexpected longjmp\n", 32);
5617c478bdstevel@tonic-gate			assert(0);
5627c478bdstevel@tonic-gate		}
5637c478bdstevel@tonic-gate		tnf_probe_dsize = (size_t)(tnf_jmpbuf[3] - tnf_jmpbuf[1]);
5647c478bdstevel@tonic-gate	}
5657c478bdstevel@tonic-gate#endif /* sparc */
5667c478bdstevel@tonic-gate
5677c478bdstevel@tonic-gate	/* initialize ops */
5687c478bdstevel@tonic-gate	(void) memset(&ops, 0, sizeof (ops));	/* zero ops */
5697c478bdstevel@tonic-gate	ops.mode = TNF_ALLOC_REUSABLE;
5707c478bdstevel@tonic-gate	ops.alloc = tnfw_b_alloc;
5717c478bdstevel@tonic-gate	ops.commit = tnfw_b_xcommit;
5727c478bdstevel@tonic-gate	ops.rollback = tnfw_b_xabort;
5737c478bdstevel@tonic-gate
5747c478bdstevel@tonic-gate	/* copy (and free) the allocated arg block */
5757c478bdstevel@tonic-gate	real_func = args_p->real_func;
5767c478bdstevel@tonic-gate	real_arg  = args_p->real_arg;
5777c478bdstevel@tonic-gate	free(args_p);
5787c478bdstevel@tonic-gate
5797c478bdstevel@tonic-gate	/* paranoia: thr_probe_setup should be defined */
5807c478bdstevel@tonic-gate	assert(thr_probe_setup != 0);
5817c478bdstevel@tonic-gate	if (thr_probe_setup != 0) thr_probe_setup(&ops);
5827c478bdstevel@tonic-gate
5837c478bdstevel@tonic-gate#ifdef VERYVERBOSE
5847c478bdstevel@tonic-gate	fprintf(stderr, "in middle of interposed start procedure\n");
5857c478bdstevel@tonic-gate#endif
5867c478bdstevel@tonic-gate
5877c478bdstevel@tonic-gate	real_retval = (*real_func)(real_arg);
5887c478bdstevel@tonic-gate
5897c478bdstevel@tonic-gate	/*
5907c478bdstevel@tonic-gate	 * we need to write a NULL into the tpd pointer to disable
5917c478bdstevel@tonic-gate	 * tracing for this thread.
5927c478bdstevel@tonic-gate	 * CAUTION: never make this function tail recursive because
5937c478bdstevel@tonic-gate	 * tpd is allocated on stack.
5947c478bdstevel@tonic-gate	 */
5957c478bdstevel@tonic-gate
5967c478bdstevel@tonic-gate	/* This should be handled by the call to tnf_thread_disable() */
5977c478bdstevel@tonic-gate	/* if (thr_probe_setup != 0) */
5987c478bdstevel@tonic-gate	/* thr_probe_setup(NULL); */
5997c478bdstevel@tonic-gate
6007c478bdstevel@tonic-gate	/* see the comment in thr_exit about tnf_thread_disable */
6017c478bdstevel@tonic-gate	tnf_thread_disable();
6027c478bdstevel@tonic-gate
6037c478bdstevel@tonic-gate	return (real_retval);
6047c478bdstevel@tonic-gate
6057c478bdstevel@tonic-gate}   /* end probestart */
6067c478bdstevel@tonic-gate
6077c478bdstevel@tonic-gate
608cb62078rafstatic thread_key_t tpd_key = THR_ONCE_KEY;
6097c478bdstevel@tonic-gatestatic tnf_ops_t *stashed_tpd = NULL;
6107c478bdstevel@tonic-gate
6117c478bdstevel@tonic-gate/*
6127c478bdstevel@tonic-gate * tnf_thread_disable: API to disable a thread
6137c478bdstevel@tonic-gate */
6147c478bdstevel@tonic-gatevoid
6157c478bdstevel@tonic-gatetnf_thread_disable(void)
6167c478bdstevel@tonic-gate{
6177c478bdstevel@tonic-gate	tnf_ops_t		*ops;
6187c478bdstevel@tonic-gate
6197c478bdstevel@tonic-gate	if (thr_probe_setup != 0) {
6207c478bdstevel@tonic-gate		/* threaded client */
6217c478bdstevel@tonic-gate
622cb62078raf		/* REMIND: destructor function ? */
623cb62078raf		(void) thr_keycreate_once(&tpd_key, NULL);
6247c478bdstevel@tonic-gate		/* get the tpd */
6257c478bdstevel@tonic-gate		ops = thr_probe_getfunc_addr();
6267c478bdstevel@tonic-gate		/* check ops to ensure function is idempotent */
6277c478bdstevel@tonic-gate		if (ops != NULL) {
6287c478bdstevel@tonic-gate			/* unlock currently held blocks */
6297c478bdstevel@tonic-gate			tnfw_b_release_block(&ops->wcb);
6307c478bdstevel@tonic-gate			/* disable the thread */
6317c478bdstevel@tonic-gate			thr_probe_setup(NULL);
6327c478bdstevel@tonic-gate			/* stash the tpd */
6337c478bdstevel@tonic-gate			(void) thr_setspecific(tpd_key, ops);
6347c478bdstevel@tonic-gate		}
6357c478bdstevel@tonic-gate	} else {
6367c478bdstevel@tonic-gate		/* non-threaded client */
6377c478bdstevel@tonic-gate
6387c478bdstevel@tonic-gate		/* get the tpd */
6397c478bdstevel@tonic-gate		ops = tnf_probe_getfunc();
6407c478bdstevel@tonic-gate		if (ops != NULL) {
6417c478bdstevel@tonic-gate			/* disable the process */
6427c478bdstevel@tonic-gate			probe_setup(NULL);
6437c478bdstevel@tonic-gate			/* stash the tpd */
6447c478bdstevel@tonic-gate			stashed_tpd = ops;
6457c478bdstevel@tonic-gate		}
6467c478bdstevel@tonic-gate	}
6477c478bdstevel@tonic-gate}
6487c478bdstevel@tonic-gate
6497c478bdstevel@tonic-gate/*
6507c478bdstevel@tonic-gate * tnf_thread_enable: API to enable a thread
6517c478bdstevel@tonic-gate */
6527c478bdstevel@tonic-gatevoid
6537c478bdstevel@tonic-gatetnf_thread_enable(void)
6547c478bdstevel@tonic-gate{
6557c478bdstevel@tonic-gate	tnf_ops_t *ops;
6567c478bdstevel@tonic-gate
6577c478bdstevel@tonic-gate	if (thr_probe_setup != 0) {
6587c478bdstevel@tonic-gate		/* threaded client */
6597c478bdstevel@tonic-gate
660cb62078raf		ops = pthread_getspecific(tpd_key);
661cb62078raf		if (ops)
662cb62078raf			thr_probe_setup(ops);
6637c478bdstevel@tonic-gate	} else {
6647c478bdstevel@tonic-gate		/* non-threaded client */
6657c478bdstevel@tonic-gate
6667c478bdstevel@tonic-gate		ops = stashed_tpd;
667cb62078raf		if (ops)
6687c478bdstevel@tonic-gate			probe_setup(ops);
6697c478bdstevel@tonic-gate	}
6707c478bdstevel@tonic-gate}
6717c478bdstevel@tonic-gate
6727c478bdstevel@tonic-gate/*
6737c478bdstevel@tonic-gate * common_fork - code that is common among the interpositions of
6747c478bdstevel@tonic-gate * fork, fork1, and vfork
6757c478bdstevel@tonic-gate */
6767c478bdstevel@tonic-gatestatic pid_t
6777c478bdstevel@tonic-gatecommon_fork(fork_t real_fork)
6787c478bdstevel@tonic-gate{
6797c478bdstevel@tonic-gate	pid_t		 retval;
6807c478bdstevel@tonic-gate	tnf_ops_t	*ops;
6817c478bdstevel@tonic-gate	tnf_tag_data_t	*metatag_data;
6827c478bdstevel@tonic-gate
6837c478bdstevel@tonic-gate#ifdef DEBUGFUNCS
6847c478bdstevel@tonic-gate	{
6857c478bdstevel@tonic-gate		char tmp_buf[512];
6867c478bdstevel@tonic-gate		(void) sprintf(tmp_buf, "in interposed fork: \n");
6877c478bdstevel@tonic-gate		(void) write(2, tmp_buf, strlen(tmp_buf));
6887c478bdstevel@tonic-gate	}
6897c478bdstevel@tonic-gate#endif
6907c478bdstevel@tonic-gate	if ((_tnfw_b_control->tnf_state == TNFW_B_NOBUFFER) &&
6917c478bdstevel@tonic-gate				(tnf_trace_file_name[0] != '\0')) {
6927c478bdstevel@tonic-gate		/*
6937c478bdstevel@tonic-gate		 * if no buffer has been allocated yet, and prex plugged in
6947c478bdstevel@tonic-gate		 * name...
6957c478bdstevel@tonic-gate		 */
6967c478bdstevel@tonic-gate		ops = tnf_get_ops();
6977c478bdstevel@tonic-gate		if (ops == NULL) {
6987c478bdstevel@tonic-gate			/*
6997c478bdstevel@tonic-gate			 * get it from stashed location
7007c478bdstevel@tonic-gate			 * don't enable thread though
7017c478bdstevel@tonic-gate			 */
7027c478bdstevel@tonic-gate			if (thr_probe_setup != 0) {
7037c478bdstevel@tonic-gate				/* threaded client */
704cb62078raf				ops = pthread_getspecific(tpd_key);
7057c478bdstevel@tonic-gate			} else {
7067c478bdstevel@tonic-gate				/* non-threaded client */
7077c478bdstevel@tonic-gate				ops = stashed_tpd;
7087c478bdstevel@tonic-gate			}
7097c478bdstevel@tonic-gate		}
7107c478bdstevel@tonic-gate
7117c478bdstevel@tonic-gate		/*
7127c478bdstevel@tonic-gate		 * ops shouldn't be NULL.  But, if it is, then we don't
7137c478bdstevel@tonic-gate		 * initialize tracing.  In the child, tracing will be
7147c478bdstevel@tonic-gate		 * set to broken.
7157c478bdstevel@tonic-gate		 */
7167c478bdstevel@tonic-gate		if (ops) {
7177c478bdstevel@tonic-gate			/* initialize tracing */
7187c478bdstevel@tonic-gate			ops->busy = 1;
7197c478bdstevel@tonic-gate			metatag_data = TAG_DATA(tnf_struct_type);
7207c478bdstevel@tonic-gate			metatag_data->tag_desc(ops, metatag_data);
7217c478bdstevel@tonic-gate			/* commit the data */
7227c478bdstevel@tonic-gate			(void) ops->commit(&(ops->wcb));
7237c478bdstevel@tonic-gate			ops->busy = 0;
7247c478bdstevel@tonic-gate		}
7257c478bdstevel@tonic-gate	}
7267c478bdstevel@tonic-gate
7277c478bdstevel@tonic-gate	retval = real_fork();
7287c478bdstevel@tonic-gate	if (retval == 0) {
7297c478bdstevel@tonic-gate		/* child process */
7307c478bdstevel@tonic-gate		_tnfw_b_control->tnf_pid = getpid();
7317c478bdstevel@tonic-gate		if ((_tnfw_b_control->tnf_state == TNFW_B_NOBUFFER) &&
7327c478bdstevel@tonic-gate				(tnf_trace_file_name[0] != '\0')) {
7337c478bdstevel@tonic-gate			/*
7347c478bdstevel@tonic-gate			 * race condition, prex attached after condition was
7357c478bdstevel@tonic-gate			 * checked in parent, so both parent and child point at
7367c478bdstevel@tonic-gate			 * the same file name and will overwrite each other.
7377c478bdstevel@tonic-gate			 * So, we set tracing to broken in child.  We could
7387c478bdstevel@tonic-gate			 * invent a new state called RACE and use prex to
7397c478bdstevel@tonic-gate			 * reset it, if needed...
7407c478bdstevel@tonic-gate			 */
7417c478bdstevel@tonic-gate			tnf_trace_file_name[0] = '\0';
7427c478bdstevel@tonic-gate			_tnfw_b_control->tnf_state = TNFW_B_BROKEN;
7437c478bdstevel@tonic-gate		} else if (_tnfw_b_control->tnf_state == TNFW_B_RUNNING) {
7447c478bdstevel@tonic-gate			/* normal expected condition */
7457c478bdstevel@tonic-gate			_tnfw_b_control->tnf_state = TNFW_B_FORKED;
7467c478bdstevel@tonic-gate		}
7477c478bdstevel@tonic-gate	}
7487c478bdstevel@tonic-gate	return (retval);
7497c478bdstevel@tonic-gate}
7507c478bdstevel@tonic-gate
7517c478bdstevel@tonic-gate/*
7527c478bdstevel@tonic-gate * tnf_threaded_test
7537c478bdstevel@tonic-gate */
7547c478bdstevel@tonic-gate/*ARGSUSED0*/
7557c478bdstevel@tonic-gatestatic void *
7567c478bdstevel@tonic-gatetnf_threaded_test(void *dummy, tnf_probe_control_t *probe_p,
7577c478bdstevel@tonic-gate			tnf_probe_setup_t *set_p)
7587c478bdstevel@tonic-gate{
7597c478bdstevel@tonic-gate	tnf_ops_t *tpd_p;
7607c478bdstevel@tonic-gate
7617c478bdstevel@tonic-gate	tpd_p = thr_probe_getfunc_addr();
7627c478bdstevel@tonic-gate	if (tpd_p) {
7637c478bdstevel@tonic-gate		return (probe_p->alloc_func(tpd_p, probe_p, set_p));
7647c478bdstevel@tonic-gate	}
7657c478bdstevel@tonic-gate	return (NULL);
7667c478bdstevel@tonic-gate}
7677c478bdstevel@tonic-gate
7687c478bdstevel@tonic-gate
7697c478bdstevel@tonic-gate/*
7707c478bdstevel@tonic-gate * tnf_non_threaded_test
7717c478bdstevel@tonic-gate */
7727c478bdstevel@tonic-gate/*ARGSUSED0*/
7737c478bdstevel@tonic-gatestatic void *
7747c478bdstevel@tonic-gatetnf_non_threaded_test(void *dummy, tnf_probe_control_t *probe_p,
7757c478bdstevel@tonic-gate				tnf_probe_setup_t *set_p)
7767c478bdstevel@tonic-gate{
7777c478bdstevel@tonic-gate	tnf_ops_t *tpd_p;
7787c478bdstevel@tonic-gate
7797c478bdstevel@tonic-gate	tpd_p = tnf_probe_getfunc();
7807c478bdstevel@tonic-gate	if (tpd_p) {
7817c478bdstevel@tonic-gate		return (probe_p->alloc_func(tpd_p, probe_p, set_p));
7827c478bdstevel@tonic-gate	}
7837c478bdstevel@tonic-gate	return (NULL);
7847c478bdstevel@tonic-gate}
7857c478bdstevel@tonic-gate
7867c478bdstevel@tonic-gate/*
7877c478bdstevel@tonic-gate * tnf_get_ops() returns the ops pointer (thread-private data), or NULL
7887c478bdstevel@tonic-gate * if tracing is disabled for this thread.
7897c478bdstevel@tonic-gate */
7907c478bdstevel@tonic-gatestatic tnf_ops_t *
7917c478bdstevel@tonic-gatetnf_get_ops()
7927c478bdstevel@tonic-gate{
7937c478bdstevel@tonic-gate	tnf_context_t	*test_func_p = &thr_probe_getfunc_addr;
7947c478bdstevel@tonic-gate	tnf_context_t	 test_func;
7957c478bdstevel@tonic-gate
7967c478bdstevel@tonic-gate	/*
7977c478bdstevel@tonic-gate	 * IMPORTANT: this test to see whether thr_probe_getfunc_addr
7987c478bdstevel@tonic-gate	 * is bound is tricky.  The compiler currently has a bug
7997c478bdstevel@tonic-gate	 * (1263684) that causes the test to be optimized away unless
8007c478bdstevel@tonic-gate	 * coded with an intermediate pointer (test_func_p).  This
8017c478bdstevel@tonic-gate	 * causes the process to SEGV when the variable is not bound.
8027c478bdstevel@tonic-gate	 */
8037c478bdstevel@tonic-gate
8047c478bdstevel@tonic-gate	test_func = test_func_p ? *test_func_p : tnf_probe_getfunc;
8057c478bdstevel@tonic-gate	return ((*test_func)());
8067c478bdstevel@tonic-gate}
807