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) 2013, 2016, Joyent, Inc. All rights reserved.
24 * Portions Copyright (c) 2013 by Delphix. All rights reserved.
28 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
29 * Use is subject to license terms.
33 * DTrace - Dynamic Tracing for Solaris
35 * This is the implementation of the Solaris Dynamic Tracing framework
36 * (DTrace). The user-visible interface to DTrace is described at length in
37 * the "Solaris Dynamic Tracing Guide". The interfaces between the libdtrace
38 * library, the in-kernel DTrace framework, and the DTrace providers are
39 * described in the block comments in the <sys/dtrace.h> header file. The
40 * internal architecture of DTrace is described in the block comments in the
41 * <sys/dtrace_impl.h> header file. The comments contained within the DTrace
42 * implementation very much assume mastery of all of these sources; if one has
43 * an unanswered question about the implementation, one should consult them
46 * The functions here are ordered roughly as follows:
48 * - Probe context functions
49 * - Probe hashing functions
50 * - Non-probe context utility functions
51 * - Matching functions
52 * - Provider-to-Framework API functions
53 * - Probe management functions
54 * - DIF object functions
56 * - Predicate functions
59 * - Enabling functions
61 * - Anonymous enabling functions
63 * - Consumer state functions
66 * - Driver cookbook functions
68 * Each group of functions begins with a block comment labelled the "DTrace
69 * [Group] Functions", allowing one to find each block by searching forward
70 * on capital-f functions.
72 #include <sys/errno.h>
73 #include <sys/types.h>
76 #include <sys/random.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/sched_prim.h>
97 #include <kern/task.h>
98 #include <netinet/in.h>
99 #include <libkern/sysctl.h>
100 #include <sys/kdebug.h>
103 #include <kern/monotonic.h>
104 #include <machine/monotonic.h>
105 #endif /* MONOTONIC */
107 #include "dtrace_xoroshiro128_plus.h"
109 #include <IOKit/IOPlatformExpert.h>
111 #include <kern/cpu_data.h>
113 extern addr64_t
kvtophys(vm_offset_t va
);
115 extern uint32_t pmap_find_phys(void *, uint64_t);
116 extern boolean_t
pmap_valid_page(uint32_t);
117 extern void OSKextRegisterKextsWithDTrace(void);
118 extern kmod_info_t g_kernel_kmod_info
;
119 extern void commpage_update_dof(boolean_t enabled
);
121 /* Solaris proc_t is the struct. Darwin's proc_t is a pointer to it. */
122 #define proc_t struct proc /* Steer clear of the Darwin typedef for proc_t */
124 #define t_predcache t_dtrace_predcache /* Cosmetic. Helps readability of thread.h */
126 extern void dtrace_suspend(void);
127 extern void dtrace_resume(void);
128 extern void dtrace_early_init(void);
129 extern int dtrace_keep_kernel_symbols(void);
130 extern void dtrace_init(void);
131 extern void helper_init(void);
132 extern void fasttrap_init(void);
134 static int dtrace_lazy_dofs_duplicate(proc_t
*, proc_t
*);
135 extern void dtrace_lazy_dofs_destroy(proc_t
*);
136 extern void dtrace_postinit(void);
138 extern void dtrace_proc_fork(proc_t
*, proc_t
*, int);
139 extern void dtrace_proc_exec(proc_t
*);
140 extern void dtrace_proc_exit(proc_t
*);
143 * DTrace Tunable Variables
145 * The following variables may be dynamically tuned by using sysctl(8), the
146 * variables being stored in the kern.dtrace namespace. For example:
147 * sysctl kern.dtrace.dof_maxsize = 1048575 # 1M
149 * In general, the only variables that one should be tuning this way are those
150 * that affect system-wide DTrace behavior, and for which the default behavior
151 * is undesirable. Most of these variables are tunable on a per-consumer
152 * basis using DTrace options, and need not be tuned on a system-wide basis.
153 * When tuning these variables, avoid pathological values; while some attempt
154 * is made to verify the integrity of these variables, they are not considered
155 * part of the supported interface to DTrace, and they are therefore not
156 * checked comprehensively.
158 uint64_t dtrace_buffer_memory_maxsize
= 0; /* initialized in dtrace_init */
159 uint64_t dtrace_buffer_memory_inuse
= 0;
160 int dtrace_destructive_disallow
= 1;
161 dtrace_optval_t dtrace_nonroot_maxsize
= (16 * 1024 * 1024);
162 size_t dtrace_difo_maxsize
= (256 * 1024);
163 dtrace_optval_t dtrace_dof_maxsize
= (512 * 1024);
164 dtrace_optval_t dtrace_statvar_maxsize
= (16 * 1024);
165 dtrace_optval_t dtrace_statvar_maxsize_max
= (16 * 10 * 1024);
166 size_t dtrace_actions_max
= (16 * 1024);
167 size_t dtrace_retain_max
= 1024;
168 dtrace_optval_t dtrace_helper_actions_max
= 32;
169 dtrace_optval_t dtrace_helper_providers_max
= 64;
170 dtrace_optval_t dtrace_dstate_defsize
= (1 * 1024 * 1024);
171 size_t dtrace_strsize_default
= 256;
172 dtrace_optval_t dtrace_strsize_min
= 8;
173 dtrace_optval_t dtrace_strsize_max
= 65536;
174 dtrace_optval_t dtrace_cleanrate_default
= 990099000; /* 1.1 hz */
175 dtrace_optval_t dtrace_cleanrate_min
= 20000000; /* 50 hz */
176 dtrace_optval_t dtrace_cleanrate_max
= (uint64_t)60 * NANOSEC
; /* 1/minute */
177 dtrace_optval_t dtrace_aggrate_default
= NANOSEC
; /* 1 hz */
178 dtrace_optval_t dtrace_statusrate_default
= NANOSEC
; /* 1 hz */
179 dtrace_optval_t dtrace_statusrate_max
= (hrtime_t
)10 * NANOSEC
; /* 6/minute */
180 dtrace_optval_t dtrace_switchrate_default
= NANOSEC
; /* 1 hz */
181 dtrace_optval_t dtrace_nspec_default
= 1;
182 dtrace_optval_t dtrace_specsize_default
= 32 * 1024;
183 dtrace_optval_t dtrace_stackframes_default
= 20;
184 dtrace_optval_t dtrace_ustackframes_default
= 20;
185 dtrace_optval_t dtrace_jstackframes_default
= 50;
186 dtrace_optval_t dtrace_jstackstrsize_default
= 512;
187 dtrace_optval_t dtrace_buflimit_default
= 75;
188 dtrace_optval_t dtrace_buflimit_min
= 1;
189 dtrace_optval_t dtrace_buflimit_max
= 99;
190 size_t dtrace_nprobes_default
= 4;
191 int dtrace_msgdsize_max
= 128;
192 hrtime_t dtrace_chill_max
= 500 * (NANOSEC
/ MILLISEC
); /* 500 ms */
193 hrtime_t dtrace_chill_interval
= NANOSEC
; /* 1000 ms */
194 int dtrace_devdepth_max
= 32;
195 int dtrace_err_verbose
;
196 hrtime_t dtrace_deadman_interval
= NANOSEC
;
197 hrtime_t dtrace_deadman_timeout
= (hrtime_t
)10 * NANOSEC
;
198 hrtime_t dtrace_deadman_user
= (hrtime_t
)30 * NANOSEC
;
201 * DTrace External Variables
203 * As dtrace(7D) is a kernel module, any DTrace variables are obviously
204 * available to DTrace consumers via the backtick (`) syntax. One of these,
205 * dtrace_zero, is made deliberately so: it is provided as a source of
206 * well-known, zero-filled memory. While this variable is not documented,
207 * it is used by some translators as an implementation detail.
209 const char dtrace_zero
[256] = { 0 }; /* zero-filled memory */
210 unsigned int dtrace_max_cpus
= 0; /* number of enabled cpus */
212 * DTrace Internal Variables
214 static dev_info_t
*dtrace_devi
; /* device info */
215 static vmem_t
*dtrace_arena
; /* probe ID arena */
216 static dtrace_probe_t
**dtrace_probes
; /* array of all probes */
217 static int dtrace_nprobes
; /* number of probes */
218 static dtrace_provider_t
*dtrace_provider
; /* provider list */
219 static dtrace_meta_t
*dtrace_meta_pid
; /* user-land meta provider */
220 static int dtrace_opens
; /* number of opens */
221 static int dtrace_helpers
; /* number of helpers */
222 static dtrace_hash_t
*dtrace_strings
;
223 static dtrace_hash_t
*dtrace_byprov
; /* probes hashed by provider */
224 static dtrace_hash_t
*dtrace_bymod
; /* probes hashed by module */
225 static dtrace_hash_t
*dtrace_byfunc
; /* probes hashed by function */
226 static dtrace_hash_t
*dtrace_byname
; /* probes hashed by name */
227 static dtrace_toxrange_t
*dtrace_toxrange
; /* toxic range array */
228 static int dtrace_toxranges
; /* number of toxic ranges */
229 static int dtrace_toxranges_max
; /* size of toxic range array */
230 static dtrace_anon_t dtrace_anon
; /* anonymous enabling */
231 static kmem_cache_t
*dtrace_state_cache
; /* cache for dynamic state */
232 static uint64_t dtrace_vtime_references
; /* number of vtimestamp refs */
233 static kthread_t
*dtrace_panicked
; /* panicking thread */
234 static dtrace_ecb_t
*dtrace_ecb_create_cache
; /* cached created ECB */
235 static dtrace_genid_t dtrace_probegen
; /* current probe generation */
236 static dtrace_helpers_t
*dtrace_deferred_pid
; /* deferred helper list */
237 static dtrace_enabling_t
*dtrace_retained
; /* list of retained enablings */
238 static dtrace_genid_t dtrace_retained_gen
; /* current retained enab gen */
239 static dtrace_dynvar_t dtrace_dynhash_sink
; /* end of dynamic hash chains */
241 static int dtrace_dof_mode
; /* See dtrace_impl.h for a description of Darwin's dof modes. */
244 * This does't quite fit as an internal variable, as it must be accessed in
245 * fbt_provide and sdt_provide. Its clearly not a dtrace tunable variable either...
247 int dtrace_kernel_symbol_mode
; /* See dtrace_impl.h for a description of Darwin's kernel symbol modes. */
248 static uint32_t dtrace_wake_clients
;
249 static uint8_t dtrace_kerneluuid
[16]; /* the 128-bit uuid */
252 * To save memory, some common memory allocations are given a
253 * unique zone. For example, dtrace_probe_t is 72 bytes in size,
254 * which means it would fall into the kalloc.128 bucket. With
255 * 20k elements allocated, the space saved is substantial.
258 static ZONE_DECLARE(dtrace_probe_t_zone
, "dtrace.dtrace_probe_t",
259 sizeof(dtrace_probe_t
), ZC_NONE
);
261 static int dtrace_module_unloaded(struct kmod_info
*kmod
);
265 * DTrace is protected by three (relatively coarse-grained) locks:
267 * (1) dtrace_lock is required to manipulate essentially any DTrace state,
268 * including enabling state, probes, ECBs, consumer state, helper state,
269 * etc. Importantly, dtrace_lock is _not_ required when in probe context;
270 * probe context is lock-free -- synchronization is handled via the
271 * dtrace_sync() cross call mechanism.
273 * (2) dtrace_provider_lock is required when manipulating provider state, or
274 * when provider state must be held constant.
276 * (3) dtrace_meta_lock is required when manipulating meta provider state, or
277 * when meta provider state must be held constant.
279 * The lock ordering between these three locks is dtrace_meta_lock before
280 * dtrace_provider_lock before dtrace_lock. (In particular, there are
281 * several places where dtrace_provider_lock is held by the framework as it
282 * calls into the providers -- which then call back into the framework,
283 * grabbing dtrace_lock.)
285 * There are two other locks in the mix: mod_lock and cpu_lock. With respect
286 * to dtrace_provider_lock and dtrace_lock, cpu_lock continues its historical
287 * role as a coarse-grained lock; it is acquired before both of these locks.
288 * With respect to dtrace_meta_lock, its behavior is stranger: cpu_lock must
289 * be acquired _between_ dtrace_meta_lock and any other DTrace locks.
290 * mod_lock is similar with respect to dtrace_provider_lock in that it must be
291 * acquired _between_ dtrace_provider_lock and dtrace_lock.
298 * For porting purposes, all kmutex_t vars have been changed
299 * to lck_mtx_t, which require explicit initialization.
301 * kmutex_t becomes lck_mtx_t
302 * mutex_enter() becomes lck_mtx_lock()
303 * mutex_exit() becomes lck_mtx_unlock()
305 * Lock asserts are changed like this:
307 * ASSERT(MUTEX_HELD(&cpu_lock));
309 * LCK_MTX_ASSERT(&cpu_lock, LCK_MTX_ASSERT_OWNED);
312 static lck_mtx_t dtrace_lock
; /* probe state lock */
313 static lck_mtx_t dtrace_provider_lock
; /* provider state lock */
314 static lck_mtx_t dtrace_meta_lock
; /* meta-provider state lock */
315 static lck_rw_t dtrace_dof_mode_lock
; /* dof mode lock */
318 * DTrace Provider Variables
320 * These are the variables relating to DTrace as a provider (that is, the
321 * provider of the BEGIN, END, and ERROR probes).
323 static dtrace_pattr_t dtrace_provider_attr
= {
324 { DTRACE_STABILITY_STABLE
, DTRACE_STABILITY_STABLE
, DTRACE_CLASS_COMMON
},
325 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_UNKNOWN
},
326 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_UNKNOWN
},
327 { DTRACE_STABILITY_STABLE
, DTRACE_STABILITY_STABLE
, DTRACE_CLASS_COMMON
},
328 { DTRACE_STABILITY_STABLE
, DTRACE_STABILITY_STABLE
, DTRACE_CLASS_COMMON
},
332 dtrace_provide_nullop(void *arg
, const dtrace_probedesc_t
*desc
)
334 #pragma unused(arg, desc)
338 dtrace_provide_module_nullop(void *arg
, struct modctl
*ctl
)
340 #pragma unused(arg, ctl)
344 dtrace_enable_nullop(void *arg
, dtrace_id_t id
, void *parg
)
346 #pragma unused(arg, id, parg)
351 dtrace_disable_nullop(void *arg
, dtrace_id_t id
, void *parg
)
353 #pragma unused(arg, id, parg)
357 dtrace_suspend_nullop(void *arg
, dtrace_id_t id
, void *parg
)
359 #pragma unused(arg, id, parg)
363 dtrace_resume_nullop(void *arg
, dtrace_id_t id
, void *parg
)
365 #pragma unused(arg, id, parg)
369 dtrace_destroy_nullop(void *arg
, dtrace_id_t id
, void *parg
)
371 #pragma unused(arg, id, parg)
375 static dtrace_pops_t dtrace_provider_ops
= {
376 .dtps_provide
= dtrace_provide_nullop
,
377 .dtps_provide_module
= dtrace_provide_module_nullop
,
378 .dtps_enable
= dtrace_enable_nullop
,
379 .dtps_disable
= dtrace_disable_nullop
,
380 .dtps_suspend
= dtrace_suspend_nullop
,
381 .dtps_resume
= dtrace_resume_nullop
,
382 .dtps_getargdesc
= NULL
,
383 .dtps_getargval
= NULL
,
384 .dtps_usermode
= NULL
,
385 .dtps_destroy
= dtrace_destroy_nullop
,
388 static dtrace_id_t dtrace_probeid_begin
; /* special BEGIN probe */
389 static dtrace_id_t dtrace_probeid_end
; /* special END probe */
390 dtrace_id_t dtrace_probeid_error
; /* special ERROR probe */
393 * DTrace Helper Tracing Variables
395 uint32_t dtrace_helptrace_next
= 0;
396 uint32_t dtrace_helptrace_nlocals
;
397 char *dtrace_helptrace_buffer
;
398 size_t dtrace_helptrace_bufsize
= 512 * 1024;
401 int dtrace_helptrace_enabled
= 1;
403 int dtrace_helptrace_enabled
= 0;
406 #if defined (__arm64__)
408 * The ioctl for adding helper DOF is based on the
409 * size of a user_addr_t. We need to recognize both
410 * U32 and U64 as the same action.
412 #define DTRACEHIOC_ADDDOF_U32 _IOW('h', 4, user32_addr_t)
413 #define DTRACEHIOC_ADDDOF_U64 _IOW('h', 4, user64_addr_t)
414 #endif /* __arm64__ */
417 * DTrace Error Hashing
419 * On DEBUG kernels, DTrace will track the errors that has seen in a hash
420 * table. This is very useful for checking coverage of tests that are
421 * expected to induce DIF or DOF processing errors, and may be useful for
422 * debugging problems in the DIF code generator or in DOF generation . The
423 * error hash may be examined with the ::dtrace_errhash MDB dcmd.
426 static dtrace_errhash_t dtrace_errhash
[DTRACE_ERRHASHSZ
];
427 static const char *dtrace_errlast
;
428 static kthread_t
*dtrace_errthread
;
429 static lck_mtx_t dtrace_errlock
;
433 * DTrace Macros and Constants
435 * These are various macros that are useful in various spots in the
436 * implementation, along with a few random constants that have no meaning
437 * outside of the implementation. There is no real structure to this cpp
438 * mishmash -- but is there ever?
441 #define DTRACE_GETSTR(hash, elm) \
442 (hash->dth_getstr(elm, hash->dth_stroffs))
444 #define DTRACE_HASHSTR(hash, elm) \
445 dtrace_hash_str(DTRACE_GETSTR(hash, elm))
447 #define DTRACE_HASHNEXT(hash, elm) \
448 (void**)((uintptr_t)(elm) + (hash)->dth_nextoffs)
450 #define DTRACE_HASHPREV(hash, elm) \
451 (void**)((uintptr_t)(elm) + (hash)->dth_prevoffs)
453 #define DTRACE_HASHEQ(hash, lhs, rhs) \
454 (strcmp(DTRACE_GETSTR(hash, lhs), \
455 DTRACE_GETSTR(hash, rhs)) == 0)
457 #define DTRACE_AGGHASHSIZE_SLEW 17
459 #define DTRACE_V4MAPPED_OFFSET (sizeof (uint32_t) * 3)
462 * The key for a thread-local variable consists of the lower 61 bits of the
463 * current_thread(), plus the 3 bits of the highest active interrupt above LOCK_LEVEL.
464 * We add DIF_VARIABLE_MAX to t_did to assure that the thread key is never
465 * equal to a variable identifier. This is necessary (but not sufficient) to
466 * assure that global associative arrays never collide with thread-local
467 * variables. To guarantee that they cannot collide, we must also define the
468 * order for keying dynamic variables. That order is:
470 * [ key0 ] ... [ keyn ] [ variable-key ] [ tls-key ]
472 * Because the variable-key and the tls-key are in orthogonal spaces, there is
473 * no way for a global variable key signature to match a thread-local key
476 #if defined (__x86_64__)
477 /* FIXME: two function calls!! */
478 #define DTRACE_TLS_THRKEY(where) { \
479 uint_t intr = ml_at_interrupt_context(); /* Note: just one measly bit */ \
480 uint64_t thr = (uintptr_t)current_thread(); \
481 ASSERT(intr < (1 << 3)); \
482 (where) = ((thr + DIF_VARIABLE_MAX) & \
483 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \
485 #elif defined(__arm__)
486 /* FIXME: three function calls!!! */
487 #define DTRACE_TLS_THRKEY(where) { \
488 uint_t intr = ml_at_interrupt_context(); /* Note: just one measly bit */ \
489 uint64_t thr = (uintptr_t)current_thread(); \
490 uint_t pid = (uint_t)dtrace_proc_selfpid(); \
491 ASSERT(intr < (1 << 3)); \
492 (where) = (((thr << 32 | pid) + DIF_VARIABLE_MAX) & \
493 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \
495 #elif defined (__arm64__)
496 /* FIXME: two function calls!! */
497 #define DTRACE_TLS_THRKEY(where) { \
498 uint_t intr = ml_at_interrupt_context(); /* Note: just one measly bit */ \
499 uint64_t thr = (uintptr_t)current_thread(); \
500 ASSERT(intr < (1 << 3)); \
501 (where) = ((thr + DIF_VARIABLE_MAX) & \
502 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \
505 #error Unknown architecture
508 #define DT_BSWAP_8(x) ((x) & 0xff)
509 #define DT_BSWAP_16(x) ((DT_BSWAP_8(x) << 8) | DT_BSWAP_8((x) >> 8))
510 #define DT_BSWAP_32(x) ((DT_BSWAP_16(x) << 16) | DT_BSWAP_16((x) >> 16))
511 #define DT_BSWAP_64(x) ((DT_BSWAP_32(x) << 32) | DT_BSWAP_32((x) >> 32))
513 #define DT_MASK_LO 0x00000000FFFFFFFFULL
515 #define DTRACE_STORE(type, tomax, offset, what) \
516 *((type *)((uintptr_t)(tomax) + (uintptr_t)offset)) = (type)(what);
519 #define DTRACE_ALIGNCHECK(addr, size, flags) \
520 if (addr & (MIN(size,4) - 1)) { \
521 *flags |= CPU_DTRACE_BADALIGN; \
522 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \
526 #define DTRACE_RANGE_REMAIN(remp, addr, baseaddr, basesz) \
528 if ((remp) != NULL) { \
529 *(remp) = (uintptr_t)(baseaddr) + (basesz) - (addr); \
535 * Test whether a range of memory starting at testaddr of size testsz falls
536 * within the range of memory described by addr, sz. We take care to avoid
537 * problems with overflow and underflow of the unsigned quantities, and
538 * disallow all negative sizes. Ranges of size 0 are allowed.
540 #define DTRACE_INRANGE(testaddr, testsz, baseaddr, basesz) \
541 ((testaddr) - (baseaddr) < (basesz) && \
542 (testaddr) + (testsz) - (baseaddr) <= (basesz) && \
543 (testaddr) + (testsz) >= (testaddr))
546 * Test whether alloc_sz bytes will fit in the scratch region. We isolate
547 * alloc_sz on the righthand side of the comparison in order to avoid overflow
548 * or underflow in the comparison with it. This is simpler than the INRANGE
549 * check above, because we know that the dtms_scratch_ptr is valid in the
550 * range. Allocations of size zero are allowed.
552 #define DTRACE_INSCRATCH(mstate, alloc_sz) \
553 ((mstate)->dtms_scratch_base + (mstate)->dtms_scratch_size - \
554 (mstate)->dtms_scratch_ptr >= (alloc_sz))
556 #define RECOVER_LABEL(bits) dtraceLoadRecover##bits:
558 #if defined (__x86_64__) || (defined (__arm__) || defined (__arm64__))
559 #define DTRACE_LOADFUNC(bits) \
561 uint##bits##_t dtrace_load##bits(uintptr_t addr); \
564 dtrace_load##bits(uintptr_t addr) \
566 size_t size = bits / NBBY; \
568 uint##bits##_t rval = 0; \
570 volatile uint16_t *flags = (volatile uint16_t *) \
571 &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; \
573 DTRACE_ALIGNCHECK(addr, size, flags); \
575 for (i = 0; i < dtrace_toxranges; i++) { \
576 if (addr >= dtrace_toxrange[i].dtt_limit) \
579 if (addr + size <= dtrace_toxrange[i].dtt_base) \
583 * This address falls within a toxic region; return 0. \
585 *flags |= CPU_DTRACE_BADADDR; \
586 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \
591 volatile vm_offset_t recover = (vm_offset_t)&&dtraceLoadRecover##bits; \
592 *flags |= CPU_DTRACE_NOFAULT; \
593 recover = dtrace_sign_and_set_thread_recover(current_thread(), recover); \
596 * PR6394061 - avoid device memory that is unpredictably \
597 * mapped and unmapped \
599 if (pmap_valid_page(pmap_find_phys(kernel_pmap, addr))) \
600 rval = *((volatile uint##bits##_t *)addr); \
602 *flags |= CPU_DTRACE_BADADDR; \
603 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \
607 RECOVER_LABEL(bits); \
608 (void)dtrace_set_thread_recover(current_thread(), recover); \
609 *flags &= ~CPU_DTRACE_NOFAULT; \
614 #else /* all other architectures */
615 #error Unknown Architecture
619 #define dtrace_loadptr dtrace_load64
621 #define dtrace_loadptr dtrace_load32
624 #define DTRACE_DYNHASH_FREE 0
625 #define DTRACE_DYNHASH_SINK 1
626 #define DTRACE_DYNHASH_VALID 2
628 #define DTRACE_MATCH_FAIL -1
629 #define DTRACE_MATCH_NEXT 0
630 #define DTRACE_MATCH_DONE 1
631 #define DTRACE_ANCHORED(probe) ((probe)->dtpr_func[0] != '\0')
632 #define DTRACE_STATE_ALIGN 64
634 #define DTRACE_FLAGS2FLT(flags) \
635 (((flags) & CPU_DTRACE_BADADDR) ? DTRACEFLT_BADADDR : \
636 ((flags) & CPU_DTRACE_ILLOP) ? DTRACEFLT_ILLOP : \
637 ((flags) & CPU_DTRACE_DIVZERO) ? DTRACEFLT_DIVZERO : \
638 ((flags) & CPU_DTRACE_KPRIV) ? DTRACEFLT_KPRIV : \
639 ((flags) & CPU_DTRACE_UPRIV) ? DTRACEFLT_UPRIV : \
640 ((flags) & CPU_DTRACE_TUPOFLOW) ? DTRACEFLT_TUPOFLOW : \
641 ((flags) & CPU_DTRACE_BADALIGN) ? DTRACEFLT_BADALIGN : \
642 ((flags) & CPU_DTRACE_NOSCRATCH) ? DTRACEFLT_NOSCRATCH : \
643 ((flags) & CPU_DTRACE_BADSTACK) ? DTRACEFLT_BADSTACK : \
646 #define DTRACEACT_ISSTRING(act) \
647 ((act)->dta_kind == DTRACEACT_DIFEXPR && \
648 (act)->dta_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING)
651 static size_t dtrace_strlen(const char *, size_t);
652 static dtrace_probe_t
*dtrace_probe_lookup_id(dtrace_id_t id
);
653 static void dtrace_enabling_provide(dtrace_provider_t
*);
654 static int dtrace_enabling_match(dtrace_enabling_t
*, int *, dtrace_match_cond_t
*cond
);
655 static void dtrace_enabling_matchall_with_cond(dtrace_match_cond_t
*cond
);
656 static void dtrace_enabling_matchall(void);
657 static dtrace_state_t
*dtrace_anon_grab(void);
658 static uint64_t dtrace_helper(int, dtrace_mstate_t
*,
659 dtrace_state_t
*, uint64_t, uint64_t);
660 static dtrace_helpers_t
*dtrace_helpers_create(proc_t
*);
661 static void dtrace_buffer_drop(dtrace_buffer_t
*);
662 static intptr_t dtrace_buffer_reserve(dtrace_buffer_t
*, size_t, size_t,
663 dtrace_state_t
*, dtrace_mstate_t
*);
664 static int dtrace_state_option(dtrace_state_t
*, dtrace_optid_t
,
666 static int dtrace_ecb_create_enable(dtrace_probe_t
*, void *, void *);
667 static void dtrace_helper_provider_destroy(dtrace_helper_provider_t
*);
668 static int dtrace_canload_remains(uint64_t, size_t, size_t *,
669 dtrace_mstate_t
*, dtrace_vstate_t
*);
670 static int dtrace_canstore_remains(uint64_t, size_t, size_t *,
671 dtrace_mstate_t
*, dtrace_vstate_t
*);
675 * DTrace sysctl handlers
677 * These declarations and functions are used for a deeper DTrace configuration.
678 * Most of them are not per-consumer basis and may impact the other DTrace
679 * consumers. Correctness may not be supported for all the variables, so you
680 * should be careful about what values you are using.
683 SYSCTL_DECL(_kern_dtrace
);
684 SYSCTL_NODE(_kern
, OID_AUTO
, dtrace
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "dtrace");
687 sysctl_dtrace_err_verbose SYSCTL_HANDLER_ARGS
689 #pragma unused(oidp, arg2)
691 int value
= *(int *) arg1
;
693 error
= sysctl_io_number(req
, value
, sizeof(value
), &value
, &changed
);
694 if (error
|| !changed
)
697 if (value
!= 0 && value
!= 1)
700 lck_mtx_lock(&dtrace_lock
);
701 dtrace_err_verbose
= value
;
702 lck_mtx_unlock(&dtrace_lock
);
708 * kern.dtrace.err_verbose
710 * Set DTrace verbosity when an error occured (0 = disabled, 1 = enabld).
711 * Errors are reported when a DIFO or a DOF has been rejected by the kernel.
713 SYSCTL_PROC(_kern_dtrace
, OID_AUTO
, err_verbose
,
714 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
715 &dtrace_err_verbose
, 0,
716 sysctl_dtrace_err_verbose
, "I", "dtrace error verbose");
719 sysctl_dtrace_buffer_memory_maxsize SYSCTL_HANDLER_ARGS
721 #pragma unused(oidp, arg2, req)
723 uint64_t value
= *(uint64_t *) arg1
;
725 error
= sysctl_io_number(req
, value
, sizeof(value
), &value
, &changed
);
726 if (error
|| !changed
)
729 if (value
<= dtrace_buffer_memory_inuse
)
732 lck_mtx_lock(&dtrace_lock
);
733 dtrace_buffer_memory_maxsize
= value
;
734 lck_mtx_unlock(&dtrace_lock
);
740 * kern.dtrace.buffer_memory_maxsize
742 * Set DTrace maximal size in bytes used by all the consumers' state buffers. By default
743 * the limit is PHYS_MEM / 3 for *all* consumers. Attempting to set a null, a negative value
744 * or a value <= to dtrace_buffer_memory_inuse will result in a failure.
746 SYSCTL_PROC(_kern_dtrace
, OID_AUTO
, buffer_memory_maxsize
,
747 CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
748 &dtrace_buffer_memory_maxsize
, 0,
749 sysctl_dtrace_buffer_memory_maxsize
, "Q", "dtrace state buffer memory maxsize");
752 * kern.dtrace.buffer_memory_inuse
754 * Current state buffer memory used, in bytes, by all the DTrace consumers.
755 * This value is read-only.
757 SYSCTL_QUAD(_kern_dtrace
, OID_AUTO
, buffer_memory_inuse
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
758 &dtrace_buffer_memory_inuse
, "dtrace state buffer memory in-use");
761 sysctl_dtrace_difo_maxsize SYSCTL_HANDLER_ARGS
763 #pragma unused(oidp, arg2, req)
765 size_t value
= *(size_t*) arg1
;
767 error
= sysctl_io_number(req
, value
, sizeof(value
), &value
, &changed
);
768 if (error
|| !changed
)
774 lck_mtx_lock(&dtrace_lock
);
775 dtrace_difo_maxsize
= value
;
776 lck_mtx_unlock(&dtrace_lock
);
782 * kern.dtrace.difo_maxsize
784 * Set the DIFO max size in bytes, check the definition of dtrace_difo_maxsize
785 * to get the default value. Attempting to set a null or negative size will
786 * result in a failure.
788 SYSCTL_PROC(_kern_dtrace
, OID_AUTO
, difo_maxsize
,
789 CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
790 &dtrace_difo_maxsize
, 0,
791 sysctl_dtrace_difo_maxsize
, "Q", "dtrace difo maxsize");
794 sysctl_dtrace_dof_maxsize SYSCTL_HANDLER_ARGS
796 #pragma unused(oidp, arg2, req)
798 dtrace_optval_t value
= *(dtrace_optval_t
*) arg1
;
800 error
= sysctl_io_number(req
, value
, sizeof(value
), &value
, &changed
);
801 if (error
|| !changed
)
807 if (value
>= dtrace_copy_maxsize())
810 lck_mtx_lock(&dtrace_lock
);
811 dtrace_dof_maxsize
= value
;
812 lck_mtx_unlock(&dtrace_lock
);
818 * kern.dtrace.dof_maxsize
820 * Set the DOF max size in bytes, check the definition of dtrace_dof_maxsize to
821 * get the default value. Attempting to set a null or negative size will result
824 SYSCTL_PROC(_kern_dtrace
, OID_AUTO
, dof_maxsize
,
825 CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
826 &dtrace_dof_maxsize
, 0,
827 sysctl_dtrace_dof_maxsize
, "Q", "dtrace dof maxsize");
830 sysctl_dtrace_statvar_maxsize SYSCTL_HANDLER_ARGS
832 #pragma unused(oidp, arg2, req)
834 dtrace_optval_t value
= *(dtrace_optval_t
*) arg1
;
836 error
= sysctl_io_number(req
, value
, sizeof(value
), &value
, &changed
);
837 if (error
|| !changed
)
842 if (value
> dtrace_statvar_maxsize_max
)
845 lck_mtx_lock(&dtrace_lock
);
846 dtrace_statvar_maxsize
= value
;
847 lck_mtx_unlock(&dtrace_lock
);
853 * kern.dtrace.global_maxsize
855 * Set the variable max size in bytes, check the definition of
856 * dtrace_statvar_maxsize to get the default value. Attempting to set a null,
857 * too high or negative size will result in a failure.
859 SYSCTL_PROC(_kern_dtrace
, OID_AUTO
, global_maxsize
,
860 CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
861 &dtrace_statvar_maxsize
, 0,
862 sysctl_dtrace_statvar_maxsize
, "Q", "dtrace statvar maxsize");
866 * kern.dtrace.provide_private_probes
868 * Set whether the providers must provide the private probes. This is
869 * kept as compatibility as they are always provided.
871 SYSCTL_INT(_kern_dtrace
, OID_AUTO
, provide_private_probes
,
872 CTLFLAG_RD
| CTLFLAG_LOCKED
,
873 (int *)NULL
, 1, "provider must provide the private probes");
876 * kern.dtrace.dof_mode
878 * Returns the current DOF mode.
879 * This value is read-only.
881 SYSCTL_INT(_kern_dtrace
, OID_AUTO
, dof_mode
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
882 &dtrace_dof_mode
, 0, "dtrace dof mode");
885 * DTrace Probe Context Functions
887 * These functions are called from probe context. Because probe context is
888 * any context in which C may be called, arbitrarily locks may be held,
889 * interrupts may be disabled, we may be in arbitrary dispatched state, etc.
890 * As a result, functions called from probe context may only call other DTrace
891 * support functions -- they may not interact at all with the system at large.
892 * (Note that the ASSERT macro is made probe-context safe by redefining it in
893 * terms of dtrace_assfail(), a probe-context safe function.) If arbitrary
894 * loads are to be performed from probe context, they _must_ be in terms of
895 * the safe dtrace_load*() variants.
897 * Some functions in this block are not actually called from probe context;
898 * for these functions, there will be a comment above the function reading
899 * "Note: not called from probe context."
903 dtrace_assfail(const char *a
, const char *f
, int l
)
905 panic("dtrace: assertion failed: %s, file: %s, line: %d", a
, f
, l
);
908 * We just need something here that even the most clever compiler
909 * cannot optimize away.
911 return (a
[(uintptr_t)f
]);
915 * Atomically increment a specified error counter from probe context.
918 dtrace_error(uint32_t *counter
)
921 * Most counters stored to in probe context are per-CPU counters.
922 * However, there are some error conditions that are sufficiently
923 * arcane that they don't merit per-CPU storage. If these counters
924 * are incremented concurrently on different CPUs, scalability will be
925 * adversely affected -- but we don't expect them to be white-hot in a
926 * correctly constructed enabling...
933 if ((nval
= oval
+ 1) == 0) {
935 * If the counter would wrap, set it to 1 -- assuring
936 * that the counter is never zero when we have seen
937 * errors. (The counter must be 32-bits because we
938 * aren't guaranteed a 64-bit compare&swap operation.)
939 * To save this code both the infamy of being fingered
940 * by a priggish news story and the indignity of being
941 * the target of a neo-puritan witch trial, we're
942 * carefully avoiding any colorful description of the
943 * likelihood of this condition -- but suffice it to
944 * say that it is only slightly more likely than the
945 * overflow of predicate cache IDs, as discussed in
946 * dtrace_predicate_create().
950 } while (dtrace_cas32(counter
, oval
, nval
) != oval
);
954 * Use the DTRACE_LOADFUNC macro to define functions for each of loading a
955 * uint8_t, a uint16_t, a uint32_t and a uint64_t.
963 dtrace_inscratch(uintptr_t dest
, size_t size
, dtrace_mstate_t
*mstate
)
965 if (dest
< mstate
->dtms_scratch_base
)
968 if (dest
+ size
< dest
)
971 if (dest
+ size
> mstate
->dtms_scratch_ptr
)
978 dtrace_canstore_statvar(uint64_t addr
, size_t sz
, size_t *remain
,
979 dtrace_statvar_t
**svars
, int nsvars
)
983 size_t maxglobalsize
, maxlocalsize
;
985 maxglobalsize
= dtrace_statvar_maxsize
+ sizeof (uint64_t);
986 maxlocalsize
= (maxglobalsize
) * NCPU
;
991 for (i
= 0; i
< nsvars
; i
++) {
992 dtrace_statvar_t
*svar
= svars
[i
];
996 if (svar
== NULL
|| (size
= svar
->dtsv_size
) == 0)
999 scope
= svar
->dtsv_var
.dtdv_scope
;
1002 * We verify that our size is valid in the spirit of providing
1003 * defense in depth: we want to prevent attackers from using
1004 * DTrace to escalate an orthogonal kernel heap corruption bug
1005 * into the ability to store to arbitrary locations in memory.
1007 VERIFY((scope
== DIFV_SCOPE_GLOBAL
&& size
<= maxglobalsize
) ||
1008 (scope
== DIFV_SCOPE_LOCAL
&& size
<= maxlocalsize
));
1010 if (DTRACE_INRANGE(addr
, sz
, svar
->dtsv_data
, svar
->dtsv_size
)) {
1011 DTRACE_RANGE_REMAIN(remain
, addr
, svar
->dtsv_data
,
1021 * Check to see if the address is within a memory region to which a store may
1022 * be issued. This includes the DTrace scratch areas, and any DTrace variable
1023 * region. The caller of dtrace_canstore() is responsible for performing any
1024 * alignment checks that are needed before stores are actually executed.
1027 dtrace_canstore(uint64_t addr
, size_t sz
, dtrace_mstate_t
*mstate
,
1028 dtrace_vstate_t
*vstate
)
1030 return (dtrace_canstore_remains(addr
, sz
, NULL
, mstate
, vstate
));
1033 * Implementation of dtrace_canstore which communicates the upper bound of the
1034 * allowed memory region.
1037 dtrace_canstore_remains(uint64_t addr
, size_t sz
, size_t *remain
,
1038 dtrace_mstate_t
*mstate
, dtrace_vstate_t
*vstate
)
1041 * First, check to see if the address is in scratch space...
1043 if (DTRACE_INRANGE(addr
, sz
, mstate
->dtms_scratch_base
,
1044 mstate
->dtms_scratch_size
)) {
1045 DTRACE_RANGE_REMAIN(remain
, addr
, mstate
->dtms_scratch_base
,
1046 mstate
->dtms_scratch_size
);
1050 * Now check to see if it's a dynamic variable. This check will pick
1051 * up both thread-local variables and any global dynamically-allocated
1054 if (DTRACE_INRANGE(addr
, sz
, (uintptr_t)vstate
->dtvs_dynvars
.dtds_base
,
1055 vstate
->dtvs_dynvars
.dtds_size
)) {
1056 dtrace_dstate_t
*dstate
= &vstate
->dtvs_dynvars
;
1057 uintptr_t base
= (uintptr_t)dstate
->dtds_base
+
1058 (dstate
->dtds_hashsize
* sizeof (dtrace_dynhash_t
));
1059 uintptr_t chunkoffs
;
1060 dtrace_dynvar_t
*dvar
;
1063 * Before we assume that we can store here, we need to make
1064 * sure that it isn't in our metadata -- storing to our
1065 * dynamic variable metadata would corrupt our state. For
1066 * the range to not include any dynamic variable metadata,
1069 * (1) Start above the hash table that is at the base of
1070 * the dynamic variable space
1072 * (2) Have a starting chunk offset that is beyond the
1073 * dtrace_dynvar_t that is at the base of every chunk
1075 * (3) Not span a chunk boundary
1077 * (4) Not be in the tuple space of a dynamic variable
1083 chunkoffs
= (addr
- base
) % dstate
->dtds_chunksize
;
1085 if (chunkoffs
< sizeof (dtrace_dynvar_t
))
1088 if (chunkoffs
+ sz
> dstate
->dtds_chunksize
)
1091 dvar
= (dtrace_dynvar_t
*)((uintptr_t)addr
- chunkoffs
);
1093 if (dvar
->dtdv_hashval
== DTRACE_DYNHASH_FREE
)
1096 if (chunkoffs
< sizeof (dtrace_dynvar_t
) +
1097 ((dvar
->dtdv_tuple
.dtt_nkeys
- 1) * sizeof (dtrace_key_t
)))
1104 * Finally, check the static local and global variables. These checks
1105 * take the longest, so we perform them last.
1107 if (dtrace_canstore_statvar(addr
, sz
, remain
,
1108 vstate
->dtvs_locals
, vstate
->dtvs_nlocals
))
1111 if (dtrace_canstore_statvar(addr
, sz
, remain
,
1112 vstate
->dtvs_globals
, vstate
->dtvs_nglobals
))
1120 * Convenience routine to check to see if the address is within a memory
1121 * region in which a load may be issued given the user's privilege level;
1122 * if not, it sets the appropriate error flags and loads 'addr' into the
1123 * illegal value slot.
1125 * DTrace subroutines (DIF_SUBR_*) should use this helper to implement
1126 * appropriate memory access protection.
1129 dtrace_canload(uint64_t addr
, size_t sz
, dtrace_mstate_t
*mstate
,
1130 dtrace_vstate_t
*vstate
)
1132 return (dtrace_canload_remains(addr
, sz
, NULL
, mstate
, vstate
));
1136 * Implementation of dtrace_canload which communicates the upper bound of the
1137 * allowed memory region.
1140 dtrace_canload_remains(uint64_t addr
, size_t sz
, size_t *remain
,
1141 dtrace_mstate_t
*mstate
, dtrace_vstate_t
*vstate
)
1143 volatile uint64_t *illval
= &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
;
1146 * If we hold the privilege to read from kernel memory, then
1147 * everything is readable.
1149 if ((mstate
->dtms_access
& DTRACE_ACCESS_KERNEL
) != 0) {
1150 DTRACE_RANGE_REMAIN(remain
, addr
, addr
, sz
);
1155 * You can obviously read that which you can store.
1157 if (dtrace_canstore_remains(addr
, sz
, remain
, mstate
, vstate
))
1161 * We're allowed to read from our own string table.
1163 if (DTRACE_INRANGE(addr
, sz
, (uintptr_t)mstate
->dtms_difo
->dtdo_strtab
,
1164 mstate
->dtms_difo
->dtdo_strlen
)) {
1165 DTRACE_RANGE_REMAIN(remain
, addr
,
1166 mstate
->dtms_difo
->dtdo_strtab
,
1167 mstate
->dtms_difo
->dtdo_strlen
);
1171 DTRACE_CPUFLAG_SET(CPU_DTRACE_KPRIV
);
1177 * Convenience routine to check to see if a given string is within a memory
1178 * region in which a load may be issued given the user's privilege level;
1179 * this exists so that we don't need to issue unnecessary dtrace_strlen()
1180 * calls in the event that the user has all privileges.
1183 dtrace_strcanload(uint64_t addr
, size_t sz
, size_t *remain
,
1184 dtrace_mstate_t
*mstate
, dtrace_vstate_t
*vstate
)
1189 * If we hold the privilege to read from kernel memory, then
1190 * everything is readable.
1192 if ((mstate
->dtms_access
& DTRACE_ACCESS_KERNEL
) != 0) {
1193 DTRACE_RANGE_REMAIN(remain
, addr
, addr
, sz
);
1198 * Even if the caller is uninterested in querying the remaining valid
1199 * range, it is required to ensure that the access is allowed.
1201 if (remain
== NULL
) {
1204 if (dtrace_canload_remains(addr
, 0, remain
, mstate
, vstate
)) {
1207 * Perform the strlen after determining the length of the
1208 * memory region which is accessible. This prevents timing
1209 * information from being used to find NULs in memory which is
1210 * not accessible to the caller.
1212 strsz
= 1 + dtrace_strlen((char *)(uintptr_t)addr
,
1214 if (strsz
<= *remain
) {
1223 * Convenience routine to check to see if a given variable is within a memory
1224 * region in which a load may be issued given the user's privilege level.
1227 dtrace_vcanload(void *src
, dtrace_diftype_t
*type
, size_t *remain
,
1228 dtrace_mstate_t
*mstate
, dtrace_vstate_t
*vstate
)
1231 ASSERT(type
->dtdt_flags
& DIF_TF_BYREF
);
1234 * Calculate the max size before performing any checks since even
1235 * DTRACE_ACCESS_KERNEL-credentialed callers expect that this function
1236 * return the max length via 'remain'.
1238 if (type
->dtdt_kind
== DIF_TYPE_STRING
) {
1239 dtrace_state_t
*state
= vstate
->dtvs_state
;
1241 if (state
!= NULL
) {
1242 sz
= state
->dts_options
[DTRACEOPT_STRSIZE
];
1245 * In helper context, we have a NULL state; fall back
1246 * to using the system-wide default for the string size
1249 sz
= dtrace_strsize_default
;
1252 sz
= type
->dtdt_size
;
1256 * If we hold the privilege to read from kernel memory, then
1257 * everything is readable.
1259 if ((mstate
->dtms_access
& DTRACE_ACCESS_KERNEL
) != 0) {
1260 DTRACE_RANGE_REMAIN(remain
, (uintptr_t)src
, src
, sz
);
1264 if (type
->dtdt_kind
== DIF_TYPE_STRING
) {
1265 return (dtrace_strcanload((uintptr_t)src
, sz
, remain
, mstate
,
1268 return (dtrace_canload_remains((uintptr_t)src
, sz
, remain
, mstate
,
1272 #define isdigit(ch) ((ch) >= '0' && (ch) <= '9')
1273 #define islower(ch) ((ch) >= 'a' && (ch) <= 'z')
1274 #define isspace(ch) (((ch) == ' ') || ((ch) == '\r') || ((ch) == '\n') || \
1275 ((ch) == '\t') || ((ch) == '\f'))
1276 #define isxdigit(ch) (isdigit(ch) || ((ch) >= 'a' && (ch) <= 'f') || \
1277 ((ch) >= 'A' && (ch) <= 'F'))
1278 #define lisalnum(x) \
1279 (isdigit(x) || ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z'))
1282 (isdigit(x) ? (x) - '0' : islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
1285 * Convert a string to a signed integer using safe loads.
1288 dtrace_strtoll(char *input
, int base
, size_t limit
)
1290 uintptr_t pos
= (uintptr_t)input
;
1293 boolean_t neg
= B_FALSE
;
1295 uintptr_t end
= pos
+ limit
;
1298 * Consume any whitespace preceding digits.
1300 while ((c
= dtrace_load8(pos
)) == ' ' || c
== '\t')
1304 * Handle an explicit sign if one is present.
1306 if (c
== '-' || c
== '+') {
1309 c
= dtrace_load8(++pos
);
1313 * Check for an explicit hexadecimal prefix ("0x" or "0X") and skip it
1316 if (base
== 16 && c
== '0' && ((cc
= dtrace_load8(pos
+ 1)) == 'x' ||
1317 cc
== 'X') && isxdigit(ccc
= dtrace_load8(pos
+ 2))) {
1323 * Read in contiguous digits until the first non-digit character.
1325 for (; pos
< end
&& c
!= '\0' && lisalnum(c
) && (x
= DIGIT(c
)) < base
;
1326 c
= dtrace_load8(++pos
))
1327 val
= val
* base
+ x
;
1329 return (neg
? -val
: val
);
1334 * Compare two strings using safe loads.
1337 dtrace_strncmp(const char *s1
, const char *s2
, size_t limit
)
1340 volatile uint16_t *flags
;
1342 if (s1
== s2
|| limit
== 0)
1345 flags
= (volatile uint16_t *)&cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
1351 c1
= dtrace_load8((uintptr_t)s1
++);
1357 c2
= dtrace_load8((uintptr_t)s2
++);
1362 } while (--limit
&& c1
!= '\0' && !(*flags
& CPU_DTRACE_FAULT
));
1368 * Compute strlen(s) for a string using safe memory accesses. The additional
1369 * len parameter is used to specify a maximum length to ensure completion.
1372 dtrace_strlen(const char *s
, size_t lim
)
1376 for (len
= 0; len
!= lim
; len
++) {
1377 if (dtrace_load8((uintptr_t)s
++) == '\0')
1385 * Check if an address falls within a toxic region.
1388 dtrace_istoxic(uintptr_t kaddr
, size_t size
)
1390 uintptr_t taddr
, tsize
;
1393 for (i
= 0; i
< dtrace_toxranges
; i
++) {
1394 taddr
= dtrace_toxrange
[i
].dtt_base
;
1395 tsize
= dtrace_toxrange
[i
].dtt_limit
- taddr
;
1397 if (kaddr
- taddr
< tsize
) {
1398 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1399 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= kaddr
;
1403 if (taddr
- kaddr
< size
) {
1404 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
1405 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= taddr
;
1414 * Copy src to dst using safe memory accesses. The src is assumed to be unsafe
1415 * memory specified by the DIF program. The dst is assumed to be safe memory
1416 * that we can store to directly because it is managed by DTrace. As with
1417 * standard bcopy, overlapping copies are handled properly.
1420 dtrace_bcopy(const void *src
, void *dst
, size_t len
)
1424 const uint8_t *s2
= src
;
1428 *s1
++ = dtrace_load8((uintptr_t)s2
++);
1429 } while (--len
!= 0);
1435 *--s1
= dtrace_load8((uintptr_t)--s2
);
1436 } while (--len
!= 0);
1442 * Copy src to dst using safe memory accesses, up to either the specified
1443 * length, or the point that a nul byte is encountered. The src is assumed to
1444 * be unsafe memory specified by the DIF program. The dst is assumed to be
1445 * safe memory that we can store to directly because it is managed by DTrace.
1446 * Unlike dtrace_bcopy(), overlapping regions are not handled.
1449 dtrace_strcpy(const void *src
, void *dst
, size_t len
)
1452 uint8_t *s1
= dst
, c
;
1453 const uint8_t *s2
= src
;
1456 *s1
++ = c
= dtrace_load8((uintptr_t)s2
++);
1457 } while (--len
!= 0 && c
!= '\0');
1462 * Copy src to dst, deriving the size and type from the specified (BYREF)
1463 * variable type. The src is assumed to be unsafe memory specified by the DIF
1464 * program. The dst is assumed to be DTrace variable memory that is of the
1465 * specified type; we assume that we can store to directly.
1468 dtrace_vcopy(void *src
, void *dst
, dtrace_diftype_t
*type
, size_t limit
)
1470 ASSERT(type
->dtdt_flags
& DIF_TF_BYREF
);
1472 if (type
->dtdt_kind
== DIF_TYPE_STRING
) {
1473 dtrace_strcpy(src
, dst
, MIN(type
->dtdt_size
, limit
));
1475 dtrace_bcopy(src
, dst
, MIN(type
->dtdt_size
, limit
));
1480 * Compare s1 to s2 using safe memory accesses. The s1 data is assumed to be
1481 * unsafe memory specified by the DIF program. The s2 data is assumed to be
1482 * safe memory that we can access directly because it is managed by DTrace.
1485 dtrace_bcmp(const void *s1
, const void *s2
, size_t len
)
1487 volatile uint16_t *flags
;
1489 flags
= (volatile uint16_t *)&cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
1494 if (s1
== NULL
|| s2
== NULL
)
1497 if (s1
!= s2
&& len
!= 0) {
1498 const uint8_t *ps1
= s1
;
1499 const uint8_t *ps2
= s2
;
1502 if (dtrace_load8((uintptr_t)ps1
++) != *ps2
++)
1504 } while (--len
!= 0 && !(*flags
& CPU_DTRACE_FAULT
));
1510 * Zero the specified region using a simple byte-by-byte loop. Note that this
1511 * is for safe DTrace-managed memory only.
1514 dtrace_bzero(void *dst
, size_t len
)
1518 for (cp
= dst
; len
!= 0; len
--)
1523 dtrace_add_128(uint64_t *addend1
, uint64_t *addend2
, uint64_t *sum
)
1527 result
[0] = addend1
[0] + addend2
[0];
1528 result
[1] = addend1
[1] + addend2
[1] +
1529 (result
[0] < addend1
[0] || result
[0] < addend2
[0] ? 1 : 0);
1536 * Shift the 128-bit value in a by b. If b is positive, shift left.
1537 * If b is negative, shift right.
1540 dtrace_shift_128(uint64_t *a
, int b
)
1550 a
[0] = a
[1] >> (b
- 64);
1554 mask
= 1LL << (64 - b
);
1556 a
[0] |= ((a
[1] & mask
) << (64 - b
));
1561 a
[1] = a
[0] << (b
- 64);
1565 mask
= a
[0] >> (64 - b
);
1573 * The basic idea is to break the 2 64-bit values into 4 32-bit values,
1574 * use native multiplication on those, and then re-combine into the
1575 * resulting 128-bit value.
1577 * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) =
1584 dtrace_multiply_128(uint64_t factor1
, uint64_t factor2
, uint64_t *product
)
1586 uint64_t hi1
, hi2
, lo1
, lo2
;
1589 hi1
= factor1
>> 32;
1590 hi2
= factor2
>> 32;
1592 lo1
= factor1
& DT_MASK_LO
;
1593 lo2
= factor2
& DT_MASK_LO
;
1595 product
[0] = lo1
* lo2
;
1596 product
[1] = hi1
* hi2
;
1600 dtrace_shift_128(tmp
, 32);
1601 dtrace_add_128(product
, tmp
, product
);
1605 dtrace_shift_128(tmp
, 32);
1606 dtrace_add_128(product
, tmp
, product
);
1610 * This privilege check should be used by actions and subroutines to
1611 * verify that the user credentials of the process that enabled the
1612 * invoking ECB match the target credentials
1615 dtrace_priv_proc_common_user(dtrace_state_t
*state
)
1617 cred_t
*cr
, *s_cr
= state
->dts_cred
.dcr_cred
;
1620 * We should always have a non-NULL state cred here, since if cred
1621 * is null (anonymous tracing), we fast-path bypass this routine.
1623 ASSERT(s_cr
!= NULL
);
1625 if ((cr
= dtrace_CRED()) != NULL
&&
1626 posix_cred_get(s_cr
)->cr_uid
== posix_cred_get(cr
)->cr_uid
&&
1627 posix_cred_get(s_cr
)->cr_uid
== posix_cred_get(cr
)->cr_ruid
&&
1628 posix_cred_get(s_cr
)->cr_uid
== posix_cred_get(cr
)->cr_suid
&&
1629 posix_cred_get(s_cr
)->cr_gid
== posix_cred_get(cr
)->cr_gid
&&
1630 posix_cred_get(s_cr
)->cr_gid
== posix_cred_get(cr
)->cr_rgid
&&
1631 posix_cred_get(s_cr
)->cr_gid
== posix_cred_get(cr
)->cr_sgid
)
1638 * This privilege check should be used by actions and subroutines to
1639 * verify that the zone of the process that enabled the invoking ECB
1640 * matches the target credentials
1643 dtrace_priv_proc_common_zone(dtrace_state_t
*state
)
1645 cred_t
*cr
, *s_cr
= state
->dts_cred
.dcr_cred
;
1646 #pragma unused(cr, s_cr, state) /* __APPLE__ */
1649 * We should always have a non-NULL state cred here, since if cred
1650 * is null (anonymous tracing), we fast-path bypass this routine.
1652 ASSERT(s_cr
!= NULL
);
1654 return 1; /* APPLE NOTE: Darwin doesn't do zones. */
1658 * This privilege check should be used by actions and subroutines to
1659 * verify that the process has not setuid or changed credentials.
1662 dtrace_priv_proc_common_nocd(void)
1664 return 1; /* Darwin omits "No Core Dump" flag. */
1668 dtrace_priv_proc_destructive(dtrace_state_t
*state
)
1670 int action
= state
->dts_cred
.dcr_action
;
1672 if (ISSET(current_proc()->p_lflag
, P_LNOATTACH
))
1675 if (dtrace_is_restricted() && !dtrace_can_attach_to_proc(current_proc()))
1678 if (((action
& DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE
) == 0) &&
1679 dtrace_priv_proc_common_zone(state
) == 0)
1682 if (((action
& DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER
) == 0) &&
1683 dtrace_priv_proc_common_user(state
) == 0)
1686 if (((action
& DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG
) == 0) &&
1687 dtrace_priv_proc_common_nocd() == 0)
1693 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
|= CPU_DTRACE_UPRIV
;
1699 dtrace_priv_proc_control(dtrace_state_t
*state
)
1701 if (ISSET(current_proc()->p_lflag
, P_LNOATTACH
))
1704 if (dtrace_is_restricted() && !dtrace_can_attach_to_proc(current_proc()))
1707 if (state
->dts_cred
.dcr_action
& DTRACE_CRA_PROC_CONTROL
)
1710 if (dtrace_priv_proc_common_zone(state
) &&
1711 dtrace_priv_proc_common_user(state
) &&
1712 dtrace_priv_proc_common_nocd())
1716 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
|= CPU_DTRACE_UPRIV
;
1722 dtrace_priv_proc(dtrace_state_t
*state
)
1724 if (ISSET(current_proc()->p_lflag
, P_LNOATTACH
))
1727 if (dtrace_is_restricted() && !dtrace_are_restrictions_relaxed() && !dtrace_can_attach_to_proc(current_proc()))
1730 if (state
->dts_cred
.dcr_action
& DTRACE_CRA_PROC
)
1734 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
|= CPU_DTRACE_UPRIV
;
1740 * The P_LNOATTACH check is an Apple specific check.
1741 * We need a version of dtrace_priv_proc() that omits
1742 * that check for PID and EXECNAME accesses
1745 dtrace_priv_proc_relaxed(dtrace_state_t
*state
)
1748 if (state
->dts_cred
.dcr_action
& DTRACE_CRA_PROC
)
1751 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
|= CPU_DTRACE_UPRIV
;
1757 dtrace_priv_kernel(dtrace_state_t
*state
)
1759 if (dtrace_is_restricted() && !dtrace_are_restrictions_relaxed())
1762 if (state
->dts_cred
.dcr_action
& DTRACE_CRA_KERNEL
)
1766 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
|= CPU_DTRACE_KPRIV
;
1772 dtrace_priv_kernel_destructive(dtrace_state_t
*state
)
1774 if (dtrace_is_restricted())
1777 if (state
->dts_cred
.dcr_action
& DTRACE_CRA_KERNEL_DESTRUCTIVE
)
1781 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
|= CPU_DTRACE_KPRIV
;
1787 * Note: not called from probe context. This function is called
1788 * asynchronously (and at a regular interval) from outside of probe context to
1789 * clean the dirty dynamic variable lists on all CPUs. Dynamic variable
1790 * cleaning is explained in detail in <sys/dtrace_impl.h>.
1793 dtrace_dynvar_clean(dtrace_dstate_t
*dstate
)
1795 dtrace_dynvar_t
*dirty
;
1796 dtrace_dstate_percpu_t
*dcpu
;
1799 for (i
= 0; i
< (int)NCPU
; i
++) {
1800 dcpu
= &dstate
->dtds_percpu
[i
];
1802 ASSERT(dcpu
->dtdsc_rinsing
== NULL
);
1805 * If the dirty list is NULL, there is no dirty work to do.
1807 if (dcpu
->dtdsc_dirty
== NULL
)
1811 * If the clean list is non-NULL, then we're not going to do
1812 * any work for this CPU -- it means that there has not been
1813 * a dtrace_dynvar() allocation on this CPU (or from this CPU)
1814 * since the last time we cleaned house.
1816 if (dcpu
->dtdsc_clean
!= NULL
)
1822 * Atomically move the dirty list aside.
1825 dirty
= dcpu
->dtdsc_dirty
;
1828 * Before we zap the dirty list, set the rinsing list.
1829 * (This allows for a potential assertion in
1830 * dtrace_dynvar(): if a free dynamic variable appears
1831 * on a hash chain, either the dirty list or the
1832 * rinsing list for some CPU must be non-NULL.)
1834 dcpu
->dtdsc_rinsing
= dirty
;
1835 dtrace_membar_producer();
1836 } while (dtrace_casptr(&dcpu
->dtdsc_dirty
,
1837 dirty
, NULL
) != dirty
);
1842 * We have no work to do; we can simply return.
1849 for (i
= 0; i
< (int)NCPU
; i
++) {
1850 dcpu
= &dstate
->dtds_percpu
[i
];
1852 if (dcpu
->dtdsc_rinsing
== NULL
)
1856 * We are now guaranteed that no hash chain contains a pointer
1857 * into this dirty list; we can make it clean.
1859 ASSERT(dcpu
->dtdsc_clean
== NULL
);
1860 dcpu
->dtdsc_clean
= dcpu
->dtdsc_rinsing
;
1861 dcpu
->dtdsc_rinsing
= NULL
;
1865 * Before we actually set the state to be DTRACE_DSTATE_CLEAN, make
1866 * sure that all CPUs have seen all of the dtdsc_clean pointers.
1867 * This prevents a race whereby a CPU incorrectly decides that
1868 * the state should be something other than DTRACE_DSTATE_CLEAN
1869 * after dtrace_dynvar_clean() has completed.
1873 dstate
->dtds_state
= DTRACE_DSTATE_CLEAN
;
1877 * Depending on the value of the op parameter, this function looks-up,
1878 * allocates or deallocates an arbitrarily-keyed dynamic variable. If an
1879 * allocation is requested, this function will return a pointer to a
1880 * dtrace_dynvar_t corresponding to the allocated variable -- or NULL if no
1881 * variable can be allocated. If NULL is returned, the appropriate counter
1882 * will be incremented.
1884 static dtrace_dynvar_t
*
1885 dtrace_dynvar(dtrace_dstate_t
*dstate
, uint_t nkeys
,
1886 dtrace_key_t
*key
, size_t dsize
, dtrace_dynvar_op_t op
,
1887 dtrace_mstate_t
*mstate
, dtrace_vstate_t
*vstate
)
1889 uint64_t hashval
= DTRACE_DYNHASH_VALID
;
1890 dtrace_dynhash_t
*hash
= dstate
->dtds_hash
;
1891 dtrace_dynvar_t
*free
, *new_free
, *next
, *dvar
, *start
, *prev
= NULL
;
1892 processorid_t me
= CPU
->cpu_id
, cpu
= me
;
1893 dtrace_dstate_percpu_t
*dcpu
= &dstate
->dtds_percpu
[me
];
1894 size_t bucket
, ksize
;
1895 size_t chunksize
= dstate
->dtds_chunksize
;
1896 uintptr_t kdata
, lock
, nstate
;
1902 * Hash the key. As with aggregations, we use Jenkins' "One-at-a-time"
1903 * algorithm. For the by-value portions, we perform the algorithm in
1904 * 16-bit chunks (as opposed to 8-bit chunks). This speeds things up a
1905 * bit, and seems to have only a minute effect on distribution. For
1906 * the by-reference data, we perform "One-at-a-time" iterating (safely)
1907 * over each referenced byte. It's painful to do this, but it's much
1908 * better than pathological hash distribution. The efficacy of the
1909 * hashing algorithm (and a comparison with other algorithms) may be
1910 * found by running the ::dtrace_dynstat MDB dcmd.
1912 for (i
= 0; i
< nkeys
; i
++) {
1913 if (key
[i
].dttk_size
== 0) {
1914 uint64_t val
= key
[i
].dttk_value
;
1916 hashval
+= (val
>> 48) & 0xffff;
1917 hashval
+= (hashval
<< 10);
1918 hashval
^= (hashval
>> 6);
1920 hashval
+= (val
>> 32) & 0xffff;
1921 hashval
+= (hashval
<< 10);
1922 hashval
^= (hashval
>> 6);
1924 hashval
+= (val
>> 16) & 0xffff;
1925 hashval
+= (hashval
<< 10);
1926 hashval
^= (hashval
>> 6);
1928 hashval
+= val
& 0xffff;
1929 hashval
+= (hashval
<< 10);
1930 hashval
^= (hashval
>> 6);
1933 * This is incredibly painful, but it beats the hell
1934 * out of the alternative.
1936 uint64_t j
, size
= key
[i
].dttk_size
;
1937 uintptr_t base
= (uintptr_t)key
[i
].dttk_value
;
1939 if (!dtrace_canload(base
, size
, mstate
, vstate
))
1942 for (j
= 0; j
< size
; j
++) {
1943 hashval
+= dtrace_load8(base
+ j
);
1944 hashval
+= (hashval
<< 10);
1945 hashval
^= (hashval
>> 6);
1950 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT
))
1953 hashval
+= (hashval
<< 3);
1954 hashval
^= (hashval
>> 11);
1955 hashval
+= (hashval
<< 15);
1958 * There is a remote chance (ideally, 1 in 2^31) that our hashval
1959 * comes out to be one of our two sentinel hash values. If this
1960 * actually happens, we set the hashval to be a value known to be a
1961 * non-sentinel value.
1963 if (hashval
== DTRACE_DYNHASH_FREE
|| hashval
== DTRACE_DYNHASH_SINK
)
1964 hashval
= DTRACE_DYNHASH_VALID
;
1967 * Yes, it's painful to do a divide here. If the cycle count becomes
1968 * important here, tricks can be pulled to reduce it. (However, it's
1969 * critical that hash collisions be kept to an absolute minimum;
1970 * they're much more painful than a divide.) It's better to have a
1971 * solution that generates few collisions and still keeps things
1972 * relatively simple.
1974 bucket
= hashval
% dstate
->dtds_hashsize
;
1976 if (op
== DTRACE_DYNVAR_DEALLOC
) {
1977 volatile uintptr_t *lockp
= &hash
[bucket
].dtdh_lock
;
1980 while ((lock
= *lockp
) & 1)
1983 if (dtrace_casptr((void *)(uintptr_t)lockp
,
1984 (void *)lock
, (void *)(lock
+ 1)) == (void *)lock
)
1988 dtrace_membar_producer();
1993 lock
= hash
[bucket
].dtdh_lock
;
1995 dtrace_membar_consumer();
1997 start
= hash
[bucket
].dtdh_chain
;
1998 ASSERT(start
!= NULL
&& (start
->dtdv_hashval
== DTRACE_DYNHASH_SINK
||
1999 start
->dtdv_hashval
!= DTRACE_DYNHASH_FREE
||
2000 op
!= DTRACE_DYNVAR_DEALLOC
));
2002 for (dvar
= start
; dvar
!= NULL
; dvar
= dvar
->dtdv_next
) {
2003 dtrace_tuple_t
*dtuple
= &dvar
->dtdv_tuple
;
2004 dtrace_key_t
*dkey
= &dtuple
->dtt_key
[0];
2006 if (dvar
->dtdv_hashval
!= hashval
) {
2007 if (dvar
->dtdv_hashval
== DTRACE_DYNHASH_SINK
) {
2009 * We've reached the sink, and therefore the
2010 * end of the hash chain; we can kick out of
2011 * the loop knowing that we have seen a valid
2012 * snapshot of state.
2014 ASSERT(dvar
->dtdv_next
== NULL
);
2015 ASSERT(dvar
== &dtrace_dynhash_sink
);
2019 if (dvar
->dtdv_hashval
== DTRACE_DYNHASH_FREE
) {
2021 * We've gone off the rails: somewhere along
2022 * the line, one of the members of this hash
2023 * chain was deleted. Note that we could also
2024 * detect this by simply letting this loop run
2025 * to completion, as we would eventually hit
2026 * the end of the dirty list. However, we
2027 * want to avoid running the length of the
2028 * dirty list unnecessarily (it might be quite
2029 * long), so we catch this as early as
2030 * possible by detecting the hash marker. In
2031 * this case, we simply set dvar to NULL and
2032 * break; the conditional after the loop will
2033 * send us back to top.
2042 if (dtuple
->dtt_nkeys
!= nkeys
)
2045 for (i
= 0; i
< nkeys
; i
++, dkey
++) {
2046 if (dkey
->dttk_size
!= key
[i
].dttk_size
)
2047 goto next
; /* size or type mismatch */
2049 if (dkey
->dttk_size
!= 0) {
2051 (void *)(uintptr_t)key
[i
].dttk_value
,
2052 (void *)(uintptr_t)dkey
->dttk_value
,
2056 if (dkey
->dttk_value
!= key
[i
].dttk_value
)
2061 if (op
!= DTRACE_DYNVAR_DEALLOC
)
2064 ASSERT(dvar
->dtdv_next
== NULL
||
2065 dvar
->dtdv_next
->dtdv_hashval
!= DTRACE_DYNHASH_FREE
);
2068 ASSERT(hash
[bucket
].dtdh_chain
!= dvar
);
2069 ASSERT(start
!= dvar
);
2070 ASSERT(prev
->dtdv_next
== dvar
);
2071 prev
->dtdv_next
= dvar
->dtdv_next
;
2073 if (dtrace_casptr(&hash
[bucket
].dtdh_chain
,
2074 start
, dvar
->dtdv_next
) != start
) {
2076 * We have failed to atomically swing the
2077 * hash table head pointer, presumably because
2078 * of a conflicting allocation on another CPU.
2079 * We need to reread the hash chain and try
2086 dtrace_membar_producer();
2089 * Now set the hash value to indicate that it's free.
2091 ASSERT(hash
[bucket
].dtdh_chain
!= dvar
);
2092 dvar
->dtdv_hashval
= DTRACE_DYNHASH_FREE
;
2094 dtrace_membar_producer();
2097 * Set the next pointer to point at the dirty list, and
2098 * atomically swing the dirty pointer to the newly freed dvar.
2101 next
= dcpu
->dtdsc_dirty
;
2102 dvar
->dtdv_next
= next
;
2103 } while (dtrace_casptr(&dcpu
->dtdsc_dirty
, next
, dvar
) != next
);
2106 * Finally, unlock this hash bucket.
2108 ASSERT(hash
[bucket
].dtdh_lock
== lock
);
2110 hash
[bucket
].dtdh_lock
++;
2120 * If dvar is NULL, it is because we went off the rails:
2121 * one of the elements that we traversed in the hash chain
2122 * was deleted while we were traversing it. In this case,
2123 * we assert that we aren't doing a dealloc (deallocs lock
2124 * the hash bucket to prevent themselves from racing with
2125 * one another), and retry the hash chain traversal.
2127 ASSERT(op
!= DTRACE_DYNVAR_DEALLOC
);
2131 if (op
!= DTRACE_DYNVAR_ALLOC
) {
2133 * If we are not to allocate a new variable, we want to
2134 * return NULL now. Before we return, check that the value
2135 * of the lock word hasn't changed. If it has, we may have
2136 * seen an inconsistent snapshot.
2138 if (op
== DTRACE_DYNVAR_NOALLOC
) {
2139 if (hash
[bucket
].dtdh_lock
!= lock
)
2142 ASSERT(op
== DTRACE_DYNVAR_DEALLOC
);
2143 ASSERT(hash
[bucket
].dtdh_lock
== lock
);
2145 hash
[bucket
].dtdh_lock
++;
2152 * We need to allocate a new dynamic variable. The size we need is the
2153 * size of dtrace_dynvar plus the size of nkeys dtrace_key_t's plus the
2154 * size of any auxiliary key data (rounded up to 8-byte alignment) plus
2155 * the size of any referred-to data (dsize). We then round the final
2156 * size up to the chunksize for allocation.
2158 for (ksize
= 0, i
= 0; i
< nkeys
; i
++)
2159 ksize
+= P2ROUNDUP(key
[i
].dttk_size
, sizeof (uint64_t));
2162 * This should be pretty much impossible, but could happen if, say,
2163 * strange DIF specified the tuple. Ideally, this should be an
2164 * assertion and not an error condition -- but that requires that the
2165 * chunksize calculation in dtrace_difo_chunksize() be absolutely
2166 * bullet-proof. (That is, it must not be able to be fooled by
2167 * malicious DIF.) Given the lack of backwards branches in DIF,
2168 * solving this would presumably not amount to solving the Halting
2169 * Problem -- but it still seems awfully hard.
2171 if (sizeof (dtrace_dynvar_t
) + sizeof (dtrace_key_t
) * (nkeys
- 1) +
2172 ksize
+ dsize
> chunksize
) {
2173 dcpu
->dtdsc_drops
++;
2177 nstate
= DTRACE_DSTATE_EMPTY
;
2181 free
= dcpu
->dtdsc_free
;
2184 dtrace_dynvar_t
*clean
= dcpu
->dtdsc_clean
;
2187 if (clean
== NULL
) {
2189 * We're out of dynamic variable space on
2190 * this CPU. Unless we have tried all CPUs,
2191 * we'll try to allocate from a different
2194 switch (dstate
->dtds_state
) {
2195 case DTRACE_DSTATE_CLEAN
: {
2196 void *sp
= &dstate
->dtds_state
;
2198 if (++cpu
>= (int)NCPU
)
2201 if (dcpu
->dtdsc_dirty
!= NULL
&&
2202 nstate
== DTRACE_DSTATE_EMPTY
)
2203 nstate
= DTRACE_DSTATE_DIRTY
;
2205 if (dcpu
->dtdsc_rinsing
!= NULL
)
2206 nstate
= DTRACE_DSTATE_RINSING
;
2208 dcpu
= &dstate
->dtds_percpu
[cpu
];
2213 (void) dtrace_cas32(sp
,
2214 DTRACE_DSTATE_CLEAN
, nstate
);
2217 * To increment the correct bean
2218 * counter, take another lap.
2223 case DTRACE_DSTATE_DIRTY
:
2224 dcpu
->dtdsc_dirty_drops
++;
2227 case DTRACE_DSTATE_RINSING
:
2228 dcpu
->dtdsc_rinsing_drops
++;
2231 case DTRACE_DSTATE_EMPTY
:
2232 dcpu
->dtdsc_drops
++;
2236 DTRACE_CPUFLAG_SET(CPU_DTRACE_DROP
);
2241 * The clean list appears to be non-empty. We want to
2242 * move the clean list to the free list; we start by
2243 * moving the clean pointer aside.
2245 if (dtrace_casptr(&dcpu
->dtdsc_clean
,
2246 clean
, NULL
) != clean
) {
2248 * We are in one of two situations:
2250 * (a) The clean list was switched to the
2251 * free list by another CPU.
2253 * (b) The clean list was added to by the
2256 * In either of these situations, we can
2257 * just reattempt the free list allocation.
2262 ASSERT(clean
->dtdv_hashval
== DTRACE_DYNHASH_FREE
);
2265 * Now we'll move the clean list to the free list.
2266 * It's impossible for this to fail: the only way
2267 * the free list can be updated is through this
2268 * code path, and only one CPU can own the clean list.
2269 * Thus, it would only be possible for this to fail if
2270 * this code were racing with dtrace_dynvar_clean().
2271 * (That is, if dtrace_dynvar_clean() updated the clean
2272 * list, and we ended up racing to update the free
2273 * list.) This race is prevented by the dtrace_sync()
2274 * in dtrace_dynvar_clean() -- which flushes the
2275 * owners of the clean lists out before resetting
2278 rval
= dtrace_casptr(&dcpu
->dtdsc_free
, NULL
, clean
);
2279 ASSERT(rval
== NULL
);
2284 new_free
= dvar
->dtdv_next
;
2285 } while (dtrace_casptr(&dcpu
->dtdsc_free
, free
, new_free
) != free
);
2288 * We have now allocated a new chunk. We copy the tuple keys into the
2289 * tuple array and copy any referenced key data into the data space
2290 * following the tuple array. As we do this, we relocate dttk_value
2291 * in the final tuple to point to the key data address in the chunk.
2293 kdata
= (uintptr_t)&dvar
->dtdv_tuple
.dtt_key
[nkeys
];
2294 dvar
->dtdv_data
= (void *)(kdata
+ ksize
);
2295 dvar
->dtdv_tuple
.dtt_nkeys
= nkeys
;
2297 for (i
= 0; i
< nkeys
; i
++) {
2298 dtrace_key_t
*dkey
= &dvar
->dtdv_tuple
.dtt_key
[i
];
2299 size_t kesize
= key
[i
].dttk_size
;
2303 (const void *)(uintptr_t)key
[i
].dttk_value
,
2304 (void *)kdata
, kesize
);
2305 dkey
->dttk_value
= kdata
;
2306 kdata
+= P2ROUNDUP(kesize
, sizeof (uint64_t));
2308 dkey
->dttk_value
= key
[i
].dttk_value
;
2311 dkey
->dttk_size
= kesize
;
2314 ASSERT(dvar
->dtdv_hashval
== DTRACE_DYNHASH_FREE
);
2315 dvar
->dtdv_hashval
= hashval
;
2316 dvar
->dtdv_next
= start
;
2318 if (dtrace_casptr(&hash
[bucket
].dtdh_chain
, start
, dvar
) == start
)
2322 * The cas has failed. Either another CPU is adding an element to
2323 * this hash chain, or another CPU is deleting an element from this
2324 * hash chain. The simplest way to deal with both of these cases
2325 * (though not necessarily the most efficient) is to free our
2326 * allocated block and tail-call ourselves. Note that the free is
2327 * to the dirty list and _not_ to the free list. This is to prevent
2328 * races with allocators, above.
2330 dvar
->dtdv_hashval
= DTRACE_DYNHASH_FREE
;
2332 dtrace_membar_producer();
2335 free
= dcpu
->dtdsc_dirty
;
2336 dvar
->dtdv_next
= free
;
2337 } while (dtrace_casptr(&dcpu
->dtdsc_dirty
, free
, dvar
) != free
);
2339 return (dtrace_dynvar(dstate
, nkeys
, key
, dsize
, op
, mstate
, vstate
));
2344 dtrace_aggregate_min(uint64_t *oval
, uint64_t nval
, uint64_t arg
)
2346 #pragma unused(arg) /* __APPLE__ */
2347 if ((int64_t)nval
< (int64_t)*oval
)
2353 dtrace_aggregate_max(uint64_t *oval
, uint64_t nval
, uint64_t arg
)
2355 #pragma unused(arg) /* __APPLE__ */
2356 if ((int64_t)nval
> (int64_t)*oval
)
2361 dtrace_aggregate_quantize(uint64_t *quanta
, uint64_t nval
, uint64_t incr
)
2363 int i
, zero
= DTRACE_QUANTIZE_ZEROBUCKET
;
2364 int64_t val
= (int64_t)nval
;
2367 for (i
= 0; i
< zero
; i
++) {
2368 if (val
<= DTRACE_QUANTIZE_BUCKETVAL(i
)) {
2374 for (i
= zero
+ 1; i
< DTRACE_QUANTIZE_NBUCKETS
; i
++) {
2375 if (val
< DTRACE_QUANTIZE_BUCKETVAL(i
)) {
2376 quanta
[i
- 1] += incr
;
2381 quanta
[DTRACE_QUANTIZE_NBUCKETS
- 1] += incr
;
2389 dtrace_aggregate_lquantize(uint64_t *lquanta
, uint64_t nval
, uint64_t incr
)
2391 uint64_t arg
= *lquanta
++;
2392 int32_t base
= DTRACE_LQUANTIZE_BASE(arg
);
2393 uint16_t step
= DTRACE_LQUANTIZE_STEP(arg
);
2394 uint16_t levels
= DTRACE_LQUANTIZE_LEVELS(arg
);
2395 int32_t val
= (int32_t)nval
, level
;
2398 ASSERT(levels
!= 0);
2402 * This is an underflow.
2408 level
= (val
- base
) / step
;
2410 if (level
< levels
) {
2411 lquanta
[level
+ 1] += incr
;
2416 * This is an overflow.
2418 lquanta
[levels
+ 1] += incr
;
2422 dtrace_aggregate_llquantize_bucket(int16_t factor
, int16_t low
, int16_t high
,
2423 int16_t nsteps
, int64_t value
)
2425 int64_t this = 1, last
, next
;
2426 int base
= 1, order
;
2428 for (order
= 0; order
< low
; ++order
)
2432 * If our value is less than our factor taken to the power of the
2433 * low order of magnitude, it goes into the zeroth bucket.
2440 for (this *= factor
; order
<= high
; ++order
) {
2441 int nbuckets
= this > nsteps
? nsteps
: this;
2444 * We should not generally get log/linear quantizations
2445 * with a high magnitude that allows 64-bits to
2446 * overflow, but we nonetheless protect against this
2447 * by explicitly checking for overflow, and clamping
2448 * our value accordingly.
2450 next
= this * factor
;
2456 * If our value lies within this order of magnitude,
2457 * determine its position by taking the offset within
2458 * the order of magnitude, dividing by the bucket
2459 * width, and adding to our (accumulated) base.
2462 return (base
+ (value
- last
) / (this / nbuckets
));
2465 base
+= nbuckets
- (nbuckets
/ factor
);
2471 * Our value is greater than or equal to our factor taken to the
2472 * power of one plus the high magnitude -- return the top bucket.
2478 dtrace_aggregate_llquantize(uint64_t *llquanta
, uint64_t nval
, uint64_t incr
)
2480 uint64_t arg
= *llquanta
++;
2481 uint16_t factor
= DTRACE_LLQUANTIZE_FACTOR(arg
);
2482 uint16_t low
= DTRACE_LLQUANTIZE_LOW(arg
);
2483 uint16_t high
= DTRACE_LLQUANTIZE_HIGH(arg
);
2484 uint16_t nsteps
= DTRACE_LLQUANTIZE_NSTEP(arg
);
2486 llquanta
[dtrace_aggregate_llquantize_bucket(factor
, low
, high
, nsteps
, nval
)] += incr
;
2491 dtrace_aggregate_avg(uint64_t *data
, uint64_t nval
, uint64_t arg
)
2493 #pragma unused(arg) /* __APPLE__ */
2500 dtrace_aggregate_stddev(uint64_t *data
, uint64_t nval
, uint64_t arg
)
2502 #pragma unused(arg) /* __APPLE__ */
2503 int64_t snval
= (int64_t)nval
;
2510 * What we want to say here is:
2512 * data[2] += nval * nval;
2514 * But given that nval is 64-bit, we could easily overflow, so
2515 * we do this as 128-bit arithmetic.
2520 dtrace_multiply_128((uint64_t)snval
, (uint64_t)snval
, tmp
);
2521 dtrace_add_128(data
+ 2, tmp
, data
+ 2);
2526 dtrace_aggregate_count(uint64_t *oval
, uint64_t nval
, uint64_t arg
)
2528 #pragma unused(nval, arg) /* __APPLE__ */
2534 dtrace_aggregate_sum(uint64_t *oval
, uint64_t nval
, uint64_t arg
)
2536 #pragma unused(arg) /* __APPLE__ */
2541 * Aggregate given the tuple in the principal data buffer, and the aggregating
2542 * action denoted by the specified dtrace_aggregation_t. The aggregation
2543 * buffer is specified as the buf parameter. This routine does not return
2544 * failure; if there is no space in the aggregation buffer, the data will be
2545 * dropped, and a corresponding counter incremented.
2547 __attribute__((noinline
))
2549 dtrace_aggregate(dtrace_aggregation_t
*agg
, dtrace_buffer_t
*dbuf
,
2550 intptr_t offset
, dtrace_buffer_t
*buf
, uint64_t expr
, uint64_t arg
)
2553 dtrace_recdesc_t
*rec
= &agg
->dtag_action
.dta_rec
;
2554 uint32_t i
, ndx
, size
, fsize
;
2555 uint32_t align
= sizeof (uint64_t) - 1;
2556 dtrace_aggbuffer_t
*agb
;
2557 dtrace_aggkey_t
*key
;
2558 uint32_t hashval
= 0, limit
, isstr
;
2559 caddr_t tomax
, data
, kdata
;
2560 dtrace_actkind_t action
;
2561 dtrace_action_t
*act
;
2567 if (!agg
->dtag_hasarg
) {
2569 * Currently, only quantize() and lquantize() take additional
2570 * arguments, and they have the same semantics: an increment
2571 * value that defaults to 1 when not present. If additional
2572 * aggregating actions take arguments, the setting of the
2573 * default argument value will presumably have to become more
2579 action
= agg
->dtag_action
.dta_kind
- DTRACEACT_AGGREGATION
;
2580 size
= rec
->dtrd_offset
- agg
->dtag_base
;
2581 fsize
= size
+ rec
->dtrd_size
;
2583 ASSERT(dbuf
->dtb_tomax
!= NULL
);
2584 data
= dbuf
->dtb_tomax
+ offset
+ agg
->dtag_base
;
2586 if ((tomax
= buf
->dtb_tomax
) == NULL
) {
2587 dtrace_buffer_drop(buf
);
2592 * The metastructure is always at the bottom of the buffer.
2594 agb
= (dtrace_aggbuffer_t
*)(tomax
+ buf
->dtb_size
-
2595 sizeof (dtrace_aggbuffer_t
));
2597 if (buf
->dtb_offset
== 0) {
2599 * We just kludge up approximately 1/8th of the size to be
2600 * buckets. If this guess ends up being routinely
2601 * off-the-mark, we may need to dynamically readjust this
2602 * based on past performance.
2604 uintptr_t hashsize
= (buf
->dtb_size
>> 3) / sizeof (uintptr_t);
2606 if ((uintptr_t)agb
- hashsize
* sizeof (dtrace_aggkey_t
*) <
2607 (uintptr_t)tomax
|| hashsize
== 0) {
2609 * We've been given a ludicrously small buffer;
2610 * increment our drop count and leave.
2612 dtrace_buffer_drop(buf
);
2617 * And now, a pathetic attempt to try to get a an odd (or
2618 * perchance, a prime) hash size for better hash distribution.
2620 if (hashsize
> (DTRACE_AGGHASHSIZE_SLEW
<< 3))
2621 hashsize
-= DTRACE_AGGHASHSIZE_SLEW
;
2623 agb
->dtagb_hashsize
= hashsize
;
2624 agb
->dtagb_hash
= (dtrace_aggkey_t
**)((uintptr_t)agb
-
2625 agb
->dtagb_hashsize
* sizeof (dtrace_aggkey_t
*));
2626 agb
->dtagb_free
= (uintptr_t)agb
->dtagb_hash
;
2628 for (i
= 0; i
< agb
->dtagb_hashsize
; i
++)
2629 agb
->dtagb_hash
[i
] = NULL
;
2632 ASSERT(agg
->dtag_first
!= NULL
);
2633 ASSERT(agg
->dtag_first
->dta_intuple
);
2636 * Calculate the hash value based on the key. Note that we _don't_
2637 * include the aggid in the hashing (but we will store it as part of
2638 * the key). The hashing algorithm is Bob Jenkins' "One-at-a-time"
2639 * algorithm: a simple, quick algorithm that has no known funnels, and
2640 * gets good distribution in practice. The efficacy of the hashing
2641 * algorithm (and a comparison with other algorithms) may be found by
2642 * running the ::dtrace_aggstat MDB dcmd.
2644 for (act
= agg
->dtag_first
; act
->dta_intuple
; act
= act
->dta_next
) {
2645 i
= act
->dta_rec
.dtrd_offset
- agg
->dtag_base
;
2646 limit
= i
+ act
->dta_rec
.dtrd_size
;
2647 ASSERT(limit
<= size
);
2648 isstr
= DTRACEACT_ISSTRING(act
);
2650 for (; i
< limit
; i
++) {
2652 hashval
+= (hashval
<< 10);
2653 hashval
^= (hashval
>> 6);
2655 if (isstr
&& data
[i
] == '\0')
2660 hashval
+= (hashval
<< 3);
2661 hashval
^= (hashval
>> 11);
2662 hashval
+= (hashval
<< 15);
2665 * Yes, the divide here is expensive -- but it's generally the least
2666 * of the performance issues given the amount of data that we iterate
2667 * over to compute hash values, compare data, etc.
2669 ndx
= hashval
% agb
->dtagb_hashsize
;
2671 for (key
= agb
->dtagb_hash
[ndx
]; key
!= NULL
; key
= key
->dtak_next
) {
2672 ASSERT((caddr_t
)key
>= tomax
);
2673 ASSERT((caddr_t
)key
< tomax
+ buf
->dtb_size
);
2675 if (hashval
!= key
->dtak_hashval
|| key
->dtak_size
!= size
)
2678 kdata
= key
->dtak_data
;
2679 ASSERT(kdata
>= tomax
&& kdata
< tomax
+ buf
->dtb_size
);
2681 for (act
= agg
->dtag_first
; act
->dta_intuple
;
2682 act
= act
->dta_next
) {
2683 i
= act
->dta_rec
.dtrd_offset
- agg
->dtag_base
;
2684 limit
= i
+ act
->dta_rec
.dtrd_size
;
2685 ASSERT(limit
<= size
);
2686 isstr
= DTRACEACT_ISSTRING(act
);
2688 for (; i
< limit
; i
++) {
2689 if (kdata
[i
] != data
[i
])
2692 if (isstr
&& data
[i
] == '\0')
2697 if (action
!= key
->dtak_action
) {
2699 * We are aggregating on the same value in the same
2700 * aggregation with two different aggregating actions.
2701 * (This should have been picked up in the compiler,
2702 * so we may be dealing with errant or devious DIF.)
2703 * This is an error condition; we indicate as much,
2706 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
2711 * This is a hit: we need to apply the aggregator to
2712 * the value at this key.
2714 agg
->dtag_aggregate((uint64_t *)(kdata
+ size
), expr
, arg
);
2721 * We didn't find it. We need to allocate some zero-filled space,
2722 * link it into the hash table appropriately, and apply the aggregator
2723 * to the (zero-filled) value.
2725 offs
= buf
->dtb_offset
;
2726 while (offs
& (align
- 1))
2727 offs
+= sizeof (uint32_t);
2730 * If we don't have enough room to both allocate a new key _and_
2731 * its associated data, increment the drop count and return.
2733 if ((uintptr_t)tomax
+ offs
+ fsize
>
2734 agb
->dtagb_free
- sizeof (dtrace_aggkey_t
)) {
2735 dtrace_buffer_drop(buf
);
2740 ASSERT(!(sizeof (dtrace_aggkey_t
) & (sizeof (uintptr_t) - 1)));
2741 key
= (dtrace_aggkey_t
*)(agb
->dtagb_free
- sizeof (dtrace_aggkey_t
));
2742 agb
->dtagb_free
-= sizeof (dtrace_aggkey_t
);
2744 key
->dtak_data
= kdata
= tomax
+ offs
;
2745 buf
->dtb_offset
= offs
+ fsize
;
2748 * Now copy the data across.
2750 *((dtrace_aggid_t
*)kdata
) = agg
->dtag_id
;
2752 for (i
= sizeof (dtrace_aggid_t
); i
< size
; i
++)
2756 * Because strings are not zeroed out by default, we need to iterate
2757 * looking for actions that store strings, and we need to explicitly
2758 * pad these strings out with zeroes.
2760 for (act
= agg
->dtag_first
; act
->dta_intuple
; act
= act
->dta_next
) {
2763 if (!DTRACEACT_ISSTRING(act
))
2766 i
= act
->dta_rec
.dtrd_offset
- agg
->dtag_base
;
2767 limit
= i
+ act
->dta_rec
.dtrd_size
;
2768 ASSERT(limit
<= size
);
2770 for (nul
= 0; i
< limit
; i
++) {
2776 if (data
[i
] != '\0')
2783 for (i
= size
; i
< fsize
; i
++)
2786 key
->dtak_hashval
= hashval
;
2787 key
->dtak_size
= size
;
2788 key
->dtak_action
= action
;
2789 key
->dtak_next
= agb
->dtagb_hash
[ndx
];
2790 agb
->dtagb_hash
[ndx
] = key
;
2793 * Finally, apply the aggregator.
2795 *((uint64_t *)(key
->dtak_data
+ size
)) = agg
->dtag_initial
;
2796 agg
->dtag_aggregate((uint64_t *)(key
->dtak_data
+ size
), expr
, arg
);
2800 * Given consumer state, this routine finds a speculation in the INACTIVE
2801 * state and transitions it into the ACTIVE state. If there is no speculation
2802 * in the INACTIVE state, 0 is returned. In this case, no error counter is
2803 * incremented -- it is up to the caller to take appropriate action.
2806 dtrace_speculation(dtrace_state_t
*state
)
2809 dtrace_speculation_state_t current
;
2810 uint32_t *stat
= &state
->dts_speculations_unavail
, count
;
2812 while (i
< state
->dts_nspeculations
) {
2813 dtrace_speculation_t
*spec
= &state
->dts_speculations
[i
];
2815 current
= spec
->dtsp_state
;
2817 if (current
!= DTRACESPEC_INACTIVE
) {
2818 if (current
== DTRACESPEC_COMMITTINGMANY
||
2819 current
== DTRACESPEC_COMMITTING
||
2820 current
== DTRACESPEC_DISCARDING
)
2821 stat
= &state
->dts_speculations_busy
;
2826 if (dtrace_cas32((uint32_t *)&spec
->dtsp_state
,
2827 current
, DTRACESPEC_ACTIVE
) == current
)
2832 * We couldn't find a speculation. If we found as much as a single
2833 * busy speculation buffer, we'll attribute this failure as "busy"
2834 * instead of "unavail".
2838 } while (dtrace_cas32(stat
, count
, count
+ 1) != count
);
2844 * This routine commits an active speculation. If the specified speculation
2845 * is not in a valid state to perform a commit(), this routine will silently do
2846 * nothing. The state of the specified speculation is transitioned according
2847 * to the state transition diagram outlined in <sys/dtrace_impl.h>
2850 dtrace_speculation_commit(dtrace_state_t
*state
, processorid_t cpu
,
2851 dtrace_specid_t which
)
2853 dtrace_speculation_t
*spec
;
2854 dtrace_buffer_t
*src
, *dest
;
2855 uintptr_t daddr
, saddr
, dlimit
, slimit
;
2856 dtrace_speculation_state_t current
, new = DTRACESPEC_INACTIVE
;
2863 if (which
> (dtrace_specid_t
)state
->dts_nspeculations
) {
2864 cpu_core
[cpu
].cpuc_dtrace_flags
|= CPU_DTRACE_ILLOP
;
2868 spec
= &state
->dts_speculations
[which
- 1];
2869 src
= &spec
->dtsp_buffer
[cpu
];
2870 dest
= &state
->dts_buffer
[cpu
];
2873 current
= spec
->dtsp_state
;
2875 if (current
== DTRACESPEC_COMMITTINGMANY
)
2879 case DTRACESPEC_INACTIVE
:
2880 case DTRACESPEC_DISCARDING
:
2883 case DTRACESPEC_COMMITTING
:
2885 * This is only possible if we are (a) commit()'ing
2886 * without having done a prior speculate() on this CPU
2887 * and (b) racing with another commit() on a different
2888 * CPU. There's nothing to do -- we just assert that
2891 ASSERT(src
->dtb_offset
== 0);
2894 case DTRACESPEC_ACTIVE
:
2895 new = DTRACESPEC_COMMITTING
;
2898 case DTRACESPEC_ACTIVEONE
:
2900 * This speculation is active on one CPU. If our
2901 * buffer offset is non-zero, we know that the one CPU
2902 * must be us. Otherwise, we are committing on a
2903 * different CPU from the speculate(), and we must
2904 * rely on being asynchronously cleaned.
2906 if (src
->dtb_offset
!= 0) {
2907 new = DTRACESPEC_COMMITTING
;
2912 case DTRACESPEC_ACTIVEMANY
:
2913 new = DTRACESPEC_COMMITTINGMANY
;
2919 } while (dtrace_cas32((uint32_t *)&spec
->dtsp_state
,
2920 current
, new) != current
);
2923 * We have set the state to indicate that we are committing this
2924 * speculation. Now reserve the necessary space in the destination
2927 if ((offs
= dtrace_buffer_reserve(dest
, src
->dtb_offset
,
2928 sizeof (uint64_t), state
, NULL
)) < 0) {
2929 dtrace_buffer_drop(dest
);
2934 * We have sufficient space to copy the speculative buffer into the
2935 * primary buffer. First, modify the speculative buffer, filling
2936 * in the timestamp of all entries with the current time. The data
2937 * must have the commit() time rather than the time it was traced,
2938 * so that all entries in the primary buffer are in timestamp order.
2940 timestamp
= dtrace_gethrtime();
2941 saddr
= (uintptr_t)src
->dtb_tomax
;
2942 slimit
= saddr
+ src
->dtb_offset
;
2943 while (saddr
< slimit
) {
2945 dtrace_rechdr_t
*dtrh
= (dtrace_rechdr_t
*)saddr
;
2947 if (dtrh
->dtrh_epid
== DTRACE_EPIDNONE
) {
2948 saddr
+= sizeof (dtrace_epid_t
);
2952 ASSERT(dtrh
->dtrh_epid
<= ((dtrace_epid_t
) state
->dts_necbs
));
2953 size
= state
->dts_ecbs
[dtrh
->dtrh_epid
- 1]->dte_size
;
2955 ASSERT(saddr
+ size
<= slimit
);
2956 ASSERT(size
>= sizeof(dtrace_rechdr_t
));
2957 ASSERT(DTRACE_RECORD_LOAD_TIMESTAMP(dtrh
) == UINT64_MAX
);
2959 DTRACE_RECORD_STORE_TIMESTAMP(dtrh
, timestamp
);
2965 * Copy the buffer across. (Note that this is a
2966 * highly subobtimal bcopy(); in the unlikely event that this becomes
2967 * a serious performance issue, a high-performance DTrace-specific
2968 * bcopy() should obviously be invented.)
2970 daddr
= (uintptr_t)dest
->dtb_tomax
+ offs
;
2971 dlimit
= daddr
+ src
->dtb_offset
;
2972 saddr
= (uintptr_t)src
->dtb_tomax
;
2975 * First, the aligned portion.
2977 while (dlimit
- daddr
>= sizeof (uint64_t)) {
2978 *((uint64_t *)daddr
) = *((uint64_t *)saddr
);
2980 daddr
+= sizeof (uint64_t);
2981 saddr
+= sizeof (uint64_t);
2985 * Now any left-over bit...
2987 while (dlimit
- daddr
)
2988 *((uint8_t *)daddr
++) = *((uint8_t *)saddr
++);
2991 * Finally, commit the reserved space in the destination buffer.
2993 dest
->dtb_offset
= offs
+ src
->dtb_offset
;
2997 * If we're lucky enough to be the only active CPU on this speculation
2998 * buffer, we can just set the state back to DTRACESPEC_INACTIVE.
3000 if (current
== DTRACESPEC_ACTIVE
||
3001 (current
== DTRACESPEC_ACTIVEONE
&& new == DTRACESPEC_COMMITTING
)) {
3002 uint32_t rval
= dtrace_cas32((uint32_t *)&spec
->dtsp_state
,
3003 DTRACESPEC_COMMITTING
, DTRACESPEC_INACTIVE
);
3004 #pragma unused(rval) /* __APPLE__ */
3006 ASSERT(rval
== DTRACESPEC_COMMITTING
);
3009 src
->dtb_offset
= 0;
3010 src
->dtb_xamot_drops
+= src
->dtb_drops
;
3015 * This routine discards an active speculation. If the specified speculation
3016 * is not in a valid state to perform a discard(), this routine will silently
3017 * do nothing. The state of the specified speculation is transitioned
3018 * according to the state transition diagram outlined in <sys/dtrace_impl.h>
3020 __attribute__((noinline
))
3022 dtrace_speculation_discard(dtrace_state_t
*state
, processorid_t cpu
,
3023 dtrace_specid_t which
)
3025 dtrace_speculation_t
*spec
;
3026 dtrace_speculation_state_t current
, new = DTRACESPEC_INACTIVE
;
3027 dtrace_buffer_t
*buf
;
3032 if (which
> (dtrace_specid_t
)state
->dts_nspeculations
) {
3033 cpu_core
[cpu
].cpuc_dtrace_flags
|= CPU_DTRACE_ILLOP
;
3037 spec
= &state
->dts_speculations
[which
- 1];
3038 buf
= &spec
->dtsp_buffer
[cpu
];
3041 current
= spec
->dtsp_state
;
3044 case DTRACESPEC_INACTIVE
:
3045 case DTRACESPEC_COMMITTINGMANY
:
3046 case DTRACESPEC_COMMITTING
:
3047 case DTRACESPEC_DISCARDING
:
3050 case DTRACESPEC_ACTIVE
:
3051 case DTRACESPEC_ACTIVEMANY
:
3052 new = DTRACESPEC_DISCARDING
;
3055 case DTRACESPEC_ACTIVEONE
:
3056 if (buf
->dtb_offset
!= 0) {
3057 new = DTRACESPEC_INACTIVE
;
3059 new = DTRACESPEC_DISCARDING
;
3066 } while (dtrace_cas32((uint32_t *)&spec
->dtsp_state
,
3067 current
, new) != current
);
3069 buf
->dtb_offset
= 0;
3074 * Note: not called from probe context. This function is called
3075 * asynchronously from cross call context to clean any speculations that are
3076 * in the COMMITTINGMANY or DISCARDING states. These speculations may not be
3077 * transitioned back to the INACTIVE state until all CPUs have cleaned the
3081 dtrace_speculation_clean_here(dtrace_state_t
*state
)
3083 dtrace_icookie_t cookie
;
3084 processorid_t cpu
= CPU
->cpu_id
;
3085 dtrace_buffer_t
*dest
= &state
->dts_buffer
[cpu
];
3088 cookie
= dtrace_interrupt_disable();
3090 if (dest
->dtb_tomax
== NULL
) {
3091 dtrace_interrupt_enable(cookie
);
3095 for (i
= 0; i
< (dtrace_specid_t
)state
->dts_nspeculations
; i
++) {
3096 dtrace_speculation_t
*spec
= &state
->dts_speculations
[i
];
3097 dtrace_buffer_t
*src
= &spec
->dtsp_buffer
[cpu
];
3099 if (src
->dtb_tomax
== NULL
)
3102 if (spec
->dtsp_state
== DTRACESPEC_DISCARDING
) {
3103 src
->dtb_offset
= 0;
3107 if (spec
->dtsp_state
!= DTRACESPEC_COMMITTINGMANY
)
3110 if (src
->dtb_offset
== 0)
3113 dtrace_speculation_commit(state
, cpu
, i
+ 1);
3116 dtrace_interrupt_enable(cookie
);
3120 * Note: not called from probe context. This function is called
3121 * asynchronously (and at a regular interval) to clean any speculations that
3122 * are in the COMMITTINGMANY or DISCARDING states. If it discovers that there
3123 * is work to be done, it cross calls all CPUs to perform that work;
3124 * COMMITMANY and DISCARDING speculations may not be transitioned back to the
3125 * INACTIVE state until they have been cleaned by all CPUs.
3128 dtrace_speculation_clean(dtrace_state_t
*state
)
3134 for (i
= 0; i
< (dtrace_specid_t
)state
->dts_nspeculations
; i
++) {
3135 dtrace_speculation_t
*spec
= &state
->dts_speculations
[i
];
3137 ASSERT(!spec
->dtsp_cleaning
);
3139 if (spec
->dtsp_state
!= DTRACESPEC_DISCARDING
&&
3140 spec
->dtsp_state
!= DTRACESPEC_COMMITTINGMANY
)
3144 spec
->dtsp_cleaning
= 1;
3150 dtrace_xcall(DTRACE_CPUALL
,
3151 (dtrace_xcall_t
)dtrace_speculation_clean_here
, state
);
3154 * We now know that all CPUs have committed or discarded their
3155 * speculation buffers, as appropriate. We can now set the state
3158 for (i
= 0; i
< (dtrace_specid_t
)state
->dts_nspeculations
; i
++) {
3159 dtrace_speculation_t
*spec
= &state
->dts_speculations
[i
];
3160 dtrace_speculation_state_t current
, new;
3162 if (!spec
->dtsp_cleaning
)
3165 current
= spec
->dtsp_state
;
3166 ASSERT(current
== DTRACESPEC_DISCARDING
||
3167 current
== DTRACESPEC_COMMITTINGMANY
);
3169 new = DTRACESPEC_INACTIVE
;
3171 rv
= dtrace_cas32((uint32_t *)&spec
->dtsp_state
, current
, new);
3172 ASSERT(rv
== current
);
3173 spec
->dtsp_cleaning
= 0;
3178 * Called as part of a speculate() to get the speculative buffer associated
3179 * with a given speculation. Returns NULL if the specified speculation is not
3180 * in an ACTIVE state. If the speculation is in the ACTIVEONE state -- and
3181 * the active CPU is not the specified CPU -- the speculation will be
3182 * atomically transitioned into the ACTIVEMANY state.
3184 __attribute__((noinline
))
3185 static dtrace_buffer_t
*
3186 dtrace_speculation_buffer(dtrace_state_t
*state
, processorid_t cpuid
,
3187 dtrace_specid_t which
)
3189 dtrace_speculation_t
*spec
;
3190 dtrace_speculation_state_t current
, new = DTRACESPEC_INACTIVE
;
3191 dtrace_buffer_t
*buf
;
3196 if (which
> (dtrace_specid_t
)state
->dts_nspeculations
) {
3197 cpu_core
[cpuid
].cpuc_dtrace_flags
|= CPU_DTRACE_ILLOP
;
3201 spec
= &state
->dts_speculations
[which
- 1];
3202 buf
= &spec
->dtsp_buffer
[cpuid
];
3205 current
= spec
->dtsp_state
;
3208 case DTRACESPEC_INACTIVE
:
3209 case DTRACESPEC_COMMITTINGMANY
:
3210 case DTRACESPEC_DISCARDING
:
3213 case DTRACESPEC_COMMITTING
:
3214 ASSERT(buf
->dtb_offset
== 0);
3217 case DTRACESPEC_ACTIVEONE
:
3219 * This speculation is currently active on one CPU.
3220 * Check the offset in the buffer; if it's non-zero,
3221 * that CPU must be us (and we leave the state alone).
3222 * If it's zero, assume that we're starting on a new
3223 * CPU -- and change the state to indicate that the
3224 * speculation is active on more than one CPU.
3226 if (buf
->dtb_offset
!= 0)
3229 new = DTRACESPEC_ACTIVEMANY
;
3232 case DTRACESPEC_ACTIVEMANY
:
3235 case DTRACESPEC_ACTIVE
:
3236 new = DTRACESPEC_ACTIVEONE
;
3242 } while (dtrace_cas32((uint32_t *)&spec
->dtsp_state
,
3243 current
, new) != current
);
3245 ASSERT(new == DTRACESPEC_ACTIVEONE
|| new == DTRACESPEC_ACTIVEMANY
);
3250 * Return a string. In the event that the user lacks the privilege to access
3251 * arbitrary kernel memory, we copy the string out to scratch memory so that we
3252 * don't fail access checking.
3254 * dtrace_dif_variable() uses this routine as a helper for various
3255 * builtin values such as 'execname' and 'probefunc.'
3259 dtrace_dif_varstr(uintptr_t addr
, dtrace_state_t
*state
,
3260 dtrace_mstate_t
*mstate
)
3262 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
3267 * The easy case: this probe is allowed to read all of memory, so
3268 * we can just return this as a vanilla pointer.
3270 if ((mstate
->dtms_access
& DTRACE_ACCESS_KERNEL
) != 0)
3274 * This is the tougher case: we copy the string in question from
3275 * kernel memory into scratch memory and return it that way: this
3276 * ensures that we won't trip up when access checking tests the
3277 * BYREF return value.
3279 strsz
= dtrace_strlen((char *)addr
, size
) + 1;
3281 if (mstate
->dtms_scratch_ptr
+ strsz
>
3282 mstate
->dtms_scratch_base
+ mstate
->dtms_scratch_size
) {
3283 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
3287 dtrace_strcpy((const void *)addr
, (void *)mstate
->dtms_scratch_ptr
,
3289 ret
= mstate
->dtms_scratch_ptr
;
3290 mstate
->dtms_scratch_ptr
+= strsz
;
3295 * This function implements the DIF emulator's variable lookups. The emulator
3296 * passes a reserved variable identifier and optional built-in array index.
3299 dtrace_dif_variable(dtrace_mstate_t
*mstate
, dtrace_state_t
*state
, uint64_t v
,
3303 * If we're accessing one of the uncached arguments, we'll turn this
3304 * into a reference in the args array.
3306 if (v
>= DIF_VAR_ARG0
&& v
<= DIF_VAR_ARG9
) {
3307 ndx
= v
- DIF_VAR_ARG0
;
3313 ASSERT(mstate
->dtms_present
& DTRACE_MSTATE_ARGS
);
3314 if (ndx
>= sizeof (mstate
->dtms_arg
) /
3315 sizeof (mstate
->dtms_arg
[0])) {
3316 int aframes
= mstate
->dtms_probe
->dtpr_aframes
+ 2;
3317 dtrace_vstate_t
*vstate
= &state
->dts_vstate
;
3318 dtrace_provider_t
*pv
;
3321 pv
= mstate
->dtms_probe
->dtpr_provider
;
3322 if (pv
->dtpv_pops
.dtps_getargval
!= NULL
)
3323 val
= pv
->dtpv_pops
.dtps_getargval(pv
->dtpv_arg
,
3324 mstate
->dtms_probe
->dtpr_id
,
3325 mstate
->dtms_probe
->dtpr_arg
, ndx
, aframes
);
3326 /* Special case access of arg5 as passed to dtrace_probe_error() (which see.) */
3327 else if (mstate
->dtms_probe
->dtpr_id
== dtrace_probeid_error
&& ndx
== 5) {
3328 return ((dtrace_state_t
*)(uintptr_t)(mstate
->dtms_arg
[0]))->dts_arg_error_illval
;
3332 val
= dtrace_getarg(ndx
, aframes
, mstate
, vstate
);
3335 * This is regrettably required to keep the compiler
3336 * from tail-optimizing the call to dtrace_getarg().
3337 * The condition always evaluates to true, but the
3338 * compiler has no way of figuring that out a priori.
3339 * (None of this would be necessary if the compiler
3340 * could be relied upon to _always_ tail-optimize
3341 * the call to dtrace_getarg() -- but it can't.)
3343 if (mstate
->dtms_probe
!= NULL
)
3349 return (mstate
->dtms_arg
[ndx
]);
3351 case DIF_VAR_UREGS
: {
3354 if (!dtrace_priv_proc(state
))
3357 if ((thread
= current_thread()) == NULL
) {
3358 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR
);
3359 cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
= 0;
3363 return (dtrace_getreg(find_user_regs(thread
), ndx
));
3366 case DIF_VAR_VMREGS
: {
3369 if (!dtrace_priv_kernel(state
))
3372 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
3374 rval
= dtrace_getvmreg(ndx
);
3376 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
3381 case DIF_VAR_CURTHREAD
:
3382 if (!dtrace_priv_kernel(state
))
3385 return ((uint64_t)(uintptr_t)current_thread());
3387 case DIF_VAR_TIMESTAMP
:
3388 if (!(mstate
->dtms_present
& DTRACE_MSTATE_TIMESTAMP
)) {
3389 mstate
->dtms_timestamp
= dtrace_gethrtime();
3390 mstate
->dtms_present
|= DTRACE_MSTATE_TIMESTAMP
;
3392 return (mstate
->dtms_timestamp
);
3394 case DIF_VAR_VTIMESTAMP
:
3395 ASSERT(dtrace_vtime_references
!= 0);
3396 return (dtrace_get_thread_vtime(current_thread()));
3398 case DIF_VAR_WALLTIMESTAMP
:
3399 if (!(mstate
->dtms_present
& DTRACE_MSTATE_WALLTIMESTAMP
)) {
3400 mstate
->dtms_walltimestamp
= dtrace_gethrestime();
3401 mstate
->dtms_present
|= DTRACE_MSTATE_WALLTIMESTAMP
;
3403 return (mstate
->dtms_walltimestamp
);
3405 case DIF_VAR_MACHTIMESTAMP
:
3406 if (!(mstate
->dtms_present
& DTRACE_MSTATE_MACHTIMESTAMP
)) {
3407 mstate
->dtms_machtimestamp
= mach_absolute_time();
3408 mstate
->dtms_present
|= DTRACE_MSTATE_MACHTIMESTAMP
;
3410 return (mstate
->dtms_machtimestamp
);
3412 case DIF_VAR_MACHCTIMESTAMP
:
3413 if (!(mstate
->dtms_present
& DTRACE_MSTATE_MACHCTIMESTAMP
)) {
3414 mstate
->dtms_machctimestamp
= mach_continuous_time();
3415 mstate
->dtms_present
|= DTRACE_MSTATE_MACHCTIMESTAMP
;
3417 return (mstate
->dtms_machctimestamp
);
3421 return ((uint64_t) dtrace_get_thread_last_cpu_id(current_thread()));
3424 if (!dtrace_priv_kernel(state
))
3426 if (!(mstate
->dtms_present
& DTRACE_MSTATE_IPL
)) {
3427 mstate
->dtms_ipl
= dtrace_getipl();
3428 mstate
->dtms_present
|= DTRACE_MSTATE_IPL
;
3430 return (mstate
->dtms_ipl
);
3433 ASSERT(mstate
->dtms_present
& DTRACE_MSTATE_EPID
);
3434 return (mstate
->dtms_epid
);
3437 ASSERT(mstate
->dtms_present
& DTRACE_MSTATE_PROBE
);
3438 return (mstate
->dtms_probe
->dtpr_id
);
3440 case DIF_VAR_STACKDEPTH
:
3441 if (!dtrace_priv_kernel(state
))
3443 if (!(mstate
->dtms_present
& DTRACE_MSTATE_STACKDEPTH
)) {
3444 int aframes
= mstate
->dtms_probe
->dtpr_aframes
+ 2;
3446 mstate
->dtms_stackdepth
= dtrace_getstackdepth(aframes
);
3447 mstate
->dtms_present
|= DTRACE_MSTATE_STACKDEPTH
;
3449 return (mstate
->dtms_stackdepth
);
3451 case DIF_VAR_USTACKDEPTH
:
3452 if (!dtrace_priv_proc(state
))
3454 if (!(mstate
->dtms_present
& DTRACE_MSTATE_USTACKDEPTH
)) {
3456 * See comment in DIF_VAR_PID.
3458 if (DTRACE_ANCHORED(mstate
->dtms_probe
) &&
3460 mstate
->dtms_ustackdepth
= 0;
3462 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
3463 mstate
->dtms_ustackdepth
=
3464 dtrace_getustackdepth();
3465 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
3467 mstate
->dtms_present
|= DTRACE_MSTATE_USTACKDEPTH
;
3469 return (mstate
->dtms_ustackdepth
);
3471 case DIF_VAR_CALLER
:
3472 if (!dtrace_priv_kernel(state
))
3474 if (!(mstate
->dtms_present
& DTRACE_MSTATE_CALLER
)) {
3475 int aframes
= mstate
->dtms_probe
->dtpr_aframes
+ 2;
3477 if (!DTRACE_ANCHORED(mstate
->dtms_probe
)) {
3479 * If this is an unanchored probe, we are
3480 * required to go through the slow path:
3481 * dtrace_caller() only guarantees correct
3482 * results for anchored probes.
3486 dtrace_getpcstack(caller
, 2, aframes
,
3487 (uint32_t *)(uintptr_t)mstate
->dtms_arg
[0]);
3488 mstate
->dtms_caller
= caller
[1];
3489 } else if ((mstate
->dtms_caller
=
3490 dtrace_caller(aframes
)) == (uintptr_t)-1) {
3492 * We have failed to do this the quick way;
3493 * we must resort to the slower approach of
3494 * calling dtrace_getpcstack().
3498 dtrace_getpcstack(&caller
, 1, aframes
, NULL
);
3499 mstate
->dtms_caller
= caller
;
3502 mstate
->dtms_present
|= DTRACE_MSTATE_CALLER
;
3504 return (mstate
->dtms_caller
);
3506 case DIF_VAR_UCALLER
:
3507 if (!dtrace_priv_proc(state
))
3510 if (!(mstate
->dtms_present
& DTRACE_MSTATE_UCALLER
)) {
3514 * dtrace_getupcstack() fills in the first uint64_t
3515 * with the current PID. The second uint64_t will
3516 * be the program counter at user-level. The third
3517 * uint64_t will contain the caller, which is what
3521 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
3522 dtrace_getupcstack(ustack
, 3);
3523 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
3524 mstate
->dtms_ucaller
= ustack
[2];
3525 mstate
->dtms_present
|= DTRACE_MSTATE_UCALLER
;
3528 return (mstate
->dtms_ucaller
);
3530 case DIF_VAR_PROBEPROV
:
3531 ASSERT(mstate
->dtms_present
& DTRACE_MSTATE_PROBE
);
3532 return (dtrace_dif_varstr(
3533 (uintptr_t)mstate
->dtms_probe
->dtpr_provider
->dtpv_name
,
3536 case DIF_VAR_PROBEMOD
:
3537 ASSERT(mstate
->dtms_present
& DTRACE_MSTATE_PROBE
);
3538 return (dtrace_dif_varstr(
3539 (uintptr_t)mstate
->dtms_probe
->dtpr_mod
,
3542 case DIF_VAR_PROBEFUNC
:
3543 ASSERT(mstate
->dtms_present
& DTRACE_MSTATE_PROBE
);
3544 return (dtrace_dif_varstr(
3545 (uintptr_t)mstate
->dtms_probe
->dtpr_func
,
3548 case DIF_VAR_PROBENAME
:
3549 ASSERT(mstate
->dtms_present
& DTRACE_MSTATE_PROBE
);
3550 return (dtrace_dif_varstr(
3551 (uintptr_t)mstate
->dtms_probe
->dtpr_name
,
3555 if (!dtrace_priv_proc_relaxed(state
))
3559 * Note that we are assuming that an unanchored probe is
3560 * always due to a high-level interrupt. (And we're assuming
3561 * that there is only a single high level interrupt.)
3563 if (DTRACE_ANCHORED(mstate
->dtms_probe
) && CPU_ON_INTR(CPU
))
3564 /* Anchored probe that fires while on an interrupt accrues to process 0 */
3567 return ((uint64_t)dtrace_proc_selfpid());
3570 if (!dtrace_priv_proc_relaxed(state
))
3574 * See comment in DIF_VAR_PID.
3576 if (DTRACE_ANCHORED(mstate
->dtms_probe
) && CPU_ON_INTR(CPU
))
3579 return ((uint64_t)dtrace_proc_selfppid());
3582 /* We do not need to check for null current_thread() */
3583 return thread_tid(current_thread()); /* globally unique */
3585 case DIF_VAR_PTHREAD_SELF
:
3586 if (!dtrace_priv_proc(state
))
3589 /* Not currently supported, but we should be able to delta the dispatchqaddr and dispatchqoffset to get pthread_self */
3592 case DIF_VAR_DISPATCHQADDR
:
3593 if (!dtrace_priv_proc(state
))
3596 /* We do not need to check for null current_thread() */
3597 return thread_dispatchqaddr(current_thread());
3599 case DIF_VAR_EXECNAME
:
3601 char *xname
= (char *)mstate
->dtms_scratch_ptr
;
3602 char *pname
= proc_best_name(curproc
);
3603 size_t scratch_size
= sizeof(proc_name_t
);
3605 /* The scratch allocation's lifetime is that of the clause. */
3606 if (!DTRACE_INSCRATCH(mstate
, scratch_size
)) {
3607 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
3611 if (!dtrace_priv_proc_relaxed(state
))
3614 mstate
->dtms_scratch_ptr
+= scratch_size
;
3615 strlcpy(xname
, pname
, scratch_size
);
3617 return ((uint64_t)(uintptr_t)xname
);
3621 case DIF_VAR_ZONENAME
:
3623 /* scratch_size is equal to length('global') + 1 for the null-terminator. */
3624 char *zname
= (char *)mstate
->dtms_scratch_ptr
;
3625 size_t scratch_size
= 6 + 1;
3627 if (!dtrace_priv_proc(state
))
3630 /* The scratch allocation's lifetime is that of the clause. */
3631 if (!DTRACE_INSCRATCH(mstate
, scratch_size
)) {
3632 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
3636 mstate
->dtms_scratch_ptr
+= scratch_size
;
3638 /* The kernel does not provide zonename, it will always return 'global'. */
3639 strlcpy(zname
, "global", scratch_size
);
3641 return ((uint64_t)(uintptr_t)zname
);
3645 case DIF_VAR_CPUINSTRS
:
3646 return mt_cur_cpu_instrs();
3648 case DIF_VAR_CPUCYCLES
:
3649 return mt_cur_cpu_cycles();
3651 case DIF_VAR_VINSTRS
:
3652 return mt_cur_thread_instrs();
3654 case DIF_VAR_VCYCLES
:
3655 return mt_cur_thread_cycles();
3656 #else /* MONOTONIC */
3657 case DIF_VAR_CPUINSTRS
: /* FALLTHROUGH */
3658 case DIF_VAR_CPUCYCLES
: /* FALLTHROUGH */
3659 case DIF_VAR_VINSTRS
: /* FALLTHROUGH */
3660 case DIF_VAR_VCYCLES
: /* FALLTHROUGH */
3662 #endif /* !MONOTONIC */
3665 if (!dtrace_priv_proc_relaxed(state
))
3669 * See comment in DIF_VAR_PID.
3671 if (DTRACE_ANCHORED(mstate
->dtms_probe
) && CPU_ON_INTR(CPU
))
3674 return ((uint64_t) dtrace_proc_selfruid());
3677 if (!dtrace_priv_proc(state
))
3681 * See comment in DIF_VAR_PID.
3683 if (DTRACE_ANCHORED(mstate
->dtms_probe
) && CPU_ON_INTR(CPU
))
3686 if (dtrace_CRED() != NULL
)
3687 /* Credential does not require lazy initialization. */
3688 return ((uint64_t)kauth_getgid());
3690 /* proc_lock would be taken under kauth_cred_proc_ref() in kauth_cred_get(). */
3691 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
3695 case DIF_VAR_ERRNO
: {
3696 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
3697 if (!dtrace_priv_proc(state
))
3701 * See comment in DIF_VAR_PID.
3703 if (DTRACE_ANCHORED(mstate
->dtms_probe
) && CPU_ON_INTR(CPU
))
3707 return (uint64_t)uthread
->t_dtrace_errno
;
3709 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
3715 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
3720 typedef enum dtrace_json_state
{
3721 DTRACE_JSON_REST
= 1,
3724 DTRACE_JSON_STRING_ESCAPE
,
3725 DTRACE_JSON_STRING_ESCAPE_UNICODE
,
3729 DTRACE_JSON_IDENTIFIER
,
3731 DTRACE_JSON_NUMBER_FRAC
,
3732 DTRACE_JSON_NUMBER_EXP
,
3733 DTRACE_JSON_COLLECT_OBJECT
3734 } dtrace_json_state_t
;
3737 * This function possesses just enough knowledge about JSON to extract a single
3738 * value from a JSON string and store it in the scratch buffer. It is able
3739 * to extract nested object values, and members of arrays by index.
3741 * elemlist is a list of JSON keys, stored as packed NUL-terminated strings, to
3742 * be looked up as we descend into the object tree. e.g.
3744 * foo[0].bar.baz[32] --> "foo" NUL "0" NUL "bar" NUL "baz" NUL "32" NUL
3747 * The run time of this function must be bounded above by strsize to limit the
3748 * amount of work done in probe context. As such, it is implemented as a
3749 * simple state machine, reading one character at a time using safe loads
3750 * until we find the requested element, hit a parsing error or run off the
3751 * end of the object or string.
3753 * As there is no way for a subroutine to return an error without interrupting
3754 * clause execution, we simply return NULL in the event of a missing key or any
3755 * other error condition. Each NULL return in this function is commented with
3756 * the error condition it represents -- parsing or otherwise.
3758 * The set of states for the state machine closely matches the JSON
3759 * specification (http://json.org/). Briefly:
3762 * Skip whitespace until we find either a top-level Object, moving
3763 * to DTRACE_JSON_OBJECT; or an Array, moving to DTRACE_JSON_VALUE.
3765 * DTRACE_JSON_OBJECT:
3766 * Locate the next key String in an Object. Sets a flag to denote
3767 * the next String as a key string and moves to DTRACE_JSON_STRING.
3769 * DTRACE_JSON_COLON:
3770 * Skip whitespace until we find the colon that separates key Strings
3771 * from their values. Once found, move to DTRACE_JSON_VALUE.
3773 * DTRACE_JSON_VALUE:
3774 * Detects the type of the next value (String, Number, Identifier, Object
3775 * or Array) and routes to the states that process that type. Here we also
3776 * deal with the element selector list if we are requested to traverse down
3777 * into the object tree.
3779 * DTRACE_JSON_COMMA:
3780 * Skip whitespace until we find the comma that separates key-value pairs
3781 * in Objects (returning to DTRACE_JSON_OBJECT) or values in Arrays
3782 * (similarly DTRACE_JSON_VALUE). All following literal value processing
3783 * states return to this state at the end of their value, unless otherwise
3786 * DTRACE_JSON_NUMBER, DTRACE_JSON_NUMBER_FRAC, DTRACE_JSON_NUMBER_EXP:
3787 * Processes a Number literal from the JSON, including any exponent
3788 * component that may be present. Numbers are returned as strings, which
3789 * may be passed to strtoll() if an integer is required.
3791 * DTRACE_JSON_IDENTIFIER:
3792 * Processes a "true", "false" or "null" literal in the JSON.
3794 * DTRACE_JSON_STRING, DTRACE_JSON_STRING_ESCAPE,
3795 * DTRACE_JSON_STRING_ESCAPE_UNICODE:
3796 * Processes a String literal from the JSON, whether the String denotes
3797 * a key, a value or part of a larger Object. Handles all escape sequences
3798 * present in the specification, including four-digit unicode characters,
3799 * but merely includes the escape sequence without converting it to the
3800 * actual escaped character. If the String is flagged as a key, we
3801 * move to DTRACE_JSON_COLON rather than DTRACE_JSON_COMMA.
3803 * DTRACE_JSON_COLLECT_OBJECT:
3804 * This state collects an entire Object (or Array), correctly handling
3805 * embedded strings. If the full element selector list matches this nested
3806 * object, we return the Object in full as a string. If not, we use this
3807 * state to skip to the next value at this level and continue processing.
3810 dtrace_json(uint64_t size
, uintptr_t json
, char *elemlist
, int nelems
,
3813 dtrace_json_state_t state
= DTRACE_JSON_REST
;
3814 int64_t array_elem
= INT64_MIN
;
3815 int64_t array_pos
= 0;
3816 uint8_t escape_unicount
= 0;
3817 boolean_t string_is_key
= B_FALSE
;
3818 boolean_t collect_object
= B_FALSE
;
3819 boolean_t found_key
= B_FALSE
;
3820 boolean_t in_array
= B_FALSE
;
3821 uint32_t braces
= 0, brackets
= 0;
3822 char *elem
= elemlist
;
3826 for (cur
= json
; cur
< json
+ size
; cur
++) {
3827 char cc
= dtrace_load8(cur
);
3832 case DTRACE_JSON_REST
:
3837 state
= DTRACE_JSON_OBJECT
;
3844 array_elem
= dtrace_strtoll(elem
, 10, size
);
3845 found_key
= array_elem
== 0 ? B_TRUE
: B_FALSE
;
3846 state
= DTRACE_JSON_VALUE
;
3851 * ERROR: expected to find a top-level object or array.
3854 case DTRACE_JSON_OBJECT
:
3859 state
= DTRACE_JSON_STRING
;
3860 string_is_key
= B_TRUE
;
3865 * ERROR: either the object did not start with a key
3866 * string, or we've run off the end of the object
3867 * without finding the requested key.
3870 case DTRACE_JSON_STRING
:
3873 state
= DTRACE_JSON_STRING_ESCAPE
;
3878 if (collect_object
) {
3880 * We don't reset the dest here, as
3881 * the string is part of a larger
3882 * object being collected.
3885 collect_object
= B_FALSE
;
3886 state
= DTRACE_JSON_COLLECT_OBJECT
;
3890 dd
= dest
; /* reset string buffer */
3891 if (string_is_key
) {
3892 if (dtrace_strncmp(dest
, elem
,
3895 } else if (found_key
) {
3898 * We expected an object, not
3905 state
= string_is_key
? DTRACE_JSON_COLON
:
3907 string_is_key
= B_FALSE
;
3913 case DTRACE_JSON_STRING_ESCAPE
:
3916 escape_unicount
= 0;
3917 state
= DTRACE_JSON_STRING_ESCAPE_UNICODE
;
3919 state
= DTRACE_JSON_STRING
;
3922 case DTRACE_JSON_STRING_ESCAPE_UNICODE
:
3923 if (!isxdigit(cc
)) {
3925 * ERROR: invalid unicode escape, expected
3926 * four valid hexidecimal digits.
3932 if (++escape_unicount
== 4)
3933 state
= DTRACE_JSON_STRING
;
3935 case DTRACE_JSON_COLON
:
3940 state
= DTRACE_JSON_VALUE
;
3945 * ERROR: expected a colon.
3948 case DTRACE_JSON_COMMA
:
3954 state
= DTRACE_JSON_VALUE
;
3955 if (++array_pos
== array_elem
)
3958 state
= DTRACE_JSON_OBJECT
;
3964 * ERROR: either we hit an unexpected character, or
3965 * we reached the end of the object or array without
3966 * finding the requested key.
3969 case DTRACE_JSON_IDENTIFIER
:
3976 dd
= dest
; /* reset string buffer */
3978 if (dtrace_strncmp(dest
, "true", 5) == 0 ||
3979 dtrace_strncmp(dest
, "false", 6) == 0 ||
3980 dtrace_strncmp(dest
, "null", 5) == 0) {
3984 * ERROR: We expected an object,
3985 * not this identifier.
3992 state
= DTRACE_JSON_COMMA
;
3998 * ERROR: we did not recognise the identifier as one
3999 * of those in the JSON specification.
4002 case DTRACE_JSON_NUMBER
:
4005 state
= DTRACE_JSON_NUMBER_FRAC
;
4009 if (cc
== 'x' || cc
== 'X') {
4011 * ERROR: specification explicitly excludes
4012 * hexidecimal or octal numbers.
4018 case DTRACE_JSON_NUMBER_FRAC
:
4019 if (cc
== 'e' || cc
== 'E') {
4021 state
= DTRACE_JSON_NUMBER_EXP
;
4025 if (cc
== '+' || cc
== '-') {
4027 * ERROR: expect sign as part of exponent only.
4032 case DTRACE_JSON_NUMBER_EXP
:
4033 if (isdigit(cc
) || cc
== '+' || cc
== '-') {
4039 dd
= dest
; /* reset string buffer */
4043 * ERROR: We expected an object, not
4052 state
= DTRACE_JSON_COMMA
;
4054 case DTRACE_JSON_VALUE
:
4058 if (cc
== '{' || cc
== '[') {
4059 if (nelems
> 1 && found_key
) {
4060 in_array
= cc
== '[' ? B_TRUE
: B_FALSE
;
4062 * If our element selector directs us
4063 * to descend into this nested object,
4064 * then move to the next selector
4065 * element in the list and restart the
4068 while (*elem
!= '\0')
4070 elem
++; /* skip the inter-element NUL */
4074 state
= DTRACE_JSON_VALUE
;
4076 array_elem
= dtrace_strtoll(
4078 found_key
= array_elem
== 0 ?
4081 found_key
= B_FALSE
;
4082 state
= DTRACE_JSON_OBJECT
;
4088 * Otherwise, we wish to either skip this
4089 * nested object or return it in full.
4096 state
= DTRACE_JSON_COLLECT_OBJECT
;
4101 state
= DTRACE_JSON_STRING
;
4107 * Here we deal with true, false and null.
4110 state
= DTRACE_JSON_IDENTIFIER
;
4114 if (cc
== '-' || isdigit(cc
)) {
4116 state
= DTRACE_JSON_NUMBER
;
4121 * ERROR: unexpected character at start of value.
4124 case DTRACE_JSON_COLLECT_OBJECT
:
4127 * ERROR: unexpected end of input.
4133 collect_object
= B_TRUE
;
4134 state
= DTRACE_JSON_STRING
;
4139 if (brackets
-- == 0) {
4141 * ERROR: unbalanced brackets.
4145 } else if (cc
== '}') {
4146 if (braces
-- == 0) {
4148 * ERROR: unbalanced braces.
4152 } else if (cc
== '{') {
4154 } else if (cc
== '[') {
4158 if (brackets
== 0 && braces
== 0) {
4163 dd
= dest
; /* reset string buffer */
4164 state
= DTRACE_JSON_COMMA
;
4173 * Emulate the execution of DTrace ID subroutines invoked by the call opcode.
4174 * Notice that we don't bother validating the proper number of arguments or
4175 * their types in the tuple stack. This isn't needed because all argument
4176 * interpretation is safe because of our load safety -- the worst that can
4177 * happen is that a bogus program can obtain bogus results.
4180 dtrace_dif_subr(uint_t subr
, uint_t rd
, uint64_t *regs
,
4181 dtrace_key_t
*tupregs
, int nargs
,
4182 dtrace_mstate_t
*mstate
, dtrace_state_t
*state
)
4184 volatile uint16_t *flags
= &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
4185 volatile uint64_t *illval
= &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
;
4186 dtrace_vstate_t
*vstate
= &state
->dts_vstate
;
4188 #if !defined(__APPLE__)
4199 /* FIXME: awaits lock/mutex work */
4200 #endif /* __APPLE__ */
4204 regs
[rd
] = dtrace_xoroshiro128_plus_next(
4205 state
->dts_rstate
[CPU
->cpu_id
]);
4208 #if !defined(__APPLE__)
4209 case DIF_SUBR_MUTEX_OWNED
:
4210 if (!dtrace_canload(tupregs
[0].dttk_value
, sizeof (kmutex_t
),
4216 m
.mx
= dtrace_load64(tupregs
[0].dttk_value
);
4217 if (MUTEX_TYPE_ADAPTIVE(&m
.mi
))
4218 regs
[rd
] = MUTEX_OWNER(&m
.mi
) != MUTEX_NO_OWNER
;
4220 regs
[rd
] = LOCK_HELD(&m
.mi
.m_spin
.m_spinlock
);
4223 case DIF_SUBR_MUTEX_OWNER
:
4224 if (!dtrace_canload(tupregs
[0].dttk_value
, sizeof (kmutex_t
),
4230 m
.mx
= dtrace_load64(tupregs
[0].dttk_value
);
4231 if (MUTEX_TYPE_ADAPTIVE(&m
.mi
) &&
4232 MUTEX_OWNER(&m
.mi
) != MUTEX_NO_OWNER
)
4233 regs
[rd
] = (uintptr_t)MUTEX_OWNER(&m
.mi
);
4238 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE
:
4239 if (!dtrace_canload(tupregs
[0].dttk_value
, sizeof (kmutex_t
),
4245 m
.mx
= dtrace_load64(tupregs
[0].dttk_value
);
4246 regs
[rd
] = MUTEX_TYPE_ADAPTIVE(&m
.mi
);
4249 case DIF_SUBR_MUTEX_TYPE_SPIN
:
4250 if (!dtrace_canload(tupregs
[0].dttk_value
, sizeof (kmutex_t
),
4256 m
.mx
= dtrace_load64(tupregs
[0].dttk_value
);
4257 regs
[rd
] = MUTEX_TYPE_SPIN(&m
.mi
);
4260 case DIF_SUBR_RW_READ_HELD
: {
4263 if (!dtrace_canload(tupregs
[0].dttk_value
, sizeof (uintptr_t),
4269 r
.rw
= dtrace_loadptr(tupregs
[0].dttk_value
);
4270 regs
[rd
] = _RW_READ_HELD(&r
.ri
, tmp
);
4274 case DIF_SUBR_RW_WRITE_HELD
:
4275 if (!dtrace_canload(tupregs
[0].dttk_value
, sizeof (krwlock_t
),
4281 r
.rw
= dtrace_loadptr(tupregs
[0].dttk_value
);
4282 regs
[rd
] = _RW_WRITE_HELD(&r
.ri
);
4285 case DIF_SUBR_RW_ISWRITER
:
4286 if (!dtrace_canload(tupregs
[0].dttk_value
, sizeof (krwlock_t
),
4292 r
.rw
= dtrace_loadptr(tupregs
[0].dttk_value
);
4293 regs
[rd
] = _RW_ISWRITER(&r
.ri
);
4296 /* FIXME: awaits lock/mutex work */
4297 #endif /* __APPLE__ */
4299 case DIF_SUBR_BCOPY
: {
4301 * We need to be sure that the destination is in the scratch
4302 * region -- no other region is allowed.
4304 uintptr_t src
= tupregs
[0].dttk_value
;
4305 uintptr_t dest
= tupregs
[1].dttk_value
;
4306 size_t size
= tupregs
[2].dttk_value
;
4308 if (!dtrace_inscratch(dest
, size
, mstate
)) {
4309 *flags
|= CPU_DTRACE_BADADDR
;
4314 if (!dtrace_canload(src
, size
, mstate
, vstate
)) {
4319 dtrace_bcopy((void *)src
, (void *)dest
, size
);
4323 case DIF_SUBR_ALLOCA
:
4324 case DIF_SUBR_COPYIN
: {
4325 uintptr_t dest
= P2ROUNDUP(mstate
->dtms_scratch_ptr
, 8);
4327 tupregs
[subr
== DIF_SUBR_ALLOCA
? 0 : 1].dttk_value
;
4328 size_t scratch_size
= (dest
- mstate
->dtms_scratch_ptr
) + size
;
4331 * Check whether the user can access kernel memory
4333 if (dtrace_priv_kernel(state
) == 0) {
4334 DTRACE_CPUFLAG_SET(CPU_DTRACE_KPRIV
);
4339 * This action doesn't require any credential checks since
4340 * probes will not activate in user contexts to which the
4341 * enabling user does not have permissions.
4345 * Rounding up the user allocation size could have overflowed
4346 * a large, bogus allocation (like -1ULL) to 0.
4348 if (scratch_size
< size
||
4349 !DTRACE_INSCRATCH(mstate
, scratch_size
)) {
4350 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
4355 if (subr
== DIF_SUBR_COPYIN
) {
4356 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
4357 if (dtrace_priv_proc(state
))
4358 dtrace_copyin(tupregs
[0].dttk_value
, dest
, size
, flags
);
4359 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
4362 mstate
->dtms_scratch_ptr
+= scratch_size
;
4367 case DIF_SUBR_COPYINTO
: {
4368 uint64_t size
= tupregs
[1].dttk_value
;
4369 uintptr_t dest
= tupregs
[2].dttk_value
;
4372 * This action doesn't require any credential checks since
4373 * probes will not activate in user contexts to which the
4374 * enabling user does not have permissions.
4376 if (!dtrace_inscratch(dest
, size
, mstate
)) {
4377 *flags
|= CPU_DTRACE_BADADDR
;
4382 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
4383 if (dtrace_priv_proc(state
))
4384 dtrace_copyin(tupregs
[0].dttk_value
, dest
, size
, flags
);
4385 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
4389 case DIF_SUBR_COPYINSTR
: {
4390 uintptr_t dest
= mstate
->dtms_scratch_ptr
;
4391 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
4393 if (nargs
> 1 && tupregs
[1].dttk_value
< size
)
4394 size
= tupregs
[1].dttk_value
+ 1;
4397 * This action doesn't require any credential checks since
4398 * probes will not activate in user contexts to which the
4399 * enabling user does not have permissions.
4401 if (!DTRACE_INSCRATCH(mstate
, size
)) {
4402 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
4407 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
4408 if (dtrace_priv_proc(state
))
4409 dtrace_copyinstr(tupregs
[0].dttk_value
, dest
, size
, flags
);
4410 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
4412 ((char *)dest
)[size
- 1] = '\0';
4413 mstate
->dtms_scratch_ptr
+= size
;
4418 case DIF_SUBR_MSGSIZE
:
4419 case DIF_SUBR_MSGDSIZE
: {
4420 /* Darwin does not implement SysV streams messages */
4421 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
4426 case DIF_SUBR_PROGENYOF
: {
4427 pid_t pid
= tupregs
[0].dttk_value
;
4428 struct proc
*p
= current_proc();
4429 int rval
= 0, lim
= nprocs
;
4431 while(p
&& (lim
-- > 0)) {
4434 ppid
= (pid_t
)dtrace_load32((uintptr_t)&(p
->p_pid
));
4435 if (*flags
& CPU_DTRACE_FAULT
)
4444 break; /* Can't climb process tree any further. */
4446 p
= (struct proc
*)dtrace_loadptr((uintptr_t)&(p
->p_pptr
));
4447 #if __has_feature(ptrauth_calls)
4448 p
= ptrauth_strip(p
, ptrauth_key_process_independent_data
);
4450 if (*flags
& CPU_DTRACE_FAULT
)
4458 case DIF_SUBR_SPECULATION
:
4459 regs
[rd
] = dtrace_speculation(state
);
4463 case DIF_SUBR_COPYOUT
: {
4464 uintptr_t kaddr
= tupregs
[0].dttk_value
;
4465 user_addr_t uaddr
= tupregs
[1].dttk_value
;
4466 uint64_t size
= tupregs
[2].dttk_value
;
4468 if (!dtrace_destructive_disallow
&&
4469 dtrace_priv_proc_control(state
) &&
4470 !dtrace_istoxic(kaddr
, size
) &&
4471 dtrace_canload(kaddr
, size
, mstate
, vstate
)) {
4472 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
4473 dtrace_copyout(kaddr
, uaddr
, size
, flags
);
4474 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
4479 case DIF_SUBR_COPYOUTSTR
: {
4480 uintptr_t kaddr
= tupregs
[0].dttk_value
;
4481 user_addr_t uaddr
= tupregs
[1].dttk_value
;
4482 uint64_t size
= tupregs
[2].dttk_value
;
4485 if (!dtrace_destructive_disallow
&&
4486 dtrace_priv_proc_control(state
) &&
4487 !dtrace_istoxic(kaddr
, size
) &&
4488 dtrace_strcanload(kaddr
, size
, &lim
, mstate
, vstate
)) {
4489 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
4490 dtrace_copyoutstr(kaddr
, uaddr
, lim
, flags
);
4491 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
4496 case DIF_SUBR_STRLEN
: {
4497 size_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
4498 uintptr_t addr
= (uintptr_t)tupregs
[0].dttk_value
;
4501 if (!dtrace_strcanload(addr
, size
, &lim
, mstate
, vstate
)) {
4506 regs
[rd
] = dtrace_strlen((char *)addr
, lim
);
4511 case DIF_SUBR_STRCHR
:
4512 case DIF_SUBR_STRRCHR
: {
4514 * We're going to iterate over the string looking for the
4515 * specified character. We will iterate until we have reached
4516 * the string length or we have found the character. If this
4517 * is DIF_SUBR_STRRCHR, we will look for the last occurrence
4518 * of the specified character instead of the first.
4520 uintptr_t addr
= tupregs
[0].dttk_value
;
4521 uintptr_t addr_limit
;
4522 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
4524 char c
, target
= (char)tupregs
[1].dttk_value
;
4526 if (!dtrace_strcanload(addr
, size
, &lim
, mstate
, vstate
)) {
4530 addr_limit
= addr
+ lim
;
4532 for (regs
[rd
] = 0; addr
< addr_limit
; addr
++) {
4533 if ((c
= dtrace_load8(addr
)) == target
) {
4536 if (subr
== DIF_SUBR_STRCHR
)
4547 case DIF_SUBR_STRSTR
:
4548 case DIF_SUBR_INDEX
:
4549 case DIF_SUBR_RINDEX
: {
4551 * We're going to iterate over the string looking for the
4552 * specified string. We will iterate until we have reached
4553 * the string length or we have found the string. (Yes, this
4554 * is done in the most naive way possible -- but considering
4555 * that the string we're searching for is likely to be
4556 * relatively short, the complexity of Rabin-Karp or similar
4557 * hardly seems merited.)
4559 char *addr
= (char *)(uintptr_t)tupregs
[0].dttk_value
;
4560 char *substr
= (char *)(uintptr_t)tupregs
[1].dttk_value
;
4561 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
4562 size_t len
= dtrace_strlen(addr
, size
);
4563 size_t sublen
= dtrace_strlen(substr
, size
);
4564 char *limit
= addr
+ len
, *orig
= addr
;
4565 int notfound
= subr
== DIF_SUBR_STRSTR
? 0 : -1;
4568 regs
[rd
] = notfound
;
4570 if (!dtrace_canload((uintptr_t)addr
, len
+ 1, mstate
, vstate
)) {
4575 if (!dtrace_canload((uintptr_t)substr
, sublen
+ 1, mstate
,
4582 * strstr() and index()/rindex() have similar semantics if
4583 * both strings are the empty string: strstr() returns a
4584 * pointer to the (empty) string, and index() and rindex()
4585 * both return index 0 (regardless of any position argument).
4587 if (sublen
== 0 && len
== 0) {
4588 if (subr
== DIF_SUBR_STRSTR
)
4589 regs
[rd
] = (uintptr_t)addr
;
4595 if (subr
!= DIF_SUBR_STRSTR
) {
4596 if (subr
== DIF_SUBR_RINDEX
) {
4603 * Both index() and rindex() take an optional position
4604 * argument that denotes the starting position.
4607 int64_t pos
= (int64_t)tupregs
[2].dttk_value
;
4610 * If the position argument to index() is
4611 * negative, Perl implicitly clamps it at
4612 * zero. This semantic is a little surprising
4613 * given the special meaning of negative
4614 * positions to similar Perl functions like
4615 * substr(), but it appears to reflect a
4616 * notion that index() can start from a
4617 * negative index and increment its way up to
4618 * the string. Given this notion, Perl's
4619 * rindex() is at least self-consistent in
4620 * that it implicitly clamps positions greater
4621 * than the string length to be the string
4622 * length. Where Perl completely loses
4623 * coherence, however, is when the specified
4624 * substring is the empty string (""). In
4625 * this case, even if the position is
4626 * negative, rindex() returns 0 -- and even if
4627 * the position is greater than the length,
4628 * index() returns the string length. These
4629 * semantics violate the notion that index()
4630 * should never return a value less than the
4631 * specified position and that rindex() should
4632 * never return a value greater than the
4633 * specified position. (One assumes that
4634 * these semantics are artifacts of Perl's
4635 * implementation and not the results of
4636 * deliberate design -- it beggars belief that
4637 * even Larry Wall could desire such oddness.)
4638 * While in the abstract one would wish for
4639 * consistent position semantics across
4640 * substr(), index() and rindex() -- or at the
4641 * very least self-consistent position
4642 * semantics for index() and rindex() -- we
4643 * instead opt to keep with the extant Perl
4644 * semantics, in all their broken glory. (Do
4645 * we have more desire to maintain Perl's
4646 * semantics than Perl does? Probably.)
4648 if (subr
== DIF_SUBR_RINDEX
) {
4655 if ((size_t)pos
> len
)
4661 if ((size_t)pos
>= len
) {
4672 for (regs
[rd
] = notfound
; addr
!= limit
; addr
+= inc
) {
4673 if (dtrace_strncmp(addr
, substr
, sublen
) == 0) {
4674 if (subr
!= DIF_SUBR_STRSTR
) {
4676 * As D index() and rindex() are
4677 * modeled on Perl (and not on awk),
4678 * we return a zero-based (and not a
4679 * one-based) index. (For you Perl
4680 * weenies: no, we're not going to add
4681 * $[ -- and shouldn't you be at a con
4684 regs
[rd
] = (uintptr_t)(addr
- orig
);
4688 ASSERT(subr
== DIF_SUBR_STRSTR
);
4689 regs
[rd
] = (uintptr_t)addr
;
4697 case DIF_SUBR_STRTOK
: {
4698 uintptr_t addr
= tupregs
[0].dttk_value
;
4699 uintptr_t tokaddr
= tupregs
[1].dttk_value
;
4700 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
4701 uintptr_t limit
, toklimit
;
4703 char *dest
= (char *)mstate
->dtms_scratch_ptr
;
4704 uint8_t c
='\0', tokmap
[32]; /* 256 / 8 */
4708 * Check both the token buffer and (later) the input buffer,
4709 * since both could be non-scratch addresses.
4711 if (!dtrace_strcanload(tokaddr
, size
, &clim
, mstate
, vstate
)) {
4715 toklimit
= tokaddr
+ clim
;
4717 if (!DTRACE_INSCRATCH(mstate
, size
)) {
4718 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
4725 * If the address specified is NULL, we use our saved
4726 * strtok pointer from the mstate. Note that this
4727 * means that the saved strtok pointer is _only_
4728 * valid within multiple enablings of the same probe --
4729 * it behaves like an implicit clause-local variable.
4731 addr
= mstate
->dtms_strtok
;
4732 limit
= mstate
->dtms_strtok_limit
;
4735 * If the user-specified address is non-NULL we must
4736 * access check it. This is the only time we have
4737 * a chance to do so, since this address may reside
4738 * in the string table of this clause-- future calls
4739 * (when we fetch addr from mstate->dtms_strtok)
4740 * would fail this access check.
4742 if (!dtrace_strcanload(addr
, size
, &clim
, mstate
,
4747 limit
= addr
+ clim
;
4751 * First, zero the token map, and then process the token
4752 * string -- setting a bit in the map for every character
4753 * found in the token string.
4755 for (i
= 0; i
< (int)sizeof (tokmap
); i
++)
4758 for (; tokaddr
< toklimit
; tokaddr
++) {
4759 if ((c
= dtrace_load8(tokaddr
)) == '\0')
4762 ASSERT((c
>> 3) < sizeof (tokmap
));
4763 tokmap
[c
>> 3] |= (1 << (c
& 0x7));
4766 for (; addr
< limit
; addr
++) {
4768 * We're looking for a character that is _not_
4769 * contained in the token string.
4771 if ((c
= dtrace_load8(addr
)) == '\0')
4774 if (!(tokmap
[c
>> 3] & (1 << (c
& 0x7))))
4780 * We reached the end of the string without finding
4781 * any character that was not in the token string.
4782 * We return NULL in this case, and we set the saved
4783 * address to NULL as well.
4786 mstate
->dtms_strtok
= 0;
4787 mstate
->dtms_strtok_limit
= 0;
4792 * From here on, we're copying into the destination string.
4794 for (i
= 0; addr
< limit
&& i
< size
- 1; addr
++) {
4795 if ((c
= dtrace_load8(addr
)) == '\0')
4798 if (tokmap
[c
>> 3] & (1 << (c
& 0x7)))
4807 regs
[rd
] = (uintptr_t)dest
;
4808 mstate
->dtms_scratch_ptr
+= size
;
4809 mstate
->dtms_strtok
= addr
;
4810 mstate
->dtms_strtok_limit
= limit
;
4814 case DIF_SUBR_SUBSTR
: {
4815 uintptr_t s
= tupregs
[0].dttk_value
;
4816 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
4817 char *d
= (char *)mstate
->dtms_scratch_ptr
;
4818 int64_t index
= (int64_t)tupregs
[1].dttk_value
;
4819 int64_t remaining
= (int64_t)tupregs
[2].dttk_value
;
4820 size_t len
= dtrace_strlen((char *)s
, size
);
4823 if (!dtrace_canload(s
, len
+ 1, mstate
, vstate
)) {
4828 if (!DTRACE_INSCRATCH(mstate
, size
)) {
4829 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
4835 remaining
= (int64_t)size
;
4840 if (index
< 0 && index
+ remaining
> 0) {
4846 if ((size_t)index
>= len
|| index
< 0) {
4848 } else if (remaining
< 0) {
4849 remaining
+= len
- index
;
4850 } else if ((uint64_t)index
+ (uint64_t)remaining
> size
) {
4851 remaining
= size
- index
;
4854 for (i
= 0; i
< remaining
; i
++) {
4855 if ((d
[i
] = dtrace_load8(s
+ index
+ i
)) == '\0')
4861 mstate
->dtms_scratch_ptr
+= size
;
4862 regs
[rd
] = (uintptr_t)d
;
4866 case DIF_SUBR_GETMAJOR
:
4867 regs
[rd
] = (uintptr_t)major( (dev_t
)tupregs
[0].dttk_value
);
4870 case DIF_SUBR_GETMINOR
:
4871 regs
[rd
] = (uintptr_t)minor( (dev_t
)tupregs
[0].dttk_value
);
4874 case DIF_SUBR_DDI_PATHNAME
: {
4875 /* APPLE NOTE: currently unsupported on Darwin */
4876 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
4881 case DIF_SUBR_STRJOIN
: {
4882 char *d
= (char *)mstate
->dtms_scratch_ptr
;
4883 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
4884 uintptr_t s1
= tupregs
[0].dttk_value
;
4885 uintptr_t s2
= tupregs
[1].dttk_value
;
4886 uint64_t i
= 0, j
= 0;
4890 if (!dtrace_strcanload(s1
, size
, &lim1
, mstate
, vstate
) ||
4891 !dtrace_strcanload(s2
, size
, &lim2
, mstate
, vstate
)) {
4896 if (!DTRACE_INSCRATCH(mstate
, size
)) {
4897 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
4904 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
4908 c
= (i
>= lim1
) ? '\0' : dtrace_load8(s1
++);
4909 if ((d
[i
++] = c
) == '\0') {
4917 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
4921 c
= (j
++ >= lim2
) ? '\0' : dtrace_load8(s2
++);
4922 if ((d
[i
++] = c
) == '\0')
4927 mstate
->dtms_scratch_ptr
+= i
;
4928 regs
[rd
] = (uintptr_t)d
;
4934 case DIF_SUBR_STRTOLL
: {
4935 uintptr_t s
= tupregs
[0].dttk_value
;
4936 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
4941 if ((base
= tupregs
[1].dttk_value
) <= 1 ||
4942 base
> ('z' - 'a' + 1) + ('9' - '0' + 1)) {
4943 *flags
|= CPU_DTRACE_ILLOP
;
4948 if (!dtrace_strcanload(s
, size
, &lim
, mstate
, vstate
)) {
4949 regs
[rd
] = INT64_MIN
;
4953 regs
[rd
] = dtrace_strtoll((char *)s
, base
, lim
);
4957 case DIF_SUBR_LLTOSTR
: {
4958 int64_t i
= (int64_t)tupregs
[0].dttk_value
;
4959 uint64_t val
, digit
;
4960 uint64_t size
= 65; /* enough room for 2^64 in binary */
4961 char *end
= (char *)mstate
->dtms_scratch_ptr
+ size
- 1;
4965 if ((base
= tupregs
[1].dttk_value
) <= 1 ||
4966 base
> ('z' - 'a' + 1) + ('9' - '0' + 1)) {
4967 *flags
|= CPU_DTRACE_ILLOP
;
4972 val
= (base
== 10 && i
< 0) ? i
* -1 : i
;
4974 if (!DTRACE_INSCRATCH(mstate
, size
)) {
4975 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
4980 for (*end
-- = '\0'; val
; val
/= base
) {
4981 if ((digit
= val
% base
) <= '9' - '0') {
4982 *end
-- = '0' + digit
;
4984 *end
-- = 'a' + (digit
- ('9' - '0') - 1);
4988 if (i
== 0 && base
== 16)
4994 if (i
== 0 || base
== 8 || base
== 16)
4997 if (i
< 0 && base
== 10)
5000 regs
[rd
] = (uintptr_t)end
+ 1;
5001 mstate
->dtms_scratch_ptr
+= size
;
5005 case DIF_SUBR_HTONS
:
5006 case DIF_SUBR_NTOHS
:
5008 regs
[rd
] = (uint16_t)tupregs
[0].dttk_value
;
5010 regs
[rd
] = DT_BSWAP_16((uint16_t)tupregs
[0].dttk_value
);
5015 case DIF_SUBR_HTONL
:
5016 case DIF_SUBR_NTOHL
:
5018 regs
[rd
] = (uint32_t)tupregs
[0].dttk_value
;
5020 regs
[rd
] = DT_BSWAP_32((uint32_t)tupregs
[0].dttk_value
);
5025 case DIF_SUBR_HTONLL
:
5026 case DIF_SUBR_NTOHLL
:
5028 regs
[rd
] = (uint64_t)tupregs
[0].dttk_value
;
5030 regs
[rd
] = DT_BSWAP_64((uint64_t)tupregs
[0].dttk_value
);
5035 case DIF_SUBR_DIRNAME
:
5036 case DIF_SUBR_BASENAME
: {
5037 char *dest
= (char *)mstate
->dtms_scratch_ptr
;
5038 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
5039 uintptr_t src
= tupregs
[0].dttk_value
;
5040 int i
, j
, len
= dtrace_strlen((char *)src
, size
);
5041 int lastbase
= -1, firstbase
= -1, lastdir
= -1;
5044 if (!dtrace_canload(src
, len
+ 1, mstate
, vstate
)) {
5049 if (!DTRACE_INSCRATCH(mstate
, size
)) {
5050 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
5056 * The basename and dirname for a zero-length string is
5061 src
= (uintptr_t)".";
5065 * Start from the back of the string, moving back toward the
5066 * front until we see a character that isn't a slash. That
5067 * character is the last character in the basename.
5069 for (i
= len
- 1; i
>= 0; i
--) {
5070 if (dtrace_load8(src
+ i
) != '/')
5078 * Starting from the last character in the basename, move
5079 * towards the front until we find a slash. The character
5080 * that we processed immediately before that is the first
5081 * character in the basename.
5083 for (; i
>= 0; i
--) {
5084 if (dtrace_load8(src
+ i
) == '/')
5092 * Now keep going until we find a non-slash character. That
5093 * character is the last character in the dirname.
5095 for (; i
>= 0; i
--) {
5096 if (dtrace_load8(src
+ i
) != '/')
5103 ASSERT(!(lastbase
== -1 && firstbase
!= -1));
5104 ASSERT(!(firstbase
== -1 && lastdir
!= -1));
5106 if (lastbase
== -1) {
5108 * We didn't find a non-slash character. We know that
5109 * the length is non-zero, so the whole string must be
5110 * slashes. In either the dirname or the basename
5111 * case, we return '/'.
5113 ASSERT(firstbase
== -1);
5114 firstbase
= lastbase
= lastdir
= 0;
5117 if (firstbase
== -1) {
5119 * The entire string consists only of a basename
5120 * component. If we're looking for dirname, we need
5121 * to change our string to be just "."; if we're
5122 * looking for a basename, we'll just set the first
5123 * character of the basename to be 0.
5125 if (subr
== DIF_SUBR_DIRNAME
) {
5126 ASSERT(lastdir
== -1);
5127 src
= (uintptr_t)".";
5134 if (subr
== DIF_SUBR_DIRNAME
) {
5135 if (lastdir
== -1) {
5137 * We know that we have a slash in the name --
5138 * or lastdir would be set to 0, above. And
5139 * because lastdir is -1, we know that this
5140 * slash must be the first character. (That
5141 * is, the full string must be of the form
5142 * "/basename".) In this case, the last
5143 * character of the directory name is 0.
5151 ASSERT(subr
== DIF_SUBR_BASENAME
);
5152 ASSERT(firstbase
!= -1 && lastbase
!= -1);
5157 for (i
= start
, j
= 0; i
<= end
&& (uint64_t)j
< size
- 1; i
++, j
++)
5158 dest
[j
] = dtrace_load8(src
+ i
);
5161 regs
[rd
] = (uintptr_t)dest
;
5162 mstate
->dtms_scratch_ptr
+= size
;
5166 case DIF_SUBR_CLEANPATH
: {
5167 char *dest
= (char *)mstate
->dtms_scratch_ptr
, c
;
5168 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
5169 uintptr_t src
= tupregs
[0].dttk_value
;
5171 size_t i
= 0, j
= 0;
5173 if (!dtrace_strcanload(src
, size
, &lim
, mstate
, vstate
)) {
5178 if (!DTRACE_INSCRATCH(mstate
, size
)) {
5179 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
5185 * Move forward, loading each character.
5188 c
= (i
>= lim
) ? '\0' : dtrace_load8(src
+ i
++);
5190 if ((uint64_t)(j
+ 5) >= size
) /* 5 = strlen("/..c\0") */
5198 c
= (i
>= lim
) ? '\0' : dtrace_load8(src
+ i
++);
5202 * We have two slashes -- we can just advance
5203 * to the next character.
5210 * This is not "." and it's not ".." -- we can
5211 * just store the "/" and this character and
5219 c
= (i
>= lim
) ? '\0' : dtrace_load8(src
+ i
++);
5223 * This is a "/./" component. We're not going
5224 * to store anything in the destination buffer;
5225 * we're just going to go to the next component.
5232 * This is not ".." -- we can just store the
5233 * "/." and this character and continue
5242 c
= (i
>= lim
) ? '\0' : dtrace_load8(src
+ i
++);
5244 if (c
!= '/' && c
!= '\0') {
5246 * This is not ".." -- it's "..[mumble]".
5247 * We'll store the "/.." and this character
5248 * and continue processing.
5258 * This is "/../" or "/..\0". We need to back up
5259 * our destination pointer until we find a "/".
5262 while (j
!= 0 && dest
[--j
] != '/')
5267 } while (c
!= '\0');
5270 regs
[rd
] = (uintptr_t)dest
;
5271 mstate
->dtms_scratch_ptr
+= size
;
5275 case DIF_SUBR_INET_NTOA
:
5276 case DIF_SUBR_INET_NTOA6
:
5277 case DIF_SUBR_INET_NTOP
: {
5282 if (subr
== DIF_SUBR_INET_NTOP
) {
5283 af
= (int)tupregs
[0].dttk_value
;
5286 af
= subr
== DIF_SUBR_INET_NTOA
? AF_INET
: AF_INET6
;
5290 if (af
== AF_INET
) {
5291 #if !defined(__APPLE__)
5295 #endif /* __APPLE__ */
5299 * Safely load the IPv4 address.
5301 #if !defined(__APPLE__)
5302 ip4
= dtrace_load32(tupregs
[argi
].dttk_value
);
5304 if (!dtrace_canload(tupregs
[argi
].dttk_value
, sizeof(ip4
),
5311 (void *)(uintptr_t)tupregs
[argi
].dttk_value
,
5312 (void *)(uintptr_t)&ip4
, sizeof (ip4
));
5313 #endif /* __APPLE__ */
5315 * Check an IPv4 string will fit in scratch.
5317 #if !defined(__APPLE__)
5318 size
= INET_ADDRSTRLEN
;
5320 size
= MAX_IPv4_STR_LEN
;
5321 #endif /* __APPLE__ */
5322 if (!DTRACE_INSCRATCH(mstate
, size
)) {
5323 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
5327 base
= (char *)mstate
->dtms_scratch_ptr
;
5328 end
= (char *)mstate
->dtms_scratch_ptr
+ size
- 1;
5331 * Stringify as a dotted decimal quad.
5334 ptr8
= (uint8_t *)&ip4
;
5335 for (i
= 3; i
>= 0; i
--) {
5341 for (; val
; val
/= 10) {
5342 *end
-- = '0' + (val
% 10);
5349 ASSERT(end
+ 1 >= base
);
5351 } else if (af
== AF_INET6
) {
5352 #if defined(__APPLE__)
5353 #define _S6_un __u6_addr
5354 #define _S6_u8 __u6_addr8
5355 #endif /* __APPLE__ */
5356 struct in6_addr ip6
;
5357 int firstzero
, tryzero
, numzero
, v6end
;
5359 const char digits
[] = "0123456789abcdef";
5362 * Stringify using RFC 1884 convention 2 - 16 bit
5363 * hexadecimal values with a zero-run compression.
5364 * Lower case hexadecimal digits are used.
5365 * eg, fe80::214:4fff:fe0b:76c8.
5366 * The IPv4 embedded form is returned for inet_ntop,
5367 * just the IPv4 string is returned for inet_ntoa6.
5370 if (!dtrace_canload(tupregs
[argi
].dttk_value
,
5371 sizeof(struct in6_addr
), mstate
, vstate
)) {
5377 * Safely load the IPv6 address.
5380 (void *)(uintptr_t)tupregs
[argi
].dttk_value
,
5381 (void *)(uintptr_t)&ip6
, sizeof (struct in6_addr
));
5384 * Check an IPv6 string will fit in scratch.
5386 size
= INET6_ADDRSTRLEN
;
5387 if (!DTRACE_INSCRATCH(mstate
, size
)) {
5388 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
5392 base
= (char *)mstate
->dtms_scratch_ptr
;
5393 end
= (char *)mstate
->dtms_scratch_ptr
+ size
- 1;
5397 * Find the longest run of 16 bit zero values
5398 * for the single allowed zero compression - "::".
5403 for (i
= 0; i
< (int)sizeof (struct in6_addr
); i
++) {
5404 if (ip6
._S6_un
._S6_u8
[i
] == 0 &&
5405 tryzero
== -1 && i
% 2 == 0) {
5410 if (tryzero
!= -1 &&
5411 (ip6
._S6_un
._S6_u8
[i
] != 0 ||
5412 i
== sizeof (struct in6_addr
) - 1)) {
5414 if (i
- tryzero
<= numzero
) {
5419 firstzero
= tryzero
;
5420 numzero
= i
- i
% 2 - tryzero
;
5423 if (ip6
._S6_un
._S6_u8
[i
] == 0 &&
5424 i
== sizeof (struct in6_addr
) - 1)
5428 ASSERT(firstzero
+ numzero
<= (int)sizeof (struct in6_addr
));
5431 * Check for an IPv4 embedded address.
5433 v6end
= sizeof (struct in6_addr
) - 2;
5434 if (IN6_IS_ADDR_V4MAPPED(&ip6
) ||
5435 IN6_IS_ADDR_V4COMPAT(&ip6
)) {
5436 for (i
= sizeof (struct in6_addr
) - 1;
5437 i
>= (int)DTRACE_V4MAPPED_OFFSET
; i
--) {
5438 ASSERT(end
>= base
);
5440 val
= ip6
._S6_un
._S6_u8
[i
];
5445 for (; val
; val
/= 10) {
5446 *end
-- = '0' + val
% 10;
5450 if (i
> (int)DTRACE_V4MAPPED_OFFSET
)
5454 if (subr
== DIF_SUBR_INET_NTOA6
)
5458 * Set v6end to skip the IPv4 address that
5459 * we have already stringified.
5465 * Build the IPv6 string by working through the
5466 * address in reverse.
5468 for (i
= v6end
; i
>= 0; i
-= 2) {
5469 ASSERT(end
>= base
);
5471 if (i
== firstzero
+ numzero
- 2) {
5478 if (i
< 14 && i
!= firstzero
- 2)
5481 val
= (ip6
._S6_un
._S6_u8
[i
] << 8) +
5482 ip6
._S6_un
._S6_u8
[i
+ 1];
5487 for (; val
; val
/= 16) {
5488 *end
-- = digits
[val
% 16];
5492 ASSERT(end
+ 1 >= base
);
5494 #if defined(__APPLE__)
5497 #endif /* __APPLE__ */
5500 * The user didn't use AH_INET or AH_INET6.
5502 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
5507 inetout
: regs
[rd
] = (uintptr_t)end
+ 1;
5508 mstate
->dtms_scratch_ptr
+= size
;
5512 case DIF_SUBR_JSON
: {
5513 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
5514 uintptr_t json
= tupregs
[0].dttk_value
;
5515 size_t jsonlen
= dtrace_strlen((char *)json
, size
);
5516 uintptr_t elem
= tupregs
[1].dttk_value
;
5517 size_t elemlen
= dtrace_strlen((char *)elem
, size
);
5519 char *dest
= (char *)mstate
->dtms_scratch_ptr
;
5520 char *elemlist
= (char *)mstate
->dtms_scratch_ptr
+ jsonlen
+ 1;
5521 char *ee
= elemlist
;
5525 if (!dtrace_canload(json
, jsonlen
+ 1, mstate
, vstate
) ||
5526 !dtrace_canload(elem
, elemlen
+ 1, mstate
, vstate
)) {
5531 if (!DTRACE_INSCRATCH(mstate
, jsonlen
+ 1 + elemlen
+ 1)) {
5532 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
5538 * Read the element selector and split it up into a packed list
5541 for (cur
= elem
; cur
< elem
+ elemlen
; cur
++) {
5542 char cc
= dtrace_load8(cur
);
5544 if (cur
== elem
&& cc
== '[') {
5546 * If the first element selector key is
5547 * actually an array index then ignore the
5556 if (cc
== '.' || cc
== '[') {
5565 if ((regs
[rd
] = (uintptr_t)dtrace_json(size
, json
, elemlist
,
5566 nelems
, dest
)) != 0)
5567 mstate
->dtms_scratch_ptr
+= jsonlen
+ 1;
5571 case DIF_SUBR_TOUPPER
:
5572 case DIF_SUBR_TOLOWER
: {
5573 uintptr_t src
= tupregs
[0].dttk_value
;
5574 char *dest
= (char *)mstate
->dtms_scratch_ptr
;
5575 char lower
, upper
, base
, c
;
5576 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
5577 size_t len
= dtrace_strlen((char*) src
, size
);
5580 lower
= (subr
== DIF_SUBR_TOUPPER
) ? 'a' : 'A';
5581 upper
= (subr
== DIF_SUBR_TOUPPER
) ? 'z' : 'Z';
5582 base
= (subr
== DIF_SUBR_TOUPPER
) ? 'A' : 'a';
5584 if (!dtrace_canload(src
, len
+ 1, mstate
, vstate
)) {
5589 if (!DTRACE_INSCRATCH(mstate
, size
)) {
5590 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
5595 for (i
= 0; i
< size
- 1; ++i
) {
5596 if ((c
= dtrace_load8(src
+ i
)) == '\0')
5598 if (c
>= lower
&& c
<= upper
)
5599 c
= base
+ (c
- lower
);
5606 regs
[rd
] = (uintptr_t) dest
;
5607 mstate
->dtms_scratch_ptr
+= size
;
5612 case DIF_SUBR_STRIP
:
5613 if (!dtrace_is_valid_ptrauth_key(tupregs
[1].dttk_value
)) {
5614 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
5617 regs
[rd
] = (uint64_t)dtrace_ptrauth_strip(
5618 (void*)tupregs
[0].dttk_value
, tupregs
[1].dttk_value
);
5621 #if defined(__APPLE__)
5622 case DIF_SUBR_VM_KERNEL_ADDRPERM
: {
5623 if (!dtrace_priv_kernel(state
)) {
5626 regs
[rd
] = VM_KERNEL_ADDRPERM((vm_offset_t
) tupregs
[0].dttk_value
);
5632 case DIF_SUBR_KDEBUG_TRACE
: {
5634 uintptr_t args
[4] = {0};
5637 if (nargs
< 2 || nargs
> 5) {
5638 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
5642 if (dtrace_destructive_disallow
||
5643 !dtrace_priv_kernel_destructive(state
)) {
5647 debugid
= tupregs
[0].dttk_value
;
5648 for (i
= 0; i
< nargs
- 1; i
++)
5649 args
[i
] = tupregs
[i
+ 1].dttk_value
;
5651 kernel_debug(debugid
, args
[0], args
[1], args
[2], args
[3], 0);
5656 case DIF_SUBR_KDEBUG_TRACE_STRING
: {
5661 if (dtrace_destructive_disallow
||
5662 !dtrace_priv_kernel_destructive(state
)) {
5666 uint64_t size
= state
->dts_options
[DTRACEOPT_STRSIZE
];
5667 uint32_t debugid
= tupregs
[0].dttk_value
;
5668 uint64_t str_id
= tupregs
[1].dttk_value
;
5669 uintptr_t src
= tupregs
[2].dttk_value
;
5674 if (src
!= (uintptr_t)0) {
5676 if (!dtrace_strcanload(src
, size
, &lim
, mstate
, vstate
)) {
5679 dtrace_strcpy((void*)src
, buf
, size
);
5682 (void)kernel_debug_string(debugid
, &str_id
, str
);
5688 case DIF_SUBR_MTONS
:
5689 absolutetime_to_nanoseconds(tupregs
[0].dttk_value
, ®s
[rd
]);
5692 case DIF_SUBR_PHYSMEM_READ
: {
5693 #if DEBUG || DEVELOPMENT
5694 if (dtrace_destructive_disallow
||
5695 !dtrace_priv_kernel_destructive(state
)) {
5698 regs
[rd
] = dtrace_physmem_read(tupregs
[0].dttk_value
,
5699 tupregs
[1].dttk_value
);
5701 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
5702 #endif /* DEBUG || DEVELOPMENT */
5705 case DIF_SUBR_PHYSMEM_WRITE
: {
5706 #if DEBUG || DEVELOPMENT
5707 if (dtrace_destructive_disallow
||
5708 !dtrace_priv_kernel_destructive(state
)) {
5712 dtrace_physmem_write(tupregs
[0].dttk_value
,
5713 tupregs
[1].dttk_value
, (size_t)tupregs
[2].dttk_value
);
5715 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
5716 #endif /* DEBUG || DEVELOPMENT */
5720 case DIF_SUBR_KVTOPHYS
: {
5721 #if DEBUG || DEVELOPMENT
5722 regs
[rd
] = kvtophys(tupregs
[0].dttk_value
);
5724 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
5725 #endif /* DEBUG || DEVELOPMENT */
5728 #endif /* defined(__APPLE__) */
5734 * Emulate the execution of DTrace IR instructions specified by the given
5735 * DIF object. This function is deliberately void of assertions as all of
5736 * the necessary checks are handled by a call to dtrace_difo_validate().
5739 dtrace_dif_emulate(dtrace_difo_t
*difo
, dtrace_mstate_t
*mstate
,
5740 dtrace_vstate_t
*vstate
, dtrace_state_t
*state
)
5742 const dif_instr_t
*text
= difo
->dtdo_buf
;
5743 const uint_t textlen
= difo
->dtdo_len
;
5744 const char *strtab
= difo
->dtdo_strtab
;
5745 const uint64_t *inttab
= difo
->dtdo_inttab
;
5748 dtrace_statvar_t
*svar
;
5749 dtrace_dstate_t
*dstate
= &vstate
->dtvs_dynvars
;
5751 volatile uint16_t *flags
= &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
5752 volatile uint64_t *illval
= &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
;
5754 dtrace_key_t tupregs
[DIF_DTR_NREGS
+ 2]; /* +2 for thread and id */
5755 uint64_t regs
[DIF_DIR_NREGS
];
5758 uint8_t cc_n
= 0, cc_z
= 0, cc_v
= 0, cc_c
= 0;
5760 uint_t pc
= 0, id
, opc
= 0;
5766 * We stash the current DIF object into the machine state: we need it
5767 * for subsequent access checking.
5769 mstate
->dtms_difo
= difo
;
5771 regs
[DIF_REG_R0
] = 0; /* %r0 is fixed at zero */
5773 while (pc
< textlen
&& !(*flags
& CPU_DTRACE_FAULT
)) {
5777 r1
= DIF_INSTR_R1(instr
);
5778 r2
= DIF_INSTR_R2(instr
);
5779 rd
= DIF_INSTR_RD(instr
);
5781 switch (DIF_INSTR_OP(instr
)) {
5783 regs
[rd
] = regs
[r1
] | regs
[r2
];
5786 regs
[rd
] = regs
[r1
] ^ regs
[r2
];
5789 regs
[rd
] = regs
[r1
] & regs
[r2
];
5792 regs
[rd
] = regs
[r1
] << regs
[r2
];
5795 regs
[rd
] = regs
[r1
] >> regs
[r2
];
5798 regs
[rd
] = regs
[r1
] - regs
[r2
];
5801 regs
[rd
] = regs
[r1
] + regs
[r2
];
5804 regs
[rd
] = regs
[r1
] * regs
[r2
];
5807 if (regs
[r2
] == 0) {
5809 *flags
|= CPU_DTRACE_DIVZERO
;
5811 regs
[rd
] = (int64_t)regs
[r1
] /
5817 if (regs
[r2
] == 0) {
5819 *flags
|= CPU_DTRACE_DIVZERO
;
5821 regs
[rd
] = regs
[r1
] / regs
[r2
];
5826 if (regs
[r2
] == 0) {
5828 *flags
|= CPU_DTRACE_DIVZERO
;
5830 regs
[rd
] = (int64_t)regs
[r1
] %
5836 if (regs
[r2
] == 0) {
5838 *flags
|= CPU_DTRACE_DIVZERO
;
5840 regs
[rd
] = regs
[r1
] % regs
[r2
];
5845 regs
[rd
] = ~regs
[r1
];
5848 regs
[rd
] = regs
[r1
];
5851 cc_r
= regs
[r1
] - regs
[r2
];
5855 cc_c
= regs
[r1
] < regs
[r2
];
5858 cc_n
= cc_v
= cc_c
= 0;
5859 cc_z
= regs
[r1
] == 0;
5862 pc
= DIF_INSTR_LABEL(instr
);
5866 pc
= DIF_INSTR_LABEL(instr
);
5870 pc
= DIF_INSTR_LABEL(instr
);
5873 if ((cc_z
| (cc_n
^ cc_v
)) == 0)
5874 pc
= DIF_INSTR_LABEL(instr
);
5877 if ((cc_c
| cc_z
) == 0)
5878 pc
= DIF_INSTR_LABEL(instr
);
5881 if ((cc_n
^ cc_v
) == 0)
5882 pc
= DIF_INSTR_LABEL(instr
);
5886 pc
= DIF_INSTR_LABEL(instr
);
5890 pc
= DIF_INSTR_LABEL(instr
);
5894 pc
= DIF_INSTR_LABEL(instr
);
5897 if (cc_z
| (cc_n
^ cc_v
))
5898 pc
= DIF_INSTR_LABEL(instr
);
5902 pc
= DIF_INSTR_LABEL(instr
);
5905 if (!dtrace_canstore(regs
[r1
], 1, mstate
, vstate
)) {
5906 *flags
|= CPU_DTRACE_KPRIV
;
5912 regs
[rd
] = (int8_t)dtrace_load8(regs
[r1
]);
5915 if (!dtrace_canstore(regs
[r1
], 2, mstate
, vstate
)) {
5916 *flags
|= CPU_DTRACE_KPRIV
;
5922 regs
[rd
] = (int16_t)dtrace_load16(regs
[r1
]);
5925 if (!dtrace_canstore(regs
[r1
], 4, mstate
, vstate
)) {
5926 *flags
|= CPU_DTRACE_KPRIV
;
5932 regs
[rd
] = (int32_t)dtrace_load32(regs
[r1
]);
5935 if (!dtrace_canstore(regs
[r1
], 1, mstate
, vstate
)) {
5936 *flags
|= CPU_DTRACE_KPRIV
;
5942 regs
[rd
] = dtrace_load8(regs
[r1
]);
5945 if (!dtrace_canstore(regs
[r1
], 2, mstate
, vstate
)) {
5946 *flags
|= CPU_DTRACE_KPRIV
;
5952 regs
[rd
] = dtrace_load16(regs
[r1
]);
5955 if (!dtrace_canstore(regs
[r1
], 4, mstate
, vstate
)) {
5956 *flags
|= CPU_DTRACE_KPRIV
;
5962 regs
[rd
] = dtrace_load32(regs
[r1
]);
5965 if (!dtrace_canstore(regs
[r1
], 8, mstate
, vstate
)) {
5966 *flags
|= CPU_DTRACE_KPRIV
;
5972 regs
[rd
] = dtrace_load64(regs
[r1
]);
5975 * Darwin 32-bit kernel may fetch from 64-bit user.
5976 * Do not cast regs to uintptr_t
5977 * DIF_OP_ULDSB,DIF_OP_ULDSH, DIF_OP_ULDSW, DIF_OP_ULDUB
5978 * DIF_OP_ULDUH, DIF_OP_ULDUW, DIF_OP_ULDX
5982 dtrace_fuword8(regs
[r1
]);
5985 regs
[rd
] = (int16_t)
5986 dtrace_fuword16(regs
[r1
]);
5989 regs
[rd
] = (int32_t)
5990 dtrace_fuword32(regs
[r1
]);
5994 dtrace_fuword8(regs
[r1
]);
5998 dtrace_fuword16(regs
[r1
]);
6002 dtrace_fuword32(regs
[r1
]);
6006 dtrace_fuword64(regs
[r1
]);
6015 regs
[rd
] = inttab
[DIF_INSTR_INTEGER(instr
)];
6018 regs
[rd
] = (uint64_t)(uintptr_t)
6019 (strtab
+ DIF_INSTR_STRING(instr
));
6022 size_t sz
= state
->dts_options
[DTRACEOPT_STRSIZE
];
6023 uintptr_t s1
= regs
[r1
];
6024 uintptr_t s2
= regs
[r2
];
6025 size_t lim1
= sz
, lim2
= sz
;
6028 !dtrace_strcanload(s1
, sz
, &lim1
, mstate
, vstate
))
6031 !dtrace_strcanload(s2
, sz
, &lim2
, mstate
, vstate
))
6034 cc_r
= dtrace_strncmp((char *)s1
, (char *)s2
,
6043 regs
[rd
] = dtrace_dif_variable(mstate
, state
,
6047 id
= DIF_INSTR_VAR(instr
);
6049 if (id
>= DIF_VAR_OTHER_UBASE
) {
6052 id
-= DIF_VAR_OTHER_UBASE
;
6053 svar
= vstate
->dtvs_globals
[id
];
6054 ASSERT(svar
!= NULL
);
6055 v
= &svar
->dtsv_var
;
6057 if (!(v
->dtdv_type
.dtdt_flags
& DIF_TF_BYREF
)) {
6058 regs
[rd
] = svar
->dtsv_data
;
6062 a
= (uintptr_t)svar
->dtsv_data
;
6064 if (*(uint8_t *)a
== UINT8_MAX
) {
6066 * If the 0th byte is set to UINT8_MAX
6067 * then this is to be treated as a
6068 * reference to a NULL variable.
6072 regs
[rd
] = a
+ sizeof (uint64_t);
6078 regs
[rd
] = dtrace_dif_variable(mstate
, state
, id
, 0);
6082 id
= DIF_INSTR_VAR(instr
);
6084 ASSERT(id
>= DIF_VAR_OTHER_UBASE
);
6085 id
-= DIF_VAR_OTHER_UBASE
;
6087 VERIFY(id
< (uint_t
)vstate
->dtvs_nglobals
);
6088 svar
= vstate
->dtvs_globals
[id
];
6089 ASSERT(svar
!= NULL
);
6090 v
= &svar
->dtsv_var
;
6092 if (v
->dtdv_type
.dtdt_flags
& DIF_TF_BYREF
) {
6093 uintptr_t a
= (uintptr_t)svar
->dtsv_data
;
6097 ASSERT(svar
->dtsv_size
!= 0);
6099 if (regs
[rd
] == 0) {
6100 *(uint8_t *)a
= UINT8_MAX
;
6104 a
+= sizeof (uint64_t);
6106 if (!dtrace_vcanload(
6107 (void *)(uintptr_t)regs
[rd
], &v
->dtdv_type
,
6108 &lim
, mstate
, vstate
))
6111 dtrace_vcopy((void *)(uintptr_t)regs
[rd
],
6112 (void *)a
, &v
->dtdv_type
, lim
);
6116 svar
->dtsv_data
= regs
[rd
];
6121 * There are no DTrace built-in thread-local arrays at
6122 * present. This opcode is saved for future work.
6124 *flags
|= CPU_DTRACE_ILLOP
;
6129 id
= DIF_INSTR_VAR(instr
);
6131 if (id
< DIF_VAR_OTHER_UBASE
) {
6133 * For now, this has no meaning.
6139 id
-= DIF_VAR_OTHER_UBASE
;
6141 ASSERT(id
< (uint_t
)vstate
->dtvs_nlocals
);
6142 ASSERT(vstate
->dtvs_locals
!= NULL
);
6143 svar
= vstate
->dtvs_locals
[id
];
6144 ASSERT(svar
!= NULL
);
6145 v
= &svar
->dtsv_var
;
6147 if (v
->dtdv_type
.dtdt_flags
& DIF_TF_BYREF
) {
6148 uintptr_t a
= (uintptr_t)svar
->dtsv_data
;
6149 size_t sz
= v
->dtdv_type
.dtdt_size
;
6151 sz
+= sizeof (uint64_t);
6152 ASSERT(svar
->dtsv_size
== (int)NCPU
* sz
);
6153 a
+= CPU
->cpu_id
* sz
;
6155 if (*(uint8_t *)a
== UINT8_MAX
) {
6157 * If the 0th byte is set to UINT8_MAX
6158 * then this is to be treated as a
6159 * reference to a NULL variable.
6163 regs
[rd
] = a
+ sizeof (uint64_t);
6169 ASSERT(svar
->dtsv_size
== (int)NCPU
* sizeof (uint64_t));
6170 tmp
= (uint64_t *)(uintptr_t)svar
->dtsv_data
;
6171 regs
[rd
] = tmp
[CPU
->cpu_id
];
6175 id
= DIF_INSTR_VAR(instr
);
6177 ASSERT(id
>= DIF_VAR_OTHER_UBASE
);
6178 id
-= DIF_VAR_OTHER_UBASE
;
6179 VERIFY(id
< (uint_t
)vstate
->dtvs_nlocals
);
6180 ASSERT(vstate
->dtvs_locals
!= NULL
);
6181 svar
= vstate
->dtvs_locals
[id
];
6182 ASSERT(svar
!= NULL
);
6183 v
= &svar
->dtsv_var
;
6185 if (v
->dtdv_type
.dtdt_flags
& DIF_TF_BYREF
) {
6186 uintptr_t a
= (uintptr_t)svar
->dtsv_data
;
6187 size_t sz
= v
->dtdv_type
.dtdt_size
;
6190 sz
+= sizeof (uint64_t);
6191 ASSERT(svar
->dtsv_size
== (int)NCPU
* sz
);
6192 a
+= CPU
->cpu_id
* sz
;
6194 if (regs
[rd
] == 0) {
6195 *(uint8_t *)a
= UINT8_MAX
;
6199 a
+= sizeof (uint64_t);
6202 if (!dtrace_vcanload(
6203 (void *)(uintptr_t)regs
[rd
], &v
->dtdv_type
,
6204 &lim
, mstate
, vstate
))
6207 dtrace_vcopy((void *)(uintptr_t)regs
[rd
],
6208 (void *)a
, &v
->dtdv_type
, lim
);
6212 ASSERT(svar
->dtsv_size
== (int)NCPU
* sizeof (uint64_t));
6213 tmp
= (uint64_t *)(uintptr_t)svar
->dtsv_data
;
6214 tmp
[CPU
->cpu_id
] = regs
[rd
];
6218 dtrace_dynvar_t
*dvar
;
6221 id
= DIF_INSTR_VAR(instr
);
6222 ASSERT(id
>= DIF_VAR_OTHER_UBASE
);
6223 id
-= DIF_VAR_OTHER_UBASE
;
6224 v
= &vstate
->dtvs_tlocals
[id
];
6226 key
= &tupregs
[DIF_DTR_NREGS
];
6227 key
[0].dttk_value
= (uint64_t)id
;
6228 key
[0].dttk_size
= 0;
6229 DTRACE_TLS_THRKEY(key
[1].dttk_value
);
6230 key
[1].dttk_size
= 0;
6232 dvar
= dtrace_dynvar(dstate
, 2, key
,
6233 sizeof (uint64_t), DTRACE_DYNVAR_NOALLOC
,
6241 if (v
->dtdv_type
.dtdt_flags
& DIF_TF_BYREF
) {
6242 regs
[rd
] = (uint64_t)(uintptr_t)dvar
->dtdv_data
;
6244 regs
[rd
] = *((uint64_t *)dvar
->dtdv_data
);
6251 dtrace_dynvar_t
*dvar
;
6254 id
= DIF_INSTR_VAR(instr
);
6255 ASSERT(id
>= DIF_VAR_OTHER_UBASE
);
6256 id
-= DIF_VAR_OTHER_UBASE
;
6257 VERIFY(id
< (uint_t
)vstate
->dtvs_ntlocals
);
6259 key
= &tupregs
[DIF_DTR_NREGS
];
6260 key
[0].dttk_value
= (uint64_t)id
;
6261 key
[0].dttk_size
= 0;
6262 DTRACE_TLS_THRKEY(key
[1].dttk_value
);
6263 key
[1].dttk_size
= 0;
6264 v
= &vstate
->dtvs_tlocals
[id
];
6266 dvar
= dtrace_dynvar(dstate
, 2, key
,
6267 v
->dtdv_type
.dtdt_size
> sizeof (uint64_t) ?
6268 v
->dtdv_type
.dtdt_size
: sizeof (uint64_t),
6269 regs
[rd
] ? DTRACE_DYNVAR_ALLOC
:
6270 DTRACE_DYNVAR_DEALLOC
, mstate
, vstate
);
6273 * Given that we're storing to thread-local data,
6274 * we need to flush our predicate cache.
6276 dtrace_set_thread_predcache(current_thread(), 0);
6281 if (v
->dtdv_type
.dtdt_flags
& DIF_TF_BYREF
) {
6284 if (!dtrace_vcanload(
6285 (void *)(uintptr_t)regs
[rd
],
6286 &v
->dtdv_type
, &lim
, mstate
, vstate
))
6289 dtrace_vcopy((void *)(uintptr_t)regs
[rd
],
6290 dvar
->dtdv_data
, &v
->dtdv_type
, lim
);
6292 *((uint64_t *)dvar
->dtdv_data
) = regs
[rd
];
6299 regs
[rd
] = (int64_t)regs
[r1
] >> regs
[r2
];
6303 dtrace_dif_subr(DIF_INSTR_SUBR(instr
), rd
,
6304 regs
, tupregs
, ttop
, mstate
, state
);
6308 if (ttop
== DIF_DTR_NREGS
) {
6309 *flags
|= CPU_DTRACE_TUPOFLOW
;
6313 if (r1
== DIF_TYPE_STRING
) {
6315 * If this is a string type and the size is 0,
6316 * we'll use the system-wide default string
6317 * size. Note that we are _not_ looking at
6318 * the value of the DTRACEOPT_STRSIZE option;
6319 * had this been set, we would expect to have
6320 * a non-zero size value in the "pushtr".
6322 tupregs
[ttop
].dttk_size
=
6323 dtrace_strlen((char *)(uintptr_t)regs
[rd
],
6324 regs
[r2
] ? regs
[r2
] :
6325 dtrace_strsize_default
) + 1;
6327 if (regs
[r2
] > LONG_MAX
) {
6328 *flags
|= CPU_DTRACE_ILLOP
;
6331 tupregs
[ttop
].dttk_size
= regs
[r2
];
6334 tupregs
[ttop
++].dttk_value
= regs
[rd
];
6338 if (ttop
== DIF_DTR_NREGS
) {
6339 *flags
|= CPU_DTRACE_TUPOFLOW
;
6343 tupregs
[ttop
].dttk_value
= regs
[rd
];
6344 tupregs
[ttop
++].dttk_size
= 0;
6352 case DIF_OP_FLUSHTS
:
6357 case DIF_OP_LDTAA
: {
6358 dtrace_dynvar_t
*dvar
;
6359 dtrace_key_t
*key
= tupregs
;
6360 uint_t nkeys
= ttop
;
6362 id
= DIF_INSTR_VAR(instr
);
6363 ASSERT(id
>= DIF_VAR_OTHER_UBASE
);
6364 id
-= DIF_VAR_OTHER_UBASE
;
6366 key
[nkeys
].dttk_value
= (uint64_t)id
;
6367 key
[nkeys
++].dttk_size
= 0;
6369 if (DIF_INSTR_OP(instr
) == DIF_OP_LDTAA
) {
6370 DTRACE_TLS_THRKEY(key
[nkeys
].dttk_value
);
6371 key
[nkeys
++].dttk_size
= 0;
6372 VERIFY(id
< (uint_t
)vstate
->dtvs_ntlocals
);
6373 v
= &vstate
->dtvs_tlocals
[id
];
6375 VERIFY(id
< (uint_t
)vstate
->dtvs_nglobals
);
6376 v
= &vstate
->dtvs_globals
[id
]->dtsv_var
;
6379 dvar
= dtrace_dynvar(dstate
, nkeys
, key
,
6380 v
->dtdv_type
.dtdt_size
> sizeof (uint64_t) ?
6381 v
->dtdv_type
.dtdt_size
: sizeof (uint64_t),
6382 DTRACE_DYNVAR_NOALLOC
, mstate
, vstate
);
6389 if (v
->dtdv_type
.dtdt_flags
& DIF_TF_BYREF
) {
6390 regs
[rd
] = (uint64_t)(uintptr_t)dvar
->dtdv_data
;
6392 regs
[rd
] = *((uint64_t *)dvar
->dtdv_data
);
6399 case DIF_OP_STTAA
: {
6400 dtrace_dynvar_t
*dvar
;
6401 dtrace_key_t
*key
= tupregs
;
6402 uint_t nkeys
= ttop
;
6404 id
= DIF_INSTR_VAR(instr
);
6405 ASSERT(id
>= DIF_VAR_OTHER_UBASE
);
6406 id
-= DIF_VAR_OTHER_UBASE
;
6408 key
[nkeys
].dttk_value
= (uint64_t)id
;
6409 key
[nkeys
++].dttk_size
= 0;
6411 if (DIF_INSTR_OP(instr
) == DIF_OP_STTAA
) {
6412 DTRACE_TLS_THRKEY(key
[nkeys
].dttk_value
);
6413 key
[nkeys
++].dttk_size
= 0;
6414 VERIFY(id
< (uint_t
)vstate
->dtvs_ntlocals
);
6415 v
= &vstate
->dtvs_tlocals
[id
];
6417 VERIFY(id
< (uint_t
)vstate
->dtvs_nglobals
);
6418 v
= &vstate
->dtvs_globals
[id
]->dtsv_var
;
6421 dvar
= dtrace_dynvar(dstate
, nkeys
, key
,
6422 v
->dtdv_type
.dtdt_size
> sizeof (uint64_t) ?
6423 v
->dtdv_type
.dtdt_size
: sizeof (uint64_t),
6424 regs
[rd
] ? DTRACE_DYNVAR_ALLOC
:
6425 DTRACE_DYNVAR_DEALLOC
, mstate
, vstate
);
6430 if (v
->dtdv_type
.dtdt_flags
& DIF_TF_BYREF
) {
6433 if (!dtrace_vcanload(
6434 (void *)(uintptr_t)regs
[rd
], &v
->dtdv_type
,
6435 &lim
, mstate
, vstate
))
6438 dtrace_vcopy((void *)(uintptr_t)regs
[rd
],
6439 dvar
->dtdv_data
, &v
->dtdv_type
, lim
);
6441 *((uint64_t *)dvar
->dtdv_data
) = regs
[rd
];
6447 case DIF_OP_ALLOCS
: {
6448 uintptr_t ptr
= P2ROUNDUP(mstate
->dtms_scratch_ptr
, 8);
6449 size_t size
= ptr
- mstate
->dtms_scratch_ptr
+ regs
[r1
];
6452 * Rounding up the user allocation size could have
6453 * overflowed large, bogus allocations (like -1ULL) to
6456 if (size
< regs
[r1
] ||
6457 !DTRACE_INSCRATCH(mstate
, size
)) {
6458 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
6463 dtrace_bzero((void *) mstate
->dtms_scratch_ptr
, size
);
6464 mstate
->dtms_scratch_ptr
+= size
;
6470 if (!dtrace_canstore(regs
[rd
], regs
[r2
],
6472 *flags
|= CPU_DTRACE_BADADDR
;
6477 if (!dtrace_canload(regs
[r1
], regs
[r2
], mstate
, vstate
))
6480 dtrace_bcopy((void *)(uintptr_t)regs
[r1
],
6481 (void *)(uintptr_t)regs
[rd
], (size_t)regs
[r2
]);
6485 if (!dtrace_canstore(regs
[rd
], 1, mstate
, vstate
)) {
6486 *flags
|= CPU_DTRACE_BADADDR
;
6490 *((uint8_t *)(uintptr_t)regs
[rd
]) = (uint8_t)regs
[r1
];
6494 if (!dtrace_canstore(regs
[rd
], 2, mstate
, vstate
)) {
6495 *flags
|= CPU_DTRACE_BADADDR
;
6500 *flags
|= CPU_DTRACE_BADALIGN
;
6504 *((uint16_t *)(uintptr_t)regs
[rd
]) = (uint16_t)regs
[r1
];
6508 if (!dtrace_canstore(regs
[rd
], 4, mstate
, vstate
)) {
6509 *flags
|= CPU_DTRACE_BADADDR
;
6514 *flags
|= CPU_DTRACE_BADALIGN
;
6518 *((uint32_t *)(uintptr_t)regs
[rd
]) = (uint32_t)regs
[r1
];
6522 if (!dtrace_canstore(regs
[rd
], 8, mstate
, vstate
)) {
6523 *flags
|= CPU_DTRACE_BADADDR
;
6529 * Darwin kmem_zalloc() called from
6530 * dtrace_difo_init() is 4-byte aligned.
6533 *flags
|= CPU_DTRACE_BADALIGN
;
6537 *((uint64_t *)(uintptr_t)regs
[rd
]) = regs
[r1
];
6540 regs
[rd
] = (uint64_t)dtrace_ptrauth_strip(
6541 (void*)regs
[r1
], r2
);
6546 if (!(*flags
& CPU_DTRACE_FAULT
))
6549 mstate
->dtms_fltoffs
= opc
* sizeof (dif_instr_t
);
6550 mstate
->dtms_present
|= DTRACE_MSTATE_FLTOFFS
;
6555 __attribute__((noinline
))
6557 dtrace_action_breakpoint(dtrace_ecb_t
*ecb
)
6559 dtrace_probe_t
*probe
= ecb
->dte_probe
;
6560 dtrace_provider_t
*prov
= probe
->dtpr_provider
;
6561 char c
[DTRACE_FULLNAMELEN
+ 80], *str
;
6562 const char *msg
= "dtrace: breakpoint action at probe ";
6563 const char *ecbmsg
= " (ecb ";
6564 uintptr_t mask
= (0xf << (sizeof (uintptr_t) * NBBY
/ 4));
6565 uintptr_t val
= (uintptr_t)ecb
;
6566 int shift
= (sizeof (uintptr_t) * NBBY
) - 4, i
= 0;
6568 if (dtrace_destructive_disallow
)
6572 * It's impossible to be taking action on the NULL probe.
6574 ASSERT(probe
!= NULL
);
6577 * This is a poor man's (destitute man's?) sprintf(): we want to
6578 * print the provider name, module name, function name and name of
6579 * the probe, along with the hex address of the ECB with the breakpoint
6580 * action -- all of which we must place in the character buffer by
6583 while (*msg
!= '\0')
6586 for (str
= prov
->dtpv_name
; *str
!= '\0'; str
++)
6590 for (str
= probe
->dtpr_mod
; *str
!= '\0'; str
++)
6594 for (str
= probe
->dtpr_func
; *str
!= '\0'; str
++)
6598 for (str
= probe
->dtpr_name
; *str
!= '\0'; str
++)
6601 while (*ecbmsg
!= '\0')
6604 while (shift
>= 0) {
6605 mask
= (uintptr_t)0xf << shift
;
6607 if (val
>= ((uintptr_t)1 << shift
))
6608 c
[i
++] = "0123456789abcdef"[(val
& mask
) >> shift
];
6618 __attribute__((noinline
))
6620 dtrace_action_panic(dtrace_ecb_t
*ecb
)
6622 dtrace_probe_t
*probe
= ecb
->dte_probe
;
6625 * It's impossible to be taking action on the NULL probe.
6627 ASSERT(probe
!= NULL
);
6629 if (dtrace_destructive_disallow
)
6632 if (dtrace_panicked
!= NULL
)
6635 if (dtrace_casptr(&dtrace_panicked
, NULL
, current_thread()) != NULL
)
6639 * We won the right to panic. (We want to be sure that only one
6640 * thread calls panic() from dtrace_probe(), and that panic() is
6641 * called exactly once.)
6643 panic("dtrace: panic action at probe %s:%s:%s:%s (ecb %p)",
6644 probe
->dtpr_provider
->dtpv_name
, probe
->dtpr_mod
,
6645 probe
->dtpr_func
, probe
->dtpr_name
, (void *)ecb
);
6648 * APPLE NOTE: this was for an old Mac OS X debug feature
6649 * allowing a return from panic(). Revisit someday.
6651 dtrace_panicked
= NULL
;
6655 dtrace_action_raise(uint64_t sig
)
6657 if (dtrace_destructive_disallow
)
6661 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
6666 * raise() has a queue depth of 1 -- we ignore all subsequent
6667 * invocations of the raise() action.
6670 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
6672 if (uthread
&& uthread
->t_dtrace_sig
== 0) {
6673 uthread
->t_dtrace_sig
= sig
;
6674 act_set_astbsd(current_thread());
6679 dtrace_action_stop(void)
6681 if (dtrace_destructive_disallow
)
6684 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
6687 * The currently running process will be set to task_suspend
6688 * when it next leaves the kernel.
6690 uthread
->t_dtrace_stop
= 1;
6691 act_set_astbsd(current_thread());
6697 * APPLE NOTE: pidresume works in conjunction with the dtrace stop action.
6698 * Both activate only when the currently running process next leaves the
6702 dtrace_action_pidresume(uint64_t pid
)
6704 if (dtrace_destructive_disallow
)
6707 if (kauth_cred_issuser(kauth_cred_get()) == 0) {
6708 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
6711 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
6714 * When the currently running process leaves the kernel, it attempts to
6715 * task_resume the process (denoted by pid), if that pid appears to have
6716 * been stopped by dtrace_action_stop().
6717 * The currently running process has a pidresume() queue depth of 1 --
6718 * subsequent invocations of the pidresume() action are ignored.
6721 if (pid
!= 0 && uthread
&& uthread
->t_dtrace_resumepid
== 0) {
6722 uthread
->t_dtrace_resumepid
= pid
;
6723 act_set_astbsd(current_thread());
6727 __attribute__((noinline
))
6729 dtrace_action_chill(dtrace_mstate_t
*mstate
, hrtime_t val
)
6732 volatile uint16_t *flags
;
6733 dtrace_cpu_t
*cpu
= CPU
;
6735 if (dtrace_destructive_disallow
)
6738 flags
= (volatile uint16_t *)&cpu_core
[cpu
->cpu_id
].cpuc_dtrace_flags
;
6740 now
= dtrace_gethrtime();
6742 if (now
- cpu
->cpu_dtrace_chillmark
> dtrace_chill_interval
) {
6744 * We need to advance the mark to the current time.
6746 cpu
->cpu_dtrace_chillmark
= now
;
6747 cpu
->cpu_dtrace_chilled
= 0;
6751 * Now check to see if the requested chill time would take us over
6752 * the maximum amount of time allowed in the chill interval. (Or
6753 * worse, if the calculation itself induces overflow.)
6755 if (cpu
->cpu_dtrace_chilled
+ val
> dtrace_chill_max
||
6756 cpu
->cpu_dtrace_chilled
+ val
< cpu
->cpu_dtrace_chilled
) {
6757 *flags
|= CPU_DTRACE_ILLOP
;
6761 while (dtrace_gethrtime() - now
< val
)
6765 * Normally, we assure that the value of the variable "timestamp" does
6766 * not change within an ECB. The presence of chill() represents an
6767 * exception to this rule, however.
6769 mstate
->dtms_present
&= ~DTRACE_MSTATE_TIMESTAMP
;
6770 cpu
->cpu_dtrace_chilled
+= val
;
6773 __attribute__((noinline
))
6775 dtrace_action_ustack(dtrace_mstate_t
*mstate
, dtrace_state_t
*state
,
6776 uint64_t *buf
, uint64_t arg
)
6778 int nframes
= DTRACE_USTACK_NFRAMES(arg
);
6779 int strsize
= DTRACE_USTACK_STRSIZE(arg
);
6780 uint64_t *pcs
= &buf
[1], *fps
;
6781 char *str
= (char *)&pcs
[nframes
];
6782 int size
, offs
= 0, i
, j
;
6783 uintptr_t old
= mstate
->dtms_scratch_ptr
, saved
;
6784 uint16_t *flags
= &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
6788 * Should be taking a faster path if string space has not been
6791 ASSERT(strsize
!= 0);
6794 * We will first allocate some temporary space for the frame pointers.
6796 fps
= (uint64_t *)P2ROUNDUP(mstate
->dtms_scratch_ptr
, 8);
6797 size
= (uintptr_t)fps
- mstate
->dtms_scratch_ptr
+
6798 (nframes
* sizeof (uint64_t));
6800 if (!DTRACE_INSCRATCH(mstate
, (uintptr_t)size
)) {
6802 * Not enough room for our frame pointers -- need to indicate
6803 * that we ran out of scratch space.
6805 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH
);
6809 mstate
->dtms_scratch_ptr
+= size
;
6810 saved
= mstate
->dtms_scratch_ptr
;
6813 * Now get a stack with both program counters and frame pointers.
6815 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
6816 dtrace_getufpstack(buf
, fps
, nframes
+ 1);
6817 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
6820 * If that faulted, we're cooked.
6822 if (*flags
& CPU_DTRACE_FAULT
)
6826 * Now we want to walk up the stack, calling the USTACK helper. For
6827 * each iteration, we restore the scratch pointer.
6829 for (i
= 0; i
< nframes
; i
++) {
6830 mstate
->dtms_scratch_ptr
= saved
;
6832 if (offs
>= strsize
)
6835 sym
= (char *)(uintptr_t)dtrace_helper(
6836 DTRACE_HELPER_ACTION_USTACK
,
6837 mstate
, state
, pcs
[i
], fps
[i
]);
6840 * If we faulted while running the helper, we're going to
6841 * clear the fault and null out the corresponding string.
6843 if (*flags
& CPU_DTRACE_FAULT
) {
6844 *flags
&= ~CPU_DTRACE_FAULT
;
6854 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
6857 * Now copy in the string that the helper returned to us.
6859 for (j
= 0; offs
+ j
< strsize
; j
++) {
6860 if ((str
[offs
+ j
] = sym
[j
]) == '\0')
6864 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
6869 if (offs
>= strsize
) {
6871 * If we didn't have room for all of the strings, we don't
6872 * abort processing -- this needn't be a fatal error -- but we
6873 * still want to increment a counter (dts_stkstroverflows) to
6874 * allow this condition to be warned about. (If this is from
6875 * a jstack() action, it is easily tuned via jstackstrsize.)
6877 dtrace_error(&state
->dts_stkstroverflows
);
6880 while (offs
< strsize
)
6884 mstate
->dtms_scratch_ptr
= old
;
6887 __attribute__((noinline
))
6889 dtrace_store_by_ref(dtrace_difo_t
*dp
, caddr_t tomax
, size_t size
,
6890 size_t *valoffsp
, uint64_t *valp
, uint64_t end
, int intuple
, int dtkind
)
6892 volatile uint16_t *flags
;
6893 uint64_t val
= *valp
;
6894 size_t valoffs
= *valoffsp
;
6896 flags
= (volatile uint16_t *)&cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
6897 ASSERT(dtkind
== DIF_TF_BYREF
|| dtkind
== DIF_TF_BYUREF
);
6900 * If this is a string, we're going to only load until we find the zero
6901 * byte -- after which we'll store zero bytes.
6903 if (dp
->dtdo_rtype
.dtdt_kind
== DIF_TYPE_STRING
) {
6907 for (s
= 0; s
< size
; s
++) {
6908 if (c
!= '\0' && dtkind
== DIF_TF_BYREF
) {
6909 c
= dtrace_load8(val
++);
6910 } else if (c
!= '\0' && dtkind
== DIF_TF_BYUREF
) {
6911 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
6912 c
= dtrace_fuword8((user_addr_t
)(uintptr_t)val
++);
6913 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
6914 if (*flags
& CPU_DTRACE_FAULT
)
6918 DTRACE_STORE(uint8_t, tomax
, valoffs
++, c
);
6920 if (c
== '\0' && intuple
)
6925 while (valoffs
< end
) {
6926 if (dtkind
== DIF_TF_BYREF
) {
6927 c
= dtrace_load8(val
++);
6928 } else if (dtkind
== DIF_TF_BYUREF
) {
6929 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
6930 c
= dtrace_fuword8((user_addr_t
)(uintptr_t)val
++);
6931 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
6932 if (*flags
& CPU_DTRACE_FAULT
)
6936 DTRACE_STORE(uint8_t, tomax
,
6942 *valoffsp
= valoffs
;
6946 * Disables interrupts and sets the per-thread inprobe flag. When DEBUG is
6947 * defined, we also assert that we are not recursing unless the probe ID is an
6950 static dtrace_icookie_t
6951 dtrace_probe_enter(dtrace_id_t id
)
6953 thread_t thread
= current_thread();
6956 dtrace_icookie_t cookie
;
6958 cookie
= dtrace_interrupt_disable();
6961 * Unless this is an ERROR probe, we are not allowed to recurse in
6962 * dtrace_probe(). Recursing into DTrace probe usually means that a
6963 * function is instrumented that should not have been instrumented or
6964 * that the ordering guarantee of the records will be violated,
6965 * resulting in unexpected output. If there is an exception to this
6966 * assertion, a new case should be added.
6968 inprobe
= dtrace_get_thread_inprobe(thread
);
6969 VERIFY(inprobe
== 0 ||
6970 id
== dtrace_probeid_error
);
6971 ASSERT(inprobe
< UINT16_MAX
);
6972 dtrace_set_thread_inprobe(thread
, inprobe
+ 1);
6978 * Clears the per-thread inprobe flag and enables interrupts.
6981 dtrace_probe_exit(dtrace_icookie_t cookie
)
6983 thread_t thread
= current_thread();
6984 uint16_t inprobe
= dtrace_get_thread_inprobe(thread
);
6986 ASSERT(inprobe
> 0);
6987 dtrace_set_thread_inprobe(thread
, inprobe
- 1);
6989 #if INTERRUPT_MASKED_DEBUG
6990 ml_spin_debug_reset(thread
);
6991 #endif /* INTERRUPT_MASKED_DEBUG */
6993 dtrace_interrupt_enable(cookie
);
6997 * If you're looking for the epicenter of DTrace, you just found it. This
6998 * is the function called by the provider to fire a probe -- from which all
6999 * subsequent probe-context DTrace activity emanates.
7002 dtrace_probe(dtrace_id_t id
, uint64_t arg0
, uint64_t arg1
,
7003 uint64_t arg2
, uint64_t arg3
, uint64_t arg4
)
7005 processorid_t cpuid
;
7006 dtrace_icookie_t cookie
;
7007 dtrace_probe_t
*probe
;
7008 dtrace_mstate_t mstate
;
7010 dtrace_action_t
*act
;
7014 volatile uint16_t *flags
;
7017 cookie
= dtrace_probe_enter(id
);
7019 /* Ensure that probe id is valid. */
7020 if (id
- 1 >= (dtrace_id_t
)dtrace_nprobes
) {
7021 dtrace_probe_exit(cookie
);
7025 probe
= dtrace_probes
[id
- 1];
7026 if (probe
== NULL
) {
7027 dtrace_probe_exit(cookie
);
7031 cpuid
= CPU
->cpu_id
;
7032 onintr
= CPU_ON_INTR(CPU
);
7034 if (!onintr
&& probe
->dtpr_predcache
!= DTRACE_CACHEIDNONE
&&
7035 probe
->dtpr_predcache
== dtrace_get_thread_predcache(current_thread())) {
7037 * We have hit in the predicate cache; we know that
7038 * this predicate would evaluate to be false.
7040 dtrace_probe_exit(cookie
);
7044 if (panic_quiesce
) {
7046 * We don't trace anything if we're panicking.
7048 dtrace_probe_exit(cookie
);
7052 #if !defined(__APPLE__)
7053 now
= dtrace_gethrtime();
7054 vtime
= dtrace_vtime_references
!= 0;
7056 if (vtime
&& curthread
->t_dtrace_start
)
7057 curthread
->t_dtrace_vtime
+= now
- curthread
->t_dtrace_start
;
7060 * APPLE NOTE: The time spent entering DTrace and arriving
7061 * to this point, is attributed to the current thread.
7062 * Instead it should accrue to DTrace. FIXME
7064 vtime
= dtrace_vtime_references
!= 0;
7068 int64_t dtrace_accum_time
, recent_vtime
;
7069 thread_t thread
= current_thread();
7071 dtrace_accum_time
= dtrace_get_thread_tracing(thread
); /* Time spent inside DTrace so far (nanoseconds) */
7073 if (dtrace_accum_time
>= 0) {
7074 recent_vtime
= dtrace_abs_to_nano(dtrace_calc_thread_recent_vtime(thread
)); /* up to the moment thread vtime */
7076 recent_vtime
= recent_vtime
- dtrace_accum_time
; /* Time without DTrace contribution */
7078 dtrace_set_thread_vtime(thread
, recent_vtime
);
7082 now
= dtrace_gethrtime(); /* must not precede dtrace_calc_thread_recent_vtime() call! */
7083 #endif /* __APPLE__ */
7086 * APPLE NOTE: A provider may call dtrace_probe_error() in lieu of
7087 * dtrace_probe() in some circumstances. See, e.g. fasttrap_isa.c.
7088 * However the provider has no access to ECB context, so passes
7089 * 0 through "arg0" and the probe_id of the overridden probe as arg1.
7090 * Detect that here and cons up a viable state (from the probe_id).
7092 if (dtrace_probeid_error
== id
&& 0 == arg0
) {
7093 dtrace_id_t ftp_id
= (dtrace_id_t
)arg1
;
7094 dtrace_probe_t
*ftp_probe
= dtrace_probes
[ftp_id
- 1];
7095 dtrace_ecb_t
*ftp_ecb
= ftp_probe
->dtpr_ecb
;
7097 if (NULL
!= ftp_ecb
) {
7098 dtrace_state_t
*ftp_state
= ftp_ecb
->dte_state
;
7100 arg0
= (uint64_t)(uintptr_t)ftp_state
;
7101 arg1
= ftp_ecb
->dte_epid
;
7103 * args[2-4] established by caller.
7105 ftp_state
->dts_arg_error_illval
= -1; /* arg5 */
7109 mstate
.dtms_difo
= NULL
;
7110 mstate
.dtms_probe
= probe
;
7111 mstate
.dtms_strtok
= 0;
7112 mstate
.dtms_arg
[0] = arg0
;
7113 mstate
.dtms_arg
[1] = arg1
;
7114 mstate
.dtms_arg
[2] = arg2
;
7115 mstate
.dtms_arg
[3] = arg3
;
7116 mstate
.dtms_arg
[4] = arg4
;
7118 flags
= (volatile uint16_t *)&cpu_core
[cpuid
].cpuc_dtrace_flags
;
7120 for (ecb
= probe
->dtpr_ecb
; ecb
!= NULL
; ecb
= ecb
->dte_next
) {
7121 dtrace_predicate_t
*pred
= ecb
->dte_predicate
;
7122 dtrace_state_t
*state
= ecb
->dte_state
;
7123 dtrace_buffer_t
*buf
= &state
->dts_buffer
[cpuid
];
7124 dtrace_buffer_t
*aggbuf
= &state
->dts_aggbuffer
[cpuid
];
7125 dtrace_vstate_t
*vstate
= &state
->dts_vstate
;
7126 dtrace_provider_t
*prov
= probe
->dtpr_provider
;
7127 uint64_t tracememsize
= 0;
7132 * A little subtlety with the following (seemingly innocuous)
7133 * declaration of the automatic 'val': by looking at the
7134 * code, you might think that it could be declared in the
7135 * action processing loop, below. (That is, it's only used in
7136 * the action processing loop.) However, it must be declared
7137 * out of that scope because in the case of DIF expression
7138 * arguments to aggregating actions, one iteration of the
7139 * action loop will use the last iteration's value.
7147 mstate
.dtms_present
= DTRACE_MSTATE_ARGS
| DTRACE_MSTATE_PROBE
;
7148 *flags
&= ~CPU_DTRACE_ERROR
;
7150 if (prov
== dtrace_provider
) {
7152 * If dtrace itself is the provider of this probe,
7153 * we're only going to continue processing the ECB if
7154 * arg0 (the dtrace_state_t) is equal to the ECB's
7155 * creating state. (This prevents disjoint consumers
7156 * from seeing one another's metaprobes.)
7158 if (arg0
!= (uint64_t)(uintptr_t)state
)
7162 if (state
->dts_activity
!= DTRACE_ACTIVITY_ACTIVE
) {
7164 * We're not currently active. If our provider isn't
7165 * the dtrace pseudo provider, we're not interested.
7167 if (prov
!= dtrace_provider
)
7171 * Now we must further check if we are in the BEGIN
7172 * probe. If we are, we will only continue processing
7173 * if we're still in WARMUP -- if one BEGIN enabling
7174 * has invoked the exit() action, we don't want to
7175 * evaluate subsequent BEGIN enablings.
7177 if (probe
->dtpr_id
== dtrace_probeid_begin
&&
7178 state
->dts_activity
!= DTRACE_ACTIVITY_WARMUP
) {
7179 ASSERT(state
->dts_activity
==
7180 DTRACE_ACTIVITY_DRAINING
);
7185 if (ecb
->dte_cond
) {
7187 * If the dte_cond bits indicate that this
7188 * consumer is only allowed to see user-mode firings
7189 * of this probe, call the provider's dtps_usermode()
7190 * entry point to check that the probe was fired
7191 * while in a user context. Skip this ECB if that's
7194 if ((ecb
->dte_cond
& DTRACE_COND_USERMODE
) &&
7195 prov
->dtpv_pops
.dtps_usermode
&&
7196 prov
->dtpv_pops
.dtps_usermode(prov
->dtpv_arg
,
7197 probe
->dtpr_id
, probe
->dtpr_arg
) == 0)
7201 * This is more subtle than it looks. We have to be
7202 * absolutely certain that CRED() isn't going to
7203 * change out from under us so it's only legit to
7204 * examine that structure if we're in constrained
7205 * situations. Currently, the only times we'll this
7206 * check is if a non-super-user has enabled the
7207 * profile or syscall providers -- providers that
7208 * allow visibility of all processes. For the
7209 * profile case, the check above will ensure that
7210 * we're examining a user context.
7212 if (ecb
->dte_cond
& DTRACE_COND_OWNER
) {
7215 ecb
->dte_state
->dts_cred
.dcr_cred
;
7217 #pragma unused(proc) /* __APPLE__ */
7219 ASSERT(s_cr
!= NULL
);
7222 * XXX this is hackish, but so is setting a variable
7223 * XXX in a McCarthy OR...
7225 if ((cr
= dtrace_CRED()) == NULL
||
7226 posix_cred_get(s_cr
)->cr_uid
!= posix_cred_get(cr
)->cr_uid
||
7227 posix_cred_get(s_cr
)->cr_uid
!= posix_cred_get(cr
)->cr_ruid
||
7228 posix_cred_get(s_cr
)->cr_uid
!= posix_cred_get(cr
)->cr_suid
||
7229 posix_cred_get(s_cr
)->cr_gid
!= posix_cred_get(cr
)->cr_gid
||
7230 posix_cred_get(s_cr
)->cr_gid
!= posix_cred_get(cr
)->cr_rgid
||
7231 posix_cred_get(s_cr
)->cr_gid
!= posix_cred_get(cr
)->cr_sgid
||
7232 #if !defined(__APPLE__)
7233 (proc
= ttoproc(curthread
)) == NULL
||
7234 (proc
->p_flag
& SNOCD
))
7236 1) /* APPLE NOTE: Darwin omits "No Core Dump" flag */
7237 #endif /* __APPLE__ */
7241 if (ecb
->dte_cond
& DTRACE_COND_ZONEOWNER
) {
7244 ecb
->dte_state
->dts_cred
.dcr_cred
;
7245 #pragma unused(cr, s_cr) /* __APPLE__ */
7247 ASSERT(s_cr
!= NULL
);
7249 #if !defined(__APPLE__)
7250 if ((cr
= CRED()) == NULL
||
7251 s_cr
->cr_zone
->zone_id
!=
7252 cr
->cr_zone
->zone_id
)
7255 /* APPLE NOTE: Darwin doesn't do zones. */
7256 #endif /* __APPLE__ */
7260 if (now
- state
->dts_alive
> dtrace_deadman_timeout
) {
7262 * We seem to be dead. Unless we (a) have kernel
7263 * destructive permissions (b) have expicitly enabled
7264 * destructive actions and (c) destructive actions have
7265 * not been disabled, we're going to transition into
7266 * the KILLED state, from which no further processing
7267 * on this state will be performed.
7269 if (!dtrace_priv_kernel_destructive(state
) ||
7270 !state
->dts_cred
.dcr_destructive
||
7271 dtrace_destructive_disallow
) {
7272 void *activity
= &state
->dts_activity
;
7273 dtrace_activity_t current
;
7276 current
= state
->dts_activity
;
7277 } while (dtrace_cas32(activity
, current
,
7278 DTRACE_ACTIVITY_KILLED
) != current
);
7284 if ((offs
= dtrace_buffer_reserve(buf
, ecb
->dte_needed
,
7285 ecb
->dte_alignment
, state
, &mstate
)) < 0)
7288 tomax
= buf
->dtb_tomax
;
7289 ASSERT(tomax
!= NULL
);
7292 * Build and store the record header corresponding to the ECB.
7294 if (ecb
->dte_size
!= 0) {
7295 dtrace_rechdr_t dtrh
;
7297 if (!(mstate
.dtms_present
& DTRACE_MSTATE_TIMESTAMP
)) {
7298 mstate
.dtms_timestamp
= dtrace_gethrtime();
7299 mstate
.dtms_present
|= DTRACE_MSTATE_TIMESTAMP
;
7302 ASSERT(ecb
->dte_size
>= sizeof(dtrace_rechdr_t
));
7304 dtrh
.dtrh_epid
= ecb
->dte_epid
;
7305 DTRACE_RECORD_STORE_TIMESTAMP(&dtrh
, mstate
.dtms_timestamp
);
7306 DTRACE_STORE(dtrace_rechdr_t
, tomax
, offs
, dtrh
);
7309 mstate
.dtms_epid
= ecb
->dte_epid
;
7310 mstate
.dtms_present
|= DTRACE_MSTATE_EPID
;
7312 if (state
->dts_cred
.dcr_visible
& DTRACE_CRV_KERNEL
)
7313 mstate
.dtms_access
= DTRACE_ACCESS_KERNEL
;
7315 mstate
.dtms_access
= 0;
7318 dtrace_difo_t
*dp
= pred
->dtp_difo
;
7321 rval
= dtrace_dif_emulate(dp
, &mstate
, vstate
, state
);
7323 if (!(*flags
& CPU_DTRACE_ERROR
) && !rval
) {
7324 dtrace_cacheid_t cid
= probe
->dtpr_predcache
;
7326 if (cid
!= DTRACE_CACHEIDNONE
&& !onintr
) {
7328 * Update the predicate cache...
7330 ASSERT(cid
== pred
->dtp_cacheid
);
7332 dtrace_set_thread_predcache(current_thread(), cid
);
7339 for (act
= ecb
->dte_action
; !(*flags
& CPU_DTRACE_ERROR
) &&
7340 act
!= NULL
; act
= act
->dta_next
) {
7343 dtrace_recdesc_t
*rec
= &act
->dta_rec
;
7345 size
= rec
->dtrd_size
;
7346 valoffs
= offs
+ rec
->dtrd_offset
;
7348 if (DTRACEACT_ISAGG(act
->dta_kind
)) {
7350 dtrace_aggregation_t
*agg
;
7352 agg
= (dtrace_aggregation_t
*)act
;
7354 if ((dp
= act
->dta_difo
) != NULL
)
7355 v
= dtrace_dif_emulate(dp
,
7356 &mstate
, vstate
, state
);
7358 if (*flags
& CPU_DTRACE_ERROR
)
7362 * Note that we always pass the expression
7363 * value from the previous iteration of the
7364 * action loop. This value will only be used
7365 * if there is an expression argument to the
7366 * aggregating action, denoted by the
7367 * dtag_hasarg field.
7369 dtrace_aggregate(agg
, buf
,
7370 offs
, aggbuf
, v
, val
);
7374 switch (act
->dta_kind
) {
7375 case DTRACEACT_STOP
:
7376 if (dtrace_priv_proc_destructive(state
))
7377 dtrace_action_stop();
7380 case DTRACEACT_BREAKPOINT
:
7381 if (dtrace_priv_kernel_destructive(state
))
7382 dtrace_action_breakpoint(ecb
);
7385 case DTRACEACT_PANIC
:
7386 if (dtrace_priv_kernel_destructive(state
))
7387 dtrace_action_panic(ecb
);
7390 case DTRACEACT_STACK
:
7391 if (!dtrace_priv_kernel(state
))
7394 dtrace_getpcstack((pc_t
*)(tomax
+ valoffs
),
7395 size
/ sizeof (pc_t
), probe
->dtpr_aframes
,
7396 DTRACE_ANCHORED(probe
) ? NULL
:
7397 (uint32_t *)(uintptr_t)arg0
);
7400 case DTRACEACT_JSTACK
:
7401 case DTRACEACT_USTACK
:
7402 if (!dtrace_priv_proc(state
))
7406 * See comment in DIF_VAR_PID.
7408 if (DTRACE_ANCHORED(mstate
.dtms_probe
) &&
7410 int depth
= DTRACE_USTACK_NFRAMES(
7413 dtrace_bzero((void *)(tomax
+ valoffs
),
7414 DTRACE_USTACK_STRSIZE(rec
->dtrd_arg
)
7415 + depth
* sizeof (uint64_t));
7420 if (DTRACE_USTACK_STRSIZE(rec
->dtrd_arg
) != 0 &&
7421 curproc
->p_dtrace_helpers
!= NULL
) {
7423 * This is the slow path -- we have
7424 * allocated string space, and we're
7425 * getting the stack of a process that
7426 * has helpers. Call into a separate
7427 * routine to perform this processing.
7429 dtrace_action_ustack(&mstate
, state
,
7430 (uint64_t *)(tomax
+ valoffs
),
7435 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
7436 dtrace_getupcstack((uint64_t *)
7438 DTRACE_USTACK_NFRAMES(rec
->dtrd_arg
) + 1);
7439 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
7449 val
= dtrace_dif_emulate(dp
, &mstate
, vstate
, state
);
7451 if (*flags
& CPU_DTRACE_ERROR
)
7454 switch (act
->dta_kind
) {
7455 case DTRACEACT_SPECULATE
: {
7456 dtrace_rechdr_t
*dtrh
= NULL
;
7458 ASSERT(buf
== &state
->dts_buffer
[cpuid
]);
7459 buf
= dtrace_speculation_buffer(state
,
7463 *flags
|= CPU_DTRACE_DROP
;
7467 offs
= dtrace_buffer_reserve(buf
,
7468 ecb
->dte_needed
, ecb
->dte_alignment
,
7472 *flags
|= CPU_DTRACE_DROP
;
7476 tomax
= buf
->dtb_tomax
;
7477 ASSERT(tomax
!= NULL
);
7479 if (ecb
->dte_size
== 0)
7482 ASSERT(ecb
->dte_size
>= sizeof(dtrace_rechdr_t
));
7483 dtrh
= ((void *)(tomax
+ offs
));
7484 dtrh
->dtrh_epid
= ecb
->dte_epid
;
7487 * When the speculation is committed, all of
7488 * the records in the speculative buffer will
7489 * have their timestamps set to the commit
7490 * time. Until then, it is set to a sentinel
7491 * value, for debugability.
7493 DTRACE_RECORD_STORE_TIMESTAMP(dtrh
, UINT64_MAX
);
7498 case DTRACEACT_CHILL
:
7499 if (dtrace_priv_kernel_destructive(state
))
7500 dtrace_action_chill(&mstate
, val
);
7503 case DTRACEACT_RAISE
:
7504 if (dtrace_priv_proc_destructive(state
))
7505 dtrace_action_raise(val
);
7508 case DTRACEACT_PIDRESUME
: /* __APPLE__ */
7509 if (dtrace_priv_proc_destructive(state
))
7510 dtrace_action_pidresume(val
);
7513 case DTRACEACT_COMMIT
:
7517 * We need to commit our buffer state.
7520 buf
->dtb_offset
= offs
+ ecb
->dte_size
;
7521 buf
= &state
->dts_buffer
[cpuid
];
7522 dtrace_speculation_commit(state
, cpuid
, val
);
7526 case DTRACEACT_DISCARD
:
7527 dtrace_speculation_discard(state
, cpuid
, val
);
7530 case DTRACEACT_DIFEXPR
:
7531 case DTRACEACT_LIBACT
:
7532 case DTRACEACT_PRINTF
:
7533 case DTRACEACT_PRINTA
:
7534 case DTRACEACT_SYSTEM
:
7535 case DTRACEACT_FREOPEN
:
7536 case DTRACEACT_APPLEBINARY
: /* __APPLE__ */
7537 case DTRACEACT_TRACEMEM
:
7540 case DTRACEACT_TRACEMEM_DYNSIZE
:
7546 if (!dtrace_priv_kernel(state
))
7550 case DTRACEACT_USYM
:
7551 case DTRACEACT_UMOD
:
7552 case DTRACEACT_UADDR
: {
7553 if (!dtrace_priv_proc(state
))
7556 DTRACE_STORE(uint64_t, tomax
,
7557 valoffs
, (uint64_t)dtrace_proc_selfpid());
7558 DTRACE_STORE(uint64_t, tomax
,
7559 valoffs
+ sizeof (uint64_t), val
);
7564 case DTRACEACT_EXIT
: {
7566 * For the exit action, we are going to attempt
7567 * to atomically set our activity to be
7568 * draining. If this fails (either because
7569 * another CPU has beat us to the exit action,
7570 * or because our current activity is something
7571 * other than ACTIVE or WARMUP), we will
7572 * continue. This assures that the exit action
7573 * can be successfully recorded at most once
7574 * when we're in the ACTIVE state. If we're
7575 * encountering the exit() action while in
7576 * COOLDOWN, however, we want to honor the new
7577 * status code. (We know that we're the only
7578 * thread in COOLDOWN, so there is no race.)
7580 void *activity
= &state
->dts_activity
;
7581 dtrace_activity_t current
= state
->dts_activity
;
7583 if (current
== DTRACE_ACTIVITY_COOLDOWN
)
7586 if (current
!= DTRACE_ACTIVITY_WARMUP
)
7587 current
= DTRACE_ACTIVITY_ACTIVE
;
7589 if (dtrace_cas32(activity
, current
,
7590 DTRACE_ACTIVITY_DRAINING
) != current
) {
7591 *flags
|= CPU_DTRACE_DROP
;
7602 if (dp
->dtdo_rtype
.dtdt_flags
& (DIF_TF_BYREF
| DIF_TF_BYUREF
)) {
7603 uintptr_t end
= valoffs
+ size
;
7605 if (tracememsize
!= 0 &&
7606 valoffs
+ tracememsize
< end
)
7608 end
= valoffs
+ tracememsize
;
7612 if (dp
->dtdo_rtype
.dtdt_flags
& DIF_TF_BYREF
&&
7613 !dtrace_vcanload((void *)(uintptr_t)val
,
7614 &dp
->dtdo_rtype
, NULL
, &mstate
, vstate
))
7619 dtrace_store_by_ref(dp
, tomax
, size
, &valoffs
,
7620 &val
, end
, act
->dta_intuple
,
7621 dp
->dtdo_rtype
.dtdt_flags
& DIF_TF_BYREF
?
7622 DIF_TF_BYREF
: DIF_TF_BYUREF
);
7631 case sizeof (uint8_t):
7632 DTRACE_STORE(uint8_t, tomax
, valoffs
, val
);
7634 case sizeof (uint16_t):
7635 DTRACE_STORE(uint16_t, tomax
, valoffs
, val
);
7637 case sizeof (uint32_t):
7638 DTRACE_STORE(uint32_t, tomax
, valoffs
, val
);
7640 case sizeof (uint64_t):
7641 DTRACE_STORE(uint64_t, tomax
, valoffs
, val
);
7645 * Any other size should have been returned by
7646 * reference, not by value.
7653 if (*flags
& CPU_DTRACE_DROP
)
7656 if (*flags
& CPU_DTRACE_FAULT
) {
7658 dtrace_action_t
*err
;
7662 if (probe
->dtpr_id
== dtrace_probeid_error
) {
7664 * There's nothing we can do -- we had an
7665 * error on the error probe. We bump an
7666 * error counter to at least indicate that
7667 * this condition happened.
7669 dtrace_error(&state
->dts_dblerrors
);
7675 * Before recursing on dtrace_probe(), we
7676 * need to explicitly clear out our start
7677 * time to prevent it from being accumulated
7678 * into t_dtrace_vtime.
7682 * Darwin sets the sign bit on t_dtrace_tracing
7683 * to suspend accumulation to it.
7685 dtrace_set_thread_tracing(current_thread(),
7686 (1ULL<<63) | dtrace_get_thread_tracing(current_thread()));
7691 * Iterate over the actions to figure out which action
7692 * we were processing when we experienced the error.
7693 * Note that act points _past_ the faulting action; if
7694 * act is ecb->dte_action, the fault was in the
7695 * predicate, if it's ecb->dte_action->dta_next it's
7696 * in action #1, and so on.
7698 for (err
= ecb
->dte_action
, ndx
= 0;
7699 err
!= act
; err
= err
->dta_next
, ndx
++)
7702 dtrace_probe_error(state
, ecb
->dte_epid
, ndx
,
7703 (mstate
.dtms_present
& DTRACE_MSTATE_FLTOFFS
) ?
7704 mstate
.dtms_fltoffs
: -1, DTRACE_FLAGS2FLT(*flags
),
7705 cpu_core
[cpuid
].cpuc_dtrace_illval
);
7711 buf
->dtb_offset
= offs
+ ecb
->dte_size
;
7714 /* FIXME: On Darwin the time spent leaving DTrace from this point to the rti is attributed
7715 to the current thread. Instead it should accrue to DTrace. */
7717 thread_t thread
= current_thread();
7718 int64_t t
= dtrace_get_thread_tracing(thread
);
7721 /* Usual case, accumulate time spent here into t_dtrace_tracing */
7722 dtrace_set_thread_tracing(thread
, t
+ (dtrace_gethrtime() - now
));
7724 /* Return from error recursion. No accumulation, just clear the sign bit on t_dtrace_tracing. */
7725 dtrace_set_thread_tracing(thread
, (~(1ULL<<63)) & t
);
7729 dtrace_probe_exit(cookie
);
7733 * DTrace Probe Hashing Functions
7735 * The functions in this section (and indeed, the functions in remaining
7736 * sections) are not _called_ from probe context. (Any exceptions to this are
7737 * marked with a "Note:".) Rather, they are called from elsewhere in the
7738 * DTrace framework to look-up probes in, add probes to and remove probes from
7739 * the DTrace probe hashes. (Each probe is hashed by each element of the
7740 * probe tuple -- allowing for fast lookups, regardless of what was
7744 dtrace_hash_str(const char *p
)
7750 hval
= (hval
<< 4) + *p
++;
7751 if ((g
= (hval
& 0xf0000000)) != 0)
7759 dtrace_strkey_probe_provider(void *elm
, uintptr_t offs
)
7761 #pragma unused(offs)
7762 dtrace_probe_t
*probe
= (dtrace_probe_t
*)elm
;
7763 return probe
->dtpr_provider
->dtpv_name
;
7767 dtrace_strkey_offset(void *elm
, uintptr_t offs
)
7769 return ((char *)((uintptr_t)(elm
) + offs
));
7773 dtrace_strkey_deref_offset(void *elm
, uintptr_t offs
)
7775 return *((char **)((uintptr_t)(elm
) + offs
));
7778 static dtrace_hash_t
*
7779 dtrace_hash_create(dtrace_strkey_f func
, uintptr_t arg
, uintptr_t nextoffs
, uintptr_t prevoffs
)
7781 dtrace_hash_t
*hash
= kmem_zalloc(sizeof (dtrace_hash_t
), KM_SLEEP
);
7783 hash
->dth_getstr
= func
;
7784 hash
->dth_stroffs
= arg
;
7785 hash
->dth_nextoffs
= nextoffs
;
7786 hash
->dth_prevoffs
= prevoffs
;
7789 hash
->dth_mask
= hash
->dth_size
- 1;
7791 hash
->dth_tab
= kmem_zalloc(hash
->dth_size
*
7792 sizeof (dtrace_hashbucket_t
*), KM_SLEEP
);
7798 * APPLE NOTE: dtrace_hash_destroy is not used.
7799 * It is called by dtrace_detach which is not
7800 * currently implemented. Revisit someday.
7802 #if !defined(__APPLE__)
7804 dtrace_hash_destroy(dtrace_hash_t
*hash
)
7809 for (i
= 0; i
< hash
->dth_size
; i
++)
7810 ASSERT(hash
->dth_tab
[i
] == NULL
);
7813 kmem_free(hash
->dth_tab
,
7814 hash
->dth_size
* sizeof (dtrace_hashbucket_t
*));
7815 kmem_free(hash
, sizeof (dtrace_hash_t
));
7817 #endif /* __APPLE__ */
7820 dtrace_hash_resize(dtrace_hash_t
*hash
)
7822 int size
= hash
->dth_size
, i
, ndx
;
7823 int new_size
= hash
->dth_size
<< 1;
7824 int new_mask
= new_size
- 1;
7825 dtrace_hashbucket_t
**new_tab
, *bucket
, *next
;
7827 ASSERT((new_size
& new_mask
) == 0);
7829 new_tab
= kmem_zalloc(new_size
* sizeof (void *), KM_SLEEP
);
7831 for (i
= 0; i
< size
; i
++) {
7832 for (bucket
= hash
->dth_tab
[i
]; bucket
!= NULL
; bucket
= next
) {
7833 void *elm
= bucket
->dthb_chain
;
7835 ASSERT(elm
!= NULL
);
7836 ndx
= DTRACE_HASHSTR(hash
, elm
) & new_mask
;
7838 next
= bucket
->dthb_next
;
7839 bucket
->dthb_next
= new_tab
[ndx
];
7840 new_tab
[ndx
] = bucket
;
7844 kmem_free(hash
->dth_tab
, hash
->dth_size
* sizeof (void *));
7845 hash
->dth_tab
= new_tab
;
7846 hash
->dth_size
= new_size
;
7847 hash
->dth_mask
= new_mask
;
7851 dtrace_hash_add(dtrace_hash_t
*hash
, void *new)
7853 int hashval
= DTRACE_HASHSTR(hash
, new);
7854 int ndx
= hashval
& hash
->dth_mask
;
7855 dtrace_hashbucket_t
*bucket
= hash
->dth_tab
[ndx
];
7856 void **nextp
, **prevp
;
7858 for (; bucket
!= NULL
; bucket
= bucket
->dthb_next
) {
7859 if (DTRACE_HASHEQ(hash
, bucket
->dthb_chain
, new))
7863 if ((hash
->dth_nbuckets
>> 1) > hash
->dth_size
) {
7864 dtrace_hash_resize(hash
);
7865 dtrace_hash_add(hash
, new);
7869 bucket
= kmem_zalloc(sizeof (dtrace_hashbucket_t
), KM_SLEEP
);
7870 bucket
->dthb_next
= hash
->dth_tab
[ndx
];
7871 hash
->dth_tab
[ndx
] = bucket
;
7872 hash
->dth_nbuckets
++;
7875 nextp
= DTRACE_HASHNEXT(hash
, new);
7876 ASSERT(*nextp
== NULL
&& *(DTRACE_HASHPREV(hash
, new)) == NULL
);
7877 *nextp
= bucket
->dthb_chain
;
7879 if (bucket
->dthb_chain
!= NULL
) {
7880 prevp
= DTRACE_HASHPREV(hash
, bucket
->dthb_chain
);
7881 ASSERT(*prevp
== NULL
);
7885 bucket
->dthb_chain
= new;
7890 dtrace_hash_lookup_string(dtrace_hash_t
*hash
, const char *str
)
7892 int hashval
= dtrace_hash_str(str
);
7893 int ndx
= hashval
& hash
->dth_mask
;
7894 dtrace_hashbucket_t
*bucket
= hash
->dth_tab
[ndx
];
7896 for (; bucket
!= NULL
; bucket
= bucket
->dthb_next
) {
7897 if (strcmp(str
, DTRACE_GETSTR(hash
, bucket
->dthb_chain
)) == 0)
7898 return (bucket
->dthb_chain
);
7904 static dtrace_probe_t
*
7905 dtrace_hash_lookup(dtrace_hash_t
*hash
, void *template)
7907 return dtrace_hash_lookup_string(hash
, DTRACE_GETSTR(hash
, template));
7911 dtrace_hash_collisions(dtrace_hash_t
*hash
, void *template)
7913 int hashval
= DTRACE_HASHSTR(hash
, template);
7914 int ndx
= hashval
& hash
->dth_mask
;
7915 dtrace_hashbucket_t
*bucket
= hash
->dth_tab
[ndx
];
7917 for (; bucket
!= NULL
; bucket
= bucket
->dthb_next
) {
7918 if (DTRACE_HASHEQ(hash
, bucket
->dthb_chain
, template))
7919 return (bucket
->dthb_len
);
7926 dtrace_hash_remove(dtrace_hash_t
*hash
, void *elm
)
7928 int ndx
= DTRACE_HASHSTR(hash
, elm
) & hash
->dth_mask
;
7929 dtrace_hashbucket_t
*bucket
= hash
->dth_tab
[ndx
];
7931 void **prevp
= DTRACE_HASHPREV(hash
, elm
);
7932 void **nextp
= DTRACE_HASHNEXT(hash
, elm
);
7935 * Find the bucket that we're removing this elm from.
7937 for (; bucket
!= NULL
; bucket
= bucket
->dthb_next
) {
7938 if (DTRACE_HASHEQ(hash
, bucket
->dthb_chain
, elm
))
7942 ASSERT(bucket
!= NULL
);
7944 if (*prevp
== NULL
) {
7945 if (*nextp
== NULL
) {
7947 * The removed element was the only element on this
7948 * bucket; we need to remove the bucket.
7950 dtrace_hashbucket_t
*b
= hash
->dth_tab
[ndx
];
7952 ASSERT(bucket
->dthb_chain
== elm
);
7956 hash
->dth_tab
[ndx
] = bucket
->dthb_next
;
7958 while (b
->dthb_next
!= bucket
)
7960 b
->dthb_next
= bucket
->dthb_next
;
7963 ASSERT(hash
->dth_nbuckets
> 0);
7964 hash
->dth_nbuckets
--;
7965 kmem_free(bucket
, sizeof (dtrace_hashbucket_t
));
7969 bucket
->dthb_chain
= *nextp
;
7971 *(DTRACE_HASHNEXT(hash
, *prevp
)) = *nextp
;
7975 *(DTRACE_HASHPREV(hash
, *nextp
)) = *prevp
;
7979 * DTrace Utility Functions
7981 * These are random utility functions that are _not_ called from probe context.
7984 dtrace_badattr(const dtrace_attribute_t
*a
)
7986 return (a
->dtat_name
> DTRACE_STABILITY_MAX
||
7987 a
->dtat_data
> DTRACE_STABILITY_MAX
||
7988 a
->dtat_class
> DTRACE_CLASS_MAX
);
7992 * Returns a dtrace-managed copy of a string, and will
7993 * deduplicate copies of the same string.
7994 * If the specified string is NULL, returns an empty string
7997 dtrace_strref(const char *str
)
7999 dtrace_string_t
*s
= NULL
;
8000 size_t bufsize
= (str
!= NULL
? strlen(str
) : 0) + 1;
8002 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
8007 for (s
= dtrace_hash_lookup_string(dtrace_strings
, str
); s
!= NULL
;
8008 s
= *(DTRACE_HASHNEXT(dtrace_strings
, s
))) {
8009 if (strncmp(str
, s
->dtst_str
, bufsize
) != 0) {
8012 ASSERT(s
->dtst_refcount
!= UINT32_MAX
);
8017 s
= kmem_zalloc(sizeof(dtrace_string_t
) + bufsize
, KM_SLEEP
);
8018 s
->dtst_refcount
= 1;
8019 (void) strlcpy(s
->dtst_str
, str
, bufsize
);
8021 dtrace_hash_add(dtrace_strings
, s
);
8027 dtrace_strunref(const char *str
)
8029 ASSERT(str
!= NULL
);
8030 dtrace_string_t
*s
= NULL
;
8031 size_t bufsize
= strlen(str
) + 1;
8033 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
8035 for (s
= dtrace_hash_lookup_string(dtrace_strings
, str
); s
!= NULL
;
8036 s
= *(DTRACE_HASHNEXT(dtrace_strings
, s
))) {
8037 if (strncmp(str
, s
->dtst_str
, bufsize
) != 0) {
8040 ASSERT(s
->dtst_refcount
!= 0);
8042 if (s
->dtst_refcount
== 0) {
8043 dtrace_hash_remove(dtrace_strings
, s
);
8044 kmem_free(s
, sizeof(dtrace_string_t
) + bufsize
);
8048 panic("attempt to unref non-existent string %s", str
);
8051 #define DTRACE_ISALPHA(c) \
8052 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
8055 dtrace_badname(const char *s
)
8059 if (s
== NULL
|| (c
= *s
++) == '\0')
8062 if (!DTRACE_ISALPHA(c
) && c
!= '-' && c
!= '_' && c
!= '.')
8065 while ((c
= *s
++) != '\0') {
8066 if (!DTRACE_ISALPHA(c
) && (c
< '0' || c
> '9') &&
8067 c
!= '-' && c
!= '_' && c
!= '.' && c
!= '`')
8075 dtrace_cred2priv(cred_t
*cr
, uint32_t *privp
, uid_t
*uidp
, zoneid_t
*zoneidp
)
8079 if (cr
== NULL
|| PRIV_POLICY_ONLY(cr
, PRIV_ALL
, B_FALSE
)) {
8080 if (dtrace_is_restricted() && !dtrace_are_restrictions_relaxed()) {
8081 priv
= DTRACE_PRIV_USER
| DTRACE_PRIV_PROC
| DTRACE_PRIV_OWNER
;
8084 priv
= DTRACE_PRIV_ALL
;
8089 *uidp
= crgetuid(cr
);
8090 *zoneidp
= crgetzoneid(cr
);
8093 if (PRIV_POLICY_ONLY(cr
, PRIV_DTRACE_KERNEL
, B_FALSE
))
8094 priv
|= DTRACE_PRIV_KERNEL
| DTRACE_PRIV_USER
;
8095 else if (PRIV_POLICY_ONLY(cr
, PRIV_DTRACE_USER
, B_FALSE
))
8096 priv
|= DTRACE_PRIV_USER
;
8097 if (PRIV_POLICY_ONLY(cr
, PRIV_DTRACE_PROC
, B_FALSE
))
8098 priv
|= DTRACE_PRIV_PROC
;
8099 if (PRIV_POLICY_ONLY(cr
, PRIV_PROC_OWNER
, B_FALSE
))
8100 priv
|= DTRACE_PRIV_OWNER
;
8101 if (PRIV_POLICY_ONLY(cr
, PRIV_PROC_ZONE
, B_FALSE
))
8102 priv
|= DTRACE_PRIV_ZONEOWNER
;
8108 #ifdef DTRACE_ERRDEBUG
8110 dtrace_errdebug(const char *str
)
8112 int hval
= dtrace_hash_str(str
) % DTRACE_ERRHASHSZ
;
8115 lck_mtx_lock(&dtrace_errlock
);
8116 dtrace_errlast
= str
;
8117 dtrace_errthread
= (kthread_t
*)current_thread();
8119 while (occupied
++ < DTRACE_ERRHASHSZ
) {
8120 if (dtrace_errhash
[hval
].dter_msg
== str
) {
8121 dtrace_errhash
[hval
].dter_count
++;
8125 if (dtrace_errhash
[hval
].dter_msg
!= NULL
) {
8126 hval
= (hval
+ 1) % DTRACE_ERRHASHSZ
;
8130 dtrace_errhash
[hval
].dter_msg
= str
;
8131 dtrace_errhash
[hval
].dter_count
= 1;
8135 panic("dtrace: undersized error hash");
8137 lck_mtx_unlock(&dtrace_errlock
);
8142 * DTrace Matching Functions
8144 * These functions are used to match groups of probes, given some elements of
8145 * a probe tuple, or some globbed expressions for elements of a probe tuple.
8148 dtrace_match_priv(const dtrace_probe_t
*prp
, uint32_t priv
, uid_t uid
,
8151 if (priv
!= DTRACE_PRIV_ALL
) {
8152 uint32_t ppriv
= prp
->dtpr_provider
->dtpv_priv
.dtpp_flags
;
8153 uint32_t match
= priv
& ppriv
;
8156 * No PRIV_DTRACE_* privileges...
8158 if ((priv
& (DTRACE_PRIV_PROC
| DTRACE_PRIV_USER
|
8159 DTRACE_PRIV_KERNEL
)) == 0)
8163 * No matching bits, but there were bits to match...
8165 if (match
== 0 && ppriv
!= 0)
8169 * Need to have permissions to the process, but don't...
8171 if (((ppriv
& ~match
) & DTRACE_PRIV_OWNER
) != 0 &&
8172 uid
!= prp
->dtpr_provider
->dtpv_priv
.dtpp_uid
) {
8177 * Need to be in the same zone unless we possess the
8178 * privilege to examine all zones.
8180 if (((ppriv
& ~match
) & DTRACE_PRIV_ZONEOWNER
) != 0 &&
8181 zoneid
!= prp
->dtpr_provider
->dtpv_priv
.dtpp_zoneid
) {
8190 * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which
8191 * consists of input pattern strings and an ops-vector to evaluate them.
8192 * This function returns >0 for match, 0 for no match, and <0 for error.
8195 dtrace_match_probe(const dtrace_probe_t
*prp
, const dtrace_probekey_t
*pkp
,
8196 uint32_t priv
, uid_t uid
, zoneid_t zoneid
)
8198 dtrace_provider_t
*pvp
= prp
->dtpr_provider
;
8201 if (pvp
->dtpv_defunct
)
8204 if ((rv
= pkp
->dtpk_pmatch(pvp
->dtpv_name
, pkp
->dtpk_prov
, 0)) <= 0)
8207 if ((rv
= pkp
->dtpk_mmatch(prp
->dtpr_mod
, pkp
->dtpk_mod
, 0)) <= 0)
8210 if ((rv
= pkp
->dtpk_fmatch(prp
->dtpr_func
, pkp
->dtpk_func
, 0)) <= 0)
8213 if ((rv
= pkp
->dtpk_nmatch(prp
->dtpr_name
, pkp
->dtpk_name
, 0)) <= 0)
8216 if (dtrace_match_priv(prp
, priv
, uid
, zoneid
) == 0)
8223 * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN)
8224 * interface for matching a glob pattern 'p' to an input string 's'. Unlike
8225 * libc's version, the kernel version only applies to 8-bit ASCII strings.
8226 * In addition, all of the recursion cases except for '*' matching have been
8227 * unwound. For '*', we still implement recursive evaluation, but a depth
8228 * counter is maintained and matching is aborted if we recurse too deep.
8229 * The function returns 0 if no match, >0 if match, and <0 if recursion error.
8232 dtrace_match_glob(const char *s
, const char *p
, int depth
)
8238 if (depth
> DTRACE_PROBEKEY_MAXDEPTH
)
8242 s
= ""; /* treat NULL as empty string */
8251 if ((c
= *p
++) == '\0')
8252 return (s1
== '\0');
8256 int ok
= 0, notflag
= 0;
8267 if ((c
= *p
++) == '\0')
8271 if (c
== '-' && lc
!= '\0' && *p
!= ']') {
8272 if ((c
= *p
++) == '\0')
8274 if (c
== '\\' && (c
= *p
++) == '\0')
8278 if (s1
< lc
|| s1
> c
)
8282 } else if (lc
<= s1
&& s1
<= c
)
8285 } else if (c
== '\\' && (c
= *p
++) == '\0')
8288 lc
= c
; /* save left-hand 'c' for next iteration */
8298 if ((c
= *p
++) == '\0')
8310 if ((c
= *p
++) == '\0')
8326 p
++; /* consecutive *'s are identical to a single one */
8331 for (s
= olds
; *s
!= '\0'; s
++) {
8332 if ((gs
= dtrace_match_glob(s
, p
, depth
+ 1)) != 0)
8342 dtrace_match_string(const char *s
, const char *p
, int depth
)
8344 #pragma unused(depth) /* __APPLE__ */
8345 return (s
!= NULL
&& s
== p
);
8350 dtrace_match_module(const char *s
, const char *p
, int depth
)
8352 #pragma unused(depth) /* __APPLE__ */
8354 if (s
== NULL
|| p
== NULL
)
8359 if (strncmp(p
, s
, len
) != 0)
8362 if (s
[len
] == '.' || s
[len
] == '\0')
8370 dtrace_match_nul(const char *s
, const char *p
, int depth
)
8372 #pragma unused(s, p, depth) /* __APPLE__ */
8373 return (1); /* always match the empty pattern */
8378 dtrace_match_nonzero(const char *s
, const char *p
, int depth
)
8380 #pragma unused(p, depth) /* __APPLE__ */
8381 return (s
!= NULL
&& s
[0] != '\0');
8385 dtrace_match(const dtrace_probekey_t
*pkp
, uint32_t priv
, uid_t uid
,
8386 zoneid_t zoneid
, int (*matched
)(dtrace_probe_t
*, void *, void *), void *arg1
, void *arg2
)
8388 dtrace_probe_t
*probe
;
8389 dtrace_provider_t prov_template
= {
8390 .dtpv_name
= (char *)(uintptr_t)pkp
->dtpk_prov
8393 dtrace_probe_t
template = {
8394 .dtpr_provider
= &prov_template
,
8395 .dtpr_mod
= (char *)(uintptr_t)pkp
->dtpk_mod
,
8396 .dtpr_func
= (char *)(uintptr_t)pkp
->dtpk_func
,
8397 .dtpr_name
= (char *)(uintptr_t)pkp
->dtpk_name
8400 dtrace_hash_t
*hash
= NULL
;
8401 int len
, rc
, best
= INT_MAX
, nmatched
= 0;
8404 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
8407 * If the probe ID is specified in the key, just lookup by ID and
8408 * invoke the match callback once if a matching probe is found.
8410 if (pkp
->dtpk_id
!= DTRACE_IDNONE
) {
8411 if ((probe
= dtrace_probe_lookup_id(pkp
->dtpk_id
)) != NULL
&&
8412 dtrace_match_probe(probe
, pkp
, priv
, uid
, zoneid
) > 0) {
8413 if ((*matched
)(probe
, arg1
, arg2
) == DTRACE_MATCH_FAIL
)
8414 return (DTRACE_MATCH_FAIL
);
8421 * We want to find the most distinct of the provider name, module name,
8422 * function name, and name. So for each one that is not a glob
8423 * pattern or empty string, we perform a lookup in the corresponding
8424 * hash and use the hash table with the fewest collisions to do our
8427 if (pkp
->dtpk_pmatch
== &dtrace_match_string
&&
8428 (len
= dtrace_hash_collisions(dtrace_byprov
, &template)) < best
) {
8430 hash
= dtrace_byprov
;
8433 if (pkp
->dtpk_mmatch
== &dtrace_match_string
&&
8434 (len
= dtrace_hash_collisions(dtrace_bymod
, &template)) < best
) {
8436 hash
= dtrace_bymod
;
8439 if (pkp
->dtpk_fmatch
== &dtrace_match_string
&&
8440 (len
= dtrace_hash_collisions(dtrace_byfunc
, &template)) < best
) {
8442 hash
= dtrace_byfunc
;
8445 if (pkp
->dtpk_nmatch
== &dtrace_match_string
&&
8446 (len
= dtrace_hash_collisions(dtrace_byname
, &template)) < best
) {
8448 hash
= dtrace_byname
;
8452 * If we did not select a hash table, iterate over every probe and
8453 * invoke our callback for each one that matches our input probe key.
8456 for (i
= 0; i
< (dtrace_id_t
)dtrace_nprobes
; i
++) {
8457 if ((probe
= dtrace_probes
[i
]) == NULL
||
8458 dtrace_match_probe(probe
, pkp
, priv
, uid
,
8464 if ((rc
= (*matched
)(probe
, arg1
, arg2
)) != DTRACE_MATCH_NEXT
) {
8465 if (rc
== DTRACE_MATCH_FAIL
)
8466 return (DTRACE_MATCH_FAIL
);
8475 * If we selected a hash table, iterate over each probe of the same key
8476 * name and invoke the callback for every probe that matches the other
8477 * attributes of our input probe key.
8479 for (probe
= dtrace_hash_lookup(hash
, &template); probe
!= NULL
;
8480 probe
= *(DTRACE_HASHNEXT(hash
, probe
))) {
8482 if (dtrace_match_probe(probe
, pkp
, priv
, uid
, zoneid
) <= 0)
8487 if ((rc
= (*matched
)(probe
, arg1
, arg2
)) != DTRACE_MATCH_NEXT
) {
8488 if (rc
== DTRACE_MATCH_FAIL
)
8489 return (DTRACE_MATCH_FAIL
);
8498 * Return the function pointer dtrace_probecmp() should use to compare the
8499 * specified pattern with a string. For NULL or empty patterns, we select
8500 * dtrace_match_nul(). For glob pattern strings, we use dtrace_match_glob().
8501 * For non-empty non-glob strings, we use dtrace_match_string().
8503 static dtrace_probekey_f
*
8504 dtrace_probekey_func(const char *p
)
8508 if (p
== NULL
|| *p
== '\0')
8509 return (&dtrace_match_nul
);
8511 while ((c
= *p
++) != '\0') {
8512 if (c
== '[' || c
== '?' || c
== '*' || c
== '\\')
8513 return (&dtrace_match_glob
);
8516 return (&dtrace_match_string
);
8519 static dtrace_probekey_f
*
8520 dtrace_probekey_module_func(const char *p
)
8522 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
8524 dtrace_probekey_f
*f
= dtrace_probekey_func(p
);
8525 if (f
== &dtrace_match_string
) {
8526 dtrace_probe_t
template = {
8527 .dtpr_mod
= (char *)(uintptr_t)p
,
8529 if (dtrace_hash_lookup(dtrace_bymod
, &template) == NULL
) {
8530 return (&dtrace_match_module
);
8532 return (&dtrace_match_string
);
8538 * Build a probe comparison key for use with dtrace_match_probe() from the
8539 * given probe description. By convention, a null key only matches anchored
8540 * probes: if each field is the empty string, reset dtpk_fmatch to
8541 * dtrace_match_nonzero().
8544 dtrace_probekey(const dtrace_probedesc_t
*pdp
, dtrace_probekey_t
*pkp
)
8547 pkp
->dtpk_prov
= dtrace_strref(pdp
->dtpd_provider
);
8548 pkp
->dtpk_pmatch
= dtrace_probekey_func(pdp
->dtpd_provider
);
8550 pkp
->dtpk_mod
= dtrace_strref(pdp
->dtpd_mod
);
8551 pkp
->dtpk_mmatch
= dtrace_probekey_module_func(pdp
->dtpd_mod
);
8553 pkp
->dtpk_func
= dtrace_strref(pdp
->dtpd_func
);
8554 pkp
->dtpk_fmatch
= dtrace_probekey_func(pdp
->dtpd_func
);
8556 pkp
->dtpk_name
= dtrace_strref(pdp
->dtpd_name
);
8557 pkp
->dtpk_nmatch
= dtrace_probekey_func(pdp
->dtpd_name
);
8559 pkp
->dtpk_id
= pdp
->dtpd_id
;
8561 if (pkp
->dtpk_id
== DTRACE_IDNONE
&&
8562 pkp
->dtpk_pmatch
== &dtrace_match_nul
&&
8563 pkp
->dtpk_mmatch
== &dtrace_match_nul
&&
8564 pkp
->dtpk_fmatch
== &dtrace_match_nul
&&
8565 pkp
->dtpk_nmatch
== &dtrace_match_nul
)
8566 pkp
->dtpk_fmatch
= &dtrace_match_nonzero
;
8570 dtrace_probekey_release(dtrace_probekey_t
*pkp
)
8572 dtrace_strunref(pkp
->dtpk_prov
);
8573 dtrace_strunref(pkp
->dtpk_mod
);
8574 dtrace_strunref(pkp
->dtpk_func
);
8575 dtrace_strunref(pkp
->dtpk_name
);
8579 dtrace_cond_provider_match(dtrace_probedesc_t
*desc
, void *data
)
8584 dtrace_probekey_f
*func
= dtrace_probekey_func(desc
->dtpd_provider
);
8586 return func((char*)data
, desc
->dtpd_provider
, 0);
8590 * DTrace Provider-to-Framework API Functions
8592 * These functions implement much of the Provider-to-Framework API, as
8593 * described in <sys/dtrace.h>. The parts of the API not in this section are
8594 * the functions in the API for probe management (found below), and
8595 * dtrace_probe() itself (found above).
8599 * Register the calling provider with the DTrace framework. This should
8600 * generally be called by DTrace providers in their attach(9E) entry point.
8603 dtrace_register(const char *name
, const dtrace_pattr_t
*pap
, uint32_t priv
,
8604 cred_t
*cr
, const dtrace_pops_t
*pops
, void *arg
, dtrace_provider_id_t
*idp
)
8606 dtrace_provider_t
*provider
;
8608 if (name
== NULL
|| pap
== NULL
|| pops
== NULL
|| idp
== NULL
) {
8609 cmn_err(CE_WARN
, "failed to register provider '%s': invalid "
8610 "arguments", name
? name
: "<NULL>");
8614 if (name
[0] == '\0' || dtrace_badname(name
)) {
8615 cmn_err(CE_WARN
, "failed to register provider '%s': invalid "
8616 "provider name", name
);
8620 if ((pops
->dtps_provide
== NULL
&& pops
->dtps_provide_module
== NULL
) ||
8621 pops
->dtps_enable
== NULL
|| pops
->dtps_disable
== NULL
||
8622 pops
->dtps_destroy
== NULL
||
8623 ((pops
->dtps_resume
== NULL
) != (pops
->dtps_suspend
== NULL
))) {
8624 cmn_err(CE_WARN
, "failed to register provider '%s': invalid "
8625 "provider ops", name
);
8629 if (dtrace_badattr(&pap
->dtpa_provider
) ||
8630 dtrace_badattr(&pap
->dtpa_mod
) ||
8631 dtrace_badattr(&pap
->dtpa_func
) ||
8632 dtrace_badattr(&pap
->dtpa_name
) ||
8633 dtrace_badattr(&pap
->dtpa_args
)) {
8634 cmn_err(CE_WARN
, "failed to register provider '%s': invalid "
8635 "provider attributes", name
);
8639 if (priv
& ~DTRACE_PRIV_ALL
) {
8640 cmn_err(CE_WARN
, "failed to register provider '%s': invalid "
8641 "privilege attributes", name
);
8645 if ((priv
& DTRACE_PRIV_KERNEL
) &&
8646 (priv
& (DTRACE_PRIV_USER
| DTRACE_PRIV_OWNER
)) &&
8647 pops
->dtps_usermode
== NULL
) {
8648 cmn_err(CE_WARN
, "failed to register provider '%s': need "
8649 "dtps_usermode() op for given privilege attributes", name
);
8653 provider
= kmem_zalloc(sizeof (dtrace_provider_t
), KM_SLEEP
);
8655 provider
->dtpv_attr
= *pap
;
8656 provider
->dtpv_priv
.dtpp_flags
= priv
;
8658 provider
->dtpv_priv
.dtpp_uid
= crgetuid(cr
);
8659 provider
->dtpv_priv
.dtpp_zoneid
= crgetzoneid(cr
);
8661 provider
->dtpv_pops
= *pops
;
8663 if (pops
->dtps_provide
== NULL
) {
8664 ASSERT(pops
->dtps_provide_module
!= NULL
);
8665 provider
->dtpv_pops
.dtps_provide
= dtrace_provide_nullop
;
8668 if (pops
->dtps_provide_module
== NULL
) {
8669 ASSERT(pops
->dtps_provide
!= NULL
);
8670 provider
->dtpv_pops
.dtps_provide_module
=
8671 dtrace_provide_module_nullop
;
8674 if (pops
->dtps_suspend
== NULL
) {
8675 ASSERT(pops
->dtps_resume
== NULL
);
8676 provider
->dtpv_pops
.dtps_suspend
= dtrace_suspend_nullop
;
8677 provider
->dtpv_pops
.dtps_resume
= dtrace_resume_nullop
;
8680 provider
->dtpv_arg
= arg
;
8681 *idp
= (dtrace_provider_id_t
)provider
;
8683 if (pops
== &dtrace_provider_ops
) {
8684 LCK_MTX_ASSERT(&dtrace_provider_lock
, LCK_MTX_ASSERT_OWNED
);
8685 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
8687 provider
->dtpv_name
= dtrace_strref(name
);
8689 ASSERT(dtrace_anon
.dta_enabling
== NULL
);
8692 * We make sure that the DTrace provider is at the head of
8693 * the provider chain.
8695 provider
->dtpv_next
= dtrace_provider
;
8696 dtrace_provider
= provider
;
8700 lck_mtx_lock(&dtrace_provider_lock
);
8701 lck_mtx_lock(&dtrace_lock
);
8703 provider
->dtpv_name
= dtrace_strref(name
);
8706 * If there is at least one provider registered, we'll add this
8707 * provider after the first provider.
8709 if (dtrace_provider
!= NULL
) {
8710 provider
->dtpv_next
= dtrace_provider
->dtpv_next
;
8711 dtrace_provider
->dtpv_next
= provider
;
8713 dtrace_provider
= provider
;
8716 if (dtrace_retained
!= NULL
) {
8717 dtrace_enabling_provide(provider
);
8720 * Now we need to call dtrace_enabling_matchall_with_cond() --
8721 * with a condition matching the provider name we just added,
8722 * which will acquire cpu_lock and dtrace_lock. We therefore need
8723 * to drop all of our locks before calling into it...
8725 lck_mtx_unlock(&dtrace_lock
);
8726 lck_mtx_unlock(&dtrace_provider_lock
);
8728 dtrace_match_cond_t cond
= {dtrace_cond_provider_match
, provider
->dtpv_name
};
8729 dtrace_enabling_matchall_with_cond(&cond
);
8734 lck_mtx_unlock(&dtrace_lock
);
8735 lck_mtx_unlock(&dtrace_provider_lock
);
8741 * Unregister the specified provider from the DTrace framework. This should
8742 * generally be called by DTrace providers in their detach(9E) entry point.
8745 dtrace_unregister(dtrace_provider_id_t id
)
8747 dtrace_provider_t
*old
= (dtrace_provider_t
*)id
;
8748 dtrace_provider_t
*prev
= NULL
;
8750 dtrace_probe_t
*probe
, *first
= NULL
, *next
= NULL
;
8751 dtrace_probe_t
template = {
8752 .dtpr_provider
= old
8755 if (old
->dtpv_pops
.dtps_enable
==
8756 (int (*)(void *, dtrace_id_t
, void *))dtrace_enable_nullop
) {
8758 * If DTrace itself is the provider, we're called with locks
8761 ASSERT(old
== dtrace_provider
);
8762 ASSERT(dtrace_devi
!= NULL
);
8763 LCK_MTX_ASSERT(&dtrace_provider_lock
, LCK_MTX_ASSERT_OWNED
);
8764 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
8767 if (dtrace_provider
->dtpv_next
!= NULL
) {
8769 * There's another provider here; return failure.
8774 lck_mtx_lock(&dtrace_provider_lock
);
8775 lck_mtx_lock(&mod_lock
);
8776 lck_mtx_lock(&dtrace_lock
);
8780 * If anyone has /dev/dtrace open, or if there are anonymous enabled
8781 * probes, we refuse to let providers slither away, unless this
8782 * provider has already been explicitly invalidated.
8784 if (!old
->dtpv_defunct
&&
8785 (dtrace_opens
|| (dtrace_anon
.dta_state
!= NULL
&&
8786 dtrace_anon
.dta_state
->dts_necbs
> 0))) {
8788 lck_mtx_unlock(&dtrace_lock
);
8789 lck_mtx_unlock(&mod_lock
);
8790 lck_mtx_unlock(&dtrace_provider_lock
);
8796 * Attempt to destroy the probes associated with this provider.
8798 if (old
->dtpv_ecb_count
!=0) {
8800 * We have at least one ECB; we can't remove this provider.
8803 lck_mtx_unlock(&dtrace_lock
);
8804 lck_mtx_unlock(&mod_lock
);
8805 lck_mtx_unlock(&dtrace_provider_lock
);
8811 * All of the probes for this provider are disabled; we can safely
8812 * remove all of them from their hash chains and from the probe array.
8814 for (probe
= dtrace_hash_lookup(dtrace_byprov
, &template); probe
!= NULL
;
8815 probe
= *(DTRACE_HASHNEXT(dtrace_byprov
, probe
))) {
8816 if (probe
->dtpr_provider
!= old
)
8819 dtrace_probes
[probe
->dtpr_id
- 1] = NULL
;
8820 old
->dtpv_probe_count
--;
8822 dtrace_hash_remove(dtrace_bymod
, probe
);
8823 dtrace_hash_remove(dtrace_byfunc
, probe
);
8824 dtrace_hash_remove(dtrace_byname
, probe
);
8826 if (first
== NULL
) {
8828 probe
->dtpr_nextmod
= NULL
;
8831 * Use nextmod as the chain of probes to remove
8833 probe
->dtpr_nextmod
= first
;
8838 for (probe
= first
; probe
!= NULL
; probe
= next
) {
8839 next
= probe
->dtpr_nextmod
;
8840 dtrace_hash_remove(dtrace_byprov
, probe
);
8844 * The provider's probes have been removed from the hash chains and
8845 * from the probe array. Now issue a dtrace_sync() to be sure that
8846 * everyone has cleared out from any probe array processing.
8850 for (probe
= first
; probe
!= NULL
; probe
= next
) {
8851 next
= probe
->dtpr_nextmod
;
8853 old
->dtpv_pops
.dtps_destroy(old
->dtpv_arg
, probe
->dtpr_id
,
8855 dtrace_strunref(probe
->dtpr_mod
);
8856 dtrace_strunref(probe
->dtpr_func
);
8857 dtrace_strunref(probe
->dtpr_name
);
8858 vmem_free(dtrace_arena
, (void *)(uintptr_t)(probe
->dtpr_id
), 1);
8859 zfree(dtrace_probe_t_zone
, probe
);
8862 if ((prev
= dtrace_provider
) == old
) {
8863 ASSERT(self
|| dtrace_devi
== NULL
);
8864 ASSERT(old
->dtpv_next
== NULL
|| dtrace_devi
== NULL
);
8865 dtrace_provider
= old
->dtpv_next
;
8867 while (prev
!= NULL
&& prev
->dtpv_next
!= old
)
8868 prev
= prev
->dtpv_next
;
8871 panic("attempt to unregister non-existent "
8872 "dtrace provider %p\n", (void *)id
);
8875 prev
->dtpv_next
= old
->dtpv_next
;
8878 dtrace_strunref(old
->dtpv_name
);
8881 lck_mtx_unlock(&dtrace_lock
);
8882 lck_mtx_unlock(&mod_lock
);
8883 lck_mtx_unlock(&dtrace_provider_lock
);
8886 kmem_free(old
, sizeof (dtrace_provider_t
));
8892 * Invalidate the specified provider. All subsequent probe lookups for the
8893 * specified provider will fail, but its probes will not be removed.
8896 dtrace_invalidate(dtrace_provider_id_t id
)
8898 dtrace_provider_t
*pvp
= (dtrace_provider_t
*)id
;
8900 ASSERT(pvp
->dtpv_pops
.dtps_enable
!=
8901 (int (*)(void *, dtrace_id_t
, void *))dtrace_enable_nullop
);
8903 lck_mtx_lock(&dtrace_provider_lock
);
8904 lck_mtx_lock(&dtrace_lock
);
8906 pvp
->dtpv_defunct
= 1;
8908 lck_mtx_unlock(&dtrace_lock
);
8909 lck_mtx_unlock(&dtrace_provider_lock
);
8913 * Indicate whether or not DTrace has attached.
8916 dtrace_attached(void)
8919 * dtrace_provider will be non-NULL iff the DTrace driver has
8920 * attached. (It's non-NULL because DTrace is always itself a
8923 return (dtrace_provider
!= NULL
);
8927 * Remove all the unenabled probes for the given provider. This function is
8928 * not unlike dtrace_unregister(), except that it doesn't remove the provider
8929 * -- just as many of its associated probes as it can.
8932 dtrace_condense(dtrace_provider_id_t id
)
8934 dtrace_provider_t
*prov
= (dtrace_provider_t
*)id
;
8935 dtrace_probe_t
*probe
, *first
= NULL
;
8936 dtrace_probe_t
template = {
8937 .dtpr_provider
= prov
8941 * Make sure this isn't the dtrace provider itself.
8943 ASSERT(prov
->dtpv_pops
.dtps_enable
!=
8944 (int (*)(void *, dtrace_id_t
, void *))dtrace_enable_nullop
);
8946 lck_mtx_lock(&dtrace_provider_lock
);
8947 lck_mtx_lock(&dtrace_lock
);
8950 * Attempt to destroy the probes associated with this provider.
8952 for (probe
= dtrace_hash_lookup(dtrace_byprov
, &template); probe
!= NULL
;
8953 probe
= *(DTRACE_HASHNEXT(dtrace_byprov
, probe
))) {
8955 if (probe
->dtpr_provider
!= prov
)
8958 if (probe
->dtpr_ecb
!= NULL
)
8961 dtrace_probes
[probe
->dtpr_id
- 1] = NULL
;
8962 prov
->dtpv_probe_count
--;
8964 dtrace_hash_remove(dtrace_bymod
, probe
);
8965 dtrace_hash_remove(dtrace_byfunc
, probe
);
8966 dtrace_hash_remove(dtrace_byname
, probe
);
8968 prov
->dtpv_pops
.dtps_destroy(prov
->dtpv_arg
, probe
->dtpr_id
,
8970 dtrace_strunref(probe
->dtpr_mod
);
8971 dtrace_strunref(probe
->dtpr_func
);
8972 dtrace_strunref(probe
->dtpr_name
);
8973 if (first
== NULL
) {
8975 probe
->dtpr_nextmod
= NULL
;
8978 * Use nextmod as the chain of probes to remove
8980 probe
->dtpr_nextmod
= first
;
8985 for (probe
= first
; probe
!= NULL
; probe
= first
) {
8986 first
= probe
->dtpr_nextmod
;
8987 dtrace_hash_remove(dtrace_byprov
, probe
);
8988 vmem_free(dtrace_arena
, (void *)((uintptr_t)probe
->dtpr_id
), 1);
8989 zfree(dtrace_probe_t_zone
, probe
);
8992 lck_mtx_unlock(&dtrace_lock
);
8993 lck_mtx_unlock(&dtrace_provider_lock
);
8999 * DTrace Probe Management Functions
9001 * The functions in this section perform the DTrace probe management,
9002 * including functions to create probes, look-up probes, and call into the
9003 * providers to request that probes be provided. Some of these functions are
9004 * in the Provider-to-Framework API; these functions can be identified by the
9005 * fact that they are not declared "static".
9009 * Create a probe with the specified module name, function name, and name.
9012 dtrace_probe_create(dtrace_provider_id_t prov
, const char *mod
,
9013 const char *func
, const char *name
, int aframes
, void *arg
)
9015 dtrace_probe_t
*probe
, **probes
;
9016 dtrace_provider_t
*provider
= (dtrace_provider_t
*)prov
;
9019 if (provider
== dtrace_provider
) {
9020 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
9022 lck_mtx_lock(&dtrace_lock
);
9025 id
= (dtrace_id_t
)(uintptr_t)vmem_alloc(dtrace_arena
, 1,
9026 VM_BESTFIT
| VM_SLEEP
);
9028 probe
= zalloc(dtrace_probe_t_zone
);
9029 bzero(probe
, sizeof (dtrace_probe_t
));
9031 probe
->dtpr_id
= id
;
9032 probe
->dtpr_gen
= dtrace_probegen
++;
9033 probe
->dtpr_mod
= dtrace_strref(mod
);
9034 probe
->dtpr_func
= dtrace_strref(func
);
9035 probe
->dtpr_name
= dtrace_strref(name
);
9036 probe
->dtpr_arg
= arg
;
9037 probe
->dtpr_aframes
= aframes
;
9038 probe
->dtpr_provider
= provider
;
9040 dtrace_hash_add(dtrace_byprov
, probe
);
9041 dtrace_hash_add(dtrace_bymod
, probe
);
9042 dtrace_hash_add(dtrace_byfunc
, probe
);
9043 dtrace_hash_add(dtrace_byname
, probe
);
9045 if (id
- 1 >= (dtrace_id_t
)dtrace_nprobes
) {
9046 size_t osize
= dtrace_nprobes
* sizeof (dtrace_probe_t
*);
9047 size_t nsize
= osize
* 2;
9049 probes
= kmem_zalloc(nsize
, KM_SLEEP
);
9051 dtrace_probe_t
**oprobes
= dtrace_probes
;
9053 bcopy(oprobes
, probes
, osize
);
9054 dtrace_membar_producer();
9055 dtrace_probes
= probes
;
9060 * All CPUs are now seeing the new probes array; we can
9061 * safely free the old array.
9063 kmem_free(oprobes
, osize
);
9064 dtrace_nprobes
*= 2;
9066 ASSERT(id
- 1 < (dtrace_id_t
)dtrace_nprobes
);
9069 ASSERT(dtrace_probes
[id
- 1] == NULL
);
9070 dtrace_probes
[id
- 1] = probe
;
9071 provider
->dtpv_probe_count
++;
9073 if (provider
!= dtrace_provider
)
9074 lck_mtx_unlock(&dtrace_lock
);
9079 static dtrace_probe_t
*
9080 dtrace_probe_lookup_id(dtrace_id_t id
)
9082 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
9084 if (id
== 0 || id
> (dtrace_id_t
)dtrace_nprobes
)
9087 return (dtrace_probes
[id
- 1]);
9091 dtrace_probe_lookup_match(dtrace_probe_t
*probe
, void *arg1
, void *arg2
)
9093 #pragma unused(arg2)
9094 *((dtrace_id_t
*)arg1
) = probe
->dtpr_id
;
9096 return (DTRACE_MATCH_DONE
);
9100 * Look up a probe based on provider and one or more of module name, function
9101 * name and probe name.
9104 dtrace_probe_lookup(dtrace_provider_id_t prid
, const char *mod
,
9105 const char *func
, const char *name
)
9107 dtrace_probekey_t pkey
;
9111 lck_mtx_lock(&dtrace_lock
);
9113 pkey
.dtpk_prov
= dtrace_strref(((dtrace_provider_t
*)prid
)->dtpv_name
);
9114 pkey
.dtpk_pmatch
= &dtrace_match_string
;
9115 pkey
.dtpk_mod
= dtrace_strref(mod
);
9116 pkey
.dtpk_mmatch
= mod
? &dtrace_match_string
: &dtrace_match_nul
;
9117 pkey
.dtpk_func
= dtrace_strref(func
);
9118 pkey
.dtpk_fmatch
= func
? &dtrace_match_string
: &dtrace_match_nul
;
9119 pkey
.dtpk_name
= dtrace_strref(name
);
9120 pkey
.dtpk_nmatch
= name
? &dtrace_match_string
: &dtrace_match_nul
;
9121 pkey
.dtpk_id
= DTRACE_IDNONE
;
9123 match
= dtrace_match(&pkey
, DTRACE_PRIV_ALL
, 0, 0,
9124 dtrace_probe_lookup_match
, &id
, NULL
);
9126 dtrace_probekey_release(&pkey
);
9128 lck_mtx_unlock(&dtrace_lock
);
9130 ASSERT(match
== 1 || match
== 0);
9131 return (match
? id
: 0);
9135 * Returns the probe argument associated with the specified probe.
9138 dtrace_probe_arg(dtrace_provider_id_t id
, dtrace_id_t pid
)
9140 dtrace_probe_t
*probe
;
9143 lck_mtx_lock(&dtrace_lock
);
9145 if ((probe
= dtrace_probe_lookup_id(pid
)) != NULL
&&
9146 probe
->dtpr_provider
== (dtrace_provider_t
*)id
)
9147 rval
= probe
->dtpr_arg
;
9149 lck_mtx_unlock(&dtrace_lock
);
9155 * Copy a probe into a probe description.
9158 dtrace_probe_description(const dtrace_probe_t
*prp
, dtrace_probedesc_t
*pdp
)
9160 bzero(pdp
, sizeof (dtrace_probedesc_t
));
9161 pdp
->dtpd_id
= prp
->dtpr_id
;
9163 /* APPLE NOTE: Darwin employs size bounded string operation. */
9164 (void) strlcpy(pdp
->dtpd_provider
,
9165 prp
->dtpr_provider
->dtpv_name
, DTRACE_PROVNAMELEN
);
9167 (void) strlcpy(pdp
->dtpd_mod
, prp
->dtpr_mod
, DTRACE_MODNAMELEN
);
9168 (void) strlcpy(pdp
->dtpd_func
, prp
->dtpr_func
, DTRACE_FUNCNAMELEN
);
9169 (void) strlcpy(pdp
->dtpd_name
, prp
->dtpr_name
, DTRACE_NAMELEN
);
9173 * Called to indicate that a probe -- or probes -- should be provided by a
9174 * specfied provider. If the specified description is NULL, the provider will
9175 * be told to provide all of its probes. (This is done whenever a new
9176 * consumer comes along, or whenever a retained enabling is to be matched.) If
9177 * the specified description is non-NULL, the provider is given the
9178 * opportunity to dynamically provide the specified probe, allowing providers
9179 * to support the creation of probes on-the-fly. (So-called _autocreated_
9180 * probes.) If the provider is NULL, the operations will be applied to all
9181 * providers; if the provider is non-NULL the operations will only be applied
9182 * to the specified provider. The dtrace_provider_lock must be held, and the
9183 * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation
9184 * will need to grab the dtrace_lock when it reenters the framework through
9185 * dtrace_probe_lookup(), dtrace_probe_create(), etc.
9188 dtrace_probe_provide(dtrace_probedesc_t
*desc
, dtrace_provider_t
*prv
)
9193 LCK_MTX_ASSERT(&dtrace_provider_lock
, LCK_MTX_ASSERT_OWNED
);
9197 prv
= dtrace_provider
;
9202 * First, call the blanket provide operation.
9204 prv
->dtpv_pops
.dtps_provide(prv
->dtpv_arg
, desc
);
9207 * Now call the per-module provide operation. We will grab
9208 * mod_lock to prevent the list from being modified. Note
9209 * that this also prevents the mod_busy bits from changing.
9210 * (mod_busy can only be changed with mod_lock held.)
9212 lck_mtx_lock(&mod_lock
);
9214 ctl
= dtrace_modctl_list
;
9216 prv
->dtpv_pops
.dtps_provide_module(prv
->dtpv_arg
, ctl
);
9217 ctl
= ctl
->mod_next
;
9220 lck_mtx_unlock(&mod_lock
);
9221 } while (all
&& (prv
= prv
->dtpv_next
) != NULL
);
9225 * Iterate over each probe, and call the Framework-to-Provider API function
9229 dtrace_probe_foreach(uintptr_t offs
)
9231 dtrace_provider_t
*prov
;
9232 void (*func
)(void *, dtrace_id_t
, void *);
9233 dtrace_probe_t
*probe
;
9234 dtrace_icookie_t cookie
;
9238 * We disable interrupts to walk through the probe array. This is
9239 * safe -- the dtrace_sync() in dtrace_unregister() assures that we
9240 * won't see stale data.
9242 cookie
= dtrace_interrupt_disable();
9244 for (i
= 0; i
< dtrace_nprobes
; i
++) {
9245 if ((probe
= dtrace_probes
[i
]) == NULL
)
9248 if (probe
->dtpr_ecb
== NULL
) {
9250 * This probe isn't enabled -- don't call the function.
9255 prov
= probe
->dtpr_provider
;
9256 func
= *((void(**)(void *, dtrace_id_t
, void *))
9257 ((uintptr_t)&prov
->dtpv_pops
+ offs
));
9259 func(prov
->dtpv_arg
, i
+ 1, probe
->dtpr_arg
);
9262 dtrace_interrupt_enable(cookie
);
9266 dtrace_probe_enable(const dtrace_probedesc_t
*desc
, dtrace_enabling_t
*enab
, dtrace_ecbdesc_t
*ep
)
9268 dtrace_probekey_t pkey
;
9274 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
9276 dtrace_ecb_create_cache
= NULL
;
9280 * If we're passed a NULL description, we're being asked to
9281 * create an ECB with a NULL probe.
9283 (void) dtrace_ecb_create_enable(NULL
, enab
, ep
);
9287 dtrace_probekey(desc
, &pkey
);
9288 dtrace_cred2priv(enab
->dten_vstate
->dtvs_state
->dts_cred
.dcr_cred
,
9289 &priv
, &uid
, &zoneid
);
9291 err
= dtrace_match(&pkey
, priv
, uid
, zoneid
, dtrace_ecb_create_enable
, enab
, ep
);
9293 dtrace_probekey_release(&pkey
);
9299 * DTrace Helper Provider Functions
9302 dtrace_dofattr2attr(dtrace_attribute_t
*attr
, const dof_attr_t dofattr
)
9304 attr
->dtat_name
= DOF_ATTR_NAME(dofattr
);
9305 attr
->dtat_data
= DOF_ATTR_DATA(dofattr
);
9306 attr
->dtat_class
= DOF_ATTR_CLASS(dofattr
);
9310 dtrace_dofprov2hprov(dtrace_helper_provdesc_t
*hprov
,
9311 const dof_provider_t
*dofprov
, char *strtab
)
9313 hprov
->dthpv_provname
= strtab
+ dofprov
->dofpv_name
;
9314 dtrace_dofattr2attr(&hprov
->dthpv_pattr
.dtpa_provider
,
9315 dofprov
->dofpv_provattr
);
9316 dtrace_dofattr2attr(&hprov
->dthpv_pattr
.dtpa_mod
,
9317 dofprov
->dofpv_modattr
);
9318 dtrace_dofattr2attr(&hprov
->dthpv_pattr
.dtpa_func
,
9319 dofprov
->dofpv_funcattr
);
9320 dtrace_dofattr2attr(&hprov
->dthpv_pattr
.dtpa_name
,
9321 dofprov
->dofpv_nameattr
);
9322 dtrace_dofattr2attr(&hprov
->dthpv_pattr
.dtpa_args
,
9323 dofprov
->dofpv_argsattr
);
9327 dtrace_helper_provide_one(dof_helper_t
*dhp
, dof_sec_t
*sec
, proc_t
*p
)
9329 uintptr_t daddr
= (uintptr_t)dhp
->dofhp_dof
;
9330 dof_hdr_t
*dof
= (dof_hdr_t
*)daddr
;
9331 dof_sec_t
*str_sec
, *prb_sec
, *arg_sec
, *off_sec
, *enoff_sec
;
9332 dof_provider_t
*provider
;
9334 uint32_t *off
, *enoff
;
9338 dtrace_helper_provdesc_t dhpv
;
9339 dtrace_helper_probedesc_t dhpb
;
9340 dtrace_meta_t
*meta
= dtrace_meta_pid
;
9341 dtrace_mops_t
*mops
= &meta
->dtm_mops
;
9344 provider
= (dof_provider_t
*)(uintptr_t)(daddr
+ sec
->dofs_offset
);
9345 str_sec
= (dof_sec_t
*)(uintptr_t)(daddr
+ dof
->dofh_secoff
+
9346 provider
->dofpv_strtab
* dof
->dofh_secsize
);
9347 prb_sec
= (dof_sec_t
*)(uintptr_t)(daddr
+ dof
->dofh_secoff
+
9348 provider
->dofpv_probes
* dof
->dofh_secsize
);
9349 arg_sec
= (dof_sec_t
*)(uintptr_t)(daddr
+ dof
->dofh_secoff
+
9350 provider
->dofpv_prargs
* dof
->dofh_secsize
);
9351 off_sec
= (dof_sec_t
*)(uintptr_t)(daddr
+ dof
->dofh_secoff
+
9352 provider
->dofpv_proffs
* dof
->dofh_secsize
);
9354 strtab
= (char *)(uintptr_t)(daddr
+ str_sec
->dofs_offset
);
9355 off
= (uint32_t *)(uintptr_t)(daddr
+ off_sec
->dofs_offset
);
9356 arg
= (uint8_t *)(uintptr_t)(daddr
+ arg_sec
->dofs_offset
);
9360 * See dtrace_helper_provider_validate().
9362 if (dof
->dofh_ident
[DOF_ID_VERSION
] != DOF_VERSION_1
&&
9363 provider
->dofpv_prenoffs
!= DOF_SECT_NONE
) {
9364 enoff_sec
= (dof_sec_t
*)(uintptr_t)(daddr
+ dof
->dofh_secoff
+
9365 provider
->dofpv_prenoffs
* dof
->dofh_secsize
);
9366 enoff
= (uint32_t *)(uintptr_t)(daddr
+ enoff_sec
->dofs_offset
);
9369 nprobes
= prb_sec
->dofs_size
/ prb_sec
->dofs_entsize
;
9372 * Create the provider.
9374 dtrace_dofprov2hprov(&dhpv
, provider
, strtab
);
9376 if ((parg
= mops
->dtms_provide_proc(meta
->dtm_arg
, &dhpv
, p
)) == NULL
)
9382 * Create the probes.
9384 for (i
= 0; i
< nprobes
; i
++) {
9385 probe
= (dof_probe_t
*)(uintptr_t)(daddr
+
9386 prb_sec
->dofs_offset
+ i
* prb_sec
->dofs_entsize
);
9388 dhpb
.dthpb_mod
= dhp
->dofhp_mod
;
9389 dhpb
.dthpb_func
= strtab
+ probe
->dofpr_func
;
9390 dhpb
.dthpb_name
= strtab
+ probe
->dofpr_name
;
9391 #if !defined(__APPLE__)
9392 dhpb
.dthpb_base
= probe
->dofpr_addr
;
9394 dhpb
.dthpb_base
= dhp
->dofhp_addr
; /* FIXME: James, why? */
9396 dhpb
.dthpb_offs
= (int32_t *)(off
+ probe
->dofpr_offidx
);
9397 dhpb
.dthpb_noffs
= probe
->dofpr_noffs
;
9398 if (enoff
!= NULL
) {
9399 dhpb
.dthpb_enoffs
= (int32_t *)(enoff
+ probe
->dofpr_enoffidx
);
9400 dhpb
.dthpb_nenoffs
= probe
->dofpr_nenoffs
;
9402 dhpb
.dthpb_enoffs
= NULL
;
9403 dhpb
.dthpb_nenoffs
= 0;
9405 dhpb
.dthpb_args
= arg
+ probe
->dofpr_argidx
;
9406 dhpb
.dthpb_nargc
= probe
->dofpr_nargc
;
9407 dhpb
.dthpb_xargc
= probe
->dofpr_xargc
;
9408 dhpb
.dthpb_ntypes
= strtab
+ probe
->dofpr_nargv
;
9409 dhpb
.dthpb_xtypes
= strtab
+ probe
->dofpr_xargv
;
9411 mops
->dtms_create_probe(meta
->dtm_arg
, parg
, &dhpb
);
9415 * Since we just created probes, we need to match our enablings
9416 * against those, with a precondition knowing that we have only
9417 * added probes from this provider
9419 char *prov_name
= mops
->dtms_provider_name(parg
);
9420 ASSERT(prov_name
!= NULL
);
9421 dtrace_match_cond_t cond
= {dtrace_cond_provider_match
, (void*)prov_name
};
9423 dtrace_enabling_matchall_with_cond(&cond
);
9427 dtrace_helper_provide(dof_helper_t
*dhp
, proc_t
*p
)
9429 uintptr_t daddr
= (uintptr_t)dhp
->dofhp_dof
;
9430 dof_hdr_t
*dof
= (dof_hdr_t
*)daddr
;
9433 LCK_MTX_ASSERT(&dtrace_meta_lock
, LCK_MTX_ASSERT_OWNED
);
9435 for (i
= 0; i
< dof
->dofh_secnum
; i
++) {
9436 dof_sec_t
*sec
= (dof_sec_t
*)(uintptr_t)(daddr
+
9437 dof
->dofh_secoff
+ i
* dof
->dofh_secsize
);
9439 if (sec
->dofs_type
!= DOF_SECT_PROVIDER
)
9442 dtrace_helper_provide_one(dhp
, sec
, p
);
9447 dtrace_helper_provider_remove_one(dof_helper_t
*dhp
, dof_sec_t
*sec
, proc_t
*p
)
9449 uintptr_t daddr
= (uintptr_t)dhp
->dofhp_dof
;
9450 dof_hdr_t
*dof
= (dof_hdr_t
*)daddr
;
9452 dof_provider_t
*provider
;
9454 dtrace_helper_provdesc_t dhpv
;
9455 dtrace_meta_t
*meta
= dtrace_meta_pid
;
9456 dtrace_mops_t
*mops
= &meta
->dtm_mops
;
9458 provider
= (dof_provider_t
*)(uintptr_t)(daddr
+ sec
->dofs_offset
);
9459 str_sec
= (dof_sec_t
*)(uintptr_t)(daddr
+ dof
->dofh_secoff
+
9460 provider
->dofpv_strtab
* dof
->dofh_secsize
);
9462 strtab
= (char *)(uintptr_t)(daddr
+ str_sec
->dofs_offset
);
9465 * Create the provider.
9467 dtrace_dofprov2hprov(&dhpv
, provider
, strtab
);
9469 mops
->dtms_remove_proc(meta
->dtm_arg
, &dhpv
, p
);
9475 dtrace_helper_provider_remove(dof_helper_t
*dhp
, proc_t
*p
)
9477 uintptr_t daddr
= (uintptr_t)dhp
->dofhp_dof
;
9478 dof_hdr_t
*dof
= (dof_hdr_t
*)daddr
;
9481 LCK_MTX_ASSERT(&dtrace_meta_lock
, LCK_MTX_ASSERT_OWNED
);
9483 for (i
= 0; i
< dof
->dofh_secnum
; i
++) {
9484 dof_sec_t
*sec
= (dof_sec_t
*)(uintptr_t)(daddr
+
9485 dof
->dofh_secoff
+ i
* dof
->dofh_secsize
);
9487 if (sec
->dofs_type
!= DOF_SECT_PROVIDER
)
9490 dtrace_helper_provider_remove_one(dhp
, sec
, p
);
9495 * DTrace Meta Provider-to-Framework API Functions
9497 * These functions implement the Meta Provider-to-Framework API, as described
9498 * in <sys/dtrace.h>.
9501 dtrace_meta_register(const char *name
, const dtrace_mops_t
*mops
, void *arg
,
9502 dtrace_meta_provider_id_t
*idp
)
9504 dtrace_meta_t
*meta
;
9505 dtrace_helpers_t
*help
, *next
;
9508 *idp
= DTRACE_METAPROVNONE
;
9511 * We strictly don't need the name, but we hold onto it for
9512 * debuggability. All hail error queues!
9515 cmn_err(CE_WARN
, "failed to register meta-provider: "
9521 mops
->dtms_create_probe
== NULL
||
9522 mops
->dtms_provide_proc
== NULL
||
9523 mops
->dtms_remove_proc
== NULL
) {
9524 cmn_err(CE_WARN
, "failed to register meta-register %s: "
9525 "invalid ops", name
);
9529 meta
= kmem_zalloc(sizeof (dtrace_meta_t
), KM_SLEEP
);
9530 meta
->dtm_mops
= *mops
;
9531 meta
->dtm_arg
= arg
;
9533 lck_mtx_lock(&dtrace_meta_lock
);
9534 lck_mtx_lock(&dtrace_lock
);
9536 if (dtrace_meta_pid
!= NULL
) {
9537 lck_mtx_unlock(&dtrace_lock
);
9538 lck_mtx_unlock(&dtrace_meta_lock
);
9539 cmn_err(CE_WARN
, "failed to register meta-register %s: "
9540 "user-land meta-provider exists", name
);
9541 kmem_free(meta
, sizeof (dtrace_meta_t
));
9545 meta
->dtm_name
= dtrace_strref(name
);
9547 dtrace_meta_pid
= meta
;
9548 *idp
= (dtrace_meta_provider_id_t
)meta
;
9551 * If there are providers and probes ready to go, pass them
9552 * off to the new meta provider now.
9555 help
= dtrace_deferred_pid
;
9556 dtrace_deferred_pid
= NULL
;
9558 lck_mtx_unlock(&dtrace_lock
);
9560 while (help
!= NULL
) {
9561 for (i
= 0; i
< help
->dthps_nprovs
; i
++) {
9562 proc_t
*p
= proc_find(help
->dthps_pid
);
9565 dtrace_helper_provide(&help
->dthps_provs
[i
]->dthp_prov
,
9570 next
= help
->dthps_next
;
9571 help
->dthps_next
= NULL
;
9572 help
->dthps_prev
= NULL
;
9573 help
->dthps_deferred
= 0;
9577 lck_mtx_unlock(&dtrace_meta_lock
);
9583 dtrace_meta_unregister(dtrace_meta_provider_id_t id
)
9585 dtrace_meta_t
**pp
, *old
= (dtrace_meta_t
*)id
;
9587 lck_mtx_lock(&dtrace_meta_lock
);
9588 lck_mtx_lock(&dtrace_lock
);
9590 if (old
== dtrace_meta_pid
) {
9591 pp
= &dtrace_meta_pid
;
9593 panic("attempt to unregister non-existent "
9594 "dtrace meta-provider %p\n", (void *)old
);
9597 if (old
->dtm_count
!= 0) {
9598 lck_mtx_unlock(&dtrace_lock
);
9599 lck_mtx_unlock(&dtrace_meta_lock
);
9605 dtrace_strunref(old
->dtm_name
);
9607 lck_mtx_unlock(&dtrace_lock
);
9608 lck_mtx_unlock(&dtrace_meta_lock
);
9610 kmem_free(old
, sizeof (dtrace_meta_t
));
9617 * DTrace DIF Object Functions
9620 dtrace_difo_err(uint_t pc
, const char *format
, ...)
9622 if (dtrace_err_verbose
) {
9625 (void) uprintf("dtrace DIF object error: [%u]: ", pc
);
9626 va_start(alist
, format
);
9627 (void) vuprintf(format
, alist
);
9631 #ifdef DTRACE_ERRDEBUG
9632 dtrace_errdebug(format
);
9638 * Validate a DTrace DIF object by checking the IR instructions. The following
9639 * rules are currently enforced by dtrace_difo_validate():
9641 * 1. Each instruction must have a valid opcode
9642 * 2. Each register, string, variable, or subroutine reference must be valid
9643 * 3. No instruction can modify register %r0 (must be zero)
9644 * 4. All instruction reserved bits must be set to zero
9645 * 5. The last instruction must be a "ret" instruction
9646 * 6. All branch targets must reference a valid instruction _after_ the branch
9649 dtrace_difo_validate(dtrace_difo_t
*dp
, dtrace_vstate_t
*vstate
, uint_t nregs
,
9655 int (*efunc
)(uint_t pc
, const char *, ...) = dtrace_difo_err
;
9658 int maxglobal
= -1, maxlocal
= -1, maxtlocal
= -1;
9660 kcheckload
= cr
== NULL
||
9661 (vstate
->dtvs_state
->dts_cred
.dcr_visible
& DTRACE_CRV_KERNEL
) == 0;
9663 dp
->dtdo_destructive
= 0;
9665 for (pc
= 0; pc
< dp
->dtdo_len
&& err
== 0; pc
++) {
9666 dif_instr_t instr
= dp
->dtdo_buf
[pc
];
9668 uint_t r1
= DIF_INSTR_R1(instr
);
9669 uint_t r2
= DIF_INSTR_R2(instr
);
9670 uint_t rd
= DIF_INSTR_RD(instr
);
9671 uint_t rs
= DIF_INSTR_RS(instr
);
9672 uint_t label
= DIF_INSTR_LABEL(instr
);
9673 uint_t v
= DIF_INSTR_VAR(instr
);
9674 uint_t subr
= DIF_INSTR_SUBR(instr
);
9675 uint_t type
= DIF_INSTR_TYPE(instr
);
9676 uint_t op
= DIF_INSTR_OP(instr
);
9694 err
+= efunc(pc
, "invalid register %u\n", r1
);
9696 err
+= efunc(pc
, "invalid register %u\n", r2
);
9698 err
+= efunc(pc
, "invalid register %u\n", rd
);
9700 err
+= efunc(pc
, "cannot write to %%r0\n");
9706 err
+= efunc(pc
, "invalid register %u\n", r1
);
9708 err
+= efunc(pc
, "non-zero reserved bits\n");
9710 err
+= efunc(pc
, "invalid register %u\n", rd
);
9712 err
+= efunc(pc
, "cannot write to %%r0\n");
9722 err
+= efunc(pc
, "invalid register %u\n", r1
);
9724 err
+= efunc(pc
, "non-zero reserved bits\n");
9726 err
+= efunc(pc
, "invalid register %u\n", rd
);
9728 err
+= efunc(pc
, "cannot write to %%r0\n");
9730 dp
->dtdo_buf
[pc
] = DIF_INSTR_LOAD(op
+
9731 DIF_OP_RLDSB
- DIF_OP_LDSB
, r1
, rd
);
9741 err
+= efunc(pc
, "invalid register %u\n", r1
);
9743 err
+= efunc(pc
, "non-zero reserved bits\n");
9745 err
+= efunc(pc
, "invalid register %u\n", rd
);
9747 err
+= efunc(pc
, "cannot write to %%r0\n");
9757 err
+= efunc(pc
, "invalid register %u\n", r1
);
9759 err
+= efunc(pc
, "non-zero reserved bits\n");
9761 err
+= efunc(pc
, "invalid register %u\n", rd
);
9763 err
+= efunc(pc
, "cannot write to %%r0\n");
9770 err
+= efunc(pc
, "invalid register %u\n", r1
);
9772 err
+= efunc(pc
, "non-zero reserved bits\n");
9774 err
+= efunc(pc
, "invalid register %u\n", rd
);
9776 err
+= efunc(pc
, "cannot write to 0 address\n");
9781 err
+= efunc(pc
, "invalid register %u\n", r1
);
9783 err
+= efunc(pc
, "invalid register %u\n", r2
);
9785 err
+= efunc(pc
, "non-zero reserved bits\n");
9789 err
+= efunc(pc
, "invalid register %u\n", r1
);
9790 if (r2
!= 0 || rd
!= 0)
9791 err
+= efunc(pc
, "non-zero reserved bits\n");
9804 if (label
>= dp
->dtdo_len
) {
9805 err
+= efunc(pc
, "invalid branch target %u\n",
9809 err
+= efunc(pc
, "backward branch to %u\n",
9814 if (r1
!= 0 || r2
!= 0)
9815 err
+= efunc(pc
, "non-zero reserved bits\n");
9817 err
+= efunc(pc
, "invalid register %u\n", rd
);
9821 case DIF_OP_FLUSHTS
:
9822 if (r1
!= 0 || r2
!= 0 || rd
!= 0)
9823 err
+= efunc(pc
, "non-zero reserved bits\n");
9826 if (DIF_INSTR_INTEGER(instr
) >= dp
->dtdo_intlen
) {
9827 err
+= efunc(pc
, "invalid integer ref %u\n",
9828 DIF_INSTR_INTEGER(instr
));
9831 err
+= efunc(pc
, "invalid register %u\n", rd
);
9833 err
+= efunc(pc
, "cannot write to %%r0\n");
9836 if (DIF_INSTR_STRING(instr
) >= dp
->dtdo_strlen
) {
9837 err
+= efunc(pc
, "invalid string ref %u\n",
9838 DIF_INSTR_STRING(instr
));
9841 err
+= efunc(pc
, "invalid register %u\n", rd
);
9843 err
+= efunc(pc
, "cannot write to %%r0\n");
9847 if (r1
> DIF_VAR_ARRAY_MAX
)
9848 err
+= efunc(pc
, "invalid array %u\n", r1
);
9850 err
+= efunc(pc
, "invalid register %u\n", r2
);
9852 err
+= efunc(pc
, "invalid register %u\n", rd
);
9854 err
+= efunc(pc
, "cannot write to %%r0\n");
9861 if (v
< DIF_VAR_OTHER_MIN
|| v
> DIF_VAR_OTHER_MAX
)
9862 err
+= efunc(pc
, "invalid variable %u\n", v
);
9864 err
+= efunc(pc
, "invalid register %u\n", rd
);
9866 err
+= efunc(pc
, "cannot write to %%r0\n");
9873 if (v
< DIF_VAR_OTHER_UBASE
|| v
> DIF_VAR_OTHER_MAX
)
9874 err
+= efunc(pc
, "invalid variable %u\n", v
);
9876 err
+= efunc(pc
, "invalid register %u\n", rd
);
9879 if (subr
> DIF_SUBR_MAX
&&
9880 !(subr
>= DIF_SUBR_APPLE_MIN
&& subr
<= DIF_SUBR_APPLE_MAX
))
9881 err
+= efunc(pc
, "invalid subr %u\n", subr
);
9883 err
+= efunc(pc
, "invalid register %u\n", rd
);
9885 err
+= efunc(pc
, "cannot write to %%r0\n");
9888 case DIF_SUBR_COPYOUT
:
9889 case DIF_SUBR_COPYOUTSTR
:
9890 case DIF_SUBR_KDEBUG_TRACE
:
9891 case DIF_SUBR_KDEBUG_TRACE_STRING
:
9892 case DIF_SUBR_PHYSMEM_READ
:
9893 case DIF_SUBR_PHYSMEM_WRITE
:
9894 dp
->dtdo_destructive
= 1;
9901 if (type
!= DIF_TYPE_STRING
&& type
!= DIF_TYPE_CTF
)
9902 err
+= efunc(pc
, "invalid ref type %u\n", type
);
9904 err
+= efunc(pc
, "invalid register %u\n", r2
);
9906 err
+= efunc(pc
, "invalid register %u\n", rs
);
9909 if (type
!= DIF_TYPE_CTF
)
9910 err
+= efunc(pc
, "invalid val type %u\n", type
);
9912 err
+= efunc(pc
, "invalid register %u\n", r2
);
9914 err
+= efunc(pc
, "invalid register %u\n", rs
);
9918 err
+= efunc(pc
, "invalid register %u\n", r1
);
9919 if (!dtrace_is_valid_ptrauth_key(r2
))
9920 err
+= efunc(pc
, "invalid key\n");
9922 err
+= efunc(pc
, "invalid register %u\n", rd
);
9924 err
+= efunc(pc
, "cannot write to %%r0\n");
9927 err
+= efunc(pc
, "invalid opcode %u\n",
9928 DIF_INSTR_OP(instr
));
9932 if (dp
->dtdo_len
!= 0 &&
9933 DIF_INSTR_OP(dp
->dtdo_buf
[dp
->dtdo_len
- 1]) != DIF_OP_RET
) {
9934 err
+= efunc(dp
->dtdo_len
- 1,
9935 "expected 'ret' as last DIF instruction\n");
9938 if (!(dp
->dtdo_rtype
.dtdt_flags
& (DIF_TF_BYREF
| DIF_TF_BYUREF
))) {
9940 * If we're not returning by reference, the size must be either
9941 * 0 or the size of one of the base types.
9943 switch (dp
->dtdo_rtype
.dtdt_size
) {
9945 case sizeof (uint8_t):
9946 case sizeof (uint16_t):
9947 case sizeof (uint32_t):
9948 case sizeof (uint64_t):
9952 err
+= efunc(dp
->dtdo_len
- 1, "bad return size\n");
9956 for (i
= 0; i
< dp
->dtdo_varlen
&& err
== 0; i
++) {
9957 dtrace_difv_t
*v
= &dp
->dtdo_vartab
[i
], *existing
= NULL
;
9958 dtrace_diftype_t
*vt
, *et
;
9962 if (v
->dtdv_scope
!= DIFV_SCOPE_GLOBAL
&&
9963 v
->dtdv_scope
!= DIFV_SCOPE_THREAD
&&
9964 v
->dtdv_scope
!= DIFV_SCOPE_LOCAL
) {
9965 err
+= efunc(i
, "unrecognized variable scope %d\n",
9970 if (v
->dtdv_kind
!= DIFV_KIND_ARRAY
&&
9971 v
->dtdv_kind
!= DIFV_KIND_SCALAR
) {
9972 err
+= efunc(i
, "unrecognized variable type %d\n",
9977 if ((id
= v
->dtdv_id
) > DIF_VARIABLE_MAX
) {
9978 err
+= efunc(i
, "%d exceeds variable id limit\n", id
);
9982 if (id
< DIF_VAR_OTHER_UBASE
)
9986 * For user-defined variables, we need to check that this
9987 * definition is identical to any previous definition that we
9990 ndx
= id
- DIF_VAR_OTHER_UBASE
;
9992 switch (v
->dtdv_scope
) {
9993 case DIFV_SCOPE_GLOBAL
:
9994 if (maxglobal
== -1 || ndx
> maxglobal
)
9997 if (ndx
< vstate
->dtvs_nglobals
) {
9998 dtrace_statvar_t
*svar
;
10000 if ((svar
= vstate
->dtvs_globals
[ndx
]) != NULL
)
10001 existing
= &svar
->dtsv_var
;
10006 case DIFV_SCOPE_THREAD
:
10007 if (maxtlocal
== -1 || ndx
> maxtlocal
)
10010 if (ndx
< vstate
->dtvs_ntlocals
)
10011 existing
= &vstate
->dtvs_tlocals
[ndx
];
10014 case DIFV_SCOPE_LOCAL
:
10015 if (maxlocal
== -1 || ndx
> maxlocal
)
10017 if (ndx
< vstate
->dtvs_nlocals
) {
10018 dtrace_statvar_t
*svar
;
10020 if ((svar
= vstate
->dtvs_locals
[ndx
]) != NULL
)
10021 existing
= &svar
->dtsv_var
;
10027 vt
= &v
->dtdv_type
;
10029 if (vt
->dtdt_flags
& DIF_TF_BYREF
) {
10030 if (vt
->dtdt_size
== 0) {
10031 err
+= efunc(i
, "zero-sized variable\n");
10035 if ((v
->dtdv_scope
== DIFV_SCOPE_GLOBAL
||
10036 v
->dtdv_scope
== DIFV_SCOPE_LOCAL
) &&
10037 vt
->dtdt_size
> dtrace_statvar_maxsize
) {
10038 err
+= efunc(i
, "oversized by-ref static\n");
10043 if (existing
== NULL
|| existing
->dtdv_id
== 0)
10046 ASSERT(existing
->dtdv_id
== v
->dtdv_id
);
10047 ASSERT(existing
->dtdv_scope
== v
->dtdv_scope
);
10049 if (existing
->dtdv_kind
!= v
->dtdv_kind
)
10050 err
+= efunc(i
, "%d changed variable kind\n", id
);
10052 et
= &existing
->dtdv_type
;
10054 if (vt
->dtdt_flags
!= et
->dtdt_flags
) {
10055 err
+= efunc(i
, "%d changed variable type flags\n", id
);
10059 if (vt
->dtdt_size
!= 0 && vt
->dtdt_size
!= et
->dtdt_size
) {
10060 err
+= efunc(i
, "%d changed variable type size\n", id
);
10065 for (pc
= 0; pc
< dp
->dtdo_len
&& err
== 0; pc
++) {
10066 dif_instr_t instr
= dp
->dtdo_buf
[pc
];
10068 uint_t v
= DIF_INSTR_VAR(instr
);
10069 uint_t op
= DIF_INSTR_OP(instr
);
10076 if (v
> (uint_t
)(DIF_VAR_OTHER_UBASE
+ maxglobal
))
10077 err
+= efunc(pc
, "invalid variable %u\n", v
);
10083 if (v
> (uint_t
)(DIF_VAR_OTHER_UBASE
+ maxtlocal
))
10084 err
+= efunc(pc
, "invalid variable %u\n", v
);
10088 if (v
> (uint_t
)(DIF_VAR_OTHER_UBASE
+ maxlocal
))
10089 err
+= efunc(pc
, "invalid variable %u\n", v
);
10100 * Validate a DTrace DIF object that it is to be used as a helper. Helpers
10101 * are much more constrained than normal DIFOs. Specifically, they may
10104 * 1. Make calls to subroutines other than copyin(), copyinstr() or
10105 * miscellaneous string routines
10106 * 2. Access DTrace variables other than the args[] array, and the
10107 * curthread, pid, ppid, tid, execname, zonename, uid and gid variables.
10108 * 3. Have thread-local variables.
10109 * 4. Have dynamic variables.
10112 dtrace_difo_validate_helper(dtrace_difo_t
*dp
)
10114 int (*efunc
)(uint_t pc
, const char *, ...) = dtrace_difo_err
;
10118 for (pc
= 0; pc
< dp
->dtdo_len
; pc
++) {
10119 dif_instr_t instr
= dp
->dtdo_buf
[pc
];
10121 uint_t v
= DIF_INSTR_VAR(instr
);
10122 uint_t subr
= DIF_INSTR_SUBR(instr
);
10123 uint_t op
= DIF_INSTR_OP(instr
);
10160 case DIF_OP_ALLOCS
:
10178 case DIF_OP_FLUSHTS
:
10185 case DIF_OP_PUSHTR
:
10186 case DIF_OP_PUSHTV
:
10190 if (v
>= DIF_VAR_OTHER_UBASE
)
10193 if (v
>= DIF_VAR_ARG0
&& v
<= DIF_VAR_ARG9
)
10196 if (v
== DIF_VAR_CURTHREAD
|| v
== DIF_VAR_PID
||
10197 v
== DIF_VAR_PPID
|| v
== DIF_VAR_TID
||
10198 v
== DIF_VAR_EXECNAME
|| v
== DIF_VAR_ZONENAME
||
10199 v
== DIF_VAR_UID
|| v
== DIF_VAR_GID
)
10202 err
+= efunc(pc
, "illegal variable %u\n", v
);
10209 err
+= efunc(pc
, "illegal dynamic variable load\n");
10215 err
+= efunc(pc
, "illegal dynamic variable store\n");
10220 case DIF_SUBR_ALLOCA
:
10221 case DIF_SUBR_BCOPY
:
10222 case DIF_SUBR_COPYIN
:
10223 case DIF_SUBR_COPYINTO
:
10224 case DIF_SUBR_COPYINSTR
:
10225 case DIF_SUBR_HTONS
:
10226 case DIF_SUBR_HTONL
:
10227 case DIF_SUBR_HTONLL
:
10228 case DIF_SUBR_INDEX
:
10229 case DIF_SUBR_INET_NTOA
:
10230 case DIF_SUBR_INET_NTOA6
:
10231 case DIF_SUBR_INET_NTOP
:
10232 case DIF_SUBR_JSON
:
10233 case DIF_SUBR_LLTOSTR
:
10234 case DIF_SUBR_NTOHS
:
10235 case DIF_SUBR_NTOHL
:
10236 case DIF_SUBR_NTOHLL
:
10237 case DIF_SUBR_RINDEX
:
10238 case DIF_SUBR_STRCHR
:
10239 case DIF_SUBR_STRTOLL
:
10240 case DIF_SUBR_STRJOIN
:
10241 case DIF_SUBR_STRRCHR
:
10242 case DIF_SUBR_STRSTR
:
10245 err
+= efunc(pc
, "invalid subr %u\n", subr
);
10250 err
+= efunc(pc
, "invalid opcode %u\n",
10251 DIF_INSTR_OP(instr
));
10259 * Returns 1 if the expression in the DIF object can be cached on a per-thread
10263 dtrace_difo_cacheable(dtrace_difo_t
*dp
)
10270 for (i
= 0; i
< dp
->dtdo_varlen
; i
++) {
10271 dtrace_difv_t
*v
= &dp
->dtdo_vartab
[i
];
10273 if (v
->dtdv_scope
!= DIFV_SCOPE_GLOBAL
)
10276 switch (v
->dtdv_id
) {
10277 case DIF_VAR_CURTHREAD
:
10280 case DIF_VAR_EXECNAME
:
10281 case DIF_VAR_ZONENAME
:
10290 * This DIF object may be cacheable. Now we need to look for any
10291 * array loading instructions, any memory loading instructions, or
10292 * any stores to thread-local variables.
10294 for (i
= 0; i
< dp
->dtdo_len
; i
++) {
10295 uint_t op
= DIF_INSTR_OP(dp
->dtdo_buf
[i
]);
10297 if ((op
>= DIF_OP_LDSB
&& op
<= DIF_OP_LDX
) ||
10298 (op
>= DIF_OP_ULDSB
&& op
<= DIF_OP_ULDX
) ||
10299 (op
>= DIF_OP_RLDSB
&& op
<= DIF_OP_RLDX
) ||
10300 op
== DIF_OP_LDGA
|| op
== DIF_OP_STTS
)
10308 dtrace_difo_hold(dtrace_difo_t
*dp
)
10312 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
10315 ASSERT(dp
->dtdo_refcnt
!= 0);
10318 * We need to check this DIF object for references to the variable
10319 * DIF_VAR_VTIMESTAMP.
10321 for (i
= 0; i
< dp
->dtdo_varlen
; i
++) {
10322 dtrace_difv_t
*v
= &dp
->dtdo_vartab
[i
];
10324 if (v
->dtdv_id
!= DIF_VAR_VTIMESTAMP
)
10327 if (dtrace_vtime_references
++ == 0)
10328 dtrace_vtime_enable();
10333 * This routine calculates the dynamic variable chunksize for a given DIF
10334 * object. The calculation is not fool-proof, and can probably be tricked by
10335 * malicious DIF -- but it works for all compiler-generated DIF. Because this
10336 * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail
10337 * if a dynamic variable size exceeds the chunksize.
10340 dtrace_difo_chunksize(dtrace_difo_t
*dp
, dtrace_vstate_t
*vstate
)
10343 dtrace_key_t tupregs
[DIF_DTR_NREGS
+ 2]; /* +2 for thread and id */
10344 const dif_instr_t
*text
= dp
->dtdo_buf
;
10345 uint_t pc
, srd
= 0;
10347 size_t size
, ksize
;
10350 for (pc
= 0; pc
< dp
->dtdo_len
; pc
++) {
10351 dif_instr_t instr
= text
[pc
];
10352 uint_t op
= DIF_INSTR_OP(instr
);
10353 uint_t rd
= DIF_INSTR_RD(instr
);
10354 uint_t r1
= DIF_INSTR_R1(instr
);
10358 dtrace_key_t
*key
= tupregs
;
10362 sval
= dp
->dtdo_inttab
[DIF_INSTR_INTEGER(instr
)];
10367 key
= &tupregs
[DIF_DTR_NREGS
];
10368 key
[0].dttk_size
= 0;
10369 key
[1].dttk_size
= 0;
10371 scope
= DIFV_SCOPE_THREAD
;
10378 if (DIF_INSTR_OP(instr
) == DIF_OP_STTAA
)
10379 key
[nkeys
++].dttk_size
= 0;
10381 key
[nkeys
++].dttk_size
= 0;
10383 if (op
== DIF_OP_STTAA
) {
10384 scope
= DIFV_SCOPE_THREAD
;
10386 scope
= DIFV_SCOPE_GLOBAL
;
10391 case DIF_OP_PUSHTR
:
10392 if (ttop
== DIF_DTR_NREGS
)
10395 if ((srd
== 0 || sval
== 0) && r1
== DIF_TYPE_STRING
) {
10397 * If the register for the size of the "pushtr"
10398 * is %r0 (or the value is 0) and the type is
10399 * a string, we'll use the system-wide default
10402 tupregs
[ttop
++].dttk_size
=
10403 dtrace_strsize_default
;
10408 if (sval
> LONG_MAX
)
10411 tupregs
[ttop
++].dttk_size
= sval
;
10416 case DIF_OP_PUSHTV
:
10417 if (ttop
== DIF_DTR_NREGS
)
10420 tupregs
[ttop
++].dttk_size
= 0;
10423 case DIF_OP_FLUSHTS
:
10440 * We have a dynamic variable allocation; calculate its size.
10442 for (ksize
= 0, i
= 0; i
< nkeys
; i
++)
10443 ksize
+= P2ROUNDUP(key
[i
].dttk_size
, sizeof (uint64_t));
10445 size
= sizeof (dtrace_dynvar_t
);
10446 size
+= sizeof (dtrace_key_t
) * (nkeys
- 1);
10450 * Now we need to determine the size of the stored data.
10452 id
= DIF_INSTR_VAR(instr
);
10454 for (i
= 0; i
< dp
->dtdo_varlen
; i
++) {
10455 dtrace_difv_t
*v
= &dp
->dtdo_vartab
[i
];
10457 if (v
->dtdv_id
== id
&& v
->dtdv_scope
== scope
) {
10458 size
+= v
->dtdv_type
.dtdt_size
;
10463 if (i
== dp
->dtdo_varlen
)
10467 * We have the size. If this is larger than the chunk size
10468 * for our dynamic variable state, reset the chunk size.
10470 size
= P2ROUNDUP(size
, sizeof (uint64_t));
10473 * Before setting the chunk size, check that we're not going
10474 * to set it to a negative value...
10476 if (size
> LONG_MAX
)
10480 * ...and make certain that we didn't badly overflow.
10482 if (size
< ksize
|| size
< sizeof (dtrace_dynvar_t
))
10485 if (size
> vstate
->dtvs_dynvars
.dtds_chunksize
)
10486 vstate
->dtvs_dynvars
.dtds_chunksize
= size
;
10491 dtrace_difo_init(dtrace_difo_t
*dp
, dtrace_vstate_t
*vstate
)
10493 int oldsvars
, osz
, nsz
, otlocals
, ntlocals
;
10496 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
10497 ASSERT(dp
->dtdo_buf
!= NULL
&& dp
->dtdo_len
!= 0);
10499 for (i
= 0; i
< dp
->dtdo_varlen
; i
++) {
10500 dtrace_difv_t
*v
= &dp
->dtdo_vartab
[i
];
10501 dtrace_statvar_t
*svar
;
10502 dtrace_statvar_t
***svarp
= NULL
;
10504 uint8_t scope
= v
->dtdv_scope
;
10505 int *np
= (int *)NULL
;
10507 if ((id
= v
->dtdv_id
) < DIF_VAR_OTHER_UBASE
)
10510 id
-= DIF_VAR_OTHER_UBASE
;
10513 case DIFV_SCOPE_THREAD
:
10514 while (id
>= (uint_t
)(otlocals
= vstate
->dtvs_ntlocals
)) {
10515 dtrace_difv_t
*tlocals
;
10517 if ((ntlocals
= (otlocals
<< 1)) == 0)
10520 osz
= otlocals
* sizeof (dtrace_difv_t
);
10521 nsz
= ntlocals
* sizeof (dtrace_difv_t
);
10523 tlocals
= kmem_zalloc(nsz
, KM_SLEEP
);
10526 bcopy(vstate
->dtvs_tlocals
,
10528 kmem_free(vstate
->dtvs_tlocals
, osz
);
10531 vstate
->dtvs_tlocals
= tlocals
;
10532 vstate
->dtvs_ntlocals
= ntlocals
;
10535 vstate
->dtvs_tlocals
[id
] = *v
;
10538 case DIFV_SCOPE_LOCAL
:
10539 np
= &vstate
->dtvs_nlocals
;
10540 svarp
= &vstate
->dtvs_locals
;
10542 if (v
->dtdv_type
.dtdt_flags
& DIF_TF_BYREF
)
10543 dsize
= (int)NCPU
* (v
->dtdv_type
.dtdt_size
+
10544 sizeof (uint64_t));
10546 dsize
= (int)NCPU
* sizeof (uint64_t);
10550 case DIFV_SCOPE_GLOBAL
:
10551 np
= &vstate
->dtvs_nglobals
;
10552 svarp
= &vstate
->dtvs_globals
;
10554 if (v
->dtdv_type
.dtdt_flags
& DIF_TF_BYREF
)
10555 dsize
= v
->dtdv_type
.dtdt_size
+
10564 while (id
>= (uint_t
)(oldsvars
= *np
)) {
10565 dtrace_statvar_t
**statics
;
10566 int newsvars
, oldsize
, newsize
;
10568 if ((newsvars
= (oldsvars
<< 1)) == 0)
10571 oldsize
= oldsvars
* sizeof (dtrace_statvar_t
*);
10572 newsize
= newsvars
* sizeof (dtrace_statvar_t
*);
10574 statics
= kmem_zalloc(newsize
, KM_SLEEP
);
10576 if (oldsize
!= 0) {
10577 bcopy(*svarp
, statics
, oldsize
);
10578 kmem_free(*svarp
, oldsize
);
10585 if ((svar
= (*svarp
)[id
]) == NULL
) {
10586 svar
= kmem_zalloc(sizeof (dtrace_statvar_t
), KM_SLEEP
);
10587 svar
->dtsv_var
= *v
;
10589 if ((svar
->dtsv_size
= dsize
) != 0) {
10590 svar
->dtsv_data
= (uint64_t)(uintptr_t)
10591 kmem_zalloc(dsize
, KM_SLEEP
);
10594 (*svarp
)[id
] = svar
;
10597 svar
->dtsv_refcnt
++;
10600 dtrace_difo_chunksize(dp
, vstate
);
10601 dtrace_difo_hold(dp
);
10604 static dtrace_difo_t
*
10605 dtrace_difo_duplicate(dtrace_difo_t
*dp
, dtrace_vstate_t
*vstate
)
10607 dtrace_difo_t
*new;
10610 ASSERT(dp
->dtdo_buf
!= NULL
);
10611 ASSERT(dp
->dtdo_refcnt
!= 0);
10613 new = kmem_zalloc(sizeof (dtrace_difo_t
), KM_SLEEP
);
10615 ASSERT(dp
->dtdo_buf
!= NULL
);
10616 sz
= dp
->dtdo_len
* sizeof (dif_instr_t
);
10617 new->dtdo_buf
= kmem_alloc(sz
, KM_SLEEP
);
10618 bcopy(dp
->dtdo_buf
, new->dtdo_buf
, sz
);
10619 new->dtdo_len
= dp
->dtdo_len
;
10621 if (dp
->dtdo_strtab
!= NULL
) {
10622 ASSERT(dp
->dtdo_strlen
!= 0);
10623 new->dtdo_strtab
= kmem_alloc(dp
->dtdo_strlen
, KM_SLEEP
);
10624 bcopy(dp
->dtdo_strtab
, new->dtdo_strtab
, dp
->dtdo_strlen
);
10625 new->dtdo_strlen
= dp
->dtdo_strlen
;
10628 if (dp
->dtdo_inttab
!= NULL
) {
10629 ASSERT(dp
->dtdo_intlen
!= 0);
10630 sz
= dp
->dtdo_intlen
* sizeof (uint64_t);
10631 new->dtdo_inttab
= kmem_alloc(sz
, KM_SLEEP
);
10632 bcopy(dp
->dtdo_inttab
, new->dtdo_inttab
, sz
);
10633 new->dtdo_intlen
= dp
->dtdo_intlen
;
10636 if (dp
->dtdo_vartab
!= NULL
) {
10637 ASSERT(dp
->dtdo_varlen
!= 0);
10638 sz
= dp
->dtdo_varlen
* sizeof (dtrace_difv_t
);
10639 new->dtdo_vartab
= kmem_alloc(sz
, KM_SLEEP
);
10640 bcopy(dp
->dtdo_vartab
, new->dtdo_vartab
, sz
);
10641 new->dtdo_varlen
= dp
->dtdo_varlen
;
10644 dtrace_difo_init(new, vstate
);
10649 dtrace_difo_destroy(dtrace_difo_t
*dp
, dtrace_vstate_t
*vstate
)
10653 ASSERT(dp
->dtdo_refcnt
== 0);
10655 for (i
= 0; i
< dp
->dtdo_varlen
; i
++) {
10656 dtrace_difv_t
*v
= &dp
->dtdo_vartab
[i
];
10657 dtrace_statvar_t
*svar
;
10658 dtrace_statvar_t
**svarp
= NULL
;
10660 uint8_t scope
= v
->dtdv_scope
;
10664 case DIFV_SCOPE_THREAD
:
10667 case DIFV_SCOPE_LOCAL
:
10668 np
= &vstate
->dtvs_nlocals
;
10669 svarp
= vstate
->dtvs_locals
;
10672 case DIFV_SCOPE_GLOBAL
:
10673 np
= &vstate
->dtvs_nglobals
;
10674 svarp
= vstate
->dtvs_globals
;
10681 if ((id
= v
->dtdv_id
) < DIF_VAR_OTHER_UBASE
)
10684 id
-= DIF_VAR_OTHER_UBASE
;
10686 ASSERT(id
< (uint_t
)*np
);
10689 ASSERT(svar
!= NULL
);
10690 ASSERT(svar
->dtsv_refcnt
> 0);
10692 if (--svar
->dtsv_refcnt
> 0)
10695 if (svar
->dtsv_size
!= 0) {
10696 ASSERT(svar
->dtsv_data
!= 0);
10697 kmem_free((void *)(uintptr_t)svar
->dtsv_data
,
10701 kmem_free(svar
, sizeof (dtrace_statvar_t
));
10705 kmem_free(dp
->dtdo_buf
, dp
->dtdo_len
* sizeof (dif_instr_t
));
10706 kmem_free(dp
->dtdo_inttab
, dp
->dtdo_intlen
* sizeof (uint64_t));
10707 kmem_free(dp
->dtdo_strtab
, dp
->dtdo_strlen
);
10708 kmem_free(dp
->dtdo_vartab
, dp
->dtdo_varlen
* sizeof (dtrace_difv_t
));
10710 kmem_free(dp
, sizeof (dtrace_difo_t
));
10714 dtrace_difo_release(dtrace_difo_t
*dp
, dtrace_vstate_t
*vstate
)
10718 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
10719 ASSERT(dp
->dtdo_refcnt
!= 0);
10721 for (i
= 0; i
< dp
->dtdo_varlen
; i
++) {
10722 dtrace_difv_t
*v
= &dp
->dtdo_vartab
[i
];
10724 if (v
->dtdv_id
!= DIF_VAR_VTIMESTAMP
)
10727 ASSERT(dtrace_vtime_references
> 0);
10728 if (--dtrace_vtime_references
== 0)
10729 dtrace_vtime_disable();
10732 if (--dp
->dtdo_refcnt
== 0)
10733 dtrace_difo_destroy(dp
, vstate
);
10737 * DTrace Format Functions
10740 static dtrace_format_t
*
10741 dtrace_format_new(char *str
)
10743 dtrace_format_t
*fmt
= NULL
;
10744 size_t bufsize
= strlen(str
) + 1;
10746 fmt
= kmem_zalloc(sizeof(*fmt
) + bufsize
, KM_SLEEP
);
10748 fmt
->dtf_refcount
= 1;
10749 (void) strlcpy(fmt
->dtf_str
, str
, bufsize
);
10755 dtrace_format_add(dtrace_state_t
*state
, char *str
)
10757 dtrace_format_t
**new;
10760 for (ndx
= 0; ndx
< state
->dts_nformats
; ndx
++) {
10761 if (state
->dts_formats
[ndx
] == NULL
) {
10762 state
->dts_formats
[ndx
] = dtrace_format_new(str
);
10765 else if (strcmp(state
->dts_formats
[ndx
]->dtf_str
, str
) == 0) {
10766 VERIFY(state
->dts_formats
[ndx
]->dtf_refcount
< UINT64_MAX
);
10767 state
->dts_formats
[ndx
]->dtf_refcount
++;
10772 if (state
->dts_nformats
== USHRT_MAX
) {
10774 * This is only likely if a denial-of-service attack is being
10775 * attempted. As such, it's okay to fail silently here.
10781 * For simplicity, we always resize the formats array to be exactly the
10782 * number of formats.
10784 ndx
= state
->dts_nformats
++;
10785 new = kmem_alloc((ndx
+ 1) * sizeof (*state
->dts_formats
), KM_SLEEP
);
10787 if (state
->dts_formats
!= NULL
) {
10789 bcopy(state
->dts_formats
, new, ndx
* sizeof (*state
->dts_formats
));
10790 kmem_free(state
->dts_formats
, ndx
* sizeof (*state
->dts_formats
));
10793 state
->dts_formats
= new;
10794 state
->dts_formats
[ndx
] = dtrace_format_new(str
);
10800 dtrace_format_remove(dtrace_state_t
*state
, uint16_t format
)
10802 dtrace_format_t
*fmt
;
10804 ASSERT(state
->dts_formats
!= NULL
);
10805 ASSERT(format
<= state
->dts_nformats
);
10807 fmt
= state
->dts_formats
[format
- 1];
10809 ASSERT(fmt
!= NULL
);
10810 VERIFY(fmt
->dtf_refcount
> 0);
10812 fmt
->dtf_refcount
--;
10814 if (fmt
->dtf_refcount
== 0) {
10815 kmem_free(fmt
, DTRACE_FORMAT_SIZE(fmt
));
10816 state
->dts_formats
[format
- 1] = NULL
;
10821 dtrace_format_destroy(dtrace_state_t
*state
)
10825 if (state
->dts_nformats
== 0) {
10826 ASSERT(state
->dts_formats
== NULL
);
10830 ASSERT(state
->dts_formats
!= NULL
);
10832 for (i
= 0; i
< state
->dts_nformats
; i
++) {
10833 dtrace_format_t
*fmt
= state
->dts_formats
[i
];
10838 kmem_free(fmt
, DTRACE_FORMAT_SIZE(fmt
));
10841 kmem_free(state
->dts_formats
, state
->dts_nformats
* sizeof (*state
->dts_formats
));
10842 state
->dts_nformats
= 0;
10843 state
->dts_formats
= NULL
;
10847 * DTrace Predicate Functions
10849 static dtrace_predicate_t
*
10850 dtrace_predicate_create(dtrace_difo_t
*dp
)
10852 dtrace_predicate_t
*pred
;
10854 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
10855 ASSERT(dp
->dtdo_refcnt
!= 0);
10857 pred
= kmem_zalloc(sizeof (dtrace_predicate_t
), KM_SLEEP
);
10858 pred
->dtp_difo
= dp
;
10859 pred
->dtp_refcnt
= 1;
10861 if (!dtrace_difo_cacheable(dp
))
10864 if (dtrace_predcache_id
== DTRACE_CACHEIDNONE
) {
10866 * This is only theoretically possible -- we have had 2^32
10867 * cacheable predicates on this machine. We cannot allow any
10868 * more predicates to become cacheable: as unlikely as it is,
10869 * there may be a thread caching a (now stale) predicate cache
10870 * ID. (N.B.: the temptation is being successfully resisted to
10871 * have this cmn_err() "Holy shit -- we executed this code!")
10876 pred
->dtp_cacheid
= dtrace_predcache_id
++;
10882 dtrace_predicate_hold(dtrace_predicate_t
*pred
)
10884 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
10885 ASSERT(pred
->dtp_difo
!= NULL
&& pred
->dtp_difo
->dtdo_refcnt
!= 0);
10886 ASSERT(pred
->dtp_refcnt
> 0);
10888 pred
->dtp_refcnt
++;
10892 dtrace_predicate_release(dtrace_predicate_t
*pred
, dtrace_vstate_t
*vstate
)
10894 dtrace_difo_t
*dp
= pred
->dtp_difo
;
10895 #pragma unused(dp) /* __APPLE__ */
10897 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
10898 ASSERT(dp
!= NULL
&& dp
->dtdo_refcnt
!= 0);
10899 ASSERT(pred
->dtp_refcnt
> 0);
10901 if (--pred
->dtp_refcnt
== 0) {
10902 dtrace_difo_release(pred
->dtp_difo
, vstate
);
10903 kmem_free(pred
, sizeof (dtrace_predicate_t
));
10908 * DTrace Action Description Functions
10910 static dtrace_actdesc_t
*
10911 dtrace_actdesc_create(dtrace_actkind_t kind
, uint32_t ntuple
,
10912 uint64_t uarg
, uint64_t arg
)
10914 dtrace_actdesc_t
*act
;
10916 ASSERT(!DTRACEACT_ISPRINTFLIKE(kind
) || (arg
!= 0 &&
10917 arg
>= KERNELBASE
) || (arg
== 0 && kind
== DTRACEACT_PRINTA
));
10919 act
= kmem_zalloc(sizeof (dtrace_actdesc_t
), KM_SLEEP
);
10920 act
->dtad_kind
= kind
;
10921 act
->dtad_ntuple
= ntuple
;
10922 act
->dtad_uarg
= uarg
;
10923 act
->dtad_arg
= arg
;
10924 act
->dtad_refcnt
= 1;
10930 dtrace_actdesc_hold(dtrace_actdesc_t
*act
)
10932 ASSERT(act
->dtad_refcnt
>= 1);
10933 act
->dtad_refcnt
++;
10937 dtrace_actdesc_release(dtrace_actdesc_t
*act
, dtrace_vstate_t
*vstate
)
10939 dtrace_actkind_t kind
= act
->dtad_kind
;
10942 ASSERT(act
->dtad_refcnt
>= 1);
10944 if (--act
->dtad_refcnt
!= 0)
10947 if ((dp
= act
->dtad_difo
) != NULL
)
10948 dtrace_difo_release(dp
, vstate
);
10950 if (DTRACEACT_ISPRINTFLIKE(kind
)) {
10951 char *str
= (char *)(uintptr_t)act
->dtad_arg
;
10953 ASSERT((str
!= NULL
&& (uintptr_t)str
>= KERNELBASE
) ||
10954 (str
== NULL
&& act
->dtad_kind
== DTRACEACT_PRINTA
));
10957 kmem_free(str
, strlen(str
) + 1);
10960 kmem_free(act
, sizeof (dtrace_actdesc_t
));
10964 * DTrace ECB Functions
10966 static dtrace_ecb_t
*
10967 dtrace_ecb_add(dtrace_state_t
*state
, dtrace_probe_t
*probe
)
10970 dtrace_epid_t epid
;
10972 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
10974 ecb
= kmem_zalloc(sizeof (dtrace_ecb_t
), KM_SLEEP
);
10975 ecb
->dte_predicate
= NULL
;
10976 ecb
->dte_probe
= probe
;
10979 * The default size is the size of the default action: recording
10982 ecb
->dte_size
= ecb
->dte_needed
= sizeof (dtrace_rechdr_t
);
10983 ecb
->dte_alignment
= sizeof (dtrace_epid_t
);
10985 epid
= state
->dts_epid
++;
10987 if (epid
- 1 >= (dtrace_epid_t
)state
->dts_necbs
) {
10988 dtrace_ecb_t
**oecbs
= state
->dts_ecbs
, **ecbs
;
10989 int necbs
= state
->dts_necbs
<< 1;
10991 ASSERT(epid
== (dtrace_epid_t
)state
->dts_necbs
+ 1);
10994 ASSERT(oecbs
== NULL
);
10998 ecbs
= kmem_zalloc(necbs
* sizeof (*ecbs
), KM_SLEEP
);
11001 bcopy(oecbs
, ecbs
, state
->dts_necbs
* sizeof (*ecbs
));
11003 dtrace_membar_producer();
11004 state
->dts_ecbs
= ecbs
;
11006 if (oecbs
!= NULL
) {
11008 * If this state is active, we must dtrace_sync()
11009 * before we can free the old dts_ecbs array: we're
11010 * coming in hot, and there may be active ring
11011 * buffer processing (which indexes into the dts_ecbs
11012 * array) on another CPU.
11014 if (state
->dts_activity
!= DTRACE_ACTIVITY_INACTIVE
)
11017 kmem_free(oecbs
, state
->dts_necbs
* sizeof (*ecbs
));
11020 dtrace_membar_producer();
11021 state
->dts_necbs
= necbs
;
11024 ecb
->dte_state
= state
;
11026 ASSERT(state
->dts_ecbs
[epid
- 1] == NULL
);
11027 dtrace_membar_producer();
11028 state
->dts_ecbs
[(ecb
->dte_epid
= epid
) - 1] = ecb
;
11034 dtrace_ecb_enable(dtrace_ecb_t
*ecb
)
11036 dtrace_probe_t
*probe
= ecb
->dte_probe
;
11038 LCK_MTX_ASSERT(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
11039 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
11040 ASSERT(ecb
->dte_next
== NULL
);
11042 if (probe
== NULL
) {
11044 * This is the NULL probe -- there's nothing to do.
11049 probe
->dtpr_provider
->dtpv_ecb_count
++;
11050 if (probe
->dtpr_ecb
== NULL
) {
11051 dtrace_provider_t
*prov
= probe
->dtpr_provider
;
11054 * We're the first ECB on this probe.
11056 probe
->dtpr_ecb
= probe
->dtpr_ecb_last
= ecb
;
11058 if (ecb
->dte_predicate
!= NULL
)
11059 probe
->dtpr_predcache
= ecb
->dte_predicate
->dtp_cacheid
;
11061 return (prov
->dtpv_pops
.dtps_enable(prov
->dtpv_arg
,
11062 probe
->dtpr_id
, probe
->dtpr_arg
));
11065 * This probe is already active. Swing the last pointer to
11066 * point to the new ECB, and issue a dtrace_sync() to assure
11067 * that all CPUs have seen the change.
11069 ASSERT(probe
->dtpr_ecb_last
!= NULL
);
11070 probe
->dtpr_ecb_last
->dte_next
= ecb
;
11071 probe
->dtpr_ecb_last
= ecb
;
11072 probe
->dtpr_predcache
= 0;
11080 dtrace_ecb_resize(dtrace_ecb_t
*ecb
)
11082 dtrace_action_t
*act
;
11083 uint32_t curneeded
= UINT32_MAX
;
11084 uint32_t aggbase
= UINT32_MAX
;
11087 * If we record anything, we always record the dtrace_rechdr_t. (And
11088 * we always record it first.)
11090 ecb
->dte_size
= sizeof (dtrace_rechdr_t
);
11091 ecb
->dte_alignment
= sizeof (dtrace_epid_t
);
11093 for (act
= ecb
->dte_action
; act
!= NULL
; act
= act
->dta_next
) {
11094 dtrace_recdesc_t
*rec
= &act
->dta_rec
;
11095 ASSERT(rec
->dtrd_size
> 0 || rec
->dtrd_alignment
== 1);
11097 ecb
->dte_alignment
= MAX(ecb
->dte_alignment
, rec
->dtrd_alignment
);
11099 if (DTRACEACT_ISAGG(act
->dta_kind
)) {
11100 dtrace_aggregation_t
*agg
= (dtrace_aggregation_t
*)act
;
11102 ASSERT(rec
->dtrd_size
!= 0);
11103 ASSERT(agg
->dtag_first
!= NULL
);
11104 ASSERT(act
->dta_prev
->dta_intuple
);
11105 ASSERT(aggbase
!= UINT32_MAX
);
11106 ASSERT(curneeded
!= UINT32_MAX
);
11108 agg
->dtag_base
= aggbase
;
11109 curneeded
= P2ROUNDUP(curneeded
, rec
->dtrd_alignment
);
11110 rec
->dtrd_offset
= curneeded
;
11111 if (curneeded
+ rec
->dtrd_size
< curneeded
)
11113 curneeded
+= rec
->dtrd_size
;
11114 ecb
->dte_needed
= MAX(ecb
->dte_needed
, curneeded
);
11116 aggbase
= UINT32_MAX
;
11117 curneeded
= UINT32_MAX
;
11118 } else if (act
->dta_intuple
) {
11119 if (curneeded
== UINT32_MAX
) {
11121 * This is the first record in a tuple. Align
11122 * curneeded to be at offset 4 in an 8-byte
11125 ASSERT(act
->dta_prev
== NULL
|| !act
->dta_prev
->dta_intuple
);
11126 ASSERT(aggbase
== UINT32_MAX
);
11128 curneeded
= P2PHASEUP(ecb
->dte_size
,
11129 sizeof (uint64_t), sizeof (dtrace_aggid_t
));
11131 aggbase
= curneeded
- sizeof (dtrace_aggid_t
);
11132 ASSERT(IS_P2ALIGNED(aggbase
,
11133 sizeof (uint64_t)));
11136 curneeded
= P2ROUNDUP(curneeded
, rec
->dtrd_alignment
);
11137 rec
->dtrd_offset
= curneeded
;
11138 curneeded
+= rec
->dtrd_size
;
11139 if (curneeded
+ rec
->dtrd_size
< curneeded
)
11142 /* tuples must be followed by an aggregation */
11143 ASSERT(act
->dta_prev
== NULL
|| !act
->dta_prev
->dta_intuple
);
11144 ecb
->dte_size
= P2ROUNDUP(ecb
->dte_size
, rec
->dtrd_alignment
);
11145 rec
->dtrd_offset
= ecb
->dte_size
;
11146 if (ecb
->dte_size
+ rec
->dtrd_size
< ecb
->dte_size
)
11148 ecb
->dte_size
+= rec
->dtrd_size
;
11149 ecb
->dte_needed
= MAX(ecb
->dte_needed
, ecb
->dte_size
);
11153 if ((act
= ecb
->dte_action
) != NULL
&&
11154 !(act
->dta_kind
== DTRACEACT_SPECULATE
&& act
->dta_next
== NULL
) &&
11155 ecb
->dte_size
== sizeof (dtrace_rechdr_t
)) {
11157 * If the size is still sizeof (dtrace_rechdr_t), then all
11158 * actions store no data; set the size to 0.
11163 ecb
->dte_size
= P2ROUNDUP(ecb
->dte_size
, sizeof (dtrace_epid_t
));
11164 ecb
->dte_needed
= P2ROUNDUP(ecb
->dte_needed
, (sizeof (dtrace_epid_t
)));
11165 ecb
->dte_state
->dts_needed
= MAX(ecb
->dte_state
->dts_needed
, ecb
->dte_needed
);
11169 static dtrace_action_t
*
11170 dtrace_ecb_aggregation_create(dtrace_ecb_t
*ecb
, dtrace_actdesc_t
*desc
)
11172 dtrace_aggregation_t
*agg
;
11173 size_t size
= sizeof (uint64_t);
11174 int ntuple
= desc
->dtad_ntuple
;
11175 dtrace_action_t
*act
;
11176 dtrace_recdesc_t
*frec
;
11177 dtrace_aggid_t aggid
;
11178 dtrace_state_t
*state
= ecb
->dte_state
;
11180 agg
= kmem_zalloc(sizeof (dtrace_aggregation_t
), KM_SLEEP
);
11181 agg
->dtag_ecb
= ecb
;
11183 ASSERT(DTRACEACT_ISAGG(desc
->dtad_kind
));
11185 switch (desc
->dtad_kind
) {
11186 case DTRACEAGG_MIN
:
11187 agg
->dtag_initial
= INT64_MAX
;
11188 agg
->dtag_aggregate
= dtrace_aggregate_min
;
11191 case DTRACEAGG_MAX
:
11192 agg
->dtag_initial
= INT64_MIN
;
11193 agg
->dtag_aggregate
= dtrace_aggregate_max
;
11196 case DTRACEAGG_COUNT
:
11197 agg
->dtag_aggregate
= dtrace_aggregate_count
;
11200 case DTRACEAGG_QUANTIZE
:
11201 agg
->dtag_aggregate
= dtrace_aggregate_quantize
;
11202 size
= (((sizeof (uint64_t) * NBBY
) - 1) * 2 + 1) *
11206 case DTRACEAGG_LQUANTIZE
: {
11207 uint16_t step
= DTRACE_LQUANTIZE_STEP(desc
->dtad_arg
);
11208 uint16_t levels
= DTRACE_LQUANTIZE_LEVELS(desc
->dtad_arg
);
11210 agg
->dtag_initial
= desc
->dtad_arg
;
11211 agg
->dtag_aggregate
= dtrace_aggregate_lquantize
;
11213 if (step
== 0 || levels
== 0)
11216 size
= levels
* sizeof (uint64_t) + 3 * sizeof (uint64_t);
11220 case DTRACEAGG_LLQUANTIZE
: {
11221 uint16_t factor
= DTRACE_LLQUANTIZE_FACTOR(desc
->dtad_arg
);
11222 uint16_t low
= DTRACE_LLQUANTIZE_LOW(desc
->dtad_arg
);
11223 uint16_t high
= DTRACE_LLQUANTIZE_HIGH(desc
->dtad_arg
);
11224 uint16_t nsteps
= DTRACE_LLQUANTIZE_NSTEP(desc
->dtad_arg
);
11227 agg
->dtag_initial
= desc
->dtad_arg
;
11228 agg
->dtag_aggregate
= dtrace_aggregate_llquantize
;
11230 if (factor
< 2 || low
>= high
|| nsteps
< factor
)
11234 * Now check that the number of steps evenly divides a power
11235 * of the factor. (This assures both integer bucket size and
11236 * linearity within each magnitude.)
11238 for (v
= factor
; v
< nsteps
; v
*= factor
)
11241 if ((v
% nsteps
) || (nsteps
% factor
))
11244 size
= (dtrace_aggregate_llquantize_bucket(factor
, low
, high
, nsteps
, INT64_MAX
) + 2) * sizeof (uint64_t);
11248 case DTRACEAGG_AVG
:
11249 agg
->dtag_aggregate
= dtrace_aggregate_avg
;
11250 size
= sizeof (uint64_t) * 2;
11253 case DTRACEAGG_STDDEV
:
11254 agg
->dtag_aggregate
= dtrace_aggregate_stddev
;
11255 size
= sizeof (uint64_t) * 4;
11258 case DTRACEAGG_SUM
:
11259 agg
->dtag_aggregate
= dtrace_aggregate_sum
;
11266 agg
->dtag_action
.dta_rec
.dtrd_size
= size
;
11272 * We must make sure that we have enough actions for the n-tuple.
11274 for (act
= ecb
->dte_action_last
; act
!= NULL
; act
= act
->dta_prev
) {
11275 if (DTRACEACT_ISAGG(act
->dta_kind
))
11278 if (--ntuple
== 0) {
11280 * This is the action with which our n-tuple begins.
11282 agg
->dtag_first
= act
;
11288 * This n-tuple is short by ntuple elements. Return failure.
11290 ASSERT(ntuple
!= 0);
11292 kmem_free(agg
, sizeof (dtrace_aggregation_t
));
11297 * If the last action in the tuple has a size of zero, it's actually
11298 * an expression argument for the aggregating action.
11300 ASSERT(ecb
->dte_action_last
!= NULL
);
11301 act
= ecb
->dte_action_last
;
11303 if (act
->dta_kind
== DTRACEACT_DIFEXPR
) {
11304 ASSERT(act
->dta_difo
!= NULL
);
11306 if (act
->dta_difo
->dtdo_rtype
.dtdt_size
== 0)
11307 agg
->dtag_hasarg
= 1;
11311 * We need to allocate an id for this aggregation.
11313 aggid
= (dtrace_aggid_t
)(uintptr_t)vmem_alloc(state
->dts_aggid_arena
, 1,
11314 VM_BESTFIT
| VM_SLEEP
);
11316 if (aggid
- 1 >= (dtrace_aggid_t
)state
->dts_naggregations
) {
11317 dtrace_aggregation_t
**oaggs
= state
->dts_aggregations
;
11318 dtrace_aggregation_t
**aggs
;
11319 int naggs
= state
->dts_naggregations
<< 1;
11320 int onaggs
= state
->dts_naggregations
;
11322 ASSERT(aggid
== (dtrace_aggid_t
)state
->dts_naggregations
+ 1);
11325 ASSERT(oaggs
== NULL
);
11329 aggs
= kmem_zalloc(naggs
* sizeof (*aggs
), KM_SLEEP
);
11331 if (oaggs
!= NULL
) {
11332 bcopy(oaggs
, aggs
, onaggs
* sizeof (*aggs
));
11333 kmem_free(oaggs
, onaggs
* sizeof (*aggs
));
11336 state
->dts_aggregations
= aggs
;
11337 state
->dts_naggregations
= naggs
;
11340 ASSERT(state
->dts_aggregations
[aggid
- 1] == NULL
);
11341 state
->dts_aggregations
[(agg
->dtag_id
= aggid
) - 1] = agg
;
11343 frec
= &agg
->dtag_first
->dta_rec
;
11344 if (frec
->dtrd_alignment
< sizeof (dtrace_aggid_t
))
11345 frec
->dtrd_alignment
= sizeof (dtrace_aggid_t
);
11347 for (act
= agg
->dtag_first
; act
!= NULL
; act
= act
->dta_next
) {
11348 ASSERT(!act
->dta_intuple
);
11349 act
->dta_intuple
= 1;
11352 return (&agg
->dtag_action
);
11356 dtrace_ecb_aggregation_destroy(dtrace_ecb_t
*ecb
, dtrace_action_t
*act
)
11358 dtrace_aggregation_t
*agg
= (dtrace_aggregation_t
*)act
;
11359 dtrace_state_t
*state
= ecb
->dte_state
;
11360 dtrace_aggid_t aggid
= agg
->dtag_id
;
11362 ASSERT(DTRACEACT_ISAGG(act
->dta_kind
));
11363 vmem_free(state
->dts_aggid_arena
, (void *)(uintptr_t)aggid
, 1);
11365 ASSERT(state
->dts_aggregations
[aggid
- 1] == agg
);
11366 state
->dts_aggregations
[aggid
- 1] = NULL
;
11368 kmem_free(agg
, sizeof (dtrace_aggregation_t
));
11372 dtrace_ecb_action_add(dtrace_ecb_t
*ecb
, dtrace_actdesc_t
*desc
)
11374 dtrace_action_t
*action
, *last
;
11375 dtrace_difo_t
*dp
= desc
->dtad_difo
;
11376 uint32_t size
= 0, align
= sizeof (uint8_t), mask
;
11377 uint16_t format
= 0;
11378 dtrace_recdesc_t
*rec
;
11379 dtrace_state_t
*state
= ecb
->dte_state
;
11380 dtrace_optval_t
*opt
= state
->dts_options
;
11381 dtrace_optval_t nframes
=0, strsize
;
11382 uint64_t arg
= desc
->dtad_arg
;
11384 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
11385 ASSERT(ecb
->dte_action
== NULL
|| ecb
->dte_action
->dta_refcnt
== 1);
11387 if (DTRACEACT_ISAGG(desc
->dtad_kind
)) {
11389 * If this is an aggregating action, there must be neither
11390 * a speculate nor a commit on the action chain.
11392 dtrace_action_t
*act
;
11394 for (act
= ecb
->dte_action
; act
!= NULL
; act
= act
->dta_next
) {
11395 if (act
->dta_kind
== DTRACEACT_COMMIT
)
11398 if (act
->dta_kind
== DTRACEACT_SPECULATE
)
11402 action
= dtrace_ecb_aggregation_create(ecb
, desc
);
11404 if (action
== NULL
)
11407 if (DTRACEACT_ISDESTRUCTIVE(desc
->dtad_kind
) ||
11408 (desc
->dtad_kind
== DTRACEACT_DIFEXPR
&&
11409 dp
!= NULL
&& dp
->dtdo_destructive
)) {
11410 state
->dts_destructive
= 1;
11413 switch (desc
->dtad_kind
) {
11414 case DTRACEACT_PRINTF
:
11415 case DTRACEACT_PRINTA
:
11416 case DTRACEACT_SYSTEM
:
11417 case DTRACEACT_FREOPEN
:
11418 case DTRACEACT_DIFEXPR
:
11420 * We know that our arg is a string -- turn it into a
11424 ASSERT(desc
->dtad_kind
== DTRACEACT_PRINTA
||
11425 desc
->dtad_kind
== DTRACEACT_DIFEXPR
);
11429 ASSERT(arg
> KERNELBASE
);
11430 format
= dtrace_format_add(state
,
11431 (char *)(uintptr_t)arg
);
11435 case DTRACEACT_LIBACT
:
11436 case DTRACEACT_TRACEMEM
:
11437 case DTRACEACT_TRACEMEM_DYNSIZE
:
11438 case DTRACEACT_APPLEBINARY
: /* __APPLE__ */
11442 if ((size
= dp
->dtdo_rtype
.dtdt_size
) != 0)
11445 if (dp
->dtdo_rtype
.dtdt_kind
== DIF_TYPE_STRING
) {
11446 if (!(dp
->dtdo_rtype
.dtdt_flags
& DIF_TF_BYREF
))
11449 size
= opt
[DTRACEOPT_STRSIZE
];
11454 case DTRACEACT_STACK
:
11455 if ((nframes
= arg
) == 0) {
11456 nframes
= opt
[DTRACEOPT_STACKFRAMES
];
11457 ASSERT(nframes
> 0);
11461 size
= nframes
* sizeof (pc_t
);
11464 case DTRACEACT_JSTACK
:
11465 if ((strsize
= DTRACE_USTACK_STRSIZE(arg
)) == 0)
11466 strsize
= opt
[DTRACEOPT_JSTACKSTRSIZE
];
11468 if ((nframes
= DTRACE_USTACK_NFRAMES(arg
)) == 0)
11469 nframes
= opt
[DTRACEOPT_JSTACKFRAMES
];
11471 arg
= DTRACE_USTACK_ARG(nframes
, strsize
);
11474 case DTRACEACT_USTACK
:
11475 if (desc
->dtad_kind
!= DTRACEACT_JSTACK
&&
11476 (nframes
= DTRACE_USTACK_NFRAMES(arg
)) == 0) {
11477 strsize
= DTRACE_USTACK_STRSIZE(arg
);
11478 nframes
= opt
[DTRACEOPT_USTACKFRAMES
];
11479 ASSERT(nframes
> 0);
11480 arg
= DTRACE_USTACK_ARG(nframes
, strsize
);
11484 * Save a slot for the pid.
11486 size
= (nframes
+ 1) * sizeof (uint64_t);
11487 size
+= DTRACE_USTACK_STRSIZE(arg
);
11488 size
= P2ROUNDUP(size
, (uint32_t)(sizeof (uintptr_t)));
11492 case DTRACEACT_SYM
:
11493 case DTRACEACT_MOD
:
11494 if (dp
== NULL
|| ((size
= dp
->dtdo_rtype
.dtdt_size
) !=
11495 sizeof (uint64_t)) ||
11496 (dp
->dtdo_rtype
.dtdt_flags
& DIF_TF_BYREF
))
11500 case DTRACEACT_USYM
:
11501 case DTRACEACT_UMOD
:
11502 case DTRACEACT_UADDR
:
11504 (dp
->dtdo_rtype
.dtdt_size
!= sizeof (uint64_t)) ||
11505 (dp
->dtdo_rtype
.dtdt_flags
& DIF_TF_BYREF
))
11509 * We have a slot for the pid, plus a slot for the
11510 * argument. To keep things simple (aligned with
11511 * bitness-neutral sizing), we store each as a 64-bit
11514 size
= 2 * sizeof (uint64_t);
11517 case DTRACEACT_STOP
:
11518 case DTRACEACT_BREAKPOINT
:
11519 case DTRACEACT_PANIC
:
11522 case DTRACEACT_CHILL
:
11523 case DTRACEACT_DISCARD
:
11524 case DTRACEACT_RAISE
:
11525 case DTRACEACT_PIDRESUME
: /* __APPLE__ */
11530 case DTRACEACT_EXIT
:
11532 (size
= dp
->dtdo_rtype
.dtdt_size
) != sizeof (int) ||
11533 (dp
->dtdo_rtype
.dtdt_flags
& DIF_TF_BYREF
))
11537 case DTRACEACT_SPECULATE
:
11538 if (ecb
->dte_size
> sizeof (dtrace_rechdr_t
))
11544 state
->dts_speculates
= 1;
11547 case DTRACEACT_COMMIT
: {
11548 dtrace_action_t
*act
= ecb
->dte_action
;
11550 for (; act
!= NULL
; act
= act
->dta_next
) {
11551 if (act
->dta_kind
== DTRACEACT_COMMIT
)
11564 if (size
!= 0 || desc
->dtad_kind
== DTRACEACT_SPECULATE
) {
11566 * If this is a data-storing action or a speculate,
11567 * we must be sure that there isn't a commit on the
11570 dtrace_action_t
*act
= ecb
->dte_action
;
11572 for (; act
!= NULL
; act
= act
->dta_next
) {
11573 if (act
->dta_kind
== DTRACEACT_COMMIT
)
11578 action
= kmem_zalloc(sizeof (dtrace_action_t
), KM_SLEEP
);
11579 action
->dta_rec
.dtrd_size
= size
;
11582 action
->dta_refcnt
= 1;
11583 rec
= &action
->dta_rec
;
11584 size
= rec
->dtrd_size
;
11586 for (mask
= sizeof (uint64_t) - 1; size
!= 0 && mask
> 0; mask
>>= 1) {
11587 if (!(size
& mask
)) {
11593 action
->dta_kind
= desc
->dtad_kind
;
11595 if ((action
->dta_difo
= dp
) != NULL
)
11596 dtrace_difo_hold(dp
);
11598 rec
->dtrd_action
= action
->dta_kind
;
11599 rec
->dtrd_arg
= arg
;
11600 rec
->dtrd_uarg
= desc
->dtad_uarg
;
11601 rec
->dtrd_alignment
= (uint16_t)align
;
11602 rec
->dtrd_format
= format
;
11604 if ((last
= ecb
->dte_action_last
) != NULL
) {
11605 ASSERT(ecb
->dte_action
!= NULL
);
11606 action
->dta_prev
= last
;
11607 last
->dta_next
= action
;
11609 ASSERT(ecb
->dte_action
== NULL
);
11610 ecb
->dte_action
= action
;
11613 ecb
->dte_action_last
= action
;
11619 dtrace_ecb_action_remove(dtrace_ecb_t
*ecb
)
11621 dtrace_action_t
*act
= ecb
->dte_action
, *next
;
11622 dtrace_vstate_t
*vstate
= &ecb
->dte_state
->dts_vstate
;
11626 if (act
!= NULL
&& act
->dta_refcnt
> 1) {
11627 ASSERT(act
->dta_next
== NULL
|| act
->dta_next
->dta_refcnt
== 1);
11630 for (; act
!= NULL
; act
= next
) {
11631 next
= act
->dta_next
;
11632 ASSERT(next
!= NULL
|| act
== ecb
->dte_action_last
);
11633 ASSERT(act
->dta_refcnt
== 1);
11635 if ((format
= act
->dta_rec
.dtrd_format
) != 0)
11636 dtrace_format_remove(ecb
->dte_state
, format
);
11638 if ((dp
= act
->dta_difo
) != NULL
)
11639 dtrace_difo_release(dp
, vstate
);
11641 if (DTRACEACT_ISAGG(act
->dta_kind
)) {
11642 dtrace_ecb_aggregation_destroy(ecb
, act
);
11644 kmem_free(act
, sizeof (dtrace_action_t
));
11649 ecb
->dte_action
= NULL
;
11650 ecb
->dte_action_last
= NULL
;
11655 dtrace_ecb_disable(dtrace_ecb_t
*ecb
)
11658 * We disable the ECB by removing it from its probe.
11660 dtrace_ecb_t
*pecb
, *prev
= NULL
;
11661 dtrace_probe_t
*probe
= ecb
->dte_probe
;
11663 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
11665 if (probe
== NULL
) {
11667 * This is the NULL probe; there is nothing to disable.
11672 for (pecb
= probe
->dtpr_ecb
; pecb
!= NULL
; pecb
= pecb
->dte_next
) {
11678 ASSERT(pecb
!= NULL
);
11680 if (prev
== NULL
) {
11681 probe
->dtpr_ecb
= ecb
->dte_next
;
11683 prev
->dte_next
= ecb
->dte_next
;
11686 if (ecb
== probe
->dtpr_ecb_last
) {
11687 ASSERT(ecb
->dte_next
== NULL
);
11688 probe
->dtpr_ecb_last
= prev
;
11691 probe
->dtpr_provider
->dtpv_ecb_count
--;
11693 * The ECB has been disconnected from the probe; now sync to assure
11694 * that all CPUs have seen the change before returning.
11698 if (probe
->dtpr_ecb
== NULL
) {
11700 * That was the last ECB on the probe; clear the predicate
11701 * cache ID for the probe, disable it and sync one more time
11702 * to assure that we'll never hit it again.
11704 dtrace_provider_t
*prov
= probe
->dtpr_provider
;
11706 ASSERT(ecb
->dte_next
== NULL
);
11707 ASSERT(probe
->dtpr_ecb_last
== NULL
);
11708 probe
->dtpr_predcache
= DTRACE_CACHEIDNONE
;
11709 prov
->dtpv_pops
.dtps_disable(prov
->dtpv_arg
,
11710 probe
->dtpr_id
, probe
->dtpr_arg
);
11714 * There is at least one ECB remaining on the probe. If there
11715 * is _exactly_ one, set the probe's predicate cache ID to be
11716 * the predicate cache ID of the remaining ECB.
11718 ASSERT(probe
->dtpr_ecb_last
!= NULL
);
11719 ASSERT(probe
->dtpr_predcache
== DTRACE_CACHEIDNONE
);
11721 if (probe
->dtpr_ecb
== probe
->dtpr_ecb_last
) {
11722 dtrace_predicate_t
*p
= probe
->dtpr_ecb
->dte_predicate
;
11724 ASSERT(probe
->dtpr_ecb
->dte_next
== NULL
);
11727 probe
->dtpr_predcache
= p
->dtp_cacheid
;
11730 ecb
->dte_next
= NULL
;
11735 dtrace_ecb_destroy(dtrace_ecb_t
*ecb
)
11737 dtrace_state_t
*state
= ecb
->dte_state
;
11738 dtrace_vstate_t
*vstate
= &state
->dts_vstate
;
11739 dtrace_predicate_t
*pred
;
11740 dtrace_epid_t epid
= ecb
->dte_epid
;
11742 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
11743 ASSERT(ecb
->dte_next
== NULL
);
11744 ASSERT(ecb
->dte_probe
== NULL
|| ecb
->dte_probe
->dtpr_ecb
!= ecb
);
11746 if ((pred
= ecb
->dte_predicate
) != NULL
)
11747 dtrace_predicate_release(pred
, vstate
);
11749 dtrace_ecb_action_remove(ecb
);
11751 ASSERT(state
->dts_ecbs
[epid
- 1] == ecb
);
11752 state
->dts_ecbs
[epid
- 1] = NULL
;
11754 kmem_free(ecb
, sizeof (dtrace_ecb_t
));
11757 static dtrace_ecb_t
*
11758 dtrace_ecb_create(dtrace_state_t
*state
, dtrace_probe_t
*probe
,
11759 dtrace_enabling_t
*enab
)
11762 dtrace_predicate_t
*pred
;
11763 dtrace_actdesc_t
*act
;
11764 dtrace_provider_t
*prov
;
11765 dtrace_ecbdesc_t
*desc
= enab
->dten_current
;
11767 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
11768 ASSERT(state
!= NULL
);
11770 ecb
= dtrace_ecb_add(state
, probe
);
11771 ecb
->dte_uarg
= desc
->dted_uarg
;
11773 if ((pred
= desc
->dted_pred
.dtpdd_predicate
) != NULL
) {
11774 dtrace_predicate_hold(pred
);
11775 ecb
->dte_predicate
= pred
;
11778 if (probe
!= NULL
) {
11780 * If the provider shows more leg than the consumer is old
11781 * enough to see, we need to enable the appropriate implicit
11782 * predicate bits to prevent the ecb from activating at
11785 * Providers specifying DTRACE_PRIV_USER at register time
11786 * are stating that they need the /proc-style privilege
11787 * model to be enforced, and this is what DTRACE_COND_OWNER
11788 * and DTRACE_COND_ZONEOWNER will then do at probe time.
11790 prov
= probe
->dtpr_provider
;
11791 if (!(state
->dts_cred
.dcr_visible
& DTRACE_CRV_ALLPROC
) &&
11792 (prov
->dtpv_priv
.dtpp_flags
& DTRACE_PRIV_USER
))
11793 ecb
->dte_cond
|= DTRACE_COND_OWNER
;
11795 if (!(state
->dts_cred
.dcr_visible
& DTRACE_CRV_ALLZONE
) &&
11796 (prov
->dtpv_priv
.dtpp_flags
& DTRACE_PRIV_USER
))
11797 ecb
->dte_cond
|= DTRACE_COND_ZONEOWNER
;
11800 * If the provider shows us kernel innards and the user
11801 * is lacking sufficient privilege, enable the
11802 * DTRACE_COND_USERMODE implicit predicate.
11804 if (!(state
->dts_cred
.dcr_visible
& DTRACE_CRV_KERNEL
) &&
11805 (prov
->dtpv_priv
.dtpp_flags
& DTRACE_PRIV_KERNEL
))
11806 ecb
->dte_cond
|= DTRACE_COND_USERMODE
;
11809 if (dtrace_ecb_create_cache
!= NULL
) {
11811 * If we have a cached ecb, we'll use its action list instead
11812 * of creating our own (saving both time and space).
11814 dtrace_ecb_t
*cached
= dtrace_ecb_create_cache
;
11815 dtrace_action_t
*act_if
= cached
->dte_action
;
11817 if (act_if
!= NULL
) {
11818 ASSERT(act_if
->dta_refcnt
> 0);
11819 act_if
->dta_refcnt
++;
11820 ecb
->dte_action
= act_if
;
11821 ecb
->dte_action_last
= cached
->dte_action_last
;
11822 ecb
->dte_needed
= cached
->dte_needed
;
11823 ecb
->dte_size
= cached
->dte_size
;
11824 ecb
->dte_alignment
= cached
->dte_alignment
;
11830 for (act
= desc
->dted_action
; act
!= NULL
; act
= act
->dtad_next
) {
11831 if ((enab
->dten_error
= dtrace_ecb_action_add(ecb
, act
)) != 0) {
11832 dtrace_ecb_destroy(ecb
);
11837 if ((enab
->dten_error
= dtrace_ecb_resize(ecb
)) != 0) {
11838 dtrace_ecb_destroy(ecb
);
11842 return (dtrace_ecb_create_cache
= ecb
);
11846 dtrace_ecb_create_enable(dtrace_probe_t
*probe
, void *arg1
, void *arg2
)
11849 dtrace_enabling_t
*enab
= arg1
;
11850 dtrace_ecbdesc_t
*ep
= arg2
;
11851 dtrace_state_t
*state
= enab
->dten_vstate
->dtvs_state
;
11853 ASSERT(state
!= NULL
);
11855 if (probe
!= NULL
&& ep
!= NULL
&& probe
->dtpr_gen
< ep
->dted_probegen
) {
11857 * This probe was created in a generation for which this
11858 * enabling has previously created ECBs; we don't want to
11859 * enable it again, so just kick out.
11861 return (DTRACE_MATCH_NEXT
);
11864 if ((ecb
= dtrace_ecb_create(state
, probe
, enab
)) == NULL
)
11865 return (DTRACE_MATCH_DONE
);
11867 if (dtrace_ecb_enable(ecb
) < 0)
11868 return (DTRACE_MATCH_FAIL
);
11870 return (DTRACE_MATCH_NEXT
);
11873 static dtrace_ecb_t
*
11874 dtrace_epid2ecb(dtrace_state_t
*state
, dtrace_epid_t id
)
11877 #pragma unused(ecb) /* __APPLE__ */
11879 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
11881 if (id
== 0 || id
> (dtrace_epid_t
)state
->dts_necbs
)
11884 ASSERT(state
->dts_necbs
> 0 && state
->dts_ecbs
!= NULL
);
11885 ASSERT((ecb
= state
->dts_ecbs
[id
- 1]) == NULL
|| ecb
->dte_epid
== id
);
11887 return (state
->dts_ecbs
[id
- 1]);
11890 static dtrace_aggregation_t
*
11891 dtrace_aggid2agg(dtrace_state_t
*state
, dtrace_aggid_t id
)
11893 dtrace_aggregation_t
*agg
;
11894 #pragma unused(agg) /* __APPLE__ */
11896 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
11898 if (id
== 0 || id
> (dtrace_aggid_t
)state
->dts_naggregations
)
11901 ASSERT(state
->dts_naggregations
> 0 && state
->dts_aggregations
!= NULL
);
11902 ASSERT((agg
= state
->dts_aggregations
[id
- 1]) == NULL
||
11903 agg
->dtag_id
== id
);
11905 return (state
->dts_aggregations
[id
- 1]);
11909 * DTrace Buffer Functions
11911 * The following functions manipulate DTrace buffers. Most of these functions
11912 * are called in the context of establishing or processing consumer state;
11913 * exceptions are explicitly noted.
11917 * Note: called from cross call context. This function switches the two
11918 * buffers on a given CPU. The atomicity of this operation is assured by
11919 * disabling interrupts while the actual switch takes place; the disabling of
11920 * interrupts serializes the execution with any execution of dtrace_probe() on
11924 dtrace_buffer_switch(dtrace_buffer_t
*buf
)
11926 caddr_t tomax
= buf
->dtb_tomax
;
11927 caddr_t xamot
= buf
->dtb_xamot
;
11928 dtrace_icookie_t cookie
;
11931 ASSERT(!(buf
->dtb_flags
& DTRACEBUF_NOSWITCH
));
11932 ASSERT(!(buf
->dtb_flags
& DTRACEBUF_RING
));
11934 cookie
= dtrace_interrupt_disable();
11935 now
= dtrace_gethrtime();
11936 buf
->dtb_tomax
= xamot
;
11937 buf
->dtb_xamot
= tomax
;
11938 buf
->dtb_xamot_drops
= buf
->dtb_drops
;
11939 buf
->dtb_xamot_offset
= buf
->dtb_offset
;
11940 buf
->dtb_xamot_errors
= buf
->dtb_errors
;
11941 buf
->dtb_xamot_flags
= buf
->dtb_flags
;
11942 buf
->dtb_offset
= 0;
11943 buf
->dtb_drops
= 0;
11944 buf
->dtb_errors
= 0;
11945 buf
->dtb_flags
&= ~(DTRACEBUF_ERROR
| DTRACEBUF_DROPPED
);
11946 buf
->dtb_interval
= now
- buf
->dtb_switched
;
11947 buf
->dtb_switched
= now
;
11948 buf
->dtb_cur_limit
= buf
->dtb_limit
;
11950 dtrace_interrupt_enable(cookie
);
11954 * Note: called from cross call context. This function activates a buffer
11955 * on a CPU. As with dtrace_buffer_switch(), the atomicity of the operation
11956 * is guaranteed by the disabling of interrupts.
11959 dtrace_buffer_activate(dtrace_state_t
*state
)
11961 dtrace_buffer_t
*buf
;
11962 dtrace_icookie_t cookie
= dtrace_interrupt_disable();
11964 buf
= &state
->dts_buffer
[CPU
->cpu_id
];
11966 if (buf
->dtb_tomax
!= NULL
) {
11968 * We might like to assert that the buffer is marked inactive,
11969 * but this isn't necessarily true: the buffer for the CPU
11970 * that processes the BEGIN probe has its buffer activated
11971 * manually. In this case, we take the (harmless) action
11972 * re-clearing the bit INACTIVE bit.
11974 buf
->dtb_flags
&= ~DTRACEBUF_INACTIVE
;
11977 dtrace_interrupt_enable(cookie
);
11981 dtrace_buffer_canalloc(size_t size
)
11983 if (size
> (UINT64_MAX
- dtrace_buffer_memory_inuse
))
11985 if ((size
+ dtrace_buffer_memory_inuse
) > dtrace_buffer_memory_maxsize
)
11992 dtrace_buffer_alloc(dtrace_buffer_t
*bufs
, size_t limit
, size_t size
, int flags
,
11996 dtrace_buffer_t
*buf
;
11997 size_t size_before_alloc
= dtrace_buffer_memory_inuse
;
11999 LCK_MTX_ASSERT(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
12000 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
12002 if (size
> (size_t)dtrace_nonroot_maxsize
&&
12003 !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL
, B_FALSE
))
12009 if (cpu
!= DTRACE_CPUALL
&& cpu
!= cp
->cpu_id
)
12012 buf
= &bufs
[cp
->cpu_id
];
12015 * If there is already a buffer allocated for this CPU, it
12016 * is only possible that this is a DR event. In this case,
12017 * the buffer size must match our specified size.
12019 if (buf
->dtb_tomax
!= NULL
) {
12020 ASSERT(buf
->dtb_size
== size
);
12024 ASSERT(buf
->dtb_xamot
== NULL
);
12026 /* DTrace, please do not eat all the memory. */
12027 if (dtrace_buffer_canalloc(size
) == B_FALSE
)
12029 if ((buf
->dtb_tomax
= kmem_zalloc(size
, KM_NOSLEEP
)) == NULL
)
12031 dtrace_buffer_memory_inuse
+= size
;
12033 /* Unsure that limit is always lower than size */
12034 limit
= limit
== size
? limit
- 1 : limit
;
12035 buf
->dtb_cur_limit
= limit
;
12036 buf
->dtb_limit
= limit
;
12037 buf
->dtb_size
= size
;
12038 buf
->dtb_flags
= flags
;
12039 buf
->dtb_offset
= 0;
12040 buf
->dtb_drops
= 0;
12042 if (flags
& DTRACEBUF_NOSWITCH
)
12045 /* DTrace, please do not eat all the memory. */
12046 if (dtrace_buffer_canalloc(size
) == B_FALSE
)
12048 if ((buf
->dtb_xamot
= kmem_zalloc(size
, KM_NOSLEEP
)) == NULL
)
12050 dtrace_buffer_memory_inuse
+= size
;
12051 } while ((cp
= cp
->cpu_next
) != cpu_list
);
12053 ASSERT(dtrace_buffer_memory_inuse
<= dtrace_buffer_memory_maxsize
);
12061 if (cpu
!= DTRACE_CPUALL
&& cpu
!= cp
->cpu_id
)
12064 buf
= &bufs
[cp
->cpu_id
];
12066 if (buf
->dtb_xamot
!= NULL
) {
12067 ASSERT(buf
->dtb_tomax
!= NULL
);
12068 ASSERT(buf
->dtb_size
== size
);
12069 kmem_free(buf
->dtb_xamot
, size
);
12072 if (buf
->dtb_tomax
!= NULL
) {
12073 ASSERT(buf
->dtb_size
== size
);
12074 kmem_free(buf
->dtb_tomax
, size
);
12077 buf
->dtb_tomax
= NULL
;
12078 buf
->dtb_xamot
= NULL
;
12080 } while ((cp
= cp
->cpu_next
) != cpu_list
);
12082 /* Restore the size saved before allocating memory */
12083 dtrace_buffer_memory_inuse
= size_before_alloc
;
12089 * Note: called from probe context. This function just increments the drop
12090 * count on a buffer. It has been made a function to allow for the
12091 * possibility of understanding the source of mysterious drop counts. (A
12092 * problem for which one may be particularly disappointed that DTrace cannot
12093 * be used to understand DTrace.)
12096 dtrace_buffer_drop(dtrace_buffer_t
*buf
)
12102 * Note: called from probe context. This function is called to reserve space
12103 * in a buffer. If mstate is non-NULL, sets the scratch base and size in the
12104 * mstate. Returns the new offset in the buffer, or a negative value if an
12105 * error has occurred.
12108 dtrace_buffer_reserve(dtrace_buffer_t
*buf
, size_t needed
, size_t align
,
12109 dtrace_state_t
*state
, dtrace_mstate_t
*mstate
)
12111 intptr_t offs
= buf
->dtb_offset
, soffs
;
12116 if (buf
->dtb_flags
& DTRACEBUF_INACTIVE
)
12119 if ((tomax
= buf
->dtb_tomax
) == NULL
) {
12120 dtrace_buffer_drop(buf
);
12124 if (!(buf
->dtb_flags
& (DTRACEBUF_RING
| DTRACEBUF_FILL
))) {
12125 while (offs
& (align
- 1)) {
12127 * Assert that our alignment is off by a number which
12128 * is itself sizeof (uint32_t) aligned.
12130 ASSERT(!((align
- (offs
& (align
- 1))) &
12131 (sizeof (uint32_t) - 1)));
12132 DTRACE_STORE(uint32_t, tomax
, offs
, DTRACE_EPIDNONE
);
12133 offs
+= sizeof (uint32_t);
12136 if ((uint64_t)(soffs
= offs
+ needed
) > buf
->dtb_cur_limit
) {
12137 if (buf
->dtb_cur_limit
== buf
->dtb_limit
) {
12138 buf
->dtb_cur_limit
= buf
->dtb_size
;
12140 os_atomic_inc(&state
->dts_buf_over_limit
, relaxed
);
12142 * Set an AST on the current processor
12143 * so that we can wake up the process
12144 * outside of probe context, when we know
12145 * it is safe to do so
12147 minor_t minor
= getminor(state
->dts_dev
);
12148 ASSERT(minor
< 32);
12150 os_atomic_or(&dtrace_wake_clients
, 1 << minor
, relaxed
);
12153 if ((uint64_t)soffs
> buf
->dtb_size
) {
12154 dtrace_buffer_drop(buf
);
12159 if (mstate
== NULL
)
12162 mstate
->dtms_scratch_base
= (uintptr_t)tomax
+ soffs
;
12163 mstate
->dtms_scratch_size
= buf
->dtb_size
- soffs
;
12164 mstate
->dtms_scratch_ptr
= mstate
->dtms_scratch_base
;
12169 if (buf
->dtb_flags
& DTRACEBUF_FILL
) {
12170 if (state
->dts_activity
!= DTRACE_ACTIVITY_COOLDOWN
&&
12171 (buf
->dtb_flags
& DTRACEBUF_FULL
))
12176 total_off
= needed
+ (offs
& (align
- 1));
12179 * For a ring buffer, life is quite a bit more complicated. Before
12180 * we can store any padding, we need to adjust our wrapping offset.
12181 * (If we've never before wrapped or we're not about to, no adjustment
12184 if ((buf
->dtb_flags
& DTRACEBUF_WRAPPED
) ||
12185 offs
+ total_off
> buf
->dtb_size
) {
12186 woffs
= buf
->dtb_xamot_offset
;
12188 if (offs
+ total_off
> buf
->dtb_size
) {
12190 * We can't fit in the end of the buffer. First, a
12191 * sanity check that we can fit in the buffer at all.
12193 if (total_off
> buf
->dtb_size
) {
12194 dtrace_buffer_drop(buf
);
12199 * We're going to be storing at the top of the buffer,
12200 * so now we need to deal with the wrapped offset. We
12201 * only reset our wrapped offset to 0 if it is
12202 * currently greater than the current offset. If it
12203 * is less than the current offset, it is because a
12204 * previous allocation induced a wrap -- but the
12205 * allocation didn't subsequently take the space due
12206 * to an error or false predicate evaluation. In this
12207 * case, we'll just leave the wrapped offset alone: if
12208 * the wrapped offset hasn't been advanced far enough
12209 * for this allocation, it will be adjusted in the
12212 if (buf
->dtb_flags
& DTRACEBUF_WRAPPED
) {
12220 * Now we know that we're going to be storing to the
12221 * top of the buffer and that there is room for us
12222 * there. We need to clear the buffer from the current
12223 * offset to the end (there may be old gunk there).
12225 while ((uint64_t)offs
< buf
->dtb_size
)
12229 * We need to set our offset to zero. And because we
12230 * are wrapping, we need to set the bit indicating as
12231 * much. We can also adjust our needed space back
12232 * down to the space required by the ECB -- we know
12233 * that the top of the buffer is aligned.
12236 total_off
= needed
;
12237 buf
->dtb_flags
|= DTRACEBUF_WRAPPED
;
12240 * There is room for us in the buffer, so we simply
12241 * need to check the wrapped offset.
12243 if (woffs
< offs
) {
12245 * The wrapped offset is less than the offset.
12246 * This can happen if we allocated buffer space
12247 * that induced a wrap, but then we didn't
12248 * subsequently take the space due to an error
12249 * or false predicate evaluation. This is
12250 * okay; we know that _this_ allocation isn't
12251 * going to induce a wrap. We still can't
12252 * reset the wrapped offset to be zero,
12253 * however: the space may have been trashed in
12254 * the previous failed probe attempt. But at
12255 * least the wrapped offset doesn't need to
12256 * be adjusted at all...
12262 while (offs
+ total_off
> (size_t)woffs
) {
12263 dtrace_epid_t epid
= *(uint32_t *)(tomax
+ woffs
);
12266 if (epid
== DTRACE_EPIDNONE
) {
12267 size
= sizeof (uint32_t);
12269 ASSERT(epid
<= (dtrace_epid_t
)state
->dts_necbs
);
12270 ASSERT(state
->dts_ecbs
[epid
- 1] != NULL
);
12272 size
= state
->dts_ecbs
[epid
- 1]->dte_size
;
12275 ASSERT(woffs
+ size
<= buf
->dtb_size
);
12278 if (woffs
+ size
== buf
->dtb_size
) {
12280 * We've reached the end of the buffer; we want
12281 * to set the wrapped offset to 0 and break
12282 * out. However, if the offs is 0, then we're
12283 * in a strange edge-condition: the amount of
12284 * space that we want to reserve plus the size
12285 * of the record that we're overwriting is
12286 * greater than the size of the buffer. This
12287 * is problematic because if we reserve the
12288 * space but subsequently don't consume it (due
12289 * to a failed predicate or error) the wrapped
12290 * offset will be 0 -- yet the EPID at offset 0
12291 * will not be committed. This situation is
12292 * relatively easy to deal with: if we're in
12293 * this case, the buffer is indistinguishable
12294 * from one that hasn't wrapped; we need only
12295 * finish the job by clearing the wrapped bit,
12296 * explicitly setting the offset to be 0, and
12297 * zero'ing out the old data in the buffer.
12300 buf
->dtb_flags
&= ~DTRACEBUF_WRAPPED
;
12301 buf
->dtb_offset
= 0;
12304 while ((uint64_t)woffs
< buf
->dtb_size
)
12305 tomax
[woffs
++] = 0;
12316 * We have a wrapped offset. It may be that the wrapped offset
12317 * has become zero -- that's okay.
12319 buf
->dtb_xamot_offset
= woffs
;
12324 * Now we can plow the buffer with any necessary padding.
12326 while (offs
& (align
- 1)) {
12328 * Assert that our alignment is off by a number which
12329 * is itself sizeof (uint32_t) aligned.
12331 ASSERT(!((align
- (offs
& (align
- 1))) &
12332 (sizeof (uint32_t) - 1)));
12333 DTRACE_STORE(uint32_t, tomax
, offs
, DTRACE_EPIDNONE
);
12334 offs
+= sizeof (uint32_t);
12337 if (buf
->dtb_flags
& DTRACEBUF_FILL
) {
12338 if (offs
+ needed
> buf
->dtb_size
- state
->dts_reserve
) {
12339 buf
->dtb_flags
|= DTRACEBUF_FULL
;
12344 if (mstate
== NULL
)
12348 * For ring buffers and fill buffers, the scratch space is always
12349 * the inactive buffer.
12351 mstate
->dtms_scratch_base
= (uintptr_t)buf
->dtb_xamot
;
12352 mstate
->dtms_scratch_size
= buf
->dtb_size
;
12353 mstate
->dtms_scratch_ptr
= mstate
->dtms_scratch_base
;
12359 dtrace_buffer_polish(dtrace_buffer_t
*buf
)
12361 ASSERT(buf
->dtb_flags
& DTRACEBUF_RING
);
12362 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
12364 if (!(buf
->dtb_flags
& DTRACEBUF_WRAPPED
))
12368 * We need to polish the ring buffer. There are three cases:
12370 * - The first (and presumably most common) is that there is no gap
12371 * between the buffer offset and the wrapped offset. In this case,
12372 * there is nothing in the buffer that isn't valid data; we can
12373 * mark the buffer as polished and return.
12375 * - The second (less common than the first but still more common
12376 * than the third) is that there is a gap between the buffer offset
12377 * and the wrapped offset, and the wrapped offset is larger than the
12378 * buffer offset. This can happen because of an alignment issue, or
12379 * can happen because of a call to dtrace_buffer_reserve() that
12380 * didn't subsequently consume the buffer space. In this case,
12381 * we need to zero the data from the buffer offset to the wrapped
12384 * - The third (and least common) is that there is a gap between the
12385 * buffer offset and the wrapped offset, but the wrapped offset is
12386 * _less_ than the buffer offset. This can only happen because a
12387 * call to dtrace_buffer_reserve() induced a wrap, but the space
12388 * was not subsequently consumed. In this case, we need to zero the
12389 * space from the offset to the end of the buffer _and_ from the
12390 * top of the buffer to the wrapped offset.
12392 if (buf
->dtb_offset
< buf
->dtb_xamot_offset
) {
12393 bzero(buf
->dtb_tomax
+ buf
->dtb_offset
,
12394 buf
->dtb_xamot_offset
- buf
->dtb_offset
);
12397 if (buf
->dtb_offset
> buf
->dtb_xamot_offset
) {
12398 bzero(buf
->dtb_tomax
+ buf
->dtb_offset
,
12399 buf
->dtb_size
- buf
->dtb_offset
);
12400 bzero(buf
->dtb_tomax
, buf
->dtb_xamot_offset
);
12405 dtrace_buffer_free(dtrace_buffer_t
*bufs
)
12409 for (i
= 0; i
< (int)NCPU
; i
++) {
12410 dtrace_buffer_t
*buf
= &bufs
[i
];
12412 if (buf
->dtb_tomax
== NULL
) {
12413 ASSERT(buf
->dtb_xamot
== NULL
);
12414 ASSERT(buf
->dtb_size
== 0);
12418 if (buf
->dtb_xamot
!= NULL
) {
12419 ASSERT(!(buf
->dtb_flags
& DTRACEBUF_NOSWITCH
));
12420 kmem_free(buf
->dtb_xamot
, buf
->dtb_size
);
12422 ASSERT(dtrace_buffer_memory_inuse
>= buf
->dtb_size
);
12423 dtrace_buffer_memory_inuse
-= buf
->dtb_size
;
12426 kmem_free(buf
->dtb_tomax
, buf
->dtb_size
);
12427 ASSERT(dtrace_buffer_memory_inuse
>= buf
->dtb_size
);
12428 dtrace_buffer_memory_inuse
-= buf
->dtb_size
;
12431 buf
->dtb_tomax
= NULL
;
12432 buf
->dtb_xamot
= NULL
;
12437 * DTrace Enabling Functions
12439 static dtrace_enabling_t
*
12440 dtrace_enabling_create(dtrace_vstate_t
*vstate
)
12442 dtrace_enabling_t
*enab
;
12444 enab
= kmem_zalloc(sizeof (dtrace_enabling_t
), KM_SLEEP
);
12445 enab
->dten_vstate
= vstate
;
12451 dtrace_enabling_add(dtrace_enabling_t
*enab
, dtrace_ecbdesc_t
*ecb
)
12453 dtrace_ecbdesc_t
**ndesc
;
12454 size_t osize
, nsize
;
12457 * We can't add to enablings after we've enabled them, or after we've
12460 ASSERT(enab
->dten_probegen
== 0);
12461 ASSERT(enab
->dten_next
== NULL
&& enab
->dten_prev
== NULL
);
12463 /* APPLE NOTE: this protects against gcc 4.0 botch on x86 */
12464 if (ecb
== NULL
) return;
12466 if (enab
->dten_ndesc
< enab
->dten_maxdesc
) {
12467 enab
->dten_desc
[enab
->dten_ndesc
++] = ecb
;
12471 osize
= enab
->dten_maxdesc
* sizeof (dtrace_enabling_t
*);
12473 if (enab
->dten_maxdesc
== 0) {
12474 enab
->dten_maxdesc
= 1;
12476 enab
->dten_maxdesc
<<= 1;
12479 ASSERT(enab
->dten_ndesc
< enab
->dten_maxdesc
);
12481 nsize
= enab
->dten_maxdesc
* sizeof (dtrace_enabling_t
*);
12482 ndesc
= kmem_zalloc(nsize
, KM_SLEEP
);
12483 bcopy(enab
->dten_desc
, ndesc
, osize
);
12484 kmem_free(enab
->dten_desc
, osize
);
12486 enab
->dten_desc
= ndesc
;
12487 enab
->dten_desc
[enab
->dten_ndesc
++] = ecb
;
12491 dtrace_enabling_addlike(dtrace_enabling_t
*enab
, dtrace_ecbdesc_t
*ecb
,
12492 dtrace_probedesc_t
*pd
)
12494 dtrace_ecbdesc_t
*new;
12495 dtrace_predicate_t
*pred
;
12496 dtrace_actdesc_t
*act
;
12499 * We're going to create a new ECB description that matches the
12500 * specified ECB in every way, but has the specified probe description.
12502 new = kmem_zalloc(sizeof (dtrace_ecbdesc_t
), KM_SLEEP
);
12504 if ((pred
= ecb
->dted_pred
.dtpdd_predicate
) != NULL
)
12505 dtrace_predicate_hold(pred
);
12507 for (act
= ecb
->dted_action
; act
!= NULL
; act
= act
->dtad_next
)
12508 dtrace_actdesc_hold(act
);
12510 new->dted_action
= ecb
->dted_action
;
12511 new->dted_pred
= ecb
->dted_pred
;
12512 new->dted_probe
= *pd
;
12513 new->dted_uarg
= ecb
->dted_uarg
;
12515 dtrace_enabling_add(enab
, new);
12519 dtrace_enabling_dump(dtrace_enabling_t
*enab
)
12523 for (i
= 0; i
< enab
->dten_ndesc
; i
++) {
12524 dtrace_probedesc_t
*desc
= &enab
->dten_desc
[i
]->dted_probe
;
12526 cmn_err(CE_NOTE
, "enabling probe %d (%s:%s:%s:%s)", i
,
12527 desc
->dtpd_provider
, desc
->dtpd_mod
,
12528 desc
->dtpd_func
, desc
->dtpd_name
);
12533 dtrace_enabling_destroy(dtrace_enabling_t
*enab
)
12536 dtrace_ecbdesc_t
*ep
;
12537 dtrace_vstate_t
*vstate
= enab
->dten_vstate
;
12539 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
12541 for (i
= 0; i
< enab
->dten_ndesc
; i
++) {
12542 dtrace_actdesc_t
*act
, *next
;
12543 dtrace_predicate_t
*pred
;
12545 ep
= enab
->dten_desc
[i
];
12547 if ((pred
= ep
->dted_pred
.dtpdd_predicate
) != NULL
)
12548 dtrace_predicate_release(pred
, vstate
);
12550 for (act
= ep
->dted_action
; act
!= NULL
; act
= next
) {
12551 next
= act
->dtad_next
;
12552 dtrace_actdesc_release(act
, vstate
);
12555 kmem_free(ep
, sizeof (dtrace_ecbdesc_t
));
12558 kmem_free(enab
->dten_desc
,
12559 enab
->dten_maxdesc
* sizeof (dtrace_enabling_t
*));
12562 * If this was a retained enabling, decrement the dts_nretained count
12563 * and take it off of the dtrace_retained list.
12565 if (enab
->dten_prev
!= NULL
|| enab
->dten_next
!= NULL
||
12566 dtrace_retained
== enab
) {
12567 ASSERT(enab
->dten_vstate
->dtvs_state
!= NULL
);
12568 ASSERT(enab
->dten_vstate
->dtvs_state
->dts_nretained
> 0);
12569 enab
->dten_vstate
->dtvs_state
->dts_nretained
--;
12570 dtrace_retained_gen
++;
12573 if (enab
->dten_prev
== NULL
) {
12574 if (dtrace_retained
== enab
) {
12575 dtrace_retained
= enab
->dten_next
;
12577 if (dtrace_retained
!= NULL
)
12578 dtrace_retained
->dten_prev
= NULL
;
12581 ASSERT(enab
!= dtrace_retained
);
12582 ASSERT(dtrace_retained
!= NULL
);
12583 enab
->dten_prev
->dten_next
= enab
->dten_next
;
12586 if (enab
->dten_next
!= NULL
) {
12587 ASSERT(dtrace_retained
!= NULL
);
12588 enab
->dten_next
->dten_prev
= enab
->dten_prev
;
12591 kmem_free(enab
, sizeof (dtrace_enabling_t
));
12595 dtrace_enabling_retain(dtrace_enabling_t
*enab
)
12597 dtrace_state_t
*state
;
12599 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
12600 ASSERT(enab
->dten_next
== NULL
&& enab
->dten_prev
== NULL
);
12601 ASSERT(enab
->dten_vstate
!= NULL
);
12603 state
= enab
->dten_vstate
->dtvs_state
;
12604 ASSERT(state
!= NULL
);
12607 * We only allow each state to retain dtrace_retain_max enablings.
12609 if (state
->dts_nretained
>= dtrace_retain_max
)
12612 state
->dts_nretained
++;
12613 dtrace_retained_gen
++;
12615 if (dtrace_retained
== NULL
) {
12616 dtrace_retained
= enab
;
12620 enab
->dten_next
= dtrace_retained
;
12621 dtrace_retained
->dten_prev
= enab
;
12622 dtrace_retained
= enab
;
12628 dtrace_enabling_replicate(dtrace_state_t
*state
, dtrace_probedesc_t
*match
,
12629 dtrace_probedesc_t
*create
)
12631 dtrace_enabling_t
*new, *enab
;
12632 int found
= 0, err
= ENOENT
;
12634 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
12635 ASSERT(strlen(match
->dtpd_provider
) < DTRACE_PROVNAMELEN
);
12636 ASSERT(strlen(match
->dtpd_mod
) < DTRACE_MODNAMELEN
);
12637 ASSERT(strlen(match
->dtpd_func
) < DTRACE_FUNCNAMELEN
);
12638 ASSERT(strlen(match
->dtpd_name
) < DTRACE_NAMELEN
);
12640 new = dtrace_enabling_create(&state
->dts_vstate
);
12643 * Iterate over all retained enablings, looking for enablings that
12644 * match the specified state.
12646 for (enab
= dtrace_retained
; enab
!= NULL
; enab
= enab
->dten_next
) {
12650 * dtvs_state can only be NULL for helper enablings -- and
12651 * helper enablings can't be retained.
12653 ASSERT(enab
->dten_vstate
->dtvs_state
!= NULL
);
12655 if (enab
->dten_vstate
->dtvs_state
!= state
)
12659 * Now iterate over each probe description; we're looking for
12660 * an exact match to the specified probe description.
12662 for (i
= 0; i
< enab
->dten_ndesc
; i
++) {
12663 dtrace_ecbdesc_t
*ep
= enab
->dten_desc
[i
];
12664 dtrace_probedesc_t
*pd
= &ep
->dted_probe
;
12666 /* APPLE NOTE: Darwin employs size bounded string operation. */
12667 if (strncmp(pd
->dtpd_provider
, match
->dtpd_provider
, DTRACE_PROVNAMELEN
))
12670 if (strncmp(pd
->dtpd_mod
, match
->dtpd_mod
, DTRACE_MODNAMELEN
))
12673 if (strncmp(pd
->dtpd_func
, match
->dtpd_func
, DTRACE_FUNCNAMELEN
))
12676 if (strncmp(pd
->dtpd_name
, match
->dtpd_name
, DTRACE_NAMELEN
))
12680 * We have a winning probe! Add it to our growing
12684 dtrace_enabling_addlike(new, ep
, create
);
12688 if (!found
|| (err
= dtrace_enabling_retain(new)) != 0) {
12689 dtrace_enabling_destroy(new);
12697 dtrace_enabling_retract(dtrace_state_t
*state
)
12699 dtrace_enabling_t
*enab
, *next
;
12701 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
12704 * Iterate over all retained enablings, destroy the enablings retained
12705 * for the specified state.
12707 for (enab
= dtrace_retained
; enab
!= NULL
; enab
= next
) {
12708 next
= enab
->dten_next
;
12711 * dtvs_state can only be NULL for helper enablings -- and
12712 * helper enablings can't be retained.
12714 ASSERT(enab
->dten_vstate
->dtvs_state
!= NULL
);
12716 if (enab
->dten_vstate
->dtvs_state
== state
) {
12717 ASSERT(state
->dts_nretained
> 0);
12718 dtrace_enabling_destroy(enab
);
12722 ASSERT(state
->dts_nretained
== 0);
12726 dtrace_enabling_match(dtrace_enabling_t
*enab
, int *nmatched
, dtrace_match_cond_t
*cond
)
12729 int total_matched
= 0, matched
= 0;
12731 LCK_MTX_ASSERT(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
12732 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
12734 for (i
= 0; i
< enab
->dten_ndesc
; i
++) {
12735 dtrace_ecbdesc_t
*ep
= enab
->dten_desc
[i
];
12737 enab
->dten_current
= ep
;
12738 enab
->dten_error
= 0;
12741 * Before doing a dtrace_probe_enable, which is really
12742 * expensive, check that this enabling matches the matching precondition
12745 if (cond
&& (cond
->dmc_func(&ep
->dted_probe
, cond
->dmc_data
) == 0)) {
12749 * If a provider failed to enable a probe then get out and
12750 * let the consumer know we failed.
12752 if ((matched
= dtrace_probe_enable(&ep
->dted_probe
, enab
, ep
)) < 0)
12755 total_matched
+= matched
;
12757 if (enab
->dten_error
!= 0) {
12759 * If we get an error half-way through enabling the
12760 * probes, we kick out -- perhaps with some number of
12761 * them enabled. Leaving enabled probes enabled may
12762 * be slightly confusing for user-level, but we expect
12763 * that no one will attempt to actually drive on in
12764 * the face of such errors. If this is an anonymous
12765 * enabling (indicated with a NULL nmatched pointer),
12766 * we cmn_err() a message. We aren't expecting to
12767 * get such an error -- such as it can exist at all,
12768 * it would be a result of corrupted DOF in the driver
12771 if (nmatched
== NULL
) {
12772 cmn_err(CE_WARN
, "dtrace_enabling_match() "
12773 "error on %p: %d", (void *)ep
,
12777 return (enab
->dten_error
);
12780 ep
->dted_probegen
= dtrace_probegen
;
12783 if (nmatched
!= NULL
)
12784 *nmatched
= total_matched
;
12790 dtrace_enabling_matchall_with_cond(dtrace_match_cond_t
*cond
)
12792 dtrace_enabling_t
*enab
;
12794 lck_mtx_lock(&cpu_lock
);
12795 lck_mtx_lock(&dtrace_lock
);
12798 * Iterate over all retained enablings to see if any probes match
12799 * against them. We only perform this operation on enablings for which
12800 * we have sufficient permissions by virtue of being in the global zone
12801 * or in the same zone as the DTrace client. Because we can be called
12802 * after dtrace_detach() has been called, we cannot assert that there
12803 * are retained enablings. We can safely load from dtrace_retained,
12804 * however: the taskq_destroy() at the end of dtrace_detach() will
12805 * block pending our completion.
12809 * Darwin doesn't do zones.
12810 * Behave as if always in "global" zone."
12812 for (enab
= dtrace_retained
; enab
!= NULL
; enab
= enab
->dten_next
) {
12813 (void) dtrace_enabling_match(enab
, NULL
, cond
);
12816 lck_mtx_unlock(&dtrace_lock
);
12817 lck_mtx_unlock(&cpu_lock
);
12822 dtrace_enabling_matchall(void)
12824 dtrace_enabling_matchall_with_cond(NULL
);
12830 * If an enabling is to be enabled without having matched probes (that is, if
12831 * dtrace_state_go() is to be called on the underlying dtrace_state_t), the
12832 * enabling must be _primed_ by creating an ECB for every ECB description.
12833 * This must be done to assure that we know the number of speculations, the
12834 * number of aggregations, the minimum buffer size needed, etc. before we
12835 * transition out of DTRACE_ACTIVITY_INACTIVE. To do this without actually
12836 * enabling any probes, we create ECBs for every ECB decription, but with a
12837 * NULL probe -- which is exactly what this function does.
12840 dtrace_enabling_prime(dtrace_state_t
*state
)
12842 dtrace_enabling_t
*enab
;
12845 for (enab
= dtrace_retained
; enab
!= NULL
; enab
= enab
->dten_next
) {
12846 ASSERT(enab
->dten_vstate
->dtvs_state
!= NULL
);
12848 if (enab
->dten_vstate
->dtvs_state
!= state
)
12852 * We don't want to prime an enabling more than once, lest
12853 * we allow a malicious user to induce resource exhaustion.
12854 * (The ECBs that result from priming an enabling aren't
12855 * leaked -- but they also aren't deallocated until the
12856 * consumer state is destroyed.)
12858 if (enab
->dten_primed
)
12861 for (i
= 0; i
< enab
->dten_ndesc
; i
++) {
12862 enab
->dten_current
= enab
->dten_desc
[i
];
12863 (void) dtrace_probe_enable(NULL
, enab
, NULL
);
12866 enab
->dten_primed
= 1;
12871 * Called to indicate that probes should be provided due to retained
12872 * enablings. This is implemented in terms of dtrace_probe_provide(), but it
12873 * must take an initial lap through the enabling calling the dtps_provide()
12874 * entry point explicitly to allow for autocreated probes.
12877 dtrace_enabling_provide(dtrace_provider_t
*prv
)
12880 dtrace_probedesc_t desc
;
12881 dtrace_genid_t gen
;
12883 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
12884 LCK_MTX_ASSERT(&dtrace_provider_lock
, LCK_MTX_ASSERT_OWNED
);
12888 prv
= dtrace_provider
;
12892 dtrace_enabling_t
*enab
;
12893 void *parg
= prv
->dtpv_arg
;
12896 gen
= dtrace_retained_gen
;
12897 for (enab
= dtrace_retained
; enab
!= NULL
;
12898 enab
= enab
->dten_next
) {
12899 for (i
= 0; i
< enab
->dten_ndesc
; i
++) {
12900 desc
= enab
->dten_desc
[i
]->dted_probe
;
12901 lck_mtx_unlock(&dtrace_lock
);
12902 prv
->dtpv_pops
.dtps_provide(parg
, &desc
);
12903 lck_mtx_lock(&dtrace_lock
);
12905 * Process the retained enablings again if
12906 * they have changed while we weren't holding
12909 if (gen
!= dtrace_retained_gen
)
12913 } while (all
&& (prv
= prv
->dtpv_next
) != NULL
);
12915 lck_mtx_unlock(&dtrace_lock
);
12916 dtrace_probe_provide(NULL
, all
? NULL
: prv
);
12917 lck_mtx_lock(&dtrace_lock
);
12921 * DTrace DOF Functions
12925 dtrace_dof_error(dof_hdr_t
*dof
, const char *str
)
12927 #pragma unused(dof) /* __APPLE__ */
12928 if (dtrace_err_verbose
)
12929 cmn_err(CE_WARN
, "failed to process DOF: %s", str
);
12931 #ifdef DTRACE_ERRDEBUG
12932 dtrace_errdebug(str
);
12937 * Create DOF out of a currently enabled state. Right now, we only create
12938 * DOF containing the run-time options -- but this could be expanded to create
12939 * complete DOF representing the enabled state.
12942 dtrace_dof_create(dtrace_state_t
*state
)
12946 dof_optdesc_t
*opt
;
12947 int i
, len
= sizeof (dof_hdr_t
) +
12948 roundup(sizeof (dof_sec_t
), sizeof (uint64_t)) +
12949 sizeof (dof_optdesc_t
) * DTRACEOPT_MAX
;
12951 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
12953 dof
= kmem_zalloc_aligned(len
, 8, KM_SLEEP
);
12954 dof
->dofh_ident
[DOF_ID_MAG0
] = DOF_MAG_MAG0
;
12955 dof
->dofh_ident
[DOF_ID_MAG1
] = DOF_MAG_MAG1
;
12956 dof
->dofh_ident
[DOF_ID_MAG2
] = DOF_MAG_MAG2
;
12957 dof
->dofh_ident
[DOF_ID_MAG3
] = DOF_MAG_MAG3
;
12959 dof
->dofh_ident
[DOF_ID_MODEL
] = DOF_MODEL_NATIVE
;
12960 dof
->dofh_ident
[DOF_ID_ENCODING
] = DOF_ENCODE_NATIVE
;
12961 dof
->dofh_ident
[DOF_ID_VERSION
] = DOF_VERSION
;
12962 dof
->dofh_ident
[DOF_ID_DIFVERS
] = DIF_VERSION
;
12963 dof
->dofh_ident
[DOF_ID_DIFIREG
] = DIF_DIR_NREGS
;
12964 dof
->dofh_ident
[DOF_ID_DIFTREG
] = DIF_DTR_NREGS
;
12966 dof
->dofh_flags
= 0;
12967 dof
->dofh_hdrsize
= sizeof (dof_hdr_t
);
12968 dof
->dofh_secsize
= sizeof (dof_sec_t
);
12969 dof
->dofh_secnum
= 1; /* only DOF_SECT_OPTDESC */
12970 dof
->dofh_secoff
= sizeof (dof_hdr_t
);
12971 dof
->dofh_loadsz
= len
;
12972 dof
->dofh_filesz
= len
;
12976 * Fill in the option section header...
12978 sec
= (dof_sec_t
*)((uintptr_t)dof
+ sizeof (dof_hdr_t
));
12979 sec
->dofs_type
= DOF_SECT_OPTDESC
;
12980 sec
->dofs_align
= sizeof (uint64_t);
12981 sec
->dofs_flags
= DOF_SECF_LOAD
;
12982 sec
->dofs_entsize
= sizeof (dof_optdesc_t
);
12984 opt
= (dof_optdesc_t
*)((uintptr_t)sec
+
12985 roundup(sizeof (dof_sec_t
), sizeof (uint64_t)));
12987 sec
->dofs_offset
= (uintptr_t)opt
- (uintptr_t)dof
;
12988 sec
->dofs_size
= sizeof (dof_optdesc_t
) * DTRACEOPT_MAX
;
12990 for (i
= 0; i
< DTRACEOPT_MAX
; i
++) {
12991 opt
[i
].dofo_option
= i
;
12992 opt
[i
].dofo_strtab
= DOF_SECIDX_NONE
;
12993 opt
[i
].dofo_value
= state
->dts_options
[i
];
13000 dtrace_dof_copyin(user_addr_t uarg
, int *errp
)
13002 dof_hdr_t hdr
, *dof
;
13004 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_NOTOWNED
);
13007 * First, we're going to copyin() the sizeof (dof_hdr_t).
13009 if (copyin(uarg
, &hdr
, sizeof (hdr
)) != 0) {
13010 dtrace_dof_error(NULL
, "failed to copyin DOF header");
13016 * Now we'll allocate the entire DOF and copy it in -- provided
13017 * that the length isn't outrageous.
13019 if (hdr
.dofh_loadsz
>= (uint64_t)dtrace_dof_maxsize
) {
13020 dtrace_dof_error(&hdr
, "load size exceeds maximum");
13025 if (hdr
.dofh_loadsz
< sizeof (hdr
)) {
13026 dtrace_dof_error(&hdr
, "invalid load size");
13031 dof
= kmem_alloc_aligned(hdr
.dofh_loadsz
, 8, KM_SLEEP
);
13033 if (copyin(uarg
, dof
, hdr
.dofh_loadsz
) != 0 ||
13034 dof
->dofh_loadsz
!= hdr
.dofh_loadsz
) {
13035 kmem_free_aligned(dof
, hdr
.dofh_loadsz
);
13044 dtrace_dof_copyin_from_proc(proc_t
* p
, user_addr_t uarg
, int *errp
)
13046 dof_hdr_t hdr
, *dof
;
13048 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_NOTOWNED
);
13051 * First, we're going to copyin() the sizeof (dof_hdr_t).
13053 if (uread(p
, &hdr
, sizeof(hdr
), uarg
) != KERN_SUCCESS
) {
13054 dtrace_dof_error(NULL
, "failed to copyin DOF header");
13060 * Now we'll allocate the entire DOF and copy it in -- provided
13061 * that the length isn't outrageous.
13063 if (hdr
.dofh_loadsz
>= (uint64_t)dtrace_dof_maxsize
) {
13064 dtrace_dof_error(&hdr
, "load size exceeds maximum");
13069 if (hdr
.dofh_loadsz
< sizeof (hdr
)) {
13070 dtrace_dof_error(&hdr
, "invalid load size");
13075 dof
= kmem_alloc_aligned(hdr
.dofh_loadsz
, 8, KM_SLEEP
);
13077 if (uread(p
, dof
, hdr
.dofh_loadsz
, uarg
) != KERN_SUCCESS
) {
13078 kmem_free_aligned(dof
, hdr
.dofh_loadsz
);
13087 dtrace_dof_destroy(dof_hdr_t
*dof
)
13089 kmem_free_aligned(dof
, dof
->dofh_loadsz
);
13093 dtrace_dof_property(const char *name
)
13095 unsigned int len
= 0;
13098 if (dtrace_is_restricted() && !dtrace_are_restrictions_relaxed()) {
13102 if (!PEReadNVRAMProperty(name
, NULL
, &len
)) {
13106 dof
= kmem_alloc_aligned(len
, 8, KM_SLEEP
);
13108 if (!PEReadNVRAMProperty(name
, dof
, &len
)) {
13109 dtrace_dof_destroy(dof
);
13110 dtrace_dof_error(NULL
, "unreadable DOF");
13114 if (len
< sizeof (dof_hdr_t
)) {
13115 dtrace_dof_destroy(dof
);
13116 dtrace_dof_error(NULL
, "truncated header");
13120 if (len
< dof
->dofh_loadsz
) {
13121 dtrace_dof_destroy(dof
);
13122 dtrace_dof_error(NULL
, "truncated DOF");
13126 if (len
!= dof
->dofh_loadsz
) {
13127 dtrace_dof_destroy(dof
);
13128 dtrace_dof_error(NULL
, "invalid DOF size");
13132 if (dof
->dofh_loadsz
>= (uint64_t)dtrace_dof_maxsize
) {
13133 dtrace_dof_destroy(dof
);
13134 dtrace_dof_error(NULL
, "oversized DOF");
13142 * Return the dof_sec_t pointer corresponding to a given section index. If the
13143 * index is not valid, dtrace_dof_error() is called and NULL is returned. If
13144 * a type other than DOF_SECT_NONE is specified, the header is checked against
13145 * this type and NULL is returned if the types do not match.
13148 dtrace_dof_sect(dof_hdr_t
*dof
, uint32_t type
, dof_secidx_t i
)
13150 dof_sec_t
*sec
= (dof_sec_t
*)(uintptr_t)
13151 ((uintptr_t)dof
+ dof
->dofh_secoff
+ i
* dof
->dofh_secsize
);
13153 if (i
>= dof
->dofh_secnum
) {
13154 dtrace_dof_error(dof
, "referenced section index is invalid");
13158 if (!(sec
->dofs_flags
& DOF_SECF_LOAD
)) {
13159 dtrace_dof_error(dof
, "referenced section is not loadable");
13163 if (type
!= DOF_SECT_NONE
&& type
!= sec
->dofs_type
) {
13164 dtrace_dof_error(dof
, "referenced section is the wrong type");
13171 static dtrace_probedesc_t
*
13172 dtrace_dof_probedesc(dof_hdr_t
*dof
, dof_sec_t
*sec
, dtrace_probedesc_t
*desc
)
13174 dof_probedesc_t
*probe
;
13176 uintptr_t daddr
= (uintptr_t)dof
;
13180 if (sec
->dofs_type
!= DOF_SECT_PROBEDESC
) {
13181 dtrace_dof_error(dof
, "invalid probe section");
13185 if (sec
->dofs_align
!= sizeof (dof_secidx_t
)) {
13186 dtrace_dof_error(dof
, "bad alignment in probe description");
13190 if (sec
->dofs_offset
+ sizeof (dof_probedesc_t
) > dof
->dofh_loadsz
) {
13191 dtrace_dof_error(dof
, "truncated probe description");
13195 probe
= (dof_probedesc_t
*)(uintptr_t)(daddr
+ sec
->dofs_offset
);
13196 strtab
= dtrace_dof_sect(dof
, DOF_SECT_STRTAB
, probe
->dofp_strtab
);
13198 if (strtab
== NULL
)
13201 str
= daddr
+ strtab
->dofs_offset
;
13202 size
= strtab
->dofs_size
;
13204 if (probe
->dofp_provider
>= strtab
->dofs_size
) {
13205 dtrace_dof_error(dof
, "corrupt probe provider");
13209 (void) strncpy(desc
->dtpd_provider
,
13210 (char *)(str
+ probe
->dofp_provider
),
13211 MIN(DTRACE_PROVNAMELEN
- 1, size
- probe
->dofp_provider
));
13213 /* APPLE NOTE: Darwin employs size bounded string operation. */
13214 desc
->dtpd_provider
[DTRACE_PROVNAMELEN
- 1] = '\0';
13216 if (probe
->dofp_mod
>= strtab
->dofs_size
) {
13217 dtrace_dof_error(dof
, "corrupt probe module");
13221 (void) strncpy(desc
->dtpd_mod
, (char *)(str
+ probe
->dofp_mod
),
13222 MIN(DTRACE_MODNAMELEN
- 1, size
- probe
->dofp_mod
));
13224 /* APPLE NOTE: Darwin employs size bounded string operation. */
13225 desc
->dtpd_mod
[DTRACE_MODNAMELEN
- 1] = '\0';
13227 if (probe
->dofp_func
>= strtab
->dofs_size
) {
13228 dtrace_dof_error(dof
, "corrupt probe function");
13232 (void) strncpy(desc
->dtpd_func
, (char *)(str
+ probe
->dofp_func
),
13233 MIN(DTRACE_FUNCNAMELEN
- 1, size
- probe
->dofp_func
));
13235 /* APPLE NOTE: Darwin employs size bounded string operation. */
13236 desc
->dtpd_func
[DTRACE_FUNCNAMELEN
- 1] = '\0';
13238 if (probe
->dofp_name
>= strtab
->dofs_size
) {
13239 dtrace_dof_error(dof
, "corrupt probe name");
13243 (void) strncpy(desc
->dtpd_name
, (char *)(str
+ probe
->dofp_name
),
13244 MIN(DTRACE_NAMELEN
- 1, size
- probe
->dofp_name
));
13246 /* APPLE NOTE: Darwin employs size bounded string operation. */
13247 desc
->dtpd_name
[DTRACE_NAMELEN
- 1] = '\0';
13252 static dtrace_difo_t
*
13253 dtrace_dof_difo(dof_hdr_t
*dof
, dof_sec_t
*sec
, dtrace_vstate_t
*vstate
,
13258 dof_difohdr_t
*dofd
;
13259 uintptr_t daddr
= (uintptr_t)dof
;
13260 size_t max_size
= dtrace_difo_maxsize
;
13265 static const struct {
13273 { DOF_SECT_DIF
, offsetof(dtrace_difo_t
, dtdo_buf
),
13274 offsetof(dtrace_difo_t
, dtdo_len
), sizeof (dif_instr_t
),
13275 sizeof (dif_instr_t
), "multiple DIF sections" },
13277 { DOF_SECT_INTTAB
, offsetof(dtrace_difo_t
, dtdo_inttab
),
13278 offsetof(dtrace_difo_t
, dtdo_intlen
), sizeof (uint64_t),
13279 sizeof (uint64_t), "multiple integer tables" },
13281 { DOF_SECT_STRTAB
, offsetof(dtrace_difo_t
, dtdo_strtab
),
13282 offsetof(dtrace_difo_t
, dtdo_strlen
), 0,
13283 sizeof (char), "multiple string tables" },
13285 { DOF_SECT_VARTAB
, offsetof(dtrace_difo_t
, dtdo_vartab
),
13286 offsetof(dtrace_difo_t
, dtdo_varlen
), sizeof (dtrace_difv_t
),
13287 sizeof (uint_t
), "multiple variable tables" },
13289 { DOF_SECT_NONE
, 0, 0, 0, 0, NULL
}
13292 if (sec
->dofs_type
!= DOF_SECT_DIFOHDR
) {
13293 dtrace_dof_error(dof
, "invalid DIFO header section");
13297 if (sec
->dofs_align
!= sizeof (dof_secidx_t
)) {
13298 dtrace_dof_error(dof
, "bad alignment in DIFO header");
13302 if (sec
->dofs_size
< sizeof (dof_difohdr_t
) ||
13303 sec
->dofs_size
% sizeof (dof_secidx_t
)) {
13304 dtrace_dof_error(dof
, "bad size in DIFO header");
13308 dofd
= (dof_difohdr_t
*)(uintptr_t)(daddr
+ sec
->dofs_offset
);
13309 n
= (sec
->dofs_size
- sizeof (*dofd
)) / sizeof (dof_secidx_t
) + 1;
13311 dp
= kmem_zalloc(sizeof (dtrace_difo_t
), KM_SLEEP
);
13312 dp
->dtdo_rtype
= dofd
->dofd_rtype
;
13314 for (l
= 0; l
< n
; l
++) {
13319 if ((subsec
= dtrace_dof_sect(dof
, DOF_SECT_NONE
,
13320 dofd
->dofd_links
[l
])) == NULL
)
13321 goto err
; /* invalid section link */
13323 if (ttl
+ subsec
->dofs_size
> max_size
) {
13324 dtrace_dof_error(dof
, "exceeds maximum size");
13328 ttl
+= subsec
->dofs_size
;
13330 for (i
= 0; difo
[i
].section
!= DOF_SECT_NONE
; i
++) {
13332 if (subsec
->dofs_type
!= (uint32_t)difo
[i
].section
)
13335 if (!(subsec
->dofs_flags
& DOF_SECF_LOAD
)) {
13336 dtrace_dof_error(dof
, "section not loaded");
13340 if (subsec
->dofs_align
!= (uint32_t)difo
[i
].align
) {
13341 dtrace_dof_error(dof
, "bad alignment");
13345 bufp
= (void **)((uintptr_t)dp
+ difo
[i
].bufoffs
);
13346 lenp
= (uint32_t *)((uintptr_t)dp
+ difo
[i
].lenoffs
);
13348 if (*bufp
!= NULL
) {
13349 dtrace_dof_error(dof
, difo
[i
].msg
);
13353 if ((uint32_t)difo
[i
].entsize
!= subsec
->dofs_entsize
) {
13354 dtrace_dof_error(dof
, "entry size mismatch");
13358 if (subsec
->dofs_entsize
!= 0 &&
13359 (subsec
->dofs_size
% subsec
->dofs_entsize
) != 0) {
13360 dtrace_dof_error(dof
, "corrupt entry size");
13364 *lenp
= subsec
->dofs_size
;
13365 *bufp
= kmem_alloc(subsec
->dofs_size
, KM_SLEEP
);
13366 bcopy((char *)(uintptr_t)(daddr
+ subsec
->dofs_offset
),
13367 *bufp
, subsec
->dofs_size
);
13369 if (subsec
->dofs_entsize
!= 0)
13370 *lenp
/= subsec
->dofs_entsize
;
13376 * If we encounter a loadable DIFO sub-section that is not
13377 * known to us, assume this is a broken program and fail.
13379 if (difo
[i
].section
== DOF_SECT_NONE
&&
13380 (subsec
->dofs_flags
& DOF_SECF_LOAD
)) {
13381 dtrace_dof_error(dof
, "unrecognized DIFO subsection");
13386 if (dp
->dtdo_buf
== NULL
) {
13388 * We can't have a DIF object without DIF text.
13390 dtrace_dof_error(dof
, "missing DIF text");
13395 * Before we validate the DIF object, run through the variable table
13396 * looking for the strings -- if any of their size are under, we'll set
13397 * their size to be the system-wide default string size. Note that
13398 * this should _not_ happen if the "strsize" option has been set --
13399 * in this case, the compiler should have set the size to reflect the
13400 * setting of the option.
13402 for (i
= 0; i
< dp
->dtdo_varlen
; i
++) {
13403 dtrace_difv_t
*v
= &dp
->dtdo_vartab
[i
];
13404 dtrace_diftype_t
*t
= &v
->dtdv_type
;
13406 if (v
->dtdv_id
< DIF_VAR_OTHER_UBASE
)
13409 if (t
->dtdt_kind
== DIF_TYPE_STRING
&& t
->dtdt_size
== 0)
13410 t
->dtdt_size
= dtrace_strsize_default
;
13413 if (dtrace_difo_validate(dp
, vstate
, DIF_DIR_NREGS
, cr
) != 0)
13416 dtrace_difo_init(dp
, vstate
);
13420 kmem_free(dp
->dtdo_buf
, dp
->dtdo_len
* sizeof (dif_instr_t
));
13421 kmem_free(dp
->dtdo_inttab
, dp
->dtdo_intlen
* sizeof (uint64_t));
13422 kmem_free(dp
->dtdo_strtab
, dp
->dtdo_strlen
);
13423 kmem_free(dp
->dtdo_vartab
, dp
->dtdo_varlen
* sizeof (dtrace_difv_t
));
13425 kmem_free(dp
, sizeof (dtrace_difo_t
));
13429 static dtrace_predicate_t
*
13430 dtrace_dof_predicate(dof_hdr_t
*dof
, dof_sec_t
*sec
, dtrace_vstate_t
*vstate
,
13435 if ((dp
= dtrace_dof_difo(dof
, sec
, vstate
, cr
)) == NULL
)
13438 return (dtrace_predicate_create(dp
));
13441 static dtrace_actdesc_t
*
13442 dtrace_dof_actdesc(dof_hdr_t
*dof
, dof_sec_t
*sec
, dtrace_vstate_t
*vstate
,
13445 dtrace_actdesc_t
*act
, *first
= NULL
, *last
= NULL
, *next
;
13446 dof_actdesc_t
*desc
;
13447 dof_sec_t
*difosec
;
13449 uintptr_t daddr
= (uintptr_t)dof
;
13451 dtrace_actkind_t kind
;
13453 if (sec
->dofs_type
!= DOF_SECT_ACTDESC
) {
13454 dtrace_dof_error(dof
, "invalid action section");
13458 if (sec
->dofs_offset
+ sizeof (dof_actdesc_t
) > dof
->dofh_loadsz
) {
13459 dtrace_dof_error(dof
, "truncated action description");
13463 if (sec
->dofs_align
!= sizeof (uint64_t)) {
13464 dtrace_dof_error(dof
, "bad alignment in action description");
13468 if (sec
->dofs_size
< sec
->dofs_entsize
) {
13469 dtrace_dof_error(dof
, "section entry size exceeds total size");
13473 if (sec
->dofs_entsize
!= sizeof (dof_actdesc_t
)) {
13474 dtrace_dof_error(dof
, "bad entry size in action description");
13478 if (sec
->dofs_size
/ sec
->dofs_entsize
> dtrace_actions_max
) {
13479 dtrace_dof_error(dof
, "actions exceed dtrace_actions_max");
13483 for (offs
= 0; offs
< sec
->dofs_size
; offs
+= sec
->dofs_entsize
) {
13484 desc
= (dof_actdesc_t
*)(daddr
+
13485 (uintptr_t)sec
->dofs_offset
+ offs
);
13486 kind
= (dtrace_actkind_t
)desc
->dofa_kind
;
13488 if ((DTRACEACT_ISPRINTFLIKE(kind
) &&
13489 (kind
!= DTRACEACT_PRINTA
|| desc
->dofa_strtab
!= DOF_SECIDX_NONE
)) ||
13490 (kind
== DTRACEACT_DIFEXPR
&& desc
->dofa_strtab
!= DOF_SECIDX_NONE
))
13497 * The argument to these actions is an index into the
13498 * DOF string table. For printf()-like actions, this
13499 * is the format string. For print(), this is the
13500 * CTF type of the expression result.
13502 if ((strtab
= dtrace_dof_sect(dof
,
13503 DOF_SECT_STRTAB
, desc
->dofa_strtab
)) == NULL
)
13506 str
= (char *)((uintptr_t)dof
+
13507 (uintptr_t)strtab
->dofs_offset
);
13509 for (i
= desc
->dofa_arg
; i
< strtab
->dofs_size
; i
++) {
13510 if (str
[i
] == '\0')
13514 if (i
>= strtab
->dofs_size
) {
13515 dtrace_dof_error(dof
, "bogus format string");
13519 if (i
== desc
->dofa_arg
) {
13520 dtrace_dof_error(dof
, "empty format string");
13524 i
-= desc
->dofa_arg
;
13525 fmt
= kmem_alloc(i
+ 1, KM_SLEEP
);
13526 bcopy(&str
[desc
->dofa_arg
], fmt
, i
+ 1);
13527 arg
= (uint64_t)(uintptr_t)fmt
;
13529 if (kind
== DTRACEACT_PRINTA
) {
13530 ASSERT(desc
->dofa_strtab
== DOF_SECIDX_NONE
);
13533 arg
= desc
->dofa_arg
;
13537 act
= dtrace_actdesc_create(kind
, desc
->dofa_ntuple
,
13538 desc
->dofa_uarg
, arg
);
13540 if (last
!= NULL
) {
13541 last
->dtad_next
= act
;
13548 if (desc
->dofa_difo
== DOF_SECIDX_NONE
)
13551 if ((difosec
= dtrace_dof_sect(dof
,
13552 DOF_SECT_DIFOHDR
, desc
->dofa_difo
)) == NULL
)
13555 act
->dtad_difo
= dtrace_dof_difo(dof
, difosec
, vstate
, cr
);
13557 if (act
->dtad_difo
== NULL
)
13561 ASSERT(first
!= NULL
);
13565 for (act
= first
; act
!= NULL
; act
= next
) {
13566 next
= act
->dtad_next
;
13567 dtrace_actdesc_release(act
, vstate
);
13573 static dtrace_ecbdesc_t
*
13574 dtrace_dof_ecbdesc(dof_hdr_t
*dof
, dof_sec_t
*sec
, dtrace_vstate_t
*vstate
,
13577 dtrace_ecbdesc_t
*ep
;
13578 dof_ecbdesc_t
*ecb
;
13579 dtrace_probedesc_t
*desc
;
13580 dtrace_predicate_t
*pred
= NULL
;
13582 if (sec
->dofs_size
< sizeof (dof_ecbdesc_t
)) {
13583 dtrace_dof_error(dof
, "truncated ECB description");
13587 if (sec
->dofs_align
!= sizeof (uint64_t)) {
13588 dtrace_dof_error(dof
, "bad alignment in ECB description");
13592 ecb
= (dof_ecbdesc_t
*)((uintptr_t)dof
+ (uintptr_t)sec
->dofs_offset
);
13593 sec
= dtrace_dof_sect(dof
, DOF_SECT_PROBEDESC
, ecb
->dofe_probes
);
13598 ep
= kmem_zalloc(sizeof (dtrace_ecbdesc_t
), KM_SLEEP
);
13599 ep
->dted_uarg
= ecb
->dofe_uarg
;
13600 desc
= &ep
->dted_probe
;
13602 if (dtrace_dof_probedesc(dof
, sec
, desc
) == NULL
)
13605 if (ecb
->dofe_pred
!= DOF_SECIDX_NONE
) {
13606 if ((sec
= dtrace_dof_sect(dof
,
13607 DOF_SECT_DIFOHDR
, ecb
->dofe_pred
)) == NULL
)
13610 if ((pred
= dtrace_dof_predicate(dof
, sec
, vstate
, cr
)) == NULL
)
13613 ep
->dted_pred
.dtpdd_predicate
= pred
;
13616 if (ecb
->dofe_actions
!= DOF_SECIDX_NONE
) {
13617 if ((sec
= dtrace_dof_sect(dof
,
13618 DOF_SECT_ACTDESC
, ecb
->dofe_actions
)) == NULL
)
13621 ep
->dted_action
= dtrace_dof_actdesc(dof
, sec
, vstate
, cr
);
13623 if (ep
->dted_action
== NULL
)
13631 dtrace_predicate_release(pred
, vstate
);
13632 kmem_free(ep
, sizeof (dtrace_ecbdesc_t
));
13637 * APPLE NOTE: dyld handles dof relocation.
13638 * Darwin does not need dtrace_dof_relocate()
13642 * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated
13643 * header: it should be at the front of a memory region that is at least
13644 * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in
13645 * size. It need not be validated in any other way.
13648 dtrace_dof_slurp(dof_hdr_t
*dof
, dtrace_vstate_t
*vstate
, cred_t
*cr
,
13649 dtrace_enabling_t
**enabp
, uint64_t ubase
, int noprobes
)
13651 #pragma unused(ubase) /* __APPLE__ */
13652 uint64_t len
= dof
->dofh_loadsz
, seclen
;
13653 uintptr_t daddr
= (uintptr_t)dof
;
13654 dtrace_ecbdesc_t
*ep
;
13655 dtrace_enabling_t
*enab
;
13658 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
13659 ASSERT(dof
->dofh_loadsz
>= sizeof (dof_hdr_t
));
13662 * Check the DOF header identification bytes. In addition to checking
13663 * valid settings, we also verify that unused bits/bytes are zeroed so
13664 * we can use them later without fear of regressing existing binaries.
13666 if (bcmp(&dof
->dofh_ident
[DOF_ID_MAG0
],
13667 DOF_MAG_STRING
, DOF_MAG_STRLEN
) != 0) {
13668 dtrace_dof_error(dof
, "DOF magic string mismatch");
13672 if (dof
->dofh_ident
[DOF_ID_MODEL
] != DOF_MODEL_ILP32
&&
13673 dof
->dofh_ident
[DOF_ID_MODEL
] != DOF_MODEL_LP64
) {
13674 dtrace_dof_error(dof
, "DOF has invalid data model");
13678 if (dof
->dofh_ident
[DOF_ID_ENCODING
] != DOF_ENCODE_NATIVE
) {
13679 dtrace_dof_error(dof
, "DOF encoding mismatch");
13684 * APPLE NOTE: Darwin only supports DOF_VERSION_3 for now.
13686 if (dof
->dofh_ident
[DOF_ID_VERSION
] != DOF_VERSION_3
) {
13687 dtrace_dof_error(dof
, "DOF version mismatch");
13691 if (dof
->dofh_ident
[DOF_ID_DIFVERS
] != DIF_VERSION_2
) {
13692 dtrace_dof_error(dof
, "DOF uses unsupported instruction set");
13696 if (dof
->dofh_ident
[DOF_ID_DIFIREG
] > DIF_DIR_NREGS
) {
13697 dtrace_dof_error(dof
, "DOF uses too many integer registers");
13701 if (dof
->dofh_ident
[DOF_ID_DIFTREG
] > DIF_DTR_NREGS
) {
13702 dtrace_dof_error(dof
, "DOF uses too many tuple registers");
13706 for (i
= DOF_ID_PAD
; i
< DOF_ID_SIZE
; i
++) {
13707 if (dof
->dofh_ident
[i
] != 0) {
13708 dtrace_dof_error(dof
, "DOF has invalid ident byte set");
13713 if (dof
->dofh_flags
& ~DOF_FL_VALID
) {
13714 dtrace_dof_error(dof
, "DOF has invalid flag bits set");
13718 if (dof
->dofh_secsize
< sizeof(dof_sec_t
)) {
13719 dtrace_dof_error(dof
, "invalid section header size");
13724 * Check that the section headers don't exceed the amount of DOF
13725 * data. Note that we cast the section size and number of sections
13726 * to uint64_t's to prevent possible overflow in the multiplication.
13728 seclen
= (uint64_t)dof
->dofh_secnum
* (uint64_t)dof
->dofh_secsize
;
13730 if (dof
->dofh_secoff
> len
|| seclen
> len
||
13731 dof
->dofh_secoff
+ seclen
> len
) {
13732 dtrace_dof_error(dof
, "truncated section headers");
13736 if (!IS_P2ALIGNED(dof
->dofh_secoff
, sizeof (uint64_t))) {
13737 dtrace_dof_error(dof
, "misaligned section headers");
13741 if (!IS_P2ALIGNED(dof
->dofh_secsize
, sizeof (uint64_t))) {
13742 dtrace_dof_error(dof
, "misaligned section size");
13747 * Take an initial pass through the section headers to be sure that
13748 * the headers don't have stray offsets. If the 'noprobes' flag is
13749 * set, do not permit sections relating to providers, probes, or args.
13751 for (i
= 0; i
< dof
->dofh_secnum
; i
++) {
13752 dof_sec_t
*sec
= (dof_sec_t
*)(daddr
+
13753 (uintptr_t)dof
->dofh_secoff
+ i
* dof
->dofh_secsize
);
13756 switch (sec
->dofs_type
) {
13757 case DOF_SECT_PROVIDER
:
13758 case DOF_SECT_PROBES
:
13759 case DOF_SECT_PRARGS
:
13760 case DOF_SECT_PROFFS
:
13761 dtrace_dof_error(dof
, "illegal sections "
13767 if (!(sec
->dofs_flags
& DOF_SECF_LOAD
))
13768 continue; /* just ignore non-loadable sections */
13770 if (sec
->dofs_align
& (sec
->dofs_align
- 1)) {
13771 dtrace_dof_error(dof
, "bad section alignment");
13775 if (sec
->dofs_offset
& (sec
->dofs_align
- 1)) {
13776 dtrace_dof_error(dof
, "misaligned section");
13780 if (sec
->dofs_offset
> len
|| sec
->dofs_size
> len
||
13781 sec
->dofs_offset
+ sec
->dofs_size
> len
) {
13782 dtrace_dof_error(dof
, "corrupt section header");
13786 if (sec
->dofs_type
== DOF_SECT_STRTAB
&& *((char *)daddr
+
13787 sec
->dofs_offset
+ sec
->dofs_size
- 1) != '\0') {
13788 dtrace_dof_error(dof
, "non-terminating string table");
13794 * APPLE NOTE: We have no further relocation to perform.
13795 * All dof values are relative offsets.
13798 if ((enab
= *enabp
) == NULL
)
13799 enab
= *enabp
= dtrace_enabling_create(vstate
);
13801 for (i
= 0; i
< dof
->dofh_secnum
; i
++) {
13802 dof_sec_t
*sec
= (dof_sec_t
*)(daddr
+
13803 (uintptr_t)dof
->dofh_secoff
+ i
* dof
->dofh_secsize
);
13805 if (sec
->dofs_type
!= DOF_SECT_ECBDESC
)
13809 * APPLE NOTE: Defend against gcc 4.0 botch on x86.
13810 * not all paths out of inlined dtrace_dof_ecbdesc
13811 * are checked for the NULL return value.
13812 * Check for NULL explicitly here.
13814 ep
= dtrace_dof_ecbdesc(dof
, sec
, vstate
, cr
);
13816 dtrace_enabling_destroy(enab
);
13821 dtrace_enabling_add(enab
, ep
);
13828 * Process DOF for any options. This routine assumes that the DOF has been
13829 * at least processed by dtrace_dof_slurp().
13832 dtrace_dof_options(dof_hdr_t
*dof
, dtrace_state_t
*state
)
13838 dof_optdesc_t
*desc
;
13840 for (i
= 0; i
< dof
->dofh_secnum
; i
++) {
13841 dof_sec_t
*sec
= (dof_sec_t
*)((uintptr_t)dof
+
13842 (uintptr_t)dof
->dofh_secoff
+ i
* dof
->dofh_secsize
);
13844 if (sec
->dofs_type
!= DOF_SECT_OPTDESC
)
13847 if (sec
->dofs_align
!= sizeof (uint64_t)) {
13848 dtrace_dof_error(dof
, "bad alignment in "
13849 "option description");
13853 if ((entsize
= sec
->dofs_entsize
) == 0) {
13854 dtrace_dof_error(dof
, "zeroed option entry size");
13858 if (entsize
< sizeof (dof_optdesc_t
)) {
13859 dtrace_dof_error(dof
, "bad option entry size");
13863 for (offs
= 0; offs
< sec
->dofs_size
; offs
+= entsize
) {
13864 desc
= (dof_optdesc_t
*)((uintptr_t)dof
+
13865 (uintptr_t)sec
->dofs_offset
+ offs
);
13867 if (desc
->dofo_strtab
!= DOF_SECIDX_NONE
) {
13868 dtrace_dof_error(dof
, "non-zero option string");
13872 if (desc
->dofo_value
== (uint64_t)DTRACEOPT_UNSET
) {
13873 dtrace_dof_error(dof
, "unset option");
13877 if ((rval
= dtrace_state_option(state
,
13878 desc
->dofo_option
, desc
->dofo_value
)) != 0) {
13879 dtrace_dof_error(dof
, "rejected option");
13889 * DTrace Consumer State Functions
13892 dtrace_dstate_init(dtrace_dstate_t
*dstate
, size_t size
)
13894 size_t hashsize
, maxper
, min_size
, chunksize
= dstate
->dtds_chunksize
;
13897 dtrace_dynvar_t
*dvar
, *next
, *start
;
13900 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
13901 ASSERT(dstate
->dtds_base
== NULL
&& dstate
->dtds_percpu
== NULL
);
13903 bzero(dstate
, sizeof (dtrace_dstate_t
));
13905 if ((dstate
->dtds_chunksize
= chunksize
) == 0)
13906 dstate
->dtds_chunksize
= DTRACE_DYNVAR_CHUNKSIZE
;
13908 VERIFY(dstate
->dtds_chunksize
< (LONG_MAX
- sizeof (dtrace_dynhash_t
)));
13910 if (size
< (min_size
= dstate
->dtds_chunksize
+ sizeof (dtrace_dynhash_t
)))
13913 if ((base
= kmem_zalloc(size
, KM_NOSLEEP
)) == NULL
)
13916 dstate
->dtds_size
= size
;
13917 dstate
->dtds_base
= base
;
13918 dstate
->dtds_percpu
= kmem_cache_alloc(dtrace_state_cache
, KM_SLEEP
);
13919 bzero(dstate
->dtds_percpu
, (int)NCPU
* sizeof (dtrace_dstate_percpu_t
));
13921 hashsize
= size
/ (dstate
->dtds_chunksize
+ sizeof (dtrace_dynhash_t
));
13923 if (hashsize
!= 1 && (hashsize
& 1))
13926 dstate
->dtds_hashsize
= hashsize
;
13927 dstate
->dtds_hash
= dstate
->dtds_base
;
13930 * Set all of our hash buckets to point to the single sink, and (if
13931 * it hasn't already been set), set the sink's hash value to be the
13932 * sink sentinel value. The sink is needed for dynamic variable
13933 * lookups to know that they have iterated over an entire, valid hash
13936 for (i
= 0; i
< hashsize
; i
++)
13937 dstate
->dtds_hash
[i
].dtdh_chain
= &dtrace_dynhash_sink
;
13939 if (dtrace_dynhash_sink
.dtdv_hashval
!= DTRACE_DYNHASH_SINK
)
13940 dtrace_dynhash_sink
.dtdv_hashval
= DTRACE_DYNHASH_SINK
;
13943 * Determine number of active CPUs. Divide free list evenly among
13946 start
= (dtrace_dynvar_t
*)
13947 ((uintptr_t)base
+ hashsize
* sizeof (dtrace_dynhash_t
));
13948 limit
= (uintptr_t)base
+ size
;
13950 VERIFY((uintptr_t)start
< limit
);
13951 VERIFY((uintptr_t)start
>= (uintptr_t)base
);
13953 maxper
= (limit
- (uintptr_t)start
) / (int)NCPU
;
13954 maxper
= (maxper
/ dstate
->dtds_chunksize
) * dstate
->dtds_chunksize
;
13956 for (i
= 0; i
< NCPU
; i
++) {
13957 dstate
->dtds_percpu
[i
].dtdsc_free
= dvar
= start
;
13960 * If we don't even have enough chunks to make it once through
13961 * NCPUs, we're just going to allocate everything to the first
13962 * CPU. And if we're on the last CPU, we're going to allocate
13963 * whatever is left over. In either case, we set the limit to
13964 * be the limit of the dynamic variable space.
13966 if (maxper
== 0 || i
== NCPU
- 1) {
13967 limit
= (uintptr_t)base
+ size
;
13970 limit
= (uintptr_t)start
+ maxper
;
13971 start
= (dtrace_dynvar_t
*)limit
;
13974 VERIFY(limit
<= (uintptr_t)base
+ size
);
13977 next
= (dtrace_dynvar_t
*)((uintptr_t)dvar
+
13978 dstate
->dtds_chunksize
);
13980 if ((uintptr_t)next
+ dstate
->dtds_chunksize
>= limit
)
13983 VERIFY((uintptr_t)dvar
>= (uintptr_t)base
&&
13984 (uintptr_t)dvar
<= (uintptr_t)base
+ size
);
13985 dvar
->dtdv_next
= next
;
13997 dtrace_dstate_fini(dtrace_dstate_t
*dstate
)
13999 LCK_MTX_ASSERT(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
14001 if (dstate
->dtds_base
== NULL
)
14004 kmem_free(dstate
->dtds_base
, dstate
->dtds_size
);
14005 kmem_cache_free(dtrace_state_cache
, dstate
->dtds_percpu
);
14009 dtrace_vstate_fini(dtrace_vstate_t
*vstate
)
14012 * Logical XOR, where are you?
14014 ASSERT((vstate
->dtvs_nglobals
== 0) ^ (vstate
->dtvs_globals
!= NULL
));
14016 if (vstate
->dtvs_nglobals
> 0) {
14017 kmem_free(vstate
->dtvs_globals
, vstate
->dtvs_nglobals
*
14018 sizeof (dtrace_statvar_t
*));
14021 if (vstate
->dtvs_ntlocals
> 0) {
14022 kmem_free(vstate
->dtvs_tlocals
, vstate
->dtvs_ntlocals
*
14023 sizeof (dtrace_difv_t
));
14026 ASSERT((vstate
->dtvs_nlocals
== 0) ^ (vstate
->dtvs_locals
!= NULL
));
14028 if (vstate
->dtvs_nlocals
> 0) {
14029 kmem_free(vstate
->dtvs_locals
, vstate
->dtvs_nlocals
*
14030 sizeof (dtrace_statvar_t
*));
14035 dtrace_state_clean(dtrace_state_t
*state
)
14037 if (state
->dts_activity
== DTRACE_ACTIVITY_INACTIVE
)
14040 dtrace_dynvar_clean(&state
->dts_vstate
.dtvs_dynvars
);
14041 dtrace_speculation_clean(state
);
14045 dtrace_state_deadman(dtrace_state_t
*state
)
14051 now
= dtrace_gethrtime();
14053 if (state
!= dtrace_anon
.dta_state
&&
14054 now
- state
->dts_laststatus
>= dtrace_deadman_user
)
14058 * We must be sure that dts_alive never appears to be less than the
14059 * value upon entry to dtrace_state_deadman(), and because we lack a
14060 * dtrace_cas64(), we cannot store to it atomically. We thus instead
14061 * store INT64_MAX to it, followed by a memory barrier, followed by
14062 * the new value. This assures that dts_alive never appears to be
14063 * less than its true value, regardless of the order in which the
14064 * stores to the underlying storage are issued.
14066 state
->dts_alive
= INT64_MAX
;
14067 dtrace_membar_producer();
14068 state
->dts_alive
= now
;
14072 dtrace_state_create(dev_t
*devp
, cred_t
*cr
, dtrace_state_t
**new_state
)
14077 dtrace_state_t
*state
;
14078 dtrace_optval_t
*opt
;
14079 int bufsize
= (int)NCPU
* sizeof (dtrace_buffer_t
), i
;
14080 unsigned int cpu_it
;
14082 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
14083 LCK_MTX_ASSERT(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
14085 /* Cause restart */
14088 if (devp
!= NULL
) {
14089 minor
= getminor(*devp
);
14092 minor
= DTRACE_NCLIENTS
- 1;
14095 state
= dtrace_state_allocate(minor
);
14096 if (NULL
== state
) {
14097 printf("dtrace_open: couldn't acquire minor number %d. This usually means that too many DTrace clients are in use at the moment", minor
);
14098 return (ERESTART
); /* can't reacquire */
14101 state
->dts_epid
= DTRACE_EPIDNONE
+ 1;
14103 (void) snprintf(c
, sizeof (c
), "dtrace_aggid_%d", minor
);
14104 state
->dts_aggid_arena
= vmem_create(c
, (void *)1, INT32_MAX
, 1,
14105 NULL
, NULL
, NULL
, 0, VM_SLEEP
| VMC_IDENTIFIER
);
14107 if (devp
!= NULL
) {
14108 major
= getemajor(*devp
);
14110 major
= ddi_driver_major(dtrace_devi
);
14113 state
->dts_dev
= makedev(major
, minor
);
14116 *devp
= state
->dts_dev
;
14119 * We allocate NCPU buffers. On the one hand, this can be quite
14120 * a bit of memory per instance (nearly 36K on a Starcat). On the
14121 * other hand, it saves an additional memory reference in the probe
14124 state
->dts_buffer
= kmem_zalloc(bufsize
, KM_SLEEP
);
14125 state
->dts_aggbuffer
= kmem_zalloc(bufsize
, KM_SLEEP
);
14126 state
->dts_buf_over_limit
= 0;
14129 * Allocate and initialise the per-process per-CPU random state.
14130 * SI_SUB_RANDOM < SI_SUB_DTRACE_ANON therefore entropy device is
14131 * assumed to be seeded at this point (if from Fortuna seed file).
14133 state
->dts_rstate
= kmem_zalloc(NCPU
* sizeof(uint64_t*), KM_SLEEP
);
14134 state
->dts_rstate
[0] = kmem_zalloc(2 * sizeof(uint64_t), KM_SLEEP
);
14135 (void) read_random(state
->dts_rstate
[0], 2 * sizeof(uint64_t));
14136 for (cpu_it
= 1; cpu_it
< NCPU
; cpu_it
++) {
14137 state
->dts_rstate
[cpu_it
] = kmem_zalloc(2 * sizeof(uint64_t), KM_SLEEP
);
14139 * Each CPU is assigned a 2^64 period, non-overlapping
14142 dtrace_xoroshiro128_plus_jump(state
->dts_rstate
[cpu_it
-1],
14143 state
->dts_rstate
[cpu_it
]);
14146 state
->dts_cleaner
= CYCLIC_NONE
;
14147 state
->dts_deadman
= CYCLIC_NONE
;
14148 state
->dts_vstate
.dtvs_state
= state
;
14150 for (i
= 0; i
< DTRACEOPT_MAX
; i
++)
14151 state
->dts_options
[i
] = DTRACEOPT_UNSET
;
14154 * Set the default options.
14156 opt
= state
->dts_options
;
14157 opt
[DTRACEOPT_BUFPOLICY
] = DTRACEOPT_BUFPOLICY_SWITCH
;
14158 opt
[DTRACEOPT_BUFRESIZE
] = DTRACEOPT_BUFRESIZE_AUTO
;
14159 opt
[DTRACEOPT_NSPEC
] = dtrace_nspec_default
;
14160 opt
[DTRACEOPT_SPECSIZE
] = dtrace_specsize_default
;
14161 opt
[DTRACEOPT_CPU
] = (dtrace_optval_t
)DTRACE_CPUALL
;
14162 opt
[DTRACEOPT_STRSIZE
] = dtrace_strsize_default
;
14163 opt
[DTRACEOPT_STACKFRAMES
] = dtrace_stackframes_default
;
14164 opt
[DTRACEOPT_USTACKFRAMES
] = dtrace_ustackframes_default
;
14165 opt
[DTRACEOPT_CLEANRATE
] = dtrace_cleanrate_default
;
14166 opt
[DTRACEOPT_AGGRATE
] = dtrace_aggrate_default
;
14167 opt
[DTRACEOPT_SWITCHRATE
] = dtrace_switchrate_default
;
14168 opt
[DTRACEOPT_STATUSRATE
] = dtrace_statusrate_default
;
14169 opt
[DTRACEOPT_JSTACKFRAMES
] = dtrace_jstackframes_default
;
14170 opt
[DTRACEOPT_JSTACKSTRSIZE
] = dtrace_jstackstrsize_default
;
14171 opt
[DTRACEOPT_BUFLIMIT
] = dtrace_buflimit_default
;
14174 * Depending on the user credentials, we set flag bits which alter probe
14175 * visibility or the amount of destructiveness allowed. In the case of
14176 * actual anonymous tracing, or the possession of all privileges, all of
14177 * the normal checks are bypassed.
14179 #if defined(__APPLE__)
14181 kauth_cred_ref(cr
);
14182 state
->dts_cred
.dcr_cred
= cr
;
14184 if (cr
== NULL
|| PRIV_POLICY_ONLY(cr
, PRIV_ALL
, B_FALSE
)) {
14185 if (dtrace_is_restricted() && !dtrace_are_restrictions_relaxed()) {
14187 * Allow only proc credentials when DTrace is
14188 * restricted by the current security policy
14190 state
->dts_cred
.dcr_visible
= DTRACE_CRV_ALLPROC
;
14191 state
->dts_cred
.dcr_action
= DTRACE_CRA_PROC
| DTRACE_CRA_PROC_CONTROL
| DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER
;
14194 state
->dts_cred
.dcr_visible
= DTRACE_CRV_ALL
;
14195 state
->dts_cred
.dcr_action
= DTRACE_CRA_ALL
;
14200 if (cr
== NULL
|| PRIV_POLICY_ONLY(cr
, PRIV_ALL
, B_FALSE
)) {
14201 state
->dts_cred
.dcr_visible
= DTRACE_CRV_ALL
;
14202 state
->dts_cred
.dcr_action
= DTRACE_CRA_ALL
;
14206 * Set up the credentials for this instantiation. We take a
14207 * hold on the credential to prevent it from disappearing on
14208 * us; this in turn prevents the zone_t referenced by this
14209 * credential from disappearing. This means that we can
14210 * examine the credential and the zone from probe context.
14213 state
->dts_cred
.dcr_cred
= cr
;
14216 * CRA_PROC means "we have *some* privilege for dtrace" and
14217 * unlocks the use of variables like pid, zonename, etc.
14219 if (PRIV_POLICY_ONLY(cr
, PRIV_DTRACE_USER
, B_FALSE
) ||
14220 PRIV_POLICY_ONLY(cr
, PRIV_DTRACE_PROC
, B_FALSE
)) {
14221 state
->dts_cred
.dcr_action
|= DTRACE_CRA_PROC
;
14225 * dtrace_user allows use of syscall and profile providers.
14226 * If the user also has proc_owner and/or proc_zone, we
14227 * extend the scope to include additional visibility and
14228 * destructive power.
14230 if (PRIV_POLICY_ONLY(cr
, PRIV_DTRACE_USER
, B_FALSE
)) {
14231 if (PRIV_POLICY_ONLY(cr
, PRIV_PROC_OWNER
, B_FALSE
)) {
14232 state
->dts_cred
.dcr_visible
|=
14233 DTRACE_CRV_ALLPROC
;
14235 state
->dts_cred
.dcr_action
|=
14236 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER
;
14239 if (PRIV_POLICY_ONLY(cr
, PRIV_PROC_ZONE
, B_FALSE
)) {
14240 state
->dts_cred
.dcr_visible
|=
14241 DTRACE_CRV_ALLZONE
;
14243 state
->dts_cred
.dcr_action
|=
14244 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE
;
14248 * If we have all privs in whatever zone this is,
14249 * we can do destructive things to processes which
14250 * have altered credentials.
14252 * APPLE NOTE: Darwin doesn't do zones.
14253 * Behave as if zone always has destructive privs.
14256 state
->dts_cred
.dcr_action
|=
14257 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG
;
14261 * Holding the dtrace_kernel privilege also implies that
14262 * the user has the dtrace_user privilege from a visibility
14263 * perspective. But without further privileges, some
14264 * destructive actions are not available.
14266 if (PRIV_POLICY_ONLY(cr
, PRIV_DTRACE_KERNEL
, B_FALSE
)) {
14268 * Make all probes in all zones visible. However,
14269 * this doesn't mean that all actions become available
14272 state
->dts_cred
.dcr_visible
|= DTRACE_CRV_KERNEL
|
14273 DTRACE_CRV_ALLPROC
| DTRACE_CRV_ALLZONE
;
14275 state
->dts_cred
.dcr_action
|= DTRACE_CRA_KERNEL
|
14278 * Holding proc_owner means that destructive actions
14279 * for *this* zone are allowed.
14281 if (PRIV_POLICY_ONLY(cr
, PRIV_PROC_OWNER
, B_FALSE
))
14282 state
->dts_cred
.dcr_action
|=
14283 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER
;
14286 * Holding proc_zone means that destructive actions
14287 * for this user/group ID in all zones is allowed.
14289 if (PRIV_POLICY_ONLY(cr
, PRIV_PROC_ZONE
, B_FALSE
))
14290 state
->dts_cred
.dcr_action
|=
14291 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE
;
14294 * If we have all privs in whatever zone this is,
14295 * we can do destructive things to processes which
14296 * have altered credentials.
14298 * APPLE NOTE: Darwin doesn't do zones.
14299 * Behave as if zone always has destructive privs.
14301 state
->dts_cred
.dcr_action
|=
14302 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG
;
14306 * Holding the dtrace_proc privilege gives control over fasttrap
14307 * and pid providers. We need to grant wider destructive
14308 * privileges in the event that the user has proc_owner and/or
14311 if (PRIV_POLICY_ONLY(cr
, PRIV_DTRACE_PROC
, B_FALSE
)) {
14312 if (PRIV_POLICY_ONLY(cr
, PRIV_PROC_OWNER
, B_FALSE
))
14313 state
->dts_cred
.dcr_action
|=
14314 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER
;
14316 if (PRIV_POLICY_ONLY(cr
, PRIV_PROC_ZONE
, B_FALSE
))
14317 state
->dts_cred
.dcr_action
|=
14318 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE
;
14323 *new_state
= state
;
14324 return(0); /* Success */
14328 dtrace_state_buffer(dtrace_state_t
*state
, dtrace_buffer_t
*buf
, int which
)
14330 dtrace_optval_t
*opt
= state
->dts_options
, size
;
14331 processorid_t cpu
= 0;
14332 size_t limit
= buf
->dtb_size
;
14333 int flags
= 0, rval
;
14335 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
14336 LCK_MTX_ASSERT(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
14337 ASSERT(which
< DTRACEOPT_MAX
);
14338 ASSERT(state
->dts_activity
== DTRACE_ACTIVITY_INACTIVE
||
14339 (state
== dtrace_anon
.dta_state
&&
14340 state
->dts_activity
== DTRACE_ACTIVITY_ACTIVE
));
14342 if (opt
[which
] == DTRACEOPT_UNSET
|| opt
[which
] == 0)
14345 if (opt
[DTRACEOPT_CPU
] != DTRACEOPT_UNSET
)
14346 cpu
= opt
[DTRACEOPT_CPU
];
14348 if (which
== DTRACEOPT_SPECSIZE
)
14349 flags
|= DTRACEBUF_NOSWITCH
;
14351 if (which
== DTRACEOPT_BUFSIZE
) {
14352 if (opt
[DTRACEOPT_BUFPOLICY
] == DTRACEOPT_BUFPOLICY_RING
)
14353 flags
|= DTRACEBUF_RING
;
14355 if (opt
[DTRACEOPT_BUFPOLICY
] == DTRACEOPT_BUFPOLICY_FILL
)
14356 flags
|= DTRACEBUF_FILL
;
14358 if (state
!= dtrace_anon
.dta_state
||
14359 state
->dts_activity
!= DTRACE_ACTIVITY_ACTIVE
)
14360 flags
|= DTRACEBUF_INACTIVE
;
14363 for (size
= opt
[which
]; (size_t)size
>= sizeof (uint64_t); size
>>= 1) {
14365 * The size must be 8-byte aligned. If the size is not 8-byte
14366 * aligned, drop it down by the difference.
14368 if (size
& (sizeof (uint64_t) - 1))
14369 size
-= size
& (sizeof (uint64_t) - 1);
14371 if (size
< state
->dts_reserve
) {
14373 * Buffers always must be large enough to accommodate
14374 * their prereserved space. We return E2BIG instead
14375 * of ENOMEM in this case to allow for user-level
14376 * software to differentiate the cases.
14380 limit
= opt
[DTRACEOPT_BUFLIMIT
] * size
/ 100;
14381 rval
= dtrace_buffer_alloc(buf
, limit
, size
, flags
, cpu
);
14383 if (rval
!= ENOMEM
) {
14388 if (opt
[DTRACEOPT_BUFRESIZE
] == DTRACEOPT_BUFRESIZE_MANUAL
)
14396 dtrace_state_buffers(dtrace_state_t
*state
)
14398 dtrace_speculation_t
*spec
= state
->dts_speculations
;
14401 if ((rval
= dtrace_state_buffer(state
, state
->dts_buffer
,
14402 DTRACEOPT_BUFSIZE
)) != 0)
14405 if ((rval
= dtrace_state_buffer(state
, state
->dts_aggbuffer
,
14406 DTRACEOPT_AGGSIZE
)) != 0)
14409 for (i
= 0; i
< state
->dts_nspeculations
; i
++) {
14410 if ((rval
= dtrace_state_buffer(state
,
14411 spec
[i
].dtsp_buffer
, DTRACEOPT_SPECSIZE
)) != 0)
14419 dtrace_state_prereserve(dtrace_state_t
*state
)
14422 dtrace_probe_t
*probe
;
14424 state
->dts_reserve
= 0;
14426 if (state
->dts_options
[DTRACEOPT_BUFPOLICY
] != DTRACEOPT_BUFPOLICY_FILL
)
14430 * If our buffer policy is a "fill" buffer policy, we need to set the
14431 * prereserved space to be the space required by the END probes.
14433 probe
= dtrace_probes
[dtrace_probeid_end
- 1];
14434 ASSERT(probe
!= NULL
);
14436 for (ecb
= probe
->dtpr_ecb
; ecb
!= NULL
; ecb
= ecb
->dte_next
) {
14437 if (ecb
->dte_state
!= state
)
14440 state
->dts_reserve
+= ecb
->dte_needed
+ ecb
->dte_alignment
;
14445 dtrace_state_go(dtrace_state_t
*state
, processorid_t
*cpu
)
14447 dtrace_optval_t
*opt
= state
->dts_options
, sz
, nspec
;
14448 dtrace_speculation_t
*spec
;
14449 dtrace_buffer_t
*buf
;
14450 cyc_handler_t hdlr
;
14452 int rval
= 0, i
, bufsize
= (int)NCPU
* sizeof (dtrace_buffer_t
);
14453 dtrace_icookie_t cookie
;
14455 lck_mtx_lock(&cpu_lock
);
14456 lck_mtx_lock(&dtrace_lock
);
14458 if (state
->dts_activity
!= DTRACE_ACTIVITY_INACTIVE
) {
14464 * Before we can perform any checks, we must prime all of the
14465 * retained enablings that correspond to this state.
14467 dtrace_enabling_prime(state
);
14469 if (state
->dts_destructive
&& !state
->dts_cred
.dcr_destructive
) {
14474 dtrace_state_prereserve(state
);
14477 * Now we want to do is try to allocate our speculations.
14478 * We do not automatically resize the number of speculations; if
14479 * this fails, we will fail the operation.
14481 nspec
= opt
[DTRACEOPT_NSPEC
];
14482 ASSERT(nspec
!= DTRACEOPT_UNSET
);
14484 if (nspec
> INT_MAX
) {
14489 spec
= kmem_zalloc(nspec
* sizeof (dtrace_speculation_t
), KM_NOSLEEP
);
14491 if (spec
== NULL
) {
14496 state
->dts_speculations
= spec
;
14497 state
->dts_nspeculations
= (int)nspec
;
14499 for (i
= 0; i
< nspec
; i
++) {
14500 if ((buf
= kmem_zalloc(bufsize
, KM_NOSLEEP
)) == NULL
) {
14505 spec
[i
].dtsp_buffer
= buf
;
14508 if (opt
[DTRACEOPT_GRABANON
] != DTRACEOPT_UNSET
) {
14509 if (dtrace_anon
.dta_state
== NULL
) {
14514 if (state
->dts_necbs
!= 0) {
14519 state
->dts_anon
= dtrace_anon_grab();
14520 ASSERT(state
->dts_anon
!= NULL
);
14521 state
= state
->dts_anon
;
14524 * We want "grabanon" to be set in the grabbed state, so we'll
14525 * copy that option value from the grabbing state into the
14528 state
->dts_options
[DTRACEOPT_GRABANON
] =
14529 opt
[DTRACEOPT_GRABANON
];
14531 *cpu
= dtrace_anon
.dta_beganon
;
14534 * If the anonymous state is active (as it almost certainly
14535 * is if the anonymous enabling ultimately matched anything),
14536 * we don't allow any further option processing -- but we
14537 * don't return failure.
14539 if (state
->dts_activity
!= DTRACE_ACTIVITY_INACTIVE
)
14543 if (opt
[DTRACEOPT_AGGSIZE
] != DTRACEOPT_UNSET
&&
14544 opt
[DTRACEOPT_AGGSIZE
] != 0) {
14545 if (state
->dts_aggregations
== NULL
) {
14547 * We're not going to create an aggregation buffer
14548 * because we don't have any ECBs that contain
14549 * aggregations -- set this option to 0.
14551 opt
[DTRACEOPT_AGGSIZE
] = 0;
14554 * If we have an aggregation buffer, we must also have
14555 * a buffer to use as scratch.
14557 if (opt
[DTRACEOPT_BUFSIZE
] == DTRACEOPT_UNSET
||
14558 (size_t)opt
[DTRACEOPT_BUFSIZE
] < state
->dts_needed
) {
14559 opt
[DTRACEOPT_BUFSIZE
] = state
->dts_needed
;
14564 if (opt
[DTRACEOPT_SPECSIZE
] != DTRACEOPT_UNSET
&&
14565 opt
[DTRACEOPT_SPECSIZE
] != 0) {
14566 if (!state
->dts_speculates
) {
14568 * We're not going to create speculation buffers
14569 * because we don't have any ECBs that actually
14570 * speculate -- set the speculation size to 0.
14572 opt
[DTRACEOPT_SPECSIZE
] = 0;
14577 * The bare minimum size for any buffer that we're actually going to
14578 * do anything to is sizeof (uint64_t).
14580 sz
= sizeof (uint64_t);
14582 if ((state
->dts_needed
!= 0 && opt
[DTRACEOPT_BUFSIZE
] < sz
) ||
14583 (state
->dts_speculates
&& opt
[DTRACEOPT_SPECSIZE
] < sz
) ||
14584 (state
->dts_aggregations
!= NULL
&& opt
[DTRACEOPT_AGGSIZE
] < sz
)) {
14586 * A buffer size has been explicitly set to 0 (or to a size
14587 * that will be adjusted to 0) and we need the space -- we
14588 * need to return failure. We return ENOSPC to differentiate
14589 * it from failing to allocate a buffer due to failure to meet
14590 * the reserve (for which we return E2BIG).
14596 if ((rval
= dtrace_state_buffers(state
)) != 0)
14599 if ((sz
= opt
[DTRACEOPT_DYNVARSIZE
]) == DTRACEOPT_UNSET
)
14600 sz
= dtrace_dstate_defsize
;
14603 rval
= dtrace_dstate_init(&state
->dts_vstate
.dtvs_dynvars
, sz
);
14608 if (opt
[DTRACEOPT_BUFRESIZE
] == DTRACEOPT_BUFRESIZE_MANUAL
)
14610 } while (sz
>>= 1);
14612 opt
[DTRACEOPT_DYNVARSIZE
] = sz
;
14617 if (opt
[DTRACEOPT_STATUSRATE
] > dtrace_statusrate_max
)
14618 opt
[DTRACEOPT_STATUSRATE
] = dtrace_statusrate_max
;
14620 if (opt
[DTRACEOPT_CLEANRATE
] == 0)
14621 opt
[DTRACEOPT_CLEANRATE
] = dtrace_cleanrate_max
;
14623 if (opt
[DTRACEOPT_CLEANRATE
] < dtrace_cleanrate_min
)
14624 opt
[DTRACEOPT_CLEANRATE
] = dtrace_cleanrate_min
;
14626 if (opt
[DTRACEOPT_CLEANRATE
] > dtrace_cleanrate_max
)
14627 opt
[DTRACEOPT_CLEANRATE
] = dtrace_cleanrate_max
;
14629 if (opt
[DTRACEOPT_STRSIZE
] > dtrace_strsize_max
)
14630 opt
[DTRACEOPT_STRSIZE
] = dtrace_strsize_max
;
14632 if (opt
[DTRACEOPT_STRSIZE
] < dtrace_strsize_min
)
14633 opt
[DTRACEOPT_STRSIZE
] = dtrace_strsize_min
;
14635 if (opt
[DTRACEOPT_BUFLIMIT
] > dtrace_buflimit_max
)
14636 opt
[DTRACEOPT_BUFLIMIT
] = dtrace_buflimit_max
;
14638 if (opt
[DTRACEOPT_BUFLIMIT
] < dtrace_buflimit_min
)
14639 opt
[DTRACEOPT_BUFLIMIT
] = dtrace_buflimit_min
;
14641 hdlr
.cyh_func
= (cyc_func_t
)dtrace_state_clean
;
14642 hdlr
.cyh_arg
= state
;
14643 hdlr
.cyh_level
= CY_LOW_LEVEL
;
14646 when
.cyt_interval
= opt
[DTRACEOPT_CLEANRATE
];
14648 state
->dts_cleaner
= cyclic_add(&hdlr
, &when
);
14650 hdlr
.cyh_func
= (cyc_func_t
)dtrace_state_deadman
;
14651 hdlr
.cyh_arg
= state
;
14652 hdlr
.cyh_level
= CY_LOW_LEVEL
;
14655 when
.cyt_interval
= dtrace_deadman_interval
;
14657 state
->dts_alive
= state
->dts_laststatus
= dtrace_gethrtime();
14658 state
->dts_deadman
= cyclic_add(&hdlr
, &when
);
14660 state
->dts_activity
= DTRACE_ACTIVITY_WARMUP
;
14663 * Now it's time to actually fire the BEGIN probe. We need to disable
14664 * interrupts here both to record the CPU on which we fired the BEGIN
14665 * probe (the data from this CPU will be processed first at user
14666 * level) and to manually activate the buffer for this CPU.
14668 cookie
= dtrace_interrupt_disable();
14669 *cpu
= CPU
->cpu_id
;
14670 ASSERT(state
->dts_buffer
[*cpu
].dtb_flags
& DTRACEBUF_INACTIVE
);
14671 state
->dts_buffer
[*cpu
].dtb_flags
&= ~DTRACEBUF_INACTIVE
;
14673 dtrace_probe(dtrace_probeid_begin
,
14674 (uint64_t)(uintptr_t)state
, 0, 0, 0, 0);
14675 dtrace_interrupt_enable(cookie
);
14677 * We may have had an exit action from a BEGIN probe; only change our
14678 * state to ACTIVE if we're still in WARMUP.
14680 ASSERT(state
->dts_activity
== DTRACE_ACTIVITY_WARMUP
||
14681 state
->dts_activity
== DTRACE_ACTIVITY_DRAINING
);
14683 if (state
->dts_activity
== DTRACE_ACTIVITY_WARMUP
)
14684 state
->dts_activity
= DTRACE_ACTIVITY_ACTIVE
;
14687 * Regardless of whether or not now we're in ACTIVE or DRAINING, we
14688 * want each CPU to transition its principal buffer out of the
14689 * INACTIVE state. Doing this assures that no CPU will suddenly begin
14690 * processing an ECB halfway down a probe's ECB chain; all CPUs will
14691 * atomically transition from processing none of a state's ECBs to
14692 * processing all of them.
14694 dtrace_xcall(DTRACE_CPUALL
,
14695 (dtrace_xcall_t
)dtrace_buffer_activate
, state
);
14699 dtrace_buffer_free(state
->dts_buffer
);
14700 dtrace_buffer_free(state
->dts_aggbuffer
);
14702 if ((nspec
= state
->dts_nspeculations
) == 0) {
14703 ASSERT(state
->dts_speculations
== NULL
);
14707 spec
= state
->dts_speculations
;
14708 ASSERT(spec
!= NULL
);
14710 for (i
= 0; i
< state
->dts_nspeculations
; i
++) {
14711 if ((buf
= spec
[i
].dtsp_buffer
) == NULL
)
14714 dtrace_buffer_free(buf
);
14715 kmem_free(buf
, bufsize
);
14718 kmem_free(spec
, nspec
* sizeof (dtrace_speculation_t
));
14719 state
->dts_nspeculations
= 0;
14720 state
->dts_speculations
= NULL
;
14723 lck_mtx_unlock(&dtrace_lock
);
14724 lck_mtx_unlock(&cpu_lock
);
14730 dtrace_state_stop(dtrace_state_t
*state
, processorid_t
*cpu
)
14732 dtrace_icookie_t cookie
;
14734 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
14736 if (state
->dts_activity
!= DTRACE_ACTIVITY_ACTIVE
&&
14737 state
->dts_activity
!= DTRACE_ACTIVITY_DRAINING
)
14741 * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync
14742 * to be sure that every CPU has seen it. See below for the details
14743 * on why this is done.
14745 state
->dts_activity
= DTRACE_ACTIVITY_DRAINING
;
14749 * By this point, it is impossible for any CPU to be still processing
14750 * with DTRACE_ACTIVITY_ACTIVE. We can thus set our activity to
14751 * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any
14752 * other CPU in dtrace_buffer_reserve(). This allows dtrace_probe()
14753 * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN
14754 * iff we're in the END probe.
14756 state
->dts_activity
= DTRACE_ACTIVITY_COOLDOWN
;
14758 ASSERT(state
->dts_activity
== DTRACE_ACTIVITY_COOLDOWN
);
14761 * Finally, we can release the reserve and call the END probe. We
14762 * disable interrupts across calling the END probe to allow us to
14763 * return the CPU on which we actually called the END probe. This
14764 * allows user-land to be sure that this CPU's principal buffer is
14767 state
->dts_reserve
= 0;
14769 cookie
= dtrace_interrupt_disable();
14770 *cpu
= CPU
->cpu_id
;
14771 dtrace_probe(dtrace_probeid_end
,
14772 (uint64_t)(uintptr_t)state
, 0, 0, 0, 0);
14773 dtrace_interrupt_enable(cookie
);
14775 state
->dts_activity
= DTRACE_ACTIVITY_STOPPED
;
14782 dtrace_state_option(dtrace_state_t
*state
, dtrace_optid_t option
,
14783 dtrace_optval_t val
)
14785 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
14787 if (state
->dts_activity
!= DTRACE_ACTIVITY_INACTIVE
)
14790 if (option
>= DTRACEOPT_MAX
)
14793 if (option
!= DTRACEOPT_CPU
&& val
< 0)
14797 case DTRACEOPT_DESTRUCTIVE
:
14798 if (dtrace_destructive_disallow
)
14801 state
->dts_cred
.dcr_destructive
= 1;
14804 case DTRACEOPT_BUFSIZE
:
14805 case DTRACEOPT_DYNVARSIZE
:
14806 case DTRACEOPT_AGGSIZE
:
14807 case DTRACEOPT_SPECSIZE
:
14808 case DTRACEOPT_STRSIZE
:
14812 if (val
>= LONG_MAX
) {
14814 * If this is an otherwise negative value, set it to
14815 * the highest multiple of 128m less than LONG_MAX.
14816 * Technically, we're adjusting the size without
14817 * regard to the buffer resizing policy, but in fact,
14818 * this has no effect -- if we set the buffer size to
14819 * ~LONG_MAX and the buffer policy is ultimately set to
14820 * be "manual", the buffer allocation is guaranteed to
14821 * fail, if only because the allocation requires two
14822 * buffers. (We set the the size to the highest
14823 * multiple of 128m because it ensures that the size
14824 * will remain a multiple of a megabyte when
14825 * repeatedly halved -- all the way down to 15m.)
14827 val
= LONG_MAX
- (1 << 27) + 1;
14831 state
->dts_options
[option
] = val
;
14837 dtrace_state_destroy(dtrace_state_t
*state
)
14840 dtrace_vstate_t
*vstate
= &state
->dts_vstate
;
14841 minor_t minor
= getminor(state
->dts_dev
);
14842 int i
, bufsize
= (int)NCPU
* sizeof (dtrace_buffer_t
);
14843 dtrace_speculation_t
*spec
= state
->dts_speculations
;
14844 int nspec
= state
->dts_nspeculations
;
14847 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
14848 LCK_MTX_ASSERT(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
14851 * First, retract any retained enablings for this state.
14853 dtrace_enabling_retract(state
);
14854 ASSERT(state
->dts_nretained
== 0);
14856 if (state
->dts_activity
== DTRACE_ACTIVITY_ACTIVE
||
14857 state
->dts_activity
== DTRACE_ACTIVITY_DRAINING
) {
14859 * We have managed to come into dtrace_state_destroy() on a
14860 * hot enabling -- almost certainly because of a disorderly
14861 * shutdown of a consumer. (That is, a consumer that is
14862 * exiting without having called dtrace_stop().) In this case,
14863 * we're going to set our activity to be KILLED, and then
14864 * issue a sync to be sure that everyone is out of probe
14865 * context before we start blowing away ECBs.
14867 state
->dts_activity
= DTRACE_ACTIVITY_KILLED
;
14872 * Release the credential hold we took in dtrace_state_create().
14874 if (state
->dts_cred
.dcr_cred
!= NULL
)
14875 kauth_cred_unref(&state
->dts_cred
.dcr_cred
);
14878 * Now we can safely disable and destroy any enabled probes. Because
14879 * any DTRACE_PRIV_KERNEL probes may actually be slowing our progress
14880 * (especially if they're all enabled), we take two passes through the
14881 * ECBs: in the first, we disable just DTRACE_PRIV_KERNEL probes, and
14882 * in the second we disable whatever is left over.
14884 for (match
= DTRACE_PRIV_KERNEL
; ; match
= 0) {
14885 for (i
= 0; i
< state
->dts_necbs
; i
++) {
14886 if ((ecb
= state
->dts_ecbs
[i
]) == NULL
)
14889 if (match
&& ecb
->dte_probe
!= NULL
) {
14890 dtrace_probe_t
*probe
= ecb
->dte_probe
;
14891 dtrace_provider_t
*prov
= probe
->dtpr_provider
;
14893 if (!(prov
->dtpv_priv
.dtpp_flags
& match
))
14897 dtrace_ecb_disable(ecb
);
14898 dtrace_ecb_destroy(ecb
);
14906 * Before we free the buffers, perform one more sync to assure that
14907 * every CPU is out of probe context.
14911 dtrace_buffer_free(state
->dts_buffer
);
14912 dtrace_buffer_free(state
->dts_aggbuffer
);
14914 for (i
= 0; i
< (int)NCPU
; i
++) {
14915 kmem_free(state
->dts_rstate
[i
], 2 * sizeof(uint64_t));
14917 kmem_free(state
->dts_rstate
, NCPU
* sizeof(uint64_t*));
14919 for (i
= 0; i
< nspec
; i
++)
14920 dtrace_buffer_free(spec
[i
].dtsp_buffer
);
14922 if (state
->dts_cleaner
!= CYCLIC_NONE
)
14923 cyclic_remove(state
->dts_cleaner
);
14925 if (state
->dts_deadman
!= CYCLIC_NONE
)
14926 cyclic_remove(state
->dts_deadman
);
14928 dtrace_dstate_fini(&vstate
->dtvs_dynvars
);
14929 dtrace_vstate_fini(vstate
);
14930 kmem_free(state
->dts_ecbs
, state
->dts_necbs
* sizeof (dtrace_ecb_t
*));
14932 if (state
->dts_aggregations
!= NULL
) {
14934 for (i
= 0; i
< state
->dts_naggregations
; i
++)
14935 ASSERT(state
->dts_aggregations
[i
] == NULL
);
14937 ASSERT(state
->dts_naggregations
> 0);
14938 kmem_free(state
->dts_aggregations
,
14939 state
->dts_naggregations
* sizeof (dtrace_aggregation_t
*));
14942 kmem_free(state
->dts_buffer
, bufsize
);
14943 kmem_free(state
->dts_aggbuffer
, bufsize
);
14945 for (i
= 0; i
< nspec
; i
++)
14946 kmem_free(spec
[i
].dtsp_buffer
, bufsize
);
14948 kmem_free(spec
, nspec
* sizeof (dtrace_speculation_t
));
14950 dtrace_format_destroy(state
);
14952 vmem_destroy(state
->dts_aggid_arena
);
14953 dtrace_state_free(minor
);
14957 * DTrace Anonymous Enabling Functions
14961 dtrace_keep_kernel_symbols(void)
14963 if (dtrace_is_restricted() && !dtrace_are_restrictions_relaxed()) {
14967 if (dtrace_kernel_symbol_mode
== DTRACE_KERNEL_SYMBOLS_ALWAYS_FROM_KERNEL
)
14973 static dtrace_state_t
*
14974 dtrace_anon_grab(void)
14976 dtrace_state_t
*state
;
14978 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
14980 if ((state
= dtrace_anon
.dta_state
) == NULL
) {
14981 ASSERT(dtrace_anon
.dta_enabling
== NULL
);
14985 ASSERT(dtrace_anon
.dta_enabling
!= NULL
);
14986 ASSERT(dtrace_retained
!= NULL
);
14988 dtrace_enabling_destroy(dtrace_anon
.dta_enabling
);
14989 dtrace_anon
.dta_enabling
= NULL
;
14990 dtrace_anon
.dta_state
= NULL
;
14996 dtrace_anon_property(void)
14999 dtrace_state_t
*state
;
15001 char c
[32]; /* enough for "dof-data-" + digits */
15003 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
15004 LCK_MTX_ASSERT(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
15006 for (i
= 0; ; i
++) {
15007 (void) snprintf(c
, sizeof (c
), "dof-data-%d", i
);
15009 dtrace_err_verbose
= 1;
15011 if ((dof
= dtrace_dof_property(c
)) == NULL
) {
15012 dtrace_err_verbose
= 0;
15018 * We want to create anonymous state, so we need to transition
15019 * the kernel debugger to indicate that DTrace is active. If
15020 * this fails (e.g. because the debugger has modified text in
15021 * some way), we won't continue with the processing.
15023 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE
) != 0) {
15024 cmn_err(CE_NOTE
, "kernel debugger active; anonymous "
15025 "enabling ignored.");
15026 dtrace_dof_destroy(dof
);
15032 * If we haven't allocated an anonymous state, we'll do so now.
15034 if ((state
= dtrace_anon
.dta_state
) == NULL
) {
15035 rv
= dtrace_state_create(NULL
, NULL
, &state
);
15036 dtrace_anon
.dta_state
= state
;
15037 if (rv
!= 0 || state
== NULL
) {
15039 * This basically shouldn't happen: the only
15040 * failure mode from dtrace_state_create() is a
15041 * failure of ddi_soft_state_zalloc() that
15042 * itself should never happen. Still, the
15043 * interface allows for a failure mode, and
15044 * we want to fail as gracefully as possible:
15045 * we'll emit an error message and cease
15046 * processing anonymous state in this case.
15048 cmn_err(CE_WARN
, "failed to create "
15049 "anonymous state");
15050 dtrace_dof_destroy(dof
);
15055 rv
= dtrace_dof_slurp(dof
, &state
->dts_vstate
, CRED(),
15056 &dtrace_anon
.dta_enabling
, 0, B_TRUE
);
15059 rv
= dtrace_dof_options(dof
, state
);
15061 dtrace_err_verbose
= 0;
15062 dtrace_dof_destroy(dof
);
15066 * This is malformed DOF; chuck any anonymous state
15069 ASSERT(dtrace_anon
.dta_enabling
== NULL
);
15070 dtrace_state_destroy(state
);
15071 dtrace_anon
.dta_state
= NULL
;
15075 ASSERT(dtrace_anon
.dta_enabling
!= NULL
);
15078 if (dtrace_anon
.dta_enabling
!= NULL
) {
15082 * dtrace_enabling_retain() can only fail because we are
15083 * trying to retain more enablings than are allowed -- but
15084 * we only have one anonymous enabling, and we are guaranteed
15085 * to be allowed at least one retained enabling; we assert
15086 * that dtrace_enabling_retain() returns success.
15088 rval
= dtrace_enabling_retain(dtrace_anon
.dta_enabling
);
15091 dtrace_enabling_dump(dtrace_anon
.dta_enabling
);
15096 * DTrace Helper Functions
15099 dtrace_helper_trace(dtrace_helper_action_t
*helper
,
15100 dtrace_mstate_t
*mstate
, dtrace_vstate_t
*vstate
, int where
)
15102 uint32_t size
, next
, nnext
;
15104 dtrace_helptrace_t
*ent
;
15105 uint16_t flags
= cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
15107 if (!dtrace_helptrace_enabled
)
15110 ASSERT((uint32_t)vstate
->dtvs_nlocals
<= dtrace_helptrace_nlocals
);
15113 * What would a tracing framework be without its own tracing
15114 * framework? (Well, a hell of a lot simpler, for starters...)
15116 size
= sizeof (dtrace_helptrace_t
) + dtrace_helptrace_nlocals
*
15117 sizeof (uint64_t) - sizeof (uint64_t);
15120 * Iterate until we can allocate a slot in the trace buffer.
15123 next
= dtrace_helptrace_next
;
15125 if (next
+ size
< dtrace_helptrace_bufsize
) {
15126 nnext
= next
+ size
;
15130 } while (dtrace_cas32(&dtrace_helptrace_next
, next
, nnext
) != next
);
15133 * We have our slot; fill it in.
15138 ent
= (dtrace_helptrace_t
*)&dtrace_helptrace_buffer
[next
];
15139 ent
->dtht_helper
= helper
;
15140 ent
->dtht_where
= where
;
15141 ent
->dtht_nlocals
= vstate
->dtvs_nlocals
;
15143 ent
->dtht_fltoffs
= (mstate
->dtms_present
& DTRACE_MSTATE_FLTOFFS
) ?
15144 mstate
->dtms_fltoffs
: -1;
15145 ent
->dtht_fault
= DTRACE_FLAGS2FLT(flags
);
15146 ent
->dtht_illval
= cpu_core
[CPU
->cpu_id
].cpuc_dtrace_illval
;
15148 for (i
= 0; i
< vstate
->dtvs_nlocals
; i
++) {
15149 dtrace_statvar_t
*svar
;
15151 if ((svar
= vstate
->dtvs_locals
[i
]) == NULL
)
15154 ASSERT(svar
->dtsv_size
>= (int)NCPU
* sizeof (uint64_t));
15155 ent
->dtht_locals
[i
] =
15156 ((uint64_t *)(uintptr_t)svar
->dtsv_data
)[CPU
->cpu_id
];
15160 __attribute__((noinline
))
15162 dtrace_helper(int which
, dtrace_mstate_t
*mstate
,
15163 dtrace_state_t
*state
, uint64_t arg0
, uint64_t arg1
)
15165 uint16_t *flags
= &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
15166 uint64_t sarg0
= mstate
->dtms_arg
[0];
15167 uint64_t sarg1
= mstate
->dtms_arg
[1];
15169 dtrace_helpers_t
*helpers
= curproc
->p_dtrace_helpers
;
15170 dtrace_helper_action_t
*helper
;
15171 dtrace_vstate_t
*vstate
;
15172 dtrace_difo_t
*pred
;
15173 int i
, trace
= dtrace_helptrace_enabled
;
15175 ASSERT(which
>= 0 && which
< DTRACE_NHELPER_ACTIONS
);
15177 if (helpers
== NULL
)
15180 if ((helper
= helpers
->dthps_actions
[which
]) == NULL
)
15183 vstate
= &helpers
->dthps_vstate
;
15184 mstate
->dtms_arg
[0] = arg0
;
15185 mstate
->dtms_arg
[1] = arg1
;
15188 * Now iterate over each helper. If its predicate evaluates to 'true',
15189 * we'll call the corresponding actions. Note that the below calls
15190 * to dtrace_dif_emulate() may set faults in machine state. This is
15191 * okay: our caller (the outer dtrace_dif_emulate()) will simply plow
15192 * the stored DIF offset with its own (which is the desired behavior).
15193 * Also, note the calls to dtrace_dif_emulate() may allocate scratch
15194 * from machine state; this is okay, too.
15196 for (; helper
!= NULL
; helper
= helper
->dtha_next
) {
15197 if ((pred
= helper
->dtha_predicate
) != NULL
) {
15199 dtrace_helper_trace(helper
, mstate
, vstate
, 0);
15201 if (!dtrace_dif_emulate(pred
, mstate
, vstate
, state
))
15204 if (*flags
& CPU_DTRACE_FAULT
)
15208 for (i
= 0; i
< helper
->dtha_nactions
; i
++) {
15210 dtrace_helper_trace(helper
,
15211 mstate
, vstate
, i
+ 1);
15213 rval
= dtrace_dif_emulate(helper
->dtha_actions
[i
],
15214 mstate
, vstate
, state
);
15216 if (*flags
& CPU_DTRACE_FAULT
)
15222 dtrace_helper_trace(helper
, mstate
, vstate
,
15223 DTRACE_HELPTRACE_NEXT
);
15227 dtrace_helper_trace(helper
, mstate
, vstate
,
15228 DTRACE_HELPTRACE_DONE
);
15231 * Restore the arg0 that we saved upon entry.
15233 mstate
->dtms_arg
[0] = sarg0
;
15234 mstate
->dtms_arg
[1] = sarg1
;
15240 dtrace_helper_trace(helper
, mstate
, vstate
,
15241 DTRACE_HELPTRACE_ERR
);
15244 * Restore the arg0 that we saved upon entry.
15246 mstate
->dtms_arg
[0] = sarg0
;
15247 mstate
->dtms_arg
[1] = sarg1
;
15253 dtrace_helper_action_destroy(dtrace_helper_action_t
*helper
,
15254 dtrace_vstate_t
*vstate
)
15258 if (helper
->dtha_predicate
!= NULL
)
15259 dtrace_difo_release(helper
->dtha_predicate
, vstate
);
15261 for (i
= 0; i
< helper
->dtha_nactions
; i
++) {
15262 ASSERT(helper
->dtha_actions
[i
] != NULL
);
15263 dtrace_difo_release(helper
->dtha_actions
[i
], vstate
);
15266 kmem_free(helper
->dtha_actions
,
15267 helper
->dtha_nactions
* sizeof (dtrace_difo_t
*));
15268 kmem_free(helper
, sizeof (dtrace_helper_action_t
));
15272 dtrace_helper_destroygen(proc_t
* p
, int gen
)
15274 dtrace_helpers_t
*help
= p
->p_dtrace_helpers
;
15275 dtrace_vstate_t
*vstate
;
15278 LCK_MTX_ASSERT(&dtrace_meta_lock
, LCK_MTX_ASSERT_OWNED
);
15279 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
15281 if (help
== NULL
|| gen
> help
->dthps_generation
)
15284 vstate
= &help
->dthps_vstate
;
15286 for (i
= 0; i
< DTRACE_NHELPER_ACTIONS
; i
++) {
15287 dtrace_helper_action_t
*last
= NULL
, *h
, *next
;
15289 for (h
= help
->dthps_actions
[i
]; h
!= NULL
; h
= next
) {
15290 next
= h
->dtha_next
;
15292 if (h
->dtha_generation
== gen
) {
15293 if (last
!= NULL
) {
15294 last
->dtha_next
= next
;
15296 help
->dthps_actions
[i
] = next
;
15299 dtrace_helper_action_destroy(h
, vstate
);
15307 * Interate until we've cleared out all helper providers with the
15308 * given generation number.
15311 dtrace_helper_provider_t
*prov
= NULL
;
15314 * Look for a helper provider with the right generation. We
15315 * have to start back at the beginning of the list each time
15316 * because we drop dtrace_lock. It's unlikely that we'll make
15317 * more than two passes.
15319 for (i
= 0; i
< help
->dthps_nprovs
; i
++) {
15320 prov
= help
->dthps_provs
[i
];
15322 if (prov
->dthp_generation
== gen
)
15327 * If there were no matches, we're done.
15329 if (i
== help
->dthps_nprovs
)
15333 * Move the last helper provider into this slot.
15335 help
->dthps_nprovs
--;
15336 help
->dthps_provs
[i
] = help
->dthps_provs
[help
->dthps_nprovs
];
15337 help
->dthps_provs
[help
->dthps_nprovs
] = NULL
;
15339 lck_mtx_unlock(&dtrace_lock
);
15342 * If we have a meta provider, remove this helper provider.
15344 if (dtrace_meta_pid
!= NULL
) {
15345 ASSERT(dtrace_deferred_pid
== NULL
);
15346 dtrace_helper_provider_remove(&prov
->dthp_prov
,
15350 dtrace_helper_provider_destroy(prov
);
15352 lck_mtx_lock(&dtrace_lock
);
15359 dtrace_helper_validate(dtrace_helper_action_t
*helper
)
15364 if ((dp
= helper
->dtha_predicate
) != NULL
)
15365 err
+= dtrace_difo_validate_helper(dp
);
15367 for (i
= 0; i
< helper
->dtha_nactions
; i
++)
15368 err
+= dtrace_difo_validate_helper(helper
->dtha_actions
[i
]);
15374 dtrace_helper_action_add(proc_t
* p
, int which
, dtrace_ecbdesc_t
*ep
)
15376 dtrace_helpers_t
*help
;
15377 dtrace_helper_action_t
*helper
, *last
;
15378 dtrace_actdesc_t
*act
;
15379 dtrace_vstate_t
*vstate
;
15380 dtrace_predicate_t
*pred
;
15381 int count
= 0, nactions
= 0, i
;
15383 if (which
< 0 || which
>= DTRACE_NHELPER_ACTIONS
)
15386 help
= p
->p_dtrace_helpers
;
15387 last
= help
->dthps_actions
[which
];
15388 vstate
= &help
->dthps_vstate
;
15390 for (count
= 0; last
!= NULL
; last
= last
->dtha_next
) {
15392 if (last
->dtha_next
== NULL
)
15397 * If we already have dtrace_helper_actions_max helper actions for this
15398 * helper action type, we'll refuse to add a new one.
15400 if (count
>= dtrace_helper_actions_max
)
15403 helper
= kmem_zalloc(sizeof (dtrace_helper_action_t
), KM_SLEEP
);
15404 helper
->dtha_generation
= help
->dthps_generation
;
15406 if ((pred
= ep
->dted_pred
.dtpdd_predicate
) != NULL
) {
15407 ASSERT(pred
->dtp_difo
!= NULL
);
15408 dtrace_difo_hold(pred
->dtp_difo
);
15409 helper
->dtha_predicate
= pred
->dtp_difo
;
15412 for (act
= ep
->dted_action
; act
!= NULL
; act
= act
->dtad_next
) {
15413 if (act
->dtad_kind
!= DTRACEACT_DIFEXPR
)
15416 if (act
->dtad_difo
== NULL
)
15422 helper
->dtha_actions
= kmem_zalloc(sizeof (dtrace_difo_t
*) *
15423 (helper
->dtha_nactions
= nactions
), KM_SLEEP
);
15425 for (act
= ep
->dted_action
, i
= 0; act
!= NULL
; act
= act
->dtad_next
) {
15426 dtrace_difo_hold(act
->dtad_difo
);
15427 helper
->dtha_actions
[i
++] = act
->dtad_difo
;
15430 if (!dtrace_helper_validate(helper
))
15433 if (last
== NULL
) {
15434 help
->dthps_actions
[which
] = helper
;
15436 last
->dtha_next
= helper
;
15439 if ((uint32_t)vstate
->dtvs_nlocals
> dtrace_helptrace_nlocals
) {
15440 dtrace_helptrace_nlocals
= vstate
->dtvs_nlocals
;
15441 dtrace_helptrace_next
= 0;
15446 dtrace_helper_action_destroy(helper
, vstate
);
15451 dtrace_helper_provider_register(proc_t
*p
, dtrace_helpers_t
*help
,
15452 dof_helper_t
*dofhp
)
15454 LCK_MTX_ASSERT(&dtrace_meta_lock
, LCK_MTX_ASSERT_OWNED
);
15455 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_NOTOWNED
);
15457 lck_mtx_lock(&dtrace_lock
);
15459 if (!dtrace_attached() || dtrace_meta_pid
== NULL
) {
15461 * If the dtrace module is loaded but not attached, or if
15462 * there aren't isn't a meta provider registered to deal with
15463 * these provider descriptions, we need to postpone creating
15464 * the actual providers until later.
15467 if (help
->dthps_next
== NULL
&& help
->dthps_prev
== NULL
&&
15468 dtrace_deferred_pid
!= help
) {
15469 help
->dthps_deferred
= 1;
15470 help
->dthps_pid
= p
->p_pid
;
15471 help
->dthps_next
= dtrace_deferred_pid
;
15472 help
->dthps_prev
= NULL
;
15473 if (dtrace_deferred_pid
!= NULL
)
15474 dtrace_deferred_pid
->dthps_prev
= help
;
15475 dtrace_deferred_pid
= help
;
15478 lck_mtx_unlock(&dtrace_lock
);
15480 } else if (dofhp
!= NULL
) {
15482 * If the dtrace module is loaded and we have a particular
15483 * helper provider description, pass that off to the
15487 lck_mtx_unlock(&dtrace_lock
);
15489 dtrace_helper_provide(dofhp
, p
);
15493 * Otherwise, just pass all the helper provider descriptions
15494 * off to the meta provider.
15498 lck_mtx_unlock(&dtrace_lock
);
15500 for (i
= 0; i
< help
->dthps_nprovs
; i
++) {
15501 dtrace_helper_provide(&help
->dthps_provs
[i
]->dthp_prov
,
15508 dtrace_helper_provider_add(proc_t
* p
, dof_helper_t
*dofhp
, int gen
)
15510 dtrace_helpers_t
*help
;
15511 dtrace_helper_provider_t
*hprov
, **tmp_provs
;
15512 uint_t tmp_maxprovs
, i
;
15514 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
15515 help
= p
->p_dtrace_helpers
;
15516 ASSERT(help
!= NULL
);
15519 * If we already have dtrace_helper_providers_max helper providers,
15520 * we're refuse to add a new one.
15522 if (help
->dthps_nprovs
>= dtrace_helper_providers_max
)
15526 * Check to make sure this isn't a duplicate.
15528 for (i
= 0; i
< help
->dthps_nprovs
; i
++) {
15529 if (dofhp
->dofhp_addr
==
15530 help
->dthps_provs
[i
]->dthp_prov
.dofhp_addr
)
15534 hprov
= kmem_zalloc(sizeof (dtrace_helper_provider_t
), KM_SLEEP
);
15535 hprov
->dthp_prov
= *dofhp
;
15536 hprov
->dthp_ref
= 1;
15537 hprov
->dthp_generation
= gen
;
15540 * Allocate a bigger table for helper providers if it's already full.
15542 if (help
->dthps_maxprovs
== help
->dthps_nprovs
) {
15543 tmp_maxprovs
= help
->dthps_maxprovs
;
15544 tmp_provs
= help
->dthps_provs
;
15546 if (help
->dthps_maxprovs
== 0)
15547 help
->dthps_maxprovs
= 2;
15549 help
->dthps_maxprovs
*= 2;
15550 if (help
->dthps_maxprovs
> dtrace_helper_providers_max
)
15551 help
->dthps_maxprovs
= dtrace_helper_providers_max
;
15553 ASSERT(tmp_maxprovs
< help
->dthps_maxprovs
);
15555 help
->dthps_provs
= kmem_zalloc(help
->dthps_maxprovs
*
15556 sizeof (dtrace_helper_provider_t
*), KM_SLEEP
);
15558 if (tmp_provs
!= NULL
) {
15559 bcopy(tmp_provs
, help
->dthps_provs
, tmp_maxprovs
*
15560 sizeof (dtrace_helper_provider_t
*));
15561 kmem_free(tmp_provs
, tmp_maxprovs
*
15562 sizeof (dtrace_helper_provider_t
*));
15566 help
->dthps_provs
[help
->dthps_nprovs
] = hprov
;
15567 help
->dthps_nprovs
++;
15573 dtrace_helper_provider_destroy(dtrace_helper_provider_t
*hprov
)
15575 lck_mtx_lock(&dtrace_lock
);
15577 if (--hprov
->dthp_ref
== 0) {
15579 lck_mtx_unlock(&dtrace_lock
);
15580 dof
= (dof_hdr_t
*)(uintptr_t)hprov
->dthp_prov
.dofhp_dof
;
15581 dtrace_dof_destroy(dof
);
15582 kmem_free(hprov
, sizeof (dtrace_helper_provider_t
));
15584 lck_mtx_unlock(&dtrace_lock
);
15589 dtrace_helper_provider_validate(dof_hdr_t
*dof
, dof_sec_t
*sec
)
15591 uintptr_t daddr
= (uintptr_t)dof
;
15592 dof_sec_t
*str_sec
, *prb_sec
, *arg_sec
, *off_sec
, *enoff_sec
;
15593 dof_provider_t
*provider
;
15594 dof_probe_t
*probe
;
15596 char *strtab
, *typestr
;
15597 dof_stridx_t typeidx
;
15599 uint_t nprobes
, j
, k
;
15601 ASSERT(sec
->dofs_type
== DOF_SECT_PROVIDER
);
15603 if (sec
->dofs_offset
& (sizeof (uint_t
) - 1)) {
15604 dtrace_dof_error(dof
, "misaligned section offset");
15609 * The section needs to be large enough to contain the DOF provider
15610 * structure appropriate for the given version.
15612 if (sec
->dofs_size
<
15613 ((dof
->dofh_ident
[DOF_ID_VERSION
] == DOF_VERSION_1
) ?
15614 offsetof(dof_provider_t
, dofpv_prenoffs
) :
15615 sizeof (dof_provider_t
))) {
15616 dtrace_dof_error(dof
, "provider section too small");
15620 provider
= (dof_provider_t
*)(uintptr_t)(daddr
+ sec
->dofs_offset
);
15621 str_sec
= dtrace_dof_sect(dof
, DOF_SECT_STRTAB
, provider
->dofpv_strtab
);
15622 prb_sec
= dtrace_dof_sect(dof
, DOF_SECT_PROBES
, provider
->dofpv_probes
);
15623 arg_sec
= dtrace_dof_sect(dof
, DOF_SECT_PRARGS
, provider
->dofpv_prargs
);
15624 off_sec
= dtrace_dof_sect(dof
, DOF_SECT_PROFFS
, provider
->dofpv_proffs
);
15626 if (str_sec
== NULL
|| prb_sec
== NULL
||
15627 arg_sec
== NULL
|| off_sec
== NULL
)
15632 if (dof
->dofh_ident
[DOF_ID_VERSION
] != DOF_VERSION_1
&&
15633 provider
->dofpv_prenoffs
!= DOF_SECT_NONE
&&
15634 (enoff_sec
= dtrace_dof_sect(dof
, DOF_SECT_PRENOFFS
,
15635 provider
->dofpv_prenoffs
)) == NULL
)
15638 strtab
= (char *)(uintptr_t)(daddr
+ str_sec
->dofs_offset
);
15640 if (provider
->dofpv_name
>= str_sec
->dofs_size
||
15641 strlen(strtab
+ provider
->dofpv_name
) >= DTRACE_PROVNAMELEN
) {
15642 dtrace_dof_error(dof
, "invalid provider name");
15646 if (prb_sec
->dofs_entsize
== 0 ||
15647 prb_sec
->dofs_entsize
> prb_sec
->dofs_size
) {
15648 dtrace_dof_error(dof
, "invalid entry size");
15652 if (prb_sec
->dofs_entsize
& (sizeof (uintptr_t) - 1)) {
15653 dtrace_dof_error(dof
, "misaligned entry size");
15657 if (off_sec
->dofs_entsize
!= sizeof (uint32_t)) {
15658 dtrace_dof_error(dof
, "invalid entry size");
15662 if (off_sec
->dofs_offset
& (sizeof (uint32_t) - 1)) {
15663 dtrace_dof_error(dof
, "misaligned section offset");
15667 if (arg_sec
->dofs_entsize
!= sizeof (uint8_t)) {
15668 dtrace_dof_error(dof
, "invalid entry size");
15672 arg
= (uint8_t *)(uintptr_t)(daddr
+ arg_sec
->dofs_offset
);
15674 nprobes
= prb_sec
->dofs_size
/ prb_sec
->dofs_entsize
;
15677 * Take a pass through the probes to check for errors.
15679 for (j
= 0; j
< nprobes
; j
++) {
15680 probe
= (dof_probe_t
*)(uintptr_t)(daddr
+
15681 prb_sec
->dofs_offset
+ j
* prb_sec
->dofs_entsize
);
15683 if (probe
->dofpr_func
>= str_sec
->dofs_size
) {
15684 dtrace_dof_error(dof
, "invalid function name");
15688 if (strlen(strtab
+ probe
->dofpr_func
) >= DTRACE_FUNCNAMELEN
) {
15689 dtrace_dof_error(dof
, "function name too long");
15693 if (probe
->dofpr_name
>= str_sec
->dofs_size
||
15694 strlen(strtab
+ probe
->dofpr_name
) >= DTRACE_NAMELEN
) {
15695 dtrace_dof_error(dof
, "invalid probe name");
15700 * The offset count must not wrap the index, and the offsets
15701 * must also not overflow the section's data.
15703 if (probe
->dofpr_offidx
+ probe
->dofpr_noffs
<
15704 probe
->dofpr_offidx
||
15705 (probe
->dofpr_offidx
+ probe
->dofpr_noffs
) *
15706 off_sec
->dofs_entsize
> off_sec
->dofs_size
) {
15707 dtrace_dof_error(dof
, "invalid probe offset");
15711 if (dof
->dofh_ident
[DOF_ID_VERSION
] != DOF_VERSION_1
) {
15713 * If there's no is-enabled offset section, make sure
15714 * there aren't any is-enabled offsets. Otherwise
15715 * perform the same checks as for probe offsets
15716 * (immediately above).
15718 if (enoff_sec
== NULL
) {
15719 if (probe
->dofpr_enoffidx
!= 0 ||
15720 probe
->dofpr_nenoffs
!= 0) {
15721 dtrace_dof_error(dof
, "is-enabled "
15722 "offsets with null section");
15725 } else if (probe
->dofpr_enoffidx
+
15726 probe
->dofpr_nenoffs
< probe
->dofpr_enoffidx
||
15727 (probe
->dofpr_enoffidx
+ probe
->dofpr_nenoffs
) *
15728 enoff_sec
->dofs_entsize
> enoff_sec
->dofs_size
) {
15729 dtrace_dof_error(dof
, "invalid is-enabled "
15734 if (probe
->dofpr_noffs
+ probe
->dofpr_nenoffs
== 0) {
15735 dtrace_dof_error(dof
, "zero probe and "
15736 "is-enabled offsets");
15739 } else if (probe
->dofpr_noffs
== 0) {
15740 dtrace_dof_error(dof
, "zero probe offsets");
15744 if (probe
->dofpr_argidx
+ probe
->dofpr_xargc
<
15745 probe
->dofpr_argidx
||
15746 (probe
->dofpr_argidx
+ probe
->dofpr_xargc
) *
15747 arg_sec
->dofs_entsize
> arg_sec
->dofs_size
) {
15748 dtrace_dof_error(dof
, "invalid args");
15752 typeidx
= probe
->dofpr_nargv
;
15753 typestr
= strtab
+ probe
->dofpr_nargv
;
15754 for (k
= 0; k
< probe
->dofpr_nargc
; k
++) {
15755 if (typeidx
>= str_sec
->dofs_size
) {
15756 dtrace_dof_error(dof
, "bad "
15757 "native argument type");
15761 typesz
= strlen(typestr
) + 1;
15762 if (typesz
> DTRACE_ARGTYPELEN
) {
15763 dtrace_dof_error(dof
, "native "
15764 "argument type too long");
15771 typeidx
= probe
->dofpr_xargv
;
15772 typestr
= strtab
+ probe
->dofpr_xargv
;
15773 for (k
= 0; k
< probe
->dofpr_xargc
; k
++) {
15774 if (arg
[probe
->dofpr_argidx
+ k
] > probe
->dofpr_nargc
) {
15775 dtrace_dof_error(dof
, "bad "
15776 "native argument index");
15780 if (typeidx
>= str_sec
->dofs_size
) {
15781 dtrace_dof_error(dof
, "bad "
15782 "translated argument type");
15786 typesz
= strlen(typestr
) + 1;
15787 if (typesz
> DTRACE_ARGTYPELEN
) {
15788 dtrace_dof_error(dof
, "translated argument "
15802 dtrace_helper_slurp(proc_t
* p
, dof_hdr_t
*dof
, dof_helper_t
*dhp
)
15804 dtrace_helpers_t
*help
;
15805 dtrace_vstate_t
*vstate
;
15806 dtrace_enabling_t
*enab
= NULL
;
15807 int i
, gen
, rv
, nhelpers
= 0, nprovs
= 0, destroy
= 1;
15808 uintptr_t daddr
= (uintptr_t)dof
;
15810 LCK_MTX_ASSERT(&dtrace_meta_lock
, LCK_MTX_ASSERT_OWNED
);
15811 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
15813 if ((help
= p
->p_dtrace_helpers
) == NULL
)
15814 help
= dtrace_helpers_create(p
);
15816 vstate
= &help
->dthps_vstate
;
15818 if ((rv
= dtrace_dof_slurp(dof
, vstate
, NULL
, &enab
,
15819 dhp
!= NULL
? dhp
->dofhp_addr
: 0, B_FALSE
)) != 0) {
15820 dtrace_dof_destroy(dof
);
15825 * Look for helper providers and validate their descriptions.
15828 for (i
= 0; (uint32_t)i
< dof
->dofh_secnum
; i
++) {
15829 dof_sec_t
*sec
= (dof_sec_t
*)(uintptr_t)(daddr
+
15830 dof
->dofh_secoff
+ i
* dof
->dofh_secsize
);
15832 if (sec
->dofs_type
!= DOF_SECT_PROVIDER
)
15835 if (dtrace_helper_provider_validate(dof
, sec
) != 0) {
15836 dtrace_enabling_destroy(enab
);
15837 dtrace_dof_destroy(dof
);
15846 * Now we need to walk through the ECB descriptions in the enabling.
15848 for (i
= 0; i
< enab
->dten_ndesc
; i
++) {
15849 dtrace_ecbdesc_t
*ep
= enab
->dten_desc
[i
];
15850 dtrace_probedesc_t
*desc
= &ep
->dted_probe
;
15852 /* APPLE NOTE: Darwin employs size bounded string operation. */
15853 if (!LIT_STRNEQL(desc
->dtpd_provider
, "dtrace"))
15856 if (!LIT_STRNEQL(desc
->dtpd_mod
, "helper"))
15859 if (!LIT_STRNEQL(desc
->dtpd_func
, "ustack"))
15862 if ((rv
= dtrace_helper_action_add(p
, DTRACE_HELPER_ACTION_USTACK
,
15865 * Adding this helper action failed -- we are now going
15866 * to rip out the entire generation and return failure.
15868 (void) dtrace_helper_destroygen(p
, help
->dthps_generation
);
15869 dtrace_enabling_destroy(enab
);
15870 dtrace_dof_destroy(dof
);
15877 if (nhelpers
< enab
->dten_ndesc
)
15878 dtrace_dof_error(dof
, "unmatched helpers");
15880 gen
= help
->dthps_generation
++;
15881 dtrace_enabling_destroy(enab
);
15883 if (dhp
!= NULL
&& nprovs
> 0) {
15884 dhp
->dofhp_dof
= (uint64_t)(uintptr_t)dof
;
15885 if (dtrace_helper_provider_add(p
, dhp
, gen
) == 0) {
15886 lck_mtx_unlock(&dtrace_lock
);
15887 dtrace_helper_provider_register(p
, help
, dhp
);
15888 lck_mtx_lock(&dtrace_lock
);
15895 dtrace_dof_destroy(dof
);
15901 * APPLE NOTE: DTrace lazy dof implementation
15903 * DTrace user static probes (USDT probes) and helper actions are loaded
15904 * in a process by proccessing dof sections. The dof sections are passed
15905 * into the kernel by dyld, in a dof_ioctl_data_t block. It is rather
15906 * expensive to process dof for a process that will never use it. There
15907 * is a memory cost (allocating the providers/probes), and a cpu cost
15908 * (creating the providers/probes).
15910 * To reduce this cost, we use "lazy dof". The normal proceedure for
15911 * dof processing is to copyin the dof(s) pointed to by the dof_ioctl_data_t
15912 * block, and invoke dof_slurp_helper() on them. When "lazy dof" is
15913 * used, each process retains the dof_ioctl_data_t block, instead of
15914 * copying in the data it points to.
15916 * The dof_ioctl_data_t blocks are managed as if they were the actual
15917 * processed dof; on fork the block is copied to the child, on exec and
15918 * exit the block is freed.
15920 * If the process loads library(s) containing additional dof, the
15921 * new dof_ioctl_data_t is merged with the existing block.
15923 * There are a few catches that make this slightly more difficult.
15924 * When dyld registers dof_ioctl_data_t blocks, it expects a unique
15925 * identifier value for each dof in the block. In non-lazy dof terms,
15926 * this is the generation that dof was loaded in. If we hand back
15927 * a UID for a lazy dof, that same UID must be able to unload the
15928 * dof once it has become non-lazy. To meet this requirement, the
15929 * code that loads lazy dof requires that the UID's for dof(s) in
15930 * the lazy dof be sorted, and in ascending order. It is okay to skip
15931 * UID's, I.E., 1 -> 5 -> 6 is legal.
15933 * Once a process has become non-lazy, it will stay non-lazy. All
15934 * future dof operations for that process will be non-lazy, even
15935 * if the dof mode transitions back to lazy.
15937 * Always do lazy dof checks before non-lazy (I.E. In fork, exit, exec.).
15938 * That way if the lazy check fails due to transitioning to non-lazy, the
15939 * right thing is done with the newly faulted in dof.
15943 * This method is a bit squicky. It must handle:
15945 * dof should not be lazy.
15946 * dof should have been handled lazily, but there was an error
15947 * dof was handled lazily, and needs to be freed.
15948 * dof was handled lazily, and must not be freed.
15951 * Returns EACCESS if dof should be handled non-lazily.
15953 * KERN_SUCCESS and all other return codes indicate lazy handling of dof.
15955 * If the dofs data is claimed by this method, dofs_claimed will be set.
15956 * Callers should not free claimed dofs.
15959 dtrace_lazy_dofs_add(proc_t
*p
, dof_ioctl_data_t
* incoming_dofs
, int *dofs_claimed
)
15962 ASSERT(incoming_dofs
&& incoming_dofs
->dofiod_count
> 0);
15967 lck_rw_lock_shared(&dtrace_dof_mode_lock
);
15969 ASSERT(p
->p_dtrace_lazy_dofs
== NULL
|| p
->p_dtrace_helpers
== NULL
);
15970 ASSERT(dtrace_dof_mode
!= DTRACE_DOF_MODE_NEVER
);
15973 * Any existing helpers force non-lazy behavior.
15975 if (dtrace_dof_mode
== DTRACE_DOF_MODE_LAZY_ON
&& (p
->p_dtrace_helpers
== NULL
)) {
15978 dof_ioctl_data_t
* existing_dofs
= p
->p_dtrace_lazy_dofs
;
15979 unsigned int existing_dofs_count
= (existing_dofs
) ? existing_dofs
->dofiod_count
: 0;
15980 unsigned int i
, merged_dofs_count
= incoming_dofs
->dofiod_count
+ existing_dofs_count
;
15985 if (merged_dofs_count
== 0 || merged_dofs_count
> 1024) {
15986 dtrace_dof_error(NULL
, "lazy_dofs_add merged_dofs_count out of range");
15992 * Each dof being added must be assigned a unique generation.
15994 uint64_t generation
= (existing_dofs
) ? existing_dofs
->dofiod_helpers
[existing_dofs_count
- 1].dofhp_dof
+ 1 : 1;
15995 for (i
=0; i
<incoming_dofs
->dofiod_count
; i
++) {
15997 * We rely on these being the same so we can overwrite dofhp_dof and not lose info.
15999 ASSERT(incoming_dofs
->dofiod_helpers
[i
].dofhp_dof
== incoming_dofs
->dofiod_helpers
[i
].dofhp_addr
);
16000 incoming_dofs
->dofiod_helpers
[i
].dofhp_dof
= generation
++;
16004 if (existing_dofs
) {
16006 * Merge the existing and incoming dofs
16008 size_t merged_dofs_size
= DOF_IOCTL_DATA_T_SIZE(merged_dofs_count
);
16009 dof_ioctl_data_t
* merged_dofs
= kmem_alloc(merged_dofs_size
, KM_SLEEP
);
16011 bcopy(&existing_dofs
->dofiod_helpers
[0],
16012 &merged_dofs
->dofiod_helpers
[0],
16013 sizeof(dof_helper_t
) * existing_dofs_count
);
16014 bcopy(&incoming_dofs
->dofiod_helpers
[0],
16015 &merged_dofs
->dofiod_helpers
[existing_dofs_count
],
16016 sizeof(dof_helper_t
) * incoming_dofs
->dofiod_count
);
16018 merged_dofs
->dofiod_count
= merged_dofs_count
;
16020 kmem_free(existing_dofs
, DOF_IOCTL_DATA_T_SIZE(existing_dofs_count
));
16022 p
->p_dtrace_lazy_dofs
= merged_dofs
;
16025 * Claim the incoming dofs
16028 p
->p_dtrace_lazy_dofs
= incoming_dofs
;
16032 dof_ioctl_data_t
* all_dofs
= p
->p_dtrace_lazy_dofs
;
16033 for (i
=0; i
<all_dofs
->dofiod_count
-1; i
++) {
16034 ASSERT(all_dofs
->dofiod_helpers
[i
].dofhp_dof
< all_dofs
->dofiod_helpers
[i
+1].dofhp_dof
);
16039 dtrace_sprunlock(p
);
16044 lck_rw_unlock_shared(&dtrace_dof_mode_lock
);
16052 * EINVAL: lazy dof is enabled, but the requested generation was not found.
16053 * EACCES: This removal needs to be handled non-lazily.
16056 dtrace_lazy_dofs_remove(proc_t
*p
, int generation
)
16060 lck_rw_lock_shared(&dtrace_dof_mode_lock
);
16062 ASSERT(p
->p_dtrace_lazy_dofs
== NULL
|| p
->p_dtrace_helpers
== NULL
);
16063 ASSERT(dtrace_dof_mode
!= DTRACE_DOF_MODE_NEVER
);
16066 * Any existing helpers force non-lazy behavior.
16068 if (dtrace_dof_mode
== DTRACE_DOF_MODE_LAZY_ON
&& (p
->p_dtrace_helpers
== NULL
)) {
16071 dof_ioctl_data_t
* existing_dofs
= p
->p_dtrace_lazy_dofs
;
16073 if (existing_dofs
) {
16074 int index
, existing_dofs_count
= existing_dofs
->dofiod_count
;
16075 for (index
=0; index
<existing_dofs_count
; index
++) {
16076 if ((int)existing_dofs
->dofiod_helpers
[index
].dofhp_dof
== generation
) {
16077 dof_ioctl_data_t
* removed_dofs
= NULL
;
16080 * If there is only 1 dof, we'll delete it and swap in NULL.
16082 if (existing_dofs_count
> 1) {
16083 int removed_dofs_count
= existing_dofs_count
- 1;
16084 size_t removed_dofs_size
= DOF_IOCTL_DATA_T_SIZE(removed_dofs_count
);
16086 removed_dofs
= kmem_alloc(removed_dofs_size
, KM_SLEEP
);
16087 removed_dofs
->dofiod_count
= removed_dofs_count
;
16090 * copy the remaining data.
16093 bcopy(&existing_dofs
->dofiod_helpers
[0],
16094 &removed_dofs
->dofiod_helpers
[0],
16095 index
* sizeof(dof_helper_t
));
16098 if (index
< existing_dofs_count
-1) {
16099 bcopy(&existing_dofs
->dofiod_helpers
[index
+1],
16100 &removed_dofs
->dofiod_helpers
[index
],
16101 (existing_dofs_count
- index
- 1) * sizeof(dof_helper_t
));
16105 kmem_free(existing_dofs
, DOF_IOCTL_DATA_T_SIZE(existing_dofs_count
));
16107 p
->p_dtrace_lazy_dofs
= removed_dofs
;
16109 rval
= KERN_SUCCESS
;
16116 dof_ioctl_data_t
* all_dofs
= p
->p_dtrace_lazy_dofs
;
16119 for (i
=0; i
<all_dofs
->dofiod_count
-1; i
++) {
16120 ASSERT(all_dofs
->dofiod_helpers
[i
].dofhp_dof
< all_dofs
->dofiod_helpers
[i
+1].dofhp_dof
);
16126 dtrace_sprunlock(p
);
16131 lck_rw_unlock_shared(&dtrace_dof_mode_lock
);
16137 dtrace_lazy_dofs_destroy(proc_t
*p
)
16139 lck_rw_lock_shared(&dtrace_dof_mode_lock
);
16142 ASSERT(p
->p_dtrace_lazy_dofs
== NULL
|| p
->p_dtrace_helpers
== NULL
);
16144 dof_ioctl_data_t
* lazy_dofs
= p
->p_dtrace_lazy_dofs
;
16145 p
->p_dtrace_lazy_dofs
= NULL
;
16147 dtrace_sprunlock(p
);
16148 lck_rw_unlock_shared(&dtrace_dof_mode_lock
);
16151 kmem_free(lazy_dofs
, DOF_IOCTL_DATA_T_SIZE(lazy_dofs
->dofiod_count
));
16156 dtrace_lazy_dofs_proc_iterate_filter(proc_t
*p
, void* ignored
)
16158 #pragma unused(ignored)
16160 * Okay to NULL test without taking the sprlock.
16162 return p
->p_dtrace_lazy_dofs
!= NULL
;
16166 dtrace_lazy_dofs_process(proc_t
*p
) {
16168 * It is possible this process may exit during our attempt to
16169 * fault in the dof. We could fix this by holding locks longer,
16170 * but the errors are benign.
16175 ASSERT(p
->p_dtrace_lazy_dofs
== NULL
|| p
->p_dtrace_helpers
== NULL
);
16176 ASSERT(dtrace_dof_mode
== DTRACE_DOF_MODE_LAZY_OFF
);
16178 dof_ioctl_data_t
* lazy_dofs
= p
->p_dtrace_lazy_dofs
;
16179 p
->p_dtrace_lazy_dofs
= NULL
;
16181 dtrace_sprunlock(p
);
16182 lck_mtx_lock(&dtrace_meta_lock
);
16184 * Process each dof_helper_t
16186 if (lazy_dofs
!= NULL
) {
16190 for (i
=0; i
<lazy_dofs
->dofiod_count
; i
++) {
16192 * When loading lazy dof, we depend on the generations being sorted in ascending order.
16194 ASSERT(i
>= (lazy_dofs
->dofiod_count
- 1) || lazy_dofs
->dofiod_helpers
[i
].dofhp_dof
< lazy_dofs
->dofiod_helpers
[i
+1].dofhp_dof
);
16196 dof_helper_t
*dhp
= &lazy_dofs
->dofiod_helpers
[i
];
16199 * We stored the generation in dofhp_dof. Save it, and restore the original value.
16201 int generation
= dhp
->dofhp_dof
;
16202 dhp
->dofhp_dof
= dhp
->dofhp_addr
;
16204 dof_hdr_t
*dof
= dtrace_dof_copyin_from_proc(p
, dhp
->dofhp_dof
, &rval
);
16207 dtrace_helpers_t
*help
;
16209 lck_mtx_lock(&dtrace_lock
);
16212 * This must be done with the dtrace_lock held
16214 if ((help
= p
->p_dtrace_helpers
) == NULL
)
16215 help
= dtrace_helpers_create(p
);
16218 * If the generation value has been bumped, someone snuck in
16219 * when we released the dtrace lock. We have to dump this generation,
16220 * there is no safe way to load it.
16222 if (help
->dthps_generation
<= generation
) {
16223 help
->dthps_generation
= generation
;
16226 * dtrace_helper_slurp() takes responsibility for the dof --
16227 * it may free it now or it may save it and free it later.
16229 if ((rval
= dtrace_helper_slurp(p
, dof
, dhp
)) != generation
) {
16230 dtrace_dof_error(NULL
, "returned value did not match expected generation");
16234 lck_mtx_unlock(&dtrace_lock
);
16237 lck_mtx_unlock(&dtrace_meta_lock
);
16238 kmem_free(lazy_dofs
, DOF_IOCTL_DATA_T_SIZE(lazy_dofs
->dofiod_count
));
16240 lck_mtx_unlock(&dtrace_meta_lock
);
16245 dtrace_lazy_dofs_proc_iterate_doit(proc_t
*p
, void* ignored
)
16247 #pragma unused(ignored)
16249 dtrace_lazy_dofs_process(p
);
16251 return PROC_RETURNED
;
16254 #define DTRACE_LAZY_DOFS_DUPLICATED 1
16257 dtrace_lazy_dofs_duplicate(proc_t
*parent
, proc_t
*child
)
16259 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_NOTOWNED
);
16260 LCK_MTX_ASSERT(&parent
->p_dtrace_sprlock
, LCK_MTX_ASSERT_NOTOWNED
);
16261 LCK_MTX_ASSERT(&child
->p_dtrace_sprlock
, LCK_MTX_ASSERT_NOTOWNED
);
16263 lck_rw_lock_shared(&dtrace_dof_mode_lock
);
16264 dtrace_sprlock(parent
);
16267 * We need to make sure that the transition to lazy dofs -> helpers
16268 * was atomic for our parent
16270 ASSERT(parent
->p_dtrace_lazy_dofs
== NULL
|| parent
->p_dtrace_helpers
== NULL
);
16272 * In theory we should hold the child sprlock, but this is safe...
16274 ASSERT(child
->p_dtrace_lazy_dofs
== NULL
&& child
->p_dtrace_helpers
== NULL
);
16276 dof_ioctl_data_t
* parent_dofs
= parent
->p_dtrace_lazy_dofs
;
16277 dof_ioctl_data_t
* child_dofs
= NULL
;
16279 size_t parent_dofs_size
= DOF_IOCTL_DATA_T_SIZE(parent_dofs
->dofiod_count
);
16280 child_dofs
= kmem_alloc(parent_dofs_size
, KM_SLEEP
);
16281 bcopy(parent_dofs
, child_dofs
, parent_dofs_size
);
16284 dtrace_sprunlock(parent
);
16287 dtrace_sprlock(child
);
16288 child
->p_dtrace_lazy_dofs
= child_dofs
;
16289 dtrace_sprunlock(child
);
16291 * We process the DOF at this point if the mode is set to
16292 * LAZY_OFF. This can happen if DTrace is still processing the
16293 * DOF of other process (which can happen because the
16294 * protected pager can have a huge latency)
16295 * but has not processed our parent yet
16297 if (dtrace_dof_mode
== DTRACE_DOF_MODE_LAZY_OFF
) {
16298 dtrace_lazy_dofs_process(child
);
16300 lck_rw_unlock_shared(&dtrace_dof_mode_lock
);
16302 return DTRACE_LAZY_DOFS_DUPLICATED
;
16304 lck_rw_unlock_shared(&dtrace_dof_mode_lock
);
16309 static dtrace_helpers_t
*
16310 dtrace_helpers_create(proc_t
*p
)
16312 dtrace_helpers_t
*help
;
16314 LCK_MTX_ASSERT(&dtrace_lock
, LCK_MTX_ASSERT_OWNED
);
16315 ASSERT(p
->p_dtrace_helpers
== NULL
);
16317 help
= kmem_zalloc(sizeof (dtrace_helpers_t
), KM_SLEEP
);
16318 help
->dthps_actions
= kmem_zalloc(sizeof (dtrace_helper_action_t
*) *
16319 DTRACE_NHELPER_ACTIONS
, KM_SLEEP
);
16321 p
->p_dtrace_helpers
= help
;
16328 dtrace_helpers_destroy(proc_t
* p
)
16330 dtrace_helpers_t
*help
;
16331 dtrace_vstate_t
*vstate
;
16334 lck_mtx_lock(&dtrace_meta_lock
);
16335 lck_mtx_lock(&dtrace_lock
);
16337 ASSERT(p
->p_dtrace_helpers
!= NULL
);
16338 ASSERT(dtrace_helpers
> 0);
16340 help
= p
->p_dtrace_helpers
;
16341 vstate
= &help
->dthps_vstate
;
16344 * We're now going to lose the help from this process.
16346 p
->p_dtrace_helpers
= NULL
;
16350 * Destory the helper actions.
16352 for (i
= 0; i
< DTRACE_NHELPER_ACTIONS
; i
++) {
16353 dtrace_helper_action_t
*h
, *next
;
16355 for (h
= help
->dthps_actions
[i
]; h
!= NULL
; h
= next
) {
16356 next
= h
->dtha_next
;
16357 dtrace_helper_action_destroy(h
, vstate
);
16362 lck_mtx_unlock(&dtrace_lock
);
16365 * Destroy the helper providers.
16367 if (help
->dthps_maxprovs
> 0) {
16368 if (dtrace_meta_pid
!= NULL
) {
16369 ASSERT(dtrace_deferred_pid
== NULL
);
16371 for (i
= 0; i
< help
->dthps_nprovs
; i
++) {
16372 dtrace_helper_provider_remove(
16373 &help
->dthps_provs
[i
]->dthp_prov
, p
);
16376 lck_mtx_lock(&dtrace_lock
);
16377 ASSERT(help
->dthps_deferred
== 0 ||
16378 help
->dthps_next
!= NULL
||
16379 help
->dthps_prev
!= NULL
||
16380 help
== dtrace_deferred_pid
);
16383 * Remove the helper from the deferred list.
16385 if (help
->dthps_next
!= NULL
)
16386 help
->dthps_next
->dthps_prev
= help
->dthps_prev
;
16387 if (help
->dthps_prev
!= NULL
)
16388 help
->dthps_prev
->dthps_next
= help
->dthps_next
;
16389 if (dtrace_deferred_pid
== help
) {
16390 dtrace_deferred_pid
= help
->dthps_next
;
16391 ASSERT(help
->dthps_prev
== NULL
);
16394 lck_mtx_unlock(&dtrace_lock
);
16398 for (i
= 0; i
< help
->dthps_nprovs
; i
++) {
16399 dtrace_helper_provider_destroy(help
->dthps_provs
[i
]);
16402 kmem_free(help
->dthps_provs
, help
->dthps_maxprovs
*
16403 sizeof (dtrace_helper_provider_t
*));
16406 lck_mtx_lock(&dtrace_lock
);
16408 dtrace_vstate_fini(&help
->dthps_vstate
);
16409 kmem_free(help
->dthps_actions
,
16410 sizeof (dtrace_helper_action_t
*) * DTRACE_NHELPER_ACTIONS
);
16411 kmem_free(help
, sizeof (dtrace_helpers_t
));
16414 lck_mtx_unlock(&dtrace_lock
);
16415 lck_mtx_unlock(&dtrace_meta_lock
);
16419 dtrace_helpers_duplicate(proc_t
*from
, proc_t
*to
)
16421 dtrace_helpers_t
*help
, *newhelp
;
16422 dtrace_helper_action_t
*helper
, *new, *last
;
16424 dtrace_vstate_t
*vstate
;
16426 int j
, sz
, hasprovs
= 0;
16428 lck_mtx_lock(&dtrace_meta_lock
);
16429 lck_mtx_lock(&dtrace_lock
);
16430 ASSERT(from
->p_dtrace_helpers
!= NULL
);
16431 ASSERT(dtrace_helpers
> 0);
16433 help
= from
->p_dtrace_helpers
;
16434 newhelp
= dtrace_helpers_create(to
);
16435 ASSERT(to
->p_dtrace_helpers
!= NULL
);
16437 newhelp
->dthps_generation
= help
->dthps_generation
;
16438 vstate
= &newhelp
->dthps_vstate
;
16441 * Duplicate the helper actions.
16443 for (i
= 0; i
< DTRACE_NHELPER_ACTIONS
; i
++) {
16444 if ((helper
= help
->dthps_actions
[i
]) == NULL
)
16447 for (last
= NULL
; helper
!= NULL
; helper
= helper
->dtha_next
) {
16448 new = kmem_zalloc(sizeof (dtrace_helper_action_t
),
16450 new->dtha_generation
= helper
->dtha_generation
;
16452 if ((dp
= helper
->dtha_predicate
) != NULL
) {
16453 dp
= dtrace_difo_duplicate(dp
, vstate
);
16454 new->dtha_predicate
= dp
;
16457 new->dtha_nactions
= helper
->dtha_nactions
;
16458 sz
= sizeof (dtrace_difo_t
*) * new->dtha_nactions
;
16459 new->dtha_actions
= kmem_alloc(sz
, KM_SLEEP
);
16461 for (j
= 0; j
< new->dtha_nactions
; j
++) {
16462 dtrace_difo_t
*dpj
= helper
->dtha_actions
[j
];
16464 ASSERT(dpj
!= NULL
);
16465 dpj
= dtrace_difo_duplicate(dpj
, vstate
);
16466 new->dtha_actions
[j
] = dpj
;
16469 if (last
!= NULL
) {
16470 last
->dtha_next
= new;
16472 newhelp
->dthps_actions
[i
] = new;
16480 * Duplicate the helper providers and register them with the
16481 * DTrace framework.
16483 if (help
->dthps_nprovs
> 0) {
16484 newhelp
->dthps_nprovs
= help
->dthps_nprovs
;
16485 newhelp
->dthps_maxprovs
= help
->dthps_nprovs
;
16486 newhelp
->dthps_provs
= kmem_alloc(newhelp
->dthps_nprovs
*
16487 sizeof (dtrace_helper_provider_t
*), KM_SLEEP
);
16488 for (i
= 0; i
< newhelp
->dthps_nprovs
; i
++) {
16489 newhelp
->dthps_provs
[i
] = help
->dthps_provs
[i
];
16490 newhelp
->dthps_provs
[i
]->dthp_ref
++;
16496 lck_mtx_unlock(&dtrace_lock
);
16499 dtrace_helper_provider_register(to
, newhelp
, NULL
);
16501 lck_mtx_unlock(&dtrace_meta_lock
);
16505 * DTrace Process functions
16509 dtrace_proc_fork(proc_t
*parent_proc
, proc_t
*child_proc
, int spawn
)
16512 * This code applies to new processes who are copying the task
16513 * and thread state and address spaces of their parent process.
16517 * APPLE NOTE: Solaris does a sprlock() and drops the
16518 * proc_lock here. We're cheating a bit and only taking
16519 * the p_dtrace_sprlock lock. A full sprlock would
16520 * task_suspend the parent.
16522 dtrace_sprlock(parent_proc
);
16525 * Remove all DTrace tracepoints from the child process. We
16526 * need to do this _before_ duplicating USDT providers since
16527 * any associated probes may be immediately enabled.
16529 if (parent_proc
->p_dtrace_count
> 0) {
16530 dtrace_fasttrap_fork(parent_proc
, child_proc
);
16533 dtrace_sprunlock(parent_proc
);
16536 * Duplicate any lazy dof(s). This must be done while NOT
16537 * holding the parent sprlock! Lock ordering is
16538 * dtrace_dof_mode_lock, then sprlock. It is imperative we
16539 * always call dtrace_lazy_dofs_duplicate, rather than null
16540 * check and call if !NULL. If we NULL test, during lazy dof
16541 * faulting we can race with the faulting code and proceed
16542 * from here to beyond the helpers copy. The lazy dof
16543 * faulting will then fail to copy the helpers to the child
16544 * process. We return if we duplicated lazy dofs as a process
16545 * can only have one at the same time to avoid a race between
16546 * a dtrace client and dtrace_proc_fork where a process would
16547 * end up with both lazy dofs and helpers.
16549 if (dtrace_lazy_dofs_duplicate(parent_proc
, child_proc
) == DTRACE_LAZY_DOFS_DUPLICATED
) {
16554 * Duplicate any helper actions and providers if they haven't
16557 #if !defined(__APPLE__)
16560 * we set above informs the code to enable USDT probes that
16561 * sprlock() may fail because the child is being forked.
16565 * APPLE NOTE: As best I can tell, Apple's sprlock() equivalent
16566 * never fails to find the child. We do not set SFORKING.
16568 if (parent_proc
->p_dtrace_helpers
!= NULL
&& dtrace_helpers_fork
) {
16569 (*dtrace_helpers_fork
)(parent_proc
, child_proc
);
16575 dtrace_proc_exec(proc_t
*p
)
16578 * Invalidate any predicate evaluation already cached for this thread by DTrace.
16579 * That's because we've just stored to p_comm and DTrace refers to that when it
16580 * evaluates the "execname" special variable. uid and gid may have changed as well.
16582 dtrace_set_thread_predcache(current_thread(), 0);
16585 * Free any outstanding lazy dof entries. It is imperative we
16586 * always call dtrace_lazy_dofs_destroy, rather than null check
16587 * and call if !NULL. If we NULL test, during lazy dof faulting
16588 * we can race with the faulting code and proceed from here to
16589 * beyond the helpers cleanup. The lazy dof faulting will then
16590 * install new helpers which no longer belong to this process!
16592 dtrace_lazy_dofs_destroy(p
);
16596 * Clean up any DTrace helpers for the process.
16598 if (p
->p_dtrace_helpers
!= NULL
&& dtrace_helpers_cleanup
) {
16599 (*dtrace_helpers_cleanup
)(p
);
16603 * Cleanup the DTrace provider associated with this process.
16606 if (p
->p_dtrace_probes
&& dtrace_fasttrap_exec_ptr
) {
16607 (*dtrace_fasttrap_exec_ptr
)(p
);
16613 dtrace_proc_exit(proc_t
*p
)
16616 * Free any outstanding lazy dof entries. It is imperative we
16617 * always call dtrace_lazy_dofs_destroy, rather than null check
16618 * and call if !NULL. If we NULL test, during lazy dof faulting
16619 * we can race with the faulting code and proceed from here to
16620 * beyond the helpers cleanup. The lazy dof faulting will then
16621 * install new helpers which will never be cleaned up, and leak.
16623 dtrace_lazy_dofs_destroy(p
);
16626 * Clean up any DTrace helper actions or probes for the process.
16628 if (p
->p_dtrace_helpers
!= NULL
) {
16629 (*dtrace_helpers_cleanup
)(p
);
16633 * Clean up any DTrace probes associated with this process.
16636 * APPLE NOTE: We release ptss pages/entries in dtrace_fasttrap_exit_ptr(),
16637 * call this after dtrace_helpers_cleanup()
16640 if (p
->p_dtrace_probes
&& dtrace_fasttrap_exit_ptr
) {
16641 (*dtrace_fasttrap_exit_ptr
)(p
);
16647 * DTrace Hook Functions
16651 * APPLE NOTE: dtrace_modctl_* routines for kext support.
16652 * Used to manipulate the modctl list within dtrace xnu.
16655 modctl_t
*dtrace_modctl_list
;
16658 dtrace_modctl_add(struct modctl
* newctl
)
16660 struct modctl
*nextp
, *prevp
;
16662 ASSERT(newctl
!= NULL
);
16663 LCK_MTX_ASSERT(&mod_lock
, LCK_MTX_ASSERT_OWNED
);
16665 // Insert new module at the front of the list,
16667 newctl
->mod_next
= dtrace_modctl_list
;
16668 dtrace_modctl_list
= newctl
;
16671 * If a module exists with the same name, then that module
16672 * must have been unloaded with enabled probes. We will move
16673 * the unloaded module to the new module's stale chain and
16674 * then stop traversing the list.
16678 nextp
= newctl
->mod_next
;
16680 while (nextp
!= NULL
) {
16681 if (nextp
->mod_loaded
) {
16682 /* This is a loaded module. Keep traversing. */
16684 nextp
= nextp
->mod_next
;
16688 /* Found an unloaded module */
16689 if (strncmp (newctl
->mod_modname
, nextp
->mod_modname
, KMOD_MAX_NAME
)) {
16690 /* Names don't match. Keep traversing. */
16692 nextp
= nextp
->mod_next
;
16696 /* We found a stale entry, move it. We're done. */
16697 prevp
->mod_next
= nextp
->mod_next
;
16698 newctl
->mod_stale
= nextp
;
16699 nextp
->mod_next
= NULL
;
16707 dtrace_modctl_lookup(struct kmod_info
* kmod
)
16709 LCK_MTX_ASSERT(&mod_lock
, LCK_MTX_ASSERT_OWNED
);
16711 struct modctl
* ctl
;
16713 for (ctl
= dtrace_modctl_list
; ctl
; ctl
=ctl
->mod_next
) {
16714 if (ctl
->mod_id
== kmod
->id
)
16721 * This routine is called from dtrace_module_unloaded().
16722 * It removes a modctl structure and its stale chain
16723 * from the kext shadow list.
16726 dtrace_modctl_remove(struct modctl
* ctl
)
16728 ASSERT(ctl
!= NULL
);
16729 LCK_MTX_ASSERT(&mod_lock
, LCK_MTX_ASSERT_OWNED
);
16730 modctl_t
*prevp
, *nextp
, *curp
;
16732 // Remove stale chain first
16733 for (curp
=ctl
->mod_stale
; curp
!= NULL
; curp
=nextp
) {
16734 nextp
= curp
->mod_stale
;
16735 /* There should NEVER be user symbols allocated at this point */
16736 ASSERT(curp
->mod_user_symbols
== NULL
);
16737 kmem_free(curp
, sizeof(modctl_t
));
16741 curp
= dtrace_modctl_list
;
16743 while (curp
!= ctl
) {
16745 curp
= curp
->mod_next
;
16748 if (prevp
!= NULL
) {
16749 prevp
->mod_next
= ctl
->mod_next
;
16752 dtrace_modctl_list
= ctl
->mod_next
;
16755 /* There should NEVER be user symbols allocated at this point */
16756 ASSERT(ctl
->mod_user_symbols
== NULL
);
16758 kmem_free (ctl
, sizeof(modctl_t
));
16762 * APPLE NOTE: The kext loader will call dtrace_module_loaded
16763 * when the kext is loaded in memory, but before calling the
16764 * kext's start routine.
16766 * Return 0 on success
16767 * Return -1 on failure
16771 dtrace_module_loaded(struct kmod_info
*kmod
, uint32_t flag
)
16773 dtrace_provider_t
*prv
;
16776 * If kernel symbols have been disabled, return immediately
16777 * DTRACE_KERNEL_SYMBOLS_NEVER is a permanent mode, it is safe to test without holding locks
16779 if (dtrace_kernel_symbol_mode
== DTRACE_KERNEL_SYMBOLS_NEVER
)
16782 struct modctl
*ctl
= NULL
;
16783 if (!kmod
|| kmod
->address
== 0 || kmod
->size
== 0)
16786 lck_mtx_lock(&dtrace_provider_lock
);
16787 lck_mtx_lock(&mod_lock
);
16790 * Have we seen this kext before?
16793 ctl
= dtrace_modctl_lookup(kmod
);
16796 /* bail... we already have this kext in the modctl list */
16797 lck_mtx_unlock(&mod_lock
);
16798 lck_mtx_unlock(&dtrace_provider_lock
);
16799 if (dtrace_err_verbose
)
16800 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
);
16804 ctl
= kmem_alloc(sizeof(struct modctl
), KM_SLEEP
);
16806 if (dtrace_err_verbose
)
16807 cmn_err(CE_WARN
, "dtrace module load '%s %u' is failing ", kmod
->name
, (uint_t
)kmod
->id
);
16808 lck_mtx_unlock(&mod_lock
);
16809 lck_mtx_unlock(&dtrace_provider_lock
);
16812 ctl
->mod_next
= NULL
;
16813 ctl
->mod_stale
= NULL
;
16814 strlcpy (ctl
->mod_modname
, kmod
->name
, sizeof(ctl
->mod_modname
));
16815 ctl
->mod_loadcnt
= kmod
->id
;
16816 ctl
->mod_nenabled
= 0;
16817 ctl
->mod_address
= kmod
->address
;
16818 ctl
->mod_size
= kmod
->size
;
16819 ctl
->mod_id
= kmod
->id
;
16820 ctl
->mod_loaded
= 1;
16821 ctl
->mod_flags
= 0;
16822 ctl
->mod_user_symbols
= NULL
;
16825 * Find the UUID for this module, if it has one
16827 kernel_mach_header_t
* header
= (kernel_mach_header_t
*)ctl
->mod_address
;
16828 struct load_command
* load_cmd
= (struct load_command
*)&header
[1];
16830 for (i
= 0; i
< header
->ncmds
; i
++) {
16831 if (load_cmd
->cmd
== LC_UUID
) {
16832 struct uuid_command
* uuid_cmd
= (struct uuid_command
*)load_cmd
;
16833 memcpy(ctl
->mod_uuid
, uuid_cmd
->uuid
, sizeof(uuid_cmd
->uuid
));
16834 ctl
->mod_flags
|= MODCTL_HAS_UUID
;
16837 load_cmd
= (struct load_command
*)((caddr_t
)load_cmd
+ load_cmd
->cmdsize
);
16840 if (ctl
->mod_address
== g_kernel_kmod_info
.address
) {
16841 ctl
->mod_flags
|= MODCTL_IS_MACH_KERNEL
;
16842 memcpy(dtrace_kerneluuid
, ctl
->mod_uuid
, sizeof(dtrace_kerneluuid
));
16845 * Static kexts have a UUID that is not used for symbolication, as all their
16846 * symbols are in kernel
16848 else if ((flag
& KMOD_DTRACE_STATIC_KEXT
) == KMOD_DTRACE_STATIC_KEXT
) {
16849 memcpy(ctl
->mod_uuid
, dtrace_kerneluuid
, sizeof(dtrace_kerneluuid
));
16850 ctl
->mod_flags
|= MODCTL_IS_STATIC_KEXT
;
16853 dtrace_modctl_add(ctl
);
16856 * We must hold the dtrace_lock to safely test non permanent dtrace_fbt_symbol_mode(s)
16858 lck_mtx_lock(&dtrace_lock
);
16861 * DTrace must decide if it will instrument modules lazily via
16862 * userspace symbols (default mode), or instrument immediately via
16863 * kernel symbols (non-default mode)
16865 * When in default/lazy mode, DTrace will only support modules
16866 * built with a valid UUID.
16868 * Overriding the default can be done explicitly in one of
16869 * the following two ways.
16871 * A module can force symbols from kernel space using the plist key,
16872 * OSBundleForceDTraceInit (see kmod.h). If this per kext state is set,
16873 * we fall through and instrument this module now.
16875 * Or, the boot-arg, dtrace_kernel_symbol_mode, can be set to force symbols
16876 * from kernel space (see dtrace_impl.h). If this system state is set
16877 * to a non-userspace mode, we fall through and instrument the module now.
16880 if ((dtrace_kernel_symbol_mode
== DTRACE_KERNEL_SYMBOLS_FROM_USERSPACE
) &&
16881 (!(flag
& KMOD_DTRACE_FORCE_INIT
)))
16883 /* We will instrument the module lazily -- this is the default */
16884 lck_mtx_unlock(&dtrace_lock
);
16885 lck_mtx_unlock(&mod_lock
);
16886 lck_mtx_unlock(&dtrace_provider_lock
);
16890 /* We will instrument the module immediately using kernel symbols */
16891 if (!(flag
& KMOD_DTRACE_NO_KERNEL_SYMS
)) {
16892 ctl
->mod_flags
|= MODCTL_HAS_KERNEL_SYMBOLS
;
16895 lck_mtx_unlock(&dtrace_lock
);
16898 * We're going to call each providers per-module provide operation
16899 * specifying only this module.
16901 for (prv
= dtrace_provider
; prv
!= NULL
; prv
= prv
->dtpv_next
)
16902 prv
->dtpv_pops
.dtps_provide_module(prv
->dtpv_arg
, ctl
);
16905 * APPLE NOTE: The contract with the kext loader is that once this function
16906 * has completed, it may delete kernel symbols at will.
16907 * We must set this while still holding the mod_lock.
16909 ctl
->mod_flags
&= ~MODCTL_HAS_KERNEL_SYMBOLS
;
16911 lck_mtx_unlock(&mod_lock
);
16912 lck_mtx_unlock(&dtrace_provider_lock
);
16915 * If we have any retained enablings, we need to match against them.
16916 * Enabling probes requires that cpu_lock be held, and we cannot hold
16917 * cpu_lock here -- it is legal for cpu_lock to be held when loading a
16918 * module. (In particular, this happens when loading scheduling
16919 * classes.) So if we have any retained enablings, we need to dispatch
16920 * our task queue to do the match for us.
16922 lck_mtx_lock(&dtrace_lock
);
16924 if (dtrace_retained
== NULL
) {
16925 lck_mtx_unlock(&dtrace_lock
);
16931 * The cpu_lock mentioned above is only held by dtrace code, Apple's xnu never actually
16932 * holds it for any reason. Thus the comment above is invalid, we can directly invoke
16933 * dtrace_enabling_matchall without jumping through all the hoops, and we can avoid
16934 * the delay call as well.
16936 lck_mtx_unlock(&dtrace_lock
);
16938 dtrace_enabling_matchall();
16944 * Return 0 on success
16945 * Return -1 on failure
16948 dtrace_module_unloaded(struct kmod_info
*kmod
)
16950 dtrace_probe_t
template, *probe
, *first
, *next
;
16951 dtrace_provider_t
*prov
;
16952 struct modctl
*ctl
= NULL
;
16953 struct modctl
*syncctl
= NULL
;
16954 struct modctl
*nextsyncctl
= NULL
;
16957 lck_mtx_lock(&dtrace_provider_lock
);
16958 lck_mtx_lock(&mod_lock
);
16959 lck_mtx_lock(&dtrace_lock
);
16961 if (kmod
== NULL
) {
16965 ctl
= dtrace_modctl_lookup(kmod
);
16968 lck_mtx_unlock(&dtrace_lock
);
16969 lck_mtx_unlock(&mod_lock
);
16970 lck_mtx_unlock(&dtrace_provider_lock
);
16973 ctl
->mod_loaded
= 0;
16974 ctl
->mod_address
= 0;
16978 if (dtrace_bymod
== NULL
) {
16980 * The DTrace module is loaded (obviously) but not attached;
16981 * we don't have any work to do.
16984 (void)dtrace_modctl_remove(ctl
);
16985 lck_mtx_unlock(&dtrace_lock
);
16986 lck_mtx_unlock(&mod_lock
);
16987 lck_mtx_unlock(&dtrace_provider_lock
);
16991 /* Syncmode set means we target and traverse entire modctl list. */
16993 nextsyncctl
= dtrace_modctl_list
;
16998 /* find a stale modctl struct */
16999 for (syncctl
= nextsyncctl
; syncctl
!= NULL
; syncctl
=syncctl
->mod_next
) {
17000 if (syncctl
->mod_address
== 0)
17005 /* We have no more work to do */
17006 lck_mtx_unlock(&dtrace_lock
);
17007 lck_mtx_unlock(&mod_lock
);
17008 lck_mtx_unlock(&dtrace_provider_lock
);
17012 /* keep track of next syncctl in case this one is removed */
17013 nextsyncctl
= syncctl
->mod_next
;
17018 template.dtpr_mod
= ctl
->mod_modname
;
17020 for (probe
= first
= dtrace_hash_lookup(dtrace_bymod
, &template);
17021 probe
!= NULL
; probe
= probe
->dtpr_nextmod
) {
17022 if (probe
->dtpr_ecb
!= NULL
) {
17024 * This shouldn't _actually_ be possible -- we're
17025 * unloading a module that has an enabled probe in it.
17026 * (It's normally up to the provider to make sure that
17027 * this can't happen.) However, because dtps_enable()
17028 * doesn't have a failure mode, there can be an
17029 * enable/unload race. Upshot: we don't want to
17030 * assert, but we're not going to disable the
17036 /* We're syncing, let's look at next in list */
17040 lck_mtx_unlock(&dtrace_lock
);
17041 lck_mtx_unlock(&mod_lock
);
17042 lck_mtx_unlock(&dtrace_provider_lock
);
17044 if (dtrace_err_verbose
) {
17045 cmn_err(CE_WARN
, "unloaded module '%s' had "
17046 "enabled probes", ctl
->mod_modname
);
17054 for (first
= NULL
; probe
!= NULL
; probe
= next
) {
17055 ASSERT(dtrace_probes
[probe
->dtpr_id
- 1] == probe
);
17057 dtrace_probes
[probe
->dtpr_id
- 1] = NULL
;
17058 probe
->dtpr_provider
->dtpv_probe_count
--;
17060 next
= probe
->dtpr_nextmod
;
17061 dtrace_hash_remove(dtrace_byprov
, probe
);
17062 dtrace_hash_remove(dtrace_bymod
, probe
);
17063 dtrace_hash_remove(dtrace_byfunc
, probe
);
17064 dtrace_hash_remove(dtrace_byname
, probe
);
17066 if (first
== NULL
) {
17068 probe
->dtpr_nextmod
= NULL
;
17070 probe
->dtpr_nextmod
= first
;
17076 * We've removed all of the module's probes from the hash chains and
17077 * from the probe array. Now issue a dtrace_sync() to be sure that
17078 * everyone has cleared out from any probe array processing.
17082 for (probe
= first
; probe
!= NULL
; probe
= first
) {
17083 first
= probe
->dtpr_nextmod
;
17084 prov
= probe
->dtpr_provider
;
17085 prov
->dtpv_pops
.dtps_destroy(prov
->dtpv_arg
, probe
->dtpr_id
,
17087 dtrace_strunref(probe
->dtpr_mod
);
17088 dtrace_strunref(probe
->dtpr_func
);
17089 dtrace_strunref(probe
->dtpr_name
);
17090 vmem_free(dtrace_arena
, (void *)(uintptr_t)probe
->dtpr_id
, 1);
17092 zfree(dtrace_probe_t_zone
, probe
);
17095 dtrace_modctl_remove(ctl
);
17100 lck_mtx_unlock(&dtrace_lock
);
17101 lck_mtx_unlock(&mod_lock
);
17102 lck_mtx_unlock(&dtrace_provider_lock
);
17108 dtrace_suspend(void)
17110 dtrace_probe_foreach(offsetof(dtrace_pops_t
, dtps_suspend
));
17114 dtrace_resume(void)
17116 dtrace_probe_foreach(offsetof(dtrace_pops_t
, dtps_resume
));
17120 dtrace_cpu_setup(cpu_setup_t what
, processorid_t cpu
)
17122 LCK_MTX_ASSERT(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
17123 lck_mtx_lock(&dtrace_lock
);
17127 dtrace_state_t
*state
;
17128 dtrace_optval_t
*opt
, rs
, c
;
17131 * For now, we only allocate a new buffer for anonymous state.
17133 if ((state
= dtrace_anon
.dta_state
) == NULL
)
17136 if (state
->dts_activity
!= DTRACE_ACTIVITY_ACTIVE
)
17139 opt
= state
->dts_options
;
17140 c
= opt
[DTRACEOPT_CPU
];
17142 if (c
!= DTRACE_CPUALL
&& c
!= DTRACEOPT_UNSET
&& c
!= cpu
)
17146 * Regardless of what the actual policy is, we're going to
17147 * temporarily set our resize policy to be manual. We're
17148 * also going to temporarily set our CPU option to denote
17149 * the newly configured CPU.
17151 rs
= opt
[DTRACEOPT_BUFRESIZE
];
17152 opt
[DTRACEOPT_BUFRESIZE
] = DTRACEOPT_BUFRESIZE_MANUAL
;
17153 opt
[DTRACEOPT_CPU
] = (dtrace_optval_t
)cpu
;
17155 (void) dtrace_state_buffers(state
);
17157 opt
[DTRACEOPT_BUFRESIZE
] = rs
;
17158 opt
[DTRACEOPT_CPU
] = c
;
17165 * We don't free the buffer in the CPU_UNCONFIG case. (The
17166 * buffer will be freed when the consumer exits.)
17174 lck_mtx_unlock(&dtrace_lock
);
17179 dtrace_cpu_setup_initial(processorid_t cpu
)
17181 (void) dtrace_cpu_setup(CPU_CONFIG
, cpu
);
17185 dtrace_toxrange_add(uintptr_t base
, uintptr_t limit
)
17187 if (dtrace_toxranges
>= dtrace_toxranges_max
) {
17189 dtrace_toxrange_t
*range
;
17191 osize
= dtrace_toxranges_max
* sizeof (dtrace_toxrange_t
);
17194 ASSERT(dtrace_toxrange
== NULL
);
17195 ASSERT(dtrace_toxranges_max
== 0);
17196 dtrace_toxranges_max
= 1;
17198 dtrace_toxranges_max
<<= 1;
17201 nsize
= dtrace_toxranges_max
* sizeof (dtrace_toxrange_t
);
17202 range
= kmem_zalloc(nsize
, KM_SLEEP
);
17204 if (dtrace_toxrange
!= NULL
) {
17205 ASSERT(osize
!= 0);
17206 bcopy(dtrace_toxrange
, range
, osize
);
17207 kmem_free(dtrace_toxrange
, osize
);
17210 dtrace_toxrange
= range
;
17213 ASSERT(dtrace_toxrange
[dtrace_toxranges
].dtt_base
== 0);
17214 ASSERT(dtrace_toxrange
[dtrace_toxranges
].dtt_limit
== 0);
17216 dtrace_toxrange
[dtrace_toxranges
].dtt_base
= base
;
17217 dtrace_toxrange
[dtrace_toxranges
].dtt_limit
= limit
;
17218 dtrace_toxranges
++;
17222 * DTrace Driver Cookbook Functions
17226 dtrace_attach(dev_info_t
*devi
)
17228 dtrace_provider_id_t id
;
17229 dtrace_state_t
*state
= NULL
;
17230 dtrace_enabling_t
*enab
;
17232 lck_mtx_lock(&cpu_lock
);
17233 lck_mtx_lock(&dtrace_provider_lock
);
17234 lck_mtx_lock(&dtrace_lock
);
17236 /* Darwin uses BSD cloning device driver to automagically obtain minor device number. */
17237 dtrace_devi
= devi
;
17239 dtrace_modload
= dtrace_module_loaded
;
17240 dtrace_modunload
= dtrace_module_unloaded
;
17241 dtrace_cpu_init
= dtrace_cpu_setup_initial
;
17242 dtrace_helpers_cleanup
= dtrace_helpers_destroy
;
17243 dtrace_helpers_fork
= dtrace_helpers_duplicate
;
17244 dtrace_cpustart_init
= dtrace_suspend
;
17245 dtrace_cpustart_fini
= dtrace_resume
;
17246 dtrace_debugger_init
= dtrace_suspend
;
17247 dtrace_debugger_fini
= dtrace_resume
;
17249 register_cpu_setup_func((cpu_setup_func_t
*)dtrace_cpu_setup
, NULL
);
17251 LCK_MTX_ASSERT(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
17253 dtrace_arena
= vmem_create("dtrace", (void *)1, INT32_MAX
, 1,
17254 NULL
, NULL
, NULL
, 0, VM_SLEEP
| VMC_IDENTIFIER
);
17256 dtrace_state_cache
= kmem_cache_create("dtrace_state_cache",
17257 sizeof (dtrace_dstate_percpu_t
) * (int)NCPU
, DTRACE_STATE_ALIGN
,
17258 NULL
, NULL
, NULL
, NULL
, NULL
, 0);
17260 LCK_MTX_ASSERT(&cpu_lock
, LCK_MTX_ASSERT_OWNED
);
17262 dtrace_nprobes
= dtrace_nprobes_default
;
17263 dtrace_probes
= kmem_zalloc(sizeof(dtrace_probe_t
*) * dtrace_nprobes
,
17266 dtrace_byprov
= dtrace_hash_create(dtrace_strkey_probe_provider
,
17268 offsetof(dtrace_probe_t
, dtpr_nextprov
),
17269 offsetof(dtrace_probe_t
, dtpr_prevprov
));
17271 dtrace_bymod
= dtrace_hash_create(dtrace_strkey_deref_offset
,
17272 offsetof(dtrace_probe_t
, dtpr_mod
),
17273 offsetof(dtrace_probe_t
, dtpr_nextmod
),
17274 offsetof(dtrace_probe_t
, dtpr_prevmod
));
17276 dtrace_byfunc
= dtrace_hash_create(dtrace_strkey_deref_offset
,
17277 offsetof(dtrace_probe_t
, dtpr_func
),
17278 offsetof(dtrace_probe_t
, dtpr_nextfunc
),
17279 offsetof(dtrace_probe_t
, dtpr_prevfunc
));
17281 dtrace_byname
= dtrace_hash_create(dtrace_strkey_deref_offset
,
17282 offsetof(dtrace_probe_t
, dtpr_name
),
17283 offsetof(dtrace_probe_t
, dtpr_nextname
),
17284 offsetof(dtrace_probe_t
, dtpr_prevname
));
17286 if (dtrace_retain_max
< 1) {
17287 cmn_err(CE_WARN
, "illegal value (%lu) for dtrace_retain_max; "
17288 "setting to 1", dtrace_retain_max
);
17289 dtrace_retain_max
= 1;
17293 * Now discover our toxic ranges.
17295 dtrace_toxic_ranges(dtrace_toxrange_add
);
17298 * Before we register ourselves as a provider to our own framework,
17299 * we would like to assert that dtrace_provider is NULL -- but that's
17300 * not true if we were loaded as a dependency of a DTrace provider.
17301 * Once we've registered, we can assert that dtrace_provider is our
17304 (void) dtrace_register("dtrace", &dtrace_provider_attr
,
17305 DTRACE_PRIV_NONE
, 0, &dtrace_provider_ops
, NULL
, &id
);
17307 ASSERT(dtrace_provider
!= NULL
);
17308 ASSERT((dtrace_provider_id_t
)dtrace_provider
== id
);
17310 #if defined (__x86_64__)
17311 dtrace_probeid_begin
= dtrace_probe_create((dtrace_provider_id_t
)
17312 dtrace_provider
, NULL
, NULL
, "BEGIN", 1, NULL
);
17313 dtrace_probeid_end
= dtrace_probe_create((dtrace_provider_id_t
)
17314 dtrace_provider
, NULL
, NULL
, "END", 0, NULL
);
17315 dtrace_probeid_error
= dtrace_probe_create((dtrace_provider_id_t
)
17316 dtrace_provider
, NULL
, NULL
, "ERROR", 3, NULL
);
17317 #elif (defined(__arm__) || defined(__arm64__))
17318 dtrace_probeid_begin
= dtrace_probe_create((dtrace_provider_id_t
)
17319 dtrace_provider
, NULL
, NULL
, "BEGIN", 2, NULL
);
17320 dtrace_probeid_end
= dtrace_probe_create((dtrace_provider_id_t
)
17321 dtrace_provider
, NULL
, NULL
, "END", 1, NULL
);
17322 dtrace_probeid_error
= dtrace_probe_create((dtrace_provider_id_t
)
17323 dtrace_provider
, NULL
, NULL
, "ERROR", 4, NULL
);
17325 #error Unknown Architecture
17328 dtrace_anon_property();
17329 lck_mtx_unlock(&cpu_lock
);
17332 * If DTrace helper tracing is enabled, we need to allocate the
17333 * trace buffer and initialize the values.
17335 if (dtrace_helptrace_enabled
) {
17336 ASSERT(dtrace_helptrace_buffer
== NULL
);
17337 dtrace_helptrace_buffer
=
17338 kmem_zalloc(dtrace_helptrace_bufsize
, KM_SLEEP
);
17339 dtrace_helptrace_next
= 0;
17343 * If there are already providers, we must ask them to provide their
17344 * probes, and then match any anonymous enabling against them. Note
17345 * that there should be no other retained enablings at this time:
17346 * the only retained enablings at this time should be the anonymous
17349 if (dtrace_anon
.dta_enabling
!= NULL
) {
17350 ASSERT(dtrace_retained
== dtrace_anon
.dta_enabling
);
17353 * APPLE NOTE: if handling anonymous dof, switch symbol modes.
17355 if (dtrace_kernel_symbol_mode
== DTRACE_KERNEL_SYMBOLS_FROM_USERSPACE
) {
17356 dtrace_kernel_symbol_mode
= DTRACE_KERNEL_SYMBOLS_FROM_KERNEL
;
17359 dtrace_enabling_provide(NULL
);
17360 state
= dtrace_anon
.dta_state
;
17363 * We couldn't hold cpu_lock across the above call to
17364 * dtrace_enabling_provide(), but we must hold it to actually
17365 * enable the probes. We have to drop all of our locks, pick
17366 * up cpu_lock, and regain our locks before matching the
17367 * retained anonymous enabling.
17369 lck_mtx_unlock(&dtrace_lock
);
17370 lck_mtx_unlock(&dtrace_provider_lock
);
17372 lck_mtx_lock(&cpu_lock
);
17373 lck_mtx_lock(&dtrace_provider_lock
);
17374 lck_mtx_lock(&dtrace_lock
);
17376 if ((enab
= dtrace_anon
.dta_enabling
) != NULL
)
17377 (void) dtrace_enabling_match(enab
, NULL
, NULL
);
17379 lck_mtx_unlock(&cpu_lock
);
17382 lck_mtx_unlock(&dtrace_lock
);
17383 lck_mtx_unlock(&dtrace_provider_lock
);
17385 if (state
!= NULL
) {
17387 * If we created any anonymous state, set it going now.
17389 (void) dtrace_state_go(state
, &dtrace_anon
.dta_beganon
);
17392 return (DDI_SUCCESS
);
17397 dtrace_open(dev_t
*devp
, int flag
, int otyp
, cred_t
*cred_p
)
17399 #pragma unused(flag, otyp)
17400 dtrace_state_t
*state
;
17406 /* APPLE: Darwin puts Helper on its own major device. */
17409 * If no DTRACE_PRIV_* bits are set in the credential, then the
17410 * caller lacks sufficient permission to do anything with DTrace.
17412 dtrace_cred2priv(cred_p
, &priv
, &uid
, &zoneid
);
17413 if (priv
== DTRACE_PRIV_NONE
)
17417 * APPLE NOTE: We delay the initialization of fasttrap as late as possible.
17418 * It certainly can't be later than now!
17423 * Ask all providers to provide all their probes.
17425 lck_mtx_lock(&dtrace_provider_lock
);
17426 dtrace_probe_provide(NULL
, NULL
);
17427 lck_mtx_unlock(&dtrace_provider_lock
);
17429 lck_mtx_lock(&cpu_lock
);
17430 lck_mtx_lock(&dtrace_lock
);
17432 dtrace_membar_producer();
17436 * If the kernel debugger is active (that is, if the kernel debugger
17437 * modified text in some way), we won't allow the open.
17439 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE
) != 0) {
17441 lck_mtx_unlock(&dtrace_lock
);
17442 lck_mtx_unlock(&cpu_lock
);
17447 rv
= dtrace_state_create(devp
, cred_p
, &state
);
17448 lck_mtx_unlock(&cpu_lock
);
17450 if (rv
!= 0 || state
== NULL
) {
17451 if (--dtrace_opens
== 0 && dtrace_anon
.dta_enabling
== NULL
) {
17453 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE
);
17456 lck_mtx_unlock(&dtrace_lock
);
17457 /* propagate EAGAIN or ERESTART */
17461 lck_mtx_unlock(&dtrace_lock
);
17463 lck_rw_lock_exclusive(&dtrace_dof_mode_lock
);
17466 * If we are currently lazy, transition states.
17468 * Unlike dtrace_close, we do not need to check the
17469 * value of dtrace_opens, as any positive value (and
17470 * we count as 1) means we transition states.
17472 if (dtrace_dof_mode
== DTRACE_DOF_MODE_LAZY_ON
) {
17473 dtrace_dof_mode
= DTRACE_DOF_MODE_LAZY_OFF
;
17475 * We do not need to hold the exclusive lock while processing
17476 * DOF on processes. We do need to make sure the mode does not get
17477 * changed to DTRACE_DOF_MODE_LAZY_ON during that stage though
17478 * (which should not happen anyway since it only happens in
17479 * dtrace_close). There is no way imcomplete USDT probes can be
17480 * activate by any DTrace clients here since they all have to
17481 * call dtrace_open and be blocked on dtrace_dof_mode_lock
17483 lck_rw_lock_exclusive_to_shared(&dtrace_dof_mode_lock
);
17485 * Iterate all existing processes and load lazy dofs.
17487 proc_iterate(PROC_ALLPROCLIST
| PROC_NOWAITTRANS
,
17488 dtrace_lazy_dofs_proc_iterate_doit
,
17490 dtrace_lazy_dofs_proc_iterate_filter
,
17493 lck_rw_unlock_shared(&dtrace_dof_mode_lock
);
17496 lck_rw_unlock_exclusive(&dtrace_dof_mode_lock
);
17501 * Update kernel symbol state.
17503 * We must own the provider and dtrace locks.
17505 * NOTE! It may appear there is a race by setting this value so late
17506 * after dtrace_probe_provide. However, any kext loaded after the
17507 * call to probe provide and before we set LAZY_OFF will be marked as
17508 * eligible for symbols from userspace. The same dtrace that is currently
17509 * calling dtrace_open() (this call!) will get a list of kexts needing
17510 * symbols and fill them in, thus closing the race window.
17512 * We want to set this value only after it certain it will succeed, as
17513 * this significantly reduces the complexity of error exits.
17515 lck_mtx_lock(&dtrace_lock
);
17516 if (dtrace_kernel_symbol_mode
== DTRACE_KERNEL_SYMBOLS_FROM_USERSPACE
) {
17517 dtrace_kernel_symbol_mode
= DTRACE_KERNEL_SYMBOLS_FROM_KERNEL
;
17519 lck_mtx_unlock(&dtrace_lock
);
17526 dtrace_close(dev_t dev
, int flag
, int otyp
, cred_t
*cred_p
)
17528 #pragma unused(flag, otyp, cred_p) /* __APPLE__ */
17529 minor_t minor
= getminor(dev
);
17530 dtrace_state_t
*state
;
17532 /* APPLE NOTE: Darwin puts Helper on its own major device. */
17533 state
= dtrace_state_get(minor
);
17535 lck_mtx_lock(&cpu_lock
);
17536 lck_mtx_lock(&dtrace_lock
);
17538 if (state
->dts_anon
) {
17540 * There is anonymous state. Destroy that first.
17542 ASSERT(dtrace_anon
.dta_state
== NULL
);
17543 dtrace_state_destroy(state
->dts_anon
);
17546 dtrace_state_destroy(state
);
17547 ASSERT(dtrace_opens
> 0);
17550 * Only relinquish control of the kernel debugger interface when there
17551 * are no consumers and no anonymous enablings.
17553 if (--dtrace_opens
== 0 && dtrace_anon
.dta_enabling
== NULL
) {
17555 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE
);
17559 lck_mtx_unlock(&dtrace_lock
);
17560 lck_mtx_unlock(&cpu_lock
);
17563 * Lock ordering requires the dof mode lock be taken before
17566 lck_rw_lock_exclusive(&dtrace_dof_mode_lock
);
17567 lck_mtx_lock(&dtrace_lock
);
17569 if (dtrace_opens
== 0) {
17571 * If we are currently lazy-off, and this is the last close, transition to
17574 if (dtrace_dof_mode
== DTRACE_DOF_MODE_LAZY_OFF
) {
17575 dtrace_dof_mode
= DTRACE_DOF_MODE_LAZY_ON
;
17579 * If we are the last dtrace client, switch back to lazy (from userspace) symbols
17581 if (dtrace_kernel_symbol_mode
== DTRACE_KERNEL_SYMBOLS_FROM_KERNEL
) {
17582 dtrace_kernel_symbol_mode
= DTRACE_KERNEL_SYMBOLS_FROM_USERSPACE
;
17586 lck_mtx_unlock(&dtrace_lock
);
17587 lck_rw_unlock_exclusive(&dtrace_dof_mode_lock
);
17590 * Kext probes may be retained past the end of the kext's lifespan. The
17591 * probes are kept until the last reference to them has been removed.
17592 * Since closing an active dtrace context is likely to drop that last reference,
17593 * lets take a shot at cleaning out the orphaned probes now.
17595 dtrace_module_unloaded(NULL
);
17602 dtrace_ioctl_helper(u_long cmd
, caddr_t arg
, int *rv
)
17606 * Safe to check this outside the dof mode lock
17608 if (dtrace_dof_mode
== DTRACE_DOF_MODE_NEVER
)
17609 return KERN_SUCCESS
;
17612 #if defined (__arm64__)
17613 case DTRACEHIOC_ADDDOF_U32
:
17614 case DTRACEHIOC_ADDDOF_U64
:
17616 case DTRACEHIOC_ADDDOF
:
17617 #endif /* __arm64__*/
17619 dof_helper_t
*dhp
= NULL
;
17620 size_t dof_ioctl_data_size
;
17621 dof_ioctl_data_t
* multi_dof
;
17624 user_addr_t user_address
= *(user_addr_t
*)arg
;
17625 uint64_t dof_count
;
17626 int multi_dof_claimed
= 0;
17627 proc_t
* p
= current_proc();
17630 * If this is a restricted process and dtrace is restricted,
17631 * do not allow DOFs to be registered
17633 if (dtrace_is_restricted() &&
17634 !dtrace_are_restrictions_relaxed() &&
17635 !dtrace_can_attach_to_proc(current_proc())) {
17640 * Read the number of DOF sections being passed in.
17642 if (copyin(user_address
+ offsetof(dof_ioctl_data_t
, dofiod_count
),
17644 sizeof(dof_count
))) {
17645 dtrace_dof_error(NULL
, "failed to copyin dofiod_count");
17650 * Range check the count.
17652 if (dof_count
== 0 || dof_count
> 1024) {
17653 dtrace_dof_error(NULL
, "dofiod_count is not valid");
17658 * Allocate a correctly sized structure and copyin the data.
17660 dof_ioctl_data_size
= DOF_IOCTL_DATA_T_SIZE(dof_count
);
17661 if ((multi_dof
= kmem_alloc(dof_ioctl_data_size
, KM_SLEEP
)) == NULL
)
17664 /* NOTE! We can no longer exit this method via return */
17665 if (copyin(user_address
, multi_dof
, dof_ioctl_data_size
) != 0) {
17666 dtrace_dof_error(NULL
, "failed copyin of dof_ioctl_data_t");
17672 * Check that the count didn't change between the first copyin and the second.
17674 if (multi_dof
->dofiod_count
!= dof_count
) {
17680 * Try to process lazily first.
17682 rval
= dtrace_lazy_dofs_add(p
, multi_dof
, &multi_dof_claimed
);
17685 * If rval is EACCES, we must be non-lazy.
17687 if (rval
== EACCES
) {
17690 * Process each dof_helper_t
17694 dhp
= &multi_dof
->dofiod_helpers
[i
];
17696 dof_hdr_t
*dof
= dtrace_dof_copyin(dhp
->dofhp_dof
, &rval
);
17699 lck_mtx_lock(&dtrace_meta_lock
);
17700 lck_mtx_lock(&dtrace_lock
);
17703 * dtrace_helper_slurp() takes responsibility for the dof --
17704 * it may free it now or it may save it and free it later.
17706 if ((dhp
->dofhp_dof
= (uint64_t)dtrace_helper_slurp(p
, dof
, dhp
)) == -1ULL) {
17710 lck_mtx_unlock(&dtrace_lock
);
17711 lck_mtx_unlock(&dtrace_meta_lock
);
17713 } while (++i
< multi_dof
->dofiod_count
&& rval
== 0);
17717 * We need to copyout the multi_dof struct, because it contains
17718 * the generation (unique id) values needed to call DTRACEHIOC_REMOVE
17720 * This could certainly be better optimized.
17722 if (copyout(multi_dof
, user_address
, dof_ioctl_data_size
) != 0) {
17723 dtrace_dof_error(NULL
, "failed copyout of dof_ioctl_data_t");
17724 /* Don't overwrite pre-existing error code */
17725 if (rval
== 0) rval
= EFAULT
;
17730 * If we had to allocate struct memory, free it.
17732 if (multi_dof
!= NULL
&& !multi_dof_claimed
) {
17733 kmem_free(multi_dof
, dof_ioctl_data_size
);
17739 case DTRACEHIOC_REMOVE
: {
17740 int generation
= *(int*)arg
;
17741 proc_t
* p
= current_proc();
17746 int rval
= dtrace_lazy_dofs_remove(p
, generation
);
17749 * EACCES means non-lazy
17751 if (rval
== EACCES
) {
17752 lck_mtx_lock(&dtrace_meta_lock
);
17753 lck_mtx_lock(&dtrace_lock
);
17754 rval
= dtrace_helper_destroygen(p
, generation
);
17755 lck_mtx_unlock(&dtrace_lock
);
17756 lck_mtx_unlock(&dtrace_meta_lock
);
17771 dtrace_ioctl(dev_t dev
, u_long cmd
, user_addr_t arg
, int md
, cred_t
*cr
, int *rv
)
17774 minor_t minor
= getminor(dev
);
17775 dtrace_state_t
*state
;
17778 /* Darwin puts Helper on its own major device. */
17780 state
= dtrace_state_get(minor
);
17782 if (state
->dts_anon
) {
17783 ASSERT(dtrace_anon
.dta_state
== NULL
);
17784 state
= state
->dts_anon
;
17788 case DTRACEIOC_PROVIDER
: {
17789 dtrace_providerdesc_t pvd
;
17790 dtrace_provider_t
*pvp
;
17792 if (copyin(arg
, &pvd
, sizeof (pvd
)) != 0)
17795 pvd
.dtvd_name
[DTRACE_PROVNAMELEN
- 1] = '\0';
17796 lck_mtx_lock(&dtrace_provider_lock
);
17798 for (pvp
= dtrace_provider
; pvp
!= NULL
; pvp
= pvp
->dtpv_next
) {
17799 if (strncmp(pvp
->dtpv_name
, pvd
.dtvd_name
, DTRACE_PROVNAMELEN
) == 0)
17803 lck_mtx_unlock(&dtrace_provider_lock
);
17808 bcopy(&pvp
->dtpv_priv
, &pvd
.dtvd_priv
, sizeof (dtrace_ppriv_t
));
17809 bcopy(&pvp
->dtpv_attr
, &pvd
.dtvd_attr
, sizeof (dtrace_pattr_t
));
17810 if (copyout(&pvd
, arg
, sizeof (pvd
)) != 0)
17816 case DTRACEIOC_EPROBE
: {
17817 dtrace_eprobedesc_t epdesc
;
17819 dtrace_action_t
*act
;
17825 if (copyin(arg
, &epdesc
, sizeof (epdesc
)) != 0)
17828 lck_mtx_lock(&dtrace_lock
);
17830 if ((ecb
= dtrace_epid2ecb(state
, epdesc
.dtepd_epid
)) == NULL
) {
17831 lck_mtx_unlock(&dtrace_lock
);
17835 if (ecb
->dte_probe
== NULL
) {
17836 lck_mtx_unlock(&dtrace_lock
);
17840 epdesc
.dtepd_probeid
= ecb
->dte_probe
->dtpr_id
;
17841 epdesc
.dtepd_uarg
= ecb
->dte_uarg
;
17842 epdesc
.dtepd_size
= ecb
->dte_size
;
17844 nrecs
= epdesc
.dtepd_nrecs
;
17845 epdesc
.dtepd_nrecs
= 0;
17846 for (act
= ecb
->dte_action
; act
!= NULL
; act
= act
->dta_next
) {
17847 if (DTRACEACT_ISAGG(act
->dta_kind
) || act
->dta_intuple
)
17850 epdesc
.dtepd_nrecs
++;
17854 * Now that we have the size, we need to allocate a temporary
17855 * buffer in which to store the complete description. We need
17856 * the temporary buffer to be able to drop dtrace_lock()
17857 * across the copyout(), below.
17859 size
= sizeof (dtrace_eprobedesc_t
) +
17860 (epdesc
.dtepd_nrecs
* sizeof (dtrace_recdesc_t
));
17862 buf
= kmem_alloc(size
, KM_SLEEP
);
17863 dest
= (uintptr_t)buf
;
17865 bcopy(&epdesc
, (void *)dest
, sizeof (epdesc
));
17866 dest
+= offsetof(dtrace_eprobedesc_t
, dtepd_rec
[0]);
17868 for (act
= ecb
->dte_action
; act
!= NULL
; act
= act
->dta_next
) {
17869 if (DTRACEACT_ISAGG(act
->dta_kind
) || act
->dta_intuple
)
17875 bcopy(&act
->dta_rec
, (void *)dest
,
17876 sizeof (dtrace_recdesc_t
));
17877 dest
+= sizeof (dtrace_recdesc_t
);
17880 lck_mtx_unlock(&dtrace_lock
);
17882 if (copyout(buf
, arg
, dest
- (uintptr_t)buf
) != 0) {
17883 kmem_free(buf
, size
);
17887 kmem_free(buf
, size
);
17891 case DTRACEIOC_AGGDESC
: {
17892 dtrace_aggdesc_t aggdesc
;
17893 dtrace_action_t
*act
;
17894 dtrace_aggregation_t
*agg
;
17897 dtrace_recdesc_t
*lrec
;
17902 if (copyin(arg
, &aggdesc
, sizeof (aggdesc
)) != 0)
17905 lck_mtx_lock(&dtrace_lock
);
17907 if ((agg
= dtrace_aggid2agg(state
, aggdesc
.dtagd_id
)) == NULL
) {
17908 lck_mtx_unlock(&dtrace_lock
);
17912 aggdesc
.dtagd_epid
= agg
->dtag_ecb
->dte_epid
;
17914 nrecs
= aggdesc
.dtagd_nrecs
;
17915 aggdesc
.dtagd_nrecs
= 0;
17917 offs
= agg
->dtag_base
;
17918 lrec
= &agg
->dtag_action
.dta_rec
;
17919 aggdesc
.dtagd_size
= lrec
->dtrd_offset
+ lrec
->dtrd_size
- offs
;
17921 for (act
= agg
->dtag_first
; ; act
= act
->dta_next
) {
17922 ASSERT(act
->dta_intuple
||
17923 DTRACEACT_ISAGG(act
->dta_kind
));
17926 * If this action has a record size of zero, it
17927 * denotes an argument to the aggregating action.
17928 * Because the presence of this record doesn't (or
17929 * shouldn't) affect the way the data is interpreted,
17930 * we don't copy it out to save user-level the
17931 * confusion of dealing with a zero-length record.
17933 if (act
->dta_rec
.dtrd_size
== 0) {
17934 ASSERT(agg
->dtag_hasarg
);
17938 aggdesc
.dtagd_nrecs
++;
17940 if (act
== &agg
->dtag_action
)
17945 * Now that we have the size, we need to allocate a temporary
17946 * buffer in which to store the complete description. We need
17947 * the temporary buffer to be able to drop dtrace_lock()
17948 * across the copyout(), below.
17950 size
= sizeof (dtrace_aggdesc_t
) +
17951 (aggdesc
.dtagd_nrecs
* sizeof (dtrace_recdesc_t
));
17953 buf
= kmem_alloc(size
, KM_SLEEP
);
17954 dest
= (uintptr_t)buf
;
17956 bcopy(&aggdesc
, (void *)dest
, sizeof (aggdesc
));
17957 dest
+= offsetof(dtrace_aggdesc_t
, dtagd_rec
[0]);
17959 for (act
= agg
->dtag_first
; ; act
= act
->dta_next
) {
17960 dtrace_recdesc_t rec
= act
->dta_rec
;
17963 * See the comment in the above loop for why we pass
17964 * over zero-length records.
17966 if (rec
.dtrd_size
== 0) {
17967 ASSERT(agg
->dtag_hasarg
);
17974 rec
.dtrd_offset
-= offs
;
17975 bcopy(&rec
, (void *)dest
, sizeof (rec
));
17976 dest
+= sizeof (dtrace_recdesc_t
);
17978 if (act
== &agg
->dtag_action
)
17982 lck_mtx_unlock(&dtrace_lock
);
17984 if (copyout(buf
, arg
, dest
- (uintptr_t)buf
) != 0) {
17985 kmem_free(buf
, size
);
17989 kmem_free(buf
, size
);
17993 case DTRACEIOC_ENABLE
: {
17995 dtrace_enabling_t
*enab
= NULL
;
17996 dtrace_vstate_t
*vstate
;
18002 * If a NULL argument has been passed, we take this as our
18003 * cue to reevaluate our enablings.
18006 dtrace_enabling_matchall();
18011 if ((dof
= dtrace_dof_copyin(arg
, &rval
)) == NULL
)
18014 lck_mtx_lock(&cpu_lock
);
18015 lck_mtx_lock(&dtrace_lock
);
18016 vstate
= &state
->dts_vstate
;
18018 if (state
->dts_activity
!= DTRACE_ACTIVITY_INACTIVE
) {
18019 lck_mtx_unlock(&dtrace_lock
);
18020 lck_mtx_unlock(&cpu_lock
);
18021 dtrace_dof_destroy(dof
);
18025 if (dtrace_dof_slurp(dof
, vstate
, cr
, &enab
, 0, B_TRUE
) != 0) {
18026 lck_mtx_unlock(&dtrace_lock
);
18027 lck_mtx_unlock(&cpu_lock
);
18028 dtrace_dof_destroy(dof
);
18032 if ((rval
= dtrace_dof_options(dof
, state
)) != 0) {
18033 dtrace_enabling_destroy(enab
);
18034 lck_mtx_unlock(&dtrace_lock
);
18035 lck_mtx_unlock(&cpu_lock
);
18036 dtrace_dof_destroy(dof
);
18040 if ((err
= dtrace_enabling_match(enab
, rv
, NULL
)) == 0) {
18041 err
= dtrace_enabling_retain(enab
);
18043 dtrace_enabling_destroy(enab
);
18046 lck_mtx_unlock(&dtrace_lock
);
18047 lck_mtx_unlock(&cpu_lock
);
18048 dtrace_dof_destroy(dof
);
18053 case DTRACEIOC_REPLICATE
: {
18054 dtrace_repldesc_t desc
;
18055 dtrace_probedesc_t
*match
= &desc
.dtrpd_match
;
18056 dtrace_probedesc_t
*create
= &desc
.dtrpd_create
;
18059 if (copyin(arg
, &desc
, sizeof (desc
)) != 0)
18062 match
->dtpd_provider
[DTRACE_PROVNAMELEN
- 1] = '\0';
18063 match
->dtpd_mod
[DTRACE_MODNAMELEN
- 1] = '\0';
18064 match
->dtpd_func
[DTRACE_FUNCNAMELEN
- 1] = '\0';
18065 match
->dtpd_name
[DTRACE_NAMELEN
- 1] = '\0';
18067 create
->dtpd_provider
[DTRACE_PROVNAMELEN
- 1] = '\0';
18068 create
->dtpd_mod
[DTRACE_MODNAMELEN
- 1] = '\0';
18069 create
->dtpd_func
[DTRACE_FUNCNAMELEN
- 1] = '\0';
18070 create
->dtpd_name
[DTRACE_NAMELEN
- 1] = '\0';
18072 lck_mtx_lock(&dtrace_lock
);
18073 err
= dtrace_enabling_replicate(state
, match
, create
);
18074 lck_mtx_unlock(&dtrace_lock
);
18079 case DTRACEIOC_PROBEMATCH
:
18080 case DTRACEIOC_PROBES
: {
18081 dtrace_probe_t
*probe
= NULL
;
18082 dtrace_probedesc_t desc
;
18083 dtrace_probekey_t pkey
;
18090 if (copyin(arg
, &desc
, sizeof (desc
)) != 0)
18093 desc
.dtpd_provider
[DTRACE_PROVNAMELEN
- 1] = '\0';
18094 desc
.dtpd_mod
[DTRACE_MODNAMELEN
- 1] = '\0';
18095 desc
.dtpd_func
[DTRACE_FUNCNAMELEN
- 1] = '\0';
18096 desc
.dtpd_name
[DTRACE_NAMELEN
- 1] = '\0';
18099 * Before we attempt to match this probe, we want to give
18100 * all providers the opportunity to provide it.
18102 if (desc
.dtpd_id
== DTRACE_IDNONE
) {
18103 lck_mtx_lock(&dtrace_provider_lock
);
18104 dtrace_probe_provide(&desc
, NULL
);
18105 lck_mtx_unlock(&dtrace_provider_lock
);
18109 dtrace_cred2priv(cr
, &priv
, &uid
, &zoneid
);
18111 lck_mtx_lock(&dtrace_lock
);
18113 if (cmd
== DTRACEIOC_PROBEMATCH
) {
18114 dtrace_probekey(&desc
, &pkey
);
18115 pkey
.dtpk_id
= DTRACE_IDNONE
;
18117 /* Quiet compiler warning */
18118 for (i
= desc
.dtpd_id
; i
<= (dtrace_id_t
)dtrace_nprobes
; i
++) {
18119 if ((probe
= dtrace_probes
[i
- 1]) != NULL
&&
18120 (m
= dtrace_match_probe(probe
, &pkey
,
18121 priv
, uid
, zoneid
)) != 0)
18126 lck_mtx_unlock(&dtrace_lock
);
18129 dtrace_probekey_release(&pkey
);
18132 /* Quiet compiler warning */
18133 for (i
= desc
.dtpd_id
; i
<= (dtrace_id_t
)dtrace_nprobes
; i
++) {
18134 if ((probe
= dtrace_probes
[i
- 1]) != NULL
&&
18135 dtrace_match_priv(probe
, priv
, uid
, zoneid
))
18140 if (probe
== NULL
) {
18141 lck_mtx_unlock(&dtrace_lock
);
18145 dtrace_probe_description(probe
, &desc
);
18146 lck_mtx_unlock(&dtrace_lock
);
18148 if (copyout(&desc
, arg
, sizeof (desc
)) != 0)
18154 case DTRACEIOC_PROBEARG
: {
18155 dtrace_argdesc_t desc
;
18156 dtrace_probe_t
*probe
;
18157 dtrace_provider_t
*prov
;
18159 if (copyin(arg
, &desc
, sizeof (desc
)) != 0)
18162 if (desc
.dtargd_id
== DTRACE_IDNONE
)
18165 if (desc
.dtargd_ndx
== DTRACE_ARGNONE
)
18168 lck_mtx_lock(&dtrace_provider_lock
);
18169 lck_mtx_lock(&mod_lock
);
18170 lck_mtx_lock(&dtrace_lock
);
18172 /* Quiet compiler warning */
18173 if (desc
.dtargd_id
> (dtrace_id_t
)dtrace_nprobes
) {
18174 lck_mtx_unlock(&dtrace_lock
);
18175 lck_mtx_unlock(&mod_lock
);
18176 lck_mtx_unlock(&dtrace_provider_lock
);
18180 if ((probe
= dtrace_probes
[desc
.dtargd_id
- 1]) == NULL
) {
18181 lck_mtx_unlock(&dtrace_lock
);
18182 lck_mtx_unlock(&mod_lock
);
18183 lck_mtx_unlock(&dtrace_provider_lock
);
18187 lck_mtx_unlock(&dtrace_lock
);
18189 prov
= probe
->dtpr_provider
;
18191 if (prov
->dtpv_pops
.dtps_getargdesc
== NULL
) {
18193 * There isn't any typed information for this probe.
18194 * Set the argument number to DTRACE_ARGNONE.
18196 desc
.dtargd_ndx
= DTRACE_ARGNONE
;
18198 desc
.dtargd_native
[0] = '\0';
18199 desc
.dtargd_xlate
[0] = '\0';
18200 desc
.dtargd_mapping
= desc
.dtargd_ndx
;
18202 prov
->dtpv_pops
.dtps_getargdesc(prov
->dtpv_arg
,
18203 probe
->dtpr_id
, probe
->dtpr_arg
, &desc
);
18206 lck_mtx_unlock(&mod_lock
);
18207 lck_mtx_unlock(&dtrace_provider_lock
);
18209 if (copyout(&desc
, arg
, sizeof (desc
)) != 0)
18215 case DTRACEIOC_GO
: {
18216 processorid_t cpuid
;
18217 rval
= dtrace_state_go(state
, &cpuid
);
18222 if (copyout(&cpuid
, arg
, sizeof (cpuid
)) != 0)
18228 case DTRACEIOC_STOP
: {
18229 processorid_t cpuid
;
18231 lck_mtx_lock(&dtrace_lock
);
18232 rval
= dtrace_state_stop(state
, &cpuid
);
18233 lck_mtx_unlock(&dtrace_lock
);
18238 if (copyout(&cpuid
, arg
, sizeof (cpuid
)) != 0)
18244 case DTRACEIOC_DOFGET
: {
18245 dof_hdr_t hdr
, *dof
;
18248 if (copyin(arg
, &hdr
, sizeof (hdr
)) != 0)
18251 lck_mtx_lock(&dtrace_lock
);
18252 dof
= dtrace_dof_create(state
);
18253 lck_mtx_unlock(&dtrace_lock
);
18255 len
= MIN(hdr
.dofh_loadsz
, dof
->dofh_loadsz
);
18256 rval
= copyout(dof
, arg
, len
);
18257 dtrace_dof_destroy(dof
);
18259 return (rval
== 0 ? 0 : EFAULT
);
18262 case DTRACEIOC_SLEEP
: {
18265 uint64_t rvalue
= DTRACE_WAKE_TIMEOUT
;
18267 if (copyin(arg
, &time
, sizeof(time
)) != 0)
18270 nanoseconds_to_absolutetime((uint64_t)time
, &abstime
);
18271 clock_absolutetime_interval_to_deadline(abstime
, &abstime
);
18273 if (assert_wait_deadline(state
, THREAD_ABORTSAFE
, abstime
) == THREAD_WAITING
) {
18274 if (state
->dts_buf_over_limit
> 0) {
18275 clear_wait(current_thread(), THREAD_INTERRUPTED
);
18276 rvalue
= DTRACE_WAKE_BUF_LIMIT
;
18278 thread_block(THREAD_CONTINUE_NULL
);
18279 if (state
->dts_buf_over_limit
> 0) {
18280 rvalue
= DTRACE_WAKE_BUF_LIMIT
;
18285 if (copyout(&rvalue
, arg
, sizeof(rvalue
)) != 0)
18291 case DTRACEIOC_SIGNAL
: {
18296 case DTRACEIOC_AGGSNAP
:
18297 case DTRACEIOC_BUFSNAP
: {
18298 dtrace_bufdesc_t desc
;
18300 boolean_t over_limit
;
18301 dtrace_buffer_t
*buf
;
18303 if (copyin(arg
, &desc
, sizeof (desc
)) != 0)
18306 if ((int)desc
.dtbd_cpu
< 0 || desc
.dtbd_cpu
>= NCPU
)
18309 lck_mtx_lock(&dtrace_lock
);
18311 if (cmd
== DTRACEIOC_BUFSNAP
) {
18312 buf
= &state
->dts_buffer
[desc
.dtbd_cpu
];
18314 buf
= &state
->dts_aggbuffer
[desc
.dtbd_cpu
];
18317 if (buf
->dtb_flags
& (DTRACEBUF_RING
| DTRACEBUF_FILL
)) {
18318 size_t sz
= buf
->dtb_offset
;
18320 if (state
->dts_activity
!= DTRACE_ACTIVITY_STOPPED
) {
18321 lck_mtx_unlock(&dtrace_lock
);
18326 * If this buffer has already been consumed, we're
18327 * going to indicate that there's nothing left here
18330 if (buf
->dtb_flags
& DTRACEBUF_CONSUMED
) {
18331 lck_mtx_unlock(&dtrace_lock
);
18333 desc
.dtbd_size
= 0;
18334 desc
.dtbd_drops
= 0;
18335 desc
.dtbd_errors
= 0;
18336 desc
.dtbd_oldest
= 0;
18337 sz
= sizeof (desc
);
18339 if (copyout(&desc
, arg
, sz
) != 0)
18346 * If this is a ring buffer that has wrapped, we want
18347 * to copy the whole thing out.
18349 if (buf
->dtb_flags
& DTRACEBUF_WRAPPED
) {
18350 dtrace_buffer_polish(buf
);
18351 sz
= buf
->dtb_size
;
18354 if (copyout(buf
->dtb_tomax
, (user_addr_t
)desc
.dtbd_data
, sz
) != 0) {
18355 lck_mtx_unlock(&dtrace_lock
);
18359 desc
.dtbd_size
= sz
;
18360 desc
.dtbd_drops
= buf
->dtb_drops
;
18361 desc
.dtbd_errors
= buf
->dtb_errors
;
18362 desc
.dtbd_oldest
= buf
->dtb_xamot_offset
;
18363 desc
.dtbd_timestamp
= dtrace_gethrtime();
18365 lck_mtx_unlock(&dtrace_lock
);
18367 if (copyout(&desc
, arg
, sizeof (desc
)) != 0)
18370 buf
->dtb_flags
|= DTRACEBUF_CONSUMED
;
18375 if (buf
->dtb_tomax
== NULL
) {
18376 ASSERT(buf
->dtb_xamot
== NULL
);
18377 lck_mtx_unlock(&dtrace_lock
);
18381 cached
= buf
->dtb_tomax
;
18382 over_limit
= buf
->dtb_cur_limit
== buf
->dtb_size
;
18384 ASSERT(!(buf
->dtb_flags
& DTRACEBUF_NOSWITCH
));
18386 dtrace_xcall(desc
.dtbd_cpu
,
18387 (dtrace_xcall_t
)dtrace_buffer_switch
, buf
);
18389 state
->dts_errors
+= buf
->dtb_xamot_errors
;
18392 * If the buffers did not actually switch, then the cross call
18393 * did not take place -- presumably because the given CPU is
18394 * not in the ready set. If this is the case, we'll return
18397 if (buf
->dtb_tomax
== cached
) {
18398 ASSERT(buf
->dtb_xamot
!= cached
);
18399 lck_mtx_unlock(&dtrace_lock
);
18403 ASSERT(cached
== buf
->dtb_xamot
);
18405 * At this point we know the buffer have switched, so we
18406 * can decrement the over limit count if the buffer was over
18407 * its limit. The new buffer might already be over its limit
18408 * yet, but we don't care since we're guaranteed not to be
18409 * checking the buffer over limit count at this point.
18412 uint32_t old
= os_atomic_dec_orig(&state
->dts_buf_over_limit
, relaxed
);
18413 #pragma unused(old)
18416 * Verify that we didn't underflow the value
18422 * We have our snapshot; now copy it out.
18424 if (dtrace_buffer_copyout(buf
->dtb_xamot
,
18425 (user_addr_t
)desc
.dtbd_data
,
18426 buf
->dtb_xamot_offset
) != 0) {
18427 lck_mtx_unlock(&dtrace_lock
);
18431 desc
.dtbd_size
= buf
->dtb_xamot_offset
;
18432 desc
.dtbd_drops
= buf
->dtb_xamot_drops
;
18433 desc
.dtbd_errors
= buf
->dtb_xamot_errors
;
18434 desc
.dtbd_oldest
= 0;
18435 desc
.dtbd_timestamp
= buf
->dtb_switched
;
18437 lck_mtx_unlock(&dtrace_lock
);
18440 * Finally, copy out the buffer description.
18442 if (copyout(&desc
, arg
, sizeof (desc
)) != 0)
18448 case DTRACEIOC_CONF
: {
18449 dtrace_conf_t conf
;
18451 bzero(&conf
, sizeof (conf
));
18452 conf
.dtc_difversion
= DIF_VERSION
;
18453 conf
.dtc_difintregs
= DIF_DIR_NREGS
;
18454 conf
.dtc_diftupregs
= DIF_DTR_NREGS
;
18455 conf
.dtc_ctfmodel
= CTF_MODEL_NATIVE
;
18457 if (copyout(&conf
, arg
, sizeof (conf
)) != 0)
18463 case DTRACEIOC_STATUS
: {
18464 dtrace_status_t stat
;
18465 dtrace_dstate_t
*dstate
;
18470 * See the comment in dtrace_state_deadman() for the reason
18471 * for setting dts_laststatus to INT64_MAX before setting
18472 * it to the correct value.
18474 state
->dts_laststatus
= INT64_MAX
;
18475 dtrace_membar_producer();
18476 state
->dts_laststatus
= dtrace_gethrtime();
18478 bzero(&stat
, sizeof (stat
));
18480 lck_mtx_lock(&dtrace_lock
);
18482 if (state
->dts_activity
== DTRACE_ACTIVITY_INACTIVE
) {
18483 lck_mtx_unlock(&dtrace_lock
);
18487 if (state
->dts_activity
== DTRACE_ACTIVITY_DRAINING
)
18488 stat
.dtst_exiting
= 1;
18490 nerrs
= state
->dts_errors
;
18491 dstate
= &state
->dts_vstate
.dtvs_dynvars
;
18493 for (i
= 0; i
< (int)NCPU
; i
++) {
18494 dtrace_dstate_percpu_t
*dcpu
= &dstate
->dtds_percpu
[i
];
18496 stat
.dtst_dyndrops
+= dcpu
->dtdsc_drops
;
18497 stat
.dtst_dyndrops_dirty
+= dcpu
->dtdsc_dirty_drops
;
18498 stat
.dtst_dyndrops_rinsing
+= dcpu
->dtdsc_rinsing_drops
;
18500 if (state
->dts_buffer
[i
].dtb_flags
& DTRACEBUF_FULL
)
18501 stat
.dtst_filled
++;
18503 nerrs
+= state
->dts_buffer
[i
].dtb_errors
;
18505 for (j
= 0; j
< state
->dts_nspeculations
; j
++) {
18506 dtrace_speculation_t
*spec
;
18507 dtrace_buffer_t
*buf
;
18509 spec
= &state
->dts_speculations
[j
];
18510 buf
= &spec
->dtsp_buffer
[i
];
18511 stat
.dtst_specdrops
+= buf
->dtb_xamot_drops
;
18515 stat
.dtst_specdrops_busy
= state
->dts_speculations_busy
;
18516 stat
.dtst_specdrops_unavail
= state
->dts_speculations_unavail
;
18517 stat
.dtst_stkstroverflows
= state
->dts_stkstroverflows
;
18518 stat
.dtst_dblerrors
= state
->dts_dblerrors
;
18520 (state
->dts_activity
== DTRACE_ACTIVITY_KILLED
);
18521 stat
.dtst_errors
= nerrs
;
18523 lck_mtx_unlock(&dtrace_lock
);
18525 if (copyout(&stat
, arg
, sizeof (stat
)) != 0)
18531 case DTRACEIOC_FORMAT
: {
18532 dtrace_fmtdesc_t fmt
;
18536 if (copyin(arg
, &fmt
, sizeof (fmt
)) != 0)
18539 lck_mtx_lock(&dtrace_lock
);
18541 if (fmt
.dtfd_format
== 0 ||
18542 fmt
.dtfd_format
> state
->dts_nformats
) {
18543 lck_mtx_unlock(&dtrace_lock
);
18548 * Format strings are allocated contiguously and they are
18549 * never freed; if a format index is less than the number
18550 * of formats, we can assert that the format map is non-NULL
18551 * and that the format for the specified index is non-NULL.
18553 ASSERT(state
->dts_formats
!= NULL
);
18554 str
= state
->dts_formats
[fmt
.dtfd_format
- 1]->dtf_str
;
18555 ASSERT(str
!= NULL
);
18557 len
= strlen(str
) + 1;
18559 if (len
> fmt
.dtfd_length
) {
18560 fmt
.dtfd_length
= len
;
18562 if (copyout(&fmt
, arg
, sizeof (fmt
)) != 0) {
18563 lck_mtx_unlock(&dtrace_lock
);
18567 if (copyout(str
, (user_addr_t
)fmt
.dtfd_string
, len
) != 0) {
18568 lck_mtx_unlock(&dtrace_lock
);
18573 lck_mtx_unlock(&dtrace_lock
);
18577 case DTRACEIOC_MODUUIDSLIST
: {
18578 size_t module_uuids_list_size
;
18579 dtrace_module_uuids_list_t
* uuids_list
;
18580 uint64_t dtmul_count
;
18583 * Security restrictions make this operation illegal, if this is enabled DTrace
18584 * must refuse to provide any fbt probes.
18586 if (dtrace_fbt_probes_restricted()) {
18587 cmn_err(CE_WARN
, "security restrictions disallow DTRACEIOC_MODUUIDSLIST");
18592 * Fail if the kernel symbol mode makes this operation illegal.
18593 * Both NEVER & ALWAYS_FROM_KERNEL are permanent states, it is legal to check
18594 * for them without holding the dtrace_lock.
18596 if (dtrace_kernel_symbol_mode
== DTRACE_KERNEL_SYMBOLS_NEVER
||
18597 dtrace_kernel_symbol_mode
== DTRACE_KERNEL_SYMBOLS_ALWAYS_FROM_KERNEL
) {
18598 cmn_err(CE_WARN
, "dtrace_kernel_symbol_mode of %u disallows DTRACEIOC_MODUUIDSLIST", dtrace_kernel_symbol_mode
);
18603 * Read the number of symbolsdesc structs being passed in.
18605 if (copyin(arg
+ offsetof(dtrace_module_uuids_list_t
, dtmul_count
),
18607 sizeof(dtmul_count
))) {
18608 cmn_err(CE_WARN
, "failed to copyin dtmul_count");
18613 * Range check the count. More than 2k kexts is probably an error.
18615 if (dtmul_count
> 2048) {
18616 cmn_err(CE_WARN
, "dtmul_count is not valid");
18621 * For all queries, we return EINVAL when the user specified
18622 * count does not match the actual number of modules we find
18625 * If the user specified count is zero, then this serves as a
18626 * simple query to count the available modules in need of symbols.
18631 if (dtmul_count
== 0)
18633 lck_mtx_lock(&mod_lock
);
18634 struct modctl
* ctl
= dtrace_modctl_list
;
18636 ASSERT(!MOD_HAS_USERSPACE_SYMBOLS(ctl
));
18637 if (!MOD_SYMBOLS_DONE(ctl
) && !MOD_IS_STATIC_KEXT(ctl
)) {
18641 ctl
= ctl
->mod_next
;
18643 lck_mtx_unlock(&mod_lock
);
18645 if (copyout(&dtmul_count
, arg
, sizeof (dtmul_count
)) != 0)
18652 * If we reach this point, then we have a request for full list data.
18653 * Allocate a correctly sized structure and copyin the data.
18655 module_uuids_list_size
= DTRACE_MODULE_UUIDS_LIST_SIZE(dtmul_count
);
18656 if ((uuids_list
= kmem_alloc(module_uuids_list_size
, KM_SLEEP
)) == NULL
)
18659 /* NOTE! We can no longer exit this method via return */
18660 if (copyin(arg
, uuids_list
, module_uuids_list_size
) != 0) {
18661 cmn_err(CE_WARN
, "failed copyin of dtrace_module_uuids_list_t");
18663 goto moduuidslist_cleanup
;
18667 * Check that the count didn't change between the first copyin and the second.
18669 if (uuids_list
->dtmul_count
!= dtmul_count
) {
18671 goto moduuidslist_cleanup
;
18675 * Build the list of UUID's that need symbols
18677 lck_mtx_lock(&mod_lock
);
18681 struct modctl
* ctl
= dtrace_modctl_list
;
18684 * We assume that userspace symbols will be "better" than kernel level symbols,
18685 * as userspace can search for dSYM(s) and symbol'd binaries. Even if kernel syms
18686 * are available, add user syms if the module might use them.
18688 ASSERT(!MOD_HAS_USERSPACE_SYMBOLS(ctl
));
18689 if (!MOD_SYMBOLS_DONE(ctl
) && !MOD_IS_STATIC_KEXT(ctl
)) {
18690 UUID
* uuid
= &uuids_list
->dtmul_uuid
[dtmul_count
];
18691 if (dtmul_count
++ < uuids_list
->dtmul_count
) {
18692 memcpy(uuid
, ctl
->mod_uuid
, sizeof(UUID
));
18695 ctl
= ctl
->mod_next
;
18698 lck_mtx_unlock(&mod_lock
);
18700 if (uuids_list
->dtmul_count
< dtmul_count
)
18703 uuids_list
->dtmul_count
= dtmul_count
;
18706 * Copyout the symbols list (or at least the count!)
18708 if (copyout(uuids_list
, arg
, module_uuids_list_size
) != 0) {
18709 cmn_err(CE_WARN
, "failed copyout of dtrace_symbolsdesc_list_t");
18713 moduuidslist_cleanup
:
18715 * If we had to allocate struct memory, free it.
18717 if (uuids_list
!= NULL
) {
18718 kmem_free(uuids_list
, module_uuids_list_size
);
18724 case DTRACEIOC_PROVMODSYMS
: {
18725 size_t module_symbols_size
;
18726 dtrace_module_symbols_t
* module_symbols
;
18727 uint64_t dtmodsyms_count
;
18730 * Security restrictions make this operation illegal, if this is enabled DTrace
18731 * must refuse to provide any fbt probes.
18733 if (dtrace_fbt_probes_restricted()) {
18734 cmn_err(CE_WARN
, "security restrictions disallow DTRACEIOC_MODUUIDSLIST");
18739 * Fail if the kernel symbol mode makes this operation illegal.
18740 * Both NEVER & ALWAYS_FROM_KERNEL are permanent states, it is legal to check
18741 * for them without holding the dtrace_lock.
18743 if (dtrace_kernel_symbol_mode
== DTRACE_KERNEL_SYMBOLS_NEVER
||
18744 dtrace_kernel_symbol_mode
== DTRACE_KERNEL_SYMBOLS_ALWAYS_FROM_KERNEL
) {
18745 cmn_err(CE_WARN
, "dtrace_kernel_symbol_mode of %u disallows DTRACEIOC_PROVMODSYMS", dtrace_kernel_symbol_mode
);
18750 * Read the number of module symbols structs being passed in.
18752 if (copyin(arg
+ offsetof(dtrace_module_symbols_t
, dtmodsyms_count
),
18754 sizeof(dtmodsyms_count
))) {
18755 cmn_err(CE_WARN
, "failed to copyin dtmodsyms_count");
18760 * Range check the count. How much data can we pass around?
18763 if (dtmodsyms_count
== 0) {
18764 cmn_err(CE_WARN
, "dtmodsyms_count is not valid");
18769 * Allocate a correctly sized structure and copyin the data.
18771 module_symbols_size
= DTRACE_MODULE_SYMBOLS_SIZE(dtmodsyms_count
);
18772 if (module_symbols_size
> (size_t)dtrace_copy_maxsize()) {
18773 size_t dtmodsyms_max
= DTRACE_MODULE_SYMBOLS_COUNT(dtrace_copy_maxsize());
18774 cmn_err(CE_WARN
, "dtmodsyms_count %ld is too high, maximum is %ld", dtmodsyms_count
, dtmodsyms_max
);
18778 if ((module_symbols
= kmem_alloc(module_symbols_size
, KM_SLEEP
)) == NULL
)
18783 /* NOTE! We can no longer exit this method via return */
18784 if (copyin(arg
, module_symbols
, module_symbols_size
) != 0) {
18785 cmn_err(CE_WARN
, "failed copyin of dtrace_module_symbols_t");
18787 goto module_symbols_cleanup
;
18791 * Check that the count didn't change between the first copyin and the second.
18793 if (module_symbols
->dtmodsyms_count
!= dtmodsyms_count
) {
18795 goto module_symbols_cleanup
;
18799 * Find the modctl to add symbols to.
18801 lck_mtx_lock(&dtrace_provider_lock
);
18802 lck_mtx_lock(&mod_lock
);
18804 struct modctl
* ctl
= dtrace_modctl_list
;
18806 ASSERT(!MOD_HAS_USERSPACE_SYMBOLS(ctl
));
18807 if (MOD_HAS_UUID(ctl
) && !MOD_SYMBOLS_DONE(ctl
) && memcmp(module_symbols
->dtmodsyms_uuid
, ctl
->mod_uuid
, sizeof(UUID
)) == 0) {
18808 dtrace_provider_t
*prv
;
18809 ctl
->mod_user_symbols
= module_symbols
;
18812 * We're going to call each providers per-module provide operation
18813 * specifying only this module.
18815 for (prv
= dtrace_provider
; prv
!= NULL
; prv
= prv
->dtpv_next
)
18816 prv
->dtpv_pops
.dtps_provide_module(prv
->dtpv_arg
, ctl
);
18818 * We gave every provider a chance to provide with the user syms, go ahead and clear them
18820 ctl
->mod_user_symbols
= NULL
; /* MUST reset this to clear HAS_USERSPACE_SYMBOLS */
18822 ctl
= ctl
->mod_next
;
18825 lck_mtx_unlock(&mod_lock
);
18826 lck_mtx_unlock(&dtrace_provider_lock
);
18828 module_symbols_cleanup
:
18830 * If we had to allocate struct memory, free it.
18832 if (module_symbols
!= NULL
) {
18833 kmem_free(module_symbols
, module_symbols_size
);
18839 case DTRACEIOC_PROCWAITFOR
: {
18840 dtrace_procdesc_t pdesc
= {
18845 if ((rval
= copyin(arg
, &pdesc
, sizeof(pdesc
))) != 0)
18846 goto proc_waitfor_error
;
18848 if ((rval
= dtrace_proc_waitfor(&pdesc
)) != 0)
18849 goto proc_waitfor_error
;
18851 if ((rval
= copyout(&pdesc
, arg
, sizeof(pdesc
))) != 0)
18852 goto proc_waitfor_error
;
18856 proc_waitfor_error
:
18857 /* The process was suspended, revert this since the client will not do it. */
18858 if (pdesc
.p_pid
!= -1) {
18859 proc_t
*proc
= proc_find(pdesc
.p_pid
);
18860 if (proc
!= PROC_NULL
) {
18861 task_pidresume(proc
->task
);
18877 * APPLE NOTE: dtrace_detach not implemented
18879 #if !defined(__APPLE__)
18882 dtrace_detach(dev_info_t
*dip
, ddi_detach_cmd_t cmd
)
18884 dtrace_state_t
*state
;
18891 return (DDI_SUCCESS
);
18894 return (DDI_FAILURE
);
18897 lck_mtx_lock(&cpu_lock
);
18898 lck_mtx_lock(&dtrace_provider_lock
);
18899 lck_mtx_lock(&dtrace_lock
);
18901 ASSERT(dtrace_opens
== 0);
18903 if (dtrace_helpers
> 0) {
18904 lck_mtx_unlock(&dtrace_lock
);
18905 lck_mtx_unlock(&dtrace_provider_lock
);
18906 lck_mtx_unlock(&cpu_lock
);
18907 return (DDI_FAILURE
);
18910 if (dtrace_unregister((dtrace_provider_id_t
)dtrace_provider
) != 0) {
18911 lck_mtx_unlock(&dtrace_lock
);
18912 lck_mtx_unlock(&dtrace_provider_lock
);
18913 lck_mtx_unlock(&cpu_lock
);
18914 return (DDI_FAILURE
);
18917 dtrace_provider
= NULL
;
18919 if ((state
= dtrace_anon_grab()) != NULL
) {
18921 * If there were ECBs on this state, the provider should
18922 * have not been allowed to detach; assert that there is
18925 ASSERT(state
->dts_necbs
== 0);
18926 dtrace_state_destroy(state
);
18929 * If we're being detached with anonymous state, we need to
18930 * indicate to the kernel debugger that DTrace is now inactive.
18932 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE
);
18935 bzero(&dtrace_anon
, sizeof (dtrace_anon_t
));
18936 unregister_cpu_setup_func((cpu_setup_func_t
*)dtrace_cpu_setup
, NULL
);
18937 dtrace_cpu_init
= NULL
;
18938 dtrace_helpers_cleanup
= NULL
;
18939 dtrace_helpers_fork
= NULL
;
18940 dtrace_cpustart_init
= NULL
;
18941 dtrace_cpustart_fini
= NULL
;
18942 dtrace_debugger_init
= NULL
;
18943 dtrace_debugger_fini
= NULL
;
18944 dtrace_kreloc_init
= NULL
;
18945 dtrace_kreloc_fini
= NULL
;
18946 dtrace_modload
= NULL
;
18947 dtrace_modunload
= NULL
;
18949 lck_mtx_unlock(&cpu_lock
);
18951 if (dtrace_helptrace_enabled
) {
18952 kmem_free(dtrace_helptrace_buffer
, dtrace_helptrace_bufsize
);
18953 dtrace_helptrace_buffer
= NULL
;
18956 kmem_free(dtrace_probes
, dtrace_nprobes
* sizeof (dtrace_probe_t
*));
18957 dtrace_probes
= NULL
;
18958 dtrace_nprobes
= 0;
18960 dtrace_hash_destroy(dtrace_strings
);
18961 dtrace_hash_destroy(dtrace_byprov
);
18962 dtrace_hash_destroy(dtrace_bymod
);
18963 dtrace_hash_destroy(dtrace_byfunc
);
18964 dtrace_hash_destroy(dtrace_byname
);
18965 dtrace_strings
= NULL
;
18966 dtrace_byprov
= NULL
;
18967 dtrace_bymod
= NULL
;
18968 dtrace_byfunc
= NULL
;
18969 dtrace_byname
= NULL
;
18971 kmem_cache_destroy(dtrace_state_cache
);
18972 vmem_destroy(dtrace_arena
);
18974 if (dtrace_toxrange
!= NULL
) {
18975 kmem_free(dtrace_toxrange
,
18976 dtrace_toxranges_max
* sizeof (dtrace_toxrange_t
));
18977 dtrace_toxrange
= NULL
;
18978 dtrace_toxranges
= 0;
18979 dtrace_toxranges_max
= 0;
18982 ddi_remove_minor_node(dtrace_devi
, NULL
);
18983 dtrace_devi
= NULL
;
18985 ddi_soft_state_fini(&dtrace_softstate
);
18987 ASSERT(dtrace_vtime_references
== 0);
18988 ASSERT(dtrace_opens
== 0);
18989 ASSERT(dtrace_retained
== NULL
);
18991 lck_mtx_unlock(&dtrace_lock
);
18992 lck_mtx_unlock(&dtrace_provider_lock
);
18996 * We don't destroy the task queue until after we have dropped our
18997 * locks (taskq_destroy() may block on running tasks). To prevent
18998 * attempting to do work after we have effectively detached but before
18999 * the task queue has been destroyed, all tasks dispatched via the
19000 * task queue must check that DTrace is still attached before
19001 * performing any operation.
19003 taskq_destroy(dtrace_taskq
);
19004 dtrace_taskq
= NULL
;
19007 return (DDI_SUCCESS
);
19009 #endif /* __APPLE__ */
19011 d_open_t _dtrace_open
, helper_open
;
19012 d_close_t _dtrace_close
, helper_close
;
19013 d_ioctl_t _dtrace_ioctl
, helper_ioctl
;
19016 _dtrace_open(dev_t dev
, int flags
, int devtype
, struct proc
*p
)
19019 dev_t locdev
= dev
;
19021 return dtrace_open( &locdev
, flags
, devtype
, CRED());
19025 helper_open(dev_t dev
, int flags
, int devtype
, struct proc
*p
)
19027 #pragma unused(dev,flags,devtype,p)
19032 _dtrace_close(dev_t dev
, int flags
, int devtype
, struct proc
*p
)
19035 return dtrace_close( dev
, flags
, devtype
, CRED());
19039 helper_close(dev_t dev
, int flags
, int devtype
, struct proc
*p
)
19041 #pragma unused(dev,flags,devtype,p)
19046 _dtrace_ioctl(dev_t dev
, u_long cmd
, caddr_t data
, int fflag
, struct proc
*p
)
19050 user_addr_t uaddrp
;
19052 if (proc_is64bit(p
))
19053 uaddrp
= *(user_addr_t
*)data
;
19055 uaddrp
= (user_addr_t
) *(uint32_t *)data
;
19057 err
= dtrace_ioctl(dev
, cmd
, uaddrp
, fflag
, CRED(), &rv
);
19059 /* Darwin's BSD ioctls only return -1 or zero. Overload errno to mimic Solaris. 20 bits suffice. */
19061 ASSERT( (err
& 0xfffff000) == 0 );
19062 return (err
& 0xfff); /* ioctl will return -1 and will set errno to an error code < 4096 */
19063 } else if (rv
!= 0) {
19064 ASSERT( (rv
& 0xfff00000) == 0 );
19065 return (((rv
& 0xfffff) << 12)); /* ioctl will return -1 and will set errno to a value >= 4096 */
19071 helper_ioctl(dev_t dev
, u_long cmd
, caddr_t data
, int fflag
, struct proc
*p
)
19073 #pragma unused(dev,fflag,p)
19076 err
= dtrace_ioctl_helper(cmd
, data
, &rv
);
19077 /* Darwin's BSD ioctls only return -1 or zero. Overload errno to mimic Solaris. 20 bits suffice. */
19079 ASSERT( (err
& 0xfffff000) == 0 );
19080 return (err
& 0xfff); /* ioctl will return -1 and will set errno to an error code < 4096 */
19081 } else if (rv
!= 0) {
19082 ASSERT( (rv
& 0xfff00000) == 0 );
19083 return (((rv
& 0xfffff) << 12)); /* ioctl will return -1 and will set errno to a value >= 4096 */
19088 #define HELPER_MAJOR -24 /* let the kernel pick the device number */
19090 const static struct cdevsw helper_cdevsw
=
19092 .d_open
= helper_open
,
19093 .d_close
= helper_close
,
19094 .d_read
= eno_rdwrt
,
19095 .d_write
= eno_rdwrt
,
19096 .d_ioctl
= helper_ioctl
,
19097 .d_stop
= (stop_fcn_t
*)nulldev
,
19098 .d_reset
= (reset_fcn_t
*)nulldev
,
19099 .d_select
= eno_select
,
19100 .d_mmap
= eno_mmap
,
19101 .d_strategy
= eno_strat
,
19102 .d_reserved_1
= eno_getc
,
19103 .d_reserved_2
= eno_putc
,
19106 static int helper_majdevno
= 0;
19108 static int gDTraceInited
= 0;
19111 helper_init( void )
19114 * Once the "helper" is initialized, it can take ioctl calls that use locks
19115 * and zones initialized in dtrace_init. Make certain dtrace_init was called
19119 if (!gDTraceInited
) {
19120 panic("helper_init before dtrace_init\n");
19123 if (0 >= helper_majdevno
)
19125 helper_majdevno
= cdevsw_add(HELPER_MAJOR
, &helper_cdevsw
);
19127 if (helper_majdevno
< 0) {
19128 printf("helper_init: failed to allocate a major number!\n");
19132 if (NULL
== devfs_make_node( makedev(helper_majdevno
, 0), DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0666,
19133 DTRACEMNR_HELPER
, 0 )) {
19134 printf("dtrace_init: failed to devfs_make_node for helper!\n");
19138 panic("helper_init: called twice!\n");
19141 #undef HELPER_MAJOR
19144 dtrace_clone_func(dev_t dev
, int action
)
19146 #pragma unused(dev)
19148 if (action
== DEVFS_CLONE_ALLOC
) {
19149 return dtrace_state_reserve();
19151 else if (action
== DEVFS_CLONE_FREE
) {
19157 void dtrace_ast(void);
19163 uint32_t clients
= os_atomic_xchg(&dtrace_wake_clients
, 0, relaxed
);
19167 * We disable preemption here to be sure that we won't get
19168 * interrupted by a wakeup to a thread that is higher
19169 * priority than us, so that we do issue all wakeups
19171 disable_preemption();
19172 for (i
= 0; i
< DTRACE_NCLIENTS
; i
++) {
19173 if (clients
& (1 << i
)) {
19174 dtrace_state_t
*state
= dtrace_state_get(i
);
19181 enable_preemption();
19185 #define DTRACE_MAJOR -24 /* let the kernel pick the device number */
19187 static const struct cdevsw dtrace_cdevsw
=
19189 .d_open
= _dtrace_open
,
19190 .d_close
= _dtrace_close
,
19191 .d_read
= eno_rdwrt
,
19192 .d_write
= eno_rdwrt
,
19193 .d_ioctl
= _dtrace_ioctl
,
19194 .d_stop
= (stop_fcn_t
*)nulldev
,
19195 .d_reset
= (reset_fcn_t
*)nulldev
,
19196 .d_select
= eno_select
,
19197 .d_mmap
= eno_mmap
,
19198 .d_strategy
= eno_strat
,
19199 .d_reserved_1
= eno_getc
,
19200 .d_reserved_2
= eno_putc
,
19203 lck_attr_t
* dtrace_lck_attr
;
19204 lck_grp_attr_t
* dtrace_lck_grp_attr
;
19205 lck_grp_t
* dtrace_lck_grp
;
19207 static int gMajDevNo
;
19209 void dtrace_early_init (void)
19211 dtrace_restriction_policy_load();
19214 * See dtrace_impl.h for a description of kernel symbol modes.
19215 * The default is to wait for symbols from userspace (lazy symbols).
19217 if (!PE_parse_boot_argn("dtrace_kernel_symbol_mode", &dtrace_kernel_symbol_mode
, sizeof (dtrace_kernel_symbol_mode
))) {
19218 dtrace_kernel_symbol_mode
= DTRACE_KERNEL_SYMBOLS_FROM_USERSPACE
;
19223 dtrace_init( void )
19225 if (0 == gDTraceInited
) {
19226 unsigned int i
, ncpu
;
19227 size_t size
= sizeof(dtrace_buffer_memory_maxsize
);
19230 * Disable destructive actions when dtrace is running
19231 * in a restricted environment
19233 dtrace_destructive_disallow
= dtrace_is_restricted() &&
19234 !dtrace_are_restrictions_relaxed();
19237 * DTrace allocates buffers based on the maximum number
19238 * of enabled cpus. This call avoids any race when finding
19241 ASSERT(dtrace_max_cpus
== 0);
19242 ncpu
= dtrace_max_cpus
= ml_wait_max_cpus();
19245 * Retrieve the size of the physical memory in order to define
19246 * the state buffer memory maximal size. If we cannot retrieve
19247 * this value, we'll consider that we have 1Gb of memory per CPU, that's
19248 * still better than raising a kernel panic.
19250 if (0 != kernel_sysctlbyname("hw.memsize", &dtrace_buffer_memory_maxsize
,
19253 dtrace_buffer_memory_maxsize
= ncpu
* 1024 * 1024 * 1024;
19254 printf("dtrace_init: failed to retrieve the hw.memsize, defaulted to %lld bytes\n",
19255 dtrace_buffer_memory_maxsize
);
19259 * Finally, divide by three to prevent DTrace from eating too
19262 dtrace_buffer_memory_maxsize
/= 3;
19263 ASSERT(dtrace_buffer_memory_maxsize
> 0);
19265 gMajDevNo
= cdevsw_add(DTRACE_MAJOR
, &dtrace_cdevsw
);
19267 if (gMajDevNo
< 0) {
19268 printf("dtrace_init: failed to allocate a major number!\n");
19273 if (NULL
== devfs_make_node_clone( makedev(gMajDevNo
, 0), DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0666,
19274 dtrace_clone_func
, DTRACEMNR_DTRACE
, 0 )) {
19275 printf("dtrace_init: failed to devfs_make_node_clone for dtrace!\n");
19281 * Create the dtrace lock group and attrs.
19283 dtrace_lck_attr
= lck_attr_alloc_init();
19284 dtrace_lck_grp_attr
= lck_grp_attr_alloc_init();
19285 dtrace_lck_grp
= lck_grp_alloc_init("dtrace", dtrace_lck_grp_attr
);
19288 * We have to initialize all locks explicitly
19290 lck_mtx_init(&dtrace_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
19291 lck_mtx_init(&dtrace_provider_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
19292 lck_mtx_init(&dtrace_meta_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
19293 lck_mtx_init(&dtrace_procwaitfor_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
19295 lck_mtx_init(&dtrace_errlock
, dtrace_lck_grp
, dtrace_lck_attr
);
19297 lck_rw_init(&dtrace_dof_mode_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
19300 * The cpu_core structure consists of per-CPU state available in any context.
19301 * On some architectures, this may mean that the page(s) containing the
19302 * NCPU-sized array of cpu_core structures must be locked in the TLB -- it
19303 * is up to the platform to assure that this is performed properly. Note that
19304 * the structure is sized to avoid false sharing.
19306 lck_mtx_init(&cpu_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
19307 lck_mtx_init(&cyc_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
19308 lck_mtx_init(&mod_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
19311 * Initialize the CPU offline/online hooks.
19313 dtrace_install_cpu_hooks();
19315 dtrace_modctl_list
= NULL
;
19317 cpu_core
= (cpu_core_t
*)kmem_zalloc( ncpu
* sizeof(cpu_core_t
), KM_SLEEP
);
19318 for (i
= 0; i
< ncpu
; ++i
) {
19319 lck_mtx_init(&cpu_core
[i
].cpuc_pid_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
19322 cpu_list
= (dtrace_cpu_t
*)kmem_zalloc( ncpu
* sizeof(dtrace_cpu_t
), KM_SLEEP
);
19323 for (i
= 0; i
< ncpu
; ++i
) {
19324 cpu_list
[i
].cpu_id
= (processorid_t
)i
;
19325 cpu_list
[i
].cpu_next
= &(cpu_list
[(i
+1) % ncpu
]);
19326 LIST_INIT(&cpu_list
[i
].cpu_cyc_list
);
19327 lck_rw_init(&cpu_list
[i
].cpu_ft_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
19330 lck_mtx_lock(&cpu_lock
);
19331 for (i
= 0; i
< ncpu
; ++i
)
19332 /* FIXME: track CPU configuration */
19333 dtrace_cpu_setup_initial( (processorid_t
)i
); /* In lieu of register_cpu_setup_func() callback */
19334 lck_mtx_unlock(&cpu_lock
);
19336 (void)dtrace_abs_to_nano(0LL); /* Force once only call to clock_timebase_info (which can take a lock) */
19338 dtrace_strings
= dtrace_hash_create(dtrace_strkey_offset
,
19339 offsetof(dtrace_string_t
, dtst_str
),
19340 offsetof(dtrace_string_t
, dtst_next
),
19341 offsetof(dtrace_string_t
, dtst_prev
));
19345 * See dtrace_impl.h for a description of dof modes.
19346 * The default is lazy dof.
19348 * FIXME: Warn if state is LAZY_OFF? It won't break anything, but
19349 * makes no sense...
19351 if (!PE_parse_boot_argn("dtrace_dof_mode", &dtrace_dof_mode
, sizeof (dtrace_dof_mode
))) {
19352 #if defined(XNU_TARGET_OS_OSX)
19353 dtrace_dof_mode
= DTRACE_DOF_MODE_LAZY_ON
;
19355 dtrace_dof_mode
= DTRACE_DOF_MODE_NEVER
;
19360 * Sanity check of dof mode value.
19362 switch (dtrace_dof_mode
) {
19363 case DTRACE_DOF_MODE_NEVER
:
19364 case DTRACE_DOF_MODE_LAZY_ON
:
19365 /* valid modes, but nothing else we need to do */
19368 case DTRACE_DOF_MODE_LAZY_OFF
:
19369 case DTRACE_DOF_MODE_NON_LAZY
:
19370 /* Cannot wait for a dtrace_open to init fasttrap */
19375 /* Invalid, clamp to non lazy */
19376 dtrace_dof_mode
= DTRACE_DOF_MODE_NON_LAZY
;
19382 if (dtrace_dof_mode
!= DTRACE_DOF_MODE_NEVER
)
19383 commpage_update_dof(true);
19389 panic("dtrace_init: called twice!\n");
19393 dtrace_postinit(void)
19396 * Called from bsd_init after all provider's *_init() routines have been
19397 * run. That way, anonymous DOF enabled under dtrace_attach() is safe
19400 dtrace_attach( (dev_info_t
*)(uintptr_t)makedev(gMajDevNo
, 0)); /* Punning a dev_t to a dev_info_t* */
19403 * Add the mach_kernel to the module list for lazy processing
19405 struct kmod_info fake_kernel_kmod
;
19406 memset(&fake_kernel_kmod
, 0, sizeof(fake_kernel_kmod
));
19408 strlcpy(fake_kernel_kmod
.name
, "mach_kernel", sizeof(fake_kernel_kmod
.name
));
19409 fake_kernel_kmod
.id
= 1;
19410 fake_kernel_kmod
.address
= g_kernel_kmod_info
.address
;
19411 fake_kernel_kmod
.size
= g_kernel_kmod_info
.size
;
19413 if (dtrace_module_loaded(&fake_kernel_kmod
, 0) != 0) {
19414 printf("dtrace_postinit: Could not register mach_kernel modctl\n");
19417 (void)OSKextRegisterKextsWithDTrace();
19419 #undef DTRACE_MAJOR
19422 * Routines used to register interest in cpu's being added to or removed
19426 register_cpu_setup_func(cpu_setup_func_t
*ignore1
, void *ignore2
)
19428 #pragma unused(ignore1,ignore2)
19432 unregister_cpu_setup_func(cpu_setup_func_t
*ignore1
, void *ignore2
)
19434 #pragma unused(ignore1,ignore2)