xref: /xnu-11215.1.10/bsd/sys/ktrace.h (revision 8d741a5de7ff4191bf97d57b9f54c2f6d4a15585)
1*8d741a5dSApple OSS Distributions /*
2*8d741a5dSApple OSS Distributions  * Copyright (c) 2015 Apple Inc. All rights reserved.
3*8d741a5dSApple OSS Distributions  *
4*8d741a5dSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*8d741a5dSApple OSS Distributions  *
6*8d741a5dSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*8d741a5dSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*8d741a5dSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*8d741a5dSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*8d741a5dSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*8d741a5dSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*8d741a5dSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*8d741a5dSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*8d741a5dSApple OSS Distributions  *
15*8d741a5dSApple OSS Distributions  * Please obtain a copy of the License at
16*8d741a5dSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*8d741a5dSApple OSS Distributions  *
18*8d741a5dSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*8d741a5dSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*8d741a5dSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*8d741a5dSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*8d741a5dSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*8d741a5dSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*8d741a5dSApple OSS Distributions  * limitations under the License.
25*8d741a5dSApple OSS Distributions  *
26*8d741a5dSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*8d741a5dSApple OSS Distributions  */
28*8d741a5dSApple OSS Distributions 
29*8d741a5dSApple OSS Distributions #ifndef SYS_KTRACE_H
30*8d741a5dSApple OSS Distributions #define SYS_KTRACE_H
31*8d741a5dSApple OSS Distributions 
32*8d741a5dSApple OSS Distributions #include <stdint.h>
33*8d741a5dSApple OSS Distributions #include <os/base.h>
34*8d741a5dSApple OSS Distributions #include <kern/locks.h>
35*8d741a5dSApple OSS Distributions 
36*8d741a5dSApple OSS Distributions __enum_decl(ktrace_state_t, unsigned int, {
37*8d741a5dSApple OSS Distributions 	/* No tool has configured ktrace. */
38*8d741a5dSApple OSS Distributions 	KTRACE_STATE_OFF = 0,
39*8d741a5dSApple OSS Distributions 	/* A foreground tool has configured ktrace. */
40*8d741a5dSApple OSS Distributions 	KTRACE_STATE_FG,
41*8d741a5dSApple OSS Distributions 	/* A background tool has configured ktrace. */
42*8d741a5dSApple OSS Distributions 	KTRACE_STATE_BG,
43*8d741a5dSApple OSS Distributions });
44*8d741a5dSApple OSS Distributions 
45*8d741a5dSApple OSS Distributions void ktrace_lock(void);
46*8d741a5dSApple OSS Distributions void ktrace_unlock(void);
47*8d741a5dSApple OSS Distributions void ktrace_assert_lock_held(void);
48*8d741a5dSApple OSS Distributions void ktrace_start_single_threaded(void);
49*8d741a5dSApple OSS Distributions void ktrace_end_single_threaded(void);
50*8d741a5dSApple OSS Distributions 
51*8d741a5dSApple OSS Distributions /*
52*8d741a5dSApple OSS Distributions  * Subsystems that use ktrace to manage ownership.  These values are passed as
53*8d741a5dSApple OSS Distributions  * part of the `*_mask` arguments in `ktrace_configure` and `ktrace_reset`.
54*8d741a5dSApple OSS Distributions  */
55*8d741a5dSApple OSS Distributions #define KTRACE_KDEBUG (1 << 0)
56*8d741a5dSApple OSS Distributions #define KTRACE_KPERF  (1 << 1)
57*8d741a5dSApple OSS Distributions 
58*8d741a5dSApple OSS Distributions /*
59*8d741a5dSApple OSS Distributions  * Used by subsystems to inform ktrace that a configuration is occurring.
60*8d741a5dSApple OSS Distributions  * Validates whether the current process has privileges to configure
61*8d741a5dSApple OSS Distributions  * ktrace.  Pass the subsystem(s) being configured in config_mask.
62*8d741a5dSApple OSS Distributions  *
63*8d741a5dSApple OSS Distributions  * `ktrace_lock` must be held.
64*8d741a5dSApple OSS Distributions  *
65*8d741a5dSApple OSS Distributions  * Returns 0 if configuration is allowed, EPERM if process is not privileged,
66*8d741a5dSApple OSS Distributions  * and EBUSY if ktrace is owned by another process.
67*8d741a5dSApple OSS Distributions  */
68*8d741a5dSApple OSS Distributions int ktrace_configure(uint32_t config_mask);
69*8d741a5dSApple OSS Distributions 
70*8d741a5dSApple OSS Distributions /*
71*8d741a5dSApple OSS Distributions  * Tell ktrace to reset a configuration.  Pass the susbsystem(s) that are to
72*8d741a5dSApple OSS Distributions  * be reset in the reset_mask.
73*8d741a5dSApple OSS Distributions  *
74*8d741a5dSApple OSS Distributions  * `ktrace_lock` must be held.
75*8d741a5dSApple OSS Distributions  */
76*8d741a5dSApple OSS Distributions void ktrace_reset(uint32_t reset_mask);
77*8d741a5dSApple OSS Distributions 
78*8d741a5dSApple OSS Distributions /*
79*8d741a5dSApple OSS Distributions  * Determine if the current process can read the configuration of ktrace.
80*8d741a5dSApple OSS Distributions  * Only the owning process or a root privileged process is allowed.
81*8d741a5dSApple OSS Distributions  *
82*8d741a5dSApple OSS Distributions  * `ktrace_lock` must be held.
83*8d741a5dSApple OSS Distributions  *
84*8d741a5dSApple OSS Distributions  * Returns 0 if allowed, EPERM otherwise.
85*8d741a5dSApple OSS Distributions  */
86*8d741a5dSApple OSS Distributions int ktrace_read_check(void);
87*8d741a5dSApple OSS Distributions 
88*8d741a5dSApple OSS Distributions /*
89*8d741a5dSApple OSS Distributions  * With certain boot-args, the kernel can start tracing without user space
90*8d741a5dSApple OSS Distributions  * intervention.  With `trace=<n_events>`, the kernel will start tracing at
91*8d741a5dSApple OSS Distributions  * boot.  With `trace_wake=<n_events>`, the kernel will start tracing on the
92*8d741a5dSApple OSS Distributions  * wake path out of hibernation (on Intel only).
93*8d741a5dSApple OSS Distributions  *
94*8d741a5dSApple OSS Distributions  * In these cases, ktrace must be aware of the state changes.  This function
95*8d741a5dSApple OSS Distributions  * should be called whenever the kernel initiates configuring ktrace.
96*8d741a5dSApple OSS Distributions  *
97*8d741a5dSApple OSS Distributions  * `ktrace_lock` must be held.
98*8d741a5dSApple OSS Distributions  */
99*8d741a5dSApple OSS Distributions void ktrace_kernel_configure(uint32_t config_mask);
100*8d741a5dSApple OSS Distributions 
101*8d741a5dSApple OSS Distributions /*
102*8d741a5dSApple OSS Distributions  * This KPI allows kernel systems to disable ktrace.  ktrace will only be
103*8d741a5dSApple OSS Distributions  * disabled if the state matches the provided state_to_match.
104*8d741a5dSApple OSS Distributions  *
105*8d741a5dSApple OSS Distributions  * This does not reset the configuration of any subsystems -- it just makes
106*8d741a5dSApple OSS Distributions  * them stop logging events or sampling data.
107*8d741a5dSApple OSS Distributions  *
108*8d741a5dSApple OSS Distributions  * `ktrace_lock` must be held.
109*8d741a5dSApple OSS Distributions  */
110*8d741a5dSApple OSS Distributions void ktrace_disable(ktrace_state_t state_to_match);
111*8d741a5dSApple OSS Distributions 
112*8d741a5dSApple OSS Distributions /*
113*8d741a5dSApple OSS Distributions  * Returns the pid of the process that owns ktrace.  If ktrace is unowned,
114*8d741a5dSApple OSS Distributions  * returns 0.
115*8d741a5dSApple OSS Distributions  *
116*8d741a5dSApple OSS Distributions  * `ktrace_lock` must be held.
117*8d741a5dSApple OSS Distributions  */
118*8d741a5dSApple OSS Distributions int ktrace_get_owning_pid(void);
119*8d741a5dSApple OSS Distributions 
120*8d741a5dSApple OSS Distributions /*
121*8d741a5dSApple OSS Distributions  * Returns true if background tracing is active, false otherwise.
122*8d741a5dSApple OSS Distributions  *
123*8d741a5dSApple OSS Distributions  * `ktrace_lock` must be held.
124*8d741a5dSApple OSS Distributions  */
125*8d741a5dSApple OSS Distributions bool ktrace_background_active(void);
126*8d741a5dSApple OSS Distributions 
127*8d741a5dSApple OSS Distributions /*
128*8d741a5dSApple OSS Distributions  * These functions exist for the transition for kperf to allow blessing other
129*8d741a5dSApple OSS Distributions  * processes.  They should not be used by other clients.
130*8d741a5dSApple OSS Distributions  */
131*8d741a5dSApple OSS Distributions extern bool ktrace_keep_ownership_on_reset;
132*8d741a5dSApple OSS Distributions extern int ktrace_root_set_owner_allowed;
133*8d741a5dSApple OSS Distributions int ktrace_set_owning_pid(int pid);
134*8d741a5dSApple OSS Distributions 
135*8d741a5dSApple OSS Distributions #endif /* SYS_KTRACE_H */
136