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