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_setup(void)
100 kperf_kdebug_filter
= kalloc_tag(sizeof(*kperf_kdebug_filter
),
101 VM_KERN_MEMORY_DIAG
);
102 bzero(kperf_kdebug_filter
, sizeof(*kperf_kdebug_filter
));
106 kperf_kdebug_reset(void)
110 kperf_kdebug_action
= 0;
111 bzero(kperf_kdebug_filter
, sizeof(*kperf_kdebug_filter
));
112 kperf_kdebug_update();
116 kperf_kdebug_should_trigger(uint32_t debugid
)
118 /* ignore kperf events */
119 if (KDBG_EXTRACT_CLASS(debugid
) == DBG_PERF
) {
124 * Search linearly through list of debugids and masks. If the filter
125 * gets larger than 128 bytes, change this to either a binary search or
126 * a sparse bitmap on the uint32_t range, depending on the new size.
128 for (uint8_t i
= 0; i
< kperf_kdebug_filter
->n_debugids
; i
++) {
129 uint32_t check_debugid
=
130 kperf_kdebug_filter
->debugids
[i
];
131 uint32_t mask
= debugid_masks
[DECODE_TYPE(kperf_kdebug_filter
->types
, i
)];
133 if ((debugid
& mask
) == check_debugid
) {
142 kperf_kdebug_set_filter(user_addr_t user_filter
, uint32_t user_size
)
144 uint32_t n_debugids_provided
= 0;
149 n_debugids_provided
= (uint32_t)KPERF_KDEBUG_N_DEBUGIDS(user_size
);
151 /* detect disabling the filter completely */
152 if (n_debugids_provided
== 0) {
153 bzero(kperf_kdebug_filter
, sizeof(*kperf_kdebug_filter
));
157 if ((err
= kperf_kdebug_set_n_debugids(n_debugids_provided
))) {
161 if ((err
= copyin(user_filter
, (char *)kperf_kdebug_filter
,
162 KPERF_KDEBUG_FILTER_SIZE(n_debugids_provided
)))) {
163 bzero(kperf_kdebug_filter
, sizeof(*kperf_kdebug_filter
));
168 kperf_kdebug_update();
174 kperf_kdebug_get_filter(struct kperf_kdebug_filter
**filter
)
178 assert(filter
!= NULL
);
180 *filter
= kperf_kdebug_filter
;
181 return kperf_kdebug_filter
->n_debugids
;
185 kperf_kdebug_set_n_debugids(uint32_t n_debugids_in
)
189 if (n_debugids_in
> KPERF_KDEBUG_DEBUGIDS_MAX
) {
193 kperf_kdebug_filter
->n_debugids
= n_debugids_in
;
199 kperf_kdebug_set_action(int action_id
)
201 if (action_id
< 0 || (unsigned int)action_id
> kperf_action_get_count()) {
205 kperf_kdebug_action
= action_id
;
206 kperf_kdebug_update();
212 kperf_kdebug_get_action(void)
214 return kperf_kdebug_action
;
218 kperf_kdebug_update(void)
222 if (kperf_kdebug_action
!= 0 &&
223 kperf_kdebug_filter
->n_debugids
!= 0) {
224 kperf_kdebug_active
= TRUE
;
226 kperf_kdebug_active
= FALSE
;