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 * Portions Copyright (c) 2011, Joyent, Inc. All rights reserved.
24 * Portions Copyright (c) 2012 by Delphix. All rights reserved.
28 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
29 * Use is subject to license terms.
32 /* #pragma ident "@(#)dtrace.c 1.65 08/07/02 SMI" */
35 * DTrace - Dynamic Tracing for Solaris
37 * This is the implementation of the Solaris Dynamic Tracing framework
38 * (DTrace). The user-visible interface to DTrace is described at length in
39 * the "Solaris Dynamic Tracing Guide". The interfaces between the libdtrace
40 * library, the in-kernel DTrace framework, and the DTrace providers are
41 * described in the block comments in the <sys/dtrace.h> header file. The
42 * internal architecture of DTrace is described in the block comments in the
43 * <sys/dtrace_impl.h> header file. The comments contained within the DTrace
44 * implementation very much assume mastery of all of these sources; if one has
45 * an unanswered question about the implementation, one should consult them
48 * The functions here are ordered roughly as follows:
50 * - Probe context functions
51 * - Probe hashing functions
52 * - Non-probe context utility functions
53 * - Matching functions
54 * - Provider-to-Framework API functions
55 * - Probe management functions
56 * - DIF object functions
58 * - Predicate functions
61 * - Enabling functions
63 * - Anonymous enabling functions
64 * - Consumer state functions
67 * - Driver cookbook functions
69 * Each group of functions begins with a block comment labelled the "DTrace
70 * [Group] Functions", allowing one to find each block by searching forward
71 * on capital-f functions.
73 #include <sys/errno.h>
74 #include <sys/types.h>
77 #include <sys/systm.h>
78 #include <sys/dtrace_impl.h>
79 #include <sys/param.h>
80 #include <sys/proc_internal.h>
81 #include <sys/ioctl.h>
82 #include <sys/fcntl.h>
83 #include <miscfs/devfs/devfs.h>
84 #include <sys/malloc.h>
85 #include <sys/kernel_types.h>
86 #include <sys/proc_internal.h>
87 #include <sys/uio_internal.h>
88 #include <sys/kauth.h>
91 #include <mach/exception_types.h>
92 #include <sys/signalvar.h>
93 #include <mach/task.h>
94 #include <kern/zalloc.h>
96 #include <kern/task.h>
97 #include <netinet/in.h>
99 #include <kern/cpu_data.h>
100 extern uint32_t pmap_find_phys(void *, uint64_t);
101 extern boolean_t
pmap_valid_page(uint32_t);
102 extern void OSKextRegisterKextsWithDTrace(void);
103 extern kmod_info_t g_kernel_kmod_info
;
105 /* Solaris proc_t is the struct. Darwin's proc_t is a pointer to it. */
106 #define proc_t struct proc /* Steer clear of the Darwin typedef for proc_t */
108 #define t_predcache t_dtrace_predcache /* Cosmetic. Helps readability of thread.h */
110 extern void dtrace_suspend(void);
111 extern void dtrace_resume(void);
112 extern void dtrace_init(void);
113 extern void helper_init(void);
114 extern void fasttrap_init(void);
115 extern void dtrace_lazy_dofs_duplicate(proc_t
*, proc_t
*);
116 extern void dtrace_lazy_dofs_destroy(proc_t
*);
117 extern void dtrace_postinit(void);
119 #include "../../../osfmk/chud/chud_dtrace.h"
121 extern kern_return_t chudxnu_dtrace_callback
122 (uint64_t selector
, uint64_t *args
, uint32_t count
);
124 /* Import this function to retrieve the physical memory. */
125 extern int kernel_sysctlbyname(const char *name
, void *oldp
,
126 size_t *oldlenp
, void *newp
, size_t newlen
);
129 * DTrace Tunable Variables
131 * The following variables may be dynamically tuned by using sysctl(8), the
132 * variables being stored in the kern.dtrace namespace. For example:
133 * sysctl kern.dtrace.dof_maxsize = 1048575 # 1M
135 * In general, the only variables that one should be tuning this way are those
136 * that affect system-wide DTrace behavior, and for which the default behavior
137 * is undesirable. Most of these variables are tunable on a per-consumer
138 * basis using DTrace options, and need not be tuned on a system-wide basis.
139 * When tuning these variables, avoid pathological values; while some attempt
140 * is made to verify the integrity of these variables, they are not considered
141 * part of the supported interface to DTrace, and they are therefore not
142 * checked comprehensively.
144 uint64_t dtrace_buffer_memory_maxsize
= 0; /* initialized in dtrace_init */
145 uint64_t dtrace_buffer_memory_inuse
= 0;
146 int dtrace_destructive_disallow
= 0;
147 dtrace_optval_t dtrace_nonroot_maxsize
= (16 * 1024 * 1024);
148 size_t dtrace_difo_maxsize
= (256 * 1024);
149 dtrace_optval_t dtrace_dof_maxsize
= (384 * 1024);
150 size_t dtrace_global_maxsize
= (16 * 1024);
151 size_t dtrace_actions_max
= (16 * 1024);
152 size_t dtrace_retain_max
= 1024;
153 dtrace_optval_t dtrace_helper_actions_max
= 32;
154 dtrace_optval_t dtrace_helper_providers_max
= 64;
155 dtrace_optval_t dtrace_dstate_defsize
= (1 * 1024 * 1024);
156 size_t dtrace_strsize_default
= 256;
157 dtrace_optval_t dtrace_cleanrate_default
= 990099000; /* 1.1 hz */
158 dtrace_optval_t dtrace_cleanrate_min
= 20000000; /* 50 hz */
159 dtrace_optval_t dtrace_cleanrate_max
= (uint64_t)60 * NANOSEC
; /* 1/minute */
160 dtrace_optval_t dtrace_aggrate_default
= NANOSEC
; /* 1 hz */
161 dtrace_optval_t dtrace_statusrate_default
= NANOSEC
; /* 1 hz */
162 dtrace_optval_t dtrace_statusrate_max
= (hrtime_t
)10 * NANOSEC
; /* 6/minute */
163 dtrace_optval_t dtrace_switchrate_default
= NANOSEC
; /* 1 hz */
164 dtrace_optval_t dtrace_nspec_default
= 1;
165 dtrace_optval_t dtrace_specsize_default
= 32 * 1024;
166 dtrace_optval_t dtrace_stackframes_default
= 20;
167 dtrace_optval_t dtrace_ustackframes_default
= 20;
168 dtrace_optval_t dtrace_jstackframes_default
= 50;
169 dtrace_optval_t dtrace_jstackstrsize_default
= 512;
170 int dtrace_msgdsize_max
= 128;
171 hrtime_t dtrace_chill_max
= 500 * (NANOSEC
/ MILLISEC
); /* 500 ms */
172 hrtime_t dtrace_chill_interval
= NANOSEC
; /* 1000 ms */
173 int dtrace_devdepth_max
= 32;
174 int dtrace_err_verbose
;
175 int dtrace_provide_private_probes
= 0;
176 hrtime_t dtrace_deadman_interval
= NANOSEC
;
177 hrtime_t dtrace_deadman_timeout
= (hrtime_t
)10 * NANOSEC
;
178 hrtime_t dtrace_deadman_user
= (hrtime_t
)30 * NANOSEC
;
181 * DTrace External Variables
183 * As dtrace(7D) is a kernel module, any DTrace variables are obviously
184 * available to DTrace consumers via the backtick (`) syntax. One of these,
185 * dtrace_zero, is made deliberately so: it is provided as a source of
186 * well-known, zero-filled memory. While this variable is not documented,
187 * it is used by some translators as an implementation detail.
189 const char dtrace_zero
[256] = { 0 }; /* zero-filled memory */
190 unsigned int dtrace_max_cpus
= 0; /* number of enabled cpus */
192 * DTrace Internal Variables
194 static dev_info_t
*dtrace_devi
; /* device info */
195 static vmem_t
*dtrace_arena
; /* probe ID arena */
196 static vmem_t
*dtrace_minor
; /* minor number arena */
197 static taskq_t
*dtrace_taskq
; /* task queue */
198 static dtrace_probe_t
**dtrace_probes
; /* array of all probes */
199 static int dtrace_nprobes
; /* number of probes */
200 static dtrace_provider_t
*dtrace_provider
; /* provider list */
201 static dtrace_meta_t
*dtrace_meta_pid
; /* user-land meta provider */
202 static int dtrace_opens
; /* number of opens */
203 static int dtrace_helpers
; /* number of helpers */
204 static void *dtrace_softstate
; /* softstate pointer */
205 static dtrace_hash_t
*dtrace_bymod
; /* probes hashed by module */
206 static dtrace_hash_t
*dtrace_byfunc
; /* probes hashed by function */
207 static dtrace_hash_t
*dtrace_byname
; /* probes hashed by name */
208 static dtrace_toxrange_t
*dtrace_toxrange
; /* toxic range array */
209 static int dtrace_toxranges
; /* number of toxic ranges */
210 static int dtrace_toxranges_max
; /* size of toxic range array */
211 static dtrace_anon_t dtrace_anon
; /* anonymous enabling */
212 static kmem_cache_t
*dtrace_state_cache
; /* cache for dynamic state */
213 static uint64_t dtrace_vtime_references
; /* number of vtimestamp refs */
214 static kthread_t
*dtrace_panicked
; /* panicking thread */
215 static dtrace_ecb_t
*dtrace_ecb_create_cache
; /* cached created ECB */
216 static dtrace_genid_t dtrace_probegen
; /* current probe generation */
217 static dtrace_helpers_t
*dtrace_deferred_pid
; /* deferred helper list */
218 static dtrace_enabling_t
*dtrace_retained
; /* list of retained enablings */
219 static dtrace_genid_t dtrace_retained_gen
; /* current retained enab gen */
220 static dtrace_dynvar_t dtrace_dynhash_sink
; /* end of dynamic hash chains */
222 static int dtrace_dof_mode
; /* See dtrace_impl.h for a description of Darwin's dof modes. */
225 * This does't quite fit as an internal variable, as it must be accessed in
226 * fbt_provide and sdt_provide. Its clearly not a dtrace tunable variable either...
228 int dtrace_kernel_symbol_mode
; /* See dtrace_impl.h for a description of Darwin's kernel symbol modes. */
232 * To save memory, some common memory allocations are given a
233 * unique zone. For example, dtrace_probe_t is 72 bytes in size,
234 * which means it would fall into the kalloc.128 bucket. With
235 * 20k elements allocated, the space saved is substantial.
238 struct zone
*dtrace_probe_t_zone
;
240 static int dtrace_module_unloaded(struct kmod_info
*kmod
);
244 * DTrace is protected by three (relatively coarse-grained) locks:
246 * (1) dtrace_lock is required to manipulate essentially any DTrace state,
247 * including enabling state, probes, ECBs, consumer state, helper state,
248 * etc. Importantly, dtrace_lock is _not_ required when in probe context;
249 * probe context is lock-free -- synchronization is handled via the
250 * dtrace_sync() cross call mechanism.
252 * (2) dtrace_provider_lock is required when manipulating provider state, or
253 * when provider state must be held constant.
255 * (3) dtrace_meta_lock is required when manipulating meta provider state, or
256 * when meta provider state must be held constant.
258 * The lock ordering between these three locks is dtrace_meta_lock before
259 * dtrace_provider_lock before dtrace_lock. (In particular, there are
260 * several places where dtrace_provider_lock is held by the framework as it
261 * calls into the providers -- which then call back into the framework,
262 * grabbing dtrace_lock.)
264 * There are two other locks in the mix: mod_lock and cpu_lock. With respect
265 * to dtrace_provider_lock and dtrace_lock, cpu_lock continues its historical
266 * role as a coarse-grained lock; it is acquired before both of these locks.
267 * With respect to dtrace_meta_lock, its behavior is stranger: cpu_lock must
268 * be acquired _between_ dtrace_meta_lock and any other DTrace locks.
269 * mod_lock is similar with respect to dtrace_provider_lock in that it must be
270 * acquired _between_ dtrace_provider_lock and dtrace_lock.
277 * For porting purposes, all kmutex_t vars have been changed
278 * to lck_mtx_t, which require explicit initialization.
280 * kmutex_t becomes lck_mtx_t
281 * mutex_enter() becomes lck_mtx_lock()
282 * mutex_exit() becomes lck_mtx_unlock()
284 * Lock asserts are changed like this:
286 * ASSERT(MUTEX_HELD(&cpu_lock));
288 * lck_mtx_assert(&cpu_lock, LCK_MTX_ASSERT_OWNED);
291 static lck_mtx_t dtrace_lock
; /* probe state lock */
292 static lck_mtx_t dtrace_provider_lock
; /* provider state lock */
293 static lck_mtx_t dtrace_meta_lock
; /* meta-provider state lock */
294 static lck_rw_t dtrace_dof_mode_lock
; /* dof mode lock */
297 * DTrace Provider Variables
299 * These are the variables relating to DTrace as a provider (that is, the
300 * provider of the BEGIN, END, and ERROR probes).
302 static dtrace_pattr_t dtrace_provider_attr
= {
303 { DTRACE_STABILITY_STABLE
, DTRACE_STABILITY_STABLE
, DTRACE_CLASS_COMMON
},
304 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_UNKNOWN
},
305 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_UNKNOWN
},
306 { DTRACE_STABILITY_STABLE
, DTRACE_STABILITY_STABLE
, DTRACE_CLASS_COMMON
},
307 { DTRACE_STABILITY_STABLE
, DTRACE_STABILITY_STABLE
, DTRACE_CLASS_COMMON
},
315 dtrace_enable_nullop(void)
320 static dtrace_pops_t dtrace_provider_ops
= {
321 (void (*)(void *, const dtrace_probedesc_t
*))dtrace_nullop
,
322 (void (*)(void *, struct modctl
*))dtrace_nullop
,
323 (int (*)(void *, dtrace_id_t
, void *))dtrace_enable_nullop
,
324 (void (*)(void *, dtrace_id_t
, void *))dtrace_nullop
,
325 (void (*)(void *, dtrace_id_t
, void *))dtrace_nullop
,
326 (void (*)(void *, dtrace_id_t
, void *))dtrace_nullop
,
330 (void (*)(void *, dtrace_id_t
, void *))dtrace_nullop
333 static dtrace_id_t dtrace_probeid_begin
; /* special BEGIN probe */
334 static dtrace_id_t dtrace_probeid_end
; /* special END probe */
335 dtrace_id_t dtrace_probeid_error
; /* special ERROR probe */
338 * DTrace Helper Tracing Variables
340 uint32_t dtrace_helptrace_next
= 0;
341 uint32_t dtrace_helptrace_nlocals
;
342 char *dtrace_helptrace_buffer
;
343 size_t dtrace_helptrace_bufsize
= 512 * 1024;
346 int dtrace_helptrace_enabled
= 1;
348 int dtrace_helptrace_enabled
= 0;
353 * DTrace Error Hashing
355 * On DEBUG kernels, DTrace will track the errors that has seen in a hash
356 * table. This is very useful for checking coverage of tests that are
357 * expected to induce DIF or DOF processing errors, and may be useful for
358 * debugging problems in the DIF code generator or in DOF generation . The
359 * error hash may be examined with the ::dtrace_errhash MDB dcmd.
362 static dtrace_errhash_t dtrace_errhash
[DTRACE_ERRHASHSZ
];
363 static const char *dtrace_errlast
;
364 static kthread_t
*dtrace_errthread
;
365 static lck_mtx_t dtrace_errlock
;
369 * DTrace Macros and Constants
371 * These are various macros that are useful in various spots in the
372 * implementation, along with a few random constants that have no meaning
373 * outside of the implementation. There is no real structure to this cpp
374 * mishmash -- but is there ever?
376 #define DTRACE_HASHSTR(hash, probe) \
377 dtrace_hash_str(*((char **)((uintptr_t)(probe) + (hash)->dth_stroffs)))
379 #define DTRACE_HASHNEXT(hash, probe) \
380 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_nextoffs)
382 #define DTRACE_HASHPREV(hash, probe) \
383 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_prevoffs)
385 #define DTRACE_HASHEQ(hash, lhs, rhs) \
386 (strcmp(*((char **)((uintptr_t)(lhs) + (hash)->dth_stroffs)), \
387 *((char **)((uintptr_t)(rhs) + (hash)->dth_stroffs))) == 0)
389 #define DTRACE_AGGHASHSIZE_SLEW 17
391 #define DTRACE_V4MAPPED_OFFSET (sizeof (uint32_t) * 3)
394 * The key for a thread-local variable consists of the lower 61 bits of the
395 * current_thread(), plus the 3 bits of the highest active interrupt above LOCK_LEVEL.
396 * We add DIF_VARIABLE_MAX to t_did to assure that the thread key is never
397 * equal to a variable identifier. This is necessary (but not sufficient) to
398 * assure that global associative arrays never collide with thread-local
399 * variables. To guarantee that they cannot collide, we must also define the
400 * order for keying dynamic variables. That order is:
402 * [ key0 ] ... [ keyn ] [ variable-key ] [ tls-key ]
404 * Because the variable-key and the tls-key are in orthogonal spaces, there is
405 * no way for a global variable key signature to match a thread-local key
408 #if defined (__x86_64__)
409 /* FIXME: two function calls!! */
410 #define DTRACE_TLS_THRKEY(where) { \
411 uint_t intr = ml_at_interrupt_context(); /* Note: just one measly bit */ \
412 uint64_t thr = (uintptr_t)current_thread(); \
413 ASSERT(intr < (1 << 3)); \
414 (where) = ((thr + DIF_VARIABLE_MAX) & \
415 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \
418 #error Unknown architecture
421 #define DT_BSWAP_8(x) ((x) & 0xff)
422 #define DT_BSWAP_16(x) ((DT_BSWAP_8(x) << 8) | DT_BSWAP_8((x) >> 8))
423 #define DT_BSWAP_32(x) ((DT_BSWAP_16(x) << 16) | DT_BSWAP_16((x) >> 16))
424 #define DT_BSWAP_64(x) ((DT_BSWAP_32(x) << 32) | DT_BSWAP_32((x) >> 32))
426 #define DT_MASK_LO 0x00000000FFFFFFFFULL
428 #define DTRACE_STORE(type, tomax, offset, what) \
429 *((type *)((uintptr_t)(tomax) + (uintptr_t)offset)) = (type)(what);
432 #define DTRACE_ALIGNCHECK(addr, size, flags) \
433 if (addr & (MIN(size,4) - 1)) { \
434 *flags |= CPU_DTRACE_BADALIGN; \
435 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \
440 * Test whether a range of memory starting at testaddr of size testsz falls
441 * within the range of memory described by addr, sz. We take care to avoid
442 * problems with overflow and underflow of the unsigned quantities, and
443 * disallow all negative sizes. Ranges of size 0 are allowed.
445 #define DTRACE_INRANGE(testaddr, testsz, baseaddr, basesz) \
446 ((testaddr) - (baseaddr) < (basesz) && \
447 (testaddr) + (testsz) - (baseaddr) <= (basesz) && \
448 (testaddr) + (testsz) >= (testaddr))
451 * Test whether alloc_sz bytes will fit in the scratch region. We isolate
452 * alloc_sz on the righthand side of the comparison in order to avoid overflow
453 * or underflow in the comparison with it. This is simpler than the INRANGE
454 * check above, because we know that the dtms_scratch_ptr is valid in the
455 * range. Allocations of size zero are allowed.
457 #define DTRACE_INSCRATCH(mstate, alloc_sz) \
458 ((mstate)->dtms_scratch_base + (mstate)->dtms_scratch_size - \
459 (mstate)->dtms_scratch_ptr >= (alloc_sz))
461 #define RECOVER_LABEL(bits) dtraceLoadRecover##bits:
463 #if defined (__x86_64__)
464 #define DTRACE_LOADFUNC(bits) \
466 uint##bits##_t dtrace_load##bits(uintptr_t addr); \
469 dtrace_load##bits(uintptr_t addr) \
471 size_t size = bits / NBBY; \
473 uint##bits##_t rval = 0; \
475 volatile uint16_t *flags = (volatile uint16_t *) \
476 &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; \
478 DTRACE_ALIGNCHECK(addr, size, flags); \
480 for (i = 0; i < dtrace_toxranges; i++) { \
481 if (addr >= dtrace_toxrange[i].dtt_limit) \
484 if (addr + size <= dtrace_toxrange[i].dtt_base) \
488 * This address falls within a toxic region; return 0. \
490 *flags |= CPU_DTRACE_BADADDR; \
491 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \
496 volatile vm_offset_t recover = (vm_offset_t)&&dtraceLoadRecover##bits; \
497 *flags |= CPU_DTRACE_NOFAULT; \
498 recover = dtrace_set_thread_recover(current_thread(), recover); \
501 * PR6394061 - avoid device memory that is unpredictably \
502 * mapped and unmapped \
504 if (pmap_valid_page(pmap_find_phys(kernel_pmap, addr))) \
505 rval = *((volatile uint##bits##_t *)addr); \
506 RECOVER_LABEL(bits); \
507 (void)dtrace_set_thread_recover(current_thread(), recover); \
508 *flags &= ~CPU_DTRACE_NOFAULT; \
513 #else /* all other architectures */
514 #error Unknown Architecture
518 #define dtrace_loadptr dtrace_load64
520 #define dtrace_loadptr dtrace_load32
523 #define DTRACE_DYNHASH_FREE 0
524 #define DTRACE_DYNHASH_SINK 1
525 #define DTRACE_DYNHASH_VALID 2
527 #define DTRACE_MATCH_FAIL -1
528 #define DTRACE_MATCH_NEXT 0
529 #define DTRACE_MATCH_DONE 1
530 #define DTRACE_ANCHORED(probe) ((probe)->dtpr_func[0] != '\0')
531 #define DTRACE_STATE_ALIGN 64
533 #define DTRACE_FLAGS2FLT(flags) \
534 (((flags) & CPU_DTRACE_BADADDR) ? DTRACEFLT_BADADDR : \
535 ((flags) & CPU_DTRACE_ILLOP) ? DTRACEFLT_ILLOP : \
536 ((flags) & CPU_DTRACE_DIVZERO) ? DTRACEFLT_DIVZERO : \
537 ((flags) & CPU_DTRACE_KPRIV) ? DTRACEFLT_KPRIV : \
538 ((flags) & CPU_DTRACE_UPRIV) ? DTRACEFLT_UPRIV : \
539 ((flags) & CPU_DTRACE_TUPOFLOW) ? DTRACEFLT_TUPOFLOW : \
540 ((flags) & CPU_DTRACE_BADALIGN) ? DTRACEFLT_BADALIGN : \
541 ((flags) & CPU_DTRACE_NOSCRATCH) ? DTRACEFLT_NOSCRATCH : \
542 ((flags) & CPU_DTRACE_BADSTACK) ? DTRACEFLT_BADSTACK : \
545 #define DTRACEACT_ISSTRING(act) \
546 ((act)->dta_kind == DTRACEACT_DIFEXPR && \
547 (act)->dta_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING)
550 static size_t dtrace_strlen(const char *, size_t);
551 static dtrace_probe_t
*dtrace_probe_lookup_id(dtrace_id_t id
);
552 static void dtrace_enabling_provide(dtrace_provider_t
*);
553 static int dtrace_enabling_match(dtrace_enabling_t
*, int *);
554 static void dtrace_enabling_matchall(void);
555 static dtrace_state_t
*dtrace_anon_grab(void);
556 static uint64_t dtrace_helper(int, dtrace_mstate_t
*,
557 dtrace_state_t
*, uint64_t, uint64_t);
558 static dtrace_helpers_t
*dtrace_helpers_create(proc_t
*);
559 static void dtrace_buffer_drop(dtrace_buffer_t
*);
560 static intptr_t dtrace_buffer_reserve(dtrace_buffer_t
*, size_t, size_t,
561 dtrace_state_t
*, dtrace_mstate_t
*);
562 static int dtrace_state_option(dtrace_state_t
*, dtrace_optid_t
,
564 static int dtrace_ecb_create_enable(dtrace_probe_t
*, void *);
565 static void dtrace_helper_provider_destroy(dtrace_helper_provider_t
*);
569 * DTrace sysctl handlers
571 * These declarations and functions are used for a deeper DTrace configuration.
572 * Most of them are not per-consumer basis and may impact the other DTrace
573 * consumers. Correctness may not be supported for all the variables, so you
574 * should be careful about what values you are using.
577 SYSCTL_DECL(_kern_dtrace
);
578 SYSCTL_NODE(_kern
, OID_AUTO
, dtrace
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "dtrace");
581 sysctl_dtrace_err_verbose SYSCTL_HANDLER_ARGS
583 #pragma unused(oidp, arg2)
585 int value
= *(int *) arg1
;
587 error
= sysctl_io_number(req
, value
, sizeof(value
), &value
, &changed
);
588 if (error
|| !changed
)
591 if (value
!= 0 && value
!= 1)
594 lck_mtx_lock(&dtrace_lock
);
595 dtrace_err_verbose
= value
;
596 lck_mtx_unlock(&dtrace_lock
);
602 * kern.dtrace.err_verbose
604 * Set DTrace verbosity when an error occured (0 = disabled, 1 = enabld).
605 * Errors are reported when a DIFO or a DOF has been rejected by the kernel.
607 SYSCTL_PROC(_kern_dtrace
, OID_AUTO
, err_verbose
,
608 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
609 &dtrace_err_verbose
, 0,
610 sysctl_dtrace_err_verbose
, "I", "dtrace error verbose");
613 sysctl_dtrace_buffer_memory_maxsize SYSCTL_HANDLER_ARGS
615 #pragma unused(oidp, arg2, req)
617 uint64_t value
= *(uint64_t *) arg1
;
619 error
= sysctl_io_number(req
, value
, sizeof(value
), &value
, &changed
);
620 if (error
|| !changed
)
623 if (value
<= dtrace_buffer_memory_inuse
)
626 lck_mtx_lock(&dtrace_lock
);
627 dtrace_buffer_memory_maxsize
= value
;
628 lck_mtx_unlock(&dtrace_lock
);
634 * kern.dtrace.buffer_memory_maxsize
636 * Set DTrace maximal size in bytes used by all the consumers' state buffers. By default
637 * the limit is PHYS_MEM / 3 for *all* consumers. Attempting to set a null, a negative value
638 * or a value <= to dtrace_buffer_memory_inuse will result in a failure.
640 SYSCTL_PROC(_kern_dtrace
, OID_AUTO
, buffer_memory_maxsize
,
641 CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
642 &dtrace_buffer_memory_maxsize
, 0,
643 sysctl_dtrace_buffer_memory_maxsize
, "Q", "dtrace state buffer memory maxsize");
646 * kern.dtrace.buffer_memory_inuse
648 * Current state buffer memory used, in bytes, by all the DTrace consumers.
649 * This value is read-only.
651 SYSCTL_QUAD(_kern_dtrace
, OID_AUTO
, buffer_memory_inuse
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
652 &dtrace_buffer_memory_inuse
, "dtrace state buffer memory in-use");
655 sysctl_dtrace_difo_maxsize SYSCTL_HANDLER_ARGS
657 #pragma unused(oidp, arg2, req)
659 size_t value
= *(size_t*) arg1
;
661 error
= sysctl_io_number(req
, value
, sizeof(value
), &value
, &changed
);
662 if (error
|| !changed
)
668 lck_mtx_lock(&dtrace_lock
);
669 dtrace_difo_maxsize
= value
;
670 lck_mtx_unlock(&dtrace_lock
);
676 * kern.dtrace.difo_maxsize
678 * Set the DIFO max size in bytes, check the definition of dtrace_difo_maxsize
679 * to get the default value. Attempting to set a null or negative size will
680 * result in a failure.
682 SYSCTL_PROC(_kern_dtrace
, OID_AUTO
, difo_maxsize
,
683 CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
684 &dtrace_difo_maxsize
, 0,
685 sysctl_dtrace_difo_maxsize
, "Q", "dtrace difo maxsize");
688 sysctl_dtrace_dof_maxsize SYSCTL_HANDLER_ARGS
690 #pragma unused(oidp, arg2, req)
692 dtrace_optval_t value
= *(dtrace_optval_t
*) arg1
;
694 error
= sysctl_io_number(req
, value
, sizeof(value
), &value
, &changed
);
695 if (error
|| !changed
)
701 lck_mtx_lock(&dtrace_lock
);
702 dtrace_dof_maxsize
= value
;
703 lck_mtx_unlock(&dtrace_lock
);
709 * kern.dtrace.dof_maxsize
711 * Set the DOF max size in bytes, check the definition of dtrace_dof_maxsize to
712 * get the default value. Attempting to set a null or negative size will result
715 SYSCTL_PROC(_kern_dtrace
, OID_AUTO
, dof_maxsize
,
716 CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
717 &dtrace_dof_maxsize
, 0,
718 sysctl_dtrace_dof_maxsize
, "Q", "dtrace dof maxsize");
721 sysctl_dtrace_global_maxsize SYSCTL_HANDLER_ARGS
723 #pragma unused(oidp, arg2, req)
725 dtrace_optval_t value
= *(dtrace_optval_t
*) arg1
;
727 error
= sysctl_io_number(req
, value
, sizeof(value
), &value
, &changed
);
728 if (error
|| !changed
)
734 lck_mtx_lock(&dtrace_lock
);
735 dtrace_global_maxsize
= value
;
736 lck_mtx_unlock(&dtrace_lock
);
742 * kern.dtrace.global_maxsize
744 * Set the global variable max size in bytes, check the definition of
745 * dtrace_global_maxsize to get the default value. Attempting to set a null or
746 * negative size will result in a failure.
748 SYSCTL_PROC(_kern_dtrace
, OID_AUTO
, global_maxsize
,
749 CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
750 &dtrace_global_maxsize
, 0,
751 sysctl_dtrace_global_maxsize
, "Q", "dtrace global maxsize");
754 sysctl_dtrace_provide_private_probes SYSCTL_HANDLER_ARGS
756 #pragma unused(oidp, arg2)
758 int value
= *(int *) arg1
;
760 error
= sysctl_io_number(req
, value
, sizeof(value
), &value
, NULL
);
764 if (value
!= 0 && value
!= 1)
767 lck_mtx_lock(&dtrace_lock
);
768 dtrace_provide_private_probes
= value
;
769 lck_mtx_unlock(&dtrace_lock
);
775 * kern.dtrace.provide_private_probes
777 * Set whether the providers must provide the private probes. This is
778 * mainly used by the FBT provider to request probes for the private/static
781 SYSCTL_PROC(_kern_dtrace
, OID_AUTO
, provide_private_probes
,
782 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
783 &dtrace_provide_private_probes
, 0,
784 sysctl_dtrace_provide_private_probes
, "I", "provider must provide the private probes");
787 * DTrace Probe Context Functions
789 * These functions are called from probe context. Because probe context is
790 * any context in which C may be called, arbitrarily locks may be held,
791 * interrupts may be disabled, we may be in arbitrary dispatched state, etc.
792 * As a result, functions called from probe context may only call other DTrace
793 * support functions -- they may not interact at all with the system at large.
794 * (Note that the ASSERT macro is made probe-context safe by redefining it in
795 * terms of dtrace_assfail(), a probe-context safe function.) If arbitrary
796 * loads are to be performed from probe context, they _must_ be in terms of
797 * the safe dtrace_load*() variants.
799 * Some functions in this block are not actually called from probe context;
800 * for these functions, there will be a comment above the function reading
801 * "Note: not called from probe context."
805 dtrace_assfail(const char *a
, const char *f
, int l
)
807 panic("dtrace: assertion failed: %s, file: %s, line: %d", a
, f
, l
);
810 * We just need something here that even the most clever compiler
811 * cannot optimize away.
813 return (a
[(uintptr_t)f
]);
817 * Atomically increment a specified error counter from probe context.
820 dtrace_error(uint32_t *counter
)
823 * Most counters stored to in probe context are per-CPU counters.
824 * However, there are some error conditions that are sufficiently
825 * arcane that they don't merit per-CPU storage. If these counters
826 * are incremented concurrently on different CPUs, scalability will be
827 * adversely affected -- but we don't expect them to be white-hot in a
828 * correctly constructed enabling...
835 if ((nval
= oval
+ 1) == 0) {
837 * If the counter would wrap, set it to 1 -- assuring
838 * that the counter is never zero when we have seen
839 * errors. (The counter must be 32-bits because we
840 * aren't guaranteed a 64-bit compare&swap operation.)
841 * To save this code both the infamy of being fingered
842 * by a priggish news story and the indignity of being
843 * the target of a neo-puritan witch trial, we're
844 * carefully avoiding any colorful description of the
845 * likelihood of this condition -- but suffice it to
846 * say that it is only slightly more likely than the
847 * overflow of predicate cache IDs, as discussed in
848 * dtrace_predicate_create().
852 } while (dtrace_cas32(counter
, oval
, nval
) != oval
);
856 * Use the DTRACE_LOADFUNC macro to define functions for each of loading a
857 * uint8_t, a uint16_t, a uint32_t and a uint64_t.
865 dtrace_inscratch(uintptr_t dest
, size_t size
, dtrace_mstate_t
*mstate
)
867 if (dest
< mstate
->dtms_scratch_base
)
870 if (dest
+ size
< dest
)
873 if (dest
+ size
> mstate
->dtms_scratch_ptr
)
880 dtrace_canstore_statvar(uint64_t addr
, size_t sz
,
881 dtrace_statvar_t
**svars
, int nsvars
)
885 for (i
= 0; i
< nsvars
; i
++) {
886 dtrace_statvar_t
*svar
= svars
[i
];
888 if (svar
== NULL
|| svar
->dtsv_size
== 0)
891 if (DTRACE_INRANGE(addr
, sz
, svar
->dtsv_data
, svar
->dtsv_size
))
899 * Check to see if the address is within a memory region to which a store may
900 * be issued. This includes the DTrace scratch areas, and any DTrace variable
901 * region. The caller of dtrace_canstore() is responsible for performing any
902 * alignment checks that are needed before stores are actually executed.
905 dtrace_canstore(uint64_t addr
, size_t sz
, dtrace_mstate_t
*mstate
,
906 dtrace_vstate_t
*vstate
)
909 * First, check to see if the address is in scratch space...
911 if (DTRACE_INRANGE(addr
, sz
, mstate
->dtms_scratch_base
,
912 mstate
->dtms_scratch_size
))
916 * Now check to see if it's a dynamic variable. This check will pick
917 * up both thread-local variables and any global dynamically-allocated
920 if (DTRACE_INRANGE(addr
, sz
, (uintptr_t)vstate
->dtvs_dynvars
.dtds_base
,
921 vstate
->dtvs_dynvars
.dtds_size
)) {
922 dtrace_dstate_t
*dstate
= &vstate
->dtvs_dynvars
;
923 uintptr_t base
= (uintptr_t)dstate
->dtds_base
+
924 (dstate
->dtds_hashsize
* sizeof (dtrace_dynhash_t
));
928 * Before we assume that we can store here, we need to make
929 * sure that it isn't in our metadata -- storing to our
930 * dynamic variable metadata would corrupt our state. For
931 * the range to not include any dynamic variable metadata,
934 * (1) Start above the hash table that is at the base of
935 * the dynamic variable space
937 * (2) Have a starting chunk offset that is beyond the
938 * dtrace_dynvar_t that is at the base of every chunk
940 * (3) Not span a chunk boundary
946 chunkoffs
= (addr
- base
) % dstate
->dtds_chunksize
;
948 if (chunkoffs
< sizeof (dtrace_dynvar_t
))
951 if (chunkoffs
+ sz
> dstate
->dtds_chunksize
)
958 * Finally, check the static local and global variables. These checks
959 * take the longest, so we perform them last.
961 if (dtrace_canstore_statvar(addr
, sz
,
962 vstate
->dtvs_locals
, vstate
->dtvs_nlocals
))
965 if (dtrace_canstore_statvar(addr
, sz
,
966 vstate
->dtvs_globals
, vstate
->dtvs_nglobals
))
974 * Convenience routine to check to see if the address is within a memory
975 * region in which a load may be issued given the user's privilege level;
976 * if not, it sets the appropriate error flags and loads 'addr' into the
977 * illegal value slot.
979 * DTrace subroutines (DIF_SUBR_*) should use this helper to implement
980 * appropriate memory access protection.
983 dtrace_canload(uint64_t addr
, size_t sz
, dtrace_mstate_t
*mstate
,
984 dtrace_vstate_t
*vstate
)
986 volatile uint64_t *illval
= &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
;
989 * If we hold the privilege to read from kernel memory, then
990 * everything is readable.
992 if ((mstate
->dtms_access
& DTRACE_ACCESS_KERNEL
) != 0)
996 * You can obviously read that which you can store.
998 if (dtrace_canstore(addr
, sz
, mstate
, vstate
))
1002 * We're allowed to read from our own string table.
1004 if (DTRACE_INRANGE(addr
, sz
, (uintptr_t)mstate
->dtms_difo
->dtdo_strtab
,
1005 mstate
->dtms_difo
->dtdo_strlen
))
1008 DTRACE_CPUFLAG_SET(CPU_DTRACE_KPRIV
);
1014 * Convenience routine to check to see if a given string is within a memory
1015 * region in which a load may be issued given the user's privilege level;
1016 * this exists so that we don't need to issue unnecessary dtrace_strlen()
1017 * calls in the event that the user has all privileges.
1020 dtrace_strcanload(uint64_t addr
, size_t sz
, dtrace_mstate_t
*mstate
,
1021 dtrace_vstate_t
*vstate
)
1026 * If we hold the privilege to read from kernel memory, then
1027 * everything is readable.
1029 if ((mstate
->dtms_access
& DTRACE_ACCESS_KERNEL
) != 0)
1032 strsz
= 1 + dtrace_strlen((char *)(uintptr_t)addr
, sz
);
1033 if (dtrace_canload(addr
, strsz
, mstate
, vstate
))
1040 * Convenience routine to check to see if a given variable is within a memory
1041 * region in which a load may be issued given the user's privilege level.
1044 dtrace_vcanload(void *src
, dtrace_diftype_t
*type
, dtrace_mstate_t
*mstate
,
1045 dtrace_vstate_t
*vstate
)
1048 ASSERT(type
->dtdt_flags
& DIF_TF_BYREF
);
1051 * If we hold the privilege to read from kernel memory, then
1052 * everything is readable.
1054 if ((mstate
->dtms_access
& DTRACE_ACCESS_KERNEL
) != 0)
1057 if (type
->dtdt_kind
== DIF_TYPE_STRING
)
1058 sz
= dtrace_strlen(src
,
1059 vstate
->dtvs_state
->dts_options
[DTRACEOPT_STRSIZE
]) + 1;
1061 sz
= type
->dtdt_size
;
1063 return (dtrace_canload((uintptr_t)src
, sz
, mstate
, vstate
));
1067 * Compare two strings using safe loads.
1070 dtrace_strncmp(char *s1
, char *s2
, size_t limit
)
1073 volatile uint16_t *flags
;
1075 if (s1
== s2
|| limit
== 0)
1078 flags
= (volatile uint16_t *)&cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
1084 c1
= dtrace_load8((uintptr_t)s1
++);
1090 c2
= dtrace_load8((uintptr_t)s2
++);
1095 } while (--limit
&& c1
!= '\0' && !(*flags
& CPU_DTRACE_FAULT
));
1101 * Compute strlen(s) for a string using safe memory accesses. The additional
1102 * len parameter is used to specify a maximum length to ensure completion.
1105 dtrace_strlen(const char *s
, size_t lim
)
1109 for (len
= 0; len
!= lim
; len
++) {
1110 if (dtrace_load8((uintptr_t)s
++) == '\0')
1118 * Check if an address falls within a toxic region.
1121 dtrace_istoxic(uintptr_t kaddr
, size_t size
)
1123 uintptr_t taddr
, tsize
;
1126 for (i
= 0; i
< dtrace_toxranges
; i
++) {
1127 taddr
= dtrace_toxrange
[i
].dtt_base
;
1128 tsize
= dtrace_toxrange
[i
].dtt_limit
- taddr
;
1130 if (kaddr
- taddr
< tsize
) {
1131 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1132 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= kaddr
;
1136 if (taddr
- kaddr
< size
) {
1137 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1138 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= taddr
;
1147 * Copy src to dst using safe memory accesses. The src is assumed to be unsafe
1148 * memory specified by the DIF program. The dst is assumed to be safe memory
1149 * that we can store to directly because it is managed by DTrace. As with
1150 * standard bcopy, overlapping copies are handled properly.
1153 dtrace_bcopy(const void *src
, void *dst
, size_t len
)
1157 const uint8_t *s2
= src
;
1161 *s1
++ = dtrace_load8((uintptr_t)s2
++);
1162 } while (--len
!= 0);
1168 *--s1
= dtrace_load8((uintptr_t)--s2
);
1169 } while (--len
!= 0);
1175 * Copy src to dst using safe memory accesses, up to either the specified
1176 * length, or the point that a nul byte is encountered. The src is assumed to
1177 * be unsafe memory specified by the DIF program. The dst is assumed to be
1178 * safe memory that we can store to directly because it is managed by DTrace.
1179 * Unlike dtrace_bcopy(), overlapping regions are not handled.
1182 dtrace_strcpy(const void *src
, void *dst
, size_t len
)
1185 uint8_t *s1
= dst
, c
;
1186 const uint8_t *s2
= src
;
1189 *s1
++ = c
= dtrace_load8((uintptr_t)s2
++);
1190 } while (--len
!= 0 && c
!= '\0');
1195 * Copy src to dst, deriving the size and type from the specified (BYREF)
1196 * variable type. The src is assumed to be unsafe memory specified by the DIF
1197 * program. The dst is assumed to be DTrace variable memory that is of the
1198 * specified type; we assume that we can store to directly.
1201 dtrace_vcopy(void *src
, void *dst
, dtrace_diftype_t
*type
)
1203 ASSERT(type
->dtdt_flags
& DIF_TF_BYREF
);
1205 if (type
->dtdt_kind
== DIF_TYPE_STRING
) {
1206 dtrace_strcpy(src
, dst
, type
->dtdt_size
);
1208 dtrace_bcopy(src
, dst
, type
->dtdt_size
);
1213 * Compare s1 to s2 using safe memory accesses. The s1 data is assumed to be
1214 * unsafe memory specified by the DIF program. The s2 data is assumed to be
1215 * safe memory that we can access directly because it is managed by DTrace.
1218 dtrace_bcmp(const void *s1
, const void *s2
, size_t len
)
1220 volatile uint16_t *flags
;
1222 flags
= (volatile uint16_t *)&cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
1227 if (s1
== NULL
|| s2
== NULL
)
1230 if (s1
!= s2
&& len
!= 0) {
1231 const uint8_t *ps1
= s1
;
1232 const uint8_t *ps2
= s2
;
1235 if (dtrace_load8((uintptr_t)ps1
++) != *ps2
++)
1237 } while (--len
!= 0 && !(*flags
& CPU_DTRACE_FAULT
));
1243 * Zero the specified region using a simple byte-by-byte loop. Note that this
1244 * is for safe DTrace-managed memory only.
1247 dtrace_bzero(void *dst
, size_t len
)
1251 for (cp
= dst
; len
!= 0; len
--)
1256 dtrace_add_128(uint64_t *addend1
, uint64_t *addend2
, uint64_t *sum
)
1260 result
[0] = addend1
[0] + addend2
[0];
1261 result
[1] = addend1
[1] + addend2
[1] +
1262 (result
[0] < addend1
[0] || result
[0] < addend2
[0] ? 1 : 0);
1269 * Shift the 128-bit value in a by b. If b is positive, shift left.
1270 * If b is negative, shift right.
1273 dtrace_shift_128(uint64_t *a
, int b
)
1283 a
[0] = a
[1] >> (b
- 64);
1287 mask
= 1LL << (64 - b
);
1289 a
[0] |= ((a
[1] & mask
) << (64 - b
));
1294 a
[1] = a
[0] << (b
- 64);
1298 mask
= a
[0] >> (64 - b
);
1306 * The basic idea is to break the 2 64-bit values into 4 32-bit values,
1307 * use native multiplication on those, and then re-combine into the
1308 * resulting 128-bit value.
1310 * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) =
1317 dtrace_multiply_128(uint64_t factor1
, uint64_t factor2
, uint64_t *product
)
1319 uint64_t hi1
, hi2
, lo1
, lo2
;
1322 hi1
= factor1
>> 32;
1323 hi2
= factor2
>> 32;
1325 lo1
= factor1
& DT_MASK_LO
;
1326 lo2
= factor2
& DT_MASK_LO
;
1328 product
[0] = lo1
* lo2
;
1329 product
[1] = hi1
* hi2
;
1333 dtrace_shift_128(tmp
, 32);
1334 dtrace_add_128(product
, tmp
, product
);
1338 dtrace_shift_128(tmp
, 32);
1339 dtrace_add_128(product
, tmp
, product
);
1343 * This privilege check should be used by actions and subroutines to
1344 * verify that the user credentials of the process that enabled the
1345 * invoking ECB match the target credentials
1348 dtrace_priv_proc_common_user(dtrace_state_t
*state
)
1350 cred_t
*cr
, *s_cr
= state
->dts_cred
.dcr_cred
;
1353 * We should always have a non-NULL state cred here, since if cred
1354 * is null (anonymous tracing), we fast-path bypass this routine.
1356 ASSERT(s_cr
!= NULL
);
1358 if ((cr
= dtrace_CRED()) != NULL
&&
1359 posix_cred_get(s_cr
)->cr_uid
== posix_cred_get(cr
)->cr_uid
&&
1360 posix_cred_get(s_cr
)->cr_uid
== posix_cred_get(cr
)->cr_ruid
&&
1361 posix_cred_get(s_cr
)->cr_uid
== posix_cred_get(cr
)->cr_suid
&&
1362 posix_cred_get(s_cr
)->cr_gid
== posix_cred_get(cr
)->cr_gid
&&
1363 posix_cred_get(s_cr
)->cr_gid
== posix_cred_get(cr
)->cr_rgid
&&
1364 posix_cred_get(s_cr
)->cr_gid
== posix_cred_get(cr
)->cr_sgid
)
1371 * This privilege check should be used by actions and subroutines to
1372 * verify that the zone of the process that enabled the invoking ECB
1373 * matches the target credentials
1376 dtrace_priv_proc_common_zone(dtrace_state_t
*state
)
1378 cred_t
*cr
, *s_cr
= state
->dts_cred
.dcr_cred
;
1379 #pragma unused(cr, s_cr, state) /* __APPLE__ */
1382 * We should always have a non-NULL state cred here, since if cred
1383 * is null (anonymous tracing), we fast-path bypass this routine.
1385 ASSERT(s_cr
!= NULL
);
1387 return 1; /* APPLE NOTE: Darwin doesn't do zones. */
1391 * This privilege check should be used by actions and subroutines to
1392 * verify that the process has not setuid or changed credentials.
1395 dtrace_priv_proc_common_nocd(void)
1397 return 1; /* Darwin omits "No Core Dump" flag. */
1401 dtrace_priv_proc_destructive(dtrace_state_t
*state
)
1403 int action
= state
->dts_cred
.dcr_action
;
1405 if (ISSET(current_proc()->p_lflag
, P_LNOATTACH
))
1408 if (dtrace_is_restricted() && !dtrace_can_attach_to_proc(current_proc()))
1411 if (((action
& DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE
) == 0) &&
1412 dtrace_priv_proc_common_zone(state
) == 0)
1415 if (((action
& DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER
) == 0) &&
1416 dtrace_priv_proc_common_user(state
) == 0)
1419 if (((action
& DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG
) == 0) &&
1420 dtrace_priv_proc_common_nocd() == 0)
1426 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
|= CPU_DTRACE_UPRIV
;
1432 dtrace_priv_proc_control(dtrace_state_t
*state
)
1434 if (ISSET(current_proc()->p_lflag
, P_LNOATTACH
))
1437 if (dtrace_is_restricted() && !dtrace_can_attach_to_proc(current_proc()))
1440 if (state
->dts_cred
.dcr_action
& DTRACE_CRA_PROC_CONTROL
)
1443 if (dtrace_priv_proc_common_zone(state
) &&
1444 dtrace_priv_proc_common_user(state
) &&
1445 dtrace_priv_proc_common_nocd())
1449 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
|= CPU_DTRACE_UPRIV
;
1455 dtrace_priv_proc(dtrace_state_t
*state
)
1457 if (ISSET(current_proc()->p_lflag
, P_LNOATTACH
))
1460 if (dtrace_is_restricted() && !dtrace_can_attach_to_proc(current_proc()))
1463 if (state
->dts_cred
.dcr_action
& DTRACE_CRA_PROC
)
1467 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
|= CPU_DTRACE_UPRIV
;
1473 * The P_LNOATTACH check is an Apple specific check.
1474 * We need a version of dtrace_priv_proc() that omits
1475 * that check for PID and EXECNAME accesses
1478 dtrace_priv_proc_relaxed(dtrace_state_t
*state
)
1481 if (state
->dts_cred
.dcr_action
& DTRACE_CRA_PROC
)
1484 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
|= CPU_DTRACE_UPRIV
;
1490 dtrace_priv_kernel(dtrace_state_t
*state
)
1492 if (dtrace_is_restricted())
1495 if (state
->dts_cred
.dcr_action
& DTRACE_CRA_KERNEL
)
1499 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
|= CPU_DTRACE_KPRIV
;
1505 dtrace_priv_kernel_destructive(dtrace_state_t
*state
)
1507 if (dtrace_is_restricted())
1510 if (state
->dts_cred
.dcr_action
& DTRACE_CRA_KERNEL_DESTRUCTIVE
)
1514 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
|= CPU_DTRACE_KPRIV
;
1520 * Note: not called from probe context. This function is called
1521 * asynchronously (and at a regular interval) from outside of probe context to
1522 * clean the dirty dynamic variable lists on all CPUs. Dynamic variable
1523 * cleaning is explained in detail in <sys/dtrace_impl.h>.
1526 dtrace_dynvar_clean(dtrace_dstate_t
*dstate
)
1528 dtrace_dynvar_t
*dirty
;
1529 dtrace_dstate_percpu_t
*dcpu
;
1532 for (i
= 0; i
< (int)NCPU
; i
++) {
1533 dcpu
= &dstate
->dtds_percpu
[i
];
1535 ASSERT(dcpu
->dtdsc_rinsing
== NULL
);
1538 * If the dirty list is NULL, there is no dirty work to do.
1540 if (dcpu
->dtdsc_dirty
== NULL
)
1544 * If the clean list is non-NULL, then we're not going to do
1545 * any work for this CPU -- it means that there has not been
1546 * a dtrace_dynvar() allocation on this CPU (or from this CPU)
1547 * since the last time we cleaned house.
1549 if (dcpu
->dtdsc_clean
!= NULL
)
1555 * Atomically move the dirty list aside.
1558 dirty
= dcpu
->dtdsc_dirty
;
1561 * Before we zap the dirty list, set the rinsing list.
1562 * (This allows for a potential assertion in
1563 * dtrace_dynvar(): if a free dynamic variable appears
1564 * on a hash chain, either the dirty list or the
1565 * rinsing list for some CPU must be non-NULL.)
1567 dcpu
->dtdsc_rinsing
= dirty
;
1568 dtrace_membar_producer();
1569 } while (dtrace_casptr(&dcpu
->dtdsc_dirty
,
1570 dirty
, NULL
) != dirty
);
1575 * We have no work to do; we can simply return.
1582 for (i
= 0; i
< (int)NCPU
; i
++) {
1583 dcpu
= &dstate
->dtds_percpu
[i
];
1585 if (dcpu
->dtdsc_rinsing
== NULL
)
1589 * We are now guaranteed that no hash chain contains a pointer
1590 * into this dirty list; we can make it clean.
1592 ASSERT(dcpu
->dtdsc_clean
== NULL
);
1593 dcpu
->dtdsc_clean
= dcpu
->dtdsc_rinsing
;
1594 dcpu
->dtdsc_rinsing
= NULL
;
1598 * Before we actually set the state to be DTRACE_DSTATE_CLEAN, make
1599 * sure that all CPUs have seen all of the dtdsc_clean pointers.
1600 * This prevents a race whereby a CPU incorrectly decides that
1601 * the state should be something other than DTRACE_DSTATE_CLEAN
1602 * after dtrace_dynvar_clean() has completed.
1606 dstate
->dtds_state
= DTRACE_DSTATE_CLEAN
;
1610 * Depending on the value of the op parameter, this function looks-up,
1611 * allocates or deallocates an arbitrarily-keyed dynamic variable. If an
1612 * allocation is requested, this function will return a pointer to a
1613 * dtrace_dynvar_t corresponding to the allocated variable -- or NULL if no
1614 * variable can be allocated. If NULL is returned, the appropriate counter
1615 * will be incremented.
1617 static dtrace_dynvar_t
*
1618 dtrace_dynvar(dtrace_dstate_t
*dstate
, uint_t nkeys
,
1619 dtrace_key_t
*key
, size_t dsize
, dtrace_dynvar_op_t op
,
1620 dtrace_mstate_t
*mstate
, dtrace_vstate_t
*vstate
)
1622 uint64_t hashval
= DTRACE_DYNHASH_VALID
;
1623 dtrace_dynhash_t
*hash
= dstate
->dtds_hash
;
1624 dtrace_dynvar_t
*free
, *new_free
, *next
, *dvar
, *start
, *prev
= NULL
;
1625 processorid_t me
= CPU
->cpu_id
, cpu
= me
;
1626 dtrace_dstate_percpu_t
*dcpu
= &dstate
->dtds_percpu
[me
];
1627 size_t bucket
, ksize
;
1628 size_t chunksize
= dstate
->dtds_chunksize
;
1629 uintptr_t kdata
, lock
, nstate
;
1635 * Hash the key. As with aggregations, we use Jenkins' "One-at-a-time"
1636 * algorithm. For the by-value portions, we perform the algorithm in
1637 * 16-bit chunks (as opposed to 8-bit chunks). This speeds things up a
1638 * bit, and seems to have only a minute effect on distribution. For
1639 * the by-reference data, we perform "One-at-a-time" iterating (safely)
1640 * over each referenced byte. It's painful to do this, but it's much
1641 * better than pathological hash distribution. The efficacy of the
1642 * hashing algorithm (and a comparison with other algorithms) may be
1643 * found by running the ::dtrace_dynstat MDB dcmd.
1645 for (i
= 0; i
< nkeys
; i
++) {
1646 if (key
[i
].dttk_size
== 0) {
1647 uint64_t val
= key
[i
].dttk_value
;
1649 hashval
+= (val
>> 48) & 0xffff;
1650 hashval
+= (hashval
<< 10);
1651 hashval
^= (hashval
>> 6);
1653 hashval
+= (val
>> 32) & 0xffff;
1654 hashval
+= (hashval
<< 10);
1655 hashval
^= (hashval
>> 6);
1657 hashval
+= (val
>> 16) & 0xffff;
1658 hashval
+= (hashval
<< 10);
1659 hashval
^= (hashval
>> 6);
1661 hashval
+= val
& 0xffff;
1662 hashval
+= (hashval
<< 10);
1663 hashval
^= (hashval
>> 6);
1666 * This is incredibly painful, but it beats the hell
1667 * out of the alternative.
1669 uint64_t j
, size
= key
[i
].dttk_size
;
1670 uintptr_t base
= (uintptr_t)key
[i
].dttk_value
;
1672 if (!dtrace_canload(base
, size
, mstate
, vstate
))
1675 for (j
= 0; j
< size
; j
++) {
1676 hashval
+= dtrace_load8(base
+ j
);
1677 hashval
+= (hashval
<< 10);
1678 hashval
^= (hashval
>> 6);
1683 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT
))
1686 hashval
+= (hashval
<< 3);
1687 hashval
^= (hashval
>> 11);
1688 hashval
+= (hashval
<< 15);
1691 * There is a remote chance (ideally, 1 in 2^31) that our hashval
1692 * comes out to be one of our two sentinel hash values. If this
1693 * actually happens, we set the hashval to be a value known to be a
1694 * non-sentinel value.
1696 if (hashval
== DTRACE_DYNHASH_FREE
|| hashval
== DTRACE_DYNHASH_SINK
)
1697 hashval
= DTRACE_DYNHASH_VALID
;
1700 * Yes, it's painful to do a divide here. If the cycle count becomes
1701 * important here, tricks can be pulled to reduce it. (However, it's
1702 * critical that hash collisions be kept to an absolute minimum;
1703 * they're much more painful than a divide.) It's better to have a
1704 * solution that generates few collisions and still keeps things
1705 * relatively simple.
1707 bucket
= hashval
% dstate
->dtds_hashsize
;
1709 if (op
== DTRACE_DYNVAR_DEALLOC
) {
1710 volatile uintptr_t *lockp
= &hash
[bucket
].dtdh_lock
;
1713 while ((lock
= *lockp
) & 1)
1716 if (dtrace_casptr((void *)(uintptr_t)lockp
,
1717 (void *)lock
, (void *)(lock
+ 1)) == (void *)lock
)
1721 dtrace_membar_producer();
1726 lock
= hash
[bucket
].dtdh_lock
;
1728 dtrace_membar_consumer();
1730 start
= hash
[bucket
].dtdh_chain
;
1731 ASSERT(start
!= NULL
&& (start
->dtdv_hashval
== DTRACE_DYNHASH_SINK
||
1732 start
->dtdv_hashval
!= DTRACE_DYNHASH_FREE
||
1733 op
!= DTRACE_DYNVAR_DEALLOC
));
1735 for (dvar
= start
; dvar
!= NULL
; dvar
= dvar
->dtdv_next
) {
1736 dtrace_tuple_t
*dtuple
= &dvar
->dtdv_tuple
;
1737 dtrace_key_t
*dkey
= &dtuple
->dtt_key
[0];
1739 if (dvar
->dtdv_hashval
!= hashval
) {
1740 if (dvar
->dtdv_hashval
== DTRACE_DYNHASH_SINK
) {
1742 * We've reached the sink, and therefore the
1743 * end of the hash chain; we can kick out of
1744 * the loop knowing that we have seen a valid
1745 * snapshot of state.
1747 ASSERT(dvar
->dtdv_next
== NULL
);
1748 ASSERT(dvar
== &dtrace_dynhash_sink
);
1752 if (dvar
->dtdv_hashval
== DTRACE_DYNHASH_FREE
) {
1754 * We've gone off the rails: somewhere along
1755 * the line, one of the members of this hash
1756 * chain was deleted. Note that we could also
1757 * detect this by simply letting this loop run
1758 * to completion, as we would eventually hit
1759 * the end of the dirty list. However, we
1760 * want to avoid running the length of the
1761 * dirty list unnecessarily (it might be quite
1762 * long), so we catch this as early as
1763 * possible by detecting the hash marker. In
1764 * this case, we simply set dvar to NULL and
1765 * break; the conditional after the loop will
1766 * send us back to top.
1775 if (dtuple
->dtt_nkeys
!= nkeys
)
1778 for (i
= 0; i
< nkeys
; i
++, dkey
++) {
1779 if (dkey
->dttk_size
!= key
[i
].dttk_size
)
1780 goto next
; /* size or type mismatch */
1782 if (dkey
->dttk_size
!= 0) {
1784 (void *)(uintptr_t)key
[i
].dttk_value
,
1785 (void *)(uintptr_t)dkey
->dttk_value
,
1789 if (dkey
->dttk_value
!= key
[i
].dttk_value
)
1794 if (op
!= DTRACE_DYNVAR_DEALLOC
)
1797 ASSERT(dvar
->dtdv_next
== NULL
||
1798 dvar
->dtdv_next
->dtdv_hashval
!= DTRACE_DYNHASH_FREE
);
1801 ASSERT(hash
[bucket
].dtdh_chain
!= dvar
);
1802 ASSERT(start
!= dvar
);
1803 ASSERT(prev
->dtdv_next
== dvar
);
1804 prev
->dtdv_next
= dvar
->dtdv_next
;
1806 if (dtrace_casptr(&hash
[bucket
].dtdh_chain
,
1807 start
, dvar
->dtdv_next
) != start
) {
1809 * We have failed to atomically swing the
1810 * hash table head pointer, presumably because
1811 * of a conflicting allocation on another CPU.
1812 * We need to reread the hash chain and try
1819 dtrace_membar_producer();
1822 * Now set the hash value to indicate that it's free.
1824 ASSERT(hash
[bucket
].dtdh_chain
!= dvar
);
1825 dvar
->dtdv_hashval
= DTRACE_DYNHASH_FREE
;
1827 dtrace_membar_producer();
1830 * Set the next pointer to point at the dirty list, and
1831 * atomically swing the dirty pointer to the newly freed dvar.
1834 next
= dcpu
->dtdsc_dirty
;
1835 dvar
->dtdv_next
= next
;
1836 } while (dtrace_casptr(&dcpu
->dtdsc_dirty
, next
, dvar
) != next
);
1839 * Finally, unlock this hash bucket.
1841 ASSERT(hash
[bucket
].dtdh_lock
== lock
);
1843 hash
[bucket
].dtdh_lock
++;
1853 * If dvar is NULL, it is because we went off the rails:
1854 * one of the elements that we traversed in the hash chain
1855 * was deleted while we were traversing it. In this case,
1856 * we assert that we aren't doing a dealloc (deallocs lock
1857 * the hash bucket to prevent themselves from racing with
1858 * one another), and retry the hash chain traversal.
1860 ASSERT(op
!= DTRACE_DYNVAR_DEALLOC
);
1864 if (op
!= DTRACE_DYNVAR_ALLOC
) {
1866 * If we are not to allocate a new variable, we want to
1867 * return NULL now. Before we return, check that the value
1868 * of the lock word hasn't changed. If it has, we may have
1869 * seen an inconsistent snapshot.
1871 if (op
== DTRACE_DYNVAR_NOALLOC
) {
1872 if (hash
[bucket
].dtdh_lock
!= lock
)
1875 ASSERT(op
== DTRACE_DYNVAR_DEALLOC
);
1876 ASSERT(hash
[bucket
].dtdh_lock
== lock
);
1878 hash
[bucket
].dtdh_lock
++;
1885 * We need to allocate a new dynamic variable. The size we need is the
1886 * size of dtrace_dynvar plus the size of nkeys dtrace_key_t's plus the
1887 * size of any auxiliary key data (rounded up to 8-byte alignment) plus
1888 * the size of any referred-to data (dsize). We then round the final
1889 * size up to the chunksize for allocation.
1891 for (ksize
= 0, i
= 0; i
< nkeys
; i
++)
1892 ksize
+= P2ROUNDUP(key
[i
].dttk_size
, sizeof (uint64_t));
1895 * This should be pretty much impossible, but could happen if, say,
1896 * strange DIF specified the tuple. Ideally, this should be an
1897 * assertion and not an error condition -- but that requires that the
1898 * chunksize calculation in dtrace_difo_chunksize() be absolutely
1899 * bullet-proof. (That is, it must not be able to be fooled by
1900 * malicious DIF.) Given the lack of backwards branches in DIF,
1901 * solving this would presumably not amount to solving the Halting
1902 * Problem -- but it still seems awfully hard.
1904 if (sizeof (dtrace_dynvar_t
) + sizeof (dtrace_key_t
) * (nkeys
- 1) +
1905 ksize
+ dsize
> chunksize
) {
1906 dcpu
->dtdsc_drops
++;
1910 nstate
= DTRACE_DSTATE_EMPTY
;
1914 free
= dcpu
->dtdsc_free
;
1917 dtrace_dynvar_t
*clean
= dcpu
->dtdsc_clean
;
1920 if (clean
== NULL
) {
1922 * We're out of dynamic variable space on
1923 * this CPU. Unless we have tried all CPUs,
1924 * we'll try to allocate from a different
1927 switch (dstate
->dtds_state
) {
1928 case DTRACE_DSTATE_CLEAN
: {
1929 void *sp
= &dstate
->dtds_state
;
1931 if (++cpu
>= (int)NCPU
)
1934 if (dcpu
->dtdsc_dirty
!= NULL
&&
1935 nstate
== DTRACE_DSTATE_EMPTY
)
1936 nstate
= DTRACE_DSTATE_DIRTY
;
1938 if (dcpu
->dtdsc_rinsing
!= NULL
)
1939 nstate
= DTRACE_DSTATE_RINSING
;
1941 dcpu
= &dstate
->dtds_percpu
[cpu
];
1946 (void) dtrace_cas32(sp
,
1947 DTRACE_DSTATE_CLEAN
, nstate
);
1950 * To increment the correct bean
1951 * counter, take another lap.
1956 case DTRACE_DSTATE_DIRTY
:
1957 dcpu
->dtdsc_dirty_drops
++;
1960 case DTRACE_DSTATE_RINSING
:
1961 dcpu
->dtdsc_rinsing_drops
++;
1964 case DTRACE_DSTATE_EMPTY
:
1965 dcpu
->dtdsc_drops
++;
1969 DTRACE_CPUFLAG_SET(CPU_DTRACE_DROP
);
1974 * The clean list appears to be non-empty. We want to
1975 * move the clean list to the free list; we start by
1976 * moving the clean pointer aside.
1978 if (dtrace_casptr(&dcpu
->dtdsc_clean
,
1979 clean
, NULL
) != clean
) {
1981 * We are in one of two situations:
1983 * (a) The clean list was switched to the
1984 * free list by another CPU.
1986 * (b) The clean list was added to by the
1989 * In either of these situations, we can
1990 * just reattempt the free list allocation.
1995 ASSERT(clean
->dtdv_hashval
== DTRACE_DYNHASH_FREE
);
1998 * Now we'll move the clean list to the free list.
1999 * It's impossible for this to fail: the only way
2000 * the free list can be updated is through this
2001 * code path, and only one CPU can own the clean list.
2002 * Thus, it would only be possible for this to fail if
2003 * this code were racing with dtrace_dynvar_clean().
2004 * (That is, if dtrace_dynvar_clean() updated the clean
2005 * list, and we ended up racing to update the free
2006 * list.) This race is prevented by the dtrace_sync()
2007 * in dtrace_dynvar_clean() -- which flushes the
2008 * owners of the clean lists out before resetting
2011 rval
= dtrace_casptr(&dcpu
->dtdsc_free
, NULL
, clean
);
2012 ASSERT(rval
== NULL
);
2017 new_free
= dvar
->dtdv_next
;
2018 } while (dtrace_casptr(&dcpu
->dtdsc_free
, free
, new_free
) != free
);
2021 * We have now allocated a new chunk. We copy the tuple keys into the
2022 * tuple array and copy any referenced key data into the data space
2023 * following the tuple array. As we do this, we relocate dttk_value
2024 * in the final tuple to point to the key data address in the chunk.
2026 kdata
= (uintptr_t)&dvar
->dtdv_tuple
.dtt_key
[nkeys
];
2027 dvar
->dtdv_data
= (void *)(kdata
+ ksize
);
2028 dvar
->dtdv_tuple
.dtt_nkeys
= nkeys
;
2030 for (i
= 0; i
< nkeys
; i
++) {
2031 dtrace_key_t
*dkey
= &dvar
->dtdv_tuple
.dtt_key
[i
];
2032 size_t kesize
= key
[i
].dttk_size
;
2036 (const void *)(uintptr_t)key
[i
].dttk_value
,
2037 (void *)kdata
, kesize
);
2038 dkey
->dttk_value
= kdata
;
2039 kdata
+= P2ROUNDUP(kesize
, sizeof (uint64_t));
2041 dkey
->dttk_value
= key
[i
].dttk_value
;
2044 dkey
->dttk_size
= kesize
;
2047 ASSERT(dvar
->dtdv_hashval
== DTRACE_DYNHASH_FREE
);
2048 dvar
->dtdv_hashval
= hashval
;
2049 dvar
->dtdv_next
= start
;
2051 if (dtrace_casptr(&hash
[bucket
].dtdh_chain
, start
, dvar
) == start
)
2055 * The cas has failed. Either another CPU is adding an element to
2056 * this hash chain, or another CPU is deleting an element from this
2057 * hash chain. The simplest way to deal with both of these cases
2058 * (though not necessarily the most efficient) is to free our
2059 * allocated block and tail-call ourselves. Note that the free is
2060 * to the dirty list and _not_ to the free list. This is to prevent
2061 * races with allocators, above.
2063 dvar
->dtdv_hashval
= DTRACE_DYNHASH_FREE
;
2065 dtrace_membar_producer();
2068 free
= dcpu
->dtdsc_dirty
;
2069 dvar
->dtdv_next
= free
;
2070 } while (dtrace_casptr(&dcpu
->dtdsc_dirty
, free
, dvar
) != free
);
2072 return (dtrace_dynvar(dstate
, nkeys
, key
, dsize
, op
, mstate
, vstate
));
2077 dtrace_aggregate_min(uint64_t *oval
, uint64_t nval
, uint64_t arg
)
2079 #pragma unused(arg) /* __APPLE__ */
2080 if ((int64_t)nval
< (int64_t)*oval
)
2086 dtrace_aggregate_max(uint64_t *oval
, uint64_t nval
, uint64_t arg
)
2088 #pragma unused(arg) /* __APPLE__ */
2089 if ((int64_t)nval
> (int64_t)*oval
)
2094 dtrace_aggregate_quantize(uint64_t *quanta
, uint64_t nval
, uint64_t incr
)
2096 int i
, zero
= DTRACE_QUANTIZE_ZEROBUCKET
;
2097 int64_t val
= (int64_t)nval
;
2100 for (i
= 0; i
< zero
; i
++) {
2101 if (val
<= DTRACE_QUANTIZE_BUCKETVAL(i
)) {
2107 for (i
= zero
+ 1; i
< DTRACE_QUANTIZE_NBUCKETS
; i
++) {
2108 if (val
< DTRACE_QUANTIZE_BUCKETVAL(i
)) {
2109 quanta
[i
- 1] += incr
;
2114 quanta
[DTRACE_QUANTIZE_NBUCKETS
- 1] += incr
;
2122 dtrace_aggregate_lquantize(uint64_t *lquanta
, uint64_t nval
, uint64_t incr
)
2124 uint64_t arg
= *lquanta
++;
2125 int32_t base
= DTRACE_LQUANTIZE_BASE(arg
);
2126 uint16_t step
= DTRACE_LQUANTIZE_STEP(arg
);
2127 uint16_t levels
= DTRACE_LQUANTIZE_LEVELS(arg
);
2128 int32_t val
= (int32_t)nval
, level
;
2131 ASSERT(levels
!= 0);
2135 * This is an underflow.
2141 level
= (val
- base
) / step
;
2143 if (level
< levels
) {
2144 lquanta
[level
+ 1] += incr
;
2149 * This is an overflow.
2151 lquanta
[levels
+ 1] += incr
;
2155 dtrace_aggregate_llquantize_bucket(int16_t factor
, int16_t low
, int16_t high
,
2156 int16_t nsteps
, int64_t value
)
2158 int64_t this = 1, last
, next
;
2159 int base
= 1, order
;
2161 for (order
= 0; order
< low
; ++order
)
2165 * If our value is less than our factor taken to the power of the
2166 * low order of magnitude, it goes into the zeroth bucket.
2173 for (this *= factor
; order
<= high
; ++order
) {
2174 int nbuckets
= this > nsteps
? nsteps
: this;
2177 * We should not generally get log/linear quantizations
2178 * with a high magnitude that allows 64-bits to
2179 * overflow, but we nonetheless protect against this
2180 * by explicitly checking for overflow, and clamping
2181 * our value accordingly.
2183 next
= this * factor
;
2189 * If our value lies within this order of magnitude,
2190 * determine its position by taking the offset within
2191 * the order of magnitude, dividing by the bucket
2192 * width, and adding to our (accumulated) base.
2195 return (base
+ (value
- last
) / (this / nbuckets
));
2198 base
+= nbuckets
- (nbuckets
/ factor
);
2204 * Our value is greater than or equal to our factor taken to the
2205 * power of one plus the high magnitude -- return the top bucket.
2211 dtrace_aggregate_llquantize(uint64_t *llquanta
, uint64_t nval
, uint64_t incr
)
2213 uint64_t arg
= *llquanta
++;
2214 uint16_t factor
= DTRACE_LLQUANTIZE_FACTOR(arg
);
2215 uint16_t low
= DTRACE_LLQUANTIZE_LOW(arg
);
2216 uint16_t high
= DTRACE_LLQUANTIZE_HIGH(arg
);
2217 uint16_t nsteps
= DTRACE_LLQUANTIZE_NSTEP(arg
);
2219 llquanta
[dtrace_aggregate_llquantize_bucket(factor
, low
, high
, nsteps
, nval
)] += incr
;
2224 dtrace_aggregate_avg(uint64_t *data
, uint64_t nval
, uint64_t arg
)
2226 #pragma unused(arg) /* __APPLE__ */
2233 dtrace_aggregate_stddev(uint64_t *data
, uint64_t nval
, uint64_t arg
)
2235 #pragma unused(arg) /* __APPLE__ */
2236 int64_t snval
= (int64_t)nval
;
2243 * What we want to say here is:
2245 * data[2] += nval * nval;
2247 * But given that nval is 64-bit, we could easily overflow, so
2248 * we do this as 128-bit arithmetic.
2253 dtrace_multiply_128((uint64_t)snval
, (uint64_t)snval
, tmp
);
2254 dtrace_add_128(data
+ 2, tmp
, data
+ 2);
2259 dtrace_aggregate_count(uint64_t *oval
, uint64_t nval
, uint64_t arg
)
2261 #pragma unused(nval, arg) /* __APPLE__ */
2267 dtrace_aggregate_sum(uint64_t *oval
, uint64_t nval
, uint64_t arg
)
2269 #pragma unused(arg) /* __APPLE__ */
2274 * Aggregate given the tuple in the principal data buffer, and the aggregating
2275 * action denoted by the specified dtrace_aggregation_t. The aggregation
2276 * buffer is specified as the buf parameter. This routine does not return
2277 * failure; if there is no space in the aggregation buffer, the data will be
2278 * dropped, and a corresponding counter incremented.
2281 dtrace_aggregate(dtrace_aggregation_t
*agg
, dtrace_buffer_t
*dbuf
,
2282 intptr_t offset
, dtrace_buffer_t
*buf
, uint64_t expr
, uint64_t arg
)
2285 dtrace_recdesc_t
*rec
= &agg
->dtag_action
.dta_rec
;
2286 uint32_t i
, ndx
, size
, fsize
;
2287 uint32_t align
= sizeof (uint64_t) - 1;
2288 dtrace_aggbuffer_t
*agb
;
2289 dtrace_aggkey_t
*key
;
2290 uint32_t hashval
= 0, limit
, isstr
;
2291 caddr_t tomax
, data
, kdata
;
2292 dtrace_actkind_t action
;
2293 dtrace_action_t
*act
;
2299 if (!agg
->dtag_hasarg
) {
2301 * Currently, only quantize() and lquantize() take additional
2302 * arguments, and they have the same semantics: an increment
2303 * value that defaults to 1 when not present. If additional
2304 * aggregating actions take arguments, the setting of the
2305 * default argument value will presumably have to become more
2311 action
= agg
->dtag_action
.dta_kind
- DTRACEACT_AGGREGATION
;
2312 size
= rec
->dtrd_offset
- agg
->dtag_base
;
2313 fsize
= size
+ rec
->dtrd_size
;
2315 ASSERT(dbuf
->dtb_tomax
!= NULL
);
2316 data
= dbuf
->dtb_tomax
+ offset
+ agg
->dtag_base
;
2318 if ((tomax
= buf
->dtb_tomax
) == NULL
) {
2319 dtrace_buffer_drop(buf
);
2324 * The metastructure is always at the bottom of the buffer.
2326 agb
= (dtrace_aggbuffer_t
*)(tomax
+ buf
->dtb_size
-
2327 sizeof (dtrace_aggbuffer_t
));
2329 if (buf
->dtb_offset
== 0) {
2331 * We just kludge up approximately 1/8th of the size to be
2332 * buckets. If this guess ends up being routinely
2333 * off-the-mark, we may need to dynamically readjust this
2334 * based on past performance.
2336 uintptr_t hashsize
= (buf
->dtb_size
>> 3) / sizeof (uintptr_t);
2338 if ((uintptr_t)agb
- hashsize
* sizeof (dtrace_aggkey_t
*) <
2339 (uintptr_t)tomax
|| hashsize
== 0) {
2341 * We've been given a ludicrously small buffer;
2342 * increment our drop count and leave.
2344 dtrace_buffer_drop(buf
);
2349 * And now, a pathetic attempt to try to get a an odd (or
2350 * perchance, a prime) hash size for better hash distribution.
2352 if (hashsize
> (DTRACE_AGGHASHSIZE_SLEW
<< 3))
2353 hashsize
-= DTRACE_AGGHASHSIZE_SLEW
;
2355 agb
->dtagb_hashsize
= hashsize
;
2356 agb
->dtagb_hash
= (dtrace_aggkey_t
**)((uintptr_t)agb
-
2357 agb
->dtagb_hashsize
* sizeof (dtrace_aggkey_t
*));
2358 agb
->dtagb_free
= (uintptr_t)agb
->dtagb_hash
;
2360 for (i
= 0; i
< agb
->dtagb_hashsize
; i
++)
2361 agb
->dtagb_hash
[i
] = NULL
;
2364 ASSERT(agg
->dtag_first
!= NULL
);
2365 ASSERT(agg
->dtag_first
->dta_intuple
);
2368 * Calculate the hash value based on the key. Note that we _don't_
2369 * include the aggid in the hashing (but we will store it as part of
2370 * the key). The hashing algorithm is Bob Jenkins' "One-at-a-time"
2371 * algorithm: a simple, quick algorithm that has no known funnels, and
2372 * gets good distribution in practice. The efficacy of the hashing
2373 * algorithm (and a comparison with other algorithms) may be found by
2374 * running the ::dtrace_aggstat MDB dcmd.
2376 for (act
= agg
->dtag_first
; act
->dta_intuple
; act
= act
->dta_next
) {
2377 i
= act
->dta_rec
.dtrd_offset
- agg
->dtag_base
;
2378 limit
= i
+ act
->dta_rec
.dtrd_size
;
2379 ASSERT(limit
<= size
);
2380 isstr
= DTRACEACT_ISSTRING(act
);
2382 for (; i
< limit
; i
++) {
2384 hashval
+= (hashval
<< 10);
2385 hashval
^= (hashval
>> 6);
2387 if (isstr
&& data
[i
] == '\0')
2392 hashval
+= (hashval
<< 3);
2393 hashval
^= (hashval
>> 11);
2394 hashval
+= (hashval
<< 15);
2397 * Yes, the divide here is expensive -- but it's generally the least
2398 * of the performance issues given the amount of data that we iterate
2399 * over to compute hash values, compare data, etc.
2401 ndx
= hashval
% agb
->dtagb_hashsize
;
2403 for (key
= agb
->dtagb_hash
[ndx
]; key
!= NULL
; key
= key
->dtak_next
) {
2404 ASSERT((caddr_t
)key
>= tomax
);
2405 ASSERT((caddr_t
)key
< tomax
+ buf
->dtb_size
);
2407 if (hashval
!= key
->dtak_hashval
|| key
->dtak_size
!= size
)
2410 kdata
= key
->dtak_data
;
2411 ASSERT(kdata
>= tomax
&& kdata
< tomax
+ buf
->dtb_size
);
2413 for (act
= agg
->dtag_first
; act
->dta_intuple
;
2414 act
= act
->dta_next
) {
2415 i
= act
->dta_rec
.dtrd_offset
- agg
->dtag_base
;
2416 limit
= i
+ act
->dta_rec
.dtrd_size
;
2417 ASSERT(limit
<= size
);
2418 isstr
= DTRACEACT_ISSTRING(act
);
2420 for (; i
< limit
; i
++) {
2421 if (kdata
[i
] != data
[i
])
2424 if (isstr
&& data
[i
] == '\0')
2429 if (action
!= key
->dtak_action
) {
2431 * We are aggregating on the same value in the same
2432 * aggregation with two different aggregating actions.
2433 * (This should have been picked up in the compiler,
2434 * so we may be dealing with errant or devious DIF.)
2435 * This is an error condition; we indicate as much,
2438 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
2443 * This is a hit: we need to apply the aggregator to
2444 * the value at this key.
2446 agg
->dtag_aggregate((uint64_t *)(kdata
+ size
), expr
, arg
);
2453 * We didn't find it. We need to allocate some zero-filled space,
2454 * link it into the hash table appropriately, and apply the aggregator
2455 * to the (zero-filled) value.
2457 offs
= buf
->dtb_offset
;
2458 while (offs
& (align
- 1))
2459 offs
+= sizeof (uint32_t);
2462 * If we don't have enough room to both allocate a new key _and_
2463 * its associated data, increment the drop count and return.
2465 if ((uintptr_t)tomax
+ offs
+ fsize
>
2466 agb
->dtagb_free
- sizeof (dtrace_aggkey_t
)) {
2467 dtrace_buffer_drop(buf
);
2472 ASSERT(!(sizeof (dtrace_aggkey_t
) & (sizeof (uintptr_t) - 1)));
2473 key
= (dtrace_aggkey_t
*)(agb
->dtagb_free
- sizeof (dtrace_aggkey_t
));
2474 agb
->dtagb_free
-= sizeof (dtrace_aggkey_t
);
2476 key
->dtak_data
= kdata
= tomax
+ offs
;
2477 buf
->dtb_offset
= offs
+ fsize
;
2480 * Now copy the data across.
2482 *((dtrace_aggid_t
*)kdata
) = agg
->dtag_id
;
2484 for (i
= sizeof (dtrace_aggid_t
); i
< size
; i
++)
2488 * Because strings are not zeroed out by default, we need to iterate
2489 * looking for actions that store strings, and we need to explicitly
2490 * pad these strings out with zeroes.
2492 for (act
= agg
->dtag_first
; act
->dta_intuple
; act
= act
->dta_next
) {
2495 if (!DTRACEACT_ISSTRING(act
))
2498 i
= act
->dta_rec
.dtrd_offset
- agg
->dtag_base
;
2499 limit
= i
+ act
->dta_rec
.dtrd_size
;
2500 ASSERT(limit
<= size
);
2502 for (nul
= 0; i
< limit
; i
++) {
2508 if (data
[i
] != '\0')
2515 for (i
= size
; i
< fsize
; i
++)
2518 key
->dtak_hashval
= hashval
;
2519 key
->dtak_size
= size
;
2520 key
->dtak_action
= action
;
2521 key
->dtak_next
= agb
->dtagb_hash
[ndx
];
2522 agb
->dtagb_hash
[ndx
] = key
;
2525 * Finally, apply the aggregator.
2527 *((uint64_t *)(key
->dtak_data
+ size
)) = agg
->dtag_initial
;
2528 agg
->dtag_aggregate((uint64_t *)(key
->dtak_data
+ size
), expr
, arg
);
2532 * Given consumer state, this routine finds a speculation in the INACTIVE
2533 * state and transitions it into the ACTIVE state. If there is no speculation
2534 * in the INACTIVE state, 0 is returned. In this case, no error counter is
2535 * incremented -- it is up to the caller to take appropriate action.
2538 dtrace_speculation(dtrace_state_t
*state
)
2541 dtrace_speculation_state_t current
;
2542 uint32_t *stat
= &state
->dts_speculations_unavail
, count
;
2544 while (i
< state
->dts_nspeculations
) {
2545 dtrace_speculation_t
*spec
= &state
->dts_speculations
[i
];
2547 current
= spec
->dtsp_state
;
2549 if (current
!= DTRACESPEC_INACTIVE
) {
2550 if (current
== DTRACESPEC_COMMITTINGMANY
||
2551 current
== DTRACESPEC_COMMITTING
||
2552 current
== DTRACESPEC_DISCARDING
)
2553 stat
= &state
->dts_speculations_busy
;
2558 if (dtrace_cas32((uint32_t *)&spec
->dtsp_state
,
2559 current
, DTRACESPEC_ACTIVE
) == current
)
2564 * We couldn't find a speculation. If we found as much as a single
2565 * busy speculation buffer, we'll attribute this failure as "busy"
2566 * instead of "unavail".
2570 } while (dtrace_cas32(stat
, count
, count
+ 1) != count
);
2576 * This routine commits an active speculation. If the specified speculation
2577 * is not in a valid state to perform a commit(), this routine will silently do
2578 * nothing. The state of the specified speculation is transitioned according
2579 * to the state transition diagram outlined in <sys/dtrace_impl.h>
2582 dtrace_speculation_commit(dtrace_state_t
*state
, processorid_t cpu
,
2583 dtrace_specid_t which
)
2585 dtrace_speculation_t
*spec
;
2586 dtrace_buffer_t
*src
, *dest
;
2587 uintptr_t daddr
, saddr
, dlimit
, slimit
;
2588 dtrace_speculation_state_t current
, new = DTRACESPEC_INACTIVE
;
2595 if (which
> (dtrace_specid_t
)state
->dts_nspeculations
) {
2596 cpu_core
[cpu
].cpuc_dtrace_flags
|= CPU_DTRACE_ILLOP
;
2600 spec
= &state
->dts_speculations
[which
- 1];
2601 src
= &spec
->dtsp_buffer
[cpu
];
2602 dest
= &state
->dts_buffer
[cpu
];
2605 current
= spec
->dtsp_state
;
2607 if (current
== DTRACESPEC_COMMITTINGMANY
)
2611 case DTRACESPEC_INACTIVE
:
2612 case DTRACESPEC_DISCARDING
:
2615 case DTRACESPEC_COMMITTING
:
2617 * This is only possible if we are (a) commit()'ing
2618 * without having done a prior speculate() on this CPU
2619 * and (b) racing with another commit() on a different
2620 * CPU. There's nothing to do -- we just assert that
2623 ASSERT(src
->dtb_offset
== 0);
2626 case DTRACESPEC_ACTIVE
:
2627 new = DTRACESPEC_COMMITTING
;
2630 case DTRACESPEC_ACTIVEONE
:
2632 * This speculation is active on one CPU. If our
2633 * buffer offset is non-zero, we know that the one CPU
2634 * must be us. Otherwise, we are committing on a
2635 * different CPU from the speculate(), and we must
2636 * rely on being asynchronously cleaned.
2638 if (src
->dtb_offset
!= 0) {
2639 new = DTRACESPEC_COMMITTING
;
2644 case DTRACESPEC_ACTIVEMANY
:
2645 new = DTRACESPEC_COMMITTINGMANY
;
2651 } while (dtrace_cas32((uint32_t *)&spec
->dtsp_state
,
2652 current
, new) != current
);
2655 * We have set the state to indicate that we are committing this
2656 * speculation. Now reserve the necessary space in the destination
2659 if ((offs
= dtrace_buffer_reserve(dest
, src
->dtb_offset
,
2660 sizeof (uint64_t), state
, NULL
)) < 0) {
2661 dtrace_buffer_drop(dest
);
2666 * We have sufficient space to copy the speculative buffer into the
2667 * primary buffer. First, modify the speculative buffer, filling
2668 * in the timestamp of all entries with the current time. The data
2669 * must have the commit() time rather than the time it was traced,
2670 * so that all entries in the primary buffer are in timestamp order.
2672 timestamp
= dtrace_gethrtime();
2673 saddr
= (uintptr_t)src
->dtb_tomax
;
2674 slimit
= saddr
+ src
->dtb_offset
;
2675 while (saddr
< slimit
) {
2677 dtrace_rechdr_t
*dtrh
= (dtrace_rechdr_t
*)saddr
;
2679 if (dtrh
->dtrh_epid
== DTRACE_EPIDNONE
) {
2680 saddr
+= sizeof (dtrace_epid_t
);
2684 ASSERT(dtrh
->dtrh_epid
<= ((dtrace_epid_t
) state
->dts_necbs
));
2685 size
= state
->dts_ecbs
[dtrh
->dtrh_epid
- 1]->dte_size
;
2687 ASSERT(saddr
+ size
<= slimit
);
2688 ASSERT(size
>= sizeof(dtrace_rechdr_t
));
2689 ASSERT(DTRACE_RECORD_LOAD_TIMESTAMP(dtrh
) == UINT64_MAX
);
2691 DTRACE_RECORD_STORE_TIMESTAMP(dtrh
, timestamp
);
2697 * Copy the buffer across. (Note that this is a
2698 * highly subobtimal bcopy(); in the unlikely event that this becomes
2699 * a serious performance issue, a high-performance DTrace-specific
2700 * bcopy() should obviously be invented.)
2702 daddr
= (uintptr_t)dest
->dtb_tomax
+ offs
;
2703 dlimit
= daddr
+ src
->dtb_offset
;
2704 saddr
= (uintptr_t)src
->dtb_tomax
;
2707 * First, the aligned portion.
2709 while (dlimit
- daddr
>= sizeof (uint64_t)) {
2710 *((uint64_t *)daddr
) = *((uint64_t *)saddr
);
2712 daddr
+= sizeof (uint64_t);
2713 saddr
+= sizeof (uint64_t);
2717 * Now any left-over bit...
2719 while (dlimit
- daddr
)
2720 *((uint8_t *)daddr
++) = *((uint8_t *)saddr
++);
2723 * Finally, commit the reserved space in the destination buffer.
2725 dest
->dtb_offset
= offs
+ src
->dtb_offset
;
2729 * If we're lucky enough to be the only active CPU on this speculation
2730 * buffer, we can just set the state back to DTRACESPEC_INACTIVE.
2732 if (current
== DTRACESPEC_ACTIVE
||
2733 (current
== DTRACESPEC_ACTIVEONE
&& new == DTRACESPEC_COMMITTING
)) {
2734 uint32_t rval
= dtrace_cas32((uint32_t *)&spec
->dtsp_state
,
2735 DTRACESPEC_COMMITTING
, DTRACESPEC_INACTIVE
);
2736 #pragma unused(rval) /* __APPLE__ */
2738 ASSERT(rval
== DTRACESPEC_COMMITTING
);
2741 src
->dtb_offset
= 0;
2742 src
->dtb_xamot_drops
+= src
->dtb_drops
;
2747 * This routine discards an active speculation. If the specified speculation
2748 * is not in a valid state to perform a discard(), this routine will silently
2749 * do nothing. The state of the specified speculation is transitioned
2750 * according to the state transition diagram outlined in <sys/dtrace_impl.h>
2753 dtrace_speculation_discard(dtrace_state_t
*state
, processorid_t cpu
,
2754 dtrace_specid_t which
)
2756 dtrace_speculation_t
*spec
;
2757 dtrace_speculation_state_t current
, new = DTRACESPEC_INACTIVE
;
2758 dtrace_buffer_t
*buf
;
2763 if (which
> (dtrace_specid_t
)state
->dts_nspeculations
) {
2764 cpu_core
[cpu
].cpuc_dtrace_flags
|= CPU_DTRACE_ILLOP
;
2768 spec
= &state
->dts_speculations
[which
- 1];
2769 buf
= &spec
->dtsp_buffer
[cpu
];
2772 current
= spec
->dtsp_state
;
2775 case DTRACESPEC_INACTIVE
:
2776 case DTRACESPEC_COMMITTINGMANY
:
2777 case DTRACESPEC_COMMITTING
:
2778 case DTRACESPEC_DISCARDING
:
2781 case DTRACESPEC_ACTIVE
:
2782 case DTRACESPEC_ACTIVEMANY
:
2783 new = DTRACESPEC_DISCARDING
;
2786 case DTRACESPEC_ACTIVEONE
:
2787 if (buf
->dtb_offset
!= 0) {
2788 new = DTRACESPEC_INACTIVE
;
2790 new = DTRACESPEC_DISCARDING
;
2797 } while (dtrace_cas32((uint32_t *)&spec
->dtsp_state
,
2798 current
, new) != current
);
2800 buf
->dtb_offset
= 0;
2805 * Note: not called from probe context. This function is called
2806 * asynchronously from cross call context to clean any speculations that are
2807 * in the COMMITTINGMANY or DISCARDING states. These speculations may not be
2808 * transitioned back to the INACTIVE state until all CPUs have cleaned the
2812 dtrace_speculation_clean_here(dtrace_state_t
*state
)
2814 dtrace_icookie_t cookie
;
2815 processorid_t cpu
= CPU
->cpu_id
;
2816 dtrace_buffer_t
*dest
= &state
->dts_buffer
[cpu
];
2819 cookie
= dtrace_interrupt_disable();
2821 if (dest
->dtb_tomax
== NULL
) {
2822 dtrace_interrupt_enable(cookie
);
2826 for (i
= 0; i
< (dtrace_specid_t
)state
->dts_nspeculations
; i
++) {
2827 dtrace_speculation_t
*spec
= &state
->dts_speculations
[i
];
2828 dtrace_buffer_t
*src
= &spec
->dtsp_buffer
[cpu
];
2830 if (src
->dtb_tomax
== NULL
)
2833 if (spec
->dtsp_state
== DTRACESPEC_DISCARDING
) {
2834 src
->dtb_offset
= 0;
2838 if (spec
->dtsp_state
!= DTRACESPEC_COMMITTINGMANY
)
2841 if (src
->dtb_offset
== 0)
2844 dtrace_speculation_commit(state
, cpu
, i
+ 1);
2847 dtrace_interrupt_enable(cookie
);
2851 * Note: not called from probe context. This function is called
2852 * asynchronously (and at a regular interval) to clean any speculations that
2853 * are in the COMMITTINGMANY or DISCARDING states. If it discovers that there
2854 * is work to be done, it cross calls all CPUs to perform that work;
2855 * COMMITMANY and DISCARDING speculations may not be transitioned back to the
2856 * INACTIVE state until they have been cleaned by all CPUs.
2859 dtrace_speculation_clean(dtrace_state_t
*state
)
2865 for (i
= 0; i
< (dtrace_specid_t
)state
->dts_nspeculations
; i
++) {
2866 dtrace_speculation_t
*spec
= &state
->dts_speculations
[i
];
2868 ASSERT(!spec
->dtsp_cleaning
);
2870 if (spec
->dtsp_state
!= DTRACESPEC_DISCARDING
&&
2871 spec
->dtsp_state
!= DTRACESPEC_COMMITTINGMANY
)
2875 spec
->dtsp_cleaning
= 1;
2881 dtrace_xcall(DTRACE_CPUALL
,
2882 (dtrace_xcall_t
)dtrace_speculation_clean_here
, state
);
2885 * We now know that all CPUs have committed or discarded their
2886 * speculation buffers, as appropriate. We can now set the state
2889 for (i
= 0; i
< (dtrace_specid_t
)state
->dts_nspeculations
; i
++) {
2890 dtrace_speculation_t
*spec
= &state
->dts_speculations
[i
];
2891 dtrace_speculation_state_t current
, new;
2893 if (!spec
->dtsp_cleaning
)
2896 current
= spec
->dtsp_state
;
2897 ASSERT(current
== DTRACESPEC_DISCARDING
||
2898 current
== DTRACESPEC_COMMITTINGMANY
);
2900 new = DTRACESPEC_INACTIVE
;
2902 rv
= dtrace_cas32((uint32_t *)&spec
->dtsp_state
, current
, new);
2903 ASSERT(rv
== current
);
2904 spec
->dtsp_cleaning
= 0;
2909 * Called as part of a speculate() to get the speculative buffer associated
2910 * with a given speculation. Returns NULL if the specified speculation is not
2911 * in an ACTIVE state. If the speculation is in the ACTIVEONE state -- and
2912 * the active CPU is not the specified CPU -- the speculation will be
2913 * atomically transitioned into the ACTIVEMANY state.
2915 static dtrace_buffer_t
*
2916 dtrace_speculation_buffer(dtrace_state_t
*state
, processorid_t cpuid
,
2917 dtrace_specid_t which
)
2919 dtrace_speculation_t
*spec
;
2920 dtrace_speculation_state_t current
, new = DTRACESPEC_INACTIVE
;
2921 dtrace_buffer_t
*buf
;
2926 if (which
> (dtrace_specid_t
)state
->dts_nspeculations
) {
2927 cpu_core
[cpuid
].cpuc_dtrace_flags
|= CPU_DTRACE_ILLOP
;
2931 spec
= &state
->dts_speculations
[which
- 1];
2932 buf
= &spec
->dtsp_buffer
[cpuid
];
2935 current
= spec
->dtsp_state
;
2938 case DTRACESPEC_INACTIVE
:
2939 case DTRACESPEC_COMMITTINGMANY
:
2940 case DTRACESPEC_DISCARDING
:
2943 case DTRACESPEC_COMMITTING
:
2944 ASSERT(buf
->dtb_offset
== 0);
2947 case DTRACESPEC_ACTIVEONE
:
2949 * This speculation is currently active on one CPU.
2950 * Check the offset in the buffer; if it's non-zero,
2951 * that CPU must be us (and we leave the state alone).
2952 * If it's zero, assume that we're starting on a new
2953 * CPU -- and change the state to indicate that the
2954 * speculation is active on more than one CPU.
2956 if (buf
->dtb_offset
!= 0)
2959 new = DTRACESPEC_ACTIVEMANY
;
2962 case DTRACESPEC_ACTIVEMANY
:
2965 case DTRACESPEC_ACTIVE
:
2966 new = DTRACESPEC_ACTIVEONE
;
2972 } while (dtrace_cas32((uint32_t *)&spec
->dtsp_state
,
2973 current
, new) != current
);
2975 ASSERT(new == DTRACESPEC_ACTIVEONE
|| new == DTRACESPEC_ACTIVEMANY
);
2980 * Return a string. In the event that the user lacks the privilege to access
2981 * arbitrary kernel memory, we copy the string out to scratch memory so that we
2982 * don't fail access checking.
2984 * dtrace_dif_variable() uses this routine as a helper for various
2985 * builtin values such as 'execname' and 'probefunc.'
2989 dtrace_dif_varstr(uintptr_t addr
, dtrace_state_t
*state
,
2990 dtrace_mstate_t
*mstate
)
2992 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
2997 * The easy case: this probe is allowed to read all of memory, so
2998 * we can just return this as a vanilla pointer.
3000 if ((mstate
->dtms_access
& DTRACE_ACCESS_KERNEL
) != 0)
3004 * This is the tougher case: we copy the string in question from
3005 * kernel memory into scratch memory and return it that way: this
3006 * ensures that we won't trip up when access checking tests the
3007 * BYREF return value.
3009 strsz
= dtrace_strlen((char *)addr
, size
) + 1;
3011 if (mstate
->dtms_scratch_ptr
+ strsz
>
3012 mstate
->dtms_scratch_base
+ mstate
->dtms_scratch_size
) {
3013 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
3017 dtrace_strcpy((const void *)addr
, (void *)mstate
->dtms_scratch_ptr
,
3019 ret
= mstate
->dtms_scratch_ptr
;
3020 mstate
->dtms_scratch_ptr
+= strsz
;
3025 * This function implements the DIF emulator's variable lookups. The emulator
3026 * passes a reserved variable identifier and optional built-in array index.
3029 dtrace_dif_variable(dtrace_mstate_t
*mstate
, dtrace_state_t
*state
, uint64_t v
,
3033 * If we're accessing one of the uncached arguments, we'll turn this
3034 * into a reference in the args array.
3036 if (v
>= DIF_VAR_ARG0
&& v
<= DIF_VAR_ARG9
) {
3037 ndx
= v
- DIF_VAR_ARG0
;
3043 ASSERT(mstate
->dtms_present
& DTRACE_MSTATE_ARGS
);
3044 if (ndx
>= sizeof (mstate
->dtms_arg
) /
3045 sizeof (mstate
->dtms_arg
[0])) {
3047 * APPLE NOTE: Account for introduction of __dtrace_probe()
3049 int aframes
= mstate
->dtms_probe
->dtpr_aframes
+ 3;
3050 dtrace_provider_t
*pv
;
3053 pv
= mstate
->dtms_probe
->dtpr_provider
;
3054 if (pv
->dtpv_pops
.dtps_getargval
!= NULL
)
3055 val
= pv
->dtpv_pops
.dtps_getargval(pv
->dtpv_arg
,
3056 mstate
->dtms_probe
->dtpr_id
,
3057 mstate
->dtms_probe
->dtpr_arg
, ndx
, aframes
);
3058 /* Special case access of arg5 as passed to dtrace_probe_error() (which see.) */
3059 else if (mstate
->dtms_probe
->dtpr_id
== dtrace_probeid_error
&& ndx
== 5) {
3060 return ((dtrace_state_t
*)(uintptr_t)(mstate
->dtms_arg
[0]))->dts_arg_error_illval
;
3064 val
= dtrace_getarg(ndx
, aframes
);
3067 * This is regrettably required to keep the compiler
3068 * from tail-optimizing the call to dtrace_getarg().
3069 * The condition always evaluates to true, but the
3070 * compiler has no way of figuring that out a priori.
3071 * (None of this would be necessary if the compiler
3072 * could be relied upon to _always_ tail-optimize
3073 * the call to dtrace_getarg() -- but it can't.)
3075 if (mstate
->dtms_probe
!= NULL
)
3081 return (mstate
->dtms_arg
[ndx
]);
3083 case DIF_VAR_UREGS
: {
3086 if (!dtrace_priv_proc(state
))
3089 if ((thread
= current_thread()) == NULL
) {
3090 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
3091 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= 0;
3095 return (dtrace_getreg(find_user_regs(thread
), ndx
));
3099 case DIF_VAR_CURTHREAD
:
3100 if (!dtrace_priv_kernel(state
))
3103 return ((uint64_t)(uintptr_t)current_thread());
3105 case DIF_VAR_TIMESTAMP
:
3106 if (!(mstate
->dtms_present
& DTRACE_MSTATE_TIMESTAMP
)) {
3107 mstate
->dtms_timestamp
= dtrace_gethrtime();
3108 mstate
->dtms_present
|= DTRACE_MSTATE_TIMESTAMP
;
3110 return (mstate
->dtms_timestamp
);
3112 case DIF_VAR_VTIMESTAMP
:
3113 ASSERT(dtrace_vtime_references
!= 0);
3114 return (dtrace_get_thread_vtime(current_thread()));
3116 case DIF_VAR_WALLTIMESTAMP
:
3117 if (!(mstate
->dtms_present
& DTRACE_MSTATE_WALLTIMESTAMP
)) {
3118 mstate
->dtms_walltimestamp
= dtrace_gethrestime();
3119 mstate
->dtms_present
|= DTRACE_MSTATE_WALLTIMESTAMP
;
3121 return (mstate
->dtms_walltimestamp
);
3123 case DIF_VAR_MACHTIMESTAMP
:
3124 if (!(mstate
->dtms_present
& DTRACE_MSTATE_MACHTIMESTAMP
)) {
3125 mstate
->dtms_machtimestamp
= mach_absolute_time();
3126 mstate
->dtms_present
|= DTRACE_MSTATE_MACHTIMESTAMP
;
3128 return (mstate
->dtms_machtimestamp
);
3131 if (!dtrace_priv_kernel(state
))
3133 if (!(mstate
->dtms_present
& DTRACE_MSTATE_IPL
)) {
3134 mstate
->dtms_ipl
= dtrace_getipl();
3135 mstate
->dtms_present
|= DTRACE_MSTATE_IPL
;
3137 return (mstate
->dtms_ipl
);
3140 ASSERT(mstate
->dtms_present
& DTRACE_MSTATE_EPID
);
3141 return (mstate
->dtms_epid
);
3144 ASSERT(mstate
->dtms_present
& DTRACE_MSTATE_PROBE
);
3145 return (mstate
->dtms_probe
->dtpr_id
);
3147 case DIF_VAR_STACKDEPTH
:
3148 if (!dtrace_priv_kernel(state
))
3150 if (!(mstate
->dtms_present
& DTRACE_MSTATE_STACKDEPTH
)) {
3152 * APPLE NOTE: Account for introduction of __dtrace_probe()
3154 int aframes
= mstate
->dtms_probe
->dtpr_aframes
+ 3;
3156 mstate
->dtms_stackdepth
= dtrace_getstackdepth(aframes
);
3157 mstate
->dtms_present
|= DTRACE_MSTATE_STACKDEPTH
;
3159 return (mstate
->dtms_stackdepth
);
3161 case DIF_VAR_USTACKDEPTH
:
3162 if (!dtrace_priv_proc(state
))
3164 if (!(mstate
->dtms_present
& DTRACE_MSTATE_USTACKDEPTH
)) {
3166 * See comment in DIF_VAR_PID.
3168 if (DTRACE_ANCHORED(mstate
->dtms_probe
) &&
3170 mstate
->dtms_ustackdepth
= 0;
3172 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
3173 mstate
->dtms_ustackdepth
=
3174 dtrace_getustackdepth();
3175 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
3177 mstate
->dtms_present
|= DTRACE_MSTATE_USTACKDEPTH
;
3179 return (mstate
->dtms_ustackdepth
);
3181 case DIF_VAR_CALLER
:
3182 if (!dtrace_priv_kernel(state
))
3184 if (!(mstate
->dtms_present
& DTRACE_MSTATE_CALLER
)) {
3186 * APPLE NOTE: Account for introduction of __dtrace_probe()
3188 int aframes
= mstate
->dtms_probe
->dtpr_aframes
+ 3;
3190 if (!DTRACE_ANCHORED(mstate
->dtms_probe
)) {
3192 * If this is an unanchored probe, we are
3193 * required to go through the slow path:
3194 * dtrace_caller() only guarantees correct
3195 * results for anchored probes.
3199 dtrace_getpcstack(caller
, 2, aframes
,
3200 (uint32_t *)(uintptr_t)mstate
->dtms_arg
[0]);
3201 mstate
->dtms_caller
= caller
[1];
3202 } else if ((mstate
->dtms_caller
=
3203 dtrace_caller(aframes
)) == (uintptr_t)-1) {
3205 * We have failed to do this the quick way;
3206 * we must resort to the slower approach of
3207 * calling dtrace_getpcstack().
3211 dtrace_getpcstack(&caller
, 1, aframes
, NULL
);
3212 mstate
->dtms_caller
= caller
;
3215 mstate
->dtms_present
|= DTRACE_MSTATE_CALLER
;
3217 return (mstate
->dtms_caller
);
3219 case DIF_VAR_UCALLER
:
3220 if (!dtrace_priv_proc(state
))
3223 if (!(mstate
->dtms_present
& DTRACE_MSTATE_UCALLER
)) {
3227 * dtrace_getupcstack() fills in the first uint64_t
3228 * with the current PID. The second uint64_t will
3229 * be the program counter at user-level. The third
3230 * uint64_t will contain the caller, which is what
3234 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
3235 dtrace_getupcstack(ustack
, 3);
3236 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
3237 mstate
->dtms_ucaller
= ustack
[2];
3238 mstate
->dtms_present
|= DTRACE_MSTATE_UCALLER
;
3241 return (mstate
->dtms_ucaller
);
3243 case DIF_VAR_PROBEPROV
:
3244 ASSERT(mstate
->dtms_present
& DTRACE_MSTATE_PROBE
);
3245 return (dtrace_dif_varstr(
3246 (uintptr_t)mstate
->dtms_probe
->dtpr_provider
->dtpv_name
,
3249 case DIF_VAR_PROBEMOD
:
3250 ASSERT(mstate
->dtms_present
& DTRACE_MSTATE_PROBE
);
3251 return (dtrace_dif_varstr(
3252 (uintptr_t)mstate
->dtms_probe
->dtpr_mod
,
3255 case DIF_VAR_PROBEFUNC
:
3256 ASSERT(mstate
->dtms_present
& DTRACE_MSTATE_PROBE
);
3257 return (dtrace_dif_varstr(
3258 (uintptr_t)mstate
->dtms_probe
->dtpr_func
,
3261 case DIF_VAR_PROBENAME
:
3262 ASSERT(mstate
->dtms_present
& DTRACE_MSTATE_PROBE
);
3263 return (dtrace_dif_varstr(
3264 (uintptr_t)mstate
->dtms_probe
->dtpr_name
,
3268 if (!dtrace_priv_proc_relaxed(state
))
3272 * Note that we are assuming that an unanchored probe is
3273 * always due to a high-level interrupt. (And we're assuming
3274 * that there is only a single high level interrupt.)
3276 if (DTRACE_ANCHORED(mstate
->dtms_probe
) && CPU_ON_INTR(CPU
))
3277 /* Anchored probe that fires while on an interrupt accrues to process 0 */
3280 return ((uint64_t)dtrace_proc_selfpid());
3283 if (!dtrace_priv_proc_relaxed(state
))
3287 * See comment in DIF_VAR_PID.
3289 if (DTRACE_ANCHORED(mstate
->dtms_probe
) && CPU_ON_INTR(CPU
))
3292 return ((uint64_t)dtrace_proc_selfppid());
3295 /* We do not need to check for null current_thread() */
3296 return thread_tid(current_thread()); /* globally unique */
3298 case DIF_VAR_PTHREAD_SELF
:
3299 if (!dtrace_priv_proc(state
))
3302 /* Not currently supported, but we should be able to delta the dispatchqaddr and dispatchqoffset to get pthread_self */
3305 case DIF_VAR_DISPATCHQADDR
:
3306 if (!dtrace_priv_proc(state
))
3309 /* We do not need to check for null current_thread() */
3310 return thread_dispatchqaddr(current_thread());
3312 case DIF_VAR_EXECNAME
:
3314 char *xname
= (char *)mstate
->dtms_scratch_ptr
;
3315 size_t scratch_size
= MAXCOMLEN
+1;
3317 /* The scratch allocation's lifetime is that of the clause. */
3318 if (!DTRACE_INSCRATCH(mstate
, scratch_size
)) {
3319 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
3323 if (!dtrace_priv_proc_relaxed(state
))
3326 mstate
->dtms_scratch_ptr
+= scratch_size
;
3327 proc_selfname( xname
, MAXCOMLEN
);
3329 return ((uint64_t)(uintptr_t)xname
);
3333 case DIF_VAR_ZONENAME
:
3335 /* scratch_size is equal to length('global') + 1 for the null-terminator. */
3336 char *zname
= (char *)mstate
->dtms_scratch_ptr
;
3337 size_t scratch_size
= 6 + 1;
3339 if (!dtrace_priv_proc(state
))
3342 /* The scratch allocation's lifetime is that of the clause. */
3343 if (!DTRACE_INSCRATCH(mstate
, scratch_size
)) {
3344 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
3348 mstate
->dtms_scratch_ptr
+= scratch_size
;
3350 /* The kernel does not provide zonename, it will always return 'global'. */
3351 strlcpy(zname
, "global", scratch_size
);
3353 return ((uint64_t)(uintptr_t)zname
);
3357 if (!dtrace_priv_proc_relaxed(state
))
3361 * See comment in DIF_VAR_PID.
3363 if (DTRACE_ANCHORED(mstate
->dtms_probe
) && CPU_ON_INTR(CPU
))
3366 return ((uint64_t) dtrace_proc_selfruid());
3369 if (!dtrace_priv_proc(state
))
3373 * See comment in DIF_VAR_PID.
3375 if (DTRACE_ANCHORED(mstate
->dtms_probe
) && CPU_ON_INTR(CPU
))
3378 if (dtrace_CRED() != NULL
)
3379 /* Credential does not require lazy initialization. */
3380 return ((uint64_t)kauth_getgid());
3382 /* proc_lock would be taken under kauth_cred_proc_ref() in kauth_cred_get(). */
3383 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
3387 case DIF_VAR_ERRNO
: {
3388 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
3389 if (!dtrace_priv_proc(state
))
3393 * See comment in DIF_VAR_PID.
3395 if (DTRACE_ANCHORED(mstate
->dtms_probe
) && CPU_ON_INTR(CPU
))
3399 return (uint64_t)uthread
->t_dtrace_errno
;
3401 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
3407 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
3413 * Emulate the execution of DTrace ID subroutines invoked by the call opcode.
3414 * Notice that we don't bother validating the proper number of arguments or
3415 * their types in the tuple stack. This isn't needed because all argument
3416 * interpretation is safe because of our load safety -- the worst that can
3417 * happen is that a bogus program can obtain bogus results.
3420 dtrace_dif_subr(uint_t subr
, uint_t rd
, uint64_t *regs
,
3421 dtrace_key_t
*tupregs
, int nargs
,
3422 dtrace_mstate_t
*mstate
, dtrace_state_t
*state
)
3424 volatile uint16_t *flags
= &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
3425 volatile uint64_t *illval
= &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
;
3426 dtrace_vstate_t
*vstate
= &state
->dts_vstate
;
3428 #if !defined(__APPLE__)
3439 /* FIXME: awaits lock/mutex work */
3440 #endif /* __APPLE__ */
3444 regs
[rd
] = (dtrace_gethrtime() * 2416 + 374441) % 1771875;
3447 #if !defined(__APPLE__)
3448 case DIF_SUBR_MUTEX_OWNED
:
3449 if (!dtrace_canload(tupregs
[0].dttk_value
, sizeof (kmutex_t
),
3455 m
.mx
= dtrace_load64(tupregs
[0].dttk_value
);
3456 if (MUTEX_TYPE_ADAPTIVE(&m
.mi
))
3457 regs
[rd
] = MUTEX_OWNER(&m
.mi
) != MUTEX_NO_OWNER
;
3459 regs
[rd
] = LOCK_HELD(&m
.mi
.m_spin
.m_spinlock
);
3462 case DIF_SUBR_MUTEX_OWNER
:
3463 if (!dtrace_canload(tupregs
[0].dttk_value
, sizeof (kmutex_t
),
3469 m
.mx
= dtrace_load64(tupregs
[0].dttk_value
);
3470 if (MUTEX_TYPE_ADAPTIVE(&m
.mi
) &&
3471 MUTEX_OWNER(&m
.mi
) != MUTEX_NO_OWNER
)
3472 regs
[rd
] = (uintptr_t)MUTEX_OWNER(&m
.mi
);
3477 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE
:
3478 if (!dtrace_canload(tupregs
[0].dttk_value
, sizeof (kmutex_t
),
3484 m
.mx
= dtrace_load64(tupregs
[0].dttk_value
);
3485 regs
[rd
] = MUTEX_TYPE_ADAPTIVE(&m
.mi
);
3488 case DIF_SUBR_MUTEX_TYPE_SPIN
:
3489 if (!dtrace_canload(tupregs
[0].dttk_value
, sizeof (kmutex_t
),
3495 m
.mx
= dtrace_load64(tupregs
[0].dttk_value
);
3496 regs
[rd
] = MUTEX_TYPE_SPIN(&m
.mi
);
3499 case DIF_SUBR_RW_READ_HELD
: {
3502 if (!dtrace_canload(tupregs
[0].dttk_value
, sizeof (uintptr_t),
3508 r
.rw
= dtrace_loadptr(tupregs
[0].dttk_value
);
3509 regs
[rd
] = _RW_READ_HELD(&r
.ri
, tmp
);
3513 case DIF_SUBR_RW_WRITE_HELD
:
3514 if (!dtrace_canload(tupregs
[0].dttk_value
, sizeof (krwlock_t
),
3520 r
.rw
= dtrace_loadptr(tupregs
[0].dttk_value
);
3521 regs
[rd
] = _RW_WRITE_HELD(&r
.ri
);
3524 case DIF_SUBR_RW_ISWRITER
:
3525 if (!dtrace_canload(tupregs
[0].dttk_value
, sizeof (krwlock_t
),
3531 r
.rw
= dtrace_loadptr(tupregs
[0].dttk_value
);
3532 regs
[rd
] = _RW_ISWRITER(&r
.ri
);
3535 /* FIXME: awaits lock/mutex work */
3536 #endif /* __APPLE__ */
3538 case DIF_SUBR_BCOPY
: {
3540 * We need to be sure that the destination is in the scratch
3541 * region -- no other region is allowed.
3543 uintptr_t src
= tupregs
[0].dttk_value
;
3544 uintptr_t dest
= tupregs
[1].dttk_value
;
3545 size_t size
= tupregs
[2].dttk_value
;
3547 if (!dtrace_inscratch(dest
, size
, mstate
)) {
3548 *flags
|= CPU_DTRACE_BADADDR
;
3553 if (!dtrace_canload(src
, size
, mstate
, vstate
)) {
3558 dtrace_bcopy((void *)src
, (void *)dest
, size
);
3562 case DIF_SUBR_ALLOCA
:
3563 case DIF_SUBR_COPYIN
: {
3564 uintptr_t dest
= P2ROUNDUP(mstate
->dtms_scratch_ptr
, 8);
3566 tupregs
[subr
== DIF_SUBR_ALLOCA
? 0 : 1].dttk_value
;
3567 size_t scratch_size
= (dest
- mstate
->dtms_scratch_ptr
) + size
;
3570 * This action doesn't require any credential checks since
3571 * probes will not activate in user contexts to which the
3572 * enabling user does not have permissions.
3576 * Rounding up the user allocation size could have overflowed
3577 * a large, bogus allocation (like -1ULL) to 0.
3579 if (scratch_size
< size
||
3580 !DTRACE_INSCRATCH(mstate
, scratch_size
)) {
3581 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
3586 if (subr
== DIF_SUBR_COPYIN
) {
3587 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
3588 if (dtrace_priv_proc(state
))
3589 dtrace_copyin(tupregs
[0].dttk_value
, dest
, size
, flags
);
3590 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
3593 mstate
->dtms_scratch_ptr
+= scratch_size
;
3598 case DIF_SUBR_COPYINTO
: {
3599 uint64_t size
= tupregs
[1].dttk_value
;
3600 uintptr_t dest
= tupregs
[2].dttk_value
;
3603 * This action doesn't require any credential checks since
3604 * probes will not activate in user contexts to which the
3605 * enabling user does not have permissions.
3607 if (!dtrace_inscratch(dest
, size
, mstate
)) {
3608 *flags
|= CPU_DTRACE_BADADDR
;
3613 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
3614 if (dtrace_priv_proc(state
))
3615 dtrace_copyin(tupregs
[0].dttk_value
, dest
, size
, flags
);
3616 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
3620 case DIF_SUBR_COPYINSTR
: {
3621 uintptr_t dest
= mstate
->dtms_scratch_ptr
;
3622 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
3624 if (nargs
> 1 && tupregs
[1].dttk_value
< size
)
3625 size
= tupregs
[1].dttk_value
+ 1;
3628 * This action doesn't require any credential checks since
3629 * probes will not activate in user contexts to which the
3630 * enabling user does not have permissions.
3632 if (!DTRACE_INSCRATCH(mstate
, size
)) {
3633 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
3638 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
3639 if (dtrace_priv_proc(state
))
3640 dtrace_copyinstr(tupregs
[0].dttk_value
, dest
, size
, flags
);
3641 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
3643 ((char *)dest
)[size
- 1] = '\0';
3644 mstate
->dtms_scratch_ptr
+= size
;
3649 case DIF_SUBR_MSGSIZE
:
3650 case DIF_SUBR_MSGDSIZE
: {
3651 /* Darwin does not implement SysV streams messages */
3652 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
3657 case DIF_SUBR_PROGENYOF
: {
3658 pid_t pid
= tupregs
[0].dttk_value
;
3659 struct proc
*p
= current_proc();
3660 int rval
= 0, lim
= nprocs
;
3662 while(p
&& (lim
-- > 0)) {
3665 ppid
= (pid_t
)dtrace_load32((uintptr_t)&(p
->p_pid
));
3666 if (*flags
& CPU_DTRACE_FAULT
)
3675 break; /* Can't climb process tree any further. */
3677 p
= (struct proc
*)dtrace_loadptr((uintptr_t)&(p
->p_pptr
));
3678 if (*flags
& CPU_DTRACE_FAULT
)
3686 case DIF_SUBR_SPECULATION
:
3687 regs
[rd
] = dtrace_speculation(state
);
3691 case DIF_SUBR_COPYOUT
: {
3692 uintptr_t kaddr
= tupregs
[0].dttk_value
;
3693 user_addr_t uaddr
= tupregs
[1].dttk_value
;
3694 uint64_t size
= tupregs
[2].dttk_value
;
3696 if (!dtrace_destructive_disallow
&&
3697 dtrace_priv_proc_control(state
) &&
3698 !dtrace_istoxic(kaddr
, size
)) {
3699 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
3700 dtrace_copyout(kaddr
, uaddr
, size
, flags
);
3701 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
3706 case DIF_SUBR_COPYOUTSTR
: {
3707 uintptr_t kaddr
= tupregs
[0].dttk_value
;
3708 user_addr_t uaddr
= tupregs
[1].dttk_value
;
3709 uint64_t size
= tupregs
[2].dttk_value
;
3711 if (!dtrace_destructive_disallow
&&
3712 dtrace_priv_proc_control(state
) &&
3713 !dtrace_istoxic(kaddr
, size
)) {
3714 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
3715 dtrace_copyoutstr(kaddr
, uaddr
, size
, flags
);
3716 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
3721 case DIF_SUBR_STRLEN
: {
3723 uintptr_t addr
= (uintptr_t)tupregs
[0].dttk_value
;
3724 sz
= dtrace_strlen((char *)addr
,
3725 state
->dts_options
[DTRACEOPT_STRSIZE
]);
3727 if (!dtrace_canload(addr
, sz
+ 1, mstate
, vstate
)) {
3737 case DIF_SUBR_STRCHR
:
3738 case DIF_SUBR_STRRCHR
: {
3740 * We're going to iterate over the string looking for the
3741 * specified character. We will iterate until we have reached
3742 * the string length or we have found the character. If this
3743 * is DIF_SUBR_STRRCHR, we will look for the last occurrence
3744 * of the specified character instead of the first.
3746 uintptr_t saddr
= tupregs
[0].dttk_value
;
3747 uintptr_t addr
= tupregs
[0].dttk_value
;
3748 uintptr_t limit
= addr
+ state
->dts_options
[DTRACEOPT_STRSIZE
];
3749 char c
, target
= (char)tupregs
[1].dttk_value
;
3751 for (regs
[rd
] = 0; addr
< limit
; addr
++) {
3752 if ((c
= dtrace_load8(addr
)) == target
) {
3755 if (subr
== DIF_SUBR_STRCHR
)
3763 if (!dtrace_canload(saddr
, addr
- saddr
, mstate
, vstate
)) {
3771 case DIF_SUBR_STRSTR
:
3772 case DIF_SUBR_INDEX
:
3773 case DIF_SUBR_RINDEX
: {
3775 * We're going to iterate over the string looking for the
3776 * specified string. We will iterate until we have reached
3777 * the string length or we have found the string. (Yes, this
3778 * is done in the most naive way possible -- but considering
3779 * that the string we're searching for is likely to be
3780 * relatively short, the complexity of Rabin-Karp or similar
3781 * hardly seems merited.)
3783 char *addr
= (char *)(uintptr_t)tupregs
[0].dttk_value
;
3784 char *substr
= (char *)(uintptr_t)tupregs
[1].dttk_value
;
3785 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
3786 size_t len
= dtrace_strlen(addr
, size
);
3787 size_t sublen
= dtrace_strlen(substr
, size
);
3788 char *limit
= addr
+ len
, *orig
= addr
;
3789 int notfound
= subr
== DIF_SUBR_STRSTR
? 0 : -1;
3792 regs
[rd
] = notfound
;
3794 if (!dtrace_canload((uintptr_t)addr
, len
+ 1, mstate
, vstate
)) {
3799 if (!dtrace_canload((uintptr_t)substr
, sublen
+ 1, mstate
,
3806 * strstr() and index()/rindex() have similar semantics if
3807 * both strings are the empty string: strstr() returns a
3808 * pointer to the (empty) string, and index() and rindex()
3809 * both return index 0 (regardless of any position argument).
3811 if (sublen
== 0 && len
== 0) {
3812 if (subr
== DIF_SUBR_STRSTR
)
3813 regs
[rd
] = (uintptr_t)addr
;
3819 if (subr
!= DIF_SUBR_STRSTR
) {
3820 if (subr
== DIF_SUBR_RINDEX
) {
3827 * Both index() and rindex() take an optional position
3828 * argument that denotes the starting position.
3831 int64_t pos
= (int64_t)tupregs
[2].dttk_value
;
3834 * If the position argument to index() is
3835 * negative, Perl implicitly clamps it at
3836 * zero. This semantic is a little surprising
3837 * given the special meaning of negative
3838 * positions to similar Perl functions like
3839 * substr(), but it appears to reflect a
3840 * notion that index() can start from a
3841 * negative index and increment its way up to
3842 * the string. Given this notion, Perl's
3843 * rindex() is at least self-consistent in
3844 * that it implicitly clamps positions greater
3845 * than the string length to be the string
3846 * length. Where Perl completely loses
3847 * coherence, however, is when the specified
3848 * substring is the empty string (""). In
3849 * this case, even if the position is
3850 * negative, rindex() returns 0 -- and even if
3851 * the position is greater than the length,
3852 * index() returns the string length. These
3853 * semantics violate the notion that index()
3854 * should never return a value less than the
3855 * specified position and that rindex() should
3856 * never return a value greater than the
3857 * specified position. (One assumes that
3858 * these semantics are artifacts of Perl's
3859 * implementation and not the results of
3860 * deliberate design -- it beggars belief that
3861 * even Larry Wall could desire such oddness.)
3862 * While in the abstract one would wish for
3863 * consistent position semantics across
3864 * substr(), index() and rindex() -- or at the
3865 * very least self-consistent position
3866 * semantics for index() and rindex() -- we
3867 * instead opt to keep with the extant Perl
3868 * semantics, in all their broken glory. (Do
3869 * we have more desire to maintain Perl's
3870 * semantics than Perl does? Probably.)
3872 if (subr
== DIF_SUBR_RINDEX
) {
3879 if ((size_t)pos
> len
)
3885 if ((size_t)pos
>= len
) {
3896 for (regs
[rd
] = notfound
; addr
!= limit
; addr
+= inc
) {
3897 if (dtrace_strncmp(addr
, substr
, sublen
) == 0) {
3898 if (subr
!= DIF_SUBR_STRSTR
) {
3900 * As D index() and rindex() are
3901 * modeled on Perl (and not on awk),
3902 * we return a zero-based (and not a
3903 * one-based) index. (For you Perl
3904 * weenies: no, we're not going to add
3905 * $[ -- and shouldn't you be at a con
3908 regs
[rd
] = (uintptr_t)(addr
- orig
);
3912 ASSERT(subr
== DIF_SUBR_STRSTR
);
3913 regs
[rd
] = (uintptr_t)addr
;
3921 case DIF_SUBR_STRTOK
: {
3922 uintptr_t addr
= tupregs
[0].dttk_value
;
3923 uintptr_t tokaddr
= tupregs
[1].dttk_value
;
3924 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
3925 uintptr_t limit
, toklimit
= tokaddr
+ size
;
3926 char *dest
= (char *)mstate
->dtms_scratch_ptr
;
3927 uint8_t c
='\0', tokmap
[32]; /* 256 / 8 */
3931 * Check both the token buffer and (later) the input buffer,
3932 * since both could be non-scratch addresses.
3934 if (!dtrace_strcanload(tokaddr
, size
, mstate
, vstate
)) {
3939 if (!DTRACE_INSCRATCH(mstate
, size
)) {
3940 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
3947 * If the address specified is NULL, we use our saved
3948 * strtok pointer from the mstate. Note that this
3949 * means that the saved strtok pointer is _only_
3950 * valid within multiple enablings of the same probe --
3951 * it behaves like an implicit clause-local variable.
3953 addr
= mstate
->dtms_strtok
;
3956 * If the user-specified address is non-NULL we must
3957 * access check it. This is the only time we have
3958 * a chance to do so, since this address may reside
3959 * in the string table of this clause-- future calls
3960 * (when we fetch addr from mstate->dtms_strtok)
3961 * would fail this access check.
3963 if (!dtrace_strcanload(addr
, size
, mstate
, vstate
)) {
3970 * First, zero the token map, and then process the token
3971 * string -- setting a bit in the map for every character
3972 * found in the token string.
3974 for (i
= 0; i
< (int)sizeof (tokmap
); i
++)
3977 for (; tokaddr
< toklimit
; tokaddr
++) {
3978 if ((c
= dtrace_load8(tokaddr
)) == '\0')
3981 ASSERT((c
>> 3) < sizeof (tokmap
));
3982 tokmap
[c
>> 3] |= (1 << (c
& 0x7));
3985 for (limit
= addr
+ size
; addr
< limit
; addr
++) {
3987 * We're looking for a character that is _not_ contained
3988 * in the token string.
3990 if ((c
= dtrace_load8(addr
)) == '\0')
3993 if (!(tokmap
[c
>> 3] & (1 << (c
& 0x7))))
3999 * We reached the end of the string without finding
4000 * any character that was not in the token string.
4001 * We return NULL in this case, and we set the saved
4002 * address to NULL as well.
4005 mstate
->dtms_strtok
= 0;
4010 * From here on, we're copying into the destination string.
4012 for (i
= 0; addr
< limit
&& i
< size
- 1; addr
++) {
4013 if ((c
= dtrace_load8(addr
)) == '\0')
4016 if (tokmap
[c
>> 3] & (1 << (c
& 0x7)))
4025 regs
[rd
] = (uintptr_t)dest
;
4026 mstate
->dtms_scratch_ptr
+= size
;
4027 mstate
->dtms_strtok
= addr
;
4031 case DIF_SUBR_SUBSTR
: {
4032 uintptr_t s
= tupregs
[0].dttk_value
;
4033 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
4034 char *d
= (char *)mstate
->dtms_scratch_ptr
;
4035 int64_t index
= (int64_t)tupregs
[1].dttk_value
;
4036 int64_t remaining
= (int64_t)tupregs
[2].dttk_value
;
4037 size_t len
= dtrace_strlen((char *)s
, size
);
4040 if (!dtrace_canload(s
, len
+ 1, mstate
, vstate
)) {
4045 if (!DTRACE_INSCRATCH(mstate
, size
)) {
4046 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
4052 remaining
= (int64_t)size
;
4057 if (index
< 0 && index
+ remaining
> 0) {
4063 if ((size_t)index
>= len
|| index
< 0) {
4065 } else if (remaining
< 0) {
4066 remaining
+= len
- index
;
4067 } else if ((uint64_t)index
+ (uint64_t)remaining
> size
) {
4068 remaining
= size
- index
;
4071 for (i
= 0; i
< remaining
; i
++) {
4072 if ((d
[i
] = dtrace_load8(s
+ index
+ i
)) == '\0')
4078 mstate
->dtms_scratch_ptr
+= size
;
4079 regs
[rd
] = (uintptr_t)d
;
4083 case DIF_SUBR_GETMAJOR
:
4084 regs
[rd
] = (uintptr_t)major( (dev_t
)tupregs
[0].dttk_value
);
4087 case DIF_SUBR_GETMINOR
:
4088 regs
[rd
] = (uintptr_t)minor( (dev_t
)tupregs
[0].dttk_value
);
4091 case DIF_SUBR_DDI_PATHNAME
: {
4092 /* APPLE NOTE: currently unsupported on Darwin */
4093 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
4098 case DIF_SUBR_STRJOIN
: {
4099 char *d
= (char *)mstate
->dtms_scratch_ptr
;
4100 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
4101 uintptr_t s1
= tupregs
[0].dttk_value
;
4102 uintptr_t s2
= tupregs
[1].dttk_value
;
4105 if (!dtrace_strcanload(s1
, size
, mstate
, vstate
) ||
4106 !dtrace_strcanload(s2
, size
, mstate
, vstate
)) {
4111 if (!DTRACE_INSCRATCH(mstate
, size
)) {
4112 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
4119 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
4124 if ((d
[i
++] = dtrace_load8(s1
++)) == '\0') {
4132 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
4137 if ((d
[i
++] = dtrace_load8(s2
++)) == '\0')
4142 mstate
->dtms_scratch_ptr
+= i
;
4143 regs
[rd
] = (uintptr_t)d
;
4149 case DIF_SUBR_LLTOSTR
: {
4150 int64_t i
= (int64_t)tupregs
[0].dttk_value
;
4151 int64_t val
= i
< 0 ? i
* -1 : i
;
4152 uint64_t size
= 22; /* enough room for 2^64 in decimal */
4153 char *end
= (char *)mstate
->dtms_scratch_ptr
+ size
- 1;
4155 if (!DTRACE_INSCRATCH(mstate
, size
)) {
4156 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
4161 for (*end
-- = '\0'; val
; val
/= 10)
4162 *end
-- = '0' + (val
% 10);
4170 regs
[rd
] = (uintptr_t)end
+ 1;
4171 mstate
->dtms_scratch_ptr
+= size
;
4175 case DIF_SUBR_HTONS
:
4176 case DIF_SUBR_NTOHS
:
4178 regs
[rd
] = (uint16_t)tupregs
[0].dttk_value
;
4180 regs
[rd
] = DT_BSWAP_16((uint16_t)tupregs
[0].dttk_value
);
4185 case DIF_SUBR_HTONL
:
4186 case DIF_SUBR_NTOHL
:
4188 regs
[rd
] = (uint32_t)tupregs
[0].dttk_value
;
4190 regs
[rd
] = DT_BSWAP_32((uint32_t)tupregs
[0].dttk_value
);
4195 case DIF_SUBR_HTONLL
:
4196 case DIF_SUBR_NTOHLL
:
4198 regs
[rd
] = (uint64_t)tupregs
[0].dttk_value
;
4200 regs
[rd
] = DT_BSWAP_64((uint64_t)tupregs
[0].dttk_value
);
4205 case DIF_SUBR_DIRNAME
:
4206 case DIF_SUBR_BASENAME
: {
4207 char *dest
= (char *)mstate
->dtms_scratch_ptr
;
4208 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
4209 uintptr_t src
= tupregs
[0].dttk_value
;
4210 int i
, j
, len
= dtrace_strlen((char *)src
, size
);
4211 int lastbase
= -1, firstbase
= -1, lastdir
= -1;
4214 if (!dtrace_canload(src
, len
+ 1, mstate
, vstate
)) {
4219 if (!DTRACE_INSCRATCH(mstate
, size
)) {
4220 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
4226 * The basename and dirname for a zero-length string is
4231 src
= (uintptr_t)".";
4235 * Start from the back of the string, moving back toward the
4236 * front until we see a character that isn't a slash. That
4237 * character is the last character in the basename.
4239 for (i
= len
- 1; i
>= 0; i
--) {
4240 if (dtrace_load8(src
+ i
) != '/')
4248 * Starting from the last character in the basename, move
4249 * towards the front until we find a slash. The character
4250 * that we processed immediately before that is the first
4251 * character in the basename.
4253 for (; i
>= 0; i
--) {
4254 if (dtrace_load8(src
+ i
) == '/')
4262 * Now keep going until we find a non-slash character. That
4263 * character is the last character in the dirname.
4265 for (; i
>= 0; i
--) {
4266 if (dtrace_load8(src
+ i
) != '/')
4273 ASSERT(!(lastbase
== -1 && firstbase
!= -1));
4274 ASSERT(!(firstbase
== -1 && lastdir
!= -1));
4276 if (lastbase
== -1) {
4278 * We didn't find a non-slash character. We know that
4279 * the length is non-zero, so the whole string must be
4280 * slashes. In either the dirname or the basename
4281 * case, we return '/'.
4283 ASSERT(firstbase
== -1);
4284 firstbase
= lastbase
= lastdir
= 0;
4287 if (firstbase
== -1) {
4289 * The entire string consists only of a basename
4290 * component. If we're looking for dirname, we need
4291 * to change our string to be just "."; if we're
4292 * looking for a basename, we'll just set the first
4293 * character of the basename to be 0.
4295 if (subr
== DIF_SUBR_DIRNAME
) {
4296 ASSERT(lastdir
== -1);
4297 src
= (uintptr_t)".";
4304 if (subr
== DIF_SUBR_DIRNAME
) {
4305 if (lastdir
== -1) {
4307 * We know that we have a slash in the name --
4308 * or lastdir would be set to 0, above. And
4309 * because lastdir is -1, we know that this
4310 * slash must be the first character. (That
4311 * is, the full string must be of the form
4312 * "/basename".) In this case, the last
4313 * character of the directory name is 0.
4321 ASSERT(subr
== DIF_SUBR_BASENAME
);
4322 ASSERT(firstbase
!= -1 && lastbase
!= -1);
4327 for (i
= start
, j
= 0; i
<= end
&& (uint64_t)j
< size
- 1; i
++, j
++)
4328 dest
[j
] = dtrace_load8(src
+ i
);
4331 regs
[rd
] = (uintptr_t)dest
;
4332 mstate
->dtms_scratch_ptr
+= size
;
4336 case DIF_SUBR_CLEANPATH
: {
4337 char *dest
= (char *)mstate
->dtms_scratch_ptr
, c
;
4338 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
4339 uintptr_t src
= tupregs
[0].dttk_value
;
4342 if (!dtrace_strcanload(src
, size
, mstate
, vstate
)) {
4347 if (!DTRACE_INSCRATCH(mstate
, size
)) {
4348 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
4354 * Move forward, loading each character.
4357 c
= dtrace_load8(src
+ i
++);
4359 if ((uint64_t)(j
+ 5) >= size
) /* 5 = strlen("/..c\0") */
4367 c
= dtrace_load8(src
+ i
++);
4371 * We have two slashes -- we can just advance
4372 * to the next character.
4379 * This is not "." and it's not ".." -- we can
4380 * just store the "/" and this character and
4388 c
= dtrace_load8(src
+ i
++);
4392 * This is a "/./" component. We're not going
4393 * to store anything in the destination buffer;
4394 * we're just going to go to the next component.
4401 * This is not ".." -- we can just store the
4402 * "/." and this character and continue
4411 c
= dtrace_load8(src
+ i
++);
4413 if (c
!= '/' && c
!= '\0') {
4415 * This is not ".." -- it's "..[mumble]".
4416 * We'll store the "/.." and this character
4417 * and continue processing.
4427 * This is "/../" or "/..\0". We need to back up
4428 * our destination pointer until we find a "/".
4431 while (j
!= 0 && dest
[--j
] != '/')
4436 } while (c
!= '\0');
4439 regs
[rd
] = (uintptr_t)dest
;
4440 mstate
->dtms_scratch_ptr
+= size
;
4444 case DIF_SUBR_INET_NTOA
:
4445 case DIF_SUBR_INET_NTOA6
:
4446 case DIF_SUBR_INET_NTOP
: {
4451 if (subr
== DIF_SUBR_INET_NTOP
) {
4452 af
= (int)tupregs
[0].dttk_value
;
4455 af
= subr
== DIF_SUBR_INET_NTOA
? AF_INET
: AF_INET6
;
4459 if (af
== AF_INET
) {
4460 #if !defined(__APPLE__)
4464 #endif /* __APPLE__ */
4468 * Safely load the IPv4 address.
4470 #if !defined(__APPLE__)
4471 ip4
= dtrace_load32(tupregs
[argi
].dttk_value
);
4474 (void *)(uintptr_t)tupregs
[argi
].dttk_value
,
4475 (void *)(uintptr_t)&ip4
, sizeof (ip4
));
4476 #endif /* __APPLE__ */
4478 * Check an IPv4 string will fit in scratch.
4480 #if !defined(__APPLE__)
4481 size
= INET_ADDRSTRLEN
;
4483 size
= MAX_IPv4_STR_LEN
;
4484 #endif /* __APPLE__ */
4485 if (!DTRACE_INSCRATCH(mstate
, size
)) {
4486 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
4490 base
= (char *)mstate
->dtms_scratch_ptr
;
4491 end
= (char *)mstate
->dtms_scratch_ptr
+ size
- 1;
4494 * Stringify as a dotted decimal quad.
4497 ptr8
= (uint8_t *)&ip4
;
4498 for (i
= 3; i
>= 0; i
--) {
4504 for (; val
; val
/= 10) {
4505 *end
-- = '0' + (val
% 10);
4512 ASSERT(end
+ 1 >= base
);
4514 } else if (af
== AF_INET6
) {
4515 #if defined(__APPLE__)
4516 #define _S6_un __u6_addr
4517 #define _S6_u8 __u6_addr8
4518 #endif /* __APPLE__ */
4519 struct in6_addr ip6
;
4520 int firstzero
, tryzero
, numzero
, v6end
;
4522 const char digits
[] = "0123456789abcdef";
4525 * Stringify using RFC 1884 convention 2 - 16 bit
4526 * hexadecimal values with a zero-run compression.
4527 * Lower case hexadecimal digits are used.
4528 * eg, fe80::214:4fff:fe0b:76c8.
4529 * The IPv4 embedded form is returned for inet_ntop,
4530 * just the IPv4 string is returned for inet_ntoa6.
4534 * Safely load the IPv6 address.
4537 (void *)(uintptr_t)tupregs
[argi
].dttk_value
,
4538 (void *)(uintptr_t)&ip6
, sizeof (struct in6_addr
));
4541 * Check an IPv6 string will fit in scratch.
4543 size
= INET6_ADDRSTRLEN
;
4544 if (!DTRACE_INSCRATCH(mstate
, size
)) {
4545 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
4549 base
= (char *)mstate
->dtms_scratch_ptr
;
4550 end
= (char *)mstate
->dtms_scratch_ptr
+ size
- 1;
4554 * Find the longest run of 16 bit zero values
4555 * for the single allowed zero compression - "::".
4560 for (i
= 0; i
< (int)sizeof (struct in6_addr
); i
++) {
4561 if (ip6
._S6_un
._S6_u8
[i
] == 0 &&
4562 tryzero
== -1 && i
% 2 == 0) {
4567 if (tryzero
!= -1 &&
4568 (ip6
._S6_un
._S6_u8
[i
] != 0 ||
4569 i
== sizeof (struct in6_addr
) - 1)) {
4571 if (i
- tryzero
<= numzero
) {
4576 firstzero
= tryzero
;
4577 numzero
= i
- i
% 2 - tryzero
;
4580 if (ip6
._S6_un
._S6_u8
[i
] == 0 &&
4581 i
== sizeof (struct in6_addr
) - 1)
4585 ASSERT(firstzero
+ numzero
<= (int)sizeof (struct in6_addr
));
4588 * Check for an IPv4 embedded address.
4590 v6end
= sizeof (struct in6_addr
) - 2;
4591 if (IN6_IS_ADDR_V4MAPPED(&ip6
) ||
4592 IN6_IS_ADDR_V4COMPAT(&ip6
)) {
4593 for (i
= sizeof (struct in6_addr
) - 1;
4594 i
>= (int)DTRACE_V4MAPPED_OFFSET
; i
--) {
4595 ASSERT(end
>= base
);
4597 val
= ip6
._S6_un
._S6_u8
[i
];
4602 for (; val
; val
/= 10) {
4603 *end
-- = '0' + val
% 10;
4607 if (i
> (int)DTRACE_V4MAPPED_OFFSET
)
4611 if (subr
== DIF_SUBR_INET_NTOA6
)
4615 * Set v6end to skip the IPv4 address that
4616 * we have already stringified.
4622 * Build the IPv6 string by working through the
4623 * address in reverse.
4625 for (i
= v6end
; i
>= 0; i
-= 2) {
4626 ASSERT(end
>= base
);
4628 if (i
== firstzero
+ numzero
- 2) {
4635 if (i
< 14 && i
!= firstzero
- 2)
4638 val
= (ip6
._S6_un
._S6_u8
[i
] << 8) +
4639 ip6
._S6_un
._S6_u8
[i
+ 1];
4644 for (; val
; val
/= 16) {
4645 *end
-- = digits
[val
% 16];
4649 ASSERT(end
+ 1 >= base
);
4651 #if defined(__APPLE__)
4654 #endif /* __APPLE__ */
4657 * The user didn't use AH_INET or AH_INET6.
4659 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
4664 inetout
: regs
[rd
] = (uintptr_t)end
+ 1;
4665 mstate
->dtms_scratch_ptr
+= size
;
4669 case DIF_SUBR_TOUPPER
:
4670 case DIF_SUBR_TOLOWER
: {
4671 uintptr_t src
= tupregs
[0].dttk_value
;
4672 char *dest
= (char *)mstate
->dtms_scratch_ptr
;
4673 char lower
, upper
, base
, c
;
4674 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
4675 size_t len
= dtrace_strlen((char*) src
, size
);
4678 lower
= (subr
== DIF_SUBR_TOUPPER
) ? 'a' : 'A';
4679 upper
= (subr
== DIF_SUBR_TOUPPER
) ? 'z' : 'Z';
4680 base
= (subr
== DIF_SUBR_TOUPPER
) ? 'A' : 'a';
4682 if (!dtrace_canload(src
, len
+ 1, mstate
, vstate
)) {
4687 if (!DTRACE_INSCRATCH(mstate
, size
)) {
4688 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
4693 for (i
= 0; i
< size
- 1; ++i
) {
4694 if ((c
= dtrace_load8(src
+ i
)) == '\0')
4696 if (c
>= lower
&& c
<= upper
)
4697 c
= base
+ (c
- lower
);
4704 regs
[rd
] = (uintptr_t) dest
;
4705 mstate
->dtms_scratch_ptr
+= size
;
4712 * CoreProfile callback ('core_profile (uint64_t, [uint64_t], [uint64_t] ...)')
4714 case DIF_SUBR_COREPROFILE
: {
4715 uint64_t selector
= tupregs
[0].dttk_value
;
4716 uint64_t args
[DIF_DTR_NREGS
-1] = {0ULL};
4718 uint32_t count
= (uint32_t)nargs
;
4721 regs
[rd
] = KERN_FAILURE
;
4725 if(count
> DIF_DTR_NREGS
)
4726 count
= DIF_DTR_NREGS
;
4728 /* copy in any variadic argument list, bounded by DIF_DTR_NREGS */
4729 for(ii
= 0; ii
< count
-1; ii
++) {
4730 args
[ii
] = tupregs
[ii
+1].dttk_value
;
4734 chudxnu_dtrace_callback(selector
, args
, count
-1);
4735 if(KERN_SUCCESS
!= ret
) {
4746 * Emulate the execution of DTrace IR instructions specified by the given
4747 * DIF object. This function is deliberately void of assertions as all of
4748 * the necessary checks are handled by a call to dtrace_difo_validate().
4751 dtrace_dif_emulate(dtrace_difo_t
*difo
, dtrace_mstate_t
*mstate
,
4752 dtrace_vstate_t
*vstate
, dtrace_state_t
*state
)
4754 const dif_instr_t
*text
= difo
->dtdo_buf
;
4755 const uint_t textlen
= difo
->dtdo_len
;
4756 const char *strtab
= difo
->dtdo_strtab
;
4757 const uint64_t *inttab
= difo
->dtdo_inttab
;
4760 dtrace_statvar_t
*svar
;
4761 dtrace_dstate_t
*dstate
= &vstate
->dtvs_dynvars
;
4763 volatile uint16_t *flags
= &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
4764 volatile uint64_t *illval
= &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
;
4766 dtrace_key_t tupregs
[DIF_DTR_NREGS
+ 2]; /* +2 for thread and id */
4767 uint64_t regs
[DIF_DIR_NREGS
];
4770 uint8_t cc_n
= 0, cc_z
= 0, cc_v
= 0, cc_c
= 0;
4772 uint_t pc
= 0, id
, opc
= 0;
4778 * We stash the current DIF object into the machine state: we need it
4779 * for subsequent access checking.
4781 mstate
->dtms_difo
= difo
;
4783 regs
[DIF_REG_R0
] = 0; /* %r0 is fixed at zero */
4785 while (pc
< textlen
&& !(*flags
& CPU_DTRACE_FAULT
)) {
4789 r1
= DIF_INSTR_R1(instr
);
4790 r2
= DIF_INSTR_R2(instr
);
4791 rd
= DIF_INSTR_RD(instr
);
4793 switch (DIF_INSTR_OP(instr
)) {
4795 regs
[rd
] = regs
[r1
] | regs
[r2
];
4798 regs
[rd
] = regs
[r1
] ^ regs
[r2
];
4801 regs
[rd
] = regs
[r1
] & regs
[r2
];
4804 regs
[rd
] = regs
[r1
] << regs
[r2
];
4807 regs
[rd
] = regs
[r1
] >> regs
[r2
];
4810 regs
[rd
] = regs
[r1
] - regs
[r2
];
4813 regs
[rd
] = regs
[r1
] + regs
[r2
];
4816 regs
[rd
] = regs
[r1
] * regs
[r2
];
4819 if (regs
[r2
] == 0) {
4821 *flags
|= CPU_DTRACE_DIVZERO
;
4823 regs
[rd
] = (int64_t)regs
[r1
] /
4829 if (regs
[r2
] == 0) {
4831 *flags
|= CPU_DTRACE_DIVZERO
;
4833 regs
[rd
] = regs
[r1
] / regs
[r2
];
4838 if (regs
[r2
] == 0) {
4840 *flags
|= CPU_DTRACE_DIVZERO
;
4842 regs
[rd
] = (int64_t)regs
[r1
] %
4848 if (regs
[r2
] == 0) {
4850 *flags
|= CPU_DTRACE_DIVZERO
;
4852 regs
[rd
] = regs
[r1
] % regs
[r2
];
4857 regs
[rd
] = ~regs
[r1
];
4860 regs
[rd
] = regs
[r1
];
4863 cc_r
= regs
[r1
] - regs
[r2
];
4867 cc_c
= regs
[r1
] < regs
[r2
];
4870 cc_n
= cc_v
= cc_c
= 0;
4871 cc_z
= regs
[r1
] == 0;
4874 pc
= DIF_INSTR_LABEL(instr
);
4878 pc
= DIF_INSTR_LABEL(instr
);
4882 pc
= DIF_INSTR_LABEL(instr
);
4885 if ((cc_z
| (cc_n
^ cc_v
)) == 0)
4886 pc
= DIF_INSTR_LABEL(instr
);
4889 if ((cc_c
| cc_z
) == 0)
4890 pc
= DIF_INSTR_LABEL(instr
);
4893 if ((cc_n
^ cc_v
) == 0)
4894 pc
= DIF_INSTR_LABEL(instr
);
4898 pc
= DIF_INSTR_LABEL(instr
);
4902 pc
= DIF_INSTR_LABEL(instr
);
4906 pc
= DIF_INSTR_LABEL(instr
);
4909 if (cc_z
| (cc_n
^ cc_v
))
4910 pc
= DIF_INSTR_LABEL(instr
);
4914 pc
= DIF_INSTR_LABEL(instr
);
4917 if (!dtrace_canstore(regs
[r1
], 1, mstate
, vstate
)) {
4918 *flags
|= CPU_DTRACE_KPRIV
;
4924 regs
[rd
] = (int8_t)dtrace_load8(regs
[r1
]);
4927 if (!dtrace_canstore(regs
[r1
], 2, mstate
, vstate
)) {
4928 *flags
|= CPU_DTRACE_KPRIV
;
4934 regs
[rd
] = (int16_t)dtrace_load16(regs
[r1
]);
4937 if (!dtrace_canstore(regs
[r1
], 4, mstate
, vstate
)) {
4938 *flags
|= CPU_DTRACE_KPRIV
;
4944 regs
[rd
] = (int32_t)dtrace_load32(regs
[r1
]);
4947 if (!dtrace_canstore(regs
[r1
], 1, mstate
, vstate
)) {
4948 *flags
|= CPU_DTRACE_KPRIV
;
4954 regs
[rd
] = dtrace_load8(regs
[r1
]);
4957 if (!dtrace_canstore(regs
[r1
], 2, mstate
, vstate
)) {
4958 *flags
|= CPU_DTRACE_KPRIV
;
4964 regs
[rd
] = dtrace_load16(regs
[r1
]);
4967 if (!dtrace_canstore(regs
[r1
], 4, mstate
, vstate
)) {
4968 *flags
|= CPU_DTRACE_KPRIV
;
4974 regs
[rd
] = dtrace_load32(regs
[r1
]);
4977 if (!dtrace_canstore(regs
[r1
], 8, mstate
, vstate
)) {
4978 *flags
|= CPU_DTRACE_KPRIV
;
4984 regs
[rd
] = dtrace_load64(regs
[r1
]);
4987 * Darwin 32-bit kernel may fetch from 64-bit user.
4988 * Do not cast regs to uintptr_t
4989 * DIF_OP_ULDSB,DIF_OP_ULDSH, DIF_OP_ULDSW, DIF_OP_ULDUB
4990 * DIF_OP_ULDUH, DIF_OP_ULDUW, DIF_OP_ULDX
4994 dtrace_fuword8(regs
[r1
]);
4997 regs
[rd
] = (int16_t)
4998 dtrace_fuword16(regs
[r1
]);
5001 regs
[rd
] = (int32_t)
5002 dtrace_fuword32(regs
[r1
]);
5006 dtrace_fuword8(regs
[r1
]);
5010 dtrace_fuword16(regs
[r1
]);
5014 dtrace_fuword32(regs
[r1
]);
5018 dtrace_fuword64(regs
[r1
]);
5027 regs
[rd
] = inttab
[DIF_INSTR_INTEGER(instr
)];
5030 regs
[rd
] = (uint64_t)(uintptr_t)
5031 (strtab
+ DIF_INSTR_STRING(instr
));
5034 size_t sz
= state
->dts_options
[DTRACEOPT_STRSIZE
];
5035 uintptr_t s1
= regs
[r1
];
5036 uintptr_t s2
= regs
[r2
];
5039 !dtrace_strcanload(s1
, sz
, mstate
, vstate
))
5042 !dtrace_strcanload(s2
, sz
, mstate
, vstate
))
5045 cc_r
= dtrace_strncmp((char *)s1
, (char *)s2
, sz
);
5053 regs
[rd
] = dtrace_dif_variable(mstate
, state
,
5057 id
= DIF_INSTR_VAR(instr
);
5059 if (id
>= DIF_VAR_OTHER_UBASE
) {
5062 id
-= DIF_VAR_OTHER_UBASE
;
5063 svar
= vstate
->dtvs_globals
[id
];
5064 ASSERT(svar
!= NULL
);
5065 v
= &svar
->dtsv_var
;
5067 if (!(v
->dtdv_type
.dtdt_flags
& DIF_TF_BYREF
)) {
5068 regs
[rd
] = svar
->dtsv_data
;
5072 a
= (uintptr_t)svar
->dtsv_data
;
5074 if (*(uint8_t *)a
== UINT8_MAX
) {
5076 * If the 0th byte is set to UINT8_MAX
5077 * then this is to be treated as a
5078 * reference to a NULL variable.
5082 regs
[rd
] = a
+ sizeof (uint64_t);
5088 regs
[rd
] = dtrace_dif_variable(mstate
, state
, id
, 0);
5092 id
= DIF_INSTR_VAR(instr
);
5094 ASSERT(id
>= DIF_VAR_OTHER_UBASE
);
5095 id
-= DIF_VAR_OTHER_UBASE
;
5097 svar
= vstate
->dtvs_globals
[id
];
5098 ASSERT(svar
!= NULL
);
5099 v
= &svar
->dtsv_var
;
5101 if (v
->dtdv_type
.dtdt_flags
& DIF_TF_BYREF
) {
5102 uintptr_t a
= (uintptr_t)svar
->dtsv_data
;
5105 ASSERT(svar
->dtsv_size
!= 0);
5107 if (regs
[rd
] == 0) {
5108 *(uint8_t *)a
= UINT8_MAX
;
5112 a
+= sizeof (uint64_t);
5114 if (!dtrace_vcanload(
5115 (void *)(uintptr_t)regs
[rd
], &v
->dtdv_type
,
5119 dtrace_vcopy((void *)(uintptr_t)regs
[rd
],
5120 (void *)a
, &v
->dtdv_type
);
5124 svar
->dtsv_data
= regs
[rd
];
5129 * There are no DTrace built-in thread-local arrays at
5130 * present. This opcode is saved for future work.
5132 *flags
|= CPU_DTRACE_ILLOP
;
5137 id
= DIF_INSTR_VAR(instr
);
5139 if (id
< DIF_VAR_OTHER_UBASE
) {
5141 * For now, this has no meaning.
5147 id
-= DIF_VAR_OTHER_UBASE
;
5149 ASSERT(id
< (uint_t
)vstate
->dtvs_nlocals
);
5150 ASSERT(vstate
->dtvs_locals
!= NULL
);
5151 svar
= vstate
->dtvs_locals
[id
];
5152 ASSERT(svar
!= NULL
);
5153 v
= &svar
->dtsv_var
;
5155 if (v
->dtdv_type
.dtdt_flags
& DIF_TF_BYREF
) {
5156 uintptr_t a
= (uintptr_t)svar
->dtsv_data
;
5157 size_t sz
= v
->dtdv_type
.dtdt_size
;
5159 sz
+= sizeof (uint64_t);
5160 ASSERT(svar
->dtsv_size
== (int)NCPU
* sz
);
5161 a
+= CPU
->cpu_id
* sz
;
5163 if (*(uint8_t *)a
== UINT8_MAX
) {
5165 * If the 0th byte is set to UINT8_MAX
5166 * then this is to be treated as a
5167 * reference to a NULL variable.
5171 regs
[rd
] = a
+ sizeof (uint64_t);
5177 ASSERT(svar
->dtsv_size
== (int)NCPU
* sizeof (uint64_t));
5178 tmp
= (uint64_t *)(uintptr_t)svar
->dtsv_data
;
5179 regs
[rd
] = tmp
[CPU
->cpu_id
];
5183 id
= DIF_INSTR_VAR(instr
);
5185 ASSERT(id
>= DIF_VAR_OTHER_UBASE
);
5186 id
-= DIF_VAR_OTHER_UBASE
;
5187 ASSERT(id
< (uint_t
)vstate
->dtvs_nlocals
);
5188 ASSERT(vstate
->dtvs_locals
!= NULL
);
5189 svar
= vstate
->dtvs_locals
[id
];
5190 ASSERT(svar
!= NULL
);
5191 v
= &svar
->dtsv_var
;
5193 if (v
->dtdv_type
.dtdt_flags
& DIF_TF_BYREF
) {
5194 uintptr_t a
= (uintptr_t)svar
->dtsv_data
;
5195 size_t sz
= v
->dtdv_type
.dtdt_size
;
5197 sz
+= sizeof (uint64_t);
5198 ASSERT(svar
->dtsv_size
== (int)NCPU
* sz
);
5199 a
+= CPU
->cpu_id
* sz
;
5201 if (regs
[rd
] == 0) {
5202 *(uint8_t *)a
= UINT8_MAX
;
5206 a
+= sizeof (uint64_t);
5209 if (!dtrace_vcanload(
5210 (void *)(uintptr_t)regs
[rd
], &v
->dtdv_type
,
5214 dtrace_vcopy((void *)(uintptr_t)regs
[rd
],
5215 (void *)a
, &v
->dtdv_type
);
5219 ASSERT(svar
->dtsv_size
== (int)NCPU
* sizeof (uint64_t));
5220 tmp
= (uint64_t *)(uintptr_t)svar
->dtsv_data
;
5221 tmp
[CPU
->cpu_id
] = regs
[rd
];
5225 dtrace_dynvar_t
*dvar
;
5228 id
= DIF_INSTR_VAR(instr
);
5229 ASSERT(id
>= DIF_VAR_OTHER_UBASE
);
5230 id
-= DIF_VAR_OTHER_UBASE
;
5231 v
= &vstate
->dtvs_tlocals
[id
];
5233 key
= &tupregs
[DIF_DTR_NREGS
];
5234 key
[0].dttk_value
= (uint64_t)id
;
5235 key
[0].dttk_size
= 0;
5236 DTRACE_TLS_THRKEY(key
[1].dttk_value
);
5237 key
[1].dttk_size
= 0;
5239 dvar
= dtrace_dynvar(dstate
, 2, key
,
5240 sizeof (uint64_t), DTRACE_DYNVAR_NOALLOC
,
5248 if (v
->dtdv_type
.dtdt_flags
& DIF_TF_BYREF
) {
5249 regs
[rd
] = (uint64_t)(uintptr_t)dvar
->dtdv_data
;
5251 regs
[rd
] = *((uint64_t *)dvar
->dtdv_data
);
5258 dtrace_dynvar_t
*dvar
;
5261 id
= DIF_INSTR_VAR(instr
);
5262 ASSERT(id
>= DIF_VAR_OTHER_UBASE
);
5263 id
-= DIF_VAR_OTHER_UBASE
;
5265 key
= &tupregs
[DIF_DTR_NREGS
];
5266 key
[0].dttk_value
= (uint64_t)id
;
5267 key
[0].dttk_size
= 0;
5268 DTRACE_TLS_THRKEY(key
[1].dttk_value
);
5269 key
[1].dttk_size
= 0;
5270 v
= &vstate
->dtvs_tlocals
[id
];
5272 dvar
= dtrace_dynvar(dstate
, 2, key
,
5273 v
->dtdv_type
.dtdt_size
> sizeof (uint64_t) ?
5274 v
->dtdv_type
.dtdt_size
: sizeof (uint64_t),
5275 regs
[rd
] ? DTRACE_DYNVAR_ALLOC
:
5276 DTRACE_DYNVAR_DEALLOC
, mstate
, vstate
);
5279 * Given that we're storing to thread-local data,
5280 * we need to flush our predicate cache.
5282 dtrace_set_thread_predcache(current_thread(), 0);
5287 if (v
->dtdv_type
.dtdt_flags
& DIF_TF_BYREF
) {
5288 if (!dtrace_vcanload(
5289 (void *)(uintptr_t)regs
[rd
],
5290 &v
->dtdv_type
, mstate
, vstate
))
5293 dtrace_vcopy((void *)(uintptr_t)regs
[rd
],
5294 dvar
->dtdv_data
, &v
->dtdv_type
);
5296 *((uint64_t *)dvar
->dtdv_data
) = regs
[rd
];
5303 regs
[rd
] = (int64_t)regs
[r1
] >> regs
[r2
];
5307 dtrace_dif_subr(DIF_INSTR_SUBR(instr
), rd
,
5308 regs
, tupregs
, ttop
, mstate
, state
);
5312 if (ttop
== DIF_DTR_NREGS
) {
5313 *flags
|= CPU_DTRACE_TUPOFLOW
;
5317 if (r1
== DIF_TYPE_STRING
) {
5319 * If this is a string type and the size is 0,
5320 * we'll use the system-wide default string
5321 * size. Note that we are _not_ looking at
5322 * the value of the DTRACEOPT_STRSIZE option;
5323 * had this been set, we would expect to have
5324 * a non-zero size value in the "pushtr".
5326 tupregs
[ttop
].dttk_size
=
5327 dtrace_strlen((char *)(uintptr_t)regs
[rd
],
5328 regs
[r2
] ? regs
[r2
] :
5329 dtrace_strsize_default
) + 1;
5331 tupregs
[ttop
].dttk_size
= regs
[r2
];
5334 tupregs
[ttop
++].dttk_value
= regs
[rd
];
5338 if (ttop
== DIF_DTR_NREGS
) {
5339 *flags
|= CPU_DTRACE_TUPOFLOW
;
5343 tupregs
[ttop
].dttk_value
= regs
[rd
];
5344 tupregs
[ttop
++].dttk_size
= 0;
5352 case DIF_OP_FLUSHTS
:
5357 case DIF_OP_LDTAA
: {
5358 dtrace_dynvar_t
*dvar
;
5359 dtrace_key_t
*key
= tupregs
;
5360 uint_t nkeys
= ttop
;
5362 id
= DIF_INSTR_VAR(instr
);
5363 ASSERT(id
>= DIF_VAR_OTHER_UBASE
);
5364 id
-= DIF_VAR_OTHER_UBASE
;
5366 key
[nkeys
].dttk_value
= (uint64_t)id
;
5367 key
[nkeys
++].dttk_size
= 0;
5369 if (DIF_INSTR_OP(instr
) == DIF_OP_LDTAA
) {
5370 DTRACE_TLS_THRKEY(key
[nkeys
].dttk_value
);
5371 key
[nkeys
++].dttk_size
= 0;
5372 v
= &vstate
->dtvs_tlocals
[id
];
5374 v
= &vstate
->dtvs_globals
[id
]->dtsv_var
;
5377 dvar
= dtrace_dynvar(dstate
, nkeys
, key
,
5378 v
->dtdv_type
.dtdt_size
> sizeof (uint64_t) ?
5379 v
->dtdv_type
.dtdt_size
: sizeof (uint64_t),
5380 DTRACE_DYNVAR_NOALLOC
, mstate
, vstate
);
5387 if (v
->dtdv_type
.dtdt_flags
& DIF_TF_BYREF
) {
5388 regs
[rd
] = (uint64_t)(uintptr_t)dvar
->dtdv_data
;
5390 regs
[rd
] = *((uint64_t *)dvar
->dtdv_data
);
5397 case DIF_OP_STTAA
: {
5398 dtrace_dynvar_t
*dvar
;
5399 dtrace_key_t
*key
= tupregs
;
5400 uint_t nkeys
= ttop
;
5402 id
= DIF_INSTR_VAR(instr
);
5403 ASSERT(id
>= DIF_VAR_OTHER_UBASE
);
5404 id
-= DIF_VAR_OTHER_UBASE
;
5406 key
[nkeys
].dttk_value
= (uint64_t)id
;
5407 key
[nkeys
++].dttk_size
= 0;
5409 if (DIF_INSTR_OP(instr
) == DIF_OP_STTAA
) {
5410 DTRACE_TLS_THRKEY(key
[nkeys
].dttk_value
);
5411 key
[nkeys
++].dttk_size
= 0;
5412 v
= &vstate
->dtvs_tlocals
[id
];
5414 v
= &vstate
->dtvs_globals
[id
]->dtsv_var
;
5417 dvar
= dtrace_dynvar(dstate
, nkeys
, key
,
5418 v
->dtdv_type
.dtdt_size
> sizeof (uint64_t) ?
5419 v
->dtdv_type
.dtdt_size
: sizeof (uint64_t),
5420 regs
[rd
] ? DTRACE_DYNVAR_ALLOC
:
5421 DTRACE_DYNVAR_DEALLOC
, mstate
, vstate
);
5426 if (v
->dtdv_type
.dtdt_flags
& DIF_TF_BYREF
) {
5427 if (!dtrace_vcanload(
5428 (void *)(uintptr_t)regs
[rd
], &v
->dtdv_type
,
5432 dtrace_vcopy((void *)(uintptr_t)regs
[rd
],
5433 dvar
->dtdv_data
, &v
->dtdv_type
);
5435 *((uint64_t *)dvar
->dtdv_data
) = regs
[rd
];
5441 case DIF_OP_ALLOCS
: {
5442 uintptr_t ptr
= P2ROUNDUP(mstate
->dtms_scratch_ptr
, 8);
5443 size_t size
= ptr
- mstate
->dtms_scratch_ptr
+ regs
[r1
];
5446 * Rounding up the user allocation size could have
5447 * overflowed large, bogus allocations (like -1ULL) to
5450 if (size
< regs
[r1
] ||
5451 !DTRACE_INSCRATCH(mstate
, size
)) {
5452 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
5457 dtrace_bzero((void *) mstate
->dtms_scratch_ptr
, size
);
5458 mstate
->dtms_scratch_ptr
+= size
;
5464 if (!dtrace_canstore(regs
[rd
], regs
[r2
],
5466 *flags
|= CPU_DTRACE_BADADDR
;
5471 if (!dtrace_canload(regs
[r1
], regs
[r2
], mstate
, vstate
))
5474 dtrace_bcopy((void *)(uintptr_t)regs
[r1
],
5475 (void *)(uintptr_t)regs
[rd
], (size_t)regs
[r2
]);
5479 if (!dtrace_canstore(regs
[rd
], 1, mstate
, vstate
)) {
5480 *flags
|= CPU_DTRACE_BADADDR
;
5484 *((uint8_t *)(uintptr_t)regs
[rd
]) = (uint8_t)regs
[r1
];
5488 if (!dtrace_canstore(regs
[rd
], 2, mstate
, vstate
)) {
5489 *flags
|= CPU_DTRACE_BADADDR
;
5494 *flags
|= CPU_DTRACE_BADALIGN
;
5498 *((uint16_t *)(uintptr_t)regs
[rd
]) = (uint16_t)regs
[r1
];
5502 if (!dtrace_canstore(regs
[rd
], 4, mstate
, vstate
)) {
5503 *flags
|= CPU_DTRACE_BADADDR
;
5508 *flags
|= CPU_DTRACE_BADALIGN
;
5512 *((uint32_t *)(uintptr_t)regs
[rd
]) = (uint32_t)regs
[r1
];
5516 if (!dtrace_canstore(regs
[rd
], 8, mstate
, vstate
)) {
5517 *flags
|= CPU_DTRACE_BADADDR
;
5523 * Darwin kmem_zalloc() called from
5524 * dtrace_difo_init() is 4-byte aligned.
5527 *flags
|= CPU_DTRACE_BADALIGN
;
5531 *((uint64_t *)(uintptr_t)regs
[rd
]) = regs
[r1
];
5536 if (!(*flags
& CPU_DTRACE_FAULT
))
5539 mstate
->dtms_fltoffs
= opc
* sizeof (dif_instr_t
);
5540 mstate
->dtms_present
|= DTRACE_MSTATE_FLTOFFS
;
5546 dtrace_action_breakpoint(dtrace_ecb_t
*ecb
)
5548 dtrace_probe_t
*probe
= ecb
->dte_probe
;
5549 dtrace_provider_t
*prov
= probe
->dtpr_provider
;
5550 char c
[DTRACE_FULLNAMELEN
+ 80], *str
;
5551 const char *msg
= "dtrace: breakpoint action at probe ";
5552 const char *ecbmsg
= " (ecb ";
5553 uintptr_t mask
= (0xf << (sizeof (uintptr_t) * NBBY
/ 4));
5554 uintptr_t val
= (uintptr_t)ecb
;
5555 int shift
= (sizeof (uintptr_t) * NBBY
) - 4, i
= 0;
5557 if (dtrace_destructive_disallow
)
5561 * It's impossible to be taking action on the NULL probe.
5563 ASSERT(probe
!= NULL
);
5566 * This is a poor man's (destitute man's?) sprintf(): we want to
5567 * print the provider name, module name, function name and name of
5568 * the probe, along with the hex address of the ECB with the breakpoint
5569 * action -- all of which we must place in the character buffer by
5572 while (*msg
!= '\0')
5575 for (str
= prov
->dtpv_name
; *str
!= '\0'; str
++)
5579 for (str
= probe
->dtpr_mod
; *str
!= '\0'; str
++)
5583 for (str
= probe
->dtpr_func
; *str
!= '\0'; str
++)
5587 for (str
= probe
->dtpr_name
; *str
!= '\0'; str
++)
5590 while (*ecbmsg
!= '\0')
5593 while (shift
>= 0) {
5594 mask
= (uintptr_t)0xf << shift
;
5596 if (val
>= ((uintptr_t)1 << shift
))
5597 c
[i
++] = "0123456789abcdef"[(val
& mask
) >> shift
];
5608 dtrace_action_panic(dtrace_ecb_t
*ecb
)
5610 dtrace_probe_t
*probe
= ecb
->dte_probe
;
5613 * It's impossible to be taking action on the NULL probe.
5615 ASSERT(probe
!= NULL
);
5617 if (dtrace_destructive_disallow
)
5620 if (dtrace_panicked
!= NULL
)
5623 if (dtrace_casptr(&dtrace_panicked
, NULL
, current_thread()) != NULL
)
5627 * We won the right to panic. (We want to be sure that only one
5628 * thread calls panic() from dtrace_probe(), and that panic() is
5629 * called exactly once.)
5631 panic("dtrace: panic action at probe %s:%s:%s:%s (ecb %p)",
5632 probe
->dtpr_provider
->dtpv_name
, probe
->dtpr_mod
,
5633 probe
->dtpr_func
, probe
->dtpr_name
, (void *)ecb
);
5636 * APPLE NOTE: this was for an old Mac OS X debug feature
5637 * allowing a return from panic(). Revisit someday.
5639 dtrace_panicked
= NULL
;
5643 dtrace_action_raise(uint64_t sig
)
5645 if (dtrace_destructive_disallow
)
5649 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
5654 * raise() has a queue depth of 1 -- we ignore all subsequent
5655 * invocations of the raise() action.
5658 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
5660 if (uthread
&& uthread
->t_dtrace_sig
== 0) {
5661 uthread
->t_dtrace_sig
= sig
;
5662 act_set_astbsd(current_thread());
5667 dtrace_action_stop(void)
5669 if (dtrace_destructive_disallow
)
5672 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
5675 * The currently running process will be set to task_suspend
5676 * when it next leaves the kernel.
5678 uthread
->t_dtrace_stop
= 1;
5679 act_set_astbsd(current_thread());
5685 * APPLE NOTE: pidresume works in conjunction with the dtrace stop action.
5686 * Both activate only when the currently running process next leaves the
5690 dtrace_action_pidresume(uint64_t pid
)
5692 if (dtrace_destructive_disallow
)
5695 if (kauth_cred_issuser(kauth_cred_get()) == 0) {
5696 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
5699 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
5702 * When the currently running process leaves the kernel, it attempts to
5703 * task_resume the process (denoted by pid), if that pid appears to have
5704 * been stopped by dtrace_action_stop().
5705 * The currently running process has a pidresume() queue depth of 1 --
5706 * subsequent invocations of the pidresume() action are ignored.
5709 if (pid
!= 0 && uthread
&& uthread
->t_dtrace_resumepid
== 0) {
5710 uthread
->t_dtrace_resumepid
= pid
;
5711 act_set_astbsd(current_thread());
5716 dtrace_action_chill(dtrace_mstate_t
*mstate
, hrtime_t val
)
5719 volatile uint16_t *flags
;
5720 dtrace_cpu_t
*cpu
= CPU
;
5722 if (dtrace_destructive_disallow
)
5725 flags
= (volatile uint16_t *)&cpu_core
[cpu
->cpu_id
].cpuc_dtrace_flags
;
5727 now
= dtrace_gethrtime();
5729 if (now
- cpu
->cpu_dtrace_chillmark
> dtrace_chill_interval
) {
5731 * We need to advance the mark to the current time.
5733 cpu
->cpu_dtrace_chillmark
= now
;
5734 cpu
->cpu_dtrace_chilled
= 0;
5738 * Now check to see if the requested chill time would take us over
5739 * the maximum amount of time allowed in the chill interval. (Or
5740 * worse, if the calculation itself induces overflow.)
5742 if (cpu
->cpu_dtrace_chilled
+ val
> dtrace_chill_max
||
5743 cpu
->cpu_dtrace_chilled
+ val
< cpu
->cpu_dtrace_chilled
) {
5744 *flags
|= CPU_DTRACE_ILLOP
;
5748 while (dtrace_gethrtime() - now
< val
)
5752 * Normally, we assure that the value of the variable "timestamp" does
5753 * not change within an ECB. The presence of chill() represents an
5754 * exception to this rule, however.
5756 mstate
->dtms_present
&= ~DTRACE_MSTATE_TIMESTAMP
;
5757 cpu
->cpu_dtrace_chilled
+= val
;
5761 dtrace_action_ustack(dtrace_mstate_t
*mstate
, dtrace_state_t
*state
,
5762 uint64_t *buf
, uint64_t arg
)
5764 int nframes
= DTRACE_USTACK_NFRAMES(arg
);
5765 int strsize
= DTRACE_USTACK_STRSIZE(arg
);
5766 uint64_t *pcs
= &buf
[1], *fps
;
5767 char *str
= (char *)&pcs
[nframes
];
5768 int size
, offs
= 0, i
, j
;
5769 uintptr_t old
= mstate
->dtms_scratch_ptr
, saved
;
5770 uint16_t *flags
= &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
5774 * Should be taking a faster path if string space has not been
5777 ASSERT(strsize
!= 0);
5780 * We will first allocate some temporary space for the frame pointers.
5782 fps
= (uint64_t *)P2ROUNDUP(mstate
->dtms_scratch_ptr
, 8);
5783 size
= (uintptr_t)fps
- mstate
->dtms_scratch_ptr
+
5784 (nframes
* sizeof (uint64_t));
5786 if (!DTRACE_INSCRATCH(mstate
, (uintptr_t)size
)) {
5788 * Not enough room for our frame pointers -- need to indicate
5789 * that we ran out of scratch space.
5791 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
5795 mstate
->dtms_scratch_ptr
+= size
;
5796 saved
= mstate
->dtms_scratch_ptr
;
5799 * Now get a stack with both program counters and frame pointers.
5801 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
5802 dtrace_getufpstack(buf
, fps
, nframes
+ 1);
5803 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
5806 * If that faulted, we're cooked.
5808 if (*flags
& CPU_DTRACE_FAULT
)
5812 * Now we want to walk up the stack, calling the USTACK helper. For
5813 * each iteration, we restore the scratch pointer.
5815 for (i
= 0; i
< nframes
; i
++) {
5816 mstate
->dtms_scratch_ptr
= saved
;
5818 if (offs
>= strsize
)
5821 sym
= (char *)(uintptr_t)dtrace_helper(
5822 DTRACE_HELPER_ACTION_USTACK
,
5823 mstate
, state
, pcs
[i
], fps
[i
]);
5826 * If we faulted while running the helper, we're going to
5827 * clear the fault and null out the corresponding string.
5829 if (*flags
& CPU_DTRACE_FAULT
) {
5830 *flags
&= ~CPU_DTRACE_FAULT
;
5840 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
5843 * Now copy in the string that the helper returned to us.
5845 for (j
= 0; offs
+ j
< strsize
; j
++) {
5846 if ((str
[offs
+ j
] = sym
[j
]) == '\0')
5850 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
5855 if (offs
>= strsize
) {
5857 * If we didn't have room for all of the strings, we don't
5858 * abort processing -- this needn't be a fatal error -- but we
5859 * still want to increment a counter (dts_stkstroverflows) to
5860 * allow this condition to be warned about. (If this is from
5861 * a jstack() action, it is easily tuned via jstackstrsize.)
5863 dtrace_error(&state
->dts_stkstroverflows
);
5866 while (offs
< strsize
)
5870 mstate
->dtms_scratch_ptr
= old
;
5874 * If you're looking for the epicenter of DTrace, you just found it. This
5875 * is the function called by the provider to fire a probe -- from which all
5876 * subsequent probe-context DTrace activity emanates.
5879 __dtrace_probe(dtrace_id_t id
, uint64_t arg0
, uint64_t arg1
,
5880 uint64_t arg2
, uint64_t arg3
, uint64_t arg4
)
5882 processorid_t cpuid
;
5883 dtrace_icookie_t cookie
;
5884 dtrace_probe_t
*probe
;
5885 dtrace_mstate_t mstate
;
5887 dtrace_action_t
*act
;
5891 volatile uint16_t *flags
;
5894 cookie
= dtrace_interrupt_disable();
5895 probe
= dtrace_probes
[id
- 1];
5896 cpuid
= CPU
->cpu_id
;
5897 onintr
= CPU_ON_INTR(CPU
);
5899 if (!onintr
&& probe
->dtpr_predcache
!= DTRACE_CACHEIDNONE
&&
5900 probe
->dtpr_predcache
== dtrace_get_thread_predcache(current_thread())) {
5902 * We have hit in the predicate cache; we know that
5903 * this predicate would evaluate to be false.
5905 dtrace_interrupt_enable(cookie
);
5909 if (panic_quiesce
) {
5911 * We don't trace anything if we're panicking.
5913 dtrace_interrupt_enable(cookie
);
5917 #if !defined(__APPLE__)
5918 now
= dtrace_gethrtime();
5919 vtime
= dtrace_vtime_references
!= 0;
5921 if (vtime
&& curthread
->t_dtrace_start
)
5922 curthread
->t_dtrace_vtime
+= now
- curthread
->t_dtrace_start
;
5925 * APPLE NOTE: The time spent entering DTrace and arriving
5926 * to this point, is attributed to the current thread.
5927 * Instead it should accrue to DTrace. FIXME
5929 vtime
= dtrace_vtime_references
!= 0;
5933 int64_t dtrace_accum_time
, recent_vtime
;
5934 thread_t thread
= current_thread();
5936 dtrace_accum_time
= dtrace_get_thread_tracing(thread
); /* Time spent inside DTrace so far (nanoseconds) */
5938 if (dtrace_accum_time
>= 0) {
5939 recent_vtime
= dtrace_abs_to_nano(dtrace_calc_thread_recent_vtime(thread
)); /* up to the moment thread vtime */
5941 recent_vtime
= recent_vtime
- dtrace_accum_time
; /* Time without DTrace contribution */
5943 dtrace_set_thread_vtime(thread
, recent_vtime
);
5947 now
= dtrace_gethrtime(); /* must not precede dtrace_calc_thread_recent_vtime() call! */
5948 #endif /* __APPLE__ */
5951 * APPLE NOTE: A provider may call dtrace_probe_error() in lieu of
5952 * dtrace_probe() in some circumstances. See, e.g. fasttrap_isa.c.
5953 * However the provider has no access to ECB context, so passes
5954 * 0 through "arg0" and the probe_id of the overridden probe as arg1.
5955 * Detect that here and cons up a viable state (from the probe_id).
5957 if (dtrace_probeid_error
== id
&& 0 == arg0
) {
5958 dtrace_id_t ftp_id
= (dtrace_id_t
)arg1
;
5959 dtrace_probe_t
*ftp_probe
= dtrace_probes
[ftp_id
- 1];
5960 dtrace_ecb_t
*ftp_ecb
= ftp_probe
->dtpr_ecb
;
5962 if (NULL
!= ftp_ecb
) {
5963 dtrace_state_t
*ftp_state
= ftp_ecb
->dte_state
;
5965 arg0
= (uint64_t)(uintptr_t)ftp_state
;
5966 arg1
= ftp_ecb
->dte_epid
;
5968 * args[2-4] established by caller.
5970 ftp_state
->dts_arg_error_illval
= -1; /* arg5 */
5974 mstate
.dtms_difo
= NULL
;
5975 mstate
.dtms_probe
= probe
;
5976 mstate
.dtms_strtok
= 0;
5977 mstate
.dtms_arg
[0] = arg0
;
5978 mstate
.dtms_arg
[1] = arg1
;
5979 mstate
.dtms_arg
[2] = arg2
;
5980 mstate
.dtms_arg
[3] = arg3
;
5981 mstate
.dtms_arg
[4] = arg4
;
5983 flags
= (volatile uint16_t *)&cpu_core
[cpuid
].cpuc_dtrace_flags
;
5985 for (ecb
= probe
->dtpr_ecb
; ecb
!= NULL
; ecb
= ecb
->dte_next
) {
5986 dtrace_predicate_t
*pred
= ecb
->dte_predicate
;
5987 dtrace_state_t
*state
= ecb
->dte_state
;
5988 dtrace_buffer_t
*buf
= &state
->dts_buffer
[cpuid
];
5989 dtrace_buffer_t
*aggbuf
= &state
->dts_aggbuffer
[cpuid
];
5990 dtrace_vstate_t
*vstate
= &state
->dts_vstate
;
5991 dtrace_provider_t
*prov
= probe
->dtpr_provider
;
5992 uint64_t tracememsize
= 0;
5997 * A little subtlety with the following (seemingly innocuous)
5998 * declaration of the automatic 'val': by looking at the
5999 * code, you might think that it could be declared in the
6000 * action processing loop, below. (That is, it's only used in
6001 * the action processing loop.) However, it must be declared
6002 * out of that scope because in the case of DIF expression
6003 * arguments to aggregating actions, one iteration of the
6004 * action loop will use the last iteration's value.
6012 mstate
.dtms_present
= DTRACE_MSTATE_ARGS
| DTRACE_MSTATE_PROBE
;
6013 *flags
&= ~CPU_DTRACE_ERROR
;
6015 if (prov
== dtrace_provider
) {
6017 * If dtrace itself is the provider of this probe,
6018 * we're only going to continue processing the ECB if
6019 * arg0 (the dtrace_state_t) is equal to the ECB's
6020 * creating state. (This prevents disjoint consumers
6021 * from seeing one another's metaprobes.)
6023 if (arg0
!= (uint64_t)(uintptr_t)state
)
6027 if (state
->dts_activity
!= DTRACE_ACTIVITY_ACTIVE
) {
6029 * We're not currently active. If our provider isn't
6030 * the dtrace pseudo provider, we're not interested.
6032 if (prov
!= dtrace_provider
)
6036 * Now we must further check if we are in the BEGIN
6037 * probe. If we are, we will only continue processing
6038 * if we're still in WARMUP -- if one BEGIN enabling
6039 * has invoked the exit() action, we don't want to
6040 * evaluate subsequent BEGIN enablings.
6042 if (probe
->dtpr_id
== dtrace_probeid_begin
&&
6043 state
->dts_activity
!= DTRACE_ACTIVITY_WARMUP
) {
6044 ASSERT(state
->dts_activity
==
6045 DTRACE_ACTIVITY_DRAINING
);
6050 if (ecb
->dte_cond
) {
6052 * If the dte_cond bits indicate that this
6053 * consumer is only allowed to see user-mode firings
6054 * of this probe, call the provider's dtps_usermode()
6055 * entry point to check that the probe was fired
6056 * while in a user context. Skip this ECB if that's
6059 if ((ecb
->dte_cond
& DTRACE_COND_USERMODE
) &&
6060 prov
->dtpv_pops
.dtps_usermode(prov
->dtpv_arg
,
6061 probe
->dtpr_id
, probe
->dtpr_arg
) == 0)
6065 * This is more subtle than it looks. We have to be
6066 * absolutely certain that CRED() isn't going to
6067 * change out from under us so it's only legit to
6068 * examine that structure if we're in constrained
6069 * situations. Currently, the only times we'll this
6070 * check is if a non-super-user has enabled the
6071 * profile or syscall providers -- providers that
6072 * allow visibility of all processes. For the
6073 * profile case, the check above will ensure that
6074 * we're examining a user context.
6076 if (ecb
->dte_cond
& DTRACE_COND_OWNER
) {
6079 ecb
->dte_state
->dts_cred
.dcr_cred
;
6081 #pragma unused(proc) /* __APPLE__ */
6083 ASSERT(s_cr
!= NULL
);
6086 * XXX this is hackish, but so is setting a variable
6087 * XXX in a McCarthy OR...
6089 if ((cr
= dtrace_CRED()) == NULL
||
6090 posix_cred_get(s_cr
)->cr_uid
!= posix_cred_get(cr
)->cr_uid
||
6091 posix_cred_get(s_cr
)->cr_uid
!= posix_cred_get(cr
)->cr_ruid
||
6092 posix_cred_get(s_cr
)->cr_uid
!= posix_cred_get(cr
)->cr_suid
||
6093 posix_cred_get(s_cr
)->cr_gid
!= posix_cred_get(cr
)->cr_gid
||
6094 posix_cred_get(s_cr
)->cr_gid
!= posix_cred_get(cr
)->cr_rgid
||
6095 posix_cred_get(s_cr
)->cr_gid
!= posix_cred_get(cr
)->cr_sgid
||
6096 #if !defined(__APPLE__)
6097 (proc
= ttoproc(curthread
)) == NULL
||
6098 (proc
->p_flag
& SNOCD
))
6100 1) /* APPLE NOTE: Darwin omits "No Core Dump" flag */
6101 #endif /* __APPLE__ */
6105 if (ecb
->dte_cond
& DTRACE_COND_ZONEOWNER
) {
6108 ecb
->dte_state
->dts_cred
.dcr_cred
;
6109 #pragma unused(cr, s_cr) /* __APPLE__ */
6111 ASSERT(s_cr
!= NULL
);
6113 #if !defined(__APPLE__)
6114 if ((cr
= CRED()) == NULL
||
6115 s_cr
->cr_zone
->zone_id
!=
6116 cr
->cr_zone
->zone_id
)
6119 /* APPLE NOTE: Darwin doesn't do zones. */
6120 #endif /* __APPLE__ */
6124 if (now
- state
->dts_alive
> dtrace_deadman_timeout
) {
6126 * We seem to be dead. Unless we (a) have kernel
6127 * destructive permissions (b) have expicitly enabled
6128 * destructive actions and (c) destructive actions have
6129 * not been disabled, we're going to transition into
6130 * the KILLED state, from which no further processing
6131 * on this state will be performed.
6133 if (!dtrace_priv_kernel_destructive(state
) ||
6134 !state
->dts_cred
.dcr_destructive
||
6135 dtrace_destructive_disallow
) {
6136 void *activity
= &state
->dts_activity
;
6137 dtrace_activity_t current
;
6140 current
= state
->dts_activity
;
6141 } while (dtrace_cas32(activity
, current
,
6142 DTRACE_ACTIVITY_KILLED
) != current
);
6148 if ((offs
= dtrace_buffer_reserve(buf
, ecb
->dte_needed
,
6149 ecb
->dte_alignment
, state
, &mstate
)) < 0)
6152 tomax
= buf
->dtb_tomax
;
6153 ASSERT(tomax
!= NULL
);
6156 * Build and store the record header corresponding to the ECB.
6158 if (ecb
->dte_size
!= 0) {
6159 dtrace_rechdr_t dtrh
;
6161 if (!(mstate
.dtms_present
& DTRACE_MSTATE_TIMESTAMP
)) {
6162 mstate
.dtms_timestamp
= dtrace_gethrtime();
6163 mstate
.dtms_present
|= DTRACE_MSTATE_TIMESTAMP
;
6166 ASSERT(ecb
->dte_size
>= sizeof(dtrace_rechdr_t
));
6168 dtrh
.dtrh_epid
= ecb
->dte_epid
;
6169 DTRACE_RECORD_STORE_TIMESTAMP(&dtrh
, mstate
.dtms_timestamp
);
6170 DTRACE_STORE(dtrace_rechdr_t
, tomax
, offs
, dtrh
);
6173 mstate
.dtms_epid
= ecb
->dte_epid
;
6174 mstate
.dtms_present
|= DTRACE_MSTATE_EPID
;
6176 if (state
->dts_cred
.dcr_visible
& DTRACE_CRV_KERNEL
)
6177 mstate
.dtms_access
= DTRACE_ACCESS_KERNEL
;
6179 mstate
.dtms_access
= 0;
6182 dtrace_difo_t
*dp
= pred
->dtp_difo
;
6185 rval
= dtrace_dif_emulate(dp
, &mstate
, vstate
, state
);
6187 if (!(*flags
& CPU_DTRACE_ERROR
) && !rval
) {
6188 dtrace_cacheid_t cid
= probe
->dtpr_predcache
;
6190 if (cid
!= DTRACE_CACHEIDNONE
&& !onintr
) {
6192 * Update the predicate cache...
6194 ASSERT(cid
== pred
->dtp_cacheid
);
6196 dtrace_set_thread_predcache(current_thread(), cid
);
6203 for (act
= ecb
->dte_action
; !(*flags
& CPU_DTRACE_ERROR
) &&
6204 act
!= NULL
; act
= act
->dta_next
) {
6207 dtrace_recdesc_t
*rec
= &act
->dta_rec
;
6209 size
= rec
->dtrd_size
;
6210 valoffs
= offs
+ rec
->dtrd_offset
;
6212 if (DTRACEACT_ISAGG(act
->dta_kind
)) {
6214 dtrace_aggregation_t
*agg
;
6216 agg
= (dtrace_aggregation_t
*)act
;
6218 if ((dp
= act
->dta_difo
) != NULL
)
6219 v
= dtrace_dif_emulate(dp
,
6220 &mstate
, vstate
, state
);
6222 if (*flags
& CPU_DTRACE_ERROR
)
6226 * Note that we always pass the expression
6227 * value from the previous iteration of the
6228 * action loop. This value will only be used
6229 * if there is an expression argument to the
6230 * aggregating action, denoted by the
6231 * dtag_hasarg field.
6233 dtrace_aggregate(agg
, buf
,
6234 offs
, aggbuf
, v
, val
);
6238 switch (act
->dta_kind
) {
6239 case DTRACEACT_STOP
:
6240 if (dtrace_priv_proc_destructive(state
))
6241 dtrace_action_stop();
6244 case DTRACEACT_BREAKPOINT
:
6245 if (dtrace_priv_kernel_destructive(state
))
6246 dtrace_action_breakpoint(ecb
);
6249 case DTRACEACT_PANIC
:
6250 if (dtrace_priv_kernel_destructive(state
))
6251 dtrace_action_panic(ecb
);
6254 case DTRACEACT_STACK
:
6255 if (!dtrace_priv_kernel(state
))
6258 dtrace_getpcstack((pc_t
*)(tomax
+ valoffs
),
6259 size
/ sizeof (pc_t
), probe
->dtpr_aframes
,
6260 DTRACE_ANCHORED(probe
) ? NULL
:
6261 (uint32_t *)(uintptr_t)arg0
);
6264 case DTRACEACT_JSTACK
:
6265 case DTRACEACT_USTACK
:
6266 if (!dtrace_priv_proc(state
))
6270 * See comment in DIF_VAR_PID.
6272 if (DTRACE_ANCHORED(mstate
.dtms_probe
) &&
6274 int depth
= DTRACE_USTACK_NFRAMES(
6277 dtrace_bzero((void *)(tomax
+ valoffs
),
6278 DTRACE_USTACK_STRSIZE(rec
->dtrd_arg
)
6279 + depth
* sizeof (uint64_t));
6284 if (DTRACE_USTACK_STRSIZE(rec
->dtrd_arg
) != 0 &&
6285 curproc
->p_dtrace_helpers
!= NULL
) {
6287 * This is the slow path -- we have
6288 * allocated string space, and we're
6289 * getting the stack of a process that
6290 * has helpers. Call into a separate
6291 * routine to perform this processing.
6293 dtrace_action_ustack(&mstate
, state
,
6294 (uint64_t *)(tomax
+ valoffs
),
6299 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
6300 dtrace_getupcstack((uint64_t *)
6302 DTRACE_USTACK_NFRAMES(rec
->dtrd_arg
) + 1);
6303 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
6313 val
= dtrace_dif_emulate(dp
, &mstate
, vstate
, state
);
6315 if (*flags
& CPU_DTRACE_ERROR
)
6318 switch (act
->dta_kind
) {
6319 case DTRACEACT_SPECULATE
: {
6320 dtrace_rechdr_t
*dtrh
= NULL
;
6322 ASSERT(buf
== &state
->dts_buffer
[cpuid
]);
6323 buf
= dtrace_speculation_buffer(state
,
6327 *flags
|= CPU_DTRACE_DROP
;
6331 offs
= dtrace_buffer_reserve(buf
,
6332 ecb
->dte_needed
, ecb
->dte_alignment
,
6336 *flags
|= CPU_DTRACE_DROP
;
6340 tomax
= buf
->dtb_tomax
;
6341 ASSERT(tomax
!= NULL
);
6343 if (ecb
->dte_size
!= 0)
6346 ASSERT(ecb
->dte_size
>= sizeof(dtrace_rechdr_t
));
6347 dtrh
= ((void *)(tomax
+ offs
));
6348 dtrh
->dtrh_epid
= ecb
->dte_epid
;
6351 * When the speculation is committed, all of
6352 * the records in the speculative buffer will
6353 * have their timestamps set to the commit
6354 * time. Until then, it is set to a sentinel
6355 * value, for debugability.
6357 DTRACE_RECORD_STORE_TIMESTAMP(dtrh
, UINT64_MAX
);
6362 case DTRACEACT_CHILL
:
6363 if (dtrace_priv_kernel_destructive(state
))
6364 dtrace_action_chill(&mstate
, val
);
6367 case DTRACEACT_RAISE
:
6368 if (dtrace_priv_proc_destructive(state
))
6369 dtrace_action_raise(val
);
6372 case DTRACEACT_PIDRESUME
: /* __APPLE__ */
6373 if (dtrace_priv_proc_destructive(state
))
6374 dtrace_action_pidresume(val
);
6377 case DTRACEACT_COMMIT
:
6381 * We need to commit our buffer state.
6384 buf
->dtb_offset
= offs
+ ecb
->dte_size
;
6385 buf
= &state
->dts_buffer
[cpuid
];
6386 dtrace_speculation_commit(state
, cpuid
, val
);
6390 case DTRACEACT_DISCARD
:
6391 dtrace_speculation_discard(state
, cpuid
, val
);
6394 case DTRACEACT_DIFEXPR
:
6395 case DTRACEACT_LIBACT
:
6396 case DTRACEACT_PRINTF
:
6397 case DTRACEACT_PRINTA
:
6398 case DTRACEACT_SYSTEM
:
6399 case DTRACEACT_FREOPEN
:
6400 case DTRACEACT_APPLEBINARY
: /* __APPLE__ */
6401 case DTRACEACT_TRACEMEM
:
6404 case DTRACEACT_TRACEMEM_DYNSIZE
:
6410 if (!dtrace_priv_kernel(state
))
6414 case DTRACEACT_USYM
:
6415 case DTRACEACT_UMOD
:
6416 case DTRACEACT_UADDR
: {
6417 if (!dtrace_priv_proc(state
))
6420 DTRACE_STORE(uint64_t, tomax
,
6421 valoffs
, (uint64_t)dtrace_proc_selfpid());
6422 DTRACE_STORE(uint64_t, tomax
,
6423 valoffs
+ sizeof (uint64_t), val
);
6428 case DTRACEACT_EXIT
: {
6430 * For the exit action, we are going to attempt
6431 * to atomically set our activity to be
6432 * draining. If this fails (either because
6433 * another CPU has beat us to the exit action,
6434 * or because our current activity is something
6435 * other than ACTIVE or WARMUP), we will
6436 * continue. This assures that the exit action
6437 * can be successfully recorded at most once
6438 * when we're in the ACTIVE state. If we're
6439 * encountering the exit() action while in
6440 * COOLDOWN, however, we want to honor the new
6441 * status code. (We know that we're the only
6442 * thread in COOLDOWN, so there is no race.)
6444 void *activity
= &state
->dts_activity
;
6445 dtrace_activity_t current
= state
->dts_activity
;
6447 if (current
== DTRACE_ACTIVITY_COOLDOWN
)
6450 if (current
!= DTRACE_ACTIVITY_WARMUP
)
6451 current
= DTRACE_ACTIVITY_ACTIVE
;
6453 if (dtrace_cas32(activity
, current
,
6454 DTRACE_ACTIVITY_DRAINING
) != current
) {
6455 *flags
|= CPU_DTRACE_DROP
;
6466 if (dp
->dtdo_rtype
.dtdt_flags
& DIF_TF_BYREF
) {
6467 uintptr_t end
= valoffs
+ size
;
6469 if (tracememsize
!= 0 &&
6470 valoffs
+ tracememsize
< end
)
6472 end
= valoffs
+ tracememsize
;
6476 if (!dtrace_vcanload((void *)(uintptr_t)val
,
6477 &dp
->dtdo_rtype
, &mstate
, vstate
))
6481 * If this is a string, we're going to only
6482 * load until we find the zero byte -- after
6483 * which we'll store zero bytes.
6485 if (dp
->dtdo_rtype
.dtdt_kind
==
6488 int intuple
= act
->dta_intuple
;
6491 for (s
= 0; s
< size
; s
++) {
6493 c
= dtrace_load8(val
++);
6495 DTRACE_STORE(uint8_t, tomax
,
6498 if (c
== '\0' && intuple
)
6505 while (valoffs
< end
) {
6506 DTRACE_STORE(uint8_t, tomax
, valoffs
++,
6507 dtrace_load8(val
++));
6517 case sizeof (uint8_t):
6518 DTRACE_STORE(uint8_t, tomax
, valoffs
, val
);
6520 case sizeof (uint16_t):
6521 DTRACE_STORE(uint16_t, tomax
, valoffs
, val
);
6523 case sizeof (uint32_t):
6524 DTRACE_STORE(uint32_t, tomax
, valoffs
, val
);
6526 case sizeof (uint64_t):
6527 DTRACE_STORE(uint64_t, tomax
, valoffs
, val
);
6531 * Any other size should have been returned by
6532 * reference, not by value.
6539 if (*flags
& CPU_DTRACE_DROP
)
6542 if (*flags
& CPU_DTRACE_FAULT
) {
6544 dtrace_action_t
*err
;
6548 if (probe
->dtpr_id
== dtrace_probeid_error
) {
6550 * There's nothing we can do -- we had an
6551 * error on the error probe. We bump an
6552 * error counter to at least indicate that
6553 * this condition happened.
6555 dtrace_error(&state
->dts_dblerrors
);
6561 * Before recursing on dtrace_probe(), we
6562 * need to explicitly clear out our start
6563 * time to prevent it from being accumulated
6564 * into t_dtrace_vtime.
6568 * Darwin sets the sign bit on t_dtrace_tracing
6569 * to suspend accumulation to it.
6571 dtrace_set_thread_tracing(current_thread(),
6572 (1ULL<<63) | dtrace_get_thread_tracing(current_thread()));
6577 * Iterate over the actions to figure out which action
6578 * we were processing when we experienced the error.
6579 * Note that act points _past_ the faulting action; if
6580 * act is ecb->dte_action, the fault was in the
6581 * predicate, if it's ecb->dte_action->dta_next it's
6582 * in action #1, and so on.
6584 for (err
= ecb
->dte_action
, ndx
= 0;
6585 err
!= act
; err
= err
->dta_next
, ndx
++)
6588 dtrace_probe_error(state
, ecb
->dte_epid
, ndx
,
6589 (mstate
.dtms_present
& DTRACE_MSTATE_FLTOFFS
) ?
6590 mstate
.dtms_fltoffs
: -1, DTRACE_FLAGS2FLT(*flags
),
6591 cpu_core
[cpuid
].cpuc_dtrace_illval
);
6597 buf
->dtb_offset
= offs
+ ecb
->dte_size
;
6600 /* FIXME: On Darwin the time spent leaving DTrace from this point to the rti is attributed
6601 to the current thread. Instead it should accrue to DTrace. */
6603 thread_t thread
= current_thread();
6604 int64_t t
= dtrace_get_thread_tracing(thread
);
6607 /* Usual case, accumulate time spent here into t_dtrace_tracing */
6608 dtrace_set_thread_tracing(thread
, t
+ (dtrace_gethrtime() - now
));
6610 /* Return from error recursion. No accumulation, just clear the sign bit on t_dtrace_tracing. */
6611 dtrace_set_thread_tracing(thread
, (~(1ULL<<63)) & t
);
6615 dtrace_interrupt_enable(cookie
);
6619 * APPLE NOTE: Don't allow a thread to re-enter dtrace_probe().
6620 * This could occur if a probe is encountered on some function in the
6621 * transitive closure of the call to dtrace_probe().
6622 * Solaris has some strong guarantees that this won't happen.
6623 * The Darwin implementation is not so mature as to make those guarantees.
6624 * Hence, the introduction of __dtrace_probe() on xnu.
6628 dtrace_probe(dtrace_id_t id
, uint64_t arg0
, uint64_t arg1
,
6629 uint64_t arg2
, uint64_t arg3
, uint64_t arg4
)
6631 thread_t thread
= current_thread();
6632 disable_preemption();
6633 if (id
== dtrace_probeid_error
) {
6634 __dtrace_probe(id
, arg0
, arg1
, arg2
, arg3
, arg4
);
6635 dtrace_getipl(); /* Defeat tail-call optimization of __dtrace_probe() */
6636 } else if (!dtrace_get_thread_reentering(thread
)) {
6637 dtrace_set_thread_reentering(thread
, TRUE
);
6638 __dtrace_probe(id
, arg0
, arg1
, arg2
, arg3
, arg4
);
6639 dtrace_set_thread_reentering(thread
, FALSE
);
6642 else __dtrace_probe(dtrace_probeid_error
, 0, id
, 1, -1, DTRACEFLT_UNKNOWN
);
6644 enable_preemption();
6648 * DTrace Probe Hashing Functions
6650 * The functions in this section (and indeed, the functions in remaining
6651 * sections) are not _called_ from probe context. (Any exceptions to this are
6652 * marked with a "Note:".) Rather, they are called from elsewhere in the
6653 * DTrace framework to look-up probes in, add probes to and remove probes from
6654 * the DTrace probe hashes. (Each probe is hashed by each element of the
6655 * probe tuple -- allowing for fast lookups, regardless of what was
6659 dtrace_hash_str(const char *p
)
6665 hval
= (hval
<< 4) + *p
++;
6666 if ((g
= (hval
& 0xf0000000)) != 0)
6673 static dtrace_hash_t
*
6674 dtrace_hash_create(uintptr_t stroffs
, uintptr_t nextoffs
, uintptr_t prevoffs
)
6676 dtrace_hash_t
*hash
= kmem_zalloc(sizeof (dtrace_hash_t
), KM_SLEEP
);
6678 hash
->dth_stroffs
= stroffs
;
6679 hash
->dth_nextoffs
= nextoffs
;
6680 hash
->dth_prevoffs
= prevoffs
;
6683 hash
->dth_mask
= hash
->dth_size
- 1;
6685 hash
->dth_tab
= kmem_zalloc(hash
->dth_size
*
6686 sizeof (dtrace_hashbucket_t
*), KM_SLEEP
);
6692 * APPLE NOTE: dtrace_hash_destroy is not used.
6693 * It is called by dtrace_detach which is not
6694 * currently implemented. Revisit someday.
6696 #if !defined(__APPLE__)
6698 dtrace_hash_destroy(dtrace_hash_t
*hash
)
6703 for (i
= 0; i
< hash
->dth_size
; i
++)
6704 ASSERT(hash
->dth_tab
[i
] == NULL
);
6707 kmem_free(hash
->dth_tab
,
6708 hash
->dth_size
* sizeof (dtrace_hashbucket_t
*));
6709 kmem_free(hash
, sizeof (dtrace_hash_t
));
6711 #endif /* __APPLE__ */
6714 dtrace_hash_resize(dtrace_hash_t
*hash
)
6716 int size
= hash
->dth_size
, i
, ndx
;
6717 int new_size
= hash
->dth_size
<< 1;
6718 int new_mask
= new_size
- 1;
6719 dtrace_hashbucket_t
**new_tab
, *bucket
, *next
;
6721 ASSERT((new_size
& new_mask
) == 0);
6723 new_tab
= kmem_zalloc(new_size
* sizeof (void *), KM_SLEEP
);
6725 for (i
= 0; i
< size
; i
++) {
6726 for (bucket
= hash
->dth_tab
[i
]; bucket
!= NULL
; bucket
= next
) {
6727 dtrace_probe_t
*probe
= bucket
->dthb_chain
;
6729 ASSERT(probe
!= NULL
);
6730 ndx
= DTRACE_HASHSTR(hash
, probe
) & new_mask
;
6732 next
= bucket
->dthb_next
;
6733 bucket
->dthb_next
= new_tab
[ndx
];
6734 new_tab
[ndx
] = bucket
;
6738 kmem_free(hash
->dth_tab
, hash
->dth_size
* sizeof (void *));
6739 hash
->dth_tab
= new_tab
;
6740 hash
->dth_size
= new_size
;
6741 hash
->dth_mask
= new_mask
;
6745 dtrace_hash_add(dtrace_hash_t
*hash
, dtrace_probe_t
*new)
6747 int hashval
= DTRACE_HASHSTR(hash
, new);
6748 int ndx
= hashval
& hash
->dth_mask
;
6749 dtrace_hashbucket_t
*bucket
= hash
->dth_tab
[ndx
];
6750 dtrace_probe_t
**nextp
, **prevp
;
6752 for (; bucket
!= NULL
; bucket
= bucket
->dthb_next
) {
6753 if (DTRACE_HASHEQ(hash
, bucket
->dthb_chain
, new))
6757 if ((hash
->dth_nbuckets
>> 1) > hash
->dth_size
) {
6758 dtrace_hash_resize(hash
);
6759 dtrace_hash_add(hash
, new);
6763 bucket
= kmem_zalloc(sizeof (dtrace_hashbucket_t
), KM_SLEEP
);
6764 bucket
->dthb_next
= hash
->dth_tab
[ndx
];
6765 hash
->dth_tab
[ndx
] = bucket
;
6766 hash
->dth_nbuckets
++;
6769 nextp
= DTRACE_HASHNEXT(hash
, new);
6770 ASSERT(*nextp
== NULL
&& *(DTRACE_HASHPREV(hash
, new)) == NULL
);
6771 *nextp
= bucket
->dthb_chain
;
6773 if (bucket
->dthb_chain
!= NULL
) {
6774 prevp
= DTRACE_HASHPREV(hash
, bucket
->dthb_chain
);
6775 ASSERT(*prevp
== NULL
);
6779 bucket
->dthb_chain
= new;
6783 static dtrace_probe_t
*
6784 dtrace_hash_lookup(dtrace_hash_t
*hash
, dtrace_probe_t
*template)
6786 int hashval
= DTRACE_HASHSTR(hash
, template);
6787 int ndx
= hashval
& hash
->dth_mask
;
6788 dtrace_hashbucket_t
*bucket
= hash
->dth_tab
[ndx
];
6790 for (; bucket
!= NULL
; bucket
= bucket
->dthb_next
) {
6791 if (DTRACE_HASHEQ(hash
, bucket
->dthb_chain
, template))
6792 return (bucket
->dthb_chain
);
6799 dtrace_hash_collisions(dtrace_hash_t
*hash
, dtrace_probe_t
*template)
6801 int hashval
= DTRACE_HASHSTR(hash
, template);
6802 int ndx
= hashval
& hash
->dth_mask
;
6803 dtrace_hashbucket_t
*bucket
= hash
->dth_tab
[ndx
];
6805 for (; bucket
!= NULL
; bucket
= bucket
->dthb_next
) {
6806 if (DTRACE_HASHEQ(hash
, bucket
->dthb_chain
, template))
6807 return (bucket
->dthb_len
);
6814 dtrace_hash_remove(dtrace_hash_t
*hash
, dtrace_probe_t
*probe
)
6816 int ndx
= DTRACE_HASHSTR(hash
, probe
) & hash
->dth_mask
;
6817 dtrace_hashbucket_t
*bucket
= hash
->dth_tab
[ndx
];
6819 dtrace_probe_t
**prevp
= DTRACE_HASHPREV(hash
, probe
);
6820 dtrace_probe_t
**nextp
= DTRACE_HASHNEXT(hash
, probe
);
6823 * Find the bucket that we're removing this probe from.
6825 for (; bucket
!= NULL
; bucket
= bucket
->dthb_next
) {
6826 if (DTRACE_HASHEQ(hash
, bucket
->dthb_chain
, probe
))
6830 ASSERT(bucket
!= NULL
);
6832 if (*prevp
== NULL
) {
6833 if (*nextp
== NULL
) {
6835 * The removed probe was the only probe on this
6836 * bucket; we need to remove the bucket.
6838 dtrace_hashbucket_t
*b
= hash
->dth_tab
[ndx
];
6840 ASSERT(bucket
->dthb_chain
== probe
);
6844 hash
->dth_tab
[ndx
] = bucket
->dthb_next
;
6846 while (b
->dthb_next
!= bucket
)
6848 b
->dthb_next
= bucket
->dthb_next
;
6851 ASSERT(hash
->dth_nbuckets
> 0);
6852 hash
->dth_nbuckets
--;
6853 kmem_free(bucket
, sizeof (dtrace_hashbucket_t
));
6857 bucket
->dthb_chain
= *nextp
;
6859 *(DTRACE_HASHNEXT(hash
, *prevp
)) = *nextp
;
6863 *(DTRACE_HASHPREV(hash
, *nextp
)) = *prevp
;
6867 * DTrace Utility Functions
6869 * These are random utility functions that are _not_ called from probe context.
6872 dtrace_badattr(const dtrace_attribute_t
*a
)
6874 return (a
->dtat_name
> DTRACE_STABILITY_MAX
||
6875 a
->dtat_data
> DTRACE_STABILITY_MAX
||
6876 a
->dtat_class
> DTRACE_CLASS_MAX
);
6880 * Return a duplicate copy of a string. If the specified string is NULL,
6881 * this function returns a zero-length string.
6882 * APPLE NOTE: Darwin employs size bounded string operation.
6885 dtrace_strdup(const char *str
)
6887 size_t bufsize
= (str
!= NULL
? strlen(str
) : 0) + 1;
6888 char *new = kmem_zalloc(bufsize
, KM_SLEEP
);
6891 (void) strlcpy(new, str
, bufsize
);
6896 #define DTRACE_ISALPHA(c) \
6897 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
6900 dtrace_badname(const char *s
)
6904 if (s
== NULL
|| (c
= *s
++) == '\0')
6907 if (!DTRACE_ISALPHA(c
) && c
!= '-' && c
!= '_' && c
!= '.')
6910 while ((c
= *s
++) != '\0') {
6911 if (!DTRACE_ISALPHA(c
) && (c
< '0' || c
> '9') &&
6912 c
!= '-' && c
!= '_' && c
!= '.' && c
!= '`')
6920 dtrace_cred2priv(cred_t
*cr
, uint32_t *privp
, uid_t
*uidp
, zoneid_t
*zoneidp
)
6924 if (cr
== NULL
|| PRIV_POLICY_ONLY(cr
, PRIV_ALL
, B_FALSE
)) {
6926 * For DTRACE_PRIV_ALL, the uid and zoneid don't matter.
6928 priv
= DTRACE_PRIV_ALL
;
6930 *uidp
= crgetuid(cr
);
6931 *zoneidp
= crgetzoneid(cr
);
6934 if (PRIV_POLICY_ONLY(cr
, PRIV_DTRACE_KERNEL
, B_FALSE
))
6935 priv
|= DTRACE_PRIV_KERNEL
| DTRACE_PRIV_USER
;
6936 else if (PRIV_POLICY_ONLY(cr
, PRIV_DTRACE_USER
, B_FALSE
))
6937 priv
|= DTRACE_PRIV_USER
;
6938 if (PRIV_POLICY_ONLY(cr
, PRIV_DTRACE_PROC
, B_FALSE
))
6939 priv
|= DTRACE_PRIV_PROC
;
6940 if (PRIV_POLICY_ONLY(cr
, PRIV_PROC_OWNER
, B_FALSE
))
6941 priv
|= DTRACE_PRIV_OWNER
;
6942 if (PRIV_POLICY_ONLY(cr
, PRIV_PROC_ZONE
, B_FALSE
))
6943 priv
|= DTRACE_PRIV_ZONEOWNER
;
6949 #ifdef DTRACE_ERRDEBUG
6951 dtrace_errdebug(const char *str
)
6953 int hval
= dtrace_hash_str(str
) % DTRACE_ERRHASHSZ
;
6956 lck_mtx_lock(&dtrace_errlock
);
6957 dtrace_errlast
= str
;
6958 dtrace_errthread
= (kthread_t
*)current_thread();
6960 while (occupied
++ < DTRACE_ERRHASHSZ
) {
6961 if (dtrace_errhash
[hval
].dter_msg
== str
) {
6962 dtrace_errhash
[hval
].dter_count
++;
6966 if (dtrace_errhash
[hval
].dter_msg
!= NULL
) {
6967 hval
= (hval
+ 1) % DTRACE_ERRHASHSZ
;
6971 dtrace_errhash
[hval
].dter_msg
= str
;
6972 dtrace_errhash
[hval
].dter_count
= 1;
6976 panic("dtrace: undersized error hash");
6978 lck_mtx_unlock(&dtrace_errlock
);
6983 * DTrace Matching Functions
6985 * These functions are used to match groups of probes, given some elements of
6986 * a probe tuple, or some globbed expressions for elements of a probe tuple.
6989 dtrace_match_priv(const dtrace_probe_t
*prp
, uint32_t priv
, uid_t uid
,
6992 if (priv
!= DTRACE_PRIV_ALL
) {
6993 uint32_t ppriv
= prp
->dtpr_provider
->dtpv_priv
.dtpp_flags
;
6994 uint32_t match
= priv
& ppriv
;
6997 * No PRIV_DTRACE_* privileges...
6999 if ((priv
& (DTRACE_PRIV_PROC
| DTRACE_PRIV_USER
|
7000 DTRACE_PRIV_KERNEL
)) == 0)
7004 * No matching bits, but there were bits to match...
7006 if (match
== 0 && ppriv
!= 0)
7010 * Need to have permissions to the process, but don't...
7012 if (((ppriv
& ~match
) & DTRACE_PRIV_OWNER
) != 0 &&
7013 uid
!= prp
->dtpr_provider
->dtpv_priv
.dtpp_uid
) {
7018 * Need to be in the same zone unless we possess the
7019 * privilege to examine all zones.
7021 if (((ppriv
& ~match
) & DTRACE_PRIV_ZONEOWNER
) != 0 &&
7022 zoneid
!= prp
->dtpr_provider
->dtpv_priv
.dtpp_zoneid
) {
7031 * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which
7032 * consists of input pattern strings and an ops-vector to evaluate them.
7033 * This function returns >0 for match, 0 for no match, and <0 for error.
7036 dtrace_match_probe(const dtrace_probe_t
*prp
, const dtrace_probekey_t
*pkp
,
7037 uint32_t priv
, uid_t uid
, zoneid_t zoneid
)
7039 dtrace_provider_t
*pvp
= prp
->dtpr_provider
;
7042 if (pvp
->dtpv_defunct
)
7045 if ((rv
= pkp
->dtpk_pmatch(pvp
->dtpv_name
, pkp
->dtpk_prov
, 0)) <= 0)
7048 if ((rv
= pkp
->dtpk_mmatch(prp
->dtpr_mod
, pkp
->dtpk_mod
, 0)) <= 0)
7051 if ((rv
= pkp
->dtpk_fmatch(prp
->dtpr_func
, pkp
->dtpk_func
, 0)) <= 0)
7054 if ((rv
= pkp
->dtpk_nmatch(prp
->dtpr_name
, pkp
->dtpk_name
, 0)) <= 0)
7057 if (dtrace_match_priv(prp
, priv
, uid
, zoneid
) == 0)
7064 * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN)
7065 * interface for matching a glob pattern 'p' to an input string 's'. Unlike
7066 * libc's version, the kernel version only applies to 8-bit ASCII strings.
7067 * In addition, all of the recursion cases except for '*' matching have been
7068 * unwound. For '*', we still implement recursive evaluation, but a depth
7069 * counter is maintained and matching is aborted if we recurse too deep.
7070 * The function returns 0 if no match, >0 if match, and <0 if recursion error.
7073 dtrace_match_glob(const char *s
, const char *p
, int depth
)
7079 if (depth
> DTRACE_PROBEKEY_MAXDEPTH
)
7083 s
= ""; /* treat NULL as empty string */
7092 if ((c
= *p
++) == '\0')
7093 return (s1
== '\0');
7097 int ok
= 0, notflag
= 0;
7108 if ((c
= *p
++) == '\0')
7112 if (c
== '-' && lc
!= '\0' && *p
!= ']') {
7113 if ((c
= *p
++) == '\0')
7115 if (c
== '\\' && (c
= *p
++) == '\0')
7119 if (s1
< lc
|| s1
> c
)
7123 } else if (lc
<= s1
&& s1
<= c
)
7126 } else if (c
== '\\' && (c
= *p
++) == '\0')
7129 lc
= c
; /* save left-hand 'c' for next iteration */
7139 if ((c
= *p
++) == '\0')
7151 if ((c
= *p
++) == '\0')
7167 p
++; /* consecutive *'s are identical to a single one */
7172 for (s
= olds
; *s
!= '\0'; s
++) {
7173 if ((gs
= dtrace_match_glob(s
, p
, depth
+ 1)) != 0)
7183 dtrace_match_string(const char *s
, const char *p
, int depth
)
7185 #pragma unused(depth) /* __APPLE__ */
7187 /* APPLE NOTE: Darwin employs size bounded string operation. */
7188 return (s
!= NULL
&& strncmp(s
, p
, strlen(s
) + 1) == 0);
7193 dtrace_match_nul(const char *s
, const char *p
, int depth
)
7195 #pragma unused(s, p, depth) /* __APPLE__ */
7196 return (1); /* always match the empty pattern */
7201 dtrace_match_nonzero(const char *s
, const char *p
, int depth
)
7203 #pragma unused(p, depth) /* __APPLE__ */
7204 return (s
!= NULL
&& s
[0] != '\0');
7208 dtrace_match(const dtrace_probekey_t
*pkp
, uint32_t priv
, uid_t uid
,
7209 zoneid_t zoneid
, int (*matched
)(dtrace_probe_t
*, void *), void *arg
)
7211 dtrace_probe_t
template, *probe
;
7212 dtrace_hash_t
*hash
= NULL
;
7213 int len
, rc
, best
= INT_MAX
, nmatched
= 0;
7216 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
7219 * If the probe ID is specified in the key, just lookup by ID and
7220 * invoke the match callback once if a matching probe is found.
7222 if (pkp
->dtpk_id
!= DTRACE_IDNONE
) {
7223 if ((probe
= dtrace_probe_lookup_id(pkp
->dtpk_id
)) != NULL
&&
7224 dtrace_match_probe(probe
, pkp
, priv
, uid
, zoneid
) > 0) {
7225 if ((*matched
)(probe
, arg
) == DTRACE_MATCH_FAIL
)
7226 return (DTRACE_MATCH_FAIL
);
7232 template.dtpr_mod
= (char *)(uintptr_t)pkp
->dtpk_mod
;
7233 template.dtpr_func
= (char *)(uintptr_t)pkp
->dtpk_func
;
7234 template.dtpr_name
= (char *)(uintptr_t)pkp
->dtpk_name
;
7237 * We want to find the most distinct of the module name, function
7238 * name, and name. So for each one that is not a glob pattern or
7239 * empty string, we perform a lookup in the corresponding hash and
7240 * use the hash table with the fewest collisions to do our search.
7242 if (pkp
->dtpk_mmatch
== &dtrace_match_string
&&
7243 (len
= dtrace_hash_collisions(dtrace_bymod
, &template)) < best
) {
7245 hash
= dtrace_bymod
;
7248 if (pkp
->dtpk_fmatch
== &dtrace_match_string
&&
7249 (len
= dtrace_hash_collisions(dtrace_byfunc
, &template)) < best
) {
7251 hash
= dtrace_byfunc
;
7254 if (pkp
->dtpk_nmatch
== &dtrace_match_string
&&
7255 (len
= dtrace_hash_collisions(dtrace_byname
, &template)) < best
) {
7257 hash
= dtrace_byname
;
7261 * If we did not select a hash table, iterate over every probe and
7262 * invoke our callback for each one that matches our input probe key.
7265 for (i
= 0; i
< (dtrace_id_t
)dtrace_nprobes
; i
++) {
7266 if ((probe
= dtrace_probes
[i
]) == NULL
||
7267 dtrace_match_probe(probe
, pkp
, priv
, uid
,
7273 if ((rc
= (*matched
)(probe
, arg
)) != DTRACE_MATCH_NEXT
) {
7274 if (rc
== DTRACE_MATCH_FAIL
)
7275 return (DTRACE_MATCH_FAIL
);
7284 * If we selected a hash table, iterate over each probe of the same key
7285 * name and invoke the callback for every probe that matches the other
7286 * attributes of our input probe key.
7288 for (probe
= dtrace_hash_lookup(hash
, &template); probe
!= NULL
;
7289 probe
= *(DTRACE_HASHNEXT(hash
, probe
))) {
7291 if (dtrace_match_probe(probe
, pkp
, priv
, uid
, zoneid
) <= 0)
7296 if ((rc
= (*matched
)(probe
, arg
)) != DTRACE_MATCH_NEXT
) {
7297 if (rc
== DTRACE_MATCH_FAIL
)
7298 return (DTRACE_MATCH_FAIL
);
7307 * Return the function pointer dtrace_probecmp() should use to compare the
7308 * specified pattern with a string. For NULL or empty patterns, we select
7309 * dtrace_match_nul(). For glob pattern strings, we use dtrace_match_glob().
7310 * For non-empty non-glob strings, we use dtrace_match_string().
7312 static dtrace_probekey_f
*
7313 dtrace_probekey_func(const char *p
)
7317 if (p
== NULL
|| *p
== '\0')
7318 return (&dtrace_match_nul
);
7320 while ((c
= *p
++) != '\0') {
7321 if (c
== '[' || c
== '?' || c
== '*' || c
== '\\')
7322 return (&dtrace_match_glob
);
7325 return (&dtrace_match_string
);
7329 * Build a probe comparison key for use with dtrace_match_probe() from the
7330 * given probe description. By convention, a null key only matches anchored
7331 * probes: if each field is the empty string, reset dtpk_fmatch to
7332 * dtrace_match_nonzero().
7335 dtrace_probekey(const dtrace_probedesc_t
*pdp
, dtrace_probekey_t
*pkp
)
7337 pkp
->dtpk_prov
= pdp
->dtpd_provider
;
7338 pkp
->dtpk_pmatch
= dtrace_probekey_func(pdp
->dtpd_provider
);
7340 pkp
->dtpk_mod
= pdp
->dtpd_mod
;
7341 pkp
->dtpk_mmatch
= dtrace_probekey_func(pdp
->dtpd_mod
);
7343 pkp
->dtpk_func
= pdp
->dtpd_func
;
7344 pkp
->dtpk_fmatch
= dtrace_probekey_func(pdp
->dtpd_func
);
7346 pkp
->dtpk_name
= pdp
->dtpd_name
;
7347 pkp
->dtpk_nmatch
= dtrace_probekey_func(pdp
->dtpd_name
);
7349 pkp
->dtpk_id
= pdp
->dtpd_id
;
7351 if (pkp
->dtpk_id
== DTRACE_IDNONE
&&
7352 pkp
->dtpk_pmatch
== &dtrace_match_nul
&&
7353 pkp
->dtpk_mmatch
== &dtrace_match_nul
&&
7354 pkp
->dtpk_fmatch
== &dtrace_match_nul
&&
7355 pkp
->dtpk_nmatch
== &dtrace_match_nul
)
7356 pkp
->dtpk_fmatch
= &dtrace_match_nonzero
;
7360 * DTrace Provider-to-Framework API Functions
7362 * These functions implement much of the Provider-to-Framework API, as
7363 * described in <sys/dtrace.h>. The parts of the API not in this section are
7364 * the functions in the API for probe management (found below), and
7365 * dtrace_probe() itself (found above).
7369 * Register the calling provider with the DTrace framework. This should
7370 * generally be called by DTrace providers in their attach(9E) entry point.
7373 dtrace_register(const char *name
, const dtrace_pattr_t
*pap
, uint32_t priv
,
7374 cred_t
*cr
, const dtrace_pops_t
*pops
, void *arg
, dtrace_provider_id_t
*idp
)
7376 dtrace_provider_t
*provider
;
7378 if (name
== NULL
|| pap
== NULL
|| pops
== NULL
|| idp
== NULL
) {
7379 cmn_err(CE_WARN
, "failed to register provider '%s': invalid "
7380 "arguments", name
? name
: "<NULL>");
7384 if (name
[0] == '\0' || dtrace_badname(name
)) {
7385 cmn_err(CE_WARN
, "failed to register provider '%s': invalid "
7386 "provider name", name
);
7390 if ((pops
->dtps_provide
== NULL
&& pops
->dtps_provide_module
== NULL
) ||
7391 pops
->dtps_enable
== NULL
|| pops
->dtps_disable
== NULL
||
7392 pops
->dtps_destroy
== NULL
||
7393 ((pops
->dtps_resume
== NULL
) != (pops
->dtps_suspend
== NULL
))) {
7394 cmn_err(CE_WARN
, "failed to register provider '%s': invalid "
7395 "provider ops", name
);
7399 if (dtrace_badattr(&pap
->dtpa_provider
) ||
7400 dtrace_badattr(&pap
->dtpa_mod
) ||
7401 dtrace_badattr(&pap
->dtpa_func
) ||
7402 dtrace_badattr(&pap
->dtpa_name
) ||
7403 dtrace_badattr(&pap
->dtpa_args
)) {
7404 cmn_err(CE_WARN
, "failed to register provider '%s': invalid "
7405 "provider attributes", name
);
7409 if (priv
& ~DTRACE_PRIV_ALL
) {
7410 cmn_err(CE_WARN
, "failed to register provider '%s': invalid "
7411 "privilege attributes", name
);
7415 if ((priv
& DTRACE_PRIV_KERNEL
) &&
7416 (priv
& (DTRACE_PRIV_USER
| DTRACE_PRIV_OWNER
)) &&
7417 pops
->dtps_usermode
== NULL
) {
7418 cmn_err(CE_WARN
, "failed to register provider '%s': need "
7419 "dtps_usermode() op for given privilege attributes", name
);
7423 provider
= kmem_zalloc(sizeof (dtrace_provider_t
), KM_SLEEP
);
7425 /* APPLE NOTE: Darwin employs size bounded string operation. */
7427 size_t bufsize
= strlen(name
) + 1;
7428 provider
->dtpv_name
= kmem_alloc(bufsize
, KM_SLEEP
);
7429 (void) strlcpy(provider
->dtpv_name
, name
, bufsize
);
7432 provider
->dtpv_attr
= *pap
;
7433 provider
->dtpv_priv
.dtpp_flags
= priv
;
7435 provider
->dtpv_priv
.dtpp_uid
= crgetuid(cr
);
7436 provider
->dtpv_priv
.dtpp_zoneid
= crgetzoneid(cr
);
7438 provider
->dtpv_pops
= *pops
;
7440 if (pops
->dtps_provide
== NULL
) {
7441 ASSERT(pops
->dtps_provide_module
!= NULL
);
7442 provider
->dtpv_pops
.dtps_provide
=
7443 (void (*)(void *, const dtrace_probedesc_t
*))dtrace_nullop
;
7446 if (pops
->dtps_provide_module
== NULL
) {
7447 ASSERT(pops
->dtps_provide
!= NULL
);
7448 provider
->dtpv_pops
.dtps_provide_module
=
7449 (void (*)(void *, struct modctl
*))dtrace_nullop
;
7452 if (pops
->dtps_suspend
== NULL
) {
7453 ASSERT(pops
->dtps_resume
== NULL
);
7454 provider
->dtpv_pops
.dtps_suspend
=
7455 (void (*)(void *, dtrace_id_t
, void *))dtrace_nullop
;
7456 provider
->dtpv_pops
.dtps_resume
=
7457 (void (*)(void *, dtrace_id_t
, void *))dtrace_nullop
;
7460 provider
->dtpv_arg
= arg
;
7461 *idp
= (dtrace_provider_id_t
)provider
;
7463 if (pops
== &dtrace_provider_ops
) {
7464 lck_mtx_assert(&dtrace_provider_lock
, LCK_MTX_ASSERT_OWNED
);
7465 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
7466 ASSERT(dtrace_anon
.dta_enabling
== NULL
);
7469 * We make sure that the DTrace provider is at the head of
7470 * the provider chain.
7472 provider
->dtpv_next
= dtrace_provider
;
7473 dtrace_provider
= provider
;
7477 lck_mtx_lock(&dtrace_provider_lock
);
7478 lck_mtx_lock(&dtrace_lock
);
7481 * If there is at least one provider registered, we'll add this
7482 * provider after the first provider.
7484 if (dtrace_provider
!= NULL
) {
7485 provider
->dtpv_next
= dtrace_provider
->dtpv_next
;
7486 dtrace_provider
->dtpv_next
= provider
;
7488 dtrace_provider
= provider
;
7491 if (dtrace_retained
!= NULL
) {
7492 dtrace_enabling_provide(provider
);
7495 * Now we need to call dtrace_enabling_matchall() -- which
7496 * will acquire cpu_lock and dtrace_lock. We therefore need
7497 * to drop all of our locks before calling into it...
7499 lck_mtx_unlock(&dtrace_lock
);
7500 lck_mtx_unlock(&dtrace_provider_lock
);
7501 dtrace_enabling_matchall();
7506 lck_mtx_unlock(&dtrace_lock
);
7507 lck_mtx_unlock(&dtrace_provider_lock
);
7513 * Unregister the specified provider from the DTrace framework. This should
7514 * generally be called by DTrace providers in their detach(9E) entry point.
7517 dtrace_unregister(dtrace_provider_id_t id
)
7519 dtrace_provider_t
*old
= (dtrace_provider_t
*)id
;
7520 dtrace_provider_t
*prev
= NULL
;
7522 dtrace_probe_t
*probe
, *first
= NULL
;
7524 if (old
->dtpv_pops
.dtps_enable
==
7525 (int (*)(void *, dtrace_id_t
, void *))dtrace_enable_nullop
) {
7527 * If DTrace itself is the provider, we're called with locks
7530 ASSERT(old
== dtrace_provider
);
7531 ASSERT(dtrace_devi
!= NULL
);
7532 lck_mtx_assert(&dtrace_provider_lock
, LCK_MTX_ASSERT_OWNED
);
7533 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
7536 if (dtrace_provider
->dtpv_next
!= NULL
) {
7538 * There's another provider here; return failure.
7543 lck_mtx_lock(&dtrace_provider_lock
);
7544 lck_mtx_lock(&mod_lock
);
7545 lck_mtx_lock(&dtrace_lock
);
7549 * If anyone has /dev/dtrace open, or if there are anonymous enabled
7550 * probes, we refuse to let providers slither away, unless this
7551 * provider has already been explicitly invalidated.
7553 if (!old
->dtpv_defunct
&&
7554 (dtrace_opens
|| (dtrace_anon
.dta_state
!= NULL
&&
7555 dtrace_anon
.dta_state
->dts_necbs
> 0))) {
7557 lck_mtx_unlock(&dtrace_lock
);
7558 lck_mtx_unlock(&mod_lock
);
7559 lck_mtx_unlock(&dtrace_provider_lock
);
7565 * Attempt to destroy the probes associated with this provider.
7567 if (old
->dtpv_ecb_count
!=0) {
7569 * We have at least one ECB; we can't remove this provider.
7572 lck_mtx_unlock(&dtrace_lock
);
7573 lck_mtx_unlock(&mod_lock
);
7574 lck_mtx_unlock(&dtrace_provider_lock
);
7580 * All of the probes for this provider are disabled; we can safely
7581 * remove all of them from their hash chains and from the probe array.
7583 for (i
= 0; i
< dtrace_nprobes
&& old
->dtpv_probe_count
!=0; i
++) {
7584 if ((probe
= dtrace_probes
[i
]) == NULL
)
7587 if (probe
->dtpr_provider
!= old
)
7590 dtrace_probes
[i
] = NULL
;
7591 old
->dtpv_probe_count
--;
7593 dtrace_hash_remove(dtrace_bymod
, probe
);
7594 dtrace_hash_remove(dtrace_byfunc
, probe
);
7595 dtrace_hash_remove(dtrace_byname
, probe
);
7597 if (first
== NULL
) {
7599 probe
->dtpr_nextmod
= NULL
;
7601 probe
->dtpr_nextmod
= first
;
7607 * The provider's probes have been removed from the hash chains and
7608 * from the probe array. Now issue a dtrace_sync() to be sure that
7609 * everyone has cleared out from any probe array processing.
7613 for (probe
= first
; probe
!= NULL
; probe
= first
) {
7614 first
= probe
->dtpr_nextmod
;
7616 old
->dtpv_pops
.dtps_destroy(old
->dtpv_arg
, probe
->dtpr_id
,
7618 kmem_free(probe
->dtpr_mod
, strlen(probe
->dtpr_mod
) + 1);
7619 kmem_free(probe
->dtpr_func
, strlen(probe
->dtpr_func
) + 1);
7620 kmem_free(probe
->dtpr_name
, strlen(probe
->dtpr_name
) + 1);
7621 vmem_free(dtrace_arena
, (void *)(uintptr_t)(probe
->dtpr_id
), 1);
7622 zfree(dtrace_probe_t_zone
, probe
);
7625 if ((prev
= dtrace_provider
) == old
) {
7626 ASSERT(self
|| dtrace_devi
== NULL
);
7627 ASSERT(old
->dtpv_next
== NULL
|| dtrace_devi
== NULL
);
7628 dtrace_provider
= old
->dtpv_next
;
7630 while (prev
!= NULL
&& prev
->dtpv_next
!= old
)
7631 prev
= prev
->dtpv_next
;
7634 panic("attempt to unregister non-existent "
7635 "dtrace provider %p\n", (void *)id
);
7638 prev
->dtpv_next
= old
->dtpv_next
;
7642 lck_mtx_unlock(&dtrace_lock
);
7643 lck_mtx_unlock(&mod_lock
);
7644 lck_mtx_unlock(&dtrace_provider_lock
);
7647 kmem_free(old
->dtpv_name
, strlen(old
->dtpv_name
) + 1);
7648 kmem_free(old
, sizeof (dtrace_provider_t
));
7654 * Invalidate the specified provider. All subsequent probe lookups for the
7655 * specified provider will fail, but its probes will not be removed.
7658 dtrace_invalidate(dtrace_provider_id_t id
)
7660 dtrace_provider_t
*pvp
= (dtrace_provider_t
*)id
;
7662 ASSERT(pvp
->dtpv_pops
.dtps_enable
!=
7663 (int (*)(void *, dtrace_id_t
, void *))dtrace_enable_nullop
);
7665 lck_mtx_lock(&dtrace_provider_lock
);
7666 lck_mtx_lock(&dtrace_lock
);
7668 pvp
->dtpv_defunct
= 1;
7670 lck_mtx_unlock(&dtrace_lock
);
7671 lck_mtx_unlock(&dtrace_provider_lock
);
7675 * Indicate whether or not DTrace has attached.
7678 dtrace_attached(void)
7681 * dtrace_provider will be non-NULL iff the DTrace driver has
7682 * attached. (It's non-NULL because DTrace is always itself a
7685 return (dtrace_provider
!= NULL
);
7689 * Remove all the unenabled probes for the given provider. This function is
7690 * not unlike dtrace_unregister(), except that it doesn't remove the provider
7691 * -- just as many of its associated probes as it can.
7694 dtrace_condense(dtrace_provider_id_t id
)
7696 dtrace_provider_t
*prov
= (dtrace_provider_t
*)id
;
7698 dtrace_probe_t
*probe
;
7701 * Make sure this isn't the dtrace provider itself.
7703 ASSERT(prov
->dtpv_pops
.dtps_enable
!=
7704 (int (*)(void *, dtrace_id_t
, void *))dtrace_enable_nullop
);
7706 lck_mtx_lock(&dtrace_provider_lock
);
7707 lck_mtx_lock(&dtrace_lock
);
7710 * Attempt to destroy the probes associated with this provider.
7712 for (i
= 0; i
< dtrace_nprobes
; i
++) {
7713 if ((probe
= dtrace_probes
[i
]) == NULL
)
7716 if (probe
->dtpr_provider
!= prov
)
7719 if (probe
->dtpr_ecb
!= NULL
)
7722 dtrace_probes
[i
] = NULL
;
7723 prov
->dtpv_probe_count
--;
7725 dtrace_hash_remove(dtrace_bymod
, probe
);
7726 dtrace_hash_remove(dtrace_byfunc
, probe
);
7727 dtrace_hash_remove(dtrace_byname
, probe
);
7729 prov
->dtpv_pops
.dtps_destroy(prov
->dtpv_arg
, i
+ 1,
7731 kmem_free(probe
->dtpr_mod
, strlen(probe
->dtpr_mod
) + 1);
7732 kmem_free(probe
->dtpr_func
, strlen(probe
->dtpr_func
) + 1);
7733 kmem_free(probe
->dtpr_name
, strlen(probe
->dtpr_name
) + 1);
7734 zfree(dtrace_probe_t_zone
, probe
);
7735 vmem_free(dtrace_arena
, (void *)((uintptr_t)i
+ 1), 1);
7738 lck_mtx_unlock(&dtrace_lock
);
7739 lck_mtx_unlock(&dtrace_provider_lock
);
7745 * DTrace Probe Management Functions
7747 * The functions in this section perform the DTrace probe management,
7748 * including functions to create probes, look-up probes, and call into the
7749 * providers to request that probes be provided. Some of these functions are
7750 * in the Provider-to-Framework API; these functions can be identified by the
7751 * fact that they are not declared "static".
7755 * Create a probe with the specified module name, function name, and name.
7758 dtrace_probe_create(dtrace_provider_id_t prov
, const char *mod
,
7759 const char *func
, const char *name
, int aframes
, void *arg
)
7761 dtrace_probe_t
*probe
, **probes
;
7762 dtrace_provider_t
*provider
= (dtrace_provider_t
*)prov
;
7765 if (provider
== dtrace_provider
) {
7766 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
7768 lck_mtx_lock(&dtrace_lock
);
7771 id
= (dtrace_id_t
)(uintptr_t)vmem_alloc(dtrace_arena
, 1,
7772 VM_BESTFIT
| VM_SLEEP
);
7774 probe
= zalloc(dtrace_probe_t_zone
);
7775 bzero(probe
, sizeof (dtrace_probe_t
));
7777 probe
->dtpr_id
= id
;
7778 probe
->dtpr_gen
= dtrace_probegen
++;
7779 probe
->dtpr_mod
= dtrace_strdup(mod
);
7780 probe
->dtpr_func
= dtrace_strdup(func
);
7781 probe
->dtpr_name
= dtrace_strdup(name
);
7782 probe
->dtpr_arg
= arg
;
7783 probe
->dtpr_aframes
= aframes
;
7784 probe
->dtpr_provider
= provider
;
7786 dtrace_hash_add(dtrace_bymod
, probe
);
7787 dtrace_hash_add(dtrace_byfunc
, probe
);
7788 dtrace_hash_add(dtrace_byname
, probe
);
7790 if (id
- 1 >= (dtrace_id_t
)dtrace_nprobes
) {
7791 size_t osize
= dtrace_nprobes
* sizeof (dtrace_probe_t
*);
7792 size_t nsize
= osize
<< 1;
7796 ASSERT(dtrace_probes
== NULL
);
7797 nsize
= sizeof (dtrace_probe_t
*);
7800 probes
= kmem_zalloc(nsize
, KM_SLEEP
);
7802 if (dtrace_probes
== NULL
) {
7804 dtrace_probes
= probes
;
7807 dtrace_probe_t
**oprobes
= dtrace_probes
;
7809 bcopy(oprobes
, probes
, osize
);
7810 dtrace_membar_producer();
7811 dtrace_probes
= probes
;
7816 * All CPUs are now seeing the new probes array; we can
7817 * safely free the old array.
7819 kmem_free(oprobes
, osize
);
7820 dtrace_nprobes
<<= 1;
7823 ASSERT(id
- 1 < (dtrace_id_t
)dtrace_nprobes
);
7826 ASSERT(dtrace_probes
[id
- 1] == NULL
);
7827 dtrace_probes
[id
- 1] = probe
;
7828 provider
->dtpv_probe_count
++;
7830 if (provider
!= dtrace_provider
)
7831 lck_mtx_unlock(&dtrace_lock
);
7836 static dtrace_probe_t
*
7837 dtrace_probe_lookup_id(dtrace_id_t id
)
7839 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
7841 if (id
== 0 || id
> (dtrace_id_t
)dtrace_nprobes
)
7844 return (dtrace_probes
[id
- 1]);
7848 dtrace_probe_lookup_match(dtrace_probe_t
*probe
, void *arg
)
7850 *((dtrace_id_t
*)arg
) = probe
->dtpr_id
;
7852 return (DTRACE_MATCH_DONE
);
7856 * Look up a probe based on provider and one or more of module name, function
7857 * name and probe name.
7860 dtrace_probe_lookup(dtrace_provider_id_t prid
, const char *mod
,
7861 const char *func
, const char *name
)
7863 dtrace_probekey_t pkey
;
7867 pkey
.dtpk_prov
= ((dtrace_provider_t
*)prid
)->dtpv_name
;
7868 pkey
.dtpk_pmatch
= &dtrace_match_string
;
7869 pkey
.dtpk_mod
= mod
;
7870 pkey
.dtpk_mmatch
= mod
? &dtrace_match_string
: &dtrace_match_nul
;
7871 pkey
.dtpk_func
= func
;
7872 pkey
.dtpk_fmatch
= func
? &dtrace_match_string
: &dtrace_match_nul
;
7873 pkey
.dtpk_name
= name
;
7874 pkey
.dtpk_nmatch
= name
? &dtrace_match_string
: &dtrace_match_nul
;
7875 pkey
.dtpk_id
= DTRACE_IDNONE
;
7877 lck_mtx_lock(&dtrace_lock
);
7878 match
= dtrace_match(&pkey
, DTRACE_PRIV_ALL
, 0, 0,
7879 dtrace_probe_lookup_match
, &id
);
7880 lck_mtx_unlock(&dtrace_lock
);
7882 ASSERT(match
== 1 || match
== 0);
7883 return (match
? id
: 0);
7887 * Returns the probe argument associated with the specified probe.
7890 dtrace_probe_arg(dtrace_provider_id_t id
, dtrace_id_t pid
)
7892 dtrace_probe_t
*probe
;
7895 lck_mtx_lock(&dtrace_lock
);
7897 if ((probe
= dtrace_probe_lookup_id(pid
)) != NULL
&&
7898 probe
->dtpr_provider
== (dtrace_provider_t
*)id
)
7899 rval
= probe
->dtpr_arg
;
7901 lck_mtx_unlock(&dtrace_lock
);
7907 * Copy a probe into a probe description.
7910 dtrace_probe_description(const dtrace_probe_t
*prp
, dtrace_probedesc_t
*pdp
)
7912 bzero(pdp
, sizeof (dtrace_probedesc_t
));
7913 pdp
->dtpd_id
= prp
->dtpr_id
;
7915 /* APPLE NOTE: Darwin employs size bounded string operation. */
7916 (void) strlcpy(pdp
->dtpd_provider
,
7917 prp
->dtpr_provider
->dtpv_name
, DTRACE_PROVNAMELEN
);
7919 (void) strlcpy(pdp
->dtpd_mod
, prp
->dtpr_mod
, DTRACE_MODNAMELEN
);
7920 (void) strlcpy(pdp
->dtpd_func
, prp
->dtpr_func
, DTRACE_FUNCNAMELEN
);
7921 (void) strlcpy(pdp
->dtpd_name
, prp
->dtpr_name
, DTRACE_NAMELEN
);
7925 * Called to indicate that a probe -- or probes -- should be provided by a
7926 * specfied provider. If the specified description is NULL, the provider will
7927 * be told to provide all of its probes. (This is done whenever a new
7928 * consumer comes along, or whenever a retained enabling is to be matched.) If
7929 * the specified description is non-NULL, the provider is given the
7930 * opportunity to dynamically provide the specified probe, allowing providers
7931 * to support the creation of probes on-the-fly. (So-called _autocreated_
7932 * probes.) If the provider is NULL, the operations will be applied to all
7933 * providers; if the provider is non-NULL the operations will only be applied
7934 * to the specified provider. The dtrace_provider_lock must be held, and the
7935 * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation
7936 * will need to grab the dtrace_lock when it reenters the framework through
7937 * dtrace_probe_lookup(), dtrace_probe_create(), etc.
7940 dtrace_probe_provide(dtrace_probedesc_t
*desc
, dtrace_provider_t
*prv
)
7945 lck_mtx_assert(&dtrace_provider_lock
, LCK_MTX_ASSERT_OWNED
);
7949 prv
= dtrace_provider
;
7954 * First, call the blanket provide operation.
7956 prv
->dtpv_pops
.dtps_provide(prv
->dtpv_arg
, desc
);
7959 * Now call the per-module provide operation. We will grab
7960 * mod_lock to prevent the list from being modified. Note
7961 * that this also prevents the mod_busy bits from changing.
7962 * (mod_busy can only be changed with mod_lock held.)
7964 lck_mtx_lock(&mod_lock
);
7966 ctl
= dtrace_modctl_list
;
7968 prv
->dtpv_pops
.dtps_provide_module(prv
->dtpv_arg
, ctl
);
7969 ctl
= ctl
->mod_next
;
7972 lck_mtx_unlock(&mod_lock
);
7973 } while (all
&& (prv
= prv
->dtpv_next
) != NULL
);
7977 * Iterate over each probe, and call the Framework-to-Provider API function
7981 dtrace_probe_foreach(uintptr_t offs
)
7983 dtrace_provider_t
*prov
;
7984 void (*func
)(void *, dtrace_id_t
, void *);
7985 dtrace_probe_t
*probe
;
7986 dtrace_icookie_t cookie
;
7990 * We disable interrupts to walk through the probe array. This is
7991 * safe -- the dtrace_sync() in dtrace_unregister() assures that we
7992 * won't see stale data.
7994 cookie
= dtrace_interrupt_disable();
7996 for (i
= 0; i
< dtrace_nprobes
; i
++) {
7997 if ((probe
= dtrace_probes
[i
]) == NULL
)
8000 if (probe
->dtpr_ecb
== NULL
) {
8002 * This probe isn't enabled -- don't call the function.
8007 prov
= probe
->dtpr_provider
;
8008 func
= *((void(**)(void *, dtrace_id_t
, void *))
8009 ((uintptr_t)&prov
->dtpv_pops
+ offs
));
8011 func(prov
->dtpv_arg
, i
+ 1, probe
->dtpr_arg
);
8014 dtrace_interrupt_enable(cookie
);
8018 dtrace_probe_enable(const dtrace_probedesc_t
*desc
, dtrace_enabling_t
*enab
)
8020 dtrace_probekey_t pkey
;
8025 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
8027 dtrace_ecb_create_cache
= NULL
;
8031 * If we're passed a NULL description, we're being asked to
8032 * create an ECB with a NULL probe.
8034 (void) dtrace_ecb_create_enable(NULL
, enab
);
8038 dtrace_probekey(desc
, &pkey
);
8039 dtrace_cred2priv(enab
->dten_vstate
->dtvs_state
->dts_cred
.dcr_cred
,
8040 &priv
, &uid
, &zoneid
);
8042 return (dtrace_match(&pkey
, priv
, uid
, zoneid
, dtrace_ecb_create_enable
,
8047 * DTrace Helper Provider Functions
8050 dtrace_dofattr2attr(dtrace_attribute_t
*attr
, const dof_attr_t dofattr
)
8052 attr
->dtat_name
= DOF_ATTR_NAME(dofattr
);
8053 attr
->dtat_data
= DOF_ATTR_DATA(dofattr
);
8054 attr
->dtat_class
= DOF_ATTR_CLASS(dofattr
);
8058 dtrace_dofprov2hprov(dtrace_helper_provdesc_t
*hprov
,
8059 const dof_provider_t
*dofprov
, char *strtab
)
8061 hprov
->dthpv_provname
= strtab
+ dofprov
->dofpv_name
;
8062 dtrace_dofattr2attr(&hprov
->dthpv_pattr
.dtpa_provider
,
8063 dofprov
->dofpv_provattr
);
8064 dtrace_dofattr2attr(&hprov
->dthpv_pattr
.dtpa_mod
,
8065 dofprov
->dofpv_modattr
);
8066 dtrace_dofattr2attr(&hprov
->dthpv_pattr
.dtpa_func
,
8067 dofprov
->dofpv_funcattr
);
8068 dtrace_dofattr2attr(&hprov
->dthpv_pattr
.dtpa_name
,
8069 dofprov
->dofpv_nameattr
);
8070 dtrace_dofattr2attr(&hprov
->dthpv_pattr
.dtpa_args
,
8071 dofprov
->dofpv_argsattr
);
8075 dtrace_helper_provide_one(dof_helper_t
*dhp
, dof_sec_t
*sec
, pid_t pid
)
8077 uintptr_t daddr
= (uintptr_t)dhp
->dofhp_dof
;
8078 dof_hdr_t
*dof
= (dof_hdr_t
*)daddr
;
8079 dof_sec_t
*str_sec
, *prb_sec
, *arg_sec
, *off_sec
, *enoff_sec
;
8080 dof_provider_t
*provider
;
8082 uint32_t *off
, *enoff
;
8086 dtrace_helper_provdesc_t dhpv
;
8087 dtrace_helper_probedesc_t dhpb
;
8088 dtrace_meta_t
*meta
= dtrace_meta_pid
;
8089 dtrace_mops_t
*mops
= &meta
->dtm_mops
;
8092 provider
= (dof_provider_t
*)(uintptr_t)(daddr
+ sec
->dofs_offset
);
8093 str_sec
= (dof_sec_t
*)(uintptr_t)(daddr
+ dof
->dofh_secoff
+
8094 provider
->dofpv_strtab
* dof
->dofh_secsize
);
8095 prb_sec
= (dof_sec_t
*)(uintptr_t)(daddr
+ dof
->dofh_secoff
+
8096 provider
->dofpv_probes
* dof
->dofh_secsize
);
8097 arg_sec
= (dof_sec_t
*)(uintptr_t)(daddr
+ dof
->dofh_secoff
+
8098 provider
->dofpv_prargs
* dof
->dofh_secsize
);
8099 off_sec
= (dof_sec_t
*)(uintptr_t)(daddr
+ dof
->dofh_secoff
+
8100 provider
->dofpv_proffs
* dof
->dofh_secsize
);
8102 strtab
= (char *)(uintptr_t)(daddr
+ str_sec
->dofs_offset
);
8103 off
= (uint32_t *)(uintptr_t)(daddr
+ off_sec
->dofs_offset
);
8104 arg
= (uint8_t *)(uintptr_t)(daddr
+ arg_sec
->dofs_offset
);
8108 * See dtrace_helper_provider_validate().
8110 if (dof
->dofh_ident
[DOF_ID_VERSION
] != DOF_VERSION_1
&&
8111 provider
->dofpv_prenoffs
!= DOF_SECT_NONE
) {
8112 enoff_sec
= (dof_sec_t
*)(uintptr_t)(daddr
+ dof
->dofh_secoff
+
8113 provider
->dofpv_prenoffs
* dof
->dofh_secsize
);
8114 enoff
= (uint32_t *)(uintptr_t)(daddr
+ enoff_sec
->dofs_offset
);
8117 nprobes
= prb_sec
->dofs_size
/ prb_sec
->dofs_entsize
;
8120 * Create the provider.
8122 dtrace_dofprov2hprov(&dhpv
, provider
, strtab
);
8124 if ((parg
= mops
->dtms_provide_pid(meta
->dtm_arg
, &dhpv
, pid
)) == NULL
)
8130 * Create the probes.
8132 for (i
= 0; i
< nprobes
; i
++) {
8133 probe
= (dof_probe_t
*)(uintptr_t)(daddr
+
8134 prb_sec
->dofs_offset
+ i
* prb_sec
->dofs_entsize
);
8136 dhpb
.dthpb_mod
= dhp
->dofhp_mod
;
8137 dhpb
.dthpb_func
= strtab
+ probe
->dofpr_func
;
8138 dhpb
.dthpb_name
= strtab
+ probe
->dofpr_name
;
8139 #if !defined(__APPLE__)
8140 dhpb
.dthpb_base
= probe
->dofpr_addr
;
8142 dhpb
.dthpb_base
= dhp
->dofhp_addr
; /* FIXME: James, why? */
8144 dhpb
.dthpb_offs
= (int32_t *)(off
+ probe
->dofpr_offidx
);
8145 dhpb
.dthpb_noffs
= probe
->dofpr_noffs
;
8146 if (enoff
!= NULL
) {
8147 dhpb
.dthpb_enoffs
= (int32_t *)(enoff
+ probe
->dofpr_enoffidx
);
8148 dhpb
.dthpb_nenoffs
= probe
->dofpr_nenoffs
;
8150 dhpb
.dthpb_enoffs
= NULL
;
8151 dhpb
.dthpb_nenoffs
= 0;
8153 dhpb
.dthpb_args
= arg
+ probe
->dofpr_argidx
;
8154 dhpb
.dthpb_nargc
= probe
->dofpr_nargc
;
8155 dhpb
.dthpb_xargc
= probe
->dofpr_xargc
;
8156 dhpb
.dthpb_ntypes
= strtab
+ probe
->dofpr_nargv
;
8157 dhpb
.dthpb_xtypes
= strtab
+ probe
->dofpr_xargv
;
8159 mops
->dtms_create_probe(meta
->dtm_arg
, parg
, &dhpb
);
8164 dtrace_helper_provide(dof_helper_t
*dhp
, pid_t pid
)
8166 uintptr_t daddr
= (uintptr_t)dhp
->dofhp_dof
;
8167 dof_hdr_t
*dof
= (dof_hdr_t
*)daddr
;
8170 lck_mtx_assert(&dtrace_meta_lock
, LCK_MTX_ASSERT_OWNED
);
8172 for (i
= 0; i
< dof
->dofh_secnum
; i
++) {
8173 dof_sec_t
*sec
= (dof_sec_t
*)(uintptr_t)(daddr
+
8174 dof
->dofh_secoff
+ i
* dof
->dofh_secsize
);
8176 if (sec
->dofs_type
!= DOF_SECT_PROVIDER
)
8179 dtrace_helper_provide_one(dhp
, sec
, pid
);
8183 * We may have just created probes, so we must now rematch against
8184 * any retained enablings. Note that this call will acquire both
8185 * cpu_lock and dtrace_lock; the fact that we are holding
8186 * dtrace_meta_lock now is what defines the ordering with respect to
8187 * these three locks.
8189 dtrace_enabling_matchall();
8193 dtrace_helper_provider_remove_one(dof_helper_t
*dhp
, dof_sec_t
*sec
, pid_t pid
)
8195 uintptr_t daddr
= (uintptr_t)dhp
->dofhp_dof
;
8196 dof_hdr_t
*dof
= (dof_hdr_t
*)daddr
;
8198 dof_provider_t
*provider
;
8200 dtrace_helper_provdesc_t dhpv
;
8201 dtrace_meta_t
*meta
= dtrace_meta_pid
;
8202 dtrace_mops_t
*mops
= &meta
->dtm_mops
;
8204 provider
= (dof_provider_t
*)(uintptr_t)(daddr
+ sec
->dofs_offset
);
8205 str_sec
= (dof_sec_t
*)(uintptr_t)(daddr
+ dof
->dofh_secoff
+
8206 provider
->dofpv_strtab
* dof
->dofh_secsize
);
8208 strtab
= (char *)(uintptr_t)(daddr
+ str_sec
->dofs_offset
);
8211 * Create the provider.
8213 dtrace_dofprov2hprov(&dhpv
, provider
, strtab
);
8215 mops
->dtms_remove_pid(meta
->dtm_arg
, &dhpv
, pid
);
8221 dtrace_helper_provider_remove(dof_helper_t
*dhp
, pid_t pid
)
8223 uintptr_t daddr
= (uintptr_t)dhp
->dofhp_dof
;
8224 dof_hdr_t
*dof
= (dof_hdr_t
*)daddr
;
8227 lck_mtx_assert(&dtrace_meta_lock
, LCK_MTX_ASSERT_OWNED
);
8229 for (i
= 0; i
< dof
->dofh_secnum
; i
++) {
8230 dof_sec_t
*sec
= (dof_sec_t
*)(uintptr_t)(daddr
+
8231 dof
->dofh_secoff
+ i
* dof
->dofh_secsize
);
8233 if (sec
->dofs_type
!= DOF_SECT_PROVIDER
)
8236 dtrace_helper_provider_remove_one(dhp
, sec
, pid
);
8241 * DTrace Meta Provider-to-Framework API Functions
8243 * These functions implement the Meta Provider-to-Framework API, as described
8244 * in <sys/dtrace.h>.
8247 dtrace_meta_register(const char *name
, const dtrace_mops_t
*mops
, void *arg
,
8248 dtrace_meta_provider_id_t
*idp
)
8250 dtrace_meta_t
*meta
;
8251 dtrace_helpers_t
*help
, *next
;
8254 *idp
= DTRACE_METAPROVNONE
;
8257 * We strictly don't need the name, but we hold onto it for
8258 * debuggability. All hail error queues!
8261 cmn_err(CE_WARN
, "failed to register meta-provider: "
8267 mops
->dtms_create_probe
== NULL
||
8268 mops
->dtms_provide_pid
== NULL
||
8269 mops
->dtms_remove_pid
== NULL
) {
8270 cmn_err(CE_WARN
, "failed to register meta-register %s: "
8271 "invalid ops", name
);
8275 meta
= kmem_zalloc(sizeof (dtrace_meta_t
), KM_SLEEP
);
8276 meta
->dtm_mops
= *mops
;
8278 /* APPLE NOTE: Darwin employs size bounded string operation. */
8280 size_t bufsize
= strlen(name
) + 1;
8281 meta
->dtm_name
= kmem_alloc(bufsize
, KM_SLEEP
);
8282 (void) strlcpy(meta
->dtm_name
, name
, bufsize
);
8285 meta
->dtm_arg
= arg
;
8287 lck_mtx_lock(&dtrace_meta_lock
);
8288 lck_mtx_lock(&dtrace_lock
);
8290 if (dtrace_meta_pid
!= NULL
) {
8291 lck_mtx_unlock(&dtrace_lock
);
8292 lck_mtx_unlock(&dtrace_meta_lock
);
8293 cmn_err(CE_WARN
, "failed to register meta-register %s: "
8294 "user-land meta-provider exists", name
);
8295 kmem_free(meta
->dtm_name
, strlen(meta
->dtm_name
) + 1);
8296 kmem_free(meta
, sizeof (dtrace_meta_t
));
8300 dtrace_meta_pid
= meta
;
8301 *idp
= (dtrace_meta_provider_id_t
)meta
;
8304 * If there are providers and probes ready to go, pass them
8305 * off to the new meta provider now.
8308 help
= dtrace_deferred_pid
;
8309 dtrace_deferred_pid
= NULL
;
8311 lck_mtx_unlock(&dtrace_lock
);
8313 while (help
!= NULL
) {
8314 for (i
= 0; i
< help
->dthps_nprovs
; i
++) {
8315 dtrace_helper_provide(&help
->dthps_provs
[i
]->dthp_prov
,
8319 next
= help
->dthps_next
;
8320 help
->dthps_next
= NULL
;
8321 help
->dthps_prev
= NULL
;
8322 help
->dthps_deferred
= 0;
8326 lck_mtx_unlock(&dtrace_meta_lock
);
8332 dtrace_meta_unregister(dtrace_meta_provider_id_t id
)
8334 dtrace_meta_t
**pp
, *old
= (dtrace_meta_t
*)id
;
8336 lck_mtx_lock(&dtrace_meta_lock
);
8337 lck_mtx_lock(&dtrace_lock
);
8339 if (old
== dtrace_meta_pid
) {
8340 pp
= &dtrace_meta_pid
;
8342 panic("attempt to unregister non-existent "
8343 "dtrace meta-provider %p\n", (void *)old
);
8346 if (old
->dtm_count
!= 0) {
8347 lck_mtx_unlock(&dtrace_lock
);
8348 lck_mtx_unlock(&dtrace_meta_lock
);
8354 lck_mtx_unlock(&dtrace_lock
);
8355 lck_mtx_unlock(&dtrace_meta_lock
);
8357 kmem_free(old
->dtm_name
, strlen(old
->dtm_name
) + 1);
8358 kmem_free(old
, sizeof (dtrace_meta_t
));
8365 * DTrace DIF Object Functions
8368 dtrace_difo_err(uint_t pc
, const char *format
, ...)
8370 if (dtrace_err_verbose
) {
8373 (void) uprintf("dtrace DIF object error: [%u]: ", pc
);
8374 va_start(alist
, format
);
8375 (void) vuprintf(format
, alist
);
8379 #ifdef DTRACE_ERRDEBUG
8380 dtrace_errdebug(format
);
8386 * Validate a DTrace DIF object by checking the IR instructions. The following
8387 * rules are currently enforced by dtrace_difo_validate():
8389 * 1. Each instruction must have a valid opcode
8390 * 2. Each register, string, variable, or subroutine reference must be valid
8391 * 3. No instruction can modify register %r0 (must be zero)
8392 * 4. All instruction reserved bits must be set to zero
8393 * 5. The last instruction must be a "ret" instruction
8394 * 6. All branch targets must reference a valid instruction _after_ the branch
8397 dtrace_difo_validate(dtrace_difo_t
*dp
, dtrace_vstate_t
*vstate
, uint_t nregs
,
8403 int (*efunc
)(uint_t pc
, const char *, ...) = dtrace_difo_err
;
8407 kcheckload
= cr
== NULL
||
8408 (vstate
->dtvs_state
->dts_cred
.dcr_visible
& DTRACE_CRV_KERNEL
) == 0;
8410 dp
->dtdo_destructive
= 0;
8412 for (pc
= 0; pc
< dp
->dtdo_len
&& err
== 0; pc
++) {
8413 dif_instr_t instr
= dp
->dtdo_buf
[pc
];
8415 uint_t r1
= DIF_INSTR_R1(instr
);
8416 uint_t r2
= DIF_INSTR_R2(instr
);
8417 uint_t rd
= DIF_INSTR_RD(instr
);
8418 uint_t rs
= DIF_INSTR_RS(instr
);
8419 uint_t label
= DIF_INSTR_LABEL(instr
);
8420 uint_t v
= DIF_INSTR_VAR(instr
);
8421 uint_t subr
= DIF_INSTR_SUBR(instr
);
8422 uint_t type
= DIF_INSTR_TYPE(instr
);
8423 uint_t op
= DIF_INSTR_OP(instr
);
8441 err
+= efunc(pc
, "invalid register %u\n", r1
);
8443 err
+= efunc(pc
, "invalid register %u\n", r2
);
8445 err
+= efunc(pc
, "invalid register %u\n", rd
);
8447 err
+= efunc(pc
, "cannot write to %r0\n");
8453 err
+= efunc(pc
, "invalid register %u\n", r1
);
8455 err
+= efunc(pc
, "non-zero reserved bits\n");
8457 err
+= efunc(pc
, "invalid register %u\n", rd
);
8459 err
+= efunc(pc
, "cannot write to %r0\n");
8469 err
+= efunc(pc
, "invalid register %u\n", r1
);
8471 err
+= efunc(pc
, "non-zero reserved bits\n");
8473 err
+= efunc(pc
, "invalid register %u\n", rd
);
8475 err
+= efunc(pc
, "cannot write to %r0\n");
8477 dp
->dtdo_buf
[pc
] = DIF_INSTR_LOAD(op
+
8478 DIF_OP_RLDSB
- DIF_OP_LDSB
, r1
, rd
);
8488 err
+= efunc(pc
, "invalid register %u\n", r1
);
8490 err
+= efunc(pc
, "non-zero reserved bits\n");
8492 err
+= efunc(pc
, "invalid register %u\n", rd
);
8494 err
+= efunc(pc
, "cannot write to %r0\n");
8504 err
+= efunc(pc
, "invalid register %u\n", r1
);
8506 err
+= efunc(pc
, "non-zero reserved bits\n");
8508 err
+= efunc(pc
, "invalid register %u\n", rd
);
8510 err
+= efunc(pc
, "cannot write to %r0\n");
8517 err
+= efunc(pc
, "invalid register %u\n", r1
);
8519 err
+= efunc(pc
, "non-zero reserved bits\n");
8521 err
+= efunc(pc
, "invalid register %u\n", rd
);
8523 err
+= efunc(pc
, "cannot write to 0 address\n");
8528 err
+= efunc(pc
, "invalid register %u\n", r1
);
8530 err
+= efunc(pc
, "invalid register %u\n", r2
);
8532 err
+= efunc(pc
, "non-zero reserved bits\n");
8536 err
+= efunc(pc
, "invalid register %u\n", r1
);
8537 if (r2
!= 0 || rd
!= 0)
8538 err
+= efunc(pc
, "non-zero reserved bits\n");
8551 if (label
>= dp
->dtdo_len
) {
8552 err
+= efunc(pc
, "invalid branch target %u\n",
8556 err
+= efunc(pc
, "backward branch to %u\n",
8561 if (r1
!= 0 || r2
!= 0)
8562 err
+= efunc(pc
, "non-zero reserved bits\n");
8564 err
+= efunc(pc
, "invalid register %u\n", rd
);
8568 case DIF_OP_FLUSHTS
:
8569 if (r1
!= 0 || r2
!= 0 || rd
!= 0)
8570 err
+= efunc(pc
, "non-zero reserved bits\n");
8573 if (DIF_INSTR_INTEGER(instr
) >= dp
->dtdo_intlen
) {
8574 err
+= efunc(pc
, "invalid integer ref %u\n",
8575 DIF_INSTR_INTEGER(instr
));
8578 err
+= efunc(pc
, "invalid register %u\n", rd
);
8580 err
+= efunc(pc
, "cannot write to %r0\n");
8583 if (DIF_INSTR_STRING(instr
) >= dp
->dtdo_strlen
) {
8584 err
+= efunc(pc
, "invalid string ref %u\n",
8585 DIF_INSTR_STRING(instr
));
8588 err
+= efunc(pc
, "invalid register %u\n", rd
);
8590 err
+= efunc(pc
, "cannot write to %r0\n");
8594 if (r1
> DIF_VAR_ARRAY_MAX
)
8595 err
+= efunc(pc
, "invalid array %u\n", r1
);
8597 err
+= efunc(pc
, "invalid register %u\n", r2
);
8599 err
+= efunc(pc
, "invalid register %u\n", rd
);
8601 err
+= efunc(pc
, "cannot write to %r0\n");
8608 if (v
< DIF_VAR_OTHER_MIN
|| v
> DIF_VAR_OTHER_MAX
)
8609 err
+= efunc(pc
, "invalid variable %u\n", v
);
8611 err
+= efunc(pc
, "invalid register %u\n", rd
);
8613 err
+= efunc(pc
, "cannot write to %r0\n");
8620 if (v
< DIF_VAR_OTHER_UBASE
|| v
> DIF_VAR_OTHER_MAX
)
8621 err
+= efunc(pc
, "invalid variable %u\n", v
);
8623 err
+= efunc(pc
, "invalid register %u\n", rd
);
8626 if (subr
> DIF_SUBR_MAX
)
8627 err
+= efunc(pc
, "invalid subr %u\n", subr
);
8629 err
+= efunc(pc
, "invalid register %u\n", rd
);
8631 err
+= efunc(pc
, "cannot write to %r0\n");
8633 if (subr
== DIF_SUBR_COPYOUT
||
8634 subr
== DIF_SUBR_COPYOUTSTR
) {
8635 dp
->dtdo_destructive
= 1;
8639 if (type
!= DIF_TYPE_STRING
&& type
!= DIF_TYPE_CTF
)
8640 err
+= efunc(pc
, "invalid ref type %u\n", type
);
8642 err
+= efunc(pc
, "invalid register %u\n", r2
);
8644 err
+= efunc(pc
, "invalid register %u\n", rs
);
8647 if (type
!= DIF_TYPE_CTF
)
8648 err
+= efunc(pc
, "invalid val type %u\n", type
);
8650 err
+= efunc(pc
, "invalid register %u\n", r2
);
8652 err
+= efunc(pc
, "invalid register %u\n", rs
);
8655 err
+= efunc(pc
, "invalid opcode %u\n",
8656 DIF_INSTR_OP(instr
));
8660 if (dp
->dtdo_len
!= 0 &&
8661 DIF_INSTR_OP(dp
->dtdo_buf
[dp
->dtdo_len
- 1]) != DIF_OP_RET
) {
8662 err
+= efunc(dp
->dtdo_len
- 1,
8663 "expected 'ret' as last DIF instruction\n");
8666 if (!(dp
->dtdo_rtype
.dtdt_flags
& DIF_TF_BYREF
)) {
8668 * If we're not returning by reference, the size must be either
8669 * 0 or the size of one of the base types.
8671 switch (dp
->dtdo_rtype
.dtdt_size
) {
8673 case sizeof (uint8_t):
8674 case sizeof (uint16_t):
8675 case sizeof (uint32_t):
8676 case sizeof (uint64_t):
8680 err
+= efunc(dp
->dtdo_len
- 1, "bad return size\n");
8684 for (i
= 0; i
< dp
->dtdo_varlen
&& err
== 0; i
++) {
8685 dtrace_difv_t
*v
= &dp
->dtdo_vartab
[i
], *existing
= NULL
;
8686 dtrace_diftype_t
*vt
, *et
;
8690 if (v
->dtdv_scope
!= DIFV_SCOPE_GLOBAL
&&
8691 v
->dtdv_scope
!= DIFV_SCOPE_THREAD
&&
8692 v
->dtdv_scope
!= DIFV_SCOPE_LOCAL
) {
8693 err
+= efunc(i
, "unrecognized variable scope %d\n",
8698 if (v
->dtdv_kind
!= DIFV_KIND_ARRAY
&&
8699 v
->dtdv_kind
!= DIFV_KIND_SCALAR
) {
8700 err
+= efunc(i
, "unrecognized variable type %d\n",
8705 if ((id
= v
->dtdv_id
) > DIF_VARIABLE_MAX
) {
8706 err
+= efunc(i
, "%d exceeds variable id limit\n", id
);
8710 if (id
< DIF_VAR_OTHER_UBASE
)
8714 * For user-defined variables, we need to check that this
8715 * definition is identical to any previous definition that we
8718 ndx
= id
- DIF_VAR_OTHER_UBASE
;
8720 switch (v
->dtdv_scope
) {
8721 case DIFV_SCOPE_GLOBAL
:
8722 if (ndx
< vstate
->dtvs_nglobals
) {
8723 dtrace_statvar_t
*svar
;
8725 if ((svar
= vstate
->dtvs_globals
[ndx
]) != NULL
)
8726 existing
= &svar
->dtsv_var
;
8731 case DIFV_SCOPE_THREAD
:
8732 if (ndx
< vstate
->dtvs_ntlocals
)
8733 existing
= &vstate
->dtvs_tlocals
[ndx
];
8736 case DIFV_SCOPE_LOCAL
:
8737 if (ndx
< vstate
->dtvs_nlocals
) {
8738 dtrace_statvar_t
*svar
;
8740 if ((svar
= vstate
->dtvs_locals
[ndx
]) != NULL
)
8741 existing
= &svar
->dtsv_var
;
8749 if (vt
->dtdt_flags
& DIF_TF_BYREF
) {
8750 if (vt
->dtdt_size
== 0) {
8751 err
+= efunc(i
, "zero-sized variable\n");
8755 if (v
->dtdv_scope
== DIFV_SCOPE_GLOBAL
&&
8756 vt
->dtdt_size
> dtrace_global_maxsize
) {
8757 err
+= efunc(i
, "oversized by-ref global\n");
8762 if (existing
== NULL
|| existing
->dtdv_id
== 0)
8765 ASSERT(existing
->dtdv_id
== v
->dtdv_id
);
8766 ASSERT(existing
->dtdv_scope
== v
->dtdv_scope
);
8768 if (existing
->dtdv_kind
!= v
->dtdv_kind
)
8769 err
+= efunc(i
, "%d changed variable kind\n", id
);
8771 et
= &existing
->dtdv_type
;
8773 if (vt
->dtdt_flags
!= et
->dtdt_flags
) {
8774 err
+= efunc(i
, "%d changed variable type flags\n", id
);
8778 if (vt
->dtdt_size
!= 0 && vt
->dtdt_size
!= et
->dtdt_size
) {
8779 err
+= efunc(i
, "%d changed variable type size\n", id
);
8788 * Validate a DTrace DIF object that it is to be used as a helper. Helpers
8789 * are much more constrained than normal DIFOs. Specifically, they may
8792 * 1. Make calls to subroutines other than copyin(), copyinstr() or
8793 * miscellaneous string routines
8794 * 2. Access DTrace variables other than the args[] array, and the
8795 * curthread, pid, ppid, tid, execname, zonename, uid and gid variables.
8796 * 3. Have thread-local variables.
8797 * 4. Have dynamic variables.
8800 dtrace_difo_validate_helper(dtrace_difo_t
*dp
)
8802 int (*efunc
)(uint_t pc
, const char *, ...) = dtrace_difo_err
;
8806 for (pc
= 0; pc
< dp
->dtdo_len
; pc
++) {
8807 dif_instr_t instr
= dp
->dtdo_buf
[pc
];
8809 uint_t v
= DIF_INSTR_VAR(instr
);
8810 uint_t subr
= DIF_INSTR_SUBR(instr
);
8811 uint_t op
= DIF_INSTR_OP(instr
);
8866 case DIF_OP_FLUSHTS
:
8878 if (v
>= DIF_VAR_OTHER_UBASE
)
8881 if (v
>= DIF_VAR_ARG0
&& v
<= DIF_VAR_ARG9
)
8884 if (v
== DIF_VAR_CURTHREAD
|| v
== DIF_VAR_PID
||
8885 v
== DIF_VAR_PPID
|| v
== DIF_VAR_TID
||
8886 v
== DIF_VAR_EXECNAME
|| v
== DIF_VAR_ZONENAME
||
8887 v
== DIF_VAR_UID
|| v
== DIF_VAR_GID
)
8890 err
+= efunc(pc
, "illegal variable %u\n", v
);
8897 err
+= efunc(pc
, "illegal dynamic variable load\n");
8903 err
+= efunc(pc
, "illegal dynamic variable store\n");
8907 if (subr
== DIF_SUBR_ALLOCA
||
8908 subr
== DIF_SUBR_BCOPY
||
8909 subr
== DIF_SUBR_COPYIN
||
8910 subr
== DIF_SUBR_COPYINTO
||
8911 subr
== DIF_SUBR_COPYINSTR
||
8912 subr
== DIF_SUBR_INDEX
||
8913 subr
== DIF_SUBR_INET_NTOA
||
8914 subr
== DIF_SUBR_INET_NTOA6
||
8915 subr
== DIF_SUBR_INET_NTOP
||
8916 subr
== DIF_SUBR_LLTOSTR
||
8917 subr
== DIF_SUBR_RINDEX
||
8918 subr
== DIF_SUBR_STRCHR
||
8919 subr
== DIF_SUBR_STRJOIN
||
8920 subr
== DIF_SUBR_STRRCHR
||
8921 subr
== DIF_SUBR_STRSTR
||
8922 subr
== DIF_SUBR_COREPROFILE
||
8923 subr
== DIF_SUBR_HTONS
||
8924 subr
== DIF_SUBR_HTONL
||
8925 subr
== DIF_SUBR_HTONLL
||
8926 subr
== DIF_SUBR_NTOHS
||
8927 subr
== DIF_SUBR_NTOHL
||
8928 subr
== DIF_SUBR_NTOHLL
)
8931 err
+= efunc(pc
, "invalid subr %u\n", subr
);
8935 err
+= efunc(pc
, "invalid opcode %u\n",
8936 DIF_INSTR_OP(instr
));
8944 * Returns 1 if the expression in the DIF object can be cached on a per-thread
8948 dtrace_difo_cacheable(dtrace_difo_t
*dp
)
8955 for (i
= 0; i
< dp
->dtdo_varlen
; i
++) {
8956 dtrace_difv_t
*v
= &dp
->dtdo_vartab
[i
];
8958 if (v
->dtdv_scope
!= DIFV_SCOPE_GLOBAL
)
8961 switch (v
->dtdv_id
) {
8962 case DIF_VAR_CURTHREAD
:
8965 case DIF_VAR_EXECNAME
:
8966 case DIF_VAR_ZONENAME
:
8975 * This DIF object may be cacheable. Now we need to look for any
8976 * array loading instructions, any memory loading instructions, or
8977 * any stores to thread-local variables.
8979 for (i
= 0; i
< dp
->dtdo_len
; i
++) {
8980 uint_t op
= DIF_INSTR_OP(dp
->dtdo_buf
[i
]);
8982 if ((op
>= DIF_OP_LDSB
&& op
<= DIF_OP_LDX
) ||
8983 (op
>= DIF_OP_ULDSB
&& op
<= DIF_OP_ULDX
) ||
8984 (op
>= DIF_OP_RLDSB
&& op
<= DIF_OP_RLDX
) ||
8985 op
== DIF_OP_LDGA
|| op
== DIF_OP_STTS
)
8993 dtrace_difo_hold(dtrace_difo_t
*dp
)
8997 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
9000 ASSERT(dp
->dtdo_refcnt
!= 0);
9003 * We need to check this DIF object for references to the variable
9004 * DIF_VAR_VTIMESTAMP.
9006 for (i
= 0; i
< dp
->dtdo_varlen
; i
++) {
9007 dtrace_difv_t
*v
= &dp
->dtdo_vartab
[i
];
9009 if (v
->dtdv_id
!= DIF_VAR_VTIMESTAMP
)
9012 if (dtrace_vtime_references
++ == 0)
9013 dtrace_vtime_enable();
9018 * This routine calculates the dynamic variable chunksize for a given DIF
9019 * object. The calculation is not fool-proof, and can probably be tricked by
9020 * malicious DIF -- but it works for all compiler-generated DIF. Because this
9021 * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail
9022 * if a dynamic variable size exceeds the chunksize.
9025 dtrace_difo_chunksize(dtrace_difo_t
*dp
, dtrace_vstate_t
*vstate
)
9028 dtrace_key_t tupregs
[DIF_DTR_NREGS
+ 2]; /* +2 for thread and id */
9029 const dif_instr_t
*text
= dp
->dtdo_buf
;
9035 for (pc
= 0; pc
< dp
->dtdo_len
; pc
++) {
9036 dif_instr_t instr
= text
[pc
];
9037 uint_t op
= DIF_INSTR_OP(instr
);
9038 uint_t rd
= DIF_INSTR_RD(instr
);
9039 uint_t r1
= DIF_INSTR_R1(instr
);
9043 dtrace_key_t
*key
= tupregs
;
9047 sval
= dp
->dtdo_inttab
[DIF_INSTR_INTEGER(instr
)];
9052 key
= &tupregs
[DIF_DTR_NREGS
];
9053 key
[0].dttk_size
= 0;
9054 key
[1].dttk_size
= 0;
9056 scope
= DIFV_SCOPE_THREAD
;
9063 if (DIF_INSTR_OP(instr
) == DIF_OP_STTAA
)
9064 key
[nkeys
++].dttk_size
= 0;
9066 key
[nkeys
++].dttk_size
= 0;
9068 if (op
== DIF_OP_STTAA
) {
9069 scope
= DIFV_SCOPE_THREAD
;
9071 scope
= DIFV_SCOPE_GLOBAL
;
9077 if (ttop
== DIF_DTR_NREGS
)
9080 if ((srd
== 0 || sval
== 0) && r1
== DIF_TYPE_STRING
) {
9082 * If the register for the size of the "pushtr"
9083 * is %r0 (or the value is 0) and the type is
9084 * a string, we'll use the system-wide default
9087 tupregs
[ttop
++].dttk_size
=
9088 dtrace_strsize_default
;
9093 tupregs
[ttop
++].dttk_size
= sval
;
9099 if (ttop
== DIF_DTR_NREGS
)
9102 tupregs
[ttop
++].dttk_size
= 0;
9105 case DIF_OP_FLUSHTS
:
9122 * We have a dynamic variable allocation; calculate its size.
9124 for (ksize
= 0, i
= 0; i
< nkeys
; i
++)
9125 ksize
+= P2ROUNDUP(key
[i
].dttk_size
, sizeof (uint64_t));
9127 size
= sizeof (dtrace_dynvar_t
);
9128 size
+= sizeof (dtrace_key_t
) * (nkeys
- 1);
9132 * Now we need to determine the size of the stored data.
9134 id
= DIF_INSTR_VAR(instr
);
9136 for (i
= 0; i
< dp
->dtdo_varlen
; i
++) {
9137 dtrace_difv_t
*v
= &dp
->dtdo_vartab
[i
];
9139 if (v
->dtdv_id
== id
&& v
->dtdv_scope
== scope
) {
9140 size
+= v
->dtdv_type
.dtdt_size
;
9145 if (i
== dp
->dtdo_varlen
)
9149 * We have the size. If this is larger than the chunk size
9150 * for our dynamic variable state, reset the chunk size.
9152 size
= P2ROUNDUP(size
, sizeof (uint64_t));
9154 if (size
> vstate
->dtvs_dynvars
.dtds_chunksize
)
9155 vstate
->dtvs_dynvars
.dtds_chunksize
= size
;
9160 dtrace_difo_init(dtrace_difo_t
*dp
, dtrace_vstate_t
*vstate
)
9162 int oldsvars
, osz
, nsz
, otlocals
, ntlocals
;
9165 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
9166 ASSERT(dp
->dtdo_buf
!= NULL
&& dp
->dtdo_len
!= 0);
9168 for (i
= 0; i
< dp
->dtdo_varlen
; i
++) {
9169 dtrace_difv_t
*v
= &dp
->dtdo_vartab
[i
];
9170 dtrace_statvar_t
*svar
;
9171 dtrace_statvar_t
***svarp
= NULL
;
9173 uint8_t scope
= v
->dtdv_scope
;
9174 int *np
= (int *)NULL
;
9176 if ((id
= v
->dtdv_id
) < DIF_VAR_OTHER_UBASE
)
9179 id
-= DIF_VAR_OTHER_UBASE
;
9182 case DIFV_SCOPE_THREAD
:
9183 while (id
>= (uint_t
)(otlocals
= vstate
->dtvs_ntlocals
)) {
9184 dtrace_difv_t
*tlocals
;
9186 if ((ntlocals
= (otlocals
<< 1)) == 0)
9189 osz
= otlocals
* sizeof (dtrace_difv_t
);
9190 nsz
= ntlocals
* sizeof (dtrace_difv_t
);
9192 tlocals
= kmem_zalloc(nsz
, KM_SLEEP
);
9195 bcopy(vstate
->dtvs_tlocals
,
9197 kmem_free(vstate
->dtvs_tlocals
, osz
);
9200 vstate
->dtvs_tlocals
= tlocals
;
9201 vstate
->dtvs_ntlocals
= ntlocals
;
9204 vstate
->dtvs_tlocals
[id
] = *v
;
9207 case DIFV_SCOPE_LOCAL
:
9208 np
= &vstate
->dtvs_nlocals
;
9209 svarp
= &vstate
->dtvs_locals
;
9211 if (v
->dtdv_type
.dtdt_flags
& DIF_TF_BYREF
)
9212 dsize
= (int)NCPU
* (v
->dtdv_type
.dtdt_size
+
9215 dsize
= (int)NCPU
* sizeof (uint64_t);
9219 case DIFV_SCOPE_GLOBAL
:
9220 np
= &vstate
->dtvs_nglobals
;
9221 svarp
= &vstate
->dtvs_globals
;
9223 if (v
->dtdv_type
.dtdt_flags
& DIF_TF_BYREF
)
9224 dsize
= v
->dtdv_type
.dtdt_size
+
9233 while (id
>= (uint_t
)(oldsvars
= *np
)) {
9234 dtrace_statvar_t
**statics
;
9235 int newsvars
, oldsize
, newsize
;
9237 if ((newsvars
= (oldsvars
<< 1)) == 0)
9240 oldsize
= oldsvars
* sizeof (dtrace_statvar_t
*);
9241 newsize
= newsvars
* sizeof (dtrace_statvar_t
*);
9243 statics
= kmem_zalloc(newsize
, KM_SLEEP
);
9246 bcopy(*svarp
, statics
, oldsize
);
9247 kmem_free(*svarp
, oldsize
);
9254 if ((svar
= (*svarp
)[id
]) == NULL
) {
9255 svar
= kmem_zalloc(sizeof (dtrace_statvar_t
), KM_SLEEP
);
9256 svar
->dtsv_var
= *v
;
9258 if ((svar
->dtsv_size
= dsize
) != 0) {
9259 svar
->dtsv_data
= (uint64_t)(uintptr_t)
9260 kmem_zalloc(dsize
, KM_SLEEP
);
9263 (*svarp
)[id
] = svar
;
9266 svar
->dtsv_refcnt
++;
9269 dtrace_difo_chunksize(dp
, vstate
);
9270 dtrace_difo_hold(dp
);
9273 static dtrace_difo_t
*
9274 dtrace_difo_duplicate(dtrace_difo_t
*dp
, dtrace_vstate_t
*vstate
)
9279 ASSERT(dp
->dtdo_buf
!= NULL
);
9280 ASSERT(dp
->dtdo_refcnt
!= 0);
9282 new = kmem_zalloc(sizeof (dtrace_difo_t
), KM_SLEEP
);
9284 ASSERT(dp
->dtdo_buf
!= NULL
);
9285 sz
= dp
->dtdo_len
* sizeof (dif_instr_t
);
9286 new->dtdo_buf
= kmem_alloc(sz
, KM_SLEEP
);
9287 bcopy(dp
->dtdo_buf
, new->dtdo_buf
, sz
);
9288 new->dtdo_len
= dp
->dtdo_len
;
9290 if (dp
->dtdo_strtab
!= NULL
) {
9291 ASSERT(dp
->dtdo_strlen
!= 0);
9292 new->dtdo_strtab
= kmem_alloc(dp
->dtdo_strlen
, KM_SLEEP
);
9293 bcopy(dp
->dtdo_strtab
, new->dtdo_strtab
, dp
->dtdo_strlen
);
9294 new->dtdo_strlen
= dp
->dtdo_strlen
;
9297 if (dp
->dtdo_inttab
!= NULL
) {
9298 ASSERT(dp
->dtdo_intlen
!= 0);
9299 sz
= dp
->dtdo_intlen
* sizeof (uint64_t);
9300 new->dtdo_inttab
= kmem_alloc(sz
, KM_SLEEP
);
9301 bcopy(dp
->dtdo_inttab
, new->dtdo_inttab
, sz
);
9302 new->dtdo_intlen
= dp
->dtdo_intlen
;
9305 if (dp
->dtdo_vartab
!= NULL
) {
9306 ASSERT(dp
->dtdo_varlen
!= 0);
9307 sz
= dp
->dtdo_varlen
* sizeof (dtrace_difv_t
);
9308 new->dtdo_vartab
= kmem_alloc(sz
, KM_SLEEP
);
9309 bcopy(dp
->dtdo_vartab
, new->dtdo_vartab
, sz
);
9310 new->dtdo_varlen
= dp
->dtdo_varlen
;
9313 dtrace_difo_init(new, vstate
);
9318 dtrace_difo_destroy(dtrace_difo_t
*dp
, dtrace_vstate_t
*vstate
)
9322 ASSERT(dp
->dtdo_refcnt
== 0);
9324 for (i
= 0; i
< dp
->dtdo_varlen
; i
++) {
9325 dtrace_difv_t
*v
= &dp
->dtdo_vartab
[i
];
9326 dtrace_statvar_t
*svar
;
9327 dtrace_statvar_t
**svarp
= NULL
;
9329 uint8_t scope
= v
->dtdv_scope
;
9333 case DIFV_SCOPE_THREAD
:
9336 case DIFV_SCOPE_LOCAL
:
9337 np
= &vstate
->dtvs_nlocals
;
9338 svarp
= vstate
->dtvs_locals
;
9341 case DIFV_SCOPE_GLOBAL
:
9342 np
= &vstate
->dtvs_nglobals
;
9343 svarp
= vstate
->dtvs_globals
;
9350 if ((id
= v
->dtdv_id
) < DIF_VAR_OTHER_UBASE
)
9353 id
-= DIF_VAR_OTHER_UBASE
;
9355 ASSERT(id
< (uint_t
)*np
);
9358 ASSERT(svar
!= NULL
);
9359 ASSERT(svar
->dtsv_refcnt
> 0);
9361 if (--svar
->dtsv_refcnt
> 0)
9364 if (svar
->dtsv_size
!= 0) {
9365 ASSERT(svar
->dtsv_data
!= 0);
9366 kmem_free((void *)(uintptr_t)svar
->dtsv_data
,
9370 kmem_free(svar
, sizeof (dtrace_statvar_t
));
9374 kmem_free(dp
->dtdo_buf
, dp
->dtdo_len
* sizeof (dif_instr_t
));
9375 kmem_free(dp
->dtdo_inttab
, dp
->dtdo_intlen
* sizeof (uint64_t));
9376 kmem_free(dp
->dtdo_strtab
, dp
->dtdo_strlen
);
9377 kmem_free(dp
->dtdo_vartab
, dp
->dtdo_varlen
* sizeof (dtrace_difv_t
));
9379 kmem_free(dp
, sizeof (dtrace_difo_t
));
9383 dtrace_difo_release(dtrace_difo_t
*dp
, dtrace_vstate_t
*vstate
)
9387 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
9388 ASSERT(dp
->dtdo_refcnt
!= 0);
9390 for (i
= 0; i
< dp
->dtdo_varlen
; i
++) {
9391 dtrace_difv_t
*v
= &dp
->dtdo_vartab
[i
];
9393 if (v
->dtdv_id
!= DIF_VAR_VTIMESTAMP
)
9396 ASSERT(dtrace_vtime_references
> 0);
9397 if (--dtrace_vtime_references
== 0)
9398 dtrace_vtime_disable();
9401 if (--dp
->dtdo_refcnt
== 0)
9402 dtrace_difo_destroy(dp
, vstate
);
9406 * DTrace Format Functions
9409 dtrace_format_add(dtrace_state_t
*state
, char *str
)
9412 uint16_t ndx
, len
= strlen(str
) + 1;
9414 fmt
= kmem_zalloc(len
, KM_SLEEP
);
9415 bcopy(str
, fmt
, len
);
9417 for (ndx
= 0; ndx
< state
->dts_nformats
; ndx
++) {
9418 if (state
->dts_formats
[ndx
] == NULL
) {
9419 state
->dts_formats
[ndx
] = fmt
;
9424 if (state
->dts_nformats
== USHRT_MAX
) {
9426 * This is only likely if a denial-of-service attack is being
9427 * attempted. As such, it's okay to fail silently here.
9429 kmem_free(fmt
, len
);
9434 * For simplicity, we always resize the formats array to be exactly the
9435 * number of formats.
9437 ndx
= state
->dts_nformats
++;
9438 new = kmem_alloc((ndx
+ 1) * sizeof (char *), KM_SLEEP
);
9440 if (state
->dts_formats
!= NULL
) {
9442 bcopy(state
->dts_formats
, new, ndx
* sizeof (char *));
9443 kmem_free(state
->dts_formats
, ndx
* sizeof (char *));
9446 state
->dts_formats
= new;
9447 state
->dts_formats
[ndx
] = fmt
;
9453 dtrace_format_remove(dtrace_state_t
*state
, uint16_t format
)
9457 ASSERT(state
->dts_formats
!= NULL
);
9458 ASSERT(format
<= state
->dts_nformats
);
9459 ASSERT(state
->dts_formats
[format
- 1] != NULL
);
9461 fmt
= state
->dts_formats
[format
- 1];
9462 kmem_free(fmt
, strlen(fmt
) + 1);
9463 state
->dts_formats
[format
- 1] = NULL
;
9467 dtrace_format_destroy(dtrace_state_t
*state
)
9471 if (state
->dts_nformats
== 0) {
9472 ASSERT(state
->dts_formats
== NULL
);
9476 ASSERT(state
->dts_formats
!= NULL
);
9478 for (i
= 0; i
< state
->dts_nformats
; i
++) {
9479 char *fmt
= state
->dts_formats
[i
];
9484 kmem_free(fmt
, strlen(fmt
) + 1);
9487 kmem_free(state
->dts_formats
, state
->dts_nformats
* sizeof (char *));
9488 state
->dts_nformats
= 0;
9489 state
->dts_formats
= NULL
;
9493 * DTrace Predicate Functions
9495 static dtrace_predicate_t
*
9496 dtrace_predicate_create(dtrace_difo_t
*dp
)
9498 dtrace_predicate_t
*pred
;
9500 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
9501 ASSERT(dp
->dtdo_refcnt
!= 0);
9503 pred
= kmem_zalloc(sizeof (dtrace_predicate_t
), KM_SLEEP
);
9504 pred
->dtp_difo
= dp
;
9505 pred
->dtp_refcnt
= 1;
9507 if (!dtrace_difo_cacheable(dp
))
9510 if (dtrace_predcache_id
== DTRACE_CACHEIDNONE
) {
9512 * This is only theoretically possible -- we have had 2^32
9513 * cacheable predicates on this machine. We cannot allow any
9514 * more predicates to become cacheable: as unlikely as it is,
9515 * there may be a thread caching a (now stale) predicate cache
9516 * ID. (N.B.: the temptation is being successfully resisted to
9517 * have this cmn_err() "Holy shit -- we executed this code!")
9522 pred
->dtp_cacheid
= dtrace_predcache_id
++;
9528 dtrace_predicate_hold(dtrace_predicate_t
*pred
)
9530 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
9531 ASSERT(pred
->dtp_difo
!= NULL
&& pred
->dtp_difo
->dtdo_refcnt
!= 0);
9532 ASSERT(pred
->dtp_refcnt
> 0);
9538 dtrace_predicate_release(dtrace_predicate_t
*pred
, dtrace_vstate_t
*vstate
)
9540 dtrace_difo_t
*dp
= pred
->dtp_difo
;
9541 #pragma unused(dp) /* __APPLE__ */
9543 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
9544 ASSERT(dp
!= NULL
&& dp
->dtdo_refcnt
!= 0);
9545 ASSERT(pred
->dtp_refcnt
> 0);
9547 if (--pred
->dtp_refcnt
== 0) {
9548 dtrace_difo_release(pred
->dtp_difo
, vstate
);
9549 kmem_free(pred
, sizeof (dtrace_predicate_t
));
9554 * DTrace Action Description Functions
9556 static dtrace_actdesc_t
*
9557 dtrace_actdesc_create(dtrace_actkind_t kind
, uint32_t ntuple
,
9558 uint64_t uarg
, uint64_t arg
)
9560 dtrace_actdesc_t
*act
;
9562 ASSERT(!DTRACEACT_ISPRINTFLIKE(kind
) || (arg
!= 0 &&
9563 arg
>= KERNELBASE
) || (arg
== 0 && kind
== DTRACEACT_PRINTA
));
9565 act
= kmem_zalloc(sizeof (dtrace_actdesc_t
), KM_SLEEP
);
9566 act
->dtad_kind
= kind
;
9567 act
->dtad_ntuple
= ntuple
;
9568 act
->dtad_uarg
= uarg
;
9569 act
->dtad_arg
= arg
;
9570 act
->dtad_refcnt
= 1;
9576 dtrace_actdesc_hold(dtrace_actdesc_t
*act
)
9578 ASSERT(act
->dtad_refcnt
>= 1);
9583 dtrace_actdesc_release(dtrace_actdesc_t
*act
, dtrace_vstate_t
*vstate
)
9585 dtrace_actkind_t kind
= act
->dtad_kind
;
9588 ASSERT(act
->dtad_refcnt
>= 1);
9590 if (--act
->dtad_refcnt
!= 0)
9593 if ((dp
= act
->dtad_difo
) != NULL
)
9594 dtrace_difo_release(dp
, vstate
);
9596 if (DTRACEACT_ISPRINTFLIKE(kind
)) {
9597 char *str
= (char *)(uintptr_t)act
->dtad_arg
;
9599 ASSERT((str
!= NULL
&& (uintptr_t)str
>= KERNELBASE
) ||
9600 (str
== NULL
&& act
->dtad_kind
== DTRACEACT_PRINTA
));
9603 kmem_free(str
, strlen(str
) + 1);
9606 kmem_free(act
, sizeof (dtrace_actdesc_t
));
9610 * DTrace ECB Functions
9612 static dtrace_ecb_t
*
9613 dtrace_ecb_add(dtrace_state_t
*state
, dtrace_probe_t
*probe
)
9618 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
9620 ecb
= kmem_zalloc(sizeof (dtrace_ecb_t
), KM_SLEEP
);
9621 ecb
->dte_predicate
= NULL
;
9622 ecb
->dte_probe
= probe
;
9625 * The default size is the size of the default action: recording
9628 ecb
->dte_size
= ecb
->dte_needed
= sizeof (dtrace_rechdr_t
);
9629 ecb
->dte_alignment
= sizeof (dtrace_epid_t
);
9631 epid
= state
->dts_epid
++;
9633 if (epid
- 1 >= (dtrace_epid_t
)state
->dts_necbs
) {
9634 dtrace_ecb_t
**oecbs
= state
->dts_ecbs
, **ecbs
;
9635 int necbs
= state
->dts_necbs
<< 1;
9637 ASSERT(epid
== (dtrace_epid_t
)state
->dts_necbs
+ 1);
9640 ASSERT(oecbs
== NULL
);
9644 ecbs
= kmem_zalloc(necbs
* sizeof (*ecbs
), KM_SLEEP
);
9647 bcopy(oecbs
, ecbs
, state
->dts_necbs
* sizeof (*ecbs
));
9649 dtrace_membar_producer();
9650 state
->dts_ecbs
= ecbs
;
9652 if (oecbs
!= NULL
) {
9654 * If this state is active, we must dtrace_sync()
9655 * before we can free the old dts_ecbs array: we're
9656 * coming in hot, and there may be active ring
9657 * buffer processing (which indexes into the dts_ecbs
9658 * array) on another CPU.
9660 if (state
->dts_activity
!= DTRACE_ACTIVITY_INACTIVE
)
9663 kmem_free(oecbs
, state
->dts_necbs
* sizeof (*ecbs
));
9666 dtrace_membar_producer();
9667 state
->dts_necbs
= necbs
;
9670 ecb
->dte_state
= state
;
9672 ASSERT(state
->dts_ecbs
[epid
- 1] == NULL
);
9673 dtrace_membar_producer();
9674 state
->dts_ecbs
[(ecb
->dte_epid
= epid
) - 1] = ecb
;
9680 dtrace_ecb_enable(dtrace_ecb_t
*ecb
)
9682 dtrace_probe_t
*probe
= ecb
->dte_probe
;
9684 lck_mtx_assert(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
9685 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
9686 ASSERT(ecb
->dte_next
== NULL
);
9688 if (probe
== NULL
) {
9690 * This is the NULL probe -- there's nothing to do.
9695 probe
->dtpr_provider
->dtpv_ecb_count
++;
9696 if (probe
->dtpr_ecb
== NULL
) {
9697 dtrace_provider_t
*prov
= probe
->dtpr_provider
;
9700 * We're the first ECB on this probe.
9702 probe
->dtpr_ecb
= probe
->dtpr_ecb_last
= ecb
;
9704 if (ecb
->dte_predicate
!= NULL
)
9705 probe
->dtpr_predcache
= ecb
->dte_predicate
->dtp_cacheid
;
9707 return (prov
->dtpv_pops
.dtps_enable(prov
->dtpv_arg
,
9708 probe
->dtpr_id
, probe
->dtpr_arg
));
9711 * This probe is already active. Swing the last pointer to
9712 * point to the new ECB, and issue a dtrace_sync() to assure
9713 * that all CPUs have seen the change.
9715 ASSERT(probe
->dtpr_ecb_last
!= NULL
);
9716 probe
->dtpr_ecb_last
->dte_next
= ecb
;
9717 probe
->dtpr_ecb_last
= ecb
;
9718 probe
->dtpr_predcache
= 0;
9726 dtrace_ecb_resize(dtrace_ecb_t
*ecb
)
9728 dtrace_action_t
*act
;
9729 uint32_t curneeded
= UINT32_MAX
;
9730 uint32_t aggbase
= UINT32_MAX
;
9733 * If we record anything, we always record the dtrace_rechdr_t. (And
9734 * we always record it first.)
9736 ecb
->dte_size
= sizeof (dtrace_rechdr_t
);
9737 ecb
->dte_alignment
= sizeof (dtrace_epid_t
);
9739 for (act
= ecb
->dte_action
; act
!= NULL
; act
= act
->dta_next
) {
9740 dtrace_recdesc_t
*rec
= &act
->dta_rec
;
9741 ASSERT(rec
->dtrd_size
> 0 || rec
->dtrd_alignment
== 1);
9743 ecb
->dte_alignment
= MAX(ecb
->dte_alignment
, rec
->dtrd_alignment
);
9745 if (DTRACEACT_ISAGG(act
->dta_kind
)) {
9746 dtrace_aggregation_t
*agg
= (dtrace_aggregation_t
*)act
;
9748 ASSERT(rec
->dtrd_size
!= 0);
9749 ASSERT(agg
->dtag_first
!= NULL
);
9750 ASSERT(act
->dta_prev
->dta_intuple
);
9751 ASSERT(aggbase
!= UINT32_MAX
);
9752 ASSERT(curneeded
!= UINT32_MAX
);
9754 agg
->dtag_base
= aggbase
;
9756 curneeded
= P2ROUNDUP(curneeded
, rec
->dtrd_alignment
);
9757 rec
->dtrd_offset
= curneeded
;
9758 curneeded
+= rec
->dtrd_size
;
9759 ecb
->dte_needed
= MAX(ecb
->dte_needed
, curneeded
);
9761 aggbase
= UINT32_MAX
;
9762 curneeded
= UINT32_MAX
;
9763 } else if (act
->dta_intuple
) {
9764 if (curneeded
== UINT32_MAX
) {
9766 * This is the first record in a tuple. Align
9767 * curneeded to be at offset 4 in an 8-byte
9770 ASSERT(act
->dta_prev
== NULL
|| !act
->dta_prev
->dta_intuple
);
9771 ASSERT(aggbase
== UINT32_MAX
);
9773 curneeded
= P2PHASEUP(ecb
->dte_size
,
9774 sizeof (uint64_t), sizeof (dtrace_aggid_t
));
9776 aggbase
= curneeded
- sizeof (dtrace_aggid_t
);
9777 ASSERT(IS_P2ALIGNED(aggbase
,
9778 sizeof (uint64_t)));
9781 curneeded
= P2ROUNDUP(curneeded
, rec
->dtrd_alignment
);
9782 rec
->dtrd_offset
= curneeded
;
9783 curneeded
+= rec
->dtrd_size
;
9785 /* tuples must be followed by an aggregation */
9786 ASSERT(act
->dta_prev
== NULL
|| !act
->dta_prev
->dta_intuple
);
9787 ecb
->dte_size
= P2ROUNDUP(ecb
->dte_size
, rec
->dtrd_alignment
);
9788 rec
->dtrd_offset
= ecb
->dte_size
;
9789 ecb
->dte_size
+= rec
->dtrd_size
;
9790 ecb
->dte_needed
= MAX(ecb
->dte_needed
, ecb
->dte_size
);
9794 if ((act
= ecb
->dte_action
) != NULL
&&
9795 !(act
->dta_kind
== DTRACEACT_SPECULATE
&& act
->dta_next
== NULL
) &&
9796 ecb
->dte_size
== sizeof (dtrace_rechdr_t
)) {
9798 * If the size is still sizeof (dtrace_rechdr_t), then all
9799 * actions store no data; set the size to 0.
9804 ecb
->dte_size
= P2ROUNDUP(ecb
->dte_size
, sizeof (dtrace_epid_t
));
9805 ecb
->dte_needed
= P2ROUNDUP(ecb
->dte_needed
, (sizeof (dtrace_epid_t
)));
9806 ecb
->dte_state
->dts_needed
= MAX(ecb
->dte_state
->dts_needed
, ecb
->dte_needed
);
9809 static dtrace_action_t
*
9810 dtrace_ecb_aggregation_create(dtrace_ecb_t
*ecb
, dtrace_actdesc_t
*desc
)
9812 dtrace_aggregation_t
*agg
;
9813 size_t size
= sizeof (uint64_t);
9814 int ntuple
= desc
->dtad_ntuple
;
9815 dtrace_action_t
*act
;
9816 dtrace_recdesc_t
*frec
;
9817 dtrace_aggid_t aggid
;
9818 dtrace_state_t
*state
= ecb
->dte_state
;
9820 agg
= kmem_zalloc(sizeof (dtrace_aggregation_t
), KM_SLEEP
);
9821 agg
->dtag_ecb
= ecb
;
9823 ASSERT(DTRACEACT_ISAGG(desc
->dtad_kind
));
9825 switch (desc
->dtad_kind
) {
9827 agg
->dtag_initial
= INT64_MAX
;
9828 agg
->dtag_aggregate
= dtrace_aggregate_min
;
9832 agg
->dtag_initial
= INT64_MIN
;
9833 agg
->dtag_aggregate
= dtrace_aggregate_max
;
9836 case DTRACEAGG_COUNT
:
9837 agg
->dtag_aggregate
= dtrace_aggregate_count
;
9840 case DTRACEAGG_QUANTIZE
:
9841 agg
->dtag_aggregate
= dtrace_aggregate_quantize
;
9842 size
= (((sizeof (uint64_t) * NBBY
) - 1) * 2 + 1) *
9846 case DTRACEAGG_LQUANTIZE
: {
9847 uint16_t step
= DTRACE_LQUANTIZE_STEP(desc
->dtad_arg
);
9848 uint16_t levels
= DTRACE_LQUANTIZE_LEVELS(desc
->dtad_arg
);
9850 agg
->dtag_initial
= desc
->dtad_arg
;
9851 agg
->dtag_aggregate
= dtrace_aggregate_lquantize
;
9853 if (step
== 0 || levels
== 0)
9856 size
= levels
* sizeof (uint64_t) + 3 * sizeof (uint64_t);
9860 case DTRACEAGG_LLQUANTIZE
: {
9861 uint16_t factor
= DTRACE_LLQUANTIZE_FACTOR(desc
->dtad_arg
);
9862 uint16_t low
= DTRACE_LLQUANTIZE_LOW(desc
->dtad_arg
);
9863 uint16_t high
= DTRACE_LLQUANTIZE_HIGH(desc
->dtad_arg
);
9864 uint16_t nsteps
= DTRACE_LLQUANTIZE_NSTEP(desc
->dtad_arg
);
9867 agg
->dtag_initial
= desc
->dtad_arg
;
9868 agg
->dtag_aggregate
= dtrace_aggregate_llquantize
;
9870 if (factor
< 2 || low
>= high
|| nsteps
< factor
)
9874 * Now check that the number of steps evenly divides a power
9875 * of the factor. (This assures both integer bucket size and
9876 * linearity within each magnitude.)
9878 for (v
= factor
; v
< nsteps
; v
*= factor
)
9881 if ((v
% nsteps
) || (nsteps
% factor
))
9884 size
= (dtrace_aggregate_llquantize_bucket(factor
, low
, high
, nsteps
, INT64_MAX
) + 2) * sizeof (uint64_t);
9889 agg
->dtag_aggregate
= dtrace_aggregate_avg
;
9890 size
= sizeof (uint64_t) * 2;
9893 case DTRACEAGG_STDDEV
:
9894 agg
->dtag_aggregate
= dtrace_aggregate_stddev
;
9895 size
= sizeof (uint64_t) * 4;
9899 agg
->dtag_aggregate
= dtrace_aggregate_sum
;
9906 agg
->dtag_action
.dta_rec
.dtrd_size
= size
;
9912 * We must make sure that we have enough actions for the n-tuple.
9914 for (act
= ecb
->dte_action_last
; act
!= NULL
; act
= act
->dta_prev
) {
9915 if (DTRACEACT_ISAGG(act
->dta_kind
))
9918 if (--ntuple
== 0) {
9920 * This is the action with which our n-tuple begins.
9922 agg
->dtag_first
= act
;
9928 * This n-tuple is short by ntuple elements. Return failure.
9930 ASSERT(ntuple
!= 0);
9932 kmem_free(agg
, sizeof (dtrace_aggregation_t
));
9937 * If the last action in the tuple has a size of zero, it's actually
9938 * an expression argument for the aggregating action.
9940 ASSERT(ecb
->dte_action_last
!= NULL
);
9941 act
= ecb
->dte_action_last
;
9943 if (act
->dta_kind
== DTRACEACT_DIFEXPR
) {
9944 ASSERT(act
->dta_difo
!= NULL
);
9946 if (act
->dta_difo
->dtdo_rtype
.dtdt_size
== 0)
9947 agg
->dtag_hasarg
= 1;
9951 * We need to allocate an id for this aggregation.
9953 aggid
= (dtrace_aggid_t
)(uintptr_t)vmem_alloc(state
->dts_aggid_arena
, 1,
9954 VM_BESTFIT
| VM_SLEEP
);
9956 if (aggid
- 1 >= (dtrace_aggid_t
)state
->dts_naggregations
) {
9957 dtrace_aggregation_t
**oaggs
= state
->dts_aggregations
;
9958 dtrace_aggregation_t
**aggs
;
9959 int naggs
= state
->dts_naggregations
<< 1;
9960 int onaggs
= state
->dts_naggregations
;
9962 ASSERT(aggid
== (dtrace_aggid_t
)state
->dts_naggregations
+ 1);
9965 ASSERT(oaggs
== NULL
);
9969 aggs
= kmem_zalloc(naggs
* sizeof (*aggs
), KM_SLEEP
);
9971 if (oaggs
!= NULL
) {
9972 bcopy(oaggs
, aggs
, onaggs
* sizeof (*aggs
));
9973 kmem_free(oaggs
, onaggs
* sizeof (*aggs
));
9976 state
->dts_aggregations
= aggs
;
9977 state
->dts_naggregations
= naggs
;
9980 ASSERT(state
->dts_aggregations
[aggid
- 1] == NULL
);
9981 state
->dts_aggregations
[(agg
->dtag_id
= aggid
) - 1] = agg
;
9983 frec
= &agg
->dtag_first
->dta_rec
;
9984 if (frec
->dtrd_alignment
< sizeof (dtrace_aggid_t
))
9985 frec
->dtrd_alignment
= sizeof (dtrace_aggid_t
);
9987 for (act
= agg
->dtag_first
; act
!= NULL
; act
= act
->dta_next
) {
9988 ASSERT(!act
->dta_intuple
);
9989 act
->dta_intuple
= 1;
9992 return (&agg
->dtag_action
);
9996 dtrace_ecb_aggregation_destroy(dtrace_ecb_t
*ecb
, dtrace_action_t
*act
)
9998 dtrace_aggregation_t
*agg
= (dtrace_aggregation_t
*)act
;
9999 dtrace_state_t
*state
= ecb
->dte_state
;
10000 dtrace_aggid_t aggid
= agg
->dtag_id
;
10002 ASSERT(DTRACEACT_ISAGG(act
->dta_kind
));
10003 vmem_free(state
->dts_aggid_arena
, (void *)(uintptr_t)aggid
, 1);
10005 ASSERT(state
->dts_aggregations
[aggid
- 1] == agg
);
10006 state
->dts_aggregations
[aggid
- 1] = NULL
;
10008 kmem_free(agg
, sizeof (dtrace_aggregation_t
));
10012 dtrace_ecb_action_add(dtrace_ecb_t
*ecb
, dtrace_actdesc_t
*desc
)
10014 dtrace_action_t
*action
, *last
;
10015 dtrace_difo_t
*dp
= desc
->dtad_difo
;
10016 uint32_t size
= 0, align
= sizeof (uint8_t), mask
;
10017 uint16_t format
= 0;
10018 dtrace_recdesc_t
*rec
;
10019 dtrace_state_t
*state
= ecb
->dte_state
;
10020 dtrace_optval_t
*opt
= state
->dts_options
;
10021 dtrace_optval_t nframes
=0, strsize
;
10022 uint64_t arg
= desc
->dtad_arg
;
10024 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
10025 ASSERT(ecb
->dte_action
== NULL
|| ecb
->dte_action
->dta_refcnt
== 1);
10027 if (DTRACEACT_ISAGG(desc
->dtad_kind
)) {
10029 * If this is an aggregating action, there must be neither
10030 * a speculate nor a commit on the action chain.
10032 dtrace_action_t
*act
;
10034 for (act
= ecb
->dte_action
; act
!= NULL
; act
= act
->dta_next
) {
10035 if (act
->dta_kind
== DTRACEACT_COMMIT
)
10038 if (act
->dta_kind
== DTRACEACT_SPECULATE
)
10042 action
= dtrace_ecb_aggregation_create(ecb
, desc
);
10044 if (action
== NULL
)
10047 if (DTRACEACT_ISDESTRUCTIVE(desc
->dtad_kind
) ||
10048 (desc
->dtad_kind
== DTRACEACT_DIFEXPR
&&
10049 dp
!= NULL
&& dp
->dtdo_destructive
)) {
10050 state
->dts_destructive
= 1;
10053 switch (desc
->dtad_kind
) {
10054 case DTRACEACT_PRINTF
:
10055 case DTRACEACT_PRINTA
:
10056 case DTRACEACT_SYSTEM
:
10057 case DTRACEACT_FREOPEN
:
10059 * We know that our arg is a string -- turn it into a
10063 ASSERT(desc
->dtad_kind
== DTRACEACT_PRINTA
);
10067 ASSERT(arg
> KERNELBASE
);
10068 format
= dtrace_format_add(state
,
10069 (char *)(uintptr_t)arg
);
10073 case DTRACEACT_LIBACT
:
10074 case DTRACEACT_DIFEXPR
:
10075 case DTRACEACT_TRACEMEM
:
10076 case DTRACEACT_TRACEMEM_DYNSIZE
:
10077 case DTRACEACT_APPLEBINARY
: /* __APPLE__ */
10081 if ((size
= dp
->dtdo_rtype
.dtdt_size
) != 0)
10084 if (dp
->dtdo_rtype
.dtdt_kind
== DIF_TYPE_STRING
) {
10085 if (!(dp
->dtdo_rtype
.dtdt_flags
& DIF_TF_BYREF
))
10088 size
= opt
[DTRACEOPT_STRSIZE
];
10093 case DTRACEACT_STACK
:
10094 if ((nframes
= arg
) == 0) {
10095 nframes
= opt
[DTRACEOPT_STACKFRAMES
];
10096 ASSERT(nframes
> 0);
10100 size
= nframes
* sizeof (pc_t
);
10103 case DTRACEACT_JSTACK
:
10104 if ((strsize
= DTRACE_USTACK_STRSIZE(arg
)) == 0)
10105 strsize
= opt
[DTRACEOPT_JSTACKSTRSIZE
];
10107 if ((nframes
= DTRACE_USTACK_NFRAMES(arg
)) == 0)
10108 nframes
= opt
[DTRACEOPT_JSTACKFRAMES
];
10110 arg
= DTRACE_USTACK_ARG(nframes
, strsize
);
10113 case DTRACEACT_USTACK
:
10114 if (desc
->dtad_kind
!= DTRACEACT_JSTACK
&&
10115 (nframes
= DTRACE_USTACK_NFRAMES(arg
)) == 0) {
10116 strsize
= DTRACE_USTACK_STRSIZE(arg
);
10117 nframes
= opt
[DTRACEOPT_USTACKFRAMES
];
10118 ASSERT(nframes
> 0);
10119 arg
= DTRACE_USTACK_ARG(nframes
, strsize
);
10123 * Save a slot for the pid.
10125 size
= (nframes
+ 1) * sizeof (uint64_t);
10126 size
+= DTRACE_USTACK_STRSIZE(arg
);
10127 size
= P2ROUNDUP(size
, (uint32_t)(sizeof (uintptr_t)));
10131 case DTRACEACT_SYM
:
10132 case DTRACEACT_MOD
:
10133 if (dp
== NULL
|| ((size
= dp
->dtdo_rtype
.dtdt_size
) !=
10134 sizeof (uint64_t)) ||
10135 (dp
->dtdo_rtype
.dtdt_flags
& DIF_TF_BYREF
))
10139 case DTRACEACT_USYM
:
10140 case DTRACEACT_UMOD
:
10141 case DTRACEACT_UADDR
:
10143 (dp
->dtdo_rtype
.dtdt_size
!= sizeof (uint64_t)) ||
10144 (dp
->dtdo_rtype
.dtdt_flags
& DIF_TF_BYREF
))
10148 * We have a slot for the pid, plus a slot for the
10149 * argument. To keep things simple (aligned with
10150 * bitness-neutral sizing), we store each as a 64-bit
10153 size
= 2 * sizeof (uint64_t);
10156 case DTRACEACT_STOP
:
10157 case DTRACEACT_BREAKPOINT
:
10158 case DTRACEACT_PANIC
:
10161 case DTRACEACT_CHILL
:
10162 case DTRACEACT_DISCARD
:
10163 case DTRACEACT_RAISE
:
10164 case DTRACEACT_PIDRESUME
: /* __APPLE__ */
10169 case DTRACEACT_EXIT
:
10171 (size
= dp
->dtdo_rtype
.dtdt_size
) != sizeof (int) ||
10172 (dp
->dtdo_rtype
.dtdt_flags
& DIF_TF_BYREF
))
10176 case DTRACEACT_SPECULATE
:
10177 if (ecb
->dte_size
> sizeof (dtrace_rechdr_t
))
10183 state
->dts_speculates
= 1;
10186 case DTRACEACT_COMMIT
: {
10187 dtrace_action_t
*act
= ecb
->dte_action
;
10189 for (; act
!= NULL
; act
= act
->dta_next
) {
10190 if (act
->dta_kind
== DTRACEACT_COMMIT
)
10203 if (size
!= 0 || desc
->dtad_kind
== DTRACEACT_SPECULATE
) {
10205 * If this is a data-storing action or a speculate,
10206 * we must be sure that there isn't a commit on the
10209 dtrace_action_t
*act
= ecb
->dte_action
;
10211 for (; act
!= NULL
; act
= act
->dta_next
) {
10212 if (act
->dta_kind
== DTRACEACT_COMMIT
)
10217 action
= kmem_zalloc(sizeof (dtrace_action_t
), KM_SLEEP
);
10218 action
->dta_rec
.dtrd_size
= size
;
10221 action
->dta_refcnt
= 1;
10222 rec
= &action
->dta_rec
;
10223 size
= rec
->dtrd_size
;
10225 for (mask
= sizeof (uint64_t) - 1; size
!= 0 && mask
> 0; mask
>>= 1) {
10226 if (!(size
& mask
)) {
10232 action
->dta_kind
= desc
->dtad_kind
;
10234 if ((action
->dta_difo
= dp
) != NULL
)
10235 dtrace_difo_hold(dp
);
10237 rec
->dtrd_action
= action
->dta_kind
;
10238 rec
->dtrd_arg
= arg
;
10239 rec
->dtrd_uarg
= desc
->dtad_uarg
;
10240 rec
->dtrd_alignment
= (uint16_t)align
;
10241 rec
->dtrd_format
= format
;
10243 if ((last
= ecb
->dte_action_last
) != NULL
) {
10244 ASSERT(ecb
->dte_action
!= NULL
);
10245 action
->dta_prev
= last
;
10246 last
->dta_next
= action
;
10248 ASSERT(ecb
->dte_action
== NULL
);
10249 ecb
->dte_action
= action
;
10252 ecb
->dte_action_last
= action
;
10258 dtrace_ecb_action_remove(dtrace_ecb_t
*ecb
)
10260 dtrace_action_t
*act
= ecb
->dte_action
, *next
;
10261 dtrace_vstate_t
*vstate
= &ecb
->dte_state
->dts_vstate
;
10265 if (act
!= NULL
&& act
->dta_refcnt
> 1) {
10266 ASSERT(act
->dta_next
== NULL
|| act
->dta_next
->dta_refcnt
== 1);
10269 for (; act
!= NULL
; act
= next
) {
10270 next
= act
->dta_next
;
10271 ASSERT(next
!= NULL
|| act
== ecb
->dte_action_last
);
10272 ASSERT(act
->dta_refcnt
== 1);
10274 if ((format
= act
->dta_rec
.dtrd_format
) != 0)
10275 dtrace_format_remove(ecb
->dte_state
, format
);
10277 if ((dp
= act
->dta_difo
) != NULL
)
10278 dtrace_difo_release(dp
, vstate
);
10280 if (DTRACEACT_ISAGG(act
->dta_kind
)) {
10281 dtrace_ecb_aggregation_destroy(ecb
, act
);
10283 kmem_free(act
, sizeof (dtrace_action_t
));
10288 ecb
->dte_action
= NULL
;
10289 ecb
->dte_action_last
= NULL
;
10294 dtrace_ecb_disable(dtrace_ecb_t
*ecb
)
10297 * We disable the ECB by removing it from its probe.
10299 dtrace_ecb_t
*pecb
, *prev
= NULL
;
10300 dtrace_probe_t
*probe
= ecb
->dte_probe
;
10302 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
10304 if (probe
== NULL
) {
10306 * This is the NULL probe; there is nothing to disable.
10311 for (pecb
= probe
->dtpr_ecb
; pecb
!= NULL
; pecb
= pecb
->dte_next
) {
10317 ASSERT(pecb
!= NULL
);
10319 if (prev
== NULL
) {
10320 probe
->dtpr_ecb
= ecb
->dte_next
;
10322 prev
->dte_next
= ecb
->dte_next
;
10325 if (ecb
== probe
->dtpr_ecb_last
) {
10326 ASSERT(ecb
->dte_next
== NULL
);
10327 probe
->dtpr_ecb_last
= prev
;
10330 probe
->dtpr_provider
->dtpv_ecb_count
--;
10332 * The ECB has been disconnected from the probe; now sync to assure
10333 * that all CPUs have seen the change before returning.
10337 if (probe
->dtpr_ecb
== NULL
) {
10339 * That was the last ECB on the probe; clear the predicate
10340 * cache ID for the probe, disable it and sync one more time
10341 * to assure that we'll never hit it again.
10343 dtrace_provider_t
*prov
= probe
->dtpr_provider
;
10345 ASSERT(ecb
->dte_next
== NULL
);
10346 ASSERT(probe
->dtpr_ecb_last
== NULL
);
10347 probe
->dtpr_predcache
= DTRACE_CACHEIDNONE
;
10348 prov
->dtpv_pops
.dtps_disable(prov
->dtpv_arg
,
10349 probe
->dtpr_id
, probe
->dtpr_arg
);
10353 * There is at least one ECB remaining on the probe. If there
10354 * is _exactly_ one, set the probe's predicate cache ID to be
10355 * the predicate cache ID of the remaining ECB.
10357 ASSERT(probe
->dtpr_ecb_last
!= NULL
);
10358 ASSERT(probe
->dtpr_predcache
== DTRACE_CACHEIDNONE
);
10360 if (probe
->dtpr_ecb
== probe
->dtpr_ecb_last
) {
10361 dtrace_predicate_t
*p
= probe
->dtpr_ecb
->dte_predicate
;
10363 ASSERT(probe
->dtpr_ecb
->dte_next
== NULL
);
10366 probe
->dtpr_predcache
= p
->dtp_cacheid
;
10369 ecb
->dte_next
= NULL
;
10374 dtrace_ecb_destroy(dtrace_ecb_t
*ecb
)
10376 dtrace_state_t
*state
= ecb
->dte_state
;
10377 dtrace_vstate_t
*vstate
= &state
->dts_vstate
;
10378 dtrace_predicate_t
*pred
;
10379 dtrace_epid_t epid
= ecb
->dte_epid
;
10381 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
10382 ASSERT(ecb
->dte_next
== NULL
);
10383 ASSERT(ecb
->dte_probe
== NULL
|| ecb
->dte_probe
->dtpr_ecb
!= ecb
);
10385 if ((pred
= ecb
->dte_predicate
) != NULL
)
10386 dtrace_predicate_release(pred
, vstate
);
10388 dtrace_ecb_action_remove(ecb
);
10390 ASSERT(state
->dts_ecbs
[epid
- 1] == ecb
);
10391 state
->dts_ecbs
[epid
- 1] = NULL
;
10393 kmem_free(ecb
, sizeof (dtrace_ecb_t
));
10396 static dtrace_ecb_t
*
10397 dtrace_ecb_create(dtrace_state_t
*state
, dtrace_probe_t
*probe
,
10398 dtrace_enabling_t
*enab
)
10401 dtrace_predicate_t
*pred
;
10402 dtrace_actdesc_t
*act
;
10403 dtrace_provider_t
*prov
;
10404 dtrace_ecbdesc_t
*desc
= enab
->dten_current
;
10406 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
10407 ASSERT(state
!= NULL
);
10409 ecb
= dtrace_ecb_add(state
, probe
);
10410 ecb
->dte_uarg
= desc
->dted_uarg
;
10412 if ((pred
= desc
->dted_pred
.dtpdd_predicate
) != NULL
) {
10413 dtrace_predicate_hold(pred
);
10414 ecb
->dte_predicate
= pred
;
10417 if (probe
!= NULL
) {
10419 * If the provider shows more leg than the consumer is old
10420 * enough to see, we need to enable the appropriate implicit
10421 * predicate bits to prevent the ecb from activating at
10424 * Providers specifying DTRACE_PRIV_USER at register time
10425 * are stating that they need the /proc-style privilege
10426 * model to be enforced, and this is what DTRACE_COND_OWNER
10427 * and DTRACE_COND_ZONEOWNER will then do at probe time.
10429 prov
= probe
->dtpr_provider
;
10430 if (!(state
->dts_cred
.dcr_visible
& DTRACE_CRV_ALLPROC
) &&
10431 (prov
->dtpv_priv
.dtpp_flags
& DTRACE_PRIV_USER
))
10432 ecb
->dte_cond
|= DTRACE_COND_OWNER
;
10434 if (!(state
->dts_cred
.dcr_visible
& DTRACE_CRV_ALLZONE
) &&
10435 (prov
->dtpv_priv
.dtpp_flags
& DTRACE_PRIV_USER
))
10436 ecb
->dte_cond
|= DTRACE_COND_ZONEOWNER
;
10439 * If the provider shows us kernel innards and the user
10440 * is lacking sufficient privilege, enable the
10441 * DTRACE_COND_USERMODE implicit predicate.
10443 if (!(state
->dts_cred
.dcr_visible
& DTRACE_CRV_KERNEL
) &&
10444 (prov
->dtpv_priv
.dtpp_flags
& DTRACE_PRIV_KERNEL
))
10445 ecb
->dte_cond
|= DTRACE_COND_USERMODE
;
10448 if (dtrace_ecb_create_cache
!= NULL
) {
10450 * If we have a cached ecb, we'll use its action list instead
10451 * of creating our own (saving both time and space).
10453 dtrace_ecb_t
*cached
= dtrace_ecb_create_cache
;
10454 dtrace_action_t
*act_if
= cached
->dte_action
;
10456 if (act_if
!= NULL
) {
10457 ASSERT(act_if
->dta_refcnt
> 0);
10458 act_if
->dta_refcnt
++;
10459 ecb
->dte_action
= act_if
;
10460 ecb
->dte_action_last
= cached
->dte_action_last
;
10461 ecb
->dte_needed
= cached
->dte_needed
;
10462 ecb
->dte_size
= cached
->dte_size
;
10463 ecb
->dte_alignment
= cached
->dte_alignment
;
10469 for (act
= desc
->dted_action
; act
!= NULL
; act
= act
->dtad_next
) {
10470 if ((enab
->dten_error
= dtrace_ecb_action_add(ecb
, act
)) != 0) {
10471 dtrace_ecb_destroy(ecb
);
10476 dtrace_ecb_resize(ecb
);
10478 return (dtrace_ecb_create_cache
= ecb
);
10482 dtrace_ecb_create_enable(dtrace_probe_t
*probe
, void *arg
)
10485 dtrace_enabling_t
*enab
= arg
;
10486 dtrace_state_t
*state
= enab
->dten_vstate
->dtvs_state
;
10488 ASSERT(state
!= NULL
);
10490 if (probe
!= NULL
&& probe
->dtpr_gen
< enab
->dten_probegen
) {
10492 * This probe was created in a generation for which this
10493 * enabling has previously created ECBs; we don't want to
10494 * enable it again, so just kick out.
10496 return (DTRACE_MATCH_NEXT
);
10499 if ((ecb
= dtrace_ecb_create(state
, probe
, enab
)) == NULL
)
10500 return (DTRACE_MATCH_DONE
);
10502 if (dtrace_ecb_enable(ecb
) < 0)
10503 return (DTRACE_MATCH_FAIL
);
10505 return (DTRACE_MATCH_NEXT
);
10508 static dtrace_ecb_t
*
10509 dtrace_epid2ecb(dtrace_state_t
*state
, dtrace_epid_t id
)
10512 #pragma unused(ecb) /* __APPLE__ */
10514 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
10516 if (id
== 0 || id
> (dtrace_epid_t
)state
->dts_necbs
)
10519 ASSERT(state
->dts_necbs
> 0 && state
->dts_ecbs
!= NULL
);
10520 ASSERT((ecb
= state
->dts_ecbs
[id
- 1]) == NULL
|| ecb
->dte_epid
== id
);
10522 return (state
->dts_ecbs
[id
- 1]);
10525 static dtrace_aggregation_t
*
10526 dtrace_aggid2agg(dtrace_state_t
*state
, dtrace_aggid_t id
)
10528 dtrace_aggregation_t
*agg
;
10529 #pragma unused(agg) /* __APPLE__ */
10531 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
10533 if (id
== 0 || id
> (dtrace_aggid_t
)state
->dts_naggregations
)
10536 ASSERT(state
->dts_naggregations
> 0 && state
->dts_aggregations
!= NULL
);
10537 ASSERT((agg
= state
->dts_aggregations
[id
- 1]) == NULL
||
10538 agg
->dtag_id
== id
);
10540 return (state
->dts_aggregations
[id
- 1]);
10544 * DTrace Buffer Functions
10546 * The following functions manipulate DTrace buffers. Most of these functions
10547 * are called in the context of establishing or processing consumer state;
10548 * exceptions are explicitly noted.
10552 * Note: called from cross call context. This function switches the two
10553 * buffers on a given CPU. The atomicity of this operation is assured by
10554 * disabling interrupts while the actual switch takes place; the disabling of
10555 * interrupts serializes the execution with any execution of dtrace_probe() on
10559 dtrace_buffer_switch(dtrace_buffer_t
*buf
)
10561 caddr_t tomax
= buf
->dtb_tomax
;
10562 caddr_t xamot
= buf
->dtb_xamot
;
10563 dtrace_icookie_t cookie
;
10566 ASSERT(!(buf
->dtb_flags
& DTRACEBUF_NOSWITCH
));
10567 ASSERT(!(buf
->dtb_flags
& DTRACEBUF_RING
));
10569 cookie
= dtrace_interrupt_disable();
10570 now
= dtrace_gethrtime();
10571 buf
->dtb_tomax
= xamot
;
10572 buf
->dtb_xamot
= tomax
;
10573 buf
->dtb_xamot_drops
= buf
->dtb_drops
;
10574 buf
->dtb_xamot_offset
= buf
->dtb_offset
;
10575 buf
->dtb_xamot_errors
= buf
->dtb_errors
;
10576 buf
->dtb_xamot_flags
= buf
->dtb_flags
;
10577 buf
->dtb_offset
= 0;
10578 buf
->dtb_drops
= 0;
10579 buf
->dtb_errors
= 0;
10580 buf
->dtb_flags
&= ~(DTRACEBUF_ERROR
| DTRACEBUF_DROPPED
);
10581 buf
->dtb_interval
= now
- buf
->dtb_switched
;
10582 buf
->dtb_switched
= now
;
10583 dtrace_interrupt_enable(cookie
);
10587 * Note: called from cross call context. This function activates a buffer
10588 * on a CPU. As with dtrace_buffer_switch(), the atomicity of the operation
10589 * is guaranteed by the disabling of interrupts.
10592 dtrace_buffer_activate(dtrace_state_t
*state
)
10594 dtrace_buffer_t
*buf
;
10595 dtrace_icookie_t cookie
= dtrace_interrupt_disable();
10597 buf
= &state
->dts_buffer
[CPU
->cpu_id
];
10599 if (buf
->dtb_tomax
!= NULL
) {
10601 * We might like to assert that the buffer is marked inactive,
10602 * but this isn't necessarily true: the buffer for the CPU
10603 * that processes the BEGIN probe has its buffer activated
10604 * manually. In this case, we take the (harmless) action
10605 * re-clearing the bit INACTIVE bit.
10607 buf
->dtb_flags
&= ~DTRACEBUF_INACTIVE
;
10610 dtrace_interrupt_enable(cookie
);
10614 dtrace_buffer_canalloc(size_t size
)
10616 if (size
> (UINT64_MAX
- dtrace_buffer_memory_inuse
))
10618 if ((size
+ dtrace_buffer_memory_inuse
) > dtrace_buffer_memory_maxsize
)
10625 dtrace_buffer_alloc(dtrace_buffer_t
*bufs
, size_t size
, int flags
,
10629 dtrace_buffer_t
*buf
;
10630 size_t size_before_alloc
= dtrace_buffer_memory_inuse
;
10632 lck_mtx_assert(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
10633 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
10635 if (size
> (size_t)dtrace_nonroot_maxsize
&&
10636 !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL
, B_FALSE
))
10642 if (cpu
!= DTRACE_CPUALL
&& cpu
!= cp
->cpu_id
)
10645 buf
= &bufs
[cp
->cpu_id
];
10648 * If there is already a buffer allocated for this CPU, it
10649 * is only possible that this is a DR event. In this case,
10650 * the buffer size must match our specified size.
10652 if (buf
->dtb_tomax
!= NULL
) {
10653 ASSERT(buf
->dtb_size
== size
);
10657 ASSERT(buf
->dtb_xamot
== NULL
);
10659 /* DTrace, please do not eat all the memory. */
10660 if (dtrace_buffer_canalloc(size
) == B_FALSE
)
10662 if ((buf
->dtb_tomax
= kmem_zalloc(size
, KM_NOSLEEP
)) == NULL
)
10664 dtrace_buffer_memory_inuse
+= size
;
10666 buf
->dtb_size
= size
;
10667 buf
->dtb_flags
= flags
;
10668 buf
->dtb_offset
= 0;
10669 buf
->dtb_drops
= 0;
10671 if (flags
& DTRACEBUF_NOSWITCH
)
10674 /* DTrace, please do not eat all the memory. */
10675 if (dtrace_buffer_canalloc(size
) == B_FALSE
)
10677 if ((buf
->dtb_xamot
= kmem_zalloc(size
, KM_NOSLEEP
)) == NULL
)
10679 dtrace_buffer_memory_inuse
+= size
;
10680 } while ((cp
= cp
->cpu_next
) != cpu_list
);
10682 ASSERT(dtrace_buffer_memory_inuse
<= dtrace_buffer_memory_maxsize
);
10690 if (cpu
!= DTRACE_CPUALL
&& cpu
!= cp
->cpu_id
)
10693 buf
= &bufs
[cp
->cpu_id
];
10695 if (buf
->dtb_xamot
!= NULL
) {
10696 ASSERT(buf
->dtb_tomax
!= NULL
);
10697 ASSERT(buf
->dtb_size
== size
);
10698 kmem_free(buf
->dtb_xamot
, size
);
10701 if (buf
->dtb_tomax
!= NULL
) {
10702 ASSERT(buf
->dtb_size
== size
);
10703 kmem_free(buf
->dtb_tomax
, size
);
10706 buf
->dtb_tomax
= NULL
;
10707 buf
->dtb_xamot
= NULL
;
10709 } while ((cp
= cp
->cpu_next
) != cpu_list
);
10711 /* Restore the size saved before allocating memory */
10712 dtrace_buffer_memory_inuse
= size_before_alloc
;
10718 * Note: called from probe context. This function just increments the drop
10719 * count on a buffer. It has been made a function to allow for the
10720 * possibility of understanding the source of mysterious drop counts. (A
10721 * problem for which one may be particularly disappointed that DTrace cannot
10722 * be used to understand DTrace.)
10725 dtrace_buffer_drop(dtrace_buffer_t
*buf
)
10731 * Note: called from probe context. This function is called to reserve space
10732 * in a buffer. If mstate is non-NULL, sets the scratch base and size in the
10733 * mstate. Returns the new offset in the buffer, or a negative value if an
10734 * error has occurred.
10737 dtrace_buffer_reserve(dtrace_buffer_t
*buf
, size_t needed
, size_t align
,
10738 dtrace_state_t
*state
, dtrace_mstate_t
*mstate
)
10740 intptr_t offs
= buf
->dtb_offset
, soffs
;
10745 if (buf
->dtb_flags
& DTRACEBUF_INACTIVE
)
10748 if ((tomax
= buf
->dtb_tomax
) == NULL
) {
10749 dtrace_buffer_drop(buf
);
10753 if (!(buf
->dtb_flags
& (DTRACEBUF_RING
| DTRACEBUF_FILL
))) {
10754 while (offs
& (align
- 1)) {
10756 * Assert that our alignment is off by a number which
10757 * is itself sizeof (uint32_t) aligned.
10759 ASSERT(!((align
- (offs
& (align
- 1))) &
10760 (sizeof (uint32_t) - 1)));
10761 DTRACE_STORE(uint32_t, tomax
, offs
, DTRACE_EPIDNONE
);
10762 offs
+= sizeof (uint32_t);
10765 if ((uint64_t)(soffs
= offs
+ needed
) > buf
->dtb_size
) {
10766 dtrace_buffer_drop(buf
);
10770 if (mstate
== NULL
)
10773 mstate
->dtms_scratch_base
= (uintptr_t)tomax
+ soffs
;
10774 mstate
->dtms_scratch_size
= buf
->dtb_size
- soffs
;
10775 mstate
->dtms_scratch_ptr
= mstate
->dtms_scratch_base
;
10780 if (buf
->dtb_flags
& DTRACEBUF_FILL
) {
10781 if (state
->dts_activity
!= DTRACE_ACTIVITY_COOLDOWN
&&
10782 (buf
->dtb_flags
& DTRACEBUF_FULL
))
10787 total_off
= needed
+ (offs
& (align
- 1));
10790 * For a ring buffer, life is quite a bit more complicated. Before
10791 * we can store any padding, we need to adjust our wrapping offset.
10792 * (If we've never before wrapped or we're not about to, no adjustment
10795 if ((buf
->dtb_flags
& DTRACEBUF_WRAPPED
) ||
10796 offs
+ total_off
> buf
->dtb_size
) {
10797 woffs
= buf
->dtb_xamot_offset
;
10799 if (offs
+ total_off
> buf
->dtb_size
) {
10801 * We can't fit in the end of the buffer. First, a
10802 * sanity check that we can fit in the buffer at all.
10804 if (total_off
> buf
->dtb_size
) {
10805 dtrace_buffer_drop(buf
);
10810 * We're going to be storing at the top of the buffer,
10811 * so now we need to deal with the wrapped offset. We
10812 * only reset our wrapped offset to 0 if it is
10813 * currently greater than the current offset. If it
10814 * is less than the current offset, it is because a
10815 * previous allocation induced a wrap -- but the
10816 * allocation didn't subsequently take the space due
10817 * to an error or false predicate evaluation. In this
10818 * case, we'll just leave the wrapped offset alone: if
10819 * the wrapped offset hasn't been advanced far enough
10820 * for this allocation, it will be adjusted in the
10823 if (buf
->dtb_flags
& DTRACEBUF_WRAPPED
) {
10831 * Now we know that we're going to be storing to the
10832 * top of the buffer and that there is room for us
10833 * there. We need to clear the buffer from the current
10834 * offset to the end (there may be old gunk there).
10836 while ((uint64_t)offs
< buf
->dtb_size
)
10840 * We need to set our offset to zero. And because we
10841 * are wrapping, we need to set the bit indicating as
10842 * much. We can also adjust our needed space back
10843 * down to the space required by the ECB -- we know
10844 * that the top of the buffer is aligned.
10847 total_off
= needed
;
10848 buf
->dtb_flags
|= DTRACEBUF_WRAPPED
;
10851 * There is room for us in the buffer, so we simply
10852 * need to check the wrapped offset.
10854 if (woffs
< offs
) {
10856 * The wrapped offset is less than the offset.
10857 * This can happen if we allocated buffer space
10858 * that induced a wrap, but then we didn't
10859 * subsequently take the space due to an error
10860 * or false predicate evaluation. This is
10861 * okay; we know that _this_ allocation isn't
10862 * going to induce a wrap. We still can't
10863 * reset the wrapped offset to be zero,
10864 * however: the space may have been trashed in
10865 * the previous failed probe attempt. But at
10866 * least the wrapped offset doesn't need to
10867 * be adjusted at all...
10873 while (offs
+ total_off
> (size_t)woffs
) {
10874 dtrace_epid_t epid
= *(uint32_t *)(tomax
+ woffs
);
10877 if (epid
== DTRACE_EPIDNONE
) {
10878 size
= sizeof (uint32_t);
10880 ASSERT(epid
<= (dtrace_epid_t
)state
->dts_necbs
);
10881 ASSERT(state
->dts_ecbs
[epid
- 1] != NULL
);
10883 size
= state
->dts_ecbs
[epid
- 1]->dte_size
;
10886 ASSERT(woffs
+ size
<= buf
->dtb_size
);
10889 if (woffs
+ size
== buf
->dtb_size
) {
10891 * We've reached the end of the buffer; we want
10892 * to set the wrapped offset to 0 and break
10893 * out. However, if the offs is 0, then we're
10894 * in a strange edge-condition: the amount of
10895 * space that we want to reserve plus the size
10896 * of the record that we're overwriting is
10897 * greater than the size of the buffer. This
10898 * is problematic because if we reserve the
10899 * space but subsequently don't consume it (due
10900 * to a failed predicate or error) the wrapped
10901 * offset will be 0 -- yet the EPID at offset 0
10902 * will not be committed. This situation is
10903 * relatively easy to deal with: if we're in
10904 * this case, the buffer is indistinguishable
10905 * from one that hasn't wrapped; we need only
10906 * finish the job by clearing the wrapped bit,
10907 * explicitly setting the offset to be 0, and
10908 * zero'ing out the old data in the buffer.
10911 buf
->dtb_flags
&= ~DTRACEBUF_WRAPPED
;
10912 buf
->dtb_offset
= 0;
10915 while ((uint64_t)woffs
< buf
->dtb_size
)
10916 tomax
[woffs
++] = 0;
10927 * We have a wrapped offset. It may be that the wrapped offset
10928 * has become zero -- that's okay.
10930 buf
->dtb_xamot_offset
= woffs
;
10935 * Now we can plow the buffer with any necessary padding.
10937 while (offs
& (align
- 1)) {
10939 * Assert that our alignment is off by a number which
10940 * is itself sizeof (uint32_t) aligned.
10942 ASSERT(!((align
- (offs
& (align
- 1))) &
10943 (sizeof (uint32_t) - 1)));
10944 DTRACE_STORE(uint32_t, tomax
, offs
, DTRACE_EPIDNONE
);
10945 offs
+= sizeof (uint32_t);
10948 if (buf
->dtb_flags
& DTRACEBUF_FILL
) {
10949 if (offs
+ needed
> buf
->dtb_size
- state
->dts_reserve
) {
10950 buf
->dtb_flags
|= DTRACEBUF_FULL
;
10955 if (mstate
== NULL
)
10959 * For ring buffers and fill buffers, the scratch space is always
10960 * the inactive buffer.
10962 mstate
->dtms_scratch_base
= (uintptr_t)buf
->dtb_xamot
;
10963 mstate
->dtms_scratch_size
= buf
->dtb_size
;
10964 mstate
->dtms_scratch_ptr
= mstate
->dtms_scratch_base
;
10970 dtrace_buffer_polish(dtrace_buffer_t
*buf
)
10972 ASSERT(buf
->dtb_flags
& DTRACEBUF_RING
);
10973 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
10975 if (!(buf
->dtb_flags
& DTRACEBUF_WRAPPED
))
10979 * We need to polish the ring buffer. There are three cases:
10981 * - The first (and presumably most common) is that there is no gap
10982 * between the buffer offset and the wrapped offset. In this case,
10983 * there is nothing in the buffer that isn't valid data; we can
10984 * mark the buffer as polished and return.
10986 * - The second (less common than the first but still more common
10987 * than the third) is that there is a gap between the buffer offset
10988 * and the wrapped offset, and the wrapped offset is larger than the
10989 * buffer offset. This can happen because of an alignment issue, or
10990 * can happen because of a call to dtrace_buffer_reserve() that
10991 * didn't subsequently consume the buffer space. In this case,
10992 * we need to zero the data from the buffer offset to the wrapped
10995 * - The third (and least common) is that there is a gap between the
10996 * buffer offset and the wrapped offset, but the wrapped offset is
10997 * _less_ than the buffer offset. This can only happen because a
10998 * call to dtrace_buffer_reserve() induced a wrap, but the space
10999 * was not subsequently consumed. In this case, we need to zero the
11000 * space from the offset to the end of the buffer _and_ from the
11001 * top of the buffer to the wrapped offset.
11003 if (buf
->dtb_offset
< buf
->dtb_xamot_offset
) {
11004 bzero(buf
->dtb_tomax
+ buf
->dtb_offset
,
11005 buf
->dtb_xamot_offset
- buf
->dtb_offset
);
11008 if (buf
->dtb_offset
> buf
->dtb_xamot_offset
) {
11009 bzero(buf
->dtb_tomax
+ buf
->dtb_offset
,
11010 buf
->dtb_size
- buf
->dtb_offset
);
11011 bzero(buf
->dtb_tomax
, buf
->dtb_xamot_offset
);
11016 dtrace_buffer_free(dtrace_buffer_t
*bufs
)
11020 for (i
= 0; i
< (int)NCPU
; i
++) {
11021 dtrace_buffer_t
*buf
= &bufs
[i
];
11023 if (buf
->dtb_tomax
== NULL
) {
11024 ASSERT(buf
->dtb_xamot
== NULL
);
11025 ASSERT(buf
->dtb_size
== 0);
11029 if (buf
->dtb_xamot
!= NULL
) {
11030 ASSERT(!(buf
->dtb_flags
& DTRACEBUF_NOSWITCH
));
11031 kmem_free(buf
->dtb_xamot
, buf
->dtb_size
);
11033 ASSERT(dtrace_buffer_memory_inuse
>= buf
->dtb_size
);
11034 dtrace_buffer_memory_inuse
-= buf
->dtb_size
;
11037 kmem_free(buf
->dtb_tomax
, buf
->dtb_size
);
11038 ASSERT(dtrace_buffer_memory_inuse
>= buf
->dtb_size
);
11039 dtrace_buffer_memory_inuse
-= buf
->dtb_size
;
11042 buf
->dtb_tomax
= NULL
;
11043 buf
->dtb_xamot
= NULL
;
11048 * DTrace Enabling Functions
11050 static dtrace_enabling_t
*
11051 dtrace_enabling_create(dtrace_vstate_t
*vstate
)
11053 dtrace_enabling_t
*enab
;
11055 enab
= kmem_zalloc(sizeof (dtrace_enabling_t
), KM_SLEEP
);
11056 enab
->dten_vstate
= vstate
;
11062 dtrace_enabling_add(dtrace_enabling_t
*enab
, dtrace_ecbdesc_t
*ecb
)
11064 dtrace_ecbdesc_t
**ndesc
;
11065 size_t osize
, nsize
;
11068 * We can't add to enablings after we've enabled them, or after we've
11071 ASSERT(enab
->dten_probegen
== 0);
11072 ASSERT(enab
->dten_next
== NULL
&& enab
->dten_prev
== NULL
);
11074 /* APPLE NOTE: this protects against gcc 4.0 botch on x86 */
11075 if (ecb
== NULL
) return;
11077 if (enab
->dten_ndesc
< enab
->dten_maxdesc
) {
11078 enab
->dten_desc
[enab
->dten_ndesc
++] = ecb
;
11082 osize
= enab
->dten_maxdesc
* sizeof (dtrace_enabling_t
*);
11084 if (enab
->dten_maxdesc
== 0) {
11085 enab
->dten_maxdesc
= 1;
11087 enab
->dten_maxdesc
<<= 1;
11090 ASSERT(enab
->dten_ndesc
< enab
->dten_maxdesc
);
11092 nsize
= enab
->dten_maxdesc
* sizeof (dtrace_enabling_t
*);
11093 ndesc
= kmem_zalloc(nsize
, KM_SLEEP
);
11094 bcopy(enab
->dten_desc
, ndesc
, osize
);
11095 kmem_free(enab
->dten_desc
, osize
);
11097 enab
->dten_desc
= ndesc
;
11098 enab
->dten_desc
[enab
->dten_ndesc
++] = ecb
;
11102 dtrace_enabling_addlike(dtrace_enabling_t
*enab
, dtrace_ecbdesc_t
*ecb
,
11103 dtrace_probedesc_t
*pd
)
11105 dtrace_ecbdesc_t
*new;
11106 dtrace_predicate_t
*pred
;
11107 dtrace_actdesc_t
*act
;
11110 * We're going to create a new ECB description that matches the
11111 * specified ECB in every way, but has the specified probe description.
11113 new = kmem_zalloc(sizeof (dtrace_ecbdesc_t
), KM_SLEEP
);
11115 if ((pred
= ecb
->dted_pred
.dtpdd_predicate
) != NULL
)
11116 dtrace_predicate_hold(pred
);
11118 for (act
= ecb
->dted_action
; act
!= NULL
; act
= act
->dtad_next
)
11119 dtrace_actdesc_hold(act
);
11121 new->dted_action
= ecb
->dted_action
;
11122 new->dted_pred
= ecb
->dted_pred
;
11123 new->dted_probe
= *pd
;
11124 new->dted_uarg
= ecb
->dted_uarg
;
11126 dtrace_enabling_add(enab
, new);
11130 dtrace_enabling_dump(dtrace_enabling_t
*enab
)
11134 for (i
= 0; i
< enab
->dten_ndesc
; i
++) {
11135 dtrace_probedesc_t
*desc
= &enab
->dten_desc
[i
]->dted_probe
;
11137 cmn_err(CE_NOTE
, "enabling probe %d (%s:%s:%s:%s)", i
,
11138 desc
->dtpd_provider
, desc
->dtpd_mod
,
11139 desc
->dtpd_func
, desc
->dtpd_name
);
11144 dtrace_enabling_destroy(dtrace_enabling_t
*enab
)
11147 dtrace_ecbdesc_t
*ep
;
11148 dtrace_vstate_t
*vstate
= enab
->dten_vstate
;
11150 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
11152 for (i
= 0; i
< enab
->dten_ndesc
; i
++) {
11153 dtrace_actdesc_t
*act
, *next
;
11154 dtrace_predicate_t
*pred
;
11156 ep
= enab
->dten_desc
[i
];
11158 if ((pred
= ep
->dted_pred
.dtpdd_predicate
) != NULL
)
11159 dtrace_predicate_release(pred
, vstate
);
11161 for (act
= ep
->dted_action
; act
!= NULL
; act
= next
) {
11162 next
= act
->dtad_next
;
11163 dtrace_actdesc_release(act
, vstate
);
11166 kmem_free(ep
, sizeof (dtrace_ecbdesc_t
));
11169 kmem_free(enab
->dten_desc
,
11170 enab
->dten_maxdesc
* sizeof (dtrace_enabling_t
*));
11173 * If this was a retained enabling, decrement the dts_nretained count
11174 * and take it off of the dtrace_retained list.
11176 if (enab
->dten_prev
!= NULL
|| enab
->dten_next
!= NULL
||
11177 dtrace_retained
== enab
) {
11178 ASSERT(enab
->dten_vstate
->dtvs_state
!= NULL
);
11179 ASSERT(enab
->dten_vstate
->dtvs_state
->dts_nretained
> 0);
11180 enab
->dten_vstate
->dtvs_state
->dts_nretained
--;
11181 dtrace_retained_gen
++;
11184 if (enab
->dten_prev
== NULL
) {
11185 if (dtrace_retained
== enab
) {
11186 dtrace_retained
= enab
->dten_next
;
11188 if (dtrace_retained
!= NULL
)
11189 dtrace_retained
->dten_prev
= NULL
;
11192 ASSERT(enab
!= dtrace_retained
);
11193 ASSERT(dtrace_retained
!= NULL
);
11194 enab
->dten_prev
->dten_next
= enab
->dten_next
;
11197 if (enab
->dten_next
!= NULL
) {
11198 ASSERT(dtrace_retained
!= NULL
);
11199 enab
->dten_next
->dten_prev
= enab
->dten_prev
;
11202 kmem_free(enab
, sizeof (dtrace_enabling_t
));
11206 dtrace_enabling_retain(dtrace_enabling_t
*enab
)
11208 dtrace_state_t
*state
;
11210 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
11211 ASSERT(enab
->dten_next
== NULL
&& enab
->dten_prev
== NULL
);
11212 ASSERT(enab
->dten_vstate
!= NULL
);
11214 state
= enab
->dten_vstate
->dtvs_state
;
11215 ASSERT(state
!= NULL
);
11218 * We only allow each state to retain dtrace_retain_max enablings.
11220 if (state
->dts_nretained
>= dtrace_retain_max
)
11223 state
->dts_nretained
++;
11224 dtrace_retained_gen
++;
11226 if (dtrace_retained
== NULL
) {
11227 dtrace_retained
= enab
;
11231 enab
->dten_next
= dtrace_retained
;
11232 dtrace_retained
->dten_prev
= enab
;
11233 dtrace_retained
= enab
;
11239 dtrace_enabling_replicate(dtrace_state_t
*state
, dtrace_probedesc_t
*match
,
11240 dtrace_probedesc_t
*create
)
11242 dtrace_enabling_t
*new, *enab
;
11243 int found
= 0, err
= ENOENT
;
11245 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
11246 ASSERT(strlen(match
->dtpd_provider
) < DTRACE_PROVNAMELEN
);
11247 ASSERT(strlen(match
->dtpd_mod
) < DTRACE_MODNAMELEN
);
11248 ASSERT(strlen(match
->dtpd_func
) < DTRACE_FUNCNAMELEN
);
11249 ASSERT(strlen(match
->dtpd_name
) < DTRACE_NAMELEN
);
11251 new = dtrace_enabling_create(&state
->dts_vstate
);
11254 * Iterate over all retained enablings, looking for enablings that
11255 * match the specified state.
11257 for (enab
= dtrace_retained
; enab
!= NULL
; enab
= enab
->dten_next
) {
11261 * dtvs_state can only be NULL for helper enablings -- and
11262 * helper enablings can't be retained.
11264 ASSERT(enab
->dten_vstate
->dtvs_state
!= NULL
);
11266 if (enab
->dten_vstate
->dtvs_state
!= state
)
11270 * Now iterate over each probe description; we're looking for
11271 * an exact match to the specified probe description.
11273 for (i
= 0; i
< enab
->dten_ndesc
; i
++) {
11274 dtrace_ecbdesc_t
*ep
= enab
->dten_desc
[i
];
11275 dtrace_probedesc_t
*pd
= &ep
->dted_probe
;
11277 /* APPLE NOTE: Darwin employs size bounded string operation. */
11278 if (strncmp(pd
->dtpd_provider
, match
->dtpd_provider
, DTRACE_PROVNAMELEN
))
11281 if (strncmp(pd
->dtpd_mod
, match
->dtpd_mod
, DTRACE_MODNAMELEN
))
11284 if (strncmp(pd
->dtpd_func
, match
->dtpd_func
, DTRACE_FUNCNAMELEN
))
11287 if (strncmp(pd
->dtpd_name
, match
->dtpd_name
, DTRACE_NAMELEN
))
11291 * We have a winning probe! Add it to our growing
11295 dtrace_enabling_addlike(new, ep
, create
);
11299 if (!found
|| (err
= dtrace_enabling_retain(new)) != 0) {
11300 dtrace_enabling_destroy(new);
11308 dtrace_enabling_retract(dtrace_state_t
*state
)
11310 dtrace_enabling_t
*enab
, *next
;
11312 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
11315 * Iterate over all retained enablings, destroy the enablings retained
11316 * for the specified state.
11318 for (enab
= dtrace_retained
; enab
!= NULL
; enab
= next
) {
11319 next
= enab
->dten_next
;
11322 * dtvs_state can only be NULL for helper enablings -- and
11323 * helper enablings can't be retained.
11325 ASSERT(enab
->dten_vstate
->dtvs_state
!= NULL
);
11327 if (enab
->dten_vstate
->dtvs_state
== state
) {
11328 ASSERT(state
->dts_nretained
> 0);
11329 dtrace_enabling_destroy(enab
);
11333 ASSERT(state
->dts_nretained
== 0);
11337 dtrace_enabling_match(dtrace_enabling_t
*enab
, int *nmatched
)
11340 int total_matched
= 0, matched
= 0;
11342 lck_mtx_assert(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
11343 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
11345 for (i
= 0; i
< enab
->dten_ndesc
; i
++) {
11346 dtrace_ecbdesc_t
*ep
= enab
->dten_desc
[i
];
11348 enab
->dten_current
= ep
;
11349 enab
->dten_error
= 0;
11352 * If a provider failed to enable a probe then get out and
11353 * let the consumer know we failed.
11355 if ((matched
= dtrace_probe_enable(&ep
->dted_probe
, enab
)) < 0)
11358 total_matched
+= matched
;
11360 if (enab
->dten_error
!= 0) {
11362 * If we get an error half-way through enabling the
11363 * probes, we kick out -- perhaps with some number of
11364 * them enabled. Leaving enabled probes enabled may
11365 * be slightly confusing for user-level, but we expect
11366 * that no one will attempt to actually drive on in
11367 * the face of such errors. If this is an anonymous
11368 * enabling (indicated with a NULL nmatched pointer),
11369 * we cmn_err() a message. We aren't expecting to
11370 * get such an error -- such as it can exist at all,
11371 * it would be a result of corrupted DOF in the driver
11374 if (nmatched
== NULL
) {
11375 cmn_err(CE_WARN
, "dtrace_enabling_match() "
11376 "error on %p: %d", (void *)ep
,
11380 return (enab
->dten_error
);
11384 enab
->dten_probegen
= dtrace_probegen
;
11385 if (nmatched
!= NULL
)
11386 *nmatched
= total_matched
;
11392 dtrace_enabling_matchall(void)
11394 dtrace_enabling_t
*enab
;
11396 lck_mtx_lock(&cpu_lock
);
11397 lck_mtx_lock(&dtrace_lock
);
11400 * Iterate over all retained enablings to see if any probes match
11401 * against them. We only perform this operation on enablings for which
11402 * we have sufficient permissions by virtue of being in the global zone
11403 * or in the same zone as the DTrace client. Because we can be called
11404 * after dtrace_detach() has been called, we cannot assert that there
11405 * are retained enablings. We can safely load from dtrace_retained,
11406 * however: the taskq_destroy() at the end of dtrace_detach() will
11407 * block pending our completion.
11411 * Darwin doesn't do zones.
11412 * Behave as if always in "global" zone."
11414 for (enab
= dtrace_retained
; enab
!= NULL
; enab
= enab
->dten_next
) {
11415 (void) dtrace_enabling_match(enab
, NULL
);
11418 lck_mtx_unlock(&dtrace_lock
);
11419 lck_mtx_unlock(&cpu_lock
);
11423 * If an enabling is to be enabled without having matched probes (that is, if
11424 * dtrace_state_go() is to be called on the underlying dtrace_state_t), the
11425 * enabling must be _primed_ by creating an ECB for every ECB description.
11426 * This must be done to assure that we know the number of speculations, the
11427 * number of aggregations, the minimum buffer size needed, etc. before we
11428 * transition out of DTRACE_ACTIVITY_INACTIVE. To do this without actually
11429 * enabling any probes, we create ECBs for every ECB decription, but with a
11430 * NULL probe -- which is exactly what this function does.
11433 dtrace_enabling_prime(dtrace_state_t
*state
)
11435 dtrace_enabling_t
*enab
;
11438 for (enab
= dtrace_retained
; enab
!= NULL
; enab
= enab
->dten_next
) {
11439 ASSERT(enab
->dten_vstate
->dtvs_state
!= NULL
);
11441 if (enab
->dten_vstate
->dtvs_state
!= state
)
11445 * We don't want to prime an enabling more than once, lest
11446 * we allow a malicious user to induce resource exhaustion.
11447 * (The ECBs that result from priming an enabling aren't
11448 * leaked -- but they also aren't deallocated until the
11449 * consumer state is destroyed.)
11451 if (enab
->dten_primed
)
11454 for (i
= 0; i
< enab
->dten_ndesc
; i
++) {
11455 enab
->dten_current
= enab
->dten_desc
[i
];
11456 (void) dtrace_probe_enable(NULL
, enab
);
11459 enab
->dten_primed
= 1;
11464 * Called to indicate that probes should be provided due to retained
11465 * enablings. This is implemented in terms of dtrace_probe_provide(), but it
11466 * must take an initial lap through the enabling calling the dtps_provide()
11467 * entry point explicitly to allow for autocreated probes.
11470 dtrace_enabling_provide(dtrace_provider_t
*prv
)
11473 dtrace_probedesc_t desc
;
11474 dtrace_genid_t gen
;
11476 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
11477 lck_mtx_assert(&dtrace_provider_lock
, LCK_MTX_ASSERT_OWNED
);
11481 prv
= dtrace_provider
;
11485 dtrace_enabling_t
*enab
;
11486 void *parg
= prv
->dtpv_arg
;
11489 gen
= dtrace_retained_gen
;
11490 for (enab
= dtrace_retained
; enab
!= NULL
;
11491 enab
= enab
->dten_next
) {
11492 for (i
= 0; i
< enab
->dten_ndesc
; i
++) {
11493 desc
= enab
->dten_desc
[i
]->dted_probe
;
11494 lck_mtx_unlock(&dtrace_lock
);
11495 prv
->dtpv_pops
.dtps_provide(parg
, &desc
);
11496 lck_mtx_lock(&dtrace_lock
);
11498 * Process the retained enablings again if
11499 * they have changed while we weren't holding
11502 if (gen
!= dtrace_retained_gen
)
11506 } while (all
&& (prv
= prv
->dtpv_next
) != NULL
);
11508 lck_mtx_unlock(&dtrace_lock
);
11509 dtrace_probe_provide(NULL
, all
? NULL
: prv
);
11510 lck_mtx_lock(&dtrace_lock
);
11514 * DTrace DOF Functions
11518 dtrace_dof_error(dof_hdr_t
*dof
, const char *str
)
11520 #pragma unused(dof) /* __APPLE__ */
11521 if (dtrace_err_verbose
)
11522 cmn_err(CE_WARN
, "failed to process DOF: %s", str
);
11524 #ifdef DTRACE_ERRDEBUG
11525 dtrace_errdebug(str
);
11530 * Create DOF out of a currently enabled state. Right now, we only create
11531 * DOF containing the run-time options -- but this could be expanded to create
11532 * complete DOF representing the enabled state.
11535 dtrace_dof_create(dtrace_state_t
*state
)
11539 dof_optdesc_t
*opt
;
11540 int i
, len
= sizeof (dof_hdr_t
) +
11541 roundup(sizeof (dof_sec_t
), sizeof (uint64_t)) +
11542 sizeof (dof_optdesc_t
) * DTRACEOPT_MAX
;
11544 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
11546 dof
= dt_kmem_zalloc_aligned(len
, 8, KM_SLEEP
);
11547 dof
->dofh_ident
[DOF_ID_MAG0
] = DOF_MAG_MAG0
;
11548 dof
->dofh_ident
[DOF_ID_MAG1
] = DOF_MAG_MAG1
;
11549 dof
->dofh_ident
[DOF_ID_MAG2
] = DOF_MAG_MAG2
;
11550 dof
->dofh_ident
[DOF_ID_MAG3
] = DOF_MAG_MAG3
;
11552 dof
->dofh_ident
[DOF_ID_MODEL
] = DOF_MODEL_NATIVE
;
11553 dof
->dofh_ident
[DOF_ID_ENCODING
] = DOF_ENCODE_NATIVE
;
11554 dof
->dofh_ident
[DOF_ID_VERSION
] = DOF_VERSION
;
11555 dof
->dofh_ident
[DOF_ID_DIFVERS
] = DIF_VERSION
;
11556 dof
->dofh_ident
[DOF_ID_DIFIREG
] = DIF_DIR_NREGS
;
11557 dof
->dofh_ident
[DOF_ID_DIFTREG
] = DIF_DTR_NREGS
;
11559 dof
->dofh_flags
= 0;
11560 dof
->dofh_hdrsize
= sizeof (dof_hdr_t
);
11561 dof
->dofh_secsize
= sizeof (dof_sec_t
);
11562 dof
->dofh_secnum
= 1; /* only DOF_SECT_OPTDESC */
11563 dof
->dofh_secoff
= sizeof (dof_hdr_t
);
11564 dof
->dofh_loadsz
= len
;
11565 dof
->dofh_filesz
= len
;
11569 * Fill in the option section header...
11571 sec
= (dof_sec_t
*)((uintptr_t)dof
+ sizeof (dof_hdr_t
));
11572 sec
->dofs_type
= DOF_SECT_OPTDESC
;
11573 sec
->dofs_align
= sizeof (uint64_t);
11574 sec
->dofs_flags
= DOF_SECF_LOAD
;
11575 sec
->dofs_entsize
= sizeof (dof_optdesc_t
);
11577 opt
= (dof_optdesc_t
*)((uintptr_t)sec
+
11578 roundup(sizeof (dof_sec_t
), sizeof (uint64_t)));
11580 sec
->dofs_offset
= (uintptr_t)opt
- (uintptr_t)dof
;
11581 sec
->dofs_size
= sizeof (dof_optdesc_t
) * DTRACEOPT_MAX
;
11583 for (i
= 0; i
< DTRACEOPT_MAX
; i
++) {
11584 opt
[i
].dofo_option
= i
;
11585 opt
[i
].dofo_strtab
= DOF_SECIDX_NONE
;
11586 opt
[i
].dofo_value
= state
->dts_options
[i
];
11593 dtrace_dof_copyin(user_addr_t uarg
, int *errp
)
11595 dof_hdr_t hdr
, *dof
;
11597 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_NOTOWNED
);
11600 * First, we're going to copyin() the sizeof (dof_hdr_t).
11602 if (copyin(uarg
, &hdr
, sizeof (hdr
)) != 0) {
11603 dtrace_dof_error(NULL
, "failed to copyin DOF header");
11609 * Now we'll allocate the entire DOF and copy it in -- provided
11610 * that the length isn't outrageous.
11612 if (hdr
.dofh_loadsz
>= (uint64_t)dtrace_dof_maxsize
) {
11613 dtrace_dof_error(&hdr
, "load size exceeds maximum");
11618 if (hdr
.dofh_loadsz
< sizeof (hdr
)) {
11619 dtrace_dof_error(&hdr
, "invalid load size");
11624 dof
= dt_kmem_alloc_aligned(hdr
.dofh_loadsz
, 8, KM_SLEEP
);
11626 if (copyin(uarg
, dof
, hdr
.dofh_loadsz
) != 0 ||
11627 dof
->dofh_loadsz
!= hdr
.dofh_loadsz
) {
11628 dt_kmem_free_aligned(dof
, hdr
.dofh_loadsz
);
11637 dtrace_dof_copyin_from_proc(proc_t
* p
, user_addr_t uarg
, int *errp
)
11639 dof_hdr_t hdr
, *dof
;
11641 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_NOTOWNED
);
11644 * First, we're going to copyin() the sizeof (dof_hdr_t).
11646 if (uread(p
, &hdr
, sizeof(hdr
), uarg
) != KERN_SUCCESS
) {
11647 dtrace_dof_error(NULL
, "failed to copyin DOF header");
11653 * Now we'll allocate the entire DOF and copy it in -- provided
11654 * that the length isn't outrageous.
11656 if (hdr
.dofh_loadsz
>= (uint64_t)dtrace_dof_maxsize
) {
11657 dtrace_dof_error(&hdr
, "load size exceeds maximum");
11662 if (hdr
.dofh_loadsz
< sizeof (hdr
)) {
11663 dtrace_dof_error(&hdr
, "invalid load size");
11668 dof
= dt_kmem_alloc_aligned(hdr
.dofh_loadsz
, 8, KM_SLEEP
);
11670 if (uread(p
, dof
, hdr
.dofh_loadsz
, uarg
) != KERN_SUCCESS
) {
11671 dt_kmem_free_aligned(dof
, hdr
.dofh_loadsz
);
11680 dtrace_dof_property(const char *name
)
11684 unsigned int len
, i
;
11688 * Unfortunately, array of values in .conf files are always (and
11689 * only) interpreted to be integer arrays. We must read our DOF
11690 * as an integer array, and then squeeze it into a byte array.
11692 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY
, dtrace_devi
, 0,
11693 name
, (int **)&buf
, &len
) != DDI_PROP_SUCCESS
)
11696 for (i
= 0; i
< len
; i
++)
11697 buf
[i
] = (uchar_t
)(((int *)buf
)[i
]);
11699 if (len
< sizeof (dof_hdr_t
)) {
11700 ddi_prop_free(buf
);
11701 dtrace_dof_error(NULL
, "truncated header");
11705 if (len
< (loadsz
= ((dof_hdr_t
*)buf
)->dofh_loadsz
)) {
11706 ddi_prop_free(buf
);
11707 dtrace_dof_error(NULL
, "truncated DOF");
11711 if (loadsz
>= (uint64_t)dtrace_dof_maxsize
) {
11712 ddi_prop_free(buf
);
11713 dtrace_dof_error(NULL
, "oversized DOF");
11717 dof
= dt_kmem_alloc_aligned(loadsz
, 8, KM_SLEEP
);
11718 bcopy(buf
, dof
, loadsz
);
11719 ddi_prop_free(buf
);
11725 dtrace_dof_destroy(dof_hdr_t
*dof
)
11727 dt_kmem_free_aligned(dof
, dof
->dofh_loadsz
);
11731 * Return the dof_sec_t pointer corresponding to a given section index. If the
11732 * index is not valid, dtrace_dof_error() is called and NULL is returned. If
11733 * a type other than DOF_SECT_NONE is specified, the header is checked against
11734 * this type and NULL is returned if the types do not match.
11737 dtrace_dof_sect(dof_hdr_t
*dof
, uint32_t type
, dof_secidx_t i
)
11739 dof_sec_t
*sec
= (dof_sec_t
*)(uintptr_t)
11740 ((uintptr_t)dof
+ dof
->dofh_secoff
+ i
* dof
->dofh_secsize
);
11742 if (i
>= dof
->dofh_secnum
) {
11743 dtrace_dof_error(dof
, "referenced section index is invalid");
11747 if (!(sec
->dofs_flags
& DOF_SECF_LOAD
)) {
11748 dtrace_dof_error(dof
, "referenced section is not loadable");
11752 if (type
!= DOF_SECT_NONE
&& type
!= sec
->dofs_type
) {
11753 dtrace_dof_error(dof
, "referenced section is the wrong type");
11760 static dtrace_probedesc_t
*
11761 dtrace_dof_probedesc(dof_hdr_t
*dof
, dof_sec_t
*sec
, dtrace_probedesc_t
*desc
)
11763 dof_probedesc_t
*probe
;
11765 uintptr_t daddr
= (uintptr_t)dof
;
11769 if (sec
->dofs_type
!= DOF_SECT_PROBEDESC
) {
11770 dtrace_dof_error(dof
, "invalid probe section");
11774 if (sec
->dofs_align
!= sizeof (dof_secidx_t
)) {
11775 dtrace_dof_error(dof
, "bad alignment in probe description");
11779 if (sec
->dofs_offset
+ sizeof (dof_probedesc_t
) > dof
->dofh_loadsz
) {
11780 dtrace_dof_error(dof
, "truncated probe description");
11784 probe
= (dof_probedesc_t
*)(uintptr_t)(daddr
+ sec
->dofs_offset
);
11785 strtab
= dtrace_dof_sect(dof
, DOF_SECT_STRTAB
, probe
->dofp_strtab
);
11787 if (strtab
== NULL
)
11790 str
= daddr
+ strtab
->dofs_offset
;
11791 size
= strtab
->dofs_size
;
11793 if (probe
->dofp_provider
>= strtab
->dofs_size
) {
11794 dtrace_dof_error(dof
, "corrupt probe provider");
11798 (void) strncpy(desc
->dtpd_provider
,
11799 (char *)(str
+ probe
->dofp_provider
),
11800 MIN(DTRACE_PROVNAMELEN
- 1, size
- probe
->dofp_provider
));
11802 /* APPLE NOTE: Darwin employs size bounded string operation. */
11803 desc
->dtpd_provider
[DTRACE_PROVNAMELEN
- 1] = '\0';
11805 if (probe
->dofp_mod
>= strtab
->dofs_size
) {
11806 dtrace_dof_error(dof
, "corrupt probe module");
11810 (void) strncpy(desc
->dtpd_mod
, (char *)(str
+ probe
->dofp_mod
),
11811 MIN(DTRACE_MODNAMELEN
- 1, size
- probe
->dofp_mod
));
11813 /* APPLE NOTE: Darwin employs size bounded string operation. */
11814 desc
->dtpd_mod
[DTRACE_MODNAMELEN
- 1] = '\0';
11816 if (probe
->dofp_func
>= strtab
->dofs_size
) {
11817 dtrace_dof_error(dof
, "corrupt probe function");
11821 (void) strncpy(desc
->dtpd_func
, (char *)(str
+ probe
->dofp_func
),
11822 MIN(DTRACE_FUNCNAMELEN
- 1, size
- probe
->dofp_func
));
11824 /* APPLE NOTE: Darwin employs size bounded string operation. */
11825 desc
->dtpd_func
[DTRACE_FUNCNAMELEN
- 1] = '\0';
11827 if (probe
->dofp_name
>= strtab
->dofs_size
) {
11828 dtrace_dof_error(dof
, "corrupt probe name");
11832 (void) strncpy(desc
->dtpd_name
, (char *)(str
+ probe
->dofp_name
),
11833 MIN(DTRACE_NAMELEN
- 1, size
- probe
->dofp_name
));
11835 /* APPLE NOTE: Darwin employs size bounded string operation. */
11836 desc
->dtpd_name
[DTRACE_NAMELEN
- 1] = '\0';
11841 static dtrace_difo_t
*
11842 dtrace_dof_difo(dof_hdr_t
*dof
, dof_sec_t
*sec
, dtrace_vstate_t
*vstate
,
11847 dof_difohdr_t
*dofd
;
11848 uintptr_t daddr
= (uintptr_t)dof
;
11849 size_t max_size
= dtrace_difo_maxsize
;
11854 static const struct {
11862 { DOF_SECT_DIF
, offsetof(dtrace_difo_t
, dtdo_buf
),
11863 offsetof(dtrace_difo_t
, dtdo_len
), sizeof (dif_instr_t
),
11864 sizeof (dif_instr_t
), "multiple DIF sections" },
11866 { DOF_SECT_INTTAB
, offsetof(dtrace_difo_t
, dtdo_inttab
),
11867 offsetof(dtrace_difo_t
, dtdo_intlen
), sizeof (uint64_t),
11868 sizeof (uint64_t), "multiple integer tables" },
11870 { DOF_SECT_STRTAB
, offsetof(dtrace_difo_t
, dtdo_strtab
),
11871 offsetof(dtrace_difo_t
, dtdo_strlen
), 0,
11872 sizeof (char), "multiple string tables" },
11874 { DOF_SECT_VARTAB
, offsetof(dtrace_difo_t
, dtdo_vartab
),
11875 offsetof(dtrace_difo_t
, dtdo_varlen
), sizeof (dtrace_difv_t
),
11876 sizeof (uint_t
), "multiple variable tables" },
11878 { DOF_SECT_NONE
, 0, 0, 0, 0, NULL
}
11881 if (sec
->dofs_type
!= DOF_SECT_DIFOHDR
) {
11882 dtrace_dof_error(dof
, "invalid DIFO header section");
11886 if (sec
->dofs_align
!= sizeof (dof_secidx_t
)) {
11887 dtrace_dof_error(dof
, "bad alignment in DIFO header");
11891 if (sec
->dofs_size
< sizeof (dof_difohdr_t
) ||
11892 sec
->dofs_size
% sizeof (dof_secidx_t
)) {
11893 dtrace_dof_error(dof
, "bad size in DIFO header");
11897 dofd
= (dof_difohdr_t
*)(uintptr_t)(daddr
+ sec
->dofs_offset
);
11898 n
= (sec
->dofs_size
- sizeof (*dofd
)) / sizeof (dof_secidx_t
) + 1;
11900 dp
= kmem_zalloc(sizeof (dtrace_difo_t
), KM_SLEEP
);
11901 dp
->dtdo_rtype
= dofd
->dofd_rtype
;
11903 for (l
= 0; l
< n
; l
++) {
11908 if ((subsec
= dtrace_dof_sect(dof
, DOF_SECT_NONE
,
11909 dofd
->dofd_links
[l
])) == NULL
)
11910 goto err
; /* invalid section link */
11912 if (ttl
+ subsec
->dofs_size
> max_size
) {
11913 dtrace_dof_error(dof
, "exceeds maximum size");
11917 ttl
+= subsec
->dofs_size
;
11919 for (i
= 0; difo
[i
].section
!= DOF_SECT_NONE
; i
++) {
11921 if (subsec
->dofs_type
!= (uint32_t)difo
[i
].section
)
11924 if (!(subsec
->dofs_flags
& DOF_SECF_LOAD
)) {
11925 dtrace_dof_error(dof
, "section not loaded");
11929 if (subsec
->dofs_align
!= (uint32_t)difo
[i
].align
) {
11930 dtrace_dof_error(dof
, "bad alignment");
11934 bufp
= (void **)((uintptr_t)dp
+ difo
[i
].bufoffs
);
11935 lenp
= (uint32_t *)((uintptr_t)dp
+ difo
[i
].lenoffs
);
11937 if (*bufp
!= NULL
) {
11938 dtrace_dof_error(dof
, difo
[i
].msg
);
11942 if ((uint32_t)difo
[i
].entsize
!= subsec
->dofs_entsize
) {
11943 dtrace_dof_error(dof
, "entry size mismatch");
11947 if (subsec
->dofs_entsize
!= 0 &&
11948 (subsec
->dofs_size
% subsec
->dofs_entsize
) != 0) {
11949 dtrace_dof_error(dof
, "corrupt entry size");
11953 *lenp
= subsec
->dofs_size
;
11954 *bufp
= kmem_alloc(subsec
->dofs_size
, KM_SLEEP
);
11955 bcopy((char *)(uintptr_t)(daddr
+ subsec
->dofs_offset
),
11956 *bufp
, subsec
->dofs_size
);
11958 if (subsec
->dofs_entsize
!= 0)
11959 *lenp
/= subsec
->dofs_entsize
;
11965 * If we encounter a loadable DIFO sub-section that is not
11966 * known to us, assume this is a broken program and fail.
11968 if (difo
[i
].section
== DOF_SECT_NONE
&&
11969 (subsec
->dofs_flags
& DOF_SECF_LOAD
)) {
11970 dtrace_dof_error(dof
, "unrecognized DIFO subsection");
11975 if (dp
->dtdo_buf
== NULL
) {
11977 * We can't have a DIF object without DIF text.
11979 dtrace_dof_error(dof
, "missing DIF text");
11984 * Before we validate the DIF object, run through the variable table
11985 * looking for the strings -- if any of their size are under, we'll set
11986 * their size to be the system-wide default string size. Note that
11987 * this should _not_ happen if the "strsize" option has been set --
11988 * in this case, the compiler should have set the size to reflect the
11989 * setting of the option.
11991 for (i
= 0; i
< dp
->dtdo_varlen
; i
++) {
11992 dtrace_difv_t
*v
= &dp
->dtdo_vartab
[i
];
11993 dtrace_diftype_t
*t
= &v
->dtdv_type
;
11995 if (v
->dtdv_id
< DIF_VAR_OTHER_UBASE
)
11998 if (t
->dtdt_kind
== DIF_TYPE_STRING
&& t
->dtdt_size
== 0)
11999 t
->dtdt_size
= dtrace_strsize_default
;
12002 if (dtrace_difo_validate(dp
, vstate
, DIF_DIR_NREGS
, cr
) != 0)
12005 dtrace_difo_init(dp
, vstate
);
12009 kmem_free(dp
->dtdo_buf
, dp
->dtdo_len
* sizeof (dif_instr_t
));
12010 kmem_free(dp
->dtdo_inttab
, dp
->dtdo_intlen
* sizeof (uint64_t));
12011 kmem_free(dp
->dtdo_strtab
, dp
->dtdo_strlen
);
12012 kmem_free(dp
->dtdo_vartab
, dp
->dtdo_varlen
* sizeof (dtrace_difv_t
));
12014 kmem_free(dp
, sizeof (dtrace_difo_t
));
12018 static dtrace_predicate_t
*
12019 dtrace_dof_predicate(dof_hdr_t
*dof
, dof_sec_t
*sec
, dtrace_vstate_t
*vstate
,
12024 if ((dp
= dtrace_dof_difo(dof
, sec
, vstate
, cr
)) == NULL
)
12027 return (dtrace_predicate_create(dp
));
12030 static dtrace_actdesc_t
*
12031 dtrace_dof_actdesc(dof_hdr_t
*dof
, dof_sec_t
*sec
, dtrace_vstate_t
*vstate
,
12034 dtrace_actdesc_t
*act
, *first
= NULL
, *last
= NULL
, *next
;
12035 dof_actdesc_t
*desc
;
12036 dof_sec_t
*difosec
;
12038 uintptr_t daddr
= (uintptr_t)dof
;
12040 dtrace_actkind_t kind
;
12042 if (sec
->dofs_type
!= DOF_SECT_ACTDESC
) {
12043 dtrace_dof_error(dof
, "invalid action section");
12047 if (sec
->dofs_offset
+ sizeof (dof_actdesc_t
) > dof
->dofh_loadsz
) {
12048 dtrace_dof_error(dof
, "truncated action description");
12052 if (sec
->dofs_align
!= sizeof (uint64_t)) {
12053 dtrace_dof_error(dof
, "bad alignment in action description");
12057 if (sec
->dofs_size
< sec
->dofs_entsize
) {
12058 dtrace_dof_error(dof
, "section entry size exceeds total size");
12062 if (sec
->dofs_entsize
!= sizeof (dof_actdesc_t
)) {
12063 dtrace_dof_error(dof
, "bad entry size in action description");
12067 if (sec
->dofs_size
/ sec
->dofs_entsize
> dtrace_actions_max
) {
12068 dtrace_dof_error(dof
, "actions exceed dtrace_actions_max");
12072 for (offs
= 0; offs
< sec
->dofs_size
; offs
+= sec
->dofs_entsize
) {
12073 desc
= (dof_actdesc_t
*)(daddr
+
12074 (uintptr_t)sec
->dofs_offset
+ offs
);
12075 kind
= (dtrace_actkind_t
)desc
->dofa_kind
;
12077 if (DTRACEACT_ISPRINTFLIKE(kind
) &&
12078 (kind
!= DTRACEACT_PRINTA
||
12079 desc
->dofa_strtab
!= DOF_SECIDX_NONE
)) {
12085 * printf()-like actions must have a format string.
12087 if ((strtab
= dtrace_dof_sect(dof
,
12088 DOF_SECT_STRTAB
, desc
->dofa_strtab
)) == NULL
)
12091 str
= (char *)((uintptr_t)dof
+
12092 (uintptr_t)strtab
->dofs_offset
);
12094 for (i
= desc
->dofa_arg
; i
< strtab
->dofs_size
; i
++) {
12095 if (str
[i
] == '\0')
12099 if (i
>= strtab
->dofs_size
) {
12100 dtrace_dof_error(dof
, "bogus format string");
12104 if (i
== desc
->dofa_arg
) {
12105 dtrace_dof_error(dof
, "empty format string");
12109 i
-= desc
->dofa_arg
;
12110 fmt
= kmem_alloc(i
+ 1, KM_SLEEP
);
12111 bcopy(&str
[desc
->dofa_arg
], fmt
, i
+ 1);
12112 arg
= (uint64_t)(uintptr_t)fmt
;
12114 if (kind
== DTRACEACT_PRINTA
) {
12115 ASSERT(desc
->dofa_strtab
== DOF_SECIDX_NONE
);
12118 arg
= desc
->dofa_arg
;
12122 act
= dtrace_actdesc_create(kind
, desc
->dofa_ntuple
,
12123 desc
->dofa_uarg
, arg
);
12125 if (last
!= NULL
) {
12126 last
->dtad_next
= act
;
12133 if (desc
->dofa_difo
== DOF_SECIDX_NONE
)
12136 if ((difosec
= dtrace_dof_sect(dof
,
12137 DOF_SECT_DIFOHDR
, desc
->dofa_difo
)) == NULL
)
12140 act
->dtad_difo
= dtrace_dof_difo(dof
, difosec
, vstate
, cr
);
12142 if (act
->dtad_difo
== NULL
)
12146 ASSERT(first
!= NULL
);
12150 for (act
= first
; act
!= NULL
; act
= next
) {
12151 next
= act
->dtad_next
;
12152 dtrace_actdesc_release(act
, vstate
);
12158 static dtrace_ecbdesc_t
*
12159 dtrace_dof_ecbdesc(dof_hdr_t
*dof
, dof_sec_t
*sec
, dtrace_vstate_t
*vstate
,
12162 dtrace_ecbdesc_t
*ep
;
12163 dof_ecbdesc_t
*ecb
;
12164 dtrace_probedesc_t
*desc
;
12165 dtrace_predicate_t
*pred
= NULL
;
12167 if (sec
->dofs_size
< sizeof (dof_ecbdesc_t
)) {
12168 dtrace_dof_error(dof
, "truncated ECB description");
12172 if (sec
->dofs_align
!= sizeof (uint64_t)) {
12173 dtrace_dof_error(dof
, "bad alignment in ECB description");
12177 ecb
= (dof_ecbdesc_t
*)((uintptr_t)dof
+ (uintptr_t)sec
->dofs_offset
);
12178 sec
= dtrace_dof_sect(dof
, DOF_SECT_PROBEDESC
, ecb
->dofe_probes
);
12183 ep
= kmem_zalloc(sizeof (dtrace_ecbdesc_t
), KM_SLEEP
);
12184 ep
->dted_uarg
= ecb
->dofe_uarg
;
12185 desc
= &ep
->dted_probe
;
12187 if (dtrace_dof_probedesc(dof
, sec
, desc
) == NULL
)
12190 if (ecb
->dofe_pred
!= DOF_SECIDX_NONE
) {
12191 if ((sec
= dtrace_dof_sect(dof
,
12192 DOF_SECT_DIFOHDR
, ecb
->dofe_pred
)) == NULL
)
12195 if ((pred
= dtrace_dof_predicate(dof
, sec
, vstate
, cr
)) == NULL
)
12198 ep
->dted_pred
.dtpdd_predicate
= pred
;
12201 if (ecb
->dofe_actions
!= DOF_SECIDX_NONE
) {
12202 if ((sec
= dtrace_dof_sect(dof
,
12203 DOF_SECT_ACTDESC
, ecb
->dofe_actions
)) == NULL
)
12206 ep
->dted_action
= dtrace_dof_actdesc(dof
, sec
, vstate
, cr
);
12208 if (ep
->dted_action
== NULL
)
12216 dtrace_predicate_release(pred
, vstate
);
12217 kmem_free(ep
, sizeof (dtrace_ecbdesc_t
));
12222 * APPLE NOTE: dyld handles dof relocation.
12223 * Darwin does not need dtrace_dof_relocate()
12227 * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated
12228 * header: it should be at the front of a memory region that is at least
12229 * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in
12230 * size. It need not be validated in any other way.
12233 dtrace_dof_slurp(dof_hdr_t
*dof
, dtrace_vstate_t
*vstate
, cred_t
*cr
,
12234 dtrace_enabling_t
**enabp
, uint64_t ubase
, int noprobes
)
12236 #pragma unused(ubase) /* __APPLE__ */
12237 uint64_t len
= dof
->dofh_loadsz
, seclen
;
12238 uintptr_t daddr
= (uintptr_t)dof
;
12239 dtrace_ecbdesc_t
*ep
;
12240 dtrace_enabling_t
*enab
;
12243 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
12244 ASSERT(dof
->dofh_loadsz
>= sizeof (dof_hdr_t
));
12247 * Check the DOF header identification bytes. In addition to checking
12248 * valid settings, we also verify that unused bits/bytes are zeroed so
12249 * we can use them later without fear of regressing existing binaries.
12251 if (bcmp(&dof
->dofh_ident
[DOF_ID_MAG0
],
12252 DOF_MAG_STRING
, DOF_MAG_STRLEN
) != 0) {
12253 dtrace_dof_error(dof
, "DOF magic string mismatch");
12257 if (dof
->dofh_ident
[DOF_ID_MODEL
] != DOF_MODEL_ILP32
&&
12258 dof
->dofh_ident
[DOF_ID_MODEL
] != DOF_MODEL_LP64
) {
12259 dtrace_dof_error(dof
, "DOF has invalid data model");
12263 if (dof
->dofh_ident
[DOF_ID_ENCODING
] != DOF_ENCODE_NATIVE
) {
12264 dtrace_dof_error(dof
, "DOF encoding mismatch");
12269 * APPLE NOTE: Darwin only supports DOF_VERSION_3 for now.
12271 if (dof
->dofh_ident
[DOF_ID_VERSION
] != DOF_VERSION_3
) {
12272 dtrace_dof_error(dof
, "DOF version mismatch");
12276 if (dof
->dofh_ident
[DOF_ID_DIFVERS
] != DIF_VERSION_2
) {
12277 dtrace_dof_error(dof
, "DOF uses unsupported instruction set");
12281 if (dof
->dofh_ident
[DOF_ID_DIFIREG
] > DIF_DIR_NREGS
) {
12282 dtrace_dof_error(dof
, "DOF uses too many integer registers");
12286 if (dof
->dofh_ident
[DOF_ID_DIFTREG
] > DIF_DTR_NREGS
) {
12287 dtrace_dof_error(dof
, "DOF uses too many tuple registers");
12291 for (i
= DOF_ID_PAD
; i
< DOF_ID_SIZE
; i
++) {
12292 if (dof
->dofh_ident
[i
] != 0) {
12293 dtrace_dof_error(dof
, "DOF has invalid ident byte set");
12298 if (dof
->dofh_flags
& ~DOF_FL_VALID
) {
12299 dtrace_dof_error(dof
, "DOF has invalid flag bits set");
12303 if (dof
->dofh_secsize
== 0) {
12304 dtrace_dof_error(dof
, "zero section header size");
12309 * Check that the section headers don't exceed the amount of DOF
12310 * data. Note that we cast the section size and number of sections
12311 * to uint64_t's to prevent possible overflow in the multiplication.
12313 seclen
= (uint64_t)dof
->dofh_secnum
* (uint64_t)dof
->dofh_secsize
;
12315 if (dof
->dofh_secoff
> len
|| seclen
> len
||
12316 dof
->dofh_secoff
+ seclen
> len
) {
12317 dtrace_dof_error(dof
, "truncated section headers");
12321 if (!IS_P2ALIGNED(dof
->dofh_secoff
, sizeof (uint64_t))) {
12322 dtrace_dof_error(dof
, "misaligned section headers");
12326 if (!IS_P2ALIGNED(dof
->dofh_secsize
, sizeof (uint64_t))) {
12327 dtrace_dof_error(dof
, "misaligned section size");
12332 * Take an initial pass through the section headers to be sure that
12333 * the headers don't have stray offsets. If the 'noprobes' flag is
12334 * set, do not permit sections relating to providers, probes, or args.
12336 for (i
= 0; i
< dof
->dofh_secnum
; i
++) {
12337 dof_sec_t
*sec
= (dof_sec_t
*)(daddr
+
12338 (uintptr_t)dof
->dofh_secoff
+ i
* dof
->dofh_secsize
);
12341 switch (sec
->dofs_type
) {
12342 case DOF_SECT_PROVIDER
:
12343 case DOF_SECT_PROBES
:
12344 case DOF_SECT_PRARGS
:
12345 case DOF_SECT_PROFFS
:
12346 dtrace_dof_error(dof
, "illegal sections "
12352 if (!(sec
->dofs_flags
& DOF_SECF_LOAD
))
12353 continue; /* just ignore non-loadable sections */
12355 if (sec
->dofs_align
& (sec
->dofs_align
- 1)) {
12356 dtrace_dof_error(dof
, "bad section alignment");
12360 if (sec
->dofs_offset
& (sec
->dofs_align
- 1)) {
12361 dtrace_dof_error(dof
, "misaligned section");
12365 if (sec
->dofs_offset
> len
|| sec
->dofs_size
> len
||
12366 sec
->dofs_offset
+ sec
->dofs_size
> len
) {
12367 dtrace_dof_error(dof
, "corrupt section header");
12371 if (sec
->dofs_type
== DOF_SECT_STRTAB
&& *((char *)daddr
+
12372 sec
->dofs_offset
+ sec
->dofs_size
- 1) != '\0') {
12373 dtrace_dof_error(dof
, "non-terminating string table");
12379 * APPLE NOTE: We have no further relocation to perform.
12380 * All dof values are relative offsets.
12383 if ((enab
= *enabp
) == NULL
)
12384 enab
= *enabp
= dtrace_enabling_create(vstate
);
12386 for (i
= 0; i
< dof
->dofh_secnum
; i
++) {
12387 dof_sec_t
*sec
= (dof_sec_t
*)(daddr
+
12388 (uintptr_t)dof
->dofh_secoff
+ i
* dof
->dofh_secsize
);
12390 if (sec
->dofs_type
!= DOF_SECT_ECBDESC
)
12394 * APPLE NOTE: Defend against gcc 4.0 botch on x86.
12395 * not all paths out of inlined dtrace_dof_ecbdesc
12396 * are checked for the NULL return value.
12397 * Check for NULL explicitly here.
12399 ep
= dtrace_dof_ecbdesc(dof
, sec
, vstate
, cr
);
12401 dtrace_enabling_destroy(enab
);
12406 dtrace_enabling_add(enab
, ep
);
12413 * Process DOF for any options. This routine assumes that the DOF has been
12414 * at least processed by dtrace_dof_slurp().
12417 dtrace_dof_options(dof_hdr_t
*dof
, dtrace_state_t
*state
)
12423 dof_optdesc_t
*desc
;
12425 for (i
= 0; i
< dof
->dofh_secnum
; i
++) {
12426 dof_sec_t
*sec
= (dof_sec_t
*)((uintptr_t)dof
+
12427 (uintptr_t)dof
->dofh_secoff
+ i
* dof
->dofh_secsize
);
12429 if (sec
->dofs_type
!= DOF_SECT_OPTDESC
)
12432 if (sec
->dofs_align
!= sizeof (uint64_t)) {
12433 dtrace_dof_error(dof
, "bad alignment in "
12434 "option description");
12438 if ((entsize
= sec
->dofs_entsize
) == 0) {
12439 dtrace_dof_error(dof
, "zeroed option entry size");
12443 if (entsize
< sizeof (dof_optdesc_t
)) {
12444 dtrace_dof_error(dof
, "bad option entry size");
12448 for (offs
= 0; offs
< sec
->dofs_size
; offs
+= entsize
) {
12449 desc
= (dof_optdesc_t
*)((uintptr_t)dof
+
12450 (uintptr_t)sec
->dofs_offset
+ offs
);
12452 if (desc
->dofo_strtab
!= DOF_SECIDX_NONE
) {
12453 dtrace_dof_error(dof
, "non-zero option string");
12457 if (desc
->dofo_value
== (uint64_t)DTRACEOPT_UNSET
) {
12458 dtrace_dof_error(dof
, "unset option");
12462 if ((rval
= dtrace_state_option(state
,
12463 desc
->dofo_option
, desc
->dofo_value
)) != 0) {
12464 dtrace_dof_error(dof
, "rejected option");
12474 * DTrace Consumer State Functions
12477 dtrace_dstate_init(dtrace_dstate_t
*dstate
, size_t size
)
12479 size_t hashsize
, maxper
, min_size
, chunksize
= dstate
->dtds_chunksize
;
12482 dtrace_dynvar_t
*dvar
, *next
, *start
;
12485 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
12486 ASSERT(dstate
->dtds_base
== NULL
&& dstate
->dtds_percpu
== NULL
);
12488 bzero(dstate
, sizeof (dtrace_dstate_t
));
12490 if ((dstate
->dtds_chunksize
= chunksize
) == 0)
12491 dstate
->dtds_chunksize
= DTRACE_DYNVAR_CHUNKSIZE
;
12493 if (size
< (min_size
= dstate
->dtds_chunksize
+ sizeof (dtrace_dynhash_t
)))
12496 if ((base
= kmem_zalloc(size
, KM_NOSLEEP
)) == NULL
)
12499 dstate
->dtds_size
= size
;
12500 dstate
->dtds_base
= base
;
12501 dstate
->dtds_percpu
= kmem_cache_alloc(dtrace_state_cache
, KM_SLEEP
);
12502 bzero(dstate
->dtds_percpu
, (int)NCPU
* sizeof (dtrace_dstate_percpu_t
));
12504 hashsize
= size
/ (dstate
->dtds_chunksize
+ sizeof (dtrace_dynhash_t
));
12506 if (hashsize
!= 1 && (hashsize
& 1))
12509 dstate
->dtds_hashsize
= hashsize
;
12510 dstate
->dtds_hash
= dstate
->dtds_base
;
12513 * Set all of our hash buckets to point to the single sink, and (if
12514 * it hasn't already been set), set the sink's hash value to be the
12515 * sink sentinel value. The sink is needed for dynamic variable
12516 * lookups to know that they have iterated over an entire, valid hash
12519 for (i
= 0; i
< hashsize
; i
++)
12520 dstate
->dtds_hash
[i
].dtdh_chain
= &dtrace_dynhash_sink
;
12522 if (dtrace_dynhash_sink
.dtdv_hashval
!= DTRACE_DYNHASH_SINK
)
12523 dtrace_dynhash_sink
.dtdv_hashval
= DTRACE_DYNHASH_SINK
;
12526 * Determine number of active CPUs. Divide free list evenly among
12529 start
= (dtrace_dynvar_t
*)
12530 ((uintptr_t)base
+ hashsize
* sizeof (dtrace_dynhash_t
));
12531 limit
= (uintptr_t)base
+ size
;
12533 maxper
= (limit
- (uintptr_t)start
) / (int)NCPU
;
12534 maxper
= (maxper
/ dstate
->dtds_chunksize
) * dstate
->dtds_chunksize
;
12536 for (i
= 0; i
< NCPU
; i
++) {
12537 dstate
->dtds_percpu
[i
].dtdsc_free
= dvar
= start
;
12540 * If we don't even have enough chunks to make it once through
12541 * NCPUs, we're just going to allocate everything to the first
12542 * CPU. And if we're on the last CPU, we're going to allocate
12543 * whatever is left over. In either case, we set the limit to
12544 * be the limit of the dynamic variable space.
12546 if (maxper
== 0 || i
== NCPU
- 1) {
12547 limit
= (uintptr_t)base
+ size
;
12550 limit
= (uintptr_t)start
+ maxper
;
12551 start
= (dtrace_dynvar_t
*)limit
;
12554 ASSERT(limit
<= (uintptr_t)base
+ size
);
12557 next
= (dtrace_dynvar_t
*)((uintptr_t)dvar
+
12558 dstate
->dtds_chunksize
);
12560 if ((uintptr_t)next
+ dstate
->dtds_chunksize
>= limit
)
12563 dvar
->dtdv_next
= next
;
12575 dtrace_dstate_fini(dtrace_dstate_t
*dstate
)
12577 lck_mtx_assert(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
12579 if (dstate
->dtds_base
== NULL
)
12582 kmem_free(dstate
->dtds_base
, dstate
->dtds_size
);
12583 kmem_cache_free(dtrace_state_cache
, dstate
->dtds_percpu
);
12587 dtrace_vstate_fini(dtrace_vstate_t
*vstate
)
12590 * Logical XOR, where are you?
12592 ASSERT((vstate
->dtvs_nglobals
== 0) ^ (vstate
->dtvs_globals
!= NULL
));
12594 if (vstate
->dtvs_nglobals
> 0) {
12595 kmem_free(vstate
->dtvs_globals
, vstate
->dtvs_nglobals
*
12596 sizeof (dtrace_statvar_t
*));
12599 if (vstate
->dtvs_ntlocals
> 0) {
12600 kmem_free(vstate
->dtvs_tlocals
, vstate
->dtvs_ntlocals
*
12601 sizeof (dtrace_difv_t
));
12604 ASSERT((vstate
->dtvs_nlocals
== 0) ^ (vstate
->dtvs_locals
!= NULL
));
12606 if (vstate
->dtvs_nlocals
> 0) {
12607 kmem_free(vstate
->dtvs_locals
, vstate
->dtvs_nlocals
*
12608 sizeof (dtrace_statvar_t
*));
12613 dtrace_state_clean(dtrace_state_t
*state
)
12615 if (state
->dts_activity
== DTRACE_ACTIVITY_INACTIVE
)
12618 dtrace_dynvar_clean(&state
->dts_vstate
.dtvs_dynvars
);
12619 dtrace_speculation_clean(state
);
12623 dtrace_state_deadman(dtrace_state_t
*state
)
12629 now
= dtrace_gethrtime();
12631 if (state
!= dtrace_anon
.dta_state
&&
12632 now
- state
->dts_laststatus
>= dtrace_deadman_user
)
12636 * We must be sure that dts_alive never appears to be less than the
12637 * value upon entry to dtrace_state_deadman(), and because we lack a
12638 * dtrace_cas64(), we cannot store to it atomically. We thus instead
12639 * store INT64_MAX to it, followed by a memory barrier, followed by
12640 * the new value. This assures that dts_alive never appears to be
12641 * less than its true value, regardless of the order in which the
12642 * stores to the underlying storage are issued.
12644 state
->dts_alive
= INT64_MAX
;
12645 dtrace_membar_producer();
12646 state
->dts_alive
= now
;
12650 dtrace_state_create(dev_t
*devp
, cred_t
*cr
, dtrace_state_t
**new_state
)
12655 dtrace_state_t
*state
;
12656 dtrace_optval_t
*opt
;
12657 int bufsize
= (int)NCPU
* sizeof (dtrace_buffer_t
), i
;
12659 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
12660 lck_mtx_assert(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
12662 /* Cause restart */
12666 * Darwin's DEVFS layer acquired the minor number for this "device" when it called
12667 * dtrace_devfs_clone_func(). At that time, dtrace_devfs_clone_func() proposed a minor number
12668 * (next unused according to vmem_alloc()) and then immediately put the number back in play
12669 * (by calling vmem_free()). Now that minor number is being used for an open, so committing it
12670 * to use. The following vmem_alloc() must deliver that same minor number. FIXME.
12673 minor
= (minor_t
)(uintptr_t)vmem_alloc(dtrace_minor
, 1,
12674 VM_BESTFIT
| VM_SLEEP
);
12676 if (NULL
!= devp
) {
12677 ASSERT(getminor(*devp
) == minor
);
12678 if (getminor(*devp
) != minor
) {
12679 printf("dtrace_open: couldn't re-acquire vended minor number %d. Instead got %d\n",
12680 getminor(*devp
), minor
);
12681 vmem_free(dtrace_minor
, (void *)(uintptr_t)minor
, 1);
12682 return (ERESTART
); /* can't reacquire */
12685 /* NULL==devp iff "Anonymous state" (see dtrace_anon_property),
12686 * so just vend the minor device number here de novo since no "open" has occurred. */
12689 if (ddi_soft_state_zalloc(dtrace_softstate
, minor
) != DDI_SUCCESS
) {
12690 vmem_free(dtrace_minor
, (void *)(uintptr_t)minor
, 1);
12691 return (EAGAIN
); /* temporary resource shortage */
12694 state
= ddi_get_soft_state(dtrace_softstate
, minor
);
12695 state
->dts_epid
= DTRACE_EPIDNONE
+ 1;
12697 (void) snprintf(c
, sizeof (c
), "dtrace_aggid_%d", minor
);
12698 state
->dts_aggid_arena
= vmem_create(c
, (void *)1, UINT32_MAX
, 1,
12699 NULL
, NULL
, NULL
, 0, VM_SLEEP
| VMC_IDENTIFIER
);
12701 if (devp
!= NULL
) {
12702 major
= getemajor(*devp
);
12704 major
= ddi_driver_major(dtrace_devi
);
12707 state
->dts_dev
= makedevice(major
, minor
);
12710 *devp
= state
->dts_dev
;
12713 * We allocate NCPU buffers. On the one hand, this can be quite
12714 * a bit of memory per instance (nearly 36K on a Starcat). On the
12715 * other hand, it saves an additional memory reference in the probe
12718 state
->dts_buffer
= kmem_zalloc(bufsize
, KM_SLEEP
);
12719 state
->dts_aggbuffer
= kmem_zalloc(bufsize
, KM_SLEEP
);
12720 state
->dts_cleaner
= CYCLIC_NONE
;
12721 state
->dts_deadman
= CYCLIC_NONE
;
12722 state
->dts_vstate
.dtvs_state
= state
;
12724 for (i
= 0; i
< DTRACEOPT_MAX
; i
++)
12725 state
->dts_options
[i
] = DTRACEOPT_UNSET
;
12728 * Set the default options.
12730 opt
= state
->dts_options
;
12731 opt
[DTRACEOPT_BUFPOLICY
] = DTRACEOPT_BUFPOLICY_SWITCH
;
12732 opt
[DTRACEOPT_BUFRESIZE
] = DTRACEOPT_BUFRESIZE_AUTO
;
12733 opt
[DTRACEOPT_NSPEC
] = dtrace_nspec_default
;
12734 opt
[DTRACEOPT_SPECSIZE
] = dtrace_specsize_default
;
12735 opt
[DTRACEOPT_CPU
] = (dtrace_optval_t
)DTRACE_CPUALL
;
12736 opt
[DTRACEOPT_STRSIZE
] = dtrace_strsize_default
;
12737 opt
[DTRACEOPT_STACKFRAMES
] = dtrace_stackframes_default
;
12738 opt
[DTRACEOPT_USTACKFRAMES
] = dtrace_ustackframes_default
;
12739 opt
[DTRACEOPT_CLEANRATE
] = dtrace_cleanrate_default
;
12740 opt
[DTRACEOPT_AGGRATE
] = dtrace_aggrate_default
;
12741 opt
[DTRACEOPT_SWITCHRATE
] = dtrace_switchrate_default
;
12742 opt
[DTRACEOPT_STATUSRATE
] = dtrace_statusrate_default
;
12743 opt
[DTRACEOPT_JSTACKFRAMES
] = dtrace_jstackframes_default
;
12744 opt
[DTRACEOPT_JSTACKSTRSIZE
] = dtrace_jstackstrsize_default
;
12746 state
->dts_activity
= DTRACE_ACTIVITY_INACTIVE
;
12749 * Depending on the user credentials, we set flag bits which alter probe
12750 * visibility or the amount of destructiveness allowed. In the case of
12751 * actual anonymous tracing, or the possession of all privileges, all of
12752 * the normal checks are bypassed.
12754 if (cr
== NULL
|| PRIV_POLICY_ONLY(cr
, PRIV_ALL
, B_FALSE
)) {
12755 state
->dts_cred
.dcr_visible
= DTRACE_CRV_ALL
;
12756 state
->dts_cred
.dcr_action
= DTRACE_CRA_ALL
;
12759 * Set up the credentials for this instantiation. We take a
12760 * hold on the credential to prevent it from disappearing on
12761 * us; this in turn prevents the zone_t referenced by this
12762 * credential from disappearing. This means that we can
12763 * examine the credential and the zone from probe context.
12766 state
->dts_cred
.dcr_cred
= cr
;
12769 * CRA_PROC means "we have *some* privilege for dtrace" and
12770 * unlocks the use of variables like pid, zonename, etc.
12772 if (PRIV_POLICY_ONLY(cr
, PRIV_DTRACE_USER
, B_FALSE
) ||
12773 PRIV_POLICY_ONLY(cr
, PRIV_DTRACE_PROC
, B_FALSE
)) {
12774 state
->dts_cred
.dcr_action
|= DTRACE_CRA_PROC
;
12778 * dtrace_user allows use of syscall and profile providers.
12779 * If the user also has proc_owner and/or proc_zone, we
12780 * extend the scope to include additional visibility and
12781 * destructive power.
12783 if (PRIV_POLICY_ONLY(cr
, PRIV_DTRACE_USER
, B_FALSE
)) {
12784 if (PRIV_POLICY_ONLY(cr
, PRIV_PROC_OWNER
, B_FALSE
)) {
12785 state
->dts_cred
.dcr_visible
|=
12786 DTRACE_CRV_ALLPROC
;
12788 state
->dts_cred
.dcr_action
|=
12789 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER
;
12792 if (PRIV_POLICY_ONLY(cr
, PRIV_PROC_ZONE
, B_FALSE
)) {
12793 state
->dts_cred
.dcr_visible
|=
12794 DTRACE_CRV_ALLZONE
;
12796 state
->dts_cred
.dcr_action
|=
12797 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE
;
12801 * If we have all privs in whatever zone this is,
12802 * we can do destructive things to processes which
12803 * have altered credentials.
12805 * APPLE NOTE: Darwin doesn't do zones.
12806 * Behave as if zone always has destructive privs.
12809 state
->dts_cred
.dcr_action
|=
12810 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG
;
12814 * Holding the dtrace_kernel privilege also implies that
12815 * the user has the dtrace_user privilege from a visibility
12816 * perspective. But without further privileges, some
12817 * destructive actions are not available.
12819 if (PRIV_POLICY_ONLY(cr
, PRIV_DTRACE_KERNEL
, B_FALSE
)) {
12821 * Make all probes in all zones visible. However,
12822 * this doesn't mean that all actions become available
12825 state
->dts_cred
.dcr_visible
|= DTRACE_CRV_KERNEL
|
12826 DTRACE_CRV_ALLPROC
| DTRACE_CRV_ALLZONE
;
12828 state
->dts_cred
.dcr_action
|= DTRACE_CRA_KERNEL
|
12831 * Holding proc_owner means that destructive actions
12832 * for *this* zone are allowed.
12834 if (PRIV_POLICY_ONLY(cr
, PRIV_PROC_OWNER
, B_FALSE
))
12835 state
->dts_cred
.dcr_action
|=
12836 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER
;
12839 * Holding proc_zone means that destructive actions
12840 * for this user/group ID in all zones is allowed.
12842 if (PRIV_POLICY_ONLY(cr
, PRIV_PROC_ZONE
, B_FALSE
))
12843 state
->dts_cred
.dcr_action
|=
12844 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE
;
12847 * If we have all privs in whatever zone this is,
12848 * we can do destructive things to processes which
12849 * have altered credentials.
12851 * APPLE NOTE: Darwin doesn't do zones.
12852 * Behave as if zone always has destructive privs.
12854 state
->dts_cred
.dcr_action
|=
12855 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG
;
12859 * Holding the dtrace_proc privilege gives control over fasttrap
12860 * and pid providers. We need to grant wider destructive
12861 * privileges in the event that the user has proc_owner and/or
12864 if (PRIV_POLICY_ONLY(cr
, PRIV_DTRACE_PROC
, B_FALSE
)) {
12865 if (PRIV_POLICY_ONLY(cr
, PRIV_PROC_OWNER
, B_FALSE
))
12866 state
->dts_cred
.dcr_action
|=
12867 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER
;
12869 if (PRIV_POLICY_ONLY(cr
, PRIV_PROC_ZONE
, B_FALSE
))
12870 state
->dts_cred
.dcr_action
|=
12871 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE
;
12875 *new_state
= state
;
12876 return(0); /* Success */
12880 dtrace_state_buffer(dtrace_state_t
*state
, dtrace_buffer_t
*buf
, int which
)
12882 dtrace_optval_t
*opt
= state
->dts_options
, size
;
12883 processorid_t cpu
= 0;
12884 int flags
= 0, rval
;
12886 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
12887 lck_mtx_assert(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
12888 ASSERT(which
< DTRACEOPT_MAX
);
12889 ASSERT(state
->dts_activity
== DTRACE_ACTIVITY_INACTIVE
||
12890 (state
== dtrace_anon
.dta_state
&&
12891 state
->dts_activity
== DTRACE_ACTIVITY_ACTIVE
));
12893 if (opt
[which
] == DTRACEOPT_UNSET
|| opt
[which
] == 0)
12896 if (opt
[DTRACEOPT_CPU
] != DTRACEOPT_UNSET
)
12897 cpu
= opt
[DTRACEOPT_CPU
];
12899 if (which
== DTRACEOPT_SPECSIZE
)
12900 flags
|= DTRACEBUF_NOSWITCH
;
12902 if (which
== DTRACEOPT_BUFSIZE
) {
12903 if (opt
[DTRACEOPT_BUFPOLICY
] == DTRACEOPT_BUFPOLICY_RING
)
12904 flags
|= DTRACEBUF_RING
;
12906 if (opt
[DTRACEOPT_BUFPOLICY
] == DTRACEOPT_BUFPOLICY_FILL
)
12907 flags
|= DTRACEBUF_FILL
;
12909 if (state
!= dtrace_anon
.dta_state
||
12910 state
->dts_activity
!= DTRACE_ACTIVITY_ACTIVE
)
12911 flags
|= DTRACEBUF_INACTIVE
;
12914 for (size
= opt
[which
]; (size_t)size
>= sizeof (uint64_t); size
>>= 1) {
12916 * The size must be 8-byte aligned. If the size is not 8-byte
12917 * aligned, drop it down by the difference.
12919 if (size
& (sizeof (uint64_t) - 1))
12920 size
-= size
& (sizeof (uint64_t) - 1);
12922 if (size
< state
->dts_reserve
) {
12924 * Buffers always must be large enough to accommodate
12925 * their prereserved space. We return E2BIG instead
12926 * of ENOMEM in this case to allow for user-level
12927 * software to differentiate the cases.
12932 rval
= dtrace_buffer_alloc(buf
, size
, flags
, cpu
);
12934 if (rval
!= ENOMEM
) {
12939 if (opt
[DTRACEOPT_BUFRESIZE
] == DTRACEOPT_BUFRESIZE_MANUAL
)
12947 dtrace_state_buffers(dtrace_state_t
*state
)
12949 dtrace_speculation_t
*spec
= state
->dts_speculations
;
12952 if ((rval
= dtrace_state_buffer(state
, state
->dts_buffer
,
12953 DTRACEOPT_BUFSIZE
)) != 0)
12956 if ((rval
= dtrace_state_buffer(state
, state
->dts_aggbuffer
,
12957 DTRACEOPT_AGGSIZE
)) != 0)
12960 for (i
= 0; i
< state
->dts_nspeculations
; i
++) {
12961 if ((rval
= dtrace_state_buffer(state
,
12962 spec
[i
].dtsp_buffer
, DTRACEOPT_SPECSIZE
)) != 0)
12970 dtrace_state_prereserve(dtrace_state_t
*state
)
12973 dtrace_probe_t
*probe
;
12975 state
->dts_reserve
= 0;
12977 if (state
->dts_options
[DTRACEOPT_BUFPOLICY
] != DTRACEOPT_BUFPOLICY_FILL
)
12981 * If our buffer policy is a "fill" buffer policy, we need to set the
12982 * prereserved space to be the space required by the END probes.
12984 probe
= dtrace_probes
[dtrace_probeid_end
- 1];
12985 ASSERT(probe
!= NULL
);
12987 for (ecb
= probe
->dtpr_ecb
; ecb
!= NULL
; ecb
= ecb
->dte_next
) {
12988 if (ecb
->dte_state
!= state
)
12991 state
->dts_reserve
+= ecb
->dte_needed
+ ecb
->dte_alignment
;
12996 dtrace_state_go(dtrace_state_t
*state
, processorid_t
*cpu
)
12998 dtrace_optval_t
*opt
= state
->dts_options
, sz
, nspec
;
12999 dtrace_speculation_t
*spec
;
13000 dtrace_buffer_t
*buf
;
13001 cyc_handler_t hdlr
;
13003 int rval
= 0, i
, bufsize
= (int)NCPU
* sizeof (dtrace_buffer_t
);
13004 dtrace_icookie_t cookie
;
13006 lck_mtx_lock(&cpu_lock
);
13007 lck_mtx_lock(&dtrace_lock
);
13009 if (state
->dts_activity
!= DTRACE_ACTIVITY_INACTIVE
) {
13015 * Before we can perform any checks, we must prime all of the
13016 * retained enablings that correspond to this state.
13018 dtrace_enabling_prime(state
);
13020 if (state
->dts_destructive
&& !state
->dts_cred
.dcr_destructive
) {
13025 dtrace_state_prereserve(state
);
13028 * Now we want to do is try to allocate our speculations.
13029 * We do not automatically resize the number of speculations; if
13030 * this fails, we will fail the operation.
13032 nspec
= opt
[DTRACEOPT_NSPEC
];
13033 ASSERT(nspec
!= DTRACEOPT_UNSET
);
13035 if (nspec
> INT_MAX
) {
13040 spec
= kmem_zalloc(nspec
* sizeof (dtrace_speculation_t
), KM_NOSLEEP
);
13042 if (spec
== NULL
) {
13047 state
->dts_speculations
= spec
;
13048 state
->dts_nspeculations
= (int)nspec
;
13050 for (i
= 0; i
< nspec
; i
++) {
13051 if ((buf
= kmem_zalloc(bufsize
, KM_NOSLEEP
)) == NULL
) {
13056 spec
[i
].dtsp_buffer
= buf
;
13059 if (opt
[DTRACEOPT_GRABANON
] != DTRACEOPT_UNSET
) {
13060 if (dtrace_anon
.dta_state
== NULL
) {
13065 if (state
->dts_necbs
!= 0) {
13070 state
->dts_anon
= dtrace_anon_grab();
13071 ASSERT(state
->dts_anon
!= NULL
);
13072 state
= state
->dts_anon
;
13075 * We want "grabanon" to be set in the grabbed state, so we'll
13076 * copy that option value from the grabbing state into the
13079 state
->dts_options
[DTRACEOPT_GRABANON
] =
13080 opt
[DTRACEOPT_GRABANON
];
13082 *cpu
= dtrace_anon
.dta_beganon
;
13085 * If the anonymous state is active (as it almost certainly
13086 * is if the anonymous enabling ultimately matched anything),
13087 * we don't allow any further option processing -- but we
13088 * don't return failure.
13090 if (state
->dts_activity
!= DTRACE_ACTIVITY_INACTIVE
)
13094 if (opt
[DTRACEOPT_AGGSIZE
] != DTRACEOPT_UNSET
&&
13095 opt
[DTRACEOPT_AGGSIZE
] != 0) {
13096 if (state
->dts_aggregations
== NULL
) {
13098 * We're not going to create an aggregation buffer
13099 * because we don't have any ECBs that contain
13100 * aggregations -- set this option to 0.
13102 opt
[DTRACEOPT_AGGSIZE
] = 0;
13105 * If we have an aggregation buffer, we must also have
13106 * a buffer to use as scratch.
13108 if (opt
[DTRACEOPT_BUFSIZE
] == DTRACEOPT_UNSET
||
13109 (size_t)opt
[DTRACEOPT_BUFSIZE
] < state
->dts_needed
) {
13110 opt
[DTRACEOPT_BUFSIZE
] = state
->dts_needed
;
13115 if (opt
[DTRACEOPT_SPECSIZE
] != DTRACEOPT_UNSET
&&
13116 opt
[DTRACEOPT_SPECSIZE
] != 0) {
13117 if (!state
->dts_speculates
) {
13119 * We're not going to create speculation buffers
13120 * because we don't have any ECBs that actually
13121 * speculate -- set the speculation size to 0.
13123 opt
[DTRACEOPT_SPECSIZE
] = 0;
13128 * The bare minimum size for any buffer that we're actually going to
13129 * do anything to is sizeof (uint64_t).
13131 sz
= sizeof (uint64_t);
13133 if ((state
->dts_needed
!= 0 && opt
[DTRACEOPT_BUFSIZE
] < sz
) ||
13134 (state
->dts_speculates
&& opt
[DTRACEOPT_SPECSIZE
] < sz
) ||
13135 (state
->dts_aggregations
!= NULL
&& opt
[DTRACEOPT_AGGSIZE
] < sz
)) {
13137 * A buffer size has been explicitly set to 0 (or to a size
13138 * that will be adjusted to 0) and we need the space -- we
13139 * need to return failure. We return ENOSPC to differentiate
13140 * it from failing to allocate a buffer due to failure to meet
13141 * the reserve (for which we return E2BIG).
13147 if ((rval
= dtrace_state_buffers(state
)) != 0)
13150 if ((sz
= opt
[DTRACEOPT_DYNVARSIZE
]) == DTRACEOPT_UNSET
)
13151 sz
= dtrace_dstate_defsize
;
13154 rval
= dtrace_dstate_init(&state
->dts_vstate
.dtvs_dynvars
, sz
);
13159 if (opt
[DTRACEOPT_BUFRESIZE
] == DTRACEOPT_BUFRESIZE_MANUAL
)
13161 } while (sz
>>= 1);
13163 opt
[DTRACEOPT_DYNVARSIZE
] = sz
;
13168 if (opt
[DTRACEOPT_STATUSRATE
] > dtrace_statusrate_max
)
13169 opt
[DTRACEOPT_STATUSRATE
] = dtrace_statusrate_max
;
13171 if (opt
[DTRACEOPT_CLEANRATE
] == 0)
13172 opt
[DTRACEOPT_CLEANRATE
] = dtrace_cleanrate_max
;
13174 if (opt
[DTRACEOPT_CLEANRATE
] < dtrace_cleanrate_min
)
13175 opt
[DTRACEOPT_CLEANRATE
] = dtrace_cleanrate_min
;
13177 if (opt
[DTRACEOPT_CLEANRATE
] > dtrace_cleanrate_max
)
13178 opt
[DTRACEOPT_CLEANRATE
] = dtrace_cleanrate_max
;
13180 hdlr
.cyh_func
= (cyc_func_t
)dtrace_state_clean
;
13181 hdlr
.cyh_arg
= state
;
13182 hdlr
.cyh_level
= CY_LOW_LEVEL
;
13185 when
.cyt_interval
= opt
[DTRACEOPT_CLEANRATE
];
13187 state
->dts_cleaner
= cyclic_add(&hdlr
, &when
);
13189 hdlr
.cyh_func
= (cyc_func_t
)dtrace_state_deadman
;
13190 hdlr
.cyh_arg
= state
;
13191 hdlr
.cyh_level
= CY_LOW_LEVEL
;
13194 when
.cyt_interval
= dtrace_deadman_interval
;
13196 state
->dts_alive
= state
->dts_laststatus
= dtrace_gethrtime();
13197 state
->dts_deadman
= cyclic_add(&hdlr
, &when
);
13199 state
->dts_activity
= DTRACE_ACTIVITY_WARMUP
;
13202 * Now it's time to actually fire the BEGIN probe. We need to disable
13203 * interrupts here both to record the CPU on which we fired the BEGIN
13204 * probe (the data from this CPU will be processed first at user
13205 * level) and to manually activate the buffer for this CPU.
13207 cookie
= dtrace_interrupt_disable();
13208 *cpu
= CPU
->cpu_id
;
13209 ASSERT(state
->dts_buffer
[*cpu
].dtb_flags
& DTRACEBUF_INACTIVE
);
13210 state
->dts_buffer
[*cpu
].dtb_flags
&= ~DTRACEBUF_INACTIVE
;
13212 dtrace_probe(dtrace_probeid_begin
,
13213 (uint64_t)(uintptr_t)state
, 0, 0, 0, 0);
13214 dtrace_interrupt_enable(cookie
);
13216 * We may have had an exit action from a BEGIN probe; only change our
13217 * state to ACTIVE if we're still in WARMUP.
13219 ASSERT(state
->dts_activity
== DTRACE_ACTIVITY_WARMUP
||
13220 state
->dts_activity
== DTRACE_ACTIVITY_DRAINING
);
13222 if (state
->dts_activity
== DTRACE_ACTIVITY_WARMUP
)
13223 state
->dts_activity
= DTRACE_ACTIVITY_ACTIVE
;
13226 * Regardless of whether or not now we're in ACTIVE or DRAINING, we
13227 * want each CPU to transition its principal buffer out of the
13228 * INACTIVE state. Doing this assures that no CPU will suddenly begin
13229 * processing an ECB halfway down a probe's ECB chain; all CPUs will
13230 * atomically transition from processing none of a state's ECBs to
13231 * processing all of them.
13233 dtrace_xcall(DTRACE_CPUALL
,
13234 (dtrace_xcall_t
)dtrace_buffer_activate
, state
);
13238 dtrace_buffer_free(state
->dts_buffer
);
13239 dtrace_buffer_free(state
->dts_aggbuffer
);
13241 if ((nspec
= state
->dts_nspeculations
) == 0) {
13242 ASSERT(state
->dts_speculations
== NULL
);
13246 spec
= state
->dts_speculations
;
13247 ASSERT(spec
!= NULL
);
13249 for (i
= 0; i
< state
->dts_nspeculations
; i
++) {
13250 if ((buf
= spec
[i
].dtsp_buffer
) == NULL
)
13253 dtrace_buffer_free(buf
);
13254 kmem_free(buf
, bufsize
);
13257 kmem_free(spec
, nspec
* sizeof (dtrace_speculation_t
));
13258 state
->dts_nspeculations
= 0;
13259 state
->dts_speculations
= NULL
;
13262 lck_mtx_unlock(&dtrace_lock
);
13263 lck_mtx_unlock(&cpu_lock
);
13269 dtrace_state_stop(dtrace_state_t
*state
, processorid_t
*cpu
)
13271 dtrace_icookie_t cookie
;
13273 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
13275 if (state
->dts_activity
!= DTRACE_ACTIVITY_ACTIVE
&&
13276 state
->dts_activity
!= DTRACE_ACTIVITY_DRAINING
)
13280 * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync
13281 * to be sure that every CPU has seen it. See below for the details
13282 * on why this is done.
13284 state
->dts_activity
= DTRACE_ACTIVITY_DRAINING
;
13288 * By this point, it is impossible for any CPU to be still processing
13289 * with DTRACE_ACTIVITY_ACTIVE. We can thus set our activity to
13290 * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any
13291 * other CPU in dtrace_buffer_reserve(). This allows dtrace_probe()
13292 * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN
13293 * iff we're in the END probe.
13295 state
->dts_activity
= DTRACE_ACTIVITY_COOLDOWN
;
13297 ASSERT(state
->dts_activity
== DTRACE_ACTIVITY_COOLDOWN
);
13300 * Finally, we can release the reserve and call the END probe. We
13301 * disable interrupts across calling the END probe to allow us to
13302 * return the CPU on which we actually called the END probe. This
13303 * allows user-land to be sure that this CPU's principal buffer is
13306 state
->dts_reserve
= 0;
13308 cookie
= dtrace_interrupt_disable();
13309 *cpu
= CPU
->cpu_id
;
13310 dtrace_probe(dtrace_probeid_end
,
13311 (uint64_t)(uintptr_t)state
, 0, 0, 0, 0);
13312 dtrace_interrupt_enable(cookie
);
13314 state
->dts_activity
= DTRACE_ACTIVITY_STOPPED
;
13321 dtrace_state_option(dtrace_state_t
*state
, dtrace_optid_t option
,
13322 dtrace_optval_t val
)
13324 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
13326 if (state
->dts_activity
!= DTRACE_ACTIVITY_INACTIVE
)
13329 if (option
>= DTRACEOPT_MAX
)
13332 if (option
!= DTRACEOPT_CPU
&& val
< 0)
13336 case DTRACEOPT_DESTRUCTIVE
:
13338 * Prevent consumers from enabling destructive actions if DTrace
13339 * is running in a restricted environment, or if actions are
13342 if (dtrace_is_restricted() || dtrace_destructive_disallow
)
13345 state
->dts_cred
.dcr_destructive
= 1;
13348 case DTRACEOPT_BUFSIZE
:
13349 case DTRACEOPT_DYNVARSIZE
:
13350 case DTRACEOPT_AGGSIZE
:
13351 case DTRACEOPT_SPECSIZE
:
13352 case DTRACEOPT_STRSIZE
:
13356 if (val
>= LONG_MAX
) {
13358 * If this is an otherwise negative value, set it to
13359 * the highest multiple of 128m less than LONG_MAX.
13360 * Technically, we're adjusting the size without
13361 * regard to the buffer resizing policy, but in fact,
13362 * this has no effect -- if we set the buffer size to
13363 * ~LONG_MAX and the buffer policy is ultimately set to
13364 * be "manual", the buffer allocation is guaranteed to
13365 * fail, if only because the allocation requires two
13366 * buffers. (We set the the size to the highest
13367 * multiple of 128m because it ensures that the size
13368 * will remain a multiple of a megabyte when
13369 * repeatedly halved -- all the way down to 15m.)
13371 val
= LONG_MAX
- (1 << 27) + 1;
13375 state
->dts_options
[option
] = val
;
13381 dtrace_state_destroy(dtrace_state_t
*state
)
13384 dtrace_vstate_t
*vstate
= &state
->dts_vstate
;
13385 minor_t minor
= getminor(state
->dts_dev
);
13386 int i
, bufsize
= (int)NCPU
* sizeof (dtrace_buffer_t
);
13387 dtrace_speculation_t
*spec
= state
->dts_speculations
;
13388 int nspec
= state
->dts_nspeculations
;
13391 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
13392 lck_mtx_assert(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
13395 * First, retract any retained enablings for this state.
13397 dtrace_enabling_retract(state
);
13398 ASSERT(state
->dts_nretained
== 0);
13400 if (state
->dts_activity
== DTRACE_ACTIVITY_ACTIVE
||
13401 state
->dts_activity
== DTRACE_ACTIVITY_DRAINING
) {
13403 * We have managed to come into dtrace_state_destroy() on a
13404 * hot enabling -- almost certainly because of a disorderly
13405 * shutdown of a consumer. (That is, a consumer that is
13406 * exiting without having called dtrace_stop().) In this case,
13407 * we're going to set our activity to be KILLED, and then
13408 * issue a sync to be sure that everyone is out of probe
13409 * context before we start blowing away ECBs.
13411 state
->dts_activity
= DTRACE_ACTIVITY_KILLED
;
13416 * Release the credential hold we took in dtrace_state_create().
13418 if (state
->dts_cred
.dcr_cred
!= NULL
)
13419 crfree(state
->dts_cred
.dcr_cred
);
13422 * Now we can safely disable and destroy any enabled probes. Because
13423 * any DTRACE_PRIV_KERNEL probes may actually be slowing our progress
13424 * (especially if they're all enabled), we take two passes through the
13425 * ECBs: in the first, we disable just DTRACE_PRIV_KERNEL probes, and
13426 * in the second we disable whatever is left over.
13428 for (match
= DTRACE_PRIV_KERNEL
; ; match
= 0) {
13429 for (i
= 0; i
< state
->dts_necbs
; i
++) {
13430 if ((ecb
= state
->dts_ecbs
[i
]) == NULL
)
13433 if (match
&& ecb
->dte_probe
!= NULL
) {
13434 dtrace_probe_t
*probe
= ecb
->dte_probe
;
13435 dtrace_provider_t
*prov
= probe
->dtpr_provider
;
13437 if (!(prov
->dtpv_priv
.dtpp_flags
& match
))
13441 dtrace_ecb_disable(ecb
);
13442 dtrace_ecb_destroy(ecb
);
13450 * Before we free the buffers, perform one more sync to assure that
13451 * every CPU is out of probe context.
13455 dtrace_buffer_free(state
->dts_buffer
);
13456 dtrace_buffer_free(state
->dts_aggbuffer
);
13458 for (i
= 0; i
< nspec
; i
++)
13459 dtrace_buffer_free(spec
[i
].dtsp_buffer
);
13461 if (state
->dts_cleaner
!= CYCLIC_NONE
)
13462 cyclic_remove(state
->dts_cleaner
);
13464 if (state
->dts_deadman
!= CYCLIC_NONE
)
13465 cyclic_remove(state
->dts_deadman
);
13467 dtrace_dstate_fini(&vstate
->dtvs_dynvars
);
13468 dtrace_vstate_fini(vstate
);
13469 kmem_free(state
->dts_ecbs
, state
->dts_necbs
* sizeof (dtrace_ecb_t
*));
13471 if (state
->dts_aggregations
!= NULL
) {
13473 for (i
= 0; i
< state
->dts_naggregations
; i
++)
13474 ASSERT(state
->dts_aggregations
[i
] == NULL
);
13476 ASSERT(state
->dts_naggregations
> 0);
13477 kmem_free(state
->dts_aggregations
,
13478 state
->dts_naggregations
* sizeof (dtrace_aggregation_t
*));
13481 kmem_free(state
->dts_buffer
, bufsize
);
13482 kmem_free(state
->dts_aggbuffer
, bufsize
);
13484 for (i
= 0; i
< nspec
; i
++)
13485 kmem_free(spec
[i
].dtsp_buffer
, bufsize
);
13487 kmem_free(spec
, nspec
* sizeof (dtrace_speculation_t
));
13489 dtrace_format_destroy(state
);
13491 vmem_destroy(state
->dts_aggid_arena
);
13492 ddi_soft_state_free(dtrace_softstate
, minor
);
13493 vmem_free(dtrace_minor
, (void *)(uintptr_t)minor
, 1);
13497 * DTrace Anonymous Enabling Functions
13499 static dtrace_state_t
*
13500 dtrace_anon_grab(void)
13502 dtrace_state_t
*state
;
13504 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
13506 if ((state
= dtrace_anon
.dta_state
) == NULL
) {
13507 ASSERT(dtrace_anon
.dta_enabling
== NULL
);
13511 ASSERT(dtrace_anon
.dta_enabling
!= NULL
);
13512 ASSERT(dtrace_retained
!= NULL
);
13514 dtrace_enabling_destroy(dtrace_anon
.dta_enabling
);
13515 dtrace_anon
.dta_enabling
= NULL
;
13516 dtrace_anon
.dta_state
= NULL
;
13522 dtrace_anon_property(void)
13525 dtrace_state_t
*state
;
13527 char c
[32]; /* enough for "dof-data-" + digits */
13529 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
13530 lck_mtx_assert(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
13532 for (i
= 0; ; i
++) {
13533 (void) snprintf(c
, sizeof (c
), "dof-data-%d", i
);
13535 dtrace_err_verbose
= 1;
13537 if ((dof
= dtrace_dof_property(c
)) == NULL
) {
13538 dtrace_err_verbose
= 0;
13543 * We want to create anonymous state, so we need to transition
13544 * the kernel debugger to indicate that DTrace is active. If
13545 * this fails (e.g. because the debugger has modified text in
13546 * some way), we won't continue with the processing.
13548 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE
) != 0) {
13549 cmn_err(CE_NOTE
, "kernel debugger active; anonymous "
13550 "enabling ignored.");
13551 dtrace_dof_destroy(dof
);
13556 * If we haven't allocated an anonymous state, we'll do so now.
13558 if ((state
= dtrace_anon
.dta_state
) == NULL
) {
13559 rv
= dtrace_state_create(NULL
, NULL
, &state
);
13560 dtrace_anon
.dta_state
= state
;
13561 if (rv
!= 0 || state
== NULL
) {
13563 * This basically shouldn't happen: the only
13564 * failure mode from dtrace_state_create() is a
13565 * failure of ddi_soft_state_zalloc() that
13566 * itself should never happen. Still, the
13567 * interface allows for a failure mode, and
13568 * we want to fail as gracefully as possible:
13569 * we'll emit an error message and cease
13570 * processing anonymous state in this case.
13572 cmn_err(CE_WARN
, "failed to create "
13573 "anonymous state");
13574 dtrace_dof_destroy(dof
);
13579 rv
= dtrace_dof_slurp(dof
, &state
->dts_vstate
, CRED(),
13580 &dtrace_anon
.dta_enabling
, 0, B_TRUE
);
13583 rv
= dtrace_dof_options(dof
, state
);
13585 dtrace_err_verbose
= 0;
13586 dtrace_dof_destroy(dof
);
13590 * This is malformed DOF; chuck any anonymous state
13593 ASSERT(dtrace_anon
.dta_enabling
== NULL
);
13594 dtrace_state_destroy(state
);
13595 dtrace_anon
.dta_state
= NULL
;
13599 ASSERT(dtrace_anon
.dta_enabling
!= NULL
);
13602 if (dtrace_anon
.dta_enabling
!= NULL
) {
13606 * dtrace_enabling_retain() can only fail because we are
13607 * trying to retain more enablings than are allowed -- but
13608 * we only have one anonymous enabling, and we are guaranteed
13609 * to be allowed at least one retained enabling; we assert
13610 * that dtrace_enabling_retain() returns success.
13612 rval
= dtrace_enabling_retain(dtrace_anon
.dta_enabling
);
13615 dtrace_enabling_dump(dtrace_anon
.dta_enabling
);
13620 * DTrace Helper Functions
13623 dtrace_helper_trace(dtrace_helper_action_t
*helper
,
13624 dtrace_mstate_t
*mstate
, dtrace_vstate_t
*vstate
, int where
)
13626 uint32_t size
, next
, nnext
;
13628 dtrace_helptrace_t
*ent
;
13629 uint16_t flags
= cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
13631 if (!dtrace_helptrace_enabled
)
13634 ASSERT((uint32_t)vstate
->dtvs_nlocals
<= dtrace_helptrace_nlocals
);
13637 * What would a tracing framework be without its own tracing
13638 * framework? (Well, a hell of a lot simpler, for starters...)
13640 size
= sizeof (dtrace_helptrace_t
) + dtrace_helptrace_nlocals
*
13641 sizeof (uint64_t) - sizeof (uint64_t);
13644 * Iterate until we can allocate a slot in the trace buffer.
13647 next
= dtrace_helptrace_next
;
13649 if (next
+ size
< dtrace_helptrace_bufsize
) {
13650 nnext
= next
+ size
;
13654 } while (dtrace_cas32(&dtrace_helptrace_next
, next
, nnext
) != next
);
13657 * We have our slot; fill it in.
13662 ent
= (dtrace_helptrace_t
*)&dtrace_helptrace_buffer
[next
];
13663 ent
->dtht_helper
= helper
;
13664 ent
->dtht_where
= where
;
13665 ent
->dtht_nlocals
= vstate
->dtvs_nlocals
;
13667 ent
->dtht_fltoffs
= (mstate
->dtms_present
& DTRACE_MSTATE_FLTOFFS
) ?
13668 mstate
->dtms_fltoffs
: -1;
13669 ent
->dtht_fault
= DTRACE_FLAGS2FLT(flags
);
13670 ent
->dtht_illval
= cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
;
13672 for (i
= 0; i
< vstate
->dtvs_nlocals
; i
++) {
13673 dtrace_statvar_t
*svar
;
13675 if ((svar
= vstate
->dtvs_locals
[i
]) == NULL
)
13678 ASSERT(svar
->dtsv_size
>= (int)NCPU
* sizeof (uint64_t));
13679 ent
->dtht_locals
[i
] =
13680 ((uint64_t *)(uintptr_t)svar
->dtsv_data
)[CPU
->cpu_id
];
13685 dtrace_helper(int which
, dtrace_mstate_t
*mstate
,
13686 dtrace_state_t
*state
, uint64_t arg0
, uint64_t arg1
)
13688 uint16_t *flags
= &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
13689 uint64_t sarg0
= mstate
->dtms_arg
[0];
13690 uint64_t sarg1
= mstate
->dtms_arg
[1];
13692 dtrace_helpers_t
*helpers
= curproc
->p_dtrace_helpers
;
13693 dtrace_helper_action_t
*helper
;
13694 dtrace_vstate_t
*vstate
;
13695 dtrace_difo_t
*pred
;
13696 int i
, trace
= dtrace_helptrace_enabled
;
13698 ASSERT(which
>= 0 && which
< DTRACE_NHELPER_ACTIONS
);
13700 if (helpers
== NULL
)
13703 if ((helper
= helpers
->dthps_actions
[which
]) == NULL
)
13706 vstate
= &helpers
->dthps_vstate
;
13707 mstate
->dtms_arg
[0] = arg0
;
13708 mstate
->dtms_arg
[1] = arg1
;
13711 * Now iterate over each helper. If its predicate evaluates to 'true',
13712 * we'll call the corresponding actions. Note that the below calls
13713 * to dtrace_dif_emulate() may set faults in machine state. This is
13714 * okay: our caller (the outer dtrace_dif_emulate()) will simply plow
13715 * the stored DIF offset with its own (which is the desired behavior).
13716 * Also, note the calls to dtrace_dif_emulate() may allocate scratch
13717 * from machine state; this is okay, too.
13719 for (; helper
!= NULL
; helper
= helper
->dtha_next
) {
13720 if ((pred
= helper
->dtha_predicate
) != NULL
) {
13722 dtrace_helper_trace(helper
, mstate
, vstate
, 0);
13724 if (!dtrace_dif_emulate(pred
, mstate
, vstate
, state
))
13727 if (*flags
& CPU_DTRACE_FAULT
)
13731 for (i
= 0; i
< helper
->dtha_nactions
; i
++) {
13733 dtrace_helper_trace(helper
,
13734 mstate
, vstate
, i
+ 1);
13736 rval
= dtrace_dif_emulate(helper
->dtha_actions
[i
],
13737 mstate
, vstate
, state
);
13739 if (*flags
& CPU_DTRACE_FAULT
)
13745 dtrace_helper_trace(helper
, mstate
, vstate
,
13746 DTRACE_HELPTRACE_NEXT
);
13750 dtrace_helper_trace(helper
, mstate
, vstate
,
13751 DTRACE_HELPTRACE_DONE
);
13754 * Restore the arg0 that we saved upon entry.
13756 mstate
->dtms_arg
[0] = sarg0
;
13757 mstate
->dtms_arg
[1] = sarg1
;
13763 dtrace_helper_trace(helper
, mstate
, vstate
,
13764 DTRACE_HELPTRACE_ERR
);
13767 * Restore the arg0 that we saved upon entry.
13769 mstate
->dtms_arg
[0] = sarg0
;
13770 mstate
->dtms_arg
[1] = sarg1
;
13776 dtrace_helper_action_destroy(dtrace_helper_action_t
*helper
,
13777 dtrace_vstate_t
*vstate
)
13781 if (helper
->dtha_predicate
!= NULL
)
13782 dtrace_difo_release(helper
->dtha_predicate
, vstate
);
13784 for (i
= 0; i
< helper
->dtha_nactions
; i
++) {
13785 ASSERT(helper
->dtha_actions
[i
] != NULL
);
13786 dtrace_difo_release(helper
->dtha_actions
[i
], vstate
);
13789 kmem_free(helper
->dtha_actions
,
13790 helper
->dtha_nactions
* sizeof (dtrace_difo_t
*));
13791 kmem_free(helper
, sizeof (dtrace_helper_action_t
));
13795 dtrace_helper_destroygen(proc_t
* p
, int gen
)
13797 dtrace_helpers_t
*help
= p
->p_dtrace_helpers
;
13798 dtrace_vstate_t
*vstate
;
13801 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
13803 if (help
== NULL
|| gen
> help
->dthps_generation
)
13806 vstate
= &help
->dthps_vstate
;
13808 for (i
= 0; i
< DTRACE_NHELPER_ACTIONS
; i
++) {
13809 dtrace_helper_action_t
*last
= NULL
, *h
, *next
;
13811 for (h
= help
->dthps_actions
[i
]; h
!= NULL
; h
= next
) {
13812 next
= h
->dtha_next
;
13814 if (h
->dtha_generation
== gen
) {
13815 if (last
!= NULL
) {
13816 last
->dtha_next
= next
;
13818 help
->dthps_actions
[i
] = next
;
13821 dtrace_helper_action_destroy(h
, vstate
);
13829 * Interate until we've cleared out all helper providers with the
13830 * given generation number.
13833 dtrace_helper_provider_t
*prov
= NULL
;
13836 * Look for a helper provider with the right generation. We
13837 * have to start back at the beginning of the list each time
13838 * because we drop dtrace_lock. It's unlikely that we'll make
13839 * more than two passes.
13841 for (i
= 0; i
< help
->dthps_nprovs
; i
++) {
13842 prov
= help
->dthps_provs
[i
];
13844 if (prov
->dthp_generation
== gen
)
13849 * If there were no matches, we're done.
13851 if (i
== help
->dthps_nprovs
)
13855 * Move the last helper provider into this slot.
13857 help
->dthps_nprovs
--;
13858 help
->dthps_provs
[i
] = help
->dthps_provs
[help
->dthps_nprovs
];
13859 help
->dthps_provs
[help
->dthps_nprovs
] = NULL
;
13861 lck_mtx_unlock(&dtrace_lock
);
13864 * If we have a meta provider, remove this helper provider.
13866 lck_mtx_lock(&dtrace_meta_lock
);
13867 if (dtrace_meta_pid
!= NULL
) {
13868 ASSERT(dtrace_deferred_pid
== NULL
);
13869 dtrace_helper_provider_remove(&prov
->dthp_prov
,
13872 lck_mtx_unlock(&dtrace_meta_lock
);
13874 dtrace_helper_provider_destroy(prov
);
13876 lck_mtx_lock(&dtrace_lock
);
13883 dtrace_helper_validate(dtrace_helper_action_t
*helper
)
13888 if ((dp
= helper
->dtha_predicate
) != NULL
)
13889 err
+= dtrace_difo_validate_helper(dp
);
13891 for (i
= 0; i
< helper
->dtha_nactions
; i
++)
13892 err
+= dtrace_difo_validate_helper(helper
->dtha_actions
[i
]);
13898 dtrace_helper_action_add(proc_t
* p
, int which
, dtrace_ecbdesc_t
*ep
)
13900 dtrace_helpers_t
*help
;
13901 dtrace_helper_action_t
*helper
, *last
;
13902 dtrace_actdesc_t
*act
;
13903 dtrace_vstate_t
*vstate
;
13904 dtrace_predicate_t
*pred
;
13905 int count
= 0, nactions
= 0, i
;
13907 if (which
< 0 || which
>= DTRACE_NHELPER_ACTIONS
)
13910 help
= p
->p_dtrace_helpers
;
13911 last
= help
->dthps_actions
[which
];
13912 vstate
= &help
->dthps_vstate
;
13914 for (count
= 0; last
!= NULL
; last
= last
->dtha_next
) {
13916 if (last
->dtha_next
== NULL
)
13921 * If we already have dtrace_helper_actions_max helper actions for this
13922 * helper action type, we'll refuse to add a new one.
13924 if (count
>= dtrace_helper_actions_max
)
13927 helper
= kmem_zalloc(sizeof (dtrace_helper_action_t
), KM_SLEEP
);
13928 helper
->dtha_generation
= help
->dthps_generation
;
13930 if ((pred
= ep
->dted_pred
.dtpdd_predicate
) != NULL
) {
13931 ASSERT(pred
->dtp_difo
!= NULL
);
13932 dtrace_difo_hold(pred
->dtp_difo
);
13933 helper
->dtha_predicate
= pred
->dtp_difo
;
13936 for (act
= ep
->dted_action
; act
!= NULL
; act
= act
->dtad_next
) {
13937 if (act
->dtad_kind
!= DTRACEACT_DIFEXPR
)
13940 if (act
->dtad_difo
== NULL
)
13946 helper
->dtha_actions
= kmem_zalloc(sizeof (dtrace_difo_t
*) *
13947 (helper
->dtha_nactions
= nactions
), KM_SLEEP
);
13949 for (act
= ep
->dted_action
, i
= 0; act
!= NULL
; act
= act
->dtad_next
) {
13950 dtrace_difo_hold(act
->dtad_difo
);
13951 helper
->dtha_actions
[i
++] = act
->dtad_difo
;
13954 if (!dtrace_helper_validate(helper
))
13957 if (last
== NULL
) {
13958 help
->dthps_actions
[which
] = helper
;
13960 last
->dtha_next
= helper
;
13963 if ((uint32_t)vstate
->dtvs_nlocals
> dtrace_helptrace_nlocals
) {
13964 dtrace_helptrace_nlocals
= vstate
->dtvs_nlocals
;
13965 dtrace_helptrace_next
= 0;
13970 dtrace_helper_action_destroy(helper
, vstate
);
13975 dtrace_helper_provider_register(proc_t
*p
, dtrace_helpers_t
*help
,
13976 dof_helper_t
*dofhp
)
13978 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_NOTOWNED
);
13980 lck_mtx_lock(&dtrace_meta_lock
);
13981 lck_mtx_lock(&dtrace_lock
);
13983 if (!dtrace_attached() || dtrace_meta_pid
== NULL
) {
13985 * If the dtrace module is loaded but not attached, or if
13986 * there aren't isn't a meta provider registered to deal with
13987 * these provider descriptions, we need to postpone creating
13988 * the actual providers until later.
13991 if (help
->dthps_next
== NULL
&& help
->dthps_prev
== NULL
&&
13992 dtrace_deferred_pid
!= help
) {
13993 help
->dthps_deferred
= 1;
13994 help
->dthps_pid
= p
->p_pid
;
13995 help
->dthps_next
= dtrace_deferred_pid
;
13996 help
->dthps_prev
= NULL
;
13997 if (dtrace_deferred_pid
!= NULL
)
13998 dtrace_deferred_pid
->dthps_prev
= help
;
13999 dtrace_deferred_pid
= help
;
14002 lck_mtx_unlock(&dtrace_lock
);
14004 } else if (dofhp
!= NULL
) {
14006 * If the dtrace module is loaded and we have a particular
14007 * helper provider description, pass that off to the
14011 lck_mtx_unlock(&dtrace_lock
);
14013 dtrace_helper_provide(dofhp
, p
->p_pid
);
14017 * Otherwise, just pass all the helper provider descriptions
14018 * off to the meta provider.
14022 lck_mtx_unlock(&dtrace_lock
);
14024 for (i
= 0; i
< help
->dthps_nprovs
; i
++) {
14025 dtrace_helper_provide(&help
->dthps_provs
[i
]->dthp_prov
,
14030 lck_mtx_unlock(&dtrace_meta_lock
);
14034 dtrace_helper_provider_add(proc_t
* p
, dof_helper_t
*dofhp
, int gen
)
14036 dtrace_helpers_t
*help
;
14037 dtrace_helper_provider_t
*hprov
, **tmp_provs
;
14038 uint_t tmp_maxprovs
, i
;
14040 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
14041 help
= p
->p_dtrace_helpers
;
14042 ASSERT(help
!= NULL
);
14045 * If we already have dtrace_helper_providers_max helper providers,
14046 * we're refuse to add a new one.
14048 if (help
->dthps_nprovs
>= dtrace_helper_providers_max
)
14052 * Check to make sure this isn't a duplicate.
14054 for (i
= 0; i
< help
->dthps_nprovs
; i
++) {
14055 if (dofhp
->dofhp_addr
==
14056 help
->dthps_provs
[i
]->dthp_prov
.dofhp_addr
)
14060 hprov
= kmem_zalloc(sizeof (dtrace_helper_provider_t
), KM_SLEEP
);
14061 hprov
->dthp_prov
= *dofhp
;
14062 hprov
->dthp_ref
= 1;
14063 hprov
->dthp_generation
= gen
;
14066 * Allocate a bigger table for helper providers if it's already full.
14068 if (help
->dthps_maxprovs
== help
->dthps_nprovs
) {
14069 tmp_maxprovs
= help
->dthps_maxprovs
;
14070 tmp_provs
= help
->dthps_provs
;
14072 if (help
->dthps_maxprovs
== 0)
14073 help
->dthps_maxprovs
= 2;
14075 help
->dthps_maxprovs
*= 2;
14076 if (help
->dthps_maxprovs
> dtrace_helper_providers_max
)
14077 help
->dthps_maxprovs
= dtrace_helper_providers_max
;
14079 ASSERT(tmp_maxprovs
< help
->dthps_maxprovs
);
14081 help
->dthps_provs
= kmem_zalloc(help
->dthps_maxprovs
*
14082 sizeof (dtrace_helper_provider_t
*), KM_SLEEP
);
14084 if (tmp_provs
!= NULL
) {
14085 bcopy(tmp_provs
, help
->dthps_provs
, tmp_maxprovs
*
14086 sizeof (dtrace_helper_provider_t
*));
14087 kmem_free(tmp_provs
, tmp_maxprovs
*
14088 sizeof (dtrace_helper_provider_t
*));
14092 help
->dthps_provs
[help
->dthps_nprovs
] = hprov
;
14093 help
->dthps_nprovs
++;
14099 dtrace_helper_provider_destroy(dtrace_helper_provider_t
*hprov
)
14101 lck_mtx_lock(&dtrace_lock
);
14103 if (--hprov
->dthp_ref
== 0) {
14105 lck_mtx_unlock(&dtrace_lock
);
14106 dof
= (dof_hdr_t
*)(uintptr_t)hprov
->dthp_prov
.dofhp_dof
;
14107 dtrace_dof_destroy(dof
);
14108 kmem_free(hprov
, sizeof (dtrace_helper_provider_t
));
14110 lck_mtx_unlock(&dtrace_lock
);
14115 dtrace_helper_provider_validate(dof_hdr_t
*dof
, dof_sec_t
*sec
)
14117 uintptr_t daddr
= (uintptr_t)dof
;
14118 dof_sec_t
*str_sec
, *prb_sec
, *arg_sec
, *off_sec
, *enoff_sec
;
14119 dof_provider_t
*provider
;
14120 dof_probe_t
*probe
;
14122 char *strtab
, *typestr
;
14123 dof_stridx_t typeidx
;
14125 uint_t nprobes
, j
, k
;
14127 ASSERT(sec
->dofs_type
== DOF_SECT_PROVIDER
);
14129 if (sec
->dofs_offset
& (sizeof (uint_t
) - 1)) {
14130 dtrace_dof_error(dof
, "misaligned section offset");
14135 * The section needs to be large enough to contain the DOF provider
14136 * structure appropriate for the given version.
14138 if (sec
->dofs_size
<
14139 ((dof
->dofh_ident
[DOF_ID_VERSION
] == DOF_VERSION_1
) ?
14140 offsetof(dof_provider_t
, dofpv_prenoffs
) :
14141 sizeof (dof_provider_t
))) {
14142 dtrace_dof_error(dof
, "provider section too small");
14146 provider
= (dof_provider_t
*)(uintptr_t)(daddr
+ sec
->dofs_offset
);
14147 str_sec
= dtrace_dof_sect(dof
, DOF_SECT_STRTAB
, provider
->dofpv_strtab
);
14148 prb_sec
= dtrace_dof_sect(dof
, DOF_SECT_PROBES
, provider
->dofpv_probes
);
14149 arg_sec
= dtrace_dof_sect(dof
, DOF_SECT_PRARGS
, provider
->dofpv_prargs
);
14150 off_sec
= dtrace_dof_sect(dof
, DOF_SECT_PROFFS
, provider
->dofpv_proffs
);
14152 if (str_sec
== NULL
|| prb_sec
== NULL
||
14153 arg_sec
== NULL
|| off_sec
== NULL
)
14158 if (dof
->dofh_ident
[DOF_ID_VERSION
] != DOF_VERSION_1
&&
14159 provider
->dofpv_prenoffs
!= DOF_SECT_NONE
&&
14160 (enoff_sec
= dtrace_dof_sect(dof
, DOF_SECT_PRENOFFS
,
14161 provider
->dofpv_prenoffs
)) == NULL
)
14164 strtab
= (char *)(uintptr_t)(daddr
+ str_sec
->dofs_offset
);
14166 if (provider
->dofpv_name
>= str_sec
->dofs_size
||
14167 strlen(strtab
+ provider
->dofpv_name
) >= DTRACE_PROVNAMELEN
) {
14168 dtrace_dof_error(dof
, "invalid provider name");
14172 if (prb_sec
->dofs_entsize
== 0 ||
14173 prb_sec
->dofs_entsize
> prb_sec
->dofs_size
) {
14174 dtrace_dof_error(dof
, "invalid entry size");
14178 if (prb_sec
->dofs_entsize
& (sizeof (uintptr_t) - 1)) {
14179 dtrace_dof_error(dof
, "misaligned entry size");
14183 if (off_sec
->dofs_entsize
!= sizeof (uint32_t)) {
14184 dtrace_dof_error(dof
, "invalid entry size");
14188 if (off_sec
->dofs_offset
& (sizeof (uint32_t) - 1)) {
14189 dtrace_dof_error(dof
, "misaligned section offset");
14193 if (arg_sec
->dofs_entsize
!= sizeof (uint8_t)) {
14194 dtrace_dof_error(dof
, "invalid entry size");
14198 arg
= (uint8_t *)(uintptr_t)(daddr
+ arg_sec
->dofs_offset
);
14200 nprobes
= prb_sec
->dofs_size
/ prb_sec
->dofs_entsize
;
14203 * Take a pass through the probes to check for errors.
14205 for (j
= 0; j
< nprobes
; j
++) {
14206 probe
= (dof_probe_t
*)(uintptr_t)(daddr
+
14207 prb_sec
->dofs_offset
+ j
* prb_sec
->dofs_entsize
);
14209 if (probe
->dofpr_func
>= str_sec
->dofs_size
) {
14210 dtrace_dof_error(dof
, "invalid function name");
14214 if (strlen(strtab
+ probe
->dofpr_func
) >= DTRACE_FUNCNAMELEN
) {
14215 dtrace_dof_error(dof
, "function name too long");
14219 if (probe
->dofpr_name
>= str_sec
->dofs_size
||
14220 strlen(strtab
+ probe
->dofpr_name
) >= DTRACE_NAMELEN
) {
14221 dtrace_dof_error(dof
, "invalid probe name");
14226 * The offset count must not wrap the index, and the offsets
14227 * must also not overflow the section's data.
14229 if (probe
->dofpr_offidx
+ probe
->dofpr_noffs
<
14230 probe
->dofpr_offidx
||
14231 (probe
->dofpr_offidx
+ probe
->dofpr_noffs
) *
14232 off_sec
->dofs_entsize
> off_sec
->dofs_size
) {
14233 dtrace_dof_error(dof
, "invalid probe offset");
14237 if (dof
->dofh_ident
[DOF_ID_VERSION
] != DOF_VERSION_1
) {
14239 * If there's no is-enabled offset section, make sure
14240 * there aren't any is-enabled offsets. Otherwise
14241 * perform the same checks as for probe offsets
14242 * (immediately above).
14244 if (enoff_sec
== NULL
) {
14245 if (probe
->dofpr_enoffidx
!= 0 ||
14246 probe
->dofpr_nenoffs
!= 0) {
14247 dtrace_dof_error(dof
, "is-enabled "
14248 "offsets with null section");
14251 } else if (probe
->dofpr_enoffidx
+
14252 probe
->dofpr_nenoffs
< probe
->dofpr_enoffidx
||
14253 (probe
->dofpr_enoffidx
+ probe
->dofpr_nenoffs
) *
14254 enoff_sec
->dofs_entsize
> enoff_sec
->dofs_size
) {
14255 dtrace_dof_error(dof
, "invalid is-enabled "
14260 if (probe
->dofpr_noffs
+ probe
->dofpr_nenoffs
== 0) {
14261 dtrace_dof_error(dof
, "zero probe and "
14262 "is-enabled offsets");
14265 } else if (probe
->dofpr_noffs
== 0) {
14266 dtrace_dof_error(dof
, "zero probe offsets");
14270 if (probe
->dofpr_argidx
+ probe
->dofpr_xargc
<
14271 probe
->dofpr_argidx
||
14272 (probe
->dofpr_argidx
+ probe
->dofpr_xargc
) *
14273 arg_sec
->dofs_entsize
> arg_sec
->dofs_size
) {
14274 dtrace_dof_error(dof
, "invalid args");
14278 typeidx
= probe
->dofpr_nargv
;
14279 typestr
= strtab
+ probe
->dofpr_nargv
;
14280 for (k
= 0; k
< probe
->dofpr_nargc
; k
++) {
14281 if (typeidx
>= str_sec
->dofs_size
) {
14282 dtrace_dof_error(dof
, "bad "
14283 "native argument type");
14287 typesz
= strlen(typestr
) + 1;
14288 if (typesz
> DTRACE_ARGTYPELEN
) {
14289 dtrace_dof_error(dof
, "native "
14290 "argument type too long");
14297 typeidx
= probe
->dofpr_xargv
;
14298 typestr
= strtab
+ probe
->dofpr_xargv
;
14299 for (k
= 0; k
< probe
->dofpr_xargc
; k
++) {
14300 if (arg
[probe
->dofpr_argidx
+ k
] > probe
->dofpr_nargc
) {
14301 dtrace_dof_error(dof
, "bad "
14302 "native argument index");
14306 if (typeidx
>= str_sec
->dofs_size
) {
14307 dtrace_dof_error(dof
, "bad "
14308 "translated argument type");
14312 typesz
= strlen(typestr
) + 1;
14313 if (typesz
> DTRACE_ARGTYPELEN
) {
14314 dtrace_dof_error(dof
, "translated argument "
14328 dtrace_helper_slurp(proc_t
* p
, dof_hdr_t
*dof
, dof_helper_t
*dhp
)
14330 dtrace_helpers_t
*help
;
14331 dtrace_vstate_t
*vstate
;
14332 dtrace_enabling_t
*enab
= NULL
;
14333 int i
, gen
, rv
, nhelpers
= 0, nprovs
= 0, destroy
= 1;
14334 uintptr_t daddr
= (uintptr_t)dof
;
14336 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
14338 if ((help
= p
->p_dtrace_helpers
) == NULL
)
14339 help
= dtrace_helpers_create(p
);
14341 vstate
= &help
->dthps_vstate
;
14343 if ((rv
= dtrace_dof_slurp(dof
, vstate
, NULL
, &enab
,
14344 dhp
!= NULL
? dhp
->dofhp_addr
: 0, B_FALSE
)) != 0) {
14345 dtrace_dof_destroy(dof
);
14350 * Look for helper providers and validate their descriptions.
14353 for (i
= 0; (uint32_t)i
< dof
->dofh_secnum
; i
++) {
14354 dof_sec_t
*sec
= (dof_sec_t
*)(uintptr_t)(daddr
+
14355 dof
->dofh_secoff
+ i
* dof
->dofh_secsize
);
14357 if (sec
->dofs_type
!= DOF_SECT_PROVIDER
)
14360 if (dtrace_helper_provider_validate(dof
, sec
) != 0) {
14361 dtrace_enabling_destroy(enab
);
14362 dtrace_dof_destroy(dof
);
14371 * Now we need to walk through the ECB descriptions in the enabling.
14373 for (i
= 0; i
< enab
->dten_ndesc
; i
++) {
14374 dtrace_ecbdesc_t
*ep
= enab
->dten_desc
[i
];
14375 dtrace_probedesc_t
*desc
= &ep
->dted_probe
;
14377 /* APPLE NOTE: Darwin employs size bounded string operation. */
14378 if (!LIT_STRNEQL(desc
->dtpd_provider
, "dtrace"))
14381 if (!LIT_STRNEQL(desc
->dtpd_mod
, "helper"))
14384 if (!LIT_STRNEQL(desc
->dtpd_func
, "ustack"))
14387 if ((rv
= dtrace_helper_action_add(p
, DTRACE_HELPER_ACTION_USTACK
,
14390 * Adding this helper action failed -- we are now going
14391 * to rip out the entire generation and return failure.
14393 (void) dtrace_helper_destroygen(p
, help
->dthps_generation
);
14394 dtrace_enabling_destroy(enab
);
14395 dtrace_dof_destroy(dof
);
14402 if (nhelpers
< enab
->dten_ndesc
)
14403 dtrace_dof_error(dof
, "unmatched helpers");
14405 gen
= help
->dthps_generation
++;
14406 dtrace_enabling_destroy(enab
);
14408 if (dhp
!= NULL
&& nprovs
> 0) {
14409 dhp
->dofhp_dof
= (uint64_t)(uintptr_t)dof
;
14410 if (dtrace_helper_provider_add(p
, dhp
, gen
) == 0) {
14411 lck_mtx_unlock(&dtrace_lock
);
14412 dtrace_helper_provider_register(p
, help
, dhp
);
14413 lck_mtx_lock(&dtrace_lock
);
14420 dtrace_dof_destroy(dof
);
14426 * APPLE NOTE: DTrace lazy dof implementation
14428 * DTrace user static probes (USDT probes) and helper actions are loaded
14429 * in a process by proccessing dof sections. The dof sections are passed
14430 * into the kernel by dyld, in a dof_ioctl_data_t block. It is rather
14431 * expensive to process dof for a process that will never use it. There
14432 * is a memory cost (allocating the providers/probes), and a cpu cost
14433 * (creating the providers/probes).
14435 * To reduce this cost, we use "lazy dof". The normal proceedure for
14436 * dof processing is to copyin the dof(s) pointed to by the dof_ioctl_data_t
14437 * block, and invoke dof_slurp_helper() on them. When "lazy dof" is
14438 * used, each process retains the dof_ioctl_data_t block, instead of
14439 * copying in the data it points to.
14441 * The dof_ioctl_data_t blocks are managed as if they were the actual
14442 * processed dof; on fork the block is copied to the child, on exec and
14443 * exit the block is freed.
14445 * If the process loads library(s) containing additional dof, the
14446 * new dof_ioctl_data_t is merged with the existing block.
14448 * There are a few catches that make this slightly more difficult.
14449 * When dyld registers dof_ioctl_data_t blocks, it expects a unique
14450 * identifier value for each dof in the block. In non-lazy dof terms,
14451 * this is the generation that dof was loaded in. If we hand back
14452 * a UID for a lazy dof, that same UID must be able to unload the
14453 * dof once it has become non-lazy. To meet this requirement, the
14454 * code that loads lazy dof requires that the UID's for dof(s) in
14455 * the lazy dof be sorted, and in ascending order. It is okay to skip
14456 * UID's, I.E., 1 -> 5 -> 6 is legal.
14458 * Once a process has become non-lazy, it will stay non-lazy. All
14459 * future dof operations for that process will be non-lazy, even
14460 * if the dof mode transitions back to lazy.
14462 * Always do lazy dof checks before non-lazy (I.E. In fork, exit, exec.).
14463 * That way if the lazy check fails due to transitioning to non-lazy, the
14464 * right thing is done with the newly faulted in dof.
14468 * This method is a bit squicky. It must handle:
14470 * dof should not be lazy.
14471 * dof should have been handled lazily, but there was an error
14472 * dof was handled lazily, and needs to be freed.
14473 * dof was handled lazily, and must not be freed.
14476 * Returns EACCESS if dof should be handled non-lazily.
14478 * KERN_SUCCESS and all other return codes indicate lazy handling of dof.
14480 * If the dofs data is claimed by this method, dofs_claimed will be set.
14481 * Callers should not free claimed dofs.
14484 dtrace_lazy_dofs_add(proc_t
*p
, dof_ioctl_data_t
* incoming_dofs
, int *dofs_claimed
)
14487 ASSERT(incoming_dofs
&& incoming_dofs
->dofiod_count
> 0);
14492 lck_rw_lock_shared(&dtrace_dof_mode_lock
);
14495 * If we have lazy dof, dof mode better be LAZY_ON.
14497 ASSERT(p
->p_dtrace_lazy_dofs
== NULL
|| dtrace_dof_mode
== DTRACE_DOF_MODE_LAZY_ON
);
14498 ASSERT(p
->p_dtrace_lazy_dofs
== NULL
|| p
->p_dtrace_helpers
== NULL
);
14499 ASSERT(dtrace_dof_mode
!= DTRACE_DOF_MODE_NEVER
);
14502 * Any existing helpers force non-lazy behavior.
14504 if (dtrace_dof_mode
== DTRACE_DOF_MODE_LAZY_ON
&& (p
->p_dtrace_helpers
== NULL
)) {
14505 lck_mtx_lock(&p
->p_dtrace_sprlock
);
14507 dof_ioctl_data_t
* existing_dofs
= p
->p_dtrace_lazy_dofs
;
14508 unsigned int existing_dofs_count
= (existing_dofs
) ? existing_dofs
->dofiod_count
: 0;
14509 unsigned int i
, merged_dofs_count
= incoming_dofs
->dofiod_count
+ existing_dofs_count
;
14514 if (merged_dofs_count
== 0 || merged_dofs_count
> 1024) {
14515 dtrace_dof_error(NULL
, "lazy_dofs_add merged_dofs_count out of range");
14521 * Each dof being added must be assigned a unique generation.
14523 uint64_t generation
= (existing_dofs
) ? existing_dofs
->dofiod_helpers
[existing_dofs_count
- 1].dofhp_dof
+ 1 : 1;
14524 for (i
=0; i
<incoming_dofs
->dofiod_count
; i
++) {
14526 * We rely on these being the same so we can overwrite dofhp_dof and not lose info.
14528 ASSERT(incoming_dofs
->dofiod_helpers
[i
].dofhp_dof
== incoming_dofs
->dofiod_helpers
[i
].dofhp_addr
);
14529 incoming_dofs
->dofiod_helpers
[i
].dofhp_dof
= generation
++;
14533 if (existing_dofs
) {
14535 * Merge the existing and incoming dofs
14537 size_t merged_dofs_size
= DOF_IOCTL_DATA_T_SIZE(merged_dofs_count
);
14538 dof_ioctl_data_t
* merged_dofs
= kmem_alloc(merged_dofs_size
, KM_SLEEP
);
14540 bcopy(&existing_dofs
->dofiod_helpers
[0],
14541 &merged_dofs
->dofiod_helpers
[0],
14542 sizeof(dof_helper_t
) * existing_dofs_count
);
14543 bcopy(&incoming_dofs
->dofiod_helpers
[0],
14544 &merged_dofs
->dofiod_helpers
[existing_dofs_count
],
14545 sizeof(dof_helper_t
) * incoming_dofs
->dofiod_count
);
14547 merged_dofs
->dofiod_count
= merged_dofs_count
;
14549 kmem_free(existing_dofs
, DOF_IOCTL_DATA_T_SIZE(existing_dofs_count
));
14551 p
->p_dtrace_lazy_dofs
= merged_dofs
;
14554 * Claim the incoming dofs
14557 p
->p_dtrace_lazy_dofs
= incoming_dofs
;
14561 dof_ioctl_data_t
* all_dofs
= p
->p_dtrace_lazy_dofs
;
14562 for (i
=0; i
<all_dofs
->dofiod_count
-1; i
++) {
14563 ASSERT(all_dofs
->dofiod_helpers
[i
].dofhp_dof
< all_dofs
->dofiod_helpers
[i
+1].dofhp_dof
);
14568 lck_mtx_unlock(&p
->p_dtrace_sprlock
);
14573 lck_rw_unlock_shared(&dtrace_dof_mode_lock
);
14581 * EINVAL: lazy dof is enabled, but the requested generation was not found.
14582 * EACCES: This removal needs to be handled non-lazily.
14585 dtrace_lazy_dofs_remove(proc_t
*p
, int generation
)
14589 lck_rw_lock_shared(&dtrace_dof_mode_lock
);
14592 * If we have lazy dof, dof mode better be LAZY_ON.
14594 ASSERT(p
->p_dtrace_lazy_dofs
== NULL
|| dtrace_dof_mode
== DTRACE_DOF_MODE_LAZY_ON
);
14595 ASSERT(p
->p_dtrace_lazy_dofs
== NULL
|| p
->p_dtrace_helpers
== NULL
);
14596 ASSERT(dtrace_dof_mode
!= DTRACE_DOF_MODE_NEVER
);
14599 * Any existing helpers force non-lazy behavior.
14601 if (dtrace_dof_mode
== DTRACE_DOF_MODE_LAZY_ON
&& (p
->p_dtrace_helpers
== NULL
)) {
14602 lck_mtx_lock(&p
->p_dtrace_sprlock
);
14604 dof_ioctl_data_t
* existing_dofs
= p
->p_dtrace_lazy_dofs
;
14606 if (existing_dofs
) {
14607 int index
, existing_dofs_count
= existing_dofs
->dofiod_count
;
14608 for (index
=0; index
<existing_dofs_count
; index
++) {
14609 if ((int)existing_dofs
->dofiod_helpers
[index
].dofhp_dof
== generation
) {
14610 dof_ioctl_data_t
* removed_dofs
= NULL
;
14613 * If there is only 1 dof, we'll delete it and swap in NULL.
14615 if (existing_dofs_count
> 1) {
14616 int removed_dofs_count
= existing_dofs_count
- 1;
14617 size_t removed_dofs_size
= DOF_IOCTL_DATA_T_SIZE(removed_dofs_count
);
14619 removed_dofs
= kmem_alloc(removed_dofs_size
, KM_SLEEP
);
14620 removed_dofs
->dofiod_count
= removed_dofs_count
;
14623 * copy the remaining data.
14626 bcopy(&existing_dofs
->dofiod_helpers
[0],
14627 &removed_dofs
->dofiod_helpers
[0],
14628 index
* sizeof(dof_helper_t
));
14631 if (index
< existing_dofs_count
-1) {
14632 bcopy(&existing_dofs
->dofiod_helpers
[index
+1],
14633 &removed_dofs
->dofiod_helpers
[index
],
14634 (existing_dofs_count
- index
- 1) * sizeof(dof_helper_t
));
14638 kmem_free(existing_dofs
, DOF_IOCTL_DATA_T_SIZE(existing_dofs_count
));
14640 p
->p_dtrace_lazy_dofs
= removed_dofs
;
14642 rval
= KERN_SUCCESS
;
14649 dof_ioctl_data_t
* all_dofs
= p
->p_dtrace_lazy_dofs
;
14652 for (i
=0; i
<all_dofs
->dofiod_count
-1; i
++) {
14653 ASSERT(all_dofs
->dofiod_helpers
[i
].dofhp_dof
< all_dofs
->dofiod_helpers
[i
+1].dofhp_dof
);
14660 lck_mtx_unlock(&p
->p_dtrace_sprlock
);
14665 lck_rw_unlock_shared(&dtrace_dof_mode_lock
);
14671 dtrace_lazy_dofs_destroy(proc_t
*p
)
14673 lck_rw_lock_shared(&dtrace_dof_mode_lock
);
14674 lck_mtx_lock(&p
->p_dtrace_sprlock
);
14677 * If we have lazy dof, dof mode better be LAZY_ON, or we must be exiting.
14678 * We cannot assert against DTRACE_DOF_MODE_NEVER here, because we are called from
14679 * kern_exit.c and kern_exec.c.
14681 ASSERT(p
->p_dtrace_lazy_dofs
== NULL
|| dtrace_dof_mode
== DTRACE_DOF_MODE_LAZY_ON
|| p
->p_lflag
& P_LEXIT
);
14682 ASSERT(p
->p_dtrace_lazy_dofs
== NULL
|| p
->p_dtrace_helpers
== NULL
);
14684 dof_ioctl_data_t
* lazy_dofs
= p
->p_dtrace_lazy_dofs
;
14685 p
->p_dtrace_lazy_dofs
= NULL
;
14687 lck_mtx_unlock(&p
->p_dtrace_sprlock
);
14688 lck_rw_unlock_shared(&dtrace_dof_mode_lock
);
14691 kmem_free(lazy_dofs
, DOF_IOCTL_DATA_T_SIZE(lazy_dofs
->dofiod_count
));
14696 dtrace_lazy_dofs_duplicate(proc_t
*parent
, proc_t
*child
)
14698 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_NOTOWNED
);
14699 lck_mtx_assert(&parent
->p_dtrace_sprlock
, LCK_MTX_ASSERT_NOTOWNED
);
14700 lck_mtx_assert(&child
->p_dtrace_sprlock
, LCK_MTX_ASSERT_NOTOWNED
);
14702 lck_rw_lock_shared(&dtrace_dof_mode_lock
);
14703 lck_mtx_lock(&parent
->p_dtrace_sprlock
);
14706 * If we have lazy dof, dof mode better be LAZY_ON, or we must be exiting.
14707 * We cannot assert against DTRACE_DOF_MODE_NEVER here, because we are called from
14710 ASSERT(parent
->p_dtrace_lazy_dofs
== NULL
|| dtrace_dof_mode
== DTRACE_DOF_MODE_LAZY_ON
);
14711 ASSERT(parent
->p_dtrace_lazy_dofs
== NULL
|| parent
->p_dtrace_helpers
== NULL
);
14713 * In theory we should hold the child sprlock, but this is safe...
14715 ASSERT(child
->p_dtrace_lazy_dofs
== NULL
&& child
->p_dtrace_helpers
== NULL
);
14717 dof_ioctl_data_t
* parent_dofs
= parent
->p_dtrace_lazy_dofs
;
14718 dof_ioctl_data_t
* child_dofs
= NULL
;
14720 size_t parent_dofs_size
= DOF_IOCTL_DATA_T_SIZE(parent_dofs
->dofiod_count
);
14721 child_dofs
= kmem_alloc(parent_dofs_size
, KM_SLEEP
);
14722 bcopy(parent_dofs
, child_dofs
, parent_dofs_size
);
14725 lck_mtx_unlock(&parent
->p_dtrace_sprlock
);
14728 lck_mtx_lock(&child
->p_dtrace_sprlock
);
14729 child
->p_dtrace_lazy_dofs
= child_dofs
;
14730 lck_mtx_unlock(&child
->p_dtrace_sprlock
);
14733 lck_rw_unlock_shared(&dtrace_dof_mode_lock
);
14737 dtrace_lazy_dofs_proc_iterate_filter(proc_t
*p
, void* ignored
)
14739 #pragma unused(ignored)
14741 * Okay to NULL test without taking the sprlock.
14743 return p
->p_dtrace_lazy_dofs
!= NULL
;
14747 dtrace_lazy_dofs_proc_iterate_doit(proc_t
*p
, void* ignored
)
14749 #pragma unused(ignored)
14751 * It is possible this process may exit during our attempt to
14752 * fault in the dof. We could fix this by holding locks longer,
14753 * but the errors are benign.
14755 lck_mtx_lock(&p
->p_dtrace_sprlock
);
14758 * In this case only, it is okay to have lazy dof when dof mode is DTRACE_DOF_MODE_LAZY_OFF
14760 ASSERT(p
->p_dtrace_lazy_dofs
== NULL
|| p
->p_dtrace_helpers
== NULL
);
14761 ASSERT(dtrace_dof_mode
== DTRACE_DOF_MODE_LAZY_OFF
);
14764 dof_ioctl_data_t
* lazy_dofs
= p
->p_dtrace_lazy_dofs
;
14765 p
->p_dtrace_lazy_dofs
= NULL
;
14767 lck_mtx_unlock(&p
->p_dtrace_sprlock
);
14770 * Process each dof_helper_t
14772 if (lazy_dofs
!= NULL
) {
14776 for (i
=0; i
<lazy_dofs
->dofiod_count
; i
++) {
14778 * When loading lazy dof, we depend on the generations being sorted in ascending order.
14780 ASSERT(i
>= (lazy_dofs
->dofiod_count
- 1) || lazy_dofs
->dofiod_helpers
[i
].dofhp_dof
< lazy_dofs
->dofiod_helpers
[i
+1].dofhp_dof
);
14782 dof_helper_t
*dhp
= &lazy_dofs
->dofiod_helpers
[i
];
14785 * We stored the generation in dofhp_dof. Save it, and restore the original value.
14787 int generation
= dhp
->dofhp_dof
;
14788 dhp
->dofhp_dof
= dhp
->dofhp_addr
;
14790 dof_hdr_t
*dof
= dtrace_dof_copyin_from_proc(p
, dhp
->dofhp_dof
, &rval
);
14793 dtrace_helpers_t
*help
;
14795 lck_mtx_lock(&dtrace_lock
);
14798 * This must be done with the dtrace_lock held
14800 if ((help
= p
->p_dtrace_helpers
) == NULL
)
14801 help
= dtrace_helpers_create(p
);
14804 * If the generation value has been bumped, someone snuck in
14805 * when we released the dtrace lock. We have to dump this generation,
14806 * there is no safe way to load it.
14808 if (help
->dthps_generation
<= generation
) {
14809 help
->dthps_generation
= generation
;
14812 * dtrace_helper_slurp() takes responsibility for the dof --
14813 * it may free it now or it may save it and free it later.
14815 if ((rval
= dtrace_helper_slurp(p
, dof
, dhp
)) != generation
) {
14816 dtrace_dof_error(NULL
, "returned value did not match expected generation");
14820 lck_mtx_unlock(&dtrace_lock
);
14824 kmem_free(lazy_dofs
, DOF_IOCTL_DATA_T_SIZE(lazy_dofs
->dofiod_count
));
14827 return PROC_RETURNED
;
14830 static dtrace_helpers_t
*
14831 dtrace_helpers_create(proc_t
*p
)
14833 dtrace_helpers_t
*help
;
14835 lck_mtx_assert(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
14836 ASSERT(p
->p_dtrace_helpers
== NULL
);
14838 help
= kmem_zalloc(sizeof (dtrace_helpers_t
), KM_SLEEP
);
14839 help
->dthps_actions
= kmem_zalloc(sizeof (dtrace_helper_action_t
*) *
14840 DTRACE_NHELPER_ACTIONS
, KM_SLEEP
);
14842 p
->p_dtrace_helpers
= help
;
14849 dtrace_helpers_destroy(proc_t
* p
)
14851 dtrace_helpers_t
*help
;
14852 dtrace_vstate_t
*vstate
;
14855 lck_mtx_lock(&dtrace_lock
);
14857 ASSERT(p
->p_dtrace_helpers
!= NULL
);
14858 ASSERT(dtrace_helpers
> 0);
14860 help
= p
->p_dtrace_helpers
;
14861 vstate
= &help
->dthps_vstate
;
14864 * We're now going to lose the help from this process.
14866 p
->p_dtrace_helpers
= NULL
;
14870 * Destory the helper actions.
14872 for (i
= 0; i
< DTRACE_NHELPER_ACTIONS
; i
++) {
14873 dtrace_helper_action_t
*h
, *next
;
14875 for (h
= help
->dthps_actions
[i
]; h
!= NULL
; h
= next
) {
14876 next
= h
->dtha_next
;
14877 dtrace_helper_action_destroy(h
, vstate
);
14882 lck_mtx_unlock(&dtrace_lock
);
14885 * Destroy the helper providers.
14887 if (help
->dthps_maxprovs
> 0) {
14888 lck_mtx_lock(&dtrace_meta_lock
);
14889 if (dtrace_meta_pid
!= NULL
) {
14890 ASSERT(dtrace_deferred_pid
== NULL
);
14892 for (i
= 0; i
< help
->dthps_nprovs
; i
++) {
14893 dtrace_helper_provider_remove(
14894 &help
->dthps_provs
[i
]->dthp_prov
, p
->p_pid
);
14897 lck_mtx_lock(&dtrace_lock
);
14898 ASSERT(help
->dthps_deferred
== 0 ||
14899 help
->dthps_next
!= NULL
||
14900 help
->dthps_prev
!= NULL
||
14901 help
== dtrace_deferred_pid
);
14904 * Remove the helper from the deferred list.
14906 if (help
->dthps_next
!= NULL
)
14907 help
->dthps_next
->dthps_prev
= help
->dthps_prev
;
14908 if (help
->dthps_prev
!= NULL
)
14909 help
->dthps_prev
->dthps_next
= help
->dthps_next
;
14910 if (dtrace_deferred_pid
== help
) {
14911 dtrace_deferred_pid
= help
->dthps_next
;
14912 ASSERT(help
->dthps_prev
== NULL
);
14915 lck_mtx_unlock(&dtrace_lock
);
14918 lck_mtx_unlock(&dtrace_meta_lock
);
14920 for (i
= 0; i
< help
->dthps_nprovs
; i
++) {
14921 dtrace_helper_provider_destroy(help
->dthps_provs
[i
]);
14924 kmem_free(help
->dthps_provs
, help
->dthps_maxprovs
*
14925 sizeof (dtrace_helper_provider_t
*));
14928 lck_mtx_lock(&dtrace_lock
);
14930 dtrace_vstate_fini(&help
->dthps_vstate
);
14931 kmem_free(help
->dthps_actions
,
14932 sizeof (dtrace_helper_action_t
*) * DTRACE_NHELPER_ACTIONS
);
14933 kmem_free(help
, sizeof (dtrace_helpers_t
));
14936 lck_mtx_unlock(&dtrace_lock
);
14940 dtrace_helpers_duplicate(proc_t
*from
, proc_t
*to
)
14942 dtrace_helpers_t
*help
, *newhelp
;
14943 dtrace_helper_action_t
*helper
, *new, *last
;
14945 dtrace_vstate_t
*vstate
;
14947 int j
, sz
, hasprovs
= 0;
14949 lck_mtx_lock(&dtrace_lock
);
14950 ASSERT(from
->p_dtrace_helpers
!= NULL
);
14951 ASSERT(dtrace_helpers
> 0);
14953 help
= from
->p_dtrace_helpers
;
14954 newhelp
= dtrace_helpers_create(to
);
14955 ASSERT(to
->p_dtrace_helpers
!= NULL
);
14957 newhelp
->dthps_generation
= help
->dthps_generation
;
14958 vstate
= &newhelp
->dthps_vstate
;
14961 * Duplicate the helper actions.
14963 for (i
= 0; i
< DTRACE_NHELPER_ACTIONS
; i
++) {
14964 if ((helper
= help
->dthps_actions
[i
]) == NULL
)
14967 for (last
= NULL
; helper
!= NULL
; helper
= helper
->dtha_next
) {
14968 new = kmem_zalloc(sizeof (dtrace_helper_action_t
),
14970 new->dtha_generation
= helper
->dtha_generation
;
14972 if ((dp
= helper
->dtha_predicate
) != NULL
) {
14973 dp
= dtrace_difo_duplicate(dp
, vstate
);
14974 new->dtha_predicate
= dp
;
14977 new->dtha_nactions
= helper
->dtha_nactions
;
14978 sz
= sizeof (dtrace_difo_t
*) * new->dtha_nactions
;
14979 new->dtha_actions
= kmem_alloc(sz
, KM_SLEEP
);
14981 for (j
= 0; j
< new->dtha_nactions
; j
++) {
14982 dtrace_difo_t
*dpj
= helper
->dtha_actions
[j
];
14984 ASSERT(dpj
!= NULL
);
14985 dpj
= dtrace_difo_duplicate(dpj
, vstate
);
14986 new->dtha_actions
[j
] = dpj
;
14989 if (last
!= NULL
) {
14990 last
->dtha_next
= new;
14992 newhelp
->dthps_actions
[i
] = new;
15000 * Duplicate the helper providers and register them with the
15001 * DTrace framework.
15003 if (help
->dthps_nprovs
> 0) {
15004 newhelp
->dthps_nprovs
= help
->dthps_nprovs
;
15005 newhelp
->dthps_maxprovs
= help
->dthps_nprovs
;
15006 newhelp
->dthps_provs
= kmem_alloc(newhelp
->dthps_nprovs
*
15007 sizeof (dtrace_helper_provider_t
*), KM_SLEEP
);
15008 for (i
= 0; i
< newhelp
->dthps_nprovs
; i
++) {
15009 newhelp
->dthps_provs
[i
] = help
->dthps_provs
[i
];
15010 newhelp
->dthps_provs
[i
]->dthp_ref
++;
15016 lck_mtx_unlock(&dtrace_lock
);
15019 dtrace_helper_provider_register(to
, newhelp
, NULL
);
15023 * DTrace Hook Functions
15027 * APPLE NOTE: dtrace_modctl_* routines for kext support.
15028 * Used to manipulate the modctl list within dtrace xnu.
15031 modctl_t
*dtrace_modctl_list
;
15034 dtrace_modctl_add(struct modctl
* newctl
)
15036 struct modctl
*nextp
, *prevp
;
15038 ASSERT(newctl
!= NULL
);
15039 lck_mtx_assert(&mod_lock
, LCK_MTX_ASSERT_OWNED
);
15041 // Insert new module at the front of the list,
15043 newctl
->mod_next
= dtrace_modctl_list
;
15044 dtrace_modctl_list
= newctl
;
15047 * If a module exists with the same name, then that module
15048 * must have been unloaded with enabled probes. We will move
15049 * the unloaded module to the new module's stale chain and
15050 * then stop traversing the list.
15054 nextp
= newctl
->mod_next
;
15056 while (nextp
!= NULL
) {
15057 if (nextp
->mod_loaded
) {
15058 /* This is a loaded module. Keep traversing. */
15060 nextp
= nextp
->mod_next
;
15064 /* Found an unloaded module */
15065 if (strncmp (newctl
->mod_modname
, nextp
->mod_modname
, KMOD_MAX_NAME
)) {
15066 /* Names don't match. Keep traversing. */
15068 nextp
= nextp
->mod_next
;
15072 /* We found a stale entry, move it. We're done. */
15073 prevp
->mod_next
= nextp
->mod_next
;
15074 newctl
->mod_stale
= nextp
;
15075 nextp
->mod_next
= NULL
;
15083 dtrace_modctl_lookup(struct kmod_info
* kmod
)
15085 lck_mtx_assert(&mod_lock
, LCK_MTX_ASSERT_OWNED
);
15087 struct modctl
* ctl
;
15089 for (ctl
= dtrace_modctl_list
; ctl
; ctl
=ctl
->mod_next
) {
15090 if (ctl
->mod_id
== kmod
->id
)
15097 * This routine is called from dtrace_module_unloaded().
15098 * It removes a modctl structure and its stale chain
15099 * from the kext shadow list.
15102 dtrace_modctl_remove(struct modctl
* ctl
)
15104 ASSERT(ctl
!= NULL
);
15105 lck_mtx_assert(&mod_lock
, LCK_MTX_ASSERT_OWNED
);
15106 modctl_t
*prevp
, *nextp
, *curp
;
15108 // Remove stale chain first
15109 for (curp
=ctl
->mod_stale
; curp
!= NULL
; curp
=nextp
) {
15110 nextp
= curp
->mod_stale
;
15111 /* There should NEVER be user symbols allocated at this point */
15112 ASSERT(curp
->mod_user_symbols
== NULL
);
15113 kmem_free(curp
, sizeof(modctl_t
));
15117 curp
= dtrace_modctl_list
;
15119 while (curp
!= ctl
) {
15121 curp
= curp
->mod_next
;
15124 if (prevp
!= NULL
) {
15125 prevp
->mod_next
= ctl
->mod_next
;
15128 dtrace_modctl_list
= ctl
->mod_next
;
15131 /* There should NEVER be user symbols allocated at this point */
15132 ASSERT(ctl
->mod_user_symbols
== NULL
);
15134 kmem_free (ctl
, sizeof(modctl_t
));
15138 * APPLE NOTE: The kext loader will call dtrace_module_loaded
15139 * when the kext is loaded in memory, but before calling the
15140 * kext's start routine.
15142 * Return 0 on success
15143 * Return -1 on failure
15147 dtrace_module_loaded(struct kmod_info
*kmod
, uint32_t flag
)
15149 dtrace_provider_t
*prv
;
15152 * If kernel symbols have been disabled, return immediately
15153 * DTRACE_KERNEL_SYMBOLS_NEVER is a permanent mode, it is safe to test without holding locks
15155 if (dtrace_kernel_symbol_mode
== DTRACE_KERNEL_SYMBOLS_NEVER
)
15158 struct modctl
*ctl
= NULL
;
15159 if (!kmod
|| kmod
->address
== 0 || kmod
->size
== 0)
15162 lck_mtx_lock(&dtrace_provider_lock
);
15163 lck_mtx_lock(&mod_lock
);
15166 * Have we seen this kext before?
15169 ctl
= dtrace_modctl_lookup(kmod
);
15172 /* bail... we already have this kext in the modctl list */
15173 lck_mtx_unlock(&mod_lock
);
15174 lck_mtx_unlock(&dtrace_provider_lock
);
15175 if (dtrace_err_verbose
)
15176 cmn_err(CE_WARN
, "dtrace load module already exists '%s %u' is failing against '%s %u'", kmod
->name
, (uint_t
)kmod
->id
, ctl
->mod_modname
, ctl
->mod_id
);
15180 ctl
= kmem_alloc(sizeof(struct modctl
), KM_SLEEP
);
15182 if (dtrace_err_verbose
)
15183 cmn_err(CE_WARN
, "dtrace module load '%s %u' is failing ", kmod
->name
, (uint_t
)kmod
->id
);
15184 lck_mtx_unlock(&mod_lock
);
15185 lck_mtx_unlock(&dtrace_provider_lock
);
15188 ctl
->mod_next
= NULL
;
15189 ctl
->mod_stale
= NULL
;
15190 strlcpy (ctl
->mod_modname
, kmod
->name
, sizeof(ctl
->mod_modname
));
15191 ctl
->mod_loadcnt
= kmod
->id
;
15192 ctl
->mod_nenabled
= 0;
15193 ctl
->mod_address
= kmod
->address
;
15194 ctl
->mod_size
= kmod
->size
;
15195 ctl
->mod_id
= kmod
->id
;
15196 ctl
->mod_loaded
= 1;
15197 ctl
->mod_flags
= 0;
15198 ctl
->mod_user_symbols
= NULL
;
15201 * Find the UUID for this module, if it has one
15203 kernel_mach_header_t
* header
= (kernel_mach_header_t
*)ctl
->mod_address
;
15204 struct load_command
* load_cmd
= (struct load_command
*)&header
[1];
15206 for (i
= 0; i
< header
->ncmds
; i
++) {
15207 if (load_cmd
->cmd
== LC_UUID
) {
15208 struct uuid_command
* uuid_cmd
= (struct uuid_command
*)load_cmd
;
15209 memcpy(ctl
->mod_uuid
, uuid_cmd
->uuid
, sizeof(uuid_cmd
->uuid
));
15210 ctl
->mod_flags
|= MODCTL_HAS_UUID
;
15213 load_cmd
= (struct load_command
*)((caddr_t
)load_cmd
+ load_cmd
->cmdsize
);
15216 if (ctl
->mod_address
== g_kernel_kmod_info
.address
) {
15217 ctl
->mod_flags
|= MODCTL_IS_MACH_KERNEL
;
15220 dtrace_modctl_add(ctl
);
15223 * We must hold the dtrace_lock to safely test non permanent dtrace_fbt_symbol_mode(s)
15225 lck_mtx_lock(&dtrace_lock
);
15228 * DTrace must decide if it will instrument modules lazily via
15229 * userspace symbols (default mode), or instrument immediately via
15230 * kernel symbols (non-default mode)
15232 * When in default/lazy mode, DTrace will only support modules
15233 * built with a valid UUID.
15235 * Overriding the default can be done explicitly in one of
15236 * the following two ways.
15238 * A module can force symbols from kernel space using the plist key,
15239 * OSBundleForceDTraceInit (see kmod.h). If this per kext state is set,
15240 * we fall through and instrument this module now.
15242 * Or, the boot-arg, dtrace_kernel_symbol_mode, can be set to force symbols
15243 * from kernel space (see dtrace_impl.h). If this system state is set
15244 * to a non-userspace mode, we fall through and instrument the module now.
15247 if ((dtrace_kernel_symbol_mode
== DTRACE_KERNEL_SYMBOLS_FROM_USERSPACE
) &&
15248 (!(flag
& KMOD_DTRACE_FORCE_INIT
)))
15250 /* We will instrument the module lazily -- this is the default */
15251 lck_mtx_unlock(&dtrace_lock
);
15252 lck_mtx_unlock(&mod_lock
);
15253 lck_mtx_unlock(&dtrace_provider_lock
);
15257 /* We will instrument the module immediately using kernel symbols */
15258 ctl
->mod_flags
|= MODCTL_HAS_KERNEL_SYMBOLS
;
15260 lck_mtx_unlock(&dtrace_lock
);
15263 * We're going to call each providers per-module provide operation
15264 * specifying only this module.
15266 for (prv
= dtrace_provider
; prv
!= NULL
; prv
= prv
->dtpv_next
)
15267 prv
->dtpv_pops
.dtps_provide_module(prv
->dtpv_arg
, ctl
);
15270 * APPLE NOTE: The contract with the kext loader is that once this function
15271 * has completed, it may delete kernel symbols at will.
15272 * We must set this while still holding the mod_lock.
15274 ctl
->mod_flags
&= ~MODCTL_HAS_KERNEL_SYMBOLS
;
15276 lck_mtx_unlock(&mod_lock
);
15277 lck_mtx_unlock(&dtrace_provider_lock
);
15280 * If we have any retained enablings, we need to match against them.
15281 * Enabling probes requires that cpu_lock be held, and we cannot hold
15282 * cpu_lock here -- it is legal for cpu_lock to be held when loading a
15283 * module. (In particular, this happens when loading scheduling
15284 * classes.) So if we have any retained enablings, we need to dispatch
15285 * our task queue to do the match for us.
15287 lck_mtx_lock(&dtrace_lock
);
15289 if (dtrace_retained
== NULL
) {
15290 lck_mtx_unlock(&dtrace_lock
);
15296 * The cpu_lock mentioned above is only held by dtrace code, Apple's xnu never actually
15297 * holds it for any reason. Thus the comment above is invalid, we can directly invoke
15298 * dtrace_enabling_matchall without jumping through all the hoops, and we can avoid
15299 * the delay call as well.
15301 lck_mtx_unlock(&dtrace_lock
);
15303 dtrace_enabling_matchall();
15309 * Return 0 on success
15310 * Return -1 on failure
15313 dtrace_module_unloaded(struct kmod_info
*kmod
)
15315 dtrace_probe_t
template, *probe
, *first
, *next
;
15316 dtrace_provider_t
*prov
;
15317 struct modctl
*ctl
= NULL
;
15318 struct modctl
*syncctl
= NULL
;
15319 struct modctl
*nextsyncctl
= NULL
;
15322 lck_mtx_lock(&dtrace_provider_lock
);
15323 lck_mtx_lock(&mod_lock
);
15324 lck_mtx_lock(&dtrace_lock
);
15326 if (kmod
== NULL
) {
15330 ctl
= dtrace_modctl_lookup(kmod
);
15333 lck_mtx_unlock(&dtrace_lock
);
15334 lck_mtx_unlock(&mod_lock
);
15335 lck_mtx_unlock(&dtrace_provider_lock
);
15338 ctl
->mod_loaded
= 0;
15339 ctl
->mod_address
= 0;
15343 if (dtrace_bymod
== NULL
) {
15345 * The DTrace module is loaded (obviously) but not attached;
15346 * we don't have any work to do.
15349 (void)dtrace_modctl_remove(ctl
);
15350 lck_mtx_unlock(&dtrace_lock
);
15351 lck_mtx_unlock(&mod_lock
);
15352 lck_mtx_unlock(&dtrace_provider_lock
);
15356 /* Syncmode set means we target and traverse entire modctl list. */
15358 nextsyncctl
= dtrace_modctl_list
;
15363 /* find a stale modctl struct */
15364 for (syncctl
= nextsyncctl
; syncctl
!= NULL
; syncctl
=syncctl
->mod_next
) {
15365 if (syncctl
->mod_address
== 0)
15370 /* We have no more work to do */
15371 lck_mtx_unlock(&dtrace_lock
);
15372 lck_mtx_unlock(&mod_lock
);
15373 lck_mtx_unlock(&dtrace_provider_lock
);
15377 /* keep track of next syncctl in case this one is removed */
15378 nextsyncctl
= syncctl
->mod_next
;
15383 template.dtpr_mod
= ctl
->mod_modname
;
15385 for (probe
= first
= dtrace_hash_lookup(dtrace_bymod
, &template);
15386 probe
!= NULL
; probe
= probe
->dtpr_nextmod
) {
15387 if (probe
->dtpr_ecb
!= NULL
) {
15389 * This shouldn't _actually_ be possible -- we're
15390 * unloading a module that has an enabled probe in it.
15391 * (It's normally up to the provider to make sure that
15392 * this can't happen.) However, because dtps_enable()
15393 * doesn't have a failure mode, there can be an
15394 * enable/unload race. Upshot: we don't want to
15395 * assert, but we're not going to disable the
15401 /* We're syncing, let's look at next in list */
15405 lck_mtx_unlock(&dtrace_lock
);
15406 lck_mtx_unlock(&mod_lock
);
15407 lck_mtx_unlock(&dtrace_provider_lock
);
15409 if (dtrace_err_verbose
) {
15410 cmn_err(CE_WARN
, "unloaded module '%s' had "
15411 "enabled probes", ctl
->mod_modname
);
15419 for (first
= NULL
; probe
!= NULL
; probe
= next
) {
15420 ASSERT(dtrace_probes
[probe
->dtpr_id
- 1] == probe
);
15422 dtrace_probes
[probe
->dtpr_id
- 1] = NULL
;
15423 probe
->dtpr_provider
->dtpv_probe_count
--;
15425 next
= probe
->dtpr_nextmod
;
15426 dtrace_hash_remove(dtrace_bymod
, probe
);
15427 dtrace_hash_remove(dtrace_byfunc
, probe
);
15428 dtrace_hash_remove(dtrace_byname
, probe
);
15430 if (first
== NULL
) {
15432 probe
->dtpr_nextmod
= NULL
;
15434 probe
->dtpr_nextmod
= first
;
15440 * We've removed all of the module's probes from the hash chains and
15441 * from the probe array. Now issue a dtrace_sync() to be sure that
15442 * everyone has cleared out from any probe array processing.
15446 for (probe
= first
; probe
!= NULL
; probe
= first
) {
15447 first
= probe
->dtpr_nextmod
;
15448 prov
= probe
->dtpr_provider
;
15449 prov
->dtpv_pops
.dtps_destroy(prov
->dtpv_arg
, probe
->dtpr_id
,
15451 kmem_free(probe
->dtpr_mod
, strlen(probe
->dtpr_mod
) + 1);
15452 kmem_free(probe
->dtpr_func
, strlen(probe
->dtpr_func
) + 1);
15453 kmem_free(probe
->dtpr_name
, strlen(probe
->dtpr_name
) + 1);
15454 vmem_free(dtrace_arena
, (void *)(uintptr_t)probe
->dtpr_id
, 1);
15456 zfree(dtrace_probe_t_zone
, probe
);
15459 dtrace_modctl_remove(ctl
);
15464 lck_mtx_unlock(&dtrace_lock
);
15465 lck_mtx_unlock(&mod_lock
);
15466 lck_mtx_unlock(&dtrace_provider_lock
);
15472 dtrace_suspend(void)
15474 dtrace_probe_foreach(offsetof(dtrace_pops_t
, dtps_suspend
));
15478 dtrace_resume(void)
15480 dtrace_probe_foreach(offsetof(dtrace_pops_t
, dtps_resume
));
15484 dtrace_cpu_setup(cpu_setup_t what
, processorid_t cpu
)
15486 lck_mtx_assert(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
15487 lck_mtx_lock(&dtrace_lock
);
15491 dtrace_state_t
*state
;
15492 dtrace_optval_t
*opt
, rs
, c
;
15495 * For now, we only allocate a new buffer for anonymous state.
15497 if ((state
= dtrace_anon
.dta_state
) == NULL
)
15500 if (state
->dts_activity
!= DTRACE_ACTIVITY_ACTIVE
)
15503 opt
= state
->dts_options
;
15504 c
= opt
[DTRACEOPT_CPU
];
15506 if (c
!= DTRACE_CPUALL
&& c
!= DTRACEOPT_UNSET
&& c
!= cpu
)
15510 * Regardless of what the actual policy is, we're going to
15511 * temporarily set our resize policy to be manual. We're
15512 * also going to temporarily set our CPU option to denote
15513 * the newly configured CPU.
15515 rs
= opt
[DTRACEOPT_BUFRESIZE
];
15516 opt
[DTRACEOPT_BUFRESIZE
] = DTRACEOPT_BUFRESIZE_MANUAL
;
15517 opt
[DTRACEOPT_CPU
] = (dtrace_optval_t
)cpu
;
15519 (void) dtrace_state_buffers(state
);
15521 opt
[DTRACEOPT_BUFRESIZE
] = rs
;
15522 opt
[DTRACEOPT_CPU
] = c
;
15529 * We don't free the buffer in the CPU_UNCONFIG case. (The
15530 * buffer will be freed when the consumer exits.)
15538 lck_mtx_unlock(&dtrace_lock
);
15543 dtrace_cpu_setup_initial(processorid_t cpu
)
15545 (void) dtrace_cpu_setup(CPU_CONFIG
, cpu
);
15549 dtrace_toxrange_add(uintptr_t base
, uintptr_t limit
)
15551 if (dtrace_toxranges
>= dtrace_toxranges_max
) {
15553 dtrace_toxrange_t
*range
;
15555 osize
= dtrace_toxranges_max
* sizeof (dtrace_toxrange_t
);
15558 ASSERT(dtrace_toxrange
== NULL
);
15559 ASSERT(dtrace_toxranges_max
== 0);
15560 dtrace_toxranges_max
= 1;
15562 dtrace_toxranges_max
<<= 1;
15565 nsize
= dtrace_toxranges_max
* sizeof (dtrace_toxrange_t
);
15566 range
= kmem_zalloc(nsize
, KM_SLEEP
);
15568 if (dtrace_toxrange
!= NULL
) {
15569 ASSERT(osize
!= 0);
15570 bcopy(dtrace_toxrange
, range
, osize
);
15571 kmem_free(dtrace_toxrange
, osize
);
15574 dtrace_toxrange
= range
;
15577 ASSERT(dtrace_toxrange
[dtrace_toxranges
].dtt_base
== 0);
15578 ASSERT(dtrace_toxrange
[dtrace_toxranges
].dtt_limit
== 0);
15580 dtrace_toxrange
[dtrace_toxranges
].dtt_base
= base
;
15581 dtrace_toxrange
[dtrace_toxranges
].dtt_limit
= limit
;
15582 dtrace_toxranges
++;
15586 * DTrace Driver Cookbook Functions
15590 dtrace_attach(dev_info_t
*devi
, ddi_attach_cmd_t cmd
)
15592 #pragma unused(cmd) /* __APPLE__ */
15593 dtrace_provider_id_t id
;
15594 dtrace_state_t
*state
= NULL
;
15595 dtrace_enabling_t
*enab
;
15597 lck_mtx_lock(&cpu_lock
);
15598 lck_mtx_lock(&dtrace_provider_lock
);
15599 lck_mtx_lock(&dtrace_lock
);
15601 if (ddi_soft_state_init(&dtrace_softstate
,
15602 sizeof (dtrace_state_t
), 0) != 0) {
15603 cmn_err(CE_NOTE
, "/dev/dtrace failed to initialize soft state");
15604 lck_mtx_unlock(&dtrace_lock
);
15605 lck_mtx_unlock(&dtrace_provider_lock
);
15606 lck_mtx_unlock(&cpu_lock
);
15607 return (DDI_FAILURE
);
15610 /* Darwin uses BSD cloning device driver to automagically obtain minor device number. */
15612 ddi_report_dev(devi
);
15613 dtrace_devi
= devi
;
15615 dtrace_modload
= dtrace_module_loaded
;
15616 dtrace_modunload
= dtrace_module_unloaded
;
15617 dtrace_cpu_init
= dtrace_cpu_setup_initial
;
15618 dtrace_helpers_cleanup
= dtrace_helpers_destroy
;
15619 dtrace_helpers_fork
= dtrace_helpers_duplicate
;
15620 dtrace_cpustart_init
= dtrace_suspend
;
15621 dtrace_cpustart_fini
= dtrace_resume
;
15622 dtrace_debugger_init
= dtrace_suspend
;
15623 dtrace_debugger_fini
= dtrace_resume
;
15625 register_cpu_setup_func((cpu_setup_func_t
*)dtrace_cpu_setup
, NULL
);
15627 lck_mtx_assert(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
15629 dtrace_arena
= vmem_create("dtrace", (void *)1, UINT32_MAX
, 1,
15630 NULL
, NULL
, NULL
, 0, VM_SLEEP
| VMC_IDENTIFIER
);
15631 dtrace_minor
= vmem_create("dtrace_minor", (void *)DTRACEMNRN_CLONE
,
15632 UINT32_MAX
- DTRACEMNRN_CLONE
, 1, NULL
, NULL
, NULL
, 0,
15633 VM_SLEEP
| VMC_IDENTIFIER
);
15634 dtrace_taskq
= taskq_create("dtrace_taskq", 1, maxclsyspri
,
15637 dtrace_state_cache
= kmem_cache_create("dtrace_state_cache",
15638 sizeof (dtrace_dstate_percpu_t
) * (int)NCPU
, DTRACE_STATE_ALIGN
,
15639 NULL
, NULL
, NULL
, NULL
, NULL
, 0);
15641 lck_mtx_assert(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
15642 dtrace_bymod
= dtrace_hash_create(offsetof(dtrace_probe_t
, dtpr_mod
),
15643 offsetof(dtrace_probe_t
, dtpr_nextmod
),
15644 offsetof(dtrace_probe_t
, dtpr_prevmod
));
15646 dtrace_byfunc
= dtrace_hash_create(offsetof(dtrace_probe_t
, dtpr_func
),
15647 offsetof(dtrace_probe_t
, dtpr_nextfunc
),
15648 offsetof(dtrace_probe_t
, dtpr_prevfunc
));
15650 dtrace_byname
= dtrace_hash_create(offsetof(dtrace_probe_t
, dtpr_name
),
15651 offsetof(dtrace_probe_t
, dtpr_nextname
),
15652 offsetof(dtrace_probe_t
, dtpr_prevname
));
15654 if (dtrace_retain_max
< 1) {
15655 cmn_err(CE_WARN
, "illegal value (%lu) for dtrace_retain_max; "
15656 "setting to 1", dtrace_retain_max
);
15657 dtrace_retain_max
= 1;
15661 * Now discover our toxic ranges.
15663 dtrace_toxic_ranges(dtrace_toxrange_add
);
15666 * Before we register ourselves as a provider to our own framework,
15667 * we would like to assert that dtrace_provider is NULL -- but that's
15668 * not true if we were loaded as a dependency of a DTrace provider.
15669 * Once we've registered, we can assert that dtrace_provider is our
15672 (void) dtrace_register("dtrace", &dtrace_provider_attr
,
15673 DTRACE_PRIV_NONE
, 0, &dtrace_provider_ops
, NULL
, &id
);
15675 ASSERT(dtrace_provider
!= NULL
);
15676 ASSERT((dtrace_provider_id_t
)dtrace_provider
== id
);
15678 #if defined (__x86_64__)
15679 dtrace_probeid_begin
= dtrace_probe_create((dtrace_provider_id_t
)
15680 dtrace_provider
, NULL
, NULL
, "BEGIN", 1, NULL
);
15681 dtrace_probeid_end
= dtrace_probe_create((dtrace_provider_id_t
)
15682 dtrace_provider
, NULL
, NULL
, "END", 0, NULL
);
15683 dtrace_probeid_error
= dtrace_probe_create((dtrace_provider_id_t
)
15684 dtrace_provider
, NULL
, NULL
, "ERROR", 3, NULL
);
15686 #error Unknown Architecture
15689 dtrace_anon_property();
15690 lck_mtx_unlock(&cpu_lock
);
15693 * If DTrace helper tracing is enabled, we need to allocate the
15694 * trace buffer and initialize the values.
15696 if (dtrace_helptrace_enabled
) {
15697 ASSERT(dtrace_helptrace_buffer
== NULL
);
15698 dtrace_helptrace_buffer
=
15699 kmem_zalloc(dtrace_helptrace_bufsize
, KM_SLEEP
);
15700 dtrace_helptrace_next
= 0;
15704 * If there are already providers, we must ask them to provide their
15705 * probes, and then match any anonymous enabling against them. Note
15706 * that there should be no other retained enablings at this time:
15707 * the only retained enablings at this time should be the anonymous
15710 if (dtrace_anon
.dta_enabling
!= NULL
) {
15711 ASSERT(dtrace_retained
== dtrace_anon
.dta_enabling
);
15714 * APPLE NOTE: if handling anonymous dof, switch symbol modes.
15716 if (dtrace_kernel_symbol_mode
== DTRACE_KERNEL_SYMBOLS_FROM_USERSPACE
) {
15717 dtrace_kernel_symbol_mode
= DTRACE_KERNEL_SYMBOLS_FROM_KERNEL
;
15720 dtrace_enabling_provide(NULL
);
15721 state
= dtrace_anon
.dta_state
;
15724 * We couldn't hold cpu_lock across the above call to
15725 * dtrace_enabling_provide(), but we must hold it to actually
15726 * enable the probes. We have to drop all of our locks, pick
15727 * up cpu_lock, and regain our locks before matching the
15728 * retained anonymous enabling.
15730 lck_mtx_unlock(&dtrace_lock
);
15731 lck_mtx_unlock(&dtrace_provider_lock
);
15733 lck_mtx_lock(&cpu_lock
);
15734 lck_mtx_lock(&dtrace_provider_lock
);
15735 lck_mtx_lock(&dtrace_lock
);
15737 if ((enab
= dtrace_anon
.dta_enabling
) != NULL
)
15738 (void) dtrace_enabling_match(enab
, NULL
);
15740 lck_mtx_unlock(&cpu_lock
);
15743 lck_mtx_unlock(&dtrace_lock
);
15744 lck_mtx_unlock(&dtrace_provider_lock
);
15746 if (state
!= NULL
) {
15748 * If we created any anonymous state, set it going now.
15750 (void) dtrace_state_go(state
, &dtrace_anon
.dta_beganon
);
15753 return (DDI_SUCCESS
);
15758 dtrace_open(dev_t
*devp
, int flag
, int otyp
, cred_t
*cred_p
)
15760 #pragma unused(flag, otyp)
15761 dtrace_state_t
*state
;
15767 /* APPLE: Darwin puts Helper on its own major device. */
15770 * If no DTRACE_PRIV_* bits are set in the credential, then the
15771 * caller lacks sufficient permission to do anything with DTrace.
15773 dtrace_cred2priv(cred_p
, &priv
, &uid
, &zoneid
);
15774 if (priv
== DTRACE_PRIV_NONE
)
15778 * APPLE NOTE: We delay the initialization of fasttrap as late as possible.
15779 * It certainly can't be later than now!
15784 * Ask all providers to provide all their probes.
15786 lck_mtx_lock(&dtrace_provider_lock
);
15787 dtrace_probe_provide(NULL
, NULL
);
15788 lck_mtx_unlock(&dtrace_provider_lock
);
15790 lck_mtx_lock(&cpu_lock
);
15791 lck_mtx_lock(&dtrace_lock
);
15793 dtrace_membar_producer();
15796 * If the kernel debugger is active (that is, if the kernel debugger
15797 * modified text in some way), we won't allow the open.
15799 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE
) != 0) {
15801 lck_mtx_unlock(&dtrace_lock
);
15802 lck_mtx_unlock(&cpu_lock
);
15806 rv
= dtrace_state_create(devp
, cred_p
, &state
);
15807 lck_mtx_unlock(&cpu_lock
);
15809 if (rv
!= 0 || state
== NULL
) {
15810 if (--dtrace_opens
== 0 && dtrace_anon
.dta_enabling
== NULL
)
15811 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE
);
15812 lck_mtx_unlock(&dtrace_lock
);
15813 /* propagate EAGAIN or ERESTART */
15817 lck_mtx_unlock(&dtrace_lock
);
15819 lck_rw_lock_exclusive(&dtrace_dof_mode_lock
);
15822 * If we are currently lazy, transition states.
15824 * Unlike dtrace_close, we do not need to check the
15825 * value of dtrace_opens, as any positive value (and
15826 * we count as 1) means we transition states.
15828 if (dtrace_dof_mode
== DTRACE_DOF_MODE_LAZY_ON
) {
15829 dtrace_dof_mode
= DTRACE_DOF_MODE_LAZY_OFF
;
15832 * Iterate all existing processes and load lazy dofs.
15834 proc_iterate(PROC_ALLPROCLIST
| PROC_NOWAITTRANS
,
15835 dtrace_lazy_dofs_proc_iterate_doit
,
15837 dtrace_lazy_dofs_proc_iterate_filter
,
15841 lck_rw_unlock_exclusive(&dtrace_dof_mode_lock
);
15844 * Update kernel symbol state.
15846 * We must own the provider and dtrace locks.
15848 * NOTE! It may appear there is a race by setting this value so late
15849 * after dtrace_probe_provide. However, any kext loaded after the
15850 * call to probe provide and before we set LAZY_OFF will be marked as
15851 * eligible for symbols from userspace. The same dtrace that is currently
15852 * calling dtrace_open() (this call!) will get a list of kexts needing
15853 * symbols and fill them in, thus closing the race window.
15855 * We want to set this value only after it certain it will succeed, as
15856 * this significantly reduces the complexity of error exits.
15858 lck_mtx_lock(&dtrace_lock
);
15859 if (dtrace_kernel_symbol_mode
== DTRACE_KERNEL_SYMBOLS_FROM_USERSPACE
) {
15860 dtrace_kernel_symbol_mode
= DTRACE_KERNEL_SYMBOLS_FROM_KERNEL
;
15862 lck_mtx_unlock(&dtrace_lock
);
15869 dtrace_close(dev_t dev
, int flag
, int otyp
, cred_t
*cred_p
)
15871 #pragma unused(flag, otyp, cred_p) /* __APPLE__ */
15872 minor_t minor
= getminor(dev
);
15873 dtrace_state_t
*state
;
15875 /* APPLE NOTE: Darwin puts Helper on its own major device. */
15877 state
= ddi_get_soft_state(dtrace_softstate
, minor
);
15879 lck_mtx_lock(&cpu_lock
);
15880 lck_mtx_lock(&dtrace_lock
);
15882 if (state
->dts_anon
) {
15884 * There is anonymous state. Destroy that first.
15886 ASSERT(dtrace_anon
.dta_state
== NULL
);
15887 dtrace_state_destroy(state
->dts_anon
);
15890 dtrace_state_destroy(state
);
15891 ASSERT(dtrace_opens
> 0);
15894 * Only relinquish control of the kernel debugger interface when there
15895 * are no consumers and no anonymous enablings.
15897 if (--dtrace_opens
== 0 && dtrace_anon
.dta_enabling
== NULL
)
15898 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE
);
15900 lck_mtx_unlock(&dtrace_lock
);
15901 lck_mtx_unlock(&cpu_lock
);
15904 * Lock ordering requires the dof mode lock be taken before
15907 lck_rw_lock_exclusive(&dtrace_dof_mode_lock
);
15908 lck_mtx_lock(&dtrace_lock
);
15910 if (dtrace_opens
== 0) {
15912 * If we are currently lazy-off, and this is the last close, transition to
15915 if (dtrace_dof_mode
== DTRACE_DOF_MODE_LAZY_OFF
) {
15916 dtrace_dof_mode
= DTRACE_DOF_MODE_LAZY_ON
;
15920 * If we are the last dtrace client, switch back to lazy (from userspace) symbols
15922 if (dtrace_kernel_symbol_mode
== DTRACE_KERNEL_SYMBOLS_FROM_KERNEL
) {
15923 dtrace_kernel_symbol_mode
= DTRACE_KERNEL_SYMBOLS_FROM_USERSPACE
;
15927 lck_mtx_unlock(&dtrace_lock
);
15928 lck_rw_unlock_exclusive(&dtrace_dof_mode_lock
);
15931 * Kext probes may be retained past the end of the kext's lifespan. The
15932 * probes are kept until the last reference to them has been removed.
15933 * Since closing an active dtrace context is likely to drop that last reference,
15934 * lets take a shot at cleaning out the orphaned probes now.
15936 dtrace_module_unloaded(NULL
);
15943 dtrace_ioctl_helper(u_long cmd
, caddr_t arg
, int *rv
)
15947 * Safe to check this outside the dof mode lock
15949 if (dtrace_dof_mode
== DTRACE_DOF_MODE_NEVER
)
15950 return KERN_SUCCESS
;
15953 case DTRACEHIOC_ADDDOF
:
15955 dof_helper_t
*dhp
= NULL
;
15956 size_t dof_ioctl_data_size
;
15957 dof_ioctl_data_t
* multi_dof
;
15960 user_addr_t user_address
= *(user_addr_t
*)arg
;
15961 uint64_t dof_count
;
15962 int multi_dof_claimed
= 0;
15963 proc_t
* p
= current_proc();
15966 * Read the number of DOF sections being passed in.
15968 if (copyin(user_address
+ offsetof(dof_ioctl_data_t
, dofiod_count
),
15970 sizeof(dof_count
))) {
15971 dtrace_dof_error(NULL
, "failed to copyin dofiod_count");
15976 * Range check the count.
15978 if (dof_count
== 0 || dof_count
> 1024) {
15979 dtrace_dof_error(NULL
, "dofiod_count is not valid");
15984 * Allocate a correctly sized structure and copyin the data.
15986 dof_ioctl_data_size
= DOF_IOCTL_DATA_T_SIZE(dof_count
);
15987 if ((multi_dof
= kmem_alloc(dof_ioctl_data_size
, KM_SLEEP
)) == NULL
)
15990 /* NOTE! We can no longer exit this method via return */
15991 if (copyin(user_address
, multi_dof
, dof_ioctl_data_size
) != 0) {
15992 dtrace_dof_error(NULL
, "failed copyin of dof_ioctl_data_t");
15998 * Check that the count didn't change between the first copyin and the second.
16000 if (multi_dof
->dofiod_count
!= dof_count
) {
16006 * Try to process lazily first.
16008 rval
= dtrace_lazy_dofs_add(p
, multi_dof
, &multi_dof_claimed
);
16011 * If rval is EACCES, we must be non-lazy.
16013 if (rval
== EACCES
) {
16016 * Process each dof_helper_t
16020 dhp
= &multi_dof
->dofiod_helpers
[i
];
16022 dof_hdr_t
*dof
= dtrace_dof_copyin(dhp
->dofhp_dof
, &rval
);
16025 lck_mtx_lock(&dtrace_lock
);
16028 * dtrace_helper_slurp() takes responsibility for the dof --
16029 * it may free it now or it may save it and free it later.
16031 if ((dhp
->dofhp_dof
= (uint64_t)dtrace_helper_slurp(p
, dof
, dhp
)) == -1ULL) {
16035 lck_mtx_unlock(&dtrace_lock
);
16037 } while (++i
< multi_dof
->dofiod_count
&& rval
== 0);
16041 * We need to copyout the multi_dof struct, because it contains
16042 * the generation (unique id) values needed to call DTRACEHIOC_REMOVE
16044 * This could certainly be better optimized.
16046 if (copyout(multi_dof
, user_address
, dof_ioctl_data_size
) != 0) {
16047 dtrace_dof_error(NULL
, "failed copyout of dof_ioctl_data_t");
16048 /* Don't overwrite pre-existing error code */
16049 if (rval
== 0) rval
= EFAULT
;
16054 * If we had to allocate struct memory, free it.
16056 if (multi_dof
!= NULL
&& !multi_dof_claimed
) {
16057 kmem_free(multi_dof
, dof_ioctl_data_size
);
16063 case DTRACEHIOC_REMOVE
: {
16064 int generation
= *(int*)arg
;
16065 proc_t
* p
= current_proc();
16070 int rval
= dtrace_lazy_dofs_remove(p
, generation
);
16073 * EACCES means non-lazy
16075 if (rval
== EACCES
) {
16076 lck_mtx_lock(&dtrace_lock
);
16077 rval
= dtrace_helper_destroygen(p
, generation
);
16078 lck_mtx_unlock(&dtrace_lock
);
16093 dtrace_ioctl(dev_t dev
, u_long cmd
, user_addr_t arg
, int md
, cred_t
*cr
, int *rv
)
16096 minor_t minor
= getminor(dev
);
16097 dtrace_state_t
*state
;
16100 /* Darwin puts Helper on its own major device. */
16102 state
= ddi_get_soft_state(dtrace_softstate
, minor
);
16104 if (state
->dts_anon
) {
16105 ASSERT(dtrace_anon
.dta_state
== NULL
);
16106 state
= state
->dts_anon
;
16110 case DTRACEIOC_PROVIDER
: {
16111 dtrace_providerdesc_t pvd
;
16112 dtrace_provider_t
*pvp
;
16114 if (copyin(arg
, &pvd
, sizeof (pvd
)) != 0)
16117 pvd
.dtvd_name
[DTRACE_PROVNAMELEN
- 1] = '\0';
16118 lck_mtx_lock(&dtrace_provider_lock
);
16120 for (pvp
= dtrace_provider
; pvp
!= NULL
; pvp
= pvp
->dtpv_next
) {
16121 if (strncmp(pvp
->dtpv_name
, pvd
.dtvd_name
, DTRACE_PROVNAMELEN
) == 0)
16125 lck_mtx_unlock(&dtrace_provider_lock
);
16130 bcopy(&pvp
->dtpv_priv
, &pvd
.dtvd_priv
, sizeof (dtrace_ppriv_t
));
16131 bcopy(&pvp
->dtpv_attr
, &pvd
.dtvd_attr
, sizeof (dtrace_pattr_t
));
16132 if (copyout(&pvd
, arg
, sizeof (pvd
)) != 0)
16138 case DTRACEIOC_EPROBE
: {
16139 dtrace_eprobedesc_t epdesc
;
16141 dtrace_action_t
*act
;
16147 if (copyin(arg
, &epdesc
, sizeof (epdesc
)) != 0)
16150 lck_mtx_lock(&dtrace_lock
);
16152 if ((ecb
= dtrace_epid2ecb(state
, epdesc
.dtepd_epid
)) == NULL
) {
16153 lck_mtx_unlock(&dtrace_lock
);
16157 if (ecb
->dte_probe
== NULL
) {
16158 lck_mtx_unlock(&dtrace_lock
);
16162 epdesc
.dtepd_probeid
= ecb
->dte_probe
->dtpr_id
;
16163 epdesc
.dtepd_uarg
= ecb
->dte_uarg
;
16164 epdesc
.dtepd_size
= ecb
->dte_size
;
16166 nrecs
= epdesc
.dtepd_nrecs
;
16167 epdesc
.dtepd_nrecs
= 0;
16168 for (act
= ecb
->dte_action
; act
!= NULL
; act
= act
->dta_next
) {
16169 if (DTRACEACT_ISAGG(act
->dta_kind
) || act
->dta_intuple
)
16172 epdesc
.dtepd_nrecs
++;
16176 * Now that we have the size, we need to allocate a temporary
16177 * buffer in which to store the complete description. We need
16178 * the temporary buffer to be able to drop dtrace_lock()
16179 * across the copyout(), below.
16181 size
= sizeof (dtrace_eprobedesc_t
) +
16182 (epdesc
.dtepd_nrecs
* sizeof (dtrace_recdesc_t
));
16184 buf
= kmem_alloc(size
, KM_SLEEP
);
16185 dest
= (uintptr_t)buf
;
16187 bcopy(&epdesc
, (void *)dest
, sizeof (epdesc
));
16188 dest
+= offsetof(dtrace_eprobedesc_t
, dtepd_rec
[0]);
16190 for (act
= ecb
->dte_action
; act
!= NULL
; act
= act
->dta_next
) {
16191 if (DTRACEACT_ISAGG(act
->dta_kind
) || act
->dta_intuple
)
16197 bcopy(&act
->dta_rec
, (void *)dest
,
16198 sizeof (dtrace_recdesc_t
));
16199 dest
+= sizeof (dtrace_recdesc_t
);
16202 lck_mtx_unlock(&dtrace_lock
);
16204 if (copyout(buf
, arg
, dest
- (uintptr_t)buf
) != 0) {
16205 kmem_free(buf
, size
);
16209 kmem_free(buf
, size
);
16213 case DTRACEIOC_AGGDESC
: {
16214 dtrace_aggdesc_t aggdesc
;
16215 dtrace_action_t
*act
;
16216 dtrace_aggregation_t
*agg
;
16219 dtrace_recdesc_t
*lrec
;
16224 if (copyin(arg
, &aggdesc
, sizeof (aggdesc
)) != 0)
16227 lck_mtx_lock(&dtrace_lock
);
16229 if ((agg
= dtrace_aggid2agg(state
, aggdesc
.dtagd_id
)) == NULL
) {
16230 lck_mtx_unlock(&dtrace_lock
);
16234 aggdesc
.dtagd_epid
= agg
->dtag_ecb
->dte_epid
;
16236 nrecs
= aggdesc
.dtagd_nrecs
;
16237 aggdesc
.dtagd_nrecs
= 0;
16239 offs
= agg
->dtag_base
;
16240 lrec
= &agg
->dtag_action
.dta_rec
;
16241 aggdesc
.dtagd_size
= lrec
->dtrd_offset
+ lrec
->dtrd_size
- offs
;
16243 for (act
= agg
->dtag_first
; ; act
= act
->dta_next
) {
16244 ASSERT(act
->dta_intuple
||
16245 DTRACEACT_ISAGG(act
->dta_kind
));
16248 * If this action has a record size of zero, it
16249 * denotes an argument to the aggregating action.
16250 * Because the presence of this record doesn't (or
16251 * shouldn't) affect the way the data is interpreted,
16252 * we don't copy it out to save user-level the
16253 * confusion of dealing with a zero-length record.
16255 if (act
->dta_rec
.dtrd_size
== 0) {
16256 ASSERT(agg
->dtag_hasarg
);
16260 aggdesc
.dtagd_nrecs
++;
16262 if (act
== &agg
->dtag_action
)
16267 * Now that we have the size, we need to allocate a temporary
16268 * buffer in which to store the complete description. We need
16269 * the temporary buffer to be able to drop dtrace_lock()
16270 * across the copyout(), below.
16272 size
= sizeof (dtrace_aggdesc_t
) +
16273 (aggdesc
.dtagd_nrecs
* sizeof (dtrace_recdesc_t
));
16275 buf
= kmem_alloc(size
, KM_SLEEP
);
16276 dest
= (uintptr_t)buf
;
16278 bcopy(&aggdesc
, (void *)dest
, sizeof (aggdesc
));
16279 dest
+= offsetof(dtrace_aggdesc_t
, dtagd_rec
[0]);
16281 for (act
= agg
->dtag_first
; ; act
= act
->dta_next
) {
16282 dtrace_recdesc_t rec
= act
->dta_rec
;
16285 * See the comment in the above loop for why we pass
16286 * over zero-length records.
16288 if (rec
.dtrd_size
== 0) {
16289 ASSERT(agg
->dtag_hasarg
);
16296 rec
.dtrd_offset
-= offs
;
16297 bcopy(&rec
, (void *)dest
, sizeof (rec
));
16298 dest
+= sizeof (dtrace_recdesc_t
);
16300 if (act
== &agg
->dtag_action
)
16304 lck_mtx_unlock(&dtrace_lock
);
16306 if (copyout(buf
, arg
, dest
- (uintptr_t)buf
) != 0) {
16307 kmem_free(buf
, size
);
16311 kmem_free(buf
, size
);
16315 case DTRACEIOC_ENABLE
: {
16317 dtrace_enabling_t
*enab
= NULL
;
16318 dtrace_vstate_t
*vstate
;
16324 * If a NULL argument has been passed, we take this as our
16325 * cue to reevaluate our enablings.
16328 dtrace_enabling_matchall();
16333 if ((dof
= dtrace_dof_copyin(arg
, &rval
)) == NULL
)
16336 lck_mtx_lock(&cpu_lock
);
16337 lck_mtx_lock(&dtrace_lock
);
16338 vstate
= &state
->dts_vstate
;
16340 if (state
->dts_activity
!= DTRACE_ACTIVITY_INACTIVE
) {
16341 lck_mtx_unlock(&dtrace_lock
);
16342 lck_mtx_unlock(&cpu_lock
);
16343 dtrace_dof_destroy(dof
);
16347 if (dtrace_dof_slurp(dof
, vstate
, cr
, &enab
, 0, B_TRUE
) != 0) {
16348 lck_mtx_unlock(&dtrace_lock
);
16349 lck_mtx_unlock(&cpu_lock
);
16350 dtrace_dof_destroy(dof
);
16354 if ((rval
= dtrace_dof_options(dof
, state
)) != 0) {
16355 dtrace_enabling_destroy(enab
);
16356 lck_mtx_unlock(&dtrace_lock
);
16357 lck_mtx_unlock(&cpu_lock
);
16358 dtrace_dof_destroy(dof
);
16362 if ((err
= dtrace_enabling_match(enab
, rv
)) == 0) {
16363 err
= dtrace_enabling_retain(enab
);
16365 dtrace_enabling_destroy(enab
);
16368 lck_mtx_unlock(&dtrace_lock
);
16369 lck_mtx_unlock(&cpu_lock
);
16370 dtrace_dof_destroy(dof
);
16375 case DTRACEIOC_REPLICATE
: {
16376 dtrace_repldesc_t desc
;
16377 dtrace_probedesc_t
*match
= &desc
.dtrpd_match
;
16378 dtrace_probedesc_t
*create
= &desc
.dtrpd_create
;
16381 if (copyin(arg
, &desc
, sizeof (desc
)) != 0)
16384 match
->dtpd_provider
[DTRACE_PROVNAMELEN
- 1] = '\0';
16385 match
->dtpd_mod
[DTRACE_MODNAMELEN
- 1] = '\0';
16386 match
->dtpd_func
[DTRACE_FUNCNAMELEN
- 1] = '\0';
16387 match
->dtpd_name
[DTRACE_NAMELEN
- 1] = '\0';
16389 create
->dtpd_provider
[DTRACE_PROVNAMELEN
- 1] = '\0';
16390 create
->dtpd_mod
[DTRACE_MODNAMELEN
- 1] = '\0';
16391 create
->dtpd_func
[DTRACE_FUNCNAMELEN
- 1] = '\0';
16392 create
->dtpd_name
[DTRACE_NAMELEN
- 1] = '\0';
16394 lck_mtx_lock(&dtrace_lock
);
16395 err
= dtrace_enabling_replicate(state
, match
, create
);
16396 lck_mtx_unlock(&dtrace_lock
);
16401 case DTRACEIOC_PROBEMATCH
:
16402 case DTRACEIOC_PROBES
: {
16403 dtrace_probe_t
*probe
= NULL
;
16404 dtrace_probedesc_t desc
;
16405 dtrace_probekey_t pkey
;
16412 if (copyin(arg
, &desc
, sizeof (desc
)) != 0)
16415 desc
.dtpd_provider
[DTRACE_PROVNAMELEN
- 1] = '\0';
16416 desc
.dtpd_mod
[DTRACE_MODNAMELEN
- 1] = '\0';
16417 desc
.dtpd_func
[DTRACE_FUNCNAMELEN
- 1] = '\0';
16418 desc
.dtpd_name
[DTRACE_NAMELEN
- 1] = '\0';
16421 * Before we attempt to match this probe, we want to give
16422 * all providers the opportunity to provide it.
16424 if (desc
.dtpd_id
== DTRACE_IDNONE
) {
16425 lck_mtx_lock(&dtrace_provider_lock
);
16426 dtrace_probe_provide(&desc
, NULL
);
16427 lck_mtx_unlock(&dtrace_provider_lock
);
16431 if (cmd
== DTRACEIOC_PROBEMATCH
) {
16432 dtrace_probekey(&desc
, &pkey
);
16433 pkey
.dtpk_id
= DTRACE_IDNONE
;
16436 dtrace_cred2priv(cr
, &priv
, &uid
, &zoneid
);
16438 lck_mtx_lock(&dtrace_lock
);
16440 if (cmd
== DTRACEIOC_PROBEMATCH
) {
16441 /* Quiet compiler warning */
16442 for (i
= desc
.dtpd_id
; i
<= (dtrace_id_t
)dtrace_nprobes
; i
++) {
16443 if ((probe
= dtrace_probes
[i
- 1]) != NULL
&&
16444 (m
= dtrace_match_probe(probe
, &pkey
,
16445 priv
, uid
, zoneid
)) != 0)
16450 lck_mtx_unlock(&dtrace_lock
);
16455 /* Quiet compiler warning */
16456 for (i
= desc
.dtpd_id
; i
<= (dtrace_id_t
)dtrace_nprobes
; i
++) {
16457 if ((probe
= dtrace_probes
[i
- 1]) != NULL
&&
16458 dtrace_match_priv(probe
, priv
, uid
, zoneid
))
16463 if (probe
== NULL
) {
16464 lck_mtx_unlock(&dtrace_lock
);
16468 dtrace_probe_description(probe
, &desc
);
16469 lck_mtx_unlock(&dtrace_lock
);
16471 if (copyout(&desc
, arg
, sizeof (desc
)) != 0)
16477 case DTRACEIOC_PROBEARG
: {
16478 dtrace_argdesc_t desc
;
16479 dtrace_probe_t
*probe
;
16480 dtrace_provider_t
*prov
;
16482 if (copyin(arg
, &desc
, sizeof (desc
)) != 0)
16485 if (desc
.dtargd_id
== DTRACE_IDNONE
)
16488 if (desc
.dtargd_ndx
== DTRACE_ARGNONE
)
16491 lck_mtx_lock(&dtrace_provider_lock
);
16492 lck_mtx_lock(&mod_lock
);
16493 lck_mtx_lock(&dtrace_lock
);
16495 /* Quiet compiler warning */
16496 if (desc
.dtargd_id
> (dtrace_id_t
)dtrace_nprobes
) {
16497 lck_mtx_unlock(&dtrace_lock
);
16498 lck_mtx_unlock(&mod_lock
);
16499 lck_mtx_unlock(&dtrace_provider_lock
);
16503 if ((probe
= dtrace_probes
[desc
.dtargd_id
- 1]) == NULL
) {
16504 lck_mtx_unlock(&dtrace_lock
);
16505 lck_mtx_unlock(&mod_lock
);
16506 lck_mtx_unlock(&dtrace_provider_lock
);
16510 lck_mtx_unlock(&dtrace_lock
);
16512 prov
= probe
->dtpr_provider
;
16514 if (prov
->dtpv_pops
.dtps_getargdesc
== NULL
) {
16516 * There isn't any typed information for this probe.
16517 * Set the argument number to DTRACE_ARGNONE.
16519 desc
.dtargd_ndx
= DTRACE_ARGNONE
;
16521 desc
.dtargd_native
[0] = '\0';
16522 desc
.dtargd_xlate
[0] = '\0';
16523 desc
.dtargd_mapping
= desc
.dtargd_ndx
;
16525 prov
->dtpv_pops
.dtps_getargdesc(prov
->dtpv_arg
,
16526 probe
->dtpr_id
, probe
->dtpr_arg
, &desc
);
16529 lck_mtx_unlock(&mod_lock
);
16530 lck_mtx_unlock(&dtrace_provider_lock
);
16532 if (copyout(&desc
, arg
, sizeof (desc
)) != 0)
16538 case DTRACEIOC_GO
: {
16539 processorid_t cpuid
;
16540 rval
= dtrace_state_go(state
, &cpuid
);
16545 if (copyout(&cpuid
, arg
, sizeof (cpuid
)) != 0)
16551 case DTRACEIOC_STOP
: {
16552 processorid_t cpuid
;
16554 lck_mtx_lock(&dtrace_lock
);
16555 rval
= dtrace_state_stop(state
, &cpuid
);
16556 lck_mtx_unlock(&dtrace_lock
);
16561 if (copyout(&cpuid
, arg
, sizeof (cpuid
)) != 0)
16567 case DTRACEIOC_DOFGET
: {
16568 dof_hdr_t hdr
, *dof
;
16571 if (copyin(arg
, &hdr
, sizeof (hdr
)) != 0)
16574 lck_mtx_lock(&dtrace_lock
);
16575 dof
= dtrace_dof_create(state
);
16576 lck_mtx_unlock(&dtrace_lock
);
16578 len
= MIN(hdr
.dofh_loadsz
, dof
->dofh_loadsz
);
16579 rval
= copyout(dof
, arg
, len
);
16580 dtrace_dof_destroy(dof
);
16582 return (rval
== 0 ? 0 : EFAULT
);
16585 case DTRACEIOC_AGGSNAP
:
16586 case DTRACEIOC_BUFSNAP
: {
16587 dtrace_bufdesc_t desc
;
16589 dtrace_buffer_t
*buf
;
16591 if (copyin(arg
, &desc
, sizeof (desc
)) != 0)
16594 if ((int)desc
.dtbd_cpu
< 0 || desc
.dtbd_cpu
>= NCPU
)
16597 lck_mtx_lock(&dtrace_lock
);
16599 if (cmd
== DTRACEIOC_BUFSNAP
) {
16600 buf
= &state
->dts_buffer
[desc
.dtbd_cpu
];
16602 buf
= &state
->dts_aggbuffer
[desc
.dtbd_cpu
];
16605 if (buf
->dtb_flags
& (DTRACEBUF_RING
| DTRACEBUF_FILL
)) {
16606 size_t sz
= buf
->dtb_offset
;
16608 if (state
->dts_activity
!= DTRACE_ACTIVITY_STOPPED
) {
16609 lck_mtx_unlock(&dtrace_lock
);
16614 * If this buffer has already been consumed, we're
16615 * going to indicate that there's nothing left here
16618 if (buf
->dtb_flags
& DTRACEBUF_CONSUMED
) {
16619 lck_mtx_unlock(&dtrace_lock
);
16621 desc
.dtbd_size
= 0;
16622 desc
.dtbd_drops
= 0;
16623 desc
.dtbd_errors
= 0;
16624 desc
.dtbd_oldest
= 0;
16625 sz
= sizeof (desc
);
16627 if (copyout(&desc
, arg
, sz
) != 0)
16634 * If this is a ring buffer that has wrapped, we want
16635 * to copy the whole thing out.
16637 if (buf
->dtb_flags
& DTRACEBUF_WRAPPED
) {
16638 dtrace_buffer_polish(buf
);
16639 sz
= buf
->dtb_size
;
16642 if (copyout(buf
->dtb_tomax
, (user_addr_t
)desc
.dtbd_data
, sz
) != 0) {
16643 lck_mtx_unlock(&dtrace_lock
);
16647 desc
.dtbd_size
= sz
;
16648 desc
.dtbd_drops
= buf
->dtb_drops
;
16649 desc
.dtbd_errors
= buf
->dtb_errors
;
16650 desc
.dtbd_oldest
= buf
->dtb_xamot_offset
;
16651 desc
.dtbd_timestamp
= dtrace_gethrtime();
16653 lck_mtx_unlock(&dtrace_lock
);
16655 if (copyout(&desc
, arg
, sizeof (desc
)) != 0)
16658 buf
->dtb_flags
|= DTRACEBUF_CONSUMED
;
16663 if (buf
->dtb_tomax
== NULL
) {
16664 ASSERT(buf
->dtb_xamot
== NULL
);
16665 lck_mtx_unlock(&dtrace_lock
);
16669 cached
= buf
->dtb_tomax
;
16670 ASSERT(!(buf
->dtb_flags
& DTRACEBUF_NOSWITCH
));
16672 dtrace_xcall(desc
.dtbd_cpu
,
16673 (dtrace_xcall_t
)dtrace_buffer_switch
, buf
);
16675 state
->dts_errors
+= buf
->dtb_xamot_errors
;
16678 * If the buffers did not actually switch, then the cross call
16679 * did not take place -- presumably because the given CPU is
16680 * not in the ready set. If this is the case, we'll return
16683 if (buf
->dtb_tomax
== cached
) {
16684 ASSERT(buf
->dtb_xamot
!= cached
);
16685 lck_mtx_unlock(&dtrace_lock
);
16689 ASSERT(cached
== buf
->dtb_xamot
);
16692 * We have our snapshot; now copy it out.
16694 if (copyout(buf
->dtb_xamot
, (user_addr_t
)desc
.dtbd_data
,
16695 buf
->dtb_xamot_offset
) != 0) {
16696 lck_mtx_unlock(&dtrace_lock
);
16700 desc
.dtbd_size
= buf
->dtb_xamot_offset
;
16701 desc
.dtbd_drops
= buf
->dtb_xamot_drops
;
16702 desc
.dtbd_errors
= buf
->dtb_xamot_errors
;
16703 desc
.dtbd_oldest
= 0;
16704 desc
.dtbd_timestamp
= buf
->dtb_switched
;
16706 lck_mtx_unlock(&dtrace_lock
);
16709 * Finally, copy out the buffer description.
16711 if (copyout(&desc
, arg
, sizeof (desc
)) != 0)
16717 case DTRACEIOC_CONF
: {
16718 dtrace_conf_t conf
;
16720 bzero(&conf
, sizeof (conf
));
16721 conf
.dtc_difversion
= DIF_VERSION
;
16722 conf
.dtc_difintregs
= DIF_DIR_NREGS
;
16723 conf
.dtc_diftupregs
= DIF_DTR_NREGS
;
16724 conf
.dtc_ctfmodel
= CTF_MODEL_NATIVE
;
16726 if (copyout(&conf
, arg
, sizeof (conf
)) != 0)
16732 case DTRACEIOC_STATUS
: {
16733 dtrace_status_t stat
;
16734 dtrace_dstate_t
*dstate
;
16739 * See the comment in dtrace_state_deadman() for the reason
16740 * for setting dts_laststatus to INT64_MAX before setting
16741 * it to the correct value.
16743 state
->dts_laststatus
= INT64_MAX
;
16744 dtrace_membar_producer();
16745 state
->dts_laststatus
= dtrace_gethrtime();
16747 bzero(&stat
, sizeof (stat
));
16749 lck_mtx_lock(&dtrace_lock
);
16751 if (state
->dts_activity
== DTRACE_ACTIVITY_INACTIVE
) {
16752 lck_mtx_unlock(&dtrace_lock
);
16756 if (state
->dts_activity
== DTRACE_ACTIVITY_DRAINING
)
16757 stat
.dtst_exiting
= 1;
16759 nerrs
= state
->dts_errors
;
16760 dstate
= &state
->dts_vstate
.dtvs_dynvars
;
16762 for (i
= 0; i
< (int)NCPU
; i
++) {
16763 dtrace_dstate_percpu_t
*dcpu
= &dstate
->dtds_percpu
[i
];
16765 stat
.dtst_dyndrops
+= dcpu
->dtdsc_drops
;
16766 stat
.dtst_dyndrops_dirty
+= dcpu
->dtdsc_dirty_drops
;
16767 stat
.dtst_dyndrops_rinsing
+= dcpu
->dtdsc_rinsing_drops
;
16769 if (state
->dts_buffer
[i
].dtb_flags
& DTRACEBUF_FULL
)
16770 stat
.dtst_filled
++;
16772 nerrs
+= state
->dts_buffer
[i
].dtb_errors
;
16774 for (j
= 0; j
< state
->dts_nspeculations
; j
++) {
16775 dtrace_speculation_t
*spec
;
16776 dtrace_buffer_t
*buf
;
16778 spec
= &state
->dts_speculations
[j
];
16779 buf
= &spec
->dtsp_buffer
[i
];
16780 stat
.dtst_specdrops
+= buf
->dtb_xamot_drops
;
16784 stat
.dtst_specdrops_busy
= state
->dts_speculations_busy
;
16785 stat
.dtst_specdrops_unavail
= state
->dts_speculations_unavail
;
16786 stat
.dtst_stkstroverflows
= state
->dts_stkstroverflows
;
16787 stat
.dtst_dblerrors
= state
->dts_dblerrors
;
16789 (state
->dts_activity
== DTRACE_ACTIVITY_KILLED
);
16790 stat
.dtst_errors
= nerrs
;
16792 lck_mtx_unlock(&dtrace_lock
);
16794 if (copyout(&stat
, arg
, sizeof (stat
)) != 0)
16800 case DTRACEIOC_FORMAT
: {
16801 dtrace_fmtdesc_t fmt
;
16805 if (copyin(arg
, &fmt
, sizeof (fmt
)) != 0)
16808 lck_mtx_lock(&dtrace_lock
);
16810 if (fmt
.dtfd_format
== 0 ||
16811 fmt
.dtfd_format
> state
->dts_nformats
) {
16812 lck_mtx_unlock(&dtrace_lock
);
16817 * Format strings are allocated contiguously and they are
16818 * never freed; if a format index is less than the number
16819 * of formats, we can assert that the format map is non-NULL
16820 * and that the format for the specified index is non-NULL.
16822 ASSERT(state
->dts_formats
!= NULL
);
16823 str
= state
->dts_formats
[fmt
.dtfd_format
- 1];
16824 ASSERT(str
!= NULL
);
16826 len
= strlen(str
) + 1;
16828 if (len
> fmt
.dtfd_length
) {
16829 fmt
.dtfd_length
= len
;
16831 if (copyout(&fmt
, arg
, sizeof (fmt
)) != 0) {
16832 lck_mtx_unlock(&dtrace_lock
);
16836 if (copyout(str
, (user_addr_t
)fmt
.dtfd_string
, len
) != 0) {
16837 lck_mtx_unlock(&dtrace_lock
);
16842 lck_mtx_unlock(&dtrace_lock
);
16846 case DTRACEIOC_MODUUIDSLIST
: {
16847 size_t module_uuids_list_size
;
16848 dtrace_module_uuids_list_t
* uuids_list
;
16849 uint64_t dtmul_count
;
16852 * Security restrictions make this operation illegal, if this is enabled DTrace
16853 * must refuse to provide any fbt probes.
16855 if (dtrace_is_restricted()) {
16856 cmn_err(CE_WARN
, "security restrictions disallow DTRACEIOC_MODUUIDSLIST");
16861 * Fail if the kernel symbol mode makes this operation illegal.
16862 * Both NEVER & ALWAYS_FROM_KERNEL are permanent states, it is legal to check
16863 * for them without holding the dtrace_lock.
16865 if (dtrace_kernel_symbol_mode
== DTRACE_KERNEL_SYMBOLS_NEVER
||
16866 dtrace_kernel_symbol_mode
== DTRACE_KERNEL_SYMBOLS_ALWAYS_FROM_KERNEL
) {
16867 cmn_err(CE_WARN
, "dtrace_kernel_symbol_mode of %u disallows DTRACEIOC_MODUUIDSLIST", dtrace_kernel_symbol_mode
);
16872 * Read the number of symbolsdesc structs being passed in.
16874 if (copyin(arg
+ offsetof(dtrace_module_uuids_list_t
, dtmul_count
),
16876 sizeof(dtmul_count
))) {
16877 cmn_err(CE_WARN
, "failed to copyin dtmul_count");
16882 * Range check the count. More than 2k kexts is probably an error.
16884 if (dtmul_count
> 2048) {
16885 cmn_err(CE_WARN
, "dtmul_count is not valid");
16890 * For all queries, we return EINVAL when the user specified
16891 * count does not match the actual number of modules we find
16894 * If the user specified count is zero, then this serves as a
16895 * simple query to count the available modules in need of symbols.
16900 if (dtmul_count
== 0)
16902 lck_mtx_lock(&mod_lock
);
16903 struct modctl
* ctl
= dtrace_modctl_list
;
16905 /* Update the private probes bit */
16906 if (dtrace_provide_private_probes
)
16907 ctl
->mod_flags
|= MODCTL_FBT_PROVIDE_PRIVATE_PROBES
;
16909 ASSERT(!MOD_HAS_USERSPACE_SYMBOLS(ctl
));
16910 if (!MOD_SYMBOLS_DONE(ctl
)) {
16914 ctl
= ctl
->mod_next
;
16916 lck_mtx_unlock(&mod_lock
);
16918 if (copyout(&dtmul_count
, arg
, sizeof (dtmul_count
)) != 0)
16925 * If we reach this point, then we have a request for full list data.
16926 * Allocate a correctly sized structure and copyin the data.
16928 module_uuids_list_size
= DTRACE_MODULE_UUIDS_LIST_SIZE(dtmul_count
);
16929 if ((uuids_list
= kmem_alloc(module_uuids_list_size
, KM_SLEEP
)) == NULL
)
16932 /* NOTE! We can no longer exit this method via return */
16933 if (copyin(arg
, uuids_list
, module_uuids_list_size
) != 0) {
16934 cmn_err(CE_WARN
, "failed copyin of dtrace_module_uuids_list_t");
16936 goto moduuidslist_cleanup
;
16940 * Check that the count didn't change between the first copyin and the second.
16942 if (uuids_list
->dtmul_count
!= dtmul_count
) {
16944 goto moduuidslist_cleanup
;
16948 * Build the list of UUID's that need symbols
16950 lck_mtx_lock(&mod_lock
);
16954 struct modctl
* ctl
= dtrace_modctl_list
;
16956 /* Update the private probes bit */
16957 if (dtrace_provide_private_probes
)
16958 ctl
->mod_flags
|= MODCTL_FBT_PROVIDE_PRIVATE_PROBES
;
16961 * We assume that userspace symbols will be "better" than kernel level symbols,
16962 * as userspace can search for dSYM(s) and symbol'd binaries. Even if kernel syms
16963 * are available, add user syms if the module might use them.
16965 ASSERT(!MOD_HAS_USERSPACE_SYMBOLS(ctl
));
16966 if (!MOD_SYMBOLS_DONE(ctl
)) {
16967 UUID
* uuid
= &uuids_list
->dtmul_uuid
[dtmul_count
];
16968 if (dtmul_count
++ < uuids_list
->dtmul_count
) {
16969 memcpy(uuid
, ctl
->mod_uuid
, sizeof(UUID
));
16972 ctl
= ctl
->mod_next
;
16975 lck_mtx_unlock(&mod_lock
);
16977 if (uuids_list
->dtmul_count
< dtmul_count
)
16980 uuids_list
->dtmul_count
= dtmul_count
;
16983 * Copyout the symbols list (or at least the count!)
16985 if (copyout(uuids_list
, arg
, module_uuids_list_size
) != 0) {
16986 cmn_err(CE_WARN
, "failed copyout of dtrace_symbolsdesc_list_t");
16990 moduuidslist_cleanup
:
16992 * If we had to allocate struct memory, free it.
16994 if (uuids_list
!= NULL
) {
16995 kmem_free(uuids_list
, module_uuids_list_size
);
17001 case DTRACEIOC_PROVMODSYMS
: {
17002 size_t module_symbols_size
;
17003 dtrace_module_symbols_t
* module_symbols
;
17004 uint64_t dtmodsyms_count
;
17007 * Security restrictions make this operation illegal, if this is enabled DTrace
17008 * must refuse to provide any fbt probes.
17010 if (dtrace_is_restricted()) {
17011 cmn_err(CE_WARN
, "security restrictions disallow DTRACEIOC_MODUUIDSLIST");
17016 * Fail if the kernel symbol mode makes this operation illegal.
17017 * Both NEVER & ALWAYS_FROM_KERNEL are permanent states, it is legal to check
17018 * for them without holding the dtrace_lock.
17020 if (dtrace_kernel_symbol_mode
== DTRACE_KERNEL_SYMBOLS_NEVER
||
17021 dtrace_kernel_symbol_mode
== DTRACE_KERNEL_SYMBOLS_ALWAYS_FROM_KERNEL
) {
17022 cmn_err(CE_WARN
, "dtrace_kernel_symbol_mode of %u disallows DTRACEIOC_PROVMODSYMS", dtrace_kernel_symbol_mode
);
17027 * Read the number of module symbols structs being passed in.
17029 if (copyin(arg
+ offsetof(dtrace_module_symbols_t
, dtmodsyms_count
),
17031 sizeof(dtmodsyms_count
))) {
17032 cmn_err(CE_WARN
, "failed to copyin dtmodsyms_count");
17037 * Range check the count. How much data can we pass around?
17040 if (dtmodsyms_count
== 0 || (dtmodsyms_count
> 100 * 1024)) {
17041 cmn_err(CE_WARN
, "dtmodsyms_count is not valid");
17046 * Allocate a correctly sized structure and copyin the data.
17048 module_symbols_size
= DTRACE_MODULE_SYMBOLS_SIZE(dtmodsyms_count
);
17049 if ((module_symbols
= kmem_alloc(module_symbols_size
, KM_SLEEP
)) == NULL
)
17054 /* NOTE! We can no longer exit this method via return */
17055 if (copyin(arg
, module_symbols
, module_symbols_size
) != 0) {
17056 cmn_err(CE_WARN
, "failed copyin of dtrace_module_symbols_t, symbol count %llu", module_symbols
->dtmodsyms_count
);
17058 goto module_symbols_cleanup
;
17062 * Check that the count didn't change between the first copyin and the second.
17064 if (module_symbols
->dtmodsyms_count
!= dtmodsyms_count
) {
17066 goto module_symbols_cleanup
;
17070 * Find the modctl to add symbols to.
17072 lck_mtx_lock(&dtrace_provider_lock
);
17073 lck_mtx_lock(&mod_lock
);
17075 struct modctl
* ctl
= dtrace_modctl_list
;
17077 /* Update the private probes bit */
17078 if (dtrace_provide_private_probes
)
17079 ctl
->mod_flags
|= MODCTL_FBT_PROVIDE_PRIVATE_PROBES
;
17081 ASSERT(!MOD_HAS_USERSPACE_SYMBOLS(ctl
));
17082 if (MOD_HAS_UUID(ctl
) && !MOD_SYMBOLS_DONE(ctl
)) {
17083 if (memcmp(module_symbols
->dtmodsyms_uuid
, ctl
->mod_uuid
, sizeof(UUID
)) == 0) {
17085 ctl
->mod_user_symbols
= module_symbols
;
17089 ctl
= ctl
->mod_next
;
17093 dtrace_provider_t
*prv
;
17096 * We're going to call each providers per-module provide operation
17097 * specifying only this module.
17099 for (prv
= dtrace_provider
; prv
!= NULL
; prv
= prv
->dtpv_next
)
17100 prv
->dtpv_pops
.dtps_provide_module(prv
->dtpv_arg
, ctl
);
17103 * We gave every provider a chance to provide with the user syms, go ahead and clear them
17105 ctl
->mod_user_symbols
= NULL
; /* MUST reset this to clear HAS_USERSPACE_SYMBOLS */
17108 lck_mtx_unlock(&mod_lock
);
17109 lck_mtx_unlock(&dtrace_provider_lock
);
17111 module_symbols_cleanup
:
17113 * If we had to allocate struct memory, free it.
17115 if (module_symbols
!= NULL
) {
17116 kmem_free(module_symbols
, module_symbols_size
);
17122 case DTRACEIOC_PROCWAITFOR
: {
17123 dtrace_procdesc_t pdesc
= {
17128 if ((rval
= copyin(arg
, &pdesc
, sizeof(pdesc
))) != 0)
17129 goto proc_waitfor_error
;
17131 if ((rval
= dtrace_proc_waitfor(&pdesc
)) != 0)
17132 goto proc_waitfor_error
;
17134 if ((rval
= copyout(&pdesc
, arg
, sizeof(pdesc
))) != 0)
17135 goto proc_waitfor_error
;
17139 proc_waitfor_error
:
17140 /* The process was suspended, revert this since the client will not do it. */
17141 if (pdesc
.p_pid
!= -1) {
17142 proc_t
*proc
= proc_find(pdesc
.p_pid
);
17143 if (proc
!= PROC_NULL
) {
17144 task_pidresume(proc
->task
);
17160 * APPLE NOTE: dtrace_detach not implemented
17162 #if !defined(__APPLE__)
17165 dtrace_detach(dev_info_t
*dip
, ddi_detach_cmd_t cmd
)
17167 dtrace_state_t
*state
;
17174 return (DDI_SUCCESS
);
17177 return (DDI_FAILURE
);
17180 lck_mtx_lock(&cpu_lock
);
17181 lck_mtx_lock(&dtrace_provider_lock
);
17182 lck_mtx_lock(&dtrace_lock
);
17184 ASSERT(dtrace_opens
== 0);
17186 if (dtrace_helpers
> 0) {
17187 lck_mtx_unlock(&dtrace_lock
);
17188 lck_mtx_unlock(&dtrace_provider_lock
);
17189 lck_mtx_unlock(&cpu_lock
);
17190 return (DDI_FAILURE
);
17193 if (dtrace_unregister((dtrace_provider_id_t
)dtrace_provider
) != 0) {
17194 lck_mtx_unlock(&dtrace_lock
);
17195 lck_mtx_unlock(&dtrace_provider_lock
);
17196 lck_mtx_unlock(&cpu_lock
);
17197 return (DDI_FAILURE
);
17200 dtrace_provider
= NULL
;
17202 if ((state
= dtrace_anon_grab()) != NULL
) {
17204 * If there were ECBs on this state, the provider should
17205 * have not been allowed to detach; assert that there is
17208 ASSERT(state
->dts_necbs
== 0);
17209 dtrace_state_destroy(state
);
17212 * If we're being detached with anonymous state, we need to
17213 * indicate to the kernel debugger that DTrace is now inactive.
17215 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE
);
17218 bzero(&dtrace_anon
, sizeof (dtrace_anon_t
));
17219 unregister_cpu_setup_func((cpu_setup_func_t
*)dtrace_cpu_setup
, NULL
);
17220 dtrace_cpu_init
= NULL
;
17221 dtrace_helpers_cleanup
= NULL
;
17222 dtrace_helpers_fork
= NULL
;
17223 dtrace_cpustart_init
= NULL
;
17224 dtrace_cpustart_fini
= NULL
;
17225 dtrace_debugger_init
= NULL
;
17226 dtrace_debugger_fini
= NULL
;
17227 dtrace_kreloc_init
= NULL
;
17228 dtrace_kreloc_fini
= NULL
;
17229 dtrace_modload
= NULL
;
17230 dtrace_modunload
= NULL
;
17232 lck_mtx_unlock(&cpu_lock
);
17234 if (dtrace_helptrace_enabled
) {
17235 kmem_free(dtrace_helptrace_buffer
, dtrace_helptrace_bufsize
);
17236 dtrace_helptrace_buffer
= NULL
;
17239 kmem_free(dtrace_probes
, dtrace_nprobes
* sizeof (dtrace_probe_t
*));
17240 dtrace_probes
= NULL
;
17241 dtrace_nprobes
= 0;
17243 dtrace_hash_destroy(dtrace_bymod
);
17244 dtrace_hash_destroy(dtrace_byfunc
);
17245 dtrace_hash_destroy(dtrace_byname
);
17246 dtrace_bymod
= NULL
;
17247 dtrace_byfunc
= NULL
;
17248 dtrace_byname
= NULL
;
17250 kmem_cache_destroy(dtrace_state_cache
);
17251 vmem_destroy(dtrace_minor
);
17252 vmem_destroy(dtrace_arena
);
17254 if (dtrace_toxrange
!= NULL
) {
17255 kmem_free(dtrace_toxrange
,
17256 dtrace_toxranges_max
* sizeof (dtrace_toxrange_t
));
17257 dtrace_toxrange
= NULL
;
17258 dtrace_toxranges
= 0;
17259 dtrace_toxranges_max
= 0;
17262 ddi_remove_minor_node(dtrace_devi
, NULL
);
17263 dtrace_devi
= NULL
;
17265 ddi_soft_state_fini(&dtrace_softstate
);
17267 ASSERT(dtrace_vtime_references
== 0);
17268 ASSERT(dtrace_opens
== 0);
17269 ASSERT(dtrace_retained
== NULL
);
17271 lck_mtx_unlock(&dtrace_lock
);
17272 lck_mtx_unlock(&dtrace_provider_lock
);
17275 * We don't destroy the task queue until after we have dropped our
17276 * locks (taskq_destroy() may block on running tasks). To prevent
17277 * attempting to do work after we have effectively detached but before
17278 * the task queue has been destroyed, all tasks dispatched via the
17279 * task queue must check that DTrace is still attached before
17280 * performing any operation.
17282 taskq_destroy(dtrace_taskq
);
17283 dtrace_taskq
= NULL
;
17285 return (DDI_SUCCESS
);
17287 #endif /* __APPLE__ */
17289 d_open_t _dtrace_open
, helper_open
;
17290 d_close_t _dtrace_close
, helper_close
;
17291 d_ioctl_t _dtrace_ioctl
, helper_ioctl
;
17294 _dtrace_open(dev_t dev
, int flags
, int devtype
, struct proc
*p
)
17297 dev_t locdev
= dev
;
17299 return dtrace_open( &locdev
, flags
, devtype
, CRED());
17303 helper_open(dev_t dev
, int flags
, int devtype
, struct proc
*p
)
17305 #pragma unused(dev,flags,devtype,p)
17310 _dtrace_close(dev_t dev
, int flags
, int devtype
, struct proc
*p
)
17313 return dtrace_close( dev
, flags
, devtype
, CRED());
17317 helper_close(dev_t dev
, int flags
, int devtype
, struct proc
*p
)
17319 #pragma unused(dev,flags,devtype,p)
17324 _dtrace_ioctl(dev_t dev
, u_long cmd
, caddr_t data
, int fflag
, struct proc
*p
)
17328 user_addr_t uaddrp
;
17330 if (proc_is64bit(p
))
17331 uaddrp
= *(user_addr_t
*)data
;
17333 uaddrp
= (user_addr_t
) *(uint32_t *)data
;
17335 err
= dtrace_ioctl(dev
, cmd
, uaddrp
, fflag
, CRED(), &rv
);
17337 /* Darwin's BSD ioctls only return -1 or zero. Overload errno to mimic Solaris. 20 bits suffice. */
17339 ASSERT( (err
& 0xfffff000) == 0 );
17340 return (err
& 0xfff); /* ioctl will return -1 and will set errno to an error code < 4096 */
17341 } else if (rv
!= 0) {
17342 ASSERT( (rv
& 0xfff00000) == 0 );
17343 return (((rv
& 0xfffff) << 12)); /* ioctl will return -1 and will set errno to a value >= 4096 */
17349 helper_ioctl(dev_t dev
, u_long cmd
, caddr_t data
, int fflag
, struct proc
*p
)
17351 #pragma unused(dev,fflag,p)
17354 err
= dtrace_ioctl_helper(cmd
, data
, &rv
);
17355 /* Darwin's BSD ioctls only return -1 or zero. Overload errno to mimic Solaris. 20 bits suffice. */
17357 ASSERT( (err
& 0xfffff000) == 0 );
17358 return (err
& 0xfff); /* ioctl will return -1 and will set errno to an error code < 4096 */
17359 } else if (rv
!= 0) {
17360 ASSERT( (rv
& 0xfff00000) == 0 );
17361 return (((rv
& 0xfffff) << 12)); /* ioctl will return -1 and will set errno to a value >= 4096 */
17366 #define HELPER_MAJOR -24 /* let the kernel pick the device number */
17369 * A struct describing which functions will get invoked for certain
17372 static struct cdevsw helper_cdevsw
=
17374 helper_open
, /* open */
17375 helper_close
, /* close */
17376 eno_rdwrt
, /* read */
17377 eno_rdwrt
, /* write */
17378 helper_ioctl
, /* ioctl */
17379 (stop_fcn_t
*)nulldev
, /* stop */
17380 (reset_fcn_t
*)nulldev
, /* reset */
17382 eno_select
, /* select */
17383 eno_mmap
, /* mmap */
17384 eno_strat
, /* strategy */
17385 eno_getc
, /* getc */
17386 eno_putc
, /* putc */
17390 static int helper_majdevno
= 0;
17392 static int gDTraceInited
= 0;
17395 helper_init( void )
17398 * Once the "helper" is initialized, it can take ioctl calls that use locks
17399 * and zones initialized in dtrace_init. Make certain dtrace_init was called
17403 if (!gDTraceInited
) {
17404 panic("helper_init before dtrace_init\n");
17407 if (0 >= helper_majdevno
)
17409 helper_majdevno
= cdevsw_add(HELPER_MAJOR
, &helper_cdevsw
);
17411 if (helper_majdevno
< 0) {
17412 printf("helper_init: failed to allocate a major number!\n");
17416 if (NULL
== devfs_make_node( makedev(helper_majdevno
, 0), DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0666,
17417 DTRACEMNR_HELPER
, 0 )) {
17418 printf("dtrace_init: failed to devfs_make_node for helper!\n");
17422 panic("helper_init: called twice!\n");
17425 #undef HELPER_MAJOR
17428 * Called with DEVFS_LOCK held, so vmem_alloc's underlying blist structures are protected.
17431 dtrace_clone_func(dev_t dev
, int action
)
17433 #pragma unused(dev)
17435 if (action
== DEVFS_CLONE_ALLOC
) {
17436 if (NULL
== dtrace_minor
) /* Arena not created yet!?! */
17440 * Propose a minor number, namely the next number that vmem_alloc() will return.
17441 * Immediately put it back in play by calling vmem_free(). FIXME.
17443 int ret
= (int)(uintptr_t)vmem_alloc(dtrace_minor
, 1, VM_BESTFIT
| VM_SLEEP
);
17445 vmem_free(dtrace_minor
, (void *)(uintptr_t)ret
, 1);
17450 else if (action
== DEVFS_CLONE_FREE
) {
17456 #define DTRACE_MAJOR -24 /* let the kernel pick the device number */
17458 static struct cdevsw dtrace_cdevsw
=
17460 _dtrace_open
, /* open */
17461 _dtrace_close
, /* close */
17462 eno_rdwrt
, /* read */
17463 eno_rdwrt
, /* write */
17464 _dtrace_ioctl
, /* ioctl */
17465 (stop_fcn_t
*)nulldev
, /* stop */
17466 (reset_fcn_t
*)nulldev
, /* reset */
17468 eno_select
, /* select */
17469 eno_mmap
, /* mmap */
17470 eno_strat
, /* strategy */
17471 eno_getc
, /* getc */
17472 eno_putc
, /* putc */
17476 lck_attr_t
* dtrace_lck_attr
;
17477 lck_grp_attr_t
* dtrace_lck_grp_attr
;
17478 lck_grp_t
* dtrace_lck_grp
;
17480 static int gMajDevNo
;
17483 dtrace_init( void )
17485 if (0 == gDTraceInited
) {
17487 size_t size
= sizeof(dtrace_buffer_memory_maxsize
);
17490 * DTrace allocates buffers based on the maximum number
17491 * of enabled cpus. This call avoids any race when finding
17494 ASSERT(dtrace_max_cpus
== 0);
17495 ncpu
= dtrace_max_cpus
= ml_get_max_cpus();
17498 * Retrieve the size of the physical memory in order to define
17499 * the state buffer memory maximal size. If we cannot retrieve
17500 * this value, we'll consider that we have 1Gb of memory per CPU, that's
17501 * still better than raising a kernel panic.
17503 if (0 != kernel_sysctlbyname("hw.memsize", &dtrace_buffer_memory_maxsize
,
17506 dtrace_buffer_memory_maxsize
= ncpu
* 1024 * 1024 * 1024;
17507 printf("dtrace_init: failed to retrieve the hw.memsize, defaulted to %lld bytes\n",
17508 dtrace_buffer_memory_maxsize
);
17512 * Finally, divide by three to prevent DTrace from eating too
17515 dtrace_buffer_memory_maxsize
/= 3;
17516 ASSERT(dtrace_buffer_memory_maxsize
> 0);
17518 gMajDevNo
= cdevsw_add(DTRACE_MAJOR
, &dtrace_cdevsw
);
17520 if (gMajDevNo
< 0) {
17521 printf("dtrace_init: failed to allocate a major number!\n");
17526 if (NULL
== devfs_make_node_clone( makedev(gMajDevNo
, 0), DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0666,
17527 dtrace_clone_func
, DTRACEMNR_DTRACE
, 0 )) {
17528 printf("dtrace_init: failed to devfs_make_node_clone for dtrace!\n");
17533 #if defined(DTRACE_MEMORY_ZONES)
17535 * Initialize the dtrace kalloc-emulation zones.
17537 dtrace_alloc_init();
17538 #endif /* DTRACE_MEMORY_ZONES */
17541 * Allocate the dtrace_probe_t zone
17543 dtrace_probe_t_zone
= zinit(sizeof(dtrace_probe_t
),
17544 1024 * sizeof(dtrace_probe_t
),
17545 sizeof(dtrace_probe_t
),
17546 "dtrace.dtrace_probe_t");
17549 * Create the dtrace lock group and attrs.
17551 dtrace_lck_attr
= lck_attr_alloc_init();
17552 dtrace_lck_grp_attr
= lck_grp_attr_alloc_init();
17553 dtrace_lck_grp
= lck_grp_alloc_init("dtrace", dtrace_lck_grp_attr
);
17556 * We have to initialize all locks explicitly
17558 lck_mtx_init(&dtrace_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
17559 lck_mtx_init(&dtrace_provider_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
17560 lck_mtx_init(&dtrace_meta_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
17561 lck_mtx_init(&dtrace_procwaitfor_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
17563 lck_mtx_init(&dtrace_errlock
, dtrace_lck_grp
, dtrace_lck_attr
);
17565 lck_rw_init(&dtrace_dof_mode_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
17568 * The cpu_core structure consists of per-CPU state available in any context.
17569 * On some architectures, this may mean that the page(s) containing the
17570 * NCPU-sized array of cpu_core structures must be locked in the TLB -- it
17571 * is up to the platform to assure that this is performed properly. Note that
17572 * the structure is sized to avoid false sharing.
17574 lck_mtx_init(&cpu_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
17575 lck_mtx_init(&cyc_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
17576 lck_mtx_init(&mod_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
17579 * Initialize the CPU offline/online hooks.
17581 dtrace_install_cpu_hooks();
17583 dtrace_modctl_list
= NULL
;
17585 cpu_core
= (cpu_core_t
*)kmem_zalloc( ncpu
* sizeof(cpu_core_t
), KM_SLEEP
);
17586 for (i
= 0; i
< ncpu
; ++i
) {
17587 lck_mtx_init(&cpu_core
[i
].cpuc_pid_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
17590 cpu_list
= (dtrace_cpu_t
*)kmem_zalloc( ncpu
* sizeof(dtrace_cpu_t
), KM_SLEEP
);
17591 for (i
= 0; i
< ncpu
; ++i
) {
17592 cpu_list
[i
].cpu_id
= (processorid_t
)i
;
17593 cpu_list
[i
].cpu_next
= &(cpu_list
[(i
+1) % ncpu
]);
17594 LIST_INIT(&cpu_list
[i
].cpu_cyc_list
);
17595 lck_rw_init(&cpu_list
[i
].cpu_ft_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
17598 lck_mtx_lock(&cpu_lock
);
17599 for (i
= 0; i
< ncpu
; ++i
)
17600 /* FIXME: track CPU configuration a la CHUD Processor Pref Pane. */
17601 dtrace_cpu_setup_initial( (processorid_t
)i
); /* In lieu of register_cpu_setup_func() callback */
17602 lck_mtx_unlock(&cpu_lock
);
17604 (void)dtrace_abs_to_nano(0LL); /* Force once only call to clock_timebase_info (which can take a lock) */
17609 * See dtrace_impl.h for a description of dof modes.
17610 * The default is lazy dof.
17612 * FIXME: Warn if state is LAZY_OFF? It won't break anything, but
17613 * makes no sense...
17615 if (!PE_parse_boot_argn("dtrace_dof_mode", &dtrace_dof_mode
, sizeof (dtrace_dof_mode
))) {
17616 dtrace_dof_mode
= DTRACE_DOF_MODE_LAZY_ON
;
17620 * Sanity check of dof mode value.
17622 switch (dtrace_dof_mode
) {
17623 case DTRACE_DOF_MODE_NEVER
:
17624 case DTRACE_DOF_MODE_LAZY_ON
:
17625 /* valid modes, but nothing else we need to do */
17628 case DTRACE_DOF_MODE_LAZY_OFF
:
17629 case DTRACE_DOF_MODE_NON_LAZY
:
17630 /* Cannot wait for a dtrace_open to init fasttrap */
17635 /* Invalid, clamp to non lazy */
17636 dtrace_dof_mode
= DTRACE_DOF_MODE_NON_LAZY
;
17642 * See dtrace_impl.h for a description of kernel symbol modes.
17643 * The default is to wait for symbols from userspace (lazy symbols).
17645 if (!PE_parse_boot_argn("dtrace_kernel_symbol_mode", &dtrace_kernel_symbol_mode
, sizeof (dtrace_kernel_symbol_mode
))) {
17646 dtrace_kernel_symbol_mode
= DTRACE_KERNEL_SYMBOLS_FROM_USERSPACE
;
17652 panic("dtrace_init: called twice!\n");
17656 dtrace_postinit(void)
17659 * Called from bsd_init after all provider's *_init() routines have been
17660 * run. That way, anonymous DOF enabled under dtrace_attach() is safe
17663 dtrace_attach( (dev_info_t
*)(uintptr_t)makedev(gMajDevNo
, 0), 0 ); /* Punning a dev_t to a dev_info_t* */
17666 * Add the mach_kernel to the module list for lazy processing
17668 struct kmod_info fake_kernel_kmod
;
17669 memset(&fake_kernel_kmod
, 0, sizeof(fake_kernel_kmod
));
17671 strlcpy(fake_kernel_kmod
.name
, "mach_kernel", sizeof(fake_kernel_kmod
.name
));
17672 fake_kernel_kmod
.id
= 1;
17673 fake_kernel_kmod
.address
= g_kernel_kmod_info
.address
;
17674 fake_kernel_kmod
.size
= g_kernel_kmod_info
.size
;
17676 if (dtrace_module_loaded(&fake_kernel_kmod
, 0) != 0) {
17677 printf("dtrace_postinit: Could not register mach_kernel modctl\n");
17680 (void)OSKextRegisterKextsWithDTrace();
17682 #undef DTRACE_MAJOR
17685 * Routines used to register interest in cpu's being added to or removed
17689 register_cpu_setup_func(cpu_setup_func_t
*ignore1
, void *ignore2
)
17691 #pragma unused(ignore1,ignore2)
17695 unregister_cpu_setup_func(cpu_setup_func_t
*ignore1
, void *ignore2
)
17697 #pragma unused(ignore1,ignore2)