xref: /xnu-10002.81.5/tools/lldbmacros/kcdata.py (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions#!/usr/bin/env python3
2*5e3eaea3SApple OSS Distributionsfrom __future__ import absolute_import, print_function, division
3*5e3eaea3SApple OSS Distributionsimport sys
4*5e3eaea3SApple OSS Distributionsimport struct
5*5e3eaea3SApple OSS Distributionsimport mmap
6*5e3eaea3SApple OSS Distributionsimport json
7*5e3eaea3SApple OSS Distributionsimport copy
8*5e3eaea3SApple OSS Distributionsimport re
9*5e3eaea3SApple OSS Distributionsimport base64
10*5e3eaea3SApple OSS Distributionsimport argparse
11*5e3eaea3SApple OSS Distributionsimport logging
12*5e3eaea3SApple OSS Distributionsimport contextlib
13*5e3eaea3SApple OSS Distributionsimport base64
14*5e3eaea3SApple OSS Distributionsimport zlib
15*5e3eaea3SApple OSS Distributionsimport six
16*5e3eaea3SApple OSS Distributions
17*5e3eaea3SApple OSS Distributionsif six.PY3:
18*5e3eaea3SApple OSS Distributions    long = int
19*5e3eaea3SApple OSS Distributionselse:
20*5e3eaea3SApple OSS Distributions    # can be removed once we move to Python3.1+
21*5e3eaea3SApple OSS Distributions    from future.utils.surrogateescape import register_surrogateescape
22*5e3eaea3SApple OSS Distributions    register_surrogateescape()
23*5e3eaea3SApple OSS Distributions
24*5e3eaea3SApple OSS Distributionsclass Globals(object):
25*5e3eaea3SApple OSS Distributions    pass
26*5e3eaea3SApple OSS DistributionsG = Globals()
27*5e3eaea3SApple OSS DistributionsG.accept_incomplete_data = False
28*5e3eaea3SApple OSS DistributionsG.data_was_incomplete = False
29*5e3eaea3SApple OSS Distributions
30*5e3eaea3SApple OSS Distributionskcdata_type_def = {
31*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_INVALID':              0x0,
32*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_STRING_DESC':          0x1,
33*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_UINT32_DESC':          0x2,
34*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_UINT64_DESC':          0x3,
35*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_INT32_DESC':           0x4,
36*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_INT64_DESC':           0x5,
37*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_BINDATA_DESC':         0x6,
38*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_ARRAY':                0x11,
39*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_TYPEDEFINITION':       0x12,
40*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_CONTAINER_BEGIN':      0x13,
41*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_CONTAINER_END':        0x14,
42*5e3eaea3SApple OSS Distributions
43*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_ARRAY_PAD0':           0x20,
44*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_ARRAY_PAD1':           0x21,
45*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_ARRAY_PAD2':           0x22,
46*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_ARRAY_PAD3':           0x23,
47*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_ARRAY_PAD4':           0x24,
48*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_ARRAY_PAD5':           0x25,
49*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_ARRAY_PAD6':           0x26,
50*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_ARRAY_PAD7':           0x27,
51*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_ARRAY_PAD8':           0x28,
52*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_ARRAY_PAD9':           0x29,
53*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_ARRAY_PADa':           0x2a,
54*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_ARRAY_PADb':           0x2b,
55*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_ARRAY_PADc':           0x2c,
56*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_ARRAY_PADd':           0x2d,
57*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_ARRAY_PADe':           0x2e,
58*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_ARRAY_PADf':           0x2f,
59*5e3eaea3SApple OSS Distributions
60*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_LIBRARY_LOADINFO':     0x30,
61*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_LIBRARY_LOADINFO64':   0x31,
62*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_TIMEBASE':             0x32,
63*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_MACH_ABSOLUTE_TIME':   0x33,
64*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_TIMEVAL':              0x34,
65*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_USECS_SINCE_EPOCH':    0x35,
66*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_PID':                  0x36,
67*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_PROCNAME':             0x37,
68*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_NESTED_KCDATA':        0x38,
69*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_LIBRARY_AOTINFO':      0x39,
70*5e3eaea3SApple OSS Distributions
71*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCCONTAINER_TASK':       0x903,
72*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCCONTAINER_THREAD':     0x904,
73*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_DONATING_PIDS':   0x907,
74*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_SHAREDCACHE_LOADINFO': 0x908,
75*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_THREAD_NAME':     0x909,
76*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_KERN_STACKFRAME': 0x90A,
77*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_KERN_STACKFRAME64': 0x90B,
78*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_USER_STACKFRAME': 0x90C,
79*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_USER_STACKFRAME64': 0x90D,
80*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_BOOTARGS':        0x90E,
81*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_OSVERSION':       0x90F,
82*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_KERN_PAGE_SIZE':  0x910,
83*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_JETSAM_LEVEL':    0x911,
84*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_DELTA_SINCE_TIMESTAMP': 0x912,
85*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_KERN_STACKLR':  0x913,
86*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_KERN_STACKLR64':  0x914,
87*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_USER_STACKLR':  0x915,
88*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_USER_STACKLR64':  0x916,
89*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_NONRUNNABLE_TIDS':  0x917,
90*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_NONRUNNABLE_TASKS':  0x918,
91*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_CPU_TIMES': 0x919,
92*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_STACKSHOT_DURATION': 0x91a,
93*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_STACKSHOT_FAULT_STATS': 0x91b,
94*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_KERNELCACHE_LOADINFO': 0x91c,
95*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_THREAD_WAITINFO' : 0x91d,
96*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_THREAD_GROUP_SNAPSHOT' : 0x91e,
97*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_THREAD_GROUP' : 0x91f,
98*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_JETSAM_COALITION_SNAPSHOT' : 0x920,
99*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_JETSAM_COALITION' : 0x921,
100*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_THREAD_POLICY_VERSION': 0x922,
101*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_INSTRS_CYCLES' : 0x923,
102*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_USER_STACKTOP' : 0x924,
103*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_ASID' : 0x925,
104*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_PAGE_TABLES' : 0x926,
105*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_SYS_SHAREDCACHE_LAYOUT' : 0x927,
106*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_THREAD_DISPATCH_QUEUE_LABEL' : 0x928,
107*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_THREAD_TURNSTILEINFO' : 0x929,
108*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_TASK_CPU_ARCHITECTURE' : 0x92a,
109*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_LATENCY_INFO' : 0x92b,
110*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_LATENCY_INFO_TASK' : 0x92c,
111*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_LATENCY_INFO_THREAD' : 0x92d,
112*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_LOADINFO64_TEXT_EXEC' : 0x92e,
113*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_AOTCACHE_LOADINFO' : 0x92f,
114*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_TRANSITIONING_TASK_SNAPSHOT' : 0x930,
115*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCCONTAINER_TRANSITIONING_TASK' : 0x931,
116*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_USER_ASYNC_START_INDEX' : 0x932,
117*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_USER_ASYNC_STACKLR64' : 0x933,
118*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCCONTAINER_PORTLABEL' : 0x934,
119*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_PORTLABEL' : 0x935,
120*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_PORTLABEL_NAME' : 0x936,
121*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_DYLD_COMPACTINFO' : 0x937,
122*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_SUSPENSION_INFO' : 0x938,
123*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_SUSPENSION_SOURCE' : 0x939,
124*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_TASK_DELTA_SNAPSHOT': 0x940,
125*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_THREAD_DELTA_SNAPSHOT': 0x941,
126*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCCONTAINER_SHAREDCACHE' : 0x942,
127*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_SHAREDCACHE_INFO' : 0x943,
128*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_SHAREDCACHE_AOTINFO' : 0x944,
129*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_SHAREDCACHE_ID' : 0x945,
130*5e3eaea3SApple OSS Distributions    'STACKSHOT_KCTYPE_CODESIGNING_INFO' : 0x946,
131*5e3eaea3SApple OSS Distributions
132*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_BUFFER_END':      0xF19158ED,
133*5e3eaea3SApple OSS Distributions
134*5e3eaea3SApple OSS Distributions    'TASK_CRASHINFO_EXTMODINFO':           0x801,
135*5e3eaea3SApple OSS Distributions    'TASK_CRASHINFO_BSDINFOWITHUNIQID':    0x802,
136*5e3eaea3SApple OSS Distributions    'TASK_CRASHINFO_TASKDYLD_INFO':        0x803,
137*5e3eaea3SApple OSS Distributions    'TASK_CRASHINFO_UUID':                 0x804,
138*5e3eaea3SApple OSS Distributions    'TASK_CRASHINFO_PID':                  0x805,
139*5e3eaea3SApple OSS Distributions    'TASK_CRASHINFO_PPID':                 0x806,
140*5e3eaea3SApple OSS Distributions
141*5e3eaea3SApple OSS Distributions    # Don't want anyone using this.  It's struct rusage from whatever machine generated the data
142*5e3eaea3SApple OSS Distributions    #'TASK_CRASHINFO_RUSAGE':               0x807,
143*5e3eaea3SApple OSS Distributions    'Type_0x807':               0x807,
144*5e3eaea3SApple OSS Distributions
145*5e3eaea3SApple OSS Distributions    'TASK_CRASHINFO_RUSAGE_INFO':          0x808,
146*5e3eaea3SApple OSS Distributions    'TASK_CRASHINFO_PROC_NAME':            0x809,
147*5e3eaea3SApple OSS Distributions    'TASK_CRASHINFO_PROC_STARTTIME':       0x80B,
148*5e3eaea3SApple OSS Distributions    'TASK_CRASHINFO_USERSTACK':            0x80C,
149*5e3eaea3SApple OSS Distributions    'TASK_CRASHINFO_ARGSLEN':              0x80D,
150*5e3eaea3SApple OSS Distributions    'TASK_CRASHINFO_EXCEPTION_CODES':      0x80E,
151*5e3eaea3SApple OSS Distributions    'TASK_CRASHINFO_PROC_PATH':            0x80F,
152*5e3eaea3SApple OSS Distributions    'TASK_CRASHINFO_PROC_CSFLAGS':         0x810,
153*5e3eaea3SApple OSS Distributions    'TASK_CRASHINFO_PROC_STATUS':          0x811,
154*5e3eaea3SApple OSS Distributions    'TASK_CRASHINFO_UID':                  0x812,
155*5e3eaea3SApple OSS Distributions    'TASK_CRASHINFO_GID':                  0x813,
156*5e3eaea3SApple OSS Distributions    'TASK_CRASHINFO_PROC_ARGC':            0x814,
157*5e3eaea3SApple OSS Distributions    'TASK_CRASHINFO_PROC_FLAGS':           0x815,
158*5e3eaea3SApple OSS Distributions    'TASK_CRASHINFO_CPUTYPE':              0x816,
159*5e3eaea3SApple OSS Distributions    'TASK_CRASHINFO_WORKQUEUEINFO':        0x817,
160*5e3eaea3SApple OSS Distributions    'TASK_CRASHINFO_RESPONSIBLE_PID':      0x818,
161*5e3eaea3SApple OSS Distributions    'TASK_CRASHINFO_DIRTY_FLAGS':          0x819,
162*5e3eaea3SApple OSS Distributions    'TASK_CRASHINFO_CRASHED_THREADID':     0x81A,
163*5e3eaea3SApple OSS Distributions    'TASK_CRASHINFO_COALITION_ID':         0x81B,
164*5e3eaea3SApple OSS Distributions    'EXIT_REASON_SNAPSHOT':                0x1001,
165*5e3eaea3SApple OSS Distributions    'EXIT_REASON_USER_DESC':               0x1002,
166*5e3eaea3SApple OSS Distributions    'EXIT_REASON_USER_PAYLOAD':            0x1003,
167*5e3eaea3SApple OSS Distributions    'EXIT_REASON_CODESIGNING_INFO':        0x1004,
168*5e3eaea3SApple OSS Distributions    'EXIT_REASON_WORKLOOP_ID':             0x1005,
169*5e3eaea3SApple OSS Distributions    'EXIT_REASON_DISPATCH_QUEUE_NO':       0x1006,
170*5e3eaea3SApple OSS Distributions    'KCDATA_BUFFER_BEGIN_CRASHINFO':       0xDEADF157,
171*5e3eaea3SApple OSS Distributions    'KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT': 0xDE17A59A,
172*5e3eaea3SApple OSS Distributions    'KCDATA_BUFFER_BEGIN_STACKSHOT':       0x59a25807,
173*5e3eaea3SApple OSS Distributions    'KCDATA_BUFFER_BEGIN_COMPRESSED':      0x434f4d50,
174*5e3eaea3SApple OSS Distributions    'KCDATA_BUFFER_BEGIN_OS_REASON':       0x53A20900,
175*5e3eaea3SApple OSS Distributions    'KCDATA_BUFFER_BEGIN_XNUPOST_CONFIG':  0x1E21C09F
176*5e3eaea3SApple OSS Distributions}
177*5e3eaea3SApple OSS Distributionskcdata_type_def_rev = dict((v, k) for k, v in iter(kcdata_type_def.items()))
178*5e3eaea3SApple OSS Distributions
179*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION = {}
180*5e3eaea3SApple OSS Distributions
181*5e3eaea3SApple OSS DistributionsKNOWN_TOPLEVEL_CONTAINER_TYPES = ()
182*5e3eaea3SApple OSS Distributions
183*5e3eaea3SApple OSS Distributionsdef enum(**args):
184*5e3eaea3SApple OSS Distributions    return type('enum', (), args)
185*5e3eaea3SApple OSS Distributions
186*5e3eaea3SApple OSS Distributions#
187*5e3eaea3SApple OSS Distributions# Decode bytes as UTF-8, using surrogateescape if there are invalid UTF-8
188*5e3eaea3SApple OSS Distributions# sequences; see PEP-383
189*5e3eaea3SApple OSS Distributions#
190*5e3eaea3SApple OSS Distributionsdef BytesToString(b):
191*5e3eaea3SApple OSS Distributions    if isinstance(b, six.string_types):
192*5e3eaea3SApple OSS Distributions        return b
193*5e3eaea3SApple OSS Distributions    return b.decode('utf-8', errors="surrogateescape")
194*5e3eaea3SApple OSS Distributions
195*5e3eaea3SApple OSS Distributions# important keys
196*5e3eaea3SApple OSS DistributionsSC_SLID_FIRSTMAPPING_KEY = 'sharedCacheSlidFirstMapping'
197*5e3eaea3SApple OSS Distributions
198*5e3eaea3SApple OSS Distributions# important builtin types
199*5e3eaea3SApple OSS DistributionsKCSUBTYPE_TYPE = enum(KC_ST_CHAR=1, KC_ST_INT8=2, KC_ST_UINT8=3, KC_ST_INT16=4, KC_ST_UINT16=5, KC_ST_INT32=6, KC_ST_UINT32=7, KC_ST_INT64=8, KC_ST_UINT64=9)
200*5e3eaea3SApple OSS Distributions
201*5e3eaea3SApple OSS Distributions
202*5e3eaea3SApple OSS DistributionsLEGAL_OLD_STYLE_ARRAY_TYPE_NAMES = ['KCDATA_TYPE_LIBRARY_LOADINFO',
203*5e3eaea3SApple OSS Distributions                                    'KCDATA_TYPE_LIBRARY_LOADINFO64',
204*5e3eaea3SApple OSS Distributions                                    'STACKSHOT_KCTYPE_KERN_STACKFRAME',
205*5e3eaea3SApple OSS Distributions                                    'STACKSHOT_KCTYPE_USER_STACKFRAME',
206*5e3eaea3SApple OSS Distributions                                    'STACKSHOT_KCTYPE_KERN_STACKFRAME64',
207*5e3eaea3SApple OSS Distributions                                    'STACKSHOT_KCTYPE_USER_STACKFRAME64',
208*5e3eaea3SApple OSS Distributions                                    'STACKSHOT_KCTYPE_DONATING_PIDS',
209*5e3eaea3SApple OSS Distributions                                    'STACKSHOT_KCTYPE_THREAD_DELTA_SNAPSHOT']
210*5e3eaea3SApple OSS Distributions
211*5e3eaea3SApple OSS DistributionsKCDATA_FLAGS_STRUCT_PADDING_MASK = 0xf
212*5e3eaea3SApple OSS DistributionsKCDATA_FLAGS_STRUCT_HAS_PADDING = 0x80
213*5e3eaea3SApple OSS Distributions
214*5e3eaea3SApple OSS Distributionsclass KCSubTypeElement(object):
215*5e3eaea3SApple OSS Distributions    """convert kcdata_subtype_descriptor to """
216*5e3eaea3SApple OSS Distributions    _unpack_formats = (None, 'c', 'b', 'B', 'h', 'H', 'i', 'I', 'q', 'Q')
217*5e3eaea3SApple OSS Distributions    _ctypes = ('Unknown', 'char', 'int8_t', 'uint8_t', 'int16_t', 'uint16_t', 'int32_t', 'uint32_t', 'int64_t', 'uint64_t')
218*5e3eaea3SApple OSS Distributions
219*5e3eaea3SApple OSS Distributions    def __init__(self, st_name, st_type, st_size, st_offset=0, st_flag=0, custom_repr=None):
220*5e3eaea3SApple OSS Distributions        self.name = st_name
221*5e3eaea3SApple OSS Distributions        self.offset = st_offset
222*5e3eaea3SApple OSS Distributions        self.type_id = st_type
223*5e3eaea3SApple OSS Distributions        if st_type <= 0 or st_type > KCSUBTYPE_TYPE.KC_ST_UINT64:
224*5e3eaea3SApple OSS Distributions            raise ValueError("Invalid type passed %d" % st_type)
225*5e3eaea3SApple OSS Distributions        self.unpack_fmt = KCSubTypeElement._unpack_formats[self.type_id]
226*5e3eaea3SApple OSS Distributions        self.size = st_size
227*5e3eaea3SApple OSS Distributions        self.totalsize = st_size
228*5e3eaea3SApple OSS Distributions        self.count = 1
229*5e3eaea3SApple OSS Distributions        self.is_array_type = False
230*5e3eaea3SApple OSS Distributions        self.custom_JsonRepr = custom_repr
231*5e3eaea3SApple OSS Distributions        if (st_flag & 0x1) == 0x1:
232*5e3eaea3SApple OSS Distributions            self.is_array_type = True
233*5e3eaea3SApple OSS Distributions            self.size = st_size & 0xffff
234*5e3eaea3SApple OSS Distributions            self.count = (st_size >> 16) & 0xffff
235*5e3eaea3SApple OSS Distributions            self.totalsize = self.size * self.count
236*5e3eaea3SApple OSS Distributions
237*5e3eaea3SApple OSS Distributions    @staticmethod
238*5e3eaea3SApple OSS Distributions    def GetSizeForArray(el_count, el_size):
239*5e3eaea3SApple OSS Distributions        return ((el_count & 0xffff) << 16) | (el_size & 0xffff)
240*5e3eaea3SApple OSS Distributions
241*5e3eaea3SApple OSS Distributions    @staticmethod
242*5e3eaea3SApple OSS Distributions    def FromBinaryTypeData(byte_data):
243*5e3eaea3SApple OSS Distributions        (st_flag, st_type, st_offset, st_size, st_name) = struct.unpack_from('=BBHI32s', byte_data)
244*5e3eaea3SApple OSS Distributions        st_name = BytesToString(st_name).rstrip('\0')
245*5e3eaea3SApple OSS Distributions        return KCSubTypeElement(st_name, st_type, st_size, st_offset, st_flag)
246*5e3eaea3SApple OSS Distributions
247*5e3eaea3SApple OSS Distributions    @staticmethod
248*5e3eaea3SApple OSS Distributions    def FromBasicCtype(st_name, st_type, st_offset=0, legacy_size=None):
249*5e3eaea3SApple OSS Distributions        if st_type <= 0 or st_type > KCSUBTYPE_TYPE.KC_ST_UINT64:
250*5e3eaea3SApple OSS Distributions            raise ValueError("Invalid type passed %d" % st_type)
251*5e3eaea3SApple OSS Distributions        st_size = struct.calcsize(KCSubTypeElement._unpack_formats[st_type])
252*5e3eaea3SApple OSS Distributions        st_flag = 0
253*5e3eaea3SApple OSS Distributions        retval = KCSubTypeElement(st_name, st_type, st_size, st_offset, st_flag, KCSubTypeElement._get_naked_element_value)
254*5e3eaea3SApple OSS Distributions        if legacy_size:
255*5e3eaea3SApple OSS Distributions            retval.legacy_size = legacy_size
256*5e3eaea3SApple OSS Distributions        return retval
257*5e3eaea3SApple OSS Distributions
258*5e3eaea3SApple OSS Distributions    @staticmethod
259*5e3eaea3SApple OSS Distributions    def FromKCSubTypeElement(other, name_override=''):
260*5e3eaea3SApple OSS Distributions        _copy = copy.copy(other)
261*5e3eaea3SApple OSS Distributions        if name_override:
262*5e3eaea3SApple OSS Distributions            _copy.name = name_override
263*5e3eaea3SApple OSS Distributions        return copy
264*5e3eaea3SApple OSS Distributions
265*5e3eaea3SApple OSS Distributions    def GetName(self):
266*5e3eaea3SApple OSS Distributions        return self.name
267*5e3eaea3SApple OSS Distributions
268*5e3eaea3SApple OSS Distributions    def GetTotalSize(self):
269*5e3eaea3SApple OSS Distributions        return self.totalsize
270*5e3eaea3SApple OSS Distributions
271*5e3eaea3SApple OSS Distributions    def GetValueAsString(self, base_data, array_pos=0):
272*5e3eaea3SApple OSS Distributions        v = self.GetValue(base_data, array_pos)
273*5e3eaea3SApple OSS Distributions        if isinstance(v, bytes):
274*5e3eaea3SApple OSS Distributions            return BytesToString(v)
275*5e3eaea3SApple OSS Distributions        return str(v)
276*5e3eaea3SApple OSS Distributions
277*5e3eaea3SApple OSS Distributions    def GetValue(self, base_data, array_pos=0):
278*5e3eaea3SApple OSS Distributions        return struct.unpack_from(self.unpack_fmt, base_data[self.offset + (array_pos * self.size):])[0]
279*5e3eaea3SApple OSS Distributions
280*5e3eaea3SApple OSS Distributions    @staticmethod
281*5e3eaea3SApple OSS Distributions    def _get_naked_element_value(elementValue, elementName):
282*5e3eaea3SApple OSS Distributions        return json.dumps(elementValue)
283*5e3eaea3SApple OSS Distributions
284*5e3eaea3SApple OSS Distributions    def __str__(self):
285*5e3eaea3SApple OSS Distributions        if self.is_array_type:
286*5e3eaea3SApple OSS Distributions            return '[%d,%d] %s  %s[%d];' % (self.offset, self.totalsize, self.GetCTypeDesc(), self.name, self.count)
287*5e3eaea3SApple OSS Distributions        return '[%d,%d] %s  %s;' % (self.offset, self.totalsize, self.GetCTypeDesc(), self.name)
288*5e3eaea3SApple OSS Distributions
289*5e3eaea3SApple OSS Distributions    def __repr__(self):
290*5e3eaea3SApple OSS Distributions        return str(self)
291*5e3eaea3SApple OSS Distributions
292*5e3eaea3SApple OSS Distributions    def GetCTypeDesc(self):
293*5e3eaea3SApple OSS Distributions        return KCSubTypeElement._ctypes[self.type_id]
294*5e3eaea3SApple OSS Distributions
295*5e3eaea3SApple OSS Distributions    def GetStringRepr(self, base_data):
296*5e3eaea3SApple OSS Distributions        if not self.is_array_type:
297*5e3eaea3SApple OSS Distributions            return self.GetValueAsString(base_data)
298*5e3eaea3SApple OSS Distributions        if self.type_id == KCSUBTYPE_TYPE.KC_ST_CHAR:
299*5e3eaea3SApple OSS Distributions            str_len = self.count
300*5e3eaea3SApple OSS Distributions            if len(base_data) < str_len:
301*5e3eaea3SApple OSS Distributions                str_len = len(base_data)
302*5e3eaea3SApple OSS Distributions            str_arr = []
303*5e3eaea3SApple OSS Distributions            for i in range(str_len):
304*5e3eaea3SApple OSS Distributions                _v = self.GetValue(base_data, i)
305*5e3eaea3SApple OSS Distributions                if ord(_v) == 0:
306*5e3eaea3SApple OSS Distributions                    break
307*5e3eaea3SApple OSS Distributions                str_arr.append(self.GetValueAsString(base_data, i))
308*5e3eaea3SApple OSS Distributions            return json.dumps(''.join(str_arr))
309*5e3eaea3SApple OSS Distributions
310*5e3eaea3SApple OSS Distributions        count = self.count
311*5e3eaea3SApple OSS Distributions        if count > len(base_data)//self.size:
312*5e3eaea3SApple OSS Distributions            count = len(base_data)//self.size
313*5e3eaea3SApple OSS Distributions
314*5e3eaea3SApple OSS Distributions        o = '[' + ','.join([self.GetValueAsString(base_data, i) for i in range(count)]) + ']'
315*5e3eaea3SApple OSS Distributions
316*5e3eaea3SApple OSS Distributions        return o
317*5e3eaea3SApple OSS Distributions
318*5e3eaea3SApple OSS Distributions    def GetJsonRepr(self, base_data, flags=0):
319*5e3eaea3SApple OSS Distributions        if (flags & (KCDATA_FLAGS_STRUCT_HAS_PADDING | KCDATA_FLAGS_STRUCT_PADDING_MASK)) != 0:
320*5e3eaea3SApple OSS Distributions            padding = (flags & KCDATA_FLAGS_STRUCT_PADDING_MASK)
321*5e3eaea3SApple OSS Distributions            if padding:
322*5e3eaea3SApple OSS Distributions                base_data = base_data[:-padding]
323*5e3eaea3SApple OSS Distributions        if self.custom_JsonRepr:
324*5e3eaea3SApple OSS Distributions            if self.is_array_type:
325*5e3eaea3SApple OSS Distributions                e_data = [self.GetValue(base_data, i) for i in range(self.count)]
326*5e3eaea3SApple OSS Distributions            else:
327*5e3eaea3SApple OSS Distributions                e_data = self.GetValue(base_data)
328*5e3eaea3SApple OSS Distributions            return self.custom_JsonRepr(e_data, self.name)
329*5e3eaea3SApple OSS Distributions        return self.GetStringRepr(base_data)
330*5e3eaea3SApple OSS Distributions
331*5e3eaea3SApple OSS Distributions    def sizeof(self):
332*5e3eaea3SApple OSS Distributions        return self.totalsize
333*5e3eaea3SApple OSS Distributions
334*5e3eaea3SApple OSS Distributions    def ShouldSkip(self, data):
335*5e3eaea3SApple OSS Distributions        return len(data) < self.offset + self.totalsize
336*5e3eaea3SApple OSS Distributions
337*5e3eaea3SApple OSS Distributions    def ShouldMerge(self):
338*5e3eaea3SApple OSS Distributions        return False
339*5e3eaea3SApple OSS Distributions
340*5e3eaea3SApple OSS Distributions
341*5e3eaea3SApple OSS Distributionsclass KCTypeDescription(object):
342*5e3eaea3SApple OSS Distributions    def __init__(self, t_type_id, t_elements=[], t_name='anon', custom_repr=None, legacy_size=None, merge=False, naked=False):
343*5e3eaea3SApple OSS Distributions        self.type_id = t_type_id
344*5e3eaea3SApple OSS Distributions        self.elements = t_elements
345*5e3eaea3SApple OSS Distributions        self.name = t_name
346*5e3eaea3SApple OSS Distributions        self.totalsize = 0
347*5e3eaea3SApple OSS Distributions        self.custom_JsonRepr = custom_repr
348*5e3eaea3SApple OSS Distributions        if legacy_size:
349*5e3eaea3SApple OSS Distributions            self.legacy_size = legacy_size
350*5e3eaea3SApple OSS Distributions        self.merge = merge
351*5e3eaea3SApple OSS Distributions        self.naked = naked
352*5e3eaea3SApple OSS Distributions        for e in self.elements:
353*5e3eaea3SApple OSS Distributions            self.totalsize += e.GetTotalSize()
354*5e3eaea3SApple OSS Distributions
355*5e3eaea3SApple OSS Distributions    def ValidateData(self, base_data):
356*5e3eaea3SApple OSS Distributions        if len(base_data) >= self.totalsize:
357*5e3eaea3SApple OSS Distributions            return True
358*5e3eaea3SApple OSS Distributions        return False
359*5e3eaea3SApple OSS Distributions
360*5e3eaea3SApple OSS Distributions    def GetTypeID(self):
361*5e3eaea3SApple OSS Distributions        return self.type_id
362*5e3eaea3SApple OSS Distributions
363*5e3eaea3SApple OSS Distributions    def GetName(self):
364*5e3eaea3SApple OSS Distributions        return self.name
365*5e3eaea3SApple OSS Distributions
366*5e3eaea3SApple OSS Distributions    def __str__(self):
367*5e3eaea3SApple OSS Distributions        o = '%s {\n\t' % self.name + "\n\t".join([str(e) for e in self.elements]) + '\n};'
368*5e3eaea3SApple OSS Distributions        return o
369*5e3eaea3SApple OSS Distributions
370*5e3eaea3SApple OSS Distributions    @staticmethod
371*5e3eaea3SApple OSS Distributions    def FromKCTypeDescription(other, t_type_id, t_name):
372*5e3eaea3SApple OSS Distributions        retval = KCTypeDescription(t_type_id, other.elements, t_name, other.custom_JsonRepr,
373*5e3eaea3SApple OSS Distributions                                   legacy_size=getattr(other, 'legacy_size', None))
374*5e3eaea3SApple OSS Distributions        return retval
375*5e3eaea3SApple OSS Distributions
376*5e3eaea3SApple OSS Distributions    def ShouldMerge(self):
377*5e3eaea3SApple OSS Distributions        return self.merge
378*5e3eaea3SApple OSS Distributions
379*5e3eaea3SApple OSS Distributions    def GetJsonRepr(self, base_data, flags):
380*5e3eaea3SApple OSS Distributions        if (flags & (KCDATA_FLAGS_STRUCT_HAS_PADDING | KCDATA_FLAGS_STRUCT_PADDING_MASK)) != 0:
381*5e3eaea3SApple OSS Distributions            padding = (flags & KCDATA_FLAGS_STRUCT_PADDING_MASK)
382*5e3eaea3SApple OSS Distributions            if padding:
383*5e3eaea3SApple OSS Distributions                base_data = base_data[:-padding]
384*5e3eaea3SApple OSS Distributions        elif hasattr(self, 'legacy_size') and len(base_data) == self.legacy_size + ((-self.legacy_size) & 0xf):
385*5e3eaea3SApple OSS Distributions            base_data = base_data[:self.legacy_size]
386*5e3eaea3SApple OSS Distributions        if self.custom_JsonRepr:
387*5e3eaea3SApple OSS Distributions            return self.custom_JsonRepr([e.GetValue(base_data) for e in self.elements])
388*5e3eaea3SApple OSS Distributions        if self.naked:
389*5e3eaea3SApple OSS Distributions            o = ", ".join([e.GetJsonRepr(base_data) for e in self.elements if not e.ShouldSkip(base_data)])
390*5e3eaea3SApple OSS Distributions        else:
391*5e3eaea3SApple OSS Distributions            o = ", ".join(['"%s": %s' % (e.GetName(), e.GetJsonRepr(base_data)) for e in self.elements if not e.ShouldSkip(base_data)])
392*5e3eaea3SApple OSS Distributions        if not self.merge:
393*5e3eaea3SApple OSS Distributions            o = '{' + o + '}'
394*5e3eaea3SApple OSS Distributions        return o
395*5e3eaea3SApple OSS Distributions
396*5e3eaea3SApple OSS Distributions    def sizeof(self):
397*5e3eaea3SApple OSS Distributions        return max(st.totalsize + st.offset for st in self.elements)
398*5e3eaea3SApple OSS Distributions
399*5e3eaea3SApple OSS Distributions
400*5e3eaea3SApple OSS Distributionsdef GetTypeNameForKey(k):
401*5e3eaea3SApple OSS Distributions    retval = "0x%x" % k
402*5e3eaea3SApple OSS Distributions    if k in KNOWN_TYPES_COLLECTION:
403*5e3eaea3SApple OSS Distributions        retval = KNOWN_TYPES_COLLECTION[k].GetName()
404*5e3eaea3SApple OSS Distributions    elif k in kcdata_type_def_rev:
405*5e3eaea3SApple OSS Distributions        retval = kcdata_type_def_rev[k]
406*5e3eaea3SApple OSS Distributions    return retval
407*5e3eaea3SApple OSS Distributions
408*5e3eaea3SApple OSS Distributions
409*5e3eaea3SApple OSS Distributionsdef GetTypeForName(n):
410*5e3eaea3SApple OSS Distributions    ret = 0
411*5e3eaea3SApple OSS Distributions    if n in kcdata_type_def:
412*5e3eaea3SApple OSS Distributions        ret = kcdata_type_def[n]
413*5e3eaea3SApple OSS Distributions    return ret
414*5e3eaea3SApple OSS Distributions
415*5e3eaea3SApple OSS Distributions
416*5e3eaea3SApple OSS DistributionsLEGAL_OLD_STYLE_ARRAY_TYPES = list(map(GetTypeForName, LEGAL_OLD_STYLE_ARRAY_TYPE_NAMES))
417*5e3eaea3SApple OSS Distributions
418*5e3eaea3SApple OSS Distributionskcdata_type_def_rev[GetTypeForName('KCDATA_BUFFER_BEGIN_STACKSHOT')] = 'kcdata_stackshot'
419*5e3eaea3SApple OSS Distributionskcdata_type_def_rev[GetTypeForName('KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT')] = 'kcdata_delta_stackshot'
420*5e3eaea3SApple OSS Distributionskcdata_type_def_rev[GetTypeForName('KCDATA_BUFFER_BEGIN_CRASHINFO')] = 'kcdata_crashinfo'
421*5e3eaea3SApple OSS Distributionskcdata_type_def_rev[GetTypeForName('KCDATA_BUFFER_BEGIN_OS_REASON')] = 'kcdata_reason'
422*5e3eaea3SApple OSS Distributionskcdata_type_def_rev[GetTypeForName('STACKSHOT_KCCONTAINER_TASK')] = 'task_snapshots'
423*5e3eaea3SApple OSS Distributionskcdata_type_def_rev[GetTypeForName('STACKSHOT_KCCONTAINER_TRANSITIONING_TASK')] = 'transitioning_task_snapshots'
424*5e3eaea3SApple OSS Distributionskcdata_type_def_rev[GetTypeForName('STACKSHOT_KCCONTAINER_THREAD')] = 'thread_snapshots'
425*5e3eaea3SApple OSS Distributionskcdata_type_def_rev[GetTypeForName('STACKSHOT_KCCONTAINER_PORTLABEL')] = 'portlabels'
426*5e3eaea3SApple OSS Distributionskcdata_type_def_rev[GetTypeForName('STACKSHOT_KCCONTAINER_SHAREDCACHE')] = 'shared_caches'
427*5e3eaea3SApple OSS Distributionskcdata_type_def_rev[GetTypeForName('KCDATA_BUFFER_BEGIN_XNUPOST_CONFIG')] = 'xnupost_testconfig'
428*5e3eaea3SApple OSS Distributions
429*5e3eaea3SApple OSS Distributionsclass Indent(object):
430*5e3eaea3SApple OSS Distributions    def __init__(self):
431*5e3eaea3SApple OSS Distributions        self.n = 0
432*5e3eaea3SApple OSS Distributions    def __call__(self, end=False):
433*5e3eaea3SApple OSS Distributions        if end:
434*5e3eaea3SApple OSS Distributions            return " " * (self.n-4)
435*5e3eaea3SApple OSS Distributions        else:
436*5e3eaea3SApple OSS Distributions            return " " * self.n
437*5e3eaea3SApple OSS Distributions    @contextlib.contextmanager
438*5e3eaea3SApple OSS Distributions    def indent(self):
439*5e3eaea3SApple OSS Distributions        self.n += 4
440*5e3eaea3SApple OSS Distributions        try:
441*5e3eaea3SApple OSS Distributions            yield
442*5e3eaea3SApple OSS Distributions        finally:
443*5e3eaea3SApple OSS Distributions            self.n -= 4
444*5e3eaea3SApple OSS Distributions
445*5e3eaea3SApple OSS DistributionsINDENT = Indent()
446*5e3eaea3SApple OSS Distributions
447*5e3eaea3SApple OSS Distributionsclass KCObject(object):
448*5e3eaea3SApple OSS Distributions
449*5e3eaea3SApple OSS Distributions    def __init__(self, type_code, data, offset, flags=0):
450*5e3eaea3SApple OSS Distributions
451*5e3eaea3SApple OSS Distributions        self.i_type = type_code
452*5e3eaea3SApple OSS Distributions        self.i_data = data
453*5e3eaea3SApple OSS Distributions        self.offset = offset
454*5e3eaea3SApple OSS Distributions        self.i_size = len(data)
455*5e3eaea3SApple OSS Distributions        self.i_flags = flags
456*5e3eaea3SApple OSS Distributions        self.obj_collection = []
457*5e3eaea3SApple OSS Distributions        self.obj = {}
458*5e3eaea3SApple OSS Distributions        self.is_container_type = False
459*5e3eaea3SApple OSS Distributions        self.is_array_type = False
460*5e3eaea3SApple OSS Distributions        self.is_naked_type = False
461*5e3eaea3SApple OSS Distributions        self.nested_kcdata = None
462*5e3eaea3SApple OSS Distributions        self.i_name = GetTypeNameForKey(type_code)
463*5e3eaea3SApple OSS Distributions
464*5e3eaea3SApple OSS Distributions        self.ParseData()
465*5e3eaea3SApple OSS Distributions
466*5e3eaea3SApple OSS Distributions        if self.i_type == GetTypeForName('KCDATA_TYPE_CONTAINER_BEGIN'):
467*5e3eaea3SApple OSS Distributions            self.__class__ = KCContainerObject
468*5e3eaea3SApple OSS Distributions        elif self.i_type == GetTypeForName('KCDATA_BUFFER_BEGIN_COMPRESSED'):
469*5e3eaea3SApple OSS Distributions            self.__class__ = KCCompressedBufferObject
470*5e3eaea3SApple OSS Distributions        elif self.i_type in KNOWN_TOPLEVEL_CONTAINER_TYPES:
471*5e3eaea3SApple OSS Distributions            self.__class__ = KCBufferObject
472*5e3eaea3SApple OSS Distributions
473*5e3eaea3SApple OSS Distributions        self.InitAfterParse()
474*5e3eaea3SApple OSS Distributions
475*5e3eaea3SApple OSS Distributions    def __str__(self):
476*5e3eaea3SApple OSS Distributions        return "<KCObject at 0x%x>" % self.offset
477*5e3eaea3SApple OSS Distributions
478*5e3eaea3SApple OSS Distributions    def InitAfterParse(self):
479*5e3eaea3SApple OSS Distributions        pass
480*5e3eaea3SApple OSS Distributions
481*5e3eaea3SApple OSS Distributions    @staticmethod
482*5e3eaea3SApple OSS Distributions    def FromKCItem(kcitem):
483*5e3eaea3SApple OSS Distributions        return KCObject(kcitem.i_type, kcitem.i_data, kcitem.i_offset, kcitem.i_flags)
484*5e3eaea3SApple OSS Distributions
485*5e3eaea3SApple OSS Distributions    def IsContainerEnd(self):
486*5e3eaea3SApple OSS Distributions        return self.i_type == GetTypeForName('KCDATA_TYPE_CONTAINER_END')
487*5e3eaea3SApple OSS Distributions
488*5e3eaea3SApple OSS Distributions    def IsBufferEnd(self):
489*5e3eaea3SApple OSS Distributions        return self.i_type == GetTypeForName('KCDATA_TYPE_BUFFER_END')
490*5e3eaea3SApple OSS Distributions
491*5e3eaea3SApple OSS Distributions    def IsArray(self):
492*5e3eaea3SApple OSS Distributions        return self.is_array_type
493*5e3eaea3SApple OSS Distributions
494*5e3eaea3SApple OSS Distributions    def ShouldMerge(self):
495*5e3eaea3SApple OSS Distributions        if self.nested_kcdata:
496*5e3eaea3SApple OSS Distributions            return True
497*5e3eaea3SApple OSS Distributions        elif not self.is_array_type and self.i_type in KNOWN_TYPES_COLLECTION:
498*5e3eaea3SApple OSS Distributions            return KNOWN_TYPES_COLLECTION[self.i_type].ShouldMerge()
499*5e3eaea3SApple OSS Distributions        else:
500*5e3eaea3SApple OSS Distributions            return False
501*5e3eaea3SApple OSS Distributions
502*5e3eaea3SApple OSS Distributions    def GetJsonRepr(self):
503*5e3eaea3SApple OSS Distributions        if self.is_array_type:
504*5e3eaea3SApple OSS Distributions            return '[' + ', '.join([i.GetJsonRepr() for i in self.obj_collection]) + ']'
505*5e3eaea3SApple OSS Distributions        if self.i_type in KNOWN_TYPES_COLLECTION:
506*5e3eaea3SApple OSS Distributions            return KNOWN_TYPES_COLLECTION[self.i_type].GetJsonRepr(self.i_data, self.i_flags)
507*5e3eaea3SApple OSS Distributions        if self.is_naked_type:
508*5e3eaea3SApple OSS Distributions            return json.dumps(self.obj)
509*5e3eaea3SApple OSS Distributions        if self.nested_kcdata:
510*5e3eaea3SApple OSS Distributions            return self.nested_kcdata.GetJsonRepr()
511*5e3eaea3SApple OSS Distributions
512*5e3eaea3SApple OSS Distributions        raise NotImplementedError("Broken GetJsonRepr implementation")
513*5e3eaea3SApple OSS Distributions
514*5e3eaea3SApple OSS Distributions    def ParseData(self):
515*5e3eaea3SApple OSS Distributions
516*5e3eaea3SApple OSS Distributions
517*5e3eaea3SApple OSS Distributions        if self.i_type == GetTypeForName('KCDATA_TYPE_CONTAINER_BEGIN'):
518*5e3eaea3SApple OSS Distributions            self.obj['uniqID'] = self.i_flags
519*5e3eaea3SApple OSS Distributions            self.i_name = str(self.obj['uniqID'])
520*5e3eaea3SApple OSS Distributions            self.obj['typeID'] = struct.unpack_from('I', self.i_data)[0]
521*5e3eaea3SApple OSS Distributions            logging.info("0x%08x: %sCONTAINER: %s(%x)" % (self.offset, INDENT(), GetTypeNameForKey(self.obj['typeID']), self.i_flags))
522*5e3eaea3SApple OSS Distributions
523*5e3eaea3SApple OSS Distributions        elif self.i_type in (KNOWN_TOPLEVEL_CONTAINER_TYPES):
524*5e3eaea3SApple OSS Distributions            self.obj['uniqID'] = self.i_name
525*5e3eaea3SApple OSS Distributions            self.obj['typeID'] = self.i_type
526*5e3eaea3SApple OSS Distributions            logging.info("0x%08x: %s%s" % (self.offset, INDENT(), self.i_name))
527*5e3eaea3SApple OSS Distributions
528*5e3eaea3SApple OSS Distributions        elif self.i_type == GetTypeForName('KCDATA_TYPE_CONTAINER_END'):
529*5e3eaea3SApple OSS Distributions            self.obj['uniqID'] = self.i_flags
530*5e3eaea3SApple OSS Distributions            logging.info("0x%08x: %sEND" % (self.offset, INDENT(end=True)))
531*5e3eaea3SApple OSS Distributions
532*5e3eaea3SApple OSS Distributions        elif self.i_type == GetTypeForName('KCDATA_TYPE_BUFFER_END'):
533*5e3eaea3SApple OSS Distributions            self.obj = ''
534*5e3eaea3SApple OSS Distributions            logging.info("0x%08x: %sEND_BUFFER" % (self.offset, INDENT(end=True)))
535*5e3eaea3SApple OSS Distributions
536*5e3eaea3SApple OSS Distributions        elif self.i_type == GetTypeForName('KCDATA_TYPE_UINT32_DESC'):
537*5e3eaea3SApple OSS Distributions            self.is_naked_type = True
538*5e3eaea3SApple OSS Distributions            u_d = struct.unpack_from('32sI', self.i_data)
539*5e3eaea3SApple OSS Distributions            self.i_name = BytesToString(u_d[0]).rstrip('\0')
540*5e3eaea3SApple OSS Distributions            self.obj = u_d[1]
541*5e3eaea3SApple OSS Distributions            logging.info("0x%08x: %s%s" % (self.offset, INDENT(), self.i_name))
542*5e3eaea3SApple OSS Distributions
543*5e3eaea3SApple OSS Distributions        elif self.i_type == GetTypeForName('KCDATA_TYPE_UINT64_DESC'):
544*5e3eaea3SApple OSS Distributions            self.is_naked_type = True
545*5e3eaea3SApple OSS Distributions            u_d = struct.unpack_from('32sQ', self.i_data)
546*5e3eaea3SApple OSS Distributions            self.i_name = BytesToString(u_d[0]).rstrip('\0')
547*5e3eaea3SApple OSS Distributions            self.obj = u_d[1]
548*5e3eaea3SApple OSS Distributions            logging.info("0x%08x: %s%s" % (self.offset, INDENT(), self.i_name))
549*5e3eaea3SApple OSS Distributions
550*5e3eaea3SApple OSS Distributions        elif self.i_type == GetTypeForName('KCDATA_TYPE_TYPEDEFINITION'):
551*5e3eaea3SApple OSS Distributions            self.is_naked_type = True
552*5e3eaea3SApple OSS Distributions            u_d = struct.unpack_from('II32s', self.i_data)
553*5e3eaea3SApple OSS Distributions            self.obj['name'] = BytesToString(u_d[2]).split(chr(0))[0]
554*5e3eaea3SApple OSS Distributions            self.i_name = "typedef[%s]" % self.obj['name']
555*5e3eaea3SApple OSS Distributions            self.obj['typeID'] = u_d[0]
556*5e3eaea3SApple OSS Distributions            self.obj['numOfFields'] = u_d[1]
557*5e3eaea3SApple OSS Distributions            element_arr = []
558*5e3eaea3SApple OSS Distributions            for i in range(u_d[1]):
559*5e3eaea3SApple OSS Distributions                e = KCSubTypeElement.FromBinaryTypeData(self.i_data[40+(i*40):])
560*5e3eaea3SApple OSS Distributions                element_arr.append(e)
561*5e3eaea3SApple OSS Distributions            type_desc = KCTypeDescription(u_d[0], element_arr, self.obj['name'])
562*5e3eaea3SApple OSS Distributions            self.obj['fields'] = [str(e) for e in element_arr]
563*5e3eaea3SApple OSS Distributions            KNOWN_TYPES_COLLECTION[type_desc.GetTypeID()] = type_desc
564*5e3eaea3SApple OSS Distributions            logging.info("0x%08x: %s%s" % (self.offset, INDENT(), self.i_name))
565*5e3eaea3SApple OSS Distributions
566*5e3eaea3SApple OSS Distributions        elif self.i_type == GetTypeForName('KCDATA_TYPE_ARRAY'):
567*5e3eaea3SApple OSS Distributions            self.is_array_type = True
568*5e3eaea3SApple OSS Distributions            e_t = (self.i_flags >> 32) & 0xffffffff
569*5e3eaea3SApple OSS Distributions            if e_t not in LEGAL_OLD_STYLE_ARRAY_TYPES:
570*5e3eaea3SApple OSS Distributions                raise Exception("illegal old-style array type: %s (0x%x)" % (GetTypeNameForKey(e_t), e_t))
571*5e3eaea3SApple OSS Distributions            e_c = self.i_flags & 0xffffffff
572*5e3eaea3SApple OSS Distributions            e_s = KNOWN_TYPES_COLLECTION[e_t].legacy_size
573*5e3eaea3SApple OSS Distributions            if e_s * e_c > self.i_size:
574*5e3eaea3SApple OSS Distributions                raise Exception("array too small for its count")
575*5e3eaea3SApple OSS Distributions            self.obj['typeID'] = e_t
576*5e3eaea3SApple OSS Distributions            self.i_name = GetTypeNameForKey(e_t)
577*5e3eaea3SApple OSS Distributions            self.i_type = e_t
578*5e3eaea3SApple OSS Distributions            self.obj['numOfElements'] = e_c
579*5e3eaea3SApple OSS Distributions            self.obj['sizeOfElement'] = e_s
580*5e3eaea3SApple OSS Distributions            logging.info("0x%08x: %sARRAY: %s" % (self.offset, INDENT(), self.i_name))
581*5e3eaea3SApple OSS Distributions            #populate the array here by recursive creation of KCObject
582*5e3eaea3SApple OSS Distributions            with INDENT.indent():
583*5e3eaea3SApple OSS Distributions                for _i in range(e_c):
584*5e3eaea3SApple OSS Distributions                    _o = KCObject(e_t, self.i_data[(_i * e_s):(_i * e_s) + e_s], self.offset + _i*e_s)
585*5e3eaea3SApple OSS Distributions                    self.obj_collection.append(_o)
586*5e3eaea3SApple OSS Distributions
587*5e3eaea3SApple OSS Distributions        elif self.i_type >= GetTypeForName('KCDATA_TYPE_ARRAY_PAD0') and self.i_type <= GetTypeForName('KCDATA_TYPE_ARRAY_PADf'):
588*5e3eaea3SApple OSS Distributions            self.is_array_type = True
589*5e3eaea3SApple OSS Distributions            e_t = (self.i_flags >> 32) & 0xffffffff
590*5e3eaea3SApple OSS Distributions            e_c = self.i_flags & 0xffffffff
591*5e3eaea3SApple OSS Distributions            e_s = (self.i_size - (self.i_type & 0xf)) // e_c if e_c != 0 else None
592*5e3eaea3SApple OSS Distributions            self.obj['typeID'] = e_t
593*5e3eaea3SApple OSS Distributions            self.i_name = GetTypeNameForKey(e_t)
594*5e3eaea3SApple OSS Distributions            self.i_type = e_t
595*5e3eaea3SApple OSS Distributions            self.obj['numOfElements'] = e_c
596*5e3eaea3SApple OSS Distributions            self.obj['sizeOfElement'] = e_s
597*5e3eaea3SApple OSS Distributions            logging.info("0x%08x: %sARRAY: %s" % (self.offset, INDENT(), self.i_name))
598*5e3eaea3SApple OSS Distributions            #populate the array here by recursive creation of KCObject
599*5e3eaea3SApple OSS Distributions            with INDENT.indent():
600*5e3eaea3SApple OSS Distributions                for _i in range(e_c):
601*5e3eaea3SApple OSS Distributions                    _o = KCObject(e_t, self.i_data[(_i * e_s):(_i * e_s) + e_s], self.offset + _i*e_s)
602*5e3eaea3SApple OSS Distributions                    self.obj_collection.append(_o)
603*5e3eaea3SApple OSS Distributions
604*5e3eaea3SApple OSS Distributions        elif self.i_type == GetTypeForName('KCDATA_TYPE_NESTED_KCDATA'):
605*5e3eaea3SApple OSS Distributions            logging.info("0x%08x: %sNESTED_KCDATA" % (self.offset, INDENT()))
606*5e3eaea3SApple OSS Distributions            with INDENT.indent():
607*5e3eaea3SApple OSS Distributions                nested_iterator = kcdata_item_iterator(self.i_data[:self.i_size])
608*5e3eaea3SApple OSS Distributions                nested_buffer = KCObject.FromKCItem(six.next(nested_iterator))
609*5e3eaea3SApple OSS Distributions                if not isinstance(nested_buffer, KCBufferObject):
610*5e3eaea3SApple OSS Distributions                    raise Exception("nested buffer isn't a KCBufferObject")
611*5e3eaea3SApple OSS Distributions                nested_buffer.ReadItems(nested_iterator)
612*5e3eaea3SApple OSS Distributions            self.nested_kcdata = nested_buffer
613*5e3eaea3SApple OSS Distributions
614*5e3eaea3SApple OSS Distributions        elif self.i_type in KNOWN_TYPES_COLLECTION:
615*5e3eaea3SApple OSS Distributions            self.i_name = KNOWN_TYPES_COLLECTION[self.i_type].GetName()
616*5e3eaea3SApple OSS Distributions            self.is_naked_type = True
617*5e3eaea3SApple OSS Distributions            logging.info("0x%08x: %s%s" % (self.offset, INDENT(), self.i_name))
618*5e3eaea3SApple OSS Distributions        else:
619*5e3eaea3SApple OSS Distributions            self.is_naked_type = True
620*5e3eaea3SApple OSS Distributions            #self.obj = "data of len %d" % len(self.i_data)
621*5e3eaea3SApple OSS Distributions            #self.obj = ''.join(["%x" % ki for ki in struct.unpack('%dB' % len(self.i_data), self.i_data)])
622*5e3eaea3SApple OSS Distributions            if isinstance(self.i_data, six.string_types):
623*5e3eaea3SApple OSS Distributions                self.obj = list(map(ord, BytesToString(self.i_data)))
624*5e3eaea3SApple OSS Distributions            else:
625*5e3eaea3SApple OSS Distributions                self.obj = [i for i in self.i_data]
626*5e3eaea3SApple OSS Distributions            logging.info("0x%08x: %s%s" % (self.offset, INDENT(), self.i_name))
627*5e3eaea3SApple OSS Distributions
628*5e3eaea3SApple OSS Distributions
629*5e3eaea3SApple OSS Distributionsclass KCContainerObject(KCObject):
630*5e3eaea3SApple OSS Distributions    def __init__(self, *args, **kwargs):
631*5e3eaea3SApple OSS Distributions        assert False
632*5e3eaea3SApple OSS Distributions
633*5e3eaea3SApple OSS Distributions    def InitAfterParse(self):
634*5e3eaea3SApple OSS Distributions        self.obj_container_dict = {}
635*5e3eaea3SApple OSS Distributions        self.obj_nested_objs = {}
636*5e3eaea3SApple OSS Distributions
637*5e3eaea3SApple OSS Distributions    def ShouldMerge(self):
638*5e3eaea3SApple OSS Distributions        return True
639*5e3eaea3SApple OSS Distributions
640*5e3eaea3SApple OSS Distributions    def GetJsonRepr(self):
641*5e3eaea3SApple OSS Distributions        # o = '"%s"' % self.obj['uniqID'] + ' : { "typeID" : %d ,' % self.obj['typeID']
642*5e3eaea3SApple OSS Distributions        o = '"%s"' % self.obj['uniqID'] + ' : { '
643*5e3eaea3SApple OSS Distributions        for (k, v) in self.obj_container_dict.items():
644*5e3eaea3SApple OSS Distributions            if v.ShouldMerge():
645*5e3eaea3SApple OSS Distributions                o += v.GetJsonRepr() + ","
646*5e3eaea3SApple OSS Distributions            else:
647*5e3eaea3SApple OSS Distributions                o += ' "%s" : ' % k + v.GetJsonRepr() + ","
648*5e3eaea3SApple OSS Distributions
649*5e3eaea3SApple OSS Distributions        for (k, v) in self.obj_nested_objs.items():
650*5e3eaea3SApple OSS Distributions            o += '"%s" : {' % k + ",".join([vi.GetJsonRepr() for vi in v.values()]) + "} ,"
651*5e3eaea3SApple OSS Distributions
652*5e3eaea3SApple OSS Distributions        o = o.rstrip(',') + "}"
653*5e3eaea3SApple OSS Distributions
654*5e3eaea3SApple OSS Distributions        return o
655*5e3eaea3SApple OSS Distributions
656*5e3eaea3SApple OSS Distributions    def AddObject(self, kco):
657*5e3eaea3SApple OSS Distributions        assert not kco.IsContainerEnd()
658*5e3eaea3SApple OSS Distributions        if isinstance(kco, KCContainerObject):
659*5e3eaea3SApple OSS Distributions            type_name = GetTypeNameForKey(kco.obj['typeID'])
660*5e3eaea3SApple OSS Distributions            if type_name not in self.obj_nested_objs:
661*5e3eaea3SApple OSS Distributions                self.obj_nested_objs[type_name] = {}
662*5e3eaea3SApple OSS Distributions            self.obj_nested_objs[type_name][kco.i_name] = kco
663*5e3eaea3SApple OSS Distributions            return
664*5e3eaea3SApple OSS Distributions        if kco.i_name in self.obj_container_dict:
665*5e3eaea3SApple OSS Distributions            if kco.IsArray() and self.obj_container_dict[kco.i_name].IsArray():
666*5e3eaea3SApple OSS Distributions                self.obj_container_dict[kco.i_name].obj_collection.extend( kco.obj_collection )
667*5e3eaea3SApple OSS Distributions        else:
668*5e3eaea3SApple OSS Distributions            self.obj_container_dict[kco.i_name] = kco
669*5e3eaea3SApple OSS Distributions
670*5e3eaea3SApple OSS Distributions    def IsEndMarker(self, o):
671*5e3eaea3SApple OSS Distributions        if not o.IsContainerEnd():
672*5e3eaea3SApple OSS Distributions            return False
673*5e3eaea3SApple OSS Distributions        if o.i_flags != self.i_flags:
674*5e3eaea3SApple OSS Distributions            raise Exception("container end marker doesn't match")
675*5e3eaea3SApple OSS Distributions        return True
676*5e3eaea3SApple OSS Distributions
677*5e3eaea3SApple OSS Distributions    no_end_message = "could not find container end marker"
678*5e3eaea3SApple OSS Distributions
679*5e3eaea3SApple OSS Distributions    def ReadItems(self, iterator):
680*5e3eaea3SApple OSS Distributions        found_end = False
681*5e3eaea3SApple OSS Distributions        with INDENT.indent():
682*5e3eaea3SApple OSS Distributions            for i in iterator:
683*5e3eaea3SApple OSS Distributions                o = KCObject.FromKCItem(i)
684*5e3eaea3SApple OSS Distributions                if self.IsEndMarker(o):
685*5e3eaea3SApple OSS Distributions                    found_end = True
686*5e3eaea3SApple OSS Distributions                    break
687*5e3eaea3SApple OSS Distributions                if o.IsBufferEnd():
688*5e3eaea3SApple OSS Distributions                    break
689*5e3eaea3SApple OSS Distributions                if isinstance(o, KCContainerObject):
690*5e3eaea3SApple OSS Distributions                    o.ReadItems(iterator)
691*5e3eaea3SApple OSS Distributions                self.AddObject(o)
692*5e3eaea3SApple OSS Distributions        if not found_end:
693*5e3eaea3SApple OSS Distributions            if G.accept_incomplete_data:
694*5e3eaea3SApple OSS Distributions                if not G.data_was_incomplete:
695*5e3eaea3SApple OSS Distributions                    print("kcdata.py WARNING: data is incomplete!", file=sys.stderr)
696*5e3eaea3SApple OSS Distributions                    G.data_was_incomplete = True
697*5e3eaea3SApple OSS Distributions            else:
698*5e3eaea3SApple OSS Distributions                raise Exception(self.no_end_message)
699*5e3eaea3SApple OSS Distributions
700*5e3eaea3SApple OSS Distributions
701*5e3eaea3SApple OSS Distributions
702*5e3eaea3SApple OSS Distributionsclass KCBufferObject(KCContainerObject):
703*5e3eaea3SApple OSS Distributions
704*5e3eaea3SApple OSS Distributions    def IsEndMarker(self,o):
705*5e3eaea3SApple OSS Distributions        if o.IsContainerEnd():
706*5e3eaea3SApple OSS Distributions            raise Exception("container end marker at the toplevel")
707*5e3eaea3SApple OSS Distributions        return o.IsBufferEnd()
708*5e3eaea3SApple OSS Distributions
709*5e3eaea3SApple OSS Distributions    no_end_message = "could not find buffer end marker"
710*5e3eaea3SApple OSS Distributions
711*5e3eaea3SApple OSS Distributionsclass KCCompressedBufferObject(KCContainerObject):
712*5e3eaea3SApple OSS Distributions
713*5e3eaea3SApple OSS Distributions    def ReadItems(self, iterator):
714*5e3eaea3SApple OSS Distributions        self.header = dict()
715*5e3eaea3SApple OSS Distributions        with INDENT.indent():
716*5e3eaea3SApple OSS Distributions            for i in iterator:
717*5e3eaea3SApple OSS Distributions                o = KCObject.FromKCItem(i)
718*5e3eaea3SApple OSS Distributions                if self.IsEndMarker(o):
719*5e3eaea3SApple OSS Distributions                    self.compressed_type = o.i_type
720*5e3eaea3SApple OSS Distributions                    self.blob_start = o.offset + 16
721*5e3eaea3SApple OSS Distributions                    break
722*5e3eaea3SApple OSS Distributions                o.ParseData()
723*5e3eaea3SApple OSS Distributions                self.header[o.i_name] = o.obj
724*5e3eaea3SApple OSS Distributions
725*5e3eaea3SApple OSS Distributions    def IsEndMarker(self, o):
726*5e3eaea3SApple OSS Distributions        return o.i_type in KNOWN_TOPLEVEL_CONTAINER_TYPES
727*5e3eaea3SApple OSS Distributions
728*5e3eaea3SApple OSS Distributions    def GetCompressedBlob(self, data):
729*5e3eaea3SApple OSS Distributions        if self.header['kcd_c_type'] != 1:
730*5e3eaea3SApple OSS Distributions            raise NotImplementedError
731*5e3eaea3SApple OSS Distributions        blob = data[self.blob_start:self.blob_start+self.header['kcd_c_totalout']]
732*5e3eaea3SApple OSS Distributions        if len(blob) != self.header['kcd_c_totalout']:
733*5e3eaea3SApple OSS Distributions            raise ValueError
734*5e3eaea3SApple OSS Distributions        return blob
735*5e3eaea3SApple OSS Distributions
736*5e3eaea3SApple OSS Distributions    def Decompress(self, data):
737*5e3eaea3SApple OSS Distributions        start_marker = struct.pack('<IIII', self.compressed_type, 0, 0, 0)
738*5e3eaea3SApple OSS Distributions        end_marker = struct.pack('<IIII', GetTypeForName('KCDATA_TYPE_BUFFER_END'), 0, 0, 0)
739*5e3eaea3SApple OSS Distributions        decompressed = zlib.decompress(self.GetCompressedBlob(data))
740*5e3eaea3SApple OSS Distributions        if len(decompressed) != self.header['kcd_c_totalin']:
741*5e3eaea3SApple OSS Distributions            raise ValueError("length of decompressed: %d vs expected %d" % (len(decompressed), self.header['kcd_c_totalin']))
742*5e3eaea3SApple OSS Distributions        alignbytes = b'\x00' * (-len(decompressed) % 16)
743*5e3eaea3SApple OSS Distributions        return start_marker + decompressed + alignbytes + end_marker
744*5e3eaea3SApple OSS Distributions
745*5e3eaea3SApple OSS Distributions
746*5e3eaea3SApple OSS Distributionsclass KCData_item:
747*5e3eaea3SApple OSS Distributions    """ a basic kcdata_item type object.
748*5e3eaea3SApple OSS Distributions    """
749*5e3eaea3SApple OSS Distributions    header_size = 16  # (uint32_t + uint32_t + uint64_t)
750*5e3eaea3SApple OSS Distributions
751*5e3eaea3SApple OSS Distributions    def __init__(self, item_type, item_size, item_flags, item_data):
752*5e3eaea3SApple OSS Distributions        self.i_type = item_type
753*5e3eaea3SApple OSS Distributions        self.i_size = item_size
754*5e3eaea3SApple OSS Distributions        self.i_flags = item_flags
755*5e3eaea3SApple OSS Distributions        self.i_data = item_data
756*5e3eaea3SApple OSS Distributions        self.i_offset = None
757*5e3eaea3SApple OSS Distributions
758*5e3eaea3SApple OSS Distributions    def __init__(self, barray, pos=0):
759*5e3eaea3SApple OSS Distributions        """ create an object by parsing data from bytes array
760*5e3eaea3SApple OSS Distributions            returns : obj - if data is readable
761*5e3eaea3SApple OSS Distributions                      raises ValueError if something is not ok.
762*5e3eaea3SApple OSS Distributions        """
763*5e3eaea3SApple OSS Distributions        self.i_type = struct.unpack('I', barray[pos:pos+4])[0]     # int.from_bytes(barray[pos:pos+4])
764*5e3eaea3SApple OSS Distributions        self.i_size = struct.unpack('I', barray[pos+4:pos+8])[0]   # int.from_bytes(barray[pos+4:pos+8])
765*5e3eaea3SApple OSS Distributions        self.i_flags = struct.unpack('Q', barray[pos+8:pos+16])[0]  # int.from_bytes(barray[pos+8:pos+16])
766*5e3eaea3SApple OSS Distributions        self.i_data = barray[pos+16: (pos + 16 + self.i_size)]
767*5e3eaea3SApple OSS Distributions        self.i_offset = pos
768*5e3eaea3SApple OSS Distributions
769*5e3eaea3SApple OSS Distributions    def __len__(self):
770*5e3eaea3SApple OSS Distributions        return self.i_size + KCData_item.header_size
771*5e3eaea3SApple OSS Distributions
772*5e3eaea3SApple OSS Distributions    def GetHeaderDescription(self):
773*5e3eaea3SApple OSS Distributions        outs = "type: 0x%x size: 0x%x flags: 0x%x  (%s)" % (self.i_type, self.i_size, self.i_flags, GetTypeNameForKey(self.i_type))
774*5e3eaea3SApple OSS Distributions        if not self.i_offset is None:
775*5e3eaea3SApple OSS Distributions            outs = "pos: 0x%x" % self.i_offset + outs
776*5e3eaea3SApple OSS Distributions        return outs
777*5e3eaea3SApple OSS Distributions
778*5e3eaea3SApple OSS Distributions    def __str__(self):
779*5e3eaea3SApple OSS Distributions        return self.GetHeaderDescription()
780*5e3eaea3SApple OSS Distributions
781*5e3eaea3SApple OSS Distributionsdef kcdata_item_iterator(data):
782*5e3eaea3SApple OSS Distributions    file_len = len(data)
783*5e3eaea3SApple OSS Distributions    curpos = 0
784*5e3eaea3SApple OSS Distributions    while curpos < file_len:
785*5e3eaea3SApple OSS Distributions        item = KCData_item(data, curpos)
786*5e3eaea3SApple OSS Distributions        yield item
787*5e3eaea3SApple OSS Distributions        curpos += len(item)
788*5e3eaea3SApple OSS Distributions
789*5e3eaea3SApple OSS Distributionsdef _get_data_element(elementValues):
790*5e3eaea3SApple OSS Distributions    return json.dumps(elementValues[-1])
791*5e3eaea3SApple OSS Distributions
792*5e3eaea3SApple OSS DistributionsKNOWN_TOPLEVEL_CONTAINER_TYPES = list(map(GetTypeForName, ('KCDATA_BUFFER_BEGIN_COMPRESSED', 'KCDATA_BUFFER_BEGIN_CRASHINFO', 'KCDATA_BUFFER_BEGIN_STACKSHOT', 'KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT', 'KCDATA_BUFFER_BEGIN_OS_REASON','KCDATA_BUFFER_BEGIN_XNUPOST_CONFIG')))
793*5e3eaea3SApple OSS Distributions
794*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('KCDATA_TYPE_UINT32_DESC')] = KCTypeDescription(GetTypeForName('KCDATA_TYPE_UINT32_DESC'), (
795*5e3eaea3SApple OSS Distributions    KCSubTypeElement('desc', KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(32, 1), 0, 1),
796*5e3eaea3SApple OSS Distributions    KCSubTypeElement('data', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 32, 0)
797*5e3eaea3SApple OSS Distributions),
798*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_UINT32_DESC',
799*5e3eaea3SApple OSS Distributions    _get_data_element
800*5e3eaea3SApple OSS Distributions)
801*5e3eaea3SApple OSS Distributions
802*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('KCDATA_TYPE_UINT64_DESC')] = KCTypeDescription(GetTypeForName('KCDATA_TYPE_UINT64_DESC'), (
803*5e3eaea3SApple OSS Distributions    KCSubTypeElement('desc', KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(32, 1), 0, 1),
804*5e3eaea3SApple OSS Distributions    KCSubTypeElement('data', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 32, 0)
805*5e3eaea3SApple OSS Distributions),
806*5e3eaea3SApple OSS Distributions    'KCDATA_TYPE_UINT64_DESC',
807*5e3eaea3SApple OSS Distributions    _get_data_element
808*5e3eaea3SApple OSS Distributions)
809*5e3eaea3SApple OSS Distributions
810*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('KCDATA_TYPE_TIMEBASE')] = KCTypeDescription(GetTypeForName('KCDATA_TYPE_TIMEBASE'), (
811*5e3eaea3SApple OSS Distributions    KCSubTypeElement('numer', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 0, 0),
812*5e3eaea3SApple OSS Distributions    KCSubTypeElement('denom', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4, 0)
813*5e3eaea3SApple OSS Distributions),
814*5e3eaea3SApple OSS Distributions    'mach_timebase_info'
815*5e3eaea3SApple OSS Distributions)
816*5e3eaea3SApple OSS Distributions
817*5e3eaea3SApple OSS Distributions
818*5e3eaea3SApple OSS DistributionsSTACKSHOT_IO_NUM_PRIORITIES = 4
819*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[0x901] = KCTypeDescription(0x901, (
820*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ss_disk_reads_count', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
821*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ss_disk_reads_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
822*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ss_disk_writes_count', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
823*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ss_disk_writes_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
824*5e3eaea3SApple OSS Distributions    KCSubTypeElement('ss_io_priority_count', KCSUBTYPE_TYPE.KC_ST_UINT64, KCSubTypeElement.GetSizeForArray(STACKSHOT_IO_NUM_PRIORITIES, 8), 32, 1),
825*5e3eaea3SApple OSS Distributions    KCSubTypeElement('ss_io_priority_size', KCSUBTYPE_TYPE.KC_ST_UINT64, KCSubTypeElement.GetSizeForArray(STACKSHOT_IO_NUM_PRIORITIES, 8), 32 + (STACKSHOT_IO_NUM_PRIORITIES * 8), 1),
826*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ss_paging_count', KCSUBTYPE_TYPE.KC_ST_UINT64, 32 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8)),
827*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ss_paging_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 40 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8)),
828*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ss_non_paging_count', KCSUBTYPE_TYPE.KC_ST_UINT64, 48 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8)),
829*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ss_non_paging_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 56 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8)),
830*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ss_data_count', KCSUBTYPE_TYPE.KC_ST_UINT64, 64 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8)),
831*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ss_data_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 72 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8)),
832*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ss_metadata_count', KCSUBTYPE_TYPE.KC_ST_UINT64, 80 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8)),
833*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ss_metadata_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 88 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8))
834*5e3eaea3SApple OSS Distributions),
835*5e3eaea3SApple OSS Distributions    'io_statistics'
836*5e3eaea3SApple OSS Distributions)
837*5e3eaea3SApple OSS Distributions
838*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[0x902] = KCTypeDescription(0x902, (
839*5e3eaea3SApple OSS Distributions    KCSubTypeElement('snapshot_magic', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 0, 0),
840*5e3eaea3SApple OSS Distributions    KCSubTypeElement('free_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 1, 0),
841*5e3eaea3SApple OSS Distributions    KCSubTypeElement('active_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 2, 0),
842*5e3eaea3SApple OSS Distributions    KCSubTypeElement('inactive_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 3, 0),
843*5e3eaea3SApple OSS Distributions    KCSubTypeElement('purgeable_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 4, 0),
844*5e3eaea3SApple OSS Distributions    KCSubTypeElement('wired_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 5, 0),
845*5e3eaea3SApple OSS Distributions    KCSubTypeElement('speculative_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 6, 0),
846*5e3eaea3SApple OSS Distributions    KCSubTypeElement('throttled_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 7, 0),
847*5e3eaea3SApple OSS Distributions    KCSubTypeElement('filebacked_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 8, 0),
848*5e3eaea3SApple OSS Distributions    KCSubTypeElement('compressions', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 9, 0),
849*5e3eaea3SApple OSS Distributions    KCSubTypeElement('decompressions', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 10, 0),
850*5e3eaea3SApple OSS Distributions    KCSubTypeElement('compressor_size', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 11, 0),
851*5e3eaea3SApple OSS Distributions    KCSubTypeElement('busy_buffer_count', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 4 * 12, 0),
852*5e3eaea3SApple OSS Distributions    KCSubTypeElement('pages_wanted', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 13, 0),
853*5e3eaea3SApple OSS Distributions    KCSubTypeElement('pages_reclaimed', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 14, 0),
854*5e3eaea3SApple OSS Distributions    KCSubTypeElement('pages_wanted_reclaimed_valid', KCSUBTYPE_TYPE.KC_ST_UINT8, 1, 4 * 15, 0)
855*5e3eaea3SApple OSS Distributions),
856*5e3eaea3SApple OSS Distributions    'mem_and_io_snapshot'
857*5e3eaea3SApple OSS Distributions)
858*5e3eaea3SApple OSS Distributions
859*5e3eaea3SApple OSS Distributions
860*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[0x930] = KCTypeDescription(0x930, (
861*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tts_unique_pid', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
862*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tts_ss_flags', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
863*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tts_transition_type', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
864*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tts_pid', KCSUBTYPE_TYPE.KC_ST_INT32, 24),
865*5e3eaea3SApple OSS Distributions    KCSubTypeElement('tts_p_comm', KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(32, 1), 28, 1)
866*5e3eaea3SApple OSS Distributions),
867*5e3eaea3SApple OSS Distributions    'transitioning_task_snapshot'
868*5e3eaea3SApple OSS Distributions)
869*5e3eaea3SApple OSS Distributions
870*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[0x905] = KCTypeDescription(0x905, (
871*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ts_unique_pid', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
872*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ts_ss_flags', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
873*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ts_user_time_in_terminated_thre', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
874*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ts_system_time_in_terminated_th', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
875*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ts_p_start_sec', KCSUBTYPE_TYPE.KC_ST_UINT64, 32),
876*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ts_task_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 40),
877*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ts_max_resident_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 48),
878*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ts_suspend_count', KCSUBTYPE_TYPE.KC_ST_UINT32, 56),
879*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ts_faults', KCSUBTYPE_TYPE.KC_ST_UINT32, 60),
880*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ts_pageins', KCSUBTYPE_TYPE.KC_ST_UINT32, 64),
881*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ts_cow_faults', KCSUBTYPE_TYPE.KC_ST_UINT32, 68),
882*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ts_was_throttled', KCSUBTYPE_TYPE.KC_ST_UINT32, 72),
883*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ts_did_throttle', KCSUBTYPE_TYPE.KC_ST_UINT32, 76),
884*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ts_latency_qos', KCSUBTYPE_TYPE.KC_ST_UINT32, 80),
885*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ts_pid', KCSUBTYPE_TYPE.KC_ST_INT32, 84),
886*5e3eaea3SApple OSS Distributions    KCSubTypeElement('ts_p_comm', KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(32, 1), 88, 1)
887*5e3eaea3SApple OSS Distributions),
888*5e3eaea3SApple OSS Distributions    'task_snapshot'
889*5e3eaea3SApple OSS Distributions)
890*5e3eaea3SApple OSS Distributions
891*5e3eaea3SApple OSS Distributions
892*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[0x946] = KCTypeDescription(0x946, (
893*5e3eaea3SApple OSS Distributions     KCSubTypeElement.FromBasicCtype('csflags', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
894*5e3eaea3SApple OSS Distributions     KCSubTypeElement.FromBasicCtype('cs_trust_level', KCSUBTYPE_TYPE.KC_ST_UINT32, 8),
895*5e3eaea3SApple OSS Distributions     ),
896*5e3eaea3SApple OSS Distributions     'stackshot_task_codesigning_info'
897*5e3eaea3SApple OSS Distributions)
898*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[0x906] = KCTypeDescription(0x906, (
899*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ths_thread_id', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
900*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ths_wait_event', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
901*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ths_continuation', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
902*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ths_total_syscalls', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
903*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ths_voucher_identifier', KCSUBTYPE_TYPE.KC_ST_UINT64, 32),
904*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ths_dqserialnum', KCSUBTYPE_TYPE.KC_ST_UINT64, 40),
905*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ths_user_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 48),
906*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ths_sys_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 56),
907*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ths_ss_flags', KCSUBTYPE_TYPE.KC_ST_UINT64, 64),
908*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ths_last_run_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 72),
909*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ths_last_made_runnable_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 80),
910*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ths_state', KCSUBTYPE_TYPE.KC_ST_UINT32, 88),
911*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ths_sched_flags', KCSUBTYPE_TYPE.KC_ST_UINT32, 92),
912*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ths_base_priority', KCSUBTYPE_TYPE.KC_ST_INT16, 96),
913*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ths_sched_priority', KCSUBTYPE_TYPE.KC_ST_INT16, 98),
914*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ths_eqos', KCSUBTYPE_TYPE.KC_ST_UINT8, 100),
915*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ths_rqos', KCSUBTYPE_TYPE.KC_ST_UINT8, 101),
916*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ths_rqos_override', KCSUBTYPE_TYPE.KC_ST_UINT8, 102),
917*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ths_io_tier', KCSUBTYPE_TYPE.KC_ST_UINT8, 103),
918*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ths_thread_t', KCSUBTYPE_TYPE.KC_ST_UINT64, 104),
919*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ths_requested_policy', KCSUBTYPE_TYPE.KC_ST_UINT64, 112),
920*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('ths_effective_policy', KCSUBTYPE_TYPE.KC_ST_UINT64, 120),
921*5e3eaea3SApple OSS Distributions),
922*5e3eaea3SApple OSS Distributions    'thread_snapshot',
923*5e3eaea3SApple OSS Distributions    legacy_size = 0x68
924*5e3eaea3SApple OSS Distributions)
925*5e3eaea3SApple OSS Distributions
926*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_THREAD_DISPATCH_QUEUE_LABEL')] = KCSubTypeElement('dispatch_queue_label', KCSUBTYPE_TYPE.KC_ST_CHAR,
927*5e3eaea3SApple OSS Distributions                          KCSubTypeElement.GetSizeForArray(64, 1), 0, 1)
928*5e3eaea3SApple OSS Distributions
929*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_THREAD_DELTA_SNAPSHOT')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_THREAD_DELTA_SNAPSHOT'), (
930*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tds_thread_id', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
931*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tds_voucher_identifier', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
932*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tds_ss_flags', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
933*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tds_last_made_runnable_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
934*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tds_state', KCSUBTYPE_TYPE.KC_ST_UINT32, 32),
935*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tds_sched_flags', KCSUBTYPE_TYPE.KC_ST_UINT32, 36),
936*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tds_base_priority', KCSUBTYPE_TYPE.KC_ST_INT16, 40),
937*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tds_sched_priority', KCSUBTYPE_TYPE.KC_ST_INT16, 42),
938*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tds_eqos', KCSUBTYPE_TYPE.KC_ST_UINT8, 44),
939*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tds_rqos', KCSUBTYPE_TYPE.KC_ST_UINT8, 45),
940*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tds_rqos_override', KCSUBTYPE_TYPE.KC_ST_UINT8, 46),
941*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tds_io_tier', KCSUBTYPE_TYPE.KC_ST_UINT8, 47),
942*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tds_requested_policy', KCSUBTYPE_TYPE.KC_ST_UINT64, 48),
943*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tds_effective_policy', KCSUBTYPE_TYPE.KC_ST_UINT64, 56),
944*5e3eaea3SApple OSS Distributions),
945*5e3eaea3SApple OSS Distributions    'thread_delta_snapshot',
946*5e3eaea3SApple OSS Distributions    legacy_size = 48
947*5e3eaea3SApple OSS Distributions)
948*5e3eaea3SApple OSS Distributions
949*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_TASK_DELTA_SNAPSHOT')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_TASK_DELTA_SNAPSHOT'), (
950*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tds_unique_pid', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
951*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tds_ss_flags', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
952*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tds_user_time_in_terminated_thr', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
953*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tds_system_time_in_terminated_t', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
954*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tds_task_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 32),
955*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tds_max_resident_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 40),
956*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tds_suspend_count', KCSUBTYPE_TYPE.KC_ST_UINT32, 48),
957*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tds_faults', KCSUBTYPE_TYPE.KC_ST_UINT32, 52),
958*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tds_pageins', KCSUBTYPE_TYPE.KC_ST_UINT32, 56),
959*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tds_cow_faults', KCSUBTYPE_TYPE.KC_ST_UINT32, 60),
960*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tds_was_throttled', KCSUBTYPE_TYPE.KC_ST_UINT32, 64),
961*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tds_did_throttle', KCSUBTYPE_TYPE.KC_ST_UINT32, 68),
962*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tds_latency_qos', KCSUBTYPE_TYPE.KC_ST_UINT32, 72),
963*5e3eaea3SApple OSS Distributions),
964*5e3eaea3SApple OSS Distributions    'task_delta_snapshot'
965*5e3eaea3SApple OSS Distributions)
966*5e3eaea3SApple OSS Distributions
967*5e3eaea3SApple OSS Distributions
968*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_THREAD_NAME')] = KCSubTypeElement('pth_name', KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(64, 1), 0, 1)
969*5e3eaea3SApple OSS Distributions
970*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_SYS_SHAREDCACHE_LAYOUT')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_SYS_SHAREDCACHE_LAYOUT'), (
971*5e3eaea3SApple OSS Distributions    KCSubTypeElement('imageLoadAddress', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0),
972*5e3eaea3SApple OSS Distributions    KCSubTypeElement('imageUUID', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 8, 1)
973*5e3eaea3SApple OSS Distributions),
974*5e3eaea3SApple OSS Distributions    'system_shared_cache_layout'
975*5e3eaea3SApple OSS Distributions)
976*5e3eaea3SApple OSS Distributions
977*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('KCDATA_TYPE_LIBRARY_LOADINFO64')] = KCTypeDescription(GetTypeForName('KCDATA_TYPE_LIBRARY_LOADINFO64'), (
978*5e3eaea3SApple OSS Distributions    KCSubTypeElement('imageLoadAddress', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0),
979*5e3eaea3SApple OSS Distributions    KCSubTypeElement('imageUUID', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 8, 1)
980*5e3eaea3SApple OSS Distributions),
981*5e3eaea3SApple OSS Distributions    'dyld_load_info',
982*5e3eaea3SApple OSS Distributions    legacy_size = 24
983*5e3eaea3SApple OSS Distributions)
984*5e3eaea3SApple OSS Distributions
985*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('KCDATA_TYPE_LIBRARY_LOADINFO')] = KCTypeDescription(GetTypeForName('KCDATA_TYPE_LIBRARY_LOADINFO'), (
986*5e3eaea3SApple OSS Distributions    KCSubTypeElement('imageLoadAddress', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 0, 0),
987*5e3eaea3SApple OSS Distributions    KCSubTypeElement('imageUUID', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 4, 1)
988*5e3eaea3SApple OSS Distributions),
989*5e3eaea3SApple OSS Distributions    'dyld_load_info',
990*5e3eaea3SApple OSS Distributions    legacy_size = 20
991*5e3eaea3SApple OSS Distributions)
992*5e3eaea3SApple OSS Distributions
993*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_LOADINFO64_TEXT_EXEC')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_LOADINFO64_TEXT_EXEC'), (
994*5e3eaea3SApple OSS Distributions    KCSubTypeElement('imageLoadAddress', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0),
995*5e3eaea3SApple OSS Distributions    KCSubTypeElement('imageUUID', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 8, 1),
996*5e3eaea3SApple OSS Distributions),
997*5e3eaea3SApple OSS Distributions    'dyld_load_info_text_exec'
998*5e3eaea3SApple OSS Distributions)
999*5e3eaea3SApple OSS Distributions
1000*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_AOTCACHE_LOADINFO')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_AOTCACHE_LOADINFO'), (
1001*5e3eaea3SApple OSS Distributions    KCSubTypeElement('x86SlidBaseAddress', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0),
1002*5e3eaea3SApple OSS Distributions    KCSubTypeElement('x86UUID', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 8, 1),
1003*5e3eaea3SApple OSS Distributions    KCSubTypeElement('aotSlidBaseAddress', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 24, 0),
1004*5e3eaea3SApple OSS Distributions    KCSubTypeElement('aotUUID', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 32, 1),
1005*5e3eaea3SApple OSS Distributions),
1006*5e3eaea3SApple OSS Distributions    'dyld_aot_cache_uuid_info'
1007*5e3eaea3SApple OSS Distributions)
1008*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_SHAREDCACHE_AOTINFO')] = KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_AOTCACHE_LOADINFO')]
1009*5e3eaea3SApple OSS Distributions
1010*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_SHAREDCACHE_LOADINFO')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_SHAREDCACHE_LOADINFO'), (
1011*5e3eaea3SApple OSS Distributions    KCSubTypeElement('imageLoadAddress', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0),
1012*5e3eaea3SApple OSS Distributions    KCSubTypeElement('imageUUID', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 8, 1),
1013*5e3eaea3SApple OSS Distributions    KCSubTypeElement('imageSlidBaseAddress', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 24, 0),
1014*5e3eaea3SApple OSS Distributions    KCSubTypeElement('sharedCacheSlidFirstMapping', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 32, 0),
1015*5e3eaea3SApple OSS Distributions),
1016*5e3eaea3SApple OSS Distributions    'shared_cache_dyld_load_info',
1017*5e3eaea3SApple OSS Distributions    legacy_size = 0x18
1018*5e3eaea3SApple OSS Distributions)
1019*5e3eaea3SApple OSS Distributions
1020*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_SHAREDCACHE_INFO')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_SHAREDCACHE_INFO'), (
1021*5e3eaea3SApple OSS Distributions    KCSubTypeElement('sharedCacheSlide', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0),
1022*5e3eaea3SApple OSS Distributions    KCSubTypeElement('sharedCacheUUID', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 8, 1),
1023*5e3eaea3SApple OSS Distributions    KCSubTypeElement('sharedCacheUnreliableSlidBaseAd', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 24, 0),
1024*5e3eaea3SApple OSS Distributions    KCSubTypeElement('sharedCacheSlidFirstMapping', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 32, 0),
1025*5e3eaea3SApple OSS Distributions    KCSubTypeElement('sharedCacheID', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 40, 0),
1026*5e3eaea3SApple OSS Distributions    KCSubTypeElement('sharedCacheFlags', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 44, 0),
1027*5e3eaea3SApple OSS Distributions),
1028*5e3eaea3SApple OSS Distributions    'shared_cache_dyld_load_info',
1029*5e3eaea3SApple OSS Distributions)
1030*5e3eaea3SApple OSS Distributions
1031*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERNELCACHE_LOADINFO')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_KERNELCACHE_LOADINFO'), (
1032*5e3eaea3SApple OSS Distributions    KCSubTypeElement('imageLoadAddress', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0),
1033*5e3eaea3SApple OSS Distributions    KCSubTypeElement('imageUUID', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 8, 1),
1034*5e3eaea3SApple OSS Distributions),
1035*5e3eaea3SApple OSS Distributions    'kernelcache_load_info'
1036*5e3eaea3SApple OSS Distributions)
1037*5e3eaea3SApple OSS Distributions
1038*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_SHAREDCACHE_ID')] = KCSubTypeElement('sharedCacheID', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 0, 0, KCSubTypeElement._get_naked_element_value)
1039*5e3eaea3SApple OSS Distributions
1040*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[0x33] = KCSubTypeElement('mach_absolute_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0, KCSubTypeElement._get_naked_element_value)
1041*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[0x907] = KCSubTypeElement.FromBasicCtype('donating_pids', KCSUBTYPE_TYPE.KC_ST_INT32, legacy_size=4)
1042*5e3eaea3SApple OSS Distributions
1043*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('KCDATA_TYPE_USECS_SINCE_EPOCH')] = KCSubTypeElement('usecs_since_epoch', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0, KCSubTypeElement._get_naked_element_value)
1044*5e3eaea3SApple OSS Distributions
1045*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKFRAME')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKFRAME'), (
1046*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('lr', KCSUBTYPE_TYPE.KC_ST_UINT32),
1047*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('sp', KCSUBTYPE_TYPE.KC_ST_UINT32, 4)
1048*5e3eaea3SApple OSS Distributions),
1049*5e3eaea3SApple OSS Distributions    'kernel_stack_frames',
1050*5e3eaea3SApple OSS Distributions    legacy_size = 8
1051*5e3eaea3SApple OSS Distributions)
1052*5e3eaea3SApple OSS Distributions
1053*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKLR')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKLR'), (
1054*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('lr', KCSUBTYPE_TYPE.KC_ST_UINT32),
1055*5e3eaea3SApple OSS Distributions),
1056*5e3eaea3SApple OSS Distributions    'kernel_stack_frames'
1057*5e3eaea3SApple OSS Distributions)
1058*5e3eaea3SApple OSS Distributions
1059*5e3eaea3SApple OSS Distributions
1060*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_USER_STACKFRAME')] = KCTypeDescription.FromKCTypeDescription(
1061*5e3eaea3SApple OSS Distributions    KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKFRAME')],
1062*5e3eaea3SApple OSS Distributions    GetTypeForName('STACKSHOT_KCTYPE_USER_STACKFRAME'),
1063*5e3eaea3SApple OSS Distributions    'user_stack_frames'
1064*5e3eaea3SApple OSS Distributions)
1065*5e3eaea3SApple OSS Distributions
1066*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_USER_STACKLR')] = KCTypeDescription.FromKCTypeDescription(
1067*5e3eaea3SApple OSS Distributions    KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKLR')],
1068*5e3eaea3SApple OSS Distributions    GetTypeForName('STACKSHOT_KCTYPE_USER_STACKLR'),
1069*5e3eaea3SApple OSS Distributions    'user_stack_frames'
1070*5e3eaea3SApple OSS Distributions)
1071*5e3eaea3SApple OSS Distributions
1072*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKFRAME64')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKFRAME64'), (
1073*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('lr', KCSUBTYPE_TYPE.KC_ST_UINT64),
1074*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('sp', KCSUBTYPE_TYPE.KC_ST_UINT64, 8)
1075*5e3eaea3SApple OSS Distributions),
1076*5e3eaea3SApple OSS Distributions    'kernel_stack_frames',
1077*5e3eaea3SApple OSS Distributions    legacy_size = 16
1078*5e3eaea3SApple OSS Distributions)
1079*5e3eaea3SApple OSS Distributions
1080*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_USER_STACKFRAME64')] = KCTypeDescription.FromKCTypeDescription(
1081*5e3eaea3SApple OSS Distributions    KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKFRAME64')],
1082*5e3eaea3SApple OSS Distributions    GetTypeForName('STACKSHOT_KCTYPE_USER_STACKFRAME64'),
1083*5e3eaea3SApple OSS Distributions    'user_stack_frames'
1084*5e3eaea3SApple OSS Distributions)
1085*5e3eaea3SApple OSS Distributions
1086*5e3eaea3SApple OSS Distributions
1087*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKLR64')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKLR64'), (
1088*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('lr', KCSUBTYPE_TYPE.KC_ST_UINT64),
1089*5e3eaea3SApple OSS Distributions),
1090*5e3eaea3SApple OSS Distributions    'kernel_stack_frames'
1091*5e3eaea3SApple OSS Distributions)
1092*5e3eaea3SApple OSS Distributions
1093*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_USER_STACKLR64')] = KCTypeDescription.FromKCTypeDescription(
1094*5e3eaea3SApple OSS Distributions    KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKLR64')],
1095*5e3eaea3SApple OSS Distributions    GetTypeForName('STACKSHOT_KCTYPE_USER_STACKLR64'),
1096*5e3eaea3SApple OSS Distributions    'user_stack_frames'
1097*5e3eaea3SApple OSS Distributions)
1098*5e3eaea3SApple OSS Distributions
1099*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_USER_ASYNC_START_INDEX')] = KCSubTypeElement.FromBasicCtype('user_async_start_index', KCSUBTYPE_TYPE.KC_ST_UINT32)
1100*5e3eaea3SApple OSS Distributions
1101*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_USER_ASYNC_STACKLR64')] = KCTypeDescription.FromKCTypeDescription(
1102*5e3eaea3SApple OSS Distributions    KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKLR64')],
1103*5e3eaea3SApple OSS Distributions    GetTypeForName('STACKSHOT_KCTYPE_USER_ASYNC_STACKLR64'),
1104*5e3eaea3SApple OSS Distributions    'user_async_stack_frames'
1105*5e3eaea3SApple OSS Distributions)
1106*5e3eaea3SApple OSS Distributions
1107*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_NONRUNNABLE_TIDS')] = KCSubTypeElement.FromBasicCtype('nonrunnable_threads', KCSUBTYPE_TYPE.KC_ST_INT64)
1108*5e3eaea3SApple OSS Distributions
1109*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_NONRUNNABLE_TASKS')] = KCSubTypeElement.FromBasicCtype('nonrunnable_tasks', KCSUBTYPE_TYPE.KC_ST_INT64)
1110*5e3eaea3SApple OSS Distributions
1111*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_OSVERSION')] = KCSubTypeElement('osversion', KCSUBTYPE_TYPE.KC_ST_CHAR,
1112*5e3eaea3SApple OSS Distributions                          KCSubTypeElement.GetSizeForArray(256, 1), 0, 1)
1113*5e3eaea3SApple OSS Distributions
1114*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_BOOTARGS')] = KCSubTypeElement('boot_args', KCSUBTYPE_TYPE.KC_ST_CHAR,
1115*5e3eaea3SApple OSS Distributions                           KCSubTypeElement.GetSizeForArray(256, 1), 0, 1)
1116*5e3eaea3SApple OSS Distributions
1117*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_PAGE_SIZE')] = KCSubTypeElement('kernel_page_size', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 0, 0, KCSubTypeElement._get_naked_element_value)
1118*5e3eaea3SApple OSS Distributions
1119*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_THREAD_POLICY_VERSION')] = KCSubTypeElement('thread_policy_version', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 0, 0, KCSubTypeElement._get_naked_element_value)
1120*5e3eaea3SApple OSS Distributions
1121*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_JETSAM_LEVEL')] = KCSubTypeElement('jetsam_level', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 0, 0, KCSubTypeElement._get_naked_element_value)
1122*5e3eaea3SApple OSS Distributions
1123*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_DELTA_SINCE_TIMESTAMP')] = KCSubTypeElement("stackshot_delta_since_timestamp", KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0, KCSubTypeElement._get_naked_element_value)
1124*5e3eaea3SApple OSS Distributions
1125*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_STACKSHOT_FAULT_STATS')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_STACKSHOT_FAULT_STATS'),
1126*5e3eaea3SApple OSS Distributions            (
1127*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('sfs_pages_faulted_in', KCSUBTYPE_TYPE.KC_ST_UINT32, 0),
1128*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('sfs_time_spent_faulting', KCSUBTYPE_TYPE.KC_ST_UINT64, 4),
1129*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('sfs_system_max_fault_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 12),
1130*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('sfs_stopped_faulting', KCSUBTYPE_TYPE.KC_ST_UINT8, 20)
1131*5e3eaea3SApple OSS Distributions            ),
1132*5e3eaea3SApple OSS Distributions            'stackshot_fault_stats')
1133*5e3eaea3SApple OSS Distributions
1134*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_THREAD_WAITINFO')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_THREAD_WAITINFO'),
1135*5e3eaea3SApple OSS Distributions            (
1136*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('owner', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1137*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('waiter', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1138*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('context', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
1139*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('wait_type', KCSUBTYPE_TYPE.KC_ST_UINT8, 24),
1140*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('portlabel_id', KCSUBTYPE_TYPE.KC_ST_INT16, 25),
1141*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('wait_flags', KCSUBTYPE_TYPE.KC_ST_INT32, 27)
1142*5e3eaea3SApple OSS Distributions            ),
1143*5e3eaea3SApple OSS Distributions            'thread_waitinfo')
1144*5e3eaea3SApple OSS Distributions
1145*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_THREAD_TURNSTILEINFO')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_THREAD_TURNSTILEINFO'),
1146*5e3eaea3SApple OSS Distributions            (
1147*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('waiter', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1148*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('turnstile_context', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1149*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('turnstile_priority', KCSUBTYPE_TYPE.KC_ST_UINT8, 16),
1150*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('number_of_hops', KCSUBTYPE_TYPE.KC_ST_UINT8, 17),
1151*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('turnstile_flags', KCSUBTYPE_TYPE.KC_ST_UINT64, 18),
1152*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('portlabel_id', KCSUBTYPE_TYPE.KC_ST_INT16, 26),
1153*5e3eaea3SApple OSS Distributions            ),
1154*5e3eaea3SApple OSS Distributions            'thread_turnstileinfo')
1155*5e3eaea3SApple OSS Distributions
1156*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_PORTLABEL')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_PORTLABEL'),
1157*5e3eaea3SApple OSS Distributions            (
1158*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('portlabel_id', KCSUBTYPE_TYPE.KC_ST_INT16, 0),
1159*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('portlabel_flags', KCSUBTYPE_TYPE.KC_ST_UINT16, 2),
1160*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('portlabel_domain', KCSUBTYPE_TYPE.KC_ST_UINT8, 4),
1161*5e3eaea3SApple OSS Distributions            ),
1162*5e3eaea3SApple OSS Distributions            'portlabel_info', merge=True)
1163*5e3eaea3SApple OSS Distributions
1164*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_PORTLABEL_NAME')] = (
1165*5e3eaea3SApple OSS Distributions    KCSubTypeElement("portlabel_name", KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(-1, 1), 0, 1))
1166*5e3eaea3SApple OSS Distributions
1167*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_THREAD_GROUP_SNAPSHOT')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_THREAD_GROUP'),
1168*5e3eaea3SApple OSS Distributions            (
1169*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('tgs_id', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1170*5e3eaea3SApple OSS Distributions                        KCSubTypeElement('tgs_name', KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(16, 1),
1171*5e3eaea3SApple OSS Distributions                            8, 1),
1172*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('tgs_flags', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
1173*5e3eaea3SApple OSS Distributions            ),
1174*5e3eaea3SApple OSS Distributions            'thread_group_snapshot')
1175*5e3eaea3SApple OSS Distributions
1176*5e3eaea3SApple OSS Distributions
1177*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_THREAD_GROUP')] = KCSubTypeElement('thread_group', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0, KCSubTypeElement._get_naked_element_value)
1178*5e3eaea3SApple OSS Distributions
1179*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_JETSAM_COALITION_SNAPSHOT')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_JETSAM_COALITION_SNAPSHOT'),
1180*5e3eaea3SApple OSS Distributions            (
1181*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('jcs_id', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1182*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('jcs_flags', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1183*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('jcs_thread_group', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
1184*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('jcs_leader_task_uniqueid', KCSUBTYPE_TYPE.KC_ST_UINT64, 24)
1185*5e3eaea3SApple OSS Distributions            ),
1186*5e3eaea3SApple OSS Distributions            'jetsam_coalition_snapshot')
1187*5e3eaea3SApple OSS Distributions
1188*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_JETSAM_COALITION')] = KCSubTypeElement('jetsam_coalition', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0, KCSubTypeElement._get_naked_element_value)
1189*5e3eaea3SApple OSS Distributions
1190*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_INSTRS_CYCLES')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_INSTRS_CYCLES'),
1191*5e3eaea3SApple OSS Distributions            (
1192*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('ics_instructions', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1193*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('ics_cycles', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1194*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('ics_p_instructions', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
1195*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('ics_p_cycles', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
1196*5e3eaea3SApple OSS Distributions            ),
1197*5e3eaea3SApple OSS Distributions            'instrs_cycles_snapshot')
1198*5e3eaea3SApple OSS Distributions
1199*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_TASK_CPU_ARCHITECTURE')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_TASK_CPU_ARCHITECTURE'),
1200*5e3eaea3SApple OSS Distributions            (
1201*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('cputype', KCSUBTYPE_TYPE.KC_ST_INT32, 0),
1202*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('cpusubtype', KCSUBTYPE_TYPE.KC_ST_INT32, 4)
1203*5e3eaea3SApple OSS Distributions            ),
1204*5e3eaea3SApple OSS Distributions            'task_cpu_architecture')
1205*5e3eaea3SApple OSS Distributions
1206*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_LATENCY_INFO')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_LATENCY_INFO'),
1207*5e3eaea3SApple OSS Distributions            (
1208*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('latency_version', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1209*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('setup_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1210*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('total_task_iteration_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
1211*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('total_terminated_task_iteration', KCSUBTYPE_TYPE.KC_ST_UINT64, 24)
1212*5e3eaea3SApple OSS Distributions            ),
1213*5e3eaea3SApple OSS Distributions            'stackshot_latency_collection')
1214*5e3eaea3SApple OSS Distributions
1215*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_LATENCY_INFO_TASK')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_LATENCY_INFO_TASK'),
1216*5e3eaea3SApple OSS Distributions            (
1217*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('task_uniqueid', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1218*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('setup_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1219*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('task_thread_count_loop_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
1220*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('task_thread_data_loop_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
1221*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('cur_tsnap_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 32),
1222*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('pmap_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 40),
1223*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('bsd_proc_ids_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 48),
1224*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('misc_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 56),
1225*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('misc2_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 64),
1226*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('end_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 72)
1227*5e3eaea3SApple OSS Distributions            ),
1228*5e3eaea3SApple OSS Distributions            'stackshot_latency_task')
1229*5e3eaea3SApple OSS Distributions
1230*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_LATENCY_INFO_THREAD')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_LATENCY_INFO_THREAD'),
1231*5e3eaea3SApple OSS Distributions            (
1232*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('thread_id', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1233*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('cur_thsnap1_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1234*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('dispatch_serial_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
1235*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('dispatch_label_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
1236*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('cur_thsnap2_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 32),
1237*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('thread_name_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 40),
1238*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('sur_times_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 48),
1239*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('user_stack_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 56),
1240*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('kernel_stack_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 64),
1241*5e3eaea3SApple OSS Distributions                        KCSubTypeElement.FromBasicCtype('misc_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 72),
1242*5e3eaea3SApple OSS Distributions            ),
1243*5e3eaea3SApple OSS Distributions            'stackshot_latency_thread')
1244*5e3eaea3SApple OSS Distributions
1245*5e3eaea3SApple OSS Distributionsdef set_type(name, *args):
1246*5e3eaea3SApple OSS Distributions    typ = GetTypeForName(name)
1247*5e3eaea3SApple OSS Distributions    KNOWN_TYPES_COLLECTION[typ] = KCTypeDescription(GetTypeForName(typ), *args)
1248*5e3eaea3SApple OSS Distributions
1249*5e3eaea3SApple OSS Distributions
1250*5e3eaea3SApple OSS Distributionsset_type('STACKSHOT_KCTYPE_USER_STACKTOP',
1251*5e3eaea3SApple OSS Distributions         (
1252*5e3eaea3SApple OSS Distributions             KCSubTypeElement.FromBasicCtype('sp', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1253*5e3eaea3SApple OSS Distributions             KCSubTypeElement('stack_contents', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(8, 1), 8, 1),
1254*5e3eaea3SApple OSS Distributions         ),
1255*5e3eaea3SApple OSS Distributions         'user_stacktop')
1256*5e3eaea3SApple OSS Distributions
1257*5e3eaea3SApple OSS Distributions#KNOWN_TYPES_COLLECTION[0x907] = KCSubTypeElement('donating_pids', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 0, 0, KCSubTypeElement._get_naked_element_value)
1258*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PID')] = KCSubTypeElement('pid', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1259*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PPID')] = KCSubTypeElement('ppid', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1260*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PROC_NAME')] = KCSubTypeElement('p_comm', KCSUBTYPE_TYPE.KC_ST_CHAR,
1261*5e3eaea3SApple OSS Distributions                           KCSubTypeElement.GetSizeForArray(32, 1), 0, 1)
1262*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_USERSTACK')] = KCSubTypeElement('userstack_ptr', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0)
1263*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_ARGSLEN')] = KCSubTypeElement('p_argslen', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1264*5e3eaea3SApple OSS Distributions
1265*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PROC_PATH')] = KCSubTypeElement('p_path', KCSUBTYPE_TYPE.KC_ST_CHAR,
1266*5e3eaea3SApple OSS Distributions                           KCSubTypeElement.GetSizeForArray(1024, 1), 0, 1)
1267*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PROC_CSFLAGS')] = KCSubTypeElement('p_csflags', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1268*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_UID')] = KCSubTypeElement('uid', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1269*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_GID')] = KCSubTypeElement('gid', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1270*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PROC_ARGC')] = KCSubTypeElement('argc', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1271*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PROC_FLAGS')] = KCSubTypeElement('p_flags', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1272*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_CPUTYPE')] = KCSubTypeElement('cputype', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1273*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_RESPONSIBLE_PID')] = KCSubTypeElement('responsible_pid', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1274*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_DIRTY_FLAGS')] = KCSubTypeElement('dirty_flags', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1275*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_CRASHED_THREADID')] = KCSubTypeElement('crashed_threadid', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0)
1276*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_COALITION_ID')] = KCSubTypeElement('coalition_id', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0)
1277*5e3eaea3SApple OSS Distributions
1278*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PROC_STATUS')] = KCSubTypeElement('p_status', KCSUBTYPE_TYPE.KC_ST_UINT8, 1, 0, 0)
1279*5e3eaea3SApple OSS Distributions
1280*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_BSDINFOWITHUNIQID')] = KCTypeDescription(GetTypeForName('TASK_CRASHINFO_BSDINFOWITHUNIQID'),
1281*5e3eaea3SApple OSS Distributions    (   KCSubTypeElement('p_uuid', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 0, 1),
1282*5e3eaea3SApple OSS Distributions        KCSubTypeElement.FromBasicCtype('p_uniqueid', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
1283*5e3eaea3SApple OSS Distributions        KCSubTypeElement.FromBasicCtype('p_puniqueid', KCSUBTYPE_TYPE.KC_ST_UINT64, 24)
1284*5e3eaea3SApple OSS Distributions    ),
1285*5e3eaea3SApple OSS Distributions    'proc_uniqidentifierinfo')
1286*5e3eaea3SApple OSS Distributions
1287*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_EXCEPTION_CODES')] = (
1288*5e3eaea3SApple OSS Distributions    KCTypeDescription(GetTypeForName('TASK_CRASHINFO_EXCEPTION_CODES'),
1289*5e3eaea3SApple OSS Distributions                      (KCSubTypeElement.FromBasicCtype('code_0', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1290*5e3eaea3SApple OSS Distributions                       KCSubTypeElement.FromBasicCtype('code_1', KCSUBTYPE_TYPE.KC_ST_UINT64, 8)),
1291*5e3eaea3SApple OSS Distributions                      'mach_exception_data_t'))
1292*5e3eaea3SApple OSS Distributions
1293*5e3eaea3SApple OSS Distributions
1294*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PROC_STARTTIME')] = (
1295*5e3eaea3SApple OSS Distributions    KCTypeDescription(GetTypeForName('TASK_CRASHINFO_PROC_STARTTIME'),
1296*5e3eaea3SApple OSS Distributions                      (KCSubTypeElement.FromBasicCtype('tv_sec', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1297*5e3eaea3SApple OSS Distributions                       KCSubTypeElement.FromBasicCtype('tv_usec', KCSUBTYPE_TYPE.KC_ST_UINT64, 8)),
1298*5e3eaea3SApple OSS Distributions                      'proc_starttime'))
1299*5e3eaea3SApple OSS Distributions
1300*5e3eaea3SApple OSS Distributions
1301*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_RUSAGE_INFO')] = KCTypeDescription(GetTypeForName('TASK_CRASHINFO_RUSAGE_INFO'),
1302*5e3eaea3SApple OSS Distributions    (
1303*5e3eaea3SApple OSS Distributions        KCSubTypeElement('ri_uuid', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 0, 1),
1304*5e3eaea3SApple OSS Distributions            KCSubTypeElement.FromBasicCtype('ri_user_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
1305*5e3eaea3SApple OSS Distributions            KCSubTypeElement.FromBasicCtype('ri_system_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
1306*5e3eaea3SApple OSS Distributions            KCSubTypeElement.FromBasicCtype('ri_pkg_idle_wkups', KCSUBTYPE_TYPE.KC_ST_UINT64, 32),
1307*5e3eaea3SApple OSS Distributions            KCSubTypeElement.FromBasicCtype('ri_interrupt_wkups', KCSUBTYPE_TYPE.KC_ST_UINT64, 40),
1308*5e3eaea3SApple OSS Distributions            KCSubTypeElement.FromBasicCtype('ri_pageins', KCSUBTYPE_TYPE.KC_ST_UINT64, 48),
1309*5e3eaea3SApple OSS Distributions            KCSubTypeElement.FromBasicCtype('ri_wired_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 56),
1310*5e3eaea3SApple OSS Distributions            KCSubTypeElement.FromBasicCtype('ri_resident_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 64),
1311*5e3eaea3SApple OSS Distributions            KCSubTypeElement.FromBasicCtype('ri_phys_footprint', KCSUBTYPE_TYPE.KC_ST_UINT64, 72),
1312*5e3eaea3SApple OSS Distributions            KCSubTypeElement.FromBasicCtype('ri_proc_start_abstime', KCSUBTYPE_TYPE.KC_ST_UINT64, 80),
1313*5e3eaea3SApple OSS Distributions            KCSubTypeElement.FromBasicCtype('ri_proc_exit_abstime', KCSUBTYPE_TYPE.KC_ST_UINT64, 88),
1314*5e3eaea3SApple OSS Distributions            KCSubTypeElement.FromBasicCtype('ri_child_user_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 96),
1315*5e3eaea3SApple OSS Distributions            KCSubTypeElement.FromBasicCtype('ri_child_system_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 104),
1316*5e3eaea3SApple OSS Distributions            KCSubTypeElement.FromBasicCtype('ri_child_pkg_idle_wkups', KCSUBTYPE_TYPE.KC_ST_UINT64, 112),
1317*5e3eaea3SApple OSS Distributions            KCSubTypeElement.FromBasicCtype('ri_child_interrupt_wkups', KCSUBTYPE_TYPE.KC_ST_UINT64, 120),
1318*5e3eaea3SApple OSS Distributions            KCSubTypeElement.FromBasicCtype('ri_child_pageins', KCSUBTYPE_TYPE.KC_ST_UINT64, 128),
1319*5e3eaea3SApple OSS Distributions            KCSubTypeElement.FromBasicCtype('ri_child_elapsed_abstime', KCSUBTYPE_TYPE.KC_ST_UINT64, 136),
1320*5e3eaea3SApple OSS Distributions            KCSubTypeElement.FromBasicCtype('ri_diskio_bytesread', KCSUBTYPE_TYPE.KC_ST_UINT64, 144),
1321*5e3eaea3SApple OSS Distributions            KCSubTypeElement.FromBasicCtype('ri_diskio_byteswritten', KCSUBTYPE_TYPE.KC_ST_UINT64, 152),
1322*5e3eaea3SApple OSS Distributions            KCSubTypeElement.FromBasicCtype('ri_cpu_time_qos_default', KCSUBTYPE_TYPE.KC_ST_UINT64, 160),
1323*5e3eaea3SApple OSS Distributions            KCSubTypeElement.FromBasicCtype('ri_cpu_time_qos_maintenance', KCSUBTYPE_TYPE.KC_ST_UINT64, 168),
1324*5e3eaea3SApple OSS Distributions            KCSubTypeElement.FromBasicCtype('ri_cpu_time_qos_background', KCSUBTYPE_TYPE.KC_ST_UINT64, 176),
1325*5e3eaea3SApple OSS Distributions            KCSubTypeElement.FromBasicCtype('ri_cpu_time_qos_utility', KCSUBTYPE_TYPE.KC_ST_UINT64, 184),
1326*5e3eaea3SApple OSS Distributions            KCSubTypeElement.FromBasicCtype('ri_cpu_time_qos_legacy', KCSUBTYPE_TYPE.KC_ST_UINT64, 192),
1327*5e3eaea3SApple OSS Distributions            KCSubTypeElement.FromBasicCtype('ri_cpu_time_qos_user_initiated', KCSUBTYPE_TYPE.KC_ST_UINT64, 200),
1328*5e3eaea3SApple OSS Distributions            KCSubTypeElement.FromBasicCtype('ri_cpu_time_qos_user_interactiv', KCSUBTYPE_TYPE.KC_ST_UINT64, 208),
1329*5e3eaea3SApple OSS Distributions            KCSubTypeElement.FromBasicCtype('ri_billed_system_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 216),
1330*5e3eaea3SApple OSS Distributions            KCSubTypeElement.FromBasicCtype('ri_serviced_system_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 224)
1331*5e3eaea3SApple OSS Distributions    ),
1332*5e3eaea3SApple OSS Distributions    'rusage_info')
1333*5e3eaea3SApple OSS Distributions
1334*5e3eaea3SApple OSS Distributions#The sizes for these need to be kept in sync with
1335*5e3eaea3SApple OSS Distributions#MAX_CRASHINFO_SIGNING_ID_LEN, MAX_CRASHINFO_TEAM_ID_LEN
1336*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_CS_SIGNING_ID')] = KCSubTypeElement('cs_signing_id', KCSUBTYPE_TYPE.KC_ST_CHAR,
1337*5e3eaea3SApple OSS Distributions                           KCSubTypeElement.GetSizeForArray(64, 1), 0, 1)
1338*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_CS_TEAM_ID')] = KCSubTypeElement('cs_team_id', KCSUBTYPE_TYPE.KC_ST_CHAR,
1339*5e3eaea3SApple OSS Distributions                           KCSubTypeElement.GetSizeForArray(32, 1), 0, 1)
1340*5e3eaea3SApple OSS Distributions
1341*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_CS_VALIDATION_CATEGORY')] = KCSubTypeElement('cs_validation_category', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 0, 0)
1342*5e3eaea3SApple OSS Distributions
1343*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_CS_TRUST_LEVEL')] = KCSubTypeElement('cs_trust_level', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 0, 0)
1344*5e3eaea3SApple OSS Distributions
1345*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_CPU_TIMES')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_CPU_TIMES'),
1346*5e3eaea3SApple OSS Distributions    (
1347*5e3eaea3SApple OSS Distributions        KCSubTypeElement.FromBasicCtype('user_usec', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1348*5e3eaea3SApple OSS Distributions        KCSubTypeElement.FromBasicCtype('system_usec', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1349*5e3eaea3SApple OSS Distributions        KCSubTypeElement.FromBasicCtype('runnable_usec', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
1350*5e3eaea3SApple OSS Distributions    ), 'cpu_times')
1351*5e3eaea3SApple OSS Distributions
1352*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_STACKSHOT_DURATION')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_STACKSHOT_DURATION'),
1353*5e3eaea3SApple OSS Distributions    (
1354*5e3eaea3SApple OSS Distributions        KCSubTypeElement.FromBasicCtype('stackshot_duration', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1355*5e3eaea3SApple OSS Distributions        KCSubTypeElement.FromBasicCtype('stackshot_duration_outer', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1356*5e3eaea3SApple OSS Distributions        KCSubTypeElement.FromBasicCtype('stackshot_duration_prior', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
1357*5e3eaea3SApple OSS Distributions    ), 'stackshot_duration', merge=True
1358*5e3eaea3SApple OSS Distributions)
1359*5e3eaea3SApple OSS Distributions
1360*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('KCDATA_TYPE_PROCNAME')] = (
1361*5e3eaea3SApple OSS Distributions    KCSubTypeElement("proc_name", KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(-1, 1), 0, 1))
1362*5e3eaea3SApple OSS Distributions
1363*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('KCDATA_TYPE_PID')] = (
1364*5e3eaea3SApple OSS Distributions    KCSubTypeElement('pid', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0))
1365*5e3eaea3SApple OSS Distributions
1366*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('KCDATA_TYPE_LIBRARY_AOTINFO')] = KCTypeDescription(GetTypeForName('KCDATA_TYPE_LIBRARY_AOTINFO'),
1367*5e3eaea3SApple OSS Distributions    (
1368*5e3eaea3SApple OSS Distributions        KCSubTypeElement('x86LoadAddress', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0),
1369*5e3eaea3SApple OSS Distributions        KCSubTypeElement('aotLoadAddress', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 8, 0),
1370*5e3eaea3SApple OSS Distributions        KCSubTypeElement('aotImageSize', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 16, 0),
1371*5e3eaea3SApple OSS Distributions        KCSubTypeElement('aotImageKey', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(32, 1), 24, 1),
1372*5e3eaea3SApple OSS Distributions    ), 'dyld_aot_info')
1373*5e3eaea3SApple OSS Distributions
1374*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('EXIT_REASON_SNAPSHOT')] = KCTypeDescription(GetTypeForName('EXIT_REASON_SNAPSHOT'),
1375*5e3eaea3SApple OSS Distributions    (
1376*5e3eaea3SApple OSS Distributions        KCSubTypeElement.FromBasicCtype('ers_namespace', KCSUBTYPE_TYPE.KC_ST_UINT32, 0),
1377*5e3eaea3SApple OSS Distributions        KCSubTypeElement.FromBasicCtype('ers_code', KCSUBTYPE_TYPE.KC_ST_UINT64, 4),
1378*5e3eaea3SApple OSS Distributions        KCSubTypeElement.FromBasicCtype('ers_flags', KCSUBTYPE_TYPE.KC_ST_UINT64, 12),
1379*5e3eaea3SApple OSS Distributions    ), 'exit_reason_basic_info')
1380*5e3eaea3SApple OSS Distributions
1381*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('EXIT_REASON_USER_DESC')] = (
1382*5e3eaea3SApple OSS Distributions    KCSubTypeElement("exit_reason_user_description", KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(-1, 1), 0, 1))
1383*5e3eaea3SApple OSS Distributions
1384*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('EXIT_REASON_USER_PAYLOAD')] = KCSubTypeElement('exit_reason_user_payload',
1385*5e3eaea3SApple OSS Distributions        KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(-1, 1), 0, 1)
1386*5e3eaea3SApple OSS Distributions
1387*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('EXIT_REASON_CODESIGNING_INFO')] = KCTypeDescription(GetTypeForName('EXIT_REASON_CODESIGNING_INFO'),
1388*5e3eaea3SApple OSS Distributions    (
1389*5e3eaea3SApple OSS Distributions        KCSubTypeElement.FromBasicCtype('ceri_virt_addr', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1390*5e3eaea3SApple OSS Distributions        KCSubTypeElement.FromBasicCtype('ceri_file_offset', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1391*5e3eaea3SApple OSS Distributions        KCSubTypeElement("ceri_pathname", KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(1024, 1), 16, 1),
1392*5e3eaea3SApple OSS Distributions        KCSubTypeElement("ceri_filename", KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(1024, 1), 1040, 1),
1393*5e3eaea3SApple OSS Distributions        KCSubTypeElement.FromBasicCtype('ceri_codesig_modtime_secs', KCSUBTYPE_TYPE.KC_ST_UINT64, 2064),
1394*5e3eaea3SApple OSS Distributions        KCSubTypeElement.FromBasicCtype('ceri_codesig_modtime_nsecs', KCSUBTYPE_TYPE.KC_ST_UINT64, 2072),
1395*5e3eaea3SApple OSS Distributions        KCSubTypeElement.FromBasicCtype('ceri_page_modtime_secs', KCSUBTYPE_TYPE.KC_ST_UINT64, 2080),
1396*5e3eaea3SApple OSS Distributions        KCSubTypeElement.FromBasicCtype('ceri_page_modtime_nsecs', KCSUBTYPE_TYPE.KC_ST_UINT64, 2088),
1397*5e3eaea3SApple OSS Distributions        KCSubTypeElement.FromBasicCtype('ceri_path_truncated', KCSUBTYPE_TYPE.KC_ST_UINT8, 2096),
1398*5e3eaea3SApple OSS Distributions        KCSubTypeElement.FromBasicCtype('ceri_object_codesigned', KCSUBTYPE_TYPE.KC_ST_UINT8, 2097),
1399*5e3eaea3SApple OSS Distributions        KCSubTypeElement.FromBasicCtype('ceri_page_codesig_validated', KCSUBTYPE_TYPE.KC_ST_UINT8, 2098),
1400*5e3eaea3SApple OSS Distributions        KCSubTypeElement.FromBasicCtype('ceri_page_codesig_tainted', KCSUBTYPE_TYPE.KC_ST_UINT8, 2099),
1401*5e3eaea3SApple OSS Distributions        KCSubTypeElement.FromBasicCtype('ceri_page_codesig_nx', KCSUBTYPE_TYPE.KC_ST_UINT8, 2100),
1402*5e3eaea3SApple OSS Distributions        KCSubTypeElement.FromBasicCtype('ceri_page_wpmapped', KCSUBTYPE_TYPE.KC_ST_UINT8, 2101),
1403*5e3eaea3SApple OSS Distributions        KCSubTypeElement.FromBasicCtype('ceri_page_slid', KCSUBTYPE_TYPE.KC_ST_UINT8, 2102),
1404*5e3eaea3SApple OSS Distributions        KCSubTypeElement.FromBasicCtype('ceri_page_dirty', KCSUBTYPE_TYPE.KC_ST_UINT8, 2103),
1405*5e3eaea3SApple OSS Distributions        KCSubTypeElement.FromBasicCtype('ceri_page_shadow_depth', KCSUBTYPE_TYPE.KC_ST_UINT32, 2104),
1406*5e3eaea3SApple OSS Distributions    ), 'exit_reason_codesigning_info')
1407*5e3eaea3SApple OSS Distributions
1408*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('EXIT_REASON_WORKLOOP_ID')] = (
1409*5e3eaea3SApple OSS Distributions        KCSubTypeElement('exit_reason_workloop_id', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0, KCSubTypeElement._get_naked_element_value))
1410*5e3eaea3SApple OSS Distributions
1411*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('EXIT_REASON_DISPATCH_QUEUE_NO')] = (
1412*5e3eaea3SApple OSS Distributions        KCSubTypeElement('exit_reason_dispatch_queue_no', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0, KCSubTypeElement._get_naked_element_value))
1413*5e3eaea3SApple OSS Distributions
1414*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_ASID')] = (
1415*5e3eaea3SApple OSS Distributions    KCSubTypeElement('ts_asid', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 0, 0))
1416*5e3eaea3SApple OSS Distributions
1417*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_PAGE_TABLES')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_PAGE_TABLES'), (
1418*5e3eaea3SApple OSS Distributions    KCSubTypeElement(None, KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0, KCSubTypeElement._get_naked_element_value), ),
1419*5e3eaea3SApple OSS Distributions    'ts_pagetable',
1420*5e3eaea3SApple OSS Distributions    merge=True,
1421*5e3eaea3SApple OSS Distributions    naked=True
1422*5e3eaea3SApple OSS Distributions)
1423*5e3eaea3SApple OSS Distributions
1424*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_SUSPENSION_INFO')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_SUSPENSION_INFO'), (
1425*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tss_last_start', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1426*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tss_last_end', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1427*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tss_count', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
1428*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tss_duration', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
1429*5e3eaea3SApple OSS Distributions), 'suspension_info')
1430*5e3eaea3SApple OSS Distributions
1431*5e3eaea3SApple OSS DistributionsKNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_SUSPENSION_SOURCE')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_SUSPENSION_SOURCE'), (
1432*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tss_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1433*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tss_tid', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1434*5e3eaea3SApple OSS Distributions    KCSubTypeElement.FromBasicCtype('tss_pid', KCSUBTYPE_TYPE.KC_ST_INT32, 16),
1435*5e3eaea3SApple OSS Distributions    KCSubTypeElement('tss_procname', KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(65, 1), 20, 1)
1436*5e3eaea3SApple OSS Distributions), 'suspension_source')
1437*5e3eaea3SApple OSS Distributions
1438*5e3eaea3SApple OSS Distributionsdef GetSecondsFromMATime(mat, tb):
1439*5e3eaea3SApple OSS Distributions    return (float(long(mat) * tb['numer']) / tb['denom']) / 1e9
1440*5e3eaea3SApple OSS Distributions
1441*5e3eaea3SApple OSS Distributionsdef GetLongForAddress(address):
1442*5e3eaea3SApple OSS Distributions    if isinstance(address, six.string_types):
1443*5e3eaea3SApple OSS Distributions        if '0x' in address.lower():
1444*5e3eaea3SApple OSS Distributions            address = long(address, 16)
1445*5e3eaea3SApple OSS Distributions        else:
1446*5e3eaea3SApple OSS Distributions            address = long(address)
1447*5e3eaea3SApple OSS Distributions    return address
1448*5e3eaea3SApple OSS Distributions
1449*5e3eaea3SApple OSS Distributionsdef FindLibraryForAddress(liblist, address):
1450*5e3eaea3SApple OSS Distributions    current_lib = None
1451*5e3eaea3SApple OSS Distributions    for l in liblist:
1452*5e3eaea3SApple OSS Distributions        l_addr = GetLongForAddress(l[1])
1453*5e3eaea3SApple OSS Distributions        if address >= l_addr:
1454*5e3eaea3SApple OSS Distributions            current_lib = l
1455*5e3eaea3SApple OSS Distributions    return current_lib
1456*5e3eaea3SApple OSS Distributions
1457*5e3eaea3SApple OSS Distributionsdef FindIndexOfLibInCatalog(catalog, lib):
1458*5e3eaea3SApple OSS Distributions    index = None
1459*5e3eaea3SApple OSS Distributions    i = 0
1460*5e3eaea3SApple OSS Distributions    for l in catalog:
1461*5e3eaea3SApple OSS Distributions        if l[0] == lib[0] and l[1] == lib[1]:
1462*5e3eaea3SApple OSS Distributions            index = i
1463*5e3eaea3SApple OSS Distributions            break
1464*5e3eaea3SApple OSS Distributions        i += 1
1465*5e3eaea3SApple OSS Distributions
1466*5e3eaea3SApple OSS Distributions    if index is None:
1467*5e3eaea3SApple OSS Distributions        catalog.append(lib)
1468*5e3eaea3SApple OSS Distributions        index = len(catalog) - 1
1469*5e3eaea3SApple OSS Distributions
1470*5e3eaea3SApple OSS Distributions    return index
1471*5e3eaea3SApple OSS Distributions
1472*5e3eaea3SApple OSS Distributionsdef GetOffsetOfAddressForLib(lib, address):
1473*5e3eaea3SApple OSS Distributions    return (address - GetLongForAddress(lib[1]))
1474*5e3eaea3SApple OSS Distributions
1475*5e3eaea3SApple OSS Distributionsdef GetSymbolInfoForFrame(catalog, liblist, address):
1476*5e3eaea3SApple OSS Distributions    address = GetLongForAddress(address)
1477*5e3eaea3SApple OSS Distributions    lib = FindLibraryForAddress(liblist, address)
1478*5e3eaea3SApple OSS Distributions    if not lib:
1479*5e3eaea3SApple OSS Distributions        lib = ["00000000000000000000000000000000",0,"A"]
1480*5e3eaea3SApple OSS Distributions    offset = GetOffsetOfAddressForLib(lib, address)
1481*5e3eaea3SApple OSS Distributions    index = FindIndexOfLibInCatalog(catalog, lib)
1482*5e3eaea3SApple OSS Distributions    return [index, offset]
1483*5e3eaea3SApple OSS Distributions
1484*5e3eaea3SApple OSS Distributionsdef GetStateDescription(s):
1485*5e3eaea3SApple OSS Distributions    retval = []
1486*5e3eaea3SApple OSS Distributions    TH_WAIT = 0x01
1487*5e3eaea3SApple OSS Distributions    TH_SUSP = 0x02
1488*5e3eaea3SApple OSS Distributions    TH_RUN = 0x04
1489*5e3eaea3SApple OSS Distributions    TH_UNINT = 0x08
1490*5e3eaea3SApple OSS Distributions    TH_TERMINATE = 0x10
1491*5e3eaea3SApple OSS Distributions    TH_TERMINATE2 = 0x20
1492*5e3eaea3SApple OSS Distributions    TH_WAIT_REPORT = 0x40
1493*5e3eaea3SApple OSS Distributions    TH_IDLE = 0x80
1494*5e3eaea3SApple OSS Distributions    if (s & TH_WAIT):
1495*5e3eaea3SApple OSS Distributions        retval.append("TH_WAIT")
1496*5e3eaea3SApple OSS Distributions    if (s & TH_SUSP):
1497*5e3eaea3SApple OSS Distributions        retval.append("TH_SUSP")
1498*5e3eaea3SApple OSS Distributions    if (s & TH_RUN):
1499*5e3eaea3SApple OSS Distributions        retval.append("TH_RUN")
1500*5e3eaea3SApple OSS Distributions    if (s & TH_UNINT):
1501*5e3eaea3SApple OSS Distributions        retval.append("TH_UNINT")
1502*5e3eaea3SApple OSS Distributions    if (s & TH_TERMINATE):
1503*5e3eaea3SApple OSS Distributions        retval.append("TH_TERMINATE")
1504*5e3eaea3SApple OSS Distributions    if (s & TH_TERMINATE2):
1505*5e3eaea3SApple OSS Distributions        retval.append("TH_TERMINATE2")
1506*5e3eaea3SApple OSS Distributions    if (s & TH_WAIT_REPORT):
1507*5e3eaea3SApple OSS Distributions        retval.append("TH_WAIT_REPORT")
1508*5e3eaea3SApple OSS Distributions    if (s & TH_IDLE):
1509*5e3eaea3SApple OSS Distributions        retval.append("TH_IDLE")
1510*5e3eaea3SApple OSS Distributions    return retval
1511*5e3eaea3SApple OSS Distributions
1512*5e3eaea3SApple OSS Distributions
1513*5e3eaea3SApple OSS Distributionsdef format_uuid(elementValues):
1514*5e3eaea3SApple OSS Distributions    # sometimes we get string like "25A926D8-F742-3E5E..."
1515*5e3eaea3SApple OSS Distributions    if isinstance(elementValues, six.string_types):
1516*5e3eaea3SApple OSS Distributions        return elementValues
1517*5e3eaea3SApple OSS Distributions    return ''.join("%02x" % i for i in elementValues)
1518*5e3eaea3SApple OSS Distributions
1519*5e3eaea3SApple OSS DistributionskThreadWaitNone			= 0x00
1520*5e3eaea3SApple OSS DistributionskThreadWaitKernelMutex          = 0x01
1521*5e3eaea3SApple OSS DistributionskThreadWaitPortReceive          = 0x02
1522*5e3eaea3SApple OSS DistributionskThreadWaitPortSetReceive       = 0x03
1523*5e3eaea3SApple OSS DistributionskThreadWaitPortSend             = 0x04
1524*5e3eaea3SApple OSS DistributionskThreadWaitPortSendInTransit    = 0x05
1525*5e3eaea3SApple OSS DistributionskThreadWaitSemaphore            = 0x06
1526*5e3eaea3SApple OSS DistributionskThreadWaitKernelRWLockRead     = 0x07
1527*5e3eaea3SApple OSS DistributionskThreadWaitKernelRWLockWrite    = 0x08
1528*5e3eaea3SApple OSS DistributionskThreadWaitKernelRWLockUpgrade  = 0x09
1529*5e3eaea3SApple OSS DistributionskThreadWaitUserLock             = 0x0a
1530*5e3eaea3SApple OSS DistributionskThreadWaitPThreadMutex         = 0x0b
1531*5e3eaea3SApple OSS DistributionskThreadWaitPThreadRWLockRead    = 0x0c
1532*5e3eaea3SApple OSS DistributionskThreadWaitPThreadRWLockWrite   = 0x0d
1533*5e3eaea3SApple OSS DistributionskThreadWaitPThreadCondVar       = 0x0e
1534*5e3eaea3SApple OSS DistributionskThreadWaitParkedWorkQueue      = 0x0f
1535*5e3eaea3SApple OSS DistributionskThreadWaitWorkloopSyncWait     = 0x10
1536*5e3eaea3SApple OSS DistributionskThreadWaitOnProcess            = 0x11
1537*5e3eaea3SApple OSS DistributionskThreadWaitSleepWithInheritor   = 0x12
1538*5e3eaea3SApple OSS DistributionskThreadWaitEventlink            = 0x13
1539*5e3eaea3SApple OSS DistributionskThreadWaitCompressor           = 0x14
1540*5e3eaea3SApple OSS Distributions
1541*5e3eaea3SApple OSS Distributions
1542*5e3eaea3SApple OSS DistributionsUINT64_MAX = 0xffffffffffffffff
1543*5e3eaea3SApple OSS DistributionsSTACKSHOT_WAITOWNER_KERNEL      = (UINT64_MAX - 1)
1544*5e3eaea3SApple OSS DistributionsSTACKSHOT_WAITOWNER_PORT_LOCKED = (UINT64_MAX - 2)
1545*5e3eaea3SApple OSS DistributionsSTACKSHOT_WAITOWNER_PSET_LOCKED = (UINT64_MAX - 3)
1546*5e3eaea3SApple OSS DistributionsSTACKSHOT_WAITOWNER_INTRANSIT   = (UINT64_MAX - 4)
1547*5e3eaea3SApple OSS DistributionsSTACKSHOT_WAITOWNER_MTXSPIN     = (UINT64_MAX - 5)
1548*5e3eaea3SApple OSS DistributionsSTACKSHOT_WAITOWNER_THREQUESTED = (UINT64_MAX - 6)
1549*5e3eaea3SApple OSS DistributionsSTACKSHOT_WAITOWNER_SUSPENDED   = (UINT64_MAX - 7)
1550*5e3eaea3SApple OSS Distributions
1551*5e3eaea3SApple OSS DistributionsSTACKSHOT_TURNSTILE_STATUS_UNKNOWN         = 0x01
1552*5e3eaea3SApple OSS DistributionsSTACKSHOT_TURNSTILE_STATUS_LOCKED_WAITQ    = 0x02
1553*5e3eaea3SApple OSS DistributionsSTACKSHOT_TURNSTILE_STATUS_WORKQUEUE       = 0x04
1554*5e3eaea3SApple OSS DistributionsSTACKSHOT_TURNSTILE_STATUS_THREAD          = 0x08
1555*5e3eaea3SApple OSS DistributionsSTACKSHOT_TURNSTILE_STATUS_BLOCKED_ON_TASK = 0x10
1556*5e3eaea3SApple OSS DistributionsSTACKSHOT_TURNSTILE_STATUS_HELD_IPLOCK     = 0x20
1557*5e3eaea3SApple OSS DistributionsSTACKSHOT_TURNSTILE_STATUS_SENDPORT        = 0x40
1558*5e3eaea3SApple OSS DistributionsSTACKSHOT_TURNSTILE_STATUS_RECEIVEPORT     = 0x80
1559*5e3eaea3SApple OSS Distributions
1560*5e3eaea3SApple OSS Distributions#
1561*5e3eaea3SApple OSS Distributions# These come from xpc_domain_type_t in <xpc/launch_private.h>
1562*5e3eaea3SApple OSS DistributionsPORTLABEL_DOMAINS = {
1563*5e3eaea3SApple OSS Distributions    1: 'system',        # XPC_DOMAIN_SYSTEM
1564*5e3eaea3SApple OSS Distributions    2: 'user',          # XPC_DOMAIN_USER
1565*5e3eaea3SApple OSS Distributions    5: 'pid',           # XPC_DOMAIN_PID
1566*5e3eaea3SApple OSS Distributions    7: 'port',          # XPC_DOMAIN_PORT
1567*5e3eaea3SApple OSS Distributions}
1568*5e3eaea3SApple OSS Distributionsdef portlabel_domain(x):
1569*5e3eaea3SApple OSS Distributions    if x is None:
1570*5e3eaea3SApple OSS Distributions        return "unknown"
1571*5e3eaea3SApple OSS Distributions    return PORTLABEL_DOMAINS.get(x, "unknown.{}".format(x))
1572*5e3eaea3SApple OSS Distributions
1573*5e3eaea3SApple OSS DistributionsSTACKSHOT_WAITINFO_FLAGS_SPECIALREPLY = 0x1
1574*5e3eaea3SApple OSS DistributionsSTACKSHOT_PORTLABEL_THROTTLED = 0x2
1575*5e3eaea3SApple OSS Distributions
1576*5e3eaea3SApple OSS Distributionsdef portThrottledSuffix(portlabel_flags):
1577*5e3eaea3SApple OSS Distributions    if (portlabel_flags & STACKSHOT_PORTLABEL_THROTTLED):
1578*5e3eaea3SApple OSS Distributions        return " (service port throttled by launchd)"
1579*5e3eaea3SApple OSS Distributions    else:
1580*5e3eaea3SApple OSS Distributions        return ""
1581*5e3eaea3SApple OSS Distributions
1582*5e3eaea3SApple OSS Distributionsdef formatPortLabelID(portlabel_id, portlabels):
1583*5e3eaea3SApple OSS Distributions    portlabel = {}
1584*5e3eaea3SApple OSS Distributions    if portlabel_id > 0:
1585*5e3eaea3SApple OSS Distributions        if portlabels is not None:
1586*5e3eaea3SApple OSS Distributions            portlabel = portlabels.get(str(portlabel_id), {})
1587*5e3eaea3SApple OSS Distributions        portlabel_name = portlabel_domain(portlabel.get('portlabel_domain')) + " "
1588*5e3eaea3SApple OSS Distributions        portlabel_name += portlabel.get("portlabel_name", "!!!unknown, ID {} !!!".format(portlabel_id));
1589*5e3eaea3SApple OSS Distributions        return " {" + portlabel_name + portThrottledSuffix(portlabel.get('portlabel_flags', 0)) + "}"
1590*5e3eaea3SApple OSS Distributions    if portlabel_id < 0:
1591*5e3eaea3SApple OSS Distributions        return " {labeled, info truncated" + portThrottledSuffix(portlabel.get('portlabel_flags', 0)) + "}"
1592*5e3eaea3SApple OSS Distributions    return ""
1593*5e3eaea3SApple OSS Distributions
1594*5e3eaea3SApple OSS Distributionsdef formatWaitInfo(info, wantHex, portlabels):
1595*5e3eaea3SApple OSS Distributions    base='#x' if wantHex else 'd'
1596*5e3eaea3SApple OSS Distributions    s = 'thread {0:{base}}: '.format(info['waiter'], base=base)
1597*5e3eaea3SApple OSS Distributions    type = info['wait_type']
1598*5e3eaea3SApple OSS Distributions    context = info['context']
1599*5e3eaea3SApple OSS Distributions    owner = info['owner']
1600*5e3eaea3SApple OSS Distributions    ownerThread = "{0:{base}}".format(owner, base=base)
1601*5e3eaea3SApple OSS Distributions    portlabel_id = info.get('portlabel_id', 0)
1602*5e3eaea3SApple OSS Distributions    flags = info.get('wait_flags', 0)
1603*5e3eaea3SApple OSS Distributions
1604*5e3eaea3SApple OSS Distributions    if type == kThreadWaitKernelMutex:
1605*5e3eaea3SApple OSS Distributions        s += 'kernel mutex %x' % context
1606*5e3eaea3SApple OSS Distributions        if owner == STACKSHOT_WAITOWNER_MTXSPIN:
1607*5e3eaea3SApple OSS Distributions            s += " in spin mode"
1608*5e3eaea3SApple OSS Distributions        elif owner:
1609*5e3eaea3SApple OSS Distributions            s += " owned by thread %s" % ownerThread
1610*5e3eaea3SApple OSS Distributions        else:
1611*5e3eaea3SApple OSS Distributions            s += "with unknown owner"
1612*5e3eaea3SApple OSS Distributions    elif type == kThreadWaitPortReceive:
1613*5e3eaea3SApple OSS Distributions        s += "mach_msg receive on "
1614*5e3eaea3SApple OSS Distributions        if flags & STACKSHOT_WAITINFO_FLAGS_SPECIALREPLY:
1615*5e3eaea3SApple OSS Distributions            s += "REPLY "
1616*5e3eaea3SApple OSS Distributions            flags = flags - STACKSHOT_WAITINFO_FLAGS_SPECIALREPLY
1617*5e3eaea3SApple OSS Distributions        if owner == STACKSHOT_WAITOWNER_PORT_LOCKED:
1618*5e3eaea3SApple OSS Distributions            s += "locked port %x" % context
1619*5e3eaea3SApple OSS Distributions        elif owner == STACKSHOT_WAITOWNER_INTRANSIT:
1620*5e3eaea3SApple OSS Distributions            s += "intransit port %x" % context
1621*5e3eaea3SApple OSS Distributions        elif owner:
1622*5e3eaea3SApple OSS Distributions            s += "port %x name %x" % (context, owner)
1623*5e3eaea3SApple OSS Distributions        else:
1624*5e3eaea3SApple OSS Distributions            s += "port %x" % context
1625*5e3eaea3SApple OSS Distributions    elif type == kThreadWaitPortSetReceive:
1626*5e3eaea3SApple OSS Distributions        if owner == STACKSHOT_WAITOWNER_PSET_LOCKED:
1627*5e3eaea3SApple OSS Distributions            s += "mach_msg receive on locked port set %x" % context
1628*5e3eaea3SApple OSS Distributions        else:
1629*5e3eaea3SApple OSS Distributions            s += "mach_msg receive on port set %x" % context
1630*5e3eaea3SApple OSS Distributions    elif type == kThreadWaitPortSend:
1631*5e3eaea3SApple OSS Distributions        s += "mach_msg send on "
1632*5e3eaea3SApple OSS Distributions        if owner == STACKSHOT_WAITOWNER_PORT_LOCKED:
1633*5e3eaea3SApple OSS Distributions            s += "locked port %x" % context
1634*5e3eaea3SApple OSS Distributions        elif owner == STACKSHOT_WAITOWNER_INTRANSIT:
1635*5e3eaea3SApple OSS Distributions            s += "intransit port %x" % context
1636*5e3eaea3SApple OSS Distributions        elif owner == STACKSHOT_WAITOWNER_KERNEL:
1637*5e3eaea3SApple OSS Distributions            s += "port %x owned by kernel" % context
1638*5e3eaea3SApple OSS Distributions        elif owner:
1639*5e3eaea3SApple OSS Distributions            s += "port %x owned by pid %d" % (context, owner)
1640*5e3eaea3SApple OSS Distributions        else:
1641*5e3eaea3SApple OSS Distributions            s += "port %x with unknown owner" % context
1642*5e3eaea3SApple OSS Distributions    elif type == kThreadWaitPortSendInTransit:
1643*5e3eaea3SApple OSS Distributions        s += "mach_msg send on port %x in transit to " % context
1644*5e3eaea3SApple OSS Distributions        if owner:
1645*5e3eaea3SApple OSS Distributions            s += "port %x" % owner
1646*5e3eaea3SApple OSS Distributions        else:
1647*5e3eaea3SApple OSS Distributions            s += "unknown port"
1648*5e3eaea3SApple OSS Distributions    elif type == kThreadWaitSemaphore:
1649*5e3eaea3SApple OSS Distributions        s += "semaphore port %x " % context
1650*5e3eaea3SApple OSS Distributions        if owner:
1651*5e3eaea3SApple OSS Distributions            s += "owned by pid %d" % owner
1652*5e3eaea3SApple OSS Distributions        else:
1653*5e3eaea3SApple OSS Distributions            s += "with unknown owner"
1654*5e3eaea3SApple OSS Distributions    elif type == kThreadWaitKernelRWLockRead:
1655*5e3eaea3SApple OSS Distributions        s += "krwlock %x for reading" % context
1656*5e3eaea3SApple OSS Distributions        if owner:
1657*5e3eaea3SApple OSS Distributions            s += " owned by thread %s" % ownerThread
1658*5e3eaea3SApple OSS Distributions    elif type == kThreadWaitKernelRWLockWrite:
1659*5e3eaea3SApple OSS Distributions        s += "krwlock %x for writing" % context
1660*5e3eaea3SApple OSS Distributions        if owner:
1661*5e3eaea3SApple OSS Distributions            s += " owned by thread %s" % ownerThread
1662*5e3eaea3SApple OSS Distributions    elif type == kThreadWaitKernelRWLockUpgrade:
1663*5e3eaea3SApple OSS Distributions        s += "krwlock %x for upgrading" % context
1664*5e3eaea3SApple OSS Distributions        if owner:
1665*5e3eaea3SApple OSS Distributions            s += " owned by thread %s" % ownerThread
1666*5e3eaea3SApple OSS Distributions    elif type == kThreadWaitUserLock:
1667*5e3eaea3SApple OSS Distributions        if owner:
1668*5e3eaea3SApple OSS Distributions            s += "unfair lock %x owned by thread %s" % (context, ownerThread)
1669*5e3eaea3SApple OSS Distributions        else:
1670*5e3eaea3SApple OSS Distributions            s += "spin lock %x" % context
1671*5e3eaea3SApple OSS Distributions    elif type == kThreadWaitPThreadMutex:
1672*5e3eaea3SApple OSS Distributions        s += "pthread mutex %x" % context
1673*5e3eaea3SApple OSS Distributions        if owner:
1674*5e3eaea3SApple OSS Distributions            s += " owned by thread %s" % ownerThread
1675*5e3eaea3SApple OSS Distributions        else:
1676*5e3eaea3SApple OSS Distributions            s += " with unknown owner"
1677*5e3eaea3SApple OSS Distributions    elif type == kThreadWaitPThreadRWLockRead:
1678*5e3eaea3SApple OSS Distributions        s += "pthread rwlock %x for reading" % context
1679*5e3eaea3SApple OSS Distributions    elif type == kThreadWaitPThreadRWLockWrite:
1680*5e3eaea3SApple OSS Distributions        s += "pthread rwlock %x for writing" % context
1681*5e3eaea3SApple OSS Distributions    elif type == kThreadWaitPThreadCondVar:
1682*5e3eaea3SApple OSS Distributions        s += "pthread condvar %x" % context
1683*5e3eaea3SApple OSS Distributions    elif type == kThreadWaitWorkloopSyncWait:
1684*5e3eaea3SApple OSS Distributions        s += "workloop sync wait"
1685*5e3eaea3SApple OSS Distributions        if owner == STACKSHOT_WAITOWNER_SUSPENDED:
1686*5e3eaea3SApple OSS Distributions            s += ", suspended"
1687*5e3eaea3SApple OSS Distributions        elif owner == STACKSHOT_WAITOWNER_THREQUESTED:
1688*5e3eaea3SApple OSS Distributions            s += ", thread requested"
1689*5e3eaea3SApple OSS Distributions        elif owner != 0:
1690*5e3eaea3SApple OSS Distributions            s += ", owned by thread %s" % ownerThread
1691*5e3eaea3SApple OSS Distributions        else:
1692*5e3eaea3SApple OSS Distributions            s += ", unknown owner"
1693*5e3eaea3SApple OSS Distributions        s += ", workloop id %x" % context
1694*5e3eaea3SApple OSS Distributions    elif type == kThreadWaitOnProcess:
1695*5e3eaea3SApple OSS Distributions        if owner == 2**64-1:
1696*5e3eaea3SApple OSS Distributions            s += "waitpid, for any children"
1697*5e3eaea3SApple OSS Distributions        elif 2**32 <= owner and owner < 2**64-1:
1698*5e3eaea3SApple OSS Distributions            s += "waitpid, for process group %d" % abs(owner - 2**64)
1699*5e3eaea3SApple OSS Distributions        else:
1700*5e3eaea3SApple OSS Distributions            s += "waitpid, for pid %d" % owner
1701*5e3eaea3SApple OSS Distributions    elif type == kThreadWaitSleepWithInheritor:
1702*5e3eaea3SApple OSS Distributions        if owner == 0:
1703*5e3eaea3SApple OSS Distributions            s += "turnstile, held waitq"
1704*5e3eaea3SApple OSS Distributions        else:
1705*5e3eaea3SApple OSS Distributions            s += "turnstile, pushing thread %s" % ownerThread
1706*5e3eaea3SApple OSS Distributions    elif type == kThreadWaitEventlink:
1707*5e3eaea3SApple OSS Distributions        if owner == 0:
1708*5e3eaea3SApple OSS Distributions            s += "eventlink, held waitq"
1709*5e3eaea3SApple OSS Distributions        else:
1710*5e3eaea3SApple OSS Distributions            s += "eventlink, signaled by thread %s" % ownerThread
1711*5e3eaea3SApple OSS Distributions    elif type == kThreadWaitCompressor:
1712*5e3eaea3SApple OSS Distributions        s += "in compressor segment %x, busy for thread %s" % (context, ownerThread)
1713*5e3eaea3SApple OSS Distributions
1714*5e3eaea3SApple OSS Distributions    else:
1715*5e3eaea3SApple OSS Distributions        s += "unknown type %d (owner %s, context %x)" % (type, ownerThread, context)
1716*5e3eaea3SApple OSS Distributions
1717*5e3eaea3SApple OSS Distributions    s += formatPortLabelID(portlabel_id, portlabels)
1718*5e3eaea3SApple OSS Distributions
1719*5e3eaea3SApple OSS Distributions    if flags != 0:
1720*5e3eaea3SApple OSS Distributions        s += "flags {}".format(hex(flags))
1721*5e3eaea3SApple OSS Distributions    return s
1722*5e3eaea3SApple OSS Distributions
1723*5e3eaea3SApple OSS Distributionsdef formatTurnstileInfo(ti, wi_portlabel_id, portlabels):
1724*5e3eaea3SApple OSS Distributions    if ti is None:
1725*5e3eaea3SApple OSS Distributions        return " [no turnstile]"
1726*5e3eaea3SApple OSS Distributions
1727*5e3eaea3SApple OSS Distributions    ts_flags = int(ti['turnstile_flags'])
1728*5e3eaea3SApple OSS Distributions    ctx = int(ti['turnstile_context'])
1729*5e3eaea3SApple OSS Distributions    hop = int(ti['number_of_hops'])
1730*5e3eaea3SApple OSS Distributions    prio = int(ti['turnstile_priority'])
1731*5e3eaea3SApple OSS Distributions    portlabel_id = ti.get("portlabel_id", 0)
1732*5e3eaea3SApple OSS Distributions
1733*5e3eaea3SApple OSS Distributions    portlabel_summary = ""
1734*5e3eaea3SApple OSS Distributions    if portlabel_id != 0 and portlabel_id != wi_portlabel_id:
1735*5e3eaea3SApple OSS Distributions        portlabel_summary += formatPortLabelID(portlabel_id, portlabels)
1736*5e3eaea3SApple OSS Distributions
1737*5e3eaea3SApple OSS Distributions    if ts_flags & STACKSHOT_TURNSTILE_STATUS_HELD_IPLOCK:
1738*5e3eaea3SApple OSS Distributions        return " [turnstile blocked on task, but ip_lock was held]" + portlabel_summary
1739*5e3eaea3SApple OSS Distributions    if ts_flags & STACKSHOT_TURNSTILE_STATUS_BLOCKED_ON_TASK:
1740*5e3eaea3SApple OSS Distributions        return " [turnstile blocked on task pid %d, hops: %d, priority: %d]%s" % (ctx, hop, prio, portlabel_summary)
1741*5e3eaea3SApple OSS Distributions    if ts_flags & STACKSHOT_TURNSTILE_STATUS_LOCKED_WAITQ:
1742*5e3eaea3SApple OSS Distributions        return " [turnstile was in process of being updated]" + portlabel_summary
1743*5e3eaea3SApple OSS Distributions    if ts_flags & STACKSHOT_TURNSTILE_STATUS_WORKQUEUE:
1744*5e3eaea3SApple OSS Distributions        return " [blocked on workqueue: 0x%x, hops: %x, priority: %d]%s" % (ctx, hop, prio, portlabel_summary)
1745*5e3eaea3SApple OSS Distributions    if ts_flags & STACKSHOT_TURNSTILE_STATUS_THREAD:
1746*5e3eaea3SApple OSS Distributions        return " [blocked on: %d, hops: %x, priority: %d]%s" % (ctx, hop, prio, portlabel_summary)
1747*5e3eaea3SApple OSS Distributions    if ts_flags & STACKSHOT_TURNSTILE_STATUS_UNKNOWN:
1748*5e3eaea3SApple OSS Distributions        return " [turnstile with unknown inheritor]" + portlabel_summary
1749*5e3eaea3SApple OSS Distributions
1750*5e3eaea3SApple OSS Distributions    return " [unknown turnstile status!]" + portlabel_summary
1751*5e3eaea3SApple OSS Distributions
1752*5e3eaea3SApple OSS Distributionsdef formatWaitInfoWithTurnstiles(waitinfos, tsinfos, portlabels):
1753*5e3eaea3SApple OSS Distributions    wis_tis = []
1754*5e3eaea3SApple OSS Distributions    for w in waitinfos:
1755*5e3eaea3SApple OSS Distributions        found_pair = False
1756*5e3eaea3SApple OSS Distributions        for t in tsinfos:
1757*5e3eaea3SApple OSS Distributions            if int(w['waiter']) == int(t['waiter']):
1758*5e3eaea3SApple OSS Distributions                wis_tis.append((w, t))
1759*5e3eaea3SApple OSS Distributions                found_pair = True
1760*5e3eaea3SApple OSS Distributions                break
1761*5e3eaea3SApple OSS Distributions        if not found_pair:
1762*5e3eaea3SApple OSS Distributions            wis_tis.append((w, None))
1763*5e3eaea3SApple OSS Distributions
1764*5e3eaea3SApple OSS Distributions    return [formatWaitInfo(wi, False, portlabels) + formatTurnstileInfo(ti, wi.get('portlabel_id', 0), portlabels) for (wi, ti) in wis_tis]
1765*5e3eaea3SApple OSS Distributions
1766*5e3eaea3SApple OSS Distributionsdef SaveStackshotReport(j, outfile_name, incomplete):
1767*5e3eaea3SApple OSS Distributions    import time
1768*5e3eaea3SApple OSS Distributions    from operator import itemgetter, attrgetter
1769*5e3eaea3SApple OSS Distributions    ss = j.get('kcdata_stackshot')
1770*5e3eaea3SApple OSS Distributions    if not ss:
1771*5e3eaea3SApple OSS Distributions        print("No KCDATA_BUFFER_BEGIN_STACKSHOT object found. Skipping writing report.")
1772*5e3eaea3SApple OSS Distributions        return
1773*5e3eaea3SApple OSS Distributions
1774*5e3eaea3SApple OSS Distributions    timestamp = ss.get('usecs_since_epoch')
1775*5e3eaea3SApple OSS Distributions    try:
1776*5e3eaea3SApple OSS Distributions        timestamp = time.strftime("%Y-%m-%d %H:%M:%S +0000",time.gmtime(timestamp // 1000000 if timestamp else None))
1777*5e3eaea3SApple OSS Distributions    except ValueError as e:
1778*5e3eaea3SApple OSS Distributions        print("couldn't convert timestamp:", str(e))
1779*5e3eaea3SApple OSS Distributions        timestamp = None
1780*5e3eaea3SApple OSS Distributions
1781*5e3eaea3SApple OSS Distributions    os_version = ss.get('osversion', 'Unknown')
1782*5e3eaea3SApple OSS Distributions    timebase = ss.get('mach_timebase_info', {"denom": 1, "numer": 1})
1783*5e3eaea3SApple OSS Distributions
1784*5e3eaea3SApple OSS Distributions    sc_note = None
1785*5e3eaea3SApple OSS Distributions    extra_note = None
1786*5e3eaea3SApple OSS Distributions    dsc_common = None
1787*5e3eaea3SApple OSS Distributions    shared_cache_info = ss.get('shared_cache_dyld_load_info')
1788*5e3eaea3SApple OSS Distributions    if shared_cache_info:
1789*5e3eaea3SApple OSS Distributions        shared_cache_base_addr = shared_cache_info['imageSlidBaseAddress']
1790*5e3eaea3SApple OSS Distributions        # If we have a slidFirstMapping and it's >= base_address, use that.
1791*5e3eaea3SApple OSS Distributions        #
1792*5e3eaea3SApple OSS Distributions        # Otherwise we're processing a stackshot from before the slidFirstMapping
1793*5e3eaea3SApple OSS Distributions        # field was introduced and corrected.  On ARM the SlidBaseAddress is the
1794*5e3eaea3SApple OSS Distributions        # same, but on x86 it's off by 0x20000000.  We use 'X86_64' in the
1795*5e3eaea3SApple OSS Distributions        # kernel version string plus checking kern_page_size == 4k' as
1796*5e3eaea3SApple OSS Distributions        # proxy for x86_64, and only adjust SlidBaseAddress if the unslid
1797*5e3eaea3SApple OSS Distributions        # address is precisely the expected incorrect value.
1798*5e3eaea3SApple OSS Distributions        #
1799*5e3eaea3SApple OSS Distributions        is_intel = ('X86_64' in ss.get('osversion', "") and
1800*5e3eaea3SApple OSS Distributions           ss.get('kernel_page_size', 0) == 4096)
1801*5e3eaea3SApple OSS Distributions        slidFirstMapping = shared_cache_info.get(SC_SLID_FIRSTMAPPING_KEY, -1);
1802*5e3eaea3SApple OSS Distributions        if slidFirstMapping >= shared_cache_base_addr:
1803*5e3eaea3SApple OSS Distributions            shared_cache_base_addr = slidFirstMapping
1804*5e3eaea3SApple OSS Distributions            sc_note = "base-accurate"
1805*5e3eaea3SApple OSS Distributions
1806*5e3eaea3SApple OSS Distributions        elif is_intel:
1807*5e3eaea3SApple OSS Distributions            sc_slide = shared_cache_info['imageLoadAddress']
1808*5e3eaea3SApple OSS Distributions            if (shared_cache_base_addr - sc_slide) == 0x7fff00000000:
1809*5e3eaea3SApple OSS Distributions                shared_cache_base_addr += 0x20000000
1810*5e3eaea3SApple OSS Distributions                sc_note = "base-x86-adjusted"
1811*5e3eaea3SApple OSS Distributions                extra_note = "Shared cache base adjusted for x86. "
1812*5e3eaea3SApple OSS Distributions            else:
1813*5e3eaea3SApple OSS Distributions                sc_note = "base-x86-unknown"
1814*5e3eaea3SApple OSS Distributions
1815*5e3eaea3SApple OSS Distributions        dsc_common = [format_uuid(shared_cache_info['imageUUID']),
1816*5e3eaea3SApple OSS Distributions                shared_cache_base_addr, "S" ]
1817*5e3eaea3SApple OSS Distributions        print("Shared cache UUID found from the binary data is <%s> " % str(dsc_common[0]))
1818*5e3eaea3SApple OSS Distributions
1819*5e3eaea3SApple OSS Distributions    dsc_layout = ss.get('system_shared_cache_layout')
1820*5e3eaea3SApple OSS Distributions
1821*5e3eaea3SApple OSS Distributions    dsc_libs = []
1822*5e3eaea3SApple OSS Distributions    if dsc_layout:
1823*5e3eaea3SApple OSS Distributions        print("Found in memory system shared cache layout with {} images".format(len(dsc_layout)))
1824*5e3eaea3SApple OSS Distributions        slide = ss.get('shared_cache_dyld_load_info')['imageLoadAddress']
1825*5e3eaea3SApple OSS Distributions
1826*5e3eaea3SApple OSS Distributions        for image in dsc_layout:
1827*5e3eaea3SApple OSS Distributions            dsc_libs.append([format_uuid(image['imageUUID']), image['imageLoadAddress'] + slide, "C"])
1828*5e3eaea3SApple OSS Distributions
1829*5e3eaea3SApple OSS Distributions    AllImageCatalog = []
1830*5e3eaea3SApple OSS Distributions    obj = {}
1831*5e3eaea3SApple OSS Distributions    obj["kernel"] = os_version
1832*5e3eaea3SApple OSS Distributions    if timestamp is not None:
1833*5e3eaea3SApple OSS Distributions        obj["date"] = timestamp
1834*5e3eaea3SApple OSS Distributions    obj["reason"] = "kernel panic stackshot"
1835*5e3eaea3SApple OSS Distributions    obj["incident"] = "ABCDEFGH-1234-56IJ-789K-0LMNOPQRSTUV"
1836*5e3eaea3SApple OSS Distributions    obj["crashReporterKey"] = "12ab34cd45aabbccdd6712ab34cd45aabbccdd67"
1837*5e3eaea3SApple OSS Distributions    obj["bootArgs"] = ss.get('boot_args','')
1838*5e3eaea3SApple OSS Distributions    obj["frontmostPids"] = [0]
1839*5e3eaea3SApple OSS Distributions    obj["exception"] = "0xDEADF157"
1840*5e3eaea3SApple OSS Distributions    obj["processByPid"] = {}
1841*5e3eaea3SApple OSS Distributions    if sc_note is not None:
1842*5e3eaea3SApple OSS Distributions        obj["sharedCacheNote"] = sc_note
1843*5e3eaea3SApple OSS Distributions
1844*5e3eaea3SApple OSS Distributions    if incomplete:
1845*5e3eaea3SApple OSS Distributions        obj["reason"] = "!!!INCOMPLETE!!! kernel panic stackshot"
1846*5e3eaea3SApple OSS Distributions        obj["notes"] = "Generated by xnu kcdata.py from incomplete data!   Some information is missing! "
1847*5e3eaea3SApple OSS Distributions    else:
1848*5e3eaea3SApple OSS Distributions        obj["notes"] = "Generated by xnu kcdata.py. "
1849*5e3eaea3SApple OSS Distributions
1850*5e3eaea3SApple OSS Distributions    if extra_note is not None:
1851*5e3eaea3SApple OSS Distributions        obj["notes"] = obj["notes"] + extra_note
1852*5e3eaea3SApple OSS Distributions
1853*5e3eaea3SApple OSS Distributions    processByPid = obj["processByPid"]
1854*5e3eaea3SApple OSS Distributions    ssplist = ss.get('task_snapshots', {})
1855*5e3eaea3SApple OSS Distributions    ssplist.update(ss.get('transitioning_task_snapshots', {}))
1856*5e3eaea3SApple OSS Distributions    kern_load_info = []
1857*5e3eaea3SApple OSS Distributions    if "0" in ssplist:
1858*5e3eaea3SApple OSS Distributions        kc_uuid = ssplist["0"].get('kernelcache_load_info', None)
1859*5e3eaea3SApple OSS Distributions        if kc_uuid:
1860*5e3eaea3SApple OSS Distributions            kernelcache_uuid = [format_uuid(kc_uuid['imageUUID']), kc_uuid['imageLoadAddress'], "U" ]
1861*5e3eaea3SApple OSS Distributions            kern_load_info.append(kernelcache_uuid)
1862*5e3eaea3SApple OSS Distributions
1863*5e3eaea3SApple OSS Distributions        kl_infos = ssplist["0"].get("dyld_load_info", [])
1864*5e3eaea3SApple OSS Distributions        for dlinfo in kl_infos:
1865*5e3eaea3SApple OSS Distributions            kern_load_info.append([format_uuid(dlinfo['imageUUID']), dlinfo['imageLoadAddress'], "K"])
1866*5e3eaea3SApple OSS Distributions
1867*5e3eaea3SApple OSS Distributions        kl_infos_text_exec = ssplist["0"].get("dyld_load_info_text_exec", [])
1868*5e3eaea3SApple OSS Distributions        for dlinfo in kl_infos_text_exec:
1869*5e3eaea3SApple OSS Distributions            kern_load_info.append([format_uuid(dlinfo['imageUUID']), dlinfo['imageLoadAddress'], "T"])
1870*5e3eaea3SApple OSS Distributions
1871*5e3eaea3SApple OSS Distributions    for pid,piddata in sorted(ssplist.items()):
1872*5e3eaea3SApple OSS Distributions        processByPid[str(pid)] = {}
1873*5e3eaea3SApple OSS Distributions        tsnap = processByPid[str(pid)]
1874*5e3eaea3SApple OSS Distributions        pr_lib_dsc = dsc_common
1875*5e3eaea3SApple OSS Distributions
1876*5e3eaea3SApple OSS Distributions        # see if there's an alternate shared cache
1877*5e3eaea3SApple OSS Distributions        scd = piddata.get('shared_cache_dyld_load_info')
1878*5e3eaea3SApple OSS Distributions        if scd is not None:
1879*5e3eaea3SApple OSS Distributions            if 'imageSlidBaseAddress' not in scd:
1880*5e3eaea3SApple OSS Distributions                print("Specific task shared cache format does not include slid shared cache base address. Skipping writing report.")
1881*5e3eaea3SApple OSS Distributions                return
1882*5e3eaea3SApple OSS Distributions
1883*5e3eaea3SApple OSS Distributions            scd_uuid = format_uuid(scd['imageUUID'])
1884*5e3eaea3SApple OSS Distributions            scd_base_addr = scd['imageSlidBaseAddress']
1885*5e3eaea3SApple OSS Distributions            pr_lib_dsc = [scd_uuid, scd_base_addr, "S"]
1886*5e3eaea3SApple OSS Distributions
1887*5e3eaea3SApple OSS Distributions        pr_libs = []
1888*5e3eaea3SApple OSS Distributions        if len(dsc_libs) == 0 and pr_lib_dsc:
1889*5e3eaea3SApple OSS Distributions            pr_libs.append(pr_lib_dsc)
1890*5e3eaea3SApple OSS Distributions        _lib_type = "P"
1891*5e3eaea3SApple OSS Distributions        if int(pid) == 0:
1892*5e3eaea3SApple OSS Distributions            _lib_type = "K"
1893*5e3eaea3SApple OSS Distributions            pr_libs = []
1894*5e3eaea3SApple OSS Distributions        else:
1895*5e3eaea3SApple OSS Distributions            for dlinfo in piddata.get('dyld_load_info',[]):
1896*5e3eaea3SApple OSS Distributions                pr_libs.append([format_uuid(dlinfo['imageUUID']), dlinfo['imageLoadAddress'], _lib_type])
1897*5e3eaea3SApple OSS Distributions
1898*5e3eaea3SApple OSS Distributions        pr_libs.extend(kern_load_info)
1899*5e3eaea3SApple OSS Distributions        pr_libs.extend(dsc_libs)
1900*5e3eaea3SApple OSS Distributions
1901*5e3eaea3SApple OSS Distributions        pr_libs.sort(key=itemgetter(1))
1902*5e3eaea3SApple OSS Distributions
1903*5e3eaea3SApple OSS Distributions        ttsnap = piddata.get('transitioning_task_snapshot', None)
1904*5e3eaea3SApple OSS Distributions        if ttsnap is not None:
1905*5e3eaea3SApple OSS Distributions            # Transitioning task snapshots have "tts_" prefixes; change them to
1906*5e3eaea3SApple OSS Distributions            # "ts_".
1907*5e3eaea3SApple OSS Distributions            ttsnap = { key[1:] : value for key,value in ttsnap.items() }
1908*5e3eaea3SApple OSS Distributions            # Add a note to let people know
1909*5e3eaea3SApple OSS Distributions            obj["notes"] = obj["notes"] + "PID {} is a transitioning (exiting) task. ".format(pid)
1910*5e3eaea3SApple OSS Distributions        tasksnap = piddata.get('task_snapshot', ttsnap);
1911*5e3eaea3SApple OSS Distributions        if tasksnap is None:
1912*5e3eaea3SApple OSS Distributions            continue;
1913*5e3eaea3SApple OSS Distributions        tsnap["pid"] = tasksnap["ts_pid"]
1914*5e3eaea3SApple OSS Distributions        if 'ts_asid' in piddata:
1915*5e3eaea3SApple OSS Distributions            tsnap["asid"] = piddata["ts_asid"]
1916*5e3eaea3SApple OSS Distributions
1917*5e3eaea3SApple OSS Distributions        if 'ts_pagetable' in piddata:
1918*5e3eaea3SApple OSS Distributions            pagetables = []
1919*5e3eaea3SApple OSS Distributions            for tte in piddata["ts_pagetable"]:
1920*5e3eaea3SApple OSS Distributions                pagetables.append(tte)
1921*5e3eaea3SApple OSS Distributions            tsnap["pageTables"] = pagetables
1922*5e3eaea3SApple OSS Distributions
1923*5e3eaea3SApple OSS Distributions        # Some fields are missing from transitioning_task snapshots.
1924*5e3eaea3SApple OSS Distributions        if ttsnap is None:
1925*5e3eaea3SApple OSS Distributions            tsnap["residentMemoryBytes"] = tasksnap["ts_task_size"]
1926*5e3eaea3SApple OSS Distributions            tsnap["timesDidThrottle"] = tasksnap["ts_did_throttle"]
1927*5e3eaea3SApple OSS Distributions            tsnap["systemTimeTask"] = GetSecondsFromMATime(tasksnap["ts_system_time_in_terminated_th"], timebase)
1928*5e3eaea3SApple OSS Distributions            tsnap["pageIns"] = tasksnap["ts_pageins"]
1929*5e3eaea3SApple OSS Distributions            tsnap["pageFaults"] = tasksnap["ts_faults"]
1930*5e3eaea3SApple OSS Distributions            tsnap["userTimeTask"] = GetSecondsFromMATime(tasksnap["ts_user_time_in_terminated_thre"], timebase)
1931*5e3eaea3SApple OSS Distributions        tsnap["procname"] = tasksnap["ts_p_comm"]
1932*5e3eaea3SApple OSS Distributions        if ttsnap is None:
1933*5e3eaea3SApple OSS Distributions            tsnap["copyOnWriteFaults"] = tasksnap["ts_cow_faults"]
1934*5e3eaea3SApple OSS Distributions            tsnap["timesThrottled"] = tasksnap["ts_was_throttled"]
1935*5e3eaea3SApple OSS Distributions        tsnap["threadById"] = {}
1936*5e3eaea3SApple OSS Distributions        threadByID = tsnap["threadById"]
1937*5e3eaea3SApple OSS Distributions        thlist = piddata.get('thread_snapshots', {})
1938*5e3eaea3SApple OSS Distributions        for tid,thdata in sorted(thlist.items()):
1939*5e3eaea3SApple OSS Distributions            threadByID[str(tid)] = {}
1940*5e3eaea3SApple OSS Distributions            thsnap = threadByID[str(tid)]
1941*5e3eaea3SApple OSS Distributions            if "thread_snapshot" not in thdata:
1942*5e3eaea3SApple OSS Distributions                print("Found broken thread state for thread ID: %s." % tid)
1943*5e3eaea3SApple OSS Distributions                break
1944*5e3eaea3SApple OSS Distributions            threadsnap = thdata["thread_snapshot"]
1945*5e3eaea3SApple OSS Distributions            thsnap["userTime"] = GetSecondsFromMATime(threadsnap["ths_user_time"], timebase)
1946*5e3eaea3SApple OSS Distributions            thsnap["id"] = threadsnap["ths_thread_id"]
1947*5e3eaea3SApple OSS Distributions            thsnap["basePriority"] = threadsnap["ths_base_priority"]
1948*5e3eaea3SApple OSS Distributions            thsnap["systemTime"] = GetSecondsFromMATime(threadsnap["ths_sys_time"], timebase)
1949*5e3eaea3SApple OSS Distributions            thsnap["schedPriority"] = threadsnap["ths_sched_priority"]
1950*5e3eaea3SApple OSS Distributions            thsnap["state"] = GetStateDescription(threadsnap['ths_state'])
1951*5e3eaea3SApple OSS Distributions            thsnap["qosEffective"] = threadsnap["ths_eqos"]
1952*5e3eaea3SApple OSS Distributions            thsnap["qosRequested"] = threadsnap["ths_rqos"]
1953*5e3eaea3SApple OSS Distributions
1954*5e3eaea3SApple OSS Distributions            if "pth_name" in thdata:
1955*5e3eaea3SApple OSS Distributions                thsnap["name"] = thdata["pth_name"];
1956*5e3eaea3SApple OSS Distributions
1957*5e3eaea3SApple OSS Distributions            if threadsnap['ths_continuation']:
1958*5e3eaea3SApple OSS Distributions                thsnap["continuation"] = GetSymbolInfoForFrame(AllImageCatalog, pr_libs, threadsnap['ths_continuation'])
1959*5e3eaea3SApple OSS Distributions            if "kernel_stack_frames" in thdata:
1960*5e3eaea3SApple OSS Distributions                kuserframes = []
1961*5e3eaea3SApple OSS Distributions                for f in thdata["kernel_stack_frames"]:
1962*5e3eaea3SApple OSS Distributions                    kuserframes.append(GetSymbolInfoForFrame(AllImageCatalog, pr_libs, f['lr']))
1963*5e3eaea3SApple OSS Distributions                thsnap["kernelFrames"] = kuserframes
1964*5e3eaea3SApple OSS Distributions
1965*5e3eaea3SApple OSS Distributions            if "user_stack_frames" in thdata:
1966*5e3eaea3SApple OSS Distributions                uframes = []
1967*5e3eaea3SApple OSS Distributions                for f in thdata["user_stack_frames"]:
1968*5e3eaea3SApple OSS Distributions                    uframes.append(GetSymbolInfoForFrame(AllImageCatalog, pr_libs, f['lr']))
1969*5e3eaea3SApple OSS Distributions                thsnap["userFrames"] = uframes
1970*5e3eaea3SApple OSS Distributions
1971*5e3eaea3SApple OSS Distributions            if "user_stacktop" in thdata:
1972*5e3eaea3SApple OSS Distributions                (address,) = struct.unpack("<Q", struct.pack("B"*8, *thdata["user_stacktop"]["stack_contents"]))
1973*5e3eaea3SApple OSS Distributions                thsnap["userStacktop"] = GetSymbolInfoForFrame(AllImageCatalog, pr_libs, address)
1974*5e3eaea3SApple OSS Distributions
1975*5e3eaea3SApple OSS Distributions            if threadsnap['ths_wait_event']:
1976*5e3eaea3SApple OSS Distributions                thsnap["waitEvent"] = GetSymbolInfoForFrame(AllImageCatalog, pr_libs, threadsnap['ths_wait_event'])
1977*5e3eaea3SApple OSS Distributions
1978*5e3eaea3SApple OSS Distributions        if 'thread_waitinfo' in piddata and 'thread_turnstileinfo' in piddata:
1979*5e3eaea3SApple OSS Distributions            tsnap['waitInfo'] = formatWaitInfoWithTurnstiles(piddata['thread_waitinfo'], piddata['thread_turnstileinfo'], piddata.get('portlabels', None))
1980*5e3eaea3SApple OSS Distributions        elif 'thread_waitinfo' in piddata:
1981*5e3eaea3SApple OSS Distributions            portlabels = ss.get('portlabels', None)
1982*5e3eaea3SApple OSS Distributions            tsnap['waitInfo'] = [formatWaitInfo(x, False, portlabels) for x in piddata['thread_waitinfo']]
1983*5e3eaea3SApple OSS Distributions        if 'stackshot_task_codesigning_info' in piddata:
1984*5e3eaea3SApple OSS Distributions            csinfo = piddata.get('stackshot_task_codesigning_info', {})
1985*5e3eaea3SApple OSS Distributions            tsnap['csflags'] = csinfo['csflags']
1986*5e3eaea3SApple OSS Distributions            tsnap['cs_trust_level'] = csinfo['cs_trust_level']
1987*5e3eaea3SApple OSS Distributions        if 'suspension_info' in piddata:
1988*5e3eaea3SApple OSS Distributions            suspinfo = piddata.get('suspension_info', {})
1989*5e3eaea3SApple OSS Distributions            tsnap['suspension_count'] = suspinfo['tss_count']
1990*5e3eaea3SApple OSS Distributions            tsnap['suspension_duration_secs'] = GetSecondsFromMATime(suspinfo['tss_duration'], timebase)
1991*5e3eaea3SApple OSS Distributions            tsnap['suspension_last_start'] = GetSecondsFromMATime(suspinfo['tss_last_start'], timebase)
1992*5e3eaea3SApple OSS Distributions            tsnap['suspension_last_end'] = GetSecondsFromMATime(suspinfo['tss_last_end'], timebase)
1993*5e3eaea3SApple OSS Distributions
1994*5e3eaea3SApple OSS Distributions            suspsources = piddata.get('suspension_source', [])
1995*5e3eaea3SApple OSS Distributions            suspension_sources = []
1996*5e3eaea3SApple OSS Distributions            for source in filter(lambda x: x['tss_time'] != 0, suspsources):
1997*5e3eaea3SApple OSS Distributions                suspension_sources.append({
1998*5e3eaea3SApple OSS Distributions                    'suspension_time': GetSecondsFromMATime(source['tss_time'], timebase),
1999*5e3eaea3SApple OSS Distributions                    'suspension_tid': source['tss_tid'],
2000*5e3eaea3SApple OSS Distributions                    'suspension_pid': source['tss_pid'],
2001*5e3eaea3SApple OSS Distributions                    'suspension_procname': source['tss_procname'],
2002*5e3eaea3SApple OSS Distributions                })
2003*5e3eaea3SApple OSS Distributions            tsnap['suspension_sources'] = suspension_sources
2004*5e3eaea3SApple OSS Distributions
2005*5e3eaea3SApple OSS Distributions            # check if process is currently suspended
2006*5e3eaea3SApple OSS Distributions            if tsnap['suspension_last_start'] > tsnap['suspension_last_end']:
2007*5e3eaea3SApple OSS Distributions                obj['notes'] += "\nPID {} ({}) is currently suspended (count: {}, total duration: {:.4f}s, last_start: {:.4f}, last_end: {:.4f}) - recent suspensions are:\n".format(pid, tsnap['procname'], tsnap['suspension_count'], tsnap['suspension_duration_secs'], tsnap['suspension_last_start'], tsnap['suspension_last_end'])
2008*5e3eaea3SApple OSS Distributions                for source in suspension_sources:
2009*5e3eaea3SApple OSS Distributions                    obj['notes'] += "From PID {} TID {} ({}) - at {}\n".format(source['suspension_pid'], source['suspension_tid'], source['suspension_procname'], source['suspension_time'])
2010*5e3eaea3SApple OSS Distributions
2011*5e3eaea3SApple OSS Distributions    obj['binaryImages'] = AllImageCatalog
2012*5e3eaea3SApple OSS Distributions    if outfile_name == '-':
2013*5e3eaea3SApple OSS Distributions        fh = sys.stdout
2014*5e3eaea3SApple OSS Distributions    else:
2015*5e3eaea3SApple OSS Distributions        fh = open(outfile_name, "w")
2016*5e3eaea3SApple OSS Distributions
2017*5e3eaea3SApple OSS Distributions    header = {}
2018*5e3eaea3SApple OSS Distributions    header['bug_type'] = 288
2019*5e3eaea3SApple OSS Distributions    if timestamp is not None:
2020*5e3eaea3SApple OSS Distributions        header['timestamp'] = timestamp
2021*5e3eaea3SApple OSS Distributions    header['os_version'] = os_version
2022*5e3eaea3SApple OSS Distributions    fh.write(json.dumps(header, sort_keys=True))
2023*5e3eaea3SApple OSS Distributions    fh.write("\n")
2024*5e3eaea3SApple OSS Distributions
2025*5e3eaea3SApple OSS Distributions    fh.write(json.dumps(obj, sort_keys=True, indent=2, separators=(',', ': ')))
2026*5e3eaea3SApple OSS Distributions    fh.close()
2027*5e3eaea3SApple OSS Distributions
2028*5e3eaea3SApple OSS Distributions
2029*5e3eaea3SApple OSS Distributions@contextlib.contextmanager
2030*5e3eaea3SApple OSS Distributionsdef data_from_stream(stream):
2031*5e3eaea3SApple OSS Distributions    try:
2032*5e3eaea3SApple OSS Distributions        fmap = mmap.mmap(stream.fileno(), 0, mmap.MAP_SHARED, mmap.PROT_READ)
2033*5e3eaea3SApple OSS Distributions    except:
2034*5e3eaea3SApple OSS Distributions        if six.PY3:
2035*5e3eaea3SApple OSS Distributions            yield stream.buffer.read()
2036*5e3eaea3SApple OSS Distributions        else:
2037*5e3eaea3SApple OSS Distributions            yield stream.read()
2038*5e3eaea3SApple OSS Distributions    else:
2039*5e3eaea3SApple OSS Distributions        try:
2040*5e3eaea3SApple OSS Distributions            yield fmap
2041*5e3eaea3SApple OSS Distributions        finally:
2042*5e3eaea3SApple OSS Distributions            fmap.close()
2043*5e3eaea3SApple OSS Distributions
2044*5e3eaea3SApple OSS Distributionsdef iterate_kcdatas(kcdata_file):
2045*5e3eaea3SApple OSS Distributions    with data_from_stream(kcdata_file) as data:
2046*5e3eaea3SApple OSS Distributions        iterator = kcdata_item_iterator(data)
2047*5e3eaea3SApple OSS Distributions        kcdata_buffer = KCObject.FromKCItem(six.next(iterator))
2048*5e3eaea3SApple OSS Distributions
2049*5e3eaea3SApple OSS Distributions        if isinstance(kcdata_buffer, KCCompressedBufferObject):
2050*5e3eaea3SApple OSS Distributions            kcdata_buffer.ReadItems(iterator)
2051*5e3eaea3SApple OSS Distributions            decompressed = kcdata_buffer.Decompress(data)
2052*5e3eaea3SApple OSS Distributions            iterator = kcdata_item_iterator(decompressed)
2053*5e3eaea3SApple OSS Distributions            kcdata_buffer = KCObject.FromKCItem(six.next(iterator))
2054*5e3eaea3SApple OSS Distributions
2055*5e3eaea3SApple OSS Distributions        if not isinstance(kcdata_buffer, KCBufferObject):
2056*5e3eaea3SApple OSS Distributions            # ktrace stackshot chunk
2057*5e3eaea3SApple OSS Distributions            iterator = kcdata_item_iterator(data[16:])
2058*5e3eaea3SApple OSS Distributions            kcdata_buffer = KCObject.FromKCItem(six.next(iterator))
2059*5e3eaea3SApple OSS Distributions
2060*5e3eaea3SApple OSS Distributions        if not isinstance(kcdata_buffer, KCBufferObject):
2061*5e3eaea3SApple OSS Distributions            try:
2062*5e3eaea3SApple OSS Distributions                decoded = base64.b64decode(data)
2063*5e3eaea3SApple OSS Distributions            except:
2064*5e3eaea3SApple OSS Distributions                pass
2065*5e3eaea3SApple OSS Distributions            else:
2066*5e3eaea3SApple OSS Distributions                iterator = kcdata_item_iterator(decoded)
2067*5e3eaea3SApple OSS Distributions                kcdata_buffer = KCObject.FromKCItem(six.next(iterator))
2068*5e3eaea3SApple OSS Distributions        if not isinstance(kcdata_buffer, KCBufferObject):
2069*5e3eaea3SApple OSS Distributions            import gzip
2070*5e3eaea3SApple OSS Distributions            from io import BytesIO
2071*5e3eaea3SApple OSS Distributions            try:
2072*5e3eaea3SApple OSS Distributions                decompressed = gzip.GzipFile(fileobj=BytesIO(data[:])).read()
2073*5e3eaea3SApple OSS Distributions            except:
2074*5e3eaea3SApple OSS Distributions                pass
2075*5e3eaea3SApple OSS Distributions            else:
2076*5e3eaea3SApple OSS Distributions                iterator = kcdata_item_iterator(decompressed)
2077*5e3eaea3SApple OSS Distributions                kcdata_buffer = KCObject.FromKCItem(six.next(iterator))
2078*5e3eaea3SApple OSS Distributions
2079*5e3eaea3SApple OSS Distributions        if not isinstance(kcdata_buffer, KCBufferObject):
2080*5e3eaea3SApple OSS Distributions            raise Exception("unknown file type")
2081*5e3eaea3SApple OSS Distributions
2082*5e3eaea3SApple OSS Distributions
2083*5e3eaea3SApple OSS Distributions        kcdata_buffer.ReadItems(iterator)
2084*5e3eaea3SApple OSS Distributions        yield kcdata_buffer
2085*5e3eaea3SApple OSS Distributions
2086*5e3eaea3SApple OSS Distributions        for magic in iterator:
2087*5e3eaea3SApple OSS Distributions            kcdata_buffer = KCObject.FromKCItem(magic)
2088*5e3eaea3SApple OSS Distributions            if kcdata_buffer.i_type == 0:
2089*5e3eaea3SApple OSS Distributions                continue
2090*5e3eaea3SApple OSS Distributions            if not isinstance(kcdata_buffer, KCBufferObject):
2091*5e3eaea3SApple OSS Distributions                raise Exception("unknown file type")
2092*5e3eaea3SApple OSS Distributions            kcdata_buffer.ReadItems(iterator)
2093*5e3eaea3SApple OSS Distributions            yield kcdata_buffer
2094*5e3eaea3SApple OSS Distributions
2095*5e3eaea3SApple OSS Distributions#
2096*5e3eaea3SApple OSS Distributions# Values for various flag fields.  Each entry's key is the key seen in the
2097*5e3eaea3SApple OSS Distributions# processed kcdata, the value is an array of bits, from low (0x1) to high, with
2098*5e3eaea3SApple OSS Distributions# either a string flag name or None for unused holes.
2099*5e3eaea3SApple OSS Distributions#
2100*5e3eaea3SApple OSS Distributions# Only put flags in here which are stable - this is run against stackshots
2101*5e3eaea3SApple OSS Distributions# of all different versions.  For anything unstable, we'll need a decoder ring
2102*5e3eaea3SApple OSS Distributions# added to the stackshot.
2103*5e3eaea3SApple OSS Distributions#
2104*5e3eaea3SApple OSS DistributionsPRETTIFY_FLAGS = {
2105*5e3eaea3SApple OSS Distributions    'jcs_flags': [
2106*5e3eaea3SApple OSS Distributions       'kCoalitionTermRequested',
2107*5e3eaea3SApple OSS Distributions       'kCoalitionTerminated',
2108*5e3eaea3SApple OSS Distributions       'kCoalitionReaped',
2109*5e3eaea3SApple OSS Distributions       'kCoalitionPrivileged',
2110*5e3eaea3SApple OSS Distributions    ],
2111*5e3eaea3SApple OSS Distributions    'sharedCacheFlags': [
2112*5e3eaea3SApple OSS Distributions       'kSharedCacheSystemPrimary',
2113*5e3eaea3SApple OSS Distributions       'kSharedCacheDriverkit'
2114*5e3eaea3SApple OSS Distributions       'kSharedCacheAOT',
2115*5e3eaea3SApple OSS Distributions    ],
2116*5e3eaea3SApple OSS Distributions    'stackshot_in_flags': [ # STACKSHOT_*, also stackshot_out_flags
2117*5e3eaea3SApple OSS Distributions        'get_dq',
2118*5e3eaea3SApple OSS Distributions        'save_loadinfo',
2119*5e3eaea3SApple OSS Distributions        'get_global_mem_stats',
2120*5e3eaea3SApple OSS Distributions        'save_kext_loadinfo',
2121*5e3eaea3SApple OSS Distributions        None,
2122*5e3eaea3SApple OSS Distributions        None,
2123*5e3eaea3SApple OSS Distributions        None,
2124*5e3eaea3SApple OSS Distributions        None,
2125*5e3eaea3SApple OSS Distributions        'active_kernel_threads_only',
2126*5e3eaea3SApple OSS Distributions        'get_boot_profile',
2127*5e3eaea3SApple OSS Distributions        'do_compress',
2128*5e3eaea3SApple OSS Distributions        None,
2129*5e3eaea3SApple OSS Distributions        None,
2130*5e3eaea3SApple OSS Distributions        'save_imp_donation_pids',
2131*5e3eaea3SApple OSS Distributions        'save_in_kernel_buffer',
2132*5e3eaea3SApple OSS Distributions        'retrieve_existing_buffer',
2133*5e3eaea3SApple OSS Distributions        'kcdata_format',
2134*5e3eaea3SApple OSS Distributions        'enable_bt_faulting',
2135*5e3eaea3SApple OSS Distributions        'collect_delta_snapshot',
2136*5e3eaea3SApple OSS Distributions        'collect_sharedcache_layout',
2137*5e3eaea3SApple OSS Distributions        'trylock',
2138*5e3eaea3SApple OSS Distributions        'enable_uuid_faulting',
2139*5e3eaea3SApple OSS Distributions        'from_panic',
2140*5e3eaea3SApple OSS Distributions        'no_io_stats',
2141*5e3eaea3SApple OSS Distributions        'thread_waitinfo',
2142*5e3eaea3SApple OSS Distributions        'thread_group',
2143*5e3eaea3SApple OSS Distributions        'save_jetsam_coalitions',
2144*5e3eaea3SApple OSS Distributions        'instrs_cycles',
2145*5e3eaea3SApple OSS Distributions        'asid',
2146*5e3eaea3SApple OSS Distributions        'page_tables',
2147*5e3eaea3SApple OSS Distributions        'disable_latency_info',
2148*5e3eaea3SApple OSS Distributions        'save_dyld_compactinfo',
2149*5e3eaea3SApple OSS Distributions        'include_driver_threads_in_kernel',
2150*5e3eaea3SApple OSS Distributions    ],
2151*5e3eaea3SApple OSS Distributions    'system_state_flags': [
2152*5e3eaea3SApple OSS Distributions        'kUser64_p',
2153*5e3eaea3SApple OSS Distributions        'kKern64_p',
2154*5e3eaea3SApple OSS Distributions    ],
2155*5e3eaea3SApple OSS Distributions    'tgs_flags': [
2156*5e3eaea3SApple OSS Distributions        'kThreadGroupEfficient',
2157*5e3eaea3SApple OSS Distributions        'kThreadGroupApplication',
2158*5e3eaea3SApple OSS Distributions        'kThreadGroupCritical',
2159*5e3eaea3SApple OSS Distributions        'kThreadGroupBestEffort',
2160*5e3eaea3SApple OSS Distributions        None,
2161*5e3eaea3SApple OSS Distributions        None,
2162*5e3eaea3SApple OSS Distributions        None,
2163*5e3eaea3SApple OSS Distributions        None,
2164*5e3eaea3SApple OSS Distributions        'kThreadGroupUIApplication',
2165*5e3eaea3SApple OSS Distributions        'kThreadGroupManaged',
2166*5e3eaea3SApple OSS Distributions        'kThreadGroupStrictTimers',
2167*5e3eaea3SApple OSS Distributions    ],
2168*5e3eaea3SApple OSS Distributions    'ths_ss_flags': [
2169*5e3eaea3SApple OSS Distributions        'kUser64_p',
2170*5e3eaea3SApple OSS Distributions        'kKern64_p',
2171*5e3eaea3SApple OSS Distributions        'kHasDispatchSerial',
2172*5e3eaea3SApple OSS Distributions        'kStacksPCOnly',
2173*5e3eaea3SApple OSS Distributions        'kThreadDarwinBG',
2174*5e3eaea3SApple OSS Distributions        'kThreadIOPassive',
2175*5e3eaea3SApple OSS Distributions        'kThreadSuspended',
2176*5e3eaea3SApple OSS Distributions        'kThreadTruncatedBT',
2177*5e3eaea3SApple OSS Distributions        'kGlobalForcedIdle',
2178*5e3eaea3SApple OSS Distributions        'kThreadFaultedBT',
2179*5e3eaea3SApple OSS Distributions        'kThreadTriedFaultBT',
2180*5e3eaea3SApple OSS Distributions        'kThreadOnCore',
2181*5e3eaea3SApple OSS Distributions        'kThreadIdleWorker',
2182*5e3eaea3SApple OSS Distributions        'kThreadMain',
2183*5e3eaea3SApple OSS Distributions        'kThreadTruncKernBT',
2184*5e3eaea3SApple OSS Distributions        'kThreadTruncUserBT',
2185*5e3eaea3SApple OSS Distributions        'kThreadTruncUserAsyncBT',
2186*5e3eaea3SApple OSS Distributions    ],
2187*5e3eaea3SApple OSS Distributions    'ths_state': [
2188*5e3eaea3SApple OSS Distributions        'TH_WAIT',
2189*5e3eaea3SApple OSS Distributions        'TH_SUSP',
2190*5e3eaea3SApple OSS Distributions        'TH_RUN',
2191*5e3eaea3SApple OSS Distributions        'TH_UNINT',
2192*5e3eaea3SApple OSS Distributions        'TH_TERMINATE',
2193*5e3eaea3SApple OSS Distributions        'TH_TERMINATE2',
2194*5e3eaea3SApple OSS Distributions        'TH_WAIT_REPORT',
2195*5e3eaea3SApple OSS Distributions        'TH_IDLE',
2196*5e3eaea3SApple OSS Distributions    ],
2197*5e3eaea3SApple OSS Distributions    'ts_ss_flags': [
2198*5e3eaea3SApple OSS Distributions        'kUser64_p',
2199*5e3eaea3SApple OSS Distributions        'kKern64_p',
2200*5e3eaea3SApple OSS Distributions        'kTaskRsrcFlagged',
2201*5e3eaea3SApple OSS Distributions        'kTerminatedSnapshot',
2202*5e3eaea3SApple OSS Distributions        'kPidSuspended',
2203*5e3eaea3SApple OSS Distributions        'kFrozen',
2204*5e3eaea3SApple OSS Distributions        'kTaskDarwinBG',
2205*5e3eaea3SApple OSS Distributions        'kTaskExtDarwinBG',
2206*5e3eaea3SApple OSS Distributions        'kTaskVisVisible',
2207*5e3eaea3SApple OSS Distributions        'kTaskVisNonvisible',
2208*5e3eaea3SApple OSS Distributions        'kTaskIsForeground',
2209*5e3eaea3SApple OSS Distributions        'kTaskIsBoosted',
2210*5e3eaea3SApple OSS Distributions        'kTaskIsSuppressed',
2211*5e3eaea3SApple OSS Distributions        'kTaskIsTimerThrottled',
2212*5e3eaea3SApple OSS Distributions        'kTaskIsImpDonor',
2213*5e3eaea3SApple OSS Distributions        'kTaskIsLiveImpDonor',
2214*5e3eaea3SApple OSS Distributions        'kTaskIsDirty',
2215*5e3eaea3SApple OSS Distributions        'kTaskWqExceededConstrainedThreadLimit',
2216*5e3eaea3SApple OSS Distributions        'kTaskWqExceededTotalThreadLimit',
2217*5e3eaea3SApple OSS Distributions        'kTaskWqFlagsAvailable',
2218*5e3eaea3SApple OSS Distributions        'kTaskUUIDInfoFaultedIn',
2219*5e3eaea3SApple OSS Distributions        'kTaskUUIDInfoMissing',
2220*5e3eaea3SApple OSS Distributions        'kTaskUUIDInfoTriedFault',
2221*5e3eaea3SApple OSS Distributions        'kTaskSharedRegionInfoUnavailable',
2222*5e3eaea3SApple OSS Distributions        'kTaskTALEngaged',
2223*5e3eaea3SApple OSS Distributions        None,
2224*5e3eaea3SApple OSS Distributions        'kTaskIsDirtyTracked',
2225*5e3eaea3SApple OSS Distributions        'kTaskAllowIdleExit',
2226*5e3eaea3SApple OSS Distributions        'kTaskIsTranslated',
2227*5e3eaea3SApple OSS Distributions        'kTaskSharedRegionNone',
2228*5e3eaea3SApple OSS Distributions        'kTaskSharedRegionSystem',
2229*5e3eaea3SApple OSS Distributions        'kTaskSharedRegionOther',
2230*5e3eaea3SApple OSS Distributions        'kTaskDyldCompactInfoNone',
2231*5e3eaea3SApple OSS Distributions        'kTaskDyldCompactInfoTooBig',
2232*5e3eaea3SApple OSS Distributions        'kTaskDyldCompactInfoFaultedIn',
2233*5e3eaea3SApple OSS Distributions        'kTaskDyldCompactInfoMissing',
2234*5e3eaea3SApple OSS Distributions        'kTaskDyldCompactInfoTriedFault',
2235*5e3eaea3SApple OSS Distributions    ],
2236*5e3eaea3SApple OSS Distributions    'turnstile_flags': [
2237*5e3eaea3SApple OSS Distributions        'turnstile_status_unknown',
2238*5e3eaea3SApple OSS Distributions        'turnstile_status_locked_waitq',
2239*5e3eaea3SApple OSS Distributions        'turnstile_status_workqueue',
2240*5e3eaea3SApple OSS Distributions        'turnstile_status_thread',
2241*5e3eaea3SApple OSS Distributions        'turnstile_status_blocked_on_task',
2242*5e3eaea3SApple OSS Distributions        'turnstile_status_held_iplock',
2243*5e3eaea3SApple OSS Distributions    ],
2244*5e3eaea3SApple OSS Distributions    'portlabel_flags': [
2245*5e3eaea3SApple OSS Distributions        'label_read_failed',
2246*5e3eaea3SApple OSS Distributions        'service_throttled',
2247*5e3eaea3SApple OSS Distributions    ]
2248*5e3eaea3SApple OSS Distributions}
2249*5e3eaea3SApple OSS DistributionsPRETTIFY_FLAGS['stackshot_out_flags'] = PRETTIFY_FLAGS['stackshot_in_flags']
2250*5e3eaea3SApple OSS DistributionsPRETTIFY_FLAGS['tts_ss_flags'] = PRETTIFY_FLAGS['ts_ss_flags']
2251*5e3eaea3SApple OSS Distributions
2252*5e3eaea3SApple OSS Distributions# Fields which should never be hexified
2253*5e3eaea3SApple OSS DistributionsPRETTIFY_DONTHEX = {
2254*5e3eaea3SApple OSS Distributions    'stackshot_in_pid': True,
2255*5e3eaea3SApple OSS Distributions    'tts_pid': True,
2256*5e3eaea3SApple OSS Distributions    'ts_pid': True,
2257*5e3eaea3SApple OSS Distributions    'donating_pids': True,
2258*5e3eaea3SApple OSS Distributions    'ppid': True,
2259*5e3eaea3SApple OSS Distributions}
2260*5e3eaea3SApple OSS Distributions
2261*5e3eaea3SApple OSS Distributions# Only hex() the value if it is multiple digits
2262*5e3eaea3SApple OSS Distributionsdef prettify_hex(v):
2263*5e3eaea3SApple OSS Distributions    if v < -9 or v > 9:
2264*5e3eaea3SApple OSS Distributions        return hex(v)
2265*5e3eaea3SApple OSS Distributions    return str(v)
2266*5e3eaea3SApple OSS Distributions
2267*5e3eaea3SApple OSS Distributionsdef prettify_flags(v, flags):
2268*5e3eaea3SApple OSS Distributions    output=""
2269*5e3eaea3SApple OSS Distributions    seen = 0
2270*5e3eaea3SApple OSS Distributions    if v == 0:
2271*5e3eaea3SApple OSS Distributions        return "0"
2272*5e3eaea3SApple OSS Distributions    for (s, n) in zip(range(len(flags)),flags):
2273*5e3eaea3SApple OSS Distributions        if n is None:
2274*5e3eaea3SApple OSS Distributions            continue
2275*5e3eaea3SApple OSS Distributions        if (v & (2 ** s)):
2276*5e3eaea3SApple OSS Distributions            output += "|" + n
2277*5e3eaea3SApple OSS Distributions            seen |= 2 ** s
2278*5e3eaea3SApple OSS Distributions    if output == "":
2279*5e3eaea3SApple OSS Distributions        return prettify_hex(v)
2280*5e3eaea3SApple OSS Distributions    rest = (v & ~seen)
2281*5e3eaea3SApple OSS Distributions    if (rest != 0):
2282*5e3eaea3SApple OSS Distributions        output += "|" + prettify_hex(rest)
2283*5e3eaea3SApple OSS Distributions    return prettify_hex(v) + " (" + output[1:] + ")"
2284*5e3eaea3SApple OSS Distributions
2285*5e3eaea3SApple OSS Distributionsdef prettify_core(data, mosthex, key, portlabels):
2286*5e3eaea3SApple OSS Distributions    if key == 'stack_contents':
2287*5e3eaea3SApple OSS Distributions        (address,) = struct.unpack("<Q", struct.pack("B"*8, *data))
2288*5e3eaea3SApple OSS Distributions        return '0x%X' % address
2289*5e3eaea3SApple OSS Distributions
2290*5e3eaea3SApple OSS Distributions    elif isinstance(data, list):
2291*5e3eaea3SApple OSS Distributions        if 'uuid' in key.lower() and len(data) == 16:
2292*5e3eaea3SApple OSS Distributions            return '%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X' % tuple(data)
2293*5e3eaea3SApple OSS Distributions
2294*5e3eaea3SApple OSS Distributions        return [prettify_core(x, mosthex, key, portlabels) for x in data]
2295*5e3eaea3SApple OSS Distributions
2296*5e3eaea3SApple OSS Distributions    elif key == 'thread_waitinfo':
2297*5e3eaea3SApple OSS Distributions        return formatWaitInfo(data, mosthex, portlabels)
2298*5e3eaea3SApple OSS Distributions
2299*5e3eaea3SApple OSS Distributions    elif isinstance(data, dict):
2300*5e3eaea3SApple OSS Distributions        if 'portlabels' in data:
2301*5e3eaea3SApple OSS Distributions            portlabels = data['portlabels']
2302*5e3eaea3SApple OSS Distributions        newdata = dict()
2303*5e3eaea3SApple OSS Distributions        for key, value in data.items():
2304*5e3eaea3SApple OSS Distributions            if mosthex and key != 'task_snapshots' and len(key) > 0 and key.isnumeric():
2305*5e3eaea3SApple OSS Distributions                key = prettify_hex(int(key))
2306*5e3eaea3SApple OSS Distributions            newdata[key] = prettify_core(value, mosthex, key, portlabels)
2307*5e3eaea3SApple OSS Distributions        return newdata
2308*5e3eaea3SApple OSS Distributions
2309*5e3eaea3SApple OSS Distributions    elif 'address' in key.lower() and isinstance(data, (int, long)):
2310*5e3eaea3SApple OSS Distributions        return '0x%X' % data
2311*5e3eaea3SApple OSS Distributions    elif key == 'lr' or key == SC_SLID_FIRSTMAPPING_KEY:
2312*5e3eaea3SApple OSS Distributions        return '0x%X' % data
2313*5e3eaea3SApple OSS Distributions    elif key in PRETTIFY_FLAGS and isinstance(data, (int, long)):
2314*5e3eaea3SApple OSS Distributions        return prettify_flags(data, PRETTIFY_FLAGS[key])
2315*5e3eaea3SApple OSS Distributions    elif key.endswith('_flags') and isinstance(data, (int, long)):
2316*5e3eaea3SApple OSS Distributions        return prettify_hex(data)
2317*5e3eaea3SApple OSS Distributions
2318*5e3eaea3SApple OSS Distributions    elif mosthex and not PRETTIFY_DONTHEX.get(key, False):
2319*5e3eaea3SApple OSS Distributions        if isinstance(data, (int, long)):
2320*5e3eaea3SApple OSS Distributions            return prettify_hex(data)
2321*5e3eaea3SApple OSS Distributions        elif isinstance(data, six.string_types) and len(data) > 0 and data.isnumeric():
2322*5e3eaea3SApple OSS Distributions            return prettify_hex(int(data))
2323*5e3eaea3SApple OSS Distributions        return data
2324*5e3eaea3SApple OSS Distributions
2325*5e3eaea3SApple OSS Distributions    else:
2326*5e3eaea3SApple OSS Distributions        return data
2327*5e3eaea3SApple OSS Distributions
2328*5e3eaea3SApple OSS Distributionsdef prettify(data, mosthex):
2329*5e3eaea3SApple OSS Distributions    return prettify_core(data, mosthex, "", None)
2330*5e3eaea3SApple OSS Distributions
2331*5e3eaea3SApple OSS Distributions# N.B.: This is called directly from `xnu.py` for `panicdata -S XXX.ips`'s implementation.
2332*5e3eaea3SApple OSS Distributionsdef decode_kcdata_file(kcdata_file, stackshot_file, multiple=False, prettyhex=False, pretty=False, output_as_plist=False):
2333*5e3eaea3SApple OSS Distributions    for i,kcdata_buffer in enumerate(iterate_kcdatas(kcdata_file)):
2334*5e3eaea3SApple OSS Distributions        if i > 0 and not multiple:
2335*5e3eaea3SApple OSS Distributions            break
2336*5e3eaea3SApple OSS Distributions
2337*5e3eaea3SApple OSS Distributions        str_data = "{" + kcdata_buffer.GetJsonRepr() + "}"
2338*5e3eaea3SApple OSS Distributions        str_data = str_data.replace("\t", "    ")
2339*5e3eaea3SApple OSS Distributions
2340*5e3eaea3SApple OSS Distributions        try:
2341*5e3eaea3SApple OSS Distributions            json_obj = json.loads(str_data)
2342*5e3eaea3SApple OSS Distributions        except:
2343*5e3eaea3SApple OSS Distributions            print("JSON reparsing failed!  Printing string data!\n", file=sys.stderr)
2344*5e3eaea3SApple OSS Distributions            import textwrap
2345*5e3eaea3SApple OSS Distributions            print(textwrap.fill(str_data, 100))
2346*5e3eaea3SApple OSS Distributions            raise
2347*5e3eaea3SApple OSS Distributions
2348*5e3eaea3SApple OSS Distributions        if prettyhex:
2349*5e3eaea3SApple OSS Distributions            json_obj = prettify(json_obj, True)
2350*5e3eaea3SApple OSS Distributions        elif pretty:
2351*5e3eaea3SApple OSS Distributions            json_obj = prettify(json_obj, False)
2352*5e3eaea3SApple OSS Distributions
2353*5e3eaea3SApple OSS Distributions        if stackshot_file:
2354*5e3eaea3SApple OSS Distributions            SaveStackshotReport(json_obj, stackshot_file, G.data_was_incomplete)
2355*5e3eaea3SApple OSS Distributions        elif output_as_plist:
2356*5e3eaea3SApple OSS Distributions            import Foundation
2357*5e3eaea3SApple OSS Distributions            plist = Foundation.NSPropertyListSerialization.dataWithPropertyList_format_options_error_(
2358*5e3eaea3SApple OSS Distributions                json_obj, Foundation.NSPropertyListXMLFormat_v1_0, 0, None)[0].bytes().tobytes()
2359*5e3eaea3SApple OSS Distributions            #sigh.  on some pythons long integers are getting output with L's in the plist.
2360*5e3eaea3SApple OSS Distributions            plist = re.sub(r'^(\s*<integer>\d+)L(</integer>\s*)$', r"\1\2", BytesToString(plist), flags=re.MULTILINE)
2361*5e3eaea3SApple OSS Distributions            print(plist,)
2362*5e3eaea3SApple OSS Distributions        else:
2363*5e3eaea3SApple OSS Distributions            print(json.dumps(json_obj, sort_keys=True, indent=4, separators=(',', ': ')))
2364*5e3eaea3SApple OSS Distributions
2365*5e3eaea3SApple OSS Distributionsif __name__ == '__main__':
2366*5e3eaea3SApple OSS Distributions    parser = argparse.ArgumentParser(description="Decode a kcdata binary file.")
2367*5e3eaea3SApple OSS Distributions    parser.add_argument("-l", "--listtypes", action="store_true", required=False, default=False,
2368*5e3eaea3SApple OSS Distributions                        help="List all known types",
2369*5e3eaea3SApple OSS Distributions                        dest="list_known_types")
2370*5e3eaea3SApple OSS Distributions
2371*5e3eaea3SApple OSS Distributions    parser.add_argument("-s", "--stackshot", required=False, default=False,
2372*5e3eaea3SApple OSS Distributions                        help="Generate a stackshot report file",
2373*5e3eaea3SApple OSS Distributions                        dest="stackshot_file")
2374*5e3eaea3SApple OSS Distributions
2375*5e3eaea3SApple OSS Distributions    parser.add_argument("--multiple", help="look for multiple stackshots in a single file", action='store_true')
2376*5e3eaea3SApple OSS Distributions
2377*5e3eaea3SApple OSS Distributions    parser.add_argument("-p", "--plist", required=False, default=False,
2378*5e3eaea3SApple OSS Distributions                        help="output as plist", action="store_true")
2379*5e3eaea3SApple OSS Distributions
2380*5e3eaea3SApple OSS Distributions    parser.add_argument("-S", "--sdk", required=False, default="", help="sdk property passed to xcrun command to find the required tools. Default is empty string.", dest="sdk")
2381*5e3eaea3SApple OSS Distributions    parser.add_argument("-P", "--pretty", default=False, action='store_true', help="make the output a little more human readable")
2382*5e3eaea3SApple OSS Distributions    parser.add_argument("-X", "--prettyhex", default=False, action='store_true', help="make the output a little more human readable, and print most things as hex")
2383*5e3eaea3SApple OSS Distributions    parser.add_argument("--incomplete", action='store_true', help="accept incomplete data")
2384*5e3eaea3SApple OSS Distributions    parser.add_argument("kcdata_file", type=argparse.FileType('r'), help="Path to a kcdata binary file.")
2385*5e3eaea3SApple OSS Distributions
2386*5e3eaea3SApple OSS Distributions    class VerboseAction(argparse.Action):
2387*5e3eaea3SApple OSS Distributions        def __call__(self, parser, namespace, values, option_string=None):
2388*5e3eaea3SApple OSS Distributions            logging.basicConfig(level=logging.INFO, stream=sys.stderr, format='%(message)s')
2389*5e3eaea3SApple OSS Distributions    parser.add_argument('-v', "--verbose", action=VerboseAction, nargs=0)
2390*5e3eaea3SApple OSS Distributions
2391*5e3eaea3SApple OSS Distributions    args = parser.parse_args()
2392*5e3eaea3SApple OSS Distributions
2393*5e3eaea3SApple OSS Distributions    if args.multiple and args.stackshot_file:
2394*5e3eaea3SApple OSS Distributions        raise NotImplementedError
2395*5e3eaea3SApple OSS Distributions
2396*5e3eaea3SApple OSS Distributions    if args.pretty and args.stackshot_file:
2397*5e3eaea3SApple OSS Distributions        raise NotImplementedError
2398*5e3eaea3SApple OSS Distributions
2399*5e3eaea3SApple OSS Distributions    if args.list_known_types:
2400*5e3eaea3SApple OSS Distributions        for (n, t) in KNOWN_TYPES_COLLECTION.items():
2401*5e3eaea3SApple OSS Distributions            print("%d : %s " % (n, str(t)))
2402*5e3eaea3SApple OSS Distributions        sys.exit(1)
2403*5e3eaea3SApple OSS Distributions
2404*5e3eaea3SApple OSS Distributions    if args.incomplete or args.stackshot_file:
2405*5e3eaea3SApple OSS Distributions        G.accept_incomplete_data = True
2406*5e3eaea3SApple OSS Distributions
2407*5e3eaea3SApple OSS Distributions    decode_kcdata_file(args.kcdata_file, args.stackshot_file, args.multiple, args.prettyhex, args.pretty, args.plist)
2408