2 * Copyright (c) 2000-2018 Apple 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@
29 #ifndef BSD_SYS_KDEBUG_KERNEL_H
30 #define BSD_SYS_KDEBUG_KERNEL_H
32 #include <mach/boolean.h>
33 #include <mach/clock_types.h>
36 #include <sys/cdefs.h>
43 * To use kdebug in the kernel:
45 * #include <sys/kdebug_kernel.h>
47 * #define DBG_NETIPINIT NETDBG_CODE(DBG_NETIP, 1)
52 * KDBG(DBG_NETIPINIT | DBG_FUNC_START, 1, 2, 3, 4);
54 * KDBG(DBG_NETIPINIT);
56 * KDBG(DBG_NETIPINIT | DBG_FUNC_END);
60 #pragma mark - kernel tracepoints
63 * The KDBG{,_DEBUG,_RELEASE,_FILTERED} macros are the preferred method of
66 * Kernel pointers must be unslid or permuted using VM_KERNEL_UNSLIDE_OR_PERM.
67 * Do not trace any sensitive data.
71 * Traced on debug and development (and release macOS) kernels.
73 #define KDBG(x, ...) KDBG_(, x, ## __VA_ARGS__, 4, 3, 2, 1, 0)
76 * Traced on debug and development (and release macOS) kernels if explicitly
77 * requested. Omitted from tracing without a typefilter.
79 #define KDBG_FILTERED(x, ...) KDBG_(_FILTERED, x, ## __VA_ARGS__, 4, 3, 2, 1, 0)
84 * Traced on debug and development (and release macOS) kernels, even if the
85 * process filter would reject it.
87 #define KDBG_RELEASE_NOPROCFILT(x, ...) \
88 KDBG_(_RELEASE_NOPROCFILT, x, ## __VA_ARGS__, 4, 3, 2, 1, 0)
90 #endif /* KERNEL_PRIVATE */
93 * Traced on debug, development, and release kernels.
95 * Only use this tracepoint if the events are required for a shipping trace
98 #define KDBG_RELEASE(x, ...) KDBG_(_RELEASE, x, ## __VA_ARGS__, 4, 3, 2, 1, 0)
101 * Traced only on debug kernels.
103 #define KDBG_DEBUG(x, ...) KDBG_(_DEBUG, x, ## __VA_ARGS__, 4, 3, 2, 1, 0)
105 #pragma mark - kernel API
107 #ifdef KERNEL_PRIVATE
110 * kernel_debug_string provides the same functionality as the
111 * kdebug_trace_string syscall as a KPI. str_id is an in/out
112 * parameter that, if it's pointing to a string ID of 0, will
113 * receive a generated ID. If it provides a value in str_id,
114 * then that will be used, instead.
116 * Returns an errno indicating the type of failure.
118 int kernel_debug_string(uint32_t debugid
, uint64_t *str_id
, const char *str
);
121 * kernel_debug_disable disables event logging, but leaves any buffers
124 void kernel_debug_disable(void);
126 #endif /* KERNEL_PRIVATE */
129 * Returns true if kdebug is using continuous time for its events, and false
132 bool kdebug_using_continuous_time(void);
135 * Returns true if kdebug will log an event with the provided debugid, and
138 bool kdebug_debugid_enabled(uint32_t debugid
);
141 * Returns true only if the debugid is explicitly enabled by filters. Returns
142 * false otherwise, including when no filters are active.
144 bool kdebug_debugid_explicitly_enabled(uint32_t debugid
);
146 uint32_t kdebug_commpage_state(void);
148 #pragma mark - IOP tracing
151 * Definitions to support IOP tracing.
155 /* Trace is now enabled; no arguments. */
156 KD_CALLBACK_KDEBUG_ENABLED
,
157 /* Trace is now disabled; no arguments. */
158 KD_CALLBACK_KDEBUG_DISABLED
,
160 * Request the latest entries from the IOP and block until complete; no
163 KD_CALLBACK_SYNC_FLUSH
,
165 * The typefilter is enabled; a read-only pointer to the typefilter is
166 * provided, valid only while in the callback.
168 KD_CALLBACK_TYPEFILTER_CHANGED
,
171 typedef void (*kd_callback_fn
) (void *context
, kd_callback_type reason
,
177 /* name of IOP, NUL-terminated */
181 typedef struct kd_callback kd_callback_t
;
184 * Registers an IOP for participation in tracing.
186 * The registered callback function will be called with the
187 * supplied context as the first argument, followed by a
188 * kd_callback_type and an associated void* argument.
190 * The return value is a nonzero coreid that shall be used in
191 * kernel_debug_enter() to refer to your IOP. If the allocation
192 * failed, then 0 will be returned.
195 * Note that not all callback calls will indicate a change in
196 * state (e.g. disabling trace twice would send two disable
199 int kernel_debug_register_callback(kd_callback_t callback
);
201 void kernel_debug_enter(uint32_t coreid
, uint32_t debugid
, uint64_t timestamp
,
202 uintptr_t arg1
, uintptr_t arg2
, uintptr_t arg3
, uintptr_t arg4
,
205 #pragma mark - internals
207 #define KDBG_(f, x, a, b, c, d, n, ...) KDBG##n(f, x, a, b, c, d)
208 #define KDBG0(f, x, a, b, c, d) KERNEL_DEBUG_CONSTANT##f(x, 0, 0, 0, 0, 0)
209 #define KDBG1(f, x, a, b, c, d) KERNEL_DEBUG_CONSTANT##f(x, a, 0, 0, 0, 0)
210 #define KDBG2(f, x, a, b, c, d) KERNEL_DEBUG_CONSTANT##f(x, a, b, 0, 0, 0)
211 #define KDBG3(f, x, a, b, c, d) KERNEL_DEBUG_CONSTANT##f(x, a, b, c, 0, 0)
212 #define KDBG4(f, x, a, b, c, d) KERNEL_DEBUG_CONSTANT##f(x, a, b, c, d, 0)
214 #ifdef XNU_KERNEL_PRIVATE
215 #define KDBG_IMPROBABLE __improbable
217 #define KDBG_IMPROBABLE
220 extern unsigned int kdebug_enable
;
223 * The kernel debug configuration level. These values control which events are
224 * compiled in under different build configurations.
226 * Infer the supported kernel debug event level from config option. Use
227 * (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD) as a guard to protect unaudited debug
230 #define KDEBUG_LEVEL_NONE 0
231 #define KDEBUG_LEVEL_IST 1
232 #define KDEBUG_LEVEL_STANDARD 2
233 #define KDEBUG_LEVEL_FULL 3
236 #define KDEBUG_LEVEL KDEBUG_LEVEL_NONE
238 #define KDEBUG_LEVEL KDEBUG_LEVEL_IST
240 #define KDEBUG_LEVEL KDEBUG_LEVEL_FULL
242 #define KDEBUG_LEVEL KDEBUG_LEVEL_STANDARD
244 * Currently, all other kernel configurations (development, etc) build with
245 * KDEBUG_LEVEL_STANDARD.
250 * KERNEL_DEBUG_CONSTANT_FILTERED events are omitted from tracing unless they
251 * are explicitly requested in the typefilter. They are not emitted when
252 * tracing without a typefilter.
254 #if (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD)
255 #define KERNEL_DEBUG_CONSTANT_FILTERED(x, a, b, c, d, ...) \
257 if (KDBG_IMPROBABLE(kdebug_enable & ~KDEBUG_ENABLE_PPT)) { \
258 kernel_debug_filtered((x), (uintptr_t)(a), (uintptr_t)(b), \
259 (uintptr_t)(c), (uintptr_t)(d)); \
262 #else /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD) */
263 #define KERNEL_DEBUG_CONSTANT_FILTERED(type, x, a, b, c, d, ...) do {} while (0)
264 #endif /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD) */
266 #if (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST)
267 #define KERNEL_DEBUG_CONSTANT_RELEASE_NOPROCFILT(x, a, b, c, d, ...) \
269 if (KDBG_IMPROBABLE(kdebug_enable & ~KDEBUG_ENABLE_PPT)) { \
270 kernel_debug_flags((x), (uintptr_t)(a), (uintptr_t)(b), \
271 (uintptr_t)(c), (uintptr_t)(d), KDBG_FLAG_NOPROCFILT); \
274 #else /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */
275 #define KERNEL_DEBUG_CONSTANT_RELEASE_NOPROCFILT(x, a, b, c, d, ...) \
277 #endif /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */
280 #if (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD)
281 #define KERNEL_DEBUG_CONSTANT(x, a, b, c, d, e) \
283 if (KDBG_IMPROBABLE(kdebug_enable & ~KDEBUG_ENABLE_PPT)) { \
284 kernel_debug((x), (uintptr_t)(a), (uintptr_t)(b), (uintptr_t)(c), \
285 (uintptr_t)(d),(uintptr_t)(e)); \
290 * DO NOT USE THIS MACRO -- it breaks fundamental assumptions about ktrace and
291 * is only meant to be used by the pthread kext and other points in the kernel
292 * where the thread ID must be provided explicitly.
294 #define KERNEL_DEBUG_CONSTANT1(x, a, b, c, d, e) \
296 if (KDBG_IMPROBABLE(kdebug_enable & ~KDEBUG_ENABLE_PPT)) { \
297 kernel_debug1((x), (uintptr_t)(a), (uintptr_t)(b), (uintptr_t)(c), \
298 (uintptr_t)(d), (uintptr_t)(e)); \
302 #define KERNEL_DEBUG_EARLY(x, a, b, c, d) \
304 kernel_debug_early((uint32_t)(x), (uintptr_t)(a), (uintptr_t)(b), \
305 (uintptr_t)(c), (uintptr_t)(d)); \
307 #else /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD) */
308 #define KERNEL_DEBUG_CONSTANT(x, a, b, c, d, e) do {} while (0)
309 #define KERNEL_DEBUG_CONSTANT1(x, a, b, c, d, e) do {} while (0)
310 #define KERNEL_DEBUG_EARLY(x, a, b, c, d) do {} while (0)
311 #endif /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD) */
314 * KERNEL_DEBUG_CONSTANT_IST (in-system trace) events provide an audited subset
315 * of tracepoints for userland system tracing tools. This tracing level was
316 * created by 8857227 to protect fairplayd and other PT_DENY_ATTACH processes.
317 * It has two effects: only KERNEL_DEBUG_CONSTANT_IST() traces are emitted and
318 * any PT_DENY_ATTACH processes will only emit basic traces as defined by the
319 * kernel_debug_filter() routine.
321 #define KERNEL_DEBUG_CONSTANT_RELEASE(x, a, b, c, d, e) \
322 KERNEL_DEBUG_CONSTANT_IST(~KDEBUG_ENABLE_PPT, x, a, b, c, d, 0)
324 #if (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST)
325 #define KERNEL_DEBUG_CONSTANT_IST(type, x, a, b, c, d, e) \
327 if (KDBG_IMPROBABLE(kdebug_enable & (type))) { \
328 kernel_debug((x), (uintptr_t)(a), (uintptr_t)(b), (uintptr_t)(c), \
329 (uintptr_t)(d), 0); \
332 #define KERNEL_DEBUG_CONSTANT_IST1(x, a, b, c, d, e) \
334 if (KDBG_IMPROBABLE(kdebug_enable)) { \
335 kernel_debug1((x), (uintptr_t)(a), (uintptr_t)(b), (uintptr_t)(c), \
336 (uintptr_t)(d), (uintptr_t)(e)); \
339 #else /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */
340 #define KERNEL_DEBUG_CONSTANT_IST(type, x, a, b, c, d, e) do {} while (0)
341 #define KERNEL_DEBUG_CONSTANT_IST1(x, a, b, c, d, e) do {} while (0)
342 #endif /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */
345 #define __kdebug_constant_only __unused
349 * KERNEL_DEBUG events are only traced for DEBUG kernels.
351 #define KERNEL_DEBUG_CONSTANT_DEBUG(x, a, b, c, d, e) \
352 KERNEL_DEBUG(x, a, b, c, d, e)
354 #if (KDEBUG_LEVEL >= KDEBUG_LEVEL_FULL)
355 #define __kdebug_only
358 #define KERNEL_DEBUG(x, a, b, c, d, e) \
360 if (KDBG_IMPROBABLE(kdebug_enable & ~KDEBUG_ENABLE_PPT)) { \
361 kernel_debug((uint32_t)(x), (uintptr_t)(a), (uintptr_t)(b), \
362 (uintptr_t)(c), (uintptr_t)(d), (uintptr_t)(e)); \
367 * DO NOT USE THIS MACRO -- see warning above for KERNEL_DEBUG_CONSTANT1.
369 #define KERNEL_DEBUG1(x, a, b, c, d, e) \
371 if (KDBG_IMPROBABLE(kdebug_enable & ~KDEBUG_ENABLE_PPT)) { \
372 kernel_debug1((uint32_t)(x), (uintptr_t)(a), (uintptr_t)(b), \
373 (uintptr_t)(c), (uintptr_t)(d), (uintptr_t)(e)); \
377 #else /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_FULL) */
378 #define __kdebug_only __unused
381 #define KERNEL_DEBUG(x, a, b, c, d, e) do {} while (0)
382 #define KERNEL_DEBUG1(x, a, b, c, d, e) do {} while (0)
383 #endif /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_FULL) */
385 void kernel_debug(uint32_t debugid
, uintptr_t arg1
, uintptr_t arg2
,
386 uintptr_t arg3
, uintptr_t arg4
, uintptr_t arg5
);
388 void kernel_debug1(uint32_t debugid
, uintptr_t arg1
, uintptr_t arg2
,
389 uintptr_t arg3
, uintptr_t arg4
, uintptr_t arg5
);
391 #define KDBG_FLAG_FILTERED 0x01
392 #define KDBG_FLAG_NOPROCFILT 0x02
394 void kernel_debug_flags(uint32_t debugid
, uintptr_t arg1
, uintptr_t arg2
,
395 uintptr_t arg3
, uintptr_t arg4
, uint64_t flags
);
397 void kernel_debug_filtered(uint32_t debugid
, uintptr_t arg1
, uintptr_t arg2
,
398 uintptr_t arg3
, uintptr_t arg4
);
400 #pragma mark - xnu API
402 #ifdef XNU_KERNEL_PRIVATE
403 /* Used in early boot to log events. */
404 void kernel_debug_early(uint32_t debugid
, uintptr_t arg1
, uintptr_t arg2
,
405 uintptr_t arg3
, uintptr_t arg4
);
406 /* Used in early boot to log strings spanning only a single tracepoint. */
407 void kernel_debug_string_early(const char *message
);
408 /* Used to trace strings within kdebug tracepoints on arbitrary eventids. */
409 void kernel_debug_string_simple(uint32_t eventid
, const char *str
);
410 /* Only used by ktrace to reset kdebug. ktrace_lock must be held. */
411 extern void kdebug_reset(void);
413 void kdbg_dump_trace_to_file(const char *);
414 void kdebug_init(unsigned int n_events
, char *filterdesc
, bool wrapping
);
415 void kdebug_trace_start(unsigned int n_events
, const char *filterdesc
,
416 bool wrapping
, bool at_wake
);
417 void kdebug_free_early_buf(void);
418 void release_storage_unit(int cpu
, uint32_t storage_unit
);
419 bool allocate_storage_unit(int cpu
);
422 void kdbg_trace_data(struct proc
*proc
, long *arg_pid
, long *arg_uniqueid
);
423 void kdbg_trace_string(struct proc
*proc
, long *arg1
, long *arg2
, long *arg3
,
426 #define KDBG_VFS_LOOKUP_FLAG_LOOKUP 0x01
427 #define KDBG_VFS_LOOKUP_FLAG_NOPROCFILT 0x02
428 void kdebug_vfs_lookup(long *dbg_parms
, int dbg_namelen
, void *dp
,
431 #endif /* XNU_KERNEL_PRIVATE */
433 #ifdef KERNEL_PRIVATE
436 void kdebug_lookup_gen_events(long *dbg_parms
, int dbg_namelen
, void *dp
,
439 #pragma mark - EnergyTracing
441 #define KERNEL_DBG_IST_SANE KDBG_RELEASE
442 #define ENTR_KDTRACEFUNC KDBG_RELEASE
444 // value is int64_t, quality is uint32_t
445 #define KERNEL_ENERGYTRACE(opcode, lifespan, id, quality, value) \
446 ENTR_KDTRACE(kEnTrCompKernel, opcode, lifespan, id, \
448 #define KERNEL_ENTR_ASSOCIATE(par_opcode, par_act_id, sub_opcode, sub_act_id) \
449 ENTR_KDASSOCIATE(kEnTrCompKernel, par_opcode, par_act_id, \
450 kEnTrCompKernel, sub_opcode, sub_act_id)
452 #endif /* KERNEL_PRIVATE */
458 #endif /* !defined(BSD_SYS_KDEBUG_KERNEL_H) */