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]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * #pragma ident "@(#)fasttrap.c 1.26 08/04/21 SMI"
31 #include <sys/types.h>
34 #include <sys/errno.h>
37 #include <sys/systm.h>
38 #include <sys/kauth.h>
40 #include <sys/fasttrap.h>
41 #include <sys/fasttrap_impl.h>
42 #include <sys/fasttrap_isa.h>
43 #include <sys/dtrace.h>
44 #include <sys/dtrace_impl.h>
47 #include <miscfs/devfs/devfs.h>
48 #include <sys/proc_internal.h>
49 #include <sys/dtrace_glue.h>
50 #include <sys/dtrace_ptss.h>
52 #include <kern/zalloc.h>
54 /* Solaris proc_t is the struct. Darwin's proc_t is a pointer to it. */
55 #define proc_t struct proc /* Steer clear of the Darwin typedef for proc_t */
59 qsort(void *a
, size_t n
, size_t es
, int (*cmp
)(const void *, const void *));
62 * User-Land Trap-Based Tracing
63 * ----------------------------
65 * The fasttrap provider allows DTrace consumers to instrument any user-level
66 * instruction to gather data; this includes probes with semantic
67 * signifigance like entry and return as well as simple offsets into the
68 * function. While the specific techniques used are very ISA specific, the
69 * methodology is generalizable to any architecture.
72 * The General Methodology
73 * -----------------------
75 * With the primary goal of tracing every user-land instruction and the
76 * limitation that we can't trust user space so don't want to rely on much
77 * information there, we begin by replacing the instructions we want to trace
78 * with trap instructions. Each instruction we overwrite is saved into a hash
79 * table keyed by process ID and pc address. When we enter the kernel due to
80 * this trap instruction, we need the effects of the replaced instruction to
81 * appear to have occurred before we proceed with the user thread's
84 * Each user level thread is represented by a ulwp_t structure which is
85 * always easily accessible through a register. The most basic way to produce
86 * the effects of the instruction we replaced is to copy that instruction out
87 * to a bit of scratch space reserved in the user thread's ulwp_t structure
88 * (a sort of kernel-private thread local storage), set the PC to that
89 * scratch space and single step. When we reenter the kernel after single
90 * stepping the instruction we must then adjust the PC to point to what would
91 * normally be the next instruction. Of course, special care must be taken
92 * for branches and jumps, but these represent such a small fraction of any
93 * instruction set that writing the code to emulate these in the kernel is
96 * Return probes may require several tracepoints to trace every return site,
97 * and, conversely, each tracepoint may activate several probes (the entry
98 * and offset 0 probes, for example). To solve this muliplexing problem,
99 * tracepoints contain lists of probes to activate and probes contain lists
100 * of tracepoints to enable. If a probe is activated, it adds its ID to
101 * existing tracepoints or creates new ones as necessary.
103 * Most probes are activated _before_ the instruction is executed, but return
104 * probes are activated _after_ the effects of the last instruction of the
105 * function are visible. Return probes must be fired _after_ we have
106 * single-stepped the instruction whereas all other probes are fired
113 * The lock ordering below -- both internally and with respect to the DTrace
114 * framework -- is a little tricky and bears some explanation. Each provider
115 * has a lock (ftp_mtx) that protects its members including reference counts
116 * for enabled probes (ftp_rcount), consumers actively creating probes
117 * (ftp_ccount) and USDT consumers (ftp_mcount); all three prevent a provider
118 * from being freed. A provider is looked up by taking the bucket lock for the
119 * provider hash table, and is returned with its lock held. The provider lock
120 * may be taken in functions invoked by the DTrace framework, but may not be
121 * held while calling functions in the DTrace framework.
123 * To ensure consistency over multiple calls to the DTrace framework, the
124 * creation lock (ftp_cmtx) should be held. Naturally, the creation lock may
125 * not be taken when holding the provider lock as that would create a cyclic
126 * lock ordering. In situations where one would naturally take the provider
127 * lock and then the creation lock, we instead up a reference count to prevent
128 * the provider from disappearing, drop the provider lock, and acquire the
132 * bucket lock before provider lock
133 * DTrace before provider lock
134 * creation lock before DTrace
135 * never hold the provider lock and creation lock simultaneously
138 static dev_info_t
*fasttrap_devi
;
139 static dtrace_meta_provider_id_t fasttrap_meta_id
;
141 static thread_call_t fasttrap_timeout
;
142 static lck_mtx_t fasttrap_cleanup_mtx
;
143 static uint_t fasttrap_cleanup_work
;
146 * Generation count on modifications to the global tracepoint lookup table.
148 static volatile uint64_t fasttrap_mod_gen
;
150 #if !defined(__APPLE__)
152 * When the fasttrap provider is loaded, fasttrap_max is set to either
153 * FASTTRAP_MAX_DEFAULT or the value for fasttrap-max-probes in the
154 * fasttrap.conf file. Each time a probe is created, fasttrap_total is
155 * incremented by the number of tracepoints that may be associated with that
156 * probe; fasttrap_total is capped at fasttrap_max.
158 #define FASTTRAP_MAX_DEFAULT 2500000
161 static uint32_t fasttrap_max
;
162 static uint32_t fasttrap_total
;
165 #define FASTTRAP_TPOINTS_DEFAULT_SIZE 0x4000
166 #define FASTTRAP_PROVIDERS_DEFAULT_SIZE 0x100
167 #define FASTTRAP_PROCS_DEFAULT_SIZE 0x100
169 fasttrap_hash_t fasttrap_tpoints
;
170 static fasttrap_hash_t fasttrap_provs
;
171 static fasttrap_hash_t fasttrap_procs
;
173 static uint64_t fasttrap_pid_count
; /* pid ref count */
174 static lck_mtx_t fasttrap_count_mtx
; /* lock on ref count */
176 #define FASTTRAP_ENABLE_FAIL 1
177 #define FASTTRAP_ENABLE_PARTIAL 2
179 static int fasttrap_tracepoint_enable(proc_t
*, fasttrap_probe_t
*, uint_t
);
180 static void fasttrap_tracepoint_disable(proc_t
*, fasttrap_probe_t
*, uint_t
);
182 #if defined(__APPLE__)
183 static fasttrap_provider_t
*fasttrap_provider_lookup(pid_t
, fasttrap_provider_type_t
, const char *,
184 const dtrace_pattr_t
*);
186 static void fasttrap_provider_retire(pid_t
, const char *, int);
187 static void fasttrap_provider_free(fasttrap_provider_t
*);
189 static fasttrap_proc_t
*fasttrap_proc_lookup(pid_t
);
190 static void fasttrap_proc_release(fasttrap_proc_t
*);
192 #define FASTTRAP_PROVS_INDEX(pid, name) \
193 ((fasttrap_hash_str(name) + (pid)) & fasttrap_provs.fth_mask)
195 #define FASTTRAP_PROCS_INDEX(pid) ((pid) & fasttrap_procs.fth_mask)
197 #if defined(__APPLE__)
200 * To save memory, some common memory allocations are given a
201 * unique zone. In example, dtrace_probe_t is 72 bytes in size,
202 * which means it would fall into the kalloc.128 bucket. With
203 * 20k elements allocated, the space saved is substantial.
206 struct zone
*fasttrap_tracepoint_t_zone
;
209 * fasttrap_probe_t's are variable in size. Some quick profiling has shown
210 * that the sweet spot for reducing memory footprint is covering the first
211 * three sizes. Everything larger goes into the common pool.
213 #define FASTTRAP_PROBE_T_ZONE_MAX_TRACEPOINTS 4
215 struct zone
*fasttrap_probe_t_zones
[FASTTRAP_PROBE_T_ZONE_MAX_TRACEPOINTS
];
217 static const char *fasttrap_probe_t_zone_names
[FASTTRAP_PROBE_T_ZONE_MAX_TRACEPOINTS
] = {
219 "dtrace.fasttrap_probe_t[1]",
220 "dtrace.fasttrap_probe_t[2]",
221 "dtrace.fasttrap_probe_t[3]"
225 * We have to manage locks explicitly
227 lck_grp_t
* fasttrap_lck_grp
;
228 lck_grp_attr_t
* fasttrap_lck_grp_attr
;
229 lck_attr_t
* fasttrap_lck_attr
;
233 fasttrap_highbit(ulong_t i
)
240 if (i
& 0xffffffff00000000ul
) {
244 if (i
& 0xffff0000) {
263 fasttrap_hash_str(const char *p
)
269 hval
= (hval
<< 4) + *p
++;
270 if ((g
= (hval
& 0xf0000000)) != 0)
278 * FIXME - needs implementation
281 fasttrap_sigtrap(proc_t
*p
, uthread_t t
, user_addr_t pc
)
283 #pragma unused(p, t, pc)
286 sigqueue_t
*sqp
= kmem_zalloc(sizeof (sigqueue_t
), KM_SLEEP
);
288 sqp
->sq_info
.si_signo
= SIGTRAP
;
289 sqp
->sq_info
.si_code
= TRAP_DTRACE
;
290 sqp
->sq_info
.si_addr
= (caddr_t
)pc
;
292 mutex_enter(&p
->p_lock
);
294 mutex_exit(&p
->p_lock
);
300 printf("fasttrap_sigtrap called with no implementation.\n");
304 * This function ensures that no threads are actively using the memory
305 * associated with probes that were formerly live.
308 fasttrap_mod_barrier(uint64_t gen
)
312 if (gen
< fasttrap_mod_gen
)
317 for (i
= 0; i
< NCPU
; i
++) {
318 lck_mtx_lock(&cpu_core
[i
].cpuc_pid_lock
);
319 lck_mtx_unlock(&cpu_core
[i
].cpuc_pid_lock
);
324 * This is the timeout's callback for cleaning up the providers and their
329 fasttrap_pid_cleanup_cb(void *ignored
, void* ignored2
)
331 #pragma unused(ignored, ignored2)
332 fasttrap_provider_t
**fpp
, *fp
;
333 fasttrap_bucket_t
*bucket
;
334 dtrace_provider_id_t provid
;
335 unsigned int i
, later
= 0;
337 static volatile int in
= 0;
341 lck_mtx_lock(&fasttrap_cleanup_mtx
);
342 while (fasttrap_cleanup_work
) {
343 fasttrap_cleanup_work
= 0;
344 lck_mtx_unlock(&fasttrap_cleanup_mtx
);
349 * Iterate over all the providers trying to remove the marked
350 * ones. If a provider is marked but not retired, we just
351 * have to take a crack at removing it -- it's no big deal if
354 for (i
= 0; i
< fasttrap_provs
.fth_nent
; i
++) {
355 bucket
= &fasttrap_provs
.fth_table
[i
];
356 lck_mtx_lock(&bucket
->ftb_mtx
);
357 fpp
= (fasttrap_provider_t
**)&bucket
->ftb_data
;
359 while ((fp
= *fpp
) != NULL
) {
360 if (!fp
->ftp_marked
) {
365 lck_mtx_lock(&fp
->ftp_mtx
);
368 * If this provider has consumers actively
369 * creating probes (ftp_ccount) or is a USDT
370 * provider (ftp_mcount), we can't unregister
373 if (fp
->ftp_ccount
!= 0 ||
374 fp
->ftp_mcount
!= 0) {
376 lck_mtx_unlock(&fp
->ftp_mtx
);
380 if (!fp
->ftp_retired
|| fp
->ftp_rcount
!= 0)
383 lck_mtx_unlock(&fp
->ftp_mtx
);
386 * If we successfully unregister this
387 * provider we can remove it from the hash
388 * chain and free the memory. If our attempt
389 * to unregister fails and this is a retired
390 * provider, increment our flag to try again
391 * pretty soon. If we've consumed more than
392 * half of our total permitted number of
393 * probes call dtrace_condense() to try to
394 * clean out the unenabled probes.
396 provid
= fp
->ftp_provid
;
397 if (dtrace_unregister(provid
) != 0) {
398 if (fasttrap_total
> fasttrap_max
/ 2)
399 (void) dtrace_condense(provid
);
400 later
+= fp
->ftp_marked
;
404 fasttrap_provider_free(fp
);
407 lck_mtx_unlock(&bucket
->ftb_mtx
);
410 lck_mtx_lock(&fasttrap_cleanup_mtx
);
413 ASSERT(fasttrap_timeout
!= 0);
416 * APPLE NOTE: You must hold the fasttrap_cleanup_mtx to do this!
418 if (fasttrap_timeout
!= (thread_call_t
)1)
419 thread_call_free(fasttrap_timeout
);
422 * If we were unable to remove a retired provider, try again after
423 * a second. This situation can occur in certain circumstances where
424 * providers cannot be unregistered even though they have no probes
425 * enabled because of an execution of dtrace -l or something similar.
426 * If the timeout has been disabled (set to 1 because we're trying
427 * to detach), we set fasttrap_cleanup_work to ensure that we'll
428 * get a chance to do that work if and when the timeout is reenabled
431 if (later
> 0 && fasttrap_timeout
!= (thread_call_t
)1)
432 /* The time value passed to dtrace_timeout is in nanos */
433 fasttrap_timeout
= dtrace_timeout(&fasttrap_pid_cleanup_cb
, NULL
, NANOSEC
/ SEC
);
435 fasttrap_cleanup_work
= 1;
437 fasttrap_timeout
= 0;
439 lck_mtx_unlock(&fasttrap_cleanup_mtx
);
444 * Activates the asynchronous cleanup mechanism.
447 fasttrap_pid_cleanup(void)
449 lck_mtx_lock(&fasttrap_cleanup_mtx
);
450 fasttrap_cleanup_work
= 1;
451 if (fasttrap_timeout
== 0)
452 fasttrap_timeout
= dtrace_timeout(&fasttrap_pid_cleanup_cb
, NULL
, NANOSEC
/ MILLISEC
);
453 lck_mtx_unlock(&fasttrap_cleanup_mtx
);
457 * This is called from cfork() via dtrace_fasttrap_fork(). The child
458 * process's address space is a (roughly) a copy of the parent process's so
459 * we have to remove all the instrumentation we had previously enabled in the
463 fasttrap_fork(proc_t
*p
, proc_t
*cp
)
465 pid_t ppid
= p
->p_pid
;
468 ASSERT(current_proc() == p
);
469 lck_mtx_assert(&p
->p_dtrace_sprlock
, LCK_MTX_ASSERT_OWNED
);
470 ASSERT(p
->p_dtrace_count
> 0);
471 ASSERT(cp
->p_dtrace_count
== 0);
474 * This would be simpler and faster if we maintained per-process
475 * hash tables of enabled tracepoints. It could, however, potentially
476 * slow down execution of a tracepoint since we'd need to go
477 * through two levels of indirection. In the future, we should
478 * consider either maintaining per-process ancillary lists of
479 * enabled tracepoints or hanging a pointer to a per-process hash
480 * table of enabled tracepoints off the proc structure.
484 * We don't have to worry about the child process disappearing
485 * because we're in fork().
487 if (cp
!= sprlock(cp
->p_pid
)) {
488 printf("fasttrap_fork: sprlock(%d) returned a differt proc\n", cp
->p_pid
);
494 * Iterate over every tracepoint looking for ones that belong to the
495 * parent process, and remove each from the child process.
497 for (i
= 0; i
< fasttrap_tpoints
.fth_nent
; i
++) {
498 fasttrap_tracepoint_t
*tp
;
499 fasttrap_bucket_t
*bucket
= &fasttrap_tpoints
.fth_table
[i
];
501 lck_mtx_lock(&bucket
->ftb_mtx
);
502 for (tp
= bucket
->ftb_data
; tp
!= NULL
; tp
= tp
->ftt_next
) {
503 if (tp
->ftt_pid
== ppid
&&
504 tp
->ftt_proc
->ftpc_acount
!= 0) {
505 fasttrap_tracepoint_remove(cp
, tp
);
508 * The count of active providers can only be
509 * decremented (i.e. to zero) during exec,
510 * exit, and removal of a meta provider so it
511 * should be impossible to drop the count
514 ASSERT(tp
->ftt_proc
->ftpc_acount
!= 0);
517 lck_mtx_unlock(&bucket
->ftb_mtx
);
521 * Free any ptss pages/entries in the child.
523 dtrace_ptss_fork(p
, cp
);
530 * This is called from proc_exit() or from exec_common() if p_dtrace_probes
531 * is set on the proc structure to indicate that there is a pid provider
532 * associated with this process.
535 fasttrap_exec_exit(proc_t
*p
)
537 ASSERT(p
== current_proc());
538 lck_mtx_assert(&p
->p_mlock
, LCK_MTX_ASSERT_OWNED
);
539 lck_mtx_assert(&p
->p_dtrace_sprlock
, LCK_MTX_ASSERT_NOTOWNED
);
542 /* APPLE NOTE: Okay, the locking here is really odd and needs some
543 * explaining. This method is always called with the proc_lock held.
544 * We must drop the proc_lock before calling fasttrap_provider_retire
545 * to avoid a deadlock when it takes the bucket lock.
547 * Next, the dtrace_ptss_exec_exit function requires the sprlock
548 * be held, but not the proc_lock.
550 * Finally, we must re-acquire the proc_lock
555 * We clean up the pid provider for this process here; user-land
556 * static probes are handled by the meta-provider remove entry point.
558 fasttrap_provider_retire(p
->p_pid
, FASTTRAP_PID_NAME
, 0);
559 #if defined(__APPLE__)
561 * We also need to remove any aliased providers.
562 * XXX optimization: track which provider types are instantiated
563 * and only retire as needed.
565 fasttrap_provider_retire(p
->p_pid
, FASTTRAP_OBJC_NAME
, 0);
566 fasttrap_provider_retire(p
->p_pid
, FASTTRAP_ONESHOT_NAME
, 0);
567 #endif /* __APPLE__ */
570 * This should be called after it is no longer possible for a user
571 * thread to execute (potentially dtrace instrumented) instructions.
573 lck_mtx_lock(&p
->p_dtrace_sprlock
);
574 dtrace_ptss_exec_exit(p
);
575 lck_mtx_unlock(&p
->p_dtrace_sprlock
);
583 fasttrap_pid_provide(void *arg
, const dtrace_probedesc_t
*desc
)
585 #pragma unused(arg, desc)
587 * There are no "default" pid probes.
592 fasttrap_tracepoint_enable(proc_t
*p
, fasttrap_probe_t
*probe
, uint_t index
)
594 fasttrap_tracepoint_t
*tp
, *new_tp
= NULL
;
595 fasttrap_bucket_t
*bucket
;
600 ASSERT(index
< probe
->ftp_ntps
);
602 pid
= probe
->ftp_pid
;
603 pc
= probe
->ftp_tps
[index
].fit_tp
->ftt_pc
;
604 id
= &probe
->ftp_tps
[index
].fit_id
;
606 ASSERT(probe
->ftp_tps
[index
].fit_tp
->ftt_pid
== pid
);
608 //ASSERT(!(p->p_flag & SVFORK));
611 * Before we make any modifications, make sure we've imposed a barrier
612 * on the generation in which this probe was last modified.
614 fasttrap_mod_barrier(probe
->ftp_gen
);
616 bucket
= &fasttrap_tpoints
.fth_table
[FASTTRAP_TPOINTS_INDEX(pid
, pc
)];
619 * If the tracepoint has already been enabled, just add our id to the
620 * list of interested probes. This may be our second time through
621 * this path in which case we'll have constructed the tracepoint we'd
622 * like to install. If we can't find a match, and have an allocated
623 * tracepoint ready to go, enable that one now.
625 * A tracepoint whose process is defunct is also considered defunct.
628 lck_mtx_lock(&bucket
->ftb_mtx
);
629 for (tp
= bucket
->ftb_data
; tp
!= NULL
; tp
= tp
->ftt_next
) {
631 * Note that it's safe to access the active count on the
632 * associated proc structure because we know that at least one
633 * provider (this one) will still be around throughout this
636 if (tp
->ftt_pid
!= pid
|| tp
->ftt_pc
!= pc
||
637 tp
->ftt_proc
->ftpc_acount
== 0)
641 * Now that we've found a matching tracepoint, it would be
642 * a decent idea to confirm that the tracepoint is still
643 * enabled and the trap instruction hasn't been overwritten.
644 * Since this is a little hairy, we'll punt for now.
648 * This can't be the first interested probe. We don't have
649 * to worry about another thread being in the midst of
650 * deleting this tracepoint (which would be the only valid
651 * reason for a tracepoint to have no interested probes)
652 * since we're holding P_PR_LOCK for this process.
654 ASSERT(tp
->ftt_ids
!= NULL
|| tp
->ftt_retids
!= NULL
);
656 switch (id
->fti_ptype
) {
659 case DTFTP_IS_ENABLED
:
660 id
->fti_next
= tp
->ftt_ids
;
661 dtrace_membar_producer();
663 dtrace_membar_producer();
667 case DTFTP_POST_OFFSETS
:
668 id
->fti_next
= tp
->ftt_retids
;
669 dtrace_membar_producer();
671 dtrace_membar_producer();
678 lck_mtx_unlock(&bucket
->ftb_mtx
);
680 if (new_tp
!= NULL
) {
681 new_tp
->ftt_ids
= NULL
;
682 new_tp
->ftt_retids
= NULL
;
689 * If we have a good tracepoint ready to go, install it now while
690 * we have the lock held and no one can screw with us.
692 if (new_tp
!= NULL
) {
695 new_tp
->ftt_next
= bucket
->ftb_data
;
696 dtrace_membar_producer();
697 bucket
->ftb_data
= new_tp
;
698 dtrace_membar_producer();
699 lck_mtx_unlock(&bucket
->ftb_mtx
);
702 * Activate the tracepoint in the ISA-specific manner.
703 * If this fails, we need to report the failure, but
704 * indicate that this tracepoint must still be disabled
705 * by calling fasttrap_tracepoint_disable().
707 if (fasttrap_tracepoint_install(p
, new_tp
) != 0)
708 rc
= FASTTRAP_ENABLE_PARTIAL
;
711 * Increment the count of the number of tracepoints active in
712 * the victim process.
714 //ASSERT(p->p_proc_flag & P_PR_LOCK);
720 lck_mtx_unlock(&bucket
->ftb_mtx
);
723 * Initialize the tracepoint that's been preallocated with the probe.
725 new_tp
= probe
->ftp_tps
[index
].fit_tp
;
727 ASSERT(new_tp
->ftt_pid
== pid
);
728 ASSERT(new_tp
->ftt_pc
== pc
);
729 ASSERT(new_tp
->ftt_proc
== probe
->ftp_prov
->ftp_proc
);
730 ASSERT(new_tp
->ftt_ids
== NULL
);
731 ASSERT(new_tp
->ftt_retids
== NULL
);
733 switch (id
->fti_ptype
) {
736 case DTFTP_IS_ENABLED
:
738 new_tp
->ftt_ids
= id
;
742 case DTFTP_POST_OFFSETS
:
744 new_tp
->ftt_retids
= id
;
752 * If the ISA-dependent initialization goes to plan, go back to the
753 * beginning and try to install this freshly made tracepoint.
755 if (fasttrap_tracepoint_init(p
, new_tp
, pc
, id
->fti_ptype
) == 0)
758 new_tp
->ftt_ids
= NULL
;
759 new_tp
->ftt_retids
= NULL
;
761 return (FASTTRAP_ENABLE_FAIL
);
765 fasttrap_tracepoint_disable(proc_t
*p
, fasttrap_probe_t
*probe
, uint_t index
)
767 fasttrap_bucket_t
*bucket
;
768 fasttrap_provider_t
*provider
= probe
->ftp_prov
;
769 fasttrap_tracepoint_t
**pp
, *tp
;
770 fasttrap_id_t
*id
, **idp
;
774 ASSERT(index
< probe
->ftp_ntps
);
776 pid
= probe
->ftp_pid
;
777 pc
= probe
->ftp_tps
[index
].fit_tp
->ftt_pc
;
778 id
= &probe
->ftp_tps
[index
].fit_id
;
780 ASSERT(probe
->ftp_tps
[index
].fit_tp
->ftt_pid
== pid
);
783 * Find the tracepoint and make sure that our id is one of the
784 * ones registered with it.
786 bucket
= &fasttrap_tpoints
.fth_table
[FASTTRAP_TPOINTS_INDEX(pid
, pc
)];
787 lck_mtx_lock(&bucket
->ftb_mtx
);
788 for (tp
= bucket
->ftb_data
; tp
!= NULL
; tp
= tp
->ftt_next
) {
789 if (tp
->ftt_pid
== pid
&& tp
->ftt_pc
== pc
&&
790 tp
->ftt_proc
== provider
->ftp_proc
)
795 * If we somehow lost this tracepoint, we're in a world of hurt.
799 switch (id
->fti_ptype
) {
802 case DTFTP_IS_ENABLED
:
803 ASSERT(tp
->ftt_ids
!= NULL
);
808 case DTFTP_POST_OFFSETS
:
809 ASSERT(tp
->ftt_retids
!= NULL
);
810 idp
= &tp
->ftt_retids
;
814 /* Fix compiler warning... */
819 while ((*idp
)->fti_probe
!= probe
) {
820 idp
= &(*idp
)->fti_next
;
821 ASSERT(*idp
!= NULL
);
826 dtrace_membar_producer();
828 ASSERT(id
->fti_probe
== probe
);
831 * If there are other registered enablings of this tracepoint, we're
832 * all done, but if this was the last probe assocated with this
833 * this tracepoint, we need to remove and free it.
835 if (tp
->ftt_ids
!= NULL
|| tp
->ftt_retids
!= NULL
) {
838 * If the current probe's tracepoint is in use, swap it
839 * for an unused tracepoint.
841 if (tp
== probe
->ftp_tps
[index
].fit_tp
) {
842 fasttrap_probe_t
*tmp_probe
;
843 fasttrap_tracepoint_t
**tmp_tp
;
846 if (tp
->ftt_ids
!= NULL
) {
847 tmp_probe
= tp
->ftt_ids
->fti_probe
;
848 /* LINTED - alignment */
849 tmp_index
= FASTTRAP_ID_INDEX(tp
->ftt_ids
);
850 tmp_tp
= &tmp_probe
->ftp_tps
[tmp_index
].fit_tp
;
852 tmp_probe
= tp
->ftt_retids
->fti_probe
;
853 /* LINTED - alignment */
854 tmp_index
= FASTTRAP_ID_INDEX(tp
->ftt_retids
);
855 tmp_tp
= &tmp_probe
->ftp_tps
[tmp_index
].fit_tp
;
858 ASSERT(*tmp_tp
!= NULL
);
859 ASSERT(*tmp_tp
!= probe
->ftp_tps
[index
].fit_tp
);
860 ASSERT((*tmp_tp
)->ftt_ids
== NULL
);
861 ASSERT((*tmp_tp
)->ftt_retids
== NULL
);
863 probe
->ftp_tps
[index
].fit_tp
= *tmp_tp
;
868 lck_mtx_unlock(&bucket
->ftb_mtx
);
871 * Tag the modified probe with the generation in which it was
874 probe
->ftp_gen
= fasttrap_mod_gen
;
878 lck_mtx_unlock(&bucket
->ftb_mtx
);
881 * We can't safely remove the tracepoint from the set of active
882 * tracepoints until we've actually removed the fasttrap instruction
883 * from the process's text. We can, however, operate on this
884 * tracepoint secure in the knowledge that no other thread is going to
885 * be looking at it since we hold P_PR_LOCK on the process if it's
886 * live or we hold the provider lock on the process if it's dead and
891 * We only need to remove the actual instruction if we're looking
892 * at an existing process
896 * If we fail to restore the instruction we need to kill
897 * this process since it's in a completely unrecoverable
900 if (fasttrap_tracepoint_remove(p
, tp
) != 0)
901 fasttrap_sigtrap(p
, NULL
, pc
);
904 * Decrement the count of the number of tracepoints active
905 * in the victim process.
907 //ASSERT(p->p_proc_flag & P_PR_LOCK);
912 * Remove the probe from the hash table of active tracepoints.
914 lck_mtx_lock(&bucket
->ftb_mtx
);
915 pp
= (fasttrap_tracepoint_t
**)&bucket
->ftb_data
;
918 pp
= &(*pp
)->ftt_next
;
923 dtrace_membar_producer();
925 lck_mtx_unlock(&bucket
->ftb_mtx
);
928 * Tag the modified probe with the generation in which it was changed.
930 probe
->ftp_gen
= fasttrap_mod_gen
;
934 fasttrap_enable_callbacks(void)
937 * We don't have to play the rw lock game here because we're
938 * providing something rather than taking something away --
939 * we can be sure that no threads have tried to follow this
940 * function pointer yet.
942 lck_mtx_lock(&fasttrap_count_mtx
);
943 if (fasttrap_pid_count
== 0) {
944 ASSERT(dtrace_pid_probe_ptr
== NULL
);
945 ASSERT(dtrace_return_probe_ptr
== NULL
);
946 dtrace_pid_probe_ptr
= &fasttrap_pid_probe
;
947 dtrace_return_probe_ptr
= &fasttrap_return_probe
;
949 ASSERT(dtrace_pid_probe_ptr
== &fasttrap_pid_probe
);
950 ASSERT(dtrace_return_probe_ptr
== &fasttrap_return_probe
);
951 fasttrap_pid_count
++;
952 lck_mtx_unlock(&fasttrap_count_mtx
);
956 fasttrap_disable_callbacks(void)
958 //ASSERT(MUTEX_HELD(&cpu_lock));
960 lck_mtx_lock(&fasttrap_count_mtx
);
961 ASSERT(fasttrap_pid_count
> 0);
962 fasttrap_pid_count
--;
963 if (fasttrap_pid_count
== 0) {
964 cpu_t
*cur
, *cpu
= CPU
;
967 * APPLE NOTE: This loop seems broken, it touches every CPU
968 * but the one we're actually running on. Need to ask Sun folks
969 * if that is safe. Scenario is this: We're running on CPU A,
970 * and lock all but A. Then we get preempted, and start running
971 * on CPU B. A probe fires on A, and is allowed to enter. BOOM!
973 for (cur
= cpu
->cpu_next
; cur
!= cpu
; cur
= cur
->cpu_next
) {
974 lck_rw_lock_exclusive(&cur
->cpu_ft_lock
);
975 // rw_enter(&cur->cpu_ft_lock, RW_WRITER);
978 dtrace_pid_probe_ptr
= NULL
;
979 dtrace_return_probe_ptr
= NULL
;
981 for (cur
= cpu
->cpu_next
; cur
!= cpu
; cur
= cur
->cpu_next
) {
982 lck_rw_unlock_exclusive(&cur
->cpu_ft_lock
);
983 // rw_exit(&cur->cpu_ft_lock);
986 lck_mtx_unlock(&fasttrap_count_mtx
);
991 fasttrap_pid_enable(void *arg
, dtrace_id_t id
, void *parg
)
993 #pragma unused(arg, id)
994 fasttrap_probe_t
*probe
= parg
;
998 ASSERT(probe
!= NULL
);
999 ASSERT(!probe
->ftp_enabled
);
1000 ASSERT(id
== probe
->ftp_id
);
1001 // ASSERT(MUTEX_HELD(&cpu_lock));
1004 * Increment the count of enabled probes on this probe's provider;
1005 * the provider can't go away while the probe still exists. We
1006 * must increment this even if we aren't able to properly enable
1009 lck_mtx_lock(&probe
->ftp_prov
->ftp_mtx
);
1010 probe
->ftp_prov
->ftp_rcount
++;
1011 lck_mtx_unlock(&probe
->ftp_prov
->ftp_mtx
);
1014 * If this probe's provider is retired (meaning it was valid in a
1015 * previously exec'ed incarnation of this address space), bail out. The
1016 * provider can't go away while we're in this code path.
1018 if (probe
->ftp_prov
->ftp_retired
)
1022 * If we can't find the process, it may be that we're in the context of
1023 * a fork in which the traced process is being born and we're copying
1024 * USDT probes. Otherwise, the process is gone so bail.
1026 if ((p
= sprlock(probe
->ftp_pid
)) == PROC_NULL
) {
1027 #if defined(__APPLE__)
1029 * APPLE NOTE: We should never end up here. The Solaris sprlock()
1030 * does not return process's with SIDL set, but we always return
1031 * the child process.
1036 if ((curproc
->p_flag
& SFORKING
) == 0)
1039 lck_mtx_lock(&pidlock
);
1040 p
= prfind(probe
->ftp_pid
);
1043 * Confirm that curproc is indeed forking the process in which
1044 * we're trying to enable probes.
1047 //ASSERT(p->p_parent == curproc);
1048 ASSERT(p
->p_stat
== SIDL
);
1050 lck_mtx_lock(&p
->p_lock
);
1051 lck_mtx_unlock(&pidlock
);
1058 * APPLE NOTE: We do not have an equivalent thread structure to Solaris.
1059 * Solaris uses its ulwp_t struct for scratch space to support the pid provider.
1060 * To mimic this, we allocate on demand scratch space. If this is the first
1061 * time a probe has been enabled in this process, we need to allocate scratch
1062 * space for each already existing thread. Now is a good time to do this, as
1063 * the target process is suspended and the proc_lock is held.
1065 if (p
->p_dtrace_ptss_pages
== NULL
) {
1066 dtrace_ptss_enable(p
);
1069 // ASSERT(!(p->p_flag & SVFORK));
1073 * We have to enable the trap entry point before any user threads have
1074 * the chance to execute the trap instruction we're about to place
1075 * in their process's text.
1077 fasttrap_enable_callbacks();
1080 * Enable all the tracepoints and add this probe's id to each
1081 * tracepoint's list of active probes.
1083 for (i
= 0; i
< (int)probe
->ftp_ntps
; i
++) {
1084 if ((rc
= fasttrap_tracepoint_enable(p
, probe
, i
)) != 0) {
1086 * If enabling the tracepoint failed completely,
1087 * we don't have to disable it; if the failure
1088 * was only partial we must disable it.
1090 if (rc
== FASTTRAP_ENABLE_FAIL
)
1093 ASSERT(rc
== FASTTRAP_ENABLE_PARTIAL
);
1096 * Back up and pull out all the tracepoints we've
1097 * created so far for this probe.
1100 fasttrap_tracepoint_disable(p
, probe
, i
);
1108 * Since we're not actually enabling this probe,
1109 * drop our reference on the trap table entry.
1111 fasttrap_disable_callbacks();
1119 probe
->ftp_enabled
= 1;
1124 fasttrap_pid_disable(void *arg
, dtrace_id_t id
, void *parg
)
1126 #pragma unused(arg, id)
1127 fasttrap_probe_t
*probe
= parg
;
1128 fasttrap_provider_t
*provider
= probe
->ftp_prov
;
1132 ASSERT(id
== probe
->ftp_id
);
1135 * We won't be able to acquire a /proc-esque lock on the process
1136 * iff the process is dead and gone. In this case, we rely on the
1137 * provider lock as a point of mutual exclusion to prevent other
1138 * DTrace consumers from disabling this probe.
1140 if ((p
= sprlock(probe
->ftp_pid
)) != PROC_NULL
) {
1141 // ASSERT(!(p->p_flag & SVFORK));
1145 lck_mtx_lock(&provider
->ftp_mtx
);
1148 * Disable all the associated tracepoints (for fully enabled probes).
1150 if (probe
->ftp_enabled
) {
1151 for (i
= 0; i
< (int)probe
->ftp_ntps
; i
++) {
1152 fasttrap_tracepoint_disable(p
, probe
, i
);
1156 ASSERT(provider
->ftp_rcount
> 0);
1157 provider
->ftp_rcount
--;
1161 * Even though we may not be able to remove it entirely, we
1162 * mark this retired provider to get a chance to remove some
1163 * of the associated probes.
1165 if (provider
->ftp_retired
&& !provider
->ftp_marked
)
1166 whack
= provider
->ftp_marked
= 1;
1167 lck_mtx_unlock(&provider
->ftp_mtx
);
1173 * If the process is dead, we're just waiting for the
1174 * last probe to be disabled to be able to free it.
1176 if (provider
->ftp_rcount
== 0 && !provider
->ftp_marked
)
1177 whack
= provider
->ftp_marked
= 1;
1178 lck_mtx_unlock(&provider
->ftp_mtx
);
1182 fasttrap_pid_cleanup();
1184 if (!probe
->ftp_enabled
)
1187 probe
->ftp_enabled
= 0;
1189 // ASSERT(MUTEX_HELD(&cpu_lock));
1190 fasttrap_disable_callbacks();
1195 fasttrap_pid_getargdesc(void *arg
, dtrace_id_t id
, void *parg
,
1196 dtrace_argdesc_t
*desc
)
1198 #pragma unused(arg, id)
1199 fasttrap_probe_t
*probe
= parg
;
1203 desc
->dtargd_native
[0] = '\0';
1204 desc
->dtargd_xlate
[0] = '\0';
1206 if (probe
->ftp_prov
->ftp_retired
!= 0 ||
1207 desc
->dtargd_ndx
>= probe
->ftp_nargs
) {
1208 desc
->dtargd_ndx
= DTRACE_ARGNONE
;
1212 ndx
= (probe
->ftp_argmap
!= NULL
) ?
1213 probe
->ftp_argmap
[desc
->dtargd_ndx
] : desc
->dtargd_ndx
;
1215 str
= probe
->ftp_ntypes
;
1216 for (i
= 0; i
< ndx
; i
++) {
1217 str
+= strlen(str
) + 1;
1220 (void) strlcpy(desc
->dtargd_native
, str
, sizeof(desc
->dtargd_native
));
1222 if (probe
->ftp_xtypes
== NULL
)
1225 str
= probe
->ftp_xtypes
;
1226 for (i
= 0; i
< desc
->dtargd_ndx
; i
++) {
1227 str
+= strlen(str
) + 1;
1230 (void) strlcpy(desc
->dtargd_xlate
, str
, sizeof(desc
->dtargd_xlate
));
1235 fasttrap_pid_destroy(void *arg
, dtrace_id_t id
, void *parg
)
1237 #pragma unused(arg, id)
1238 fasttrap_probe_t
*probe
= parg
;
1241 ASSERT(probe
!= NULL
);
1242 ASSERT(!probe
->ftp_enabled
);
1243 ASSERT(fasttrap_total
>= probe
->ftp_ntps
);
1245 atomic_add_32(&fasttrap_total
, -probe
->ftp_ntps
);
1246 #if !defined(__APPLE__)
1247 size_t size
= offsetof(fasttrap_probe_t
, ftp_tps
[probe
->ftp_ntps
]);
1250 if (probe
->ftp_gen
+ 1 >= fasttrap_mod_gen
)
1251 fasttrap_mod_barrier(probe
->ftp_gen
);
1253 for (i
= 0; i
< probe
->ftp_ntps
; i
++) {
1254 #if !defined(__APPLE__)
1255 kmem_free(probe
->ftp_tps
[i
].fit_tp
, sizeof (fasttrap_tracepoint_t
));
1257 zfree(fasttrap_tracepoint_t_zone
, probe
->ftp_tps
[i
].fit_tp
);
1261 #if !defined(__APPLE__)
1262 kmem_free(probe
, size
);
1264 if (probe
->ftp_ntps
< FASTTRAP_PROBE_T_ZONE_MAX_TRACEPOINTS
) {
1265 zfree(fasttrap_probe_t_zones
[probe
->ftp_ntps
], probe
);
1267 size_t size
= offsetof(fasttrap_probe_t
, ftp_tps
[probe
->ftp_ntps
]);
1268 kmem_free(probe
, size
);
1274 static const dtrace_pattr_t pid_attr
= {
1275 { DTRACE_STABILITY_EVOLVING
, DTRACE_STABILITY_EVOLVING
, DTRACE_CLASS_ISA
},
1276 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_UNKNOWN
},
1277 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_UNKNOWN
},
1278 { DTRACE_STABILITY_EVOLVING
, DTRACE_STABILITY_EVOLVING
, DTRACE_CLASS_ISA
},
1279 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_UNKNOWN
},
1282 static dtrace_pops_t pid_pops
= {
1283 fasttrap_pid_provide
,
1285 fasttrap_pid_enable
,
1286 fasttrap_pid_disable
,
1289 fasttrap_pid_getargdesc
,
1290 fasttrap_pid_getarg
,
1292 fasttrap_pid_destroy
1295 static dtrace_pops_t usdt_pops
= {
1296 fasttrap_pid_provide
,
1298 fasttrap_pid_enable
,
1299 fasttrap_pid_disable
,
1302 fasttrap_pid_getargdesc
,
1303 fasttrap_usdt_getarg
,
1305 fasttrap_pid_destroy
1308 static fasttrap_proc_t
*
1309 fasttrap_proc_lookup(pid_t pid
)
1311 fasttrap_bucket_t
*bucket
;
1312 fasttrap_proc_t
*fprc
, *new_fprc
;
1314 bucket
= &fasttrap_procs
.fth_table
[FASTTRAP_PROCS_INDEX(pid
)];
1315 lck_mtx_lock(&bucket
->ftb_mtx
);
1317 for (fprc
= bucket
->ftb_data
; fprc
!= NULL
; fprc
= fprc
->ftpc_next
) {
1318 if (fprc
->ftpc_pid
== pid
&& fprc
->ftpc_acount
!= 0) {
1319 lck_mtx_lock(&fprc
->ftpc_mtx
);
1320 lck_mtx_unlock(&bucket
->ftb_mtx
);
1321 fprc
->ftpc_rcount
++;
1322 atomic_add_64(&fprc
->ftpc_acount
, 1);
1323 ASSERT(fprc
->ftpc_acount
<= fprc
->ftpc_rcount
);
1324 lck_mtx_unlock(&fprc
->ftpc_mtx
);
1331 * Drop the bucket lock so we don't try to perform a sleeping
1332 * allocation under it.
1334 lck_mtx_unlock(&bucket
->ftb_mtx
);
1336 new_fprc
= kmem_zalloc(sizeof (fasttrap_proc_t
), KM_SLEEP
);
1337 ASSERT(new_fprc
!= NULL
);
1338 new_fprc
->ftpc_pid
= pid
;
1339 new_fprc
->ftpc_rcount
= 1;
1340 new_fprc
->ftpc_acount
= 1;
1342 lck_mtx_lock(&bucket
->ftb_mtx
);
1345 * Take another lap through the list to make sure a proc hasn't
1346 * been created for this pid while we weren't under the bucket lock.
1348 for (fprc
= bucket
->ftb_data
; fprc
!= NULL
; fprc
= fprc
->ftpc_next
) {
1349 if (fprc
->ftpc_pid
== pid
&& fprc
->ftpc_acount
!= 0) {
1350 lck_mtx_lock(&fprc
->ftpc_mtx
);
1351 lck_mtx_unlock(&bucket
->ftb_mtx
);
1352 fprc
->ftpc_rcount
++;
1353 atomic_add_64(&fprc
->ftpc_acount
, 1);
1354 ASSERT(fprc
->ftpc_acount
<= fprc
->ftpc_rcount
);
1355 lck_mtx_unlock(&fprc
->ftpc_mtx
);
1357 kmem_free(new_fprc
, sizeof (fasttrap_proc_t
));
1363 #if defined(__APPLE__)
1365 * We have to initialize all locks explicitly
1367 lck_mtx_init(&new_fprc
->ftpc_mtx
, fasttrap_lck_grp
, fasttrap_lck_attr
);
1370 new_fprc
->ftpc_next
= bucket
->ftb_data
;
1371 bucket
->ftb_data
= new_fprc
;
1373 lck_mtx_unlock(&bucket
->ftb_mtx
);
1379 fasttrap_proc_release(fasttrap_proc_t
*proc
)
1381 fasttrap_bucket_t
*bucket
;
1382 fasttrap_proc_t
*fprc
, **fprcp
;
1383 pid_t pid
= proc
->ftpc_pid
;
1385 lck_mtx_lock(&proc
->ftpc_mtx
);
1387 ASSERT(proc
->ftpc_rcount
!= 0);
1388 ASSERT(proc
->ftpc_acount
<= proc
->ftpc_rcount
);
1390 if (--proc
->ftpc_rcount
!= 0) {
1391 lck_mtx_unlock(&proc
->ftpc_mtx
);
1395 lck_mtx_unlock(&proc
->ftpc_mtx
);
1398 * There should definitely be no live providers associated with this
1399 * process at this point.
1401 ASSERT(proc
->ftpc_acount
== 0);
1403 bucket
= &fasttrap_procs
.fth_table
[FASTTRAP_PROCS_INDEX(pid
)];
1404 lck_mtx_lock(&bucket
->ftb_mtx
);
1406 fprcp
= (fasttrap_proc_t
**)&bucket
->ftb_data
;
1407 while ((fprc
= *fprcp
) != NULL
) {
1411 fprcp
= &fprc
->ftpc_next
;
1415 * Something strange has happened if we can't find the proc.
1417 ASSERT(fprc
!= NULL
);
1419 *fprcp
= fprc
->ftpc_next
;
1421 lck_mtx_unlock(&bucket
->ftb_mtx
);
1423 #if defined(__APPLE__)
1425 * Apple explicit lock management. Not 100% certain we need this, the
1426 * memory is freed even without the destroy. Maybe accounting cleanup?
1428 lck_mtx_destroy(&fprc
->ftpc_mtx
, fasttrap_lck_grp
);
1431 kmem_free(fprc
, sizeof (fasttrap_proc_t
));
1435 * Lookup a fasttrap-managed provider based on its name and associated pid.
1436 * If the pattr argument is non-NULL, this function instantiates the provider
1437 * if it doesn't exist otherwise it returns NULL. The provider is returned
1438 * with its lock held.
1440 #if defined(__APPLE__)
1441 static fasttrap_provider_t
*
1442 fasttrap_provider_lookup(pid_t pid
, fasttrap_provider_type_t provider_type
, const char *name
,
1443 const dtrace_pattr_t
*pattr
)
1444 #endif /* __APPLE__ */
1446 fasttrap_provider_t
*fp
, *new_fp
= NULL
;
1447 fasttrap_bucket_t
*bucket
;
1448 char provname
[DTRACE_PROVNAMELEN
];
1452 ASSERT(strlen(name
) < sizeof (fp
->ftp_name
));
1453 ASSERT(pattr
!= NULL
);
1455 bucket
= &fasttrap_provs
.fth_table
[FASTTRAP_PROVS_INDEX(pid
, name
)];
1456 lck_mtx_lock(&bucket
->ftb_mtx
);
1459 * Take a lap through the list and return the match if we find it.
1461 for (fp
= bucket
->ftb_data
; fp
!= NULL
; fp
= fp
->ftp_next
) {
1462 if (fp
->ftp_pid
== pid
&&
1463 #if defined(__APPLE__)
1464 fp
->ftp_provider_type
== provider_type
&&
1465 #endif /* __APPLE__ */
1466 strncmp(fp
->ftp_name
, name
, sizeof(fp
->ftp_name
)) == 0 &&
1468 lck_mtx_lock(&fp
->ftp_mtx
);
1469 lck_mtx_unlock(&bucket
->ftb_mtx
);
1475 * Drop the bucket lock so we don't try to perform a sleeping
1476 * allocation under it.
1478 lck_mtx_unlock(&bucket
->ftb_mtx
);
1481 * Make sure the process exists, isn't a child created as the result
1482 * of a vfork(2), and isn't a zombie (but may be in fork).
1484 if ((p
= proc_find(pid
)) == NULL
) {
1488 if (p
->p_lflag
& (P_LINVFORK
| P_LEXIT
)) {
1495 * Increment p_dtrace_probes so that the process knows to inform us
1496 * when it exits or execs. fasttrap_provider_free() decrements this
1497 * when we're done with this provider.
1499 p
->p_dtrace_probes
++;
1502 * Grab the credentials for this process so we have
1503 * something to pass to dtrace_register().
1505 #if !defined(__APPLE__)
1506 mutex_enter(&p
->p_crlock
);
1509 mutex_exit(&p
->p_crlock
);
1510 mutex_exit(&p
->p_lock
);
1512 // lck_mtx_lock(&p->p_crlock);
1513 // Seems like OS X has no equivalent to crhold, even though it has a cr_ref field in ucred
1516 // lck_mtx_unlock(&p->p_crlock);
1519 #endif /* __APPLE__ */
1521 new_fp
= kmem_zalloc(sizeof (fasttrap_provider_t
), KM_SLEEP
);
1522 ASSERT(new_fp
!= NULL
);
1523 new_fp
->ftp_pid
= pid
;
1524 new_fp
->ftp_proc
= fasttrap_proc_lookup(pid
);
1525 #if defined(__APPLE__)
1526 new_fp
->ftp_provider_type
= provider_type
;
1529 * Apple locks require explicit init.
1531 lck_mtx_init(&new_fp
->ftp_mtx
, fasttrap_lck_grp
, fasttrap_lck_attr
);
1532 lck_mtx_init(&new_fp
->ftp_cmtx
, fasttrap_lck_grp
, fasttrap_lck_attr
);
1533 #endif /* __APPLE__ */
1535 ASSERT(new_fp
->ftp_proc
!= NULL
);
1537 lck_mtx_lock(&bucket
->ftb_mtx
);
1540 * Take another lap through the list to make sure a provider hasn't
1541 * been created for this pid while we weren't under the bucket lock.
1543 for (fp
= bucket
->ftb_data
; fp
!= NULL
; fp
= fp
->ftp_next
) {
1544 if (fp
->ftp_pid
== pid
&& strncmp(fp
->ftp_name
, name
, sizeof(fp
->ftp_name
)) == 0 &&
1546 lck_mtx_lock(&fp
->ftp_mtx
);
1547 lck_mtx_unlock(&bucket
->ftb_mtx
);
1548 fasttrap_provider_free(new_fp
);
1554 (void) strlcpy(new_fp
->ftp_name
, name
, sizeof(new_fp
->ftp_name
));
1557 * Fail and return NULL if either the provider name is too long
1558 * or we fail to register this new provider with the DTrace
1559 * framework. Note that this is the only place we ever construct
1560 * the full provider name -- we keep it in pieces in the provider
1563 if (snprintf(provname
, sizeof (provname
), "%s%u", name
, (uint_t
)pid
) >=
1564 (int)sizeof (provname
) ||
1565 dtrace_register(provname
, pattr
,
1566 DTRACE_PRIV_PROC
| DTRACE_PRIV_OWNER
| DTRACE_PRIV_ZONEOWNER
, cred
,
1567 pattr
== &pid_attr
? &pid_pops
: &usdt_pops
, new_fp
,
1568 &new_fp
->ftp_provid
) != 0) {
1569 lck_mtx_unlock(&bucket
->ftb_mtx
);
1570 fasttrap_provider_free(new_fp
);
1575 new_fp
->ftp_next
= bucket
->ftb_data
;
1576 bucket
->ftb_data
= new_fp
;
1578 lck_mtx_lock(&new_fp
->ftp_mtx
);
1579 lck_mtx_unlock(&bucket
->ftb_mtx
);
1586 fasttrap_provider_free(fasttrap_provider_t
*provider
)
1588 pid_t pid
= provider
->ftp_pid
;
1592 * There need to be no associated enabled probes, no consumers
1593 * creating probes, and no meta providers referencing this provider.
1595 ASSERT(provider
->ftp_rcount
== 0);
1596 ASSERT(provider
->ftp_ccount
== 0);
1597 ASSERT(provider
->ftp_mcount
== 0);
1600 * If this provider hasn't been retired, we need to explicitly drop the
1601 * count of active providers on the associated process structure.
1603 if (!provider
->ftp_retired
) {
1604 atomic_add_64(&provider
->ftp_proc
->ftpc_acount
, -1);
1605 ASSERT(provider
->ftp_proc
->ftpc_acount
<
1606 provider
->ftp_proc
->ftpc_rcount
);
1609 fasttrap_proc_release(provider
->ftp_proc
);
1611 #if defined(__APPLE__)
1613 * Apple explicit lock management. Not 100% certain we need this, the
1614 * memory is freed even without the destroy. Maybe accounting cleanup?
1616 lck_mtx_destroy(&provider
->ftp_mtx
, fasttrap_lck_grp
);
1617 lck_mtx_destroy(&provider
->ftp_cmtx
, fasttrap_lck_grp
);
1620 kmem_free(provider
, sizeof (fasttrap_provider_t
));
1623 * Decrement p_dtrace_probes on the process whose provider we're
1624 * freeing. We don't have to worry about clobbering somone else's
1625 * modifications to it because we have locked the bucket that
1626 * corresponds to this process's hash chain in the provider hash
1627 * table. Don't sweat it if we can't find the process.
1629 if ((p
= proc_find(pid
)) == NULL
) {
1634 p
->p_dtrace_probes
--;
1641 fasttrap_provider_retire(pid_t pid
, const char *name
, int mprov
)
1643 fasttrap_provider_t
*fp
;
1644 fasttrap_bucket_t
*bucket
;
1645 dtrace_provider_id_t provid
;
1647 ASSERT(strlen(name
) < sizeof (fp
->ftp_name
));
1649 bucket
= &fasttrap_provs
.fth_table
[FASTTRAP_PROVS_INDEX(pid
, name
)];
1650 lck_mtx_lock(&bucket
->ftb_mtx
);
1652 for (fp
= bucket
->ftb_data
; fp
!= NULL
; fp
= fp
->ftp_next
) {
1653 if (fp
->ftp_pid
== pid
&& strncmp(fp
->ftp_name
, name
, sizeof(fp
->ftp_name
)) == 0 &&
1659 lck_mtx_unlock(&bucket
->ftb_mtx
);
1663 lck_mtx_lock(&fp
->ftp_mtx
);
1664 ASSERT(!mprov
|| fp
->ftp_mcount
> 0);
1665 if (mprov
&& --fp
->ftp_mcount
!= 0) {
1666 lck_mtx_unlock(&fp
->ftp_mtx
);
1667 lck_mtx_unlock(&bucket
->ftb_mtx
);
1672 * Mark the provider to be removed in our post-processing step, mark it
1673 * retired, and drop the active count on its proc. Marking it indicates
1674 * that we should try to remove it; setting the retired flag indicates
1675 * that we're done with this provider; dropping the active the proc
1676 * releases our hold, and when this reaches zero (as it will during
1677 * exit or exec) the proc and associated providers become defunct.
1679 * We obviously need to take the bucket lock before the provider lock
1680 * to perform the lookup, but we need to drop the provider lock
1681 * before calling into the DTrace framework since we acquire the
1682 * provider lock in callbacks invoked from the DTrace framework. The
1683 * bucket lock therefore protects the integrity of the provider hash
1686 atomic_add_64(&fp
->ftp_proc
->ftpc_acount
, -1);
1687 ASSERT(fp
->ftp_proc
->ftpc_acount
< fp
->ftp_proc
->ftpc_rcount
);
1689 fp
->ftp_retired
= 1;
1691 provid
= fp
->ftp_provid
;
1692 lck_mtx_unlock(&fp
->ftp_mtx
);
1695 * We don't have to worry about invalidating the same provider twice
1696 * since fasttrap_provider_lookup() will ignore provider that have
1697 * been marked as retired.
1699 dtrace_invalidate(provid
);
1701 lck_mtx_unlock(&bucket
->ftb_mtx
);
1703 fasttrap_pid_cleanup();
1707 fasttrap_uint32_cmp(const void *ap
, const void *bp
)
1709 return (*(const uint32_t *)ap
- *(const uint32_t *)bp
);
1713 fasttrap_uint64_cmp(const void *ap
, const void *bp
)
1715 return (*(const uint64_t *)ap
- *(const uint64_t *)bp
);
1719 fasttrap_add_probe(fasttrap_probe_spec_t
*pdata
)
1721 fasttrap_provider_t
*provider
;
1722 fasttrap_probe_t
*pp
;
1723 fasttrap_tracepoint_t
*tp
;
1725 unsigned int i
, aframes
, whack
;
1728 * There needs to be at least one desired trace point.
1730 if (pdata
->ftps_noffs
== 0)
1733 #if defined(__APPLE__)
1734 switch (pdata
->ftps_probe_type
) {
1738 aframes
= FASTTRAP_ENTRY_AFRAMES
;
1742 aframes
= FASTTRAP_RETURN_AFRAMES
;
1752 #if defined(__APPLE__)
1753 const char* provider_name
;
1754 switch (pdata
->ftps_provider_type
) {
1755 case DTFTP_PROVIDER_PID
:
1756 provider_name
= FASTTRAP_PID_NAME
;
1758 case DTFTP_PROVIDER_OBJC
:
1759 provider_name
= FASTTRAP_OBJC_NAME
;
1761 case DTFTP_PROVIDER_ONESHOT
:
1762 provider_name
= FASTTRAP_ONESHOT_NAME
;
1768 if ((provider
= fasttrap_provider_lookup(pdata
->ftps_pid
, pdata
->ftps_provider_type
,
1769 provider_name
, &pid_attr
)) == NULL
)
1771 #endif /* __APPLE__ */
1774 * Increment this reference count to indicate that a consumer is
1775 * actively adding a new probe associated with this provider. This
1776 * prevents the provider from being deleted -- we'll need to check
1777 * for pending deletions when we drop this reference count.
1779 provider
->ftp_ccount
++;
1780 lck_mtx_unlock(&provider
->ftp_mtx
);
1783 * Grab the creation lock to ensure consistency between calls to
1784 * dtrace_probe_lookup() and dtrace_probe_create() in the face of
1785 * other threads creating probes. We must drop the provider lock
1786 * before taking this lock to avoid a three-way deadlock with the
1789 lck_mtx_lock(&provider
->ftp_cmtx
);
1792 for (i
= 0; i
< pdata
->ftps_noffs
; i
++) {
1795 (void) snprintf(name_str
, sizeof(name_str
), "%llx",
1796 (uint64_t)pdata
->ftps_offs
[i
]);
1798 if (dtrace_probe_lookup(provider
->ftp_provid
,
1799 pdata
->ftps_mod
, pdata
->ftps_func
, name_str
) != 0)
1802 atomic_add_32(&fasttrap_total
, 1);
1804 if (fasttrap_total
> fasttrap_max
) {
1805 atomic_add_32(&fasttrap_total
, -1);
1809 #if !defined(__APPLE__)
1810 pp
= kmem_zalloc(sizeof (fasttrap_probe_t
), KM_SLEEP
);
1813 pp
= zalloc(fasttrap_probe_t_zones
[1]);
1814 bzero(pp
, sizeof (fasttrap_probe_t
));
1817 pp
->ftp_prov
= provider
;
1818 pp
->ftp_faddr
= pdata
->ftps_pc
;
1819 pp
->ftp_fsize
= pdata
->ftps_size
;
1820 pp
->ftp_pid
= pdata
->ftps_pid
;
1823 #if !defined(__APPLE__)
1824 tp
= kmem_zalloc(sizeof (fasttrap_tracepoint_t
), KM_SLEEP
);
1826 tp
= zalloc(fasttrap_tracepoint_t_zone
);
1827 bzero(tp
, sizeof (fasttrap_tracepoint_t
));
1830 tp
->ftt_proc
= provider
->ftp_proc
;
1831 tp
->ftt_pc
= pdata
->ftps_offs
[i
] + pdata
->ftps_pc
;
1832 tp
->ftt_pid
= pdata
->ftps_pid
;
1835 pp
->ftp_tps
[0].fit_tp
= tp
;
1836 pp
->ftp_tps
[0].fit_id
.fti_probe
= pp
;
1837 #if defined(__APPLE__)
1838 pp
->ftp_tps
[0].fit_id
.fti_ptype
= pdata
->ftps_probe_type
;
1840 pp
->ftp_id
= dtrace_probe_create(provider
->ftp_provid
,
1841 pdata
->ftps_mod
, pdata
->ftps_func
, name_str
,
1842 FASTTRAP_OFFSET_AFRAMES
, pp
);
1845 } else if (dtrace_probe_lookup(provider
->ftp_provid
, pdata
->ftps_mod
,
1846 pdata
->ftps_func
, name
) == 0) {
1847 atomic_add_32(&fasttrap_total
, pdata
->ftps_noffs
);
1849 if (fasttrap_total
> fasttrap_max
) {
1850 atomic_add_32(&fasttrap_total
, -pdata
->ftps_noffs
);
1855 * Make sure all tracepoint program counter values are unique.
1856 * We later assume that each probe has exactly one tracepoint
1859 qsort(pdata
->ftps_offs
, pdata
->ftps_noffs
,
1860 sizeof (uint64_t), fasttrap_uint64_cmp
);
1861 for (i
= 1; i
< pdata
->ftps_noffs
; i
++) {
1862 if (pdata
->ftps_offs
[i
] > pdata
->ftps_offs
[i
- 1])
1865 atomic_add_32(&fasttrap_total
, -pdata
->ftps_noffs
);
1869 ASSERT(pdata
->ftps_noffs
> 0);
1870 #if !defined(__APPLE__)
1871 pp
= kmem_zalloc(offsetof(fasttrap_probe_t
,
1872 ftp_tps
[pdata
->ftps_noffs
]), KM_SLEEP
);
1875 if (pdata
->ftps_noffs
< FASTTRAP_PROBE_T_ZONE_MAX_TRACEPOINTS
) {
1876 pp
= zalloc(fasttrap_probe_t_zones
[pdata
->ftps_noffs
]);
1877 bzero(pp
, offsetof(fasttrap_probe_t
, ftp_tps
[pdata
->ftps_noffs
]));
1879 pp
= kmem_zalloc(offsetof(fasttrap_probe_t
, ftp_tps
[pdata
->ftps_noffs
]), KM_SLEEP
);
1883 pp
->ftp_prov
= provider
;
1884 pp
->ftp_faddr
= pdata
->ftps_pc
;
1885 pp
->ftp_fsize
= pdata
->ftps_size
;
1886 pp
->ftp_pid
= pdata
->ftps_pid
;
1887 pp
->ftp_ntps
= pdata
->ftps_noffs
;
1889 for (i
= 0; i
< pdata
->ftps_noffs
; i
++) {
1890 #if !defined(__APPLE__)
1891 tp
= kmem_zalloc(sizeof (fasttrap_tracepoint_t
), KM_SLEEP
);
1893 tp
= zalloc(fasttrap_tracepoint_t_zone
);
1894 bzero(tp
, sizeof (fasttrap_tracepoint_t
));
1897 tp
->ftt_proc
= provider
->ftp_proc
;
1898 tp
->ftt_pc
= pdata
->ftps_offs
[i
] + pdata
->ftps_pc
;
1899 tp
->ftt_pid
= pdata
->ftps_pid
;
1901 pp
->ftp_tps
[i
].fit_tp
= tp
;
1902 pp
->ftp_tps
[i
].fit_id
.fti_probe
= pp
;
1903 #if defined(__APPLE__)
1904 pp
->ftp_tps
[i
].fit_id
.fti_ptype
= pdata
->ftps_probe_type
;
1908 pp
->ftp_id
= dtrace_probe_create(provider
->ftp_provid
,
1909 pdata
->ftps_mod
, pdata
->ftps_func
, name
, aframes
, pp
);
1912 lck_mtx_unlock(&provider
->ftp_cmtx
);
1915 * We know that the provider is still valid since we incremented the
1916 * creation reference count. If someone tried to clean up this provider
1917 * while we were using it (e.g. because the process called exec(2) or
1918 * exit(2)), take note of that and try to clean it up now.
1920 lck_mtx_lock(&provider
->ftp_mtx
);
1921 provider
->ftp_ccount
--;
1922 whack
= provider
->ftp_retired
;
1923 lck_mtx_unlock(&provider
->ftp_mtx
);
1926 fasttrap_pid_cleanup();
1932 * If we've exhausted the allowable resources, we'll try to remove
1933 * this provider to free some up. This is to cover the case where
1934 * the user has accidentally created many more probes than was
1935 * intended (e.g. pid123:::).
1937 lck_mtx_unlock(&provider
->ftp_cmtx
);
1938 lck_mtx_lock(&provider
->ftp_mtx
);
1939 provider
->ftp_ccount
--;
1940 provider
->ftp_marked
= 1;
1941 lck_mtx_unlock(&provider
->ftp_mtx
);
1943 fasttrap_pid_cleanup();
1950 fasttrap_meta_provide(void *arg
, dtrace_helper_provdesc_t
*dhpv
, pid_t pid
)
1953 fasttrap_provider_t
*provider
;
1956 * A 32-bit unsigned integer (like a pid for example) can be
1957 * expressed in 10 or fewer decimal digits. Make sure that we'll
1958 * have enough space for the provider name.
1960 if (strlen(dhpv
->dthpv_provname
) + 10 >=
1961 sizeof (provider
->ftp_name
)) {
1962 cmn_err(CE_WARN
, "failed to instantiate provider %s: "
1963 "name too long to accomodate pid", dhpv
->dthpv_provname
);
1968 * Don't let folks spoof the true pid provider.
1970 if (strncmp(dhpv
->dthpv_provname
, FASTTRAP_PID_NAME
, sizeof(FASTTRAP_PID_NAME
)) == 0) {
1971 cmn_err(CE_WARN
, "failed to instantiate provider %s: "
1972 "%s is an invalid name", dhpv
->dthpv_provname
,
1976 #if defined(__APPLE__)
1978 * We also need to check the other pid provider types
1980 if (strncmp(dhpv
->dthpv_provname
, FASTTRAP_OBJC_NAME
, sizeof(FASTTRAP_OBJC_NAME
)) == 0) {
1981 cmn_err(CE_WARN
, "failed to instantiate provider %s: "
1982 "%s is an invalid name", dhpv
->dthpv_provname
,
1983 FASTTRAP_OBJC_NAME
);
1986 if (strncmp(dhpv
->dthpv_provname
, FASTTRAP_ONESHOT_NAME
, sizeof(FASTTRAP_ONESHOT_NAME
)) == 0) {
1987 cmn_err(CE_WARN
, "failed to instantiate provider %s: "
1988 "%s is an invalid name", dhpv
->dthpv_provname
,
1989 FASTTRAP_ONESHOT_NAME
);
1992 #endif /* __APPLE__ */
1995 * The highest stability class that fasttrap supports is ISA; cap
1996 * the stability of the new provider accordingly.
1998 if (dhpv
->dthpv_pattr
.dtpa_provider
.dtat_class
> DTRACE_CLASS_ISA
)
1999 dhpv
->dthpv_pattr
.dtpa_provider
.dtat_class
= DTRACE_CLASS_ISA
;
2000 if (dhpv
->dthpv_pattr
.dtpa_mod
.dtat_class
> DTRACE_CLASS_ISA
)
2001 dhpv
->dthpv_pattr
.dtpa_mod
.dtat_class
= DTRACE_CLASS_ISA
;
2002 if (dhpv
->dthpv_pattr
.dtpa_func
.dtat_class
> DTRACE_CLASS_ISA
)
2003 dhpv
->dthpv_pattr
.dtpa_func
.dtat_class
= DTRACE_CLASS_ISA
;
2004 if (dhpv
->dthpv_pattr
.dtpa_name
.dtat_class
> DTRACE_CLASS_ISA
)
2005 dhpv
->dthpv_pattr
.dtpa_name
.dtat_class
= DTRACE_CLASS_ISA
;
2006 if (dhpv
->dthpv_pattr
.dtpa_args
.dtat_class
> DTRACE_CLASS_ISA
)
2007 dhpv
->dthpv_pattr
.dtpa_args
.dtat_class
= DTRACE_CLASS_ISA
;
2009 #if defined(__APPLE__)
2010 if ((provider
= fasttrap_provider_lookup(pid
, DTFTP_PROVIDER_USDT
, dhpv
->dthpv_provname
,
2011 &dhpv
->dthpv_pattr
)) == NULL
) {
2012 cmn_err(CE_WARN
, "failed to instantiate provider %s for "
2013 "process %u", dhpv
->dthpv_provname
, (uint_t
)pid
);
2020 * USDT probes (fasttrap meta probes) are very expensive to create.
2021 * Profiling has shown that the largest single cost is verifying that
2022 * dtrace hasn't already created a given meta_probe. The reason for
2023 * this is dtrace_match() often has to strcmp ~100 hashed entries for
2024 * each static probe being created. We want to get rid of that check.
2025 * The simplest way of eliminating it is to deny the ability to add
2026 * probes to an existing provider. If the provider already exists, BZZT!
2027 * This still leaves the possibility of intentionally malformed DOF
2028 * having duplicate probes. However, duplicate probes are not fatal,
2029 * and there is no way to get that by accident, so we will not check
2032 * UPDATE: It turns out there are several use cases that require adding
2033 * probes to existing providers. Disabling this optimization for now...
2035 #endif /* __APPLE__ */
2038 * Up the meta provider count so this provider isn't removed until
2039 * the meta provider has been told to remove it.
2041 provider
->ftp_mcount
++;
2043 lck_mtx_unlock(&provider
->ftp_mtx
);
2050 fasttrap_meta_create_probe(void *arg
, void *parg
,
2051 dtrace_helper_probedesc_t
*dhpb
)
2054 fasttrap_provider_t
*provider
= parg
;
2055 fasttrap_probe_t
*pp
;
2056 fasttrap_tracepoint_t
*tp
;
2061 * Since the meta provider count is non-zero we don't have to worry
2062 * about this provider disappearing.
2064 ASSERT(provider
->ftp_mcount
> 0);
2067 * The offsets must be unique.
2069 qsort(dhpb
->dthpb_offs
, dhpb
->dthpb_noffs
, sizeof (uint32_t),
2070 fasttrap_uint32_cmp
);
2071 for (i
= 1; i
< dhpb
->dthpb_noffs
; i
++) {
2072 if (dhpb
->dthpb_base
+ dhpb
->dthpb_offs
[i
] <=
2073 dhpb
->dthpb_base
+ dhpb
->dthpb_offs
[i
- 1])
2077 qsort(dhpb
->dthpb_enoffs
, dhpb
->dthpb_nenoffs
, sizeof (uint32_t),
2078 fasttrap_uint32_cmp
);
2079 for (i
= 1; i
< dhpb
->dthpb_nenoffs
; i
++) {
2080 if (dhpb
->dthpb_base
+ dhpb
->dthpb_enoffs
[i
] <=
2081 dhpb
->dthpb_base
+ dhpb
->dthpb_enoffs
[i
- 1])
2086 * Grab the creation lock to ensure consistency between calls to
2087 * dtrace_probe_lookup() and dtrace_probe_create() in the face of
2088 * other threads creating probes.
2090 lck_mtx_lock(&provider
->ftp_cmtx
);
2092 #if !defined(__APPLE__)
2094 * APPLE NOTE: This is hideously expensive. See note in
2095 * fasttrap_meta_provide() for why we can get away without
2098 if (dtrace_probe_lookup(provider
->ftp_provid
, dhpb
->dthpb_mod
,
2099 dhpb
->dthpb_func
, dhpb
->dthpb_name
) != 0) {
2100 lck_mtx_unlock(&provider
->ftp_cmtx
);
2105 ntps
= dhpb
->dthpb_noffs
+ dhpb
->dthpb_nenoffs
;
2108 atomic_add_32(&fasttrap_total
, ntps
);
2110 if (fasttrap_total
> fasttrap_max
) {
2111 atomic_add_32(&fasttrap_total
, -ntps
);
2112 lck_mtx_unlock(&provider
->ftp_cmtx
);
2116 #if !defined(__APPLE__)
2117 pp
= kmem_zalloc(offsetof(fasttrap_probe_t
, ftp_tps
[ntps
]), KM_SLEEP
);
2120 if (ntps
< FASTTRAP_PROBE_T_ZONE_MAX_TRACEPOINTS
) {
2121 pp
= zalloc(fasttrap_probe_t_zones
[ntps
]);
2122 bzero(pp
, offsetof(fasttrap_probe_t
, ftp_tps
[ntps
]));
2124 pp
= kmem_zalloc(offsetof(fasttrap_probe_t
, ftp_tps
[ntps
]), KM_SLEEP
);
2128 pp
->ftp_prov
= provider
;
2129 pp
->ftp_pid
= provider
->ftp_pid
;
2130 pp
->ftp_ntps
= ntps
;
2131 pp
->ftp_nargs
= dhpb
->dthpb_xargc
;
2132 pp
->ftp_xtypes
= dhpb
->dthpb_xtypes
;
2133 pp
->ftp_ntypes
= dhpb
->dthpb_ntypes
;
2136 * First create a tracepoint for each actual point of interest.
2138 for (i
= 0; i
< dhpb
->dthpb_noffs
; i
++) {
2139 #if !defined(__APPLE__)
2140 tp
= kmem_zalloc(sizeof (fasttrap_tracepoint_t
), KM_SLEEP
);
2142 tp
= zalloc(fasttrap_tracepoint_t_zone
);
2143 bzero(tp
, sizeof (fasttrap_tracepoint_t
));
2146 tp
->ftt_proc
= provider
->ftp_proc
;
2147 #if defined(__APPLE__)
2149 * APPLE NOTE: We have linker support when creating DOF to handle all relocations for us.
2150 * Unfortunately, a side effect of this is that the relocations do not point at exactly
2151 * the location we want. We need to fix up the addresses here. The fixups vary by arch and type.
2153 #if defined(__i386__) || defined(__x86_64__)
2155 * Both 32 & 64 bit want to go back one byte, to point at the first NOP
2157 tp
->ftt_pc
= dhpb
->dthpb_base
+ (int64_t)dhpb
->dthpb_offs
[i
] - 1;
2158 #elif defined(__ppc__)
2159 /* All PPC probes are zero offset. */
2160 tp
->ftt_pc
= dhpb
->dthpb_base
+ (int64_t)dhpb
->dthpb_offs
[i
];
2162 #error "Architecture not supported"
2166 tp
->ftt_pc
= dhpb
->dthpb_base
+ dhpb
->dthpb_offs
[i
];
2168 tp
->ftt_pid
= provider
->ftp_pid
;
2170 pp
->ftp_tps
[i
].fit_tp
= tp
;
2171 pp
->ftp_tps
[i
].fit_id
.fti_probe
= pp
;
2173 pp
->ftp_tps
[i
].fit_id
.fti_ptype
= DTFTP_POST_OFFSETS
;
2175 pp
->ftp_tps
[i
].fit_id
.fti_ptype
= DTFTP_OFFSETS
;
2180 * Then create a tracepoint for each is-enabled point.
2182 for (j
= 0; i
< ntps
; i
++, j
++) {
2183 #if !defined(__APPLE__)
2184 tp
= kmem_zalloc(sizeof (fasttrap_tracepoint_t
), KM_SLEEP
);
2186 tp
= zalloc(fasttrap_tracepoint_t_zone
);
2187 bzero(tp
, sizeof (fasttrap_tracepoint_t
));
2190 tp
->ftt_proc
= provider
->ftp_proc
;
2191 #if defined(__APPLE__)
2193 * APPLE NOTE: We have linker support when creating DOF to handle all relocations for us.
2194 * Unfortunately, a side effect of this is that the relocations do not point at exactly
2195 * the location we want. We need to fix up the addresses here. The fixups vary by arch and type.
2197 #if defined(__i386__) || defined(__x86_64__)
2199 * Both 32 & 64 bit want to go forward two bytes, to point at a single byte nop.
2201 tp
->ftt_pc
= dhpb
->dthpb_base
+ (int64_t)dhpb
->dthpb_enoffs
[j
] + 2;
2202 #elif defined(__ppc__)
2203 /* All PPC is-enabled probes are zero offset. */
2204 tp
->ftt_pc
= dhpb
->dthpb_base
+ (int64_t)dhpb
->dthpb_enoffs
[j
];
2206 #error "Architecture not supported"
2210 tp
->ftt_pc
= dhpb
->dthpb_base
+ dhpb
->dthpb_enoffs
[j
];
2212 tp
->ftt_pid
= provider
->ftp_pid
;
2214 pp
->ftp_tps
[i
].fit_tp
= tp
;
2215 pp
->ftp_tps
[i
].fit_id
.fti_probe
= pp
;
2216 pp
->ftp_tps
[i
].fit_id
.fti_ptype
= DTFTP_IS_ENABLED
;
2220 * If the arguments are shuffled around we set the argument remapping
2221 * table. Later, when the probe fires, we only remap the arguments
2222 * if the table is non-NULL.
2224 for (i
= 0; i
< dhpb
->dthpb_xargc
; i
++) {
2225 if (dhpb
->dthpb_args
[i
] != i
) {
2226 pp
->ftp_argmap
= dhpb
->dthpb_args
;
2232 * The probe is fully constructed -- register it with DTrace.
2234 pp
->ftp_id
= dtrace_probe_create(provider
->ftp_provid
, dhpb
->dthpb_mod
,
2235 dhpb
->dthpb_func
, dhpb
->dthpb_name
, FASTTRAP_OFFSET_AFRAMES
, pp
);
2237 lck_mtx_unlock(&provider
->ftp_cmtx
);
2242 fasttrap_meta_remove(void *arg
, dtrace_helper_provdesc_t
*dhpv
, pid_t pid
)
2246 * Clean up the USDT provider. There may be active consumers of the
2247 * provider busy adding probes, no damage will actually befall the
2248 * provider until that count has dropped to zero. This just puts
2249 * the provider on death row.
2251 fasttrap_provider_retire(pid
, dhpv
->dthpv_provname
, 1);
2254 static dtrace_mops_t fasttrap_mops
= {
2255 fasttrap_meta_create_probe
,
2256 fasttrap_meta_provide
,
2257 fasttrap_meta_remove
2262 fasttrap_ioctl(dev_t dev
, u_long cmd
, user_addr_t arg
, int md
, cred_t
*cr
, int *rv
)
2264 #pragma unused(dev, md, rv)
2265 if (!dtrace_attached())
2268 if (cmd
== FASTTRAPIOC_MAKEPROBE
) {
2269 fasttrap_probe_spec_t
*probe
;
2275 if (copyin(arg
+ __offsetof(fasttrap_probe_spec_t
, ftps_noffs
), &noffs
,
2276 sizeof (probe
->ftps_noffs
)))
2280 * Probes must have at least one tracepoint.
2286 * We want to check the number of noffs before doing
2287 * sizing math, to prevent potential buffer overflows.
2289 if (noffs
> ((1024 * 1024) - sizeof(fasttrap_probe_spec_t
)) / sizeof(probe
->ftps_offs
[0]))
2292 size
= sizeof (fasttrap_probe_spec_t
) +
2293 sizeof (probe
->ftps_offs
[0]) * (noffs
- 1);
2295 probe
= kmem_alloc(size
, KM_SLEEP
);
2297 if (copyin(arg
, probe
, size
) != 0) {
2298 kmem_free(probe
, size
);
2303 * Verify that the function and module strings contain no
2306 for (i
= 0, c
= &probe
->ftps_func
[0]; i
< sizeof(probe
->ftps_func
) && *c
!= '\0'; i
++, c
++) {
2307 if (*c
< 0x20 || 0x7f <= *c
) {
2317 for (i
= 0, c
= &probe
->ftps_mod
[0]; i
< sizeof(probe
->ftps_mod
) && *c
!= '\0'; i
++, c
++) {
2318 if (*c
< 0x20 || 0x7f <= *c
) {
2328 if (!PRIV_POLICY_CHOICE(cr
, PRIV_ALL
, B_FALSE
)) {
2330 pid_t pid
= probe
->ftps_pid
;
2333 * Report an error if the process doesn't exist
2334 * or is actively being birthed.
2336 if ((p
= proc_find(pid
)) == PROC_NULL
|| p
->p_stat
== SIDL
) {
2342 // FIXME! How is this done on OS X?
2343 // if ((ret = priv_proc_cred_perm(cr, p, NULL,
2344 // VREAD | VWRITE)) != 0) {
2345 // mutex_exit(&p->p_lock);
2352 ret
= fasttrap_add_probe(probe
);
2355 kmem_free(probe
, size
);
2359 } else if (cmd
== FASTTRAPIOC_GETINSTR
) {
2360 fasttrap_instr_query_t instr
;
2361 fasttrap_tracepoint_t
*tp
;
2365 if (copyin(arg
, &instr
, sizeof (instr
)) != 0)
2368 if (!PRIV_POLICY_CHOICE(cr
, PRIV_ALL
, B_FALSE
)) {
2370 pid_t pid
= instr
.ftiq_pid
;
2373 * Report an error if the process doesn't exist
2374 * or is actively being birthed.
2376 if ((p
= proc_find(pid
)) == NULL
|| p
->p_stat
== SIDL
) {
2382 // FIXME! How is this done on OS X?
2383 // if ((ret = priv_proc_cred_perm(cr, p, NULL,
2385 // mutex_exit(&p->p_lock);
2392 index
= FASTTRAP_TPOINTS_INDEX(instr
.ftiq_pid
, instr
.ftiq_pc
);
2394 lck_mtx_lock(&fasttrap_tpoints
.fth_table
[index
].ftb_mtx
);
2395 tp
= fasttrap_tpoints
.fth_table
[index
].ftb_data
;
2396 while (tp
!= NULL
) {
2397 if (instr
.ftiq_pid
== tp
->ftt_pid
&&
2398 instr
.ftiq_pc
== tp
->ftt_pc
&&
2399 tp
->ftt_proc
->ftpc_acount
!= 0)
2406 lck_mtx_unlock(&fasttrap_tpoints
.fth_table
[index
].ftb_mtx
);
2410 bcopy(&tp
->ftt_instr
, &instr
.ftiq_instr
,
2411 sizeof (instr
.ftiq_instr
));
2412 lck_mtx_unlock(&fasttrap_tpoints
.fth_table
[index
].ftb_mtx
);
2414 if (copyout(&instr
, arg
, sizeof (instr
)) != 0)
2424 fasttrap_attach(dev_info_t
*devi
, ddi_attach_cmd_t cmd
)
2432 return (DDI_SUCCESS
);
2434 return (DDI_FAILURE
);
2437 ddi_report_dev(devi
);
2438 fasttrap_devi
= devi
;
2441 * Install our hooks into fork(2), exec(2), and exit(2).
2443 dtrace_fasttrap_fork_ptr
= &fasttrap_fork
;
2444 dtrace_fasttrap_exit_ptr
= &fasttrap_exec_exit
;
2445 dtrace_fasttrap_exec_ptr
= &fasttrap_exec_exit
;
2447 #if !defined(__APPLE__)
2448 fasttrap_max
= ddi_getprop(DDI_DEV_T_ANY
, devi
, DDI_PROP_DONTPASS
,
2449 "fasttrap-max-probes", FASTTRAP_MAX_DEFAULT
);
2452 * We're sizing based on system memory. 100k probes per 256M of system memory.
2453 * Yes, this is a WAG.
2455 fasttrap_max
= (sane_size
>> 28) * 100000;
2456 if (fasttrap_max
== 0)
2457 fasttrap_max
= 50000;
2462 * Conjure up the tracepoints hashtable...
2464 nent
= ddi_getprop(DDI_DEV_T_ANY
, devi
, DDI_PROP_DONTPASS
,
2465 "fasttrap-hash-size", FASTTRAP_TPOINTS_DEFAULT_SIZE
);
2467 if (nent
<= 0 || nent
> 0x1000000)
2468 nent
= FASTTRAP_TPOINTS_DEFAULT_SIZE
;
2470 if ((nent
& (nent
- 1)) == 0)
2471 fasttrap_tpoints
.fth_nent
= nent
;
2473 fasttrap_tpoints
.fth_nent
= 1 << fasttrap_highbit(nent
);
2474 ASSERT(fasttrap_tpoints
.fth_nent
> 0);
2475 fasttrap_tpoints
.fth_mask
= fasttrap_tpoints
.fth_nent
- 1;
2476 fasttrap_tpoints
.fth_table
= kmem_zalloc(fasttrap_tpoints
.fth_nent
*
2477 sizeof (fasttrap_bucket_t
), KM_SLEEP
);
2478 ASSERT(fasttrap_tpoints
.fth_table
!= NULL
);
2479 #if defined(__APPLE__)
2481 * We have to explicitly initialize all locks...
2484 for (i
=0; i
<fasttrap_tpoints
.fth_nent
; i
++) {
2485 lck_mtx_init(&fasttrap_tpoints
.fth_table
[i
].ftb_mtx
, fasttrap_lck_grp
, fasttrap_lck_attr
);
2490 * ... and the providers hash table...
2492 nent
= FASTTRAP_PROVIDERS_DEFAULT_SIZE
;
2493 if ((nent
& (nent
- 1)) == 0)
2494 fasttrap_provs
.fth_nent
= nent
;
2496 fasttrap_provs
.fth_nent
= 1 << fasttrap_highbit(nent
);
2497 ASSERT(fasttrap_provs
.fth_nent
> 0);
2498 fasttrap_provs
.fth_mask
= fasttrap_provs
.fth_nent
- 1;
2499 fasttrap_provs
.fth_table
= kmem_zalloc(fasttrap_provs
.fth_nent
*
2500 sizeof (fasttrap_bucket_t
), KM_SLEEP
);
2501 ASSERT(fasttrap_provs
.fth_table
!= NULL
);
2502 #if defined(__APPLE__)
2504 * We have to explicitly initialize all locks...
2506 for (i
=0; i
<fasttrap_provs
.fth_nent
; i
++) {
2507 lck_mtx_init(&fasttrap_provs
.fth_table
[i
].ftb_mtx
, fasttrap_lck_grp
, fasttrap_lck_attr
);
2512 * ... and the procs hash table.
2514 nent
= FASTTRAP_PROCS_DEFAULT_SIZE
;
2515 if ((nent
& (nent
- 1)) == 0)
2516 fasttrap_procs
.fth_nent
= nent
;
2518 fasttrap_procs
.fth_nent
= 1 << fasttrap_highbit(nent
);
2519 ASSERT(fasttrap_procs
.fth_nent
> 0);
2520 fasttrap_procs
.fth_mask
= fasttrap_procs
.fth_nent
- 1;
2521 fasttrap_procs
.fth_table
= kmem_zalloc(fasttrap_procs
.fth_nent
*
2522 sizeof (fasttrap_bucket_t
), KM_SLEEP
);
2523 ASSERT(fasttrap_procs
.fth_table
!= NULL
);
2524 #if defined(__APPLE__)
2526 * We have to explicitly initialize all locks...
2528 for (i
=0; i
<fasttrap_procs
.fth_nent
; i
++) {
2529 lck_mtx_init(&fasttrap_procs
.fth_table
[i
].ftb_mtx
, fasttrap_lck_grp
, fasttrap_lck_attr
);
2533 (void) dtrace_meta_register("fasttrap", &fasttrap_mops
, NULL
,
2536 return (DDI_SUCCESS
);
2540 _fasttrap_open(dev_t dev
, int flags
, int devtype
, struct proc
*p
)
2542 #pragma unused(dev, flags, devtype, p)
2547 _fasttrap_ioctl(dev_t dev
, u_long cmd
, caddr_t data
, int fflag
, struct proc
*p
)
2552 if (proc_is64bit(p
))
2553 uaddrp
= *(user_addr_t
*)data
;
2555 uaddrp
= (user_addr_t
) *(uint32_t *)data
;
2557 err
= fasttrap_ioctl(dev
, cmd
, uaddrp
, fflag
, CRED(), &rv
);
2559 /* XXX Darwin's BSD ioctls only return -1 or zero. Overload errno to mimic Solaris. 20 bits suffice. */
2561 ASSERT( (err
& 0xfffff000) == 0 );
2562 return (err
& 0xfff); /* ioctl returns -1 and errno set to an error code < 4096 */
2563 } else if (rv
!= 0) {
2564 ASSERT( (rv
& 0xfff00000) == 0 );
2565 return (((rv
& 0xfffff) << 12)); /* ioctl returns -1 and errno set to a return value >= 4096 */
2570 static int gFasttrapInited
= 0;
2572 #define FASTTRAP_MAJOR -24 /* let the kernel pick the device number */
2575 * A struct describing which functions will get invoked for certain
2579 static struct cdevsw fasttrap_cdevsw
=
2581 _fasttrap_open
, /* open */
2582 eno_opcl
, /* close */
2583 eno_rdwrt
, /* read */
2584 eno_rdwrt
, /* write */
2585 _fasttrap_ioctl
, /* ioctl */
2586 (stop_fcn_t
*)nulldev
, /* stop */
2587 (reset_fcn_t
*)nulldev
, /* reset */
2589 eno_select
, /* select */
2590 eno_mmap
, /* mmap */
2591 eno_strat
, /* strategy */
2592 eno_getc
, /* getc */
2593 eno_putc
, /* putc */
2597 void fasttrap_init(void);
2600 fasttrap_init( void )
2603 * This method is now invoked from multiple places. Any open of /dev/dtrace,
2604 * also dtrace_init if the dtrace_dof_mode is DTRACE_DOF_MODE_NON_LAZY.
2606 * The reason is to delay allocating the (rather large) resources as late as possible.
2608 if (0 == gFasttrapInited
) {
2609 int majdevno
= cdevsw_add(FASTTRAP_MAJOR
, &fasttrap_cdevsw
);
2612 // FIX ME! What kind of error reporting to do here?
2613 printf("fasttrap_init: failed to allocate a major number!\n");
2617 dev_t device
= makedev( (uint32_t)majdevno
, 0 );
2618 if (NULL
== devfs_make_node( device
, DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0666, "fasttrap", 0 )) {
2623 * Allocate the fasttrap_tracepoint_t zone
2625 fasttrap_tracepoint_t_zone
= zinit(sizeof(fasttrap_tracepoint_t
),
2626 1024 * sizeof(fasttrap_tracepoint_t
),
2627 sizeof(fasttrap_tracepoint_t
),
2628 "dtrace.fasttrap_tracepoint_t");
2631 * fasttrap_probe_t's are variable in size. We use an array of zones to
2632 * cover the most common sizes.
2635 for (i
=1; i
<FASTTRAP_PROBE_T_ZONE_MAX_TRACEPOINTS
; i
++) {
2636 size_t zone_element_size
= offsetof(fasttrap_probe_t
, ftp_tps
[i
]);
2637 fasttrap_probe_t_zones
[i
] = zinit(zone_element_size
,
2638 1024 * zone_element_size
,
2640 fasttrap_probe_t_zone_names
[i
]);
2645 * Create the fasttrap lock group. Must be done before fasttrap_attach()!
2647 fasttrap_lck_attr
= lck_attr_alloc_init();
2648 fasttrap_lck_grp_attr
= lck_grp_attr_alloc_init();
2649 fasttrap_lck_grp
= lck_grp_alloc_init("fasttrap", fasttrap_lck_grp_attr
);
2652 * Initialize global locks
2654 lck_mtx_init(&fasttrap_cleanup_mtx
, fasttrap_lck_grp
, fasttrap_lck_attr
);
2655 lck_mtx_init(&fasttrap_count_mtx
, fasttrap_lck_grp
, fasttrap_lck_attr
);
2657 if (DDI_FAILURE
== fasttrap_attach((dev_info_t
*)(uintptr_t)device
, 0 )) {
2658 // FIX ME! Do we remove the devfs node here?
2659 // What kind of error reporting?
2660 printf("fasttrap_init: Call to fasttrap_attach failed.\n");
2664 gFasttrapInited
= 1;
2668 #undef FASTTRAP_MAJOR