2 * Copyright (c) 2016 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * kperf's kdebug trigger is a precise mechanism for taking samples of the
31 * thread tracing a kdebug event.
33 * The filter used by kperf differs from kdebug's typefilter. kperf's filter
34 * is small -- only around 140 bytes, as opposed to kdebug's 8KB filter. It
35 * can also target precise debug IDs, instead of only being able to specify
36 * an entire subclass in a kdebug typefilter. Function specifiers can be
37 * provided to match against along with a class or subclass. For instance, this
38 * allows the kperf filter to only trigger a sample if an ending syscall event
39 * (DBG_BSD, DBG_BSD_EXCP_SC) occurs.
41 * The tradeoff for this flexibility is that only KPERF_KDEBUG_DEBUGIDS_MAX (32)
42 * classes, subclasses, or exact debug IDs can be filtered at one time.
44 * The filter consists of up to 32 debug IDs and an array of 2-bit type codes
45 * packed into a 64-bit value. To determine if a given debug ID should trigger
46 * a kperf sample, each debug ID is checked. The type code is unpacked from the
47 * 64-bit value to apply a mask to the debug ID. Then, a sample occurs if the
48 * masked debug ID is equal to the debug ID in the filter's list.
51 #include <kern/kalloc.h>
52 #include <kperf/action.h>
53 #include <kperf/buffer.h>
54 #include <kperf/context.h>
55 #include <kperf/kdebug_trigger.h>
56 #include <kperf/kperf.h>
57 #include <sys/errno.h>
59 boolean_t kperf_kdebug_active
= FALSE
;
60 static void kperf_kdebug_update(void);
62 static uint8_t kperf_kdebug_action
= 0;
64 static struct kperf_kdebug_filter
{
66 uint32_t debugids
[KPERF_KDEBUG_DEBUGIDS_MAX
];
68 } __attribute__((packed
)) *kperf_kdebug_filter
= NULL
;
70 enum kperf_kdebug_filter_type
{
71 KPERF_KDEBUG_FILTER_CLASS
,
72 KPERF_KDEBUG_FILTER_CLASS_FN
,
73 KPERF_KDEBUG_FILTER_CSC
,
74 KPERF_KDEBUG_FILTER_CSC_FN
,
75 KPERF_KDEBUG_FILTER_DEBUGID
,
76 KPERF_KDEBUG_FILTER_DEBUGID_FN
79 const static uint32_t debugid_masks
[] = {
80 [KPERF_KDEBUG_FILTER_CLASS
] = KDBG_CLASS_MASK
,
81 [KPERF_KDEBUG_FILTER_CLASS_FN
] = KDBG_CLASS_MASK
| KDBG_FUNC_MASK
,
82 [KPERF_KDEBUG_FILTER_CSC
] = KDBG_CSC_MASK
,
83 [KPERF_KDEBUG_FILTER_CSC_FN
] = KDBG_CSC_MASK
| KDBG_FUNC_MASK
,
84 [KPERF_KDEBUG_FILTER_DEBUGID
] = KDBG_EVENTID_MASK
,
85 [KPERF_KDEBUG_FILTER_DEBUGID_FN
] = UINT32_MAX
,
89 * Types are packed into 2 64-bit fields in the filter, with 4-bits for each
90 * type. Only 3 bits are strictly necessary, but using 4 simplifies the
95 #define DECODE_TYPE(TYPES, I) ((((uint8_t *)(TYPES))[(I) / 2] >> ((I) % 2) * 4) & 0xf)
98 kperf_kdebug_init(void)
100 kperf_kdebug_filter
= kalloc_tag(sizeof(*kperf_kdebug_filter
),
101 VM_KERN_MEMORY_DIAG
);
102 if (kperf_kdebug_filter
== NULL
) {
105 bzero(kperf_kdebug_filter
, sizeof(*kperf_kdebug_filter
));
111 kperf_kdebug_reset(void)
115 if ((err
= kperf_init())) {
119 kperf_kdebug_action
= 0;
120 bzero(kperf_kdebug_filter
, sizeof(*kperf_kdebug_filter
));
121 kperf_kdebug_update();
125 kperf_kdebug_should_trigger(uint32_t debugid
)
127 /* ignore kperf events */
128 if (KDBG_EXTRACT_CLASS(debugid
) == DBG_PERF
) {
133 * Search linearly through list of debugids and masks. If the filter
134 * gets larger than 128 bytes, change this to either a binary search or
135 * a sparse bitmap on the uint32_t range, depending on the new size.
137 for (uint8_t i
= 0; i
< kperf_kdebug_filter
->n_debugids
; i
++) {
138 uint32_t check_debugid
=
139 kperf_kdebug_filter
->debugids
[i
];
140 uint32_t mask
= debugid_masks
[DECODE_TYPE(kperf_kdebug_filter
->types
, i
)];
142 if ((debugid
& mask
) == check_debugid
) {
151 kperf_kdebug_set_filter(user_addr_t user_filter
, uint32_t user_size
)
153 uint32_t n_debugids_provided
= 0;
156 if ((err
= kperf_init())) {
160 n_debugids_provided
= (uint32_t)KPERF_KDEBUG_N_DEBUGIDS(user_size
);
162 /* detect disabling the filter completely */
163 if (n_debugids_provided
== 0) {
164 bzero(kperf_kdebug_filter
, sizeof(*kperf_kdebug_filter
));
168 if ((err
= kperf_kdebug_set_n_debugids(n_debugids_provided
))) {
172 if ((err
= copyin(user_filter
, (char *)kperf_kdebug_filter
,
173 KPERF_KDEBUG_FILTER_SIZE(n_debugids_provided
))))
175 bzero(kperf_kdebug_filter
, sizeof(*kperf_kdebug_filter
));
180 kperf_kdebug_update();
186 kperf_kdebug_get_filter(struct kperf_kdebug_filter
**filter
)
190 if ((err
= kperf_init())) {
194 assert(filter
!= NULL
);
196 *filter
= kperf_kdebug_filter
;
197 return kperf_kdebug_filter
->n_debugids
;
201 kperf_kdebug_set_n_debugids(uint32_t n_debugids_in
)
205 if ((err
= kperf_init())) {
209 if (n_debugids_in
> KPERF_KDEBUG_DEBUGIDS_MAX
) {
213 kperf_kdebug_filter
->n_debugids
= n_debugids_in
;
219 kperf_kdebug_set_action(int action_id
)
221 if (action_id
< 0 || (unsigned int)action_id
> kperf_action_get_count()) {
225 kperf_kdebug_action
= action_id
;
226 kperf_kdebug_update();
232 kperf_kdebug_get_action(void)
234 return kperf_kdebug_action
;
238 kperf_kdebug_update(void)
242 if ((err
= kperf_init())) {
246 if (kperf_kdebug_action
!= 0 &&
247 kperf_kdebug_filter
->n_debugids
!= 0)
249 kperf_kdebug_active
= TRUE
;
251 kperf_kdebug_active
= FALSE
;