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