]> git.saurik.com Git - apple/xnu.git/blob - bsd/sys/ktrace.h
xnu-3789.1.32.tar.gz
[apple/xnu.git] / bsd / sys / ktrace.h
1 /*
2 * Copyright (c) 2015 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #ifndef SYS_KTRACE_H
30 #define SYS_KTRACE_H
31
32 #include <stdint.h>
33
34 #include <kern/locks.h>
35
36 /* The states that ktrace can be in. */
37 enum ktrace_state {
38 /* No tool has configured ktrace. */
39 KTRACE_STATE_OFF = 0,
40 /* A foreground tool has configured ktrace. */
41 KTRACE_STATE_FG,
42 /* A background tool has configured ktrace. */
43 KTRACE_STATE_BG
44 };
45
46 extern lck_mtx_t *ktrace_lock;
47
48 /*
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`.
51 */
52 #define KTRACE_KDEBUG (1 << 0)
53 #define KTRACE_KPERF (1 << 1)
54
55 /*
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.
59 *
60 * `ktrace_lock` must be held.
61 *
62 * Returns 0 if configuration is allowed, EPERM if process is not privileged,
63 * and EBUSY if ktrace is owned by another process.
64 */
65 int ktrace_configure(uint32_t config_mask);
66
67 /*
68 * Tell ktrace to reset a configuration. Pass the susbsystem(s) that are to
69 * be reset in the reset_mask.
70 *
71 * `ktrace_lock` must be held.
72 */
73 void ktrace_reset(uint32_t reset_mask);
74
75 /*
76 * Determine if the current process can read the configuration of ktrace.
77 * Only the owning process or a root privileged process is allowed.
78 *
79 * `ktrace_lock` must be held.
80 *
81 * Returns 0 if allowed, EPERM otherwise.
82 */
83 int ktrace_read_check(void);
84
85 /*
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).
90 *
91 * In these cases, ktrace must be aware of the state changes. This function
92 * should be called whenever the kernel initiates configuring ktrace.
93 *
94 * `ktrace_lock` must be held.
95 */
96 void ktrace_kernel_configure(uint32_t config_mask);
97
98 /*
99 * This KPI allows kernel systems to disable ktrace. ktrace will only be
100 * disabled if the state matches the provided state_to_match.
101 *
102 * This does not reset the configuration of any subsystems -- it just makes
103 * them stop logging events or sampling data.
104 *
105 * `ktrace_lock` must be held.
106 */
107 void ktrace_disable(enum ktrace_state state_to_match);
108
109 /*
110 * Returns the pid of the process that owns ktrace. If ktrace is unowned,
111 * returns 0.
112 *
113 * `ktrace_lock` must be held.
114 */
115 int ktrace_get_owning_pid(void);
116
117 /*
118 * Returns true if background tracing is active, false otherwise.
119 *
120 * `ktrace_lock` must be held.
121 */
122 bool ktrace_background_active(void);
123
124 /*
125 * These functions exist for the transition for kperf to allow blessing other
126 * processes. They should not be used by other clients.
127 */
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);
131
132 /* Initialize ktrace. Must only be called by the bootstrap thread. */
133 void ktrace_init(void);
134
135 #endif /* SYS_KTRACE_H */