4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
21 * Portions Copyright 2010 The FreeBSD Foundation
27 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
32 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
35 #include <sys/atomic.h>
36 #include <sys/errno.h>
38 #include <sys/modctl.h>
40 #include <sys/systm.h>
44 #include <sys/sunddi.h>
45 #include <sys/cpuvar.h>
48 #include <sys/strsubr.h>
50 #include <sys/fasttrap.h>
51 #include <sys/fasttrap_impl.h>
52 #include <sys/fasttrap_isa.h>
53 #include <sys/dtrace.h>
54 #include <sys/dtrace_impl.h>
55 #include <sys/sysmacros.h>
57 #include <sys/policy.h>
59 #include <util/qsort.h>
61 #include <sys/mutex.h>
62 #include <sys/kernel.h>
64 #include <sys/dtrace_bsd.h>
65 #include <sys/eventhandler.h>
69 #include <vm/vm_map.h>
70 #include <vm/vm_param.h>
71 #include <sys/u8_textprep.h>
72 #include <cddl/dev/dtrace/dtrace_cddl.h>
76 * User-Land Trap-Based Tracing
77 * ----------------------------
79 * The fasttrap provider allows DTrace consumers to instrument any user-level
80 * instruction to gather data; this includes probes with semantic
81 * signifigance like entry and return as well as simple offsets into the
82 * function. While the specific techniques used are very ISA specific, the
83 * methodology is generalizable to any architecture.
86 * The General Methodology
87 * -----------------------
89 * With the primary goal of tracing every user-land instruction and the
90 * limitation that we can't trust user space so don't want to rely on much
91 * information there, we begin by replacing the instructions we want to trace
92 * with trap instructions. Each instruction we overwrite is saved into a hash
93 * table keyed by process ID and pc address. When we enter the kernel due to
94 * this trap instruction, we need the effects of the replaced instruction to
95 * appear to have occurred before we proceed with the user thread's
98 * Each user level thread is represented by a ulwp_t structure which is
99 * always easily accessible through a register. The most basic way to produce
100 * the effects of the instruction we replaced is to copy that instruction out
101 * to a bit of scratch space reserved in the user thread's ulwp_t structure
102 * (a sort of kernel-private thread local storage), set the PC to that
103 * scratch space and single step. When we reenter the kernel after single
104 * stepping the instruction we must then adjust the PC to point to what would
105 * normally be the next instruction. Of course, special care must be taken
106 * for branches and jumps, but these represent such a small fraction of any
107 * instruction set that writing the code to emulate these in the kernel is
110 * Return probes may require several tracepoints to trace every return site,
111 * and, conversely, each tracepoint may activate several probes (the entry
112 * and offset 0 probes, for example). To solve this muliplexing problem,
113 * tracepoints contain lists of probes to activate and probes contain lists
114 * of tracepoints to enable. If a probe is activated, it adds its ID to
115 * existing tracepoints or creates new ones as necessary.
117 * Most probes are activated _before_ the instruction is executed, but return
118 * probes are activated _after_ the effects of the last instruction of the
119 * function are visible. Return probes must be fired _after_ we have
120 * single-stepped the instruction whereas all other probes are fired
127 * The lock ordering below -- both internally and with respect to the DTrace
128 * framework -- is a little tricky and bears some explanation. Each provider
129 * has a lock (ftp_mtx) that protects its members including reference counts
130 * for enabled probes (ftp_rcount), consumers actively creating probes
131 * (ftp_ccount) and USDT consumers (ftp_mcount); all three prevent a provider
132 * from being freed. A provider is looked up by taking the bucket lock for the
133 * provider hash table, and is returned with its lock held. The provider lock
134 * may be taken in functions invoked by the DTrace framework, but may not be
135 * held while calling functions in the DTrace framework.
137 * To ensure consistency over multiple calls to the DTrace framework, the
138 * creation lock (ftp_cmtx) should be held. Naturally, the creation lock may
139 * not be taken when holding the provider lock as that would create a cyclic
140 * lock ordering. In situations where one would naturally take the provider
141 * lock and then the creation lock, we instead up a reference count to prevent
142 * the provider from disappearing, drop the provider lock, and acquire the
146 * bucket lock before provider lock
147 * DTrace before provider lock
148 * creation lock before DTrace
149 * never hold the provider lock and creation lock simultaneously
152 static d_open_t fasttrap_open;
153 static d_ioctl_t fasttrap_ioctl;
155 static struct cdevsw fasttrap_cdevsw = {
156 .d_version = D_VERSION,
157 .d_open = fasttrap_open,
158 .d_ioctl = fasttrap_ioctl,
159 .d_name = "fasttrap",
161 static struct cdev *fasttrap_cdev;
162 static dtrace_meta_provider_id_t fasttrap_meta_id;
164 static struct proc *fasttrap_cleanup_proc;
165 static struct mtx fasttrap_cleanup_mtx;
166 static uint_t fasttrap_cleanup_work, fasttrap_cleanup_drain, fasttrap_cleanup_cv;
169 * Generation count on modifications to the global tracepoint lookup table.
171 static volatile uint64_t fasttrap_mod_gen;
174 * When the fasttrap provider is loaded, fasttrap_max is set to either
175 * FASTTRAP_MAX_DEFAULT or the value for fasttrap-max-probes in the
176 * fasttrap.conf file. Each time a probe is created, fasttrap_total is
177 * incremented by the number of tracepoints that may be associated with that
178 * probe; fasttrap_total is capped at fasttrap_max.
180 #define FASTTRAP_MAX_DEFAULT 250000
181 static uint32_t fasttrap_max;
182 static uint32_t fasttrap_total;
185 * Copyright (c) 2011, Joyent, Inc. All rights reserved.
188 #define FASTTRAP_TPOINTS_DEFAULT_SIZE 0x4000
189 #define FASTTRAP_PROVIDERS_DEFAULT_SIZE 0x100
190 #define FASTTRAP_PROCS_DEFAULT_SIZE 0x100
192 #define FASTTRAP_PID_NAME "pid"
194 fasttrap_hash_t fasttrap_tpoints;
195 static fasttrap_hash_t fasttrap_provs;
196 static fasttrap_hash_t fasttrap_procs;
198 static uint64_t fasttrap_pid_count; /* pid ref count */
199 static kmutex_t fasttrap_count_mtx; /* lock on ref count */
201 #define FASTTRAP_ENABLE_FAIL 1
202 #define FASTTRAP_ENABLE_PARTIAL 2
204 static int fasttrap_tracepoint_enable(proc_t *, fasttrap_probe_t *, uint_t);
205 static void fasttrap_tracepoint_disable(proc_t *, fasttrap_probe_t *, uint_t);
207 static fasttrap_provider_t *fasttrap_provider_lookup(pid_t, const char *,
208 const dtrace_pattr_t *);
209 static void fasttrap_provider_retire(pid_t, const char *, int);
210 static void fasttrap_provider_free(fasttrap_provider_t *);
212 static fasttrap_proc_t *fasttrap_proc_lookup(pid_t);
213 static void fasttrap_proc_release(fasttrap_proc_t *);
216 static void fasttrap_thread_dtor(void *, struct thread *);
219 #define FASTTRAP_PROVS_INDEX(pid, name) \
220 ((fasttrap_hash_str(name) + (pid)) & fasttrap_provs.fth_mask)
222 #define FASTTRAP_PROCS_INDEX(pid) ((pid) & fasttrap_procs.fth_mask)
225 static kmutex_t fasttrap_cpuc_pid_lock[MAXCPU];
226 static eventhandler_tag fasttrap_thread_dtor_tag;
230 fasttrap_highbit(ulong_t i)
237 if (i & 0xffffffff00000000ul) {
241 if (i & 0xffff0000) {
260 fasttrap_hash_str(const char *p)
266 hval = (hval << 4) + *p++;
267 if ((g = (hval & 0xf0000000)) != 0)
275 fasttrap_sigtrap(proc_t *p, kthread_t *t, uintptr_t pc)
278 sigqueue_t *sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
280 sqp->sq_info.si_signo = SIGTRAP;
281 sqp->sq_info.si_code = TRAP_DTRACE;
282 sqp->sq_info.si_addr = (caddr_t)pc;
284 mutex_enter(&p->p_lock);
286 mutex_exit(&p->p_lock);
291 ksiginfo_t *ksi = kmem_zalloc(sizeof (ksiginfo_t), KM_SLEEP);
294 ksi->ksi_signo = SIGTRAP;
295 ksi->ksi_code = TRAP_DTRACE;
296 ksi->ksi_addr = (caddr_t)pc;
298 (void) tdksignal(t, SIGTRAP, ksi);
305 * Obtain a chunk of scratch space in the address space of the target process.
307 fasttrap_scrspace_t *
308 fasttrap_scraddr(struct thread *td, fasttrap_proc_t *fprc)
310 fasttrap_scrblock_t *scrblk;
311 fasttrap_scrspace_t *scrspc;
317 if (td->t_dtrace_sscr != NULL) {
318 /* If the thread already has scratch space, we're done. */
319 scrspc = (fasttrap_scrspace_t *)td->t_dtrace_sscr;
325 mutex_enter(&fprc->ftpc_mtx);
326 if (LIST_EMPTY(&fprc->ftpc_fscr)) {
328 * No scratch space is available, so we'll map a new scratch
329 * space block into the traced process' address space.
332 error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0, &addr,
333 FASTTRAP_SCRBLOCK_SIZE, 0, VMFS_ANY_SPACE, VM_PROT_ALL,
335 if (error != KERN_SUCCESS)
338 scrblk = malloc(sizeof(*scrblk), M_SOLARIS, M_WAITOK);
339 scrblk->ftsb_addr = addr;
340 LIST_INSERT_HEAD(&fprc->ftpc_scrblks, scrblk, ftsb_next);
343 * Carve the block up into chunks and put them on the free list.
346 i < FASTTRAP_SCRBLOCK_SIZE / FASTTRAP_SCRSPACE_SIZE; i++) {
347 scrspc = malloc(sizeof(*scrspc), M_SOLARIS, M_WAITOK);
348 scrspc->ftss_addr = addr +
349 i * FASTTRAP_SCRSPACE_SIZE;
350 LIST_INSERT_HEAD(&fprc->ftpc_fscr, scrspc,
356 * Take the first scratch chunk off the free list, put it on the
357 * allocated list, and return its address.
359 scrspc = LIST_FIRST(&fprc->ftpc_fscr);
360 LIST_REMOVE(scrspc, ftss_next);
361 LIST_INSERT_HEAD(&fprc->ftpc_ascr, scrspc, ftss_next);
364 * This scratch space is reserved for use by td until the thread exits.
366 td->t_dtrace_sscr = scrspc;
369 mutex_exit(&fprc->ftpc_mtx);
375 * Return any allocated per-thread scratch space chunks back to the process'
379 fasttrap_thread_dtor(void *arg __unused, struct thread *td)
381 fasttrap_bucket_t *bucket;
382 fasttrap_proc_t *fprc;
383 fasttrap_scrspace_t *scrspc;
386 if (td->t_dtrace_sscr == NULL)
389 pid = td->td_proc->p_pid;
390 bucket = &fasttrap_procs.fth_table[FASTTRAP_PROCS_INDEX(pid)];
393 /* Look up the fasttrap process handle for this process. */
394 mutex_enter(&bucket->ftb_mtx);
395 for (fprc = bucket->ftb_data; fprc != NULL; fprc = fprc->ftpc_next) {
396 if (fprc->ftpc_pid == pid) {
397 mutex_enter(&fprc->ftpc_mtx);
398 mutex_exit(&bucket->ftb_mtx);
403 mutex_exit(&bucket->ftb_mtx);
407 scrspc = (fasttrap_scrspace_t *)td->t_dtrace_sscr;
408 LIST_REMOVE(scrspc, ftss_next);
409 LIST_INSERT_HEAD(&fprc->ftpc_fscr, scrspc, ftss_next);
411 mutex_exit(&fprc->ftpc_mtx);
416 * This function ensures that no threads are actively using the memory
417 * associated with probes that were formerly live.
420 fasttrap_mod_barrier(uint64_t gen)
424 if (gen < fasttrap_mod_gen)
430 mutex_enter(&fasttrap_cpuc_pid_lock[i]);
431 mutex_exit(&fasttrap_cpuc_pid_lock[i]);
436 * This function performs asynchronous cleanup of fasttrap providers. The
437 * Solaris implementation of this mechanism use a timeout that's activated in
438 * fasttrap_pid_cleanup(), but this doesn't work in FreeBSD: one may sleep while
439 * holding the DTrace mutexes, but it is unsafe to sleep in a callout handler.
440 * Thus we use a dedicated process to perform the cleanup when requested.
444 fasttrap_pid_cleanup_cb(void *data)
446 fasttrap_provider_t **fpp, *fp;
447 fasttrap_bucket_t *bucket;
448 dtrace_provider_id_t provid;
449 int i, later = 0, rval;
451 mtx_lock(&fasttrap_cleanup_mtx);
452 while (!fasttrap_cleanup_drain || later > 0) {
453 fasttrap_cleanup_work = 0;
454 mtx_unlock(&fasttrap_cleanup_mtx);
459 * Iterate over all the providers trying to remove the marked
460 * ones. If a provider is marked but not retired, we just
461 * have to take a crack at removing it -- it's no big deal if
464 for (i = 0; i < fasttrap_provs.fth_nent; i++) {
465 bucket = &fasttrap_provs.fth_table[i];
466 mutex_enter(&bucket->ftb_mtx);
467 fpp = (fasttrap_provider_t **)&bucket->ftb_data;
469 while ((fp = *fpp) != NULL) {
470 if (!fp->ftp_marked) {
475 mutex_enter(&fp->ftp_mtx);
478 * If this provider has consumers actively
479 * creating probes (ftp_ccount) or is a USDT
480 * provider (ftp_mcount), we can't unregister
483 if (fp->ftp_ccount != 0 ||
484 fp->ftp_mcount != 0) {
485 mutex_exit(&fp->ftp_mtx);
490 if (!fp->ftp_retired || fp->ftp_rcount != 0)
493 mutex_exit(&fp->ftp_mtx);
496 * If we successfully unregister this
497 * provider we can remove it from the hash
498 * chain and free the memory. If our attempt
499 * to unregister fails and this is a retired
500 * provider, increment our flag to try again
501 * pretty soon. If we've consumed more than
502 * half of our total permitted number of
503 * probes call dtrace_condense() to try to
504 * clean out the unenabled probes.
506 provid = fp->ftp_provid;
507 if ((rval = dtrace_unregister(provid)) != 0) {
508 if (fasttrap_total > fasttrap_max / 2)
509 (void) dtrace_condense(provid);
514 later += fp->ftp_marked;
518 fasttrap_provider_free(fp);
521 mutex_exit(&bucket->ftb_mtx);
523 mtx_lock(&fasttrap_cleanup_mtx);
526 * If we were unable to retire a provider, try again after a
527 * second. This situation can occur in certain circumstances
528 * where providers cannot be unregistered even though they have
529 * no probes enabled because of an execution of dtrace -l or
532 if (later > 0 || fasttrap_cleanup_work ||
533 fasttrap_cleanup_drain) {
534 mtx_unlock(&fasttrap_cleanup_mtx);
535 pause("ftclean", hz);
536 mtx_lock(&fasttrap_cleanup_mtx);
538 mtx_sleep(&fasttrap_cleanup_cv, &fasttrap_cleanup_mtx,
543 * Wake up the thread in fasttrap_unload() now that we're done.
545 wakeup(&fasttrap_cleanup_drain);
546 mtx_unlock(&fasttrap_cleanup_mtx);
552 * Activates the asynchronous cleanup mechanism.
555 fasttrap_pid_cleanup(void)
558 mtx_lock(&fasttrap_cleanup_mtx);
559 if (!fasttrap_cleanup_work) {
560 fasttrap_cleanup_work = 1;
561 wakeup(&fasttrap_cleanup_cv);
563 mtx_unlock(&fasttrap_cleanup_mtx);
567 * This is called from cfork() via dtrace_fasttrap_fork(). The child
568 * process's address space is (roughly) a copy of the parent process's so
569 * we have to remove all the instrumentation we had previously enabled in the
573 fasttrap_fork(proc_t *p, proc_t *cp)
576 fasttrap_scrblock_t *scrblk;
577 fasttrap_proc_t *fprc = NULL;
579 pid_t ppid = p->p_pid;
583 ASSERT(curproc == p);
584 ASSERT(p->p_proc_flag & P_PR_LOCK);
586 PROC_LOCK_ASSERT(p, MA_OWNED);
589 ASSERT(p->p_dtrace_count > 0);
591 if (p->p_dtrace_helpers) {
593 * dtrace_helpers_duplicate() allocates memory.
598 dtrace_helpers_duplicate(p, cp);
604 * This check is purposely here instead of in kern_fork.c because,
605 * for legal resons, we cannot include the dtrace_cddl.h header
606 * inside kern_fork.c and insert if-clause there.
608 if (p->p_dtrace_count == 0)
611 ASSERT(cp->p_dtrace_count == 0);
614 * This would be simpler and faster if we maintained per-process
615 * hash tables of enabled tracepoints. It could, however, potentially
616 * slow down execution of a tracepoint since we'd need to go
617 * through two levels of indirection. In the future, we should
618 * consider either maintaining per-process ancillary lists of
619 * enabled tracepoints or hanging a pointer to a per-process hash
620 * table of enabled tracepoints off the proc structure.
624 * We don't have to worry about the child process disappearing
625 * because we're in fork().
628 mtx_lock_spin(&cp->p_slock);
630 mtx_unlock_spin(&cp->p_slock);
633 * fasttrap_tracepoint_remove() expects the child process to be
634 * unlocked and the VM then expects curproc to be unlocked.
642 * Iterate over every tracepoint looking for ones that belong to the
643 * parent process, and remove each from the child process.
645 for (i = 0; i < fasttrap_tpoints.fth_nent; i++) {
646 fasttrap_tracepoint_t *tp;
647 fasttrap_bucket_t *bucket = &fasttrap_tpoints.fth_table[i];
649 mutex_enter(&bucket->ftb_mtx);
650 for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
651 if (tp->ftt_pid == ppid &&
652 tp->ftt_proc->ftpc_acount != 0) {
653 int ret = fasttrap_tracepoint_remove(cp, tp);
657 * The count of active providers can only be
658 * decremented (i.e. to zero) during exec,
659 * exit, and removal of a meta provider so it
660 * should be impossible to drop the count
663 ASSERT(tp->ftt_proc->ftpc_acount != 0);
669 mutex_exit(&bucket->ftb_mtx);
673 * Unmap any scratch space inherited from the parent's address
677 mutex_enter(&fprc->ftpc_mtx);
678 LIST_FOREACH(scrblk, &fprc->ftpc_scrblks, ftsb_next) {
679 vm_map_remove(&cp->p_vmspace->vm_map,
681 scrblk->ftsb_addr + FASTTRAP_SCRBLOCK_SIZE);
683 mutex_exit(&fprc->ftpc_mtx);
689 mutex_enter(&cp->p_lock);
699 * This is called from proc_exit() or from exec_common() if p_dtrace_probes
700 * is set on the proc structure to indicate that there is a pid provider
701 * associated with this process.
704 fasttrap_exec_exit(proc_t *p)
711 ASSERT(p == curproc);
713 PROC_LOCK_ASSERT(p, MA_OWNED);
716 * Since struct threads may be recycled, we cannot rely on t_dtrace_sscr
717 * fields to be zeroed by kdtrace_thread_ctor. Thus we must zero it
718 * ourselves when a process exits.
720 FOREACH_THREAD_IN_PROC(p, td)
721 td->t_dtrace_sscr = NULL;
726 * We clean up the pid provider for this process here; user-land
727 * static probes are handled by the meta-provider remove entry point.
729 fasttrap_provider_retire(p->p_pid, FASTTRAP_PID_NAME, 0);
731 if (p->p_dtrace_helpers)
732 dtrace_helpers_destroy(p);
741 fasttrap_pid_provide(void *arg, dtrace_probedesc_t *desc)
744 * There are no "default" pid probes.
749 fasttrap_tracepoint_enable(proc_t *p, fasttrap_probe_t *probe, uint_t index)
751 fasttrap_tracepoint_t *tp, *new_tp = NULL;
752 fasttrap_bucket_t *bucket;
757 ASSERT(index < probe->ftp_ntps);
759 pid = probe->ftp_pid;
760 pc = probe->ftp_tps[index].fit_tp->ftt_pc;
761 id = &probe->ftp_tps[index].fit_id;
763 ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid);
766 ASSERT(!(p->p_flag & SVFORK));
770 * Before we make any modifications, make sure we've imposed a barrier
771 * on the generation in which this probe was last modified.
773 fasttrap_mod_barrier(probe->ftp_gen);
775 bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
778 * If the tracepoint has already been enabled, just add our id to the
779 * list of interested probes. This may be our second time through
780 * this path in which case we'll have constructed the tracepoint we'd
781 * like to install. If we can't find a match, and have an allocated
782 * tracepoint ready to go, enable that one now.
784 * A tracepoint whose process is defunct is also considered defunct.
787 mutex_enter(&bucket->ftb_mtx);
788 for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
790 * Note that it's safe to access the active count on the
791 * associated proc structure because we know that at least one
792 * provider (this one) will still be around throughout this
795 if (tp->ftt_pid != pid || tp->ftt_pc != pc ||
796 tp->ftt_proc->ftpc_acount == 0)
800 * Now that we've found a matching tracepoint, it would be
801 * a decent idea to confirm that the tracepoint is still
802 * enabled and the trap instruction hasn't been overwritten.
803 * Since this is a little hairy, we'll punt for now.
807 * This can't be the first interested probe. We don't have
808 * to worry about another thread being in the midst of
809 * deleting this tracepoint (which would be the only valid
810 * reason for a tracepoint to have no interested probes)
811 * since we're holding P_PR_LOCK for this process.
813 ASSERT(tp->ftt_ids != NULL || tp->ftt_retids != NULL);
815 switch (id->fti_ptype) {
818 case DTFTP_IS_ENABLED:
819 id->fti_next = tp->ftt_ids;
826 case DTFTP_POST_OFFSETS:
827 id->fti_next = tp->ftt_retids;
837 mutex_exit(&bucket->ftb_mtx);
839 if (new_tp != NULL) {
840 new_tp->ftt_ids = NULL;
841 new_tp->ftt_retids = NULL;
848 * If we have a good tracepoint ready to go, install it now while
849 * we have the lock held and no one can screw with us.
851 if (new_tp != NULL) {
854 new_tp->ftt_next = bucket->ftb_data;
856 bucket->ftb_data = new_tp;
858 mutex_exit(&bucket->ftb_mtx);
861 * Activate the tracepoint in the ISA-specific manner.
862 * If this fails, we need to report the failure, but
863 * indicate that this tracepoint must still be disabled
864 * by calling fasttrap_tracepoint_disable().
866 if (fasttrap_tracepoint_install(p, new_tp) != 0)
867 rc = FASTTRAP_ENABLE_PARTIAL;
870 * Increment the count of the number of tracepoints active in
871 * the victim process.
874 ASSERT(p->p_proc_flag & P_PR_LOCK);
881 mutex_exit(&bucket->ftb_mtx);
884 * Initialize the tracepoint that's been preallocated with the probe.
886 new_tp = probe->ftp_tps[index].fit_tp;
888 ASSERT(new_tp->ftt_pid == pid);
889 ASSERT(new_tp->ftt_pc == pc);
890 ASSERT(new_tp->ftt_proc == probe->ftp_prov->ftp_proc);
891 ASSERT(new_tp->ftt_ids == NULL);
892 ASSERT(new_tp->ftt_retids == NULL);
894 switch (id->fti_ptype) {
897 case DTFTP_IS_ENABLED:
899 new_tp->ftt_ids = id;
903 case DTFTP_POST_OFFSETS:
905 new_tp->ftt_retids = id;
913 * If the ISA-dependent initialization goes to plan, go back to the
914 * beginning and try to install this freshly made tracepoint.
916 if (fasttrap_tracepoint_init(p, new_tp, pc, id->fti_ptype) == 0)
919 new_tp->ftt_ids = NULL;
920 new_tp->ftt_retids = NULL;
922 return (FASTTRAP_ENABLE_FAIL);
926 fasttrap_tracepoint_disable(proc_t *p, fasttrap_probe_t *probe, uint_t index)
928 fasttrap_bucket_t *bucket;
929 fasttrap_provider_t *provider = probe->ftp_prov;
930 fasttrap_tracepoint_t **pp, *tp;
931 fasttrap_id_t *id, **idp = NULL;
935 ASSERT(index < probe->ftp_ntps);
937 pid = probe->ftp_pid;
938 pc = probe->ftp_tps[index].fit_tp->ftt_pc;
939 id = &probe->ftp_tps[index].fit_id;
941 ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid);
944 * Find the tracepoint and make sure that our id is one of the
945 * ones registered with it.
947 bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
948 mutex_enter(&bucket->ftb_mtx);
949 for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
950 if (tp->ftt_pid == pid && tp->ftt_pc == pc &&
951 tp->ftt_proc == provider->ftp_proc)
956 * If we somehow lost this tracepoint, we're in a world of hurt.
960 switch (id->fti_ptype) {
963 case DTFTP_IS_ENABLED:
964 ASSERT(tp->ftt_ids != NULL);
969 case DTFTP_POST_OFFSETS:
970 ASSERT(tp->ftt_retids != NULL);
971 idp = &tp->ftt_retids;
978 while ((*idp)->fti_probe != probe) {
979 idp = &(*idp)->fti_next;
980 ASSERT(*idp != NULL);
987 ASSERT(id->fti_probe == probe);
990 * If there are other registered enablings of this tracepoint, we're
991 * all done, but if this was the last probe assocated with this
992 * this tracepoint, we need to remove and free it.
994 if (tp->ftt_ids != NULL || tp->ftt_retids != NULL) {
997 * If the current probe's tracepoint is in use, swap it
998 * for an unused tracepoint.
1000 if (tp == probe->ftp_tps[index].fit_tp) {
1001 fasttrap_probe_t *tmp_probe;
1002 fasttrap_tracepoint_t **tmp_tp;
1005 if (tp->ftt_ids != NULL) {
1006 tmp_probe = tp->ftt_ids->fti_probe;
1007 /* LINTED - alignment */
1008 tmp_index = FASTTRAP_ID_INDEX(tp->ftt_ids);
1009 tmp_tp = &tmp_probe->ftp_tps[tmp_index].fit_tp;
1011 tmp_probe = tp->ftt_retids->fti_probe;
1012 /* LINTED - alignment */
1013 tmp_index = FASTTRAP_ID_INDEX(tp->ftt_retids);
1014 tmp_tp = &tmp_probe->ftp_tps[tmp_index].fit_tp;
1017 ASSERT(*tmp_tp != NULL);
1018 ASSERT(*tmp_tp != probe->ftp_tps[index].fit_tp);
1019 ASSERT((*tmp_tp)->ftt_ids == NULL);
1020 ASSERT((*tmp_tp)->ftt_retids == NULL);
1022 probe->ftp_tps[index].fit_tp = *tmp_tp;
1026 mutex_exit(&bucket->ftb_mtx);
1029 * Tag the modified probe with the generation in which it was
1032 probe->ftp_gen = fasttrap_mod_gen;
1036 mutex_exit(&bucket->ftb_mtx);
1039 * We can't safely remove the tracepoint from the set of active
1040 * tracepoints until we've actually removed the fasttrap instruction
1041 * from the process's text. We can, however, operate on this
1042 * tracepoint secure in the knowledge that no other thread is going to
1043 * be looking at it since we hold P_PR_LOCK on the process if it's
1044 * live or we hold the provider lock on the process if it's dead and
1049 * We only need to remove the actual instruction if we're looking
1050 * at an existing process
1054 * If we fail to restore the instruction we need to kill
1055 * this process since it's in a completely unrecoverable
1058 if (fasttrap_tracepoint_remove(p, tp) != 0)
1059 fasttrap_sigtrap(p, NULL, pc);
1062 * Decrement the count of the number of tracepoints active
1063 * in the victim process.
1066 ASSERT(p->p_proc_flag & P_PR_LOCK);
1068 p->p_dtrace_count--;
1072 * Remove the probe from the hash table of active tracepoints.
1074 mutex_enter(&bucket->ftb_mtx);
1075 pp = (fasttrap_tracepoint_t **)&bucket->ftb_data;
1076 ASSERT(*pp != NULL);
1078 pp = &(*pp)->ftt_next;
1079 ASSERT(*pp != NULL);
1085 mutex_exit(&bucket->ftb_mtx);
1088 * Tag the modified probe with the generation in which it was changed.
1090 probe->ftp_gen = fasttrap_mod_gen;
1094 fasttrap_enable_callbacks(void)
1097 * We don't have to play the rw lock game here because we're
1098 * providing something rather than taking something away --
1099 * we can be sure that no threads have tried to follow this
1100 * function pointer yet.
1102 mutex_enter(&fasttrap_count_mtx);
1103 if (fasttrap_pid_count == 0) {
1104 ASSERT(dtrace_pid_probe_ptr == NULL);
1105 ASSERT(dtrace_return_probe_ptr == NULL);
1106 dtrace_pid_probe_ptr = &fasttrap_pid_probe;
1107 dtrace_return_probe_ptr = &fasttrap_return_probe;
1109 ASSERT(dtrace_pid_probe_ptr == &fasttrap_pid_probe);
1110 ASSERT(dtrace_return_probe_ptr == &fasttrap_return_probe);
1111 fasttrap_pid_count++;
1112 mutex_exit(&fasttrap_count_mtx);
1116 fasttrap_disable_callbacks(void)
1119 ASSERT(MUTEX_HELD(&cpu_lock));
1123 mutex_enter(&fasttrap_count_mtx);
1124 ASSERT(fasttrap_pid_count > 0);
1125 fasttrap_pid_count--;
1126 if (fasttrap_pid_count == 0) {
1128 cpu_t *cur, *cpu = CPU;
1130 for (cur = cpu->cpu_next_onln; cur != cpu;
1131 cur = cur->cpu_next_onln) {
1132 rw_enter(&cur->cpu_ft_lock, RW_WRITER);
1135 dtrace_pid_probe_ptr = NULL;
1136 dtrace_return_probe_ptr = NULL;
1138 for (cur = cpu->cpu_next_onln; cur != cpu;
1139 cur = cur->cpu_next_onln) {
1140 rw_exit(&cur->cpu_ft_lock);
1144 mutex_exit(&fasttrap_count_mtx);
1149 fasttrap_pid_enable(void *arg, dtrace_id_t id, void *parg)
1151 fasttrap_probe_t *probe = parg;
1155 ASSERT(probe != NULL);
1156 ASSERT(!probe->ftp_enabled);
1157 ASSERT(id == probe->ftp_id);
1159 ASSERT(MUTEX_HELD(&cpu_lock));
1163 * Increment the count of enabled probes on this probe's provider;
1164 * the provider can't go away while the probe still exists. We
1165 * must increment this even if we aren't able to properly enable
1168 mutex_enter(&probe->ftp_prov->ftp_mtx);
1169 probe->ftp_prov->ftp_rcount++;
1170 mutex_exit(&probe->ftp_prov->ftp_mtx);
1173 * If this probe's provider is retired (meaning it was valid in a
1174 * previously exec'ed incarnation of this address space), bail out. The
1175 * provider can't go away while we're in this code path.
1177 if (probe->ftp_prov->ftp_retired)
1181 * If we can't find the process, it may be that we're in the context of
1182 * a fork in which the traced process is being born and we're copying
1183 * USDT probes. Otherwise, the process is gone so bail.
1186 if ((p = sprlock(probe->ftp_pid)) == NULL) {
1187 if ((curproc->p_flag & SFORKING) == 0)
1190 mutex_enter(&pidlock);
1191 p = prfind(probe->ftp_pid);
1194 * Confirm that curproc is indeed forking the process in which
1195 * we're trying to enable probes.
1198 ASSERT(p->p_parent == curproc);
1199 ASSERT(p->p_stat == SIDL);
1201 mutex_enter(&p->p_lock);
1202 mutex_exit(&pidlock);
1207 ASSERT(!(p->p_flag & SVFORK));
1208 mutex_exit(&p->p_lock);
1210 if ((p = pfind(probe->ftp_pid)) == NULL)
1215 * We have to enable the trap entry point before any user threads have
1216 * the chance to execute the trap instruction we're about to place
1217 * in their process's text.
1221 * pfind() returns a locked process.
1226 fasttrap_enable_callbacks();
1229 * Enable all the tracepoints and add this probe's id to each
1230 * tracepoint's list of active probes.
1232 for (i = 0; i < probe->ftp_ntps; i++) {
1233 if ((rc = fasttrap_tracepoint_enable(p, probe, i)) != 0) {
1235 * If enabling the tracepoint failed completely,
1236 * we don't have to disable it; if the failure
1237 * was only partial we must disable it.
1239 if (rc == FASTTRAP_ENABLE_FAIL)
1242 ASSERT(rc == FASTTRAP_ENABLE_PARTIAL);
1245 * Back up and pull out all the tracepoints we've
1246 * created so far for this probe.
1249 fasttrap_tracepoint_disable(p, probe, i);
1254 mutex_enter(&p->p_lock);
1261 * Since we're not actually enabling this probe,
1262 * drop our reference on the trap table entry.
1264 fasttrap_disable_callbacks();
1269 mutex_enter(&p->p_lock);
1275 probe->ftp_enabled = 1;
1280 fasttrap_pid_disable(void *arg, dtrace_id_t id, void *parg)
1282 fasttrap_probe_t *probe = parg;
1283 fasttrap_provider_t *provider = probe->ftp_prov;
1287 ASSERT(id == probe->ftp_id);
1289 mutex_enter(&provider->ftp_mtx);
1292 * We won't be able to acquire a /proc-esque lock on the process
1293 * iff the process is dead and gone. In this case, we rely on the
1294 * provider lock as a point of mutual exclusion to prevent other
1295 * DTrace consumers from disabling this probe.
1297 if ((p = pfind(probe->ftp_pid)) != NULL) {
1305 * Disable all the associated tracepoints (for fully enabled probes).
1307 if (probe->ftp_enabled) {
1308 for (i = 0; i < probe->ftp_ntps; i++) {
1309 fasttrap_tracepoint_disable(p, probe, i);
1313 ASSERT(provider->ftp_rcount > 0);
1314 provider->ftp_rcount--;
1318 * Even though we may not be able to remove it entirely, we
1319 * mark this retired provider to get a chance to remove some
1320 * of the associated probes.
1322 if (provider->ftp_retired && !provider->ftp_marked)
1323 whack = provider->ftp_marked = 1;
1324 mutex_exit(&provider->ftp_mtx);
1327 * If the process is dead, we're just waiting for the
1328 * last probe to be disabled to be able to free it.
1330 if (provider->ftp_rcount == 0 && !provider->ftp_marked)
1331 whack = provider->ftp_marked = 1;
1332 mutex_exit(&provider->ftp_mtx);
1336 fasttrap_pid_cleanup();
1342 if (!probe->ftp_enabled)
1345 probe->ftp_enabled = 0;
1348 ASSERT(MUTEX_HELD(&cpu_lock));
1350 fasttrap_disable_callbacks();
1355 fasttrap_pid_getargdesc(void *arg, dtrace_id_t id, void *parg,
1356 dtrace_argdesc_t *desc)
1358 fasttrap_probe_t *probe = parg;
1362 desc->dtargd_native[0] = '\0';
1363 desc->dtargd_xlate[0] = '\0';
1365 if (probe->ftp_prov->ftp_retired != 0 ||
1366 desc->dtargd_ndx >= probe->ftp_nargs) {
1367 desc->dtargd_ndx = DTRACE_ARGNONE;
1371 ndx = (probe->ftp_argmap != NULL) ?
1372 probe->ftp_argmap[desc->dtargd_ndx] : desc->dtargd_ndx;
1374 str = probe->ftp_ntypes;
1375 for (i = 0; i < ndx; i++) {
1376 str += strlen(str) + 1;
1379 ASSERT(strlen(str + 1) < sizeof (desc->dtargd_native));
1380 (void) strcpy(desc->dtargd_native, str);
1382 if (probe->ftp_xtypes == NULL)
1385 str = probe->ftp_xtypes;
1386 for (i = 0; i < desc->dtargd_ndx; i++) {
1387 str += strlen(str) + 1;
1390 ASSERT(strlen(str + 1) < sizeof (desc->dtargd_xlate));
1391 (void) strcpy(desc->dtargd_xlate, str);
1396 fasttrap_pid_destroy(void *arg, dtrace_id_t id, void *parg)
1398 fasttrap_probe_t *probe = parg;
1402 ASSERT(probe != NULL);
1403 ASSERT(!probe->ftp_enabled);
1404 ASSERT(fasttrap_total >= probe->ftp_ntps);
1406 atomic_add_32(&fasttrap_total, -probe->ftp_ntps);
1407 size = offsetof(fasttrap_probe_t, ftp_tps[probe->ftp_ntps]);
1409 if (probe->ftp_gen + 1 >= fasttrap_mod_gen)
1410 fasttrap_mod_barrier(probe->ftp_gen);
1412 for (i = 0; i < probe->ftp_ntps; i++) {
1413 kmem_free(probe->ftp_tps[i].fit_tp,
1414 sizeof (fasttrap_tracepoint_t));
1417 kmem_free(probe, size);
1421 static const dtrace_pattr_t pid_attr = {
1422 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
1423 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
1424 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
1425 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
1426 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
1429 static dtrace_pops_t pid_pops = {
1430 fasttrap_pid_provide,
1432 fasttrap_pid_enable,
1433 fasttrap_pid_disable,
1436 fasttrap_pid_getargdesc,
1437 fasttrap_pid_getarg,
1439 fasttrap_pid_destroy
1442 static dtrace_pops_t usdt_pops = {
1443 fasttrap_pid_provide,
1445 fasttrap_pid_enable,
1446 fasttrap_pid_disable,
1449 fasttrap_pid_getargdesc,
1450 fasttrap_usdt_getarg,
1452 fasttrap_pid_destroy
1455 static fasttrap_proc_t *
1456 fasttrap_proc_lookup(pid_t pid)
1458 fasttrap_bucket_t *bucket;
1459 fasttrap_proc_t *fprc, *new_fprc;
1462 bucket = &fasttrap_procs.fth_table[FASTTRAP_PROCS_INDEX(pid)];
1463 mutex_enter(&bucket->ftb_mtx);
1465 for (fprc = bucket->ftb_data; fprc != NULL; fprc = fprc->ftpc_next) {
1466 if (fprc->ftpc_pid == pid && fprc->ftpc_acount != 0) {
1467 mutex_enter(&fprc->ftpc_mtx);
1468 mutex_exit(&bucket->ftb_mtx);
1469 fprc->ftpc_rcount++;
1470 atomic_add_64(&fprc->ftpc_acount, 1);
1471 ASSERT(fprc->ftpc_acount <= fprc->ftpc_rcount);
1472 mutex_exit(&fprc->ftpc_mtx);
1479 * Drop the bucket lock so we don't try to perform a sleeping
1480 * allocation under it.
1482 mutex_exit(&bucket->ftb_mtx);
1484 new_fprc = kmem_zalloc(sizeof (fasttrap_proc_t), KM_SLEEP);
1485 new_fprc->ftpc_pid = pid;
1486 new_fprc->ftpc_rcount = 1;
1487 new_fprc->ftpc_acount = 1;
1489 mutex_init(&new_fprc->ftpc_mtx, "fasttrap proc mtx", MUTEX_DEFAULT,
1493 mutex_enter(&bucket->ftb_mtx);
1496 * Take another lap through the list to make sure a proc hasn't
1497 * been created for this pid while we weren't under the bucket lock.
1499 for (fprc = bucket->ftb_data; fprc != NULL; fprc = fprc->ftpc_next) {
1500 if (fprc->ftpc_pid == pid && fprc->ftpc_acount != 0) {
1501 mutex_enter(&fprc->ftpc_mtx);
1502 mutex_exit(&bucket->ftb_mtx);
1503 fprc->ftpc_rcount++;
1504 atomic_add_64(&fprc->ftpc_acount, 1);
1505 ASSERT(fprc->ftpc_acount <= fprc->ftpc_rcount);
1506 mutex_exit(&fprc->ftpc_mtx);
1508 kmem_free(new_fprc, sizeof (fasttrap_proc_t));
1514 new_fprc->ftpc_next = bucket->ftb_data;
1515 bucket->ftb_data = new_fprc;
1517 mutex_exit(&bucket->ftb_mtx);
1523 fasttrap_proc_release(fasttrap_proc_t *proc)
1525 fasttrap_bucket_t *bucket;
1526 fasttrap_proc_t *fprc, **fprcp;
1527 pid_t pid = proc->ftpc_pid;
1529 fasttrap_scrblock_t *scrblk, *scrblktmp;
1530 fasttrap_scrspace_t *scrspc, *scrspctmp;
1535 mutex_enter(&proc->ftpc_mtx);
1537 ASSERT(proc->ftpc_rcount != 0);
1538 ASSERT(proc->ftpc_acount <= proc->ftpc_rcount);
1540 if (--proc->ftpc_rcount != 0) {
1541 mutex_exit(&proc->ftpc_mtx);
1547 * Free all structures used to manage per-thread scratch space.
1549 LIST_FOREACH_SAFE(scrblk, &proc->ftpc_scrblks, ftsb_next,
1551 LIST_REMOVE(scrblk, ftsb_next);
1552 free(scrblk, M_SOLARIS);
1554 LIST_FOREACH_SAFE(scrspc, &proc->ftpc_fscr, ftss_next, scrspctmp) {
1555 LIST_REMOVE(scrspc, ftss_next);
1556 free(scrspc, M_SOLARIS);
1558 LIST_FOREACH_SAFE(scrspc, &proc->ftpc_ascr, ftss_next, scrspctmp) {
1559 LIST_REMOVE(scrspc, ftss_next);
1560 free(scrspc, M_SOLARIS);
1563 if ((p = pfind(pid)) != NULL) {
1564 FOREACH_THREAD_IN_PROC(p, td)
1565 td->t_dtrace_sscr = NULL;
1570 mutex_exit(&proc->ftpc_mtx);
1573 * There should definitely be no live providers associated with this
1574 * process at this point.
1576 ASSERT(proc->ftpc_acount == 0);
1578 bucket = &fasttrap_procs.fth_table[FASTTRAP_PROCS_INDEX(pid)];
1579 mutex_enter(&bucket->ftb_mtx);
1581 fprcp = (fasttrap_proc_t **)&bucket->ftb_data;
1582 while ((fprc = *fprcp) != NULL) {
1586 fprcp = &fprc->ftpc_next;
1590 * Something strange has happened if we can't find the proc.
1592 ASSERT(fprc != NULL);
1594 *fprcp = fprc->ftpc_next;
1596 mutex_exit(&bucket->ftb_mtx);
1598 kmem_free(fprc, sizeof (fasttrap_proc_t));
1602 * Lookup a fasttrap-managed provider based on its name and associated pid.
1603 * If the pattr argument is non-NULL, this function instantiates the provider
1604 * if it doesn't exist otherwise it returns NULL. The provider is returned
1605 * with its lock held.
1607 static fasttrap_provider_t *
1608 fasttrap_provider_lookup(pid_t pid, const char *name,
1609 const dtrace_pattr_t *pattr)
1611 fasttrap_provider_t *fp, *new_fp = NULL;
1612 fasttrap_bucket_t *bucket;
1613 char provname[DTRACE_PROVNAMELEN];
1617 ASSERT(strlen(name) < sizeof (fp->ftp_name));
1618 ASSERT(pattr != NULL);
1620 bucket = &fasttrap_provs.fth_table[FASTTRAP_PROVS_INDEX(pid, name)];
1621 mutex_enter(&bucket->ftb_mtx);
1624 * Take a lap through the list and return the match if we find it.
1626 for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) {
1627 if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 &&
1629 mutex_enter(&fp->ftp_mtx);
1630 mutex_exit(&bucket->ftb_mtx);
1636 * Drop the bucket lock so we don't try to perform a sleeping
1637 * allocation under it.
1639 mutex_exit(&bucket->ftb_mtx);
1642 * Make sure the process exists, isn't a child created as the result
1643 * of a vfork(2), and isn't a zombie (but may be in fork).
1645 if ((p = pfind(pid)) == NULL)
1649 * Increment p_dtrace_probes so that the process knows to inform us
1650 * when it exits or execs. fasttrap_provider_free() decrements this
1651 * when we're done with this provider.
1653 p->p_dtrace_probes++;
1656 * Grab the credentials for this process so we have
1657 * something to pass to dtrace_register().
1659 PROC_LOCK_ASSERT(p, MA_OWNED);
1664 new_fp = kmem_zalloc(sizeof (fasttrap_provider_t), KM_SLEEP);
1665 new_fp->ftp_pid = pid;
1666 new_fp->ftp_proc = fasttrap_proc_lookup(pid);
1668 mutex_init(&new_fp->ftp_mtx, "provider mtx", MUTEX_DEFAULT, NULL);
1669 mutex_init(&new_fp->ftp_cmtx, "lock on creating", MUTEX_DEFAULT, NULL);
1672 ASSERT(new_fp->ftp_proc != NULL);
1674 mutex_enter(&bucket->ftb_mtx);
1677 * Take another lap through the list to make sure a provider hasn't
1678 * been created for this pid while we weren't under the bucket lock.
1680 for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) {
1681 if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 &&
1683 mutex_enter(&fp->ftp_mtx);
1684 mutex_exit(&bucket->ftb_mtx);
1685 fasttrap_provider_free(new_fp);
1691 (void) strcpy(new_fp->ftp_name, name);
1694 * Fail and return NULL if either the provider name is too long
1695 * or we fail to register this new provider with the DTrace
1696 * framework. Note that this is the only place we ever construct
1697 * the full provider name -- we keep it in pieces in the provider
1700 if (snprintf(provname, sizeof (provname), "%s%u", name, (uint_t)pid) >=
1701 sizeof (provname) ||
1702 dtrace_register(provname, pattr,
1703 DTRACE_PRIV_PROC | DTRACE_PRIV_OWNER | DTRACE_PRIV_ZONEOWNER, cred,
1704 pattr == &pid_attr ? &pid_pops : &usdt_pops, new_fp,
1705 &new_fp->ftp_provid) != 0) {
1706 mutex_exit(&bucket->ftb_mtx);
1707 fasttrap_provider_free(new_fp);
1712 new_fp->ftp_next = bucket->ftb_data;
1713 bucket->ftb_data = new_fp;
1715 mutex_enter(&new_fp->ftp_mtx);
1716 mutex_exit(&bucket->ftb_mtx);
1723 fasttrap_provider_free(fasttrap_provider_t *provider)
1725 pid_t pid = provider->ftp_pid;
1729 * There need to be no associated enabled probes, no consumers
1730 * creating probes, and no meta providers referencing this provider.
1732 ASSERT(provider->ftp_rcount == 0);
1733 ASSERT(provider->ftp_ccount == 0);
1734 ASSERT(provider->ftp_mcount == 0);
1737 * If this provider hasn't been retired, we need to explicitly drop the
1738 * count of active providers on the associated process structure.
1740 if (!provider->ftp_retired) {
1741 atomic_add_64(&provider->ftp_proc->ftpc_acount, -1);
1742 ASSERT(provider->ftp_proc->ftpc_acount <
1743 provider->ftp_proc->ftpc_rcount);
1746 fasttrap_proc_release(provider->ftp_proc);
1749 mutex_destroy(&provider->ftp_mtx);
1750 mutex_destroy(&provider->ftp_cmtx);
1752 kmem_free(provider, sizeof (fasttrap_provider_t));
1755 * Decrement p_dtrace_probes on the process whose provider we're
1756 * freeing. We don't have to worry about clobbering somone else's
1757 * modifications to it because we have locked the bucket that
1758 * corresponds to this process's hash chain in the provider hash
1759 * table. Don't sweat it if we can't find the process.
1761 if ((p = pfind(pid)) == NULL) {
1765 p->p_dtrace_probes--;
1772 fasttrap_provider_retire(pid_t pid, const char *name, int mprov)
1774 fasttrap_provider_t *fp;
1775 fasttrap_bucket_t *bucket;
1776 dtrace_provider_id_t provid;
1778 ASSERT(strlen(name) < sizeof (fp->ftp_name));
1780 bucket = &fasttrap_provs.fth_table[FASTTRAP_PROVS_INDEX(pid, name)];
1781 mutex_enter(&bucket->ftb_mtx);
1783 for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) {
1784 if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 &&
1790 mutex_exit(&bucket->ftb_mtx);
1794 mutex_enter(&fp->ftp_mtx);
1795 ASSERT(!mprov || fp->ftp_mcount > 0);
1796 if (mprov && --fp->ftp_mcount != 0) {
1797 mutex_exit(&fp->ftp_mtx);
1798 mutex_exit(&bucket->ftb_mtx);
1803 * Mark the provider to be removed in our post-processing step, mark it
1804 * retired, and drop the active count on its proc. Marking it indicates
1805 * that we should try to remove it; setting the retired flag indicates
1806 * that we're done with this provider; dropping the active the proc
1807 * releases our hold, and when this reaches zero (as it will during
1808 * exit or exec) the proc and associated providers become defunct.
1810 * We obviously need to take the bucket lock before the provider lock
1811 * to perform the lookup, but we need to drop the provider lock
1812 * before calling into the DTrace framework since we acquire the
1813 * provider lock in callbacks invoked from the DTrace framework. The
1814 * bucket lock therefore protects the integrity of the provider hash
1817 atomic_add_64(&fp->ftp_proc->ftpc_acount, -1);
1818 ASSERT(fp->ftp_proc->ftpc_acount < fp->ftp_proc->ftpc_rcount);
1820 fp->ftp_retired = 1;
1822 provid = fp->ftp_provid;
1823 mutex_exit(&fp->ftp_mtx);
1826 * We don't have to worry about invalidating the same provider twice
1827 * since fasttrap_provider_lookup() will ignore provider that have
1828 * been marked as retired.
1830 dtrace_invalidate(provid);
1832 mutex_exit(&bucket->ftb_mtx);
1834 fasttrap_pid_cleanup();
1838 fasttrap_uint32_cmp(const void *ap, const void *bp)
1840 return (*(const uint32_t *)ap - *(const uint32_t *)bp);
1844 fasttrap_uint64_cmp(const void *ap, const void *bp)
1846 return (*(const uint64_t *)ap - *(const uint64_t *)bp);
1850 fasttrap_add_probe(fasttrap_probe_spec_t *pdata)
1852 fasttrap_provider_t *provider;
1853 fasttrap_probe_t *pp;
1854 fasttrap_tracepoint_t *tp;
1856 int i, aframes = 0, whack;
1859 * There needs to be at least one desired trace point.
1861 if (pdata->ftps_noffs == 0)
1864 switch (pdata->ftps_type) {
1867 aframes = FASTTRAP_ENTRY_AFRAMES;
1871 aframes = FASTTRAP_RETURN_AFRAMES;
1880 if ((provider = fasttrap_provider_lookup(pdata->ftps_pid,
1881 FASTTRAP_PID_NAME, &pid_attr)) == NULL)
1885 * Increment this reference count to indicate that a consumer is
1886 * actively adding a new probe associated with this provider. This
1887 * prevents the provider from being deleted -- we'll need to check
1888 * for pending deletions when we drop this reference count.
1890 provider->ftp_ccount++;
1891 mutex_exit(&provider->ftp_mtx);
1894 * Grab the creation lock to ensure consistency between calls to
1895 * dtrace_probe_lookup() and dtrace_probe_create() in the face of
1896 * other threads creating probes. We must drop the provider lock
1897 * before taking this lock to avoid a three-way deadlock with the
1900 mutex_enter(&provider->ftp_cmtx);
1903 for (i = 0; i < pdata->ftps_noffs; i++) {
1906 (void) sprintf(name_str, "%llx",
1907 (unsigned long long)pdata->ftps_offs[i]);
1909 if (dtrace_probe_lookup(provider->ftp_provid,
1910 pdata->ftps_mod, pdata->ftps_func, name_str) != 0)
1913 atomic_add_32(&fasttrap_total, 1);
1915 if (fasttrap_total > fasttrap_max) {
1916 atomic_add_32(&fasttrap_total, -1);
1920 pp = kmem_zalloc(sizeof (fasttrap_probe_t), KM_SLEEP);
1922 pp->ftp_prov = provider;
1923 pp->ftp_faddr = pdata->ftps_pc;
1924 pp->ftp_fsize = pdata->ftps_size;
1925 pp->ftp_pid = pdata->ftps_pid;
1928 tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t),
1931 tp->ftt_proc = provider->ftp_proc;
1932 tp->ftt_pc = pdata->ftps_offs[i] + pdata->ftps_pc;
1933 tp->ftt_pid = pdata->ftps_pid;
1935 pp->ftp_tps[0].fit_tp = tp;
1936 pp->ftp_tps[0].fit_id.fti_probe = pp;
1937 pp->ftp_tps[0].fit_id.fti_ptype = pdata->ftps_type;
1939 pp->ftp_id = dtrace_probe_create(provider->ftp_provid,
1940 pdata->ftps_mod, pdata->ftps_func, name_str,
1941 FASTTRAP_OFFSET_AFRAMES, pp);
1944 } else if (dtrace_probe_lookup(provider->ftp_provid, pdata->ftps_mod,
1945 pdata->ftps_func, name) == 0) {
1946 atomic_add_32(&fasttrap_total, pdata->ftps_noffs);
1948 if (fasttrap_total > fasttrap_max) {
1949 atomic_add_32(&fasttrap_total, -pdata->ftps_noffs);
1954 * Make sure all tracepoint program counter values are unique.
1955 * We later assume that each probe has exactly one tracepoint
1958 qsort(pdata->ftps_offs, pdata->ftps_noffs,
1959 sizeof (uint64_t), fasttrap_uint64_cmp);
1960 for (i = 1; i < pdata->ftps_noffs; i++) {
1961 if (pdata->ftps_offs[i] > pdata->ftps_offs[i - 1])
1964 atomic_add_32(&fasttrap_total, -pdata->ftps_noffs);
1968 ASSERT(pdata->ftps_noffs > 0);
1969 pp = kmem_zalloc(offsetof(fasttrap_probe_t,
1970 ftp_tps[pdata->ftps_noffs]), KM_SLEEP);
1972 pp->ftp_prov = provider;
1973 pp->ftp_faddr = pdata->ftps_pc;
1974 pp->ftp_fsize = pdata->ftps_size;
1975 pp->ftp_pid = pdata->ftps_pid;
1976 pp->ftp_ntps = pdata->ftps_noffs;
1978 for (i = 0; i < pdata->ftps_noffs; i++) {
1979 tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t),
1982 tp->ftt_proc = provider->ftp_proc;
1983 tp->ftt_pc = pdata->ftps_offs[i] + pdata->ftps_pc;
1984 tp->ftt_pid = pdata->ftps_pid;
1986 pp->ftp_tps[i].fit_tp = tp;
1987 pp->ftp_tps[i].fit_id.fti_probe = pp;
1988 pp->ftp_tps[i].fit_id.fti_ptype = pdata->ftps_type;
1991 pp->ftp_id = dtrace_probe_create(provider->ftp_provid,
1992 pdata->ftps_mod, pdata->ftps_func, name, aframes, pp);
1995 mutex_exit(&provider->ftp_cmtx);
1998 * We know that the provider is still valid since we incremented the
1999 * creation reference count. If someone tried to clean up this provider
2000 * while we were using it (e.g. because the process called exec(2) or
2001 * exit(2)), take note of that and try to clean it up now.
2003 mutex_enter(&provider->ftp_mtx);
2004 provider->ftp_ccount--;
2005 whack = provider->ftp_retired;
2006 mutex_exit(&provider->ftp_mtx);
2009 fasttrap_pid_cleanup();
2015 * If we've exhausted the allowable resources, we'll try to remove
2016 * this provider to free some up. This is to cover the case where
2017 * the user has accidentally created many more probes than was
2018 * intended (e.g. pid123:::).
2020 mutex_exit(&provider->ftp_cmtx);
2021 mutex_enter(&provider->ftp_mtx);
2022 provider->ftp_ccount--;
2023 provider->ftp_marked = 1;
2024 mutex_exit(&provider->ftp_mtx);
2026 fasttrap_pid_cleanup();
2033 fasttrap_meta_provide(void *arg, dtrace_helper_provdesc_t *dhpv, pid_t pid)
2035 fasttrap_provider_t *provider;
2038 * A 32-bit unsigned integer (like a pid for example) can be
2039 * expressed in 10 or fewer decimal digits. Make sure that we'll
2040 * have enough space for the provider name.
2042 if (strlen(dhpv->dthpv_provname) + 10 >=
2043 sizeof (provider->ftp_name)) {
2044 printf("failed to instantiate provider %s: "
2045 "name too long to accomodate pid", dhpv->dthpv_provname);
2050 * Don't let folks spoof the true pid provider.
2052 if (strcmp(dhpv->dthpv_provname, FASTTRAP_PID_NAME) == 0) {
2053 printf("failed to instantiate provider %s: "
2054 "%s is an invalid name", dhpv->dthpv_provname,
2060 * The highest stability class that fasttrap supports is ISA; cap
2061 * the stability of the new provider accordingly.
2063 if (dhpv->dthpv_pattr.dtpa_provider.dtat_class > DTRACE_CLASS_ISA)
2064 dhpv->dthpv_pattr.dtpa_provider.dtat_class = DTRACE_CLASS_ISA;
2065 if (dhpv->dthpv_pattr.dtpa_mod.dtat_class > DTRACE_CLASS_ISA)
2066 dhpv->dthpv_pattr.dtpa_mod.dtat_class = DTRACE_CLASS_ISA;
2067 if (dhpv->dthpv_pattr.dtpa_func.dtat_class > DTRACE_CLASS_ISA)
2068 dhpv->dthpv_pattr.dtpa_func.dtat_class = DTRACE_CLASS_ISA;
2069 if (dhpv->dthpv_pattr.dtpa_name.dtat_class > DTRACE_CLASS_ISA)
2070 dhpv->dthpv_pattr.dtpa_name.dtat_class = DTRACE_CLASS_ISA;
2071 if (dhpv->dthpv_pattr.dtpa_args.dtat_class > DTRACE_CLASS_ISA)
2072 dhpv->dthpv_pattr.dtpa_args.dtat_class = DTRACE_CLASS_ISA;
2074 if ((provider = fasttrap_provider_lookup(pid, dhpv->dthpv_provname,
2075 &dhpv->dthpv_pattr)) == NULL) {
2076 printf("failed to instantiate provider %s for "
2077 "process %u", dhpv->dthpv_provname, (uint_t)pid);
2082 * Up the meta provider count so this provider isn't removed until
2083 * the meta provider has been told to remove it.
2085 provider->ftp_mcount++;
2087 mutex_exit(&provider->ftp_mtx);
2094 fasttrap_meta_create_probe(void *arg, void *parg,
2095 dtrace_helper_probedesc_t *dhpb)
2097 fasttrap_provider_t *provider = parg;
2098 fasttrap_probe_t *pp;
2099 fasttrap_tracepoint_t *tp;
2104 * Since the meta provider count is non-zero we don't have to worry
2105 * about this provider disappearing.
2107 ASSERT(provider->ftp_mcount > 0);
2110 * The offsets must be unique.
2112 qsort(dhpb->dthpb_offs, dhpb->dthpb_noffs, sizeof (uint32_t),
2113 fasttrap_uint32_cmp);
2114 for (i = 1; i < dhpb->dthpb_noffs; i++) {
2115 if (dhpb->dthpb_base + dhpb->dthpb_offs[i] <=
2116 dhpb->dthpb_base + dhpb->dthpb_offs[i - 1])
2120 qsort(dhpb->dthpb_enoffs, dhpb->dthpb_nenoffs, sizeof (uint32_t),
2121 fasttrap_uint32_cmp);
2122 for (i = 1; i < dhpb->dthpb_nenoffs; i++) {
2123 if (dhpb->dthpb_base + dhpb->dthpb_enoffs[i] <=
2124 dhpb->dthpb_base + dhpb->dthpb_enoffs[i - 1])
2129 * Grab the creation lock to ensure consistency between calls to
2130 * dtrace_probe_lookup() and dtrace_probe_create() in the face of
2131 * other threads creating probes.
2133 mutex_enter(&provider->ftp_cmtx);
2135 if (dtrace_probe_lookup(provider->ftp_provid, dhpb->dthpb_mod,
2136 dhpb->dthpb_func, dhpb->dthpb_name) != 0) {
2137 mutex_exit(&provider->ftp_cmtx);
2141 ntps = dhpb->dthpb_noffs + dhpb->dthpb_nenoffs;
2144 atomic_add_32(&fasttrap_total, ntps);
2146 if (fasttrap_total > fasttrap_max) {
2147 atomic_add_32(&fasttrap_total, -ntps);
2148 mutex_exit(&provider->ftp_cmtx);
2152 pp = kmem_zalloc(offsetof(fasttrap_probe_t, ftp_tps[ntps]), KM_SLEEP);
2154 pp->ftp_prov = provider;
2155 pp->ftp_pid = provider->ftp_pid;
2156 pp->ftp_ntps = ntps;
2157 pp->ftp_nargs = dhpb->dthpb_xargc;
2158 pp->ftp_xtypes = dhpb->dthpb_xtypes;
2159 pp->ftp_ntypes = dhpb->dthpb_ntypes;
2162 * First create a tracepoint for each actual point of interest.
2164 for (i = 0; i < dhpb->dthpb_noffs; i++) {
2165 tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), KM_SLEEP);
2167 tp->ftt_proc = provider->ftp_proc;
2168 tp->ftt_pc = dhpb->dthpb_base + dhpb->dthpb_offs[i];
2169 tp->ftt_pid = provider->ftp_pid;
2171 pp->ftp_tps[i].fit_tp = tp;
2172 pp->ftp_tps[i].fit_id.fti_probe = pp;
2174 pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_POST_OFFSETS;
2176 pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_OFFSETS;
2181 * Then create a tracepoint for each is-enabled point.
2183 for (j = 0; i < ntps; i++, j++) {
2184 tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), KM_SLEEP);
2186 tp->ftt_proc = provider->ftp_proc;
2187 tp->ftt_pc = dhpb->dthpb_base + dhpb->dthpb_enoffs[j];
2188 tp->ftt_pid = provider->ftp_pid;
2190 pp->ftp_tps[i].fit_tp = tp;
2191 pp->ftp_tps[i].fit_id.fti_probe = pp;
2192 pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_IS_ENABLED;
2196 * If the arguments are shuffled around we set the argument remapping
2197 * table. Later, when the probe fires, we only remap the arguments
2198 * if the table is non-NULL.
2200 for (i = 0; i < dhpb->dthpb_xargc; i++) {
2201 if (dhpb->dthpb_args[i] != i) {
2202 pp->ftp_argmap = dhpb->dthpb_args;
2208 * The probe is fully constructed -- register it with DTrace.
2210 pp->ftp_id = dtrace_probe_create(provider->ftp_provid, dhpb->dthpb_mod,
2211 dhpb->dthpb_func, dhpb->dthpb_name, FASTTRAP_OFFSET_AFRAMES, pp);
2213 mutex_exit(&provider->ftp_cmtx);
2218 fasttrap_meta_remove(void *arg, dtrace_helper_provdesc_t *dhpv, pid_t pid)
2221 * Clean up the USDT provider. There may be active consumers of the
2222 * provider busy adding probes, no damage will actually befall the
2223 * provider until that count has dropped to zero. This just puts
2224 * the provider on death row.
2226 fasttrap_provider_retire(pid, dhpv->dthpv_provname, 1);
2229 static dtrace_mops_t fasttrap_mops = {
2230 fasttrap_meta_create_probe,
2231 fasttrap_meta_provide,
2232 fasttrap_meta_remove
2237 fasttrap_open(struct cdev *dev __unused, int oflags __unused,
2238 int devtype __unused, struct thread *td __unused)
2245 fasttrap_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int fflag,
2249 struct kinfo_proc kp;
2250 const cred_t *cr = td->td_ucred;
2252 if (!dtrace_attached())
2255 if (cmd == FASTTRAPIOC_MAKEPROBE) {
2256 fasttrap_probe_spec_t *uprobe = *(fasttrap_probe_spec_t **)arg;
2257 fasttrap_probe_spec_t *probe;
2262 if (copyin(&uprobe->ftps_noffs, &noffs,
2263 sizeof (uprobe->ftps_noffs)))
2267 * Probes must have at least one tracepoint.
2272 size = sizeof (fasttrap_probe_spec_t) +
2273 sizeof (probe->ftps_offs[0]) * (noffs - 1);
2275 if (size > 1024 * 1024)
2278 probe = kmem_alloc(size, KM_SLEEP);
2280 if (copyin(uprobe, probe, size) != 0 ||
2281 probe->ftps_noffs != noffs) {
2282 kmem_free(probe, size);
2287 * Verify that the function and module strings contain no
2290 if (u8_validate(probe->ftps_func, strlen(probe->ftps_func),
2291 NULL, U8_VALIDATE_ENTIRE, &err) < 0) {
2296 if (u8_validate(probe->ftps_mod, strlen(probe->ftps_mod),
2297 NULL, U8_VALIDATE_ENTIRE, &err) < 0) {
2303 if (!PRIV_POLICY_CHOICE(cr, PRIV_ALL, B_FALSE)) {
2305 pid_t pid = probe->ftps_pid;
2308 mutex_enter(&pidlock);
2311 * Report an error if the process doesn't exist
2312 * or is actively being birthed.
2316 fill_kinfo_proc(p, &kp);
2317 if (p == NULL || kp.ki_stat == SIDL) {
2319 mutex_exit(&pidlock);
2324 mutex_enter(&p->p_lock);
2325 mutex_exit(&pidlock);
2327 PROC_LOCK_ASSERT(p, MA_OWNED);
2331 if ((ret = priv_proc_cred_perm(cr, p, NULL,
2332 VREAD | VWRITE)) != 0) {
2334 mutex_exit(&p->p_lock);
2342 mutex_exit(&p->p_lock);
2349 ret = fasttrap_add_probe(probe);
2351 kmem_free(probe, size);
2355 } else if (cmd == FASTTRAPIOC_GETINSTR) {
2356 fasttrap_instr_query_t instr;
2357 fasttrap_tracepoint_t *tp;
2364 if (copyin((void *)arg, &instr, sizeof (instr)) != 0)
2369 if (!PRIV_POLICY_CHOICE(cr, PRIV_ALL, B_FALSE)) {
2371 pid_t pid = instr.ftiq_pid;
2374 mutex_enter(&pidlock);
2377 * Report an error if the process doesn't exist
2378 * or is actively being birthed.
2382 fill_kinfo_proc(p, &kp);
2383 if (p == NULL || kp.ki_stat == SIDL) {
2385 mutex_exit(&pidlock);
2390 mutex_enter(&p->p_lock);
2391 mutex_exit(&pidlock);
2393 PROC_LOCK_ASSERT(p, MA_OWNED);
2397 if ((ret = priv_proc_cred_perm(cr, p, NULL,
2400 mutex_exit(&p->p_lock);
2409 mutex_exit(&p->p_lock);
2416 index = FASTTRAP_TPOINTS_INDEX(instr.ftiq_pid, instr.ftiq_pc);
2418 mutex_enter(&fasttrap_tpoints.fth_table[index].ftb_mtx);
2419 tp = fasttrap_tpoints.fth_table[index].ftb_data;
2420 while (tp != NULL) {
2421 if (instr.ftiq_pid == tp->ftt_pid &&
2422 instr.ftiq_pc == tp->ftt_pc &&
2423 tp->ftt_proc->ftpc_acount != 0)
2430 mutex_exit(&fasttrap_tpoints.fth_table[index].ftb_mtx);
2434 bcopy(&tp->ftt_instr, &instr.ftiq_instr,
2435 sizeof (instr.ftiq_instr));
2436 mutex_exit(&fasttrap_tpoints.fth_table[index].ftb_mtx);
2438 if (copyout(&instr, (void *)arg, sizeof (instr)) != 0)
2453 /* Create the /dev/dtrace/fasttrap entry. */
2454 fasttrap_cdev = make_dev(&fasttrap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
2457 mtx_init(&fasttrap_cleanup_mtx, "fasttrap clean", "dtrace", MTX_DEF);
2458 mutex_init(&fasttrap_count_mtx, "fasttrap count mtx", MUTEX_DEFAULT,
2462 fasttrap_max = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
2463 "fasttrap-max-probes", FASTTRAP_MAX_DEFAULT);
2465 fasttrap_max = FASTTRAP_MAX_DEFAULT;
2470 * Conjure up the tracepoints hashtable...
2473 nent = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
2474 "fasttrap-hash-size", FASTTRAP_TPOINTS_DEFAULT_SIZE);
2476 nent = FASTTRAP_TPOINTS_DEFAULT_SIZE;
2479 if (nent == 0 || nent > 0x1000000)
2480 nent = FASTTRAP_TPOINTS_DEFAULT_SIZE;
2482 if ((nent & (nent - 1)) == 0)
2483 fasttrap_tpoints.fth_nent = nent;
2485 fasttrap_tpoints.fth_nent = 1 << fasttrap_highbit(nent);
2486 ASSERT(fasttrap_tpoints.fth_nent > 0);
2487 fasttrap_tpoints.fth_mask = fasttrap_tpoints.fth_nent - 1;
2488 fasttrap_tpoints.fth_table = kmem_zalloc(fasttrap_tpoints.fth_nent *
2489 sizeof (fasttrap_bucket_t), KM_SLEEP);
2491 for (i = 0; i < fasttrap_tpoints.fth_nent; i++)
2492 mutex_init(&fasttrap_tpoints.fth_table[i].ftb_mtx,
2493 "tracepoints bucket mtx", MUTEX_DEFAULT, NULL);
2497 * ... and the providers hash table...
2499 nent = FASTTRAP_PROVIDERS_DEFAULT_SIZE;
2500 if ((nent & (nent - 1)) == 0)
2501 fasttrap_provs.fth_nent = nent;
2503 fasttrap_provs.fth_nent = 1 << fasttrap_highbit(nent);
2504 ASSERT(fasttrap_provs.fth_nent > 0);
2505 fasttrap_provs.fth_mask = fasttrap_provs.fth_nent - 1;
2506 fasttrap_provs.fth_table = kmem_zalloc(fasttrap_provs.fth_nent *
2507 sizeof (fasttrap_bucket_t), KM_SLEEP);
2509 for (i = 0; i < fasttrap_provs.fth_nent; i++)
2510 mutex_init(&fasttrap_provs.fth_table[i].ftb_mtx,
2511 "providers bucket mtx", MUTEX_DEFAULT, NULL);
2514 ret = kproc_create(fasttrap_pid_cleanup_cb, NULL,
2515 &fasttrap_cleanup_proc, 0, 0, "ftcleanup");
2517 destroy_dev(fasttrap_cdev);
2519 for (i = 0; i < fasttrap_provs.fth_nent; i++)
2520 mutex_destroy(&fasttrap_provs.fth_table[i].ftb_mtx);
2521 for (i = 0; i < fasttrap_tpoints.fth_nent; i++)
2522 mutex_destroy(&fasttrap_tpoints.fth_table[i].ftb_mtx);
2524 kmem_free(fasttrap_provs.fth_table, fasttrap_provs.fth_nent *
2525 sizeof (fasttrap_bucket_t));
2526 mtx_destroy(&fasttrap_cleanup_mtx);
2527 mutex_destroy(&fasttrap_count_mtx);
2533 * ... and the procs hash table.
2535 nent = FASTTRAP_PROCS_DEFAULT_SIZE;
2536 if ((nent & (nent - 1)) == 0)
2537 fasttrap_procs.fth_nent = nent;
2539 fasttrap_procs.fth_nent = 1 << fasttrap_highbit(nent);
2540 ASSERT(fasttrap_procs.fth_nent > 0);
2541 fasttrap_procs.fth_mask = fasttrap_procs.fth_nent - 1;
2542 fasttrap_procs.fth_table = kmem_zalloc(fasttrap_procs.fth_nent *
2543 sizeof (fasttrap_bucket_t), KM_SLEEP);
2545 for (i = 0; i < fasttrap_procs.fth_nent; i++)
2546 mutex_init(&fasttrap_procs.fth_table[i].ftb_mtx,
2547 "processes bucket mtx", MUTEX_DEFAULT, NULL);
2550 mutex_init(&fasttrap_cpuc_pid_lock[i], "fasttrap barrier",
2551 MUTEX_DEFAULT, NULL);
2555 * This event handler must run before kdtrace_thread_dtor() since it
2556 * accesses the thread's struct kdtrace_thread.
2558 fasttrap_thread_dtor_tag = EVENTHANDLER_REGISTER(thread_dtor,
2559 fasttrap_thread_dtor, NULL, EVENTHANDLER_PRI_FIRST);
2563 * Install our hooks into fork(2), exec(2), and exit(2).
2565 dtrace_fasttrap_fork = &fasttrap_fork;
2566 dtrace_fasttrap_exit = &fasttrap_exec_exit;
2567 dtrace_fasttrap_exec = &fasttrap_exec_exit;
2569 (void) dtrace_meta_register("fasttrap", &fasttrap_mops, NULL,
2576 fasttrap_unload(void)
2581 * Unregister the meta-provider to make sure no new fasttrap-
2582 * managed providers come along while we're trying to close up
2583 * shop. If we fail to detach, we'll need to re-register as a
2584 * meta-provider. We can fail to unregister as a meta-provider
2585 * if providers we manage still exist.
2587 if (fasttrap_meta_id != DTRACE_METAPROVNONE &&
2588 dtrace_meta_unregister(fasttrap_meta_id) != 0)
2592 * Iterate over all of our providers. If there's still a process
2593 * that corresponds to that pid, fail to detach.
2595 for (i = 0; i < fasttrap_provs.fth_nent; i++) {
2596 fasttrap_provider_t **fpp, *fp;
2597 fasttrap_bucket_t *bucket = &fasttrap_provs.fth_table[i];
2599 mutex_enter(&bucket->ftb_mtx);
2600 fpp = (fasttrap_provider_t **)&bucket->ftb_data;
2601 while ((fp = *fpp) != NULL) {
2603 * Acquire and release the lock as a simple way of
2604 * waiting for any other consumer to finish with
2605 * this provider. A thread must first acquire the
2606 * bucket lock so there's no chance of another thread
2607 * blocking on the provider's lock.
2609 mutex_enter(&fp->ftp_mtx);
2610 mutex_exit(&fp->ftp_mtx);
2612 if (dtrace_unregister(fp->ftp_provid) != 0) {
2614 fpp = &fp->ftp_next;
2616 *fpp = fp->ftp_next;
2617 fasttrap_provider_free(fp);
2621 mutex_exit(&bucket->ftb_mtx);
2625 (void) dtrace_meta_register("fasttrap", &fasttrap_mops, NULL,
2632 * Stop new processes from entering these hooks now, before the
2633 * fasttrap_cleanup thread runs. That way all processes will hopefully
2634 * be out of these hooks before we free fasttrap_provs.fth_table
2636 ASSERT(dtrace_fasttrap_fork == &fasttrap_fork);
2637 dtrace_fasttrap_fork = NULL;
2639 ASSERT(dtrace_fasttrap_exec == &fasttrap_exec_exit);
2640 dtrace_fasttrap_exec = NULL;
2642 ASSERT(dtrace_fasttrap_exit == &fasttrap_exec_exit);
2643 dtrace_fasttrap_exit = NULL;
2645 mtx_lock(&fasttrap_cleanup_mtx);
2646 fasttrap_cleanup_drain = 1;
2647 /* Wait for the cleanup thread to finish up and signal us. */
2648 wakeup(&fasttrap_cleanup_cv);
2649 mtx_sleep(&fasttrap_cleanup_drain, &fasttrap_cleanup_mtx, 0, "ftcld",
2651 fasttrap_cleanup_proc = NULL;
2652 mtx_destroy(&fasttrap_cleanup_mtx);
2655 mutex_enter(&fasttrap_count_mtx);
2656 ASSERT(fasttrap_pid_count == 0);
2657 mutex_exit(&fasttrap_count_mtx);
2661 EVENTHANDLER_DEREGISTER(thread_dtor, fasttrap_thread_dtor_tag);
2663 for (i = 0; i < fasttrap_tpoints.fth_nent; i++)
2664 mutex_destroy(&fasttrap_tpoints.fth_table[i].ftb_mtx);
2665 for (i = 0; i < fasttrap_provs.fth_nent; i++)
2666 mutex_destroy(&fasttrap_provs.fth_table[i].ftb_mtx);
2667 for (i = 0; i < fasttrap_procs.fth_nent; i++)
2668 mutex_destroy(&fasttrap_procs.fth_table[i].ftb_mtx);
2670 kmem_free(fasttrap_tpoints.fth_table,
2671 fasttrap_tpoints.fth_nent * sizeof (fasttrap_bucket_t));
2672 fasttrap_tpoints.fth_nent = 0;
2674 kmem_free(fasttrap_provs.fth_table,
2675 fasttrap_provs.fth_nent * sizeof (fasttrap_bucket_t));
2676 fasttrap_provs.fth_nent = 0;
2678 kmem_free(fasttrap_procs.fth_table,
2679 fasttrap_procs.fth_nent * sizeof (fasttrap_bucket_t));
2680 fasttrap_procs.fth_nent = 0;
2683 destroy_dev(fasttrap_cdev);
2684 mutex_destroy(&fasttrap_count_mtx);
2686 mutex_destroy(&fasttrap_cpuc_pid_lock[i]);
2695 fasttrap_modevent(module_t mod __unused, int type, void *data __unused)
2716 SYSINIT(fasttrap_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, fasttrap_load,
2718 SYSUNINIT(fasttrap_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY,
2719 fasttrap_unload, NULL);
2721 DEV_MODULE(fasttrap, fasttrap_modevent, NULL);
2722 MODULE_VERSION(fasttrap, 1);
2723 MODULE_DEPEND(fasttrap, dtrace, 1, 1, 1);
2724 MODULE_DEPEND(fasttrap, opensolaris, 1, 1, 1);