10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51677Sdp * Common Development and Distribution License (the "License").
61677Sdp * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21390Sraf
220Sstevel@tonic-gate /*
238803SJonathan.Haslam@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include <sys/atomic.h>
290Sstevel@tonic-gate #include <sys/errno.h>
300Sstevel@tonic-gate #include <sys/stat.h>
310Sstevel@tonic-gate #include <sys/modctl.h>
320Sstevel@tonic-gate #include <sys/conf.h>
330Sstevel@tonic-gate #include <sys/systm.h>
340Sstevel@tonic-gate #include <sys/ddi.h>
350Sstevel@tonic-gate #include <sys/sunddi.h>
360Sstevel@tonic-gate #include <sys/cpuvar.h>
370Sstevel@tonic-gate #include <sys/kmem.h>
380Sstevel@tonic-gate #include <sys/strsubr.h>
390Sstevel@tonic-gate #include <sys/fasttrap.h>
400Sstevel@tonic-gate #include <sys/fasttrap_impl.h>
410Sstevel@tonic-gate #include <sys/fasttrap_isa.h>
420Sstevel@tonic-gate #include <sys/dtrace.h>
430Sstevel@tonic-gate #include <sys/dtrace_impl.h>
440Sstevel@tonic-gate #include <sys/sysmacros.h>
450Sstevel@tonic-gate #include <sys/proc.h>
460Sstevel@tonic-gate #include <sys/priv.h>
470Sstevel@tonic-gate #include <sys/policy.h>
483944Sahl #include <util/qsort.h>
490Sstevel@tonic-gate
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate * User-Land Trap-Based Tracing
520Sstevel@tonic-gate * ----------------------------
530Sstevel@tonic-gate *
540Sstevel@tonic-gate * The fasttrap provider allows DTrace consumers to instrument any user-level
550Sstevel@tonic-gate * instruction to gather data; this includes probes with semantic
560Sstevel@tonic-gate * signifigance like entry and return as well as simple offsets into the
570Sstevel@tonic-gate * function. While the specific techniques used are very ISA specific, the
580Sstevel@tonic-gate * methodology is generalizable to any architecture.
590Sstevel@tonic-gate *
600Sstevel@tonic-gate *
610Sstevel@tonic-gate * The General Methodology
620Sstevel@tonic-gate * -----------------------
630Sstevel@tonic-gate *
640Sstevel@tonic-gate * With the primary goal of tracing every user-land instruction and the
650Sstevel@tonic-gate * limitation that we can't trust user space so don't want to rely on much
660Sstevel@tonic-gate * information there, we begin by replacing the instructions we want to trace
670Sstevel@tonic-gate * with trap instructions. Each instruction we overwrite is saved into a hash
680Sstevel@tonic-gate * table keyed by process ID and pc address. When we enter the kernel due to
690Sstevel@tonic-gate * this trap instruction, we need the effects of the replaced instruction to
700Sstevel@tonic-gate * appear to have occurred before we proceed with the user thread's
710Sstevel@tonic-gate * execution.
720Sstevel@tonic-gate *
730Sstevel@tonic-gate * Each user level thread is represented by a ulwp_t structure which is
740Sstevel@tonic-gate * always easily accessible through a register. The most basic way to produce
750Sstevel@tonic-gate * the effects of the instruction we replaced is to copy that instruction out
760Sstevel@tonic-gate * to a bit of scratch space reserved in the user thread's ulwp_t structure
770Sstevel@tonic-gate * (a sort of kernel-private thread local storage), set the PC to that
780Sstevel@tonic-gate * scratch space and single step. When we reenter the kernel after single
790Sstevel@tonic-gate * stepping the instruction we must then adjust the PC to point to what would
800Sstevel@tonic-gate * normally be the next instruction. Of course, special care must be taken
810Sstevel@tonic-gate * for branches and jumps, but these represent such a small fraction of any
820Sstevel@tonic-gate * instruction set that writing the code to emulate these in the kernel is
830Sstevel@tonic-gate * not too difficult.
840Sstevel@tonic-gate *
850Sstevel@tonic-gate * Return probes may require several tracepoints to trace every return site,
860Sstevel@tonic-gate * and, conversely, each tracepoint may activate several probes (the entry
870Sstevel@tonic-gate * and offset 0 probes, for example). To solve this muliplexing problem,
880Sstevel@tonic-gate * tracepoints contain lists of probes to activate and probes contain lists
890Sstevel@tonic-gate * of tracepoints to enable. If a probe is activated, it adds its ID to
900Sstevel@tonic-gate * existing tracepoints or creates new ones as necessary.
910Sstevel@tonic-gate *
920Sstevel@tonic-gate * Most probes are activated _before_ the instruction is executed, but return
930Sstevel@tonic-gate * probes are activated _after_ the effects of the last instruction of the
940Sstevel@tonic-gate * function are visible. Return probes must be fired _after_ we have
950Sstevel@tonic-gate * single-stepped the instruction whereas all other probes are fired
960Sstevel@tonic-gate * beforehand.
972179Sahl *
982179Sahl *
992179Sahl * Lock Ordering
1002179Sahl * -------------
1012179Sahl *
1022179Sahl * The lock ordering below -- both internally and with respect to the DTrace
1032179Sahl * framework -- is a little tricky and bears some explanation. Each provider
1042179Sahl * has a lock (ftp_mtx) that protects its members including reference counts
1052179Sahl * for enabled probes (ftp_rcount), consumers actively creating probes
1062179Sahl * (ftp_ccount) and USDT consumers (ftp_mcount); all three prevent a provider
1072179Sahl * from being freed. A provider is looked up by taking the bucket lock for the
1082179Sahl * provider hash table, and is returned with its lock held. The provider lock
1092179Sahl * may be taken in functions invoked by the DTrace framework, but may not be
1102179Sahl * held while calling functions in the DTrace framework.
1112179Sahl *
1122179Sahl * To ensure consistency over multiple calls to the DTrace framework, the
1132179Sahl * creation lock (ftp_cmtx) should be held. Naturally, the creation lock may
1142179Sahl * not be taken when holding the provider lock as that would create a cyclic
1152179Sahl * lock ordering. In situations where one would naturally take the provider
1162179Sahl * lock and then the creation lock, we instead up a reference count to prevent
1172179Sahl * the provider from disappearing, drop the provider lock, and acquire the
1182179Sahl * creation lock.
1192179Sahl *
1202179Sahl * Briefly:
1212179Sahl * bucket lock before provider lock
1222179Sahl * DTrace before provider lock
1232179Sahl * creation lock before DTrace
1242179Sahl * never hold the provider lock and creation lock simultaneously
1250Sstevel@tonic-gate */
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate static dev_info_t *fasttrap_devi;
1280Sstevel@tonic-gate static dtrace_meta_provider_id_t fasttrap_meta_id;
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate static timeout_id_t fasttrap_timeout;
1310Sstevel@tonic-gate static kmutex_t fasttrap_cleanup_mtx;
1320Sstevel@tonic-gate static uint_t fasttrap_cleanup_work;
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate /*
1350Sstevel@tonic-gate * Generation count on modifications to the global tracepoint lookup table.
1360Sstevel@tonic-gate */
1370Sstevel@tonic-gate static volatile uint64_t fasttrap_mod_gen;
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate /*
1400Sstevel@tonic-gate * When the fasttrap provider is loaded, fasttrap_max is set to either
1410Sstevel@tonic-gate * FASTTRAP_MAX_DEFAULT or the value for fasttrap-max-probes in the
1420Sstevel@tonic-gate * fasttrap.conf file. Each time a probe is created, fasttrap_total is
1430Sstevel@tonic-gate * incremented by the number of tracepoints that may be associated with that
1440Sstevel@tonic-gate * probe; fasttrap_total is capped at fasttrap_max.
1450Sstevel@tonic-gate */
1460Sstevel@tonic-gate #define FASTTRAP_MAX_DEFAULT 250000
1470Sstevel@tonic-gate static uint32_t fasttrap_max;
1480Sstevel@tonic-gate static uint32_t fasttrap_total;
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate #define FASTTRAP_TPOINTS_DEFAULT_SIZE 0x4000
1520Sstevel@tonic-gate #define FASTTRAP_PROVIDERS_DEFAULT_SIZE 0x100
153532Sahl #define FASTTRAP_PROCS_DEFAULT_SIZE 0x100
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate #define FASTTRAP_PID_NAME "pid"
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate fasttrap_hash_t fasttrap_tpoints;
1580Sstevel@tonic-gate static fasttrap_hash_t fasttrap_provs;
159532Sahl static fasttrap_hash_t fasttrap_procs;
1600Sstevel@tonic-gate
1612179Sahl static uint64_t fasttrap_pid_count; /* pid ref count */
1620Sstevel@tonic-gate static kmutex_t fasttrap_count_mtx; /* lock on ref count */
1630Sstevel@tonic-gate
164315Sahl #define FASTTRAP_ENABLE_FAIL 1
165315Sahl #define FASTTRAP_ENABLE_PARTIAL 2
166315Sahl
1670Sstevel@tonic-gate static int fasttrap_tracepoint_enable(proc_t *, fasttrap_probe_t *, uint_t);
1680Sstevel@tonic-gate static void fasttrap_tracepoint_disable(proc_t *, fasttrap_probe_t *, uint_t);
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate static fasttrap_provider_t *fasttrap_provider_lookup(pid_t, const char *,
1710Sstevel@tonic-gate const dtrace_pattr_t *);
172935Sahl static void fasttrap_provider_retire(pid_t, const char *, int);
1730Sstevel@tonic-gate static void fasttrap_provider_free(fasttrap_provider_t *);
1740Sstevel@tonic-gate
175532Sahl static fasttrap_proc_t *fasttrap_proc_lookup(pid_t);
176532Sahl static void fasttrap_proc_release(fasttrap_proc_t *);
177532Sahl
1780Sstevel@tonic-gate #define FASTTRAP_PROVS_INDEX(pid, name) \
1790Sstevel@tonic-gate ((fasttrap_hash_str(name) + (pid)) & fasttrap_provs.fth_mask)
1800Sstevel@tonic-gate
181532Sahl #define FASTTRAP_PROCS_INDEX(pid) ((pid) & fasttrap_procs.fth_mask)
182532Sahl
1830Sstevel@tonic-gate static int
fasttrap_highbit(ulong_t i)1840Sstevel@tonic-gate fasttrap_highbit(ulong_t i)
1850Sstevel@tonic-gate {
1860Sstevel@tonic-gate int h = 1;
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate if (i == 0)
1890Sstevel@tonic-gate return (0);
1900Sstevel@tonic-gate #ifdef _LP64
1910Sstevel@tonic-gate if (i & 0xffffffff00000000ul) {
1920Sstevel@tonic-gate h += 32; i >>= 32;
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate #endif
1950Sstevel@tonic-gate if (i & 0xffff0000) {
1960Sstevel@tonic-gate h += 16; i >>= 16;
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate if (i & 0xff00) {
1990Sstevel@tonic-gate h += 8; i >>= 8;
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate if (i & 0xf0) {
2020Sstevel@tonic-gate h += 4; i >>= 4;
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate if (i & 0xc) {
2050Sstevel@tonic-gate h += 2; i >>= 2;
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate if (i & 0x2) {
2080Sstevel@tonic-gate h += 1;
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate return (h);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate static uint_t
fasttrap_hash_str(const char * p)2140Sstevel@tonic-gate fasttrap_hash_str(const char *p)
2150Sstevel@tonic-gate {
2160Sstevel@tonic-gate unsigned int g;
2170Sstevel@tonic-gate uint_t hval = 0;
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate while (*p) {
2200Sstevel@tonic-gate hval = (hval << 4) + *p++;
2210Sstevel@tonic-gate if ((g = (hval & 0xf0000000)) != 0)
2220Sstevel@tonic-gate hval ^= g >> 24;
2230Sstevel@tonic-gate hval &= ~g;
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate return (hval);
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate
2280Sstevel@tonic-gate void
fasttrap_sigtrap(proc_t * p,kthread_t * t,uintptr_t pc)2290Sstevel@tonic-gate fasttrap_sigtrap(proc_t *p, kthread_t *t, uintptr_t pc)
2300Sstevel@tonic-gate {
2310Sstevel@tonic-gate sigqueue_t *sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate sqp->sq_info.si_signo = SIGTRAP;
2340Sstevel@tonic-gate sqp->sq_info.si_code = TRAP_DTRACE;
2350Sstevel@tonic-gate sqp->sq_info.si_addr = (caddr_t)pc;
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate mutex_enter(&p->p_lock);
2380Sstevel@tonic-gate sigaddqa(p, t, sqp);
2390Sstevel@tonic-gate mutex_exit(&p->p_lock);
2400Sstevel@tonic-gate
2410Sstevel@tonic-gate if (t != NULL)
2420Sstevel@tonic-gate aston(t);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate /*
2460Sstevel@tonic-gate * This function ensures that no threads are actively using the memory
2470Sstevel@tonic-gate * associated with probes that were formerly live.
2480Sstevel@tonic-gate */
2490Sstevel@tonic-gate static void
fasttrap_mod_barrier(uint64_t gen)2500Sstevel@tonic-gate fasttrap_mod_barrier(uint64_t gen)
2510Sstevel@tonic-gate {
2520Sstevel@tonic-gate int i;
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate if (gen < fasttrap_mod_gen)
2550Sstevel@tonic-gate return;
2560Sstevel@tonic-gate
2570Sstevel@tonic-gate fasttrap_mod_gen++;
2580Sstevel@tonic-gate
2590Sstevel@tonic-gate for (i = 0; i < NCPU; i++) {
2600Sstevel@tonic-gate mutex_enter(&cpu_core[i].cpuc_pid_lock);
2610Sstevel@tonic-gate mutex_exit(&cpu_core[i].cpuc_pid_lock);
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate
2650Sstevel@tonic-gate /*
2660Sstevel@tonic-gate * This is the timeout's callback for cleaning up the providers and their
2670Sstevel@tonic-gate * probes.
2680Sstevel@tonic-gate */
2690Sstevel@tonic-gate /*ARGSUSED*/
2700Sstevel@tonic-gate static void
fasttrap_pid_cleanup_cb(void * data)2710Sstevel@tonic-gate fasttrap_pid_cleanup_cb(void *data)
2720Sstevel@tonic-gate {
2730Sstevel@tonic-gate fasttrap_provider_t **fpp, *fp;
2740Sstevel@tonic-gate fasttrap_bucket_t *bucket;
2750Sstevel@tonic-gate dtrace_provider_id_t provid;
2760Sstevel@tonic-gate int i, later;
2770Sstevel@tonic-gate
2780Sstevel@tonic-gate static volatile int in = 0;
2790Sstevel@tonic-gate ASSERT(in == 0);
2800Sstevel@tonic-gate in = 1;
2810Sstevel@tonic-gate
2820Sstevel@tonic-gate mutex_enter(&fasttrap_cleanup_mtx);
2830Sstevel@tonic-gate while (fasttrap_cleanup_work) {
2840Sstevel@tonic-gate fasttrap_cleanup_work = 0;
2850Sstevel@tonic-gate mutex_exit(&fasttrap_cleanup_mtx);
2860Sstevel@tonic-gate
2870Sstevel@tonic-gate later = 0;
2880Sstevel@tonic-gate
2890Sstevel@tonic-gate /*
2900Sstevel@tonic-gate * Iterate over all the providers trying to remove the marked
291532Sahl * ones. If a provider is marked but not retired, we just
2920Sstevel@tonic-gate * have to take a crack at removing it -- it's no big deal if
2930Sstevel@tonic-gate * we can't.
2940Sstevel@tonic-gate */
2950Sstevel@tonic-gate for (i = 0; i < fasttrap_provs.fth_nent; i++) {
2960Sstevel@tonic-gate bucket = &fasttrap_provs.fth_table[i];
2970Sstevel@tonic-gate mutex_enter(&bucket->ftb_mtx);
2980Sstevel@tonic-gate fpp = (fasttrap_provider_t **)&bucket->ftb_data;
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate while ((fp = *fpp) != NULL) {
3010Sstevel@tonic-gate if (!fp->ftp_marked) {
3020Sstevel@tonic-gate fpp = &fp->ftp_next;
3030Sstevel@tonic-gate continue;
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate
3060Sstevel@tonic-gate mutex_enter(&fp->ftp_mtx);
3070Sstevel@tonic-gate
3080Sstevel@tonic-gate /*
3092179Sahl * If this provider has consumers actively
3102179Sahl * creating probes (ftp_ccount) or is a USDT
3112179Sahl * provider (ftp_mcount), we can't unregister
3122179Sahl * or even condense.
3130Sstevel@tonic-gate */
3141880Sahl if (fp->ftp_ccount != 0 ||
3151880Sahl fp->ftp_mcount != 0) {
3160Sstevel@tonic-gate mutex_exit(&fp->ftp_mtx);
3170Sstevel@tonic-gate fp->ftp_marked = 0;
3180Sstevel@tonic-gate continue;
3190Sstevel@tonic-gate }
3200Sstevel@tonic-gate
321532Sahl if (!fp->ftp_retired || fp->ftp_rcount != 0)
3220Sstevel@tonic-gate fp->ftp_marked = 0;
3230Sstevel@tonic-gate
3240Sstevel@tonic-gate mutex_exit(&fp->ftp_mtx);
3250Sstevel@tonic-gate
3260Sstevel@tonic-gate /*
3270Sstevel@tonic-gate * If we successfully unregister this
3280Sstevel@tonic-gate * provider we can remove it from the hash
3290Sstevel@tonic-gate * chain and free the memory. If our attempt
330532Sahl * to unregister fails and this is a retired
3310Sstevel@tonic-gate * provider, increment our flag to try again
3320Sstevel@tonic-gate * pretty soon. If we've consumed more than
3330Sstevel@tonic-gate * half of our total permitted number of
3340Sstevel@tonic-gate * probes call dtrace_condense() to try to
3350Sstevel@tonic-gate * clean out the unenabled probes.
3360Sstevel@tonic-gate */
3370Sstevel@tonic-gate provid = fp->ftp_provid;
3380Sstevel@tonic-gate if (dtrace_unregister(provid) != 0) {
3390Sstevel@tonic-gate if (fasttrap_total > fasttrap_max / 2)
3400Sstevel@tonic-gate (void) dtrace_condense(provid);
3410Sstevel@tonic-gate later += fp->ftp_marked;
3420Sstevel@tonic-gate fpp = &fp->ftp_next;
3430Sstevel@tonic-gate } else {
3440Sstevel@tonic-gate *fpp = fp->ftp_next;
3450Sstevel@tonic-gate fasttrap_provider_free(fp);
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx);
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate
3510Sstevel@tonic-gate mutex_enter(&fasttrap_cleanup_mtx);
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate
3540Sstevel@tonic-gate ASSERT(fasttrap_timeout != 0);
3550Sstevel@tonic-gate
3560Sstevel@tonic-gate /*
357532Sahl * If we were unable to remove a retired provider, try again after
3580Sstevel@tonic-gate * a second. This situation can occur in certain circumstances where
3590Sstevel@tonic-gate * providers cannot be unregistered even though they have no probes
3600Sstevel@tonic-gate * enabled because of an execution of dtrace -l or something similar.
3610Sstevel@tonic-gate * If the timeout has been disabled (set to 1 because we're trying
3620Sstevel@tonic-gate * to detach), we set fasttrap_cleanup_work to ensure that we'll
3630Sstevel@tonic-gate * get a chance to do that work if and when the timeout is reenabled
3640Sstevel@tonic-gate * (if detach fails).
3650Sstevel@tonic-gate */
3660Sstevel@tonic-gate if (later > 0 && fasttrap_timeout != (timeout_id_t)1)
3670Sstevel@tonic-gate fasttrap_timeout = timeout(&fasttrap_pid_cleanup_cb, NULL, hz);
3680Sstevel@tonic-gate else if (later > 0)
3690Sstevel@tonic-gate fasttrap_cleanup_work = 1;
3700Sstevel@tonic-gate else
3710Sstevel@tonic-gate fasttrap_timeout = 0;
3720Sstevel@tonic-gate
3730Sstevel@tonic-gate mutex_exit(&fasttrap_cleanup_mtx);
3740Sstevel@tonic-gate in = 0;
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate
3770Sstevel@tonic-gate /*
3780Sstevel@tonic-gate * Activates the asynchronous cleanup mechanism.
3790Sstevel@tonic-gate */
3800Sstevel@tonic-gate static void
fasttrap_pid_cleanup(void)3810Sstevel@tonic-gate fasttrap_pid_cleanup(void)
3820Sstevel@tonic-gate {
3830Sstevel@tonic-gate mutex_enter(&fasttrap_cleanup_mtx);
3840Sstevel@tonic-gate fasttrap_cleanup_work = 1;
3850Sstevel@tonic-gate if (fasttrap_timeout == 0)
3860Sstevel@tonic-gate fasttrap_timeout = timeout(&fasttrap_pid_cleanup_cb, NULL, 1);
3870Sstevel@tonic-gate mutex_exit(&fasttrap_cleanup_mtx);
3880Sstevel@tonic-gate }
3890Sstevel@tonic-gate
3900Sstevel@tonic-gate /*
3910Sstevel@tonic-gate * This is called from cfork() via dtrace_fasttrap_fork(). The child
3926390Sahl * process's address space is (roughly) a copy of the parent process's so
3930Sstevel@tonic-gate * we have to remove all the instrumentation we had previously enabled in the
3940Sstevel@tonic-gate * parent.
3950Sstevel@tonic-gate */
3960Sstevel@tonic-gate static void
fasttrap_fork(proc_t * p,proc_t * cp)3970Sstevel@tonic-gate fasttrap_fork(proc_t *p, proc_t *cp)
3980Sstevel@tonic-gate {
3990Sstevel@tonic-gate pid_t ppid = p->p_pid;
4000Sstevel@tonic-gate int i;
4010Sstevel@tonic-gate
4020Sstevel@tonic-gate ASSERT(curproc == p);
4030Sstevel@tonic-gate ASSERT(p->p_proc_flag & P_PR_LOCK);
4040Sstevel@tonic-gate ASSERT(p->p_dtrace_count > 0);
4050Sstevel@tonic-gate ASSERT(cp->p_dtrace_count == 0);
4060Sstevel@tonic-gate
4070Sstevel@tonic-gate /*
4080Sstevel@tonic-gate * This would be simpler and faster if we maintained per-process
4090Sstevel@tonic-gate * hash tables of enabled tracepoints. It could, however, potentially
4100Sstevel@tonic-gate * slow down execution of a tracepoint since we'd need to go
4110Sstevel@tonic-gate * through two levels of indirection. In the future, we should
4120Sstevel@tonic-gate * consider either maintaining per-process ancillary lists of
4130Sstevel@tonic-gate * enabled tracepoints or hanging a pointer to a per-process hash
4140Sstevel@tonic-gate * table of enabled tracepoints off the proc structure.
4150Sstevel@tonic-gate */
4160Sstevel@tonic-gate
4170Sstevel@tonic-gate /*
4180Sstevel@tonic-gate * We don't have to worry about the child process disappearing
4190Sstevel@tonic-gate * because we're in fork().
4200Sstevel@tonic-gate */
4210Sstevel@tonic-gate mutex_enter(&cp->p_lock);
4220Sstevel@tonic-gate sprlock_proc(cp);
4230Sstevel@tonic-gate mutex_exit(&cp->p_lock);
4240Sstevel@tonic-gate
4250Sstevel@tonic-gate /*
4260Sstevel@tonic-gate * Iterate over every tracepoint looking for ones that belong to the
4270Sstevel@tonic-gate * parent process, and remove each from the child process.
4280Sstevel@tonic-gate */
4290Sstevel@tonic-gate for (i = 0; i < fasttrap_tpoints.fth_nent; i++) {
4300Sstevel@tonic-gate fasttrap_tracepoint_t *tp;
4310Sstevel@tonic-gate fasttrap_bucket_t *bucket = &fasttrap_tpoints.fth_table[i];
4320Sstevel@tonic-gate
4330Sstevel@tonic-gate mutex_enter(&bucket->ftb_mtx);
4340Sstevel@tonic-gate for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
435532Sahl if (tp->ftt_pid == ppid &&
4364821Sahl tp->ftt_proc->ftpc_acount != 0) {
4370Sstevel@tonic-gate int ret = fasttrap_tracepoint_remove(cp, tp);
4380Sstevel@tonic-gate ASSERT(ret == 0);
4396390Sahl
4406390Sahl /*
4416390Sahl * The count of active providers can only be
4426390Sahl * decremented (i.e. to zero) during exec,
4436390Sahl * exit, and removal of a meta provider so it
4446390Sahl * should be impossible to drop the count
4456390Sahl * mid-fork.
4466390Sahl */
4476390Sahl ASSERT(tp->ftt_proc->ftpc_acount != 0);
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx);
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate
4530Sstevel@tonic-gate mutex_enter(&cp->p_lock);
4540Sstevel@tonic-gate sprunlock(cp);
4550Sstevel@tonic-gate }
4560Sstevel@tonic-gate
4570Sstevel@tonic-gate /*
4580Sstevel@tonic-gate * This is called from proc_exit() or from exec_common() if p_dtrace_probes
4590Sstevel@tonic-gate * is set on the proc structure to indicate that there is a pid provider
4600Sstevel@tonic-gate * associated with this process.
4610Sstevel@tonic-gate */
4620Sstevel@tonic-gate static void
fasttrap_exec_exit(proc_t * p)4630Sstevel@tonic-gate fasttrap_exec_exit(proc_t *p)
4640Sstevel@tonic-gate {
4650Sstevel@tonic-gate ASSERT(p == curproc);
4660Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock));
4670Sstevel@tonic-gate
4680Sstevel@tonic-gate mutex_exit(&p->p_lock);
4690Sstevel@tonic-gate
4700Sstevel@tonic-gate /*
4710Sstevel@tonic-gate * We clean up the pid provider for this process here; user-land
4720Sstevel@tonic-gate * static probes are handled by the meta-provider remove entry point.
4730Sstevel@tonic-gate */
474935Sahl fasttrap_provider_retire(p->p_pid, FASTTRAP_PID_NAME, 0);
4750Sstevel@tonic-gate
4760Sstevel@tonic-gate mutex_enter(&p->p_lock);
4770Sstevel@tonic-gate }
4780Sstevel@tonic-gate
4790Sstevel@tonic-gate
4800Sstevel@tonic-gate /*ARGSUSED*/
4810Sstevel@tonic-gate static void
fasttrap_pid_provide(void * arg,const dtrace_probedesc_t * desc)4820Sstevel@tonic-gate fasttrap_pid_provide(void *arg, const dtrace_probedesc_t *desc)
4830Sstevel@tonic-gate {
4840Sstevel@tonic-gate /*
4850Sstevel@tonic-gate * There are no "default" pid probes.
4860Sstevel@tonic-gate */
4870Sstevel@tonic-gate }
4880Sstevel@tonic-gate
4890Sstevel@tonic-gate static int
fasttrap_tracepoint_enable(proc_t * p,fasttrap_probe_t * probe,uint_t index)4900Sstevel@tonic-gate fasttrap_tracepoint_enable(proc_t *p, fasttrap_probe_t *probe, uint_t index)
4910Sstevel@tonic-gate {
4920Sstevel@tonic-gate fasttrap_tracepoint_t *tp, *new_tp = NULL;
4930Sstevel@tonic-gate fasttrap_bucket_t *bucket;
4940Sstevel@tonic-gate fasttrap_id_t *id;
4950Sstevel@tonic-gate pid_t pid;
4960Sstevel@tonic-gate uintptr_t pc;
4970Sstevel@tonic-gate
4980Sstevel@tonic-gate ASSERT(index < probe->ftp_ntps);
4990Sstevel@tonic-gate
5000Sstevel@tonic-gate pid = probe->ftp_pid;
5010Sstevel@tonic-gate pc = probe->ftp_tps[index].fit_tp->ftt_pc;
5020Sstevel@tonic-gate id = &probe->ftp_tps[index].fit_id;
5030Sstevel@tonic-gate
5040Sstevel@tonic-gate ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid);
5050Sstevel@tonic-gate
5060Sstevel@tonic-gate ASSERT(!(p->p_flag & SVFORK));
5070Sstevel@tonic-gate
5080Sstevel@tonic-gate /*
5090Sstevel@tonic-gate * Before we make any modifications, make sure we've imposed a barrier
5100Sstevel@tonic-gate * on the generation in which this probe was last modified.
5110Sstevel@tonic-gate */
5120Sstevel@tonic-gate fasttrap_mod_barrier(probe->ftp_gen);
5130Sstevel@tonic-gate
5140Sstevel@tonic-gate bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
5150Sstevel@tonic-gate
5160Sstevel@tonic-gate /*
5170Sstevel@tonic-gate * If the tracepoint has already been enabled, just add our id to the
5180Sstevel@tonic-gate * list of interested probes. This may be our second time through
5190Sstevel@tonic-gate * this path in which case we'll have constructed the tracepoint we'd
5200Sstevel@tonic-gate * like to install. If we can't find a match, and have an allocated
5210Sstevel@tonic-gate * tracepoint ready to go, enable that one now.
5220Sstevel@tonic-gate *
5231710Sahl * A tracepoint whose process is defunct is also considered defunct.
5240Sstevel@tonic-gate */
5250Sstevel@tonic-gate again:
5260Sstevel@tonic-gate mutex_enter(&bucket->ftb_mtx);
5270Sstevel@tonic-gate for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
5286390Sahl /*
5296390Sahl * Note that it's safe to access the active count on the
5306390Sahl * associated proc structure because we know that at least one
5316390Sahl * provider (this one) will still be around throughout this
5326390Sahl * operation.
5336390Sahl */
5340Sstevel@tonic-gate if (tp->ftt_pid != pid || tp->ftt_pc != pc ||
5354821Sahl tp->ftt_proc->ftpc_acount == 0)
5360Sstevel@tonic-gate continue;
5370Sstevel@tonic-gate
5380Sstevel@tonic-gate /*
5390Sstevel@tonic-gate * Now that we've found a matching tracepoint, it would be
5400Sstevel@tonic-gate * a decent idea to confirm that the tracepoint is still
5410Sstevel@tonic-gate * enabled and the trap instruction hasn't been overwritten.
5420Sstevel@tonic-gate * Since this is a little hairy, we'll punt for now.
5430Sstevel@tonic-gate */
5440Sstevel@tonic-gate
5450Sstevel@tonic-gate /*
5460Sstevel@tonic-gate * This can't be the first interested probe. We don't have
5470Sstevel@tonic-gate * to worry about another thread being in the midst of
5480Sstevel@tonic-gate * deleting this tracepoint (which would be the only valid
5490Sstevel@tonic-gate * reason for a tracepoint to have no interested probes)
5500Sstevel@tonic-gate * since we're holding P_PR_LOCK for this process.
5510Sstevel@tonic-gate */
5520Sstevel@tonic-gate ASSERT(tp->ftt_ids != NULL || tp->ftt_retids != NULL);
5530Sstevel@tonic-gate
5541710Sahl switch (id->fti_ptype) {
5551710Sahl case DTFTP_ENTRY:
5561710Sahl case DTFTP_OFFSETS:
5571710Sahl case DTFTP_IS_ENABLED:
5581710Sahl id->fti_next = tp->ftt_ids;
5591710Sahl membar_producer();
5601710Sahl tp->ftt_ids = id;
5611710Sahl membar_producer();
5621710Sahl break;
5631710Sahl
5641710Sahl case DTFTP_RETURN:
5651710Sahl case DTFTP_POST_OFFSETS:
5660Sstevel@tonic-gate id->fti_next = tp->ftt_retids;
5670Sstevel@tonic-gate membar_producer();
5680Sstevel@tonic-gate tp->ftt_retids = id;
5690Sstevel@tonic-gate membar_producer();
5701710Sahl break;
5711710Sahl
5721710Sahl default:
5731710Sahl ASSERT(0);
5740Sstevel@tonic-gate }
5750Sstevel@tonic-gate
5760Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx);
5770Sstevel@tonic-gate
5780Sstevel@tonic-gate if (new_tp != NULL) {
5790Sstevel@tonic-gate new_tp->ftt_ids = NULL;
5800Sstevel@tonic-gate new_tp->ftt_retids = NULL;
5810Sstevel@tonic-gate }
5820Sstevel@tonic-gate
5830Sstevel@tonic-gate return (0);
5840Sstevel@tonic-gate }
5850Sstevel@tonic-gate
5860Sstevel@tonic-gate /*
5870Sstevel@tonic-gate * If we have a good tracepoint ready to go, install it now while
5880Sstevel@tonic-gate * we have the lock held and no one can screw with us.
5890Sstevel@tonic-gate */
5900Sstevel@tonic-gate if (new_tp != NULL) {
591315Sahl int rc = 0;
5920Sstevel@tonic-gate
5930Sstevel@tonic-gate new_tp->ftt_next = bucket->ftb_data;
5940Sstevel@tonic-gate membar_producer();
5950Sstevel@tonic-gate bucket->ftb_data = new_tp;
5960Sstevel@tonic-gate membar_producer();
5970Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx);
5980Sstevel@tonic-gate
5990Sstevel@tonic-gate /*
600315Sahl * Activate the tracepoint in the ISA-specific manner.
601315Sahl * If this fails, we need to report the failure, but
602315Sahl * indicate that this tracepoint must still be disabled
603315Sahl * by calling fasttrap_tracepoint_disable().
6040Sstevel@tonic-gate */
605315Sahl if (fasttrap_tracepoint_install(p, new_tp) != 0)
606315Sahl rc = FASTTRAP_ENABLE_PARTIAL;
607315Sahl
608315Sahl /*
609315Sahl * Increment the count of the number of tracepoints active in
610315Sahl * the victim process.
611315Sahl */
612315Sahl ASSERT(p->p_proc_flag & P_PR_LOCK);
613315Sahl p->p_dtrace_count++;
6140Sstevel@tonic-gate
6150Sstevel@tonic-gate return (rc);
6160Sstevel@tonic-gate }
6170Sstevel@tonic-gate
6180Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx);
6190Sstevel@tonic-gate
6200Sstevel@tonic-gate /*
6210Sstevel@tonic-gate * Initialize the tracepoint that's been preallocated with the probe.
6220Sstevel@tonic-gate */
6230Sstevel@tonic-gate new_tp = probe->ftp_tps[index].fit_tp;
6240Sstevel@tonic-gate
6250Sstevel@tonic-gate ASSERT(new_tp->ftt_pid == pid);
6260Sstevel@tonic-gate ASSERT(new_tp->ftt_pc == pc);
627532Sahl ASSERT(new_tp->ftt_proc == probe->ftp_prov->ftp_proc);
6280Sstevel@tonic-gate ASSERT(new_tp->ftt_ids == NULL);
6290Sstevel@tonic-gate ASSERT(new_tp->ftt_retids == NULL);
6300Sstevel@tonic-gate
6311710Sahl switch (id->fti_ptype) {
6321710Sahl case DTFTP_ENTRY:
6331710Sahl case DTFTP_OFFSETS:
6341710Sahl case DTFTP_IS_ENABLED:
6351710Sahl id->fti_next = NULL;
6361710Sahl new_tp->ftt_ids = id;
6371710Sahl break;
6381710Sahl
6391710Sahl case DTFTP_RETURN:
6401710Sahl case DTFTP_POST_OFFSETS:
6410Sstevel@tonic-gate id->fti_next = NULL;
6420Sstevel@tonic-gate new_tp->ftt_retids = id;
6431710Sahl break;
6441710Sahl
6451710Sahl default:
6461710Sahl ASSERT(0);
6470Sstevel@tonic-gate }
6480Sstevel@tonic-gate
6490Sstevel@tonic-gate /*
650315Sahl * If the ISA-dependent initialization goes to plan, go back to the
6510Sstevel@tonic-gate * beginning and try to install this freshly made tracepoint.
6520Sstevel@tonic-gate */
6531710Sahl if (fasttrap_tracepoint_init(p, new_tp, pc, id->fti_ptype) == 0)
6540Sstevel@tonic-gate goto again;
6550Sstevel@tonic-gate
6560Sstevel@tonic-gate new_tp->ftt_ids = NULL;
6570Sstevel@tonic-gate new_tp->ftt_retids = NULL;
6580Sstevel@tonic-gate
659315Sahl return (FASTTRAP_ENABLE_FAIL);
6600Sstevel@tonic-gate }
6610Sstevel@tonic-gate
6620Sstevel@tonic-gate static void
fasttrap_tracepoint_disable(proc_t * p,fasttrap_probe_t * probe,uint_t index)6630Sstevel@tonic-gate fasttrap_tracepoint_disable(proc_t *p, fasttrap_probe_t *probe, uint_t index)
6640Sstevel@tonic-gate {
6650Sstevel@tonic-gate fasttrap_bucket_t *bucket;
6660Sstevel@tonic-gate fasttrap_provider_t *provider = probe->ftp_prov;
6670Sstevel@tonic-gate fasttrap_tracepoint_t **pp, *tp;
6680Sstevel@tonic-gate fasttrap_id_t *id, **idp;
6690Sstevel@tonic-gate pid_t pid;
6700Sstevel@tonic-gate uintptr_t pc;
6710Sstevel@tonic-gate
6720Sstevel@tonic-gate ASSERT(index < probe->ftp_ntps);
6730Sstevel@tonic-gate
6740Sstevel@tonic-gate pid = probe->ftp_pid;
6750Sstevel@tonic-gate pc = probe->ftp_tps[index].fit_tp->ftt_pc;
6761710Sahl id = &probe->ftp_tps[index].fit_id;
6770Sstevel@tonic-gate
6780Sstevel@tonic-gate ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid);
6790Sstevel@tonic-gate
6800Sstevel@tonic-gate /*
6810Sstevel@tonic-gate * Find the tracepoint and make sure that our id is one of the
6820Sstevel@tonic-gate * ones registered with it.
6830Sstevel@tonic-gate */
6840Sstevel@tonic-gate bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
6850Sstevel@tonic-gate mutex_enter(&bucket->ftb_mtx);
6860Sstevel@tonic-gate for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
6870Sstevel@tonic-gate if (tp->ftt_pid == pid && tp->ftt_pc == pc &&
688532Sahl tp->ftt_proc == provider->ftp_proc)
6890Sstevel@tonic-gate break;
6900Sstevel@tonic-gate }
6910Sstevel@tonic-gate
6920Sstevel@tonic-gate /*
6930Sstevel@tonic-gate * If we somehow lost this tracepoint, we're in a world of hurt.
6940Sstevel@tonic-gate */
6950Sstevel@tonic-gate ASSERT(tp != NULL);
6960Sstevel@tonic-gate
6971710Sahl switch (id->fti_ptype) {
6981710Sahl case DTFTP_ENTRY:
6991710Sahl case DTFTP_OFFSETS:
7001710Sahl case DTFTP_IS_ENABLED:
7011710Sahl ASSERT(tp->ftt_ids != NULL);
7021710Sahl idp = &tp->ftt_ids;
7031710Sahl break;
7041710Sahl
7051710Sahl case DTFTP_RETURN:
7061710Sahl case DTFTP_POST_OFFSETS:
7070Sstevel@tonic-gate ASSERT(tp->ftt_retids != NULL);
7080Sstevel@tonic-gate idp = &tp->ftt_retids;
7091710Sahl break;
7101710Sahl
7111710Sahl default:
7121710Sahl ASSERT(0);
7130Sstevel@tonic-gate }
7140Sstevel@tonic-gate
7150Sstevel@tonic-gate while ((*idp)->fti_probe != probe) {
7160Sstevel@tonic-gate idp = &(*idp)->fti_next;
7170Sstevel@tonic-gate ASSERT(*idp != NULL);
7180Sstevel@tonic-gate }
7190Sstevel@tonic-gate
7200Sstevel@tonic-gate id = *idp;
7210Sstevel@tonic-gate *idp = id->fti_next;
7220Sstevel@tonic-gate membar_producer();
7230Sstevel@tonic-gate
7240Sstevel@tonic-gate ASSERT(id->fti_probe == probe);
7250Sstevel@tonic-gate
7260Sstevel@tonic-gate /*
7270Sstevel@tonic-gate * If there are other registered enablings of this tracepoint, we're
7280Sstevel@tonic-gate * all done, but if this was the last probe assocated with this
7290Sstevel@tonic-gate * this tracepoint, we need to remove and free it.
7300Sstevel@tonic-gate */
7310Sstevel@tonic-gate if (tp->ftt_ids != NULL || tp->ftt_retids != NULL) {
7320Sstevel@tonic-gate
7330Sstevel@tonic-gate /*
7340Sstevel@tonic-gate * If the current probe's tracepoint is in use, swap it
7350Sstevel@tonic-gate * for an unused tracepoint.
7360Sstevel@tonic-gate */
7370Sstevel@tonic-gate if (tp == probe->ftp_tps[index].fit_tp) {
7380Sstevel@tonic-gate fasttrap_probe_t *tmp_probe;
7390Sstevel@tonic-gate fasttrap_tracepoint_t **tmp_tp;
7400Sstevel@tonic-gate uint_t tmp_index;
7410Sstevel@tonic-gate
7420Sstevel@tonic-gate if (tp->ftt_ids != NULL) {
7430Sstevel@tonic-gate tmp_probe = tp->ftt_ids->fti_probe;
7443944Sahl /* LINTED - alignment */
7450Sstevel@tonic-gate tmp_index = FASTTRAP_ID_INDEX(tp->ftt_ids);
7460Sstevel@tonic-gate tmp_tp = &tmp_probe->ftp_tps[tmp_index].fit_tp;
7470Sstevel@tonic-gate } else {
7480Sstevel@tonic-gate tmp_probe = tp->ftt_retids->fti_probe;
7493944Sahl /* LINTED - alignment */
7500Sstevel@tonic-gate tmp_index = FASTTRAP_ID_INDEX(tp->ftt_retids);
7510Sstevel@tonic-gate tmp_tp = &tmp_probe->ftp_tps[tmp_index].fit_tp;
7520Sstevel@tonic-gate }
7530Sstevel@tonic-gate
7540Sstevel@tonic-gate ASSERT(*tmp_tp != NULL);
7550Sstevel@tonic-gate ASSERT(*tmp_tp != probe->ftp_tps[index].fit_tp);
7560Sstevel@tonic-gate ASSERT((*tmp_tp)->ftt_ids == NULL);
7570Sstevel@tonic-gate ASSERT((*tmp_tp)->ftt_retids == NULL);
7580Sstevel@tonic-gate
7590Sstevel@tonic-gate probe->ftp_tps[index].fit_tp = *tmp_tp;
7600Sstevel@tonic-gate *tmp_tp = tp;
7610Sstevel@tonic-gate }
7620Sstevel@tonic-gate
7630Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx);
7640Sstevel@tonic-gate
7650Sstevel@tonic-gate /*
7660Sstevel@tonic-gate * Tag the modified probe with the generation in which it was
7670Sstevel@tonic-gate * changed.
7680Sstevel@tonic-gate */
7690Sstevel@tonic-gate probe->ftp_gen = fasttrap_mod_gen;
7700Sstevel@tonic-gate return;
7710Sstevel@tonic-gate }
7720Sstevel@tonic-gate
7730Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx);
7740Sstevel@tonic-gate
7750Sstevel@tonic-gate /*
7760Sstevel@tonic-gate * We can't safely remove the tracepoint from the set of active
7770Sstevel@tonic-gate * tracepoints until we've actually removed the fasttrap instruction
7780Sstevel@tonic-gate * from the process's text. We can, however, operate on this
7790Sstevel@tonic-gate * tracepoint secure in the knowledge that no other thread is going to
7800Sstevel@tonic-gate * be looking at it since we hold P_PR_LOCK on the process if it's
7810Sstevel@tonic-gate * live or we hold the provider lock on the process if it's dead and
7820Sstevel@tonic-gate * gone.
7830Sstevel@tonic-gate */
7840Sstevel@tonic-gate
7850Sstevel@tonic-gate /*
7860Sstevel@tonic-gate * We only need to remove the actual instruction if we're looking
7870Sstevel@tonic-gate * at an existing process
7880Sstevel@tonic-gate */
7890Sstevel@tonic-gate if (p != NULL) {
7900Sstevel@tonic-gate /*
7910Sstevel@tonic-gate * If we fail to restore the instruction we need to kill
7920Sstevel@tonic-gate * this process since it's in a completely unrecoverable
7930Sstevel@tonic-gate * state.
7940Sstevel@tonic-gate */
7950Sstevel@tonic-gate if (fasttrap_tracepoint_remove(p, tp) != 0)
7960Sstevel@tonic-gate fasttrap_sigtrap(p, NULL, pc);
7970Sstevel@tonic-gate
7980Sstevel@tonic-gate /*
7990Sstevel@tonic-gate * Decrement the count of the number of tracepoints active
8000Sstevel@tonic-gate * in the victim process.
8010Sstevel@tonic-gate */
8020Sstevel@tonic-gate ASSERT(p->p_proc_flag & P_PR_LOCK);
8030Sstevel@tonic-gate p->p_dtrace_count--;
8040Sstevel@tonic-gate }
8050Sstevel@tonic-gate
8060Sstevel@tonic-gate /*
8070Sstevel@tonic-gate * Remove the probe from the hash table of active tracepoints.
8080Sstevel@tonic-gate */
8090Sstevel@tonic-gate mutex_enter(&bucket->ftb_mtx);
8100Sstevel@tonic-gate pp = (fasttrap_tracepoint_t **)&bucket->ftb_data;
8110Sstevel@tonic-gate ASSERT(*pp != NULL);
8120Sstevel@tonic-gate while (*pp != tp) {
8130Sstevel@tonic-gate pp = &(*pp)->ftt_next;
8140Sstevel@tonic-gate ASSERT(*pp != NULL);
8150Sstevel@tonic-gate }
8160Sstevel@tonic-gate
8170Sstevel@tonic-gate *pp = tp->ftt_next;
8180Sstevel@tonic-gate membar_producer();
8190Sstevel@tonic-gate
8200Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx);
8210Sstevel@tonic-gate
8220Sstevel@tonic-gate /*
8230Sstevel@tonic-gate * Tag the modified probe with the generation in which it was changed.
8240Sstevel@tonic-gate */
8250Sstevel@tonic-gate probe->ftp_gen = fasttrap_mod_gen;
8260Sstevel@tonic-gate }
8270Sstevel@tonic-gate
8280Sstevel@tonic-gate static void
fasttrap_enable_callbacks(void)8292179Sahl fasttrap_enable_callbacks(void)
8300Sstevel@tonic-gate {
8310Sstevel@tonic-gate /*
8320Sstevel@tonic-gate * We don't have to play the rw lock game here because we're
8330Sstevel@tonic-gate * providing something rather than taking something away --
8340Sstevel@tonic-gate * we can be sure that no threads have tried to follow this
8350Sstevel@tonic-gate * function pointer yet.
8360Sstevel@tonic-gate */
8370Sstevel@tonic-gate mutex_enter(&fasttrap_count_mtx);
8382179Sahl if (fasttrap_pid_count == 0) {
8392179Sahl ASSERT(dtrace_pid_probe_ptr == NULL);
8402179Sahl ASSERT(dtrace_return_probe_ptr == NULL);
8412179Sahl dtrace_pid_probe_ptr = &fasttrap_pid_probe;
8422179Sahl dtrace_return_probe_ptr = &fasttrap_return_probe;
8430Sstevel@tonic-gate }
8442179Sahl ASSERT(dtrace_pid_probe_ptr == &fasttrap_pid_probe);
8452179Sahl ASSERT(dtrace_return_probe_ptr == &fasttrap_return_probe);
8462179Sahl fasttrap_pid_count++;
8470Sstevel@tonic-gate mutex_exit(&fasttrap_count_mtx);
8480Sstevel@tonic-gate }
8490Sstevel@tonic-gate
8500Sstevel@tonic-gate static void
fasttrap_disable_callbacks(void)8512179Sahl fasttrap_disable_callbacks(void)
8520Sstevel@tonic-gate {
8530Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
8540Sstevel@tonic-gate
8550Sstevel@tonic-gate mutex_enter(&fasttrap_count_mtx);
8562179Sahl ASSERT(fasttrap_pid_count > 0);
8572179Sahl fasttrap_pid_count--;
8582179Sahl if (fasttrap_pid_count == 0) {
8590Sstevel@tonic-gate cpu_t *cur, *cpu = CPU;
8600Sstevel@tonic-gate
8610Sstevel@tonic-gate for (cur = cpu->cpu_next_onln; cur != cpu;
8624568Sahl cur = cur->cpu_next_onln) {
8630Sstevel@tonic-gate rw_enter(&cur->cpu_ft_lock, RW_WRITER);
8640Sstevel@tonic-gate }
8650Sstevel@tonic-gate
8662179Sahl dtrace_pid_probe_ptr = NULL;
8672179Sahl dtrace_return_probe_ptr = NULL;
8680Sstevel@tonic-gate
8690Sstevel@tonic-gate for (cur = cpu->cpu_next_onln; cur != cpu;
8704568Sahl cur = cur->cpu_next_onln) {
8710Sstevel@tonic-gate rw_exit(&cur->cpu_ft_lock);
8720Sstevel@tonic-gate }
8730Sstevel@tonic-gate }
8740Sstevel@tonic-gate mutex_exit(&fasttrap_count_mtx);
8750Sstevel@tonic-gate }
8760Sstevel@tonic-gate
8770Sstevel@tonic-gate /*ARGSUSED*/
8788803SJonathan.Haslam@Sun.COM static int
fasttrap_pid_enable(void * arg,dtrace_id_t id,void * parg)8790Sstevel@tonic-gate fasttrap_pid_enable(void *arg, dtrace_id_t id, void *parg)
8800Sstevel@tonic-gate {
8810Sstevel@tonic-gate fasttrap_probe_t *probe = parg;
8820Sstevel@tonic-gate proc_t *p;
883315Sahl int i, rc;
8840Sstevel@tonic-gate
8850Sstevel@tonic-gate ASSERT(probe != NULL);
8860Sstevel@tonic-gate ASSERT(!probe->ftp_enabled);
8870Sstevel@tonic-gate ASSERT(id == probe->ftp_id);
8880Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
8890Sstevel@tonic-gate
8900Sstevel@tonic-gate /*
8910Sstevel@tonic-gate * Increment the count of enabled probes on this probe's provider;
8920Sstevel@tonic-gate * the provider can't go away while the probe still exists. We
8930Sstevel@tonic-gate * must increment this even if we aren't able to properly enable
8940Sstevel@tonic-gate * this probe.
8950Sstevel@tonic-gate */
8960Sstevel@tonic-gate mutex_enter(&probe->ftp_prov->ftp_mtx);
8970Sstevel@tonic-gate probe->ftp_prov->ftp_rcount++;
8980Sstevel@tonic-gate mutex_exit(&probe->ftp_prov->ftp_mtx);
8990Sstevel@tonic-gate
9000Sstevel@tonic-gate /*
9011880Sahl * If this probe's provider is retired (meaning it was valid in a
9021880Sahl * previously exec'ed incarnation of this address space), bail out. The
9031880Sahl * provider can't go away while we're in this code path.
9041880Sahl */
9051880Sahl if (probe->ftp_prov->ftp_retired)
9068803SJonathan.Haslam@Sun.COM return (0);
9071880Sahl
9081880Sahl /*
9091880Sahl * If we can't find the process, it may be that we're in the context of
9101880Sahl * a fork in which the traced process is being born and we're copying
9111880Sahl * USDT probes. Otherwise, the process is gone so bail.
9120Sstevel@tonic-gate */
9131880Sahl if ((p = sprlock(probe->ftp_pid)) == NULL) {
9141880Sahl if ((curproc->p_flag & SFORKING) == 0)
9158803SJonathan.Haslam@Sun.COM return (0);
9161880Sahl
9171880Sahl mutex_enter(&pidlock);
9181880Sahl p = prfind(probe->ftp_pid);
9191880Sahl
9201880Sahl /*
9211880Sahl * Confirm that curproc is indeed forking the process in which
9221880Sahl * we're trying to enable probes.
9231880Sahl */
9241880Sahl ASSERT(p != NULL);
9251880Sahl ASSERT(p->p_parent == curproc);
9261880Sahl ASSERT(p->p_stat == SIDL);
9271880Sahl
9281880Sahl mutex_enter(&p->p_lock);
9291880Sahl mutex_exit(&pidlock);
9301880Sahl
9311880Sahl sprlock_proc(p);
9321880Sahl }
9330Sstevel@tonic-gate
9340Sstevel@tonic-gate ASSERT(!(p->p_flag & SVFORK));
9350Sstevel@tonic-gate mutex_exit(&p->p_lock);
9360Sstevel@tonic-gate
9370Sstevel@tonic-gate /*
9382179Sahl * We have to enable the trap entry point before any user threads have
9390Sstevel@tonic-gate * the chance to execute the trap instruction we're about to place
9400Sstevel@tonic-gate * in their process's text.
9410Sstevel@tonic-gate */
9422179Sahl fasttrap_enable_callbacks();
9430Sstevel@tonic-gate
9440Sstevel@tonic-gate /*
9450Sstevel@tonic-gate * Enable all the tracepoints and add this probe's id to each
9460Sstevel@tonic-gate * tracepoint's list of active probes.
9470Sstevel@tonic-gate */
9480Sstevel@tonic-gate for (i = 0; i < probe->ftp_ntps; i++) {
949315Sahl if ((rc = fasttrap_tracepoint_enable(p, probe, i)) != 0) {
950315Sahl /*
951315Sahl * If enabling the tracepoint failed completely,
952315Sahl * we don't have to disable it; if the failure
953315Sahl * was only partial we must disable it.
954315Sahl */
955315Sahl if (rc == FASTTRAP_ENABLE_FAIL)
956315Sahl i--;
957315Sahl else
958315Sahl ASSERT(rc == FASTTRAP_ENABLE_PARTIAL);
959315Sahl
9600Sstevel@tonic-gate /*
9610Sstevel@tonic-gate * Back up and pull out all the tracepoints we've
9620Sstevel@tonic-gate * created so far for this probe.
9630Sstevel@tonic-gate */
964665Sahl while (i >= 0) {
9650Sstevel@tonic-gate fasttrap_tracepoint_disable(p, probe, i);
966665Sahl i--;
9670Sstevel@tonic-gate }
9680Sstevel@tonic-gate
9690Sstevel@tonic-gate mutex_enter(&p->p_lock);
9700Sstevel@tonic-gate sprunlock(p);
9710Sstevel@tonic-gate
9720Sstevel@tonic-gate /*
9730Sstevel@tonic-gate * Since we're not actually enabling this probe,
9740Sstevel@tonic-gate * drop our reference on the trap table entry.
9750Sstevel@tonic-gate */
9762179Sahl fasttrap_disable_callbacks();
9778803SJonathan.Haslam@Sun.COM return (0);
9780Sstevel@tonic-gate }
9790Sstevel@tonic-gate }
9800Sstevel@tonic-gate
9810Sstevel@tonic-gate mutex_enter(&p->p_lock);
9820Sstevel@tonic-gate sprunlock(p);
9830Sstevel@tonic-gate
9840Sstevel@tonic-gate probe->ftp_enabled = 1;
9858803SJonathan.Haslam@Sun.COM return (0);
9860Sstevel@tonic-gate }
9870Sstevel@tonic-gate
9880Sstevel@tonic-gate /*ARGSUSED*/
9890Sstevel@tonic-gate static void
fasttrap_pid_disable(void * arg,dtrace_id_t id,void * parg)9900Sstevel@tonic-gate fasttrap_pid_disable(void *arg, dtrace_id_t id, void *parg)
9910Sstevel@tonic-gate {
9920Sstevel@tonic-gate fasttrap_probe_t *probe = parg;
9930Sstevel@tonic-gate fasttrap_provider_t *provider = probe->ftp_prov;
9940Sstevel@tonic-gate proc_t *p;
9950Sstevel@tonic-gate int i, whack = 0;
9960Sstevel@tonic-gate
9970Sstevel@tonic-gate ASSERT(id == probe->ftp_id);
9980Sstevel@tonic-gate
9990Sstevel@tonic-gate /*
10000Sstevel@tonic-gate * We won't be able to acquire a /proc-esque lock on the process
10010Sstevel@tonic-gate * iff the process is dead and gone. In this case, we rely on the
10020Sstevel@tonic-gate * provider lock as a point of mutual exclusion to prevent other
10030Sstevel@tonic-gate * DTrace consumers from disabling this probe.
10040Sstevel@tonic-gate */
10050Sstevel@tonic-gate if ((p = sprlock(probe->ftp_pid)) != NULL) {
10060Sstevel@tonic-gate ASSERT(!(p->p_flag & SVFORK));
10070Sstevel@tonic-gate mutex_exit(&p->p_lock);
10080Sstevel@tonic-gate }
10090Sstevel@tonic-gate
10100Sstevel@tonic-gate mutex_enter(&provider->ftp_mtx);
10110Sstevel@tonic-gate
10120Sstevel@tonic-gate /*
10133944Sahl * Disable all the associated tracepoints (for fully enabled probes).
10140Sstevel@tonic-gate */
10153944Sahl if (probe->ftp_enabled) {
10163944Sahl for (i = 0; i < probe->ftp_ntps; i++) {
10173944Sahl fasttrap_tracepoint_disable(p, probe, i);
10183944Sahl }
10190Sstevel@tonic-gate }
10200Sstevel@tonic-gate
10210Sstevel@tonic-gate ASSERT(provider->ftp_rcount > 0);
10220Sstevel@tonic-gate provider->ftp_rcount--;
10230Sstevel@tonic-gate
10240Sstevel@tonic-gate if (p != NULL) {
10250Sstevel@tonic-gate /*
10260Sstevel@tonic-gate * Even though we may not be able to remove it entirely, we
1027532Sahl * mark this retired provider to get a chance to remove some
10280Sstevel@tonic-gate * of the associated probes.
10290Sstevel@tonic-gate */
1030532Sahl if (provider->ftp_retired && !provider->ftp_marked)
10310Sstevel@tonic-gate whack = provider->ftp_marked = 1;
10320Sstevel@tonic-gate mutex_exit(&provider->ftp_mtx);
10330Sstevel@tonic-gate
10340Sstevel@tonic-gate mutex_enter(&p->p_lock);
10350Sstevel@tonic-gate sprunlock(p);
10360Sstevel@tonic-gate } else {
10370Sstevel@tonic-gate /*
10380Sstevel@tonic-gate * If the process is dead, we're just waiting for the
10390Sstevel@tonic-gate * last probe to be disabled to be able to free it.
10400Sstevel@tonic-gate */
10410Sstevel@tonic-gate if (provider->ftp_rcount == 0 && !provider->ftp_marked)
10420Sstevel@tonic-gate whack = provider->ftp_marked = 1;
10430Sstevel@tonic-gate mutex_exit(&provider->ftp_mtx);
10440Sstevel@tonic-gate }
10450Sstevel@tonic-gate
10460Sstevel@tonic-gate if (whack)
10470Sstevel@tonic-gate fasttrap_pid_cleanup();
10480Sstevel@tonic-gate
10493944Sahl if (!probe->ftp_enabled)
10503944Sahl return;
10513944Sahl
10520Sstevel@tonic-gate probe->ftp_enabled = 0;
10530Sstevel@tonic-gate
10540Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
10552179Sahl fasttrap_disable_callbacks();
10560Sstevel@tonic-gate }
10570Sstevel@tonic-gate
10580Sstevel@tonic-gate /*ARGSUSED*/
10590Sstevel@tonic-gate static void
fasttrap_pid_getargdesc(void * arg,dtrace_id_t id,void * parg,dtrace_argdesc_t * desc)10600Sstevel@tonic-gate fasttrap_pid_getargdesc(void *arg, dtrace_id_t id, void *parg,
10610Sstevel@tonic-gate dtrace_argdesc_t *desc)
10620Sstevel@tonic-gate {
10630Sstevel@tonic-gate fasttrap_probe_t *probe = parg;
10640Sstevel@tonic-gate char *str;
10654568Sahl int i, ndx;
10660Sstevel@tonic-gate
10670Sstevel@tonic-gate desc->dtargd_native[0] = '\0';
10680Sstevel@tonic-gate desc->dtargd_xlate[0] = '\0';
10690Sstevel@tonic-gate
1070532Sahl if (probe->ftp_prov->ftp_retired != 0 ||
10710Sstevel@tonic-gate desc->dtargd_ndx >= probe->ftp_nargs) {
10720Sstevel@tonic-gate desc->dtargd_ndx = DTRACE_ARGNONE;
10730Sstevel@tonic-gate return;
10740Sstevel@tonic-gate }
10750Sstevel@tonic-gate
10764568Sahl ndx = (probe->ftp_argmap != NULL) ?
10774568Sahl probe->ftp_argmap[desc->dtargd_ndx] : desc->dtargd_ndx;
10780Sstevel@tonic-gate
10790Sstevel@tonic-gate str = probe->ftp_ntypes;
10804568Sahl for (i = 0; i < ndx; i++) {
10810Sstevel@tonic-gate str += strlen(str) + 1;
10820Sstevel@tonic-gate }
10830Sstevel@tonic-gate
10840Sstevel@tonic-gate ASSERT(strlen(str + 1) < sizeof (desc->dtargd_native));
10850Sstevel@tonic-gate (void) strcpy(desc->dtargd_native, str);
10860Sstevel@tonic-gate
10870Sstevel@tonic-gate if (probe->ftp_xtypes == NULL)
10880Sstevel@tonic-gate return;
10890Sstevel@tonic-gate
10900Sstevel@tonic-gate str = probe->ftp_xtypes;
10910Sstevel@tonic-gate for (i = 0; i < desc->dtargd_ndx; i++) {
10920Sstevel@tonic-gate str += strlen(str) + 1;
10930Sstevel@tonic-gate }
10940Sstevel@tonic-gate
10950Sstevel@tonic-gate ASSERT(strlen(str + 1) < sizeof (desc->dtargd_xlate));
10960Sstevel@tonic-gate (void) strcpy(desc->dtargd_xlate, str);
10970Sstevel@tonic-gate }
10980Sstevel@tonic-gate
10990Sstevel@tonic-gate /*ARGSUSED*/
11000Sstevel@tonic-gate static void
fasttrap_pid_destroy(void * arg,dtrace_id_t id,void * parg)11010Sstevel@tonic-gate fasttrap_pid_destroy(void *arg, dtrace_id_t id, void *parg)
11020Sstevel@tonic-gate {
11030Sstevel@tonic-gate fasttrap_probe_t *probe = parg;
11040Sstevel@tonic-gate int i;
11050Sstevel@tonic-gate size_t size;
11060Sstevel@tonic-gate
11070Sstevel@tonic-gate ASSERT(probe != NULL);
11080Sstevel@tonic-gate ASSERT(!probe->ftp_enabled);
11090Sstevel@tonic-gate ASSERT(fasttrap_total >= probe->ftp_ntps);
11100Sstevel@tonic-gate
11110Sstevel@tonic-gate atomic_add_32(&fasttrap_total, -probe->ftp_ntps);
11121880Sahl size = offsetof(fasttrap_probe_t, ftp_tps[probe->ftp_ntps]);
11130Sstevel@tonic-gate
11140Sstevel@tonic-gate if (probe->ftp_gen + 1 >= fasttrap_mod_gen)
11150Sstevel@tonic-gate fasttrap_mod_barrier(probe->ftp_gen);
11160Sstevel@tonic-gate
11170Sstevel@tonic-gate for (i = 0; i < probe->ftp_ntps; i++) {
11180Sstevel@tonic-gate kmem_free(probe->ftp_tps[i].fit_tp,
11190Sstevel@tonic-gate sizeof (fasttrap_tracepoint_t));
11200Sstevel@tonic-gate }
11210Sstevel@tonic-gate
11220Sstevel@tonic-gate kmem_free(probe, size);
11230Sstevel@tonic-gate }
11240Sstevel@tonic-gate
11250Sstevel@tonic-gate
11260Sstevel@tonic-gate static const dtrace_pattr_t pid_attr = {
11270Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
11280Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
11290Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
11300Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
11310Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
11320Sstevel@tonic-gate };
11330Sstevel@tonic-gate
11340Sstevel@tonic-gate static dtrace_pops_t pid_pops = {
11350Sstevel@tonic-gate fasttrap_pid_provide,
11360Sstevel@tonic-gate NULL,
11370Sstevel@tonic-gate fasttrap_pid_enable,
11380Sstevel@tonic-gate fasttrap_pid_disable,
11390Sstevel@tonic-gate NULL,
11400Sstevel@tonic-gate NULL,
11410Sstevel@tonic-gate fasttrap_pid_getargdesc,
11422179Sahl fasttrap_pid_getarg,
11430Sstevel@tonic-gate NULL,
11440Sstevel@tonic-gate fasttrap_pid_destroy
11450Sstevel@tonic-gate };
11460Sstevel@tonic-gate
11470Sstevel@tonic-gate static dtrace_pops_t usdt_pops = {
11480Sstevel@tonic-gate fasttrap_pid_provide,
11490Sstevel@tonic-gate NULL,
11500Sstevel@tonic-gate fasttrap_pid_enable,
11510Sstevel@tonic-gate fasttrap_pid_disable,
11520Sstevel@tonic-gate NULL,
11530Sstevel@tonic-gate NULL,
11540Sstevel@tonic-gate fasttrap_pid_getargdesc,
11550Sstevel@tonic-gate fasttrap_usdt_getarg,
11560Sstevel@tonic-gate NULL,
11570Sstevel@tonic-gate fasttrap_pid_destroy
11580Sstevel@tonic-gate };
11590Sstevel@tonic-gate
1160532Sahl static fasttrap_proc_t *
fasttrap_proc_lookup(pid_t pid)1161532Sahl fasttrap_proc_lookup(pid_t pid)
1162532Sahl {
1163532Sahl fasttrap_bucket_t *bucket;
1164532Sahl fasttrap_proc_t *fprc, *new_fprc;
1165532Sahl
1166532Sahl bucket = &fasttrap_procs.fth_table[FASTTRAP_PROCS_INDEX(pid)];
1167532Sahl mutex_enter(&bucket->ftb_mtx);
1168532Sahl
1169532Sahl for (fprc = bucket->ftb_data; fprc != NULL; fprc = fprc->ftpc_next) {
11704821Sahl if (fprc->ftpc_pid == pid && fprc->ftpc_acount != 0) {
1171532Sahl mutex_enter(&fprc->ftpc_mtx);
1172532Sahl mutex_exit(&bucket->ftb_mtx);
11734821Sahl fprc->ftpc_rcount++;
11744821Sahl atomic_add_64(&fprc->ftpc_acount, 1);
11756390Sahl ASSERT(fprc->ftpc_acount <= fprc->ftpc_rcount);
1176532Sahl mutex_exit(&fprc->ftpc_mtx);
1177532Sahl
1178532Sahl return (fprc);
1179532Sahl }
1180532Sahl }
1181532Sahl
1182532Sahl /*
1183532Sahl * Drop the bucket lock so we don't try to perform a sleeping
1184532Sahl * allocation under it.
1185532Sahl */
1186532Sahl mutex_exit(&bucket->ftb_mtx);
1187532Sahl
1188532Sahl new_fprc = kmem_zalloc(sizeof (fasttrap_proc_t), KM_SLEEP);
1189532Sahl new_fprc->ftpc_pid = pid;
11904821Sahl new_fprc->ftpc_rcount = 1;
11914821Sahl new_fprc->ftpc_acount = 1;
1192532Sahl
1193532Sahl mutex_enter(&bucket->ftb_mtx);
1194532Sahl
1195532Sahl /*
1196532Sahl * Take another lap through the list to make sure a proc hasn't
1197532Sahl * been created for this pid while we weren't under the bucket lock.
1198532Sahl */
1199532Sahl for (fprc = bucket->ftb_data; fprc != NULL; fprc = fprc->ftpc_next) {
12004821Sahl if (fprc->ftpc_pid == pid && fprc->ftpc_acount != 0) {
1201532Sahl mutex_enter(&fprc->ftpc_mtx);
1202532Sahl mutex_exit(&bucket->ftb_mtx);
12034821Sahl fprc->ftpc_rcount++;
12044821Sahl atomic_add_64(&fprc->ftpc_acount, 1);
12056390Sahl ASSERT(fprc->ftpc_acount <= fprc->ftpc_rcount);
1206532Sahl mutex_exit(&fprc->ftpc_mtx);
1207532Sahl
1208532Sahl kmem_free(new_fprc, sizeof (fasttrap_proc_t));
1209532Sahl
1210532Sahl return (fprc);
1211532Sahl }
1212532Sahl }
1213532Sahl
1214532Sahl new_fprc->ftpc_next = bucket->ftb_data;
1215532Sahl bucket->ftb_data = new_fprc;
1216532Sahl
1217532Sahl mutex_exit(&bucket->ftb_mtx);
1218532Sahl
1219532Sahl return (new_fprc);
1220532Sahl }
1221532Sahl
1222532Sahl static void
fasttrap_proc_release(fasttrap_proc_t * proc)1223532Sahl fasttrap_proc_release(fasttrap_proc_t *proc)
1224532Sahl {
1225532Sahl fasttrap_bucket_t *bucket;
1226532Sahl fasttrap_proc_t *fprc, **fprcp;
1227532Sahl pid_t pid = proc->ftpc_pid;
1228532Sahl
1229532Sahl mutex_enter(&proc->ftpc_mtx);
1230532Sahl
12314821Sahl ASSERT(proc->ftpc_rcount != 0);
12326390Sahl ASSERT(proc->ftpc_acount <= proc->ftpc_rcount);
1233532Sahl
12344821Sahl if (--proc->ftpc_rcount != 0) {
1235532Sahl mutex_exit(&proc->ftpc_mtx);
1236532Sahl return;
1237532Sahl }
1238532Sahl
1239532Sahl mutex_exit(&proc->ftpc_mtx);
1240532Sahl
12414821Sahl /*
12424821Sahl * There should definitely be no live providers associated with this
12434821Sahl * process at this point.
12444821Sahl */
12454821Sahl ASSERT(proc->ftpc_acount == 0);
12464821Sahl
1247532Sahl bucket = &fasttrap_procs.fth_table[FASTTRAP_PROCS_INDEX(pid)];
1248532Sahl mutex_enter(&bucket->ftb_mtx);
1249532Sahl
1250532Sahl fprcp = (fasttrap_proc_t **)&bucket->ftb_data;
1251532Sahl while ((fprc = *fprcp) != NULL) {
1252532Sahl if (fprc == proc)
1253532Sahl break;
1254532Sahl
1255532Sahl fprcp = &fprc->ftpc_next;
1256532Sahl }
1257532Sahl
1258532Sahl /*
1259532Sahl * Something strange has happened if we can't find the proc.
1260532Sahl */
1261532Sahl ASSERT(fprc != NULL);
1262532Sahl
1263532Sahl *fprcp = fprc->ftpc_next;
1264532Sahl
1265532Sahl mutex_exit(&bucket->ftb_mtx);
1266532Sahl
1267532Sahl kmem_free(fprc, sizeof (fasttrap_proc_t));
1268532Sahl }
1269532Sahl
12700Sstevel@tonic-gate /*
12710Sstevel@tonic-gate * Lookup a fasttrap-managed provider based on its name and associated pid.
12720Sstevel@tonic-gate * If the pattr argument is non-NULL, this function instantiates the provider
12730Sstevel@tonic-gate * if it doesn't exist otherwise it returns NULL. The provider is returned
12740Sstevel@tonic-gate * with its lock held.
12750Sstevel@tonic-gate */
12760Sstevel@tonic-gate static fasttrap_provider_t *
fasttrap_provider_lookup(pid_t pid,const char * name,const dtrace_pattr_t * pattr)12770Sstevel@tonic-gate fasttrap_provider_lookup(pid_t pid, const char *name,
12780Sstevel@tonic-gate const dtrace_pattr_t *pattr)
12790Sstevel@tonic-gate {
12800Sstevel@tonic-gate fasttrap_provider_t *fp, *new_fp = NULL;
12810Sstevel@tonic-gate fasttrap_bucket_t *bucket;
12820Sstevel@tonic-gate char provname[DTRACE_PROVNAMELEN];
12830Sstevel@tonic-gate proc_t *p;
12841677Sdp cred_t *cred;
12850Sstevel@tonic-gate
12860Sstevel@tonic-gate ASSERT(strlen(name) < sizeof (fp->ftp_name));
1287935Sahl ASSERT(pattr != NULL);
12880Sstevel@tonic-gate
12890Sstevel@tonic-gate bucket = &fasttrap_provs.fth_table[FASTTRAP_PROVS_INDEX(pid, name)];
12900Sstevel@tonic-gate mutex_enter(&bucket->ftb_mtx);
12910Sstevel@tonic-gate
12920Sstevel@tonic-gate /*
12930Sstevel@tonic-gate * Take a lap through the list and return the match if we find it.
12940Sstevel@tonic-gate */
12950Sstevel@tonic-gate for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) {
12960Sstevel@tonic-gate if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 &&
1297532Sahl !fp->ftp_retired) {
12980Sstevel@tonic-gate mutex_enter(&fp->ftp_mtx);
12990Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx);
13000Sstevel@tonic-gate return (fp);
13010Sstevel@tonic-gate }
13020Sstevel@tonic-gate }
13030Sstevel@tonic-gate
13040Sstevel@tonic-gate /*
13050Sstevel@tonic-gate * Drop the bucket lock so we don't try to perform a sleeping
13060Sstevel@tonic-gate * allocation under it.
13070Sstevel@tonic-gate */
13080Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx);
13090Sstevel@tonic-gate
1310532Sahl /*
13110Sstevel@tonic-gate * Make sure the process exists, isn't a child created as the result
13121677Sdp * of a vfork(2), and isn't a zombie (but may be in fork).
13130Sstevel@tonic-gate */
13140Sstevel@tonic-gate mutex_enter(&pidlock);
1315390Sraf if ((p = prfind(pid)) == NULL) {
13160Sstevel@tonic-gate mutex_exit(&pidlock);
13170Sstevel@tonic-gate return (NULL);
13180Sstevel@tonic-gate }
13190Sstevel@tonic-gate mutex_enter(&p->p_lock);
13200Sstevel@tonic-gate mutex_exit(&pidlock);
1321390Sraf if (p->p_flag & (SVFORK | SEXITING)) {
1322390Sraf mutex_exit(&p->p_lock);
1323390Sraf return (NULL);
1324390Sraf }
13250Sstevel@tonic-gate
13260Sstevel@tonic-gate /*
13270Sstevel@tonic-gate * Increment p_dtrace_probes so that the process knows to inform us
13280Sstevel@tonic-gate * when it exits or execs. fasttrap_provider_free() decrements this
13290Sstevel@tonic-gate * when we're done with this provider.
13300Sstevel@tonic-gate */
13310Sstevel@tonic-gate p->p_dtrace_probes++;
13320Sstevel@tonic-gate
13331677Sdp /*
13341677Sdp * Grab the credentials for this process so we have
13351677Sdp * something to pass to dtrace_register().
13361677Sdp */
13370Sstevel@tonic-gate mutex_enter(&p->p_crlock);
13381677Sdp crhold(p->p_cred);
13391677Sdp cred = p->p_cred;
13400Sstevel@tonic-gate mutex_exit(&p->p_crlock);
13410Sstevel@tonic-gate mutex_exit(&p->p_lock);
13420Sstevel@tonic-gate
13430Sstevel@tonic-gate new_fp = kmem_zalloc(sizeof (fasttrap_provider_t), KM_SLEEP);
1344532Sahl new_fp->ftp_pid = pid;
1345532Sahl new_fp->ftp_proc = fasttrap_proc_lookup(pid);
1346532Sahl
1347532Sahl ASSERT(new_fp->ftp_proc != NULL);
13480Sstevel@tonic-gate
13490Sstevel@tonic-gate mutex_enter(&bucket->ftb_mtx);
13500Sstevel@tonic-gate
13510Sstevel@tonic-gate /*
13520Sstevel@tonic-gate * Take another lap through the list to make sure a provider hasn't
13530Sstevel@tonic-gate * been created for this pid while we weren't under the bucket lock.
13540Sstevel@tonic-gate */
13550Sstevel@tonic-gate for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) {
13560Sstevel@tonic-gate if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 &&
1357532Sahl !fp->ftp_retired) {
13580Sstevel@tonic-gate mutex_enter(&fp->ftp_mtx);
13590Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx);
13600Sstevel@tonic-gate fasttrap_provider_free(new_fp);
13611677Sdp crfree(cred);
13620Sstevel@tonic-gate return (fp);
13630Sstevel@tonic-gate }
13640Sstevel@tonic-gate }
13650Sstevel@tonic-gate
13660Sstevel@tonic-gate (void) strcpy(new_fp->ftp_name, name);
13670Sstevel@tonic-gate
13680Sstevel@tonic-gate /*
13690Sstevel@tonic-gate * Fail and return NULL if either the provider name is too long
13700Sstevel@tonic-gate * or we fail to register this new provider with the DTrace
13710Sstevel@tonic-gate * framework. Note that this is the only place we ever construct
13720Sstevel@tonic-gate * the full provider name -- we keep it in pieces in the provider
13730Sstevel@tonic-gate * structure.
13740Sstevel@tonic-gate */
13750Sstevel@tonic-gate if (snprintf(provname, sizeof (provname), "%s%u", name, (uint_t)pid) >=
13760Sstevel@tonic-gate sizeof (provname) ||
13770Sstevel@tonic-gate dtrace_register(provname, pattr,
13781677Sdp DTRACE_PRIV_PROC | DTRACE_PRIV_OWNER | DTRACE_PRIV_ZONEOWNER, cred,
13790Sstevel@tonic-gate pattr == &pid_attr ? &pid_pops : &usdt_pops, new_fp,
13800Sstevel@tonic-gate &new_fp->ftp_provid) != 0) {
13810Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx);
13820Sstevel@tonic-gate fasttrap_provider_free(new_fp);
13831677Sdp crfree(cred);
13840Sstevel@tonic-gate return (NULL);
13850Sstevel@tonic-gate }
13860Sstevel@tonic-gate
13870Sstevel@tonic-gate new_fp->ftp_next = bucket->ftb_data;
13880Sstevel@tonic-gate bucket->ftb_data = new_fp;
13890Sstevel@tonic-gate
13900Sstevel@tonic-gate mutex_enter(&new_fp->ftp_mtx);
13910Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx);
13920Sstevel@tonic-gate
13931677Sdp crfree(cred);
13940Sstevel@tonic-gate return (new_fp);
13950Sstevel@tonic-gate }
13960Sstevel@tonic-gate
13970Sstevel@tonic-gate static void
fasttrap_provider_free(fasttrap_provider_t * provider)13980Sstevel@tonic-gate fasttrap_provider_free(fasttrap_provider_t *provider)
13990Sstevel@tonic-gate {
14000Sstevel@tonic-gate pid_t pid = provider->ftp_pid;
14010Sstevel@tonic-gate proc_t *p;
14020Sstevel@tonic-gate
14030Sstevel@tonic-gate /*
14041880Sahl * There need to be no associated enabled probes, no consumers
14051880Sahl * creating probes, and no meta providers referencing this provider.
14060Sstevel@tonic-gate */
14071880Sahl ASSERT(provider->ftp_rcount == 0);
14080Sstevel@tonic-gate ASSERT(provider->ftp_ccount == 0);
14091880Sahl ASSERT(provider->ftp_mcount == 0);
14100Sstevel@tonic-gate
14116390Sahl /*
14126390Sahl * If this provider hasn't been retired, we need to explicitly drop the
14136390Sahl * count of active providers on the associated process structure.
14146390Sahl */
14156390Sahl if (!provider->ftp_retired) {
14166390Sahl atomic_add_64(&provider->ftp_proc->ftpc_acount, -1);
14176390Sahl ASSERT(provider->ftp_proc->ftpc_acount <
14186390Sahl provider->ftp_proc->ftpc_rcount);
14196390Sahl }
14206390Sahl
1421532Sahl fasttrap_proc_release(provider->ftp_proc);
1422532Sahl
14230Sstevel@tonic-gate kmem_free(provider, sizeof (fasttrap_provider_t));
14240Sstevel@tonic-gate
14250Sstevel@tonic-gate /*
14260Sstevel@tonic-gate * Decrement p_dtrace_probes on the process whose provider we're
14270Sstevel@tonic-gate * freeing. We don't have to worry about clobbering somone else's
14280Sstevel@tonic-gate * modifications to it because we have locked the bucket that
14290Sstevel@tonic-gate * corresponds to this process's hash chain in the provider hash
14300Sstevel@tonic-gate * table. Don't sweat it if we can't find the process.
14310Sstevel@tonic-gate */
14320Sstevel@tonic-gate mutex_enter(&pidlock);
14330Sstevel@tonic-gate if ((p = prfind(pid)) == NULL) {
14340Sstevel@tonic-gate mutex_exit(&pidlock);
14350Sstevel@tonic-gate return;
14360Sstevel@tonic-gate }
14370Sstevel@tonic-gate
14380Sstevel@tonic-gate mutex_enter(&p->p_lock);
14390Sstevel@tonic-gate mutex_exit(&pidlock);
14400Sstevel@tonic-gate
14410Sstevel@tonic-gate p->p_dtrace_probes--;
14420Sstevel@tonic-gate mutex_exit(&p->p_lock);
14430Sstevel@tonic-gate }
14440Sstevel@tonic-gate
14450Sstevel@tonic-gate static void
fasttrap_provider_retire(pid_t pid,const char * name,int mprov)14461880Sahl fasttrap_provider_retire(pid_t pid, const char *name, int mprov)
14470Sstevel@tonic-gate {
1448935Sahl fasttrap_provider_t *fp;
1449935Sahl fasttrap_bucket_t *bucket;
1450935Sahl dtrace_provider_id_t provid;
1451935Sahl
1452935Sahl ASSERT(strlen(name) < sizeof (fp->ftp_name));
1453935Sahl
1454935Sahl bucket = &fasttrap_provs.fth_table[FASTTRAP_PROVS_INDEX(pid, name)];
1455935Sahl mutex_enter(&bucket->ftb_mtx);
1456935Sahl
1457935Sahl for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) {
1458935Sahl if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 &&
1459935Sahl !fp->ftp_retired)
1460935Sahl break;
1461935Sahl }
1462935Sahl
1463935Sahl if (fp == NULL) {
1464935Sahl mutex_exit(&bucket->ftb_mtx);
1465935Sahl return;
1466935Sahl }
14670Sstevel@tonic-gate
14681880Sahl mutex_enter(&fp->ftp_mtx);
14691880Sahl ASSERT(!mprov || fp->ftp_mcount > 0);
14701880Sahl if (mprov && --fp->ftp_mcount != 0) {
14711880Sahl mutex_exit(&fp->ftp_mtx);
14721880Sahl mutex_exit(&bucket->ftb_mtx);
14731880Sahl return;
14741880Sahl }
14751880Sahl
14760Sstevel@tonic-gate /*
14774821Sahl * Mark the provider to be removed in our post-processing step, mark it
14784821Sahl * retired, and drop the active count on its proc. Marking it indicates
14794821Sahl * that we should try to remove it; setting the retired flag indicates
14804821Sahl * that we're done with this provider; dropping the active the proc
14814821Sahl * releases our hold, and when this reaches zero (as it will during
14824821Sahl * exit or exec) the proc and associated providers become defunct.
1483935Sahl *
1484935Sahl * We obviously need to take the bucket lock before the provider lock
1485935Sahl * to perform the lookup, but we need to drop the provider lock
1486935Sahl * before calling into the DTrace framework since we acquire the
1487935Sahl * provider lock in callbacks invoked from the DTrace framework. The
1488935Sahl * bucket lock therefore protects the integrity of the provider hash
1489935Sahl * table.
14900Sstevel@tonic-gate */
14914821Sahl atomic_add_64(&fp->ftp_proc->ftpc_acount, -1);
14926390Sahl ASSERT(fp->ftp_proc->ftpc_acount < fp->ftp_proc->ftpc_rcount);
14936390Sahl
1494935Sahl fp->ftp_retired = 1;
1495935Sahl fp->ftp_marked = 1;
1496935Sahl provid = fp->ftp_provid;
1497935Sahl mutex_exit(&fp->ftp_mtx);
14980Sstevel@tonic-gate
14990Sstevel@tonic-gate /*
15000Sstevel@tonic-gate * We don't have to worry about invalidating the same provider twice
15010Sstevel@tonic-gate * since fasttrap_provider_lookup() will ignore provider that have
1502532Sahl * been marked as retired.
15030Sstevel@tonic-gate */
15040Sstevel@tonic-gate dtrace_invalidate(provid);
15050Sstevel@tonic-gate
1506935Sahl mutex_exit(&bucket->ftb_mtx);
1507935Sahl
15080Sstevel@tonic-gate fasttrap_pid_cleanup();
15090Sstevel@tonic-gate }
15100Sstevel@tonic-gate
15110Sstevel@tonic-gate static int
fasttrap_uint32_cmp(const void * ap,const void * bp)15123944Sahl fasttrap_uint32_cmp(const void *ap, const void *bp)
15133944Sahl {
15143944Sahl return (*(const uint32_t *)ap - *(const uint32_t *)bp);
15153944Sahl }
15163944Sahl
15173944Sahl static int
fasttrap_uint64_cmp(const void * ap,const void * bp)15183944Sahl fasttrap_uint64_cmp(const void *ap, const void *bp)
15193944Sahl {
15203944Sahl return (*(const uint64_t *)ap - *(const uint64_t *)bp);
15213944Sahl }
15223944Sahl
15233944Sahl static int
fasttrap_add_probe(fasttrap_probe_spec_t * pdata)15240Sstevel@tonic-gate fasttrap_add_probe(fasttrap_probe_spec_t *pdata)
15250Sstevel@tonic-gate {
15260Sstevel@tonic-gate fasttrap_provider_t *provider;
15270Sstevel@tonic-gate fasttrap_probe_t *pp;
15280Sstevel@tonic-gate fasttrap_tracepoint_t *tp;
15290Sstevel@tonic-gate char *name;
15300Sstevel@tonic-gate int i, aframes, whack;
15310Sstevel@tonic-gate
15323944Sahl /*
15333944Sahl * There needs to be at least one desired trace point.
15343944Sahl */
15353944Sahl if (pdata->ftps_noffs == 0)
15363944Sahl return (EINVAL);
15373944Sahl
15380Sstevel@tonic-gate switch (pdata->ftps_type) {
15390Sstevel@tonic-gate case DTFTP_ENTRY:
15400Sstevel@tonic-gate name = "entry";
15410Sstevel@tonic-gate aframes = FASTTRAP_ENTRY_AFRAMES;
15420Sstevel@tonic-gate break;
15430Sstevel@tonic-gate case DTFTP_RETURN:
15440Sstevel@tonic-gate name = "return";
15450Sstevel@tonic-gate aframes = FASTTRAP_RETURN_AFRAMES;
15460Sstevel@tonic-gate break;
15470Sstevel@tonic-gate case DTFTP_OFFSETS:
15480Sstevel@tonic-gate name = NULL;
15490Sstevel@tonic-gate break;
15500Sstevel@tonic-gate default:
15510Sstevel@tonic-gate return (EINVAL);
15520Sstevel@tonic-gate }
15530Sstevel@tonic-gate
15540Sstevel@tonic-gate if ((provider = fasttrap_provider_lookup(pdata->ftps_pid,
15550Sstevel@tonic-gate FASTTRAP_PID_NAME, &pid_attr)) == NULL)
15560Sstevel@tonic-gate return (ESRCH);
15570Sstevel@tonic-gate
15580Sstevel@tonic-gate /*
15590Sstevel@tonic-gate * Increment this reference count to indicate that a consumer is
15602179Sahl * actively adding a new probe associated with this provider. This
15612179Sahl * prevents the provider from being deleted -- we'll need to check
15622179Sahl * for pending deletions when we drop this reference count.
15630Sstevel@tonic-gate */
15640Sstevel@tonic-gate provider->ftp_ccount++;
15650Sstevel@tonic-gate mutex_exit(&provider->ftp_mtx);
15660Sstevel@tonic-gate
15672179Sahl /*
15682179Sahl * Grab the creation lock to ensure consistency between calls to
15692179Sahl * dtrace_probe_lookup() and dtrace_probe_create() in the face of
15702179Sahl * other threads creating probes. We must drop the provider lock
15712179Sahl * before taking this lock to avoid a three-way deadlock with the
15722179Sahl * DTrace framework.
15732179Sahl */
15742179Sahl mutex_enter(&provider->ftp_cmtx);
15750Sstevel@tonic-gate
15762179Sahl if (name == NULL) {
15770Sstevel@tonic-gate for (i = 0; i < pdata->ftps_noffs; i++) {
15780Sstevel@tonic-gate char name_str[17];
15790Sstevel@tonic-gate
15800Sstevel@tonic-gate (void) sprintf(name_str, "%llx",
15810Sstevel@tonic-gate (unsigned long long)pdata->ftps_offs[i]);
15820Sstevel@tonic-gate
15830Sstevel@tonic-gate if (dtrace_probe_lookup(provider->ftp_provid,
15840Sstevel@tonic-gate pdata->ftps_mod, pdata->ftps_func, name_str) != 0)
15850Sstevel@tonic-gate continue;
15860Sstevel@tonic-gate
15870Sstevel@tonic-gate atomic_add_32(&fasttrap_total, 1);
15880Sstevel@tonic-gate
15890Sstevel@tonic-gate if (fasttrap_total > fasttrap_max) {
15900Sstevel@tonic-gate atomic_add_32(&fasttrap_total, -1);
15910Sstevel@tonic-gate goto no_mem;
15920Sstevel@tonic-gate }
15930Sstevel@tonic-gate
15940Sstevel@tonic-gate pp = kmem_zalloc(sizeof (fasttrap_probe_t), KM_SLEEP);
15950Sstevel@tonic-gate
15960Sstevel@tonic-gate pp->ftp_prov = provider;
15970Sstevel@tonic-gate pp->ftp_faddr = pdata->ftps_pc;
15980Sstevel@tonic-gate pp->ftp_fsize = pdata->ftps_size;
15990Sstevel@tonic-gate pp->ftp_pid = pdata->ftps_pid;
16000Sstevel@tonic-gate pp->ftp_ntps = 1;
16010Sstevel@tonic-gate
16020Sstevel@tonic-gate tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t),
16030Sstevel@tonic-gate KM_SLEEP);
16040Sstevel@tonic-gate
1605532Sahl tp->ftt_proc = provider->ftp_proc;
16060Sstevel@tonic-gate tp->ftt_pc = pdata->ftps_offs[i] + pdata->ftps_pc;
16070Sstevel@tonic-gate tp->ftt_pid = pdata->ftps_pid;
16080Sstevel@tonic-gate
16090Sstevel@tonic-gate pp->ftp_tps[0].fit_tp = tp;
16100Sstevel@tonic-gate pp->ftp_tps[0].fit_id.fti_probe = pp;
16111710Sahl pp->ftp_tps[0].fit_id.fti_ptype = pdata->ftps_type;
16120Sstevel@tonic-gate
16130Sstevel@tonic-gate pp->ftp_id = dtrace_probe_create(provider->ftp_provid,
16140Sstevel@tonic-gate pdata->ftps_mod, pdata->ftps_func, name_str,
16150Sstevel@tonic-gate FASTTRAP_OFFSET_AFRAMES, pp);
16160Sstevel@tonic-gate }
16172179Sahl
16182179Sahl } else if (dtrace_probe_lookup(provider->ftp_provid, pdata->ftps_mod,
16192179Sahl pdata->ftps_func, name) == 0) {
16202179Sahl atomic_add_32(&fasttrap_total, pdata->ftps_noffs);
16212179Sahl
16222179Sahl if (fasttrap_total > fasttrap_max) {
16232179Sahl atomic_add_32(&fasttrap_total, -pdata->ftps_noffs);
16242179Sahl goto no_mem;
16252179Sahl }
16262179Sahl
16273944Sahl /*
16283944Sahl * Make sure all tracepoint program counter values are unique.
16293944Sahl * We later assume that each probe has exactly one tracepoint
16303944Sahl * for a given pc.
16313944Sahl */
16323944Sahl qsort(pdata->ftps_offs, pdata->ftps_noffs,
16333944Sahl sizeof (uint64_t), fasttrap_uint64_cmp);
16343944Sahl for (i = 1; i < pdata->ftps_noffs; i++) {
16353944Sahl if (pdata->ftps_offs[i] > pdata->ftps_offs[i - 1])
16363944Sahl continue;
16373944Sahl
16383944Sahl atomic_add_32(&fasttrap_total, -pdata->ftps_noffs);
16393944Sahl goto no_mem;
16403944Sahl }
16413944Sahl
16422179Sahl ASSERT(pdata->ftps_noffs > 0);
16432179Sahl pp = kmem_zalloc(offsetof(fasttrap_probe_t,
16442179Sahl ftp_tps[pdata->ftps_noffs]), KM_SLEEP);
16452179Sahl
16462179Sahl pp->ftp_prov = provider;
16472179Sahl pp->ftp_faddr = pdata->ftps_pc;
16482179Sahl pp->ftp_fsize = pdata->ftps_size;
16492179Sahl pp->ftp_pid = pdata->ftps_pid;
16502179Sahl pp->ftp_ntps = pdata->ftps_noffs;
16512179Sahl
16522179Sahl for (i = 0; i < pdata->ftps_noffs; i++) {
16532179Sahl tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t),
16542179Sahl KM_SLEEP);
16552179Sahl
16562179Sahl tp->ftt_proc = provider->ftp_proc;
16572179Sahl tp->ftt_pc = pdata->ftps_offs[i] + pdata->ftps_pc;
16582179Sahl tp->ftt_pid = pdata->ftps_pid;
16592179Sahl
16602179Sahl pp->ftp_tps[i].fit_tp = tp;
16612179Sahl pp->ftp_tps[i].fit_id.fti_probe = pp;
16622179Sahl pp->ftp_tps[i].fit_id.fti_ptype = pdata->ftps_type;
16632179Sahl }
16642179Sahl
16652179Sahl pp->ftp_id = dtrace_probe_create(provider->ftp_provid,
16662179Sahl pdata->ftps_mod, pdata->ftps_func, name, aframes, pp);
16670Sstevel@tonic-gate }
16680Sstevel@tonic-gate
16692179Sahl mutex_exit(&provider->ftp_cmtx);
16702179Sahl
16710Sstevel@tonic-gate /*
16720Sstevel@tonic-gate * We know that the provider is still valid since we incremented the
16732179Sahl * creation reference count. If someone tried to clean up this provider
16742179Sahl * while we were using it (e.g. because the process called exec(2) or
16752179Sahl * exit(2)), take note of that and try to clean it up now.
16760Sstevel@tonic-gate */
16770Sstevel@tonic-gate mutex_enter(&provider->ftp_mtx);
16780Sstevel@tonic-gate provider->ftp_ccount--;
1679532Sahl whack = provider->ftp_retired;
16800Sstevel@tonic-gate mutex_exit(&provider->ftp_mtx);
16810Sstevel@tonic-gate
16820Sstevel@tonic-gate if (whack)
16830Sstevel@tonic-gate fasttrap_pid_cleanup();
16840Sstevel@tonic-gate
16850Sstevel@tonic-gate return (0);
16860Sstevel@tonic-gate
16870Sstevel@tonic-gate no_mem:
16880Sstevel@tonic-gate /*
16890Sstevel@tonic-gate * If we've exhausted the allowable resources, we'll try to remove
16900Sstevel@tonic-gate * this provider to free some up. This is to cover the case where
16910Sstevel@tonic-gate * the user has accidentally created many more probes than was
16920Sstevel@tonic-gate * intended (e.g. pid123:::).
16930Sstevel@tonic-gate */
16942179Sahl mutex_exit(&provider->ftp_cmtx);
16950Sstevel@tonic-gate mutex_enter(&provider->ftp_mtx);
16960Sstevel@tonic-gate provider->ftp_ccount--;
16970Sstevel@tonic-gate provider->ftp_marked = 1;
16980Sstevel@tonic-gate mutex_exit(&provider->ftp_mtx);
16990Sstevel@tonic-gate
17000Sstevel@tonic-gate fasttrap_pid_cleanup();
17010Sstevel@tonic-gate
17020Sstevel@tonic-gate return (ENOMEM);
17030Sstevel@tonic-gate }
17040Sstevel@tonic-gate
17050Sstevel@tonic-gate /*ARGSUSED*/
17060Sstevel@tonic-gate static void *
fasttrap_meta_provide(void * arg,dtrace_helper_provdesc_t * dhpv,pid_t pid)17070Sstevel@tonic-gate fasttrap_meta_provide(void *arg, dtrace_helper_provdesc_t *dhpv, pid_t pid)
17080Sstevel@tonic-gate {
17090Sstevel@tonic-gate fasttrap_provider_t *provider;
17100Sstevel@tonic-gate
17110Sstevel@tonic-gate /*
17120Sstevel@tonic-gate * A 32-bit unsigned integer (like a pid for example) can be
17130Sstevel@tonic-gate * expressed in 10 or fewer decimal digits. Make sure that we'll
17140Sstevel@tonic-gate * have enough space for the provider name.
17150Sstevel@tonic-gate */
17160Sstevel@tonic-gate if (strlen(dhpv->dthpv_provname) + 10 >=
17170Sstevel@tonic-gate sizeof (provider->ftp_name)) {
17180Sstevel@tonic-gate cmn_err(CE_WARN, "failed to instantiate provider %s: "
17190Sstevel@tonic-gate "name too long to accomodate pid", dhpv->dthpv_provname);
17200Sstevel@tonic-gate return (NULL);
17210Sstevel@tonic-gate }
17220Sstevel@tonic-gate
17230Sstevel@tonic-gate /*
17240Sstevel@tonic-gate * Don't let folks spoof the true pid provider.
17250Sstevel@tonic-gate */
17260Sstevel@tonic-gate if (strcmp(dhpv->dthpv_provname, FASTTRAP_PID_NAME) == 0) {
17270Sstevel@tonic-gate cmn_err(CE_WARN, "failed to instantiate provider %s: "
17280Sstevel@tonic-gate "%s is an invalid name", dhpv->dthpv_provname,
17290Sstevel@tonic-gate FASTTRAP_PID_NAME);
17300Sstevel@tonic-gate return (NULL);
17310Sstevel@tonic-gate }
17320Sstevel@tonic-gate
17330Sstevel@tonic-gate /*
17340Sstevel@tonic-gate * The highest stability class that fasttrap supports is ISA; cap
17350Sstevel@tonic-gate * the stability of the new provider accordingly.
17360Sstevel@tonic-gate */
17374821Sahl if (dhpv->dthpv_pattr.dtpa_provider.dtat_class > DTRACE_CLASS_ISA)
17380Sstevel@tonic-gate dhpv->dthpv_pattr.dtpa_provider.dtat_class = DTRACE_CLASS_ISA;
17394821Sahl if (dhpv->dthpv_pattr.dtpa_mod.dtat_class > DTRACE_CLASS_ISA)
17400Sstevel@tonic-gate dhpv->dthpv_pattr.dtpa_mod.dtat_class = DTRACE_CLASS_ISA;
17414821Sahl if (dhpv->dthpv_pattr.dtpa_func.dtat_class > DTRACE_CLASS_ISA)
17420Sstevel@tonic-gate dhpv->dthpv_pattr.dtpa_func.dtat_class = DTRACE_CLASS_ISA;
17434821Sahl if (dhpv->dthpv_pattr.dtpa_name.dtat_class > DTRACE_CLASS_ISA)
17440Sstevel@tonic-gate dhpv->dthpv_pattr.dtpa_name.dtat_class = DTRACE_CLASS_ISA;
17454821Sahl if (dhpv->dthpv_pattr.dtpa_args.dtat_class > DTRACE_CLASS_ISA)
17460Sstevel@tonic-gate dhpv->dthpv_pattr.dtpa_args.dtat_class = DTRACE_CLASS_ISA;
17470Sstevel@tonic-gate
17480Sstevel@tonic-gate if ((provider = fasttrap_provider_lookup(pid, dhpv->dthpv_provname,
17490Sstevel@tonic-gate &dhpv->dthpv_pattr)) == NULL) {
17500Sstevel@tonic-gate cmn_err(CE_WARN, "failed to instantiate provider %s for "
17510Sstevel@tonic-gate "process %u", dhpv->dthpv_provname, (uint_t)pid);
17520Sstevel@tonic-gate return (NULL);
17530Sstevel@tonic-gate }
17540Sstevel@tonic-gate
17550Sstevel@tonic-gate /*
17561880Sahl * Up the meta provider count so this provider isn't removed until
17571880Sahl * the meta provider has been told to remove it.
17580Sstevel@tonic-gate */
17591880Sahl provider->ftp_mcount++;
17600Sstevel@tonic-gate
17610Sstevel@tonic-gate mutex_exit(&provider->ftp_mtx);
17620Sstevel@tonic-gate
17630Sstevel@tonic-gate return (provider);
17640Sstevel@tonic-gate }
17650Sstevel@tonic-gate
17660Sstevel@tonic-gate /*ARGSUSED*/
17670Sstevel@tonic-gate static void
fasttrap_meta_create_probe(void * arg,void * parg,dtrace_helper_probedesc_t * dhpb)17680Sstevel@tonic-gate fasttrap_meta_create_probe(void *arg, void *parg,
17690Sstevel@tonic-gate dtrace_helper_probedesc_t *dhpb)
17700Sstevel@tonic-gate {
17710Sstevel@tonic-gate fasttrap_provider_t *provider = parg;
17720Sstevel@tonic-gate fasttrap_probe_t *pp;
17730Sstevel@tonic-gate fasttrap_tracepoint_t *tp;
17741710Sahl int i, j;
17751710Sahl uint32_t ntps;
17760Sstevel@tonic-gate
17772179Sahl /*
17782179Sahl * Since the meta provider count is non-zero we don't have to worry
17792179Sahl * about this provider disappearing.
17802179Sahl */
17812179Sahl ASSERT(provider->ftp_mcount > 0);
17822179Sahl
17832179Sahl /*
17843944Sahl * The offsets must be unique.
17853944Sahl */
17863944Sahl qsort(dhpb->dthpb_offs, dhpb->dthpb_noffs, sizeof (uint32_t),
17873944Sahl fasttrap_uint32_cmp);
17883944Sahl for (i = 1; i < dhpb->dthpb_noffs; i++) {
17893944Sahl if (dhpb->dthpb_base + dhpb->dthpb_offs[i] <=
17903944Sahl dhpb->dthpb_base + dhpb->dthpb_offs[i - 1])
17913944Sahl return;
17923944Sahl }
17933944Sahl
17943944Sahl qsort(dhpb->dthpb_enoffs, dhpb->dthpb_nenoffs, sizeof (uint32_t),
17953944Sahl fasttrap_uint32_cmp);
17963944Sahl for (i = 1; i < dhpb->dthpb_nenoffs; i++) {
17973944Sahl if (dhpb->dthpb_base + dhpb->dthpb_enoffs[i] <=
17983944Sahl dhpb->dthpb_base + dhpb->dthpb_enoffs[i - 1])
17993944Sahl return;
18003944Sahl }
18013944Sahl
18023944Sahl /*
18032179Sahl * Grab the creation lock to ensure consistency between calls to
18042179Sahl * dtrace_probe_lookup() and dtrace_probe_create() in the face of
18052179Sahl * other threads creating probes.
18062179Sahl */
18072179Sahl mutex_enter(&provider->ftp_cmtx);
18080Sstevel@tonic-gate
18090Sstevel@tonic-gate if (dtrace_probe_lookup(provider->ftp_provid, dhpb->dthpb_mod,
18100Sstevel@tonic-gate dhpb->dthpb_func, dhpb->dthpb_name) != 0) {
18112179Sahl mutex_exit(&provider->ftp_cmtx);
18120Sstevel@tonic-gate return;
18130Sstevel@tonic-gate }
18140Sstevel@tonic-gate
18151710Sahl ntps = dhpb->dthpb_noffs + dhpb->dthpb_nenoffs;
18161880Sahl ASSERT(ntps > 0);
18171710Sahl
18181710Sahl atomic_add_32(&fasttrap_total, ntps);
18190Sstevel@tonic-gate
18200Sstevel@tonic-gate if (fasttrap_total > fasttrap_max) {
18211710Sahl atomic_add_32(&fasttrap_total, -ntps);
18222179Sahl mutex_exit(&provider->ftp_cmtx);
18230Sstevel@tonic-gate return;
18240Sstevel@tonic-gate }
18250Sstevel@tonic-gate
18261880Sahl pp = kmem_zalloc(offsetof(fasttrap_probe_t, ftp_tps[ntps]), KM_SLEEP);
18270Sstevel@tonic-gate
18280Sstevel@tonic-gate pp->ftp_prov = provider;
18290Sstevel@tonic-gate pp->ftp_pid = provider->ftp_pid;
18301710Sahl pp->ftp_ntps = ntps;
18310Sstevel@tonic-gate pp->ftp_nargs = dhpb->dthpb_xargc;
18320Sstevel@tonic-gate pp->ftp_xtypes = dhpb->dthpb_xtypes;
18330Sstevel@tonic-gate pp->ftp_ntypes = dhpb->dthpb_ntypes;
18340Sstevel@tonic-gate
18351710Sahl /*
18361710Sahl * First create a tracepoint for each actual point of interest.
18371710Sahl */
18381710Sahl for (i = 0; i < dhpb->dthpb_noffs; i++) {
18390Sstevel@tonic-gate tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), KM_SLEEP);
18400Sstevel@tonic-gate
1841532Sahl tp->ftt_proc = provider->ftp_proc;
18420Sstevel@tonic-gate tp->ftt_pc = dhpb->dthpb_base + dhpb->dthpb_offs[i];
18430Sstevel@tonic-gate tp->ftt_pid = provider->ftp_pid;
18440Sstevel@tonic-gate
18450Sstevel@tonic-gate pp->ftp_tps[i].fit_tp = tp;
18460Sstevel@tonic-gate pp->ftp_tps[i].fit_id.fti_probe = pp;
18471710Sahl #ifdef __sparc
18481710Sahl pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_POST_OFFSETS;
18491710Sahl #else
18501710Sahl pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_OFFSETS;
18511710Sahl #endif
18521710Sahl }
18531710Sahl
18541710Sahl /*
18551710Sahl * Then create a tracepoint for each is-enabled point.
18561710Sahl */
18571710Sahl for (j = 0; i < ntps; i++, j++) {
18581710Sahl tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), KM_SLEEP);
18591710Sahl
18601710Sahl tp->ftt_proc = provider->ftp_proc;
18611710Sahl tp->ftt_pc = dhpb->dthpb_base + dhpb->dthpb_enoffs[j];
18621710Sahl tp->ftt_pid = provider->ftp_pid;
18631710Sahl
18641710Sahl pp->ftp_tps[i].fit_tp = tp;
18651710Sahl pp->ftp_tps[i].fit_id.fti_probe = pp;
18661710Sahl pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_IS_ENABLED;
18670Sstevel@tonic-gate }
18680Sstevel@tonic-gate
18690Sstevel@tonic-gate /*
18700Sstevel@tonic-gate * If the arguments are shuffled around we set the argument remapping
18710Sstevel@tonic-gate * table. Later, when the probe fires, we only remap the arguments
18720Sstevel@tonic-gate * if the table is non-NULL.
18730Sstevel@tonic-gate */
18740Sstevel@tonic-gate for (i = 0; i < dhpb->dthpb_xargc; i++) {
18750Sstevel@tonic-gate if (dhpb->dthpb_args[i] != i) {
18760Sstevel@tonic-gate pp->ftp_argmap = dhpb->dthpb_args;
18770Sstevel@tonic-gate break;
18780Sstevel@tonic-gate }
18790Sstevel@tonic-gate }
18800Sstevel@tonic-gate
18810Sstevel@tonic-gate /*
18820Sstevel@tonic-gate * The probe is fully constructed -- register it with DTrace.
18830Sstevel@tonic-gate */
18840Sstevel@tonic-gate pp->ftp_id = dtrace_probe_create(provider->ftp_provid, dhpb->dthpb_mod,
18850Sstevel@tonic-gate dhpb->dthpb_func, dhpb->dthpb_name, FASTTRAP_OFFSET_AFRAMES, pp);
18860Sstevel@tonic-gate
18872179Sahl mutex_exit(&provider->ftp_cmtx);
18880Sstevel@tonic-gate }
18890Sstevel@tonic-gate
18900Sstevel@tonic-gate /*ARGSUSED*/
18910Sstevel@tonic-gate static void
fasttrap_meta_remove(void * arg,dtrace_helper_provdesc_t * dhpv,pid_t pid)18920Sstevel@tonic-gate fasttrap_meta_remove(void *arg, dtrace_helper_provdesc_t *dhpv, pid_t pid)
18930Sstevel@tonic-gate {
1894935Sahl /*
1895935Sahl * Clean up the USDT provider. There may be active consumers of the
1896935Sahl * provider busy adding probes, no damage will actually befall the
1897935Sahl * provider until that count has dropped to zero. This just puts
1898935Sahl * the provider on death row.
1899935Sahl */
1900935Sahl fasttrap_provider_retire(pid, dhpv->dthpv_provname, 1);
19010Sstevel@tonic-gate }
19020Sstevel@tonic-gate
19030Sstevel@tonic-gate static dtrace_mops_t fasttrap_mops = {
19040Sstevel@tonic-gate fasttrap_meta_create_probe,
19050Sstevel@tonic-gate fasttrap_meta_provide,
19060Sstevel@tonic-gate fasttrap_meta_remove
19070Sstevel@tonic-gate };
19080Sstevel@tonic-gate
19090Sstevel@tonic-gate /*ARGSUSED*/
19100Sstevel@tonic-gate static int
fasttrap_open(dev_t * devp,int flag,int otyp,cred_t * cred_p)19110Sstevel@tonic-gate fasttrap_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
19120Sstevel@tonic-gate {
19130Sstevel@tonic-gate return (0);
19140Sstevel@tonic-gate }
19150Sstevel@tonic-gate
19160Sstevel@tonic-gate /*ARGSUSED*/
19170Sstevel@tonic-gate static int
fasttrap_ioctl(dev_t dev,int cmd,intptr_t arg,int md,cred_t * cr,int * rv)19180Sstevel@tonic-gate fasttrap_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv)
19190Sstevel@tonic-gate {
19200Sstevel@tonic-gate if (!dtrace_attached())
19210Sstevel@tonic-gate return (EAGAIN);
19220Sstevel@tonic-gate
19230Sstevel@tonic-gate if (cmd == FASTTRAPIOC_MAKEPROBE) {
19240Sstevel@tonic-gate fasttrap_probe_spec_t *uprobe = (void *)arg;
19250Sstevel@tonic-gate fasttrap_probe_spec_t *probe;
19260Sstevel@tonic-gate uint64_t noffs;
19270Sstevel@tonic-gate size_t size;
19280Sstevel@tonic-gate int ret;
19290Sstevel@tonic-gate char *c;
19300Sstevel@tonic-gate
19310Sstevel@tonic-gate if (copyin(&uprobe->ftps_noffs, &noffs,
19320Sstevel@tonic-gate sizeof (uprobe->ftps_noffs)))
19330Sstevel@tonic-gate return (EFAULT);
19340Sstevel@tonic-gate
19350Sstevel@tonic-gate /*
19360Sstevel@tonic-gate * Probes must have at least one tracepoint.
19370Sstevel@tonic-gate */
19380Sstevel@tonic-gate if (noffs == 0)
19390Sstevel@tonic-gate return (EINVAL);
19400Sstevel@tonic-gate
19410Sstevel@tonic-gate size = sizeof (fasttrap_probe_spec_t) +
19420Sstevel@tonic-gate sizeof (probe->ftps_offs[0]) * (noffs - 1);
19430Sstevel@tonic-gate
19440Sstevel@tonic-gate if (size > 1024 * 1024)
19450Sstevel@tonic-gate return (ENOMEM);
19460Sstevel@tonic-gate
19470Sstevel@tonic-gate probe = kmem_alloc(size, KM_SLEEP);
19480Sstevel@tonic-gate
1949*9417SJonathan.Haslam@Sun.COM if (copyin(uprobe, probe, size) != 0 ||
1950*9417SJonathan.Haslam@Sun.COM probe->ftps_noffs != noffs) {
19510Sstevel@tonic-gate kmem_free(probe, size);
19520Sstevel@tonic-gate return (EFAULT);
19530Sstevel@tonic-gate }
19540Sstevel@tonic-gate
19550Sstevel@tonic-gate /*
19560Sstevel@tonic-gate * Verify that the function and module strings contain no
19570Sstevel@tonic-gate * funny characters.
19580Sstevel@tonic-gate */
19590Sstevel@tonic-gate for (c = &probe->ftps_func[0]; *c != '\0'; c++) {
19600Sstevel@tonic-gate if (*c < 0x20 || 0x7f <= *c) {
19610Sstevel@tonic-gate ret = EINVAL;
19620Sstevel@tonic-gate goto err;
19630Sstevel@tonic-gate }
19640Sstevel@tonic-gate }
19650Sstevel@tonic-gate
19660Sstevel@tonic-gate for (c = &probe->ftps_mod[0]; *c != '\0'; c++) {
19670Sstevel@tonic-gate if (*c < 0x20 || 0x7f <= *c) {
19680Sstevel@tonic-gate ret = EINVAL;
19690Sstevel@tonic-gate goto err;
19700Sstevel@tonic-gate }
19710Sstevel@tonic-gate }
19720Sstevel@tonic-gate
19730Sstevel@tonic-gate if (!PRIV_POLICY_CHOICE(cr, PRIV_ALL, B_FALSE)) {
19740Sstevel@tonic-gate proc_t *p;
19750Sstevel@tonic-gate pid_t pid = probe->ftps_pid;
19760Sstevel@tonic-gate
19770Sstevel@tonic-gate mutex_enter(&pidlock);
19780Sstevel@tonic-gate /*
19790Sstevel@tonic-gate * Report an error if the process doesn't exist
19800Sstevel@tonic-gate * or is actively being birthed.
19810Sstevel@tonic-gate */
19820Sstevel@tonic-gate if ((p = prfind(pid)) == NULL || p->p_stat == SIDL) {
19830Sstevel@tonic-gate mutex_exit(&pidlock);
19840Sstevel@tonic-gate return (ESRCH);
19850Sstevel@tonic-gate }
19860Sstevel@tonic-gate mutex_enter(&p->p_lock);
19870Sstevel@tonic-gate mutex_exit(&pidlock);
19880Sstevel@tonic-gate
19890Sstevel@tonic-gate if ((ret = priv_proc_cred_perm(cr, p, NULL,
19900Sstevel@tonic-gate VREAD | VWRITE)) != 0) {
19910Sstevel@tonic-gate mutex_exit(&p->p_lock);
19920Sstevel@tonic-gate return (ret);
19930Sstevel@tonic-gate }
19940Sstevel@tonic-gate
19950Sstevel@tonic-gate mutex_exit(&p->p_lock);
19960Sstevel@tonic-gate }
19970Sstevel@tonic-gate
19980Sstevel@tonic-gate ret = fasttrap_add_probe(probe);
19990Sstevel@tonic-gate err:
20000Sstevel@tonic-gate kmem_free(probe, size);
20010Sstevel@tonic-gate
20020Sstevel@tonic-gate return (ret);
20030Sstevel@tonic-gate
20040Sstevel@tonic-gate } else if (cmd == FASTTRAPIOC_GETINSTR) {
20050Sstevel@tonic-gate fasttrap_instr_query_t instr;
20060Sstevel@tonic-gate fasttrap_tracepoint_t *tp;
20070Sstevel@tonic-gate uint_t index;
20080Sstevel@tonic-gate int ret;
20090Sstevel@tonic-gate
20100Sstevel@tonic-gate if (copyin((void *)arg, &instr, sizeof (instr)) != 0)
20110Sstevel@tonic-gate return (EFAULT);
20120Sstevel@tonic-gate
20130Sstevel@tonic-gate if (!PRIV_POLICY_CHOICE(cr, PRIV_ALL, B_FALSE)) {
20140Sstevel@tonic-gate proc_t *p;
20150Sstevel@tonic-gate pid_t pid = instr.ftiq_pid;
20160Sstevel@tonic-gate
20170Sstevel@tonic-gate mutex_enter(&pidlock);
20180Sstevel@tonic-gate /*
20190Sstevel@tonic-gate * Report an error if the process doesn't exist
20200Sstevel@tonic-gate * or is actively being birthed.
20210Sstevel@tonic-gate */
20220Sstevel@tonic-gate if ((p = prfind(pid)) == NULL || p->p_stat == SIDL) {
20230Sstevel@tonic-gate mutex_exit(&pidlock);
20240Sstevel@tonic-gate return (ESRCH);
20250Sstevel@tonic-gate }
20260Sstevel@tonic-gate mutex_enter(&p->p_lock);
20270Sstevel@tonic-gate mutex_exit(&pidlock);
20280Sstevel@tonic-gate
20290Sstevel@tonic-gate if ((ret = priv_proc_cred_perm(cr, p, NULL,
20300Sstevel@tonic-gate VREAD)) != 0) {
20310Sstevel@tonic-gate mutex_exit(&p->p_lock);
20320Sstevel@tonic-gate return (ret);
20330Sstevel@tonic-gate }
20340Sstevel@tonic-gate
20350Sstevel@tonic-gate mutex_exit(&p->p_lock);
20360Sstevel@tonic-gate }
20370Sstevel@tonic-gate
20380Sstevel@tonic-gate index = FASTTRAP_TPOINTS_INDEX(instr.ftiq_pid, instr.ftiq_pc);
20390Sstevel@tonic-gate
20400Sstevel@tonic-gate mutex_enter(&fasttrap_tpoints.fth_table[index].ftb_mtx);
20410Sstevel@tonic-gate tp = fasttrap_tpoints.fth_table[index].ftb_data;
20420Sstevel@tonic-gate while (tp != NULL) {
20430Sstevel@tonic-gate if (instr.ftiq_pid == tp->ftt_pid &&
20440Sstevel@tonic-gate instr.ftiq_pc == tp->ftt_pc &&
20454821Sahl tp->ftt_proc->ftpc_acount != 0)
20460Sstevel@tonic-gate break;
20470Sstevel@tonic-gate
20480Sstevel@tonic-gate tp = tp->ftt_next;
20490Sstevel@tonic-gate }
20500Sstevel@tonic-gate
20510Sstevel@tonic-gate if (tp == NULL) {
20520Sstevel@tonic-gate mutex_exit(&fasttrap_tpoints.fth_table[index].ftb_mtx);
20530Sstevel@tonic-gate return (ENOENT);
20540Sstevel@tonic-gate }
20550Sstevel@tonic-gate
20560Sstevel@tonic-gate bcopy(&tp->ftt_instr, &instr.ftiq_instr,
20570Sstevel@tonic-gate sizeof (instr.ftiq_instr));
20580Sstevel@tonic-gate mutex_exit(&fasttrap_tpoints.fth_table[index].ftb_mtx);
20590Sstevel@tonic-gate
20600Sstevel@tonic-gate if (copyout(&instr, (void *)arg, sizeof (instr)) != 0)
20610Sstevel@tonic-gate return (EFAULT);
20620Sstevel@tonic-gate
20630Sstevel@tonic-gate return (0);
20640Sstevel@tonic-gate }
20650Sstevel@tonic-gate
20660Sstevel@tonic-gate return (EINVAL);
20670Sstevel@tonic-gate }
20680Sstevel@tonic-gate
20690Sstevel@tonic-gate static struct cb_ops fasttrap_cb_ops = {
20700Sstevel@tonic-gate fasttrap_open, /* open */
20710Sstevel@tonic-gate nodev, /* close */
20720Sstevel@tonic-gate nulldev, /* strategy */
20730Sstevel@tonic-gate nulldev, /* print */
20740Sstevel@tonic-gate nodev, /* dump */
20750Sstevel@tonic-gate nodev, /* read */
20760Sstevel@tonic-gate nodev, /* write */
20770Sstevel@tonic-gate fasttrap_ioctl, /* ioctl */
20780Sstevel@tonic-gate nodev, /* devmap */
20790Sstevel@tonic-gate nodev, /* mmap */
20800Sstevel@tonic-gate nodev, /* segmap */
20810Sstevel@tonic-gate nochpoll, /* poll */
20820Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */
20830Sstevel@tonic-gate 0, /* streamtab */
20840Sstevel@tonic-gate D_NEW | D_MP /* Driver compatibility flag */
20850Sstevel@tonic-gate };
20860Sstevel@tonic-gate
20870Sstevel@tonic-gate /*ARGSUSED*/
20880Sstevel@tonic-gate static int
fasttrap_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)20890Sstevel@tonic-gate fasttrap_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
20900Sstevel@tonic-gate {
20910Sstevel@tonic-gate int error;
20920Sstevel@tonic-gate
20930Sstevel@tonic-gate switch (infocmd) {
20940Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO:
20950Sstevel@tonic-gate *result = (void *)fasttrap_devi;
20960Sstevel@tonic-gate error = DDI_SUCCESS;
20970Sstevel@tonic-gate break;
20980Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE:
20990Sstevel@tonic-gate *result = (void *)0;
21000Sstevel@tonic-gate error = DDI_SUCCESS;
21010Sstevel@tonic-gate break;
21020Sstevel@tonic-gate default:
21030Sstevel@tonic-gate error = DDI_FAILURE;
21040Sstevel@tonic-gate }
21050Sstevel@tonic-gate return (error);
21060Sstevel@tonic-gate }
21070Sstevel@tonic-gate
21080Sstevel@tonic-gate static int
fasttrap_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)21090Sstevel@tonic-gate fasttrap_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
21100Sstevel@tonic-gate {
21110Sstevel@tonic-gate ulong_t nent;
21120Sstevel@tonic-gate
21130Sstevel@tonic-gate switch (cmd) {
21140Sstevel@tonic-gate case DDI_ATTACH:
21150Sstevel@tonic-gate break;
21160Sstevel@tonic-gate case DDI_RESUME:
21170Sstevel@tonic-gate return (DDI_SUCCESS);
21180Sstevel@tonic-gate default:
21190Sstevel@tonic-gate return (DDI_FAILURE);
21200Sstevel@tonic-gate }
21210Sstevel@tonic-gate
21220Sstevel@tonic-gate if (ddi_create_minor_node(devi, "fasttrap", S_IFCHR, 0,
21232179Sahl DDI_PSEUDO, NULL) == DDI_FAILURE) {
21240Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL);
21250Sstevel@tonic-gate return (DDI_FAILURE);
21260Sstevel@tonic-gate }
21270Sstevel@tonic-gate
21280Sstevel@tonic-gate ddi_report_dev(devi);
21290Sstevel@tonic-gate fasttrap_devi = devi;
21300Sstevel@tonic-gate
21310Sstevel@tonic-gate /*
21320Sstevel@tonic-gate * Install our hooks into fork(2), exec(2), and exit(2).
21330Sstevel@tonic-gate */
21340Sstevel@tonic-gate dtrace_fasttrap_fork_ptr = &fasttrap_fork;
21350Sstevel@tonic-gate dtrace_fasttrap_exit_ptr = &fasttrap_exec_exit;
21360Sstevel@tonic-gate dtrace_fasttrap_exec_ptr = &fasttrap_exec_exit;
21370Sstevel@tonic-gate
21380Sstevel@tonic-gate fasttrap_max = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
21390Sstevel@tonic-gate "fasttrap-max-probes", FASTTRAP_MAX_DEFAULT);
21400Sstevel@tonic-gate fasttrap_total = 0;
21410Sstevel@tonic-gate
21420Sstevel@tonic-gate /*
21430Sstevel@tonic-gate * Conjure up the tracepoints hashtable...
21440Sstevel@tonic-gate */
21450Sstevel@tonic-gate nent = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
21460Sstevel@tonic-gate "fasttrap-hash-size", FASTTRAP_TPOINTS_DEFAULT_SIZE);
21470Sstevel@tonic-gate
21483944Sahl if (nent == 0 || nent > 0x1000000)
21490Sstevel@tonic-gate nent = FASTTRAP_TPOINTS_DEFAULT_SIZE;
21500Sstevel@tonic-gate
21510Sstevel@tonic-gate if ((nent & (nent - 1)) == 0)
21520Sstevel@tonic-gate fasttrap_tpoints.fth_nent = nent;
21530Sstevel@tonic-gate else
21540Sstevel@tonic-gate fasttrap_tpoints.fth_nent = 1 << fasttrap_highbit(nent);
21550Sstevel@tonic-gate ASSERT(fasttrap_tpoints.fth_nent > 0);
21560Sstevel@tonic-gate fasttrap_tpoints.fth_mask = fasttrap_tpoints.fth_nent - 1;
21570Sstevel@tonic-gate fasttrap_tpoints.fth_table = kmem_zalloc(fasttrap_tpoints.fth_nent *
21580Sstevel@tonic-gate sizeof (fasttrap_bucket_t), KM_SLEEP);
21590Sstevel@tonic-gate
21600Sstevel@tonic-gate /*
2161532Sahl * ... and the providers hash table...
21620Sstevel@tonic-gate */
21630Sstevel@tonic-gate nent = FASTTRAP_PROVIDERS_DEFAULT_SIZE;
21640Sstevel@tonic-gate if ((nent & (nent - 1)) == 0)
21650Sstevel@tonic-gate fasttrap_provs.fth_nent = nent;
21660Sstevel@tonic-gate else
21670Sstevel@tonic-gate fasttrap_provs.fth_nent = 1 << fasttrap_highbit(nent);
21680Sstevel@tonic-gate ASSERT(fasttrap_provs.fth_nent > 0);
21690Sstevel@tonic-gate fasttrap_provs.fth_mask = fasttrap_provs.fth_nent - 1;
2170532Sahl fasttrap_provs.fth_table = kmem_zalloc(fasttrap_provs.fth_nent *
2171532Sahl sizeof (fasttrap_bucket_t), KM_SLEEP);
21720Sstevel@tonic-gate
2173532Sahl /*
2174532Sahl * ... and the procs hash table.
2175532Sahl */
2176532Sahl nent = FASTTRAP_PROCS_DEFAULT_SIZE;
2177532Sahl if ((nent & (nent - 1)) == 0)
2178532Sahl fasttrap_procs.fth_nent = nent;
2179532Sahl else
2180532Sahl fasttrap_procs.fth_nent = 1 << fasttrap_highbit(nent);
2181532Sahl ASSERT(fasttrap_procs.fth_nent > 0);
2182532Sahl fasttrap_procs.fth_mask = fasttrap_procs.fth_nent - 1;
2183532Sahl fasttrap_procs.fth_table = kmem_zalloc(fasttrap_procs.fth_nent *
21840Sstevel@tonic-gate sizeof (fasttrap_bucket_t), KM_SLEEP);
21850Sstevel@tonic-gate
21860Sstevel@tonic-gate (void) dtrace_meta_register("fasttrap", &fasttrap_mops, NULL,
21870Sstevel@tonic-gate &fasttrap_meta_id);
21880Sstevel@tonic-gate
21890Sstevel@tonic-gate return (DDI_SUCCESS);
21900Sstevel@tonic-gate }
21910Sstevel@tonic-gate
21920Sstevel@tonic-gate static int
fasttrap_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)21930Sstevel@tonic-gate fasttrap_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
21940Sstevel@tonic-gate {
21950Sstevel@tonic-gate int i, fail = 0;
21960Sstevel@tonic-gate timeout_id_t tmp;
21970Sstevel@tonic-gate
21980Sstevel@tonic-gate switch (cmd) {
21990Sstevel@tonic-gate case DDI_DETACH:
22000Sstevel@tonic-gate break;
22010Sstevel@tonic-gate case DDI_SUSPEND:
22020Sstevel@tonic-gate return (DDI_SUCCESS);
22030Sstevel@tonic-gate default:
22040Sstevel@tonic-gate return (DDI_FAILURE);
22050Sstevel@tonic-gate }
22060Sstevel@tonic-gate
22070Sstevel@tonic-gate /*
22080Sstevel@tonic-gate * Unregister the meta-provider to make sure no new fasttrap-
22090Sstevel@tonic-gate * managed providers come along while we're trying to close up
22100Sstevel@tonic-gate * shop. If we fail to detach, we'll need to re-register as a
22110Sstevel@tonic-gate * meta-provider. We can fail to unregister as a meta-provider
22120Sstevel@tonic-gate * if providers we manage still exist.
22130Sstevel@tonic-gate */
22140Sstevel@tonic-gate if (fasttrap_meta_id != DTRACE_METAPROVNONE &&
22150Sstevel@tonic-gate dtrace_meta_unregister(fasttrap_meta_id) != 0)
22160Sstevel@tonic-gate return (DDI_FAILURE);
22170Sstevel@tonic-gate
22180Sstevel@tonic-gate /*
22190Sstevel@tonic-gate * Prevent any new timeouts from running by setting fasttrap_timeout
22200Sstevel@tonic-gate * to a non-zero value, and wait for the current timeout to complete.
22210Sstevel@tonic-gate */
22220Sstevel@tonic-gate mutex_enter(&fasttrap_cleanup_mtx);
22230Sstevel@tonic-gate fasttrap_cleanup_work = 0;
22240Sstevel@tonic-gate
22250Sstevel@tonic-gate while (fasttrap_timeout != (timeout_id_t)1) {
22260Sstevel@tonic-gate tmp = fasttrap_timeout;
22270Sstevel@tonic-gate fasttrap_timeout = (timeout_id_t)1;
22280Sstevel@tonic-gate
22290Sstevel@tonic-gate if (tmp != 0) {
22300Sstevel@tonic-gate mutex_exit(&fasttrap_cleanup_mtx);
22310Sstevel@tonic-gate (void) untimeout(tmp);
22320Sstevel@tonic-gate mutex_enter(&fasttrap_cleanup_mtx);
22330Sstevel@tonic-gate }
22340Sstevel@tonic-gate }
22350Sstevel@tonic-gate
22360Sstevel@tonic-gate fasttrap_cleanup_work = 0;
22370Sstevel@tonic-gate mutex_exit(&fasttrap_cleanup_mtx);
22380Sstevel@tonic-gate
22390Sstevel@tonic-gate /*
22400Sstevel@tonic-gate * Iterate over all of our providers. If there's still a process
22410Sstevel@tonic-gate * that corresponds to that pid, fail to detach.
22420Sstevel@tonic-gate */
22430Sstevel@tonic-gate for (i = 0; i < fasttrap_provs.fth_nent; i++) {
22440Sstevel@tonic-gate fasttrap_provider_t **fpp, *fp;
22450Sstevel@tonic-gate fasttrap_bucket_t *bucket = &fasttrap_provs.fth_table[i];
22460Sstevel@tonic-gate
22470Sstevel@tonic-gate mutex_enter(&bucket->ftb_mtx);
22480Sstevel@tonic-gate fpp = (fasttrap_provider_t **)&bucket->ftb_data;
22490Sstevel@tonic-gate while ((fp = *fpp) != NULL) {
22500Sstevel@tonic-gate /*
22510Sstevel@tonic-gate * Acquire and release the lock as a simple way of
22520Sstevel@tonic-gate * waiting for any other consumer to finish with
22530Sstevel@tonic-gate * this provider. A thread must first acquire the
22540Sstevel@tonic-gate * bucket lock so there's no chance of another thread
2255935Sahl * blocking on the provider's lock.
22560Sstevel@tonic-gate */
22570Sstevel@tonic-gate mutex_enter(&fp->ftp_mtx);
22580Sstevel@tonic-gate mutex_exit(&fp->ftp_mtx);
22590Sstevel@tonic-gate
22600Sstevel@tonic-gate if (dtrace_unregister(fp->ftp_provid) != 0) {
22610Sstevel@tonic-gate fail = 1;
22620Sstevel@tonic-gate fpp = &fp->ftp_next;
22630Sstevel@tonic-gate } else {
22640Sstevel@tonic-gate *fpp = fp->ftp_next;
22650Sstevel@tonic-gate fasttrap_provider_free(fp);
22660Sstevel@tonic-gate }
22670Sstevel@tonic-gate }
22680Sstevel@tonic-gate
22690Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx);
22700Sstevel@tonic-gate }
22710Sstevel@tonic-gate
22722179Sahl if (fail) {
22730Sstevel@tonic-gate uint_t work;
22740Sstevel@tonic-gate /*
22750Sstevel@tonic-gate * If we're failing to detach, we need to unblock timeouts
22760Sstevel@tonic-gate * and start a new timeout if any work has accumulated while
22770Sstevel@tonic-gate * we've been unsuccessfully trying to detach.
22780Sstevel@tonic-gate */
22790Sstevel@tonic-gate mutex_enter(&fasttrap_cleanup_mtx);
22800Sstevel@tonic-gate fasttrap_timeout = 0;
22810Sstevel@tonic-gate work = fasttrap_cleanup_work;
22820Sstevel@tonic-gate mutex_exit(&fasttrap_cleanup_mtx);
22830Sstevel@tonic-gate
22840Sstevel@tonic-gate if (work)
22850Sstevel@tonic-gate fasttrap_pid_cleanup();
22860Sstevel@tonic-gate
22870Sstevel@tonic-gate (void) dtrace_meta_register("fasttrap", &fasttrap_mops, NULL,
22880Sstevel@tonic-gate &fasttrap_meta_id);
22890Sstevel@tonic-gate
22900Sstevel@tonic-gate return (DDI_FAILURE);
22910Sstevel@tonic-gate }
22920Sstevel@tonic-gate
22930Sstevel@tonic-gate #ifdef DEBUG
22940Sstevel@tonic-gate mutex_enter(&fasttrap_count_mtx);
22952179Sahl ASSERT(fasttrap_pid_count == 0);
22960Sstevel@tonic-gate mutex_exit(&fasttrap_count_mtx);
22970Sstevel@tonic-gate #endif
22980Sstevel@tonic-gate
22990Sstevel@tonic-gate kmem_free(fasttrap_tpoints.fth_table,
23000Sstevel@tonic-gate fasttrap_tpoints.fth_nent * sizeof (fasttrap_bucket_t));
23010Sstevel@tonic-gate fasttrap_tpoints.fth_nent = 0;
23020Sstevel@tonic-gate
23030Sstevel@tonic-gate kmem_free(fasttrap_provs.fth_table,
23040Sstevel@tonic-gate fasttrap_provs.fth_nent * sizeof (fasttrap_bucket_t));
23050Sstevel@tonic-gate fasttrap_provs.fth_nent = 0;
23060Sstevel@tonic-gate
2307532Sahl kmem_free(fasttrap_procs.fth_table,
2308532Sahl fasttrap_procs.fth_nent * sizeof (fasttrap_bucket_t));
2309532Sahl fasttrap_procs.fth_nent = 0;
2310532Sahl
23110Sstevel@tonic-gate /*
23120Sstevel@tonic-gate * We know there are no tracepoints in any process anywhere in
23130Sstevel@tonic-gate * the system so there is no process which has its p_dtrace_count
23140Sstevel@tonic-gate * greater than zero, therefore we know that no thread can actively
23150Sstevel@tonic-gate * be executing code in fasttrap_fork(). Similarly for p_dtrace_probes
23160Sstevel@tonic-gate * and fasttrap_exec() and fasttrap_exit().
23170Sstevel@tonic-gate */
23180Sstevel@tonic-gate ASSERT(dtrace_fasttrap_fork_ptr == &fasttrap_fork);
23190Sstevel@tonic-gate dtrace_fasttrap_fork_ptr = NULL;
23200Sstevel@tonic-gate
23210Sstevel@tonic-gate ASSERT(dtrace_fasttrap_exec_ptr == &fasttrap_exec_exit);
23220Sstevel@tonic-gate dtrace_fasttrap_exec_ptr = NULL;
23230Sstevel@tonic-gate
23240Sstevel@tonic-gate ASSERT(dtrace_fasttrap_exit_ptr == &fasttrap_exec_exit);
23250Sstevel@tonic-gate dtrace_fasttrap_exit_ptr = NULL;
23260Sstevel@tonic-gate
23270Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL);
23280Sstevel@tonic-gate
23290Sstevel@tonic-gate return (DDI_SUCCESS);
23300Sstevel@tonic-gate }
23310Sstevel@tonic-gate
23320Sstevel@tonic-gate static struct dev_ops fasttrap_ops = {
23330Sstevel@tonic-gate DEVO_REV, /* devo_rev */
23340Sstevel@tonic-gate 0, /* refcnt */
23350Sstevel@tonic-gate fasttrap_info, /* get_dev_info */
23360Sstevel@tonic-gate nulldev, /* identify */
23370Sstevel@tonic-gate nulldev, /* probe */
23380Sstevel@tonic-gate fasttrap_attach, /* attach */
23390Sstevel@tonic-gate fasttrap_detach, /* detach */
23400Sstevel@tonic-gate nodev, /* reset */
23410Sstevel@tonic-gate &fasttrap_cb_ops, /* driver operations */
23420Sstevel@tonic-gate NULL, /* bus operations */
23437656SSherry.Moore@Sun.COM nodev, /* dev power */
23447656SSherry.Moore@Sun.COM ddi_quiesce_not_needed, /* quiesce */
23450Sstevel@tonic-gate };
23460Sstevel@tonic-gate
23470Sstevel@tonic-gate /*
23480Sstevel@tonic-gate * Module linkage information for the kernel.
23490Sstevel@tonic-gate */
23500Sstevel@tonic-gate static struct modldrv modldrv = {
23510Sstevel@tonic-gate &mod_driverops, /* module type (this is a pseudo driver) */
23520Sstevel@tonic-gate "Fasttrap Tracing", /* name of module */
23530Sstevel@tonic-gate &fasttrap_ops, /* driver ops */
23540Sstevel@tonic-gate };
23550Sstevel@tonic-gate
23560Sstevel@tonic-gate static struct modlinkage modlinkage = {
23570Sstevel@tonic-gate MODREV_1,
23580Sstevel@tonic-gate (void *)&modldrv,
23590Sstevel@tonic-gate NULL
23600Sstevel@tonic-gate };
23610Sstevel@tonic-gate
23620Sstevel@tonic-gate int
_init(void)23630Sstevel@tonic-gate _init(void)
23640Sstevel@tonic-gate {
23650Sstevel@tonic-gate return (mod_install(&modlinkage));
23660Sstevel@tonic-gate }
23670Sstevel@tonic-gate
23680Sstevel@tonic-gate int
_info(struct modinfo * modinfop)23690Sstevel@tonic-gate _info(struct modinfo *modinfop)
23700Sstevel@tonic-gate {
23710Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
23720Sstevel@tonic-gate }
23730Sstevel@tonic-gate
23740Sstevel@tonic-gate int
_fini(void)23750Sstevel@tonic-gate _fini(void)
23760Sstevel@tonic-gate {
23770Sstevel@tonic-gate return (mod_remove(&modlinkage));
23780Sstevel@tonic-gate }
2379