|
| 1 | +// |
| 2 | +// kdebug_Private.h |
| 3 | +// COpenSwiftUICore |
| 4 | +// |
| 5 | +// Audited for RELEASE_2024 |
| 6 | +// Status: Complete |
| 7 | + |
| 8 | +#ifndef kdebug_Private_h |
| 9 | +#define kdebug_Private_h |
| 10 | + |
| 11 | +#include "OpenSwiftUIBase.h" |
| 12 | + |
| 13 | +#if OPENSWIFTUI_TARGET_OS_DARWIN |
| 14 | + |
| 15 | +/* |
| 16 | + * Copyright (c) 2000-2018 Apple Inc. All rights reserved. |
| 17 | + * |
| 18 | + * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
| 19 | + * |
| 20 | + * This file contains Original Code and/or Modifications of Original Code |
| 21 | + * as defined in and that are subject to the Apple Public Source License |
| 22 | + * Version 2.0 (the 'License'). You may not use this file except in |
| 23 | + * compliance with the License. The rights granted to you under the License |
| 24 | + * may not be used to create, or enable the creation or redistribution of, |
| 25 | + * unlawful or unlicensed copies of an Apple operating system, or to |
| 26 | + * circumvent, violate, or enable the circumvention or violation of, any |
| 27 | + * terms of an Apple operating system software license agreement. |
| 28 | + * |
| 29 | + * Please obtain a copy of the License at |
| 30 | + * http://www.opensource.apple.com/apsl/ and read it before using this file. |
| 31 | + * |
| 32 | + * The Original Code and all software distributed under the License are |
| 33 | + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER |
| 34 | + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
| 35 | + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, |
| 36 | + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
| 37 | + * Please see the License for the specific language governing rights and |
| 38 | + * limitations under the License. |
| 39 | + * |
| 40 | + * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
| 41 | + */ |
| 42 | + |
| 43 | +#include <stdint.h> |
| 44 | +#include <stdbool.h> |
| 45 | +#include <sys/cdefs.h> |
| 46 | + |
| 47 | +#if OPENSWIFTUI_TARGET_OS_OSX |
| 48 | +#include <sys/kdebug.h> |
| 49 | +#endif |
| 50 | + |
| 51 | +#include <Availability.h> |
| 52 | + |
| 53 | +#pragma mark - user space SPI |
| 54 | + |
| 55 | +/* |
| 56 | +* OS components can use the full precision of the "code" field |
| 57 | +* (Class, SubClass, Code) to inject events using kdebug_trace() by |
| 58 | +* using: |
| 59 | +* |
| 60 | +* kdebug_trace(KDBG_CODE(DBG_XPC, 15, 1) | DBG_FUNC_NONE, 1, 2, 3, 4); |
| 61 | +* |
| 62 | +* These trace points can be included in production code, since they |
| 63 | +* use reserved, non-overlapping ranges. The performance impact when |
| 64 | +* kernel tracing is not enabled is minimal. However, when tracing is enabled, |
| 65 | +* each tracepoint becomes a syscall. For this reason, os_signpost(3) is |
| 66 | +* recommended instead of kdebug_trace(2). |
| 67 | +* |
| 68 | +* Classes can be reserved by filing a Radar in xnu | ktrace. |
| 69 | +* |
| 70 | +* 64-bit arguments may be truncated if the system is using a 32-bit |
| 71 | +* kernel. |
| 72 | +* |
| 73 | +* On error, -1 will be returned and errno will indicate the error. |
| 74 | +*/ |
| 75 | +OPENSWIFTUI_EXPORT int kdebug_trace(uint32_t code, uint64_t arg1, uint64_t arg2, uint64_t arg3, |
| 76 | + uint64_t arg4) |
| 77 | +__OSX_AVAILABLE(10.10) __IOS_AVAILABLE(8.2); |
| 78 | + |
| 79 | +/*! |
| 80 | +* @function kdebug_trace_string |
| 81 | +* |
| 82 | +* @discussion |
| 83 | +* This function emits strings to kdebug trace along with an ID and allows |
| 84 | +* for previously-traced strings to be overwritten and invalidated. |
| 85 | +* |
| 86 | +* To start tracing a string and generate an ID to use to refer to it: |
| 87 | +* |
| 88 | +* string_id = kdebug_trace_string(debugid, 0, "string"); |
| 89 | +* |
| 90 | +* To replace a string previously traced: |
| 91 | +* |
| 92 | +* string_id = kdebug_trace_string(debugid, string_id, "new string"); |
| 93 | +* |
| 94 | +* To invalidate a string ID: |
| 95 | +* |
| 96 | +* string_id = kdebug_trace_string(debugid, string_id, NULL); |
| 97 | +* |
| 98 | +* To check for errors: |
| 99 | +* |
| 100 | +* if ((int64_t)string_id == -1) { perror("string error") } |
| 101 | +* |
| 102 | +* @param debugid |
| 103 | +* The `debugid` to check if its enabled before tracing and include as |
| 104 | +* an argument in the event containing the string. |
| 105 | +* |
| 106 | +* Some classes or subclasses are reserved for specific uses and are not |
| 107 | +* allowed to be used with this function. No function qualifiers are |
| 108 | +* allowed on `debugid`. |
| 109 | +* |
| 110 | +* @param str_id |
| 111 | +* When 0, a new ID will be generated and returned if tracing is |
| 112 | +* enabled. |
| 113 | +* |
| 114 | +* Otherwise `str_id` must contain an ID that was previously generated |
| 115 | +* with this function. Clents should pass NULL in `str` if `str_id` |
| 116 | +* is no longer in use. Otherwise, the string previously mapped to |
| 117 | +* `str_id` will be overwritten with the contents of `str`. |
| 118 | +* |
| 119 | +* @param str |
| 120 | +* A NUL-terminated 'C' string containing the characters that should be |
| 121 | +* traced alongside `str_id`. |
| 122 | +* |
| 123 | +* If necessary, the string will be truncated at an |
| 124 | +* implementation-defined length. The string must not be the empty |
| 125 | +* string, but can be NULL if a valid `str_id` is provided. |
| 126 | +* |
| 127 | +* @return |
| 128 | +* 0 if tracing is disabled or `debugid` is being filtered out of trace. |
| 129 | +* It can also return (int64_t)-1 if an error occured. Otherwise, |
| 130 | +* it returns the ID to use to refer to the string in future |
| 131 | +* kdebug_trace(2) calls. |
| 132 | +* |
| 133 | +* The errors that can occur are: |
| 134 | +* |
| 135 | +* EINVAL |
| 136 | +* There are function qualifiers on `debugid`, `str` is empty, or |
| 137 | +* `str_id` was not generated by this function. |
| 138 | +* EPERM |
| 139 | +* The `debugid`'s class or subclass is reserved for internal use. |
| 140 | +* EFAULT |
| 141 | +* `str` is an invalid address or NULL when `str_id` is 0. |
| 142 | +*/ |
| 143 | +OPENSWIFTUI_EXPORT uint64_t kdebug_trace_string(uint32_t debugid, uint64_t str_id, |
| 144 | + const char *str) |
| 145 | +__OSX_AVAILABLE(10.11) __IOS_AVAILABLE(9.0); |
| 146 | + |
| 147 | +/* |
| 148 | +* Although the performance impact of kdebug_trace() when kernel |
| 149 | +* tracing is not enabled is minimal, it may require the caller to |
| 150 | +* perform an expensive calculation/summarization. This cost can be |
| 151 | +* skipped by checking the kdebug_is_enabled() predicate: |
| 152 | +* |
| 153 | +* if (kdebug_is_enabled(KDBG_CODE(DBG_XPC, 15, 1))) { |
| 154 | +* uint64_t arg1 = ...; |
| 155 | +* uint64_t arg2 = ...; |
| 156 | +* kdebug_trace(KDBG_CODE(DBG_XPC, 15, 1) | DBG_FUNC_NONE, arg1, arg2, 0, 0); |
| 157 | +* } |
| 158 | +* |
| 159 | +* If tracing is enabled for the code at the time of the check, 1 |
| 160 | +* will be returned. Otherwise, 0 will be returned. |
| 161 | +*/ |
| 162 | +OPENSWIFTUI_EXPORT bool kdebug_is_enabled(uint32_t code) |
| 163 | +__OSX_AVAILABLE(10.12) __IOS_AVAILABLE(10.0) |
| 164 | +__WATCHOS_AVAILABLE(3.0) __TVOS_AVAILABLE(10.0); |
| 165 | + |
| 166 | +#endif |
| 167 | + |
| 168 | +#endif /* kdebug_Private_h */ |
0 commit comments