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