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.
27 /* #pragma ident "@(#)dtrace.c 1.65 08/07/02 SMI" */
30 * DTrace - Dynamic Tracing for Solaris
32 * This is the implementation of the Solaris Dynamic Tracing framework
33 * (DTrace). The user-visible interface to DTrace is described at length in
34 * the "Solaris Dynamic Tracing Guide". The interfaces between the libdtrace
35 * library, the in-kernel DTrace framework, and the DTrace providers are
36 * described in the block comments in the <sys/dtrace.h> header file. The
37 * internal architecture of DTrace is described in the block comments in the
38 * <sys/dtrace_impl.h> header file. The comments contained within the DTrace
39 * implementation very much assume mastery of all of these sources; if one has
40 * an unanswered question about the implementation, one should consult them
43 * The functions here are ordered roughly as follows:
45 * - Probe context functions
46 * - Probe hashing functions
47 * - Non-probe context utility functions
48 * - Matching functions
49 * - Provider-to-Framework API functions
50 * - Probe management functions
51 * - DIF object functions
53 * - Predicate functions
56 * - Enabling functions
58 * - Anonymous enabling functions
59 * - Consumer state functions
62 * - Driver cookbook functions
64 * Each group of functions begins with a block comment labelled the "DTrace
65 * [Group] Functions", allowing one to find each block by searching forward
66 * on capital-f functions.
68 #if !defined(__APPLE__)
69 #include <sys/errno.h>
71 #include <sys/modctl.h>
73 #include <sys/systm.h>
75 #include <sys/sunddi.h>
76 #include <sys/cpuvar.h>
78 #include <sys/strsubr.h>
79 #include <sys/sysmacros.h>
80 #include <sys/dtrace_impl.h>
81 #include <sys/atomic.h>
82 #include <sys/cmn_err.h>
83 #include <sys/mutex_impl.h>
84 #include <sys/rwlock_impl.h>
85 #include <sys/ctf_api.h>
86 #include <sys/panic.h>
87 #include <sys/priv_impl.h>
88 #include <sys/policy.h>
89 #include <sys/cred_impl.h>
90 #include <sys/procfs_isa.h>
91 #include <sys/taskq.h>
92 #include <sys/mkdev.h>
96 #include <sys/errno.h>
97 #include <sys/types.h>
100 #include <sys/systm.h>
101 #include <sys/dtrace_impl.h>
102 #include <sys/param.h>
103 #include <sys/ioctl.h>
104 #include <sys/fcntl.h>
105 #include <miscfs/devfs/devfs.h>
106 #include <sys/malloc.h>
107 #include <sys/kernel_types.h>
108 #include <sys/proc_internal.h>
109 #include <sys/uio_internal.h>
110 #include <sys/kauth.h>
112 #include <sys/user.h>
113 #include <mach/exception_types.h>
114 #include <sys/signalvar.h>
115 #include <kern/zalloc.h>
116 #include <kern/ast.h>
117 #include <netinet/in.h>
119 #if defined(__APPLE__)
120 extern uint32_t pmap_find_phys(void *, uint64_t);
121 extern boolean_t
pmap_valid_page(uint32_t);
122 #endif /* __APPLE__ */
125 /* Solaris proc_t is the struct. Darwin's proc_t is a pointer to it. */
126 #define proc_t struct proc /* Steer clear of the Darwin typedef for proc_t */
128 #define t_predcache t_dtrace_predcache /* Cosmetic. Helps readability of thread.h */
130 extern void dtrace_suspend(void);
131 extern void dtrace_resume(void);
132 extern void dtrace_init(void);
133 extern void helper_init(void);
134 extern void fasttrap_init(void);
135 extern void dtrace_lazy_dofs_duplicate(proc_t
*, proc_t
*);
136 extern void dtrace_lazy_dofs_destroy(proc_t
*);
137 extern void dtrace_postinit(void);
139 #include "../../../osfmk/chud/chud_dtrace.h"
141 extern kern_return_t chudxnu_dtrace_callback
142 (uint64_t selector
, uint64_t *args
, uint32_t count
);
143 #endif /* __APPLE__ */
146 * DTrace Tunable Variables
148 * The following variables may be tuned by adding a line to /etc/system that
149 * includes both the name of the DTrace module ("dtrace") and the name of the
150 * variable. For example:
152 * set dtrace:dtrace_destructive_disallow = 1
154 * In general, the only variables that one should be tuning this way are those
155 * that affect system-wide DTrace behavior, and for which the default behavior
156 * is undesirable. Most of these variables are tunable on a per-consumer
157 * basis using DTrace options, and need not be tuned on a system-wide basis.
158 * When tuning these variables, avoid pathological values; while some attempt
159 * is made to verify the integrity of these variables, they are not considered
160 * part of the supported interface to DTrace, and they are therefore not
161 * checked comprehensively. Further, these variables should not be tuned
162 * dynamically via "mdb -kw" or other means; they should only be tuned via
165 int dtrace_destructive_disallow
= 0;
166 dtrace_optval_t dtrace_nonroot_maxsize
= (16 * 1024 * 1024);
167 size_t dtrace_difo_maxsize
= (256 * 1024);
168 dtrace_optval_t dtrace_dof_maxsize
= (384 * 1024);
169 size_t dtrace_global_maxsize
= (16 * 1024);
170 size_t dtrace_actions_max
= (16 * 1024);
171 size_t dtrace_retain_max
= 1024;
172 dtrace_optval_t dtrace_helper_actions_max
= 32;
173 dtrace_optval_t dtrace_helper_providers_max
= 32;
174 dtrace_optval_t dtrace_dstate_defsize
= (1 * 1024 * 1024);
175 size_t dtrace_strsize_default
= 256;
176 dtrace_optval_t dtrace_cleanrate_default
= 9900990; /* 101 hz */
177 dtrace_optval_t dtrace_cleanrate_min
= 200000; /* 5000 hz */
178 dtrace_optval_t dtrace_cleanrate_max
= (uint64_t)60 * NANOSEC
; /* 1/minute */
179 dtrace_optval_t dtrace_aggrate_default
= NANOSEC
; /* 1 hz */
180 dtrace_optval_t dtrace_statusrate_default
= NANOSEC
; /* 1 hz */
181 dtrace_optval_t dtrace_statusrate_max
= (hrtime_t
)10 * NANOSEC
; /* 6/minute */
182 dtrace_optval_t dtrace_switchrate_default
= NANOSEC
; /* 1 hz */
183 dtrace_optval_t dtrace_nspec_default
= 1;
184 dtrace_optval_t dtrace_specsize_default
= 32 * 1024;
185 dtrace_optval_t dtrace_stackframes_default
= 20;
186 dtrace_optval_t dtrace_ustackframes_default
= 20;
187 dtrace_optval_t dtrace_jstackframes_default
= 50;
188 dtrace_optval_t dtrace_jstackstrsize_default
= 512;
189 int dtrace_msgdsize_max
= 128;
190 hrtime_t dtrace_chill_max
= 500 * (NANOSEC
/ MILLISEC
); /* 500 ms */
191 hrtime_t dtrace_chill_interval
= NANOSEC
; /* 1000 ms */
192 int dtrace_devdepth_max
= 32;
193 int dtrace_err_verbose
;
194 hrtime_t dtrace_deadman_interval
= NANOSEC
;
195 hrtime_t dtrace_deadman_timeout
= (hrtime_t
)10 * NANOSEC
;
196 hrtime_t dtrace_deadman_user
= (hrtime_t
)30 * NANOSEC
;
199 * DTrace External Variables
201 * As dtrace(7D) is a kernel module, any DTrace variables are obviously
202 * available to DTrace consumers via the backtick (`) syntax. One of these,
203 * dtrace_zero, is made deliberately so: it is provided as a source of
204 * well-known, zero-filled memory. While this variable is not documented,
205 * it is used by some translators as an implementation detail.
207 const char dtrace_zero
[256] = { 0 }; /* zero-filled memory */
210 * DTrace Internal Variables
212 static dev_info_t
*dtrace_devi
; /* device info */
213 static vmem_t
*dtrace_arena
; /* probe ID arena */
214 static vmem_t
*dtrace_minor
; /* minor number arena */
215 static taskq_t
*dtrace_taskq
; /* task queue */
216 static dtrace_probe_t
**dtrace_probes
; /* array of all probes */
217 static int dtrace_nprobes
; /* number of probes */
218 static dtrace_provider_t
*dtrace_provider
; /* provider list */
219 static dtrace_meta_t
*dtrace_meta_pid
; /* user-land meta provider */
220 static int dtrace_opens
; /* number of opens */
221 static int dtrace_helpers
; /* number of helpers */
222 static void *dtrace_softstate
; /* softstate pointer */
223 static dtrace_hash_t
*dtrace_bymod
; /* probes hashed by module */
224 static dtrace_hash_t
*dtrace_byfunc
; /* probes hashed by function */
225 static dtrace_hash_t
*dtrace_byname
; /* probes hashed by name */
226 static dtrace_toxrange_t
*dtrace_toxrange
; /* toxic range array */
227 static int dtrace_toxranges
; /* number of toxic ranges */
228 static int dtrace_toxranges_max
; /* size of toxic range array */
229 static dtrace_anon_t dtrace_anon
; /* anonymous enabling */
230 static kmem_cache_t
*dtrace_state_cache
; /* cache for dynamic state */
231 static uint64_t dtrace_vtime_references
; /* number of vtimestamp refs */
232 static kthread_t
*dtrace_panicked
; /* panicking thread */
233 static dtrace_ecb_t
*dtrace_ecb_create_cache
; /* cached created ECB */
234 static dtrace_genid_t dtrace_probegen
; /* current probe generation */
235 static dtrace_helpers_t
*dtrace_deferred_pid
; /* deferred helper list */
236 static dtrace_enabling_t
*dtrace_retained
; /* list of retained enablings */
237 static dtrace_genid_t dtrace_retained_gen
; /* current retained enab gen */
238 static dtrace_dynvar_t dtrace_dynhash_sink
; /* end of dynamic hash chains */
239 #if defined(__APPLE__)
240 static int dtrace_dof_mode
; /* See dtrace_impl.h for a description of Darwin's dof modes. */
243 #if defined(__APPLE__)
245 * To save memory, some common memory allocations are given a
246 * unique zone. For example, dtrace_probe_t is 72 bytes in size,
247 * which means it would fall into the kalloc.128 bucket. With
248 * 20k elements allocated, the space saved is substantial.
251 struct zone
*dtrace_probe_t_zone
;
252 #endif /* __APPLE__ */
256 * DTrace is protected by three (relatively coarse-grained) locks:
258 * (1) dtrace_lock is required to manipulate essentially any DTrace state,
259 * including enabling state, probes, ECBs, consumer state, helper state,
260 * etc. Importantly, dtrace_lock is _not_ required when in probe context;
261 * probe context is lock-free -- synchronization is handled via the
262 * dtrace_sync() cross call mechanism.
264 * (2) dtrace_provider_lock is required when manipulating provider state, or
265 * when provider state must be held constant.
267 * (3) dtrace_meta_lock is required when manipulating meta provider state, or
268 * when meta provider state must be held constant.
270 * The lock ordering between these three locks is dtrace_meta_lock before
271 * dtrace_provider_lock before dtrace_lock. (In particular, there are
272 * several places where dtrace_provider_lock is held by the framework as it
273 * calls into the providers -- which then call back into the framework,
274 * grabbing dtrace_lock.)
276 * There are two other locks in the mix: mod_lock and cpu_lock. With respect
277 * to dtrace_provider_lock and dtrace_lock, cpu_lock continues its historical
278 * role as a coarse-grained lock; it is acquired before both of these locks.
279 * With respect to dtrace_meta_lock, its behavior is stranger: cpu_lock must
280 * be acquired _between_ dtrace_meta_lock and any other DTrace locks.
281 * mod_lock is similar with respect to dtrace_provider_lock in that it must be
282 * acquired _between_ dtrace_provider_lock and dtrace_lock.
285 #if !defined(__APPLE__)
286 static kmutex_t dtrace_lock
; /* probe state lock */
287 static kmutex_t dtrace_provider_lock
; /* provider state lock */
288 static kmutex_t dtrace_meta_lock
; /* meta-provider state lock */
293 * All kmutex_t vars have been changed to lck_mtx_t.
294 * Note that lck_mtx_t's require explicit initialization.
296 * mutex_enter() becomes lck_mtx_lock()
297 * mutex_exit() becomes lck_mtx_unlock()
299 * Lock asserts are changed like this:
301 * ASSERT(MUTEX_HELD(&cpu_lock));
303 * lck_mtx_assert(&cpu_lock, LCK_MTX_ASSERT_OWNED);
305 * Due to the number of these changes, they are not called out explicitly.
307 static lck_mtx_t dtrace_lock
; /* probe state lock */
308 static lck_mtx_t dtrace_provider_lock
; /* provider state lock */
309 static lck_mtx_t dtrace_meta_lock
; /* meta-provider state lock */
310 static lck_rw_t dtrace_dof_mode_lock
; /* dof mode lock */
311 #endif /* __APPLE__ */
314 * DTrace Provider Variables
316 * These are the variables relating to DTrace as a provider (that is, the
317 * provider of the BEGIN, END, and ERROR probes).
319 static dtrace_pattr_t dtrace_provider_attr
= {
320 { DTRACE_STABILITY_STABLE
, DTRACE_STABILITY_STABLE
, DTRACE_CLASS_COMMON
},
321 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_UNKNOWN
},
322 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_UNKNOWN
},
323 { DTRACE_STABILITY_STABLE
, DTRACE_STABILITY_STABLE
, DTRACE_CLASS_COMMON
},
324 { DTRACE_STABILITY_STABLE
, DTRACE_STABILITY_STABLE
, DTRACE_CLASS_COMMON
},
331 static dtrace_pops_t dtrace_provider_ops
= {
332 (void (*)(void *, const dtrace_probedesc_t
*))dtrace_nullop
,
333 (void (*)(void *, struct modctl
*))dtrace_nullop
,
334 (void (*)(void *, dtrace_id_t
, void *))dtrace_nullop
,
335 (void (*)(void *, dtrace_id_t
, void *))dtrace_nullop
,
336 (void (*)(void *, dtrace_id_t
, void *))dtrace_nullop
,
337 (void (*)(void *, dtrace_id_t
, void *))dtrace_nullop
,
341 (void (*)(void *, dtrace_id_t
, void *))dtrace_nullop
344 static dtrace_id_t dtrace_probeid_begin
; /* special BEGIN probe */
345 static dtrace_id_t dtrace_probeid_end
; /* special END probe */
346 dtrace_id_t dtrace_probeid_error
; /* special ERROR probe */
349 * DTrace Helper Tracing Variables
351 uint32_t dtrace_helptrace_next
= 0;
352 uint32_t dtrace_helptrace_nlocals
;
353 char *dtrace_helptrace_buffer
;
354 #if !defined(__APPLE__) /* Quiet compiler warning */
355 int dtrace_helptrace_bufsize
= 512 * 1024;
357 size_t dtrace_helptrace_bufsize
= 512 * 1024;
358 #endif /* __APPLE__ */
361 int dtrace_helptrace_enabled
= 1;
363 int dtrace_helptrace_enabled
= 0;
367 * DTrace Error Hashing
369 * On DEBUG kernels, DTrace will track the errors that has seen in a hash
370 * table. This is very useful for checking coverage of tests that are
371 * expected to induce DIF or DOF processing errors, and may be useful for
372 * debugging problems in the DIF code generator or in DOF generation . The
373 * error hash may be examined with the ::dtrace_errhash MDB dcmd.
376 static dtrace_errhash_t dtrace_errhash
[DTRACE_ERRHASHSZ
];
377 static const char *dtrace_errlast
;
378 static kthread_t
*dtrace_errthread
;
379 static lck_mtx_t dtrace_errlock
;
383 * DTrace Macros and Constants
385 * These are various macros that are useful in various spots in the
386 * implementation, along with a few random constants that have no meaning
387 * outside of the implementation. There is no real structure to this cpp
388 * mishmash -- but is there ever?
390 #define DTRACE_HASHSTR(hash, probe) \
391 dtrace_hash_str(*((char **)((uintptr_t)(probe) + (hash)->dth_stroffs)))
393 #define DTRACE_HASHNEXT(hash, probe) \
394 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_nextoffs)
396 #define DTRACE_HASHPREV(hash, probe) \
397 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_prevoffs)
399 #define DTRACE_HASHEQ(hash, lhs, rhs) \
400 (strcmp(*((char **)((uintptr_t)(lhs) + (hash)->dth_stroffs)), \
401 *((char **)((uintptr_t)(rhs) + (hash)->dth_stroffs))) == 0)
403 #define DTRACE_AGGHASHSIZE_SLEW 17
405 #define DTRACE_V4MAPPED_OFFSET (sizeof (uint32_t) * 3)
408 * The key for a thread-local variable consists of the lower 61 bits of the
409 * t_did, plus the 3 bits of the highest active interrupt above LOCK_LEVEL.
410 * We add DIF_VARIABLE_MAX to t_did to assure that the thread key is never
411 * equal to a variable identifier. This is necessary (but not sufficient) to
412 * assure that global associative arrays never collide with thread-local
413 * variables. To guarantee that they cannot collide, we must also define the
414 * order for keying dynamic variables. That order is:
416 * [ key0 ] ... [ keyn ] [ variable-key ] [ tls-key ]
418 * Because the variable-key and the tls-key are in orthogonal spaces, there is
419 * no way for a global variable key signature to match a thread-local key
422 #if !defined(__APPLE__)
423 #define DTRACE_TLS_THRKEY(where) { \
425 uint_t actv = CPU->cpu_intr_actv >> (LOCK_LEVEL + 1); \
426 for (; actv; actv >>= 1) \
428 ASSERT(intr < (1 << 3)); \
429 (where) = ((curthread->t_did + DIF_VARIABLE_MAX) & \
430 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \
433 #if (defined(__x86_64__) || defined(__ppc64__))
434 /* FIXME: two function calls!! */
435 #define DTRACE_TLS_THRKEY(where) { \
436 uint_t intr = ml_at_interrupt_context(); /* Note: just one measly bit */ \
437 uint64_t thr = (uintptr_t)current_thread(); \
438 ASSERT(intr < (1 << 3)); \
439 (where) = ((thr + DIF_VARIABLE_MAX) & \
440 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \
443 /* FIXME: three function calls!!! */
444 #define DTRACE_TLS_THRKEY(where) { \
445 uint_t intr = ml_at_interrupt_context(); /* Note: just one measly bit */ \
446 uint64_t thr = (uintptr_t)current_thread(); \
447 uint_t pid = (uint_t)proc_selfpid(); \
448 ASSERT(intr < (1 << 3)); \
449 (where) = (((thr << 32 | pid) + DIF_VARIABLE_MAX) & \
450 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \
453 #endif /* __APPLE__ */
455 #define DT_BSWAP_8(x) ((x) & 0xff)
456 #define DT_BSWAP_16(x) ((DT_BSWAP_8(x) << 8) | DT_BSWAP_8((x) >> 8))
457 #define DT_BSWAP_32(x) ((DT_BSWAP_16(x) << 16) | DT_BSWAP_16((x) >> 16))
458 #define DT_BSWAP_64(x) ((DT_BSWAP_32(x) << 32) | DT_BSWAP_32((x) >> 32))
460 #define DT_MASK_LO 0x00000000FFFFFFFFULL
462 #define DTRACE_STORE(type, tomax, offset, what) \
463 *((type *)((uintptr_t)(tomax) + (uintptr_t)offset)) = (type)(what);
465 #if !defined(__APPLE__)
467 #define DTRACE_ALIGNCHECK(addr, size, flags) \
468 if (addr & (size - 1)) { \
469 *flags |= CPU_DTRACE_BADALIGN; \
470 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \
474 #define DTRACE_ALIGNCHECK(addr, size, flags)
476 #else /* __APPLE__ */
477 #define DTRACE_ALIGNCHECK(addr, size, flags) \
478 if (addr & (MIN(size,4) - 1)) { \
479 *flags |= CPU_DTRACE_BADALIGN; \
480 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \
483 #endif /* __APPLE__ */
486 * Test whether a range of memory starting at testaddr of size testsz falls
487 * within the range of memory described by addr, sz. We take care to avoid
488 * problems with overflow and underflow of the unsigned quantities, and
489 * disallow all negative sizes. Ranges of size 0 are allowed.
491 #define DTRACE_INRANGE(testaddr, testsz, baseaddr, basesz) \
492 ((testaddr) - (baseaddr) < (basesz) && \
493 (testaddr) + (testsz) - (baseaddr) <= (basesz) && \
494 (testaddr) + (testsz) >= (testaddr))
497 * Test whether alloc_sz bytes will fit in the scratch region. We isolate
498 * alloc_sz on the righthand side of the comparison in order to avoid overflow
499 * or underflow in the comparison with it. This is simpler than the INRANGE
500 * check above, because we know that the dtms_scratch_ptr is valid in the
501 * range. Allocations of size zero are allowed.
503 #define DTRACE_INSCRATCH(mstate, alloc_sz) \
504 ((mstate)->dtms_scratch_base + (mstate)->dtms_scratch_size - \
505 (mstate)->dtms_scratch_ptr >= (alloc_sz))
507 #if !defined(__APPLE__)
508 #define DTRACE_LOADFUNC(bits) \
511 dtrace_load##bits(uintptr_t addr) \
513 size_t size = bits / NBBY; \
515 uint##bits##_t rval; \
517 volatile uint16_t *flags = (volatile uint16_t *) \
518 &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; \
520 DTRACE_ALIGNCHECK(addr, size, flags); \
522 for (i = 0; i < dtrace_toxranges; i++) { \
523 if (addr >= dtrace_toxrange[i].dtt_limit) \
526 if (addr + size <= dtrace_toxrange[i].dtt_base) \
530 * This address falls within a toxic region; return 0. \
532 *flags |= CPU_DTRACE_BADADDR; \
533 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \
537 *flags |= CPU_DTRACE_NOFAULT; \
539 rval = *((volatile uint##bits##_t *)addr); \
540 *flags &= ~CPU_DTRACE_NOFAULT; \
542 return (!(*flags & CPU_DTRACE_FAULT) ? rval : 0); \
544 #else /* __APPLE__ */
545 #define RECOVER_LABEL(bits) __asm__ volatile("_dtraceLoadRecover" #bits ":" );
547 #if (defined(__i386__) || defined (__x86_64__))
548 #define DTRACE_LOADFUNC(bits) \
550 extern vm_offset_t dtraceLoadRecover##bits; \
551 uint##bits##_t dtrace_load##bits(uintptr_t addr); \
554 dtrace_load##bits(uintptr_t addr) \
556 size_t size = bits / NBBY; \
558 uint##bits##_t rval = 0; \
560 volatile uint16_t *flags = (volatile uint16_t *) \
561 &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; \
563 DTRACE_ALIGNCHECK(addr, size, flags); \
565 for (i = 0; i < dtrace_toxranges; i++) { \
566 if (addr >= dtrace_toxrange[i].dtt_limit) \
569 if (addr + size <= dtrace_toxrange[i].dtt_base) \
573 * This address falls within a toxic region; return 0. \
575 *flags |= CPU_DTRACE_BADADDR; \
576 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \
581 volatile vm_offset_t recover = (vm_offset_t)&dtraceLoadRecover##bits; \
582 *flags |= CPU_DTRACE_NOFAULT; \
583 recover = dtrace_set_thread_recover(current_thread(), recover); \
586 * PR6394061 - avoid device memory that is unpredictably \
587 * mapped and unmapped \
589 if (pmap_valid_page(pmap_find_phys(kernel_pmap, addr))) \
590 rval = *((volatile uint##bits##_t *)addr); \
591 RECOVER_LABEL(bits); \
592 (void)dtrace_set_thread_recover(current_thread(), recover); \
593 *flags &= ~CPU_DTRACE_NOFAULT; \
598 #else /* all other architectures */
599 #define DTRACE_LOADFUNC(bits) \
601 extern vm_offset_t dtraceLoadRecover##bits; \
602 uint##bits##_t dtrace_load##bits(uintptr_t addr); \
605 dtrace_load##bits(uintptr_t addr) \
607 size_t size = bits / NBBY; \
609 uint##bits##_t rval = 0; \
611 volatile uint16_t *flags = (volatile uint16_t *) \
612 &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; \
614 DTRACE_ALIGNCHECK(addr, size, flags); \
616 for (i = 0; i < dtrace_toxranges; i++) { \
617 if (addr >= dtrace_toxrange[i].dtt_limit) \
620 if (addr + size <= dtrace_toxrange[i].dtt_base) \
624 * This address falls within a toxic region; return 0. \
626 *flags |= CPU_DTRACE_BADADDR; \
627 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \
632 volatile vm_offset_t recover = (vm_offset_t)&dtraceLoadRecover##bits; \
633 *flags |= CPU_DTRACE_NOFAULT; \
634 recover = dtrace_set_thread_recover(current_thread(), recover); \
636 rval = *((volatile uint##bits##_t *)addr); \
637 RECOVER_LABEL(bits); \
638 (void)dtrace_set_thread_recover(current_thread(), recover); \
639 *flags &= ~CPU_DTRACE_NOFAULT; \
645 #endif /* __APPLE__ */
648 #define dtrace_loadptr dtrace_load64
650 #define dtrace_loadptr dtrace_load32
653 #define DTRACE_DYNHASH_FREE 0
654 #define DTRACE_DYNHASH_SINK 1
655 #define DTRACE_DYNHASH_VALID 2
657 #define DTRACE_MATCH_NEXT 0
658 #define DTRACE_MATCH_DONE 1
659 #define DTRACE_ANCHORED(probe) ((probe)->dtpr_func[0] != '\0')
660 #define DTRACE_STATE_ALIGN 64
662 #define DTRACE_FLAGS2FLT(flags) \
663 (((flags) & CPU_DTRACE_BADADDR) ? DTRACEFLT_BADADDR : \
664 ((flags) & CPU_DTRACE_ILLOP) ? DTRACEFLT_ILLOP : \
665 ((flags) & CPU_DTRACE_DIVZERO) ? DTRACEFLT_DIVZERO : \
666 ((flags) & CPU_DTRACE_KPRIV) ? DTRACEFLT_KPRIV : \
667 ((flags) & CPU_DTRACE_UPRIV) ? DTRACEFLT_UPRIV : \
668 ((flags) & CPU_DTRACE_TUPOFLOW) ? DTRACEFLT_TUPOFLOW : \
669 ((flags) & CPU_DTRACE_BADALIGN) ? DTRACEFLT_BADALIGN : \
670 ((flags) & CPU_DTRACE_NOSCRATCH) ? DTRACEFLT_NOSCRATCH : \
671 ((flags) & CPU_DTRACE_BADSTACK) ? DTRACEFLT_BADSTACK : \
674 #define DTRACEACT_ISSTRING(act) \
675 ((act)->dta_kind == DTRACEACT_DIFEXPR && \
676 (act)->dta_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING)
679 #if defined (__APPLE__)
680 /* Avoid compiler warnings when assigning regs[rd] = NULL */
683 #define NULL (uintptr_t)0
685 #endif /* __APPLE__ */
687 static size_t dtrace_strlen(const char *, size_t);
688 static dtrace_probe_t
*dtrace_probe_lookup_id(dtrace_id_t id
);
689 static void dtrace_enabling_provide(dtrace_provider_t
*);
690 static int dtrace_enabling_match(dtrace_enabling_t
*, int *);
691 static void dtrace_enabling_matchall(void);
692 static dtrace_state_t
*dtrace_anon_grab(void);
693 static uint64_t dtrace_helper(int, dtrace_mstate_t
*,
694 dtrace_state_t
*, uint64_t, uint64_t);
695 static dtrace_helpers_t
*dtrace_helpers_create(proc_t
*);
696 static void dtrace_buffer_drop(dtrace_buffer_t
*);
697 static intptr_t dtrace_buffer_reserve(dtrace_buffer_t
*, size_t, size_t,
698 dtrace_state_t
*, dtrace_mstate_t
*);
699 static int dtrace_state_option(dtrace_state_t
*, dtrace_optid_t
,
701 static int dtrace_ecb_create_enable(dtrace_probe_t
*, void *);
702 static void dtrace_helper_provider_destroy(dtrace_helper_provider_t
*);
705 * DTrace Probe Context Functions
707 * These functions are called from probe context. Because probe context is
708 * any context in which C may be called, arbitrarily locks may be held,
709 * interrupts may be disabled, we may be in arbitrary dispatched state, etc.
710 * As a result, functions called from probe context may only call other DTrace
711 * support functions -- they may not interact at all with the system at large.
712 * (Note that the ASSERT macro is made probe-context safe by redefining it in
713 * terms of dtrace_assfail(), a probe-context safe function.) If arbitrary
714 * loads are to be performed from probe context, they _must_ be in terms of
715 * the safe dtrace_load*() variants.
717 * Some functions in this block are not actually called from probe context;
718 * for these functions, there will be a comment above the function reading
719 * "Note: not called from probe context."
722 dtrace_panic(const char *format
, ...)
726 va_start(alist
, format
);
727 dtrace_vpanic(format
, alist
);
732 dtrace_assfail(const char *a
, const char *f
, int l
)
734 dtrace_panic("assertion failed: %s, file: %s, line: %d", a
, f
, l
);
737 * We just need something here that even the most clever compiler
738 * cannot optimize away.
740 return (a
[(uintptr_t)f
]);
744 * Atomically increment a specified error counter from probe context.
747 dtrace_error(uint32_t *counter
)
750 * Most counters stored to in probe context are per-CPU counters.
751 * However, there are some error conditions that are sufficiently
752 * arcane that they don't merit per-CPU storage. If these counters
753 * are incremented concurrently on different CPUs, scalability will be
754 * adversely affected -- but we don't expect them to be white-hot in a
755 * correctly constructed enabling...
762 if ((nval
= oval
+ 1) == 0) {
764 * If the counter would wrap, set it to 1 -- assuring
765 * that the counter is never zero when we have seen
766 * errors. (The counter must be 32-bits because we
767 * aren't guaranteed a 64-bit compare&swap operation.)
768 * To save this code both the infamy of being fingered
769 * by a priggish news story and the indignity of being
770 * the target of a neo-puritan witch trial, we're
771 * carefully avoiding any colorful description of the
772 * likelihood of this condition -- but suffice it to
773 * say that it is only slightly more likely than the
774 * overflow of predicate cache IDs, as discussed in
775 * dtrace_predicate_create().
779 } while (dtrace_cas32(counter
, oval
, nval
) != oval
);
783 * Use the DTRACE_LOADFUNC macro to define functions for each of loading a
784 * uint8_t, a uint16_t, a uint32_t and a uint64_t.
792 dtrace_inscratch(uintptr_t dest
, size_t size
, dtrace_mstate_t
*mstate
)
794 if (dest
< mstate
->dtms_scratch_base
)
797 if (dest
+ size
< dest
)
800 if (dest
+ size
> mstate
->dtms_scratch_ptr
)
807 dtrace_canstore_statvar(uint64_t addr
, size_t sz
,
808 dtrace_statvar_t
**svars
, int nsvars
)
812 for (i
= 0; i
< nsvars
; i
++) {
813 dtrace_statvar_t
*svar
= svars
[i
];
815 if (svar
== NULL
|| svar
->dtsv_size
== 0)
818 if (DTRACE_INRANGE(addr
, sz
, svar
->dtsv_data
, svar
->dtsv_size
))
826 * Check to see if the address is within a memory region to which a store may
827 * be issued. This includes the DTrace scratch areas, and any DTrace variable
828 * region. The caller of dtrace_canstore() is responsible for performing any
829 * alignment checks that are needed before stores are actually executed.
832 dtrace_canstore(uint64_t addr
, size_t sz
, dtrace_mstate_t
*mstate
,
833 dtrace_vstate_t
*vstate
)
836 * First, check to see if the address is in scratch space...
838 if (DTRACE_INRANGE(addr
, sz
, mstate
->dtms_scratch_base
,
839 mstate
->dtms_scratch_size
))
843 * Now check to see if it's a dynamic variable. This check will pick
844 * up both thread-local variables and any global dynamically-allocated
847 if (DTRACE_INRANGE(addr
, sz
, (uintptr_t)vstate
->dtvs_dynvars
.dtds_base
,
848 vstate
->dtvs_dynvars
.dtds_size
)) {
849 dtrace_dstate_t
*dstate
= &vstate
->dtvs_dynvars
;
850 uintptr_t base
= (uintptr_t)dstate
->dtds_base
+
851 (dstate
->dtds_hashsize
* sizeof (dtrace_dynhash_t
));
855 * Before we assume that we can store here, we need to make
856 * sure that it isn't in our metadata -- storing to our
857 * dynamic variable metadata would corrupt our state. For
858 * the range to not include any dynamic variable metadata,
861 * (1) Start above the hash table that is at the base of
862 * the dynamic variable space
864 * (2) Have a starting chunk offset that is beyond the
865 * dtrace_dynvar_t that is at the base of every chunk
867 * (3) Not span a chunk boundary
873 chunkoffs
= (addr
- base
) % dstate
->dtds_chunksize
;
875 if (chunkoffs
< sizeof (dtrace_dynvar_t
))
878 if (chunkoffs
+ sz
> dstate
->dtds_chunksize
)
885 * Finally, check the static local and global variables. These checks
886 * take the longest, so we perform them last.
888 if (dtrace_canstore_statvar(addr
, sz
,
889 vstate
->dtvs_locals
, vstate
->dtvs_nlocals
))
892 if (dtrace_canstore_statvar(addr
, sz
,
893 vstate
->dtvs_globals
, vstate
->dtvs_nglobals
))
901 * Convenience routine to check to see if the address is within a memory
902 * region in which a load may be issued given the user's privilege level;
903 * if not, it sets the appropriate error flags and loads 'addr' into the
904 * illegal value slot.
906 * DTrace subroutines (DIF_SUBR_*) should use this helper to implement
907 * appropriate memory access protection.
910 dtrace_canload(uint64_t addr
, size_t sz
, dtrace_mstate_t
*mstate
,
911 dtrace_vstate_t
*vstate
)
913 #if !defined(__APPLE__) /* Quiet compiler warning - matches dtrace_dif_emulate */
914 volatile uintptr_t *illval
= &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
;
916 volatile uint64_t *illval
= &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
;
920 * If we hold the privilege to read from kernel memory, then
921 * everything is readable.
923 if ((mstate
->dtms_access
& DTRACE_ACCESS_KERNEL
) != 0)
927 * You can obviously read that which you can store.
929 if (dtrace_canstore(addr
, sz
, mstate
, vstate
))
933 * We're allowed to read from our own string table.
935 if (DTRACE_INRANGE(addr
, sz
, (uintptr_t)mstate
->dtms_difo
->dtdo_strtab
,
936 mstate
->dtms_difo
->dtdo_strlen
))
939 DTRACE_CPUFLAG_SET(CPU_DTRACE_KPRIV
);
945 * Convenience routine to check to see if a given string is within a memory
946 * region in which a load may be issued given the user's privilege level;
947 * this exists so that we don't need to issue unnecessary dtrace_strlen()
948 * calls in the event that the user has all privileges.
951 dtrace_strcanload(uint64_t addr
, size_t sz
, dtrace_mstate_t
*mstate
,
952 dtrace_vstate_t
*vstate
)
957 * If we hold the privilege to read from kernel memory, then
958 * everything is readable.
960 if ((mstate
->dtms_access
& DTRACE_ACCESS_KERNEL
) != 0)
963 strsz
= 1 + dtrace_strlen((char *)(uintptr_t)addr
, sz
);
964 if (dtrace_canload(addr
, strsz
, mstate
, vstate
))
971 * Convenience routine to check to see if a given variable is within a memory
972 * region in which a load may be issued given the user's privilege level.
975 dtrace_vcanload(void *src
, dtrace_diftype_t
*type
, dtrace_mstate_t
*mstate
,
976 dtrace_vstate_t
*vstate
)
979 ASSERT(type
->dtdt_flags
& DIF_TF_BYREF
);
982 * If we hold the privilege to read from kernel memory, then
983 * everything is readable.
985 if ((mstate
->dtms_access
& DTRACE_ACCESS_KERNEL
) != 0)
988 if (type
->dtdt_kind
== DIF_TYPE_STRING
)
989 sz
= dtrace_strlen(src
,
990 vstate
->dtvs_state
->dts_options
[DTRACEOPT_STRSIZE
]) + 1;
992 sz
= type
->dtdt_size
;
994 return (dtrace_canload((uintptr_t)src
, sz
, mstate
, vstate
));
998 * Compare two strings using safe loads.
1001 dtrace_strncmp(char *s1
, char *s2
, size_t limit
)
1004 volatile uint16_t *flags
;
1006 if (s1
== s2
|| limit
== 0)
1009 flags
= (volatile uint16_t *)&cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
1015 c1
= dtrace_load8((uintptr_t)s1
++);
1021 c2
= dtrace_load8((uintptr_t)s2
++);
1026 } while (--limit
&& c1
!= '\0' && !(*flags
& CPU_DTRACE_FAULT
));
1032 * Compute strlen(s) for a string using safe memory accesses. The additional
1033 * len parameter is used to specify a maximum length to ensure completion.
1036 dtrace_strlen(const char *s
, size_t lim
)
1040 for (len
= 0; len
!= lim
; len
++) {
1041 if (dtrace_load8((uintptr_t)s
++) == '\0')
1049 * Check if an address falls within a toxic region.
1052 dtrace_istoxic(uintptr_t kaddr
, size_t size
)
1054 uintptr_t taddr
, tsize
;
1057 for (i
= 0; i
< dtrace_toxranges
; i
++) {
1058 taddr
= dtrace_toxrange
[i
].dtt_base
;
1059 tsize
= dtrace_toxrange
[i
].dtt_limit
- taddr
;
1061 if (kaddr
- taddr
< tsize
) {
1062 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1063 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= kaddr
;
1067 if (taddr
- kaddr
< size
) {
1068 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1069 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= taddr
;
1078 * Copy src to dst using safe memory accesses. The src is assumed to be unsafe
1079 * memory specified by the DIF program. The dst is assumed to be safe memory
1080 * that we can store to directly because it is managed by DTrace. As with
1081 * standard bcopy, overlapping copies are handled properly.
1084 dtrace_bcopy(const void *src
, void *dst
, size_t len
)
1088 const uint8_t *s2
= src
;
1092 *s1
++ = dtrace_load8((uintptr_t)s2
++);
1093 } while (--len
!= 0);
1099 *--s1
= dtrace_load8((uintptr_t)--s2
);
1100 } while (--len
!= 0);
1106 * Copy src to dst using safe memory accesses, up to either the specified
1107 * length, or the point that a nul byte is encountered. The src is assumed to
1108 * be unsafe memory specified by the DIF program. The dst is assumed to be
1109 * safe memory that we can store to directly because it is managed by DTrace.
1110 * Unlike dtrace_bcopy(), overlapping regions are not handled.
1113 dtrace_strcpy(const void *src
, void *dst
, size_t len
)
1116 uint8_t *s1
= dst
, c
;
1117 const uint8_t *s2
= src
;
1120 *s1
++ = c
= dtrace_load8((uintptr_t)s2
++);
1121 } while (--len
!= 0 && c
!= '\0');
1126 * Copy src to dst, deriving the size and type from the specified (BYREF)
1127 * variable type. The src is assumed to be unsafe memory specified by the DIF
1128 * program. The dst is assumed to be DTrace variable memory that is of the
1129 * specified type; we assume that we can store to directly.
1132 dtrace_vcopy(void *src
, void *dst
, dtrace_diftype_t
*type
)
1134 ASSERT(type
->dtdt_flags
& DIF_TF_BYREF
);
1136 if (type
->dtdt_kind
== DIF_TYPE_STRING
) {
1137 dtrace_strcpy(src
, dst
, type
->dtdt_size
);
1139 dtrace_bcopy(src
, dst
, type
->dtdt_size
);
1144 * Compare s1 to s2 using safe memory accesses. The s1 data is assumed to be
1145 * unsafe memory specified by the DIF program. The s2 data is assumed to be
1146 * safe memory that we can access directly because it is managed by DTrace.
1149 dtrace_bcmp(const void *s1
, const void *s2
, size_t len
)
1151 volatile uint16_t *flags
;
1153 flags
= (volatile uint16_t *)&cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
1158 if (s1
== NULL
|| s2
== NULL
)
1161 if (s1
!= s2
&& len
!= 0) {
1162 const uint8_t *ps1
= s1
;
1163 const uint8_t *ps2
= s2
;
1166 if (dtrace_load8((uintptr_t)ps1
++) != *ps2
++)
1168 } while (--len
!= 0 && !(*flags
& CPU_DTRACE_FAULT
));
1174 * Zero the specified region using a simple byte-by-byte loop. Note that this
1175 * is for safe DTrace-managed memory only.
1178 dtrace_bzero(void *dst
, size_t len
)
1182 for (cp
= dst
; len
!= 0; len
--)
1187 dtrace_add_128(uint64_t *addend1
, uint64_t *addend2
, uint64_t *sum
)
1191 result
[0] = addend1
[0] + addend2
[0];
1192 result
[1] = addend1
[1] + addend2
[1] +
1193 (result
[0] < addend1
[0] || result
[0] < addend2
[0] ? 1 : 0);
1200 * Shift the 128-bit value in a by b. If b is positive, shift left.
1201 * If b is negative, shift right.
1204 dtrace_shift_128(uint64_t *a
, int b
)
1214 a
[0] = a
[1] >> (b
- 64);
1218 mask
= 1LL << (64 - b
);
1220 a
[0] |= ((a
[1] & mask
) << (64 - b
));
1225 a
[1] = a
[0] << (b
- 64);
1229 mask
= a
[0] >> (64 - b
);
1237 * The basic idea is to break the 2 64-bit values into 4 32-bit values,
1238 * use native multiplication on those, and then re-combine into the
1239 * resulting 128-bit value.
1241 * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) =
1248 dtrace_multiply_128(uint64_t factor1
, uint64_t factor2
, uint64_t *product
)
1250 uint64_t hi1
, hi2
, lo1
, lo2
;
1253 hi1
= factor1
>> 32;
1254 hi2
= factor2
>> 32;
1256 lo1
= factor1
& DT_MASK_LO
;
1257 lo2
= factor2
& DT_MASK_LO
;
1259 product
[0] = lo1
* lo2
;
1260 product
[1] = hi1
* hi2
;
1264 dtrace_shift_128(tmp
, 32);
1265 dtrace_add_128(product
, tmp
, product
);
1269 dtrace_shift_128(tmp
, 32);
1270 dtrace_add_128(product
, tmp
, product
);
1274 * This privilege check should be used by actions and subroutines to
1275 * verify that the user credentials of the process that enabled the
1276 * invoking ECB match the target credentials
1279 dtrace_priv_proc_common_user(dtrace_state_t
*state
)
1281 cred_t
*cr
, *s_cr
= state
->dts_cred
.dcr_cred
;
1284 * We should always have a non-NULL state cred here, since if cred
1285 * is null (anonymous tracing), we fast-path bypass this routine.
1287 ASSERT(s_cr
!= NULL
);
1289 #if !defined(__APPLE__)
1290 if ((cr
= CRED()) != NULL
&&
1292 if ((cr
= dtrace_CRED()) != NULL
&&
1293 #endif /* __APPLE__ */
1294 s_cr
->cr_uid
== cr
->cr_uid
&&
1295 s_cr
->cr_uid
== cr
->cr_ruid
&&
1296 s_cr
->cr_uid
== cr
->cr_suid
&&
1297 s_cr
->cr_gid
== cr
->cr_gid
&&
1298 s_cr
->cr_gid
== cr
->cr_rgid
&&
1299 s_cr
->cr_gid
== cr
->cr_sgid
)
1306 * This privilege check should be used by actions and subroutines to
1307 * verify that the zone of the process that enabled the invoking ECB
1308 * matches the target credentials
1311 dtrace_priv_proc_common_zone(dtrace_state_t
*state
)
1313 cred_t
*cr
, *s_cr
= state
->dts_cred
.dcr_cred
;
1314 #pragma unused(cr, s_cr) /* __APPLE__ */
1317 * We should always have a non-NULL state cred here, since if cred
1318 * is null (anonymous tracing), we fast-path bypass this routine.
1320 ASSERT(s_cr
!= NULL
);
1322 #if !defined(__APPLE__)
1323 if ((cr
= CRED()) != NULL
&&
1324 s_cr
->cr_zone
== cr
->cr_zone
)
1329 #pragma unused(state)
1331 return 1; /* Darwin doesn't do zones. */
1332 #endif /* __APPLE__ */
1336 * This privilege check should be used by actions and subroutines to
1337 * verify that the process has not setuid or changed credentials.
1339 #if !defined(__APPLE__)
1341 dtrace_priv_proc_common_nocd()
1345 if ((proc
= ttoproc(curthread
)) != NULL
&&
1346 !(proc
->p_flag
& SNOCD
))
1353 dtrace_priv_proc_common_nocd(void)
1355 return 1; /* Darwin omits "No Core Dump" flag. */
1357 #endif /* __APPLE__ */
1360 dtrace_priv_proc_destructive(dtrace_state_t
*state
)
1362 int action
= state
->dts_cred
.dcr_action
;
1364 #if defined(__APPLE__)
1365 if (ISSET(current_proc()->p_lflag
, P_LNOATTACH
))
1367 #endif /* __APPLE__ */
1369 if (((action
& DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE
) == 0) &&
1370 dtrace_priv_proc_common_zone(state
) == 0)
1373 if (((action
& DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER
) == 0) &&
1374 dtrace_priv_proc_common_user(state
) == 0)
1377 if (((action
& DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG
) == 0) &&
1378 dtrace_priv_proc_common_nocd() == 0)
1384 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
|= CPU_DTRACE_UPRIV
;
1390 dtrace_priv_proc_control(dtrace_state_t
*state
)
1392 #if defined(__APPLE__)
1393 if (ISSET(current_proc()->p_lflag
, P_LNOATTACH
))
1395 #endif /* __APPLE__ */
1397 if (state
->dts_cred
.dcr_action
& DTRACE_CRA_PROC_CONTROL
)
1400 if (dtrace_priv_proc_common_zone(state
) &&
1401 dtrace_priv_proc_common_user(state
) &&
1402 dtrace_priv_proc_common_nocd())
1405 #if defined(__APPLE__)
1407 #endif /* __APPLE__ */
1408 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
|= CPU_DTRACE_UPRIV
;
1414 dtrace_priv_proc(dtrace_state_t
*state
)
1416 #if defined(__APPLE__)
1417 if (ISSET(current_proc()->p_lflag
, P_LNOATTACH
))
1419 #endif /* __APPLE__ */
1421 if (state
->dts_cred
.dcr_action
& DTRACE_CRA_PROC
)
1424 #if defined(__APPLE__)
1426 #endif /* __APPLE__ */
1427 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
|= CPU_DTRACE_UPRIV
;
1432 #if defined(__APPLE__)
1433 /* dtrace_priv_proc() omitting the P_LNOATTACH check. For PID and EXECNAME accesses. */
1435 dtrace_priv_proc_relaxed(dtrace_state_t
*state
)
1438 if (state
->dts_cred
.dcr_action
& DTRACE_CRA_PROC
)
1441 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
|= CPU_DTRACE_UPRIV
;
1445 #endif /* __APPLE__ */
1448 dtrace_priv_kernel(dtrace_state_t
*state
)
1450 if (state
->dts_cred
.dcr_action
& DTRACE_CRA_KERNEL
)
1453 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
|= CPU_DTRACE_KPRIV
;
1459 dtrace_priv_kernel_destructive(dtrace_state_t
*state
)
1461 if (state
->dts_cred
.dcr_action
& DTRACE_CRA_KERNEL_DESTRUCTIVE
)
1464 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
|= CPU_DTRACE_KPRIV
;
1470 * Note: not called from probe context. This function is called
1471 * asynchronously (and at a regular interval) from outside of probe context to
1472 * clean the dirty dynamic variable lists on all CPUs. Dynamic variable
1473 * cleaning is explained in detail in <sys/dtrace_impl.h>.
1475 #if defined(__APPLE__) /* Quiet compiler warning. */
1477 #endif /* __APPLE__ */
1479 dtrace_dynvar_clean(dtrace_dstate_t
*dstate
)
1481 dtrace_dynvar_t
*dirty
;
1482 dtrace_dstate_percpu_t
*dcpu
;
1485 for (i
= 0; i
< (int)NCPU
; i
++) {
1486 dcpu
= &dstate
->dtds_percpu
[i
];
1488 ASSERT(dcpu
->dtdsc_rinsing
== NULL
);
1491 * If the dirty list is NULL, there is no dirty work to do.
1493 if (dcpu
->dtdsc_dirty
== NULL
)
1497 * If the clean list is non-NULL, then we're not going to do
1498 * any work for this CPU -- it means that there has not been
1499 * a dtrace_dynvar() allocation on this CPU (or from this CPU)
1500 * since the last time we cleaned house.
1502 if (dcpu
->dtdsc_clean
!= NULL
)
1508 * Atomically move the dirty list aside.
1511 dirty
= dcpu
->dtdsc_dirty
;
1514 * Before we zap the dirty list, set the rinsing list.
1515 * (This allows for a potential assertion in
1516 * dtrace_dynvar(): if a free dynamic variable appears
1517 * on a hash chain, either the dirty list or the
1518 * rinsing list for some CPU must be non-NULL.)
1520 dcpu
->dtdsc_rinsing
= dirty
;
1521 dtrace_membar_producer();
1522 } while (dtrace_casptr(&dcpu
->dtdsc_dirty
,
1523 dirty
, NULL
) != dirty
);
1528 * We have no work to do; we can simply return.
1535 for (i
= 0; i
< (int)NCPU
; i
++) {
1536 dcpu
= &dstate
->dtds_percpu
[i
];
1538 if (dcpu
->dtdsc_rinsing
== NULL
)
1542 * We are now guaranteed that no hash chain contains a pointer
1543 * into this dirty list; we can make it clean.
1545 ASSERT(dcpu
->dtdsc_clean
== NULL
);
1546 dcpu
->dtdsc_clean
= dcpu
->dtdsc_rinsing
;
1547 dcpu
->dtdsc_rinsing
= NULL
;
1551 * Before we actually set the state to be DTRACE_DSTATE_CLEAN, make
1552 * sure that all CPUs have seen all of the dtdsc_clean pointers.
1553 * This prevents a race whereby a CPU incorrectly decides that
1554 * the state should be something other than DTRACE_DSTATE_CLEAN
1555 * after dtrace_dynvar_clean() has completed.
1559 dstate
->dtds_state
= DTRACE_DSTATE_CLEAN
;
1563 * Depending on the value of the op parameter, this function looks-up,
1564 * allocates or deallocates an arbitrarily-keyed dynamic variable. If an
1565 * allocation is requested, this function will return a pointer to a
1566 * dtrace_dynvar_t corresponding to the allocated variable -- or NULL if no
1567 * variable can be allocated. If NULL is returned, the appropriate counter
1568 * will be incremented.
1570 #if defined(__APPLE__) /* Quiet compiler warning. */
1572 #endif /* __APPLE__ */
1574 dtrace_dynvar(dtrace_dstate_t
*dstate
, uint_t nkeys
,
1575 dtrace_key_t
*key
, size_t dsize
, dtrace_dynvar_op_t op
,
1576 dtrace_mstate_t
*mstate
, dtrace_vstate_t
*vstate
)
1578 uint64_t hashval
= DTRACE_DYNHASH_VALID
;
1579 dtrace_dynhash_t
*hash
= dstate
->dtds_hash
;
1580 dtrace_dynvar_t
*free
, *new_free
, *next
, *dvar
, *start
, *prev
= NULL
;
1581 processorid_t me
= CPU
->cpu_id
, cpu
= me
;
1582 dtrace_dstate_percpu_t
*dcpu
= &dstate
->dtds_percpu
[me
];
1583 size_t bucket
, ksize
;
1584 size_t chunksize
= dstate
->dtds_chunksize
;
1585 uintptr_t kdata
, lock
, nstate
;
1591 * Hash the key. As with aggregations, we use Jenkins' "One-at-a-time"
1592 * algorithm. For the by-value portions, we perform the algorithm in
1593 * 16-bit chunks (as opposed to 8-bit chunks). This speeds things up a
1594 * bit, and seems to have only a minute effect on distribution. For
1595 * the by-reference data, we perform "One-at-a-time" iterating (safely)
1596 * over each referenced byte. It's painful to do this, but it's much
1597 * better than pathological hash distribution. The efficacy of the
1598 * hashing algorithm (and a comparison with other algorithms) may be
1599 * found by running the ::dtrace_dynstat MDB dcmd.
1601 for (i
= 0; i
< nkeys
; i
++) {
1602 if (key
[i
].dttk_size
== 0) {
1603 uint64_t val
= key
[i
].dttk_value
;
1605 hashval
+= (val
>> 48) & 0xffff;
1606 hashval
+= (hashval
<< 10);
1607 hashval
^= (hashval
>> 6);
1609 hashval
+= (val
>> 32) & 0xffff;
1610 hashval
+= (hashval
<< 10);
1611 hashval
^= (hashval
>> 6);
1613 hashval
+= (val
>> 16) & 0xffff;
1614 hashval
+= (hashval
<< 10);
1615 hashval
^= (hashval
>> 6);
1617 hashval
+= val
& 0xffff;
1618 hashval
+= (hashval
<< 10);
1619 hashval
^= (hashval
>> 6);
1622 * This is incredibly painful, but it beats the hell
1623 * out of the alternative.
1625 uint64_t j
, size
= key
[i
].dttk_size
;
1626 uintptr_t base
= (uintptr_t)key
[i
].dttk_value
;
1628 if (!dtrace_canload(base
, size
, mstate
, vstate
))
1631 for (j
= 0; j
< size
; j
++) {
1632 hashval
+= dtrace_load8(base
+ j
);
1633 hashval
+= (hashval
<< 10);
1634 hashval
^= (hashval
>> 6);
1639 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT
))
1642 hashval
+= (hashval
<< 3);
1643 hashval
^= (hashval
>> 11);
1644 hashval
+= (hashval
<< 15);
1647 * There is a remote chance (ideally, 1 in 2^31) that our hashval
1648 * comes out to be one of our two sentinel hash values. If this
1649 * actually happens, we set the hashval to be a value known to be a
1650 * non-sentinel value.
1652 if (hashval
== DTRACE_DYNHASH_FREE
|| hashval
== DTRACE_DYNHASH_SINK
)
1653 hashval
= DTRACE_DYNHASH_VALID
;
1656 * Yes, it's painful to do a divide here. If the cycle count becomes
1657 * important here, tricks can be pulled to reduce it. (However, it's
1658 * critical that hash collisions be kept to an absolute minimum;
1659 * they're much more painful than a divide.) It's better to have a
1660 * solution that generates few collisions and still keeps things
1661 * relatively simple.
1663 bucket
= hashval
% dstate
->dtds_hashsize
;
1665 if (op
== DTRACE_DYNVAR_DEALLOC
) {
1666 volatile uintptr_t *lockp
= &hash
[bucket
].dtdh_lock
;
1669 while ((lock
= *lockp
) & 1)
1672 #if !defined(__APPLE__) /* Quiet compiler warning */
1673 if (dtrace_casptr((void *)lockp
,
1674 (void *)lock
, (void *)(lock
+ 1)) == (void *)lock
)
1677 if (dtrace_casptr((void *)(uintptr_t)lockp
,
1678 (void *)lock
, (void *)(lock
+ 1)) == (void *)lock
)
1680 #endif /* __APPLE__ */
1683 dtrace_membar_producer();
1688 lock
= hash
[bucket
].dtdh_lock
;
1690 dtrace_membar_consumer();
1692 start
= hash
[bucket
].dtdh_chain
;
1693 ASSERT(start
!= NULL
&& (start
->dtdv_hashval
== DTRACE_DYNHASH_SINK
||
1694 start
->dtdv_hashval
!= DTRACE_DYNHASH_FREE
||
1695 op
!= DTRACE_DYNVAR_DEALLOC
));
1697 for (dvar
= start
; dvar
!= NULL
; dvar
= dvar
->dtdv_next
) {
1698 dtrace_tuple_t
*dtuple
= &dvar
->dtdv_tuple
;
1699 dtrace_key_t
*dkey
= &dtuple
->dtt_key
[0];
1701 if (dvar
->dtdv_hashval
!= hashval
) {
1702 if (dvar
->dtdv_hashval
== DTRACE_DYNHASH_SINK
) {
1704 * We've reached the sink, and therefore the
1705 * end of the hash chain; we can kick out of
1706 * the loop knowing that we have seen a valid
1707 * snapshot of state.
1709 ASSERT(dvar
->dtdv_next
== NULL
);
1710 ASSERT(dvar
== &dtrace_dynhash_sink
);
1714 if (dvar
->dtdv_hashval
== DTRACE_DYNHASH_FREE
) {
1716 * We've gone off the rails: somewhere along
1717 * the line, one of the members of this hash
1718 * chain was deleted. Note that we could also
1719 * detect this by simply letting this loop run
1720 * to completion, as we would eventually hit
1721 * the end of the dirty list. However, we
1722 * want to avoid running the length of the
1723 * dirty list unnecessarily (it might be quite
1724 * long), so we catch this as early as
1725 * possible by detecting the hash marker. In
1726 * this case, we simply set dvar to NULL and
1727 * break; the conditional after the loop will
1728 * send us back to top.
1737 if (dtuple
->dtt_nkeys
!= nkeys
)
1740 for (i
= 0; i
< nkeys
; i
++, dkey
++) {
1741 if (dkey
->dttk_size
!= key
[i
].dttk_size
)
1742 goto next
; /* size or type mismatch */
1744 if (dkey
->dttk_size
!= 0) {
1746 (void *)(uintptr_t)key
[i
].dttk_value
,
1747 (void *)(uintptr_t)dkey
->dttk_value
,
1751 if (dkey
->dttk_value
!= key
[i
].dttk_value
)
1756 if (op
!= DTRACE_DYNVAR_DEALLOC
)
1759 ASSERT(dvar
->dtdv_next
== NULL
||
1760 dvar
->dtdv_next
->dtdv_hashval
!= DTRACE_DYNHASH_FREE
);
1763 ASSERT(hash
[bucket
].dtdh_chain
!= dvar
);
1764 ASSERT(start
!= dvar
);
1765 ASSERT(prev
->dtdv_next
== dvar
);
1766 prev
->dtdv_next
= dvar
->dtdv_next
;
1768 if (dtrace_casptr(&hash
[bucket
].dtdh_chain
,
1769 start
, dvar
->dtdv_next
) != start
) {
1771 * We have failed to atomically swing the
1772 * hash table head pointer, presumably because
1773 * of a conflicting allocation on another CPU.
1774 * We need to reread the hash chain and try
1781 dtrace_membar_producer();
1784 * Now set the hash value to indicate that it's free.
1786 ASSERT(hash
[bucket
].dtdh_chain
!= dvar
);
1787 dvar
->dtdv_hashval
= DTRACE_DYNHASH_FREE
;
1789 dtrace_membar_producer();
1792 * Set the next pointer to point at the dirty list, and
1793 * atomically swing the dirty pointer to the newly freed dvar.
1796 next
= dcpu
->dtdsc_dirty
;
1797 dvar
->dtdv_next
= next
;
1798 } while (dtrace_casptr(&dcpu
->dtdsc_dirty
, next
, dvar
) != next
);
1801 * Finally, unlock this hash bucket.
1803 ASSERT(hash
[bucket
].dtdh_lock
== lock
);
1805 hash
[bucket
].dtdh_lock
++;
1815 * If dvar is NULL, it is because we went off the rails:
1816 * one of the elements that we traversed in the hash chain
1817 * was deleted while we were traversing it. In this case,
1818 * we assert that we aren't doing a dealloc (deallocs lock
1819 * the hash bucket to prevent themselves from racing with
1820 * one another), and retry the hash chain traversal.
1822 ASSERT(op
!= DTRACE_DYNVAR_DEALLOC
);
1826 if (op
!= DTRACE_DYNVAR_ALLOC
) {
1828 * If we are not to allocate a new variable, we want to
1829 * return NULL now. Before we return, check that the value
1830 * of the lock word hasn't changed. If it has, we may have
1831 * seen an inconsistent snapshot.
1833 if (op
== DTRACE_DYNVAR_NOALLOC
) {
1834 if (hash
[bucket
].dtdh_lock
!= lock
)
1837 ASSERT(op
== DTRACE_DYNVAR_DEALLOC
);
1838 ASSERT(hash
[bucket
].dtdh_lock
== lock
);
1840 hash
[bucket
].dtdh_lock
++;
1847 * We need to allocate a new dynamic variable. The size we need is the
1848 * size of dtrace_dynvar plus the size of nkeys dtrace_key_t's plus the
1849 * size of any auxiliary key data (rounded up to 8-byte alignment) plus
1850 * the size of any referred-to data (dsize). We then round the final
1851 * size up to the chunksize for allocation.
1853 for (ksize
= 0, i
= 0; i
< nkeys
; i
++)
1854 ksize
+= P2ROUNDUP(key
[i
].dttk_size
, sizeof (uint64_t));
1857 * This should be pretty much impossible, but could happen if, say,
1858 * strange DIF specified the tuple. Ideally, this should be an
1859 * assertion and not an error condition -- but that requires that the
1860 * chunksize calculation in dtrace_difo_chunksize() be absolutely
1861 * bullet-proof. (That is, it must not be able to be fooled by
1862 * malicious DIF.) Given the lack of backwards branches in DIF,
1863 * solving this would presumably not amount to solving the Halting
1864 * Problem -- but it still seems awfully hard.
1866 if (sizeof (dtrace_dynvar_t
) + sizeof (dtrace_key_t
) * (nkeys
- 1) +
1867 ksize
+ dsize
> chunksize
) {
1868 dcpu
->dtdsc_drops
++;
1872 nstate
= DTRACE_DSTATE_EMPTY
;
1876 free
= dcpu
->dtdsc_free
;
1879 dtrace_dynvar_t
*clean
= dcpu
->dtdsc_clean
;
1882 if (clean
== NULL
) {
1884 * We're out of dynamic variable space on
1885 * this CPU. Unless we have tried all CPUs,
1886 * we'll try to allocate from a different
1889 switch (dstate
->dtds_state
) {
1890 case DTRACE_DSTATE_CLEAN
: {
1891 void *sp
= &dstate
->dtds_state
;
1893 if (++cpu
>= (int)NCPU
)
1896 if (dcpu
->dtdsc_dirty
!= NULL
&&
1897 nstate
== DTRACE_DSTATE_EMPTY
)
1898 nstate
= DTRACE_DSTATE_DIRTY
;
1900 if (dcpu
->dtdsc_rinsing
!= NULL
)
1901 nstate
= DTRACE_DSTATE_RINSING
;
1903 dcpu
= &dstate
->dtds_percpu
[cpu
];
1908 (void) dtrace_cas32(sp
,
1909 DTRACE_DSTATE_CLEAN
, nstate
);
1912 * To increment the correct bean
1913 * counter, take another lap.
1918 case DTRACE_DSTATE_DIRTY
:
1919 dcpu
->dtdsc_dirty_drops
++;
1922 case DTRACE_DSTATE_RINSING
:
1923 dcpu
->dtdsc_rinsing_drops
++;
1926 case DTRACE_DSTATE_EMPTY
:
1927 dcpu
->dtdsc_drops
++;
1931 DTRACE_CPUFLAG_SET(CPU_DTRACE_DROP
);
1936 * The clean list appears to be non-empty. We want to
1937 * move the clean list to the free list; we start by
1938 * moving the clean pointer aside.
1940 if (dtrace_casptr(&dcpu
->dtdsc_clean
,
1941 clean
, NULL
) != clean
) {
1943 * We are in one of two situations:
1945 * (a) The clean list was switched to the
1946 * free list by another CPU.
1948 * (b) The clean list was added to by the
1951 * In either of these situations, we can
1952 * just reattempt the free list allocation.
1957 ASSERT(clean
->dtdv_hashval
== DTRACE_DYNHASH_FREE
);
1960 * Now we'll move the clean list to the free list.
1961 * It's impossible for this to fail: the only way
1962 * the free list can be updated is through this
1963 * code path, and only one CPU can own the clean list.
1964 * Thus, it would only be possible for this to fail if
1965 * this code were racing with dtrace_dynvar_clean().
1966 * (That is, if dtrace_dynvar_clean() updated the clean
1967 * list, and we ended up racing to update the free
1968 * list.) This race is prevented by the dtrace_sync()
1969 * in dtrace_dynvar_clean() -- which flushes the
1970 * owners of the clean lists out before resetting
1973 rval
= dtrace_casptr(&dcpu
->dtdsc_free
, NULL
, clean
);
1974 ASSERT(rval
== NULL
);
1979 new_free
= dvar
->dtdv_next
;
1980 } while (dtrace_casptr(&dcpu
->dtdsc_free
, free
, new_free
) != free
);
1983 * We have now allocated a new chunk. We copy the tuple keys into the
1984 * tuple array and copy any referenced key data into the data space
1985 * following the tuple array. As we do this, we relocate dttk_value
1986 * in the final tuple to point to the key data address in the chunk.
1988 kdata
= (uintptr_t)&dvar
->dtdv_tuple
.dtt_key
[nkeys
];
1989 dvar
->dtdv_data
= (void *)(kdata
+ ksize
);
1990 dvar
->dtdv_tuple
.dtt_nkeys
= nkeys
;
1992 for (i
= 0; i
< nkeys
; i
++) {
1993 dtrace_key_t
*dkey
= &dvar
->dtdv_tuple
.dtt_key
[i
];
1994 size_t kesize
= key
[i
].dttk_size
;
1998 (const void *)(uintptr_t)key
[i
].dttk_value
,
1999 (void *)kdata
, kesize
);
2000 dkey
->dttk_value
= kdata
;
2001 kdata
+= P2ROUNDUP(kesize
, sizeof (uint64_t));
2003 dkey
->dttk_value
= key
[i
].dttk_value
;
2006 dkey
->dttk_size
= kesize
;
2009 ASSERT(dvar
->dtdv_hashval
== DTRACE_DYNHASH_FREE
);
2010 dvar
->dtdv_hashval
= hashval
;
2011 dvar
->dtdv_next
= start
;
2013 if (dtrace_casptr(&hash
[bucket
].dtdh_chain
, start
, dvar
) == start
)
2017 * The cas has failed. Either another CPU is adding an element to
2018 * this hash chain, or another CPU is deleting an element from this
2019 * hash chain. The simplest way to deal with both of these cases
2020 * (though not necessarily the most efficient) is to free our
2021 * allocated block and tail-call ourselves. Note that the free is
2022 * to the dirty list and _not_ to the free list. This is to prevent
2023 * races with allocators, above.
2025 dvar
->dtdv_hashval
= DTRACE_DYNHASH_FREE
;
2027 dtrace_membar_producer();
2030 free
= dcpu
->dtdsc_dirty
;
2031 dvar
->dtdv_next
= free
;
2032 } while (dtrace_casptr(&dcpu
->dtdsc_dirty
, free
, dvar
) != free
);
2034 return (dtrace_dynvar(dstate
, nkeys
, key
, dsize
, op
, mstate
, vstate
));
2039 dtrace_aggregate_min(uint64_t *oval
, uint64_t nval
, uint64_t arg
)
2041 #pragma unused(arg) /* __APPLE__ */
2042 if ((int64_t)nval
< (int64_t)*oval
)
2048 dtrace_aggregate_max(uint64_t *oval
, uint64_t nval
, uint64_t arg
)
2050 #pragma unused(arg) /* __APPLE__ */
2051 if ((int64_t)nval
> (int64_t)*oval
)
2056 dtrace_aggregate_quantize(uint64_t *quanta
, uint64_t nval
, uint64_t incr
)
2058 int i
, zero
= DTRACE_QUANTIZE_ZEROBUCKET
;
2059 int64_t val
= (int64_t)nval
;
2062 for (i
= 0; i
< zero
; i
++) {
2063 if (val
<= DTRACE_QUANTIZE_BUCKETVAL(i
)) {
2069 for (i
= zero
+ 1; i
< DTRACE_QUANTIZE_NBUCKETS
; i
++) {
2070 if (val
< DTRACE_QUANTIZE_BUCKETVAL(i
)) {
2071 quanta
[i
- 1] += incr
;
2076 quanta
[DTRACE_QUANTIZE_NBUCKETS
- 1] += incr
;
2084 dtrace_aggregate_lquantize(uint64_t *lquanta
, uint64_t nval
, uint64_t incr
)
2086 uint64_t arg
= *lquanta
++;
2087 int32_t base
= DTRACE_LQUANTIZE_BASE(arg
);
2088 uint16_t step
= DTRACE_LQUANTIZE_STEP(arg
);
2089 uint16_t levels
= DTRACE_LQUANTIZE_LEVELS(arg
);
2090 int32_t val
= (int32_t)nval
, level
;
2093 ASSERT(levels
!= 0);
2097 * This is an underflow.
2103 level
= (val
- base
) / step
;
2105 if (level
< levels
) {
2106 lquanta
[level
+ 1] += incr
;
2111 * This is an overflow.
2113 lquanta
[levels
+ 1] += incr
;
2118 dtrace_aggregate_avg(uint64_t *data
, uint64_t nval
, uint64_t arg
)
2120 #pragma unused(arg) /* __APPLE__ */
2127 dtrace_aggregate_stddev(uint64_t *data
, uint64_t nval
, uint64_t arg
)
2129 #pragma unused(arg) /* __APPLE__ */
2130 int64_t snval
= (int64_t)nval
;
2137 * What we want to say here is:
2139 * data[2] += nval * nval;
2141 * But given that nval is 64-bit, we could easily overflow, so
2142 * we do this as 128-bit arithmetic.
2147 dtrace_multiply_128((uint64_t)snval
, (uint64_t)snval
, tmp
);
2148 dtrace_add_128(data
+ 2, tmp
, data
+ 2);
2153 dtrace_aggregate_count(uint64_t *oval
, uint64_t nval
, uint64_t arg
)
2155 #pragma unused(nval, arg) /* __APPLE__ */
2161 dtrace_aggregate_sum(uint64_t *oval
, uint64_t nval
, uint64_t arg
)
2163 #pragma unused(arg) /* __APPLE__ */
2168 * Aggregate given the tuple in the principal data buffer, and the aggregating
2169 * action denoted by the specified dtrace_aggregation_t. The aggregation
2170 * buffer is specified as the buf parameter. This routine does not return
2171 * failure; if there is no space in the aggregation buffer, the data will be
2172 * dropped, and a corresponding counter incremented.
2175 dtrace_aggregate(dtrace_aggregation_t
*agg
, dtrace_buffer_t
*dbuf
,
2176 intptr_t offset
, dtrace_buffer_t
*buf
, uint64_t expr
, uint64_t arg
)
2179 dtrace_recdesc_t
*rec
= &agg
->dtag_action
.dta_rec
;
2180 uint32_t i
, ndx
, size
, fsize
;
2181 uint32_t align
= sizeof (uint64_t) - 1;
2182 dtrace_aggbuffer_t
*agb
;
2183 dtrace_aggkey_t
*key
;
2184 uint32_t hashval
= 0, limit
, isstr
;
2185 caddr_t tomax
, data
, kdata
;
2186 dtrace_actkind_t action
;
2187 dtrace_action_t
*act
;
2193 if (!agg
->dtag_hasarg
) {
2195 * Currently, only quantize() and lquantize() take additional
2196 * arguments, and they have the same semantics: an increment
2197 * value that defaults to 1 when not present. If additional
2198 * aggregating actions take arguments, the setting of the
2199 * default argument value will presumably have to become more
2205 action
= agg
->dtag_action
.dta_kind
- DTRACEACT_AGGREGATION
;
2206 size
= rec
->dtrd_offset
- agg
->dtag_base
;
2207 fsize
= size
+ rec
->dtrd_size
;
2209 ASSERT(dbuf
->dtb_tomax
!= NULL
);
2210 data
= dbuf
->dtb_tomax
+ offset
+ agg
->dtag_base
;
2212 if ((tomax
= buf
->dtb_tomax
) == NULL
) {
2213 dtrace_buffer_drop(buf
);
2218 * The metastructure is always at the bottom of the buffer.
2220 agb
= (dtrace_aggbuffer_t
*)(tomax
+ buf
->dtb_size
-
2221 sizeof (dtrace_aggbuffer_t
));
2223 if (buf
->dtb_offset
== 0) {
2225 * We just kludge up approximately 1/8th of the size to be
2226 * buckets. If this guess ends up being routinely
2227 * off-the-mark, we may need to dynamically readjust this
2228 * based on past performance.
2230 uintptr_t hashsize
= (buf
->dtb_size
>> 3) / sizeof (uintptr_t);
2232 if ((uintptr_t)agb
- hashsize
* sizeof (dtrace_aggkey_t
*) <
2233 (uintptr_t)tomax
|| hashsize
== 0) {
2235 * We've been given a ludicrously small buffer;
2236 * increment our drop count and leave.
2238 dtrace_buffer_drop(buf
);
2243 * And now, a pathetic attempt to try to get a an odd (or
2244 * perchance, a prime) hash size for better hash distribution.
2246 if (hashsize
> (DTRACE_AGGHASHSIZE_SLEW
<< 3))
2247 hashsize
-= DTRACE_AGGHASHSIZE_SLEW
;
2249 agb
->dtagb_hashsize
= hashsize
;
2250 agb
->dtagb_hash
= (dtrace_aggkey_t
**)((uintptr_t)agb
-
2251 agb
->dtagb_hashsize
* sizeof (dtrace_aggkey_t
*));
2252 agb
->dtagb_free
= (uintptr_t)agb
->dtagb_hash
;
2254 for (i
= 0; i
< agb
->dtagb_hashsize
; i
++)
2255 agb
->dtagb_hash
[i
] = NULL
;
2258 ASSERT(agg
->dtag_first
!= NULL
);
2259 ASSERT(agg
->dtag_first
->dta_intuple
);
2262 * Calculate the hash value based on the key. Note that we _don't_
2263 * include the aggid in the hashing (but we will store it as part of
2264 * the key). The hashing algorithm is Bob Jenkins' "One-at-a-time"
2265 * algorithm: a simple, quick algorithm that has no known funnels, and
2266 * gets good distribution in practice. The efficacy of the hashing
2267 * algorithm (and a comparison with other algorithms) may be found by
2268 * running the ::dtrace_aggstat MDB dcmd.
2270 for (act
= agg
->dtag_first
; act
->dta_intuple
; act
= act
->dta_next
) {
2271 i
= act
->dta_rec
.dtrd_offset
- agg
->dtag_base
;
2272 limit
= i
+ act
->dta_rec
.dtrd_size
;
2273 ASSERT(limit
<= size
);
2274 isstr
= DTRACEACT_ISSTRING(act
);
2276 for (; i
< limit
; i
++) {
2278 hashval
+= (hashval
<< 10);
2279 hashval
^= (hashval
>> 6);
2281 if (isstr
&& data
[i
] == '\0')
2286 hashval
+= (hashval
<< 3);
2287 hashval
^= (hashval
>> 11);
2288 hashval
+= (hashval
<< 15);
2291 * Yes, the divide here is expensive -- but it's generally the least
2292 * of the performance issues given the amount of data that we iterate
2293 * over to compute hash values, compare data, etc.
2295 ndx
= hashval
% agb
->dtagb_hashsize
;
2297 for (key
= agb
->dtagb_hash
[ndx
]; key
!= NULL
; key
= key
->dtak_next
) {
2298 ASSERT((caddr_t
)key
>= tomax
);
2299 ASSERT((caddr_t
)key
< tomax
+ buf
->dtb_size
);
2301 if (hashval
!= key
->dtak_hashval
|| key
->dtak_size
!= size
)
2304 kdata
= key
->dtak_data
;
2305 ASSERT(kdata
>= tomax
&& kdata
< tomax
+ buf
->dtb_size
);
2307 for (act
= agg
->dtag_first
; act
->dta_intuple
;
2308 act
= act
->dta_next
) {
2309 i
= act
->dta_rec
.dtrd_offset
- agg
->dtag_base
;
2310 limit
= i
+ act
->dta_rec
.dtrd_size
;
2311 ASSERT(limit
<= size
);
2312 isstr
= DTRACEACT_ISSTRING(act
);
2314 for (; i
< limit
; i
++) {
2315 if (kdata
[i
] != data
[i
])
2318 if (isstr
&& data
[i
] == '\0')
2323 if (action
!= key
->dtak_action
) {
2325 * We are aggregating on the same value in the same
2326 * aggregation with two different aggregating actions.
2327 * (This should have been picked up in the compiler,
2328 * so we may be dealing with errant or devious DIF.)
2329 * This is an error condition; we indicate as much,
2332 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
2337 * This is a hit: we need to apply the aggregator to
2338 * the value at this key.
2340 agg
->dtag_aggregate((uint64_t *)(kdata
+ size
), expr
, arg
);
2347 * We didn't find it. We need to allocate some zero-filled space,
2348 * link it into the hash table appropriately, and apply the aggregator
2349 * to the (zero-filled) value.
2351 offs
= buf
->dtb_offset
;
2352 while (offs
& (align
- 1))
2353 offs
+= sizeof (uint32_t);
2356 * If we don't have enough room to both allocate a new key _and_
2357 * its associated data, increment the drop count and return.
2359 if ((uintptr_t)tomax
+ offs
+ fsize
>
2360 agb
->dtagb_free
- sizeof (dtrace_aggkey_t
)) {
2361 dtrace_buffer_drop(buf
);
2366 ASSERT(!(sizeof (dtrace_aggkey_t
) & (sizeof (uintptr_t) - 1)));
2367 key
= (dtrace_aggkey_t
*)(agb
->dtagb_free
- sizeof (dtrace_aggkey_t
));
2368 agb
->dtagb_free
-= sizeof (dtrace_aggkey_t
);
2370 key
->dtak_data
= kdata
= tomax
+ offs
;
2371 buf
->dtb_offset
= offs
+ fsize
;
2374 * Now copy the data across.
2376 *((dtrace_aggid_t
*)kdata
) = agg
->dtag_id
;
2378 for (i
= sizeof (dtrace_aggid_t
); i
< size
; i
++)
2382 * Because strings are not zeroed out by default, we need to iterate
2383 * looking for actions that store strings, and we need to explicitly
2384 * pad these strings out with zeroes.
2386 for (act
= agg
->dtag_first
; act
->dta_intuple
; act
= act
->dta_next
) {
2389 if (!DTRACEACT_ISSTRING(act
))
2392 i
= act
->dta_rec
.dtrd_offset
- agg
->dtag_base
;
2393 limit
= i
+ act
->dta_rec
.dtrd_size
;
2394 ASSERT(limit
<= size
);
2396 for (nul
= 0; i
< limit
; i
++) {
2402 if (data
[i
] != '\0')
2409 for (i
= size
; i
< fsize
; i
++)
2412 key
->dtak_hashval
= hashval
;
2413 key
->dtak_size
= size
;
2414 key
->dtak_action
= action
;
2415 key
->dtak_next
= agb
->dtagb_hash
[ndx
];
2416 agb
->dtagb_hash
[ndx
] = key
;
2419 * Finally, apply the aggregator.
2421 *((uint64_t *)(key
->dtak_data
+ size
)) = agg
->dtag_initial
;
2422 agg
->dtag_aggregate((uint64_t *)(key
->dtak_data
+ size
), expr
, arg
);
2426 * Given consumer state, this routine finds a speculation in the INACTIVE
2427 * state and transitions it into the ACTIVE state. If there is no speculation
2428 * in the INACTIVE state, 0 is returned. In this case, no error counter is
2429 * incremented -- it is up to the caller to take appropriate action.
2432 dtrace_speculation(dtrace_state_t
*state
)
2435 dtrace_speculation_state_t current
;
2436 uint32_t *stat
= &state
->dts_speculations_unavail
, count
;
2438 while (i
< state
->dts_nspeculations
) {
2439 dtrace_speculation_t
*spec
= &state
->dts_speculations
[i
];
2441 current
= spec
->dtsp_state
;
2443 if (current
!= DTRACESPEC_INACTIVE
) {
2444 if (current
== DTRACESPEC_COMMITTINGMANY
||
2445 current
== DTRACESPEC_COMMITTING
||
2446 current
== DTRACESPEC_DISCARDING
)
2447 stat
= &state
->dts_speculations_busy
;
2452 if (dtrace_cas32((uint32_t *)&spec
->dtsp_state
,
2453 current
, DTRACESPEC_ACTIVE
) == current
)
2458 * We couldn't find a speculation. If we found as much as a single
2459 * busy speculation buffer, we'll attribute this failure as "busy"
2460 * instead of "unavail".
2464 } while (dtrace_cas32(stat
, count
, count
+ 1) != count
);
2470 * This routine commits an active speculation. If the specified speculation
2471 * is not in a valid state to perform a commit(), this routine will silently do
2472 * nothing. The state of the specified speculation is transitioned according
2473 * to the state transition diagram outlined in <sys/dtrace_impl.h>
2476 dtrace_speculation_commit(dtrace_state_t
*state
, processorid_t cpu
,
2477 dtrace_specid_t which
)
2479 dtrace_speculation_t
*spec
;
2480 dtrace_buffer_t
*src
, *dest
;
2481 uintptr_t daddr
, saddr
, dlimit
;
2482 #if !defined(__APPLE__) /* Quiet compiler warning */
2483 dtrace_speculation_state_t current
, new;
2485 dtrace_speculation_state_t current
, new = DTRACESPEC_INACTIVE
;
2486 #endif /* __APPLE__ */
2492 #if !defined(__APPLE__) /* Quiet compiler warning */
2493 if (which
> state
->dts_nspeculations
) {
2494 cpu_core
[cpu
].cpuc_dtrace_flags
|= CPU_DTRACE_ILLOP
;
2498 if (which
> (dtrace_specid_t
)state
->dts_nspeculations
) {
2499 cpu_core
[cpu
].cpuc_dtrace_flags
|= CPU_DTRACE_ILLOP
;
2502 #endif /* __APPLE__ */
2504 spec
= &state
->dts_speculations
[which
- 1];
2505 src
= &spec
->dtsp_buffer
[cpu
];
2506 dest
= &state
->dts_buffer
[cpu
];
2509 current
= spec
->dtsp_state
;
2511 if (current
== DTRACESPEC_COMMITTINGMANY
)
2515 case DTRACESPEC_INACTIVE
:
2516 case DTRACESPEC_DISCARDING
:
2519 case DTRACESPEC_COMMITTING
:
2521 * This is only possible if we are (a) commit()'ing
2522 * without having done a prior speculate() on this CPU
2523 * and (b) racing with another commit() on a different
2524 * CPU. There's nothing to do -- we just assert that
2527 ASSERT(src
->dtb_offset
== 0);
2530 case DTRACESPEC_ACTIVE
:
2531 new = DTRACESPEC_COMMITTING
;
2534 case DTRACESPEC_ACTIVEONE
:
2536 * This speculation is active on one CPU. If our
2537 * buffer offset is non-zero, we know that the one CPU
2538 * must be us. Otherwise, we are committing on a
2539 * different CPU from the speculate(), and we must
2540 * rely on being asynchronously cleaned.
2542 if (src
->dtb_offset
!= 0) {
2543 new = DTRACESPEC_COMMITTING
;
2548 case DTRACESPEC_ACTIVEMANY
:
2549 new = DTRACESPEC_COMMITTINGMANY
;
2555 } while (dtrace_cas32((uint32_t *)&spec
->dtsp_state
,
2556 current
, new) != current
);
2559 * We have set the state to indicate that we are committing this
2560 * speculation. Now reserve the necessary space in the destination
2563 if ((offs
= dtrace_buffer_reserve(dest
, src
->dtb_offset
,
2564 sizeof (uint64_t), state
, NULL
)) < 0) {
2565 dtrace_buffer_drop(dest
);
2570 * We have the space; copy the buffer across. (Note that this is a
2571 * highly subobtimal bcopy(); in the unlikely event that this becomes
2572 * a serious performance issue, a high-performance DTrace-specific
2573 * bcopy() should obviously be invented.)
2575 daddr
= (uintptr_t)dest
->dtb_tomax
+ offs
;
2576 dlimit
= daddr
+ src
->dtb_offset
;
2577 saddr
= (uintptr_t)src
->dtb_tomax
;
2580 * First, the aligned portion.
2582 while (dlimit
- daddr
>= sizeof (uint64_t)) {
2583 *((uint64_t *)daddr
) = *((uint64_t *)saddr
);
2585 daddr
+= sizeof (uint64_t);
2586 saddr
+= sizeof (uint64_t);
2590 * Now any left-over bit...
2592 while (dlimit
- daddr
)
2593 *((uint8_t *)daddr
++) = *((uint8_t *)saddr
++);
2596 * Finally, commit the reserved space in the destination buffer.
2598 dest
->dtb_offset
= offs
+ src
->dtb_offset
;
2602 * If we're lucky enough to be the only active CPU on this speculation
2603 * buffer, we can just set the state back to DTRACESPEC_INACTIVE.
2605 if (current
== DTRACESPEC_ACTIVE
||
2606 (current
== DTRACESPEC_ACTIVEONE
&& new == DTRACESPEC_COMMITTING
)) {
2607 uint32_t rval
= dtrace_cas32((uint32_t *)&spec
->dtsp_state
,
2608 DTRACESPEC_COMMITTING
, DTRACESPEC_INACTIVE
);
2609 #pragma unused(rval) /* __APPLE__ */
2611 ASSERT(rval
== DTRACESPEC_COMMITTING
);
2614 src
->dtb_offset
= 0;
2615 src
->dtb_xamot_drops
+= src
->dtb_drops
;
2620 * This routine discards an active speculation. If the specified speculation
2621 * is not in a valid state to perform a discard(), this routine will silently
2622 * do nothing. The state of the specified speculation is transitioned
2623 * according to the state transition diagram outlined in <sys/dtrace_impl.h>
2626 dtrace_speculation_discard(dtrace_state_t
*state
, processorid_t cpu
,
2627 dtrace_specid_t which
)
2629 dtrace_speculation_t
*spec
;
2630 #if !defined(__APPLE__) /* Quiet compiler warning */
2631 dtrace_speculation_state_t current
, new;
2633 dtrace_speculation_state_t current
, new = DTRACESPEC_INACTIVE
;
2634 #endif /* __APPLE__ */
2635 dtrace_buffer_t
*buf
;
2640 #if !defined(__APPLE__) /* Quiet compiler warning */
2641 if (which
> state
->dts_nspeculations
) {
2642 cpu_core
[cpu
].cpuc_dtrace_flags
|= CPU_DTRACE_ILLOP
;
2646 if (which
> (dtrace_specid_t
)state
->dts_nspeculations
) {
2647 cpu_core
[cpu
].cpuc_dtrace_flags
|= CPU_DTRACE_ILLOP
;
2650 #endif /* __APPLE__ */
2652 spec
= &state
->dts_speculations
[which
- 1];
2653 buf
= &spec
->dtsp_buffer
[cpu
];
2656 current
= spec
->dtsp_state
;
2659 case DTRACESPEC_INACTIVE
:
2660 case DTRACESPEC_COMMITTINGMANY
:
2661 case DTRACESPEC_COMMITTING
:
2662 case DTRACESPEC_DISCARDING
:
2665 case DTRACESPEC_ACTIVE
:
2666 case DTRACESPEC_ACTIVEMANY
:
2667 new = DTRACESPEC_DISCARDING
;
2670 case DTRACESPEC_ACTIVEONE
:
2671 if (buf
->dtb_offset
!= 0) {
2672 new = DTRACESPEC_INACTIVE
;
2674 new = DTRACESPEC_DISCARDING
;
2681 } while (dtrace_cas32((uint32_t *)&spec
->dtsp_state
,
2682 current
, new) != current
);
2684 buf
->dtb_offset
= 0;
2689 * Note: not called from probe context. This function is called
2690 * asynchronously from cross call context to clean any speculations that are
2691 * in the COMMITTINGMANY or DISCARDING states. These speculations may not be
2692 * transitioned back to the INACTIVE state until all CPUs have cleaned the
2696 dtrace_speculation_clean_here(dtrace_state_t
*state
)
2698 dtrace_icookie_t cookie
;
2699 processorid_t cpu
= CPU
->cpu_id
;
2700 dtrace_buffer_t
*dest
= &state
->dts_buffer
[cpu
];
2703 cookie
= dtrace_interrupt_disable();
2705 if (dest
->dtb_tomax
== NULL
) {
2706 dtrace_interrupt_enable(cookie
);
2710 #if !defined(__APPLE__) /* Quiet compiler warning */
2711 for (i
= 0; i
< state
->dts_nspeculations
; i
++) {
2713 for (i
= 0; i
< (dtrace_specid_t
)state
->dts_nspeculations
; i
++) {
2714 #endif /* __APPLE__ */
2715 dtrace_speculation_t
*spec
= &state
->dts_speculations
[i
];
2716 dtrace_buffer_t
*src
= &spec
->dtsp_buffer
[cpu
];
2718 if (src
->dtb_tomax
== NULL
)
2721 if (spec
->dtsp_state
== DTRACESPEC_DISCARDING
) {
2722 src
->dtb_offset
= 0;
2726 if (spec
->dtsp_state
!= DTRACESPEC_COMMITTINGMANY
)
2729 if (src
->dtb_offset
== 0)
2732 dtrace_speculation_commit(state
, cpu
, i
+ 1);
2735 dtrace_interrupt_enable(cookie
);
2739 * Note: not called from probe context. This function is called
2740 * asynchronously (and at a regular interval) to clean any speculations that
2741 * are in the COMMITTINGMANY or DISCARDING states. If it discovers that there
2742 * is work to be done, it cross calls all CPUs to perform that work;
2743 * COMMITMANY and DISCARDING speculations may not be transitioned back to the
2744 * INACTIVE state until they have been cleaned by all CPUs.
2747 dtrace_speculation_clean(dtrace_state_t
*state
)
2749 #if !defined(__APPLE__) /* Quiet compiler warning */
2754 #endif /* __APPLE__ */
2757 #if !defined(__APPLE__) /* Quiet compiler warning */
2758 for (i
= 0; i
< state
->dts_nspeculations
; i
++) {
2760 for (i
= 0; i
< (dtrace_specid_t
)state
->dts_nspeculations
; i
++) {
2761 #endif /* __APPLE__ */
2762 dtrace_speculation_t
*spec
= &state
->dts_speculations
[i
];
2764 ASSERT(!spec
->dtsp_cleaning
);
2766 if (spec
->dtsp_state
!= DTRACESPEC_DISCARDING
&&
2767 spec
->dtsp_state
!= DTRACESPEC_COMMITTINGMANY
)
2771 spec
->dtsp_cleaning
= 1;
2777 dtrace_xcall(DTRACE_CPUALL
,
2778 (dtrace_xcall_t
)dtrace_speculation_clean_here
, state
);
2781 * We now know that all CPUs have committed or discarded their
2782 * speculation buffers, as appropriate. We can now set the state
2785 #if !defined(__APPLE__) /* Quiet compiler warning */
2786 for (i
= 0; i
< state
->dts_nspeculations
; i
++) {
2788 for (i
= 0; i
< (dtrace_specid_t
)state
->dts_nspeculations
; i
++) {
2789 #endif /* __APPLE__ */
2790 dtrace_speculation_t
*spec
= &state
->dts_speculations
[i
];
2791 dtrace_speculation_state_t current
, new;
2793 if (!spec
->dtsp_cleaning
)
2796 current
= spec
->dtsp_state
;
2797 ASSERT(current
== DTRACESPEC_DISCARDING
||
2798 current
== DTRACESPEC_COMMITTINGMANY
);
2800 new = DTRACESPEC_INACTIVE
;
2802 rv
= dtrace_cas32((uint32_t *)&spec
->dtsp_state
, current
, new);
2803 ASSERT(rv
== current
);
2804 spec
->dtsp_cleaning
= 0;
2809 * Called as part of a speculate() to get the speculative buffer associated
2810 * with a given speculation. Returns NULL if the specified speculation is not
2811 * in an ACTIVE state. If the speculation is in the ACTIVEONE state -- and
2812 * the active CPU is not the specified CPU -- the speculation will be
2813 * atomically transitioned into the ACTIVEMANY state.
2815 static dtrace_buffer_t
*
2816 dtrace_speculation_buffer(dtrace_state_t
*state
, processorid_t cpuid
,
2817 dtrace_specid_t which
)
2819 dtrace_speculation_t
*spec
;
2820 #if !defined(__APPLE__) /* Quiet compiler warning */
2821 dtrace_speculation_state_t current
, new;
2823 dtrace_speculation_state_t current
, new = DTRACESPEC_INACTIVE
;
2824 #endif /* __APPLE__ */
2825 dtrace_buffer_t
*buf
;
2830 #if !defined(__APPLE__) /* Quiet compiler warning */
2831 if (which
> state
->dts_nspeculations
) {
2833 if (which
> (dtrace_specid_t
)state
->dts_nspeculations
) {
2834 #endif /* __APPLE__ */
2835 cpu_core
[cpuid
].cpuc_dtrace_flags
|= CPU_DTRACE_ILLOP
;
2839 spec
= &state
->dts_speculations
[which
- 1];
2840 buf
= &spec
->dtsp_buffer
[cpuid
];
2843 current
= spec
->dtsp_state
;
2846 case DTRACESPEC_INACTIVE
:
2847 case DTRACESPEC_COMMITTINGMANY
:
2848 case DTRACESPEC_DISCARDING
:
2851 case DTRACESPEC_COMMITTING
:
2852 ASSERT(buf
->dtb_offset
== 0);
2855 case DTRACESPEC_ACTIVEONE
:
2857 * This speculation is currently active on one CPU.
2858 * Check the offset in the buffer; if it's non-zero,
2859 * that CPU must be us (and we leave the state alone).
2860 * If it's zero, assume that we're starting on a new
2861 * CPU -- and change the state to indicate that the
2862 * speculation is active on more than one CPU.
2864 if (buf
->dtb_offset
!= 0)
2867 new = DTRACESPEC_ACTIVEMANY
;
2870 case DTRACESPEC_ACTIVEMANY
:
2873 case DTRACESPEC_ACTIVE
:
2874 new = DTRACESPEC_ACTIVEONE
;
2880 } while (dtrace_cas32((uint32_t *)&spec
->dtsp_state
,
2881 current
, new) != current
);
2883 ASSERT(new == DTRACESPEC_ACTIVEONE
|| new == DTRACESPEC_ACTIVEMANY
);
2888 * Return a string. In the event that the user lacks the privilege to access
2889 * arbitrary kernel memory, we copy the string out to scratch memory so that we
2890 * don't fail access checking.
2892 * dtrace_dif_variable() uses this routine as a helper for various
2893 * builtin values such as 'execname' and 'probefunc.'
2895 #if defined(__APPLE__) /* Quiet compiler warning. */
2897 #endif /* __APPLE__ */
2899 dtrace_dif_varstr(uintptr_t addr
, dtrace_state_t
*state
,
2900 dtrace_mstate_t
*mstate
)
2902 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
2907 * The easy case: this probe is allowed to read all of memory, so
2908 * we can just return this as a vanilla pointer.
2910 if ((mstate
->dtms_access
& DTRACE_ACCESS_KERNEL
) != 0)
2914 * This is the tougher case: we copy the string in question from
2915 * kernel memory into scratch memory and return it that way: this
2916 * ensures that we won't trip up when access checking tests the
2917 * BYREF return value.
2919 strsz
= dtrace_strlen((char *)addr
, size
) + 1;
2921 if (mstate
->dtms_scratch_ptr
+ strsz
>
2922 mstate
->dtms_scratch_base
+ mstate
->dtms_scratch_size
) {
2923 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
2927 dtrace_strcpy((const void *)addr
, (void *)mstate
->dtms_scratch_ptr
,
2929 ret
= mstate
->dtms_scratch_ptr
;
2930 mstate
->dtms_scratch_ptr
+= strsz
;
2935 * This function implements the DIF emulator's variable lookups. The emulator
2936 * passes a reserved variable identifier and optional built-in array index.
2939 dtrace_dif_variable(dtrace_mstate_t
*mstate
, dtrace_state_t
*state
, uint64_t v
,
2943 * If we're accessing one of the uncached arguments, we'll turn this
2944 * into a reference in the args array.
2946 if (v
>= DIF_VAR_ARG0
&& v
<= DIF_VAR_ARG9
) {
2947 ndx
= v
- DIF_VAR_ARG0
;
2953 ASSERT(mstate
->dtms_present
& DTRACE_MSTATE_ARGS
);
2954 if (ndx
>= sizeof (mstate
->dtms_arg
) /
2955 sizeof (mstate
->dtms_arg
[0])) {
2956 #if !defined(__APPLE__)
2957 int aframes
= mstate
->dtms_probe
->dtpr_aframes
+ 2;
2959 /* Account for introduction of __dtrace_probe() on xnu. */
2960 int aframes
= mstate
->dtms_probe
->dtpr_aframes
+ 3;
2961 #endif /* __APPLE__ */
2962 dtrace_provider_t
*pv
;
2965 pv
= mstate
->dtms_probe
->dtpr_provider
;
2966 if (pv
->dtpv_pops
.dtps_getargval
!= NULL
)
2967 val
= pv
->dtpv_pops
.dtps_getargval(pv
->dtpv_arg
,
2968 mstate
->dtms_probe
->dtpr_id
,
2969 mstate
->dtms_probe
->dtpr_arg
, ndx
, aframes
);
2970 #if defined(__APPLE__)
2971 /* Special case access of arg5 as passed to dtrace_probe_error() (which see.) */
2972 else if (mstate
->dtms_probe
->dtpr_id
== dtrace_probeid_error
&& ndx
== 5) {
2973 return ((dtrace_state_t
*)(uintptr_t)(mstate
->dtms_arg
[0]))->dts_arg_error_illval
;
2975 #endif /* __APPLE__ */
2977 val
= dtrace_getarg(ndx
, aframes
);
2980 * This is regrettably required to keep the compiler
2981 * from tail-optimizing the call to dtrace_getarg().
2982 * The condition always evaluates to true, but the
2983 * compiler has no way of figuring that out a priori.
2984 * (None of this would be necessary if the compiler
2985 * could be relied upon to _always_ tail-optimize
2986 * the call to dtrace_getarg() -- but it can't.)
2988 if (mstate
->dtms_probe
!= NULL
)
2994 return (mstate
->dtms_arg
[ndx
]);
2996 #if !defined(__APPLE__)
2997 case DIF_VAR_UREGS
: {
3000 if (!dtrace_priv_proc(state
))
3003 if ((lwp
= curthread
->t_lwp
) == NULL
) {
3004 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
3005 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= NULL
;
3009 return (dtrace_getreg(lwp
->lwp_regs
, ndx
));
3012 case DIF_VAR_UREGS
: {
3015 if (!dtrace_priv_proc(state
))
3018 if ((thread
= current_thread()) == NULL
) {
3019 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
3020 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= 0;
3024 return (dtrace_getreg(find_user_regs(thread
), ndx
));
3026 #endif /* __APPLE__ */
3028 #if !defined(__APPLE__)
3029 case DIF_VAR_CURTHREAD
:
3030 if (!dtrace_priv_kernel(state
))
3032 return ((uint64_t)(uintptr_t)curthread
);
3034 case DIF_VAR_CURTHREAD
:
3035 if (!dtrace_priv_kernel(state
))
3038 return ((uint64_t)(uintptr_t)current_thread());
3039 #endif /* __APPLE__ */
3041 case DIF_VAR_TIMESTAMP
:
3042 if (!(mstate
->dtms_present
& DTRACE_MSTATE_TIMESTAMP
)) {
3043 mstate
->dtms_timestamp
= dtrace_gethrtime();
3044 mstate
->dtms_present
|= DTRACE_MSTATE_TIMESTAMP
;
3046 return (mstate
->dtms_timestamp
);
3048 #if !defined(__APPLE__)
3049 case DIF_VAR_VTIMESTAMP
:
3050 ASSERT(dtrace_vtime_references
!= 0);
3051 return (curthread
->t_dtrace_vtime
);
3053 case DIF_VAR_VTIMESTAMP
:
3054 ASSERT(dtrace_vtime_references
!= 0);
3055 return (dtrace_get_thread_vtime(current_thread()));
3056 #endif /* __APPLE__ */
3058 case DIF_VAR_WALLTIMESTAMP
:
3059 if (!(mstate
->dtms_present
& DTRACE_MSTATE_WALLTIMESTAMP
)) {
3060 mstate
->dtms_walltimestamp
= dtrace_gethrestime();
3061 mstate
->dtms_present
|= DTRACE_MSTATE_WALLTIMESTAMP
;
3063 return (mstate
->dtms_walltimestamp
);
3066 if (!dtrace_priv_kernel(state
))
3068 if (!(mstate
->dtms_present
& DTRACE_MSTATE_IPL
)) {
3069 mstate
->dtms_ipl
= dtrace_getipl();
3070 mstate
->dtms_present
|= DTRACE_MSTATE_IPL
;
3072 return (mstate
->dtms_ipl
);
3075 ASSERT(mstate
->dtms_present
& DTRACE_MSTATE_EPID
);
3076 return (mstate
->dtms_epid
);
3079 ASSERT(mstate
->dtms_present
& DTRACE_MSTATE_PROBE
);
3080 return (mstate
->dtms_probe
->dtpr_id
);
3082 case DIF_VAR_STACKDEPTH
:
3083 if (!dtrace_priv_kernel(state
))
3085 if (!(mstate
->dtms_present
& DTRACE_MSTATE_STACKDEPTH
)) {
3086 #if !defined(__APPLE__)
3087 int aframes
= mstate
->dtms_probe
->dtpr_aframes
+ 2;
3089 /* Account for introduction of __dtrace_probe() on xnu. */
3090 int aframes
= mstate
->dtms_probe
->dtpr_aframes
+ 3;
3091 #endif /* __APPLE__ */
3093 mstate
->dtms_stackdepth
= dtrace_getstackdepth(aframes
);
3094 mstate
->dtms_present
|= DTRACE_MSTATE_STACKDEPTH
;
3096 return (mstate
->dtms_stackdepth
);
3098 case DIF_VAR_USTACKDEPTH
:
3099 if (!dtrace_priv_proc(state
))
3101 if (!(mstate
->dtms_present
& DTRACE_MSTATE_USTACKDEPTH
)) {
3103 * See comment in DIF_VAR_PID.
3105 if (DTRACE_ANCHORED(mstate
->dtms_probe
) &&
3107 mstate
->dtms_ustackdepth
= 0;
3109 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
3110 mstate
->dtms_ustackdepth
=
3111 dtrace_getustackdepth();
3112 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
3114 mstate
->dtms_present
|= DTRACE_MSTATE_USTACKDEPTH
;
3116 return (mstate
->dtms_ustackdepth
);
3118 case DIF_VAR_CALLER
:
3119 if (!dtrace_priv_kernel(state
))
3121 if (!(mstate
->dtms_present
& DTRACE_MSTATE_CALLER
)) {
3122 #if !defined(__APPLE__)
3123 int aframes
= mstate
->dtms_probe
->dtpr_aframes
+ 2;
3125 /* Account for introduction of __dtrace_probe() on xnu. */
3126 int aframes
= mstate
->dtms_probe
->dtpr_aframes
+ 3;
3127 #endif /* __APPLE__ */
3129 if (!DTRACE_ANCHORED(mstate
->dtms_probe
)) {
3131 * If this is an unanchored probe, we are
3132 * required to go through the slow path:
3133 * dtrace_caller() only guarantees correct
3134 * results for anchored probes.
3138 dtrace_getpcstack(caller
, 2, aframes
,
3139 (uint32_t *)(uintptr_t)mstate
->dtms_arg
[0]);
3140 mstate
->dtms_caller
= caller
[1];
3141 } else if ((mstate
->dtms_caller
=
3142 #if !defined(__APPLE__) /* Quiet compiler warnings */
3143 dtrace_caller(aframes
)) == -1) {
3145 dtrace_caller(aframes
)) == (uintptr_t)-1) {
3146 #endif /* __APPLE__ */
3148 * We have failed to do this the quick way;
3149 * we must resort to the slower approach of
3150 * calling dtrace_getpcstack().
3154 dtrace_getpcstack(&caller
, 1, aframes
, NULL
);
3155 mstate
->dtms_caller
= caller
;
3158 mstate
->dtms_present
|= DTRACE_MSTATE_CALLER
;
3160 return (mstate
->dtms_caller
);
3162 case DIF_VAR_UCALLER
:
3163 if (!dtrace_priv_proc(state
))
3166 if (!(mstate
->dtms_present
& DTRACE_MSTATE_UCALLER
)) {
3170 * dtrace_getupcstack() fills in the first uint64_t
3171 * with the current PID. The second uint64_t will
3172 * be the program counter at user-level. The third
3173 * uint64_t will contain the caller, which is what
3177 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
3178 dtrace_getupcstack(ustack
, 3);
3179 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
3180 mstate
->dtms_ucaller
= ustack
[2];
3181 mstate
->dtms_present
|= DTRACE_MSTATE_UCALLER
;
3184 return (mstate
->dtms_ucaller
);
3186 case DIF_VAR_PROBEPROV
:
3187 ASSERT(mstate
->dtms_present
& DTRACE_MSTATE_PROBE
);
3188 return (dtrace_dif_varstr(
3189 (uintptr_t)mstate
->dtms_probe
->dtpr_provider
->dtpv_name
,
3192 case DIF_VAR_PROBEMOD
:
3193 ASSERT(mstate
->dtms_present
& DTRACE_MSTATE_PROBE
);
3194 return (dtrace_dif_varstr(
3195 (uintptr_t)mstate
->dtms_probe
->dtpr_mod
,
3198 case DIF_VAR_PROBEFUNC
:
3199 ASSERT(mstate
->dtms_present
& DTRACE_MSTATE_PROBE
);
3200 return (dtrace_dif_varstr(
3201 (uintptr_t)mstate
->dtms_probe
->dtpr_func
,
3204 case DIF_VAR_PROBENAME
:
3205 ASSERT(mstate
->dtms_present
& DTRACE_MSTATE_PROBE
);
3206 return (dtrace_dif_varstr(
3207 (uintptr_t)mstate
->dtms_probe
->dtpr_name
,
3210 #if !defined(__APPLE__)
3212 if (!dtrace_priv_proc(state
))
3216 * Note that we are assuming that an unanchored probe is
3217 * always due to a high-level interrupt. (And we're assuming
3218 * that there is only a single high level interrupt.)
3220 if (DTRACE_ANCHORED(mstate
->dtms_probe
) && CPU_ON_INTR(CPU
))
3221 return (pid0
.pid_id
);
3224 * It is always safe to dereference one's own t_procp pointer:
3225 * it always points to a valid, allocated proc structure.
3226 * Further, it is always safe to dereference the p_pidp member
3227 * of one's own proc structure. (These are truisms becuase
3228 * threads and processes don't clean up their own state --
3229 * they leave that task to whomever reaps them.)
3231 return ((uint64_t)curthread
->t_procp
->p_pidp
->pid_id
);
3235 if (!dtrace_priv_proc_relaxed(state
))
3239 * Note that we are assuming that an unanchored probe is
3240 * always due to a high-level interrupt. (And we're assuming
3241 * that there is only a single high level interrupt.)
3243 if (DTRACE_ANCHORED(mstate
->dtms_probe
) && CPU_ON_INTR(CPU
))
3244 /* Anchored probe that fires while on an interrupt accrues to process 0 */
3247 return ((uint64_t)proc_selfpid());
3248 #endif /* __APPLE__ */
3250 #if !defined(__APPLE__)
3252 if (!dtrace_priv_proc(state
))
3256 * See comment in DIF_VAR_PID.
3258 if (DTRACE_ANCHORED(mstate
->dtms_probe
) && CPU_ON_INTR(CPU
))
3259 return (pid0
.pid_id
);
3262 * It is always safe to dereference one's own t_procp pointer:
3263 * it always points to a valid, allocated proc structure.
3264 * (This is true because threads don't clean up their own
3265 * state -- they leave that task to whomever reaps them.)
3267 return ((uint64_t)curthread
->t_procp
->p_ppid
);
3270 if (!dtrace_priv_proc_relaxed(state
))
3274 * See comment in DIF_VAR_PID.
3276 if (DTRACE_ANCHORED(mstate
->dtms_probe
) && CPU_ON_INTR(CPU
))
3279 return ((uint64_t)proc_selfppid());
3280 #endif /* __APPLE__ */
3282 #if !defined(__APPLE__)
3285 * See comment in DIF_VAR_PID.
3287 if (DTRACE_ANCHORED(mstate
->dtms_probe
) && CPU_ON_INTR(CPU
))
3290 return ((uint64_t)curthread
->t_tid
);
3293 /* We do not need to check for null current_thread() */
3294 return thread_tid(current_thread()); /* globally unique */
3296 case DIF_VAR_PTHREAD_SELF
:
3297 if (!dtrace_priv_proc(state
))
3300 /* Not currently supported, but we should be able to delta the dispatchqaddr and dispatchqoffset to get pthread_self */
3303 case DIF_VAR_DISPATCHQADDR
:
3304 if (!dtrace_priv_proc(state
))
3307 /* We do not need to check for null current_thread() */
3308 return thread_dispatchqaddr(current_thread());
3309 #endif /* __APPLE__ */
3311 #if !defined(__APPLE__)
3312 case DIF_VAR_EXECNAME
:
3313 if (!dtrace_priv_proc(state
))
3317 * See comment in DIF_VAR_PID.
3319 if (DTRACE_ANCHORED(mstate
->dtms_probe
) && CPU_ON_INTR(CPU
))
3320 return ((uint64_t)(uintptr_t)p0
.p_user
.u_comm
);
3323 * It is always safe to dereference one's own t_procp pointer:
3324 * it always points to a valid, allocated proc structure.
3325 * (This is true because threads don't clean up their own
3326 * state -- they leave that task to whomever reaps them.)
3328 return (dtrace_dif_varstr(
3329 (uintptr_t)curthread
->t_procp
->p_user
.u_comm
,
3332 case DIF_VAR_EXECNAME
:
3334 char *xname
= (char *)mstate
->dtms_scratch_ptr
;
3335 size_t scratch_size
= MAXCOMLEN
+1;
3337 /* The scratch allocation's lifetime is that of the clause. */
3338 if (!DTRACE_INSCRATCH(mstate
, scratch_size
)) {
3339 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
3343 if (!dtrace_priv_proc_relaxed(state
))
3346 mstate
->dtms_scratch_ptr
+= scratch_size
;
3347 proc_selfname( xname
, MAXCOMLEN
);
3349 return ((uint64_t)(uintptr_t)xname
);
3351 #endif /* __APPLE__ */
3352 #if !defined(__APPLE__)
3353 case DIF_VAR_ZONENAME
:
3354 if (!dtrace_priv_proc(state
))
3358 * See comment in DIF_VAR_PID.
3360 if (DTRACE_ANCHORED(mstate
->dtms_probe
) && CPU_ON_INTR(CPU
))
3361 return ((uint64_t)(uintptr_t)p0
.p_zone
->zone_name
);
3364 * It is always safe to dereference one's own t_procp pointer:
3365 * it always points to a valid, allocated proc structure.
3366 * (This is true because threads don't clean up their own
3367 * state -- they leave that task to whomever reaps them.)
3369 return (dtrace_dif_varstr(
3370 (uintptr_t)curthread
->t_procp
->p_zone
->zone_name
,
3374 case DIF_VAR_ZONENAME
:
3375 if (!dtrace_priv_proc(state
))
3378 /* FIXME: return e.g. "global" allocated from scratch a la execname. */
3379 return ((uint64_t)(uintptr_t)NULL
); /* Darwin doesn't do "zones" */
3380 #endif /* __APPLE__ */
3382 #if !defined(__APPLE__)
3384 if (!dtrace_priv_proc(state
))
3388 * See comment in DIF_VAR_PID.
3390 if (DTRACE_ANCHORED(mstate
->dtms_probe
) && CPU_ON_INTR(CPU
))
3391 return ((uint64_t)p0
.p_cred
->cr_uid
);
3394 * It is always safe to dereference one's own t_procp pointer:
3395 * it always points to a valid, allocated proc structure.
3396 * (This is true because threads don't clean up their own
3397 * state -- they leave that task to whomever reaps them.)
3399 * Additionally, it is safe to dereference one's own process
3400 * credential, since this is never NULL after process birth.
3402 return ((uint64_t)curthread
->t_procp
->p_cred
->cr_uid
);
3405 if (!dtrace_priv_proc(state
))
3409 * See comment in DIF_VAR_PID.
3411 if (DTRACE_ANCHORED(mstate
->dtms_probe
) && CPU_ON_INTR(CPU
))
3414 if (dtrace_CRED() != NULL
)
3415 /* Credential does not require lazy initialization. */
3416 return ((uint64_t)kauth_getuid());
3418 /* proc_lock would be taken under kauth_cred_proc_ref() in kauth_cred_get(). */
3419 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
3422 #endif /* __APPLE__ */
3424 #if !defined(__APPLE__)
3426 if (!dtrace_priv_proc(state
))
3430 * See comment in DIF_VAR_PID.
3432 if (DTRACE_ANCHORED(mstate
->dtms_probe
) && CPU_ON_INTR(CPU
))
3433 return ((uint64_t)p0
.p_cred
->cr_gid
);
3436 * It is always safe to dereference one's own t_procp pointer:
3437 * it always points to a valid, allocated proc structure.
3438 * (This is true because threads don't clean up their own
3439 * state -- they leave that task to whomever reaps them.)
3441 * Additionally, it is safe to dereference one's own process
3442 * credential, since this is never NULL after process birth.
3444 return ((uint64_t)curthread
->t_procp
->p_cred
->cr_gid
);
3447 if (!dtrace_priv_proc(state
))
3451 * See comment in DIF_VAR_PID.
3453 if (DTRACE_ANCHORED(mstate
->dtms_probe
) && CPU_ON_INTR(CPU
))
3456 if (dtrace_CRED() != NULL
)
3457 /* Credential does not require lazy initialization. */
3458 return ((uint64_t)kauth_getgid());
3460 /* proc_lock would be taken under kauth_cred_proc_ref() in kauth_cred_get(). */
3461 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
3464 #endif /* __APPLE__ */
3466 #if !defined(__APPLE__)
3467 case DIF_VAR_ERRNO
: {
3469 if (!dtrace_priv_proc(state
))
3473 * See comment in DIF_VAR_PID.
3475 if (DTRACE_ANCHORED(mstate
->dtms_probe
) && CPU_ON_INTR(CPU
))
3479 * It is always safe to dereference one's own t_lwp pointer in
3480 * the event that this pointer is non-NULL. (This is true
3481 * because threads and lwps don't clean up their own state --
3482 * they leave that task to whomever reaps them.)
3484 if ((lwp
= curthread
->t_lwp
) == NULL
)
3487 return ((uint64_t)lwp
->lwp_errno
);
3490 case DIF_VAR_ERRNO
: {
3491 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
3492 if (!dtrace_priv_proc(state
))
3496 * See comment in DIF_VAR_PID.
3498 if (DTRACE_ANCHORED(mstate
->dtms_probe
) && CPU_ON_INTR(CPU
))
3502 return (uint64_t)uthread
->t_dtrace_errno
;
3504 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
3508 #endif /* __APPLE__ */
3511 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
3517 * Emulate the execution of DTrace ID subroutines invoked by the call opcode.
3518 * Notice that we don't bother validating the proper number of arguments or
3519 * their types in the tuple stack. This isn't needed because all argument
3520 * interpretation is safe because of our load safety -- the worst that can
3521 * happen is that a bogus program can obtain bogus results.
3524 dtrace_dif_subr(uint_t subr
, uint_t rd
, uint64_t *regs
,
3525 dtrace_key_t
*tupregs
, int nargs
,
3526 dtrace_mstate_t
*mstate
, dtrace_state_t
*state
)
3528 volatile uint16_t *flags
= &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
3529 #if !defined(__APPLE__)
3530 volatile uintptr_t *illval
= &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
;
3532 volatile uint64_t *illval
= &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
;
3533 #endif /* __APPLE__ */
3534 dtrace_vstate_t
*vstate
= &state
->dts_vstate
;
3536 #if !defined(__APPLE__)
3547 /* FIXME: awaits lock/mutex work */
3548 #endif /* __APPLE__ */
3552 regs
[rd
] = (dtrace_gethrtime() * 2416 + 374441) % 1771875;
3555 #if !defined(__APPLE__)
3556 case DIF_SUBR_MUTEX_OWNED
:
3557 if (!dtrace_canload(tupregs
[0].dttk_value
, sizeof (kmutex_t
),
3563 m
.mx
= dtrace_load64(tupregs
[0].dttk_value
);
3564 if (MUTEX_TYPE_ADAPTIVE(&m
.mi
))
3565 regs
[rd
] = MUTEX_OWNER(&m
.mi
) != MUTEX_NO_OWNER
;
3567 regs
[rd
] = LOCK_HELD(&m
.mi
.m_spin
.m_spinlock
);
3570 case DIF_SUBR_MUTEX_OWNER
:
3571 if (!dtrace_canload(tupregs
[0].dttk_value
, sizeof (kmutex_t
),
3577 m
.mx
= dtrace_load64(tupregs
[0].dttk_value
);
3578 if (MUTEX_TYPE_ADAPTIVE(&m
.mi
) &&
3579 MUTEX_OWNER(&m
.mi
) != MUTEX_NO_OWNER
)
3580 regs
[rd
] = (uintptr_t)MUTEX_OWNER(&m
.mi
);
3585 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE
:
3586 if (!dtrace_canload(tupregs
[0].dttk_value
, sizeof (kmutex_t
),
3592 m
.mx
= dtrace_load64(tupregs
[0].dttk_value
);
3593 regs
[rd
] = MUTEX_TYPE_ADAPTIVE(&m
.mi
);
3596 case DIF_SUBR_MUTEX_TYPE_SPIN
:
3597 if (!dtrace_canload(tupregs
[0].dttk_value
, sizeof (kmutex_t
),
3603 m
.mx
= dtrace_load64(tupregs
[0].dttk_value
);
3604 regs
[rd
] = MUTEX_TYPE_SPIN(&m
.mi
);
3607 case DIF_SUBR_RW_READ_HELD
: {
3610 if (!dtrace_canload(tupregs
[0].dttk_value
, sizeof (uintptr_t),
3616 r
.rw
= dtrace_loadptr(tupregs
[0].dttk_value
);
3617 regs
[rd
] = _RW_READ_HELD(&r
.ri
, tmp
);
3621 case DIF_SUBR_RW_WRITE_HELD
:
3622 if (!dtrace_canload(tupregs
[0].dttk_value
, sizeof (krwlock_t
),
3628 r
.rw
= dtrace_loadptr(tupregs
[0].dttk_value
);
3629 regs
[rd
] = _RW_WRITE_HELD(&r
.ri
);
3632 case DIF_SUBR_RW_ISWRITER
:
3633 if (!dtrace_canload(tupregs
[0].dttk_value
, sizeof (krwlock_t
),
3639 r
.rw
= dtrace_loadptr(tupregs
[0].dttk_value
);
3640 regs
[rd
] = _RW_ISWRITER(&r
.ri
);
3643 /* FIXME: awaits lock/mutex work */
3644 #endif /* __APPLE__ */
3646 case DIF_SUBR_BCOPY
: {
3648 * We need to be sure that the destination is in the scratch
3649 * region -- no other region is allowed.
3651 uintptr_t src
= tupregs
[0].dttk_value
;
3652 uintptr_t dest
= tupregs
[1].dttk_value
;
3653 size_t size
= tupregs
[2].dttk_value
;
3655 if (!dtrace_inscratch(dest
, size
, mstate
)) {
3656 *flags
|= CPU_DTRACE_BADADDR
;
3661 if (!dtrace_canload(src
, size
, mstate
, vstate
)) {
3666 dtrace_bcopy((void *)src
, (void *)dest
, size
);
3670 case DIF_SUBR_ALLOCA
:
3671 case DIF_SUBR_COPYIN
: {
3672 uintptr_t dest
= P2ROUNDUP(mstate
->dtms_scratch_ptr
, 8);
3674 tupregs
[subr
== DIF_SUBR_ALLOCA
? 0 : 1].dttk_value
;
3675 size_t scratch_size
= (dest
- mstate
->dtms_scratch_ptr
) + size
;
3678 * This action doesn't require any credential checks since
3679 * probes will not activate in user contexts to which the
3680 * enabling user does not have permissions.
3684 * Rounding up the user allocation size could have overflowed
3685 * a large, bogus allocation (like -1ULL) to 0.
3687 if (scratch_size
< size
||
3688 !DTRACE_INSCRATCH(mstate
, scratch_size
)) {
3689 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
3694 if (subr
== DIF_SUBR_COPYIN
) {
3695 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
3696 #if !defined(__APPLE__)
3697 dtrace_copyin(tupregs
[0].dttk_value
, dest
, size
, flags
);
3699 if (dtrace_priv_proc(state
))
3700 dtrace_copyin(tupregs
[0].dttk_value
, dest
, size
, flags
);
3701 #endif /* __APPLE__ */
3702 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
3705 mstate
->dtms_scratch_ptr
+= scratch_size
;
3710 case DIF_SUBR_COPYINTO
: {
3711 uint64_t size
= tupregs
[1].dttk_value
;
3712 uintptr_t dest
= tupregs
[2].dttk_value
;
3715 * This action doesn't require any credential checks since
3716 * probes will not activate in user contexts to which the
3717 * enabling user does not have permissions.
3719 if (!dtrace_inscratch(dest
, size
, mstate
)) {
3720 *flags
|= CPU_DTRACE_BADADDR
;
3725 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
3726 #if !defined(__APPLE__)
3727 dtrace_copyin(tupregs
[0].dttk_value
, dest
, size
, flags
);
3729 if (dtrace_priv_proc(state
))
3730 dtrace_copyin(tupregs
[0].dttk_value
, dest
, size
, flags
);
3731 #endif /* __APPLE__ */
3732 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
3736 case DIF_SUBR_COPYINSTR
: {
3737 uintptr_t dest
= mstate
->dtms_scratch_ptr
;
3738 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
3740 if (nargs
> 1 && tupregs
[1].dttk_value
< size
)
3741 size
= tupregs
[1].dttk_value
+ 1;
3744 * This action doesn't require any credential checks since
3745 * probes will not activate in user contexts to which the
3746 * enabling user does not have permissions.
3748 if (!DTRACE_INSCRATCH(mstate
, size
)) {
3749 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
3754 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
3755 #if !defined(__APPLE__)
3756 dtrace_copyinstr(tupregs
[0].dttk_value
, dest
, size
, flags
);
3758 if (dtrace_priv_proc(state
))
3759 dtrace_copyinstr(tupregs
[0].dttk_value
, dest
, size
, flags
);
3760 #endif /* __APPLE__ */
3761 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
3763 ((char *)dest
)[size
- 1] = '\0';
3764 mstate
->dtms_scratch_ptr
+= size
;
3769 #if !defined(__APPLE__)
3770 case DIF_SUBR_MSGSIZE
:
3771 case DIF_SUBR_MSGDSIZE
: {
3772 uintptr_t baddr
= tupregs
[0].dttk_value
, daddr
;
3773 uintptr_t wptr
, rptr
;
3777 while (baddr
!= NULL
&& !(*flags
& CPU_DTRACE_FAULT
)) {
3779 if (!dtrace_canload(baddr
, sizeof (mblk_t
), mstate
,
3785 wptr
= dtrace_loadptr(baddr
+
3786 offsetof(mblk_t
, b_wptr
));
3788 rptr
= dtrace_loadptr(baddr
+
3789 offsetof(mblk_t
, b_rptr
));
3792 *flags
|= CPU_DTRACE_BADADDR
;
3793 *illval
= tupregs
[0].dttk_value
;
3797 daddr
= dtrace_loadptr(baddr
+
3798 offsetof(mblk_t
, b_datap
));
3800 baddr
= dtrace_loadptr(baddr
+
3801 offsetof(mblk_t
, b_cont
));
3804 * We want to prevent against denial-of-service here,
3805 * so we're only going to search the list for
3806 * dtrace_msgdsize_max mblks.
3808 if (cont
++ > dtrace_msgdsize_max
) {
3809 *flags
|= CPU_DTRACE_ILLOP
;
3813 if (subr
== DIF_SUBR_MSGDSIZE
) {
3814 if (dtrace_load8(daddr
+
3815 offsetof(dblk_t
, db_type
)) != M_DATA
)
3819 count
+= wptr
- rptr
;
3822 if (!(*flags
& CPU_DTRACE_FAULT
))
3828 case DIF_SUBR_MSGSIZE
:
3829 case DIF_SUBR_MSGDSIZE
: {
3830 /* Darwin does not implement SysV streams messages */
3831 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
3835 #endif /* __APPLE__ */
3837 #if !defined(__APPLE__)
3838 case DIF_SUBR_PROGENYOF
: {
3839 pid_t pid
= tupregs
[0].dttk_value
;
3843 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
3845 for (p
= curthread
->t_procp
; p
!= NULL
; p
= p
->p_parent
) {
3846 if (p
->p_pidp
->pid_id
== pid
) {
3852 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
3858 case DIF_SUBR_PROGENYOF
: {
3859 pid_t pid
= tupregs
[0].dttk_value
;
3860 struct proc
*p
= current_proc();
3861 int rval
= 0, lim
= nprocs
;
3863 while(p
&& (lim
-- > 0)) {
3866 ppid
= (pid_t
)dtrace_load32((uintptr_t)&(p
->p_pid
));
3867 if (*flags
& CPU_DTRACE_FAULT
)
3876 break; /* Can't climb process tree any further. */
3878 p
= (struct proc
*)dtrace_loadptr((uintptr_t)&(p
->p_pptr
));
3879 if (*flags
& CPU_DTRACE_FAULT
)
3886 #endif /* __APPLE__ */
3888 case DIF_SUBR_SPECULATION
:
3889 regs
[rd
] = dtrace_speculation(state
);
3892 #if !defined(__APPLE__)
3893 case DIF_SUBR_COPYOUT
: {
3894 uintptr_t kaddr
= tupregs
[0].dttk_value
;
3895 uintptr_t uaddr
= tupregs
[1].dttk_value
;
3896 uint64_t size
= tupregs
[2].dttk_value
;
3898 if (!dtrace_destructive_disallow
&&
3899 dtrace_priv_proc_control(state
) &&
3900 !dtrace_istoxic(kaddr
, size
)) {
3901 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
3902 dtrace_copyout(kaddr
, uaddr
, size
, flags
);
3903 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
3908 case DIF_SUBR_COPYOUTSTR
: {
3909 uintptr_t kaddr
= tupregs
[0].dttk_value
;
3910 uintptr_t uaddr
= tupregs
[1].dttk_value
;
3911 uint64_t size
= tupregs
[2].dttk_value
;
3913 if (!dtrace_destructive_disallow
&&
3914 dtrace_priv_proc_control(state
) &&
3915 !dtrace_istoxic(kaddr
, size
)) {
3916 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
3917 dtrace_copyoutstr(kaddr
, uaddr
, size
, flags
);
3918 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
3923 case DIF_SUBR_COPYOUT
: {
3924 uintptr_t kaddr
= tupregs
[0].dttk_value
;
3925 user_addr_t uaddr
= tupregs
[1].dttk_value
;
3926 uint64_t size
= tupregs
[2].dttk_value
;
3928 if (!dtrace_destructive_disallow
&&
3929 dtrace_priv_proc_control(state
) &&
3930 !dtrace_istoxic(kaddr
, size
)) {
3931 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
3932 dtrace_copyout(kaddr
, uaddr
, size
, flags
);
3933 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
3938 case DIF_SUBR_COPYOUTSTR
: {
3939 uintptr_t kaddr
= tupregs
[0].dttk_value
;
3940 user_addr_t uaddr
= tupregs
[1].dttk_value
;
3941 uint64_t size
= tupregs
[2].dttk_value
;
3943 if (!dtrace_destructive_disallow
&&
3944 dtrace_priv_proc_control(state
) &&
3945 !dtrace_istoxic(kaddr
, size
)) {
3946 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
3947 dtrace_copyoutstr(kaddr
, uaddr
, size
, flags
);
3948 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
3952 #endif /* __APPLE__ */
3954 case DIF_SUBR_STRLEN
: {
3956 uintptr_t addr
= (uintptr_t)tupregs
[0].dttk_value
;
3957 sz
= dtrace_strlen((char *)addr
,
3958 state
->dts_options
[DTRACEOPT_STRSIZE
]);
3960 if (!dtrace_canload(addr
, sz
+ 1, mstate
, vstate
)) {
3970 case DIF_SUBR_STRCHR
:
3971 case DIF_SUBR_STRRCHR
: {
3973 * We're going to iterate over the string looking for the
3974 * specified character. We will iterate until we have reached
3975 * the string length or we have found the character. If this
3976 * is DIF_SUBR_STRRCHR, we will look for the last occurrence
3977 * of the specified character instead of the first.
3979 uintptr_t saddr
= tupregs
[0].dttk_value
;
3980 uintptr_t addr
= tupregs
[0].dttk_value
;
3981 uintptr_t limit
= addr
+ state
->dts_options
[DTRACEOPT_STRSIZE
];
3982 char c
, target
= (char)tupregs
[1].dttk_value
;
3984 for (regs
[rd
] = NULL
; addr
< limit
; addr
++) {
3985 if ((c
= dtrace_load8(addr
)) == target
) {
3988 if (subr
== DIF_SUBR_STRCHR
)
3996 if (!dtrace_canload(saddr
, addr
- saddr
, mstate
, vstate
)) {
4004 case DIF_SUBR_STRSTR
:
4005 case DIF_SUBR_INDEX
:
4006 case DIF_SUBR_RINDEX
: {
4008 * We're going to iterate over the string looking for the
4009 * specified string. We will iterate until we have reached
4010 * the string length or we have found the string. (Yes, this
4011 * is done in the most naive way possible -- but considering
4012 * that the string we're searching for is likely to be
4013 * relatively short, the complexity of Rabin-Karp or similar
4014 * hardly seems merited.)
4016 char *addr
= (char *)(uintptr_t)tupregs
[0].dttk_value
;
4017 char *substr
= (char *)(uintptr_t)tupregs
[1].dttk_value
;
4018 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
4019 size_t len
= dtrace_strlen(addr
, size
);
4020 size_t sublen
= dtrace_strlen(substr
, size
);
4021 char *limit
= addr
+ len
, *orig
= addr
;
4022 int notfound
= subr
== DIF_SUBR_STRSTR
? 0 : -1;
4025 regs
[rd
] = notfound
;
4027 if (!dtrace_canload((uintptr_t)addr
, len
+ 1, mstate
, vstate
)) {
4032 if (!dtrace_canload((uintptr_t)substr
, sublen
+ 1, mstate
,
4039 * strstr() and index()/rindex() have similar semantics if
4040 * both strings are the empty string: strstr() returns a
4041 * pointer to the (empty) string, and index() and rindex()
4042 * both return index 0 (regardless of any position argument).
4044 if (sublen
== 0 && len
== 0) {
4045 if (subr
== DIF_SUBR_STRSTR
)
4046 regs
[rd
] = (uintptr_t)addr
;
4052 if (subr
!= DIF_SUBR_STRSTR
) {
4053 if (subr
== DIF_SUBR_RINDEX
) {
4060 * Both index() and rindex() take an optional position
4061 * argument that denotes the starting position.
4064 int64_t pos
= (int64_t)tupregs
[2].dttk_value
;
4067 * If the position argument to index() is
4068 * negative, Perl implicitly clamps it at
4069 * zero. This semantic is a little surprising
4070 * given the special meaning of negative
4071 * positions to similar Perl functions like
4072 * substr(), but it appears to reflect a
4073 * notion that index() can start from a
4074 * negative index and increment its way up to
4075 * the string. Given this notion, Perl's
4076 * rindex() is at least self-consistent in
4077 * that it implicitly clamps positions greater
4078 * than the string length to be the string
4079 * length. Where Perl completely loses
4080 * coherence, however, is when the specified
4081 * substring is the empty string (""). In
4082 * this case, even if the position is
4083 * negative, rindex() returns 0 -- and even if
4084 * the position is greater than the length,
4085 * index() returns the string length. These
4086 * semantics violate the notion that index()
4087 * should never return a value less than the
4088 * specified position and that rindex() should
4089 * never return a value greater than the
4090 * specified position. (One assumes that
4091 * these semantics are artifacts of Perl's
4092 * implementation and not the results of
4093 * deliberate design -- it beggars belief that
4094 * even Larry Wall could desire such oddness.)
4095 * While in the abstract one would wish for
4096 * consistent position semantics across
4097 * substr(), index() and rindex() -- or at the
4098 * very least self-consistent position
4099 * semantics for index() and rindex() -- we
4100 * instead opt to keep with the extant Perl
4101 * semantics, in all their broken glory. (Do
4102 * we have more desire to maintain Perl's
4103 * semantics than Perl does? Probably.)
4105 if (subr
== DIF_SUBR_RINDEX
) {
4112 #if !defined(__APPLE__) /* Quiet compiler warnings */
4115 if ((size_t)pos
> len
)
4116 #endif /* __APPLE__ */
4122 #if !defined(__APPLE__) /* Quiet compiler warnings */
4125 if ((size_t)pos
>= len
) {
4126 #endif /* __APPLE__ */
4137 for (regs
[rd
] = notfound
; addr
!= limit
; addr
+= inc
) {
4138 if (dtrace_strncmp(addr
, substr
, sublen
) == 0) {
4139 if (subr
!= DIF_SUBR_STRSTR
) {
4141 * As D index() and rindex() are
4142 * modeled on Perl (and not on awk),
4143 * we return a zero-based (and not a
4144 * one-based) index. (For you Perl
4145 * weenies: no, we're not going to add
4146 * $[ -- and shouldn't you be at a con
4149 regs
[rd
] = (uintptr_t)(addr
- orig
);
4153 ASSERT(subr
== DIF_SUBR_STRSTR
);
4154 regs
[rd
] = (uintptr_t)addr
;
4162 case DIF_SUBR_STRTOK
: {
4163 uintptr_t addr
= tupregs
[0].dttk_value
;
4164 uintptr_t tokaddr
= tupregs
[1].dttk_value
;
4165 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
4166 uintptr_t limit
, toklimit
= tokaddr
+ size
;
4167 char *dest
= (char *)mstate
->dtms_scratch_ptr
;
4168 #if !defined(__APPLE__) /* Quiet compiler warnings */
4169 uint8_t c
, tokmap
[32]; /* 256 / 8 */
4172 uint8_t c
='\0', tokmap
[32]; /* 256 / 8 */
4174 #endif /* __APPLE__ */
4177 * Check both the token buffer and (later) the input buffer,
4178 * since both could be non-scratch addresses.
4180 if (!dtrace_strcanload(tokaddr
, size
, mstate
, vstate
)) {
4185 if (!DTRACE_INSCRATCH(mstate
, size
)) {
4186 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
4193 * If the address specified is NULL, we use our saved
4194 * strtok pointer from the mstate. Note that this
4195 * means that the saved strtok pointer is _only_
4196 * valid within multiple enablings of the same probe --
4197 * it behaves like an implicit clause-local variable.
4199 addr
= mstate
->dtms_strtok
;
4202 * If the user-specified address is non-NULL we must
4203 * access check it. This is the only time we have
4204 * a chance to do so, since this address may reside
4205 * in the string table of this clause-- future calls
4206 * (when we fetch addr from mstate->dtms_strtok)
4207 * would fail this access check.
4209 if (!dtrace_strcanload(addr
, size
, mstate
, vstate
)) {
4216 * First, zero the token map, and then process the token
4217 * string -- setting a bit in the map for every character
4218 * found in the token string.
4220 for (i
= 0; i
< (int)sizeof (tokmap
); i
++)
4223 for (; tokaddr
< toklimit
; tokaddr
++) {
4224 if ((c
= dtrace_load8(tokaddr
)) == '\0')
4227 ASSERT((c
>> 3) < sizeof (tokmap
));
4228 tokmap
[c
>> 3] |= (1 << (c
& 0x7));
4231 for (limit
= addr
+ size
; addr
< limit
; addr
++) {
4233 * We're looking for a character that is _not_ contained
4234 * in the token string.
4236 if ((c
= dtrace_load8(addr
)) == '\0')
4239 if (!(tokmap
[c
>> 3] & (1 << (c
& 0x7))))
4245 * We reached the end of the string without finding
4246 * any character that was not in the token string.
4247 * We return NULL in this case, and we set the saved
4248 * address to NULL as well.
4251 mstate
->dtms_strtok
= NULL
;
4256 * From here on, we're copying into the destination string.
4258 for (i
= 0; addr
< limit
&& i
< size
- 1; addr
++) {
4259 if ((c
= dtrace_load8(addr
)) == '\0')
4262 if (tokmap
[c
>> 3] & (1 << (c
& 0x7)))
4271 regs
[rd
] = (uintptr_t)dest
;
4272 mstate
->dtms_scratch_ptr
+= size
;
4273 mstate
->dtms_strtok
= addr
;
4277 case DIF_SUBR_SUBSTR
: {
4278 uintptr_t s
= tupregs
[0].dttk_value
;
4279 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
4280 char *d
= (char *)mstate
->dtms_scratch_ptr
;
4281 int64_t index
= (int64_t)tupregs
[1].dttk_value
;
4282 int64_t remaining
= (int64_t)tupregs
[2].dttk_value
;
4283 size_t len
= dtrace_strlen((char *)s
, size
);
4286 if (!dtrace_canload(s
, len
+ 1, mstate
, vstate
)) {
4291 if (!DTRACE_INSCRATCH(mstate
, size
)) {
4292 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
4298 remaining
= (int64_t)size
;
4303 if (index
< 0 && index
+ remaining
> 0) {
4309 #if !defined(__APPLE__) /* Quiet compiler warnings */
4310 if (index
>= len
|| index
< 0) {
4312 } else if (remaining
< 0) {
4313 remaining
+= len
- index
;
4314 } else if (index
+ remaining
> size
) {
4315 remaining
= size
- index
;
4318 if ((size_t)index
>= len
|| index
< 0) {
4320 } else if (remaining
< 0) {
4321 remaining
+= len
- index
;
4322 } else if ((uint64_t)index
+ (uint64_t)remaining
> size
) {
4323 remaining
= size
- index
;
4325 #endif /* __APPLE__ */
4326 for (i
= 0; i
< remaining
; i
++) {
4327 if ((d
[i
] = dtrace_load8(s
+ index
+ i
)) == '\0')
4333 mstate
->dtms_scratch_ptr
+= size
;
4334 regs
[rd
] = (uintptr_t)d
;
4338 #if !defined(__APPLE__)
4339 case DIF_SUBR_GETMAJOR
:
4341 regs
[rd
] = (tupregs
[0].dttk_value
>> NBITSMINOR64
) & MAXMAJ64
;
4343 regs
[rd
] = (tupregs
[0].dttk_value
>> NBITSMINOR
) & MAXMAJ
;
4347 #else /* __APPLE__ */
4348 case DIF_SUBR_GETMAJOR
:
4349 regs
[rd
] = (uintptr_t)major( (dev_t
)tupregs
[0].dttk_value
);
4351 #endif /* __APPLE__ */
4353 #if !defined(__APPLE__)
4354 case DIF_SUBR_GETMINOR
:
4356 regs
[rd
] = tupregs
[0].dttk_value
& MAXMIN64
;
4358 regs
[rd
] = tupregs
[0].dttk_value
& MAXMIN
;
4362 #else /* __APPLE__ */
4363 case DIF_SUBR_GETMINOR
:
4364 regs
[rd
] = (uintptr_t)minor( (dev_t
)tupregs
[0].dttk_value
);
4366 #endif /* __APPLE__ */
4368 #if !defined(__APPLE__)
4369 case DIF_SUBR_DDI_PATHNAME
: {
4371 * This one is a galactic mess. We are going to roughly
4372 * emulate ddi_pathname(), but it's made more complicated
4373 * by the fact that we (a) want to include the minor name and
4374 * (b) must proceed iteratively instead of recursively.
4376 uintptr_t dest
= mstate
->dtms_scratch_ptr
;
4377 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
4378 char *start
= (char *)dest
, *end
= start
+ size
- 1;
4379 uintptr_t daddr
= tupregs
[0].dttk_value
;
4380 int64_t minor
= (int64_t)tupregs
[1].dttk_value
;
4382 int i
, len
, depth
= 0;
4385 * Due to all the pointer jumping we do and context we must
4386 * rely upon, we just mandate that the user must have kernel
4387 * read privileges to use this routine.
4389 if ((mstate
->dtms_access
& DTRACE_ACCESS_KERNEL
) == 0) {
4390 *flags
|= CPU_DTRACE_KPRIV
;
4395 if (!DTRACE_INSCRATCH(mstate
, size
)) {
4396 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
4404 * We want to have a name for the minor. In order to do this,
4405 * we need to walk the minor list from the devinfo. We want
4406 * to be sure that we don't infinitely walk a circular list,
4407 * so we check for circularity by sending a scout pointer
4408 * ahead two elements for every element that we iterate over;
4409 * if the list is circular, these will ultimately point to the
4410 * same element. You may recognize this little trick as the
4411 * answer to a stupid interview question -- one that always
4412 * seems to be asked by those who had to have it laboriously
4413 * explained to them, and who can't even concisely describe
4414 * the conditions under which one would be forced to resort to
4415 * this technique. Needless to say, those conditions are
4416 * found here -- and probably only here. Is this the only use
4417 * of this infamous trick in shipping, production code? If it
4418 * isn't, it probably should be...
4421 uintptr_t maddr
= dtrace_loadptr(daddr
+
4422 offsetof(struct dev_info
, devi_minor
));
4424 uintptr_t next
= offsetof(struct ddi_minor_data
, next
);
4425 uintptr_t name
= offsetof(struct ddi_minor_data
,
4426 d_minor
) + offsetof(struct ddi_minor
, name
);
4427 uintptr_t dev
= offsetof(struct ddi_minor_data
,
4428 d_minor
) + offsetof(struct ddi_minor
, dev
);
4432 scout
= dtrace_loadptr(maddr
+ next
);
4434 while (maddr
!= NULL
&& !(*flags
& CPU_DTRACE_FAULT
)) {
4437 m
= dtrace_load64(maddr
+ dev
) & MAXMIN64
;
4439 m
= dtrace_load32(maddr
+ dev
) & MAXMIN
;
4442 maddr
= dtrace_loadptr(maddr
+ next
);
4447 scout
= dtrace_loadptr(scout
+ next
);
4452 scout
= dtrace_loadptr(scout
+ next
);
4457 if (scout
== maddr
) {
4458 *flags
|= CPU_DTRACE_ILLOP
;
4466 * We have the minor data. Now we need to
4467 * copy the minor's name into the end of the
4470 s
= (char *)dtrace_loadptr(maddr
+ name
);
4471 len
= dtrace_strlen(s
, size
);
4473 if (*flags
& CPU_DTRACE_FAULT
)
4477 if ((end
-= (len
+ 1)) < start
)
4483 for (i
= 1; i
<= len
; i
++)
4484 end
[i
] = dtrace_load8((uintptr_t)s
++);
4489 while (daddr
!= NULL
&& !(*flags
& CPU_DTRACE_FAULT
)) {
4490 ddi_node_state_t devi_state
;
4492 devi_state
= dtrace_load32(daddr
+
4493 offsetof(struct dev_info
, devi_node_state
));
4495 if (*flags
& CPU_DTRACE_FAULT
)
4498 if (devi_state
>= DS_INITIALIZED
) {
4499 s
= (char *)dtrace_loadptr(daddr
+
4500 offsetof(struct dev_info
, devi_addr
));
4501 len
= dtrace_strlen(s
, size
);
4503 if (*flags
& CPU_DTRACE_FAULT
)
4507 if ((end
-= (len
+ 1)) < start
)
4513 for (i
= 1; i
<= len
; i
++)
4514 end
[i
] = dtrace_load8((uintptr_t)s
++);
4518 * Now for the node name...
4520 s
= (char *)dtrace_loadptr(daddr
+
4521 offsetof(struct dev_info
, devi_node_name
));
4523 daddr
= dtrace_loadptr(daddr
+
4524 offsetof(struct dev_info
, devi_parent
));
4527 * If our parent is NULL (that is, if we're the root
4528 * node), we're going to use the special path
4534 len
= dtrace_strlen(s
, size
);
4535 if (*flags
& CPU_DTRACE_FAULT
)
4538 if ((end
-= (len
+ 1)) < start
)
4541 for (i
= 1; i
<= len
; i
++)
4542 end
[i
] = dtrace_load8((uintptr_t)s
++);
4545 if (depth
++ > dtrace_devdepth_max
) {
4546 *flags
|= CPU_DTRACE_ILLOP
;
4552 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
4554 if (daddr
== NULL
) {
4555 regs
[rd
] = (uintptr_t)end
;
4556 mstate
->dtms_scratch_ptr
+= size
;
4562 case DIF_SUBR_DDI_PATHNAME
: {
4563 /* FIXME: awaits galactic disentanglement ;-} */
4564 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
4568 #endif /* __APPLE__ */
4570 case DIF_SUBR_STRJOIN
: {
4571 char *d
= (char *)mstate
->dtms_scratch_ptr
;
4572 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
4573 uintptr_t s1
= tupregs
[0].dttk_value
;
4574 uintptr_t s2
= tupregs
[1].dttk_value
;
4575 #if !defined(__APPLE__) /* Quiet compiler warnings */
4579 #endif /* __APPLE__ */
4581 if (!dtrace_strcanload(s1
, size
, mstate
, vstate
) ||
4582 !dtrace_strcanload(s2
, size
, mstate
, vstate
)) {
4587 if (!DTRACE_INSCRATCH(mstate
, size
)) {
4588 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
4595 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
4600 if ((d
[i
++] = dtrace_load8(s1
++)) == '\0') {
4608 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
4613 if ((d
[i
++] = dtrace_load8(s2
++)) == '\0')
4618 mstate
->dtms_scratch_ptr
+= i
;
4619 regs
[rd
] = (uintptr_t)d
;
4625 case DIF_SUBR_LLTOSTR
: {
4626 int64_t i
= (int64_t)tupregs
[0].dttk_value
;
4627 int64_t val
= i
< 0 ? i
* -1 : i
;
4628 uint64_t size
= 22; /* enough room for 2^64 in decimal */
4629 char *end
= (char *)mstate
->dtms_scratch_ptr
+ size
- 1;
4631 if (!DTRACE_INSCRATCH(mstate
, size
)) {
4632 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
4637 for (*end
-- = '\0'; val
; val
/= 10)
4638 *end
-- = '0' + (val
% 10);
4646 regs
[rd
] = (uintptr_t)end
+ 1;
4647 mstate
->dtms_scratch_ptr
+= size
;
4651 case DIF_SUBR_HTONS
:
4652 case DIF_SUBR_NTOHS
:
4654 regs
[rd
] = (uint16_t)tupregs
[0].dttk_value
;
4656 regs
[rd
] = DT_BSWAP_16((uint16_t)tupregs
[0].dttk_value
);
4661 case DIF_SUBR_HTONL
:
4662 case DIF_SUBR_NTOHL
:
4664 regs
[rd
] = (uint32_t)tupregs
[0].dttk_value
;
4666 regs
[rd
] = DT_BSWAP_32((uint32_t)tupregs
[0].dttk_value
);
4671 case DIF_SUBR_HTONLL
:
4672 case DIF_SUBR_NTOHLL
:
4674 regs
[rd
] = (uint64_t)tupregs
[0].dttk_value
;
4676 regs
[rd
] = DT_BSWAP_64((uint64_t)tupregs
[0].dttk_value
);
4681 case DIF_SUBR_DIRNAME
:
4682 case DIF_SUBR_BASENAME
: {
4683 char *dest
= (char *)mstate
->dtms_scratch_ptr
;
4684 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
4685 uintptr_t src
= tupregs
[0].dttk_value
;
4686 int i
, j
, len
= dtrace_strlen((char *)src
, size
);
4687 int lastbase
= -1, firstbase
= -1, lastdir
= -1;
4690 if (!dtrace_canload(src
, len
+ 1, mstate
, vstate
)) {
4695 if (!DTRACE_INSCRATCH(mstate
, size
)) {
4696 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
4702 * The basename and dirname for a zero-length string is
4707 src
= (uintptr_t)".";
4711 * Start from the back of the string, moving back toward the
4712 * front until we see a character that isn't a slash. That
4713 * character is the last character in the basename.
4715 for (i
= len
- 1; i
>= 0; i
--) {
4716 if (dtrace_load8(src
+ i
) != '/')
4724 * Starting from the last character in the basename, move
4725 * towards the front until we find a slash. The character
4726 * that we processed immediately before that is the first
4727 * character in the basename.
4729 for (; i
>= 0; i
--) {
4730 if (dtrace_load8(src
+ i
) == '/')
4738 * Now keep going until we find a non-slash character. That
4739 * character is the last character in the dirname.
4741 for (; i
>= 0; i
--) {
4742 if (dtrace_load8(src
+ i
) != '/')
4749 ASSERT(!(lastbase
== -1 && firstbase
!= -1));
4750 ASSERT(!(firstbase
== -1 && lastdir
!= -1));
4752 if (lastbase
== -1) {
4754 * We didn't find a non-slash character. We know that
4755 * the length is non-zero, so the whole string must be
4756 * slashes. In either the dirname or the basename
4757 * case, we return '/'.
4759 ASSERT(firstbase
== -1);
4760 firstbase
= lastbase
= lastdir
= 0;
4763 if (firstbase
== -1) {
4765 * The entire string consists only of a basename
4766 * component. If we're looking for dirname, we need
4767 * to change our string to be just "."; if we're
4768 * looking for a basename, we'll just set the first
4769 * character of the basename to be 0.
4771 if (subr
== DIF_SUBR_DIRNAME
) {
4772 ASSERT(lastdir
== -1);
4773 src
= (uintptr_t)".";
4780 if (subr
== DIF_SUBR_DIRNAME
) {
4781 if (lastdir
== -1) {
4783 * We know that we have a slash in the name --
4784 * or lastdir would be set to 0, above. And
4785 * because lastdir is -1, we know that this
4786 * slash must be the first character. (That
4787 * is, the full string must be of the form
4788 * "/basename".) In this case, the last
4789 * character of the directory name is 0.
4797 ASSERT(subr
== DIF_SUBR_BASENAME
);
4798 ASSERT(firstbase
!= -1 && lastbase
!= -1);
4803 #if !defined(__APPLE__) /* Quiet compiler warnings */
4804 for (i
= start
, j
= 0; i
<= end
&& j
< size
- 1; i
++, j
++)
4805 dest
[j
] = dtrace_load8(src
+ i
);
4807 for (i
= start
, j
= 0; i
<= end
&& (uint64_t)j
< size
- 1; i
++, j
++)
4808 dest
[j
] = dtrace_load8(src
+ i
);
4809 #endif /* __APPLE__ */
4812 regs
[rd
] = (uintptr_t)dest
;
4813 mstate
->dtms_scratch_ptr
+= size
;
4817 case DIF_SUBR_CLEANPATH
: {
4818 char *dest
= (char *)mstate
->dtms_scratch_ptr
, c
;
4819 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
4820 uintptr_t src
= tupregs
[0].dttk_value
;
4823 if (!dtrace_strcanload(src
, size
, mstate
, vstate
)) {
4828 if (!DTRACE_INSCRATCH(mstate
, size
)) {
4829 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
4835 * Move forward, loading each character.
4838 c
= dtrace_load8(src
+ i
++);
4840 #if !defined(__APPLE__) /* Quiet compiler warnings */
4841 if (j
+ 5 >= size
) /* 5 = strlen("/..c\0") */
4844 if ((uint64_t)(j
+ 5) >= size
) /* 5 = strlen("/..c\0") */
4846 #endif /* __APPLE__ */
4853 c
= dtrace_load8(src
+ i
++);
4857 * We have two slashes -- we can just advance
4858 * to the next character.
4865 * This is not "." and it's not ".." -- we can
4866 * just store the "/" and this character and
4874 c
= dtrace_load8(src
+ i
++);
4878 * This is a "/./" component. We're not going
4879 * to store anything in the destination buffer;
4880 * we're just going to go to the next component.
4887 * This is not ".." -- we can just store the
4888 * "/." and this character and continue
4897 c
= dtrace_load8(src
+ i
++);
4899 if (c
!= '/' && c
!= '\0') {
4901 * This is not ".." -- it's "..[mumble]".
4902 * We'll store the "/.." and this character
4903 * and continue processing.
4913 * This is "/../" or "/..\0". We need to back up
4914 * our destination pointer until we find a "/".
4917 while (j
!= 0 && dest
[--j
] != '/')
4922 } while (c
!= '\0');
4925 regs
[rd
] = (uintptr_t)dest
;
4926 mstate
->dtms_scratch_ptr
+= size
;
4930 case DIF_SUBR_INET_NTOA
:
4931 case DIF_SUBR_INET_NTOA6
:
4932 case DIF_SUBR_INET_NTOP
: {
4937 if (subr
== DIF_SUBR_INET_NTOP
) {
4938 af
= (int)tupregs
[0].dttk_value
;
4941 af
= subr
== DIF_SUBR_INET_NTOA
? AF_INET
: AF_INET6
;
4945 if (af
== AF_INET
) {
4946 #if !defined(__APPLE__)
4950 #endif /* __APPLE__ */
4954 * Safely load the IPv4 address.
4956 ip4
= dtrace_load32(tupregs
[argi
].dttk_value
);
4959 * Check an IPv4 string will fit in scratch.
4961 #if !defined(__APPLE__)
4962 size
= INET_ADDRSTRLEN
;
4964 size
= MAX_IPv4_STR_LEN
;
4965 #endif /* __APPLE__ */
4966 if (!DTRACE_INSCRATCH(mstate
, size
)) {
4967 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
4971 base
= (char *)mstate
->dtms_scratch_ptr
;
4972 end
= (char *)mstate
->dtms_scratch_ptr
+ size
- 1;
4975 * Stringify as a dotted decimal quad.
4978 ptr8
= (uint8_t *)&ip4
;
4979 for (i
= 3; i
>= 0; i
--) {
4985 for (; val
; val
/= 10) {
4986 *end
-- = '0' + (val
% 10);
4993 ASSERT(end
+ 1 >= base
);
4995 } else if (af
== AF_INET6
) {
4996 #if defined(__APPLE__)
4997 #define _S6_un __u6_addr
4998 #define _S6_u8 __u6_addr8
4999 #endif /* __APPLE__ */
5000 struct in6_addr ip6
;
5001 int firstzero
, tryzero
, numzero
, v6end
;
5003 const char digits
[] = "0123456789abcdef";
5006 * Stringify using RFC 1884 convention 2 - 16 bit
5007 * hexadecimal values with a zero-run compression.
5008 * Lower case hexadecimal digits are used.
5009 * eg, fe80::214:4fff:fe0b:76c8.
5010 * The IPv4 embedded form is returned for inet_ntop,
5011 * just the IPv4 string is returned for inet_ntoa6.
5015 * Safely load the IPv6 address.
5018 (void *)(uintptr_t)tupregs
[argi
].dttk_value
,
5019 (void *)(uintptr_t)&ip6
, sizeof (struct in6_addr
));
5022 * Check an IPv6 string will fit in scratch.
5024 size
= INET6_ADDRSTRLEN
;
5025 if (!DTRACE_INSCRATCH(mstate
, size
)) {
5026 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
5030 base
= (char *)mstate
->dtms_scratch_ptr
;
5031 end
= (char *)mstate
->dtms_scratch_ptr
+ size
- 1;
5035 * Find the longest run of 16 bit zero values
5036 * for the single allowed zero compression - "::".
5041 #if !defined(__APPLE__) /* Quiet compiler warnings */
5042 for (i
= 0; i
< sizeof (struct in6_addr
); i
++) {
5044 for (i
= 0; i
< (int)sizeof (struct in6_addr
); i
++) {
5045 #endif /* __APPLE__ */
5046 if (ip6
._S6_un
._S6_u8
[i
] == 0 &&
5047 tryzero
== -1 && i
% 2 == 0) {
5052 if (tryzero
!= -1 &&
5053 (ip6
._S6_un
._S6_u8
[i
] != 0 ||
5054 i
== sizeof (struct in6_addr
) - 1)) {
5056 if (i
- tryzero
<= numzero
) {
5061 firstzero
= tryzero
;
5062 numzero
= i
- i
% 2 - tryzero
;
5065 if (ip6
._S6_un
._S6_u8
[i
] == 0 &&
5066 i
== sizeof (struct in6_addr
) - 1)
5070 #if !defined(__APPLE__) /* Quiet compiler warnings */
5071 ASSERT(firstzero
+ numzero
<= sizeof (struct in6_addr
));
5073 ASSERT(firstzero
+ numzero
<= (int)sizeof (struct in6_addr
));
5074 #endif /* __APPLE__ */
5077 * Check for an IPv4 embedded address.
5079 v6end
= sizeof (struct in6_addr
) - 2;
5080 if (IN6_IS_ADDR_V4MAPPED(&ip6
) ||
5081 IN6_IS_ADDR_V4COMPAT(&ip6
)) {
5082 #if !defined(__APPLE__) /* Quiet compiler warnings */
5083 for (i
= sizeof (struct in6_addr
) - 1;
5084 i
>= DTRACE_V4MAPPED_OFFSET
; i
--) {
5086 for (i
= sizeof (struct in6_addr
) - 1;
5087 i
>= (int)DTRACE_V4MAPPED_OFFSET
; i
--) {
5088 #endif /* __APPLE__ */
5089 ASSERT(end
>= base
);
5091 val
= ip6
._S6_un
._S6_u8
[i
];
5096 for (; val
; val
/= 10) {
5097 *end
-- = '0' + val
% 10;
5101 #if !defined(__APPLE__) /* Quiet compiler warnings */
5102 if (i
> DTRACE_V4MAPPED_OFFSET
)
5105 if (i
> (int)DTRACE_V4MAPPED_OFFSET
)
5107 #endif /* __APPLE__ */
5110 if (subr
== DIF_SUBR_INET_NTOA6
)
5114 * Set v6end to skip the IPv4 address that
5115 * we have already stringified.
5121 * Build the IPv6 string by working through the
5122 * address in reverse.
5124 for (i
= v6end
; i
>= 0; i
-= 2) {
5125 ASSERT(end
>= base
);
5127 if (i
== firstzero
+ numzero
- 2) {
5134 if (i
< 14 && i
!= firstzero
- 2)
5137 val
= (ip6
._S6_un
._S6_u8
[i
] << 8) +
5138 ip6
._S6_un
._S6_u8
[i
+ 1];
5143 for (; val
; val
/= 16) {
5144 *end
-- = digits
[val
% 16];
5148 ASSERT(end
+ 1 >= base
);
5150 #if defined(__APPLE__)
5153 #endif /* __APPLE__ */
5156 * The user didn't use AH_INET or AH_INET6.
5158 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
5163 inetout
: regs
[rd
] = (uintptr_t)end
+ 1;
5164 mstate
->dtms_scratch_ptr
+= size
;
5170 /* CoreProfile callback ('core_profile(uint64_t, [uint64_t], [uint64_t] ...)') */
5171 case DIF_SUBR_COREPROFILE
: {
5172 uint64_t selector
= tupregs
[0].dttk_value
;
5173 uint64_t args
[DIF_DTR_NREGS
-1] = {0ULL};
5175 uint32_t count
= (uint32_t)nargs
;
5178 regs
[rd
] = KERN_FAILURE
;
5182 if(count
> DIF_DTR_NREGS
)
5183 count
= DIF_DTR_NREGS
;
5185 /* copy in any variadic argument list, bounded by DIF_DTR_NREGS */
5186 for(ii
= 0; ii
< count
-1; ii
++) {
5187 args
[ii
] = tupregs
[ii
+1].dttk_value
;
5191 chudxnu_dtrace_callback(selector
, args
, count
-1);
5192 if(KERN_SUCCESS
!= ret
) {
5200 #endif /* __APPLE__ */
5206 * Emulate the execution of DTrace IR instructions specified by the given
5207 * DIF object. This function is deliberately void of assertions as all of
5208 * the necessary checks are handled by a call to dtrace_difo_validate().
5211 dtrace_dif_emulate(dtrace_difo_t
*difo
, dtrace_mstate_t
*mstate
,
5212 dtrace_vstate_t
*vstate
, dtrace_state_t
*state
)
5214 const dif_instr_t
*text
= difo
->dtdo_buf
;
5215 const uint_t textlen
= difo
->dtdo_len
;
5216 const char *strtab
= difo
->dtdo_strtab
;
5217 const uint64_t *inttab
= difo
->dtdo_inttab
;
5220 dtrace_statvar_t
*svar
;
5221 dtrace_dstate_t
*dstate
= &vstate
->dtvs_dynvars
;
5223 volatile uint16_t *flags
= &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
5224 #if !defined(__APPLE__)
5225 volatile uintptr_t *illval
= &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
;
5227 volatile uint64_t *illval
= &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
;
5228 #endif /* __APPLE__ */
5230 dtrace_key_t tupregs
[DIF_DTR_NREGS
+ 2]; /* +2 for thread and id */
5231 uint64_t regs
[DIF_DIR_NREGS
];
5234 uint8_t cc_n
= 0, cc_z
= 0, cc_v
= 0, cc_c
= 0;
5236 #if !defined(__APPLE__) /* Quiet compiler warnings */
5237 uint_t pc
= 0, id
, opc
;
5239 uint_t pc
= 0, id
, opc
= 0;
5240 #endif /* __APPLE__ */
5246 * We stash the current DIF object into the machine state: we need it
5247 * for subsequent access checking.
5249 mstate
->dtms_difo
= difo
;
5251 regs
[DIF_REG_R0
] = 0; /* %r0 is fixed at zero */
5253 while (pc
< textlen
&& !(*flags
& CPU_DTRACE_FAULT
)) {
5257 r1
= DIF_INSTR_R1(instr
);
5258 r2
= DIF_INSTR_R2(instr
);
5259 rd
= DIF_INSTR_RD(instr
);
5261 switch (DIF_INSTR_OP(instr
)) {
5263 regs
[rd
] = regs
[r1
] | regs
[r2
];
5266 regs
[rd
] = regs
[r1
] ^ regs
[r2
];
5269 regs
[rd
] = regs
[r1
] & regs
[r2
];
5272 regs
[rd
] = regs
[r1
] << regs
[r2
];
5275 regs
[rd
] = regs
[r1
] >> regs
[r2
];
5278 regs
[rd
] = regs
[r1
] - regs
[r2
];
5281 regs
[rd
] = regs
[r1
] + regs
[r2
];
5284 regs
[rd
] = regs
[r1
] * regs
[r2
];
5287 if (regs
[r2
] == 0) {
5289 *flags
|= CPU_DTRACE_DIVZERO
;
5291 regs
[rd
] = (int64_t)regs
[r1
] /
5297 if (regs
[r2
] == 0) {
5299 *flags
|= CPU_DTRACE_DIVZERO
;
5301 regs
[rd
] = regs
[r1
] / regs
[r2
];
5306 if (regs
[r2
] == 0) {
5308 *flags
|= CPU_DTRACE_DIVZERO
;
5310 regs
[rd
] = (int64_t)regs
[r1
] %
5316 if (regs
[r2
] == 0) {
5318 *flags
|= CPU_DTRACE_DIVZERO
;
5320 regs
[rd
] = regs
[r1
] % regs
[r2
];
5325 regs
[rd
] = ~regs
[r1
];
5328 regs
[rd
] = regs
[r1
];
5331 cc_r
= regs
[r1
] - regs
[r2
];
5335 cc_c
= regs
[r1
] < regs
[r2
];
5338 cc_n
= cc_v
= cc_c
= 0;
5339 cc_z
= regs
[r1
] == 0;
5342 pc
= DIF_INSTR_LABEL(instr
);
5346 pc
= DIF_INSTR_LABEL(instr
);
5350 pc
= DIF_INSTR_LABEL(instr
);
5353 if ((cc_z
| (cc_n
^ cc_v
)) == 0)
5354 pc
= DIF_INSTR_LABEL(instr
);
5357 if ((cc_c
| cc_z
) == 0)
5358 pc
= DIF_INSTR_LABEL(instr
);
5361 if ((cc_n
^ cc_v
) == 0)
5362 pc
= DIF_INSTR_LABEL(instr
);
5366 pc
= DIF_INSTR_LABEL(instr
);
5370 pc
= DIF_INSTR_LABEL(instr
);
5374 pc
= DIF_INSTR_LABEL(instr
);
5377 if (cc_z
| (cc_n
^ cc_v
))
5378 pc
= DIF_INSTR_LABEL(instr
);
5382 pc
= DIF_INSTR_LABEL(instr
);
5385 if (!dtrace_canstore(regs
[r1
], 1, mstate
, vstate
)) {
5386 *flags
|= CPU_DTRACE_KPRIV
;
5392 regs
[rd
] = (int8_t)dtrace_load8(regs
[r1
]);
5395 if (!dtrace_canstore(regs
[r1
], 2, mstate
, vstate
)) {
5396 *flags
|= CPU_DTRACE_KPRIV
;
5402 regs
[rd
] = (int16_t)dtrace_load16(regs
[r1
]);
5405 if (!dtrace_canstore(regs
[r1
], 4, mstate
, vstate
)) {
5406 *flags
|= CPU_DTRACE_KPRIV
;
5412 regs
[rd
] = (int32_t)dtrace_load32(regs
[r1
]);
5415 if (!dtrace_canstore(regs
[r1
], 1, mstate
, vstate
)) {
5416 *flags
|= CPU_DTRACE_KPRIV
;
5422 regs
[rd
] = dtrace_load8(regs
[r1
]);
5425 if (!dtrace_canstore(regs
[r1
], 2, mstate
, vstate
)) {
5426 *flags
|= CPU_DTRACE_KPRIV
;
5432 regs
[rd
] = dtrace_load16(regs
[r1
]);
5435 if (!dtrace_canstore(regs
[r1
], 4, mstate
, vstate
)) {
5436 *flags
|= CPU_DTRACE_KPRIV
;
5442 regs
[rd
] = dtrace_load32(regs
[r1
]);
5445 if (!dtrace_canstore(regs
[r1
], 8, mstate
, vstate
)) {
5446 *flags
|= CPU_DTRACE_KPRIV
;
5452 regs
[rd
] = dtrace_load64(regs
[r1
]);
5454 #if !defined(__APPLE__)
5457 dtrace_fuword8((void *)(uintptr_t)regs
[r1
]);
5460 regs
[rd
] = (int16_t)
5461 dtrace_fuword16((void *)(uintptr_t)regs
[r1
]);
5464 regs
[rd
] = (int32_t)
5465 dtrace_fuword32((void *)(uintptr_t)regs
[r1
]);
5469 dtrace_fuword8((void *)(uintptr_t)regs
[r1
]);
5473 dtrace_fuword16((void *)(uintptr_t)regs
[r1
]);
5477 dtrace_fuword32((void *)(uintptr_t)regs
[r1
]);
5481 dtrace_fuword64((void *)(uintptr_t)regs
[r1
]);
5483 #else /* Darwin 32-bit kernel may fetch from 64-bit user. Don't want uintptr_t cast. */
5486 dtrace_fuword8(regs
[r1
]);
5489 regs
[rd
] = (int16_t)
5490 dtrace_fuword16(regs
[r1
]);
5493 regs
[rd
] = (int32_t)
5494 dtrace_fuword32(regs
[r1
]);
5498 dtrace_fuword8(regs
[r1
]);
5502 dtrace_fuword16(regs
[r1
]);
5506 dtrace_fuword32(regs
[r1
]);
5510 dtrace_fuword64(regs
[r1
]);
5511 #endif /* __APPLE__ */
5520 regs
[rd
] = inttab
[DIF_INSTR_INTEGER(instr
)];
5523 regs
[rd
] = (uint64_t)(uintptr_t)
5524 (strtab
+ DIF_INSTR_STRING(instr
));
5527 size_t sz
= state
->dts_options
[DTRACEOPT_STRSIZE
];
5528 uintptr_t s1
= regs
[r1
];
5529 uintptr_t s2
= regs
[r2
];
5532 !dtrace_strcanload(s1
, sz
, mstate
, vstate
))
5535 !dtrace_strcanload(s2
, sz
, mstate
, vstate
))
5538 cc_r
= dtrace_strncmp((char *)s1
, (char *)s2
, sz
);
5546 regs
[rd
] = dtrace_dif_variable(mstate
, state
,
5550 id
= DIF_INSTR_VAR(instr
);
5552 if (id
>= DIF_VAR_OTHER_UBASE
) {
5555 id
-= DIF_VAR_OTHER_UBASE
;
5556 svar
= vstate
->dtvs_globals
[id
];
5557 ASSERT(svar
!= NULL
);
5558 v
= &svar
->dtsv_var
;
5560 if (!(v
->dtdv_type
.dtdt_flags
& DIF_TF_BYREF
)) {
5561 regs
[rd
] = svar
->dtsv_data
;
5565 a
= (uintptr_t)svar
->dtsv_data
;
5567 if (*(uint8_t *)a
== UINT8_MAX
) {
5569 * If the 0th byte is set to UINT8_MAX
5570 * then this is to be treated as a
5571 * reference to a NULL variable.
5575 regs
[rd
] = a
+ sizeof (uint64_t);
5581 regs
[rd
] = dtrace_dif_variable(mstate
, state
, id
, 0);
5585 id
= DIF_INSTR_VAR(instr
);
5587 ASSERT(id
>= DIF_VAR_OTHER_UBASE
);
5588 id
-= DIF_VAR_OTHER_UBASE
;
5590 svar
= vstate
->dtvs_globals
[id
];
5591 ASSERT(svar
!= NULL
);
5592 v
= &svar
->dtsv_var
;
5594 if (v
->dtdv_type
.dtdt_flags
& DIF_TF_BYREF
) {
5595 uintptr_t a
= (uintptr_t)svar
->dtsv_data
;
5598 ASSERT(svar
->dtsv_size
!= 0);
5600 if (regs
[rd
] == NULL
) {
5601 *(uint8_t *)a
= UINT8_MAX
;
5605 a
+= sizeof (uint64_t);
5607 if (!dtrace_vcanload(
5608 (void *)(uintptr_t)regs
[rd
], &v
->dtdv_type
,
5612 dtrace_vcopy((void *)(uintptr_t)regs
[rd
],
5613 (void *)a
, &v
->dtdv_type
);
5617 svar
->dtsv_data
= regs
[rd
];
5622 * There are no DTrace built-in thread-local arrays at
5623 * present. This opcode is saved for future work.
5625 *flags
|= CPU_DTRACE_ILLOP
;
5630 id
= DIF_INSTR_VAR(instr
);
5632 if (id
< DIF_VAR_OTHER_UBASE
) {
5634 * For now, this has no meaning.
5640 id
-= DIF_VAR_OTHER_UBASE
;
5642 #if !defined(__APPLE__) /* Quiet compiler warnings */
5643 ASSERT(id
< vstate
->dtvs_nlocals
);
5645 ASSERT(id
< (uint_t
)vstate
->dtvs_nlocals
);
5646 #endif /* __APPLE__ */
5647 ASSERT(vstate
->dtvs_locals
!= NULL
);
5649 svar
= vstate
->dtvs_locals
[id
];
5650 ASSERT(svar
!= NULL
);
5651 v
= &svar
->dtsv_var
;
5653 if (v
->dtdv_type
.dtdt_flags
& DIF_TF_BYREF
) {
5654 uintptr_t a
= (uintptr_t)svar
->dtsv_data
;
5655 size_t sz
= v
->dtdv_type
.dtdt_size
;
5657 sz
+= sizeof (uint64_t);
5658 ASSERT(svar
->dtsv_size
== (int)NCPU
* sz
);
5659 a
+= CPU
->cpu_id
* sz
;
5661 if (*(uint8_t *)a
== UINT8_MAX
) {
5663 * If the 0th byte is set to UINT8_MAX
5664 * then this is to be treated as a
5665 * reference to a NULL variable.
5669 regs
[rd
] = a
+ sizeof (uint64_t);
5675 ASSERT(svar
->dtsv_size
== (int)NCPU
* sizeof (uint64_t));
5676 tmp
= (uint64_t *)(uintptr_t)svar
->dtsv_data
;
5677 regs
[rd
] = tmp
[CPU
->cpu_id
];
5681 id
= DIF_INSTR_VAR(instr
);
5683 ASSERT(id
>= DIF_VAR_OTHER_UBASE
);
5684 id
-= DIF_VAR_OTHER_UBASE
;
5685 #if !defined(__APPLE__) /* Quiet compiler warnings */
5686 ASSERT(id
< vstate
->dtvs_nlocals
);
5688 ASSERT(id
< (uint_t
)vstate
->dtvs_nlocals
);
5689 #endif /* __APPLE__ */
5691 ASSERT(vstate
->dtvs_locals
!= NULL
);
5692 svar
= vstate
->dtvs_locals
[id
];
5693 ASSERT(svar
!= NULL
);
5694 v
= &svar
->dtsv_var
;
5696 if (v
->dtdv_type
.dtdt_flags
& DIF_TF_BYREF
) {
5697 uintptr_t a
= (uintptr_t)svar
->dtsv_data
;
5698 size_t sz
= v
->dtdv_type
.dtdt_size
;
5700 sz
+= sizeof (uint64_t);
5701 ASSERT(svar
->dtsv_size
== (int)NCPU
* sz
);
5702 a
+= CPU
->cpu_id
* sz
;
5704 if (regs
[rd
] == NULL
) {
5705 *(uint8_t *)a
= UINT8_MAX
;
5709 a
+= sizeof (uint64_t);
5712 if (!dtrace_vcanload(
5713 (void *)(uintptr_t)regs
[rd
], &v
->dtdv_type
,
5717 dtrace_vcopy((void *)(uintptr_t)regs
[rd
],
5718 (void *)a
, &v
->dtdv_type
);
5722 ASSERT(svar
->dtsv_size
== (int)NCPU
* sizeof (uint64_t));
5723 tmp
= (uint64_t *)(uintptr_t)svar
->dtsv_data
;
5724 tmp
[CPU
->cpu_id
] = regs
[rd
];
5728 dtrace_dynvar_t
*dvar
;
5731 id
= DIF_INSTR_VAR(instr
);
5732 ASSERT(id
>= DIF_VAR_OTHER_UBASE
);
5733 id
-= DIF_VAR_OTHER_UBASE
;
5734 v
= &vstate
->dtvs_tlocals
[id
];
5736 key
= &tupregs
[DIF_DTR_NREGS
];
5737 key
[0].dttk_value
= (uint64_t)id
;
5738 key
[0].dttk_size
= 0;
5739 DTRACE_TLS_THRKEY(key
[1].dttk_value
);
5740 key
[1].dttk_size
= 0;
5742 dvar
= dtrace_dynvar(dstate
, 2, key
,
5743 sizeof (uint64_t), DTRACE_DYNVAR_NOALLOC
,
5751 if (v
->dtdv_type
.dtdt_flags
& DIF_TF_BYREF
) {
5752 regs
[rd
] = (uint64_t)(uintptr_t)dvar
->dtdv_data
;
5754 regs
[rd
] = *((uint64_t *)dvar
->dtdv_data
);
5761 dtrace_dynvar_t
*dvar
;
5764 id
= DIF_INSTR_VAR(instr
);
5765 ASSERT(id
>= DIF_VAR_OTHER_UBASE
);
5766 id
-= DIF_VAR_OTHER_UBASE
;
5768 key
= &tupregs
[DIF_DTR_NREGS
];
5769 key
[0].dttk_value
= (uint64_t)id
;
5770 key
[0].dttk_size
= 0;
5771 DTRACE_TLS_THRKEY(key
[1].dttk_value
);
5772 key
[1].dttk_size
= 0;
5773 v
= &vstate
->dtvs_tlocals
[id
];
5775 dvar
= dtrace_dynvar(dstate
, 2, key
,
5776 v
->dtdv_type
.dtdt_size
> sizeof (uint64_t) ?
5777 v
->dtdv_type
.dtdt_size
: sizeof (uint64_t),
5778 regs
[rd
] ? DTRACE_DYNVAR_ALLOC
:
5779 DTRACE_DYNVAR_DEALLOC
, mstate
, vstate
);
5782 * Given that we're storing to thread-local data,
5783 * we need to flush our predicate cache.
5785 #if !defined(__APPLE__)
5786 curthread
->t_predcache
= NULL
;
5788 dtrace_set_thread_predcache(current_thread(), 0);
5789 #endif /* __APPLE__ */
5794 if (v
->dtdv_type
.dtdt_flags
& DIF_TF_BYREF
) {
5795 if (!dtrace_vcanload(
5796 (void *)(uintptr_t)regs
[rd
],
5797 &v
->dtdv_type
, mstate
, vstate
))
5800 dtrace_vcopy((void *)(uintptr_t)regs
[rd
],
5801 dvar
->dtdv_data
, &v
->dtdv_type
);
5803 *((uint64_t *)dvar
->dtdv_data
) = regs
[rd
];
5810 regs
[rd
] = (int64_t)regs
[r1
] >> regs
[r2
];
5814 dtrace_dif_subr(DIF_INSTR_SUBR(instr
), rd
,
5815 regs
, tupregs
, ttop
, mstate
, state
);
5819 if (ttop
== DIF_DTR_NREGS
) {
5820 *flags
|= CPU_DTRACE_TUPOFLOW
;
5824 if (r1
== DIF_TYPE_STRING
) {
5826 * If this is a string type and the size is 0,
5827 * we'll use the system-wide default string
5828 * size. Note that we are _not_ looking at
5829 * the value of the DTRACEOPT_STRSIZE option;
5830 * had this been set, we would expect to have
5831 * a non-zero size value in the "pushtr".
5833 tupregs
[ttop
].dttk_size
=
5834 dtrace_strlen((char *)(uintptr_t)regs
[rd
],
5835 regs
[r2
] ? regs
[r2
] :
5836 dtrace_strsize_default
) + 1;
5838 tupregs
[ttop
].dttk_size
= regs
[r2
];
5841 tupregs
[ttop
++].dttk_value
= regs
[rd
];
5845 if (ttop
== DIF_DTR_NREGS
) {
5846 *flags
|= CPU_DTRACE_TUPOFLOW
;
5850 tupregs
[ttop
].dttk_value
= regs
[rd
];
5851 tupregs
[ttop
++].dttk_size
= 0;
5859 case DIF_OP_FLUSHTS
:
5864 case DIF_OP_LDTAA
: {
5865 dtrace_dynvar_t
*dvar
;
5866 dtrace_key_t
*key
= tupregs
;
5867 uint_t nkeys
= ttop
;
5869 id
= DIF_INSTR_VAR(instr
);
5870 ASSERT(id
>= DIF_VAR_OTHER_UBASE
);
5871 id
-= DIF_VAR_OTHER_UBASE
;
5873 key
[nkeys
].dttk_value
= (uint64_t)id
;
5874 key
[nkeys
++].dttk_size
= 0;
5876 if (DIF_INSTR_OP(instr
) == DIF_OP_LDTAA
) {
5877 DTRACE_TLS_THRKEY(key
[nkeys
].dttk_value
);
5878 key
[nkeys
++].dttk_size
= 0;
5879 v
= &vstate
->dtvs_tlocals
[id
];
5881 v
= &vstate
->dtvs_globals
[id
]->dtsv_var
;
5884 dvar
= dtrace_dynvar(dstate
, nkeys
, key
,
5885 v
->dtdv_type
.dtdt_size
> sizeof (uint64_t) ?
5886 v
->dtdv_type
.dtdt_size
: sizeof (uint64_t),
5887 DTRACE_DYNVAR_NOALLOC
, mstate
, vstate
);
5894 if (v
->dtdv_type
.dtdt_flags
& DIF_TF_BYREF
) {
5895 regs
[rd
] = (uint64_t)(uintptr_t)dvar
->dtdv_data
;
5897 regs
[rd
] = *((uint64_t *)dvar
->dtdv_data
);
5904 case DIF_OP_STTAA
: {
5905 dtrace_dynvar_t
*dvar
;
5906 dtrace_key_t
*key
= tupregs
;
5907 uint_t nkeys
= ttop
;
5909 id
= DIF_INSTR_VAR(instr
);
5910 ASSERT(id
>= DIF_VAR_OTHER_UBASE
);
5911 id
-= DIF_VAR_OTHER_UBASE
;
5913 key
[nkeys
].dttk_value
= (uint64_t)id
;
5914 key
[nkeys
++].dttk_size
= 0;
5916 if (DIF_INSTR_OP(instr
) == DIF_OP_STTAA
) {
5917 DTRACE_TLS_THRKEY(key
[nkeys
].dttk_value
);
5918 key
[nkeys
++].dttk_size
= 0;
5919 v
= &vstate
->dtvs_tlocals
[id
];
5921 v
= &vstate
->dtvs_globals
[id
]->dtsv_var
;
5924 dvar
= dtrace_dynvar(dstate
, nkeys
, key
,
5925 v
->dtdv_type
.dtdt_size
> sizeof (uint64_t) ?
5926 v
->dtdv_type
.dtdt_size
: sizeof (uint64_t),
5927 regs
[rd
] ? DTRACE_DYNVAR_ALLOC
:
5928 DTRACE_DYNVAR_DEALLOC
, mstate
, vstate
);
5933 if (v
->dtdv_type
.dtdt_flags
& DIF_TF_BYREF
) {
5934 if (!dtrace_vcanload(
5935 (void *)(uintptr_t)regs
[rd
], &v
->dtdv_type
,
5939 dtrace_vcopy((void *)(uintptr_t)regs
[rd
],
5940 dvar
->dtdv_data
, &v
->dtdv_type
);
5942 *((uint64_t *)dvar
->dtdv_data
) = regs
[rd
];
5948 case DIF_OP_ALLOCS
: {
5949 uintptr_t ptr
= P2ROUNDUP(mstate
->dtms_scratch_ptr
, 8);
5950 size_t size
= ptr
- mstate
->dtms_scratch_ptr
+ regs
[r1
];
5953 * Rounding up the user allocation size could have
5954 * overflowed large, bogus allocations (like -1ULL) to
5957 if (size
< regs
[r1
] ||
5958 !DTRACE_INSCRATCH(mstate
, size
)) {
5959 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
5964 dtrace_bzero((void *) mstate
->dtms_scratch_ptr
, size
);
5965 mstate
->dtms_scratch_ptr
+= size
;
5971 if (!dtrace_canstore(regs
[rd
], regs
[r2
],
5973 *flags
|= CPU_DTRACE_BADADDR
;
5978 if (!dtrace_canload(regs
[r1
], regs
[r2
], mstate
, vstate
))
5981 dtrace_bcopy((void *)(uintptr_t)regs
[r1
],
5982 (void *)(uintptr_t)regs
[rd
], (size_t)regs
[r2
]);
5986 if (!dtrace_canstore(regs
[rd
], 1, mstate
, vstate
)) {
5987 *flags
|= CPU_DTRACE_BADADDR
;
5991 *((uint8_t *)(uintptr_t)regs
[rd
]) = (uint8_t)regs
[r1
];
5995 if (!dtrace_canstore(regs
[rd
], 2, mstate
, vstate
)) {
5996 *flags
|= CPU_DTRACE_BADADDR
;
6001 *flags
|= CPU_DTRACE_BADALIGN
;
6005 *((uint16_t *)(uintptr_t)regs
[rd
]) = (uint16_t)regs
[r1
];
6009 if (!dtrace_canstore(regs
[rd
], 4, mstate
, vstate
)) {
6010 *flags
|= CPU_DTRACE_BADADDR
;
6015 *flags
|= CPU_DTRACE_BADALIGN
;
6019 *((uint32_t *)(uintptr_t)regs
[rd
]) = (uint32_t)regs
[r1
];
6023 if (!dtrace_canstore(regs
[rd
], 8, mstate
, vstate
)) {
6024 *flags
|= CPU_DTRACE_BADADDR
;
6028 #if !defined(__APPLE__)
6031 if (regs
[rd
] & 3) { /* Darwin kmem_zalloc() called from dtrace_difo_init() is 4-byte aligned. */
6032 #endif /* __APPLE__ */
6033 *flags
|= CPU_DTRACE_BADALIGN
;
6037 *((uint64_t *)(uintptr_t)regs
[rd
]) = regs
[r1
];
6042 if (!(*flags
& CPU_DTRACE_FAULT
))
6045 mstate
->dtms_fltoffs
= opc
* sizeof (dif_instr_t
);
6046 mstate
->dtms_present
|= DTRACE_MSTATE_FLTOFFS
;
6052 dtrace_action_breakpoint(dtrace_ecb_t
*ecb
)
6054 dtrace_probe_t
*probe
= ecb
->dte_probe
;
6055 dtrace_provider_t
*prov
= probe
->dtpr_provider
;
6056 char c
[DTRACE_FULLNAMELEN
+ 80], *str
;
6057 #if !defined(__APPLE__) /* Quiet compiler warnings */
6058 char *msg
= "dtrace: breakpoint action at probe ";
6059 char *ecbmsg
= " (ecb ";
6061 const char *msg
= "dtrace: breakpoint action at probe ";
6062 const char *ecbmsg
= " (ecb ";
6063 #endif /* __APPLE__ */
6064 uintptr_t mask
= (0xf << (sizeof (uintptr_t) * NBBY
/ 4));
6065 uintptr_t val
= (uintptr_t)ecb
;
6066 int shift
= (sizeof (uintptr_t) * NBBY
) - 4, i
= 0;
6068 if (dtrace_destructive_disallow
)
6072 * It's impossible to be taking action on the NULL probe.
6074 ASSERT(probe
!= NULL
);
6077 * This is a poor man's (destitute man's?) sprintf(): we want to
6078 * print the provider name, module name, function name and name of
6079 * the probe, along with the hex address of the ECB with the breakpoint
6080 * action -- all of which we must place in the character buffer by
6083 while (*msg
!= '\0')
6086 for (str
= prov
->dtpv_name
; *str
!= '\0'; str
++)
6090 for (str
= probe
->dtpr_mod
; *str
!= '\0'; str
++)
6094 for (str
= probe
->dtpr_func
; *str
!= '\0'; str
++)
6098 for (str
= probe
->dtpr_name
; *str
!= '\0'; str
++)
6101 while (*ecbmsg
!= '\0')
6104 while (shift
>= 0) {
6105 mask
= (uintptr_t)0xf << shift
;
6107 if (val
>= ((uintptr_t)1 << shift
))
6108 c
[i
++] = "0123456789abcdef"[(val
& mask
) >> shift
];
6119 dtrace_action_panic(dtrace_ecb_t
*ecb
)
6121 dtrace_probe_t
*probe
= ecb
->dte_probe
;
6124 * It's impossible to be taking action on the NULL probe.
6126 ASSERT(probe
!= NULL
);
6128 if (dtrace_destructive_disallow
)
6131 if (dtrace_panicked
!= NULL
)
6134 #if !defined(__APPLE__)
6135 if (dtrace_casptr(&dtrace_panicked
, NULL
, curthread
) != NULL
)
6138 if (dtrace_casptr(&dtrace_panicked
, NULL
, current_thread()) != NULL
)
6140 #endif /* __APPLE__ */
6143 * We won the right to panic. (We want to be sure that only one
6144 * thread calls panic() from dtrace_probe(), and that panic() is
6145 * called exactly once.)
6147 dtrace_panic("dtrace: panic action at probe %s:%s:%s:%s (ecb %p)",
6148 probe
->dtpr_provider
->dtpv_name
, probe
->dtpr_mod
,
6149 probe
->dtpr_func
, probe
->dtpr_name
, (void *)ecb
);
6151 #if defined(__APPLE__)
6152 /* Mac OS X debug feature -- can return from panic() */
6153 dtrace_panicked
= NULL
;
6154 #endif /* __APPLE__ */
6158 dtrace_action_raise(uint64_t sig
)
6160 if (dtrace_destructive_disallow
)
6164 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
6168 #if !defined(__APPLE__)
6170 * raise() has a queue depth of 1 -- we ignore all subsequent
6171 * invocations of the raise() action.
6173 if (curthread
->t_dtrace_sig
== 0)
6174 curthread
->t_dtrace_sig
= (uint8_t)sig
;
6176 curthread
->t_sig_check
= 1;
6179 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
6181 if (uthread
&& uthread
->t_dtrace_sig
== 0) {
6182 uthread
->t_dtrace_sig
= sig
;
6185 #endif /* __APPLE__ */
6189 dtrace_action_stop(void)
6191 if (dtrace_destructive_disallow
)
6194 #if !defined(__APPLE__)
6195 if (!curthread
->t_dtrace_stop
) {
6196 curthread
->t_dtrace_stop
= 1;
6197 curthread
->t_sig_check
= 1;
6201 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
6203 if (uthread
&& uthread
->t_dtrace_stop
== 0) {
6204 uthread
->t_dtrace_stop
= 1;
6207 #endif /* __APPLE__ */
6211 dtrace_action_chill(dtrace_mstate_t
*mstate
, hrtime_t val
)
6214 volatile uint16_t *flags
;
6217 if (dtrace_destructive_disallow
)
6220 flags
= (volatile uint16_t *)&cpu_core
[cpu
->cpu_id
].cpuc_dtrace_flags
;
6222 now
= dtrace_gethrtime();
6224 if (now
- cpu
->cpu_dtrace_chillmark
> dtrace_chill_interval
) {
6226 * We need to advance the mark to the current time.
6228 cpu
->cpu_dtrace_chillmark
= now
;
6229 cpu
->cpu_dtrace_chilled
= 0;
6233 * Now check to see if the requested chill time would take us over
6234 * the maximum amount of time allowed in the chill interval. (Or
6235 * worse, if the calculation itself induces overflow.)
6237 if (cpu
->cpu_dtrace_chilled
+ val
> dtrace_chill_max
||
6238 cpu
->cpu_dtrace_chilled
+ val
< cpu
->cpu_dtrace_chilled
) {
6239 *flags
|= CPU_DTRACE_ILLOP
;
6243 while (dtrace_gethrtime() - now
< val
)
6247 * Normally, we assure that the value of the variable "timestamp" does
6248 * not change within an ECB. The presence of chill() represents an
6249 * exception to this rule, however.
6251 mstate
->dtms_present
&= ~DTRACE_MSTATE_TIMESTAMP
;
6252 cpu
->cpu_dtrace_chilled
+= val
;
6256 dtrace_action_ustack(dtrace_mstate_t
*mstate
, dtrace_state_t
*state
,
6257 uint64_t *buf
, uint64_t arg
)
6259 int nframes
= DTRACE_USTACK_NFRAMES(arg
);
6260 int strsize
= DTRACE_USTACK_STRSIZE(arg
);
6261 uint64_t *pcs
= &buf
[1], *fps
;
6262 char *str
= (char *)&pcs
[nframes
];
6263 int size
, offs
= 0, i
, j
;
6264 uintptr_t old
= mstate
->dtms_scratch_ptr
, saved
;
6265 uint16_t *flags
= &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
6269 * Should be taking a faster path if string space has not been
6272 ASSERT(strsize
!= 0);
6275 * We will first allocate some temporary space for the frame pointers.
6277 fps
= (uint64_t *)P2ROUNDUP(mstate
->dtms_scratch_ptr
, 8);
6278 size
= (uintptr_t)fps
- mstate
->dtms_scratch_ptr
+
6279 (nframes
* sizeof (uint64_t));
6281 #if !defined(__APPLE__) /* Quiet compiler warnings */
6282 if (!DTRACE_INSCRATCH(mstate
, size
)) {
6284 if (!DTRACE_INSCRATCH(mstate
, (uintptr_t)size
)) {
6285 #endif /* __APPLE__ */
6287 * Not enough room for our frame pointers -- need to indicate
6288 * that we ran out of scratch space.
6290 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
6294 mstate
->dtms_scratch_ptr
+= size
;
6295 saved
= mstate
->dtms_scratch_ptr
;
6298 * Now get a stack with both program counters and frame pointers.
6300 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
6301 dtrace_getufpstack(buf
, fps
, nframes
+ 1);
6302 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
6305 * If that faulted, we're cooked.
6307 if (*flags
& CPU_DTRACE_FAULT
)
6311 * Now we want to walk up the stack, calling the USTACK helper. For
6312 * each iteration, we restore the scratch pointer.
6314 for (i
= 0; i
< nframes
; i
++) {
6315 mstate
->dtms_scratch_ptr
= saved
;
6317 if (offs
>= strsize
)
6320 sym
= (char *)(uintptr_t)dtrace_helper(
6321 DTRACE_HELPER_ACTION_USTACK
,
6322 mstate
, state
, pcs
[i
], fps
[i
]);
6325 * If we faulted while running the helper, we're going to
6326 * clear the fault and null out the corresponding string.
6328 if (*flags
& CPU_DTRACE_FAULT
) {
6329 *flags
&= ~CPU_DTRACE_FAULT
;
6339 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
6342 * Now copy in the string that the helper returned to us.
6344 for (j
= 0; offs
+ j
< strsize
; j
++) {
6345 if ((str
[offs
+ j
] = sym
[j
]) == '\0')
6349 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
6354 if (offs
>= strsize
) {
6356 * If we didn't have room for all of the strings, we don't
6357 * abort processing -- this needn't be a fatal error -- but we
6358 * still want to increment a counter (dts_stkstroverflows) to
6359 * allow this condition to be warned about. (If this is from
6360 * a jstack() action, it is easily tuned via jstackstrsize.)
6362 dtrace_error(&state
->dts_stkstroverflows
);
6365 while (offs
< strsize
)
6369 mstate
->dtms_scratch_ptr
= old
;
6373 * If you're looking for the epicenter of DTrace, you just found it. This
6374 * is the function called by the provider to fire a probe -- from which all
6375 * subsequent probe-context DTrace activity emanates.
6377 #if !defined(__APPLE__)
6379 dtrace_probe(dtrace_id_t id
, uintptr_t arg0
, uintptr_t arg1
,
6380 uintptr_t arg2
, uintptr_t arg3
, uintptr_t arg4
)
6383 __dtrace_probe(dtrace_id_t id
, uint64_t arg0
, uint64_t arg1
,
6384 uint64_t arg2
, uint64_t arg3
, uint64_t arg4
)
6385 #endif /* __APPLE__ */
6387 processorid_t cpuid
;
6388 dtrace_icookie_t cookie
;
6389 dtrace_probe_t
*probe
;
6390 dtrace_mstate_t mstate
;
6392 dtrace_action_t
*act
;
6396 volatile uint16_t *flags
;
6399 #if !defined(__APPLE__)
6401 * Kick out immediately if this CPU is still being born (in which case
6402 * curthread will be set to -1) or the current thread can't allow
6403 * probes in its current context.
6405 if (((uintptr_t)curthread
& 1) || (curthread
->t_flag
& T_DONTDTRACE
))
6408 /* Not a concern for Darwin */
6409 #endif /* __APPLE__ */
6411 cookie
= dtrace_interrupt_disable();
6412 probe
= dtrace_probes
[id
- 1];
6413 cpuid
= CPU
->cpu_id
;
6414 onintr
= CPU_ON_INTR(CPU
);
6416 #if !defined(__APPLE__)
6417 if (!onintr
&& probe
->dtpr_predcache
!= DTRACE_CACHEIDNONE
&&
6418 probe
->dtpr_predcache
== curthread
->t_predcache
) {
6420 if (!onintr
&& probe
->dtpr_predcache
!= DTRACE_CACHEIDNONE
&&
6421 probe
->dtpr_predcache
== dtrace_get_thread_predcache(current_thread())) {
6422 #endif /* __APPLE__ */
6424 * We have hit in the predicate cache; we know that
6425 * this predicate would evaluate to be false.
6427 dtrace_interrupt_enable(cookie
);
6431 if (panic_quiesce
) {
6433 * We don't trace anything if we're panicking.
6435 dtrace_interrupt_enable(cookie
);
6439 #if !defined(__APPLE__)
6440 now
= dtrace_gethrtime();
6441 vtime
= dtrace_vtime_references
!= 0;
6443 if (vtime
&& curthread
->t_dtrace_start
)
6444 curthread
->t_dtrace_vtime
+= now
- curthread
->t_dtrace_start
;
6446 /* FIXME: the time spent entering DTrace and arriving to this point is attributed
6447 to the current thread. Instead it should accrue to DTrace. */
6448 vtime
= dtrace_vtime_references
!= 0;
6452 int64_t dtrace_accum_time
, recent_vtime
;
6453 thread_t thread
= current_thread();
6455 dtrace_accum_time
= dtrace_get_thread_tracing(thread
); /* Time spent inside DTrace so far (nanoseconds) */
6457 if (dtrace_accum_time
>= 0) {
6458 recent_vtime
= dtrace_abs_to_nano(dtrace_calc_thread_recent_vtime(thread
)); /* up to the moment thread vtime */
6460 recent_vtime
= recent_vtime
- dtrace_accum_time
; /* Time without DTrace contribution */
6462 dtrace_set_thread_vtime(thread
, recent_vtime
);
6466 now
= dtrace_gethrtime(); /* must not precede dtrace_calc_thread_recent_vtime() call! */
6467 #endif /* __APPLE__ */
6469 #if defined(__APPLE__)
6471 * A provider may call dtrace_probe_error() in lieu of dtrace_probe() in some circumstances.
6472 * See, e.g. fasttrap_isa.c. However the provider has no access to ECB context, so passes
6473 * 0 through "arg0" and the probe_id of the overridden probe as arg1. Detect that here
6474 * and cons up a viable state (from the probe_id).
6476 if (dtrace_probeid_error
== id
&& 0 == arg0
) {
6477 dtrace_id_t ftp_id
= (dtrace_id_t
)arg1
;
6478 dtrace_probe_t
*ftp_probe
= dtrace_probes
[ftp_id
- 1];
6479 dtrace_ecb_t
*ftp_ecb
= ftp_probe
->dtpr_ecb
;
6481 if (NULL
!= ftp_ecb
) {
6482 dtrace_state_t
*ftp_state
= ftp_ecb
->dte_state
;
6484 arg0
= (uint64_t)(uintptr_t)ftp_state
;
6485 arg1
= ftp_ecb
->dte_epid
;
6487 * args[2-4] established by caller.
6489 ftp_state
->dts_arg_error_illval
= -1; /* arg5 */
6492 #endif /* __APPLE__ */
6494 mstate
.dtms_difo
= NULL
;
6495 mstate
.dtms_probe
= probe
;
6496 mstate
.dtms_strtok
= NULL
;
6497 mstate
.dtms_arg
[0] = arg0
;
6498 mstate
.dtms_arg
[1] = arg1
;
6499 mstate
.dtms_arg
[2] = arg2
;
6500 mstate
.dtms_arg
[3] = arg3
;
6501 mstate
.dtms_arg
[4] = arg4
;
6503 flags
= (volatile uint16_t *)&cpu_core
[cpuid
].cpuc_dtrace_flags
;
6505 for (ecb
= probe
->dtpr_ecb
; ecb
!= NULL
; ecb
= ecb
->dte_next
) {
6506 dtrace_predicate_t
*pred
= ecb
->dte_predicate
;
6507 dtrace_state_t
*state
= ecb
->dte_state
;
6508 dtrace_buffer_t
*buf
= &state
->dts_buffer
[cpuid
];
6509 dtrace_buffer_t
*aggbuf
= &state
->dts_aggbuffer
[cpuid
];
6510 dtrace_vstate_t
*vstate
= &state
->dts_vstate
;
6511 dtrace_provider_t
*prov
= probe
->dtpr_provider
;
6516 * A little subtlety with the following (seemingly innocuous)
6517 * declaration of the automatic 'val': by looking at the
6518 * code, you might think that it could be declared in the
6519 * action processing loop, below. (That is, it's only used in
6520 * the action processing loop.) However, it must be declared
6521 * out of that scope because in the case of DIF expression
6522 * arguments to aggregating actions, one iteration of the
6523 * action loop will use the last iteration's value.
6531 mstate
.dtms_present
= DTRACE_MSTATE_ARGS
| DTRACE_MSTATE_PROBE
;
6532 *flags
&= ~CPU_DTRACE_ERROR
;
6534 if (prov
== dtrace_provider
) {
6536 * If dtrace itself is the provider of this probe,
6537 * we're only going to continue processing the ECB if
6538 * arg0 (the dtrace_state_t) is equal to the ECB's
6539 * creating state. (This prevents disjoint consumers
6540 * from seeing one another's metaprobes.)
6542 if (arg0
!= (uint64_t)(uintptr_t)state
)
6546 if (state
->dts_activity
!= DTRACE_ACTIVITY_ACTIVE
) {
6548 * We're not currently active. If our provider isn't
6549 * the dtrace pseudo provider, we're not interested.
6551 if (prov
!= dtrace_provider
)
6555 * Now we must further check if we are in the BEGIN
6556 * probe. If we are, we will only continue processing
6557 * if we're still in WARMUP -- if one BEGIN enabling
6558 * has invoked the exit() action, we don't want to
6559 * evaluate subsequent BEGIN enablings.
6561 if (probe
->dtpr_id
== dtrace_probeid_begin
&&
6562 state
->dts_activity
!= DTRACE_ACTIVITY_WARMUP
) {
6563 ASSERT(state
->dts_activity
==
6564 DTRACE_ACTIVITY_DRAINING
);
6569 if (ecb
->dte_cond
) {
6571 * If the dte_cond bits indicate that this
6572 * consumer is only allowed to see user-mode firings
6573 * of this probe, call the provider's dtps_usermode()
6574 * entry point to check that the probe was fired
6575 * while in a user context. Skip this ECB if that's
6578 if ((ecb
->dte_cond
& DTRACE_COND_USERMODE
) &&
6579 prov
->dtpv_pops
.dtps_usermode(prov
->dtpv_arg
,
6580 probe
->dtpr_id
, probe
->dtpr_arg
) == 0)
6584 * This is more subtle than it looks. We have to be
6585 * absolutely certain that CRED() isn't going to
6586 * change out from under us so it's only legit to
6587 * examine that structure if we're in constrained
6588 * situations. Currently, the only times we'll this
6589 * check is if a non-super-user has enabled the
6590 * profile or syscall providers -- providers that
6591 * allow visibility of all processes. For the
6592 * profile case, the check above will ensure that
6593 * we're examining a user context.
6595 if (ecb
->dte_cond
& DTRACE_COND_OWNER
) {
6598 ecb
->dte_state
->dts_cred
.dcr_cred
;
6600 #pragma unused(proc) /* __APPLE__ */
6602 ASSERT(s_cr
!= NULL
);
6604 #if !defined(__APPLE__)
6605 if ((cr
= CRED()) == NULL
||
6607 if ((cr
= dtrace_CRED()) == NULL
||
6608 #endif /* __APPLE__ */
6609 s_cr
->cr_uid
!= cr
->cr_uid
||
6610 s_cr
->cr_uid
!= cr
->cr_ruid
||
6611 s_cr
->cr_uid
!= cr
->cr_suid
||
6612 s_cr
->cr_gid
!= cr
->cr_gid
||
6613 s_cr
->cr_gid
!= cr
->cr_rgid
||
6614 s_cr
->cr_gid
!= cr
->cr_sgid
||
6615 #if !defined(__APPLE__)
6616 (proc
= ttoproc(curthread
)) == NULL
||
6617 (proc
->p_flag
& SNOCD
))
6619 1) /* Darwin omits "No Core Dump" flag. */
6620 #endif /* __APPLE__ */
6624 if (ecb
->dte_cond
& DTRACE_COND_ZONEOWNER
) {
6627 ecb
->dte_state
->dts_cred
.dcr_cred
;
6628 #pragma unused(cr, s_cr) /* __APPLE__ */
6630 ASSERT(s_cr
!= NULL
);
6632 #if !defined(__APPLE__)
6633 if ((cr
= CRED()) == NULL
||
6634 s_cr
->cr_zone
->zone_id
!=
6635 cr
->cr_zone
->zone_id
)
6638 /* Darwin doesn't do zones. */
6639 #endif /* __APPLE__ */
6643 if (now
- state
->dts_alive
> dtrace_deadman_timeout
) {
6645 * We seem to be dead. Unless we (a) have kernel
6646 * destructive permissions (b) have expicitly enabled
6647 * destructive actions and (c) destructive actions have
6648 * not been disabled, we're going to transition into
6649 * the KILLED state, from which no further processing
6650 * on this state will be performed.
6652 if (!dtrace_priv_kernel_destructive(state
) ||
6653 !state
->dts_cred
.dcr_destructive
||
6654 dtrace_destructive_disallow
) {
6655 void *activity
= &state
->dts_activity
;
6656 dtrace_activity_t current
;
6659 current
= state
->dts_activity
;
6660 } while (dtrace_cas32(activity
, current
,
6661 DTRACE_ACTIVITY_KILLED
) != current
);
6667 if ((offs
= dtrace_buffer_reserve(buf
, ecb
->dte_needed
,
6668 ecb
->dte_alignment
, state
, &mstate
)) < 0)
6671 tomax
= buf
->dtb_tomax
;
6672 ASSERT(tomax
!= NULL
);
6674 if (ecb
->dte_size
!= 0)
6675 DTRACE_STORE(uint32_t, tomax
, offs
, ecb
->dte_epid
);
6677 mstate
.dtms_epid
= ecb
->dte_epid
;
6678 mstate
.dtms_present
|= DTRACE_MSTATE_EPID
;
6680 if (state
->dts_cred
.dcr_visible
& DTRACE_CRV_KERNEL
)
6681 mstate
.dtms_access
= DTRACE_ACCESS_KERNEL
;
6683 mstate
.dtms_access
= 0;
6686 dtrace_difo_t
*dp
= pred
->dtp_difo
;
6689 rval
= dtrace_dif_emulate(dp
, &mstate
, vstate
, state
);
6691 if (!(*flags
& CPU_DTRACE_ERROR
) && !rval
) {
6692 dtrace_cacheid_t cid
= probe
->dtpr_predcache
;
6694 if (cid
!= DTRACE_CACHEIDNONE
&& !onintr
) {
6696 * Update the predicate cache...
6698 ASSERT(cid
== pred
->dtp_cacheid
);
6699 #if !defined(__APPLE__)
6700 curthread
->t_predcache
= cid
;
6702 dtrace_set_thread_predcache(current_thread(), cid
);
6703 #endif /* __APPLE__ */
6710 for (act
= ecb
->dte_action
; !(*flags
& CPU_DTRACE_ERROR
) &&
6711 act
!= NULL
; act
= act
->dta_next
) {
6714 dtrace_recdesc_t
*rec
= &act
->dta_rec
;
6716 size
= rec
->dtrd_size
;
6717 valoffs
= offs
+ rec
->dtrd_offset
;
6719 if (DTRACEACT_ISAGG(act
->dta_kind
)) {
6721 dtrace_aggregation_t
*agg
;
6723 agg
= (dtrace_aggregation_t
*)act
;
6725 if ((dp
= act
->dta_difo
) != NULL
)
6726 v
= dtrace_dif_emulate(dp
,
6727 &mstate
, vstate
, state
);
6729 if (*flags
& CPU_DTRACE_ERROR
)
6733 * Note that we always pass the expression
6734 * value from the previous iteration of the
6735 * action loop. This value will only be used
6736 * if there is an expression argument to the
6737 * aggregating action, denoted by the
6738 * dtag_hasarg field.
6740 dtrace_aggregate(agg
, buf
,
6741 offs
, aggbuf
, v
, val
);
6745 switch (act
->dta_kind
) {
6746 case DTRACEACT_STOP
:
6747 if (dtrace_priv_proc_destructive(state
))
6748 dtrace_action_stop();
6751 case DTRACEACT_BREAKPOINT
:
6752 if (dtrace_priv_kernel_destructive(state
))
6753 dtrace_action_breakpoint(ecb
);
6756 case DTRACEACT_PANIC
:
6757 if (dtrace_priv_kernel_destructive(state
))
6758 dtrace_action_panic(ecb
);
6761 case DTRACEACT_STACK
:
6762 if (!dtrace_priv_kernel(state
))
6765 #if !defined(__APPLE__) /* Quiet compiler warnings */
6766 dtrace_getpcstack((pc_t
*)(tomax
+ valoffs
),
6767 size
/ sizeof (pc_t
), probe
->dtpr_aframes
,
6768 DTRACE_ANCHORED(probe
) ? NULL
:
6771 dtrace_getpcstack((pc_t
*)(tomax
+ valoffs
),
6772 size
/ sizeof (pc_t
), probe
->dtpr_aframes
,
6773 DTRACE_ANCHORED(probe
) ? NULL
:
6774 (uint32_t *)(uintptr_t)arg0
);
6775 #endif /* __APPLE__ */
6779 case DTRACEACT_JSTACK
:
6780 case DTRACEACT_USTACK
:
6781 if (!dtrace_priv_proc(state
))
6785 * See comment in DIF_VAR_PID.
6787 if (DTRACE_ANCHORED(mstate
.dtms_probe
) &&
6789 int depth
= DTRACE_USTACK_NFRAMES(
6792 dtrace_bzero((void *)(tomax
+ valoffs
),
6793 DTRACE_USTACK_STRSIZE(rec
->dtrd_arg
)
6794 + depth
* sizeof (uint64_t));
6799 if (DTRACE_USTACK_STRSIZE(rec
->dtrd_arg
) != 0 &&
6800 curproc
->p_dtrace_helpers
!= NULL
) {
6802 * This is the slow path -- we have
6803 * allocated string space, and we're
6804 * getting the stack of a process that
6805 * has helpers. Call into a separate
6806 * routine to perform this processing.
6808 dtrace_action_ustack(&mstate
, state
,
6809 (uint64_t *)(tomax
+ valoffs
),
6814 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
6815 dtrace_getupcstack((uint64_t *)
6817 DTRACE_USTACK_NFRAMES(rec
->dtrd_arg
) + 1);
6818 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
6828 val
= dtrace_dif_emulate(dp
, &mstate
, vstate
, state
);
6830 if (*flags
& CPU_DTRACE_ERROR
)
6833 switch (act
->dta_kind
) {
6834 case DTRACEACT_SPECULATE
:
6835 ASSERT(buf
== &state
->dts_buffer
[cpuid
]);
6836 buf
= dtrace_speculation_buffer(state
,
6840 *flags
|= CPU_DTRACE_DROP
;
6844 offs
= dtrace_buffer_reserve(buf
,
6845 ecb
->dte_needed
, ecb
->dte_alignment
,
6849 *flags
|= CPU_DTRACE_DROP
;
6853 tomax
= buf
->dtb_tomax
;
6854 ASSERT(tomax
!= NULL
);
6856 if (ecb
->dte_size
!= 0)
6857 DTRACE_STORE(uint32_t, tomax
, offs
,
6861 case DTRACEACT_CHILL
:
6862 if (dtrace_priv_kernel_destructive(state
))
6863 dtrace_action_chill(&mstate
, val
);
6866 case DTRACEACT_RAISE
:
6867 if (dtrace_priv_proc_destructive(state
))
6868 dtrace_action_raise(val
);
6871 case DTRACEACT_COMMIT
:
6875 * We need to commit our buffer state.
6878 buf
->dtb_offset
= offs
+ ecb
->dte_size
;
6879 buf
= &state
->dts_buffer
[cpuid
];
6880 dtrace_speculation_commit(state
, cpuid
, val
);
6884 case DTRACEACT_DISCARD
:
6885 dtrace_speculation_discard(state
, cpuid
, val
);
6888 case DTRACEACT_DIFEXPR
:
6889 case DTRACEACT_LIBACT
:
6890 case DTRACEACT_PRINTF
:
6891 case DTRACEACT_PRINTA
:
6892 case DTRACEACT_SYSTEM
:
6893 case DTRACEACT_FREOPEN
:
6894 #if defined(__APPLE__)
6895 case DTRACEACT_APPLEBINARY
:
6896 #endif /* __APPLE__ */
6901 if (!dtrace_priv_kernel(state
))
6905 #if !defined(__APPLE__)
6906 case DTRACEACT_USYM
:
6907 case DTRACEACT_UMOD
:
6908 case DTRACEACT_UADDR
: {
6909 struct pid
*pid
= curthread
->t_procp
->p_pidp
;
6911 if (!dtrace_priv_proc(state
))
6914 DTRACE_STORE(uint64_t, tomax
,
6915 valoffs
, (uint64_t)pid
->pid_id
);
6916 DTRACE_STORE(uint64_t, tomax
,
6917 valoffs
+ sizeof (uint64_t), val
);
6922 case DTRACEACT_USYM
:
6923 case DTRACEACT_UMOD
:
6924 case DTRACEACT_UADDR
: {
6925 if (!dtrace_priv_proc(state
))
6928 DTRACE_STORE(uint64_t, tomax
,
6929 valoffs
, (uint64_t)proc_selfpid());
6930 DTRACE_STORE(uint64_t, tomax
,
6931 valoffs
+ sizeof (uint64_t), val
);
6935 #endif /* __APPLE__ */
6937 case DTRACEACT_EXIT
: {
6939 * For the exit action, we are going to attempt
6940 * to atomically set our activity to be
6941 * draining. If this fails (either because
6942 * another CPU has beat us to the exit action,
6943 * or because our current activity is something
6944 * other than ACTIVE or WARMUP), we will
6945 * continue. This assures that the exit action
6946 * can be successfully recorded at most once
6947 * when we're in the ACTIVE state. If we're
6948 * encountering the exit() action while in
6949 * COOLDOWN, however, we want to honor the new
6950 * status code. (We know that we're the only
6951 * thread in COOLDOWN, so there is no race.)
6953 void *activity
= &state
->dts_activity
;
6954 dtrace_activity_t current
= state
->dts_activity
;
6956 if (current
== DTRACE_ACTIVITY_COOLDOWN
)
6959 if (current
!= DTRACE_ACTIVITY_WARMUP
)
6960 current
= DTRACE_ACTIVITY_ACTIVE
;
6962 if (dtrace_cas32(activity
, current
,
6963 DTRACE_ACTIVITY_DRAINING
) != current
) {
6964 *flags
|= CPU_DTRACE_DROP
;
6975 if (dp
->dtdo_rtype
.dtdt_flags
& DIF_TF_BYREF
) {
6976 uintptr_t end
= valoffs
+ size
;
6978 if (!dtrace_vcanload((void *)(uintptr_t)val
,
6979 &dp
->dtdo_rtype
, &mstate
, vstate
))
6983 * If this is a string, we're going to only
6984 * load until we find the zero byte -- after
6985 * which we'll store zero bytes.
6987 if (dp
->dtdo_rtype
.dtdt_kind
==
6990 int intuple
= act
->dta_intuple
;
6993 for (s
= 0; s
< size
; s
++) {
6995 c
= dtrace_load8(val
++);
6997 DTRACE_STORE(uint8_t, tomax
,
7000 if (c
== '\0' && intuple
)
7007 while (valoffs
< end
) {
7008 DTRACE_STORE(uint8_t, tomax
, valoffs
++,
7009 dtrace_load8(val
++));
7019 case sizeof (uint8_t):
7020 DTRACE_STORE(uint8_t, tomax
, valoffs
, val
);
7022 case sizeof (uint16_t):
7023 DTRACE_STORE(uint16_t, tomax
, valoffs
, val
);
7025 case sizeof (uint32_t):
7026 DTRACE_STORE(uint32_t, tomax
, valoffs
, val
);
7028 case sizeof (uint64_t):
7029 DTRACE_STORE(uint64_t, tomax
, valoffs
, val
);
7033 * Any other size should have been returned by
7034 * reference, not by value.
7041 if (*flags
& CPU_DTRACE_DROP
)
7044 if (*flags
& CPU_DTRACE_FAULT
) {
7046 dtrace_action_t
*err
;
7050 if (probe
->dtpr_id
== dtrace_probeid_error
) {
7052 * There's nothing we can do -- we had an
7053 * error on the error probe. We bump an
7054 * error counter to at least indicate that
7055 * this condition happened.
7057 dtrace_error(&state
->dts_dblerrors
);
7063 * Before recursing on dtrace_probe(), we
7064 * need to explicitly clear out our start
7065 * time to prevent it from being accumulated
7066 * into t_dtrace_vtime.
7068 #if !defined(__APPLE__)
7069 curthread
->t_dtrace_start
= 0;
7071 /* Set the sign bit on t_dtrace_tracing to suspend accumulation to it. */
7072 dtrace_set_thread_tracing(current_thread(),
7073 (1ULL<<63) | dtrace_get_thread_tracing(current_thread()));
7074 #endif /* __APPLE__ */
7078 * Iterate over the actions to figure out which action
7079 * we were processing when we experienced the error.
7080 * Note that act points _past_ the faulting action; if
7081 * act is ecb->dte_action, the fault was in the
7082 * predicate, if it's ecb->dte_action->dta_next it's
7083 * in action #1, and so on.
7085 for (err
= ecb
->dte_action
, ndx
= 0;
7086 err
!= act
; err
= err
->dta_next
, ndx
++)
7089 dtrace_probe_error(state
, ecb
->dte_epid
, ndx
,
7090 (mstate
.dtms_present
& DTRACE_MSTATE_FLTOFFS
) ?
7091 mstate
.dtms_fltoffs
: -1, DTRACE_FLAGS2FLT(*flags
),
7092 cpu_core
[cpuid
].cpuc_dtrace_illval
);
7098 buf
->dtb_offset
= offs
+ ecb
->dte_size
;
7101 #if !defined(__APPLE__)
7103 curthread
->t_dtrace_start
= dtrace_gethrtime();
7105 /* FIXME: the time spent leaving DTrace from this point to the rti is attributed
7106 to the current thread. Instead it should accrue to DTrace. */
7108 thread_t thread
= current_thread();
7109 int64_t t
= dtrace_get_thread_tracing(thread
);
7112 /* Usual case, accumulate time spent here into t_dtrace_tracing */
7113 dtrace_set_thread_tracing(thread
, t
+ (dtrace_gethrtime() - now
));
7115 /* Return from error recursion. No accumulation, just clear the sign bit on t_dtrace_tracing. */
7116 dtrace_set_thread_tracing(thread
, (~(1ULL<<63)) & t
);
7119 #endif /* __APPLE__ */
7121 dtrace_interrupt_enable(cookie
);
7124 #if defined(__APPLE__)
7125 /* Don't allow a thread to re-enter dtrace_probe(). This could occur if a probe is encountered
7126 on some function in the transitive closure of the call to dtrace_probe(). Solaris has some
7127 strong guarantees that this won't happen, the Darwin implementation is not so mature as to
7128 make those guarantees. */
7130 dtrace_probe(dtrace_id_t id
, uint64_t arg0
, uint64_t arg1
,
7131 uint64_t arg2
, uint64_t arg3
, uint64_t arg4
)
7133 thread_t thread
= current_thread();
7135 if (id
== dtrace_probeid_error
) {
7136 __dtrace_probe(id
, arg0
, arg1
, arg2
, arg3
, arg4
);
7137 dtrace_getipl(); /* Defeat tail-call optimization of __dtrace_probe() */
7138 } else if (!dtrace_get_thread_reentering(thread
)) {
7139 dtrace_set_thread_reentering(thread
, TRUE
);
7140 __dtrace_probe(id
, arg0
, arg1
, arg2
, arg3
, arg4
);
7141 dtrace_set_thread_reentering(thread
, FALSE
);
7144 else __dtrace_probe(dtrace_probeid_error
, 0, id
, 1, -1, DTRACEFLT_UNKNOWN
);
7147 #endif /* __APPLE__ */
7150 * DTrace Probe Hashing Functions
7152 * The functions in this section (and indeed, the functions in remaining
7153 * sections) are not _called_ from probe context. (Any exceptions to this are
7154 * marked with a "Note:".) Rather, they are called from elsewhere in the
7155 * DTrace framework to look-up probes in, add probes to and remove probes from
7156 * the DTrace probe hashes. (Each probe is hashed by each element of the
7157 * probe tuple -- allowing for fast lookups, regardless of what was
7161 #if !defined(__APPLE__) /* Quiet compiler warnings */
7162 dtrace_hash_str(char *p
)
7164 dtrace_hash_str(const char *p
)
7165 #endif /* __APPLE__ */
7171 hval
= (hval
<< 4) + *p
++;
7172 if ((g
= (hval
& 0xf0000000)) != 0)
7179 static dtrace_hash_t
*
7180 dtrace_hash_create(uintptr_t stroffs
, uintptr_t nextoffs
, uintptr_t prevoffs
)
7182 dtrace_hash_t
*hash
= kmem_zalloc(sizeof (dtrace_hash_t
), KM_SLEEP
);
7184 hash
->dth_stroffs
= stroffs
;
7185 hash
->dth_nextoffs
= nextoffs
;
7186 hash
->dth_prevoffs
= prevoffs
;
7189 hash
->dth_mask
= hash
->dth_size
- 1;
7191 hash
->dth_tab
= kmem_zalloc(hash
->dth_size
*
7192 sizeof (dtrace_hashbucket_t
*), KM_SLEEP
);
7197 #if !defined(__APPLE__) /* Unused. Quiet compiler warning. */
7199 dtrace_hash_destroy(dtrace_hash_t
*hash
)
7204 for (i
= 0; i
< hash
->dth_size
; i
++)
7205 ASSERT(hash
->dth_tab
[i
] == NULL
);
7208 kmem_free(hash
->dth_tab
,
7209 hash
->dth_size
* sizeof (dtrace_hashbucket_t
*));
7210 kmem_free(hash
, sizeof (dtrace_hash_t
));
7212 #endif /* __APPLE__ */
7215 dtrace_hash_resize(dtrace_hash_t
*hash
)
7217 int size
= hash
->dth_size
, i
, ndx
;
7218 int new_size
= hash
->dth_size
<< 1;
7219 int new_mask
= new_size
- 1;
7220 dtrace_hashbucket_t
**new_tab
, *bucket
, *next
;
7222 ASSERT((new_size
& new_mask
) == 0);
7224 new_tab
= kmem_zalloc(new_size
* sizeof (void *), KM_SLEEP
);
7226 for (i
= 0; i
< size
; i
++) {
7227 for (bucket
= hash
->dth_tab
[i
]; bucket
!= NULL
; bucket
= next
) {
7228 dtrace_probe_t
*probe
= bucket
->dthb_chain
;
7230 ASSERT(probe
!= NULL
);
7231 ndx
= DTRACE_HASHSTR(hash
, probe
) & new_mask
;
7233 next
= bucket
->dthb_next
;
7234 bucket
->dthb_next
= new_tab
[ndx
];
7235 new_tab
[ndx
] = bucket
;
7239 kmem_free(hash
->dth_tab
, hash
->dth_size
* sizeof (void *));
7240 hash
->dth_tab
= new_tab
;
7241 hash
->dth_size
= new_size
;
7242 hash
->dth_mask
= new_mask
;
7246 dtrace_hash_add(dtrace_hash_t
*hash
, dtrace_probe_t
*new)
7248 int hashval
= DTRACE_HASHSTR(hash
, new);
7249 int ndx
= hashval
& hash
->dth_mask
;
7250 dtrace_hashbucket_t
*bucket
= hash
->dth_tab
[ndx
];
7251 dtrace_probe_t
**nextp
, **prevp
;
7253 for (; bucket
!= NULL
; bucket
= bucket
->dthb_next
) {
7254 if (DTRACE_HASHEQ(hash
, bucket
->dthb_chain
, new))
7258 if ((hash
->dth_nbuckets
>> 1) > hash
->dth_size
) {
7259 dtrace_hash_resize(hash
);
7260 dtrace_hash_add(hash
, new);
7264 bucket
= kmem_zalloc(sizeof (dtrace_hashbucket_t
), KM_SLEEP
);
7265 bucket
->dthb_next
= hash
->dth_tab
[ndx
];
7266 hash
->dth_tab
[ndx
] = bucket
;
7267 hash
->dth_nbuckets
++;
7270 nextp
= DTRACE_HASHNEXT(hash
, new);
7271 ASSERT(*nextp
== NULL
&& *(DTRACE_HASHPREV(hash
, new)) == NULL
);
7272 *nextp
= bucket
->dthb_chain
;
7274 if (bucket
->dthb_chain
!= NULL
) {
7275 prevp
= DTRACE_HASHPREV(hash
, bucket
->dthb_chain
);
7276 ASSERT(*prevp
== NULL
);
7280 bucket
->dthb_chain
= new;
7284 static dtrace_probe_t
*
7285 dtrace_hash_lookup(dtrace_hash_t
*hash
, dtrace_probe_t
*template)
7287 int hashval
= DTRACE_HASHSTR(hash
, template);
7288 int ndx
= hashval
& hash
->dth_mask
;
7289 dtrace_hashbucket_t
*bucket
= hash
->dth_tab
[ndx
];
7291 for (; bucket
!= NULL
; bucket
= bucket
->dthb_next
) {
7292 if (DTRACE_HASHEQ(hash
, bucket
->dthb_chain
, template))
7293 return (bucket
->dthb_chain
);
7300 dtrace_hash_collisions(dtrace_hash_t
*hash
, dtrace_probe_t
*template)
7302 int hashval
= DTRACE_HASHSTR(hash
, template);
7303 int ndx
= hashval
& hash
->dth_mask
;
7304 dtrace_hashbucket_t
*bucket
= hash
->dth_tab
[ndx
];
7306 for (; bucket
!= NULL
; bucket
= bucket
->dthb_next
) {
7307 if (DTRACE_HASHEQ(hash
, bucket
->dthb_chain
, template))
7308 return (bucket
->dthb_len
);
7315 dtrace_hash_remove(dtrace_hash_t
*hash
, dtrace_probe_t
*probe
)
7317 int ndx
= DTRACE_HASHSTR(hash
, probe
) & hash
->dth_mask
;
7318 dtrace_hashbucket_t
*bucket
= hash
->dth_tab
[ndx
];
7320 dtrace_probe_t
**prevp
= DTRACE_HASHPREV(hash
, probe
);
7321 dtrace_probe_t
**nextp
= DTRACE_HASHNEXT(hash
, probe
);
7324 * Find the bucket that we're removing this probe from.
7326 for (; bucket
!= NULL
; bucket
= bucket
->dthb_next
) {
7327 if (DTRACE_HASHEQ(hash
, bucket
->dthb_chain
, probe
))
7331 ASSERT(bucket
!= NULL
);
7333 if (*prevp
== NULL
) {
7334 if (*nextp
== NULL
) {
7336 * The removed probe was the only probe on this
7337 * bucket; we need to remove the bucket.
7339 dtrace_hashbucket_t
*b
= hash
->dth_tab
[ndx
];
7341 ASSERT(bucket
->dthb_chain
== probe
);
7345 hash
->dth_tab
[ndx
] = bucket
->dthb_next
;
7347 while (b
->dthb_next
!= bucket
)
7349 b
->dthb_next
= bucket
->dthb_next
;
7352 ASSERT(hash
->dth_nbuckets
> 0);
7353 hash
->dth_nbuckets
--;
7354 kmem_free(bucket
, sizeof (dtrace_hashbucket_t
));
7358 bucket
->dthb_chain
= *nextp
;
7360 *(DTRACE_HASHNEXT(hash
, *prevp
)) = *nextp
;
7364 *(DTRACE_HASHPREV(hash
, *nextp
)) = *prevp
;
7368 * DTrace Utility Functions
7370 * These are random utility functions that are _not_ called from probe context.
7373 dtrace_badattr(const dtrace_attribute_t
*a
)
7375 return (a
->dtat_name
> DTRACE_STABILITY_MAX
||
7376 a
->dtat_data
> DTRACE_STABILITY_MAX
||
7377 a
->dtat_class
> DTRACE_CLASS_MAX
);
7381 * Return a duplicate copy of a string. If the specified string is NULL,
7382 * this function returns a zero-length string.
7384 #if !defined(__APPLE__)
7386 dtrace_strdup(const char *str
)
7388 char *new = kmem_zalloc((str
!= NULL
? strlen(str
) : 0) + 1, KM_SLEEP
);
7391 (void) strcpy(new, str
);
7395 #else /* Employ size bounded string operation. */
7397 dtrace_strdup(const char *str
)
7399 size_t bufsize
= (str
!= NULL
? strlen(str
) : 0) + 1;
7400 char *new = kmem_zalloc(bufsize
, KM_SLEEP
);
7403 (void) strlcpy(new, str
, bufsize
);
7407 #endif /* __APPLE__ */
7409 #define DTRACE_ISALPHA(c) \
7410 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
7413 dtrace_badname(const char *s
)
7417 if (s
== NULL
|| (c
= *s
++) == '\0')
7420 if (!DTRACE_ISALPHA(c
) && c
!= '-' && c
!= '_' && c
!= '.')
7423 while ((c
= *s
++) != '\0') {
7424 if (!DTRACE_ISALPHA(c
) && (c
< '0' || c
> '9') &&
7425 c
!= '-' && c
!= '_' && c
!= '.' && c
!= '`')
7433 dtrace_cred2priv(cred_t
*cr
, uint32_t *privp
, uid_t
*uidp
, zoneid_t
*zoneidp
)
7437 if (cr
== NULL
|| PRIV_POLICY_ONLY(cr
, PRIV_ALL
, B_FALSE
)) {
7439 * For DTRACE_PRIV_ALL, the uid and zoneid don't matter.
7441 priv
= DTRACE_PRIV_ALL
;
7443 *uidp
= crgetuid(cr
);
7444 *zoneidp
= crgetzoneid(cr
);
7447 if (PRIV_POLICY_ONLY(cr
, PRIV_DTRACE_KERNEL
, B_FALSE
))
7448 priv
|= DTRACE_PRIV_KERNEL
| DTRACE_PRIV_USER
;
7449 else if (PRIV_POLICY_ONLY(cr
, PRIV_DTRACE_USER
, B_FALSE
))
7450 priv
|= DTRACE_PRIV_USER
;
7451 if (PRIV_POLICY_ONLY(cr
, PRIV_DTRACE_PROC
, B_FALSE
))
7452 priv
|= DTRACE_PRIV_PROC
;
7453 if (PRIV_POLICY_ONLY(cr
, PRIV_PROC_OWNER
, B_FALSE
))
7454 priv
|= DTRACE_PRIV_OWNER
;
7455 if (PRIV_POLICY_ONLY(cr
, PRIV_PROC_ZONE
, B_FALSE
))
7456 priv
|= DTRACE_PRIV_ZONEOWNER
;
7462 #ifdef DTRACE_ERRDEBUG
7464 dtrace_errdebug(const char *str
)
7466 #if !defined(__APPLE__) /* Quiet compiler warnings */
7467 int hval
= dtrace_hash_str((char *)str
) % DTRACE_ERRHASHSZ
;
7469 int hval
= dtrace_hash_str(str
) % DTRACE_ERRHASHSZ
;
7470 #endif /* __APPLE__ */
7473 lck_mtx_lock(&dtrace_errlock
);
7474 dtrace_errlast
= str
;
7475 #if !defined(__APPLE__)
7476 dtrace_errthread
= curthread
;
7478 dtrace_errthread
= (kthread_t
*)current_thread();
7479 #endif /* __APPLE__ */
7481 while (occupied
++ < DTRACE_ERRHASHSZ
) {
7482 if (dtrace_errhash
[hval
].dter_msg
== str
) {
7483 dtrace_errhash
[hval
].dter_count
++;
7487 if (dtrace_errhash
[hval
].dter_msg
!= NULL
) {
7488 hval
= (hval
+ 1) % DTRACE_ERRHASHSZ
;
7492 dtrace_errhash
[hval
].dter_msg
= str
;
7493 dtrace_errhash
[hval
].dter_count
= 1;
7497 panic("dtrace: undersized error hash");
7499 lck_mtx_unlock(&dtrace_errlock
);
7504 * DTrace Matching Functions
7506 * These functions are used to match groups of probes, given some elements of
7507 * a probe tuple, or some globbed expressions for elements of a probe tuple.
7510 dtrace_match_priv(const dtrace_probe_t
*prp
, uint32_t priv
, uid_t uid
,
7513 if (priv
!= DTRACE_PRIV_ALL
) {
7514 uint32_t ppriv
= prp
->dtpr_provider
->dtpv_priv
.dtpp_flags
;
7515 uint32_t match
= priv
& ppriv
;
7518 * No PRIV_DTRACE_* privileges...
7520 if ((priv
& (DTRACE_PRIV_PROC
| DTRACE_PRIV_USER
|
7521 DTRACE_PRIV_KERNEL
)) == 0)
7525 * No matching bits, but there were bits to match...
7527 if (match
== 0 && ppriv
!= 0)
7531 * Need to have permissions to the process, but don't...
7533 if (((ppriv
& ~match
) & DTRACE_PRIV_OWNER
) != 0 &&
7534 uid
!= prp
->dtpr_provider
->dtpv_priv
.dtpp_uid
) {
7539 * Need to be in the same zone unless we possess the
7540 * privilege to examine all zones.
7542 if (((ppriv
& ~match
) & DTRACE_PRIV_ZONEOWNER
) != 0 &&
7543 zoneid
!= prp
->dtpr_provider
->dtpv_priv
.dtpp_zoneid
) {
7552 * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which
7553 * consists of input pattern strings and an ops-vector to evaluate them.
7554 * This function returns >0 for match, 0 for no match, and <0 for error.
7557 dtrace_match_probe(const dtrace_probe_t
*prp
, const dtrace_probekey_t
*pkp
,
7558 uint32_t priv
, uid_t uid
, zoneid_t zoneid
)
7560 dtrace_provider_t
*pvp
= prp
->dtpr_provider
;
7563 if (pvp
->dtpv_defunct
)
7566 if ((rv
= pkp
->dtpk_pmatch(pvp
->dtpv_name
, pkp
->dtpk_prov
, 0)) <= 0)
7569 if ((rv
= pkp
->dtpk_mmatch(prp
->dtpr_mod
, pkp
->dtpk_mod
, 0)) <= 0)
7572 if ((rv
= pkp
->dtpk_fmatch(prp
->dtpr_func
, pkp
->dtpk_func
, 0)) <= 0)
7575 if ((rv
= pkp
->dtpk_nmatch(prp
->dtpr_name
, pkp
->dtpk_name
, 0)) <= 0)
7578 if (dtrace_match_priv(prp
, priv
, uid
, zoneid
) == 0)
7585 * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN)
7586 * interface for matching a glob pattern 'p' to an input string 's'. Unlike
7587 * libc's version, the kernel version only applies to 8-bit ASCII strings.
7588 * In addition, all of the recursion cases except for '*' matching have been
7589 * unwound. For '*', we still implement recursive evaluation, but a depth
7590 * counter is maintained and matching is aborted if we recurse too deep.
7591 * The function returns 0 if no match, >0 if match, and <0 if recursion error.
7594 dtrace_match_glob(const char *s
, const char *p
, int depth
)
7600 if (depth
> DTRACE_PROBEKEY_MAXDEPTH
)
7604 s
= ""; /* treat NULL as empty string */
7613 if ((c
= *p
++) == '\0')
7614 return (s1
== '\0');
7618 int ok
= 0, notflag
= 0;
7629 if ((c
= *p
++) == '\0')
7633 if (c
== '-' && lc
!= '\0' && *p
!= ']') {
7634 if ((c
= *p
++) == '\0')
7636 if (c
== '\\' && (c
= *p
++) == '\0')
7640 if (s1
< lc
|| s1
> c
)
7644 } else if (lc
<= s1
&& s1
<= c
)
7647 } else if (c
== '\\' && (c
= *p
++) == '\0')
7650 lc
= c
; /* save left-hand 'c' for next iteration */
7660 if ((c
= *p
++) == '\0')
7672 if ((c
= *p
++) == '\0')
7688 p
++; /* consecutive *'s are identical to a single one */
7693 for (s
= olds
; *s
!= '\0'; s
++) {
7694 if ((gs
= dtrace_match_glob(s
, p
, depth
+ 1)) != 0)
7704 dtrace_match_string(const char *s
, const char *p
, int depth
)
7706 #pragma unused(depth) /* __APPLE__ */
7707 #if !defined(__APPLE__)
7708 return (s
!= NULL
&& strcmp(s
, p
) == 0);
7709 #else /* Employ size bounded string operation. */
7710 return (s
!= NULL
&& strncmp(s
, p
, strlen(s
) + 1) == 0);
7711 #endif /* __APPLE__ */
7716 dtrace_match_nul(const char *s
, const char *p
, int depth
)
7718 #pragma unused(s, p, depth) /* __APPLE__ */
7719 return (1); /* always match the empty pattern */
7724 dtrace_match_nonzero(const char *s
, const char *p
, int depth
)
7726 #pragma unused(p, depth) /* __APPLE__ */
7727 return (s
!= NULL
&& s
[0] != '\0');
7731 dtrace_match(const dtrace_probekey_t
*pkp
, uint32_t priv
, uid_t uid
,
7732 zoneid_t zoneid
, int (*matched
)(dtrace_probe_t
*, void *), void *arg
)
7734 dtrace_probe_t
template, *probe
;
7735 dtrace_hash_t
*hash
= NULL
;
7736 int len
, best
= INT_MAX
, nmatched
= 0;
7739 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
7742 * If the probe ID is specified in the key, just lookup by ID and
7743 * invoke the match callback once if a matching probe is found.
7745 if (pkp
->dtpk_id
!= DTRACE_IDNONE
) {
7746 if ((probe
= dtrace_probe_lookup_id(pkp
->dtpk_id
)) != NULL
&&
7747 dtrace_match_probe(probe
, pkp
, priv
, uid
, zoneid
) > 0) {
7748 (void) (*matched
)(probe
, arg
);
7754 #if !defined(__APPLE__) /* Quiet compiler warnings */
7755 template.dtpr_mod
= (char *)pkp
->dtpk_mod
;
7756 template.dtpr_func
= (char *)pkp
->dtpk_func
;
7757 template.dtpr_name
= (char *)pkp
->dtpk_name
;
7759 template.dtpr_mod
= (char *)(uintptr_t)pkp
->dtpk_mod
;
7760 template.dtpr_func
= (char *)(uintptr_t)pkp
->dtpk_func
;
7761 template.dtpr_name
= (char *)(uintptr_t)pkp
->dtpk_name
;
7762 #endif /* __APPLE__ */
7765 * We want to find the most distinct of the module name, function
7766 * name, and name. So for each one that is not a glob pattern or
7767 * empty string, we perform a lookup in the corresponding hash and
7768 * use the hash table with the fewest collisions to do our search.
7770 if (pkp
->dtpk_mmatch
== &dtrace_match_string
&&
7771 (len
= dtrace_hash_collisions(dtrace_bymod
, &template)) < best
) {
7773 hash
= dtrace_bymod
;
7776 if (pkp
->dtpk_fmatch
== &dtrace_match_string
&&
7777 (len
= dtrace_hash_collisions(dtrace_byfunc
, &template)) < best
) {
7779 hash
= dtrace_byfunc
;
7782 if (pkp
->dtpk_nmatch
== &dtrace_match_string
&&
7783 (len
= dtrace_hash_collisions(dtrace_byname
, &template)) < best
) {
7785 hash
= dtrace_byname
;
7789 * If we did not select a hash table, iterate over every probe and
7790 * invoke our callback for each one that matches our input probe key.
7793 #if !defined(__APPLE__) /* Quiet compiler warning */
7794 for (i
= 0; i
< dtrace_nprobes
; i
++) {
7796 for (i
= 0; i
< (dtrace_id_t
)dtrace_nprobes
; i
++) {
7797 #endif /* __APPLE__ */
7798 if ((probe
= dtrace_probes
[i
]) == NULL
||
7799 dtrace_match_probe(probe
, pkp
, priv
, uid
,
7805 if ((*matched
)(probe
, arg
) != DTRACE_MATCH_NEXT
)
7813 * If we selected a hash table, iterate over each probe of the same key
7814 * name and invoke the callback for every probe that matches the other
7815 * attributes of our input probe key.
7817 for (probe
= dtrace_hash_lookup(hash
, &template); probe
!= NULL
;
7818 probe
= *(DTRACE_HASHNEXT(hash
, probe
))) {
7820 if (dtrace_match_probe(probe
, pkp
, priv
, uid
, zoneid
) <= 0)
7825 if ((*matched
)(probe
, arg
) != DTRACE_MATCH_NEXT
)
7833 * Return the function pointer dtrace_probecmp() should use to compare the
7834 * specified pattern with a string. For NULL or empty patterns, we select
7835 * dtrace_match_nul(). For glob pattern strings, we use dtrace_match_glob().
7836 * For non-empty non-glob strings, we use dtrace_match_string().
7838 static dtrace_probekey_f
*
7839 dtrace_probekey_func(const char *p
)
7843 if (p
== NULL
|| *p
== '\0')
7844 return (&dtrace_match_nul
);
7846 while ((c
= *p
++) != '\0') {
7847 if (c
== '[' || c
== '?' || c
== '*' || c
== '\\')
7848 return (&dtrace_match_glob
);
7851 return (&dtrace_match_string
);
7855 * Build a probe comparison key for use with dtrace_match_probe() from the
7856 * given probe description. By convention, a null key only matches anchored
7857 * probes: if each field is the empty string, reset dtpk_fmatch to
7858 * dtrace_match_nonzero().
7861 dtrace_probekey(const dtrace_probedesc_t
*pdp
, dtrace_probekey_t
*pkp
)
7863 pkp
->dtpk_prov
= pdp
->dtpd_provider
;
7864 pkp
->dtpk_pmatch
= dtrace_probekey_func(pdp
->dtpd_provider
);
7866 pkp
->dtpk_mod
= pdp
->dtpd_mod
;
7867 pkp
->dtpk_mmatch
= dtrace_probekey_func(pdp
->dtpd_mod
);
7869 pkp
->dtpk_func
= pdp
->dtpd_func
;
7870 pkp
->dtpk_fmatch
= dtrace_probekey_func(pdp
->dtpd_func
);
7872 pkp
->dtpk_name
= pdp
->dtpd_name
;
7873 pkp
->dtpk_nmatch
= dtrace_probekey_func(pdp
->dtpd_name
);
7875 pkp
->dtpk_id
= pdp
->dtpd_id
;
7877 if (pkp
->dtpk_id
== DTRACE_IDNONE
&&
7878 pkp
->dtpk_pmatch
== &dtrace_match_nul
&&
7879 pkp
->dtpk_mmatch
== &dtrace_match_nul
&&
7880 pkp
->dtpk_fmatch
== &dtrace_match_nul
&&
7881 pkp
->dtpk_nmatch
== &dtrace_match_nul
)
7882 pkp
->dtpk_fmatch
= &dtrace_match_nonzero
;
7886 * DTrace Provider-to-Framework API Functions
7888 * These functions implement much of the Provider-to-Framework API, as
7889 * described in <sys/dtrace.h>. The parts of the API not in this section are
7890 * the functions in the API for probe management (found below), and
7891 * dtrace_probe() itself (found above).
7895 * Register the calling provider with the DTrace framework. This should
7896 * generally be called by DTrace providers in their attach(9E) entry point.
7899 dtrace_register(const char *name
, const dtrace_pattr_t
*pap
, uint32_t priv
,
7900 cred_t
*cr
, const dtrace_pops_t
*pops
, void *arg
, dtrace_provider_id_t
*idp
)
7902 dtrace_provider_t
*provider
;
7904 if (name
== NULL
|| pap
== NULL
|| pops
== NULL
|| idp
== NULL
) {
7905 cmn_err(CE_WARN
, "failed to register provider '%s': invalid "
7906 "arguments", name
? name
: "<NULL>");
7910 if (name
[0] == '\0' || dtrace_badname(name
)) {
7911 cmn_err(CE_WARN
, "failed to register provider '%s': invalid "
7912 "provider name", name
);
7916 if ((pops
->dtps_provide
== NULL
&& pops
->dtps_provide_module
== NULL
) ||
7917 pops
->dtps_enable
== NULL
|| pops
->dtps_disable
== NULL
||
7918 pops
->dtps_destroy
== NULL
||
7919 ((pops
->dtps_resume
== NULL
) != (pops
->dtps_suspend
== NULL
))) {
7920 cmn_err(CE_WARN
, "failed to register provider '%s': invalid "
7921 "provider ops", name
);
7925 if (dtrace_badattr(&pap
->dtpa_provider
) ||
7926 dtrace_badattr(&pap
->dtpa_mod
) ||
7927 dtrace_badattr(&pap
->dtpa_func
) ||
7928 dtrace_badattr(&pap
->dtpa_name
) ||
7929 dtrace_badattr(&pap
->dtpa_args
)) {
7930 cmn_err(CE_WARN
, "failed to register provider '%s': invalid "
7931 "provider attributes", name
);
7935 if (priv
& ~DTRACE_PRIV_ALL
) {
7936 cmn_err(CE_WARN
, "failed to register provider '%s': invalid "
7937 "privilege attributes", name
);
7941 if ((priv
& DTRACE_PRIV_KERNEL
) &&
7942 (priv
& (DTRACE_PRIV_USER
| DTRACE_PRIV_OWNER
)) &&
7943 pops
->dtps_usermode
== NULL
) {
7944 cmn_err(CE_WARN
, "failed to register provider '%s': need "
7945 "dtps_usermode() op for given privilege attributes", name
);
7949 provider
= kmem_zalloc(sizeof (dtrace_provider_t
), KM_SLEEP
);
7950 #if !defined(__APPLE__)
7951 provider
->dtpv_name
= kmem_alloc(strlen(name
) + 1, KM_SLEEP
);
7952 (void) strcpy(provider
->dtpv_name
, name
);
7953 #else /* Employ size bounded string operation. */
7955 size_t bufsize
= strlen(name
) + 1;
7956 provider
->dtpv_name
= kmem_alloc(bufsize
, KM_SLEEP
);
7957 (void) strlcpy(provider
->dtpv_name
, name
, bufsize
);
7959 #endif /* __APPLE__ */
7961 provider
->dtpv_attr
= *pap
;
7962 provider
->dtpv_priv
.dtpp_flags
= priv
;
7964 provider
->dtpv_priv
.dtpp_uid
= crgetuid(cr
);
7965 provider
->dtpv_priv
.dtpp_zoneid
= crgetzoneid(cr
);
7967 provider
->dtpv_pops
= *pops
;
7969 if (pops
->dtps_provide
== NULL
) {
7970 ASSERT(pops
->dtps_provide_module
!= NULL
);
7971 provider
->dtpv_pops
.dtps_provide
=
7972 (void (*)(void *, const dtrace_probedesc_t
*))dtrace_nullop
;
7975 if (pops
->dtps_provide_module
== NULL
) {
7976 ASSERT(pops
->dtps_provide
!= NULL
);
7977 provider
->dtpv_pops
.dtps_provide_module
=
7978 (void (*)(void *, struct modctl
*))dtrace_nullop
;
7981 if (pops
->dtps_suspend
== NULL
) {
7982 ASSERT(pops
->dtps_resume
== NULL
);
7983 provider
->dtpv_pops
.dtps_suspend
=
7984 (void (*)(void *, dtrace_id_t
, void *))dtrace_nullop
;
7985 provider
->dtpv_pops
.dtps_resume
=
7986 (void (*)(void *, dtrace_id_t
, void *))dtrace_nullop
;
7989 provider
->dtpv_arg
= arg
;
7990 *idp
= (dtrace_provider_id_t
)provider
;
7992 if (pops
== &dtrace_provider_ops
) {
7993 lck_mtx_assert(&dtrace_provider_lock
, LCK_MTX_ASSERT_OWNED
);
7994 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
7995 ASSERT(dtrace_anon
.dta_enabling
== NULL
);
7998 * We make sure that the DTrace provider is at the head of
7999 * the provider chain.
8001 provider
->dtpv_next
= dtrace_provider
;
8002 dtrace_provider
= provider
;
8006 lck_mtx_lock(&dtrace_provider_lock
);
8007 lck_mtx_lock(&dtrace_lock
);
8010 * If there is at least one provider registered, we'll add this
8011 * provider after the first provider.
8013 if (dtrace_provider
!= NULL
) {
8014 provider
->dtpv_next
= dtrace_provider
->dtpv_next
;
8015 dtrace_provider
->dtpv_next
= provider
;
8017 dtrace_provider
= provider
;
8020 if (dtrace_retained
!= NULL
) {
8021 dtrace_enabling_provide(provider
);
8024 * Now we need to call dtrace_enabling_matchall() -- which
8025 * will acquire cpu_lock and dtrace_lock. We therefore need
8026 * to drop all of our locks before calling into it...
8028 lck_mtx_unlock(&dtrace_lock
);
8029 lck_mtx_unlock(&dtrace_provider_lock
);
8030 dtrace_enabling_matchall();
8035 lck_mtx_unlock(&dtrace_lock
);
8036 lck_mtx_unlock(&dtrace_provider_lock
);
8042 * Unregister the specified provider from the DTrace framework. This should
8043 * generally be called by DTrace providers in their detach(9E) entry point.
8046 dtrace_unregister(dtrace_provider_id_t id
)
8048 dtrace_provider_t
*old
= (dtrace_provider_t
*)id
;
8049 dtrace_provider_t
*prev
= NULL
;
8051 dtrace_probe_t
*probe
, *first
= NULL
;
8053 if (old
->dtpv_pops
.dtps_enable
==
8054 (void (*)(void *, dtrace_id_t
, void *))dtrace_nullop
) {
8056 * If DTrace itself is the provider, we're called with locks
8059 ASSERT(old
== dtrace_provider
);
8060 ASSERT(dtrace_devi
!= NULL
);
8061 lck_mtx_assert(&dtrace_provider_lock
, LCK_MTX_ASSERT_OWNED
);
8062 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
8065 if (dtrace_provider
->dtpv_next
!= NULL
) {
8067 * There's another provider here; return failure.
8072 lck_mtx_lock(&dtrace_provider_lock
);
8073 lck_mtx_lock(&mod_lock
);
8074 lck_mtx_lock(&dtrace_lock
);
8078 * If anyone has /dev/dtrace open, or if there are anonymous enabled
8079 * probes, we refuse to let providers slither away, unless this
8080 * provider has already been explicitly invalidated.
8082 if (!old
->dtpv_defunct
&&
8083 (dtrace_opens
|| (dtrace_anon
.dta_state
!= NULL
&&
8084 dtrace_anon
.dta_state
->dts_necbs
> 0))) {
8086 lck_mtx_unlock(&dtrace_lock
);
8087 lck_mtx_unlock(&mod_lock
);
8088 lck_mtx_unlock(&dtrace_provider_lock
);
8094 * Attempt to destroy the probes associated with this provider.
8096 for (i
= 0; i
< dtrace_nprobes
; i
++) {
8097 if ((probe
= dtrace_probes
[i
]) == NULL
)
8100 if (probe
->dtpr_provider
!= old
)
8103 if (probe
->dtpr_ecb
== NULL
)
8107 * We have at least one ECB; we can't remove this provider.
8110 lck_mtx_unlock(&dtrace_lock
);
8111 lck_mtx_unlock(&mod_lock
);
8112 lck_mtx_unlock(&dtrace_provider_lock
);
8118 * All of the probes for this provider are disabled; we can safely
8119 * remove all of them from their hash chains and from the probe array.
8121 for (i
= 0; i
< dtrace_nprobes
; i
++) {
8122 if ((probe
= dtrace_probes
[i
]) == NULL
)
8125 if (probe
->dtpr_provider
!= old
)
8128 dtrace_probes
[i
] = NULL
;
8130 dtrace_hash_remove(dtrace_bymod
, probe
);
8131 dtrace_hash_remove(dtrace_byfunc
, probe
);
8132 dtrace_hash_remove(dtrace_byname
, probe
);
8134 if (first
== NULL
) {
8136 probe
->dtpr_nextmod
= NULL
;
8138 probe
->dtpr_nextmod
= first
;
8144 * The provider's probes have been removed from the hash chains and
8145 * from the probe array. Now issue a dtrace_sync() to be sure that
8146 * everyone has cleared out from any probe array processing.
8150 for (probe
= first
; probe
!= NULL
; probe
= first
) {
8151 first
= probe
->dtpr_nextmod
;
8153 old
->dtpv_pops
.dtps_destroy(old
->dtpv_arg
, probe
->dtpr_id
,
8155 kmem_free(probe
->dtpr_mod
, strlen(probe
->dtpr_mod
) + 1);
8156 kmem_free(probe
->dtpr_func
, strlen(probe
->dtpr_func
) + 1);
8157 kmem_free(probe
->dtpr_name
, strlen(probe
->dtpr_name
) + 1);
8158 vmem_free(dtrace_arena
, (void *)(uintptr_t)(probe
->dtpr_id
), 1);
8159 #if !defined(__APPLE__)
8160 kmem_free(probe
, sizeof (dtrace_probe_t
));
8162 zfree(dtrace_probe_t_zone
, probe
);
8166 if ((prev
= dtrace_provider
) == old
) {
8167 ASSERT(self
|| dtrace_devi
== NULL
);
8168 ASSERT(old
->dtpv_next
== NULL
|| dtrace_devi
== NULL
);
8169 dtrace_provider
= old
->dtpv_next
;
8171 while (prev
!= NULL
&& prev
->dtpv_next
!= old
)
8172 prev
= prev
->dtpv_next
;
8175 panic("attempt to unregister non-existent "
8176 "dtrace provider %p\n", (void *)id
);
8179 prev
->dtpv_next
= old
->dtpv_next
;
8183 lck_mtx_unlock(&dtrace_lock
);
8184 lck_mtx_unlock(&mod_lock
);
8185 lck_mtx_unlock(&dtrace_provider_lock
);
8188 kmem_free(old
->dtpv_name
, strlen(old
->dtpv_name
) + 1);
8189 kmem_free(old
, sizeof (dtrace_provider_t
));
8195 * Invalidate the specified provider. All subsequent probe lookups for the
8196 * specified provider will fail, but its probes will not be removed.
8199 dtrace_invalidate(dtrace_provider_id_t id
)
8201 dtrace_provider_t
*pvp
= (dtrace_provider_t
*)id
;
8203 ASSERT(pvp
->dtpv_pops
.dtps_enable
!=
8204 (void (*)(void *, dtrace_id_t
, void *))dtrace_nullop
);
8206 lck_mtx_lock(&dtrace_provider_lock
);
8207 lck_mtx_lock(&dtrace_lock
);
8209 pvp
->dtpv_defunct
= 1;
8211 lck_mtx_unlock(&dtrace_lock
);
8212 lck_mtx_unlock(&dtrace_provider_lock
);
8216 * Indicate whether or not DTrace has attached.
8219 dtrace_attached(void)
8222 * dtrace_provider will be non-NULL iff the DTrace driver has
8223 * attached. (It's non-NULL because DTrace is always itself a
8226 return (dtrace_provider
!= NULL
);
8230 * Remove all the unenabled probes for the given provider. This function is
8231 * not unlike dtrace_unregister(), except that it doesn't remove the provider
8232 * -- just as many of its associated probes as it can.
8235 dtrace_condense(dtrace_provider_id_t id
)
8237 dtrace_provider_t
*prov
= (dtrace_provider_t
*)id
;
8239 dtrace_probe_t
*probe
;
8242 * Make sure this isn't the dtrace provider itself.
8244 ASSERT(prov
->dtpv_pops
.dtps_enable
!=
8245 (void (*)(void *, dtrace_id_t
, void *))dtrace_nullop
);
8247 lck_mtx_lock(&dtrace_provider_lock
);
8248 lck_mtx_lock(&dtrace_lock
);
8251 * Attempt to destroy the probes associated with this provider.
8253 for (i
= 0; i
< dtrace_nprobes
; i
++) {
8254 if ((probe
= dtrace_probes
[i
]) == NULL
)
8257 if (probe
->dtpr_provider
!= prov
)
8260 if (probe
->dtpr_ecb
!= NULL
)
8263 dtrace_probes
[i
] = NULL
;
8265 dtrace_hash_remove(dtrace_bymod
, probe
);
8266 dtrace_hash_remove(dtrace_byfunc
, probe
);
8267 dtrace_hash_remove(dtrace_byname
, probe
);
8269 prov
->dtpv_pops
.dtps_destroy(prov
->dtpv_arg
, i
+ 1,
8271 kmem_free(probe
->dtpr_mod
, strlen(probe
->dtpr_mod
) + 1);
8272 kmem_free(probe
->dtpr_func
, strlen(probe
->dtpr_func
) + 1);
8273 kmem_free(probe
->dtpr_name
, strlen(probe
->dtpr_name
) + 1);
8274 #if !defined(__APPLE__)
8275 kmem_free(probe
, sizeof (dtrace_probe_t
));
8277 zfree(dtrace_probe_t_zone
, probe
);
8279 vmem_free(dtrace_arena
, (void *)((uintptr_t)i
+ 1), 1);
8282 lck_mtx_unlock(&dtrace_lock
);
8283 lck_mtx_unlock(&dtrace_provider_lock
);
8289 * DTrace Probe Management Functions
8291 * The functions in this section perform the DTrace probe management,
8292 * including functions to create probes, look-up probes, and call into the
8293 * providers to request that probes be provided. Some of these functions are
8294 * in the Provider-to-Framework API; these functions can be identified by the
8295 * fact that they are not declared "static".
8299 * Create a probe with the specified module name, function name, and name.
8302 dtrace_probe_create(dtrace_provider_id_t prov
, const char *mod
,
8303 const char *func
, const char *name
, int aframes
, void *arg
)
8305 dtrace_probe_t
*probe
, **probes
;
8306 dtrace_provider_t
*provider
= (dtrace_provider_t
*)prov
;
8309 if (provider
== dtrace_provider
) {
8310 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
8312 lck_mtx_lock(&dtrace_lock
);
8315 id
= (dtrace_id_t
)(uintptr_t)vmem_alloc(dtrace_arena
, 1,
8316 VM_BESTFIT
| VM_SLEEP
);
8317 #if !defined(__APPLE__)
8318 probe
= kmem_zalloc(sizeof (dtrace_probe_t
), KM_SLEEP
);
8320 probe
= zalloc(dtrace_probe_t_zone
);
8321 bzero(probe
, sizeof (dtrace_probe_t
));
8324 probe
->dtpr_id
= id
;
8325 probe
->dtpr_gen
= dtrace_probegen
++;
8326 probe
->dtpr_mod
= dtrace_strdup(mod
);
8327 probe
->dtpr_func
= dtrace_strdup(func
);
8328 probe
->dtpr_name
= dtrace_strdup(name
);
8329 probe
->dtpr_arg
= arg
;
8330 probe
->dtpr_aframes
= aframes
;
8331 probe
->dtpr_provider
= provider
;
8333 dtrace_hash_add(dtrace_bymod
, probe
);
8334 dtrace_hash_add(dtrace_byfunc
, probe
);
8335 dtrace_hash_add(dtrace_byname
, probe
);
8337 #if !defined(__APPLE__) /* Quiet compiler warning */
8338 if (id
- 1 >= dtrace_nprobes
) {
8340 if (id
- 1 >= (dtrace_id_t
)dtrace_nprobes
) {
8341 #endif /* __APPLE__ */
8342 size_t osize
= dtrace_nprobes
* sizeof (dtrace_probe_t
*);
8343 size_t nsize
= osize
<< 1;
8347 ASSERT(dtrace_probes
== NULL
);
8348 nsize
= sizeof (dtrace_probe_t
*);
8351 probes
= kmem_zalloc(nsize
, KM_SLEEP
);
8353 if (dtrace_probes
== NULL
) {
8355 dtrace_probes
= probes
;
8358 dtrace_probe_t
**oprobes
= dtrace_probes
;
8360 bcopy(oprobes
, probes
, osize
);
8361 dtrace_membar_producer();
8362 dtrace_probes
= probes
;
8367 * All CPUs are now seeing the new probes array; we can
8368 * safely free the old array.
8370 kmem_free(oprobes
, osize
);
8371 dtrace_nprobes
<<= 1;
8374 #if !defined(__APPLE__) /* Quiet compiler warning */
8375 ASSERT(id
- 1 < dtrace_nprobes
);
8377 ASSERT(id
- 1 < (dtrace_id_t
)dtrace_nprobes
);
8378 #endif /* __APPLE__ */
8381 ASSERT(dtrace_probes
[id
- 1] == NULL
);
8382 dtrace_probes
[id
- 1] = probe
;
8384 if (provider
!= dtrace_provider
)
8385 lck_mtx_unlock(&dtrace_lock
);
8390 static dtrace_probe_t
*
8391 dtrace_probe_lookup_id(dtrace_id_t id
)
8393 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
8395 #if !defined(__APPLE__) /* Quiet compiler warning */
8396 if (id
== 0 || id
> dtrace_nprobes
)
8399 if (id
== 0 || id
> (dtrace_id_t
)dtrace_nprobes
)
8401 #endif /* __APPLE__ */
8403 return (dtrace_probes
[id
- 1]);
8407 dtrace_probe_lookup_match(dtrace_probe_t
*probe
, void *arg
)
8409 *((dtrace_id_t
*)arg
) = probe
->dtpr_id
;
8411 return (DTRACE_MATCH_DONE
);
8415 * Look up a probe based on provider and one or more of module name, function
8416 * name and probe name.
8419 dtrace_probe_lookup(dtrace_provider_id_t prid
, const char *mod
,
8420 const char *func
, const char *name
)
8422 dtrace_probekey_t pkey
;
8426 pkey
.dtpk_prov
= ((dtrace_provider_t
*)prid
)->dtpv_name
;
8427 pkey
.dtpk_pmatch
= &dtrace_match_string
;
8428 pkey
.dtpk_mod
= mod
;
8429 pkey
.dtpk_mmatch
= mod
? &dtrace_match_string
: &dtrace_match_nul
;
8430 pkey
.dtpk_func
= func
;
8431 pkey
.dtpk_fmatch
= func
? &dtrace_match_string
: &dtrace_match_nul
;
8432 pkey
.dtpk_name
= name
;
8433 pkey
.dtpk_nmatch
= name
? &dtrace_match_string
: &dtrace_match_nul
;
8434 pkey
.dtpk_id
= DTRACE_IDNONE
;
8436 lck_mtx_lock(&dtrace_lock
);
8437 match
= dtrace_match(&pkey
, DTRACE_PRIV_ALL
, 0, 0,
8438 dtrace_probe_lookup_match
, &id
);
8439 lck_mtx_unlock(&dtrace_lock
);
8441 ASSERT(match
== 1 || match
== 0);
8442 return (match
? id
: 0);
8446 * Returns the probe argument associated with the specified probe.
8449 dtrace_probe_arg(dtrace_provider_id_t id
, dtrace_id_t pid
)
8451 dtrace_probe_t
*probe
;
8454 lck_mtx_lock(&dtrace_lock
);
8456 if ((probe
= dtrace_probe_lookup_id(pid
)) != NULL
&&
8457 probe
->dtpr_provider
== (dtrace_provider_t
*)id
)
8458 rval
= probe
->dtpr_arg
;
8460 lck_mtx_unlock(&dtrace_lock
);
8466 * Copy a probe into a probe description.
8469 dtrace_probe_description(const dtrace_probe_t
*prp
, dtrace_probedesc_t
*pdp
)
8471 bzero(pdp
, sizeof (dtrace_probedesc_t
));
8472 pdp
->dtpd_id
= prp
->dtpr_id
;
8474 #if !defined(__APPLE__)
8475 (void) strncpy(pdp
->dtpd_provider
,
8476 prp
->dtpr_provider
->dtpv_name
, DTRACE_PROVNAMELEN
- 1);
8478 (void) strncpy(pdp
->dtpd_mod
, prp
->dtpr_mod
, DTRACE_MODNAMELEN
- 1);
8479 (void) strncpy(pdp
->dtpd_func
, prp
->dtpr_func
, DTRACE_FUNCNAMELEN
- 1);
8480 (void) strncpy(pdp
->dtpd_name
, prp
->dtpr_name
, DTRACE_NAMELEN
- 1);
8481 #else /* Employ size bounded string operation. */
8482 (void) strlcpy(pdp
->dtpd_provider
,
8483 prp
->dtpr_provider
->dtpv_name
, DTRACE_PROVNAMELEN
);
8485 (void) strlcpy(pdp
->dtpd_mod
, prp
->dtpr_mod
, DTRACE_MODNAMELEN
);
8486 (void) strlcpy(pdp
->dtpd_func
, prp
->dtpr_func
, DTRACE_FUNCNAMELEN
);
8487 (void) strlcpy(pdp
->dtpd_name
, prp
->dtpr_name
, DTRACE_NAMELEN
);
8488 #endif /* __APPLE__ */
8492 * Called to indicate that a probe -- or probes -- should be provided by a
8493 * specfied provider. If the specified description is NULL, the provider will
8494 * be told to provide all of its probes. (This is done whenever a new
8495 * consumer comes along, or whenever a retained enabling is to be matched.) If
8496 * the specified description is non-NULL, the provider is given the
8497 * opportunity to dynamically provide the specified probe, allowing providers
8498 * to support the creation of probes on-the-fly. (So-called _autocreated_
8499 * probes.) If the provider is NULL, the operations will be applied to all
8500 * providers; if the provider is non-NULL the operations will only be applied
8501 * to the specified provider. The dtrace_provider_lock must be held, and the
8502 * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation
8503 * will need to grab the dtrace_lock when it reenters the framework through
8504 * dtrace_probe_lookup(), dtrace_probe_create(), etc.
8507 dtrace_probe_provide(dtrace_probedesc_t
*desc
, dtrace_provider_t
*prv
)
8511 #pragma unused(ctl) /* __APPLE__ */
8513 lck_mtx_assert(&dtrace_provider_lock
, LCK_MTX_ASSERT_OWNED
);
8517 prv
= dtrace_provider
;
8522 * First, call the blanket provide operation.
8524 prv
->dtpv_pops
.dtps_provide(prv
->dtpv_arg
, desc
);
8526 #if !defined(__APPLE__)
8528 * Now call the per-module provide operation. We will grab
8529 * mod_lock to prevent the list from being modified. Note
8530 * that this also prevents the mod_busy bits from changing.
8531 * (mod_busy can only be changed with mod_lock held.)
8533 mutex_enter(&mod_lock
);
8537 if (ctl
->mod_busy
|| ctl
->mod_mp
== NULL
)
8540 prv
->dtpv_pops
.dtps_provide_module(prv
->dtpv_arg
, ctl
);
8542 } while ((ctl
= ctl
->mod_next
) != &modules
);
8544 mutex_exit(&mod_lock
);
8546 #if 0 /* FIXME: Workaround for PR_4643546 */
8547 /* NOTE: kmod_lock has been removed. */
8548 simple_lock(&kmod_lock
);
8550 kmod_info_t
*ktl
= kmod
;
8552 prv
->dtpv_pops
.dtps_provide_module(prv
->dtpv_arg
, ktl
);
8556 simple_unlock(&kmod_lock
);
8559 * Don't bother to iterate over the kmod list. At present only fbt
8560 * offers a provide_module in its dtpv_pops, and then it ignores the
8563 prv
->dtpv_pops
.dtps_provide_module(prv
->dtpv_arg
, NULL
);
8565 #endif /* __APPLE__ */
8566 } while (all
&& (prv
= prv
->dtpv_next
) != NULL
);
8570 * Iterate over each probe, and call the Framework-to-Provider API function
8574 dtrace_probe_foreach(uintptr_t offs
)
8576 dtrace_provider_t
*prov
;
8577 void (*func
)(void *, dtrace_id_t
, void *);
8578 dtrace_probe_t
*probe
;
8579 dtrace_icookie_t cookie
;
8583 * We disable interrupts to walk through the probe array. This is
8584 * safe -- the dtrace_sync() in dtrace_unregister() assures that we
8585 * won't see stale data.
8587 cookie
= dtrace_interrupt_disable();
8589 for (i
= 0; i
< dtrace_nprobes
; i
++) {
8590 if ((probe
= dtrace_probes
[i
]) == NULL
)
8593 if (probe
->dtpr_ecb
== NULL
) {
8595 * This probe isn't enabled -- don't call the function.
8600 prov
= probe
->dtpr_provider
;
8601 func
= *((void(**)(void *, dtrace_id_t
, void *))
8602 ((uintptr_t)&prov
->dtpv_pops
+ offs
));
8604 func(prov
->dtpv_arg
, i
+ 1, probe
->dtpr_arg
);
8607 dtrace_interrupt_enable(cookie
);
8611 dtrace_probe_enable(const dtrace_probedesc_t
*desc
, dtrace_enabling_t
*enab
)
8613 dtrace_probekey_t pkey
;
8618 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
8620 dtrace_ecb_create_cache
= NULL
;
8624 * If we're passed a NULL description, we're being asked to
8625 * create an ECB with a NULL probe.
8627 (void) dtrace_ecb_create_enable(NULL
, enab
);
8631 dtrace_probekey(desc
, &pkey
);
8632 dtrace_cred2priv(enab
->dten_vstate
->dtvs_state
->dts_cred
.dcr_cred
,
8633 &priv
, &uid
, &zoneid
);
8635 return (dtrace_match(&pkey
, priv
, uid
, zoneid
, dtrace_ecb_create_enable
,
8640 * DTrace Helper Provider Functions
8643 dtrace_dofattr2attr(dtrace_attribute_t
*attr
, const dof_attr_t dofattr
)
8645 attr
->dtat_name
= DOF_ATTR_NAME(dofattr
);
8646 attr
->dtat_data
= DOF_ATTR_DATA(dofattr
);
8647 attr
->dtat_class
= DOF_ATTR_CLASS(dofattr
);
8651 dtrace_dofprov2hprov(dtrace_helper_provdesc_t
*hprov
,
8652 const dof_provider_t
*dofprov
, char *strtab
)
8654 hprov
->dthpv_provname
= strtab
+ dofprov
->dofpv_name
;
8655 dtrace_dofattr2attr(&hprov
->dthpv_pattr
.dtpa_provider
,
8656 dofprov
->dofpv_provattr
);
8657 dtrace_dofattr2attr(&hprov
->dthpv_pattr
.dtpa_mod
,
8658 dofprov
->dofpv_modattr
);
8659 dtrace_dofattr2attr(&hprov
->dthpv_pattr
.dtpa_func
,
8660 dofprov
->dofpv_funcattr
);
8661 dtrace_dofattr2attr(&hprov
->dthpv_pattr
.dtpa_name
,
8662 dofprov
->dofpv_nameattr
);
8663 dtrace_dofattr2attr(&hprov
->dthpv_pattr
.dtpa_args
,
8664 dofprov
->dofpv_argsattr
);
8668 dtrace_helper_provide_one(dof_helper_t
*dhp
, dof_sec_t
*sec
, pid_t pid
)
8670 uintptr_t daddr
= (uintptr_t)dhp
->dofhp_dof
;
8671 dof_hdr_t
*dof
= (dof_hdr_t
*)daddr
;
8672 dof_sec_t
*str_sec
, *prb_sec
, *arg_sec
, *off_sec
, *enoff_sec
;
8673 dof_provider_t
*provider
;
8675 uint32_t *off
, *enoff
;
8679 dtrace_helper_provdesc_t dhpv
;
8680 dtrace_helper_probedesc_t dhpb
;
8681 dtrace_meta_t
*meta
= dtrace_meta_pid
;
8682 dtrace_mops_t
*mops
= &meta
->dtm_mops
;
8685 provider
= (dof_provider_t
*)(uintptr_t)(daddr
+ sec
->dofs_offset
);
8686 str_sec
= (dof_sec_t
*)(uintptr_t)(daddr
+ dof
->dofh_secoff
+
8687 provider
->dofpv_strtab
* dof
->dofh_secsize
);
8688 prb_sec
= (dof_sec_t
*)(uintptr_t)(daddr
+ dof
->dofh_secoff
+
8689 provider
->dofpv_probes
* dof
->dofh_secsize
);
8690 arg_sec
= (dof_sec_t
*)(uintptr_t)(daddr
+ dof
->dofh_secoff
+
8691 provider
->dofpv_prargs
* dof
->dofh_secsize
);
8692 off_sec
= (dof_sec_t
*)(uintptr_t)(daddr
+ dof
->dofh_secoff
+
8693 provider
->dofpv_proffs
* dof
->dofh_secsize
);
8695 strtab
= (char *)(uintptr_t)(daddr
+ str_sec
->dofs_offset
);
8696 off
= (uint32_t *)(uintptr_t)(daddr
+ off_sec
->dofs_offset
);
8697 arg
= (uint8_t *)(uintptr_t)(daddr
+ arg_sec
->dofs_offset
);
8701 * See dtrace_helper_provider_validate().
8703 if (dof
->dofh_ident
[DOF_ID_VERSION
] != DOF_VERSION_1
&&
8704 provider
->dofpv_prenoffs
!= DOF_SECT_NONE
) {
8705 enoff_sec
= (dof_sec_t
*)(uintptr_t)(daddr
+ dof
->dofh_secoff
+
8706 provider
->dofpv_prenoffs
* dof
->dofh_secsize
);
8707 enoff
= (uint32_t *)(uintptr_t)(daddr
+ enoff_sec
->dofs_offset
);
8710 nprobes
= prb_sec
->dofs_size
/ prb_sec
->dofs_entsize
;
8713 * Create the provider.
8715 dtrace_dofprov2hprov(&dhpv
, provider
, strtab
);
8717 if ((parg
= mops
->dtms_provide_pid(meta
->dtm_arg
, &dhpv
, pid
)) == NULL
)
8723 * Create the probes.
8725 for (i
= 0; i
< nprobes
; i
++) {
8726 probe
= (dof_probe_t
*)(uintptr_t)(daddr
+
8727 prb_sec
->dofs_offset
+ i
* prb_sec
->dofs_entsize
);
8729 dhpb
.dthpb_mod
= dhp
->dofhp_mod
;
8730 dhpb
.dthpb_func
= strtab
+ probe
->dofpr_func
;
8731 dhpb
.dthpb_name
= strtab
+ probe
->dofpr_name
;
8732 #if !defined(__APPLE__)
8733 dhpb
.dthpb_base
= probe
->dofpr_addr
;
8735 dhpb
.dthpb_base
= dhp
->dofhp_addr
; /* FIXME: James, why? */
8737 #if !defined(__APPLE__) /* Quiet compiler warning */
8738 dhpb
.dthpb_offs
= off
+ probe
->dofpr_offidx
;
8740 dhpb
.dthpb_offs
= (int32_t *)(off
+ probe
->dofpr_offidx
);
8741 #endif /* __APPLE__ */
8742 dhpb
.dthpb_noffs
= probe
->dofpr_noffs
;
8743 if (enoff
!= NULL
) {
8744 #if !defined(__APPLE__) /* Quiet compiler warning */
8745 dhpb
.dthpb_enoffs
= enoff
+ probe
->dofpr_enoffidx
;
8747 dhpb
.dthpb_enoffs
= (int32_t *)(enoff
+ probe
->dofpr_enoffidx
);
8748 #endif /* __APPLE__ */
8749 dhpb
.dthpb_nenoffs
= probe
->dofpr_nenoffs
;
8751 dhpb
.dthpb_enoffs
= NULL
;
8752 dhpb
.dthpb_nenoffs
= 0;
8754 dhpb
.dthpb_args
= arg
+ probe
->dofpr_argidx
;
8755 dhpb
.dthpb_nargc
= probe
->dofpr_nargc
;
8756 dhpb
.dthpb_xargc
= probe
->dofpr_xargc
;
8757 dhpb
.dthpb_ntypes
= strtab
+ probe
->dofpr_nargv
;
8758 dhpb
.dthpb_xtypes
= strtab
+ probe
->dofpr_xargv
;
8760 mops
->dtms_create_probe(meta
->dtm_arg
, parg
, &dhpb
);
8765 dtrace_helper_provide(dof_helper_t
*dhp
, pid_t pid
)
8767 uintptr_t daddr
= (uintptr_t)dhp
->dofhp_dof
;
8768 dof_hdr_t
*dof
= (dof_hdr_t
*)daddr
;
8769 #if !defined(__APPLE__) /* Quiet compiler warning */
8773 #endif /* __APPLE__ */
8775 lck_mtx_assert(&dtrace_meta_lock
, LCK_MTX_ASSERT_OWNED
);
8777 for (i
= 0; i
< dof
->dofh_secnum
; i
++) {
8778 dof_sec_t
*sec
= (dof_sec_t
*)(uintptr_t)(daddr
+
8779 dof
->dofh_secoff
+ i
* dof
->dofh_secsize
);
8781 if (sec
->dofs_type
!= DOF_SECT_PROVIDER
)
8784 dtrace_helper_provide_one(dhp
, sec
, pid
);
8788 * We may have just created probes, so we must now rematch against
8789 * any retained enablings. Note that this call will acquire both
8790 * cpu_lock and dtrace_lock; the fact that we are holding
8791 * dtrace_meta_lock now is what defines the ordering with respect to
8792 * these three locks.
8794 dtrace_enabling_matchall();
8798 dtrace_helper_provider_remove_one(dof_helper_t
*dhp
, dof_sec_t
*sec
, pid_t pid
)
8800 uintptr_t daddr
= (uintptr_t)dhp
->dofhp_dof
;
8801 dof_hdr_t
*dof
= (dof_hdr_t
*)daddr
;
8803 dof_provider_t
*provider
;
8805 dtrace_helper_provdesc_t dhpv
;
8806 dtrace_meta_t
*meta
= dtrace_meta_pid
;
8807 dtrace_mops_t
*mops
= &meta
->dtm_mops
;
8809 provider
= (dof_provider_t
*)(uintptr_t)(daddr
+ sec
->dofs_offset
);
8810 str_sec
= (dof_sec_t
*)(uintptr_t)(daddr
+ dof
->dofh_secoff
+
8811 provider
->dofpv_strtab
* dof
->dofh_secsize
);
8813 strtab
= (char *)(uintptr_t)(daddr
+ str_sec
->dofs_offset
);
8816 * Create the provider.
8818 dtrace_dofprov2hprov(&dhpv
, provider
, strtab
);
8820 mops
->dtms_remove_pid(meta
->dtm_arg
, &dhpv
, pid
);
8826 dtrace_helper_provider_remove(dof_helper_t
*dhp
, pid_t pid
)
8828 uintptr_t daddr
= (uintptr_t)dhp
->dofhp_dof
;
8829 dof_hdr_t
*dof
= (dof_hdr_t
*)daddr
;
8830 #if !defined(__APPLE__) /* Quiet compiler warning */
8834 #endif /* __APPLE__ */
8836 lck_mtx_assert(&dtrace_meta_lock
, LCK_MTX_ASSERT_OWNED
);
8838 for (i
= 0; i
< dof
->dofh_secnum
; i
++) {
8839 dof_sec_t
*sec
= (dof_sec_t
*)(uintptr_t)(daddr
+
8840 dof
->dofh_secoff
+ i
* dof
->dofh_secsize
);
8842 if (sec
->dofs_type
!= DOF_SECT_PROVIDER
)
8845 dtrace_helper_provider_remove_one(dhp
, sec
, pid
);
8850 * DTrace Meta Provider-to-Framework API Functions
8852 * These functions implement the Meta Provider-to-Framework API, as described
8853 * in <sys/dtrace.h>.
8856 dtrace_meta_register(const char *name
, const dtrace_mops_t
*mops
, void *arg
,
8857 dtrace_meta_provider_id_t
*idp
)
8859 dtrace_meta_t
*meta
;
8860 dtrace_helpers_t
*help
, *next
;
8861 #if !defined(__APPLE__) /* Quiet compiler warning */
8865 #endif /* __APPLE__ */
8867 *idp
= DTRACE_METAPROVNONE
;
8870 * We strictly don't need the name, but we hold onto it for
8871 * debuggability. All hail error queues!
8874 cmn_err(CE_WARN
, "failed to register meta-provider: "
8880 mops
->dtms_create_probe
== NULL
||
8881 mops
->dtms_provide_pid
== NULL
||
8882 mops
->dtms_remove_pid
== NULL
) {
8883 cmn_err(CE_WARN
, "failed to register meta-register %s: "
8884 "invalid ops", name
);
8888 meta
= kmem_zalloc(sizeof (dtrace_meta_t
), KM_SLEEP
);
8889 meta
->dtm_mops
= *mops
;
8890 #if !defined(__APPLE__)
8891 meta
->dtm_name
= kmem_alloc(strlen(name
) + 1, KM_SLEEP
);
8892 (void) strcpy(meta
->dtm_name
, name
);
8893 #else /* Employ size bounded string operation. */
8895 size_t bufsize
= strlen(name
) + 1;
8896 meta
->dtm_name
= kmem_alloc(bufsize
, KM_SLEEP
);
8897 (void) strlcpy(meta
->dtm_name
, name
, bufsize
);
8899 #endif /* __APPLE__ */
8900 meta
->dtm_arg
= arg
;
8902 lck_mtx_lock(&dtrace_meta_lock
);
8903 lck_mtx_lock(&dtrace_lock
);
8905 if (dtrace_meta_pid
!= NULL
) {
8906 lck_mtx_unlock(&dtrace_lock
);
8907 lck_mtx_unlock(&dtrace_meta_lock
);
8908 cmn_err(CE_WARN
, "failed to register meta-register %s: "
8909 "user-land meta-provider exists", name
);
8910 kmem_free(meta
->dtm_name
, strlen(meta
->dtm_name
) + 1);
8911 kmem_free(meta
, sizeof (dtrace_meta_t
));
8915 dtrace_meta_pid
= meta
;
8916 *idp
= (dtrace_meta_provider_id_t
)meta
;
8919 * If there are providers and probes ready to go, pass them
8920 * off to the new meta provider now.
8923 help
= dtrace_deferred_pid
;
8924 dtrace_deferred_pid
= NULL
;
8926 lck_mtx_unlock(&dtrace_lock
);
8928 while (help
!= NULL
) {
8929 for (i
= 0; i
< help
->dthps_nprovs
; i
++) {
8930 dtrace_helper_provide(&help
->dthps_provs
[i
]->dthp_prov
,
8934 next
= help
->dthps_next
;
8935 help
->dthps_next
= NULL
;
8936 help
->dthps_prev
= NULL
;
8937 help
->dthps_deferred
= 0;
8941 lck_mtx_unlock(&dtrace_meta_lock
);
8947 dtrace_meta_unregister(dtrace_meta_provider_id_t id
)
8949 dtrace_meta_t
**pp
, *old
= (dtrace_meta_t
*)id
;
8951 lck_mtx_lock(&dtrace_meta_lock
);
8952 lck_mtx_lock(&dtrace_lock
);
8954 if (old
== dtrace_meta_pid
) {
8955 pp
= &dtrace_meta_pid
;
8957 panic("attempt to unregister non-existent "
8958 "dtrace meta-provider %p\n", (void *)old
);
8961 if (old
->dtm_count
!= 0) {
8962 lck_mtx_unlock(&dtrace_lock
);
8963 lck_mtx_unlock(&dtrace_meta_lock
);
8969 lck_mtx_unlock(&dtrace_lock
);
8970 lck_mtx_unlock(&dtrace_meta_lock
);
8972 kmem_free(old
->dtm_name
, strlen(old
->dtm_name
) + 1);
8973 kmem_free(old
, sizeof (dtrace_meta_t
));
8980 * DTrace DIF Object Functions
8983 dtrace_difo_err(uint_t pc
, const char *format
, ...)
8985 if (dtrace_err_verbose
) {
8988 (void) uprintf("dtrace DIF object error: [%u]: ", pc
);
8989 va_start(alist
, format
);
8990 (void) vuprintf(format
, alist
);
8994 #ifdef DTRACE_ERRDEBUG
8995 dtrace_errdebug(format
);
9001 * Validate a DTrace DIF object by checking the IR instructions. The following
9002 * rules are currently enforced by dtrace_difo_validate():
9004 * 1. Each instruction must have a valid opcode
9005 * 2. Each register, string, variable, or subroutine reference must be valid
9006 * 3. No instruction can modify register %r0 (must be zero)
9007 * 4. All instruction reserved bits must be set to zero
9008 * 5. The last instruction must be a "ret" instruction
9009 * 6. All branch targets must reference a valid instruction _after_ the branch
9012 dtrace_difo_validate(dtrace_difo_t
*dp
, dtrace_vstate_t
*vstate
, uint_t nregs
,
9015 #if !defined(__APPLE__) /* Quiet compiler warnings */
9020 #endif /* __APPLE__ */
9021 int (*efunc
)(uint_t pc
, const char *, ...) = dtrace_difo_err
;
9025 kcheckload
= cr
== NULL
||
9026 (vstate
->dtvs_state
->dts_cred
.dcr_visible
& DTRACE_CRV_KERNEL
) == 0;
9028 dp
->dtdo_destructive
= 0;
9030 for (pc
= 0; pc
< dp
->dtdo_len
&& err
== 0; pc
++) {
9031 dif_instr_t instr
= dp
->dtdo_buf
[pc
];
9033 uint_t r1
= DIF_INSTR_R1(instr
);
9034 uint_t r2
= DIF_INSTR_R2(instr
);
9035 uint_t rd
= DIF_INSTR_RD(instr
);
9036 uint_t rs
= DIF_INSTR_RS(instr
);
9037 uint_t label
= DIF_INSTR_LABEL(instr
);
9038 uint_t v
= DIF_INSTR_VAR(instr
);
9039 uint_t subr
= DIF_INSTR_SUBR(instr
);
9040 uint_t type
= DIF_INSTR_TYPE(instr
);
9041 uint_t op
= DIF_INSTR_OP(instr
);
9059 err
+= efunc(pc
, "invalid register %u\n", r1
);
9061 err
+= efunc(pc
, "invalid register %u\n", r2
);
9063 err
+= efunc(pc
, "invalid register %u\n", rd
);
9065 err
+= efunc(pc
, "cannot write to %r0\n");
9071 err
+= efunc(pc
, "invalid register %u\n", r1
);
9073 err
+= efunc(pc
, "non-zero reserved bits\n");
9075 err
+= efunc(pc
, "invalid register %u\n", rd
);
9077 err
+= efunc(pc
, "cannot write to %r0\n");
9087 err
+= efunc(pc
, "invalid register %u\n", r1
);
9089 err
+= efunc(pc
, "non-zero reserved bits\n");
9091 err
+= efunc(pc
, "invalid register %u\n", rd
);
9093 err
+= efunc(pc
, "cannot write to %r0\n");
9095 dp
->dtdo_buf
[pc
] = DIF_INSTR_LOAD(op
+
9096 DIF_OP_RLDSB
- DIF_OP_LDSB
, r1
, rd
);
9106 err
+= efunc(pc
, "invalid register %u\n", r1
);
9108 err
+= efunc(pc
, "non-zero reserved bits\n");
9110 err
+= efunc(pc
, "invalid register %u\n", rd
);
9112 err
+= efunc(pc
, "cannot write to %r0\n");
9122 err
+= efunc(pc
, "invalid register %u\n", r1
);
9124 err
+= efunc(pc
, "non-zero reserved bits\n");
9126 err
+= efunc(pc
, "invalid register %u\n", rd
);
9128 err
+= efunc(pc
, "cannot write to %r0\n");
9135 err
+= efunc(pc
, "invalid register %u\n", r1
);
9137 err
+= efunc(pc
, "non-zero reserved bits\n");
9139 err
+= efunc(pc
, "invalid register %u\n", rd
);
9141 err
+= efunc(pc
, "cannot write to 0 address\n");
9146 err
+= efunc(pc
, "invalid register %u\n", r1
);
9148 err
+= efunc(pc
, "invalid register %u\n", r2
);
9150 err
+= efunc(pc
, "non-zero reserved bits\n");
9154 err
+= efunc(pc
, "invalid register %u\n", r1
);
9155 if (r2
!= 0 || rd
!= 0)
9156 err
+= efunc(pc
, "non-zero reserved bits\n");
9169 if (label
>= dp
->dtdo_len
) {
9170 err
+= efunc(pc
, "invalid branch target %u\n",
9174 err
+= efunc(pc
, "backward branch to %u\n",
9179 if (r1
!= 0 || r2
!= 0)
9180 err
+= efunc(pc
, "non-zero reserved bits\n");
9182 err
+= efunc(pc
, "invalid register %u\n", rd
);
9186 case DIF_OP_FLUSHTS
:
9187 if (r1
!= 0 || r2
!= 0 || rd
!= 0)
9188 err
+= efunc(pc
, "non-zero reserved bits\n");
9191 if (DIF_INSTR_INTEGER(instr
) >= dp
->dtdo_intlen
) {
9192 err
+= efunc(pc
, "invalid integer ref %u\n",
9193 DIF_INSTR_INTEGER(instr
));
9196 err
+= efunc(pc
, "invalid register %u\n", rd
);
9198 err
+= efunc(pc
, "cannot write to %r0\n");
9201 if (DIF_INSTR_STRING(instr
) >= dp
->dtdo_strlen
) {
9202 err
+= efunc(pc
, "invalid string ref %u\n",
9203 DIF_INSTR_STRING(instr
));
9206 err
+= efunc(pc
, "invalid register %u\n", rd
);
9208 err
+= efunc(pc
, "cannot write to %r0\n");
9212 if (r1
> DIF_VAR_ARRAY_MAX
)
9213 err
+= efunc(pc
, "invalid array %u\n", r1
);
9215 err
+= efunc(pc
, "invalid register %u\n", r2
);
9217 err
+= efunc(pc
, "invalid register %u\n", rd
);
9219 err
+= efunc(pc
, "cannot write to %r0\n");
9226 if (v
< DIF_VAR_OTHER_MIN
|| v
> DIF_VAR_OTHER_MAX
)
9227 err
+= efunc(pc
, "invalid variable %u\n", v
);
9229 err
+= efunc(pc
, "invalid register %u\n", rd
);
9231 err
+= efunc(pc
, "cannot write to %r0\n");
9238 if (v
< DIF_VAR_OTHER_UBASE
|| v
> DIF_VAR_OTHER_MAX
)
9239 err
+= efunc(pc
, "invalid variable %u\n", v
);
9241 err
+= efunc(pc
, "invalid register %u\n", rd
);
9244 if (subr
> DIF_SUBR_MAX
)
9245 err
+= efunc(pc
, "invalid subr %u\n", subr
);
9247 err
+= efunc(pc
, "invalid register %u\n", rd
);
9249 err
+= efunc(pc
, "cannot write to %r0\n");
9251 if (subr
== DIF_SUBR_COPYOUT
||
9252 subr
== DIF_SUBR_COPYOUTSTR
) {
9253 dp
->dtdo_destructive
= 1;
9257 if (type
!= DIF_TYPE_STRING
&& type
!= DIF_TYPE_CTF
)
9258 err
+= efunc(pc
, "invalid ref type %u\n", type
);
9260 err
+= efunc(pc
, "invalid register %u\n", r2
);
9262 err
+= efunc(pc
, "invalid register %u\n", rs
);
9265 if (type
!= DIF_TYPE_CTF
)
9266 err
+= efunc(pc
, "invalid val type %u\n", type
);
9268 err
+= efunc(pc
, "invalid register %u\n", r2
);
9270 err
+= efunc(pc
, "invalid register %u\n", rs
);
9273 err
+= efunc(pc
, "invalid opcode %u\n",
9274 DIF_INSTR_OP(instr
));
9278 if (dp
->dtdo_len
!= 0 &&
9279 DIF_INSTR_OP(dp
->dtdo_buf
[dp
->dtdo_len
- 1]) != DIF_OP_RET
) {
9280 err
+= efunc(dp
->dtdo_len
- 1,
9281 "expected 'ret' as last DIF instruction\n");
9284 if (!(dp
->dtdo_rtype
.dtdt_flags
& DIF_TF_BYREF
)) {
9286 * If we're not returning by reference, the size must be either
9287 * 0 or the size of one of the base types.
9289 switch (dp
->dtdo_rtype
.dtdt_size
) {
9291 case sizeof (uint8_t):
9292 case sizeof (uint16_t):
9293 case sizeof (uint32_t):
9294 case sizeof (uint64_t):
9298 err
+= efunc(dp
->dtdo_len
- 1, "bad return size");
9302 for (i
= 0; i
< dp
->dtdo_varlen
&& err
== 0; i
++) {
9303 dtrace_difv_t
*v
= &dp
->dtdo_vartab
[i
], *existing
= NULL
;
9304 dtrace_diftype_t
*vt
, *et
;
9305 #if !defined(__APPLE__) /* Quiet compiler warnings */
9310 #endif /* __APPLE__ */
9312 if (v
->dtdv_scope
!= DIFV_SCOPE_GLOBAL
&&
9313 v
->dtdv_scope
!= DIFV_SCOPE_THREAD
&&
9314 v
->dtdv_scope
!= DIFV_SCOPE_LOCAL
) {
9315 err
+= efunc(i
, "unrecognized variable scope %d\n",
9320 if (v
->dtdv_kind
!= DIFV_KIND_ARRAY
&&
9321 v
->dtdv_kind
!= DIFV_KIND_SCALAR
) {
9322 err
+= efunc(i
, "unrecognized variable type %d\n",
9327 if ((id
= v
->dtdv_id
) > DIF_VARIABLE_MAX
) {
9328 err
+= efunc(i
, "%d exceeds variable id limit\n", id
);
9332 if (id
< DIF_VAR_OTHER_UBASE
)
9336 * For user-defined variables, we need to check that this
9337 * definition is identical to any previous definition that we
9340 ndx
= id
- DIF_VAR_OTHER_UBASE
;
9342 switch (v
->dtdv_scope
) {
9343 case DIFV_SCOPE_GLOBAL
:
9344 if (ndx
< vstate
->dtvs_nglobals
) {
9345 dtrace_statvar_t
*svar
;
9347 if ((svar
= vstate
->dtvs_globals
[ndx
]) != NULL
)
9348 existing
= &svar
->dtsv_var
;
9353 case DIFV_SCOPE_THREAD
:
9354 if (ndx
< vstate
->dtvs_ntlocals
)
9355 existing
= &vstate
->dtvs_tlocals
[ndx
];
9358 case DIFV_SCOPE_LOCAL
:
9359 if (ndx
< vstate
->dtvs_nlocals
) {
9360 dtrace_statvar_t
*svar
;
9362 if ((svar
= vstate
->dtvs_locals
[ndx
]) != NULL
)
9363 existing
= &svar
->dtsv_var
;
9371 if (vt
->dtdt_flags
& DIF_TF_BYREF
) {
9372 if (vt
->dtdt_size
== 0) {
9373 err
+= efunc(i
, "zero-sized variable\n");
9377 if (v
->dtdv_scope
== DIFV_SCOPE_GLOBAL
&&
9378 vt
->dtdt_size
> dtrace_global_maxsize
) {
9379 err
+= efunc(i
, "oversized by-ref global\n");
9384 if (existing
== NULL
|| existing
->dtdv_id
== 0)
9387 ASSERT(existing
->dtdv_id
== v
->dtdv_id
);
9388 ASSERT(existing
->dtdv_scope
== v
->dtdv_scope
);
9390 if (existing
->dtdv_kind
!= v
->dtdv_kind
)
9391 err
+= efunc(i
, "%d changed variable kind\n", id
);
9393 et
= &existing
->dtdv_type
;
9395 if (vt
->dtdt_flags
!= et
->dtdt_flags
) {
9396 err
+= efunc(i
, "%d changed variable type flags\n", id
);
9400 if (vt
->dtdt_size
!= 0 && vt
->dtdt_size
!= et
->dtdt_size
) {
9401 err
+= efunc(i
, "%d changed variable type size\n", id
);
9410 * Validate a DTrace DIF object that it is to be used as a helper. Helpers
9411 * are much more constrained than normal DIFOs. Specifically, they may
9414 * 1. Make calls to subroutines other than copyin(), copyinstr() or
9415 * miscellaneous string routines
9416 * 2. Access DTrace variables other than the args[] array, and the
9417 * curthread, pid, ppid, tid, execname, zonename, uid and gid variables.
9418 * 3. Have thread-local variables.
9419 * 4. Have dynamic variables.
9422 dtrace_difo_validate_helper(dtrace_difo_t
*dp
)
9424 int (*efunc
)(uint_t pc
, const char *, ...) = dtrace_difo_err
;
9428 for (pc
= 0; pc
< dp
->dtdo_len
; pc
++) {
9429 dif_instr_t instr
= dp
->dtdo_buf
[pc
];
9431 uint_t v
= DIF_INSTR_VAR(instr
);
9432 uint_t subr
= DIF_INSTR_SUBR(instr
);
9433 uint_t op
= DIF_INSTR_OP(instr
);
9488 case DIF_OP_FLUSHTS
:
9500 if (v
>= DIF_VAR_OTHER_UBASE
)
9503 if (v
>= DIF_VAR_ARG0
&& v
<= DIF_VAR_ARG9
)
9506 if (v
== DIF_VAR_CURTHREAD
|| v
== DIF_VAR_PID
||
9507 v
== DIF_VAR_PPID
|| v
== DIF_VAR_TID
||
9508 v
== DIF_VAR_EXECNAME
|| v
== DIF_VAR_ZONENAME
||
9509 v
== DIF_VAR_UID
|| v
== DIF_VAR_GID
)
9512 err
+= efunc(pc
, "illegal variable %u\n", v
);
9519 err
+= efunc(pc
, "illegal dynamic variable load\n");
9525 err
+= efunc(pc
, "illegal dynamic variable store\n");
9529 if (subr
== DIF_SUBR_ALLOCA
||
9530 subr
== DIF_SUBR_BCOPY
||
9531 subr
== DIF_SUBR_COPYIN
||
9532 subr
== DIF_SUBR_COPYINTO
||
9533 subr
== DIF_SUBR_COPYINSTR
||
9534 subr
== DIF_SUBR_INDEX
||
9535 subr
== DIF_SUBR_INET_NTOA
||
9536 subr
== DIF_SUBR_INET_NTOA6
||
9537 subr
== DIF_SUBR_INET_NTOP
||
9538 subr
== DIF_SUBR_LLTOSTR
||
9539 subr
== DIF_SUBR_RINDEX
||
9540 subr
== DIF_SUBR_STRCHR
||
9541 subr
== DIF_SUBR_STRJOIN
||
9542 subr
== DIF_SUBR_STRRCHR
||
9543 subr
== DIF_SUBR_STRSTR
||
9544 #if defined(__APPLE__)
9545 subr
== DIF_SUBR_COREPROFILE
||
9546 #endif /* __APPLE__ */
9547 subr
== DIF_SUBR_HTONS
||
9548 subr
== DIF_SUBR_HTONL
||
9549 subr
== DIF_SUBR_HTONLL
||
9550 subr
== DIF_SUBR_NTOHS
||
9551 subr
== DIF_SUBR_NTOHL
||
9552 subr
== DIF_SUBR_NTOHLL
)
9555 err
+= efunc(pc
, "invalid subr %u\n", subr
);
9559 err
+= efunc(pc
, "invalid opcode %u\n",
9560 DIF_INSTR_OP(instr
));
9568 * Returns 1 if the expression in the DIF object can be cached on a per-thread
9572 dtrace_difo_cacheable(dtrace_difo_t
*dp
)
9574 #if !defined(__APPLE__) /* Quiet compiler warnings */
9578 #endif /* __APPLE__ */
9583 for (i
= 0; i
< dp
->dtdo_varlen
; i
++) {
9584 dtrace_difv_t
*v
= &dp
->dtdo_vartab
[i
];
9586 if (v
->dtdv_scope
!= DIFV_SCOPE_GLOBAL
)
9589 switch (v
->dtdv_id
) {
9590 case DIF_VAR_CURTHREAD
:
9593 case DIF_VAR_EXECNAME
:
9594 case DIF_VAR_ZONENAME
:
9603 * This DIF object may be cacheable. Now we need to look for any
9604 * array loading instructions, any memory loading instructions, or
9605 * any stores to thread-local variables.
9607 for (i
= 0; i
< dp
->dtdo_len
; i
++) {
9608 uint_t op
= DIF_INSTR_OP(dp
->dtdo_buf
[i
]);
9610 if ((op
>= DIF_OP_LDSB
&& op
<= DIF_OP_LDX
) ||
9611 (op
>= DIF_OP_ULDSB
&& op
<= DIF_OP_ULDX
) ||
9612 (op
>= DIF_OP_RLDSB
&& op
<= DIF_OP_RLDX
) ||
9613 op
== DIF_OP_LDGA
|| op
== DIF_OP_STTS
)
9621 dtrace_difo_hold(dtrace_difo_t
*dp
)
9623 #if !defined(__APPLE__) /* Quiet compiler warnings */
9627 #endif /* __APPLE__ */
9629 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
9632 ASSERT(dp
->dtdo_refcnt
!= 0);
9635 * We need to check this DIF object for references to the variable
9636 * DIF_VAR_VTIMESTAMP.
9638 for (i
= 0; i
< dp
->dtdo_varlen
; i
++) {
9639 dtrace_difv_t
*v
= &dp
->dtdo_vartab
[i
];
9641 if (v
->dtdv_id
!= DIF_VAR_VTIMESTAMP
)
9644 if (dtrace_vtime_references
++ == 0)
9645 dtrace_vtime_enable();
9650 * This routine calculates the dynamic variable chunksize for a given DIF
9651 * object. The calculation is not fool-proof, and can probably be tricked by
9652 * malicious DIF -- but it works for all compiler-generated DIF. Because this
9653 * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail
9654 * if a dynamic variable size exceeds the chunksize.
9657 dtrace_difo_chunksize(dtrace_difo_t
*dp
, dtrace_vstate_t
*vstate
)
9659 #if !defined(__APPLE__) /* Quiet compiler warnings */
9663 #endif /* __APPLE__ */
9664 dtrace_key_t tupregs
[DIF_DTR_NREGS
+ 2]; /* +2 for thread and id */
9665 const dif_instr_t
*text
= dp
->dtdo_buf
;
9671 for (pc
= 0; pc
< dp
->dtdo_len
; pc
++) {
9672 dif_instr_t instr
= text
[pc
];
9673 uint_t op
= DIF_INSTR_OP(instr
);
9674 uint_t rd
= DIF_INSTR_RD(instr
);
9675 uint_t r1
= DIF_INSTR_R1(instr
);
9679 dtrace_key_t
*key
= tupregs
;
9683 sval
= dp
->dtdo_inttab
[DIF_INSTR_INTEGER(instr
)];
9688 key
= &tupregs
[DIF_DTR_NREGS
];
9689 key
[0].dttk_size
= 0;
9690 key
[1].dttk_size
= 0;
9692 scope
= DIFV_SCOPE_THREAD
;
9699 if (DIF_INSTR_OP(instr
) == DIF_OP_STTAA
)
9700 key
[nkeys
++].dttk_size
= 0;
9702 key
[nkeys
++].dttk_size
= 0;
9704 if (op
== DIF_OP_STTAA
) {
9705 scope
= DIFV_SCOPE_THREAD
;
9707 scope
= DIFV_SCOPE_GLOBAL
;
9713 if (ttop
== DIF_DTR_NREGS
)
9716 if ((srd
== 0 || sval
== 0) && r1
== DIF_TYPE_STRING
) {
9718 * If the register for the size of the "pushtr"
9719 * is %r0 (or the value is 0) and the type is
9720 * a string, we'll use the system-wide default
9723 tupregs
[ttop
++].dttk_size
=
9724 dtrace_strsize_default
;
9729 tupregs
[ttop
++].dttk_size
= sval
;
9735 if (ttop
== DIF_DTR_NREGS
)
9738 tupregs
[ttop
++].dttk_size
= 0;
9741 case DIF_OP_FLUSHTS
:
9758 * We have a dynamic variable allocation; calculate its size.
9760 for (ksize
= 0, i
= 0; i
< nkeys
; i
++)
9761 ksize
+= P2ROUNDUP(key
[i
].dttk_size
, sizeof (uint64_t));
9763 size
= sizeof (dtrace_dynvar_t
);
9764 size
+= sizeof (dtrace_key_t
) * (nkeys
- 1);
9768 * Now we need to determine the size of the stored data.
9770 id
= DIF_INSTR_VAR(instr
);
9772 for (i
= 0; i
< dp
->dtdo_varlen
; i
++) {
9773 dtrace_difv_t
*v
= &dp
->dtdo_vartab
[i
];
9775 if (v
->dtdv_id
== id
&& v
->dtdv_scope
== scope
) {
9776 size
+= v
->dtdv_type
.dtdt_size
;
9781 if (i
== dp
->dtdo_varlen
)
9785 * We have the size. If this is larger than the chunk size
9786 * for our dynamic variable state, reset the chunk size.
9788 size
= P2ROUNDUP(size
, sizeof (uint64_t));
9790 if (size
> vstate
->dtvs_dynvars
.dtds_chunksize
)
9791 vstate
->dtvs_dynvars
.dtds_chunksize
= size
;
9796 dtrace_difo_init(dtrace_difo_t
*dp
, dtrace_vstate_t
*vstate
)
9798 #if !defined(__APPLE__) /* Quiet compiler warnings */
9799 int i
, oldsvars
, osz
, nsz
, otlocals
, ntlocals
;
9802 int oldsvars
, osz
, nsz
, otlocals
, ntlocals
;
9804 #endif /* __APPLE__ */
9806 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
9807 ASSERT(dp
->dtdo_buf
!= NULL
&& dp
->dtdo_len
!= 0);
9809 for (i
= 0; i
< dp
->dtdo_varlen
; i
++) {
9810 dtrace_difv_t
*v
= &dp
->dtdo_vartab
[i
];
9811 #if !defined(__APPLE__) /* Quiet compiler warnings */
9812 dtrace_statvar_t
*svar
, ***svarp
;
9814 dtrace_statvar_t
*svar
;
9815 dtrace_statvar_t
***svarp
= NULL
;
9816 #endif /* __APPLE__ */
9818 uint8_t scope
= v
->dtdv_scope
;
9819 int *np
= (int *)NULL
;
9821 if ((id
= v
->dtdv_id
) < DIF_VAR_OTHER_UBASE
)
9824 id
-= DIF_VAR_OTHER_UBASE
;
9827 case DIFV_SCOPE_THREAD
:
9828 #if !defined(__APPLE__) /* Quiet compiler warnings */
9829 while (id
>= (otlocals
= vstate
->dtvs_ntlocals
)) {
9831 while (id
>= (uint_t
)(otlocals
= vstate
->dtvs_ntlocals
)) {
9832 #endif /* __APPLE__ */
9833 dtrace_difv_t
*tlocals
;
9835 if ((ntlocals
= (otlocals
<< 1)) == 0)
9838 osz
= otlocals
* sizeof (dtrace_difv_t
);
9839 nsz
= ntlocals
* sizeof (dtrace_difv_t
);
9841 tlocals
= kmem_zalloc(nsz
, KM_SLEEP
);
9844 bcopy(vstate
->dtvs_tlocals
,
9846 kmem_free(vstate
->dtvs_tlocals
, osz
);
9849 vstate
->dtvs_tlocals
= tlocals
;
9850 vstate
->dtvs_ntlocals
= ntlocals
;
9853 vstate
->dtvs_tlocals
[id
] = *v
;
9856 case DIFV_SCOPE_LOCAL
:
9857 np
= &vstate
->dtvs_nlocals
;
9858 svarp
= &vstate
->dtvs_locals
;
9860 if (v
->dtdv_type
.dtdt_flags
& DIF_TF_BYREF
)
9861 dsize
= (int)NCPU
* (v
->dtdv_type
.dtdt_size
+
9864 dsize
= (int)NCPU
* sizeof (uint64_t);
9868 case DIFV_SCOPE_GLOBAL
:
9869 np
= &vstate
->dtvs_nglobals
;
9870 svarp
= &vstate
->dtvs_globals
;
9872 if (v
->dtdv_type
.dtdt_flags
& DIF_TF_BYREF
)
9873 dsize
= v
->dtdv_type
.dtdt_size
+
9882 #if !defined(__APPLE__) /* Quiet compiler warnings */
9883 while (id
>= (oldsvars
= *np
)) {
9885 while (id
>= (uint_t
)(oldsvars
= *np
)) {
9886 #endif /* __APPLE__ */
9887 dtrace_statvar_t
**statics
;
9888 int newsvars
, oldsize
, newsize
;
9890 if ((newsvars
= (oldsvars
<< 1)) == 0)
9893 oldsize
= oldsvars
* sizeof (dtrace_statvar_t
*);
9894 newsize
= newsvars
* sizeof (dtrace_statvar_t
*);
9896 statics
= kmem_zalloc(newsize
, KM_SLEEP
);
9899 bcopy(*svarp
, statics
, oldsize
);
9900 kmem_free(*svarp
, oldsize
);
9907 if ((svar
= (*svarp
)[id
]) == NULL
) {
9908 svar
= kmem_zalloc(sizeof (dtrace_statvar_t
), KM_SLEEP
);
9909 svar
->dtsv_var
= *v
;
9911 if ((svar
->dtsv_size
= dsize
) != 0) {
9912 svar
->dtsv_data
= (uint64_t)(uintptr_t)
9913 kmem_zalloc(dsize
, KM_SLEEP
);
9916 (*svarp
)[id
] = svar
;
9919 svar
->dtsv_refcnt
++;
9922 dtrace_difo_chunksize(dp
, vstate
);
9923 dtrace_difo_hold(dp
);
9926 static dtrace_difo_t
*
9927 dtrace_difo_duplicate(dtrace_difo_t
*dp
, dtrace_vstate_t
*vstate
)
9932 ASSERT(dp
->dtdo_buf
!= NULL
);
9933 ASSERT(dp
->dtdo_refcnt
!= 0);
9935 new = kmem_zalloc(sizeof (dtrace_difo_t
), KM_SLEEP
);
9937 ASSERT(dp
->dtdo_buf
!= NULL
);
9938 sz
= dp
->dtdo_len
* sizeof (dif_instr_t
);
9939 new->dtdo_buf
= kmem_alloc(sz
, KM_SLEEP
);
9940 bcopy(dp
->dtdo_buf
, new->dtdo_buf
, sz
);
9941 new->dtdo_len
= dp
->dtdo_len
;
9943 if (dp
->dtdo_strtab
!= NULL
) {
9944 ASSERT(dp
->dtdo_strlen
!= 0);
9945 new->dtdo_strtab
= kmem_alloc(dp
->dtdo_strlen
, KM_SLEEP
);
9946 bcopy(dp
->dtdo_strtab
, new->dtdo_strtab
, dp
->dtdo_strlen
);
9947 new->dtdo_strlen
= dp
->dtdo_strlen
;
9950 if (dp
->dtdo_inttab
!= NULL
) {
9951 ASSERT(dp
->dtdo_intlen
!= 0);
9952 sz
= dp
->dtdo_intlen
* sizeof (uint64_t);
9953 new->dtdo_inttab
= kmem_alloc(sz
, KM_SLEEP
);
9954 bcopy(dp
->dtdo_inttab
, new->dtdo_inttab
, sz
);
9955 new->dtdo_intlen
= dp
->dtdo_intlen
;
9958 if (dp
->dtdo_vartab
!= NULL
) {
9959 ASSERT(dp
->dtdo_varlen
!= 0);
9960 sz
= dp
->dtdo_varlen
* sizeof (dtrace_difv_t
);
9961 new->dtdo_vartab
= kmem_alloc(sz
, KM_SLEEP
);
9962 bcopy(dp
->dtdo_vartab
, new->dtdo_vartab
, sz
);
9963 new->dtdo_varlen
= dp
->dtdo_varlen
;
9966 dtrace_difo_init(new, vstate
);
9971 dtrace_difo_destroy(dtrace_difo_t
*dp
, dtrace_vstate_t
*vstate
)
9973 #if !defined(__APPLE__) /* Quiet compiler warnings */
9977 #endif /* __APPLE__ */
9979 ASSERT(dp
->dtdo_refcnt
== 0);
9981 for (i
= 0; i
< dp
->dtdo_varlen
; i
++) {
9982 dtrace_difv_t
*v
= &dp
->dtdo_vartab
[i
];
9983 #if !defined(__APPLE__) /* Quiet compiler warnings */
9984 dtrace_statvar_t
*svar
, **svarp
;
9986 uint8_t scope
= v
->dtdv_scope
;
9989 dtrace_statvar_t
*svar
;
9990 dtrace_statvar_t
**svarp
= NULL
;
9992 uint8_t scope
= v
->dtdv_scope
;
9994 #endif /* __APPLE__ */
9997 case DIFV_SCOPE_THREAD
:
10000 case DIFV_SCOPE_LOCAL
:
10001 np
= &vstate
->dtvs_nlocals
;
10002 svarp
= vstate
->dtvs_locals
;
10005 case DIFV_SCOPE_GLOBAL
:
10006 np
= &vstate
->dtvs_nglobals
;
10007 svarp
= vstate
->dtvs_globals
;
10014 if ((id
= v
->dtdv_id
) < DIF_VAR_OTHER_UBASE
)
10017 id
-= DIF_VAR_OTHER_UBASE
;
10019 #if !defined(__APPLE__) /* Quiet compiler warnings */
10022 ASSERT(id
< (uint_t
)*np
);
10023 #endif /* __APPLE__ */
10026 ASSERT(svar
!= NULL
);
10027 ASSERT(svar
->dtsv_refcnt
> 0);
10029 if (--svar
->dtsv_refcnt
> 0)
10032 if (svar
->dtsv_size
!= 0) {
10033 ASSERT(svar
->dtsv_data
!= NULL
);
10034 kmem_free((void *)(uintptr_t)svar
->dtsv_data
,
10038 kmem_free(svar
, sizeof (dtrace_statvar_t
));
10042 kmem_free(dp
->dtdo_buf
, dp
->dtdo_len
* sizeof (dif_instr_t
));
10043 kmem_free(dp
->dtdo_inttab
, dp
->dtdo_intlen
* sizeof (uint64_t));
10044 kmem_free(dp
->dtdo_strtab
, dp
->dtdo_strlen
);
10045 kmem_free(dp
->dtdo_vartab
, dp
->dtdo_varlen
* sizeof (dtrace_difv_t
));
10047 kmem_free(dp
, sizeof (dtrace_difo_t
));
10051 dtrace_difo_release(dtrace_difo_t
*dp
, dtrace_vstate_t
*vstate
)
10053 #if !defined(__APPLE__) /* Quiet compiler warnings */
10057 #endif /* __APPLE__ */
10059 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
10060 ASSERT(dp
->dtdo_refcnt
!= 0);
10062 for (i
= 0; i
< dp
->dtdo_varlen
; i
++) {
10063 dtrace_difv_t
*v
= &dp
->dtdo_vartab
[i
];
10065 if (v
->dtdv_id
!= DIF_VAR_VTIMESTAMP
)
10068 ASSERT(dtrace_vtime_references
> 0);
10069 if (--dtrace_vtime_references
== 0)
10070 dtrace_vtime_disable();
10073 if (--dp
->dtdo_refcnt
== 0)
10074 dtrace_difo_destroy(dp
, vstate
);
10078 * DTrace Format Functions
10081 dtrace_format_add(dtrace_state_t
*state
, char *str
)
10084 uint16_t ndx
, len
= strlen(str
) + 1;
10086 fmt
= kmem_zalloc(len
, KM_SLEEP
);
10087 bcopy(str
, fmt
, len
);
10089 for (ndx
= 0; ndx
< state
->dts_nformats
; ndx
++) {
10090 if (state
->dts_formats
[ndx
] == NULL
) {
10091 state
->dts_formats
[ndx
] = fmt
;
10096 if (state
->dts_nformats
== USHRT_MAX
) {
10098 * This is only likely if a denial-of-service attack is being
10099 * attempted. As such, it's okay to fail silently here.
10101 kmem_free(fmt
, len
);
10106 * For simplicity, we always resize the formats array to be exactly the
10107 * number of formats.
10109 ndx
= state
->dts_nformats
++;
10110 new = kmem_alloc((ndx
+ 1) * sizeof (char *), KM_SLEEP
);
10112 if (state
->dts_formats
!= NULL
) {
10114 bcopy(state
->dts_formats
, new, ndx
* sizeof (char *));
10115 kmem_free(state
->dts_formats
, ndx
* sizeof (char *));
10118 state
->dts_formats
= new;
10119 state
->dts_formats
[ndx
] = fmt
;
10125 dtrace_format_remove(dtrace_state_t
*state
, uint16_t format
)
10129 ASSERT(state
->dts_formats
!= NULL
);
10130 ASSERT(format
<= state
->dts_nformats
);
10131 ASSERT(state
->dts_formats
[format
- 1] != NULL
);
10133 fmt
= state
->dts_formats
[format
- 1];
10134 kmem_free(fmt
, strlen(fmt
) + 1);
10135 state
->dts_formats
[format
- 1] = NULL
;
10139 dtrace_format_destroy(dtrace_state_t
*state
)
10143 if (state
->dts_nformats
== 0) {
10144 ASSERT(state
->dts_formats
== NULL
);
10148 ASSERT(state
->dts_formats
!= NULL
);
10150 for (i
= 0; i
< state
->dts_nformats
; i
++) {
10151 char *fmt
= state
->dts_formats
[i
];
10156 kmem_free(fmt
, strlen(fmt
) + 1);
10159 kmem_free(state
->dts_formats
, state
->dts_nformats
* sizeof (char *));
10160 state
->dts_nformats
= 0;
10161 state
->dts_formats
= NULL
;
10165 * DTrace Predicate Functions
10167 static dtrace_predicate_t
*
10168 dtrace_predicate_create(dtrace_difo_t
*dp
)
10170 dtrace_predicate_t
*pred
;
10172 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
10173 ASSERT(dp
->dtdo_refcnt
!= 0);
10175 pred
= kmem_zalloc(sizeof (dtrace_predicate_t
), KM_SLEEP
);
10176 pred
->dtp_difo
= dp
;
10177 pred
->dtp_refcnt
= 1;
10179 if (!dtrace_difo_cacheable(dp
))
10182 if (dtrace_predcache_id
== DTRACE_CACHEIDNONE
) {
10184 * This is only theoretically possible -- we have had 2^32
10185 * cacheable predicates on this machine. We cannot allow any
10186 * more predicates to become cacheable: as unlikely as it is,
10187 * there may be a thread caching a (now stale) predicate cache
10188 * ID. (N.B.: the temptation is being successfully resisted to
10189 * have this cmn_err() "Holy shit -- we executed this code!")
10194 pred
->dtp_cacheid
= dtrace_predcache_id
++;
10200 dtrace_predicate_hold(dtrace_predicate_t
*pred
)
10202 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
10203 ASSERT(pred
->dtp_difo
!= NULL
&& pred
->dtp_difo
->dtdo_refcnt
!= 0);
10204 ASSERT(pred
->dtp_refcnt
> 0);
10206 pred
->dtp_refcnt
++;
10210 dtrace_predicate_release(dtrace_predicate_t
*pred
, dtrace_vstate_t
*vstate
)
10212 dtrace_difo_t
*dp
= pred
->dtp_difo
;
10213 #pragma unused(dp) /* __APPLE__ */
10215 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
10216 ASSERT(dp
!= NULL
&& dp
->dtdo_refcnt
!= 0);
10217 ASSERT(pred
->dtp_refcnt
> 0);
10219 if (--pred
->dtp_refcnt
== 0) {
10220 dtrace_difo_release(pred
->dtp_difo
, vstate
);
10221 kmem_free(pred
, sizeof (dtrace_predicate_t
));
10226 * DTrace Action Description Functions
10228 static dtrace_actdesc_t
*
10229 dtrace_actdesc_create(dtrace_actkind_t kind
, uint32_t ntuple
,
10230 uint64_t uarg
, uint64_t arg
)
10232 dtrace_actdesc_t
*act
;
10234 ASSERT(!DTRACEACT_ISPRINTFLIKE(kind
) || (arg
!= NULL
&&
10235 arg
>= KERNELBASE
) || (arg
== NULL
&& kind
== DTRACEACT_PRINTA
));
10237 act
= kmem_zalloc(sizeof (dtrace_actdesc_t
), KM_SLEEP
);
10238 act
->dtad_kind
= kind
;
10239 act
->dtad_ntuple
= ntuple
;
10240 act
->dtad_uarg
= uarg
;
10241 act
->dtad_arg
= arg
;
10242 act
->dtad_refcnt
= 1;
10248 dtrace_actdesc_hold(dtrace_actdesc_t
*act
)
10250 ASSERT(act
->dtad_refcnt
>= 1);
10251 act
->dtad_refcnt
++;
10255 dtrace_actdesc_release(dtrace_actdesc_t
*act
, dtrace_vstate_t
*vstate
)
10257 dtrace_actkind_t kind
= act
->dtad_kind
;
10260 ASSERT(act
->dtad_refcnt
>= 1);
10262 if (--act
->dtad_refcnt
!= 0)
10265 if ((dp
= act
->dtad_difo
) != NULL
)
10266 dtrace_difo_release(dp
, vstate
);
10268 if (DTRACEACT_ISPRINTFLIKE(kind
)) {
10269 char *str
= (char *)(uintptr_t)act
->dtad_arg
;
10271 ASSERT((str
!= NULL
&& (uintptr_t)str
>= KERNELBASE
) ||
10272 (str
== NULL
&& act
->dtad_kind
== DTRACEACT_PRINTA
));
10275 kmem_free(str
, strlen(str
) + 1);
10278 kmem_free(act
, sizeof (dtrace_actdesc_t
));
10282 * DTrace ECB Functions
10284 static dtrace_ecb_t
*
10285 dtrace_ecb_add(dtrace_state_t
*state
, dtrace_probe_t
*probe
)
10288 dtrace_epid_t epid
;
10290 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
10292 ecb
= kmem_zalloc(sizeof (dtrace_ecb_t
), KM_SLEEP
);
10293 ecb
->dte_predicate
= NULL
;
10294 ecb
->dte_probe
= probe
;
10297 * The default size is the size of the default action: recording
10300 ecb
->dte_size
= ecb
->dte_needed
= sizeof (dtrace_epid_t
);
10301 ecb
->dte_alignment
= sizeof (dtrace_epid_t
);
10303 epid
= state
->dts_epid
++;
10305 #if !defined(__APPLE__) /* Quiet compiler warnings */
10306 if (epid
- 1 >= state
->dts_necbs
) {
10308 if (epid
- 1 >= (dtrace_epid_t
)state
->dts_necbs
) {
10309 #endif /* __APPLE__ */
10310 dtrace_ecb_t
**oecbs
= state
->dts_ecbs
, **ecbs
;
10311 int necbs
= state
->dts_necbs
<< 1;
10313 #if !defined(__APPLE__) /* Quiet compiler warnings */
10314 ASSERT(epid
== state
->dts_necbs
+ 1);
10316 ASSERT(epid
== (dtrace_epid_t
)state
->dts_necbs
+ 1);
10317 #endif /* __APPLE__ */
10320 ASSERT(oecbs
== NULL
);
10324 ecbs
= kmem_zalloc(necbs
* sizeof (*ecbs
), KM_SLEEP
);
10327 bcopy(oecbs
, ecbs
, state
->dts_necbs
* sizeof (*ecbs
));
10329 dtrace_membar_producer();
10330 state
->dts_ecbs
= ecbs
;
10332 if (oecbs
!= NULL
) {
10334 * If this state is active, we must dtrace_sync()
10335 * before we can free the old dts_ecbs array: we're
10336 * coming in hot, and there may be active ring
10337 * buffer processing (which indexes into the dts_ecbs
10338 * array) on another CPU.
10340 if (state
->dts_activity
!= DTRACE_ACTIVITY_INACTIVE
)
10343 kmem_free(oecbs
, state
->dts_necbs
* sizeof (*ecbs
));
10346 dtrace_membar_producer();
10347 state
->dts_necbs
= necbs
;
10350 ecb
->dte_state
= state
;
10352 ASSERT(state
->dts_ecbs
[epid
- 1] == NULL
);
10353 dtrace_membar_producer();
10354 state
->dts_ecbs
[(ecb
->dte_epid
= epid
) - 1] = ecb
;
10360 dtrace_ecb_enable(dtrace_ecb_t
*ecb
)
10362 dtrace_probe_t
*probe
= ecb
->dte_probe
;
10364 lck_mtx_assert(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
10365 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
10366 ASSERT(ecb
->dte_next
== NULL
);
10368 if (probe
== NULL
) {
10370 * This is the NULL probe -- there's nothing to do.
10375 if (probe
->dtpr_ecb
== NULL
) {
10376 dtrace_provider_t
*prov
= probe
->dtpr_provider
;
10379 * We're the first ECB on this probe.
10381 probe
->dtpr_ecb
= probe
->dtpr_ecb_last
= ecb
;
10383 if (ecb
->dte_predicate
!= NULL
)
10384 probe
->dtpr_predcache
= ecb
->dte_predicate
->dtp_cacheid
;
10386 prov
->dtpv_pops
.dtps_enable(prov
->dtpv_arg
,
10387 probe
->dtpr_id
, probe
->dtpr_arg
);
10390 * This probe is already active. Swing the last pointer to
10391 * point to the new ECB, and issue a dtrace_sync() to assure
10392 * that all CPUs have seen the change.
10394 ASSERT(probe
->dtpr_ecb_last
!= NULL
);
10395 probe
->dtpr_ecb_last
->dte_next
= ecb
;
10396 probe
->dtpr_ecb_last
= ecb
;
10397 probe
->dtpr_predcache
= 0;
10404 dtrace_ecb_resize(dtrace_ecb_t
*ecb
)
10406 uint32_t maxalign
= sizeof (dtrace_epid_t
);
10407 uint32_t align
= sizeof (uint8_t), offs
, diff
;
10408 dtrace_action_t
*act
;
10410 uint32_t aggbase
= UINT32_MAX
;
10411 dtrace_state_t
*state
= ecb
->dte_state
;
10414 * If we record anything, we always record the epid. (And we always
10415 * record it first.)
10417 offs
= sizeof (dtrace_epid_t
);
10418 ecb
->dte_size
= ecb
->dte_needed
= sizeof (dtrace_epid_t
);
10420 for (act
= ecb
->dte_action
; act
!= NULL
; act
= act
->dta_next
) {
10421 dtrace_recdesc_t
*rec
= &act
->dta_rec
;
10423 if ((align
= rec
->dtrd_alignment
) > maxalign
)
10426 if (!wastuple
&& act
->dta_intuple
) {
10428 * This is the first record in a tuple. Align the
10429 * offset to be at offset 4 in an 8-byte aligned
10432 diff
= offs
+ sizeof (dtrace_aggid_t
);
10434 if ((diff
= (diff
& (sizeof (uint64_t) - 1))))
10435 offs
+= sizeof (uint64_t) - diff
;
10437 aggbase
= offs
- sizeof (dtrace_aggid_t
);
10438 ASSERT(!(aggbase
& (sizeof (uint64_t) - 1)));
10442 if (rec
->dtrd_size
!= 0 && (diff
= (offs
& (align
- 1)))) {
10444 * The current offset is not properly aligned; align it.
10446 offs
+= align
- diff
;
10449 rec
->dtrd_offset
= offs
;
10451 if (offs
+ rec
->dtrd_size
> ecb
->dte_needed
) {
10452 ecb
->dte_needed
= offs
+ rec
->dtrd_size
;
10454 if (ecb
->dte_needed
> state
->dts_needed
)
10455 state
->dts_needed
= ecb
->dte_needed
;
10458 if (DTRACEACT_ISAGG(act
->dta_kind
)) {
10459 dtrace_aggregation_t
*agg
= (dtrace_aggregation_t
*)act
;
10460 dtrace_action_t
*first
= agg
->dtag_first
, *prev
;
10462 ASSERT(rec
->dtrd_size
!= 0 && first
!= NULL
);
10464 ASSERT(aggbase
!= UINT32_MAX
);
10466 agg
->dtag_base
= aggbase
;
10468 while ((prev
= first
->dta_prev
) != NULL
&&
10469 DTRACEACT_ISAGG(prev
->dta_kind
)) {
10470 agg
= (dtrace_aggregation_t
*)prev
;
10471 first
= agg
->dtag_first
;
10474 if (prev
!= NULL
) {
10475 offs
= prev
->dta_rec
.dtrd_offset
+
10476 prev
->dta_rec
.dtrd_size
;
10478 offs
= sizeof (dtrace_epid_t
);
10482 if (!act
->dta_intuple
)
10483 ecb
->dte_size
= offs
+ rec
->dtrd_size
;
10485 offs
+= rec
->dtrd_size
;
10488 wastuple
= act
->dta_intuple
;
10491 if ((act
= ecb
->dte_action
) != NULL
&&
10492 !(act
->dta_kind
== DTRACEACT_SPECULATE
&& act
->dta_next
== NULL
) &&
10493 ecb
->dte_size
== sizeof (dtrace_epid_t
)) {
10495 * If the size is still sizeof (dtrace_epid_t), then all
10496 * actions store no data; set the size to 0.
10498 ecb
->dte_alignment
= maxalign
;
10502 * If the needed space is still sizeof (dtrace_epid_t), then
10503 * all actions need no additional space; set the needed
10506 if (ecb
->dte_needed
== sizeof (dtrace_epid_t
))
10507 ecb
->dte_needed
= 0;
10513 * Set our alignment, and make sure that the dte_size and dte_needed
10514 * are aligned to the size of an EPID.
10516 ecb
->dte_alignment
= maxalign
;
10517 ecb
->dte_size
= (ecb
->dte_size
+ (sizeof (dtrace_epid_t
) - 1)) &
10518 ~(sizeof (dtrace_epid_t
) - 1);
10519 ecb
->dte_needed
= (ecb
->dte_needed
+ (sizeof (dtrace_epid_t
) - 1)) &
10520 ~(sizeof (dtrace_epid_t
) - 1);
10521 ASSERT(ecb
->dte_size
<= ecb
->dte_needed
);
10524 static dtrace_action_t
*
10525 dtrace_ecb_aggregation_create(dtrace_ecb_t
*ecb
, dtrace_actdesc_t
*desc
)
10527 dtrace_aggregation_t
*agg
;
10528 size_t size
= sizeof (uint64_t);
10529 int ntuple
= desc
->dtad_ntuple
;
10530 dtrace_action_t
*act
;
10531 dtrace_recdesc_t
*frec
;
10532 dtrace_aggid_t aggid
;
10533 dtrace_state_t
*state
= ecb
->dte_state
;
10535 agg
= kmem_zalloc(sizeof (dtrace_aggregation_t
), KM_SLEEP
);
10536 agg
->dtag_ecb
= ecb
;
10538 ASSERT(DTRACEACT_ISAGG(desc
->dtad_kind
));
10540 switch (desc
->dtad_kind
) {
10541 case DTRACEAGG_MIN
:
10542 agg
->dtag_initial
= INT64_MAX
;
10543 agg
->dtag_aggregate
= dtrace_aggregate_min
;
10546 case DTRACEAGG_MAX
:
10547 agg
->dtag_initial
= INT64_MIN
;
10548 agg
->dtag_aggregate
= dtrace_aggregate_max
;
10551 case DTRACEAGG_COUNT
:
10552 agg
->dtag_aggregate
= dtrace_aggregate_count
;
10555 case DTRACEAGG_QUANTIZE
:
10556 agg
->dtag_aggregate
= dtrace_aggregate_quantize
;
10557 size
= (((sizeof (uint64_t) * NBBY
) - 1) * 2 + 1) *
10561 case DTRACEAGG_LQUANTIZE
: {
10562 uint16_t step
= DTRACE_LQUANTIZE_STEP(desc
->dtad_arg
);
10563 uint16_t levels
= DTRACE_LQUANTIZE_LEVELS(desc
->dtad_arg
);
10565 agg
->dtag_initial
= desc
->dtad_arg
;
10566 agg
->dtag_aggregate
= dtrace_aggregate_lquantize
;
10568 if (step
== 0 || levels
== 0)
10571 size
= levels
* sizeof (uint64_t) + 3 * sizeof (uint64_t);
10575 case DTRACEAGG_AVG
:
10576 agg
->dtag_aggregate
= dtrace_aggregate_avg
;
10577 size
= sizeof (uint64_t) * 2;
10580 case DTRACEAGG_STDDEV
:
10581 agg
->dtag_aggregate
= dtrace_aggregate_stddev
;
10582 size
= sizeof (uint64_t) * 4;
10585 case DTRACEAGG_SUM
:
10586 agg
->dtag_aggregate
= dtrace_aggregate_sum
;
10593 agg
->dtag_action
.dta_rec
.dtrd_size
= size
;
10599 * We must make sure that we have enough actions for the n-tuple.
10601 for (act
= ecb
->dte_action_last
; act
!= NULL
; act
= act
->dta_prev
) {
10602 if (DTRACEACT_ISAGG(act
->dta_kind
))
10605 if (--ntuple
== 0) {
10607 * This is the action with which our n-tuple begins.
10609 agg
->dtag_first
= act
;
10615 * This n-tuple is short by ntuple elements. Return failure.
10617 ASSERT(ntuple
!= 0);
10619 kmem_free(agg
, sizeof (dtrace_aggregation_t
));
10624 * If the last action in the tuple has a size of zero, it's actually
10625 * an expression argument for the aggregating action.
10627 ASSERT(ecb
->dte_action_last
!= NULL
);
10628 act
= ecb
->dte_action_last
;
10630 if (act
->dta_kind
== DTRACEACT_DIFEXPR
) {
10631 ASSERT(act
->dta_difo
!= NULL
);
10633 if (act
->dta_difo
->dtdo_rtype
.dtdt_size
== 0)
10634 agg
->dtag_hasarg
= 1;
10638 * We need to allocate an id for this aggregation.
10640 aggid
= (dtrace_aggid_t
)(uintptr_t)vmem_alloc(state
->dts_aggid_arena
, 1,
10641 VM_BESTFIT
| VM_SLEEP
);
10643 #if !defined(__APPLE__) /* Quiet compiler warnings */
10644 if (aggid
- 1 >= state
->dts_naggregations
) {
10646 if (aggid
- 1 >= (dtrace_aggid_t
)state
->dts_naggregations
) {
10647 #endif /* __APPLE__ */
10648 dtrace_aggregation_t
**oaggs
= state
->dts_aggregations
;
10649 dtrace_aggregation_t
**aggs
;
10650 int naggs
= state
->dts_naggregations
<< 1;
10651 int onaggs
= state
->dts_naggregations
;
10653 #if !defined(__APPLE__) /* Quiet compiler warnings */
10654 ASSERT(aggid
== state
->dts_naggregations
+ 1);
10656 ASSERT(aggid
== (dtrace_aggid_t
)state
->dts_naggregations
+ 1);
10657 #endif /* __APPLE */
10660 ASSERT(oaggs
== NULL
);
10664 aggs
= kmem_zalloc(naggs
* sizeof (*aggs
), KM_SLEEP
);
10666 if (oaggs
!= NULL
) {
10667 bcopy(oaggs
, aggs
, onaggs
* sizeof (*aggs
));
10668 kmem_free(oaggs
, onaggs
* sizeof (*aggs
));
10671 state
->dts_aggregations
= aggs
;
10672 state
->dts_naggregations
= naggs
;
10675 ASSERT(state
->dts_aggregations
[aggid
- 1] == NULL
);
10676 state
->dts_aggregations
[(agg
->dtag_id
= aggid
) - 1] = agg
;
10678 frec
= &agg
->dtag_first
->dta_rec
;
10679 if (frec
->dtrd_alignment
< sizeof (dtrace_aggid_t
))
10680 frec
->dtrd_alignment
= sizeof (dtrace_aggid_t
);
10682 for (act
= agg
->dtag_first
; act
!= NULL
; act
= act
->dta_next
) {
10683 ASSERT(!act
->dta_intuple
);
10684 act
->dta_intuple
= 1;
10687 return (&agg
->dtag_action
);
10691 dtrace_ecb_aggregation_destroy(dtrace_ecb_t
*ecb
, dtrace_action_t
*act
)
10693 dtrace_aggregation_t
*agg
= (dtrace_aggregation_t
*)act
;
10694 dtrace_state_t
*state
= ecb
->dte_state
;
10695 dtrace_aggid_t aggid
= agg
->dtag_id
;
10697 ASSERT(DTRACEACT_ISAGG(act
->dta_kind
));
10698 vmem_free(state
->dts_aggid_arena
, (void *)(uintptr_t)aggid
, 1);
10700 ASSERT(state
->dts_aggregations
[aggid
- 1] == agg
);
10701 state
->dts_aggregations
[aggid
- 1] = NULL
;
10703 kmem_free(agg
, sizeof (dtrace_aggregation_t
));
10707 dtrace_ecb_action_add(dtrace_ecb_t
*ecb
, dtrace_actdesc_t
*desc
)
10709 dtrace_action_t
*action
, *last
;
10710 dtrace_difo_t
*dp
= desc
->dtad_difo
;
10711 uint32_t size
= 0, align
= sizeof (uint8_t), mask
;
10712 uint16_t format
= 0;
10713 dtrace_recdesc_t
*rec
;
10714 dtrace_state_t
*state
= ecb
->dte_state
;
10715 #if !defined(__APPLE__) /* Quiet compiler warnings */
10716 dtrace_optval_t
*opt
= state
->dts_options
, nframes
, strsize
;
10718 dtrace_optval_t
*opt
= state
->dts_options
;
10719 dtrace_optval_t nframes
=0, strsize
;
10720 #endif /* __APPLE__ */
10721 uint64_t arg
= desc
->dtad_arg
;
10723 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
10724 ASSERT(ecb
->dte_action
== NULL
|| ecb
->dte_action
->dta_refcnt
== 1);
10726 if (DTRACEACT_ISAGG(desc
->dtad_kind
)) {
10728 * If this is an aggregating action, there must be neither
10729 * a speculate nor a commit on the action chain.
10731 dtrace_action_t
*act
;
10733 for (act
= ecb
->dte_action
; act
!= NULL
; act
= act
->dta_next
) {
10734 if (act
->dta_kind
== DTRACEACT_COMMIT
)
10737 if (act
->dta_kind
== DTRACEACT_SPECULATE
)
10741 action
= dtrace_ecb_aggregation_create(ecb
, desc
);
10743 if (action
== NULL
)
10746 if (DTRACEACT_ISDESTRUCTIVE(desc
->dtad_kind
) ||
10747 (desc
->dtad_kind
== DTRACEACT_DIFEXPR
&&
10748 dp
!= NULL
&& dp
->dtdo_destructive
)) {
10749 state
->dts_destructive
= 1;
10752 switch (desc
->dtad_kind
) {
10753 case DTRACEACT_PRINTF
:
10754 case DTRACEACT_PRINTA
:
10755 case DTRACEACT_SYSTEM
:
10756 case DTRACEACT_FREOPEN
:
10758 * We know that our arg is a string -- turn it into a
10762 ASSERT(desc
->dtad_kind
== DTRACEACT_PRINTA
);
10765 ASSERT(arg
!= NULL
);
10766 ASSERT(arg
> KERNELBASE
);
10767 format
= dtrace_format_add(state
,
10768 (char *)(uintptr_t)arg
);
10772 case DTRACEACT_LIBACT
:
10773 case DTRACEACT_DIFEXPR
:
10774 #if defined(__APPLE__)
10775 case DTRACEACT_APPLEBINARY
:
10776 #endif /* __APPLE__ */
10780 if ((size
= dp
->dtdo_rtype
.dtdt_size
) != 0)
10783 if (dp
->dtdo_rtype
.dtdt_kind
== DIF_TYPE_STRING
) {
10784 if (!(dp
->dtdo_rtype
.dtdt_flags
& DIF_TF_BYREF
))
10787 size
= opt
[DTRACEOPT_STRSIZE
];
10792 case DTRACEACT_STACK
:
10793 if ((nframes
= arg
) == 0) {
10794 nframes
= opt
[DTRACEOPT_STACKFRAMES
];
10795 ASSERT(nframes
> 0);
10799 size
= nframes
* sizeof (pc_t
);
10802 case DTRACEACT_JSTACK
:
10803 if ((strsize
= DTRACE_USTACK_STRSIZE(arg
)) == 0)
10804 strsize
= opt
[DTRACEOPT_JSTACKSTRSIZE
];
10806 if ((nframes
= DTRACE_USTACK_NFRAMES(arg
)) == 0)
10807 nframes
= opt
[DTRACEOPT_JSTACKFRAMES
];
10809 arg
= DTRACE_USTACK_ARG(nframes
, strsize
);
10812 case DTRACEACT_USTACK
:
10813 if (desc
->dtad_kind
!= DTRACEACT_JSTACK
&&
10814 (nframes
= DTRACE_USTACK_NFRAMES(arg
)) == 0) {
10815 strsize
= DTRACE_USTACK_STRSIZE(arg
);
10816 nframes
= opt
[DTRACEOPT_USTACKFRAMES
];
10817 ASSERT(nframes
> 0);
10818 arg
= DTRACE_USTACK_ARG(nframes
, strsize
);
10822 * Save a slot for the pid.
10824 size
= (nframes
+ 1) * sizeof (uint64_t);
10825 size
+= DTRACE_USTACK_STRSIZE(arg
);
10826 size
= P2ROUNDUP(size
, (uint32_t)(sizeof (uintptr_t)));
10830 case DTRACEACT_SYM
:
10831 case DTRACEACT_MOD
:
10832 if (dp
== NULL
|| ((size
= dp
->dtdo_rtype
.dtdt_size
) !=
10833 sizeof (uint64_t)) ||
10834 (dp
->dtdo_rtype
.dtdt_flags
& DIF_TF_BYREF
))
10838 case DTRACEACT_USYM
:
10839 case DTRACEACT_UMOD
:
10840 case DTRACEACT_UADDR
:
10842 (dp
->dtdo_rtype
.dtdt_size
!= sizeof (uint64_t)) ||
10843 (dp
->dtdo_rtype
.dtdt_flags
& DIF_TF_BYREF
))
10847 * We have a slot for the pid, plus a slot for the
10848 * argument. To keep things simple (aligned with
10849 * bitness-neutral sizing), we store each as a 64-bit
10852 size
= 2 * sizeof (uint64_t);
10855 case DTRACEACT_STOP
:
10856 case DTRACEACT_BREAKPOINT
:
10857 case DTRACEACT_PANIC
:
10860 case DTRACEACT_CHILL
:
10861 case DTRACEACT_DISCARD
:
10862 case DTRACEACT_RAISE
:
10867 case DTRACEACT_EXIT
:
10869 (size
= dp
->dtdo_rtype
.dtdt_size
) != sizeof (int) ||
10870 (dp
->dtdo_rtype
.dtdt_flags
& DIF_TF_BYREF
))
10874 case DTRACEACT_SPECULATE
:
10875 if (ecb
->dte_size
> sizeof (dtrace_epid_t
))
10881 state
->dts_speculates
= 1;
10884 case DTRACEACT_COMMIT
: {
10885 dtrace_action_t
*act
= ecb
->dte_action
;
10887 for (; act
!= NULL
; act
= act
->dta_next
) {
10888 if (act
->dta_kind
== DTRACEACT_COMMIT
)
10901 if (size
!= 0 || desc
->dtad_kind
== DTRACEACT_SPECULATE
) {
10903 * If this is a data-storing action or a speculate,
10904 * we must be sure that there isn't a commit on the
10907 dtrace_action_t
*act
= ecb
->dte_action
;
10909 for (; act
!= NULL
; act
= act
->dta_next
) {
10910 if (act
->dta_kind
== DTRACEACT_COMMIT
)
10915 action
= kmem_zalloc(sizeof (dtrace_action_t
), KM_SLEEP
);
10916 action
->dta_rec
.dtrd_size
= size
;
10919 action
->dta_refcnt
= 1;
10920 rec
= &action
->dta_rec
;
10921 size
= rec
->dtrd_size
;
10923 for (mask
= sizeof (uint64_t) - 1; size
!= 0 && mask
> 0; mask
>>= 1) {
10924 if (!(size
& mask
)) {
10930 action
->dta_kind
= desc
->dtad_kind
;
10932 if ((action
->dta_difo
= dp
) != NULL
)
10933 dtrace_difo_hold(dp
);
10935 rec
->dtrd_action
= action
->dta_kind
;
10936 rec
->dtrd_arg
= arg
;
10937 rec
->dtrd_uarg
= desc
->dtad_uarg
;
10938 rec
->dtrd_alignment
= (uint16_t)align
;
10939 rec
->dtrd_format
= format
;
10941 if ((last
= ecb
->dte_action_last
) != NULL
) {
10942 ASSERT(ecb
->dte_action
!= NULL
);
10943 action
->dta_prev
= last
;
10944 last
->dta_next
= action
;
10946 ASSERT(ecb
->dte_action
== NULL
);
10947 ecb
->dte_action
= action
;
10950 ecb
->dte_action_last
= action
;
10956 dtrace_ecb_action_remove(dtrace_ecb_t
*ecb
)
10958 dtrace_action_t
*act
= ecb
->dte_action
, *next
;
10959 dtrace_vstate_t
*vstate
= &ecb
->dte_state
->dts_vstate
;
10963 if (act
!= NULL
&& act
->dta_refcnt
> 1) {
10964 ASSERT(act
->dta_next
== NULL
|| act
->dta_next
->dta_refcnt
== 1);
10967 for (; act
!= NULL
; act
= next
) {
10968 next
= act
->dta_next
;
10969 ASSERT(next
!= NULL
|| act
== ecb
->dte_action_last
);
10970 ASSERT(act
->dta_refcnt
== 1);
10972 if ((format
= act
->dta_rec
.dtrd_format
) != 0)
10973 dtrace_format_remove(ecb
->dte_state
, format
);
10975 if ((dp
= act
->dta_difo
) != NULL
)
10976 dtrace_difo_release(dp
, vstate
);
10978 if (DTRACEACT_ISAGG(act
->dta_kind
)) {
10979 dtrace_ecb_aggregation_destroy(ecb
, act
);
10981 kmem_free(act
, sizeof (dtrace_action_t
));
10986 ecb
->dte_action
= NULL
;
10987 ecb
->dte_action_last
= NULL
;
10988 ecb
->dte_size
= sizeof (dtrace_epid_t
);
10992 dtrace_ecb_disable(dtrace_ecb_t
*ecb
)
10995 * We disable the ECB by removing it from its probe.
10997 dtrace_ecb_t
*pecb
, *prev
= NULL
;
10998 dtrace_probe_t
*probe
= ecb
->dte_probe
;
11000 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
11002 if (probe
== NULL
) {
11004 * This is the NULL probe; there is nothing to disable.
11009 for (pecb
= probe
->dtpr_ecb
; pecb
!= NULL
; pecb
= pecb
->dte_next
) {
11015 ASSERT(pecb
!= NULL
);
11017 if (prev
== NULL
) {
11018 probe
->dtpr_ecb
= ecb
->dte_next
;
11020 prev
->dte_next
= ecb
->dte_next
;
11023 if (ecb
== probe
->dtpr_ecb_last
) {
11024 ASSERT(ecb
->dte_next
== NULL
);
11025 probe
->dtpr_ecb_last
= prev
;
11029 * The ECB has been disconnected from the probe; now sync to assure
11030 * that all CPUs have seen the change before returning.
11034 if (probe
->dtpr_ecb
== NULL
) {
11036 * That was the last ECB on the probe; clear the predicate
11037 * cache ID for the probe, disable it and sync one more time
11038 * to assure that we'll never hit it again.
11040 dtrace_provider_t
*prov
= probe
->dtpr_provider
;
11042 ASSERT(ecb
->dte_next
== NULL
);
11043 ASSERT(probe
->dtpr_ecb_last
== NULL
);
11044 probe
->dtpr_predcache
= DTRACE_CACHEIDNONE
;
11045 prov
->dtpv_pops
.dtps_disable(prov
->dtpv_arg
,
11046 probe
->dtpr_id
, probe
->dtpr_arg
);
11050 * There is at least one ECB remaining on the probe. If there
11051 * is _exactly_ one, set the probe's predicate cache ID to be
11052 * the predicate cache ID of the remaining ECB.
11054 ASSERT(probe
->dtpr_ecb_last
!= NULL
);
11055 ASSERT(probe
->dtpr_predcache
== DTRACE_CACHEIDNONE
);
11057 if (probe
->dtpr_ecb
== probe
->dtpr_ecb_last
) {
11058 dtrace_predicate_t
*p
= probe
->dtpr_ecb
->dte_predicate
;
11060 ASSERT(probe
->dtpr_ecb
->dte_next
== NULL
);
11063 probe
->dtpr_predcache
= p
->dtp_cacheid
;
11066 ecb
->dte_next
= NULL
;
11071 dtrace_ecb_destroy(dtrace_ecb_t
*ecb
)
11073 dtrace_state_t
*state
= ecb
->dte_state
;
11074 dtrace_vstate_t
*vstate
= &state
->dts_vstate
;
11075 dtrace_predicate_t
*pred
;
11076 dtrace_epid_t epid
= ecb
->dte_epid
;
11078 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
11079 ASSERT(ecb
->dte_next
== NULL
);
11080 ASSERT(ecb
->dte_probe
== NULL
|| ecb
->dte_probe
->dtpr_ecb
!= ecb
);
11082 if ((pred
= ecb
->dte_predicate
) != NULL
)
11083 dtrace_predicate_release(pred
, vstate
);
11085 dtrace_ecb_action_remove(ecb
);
11087 ASSERT(state
->dts_ecbs
[epid
- 1] == ecb
);
11088 state
->dts_ecbs
[epid
- 1] = NULL
;
11090 kmem_free(ecb
, sizeof (dtrace_ecb_t
));
11093 static dtrace_ecb_t
*
11094 dtrace_ecb_create(dtrace_state_t
*state
, dtrace_probe_t
*probe
,
11095 dtrace_enabling_t
*enab
)
11098 dtrace_predicate_t
*pred
;
11099 dtrace_actdesc_t
*act
;
11100 dtrace_provider_t
*prov
;
11101 dtrace_ecbdesc_t
*desc
= enab
->dten_current
;
11103 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
11104 ASSERT(state
!= NULL
);
11106 ecb
= dtrace_ecb_add(state
, probe
);
11107 ecb
->dte_uarg
= desc
->dted_uarg
;
11109 if ((pred
= desc
->dted_pred
.dtpdd_predicate
) != NULL
) {
11110 dtrace_predicate_hold(pred
);
11111 ecb
->dte_predicate
= pred
;
11114 if (probe
!= NULL
) {
11116 * If the provider shows more leg than the consumer is old
11117 * enough to see, we need to enable the appropriate implicit
11118 * predicate bits to prevent the ecb from activating at
11121 * Providers specifying DTRACE_PRIV_USER at register time
11122 * are stating that they need the /proc-style privilege
11123 * model to be enforced, and this is what DTRACE_COND_OWNER
11124 * and DTRACE_COND_ZONEOWNER will then do at probe time.
11126 prov
= probe
->dtpr_provider
;
11127 if (!(state
->dts_cred
.dcr_visible
& DTRACE_CRV_ALLPROC
) &&
11128 (prov
->dtpv_priv
.dtpp_flags
& DTRACE_PRIV_USER
))
11129 ecb
->dte_cond
|= DTRACE_COND_OWNER
;
11131 if (!(state
->dts_cred
.dcr_visible
& DTRACE_CRV_ALLZONE
) &&
11132 (prov
->dtpv_priv
.dtpp_flags
& DTRACE_PRIV_USER
))
11133 ecb
->dte_cond
|= DTRACE_COND_ZONEOWNER
;
11136 * If the provider shows us kernel innards and the user
11137 * is lacking sufficient privilege, enable the
11138 * DTRACE_COND_USERMODE implicit predicate.
11140 if (!(state
->dts_cred
.dcr_visible
& DTRACE_CRV_KERNEL
) &&
11141 (prov
->dtpv_priv
.dtpp_flags
& DTRACE_PRIV_KERNEL
))
11142 ecb
->dte_cond
|= DTRACE_COND_USERMODE
;
11145 if (dtrace_ecb_create_cache
!= NULL
) {
11147 * If we have a cached ecb, we'll use its action list instead
11148 * of creating our own (saving both time and space).
11150 dtrace_ecb_t
*cached
= dtrace_ecb_create_cache
;
11151 dtrace_action_t
*act_if
= cached
->dte_action
;
11153 if (act_if
!= NULL
) {
11154 ASSERT(act_if
->dta_refcnt
> 0);
11155 act_if
->dta_refcnt
++;
11156 ecb
->dte_action
= act_if
;
11157 ecb
->dte_action_last
= cached
->dte_action_last
;
11158 ecb
->dte_needed
= cached
->dte_needed
;
11159 ecb
->dte_size
= cached
->dte_size
;
11160 ecb
->dte_alignment
= cached
->dte_alignment
;
11166 for (act
= desc
->dted_action
; act
!= NULL
; act
= act
->dtad_next
) {
11167 if ((enab
->dten_error
= dtrace_ecb_action_add(ecb
, act
)) != 0) {
11168 dtrace_ecb_destroy(ecb
);
11173 dtrace_ecb_resize(ecb
);
11175 return (dtrace_ecb_create_cache
= ecb
);
11179 dtrace_ecb_create_enable(dtrace_probe_t
*probe
, void *arg
)
11182 dtrace_enabling_t
*enab
= arg
;
11183 dtrace_state_t
*state
= enab
->dten_vstate
->dtvs_state
;
11185 ASSERT(state
!= NULL
);
11187 if (probe
!= NULL
&& probe
->dtpr_gen
< enab
->dten_probegen
) {
11189 * This probe was created in a generation for which this
11190 * enabling has previously created ECBs; we don't want to
11191 * enable it again, so just kick out.
11193 return (DTRACE_MATCH_NEXT
);
11196 if ((ecb
= dtrace_ecb_create(state
, probe
, enab
)) == NULL
)
11197 return (DTRACE_MATCH_DONE
);
11199 dtrace_ecb_enable(ecb
);
11200 return (DTRACE_MATCH_NEXT
);
11203 static dtrace_ecb_t
*
11204 dtrace_epid2ecb(dtrace_state_t
*state
, dtrace_epid_t id
)
11207 #pragma unused(ecb) /* __APPLE__ */
11209 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
11211 #if !defined(__APPLE__) /* Quiet compiler warnings */
11212 if (id
== 0 || id
> state
->dts_necbs
)
11214 if (id
== 0 || id
> (dtrace_epid_t
)state
->dts_necbs
)
11215 #endif /* __APPLE__ */
11218 ASSERT(state
->dts_necbs
> 0 && state
->dts_ecbs
!= NULL
);
11219 ASSERT((ecb
= state
->dts_ecbs
[id
- 1]) == NULL
|| ecb
->dte_epid
== id
);
11221 return (state
->dts_ecbs
[id
- 1]);
11224 static dtrace_aggregation_t
*
11225 dtrace_aggid2agg(dtrace_state_t
*state
, dtrace_aggid_t id
)
11227 dtrace_aggregation_t
*agg
;
11228 #pragma unused(agg) /* __APPLE__ */
11230 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
11232 #if !defined(__APPLE__) /* Quiet compiler warnings */
11233 if (id
== 0 || id
> state
->dts_naggregations
)
11235 if (id
== 0 || id
> (dtrace_aggid_t
)state
->dts_naggregations
)
11236 #endif /* __APPLE__ */
11239 ASSERT(state
->dts_naggregations
> 0 && state
->dts_aggregations
!= NULL
);
11240 ASSERT((agg
= state
->dts_aggregations
[id
- 1]) == NULL
||
11241 agg
->dtag_id
== id
);
11243 return (state
->dts_aggregations
[id
- 1]);
11247 * DTrace Buffer Functions
11249 * The following functions manipulate DTrace buffers. Most of these functions
11250 * are called in the context of establishing or processing consumer state;
11251 * exceptions are explicitly noted.
11255 * Note: called from cross call context. This function switches the two
11256 * buffers on a given CPU. The atomicity of this operation is assured by
11257 * disabling interrupts while the actual switch takes place; the disabling of
11258 * interrupts serializes the execution with any execution of dtrace_probe() on
11262 dtrace_buffer_switch(dtrace_buffer_t
*buf
)
11264 caddr_t tomax
= buf
->dtb_tomax
;
11265 caddr_t xamot
= buf
->dtb_xamot
;
11266 dtrace_icookie_t cookie
;
11268 ASSERT(!(buf
->dtb_flags
& DTRACEBUF_NOSWITCH
));
11269 ASSERT(!(buf
->dtb_flags
& DTRACEBUF_RING
));
11271 cookie
= dtrace_interrupt_disable();
11272 buf
->dtb_tomax
= xamot
;
11273 buf
->dtb_xamot
= tomax
;
11274 buf
->dtb_xamot_drops
= buf
->dtb_drops
;
11275 buf
->dtb_xamot_offset
= buf
->dtb_offset
;
11276 buf
->dtb_xamot_errors
= buf
->dtb_errors
;
11277 buf
->dtb_xamot_flags
= buf
->dtb_flags
;
11278 buf
->dtb_offset
= 0;
11279 buf
->dtb_drops
= 0;
11280 buf
->dtb_errors
= 0;
11281 buf
->dtb_flags
&= ~(DTRACEBUF_ERROR
| DTRACEBUF_DROPPED
);
11282 dtrace_interrupt_enable(cookie
);
11286 * Note: called from cross call context. This function activates a buffer
11287 * on a CPU. As with dtrace_buffer_switch(), the atomicity of the operation
11288 * is guaranteed by the disabling of interrupts.
11291 dtrace_buffer_activate(dtrace_state_t
*state
)
11293 dtrace_buffer_t
*buf
;
11294 dtrace_icookie_t cookie
= dtrace_interrupt_disable();
11296 buf
= &state
->dts_buffer
[CPU
->cpu_id
];
11298 if (buf
->dtb_tomax
!= NULL
) {
11300 * We might like to assert that the buffer is marked inactive,
11301 * but this isn't necessarily true: the buffer for the CPU
11302 * that processes the BEGIN probe has its buffer activated
11303 * manually. In this case, we take the (harmless) action
11304 * re-clearing the bit INACTIVE bit.
11306 buf
->dtb_flags
&= ~DTRACEBUF_INACTIVE
;
11309 dtrace_interrupt_enable(cookie
);
11313 dtrace_buffer_alloc(dtrace_buffer_t
*bufs
, size_t size
, int flags
,
11317 dtrace_buffer_t
*buf
;
11319 lck_mtx_assert(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
11320 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
11322 #if !defined(__APPLE__) /* Quiet compiler warnings */
11323 if (size
> dtrace_nonroot_maxsize
&&
11324 !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL
, B_FALSE
))
11327 if (size
> (size_t)dtrace_nonroot_maxsize
&&
11328 !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL
, B_FALSE
))
11330 #endif /* __APPLE__ */
11333 #if defined(__APPLE__)
11334 if (size
> (sane_size
/ 8) / (int)NCPU
) /* As in kdbg_set_nkdbufs(), roughly. */
11336 #endif /* __APPLE__ */
11341 if (cpu
!= DTRACE_CPUALL
&& cpu
!= cp
->cpu_id
)
11344 buf
= &bufs
[cp
->cpu_id
];
11347 * If there is already a buffer allocated for this CPU, it
11348 * is only possible that this is a DR event. In this case,
11349 * the buffer size must match our specified size.
11351 if (buf
->dtb_tomax
!= NULL
) {
11352 ASSERT(buf
->dtb_size
== size
);
11356 ASSERT(buf
->dtb_xamot
== NULL
);
11358 if ((buf
->dtb_tomax
= kmem_zalloc(size
, KM_NOSLEEP
)) == NULL
)
11361 buf
->dtb_size
= size
;
11362 buf
->dtb_flags
= flags
;
11363 buf
->dtb_offset
= 0;
11364 buf
->dtb_drops
= 0;
11366 if (flags
& DTRACEBUF_NOSWITCH
)
11369 if ((buf
->dtb_xamot
= kmem_zalloc(size
, KM_NOSLEEP
)) == NULL
)
11371 } while ((cp
= cp
->cpu_next
) != cpu_list
);
11379 if (cpu
!= DTRACE_CPUALL
&& cpu
!= cp
->cpu_id
)
11382 buf
= &bufs
[cp
->cpu_id
];
11384 if (buf
->dtb_xamot
!= NULL
) {
11385 ASSERT(buf
->dtb_tomax
!= NULL
);
11386 ASSERT(buf
->dtb_size
== size
);
11387 kmem_free(buf
->dtb_xamot
, size
);
11390 if (buf
->dtb_tomax
!= NULL
) {
11391 ASSERT(buf
->dtb_size
== size
);
11392 kmem_free(buf
->dtb_tomax
, size
);
11395 buf
->dtb_tomax
= NULL
;
11396 buf
->dtb_xamot
= NULL
;
11398 } while ((cp
= cp
->cpu_next
) != cpu_list
);
11404 * Note: called from probe context. This function just increments the drop
11405 * count on a buffer. It has been made a function to allow for the
11406 * possibility of understanding the source of mysterious drop counts. (A
11407 * problem for which one may be particularly disappointed that DTrace cannot
11408 * be used to understand DTrace.)
11411 dtrace_buffer_drop(dtrace_buffer_t
*buf
)
11417 * Note: called from probe context. This function is called to reserve space
11418 * in a buffer. If mstate is non-NULL, sets the scratch base and size in the
11419 * mstate. Returns the new offset in the buffer, or a negative value if an
11420 * error has occurred.
11423 dtrace_buffer_reserve(dtrace_buffer_t
*buf
, size_t needed
, size_t align
,
11424 dtrace_state_t
*state
, dtrace_mstate_t
*mstate
)
11426 intptr_t offs
= buf
->dtb_offset
, soffs
;
11431 if (buf
->dtb_flags
& DTRACEBUF_INACTIVE
)
11434 if ((tomax
= buf
->dtb_tomax
) == NULL
) {
11435 dtrace_buffer_drop(buf
);
11439 if (!(buf
->dtb_flags
& (DTRACEBUF_RING
| DTRACEBUF_FILL
))) {
11440 while (offs
& (align
- 1)) {
11442 * Assert that our alignment is off by a number which
11443 * is itself sizeof (uint32_t) aligned.
11445 ASSERT(!((align
- (offs
& (align
- 1))) &
11446 (sizeof (uint32_t) - 1)));
11447 DTRACE_STORE(uint32_t, tomax
, offs
, DTRACE_EPIDNONE
);
11448 offs
+= sizeof (uint32_t);
11451 #if !defined(__APPLE__) /* Quiet compiler warnings */
11452 if ((soffs
= offs
+ needed
) > buf
->dtb_size
) {
11454 if ((uint64_t)(soffs
= offs
+ needed
) > buf
->dtb_size
) {
11455 #endif /* __APPLE__ */
11456 dtrace_buffer_drop(buf
);
11460 if (mstate
== NULL
)
11463 mstate
->dtms_scratch_base
= (uintptr_t)tomax
+ soffs
;
11464 mstate
->dtms_scratch_size
= buf
->dtb_size
- soffs
;
11465 mstate
->dtms_scratch_ptr
= mstate
->dtms_scratch_base
;
11470 if (buf
->dtb_flags
& DTRACEBUF_FILL
) {
11471 if (state
->dts_activity
!= DTRACE_ACTIVITY_COOLDOWN
&&
11472 (buf
->dtb_flags
& DTRACEBUF_FULL
))
11477 total_off
= needed
+ (offs
& (align
- 1));
11480 * For a ring buffer, life is quite a bit more complicated. Before
11481 * we can store any padding, we need to adjust our wrapping offset.
11482 * (If we've never before wrapped or we're not about to, no adjustment
11485 if ((buf
->dtb_flags
& DTRACEBUF_WRAPPED
) ||
11486 offs
+ total_off
> buf
->dtb_size
) {
11487 woffs
= buf
->dtb_xamot_offset
;
11489 if (offs
+ total_off
> buf
->dtb_size
) {
11491 * We can't fit in the end of the buffer. First, a
11492 * sanity check that we can fit in the buffer at all.
11494 if (total_off
> buf
->dtb_size
) {
11495 dtrace_buffer_drop(buf
);
11500 * We're going to be storing at the top of the buffer,
11501 * so now we need to deal with the wrapped offset. We
11502 * only reset our wrapped offset to 0 if it is
11503 * currently greater than the current offset. If it
11504 * is less than the current offset, it is because a
11505 * previous allocation induced a wrap -- but the
11506 * allocation didn't subsequently take the space due
11507 * to an error or false predicate evaluation. In this
11508 * case, we'll just leave the wrapped offset alone: if
11509 * the wrapped offset hasn't been advanced far enough
11510 * for this allocation, it will be adjusted in the
11513 if (buf
->dtb_flags
& DTRACEBUF_WRAPPED
) {
11521 * Now we know that we're going to be storing to the
11522 * top of the buffer and that there is room for us
11523 * there. We need to clear the buffer from the current
11524 * offset to the end (there may be old gunk there).
11526 #if !defined(__APPLE__) /* Quiet compiler warnings */
11527 while (offs
< buf
->dtb_size
)
11529 while ((uint64_t)offs
< buf
->dtb_size
)
11530 #endif /* __APPLE__ */
11534 * We need to set our offset to zero. And because we
11535 * are wrapping, we need to set the bit indicating as
11536 * much. We can also adjust our needed space back
11537 * down to the space required by the ECB -- we know
11538 * that the top of the buffer is aligned.
11541 total_off
= needed
;
11542 buf
->dtb_flags
|= DTRACEBUF_WRAPPED
;
11545 * There is room for us in the buffer, so we simply
11546 * need to check the wrapped offset.
11548 if (woffs
< offs
) {
11550 * The wrapped offset is less than the offset.
11551 * This can happen if we allocated buffer space
11552 * that induced a wrap, but then we didn't
11553 * subsequently take the space due to an error
11554 * or false predicate evaluation. This is
11555 * okay; we know that _this_ allocation isn't
11556 * going to induce a wrap. We still can't
11557 * reset the wrapped offset to be zero,
11558 * however: the space may have been trashed in
11559 * the previous failed probe attempt. But at
11560 * least the wrapped offset doesn't need to
11561 * be adjusted at all...
11567 #if !defined(__APPLE__) /* Quiet compiler warnings */
11568 while (offs
+ total_off
> woffs
) {
11570 while (offs
+ total_off
> (size_t)woffs
) {
11571 #endif /* __APPLE__ */
11572 dtrace_epid_t epid
= *(uint32_t *)(tomax
+ woffs
);
11575 if (epid
== DTRACE_EPIDNONE
) {
11576 size
= sizeof (uint32_t);
11578 #if !defined(__APPLE__) /* Quiet compiler warnings */
11579 ASSERT(epid
<= state
->dts_necbs
);
11581 ASSERT(epid
<= (dtrace_epid_t
)state
->dts_necbs
);
11582 #endif /* __APPLE__ */
11583 ASSERT(state
->dts_ecbs
[epid
- 1] != NULL
);
11585 size
= state
->dts_ecbs
[epid
- 1]->dte_size
;
11588 ASSERT(woffs
+ size
<= buf
->dtb_size
);
11591 if (woffs
+ size
== buf
->dtb_size
) {
11593 * We've reached the end of the buffer; we want
11594 * to set the wrapped offset to 0 and break
11595 * out. However, if the offs is 0, then we're
11596 * in a strange edge-condition: the amount of
11597 * space that we want to reserve plus the size
11598 * of the record that we're overwriting is
11599 * greater than the size of the buffer. This
11600 * is problematic because if we reserve the
11601 * space but subsequently don't consume it (due
11602 * to a failed predicate or error) the wrapped
11603 * offset will be 0 -- yet the EPID at offset 0
11604 * will not be committed. This situation is
11605 * relatively easy to deal with: if we're in
11606 * this case, the buffer is indistinguishable
11607 * from one that hasn't wrapped; we need only
11608 * finish the job by clearing the wrapped bit,
11609 * explicitly setting the offset to be 0, and
11610 * zero'ing out the old data in the buffer.
11613 buf
->dtb_flags
&= ~DTRACEBUF_WRAPPED
;
11614 buf
->dtb_offset
= 0;
11617 #if !defined(__APPLE__) /* Quiet compiler warnings */
11618 while (woffs
< buf
->dtb_size
)
11620 while ((uint64_t)woffs
< buf
->dtb_size
)
11621 #endif /* __APPLE__ */
11623 tomax
[woffs
++] = 0;
11634 * We have a wrapped offset. It may be that the wrapped offset
11635 * has become zero -- that's okay.
11637 buf
->dtb_xamot_offset
= woffs
;
11642 * Now we can plow the buffer with any necessary padding.
11644 while (offs
& (align
- 1)) {
11646 * Assert that our alignment is off by a number which
11647 * is itself sizeof (uint32_t) aligned.
11649 ASSERT(!((align
- (offs
& (align
- 1))) &
11650 (sizeof (uint32_t) - 1)));
11651 DTRACE_STORE(uint32_t, tomax
, offs
, DTRACE_EPIDNONE
);
11652 offs
+= sizeof (uint32_t);
11655 if (buf
->dtb_flags
& DTRACEBUF_FILL
) {
11656 if (offs
+ needed
> buf
->dtb_size
- state
->dts_reserve
) {
11657 buf
->dtb_flags
|= DTRACEBUF_FULL
;
11662 if (mstate
== NULL
)
11666 * For ring buffers and fill buffers, the scratch space is always
11667 * the inactive buffer.
11669 mstate
->dtms_scratch_base
= (uintptr_t)buf
->dtb_xamot
;
11670 mstate
->dtms_scratch_size
= buf
->dtb_size
;
11671 mstate
->dtms_scratch_ptr
= mstate
->dtms_scratch_base
;
11677 dtrace_buffer_polish(dtrace_buffer_t
*buf
)
11679 ASSERT(buf
->dtb_flags
& DTRACEBUF_RING
);
11680 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
11682 if (!(buf
->dtb_flags
& DTRACEBUF_WRAPPED
))
11686 * We need to polish the ring buffer. There are three cases:
11688 * - The first (and presumably most common) is that there is no gap
11689 * between the buffer offset and the wrapped offset. In this case,
11690 * there is nothing in the buffer that isn't valid data; we can
11691 * mark the buffer as polished and return.
11693 * - The second (less common than the first but still more common
11694 * than the third) is that there is a gap between the buffer offset
11695 * and the wrapped offset, and the wrapped offset is larger than the
11696 * buffer offset. This can happen because of an alignment issue, or
11697 * can happen because of a call to dtrace_buffer_reserve() that
11698 * didn't subsequently consume the buffer space. In this case,
11699 * we need to zero the data from the buffer offset to the wrapped
11702 * - The third (and least common) is that there is a gap between the
11703 * buffer offset and the wrapped offset, but the wrapped offset is
11704 * _less_ than the buffer offset. This can only happen because a
11705 * call to dtrace_buffer_reserve() induced a wrap, but the space
11706 * was not subsequently consumed. In this case, we need to zero the
11707 * space from the offset to the end of the buffer _and_ from the
11708 * top of the buffer to the wrapped offset.
11710 if (buf
->dtb_offset
< buf
->dtb_xamot_offset
) {
11711 bzero(buf
->dtb_tomax
+ buf
->dtb_offset
,
11712 buf
->dtb_xamot_offset
- buf
->dtb_offset
);
11715 if (buf
->dtb_offset
> buf
->dtb_xamot_offset
) {
11716 bzero(buf
->dtb_tomax
+ buf
->dtb_offset
,
11717 buf
->dtb_size
- buf
->dtb_offset
);
11718 bzero(buf
->dtb_tomax
, buf
->dtb_xamot_offset
);
11723 dtrace_buffer_free(dtrace_buffer_t
*bufs
)
11727 for (i
= 0; i
< (int)NCPU
; i
++) {
11728 dtrace_buffer_t
*buf
= &bufs
[i
];
11730 if (buf
->dtb_tomax
== NULL
) {
11731 ASSERT(buf
->dtb_xamot
== NULL
);
11732 ASSERT(buf
->dtb_size
== 0);
11736 if (buf
->dtb_xamot
!= NULL
) {
11737 ASSERT(!(buf
->dtb_flags
& DTRACEBUF_NOSWITCH
));
11738 kmem_free(buf
->dtb_xamot
, buf
->dtb_size
);
11741 kmem_free(buf
->dtb_tomax
, buf
->dtb_size
);
11743 buf
->dtb_tomax
= NULL
;
11744 buf
->dtb_xamot
= NULL
;
11749 * DTrace Enabling Functions
11751 static dtrace_enabling_t
*
11752 dtrace_enabling_create(dtrace_vstate_t
*vstate
)
11754 dtrace_enabling_t
*enab
;
11756 enab
= kmem_zalloc(sizeof (dtrace_enabling_t
), KM_SLEEP
);
11757 enab
->dten_vstate
= vstate
;
11763 dtrace_enabling_add(dtrace_enabling_t
*enab
, dtrace_ecbdesc_t
*ecb
)
11765 dtrace_ecbdesc_t
**ndesc
;
11766 size_t osize
, nsize
;
11769 * We can't add to enablings after we've enabled them, or after we've
11772 ASSERT(enab
->dten_probegen
== 0);
11773 ASSERT(enab
->dten_next
== NULL
&& enab
->dten_prev
== NULL
);
11775 #if defined(__APPLE__)
11776 if (ecb
== NULL
) return; /* Note: protection against gcc 4.0 botch on x86 */
11777 #endif /* __APPLE__ */
11779 if (enab
->dten_ndesc
< enab
->dten_maxdesc
) {
11780 enab
->dten_desc
[enab
->dten_ndesc
++] = ecb
;
11784 osize
= enab
->dten_maxdesc
* sizeof (dtrace_enabling_t
*);
11786 if (enab
->dten_maxdesc
== 0) {
11787 enab
->dten_maxdesc
= 1;
11789 enab
->dten_maxdesc
<<= 1;
11792 ASSERT(enab
->dten_ndesc
< enab
->dten_maxdesc
);
11794 nsize
= enab
->dten_maxdesc
* sizeof (dtrace_enabling_t
*);
11795 ndesc
= kmem_zalloc(nsize
, KM_SLEEP
);
11796 bcopy(enab
->dten_desc
, ndesc
, osize
);
11797 kmem_free(enab
->dten_desc
, osize
);
11799 enab
->dten_desc
= ndesc
;
11800 enab
->dten_desc
[enab
->dten_ndesc
++] = ecb
;
11804 dtrace_enabling_addlike(dtrace_enabling_t
*enab
, dtrace_ecbdesc_t
*ecb
,
11805 dtrace_probedesc_t
*pd
)
11807 dtrace_ecbdesc_t
*new;
11808 dtrace_predicate_t
*pred
;
11809 dtrace_actdesc_t
*act
;
11812 * We're going to create a new ECB description that matches the
11813 * specified ECB in every way, but has the specified probe description.
11815 new = kmem_zalloc(sizeof (dtrace_ecbdesc_t
), KM_SLEEP
);
11817 if ((pred
= ecb
->dted_pred
.dtpdd_predicate
) != NULL
)
11818 dtrace_predicate_hold(pred
);
11820 for (act
= ecb
->dted_action
; act
!= NULL
; act
= act
->dtad_next
)
11821 dtrace_actdesc_hold(act
);
11823 new->dted_action
= ecb
->dted_action
;
11824 new->dted_pred
= ecb
->dted_pred
;
11825 new->dted_probe
= *pd
;
11826 new->dted_uarg
= ecb
->dted_uarg
;
11828 dtrace_enabling_add(enab
, new);
11832 dtrace_enabling_dump(dtrace_enabling_t
*enab
)
11836 for (i
= 0; i
< enab
->dten_ndesc
; i
++) {
11837 dtrace_probedesc_t
*desc
= &enab
->dten_desc
[i
]->dted_probe
;
11839 cmn_err(CE_NOTE
, "enabling probe %d (%s:%s:%s:%s)", i
,
11840 desc
->dtpd_provider
, desc
->dtpd_mod
,
11841 desc
->dtpd_func
, desc
->dtpd_name
);
11846 dtrace_enabling_destroy(dtrace_enabling_t
*enab
)
11849 dtrace_ecbdesc_t
*ep
;
11850 dtrace_vstate_t
*vstate
= enab
->dten_vstate
;
11852 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
11854 for (i
= 0; i
< enab
->dten_ndesc
; i
++) {
11855 dtrace_actdesc_t
*act
, *next
;
11856 dtrace_predicate_t
*pred
;
11858 ep
= enab
->dten_desc
[i
];
11860 if ((pred
= ep
->dted_pred
.dtpdd_predicate
) != NULL
)
11861 dtrace_predicate_release(pred
, vstate
);
11863 for (act
= ep
->dted_action
; act
!= NULL
; act
= next
) {
11864 next
= act
->dtad_next
;
11865 dtrace_actdesc_release(act
, vstate
);
11868 kmem_free(ep
, sizeof (dtrace_ecbdesc_t
));
11871 kmem_free(enab
->dten_desc
,
11872 enab
->dten_maxdesc
* sizeof (dtrace_enabling_t
*));
11875 * If this was a retained enabling, decrement the dts_nretained count
11876 * and take it off of the dtrace_retained list.
11878 if (enab
->dten_prev
!= NULL
|| enab
->dten_next
!= NULL
||
11879 dtrace_retained
== enab
) {
11880 ASSERT(enab
->dten_vstate
->dtvs_state
!= NULL
);
11881 ASSERT(enab
->dten_vstate
->dtvs_state
->dts_nretained
> 0);
11882 enab
->dten_vstate
->dtvs_state
->dts_nretained
--;
11883 dtrace_retained_gen
++;
11886 if (enab
->dten_prev
== NULL
) {
11887 if (dtrace_retained
== enab
) {
11888 dtrace_retained
= enab
->dten_next
;
11890 if (dtrace_retained
!= NULL
)
11891 dtrace_retained
->dten_prev
= NULL
;
11894 ASSERT(enab
!= dtrace_retained
);
11895 ASSERT(dtrace_retained
!= NULL
);
11896 enab
->dten_prev
->dten_next
= enab
->dten_next
;
11899 if (enab
->dten_next
!= NULL
) {
11900 ASSERT(dtrace_retained
!= NULL
);
11901 enab
->dten_next
->dten_prev
= enab
->dten_prev
;
11904 kmem_free(enab
, sizeof (dtrace_enabling_t
));
11908 dtrace_enabling_retain(dtrace_enabling_t
*enab
)
11910 dtrace_state_t
*state
;
11912 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
11913 ASSERT(enab
->dten_next
== NULL
&& enab
->dten_prev
== NULL
);
11914 ASSERT(enab
->dten_vstate
!= NULL
);
11916 state
= enab
->dten_vstate
->dtvs_state
;
11917 ASSERT(state
!= NULL
);
11920 * We only allow each state to retain dtrace_retain_max enablings.
11922 if (state
->dts_nretained
>= dtrace_retain_max
)
11925 state
->dts_nretained
++;
11926 dtrace_retained_gen
++;
11928 if (dtrace_retained
== NULL
) {
11929 dtrace_retained
= enab
;
11933 enab
->dten_next
= dtrace_retained
;
11934 dtrace_retained
->dten_prev
= enab
;
11935 dtrace_retained
= enab
;
11941 dtrace_enabling_replicate(dtrace_state_t
*state
, dtrace_probedesc_t
*match
,
11942 dtrace_probedesc_t
*create
)
11944 dtrace_enabling_t
*new, *enab
;
11945 int found
= 0, err
= ENOENT
;
11947 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
11948 ASSERT(strlen(match
->dtpd_provider
) < DTRACE_PROVNAMELEN
);
11949 ASSERT(strlen(match
->dtpd_mod
) < DTRACE_MODNAMELEN
);
11950 ASSERT(strlen(match
->dtpd_func
) < DTRACE_FUNCNAMELEN
);
11951 ASSERT(strlen(match
->dtpd_name
) < DTRACE_NAMELEN
);
11953 new = dtrace_enabling_create(&state
->dts_vstate
);
11956 * Iterate over all retained enablings, looking for enablings that
11957 * match the specified state.
11959 for (enab
= dtrace_retained
; enab
!= NULL
; enab
= enab
->dten_next
) {
11963 * dtvs_state can only be NULL for helper enablings -- and
11964 * helper enablings can't be retained.
11966 ASSERT(enab
->dten_vstate
->dtvs_state
!= NULL
);
11968 if (enab
->dten_vstate
->dtvs_state
!= state
)
11972 * Now iterate over each probe description; we're looking for
11973 * an exact match to the specified probe description.
11975 for (i
= 0; i
< enab
->dten_ndesc
; i
++) {
11976 dtrace_ecbdesc_t
*ep
= enab
->dten_desc
[i
];
11977 dtrace_probedesc_t
*pd
= &ep
->dted_probe
;
11979 #if !defined(__APPLE__)
11980 if (strcmp(pd
->dtpd_provider
, match
->dtpd_provider
))
11983 if (strcmp(pd
->dtpd_mod
, match
->dtpd_mod
))
11986 if (strcmp(pd
->dtpd_func
, match
->dtpd_func
))
11989 if (strcmp(pd
->dtpd_name
, match
->dtpd_name
))
11991 #else /* Employ size bounded string operation. */
11992 if (strncmp(pd
->dtpd_provider
, match
->dtpd_provider
, DTRACE_PROVNAMELEN
))
11995 if (strncmp(pd
->dtpd_mod
, match
->dtpd_mod
, DTRACE_MODNAMELEN
))
11998 if (strncmp(pd
->dtpd_func
, match
->dtpd_func
, DTRACE_FUNCNAMELEN
))
12001 if (strncmp(pd
->dtpd_name
, match
->dtpd_name
, DTRACE_NAMELEN
))
12003 #endif /* __APPLE__ */
12006 * We have a winning probe! Add it to our growing
12010 dtrace_enabling_addlike(new, ep
, create
);
12014 if (!found
|| (err
= dtrace_enabling_retain(new)) != 0) {
12015 dtrace_enabling_destroy(new);
12023 dtrace_enabling_retract(dtrace_state_t
*state
)
12025 dtrace_enabling_t
*enab
, *next
;
12027 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
12030 * Iterate over all retained enablings, destroy the enablings retained
12031 * for the specified state.
12033 for (enab
= dtrace_retained
; enab
!= NULL
; enab
= next
) {
12034 next
= enab
->dten_next
;
12037 * dtvs_state can only be NULL for helper enablings -- and
12038 * helper enablings can't be retained.
12040 ASSERT(enab
->dten_vstate
->dtvs_state
!= NULL
);
12042 if (enab
->dten_vstate
->dtvs_state
== state
) {
12043 ASSERT(state
->dts_nretained
> 0);
12044 dtrace_enabling_destroy(enab
);
12048 ASSERT(state
->dts_nretained
== 0);
12052 dtrace_enabling_match(dtrace_enabling_t
*enab
, int *nmatched
)
12057 lck_mtx_assert(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
12058 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
12060 for (i
= 0; i
< enab
->dten_ndesc
; i
++) {
12061 dtrace_ecbdesc_t
*ep
= enab
->dten_desc
[i
];
12063 enab
->dten_current
= ep
;
12064 enab
->dten_error
= 0;
12066 matched
+= dtrace_probe_enable(&ep
->dted_probe
, enab
);
12068 if (enab
->dten_error
!= 0) {
12070 * If we get an error half-way through enabling the
12071 * probes, we kick out -- perhaps with some number of
12072 * them enabled. Leaving enabled probes enabled may
12073 * be slightly confusing for user-level, but we expect
12074 * that no one will attempt to actually drive on in
12075 * the face of such errors. If this is an anonymous
12076 * enabling (indicated with a NULL nmatched pointer),
12077 * we cmn_err() a message. We aren't expecting to
12078 * get such an error -- such as it can exist at all,
12079 * it would be a result of corrupted DOF in the driver
12082 if (nmatched
== NULL
) {
12083 cmn_err(CE_WARN
, "dtrace_enabling_match() "
12084 "error on %p: %d", (void *)ep
,
12088 return (enab
->dten_error
);
12092 enab
->dten_probegen
= dtrace_probegen
;
12093 if (nmatched
!= NULL
)
12094 *nmatched
= matched
;
12100 dtrace_enabling_matchall(void)
12102 dtrace_enabling_t
*enab
;
12104 lck_mtx_lock(&cpu_lock
);
12105 lck_mtx_lock(&dtrace_lock
);
12108 * Iterate over all retained enablings to see if any probes match
12109 * against them. We only perform this operation on enablings for which
12110 * we have sufficient permissions by virtue of being in the global zone
12111 * or in the same zone as the DTrace client. Because we can be called
12112 * after dtrace_detach() has been called, we cannot assert that there
12113 * are retained enablings. We can safely load from dtrace_retained,
12114 * however: the taskq_destroy() at the end of dtrace_detach() will
12115 * block pending our completion.
12117 for (enab
= dtrace_retained
; enab
!= NULL
; enab
= enab
->dten_next
) {
12118 #if !defined(__APPLE__)
12119 cred_t
*cr
= enab
->dten_vstate
->dtvs_state
->dts_cred
.dcr_cred
;
12121 if (INGLOBALZONE(curproc
) ||
12122 cr
!= NULL
&& getzoneid() == crgetzoneid(cr
))
12123 (void) dtrace_enabling_match(enab
, NULL
);
12125 (void) dtrace_enabling_match(enab
, NULL
); /* As if always in "global" zone." */
12126 #endif /* __APPLE__ */
12129 lck_mtx_unlock(&dtrace_lock
);
12130 lck_mtx_unlock(&cpu_lock
);
12134 * If an enabling is to be enabled without having matched probes (that is, if
12135 * dtrace_state_go() is to be called on the underlying dtrace_state_t), the
12136 * enabling must be _primed_ by creating an ECB for every ECB description.
12137 * This must be done to assure that we know the number of speculations, the
12138 * number of aggregations, the minimum buffer size needed, etc. before we
12139 * transition out of DTRACE_ACTIVITY_INACTIVE. To do this without actually
12140 * enabling any probes, we create ECBs for every ECB decription, but with a
12141 * NULL probe -- which is exactly what this function does.
12144 dtrace_enabling_prime(dtrace_state_t
*state
)
12146 dtrace_enabling_t
*enab
;
12149 for (enab
= dtrace_retained
; enab
!= NULL
; enab
= enab
->dten_next
) {
12150 ASSERT(enab
->dten_vstate
->dtvs_state
!= NULL
);
12152 if (enab
->dten_vstate
->dtvs_state
!= state
)
12156 * We don't want to prime an enabling more than once, lest
12157 * we allow a malicious user to induce resource exhaustion.
12158 * (The ECBs that result from priming an enabling aren't
12159 * leaked -- but they also aren't deallocated until the
12160 * consumer state is destroyed.)
12162 if (enab
->dten_primed
)
12165 for (i
= 0; i
< enab
->dten_ndesc
; i
++) {
12166 enab
->dten_current
= enab
->dten_desc
[i
];
12167 (void) dtrace_probe_enable(NULL
, enab
);
12170 enab
->dten_primed
= 1;
12175 * Called to indicate that probes should be provided due to retained
12176 * enablings. This is implemented in terms of dtrace_probe_provide(), but it
12177 * must take an initial lap through the enabling calling the dtps_provide()
12178 * entry point explicitly to allow for autocreated probes.
12181 dtrace_enabling_provide(dtrace_provider_t
*prv
)
12184 dtrace_probedesc_t desc
;
12185 dtrace_genid_t gen
;
12187 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
12188 lck_mtx_assert(&dtrace_provider_lock
, LCK_MTX_ASSERT_OWNED
);
12192 prv
= dtrace_provider
;
12196 dtrace_enabling_t
*enab
;
12197 void *parg
= prv
->dtpv_arg
;
12200 gen
= dtrace_retained_gen
;
12201 for (enab
= dtrace_retained
; enab
!= NULL
;
12202 enab
= enab
->dten_next
) {
12203 for (i
= 0; i
< enab
->dten_ndesc
; i
++) {
12204 desc
= enab
->dten_desc
[i
]->dted_probe
;
12205 lck_mtx_unlock(&dtrace_lock
);
12206 prv
->dtpv_pops
.dtps_provide(parg
, &desc
);
12207 lck_mtx_lock(&dtrace_lock
);
12209 * Process the retained enablings again if
12210 * they have changed while we weren't holding
12213 if (gen
!= dtrace_retained_gen
)
12217 } while (all
&& (prv
= prv
->dtpv_next
) != NULL
);
12219 lck_mtx_unlock(&dtrace_lock
);
12220 dtrace_probe_provide(NULL
, all
? NULL
: prv
);
12221 lck_mtx_lock(&dtrace_lock
);
12225 * DTrace DOF Functions
12229 dtrace_dof_error(dof_hdr_t
*dof
, const char *str
)
12231 #pragma unused(dof) /* __APPLE__ */
12232 if (dtrace_err_verbose
)
12233 cmn_err(CE_WARN
, "failed to process DOF: %s", str
);
12235 #ifdef DTRACE_ERRDEBUG
12236 dtrace_errdebug(str
);
12241 * Create DOF out of a currently enabled state. Right now, we only create
12242 * DOF containing the run-time options -- but this could be expanded to create
12243 * complete DOF representing the enabled state.
12246 dtrace_dof_create(dtrace_state_t
*state
)
12250 dof_optdesc_t
*opt
;
12251 int i
, len
= sizeof (dof_hdr_t
) +
12252 roundup(sizeof (dof_sec_t
), sizeof (uint64_t)) +
12253 sizeof (dof_optdesc_t
) * DTRACEOPT_MAX
;
12255 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
12257 #if !defined(__APPLE__)
12258 dof
= kmem_zalloc(len
, KM_SLEEP
);
12260 dof
= dt_kmem_zalloc_aligned(len
, 8, KM_SLEEP
);
12261 #endif /* __APPLE__ */
12262 dof
->dofh_ident
[DOF_ID_MAG0
] = DOF_MAG_MAG0
;
12263 dof
->dofh_ident
[DOF_ID_MAG1
] = DOF_MAG_MAG1
;
12264 dof
->dofh_ident
[DOF_ID_MAG2
] = DOF_MAG_MAG2
;
12265 dof
->dofh_ident
[DOF_ID_MAG3
] = DOF_MAG_MAG3
;
12267 dof
->dofh_ident
[DOF_ID_MODEL
] = DOF_MODEL_NATIVE
;
12268 dof
->dofh_ident
[DOF_ID_ENCODING
] = DOF_ENCODE_NATIVE
;
12269 dof
->dofh_ident
[DOF_ID_VERSION
] = DOF_VERSION
;
12270 dof
->dofh_ident
[DOF_ID_DIFVERS
] = DIF_VERSION
;
12271 dof
->dofh_ident
[DOF_ID_DIFIREG
] = DIF_DIR_NREGS
;
12272 dof
->dofh_ident
[DOF_ID_DIFTREG
] = DIF_DTR_NREGS
;
12274 dof
->dofh_flags
= 0;
12275 dof
->dofh_hdrsize
= sizeof (dof_hdr_t
);
12276 dof
->dofh_secsize
= sizeof (dof_sec_t
);
12277 dof
->dofh_secnum
= 1; /* only DOF_SECT_OPTDESC */
12278 dof
->dofh_secoff
= sizeof (dof_hdr_t
);
12279 dof
->dofh_loadsz
= len
;
12280 dof
->dofh_filesz
= len
;
12284 * Fill in the option section header...
12286 sec
= (dof_sec_t
*)((uintptr_t)dof
+ sizeof (dof_hdr_t
));
12287 sec
->dofs_type
= DOF_SECT_OPTDESC
;
12288 sec
->dofs_align
= sizeof (uint64_t);
12289 sec
->dofs_flags
= DOF_SECF_LOAD
;
12290 sec
->dofs_entsize
= sizeof (dof_optdesc_t
);
12292 opt
= (dof_optdesc_t
*)((uintptr_t)sec
+
12293 roundup(sizeof (dof_sec_t
), sizeof (uint64_t)));
12295 sec
->dofs_offset
= (uintptr_t)opt
- (uintptr_t)dof
;
12296 sec
->dofs_size
= sizeof (dof_optdesc_t
) * DTRACEOPT_MAX
;
12298 for (i
= 0; i
< DTRACEOPT_MAX
; i
++) {
12299 opt
[i
].dofo_option
= i
;
12300 opt
[i
].dofo_strtab
= DOF_SECIDX_NONE
;
12301 opt
[i
].dofo_value
= state
->dts_options
[i
];
12308 #if !defined(__APPLE__)
12309 dtrace_dof_copyin(uintptr_t uarg
, int *errp
)
12311 dtrace_dof_copyin(user_addr_t uarg
, int *errp
)
12314 dof_hdr_t hdr
, *dof
;
12316 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_NOTOWNED
);
12319 * First, we're going to copyin() the sizeof (dof_hdr_t).
12321 #if !defined(__APPLE__)
12322 if (copyin((void *)uarg
, &hdr
, sizeof (hdr
)) != 0) {
12324 if (copyin(uarg
, &hdr
, sizeof (hdr
)) != 0) {
12326 dtrace_dof_error(NULL
, "failed to copyin DOF header");
12332 * Now we'll allocate the entire DOF and copy it in -- provided
12333 * that the length isn't outrageous.
12335 #if !defined(__APPLE__) /* Quiet compiler warnings */
12336 if (hdr
.dofh_loadsz
>= dtrace_dof_maxsize
) {
12338 if (hdr
.dofh_loadsz
>= (uint64_t)dtrace_dof_maxsize
) {
12339 #endif /* __APPLE__ */
12340 dtrace_dof_error(&hdr
, "load size exceeds maximum");
12345 if (hdr
.dofh_loadsz
< sizeof (hdr
)) {
12346 dtrace_dof_error(&hdr
, "invalid load size");
12351 #if !defined(__APPLE__)
12352 dof
= kmem_alloc(hdr
.dofh_loadsz
, KM_SLEEP
);
12354 if (copyin((void *)uarg
, dof
, hdr
.dofh_loadsz
) != 0) {
12356 dof
= dt_kmem_alloc_aligned(hdr
.dofh_loadsz
, 8, KM_SLEEP
);
12358 if (copyin(uarg
, dof
, hdr
.dofh_loadsz
) != 0) {
12360 dt_kmem_free_aligned(dof
, hdr
.dofh_loadsz
);
12368 #if defined(__APPLE__)
12371 dtrace_dof_copyin_from_proc(proc_t
* p
, user_addr_t uarg
, int *errp
)
12373 dof_hdr_t hdr
, *dof
;
12375 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_NOTOWNED
);
12378 * First, we're going to copyin() the sizeof (dof_hdr_t).
12380 if (uread(p
, &hdr
, sizeof(hdr
), uarg
) != KERN_SUCCESS
) {
12381 dtrace_dof_error(NULL
, "failed to copyin DOF header");
12387 * Now we'll allocate the entire DOF and copy it in -- provided
12388 * that the length isn't outrageous.
12390 if (hdr
.dofh_loadsz
>= (uint64_t)dtrace_dof_maxsize
) {
12391 dtrace_dof_error(&hdr
, "load size exceeds maximum");
12396 if (hdr
.dofh_loadsz
< sizeof (hdr
)) {
12397 dtrace_dof_error(&hdr
, "invalid load size");
12402 dof
= dt_kmem_alloc_aligned(hdr
.dofh_loadsz
, 8, KM_SLEEP
);
12404 if (uread(p
, dof
, hdr
.dofh_loadsz
, uarg
) != KERN_SUCCESS
) {
12405 dt_kmem_free_aligned(dof
, hdr
.dofh_loadsz
);
12413 #endif /* __APPLE__ */
12416 dtrace_dof_property(const char *name
)
12420 unsigned int len
, i
;
12424 * Unfortunately, array of values in .conf files are always (and
12425 * only) interpreted to be integer arrays. We must read our DOF
12426 * as an integer array, and then squeeze it into a byte array.
12428 #if !defined(__APPLE__) /* Quiet compiler warnings */
12429 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY
, dtrace_devi
, 0,
12430 (char *)name
, (int **)&buf
, &len
) != DDI_PROP_SUCCESS
)
12433 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY
, dtrace_devi
, 0,
12434 name
, (int **)&buf
, &len
) != DDI_PROP_SUCCESS
)
12436 #endif /* __APPLE__ */
12438 for (i
= 0; i
< len
; i
++)
12439 buf
[i
] = (uchar_t
)(((int *)buf
)[i
]);
12441 if (len
< sizeof (dof_hdr_t
)) {
12442 ddi_prop_free(buf
);
12443 dtrace_dof_error(NULL
, "truncated header");
12447 if (len
< (loadsz
= ((dof_hdr_t
*)buf
)->dofh_loadsz
)) {
12448 ddi_prop_free(buf
);
12449 dtrace_dof_error(NULL
, "truncated DOF");
12453 #if !defined(__APPLE__) /* Quiet compiler warnings */
12454 if (loadsz
>= dtrace_dof_maxsize
) {
12456 if (loadsz
>= (uint64_t)dtrace_dof_maxsize
) {
12457 #endif /* __APPLE__ */
12458 ddi_prop_free(buf
);
12459 dtrace_dof_error(NULL
, "oversized DOF");
12463 #if !defined(__APPLE__)
12464 dof
= kmem_alloc(loadsz
, KM_SLEEP
);
12466 dof
= dt_kmem_alloc_aligned(loadsz
, 8, KM_SLEEP
);
12467 #endif /* __APPLE__ */
12468 bcopy(buf
, dof
, loadsz
);
12469 ddi_prop_free(buf
);
12475 dtrace_dof_destroy(dof_hdr_t
*dof
)
12477 #if !defined(__APPLE__)
12478 kmem_free(dof
, dof
->dofh_loadsz
);
12480 dt_kmem_free_aligned(dof
, dof
->dofh_loadsz
);
12481 #endif /* __APPLE__ */
12485 * Return the dof_sec_t pointer corresponding to a given section index. If the
12486 * index is not valid, dtrace_dof_error() is called and NULL is returned. If
12487 * a type other than DOF_SECT_NONE is specified, the header is checked against
12488 * this type and NULL is returned if the types do not match.
12491 dtrace_dof_sect(dof_hdr_t
*dof
, uint32_t type
, dof_secidx_t i
)
12493 dof_sec_t
*sec
= (dof_sec_t
*)(uintptr_t)
12494 ((uintptr_t)dof
+ dof
->dofh_secoff
+ i
* dof
->dofh_secsize
);
12496 if (i
>= dof
->dofh_secnum
) {
12497 dtrace_dof_error(dof
, "referenced section index is invalid");
12501 if (!(sec
->dofs_flags
& DOF_SECF_LOAD
)) {
12502 dtrace_dof_error(dof
, "referenced section is not loadable");
12506 if (type
!= DOF_SECT_NONE
&& type
!= sec
->dofs_type
) {
12507 dtrace_dof_error(dof
, "referenced section is the wrong type");
12514 static dtrace_probedesc_t
*
12515 dtrace_dof_probedesc(dof_hdr_t
*dof
, dof_sec_t
*sec
, dtrace_probedesc_t
*desc
)
12517 dof_probedesc_t
*probe
;
12519 uintptr_t daddr
= (uintptr_t)dof
;
12523 if (sec
->dofs_type
!= DOF_SECT_PROBEDESC
) {
12524 dtrace_dof_error(dof
, "invalid probe section");
12528 if (sec
->dofs_align
!= sizeof (dof_secidx_t
)) {
12529 dtrace_dof_error(dof
, "bad alignment in probe description");
12533 if (sec
->dofs_offset
+ sizeof (dof_probedesc_t
) > dof
->dofh_loadsz
) {
12534 dtrace_dof_error(dof
, "truncated probe description");
12538 probe
= (dof_probedesc_t
*)(uintptr_t)(daddr
+ sec
->dofs_offset
);
12539 strtab
= dtrace_dof_sect(dof
, DOF_SECT_STRTAB
, probe
->dofp_strtab
);
12541 if (strtab
== NULL
)
12544 str
= daddr
+ strtab
->dofs_offset
;
12545 size
= strtab
->dofs_size
;
12547 if (probe
->dofp_provider
>= strtab
->dofs_size
) {
12548 dtrace_dof_error(dof
, "corrupt probe provider");
12552 (void) strncpy(desc
->dtpd_provider
,
12553 (char *)(str
+ probe
->dofp_provider
),
12554 MIN(DTRACE_PROVNAMELEN
- 1, size
- probe
->dofp_provider
));
12555 #if defined(__APPLE__) /* Employ size bounded string operation. */
12556 desc
->dtpd_provider
[DTRACE_PROVNAMELEN
- 1] = '\0';
12557 #endif /* __APPLE__ */
12559 if (probe
->dofp_mod
>= strtab
->dofs_size
) {
12560 dtrace_dof_error(dof
, "corrupt probe module");
12564 (void) strncpy(desc
->dtpd_mod
, (char *)(str
+ probe
->dofp_mod
),
12565 MIN(DTRACE_MODNAMELEN
- 1, size
- probe
->dofp_mod
));
12566 #if defined(__APPLE__) /* Employ size bounded string operation. */
12567 desc
->dtpd_mod
[DTRACE_MODNAMELEN
- 1] = '\0';
12568 #endif /* __APPLE__ */
12570 if (probe
->dofp_func
>= strtab
->dofs_size
) {
12571 dtrace_dof_error(dof
, "corrupt probe function");
12575 (void) strncpy(desc
->dtpd_func
, (char *)(str
+ probe
->dofp_func
),
12576 MIN(DTRACE_FUNCNAMELEN
- 1, size
- probe
->dofp_func
));
12577 #if defined(__APPLE__) /* Employ size bounded string operation. */
12578 desc
->dtpd_func
[DTRACE_FUNCNAMELEN
- 1] = '\0';
12579 #endif /* __APPLE__ */
12581 if (probe
->dofp_name
>= strtab
->dofs_size
) {
12582 dtrace_dof_error(dof
, "corrupt probe name");
12586 (void) strncpy(desc
->dtpd_name
, (char *)(str
+ probe
->dofp_name
),
12587 MIN(DTRACE_NAMELEN
- 1, size
- probe
->dofp_name
));
12588 #if defined(__APPLE__) /* Employ size bounded string operation. */
12589 desc
->dtpd_name
[DTRACE_NAMELEN
- 1] = '\0';
12590 #endif /* __APPLE__ */
12595 static dtrace_difo_t
*
12596 dtrace_dof_difo(dof_hdr_t
*dof
, dof_sec_t
*sec
, dtrace_vstate_t
*vstate
,
12601 dof_difohdr_t
*dofd
;
12602 uintptr_t daddr
= (uintptr_t)dof
;
12603 size_t max_size
= dtrace_difo_maxsize
;
12604 #if !defined(__APPLE__) /* Quiet compiler warnings */
12609 #endif /* __APPLE__ */
12612 static const struct {
12620 { DOF_SECT_DIF
, offsetof(dtrace_difo_t
, dtdo_buf
),
12621 offsetof(dtrace_difo_t
, dtdo_len
), sizeof (dif_instr_t
),
12622 sizeof (dif_instr_t
), "multiple DIF sections" },
12624 { DOF_SECT_INTTAB
, offsetof(dtrace_difo_t
, dtdo_inttab
),
12625 offsetof(dtrace_difo_t
, dtdo_intlen
), sizeof (uint64_t),
12626 sizeof (uint64_t), "multiple integer tables" },
12628 { DOF_SECT_STRTAB
, offsetof(dtrace_difo_t
, dtdo_strtab
),
12629 offsetof(dtrace_difo_t
, dtdo_strlen
), 0,
12630 sizeof (char), "multiple string tables" },
12632 { DOF_SECT_VARTAB
, offsetof(dtrace_difo_t
, dtdo_vartab
),
12633 offsetof(dtrace_difo_t
, dtdo_varlen
), sizeof (dtrace_difv_t
),
12634 sizeof (uint_t
), "multiple variable tables" },
12636 #if !defined(__APPLE__)
12637 { DOF_SECT_NONE
, 0, 0, 0, NULL
}
12639 { DOF_SECT_NONE
, 0, 0, 0, 0, NULL
}
12640 #endif /* __APPLE__ */
12643 if (sec
->dofs_type
!= DOF_SECT_DIFOHDR
) {
12644 dtrace_dof_error(dof
, "invalid DIFO header section");
12648 if (sec
->dofs_align
!= sizeof (dof_secidx_t
)) {
12649 dtrace_dof_error(dof
, "bad alignment in DIFO header");
12653 if (sec
->dofs_size
< sizeof (dof_difohdr_t
) ||
12654 sec
->dofs_size
% sizeof (dof_secidx_t
)) {
12655 dtrace_dof_error(dof
, "bad size in DIFO header");
12659 dofd
= (dof_difohdr_t
*)(uintptr_t)(daddr
+ sec
->dofs_offset
);
12660 n
= (sec
->dofs_size
- sizeof (*dofd
)) / sizeof (dof_secidx_t
) + 1;
12662 dp
= kmem_zalloc(sizeof (dtrace_difo_t
), KM_SLEEP
);
12663 dp
->dtdo_rtype
= dofd
->dofd_rtype
;
12665 for (l
= 0; l
< n
; l
++) {
12670 if ((subsec
= dtrace_dof_sect(dof
, DOF_SECT_NONE
,
12671 dofd
->dofd_links
[l
])) == NULL
)
12672 goto err
; /* invalid section link */
12674 if (ttl
+ subsec
->dofs_size
> max_size
) {
12675 dtrace_dof_error(dof
, "exceeds maximum size");
12679 ttl
+= subsec
->dofs_size
;
12681 for (i
= 0; difo
[i
].section
!= DOF_SECT_NONE
; i
++) {
12683 #if !defined(__APPLE__) /* Quiet compiler warnings */
12684 if (subsec
->dofs_type
!= difo
[i
].section
)
12687 if (subsec
->dofs_type
!= (uint32_t)difo
[i
].section
)
12689 #endif /* __APPLE __ */
12691 if (!(subsec
->dofs_flags
& DOF_SECF_LOAD
)) {
12692 dtrace_dof_error(dof
, "section not loaded");
12696 #if !defined(__APPLE__) /* Quiet compiler warnings */
12697 if (subsec
->dofs_align
!= difo
[i
].align
) {
12698 dtrace_dof_error(dof
, "bad alignment");
12702 if (subsec
->dofs_align
!= (uint32_t)difo
[i
].align
) {
12703 dtrace_dof_error(dof
, "bad alignment");
12706 #endif /* __APPLE__ */
12708 bufp
= (void **)((uintptr_t)dp
+ difo
[i
].bufoffs
);
12709 lenp
= (uint32_t *)((uintptr_t)dp
+ difo
[i
].lenoffs
);
12711 if (*bufp
!= NULL
) {
12712 dtrace_dof_error(dof
, difo
[i
].msg
);
12716 #if !defined(__APPLE__) /* Quiet compiler warnings */
12717 if (difo
[i
].entsize
!= subsec
->dofs_entsize
) {
12718 dtrace_dof_error(dof
, "entry size mismatch");
12722 if ((uint32_t)difo
[i
].entsize
!= subsec
->dofs_entsize
) {
12723 dtrace_dof_error(dof
, "entry size mismatch");
12726 #endif /* __APPLE__ */
12728 if (subsec
->dofs_entsize
!= 0 &&
12729 (subsec
->dofs_size
% subsec
->dofs_entsize
) != 0) {
12730 dtrace_dof_error(dof
, "corrupt entry size");
12734 *lenp
= subsec
->dofs_size
;
12735 *bufp
= kmem_alloc(subsec
->dofs_size
, KM_SLEEP
);
12736 bcopy((char *)(uintptr_t)(daddr
+ subsec
->dofs_offset
),
12737 *bufp
, subsec
->dofs_size
);
12739 if (subsec
->dofs_entsize
!= 0)
12740 *lenp
/= subsec
->dofs_entsize
;
12746 * If we encounter a loadable DIFO sub-section that is not
12747 * known to us, assume this is a broken program and fail.
12749 if (difo
[i
].section
== DOF_SECT_NONE
&&
12750 (subsec
->dofs_flags
& DOF_SECF_LOAD
)) {
12751 dtrace_dof_error(dof
, "unrecognized DIFO subsection");
12756 if (dp
->dtdo_buf
== NULL
) {
12758 * We can't have a DIF object without DIF text.
12760 dtrace_dof_error(dof
, "missing DIF text");
12765 * Before we validate the DIF object, run through the variable table
12766 * looking for the strings -- if any of their size are under, we'll set
12767 * their size to be the system-wide default string size. Note that
12768 * this should _not_ happen if the "strsize" option has been set --
12769 * in this case, the compiler should have set the size to reflect the
12770 * setting of the option.
12772 for (i
= 0; i
< dp
->dtdo_varlen
; i
++) {
12773 dtrace_difv_t
*v
= &dp
->dtdo_vartab
[i
];
12774 dtrace_diftype_t
*t
= &v
->dtdv_type
;
12776 if (v
->dtdv_id
< DIF_VAR_OTHER_UBASE
)
12779 if (t
->dtdt_kind
== DIF_TYPE_STRING
&& t
->dtdt_size
== 0)
12780 t
->dtdt_size
= dtrace_strsize_default
;
12783 if (dtrace_difo_validate(dp
, vstate
, DIF_DIR_NREGS
, cr
) != 0)
12786 dtrace_difo_init(dp
, vstate
);
12790 kmem_free(dp
->dtdo_buf
, dp
->dtdo_len
* sizeof (dif_instr_t
));
12791 kmem_free(dp
->dtdo_inttab
, dp
->dtdo_intlen
* sizeof (uint64_t));
12792 kmem_free(dp
->dtdo_strtab
, dp
->dtdo_strlen
);
12793 kmem_free(dp
->dtdo_vartab
, dp
->dtdo_varlen
* sizeof (dtrace_difv_t
));
12795 kmem_free(dp
, sizeof (dtrace_difo_t
));
12799 static dtrace_predicate_t
*
12800 dtrace_dof_predicate(dof_hdr_t
*dof
, dof_sec_t
*sec
, dtrace_vstate_t
*vstate
,
12805 if ((dp
= dtrace_dof_difo(dof
, sec
, vstate
, cr
)) == NULL
)
12808 return (dtrace_predicate_create(dp
));
12811 static dtrace_actdesc_t
*
12812 dtrace_dof_actdesc(dof_hdr_t
*dof
, dof_sec_t
*sec
, dtrace_vstate_t
*vstate
,
12815 dtrace_actdesc_t
*act
, *first
= NULL
, *last
= NULL
, *next
;
12816 dof_actdesc_t
*desc
;
12817 dof_sec_t
*difosec
;
12819 uintptr_t daddr
= (uintptr_t)dof
;
12821 dtrace_actkind_t kind
;
12823 if (sec
->dofs_type
!= DOF_SECT_ACTDESC
) {
12824 dtrace_dof_error(dof
, "invalid action section");
12828 if (sec
->dofs_offset
+ sizeof (dof_actdesc_t
) > dof
->dofh_loadsz
) {
12829 dtrace_dof_error(dof
, "truncated action description");
12833 if (sec
->dofs_align
!= sizeof (uint64_t)) {
12834 dtrace_dof_error(dof
, "bad alignment in action description");
12838 if (sec
->dofs_size
< sec
->dofs_entsize
) {
12839 dtrace_dof_error(dof
, "section entry size exceeds total size");
12843 if (sec
->dofs_entsize
!= sizeof (dof_actdesc_t
)) {
12844 dtrace_dof_error(dof
, "bad entry size in action description");
12848 if (sec
->dofs_size
/ sec
->dofs_entsize
> dtrace_actions_max
) {
12849 dtrace_dof_error(dof
, "actions exceed dtrace_actions_max");
12853 for (offs
= 0; offs
< sec
->dofs_size
; offs
+= sec
->dofs_entsize
) {
12854 desc
= (dof_actdesc_t
*)(daddr
+
12855 (uintptr_t)sec
->dofs_offset
+ offs
);
12856 kind
= (dtrace_actkind_t
)desc
->dofa_kind
;
12858 if (DTRACEACT_ISPRINTFLIKE(kind
) &&
12859 (kind
!= DTRACEACT_PRINTA
||
12860 desc
->dofa_strtab
!= DOF_SECIDX_NONE
)) {
12866 * printf()-like actions must have a format string.
12868 if ((strtab
= dtrace_dof_sect(dof
,
12869 DOF_SECT_STRTAB
, desc
->dofa_strtab
)) == NULL
)
12872 str
= (char *)((uintptr_t)dof
+
12873 (uintptr_t)strtab
->dofs_offset
);
12875 for (i
= desc
->dofa_arg
; i
< strtab
->dofs_size
; i
++) {
12876 if (str
[i
] == '\0')
12880 if (i
>= strtab
->dofs_size
) {
12881 dtrace_dof_error(dof
, "bogus format string");
12885 if (i
== desc
->dofa_arg
) {
12886 dtrace_dof_error(dof
, "empty format string");
12890 i
-= desc
->dofa_arg
;
12891 fmt
= kmem_alloc(i
+ 1, KM_SLEEP
);
12892 bcopy(&str
[desc
->dofa_arg
], fmt
, i
+ 1);
12893 arg
= (uint64_t)(uintptr_t)fmt
;
12895 if (kind
== DTRACEACT_PRINTA
) {
12896 ASSERT(desc
->dofa_strtab
== DOF_SECIDX_NONE
);
12899 arg
= desc
->dofa_arg
;
12903 act
= dtrace_actdesc_create(kind
, desc
->dofa_ntuple
,
12904 desc
->dofa_uarg
, arg
);
12906 if (last
!= NULL
) {
12907 last
->dtad_next
= act
;
12914 if (desc
->dofa_difo
== DOF_SECIDX_NONE
)
12917 if ((difosec
= dtrace_dof_sect(dof
,
12918 DOF_SECT_DIFOHDR
, desc
->dofa_difo
)) == NULL
)
12921 act
->dtad_difo
= dtrace_dof_difo(dof
, difosec
, vstate
, cr
);
12923 if (act
->dtad_difo
== NULL
)
12927 ASSERT(first
!= NULL
);
12931 for (act
= first
; act
!= NULL
; act
= next
) {
12932 next
= act
->dtad_next
;
12933 dtrace_actdesc_release(act
, vstate
);
12939 static dtrace_ecbdesc_t
*
12940 dtrace_dof_ecbdesc(dof_hdr_t
*dof
, dof_sec_t
*sec
, dtrace_vstate_t
*vstate
,
12943 dtrace_ecbdesc_t
*ep
;
12944 dof_ecbdesc_t
*ecb
;
12945 dtrace_probedesc_t
*desc
;
12946 dtrace_predicate_t
*pred
= NULL
;
12948 if (sec
->dofs_size
< sizeof (dof_ecbdesc_t
)) {
12949 dtrace_dof_error(dof
, "truncated ECB description");
12953 if (sec
->dofs_align
!= sizeof (uint64_t)) {
12954 dtrace_dof_error(dof
, "bad alignment in ECB description");
12958 ecb
= (dof_ecbdesc_t
*)((uintptr_t)dof
+ (uintptr_t)sec
->dofs_offset
);
12959 sec
= dtrace_dof_sect(dof
, DOF_SECT_PROBEDESC
, ecb
->dofe_probes
);
12964 ep
= kmem_zalloc(sizeof (dtrace_ecbdesc_t
), KM_SLEEP
);
12965 ep
->dted_uarg
= ecb
->dofe_uarg
;
12966 desc
= &ep
->dted_probe
;
12968 if (dtrace_dof_probedesc(dof
, sec
, desc
) == NULL
)
12971 if (ecb
->dofe_pred
!= DOF_SECIDX_NONE
) {
12972 if ((sec
= dtrace_dof_sect(dof
,
12973 DOF_SECT_DIFOHDR
, ecb
->dofe_pred
)) == NULL
)
12976 if ((pred
= dtrace_dof_predicate(dof
, sec
, vstate
, cr
)) == NULL
)
12979 ep
->dted_pred
.dtpdd_predicate
= pred
;
12982 if (ecb
->dofe_actions
!= DOF_SECIDX_NONE
) {
12983 if ((sec
= dtrace_dof_sect(dof
,
12984 DOF_SECT_ACTDESC
, ecb
->dofe_actions
)) == NULL
)
12987 ep
->dted_action
= dtrace_dof_actdesc(dof
, sec
, vstate
, cr
);
12989 if (ep
->dted_action
== NULL
)
12997 dtrace_predicate_release(pred
, vstate
);
12998 kmem_free(ep
, sizeof (dtrace_ecbdesc_t
));
13002 #if !defined(__APPLE__) /* APPLE dyld has already done this for us */
13004 * Apply the relocations from the specified 'sec' (a DOF_SECT_URELHDR) to the
13005 * specified DOF. At present, this amounts to simply adding 'ubase' to the
13006 * site of any user SETX relocations to account for load object base address.
13007 * In the future, if we need other relocations, this function can be extended.
13010 dtrace_dof_relocate(dof_hdr_t
*dof
, dof_sec_t
*sec
, uint64_t ubase
)
13012 uintptr_t daddr
= (uintptr_t)dof
;
13013 dof_relohdr_t
*dofr
=
13014 (dof_relohdr_t
*)(uintptr_t)(daddr
+ sec
->dofs_offset
);
13015 dof_sec_t
*ss
, *rs
, *ts
;
13019 if (sec
->dofs_size
< sizeof (dof_relohdr_t
) ||
13020 sec
->dofs_align
!= sizeof (dof_secidx_t
)) {
13021 dtrace_dof_error(dof
, "invalid relocation header");
13025 ss
= dtrace_dof_sect(dof
, DOF_SECT_STRTAB
, dofr
->dofr_strtab
);
13026 rs
= dtrace_dof_sect(dof
, DOF_SECT_RELTAB
, dofr
->dofr_relsec
);
13027 ts
= dtrace_dof_sect(dof
, DOF_SECT_NONE
, dofr
->dofr_tgtsec
);
13029 if (ss
== NULL
|| rs
== NULL
|| ts
== NULL
)
13030 return (-1); /* dtrace_dof_error() has been called already */
13032 if (rs
->dofs_entsize
< sizeof (dof_relodesc_t
) ||
13033 rs
->dofs_align
!= sizeof (uint64_t)) {
13034 dtrace_dof_error(dof
, "invalid relocation section");
13038 r
= (dof_relodesc_t
*)(uintptr_t)(daddr
+ rs
->dofs_offset
);
13039 n
= rs
->dofs_size
/ rs
->dofs_entsize
;
13041 for (i
= 0; i
< n
; i
++) {
13042 uintptr_t taddr
= daddr
+ ts
->dofs_offset
+ r
->dofr_offset
;
13044 switch (r
->dofr_type
) {
13045 case DOF_RELO_NONE
:
13047 case DOF_RELO_SETX
:
13048 if (r
->dofr_offset
>= ts
->dofs_size
|| r
->dofr_offset
+
13049 sizeof (uint64_t) > ts
->dofs_size
) {
13050 dtrace_dof_error(dof
, "bad relocation offset");
13054 if (!IS_P2ALIGNED(taddr
, sizeof (uint64_t))) {
13055 dtrace_dof_error(dof
, "misaligned setx relo");
13059 *(uint64_t *)taddr
+= ubase
;
13062 dtrace_dof_error(dof
, "invalid relocation type");
13066 r
= (dof_relodesc_t
*)((uintptr_t)r
+ rs
->dofs_entsize
);
13071 #endif /* __APPLE__ */
13074 * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated
13075 * header: it should be at the front of a memory region that is at least
13076 * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in
13077 * size. It need not be validated in any other way.
13080 dtrace_dof_slurp(dof_hdr_t
*dof
, dtrace_vstate_t
*vstate
, cred_t
*cr
,
13081 dtrace_enabling_t
**enabp
, uint64_t ubase
, int noprobes
)
13083 #pragma unused(ubase) /* __APPLE__ */
13084 uint64_t len
= dof
->dofh_loadsz
, seclen
;
13085 uintptr_t daddr
= (uintptr_t)dof
;
13086 dtrace_ecbdesc_t
*ep
;
13087 dtrace_enabling_t
*enab
;
13090 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
13091 ASSERT(dof
->dofh_loadsz
>= sizeof (dof_hdr_t
));
13094 * Check the DOF header identification bytes. In addition to checking
13095 * valid settings, we also verify that unused bits/bytes are zeroed so
13096 * we can use them later without fear of regressing existing binaries.
13098 if (bcmp(&dof
->dofh_ident
[DOF_ID_MAG0
],
13099 DOF_MAG_STRING
, DOF_MAG_STRLEN
) != 0) {
13100 dtrace_dof_error(dof
, "DOF magic string mismatch");
13104 if (dof
->dofh_ident
[DOF_ID_MODEL
] != DOF_MODEL_ILP32
&&
13105 dof
->dofh_ident
[DOF_ID_MODEL
] != DOF_MODEL_LP64
) {
13106 dtrace_dof_error(dof
, "DOF has invalid data model");
13110 if (dof
->dofh_ident
[DOF_ID_ENCODING
] != DOF_ENCODE_NATIVE
) {
13111 dtrace_dof_error(dof
, "DOF encoding mismatch");
13115 #if !defined(__APPLE__)
13116 if (dof
->dofh_ident
[DOF_ID_VERSION
] != DOF_VERSION_1
&&
13117 dof
->dofh_ident
[DOF_ID_VERSION
] != DOF_VERSION_2
) {
13118 dtrace_dof_error(dof
, "DOF version mismatch");
13123 * We only support DOF_VERSION_3 for now.
13125 if (dof
->dofh_ident
[DOF_ID_VERSION
] != DOF_VERSION_3
) {
13126 dtrace_dof_error(dof
, "DOF version mismatch");
13131 if (dof
->dofh_ident
[DOF_ID_DIFVERS
] != DIF_VERSION_2
) {
13132 dtrace_dof_error(dof
, "DOF uses unsupported instruction set");
13136 if (dof
->dofh_ident
[DOF_ID_DIFIREG
] > DIF_DIR_NREGS
) {
13137 dtrace_dof_error(dof
, "DOF uses too many integer registers");
13141 if (dof
->dofh_ident
[DOF_ID_DIFTREG
] > DIF_DTR_NREGS
) {
13142 dtrace_dof_error(dof
, "DOF uses too many tuple registers");
13146 for (i
= DOF_ID_PAD
; i
< DOF_ID_SIZE
; i
++) {
13147 if (dof
->dofh_ident
[i
] != 0) {
13148 dtrace_dof_error(dof
, "DOF has invalid ident byte set");
13153 if (dof
->dofh_flags
& ~DOF_FL_VALID
) {
13154 dtrace_dof_error(dof
, "DOF has invalid flag bits set");
13158 if (dof
->dofh_secsize
== 0) {
13159 dtrace_dof_error(dof
, "zero section header size");
13164 * Check that the section headers don't exceed the amount of DOF
13165 * data. Note that we cast the section size and number of sections
13166 * to uint64_t's to prevent possible overflow in the multiplication.
13168 seclen
= (uint64_t)dof
->dofh_secnum
* (uint64_t)dof
->dofh_secsize
;
13170 if (dof
->dofh_secoff
> len
|| seclen
> len
||
13171 dof
->dofh_secoff
+ seclen
> len
) {
13172 dtrace_dof_error(dof
, "truncated section headers");
13176 if (!IS_P2ALIGNED(dof
->dofh_secoff
, sizeof (uint64_t))) {
13177 dtrace_dof_error(dof
, "misaligned section headers");
13181 if (!IS_P2ALIGNED(dof
->dofh_secsize
, sizeof (uint64_t))) {
13182 dtrace_dof_error(dof
, "misaligned section size");
13187 * Take an initial pass through the section headers to be sure that
13188 * the headers don't have stray offsets. If the 'noprobes' flag is
13189 * set, do not permit sections relating to providers, probes, or args.
13191 for (i
= 0; i
< dof
->dofh_secnum
; i
++) {
13192 dof_sec_t
*sec
= (dof_sec_t
*)(daddr
+
13193 (uintptr_t)dof
->dofh_secoff
+ i
* dof
->dofh_secsize
);
13196 switch (sec
->dofs_type
) {
13197 case DOF_SECT_PROVIDER
:
13198 case DOF_SECT_PROBES
:
13199 case DOF_SECT_PRARGS
:
13200 case DOF_SECT_PROFFS
:
13201 dtrace_dof_error(dof
, "illegal sections "
13207 if (!(sec
->dofs_flags
& DOF_SECF_LOAD
))
13208 continue; /* just ignore non-loadable sections */
13210 if (sec
->dofs_align
& (sec
->dofs_align
- 1)) {
13211 dtrace_dof_error(dof
, "bad section alignment");
13215 if (sec
->dofs_offset
& (sec
->dofs_align
- 1)) {
13216 dtrace_dof_error(dof
, "misaligned section");
13220 if (sec
->dofs_offset
> len
|| sec
->dofs_size
> len
||
13221 sec
->dofs_offset
+ sec
->dofs_size
> len
) {
13222 dtrace_dof_error(dof
, "corrupt section header");
13226 if (sec
->dofs_type
== DOF_SECT_STRTAB
&& *((char *)daddr
+
13227 sec
->dofs_offset
+ sec
->dofs_size
- 1) != '\0') {
13228 dtrace_dof_error(dof
, "non-terminating string table");
13233 #if !defined(__APPLE__)
13235 * Take a second pass through the sections and locate and perform any
13236 * relocations that are present. We do this after the first pass to
13237 * be sure that all sections have had their headers validated.
13239 for (i
= 0; i
< dof
->dofh_secnum
; i
++) {
13240 dof_sec_t
*sec
= (dof_sec_t
*)(daddr
+
13241 (uintptr_t)dof
->dofh_secoff
+ i
* dof
->dofh_secsize
);
13243 if (!(sec
->dofs_flags
& DOF_SECF_LOAD
))
13244 continue; /* skip sections that are not loadable */
13246 switch (sec
->dofs_type
) {
13247 case DOF_SECT_URELHDR
:
13248 if (dtrace_dof_relocate(dof
, sec
, ubase
) != 0)
13255 * APPLE NOTE: We have no relocation to perform. All dof values are
13256 * relative offsets.
13258 #endif /* __APPLE__ */
13260 if ((enab
= *enabp
) == NULL
)
13261 enab
= *enabp
= dtrace_enabling_create(vstate
);
13263 for (i
= 0; i
< dof
->dofh_secnum
; i
++) {
13264 dof_sec_t
*sec
= (dof_sec_t
*)(daddr
+
13265 (uintptr_t)dof
->dofh_secoff
+ i
* dof
->dofh_secsize
);
13267 if (sec
->dofs_type
!= DOF_SECT_ECBDESC
)
13270 #if !defined(__APPLE__)
13271 if ((ep
= dtrace_dof_ecbdesc(dof
, sec
, vstate
, cr
)) == NULL
) {
13272 dtrace_enabling_destroy(enab
);
13277 /* Note: Defend against gcc 4.0 botch on x86 (not all paths out of inlined dtrace_dof_ecbdesc
13278 are checked for the NULL return value.) */
13279 ep
= dtrace_dof_ecbdesc(dof
, sec
, vstate
, cr
);
13281 dtrace_enabling_destroy(enab
);
13285 #endif /* __APPLE__ */
13287 dtrace_enabling_add(enab
, ep
);
13294 * Process DOF for any options. This routine assumes that the DOF has been
13295 * at least processed by dtrace_dof_slurp().
13298 dtrace_dof_options(dof_hdr_t
*dof
, dtrace_state_t
*state
)
13300 #if !defined(__APPLE__) /* Quiet compiler warnings */
13305 #endif /* __APPLE__ */
13308 dof_optdesc_t
*desc
;
13310 for (i
= 0; i
< dof
->dofh_secnum
; i
++) {
13311 dof_sec_t
*sec
= (dof_sec_t
*)((uintptr_t)dof
+
13312 (uintptr_t)dof
->dofh_secoff
+ i
* dof
->dofh_secsize
);
13314 if (sec
->dofs_type
!= DOF_SECT_OPTDESC
)
13317 if (sec
->dofs_align
!= sizeof (uint64_t)) {
13318 dtrace_dof_error(dof
, "bad alignment in "
13319 "option description");
13323 if ((entsize
= sec
->dofs_entsize
) == 0) {
13324 dtrace_dof_error(dof
, "zeroed option entry size");
13328 if (entsize
< sizeof (dof_optdesc_t
)) {
13329 dtrace_dof_error(dof
, "bad option entry size");
13333 for (offs
= 0; offs
< sec
->dofs_size
; offs
+= entsize
) {
13334 desc
= (dof_optdesc_t
*)((uintptr_t)dof
+
13335 (uintptr_t)sec
->dofs_offset
+ offs
);
13337 if (desc
->dofo_strtab
!= DOF_SECIDX_NONE
) {
13338 dtrace_dof_error(dof
, "non-zero option string");
13342 #if !defined(__APPLE__) /* Quiet compiler warnings */
13343 if (desc
->dofo_value
== DTRACEOPT_UNSET
) {
13345 if (desc
->dofo_value
== (uint64_t)DTRACEOPT_UNSET
) {
13346 #endif /* __APPLE __ */
13347 dtrace_dof_error(dof
, "unset option");
13351 if ((rval
= dtrace_state_option(state
,
13352 desc
->dofo_option
, desc
->dofo_value
)) != 0) {
13353 dtrace_dof_error(dof
, "rejected option");
13363 * DTrace Consumer State Functions
13365 #if defined(__APPLE__) /* Quiet compiler warning. */
13367 #endif /* __APPLE__ */
13369 dtrace_dstate_init(dtrace_dstate_t
*dstate
, size_t size
)
13371 size_t hashsize
, maxper
, min_size
, chunksize
= dstate
->dtds_chunksize
;
13374 dtrace_dynvar_t
*dvar
, *next
, *start
;
13375 #if !defined(__APPLE__) /* Quiet compiler warning */
13379 #endif /* __APPLE__ */
13381 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
13382 ASSERT(dstate
->dtds_base
== NULL
&& dstate
->dtds_percpu
== NULL
);
13384 bzero(dstate
, sizeof (dtrace_dstate_t
));
13386 if ((dstate
->dtds_chunksize
= chunksize
) == 0)
13387 dstate
->dtds_chunksize
= DTRACE_DYNVAR_CHUNKSIZE
;
13389 if (size
< (min_size
= dstate
->dtds_chunksize
+ sizeof (dtrace_dynhash_t
)))
13392 if ((base
= kmem_zalloc(size
, KM_NOSLEEP
)) == NULL
)
13395 dstate
->dtds_size
= size
;
13396 dstate
->dtds_base
= base
;
13397 dstate
->dtds_percpu
= kmem_cache_alloc(dtrace_state_cache
, KM_SLEEP
);
13398 bzero(dstate
->dtds_percpu
, (int)NCPU
* sizeof (dtrace_dstate_percpu_t
));
13400 hashsize
= size
/ (dstate
->dtds_chunksize
+ sizeof (dtrace_dynhash_t
));
13402 if (hashsize
!= 1 && (hashsize
& 1))
13405 dstate
->dtds_hashsize
= hashsize
;
13406 dstate
->dtds_hash
= dstate
->dtds_base
;
13409 * Set all of our hash buckets to point to the single sink, and (if
13410 * it hasn't already been set), set the sink's hash value to be the
13411 * sink sentinel value. The sink is needed for dynamic variable
13412 * lookups to know that they have iterated over an entire, valid hash
13415 for (i
= 0; i
< hashsize
; i
++)
13416 dstate
->dtds_hash
[i
].dtdh_chain
= &dtrace_dynhash_sink
;
13418 if (dtrace_dynhash_sink
.dtdv_hashval
!= DTRACE_DYNHASH_SINK
)
13419 dtrace_dynhash_sink
.dtdv_hashval
= DTRACE_DYNHASH_SINK
;
13422 * Determine number of active CPUs. Divide free list evenly among
13425 start
= (dtrace_dynvar_t
*)
13426 ((uintptr_t)base
+ hashsize
* sizeof (dtrace_dynhash_t
));
13427 limit
= (uintptr_t)base
+ size
;
13429 maxper
= (limit
- (uintptr_t)start
) / (int)NCPU
;
13430 maxper
= (maxper
/ dstate
->dtds_chunksize
) * dstate
->dtds_chunksize
;
13432 for (i
= 0; i
< NCPU
; i
++) {
13433 dstate
->dtds_percpu
[i
].dtdsc_free
= dvar
= start
;
13436 * If we don't even have enough chunks to make it once through
13437 * NCPUs, we're just going to allocate everything to the first
13438 * CPU. And if we're on the last CPU, we're going to allocate
13439 * whatever is left over. In either case, we set the limit to
13440 * be the limit of the dynamic variable space.
13442 if (maxper
== 0 || i
== NCPU
- 1) {
13443 limit
= (uintptr_t)base
+ size
;
13446 limit
= (uintptr_t)start
+ maxper
;
13447 start
= (dtrace_dynvar_t
*)limit
;
13450 ASSERT(limit
<= (uintptr_t)base
+ size
);
13453 next
= (dtrace_dynvar_t
*)((uintptr_t)dvar
+
13454 dstate
->dtds_chunksize
);
13456 if ((uintptr_t)next
+ dstate
->dtds_chunksize
>= limit
)
13459 dvar
->dtdv_next
= next
;
13470 #if defined(__APPLE__) /* Quiet compiler warning. */
13472 #endif /* __APPLE__ */
13474 dtrace_dstate_fini(dtrace_dstate_t
*dstate
)
13476 lck_mtx_assert(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
13478 if (dstate
->dtds_base
== NULL
)
13481 kmem_free(dstate
->dtds_base
, dstate
->dtds_size
);
13482 kmem_cache_free(dtrace_state_cache
, dstate
->dtds_percpu
);
13486 dtrace_vstate_fini(dtrace_vstate_t
*vstate
)
13489 * Logical XOR, where are you?
13491 ASSERT((vstate
->dtvs_nglobals
== 0) ^ (vstate
->dtvs_globals
!= NULL
));
13493 if (vstate
->dtvs_nglobals
> 0) {
13494 kmem_free(vstate
->dtvs_globals
, vstate
->dtvs_nglobals
*
13495 sizeof (dtrace_statvar_t
*));
13498 if (vstate
->dtvs_ntlocals
> 0) {
13499 kmem_free(vstate
->dtvs_tlocals
, vstate
->dtvs_ntlocals
*
13500 sizeof (dtrace_difv_t
));
13503 ASSERT((vstate
->dtvs_nlocals
== 0) ^ (vstate
->dtvs_locals
!= NULL
));
13505 if (vstate
->dtvs_nlocals
> 0) {
13506 kmem_free(vstate
->dtvs_locals
, vstate
->dtvs_nlocals
*
13507 sizeof (dtrace_statvar_t
*));
13512 dtrace_state_clean(dtrace_state_t
*state
)
13514 if (state
->dts_activity
== DTRACE_ACTIVITY_INACTIVE
)
13517 dtrace_dynvar_clean(&state
->dts_vstate
.dtvs_dynvars
);
13518 dtrace_speculation_clean(state
);
13522 dtrace_state_deadman(dtrace_state_t
*state
)
13528 now
= dtrace_gethrtime();
13530 if (state
!= dtrace_anon
.dta_state
&&
13531 now
- state
->dts_laststatus
>= dtrace_deadman_user
)
13535 * We must be sure that dts_alive never appears to be less than the
13536 * value upon entry to dtrace_state_deadman(), and because we lack a
13537 * dtrace_cas64(), we cannot store to it atomically. We thus instead
13538 * store INT64_MAX to it, followed by a memory barrier, followed by
13539 * the new value. This assures that dts_alive never appears to be
13540 * less than its true value, regardless of the order in which the
13541 * stores to the underlying storage are issued.
13543 state
->dts_alive
= INT64_MAX
;
13544 dtrace_membar_producer();
13545 state
->dts_alive
= now
;
13548 #if !defined(__APPLE__)
13550 dtrace_state_create(dev_t
*devp
, cred_t
*cr
)
13553 dtrace_state_create(dev_t
*devp
, cred_t
*cr
, dtrace_state_t
**new_state
)
13554 #endif /* __APPLE__ */
13559 dtrace_state_t
*state
;
13560 dtrace_optval_t
*opt
;
13561 int bufsize
= (int)NCPU
* sizeof (dtrace_buffer_t
), i
;
13563 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
13564 lck_mtx_assert(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
13566 #if !defined(__APPLE__)
13567 minor
= (minor_t
)(uintptr_t)vmem_alloc(dtrace_minor
, 1,
13568 VM_BESTFIT
| VM_SLEEP
);
13570 if (ddi_soft_state_zalloc(dtrace_softstate
, minor
) != DDI_SUCCESS
) {
13571 vmem_free(dtrace_minor
, (void *)(uintptr_t)minor
, 1);
13575 /* Cause restart */
13579 * Darwin's DEVFS layer acquired the minor number for this "device" when it called
13580 * dtrace_devfs_clone_func(). At that time, dtrace_devfs_clone_func() proposed a minor number
13581 * (next unused according to vmem_alloc()) and then immediately put the number back in play
13582 * (by calling vmem_free()). Now that minor number is being used for an open, so committing it
13583 * to use. The following vmem_alloc() must deliver that same minor number. FIXME.
13586 minor
= (minor_t
)(uintptr_t)vmem_alloc(dtrace_minor
, 1,
13587 VM_BESTFIT
| VM_SLEEP
);
13589 if (NULL
!= devp
) {
13590 ASSERT(getminor(*devp
) == minor
);
13591 if (getminor(*devp
) != minor
) {
13592 printf("dtrace_open: couldn't re-acquire vended minor number %d. Instead got %d\n",
13593 getminor(*devp
), minor
);
13594 vmem_free(dtrace_minor
, (void *)(uintptr_t)minor
, 1);
13595 return (ERESTART
); /* can't reacquire */
13598 /* NULL==devp iff "Anonymous state" (see dtrace_anon_property),
13599 * so just vend the minor device number here de novo since no "open" has occurred. */
13602 if (ddi_soft_state_zalloc(dtrace_softstate
, minor
) != DDI_SUCCESS
) {
13603 vmem_free(dtrace_minor
, (void *)(uintptr_t)minor
, 1);
13604 return (EAGAIN
); /* temporary resource shortage */
13607 #endif /* __APPLE__ */
13609 state
= ddi_get_soft_state(dtrace_softstate
, minor
);
13610 state
->dts_epid
= DTRACE_EPIDNONE
+ 1;
13612 (void) snprintf(c
, sizeof (c
), "dtrace_aggid_%d", minor
);
13613 state
->dts_aggid_arena
= vmem_create(c
, (void *)1, UINT32_MAX
, 1,
13614 NULL
, NULL
, NULL
, 0, VM_SLEEP
| VMC_IDENTIFIER
);
13616 if (devp
!= NULL
) {
13617 major
= getemajor(*devp
);
13619 major
= ddi_driver_major(dtrace_devi
);
13622 state
->dts_dev
= makedevice(major
, minor
);
13625 *devp
= state
->dts_dev
;
13628 * We allocate NCPU buffers. On the one hand, this can be quite
13629 * a bit of memory per instance (nearly 36K on a Starcat). On the
13630 * other hand, it saves an additional memory reference in the probe
13633 state
->dts_buffer
= kmem_zalloc(bufsize
, KM_SLEEP
);
13634 state
->dts_aggbuffer
= kmem_zalloc(bufsize
, KM_SLEEP
);
13635 state
->dts_cleaner
= CYCLIC_NONE
;
13636 state
->dts_deadman
= CYCLIC_NONE
;
13637 state
->dts_vstate
.dtvs_state
= state
;
13639 for (i
= 0; i
< DTRACEOPT_MAX
; i
++)
13640 state
->dts_options
[i
] = DTRACEOPT_UNSET
;
13643 * Set the default options.
13645 opt
= state
->dts_options
;
13646 opt
[DTRACEOPT_BUFPOLICY
] = DTRACEOPT_BUFPOLICY_SWITCH
;
13647 opt
[DTRACEOPT_BUFRESIZE
] = DTRACEOPT_BUFRESIZE_AUTO
;
13648 opt
[DTRACEOPT_NSPEC
] = dtrace_nspec_default
;
13649 opt
[DTRACEOPT_SPECSIZE
] = dtrace_specsize_default
;
13650 opt
[DTRACEOPT_CPU
] = (dtrace_optval_t
)DTRACE_CPUALL
;
13651 opt
[DTRACEOPT_STRSIZE
] = dtrace_strsize_default
;
13652 opt
[DTRACEOPT_STACKFRAMES
] = dtrace_stackframes_default
;
13653 opt
[DTRACEOPT_USTACKFRAMES
] = dtrace_ustackframes_default
;
13654 opt
[DTRACEOPT_CLEANRATE
] = dtrace_cleanrate_default
;
13655 opt
[DTRACEOPT_AGGRATE
] = dtrace_aggrate_default
;
13656 opt
[DTRACEOPT_SWITCHRATE
] = dtrace_switchrate_default
;
13657 opt
[DTRACEOPT_STATUSRATE
] = dtrace_statusrate_default
;
13658 opt
[DTRACEOPT_JSTACKFRAMES
] = dtrace_jstackframes_default
;
13659 opt
[DTRACEOPT_JSTACKSTRSIZE
] = dtrace_jstackstrsize_default
;
13661 state
->dts_activity
= DTRACE_ACTIVITY_INACTIVE
;
13664 * Depending on the user credentials, we set flag bits which alter probe
13665 * visibility or the amount of destructiveness allowed. In the case of
13666 * actual anonymous tracing, or the possession of all privileges, all of
13667 * the normal checks are bypassed.
13669 if (cr
== NULL
|| PRIV_POLICY_ONLY(cr
, PRIV_ALL
, B_FALSE
)) {
13670 state
->dts_cred
.dcr_visible
= DTRACE_CRV_ALL
;
13671 state
->dts_cred
.dcr_action
= DTRACE_CRA_ALL
;
13674 * Set up the credentials for this instantiation. We take a
13675 * hold on the credential to prevent it from disappearing on
13676 * us; this in turn prevents the zone_t referenced by this
13677 * credential from disappearing. This means that we can
13678 * examine the credential and the zone from probe context.
13681 state
->dts_cred
.dcr_cred
= cr
;
13684 * CRA_PROC means "we have *some* privilege for dtrace" and
13685 * unlocks the use of variables like pid, zonename, etc.
13687 if (PRIV_POLICY_ONLY(cr
, PRIV_DTRACE_USER
, B_FALSE
) ||
13688 PRIV_POLICY_ONLY(cr
, PRIV_DTRACE_PROC
, B_FALSE
)) {
13689 state
->dts_cred
.dcr_action
|= DTRACE_CRA_PROC
;
13693 * dtrace_user allows use of syscall and profile providers.
13694 * If the user also has proc_owner and/or proc_zone, we
13695 * extend the scope to include additional visibility and
13696 * destructive power.
13698 if (PRIV_POLICY_ONLY(cr
, PRIV_DTRACE_USER
, B_FALSE
)) {
13699 if (PRIV_POLICY_ONLY(cr
, PRIV_PROC_OWNER
, B_FALSE
)) {
13700 state
->dts_cred
.dcr_visible
|=
13701 DTRACE_CRV_ALLPROC
;
13703 state
->dts_cred
.dcr_action
|=
13704 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER
;
13707 if (PRIV_POLICY_ONLY(cr
, PRIV_PROC_ZONE
, B_FALSE
)) {
13708 state
->dts_cred
.dcr_visible
|=
13709 DTRACE_CRV_ALLZONE
;
13711 state
->dts_cred
.dcr_action
|=
13712 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE
;
13716 * If we have all privs in whatever zone this is,
13717 * we can do destructive things to processes which
13718 * have altered credentials.
13720 #if !defined(__APPLE__)
13721 if (priv_isequalset(priv_getset(cr
, PRIV_EFFECTIVE
),
13722 cr
->cr_zone
->zone_privset
)) {
13723 state
->dts_cred
.dcr_action
|=
13724 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG
;
13727 /* Darwin doesn't do zones. */
13728 state
->dts_cred
.dcr_action
|=
13729 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG
;
13730 #endif /* __APPLE__ */
13734 * Holding the dtrace_kernel privilege also implies that
13735 * the user has the dtrace_user privilege from a visibility
13736 * perspective. But without further privileges, some
13737 * destructive actions are not available.
13739 if (PRIV_POLICY_ONLY(cr
, PRIV_DTRACE_KERNEL
, B_FALSE
)) {
13741 * Make all probes in all zones visible. However,
13742 * this doesn't mean that all actions become available
13745 state
->dts_cred
.dcr_visible
|= DTRACE_CRV_KERNEL
|
13746 DTRACE_CRV_ALLPROC
| DTRACE_CRV_ALLZONE
;
13748 state
->dts_cred
.dcr_action
|= DTRACE_CRA_KERNEL
|
13751 * Holding proc_owner means that destructive actions
13752 * for *this* zone are allowed.
13754 if (PRIV_POLICY_ONLY(cr
, PRIV_PROC_OWNER
, B_FALSE
))
13755 state
->dts_cred
.dcr_action
|=
13756 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER
;
13759 * Holding proc_zone means that destructive actions
13760 * for this user/group ID in all zones is allowed.
13762 if (PRIV_POLICY_ONLY(cr
, PRIV_PROC_ZONE
, B_FALSE
))
13763 state
->dts_cred
.dcr_action
|=
13764 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE
;
13767 * If we have all privs in whatever zone this is,
13768 * we can do destructive things to processes which
13769 * have altered credentials.
13771 #if !defined(__APPLE__)
13772 if (priv_isequalset(priv_getset(cr
, PRIV_EFFECTIVE
),
13773 cr
->cr_zone
->zone_privset
)) {
13774 state
->dts_cred
.dcr_action
|=
13775 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG
;
13778 /* Darwin doesn't do zones. */
13779 state
->dts_cred
.dcr_action
|=
13780 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG
;
13781 #endif /* __APPLE__ */
13785 * Holding the dtrace_proc privilege gives control over fasttrap
13786 * and pid providers. We need to grant wider destructive
13787 * privileges in the event that the user has proc_owner and/or
13790 if (PRIV_POLICY_ONLY(cr
, PRIV_DTRACE_PROC
, B_FALSE
)) {
13791 if (PRIV_POLICY_ONLY(cr
, PRIV_PROC_OWNER
, B_FALSE
))
13792 state
->dts_cred
.dcr_action
|=
13793 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER
;
13795 if (PRIV_POLICY_ONLY(cr
, PRIV_PROC_ZONE
, B_FALSE
))
13796 state
->dts_cred
.dcr_action
|=
13797 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE
;
13801 #if !defined(__APPLE__)
13804 *new_state
= state
;
13805 return(0); /* Success */
13806 #endif /* __APPLE__ */
13810 dtrace_state_buffer(dtrace_state_t
*state
, dtrace_buffer_t
*buf
, int which
)
13812 dtrace_optval_t
*opt
= state
->dts_options
, size
;
13813 processorid_t cpu
= 0;
13814 int flags
= 0, rval
;
13816 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
13817 lck_mtx_assert(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
13818 ASSERT(which
< DTRACEOPT_MAX
);
13819 ASSERT(state
->dts_activity
== DTRACE_ACTIVITY_INACTIVE
||
13820 (state
== dtrace_anon
.dta_state
&&
13821 state
->dts_activity
== DTRACE_ACTIVITY_ACTIVE
));
13823 if (opt
[which
] == DTRACEOPT_UNSET
|| opt
[which
] == 0)
13826 if (opt
[DTRACEOPT_CPU
] != DTRACEOPT_UNSET
)
13827 cpu
= opt
[DTRACEOPT_CPU
];
13829 if (which
== DTRACEOPT_SPECSIZE
)
13830 flags
|= DTRACEBUF_NOSWITCH
;
13832 if (which
== DTRACEOPT_BUFSIZE
) {
13833 if (opt
[DTRACEOPT_BUFPOLICY
] == DTRACEOPT_BUFPOLICY_RING
)
13834 flags
|= DTRACEBUF_RING
;
13836 if (opt
[DTRACEOPT_BUFPOLICY
] == DTRACEOPT_BUFPOLICY_FILL
)
13837 flags
|= DTRACEBUF_FILL
;
13839 if (state
!= dtrace_anon
.dta_state
||
13840 state
->dts_activity
!= DTRACE_ACTIVITY_ACTIVE
)
13841 flags
|= DTRACEBUF_INACTIVE
;
13844 #if !defined(__APPLE__) /* Quiet compiler warning */
13845 for (size
= opt
[which
]; size
>= sizeof (uint64_t); size
>>= 1) {
13847 for (size
= opt
[which
]; (size_t)size
>= sizeof (uint64_t); size
>>= 1) {
13848 #endif /* __APPLE__ */
13850 * The size must be 8-byte aligned. If the size is not 8-byte
13851 * aligned, drop it down by the difference.
13853 if (size
& (sizeof (uint64_t) - 1))
13854 size
-= size
& (sizeof (uint64_t) - 1);
13856 if (size
< state
->dts_reserve
) {
13858 * Buffers always must be large enough to accommodate
13859 * their prereserved space. We return E2BIG instead
13860 * of ENOMEM in this case to allow for user-level
13861 * software to differentiate the cases.
13866 rval
= dtrace_buffer_alloc(buf
, size
, flags
, cpu
);
13868 if (rval
!= ENOMEM
) {
13873 if (opt
[DTRACEOPT_BUFRESIZE
] == DTRACEOPT_BUFRESIZE_MANUAL
)
13881 dtrace_state_buffers(dtrace_state_t
*state
)
13883 dtrace_speculation_t
*spec
= state
->dts_speculations
;
13886 if ((rval
= dtrace_state_buffer(state
, state
->dts_buffer
,
13887 DTRACEOPT_BUFSIZE
)) != 0)
13890 if ((rval
= dtrace_state_buffer(state
, state
->dts_aggbuffer
,
13891 DTRACEOPT_AGGSIZE
)) != 0)
13894 for (i
= 0; i
< state
->dts_nspeculations
; i
++) {
13895 if ((rval
= dtrace_state_buffer(state
,
13896 spec
[i
].dtsp_buffer
, DTRACEOPT_SPECSIZE
)) != 0)
13904 dtrace_state_prereserve(dtrace_state_t
*state
)
13907 dtrace_probe_t
*probe
;
13909 state
->dts_reserve
= 0;
13911 if (state
->dts_options
[DTRACEOPT_BUFPOLICY
] != DTRACEOPT_BUFPOLICY_FILL
)
13915 * If our buffer policy is a "fill" buffer policy, we need to set the
13916 * prereserved space to be the space required by the END probes.
13918 probe
= dtrace_probes
[dtrace_probeid_end
- 1];
13919 ASSERT(probe
!= NULL
);
13921 for (ecb
= probe
->dtpr_ecb
; ecb
!= NULL
; ecb
= ecb
->dte_next
) {
13922 if (ecb
->dte_state
!= state
)
13925 state
->dts_reserve
+= ecb
->dte_needed
+ ecb
->dte_alignment
;
13930 dtrace_state_go(dtrace_state_t
*state
, processorid_t
*cpu
)
13932 dtrace_optval_t
*opt
= state
->dts_options
, sz
, nspec
;
13933 dtrace_speculation_t
*spec
;
13934 dtrace_buffer_t
*buf
;
13935 cyc_handler_t hdlr
;
13937 int rval
= 0, i
, bufsize
= (int)NCPU
* sizeof (dtrace_buffer_t
);
13938 dtrace_icookie_t cookie
;
13940 lck_mtx_lock(&cpu_lock
);
13941 lck_mtx_lock(&dtrace_lock
);
13943 if (state
->dts_activity
!= DTRACE_ACTIVITY_INACTIVE
) {
13949 * Before we can perform any checks, we must prime all of the
13950 * retained enablings that correspond to this state.
13952 dtrace_enabling_prime(state
);
13954 if (state
->dts_destructive
&& !state
->dts_cred
.dcr_destructive
) {
13959 dtrace_state_prereserve(state
);
13962 * Now we want to do is try to allocate our speculations.
13963 * We do not automatically resize the number of speculations; if
13964 * this fails, we will fail the operation.
13966 nspec
= opt
[DTRACEOPT_NSPEC
];
13967 ASSERT(nspec
!= DTRACEOPT_UNSET
);
13969 if (nspec
> INT_MAX
) {
13974 spec
= kmem_zalloc(nspec
* sizeof (dtrace_speculation_t
), KM_NOSLEEP
);
13976 if (spec
== NULL
) {
13981 state
->dts_speculations
= spec
;
13982 state
->dts_nspeculations
= (int)nspec
;
13984 for (i
= 0; i
< nspec
; i
++) {
13985 if ((buf
= kmem_zalloc(bufsize
, KM_NOSLEEP
)) == NULL
) {
13990 spec
[i
].dtsp_buffer
= buf
;
13993 if (opt
[DTRACEOPT_GRABANON
] != DTRACEOPT_UNSET
) {
13994 if (dtrace_anon
.dta_state
== NULL
) {
13999 if (state
->dts_necbs
!= 0) {
14004 state
->dts_anon
= dtrace_anon_grab();
14005 ASSERT(state
->dts_anon
!= NULL
);
14006 state
= state
->dts_anon
;
14009 * We want "grabanon" to be set in the grabbed state, so we'll
14010 * copy that option value from the grabbing state into the
14013 state
->dts_options
[DTRACEOPT_GRABANON
] =
14014 opt
[DTRACEOPT_GRABANON
];
14016 *cpu
= dtrace_anon
.dta_beganon
;
14019 * If the anonymous state is active (as it almost certainly
14020 * is if the anonymous enabling ultimately matched anything),
14021 * we don't allow any further option processing -- but we
14022 * don't return failure.
14024 if (state
->dts_activity
!= DTRACE_ACTIVITY_INACTIVE
)
14028 if (opt
[DTRACEOPT_AGGSIZE
] != DTRACEOPT_UNSET
&&
14029 opt
[DTRACEOPT_AGGSIZE
] != 0) {
14030 if (state
->dts_aggregations
== NULL
) {
14032 * We're not going to create an aggregation buffer
14033 * because we don't have any ECBs that contain
14034 * aggregations -- set this option to 0.
14036 opt
[DTRACEOPT_AGGSIZE
] = 0;
14039 * If we have an aggregation buffer, we must also have
14040 * a buffer to use as scratch.
14042 #if !defined(__APPLE__) /* Quiet compiler warning */
14043 if (opt
[DTRACEOPT_BUFSIZE
] == DTRACEOPT_UNSET
||
14044 opt
[DTRACEOPT_BUFSIZE
] < state
->dts_needed
) {
14045 opt
[DTRACEOPT_BUFSIZE
] = state
->dts_needed
;
14048 if (opt
[DTRACEOPT_BUFSIZE
] == DTRACEOPT_UNSET
||
14049 (size_t)opt
[DTRACEOPT_BUFSIZE
] < state
->dts_needed
) {
14050 opt
[DTRACEOPT_BUFSIZE
] = state
->dts_needed
;
14052 #endif /* __APPLE__ */
14056 if (opt
[DTRACEOPT_SPECSIZE
] != DTRACEOPT_UNSET
&&
14057 opt
[DTRACEOPT_SPECSIZE
] != 0) {
14058 if (!state
->dts_speculates
) {
14060 * We're not going to create speculation buffers
14061 * because we don't have any ECBs that actually
14062 * speculate -- set the speculation size to 0.
14064 opt
[DTRACEOPT_SPECSIZE
] = 0;
14069 * The bare minimum size for any buffer that we're actually going to
14070 * do anything to is sizeof (uint64_t).
14072 sz
= sizeof (uint64_t);
14074 if ((state
->dts_needed
!= 0 && opt
[DTRACEOPT_BUFSIZE
] < sz
) ||
14075 (state
->dts_speculates
&& opt
[DTRACEOPT_SPECSIZE
] < sz
) ||
14076 (state
->dts_aggregations
!= NULL
&& opt
[DTRACEOPT_AGGSIZE
] < sz
)) {
14078 * A buffer size has been explicitly set to 0 (or to a size
14079 * that will be adjusted to 0) and we need the space -- we
14080 * need to return failure. We return ENOSPC to differentiate
14081 * it from failing to allocate a buffer due to failure to meet
14082 * the reserve (for which we return E2BIG).
14088 if ((rval
= dtrace_state_buffers(state
)) != 0)
14091 if ((sz
= opt
[DTRACEOPT_DYNVARSIZE
]) == DTRACEOPT_UNSET
)
14092 sz
= dtrace_dstate_defsize
;
14095 rval
= dtrace_dstate_init(&state
->dts_vstate
.dtvs_dynvars
, sz
);
14100 if (opt
[DTRACEOPT_BUFRESIZE
] == DTRACEOPT_BUFRESIZE_MANUAL
)
14102 } while (sz
>>= 1);
14104 opt
[DTRACEOPT_DYNVARSIZE
] = sz
;
14109 if (opt
[DTRACEOPT_STATUSRATE
] > dtrace_statusrate_max
)
14110 opt
[DTRACEOPT_STATUSRATE
] = dtrace_statusrate_max
;
14112 if (opt
[DTRACEOPT_CLEANRATE
] == 0)
14113 opt
[DTRACEOPT_CLEANRATE
] = dtrace_cleanrate_max
;
14115 if (opt
[DTRACEOPT_CLEANRATE
] < dtrace_cleanrate_min
)
14116 opt
[DTRACEOPT_CLEANRATE
] = dtrace_cleanrate_min
;
14118 if (opt
[DTRACEOPT_CLEANRATE
] > dtrace_cleanrate_max
)
14119 opt
[DTRACEOPT_CLEANRATE
] = dtrace_cleanrate_max
;
14121 hdlr
.cyh_func
= (cyc_func_t
)dtrace_state_clean
;
14122 hdlr
.cyh_arg
= state
;
14123 hdlr
.cyh_level
= CY_LOW_LEVEL
;
14126 when
.cyt_interval
= opt
[DTRACEOPT_CLEANRATE
];
14128 state
->dts_cleaner
= cyclic_add(&hdlr
, &when
);
14130 hdlr
.cyh_func
= (cyc_func_t
)dtrace_state_deadman
;
14131 hdlr
.cyh_arg
= state
;
14132 hdlr
.cyh_level
= CY_LOW_LEVEL
;
14135 when
.cyt_interval
= dtrace_deadman_interval
;
14137 state
->dts_alive
= state
->dts_laststatus
= dtrace_gethrtime();
14138 state
->dts_deadman
= cyclic_add(&hdlr
, &when
);
14140 state
->dts_activity
= DTRACE_ACTIVITY_WARMUP
;
14143 * Now it's time to actually fire the BEGIN probe. We need to disable
14144 * interrupts here both to record the CPU on which we fired the BEGIN
14145 * probe (the data from this CPU will be processed first at user
14146 * level) and to manually activate the buffer for this CPU.
14148 cookie
= dtrace_interrupt_disable();
14149 *cpu
= CPU
->cpu_id
;
14150 ASSERT(state
->dts_buffer
[*cpu
].dtb_flags
& DTRACEBUF_INACTIVE
);
14151 state
->dts_buffer
[*cpu
].dtb_flags
&= ~DTRACEBUF_INACTIVE
;
14153 dtrace_probe(dtrace_probeid_begin
,
14154 (uint64_t)(uintptr_t)state
, 0, 0, 0, 0);
14155 dtrace_interrupt_enable(cookie
);
14157 * We may have had an exit action from a BEGIN probe; only change our
14158 * state to ACTIVE if we're still in WARMUP.
14160 ASSERT(state
->dts_activity
== DTRACE_ACTIVITY_WARMUP
||
14161 state
->dts_activity
== DTRACE_ACTIVITY_DRAINING
);
14163 if (state
->dts_activity
== DTRACE_ACTIVITY_WARMUP
)
14164 state
->dts_activity
= DTRACE_ACTIVITY_ACTIVE
;
14167 * Regardless of whether or not now we're in ACTIVE or DRAINING, we
14168 * want each CPU to transition its principal buffer out of the
14169 * INACTIVE state. Doing this assures that no CPU will suddenly begin
14170 * processing an ECB halfway down a probe's ECB chain; all CPUs will
14171 * atomically transition from processing none of a state's ECBs to
14172 * processing all of them.
14174 dtrace_xcall(DTRACE_CPUALL
,
14175 (dtrace_xcall_t
)dtrace_buffer_activate
, state
);
14179 dtrace_buffer_free(state
->dts_buffer
);
14180 dtrace_buffer_free(state
->dts_aggbuffer
);
14182 if ((nspec
= state
->dts_nspeculations
) == 0) {
14183 ASSERT(state
->dts_speculations
== NULL
);
14187 spec
= state
->dts_speculations
;
14188 ASSERT(spec
!= NULL
);
14190 for (i
= 0; i
< state
->dts_nspeculations
; i
++) {
14191 if ((buf
= spec
[i
].dtsp_buffer
) == NULL
)
14194 dtrace_buffer_free(buf
);
14195 kmem_free(buf
, bufsize
);
14198 kmem_free(spec
, nspec
* sizeof (dtrace_speculation_t
));
14199 state
->dts_nspeculations
= 0;
14200 state
->dts_speculations
= NULL
;
14203 lck_mtx_unlock(&dtrace_lock
);
14204 lck_mtx_unlock(&cpu_lock
);
14210 dtrace_state_stop(dtrace_state_t
*state
, processorid_t
*cpu
)
14212 dtrace_icookie_t cookie
;
14214 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
14216 if (state
->dts_activity
!= DTRACE_ACTIVITY_ACTIVE
&&
14217 state
->dts_activity
!= DTRACE_ACTIVITY_DRAINING
)
14221 * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync
14222 * to be sure that every CPU has seen it. See below for the details
14223 * on why this is done.
14225 state
->dts_activity
= DTRACE_ACTIVITY_DRAINING
;
14229 * By this point, it is impossible for any CPU to be still processing
14230 * with DTRACE_ACTIVITY_ACTIVE. We can thus set our activity to
14231 * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any
14232 * other CPU in dtrace_buffer_reserve(). This allows dtrace_probe()
14233 * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN
14234 * iff we're in the END probe.
14236 state
->dts_activity
= DTRACE_ACTIVITY_COOLDOWN
;
14238 ASSERT(state
->dts_activity
== DTRACE_ACTIVITY_COOLDOWN
);
14241 * Finally, we can release the reserve and call the END probe. We
14242 * disable interrupts across calling the END probe to allow us to
14243 * return the CPU on which we actually called the END probe. This
14244 * allows user-land to be sure that this CPU's principal buffer is
14247 state
->dts_reserve
= 0;
14249 cookie
= dtrace_interrupt_disable();
14250 *cpu
= CPU
->cpu_id
;
14251 dtrace_probe(dtrace_probeid_end
,
14252 (uint64_t)(uintptr_t)state
, 0, 0, 0, 0);
14253 dtrace_interrupt_enable(cookie
);
14255 state
->dts_activity
= DTRACE_ACTIVITY_STOPPED
;
14262 dtrace_state_option(dtrace_state_t
*state
, dtrace_optid_t option
,
14263 dtrace_optval_t val
)
14265 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
14267 if (state
->dts_activity
!= DTRACE_ACTIVITY_INACTIVE
)
14270 if (option
>= DTRACEOPT_MAX
)
14273 if (option
!= DTRACEOPT_CPU
&& val
< 0)
14277 case DTRACEOPT_DESTRUCTIVE
:
14278 if (dtrace_destructive_disallow
)
14281 state
->dts_cred
.dcr_destructive
= 1;
14284 case DTRACEOPT_BUFSIZE
:
14285 case DTRACEOPT_DYNVARSIZE
:
14286 case DTRACEOPT_AGGSIZE
:
14287 case DTRACEOPT_SPECSIZE
:
14288 case DTRACEOPT_STRSIZE
:
14292 if (val
>= LONG_MAX
) {
14294 * If this is an otherwise negative value, set it to
14295 * the highest multiple of 128m less than LONG_MAX.
14296 * Technically, we're adjusting the size without
14297 * regard to the buffer resizing policy, but in fact,
14298 * this has no effect -- if we set the buffer size to
14299 * ~LONG_MAX and the buffer policy is ultimately set to
14300 * be "manual", the buffer allocation is guaranteed to
14301 * fail, if only because the allocation requires two
14302 * buffers. (We set the the size to the highest
14303 * multiple of 128m because it ensures that the size
14304 * will remain a multiple of a megabyte when
14305 * repeatedly halved -- all the way down to 15m.)
14307 val
= LONG_MAX
- (1 << 27) + 1;
14311 state
->dts_options
[option
] = val
;
14317 dtrace_state_destroy(dtrace_state_t
*state
)
14320 dtrace_vstate_t
*vstate
= &state
->dts_vstate
;
14321 minor_t minor
= getminor(state
->dts_dev
);
14322 int i
, bufsize
= (int)NCPU
* sizeof (dtrace_buffer_t
);
14323 dtrace_speculation_t
*spec
= state
->dts_speculations
;
14324 int nspec
= state
->dts_nspeculations
;
14327 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
14328 lck_mtx_assert(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
14331 * First, retract any retained enablings for this state.
14333 dtrace_enabling_retract(state
);
14334 ASSERT(state
->dts_nretained
== 0);
14336 if (state
->dts_activity
== DTRACE_ACTIVITY_ACTIVE
||
14337 state
->dts_activity
== DTRACE_ACTIVITY_DRAINING
) {
14339 * We have managed to come into dtrace_state_destroy() on a
14340 * hot enabling -- almost certainly because of a disorderly
14341 * shutdown of a consumer. (That is, a consumer that is
14342 * exiting without having called dtrace_stop().) In this case,
14343 * we're going to set our activity to be KILLED, and then
14344 * issue a sync to be sure that everyone is out of probe
14345 * context before we start blowing away ECBs.
14347 state
->dts_activity
= DTRACE_ACTIVITY_KILLED
;
14352 * Release the credential hold we took in dtrace_state_create().
14354 if (state
->dts_cred
.dcr_cred
!= NULL
)
14355 crfree(state
->dts_cred
.dcr_cred
);
14358 * Now we can safely disable and destroy any enabled probes. Because
14359 * any DTRACE_PRIV_KERNEL probes may actually be slowing our progress
14360 * (especially if they're all enabled), we take two passes through the
14361 * ECBs: in the first, we disable just DTRACE_PRIV_KERNEL probes, and
14362 * in the second we disable whatever is left over.
14364 for (match
= DTRACE_PRIV_KERNEL
; ; match
= 0) {
14365 for (i
= 0; i
< state
->dts_necbs
; i
++) {
14366 if ((ecb
= state
->dts_ecbs
[i
]) == NULL
)
14369 if (match
&& ecb
->dte_probe
!= NULL
) {
14370 dtrace_probe_t
*probe
= ecb
->dte_probe
;
14371 dtrace_provider_t
*prov
= probe
->dtpr_provider
;
14373 if (!(prov
->dtpv_priv
.dtpp_flags
& match
))
14377 dtrace_ecb_disable(ecb
);
14378 dtrace_ecb_destroy(ecb
);
14386 * Before we free the buffers, perform one more sync to assure that
14387 * every CPU is out of probe context.
14391 dtrace_buffer_free(state
->dts_buffer
);
14392 dtrace_buffer_free(state
->dts_aggbuffer
);
14394 for (i
= 0; i
< nspec
; i
++)
14395 dtrace_buffer_free(spec
[i
].dtsp_buffer
);
14397 if (state
->dts_cleaner
!= CYCLIC_NONE
)
14398 cyclic_remove(state
->dts_cleaner
);
14400 if (state
->dts_deadman
!= CYCLIC_NONE
)
14401 cyclic_remove(state
->dts_deadman
);
14403 dtrace_dstate_fini(&vstate
->dtvs_dynvars
);
14404 dtrace_vstate_fini(vstate
);
14405 kmem_free(state
->dts_ecbs
, state
->dts_necbs
* sizeof (dtrace_ecb_t
*));
14407 if (state
->dts_aggregations
!= NULL
) {
14409 for (i
= 0; i
< state
->dts_naggregations
; i
++)
14410 ASSERT(state
->dts_aggregations
[i
] == NULL
);
14412 ASSERT(state
->dts_naggregations
> 0);
14413 kmem_free(state
->dts_aggregations
,
14414 state
->dts_naggregations
* sizeof (dtrace_aggregation_t
*));
14417 kmem_free(state
->dts_buffer
, bufsize
);
14418 kmem_free(state
->dts_aggbuffer
, bufsize
);
14420 for (i
= 0; i
< nspec
; i
++)
14421 kmem_free(spec
[i
].dtsp_buffer
, bufsize
);
14423 kmem_free(spec
, nspec
* sizeof (dtrace_speculation_t
));
14425 dtrace_format_destroy(state
);
14427 vmem_destroy(state
->dts_aggid_arena
);
14428 ddi_soft_state_free(dtrace_softstate
, minor
);
14429 vmem_free(dtrace_minor
, (void *)(uintptr_t)minor
, 1);
14433 * DTrace Anonymous Enabling Functions
14435 static dtrace_state_t
*
14436 dtrace_anon_grab(void)
14438 dtrace_state_t
*state
;
14440 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
14442 if ((state
= dtrace_anon
.dta_state
) == NULL
) {
14443 ASSERT(dtrace_anon
.dta_enabling
== NULL
);
14447 ASSERT(dtrace_anon
.dta_enabling
!= NULL
);
14448 ASSERT(dtrace_retained
!= NULL
);
14450 dtrace_enabling_destroy(dtrace_anon
.dta_enabling
);
14451 dtrace_anon
.dta_enabling
= NULL
;
14452 dtrace_anon
.dta_state
= NULL
;
14458 dtrace_anon_property(void)
14461 dtrace_state_t
*state
;
14463 char c
[32]; /* enough for "dof-data-" + digits */
14465 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
14466 lck_mtx_assert(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
14468 for (i
= 0; ; i
++) {
14469 (void) snprintf(c
, sizeof (c
), "dof-data-%d", i
);
14471 dtrace_err_verbose
= 1;
14473 if ((dof
= dtrace_dof_property(c
)) == NULL
) {
14474 dtrace_err_verbose
= 0;
14479 * We want to create anonymous state, so we need to transition
14480 * the kernel debugger to indicate that DTrace is active. If
14481 * this fails (e.g. because the debugger has modified text in
14482 * some way), we won't continue with the processing.
14484 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE
) != 0) {
14485 cmn_err(CE_NOTE
, "kernel debugger active; anonymous "
14486 "enabling ignored.");
14487 dtrace_dof_destroy(dof
);
14492 * If we haven't allocated an anonymous state, we'll do so now.
14494 if ((state
= dtrace_anon
.dta_state
) == NULL
) {
14495 #if !defined(__APPLE__)
14496 state
= dtrace_state_create(NULL
, NULL
);
14497 dtrace_anon
.dta_state
= state
;
14498 if (state
== NULL
) {
14500 rv
= dtrace_state_create(NULL
, NULL
, &state
);
14501 dtrace_anon
.dta_state
= state
;
14502 if (rv
!= 0 || state
== NULL
) {
14503 #endif /* __APPLE__ */
14505 * This basically shouldn't happen: the only
14506 * failure mode from dtrace_state_create() is a
14507 * failure of ddi_soft_state_zalloc() that
14508 * itself should never happen. Still, the
14509 * interface allows for a failure mode, and
14510 * we want to fail as gracefully as possible:
14511 * we'll emit an error message and cease
14512 * processing anonymous state in this case.
14514 cmn_err(CE_WARN
, "failed to create "
14515 "anonymous state");
14516 dtrace_dof_destroy(dof
);
14521 rv
= dtrace_dof_slurp(dof
, &state
->dts_vstate
, CRED(),
14522 &dtrace_anon
.dta_enabling
, 0, B_TRUE
);
14525 rv
= dtrace_dof_options(dof
, state
);
14527 dtrace_err_verbose
= 0;
14528 dtrace_dof_destroy(dof
);
14532 * This is malformed DOF; chuck any anonymous state
14535 ASSERT(dtrace_anon
.dta_enabling
== NULL
);
14536 dtrace_state_destroy(state
);
14537 dtrace_anon
.dta_state
= NULL
;
14541 ASSERT(dtrace_anon
.dta_enabling
!= NULL
);
14544 if (dtrace_anon
.dta_enabling
!= NULL
) {
14548 * dtrace_enabling_retain() can only fail because we are
14549 * trying to retain more enablings than are allowed -- but
14550 * we only have one anonymous enabling, and we are guaranteed
14551 * to be allowed at least one retained enabling; we assert
14552 * that dtrace_enabling_retain() returns success.
14554 rval
= dtrace_enabling_retain(dtrace_anon
.dta_enabling
);
14557 dtrace_enabling_dump(dtrace_anon
.dta_enabling
);
14562 * DTrace Helper Functions
14565 dtrace_helper_trace(dtrace_helper_action_t
*helper
,
14566 dtrace_mstate_t
*mstate
, dtrace_vstate_t
*vstate
, int where
)
14568 #if !defined(__APPLE__) /* Quiet compiler warning */
14569 uint32_t size
, next
, nnext
, i
;
14571 uint32_t size
, next
, nnext
;
14573 #endif /* __APPLE__ */
14574 dtrace_helptrace_t
*ent
;
14575 uint16_t flags
= cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
14577 if (!dtrace_helptrace_enabled
)
14580 #if !defined(__APPLE__) /* Quiet compiler warning */
14581 ASSERT(vstate
->dtvs_nlocals
<= dtrace_helptrace_nlocals
);
14583 ASSERT((uint32_t)vstate
->dtvs_nlocals
<= dtrace_helptrace_nlocals
);
14584 #endif /* __APPLE__ */
14587 * What would a tracing framework be without its own tracing
14588 * framework? (Well, a hell of a lot simpler, for starters...)
14590 size
= sizeof (dtrace_helptrace_t
) + dtrace_helptrace_nlocals
*
14591 sizeof (uint64_t) - sizeof (uint64_t);
14594 * Iterate until we can allocate a slot in the trace buffer.
14597 next
= dtrace_helptrace_next
;
14599 if (next
+ size
< dtrace_helptrace_bufsize
) {
14600 nnext
= next
+ size
;
14604 } while (dtrace_cas32(&dtrace_helptrace_next
, next
, nnext
) != next
);
14607 * We have our slot; fill it in.
14612 ent
= (dtrace_helptrace_t
*)&dtrace_helptrace_buffer
[next
];
14613 ent
->dtht_helper
= helper
;
14614 ent
->dtht_where
= where
;
14615 ent
->dtht_nlocals
= vstate
->dtvs_nlocals
;
14617 ent
->dtht_fltoffs
= (mstate
->dtms_present
& DTRACE_MSTATE_FLTOFFS
) ?
14618 mstate
->dtms_fltoffs
: -1;
14619 ent
->dtht_fault
= DTRACE_FLAGS2FLT(flags
);
14620 ent
->dtht_illval
= cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
;
14622 for (i
= 0; i
< vstate
->dtvs_nlocals
; i
++) {
14623 dtrace_statvar_t
*svar
;
14625 if ((svar
= vstate
->dtvs_locals
[i
]) == NULL
)
14628 ASSERT(svar
->dtsv_size
>= (int)NCPU
* sizeof (uint64_t));
14629 ent
->dtht_locals
[i
] =
14630 ((uint64_t *)(uintptr_t)svar
->dtsv_data
)[CPU
->cpu_id
];
14635 dtrace_helper(int which
, dtrace_mstate_t
*mstate
,
14636 dtrace_state_t
*state
, uint64_t arg0
, uint64_t arg1
)
14638 uint16_t *flags
= &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
14639 uint64_t sarg0
= mstate
->dtms_arg
[0];
14640 uint64_t sarg1
= mstate
->dtms_arg
[1];
14642 dtrace_helpers_t
*helpers
= curproc
->p_dtrace_helpers
;
14643 dtrace_helper_action_t
*helper
;
14644 dtrace_vstate_t
*vstate
;
14645 dtrace_difo_t
*pred
;
14646 int i
, trace
= dtrace_helptrace_enabled
;
14648 ASSERT(which
>= 0 && which
< DTRACE_NHELPER_ACTIONS
);
14650 if (helpers
== NULL
)
14653 if ((helper
= helpers
->dthps_actions
[which
]) == NULL
)
14656 vstate
= &helpers
->dthps_vstate
;
14657 mstate
->dtms_arg
[0] = arg0
;
14658 mstate
->dtms_arg
[1] = arg1
;
14661 * Now iterate over each helper. If its predicate evaluates to 'true',
14662 * we'll call the corresponding actions. Note that the below calls
14663 * to dtrace_dif_emulate() may set faults in machine state. This is
14664 * okay: our caller (the outer dtrace_dif_emulate()) will simply plow
14665 * the stored DIF offset with its own (which is the desired behavior).
14666 * Also, note the calls to dtrace_dif_emulate() may allocate scratch
14667 * from machine state; this is okay, too.
14669 for (; helper
!= NULL
; helper
= helper
->dtha_next
) {
14670 if ((pred
= helper
->dtha_predicate
) != NULL
) {
14672 dtrace_helper_trace(helper
, mstate
, vstate
, 0);
14674 if (!dtrace_dif_emulate(pred
, mstate
, vstate
, state
))
14677 if (*flags
& CPU_DTRACE_FAULT
)
14681 for (i
= 0; i
< helper
->dtha_nactions
; i
++) {
14683 dtrace_helper_trace(helper
,
14684 mstate
, vstate
, i
+ 1);
14686 rval
= dtrace_dif_emulate(helper
->dtha_actions
[i
],
14687 mstate
, vstate
, state
);
14689 if (*flags
& CPU_DTRACE_FAULT
)
14695 dtrace_helper_trace(helper
, mstate
, vstate
,
14696 DTRACE_HELPTRACE_NEXT
);
14700 dtrace_helper_trace(helper
, mstate
, vstate
,
14701 DTRACE_HELPTRACE_DONE
);
14704 * Restore the arg0 that we saved upon entry.
14706 mstate
->dtms_arg
[0] = sarg0
;
14707 mstate
->dtms_arg
[1] = sarg1
;
14713 dtrace_helper_trace(helper
, mstate
, vstate
,
14714 DTRACE_HELPTRACE_ERR
);
14717 * Restore the arg0 that we saved upon entry.
14719 mstate
->dtms_arg
[0] = sarg0
;
14720 mstate
->dtms_arg
[1] = sarg1
;
14726 dtrace_helper_action_destroy(dtrace_helper_action_t
*helper
,
14727 dtrace_vstate_t
*vstate
)
14731 if (helper
->dtha_predicate
!= NULL
)
14732 dtrace_difo_release(helper
->dtha_predicate
, vstate
);
14734 for (i
= 0; i
< helper
->dtha_nactions
; i
++) {
14735 ASSERT(helper
->dtha_actions
[i
] != NULL
);
14736 dtrace_difo_release(helper
->dtha_actions
[i
], vstate
);
14739 kmem_free(helper
->dtha_actions
,
14740 helper
->dtha_nactions
* sizeof (dtrace_difo_t
*));
14741 kmem_free(helper
, sizeof (dtrace_helper_action_t
));
14744 #if !defined(__APPLE__)
14746 dtrace_helper_destroygen(int gen
)
14748 proc_t
*p
= curproc
;
14751 dtrace_helper_destroygen(proc_t
* p
, int gen
)
14754 dtrace_helpers_t
*help
= p
->p_dtrace_helpers
;
14755 dtrace_vstate_t
*vstate
;
14756 #if !defined(__APPLE__) /* Quiet compiler warning */
14760 #endif /* __APPLE__ */
14762 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
14764 if (help
== NULL
|| gen
> help
->dthps_generation
)
14767 vstate
= &help
->dthps_vstate
;
14769 for (i
= 0; i
< DTRACE_NHELPER_ACTIONS
; i
++) {
14770 dtrace_helper_action_t
*last
= NULL
, *h
, *next
;
14772 for (h
= help
->dthps_actions
[i
]; h
!= NULL
; h
= next
) {
14773 next
= h
->dtha_next
;
14775 if (h
->dtha_generation
== gen
) {
14776 if (last
!= NULL
) {
14777 last
->dtha_next
= next
;
14779 help
->dthps_actions
[i
] = next
;
14782 dtrace_helper_action_destroy(h
, vstate
);
14790 * Interate until we've cleared out all helper providers with the
14791 * given generation number.
14794 dtrace_helper_provider_t
*prov
= NULL
;
14797 * Look for a helper provider with the right generation. We
14798 * have to start back at the beginning of the list each time
14799 * because we drop dtrace_lock. It's unlikely that we'll make
14800 * more than two passes.
14802 for (i
= 0; i
< help
->dthps_nprovs
; i
++) {
14803 prov
= help
->dthps_provs
[i
];
14805 if (prov
->dthp_generation
== gen
)
14810 * If there were no matches, we're done.
14812 if (i
== help
->dthps_nprovs
)
14816 * Move the last helper provider into this slot.
14818 help
->dthps_nprovs
--;
14819 help
->dthps_provs
[i
] = help
->dthps_provs
[help
->dthps_nprovs
];
14820 help
->dthps_provs
[help
->dthps_nprovs
] = NULL
;
14822 lck_mtx_unlock(&dtrace_lock
);
14825 * If we have a meta provider, remove this helper provider.
14827 lck_mtx_lock(&dtrace_meta_lock
);
14828 if (dtrace_meta_pid
!= NULL
) {
14829 ASSERT(dtrace_deferred_pid
== NULL
);
14830 dtrace_helper_provider_remove(&prov
->dthp_prov
,
14833 lck_mtx_unlock(&dtrace_meta_lock
);
14835 dtrace_helper_provider_destroy(prov
);
14837 lck_mtx_lock(&dtrace_lock
);
14844 dtrace_helper_validate(dtrace_helper_action_t
*helper
)
14849 if ((dp
= helper
->dtha_predicate
) != NULL
)
14850 err
+= dtrace_difo_validate_helper(dp
);
14852 for (i
= 0; i
< helper
->dtha_nactions
; i
++)
14853 err
+= dtrace_difo_validate_helper(helper
->dtha_actions
[i
]);
14858 #if !defined(__APPLE__)
14860 dtrace_helper_action_add(int which
, dtrace_ecbdesc_t
*ep
)
14863 dtrace_helper_action_add(proc_t
* p
, int which
, dtrace_ecbdesc_t
*ep
)
14866 dtrace_helpers_t
*help
;
14867 dtrace_helper_action_t
*helper
, *last
;
14868 dtrace_actdesc_t
*act
;
14869 dtrace_vstate_t
*vstate
;
14870 dtrace_predicate_t
*pred
;
14871 int count
= 0, nactions
= 0, i
;
14873 if (which
< 0 || which
>= DTRACE_NHELPER_ACTIONS
)
14876 #if !defined(__APPLE__)
14877 help
= curproc
->p_dtrace_helpers
;
14879 help
= p
->p_dtrace_helpers
;
14881 last
= help
->dthps_actions
[which
];
14882 vstate
= &help
->dthps_vstate
;
14884 for (count
= 0; last
!= NULL
; last
= last
->dtha_next
) {
14886 if (last
->dtha_next
== NULL
)
14891 * If we already have dtrace_helper_actions_max helper actions for this
14892 * helper action type, we'll refuse to add a new one.
14894 if (count
>= dtrace_helper_actions_max
)
14897 helper
= kmem_zalloc(sizeof (dtrace_helper_action_t
), KM_SLEEP
);
14898 helper
->dtha_generation
= help
->dthps_generation
;
14900 if ((pred
= ep
->dted_pred
.dtpdd_predicate
) != NULL
) {
14901 ASSERT(pred
->dtp_difo
!= NULL
);
14902 dtrace_difo_hold(pred
->dtp_difo
);
14903 helper
->dtha_predicate
= pred
->dtp_difo
;
14906 for (act
= ep
->dted_action
; act
!= NULL
; act
= act
->dtad_next
) {
14907 if (act
->dtad_kind
!= DTRACEACT_DIFEXPR
)
14910 if (act
->dtad_difo
== NULL
)
14916 helper
->dtha_actions
= kmem_zalloc(sizeof (dtrace_difo_t
*) *
14917 (helper
->dtha_nactions
= nactions
), KM_SLEEP
);
14919 for (act
= ep
->dted_action
, i
= 0; act
!= NULL
; act
= act
->dtad_next
) {
14920 dtrace_difo_hold(act
->dtad_difo
);
14921 helper
->dtha_actions
[i
++] = act
->dtad_difo
;
14924 if (!dtrace_helper_validate(helper
))
14927 if (last
== NULL
) {
14928 help
->dthps_actions
[which
] = helper
;
14930 last
->dtha_next
= helper
;
14933 #if !defined(__APPLE__) /* Quiet compiler warning */
14934 if (vstate
->dtvs_nlocals
> dtrace_helptrace_nlocals
) {
14936 if ((uint32_t)vstate
->dtvs_nlocals
> dtrace_helptrace_nlocals
) {
14937 #endif /* __APPLE__ */
14938 dtrace_helptrace_nlocals
= vstate
->dtvs_nlocals
;
14939 dtrace_helptrace_next
= 0;
14944 dtrace_helper_action_destroy(helper
, vstate
);
14949 dtrace_helper_provider_register(proc_t
*p
, dtrace_helpers_t
*help
,
14950 dof_helper_t
*dofhp
)
14952 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_NOTOWNED
);
14954 lck_mtx_lock(&dtrace_meta_lock
);
14955 lck_mtx_lock(&dtrace_lock
);
14957 if (!dtrace_attached() || dtrace_meta_pid
== NULL
) {
14959 * If the dtrace module is loaded but not attached, or if
14960 * there aren't isn't a meta provider registered to deal with
14961 * these provider descriptions, we need to postpone creating
14962 * the actual providers until later.
14965 if (help
->dthps_next
== NULL
&& help
->dthps_prev
== NULL
&&
14966 dtrace_deferred_pid
!= help
) {
14967 help
->dthps_deferred
= 1;
14968 help
->dthps_pid
= p
->p_pid
;
14969 help
->dthps_next
= dtrace_deferred_pid
;
14970 help
->dthps_prev
= NULL
;
14971 if (dtrace_deferred_pid
!= NULL
)
14972 dtrace_deferred_pid
->dthps_prev
= help
;
14973 dtrace_deferred_pid
= help
;
14976 lck_mtx_unlock(&dtrace_lock
);
14978 } else if (dofhp
!= NULL
) {
14980 * If the dtrace module is loaded and we have a particular
14981 * helper provider description, pass that off to the
14985 lck_mtx_unlock(&dtrace_lock
);
14987 dtrace_helper_provide(dofhp
, p
->p_pid
);
14991 * Otherwise, just pass all the helper provider descriptions
14992 * off to the meta provider.
14995 #if !defined(__APPLE__) /* Quiet compiler warning */
14999 #endif /* __APPLE__ */
15000 lck_mtx_unlock(&dtrace_lock
);
15002 for (i
= 0; i
< help
->dthps_nprovs
; i
++) {
15003 dtrace_helper_provide(&help
->dthps_provs
[i
]->dthp_prov
,
15008 lck_mtx_unlock(&dtrace_meta_lock
);
15011 #if !defined(__APPLE__)
15013 dtrace_helper_provider_add(dof_helper_t
*dofhp
, int gen
)
15016 dtrace_helper_provider_add(proc_t
* p
, dof_helper_t
*dofhp
, int gen
)
15019 dtrace_helpers_t
*help
;
15020 dtrace_helper_provider_t
*hprov
, **tmp_provs
;
15021 uint_t tmp_maxprovs
, i
;
15023 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
15025 #if !defined(__APPLE__)
15026 help
= curproc
->p_dtrace_helpers
;
15028 help
= p
->p_dtrace_helpers
;
15030 ASSERT(help
!= NULL
);
15033 * If we already have dtrace_helper_providers_max helper providers,
15034 * we're refuse to add a new one.
15036 if (help
->dthps_nprovs
>= dtrace_helper_providers_max
)
15040 * Check to make sure this isn't a duplicate.
15042 for (i
= 0; i
< help
->dthps_nprovs
; i
++) {
15043 if (dofhp
->dofhp_addr
==
15044 help
->dthps_provs
[i
]->dthp_prov
.dofhp_addr
)
15048 hprov
= kmem_zalloc(sizeof (dtrace_helper_provider_t
), KM_SLEEP
);
15049 hprov
->dthp_prov
= *dofhp
;
15050 hprov
->dthp_ref
= 1;
15051 hprov
->dthp_generation
= gen
;
15054 * Allocate a bigger table for helper providers if it's already full.
15056 if (help
->dthps_maxprovs
== help
->dthps_nprovs
) {
15057 tmp_maxprovs
= help
->dthps_maxprovs
;
15058 tmp_provs
= help
->dthps_provs
;
15060 if (help
->dthps_maxprovs
== 0)
15061 help
->dthps_maxprovs
= 2;
15063 help
->dthps_maxprovs
*= 2;
15064 if (help
->dthps_maxprovs
> dtrace_helper_providers_max
)
15065 help
->dthps_maxprovs
= dtrace_helper_providers_max
;
15067 ASSERT(tmp_maxprovs
< help
->dthps_maxprovs
);
15069 help
->dthps_provs
= kmem_zalloc(help
->dthps_maxprovs
*
15070 sizeof (dtrace_helper_provider_t
*), KM_SLEEP
);
15072 if (tmp_provs
!= NULL
) {
15073 bcopy(tmp_provs
, help
->dthps_provs
, tmp_maxprovs
*
15074 sizeof (dtrace_helper_provider_t
*));
15075 kmem_free(tmp_provs
, tmp_maxprovs
*
15076 sizeof (dtrace_helper_provider_t
*));
15080 help
->dthps_provs
[help
->dthps_nprovs
] = hprov
;
15081 help
->dthps_nprovs
++;
15087 dtrace_helper_provider_destroy(dtrace_helper_provider_t
*hprov
)
15089 lck_mtx_lock(&dtrace_lock
);
15091 if (--hprov
->dthp_ref
== 0) {
15093 lck_mtx_unlock(&dtrace_lock
);
15094 dof
= (dof_hdr_t
*)(uintptr_t)hprov
->dthp_prov
.dofhp_dof
;
15095 dtrace_dof_destroy(dof
);
15096 kmem_free(hprov
, sizeof (dtrace_helper_provider_t
));
15098 lck_mtx_unlock(&dtrace_lock
);
15103 dtrace_helper_provider_validate(dof_hdr_t
*dof
, dof_sec_t
*sec
)
15105 uintptr_t daddr
= (uintptr_t)dof
;
15106 dof_sec_t
*str_sec
, *prb_sec
, *arg_sec
, *off_sec
, *enoff_sec
;
15107 dof_provider_t
*provider
;
15108 dof_probe_t
*probe
;
15110 char *strtab
, *typestr
;
15111 dof_stridx_t typeidx
;
15113 uint_t nprobes
, j
, k
;
15115 ASSERT(sec
->dofs_type
== DOF_SECT_PROVIDER
);
15117 if (sec
->dofs_offset
& (sizeof (uint_t
) - 1)) {
15118 dtrace_dof_error(dof
, "misaligned section offset");
15123 * The section needs to be large enough to contain the DOF provider
15124 * structure appropriate for the given version.
15126 if (sec
->dofs_size
<
15127 ((dof
->dofh_ident
[DOF_ID_VERSION
] == DOF_VERSION_1
) ?
15128 offsetof(dof_provider_t
, dofpv_prenoffs
) :
15129 sizeof (dof_provider_t
))) {
15130 dtrace_dof_error(dof
, "provider section too small");
15134 provider
= (dof_provider_t
*)(uintptr_t)(daddr
+ sec
->dofs_offset
);
15135 str_sec
= dtrace_dof_sect(dof
, DOF_SECT_STRTAB
, provider
->dofpv_strtab
);
15136 prb_sec
= dtrace_dof_sect(dof
, DOF_SECT_PROBES
, provider
->dofpv_probes
);
15137 arg_sec
= dtrace_dof_sect(dof
, DOF_SECT_PRARGS
, provider
->dofpv_prargs
);
15138 off_sec
= dtrace_dof_sect(dof
, DOF_SECT_PROFFS
, provider
->dofpv_proffs
);
15140 if (str_sec
== NULL
|| prb_sec
== NULL
||
15141 arg_sec
== NULL
|| off_sec
== NULL
)
15146 if (dof
->dofh_ident
[DOF_ID_VERSION
] != DOF_VERSION_1
&&
15147 provider
->dofpv_prenoffs
!= DOF_SECT_NONE
&&
15148 (enoff_sec
= dtrace_dof_sect(dof
, DOF_SECT_PRENOFFS
,
15149 provider
->dofpv_prenoffs
)) == NULL
)
15152 strtab
= (char *)(uintptr_t)(daddr
+ str_sec
->dofs_offset
);
15154 if (provider
->dofpv_name
>= str_sec
->dofs_size
||
15155 strlen(strtab
+ provider
->dofpv_name
) >= DTRACE_PROVNAMELEN
) {
15156 dtrace_dof_error(dof
, "invalid provider name");
15160 if (prb_sec
->dofs_entsize
== 0 ||
15161 prb_sec
->dofs_entsize
> prb_sec
->dofs_size
) {
15162 dtrace_dof_error(dof
, "invalid entry size");
15166 if (prb_sec
->dofs_entsize
& (sizeof (uintptr_t) - 1)) {
15167 dtrace_dof_error(dof
, "misaligned entry size");
15171 if (off_sec
->dofs_entsize
!= sizeof (uint32_t)) {
15172 dtrace_dof_error(dof
, "invalid entry size");
15176 if (off_sec
->dofs_offset
& (sizeof (uint32_t) - 1)) {
15177 dtrace_dof_error(dof
, "misaligned section offset");
15181 if (arg_sec
->dofs_entsize
!= sizeof (uint8_t)) {
15182 dtrace_dof_error(dof
, "invalid entry size");
15186 arg
= (uint8_t *)(uintptr_t)(daddr
+ arg_sec
->dofs_offset
);
15188 nprobes
= prb_sec
->dofs_size
/ prb_sec
->dofs_entsize
;
15191 * Take a pass through the probes to check for errors.
15193 for (j
= 0; j
< nprobes
; j
++) {
15194 probe
= (dof_probe_t
*)(uintptr_t)(daddr
+
15195 prb_sec
->dofs_offset
+ j
* prb_sec
->dofs_entsize
);
15197 if (probe
->dofpr_func
>= str_sec
->dofs_size
) {
15198 dtrace_dof_error(dof
, "invalid function name");
15202 if (strlen(strtab
+ probe
->dofpr_func
) >= DTRACE_FUNCNAMELEN
) {
15203 dtrace_dof_error(dof
, "function name too long");
15207 if (probe
->dofpr_name
>= str_sec
->dofs_size
||
15208 strlen(strtab
+ probe
->dofpr_name
) >= DTRACE_NAMELEN
) {
15209 dtrace_dof_error(dof
, "invalid probe name");
15214 * The offset count must not wrap the index, and the offsets
15215 * must also not overflow the section's data.
15217 if (probe
->dofpr_offidx
+ probe
->dofpr_noffs
<
15218 probe
->dofpr_offidx
||
15219 (probe
->dofpr_offidx
+ probe
->dofpr_noffs
) *
15220 off_sec
->dofs_entsize
> off_sec
->dofs_size
) {
15221 dtrace_dof_error(dof
, "invalid probe offset");
15225 if (dof
->dofh_ident
[DOF_ID_VERSION
] != DOF_VERSION_1
) {
15227 * If there's no is-enabled offset section, make sure
15228 * there aren't any is-enabled offsets. Otherwise
15229 * perform the same checks as for probe offsets
15230 * (immediately above).
15232 if (enoff_sec
== NULL
) {
15233 if (probe
->dofpr_enoffidx
!= 0 ||
15234 probe
->dofpr_nenoffs
!= 0) {
15235 dtrace_dof_error(dof
, "is-enabled "
15236 "offsets with null section");
15239 } else if (probe
->dofpr_enoffidx
+
15240 probe
->dofpr_nenoffs
< probe
->dofpr_enoffidx
||
15241 (probe
->dofpr_enoffidx
+ probe
->dofpr_nenoffs
) *
15242 enoff_sec
->dofs_entsize
> enoff_sec
->dofs_size
) {
15243 dtrace_dof_error(dof
, "invalid is-enabled "
15248 if (probe
->dofpr_noffs
+ probe
->dofpr_nenoffs
== 0) {
15249 dtrace_dof_error(dof
, "zero probe and "
15250 "is-enabled offsets");
15253 } else if (probe
->dofpr_noffs
== 0) {
15254 dtrace_dof_error(dof
, "zero probe offsets");
15258 if (probe
->dofpr_argidx
+ probe
->dofpr_xargc
<
15259 probe
->dofpr_argidx
||
15260 (probe
->dofpr_argidx
+ probe
->dofpr_xargc
) *
15261 arg_sec
->dofs_entsize
> arg_sec
->dofs_size
) {
15262 dtrace_dof_error(dof
, "invalid args");
15266 typeidx
= probe
->dofpr_nargv
;
15267 typestr
= strtab
+ probe
->dofpr_nargv
;
15268 for (k
= 0; k
< probe
->dofpr_nargc
; k
++) {
15269 if (typeidx
>= str_sec
->dofs_size
) {
15270 dtrace_dof_error(dof
, "bad "
15271 "native argument type");
15275 typesz
= strlen(typestr
) + 1;
15276 if (typesz
> DTRACE_ARGTYPELEN
) {
15277 dtrace_dof_error(dof
, "native "
15278 "argument type too long");
15285 typeidx
= probe
->dofpr_xargv
;
15286 typestr
= strtab
+ probe
->dofpr_xargv
;
15287 for (k
= 0; k
< probe
->dofpr_xargc
; k
++) {
15288 if (arg
[probe
->dofpr_argidx
+ k
] > probe
->dofpr_nargc
) {
15289 dtrace_dof_error(dof
, "bad "
15290 "native argument index");
15294 if (typeidx
>= str_sec
->dofs_size
) {
15295 dtrace_dof_error(dof
, "bad "
15296 "translated argument type");
15300 typesz
= strlen(typestr
) + 1;
15301 if (typesz
> DTRACE_ARGTYPELEN
) {
15302 dtrace_dof_error(dof
, "translated argument "
15315 #if !defined(__APPLE__)
15317 dtrace_helper_slurp(dof_hdr_t
*dof
, dof_helper_t
*dhp
)
15320 dtrace_helper_slurp(proc_t
* p
, dof_hdr_t
*dof
, dof_helper_t
*dhp
)
15323 dtrace_helpers_t
*help
;
15324 dtrace_vstate_t
*vstate
;
15325 dtrace_enabling_t
*enab
= NULL
;
15326 int i
, gen
, rv
, nhelpers
= 0, nprovs
= 0, destroy
= 1;
15327 uintptr_t daddr
= (uintptr_t)dof
;
15329 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
15331 #if !defined(__APPLE__)
15332 if ((help
= curproc
->p_dtrace_helpers
) == NULL
)
15333 help
= dtrace_helpers_create(curproc
);
15335 if ((help
= p
->p_dtrace_helpers
) == NULL
)
15336 help
= dtrace_helpers_create(p
);
15339 vstate
= &help
->dthps_vstate
;
15341 if ((rv
= dtrace_dof_slurp(dof
, vstate
, NULL
, &enab
,
15342 dhp
!= NULL
? dhp
->dofhp_addr
: 0, B_FALSE
)) != 0) {
15343 dtrace_dof_destroy(dof
);
15348 * Look for helper providers and validate their descriptions.
15351 #if !defined(__APPLE__) /* Quiet compiler warning */
15352 for (i
= 0; i
< dof
->dofh_secnum
; i
++) {
15354 for (i
= 0; (uint32_t)i
< dof
->dofh_secnum
; i
++) {
15355 #endif /* __APPLE__ */
15356 dof_sec_t
*sec
= (dof_sec_t
*)(uintptr_t)(daddr
+
15357 dof
->dofh_secoff
+ i
* dof
->dofh_secsize
);
15359 if (sec
->dofs_type
!= DOF_SECT_PROVIDER
)
15362 if (dtrace_helper_provider_validate(dof
, sec
) != 0) {
15363 dtrace_enabling_destroy(enab
);
15364 dtrace_dof_destroy(dof
);
15373 * Now we need to walk through the ECB descriptions in the enabling.
15375 for (i
= 0; i
< enab
->dten_ndesc
; i
++) {
15376 dtrace_ecbdesc_t
*ep
= enab
->dten_desc
[i
];
15377 dtrace_probedesc_t
*desc
= &ep
->dted_probe
;
15379 #if !defined(__APPLE__)
15380 if (strcmp(desc
->dtpd_provider
, "dtrace") != 0)
15383 if (strcmp(desc
->dtpd_mod
, "helper") != 0)
15386 if (strcmp(desc
->dtpd_func
, "ustack") != 0)
15388 #else /* Employ size bounded string operation. */
15389 if (!LIT_STRNEQL(desc
->dtpd_provider
, "dtrace"))
15392 if (!LIT_STRNEQL(desc
->dtpd_mod
, "helper"))
15395 if (!LIT_STRNEQL(desc
->dtpd_func
, "ustack"))
15397 #endif /* __APPLE__ */
15399 #if !defined(__APPLE__)
15400 if ((rv
= dtrace_helper_action_add(DTRACE_HELPER_ACTION_USTACK
,
15403 if ((rv
= dtrace_helper_action_add(p
, DTRACE_HELPER_ACTION_USTACK
,
15407 * Adding this helper action failed -- we are now going
15408 * to rip out the entire generation and return failure.
15410 #if !defined(__APPLE__)
15411 (void) dtrace_helper_destroygen(help
->dthps_generation
);
15413 (void) dtrace_helper_destroygen(p
, help
->dthps_generation
);
15415 dtrace_enabling_destroy(enab
);
15416 dtrace_dof_destroy(dof
);
15423 if (nhelpers
< enab
->dten_ndesc
)
15424 dtrace_dof_error(dof
, "unmatched helpers");
15426 gen
= help
->dthps_generation
++;
15427 dtrace_enabling_destroy(enab
);
15429 if (dhp
!= NULL
&& nprovs
> 0) {
15430 dhp
->dofhp_dof
= (uint64_t)(uintptr_t)dof
;
15431 #if !defined(__APPLE__)
15432 if (dtrace_helper_provider_add(dhp
, gen
) == 0) {
15434 if (dtrace_helper_provider_add(p
, dhp
, gen
) == 0) {
15436 lck_mtx_unlock(&dtrace_lock
);
15437 #if !defined(__APPLE__)
15438 dtrace_helper_provider_register(curproc
, help
, dhp
);
15440 dtrace_helper_provider_register(p
, help
, dhp
);
15442 lck_mtx_lock(&dtrace_lock
);
15449 dtrace_dof_destroy(dof
);
15454 #if defined(__APPLE__)
15459 * DTrace user static probes (USDT probes) and helper actions are loaded
15460 * in a process by proccessing dof sections. The dof sections are passed
15461 * into the kernel by dyld, in a dof_ioctl_data_t block. It is rather
15462 * expensive to process dof for a process that will never use it. There
15463 * is a memory cost (allocating the providers/probes), and a cpu cost
15464 * (creating the providers/probes).
15466 * To reduce this cost, we use "lazy dof". The normal proceedure for
15467 * dof processing is to copyin the dof(s) pointed to by the dof_ioctl_data_t
15468 * block, and invoke dof_slurp_helper() on them. When "lazy dof" is
15469 * used, each process retains the dof_ioctl_data_t block, instead of
15470 * copying in the data it points to.
15472 * The dof_ioctl_data_t blocks are managed as if they were the actual
15473 * processed dof; on fork the block is copied to the child, on exec and
15474 * exit the block is freed.
15476 * If the process loads library(s) containing additional dof, the
15477 * new dof_ioctl_data_t is merged with the existing block.
15479 * There are a few catches that make this slightly more difficult.
15480 * When dyld registers dof_ioctl_data_t blocks, it expects a unique
15481 * identifier value for each dof in the block. In non-lazy dof terms,
15482 * this is the generation that dof was loaded in. If we hand back
15483 * a UID for a lazy dof, that same UID must be able to unload the
15484 * dof once it has become non-lazy. To meet this requirement, the
15485 * code that loads lazy dof requires that the UID's for dof(s) in
15486 * the lazy dof be sorted, and in ascending order. It is okay to skip
15487 * UID's, I.E., 1 -> 5 -> 6 is legal.
15489 * Once a process has become non-lazy, it will stay non-lazy. All
15490 * future dof operations for that process will be non-lazy, even
15491 * if the dof mode transitions back to lazy.
15493 * Always do lazy dof checks before non-lazy (I.E. In fork, exit, exec.).
15494 * That way if the lazy check fails due to transitioning to non-lazy, the
15495 * right thing is done with the newly faulted in dof.
15499 * This method is a bit squicky. It must handle:
15501 * dof should not be lazy.
15502 * dof should have been handled lazily, but there was an error
15503 * dof was handled lazily, and needs to be freed.
15504 * dof was handled lazily, and must not be freed.
15507 * Returns EACCESS if dof should be handled non-lazily.
15509 * KERN_SUCCESS and all other return codes indicate lazy handling of dof.
15511 * If the dofs data is claimed by this method, dofs_claimed will be set.
15512 * Callers should not free claimed dofs.
15515 dtrace_lazy_dofs_add(proc_t
*p
, dof_ioctl_data_t
* incoming_dofs
, int *dofs_claimed
)
15518 ASSERT(incoming_dofs
&& incoming_dofs
->dofiod_count
> 0);
15523 lck_rw_lock_shared(&dtrace_dof_mode_lock
);
15526 * If we have lazy dof, dof mode better be LAZY_ON.
15528 ASSERT(p
->p_dtrace_lazy_dofs
== NULL
|| dtrace_dof_mode
== DTRACE_DOF_MODE_LAZY_ON
);
15529 ASSERT(p
->p_dtrace_lazy_dofs
== NULL
|| p
->p_dtrace_helpers
== NULL
);
15530 ASSERT(dtrace_dof_mode
!= DTRACE_DOF_MODE_NEVER
);
15533 * Any existing helpers force non-lazy behavior.
15535 if (dtrace_dof_mode
== DTRACE_DOF_MODE_LAZY_ON
&& (p
->p_dtrace_helpers
== NULL
)) {
15536 lck_mtx_lock(&p
->p_dtrace_sprlock
);
15538 dof_ioctl_data_t
* existing_dofs
= p
->p_dtrace_lazy_dofs
;
15539 unsigned int existing_dofs_count
= (existing_dofs
) ? existing_dofs
->dofiod_count
: 0;
15540 unsigned int i
, merged_dofs_count
= incoming_dofs
->dofiod_count
+ existing_dofs_count
;
15545 if (merged_dofs_count
== 0 || merged_dofs_count
> 1024) {
15546 dtrace_dof_error(NULL
, "lazy_dofs_add merged_dofs_count out of range");
15552 * Each dof being added must be assigned a unique generation.
15554 uint64_t generation
= (existing_dofs
) ? existing_dofs
->dofiod_helpers
[existing_dofs_count
- 1].dofhp_dof
+ 1 : 1;
15555 for (i
=0; i
<incoming_dofs
->dofiod_count
; i
++) {
15557 * We rely on these being the same so we can overwrite dofhp_dof and not lose info.
15559 ASSERT(incoming_dofs
->dofiod_helpers
[i
].dofhp_dof
== incoming_dofs
->dofiod_helpers
[i
].dofhp_addr
);
15560 incoming_dofs
->dofiod_helpers
[i
].dofhp_dof
= generation
++;
15564 if (existing_dofs
) {
15566 * Merge the existing and incoming dofs
15568 size_t merged_dofs_size
= DOF_IOCTL_DATA_T_SIZE(merged_dofs_count
);
15569 dof_ioctl_data_t
* merged_dofs
= kmem_alloc(merged_dofs_size
, KM_SLEEP
);
15571 bcopy(&existing_dofs
->dofiod_helpers
[0],
15572 &merged_dofs
->dofiod_helpers
[0],
15573 sizeof(dof_helper_t
) * existing_dofs_count
);
15574 bcopy(&incoming_dofs
->dofiod_helpers
[0],
15575 &merged_dofs
->dofiod_helpers
[existing_dofs_count
],
15576 sizeof(dof_helper_t
) * incoming_dofs
->dofiod_count
);
15578 merged_dofs
->dofiod_count
= merged_dofs_count
;
15580 kmem_free(existing_dofs
, DOF_IOCTL_DATA_T_SIZE(existing_dofs_count
));
15582 p
->p_dtrace_lazy_dofs
= merged_dofs
;
15585 * Claim the incoming dofs
15588 p
->p_dtrace_lazy_dofs
= incoming_dofs
;
15592 dof_ioctl_data_t
* all_dofs
= p
->p_dtrace_lazy_dofs
;
15593 for (i
=0; i
<all_dofs
->dofiod_count
-1; i
++) {
15594 ASSERT(all_dofs
->dofiod_helpers
[i
].dofhp_dof
< all_dofs
->dofiod_helpers
[i
+1].dofhp_dof
);
15599 lck_mtx_unlock(&p
->p_dtrace_sprlock
);
15604 lck_rw_unlock_shared(&dtrace_dof_mode_lock
);
15612 * EINVAL: lazy dof is enabled, but the requested generation was not found.
15613 * EACCES: This removal needs to be handled non-lazily.
15616 dtrace_lazy_dofs_remove(proc_t
*p
, int generation
)
15620 lck_rw_lock_shared(&dtrace_dof_mode_lock
);
15623 * If we have lazy dof, dof mode better be LAZY_ON.
15625 ASSERT(p
->p_dtrace_lazy_dofs
== NULL
|| dtrace_dof_mode
== DTRACE_DOF_MODE_LAZY_ON
);
15626 ASSERT(p
->p_dtrace_lazy_dofs
== NULL
|| p
->p_dtrace_helpers
== NULL
);
15627 ASSERT(dtrace_dof_mode
!= DTRACE_DOF_MODE_NEVER
);
15630 * Any existing helpers force non-lazy behavior.
15632 if (dtrace_dof_mode
== DTRACE_DOF_MODE_LAZY_ON
&& (p
->p_dtrace_helpers
== NULL
)) {
15633 lck_mtx_lock(&p
->p_dtrace_sprlock
);
15635 dof_ioctl_data_t
* existing_dofs
= p
->p_dtrace_lazy_dofs
;
15637 if (existing_dofs
) {
15638 int index
, existing_dofs_count
= existing_dofs
->dofiod_count
;
15639 for (index
=0; index
<existing_dofs_count
; index
++) {
15640 if ((int)existing_dofs
->dofiod_helpers
[index
].dofhp_dof
== generation
) {
15641 dof_ioctl_data_t
* removed_dofs
= NULL
;
15644 * If there is only 1 dof, we'll delete it and swap in NULL.
15646 if (existing_dofs_count
> 1) {
15647 int removed_dofs_count
= existing_dofs_count
- 1;
15648 size_t removed_dofs_size
= DOF_IOCTL_DATA_T_SIZE(removed_dofs_count
);
15650 removed_dofs
= kmem_alloc(removed_dofs_size
, KM_SLEEP
);
15651 removed_dofs
->dofiod_count
= removed_dofs_count
;
15654 * copy the remaining data.
15657 bcopy(&existing_dofs
->dofiod_helpers
[0],
15658 &removed_dofs
->dofiod_helpers
[0],
15659 index
* sizeof(dof_helper_t
));
15662 if (index
< existing_dofs_count
-1) {
15663 bcopy(&existing_dofs
->dofiod_helpers
[index
+1],
15664 &removed_dofs
->dofiod_helpers
[index
],
15665 (existing_dofs_count
- index
- 1) * sizeof(dof_helper_t
));
15669 kmem_free(existing_dofs
, DOF_IOCTL_DATA_T_SIZE(existing_dofs_count
));
15671 p
->p_dtrace_lazy_dofs
= removed_dofs
;
15673 rval
= KERN_SUCCESS
;
15680 dof_ioctl_data_t
* all_dofs
= p
->p_dtrace_lazy_dofs
;
15683 for (i
=0; i
<all_dofs
->dofiod_count
-1; i
++) {
15684 ASSERT(all_dofs
->dofiod_helpers
[i
].dofhp_dof
< all_dofs
->dofiod_helpers
[i
+1].dofhp_dof
);
15691 lck_mtx_unlock(&p
->p_dtrace_sprlock
);
15696 lck_rw_unlock_shared(&dtrace_dof_mode_lock
);
15702 dtrace_lazy_dofs_destroy(proc_t
*p
)
15704 lck_rw_lock_shared(&dtrace_dof_mode_lock
);
15705 lck_mtx_lock(&p
->p_dtrace_sprlock
);
15708 * If we have lazy dof, dof mode better be LAZY_ON, or we must be exiting.
15709 * We cannot assert against DTRACE_DOF_MODE_NEVER here, because we are called from
15710 * kern_exit.c and kern_exec.c.
15712 ASSERT(p
->p_dtrace_lazy_dofs
== NULL
|| dtrace_dof_mode
== DTRACE_DOF_MODE_LAZY_ON
|| p
->p_lflag
& P_LEXIT
);
15713 ASSERT(p
->p_dtrace_lazy_dofs
== NULL
|| p
->p_dtrace_helpers
== NULL
);
15715 dof_ioctl_data_t
* lazy_dofs
= p
->p_dtrace_lazy_dofs
;
15716 p
->p_dtrace_lazy_dofs
= NULL
;
15718 lck_mtx_unlock(&p
->p_dtrace_sprlock
);
15719 lck_rw_unlock_shared(&dtrace_dof_mode_lock
);
15722 kmem_free(lazy_dofs
, DOF_IOCTL_DATA_T_SIZE(lazy_dofs
->dofiod_count
));
15727 dtrace_lazy_dofs_duplicate(proc_t
*parent
, proc_t
*child
)
15729 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_NOTOWNED
);
15730 lck_mtx_assert(&parent
->p_dtrace_sprlock
, LCK_MTX_ASSERT_NOTOWNED
);
15731 lck_mtx_assert(&child
->p_dtrace_sprlock
, LCK_MTX_ASSERT_NOTOWNED
);
15733 lck_rw_lock_shared(&dtrace_dof_mode_lock
);
15734 lck_mtx_lock(&parent
->p_dtrace_sprlock
);
15737 * If we have lazy dof, dof mode better be LAZY_ON, or we must be exiting.
15738 * We cannot assert against DTRACE_DOF_MODE_NEVER here, because we are called from
15741 ASSERT(parent
->p_dtrace_lazy_dofs
== NULL
|| dtrace_dof_mode
== DTRACE_DOF_MODE_LAZY_ON
);
15742 ASSERT(parent
->p_dtrace_lazy_dofs
== NULL
|| parent
->p_dtrace_helpers
== NULL
);
15744 * In theory we should hold the child sprlock, but this is safe...
15746 ASSERT(child
->p_dtrace_lazy_dofs
== NULL
&& child
->p_dtrace_helpers
== NULL
);
15748 dof_ioctl_data_t
* parent_dofs
= parent
->p_dtrace_lazy_dofs
;
15749 dof_ioctl_data_t
* child_dofs
= NULL
;
15751 size_t parent_dofs_size
= DOF_IOCTL_DATA_T_SIZE(parent_dofs
->dofiod_count
);
15752 child_dofs
= kmem_alloc(parent_dofs_size
, KM_SLEEP
);
15753 bcopy(parent_dofs
, child_dofs
, parent_dofs_size
);
15756 lck_mtx_unlock(&parent
->p_dtrace_sprlock
);
15759 lck_mtx_lock(&child
->p_dtrace_sprlock
);
15760 child
->p_dtrace_lazy_dofs
= child_dofs
;
15761 lck_mtx_unlock(&child
->p_dtrace_sprlock
);
15764 lck_rw_unlock_shared(&dtrace_dof_mode_lock
);
15768 dtrace_lazy_dofs_proc_iterate_filter(proc_t
*p
, void* ignored
)
15770 #pragma unused(ignored)
15772 * Okay to NULL test without taking the sprlock.
15774 return p
->p_dtrace_lazy_dofs
!= NULL
;
15778 dtrace_lazy_dofs_proc_iterate_doit(proc_t
*p
, void* ignored
)
15780 #pragma unused(ignored)
15782 * It is possible this process may exit during our attempt to
15783 * fault in the dof. We could fix this by holding locks longer,
15784 * but the errors are benign.
15786 lck_mtx_lock(&p
->p_dtrace_sprlock
);
15789 * In this case only, it is okay to have lazy dof when dof mode is DTRACE_DOF_MODE_LAZY_OFF
15791 ASSERT(p
->p_dtrace_lazy_dofs
== NULL
|| p
->p_dtrace_helpers
== NULL
);
15792 ASSERT(dtrace_dof_mode
== DTRACE_DOF_MODE_LAZY_OFF
);
15795 dof_ioctl_data_t
* lazy_dofs
= p
->p_dtrace_lazy_dofs
;
15796 p
->p_dtrace_lazy_dofs
= NULL
;
15798 lck_mtx_unlock(&p
->p_dtrace_sprlock
);
15801 * Process each dof_helper_t
15803 if (lazy_dofs
!= NULL
) {
15807 for (i
=0; i
<lazy_dofs
->dofiod_count
; i
++) {
15809 * When loading lazy dof, we depend on the generations being sorted in ascending order.
15811 ASSERT(i
>= (lazy_dofs
->dofiod_count
- 1) || lazy_dofs
->dofiod_helpers
[i
].dofhp_dof
< lazy_dofs
->dofiod_helpers
[i
+1].dofhp_dof
);
15813 dof_helper_t
*dhp
= &lazy_dofs
->dofiod_helpers
[i
];
15816 * We stored the generation in dofhp_dof. Save it, and restore the original value.
15818 int generation
= dhp
->dofhp_dof
;
15819 dhp
->dofhp_dof
= dhp
->dofhp_addr
;
15821 dof_hdr_t
*dof
= dtrace_dof_copyin_from_proc(p
, dhp
->dofhp_dof
, &rval
);
15824 dtrace_helpers_t
*help
;
15826 lck_mtx_lock(&dtrace_lock
);
15829 * This must be done with the dtrace_lock held
15831 if ((help
= p
->p_dtrace_helpers
) == NULL
)
15832 help
= dtrace_helpers_create(p
);
15835 * If the generation value has been bumped, someone snuck in
15836 * when we released the dtrace lock. We have to dump this generation,
15837 * there is no safe way to load it.
15839 if (help
->dthps_generation
<= generation
) {
15840 help
->dthps_generation
= generation
;
15843 * dtrace_helper_slurp() takes responsibility for the dof --
15844 * it may free it now or it may save it and free it later.
15846 if ((rval
= dtrace_helper_slurp(p
, dof
, dhp
)) != generation
) {
15847 dtrace_dof_error(NULL
, "returned value did not match expected generation");
15851 lck_mtx_unlock(&dtrace_lock
);
15855 kmem_free(lazy_dofs
, DOF_IOCTL_DATA_T_SIZE(lazy_dofs
->dofiod_count
));
15858 return PROC_RETURNED
;
15861 #endif /* __APPLE__ */
15863 static dtrace_helpers_t
*
15864 dtrace_helpers_create(proc_t
*p
)
15866 dtrace_helpers_t
*help
;
15868 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
15869 ASSERT(p
->p_dtrace_helpers
== NULL
);
15871 help
= kmem_zalloc(sizeof (dtrace_helpers_t
), KM_SLEEP
);
15872 help
->dthps_actions
= kmem_zalloc(sizeof (dtrace_helper_action_t
*) *
15873 DTRACE_NHELPER_ACTIONS
, KM_SLEEP
);
15875 p
->p_dtrace_helpers
= help
;
15881 #if !defined(__APPLE__)
15883 dtrace_helpers_destroy(void)
15885 dtrace_helpers_t
*help
;
15886 dtrace_vstate_t
*vstate
;
15887 proc_t
*p
= curproc
;
15891 dtrace_helpers_destroy(proc_t
* p
)
15893 dtrace_helpers_t
*help
;
15894 dtrace_vstate_t
*vstate
;
15898 lck_mtx_lock(&dtrace_lock
);
15900 ASSERT(p
->p_dtrace_helpers
!= NULL
);
15901 ASSERT(dtrace_helpers
> 0);
15903 help
= p
->p_dtrace_helpers
;
15904 vstate
= &help
->dthps_vstate
;
15907 * We're now going to lose the help from this process.
15909 p
->p_dtrace_helpers
= NULL
;
15913 * Destory the helper actions.
15915 for (i
= 0; i
< DTRACE_NHELPER_ACTIONS
; i
++) {
15916 dtrace_helper_action_t
*h
, *next
;
15918 for (h
= help
->dthps_actions
[i
]; h
!= NULL
; h
= next
) {
15919 next
= h
->dtha_next
;
15920 dtrace_helper_action_destroy(h
, vstate
);
15925 lck_mtx_unlock(&dtrace_lock
);
15928 * Destroy the helper providers.
15930 if (help
->dthps_maxprovs
> 0) {
15931 lck_mtx_lock(&dtrace_meta_lock
);
15932 if (dtrace_meta_pid
!= NULL
) {
15933 ASSERT(dtrace_deferred_pid
== NULL
);
15935 for (i
= 0; i
< help
->dthps_nprovs
; i
++) {
15936 dtrace_helper_provider_remove(
15937 &help
->dthps_provs
[i
]->dthp_prov
, p
->p_pid
);
15940 lck_mtx_lock(&dtrace_lock
);
15941 ASSERT(help
->dthps_deferred
== 0 ||
15942 help
->dthps_next
!= NULL
||
15943 help
->dthps_prev
!= NULL
||
15944 help
== dtrace_deferred_pid
);
15947 * Remove the helper from the deferred list.
15949 if (help
->dthps_next
!= NULL
)
15950 help
->dthps_next
->dthps_prev
= help
->dthps_prev
;
15951 if (help
->dthps_prev
!= NULL
)
15952 help
->dthps_prev
->dthps_next
= help
->dthps_next
;
15953 if (dtrace_deferred_pid
== help
) {
15954 dtrace_deferred_pid
= help
->dthps_next
;
15955 ASSERT(help
->dthps_prev
== NULL
);
15958 lck_mtx_unlock(&dtrace_lock
);
15961 lck_mtx_unlock(&dtrace_meta_lock
);
15963 for (i
= 0; i
< help
->dthps_nprovs
; i
++) {
15964 dtrace_helper_provider_destroy(help
->dthps_provs
[i
]);
15967 kmem_free(help
->dthps_provs
, help
->dthps_maxprovs
*
15968 sizeof (dtrace_helper_provider_t
*));
15971 lck_mtx_lock(&dtrace_lock
);
15973 dtrace_vstate_fini(&help
->dthps_vstate
);
15974 kmem_free(help
->dthps_actions
,
15975 sizeof (dtrace_helper_action_t
*) * DTRACE_NHELPER_ACTIONS
);
15976 kmem_free(help
, sizeof (dtrace_helpers_t
));
15979 lck_mtx_unlock(&dtrace_lock
);
15983 dtrace_helpers_duplicate(proc_t
*from
, proc_t
*to
)
15985 dtrace_helpers_t
*help
, *newhelp
;
15986 dtrace_helper_action_t
*helper
, *new, *last
;
15988 dtrace_vstate_t
*vstate
;
15989 #if !defined(__APPLE__) /* Quiet compiler warning */
15990 int i
, j
, sz
, hasprovs
= 0;
15993 int j
, sz
, hasprovs
= 0;
15994 #endif /* __APPLE__ */
15996 lck_mtx_lock(&dtrace_lock
);
15997 ASSERT(from
->p_dtrace_helpers
!= NULL
);
15998 ASSERT(dtrace_helpers
> 0);
16000 help
= from
->p_dtrace_helpers
;
16001 newhelp
= dtrace_helpers_create(to
);
16002 ASSERT(to
->p_dtrace_helpers
!= NULL
);
16004 newhelp
->dthps_generation
= help
->dthps_generation
;
16005 vstate
= &newhelp
->dthps_vstate
;
16008 * Duplicate the helper actions.
16010 for (i
= 0; i
< DTRACE_NHELPER_ACTIONS
; i
++) {
16011 if ((helper
= help
->dthps_actions
[i
]) == NULL
)
16014 for (last
= NULL
; helper
!= NULL
; helper
= helper
->dtha_next
) {
16015 new = kmem_zalloc(sizeof (dtrace_helper_action_t
),
16017 new->dtha_generation
= helper
->dtha_generation
;
16019 if ((dp
= helper
->dtha_predicate
) != NULL
) {
16020 dp
= dtrace_difo_duplicate(dp
, vstate
);
16021 new->dtha_predicate
= dp
;
16024 new->dtha_nactions
= helper
->dtha_nactions
;
16025 sz
= sizeof (dtrace_difo_t
*) * new->dtha_nactions
;
16026 new->dtha_actions
= kmem_alloc(sz
, KM_SLEEP
);
16028 #if !defined(__APPLE__) /* Quiet compiler warning */
16029 for (j
= 0; j
< new->dtha_nactions
; j
++) {
16030 dtrace_difo_t
*dp
= helper
->dtha_actions
[j
];
16032 ASSERT(dp
!= NULL
);
16033 dp
= dtrace_difo_duplicate(dp
, vstate
);
16034 new->dtha_actions
[j
] = dp
;
16037 for (j
= 0; j
< new->dtha_nactions
; j
++) {
16038 dtrace_difo_t
*dpj
= helper
->dtha_actions
[j
];
16040 ASSERT(dpj
!= NULL
);
16041 dpj
= dtrace_difo_duplicate(dpj
, vstate
);
16042 new->dtha_actions
[j
] = dpj
;
16044 #endif /* __APPLE__ */
16046 if (last
!= NULL
) {
16047 last
->dtha_next
= new;
16049 newhelp
->dthps_actions
[i
] = new;
16057 * Duplicate the helper providers and register them with the
16058 * DTrace framework.
16060 if (help
->dthps_nprovs
> 0) {
16061 newhelp
->dthps_nprovs
= help
->dthps_nprovs
;
16062 newhelp
->dthps_maxprovs
= help
->dthps_nprovs
;
16063 newhelp
->dthps_provs
= kmem_alloc(newhelp
->dthps_nprovs
*
16064 sizeof (dtrace_helper_provider_t
*), KM_SLEEP
);
16065 for (i
= 0; i
< newhelp
->dthps_nprovs
; i
++) {
16066 newhelp
->dthps_provs
[i
] = help
->dthps_provs
[i
];
16067 newhelp
->dthps_provs
[i
]->dthp_ref
++;
16073 lck_mtx_unlock(&dtrace_lock
);
16076 dtrace_helper_provider_register(to
, newhelp
, NULL
);
16080 * DTrace Hook Functions
16083 dtrace_module_loaded(struct modctl
*ctl
)
16085 dtrace_provider_t
*prv
;
16087 lck_mtx_lock(&dtrace_provider_lock
);
16088 lck_mtx_lock(&mod_lock
);
16090 #if !defined(__APPLE__)
16091 ASSERT(ctl
->mod_busy
);
16093 /* FIXME: awaits kmod awareness PR_4648477. */
16094 #endif /* __APPLE__ */
16097 * We're going to call each providers per-module provide operation
16098 * specifying only this module.
16100 for (prv
= dtrace_provider
; prv
!= NULL
; prv
= prv
->dtpv_next
)
16101 prv
->dtpv_pops
.dtps_provide_module(prv
->dtpv_arg
, ctl
);
16103 lck_mtx_unlock(&mod_lock
);
16104 lck_mtx_unlock(&dtrace_provider_lock
);
16107 * If we have any retained enablings, we need to match against them.
16108 * Enabling probes requires that cpu_lock be held, and we cannot hold
16109 * cpu_lock here -- it is legal for cpu_lock to be held when loading a
16110 * module. (In particular, this happens when loading scheduling
16111 * classes.) So if we have any retained enablings, we need to dispatch
16112 * our task queue to do the match for us.
16114 lck_mtx_lock(&dtrace_lock
);
16116 if (dtrace_retained
== NULL
) {
16117 lck_mtx_unlock(&dtrace_lock
);
16121 (void) taskq_dispatch(dtrace_taskq
,
16122 (task_func_t
*)dtrace_enabling_matchall
, NULL
, TQ_SLEEP
);
16124 lck_mtx_unlock(&dtrace_lock
);
16127 * And now, for a little heuristic sleaze: in general, we want to
16128 * match modules as soon as they load. However, we cannot guarantee
16129 * this, because it would lead us to the lock ordering violation
16130 * outlined above. The common case, of course, is that cpu_lock is
16131 * _not_ held -- so we delay here for a clock tick, hoping that that's
16132 * long enough for the task queue to do its work. If it's not, it's
16133 * not a serious problem -- it just means that the module that we
16134 * just loaded may not be immediately instrumentable.
16140 dtrace_module_unloaded(struct modctl
*ctl
)
16142 dtrace_probe_t
template, *probe
, *first
, *next
;
16143 dtrace_provider_t
*prov
;
16145 template.dtpr_mod
= ctl
->mod_modname
;
16147 lck_mtx_lock(&dtrace_provider_lock
);
16148 lck_mtx_lock(&mod_lock
);
16149 lck_mtx_lock(&dtrace_lock
);
16151 if (dtrace_bymod
== NULL
) {
16153 * The DTrace module is loaded (obviously) but not attached;
16154 * we don't have any work to do.
16156 lck_mtx_unlock(&dtrace_provider_lock
);
16157 lck_mtx_unlock(&mod_lock
);
16158 lck_mtx_unlock(&dtrace_lock
);
16162 for (probe
= first
= dtrace_hash_lookup(dtrace_bymod
, &template);
16163 probe
!= NULL
; probe
= probe
->dtpr_nextmod
) {
16164 if (probe
->dtpr_ecb
!= NULL
) {
16165 lck_mtx_unlock(&dtrace_provider_lock
);
16166 lck_mtx_unlock(&mod_lock
);
16167 lck_mtx_unlock(&dtrace_lock
);
16170 * This shouldn't _actually_ be possible -- we're
16171 * unloading a module that has an enabled probe in it.
16172 * (It's normally up to the provider to make sure that
16173 * this can't happen.) However, because dtps_enable()
16174 * doesn't have a failure mode, there can be an
16175 * enable/unload race. Upshot: we don't want to
16176 * assert, but we're not going to disable the
16179 if (dtrace_err_verbose
) {
16180 cmn_err(CE_WARN
, "unloaded module '%s' had "
16181 "enabled probes", ctl
->mod_modname
);
16190 for (first
= NULL
; probe
!= NULL
; probe
= next
) {
16191 ASSERT(dtrace_probes
[probe
->dtpr_id
- 1] == probe
);
16193 dtrace_probes
[probe
->dtpr_id
- 1] = NULL
;
16195 next
= probe
->dtpr_nextmod
;
16196 dtrace_hash_remove(dtrace_bymod
, probe
);
16197 dtrace_hash_remove(dtrace_byfunc
, probe
);
16198 dtrace_hash_remove(dtrace_byname
, probe
);
16200 if (first
== NULL
) {
16202 probe
->dtpr_nextmod
= NULL
;
16204 probe
->dtpr_nextmod
= first
;
16210 * We've removed all of the module's probes from the hash chains and
16211 * from the probe array. Now issue a dtrace_sync() to be sure that
16212 * everyone has cleared out from any probe array processing.
16216 for (probe
= first
; probe
!= NULL
; probe
= first
) {
16217 first
= probe
->dtpr_nextmod
;
16218 prov
= probe
->dtpr_provider
;
16219 prov
->dtpv_pops
.dtps_destroy(prov
->dtpv_arg
, probe
->dtpr_id
,
16221 kmem_free(probe
->dtpr_mod
, strlen(probe
->dtpr_mod
) + 1);
16222 kmem_free(probe
->dtpr_func
, strlen(probe
->dtpr_func
) + 1);
16223 kmem_free(probe
->dtpr_name
, strlen(probe
->dtpr_name
) + 1);
16224 vmem_free(dtrace_arena
, (void *)(uintptr_t)probe
->dtpr_id
, 1);
16225 #if !defined(__APPLE__)
16226 kmem_free(probe
, sizeof (dtrace_probe_t
));
16228 zfree(dtrace_probe_t_zone
, probe
);
16229 #endif /* __APPLE__ */
16232 lck_mtx_unlock(&dtrace_lock
);
16233 lck_mtx_unlock(&mod_lock
);
16234 lck_mtx_unlock(&dtrace_provider_lock
);
16238 dtrace_suspend(void)
16240 dtrace_probe_foreach(offsetof(dtrace_pops_t
, dtps_suspend
));
16244 dtrace_resume(void)
16246 dtrace_probe_foreach(offsetof(dtrace_pops_t
, dtps_resume
));
16250 dtrace_cpu_setup(cpu_setup_t what
, processorid_t cpu
)
16252 lck_mtx_assert(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
16253 lck_mtx_lock(&dtrace_lock
);
16257 dtrace_state_t
*state
;
16258 dtrace_optval_t
*opt
, rs
, c
;
16261 * For now, we only allocate a new buffer for anonymous state.
16263 if ((state
= dtrace_anon
.dta_state
) == NULL
)
16266 if (state
->dts_activity
!= DTRACE_ACTIVITY_ACTIVE
)
16269 opt
= state
->dts_options
;
16270 c
= opt
[DTRACEOPT_CPU
];
16272 if (c
!= DTRACE_CPUALL
&& c
!= DTRACEOPT_UNSET
&& c
!= cpu
)
16276 * Regardless of what the actual policy is, we're going to
16277 * temporarily set our resize policy to be manual. We're
16278 * also going to temporarily set our CPU option to denote
16279 * the newly configured CPU.
16281 rs
= opt
[DTRACEOPT_BUFRESIZE
];
16282 opt
[DTRACEOPT_BUFRESIZE
] = DTRACEOPT_BUFRESIZE_MANUAL
;
16283 opt
[DTRACEOPT_CPU
] = (dtrace_optval_t
)cpu
;
16285 (void) dtrace_state_buffers(state
);
16287 opt
[DTRACEOPT_BUFRESIZE
] = rs
;
16288 opt
[DTRACEOPT_CPU
] = c
;
16295 * We don't free the buffer in the CPU_UNCONFIG case. (The
16296 * buffer will be freed when the consumer exits.)
16304 lck_mtx_unlock(&dtrace_lock
);
16309 dtrace_cpu_setup_initial(processorid_t cpu
)
16311 (void) dtrace_cpu_setup(CPU_CONFIG
, cpu
);
16315 dtrace_toxrange_add(uintptr_t base
, uintptr_t limit
)
16317 if (dtrace_toxranges
>= dtrace_toxranges_max
) {
16319 dtrace_toxrange_t
*range
;
16321 osize
= dtrace_toxranges_max
* sizeof (dtrace_toxrange_t
);
16324 ASSERT(dtrace_toxrange
== NULL
);
16325 ASSERT(dtrace_toxranges_max
== 0);
16326 dtrace_toxranges_max
= 1;
16328 dtrace_toxranges_max
<<= 1;
16331 nsize
= dtrace_toxranges_max
* sizeof (dtrace_toxrange_t
);
16332 range
= kmem_zalloc(nsize
, KM_SLEEP
);
16334 if (dtrace_toxrange
!= NULL
) {
16335 ASSERT(osize
!= 0);
16336 bcopy(dtrace_toxrange
, range
, osize
);
16337 kmem_free(dtrace_toxrange
, osize
);
16340 dtrace_toxrange
= range
;
16343 ASSERT(dtrace_toxrange
[dtrace_toxranges
].dtt_base
== NULL
);
16344 ASSERT(dtrace_toxrange
[dtrace_toxranges
].dtt_limit
== NULL
);
16346 dtrace_toxrange
[dtrace_toxranges
].dtt_base
= base
;
16347 dtrace_toxrange
[dtrace_toxranges
].dtt_limit
= limit
;
16348 dtrace_toxranges
++;
16352 * DTrace Driver Cookbook Functions
16356 dtrace_attach(dev_info_t
*devi
, ddi_attach_cmd_t cmd
)
16358 #pragma unused(cmd) /* __APPLE__ */
16359 dtrace_provider_id_t id
;
16360 dtrace_state_t
*state
= NULL
;
16361 dtrace_enabling_t
*enab
;
16363 lck_mtx_lock(&cpu_lock
);
16364 lck_mtx_lock(&dtrace_provider_lock
);
16365 lck_mtx_lock(&dtrace_lock
);
16367 if (ddi_soft_state_init(&dtrace_softstate
,
16368 sizeof (dtrace_state_t
), 0) != 0) {
16369 cmn_err(CE_NOTE
, "/dev/dtrace failed to initialize soft state");
16370 lck_mtx_unlock(&cpu_lock
);
16371 lck_mtx_unlock(&dtrace_provider_lock
);
16372 lck_mtx_unlock(&dtrace_lock
);
16373 return (DDI_FAILURE
);
16376 #if !defined(__APPLE__)
16377 if (ddi_create_minor_node(devi
, DTRACEMNR_DTRACE
, S_IFCHR
,
16378 DTRACEMNRN_DTRACE
, DDI_PSEUDO
, NULL
) == DDI_FAILURE
||
16379 ddi_create_minor_node(devi
, DTRACEMNR_HELPER
, S_IFCHR
,
16380 DTRACEMNRN_HELPER
, DDI_PSEUDO
, NULL
) == DDI_FAILURE
) {
16381 cmn_err(CE_NOTE
, "/dev/dtrace couldn't create minor nodes");
16382 ddi_remove_minor_node(devi
, NULL
);
16383 ddi_soft_state_fini(&dtrace_softstate
);
16384 lck_mtx_unlock(&cpu_lock
);
16385 lck_mtx_unlock(&dtrace_provider_lock
);
16386 lck_mtx_unlock(&dtrace_lock
);
16387 return (DDI_FAILURE
);
16390 /* Darwin uses BSD cloning device driver to automagically obtain minor device number. */
16391 #endif /* __APPLE__ */
16393 ddi_report_dev(devi
);
16394 dtrace_devi
= devi
;
16396 dtrace_modload
= dtrace_module_loaded
;
16397 dtrace_modunload
= dtrace_module_unloaded
;
16398 dtrace_cpu_init
= dtrace_cpu_setup_initial
;
16399 dtrace_helpers_cleanup
= dtrace_helpers_destroy
;
16400 dtrace_helpers_fork
= dtrace_helpers_duplicate
;
16401 dtrace_cpustart_init
= dtrace_suspend
;
16402 dtrace_cpustart_fini
= dtrace_resume
;
16403 dtrace_debugger_init
= dtrace_suspend
;
16404 dtrace_debugger_fini
= dtrace_resume
;
16406 register_cpu_setup_func((cpu_setup_func_t
*)dtrace_cpu_setup
, NULL
);
16408 lck_mtx_assert(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
16410 dtrace_arena
= vmem_create("dtrace", (void *)1, UINT32_MAX
, 1,
16411 NULL
, NULL
, NULL
, 0, VM_SLEEP
| VMC_IDENTIFIER
);
16412 dtrace_minor
= vmem_create("dtrace_minor", (void *)DTRACEMNRN_CLONE
,
16413 UINT32_MAX
- DTRACEMNRN_CLONE
, 1, NULL
, NULL
, NULL
, 0,
16414 VM_SLEEP
| VMC_IDENTIFIER
);
16415 dtrace_taskq
= taskq_create("dtrace_taskq", 1, maxclsyspri
,
16418 dtrace_state_cache
= kmem_cache_create("dtrace_state_cache",
16419 sizeof (dtrace_dstate_percpu_t
) * (int)NCPU
, DTRACE_STATE_ALIGN
,
16420 NULL
, NULL
, NULL
, NULL
, NULL
, 0);
16422 lck_mtx_assert(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
16423 dtrace_bymod
= dtrace_hash_create(offsetof(dtrace_probe_t
, dtpr_mod
),
16424 offsetof(dtrace_probe_t
, dtpr_nextmod
),
16425 offsetof(dtrace_probe_t
, dtpr_prevmod
));
16427 dtrace_byfunc
= dtrace_hash_create(offsetof(dtrace_probe_t
, dtpr_func
),
16428 offsetof(dtrace_probe_t
, dtpr_nextfunc
),
16429 offsetof(dtrace_probe_t
, dtpr_prevfunc
));
16431 dtrace_byname
= dtrace_hash_create(offsetof(dtrace_probe_t
, dtpr_name
),
16432 offsetof(dtrace_probe_t
, dtpr_nextname
),
16433 offsetof(dtrace_probe_t
, dtpr_prevname
));
16435 if (dtrace_retain_max
< 1) {
16436 cmn_err(CE_WARN
, "illegal value (%lu) for dtrace_retain_max; "
16437 "setting to 1", dtrace_retain_max
);
16438 dtrace_retain_max
= 1;
16442 * Now discover our toxic ranges.
16444 dtrace_toxic_ranges(dtrace_toxrange_add
);
16447 * Before we register ourselves as a provider to our own framework,
16448 * we would like to assert that dtrace_provider is NULL -- but that's
16449 * not true if we were loaded as a dependency of a DTrace provider.
16450 * Once we've registered, we can assert that dtrace_provider is our
16453 (void) dtrace_register("dtrace", &dtrace_provider_attr
,
16454 DTRACE_PRIV_NONE
, 0, &dtrace_provider_ops
, NULL
, &id
);
16456 ASSERT(dtrace_provider
!= NULL
);
16457 ASSERT((dtrace_provider_id_t
)dtrace_provider
== id
);
16459 #if !defined(__APPLE__)
16460 dtrace_probeid_begin
= dtrace_probe_create((dtrace_provider_id_t
)
16461 dtrace_provider
, NULL
, NULL
, "BEGIN", 0, NULL
);
16462 dtrace_probeid_end
= dtrace_probe_create((dtrace_provider_id_t
)
16463 dtrace_provider
, NULL
, NULL
, "END", 0, NULL
);
16464 dtrace_probeid_error
= dtrace_probe_create((dtrace_provider_id_t
)
16465 dtrace_provider
, NULL
, NULL
, "ERROR", 1, NULL
);
16466 #elif defined(__ppc__) || defined(__ppc64__)
16467 dtrace_probeid_begin
= dtrace_probe_create((dtrace_provider_id_t
)
16468 dtrace_provider
, NULL
, NULL
, "BEGIN", 2, NULL
);
16469 dtrace_probeid_end
= dtrace_probe_create((dtrace_provider_id_t
)
16470 dtrace_provider
, NULL
, NULL
, "END", 1, NULL
);
16471 dtrace_probeid_error
= dtrace_probe_create((dtrace_provider_id_t
)
16472 dtrace_provider
, NULL
, NULL
, "ERROR", 4, NULL
);
16473 #elif (defined(__i386__) || defined (__x86_64__))
16474 dtrace_probeid_begin
= dtrace_probe_create((dtrace_provider_id_t
)
16475 dtrace_provider
, NULL
, NULL
, "BEGIN", 1, NULL
);
16476 dtrace_probeid_end
= dtrace_probe_create((dtrace_provider_id_t
)
16477 dtrace_provider
, NULL
, NULL
, "END", 0, NULL
);
16478 dtrace_probeid_error
= dtrace_probe_create((dtrace_provider_id_t
)
16479 dtrace_provider
, NULL
, NULL
, "ERROR", 3, NULL
);
16481 #error Unknown Architecture
16482 #endif /* __APPLE__ */
16484 dtrace_anon_property();
16485 lck_mtx_unlock(&cpu_lock
);
16488 * If DTrace helper tracing is enabled, we need to allocate the
16489 * trace buffer and initialize the values.
16491 if (dtrace_helptrace_enabled
) {
16492 ASSERT(dtrace_helptrace_buffer
== NULL
);
16493 dtrace_helptrace_buffer
=
16494 kmem_zalloc(dtrace_helptrace_bufsize
, KM_SLEEP
);
16495 dtrace_helptrace_next
= 0;
16499 * If there are already providers, we must ask them to provide their
16500 * probes, and then match any anonymous enabling against them. Note
16501 * that there should be no other retained enablings at this time:
16502 * the only retained enablings at this time should be the anonymous
16505 if (dtrace_anon
.dta_enabling
!= NULL
) {
16506 ASSERT(dtrace_retained
== dtrace_anon
.dta_enabling
);
16508 dtrace_enabling_provide(NULL
);
16509 state
= dtrace_anon
.dta_state
;
16512 * We couldn't hold cpu_lock across the above call to
16513 * dtrace_enabling_provide(), but we must hold it to actually
16514 * enable the probes. We have to drop all of our locks, pick
16515 * up cpu_lock, and regain our locks before matching the
16516 * retained anonymous enabling.
16518 lck_mtx_unlock(&dtrace_lock
);
16519 lck_mtx_unlock(&dtrace_provider_lock
);
16521 lck_mtx_lock(&cpu_lock
);
16522 lck_mtx_lock(&dtrace_provider_lock
);
16523 lck_mtx_lock(&dtrace_lock
);
16525 if ((enab
= dtrace_anon
.dta_enabling
) != NULL
)
16526 (void) dtrace_enabling_match(enab
, NULL
);
16528 lck_mtx_unlock(&cpu_lock
);
16531 lck_mtx_unlock(&dtrace_lock
);
16532 lck_mtx_unlock(&dtrace_provider_lock
);
16534 if (state
!= NULL
) {
16536 * If we created any anonymous state, set it going now.
16538 (void) dtrace_state_go(state
, &dtrace_anon
.dta_beganon
);
16541 return (DDI_SUCCESS
);
16546 dtrace_open(dev_t
*devp
, int flag
, int otyp
, cred_t
*cred_p
)
16548 #pragma unused(flag, otyp)
16549 dtrace_state_t
*state
;
16553 #if defined (__APPLE__)
16555 #endif /* __APPLE__ */
16557 #if !defined(__APPLE__)
16558 if (getminor(*devp
) == DTRACEMNRN_HELPER
)
16562 * If this wasn't an open with the "helper" minor, then it must be
16563 * the "dtrace" minor.
16565 if (getminor(*devp
) != DTRACEMNRN_DTRACE
)
16568 /* Darwin puts Helper on its own major device. */
16569 #endif /* __APPLE__ */
16572 * If no DTRACE_PRIV_* bits are set in the credential, then the
16573 * caller lacks sufficient permission to do anything with DTrace.
16575 dtrace_cred2priv(cred_p
, &priv
, &uid
, &zoneid
);
16576 if (priv
== DTRACE_PRIV_NONE
)
16579 #if defined(__APPLE__)
16581 * We delay the initialization of fasttrap as late as possible.
16582 * It certainly can't be later than now!
16585 #endif /* __APPLE__ */
16588 * Ask all providers to provide all their probes.
16590 lck_mtx_lock(&dtrace_provider_lock
);
16591 dtrace_probe_provide(NULL
, NULL
);
16592 lck_mtx_unlock(&dtrace_provider_lock
);
16594 lck_mtx_lock(&cpu_lock
);
16595 lck_mtx_lock(&dtrace_lock
);
16597 dtrace_membar_producer();
16600 * If the kernel debugger is active (that is, if the kernel debugger
16601 * modified text in some way), we won't allow the open.
16603 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE
) != 0) {
16605 lck_mtx_unlock(&cpu_lock
);
16606 lck_mtx_unlock(&dtrace_lock
);
16610 #if !defined(__APPLE__)
16611 state
= dtrace_state_create(devp
, cred_p
);
16612 lck_mtx_unlock(&cpu_lock
);
16614 if (state
== NULL
) {
16615 if (--dtrace_opens
== 0)
16616 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE
);
16617 lck_mtx_unlock(&dtrace_lock
);
16621 lck_mtx_unlock(&dtrace_lock
);
16623 rv
= dtrace_state_create(devp
, cred_p
, &state
);
16624 lck_mtx_unlock(&cpu_lock
);
16626 if (rv
!= 0 || state
== NULL
) {
16627 if (--dtrace_opens
== 0)
16628 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE
);
16629 lck_mtx_unlock(&dtrace_lock
);
16630 /* propagate EAGAIN or ERESTART */
16634 lck_mtx_unlock(&dtrace_lock
);
16636 lck_rw_lock_exclusive(&dtrace_dof_mode_lock
);
16639 * If we are currently lazy, transition states.
16641 * Unlike dtrace_close, we do not need to check the
16642 * value of dtrace_opens, as any positive value (and
16643 * we count as 1) means we transition states.
16645 if (dtrace_dof_mode
== DTRACE_DOF_MODE_LAZY_ON
) {
16646 dtrace_dof_mode
= DTRACE_DOF_MODE_LAZY_OFF
;
16649 * Iterate all existing processes and load lazy dofs.
16651 proc_iterate(PROC_ALLPROCLIST
| PROC_NOWAITTRANS
,
16652 dtrace_lazy_dofs_proc_iterate_doit
,
16654 dtrace_lazy_dofs_proc_iterate_filter
,
16658 lck_rw_unlock_exclusive(&dtrace_dof_mode_lock
);
16659 #endif /* __APPLE__ */
16666 dtrace_close(dev_t dev
, int flag
, int otyp
, cred_t
*cred_p
)
16668 #pragma unused(flag, otyp, cred_p) /* __APPLE__ */
16669 minor_t minor
= getminor(dev
);
16670 dtrace_state_t
*state
;
16672 #if !defined(__APPLE__)
16673 if (minor
== DTRACEMNRN_HELPER
)
16676 /* Darwin puts Helper on its own major device. */
16677 #endif /* __APPLE__ */
16679 state
= ddi_get_soft_state(dtrace_softstate
, minor
);
16681 lck_mtx_lock(&cpu_lock
);
16682 lck_mtx_lock(&dtrace_lock
);
16684 if (state
->dts_anon
) {
16686 * There is anonymous state. Destroy that first.
16688 ASSERT(dtrace_anon
.dta_state
== NULL
);
16689 dtrace_state_destroy(state
->dts_anon
);
16692 dtrace_state_destroy(state
);
16693 ASSERT(dtrace_opens
> 0);
16694 if (--dtrace_opens
== 0)
16695 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE
);
16697 lck_mtx_unlock(&dtrace_lock
);
16698 lck_mtx_unlock(&cpu_lock
);
16700 #if defined(__APPLE__)
16703 * Lock ordering requires the dof mode lock be taken before
16706 lck_rw_lock_exclusive(&dtrace_dof_mode_lock
);
16707 lck_mtx_lock(&dtrace_lock
);
16710 * If we are currently lazy-off, and this is the last close, transition to
16713 if (dtrace_dof_mode
== DTRACE_DOF_MODE_LAZY_OFF
&& dtrace_opens
== 0) {
16714 dtrace_dof_mode
= DTRACE_DOF_MODE_LAZY_ON
;
16717 lck_mtx_unlock(&dtrace_lock
);
16718 lck_rw_unlock_exclusive(&dtrace_dof_mode_lock
);
16719 #endif /* __APPLE__ */
16724 #if !defined(__APPLE__)
16727 dtrace_ioctl_helper(int cmd
, intptr_t arg
, int *rv
)
16730 dof_helper_t help
, *dhp
= NULL
;
16733 case DTRACEHIOC_ADDDOF
:
16734 if (copyin((void *)arg
, &help
, sizeof (help
)) != 0) {
16735 dtrace_dof_error(NULL
, "failed to copyin DOF helper");
16740 arg
= (intptr_t)help
.dofhp_dof
;
16743 case DTRACEHIOC_ADD
: {
16744 dof_hdr_t
*dof
= dtrace_dof_copyin(arg
, &rval
);
16749 mutex_enter(&dtrace_lock
);
16752 * dtrace_helper_slurp() takes responsibility for the dof --
16753 * it may free it now or it may save it and free it later.
16755 if ((rval
= dtrace_helper_slurp(dof
, dhp
)) != -1) {
16762 mutex_exit(&dtrace_lock
);
16766 case DTRACEHIOC_REMOVE
: {
16767 mutex_enter(&dtrace_lock
);
16768 rval
= dtrace_helper_destroygen(arg
);
16769 mutex_exit(&dtrace_lock
);
16783 dtrace_ioctl(dev_t dev
, u_long cmd
, intptr_t arg
, int md
, cred_t
*cr
, int *rv
)
16785 minor_t minor
= getminor(dev
);
16786 dtrace_state_t
*state
;
16789 if (minor
== DTRACEMNRN_HELPER
)
16790 return (dtrace_ioctl_helper(cmd
, arg
, rv
));
16792 state
= ddi_get_soft_state(dtrace_softstate
, minor
);
16794 if (state
->dts_anon
) {
16795 ASSERT(dtrace_anon
.dta_state
== NULL
);
16796 state
= state
->dts_anon
;
16800 case DTRACEIOC_PROVIDER
: {
16801 dtrace_providerdesc_t pvd
;
16802 dtrace_provider_t
*pvp
;
16804 if (copyin((void *)arg
, &pvd
, sizeof (pvd
)) != 0)
16807 pvd
.dtvd_name
[DTRACE_PROVNAMELEN
- 1] = '\0';
16808 lck_mtx_lock(&dtrace_provider_lock
);
16810 for (pvp
= dtrace_provider
; pvp
!= NULL
; pvp
= pvp
->dtpv_next
) {
16811 if (strcmp(pvp
->dtpv_name
, pvd
.dtvd_name
) == 0)
16815 lck_mtx_unlock(&dtrace_provider_lock
);
16820 bcopy(&pvp
->dtpv_priv
, &pvd
.dtvd_priv
, sizeof (dtrace_ppriv_t
));
16821 bcopy(&pvp
->dtpv_attr
, &pvd
.dtvd_attr
, sizeof (dtrace_pattr_t
));
16822 if (copyout(&pvd
, (void *)arg
, sizeof (pvd
)) != 0)
16828 case DTRACEIOC_EPROBE
: {
16829 dtrace_eprobedesc_t epdesc
;
16831 dtrace_action_t
*act
;
16837 if (copyin((void *)arg
, &epdesc
, sizeof (epdesc
)) != 0)
16840 lck_mtx_lock(&dtrace_lock
);
16842 if ((ecb
= dtrace_epid2ecb(state
, epdesc
.dtepd_epid
)) == NULL
) {
16843 lck_mtx_unlock(&dtrace_lock
);
16847 if (ecb
->dte_probe
== NULL
) {
16848 lck_mtx_unlock(&dtrace_lock
);
16852 epdesc
.dtepd_probeid
= ecb
->dte_probe
->dtpr_id
;
16853 epdesc
.dtepd_uarg
= ecb
->dte_uarg
;
16854 epdesc
.dtepd_size
= ecb
->dte_size
;
16856 nrecs
= epdesc
.dtepd_nrecs
;
16857 epdesc
.dtepd_nrecs
= 0;
16858 for (act
= ecb
->dte_action
; act
!= NULL
; act
= act
->dta_next
) {
16859 if (DTRACEACT_ISAGG(act
->dta_kind
) || act
->dta_intuple
)
16862 epdesc
.dtepd_nrecs
++;
16866 * Now that we have the size, we need to allocate a temporary
16867 * buffer in which to store the complete description. We need
16868 * the temporary buffer to be able to drop dtrace_lock()
16869 * across the copyout(), below.
16871 size
= sizeof (dtrace_eprobedesc_t
) +
16872 (epdesc
.dtepd_nrecs
* sizeof (dtrace_recdesc_t
));
16874 buf
= kmem_alloc(size
, KM_SLEEP
);
16875 dest
= (uintptr_t)buf
;
16877 bcopy(&epdesc
, (void *)dest
, sizeof (epdesc
));
16878 dest
+= offsetof(dtrace_eprobedesc_t
, dtepd_rec
[0]);
16880 for (act
= ecb
->dte_action
; act
!= NULL
; act
= act
->dta_next
) {
16881 if (DTRACEACT_ISAGG(act
->dta_kind
) || act
->dta_intuple
)
16887 bcopy(&act
->dta_rec
, (void *)dest
,
16888 sizeof (dtrace_recdesc_t
));
16889 dest
+= sizeof (dtrace_recdesc_t
);
16892 lck_mtx_unlock(&dtrace_lock
);
16894 if (copyout(buf
, (void *)arg
, dest
- (uintptr_t)buf
) != 0) {
16895 kmem_free(buf
, size
);
16899 kmem_free(buf
, size
);
16903 case DTRACEIOC_AGGDESC
: {
16904 dtrace_aggdesc_t aggdesc
;
16905 dtrace_action_t
*act
;
16906 dtrace_aggregation_t
*agg
;
16909 dtrace_recdesc_t
*lrec
;
16914 if (copyin((void *)arg
, &aggdesc
, sizeof (aggdesc
)) != 0)
16917 lck_mtx_lock(&dtrace_lock
);
16919 if ((agg
= dtrace_aggid2agg(state
, aggdesc
.dtagd_id
)) == NULL
) {
16920 lck_mtx_unlock(&dtrace_lock
);
16924 aggdesc
.dtagd_epid
= agg
->dtag_ecb
->dte_epid
;
16926 nrecs
= aggdesc
.dtagd_nrecs
;
16927 aggdesc
.dtagd_nrecs
= 0;
16929 offs
= agg
->dtag_base
;
16930 lrec
= &agg
->dtag_action
.dta_rec
;
16931 aggdesc
.dtagd_size
= lrec
->dtrd_offset
+ lrec
->dtrd_size
- offs
;
16933 for (act
= agg
->dtag_first
; ; act
= act
->dta_next
) {
16934 ASSERT(act
->dta_intuple
||
16935 DTRACEACT_ISAGG(act
->dta_kind
));
16938 * If this action has a record size of zero, it
16939 * denotes an argument to the aggregating action.
16940 * Because the presence of this record doesn't (or
16941 * shouldn't) affect the way the data is interpreted,
16942 * we don't copy it out to save user-level the
16943 * confusion of dealing with a zero-length record.
16945 if (act
->dta_rec
.dtrd_size
== 0) {
16946 ASSERT(agg
->dtag_hasarg
);
16950 aggdesc
.dtagd_nrecs
++;
16952 if (act
== &agg
->dtag_action
)
16957 * Now that we have the size, we need to allocate a temporary
16958 * buffer in which to store the complete description. We need
16959 * the temporary buffer to be able to drop dtrace_lock()
16960 * across the copyout(), below.
16962 size
= sizeof (dtrace_aggdesc_t
) +
16963 (aggdesc
.dtagd_nrecs
* sizeof (dtrace_recdesc_t
));
16965 buf
= kmem_alloc(size
, KM_SLEEP
);
16966 dest
= (uintptr_t)buf
;
16968 bcopy(&aggdesc
, (void *)dest
, sizeof (aggdesc
));
16969 dest
+= offsetof(dtrace_aggdesc_t
, dtagd_rec
[0]);
16971 for (act
= agg
->dtag_first
; ; act
= act
->dta_next
) {
16972 dtrace_recdesc_t rec
= act
->dta_rec
;
16975 * See the comment in the above loop for why we pass
16976 * over zero-length records.
16978 if (rec
.dtrd_size
== 0) {
16979 ASSERT(agg
->dtag_hasarg
);
16986 rec
.dtrd_offset
-= offs
;
16987 bcopy(&rec
, (void *)dest
, sizeof (rec
));
16988 dest
+= sizeof (dtrace_recdesc_t
);
16990 if (act
== &agg
->dtag_action
)
16994 lck_mtx_unlock(&dtrace_lock
);
16996 if (copyout(buf
, (void *)arg
, dest
- (uintptr_t)buf
) != 0) {
16997 kmem_free(buf
, size
);
17001 kmem_free(buf
, size
);
17005 case DTRACEIOC_ENABLE
: {
17007 dtrace_enabling_t
*enab
= NULL
;
17008 dtrace_vstate_t
*vstate
;
17014 * If a NULL argument has been passed, we take this as our
17015 * cue to reevaluate our enablings.
17018 dtrace_enabling_matchall();
17023 if ((dof
= dtrace_dof_copyin(arg
, &rval
)) == NULL
)
17026 lck_mtx_lock(&cpu_lock
);
17027 lck_mtx_lock(&dtrace_lock
);
17028 vstate
= &state
->dts_vstate
;
17030 if (state
->dts_activity
!= DTRACE_ACTIVITY_INACTIVE
) {
17031 lck_mtx_unlock(&dtrace_lock
);
17032 lck_mtx_unlock(&cpu_lock
);
17033 dtrace_dof_destroy(dof
);
17037 if (dtrace_dof_slurp(dof
, vstate
, cr
, &enab
, 0, B_TRUE
) != 0) {
17038 lck_mtx_unlock(&dtrace_lock
);
17039 lck_mtx_unlock(&cpu_lock
);
17040 dtrace_dof_destroy(dof
);
17044 if ((rval
= dtrace_dof_options(dof
, state
)) != 0) {
17045 dtrace_enabling_destroy(enab
);
17046 lck_mtx_unlock(&dtrace_lock
);
17047 lck_mtx_unlock(&cpu_lock
);
17048 dtrace_dof_destroy(dof
);
17052 if ((err
= dtrace_enabling_match(enab
, rv
)) == 0) {
17053 err
= dtrace_enabling_retain(enab
);
17055 dtrace_enabling_destroy(enab
);
17058 lck_mtx_unlock(&cpu_lock
);
17059 lck_mtx_unlock(&dtrace_lock
);
17060 dtrace_dof_destroy(dof
);
17065 case DTRACEIOC_REPLICATE
: {
17066 dtrace_repldesc_t desc
;
17067 dtrace_probedesc_t
*match
= &desc
.dtrpd_match
;
17068 dtrace_probedesc_t
*create
= &desc
.dtrpd_create
;
17071 if (copyin((void *)arg
, &desc
, sizeof (desc
)) != 0)
17074 match
->dtpd_provider
[DTRACE_PROVNAMELEN
- 1] = '\0';
17075 match
->dtpd_mod
[DTRACE_MODNAMELEN
- 1] = '\0';
17076 match
->dtpd_func
[DTRACE_FUNCNAMELEN
- 1] = '\0';
17077 match
->dtpd_name
[DTRACE_NAMELEN
- 1] = '\0';
17079 create
->dtpd_provider
[DTRACE_PROVNAMELEN
- 1] = '\0';
17080 create
->dtpd_mod
[DTRACE_MODNAMELEN
- 1] = '\0';
17081 create
->dtpd_func
[DTRACE_FUNCNAMELEN
- 1] = '\0';
17082 create
->dtpd_name
[DTRACE_NAMELEN
- 1] = '\0';
17084 lck_mtx_lock(&dtrace_lock
);
17085 err
= dtrace_enabling_replicate(state
, match
, create
);
17086 lck_mtx_unlock(&dtrace_lock
);
17091 case DTRACEIOC_PROBEMATCH
:
17092 case DTRACEIOC_PROBES
: {
17093 dtrace_probe_t
*probe
= NULL
;
17094 dtrace_probedesc_t desc
;
17095 dtrace_probekey_t pkey
;
17102 if (copyin((void *)arg
, &desc
, sizeof (desc
)) != 0)
17105 desc
.dtpd_provider
[DTRACE_PROVNAMELEN
- 1] = '\0';
17106 desc
.dtpd_mod
[DTRACE_MODNAMELEN
- 1] = '\0';
17107 desc
.dtpd_func
[DTRACE_FUNCNAMELEN
- 1] = '\0';
17108 desc
.dtpd_name
[DTRACE_NAMELEN
- 1] = '\0';
17111 * Before we attempt to match this probe, we want to give
17112 * all providers the opportunity to provide it.
17114 if (desc
.dtpd_id
== DTRACE_IDNONE
) {
17115 lck_mtx_lock(&dtrace_provider_lock
);
17116 dtrace_probe_provide(&desc
, NULL
);
17117 lck_mtx_unlock(&dtrace_provider_lock
);
17121 if (cmd
== DTRACEIOC_PROBEMATCH
) {
17122 dtrace_probekey(&desc
, &pkey
);
17123 pkey
.dtpk_id
= DTRACE_IDNONE
;
17126 dtrace_cred2priv(cr
, &priv
, &uid
, &zoneid
);
17128 lck_mtx_lock(&dtrace_lock
);
17130 if (cmd
== DTRACEIOC_PROBEMATCH
) {
17131 for (i
= desc
.dtpd_id
; i
<= dtrace_nprobes
; i
++) {
17132 if ((probe
= dtrace_probes
[i
- 1]) != NULL
&&
17133 (m
= dtrace_match_probe(probe
, &pkey
,
17134 priv
, uid
, zoneid
)) != 0)
17139 lck_mtx_unlock(&dtrace_lock
);
17144 for (i
= desc
.dtpd_id
; i
<= dtrace_nprobes
; i
++) {
17145 if ((probe
= dtrace_probes
[i
- 1]) != NULL
&&
17146 dtrace_match_priv(probe
, priv
, uid
, zoneid
))
17151 if (probe
== NULL
) {
17152 lck_mtx_unlock(&dtrace_lock
);
17156 dtrace_probe_description(probe
, &desc
);
17157 lck_mtx_unlock(&dtrace_lock
);
17159 if (copyout(&desc
, (void *)arg
, sizeof (desc
)) != 0)
17165 case DTRACEIOC_PROBEARG
: {
17166 dtrace_argdesc_t desc
;
17167 dtrace_probe_t
*probe
;
17168 dtrace_provider_t
*prov
;
17170 if (copyin((void *)arg
, &desc
, sizeof (desc
)) != 0)
17173 if (desc
.dtargd_id
== DTRACE_IDNONE
)
17176 if (desc
.dtargd_ndx
== DTRACE_ARGNONE
)
17179 lck_mtx_lock(&dtrace_provider_lock
);
17180 lck_mtx_lock(&mod_lock
);
17181 lck_mtx_lock(&dtrace_lock
);
17183 if (desc
.dtargd_id
> dtrace_nprobes
) {
17184 lck_mtx_unlock(&dtrace_lock
);
17185 lck_mtx_unlock(&mod_lock
);
17186 lck_mtx_unlock(&dtrace_provider_lock
);
17190 if ((probe
= dtrace_probes
[desc
.dtargd_id
- 1]) == NULL
) {
17191 lck_mtx_unlock(&dtrace_lock
);
17192 lck_mtx_unlock(&mod_lock
);
17193 lck_mtx_unlock(&dtrace_provider_lock
);
17197 lck_mtx_unlock(&dtrace_lock
);
17199 prov
= probe
->dtpr_provider
;
17201 if (prov
->dtpv_pops
.dtps_getargdesc
== NULL
) {
17203 * There isn't any typed information for this probe.
17204 * Set the argument number to DTRACE_ARGNONE.
17206 desc
.dtargd_ndx
= DTRACE_ARGNONE
;
17208 desc
.dtargd_native
[0] = '\0';
17209 desc
.dtargd_xlate
[0] = '\0';
17210 desc
.dtargd_mapping
= desc
.dtargd_ndx
;
17212 prov
->dtpv_pops
.dtps_getargdesc(prov
->dtpv_arg
,
17213 probe
->dtpr_id
, probe
->dtpr_arg
, &desc
);
17216 lck_mtx_unlock(&mod_lock
);
17217 lck_mtx_unlock(&dtrace_provider_lock
);
17219 if (copyout(&desc
, (void *)arg
, sizeof (desc
)) != 0)
17225 case DTRACEIOC_GO
: {
17226 processorid_t cpuid
;
17227 rval
= dtrace_state_go(state
, &cpuid
);
17232 if (copyout(&cpuid
, (void *)arg
, sizeof (cpuid
)) != 0)
17238 case DTRACEIOC_STOP
: {
17239 processorid_t cpuid
;
17241 lck_mtx_lock(&dtrace_lock
);
17242 rval
= dtrace_state_stop(state
, &cpuid
);
17243 lck_mtx_unlock(&dtrace_lock
);
17248 if (copyout(&cpuid
, (void *)arg
, sizeof (cpuid
)) != 0)
17254 case DTRACEIOC_DOFGET
: {
17255 dof_hdr_t hdr
, *dof
;
17258 if (copyin((void *)arg
, &hdr
, sizeof (hdr
)) != 0)
17261 lck_mtx_lock(&dtrace_lock
);
17262 dof
= dtrace_dof_create(state
);
17263 lck_mtx_unlock(&dtrace_lock
);
17265 len
= MIN(hdr
.dofh_loadsz
, dof
->dofh_loadsz
);
17266 rval
= copyout(dof
, (void *)arg
, len
);
17267 dtrace_dof_destroy(dof
);
17269 return (rval
== 0 ? 0 : EFAULT
);
17272 case DTRACEIOC_AGGSNAP
:
17273 case DTRACEIOC_BUFSNAP
: {
17274 dtrace_bufdesc_t desc
;
17276 dtrace_buffer_t
*buf
;
17278 if (copyin((void *)arg
, &desc
, sizeof (desc
)) != 0)
17281 if (desc
.dtbd_cpu
< 0 || desc
.dtbd_cpu
>= NCPU
)
17284 lck_mtx_lock(&dtrace_lock
);
17286 if (cmd
== DTRACEIOC_BUFSNAP
) {
17287 buf
= &state
->dts_buffer
[desc
.dtbd_cpu
];
17289 buf
= &state
->dts_aggbuffer
[desc
.dtbd_cpu
];
17292 if (buf
->dtb_flags
& (DTRACEBUF_RING
| DTRACEBUF_FILL
)) {
17293 size_t sz
= buf
->dtb_offset
;
17295 if (state
->dts_activity
!= DTRACE_ACTIVITY_STOPPED
) {
17296 lck_mtx_unlock(&dtrace_lock
);
17301 * If this buffer has already been consumed, we're
17302 * going to indicate that there's nothing left here
17305 if (buf
->dtb_flags
& DTRACEBUF_CONSUMED
) {
17306 lck_mtx_unlock(&dtrace_lock
);
17308 desc
.dtbd_size
= 0;
17309 desc
.dtbd_drops
= 0;
17310 desc
.dtbd_errors
= 0;
17311 desc
.dtbd_oldest
= 0;
17312 sz
= sizeof (desc
);
17314 if (copyout(&desc
, (void *)arg
, sz
) != 0)
17321 * If this is a ring buffer that has wrapped, we want
17322 * to copy the whole thing out.
17324 if (buf
->dtb_flags
& DTRACEBUF_WRAPPED
) {
17325 dtrace_buffer_polish(buf
);
17326 sz
= buf
->dtb_size
;
17329 if (copyout(buf
->dtb_tomax
, desc
.dtbd_data
, sz
) != 0) {
17330 lck_mtx_unlock(&dtrace_lock
);
17334 desc
.dtbd_size
= sz
;
17335 desc
.dtbd_drops
= buf
->dtb_drops
;
17336 desc
.dtbd_errors
= buf
->dtb_errors
;
17337 desc
.dtbd_oldest
= buf
->dtb_xamot_offset
;
17339 lck_mtx_unlock(&dtrace_lock
);
17341 if (copyout(&desc
, (void *)arg
, sizeof (desc
)) != 0)
17344 buf
->dtb_flags
|= DTRACEBUF_CONSUMED
;
17349 if (buf
->dtb_tomax
== NULL
) {
17350 ASSERT(buf
->dtb_xamot
== NULL
);
17351 lck_mtx_unlock(&dtrace_lock
);
17355 cached
= buf
->dtb_tomax
;
17356 ASSERT(!(buf
->dtb_flags
& DTRACEBUF_NOSWITCH
));
17358 dtrace_xcall(desc
.dtbd_cpu
,
17359 (dtrace_xcall_t
)dtrace_buffer_switch
, buf
);
17361 state
->dts_errors
+= buf
->dtb_xamot_errors
;
17364 * If the buffers did not actually switch, then the cross call
17365 * did not take place -- presumably because the given CPU is
17366 * not in the ready set. If this is the case, we'll return
17369 if (buf
->dtb_tomax
== cached
) {
17370 ASSERT(buf
->dtb_xamot
!= cached
);
17371 lck_mtx_unlock(&dtrace_lock
);
17375 ASSERT(cached
== buf
->dtb_xamot
);
17378 * We have our snapshot; now copy it out.
17380 if (copyout(buf
->dtb_xamot
, desc
.dtbd_data
,
17381 buf
->dtb_xamot_offset
) != 0) {
17382 lck_mtx_unlock(&dtrace_lock
);
17386 desc
.dtbd_size
= buf
->dtb_xamot_offset
;
17387 desc
.dtbd_drops
= buf
->dtb_xamot_drops
;
17388 desc
.dtbd_errors
= buf
->dtb_xamot_errors
;
17389 desc
.dtbd_oldest
= 0;
17391 lck_mtx_unlock(&dtrace_lock
);
17394 * Finally, copy out the buffer description.
17396 if (copyout(&desc
, (void *)arg
, sizeof (desc
)) != 0)
17402 case DTRACEIOC_CONF
: {
17403 dtrace_conf_t conf
;
17405 bzero(&conf
, sizeof (conf
));
17406 conf
.dtc_difversion
= DIF_VERSION
;
17407 conf
.dtc_difintregs
= DIF_DIR_NREGS
;
17408 conf
.dtc_diftupregs
= DIF_DTR_NREGS
;
17409 conf
.dtc_ctfmodel
= CTF_MODEL_NATIVE
;
17411 if (copyout(&conf
, (void *)arg
, sizeof (conf
)) != 0)
17417 case DTRACEIOC_STATUS
: {
17418 dtrace_status_t stat
;
17419 dtrace_dstate_t
*dstate
;
17424 * See the comment in dtrace_state_deadman() for the reason
17425 * for setting dts_laststatus to INT64_MAX before setting
17426 * it to the correct value.
17428 state
->dts_laststatus
= INT64_MAX
;
17429 dtrace_membar_producer();
17430 state
->dts_laststatus
= dtrace_gethrtime();
17432 bzero(&stat
, sizeof (stat
));
17434 lck_mtx_lock(&dtrace_lock
);
17436 if (state
->dts_activity
== DTRACE_ACTIVITY_INACTIVE
) {
17437 lck_mtx_unlock(&dtrace_lock
);
17441 if (state
->dts_activity
== DTRACE_ACTIVITY_DRAINING
)
17442 stat
.dtst_exiting
= 1;
17444 nerrs
= state
->dts_errors
;
17445 dstate
= &state
->dts_vstate
.dtvs_dynvars
;
17447 for (i
= 0; i
< NCPU
; i
++) {
17448 dtrace_dstate_percpu_t
*dcpu
= &dstate
->dtds_percpu
[i
];
17450 stat
.dtst_dyndrops
+= dcpu
->dtdsc_drops
;
17451 stat
.dtst_dyndrops_dirty
+= dcpu
->dtdsc_dirty_drops
;
17452 stat
.dtst_dyndrops_rinsing
+= dcpu
->dtdsc_rinsing_drops
;
17454 if (state
->dts_buffer
[i
].dtb_flags
& DTRACEBUF_FULL
)
17455 stat
.dtst_filled
++;
17457 nerrs
+= state
->dts_buffer
[i
].dtb_errors
;
17459 for (j
= 0; j
< state
->dts_nspeculations
; j
++) {
17460 dtrace_speculation_t
*spec
;
17461 dtrace_buffer_t
*buf
;
17463 spec
= &state
->dts_speculations
[j
];
17464 buf
= &spec
->dtsp_buffer
[i
];
17465 stat
.dtst_specdrops
+= buf
->dtb_xamot_drops
;
17469 stat
.dtst_specdrops_busy
= state
->dts_speculations_busy
;
17470 stat
.dtst_specdrops_unavail
= state
->dts_speculations_unavail
;
17471 stat
.dtst_stkstroverflows
= state
->dts_stkstroverflows
;
17472 stat
.dtst_dblerrors
= state
->dts_dblerrors
;
17474 (state
->dts_activity
== DTRACE_ACTIVITY_KILLED
);
17475 stat
.dtst_errors
= nerrs
;
17477 lck_mtx_unlock(&dtrace_lock
);
17479 if (copyout(&stat
, (void *)arg
, sizeof (stat
)) != 0)
17485 case DTRACEIOC_FORMAT
: {
17486 dtrace_fmtdesc_t fmt
;
17490 if (copyin((void *)arg
, &fmt
, sizeof (fmt
)) != 0)
17493 lck_mtx_lock(&dtrace_lock
);
17495 if (fmt
.dtfd_format
== 0 ||
17496 fmt
.dtfd_format
> state
->dts_nformats
) {
17497 lck_mtx_unlock(&dtrace_lock
);
17502 * Format strings are allocated contiguously and they are
17503 * never freed; if a format index is less than the number
17504 * of formats, we can assert that the format map is non-NULL
17505 * and that the format for the specified index is non-NULL.
17507 ASSERT(state
->dts_formats
!= NULL
);
17508 str
= state
->dts_formats
[fmt
.dtfd_format
- 1];
17509 ASSERT(str
!= NULL
);
17511 len
= strlen(str
) + 1;
17513 if (len
> fmt
.dtfd_length
) {
17514 fmt
.dtfd_length
= len
;
17516 if (copyout(&fmt
, (void *)arg
, sizeof (fmt
)) != 0) {
17517 lck_mtx_unlock(&dtrace_lock
);
17521 if (copyout(str
, fmt
.dtfd_string
, len
) != 0) {
17522 lck_mtx_unlock(&dtrace_lock
);
17527 lck_mtx_unlock(&dtrace_lock
);
17540 dtrace_ioctl_helper(u_long cmd
, caddr_t arg
, int *rv
)
17544 * Safe to check this outside the dof mode lock
17546 if (dtrace_dof_mode
== DTRACE_DOF_MODE_NEVER
)
17547 return KERN_SUCCESS
;
17550 case DTRACEHIOC_ADDDOF
: {
17551 dof_helper_t
*dhp
= NULL
;
17552 size_t dof_ioctl_data_size
;
17553 dof_ioctl_data_t
* multi_dof
;
17556 user_addr_t user_address
= *(user_addr_t
*)arg
;
17557 uint64_t dof_count
;
17558 int multi_dof_claimed
= 0;
17559 proc_t
* p
= current_proc();
17562 * Read the number of DOF sections being passed in.
17564 if (copyin(user_address
+ offsetof(dof_ioctl_data_t
, dofiod_count
),
17566 sizeof(dof_count
))) {
17567 dtrace_dof_error(NULL
, "failed to copyin dofiod_count");
17572 * Range check the count.
17574 if (dof_count
== 0 || dof_count
> 1024) {
17575 dtrace_dof_error(NULL
, "dofiod_count is not valid");
17580 * Allocate a correctly sized structure and copyin the data.
17582 dof_ioctl_data_size
= DOF_IOCTL_DATA_T_SIZE(dof_count
);
17583 if ((multi_dof
= kmem_alloc(dof_ioctl_data_size
, KM_SLEEP
)) == NULL
)
17586 /* NOTE! We can no longer exit this method via return */
17587 if (copyin(user_address
, multi_dof
, dof_ioctl_data_size
) != 0) {
17588 dtrace_dof_error(NULL
, "failed copyin of dof_ioctl_data_t");
17594 * Check that the count didn't change between the first copyin and the second.
17596 if (multi_dof
->dofiod_count
!= dof_count
) {
17602 * Try to process lazily first.
17604 rval
= dtrace_lazy_dofs_add(p
, multi_dof
, &multi_dof_claimed
);
17607 * If rval is EACCES, we must be non-lazy.
17609 if (rval
== EACCES
) {
17612 * Process each dof_helper_t
17616 dhp
= &multi_dof
->dofiod_helpers
[i
];
17618 dof_hdr_t
*dof
= dtrace_dof_copyin(dhp
->dofhp_dof
, &rval
);
17621 lck_mtx_lock(&dtrace_lock
);
17624 * dtrace_helper_slurp() takes responsibility for the dof --
17625 * it may free it now or it may save it and free it later.
17627 if ((dhp
->dofhp_dof
= (uint64_t)dtrace_helper_slurp(p
, dof
, dhp
)) == -1ULL) {
17631 lck_mtx_unlock(&dtrace_lock
);
17633 } while (++i
< multi_dof
->dofiod_count
&& rval
== 0);
17637 * We need to copyout the multi_dof struct, because it contains
17638 * the generation (unique id) values needed to call DTRACEHIOC_REMOVE
17640 * This could certainly be better optimized.
17642 if (copyout(multi_dof
, user_address
, dof_ioctl_data_size
) != 0) {
17643 dtrace_dof_error(NULL
, "failed copyout of dof_ioctl_data_t");
17644 /* Don't overwrite pre-existing error code */
17645 if (rval
== 0) rval
= EFAULT
;
17650 * If we had to allocate struct memory, free it.
17652 if (multi_dof
!= NULL
&& !multi_dof_claimed
) {
17653 kmem_free(multi_dof
, dof_ioctl_data_size
);
17659 case DTRACEHIOC_REMOVE
: {
17660 int generation
= *(int*)arg
;
17661 proc_t
* p
= current_proc();
17666 int rval
= dtrace_lazy_dofs_remove(p
, generation
);
17669 * EACCES means non-lazy
17671 if (rval
== EACCES
) {
17672 lck_mtx_lock(&dtrace_lock
);
17673 rval
= dtrace_helper_destroygen(p
, generation
);
17674 lck_mtx_unlock(&dtrace_lock
);
17689 dtrace_ioctl(dev_t dev
, u_long cmd
, user_addr_t arg
, int md
, cred_t
*cr
, int *rv
)
17692 minor_t minor
= getminor(dev
);
17693 dtrace_state_t
*state
;
17696 /* Darwin puts Helper on its own major device. */
17698 state
= ddi_get_soft_state(dtrace_softstate
, minor
);
17700 if (state
->dts_anon
) {
17701 ASSERT(dtrace_anon
.dta_state
== NULL
);
17702 state
= state
->dts_anon
;
17706 case DTRACEIOC_PROVIDER
: {
17707 dtrace_providerdesc_t pvd
;
17708 dtrace_provider_t
*pvp
;
17710 if (copyin(arg
, &pvd
, sizeof (pvd
)) != 0)
17713 pvd
.dtvd_name
[DTRACE_PROVNAMELEN
- 1] = '\0';
17714 lck_mtx_lock(&dtrace_provider_lock
);
17716 for (pvp
= dtrace_provider
; pvp
!= NULL
; pvp
= pvp
->dtpv_next
) {
17717 if (strncmp(pvp
->dtpv_name
, pvd
.dtvd_name
, DTRACE_PROVNAMELEN
) == 0)
17721 lck_mtx_unlock(&dtrace_provider_lock
);
17726 bcopy(&pvp
->dtpv_priv
, &pvd
.dtvd_priv
, sizeof (dtrace_ppriv_t
));
17727 bcopy(&pvp
->dtpv_attr
, &pvd
.dtvd_attr
, sizeof (dtrace_pattr_t
));
17728 if (copyout(&pvd
, arg
, sizeof (pvd
)) != 0)
17734 case DTRACEIOC_EPROBE
: {
17735 dtrace_eprobedesc_t epdesc
;
17737 dtrace_action_t
*act
;
17743 if (copyin(arg
, &epdesc
, sizeof (epdesc
)) != 0)
17746 lck_mtx_lock(&dtrace_lock
);
17748 if ((ecb
= dtrace_epid2ecb(state
, epdesc
.dtepd_epid
)) == NULL
) {
17749 lck_mtx_unlock(&dtrace_lock
);
17753 if (ecb
->dte_probe
== NULL
) {
17754 lck_mtx_unlock(&dtrace_lock
);
17758 epdesc
.dtepd_probeid
= ecb
->dte_probe
->dtpr_id
;
17759 epdesc
.dtepd_uarg
= ecb
->dte_uarg
;
17760 epdesc
.dtepd_size
= ecb
->dte_size
;
17762 nrecs
= epdesc
.dtepd_nrecs
;
17763 epdesc
.dtepd_nrecs
= 0;
17764 for (act
= ecb
->dte_action
; act
!= NULL
; act
= act
->dta_next
) {
17765 if (DTRACEACT_ISAGG(act
->dta_kind
) || act
->dta_intuple
)
17768 epdesc
.dtepd_nrecs
++;
17772 * Now that we have the size, we need to allocate a temporary
17773 * buffer in which to store the complete description. We need
17774 * the temporary buffer to be able to drop dtrace_lock()
17775 * across the copyout(), below.
17777 size
= sizeof (dtrace_eprobedesc_t
) +
17778 (epdesc
.dtepd_nrecs
* sizeof (dtrace_recdesc_t
));
17780 buf
= kmem_alloc(size
, KM_SLEEP
);
17781 dest
= (uintptr_t)buf
;
17783 bcopy(&epdesc
, (void *)dest
, sizeof (epdesc
));
17784 dest
+= offsetof(dtrace_eprobedesc_t
, dtepd_rec
[0]);
17786 for (act
= ecb
->dte_action
; act
!= NULL
; act
= act
->dta_next
) {
17787 if (DTRACEACT_ISAGG(act
->dta_kind
) || act
->dta_intuple
)
17793 bcopy(&act
->dta_rec
, (void *)dest
,
17794 sizeof (dtrace_recdesc_t
));
17795 dest
+= sizeof (dtrace_recdesc_t
);
17798 lck_mtx_unlock(&dtrace_lock
);
17800 if (copyout(buf
, arg
, dest
- (uintptr_t)buf
) != 0) {
17801 kmem_free(buf
, size
);
17805 kmem_free(buf
, size
);
17809 case DTRACEIOC_AGGDESC
: {
17810 dtrace_aggdesc_t aggdesc
;
17811 dtrace_action_t
*act
;
17812 dtrace_aggregation_t
*agg
;
17815 dtrace_recdesc_t
*lrec
;
17820 if (copyin(arg
, &aggdesc
, sizeof (aggdesc
)) != 0)
17823 lck_mtx_lock(&dtrace_lock
);
17825 if ((agg
= dtrace_aggid2agg(state
, aggdesc
.dtagd_id
)) == NULL
) {
17826 lck_mtx_unlock(&dtrace_lock
);
17830 aggdesc
.dtagd_epid
= agg
->dtag_ecb
->dte_epid
;
17832 nrecs
= aggdesc
.dtagd_nrecs
;
17833 aggdesc
.dtagd_nrecs
= 0;
17835 offs
= agg
->dtag_base
;
17836 lrec
= &agg
->dtag_action
.dta_rec
;
17837 aggdesc
.dtagd_size
= lrec
->dtrd_offset
+ lrec
->dtrd_size
- offs
;
17839 for (act
= agg
->dtag_first
; ; act
= act
->dta_next
) {
17840 ASSERT(act
->dta_intuple
||
17841 DTRACEACT_ISAGG(act
->dta_kind
));
17844 * If this action has a record size of zero, it
17845 * denotes an argument to the aggregating action.
17846 * Because the presence of this record doesn't (or
17847 * shouldn't) affect the way the data is interpreted,
17848 * we don't copy it out to save user-level the
17849 * confusion of dealing with a zero-length record.
17851 if (act
->dta_rec
.dtrd_size
== 0) {
17852 ASSERT(agg
->dtag_hasarg
);
17856 aggdesc
.dtagd_nrecs
++;
17858 if (act
== &agg
->dtag_action
)
17863 * Now that we have the size, we need to allocate a temporary
17864 * buffer in which to store the complete description. We need
17865 * the temporary buffer to be able to drop dtrace_lock()
17866 * across the copyout(), below.
17868 size
= sizeof (dtrace_aggdesc_t
) +
17869 (aggdesc
.dtagd_nrecs
* sizeof (dtrace_recdesc_t
));
17871 buf
= kmem_alloc(size
, KM_SLEEP
);
17872 dest
= (uintptr_t)buf
;
17874 bcopy(&aggdesc
, (void *)dest
, sizeof (aggdesc
));
17875 dest
+= offsetof(dtrace_aggdesc_t
, dtagd_rec
[0]);
17877 for (act
= agg
->dtag_first
; ; act
= act
->dta_next
) {
17878 dtrace_recdesc_t rec
= act
->dta_rec
;
17881 * See the comment in the above loop for why we pass
17882 * over zero-length records.
17884 if (rec
.dtrd_size
== 0) {
17885 ASSERT(agg
->dtag_hasarg
);
17892 rec
.dtrd_offset
-= offs
;
17893 bcopy(&rec
, (void *)dest
, sizeof (rec
));
17894 dest
+= sizeof (dtrace_recdesc_t
);
17896 if (act
== &agg
->dtag_action
)
17900 lck_mtx_unlock(&dtrace_lock
);
17902 if (copyout(buf
, arg
, dest
- (uintptr_t)buf
) != 0) {
17903 kmem_free(buf
, size
);
17907 kmem_free(buf
, size
);
17911 case DTRACEIOC_ENABLE
: {
17913 dtrace_enabling_t
*enab
= NULL
;
17914 dtrace_vstate_t
*vstate
;
17920 * If a NULL argument has been passed, we take this as our
17921 * cue to reevaluate our enablings.
17924 dtrace_enabling_matchall();
17929 if ((dof
= dtrace_dof_copyin(arg
, &rval
)) == NULL
)
17932 lck_mtx_lock(&cpu_lock
);
17933 lck_mtx_lock(&dtrace_lock
);
17934 vstate
= &state
->dts_vstate
;
17936 if (state
->dts_activity
!= DTRACE_ACTIVITY_INACTIVE
) {
17937 lck_mtx_unlock(&dtrace_lock
);
17938 lck_mtx_unlock(&cpu_lock
);
17939 dtrace_dof_destroy(dof
);
17943 if (dtrace_dof_slurp(dof
, vstate
, cr
, &enab
, 0, B_TRUE
) != 0) {
17944 lck_mtx_unlock(&dtrace_lock
);
17945 lck_mtx_unlock(&cpu_lock
);
17946 dtrace_dof_destroy(dof
);
17950 if ((rval
= dtrace_dof_options(dof
, state
)) != 0) {
17951 dtrace_enabling_destroy(enab
);
17952 lck_mtx_unlock(&dtrace_lock
);
17953 lck_mtx_unlock(&cpu_lock
);
17954 dtrace_dof_destroy(dof
);
17958 if ((err
= dtrace_enabling_match(enab
, rv
)) == 0) {
17959 err
= dtrace_enabling_retain(enab
);
17961 dtrace_enabling_destroy(enab
);
17964 lck_mtx_unlock(&cpu_lock
);
17965 lck_mtx_unlock(&dtrace_lock
);
17966 dtrace_dof_destroy(dof
);
17971 case DTRACEIOC_REPLICATE
: {
17972 dtrace_repldesc_t desc
;
17973 dtrace_probedesc_t
*match
= &desc
.dtrpd_match
;
17974 dtrace_probedesc_t
*create
= &desc
.dtrpd_create
;
17977 if (copyin(arg
, &desc
, sizeof (desc
)) != 0)
17980 match
->dtpd_provider
[DTRACE_PROVNAMELEN
- 1] = '\0';
17981 match
->dtpd_mod
[DTRACE_MODNAMELEN
- 1] = '\0';
17982 match
->dtpd_func
[DTRACE_FUNCNAMELEN
- 1] = '\0';
17983 match
->dtpd_name
[DTRACE_NAMELEN
- 1] = '\0';
17985 create
->dtpd_provider
[DTRACE_PROVNAMELEN
- 1] = '\0';
17986 create
->dtpd_mod
[DTRACE_MODNAMELEN
- 1] = '\0';
17987 create
->dtpd_func
[DTRACE_FUNCNAMELEN
- 1] = '\0';
17988 create
->dtpd_name
[DTRACE_NAMELEN
- 1] = '\0';
17990 lck_mtx_lock(&dtrace_lock
);
17991 err
= dtrace_enabling_replicate(state
, match
, create
);
17992 lck_mtx_unlock(&dtrace_lock
);
17997 case DTRACEIOC_PROBEMATCH
:
17998 case DTRACEIOC_PROBES
: {
17999 dtrace_probe_t
*probe
= NULL
;
18000 dtrace_probedesc_t desc
;
18001 dtrace_probekey_t pkey
;
18008 if (copyin(arg
, &desc
, sizeof (desc
)) != 0)
18011 desc
.dtpd_provider
[DTRACE_PROVNAMELEN
- 1] = '\0';
18012 desc
.dtpd_mod
[DTRACE_MODNAMELEN
- 1] = '\0';
18013 desc
.dtpd_func
[DTRACE_FUNCNAMELEN
- 1] = '\0';
18014 desc
.dtpd_name
[DTRACE_NAMELEN
- 1] = '\0';
18017 * Before we attempt to match this probe, we want to give
18018 * all providers the opportunity to provide it.
18020 if (desc
.dtpd_id
== DTRACE_IDNONE
) {
18021 lck_mtx_lock(&dtrace_provider_lock
);
18022 dtrace_probe_provide(&desc
, NULL
);
18023 lck_mtx_unlock(&dtrace_provider_lock
);
18027 if (cmd
== DTRACEIOC_PROBEMATCH
) {
18028 dtrace_probekey(&desc
, &pkey
);
18029 pkey
.dtpk_id
= DTRACE_IDNONE
;
18032 dtrace_cred2priv(cr
, &priv
, &uid
, &zoneid
);
18034 lck_mtx_lock(&dtrace_lock
);
18036 if (cmd
== DTRACEIOC_PROBEMATCH
) {
18037 /* Quiet compiler warning */
18038 for (i
= desc
.dtpd_id
; i
<= (dtrace_id_t
)dtrace_nprobes
; i
++) {
18039 if ((probe
= dtrace_probes
[i
- 1]) != NULL
&&
18040 (m
= dtrace_match_probe(probe
, &pkey
,
18041 priv
, uid
, zoneid
)) != 0)
18046 lck_mtx_unlock(&dtrace_lock
);
18051 /* Quiet compiler warning */
18052 for (i
= desc
.dtpd_id
; i
<= (dtrace_id_t
)dtrace_nprobes
; i
++) {
18053 if ((probe
= dtrace_probes
[i
- 1]) != NULL
&&
18054 dtrace_match_priv(probe
, priv
, uid
, zoneid
))
18059 if (probe
== NULL
) {
18060 lck_mtx_unlock(&dtrace_lock
);
18064 dtrace_probe_description(probe
, &desc
);
18065 lck_mtx_unlock(&dtrace_lock
);
18067 if (copyout(&desc
, arg
, sizeof (desc
)) != 0)
18073 case DTRACEIOC_PROBEARG
: {
18074 dtrace_argdesc_t desc
;
18075 dtrace_probe_t
*probe
;
18076 dtrace_provider_t
*prov
;
18078 if (copyin(arg
, &desc
, sizeof (desc
)) != 0)
18081 if (desc
.dtargd_id
== DTRACE_IDNONE
)
18084 if (desc
.dtargd_ndx
== DTRACE_ARGNONE
)
18087 lck_mtx_lock(&dtrace_provider_lock
);
18088 lck_mtx_lock(&mod_lock
);
18089 lck_mtx_lock(&dtrace_lock
);
18091 /* Quiet compiler warning */
18092 if (desc
.dtargd_id
> (dtrace_id_t
)dtrace_nprobes
) {
18093 lck_mtx_unlock(&dtrace_lock
);
18094 lck_mtx_unlock(&mod_lock
);
18095 lck_mtx_unlock(&dtrace_provider_lock
);
18099 if ((probe
= dtrace_probes
[desc
.dtargd_id
- 1]) == NULL
) {
18100 lck_mtx_unlock(&dtrace_lock
);
18101 lck_mtx_unlock(&mod_lock
);
18102 lck_mtx_unlock(&dtrace_provider_lock
);
18106 lck_mtx_unlock(&dtrace_lock
);
18108 prov
= probe
->dtpr_provider
;
18110 if (prov
->dtpv_pops
.dtps_getargdesc
== NULL
) {
18112 * There isn't any typed information for this probe.
18113 * Set the argument number to DTRACE_ARGNONE.
18115 desc
.dtargd_ndx
= DTRACE_ARGNONE
;
18117 desc
.dtargd_native
[0] = '\0';
18118 desc
.dtargd_xlate
[0] = '\0';
18119 desc
.dtargd_mapping
= desc
.dtargd_ndx
;
18121 prov
->dtpv_pops
.dtps_getargdesc(prov
->dtpv_arg
,
18122 probe
->dtpr_id
, probe
->dtpr_arg
, &desc
);
18125 lck_mtx_unlock(&mod_lock
);
18126 lck_mtx_unlock(&dtrace_provider_lock
);
18128 if (copyout(&desc
, arg
, sizeof (desc
)) != 0)
18134 case DTRACEIOC_GO
: {
18135 processorid_t cpuid
;
18136 rval
= dtrace_state_go(state
, &cpuid
);
18141 if (copyout(&cpuid
, arg
, sizeof (cpuid
)) != 0)
18147 case DTRACEIOC_STOP
: {
18148 processorid_t cpuid
;
18150 lck_mtx_lock(&dtrace_lock
);
18151 rval
= dtrace_state_stop(state
, &cpuid
);
18152 lck_mtx_unlock(&dtrace_lock
);
18157 if (copyout(&cpuid
, arg
, sizeof (cpuid
)) != 0)
18163 case DTRACEIOC_DOFGET
: {
18164 dof_hdr_t hdr
, *dof
;
18167 if (copyin(arg
, &hdr
, sizeof (hdr
)) != 0)
18170 lck_mtx_lock(&dtrace_lock
);
18171 dof
= dtrace_dof_create(state
);
18172 lck_mtx_unlock(&dtrace_lock
);
18174 len
= MIN(hdr
.dofh_loadsz
, dof
->dofh_loadsz
);
18175 rval
= copyout(dof
, arg
, len
);
18176 dtrace_dof_destroy(dof
);
18178 return (rval
== 0 ? 0 : EFAULT
);
18181 case DTRACEIOC_AGGSNAP
:
18182 case DTRACEIOC_BUFSNAP
: {
18183 dtrace_bufdesc_t desc
;
18185 dtrace_buffer_t
*buf
;
18187 if (copyin(arg
, &desc
, sizeof (desc
)) != 0)
18190 if ((int)desc
.dtbd_cpu
< 0 || desc
.dtbd_cpu
>= NCPU
)
18193 lck_mtx_lock(&dtrace_lock
);
18195 if (cmd
== DTRACEIOC_BUFSNAP
) {
18196 buf
= &state
->dts_buffer
[desc
.dtbd_cpu
];
18198 buf
= &state
->dts_aggbuffer
[desc
.dtbd_cpu
];
18201 if (buf
->dtb_flags
& (DTRACEBUF_RING
| DTRACEBUF_FILL
)) {
18202 size_t sz
= buf
->dtb_offset
;
18204 if (state
->dts_activity
!= DTRACE_ACTIVITY_STOPPED
) {
18205 lck_mtx_unlock(&dtrace_lock
);
18210 * If this buffer has already been consumed, we're
18211 * going to indicate that there's nothing left here
18214 if (buf
->dtb_flags
& DTRACEBUF_CONSUMED
) {
18215 lck_mtx_unlock(&dtrace_lock
);
18217 desc
.dtbd_size
= 0;
18218 desc
.dtbd_drops
= 0;
18219 desc
.dtbd_errors
= 0;
18220 desc
.dtbd_oldest
= 0;
18221 sz
= sizeof (desc
);
18223 if (copyout(&desc
, arg
, sz
) != 0)
18230 * If this is a ring buffer that has wrapped, we want
18231 * to copy the whole thing out.
18233 if (buf
->dtb_flags
& DTRACEBUF_WRAPPED
) {
18234 dtrace_buffer_polish(buf
);
18235 sz
= buf
->dtb_size
;
18238 if (copyout(buf
->dtb_tomax
, (user_addr_t
)desc
.dtbd_data
, sz
) != 0) {
18239 lck_mtx_unlock(&dtrace_lock
);
18243 desc
.dtbd_size
= sz
;
18244 desc
.dtbd_drops
= buf
->dtb_drops
;
18245 desc
.dtbd_errors
= buf
->dtb_errors
;
18246 desc
.dtbd_oldest
= buf
->dtb_xamot_offset
;
18248 lck_mtx_unlock(&dtrace_lock
);
18250 if (copyout(&desc
, arg
, sizeof (desc
)) != 0)
18253 buf
->dtb_flags
|= DTRACEBUF_CONSUMED
;
18258 if (buf
->dtb_tomax
== NULL
) {
18259 ASSERT(buf
->dtb_xamot
== NULL
);
18260 lck_mtx_unlock(&dtrace_lock
);
18264 cached
= buf
->dtb_tomax
;
18265 ASSERT(!(buf
->dtb_flags
& DTRACEBUF_NOSWITCH
));
18267 dtrace_xcall(desc
.dtbd_cpu
,
18268 (dtrace_xcall_t
)dtrace_buffer_switch
, buf
);
18270 state
->dts_errors
+= buf
->dtb_xamot_errors
;
18273 * If the buffers did not actually switch, then the cross call
18274 * did not take place -- presumably because the given CPU is
18275 * not in the ready set. If this is the case, we'll return
18278 if (buf
->dtb_tomax
== cached
) {
18279 ASSERT(buf
->dtb_xamot
!= cached
);
18280 lck_mtx_unlock(&dtrace_lock
);
18284 ASSERT(cached
== buf
->dtb_xamot
);
18287 * We have our snapshot; now copy it out.
18289 if (copyout(buf
->dtb_xamot
, (user_addr_t
)desc
.dtbd_data
,
18290 buf
->dtb_xamot_offset
) != 0) {
18291 lck_mtx_unlock(&dtrace_lock
);
18295 desc
.dtbd_size
= buf
->dtb_xamot_offset
;
18296 desc
.dtbd_drops
= buf
->dtb_xamot_drops
;
18297 desc
.dtbd_errors
= buf
->dtb_xamot_errors
;
18298 desc
.dtbd_oldest
= 0;
18300 lck_mtx_unlock(&dtrace_lock
);
18303 * Finally, copy out the buffer description.
18305 if (copyout(&desc
, arg
, sizeof (desc
)) != 0)
18311 case DTRACEIOC_CONF
: {
18312 dtrace_conf_t conf
;
18314 bzero(&conf
, sizeof (conf
));
18315 conf
.dtc_difversion
= DIF_VERSION
;
18316 conf
.dtc_difintregs
= DIF_DIR_NREGS
;
18317 conf
.dtc_diftupregs
= DIF_DTR_NREGS
;
18318 conf
.dtc_ctfmodel
= CTF_MODEL_NATIVE
;
18320 if (copyout(&conf
, arg
, sizeof (conf
)) != 0)
18326 case DTRACEIOC_STATUS
: {
18327 dtrace_status_t stat
;
18328 dtrace_dstate_t
*dstate
;
18333 * See the comment in dtrace_state_deadman() for the reason
18334 * for setting dts_laststatus to INT64_MAX before setting
18335 * it to the correct value.
18337 state
->dts_laststatus
= INT64_MAX
;
18338 dtrace_membar_producer();
18339 state
->dts_laststatus
= dtrace_gethrtime();
18341 bzero(&stat
, sizeof (stat
));
18343 lck_mtx_lock(&dtrace_lock
);
18345 if (state
->dts_activity
== DTRACE_ACTIVITY_INACTIVE
) {
18346 lck_mtx_unlock(&dtrace_lock
);
18350 if (state
->dts_activity
== DTRACE_ACTIVITY_DRAINING
)
18351 stat
.dtst_exiting
= 1;
18353 nerrs
= state
->dts_errors
;
18354 dstate
= &state
->dts_vstate
.dtvs_dynvars
;
18356 for (i
= 0; i
< (int)NCPU
; i
++) {
18357 dtrace_dstate_percpu_t
*dcpu
= &dstate
->dtds_percpu
[i
];
18359 stat
.dtst_dyndrops
+= dcpu
->dtdsc_drops
;
18360 stat
.dtst_dyndrops_dirty
+= dcpu
->dtdsc_dirty_drops
;
18361 stat
.dtst_dyndrops_rinsing
+= dcpu
->dtdsc_rinsing_drops
;
18363 if (state
->dts_buffer
[i
].dtb_flags
& DTRACEBUF_FULL
)
18364 stat
.dtst_filled
++;
18366 nerrs
+= state
->dts_buffer
[i
].dtb_errors
;
18368 for (j
= 0; j
< state
->dts_nspeculations
; j
++) {
18369 dtrace_speculation_t
*spec
;
18370 dtrace_buffer_t
*buf
;
18372 spec
= &state
->dts_speculations
[j
];
18373 buf
= &spec
->dtsp_buffer
[i
];
18374 stat
.dtst_specdrops
+= buf
->dtb_xamot_drops
;
18378 stat
.dtst_specdrops_busy
= state
->dts_speculations_busy
;
18379 stat
.dtst_specdrops_unavail
= state
->dts_speculations_unavail
;
18380 stat
.dtst_stkstroverflows
= state
->dts_stkstroverflows
;
18381 stat
.dtst_dblerrors
= state
->dts_dblerrors
;
18383 (state
->dts_activity
== DTRACE_ACTIVITY_KILLED
);
18384 stat
.dtst_errors
= nerrs
;
18386 lck_mtx_unlock(&dtrace_lock
);
18388 if (copyout(&stat
, arg
, sizeof (stat
)) != 0)
18394 case DTRACEIOC_FORMAT
: {
18395 dtrace_fmtdesc_t fmt
;
18399 if (copyin(arg
, &fmt
, sizeof (fmt
)) != 0)
18402 lck_mtx_lock(&dtrace_lock
);
18404 if (fmt
.dtfd_format
== 0 ||
18405 fmt
.dtfd_format
> state
->dts_nformats
) {
18406 lck_mtx_unlock(&dtrace_lock
);
18411 * Format strings are allocated contiguously and they are
18412 * never freed; if a format index is less than the number
18413 * of formats, we can assert that the format map is non-NULL
18414 * and that the format for the specified index is non-NULL.
18416 ASSERT(state
->dts_formats
!= NULL
);
18417 str
= state
->dts_formats
[fmt
.dtfd_format
- 1];
18418 ASSERT(str
!= NULL
);
18420 len
= strlen(str
) + 1;
18422 if (len
> fmt
.dtfd_length
) {
18423 fmt
.dtfd_length
= len
;
18425 if (copyout(&fmt
, arg
, sizeof (fmt
)) != 0) {
18426 lck_mtx_unlock(&dtrace_lock
);
18430 if (copyout(str
, (user_addr_t
)fmt
.dtfd_string
, len
) != 0) {
18431 lck_mtx_unlock(&dtrace_lock
);
18436 lck_mtx_unlock(&dtrace_lock
);
18446 #endif /* __APPLE__ */
18448 #if !defined(__APPLE__)
18451 dtrace_detach(dev_info_t
*dip
, ddi_detach_cmd_t cmd
)
18453 dtrace_state_t
*state
;
18460 return (DDI_SUCCESS
);
18463 return (DDI_FAILURE
);
18466 lck_mtx_lock(&cpu_lock
);
18467 lck_mtx_lock(&dtrace_provider_lock
);
18468 lck_mtx_lock(&dtrace_lock
);
18470 ASSERT(dtrace_opens
== 0);
18472 if (dtrace_helpers
> 0) {
18473 lck_mtx_unlock(&dtrace_provider_lock
);
18474 lck_mtx_unlock(&dtrace_lock
);
18475 lck_mtx_unlock(&cpu_lock
);
18476 return (DDI_FAILURE
);
18479 if (dtrace_unregister((dtrace_provider_id_t
)dtrace_provider
) != 0) {
18480 lck_mtx_unlock(&dtrace_provider_lock
);
18481 lck_mtx_unlock(&dtrace_lock
);
18482 lck_mtx_unlock(&cpu_lock
);
18483 return (DDI_FAILURE
);
18486 dtrace_provider
= NULL
;
18488 if ((state
= dtrace_anon_grab()) != NULL
) {
18490 * If there were ECBs on this state, the provider should
18491 * have not been allowed to detach; assert that there is
18494 ASSERT(state
->dts_necbs
== 0);
18495 dtrace_state_destroy(state
);
18498 * If we're being detached with anonymous state, we need to
18499 * indicate to the kernel debugger that DTrace is now inactive.
18501 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE
);
18504 bzero(&dtrace_anon
, sizeof (dtrace_anon_t
));
18505 unregister_cpu_setup_func((cpu_setup_func_t
*)dtrace_cpu_setup
, NULL
);
18506 dtrace_cpu_init
= NULL
;
18507 dtrace_helpers_cleanup
= NULL
;
18508 dtrace_helpers_fork
= NULL
;
18509 dtrace_cpustart_init
= NULL
;
18510 dtrace_cpustart_fini
= NULL
;
18511 dtrace_debugger_init
= NULL
;
18512 dtrace_debugger_fini
= NULL
;
18513 dtrace_kreloc_init
= NULL
;
18514 dtrace_kreloc_fini
= NULL
;
18515 dtrace_modload
= NULL
;
18516 dtrace_modunload
= NULL
;
18518 lck_mtx_unlock(&cpu_lock
);
18520 if (dtrace_helptrace_enabled
) {
18521 kmem_free(dtrace_helptrace_buffer
, dtrace_helptrace_bufsize
);
18522 dtrace_helptrace_buffer
= NULL
;
18525 kmem_free(dtrace_probes
, dtrace_nprobes
* sizeof (dtrace_probe_t
*));
18526 dtrace_probes
= NULL
;
18527 dtrace_nprobes
= 0;
18529 dtrace_hash_destroy(dtrace_bymod
);
18530 dtrace_hash_destroy(dtrace_byfunc
);
18531 dtrace_hash_destroy(dtrace_byname
);
18532 dtrace_bymod
= NULL
;
18533 dtrace_byfunc
= NULL
;
18534 dtrace_byname
= NULL
;
18536 kmem_cache_destroy(dtrace_state_cache
);
18537 vmem_destroy(dtrace_minor
);
18538 vmem_destroy(dtrace_arena
);
18540 if (dtrace_toxrange
!= NULL
) {
18541 kmem_free(dtrace_toxrange
,
18542 dtrace_toxranges_max
* sizeof (dtrace_toxrange_t
));
18543 dtrace_toxrange
= NULL
;
18544 dtrace_toxranges
= 0;
18545 dtrace_toxranges_max
= 0;
18548 ddi_remove_minor_node(dtrace_devi
, NULL
);
18549 dtrace_devi
= NULL
;
18551 ddi_soft_state_fini(&dtrace_softstate
);
18553 ASSERT(dtrace_vtime_references
== 0);
18554 ASSERT(dtrace_opens
== 0);
18555 ASSERT(dtrace_retained
== NULL
);
18557 lck_mtx_unlock(&dtrace_lock
);
18558 lck_mtx_unlock(&dtrace_provider_lock
);
18561 * We don't destroy the task queue until after we have dropped our
18562 * locks (taskq_destroy() may block on running tasks). To prevent
18563 * attempting to do work after we have effectively detached but before
18564 * the task queue has been destroyed, all tasks dispatched via the
18565 * task queue must check that DTrace is still attached before
18566 * performing any operation.
18568 taskq_destroy(dtrace_taskq
);
18569 dtrace_taskq
= NULL
;
18571 return (DDI_SUCCESS
);
18576 dtrace_info(dev_info_t
*dip
, ddi_info_cmd_t infocmd
, void *arg
, void **result
)
18581 case DDI_INFO_DEVT2DEVINFO
:
18582 *result
= (void *)dtrace_devi
;
18583 error
= DDI_SUCCESS
;
18585 case DDI_INFO_DEVT2INSTANCE
:
18586 *result
= (void *)0;
18587 error
= DDI_SUCCESS
;
18590 error
= DDI_FAILURE
;
18595 static struct cb_ops dtrace_cb_ops
= {
18596 dtrace_open
, /* open */
18597 dtrace_close
, /* close */
18598 nulldev
, /* strategy */
18599 nulldev
, /* print */
18603 dtrace_ioctl
, /* ioctl */
18604 nodev
, /* devmap */
18606 nodev
, /* segmap */
18607 nochpoll
, /* poll */
18608 ddi_prop_op
, /* cb_prop_op */
18610 D_NEW
| D_MP
/* Driver compatibility flag */
18613 static struct dev_ops dtrace_ops
= {
18614 DEVO_REV
, /* devo_rev */
18616 dtrace_info
, /* get_dev_info */
18617 nulldev
, /* identify */
18618 nulldev
, /* probe */
18619 dtrace_attach
, /* attach */
18620 dtrace_detach
, /* detach */
18622 &dtrace_cb_ops
, /* driver operations */
18623 NULL
, /* bus operations */
18624 nodev
/* dev power */
18627 static struct modldrv modldrv
= {
18628 &mod_driverops
, /* module type (this is a pseudo driver) */
18629 "Dynamic Tracing", /* name of module */
18630 &dtrace_ops
, /* driver ops */
18633 static struct modlinkage modlinkage
= {
18642 return (mod_install(&modlinkage
));
18646 _info(struct modinfo
*modinfop
)
18648 return (mod_info(&modlinkage
, modinfop
));
18654 return (mod_remove(&modlinkage
));
18656 #else /* Darwin BSD driver model. */
18658 d_open_t _dtrace_open
, helper_open
;
18659 d_close_t _dtrace_close
, helper_close
;
18660 d_ioctl_t _dtrace_ioctl
, helper_ioctl
;
18663 _dtrace_open(dev_t dev
, int flags
, int devtype
, struct proc
*p
)
18666 dev_t locdev
= dev
;
18668 return dtrace_open( &locdev
, flags
, devtype
, CRED());
18672 helper_open(dev_t dev
, int flags
, int devtype
, struct proc
*p
)
18674 #pragma unused(dev,flags,devtype,p)
18679 _dtrace_close(dev_t dev
, int flags
, int devtype
, struct proc
*p
)
18682 return dtrace_close( dev
, flags
, devtype
, CRED());
18686 helper_close(dev_t dev
, int flags
, int devtype
, struct proc
*p
)
18688 #pragma unused(dev,flags,devtype,p)
18693 _dtrace_ioctl(dev_t dev
, u_long cmd
, caddr_t data
, int fflag
, struct proc
*p
)
18697 user_addr_t uaddrp
;
18699 if (proc_is64bit(p
))
18700 uaddrp
= *(user_addr_t
*)data
;
18702 uaddrp
= (user_addr_t
) *(uint32_t *)data
;
18704 err
= dtrace_ioctl(dev
, cmd
, uaddrp
, fflag
, CRED(), &rv
);
18706 /* Darwin's BSD ioctls only return -1 or zero. Overload errno to mimic Solaris. 20 bits suffice. */
18708 ASSERT( (err
& 0xfffff000) == 0 );
18709 return (err
& 0xfff); /* ioctl will return -1 and will set errno to an error code < 4096 */
18710 } else if (rv
!= 0) {
18711 ASSERT( (rv
& 0xfff00000) == 0 );
18712 return (((rv
& 0xfffff) << 12)); /* ioctl will return -1 and will set errno to a value >= 4096 */
18718 helper_ioctl(dev_t dev
, u_long cmd
, caddr_t data
, int fflag
, struct proc
*p
)
18720 #pragma unused(dev,fflag,p)
18723 err
= dtrace_ioctl_helper(cmd
, data
, &rv
);
18724 /* Darwin's BSD ioctls only return -1 or zero. Overload errno to mimic Solaris. 20 bits suffice. */
18726 ASSERT( (err
& 0xfffff000) == 0 );
18727 return (err
& 0xfff); /* ioctl will return -1 and will set errno to an error code < 4096 */
18728 } else if (rv
!= 0) {
18729 ASSERT( (rv
& 0xfff00000) == 0 );
18730 return (((rv
& 0xfffff) << 12)); /* ioctl will return -1 and will set errno to a value >= 4096 */
18735 #define HELPER_MAJOR -24 /* let the kernel pick the device number */
18738 * A struct describing which functions will get invoked for certain
18741 static struct cdevsw helper_cdevsw
=
18743 helper_open
, /* open */
18744 helper_close
, /* close */
18745 eno_rdwrt
, /* read */
18746 eno_rdwrt
, /* write */
18747 helper_ioctl
, /* ioctl */
18748 (stop_fcn_t
*)nulldev
, /* stop */
18749 (reset_fcn_t
*)nulldev
, /* reset */
18751 eno_select
, /* select */
18752 eno_mmap
, /* mmap */
18753 eno_strat
, /* strategy */
18754 eno_getc
, /* getc */
18755 eno_putc
, /* putc */
18759 static int helper_majdevno
= 0;
18761 static int gDTraceInited
= 0;
18764 helper_init( void )
18767 * Once the "helper" is initialized, it can take ioctl calls that use locks
18768 * and zones initialized in dtrace_init. Make certain dtrace_init was called
18772 if (!gDTraceInited
) {
18773 panic("helper_init before dtrace_init\n");
18776 if (0 >= helper_majdevno
)
18778 helper_majdevno
= cdevsw_add(HELPER_MAJOR
, &helper_cdevsw
);
18780 if (helper_majdevno
< 0) {
18781 printf("helper_init: failed to allocate a major number!\n");
18785 if (NULL
== devfs_make_node( makedev(helper_majdevno
, 0), DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0666,
18786 DTRACEMNR_HELPER
, 0 )) {
18787 printf("dtrace_init: failed to devfs_make_node for helper!\n");
18791 panic("helper_init: called twice!\n");
18794 #undef HELPER_MAJOR
18797 * Called with DEVFS_LOCK held, so vmem_alloc's underlying blist structures are protected.
18800 dtrace_clone_func(dev_t dev
, int action
)
18802 #pragma unused(dev)
18804 if (action
== DEVFS_CLONE_ALLOC
) {
18805 if (NULL
== dtrace_minor
) /* Arena not created yet!?! */
18809 * Propose a minor number, namely the next number that vmem_alloc() will return.
18810 * Immediately put it back in play by calling vmem_free(). FIXME.
18812 int ret
= (int)(uintptr_t)vmem_alloc(dtrace_minor
, 1, VM_BESTFIT
| VM_SLEEP
);
18814 vmem_free(dtrace_minor
, (void *)(uintptr_t)ret
, 1);
18819 else if (action
== DEVFS_CLONE_FREE
) {
18825 #define DTRACE_MAJOR -24 /* let the kernel pick the device number */
18827 static struct cdevsw dtrace_cdevsw
=
18829 _dtrace_open
, /* open */
18830 _dtrace_close
, /* close */
18831 eno_rdwrt
, /* read */
18832 eno_rdwrt
, /* write */
18833 _dtrace_ioctl
, /* ioctl */
18834 (stop_fcn_t
*)nulldev
, /* stop */
18835 (reset_fcn_t
*)nulldev
, /* reset */
18837 eno_select
, /* select */
18838 eno_mmap
, /* mmap */
18839 eno_strat
, /* strategy */
18840 eno_getc
, /* getc */
18841 eno_putc
, /* putc */
18845 lck_attr_t
* dtrace_lck_attr
;
18846 lck_grp_attr_t
* dtrace_lck_grp_attr
;
18847 lck_grp_t
* dtrace_lck_grp
;
18849 static int gMajDevNo
;
18852 dtrace_init( void )
18854 if (0 == gDTraceInited
) {
18855 int i
, ncpu
= NCPU
;
18857 gMajDevNo
= cdevsw_add(DTRACE_MAJOR
, &dtrace_cdevsw
);
18859 if (gMajDevNo
< 0) {
18860 printf("dtrace_init: failed to allocate a major number!\n");
18865 if (NULL
== devfs_make_node_clone( makedev(gMajDevNo
, 0), DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0666,
18866 dtrace_clone_func
, DTRACEMNR_DTRACE
, 0 )) {
18867 printf("dtrace_init: failed to devfs_make_node_clone for dtrace!\n");
18872 #if defined(DTRACE_MEMORY_ZONES)
18874 * Initialize the dtrace kalloc-emulation zones.
18876 dtrace_alloc_init();
18877 #endif /* DTRACE_MEMORY_ZONES */
18880 * Allocate the dtrace_probe_t zone
18882 dtrace_probe_t_zone
= zinit(sizeof(dtrace_probe_t
),
18883 1024 * sizeof(dtrace_probe_t
),
18884 sizeof(dtrace_probe_t
),
18885 "dtrace.dtrace_probe_t");
18888 * Create the dtrace lock group and attrs.
18890 dtrace_lck_attr
= lck_attr_alloc_init();
18891 dtrace_lck_grp_attr
= lck_grp_attr_alloc_init();
18892 dtrace_lck_grp
= lck_grp_alloc_init("dtrace", dtrace_lck_grp_attr
);
18895 * We have to initialize all locks explicitly
18897 lck_mtx_init(&dtrace_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
18898 lck_mtx_init(&dtrace_provider_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
18899 lck_mtx_init(&dtrace_meta_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
18901 lck_mtx_init(&dtrace_errlock
, dtrace_lck_grp
, dtrace_lck_attr
);
18903 lck_rw_init(&dtrace_dof_mode_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
18906 * The cpu_core structure consists of per-CPU state available in any context.
18907 * On some architectures, this may mean that the page(s) containing the
18908 * NCPU-sized array of cpu_core structures must be locked in the TLB -- it
18909 * is up to the platform to assure that this is performed properly. Note that
18910 * the structure is sized to avoid false sharing.
18912 lck_mtx_init(&cpu_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
18913 lck_mtx_init(&mod_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
18915 cpu_core
= (cpu_core_t
*)kmem_zalloc( ncpu
* sizeof(cpu_core_t
), KM_SLEEP
);
18916 for (i
= 0; i
< ncpu
; ++i
) {
18917 lck_mtx_init(&cpu_core
[i
].cpuc_pid_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
18920 cpu_list
= (cpu_t
*)kmem_zalloc( ncpu
* sizeof(cpu_t
), KM_SLEEP
);
18921 for (i
= 0; i
< ncpu
; ++i
) {
18922 cpu_list
[i
].cpu_id
= (processorid_t
)i
;
18923 cpu_list
[i
].cpu_next
= &(cpu_list
[(i
+1) % ncpu
]);
18924 lck_rw_init(&cpu_list
[i
].cpu_ft_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
18927 lck_mtx_lock(&cpu_lock
);
18928 for (i
= 0; i
< ncpu
; ++i
)
18929 /* FIXME: track CPU configuration a la CHUD Processor Pref Pane. */
18930 dtrace_cpu_setup_initial( (processorid_t
)i
); /* In lieu of register_cpu_setup_func() callback */
18931 lck_mtx_unlock(&cpu_lock
);
18933 (void)dtrace_abs_to_nano(0LL); /* Force once only call to clock_timebase_info (which can take a lock) */
18936 * See dtrace_impl.h for a description of dof modes.
18937 * The default is lazy dof.
18939 * FIXME: Warn if state is LAZY_OFF? It won't break anything, but
18940 * makes no sense...
18942 if (!PE_parse_boot_argn("dtrace_dof_mode", &dtrace_dof_mode
, sizeof (dtrace_dof_mode
))) {
18943 dtrace_dof_mode
= DTRACE_DOF_MODE_LAZY_ON
;
18947 * Sanity check of dof mode value.
18949 switch (dtrace_dof_mode
) {
18950 case DTRACE_DOF_MODE_NEVER
:
18951 case DTRACE_DOF_MODE_LAZY_ON
:
18952 /* valid modes, but nothing else we need to do */
18955 case DTRACE_DOF_MODE_LAZY_OFF
:
18956 case DTRACE_DOF_MODE_NON_LAZY
:
18957 /* Cannot wait for a dtrace_open to init fasttrap */
18962 /* Invalid, clamp to non lazy */
18963 dtrace_dof_mode
= DTRACE_DOF_MODE_NON_LAZY
;
18971 panic("dtrace_init: called twice!\n");
18975 dtrace_postinit(void)
18978 * Called from bsd_init after all provider's *_init() routines have been
18979 * run. That way, anonymous DOF enabled under dtrace_attach() is safe
18982 dtrace_attach( (dev_info_t
*)(uintptr_t)makedev(gMajDevNo
, 0), 0 ); /* Punning a dev_t to a dev_info_t* */
18984 #undef DTRACE_MAJOR
18987 * Routines used to register interest in cpu's being added to or removed
18991 register_cpu_setup_func(cpu_setup_func_t
*ignore1
, void *ignore2
)
18993 #pragma unused(ignore1,ignore2)
18997 unregister_cpu_setup_func(cpu_setup_func_t
*ignore1
, void *ignore2
)
18999 #pragma unused(ignore1,ignore2)
19001 #endif /* __APPLE__ */