2 * Copyright (c) 2015 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@
34 #include <kern/locks.h>
36 /* The states that ktrace can be in. */
38 /* No tool has configured ktrace. */
40 /* A foreground tool has configured ktrace. */
42 /* A background tool has configured ktrace. */
46 extern lck_mtx_t
*ktrace_lock
;
49 * Subsystems that use ktrace to manage ownership. These values are passed as
50 * part of the `*_mask` arguments in `ktrace_configure` and `ktrace_reset`.
52 #define KTRACE_KDEBUG (1 << 0)
53 #define KTRACE_KPERF (1 << 1)
56 * Used by subsystems to inform ktrace that a configuration is occurring.
57 * Validates whether the current process has privileges to configure
58 * ktrace. Pass the subsystem(s) being configured in config_mask.
60 * `ktrace_lock` must be held.
62 * Returns 0 if configuration is allowed, EPERM if process is not privileged,
63 * and EBUSY if ktrace is owned by another process.
65 int ktrace_configure(uint32_t config_mask
);
68 * Tell ktrace to reset a configuration. Pass the susbsystem(s) that are to
69 * be reset in the reset_mask.
71 * `ktrace_lock` must be held.
73 void ktrace_reset(uint32_t reset_mask
);
76 * Determine if the current process can read the configuration of ktrace.
77 * Only the owning process or a root privileged process is allowed.
79 * `ktrace_lock` must be held.
81 * Returns 0 if allowed, EPERM otherwise.
83 int ktrace_read_check(void);
86 * With certain boot-args, the kernel can start tracing without user space
87 * intervention. With `trace=<n_events>`, the kernel will start tracing at
88 * boot. With `trace_wake=<n_events>`, the kernel will start tracing on the
89 * wake path out of hibernation (on Intel only).
91 * In these cases, ktrace must be aware of the state changes. This function
92 * should be called whenever the kernel initiates configuring ktrace.
94 * `ktrace_lock` must be held.
96 void ktrace_kernel_configure(uint32_t config_mask
);
99 * This KPI allows kernel systems to disable ktrace. ktrace will only be
100 * disabled if the state matches the provided state_to_match.
102 * This does not reset the configuration of any subsystems -- it just makes
103 * them stop logging events or sampling data.
105 * `ktrace_lock` must be held.
107 void ktrace_disable(enum ktrace_state state_to_match
);
110 * Returns the pid of the process that owns ktrace. If ktrace is unowned,
113 * `ktrace_lock` must be held.
115 int ktrace_get_owning_pid(void);
118 * Returns true if background tracing is active, false otherwise.
120 * `ktrace_lock` must be held.
122 bool ktrace_background_active(void);
125 * These functions exist for the transition for kperf to allow blessing other
126 * processes. They should not be used by other clients.
128 extern boolean_t ktrace_keep_ownership_on_reset
;
129 extern int ktrace_root_set_owner_allowed
;
130 int ktrace_set_owning_pid(int pid
);
132 /* Initialize ktrace. Must only be called by the bootstrap thread. */
133 void ktrace_init(void);
135 #endif /* SYS_KTRACE_H */