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 void ktrace_lock(void);
47 void ktrace_unlock(void);
48 void ktrace_assert_lock_held(void);
49 void ktrace_start_single_threaded(void);
50 void ktrace_end_single_threaded(void);
53 * Subsystems that use ktrace to manage ownership. These values are passed as
54 * part of the `*_mask` arguments in `ktrace_configure` and `ktrace_reset`.
56 #define KTRACE_KDEBUG (1 << 0)
57 #define KTRACE_KPERF (1 << 1)
60 * Used by subsystems to inform ktrace that a configuration is occurring.
61 * Validates whether the current process has privileges to configure
62 * ktrace. Pass the subsystem(s) being configured in config_mask.
64 * `ktrace_lock` must be held.
66 * Returns 0 if configuration is allowed, EPERM if process is not privileged,
67 * and EBUSY if ktrace is owned by another process.
69 int ktrace_configure(uint32_t config_mask
);
72 * Tell ktrace to reset a configuration. Pass the susbsystem(s) that are to
73 * be reset in the reset_mask.
75 * `ktrace_lock` must be held.
77 void ktrace_reset(uint32_t reset_mask
);
80 * Determine if the current process can read the configuration of ktrace.
81 * Only the owning process or a root privileged process is allowed.
83 * `ktrace_lock` must be held.
85 * Returns 0 if allowed, EPERM otherwise.
87 int ktrace_read_check(void);
90 * With certain boot-args, the kernel can start tracing without user space
91 * intervention. With `trace=<n_events>`, the kernel will start tracing at
92 * boot. With `trace_wake=<n_events>`, the kernel will start tracing on the
93 * wake path out of hibernation (on Intel only).
95 * In these cases, ktrace must be aware of the state changes. This function
96 * should be called whenever the kernel initiates configuring ktrace.
98 * `ktrace_lock` must be held.
100 void ktrace_kernel_configure(uint32_t config_mask
);
103 * This KPI allows kernel systems to disable ktrace. ktrace will only be
104 * disabled if the state matches the provided state_to_match.
106 * This does not reset the configuration of any subsystems -- it just makes
107 * them stop logging events or sampling data.
109 * `ktrace_lock` must be held.
111 void ktrace_disable(enum ktrace_state state_to_match
);
114 * Returns the pid of the process that owns ktrace. If ktrace is unowned,
117 * `ktrace_lock` must be held.
119 int ktrace_get_owning_pid(void);
122 * Returns true if background tracing is active, false otherwise.
124 * `ktrace_lock` must be held.
126 bool ktrace_background_active(void);
129 * These functions exist for the transition for kperf to allow blessing other
130 * processes. They should not be used by other clients.
132 extern bool ktrace_keep_ownership_on_reset
;
133 extern int ktrace_root_set_owner_allowed
;
134 int ktrace_set_owning_pid(int pid
);
136 /* Initialize ktrace. Must only be called by the bootstrap thread. */
137 void ktrace_init(void);
139 #endif /* SYS_KTRACE_H */