xref: /xnu-10002.81.5/libkdd/kcdata.h (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions /*
2*5e3eaea3SApple OSS Distributions  * Copyright (c) 2015 Apple Inc. All rights reserved.
3*5e3eaea3SApple OSS Distributions  *
4*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5e3eaea3SApple OSS Distributions  *
6*5e3eaea3SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*5e3eaea3SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*5e3eaea3SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*5e3eaea3SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*5e3eaea3SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*5e3eaea3SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*5e3eaea3SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*5e3eaea3SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*5e3eaea3SApple OSS Distributions  *
15*5e3eaea3SApple OSS Distributions  * Please obtain a copy of the License at
16*5e3eaea3SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5e3eaea3SApple OSS Distributions  *
18*5e3eaea3SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*5e3eaea3SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5e3eaea3SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5e3eaea3SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5e3eaea3SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5e3eaea3SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*5e3eaea3SApple OSS Distributions  * limitations under the License.
25*5e3eaea3SApple OSS Distributions  *
26*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5e3eaea3SApple OSS Distributions  */
28*5e3eaea3SApple OSS Distributions 
29*5e3eaea3SApple OSS Distributions 
30*5e3eaea3SApple OSS Distributions /*
31*5e3eaea3SApple OSS Distributions  *
32*5e3eaea3SApple OSS Distributions  *                     THE KCDATA MANIFESTO
33*5e3eaea3SApple OSS Distributions  *
34*5e3eaea3SApple OSS Distributions  *   Kcdata is a self-describing data serialization format.  It is meant to get
35*5e3eaea3SApple OSS Distributions  *   nested data structures out of xnu with minimum fuss, but also for that data
36*5e3eaea3SApple OSS Distributions  *   to be easy to parse.  It is also meant to allow us to add new fields and
37*5e3eaea3SApple OSS Distributions  *   evolve the data format without breaking old parsers.
38*5e3eaea3SApple OSS Distributions  *
39*5e3eaea3SApple OSS Distributions  *   Kcdata is a permanent data format suitable for long-term storage including
40*5e3eaea3SApple OSS Distributions  *   in files.  It is very important that we continue to be able to parse old
41*5e3eaea3SApple OSS Distributions  *   versions of kcdata-based formats.  To this end, there are several
42*5e3eaea3SApple OSS Distributions  *   invariants you MUST MAINTAIN if you alter this file.
43*5e3eaea3SApple OSS Distributions  *
44*5e3eaea3SApple OSS Distributions  *     * None of the magic numbers should ever be a byteswap of themselves or
45*5e3eaea3SApple OSS Distributions  *       of any of the other magic numbers.
46*5e3eaea3SApple OSS Distributions  *
47*5e3eaea3SApple OSS Distributions  *     * Never remove any type.
48*5e3eaea3SApple OSS Distributions  *
49*5e3eaea3SApple OSS Distributions  *     * All kcdata structs must be packed, and must exclusively use fixed-size
50*5e3eaea3SApple OSS Distributions  *        types.
51*5e3eaea3SApple OSS Distributions  *
52*5e3eaea3SApple OSS Distributions  *     * Never change the definition of any type, except to add new fields to
53*5e3eaea3SApple OSS Distributions  *      the end.
54*5e3eaea3SApple OSS Distributions  *
55*5e3eaea3SApple OSS Distributions  *     * If you do add new fields to the end of a type, do not actually change
56*5e3eaea3SApple OSS Distributions  *       the definition of the old structure.  Instead, define a new structure
57*5e3eaea3SApple OSS Distributions  *       with the new fields.  See thread_snapshot_v3 as an example.  This
58*5e3eaea3SApple OSS Distributions  *       provides source compatibility for old readers, and also documents where
59*5e3eaea3SApple OSS Distributions  *       the potential size cutoffs are.
60*5e3eaea3SApple OSS Distributions  *
61*5e3eaea3SApple OSS Distributions  *     * If you change libkdd, or kcdata.py run the unit tests under libkdd.
62*5e3eaea3SApple OSS Distributions  *
63*5e3eaea3SApple OSS Distributions  *     * If you add a type or extend an existing one, add a sample test to
64*5e3eaea3SApple OSS Distributions  *       libkdd/tests so future changes to libkdd will always parse your struct
65*5e3eaea3SApple OSS Distributions  *       correctly.
66*5e3eaea3SApple OSS Distributions  *
67*5e3eaea3SApple OSS Distributions  *       For example to add a field to this:
68*5e3eaea3SApple OSS Distributions  *
69*5e3eaea3SApple OSS Distributions  *          struct foobar {
70*5e3eaea3SApple OSS Distributions  *              uint32_t baz;
71*5e3eaea3SApple OSS Distributions  *              uint32_t quux;
72*5e3eaea3SApple OSS Distributions  *          } __attribute__ ((packed));
73*5e3eaea3SApple OSS Distributions  *
74*5e3eaea3SApple OSS Distributions  *       Make it look like this:
75*5e3eaea3SApple OSS Distributions  *
76*5e3eaea3SApple OSS Distributions  *          struct foobar {
77*5e3eaea3SApple OSS Distributions  *              uint32_t baz;
78*5e3eaea3SApple OSS Distributions  *              uint32_t quux;
79*5e3eaea3SApple OSS Distributions  *              ///////// end version 1 of foobar.  sizeof(struct foobar) was 8 ////////
80*5e3eaea3SApple OSS Distributions  *              uint32_t frozzle;
81*5e3eaea3SApple OSS Distributions  *          } __attribute__ ((packed));
82*5e3eaea3SApple OSS Distributions  *
83*5e3eaea3SApple OSS Distributions  *   If you are parsing kcdata formats, you MUST
84*5e3eaea3SApple OSS Distributions  *
85*5e3eaea3SApple OSS Distributions  *     * Check the length field of each struct, including array elements.   If the
86*5e3eaea3SApple OSS Distributions  *       struct is longer than you expect, you must ignore the extra data.
87*5e3eaea3SApple OSS Distributions  *
88*5e3eaea3SApple OSS Distributions  *     * Ignore any data types you do not understand.
89*5e3eaea3SApple OSS Distributions  *
90*5e3eaea3SApple OSS Distributions  *   Additionally, we want to be as forward compatible as we can.  Meaning old
91*5e3eaea3SApple OSS Distributions  *   tools should still be able to use new data whenever possible.  To this end,
92*5e3eaea3SApple OSS Distributions  *   you should:
93*5e3eaea3SApple OSS Distributions  *
94*5e3eaea3SApple OSS Distributions  *     * Try not to add new versions of types that supplant old ones.  Instead
95*5e3eaea3SApple OSS Distributions  *        extend the length of existing types or add supplemental types.
96*5e3eaea3SApple OSS Distributions  *
97*5e3eaea3SApple OSS Distributions  *     * Try not to remove information from existing kcdata formats, unless
98*5e3eaea3SApple OSS Distributions  *        removal was explicitly asked for.  For example it is fine to add a
99*5e3eaea3SApple OSS Distributions  *        stackshot flag to remove unwanted information, but you should not
100*5e3eaea3SApple OSS Distributions  *        remove it from the default stackshot if the new flag is absent.
101*5e3eaea3SApple OSS Distributions  *
102*5e3eaea3SApple OSS Distributions  *     * (TBD) If you do break old readers by removing information or
103*5e3eaea3SApple OSS Distributions  *        supplanting old structs, then increase the major version number.
104*5e3eaea3SApple OSS Distributions  *
105*5e3eaea3SApple OSS Distributions  *
106*5e3eaea3SApple OSS Distributions  *
107*5e3eaea3SApple OSS Distributions  *  The following is a description of the kcdata format.
108*5e3eaea3SApple OSS Distributions  *
109*5e3eaea3SApple OSS Distributions  *
110*5e3eaea3SApple OSS Distributions  * The format for data is setup in a generic format as follows
111*5e3eaea3SApple OSS Distributions  *
112*5e3eaea3SApple OSS Distributions  * Layout of data structure:
113*5e3eaea3SApple OSS Distributions  *
114*5e3eaea3SApple OSS Distributions  *   |         8 - bytes         |
115*5e3eaea3SApple OSS Distributions  *   |  type = MAGIC |  LENGTH   |
116*5e3eaea3SApple OSS Distributions  *   |            0              |
117*5e3eaea3SApple OSS Distributions  *   |      type     |  size     |
118*5e3eaea3SApple OSS Distributions  *   |          flags            |
119*5e3eaea3SApple OSS Distributions  *   |           data            |
120*5e3eaea3SApple OSS Distributions  *   |___________data____________|
121*5e3eaea3SApple OSS Distributions  *   |      type     |   size    |
122*5e3eaea3SApple OSS Distributions  *   |          flags            |
123*5e3eaea3SApple OSS Distributions  *   |___________data____________|
124*5e3eaea3SApple OSS Distributions  *   |  type = END   |  size=0   |
125*5e3eaea3SApple OSS Distributions  *   |            0              |
126*5e3eaea3SApple OSS Distributions  *
127*5e3eaea3SApple OSS Distributions  *
128*5e3eaea3SApple OSS Distributions  * The type field describes what kind of data is passed. For example type = TASK_CRASHINFO_UUID means the following data is a uuid.
129*5e3eaea3SApple OSS Distributions  * These types need to be defined in task_corpses.h for easy consumption by userspace inspection tools.
130*5e3eaea3SApple OSS Distributions  *
131*5e3eaea3SApple OSS Distributions  * Some range of types is reserved for special types like ints, longs etc. A cool new functionality made possible with this
132*5e3eaea3SApple OSS Distributions  * extensible data format is that kernel can decide to put more information as required without requiring user space tools to
133*5e3eaea3SApple OSS Distributions  * re-compile to be compatible. The case of rusage struct versions could be introduced without breaking existing tools.
134*5e3eaea3SApple OSS Distributions  *
135*5e3eaea3SApple OSS Distributions  * Feature description: Generic data with description
136*5e3eaea3SApple OSS Distributions  * -------------------
137*5e3eaea3SApple OSS Distributions  * Further more generic data with description is very much possible now. For example
138*5e3eaea3SApple OSS Distributions  *
139*5e3eaea3SApple OSS Distributions  *   - kcdata_add_uint64_with_description(cdatainfo, 0x700, "NUM MACH PORTS");
140*5e3eaea3SApple OSS Distributions  *   - and more functions that allow adding description.
141*5e3eaea3SApple OSS Distributions  * The userspace tools can then look at the description and print the data even if they are not compiled with knowledge of the field apriori.
142*5e3eaea3SApple OSS Distributions  *
143*5e3eaea3SApple OSS Distributions  *  Example data:
144*5e3eaea3SApple OSS Distributions  * 0000  57 f1 ad de 00 00 00 00 00 00 00 00 00 00 00 00  W...............
145*5e3eaea3SApple OSS Distributions  * 0010  01 00 00 00 00 00 00 00 30 00 00 00 00 00 00 00  ........0.......
146*5e3eaea3SApple OSS Distributions  * 0020  50 49 44 00 00 00 00 00 00 00 00 00 00 00 00 00  PID.............
147*5e3eaea3SApple OSS Distributions  * 0030  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
148*5e3eaea3SApple OSS Distributions  * 0040  9c 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
149*5e3eaea3SApple OSS Distributions  * 0050  01 00 00 00 00 00 00 00 30 00 00 00 00 00 00 00  ........0.......
150*5e3eaea3SApple OSS Distributions  * 0060  50 41 52 45 4e 54 20 50 49 44 00 00 00 00 00 00  PARENT PID......
151*5e3eaea3SApple OSS Distributions  * 0070  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
152*5e3eaea3SApple OSS Distributions  * 0080  01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
153*5e3eaea3SApple OSS Distributions  * 0090  ed 58 91 f1
154*5e3eaea3SApple OSS Distributions  *
155*5e3eaea3SApple OSS Distributions  * Feature description: Container markers for compound data
156*5e3eaea3SApple OSS Distributions  * ------------------
157*5e3eaea3SApple OSS Distributions  * If a given kernel data type is complex and requires adding multiple optional fields inside a container
158*5e3eaea3SApple OSS Distributions  * object for a consumer to understand arbitrary data, we package it using container markers.
159*5e3eaea3SApple OSS Distributions  *
160*5e3eaea3SApple OSS Distributions  * For example, the stackshot code gathers information and describes the state of a given task with respect
161*5e3eaea3SApple OSS Distributions  * to many subsystems. It includes data such as io stats, vm counters, process names/flags and syscall counts.
162*5e3eaea3SApple OSS Distributions  *
163*5e3eaea3SApple OSS Distributions  * kcdata_add_container_marker(kcdata_p, KCDATA_TYPE_CONTAINER_BEGIN, STACKSHOT_KCCONTAINER_TASK, task_uniqueid);
164*5e3eaea3SApple OSS Distributions  * // add multiple data, or add_<type>_with_description()s here
165*5e3eaea3SApple OSS Distributions  *
166*5e3eaea3SApple OSS Distributions  * kcdata_add_container_marker(kcdata_p, KCDATA_TYPE_CONTAINER_END, STACKSHOT_KCCONTAINER_TASK, task_uniqueid);
167*5e3eaea3SApple OSS Distributions  *
168*5e3eaea3SApple OSS Distributions  * Feature description: Custom Data formats on demand
169*5e3eaea3SApple OSS Distributions  * --------------------
170*5e3eaea3SApple OSS Distributions  * With the self describing nature of format, the kernel provider can describe a data type (uniquely identified by a number) and use
171*5e3eaea3SApple OSS Distributions  * it in the buffer for sending data. The consumer can parse the type information and have knowledge of describing incoming data.
172*5e3eaea3SApple OSS Distributions  * Following is an example of how we can describe a kernel specific struct sample_disk_io_stats in buffer.
173*5e3eaea3SApple OSS Distributions  *
174*5e3eaea3SApple OSS Distributions  * struct sample_disk_io_stats {
175*5e3eaea3SApple OSS Distributions  *     uint64_t        disk_reads_count;
176*5e3eaea3SApple OSS Distributions  *     uint64_t        disk_reads_size;
177*5e3eaea3SApple OSS Distributions  *     uint64_t        io_priority_count[4];
178*5e3eaea3SApple OSS Distributions  *     uint64_t        io_priority_size;
179*5e3eaea3SApple OSS Distributions  * } __attribute__ ((packed));
180*5e3eaea3SApple OSS Distributions  *
181*5e3eaea3SApple OSS Distributions  *
182*5e3eaea3SApple OSS Distributions  * struct kcdata_subtype_descriptor disk_io_stats_def[] = {
183*5e3eaea3SApple OSS Distributions  *     {KCS_SUBTYPE_FLAGS_NONE, KC_ST_UINT64, 0 * sizeof(uint64_t), sizeof(uint64_t), "disk_reads_count"},
184*5e3eaea3SApple OSS Distributions  *     {KCS_SUBTYPE_FLAGS_NONE, KC_ST_UINT64, 1 * sizeof(uint64_t), sizeof(uint64_t), "disk_reads_size"},
185*5e3eaea3SApple OSS Distributions  *     {KCS_SUBTYPE_FLAGS_ARRAY, KC_ST_UINT64, 2 * sizeof(uint64_t), KCS_SUBTYPE_PACK_SIZE(4, sizeof(uint64_t)), "io_priority_count"},
186*5e3eaea3SApple OSS Distributions  *     {KCS_SUBTYPE_FLAGS_ARRAY, KC_ST_UINT64, (2 + 4) * sizeof(uint64_t), sizeof(uint64_t), "io_priority_size"},
187*5e3eaea3SApple OSS Distributions  * };
188*5e3eaea3SApple OSS Distributions  *
189*5e3eaea3SApple OSS Distributions  * Now you can add this custom type definition into the buffer as
190*5e3eaea3SApple OSS Distributions  * kcdata_add_type_definition(kcdata_p, KCTYPE_SAMPLE_DISK_IO_STATS, "sample_disk_io_stats",
191*5e3eaea3SApple OSS Distributions  *          &disk_io_stats_def[0], sizeof(disk_io_stats_def)/sizeof(struct kcdata_subtype_descriptor));
192*5e3eaea3SApple OSS Distributions  *
193*5e3eaea3SApple OSS Distributions  * Feature description: Compression
194*5e3eaea3SApple OSS Distributions  * --------------------
195*5e3eaea3SApple OSS Distributions  * In order to avoid keeping large amounts of memory reserved for a panic stackshot, kcdata has support
196*5e3eaea3SApple OSS Distributions  * for compressing the buffer in a streaming fashion. New data pushed to the kcdata buffer will be
197*5e3eaea3SApple OSS Distributions  * automatically compressed using an algorithm selected by the API user (currently, we only support
198*5e3eaea3SApple OSS Distributions  * pass-through and zlib, in the future we plan to add WKDM support, see: 57913859).
199*5e3eaea3SApple OSS Distributions  *
200*5e3eaea3SApple OSS Distributions  * To start using compression, call:
201*5e3eaea3SApple OSS Distributions  *   kcdata_init_compress(kcdata_p, hdr_tag, memcpy_f, comp_type);
202*5e3eaea3SApple OSS Distributions  * where:
203*5e3eaea3SApple OSS Distributions  *   `kcdata_p` is the kcdata buffer that will be used
204*5e3eaea3SApple OSS Distributions  *   `hdr_tag` is the usual header tag denoting what type of kcdata buffer this will be
205*5e3eaea3SApple OSS Distributions  *   `memcpy_f` a memcpy(3) function to use to copy into the buffer, optional.
206*5e3eaea3SApple OSS Distributions  *	 `compy_type` is the compression type, see KCDCT_ZLIB for an example.
207*5e3eaea3SApple OSS Distributions  *
208*5e3eaea3SApple OSS Distributions  * Once compression is initialized:
209*5e3eaea3SApple OSS Distributions  *  (1) all self-describing APIs will automatically compress
210*5e3eaea3SApple OSS Distributions  *  (2) you can now use the following APIs to compress data into the buffer:
211*5e3eaea3SApple OSS Distributions  *    (None of the following will compress unless kcdata_init_compress() has been called)
212*5e3eaea3SApple OSS Distributions  *
213*5e3eaea3SApple OSS Distributions  * - kcdata_push_data(kcdata_descriptor_t data, uint32_t type, uint32_t size, const void *input_data)
214*5e3eaea3SApple OSS Distributions  *   Pushes the buffer of kctype @type at[@input_data, @input_data + @size]
215*5e3eaea3SApple OSS Distributions  *   into the kcdata buffer @data, compressing if needed.
216*5e3eaea3SApple OSS Distributions  *
217*5e3eaea3SApple OSS Distributions  * - kcdata_push_array(kcdata_descriptor_t data, uint32_t type_of_element,
218*5e3eaea3SApple OSS Distributions  *            uint32_t size_of_element, uint32_t count, const void *input_data)
219*5e3eaea3SApple OSS Distributions  *   Pushes the array found at @input_data, with element type @type_of_element, where
220*5e3eaea3SApple OSS Distributions  *   each element is of size @size_of_element and there are @count elements into the kcdata buffer
221*5e3eaea3SApple OSS Distributions  *   at @data.
222*5e3eaea3SApple OSS Distributions  *
223*5e3eaea3SApple OSS Distributions  * - kcdata_compression_window_open/close(kcdata_descriptor_t data)
224*5e3eaea3SApple OSS Distributions  *   In case the data you are trying to push to the kcdata buffer @data is difficult to predict,
225*5e3eaea3SApple OSS Distributions  *   you can open a "compression window". Between an open and a close, no compression will be done.
226*5e3eaea3SApple OSS Distributions  *   Once you clsoe the window, the underlying compression algorithm will compress the data into the buffer
227*5e3eaea3SApple OSS Distributions  *   and automatically rewind the current end marker of the kcdata buffer.
228*5e3eaea3SApple OSS Distributions  *   There is an ASCII art in kern_cdata.c to aid the reader in understanding
229*5e3eaea3SApple OSS Distributions  *   this.
230*5e3eaea3SApple OSS Distributions  *
231*5e3eaea3SApple OSS Distributions  * - kcdata_finish_compression(kcdata_descriptor_t data)
232*5e3eaea3SApple OSS Distributions  *   Must be called at the end to flush any underlying buffers used by the compression algorithms.
233*5e3eaea3SApple OSS Distributions  *   This function will also add some statistics about the compression to the buffer which helps with
234*5e3eaea3SApple OSS Distributions  *   decompressing later.
235*5e3eaea3SApple OSS Distributions  *
236*5e3eaea3SApple OSS Distributions  * Once you are done with the kcdata buffer, call kcdata_deinit_compress to
237*5e3eaea3SApple OSS Distributions  * free any buffers that may have been allocated internal to the compression
238*5e3eaea3SApple OSS Distributions  * algorithm.
239*5e3eaea3SApple OSS Distributions  */
240*5e3eaea3SApple OSS Distributions 
241*5e3eaea3SApple OSS Distributions 
242*5e3eaea3SApple OSS Distributions #ifndef _KCDATA_H_
243*5e3eaea3SApple OSS Distributions #define _KCDATA_H_
244*5e3eaea3SApple OSS Distributions 
245*5e3eaea3SApple OSS Distributions #include <stdint.h>
246*5e3eaea3SApple OSS Distributions #include <string.h>
247*5e3eaea3SApple OSS Distributions #include <uuid/uuid.h>
248*5e3eaea3SApple OSS Distributions 
249*5e3eaea3SApple OSS Distributions #define KCDATA_DESC_MAXLEN 32 /* including NULL byte at end */
250*5e3eaea3SApple OSS Distributions 
251*5e3eaea3SApple OSS Distributions #define KCDATA_FLAGS_STRUCT_PADDING_MASK 0xf
252*5e3eaea3SApple OSS Distributions #define KCDATA_FLAGS_STRUCT_HAS_PADDING 0x80
253*5e3eaea3SApple OSS Distributions 
254*5e3eaea3SApple OSS Distributions /*
255*5e3eaea3SApple OSS Distributions  * kcdata aligns elements to 16 byte boundaries.
256*5e3eaea3SApple OSS Distributions  */
257*5e3eaea3SApple OSS Distributions #define KCDATA_ALIGNMENT_SIZE       0x10
258*5e3eaea3SApple OSS Distributions 
259*5e3eaea3SApple OSS Distributions struct kcdata_item {
260*5e3eaea3SApple OSS Distributions 	uint32_t type;
261*5e3eaea3SApple OSS Distributions 	uint32_t size; /* len(data)  */
262*5e3eaea3SApple OSS Distributions 	               /* flags.
263*5e3eaea3SApple OSS Distributions 	                *
264*5e3eaea3SApple OSS Distributions 	                * For structures:
265*5e3eaea3SApple OSS Distributions 	                *    padding      = flags & 0xf
266*5e3eaea3SApple OSS Distributions 	                *    has_padding  = (flags & 0x80) >> 7
267*5e3eaea3SApple OSS Distributions 	                *
268*5e3eaea3SApple OSS Distributions 	                * has_padding is needed to disambiguate cases such as
269*5e3eaea3SApple OSS Distributions 	                * thread_snapshot_v2 and thread_snapshot_v3.  Their
270*5e3eaea3SApple OSS Distributions 	                * respective sizes are 0x68 and 0x70, and thread_snapshot_v2
271*5e3eaea3SApple OSS Distributions 	                * was emitted by old kernels *before* we started recording
272*5e3eaea3SApple OSS Distributions 	                * padding.  Since legacy thread_snapsht_v2 and modern
273*5e3eaea3SApple OSS Distributions 	                * thread_snapshot_v3 will both record 0 for the padding
274*5e3eaea3SApple OSS Distributions 	                * flags, we need some other bit which will be nonzero in the
275*5e3eaea3SApple OSS Distributions 	                * flags to disambiguate.
276*5e3eaea3SApple OSS Distributions 	                *
277*5e3eaea3SApple OSS Distributions 	                * This is why we hardcode a special case for
278*5e3eaea3SApple OSS Distributions 	                * STACKSHOT_KCTYPE_THREAD_SNAPSHOT into the iterator
279*5e3eaea3SApple OSS Distributions 	                * functions below.  There is only a finite number of such
280*5e3eaea3SApple OSS Distributions 	                * hardcodings which will ever be needed.  They can occur
281*5e3eaea3SApple OSS Distributions 	                * when:
282*5e3eaea3SApple OSS Distributions 	                *
283*5e3eaea3SApple OSS Distributions 	                *  * We have a legacy structure that predates padding flags
284*5e3eaea3SApple OSS Distributions 	                *
285*5e3eaea3SApple OSS Distributions 	                *  * which we want to extend without changing the kcdata type
286*5e3eaea3SApple OSS Distributions 	                *
287*5e3eaea3SApple OSS Distributions 	                *  * by only so many bytes as would fit in the space that
288*5e3eaea3SApple OSS Distributions 	                *  was previously unused padding.
289*5e3eaea3SApple OSS Distributions 	                *
290*5e3eaea3SApple OSS Distributions 	                * For containers:
291*5e3eaea3SApple OSS Distributions 	                *    container_id = flags
292*5e3eaea3SApple OSS Distributions 	                *
293*5e3eaea3SApple OSS Distributions 	                * For arrays:
294*5e3eaea3SApple OSS Distributions 	                *    element_count = flags & UINT32_MAX
295*5e3eaea3SApple OSS Distributions 	                *    element_type = (flags >> 32) & UINT32_MAX
296*5e3eaea3SApple OSS Distributions 	                */
297*5e3eaea3SApple OSS Distributions 	uint64_t flags;
298*5e3eaea3SApple OSS Distributions 	char data[]; /* must be at the end */
299*5e3eaea3SApple OSS Distributions };
300*5e3eaea3SApple OSS Distributions 
301*5e3eaea3SApple OSS Distributions typedef struct kcdata_item * kcdata_item_t;
302*5e3eaea3SApple OSS Distributions 
303*5e3eaea3SApple OSS Distributions enum KCDATA_SUBTYPE_TYPES { KC_ST_CHAR = 1, KC_ST_INT8, KC_ST_UINT8, KC_ST_INT16, KC_ST_UINT16, KC_ST_INT32, KC_ST_UINT32, KC_ST_INT64, KC_ST_UINT64 };
304*5e3eaea3SApple OSS Distributions typedef enum KCDATA_SUBTYPE_TYPES kctype_subtype_t;
305*5e3eaea3SApple OSS Distributions 
306*5e3eaea3SApple OSS Distributions /*
307*5e3eaea3SApple OSS Distributions  * A subtype description structure that defines
308*5e3eaea3SApple OSS Distributions  * how a compound data is laid out in memory. This
309*5e3eaea3SApple OSS Distributions  * provides on the fly definition of types and consumption
310*5e3eaea3SApple OSS Distributions  * by the parser.
311*5e3eaea3SApple OSS Distributions  */
312*5e3eaea3SApple OSS Distributions struct kcdata_subtype_descriptor {
313*5e3eaea3SApple OSS Distributions 	uint8_t kcs_flags;
314*5e3eaea3SApple OSS Distributions #define KCS_SUBTYPE_FLAGS_NONE 0x0
315*5e3eaea3SApple OSS Distributions #define KCS_SUBTYPE_FLAGS_ARRAY 0x1
316*5e3eaea3SApple OSS Distributions /* Force struct type even if only one element.
317*5e3eaea3SApple OSS Distributions  *
318*5e3eaea3SApple OSS Distributions  * Normally a kcdata_type_definition is treated as a structure if it has
319*5e3eaea3SApple OSS Distributions  * more than one subtype descriptor.  Otherwise it is treated as a simple
320*5e3eaea3SApple OSS Distributions  * type.  For example libkdd will represent a simple integer 42 as simply
321*5e3eaea3SApple OSS Distributions  * 42, but it will represent a structure containing an integer 42 as
322*5e3eaea3SApple OSS Distributions  * {"field_name": 42}..
323*5e3eaea3SApple OSS Distributions  *
324*5e3eaea3SApple OSS Distributions  * If a kcdata_type_definition has only single subtype, then it will be
325*5e3eaea3SApple OSS Distributions  * treated as a structure iff KCS_SUBTYPE_FLAGS_STRUCT is set.  If it has
326*5e3eaea3SApple OSS Distributions  * multiple subtypes, it will always be treated as a structure.
327*5e3eaea3SApple OSS Distributions  *
328*5e3eaea3SApple OSS Distributions  * KCS_SUBTYPE_FLAGS_MERGE has the opposite effect.  If this flag is used then
329*5e3eaea3SApple OSS Distributions  * even if there are multiple elements, they will all be treated as individual
330*5e3eaea3SApple OSS Distributions  * properties of the parent dictionary.
331*5e3eaea3SApple OSS Distributions  */
332*5e3eaea3SApple OSS Distributions #define KCS_SUBTYPE_FLAGS_STRUCT 0x2                    /* force struct type even if only one element */
333*5e3eaea3SApple OSS Distributions #define KCS_SUBTYPE_FLAGS_MERGE 0x4                     /* treat as multiple elements of parents instead of struct */
334*5e3eaea3SApple OSS Distributions 	uint8_t kcs_elem_type;                              /* restricted to kctype_subtype_t */
335*5e3eaea3SApple OSS Distributions 	uint16_t kcs_elem_offset;                           /* offset in struct where data is found */
336*5e3eaea3SApple OSS Distributions 	uint32_t kcs_elem_size;                             /* size of element (or) packed state for array type */
337*5e3eaea3SApple OSS Distributions 	char                 kcs_name[KCDATA_DESC_MAXLEN];  /* max 31 bytes for name of field */
338*5e3eaea3SApple OSS Distributions };
339*5e3eaea3SApple OSS Distributions 
340*5e3eaea3SApple OSS Distributions typedef struct kcdata_subtype_descriptor * kcdata_subtype_descriptor_t;
341*5e3eaea3SApple OSS Distributions 
342*5e3eaea3SApple OSS Distributions /*
343*5e3eaea3SApple OSS Distributions  * In case of array of basic c types in kctype_subtype_t,
344*5e3eaea3SApple OSS Distributions  * size is packed in lower 16 bits and
345*5e3eaea3SApple OSS Distributions  * count is packed in upper 16 bits of kcs_elem_size field.
346*5e3eaea3SApple OSS Distributions  */
347*5e3eaea3SApple OSS Distributions #define KCS_SUBTYPE_PACK_SIZE(e_count, e_size) (((e_count)&0xffffu) << 16 | ((e_size)&0xffffu))
348*5e3eaea3SApple OSS Distributions 
349*5e3eaea3SApple OSS Distributions static inline uint32_t
kcs_get_elem_size(kcdata_subtype_descriptor_t d)350*5e3eaea3SApple OSS Distributions kcs_get_elem_size(kcdata_subtype_descriptor_t d)
351*5e3eaea3SApple OSS Distributions {
352*5e3eaea3SApple OSS Distributions 	if (d->kcs_flags & KCS_SUBTYPE_FLAGS_ARRAY) {
353*5e3eaea3SApple OSS Distributions 		/* size is composed as ((count &0xffff)<<16 | (elem_size & 0xffff)) */
354*5e3eaea3SApple OSS Distributions 		return (uint32_t)((d->kcs_elem_size & 0xffff) * ((d->kcs_elem_size & 0xffff0000) >> 16));
355*5e3eaea3SApple OSS Distributions 	}
356*5e3eaea3SApple OSS Distributions 	return d->kcs_elem_size;
357*5e3eaea3SApple OSS Distributions }
358*5e3eaea3SApple OSS Distributions 
359*5e3eaea3SApple OSS Distributions static inline uint32_t
kcs_get_elem_count(kcdata_subtype_descriptor_t d)360*5e3eaea3SApple OSS Distributions kcs_get_elem_count(kcdata_subtype_descriptor_t d)
361*5e3eaea3SApple OSS Distributions {
362*5e3eaea3SApple OSS Distributions 	if (d->kcs_flags & KCS_SUBTYPE_FLAGS_ARRAY) {
363*5e3eaea3SApple OSS Distributions 		return (d->kcs_elem_size >> 16) & 0xffff;
364*5e3eaea3SApple OSS Distributions 	}
365*5e3eaea3SApple OSS Distributions 	return 1;
366*5e3eaea3SApple OSS Distributions }
367*5e3eaea3SApple OSS Distributions 
368*5e3eaea3SApple OSS Distributions static inline int
kcs_set_elem_size(kcdata_subtype_descriptor_t d,uint32_t size,uint32_t count)369*5e3eaea3SApple OSS Distributions kcs_set_elem_size(kcdata_subtype_descriptor_t d, uint32_t size, uint32_t count)
370*5e3eaea3SApple OSS Distributions {
371*5e3eaea3SApple OSS Distributions 	if (count > 1) {
372*5e3eaea3SApple OSS Distributions 		/* means we are setting up an array */
373*5e3eaea3SApple OSS Distributions 		if (size > 0xffff || count > 0xffff) {
374*5e3eaea3SApple OSS Distributions 			return -1; //invalid argument
375*5e3eaea3SApple OSS Distributions 		}
376*5e3eaea3SApple OSS Distributions 		d->kcs_elem_size = ((count & 0xffff) << 16 | (size & 0xffff));
377*5e3eaea3SApple OSS Distributions 	} else {
378*5e3eaea3SApple OSS Distributions 		d->kcs_elem_size = size;
379*5e3eaea3SApple OSS Distributions 	}
380*5e3eaea3SApple OSS Distributions 	return 0;
381*5e3eaea3SApple OSS Distributions }
382*5e3eaea3SApple OSS Distributions 
383*5e3eaea3SApple OSS Distributions struct kcdata_type_definition {
384*5e3eaea3SApple OSS Distributions 	uint32_t kct_type_identifier;
385*5e3eaea3SApple OSS Distributions 	uint32_t kct_num_elements;
386*5e3eaea3SApple OSS Distributions 	char kct_name[KCDATA_DESC_MAXLEN];
387*5e3eaea3SApple OSS Distributions 	struct kcdata_subtype_descriptor kct_elements[];
388*5e3eaea3SApple OSS Distributions };
389*5e3eaea3SApple OSS Distributions 
390*5e3eaea3SApple OSS Distributions 
391*5e3eaea3SApple OSS Distributions /* chunk type definitions. 0 - 0x7ff are reserved  and defined here
392*5e3eaea3SApple OSS Distributions  * NOTE: Please update kcdata/libkdd/kcdtypes.c if you make any changes
393*5e3eaea3SApple OSS Distributions  * in STACKSHOT_KCTYPE_* types.
394*5e3eaea3SApple OSS Distributions  */
395*5e3eaea3SApple OSS Distributions 
396*5e3eaea3SApple OSS Distributions /*
397*5e3eaea3SApple OSS Distributions  * Types with description value.
398*5e3eaea3SApple OSS Distributions  * these will have KCDATA_DESC_MAXLEN-1 length string description
399*5e3eaea3SApple OSS Distributions  * and rest of kcdata_iter_size() - KCDATA_DESC_MAXLEN bytes as data
400*5e3eaea3SApple OSS Distributions  */
401*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_INVALID 0x0u
402*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_STRING_DESC 0x1u
403*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_UINT32_DESC 0x2u
404*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_UINT64_DESC 0x3u
405*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_INT32_DESC 0x4u
406*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_INT64_DESC 0x5u
407*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_BINDATA_DESC 0x6u
408*5e3eaea3SApple OSS Distributions 
409*5e3eaea3SApple OSS Distributions /*
410*5e3eaea3SApple OSS Distributions  * Compound type definitions
411*5e3eaea3SApple OSS Distributions  */
412*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_ARRAY 0x11u         /* Array of data OBSOLETE DONT USE THIS*/
413*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_TYPEDEFINTION 0x12u /* Meta type that describes a type on the fly. */
414*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_CONTAINER_BEGIN                                       \
415*5e3eaea3SApple OSS Distributions 	0x13u /* Container type which has corresponding CONTAINER_END header. \
416*5e3eaea3SApple OSS Distributions 	       * KCDATA_TYPE_CONTAINER_BEGIN has type in the data segment. \
417*5e3eaea3SApple OSS Distributions 	       * Both headers have (uint64_t) ID for matching up nested data. \
418*5e3eaea3SApple OSS Distributions 	       */
419*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_CONTAINER_END 0x14u
420*5e3eaea3SApple OSS Distributions 
421*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_ARRAY_PAD0 0x20u /* Array of data with 0 byte of padding*/
422*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_ARRAY_PAD1 0x21u /* Array of data with 1 byte of padding*/
423*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_ARRAY_PAD2 0x22u /* Array of data with 2 byte of padding*/
424*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_ARRAY_PAD3 0x23u /* Array of data with 3 byte of padding*/
425*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_ARRAY_PAD4 0x24u /* Array of data with 4 byte of padding*/
426*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_ARRAY_PAD5 0x25u /* Array of data with 5 byte of padding*/
427*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_ARRAY_PAD6 0x26u /* Array of data with 6 byte of padding*/
428*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_ARRAY_PAD7 0x27u /* Array of data with 7 byte of padding*/
429*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_ARRAY_PAD8 0x28u /* Array of data with 8 byte of padding*/
430*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_ARRAY_PAD9 0x29u /* Array of data with 9 byte of padding*/
431*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_ARRAY_PADa 0x2au /* Array of data with a byte of padding*/
432*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_ARRAY_PADb 0x2bu /* Array of data with b byte of padding*/
433*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_ARRAY_PADc 0x2cu /* Array of data with c byte of padding*/
434*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_ARRAY_PADd 0x2du /* Array of data with d byte of padding*/
435*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_ARRAY_PADe 0x2eu /* Array of data with e byte of padding*/
436*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_ARRAY_PADf 0x2fu /* Array of data with f byte of padding*/
437*5e3eaea3SApple OSS Distributions 
438*5e3eaea3SApple OSS Distributions /*
439*5e3eaea3SApple OSS Distributions  * Generic data types that are most commonly used
440*5e3eaea3SApple OSS Distributions  */
441*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_LIBRARY_LOADINFO 0x30u   /* struct dyld_uuid_info_32 */
442*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_LIBRARY_LOADINFO64 0x31u /* struct dyld_uuid_info_64 */
443*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_TIMEBASE 0x32u           /* struct mach_timebase_info */
444*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_MACH_ABSOLUTE_TIME 0x33u /* uint64_t */
445*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_TIMEVAL 0x34u            /* struct timeval64 */
446*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_USECS_SINCE_EPOCH 0x35u  /* time in usecs uint64_t */
447*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_PID 0x36u                /* int32_t */
448*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_PROCNAME 0x37u           /* char * */
449*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_NESTED_KCDATA 0x38u      /* nested kcdata buffer */
450*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_LIBRARY_AOTINFO 0x39u    /* struct user64_dyld_aot_info */
451*5e3eaea3SApple OSS Distributions 
452*5e3eaea3SApple OSS Distributions #define KCDATA_TYPE_BUFFER_END 0xF19158EDu
453*5e3eaea3SApple OSS Distributions 
454*5e3eaea3SApple OSS Distributions /* MAGIC numbers defined for each class of chunked data
455*5e3eaea3SApple OSS Distributions  *
456*5e3eaea3SApple OSS Distributions  * To future-proof against big-endian arches, make sure none of these magic
457*5e3eaea3SApple OSS Distributions  * numbers are byteswaps of each other
458*5e3eaea3SApple OSS Distributions  */
459*5e3eaea3SApple OSS Distributions 
460*5e3eaea3SApple OSS Distributions #define KCDATA_BUFFER_BEGIN_CRASHINFO 0xDEADF157u            /* owner: corpses/task_corpse.h */
461*5e3eaea3SApple OSS Distributions                                                              /* type-range: 0x800 - 0x8ff */
462*5e3eaea3SApple OSS Distributions #define KCDATA_BUFFER_BEGIN_STACKSHOT 0x59a25807u            /* owner: sys/stackshot.h */
463*5e3eaea3SApple OSS Distributions                                                              /* type-range: 0x900 - 0x93f */
464*5e3eaea3SApple OSS Distributions #define KCDATA_BUFFER_BEGIN_COMPRESSED 0x434f4d50u           /* owner: sys/stackshot.h */
465*5e3eaea3SApple OSS Distributions                                                              /* type-range: 0x900 - 0x93f */
466*5e3eaea3SApple OSS Distributions #define KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT 0xDE17A59Au      /* owner: sys/stackshot.h */
467*5e3eaea3SApple OSS Distributions                                                              /* type-range: 0x940 - 0x9ff */
468*5e3eaea3SApple OSS Distributions #define KCDATA_BUFFER_BEGIN_BTINFO    0x46414E47u            /* owner: kern/kern_exit.c */
469*5e3eaea3SApple OSS Distributions                                                              /* type-range: 0xa01 - 0xaff */
470*5e3eaea3SApple OSS Distributions #define KCDATA_BUFFER_BEGIN_OS_REASON 0x53A20900u            /* owner: sys/reason.h */
471*5e3eaea3SApple OSS Distributions                                                              /* type-range: 0x1000-0x103f */
472*5e3eaea3SApple OSS Distributions #define KCDATA_BUFFER_BEGIN_XNUPOST_CONFIG 0x1e21c09fu       /* owner: osfmk/tests/kernel_tests.c */
473*5e3eaea3SApple OSS Distributions                                                              /* type-range: 0x1040-0x105f */
474*5e3eaea3SApple OSS Distributions 
475*5e3eaea3SApple OSS Distributions /* next type range number available 0x1060 */
476*5e3eaea3SApple OSS Distributions /**************** definitions for XNUPOST *********************/
477*5e3eaea3SApple OSS Distributions #define XNUPOST_KCTYPE_TESTCONFIG               0x1040
478*5e3eaea3SApple OSS Distributions 
479*5e3eaea3SApple OSS Distributions /**************** definitions for stackshot *********************/
480*5e3eaea3SApple OSS Distributions 
481*5e3eaea3SApple OSS Distributions /* This value must always match IO_NUM_PRIORITIES defined in thread_info.h */
482*5e3eaea3SApple OSS Distributions #define STACKSHOT_IO_NUM_PRIORITIES     4
483*5e3eaea3SApple OSS Distributions /* This value must always match MAXTHREADNAMESIZE used in bsd */
484*5e3eaea3SApple OSS Distributions #define STACKSHOT_MAX_THREAD_NAME_SIZE  64
485*5e3eaea3SApple OSS Distributions 
486*5e3eaea3SApple OSS Distributions /*
487*5e3eaea3SApple OSS Distributions  * NOTE: Please update kcdata/libkdd/kcdtypes.c if you make any changes
488*5e3eaea3SApple OSS Distributions  * in STACKSHOT_KCTYPE_* types.
489*5e3eaea3SApple OSS Distributions  */
490*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_IOSTATS                     0x901u /* io_stats_snapshot */
491*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_GLOBAL_MEM_STATS            0x902u /* struct mem_and_io_snapshot */
492*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCCONTAINER_TASK                   0x903u
493*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCCONTAINER_THREAD                 0x904u
494*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_TASK_SNAPSHOT               0x905u /* task_snapshot_v2 */
495*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_THREAD_SNAPSHOT             0x906u /* thread_snapshot_v2, thread_snapshot_v3 */
496*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_DONATING_PIDS               0x907u /* int[] */
497*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_SHAREDCACHE_LOADINFO        0x908u /* dyld_shared_cache_loadinfo */
498*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_THREAD_NAME                 0x909u /* char[] */
499*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_KERN_STACKFRAME             0x90Au /* struct stack_snapshot_frame32 */
500*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_KERN_STACKFRAME64           0x90Bu /* struct stack_snapshot_frame64 */
501*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_USER_STACKFRAME             0x90Cu /* struct stack_snapshot_frame32 */
502*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_USER_STACKFRAME64           0x90Du /* struct stack_snapshot_frame64 */
503*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_BOOTARGS                    0x90Eu /* boot args string */
504*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_OSVERSION                   0x90Fu /* os version string */
505*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_KERN_PAGE_SIZE              0x910u /* kernel page size in uint32_t */
506*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_JETSAM_LEVEL                0x911u /* jetsam level in uint32_t */
507*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_DELTA_SINCE_TIMESTAMP       0x912u /* timestamp used for the delta stackshot */
508*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_KERN_STACKLR                0x913u /* uint32_t */
509*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_KERN_STACKLR64              0x914u /* uint64_t */
510*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_USER_STACKLR                0x915u /* uint32_t */
511*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_USER_STACKLR64              0x916u /* uint64_t */
512*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_NONRUNNABLE_TIDS            0x917u /* uint64_t */
513*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_NONRUNNABLE_TASKS           0x918u /* uint64_t */
514*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_CPU_TIMES                   0x919u /* struct stackshot_cpu_times or stackshot_cpu_times_v2 */
515*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_STACKSHOT_DURATION          0x91au /* struct stackshot_duration */
516*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_STACKSHOT_FAULT_STATS       0x91bu /* struct stackshot_fault_stats */
517*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_KERNELCACHE_LOADINFO        0x91cu /* kernelcache UUID -- same as KCDATA_TYPE_LIBRARY_LOADINFO64 */
518*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_THREAD_WAITINFO             0x91du /* struct stackshot_thread_waitinfo */
519*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_THREAD_GROUP_SNAPSHOT       0x91eu /* struct thread_group_snapshot{,_v2,_v3} */
520*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_THREAD_GROUP                0x91fu /* uint64_t */
521*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_JETSAM_COALITION_SNAPSHOT   0x920u /* struct jetsam_coalition_snapshot */
522*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_JETSAM_COALITION            0x921u /* uint64_t */
523*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_THREAD_POLICY_VERSION       0x922u /* THREAD_POLICY_INTERNAL_STRUCT_VERSION in uint32 */
524*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_INSTRS_CYCLES               0x923u /* struct instrs_cycles_snapshot_v2 */
525*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_USER_STACKTOP               0x924u /* struct stack_snapshot_stacktop */
526*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_ASID                        0x925u /* uint32_t */
527*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_PAGE_TABLES                 0x926u /* uint64_t */
528*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_SYS_SHAREDCACHE_LAYOUT      0x927u /* same as KCDATA_TYPE_LIBRARY_LOADINFO64 */
529*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_THREAD_DISPATCH_QUEUE_LABEL 0x928u /* dispatch queue label */
530*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_THREAD_TURNSTILEINFO        0x929u /* struct stackshot_thread_turnstileinfo */
531*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_TASK_CPU_ARCHITECTURE       0x92au /* struct stackshot_cpu_architecture */
532*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_LATENCY_INFO                0x92bu /* struct stackshot_latency_collection */
533*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_LATENCY_INFO_TASK           0x92cu /* struct stackshot_latency_task */
534*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_LATENCY_INFO_THREAD         0x92du /* struct stackshot_latency_thread */
535*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_LOADINFO64_TEXT_EXEC        0x92eu /* TEXT_EXEC load info -- same as KCDATA_TYPE_LIBRARY_LOADINFO64 */
536*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_AOTCACHE_LOADINFO           0x92fu /* struct dyld_aot_cache_uuid_info */
537*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_TRANSITIONING_TASK_SNAPSHOT 0x930u /* transitioning_task_snapshot */
538*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCCONTAINER_TRANSITIONING_TASK     0x931u
539*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_USER_ASYNC_START_INDEX      0x932u /* uint32_t index in user_stack of beginning of async stack */
540*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_USER_ASYNC_STACKLR64        0x933u /* uint64_t async stack pointers */
541*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCCONTAINER_PORTLABEL              0x934u /* container for port label info */
542*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_PORTLABEL                   0x935u /* struct stackshot_portlabel */
543*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_PORTLABEL_NAME              0x936u /* string port name */
544*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_DYLD_COMPACTINFO            0x937u /* binary blob of dyld info (variable size) */
545*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_SUSPENSION_INFO             0x938u /* struct stackshot_suspension_info */
546*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_SUSPENSION_SOURCE           0x939u /* struct stackshot_suspension_source */
547*5e3eaea3SApple OSS Distributions 
548*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_TASK_DELTA_SNAPSHOT         0x940u   /* task_delta_snapshot_v2 */
549*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_THREAD_DELTA_SNAPSHOT       0x941u /* thread_delta_snapshot_v* */
550*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCCONTAINER_SHAREDCACHE            0x942u /* container for shared cache info */
551*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_SHAREDCACHE_INFO            0x943u /* dyld_shared_cache_loadinfo_v2 */
552*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_SHAREDCACHE_AOTINFO         0x944u /* struct dyld_aot_cache_uuid_info */
553*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_SHAREDCACHE_ID              0x945u /* uint32_t in task: if we aren't attached to Primary, which one */
554*5e3eaea3SApple OSS Distributions #define STACKSHOT_KCTYPE_CODESIGNING_INFO            0x946u /* struct stackshot_task_codesigning_info */
555*5e3eaea3SApple OSS Distributions 
556*5e3eaea3SApple OSS Distributions 
557*5e3eaea3SApple OSS Distributions struct stack_snapshot_frame32 {
558*5e3eaea3SApple OSS Distributions 	uint32_t lr;
559*5e3eaea3SApple OSS Distributions 	uint32_t sp;
560*5e3eaea3SApple OSS Distributions };
561*5e3eaea3SApple OSS Distributions 
562*5e3eaea3SApple OSS Distributions struct stack_snapshot_frame64 {
563*5e3eaea3SApple OSS Distributions 	uint64_t lr;
564*5e3eaea3SApple OSS Distributions 	uint64_t sp;
565*5e3eaea3SApple OSS Distributions };
566*5e3eaea3SApple OSS Distributions 
567*5e3eaea3SApple OSS Distributions struct dyld_uuid_info_32 {
568*5e3eaea3SApple OSS Distributions 	uint32_t imageLoadAddress; /* base address image is mapped at */
569*5e3eaea3SApple OSS Distributions 	uuid_t   imageUUID;
570*5e3eaea3SApple OSS Distributions };
571*5e3eaea3SApple OSS Distributions 
572*5e3eaea3SApple OSS Distributions struct dyld_uuid_info_64 {
573*5e3eaea3SApple OSS Distributions 	uint64_t imageLoadAddress; /* XXX image slide */
574*5e3eaea3SApple OSS Distributions 	uuid_t   imageUUID;
575*5e3eaea3SApple OSS Distributions };
576*5e3eaea3SApple OSS Distributions 
577*5e3eaea3SApple OSS Distributions /*
578*5e3eaea3SApple OSS Distributions  * N.B.: Newer kernels output dyld_shared_cache_loadinfo structures
579*5e3eaea3SApple OSS Distributions  * instead of this, since the field names match their contents better.
580*5e3eaea3SApple OSS Distributions  */
581*5e3eaea3SApple OSS Distributions struct dyld_uuid_info_64_v2 {
582*5e3eaea3SApple OSS Distributions 	uint64_t imageLoadAddress; /* XXX image slide */
583*5e3eaea3SApple OSS Distributions 	uuid_t   imageUUID;
584*5e3eaea3SApple OSS Distributions 	/* end of version 1 of dyld_uuid_info_64. sizeof v1 was 24 */
585*5e3eaea3SApple OSS Distributions 	uint64_t imageSlidBaseAddress; /* slid base address or slid first mapping of image */
586*5e3eaea3SApple OSS Distributions };
587*5e3eaea3SApple OSS Distributions 
588*5e3eaea3SApple OSS Distributions enum dyld_shared_cache_flags {
589*5e3eaea3SApple OSS Distributions 	kSharedCacheSystemPrimary = 0x1, /* primary shared cache on the system; attached tasks will have kTaskSharedRegionSystem set */
590*5e3eaea3SApple OSS Distributions 	kSharedCacheDriverkit = 0x2, /* driverkit shared cache */
591*5e3eaea3SApple OSS Distributions 	kSharedCacheAOT = 0x4,    /* Rosetta shared cache */
592*5e3eaea3SApple OSS Distributions };
593*5e3eaea3SApple OSS Distributions 
594*5e3eaea3SApple OSS Distributions /*
595*5e3eaea3SApple OSS Distributions  * This is the renamed version of dyld_uuid_info_64 with more accurate
596*5e3eaea3SApple OSS Distributions  * field names, for STACKSHOT_KCTYPE_SHAREDCACHE_LOADINFO.  Any users
597*5e3eaea3SApple OSS Distributions  * must be aware of the dyld_uuid_info_64* version history and ensure
598*5e3eaea3SApple OSS Distributions  * the fields they are accessing are within the actual bounds.
599*5e3eaea3SApple OSS Distributions  *
600*5e3eaea3SApple OSS Distributions  * OLD_FIELD              NEW_FIELD
601*5e3eaea3SApple OSS Distributions  * imageLoadAddress       sharedCacheSlide
602*5e3eaea3SApple OSS Distributions  * imageUUID              sharedCacheUUID
603*5e3eaea3SApple OSS Distributions  * imageSlidBaseAddress   sharedCacheUnreliableSlidBaseAddress
604*5e3eaea3SApple OSS Distributions  * -                      sharedCacheSlidFirstMapping
605*5e3eaea3SApple OSS Distributions  * -                      sharedCacheID
606*5e3eaea3SApple OSS Distributions  * -                      sharedCacheFlags
607*5e3eaea3SApple OSS Distributions  */
608*5e3eaea3SApple OSS Distributions struct dyld_shared_cache_loadinfo_v2 {
609*5e3eaea3SApple OSS Distributions 	uint64_t sharedCacheSlide;      /* image slide value */
610*5e3eaea3SApple OSS Distributions 	uuid_t   sharedCacheUUID;
611*5e3eaea3SApple OSS Distributions 	/* end of version 1 of dyld_uuid_info_64. sizeof v1 was 24 */
612*5e3eaea3SApple OSS Distributions 	uint64_t sharedCacheUnreliableSlidBaseAddress;  /* for backwards-compatibility; use sharedCacheSlidFirstMapping if available */
613*5e3eaea3SApple OSS Distributions 	/* end of version 2 of dyld_uuid_info_64. sizeof v2 was 32 */
614*5e3eaea3SApple OSS Distributions 	uint64_t sharedCacheSlidFirstMapping; /* slid base address of first mapping */
615*5e3eaea3SApple OSS Distributions 	/* end of version 1 of dyld_shared_cache_loadinfo. sizeof was 40 */
616*5e3eaea3SApple OSS Distributions 	uint32_t sharedCacheID; /* ID of shared cache */
617*5e3eaea3SApple OSS Distributions 	uint32_t sharedCacheFlags;
618*5e3eaea3SApple OSS Distributions };
619*5e3eaea3SApple OSS Distributions 
620*5e3eaea3SApple OSS Distributions struct dyld_shared_cache_loadinfo {
621*5e3eaea3SApple OSS Distributions 	uint64_t sharedCacheSlide;      /* image slide value */
622*5e3eaea3SApple OSS Distributions 	uuid_t   sharedCacheUUID;
623*5e3eaea3SApple OSS Distributions 	/* end of version 1 of dyld_uuid_info_64. sizeof v1 was 24 */
624*5e3eaea3SApple OSS Distributions 	uint64_t sharedCacheUnreliableSlidBaseAddress;  /* for backwards-compatibility; use sharedCacheSlidFirstMapping if available */
625*5e3eaea3SApple OSS Distributions 	/* end of version 2 of dyld_uuid_info_64. sizeof v2 was 32 */
626*5e3eaea3SApple OSS Distributions 	uint64_t sharedCacheSlidFirstMapping; /* slid base address of first mapping */
627*5e3eaea3SApple OSS Distributions };
628*5e3eaea3SApple OSS Distributions 
629*5e3eaea3SApple OSS Distributions struct dyld_aot_cache_uuid_info {
630*5e3eaea3SApple OSS Distributions 	uint64_t x86SlidBaseAddress; /* slid first mapping address of x86 shared cache */
631*5e3eaea3SApple OSS Distributions 	uuid_t x86UUID; /* UUID of x86 shared cache */
632*5e3eaea3SApple OSS Distributions 	uint64_t aotSlidBaseAddress; /* slide first mapping address of aot cache */
633*5e3eaea3SApple OSS Distributions 	uuid_t aotUUID; /* UUID of aot shared cache */
634*5e3eaea3SApple OSS Distributions };
635*5e3eaea3SApple OSS Distributions 
636*5e3eaea3SApple OSS Distributions struct user32_dyld_uuid_info {
637*5e3eaea3SApple OSS Distributions 	uint32_t        imageLoadAddress;       /* base address image is mapped into */
638*5e3eaea3SApple OSS Distributions 	uuid_t                  imageUUID;                      /* UUID of image */
639*5e3eaea3SApple OSS Distributions };
640*5e3eaea3SApple OSS Distributions 
641*5e3eaea3SApple OSS Distributions struct user64_dyld_uuid_info {
642*5e3eaea3SApple OSS Distributions 	uint64_t        imageLoadAddress;       /* base address image is mapped into */
643*5e3eaea3SApple OSS Distributions 	uuid_t                  imageUUID;                      /* UUID of image */
644*5e3eaea3SApple OSS Distributions };
645*5e3eaea3SApple OSS Distributions 
646*5e3eaea3SApple OSS Distributions #define DYLD_AOT_IMAGE_KEY_SIZE 32
647*5e3eaea3SApple OSS Distributions 
648*5e3eaea3SApple OSS Distributions struct user64_dyld_aot_info {
649*5e3eaea3SApple OSS Distributions 	uint64_t x86LoadAddress;
650*5e3eaea3SApple OSS Distributions 	uint64_t aotLoadAddress;
651*5e3eaea3SApple OSS Distributions 	uint64_t aotImageSize;
652*5e3eaea3SApple OSS Distributions 	uint8_t  aotImageKey[DYLD_AOT_IMAGE_KEY_SIZE];
653*5e3eaea3SApple OSS Distributions };
654*5e3eaea3SApple OSS Distributions 
655*5e3eaea3SApple OSS Distributions enum task_snapshot_flags {
656*5e3eaea3SApple OSS Distributions 	/* k{User,Kernel}64_p (values 0x1 and 0x2) are defined in generic_snapshot_flags */
657*5e3eaea3SApple OSS Distributions 	kTaskRsrcFlagged                      = 0x4, // In the EXC_RESOURCE danger zone?
658*5e3eaea3SApple OSS Distributions 	kTerminatedSnapshot                   = 0x8,
659*5e3eaea3SApple OSS Distributions 	kPidSuspended                         = 0x10, // true for suspended task
660*5e3eaea3SApple OSS Distributions 	kFrozen                               = 0x20, // true for hibernated task (along with pidsuspended)
661*5e3eaea3SApple OSS Distributions 	kTaskDarwinBG                         = 0x40,
662*5e3eaea3SApple OSS Distributions 	kTaskExtDarwinBG                      = 0x80,
663*5e3eaea3SApple OSS Distributions 	kTaskVisVisible                       = 0x100,
664*5e3eaea3SApple OSS Distributions 	kTaskVisNonvisible                    = 0x200,
665*5e3eaea3SApple OSS Distributions 	kTaskIsForeground                     = 0x400,
666*5e3eaea3SApple OSS Distributions 	kTaskIsBoosted                        = 0x800,
667*5e3eaea3SApple OSS Distributions 	kTaskIsSuppressed                     = 0x1000,
668*5e3eaea3SApple OSS Distributions 	kTaskIsTimerThrottled                 = 0x2000, /* deprecated */
669*5e3eaea3SApple OSS Distributions 	kTaskIsImpDonor                       = 0x4000,
670*5e3eaea3SApple OSS Distributions 	kTaskIsLiveImpDonor                   = 0x8000,
671*5e3eaea3SApple OSS Distributions 	kTaskIsDirty                          = 0x10000,
672*5e3eaea3SApple OSS Distributions 	kTaskWqExceededConstrainedThreadLimit = 0x20000,
673*5e3eaea3SApple OSS Distributions 	kTaskWqExceededTotalThreadLimit       = 0x40000,
674*5e3eaea3SApple OSS Distributions 	kTaskWqFlagsAvailable                 = 0x80000,
675*5e3eaea3SApple OSS Distributions 	kTaskUUIDInfoFaultedIn                = 0x100000, /* successfully faulted in some UUID info */
676*5e3eaea3SApple OSS Distributions 	kTaskUUIDInfoMissing                  = 0x200000, /* some UUID info was paged out */
677*5e3eaea3SApple OSS Distributions 	kTaskUUIDInfoTriedFault               = 0x400000, /* tried to fault in UUID info */
678*5e3eaea3SApple OSS Distributions 	kTaskSharedRegionInfoUnavailable      = 0x800000,  /* shared region info unavailable */
679*5e3eaea3SApple OSS Distributions 	kTaskTALEngaged                       = 0x1000000,
680*5e3eaea3SApple OSS Distributions 	/* 0x2000000 unused */
681*5e3eaea3SApple OSS Distributions 	kTaskIsDirtyTracked                   = 0x4000000,
682*5e3eaea3SApple OSS Distributions 	kTaskAllowIdleExit                    = 0x8000000,
683*5e3eaea3SApple OSS Distributions 	kTaskIsTranslated                     = 0x10000000,
684*5e3eaea3SApple OSS Distributions 	kTaskSharedRegionNone                 = 0x20000000,     /* task doesn't have a shared region */
685*5e3eaea3SApple OSS Distributions 	kTaskSharedRegionSystem               = 0x40000000,     /* task attached to region with kSharedCacheSystemPrimary set */
686*5e3eaea3SApple OSS Distributions 	kTaskSharedRegionOther                = 0x80000000,     /* task is attached to a different shared region */
687*5e3eaea3SApple OSS Distributions 	kTaskDyldCompactInfoNone              = 0x100000000,
688*5e3eaea3SApple OSS Distributions 	kTaskDyldCompactInfoTooBig            = 0x200000000,
689*5e3eaea3SApple OSS Distributions 	kTaskDyldCompactInfoFaultedIn         = 0x400000000,
690*5e3eaea3SApple OSS Distributions 	kTaskDyldCompactInfoMissing           = 0x800000000,
691*5e3eaea3SApple OSS Distributions 	kTaskDyldCompactInfoTriedFault        = 0x1000000000,
692*5e3eaea3SApple OSS Distributions }; // Note: Add any new flags to kcdata.py (ts_ss_flags)
693*5e3eaea3SApple OSS Distributions 
694*5e3eaea3SApple OSS Distributions enum task_transition_type {
695*5e3eaea3SApple OSS Distributions 	kTaskIsTerminated                      = 0x1,// Past LPEXIT
696*5e3eaea3SApple OSS Distributions };
697*5e3eaea3SApple OSS Distributions 
698*5e3eaea3SApple OSS Distributions enum thread_snapshot_flags {
699*5e3eaea3SApple OSS Distributions 	/* k{User,Kernel}64_p (values 0x1 and 0x2) are defined in generic_snapshot_flags */
700*5e3eaea3SApple OSS Distributions 	kHasDispatchSerial      = 0x4,
701*5e3eaea3SApple OSS Distributions 	kStacksPCOnly           = 0x8,    /* Stack traces have no frame pointers. */
702*5e3eaea3SApple OSS Distributions 	kThreadDarwinBG         = 0x10,   /* Thread is darwinbg */
703*5e3eaea3SApple OSS Distributions 	kThreadIOPassive        = 0x20,   /* Thread uses passive IO */
704*5e3eaea3SApple OSS Distributions 	kThreadSuspended        = 0x40,   /* Thread is suspended */
705*5e3eaea3SApple OSS Distributions 	kThreadTruncatedBT      = 0x80,   /* Unmapped pages caused truncated backtrace */
706*5e3eaea3SApple OSS Distributions 	kGlobalForcedIdle       = 0x100,  /* Thread performs global forced idle */
707*5e3eaea3SApple OSS Distributions 	kThreadFaultedBT        = 0x200,  /* Some thread stack pages were faulted in as part of BT */
708*5e3eaea3SApple OSS Distributions 	kThreadTriedFaultBT     = 0x400,  /* We tried to fault in thread stack pages as part of BT */
709*5e3eaea3SApple OSS Distributions 	kThreadOnCore           = 0x800,  /* Thread was on-core when we entered debugger context */
710*5e3eaea3SApple OSS Distributions 	kThreadIdleWorker       = 0x1000, /* Thread is an idle libpthread worker thread */
711*5e3eaea3SApple OSS Distributions 	kThreadMain             = 0x2000, /* Thread is the main thread */
712*5e3eaea3SApple OSS Distributions 	kThreadTruncKernBT      = 0x4000, /* Unmapped pages caused truncated kernel BT */
713*5e3eaea3SApple OSS Distributions 	kThreadTruncUserBT      = 0x8000, /* Unmapped pages caused truncated user BT */
714*5e3eaea3SApple OSS Distributions 	kThreadTruncUserAsyncBT = 0x10000, /* Unmapped pages caused truncated user async BT */
715*5e3eaea3SApple OSS Distributions }; // Note: Add any new flags to kcdata.py (ths_ss_flags)
716*5e3eaea3SApple OSS Distributions 
717*5e3eaea3SApple OSS Distributions struct mem_and_io_snapshot {
718*5e3eaea3SApple OSS Distributions 	uint32_t        snapshot_magic;
719*5e3eaea3SApple OSS Distributions 	uint32_t        free_pages;
720*5e3eaea3SApple OSS Distributions 	uint32_t        active_pages;
721*5e3eaea3SApple OSS Distributions 	uint32_t        inactive_pages;
722*5e3eaea3SApple OSS Distributions 	uint32_t        purgeable_pages;
723*5e3eaea3SApple OSS Distributions 	uint32_t        wired_pages;
724*5e3eaea3SApple OSS Distributions 	uint32_t        speculative_pages;
725*5e3eaea3SApple OSS Distributions 	uint32_t        throttled_pages;
726*5e3eaea3SApple OSS Distributions 	uint32_t        filebacked_pages;
727*5e3eaea3SApple OSS Distributions 	uint32_t        compressions;
728*5e3eaea3SApple OSS Distributions 	uint32_t        decompressions;
729*5e3eaea3SApple OSS Distributions 	uint32_t        compressor_size;
730*5e3eaea3SApple OSS Distributions 	int32_t         busy_buffer_count;
731*5e3eaea3SApple OSS Distributions 	uint32_t        pages_wanted;
732*5e3eaea3SApple OSS Distributions 	uint32_t        pages_reclaimed;
733*5e3eaea3SApple OSS Distributions 	uint8_t         pages_wanted_reclaimed_valid; // did mach_vm_pressure_monitor succeed?
734*5e3eaea3SApple OSS Distributions } __attribute__((packed));
735*5e3eaea3SApple OSS Distributions 
736*5e3eaea3SApple OSS Distributions /* SS_TH_* macros are for ths_state */
737*5e3eaea3SApple OSS Distributions #define SS_TH_WAIT 0x01       /* queued for waiting */
738*5e3eaea3SApple OSS Distributions #define SS_TH_SUSP 0x02       /* stopped or requested to stop */
739*5e3eaea3SApple OSS Distributions #define SS_TH_RUN 0x04        /* running or on runq */
740*5e3eaea3SApple OSS Distributions #define SS_TH_UNINT 0x08      /* waiting uninteruptibly */
741*5e3eaea3SApple OSS Distributions #define SS_TH_TERMINATE 0x10  /* halted at termination */
742*5e3eaea3SApple OSS Distributions #define SS_TH_TERMINATE2 0x20 /* added to termination queue */
743*5e3eaea3SApple OSS Distributions #define SS_TH_IDLE 0x80       /* idling processor */
744*5e3eaea3SApple OSS Distributions 
745*5e3eaea3SApple OSS Distributions struct thread_snapshot_v2 {
746*5e3eaea3SApple OSS Distributions 	uint64_t  ths_thread_id;
747*5e3eaea3SApple OSS Distributions 	uint64_t  ths_wait_event;
748*5e3eaea3SApple OSS Distributions 	uint64_t  ths_continuation;
749*5e3eaea3SApple OSS Distributions 	uint64_t  ths_total_syscalls;
750*5e3eaea3SApple OSS Distributions 	uint64_t  ths_voucher_identifier;
751*5e3eaea3SApple OSS Distributions 	uint64_t  ths_dqserialnum;
752*5e3eaea3SApple OSS Distributions 	uint64_t  ths_user_time;
753*5e3eaea3SApple OSS Distributions 	uint64_t  ths_sys_time;
754*5e3eaea3SApple OSS Distributions 	uint64_t  ths_ss_flags;
755*5e3eaea3SApple OSS Distributions 	uint64_t  ths_last_run_time;
756*5e3eaea3SApple OSS Distributions 	uint64_t  ths_last_made_runnable_time;
757*5e3eaea3SApple OSS Distributions 	uint32_t  ths_state;
758*5e3eaea3SApple OSS Distributions 	uint32_t  ths_sched_flags;
759*5e3eaea3SApple OSS Distributions 	int16_t   ths_base_priority;
760*5e3eaea3SApple OSS Distributions 	int16_t   ths_sched_priority;
761*5e3eaea3SApple OSS Distributions 	uint8_t   ths_eqos;
762*5e3eaea3SApple OSS Distributions 	uint8_t ths_rqos;
763*5e3eaea3SApple OSS Distributions 	uint8_t ths_rqos_override;
764*5e3eaea3SApple OSS Distributions 	uint8_t ths_io_tier;
765*5e3eaea3SApple OSS Distributions } __attribute__((packed));
766*5e3eaea3SApple OSS Distributions 
767*5e3eaea3SApple OSS Distributions struct thread_snapshot_v3 {
768*5e3eaea3SApple OSS Distributions 	uint64_t ths_thread_id;
769*5e3eaea3SApple OSS Distributions 	uint64_t ths_wait_event;
770*5e3eaea3SApple OSS Distributions 	uint64_t ths_continuation;
771*5e3eaea3SApple OSS Distributions 	uint64_t ths_total_syscalls;
772*5e3eaea3SApple OSS Distributions 	uint64_t ths_voucher_identifier;
773*5e3eaea3SApple OSS Distributions 	uint64_t ths_dqserialnum;
774*5e3eaea3SApple OSS Distributions 	uint64_t ths_user_time;
775*5e3eaea3SApple OSS Distributions 	uint64_t ths_sys_time;
776*5e3eaea3SApple OSS Distributions 	uint64_t ths_ss_flags;
777*5e3eaea3SApple OSS Distributions 	uint64_t ths_last_run_time;
778*5e3eaea3SApple OSS Distributions 	uint64_t ths_last_made_runnable_time;
779*5e3eaea3SApple OSS Distributions 	uint32_t ths_state;
780*5e3eaea3SApple OSS Distributions 	uint32_t ths_sched_flags;
781*5e3eaea3SApple OSS Distributions 	int16_t ths_base_priority;
782*5e3eaea3SApple OSS Distributions 	int16_t ths_sched_priority;
783*5e3eaea3SApple OSS Distributions 	uint8_t ths_eqos;
784*5e3eaea3SApple OSS Distributions 	uint8_t ths_rqos;
785*5e3eaea3SApple OSS Distributions 	uint8_t ths_rqos_override;
786*5e3eaea3SApple OSS Distributions 	uint8_t ths_io_tier;
787*5e3eaea3SApple OSS Distributions 	uint64_t ths_thread_t;
788*5e3eaea3SApple OSS Distributions } __attribute__((packed));
789*5e3eaea3SApple OSS Distributions 
790*5e3eaea3SApple OSS Distributions 
791*5e3eaea3SApple OSS Distributions struct thread_snapshot_v4 {
792*5e3eaea3SApple OSS Distributions 	uint64_t ths_thread_id;
793*5e3eaea3SApple OSS Distributions 	uint64_t ths_wait_event;
794*5e3eaea3SApple OSS Distributions 	uint64_t ths_continuation;
795*5e3eaea3SApple OSS Distributions 	uint64_t ths_total_syscalls;
796*5e3eaea3SApple OSS Distributions 	uint64_t ths_voucher_identifier;
797*5e3eaea3SApple OSS Distributions 	uint64_t ths_dqserialnum;
798*5e3eaea3SApple OSS Distributions 	uint64_t ths_user_time;
799*5e3eaea3SApple OSS Distributions 	uint64_t ths_sys_time;
800*5e3eaea3SApple OSS Distributions 	uint64_t ths_ss_flags;
801*5e3eaea3SApple OSS Distributions 	uint64_t ths_last_run_time;
802*5e3eaea3SApple OSS Distributions 	uint64_t ths_last_made_runnable_time;
803*5e3eaea3SApple OSS Distributions 	uint32_t ths_state;
804*5e3eaea3SApple OSS Distributions 	uint32_t ths_sched_flags;
805*5e3eaea3SApple OSS Distributions 	int16_t ths_base_priority;
806*5e3eaea3SApple OSS Distributions 	int16_t ths_sched_priority;
807*5e3eaea3SApple OSS Distributions 	uint8_t ths_eqos;
808*5e3eaea3SApple OSS Distributions 	uint8_t ths_rqos;
809*5e3eaea3SApple OSS Distributions 	uint8_t ths_rqos_override;
810*5e3eaea3SApple OSS Distributions 	uint8_t ths_io_tier;
811*5e3eaea3SApple OSS Distributions 	uint64_t ths_thread_t;
812*5e3eaea3SApple OSS Distributions 	uint64_t ths_requested_policy;
813*5e3eaea3SApple OSS Distributions 	uint64_t ths_effective_policy;
814*5e3eaea3SApple OSS Distributions } __attribute__((packed));
815*5e3eaea3SApple OSS Distributions 
816*5e3eaea3SApple OSS Distributions 
817*5e3eaea3SApple OSS Distributions struct thread_group_snapshot {
818*5e3eaea3SApple OSS Distributions 	uint64_t tgs_id;
819*5e3eaea3SApple OSS Distributions 	char tgs_name[16];
820*5e3eaea3SApple OSS Distributions } __attribute__((packed));
821*5e3eaea3SApple OSS Distributions 
822*5e3eaea3SApple OSS Distributions /*
823*5e3eaea3SApple OSS Distributions  * In general these flags mirror their THREAD_GROUP_FLAGS_ counterparts.
824*5e3eaea3SApple OSS Distributions  * THREAD_GROUP_FLAGS_UI_APP was repurposed and THREAD_GROUP_FLAGS_APPLICATION
825*5e3eaea3SApple OSS Distributions  * introduced to take its place. To remain compatible, kThreadGroupUIApp is
826*5e3eaea3SApple OSS Distributions  * kept around and kThreadGroupUIApplication introduced.
827*5e3eaea3SApple OSS Distributions  */
828*5e3eaea3SApple OSS Distributions enum thread_group_flags {
829*5e3eaea3SApple OSS Distributions 	kThreadGroupEfficient     = 0x1,
830*5e3eaea3SApple OSS Distributions 	kThreadGroupApplication   = 0x2,
831*5e3eaea3SApple OSS Distributions 	kThreadGroupUIApp         = 0x2,
832*5e3eaea3SApple OSS Distributions 	kThreadGroupCritical      = 0x4,
833*5e3eaea3SApple OSS Distributions 	kThreadGroupBestEffort    = 0x8,
834*5e3eaea3SApple OSS Distributions 	kThreadGroupUIApplication = 0x100,
835*5e3eaea3SApple OSS Distributions 	kThreadGroupManaged       = 0x200,
836*5e3eaea3SApple OSS Distributions 	kThreadGroupStrictTimers  = 0x400,
837*5e3eaea3SApple OSS Distributions }; // Note: Add any new flags to kcdata.py (tgs_flags)
838*5e3eaea3SApple OSS Distributions 
839*5e3eaea3SApple OSS Distributions struct thread_group_snapshot_v2 {
840*5e3eaea3SApple OSS Distributions 	uint64_t tgs_id;
841*5e3eaea3SApple OSS Distributions 	char tgs_name[16];
842*5e3eaea3SApple OSS Distributions 	uint64_t tgs_flags;
843*5e3eaea3SApple OSS Distributions } __attribute__((packed));
844*5e3eaea3SApple OSS Distributions 
845*5e3eaea3SApple OSS Distributions struct thread_group_snapshot_v3 {
846*5e3eaea3SApple OSS Distributions 	uint64_t tgs_id;
847*5e3eaea3SApple OSS Distributions 	char tgs_name[16];
848*5e3eaea3SApple OSS Distributions 	uint64_t tgs_flags;
849*5e3eaea3SApple OSS Distributions 	char tgs_name_cont[16];
850*5e3eaea3SApple OSS Distributions } __attribute__((packed));
851*5e3eaea3SApple OSS Distributions 
852*5e3eaea3SApple OSS Distributions enum coalition_flags {
853*5e3eaea3SApple OSS Distributions 	kCoalitionTermRequested = 0x1,
854*5e3eaea3SApple OSS Distributions 	kCoalitionTerminated    = 0x2,
855*5e3eaea3SApple OSS Distributions 	kCoalitionReaped        = 0x4,
856*5e3eaea3SApple OSS Distributions 	kCoalitionPrivileged    = 0x8,
857*5e3eaea3SApple OSS Distributions }; // Note: Add any new flags to kcdata.py (jcs_flags)
858*5e3eaea3SApple OSS Distributions 
859*5e3eaea3SApple OSS Distributions struct jetsam_coalition_snapshot {
860*5e3eaea3SApple OSS Distributions 	uint64_t jcs_id;
861*5e3eaea3SApple OSS Distributions 	uint64_t jcs_flags;
862*5e3eaea3SApple OSS Distributions 	uint64_t jcs_thread_group;
863*5e3eaea3SApple OSS Distributions 	uint64_t jcs_leader_task_uniqueid;
864*5e3eaea3SApple OSS Distributions } __attribute__((packed));
865*5e3eaea3SApple OSS Distributions 
866*5e3eaea3SApple OSS Distributions struct instrs_cycles_snapshot {
867*5e3eaea3SApple OSS Distributions 	uint64_t ics_instructions;
868*5e3eaea3SApple OSS Distributions 	uint64_t ics_cycles;
869*5e3eaea3SApple OSS Distributions } __attribute__((packed));
870*5e3eaea3SApple OSS Distributions 
871*5e3eaea3SApple OSS Distributions struct instrs_cycles_snapshot_v2 {
872*5e3eaea3SApple OSS Distributions 	uint64_t ics_instructions;
873*5e3eaea3SApple OSS Distributions 	uint64_t ics_cycles;
874*5e3eaea3SApple OSS Distributions 	uint64_t ics_p_instructions;
875*5e3eaea3SApple OSS Distributions 	uint64_t ics_p_cycles;
876*5e3eaea3SApple OSS Distributions } __attribute__((packed));
877*5e3eaea3SApple OSS Distributions 
878*5e3eaea3SApple OSS Distributions struct thread_delta_snapshot_v2 {
879*5e3eaea3SApple OSS Distributions 	uint64_t  tds_thread_id;
880*5e3eaea3SApple OSS Distributions 	uint64_t  tds_voucher_identifier;
881*5e3eaea3SApple OSS Distributions 	uint64_t  tds_ss_flags;
882*5e3eaea3SApple OSS Distributions 	uint64_t  tds_last_made_runnable_time;
883*5e3eaea3SApple OSS Distributions 	uint32_t  tds_state;
884*5e3eaea3SApple OSS Distributions 	uint32_t  tds_sched_flags;
885*5e3eaea3SApple OSS Distributions 	int16_t   tds_base_priority;
886*5e3eaea3SApple OSS Distributions 	int16_t   tds_sched_priority;
887*5e3eaea3SApple OSS Distributions 	uint8_t   tds_eqos;
888*5e3eaea3SApple OSS Distributions 	uint8_t   tds_rqos;
889*5e3eaea3SApple OSS Distributions 	uint8_t   tds_rqos_override;
890*5e3eaea3SApple OSS Distributions 	uint8_t   tds_io_tier;
891*5e3eaea3SApple OSS Distributions } __attribute__ ((packed));
892*5e3eaea3SApple OSS Distributions 
893*5e3eaea3SApple OSS Distributions struct thread_delta_snapshot_v3 {
894*5e3eaea3SApple OSS Distributions 	uint64_t  tds_thread_id;
895*5e3eaea3SApple OSS Distributions 	uint64_t  tds_voucher_identifier;
896*5e3eaea3SApple OSS Distributions 	uint64_t  tds_ss_flags;
897*5e3eaea3SApple OSS Distributions 	uint64_t  tds_last_made_runnable_time;
898*5e3eaea3SApple OSS Distributions 	uint32_t  tds_state;
899*5e3eaea3SApple OSS Distributions 	uint32_t  tds_sched_flags;
900*5e3eaea3SApple OSS Distributions 	int16_t   tds_base_priority;
901*5e3eaea3SApple OSS Distributions 	int16_t   tds_sched_priority;
902*5e3eaea3SApple OSS Distributions 	uint8_t   tds_eqos;
903*5e3eaea3SApple OSS Distributions 	uint8_t   tds_rqos;
904*5e3eaea3SApple OSS Distributions 	uint8_t   tds_rqos_override;
905*5e3eaea3SApple OSS Distributions 	uint8_t   tds_io_tier;
906*5e3eaea3SApple OSS Distributions 	uint64_t  tds_requested_policy;
907*5e3eaea3SApple OSS Distributions 	uint64_t  tds_effective_policy;
908*5e3eaea3SApple OSS Distributions } __attribute__ ((packed));
909*5e3eaea3SApple OSS Distributions 
910*5e3eaea3SApple OSS Distributions struct io_stats_snapshot {
911*5e3eaea3SApple OSS Distributions 	/*
912*5e3eaea3SApple OSS Distributions 	 * I/O Statistics
913*5e3eaea3SApple OSS Distributions 	 * XXX: These fields must be together.
914*5e3eaea3SApple OSS Distributions 	 */
915*5e3eaea3SApple OSS Distributions 	uint64_t         ss_disk_reads_count;
916*5e3eaea3SApple OSS Distributions 	uint64_t         ss_disk_reads_size;
917*5e3eaea3SApple OSS Distributions 	uint64_t         ss_disk_writes_count;
918*5e3eaea3SApple OSS Distributions 	uint64_t         ss_disk_writes_size;
919*5e3eaea3SApple OSS Distributions 	uint64_t         ss_io_priority_count[STACKSHOT_IO_NUM_PRIORITIES];
920*5e3eaea3SApple OSS Distributions 	uint64_t         ss_io_priority_size[STACKSHOT_IO_NUM_PRIORITIES];
921*5e3eaea3SApple OSS Distributions 	uint64_t         ss_paging_count;
922*5e3eaea3SApple OSS Distributions 	uint64_t         ss_paging_size;
923*5e3eaea3SApple OSS Distributions 	uint64_t         ss_non_paging_count;
924*5e3eaea3SApple OSS Distributions 	uint64_t         ss_non_paging_size;
925*5e3eaea3SApple OSS Distributions 	uint64_t         ss_data_count;
926*5e3eaea3SApple OSS Distributions 	uint64_t         ss_data_size;
927*5e3eaea3SApple OSS Distributions 	uint64_t         ss_metadata_count;
928*5e3eaea3SApple OSS Distributions 	uint64_t         ss_metadata_size;
929*5e3eaea3SApple OSS Distributions 	/* XXX: I/O Statistics end */
930*5e3eaea3SApple OSS Distributions } __attribute__ ((packed));
931*5e3eaea3SApple OSS Distributions 
932*5e3eaea3SApple OSS Distributions struct task_snapshot_v2 {
933*5e3eaea3SApple OSS Distributions 	uint64_t  ts_unique_pid;
934*5e3eaea3SApple OSS Distributions 	uint64_t  ts_ss_flags;
935*5e3eaea3SApple OSS Distributions 	uint64_t  ts_user_time_in_terminated_threads;
936*5e3eaea3SApple OSS Distributions 	uint64_t  ts_system_time_in_terminated_threads;
937*5e3eaea3SApple OSS Distributions 	uint64_t  ts_p_start_sec;
938*5e3eaea3SApple OSS Distributions 	uint64_t  ts_task_size;
939*5e3eaea3SApple OSS Distributions 	uint64_t  ts_max_resident_size;
940*5e3eaea3SApple OSS Distributions 	uint32_t  ts_suspend_count;
941*5e3eaea3SApple OSS Distributions 	uint32_t  ts_faults;
942*5e3eaea3SApple OSS Distributions 	uint32_t  ts_pageins;
943*5e3eaea3SApple OSS Distributions 	uint32_t  ts_cow_faults;
944*5e3eaea3SApple OSS Distributions 	uint32_t  ts_was_throttled;
945*5e3eaea3SApple OSS Distributions 	uint32_t  ts_did_throttle;
946*5e3eaea3SApple OSS Distributions 	uint32_t  ts_latency_qos;
947*5e3eaea3SApple OSS Distributions 	int32_t   ts_pid;
948*5e3eaea3SApple OSS Distributions 	char      ts_p_comm[32];
949*5e3eaea3SApple OSS Distributions } __attribute__ ((packed));
950*5e3eaea3SApple OSS Distributions 
951*5e3eaea3SApple OSS Distributions struct transitioning_task_snapshot {
952*5e3eaea3SApple OSS Distributions 	uint64_t  tts_unique_pid;
953*5e3eaea3SApple OSS Distributions 	uint64_t  tts_ss_flags;
954*5e3eaea3SApple OSS Distributions 	uint64_t  tts_transition_type;
955*5e3eaea3SApple OSS Distributions 	int32_t   tts_pid;
956*5e3eaea3SApple OSS Distributions 	char      tts_p_comm[32];
957*5e3eaea3SApple OSS Distributions } __attribute__ ((packed));
958*5e3eaea3SApple OSS Distributions 
959*5e3eaea3SApple OSS Distributions struct task_delta_snapshot_v2 {
960*5e3eaea3SApple OSS Distributions 	uint64_t  tds_unique_pid;
961*5e3eaea3SApple OSS Distributions 	uint64_t  tds_ss_flags;
962*5e3eaea3SApple OSS Distributions 	uint64_t  tds_user_time_in_terminated_threads;
963*5e3eaea3SApple OSS Distributions 	uint64_t  tds_system_time_in_terminated_threads;
964*5e3eaea3SApple OSS Distributions 	uint64_t  tds_task_size;
965*5e3eaea3SApple OSS Distributions 	uint64_t  tds_max_resident_size;
966*5e3eaea3SApple OSS Distributions 	uint32_t  tds_suspend_count;
967*5e3eaea3SApple OSS Distributions 	uint32_t  tds_faults;
968*5e3eaea3SApple OSS Distributions 	uint32_t  tds_pageins;
969*5e3eaea3SApple OSS Distributions 	uint32_t  tds_cow_faults;
970*5e3eaea3SApple OSS Distributions 	uint32_t  tds_was_throttled;
971*5e3eaea3SApple OSS Distributions 	uint32_t  tds_did_throttle;
972*5e3eaea3SApple OSS Distributions 	uint32_t  tds_latency_qos;
973*5e3eaea3SApple OSS Distributions } __attribute__ ((packed));
974*5e3eaea3SApple OSS Distributions 
975*5e3eaea3SApple OSS Distributions #define KCDATA_INVALID_CS_TRUST_LEVEL 0xffffffff
976*5e3eaea3SApple OSS Distributions struct stackshot_task_codesigning_info {
977*5e3eaea3SApple OSS Distributions 	uint64_t csflags;
978*5e3eaea3SApple OSS Distributions 	uint32_t cs_trust_level;
979*5e3eaea3SApple OSS Distributions } __attribute__ ((packed));
980*5e3eaea3SApple OSS Distributions 
981*5e3eaea3SApple OSS Distributions struct stackshot_cpu_times {
982*5e3eaea3SApple OSS Distributions 	uint64_t user_usec;
983*5e3eaea3SApple OSS Distributions 	uint64_t system_usec;
984*5e3eaea3SApple OSS Distributions } __attribute__((packed));
985*5e3eaea3SApple OSS Distributions 
986*5e3eaea3SApple OSS Distributions struct stackshot_cpu_times_v2 {
987*5e3eaea3SApple OSS Distributions 	uint64_t user_usec;
988*5e3eaea3SApple OSS Distributions 	uint64_t system_usec;
989*5e3eaea3SApple OSS Distributions 	uint64_t runnable_usec;
990*5e3eaea3SApple OSS Distributions } __attribute__((packed));
991*5e3eaea3SApple OSS Distributions 
992*5e3eaea3SApple OSS Distributions struct stackshot_duration {
993*5e3eaea3SApple OSS Distributions 	uint64_t stackshot_duration;
994*5e3eaea3SApple OSS Distributions 	uint64_t stackshot_duration_outer;
995*5e3eaea3SApple OSS Distributions } __attribute__((packed));
996*5e3eaea3SApple OSS Distributions 
997*5e3eaea3SApple OSS Distributions struct stackshot_duration_v2 {
998*5e3eaea3SApple OSS Distributions 	uint64_t stackshot_duration;
999*5e3eaea3SApple OSS Distributions 	uint64_t stackshot_duration_outer;
1000*5e3eaea3SApple OSS Distributions 	uint64_t stackshot_duration_prior;
1001*5e3eaea3SApple OSS Distributions } __attribute__((packed));
1002*5e3eaea3SApple OSS Distributions 
1003*5e3eaea3SApple OSS Distributions struct stackshot_fault_stats {
1004*5e3eaea3SApple OSS Distributions 	uint32_t sfs_pages_faulted_in;      /* number of pages faulted in using KDP fault path */
1005*5e3eaea3SApple OSS Distributions 	uint64_t sfs_time_spent_faulting;   /* MATUs spent faulting */
1006*5e3eaea3SApple OSS Distributions 	uint64_t sfs_system_max_fault_time; /* MATUs fault time limit per stackshot */
1007*5e3eaea3SApple OSS Distributions 	uint8_t  sfs_stopped_faulting;      /* we stopped decompressing because we hit the limit */
1008*5e3eaea3SApple OSS Distributions } __attribute__((packed));
1009*5e3eaea3SApple OSS Distributions 
1010*5e3eaea3SApple OSS Distributions typedef struct stackshot_thread_waitinfo {
1011*5e3eaea3SApple OSS Distributions 	uint64_t owner;         /* The thread that owns the object */
1012*5e3eaea3SApple OSS Distributions 	uint64_t waiter;        /* The thread that's waiting on the object */
1013*5e3eaea3SApple OSS Distributions 	uint64_t context;       /* A context uniquely identifying the object */
1014*5e3eaea3SApple OSS Distributions 	uint8_t wait_type;      /* The type of object that the thread is waiting on */
1015*5e3eaea3SApple OSS Distributions } __attribute__((packed)) thread_waitinfo_t;
1016*5e3eaea3SApple OSS Distributions 
1017*5e3eaea3SApple OSS Distributions typedef struct stackshot_thread_waitinfo_v2 {
1018*5e3eaea3SApple OSS Distributions 	uint64_t owner;         /* The thread that owns the object */
1019*5e3eaea3SApple OSS Distributions 	uint64_t waiter;        /* The thread that's waiting on the object */
1020*5e3eaea3SApple OSS Distributions 	uint64_t context;       /* A context uniquely identifying the object */
1021*5e3eaea3SApple OSS Distributions 	uint8_t wait_type;      /* The type of object that the thread is waiting on */
1022*5e3eaea3SApple OSS Distributions 	int16_t portlabel_id;   /* matches to a stackshot_portlabel, or NONE or MISSING */
1023*5e3eaea3SApple OSS Distributions 	uint32_t wait_flags;    /* info about the wait */
1024*5e3eaea3SApple OSS Distributions #define STACKSHOT_WAITINFO_FLAGS_SPECIALREPLY 0x1  /* We're waiting on a special reply port */
1025*5e3eaea3SApple OSS Distributions } __attribute__((packed)) thread_waitinfo_v2_t;
1026*5e3eaea3SApple OSS Distributions 
1027*5e3eaea3SApple OSS Distributions 
1028*5e3eaea3SApple OSS Distributions typedef struct stackshot_thread_turnstileinfo {
1029*5e3eaea3SApple OSS Distributions 	uint64_t waiter;        /* The thread that's waiting on the object */
1030*5e3eaea3SApple OSS Distributions 	uint64_t turnstile_context; /* Associated data (either thread id, or workq addr) */
1031*5e3eaea3SApple OSS Distributions 	uint8_t turnstile_priority;
1032*5e3eaea3SApple OSS Distributions 	uint8_t number_of_hops;
1033*5e3eaea3SApple OSS Distributions 	uint64_t turnstile_flags;               /* see below */
1034*5e3eaea3SApple OSS Distributions } __attribute__((packed)) thread_turnstileinfo_t;
1035*5e3eaea3SApple OSS Distributions 
1036*5e3eaea3SApple OSS Distributions typedef struct stackshot_thread_turnstileinfo_v2 {
1037*5e3eaea3SApple OSS Distributions 	uint64_t waiter;        /* The thread that's waiting on the object */
1038*5e3eaea3SApple OSS Distributions 	uint64_t turnstile_context; /* Associated data (either thread id, or workq addr) */
1039*5e3eaea3SApple OSS Distributions 	uint8_t turnstile_priority;
1040*5e3eaea3SApple OSS Distributions 	uint8_t number_of_hops;
1041*5e3eaea3SApple OSS Distributions #define STACKSHOT_TURNSTILE_STATUS_UNKNOWN         0x01   /* The final inheritor is unknown (bug?) */
1042*5e3eaea3SApple OSS Distributions #define STACKSHOT_TURNSTILE_STATUS_LOCKED_WAITQ    0x02   /* A waitq was found to be locked */
1043*5e3eaea3SApple OSS Distributions #define STACKSHOT_TURNSTILE_STATUS_WORKQUEUE       0x04   /* The final inheritor is a workqueue */
1044*5e3eaea3SApple OSS Distributions #define STACKSHOT_TURNSTILE_STATUS_THREAD          0x08   /* The final inheritor is a thread */
1045*5e3eaea3SApple OSS Distributions #define STACKSHOT_TURNSTILE_STATUS_BLOCKED_ON_TASK 0x10   /* blocked on task, dind't find thread */
1046*5e3eaea3SApple OSS Distributions #define STACKSHOT_TURNSTILE_STATUS_HELD_IPLOCK     0x20   /* the ip_lock was held */
1047*5e3eaea3SApple OSS Distributions #define STACKSHOT_TURNSTILE_STATUS_SENDPORT        0x40   /* port_labelid was from a send port */
1048*5e3eaea3SApple OSS Distributions #define STACKSHOT_TURNSTILE_STATUS_RECEIVEPORT     0x80   /* port_labelid was from a receive port */
1049*5e3eaea3SApple OSS Distributions 	uint64_t turnstile_flags; // Note: Add any new flags to kcdata.py (turnstile_flags)
1050*5e3eaea3SApple OSS Distributions 	int16_t portlabel_id;   /* matches to a stackshot_portlabel, or NONE or MISSING */
1051*5e3eaea3SApple OSS Distributions } __attribute__((packed)) thread_turnstileinfo_v2_t;
1052*5e3eaea3SApple OSS Distributions 
1053*5e3eaea3SApple OSS Distributions #define STACKSHOT_TURNSTILE_STATUS_PORTFLAGS (STACKSHOT_TURNSTILE_STATUS_SENDPORT | STACKSHOT_TURNSTILE_STATUS_RECEIVEPORT)
1054*5e3eaea3SApple OSS Distributions 
1055*5e3eaea3SApple OSS Distributions #define STACKSHOT_PORTLABELID_NONE    (0)  /* No port label found */
1056*5e3eaea3SApple OSS Distributions #define STACKSHOT_PORTLABELID_MISSING (-1) /* portlabel found, but stackshot ran out of space to track it */
1057*5e3eaea3SApple OSS Distributions 
1058*5e3eaea3SApple OSS Distributions #define STACKSHOT_WAITOWNER_KERNEL         (UINT64_MAX - 1)
1059*5e3eaea3SApple OSS Distributions #define STACKSHOT_WAITOWNER_PORT_LOCKED    (UINT64_MAX - 2)
1060*5e3eaea3SApple OSS Distributions #define STACKSHOT_WAITOWNER_PSET_LOCKED    (UINT64_MAX - 3)
1061*5e3eaea3SApple OSS Distributions #define STACKSHOT_WAITOWNER_INTRANSIT      (UINT64_MAX - 4)
1062*5e3eaea3SApple OSS Distributions #define STACKSHOT_WAITOWNER_MTXSPIN        (UINT64_MAX - 5)
1063*5e3eaea3SApple OSS Distributions #define STACKSHOT_WAITOWNER_THREQUESTED    (UINT64_MAX - 6) /* workloop waiting for a new worker thread */
1064*5e3eaea3SApple OSS Distributions #define STACKSHOT_WAITOWNER_SUSPENDED      (UINT64_MAX - 7) /* workloop is suspended */
1065*5e3eaea3SApple OSS Distributions 
1066*5e3eaea3SApple OSS Distributions #define STACKSHOT_PORTLABEL_READFAILED     0x1  /* could not read port information */
1067*5e3eaea3SApple OSS Distributions #define STACKSHOT_PORTLABEL_THROTTLED      0x2  /* service port is marked as throttled */
1068*5e3eaea3SApple OSS Distributions 
1069*5e3eaea3SApple OSS Distributions struct portlabel_info {
1070*5e3eaea3SApple OSS Distributions 	int16_t portlabel_id;         /* kcdata-specific ID for this port label  */
1071*5e3eaea3SApple OSS Distributions 	uint16_t portlabel_flags;           /* STACKSHOT_PORTLABEL_* */
1072*5e3eaea3SApple OSS Distributions 	uint8_t portlabel_domain;           /* launchd domain */
1073*5e3eaea3SApple OSS Distributions } __attribute__((packed));
1074*5e3eaea3SApple OSS Distributions 
1075*5e3eaea3SApple OSS Distributions struct stackshot_cpu_architecture {
1076*5e3eaea3SApple OSS Distributions 	int32_t cputype;
1077*5e3eaea3SApple OSS Distributions 	int32_t cpusubtype;
1078*5e3eaea3SApple OSS Distributions } __attribute__((packed));
1079*5e3eaea3SApple OSS Distributions 
1080*5e3eaea3SApple OSS Distributions struct stack_snapshot_stacktop {
1081*5e3eaea3SApple OSS Distributions 	uint64_t sp;
1082*5e3eaea3SApple OSS Distributions 	uint8_t stack_contents[8];
1083*5e3eaea3SApple OSS Distributions };
1084*5e3eaea3SApple OSS Distributions 
1085*5e3eaea3SApple OSS Distributions /* only collected if STACKSHOT_COLLECTS_LATENCY_INFO is set to !0 */
1086*5e3eaea3SApple OSS Distributions struct stackshot_latency_collection {
1087*5e3eaea3SApple OSS Distributions 	uint64_t latency_version;
1088*5e3eaea3SApple OSS Distributions 	uint64_t setup_latency;
1089*5e3eaea3SApple OSS Distributions 	uint64_t total_task_iteration_latency;
1090*5e3eaea3SApple OSS Distributions 	uint64_t total_terminated_task_iteration_latency;
1091*5e3eaea3SApple OSS Distributions } __attribute__((packed));
1092*5e3eaea3SApple OSS Distributions 
1093*5e3eaea3SApple OSS Distributions /* only collected if STACKSHOT_COLLECTS_LATENCY_INFO is set to !0 */
1094*5e3eaea3SApple OSS Distributions struct stackshot_latency_task {
1095*5e3eaea3SApple OSS Distributions 	uint64_t task_uniqueid;
1096*5e3eaea3SApple OSS Distributions 	uint64_t setup_latency;
1097*5e3eaea3SApple OSS Distributions 	uint64_t task_thread_count_loop_latency;
1098*5e3eaea3SApple OSS Distributions 	uint64_t task_thread_data_loop_latency;
1099*5e3eaea3SApple OSS Distributions 	uint64_t cur_tsnap_latency;
1100*5e3eaea3SApple OSS Distributions 	uint64_t pmap_latency;
1101*5e3eaea3SApple OSS Distributions 	uint64_t bsd_proc_ids_latency;
1102*5e3eaea3SApple OSS Distributions 	uint64_t misc_latency;
1103*5e3eaea3SApple OSS Distributions 	uint64_t misc2_latency;
1104*5e3eaea3SApple OSS Distributions 	uint64_t end_latency;
1105*5e3eaea3SApple OSS Distributions } __attribute__((packed));
1106*5e3eaea3SApple OSS Distributions 
1107*5e3eaea3SApple OSS Distributions /* only collected if STACKSHOT_COLLECTS_LATENCY_INFO is set to !0 */
1108*5e3eaea3SApple OSS Distributions struct stackshot_latency_thread {
1109*5e3eaea3SApple OSS Distributions 	uint64_t thread_id;
1110*5e3eaea3SApple OSS Distributions 	uint64_t cur_thsnap1_latency;
1111*5e3eaea3SApple OSS Distributions 	uint64_t dispatch_serial_latency;
1112*5e3eaea3SApple OSS Distributions 	uint64_t dispatch_label_latency;
1113*5e3eaea3SApple OSS Distributions 	uint64_t cur_thsnap2_latency;
1114*5e3eaea3SApple OSS Distributions 	uint64_t thread_name_latency;
1115*5e3eaea3SApple OSS Distributions 	uint64_t sur_times_latency;
1116*5e3eaea3SApple OSS Distributions 	uint64_t user_stack_latency;
1117*5e3eaea3SApple OSS Distributions 	uint64_t kernel_stack_latency;
1118*5e3eaea3SApple OSS Distributions 	uint64_t misc_latency;
1119*5e3eaea3SApple OSS Distributions } __attribute__((packed));
1120*5e3eaea3SApple OSS Distributions 
1121*5e3eaea3SApple OSS Distributions struct stackshot_suspension_info {
1122*5e3eaea3SApple OSS Distributions 	uint64_t tss_last_start; /* mach_absolute_time of beginning of last suspension*/
1123*5e3eaea3SApple OSS Distributions 	uint64_t tss_last_end;   /* mach_absolute_time of end of last suspension */
1124*5e3eaea3SApple OSS Distributions 	uint64_t tss_count;      /* number of times this task has been suspended */
1125*5e3eaea3SApple OSS Distributions 	uint64_t tss_duration;   /* sum(mach_absolute_time) of time spend suspended */
1126*5e3eaea3SApple OSS Distributions } __attribute__((packed));
1127*5e3eaea3SApple OSS Distributions 
1128*5e3eaea3SApple OSS Distributions struct stackshot_suspension_source {
1129*5e3eaea3SApple OSS Distributions 	uint64_t tss_time;     /* mach_absolute_time of suspend */
1130*5e3eaea3SApple OSS Distributions 	uint64_t tss_tid;      /* tid of suspending thread */
1131*5e3eaea3SApple OSS Distributions 	int tss_pid;           /* pid of suspending task */
1132*5e3eaea3SApple OSS Distributions 	char tss_procname[65]; /* name of suspending task */
1133*5e3eaea3SApple OSS Distributions } __attribute__((packed));
1134*5e3eaea3SApple OSS Distributions 
1135*5e3eaea3SApple OSS Distributions /**************** definitions for crashinfo *********************/
1136*5e3eaea3SApple OSS Distributions 
1137*5e3eaea3SApple OSS Distributions /*
1138*5e3eaea3SApple OSS Distributions  * NOTE: Please update kcdata/libkdd/kcdtypes.c if you make any changes
1139*5e3eaea3SApple OSS Distributions  * in TASK_CRASHINFO_* types.
1140*5e3eaea3SApple OSS Distributions  */
1141*5e3eaea3SApple OSS Distributions 
1142*5e3eaea3SApple OSS Distributions /* FIXME some of these types aren't clean (fixed width,  packed, and defined *here*) */
1143*5e3eaea3SApple OSS Distributions 
1144*5e3eaea3SApple OSS Distributions struct crashinfo_proc_uniqidentifierinfo {
1145*5e3eaea3SApple OSS Distributions 	uint8_t                 p_uuid[16];             /* UUID of the main executable */
1146*5e3eaea3SApple OSS Distributions 	uint64_t                p_uniqueid;             /* 64 bit unique identifier for process */
1147*5e3eaea3SApple OSS Distributions 	uint64_t                p_puniqueid;            /* unique identifier for process's parent */
1148*5e3eaea3SApple OSS Distributions 	uint64_t                p_reserve2;             /* reserved for future use */
1149*5e3eaea3SApple OSS Distributions 	uint64_t                p_reserve3;             /* reserved for future use */
1150*5e3eaea3SApple OSS Distributions 	uint64_t                p_reserve4;             /* reserved for future use */
1151*5e3eaea3SApple OSS Distributions } __attribute__((packed));
1152*5e3eaea3SApple OSS Distributions 
1153*5e3eaea3SApple OSS Distributions #define MAX_TRIAGE_STRING_LEN   (128)
1154*5e3eaea3SApple OSS Distributions 
1155*5e3eaea3SApple OSS Distributions struct kernel_triage_info_v1 {
1156*5e3eaea3SApple OSS Distributions 	char triage_string1[MAX_TRIAGE_STRING_LEN];
1157*5e3eaea3SApple OSS Distributions 	char triage_string2[MAX_TRIAGE_STRING_LEN];
1158*5e3eaea3SApple OSS Distributions 	char triage_string3[MAX_TRIAGE_STRING_LEN];
1159*5e3eaea3SApple OSS Distributions 	char triage_string4[MAX_TRIAGE_STRING_LEN];
1160*5e3eaea3SApple OSS Distributions 	char triage_string5[MAX_TRIAGE_STRING_LEN];
1161*5e3eaea3SApple OSS Distributions } __attribute__((packed));
1162*5e3eaea3SApple OSS Distributions 
1163*5e3eaea3SApple OSS Distributions #define MAX_CRASHINFO_SIGNING_ID_LEN 64
1164*5e3eaea3SApple OSS Distributions #define MAX_CRASHINFO_TEAM_ID_LEN 32
1165*5e3eaea3SApple OSS Distributions 
1166*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_BEGIN                KCDATA_BUFFER_BEGIN_CRASHINFO
1167*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_STRING_DESC          KCDATA_TYPE_STRING_DESC
1168*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_UINT32_DESC          KCDATA_TYPE_UINT32_DESC
1169*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_UINT64_DESC          KCDATA_TYPE_UINT64_DESC
1170*5e3eaea3SApple OSS Distributions 
1171*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_EXTMODINFO           0x801
1172*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_BSDINFOWITHUNIQID    0x802 /* struct crashinfo_proc_uniqidentifierinfo */
1173*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_TASKDYLD_INFO        0x803
1174*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_UUID                 0x804
1175*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_PID                  0x805
1176*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_PPID                 0x806
1177*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_RUSAGE               0x807  /* struct rusage DEPRECATED do not use.
1178*5e3eaea3SApple OSS Distributions 	                                            *                                                      This struct has longs in it */
1179*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_RUSAGE_INFO          0x808  /* struct rusage_info_v3 from resource.h */
1180*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_PROC_NAME            0x809  /* char * */
1181*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_PROC_STARTTIME       0x80B  /* struct timeval64 */
1182*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_USERSTACK            0x80C  /* uint64_t */
1183*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_ARGSLEN              0x80D
1184*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_EXCEPTION_CODES      0x80E  /* mach_exception_data_t */
1185*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_PROC_PATH            0x80F  /* string of len MAXPATHLEN */
1186*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_PROC_CSFLAGS         0x810  /* uint32_t */
1187*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_PROC_STATUS          0x811  /* char */
1188*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_UID                  0x812  /* uid_t */
1189*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_GID                  0x813  /* gid_t */
1190*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_PROC_ARGC            0x814  /* int */
1191*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_PROC_FLAGS           0x815  /* unsigned int */
1192*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_CPUTYPE              0x816  /* cpu_type_t */
1193*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_WORKQUEUEINFO        0x817  /* struct proc_workqueueinfo */
1194*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_RESPONSIBLE_PID      0x818  /* pid_t */
1195*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_DIRTY_FLAGS          0x819  /* int */
1196*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_CRASHED_THREADID     0x81A  /* uint64_t */
1197*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_COALITION_ID         0x81B  /* uint64_t */
1198*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_UDATA_PTRS           0x81C  /* uint64_t */
1199*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_MEMORY_LIMIT         0x81D  /* uint64_t */
1200*5e3eaea3SApple OSS Distributions 
1201*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_LEDGER_INTERNAL                          0x81E /* uint64_t */
1202*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_LEDGER_INTERNAL_COMPRESSED               0x81F /* uint64_t */
1203*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_LEDGER_IOKIT_MAPPED                      0x820 /* uint64_t */
1204*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_LEDGER_ALTERNATE_ACCOUNTING              0x821 /* uint64_t */
1205*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_LEDGER_ALTERNATE_ACCOUNTING_COMPRESSED   0x822 /* uint64_t */
1206*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_LEDGER_PURGEABLE_NONVOLATILE             0x823 /* uint64_t */
1207*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_LEDGER_PURGEABLE_NONVOLATILE_COMPRESSED  0x824 /* uint64_t */
1208*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_LEDGER_PAGE_TABLE                        0x825 /* uint64_t */
1209*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_LEDGER_PHYS_FOOTPRINT                    0x826 /* uint64_t */
1210*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_LEDGER_PHYS_FOOTPRINT_LIFETIME_MAX       0x827 /* uint64_t */
1211*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_LEDGER_NETWORK_NONVOLATILE               0x828 /* uint64_t */
1212*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_LEDGER_NETWORK_NONVOLATILE_COMPRESSED    0x829 /* uint64_t */
1213*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_LEDGER_WIRED_MEM                         0x82A /* uint64_t */
1214*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_PROC_PERSONA_ID                          0x82B /* uid_t */
1215*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_MEMORY_LIMIT_INCREASE                    0x82C /* uint32_t */
1216*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_LEDGER_TAGGED_FOOTPRINT                  0x82D /* uint64_t */
1217*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_LEDGER_TAGGED_FOOTPRINT_COMPRESSED       0x82E /* uint64_t */
1218*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_LEDGER_MEDIA_FOOTPRINT                   0x82F /* uint64_t */
1219*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_LEDGER_MEDIA_FOOTPRINT_COMPRESSED        0x830 /* uint64_t */
1220*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_LEDGER_GRAPHICS_FOOTPRINT                0x831 /* uint64_t */
1221*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_LEDGER_GRAPHICS_FOOTPRINT_COMPRESSED     0x832 /* uint64_t */
1222*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_LEDGER_NEURAL_FOOTPRINT                  0x833 /* uint64_t */
1223*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_LEDGER_NEURAL_FOOTPRINT_COMPRESSED       0x834 /* uint64_t */
1224*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_MEMORYSTATUS_EFFECTIVE_PRIORITY          0x835 /* int32_t */
1225*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_KERNEL_TRIAGE_INFO_V1                    0x836 /* struct kernel_triage_info_v1 */
1226*5e3eaea3SApple OSS Distributions 
1227*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_TASK_IS_CORPSE_FORK                      0x837 /* boolean_t */
1228*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_EXCEPTION_TYPE                           0x838 /* int */
1229*5e3eaea3SApple OSS Distributions 
1230*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_CRASH_COUNT                              0x839 /* int */
1231*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_THROTTLE_TIMEOUT                         0x83A /* int */
1232*5e3eaea3SApple OSS Distributions 
1233*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_CS_SIGNING_ID                            0x83B /* string of len MAX_CRASHINFO_SIGNING_ID_LEN */
1234*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_CS_TEAM_ID                               0x83C /* string of len MAX_CRASHINFO_TEAM_ID_LEN */
1235*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_CS_VALIDATION_CATEGORY                   0x83D /* uint32_t */
1236*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_CS_TRUST_LEVEL                           0x83E /* uint32_t */
1237*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_PROC_CPUTYPE                             0x83F /* cpu_type_t */
1238*5e3eaea3SApple OSS Distributions 
1239*5e3eaea3SApple OSS Distributions #define TASK_CRASHINFO_END                  KCDATA_TYPE_BUFFER_END
1240*5e3eaea3SApple OSS Distributions 
1241*5e3eaea3SApple OSS Distributions /**************** definitions for backtrace info *********************/
1242*5e3eaea3SApple OSS Distributions 
1243*5e3eaea3SApple OSS Distributions /* tstate is variable length with count elements */
1244*5e3eaea3SApple OSS Distributions struct btinfo_thread_state_data_t {
1245*5e3eaea3SApple OSS Distributions 	uint32_t flavor;
1246*5e3eaea3SApple OSS Distributions 	uint32_t count;
1247*5e3eaea3SApple OSS Distributions 	int tstate[];
1248*5e3eaea3SApple OSS Distributions };
1249*5e3eaea3SApple OSS Distributions 
1250*5e3eaea3SApple OSS Distributions struct btinfo_sc_load_info64 {
1251*5e3eaea3SApple OSS Distributions 	uint64_t sharedCacheSlide;
1252*5e3eaea3SApple OSS Distributions 	uuid_t   sharedCacheUUID;
1253*5e3eaea3SApple OSS Distributions 	uint64_t sharedCacheBaseAddress;
1254*5e3eaea3SApple OSS Distributions };
1255*5e3eaea3SApple OSS Distributions 
1256*5e3eaea3SApple OSS Distributions struct btinfo_sc_load_info {
1257*5e3eaea3SApple OSS Distributions 	uint32_t sharedCacheSlide;
1258*5e3eaea3SApple OSS Distributions 	uuid_t   sharedCacheUUID;
1259*5e3eaea3SApple OSS Distributions 	uint32_t sharedCacheBaseAddress;
1260*5e3eaea3SApple OSS Distributions };
1261*5e3eaea3SApple OSS Distributions 
1262*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_BEGIN                                       KCDATA_BUFFER_BEGIN_BTINFO
1263*5e3eaea3SApple OSS Distributions 
1264*5e3eaea3SApple OSS Distributions /* Shared keys with CRASHINFO */
1265*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_PID                                         0xA01
1266*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_PPID                                        0xA02
1267*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_PROC_NAME                                   0xA03
1268*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_PROC_PATH                                   0xA04
1269*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_UID                                         0xA05
1270*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_GID                                         0xA06
1271*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_PROC_FLAGS                                  0xA07
1272*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_CPUTYPE                                     0xA08
1273*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_EXCEPTION_CODES                             0xA09
1274*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_EXCEPTION_TYPE                              0xA0A
1275*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_RUSAGE_INFO                                 0xA0B
1276*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_COALITION_ID                                0xA0C
1277*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_CRASH_COUNT                                 0xA0D
1278*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_THROTTLE_TIMEOUT                            0xA0E
1279*5e3eaea3SApple OSS Distributions 
1280*5e3eaea3SApple OSS Distributions /* Only in BTINFO */
1281*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_THREAD_ID                                   0xA20 /* uint64_t */
1282*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_THREAD_NAME                                 0xA21 /* string of len MAXTHREADNAMESIZE */
1283*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_THREAD_STATE                                0xA22 /* struct btinfo_thread_state_data_t */
1284*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_THREAD_EXCEPTION_STATE                      0xA23 /* struct btinfo_thread_state_data_t */
1285*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_BACKTRACE                                   0xA24 /* array of uintptr_t */
1286*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_BACKTRACE64                                 0xA25 /* array of uintptr_t */
1287*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_ASYNC_BACKTRACE64                           0xA26 /* array of uintptr_t */
1288*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_ASYNC_START_INDEX                           0xA27 /* uint32_t */
1289*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_PLATFORM                                    0xA28 /* uint32_t */
1290*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_SC_LOADINFO                                 0xA29 /* struct btinfo_sc_load_info */
1291*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_SC_LOADINFO64                               0xA2A /* struct btinfo_sc_load_info64 */
1292*5e3eaea3SApple OSS Distributions 
1293*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_DYLD_LOADINFO                               KCDATA_TYPE_LIBRARY_LOADINFO
1294*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_DYLD_LOADINFO64                             KCDATA_TYPE_LIBRARY_LOADINFO64
1295*5e3eaea3SApple OSS Distributions 
1296*5e3eaea3SApple OSS Distributions /* Last one */
1297*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_FLAGS                                       0xAFF /* uint32_t */
1298*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_FLAG_BT_TRUNCATED                           0x1
1299*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_FLAG_ASYNC_BT_TRUNCATED                     0x2
1300*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_FLAG_TASK_TERMINATED                        0x4 /* task is terminated */
1301*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_FLAG_KCDATA_INCOMPLETE                      0x8 /* lw corpse collection is incomplete */
1302*5e3eaea3SApple OSS Distributions 
1303*5e3eaea3SApple OSS Distributions #define TASK_BTINFO_END                                         KCDATA_TYPE_BUFFER_END
1304*5e3eaea3SApple OSS Distributions 
1305*5e3eaea3SApple OSS Distributions /**************** definitions for os reasons *********************/
1306*5e3eaea3SApple OSS Distributions 
1307*5e3eaea3SApple OSS Distributions #define EXIT_REASON_SNAPSHOT            0x1001
1308*5e3eaea3SApple OSS Distributions #define EXIT_REASON_USER_DESC           0x1002 /* string description of reason */
1309*5e3eaea3SApple OSS Distributions #define EXIT_REASON_USER_PAYLOAD        0x1003 /* user payload data */
1310*5e3eaea3SApple OSS Distributions #define EXIT_REASON_CODESIGNING_INFO    0x1004
1311*5e3eaea3SApple OSS Distributions #define EXIT_REASON_WORKLOOP_ID         0x1005
1312*5e3eaea3SApple OSS Distributions #define EXIT_REASON_DISPATCH_QUEUE_NO   0x1006
1313*5e3eaea3SApple OSS Distributions 
1314*5e3eaea3SApple OSS Distributions struct exit_reason_snapshot {
1315*5e3eaea3SApple OSS Distributions 	uint32_t ers_namespace;
1316*5e3eaea3SApple OSS Distributions 	uint64_t ers_code;
1317*5e3eaea3SApple OSS Distributions 	/* end of version 1 of exit_reason_snapshot. sizeof v1 was 12 */
1318*5e3eaea3SApple OSS Distributions 	uint64_t ers_flags;
1319*5e3eaea3SApple OSS Distributions } __attribute__((packed));
1320*5e3eaea3SApple OSS Distributions 
1321*5e3eaea3SApple OSS Distributions #define EXIT_REASON_CODESIG_PATH_MAX    1024
1322*5e3eaea3SApple OSS Distributions 
1323*5e3eaea3SApple OSS Distributions struct codesigning_exit_reason_info {
1324*5e3eaea3SApple OSS Distributions 	uint64_t  ceri_virt_addr;
1325*5e3eaea3SApple OSS Distributions 	uint64_t  ceri_file_offset;
1326*5e3eaea3SApple OSS Distributions 	char      ceri_pathname[EXIT_REASON_CODESIG_PATH_MAX];
1327*5e3eaea3SApple OSS Distributions 	char      ceri_filename[EXIT_REASON_CODESIG_PATH_MAX];
1328*5e3eaea3SApple OSS Distributions 	uint64_t  ceri_codesig_modtime_secs;
1329*5e3eaea3SApple OSS Distributions 	uint64_t  ceri_codesig_modtime_nsecs;
1330*5e3eaea3SApple OSS Distributions 	uint64_t  ceri_page_modtime_secs;
1331*5e3eaea3SApple OSS Distributions 	uint64_t  ceri_page_modtime_nsecs;
1332*5e3eaea3SApple OSS Distributions 	uint8_t   ceri_path_truncated;
1333*5e3eaea3SApple OSS Distributions 	uint8_t   ceri_object_codesigned;
1334*5e3eaea3SApple OSS Distributions 	uint8_t   ceri_page_codesig_validated;
1335*5e3eaea3SApple OSS Distributions 	uint8_t   ceri_page_codesig_tainted;
1336*5e3eaea3SApple OSS Distributions 	uint8_t   ceri_page_codesig_nx;
1337*5e3eaea3SApple OSS Distributions 	uint8_t   ceri_page_wpmapped;
1338*5e3eaea3SApple OSS Distributions 	uint8_t   ceri_page_slid;
1339*5e3eaea3SApple OSS Distributions 	uint8_t   ceri_page_dirty;
1340*5e3eaea3SApple OSS Distributions 	uint32_t  ceri_page_shadow_depth;
1341*5e3eaea3SApple OSS Distributions } __attribute__((packed));
1342*5e3eaea3SApple OSS Distributions 
1343*5e3eaea3SApple OSS Distributions #define EXIT_REASON_USER_DESC_MAX_LEN   1024
1344*5e3eaea3SApple OSS Distributions #define EXIT_REASON_PAYLOAD_MAX_LEN     2048
1345*5e3eaea3SApple OSS Distributions /**************** safe iterators *********************/
1346*5e3eaea3SApple OSS Distributions #if !__has_ptrcheck
1347*5e3eaea3SApple OSS Distributions 
1348*5e3eaea3SApple OSS Distributions typedef struct kcdata_iter {
1349*5e3eaea3SApple OSS Distributions 	kcdata_item_t item;
1350*5e3eaea3SApple OSS Distributions 	void *end;
1351*5e3eaea3SApple OSS Distributions } kcdata_iter_t;
1352*5e3eaea3SApple OSS Distributions 
1353*5e3eaea3SApple OSS Distributions 
1354*5e3eaea3SApple OSS Distributions static inline
1355*5e3eaea3SApple OSS Distributions kcdata_iter_t
kcdata_iter(void * buffer,unsigned long size)1356*5e3eaea3SApple OSS Distributions kcdata_iter(void *buffer, unsigned long size)
1357*5e3eaea3SApple OSS Distributions {
1358*5e3eaea3SApple OSS Distributions 	kcdata_iter_t iter;
1359*5e3eaea3SApple OSS Distributions 	iter.item = (kcdata_item_t) buffer;
1360*5e3eaea3SApple OSS Distributions 	iter.end = (void*) (((uintptr_t)buffer) + size);
1361*5e3eaea3SApple OSS Distributions 	return iter;
1362*5e3eaea3SApple OSS Distributions }
1363*5e3eaea3SApple OSS Distributions 
1364*5e3eaea3SApple OSS Distributions static inline
1365*5e3eaea3SApple OSS Distributions kcdata_iter_t kcdata_iter_unsafe(void *buffer) __attribute__((deprecated));
1366*5e3eaea3SApple OSS Distributions 
1367*5e3eaea3SApple OSS Distributions static inline
1368*5e3eaea3SApple OSS Distributions kcdata_iter_t
kcdata_iter_unsafe(void * buffer)1369*5e3eaea3SApple OSS Distributions kcdata_iter_unsafe(void *buffer)
1370*5e3eaea3SApple OSS Distributions {
1371*5e3eaea3SApple OSS Distributions 	kcdata_iter_t iter;
1372*5e3eaea3SApple OSS Distributions 	iter.item = (kcdata_item_t) buffer;
1373*5e3eaea3SApple OSS Distributions 	iter.end = (void*) (uintptr_t) ~0;
1374*5e3eaea3SApple OSS Distributions 	return iter;
1375*5e3eaea3SApple OSS Distributions }
1376*5e3eaea3SApple OSS Distributions 
1377*5e3eaea3SApple OSS Distributions static const kcdata_iter_t kcdata_invalid_iter = { .item = NULL, .end = NULL };
1378*5e3eaea3SApple OSS Distributions 
1379*5e3eaea3SApple OSS Distributions static inline
1380*5e3eaea3SApple OSS Distributions int
kcdata_iter_valid(kcdata_iter_t iter)1381*5e3eaea3SApple OSS Distributions kcdata_iter_valid(kcdata_iter_t iter)
1382*5e3eaea3SApple OSS Distributions {
1383*5e3eaea3SApple OSS Distributions 	return
1384*5e3eaea3SApple OSS Distributions 	        ((uintptr_t)iter.item + sizeof(struct kcdata_item) <= (uintptr_t)iter.end) &&
1385*5e3eaea3SApple OSS Distributions 	        ((uintptr_t)iter.item + sizeof(struct kcdata_item) + iter.item->size <= (uintptr_t)iter.end);
1386*5e3eaea3SApple OSS Distributions }
1387*5e3eaea3SApple OSS Distributions 
1388*5e3eaea3SApple OSS Distributions 
1389*5e3eaea3SApple OSS Distributions static inline
1390*5e3eaea3SApple OSS Distributions kcdata_iter_t
kcdata_iter_next(kcdata_iter_t iter)1391*5e3eaea3SApple OSS Distributions kcdata_iter_next(kcdata_iter_t iter)
1392*5e3eaea3SApple OSS Distributions {
1393*5e3eaea3SApple OSS Distributions 	iter.item = (kcdata_item_t) (((uintptr_t)iter.item) + sizeof(struct kcdata_item) + (iter.item->size));
1394*5e3eaea3SApple OSS Distributions 	return iter;
1395*5e3eaea3SApple OSS Distributions }
1396*5e3eaea3SApple OSS Distributions 
1397*5e3eaea3SApple OSS Distributions static inline uint32_t
kcdata_iter_type(kcdata_iter_t iter)1398*5e3eaea3SApple OSS Distributions kcdata_iter_type(kcdata_iter_t iter)
1399*5e3eaea3SApple OSS Distributions {
1400*5e3eaea3SApple OSS Distributions 	if ((iter.item->type & ~0xfu) == KCDATA_TYPE_ARRAY_PAD0) {
1401*5e3eaea3SApple OSS Distributions 		return KCDATA_TYPE_ARRAY;
1402*5e3eaea3SApple OSS Distributions 	} else {
1403*5e3eaea3SApple OSS Distributions 		return iter.item->type;
1404*5e3eaea3SApple OSS Distributions 	}
1405*5e3eaea3SApple OSS Distributions }
1406*5e3eaea3SApple OSS Distributions 
1407*5e3eaea3SApple OSS Distributions static inline uint32_t
kcdata_calc_padding(uint32_t size)1408*5e3eaea3SApple OSS Distributions kcdata_calc_padding(uint32_t size)
1409*5e3eaea3SApple OSS Distributions {
1410*5e3eaea3SApple OSS Distributions 	/* calculate number of bytes to add to size to get something divisible by 16 */
1411*5e3eaea3SApple OSS Distributions 	return (-size) & 0xf;
1412*5e3eaea3SApple OSS Distributions }
1413*5e3eaea3SApple OSS Distributions 
1414*5e3eaea3SApple OSS Distributions static inline uint32_t
kcdata_flags_get_padding(uint64_t flags)1415*5e3eaea3SApple OSS Distributions kcdata_flags_get_padding(uint64_t flags)
1416*5e3eaea3SApple OSS Distributions {
1417*5e3eaea3SApple OSS Distributions 	return flags & KCDATA_FLAGS_STRUCT_PADDING_MASK;
1418*5e3eaea3SApple OSS Distributions }
1419*5e3eaea3SApple OSS Distributions 
1420*5e3eaea3SApple OSS Distributions /* see comment above about has_padding */
1421*5e3eaea3SApple OSS Distributions static inline int
kcdata_iter_is_legacy_item(kcdata_iter_t iter,uint32_t legacy_size)1422*5e3eaea3SApple OSS Distributions kcdata_iter_is_legacy_item(kcdata_iter_t iter, uint32_t legacy_size)
1423*5e3eaea3SApple OSS Distributions {
1424*5e3eaea3SApple OSS Distributions 	uint32_t legacy_size_padded = legacy_size + kcdata_calc_padding(legacy_size);
1425*5e3eaea3SApple OSS Distributions 	return iter.item->size == legacy_size_padded &&
1426*5e3eaea3SApple OSS Distributions 	       (iter.item->flags & (KCDATA_FLAGS_STRUCT_PADDING_MASK | KCDATA_FLAGS_STRUCT_HAS_PADDING)) == 0;
1427*5e3eaea3SApple OSS Distributions }
1428*5e3eaea3SApple OSS Distributions 
1429*5e3eaea3SApple OSS Distributions static inline uint32_t
kcdata_iter_size(kcdata_iter_t iter)1430*5e3eaea3SApple OSS Distributions kcdata_iter_size(kcdata_iter_t iter)
1431*5e3eaea3SApple OSS Distributions {
1432*5e3eaea3SApple OSS Distributions 	uint32_t legacy_size = 0;
1433*5e3eaea3SApple OSS Distributions 
1434*5e3eaea3SApple OSS Distributions 	switch (kcdata_iter_type(iter)) {
1435*5e3eaea3SApple OSS Distributions 	case KCDATA_TYPE_ARRAY:
1436*5e3eaea3SApple OSS Distributions 	case KCDATA_TYPE_CONTAINER_BEGIN:
1437*5e3eaea3SApple OSS Distributions 		return iter.item->size;
1438*5e3eaea3SApple OSS Distributions 	case STACKSHOT_KCTYPE_THREAD_SNAPSHOT: {
1439*5e3eaea3SApple OSS Distributions 		legacy_size = sizeof(struct thread_snapshot_v2);
1440*5e3eaea3SApple OSS Distributions 		if (kcdata_iter_is_legacy_item(iter, legacy_size)) {
1441*5e3eaea3SApple OSS Distributions 			return legacy_size;
1442*5e3eaea3SApple OSS Distributions 		}
1443*5e3eaea3SApple OSS Distributions 
1444*5e3eaea3SApple OSS Distributions 		goto not_legacy;
1445*5e3eaea3SApple OSS Distributions 	}
1446*5e3eaea3SApple OSS Distributions 	case STACKSHOT_KCTYPE_SHAREDCACHE_LOADINFO: {
1447*5e3eaea3SApple OSS Distributions 		legacy_size = sizeof(struct dyld_uuid_info_64);
1448*5e3eaea3SApple OSS Distributions 		if (kcdata_iter_is_legacy_item(iter, legacy_size)) {
1449*5e3eaea3SApple OSS Distributions 			return legacy_size;
1450*5e3eaea3SApple OSS Distributions 		}
1451*5e3eaea3SApple OSS Distributions 
1452*5e3eaea3SApple OSS Distributions 		goto not_legacy;
1453*5e3eaea3SApple OSS Distributions 	}
1454*5e3eaea3SApple OSS Distributions not_legacy:
1455*5e3eaea3SApple OSS Distributions 	default:
1456*5e3eaea3SApple OSS Distributions 		if (iter.item->size < kcdata_flags_get_padding(iter.item->flags)) {
1457*5e3eaea3SApple OSS Distributions 			return 0;
1458*5e3eaea3SApple OSS Distributions 		} else {
1459*5e3eaea3SApple OSS Distributions 			return iter.item->size - kcdata_flags_get_padding(iter.item->flags);
1460*5e3eaea3SApple OSS Distributions 		}
1461*5e3eaea3SApple OSS Distributions 	}
1462*5e3eaea3SApple OSS Distributions }
1463*5e3eaea3SApple OSS Distributions 
1464*5e3eaea3SApple OSS Distributions static inline uint64_t
kcdata_iter_flags(kcdata_iter_t iter)1465*5e3eaea3SApple OSS Distributions kcdata_iter_flags(kcdata_iter_t iter)
1466*5e3eaea3SApple OSS Distributions {
1467*5e3eaea3SApple OSS Distributions 	return iter.item->flags;
1468*5e3eaea3SApple OSS Distributions }
1469*5e3eaea3SApple OSS Distributions 
1470*5e3eaea3SApple OSS Distributions static inline
1471*5e3eaea3SApple OSS Distributions void *
kcdata_iter_payload(kcdata_iter_t iter)1472*5e3eaea3SApple OSS Distributions kcdata_iter_payload(kcdata_iter_t iter)
1473*5e3eaea3SApple OSS Distributions {
1474*5e3eaea3SApple OSS Distributions 	return &iter.item->data;
1475*5e3eaea3SApple OSS Distributions }
1476*5e3eaea3SApple OSS Distributions 
1477*5e3eaea3SApple OSS Distributions 
1478*5e3eaea3SApple OSS Distributions static inline
1479*5e3eaea3SApple OSS Distributions uint32_t
kcdata_iter_array_elem_type(kcdata_iter_t iter)1480*5e3eaea3SApple OSS Distributions kcdata_iter_array_elem_type(kcdata_iter_t iter)
1481*5e3eaea3SApple OSS Distributions {
1482*5e3eaea3SApple OSS Distributions 	return (iter.item->flags >> 32) & UINT32_MAX;
1483*5e3eaea3SApple OSS Distributions }
1484*5e3eaea3SApple OSS Distributions 
1485*5e3eaea3SApple OSS Distributions static inline
1486*5e3eaea3SApple OSS Distributions uint32_t
kcdata_iter_array_elem_count(kcdata_iter_t iter)1487*5e3eaea3SApple OSS Distributions kcdata_iter_array_elem_count(kcdata_iter_t iter)
1488*5e3eaea3SApple OSS Distributions {
1489*5e3eaea3SApple OSS Distributions 	return (iter.item->flags) & UINT32_MAX;
1490*5e3eaea3SApple OSS Distributions }
1491*5e3eaea3SApple OSS Distributions 
1492*5e3eaea3SApple OSS Distributions /* KCDATA_TYPE_ARRAY is ambiguous about the size of the array elements.  Size is
1493*5e3eaea3SApple OSS Distributions  * calculated as total_size / elements_count, but total size got padded out to a
1494*5e3eaea3SApple OSS Distributions  * 16 byte alignment.  New kernels will generate KCDATA_TYPE_ARRAY_PAD* instead
1495*5e3eaea3SApple OSS Distributions  * to explicitly tell us how much padding was used.  Here we have a fixed, never
1496*5e3eaea3SApple OSS Distributions  * to be altered list of the sizes of array elements that were used before I
1497*5e3eaea3SApple OSS Distributions  * discovered this issue.  If you find a KCDATA_TYPE_ARRAY that is not one of
1498*5e3eaea3SApple OSS Distributions  * these types, treat it as invalid data. */
1499*5e3eaea3SApple OSS Distributions 
1500*5e3eaea3SApple OSS Distributions static inline
1501*5e3eaea3SApple OSS Distributions uint32_t
kcdata_iter_array_size_switch(kcdata_iter_t iter)1502*5e3eaea3SApple OSS Distributions kcdata_iter_array_size_switch(kcdata_iter_t iter)
1503*5e3eaea3SApple OSS Distributions {
1504*5e3eaea3SApple OSS Distributions 	switch (kcdata_iter_array_elem_type(iter)) {
1505*5e3eaea3SApple OSS Distributions 	case KCDATA_TYPE_LIBRARY_LOADINFO:
1506*5e3eaea3SApple OSS Distributions 		return sizeof(struct dyld_uuid_info_32);
1507*5e3eaea3SApple OSS Distributions 	case KCDATA_TYPE_LIBRARY_LOADINFO64:
1508*5e3eaea3SApple OSS Distributions 		return sizeof(struct dyld_uuid_info_64);
1509*5e3eaea3SApple OSS Distributions 	case STACKSHOT_KCTYPE_KERN_STACKFRAME:
1510*5e3eaea3SApple OSS Distributions 	case STACKSHOT_KCTYPE_USER_STACKFRAME:
1511*5e3eaea3SApple OSS Distributions 		return sizeof(struct stack_snapshot_frame32);
1512*5e3eaea3SApple OSS Distributions 	case STACKSHOT_KCTYPE_KERN_STACKFRAME64:
1513*5e3eaea3SApple OSS Distributions 	case STACKSHOT_KCTYPE_USER_STACKFRAME64:
1514*5e3eaea3SApple OSS Distributions 		return sizeof(struct stack_snapshot_frame64);
1515*5e3eaea3SApple OSS Distributions 	case STACKSHOT_KCTYPE_DONATING_PIDS:
1516*5e3eaea3SApple OSS Distributions 		return sizeof(int32_t);
1517*5e3eaea3SApple OSS Distributions 	case STACKSHOT_KCTYPE_THREAD_DELTA_SNAPSHOT:
1518*5e3eaea3SApple OSS Distributions 		return sizeof(struct thread_delta_snapshot_v2);
1519*5e3eaea3SApple OSS Distributions 	// This one is only here to make some unit tests work. It should be OK to
1520*5e3eaea3SApple OSS Distributions 	// remove.
1521*5e3eaea3SApple OSS Distributions 	case TASK_CRASHINFO_CRASHED_THREADID:
1522*5e3eaea3SApple OSS Distributions 		return sizeof(uint64_t);
1523*5e3eaea3SApple OSS Distributions 	default:
1524*5e3eaea3SApple OSS Distributions 		return 0;
1525*5e3eaea3SApple OSS Distributions 	}
1526*5e3eaea3SApple OSS Distributions }
1527*5e3eaea3SApple OSS Distributions 
1528*5e3eaea3SApple OSS Distributions static inline
1529*5e3eaea3SApple OSS Distributions int
kcdata_iter_array_valid(kcdata_iter_t iter)1530*5e3eaea3SApple OSS Distributions kcdata_iter_array_valid(kcdata_iter_t iter)
1531*5e3eaea3SApple OSS Distributions {
1532*5e3eaea3SApple OSS Distributions 	if (!kcdata_iter_valid(iter)) {
1533*5e3eaea3SApple OSS Distributions 		return 0;
1534*5e3eaea3SApple OSS Distributions 	}
1535*5e3eaea3SApple OSS Distributions 	if (kcdata_iter_type(iter) != KCDATA_TYPE_ARRAY) {
1536*5e3eaea3SApple OSS Distributions 		return 0;
1537*5e3eaea3SApple OSS Distributions 	}
1538*5e3eaea3SApple OSS Distributions 	if (kcdata_iter_array_elem_count(iter) == 0) {
1539*5e3eaea3SApple OSS Distributions 		return iter.item->size == 0;
1540*5e3eaea3SApple OSS Distributions 	}
1541*5e3eaea3SApple OSS Distributions 	if (iter.item->type == KCDATA_TYPE_ARRAY) {
1542*5e3eaea3SApple OSS Distributions 		uint32_t elem_size = kcdata_iter_array_size_switch(iter);
1543*5e3eaea3SApple OSS Distributions 		if (elem_size == 0) {
1544*5e3eaea3SApple OSS Distributions 			return 0;
1545*5e3eaea3SApple OSS Distributions 		}
1546*5e3eaea3SApple OSS Distributions 		/* sizes get aligned to the nearest 16. */
1547*5e3eaea3SApple OSS Distributions 		return
1548*5e3eaea3SApple OSS Distributions 		        kcdata_iter_array_elem_count(iter) <= iter.item->size / elem_size &&
1549*5e3eaea3SApple OSS Distributions 		        iter.item->size % kcdata_iter_array_elem_count(iter) < 16;
1550*5e3eaea3SApple OSS Distributions 	} else {
1551*5e3eaea3SApple OSS Distributions 		return
1552*5e3eaea3SApple OSS Distributions 		        (iter.item->type & 0xf) <= iter.item->size &&
1553*5e3eaea3SApple OSS Distributions 		        kcdata_iter_array_elem_count(iter) <= iter.item->size - (iter.item->type & 0xf) &&
1554*5e3eaea3SApple OSS Distributions 		        (iter.item->size - (iter.item->type & 0xf)) % kcdata_iter_array_elem_count(iter) == 0;
1555*5e3eaea3SApple OSS Distributions 	}
1556*5e3eaea3SApple OSS Distributions }
1557*5e3eaea3SApple OSS Distributions 
1558*5e3eaea3SApple OSS Distributions 
1559*5e3eaea3SApple OSS Distributions static inline
1560*5e3eaea3SApple OSS Distributions uint32_t
kcdata_iter_array_elem_size(kcdata_iter_t iter)1561*5e3eaea3SApple OSS Distributions kcdata_iter_array_elem_size(kcdata_iter_t iter)
1562*5e3eaea3SApple OSS Distributions {
1563*5e3eaea3SApple OSS Distributions 	if (iter.item->type == KCDATA_TYPE_ARRAY) {
1564*5e3eaea3SApple OSS Distributions 		return kcdata_iter_array_size_switch(iter);
1565*5e3eaea3SApple OSS Distributions 	}
1566*5e3eaea3SApple OSS Distributions 	if (kcdata_iter_array_elem_count(iter) == 0) {
1567*5e3eaea3SApple OSS Distributions 		return 0;
1568*5e3eaea3SApple OSS Distributions 	}
1569*5e3eaea3SApple OSS Distributions 	return (iter.item->size - (iter.item->type & 0xf)) / kcdata_iter_array_elem_count(iter);
1570*5e3eaea3SApple OSS Distributions }
1571*5e3eaea3SApple OSS Distributions 
1572*5e3eaea3SApple OSS Distributions static inline
1573*5e3eaea3SApple OSS Distributions int
kcdata_iter_container_valid(kcdata_iter_t iter)1574*5e3eaea3SApple OSS Distributions kcdata_iter_container_valid(kcdata_iter_t iter)
1575*5e3eaea3SApple OSS Distributions {
1576*5e3eaea3SApple OSS Distributions 	return
1577*5e3eaea3SApple OSS Distributions 	        kcdata_iter_valid(iter) &&
1578*5e3eaea3SApple OSS Distributions 	        kcdata_iter_type(iter) == KCDATA_TYPE_CONTAINER_BEGIN &&
1579*5e3eaea3SApple OSS Distributions 	        iter.item->size >= sizeof(uint32_t);
1580*5e3eaea3SApple OSS Distributions }
1581*5e3eaea3SApple OSS Distributions 
1582*5e3eaea3SApple OSS Distributions static inline
1583*5e3eaea3SApple OSS Distributions uint32_t
kcdata_iter_container_type(kcdata_iter_t iter)1584*5e3eaea3SApple OSS Distributions kcdata_iter_container_type(kcdata_iter_t iter)
1585*5e3eaea3SApple OSS Distributions {
1586*5e3eaea3SApple OSS Distributions 	return *(uint32_t *) kcdata_iter_payload(iter);
1587*5e3eaea3SApple OSS Distributions }
1588*5e3eaea3SApple OSS Distributions 
1589*5e3eaea3SApple OSS Distributions static inline
1590*5e3eaea3SApple OSS Distributions uint64_t
kcdata_iter_container_id(kcdata_iter_t iter)1591*5e3eaea3SApple OSS Distributions kcdata_iter_container_id(kcdata_iter_t iter)
1592*5e3eaea3SApple OSS Distributions {
1593*5e3eaea3SApple OSS Distributions 	return iter.item->flags;
1594*5e3eaea3SApple OSS Distributions }
1595*5e3eaea3SApple OSS Distributions 
1596*5e3eaea3SApple OSS Distributions 
1597*5e3eaea3SApple OSS Distributions #define KCDATA_ITER_FOREACH(iter) for(; kcdata_iter_valid(iter) && iter.item->type != KCDATA_TYPE_BUFFER_END; iter = kcdata_iter_next(iter))
1598*5e3eaea3SApple OSS Distributions #define KCDATA_ITER_FOREACH_FAILED(iter) (!kcdata_iter_valid(iter) || (iter).item->type != KCDATA_TYPE_BUFFER_END)
1599*5e3eaea3SApple OSS Distributions 
1600*5e3eaea3SApple OSS Distributions static inline
1601*5e3eaea3SApple OSS Distributions kcdata_iter_t
kcdata_iter_find_type(kcdata_iter_t iter,uint32_t type)1602*5e3eaea3SApple OSS Distributions kcdata_iter_find_type(kcdata_iter_t iter, uint32_t type)
1603*5e3eaea3SApple OSS Distributions {
1604*5e3eaea3SApple OSS Distributions 	KCDATA_ITER_FOREACH(iter)
1605*5e3eaea3SApple OSS Distributions 	{
1606*5e3eaea3SApple OSS Distributions 		if (kcdata_iter_type(iter) == type) {
1607*5e3eaea3SApple OSS Distributions 			return iter;
1608*5e3eaea3SApple OSS Distributions 		}
1609*5e3eaea3SApple OSS Distributions 	}
1610*5e3eaea3SApple OSS Distributions 	return kcdata_invalid_iter;
1611*5e3eaea3SApple OSS Distributions }
1612*5e3eaea3SApple OSS Distributions 
1613*5e3eaea3SApple OSS Distributions static inline
1614*5e3eaea3SApple OSS Distributions int
kcdata_iter_data_with_desc_valid(kcdata_iter_t iter,uint32_t minsize)1615*5e3eaea3SApple OSS Distributions kcdata_iter_data_with_desc_valid(kcdata_iter_t iter, uint32_t minsize)
1616*5e3eaea3SApple OSS Distributions {
1617*5e3eaea3SApple OSS Distributions 	return
1618*5e3eaea3SApple OSS Distributions 	        kcdata_iter_valid(iter) &&
1619*5e3eaea3SApple OSS Distributions 	        kcdata_iter_size(iter) >= KCDATA_DESC_MAXLEN + minsize &&
1620*5e3eaea3SApple OSS Distributions 	        ((char*)kcdata_iter_payload(iter))[KCDATA_DESC_MAXLEN - 1] == 0;
1621*5e3eaea3SApple OSS Distributions }
1622*5e3eaea3SApple OSS Distributions 
1623*5e3eaea3SApple OSS Distributions static inline
1624*5e3eaea3SApple OSS Distributions char *
kcdata_iter_string(kcdata_iter_t iter,uint32_t offset)1625*5e3eaea3SApple OSS Distributions kcdata_iter_string(kcdata_iter_t iter, uint32_t offset)
1626*5e3eaea3SApple OSS Distributions {
1627*5e3eaea3SApple OSS Distributions 	if (offset > kcdata_iter_size(iter)) {
1628*5e3eaea3SApple OSS Distributions 		return NULL;
1629*5e3eaea3SApple OSS Distributions 	}
1630*5e3eaea3SApple OSS Distributions 	uint32_t maxlen = kcdata_iter_size(iter) - offset;
1631*5e3eaea3SApple OSS Distributions 	char *s = ((char*)kcdata_iter_payload(iter)) + offset;
1632*5e3eaea3SApple OSS Distributions 	if (strnlen(s, maxlen) < maxlen) {
1633*5e3eaea3SApple OSS Distributions 		return s;
1634*5e3eaea3SApple OSS Distributions 	} else {
1635*5e3eaea3SApple OSS Distributions 		return NULL;
1636*5e3eaea3SApple OSS Distributions 	}
1637*5e3eaea3SApple OSS Distributions }
1638*5e3eaea3SApple OSS Distributions 
1639*5e3eaea3SApple OSS Distributions static inline void
kcdata_iter_get_data_with_desc(kcdata_iter_t iter,char ** desc_ptr,void ** data_ptr,uint32_t * size_ptr)1640*5e3eaea3SApple OSS Distributions kcdata_iter_get_data_with_desc(kcdata_iter_t iter, char **desc_ptr, void **data_ptr, uint32_t *size_ptr)
1641*5e3eaea3SApple OSS Distributions {
1642*5e3eaea3SApple OSS Distributions 	if (desc_ptr) {
1643*5e3eaea3SApple OSS Distributions 		*desc_ptr = (char *)kcdata_iter_payload(iter);
1644*5e3eaea3SApple OSS Distributions 	}
1645*5e3eaea3SApple OSS Distributions 	if (data_ptr) {
1646*5e3eaea3SApple OSS Distributions 		*data_ptr = (void *)((uintptr_t)kcdata_iter_payload(iter) + KCDATA_DESC_MAXLEN);
1647*5e3eaea3SApple OSS Distributions 	}
1648*5e3eaea3SApple OSS Distributions 	if (size_ptr) {
1649*5e3eaea3SApple OSS Distributions 		*size_ptr = kcdata_iter_size(iter) - KCDATA_DESC_MAXLEN;
1650*5e3eaea3SApple OSS Distributions 	}
1651*5e3eaea3SApple OSS Distributions }
1652*5e3eaea3SApple OSS Distributions 
1653*5e3eaea3SApple OSS Distributions #endif /* !__has_ptrcheck */
1654*5e3eaea3SApple OSS Distributions #endif
1655