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