xref: /xnu-8020.121.3/bsd/sys/ktrace.h (revision fdd8201d7b966f0c3ea610489d29bd841d358941)
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 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);
51 
52 /*
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`.
55  */
56 #define KTRACE_KDEBUG (1 << 0)
57 #define KTRACE_KPERF  (1 << 1)
58 
59 /*
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.
63  *
64  * `ktrace_lock` must be held.
65  *
66  * Returns 0 if configuration is allowed, EPERM if process is not privileged,
67  * and EBUSY if ktrace is owned by another process.
68  */
69 int ktrace_configure(uint32_t config_mask);
70 
71 /*
72  * Tell ktrace to reset a configuration.  Pass the susbsystem(s) that are to
73  * be reset in the reset_mask.
74  *
75  * `ktrace_lock` must be held.
76  */
77 void ktrace_reset(uint32_t reset_mask);
78 
79 /*
80  * Determine if the current process can read the configuration of ktrace.
81  * Only the owning process or a root privileged process is allowed.
82  *
83  * `ktrace_lock` must be held.
84  *
85  * Returns 0 if allowed, EPERM otherwise.
86  */
87 int ktrace_read_check(void);
88 
89 /*
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).
94  *
95  * In these cases, ktrace must be aware of the state changes.  This function
96  * should be called whenever the kernel initiates configuring ktrace.
97  *
98  * `ktrace_lock` must be held.
99  */
100 void ktrace_kernel_configure(uint32_t config_mask);
101 
102 /*
103  * This KPI allows kernel systems to disable ktrace.  ktrace will only be
104  * disabled if the state matches the provided state_to_match.
105  *
106  * This does not reset the configuration of any subsystems -- it just makes
107  * them stop logging events or sampling data.
108  *
109  * `ktrace_lock` must be held.
110  */
111 void ktrace_disable(enum ktrace_state state_to_match);
112 
113 /*
114  * Returns the pid of the process that owns ktrace.  If ktrace is unowned,
115  * returns 0.
116  *
117  * `ktrace_lock` must be held.
118  */
119 int ktrace_get_owning_pid(void);
120 
121 /*
122  * Returns true if background tracing is active, false otherwise.
123  *
124  * `ktrace_lock` must be held.
125  */
126 bool ktrace_background_active(void);
127 
128 /*
129  * These functions exist for the transition for kperf to allow blessing other
130  * processes.  They should not be used by other clients.
131  */
132 extern bool ktrace_keep_ownership_on_reset;
133 extern int ktrace_root_set_owner_allowed;
134 int ktrace_set_owning_pid(int pid);
135 
136 /* Initialize ktrace.  Must only be called by the bootstrap thread. */
137 void ktrace_init(void);
138 
139 #endif /* SYS_KTRACE_H */
140