]> git.saurik.com Git - apple/xnu.git/blob - tools/lldbmacros/kcdata.py
xnu-3789.31.2.tar.gz
[apple/xnu.git] / tools / lldbmacros / kcdata.py
1 #!/usr/bin/env python
2 import sys
3 import struct
4 import mmap
5 import json
6 import cgitb
7 import copy
8 import re
9 import base64
10 import argparse
11 import os
12 import shlex
13 import subprocess
14 import logging
15 import contextlib
16 import base64
17
18 kcdata_type_def = {
19 'KCDATA_TYPE_INVALID': 0x0,
20 'KCDATA_TYPE_STRING_DESC': 0x1,
21 'KCDATA_TYPE_UINT32_DESC': 0x2,
22 'KCDATA_TYPE_UINT64_DESC': 0x3,
23 'KCDATA_TYPE_INT32_DESC': 0x4,
24 'KCDATA_TYPE_INT64_DESC': 0x5,
25 'KCDATA_TYPE_BINDATA_DESC': 0x6,
26 'KCDATA_TYPE_ARRAY': 0x11,
27 'KCDATA_TYPE_TYPEDEFINTION': 0x12,
28 'KCDATA_TYPE_CONTAINER_BEGIN': 0x13,
29 'KCDATA_TYPE_CONTIANER_END': 0x14,
30
31 'KCDATA_TYPE_ARRAY_PAD0': 0x20,
32 'KCDATA_TYPE_ARRAY_PAD1': 0x21,
33 'KCDATA_TYPE_ARRAY_PAD2': 0x22,
34 'KCDATA_TYPE_ARRAY_PAD3': 0x23,
35 'KCDATA_TYPE_ARRAY_PAD4': 0x24,
36 'KCDATA_TYPE_ARRAY_PAD5': 0x25,
37 'KCDATA_TYPE_ARRAY_PAD6': 0x26,
38 'KCDATA_TYPE_ARRAY_PAD7': 0x27,
39 'KCDATA_TYPE_ARRAY_PAD8': 0x28,
40 'KCDATA_TYPE_ARRAY_PAD9': 0x29,
41 'KCDATA_TYPE_ARRAY_PADa': 0x2a,
42 'KCDATA_TYPE_ARRAY_PADb': 0x2b,
43 'KCDATA_TYPE_ARRAY_PADc': 0x2c,
44 'KCDATA_TYPE_ARRAY_PADd': 0x2d,
45 'KCDATA_TYPE_ARRAY_PADe': 0x2e,
46 'KCDATA_TYPE_ARRAY_PADf': 0x2f,
47
48 'KCDATA_TYPE_LIBRARY_LOADINFO': 0x30,
49 'KCDATA_TYPE_LIBRARY_LOADINFO64': 0x31,
50 'KCDATA_TYPE_TIMEBASE': 0x32,
51 'KCDATA_TYPE_MACH_ABSOLUTE_TIME': 0x33,
52 'KCDATA_TYPE_TIMEVAL': 0x34,
53 'KCDATA_TYPE_USECS_SINCE_EPOCH': 0x35,
54 'KCDATA_TYPE_PID': 0x36,
55 'KCDATA_TYPE_PROCNAME': 0x37,
56 'KCDATA_TYPE_NESTED_KCDATA': 0x38,
57
58 'STACKSHOT_KCCONTAINER_TASK': 0x903,
59 'STACKSHOT_KCCONTAINER_THREAD': 0x904,
60 'STACKSHOT_KCTYPE_DONATING_PIDS': 0x907,
61 'STACKSHOT_KCTYPE_SHAREDCACHE_LOADINFO': 0x908,
62 'STACKSHOT_KCTYPE_KERN_STACKFRAME': 0x90A,
63 'STACKSHOT_KCTYPE_KERN_STACKFRAME64': 0x90B,
64 'STACKSHOT_KCTYPE_USER_STACKFRAME': 0x90C,
65 'STACKSHOT_KCTYPE_USER_STACKFRAME64': 0x90D,
66 'STACKSHOT_KCTYPE_BOOTARGS': 0x90E,
67 'STACKSHOT_KCTYPE_OSVERSION': 0x90F,
68 'STACKSHOT_KCTYPE_KERN_PAGE_SIZE': 0x910,
69 'STACKSHOT_KCTYPE_JETSAM_LEVEL': 0x911,
70 'STACKSHOT_KCTYPE_DELTA_SINCE_TIMESTAMP': 0x912,
71 'STACKSHOT_KCTYPE_TASK_DELTA_SNAPSHOT': 0x940,
72 'STACKSHOT_KCTYPE_THREAD_DELTA_SNAPSHOT': 0x941,
73
74 'STACKSHOT_KCTYPE_KERN_STACKLR': 0x913,
75 'STACKSHOT_KCTYPE_KERN_STACKLR64': 0x914,
76 'STACKSHOT_KCTYPE_USER_STACKLR': 0x915,
77 'STACKSHOT_KCTYPE_USER_STACKLR64': 0x916,
78 'STACKSHOT_KCTYPE_NONRUNNABLE_TIDS': 0x917,
79 'STACKSHOT_KCTYPE_NONRUNNABLE_TASKS': 0x918,
80 'STACKSHOT_KCTYPE_CPU_TIMES': 0x919,
81 'STACKSHOT_KCTYPE_STACKSHOT_DURATION': 0x91a,
82 'STACKSHOT_KCTYPE_STACKSHOT_FAULT_STATS': 0x91b,
83 'STACKSHOT_KCTYPE_KERNELCACHE_LOADINFO': 0x91c,
84
85 'KCDATA_TYPE_BUFFER_END': 0xF19158ED,
86
87
88 'TASK_CRASHINFO_EXTMODINFO': 0x801,
89 'TASK_CRASHINFO_BSDINFOWITHUNIQID': 0x802,
90 'TASK_CRASHINFO_TASKDYLD_INFO': 0x803,
91 'TASK_CRASHINFO_UUID': 0x804,
92 'TASK_CRASHINFO_PID': 0x805,
93 'TASK_CRASHINFO_PPID': 0x806,
94
95 # Don't want anyone using this. It's struct rusage from whatever machine generated the data
96 #'TASK_CRASHINFO_RUSAGE': 0x807,
97 'Type_0x807': 0x807,
98
99 'TASK_CRASHINFO_RUSAGE_INFO': 0x808,
100 'TASK_CRASHINFO_PROC_NAME': 0x809,
101 'TASK_CRASHINFO_PROC_STARTTIME': 0x80B,
102 'TASK_CRASHINFO_USERSTACK': 0x80C,
103 'TASK_CRASHINFO_ARGSLEN': 0x80D,
104 'TASK_CRASHINFO_EXCEPTION_CODES': 0x80E,
105 'TASK_CRASHINFO_PROC_PATH': 0x80F,
106 'TASK_CRASHINFO_PROC_CSFLAGS': 0x810,
107 'TASK_CRASHINFO_PROC_STATUS': 0x811,
108 'TASK_CRASHINFO_UID': 0x812,
109 'TASK_CRASHINFO_GID': 0x813,
110 'TASK_CRASHINFO_PROC_ARGC': 0x814,
111 'TASK_CRASHINFO_PROC_FLAGS': 0x815,
112 'TASK_CRASHINFO_CPUTYPE': 0x816,
113 'TASK_CRASHINFO_WORKQUEUEINFO': 0x817,
114 'TASK_CRASHINFO_RESPONSIBLE_PID': 0x818,
115 'TASK_CRASHINFO_DIRTY_FLAGS': 0x819,
116 'TASK_CRASHINFO_CRASHED_THREADID': 0x81A,
117 'TASK_CRASHINFO_COALITION_ID': 0x81B,
118 'EXIT_REASON_SNAPSHOT': 0x1001,
119 'EXIT_REASON_USER_DESC': 0x1002,
120 'EXIT_REASON_USER_PAYLOAD': 0x1003,
121 'EXIT_REASON_CODESIGNING_INFO': 0x1004,
122 'KCDATA_BUFFER_BEGIN_CRASHINFO': 0xDEADF157,
123 'KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT': 0xDE17A59A,
124 'KCDATA_BUFFER_BEGIN_STACKSHOT': 0x59a25807,
125 'KCDATA_BUFFER_BEGIN_OS_REASON': 0x53A20900,
126 'KCDATA_BUFFER_BEGIN_XNUPOST_CONFIG': 0x1E21C09F
127 }
128 kcdata_type_def_rev = dict((v, k) for k, v in kcdata_type_def.iteritems())
129
130 KNOWN_TYPES_COLLECTION = {}
131
132 KNOWN_TOPLEVEL_CONTAINER_TYPES = ()
133
134 def enum(**args):
135 return type('enum', (), args)
136
137 KCSUBTYPE_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)
138
139
140 LEGAL_OLD_STYLE_ARRAY_TYPE_NAMES = ['KCDATA_TYPE_LIBRARY_LOADINFO',
141 'KCDATA_TYPE_LIBRARY_LOADINFO64',
142 'STACKSHOT_KCTYPE_KERN_STACKFRAME',
143 'STACKSHOT_KCTYPE_USER_STACKFRAME',
144 'STACKSHOT_KCTYPE_KERN_STACKFRAME64',
145 'STACKSHOT_KCTYPE_USER_STACKFRAME64',
146 'STACKSHOT_KCTYPE_DONATING_PIDS',
147 'STACKSHOT_KCTYPE_THREAD_DELTA_SNAPSHOT']
148
149 KCDATA_FLAGS_STRUCT_PADDING_MASK = 0xf
150 KCDATA_FLAGS_STRUCT_HAS_PADDING = 0x80
151
152 class KCSubTypeElement(object):
153 """convert kcdata_subtype_descriptor to """
154 _unpack_formats = (None, 'c', 'b', 'B', 'h', 'H', 'i', 'I', 'q', 'Q')
155 _ctypes = ('Unknown', 'char', 'int8_t', 'uint8_t', 'int16_t', 'uint16_t', 'int32_t', 'uint32_t', 'int64_t', 'uint64_t')
156
157 def __init__(self, st_name, st_type, st_size, st_offset=0, st_flag=0, custom_repr=None):
158 self.name = st_name
159 self.offset = st_offset
160 self.type_id = st_type
161 if st_type <= 0 or st_type > KCSUBTYPE_TYPE.KC_ST_UINT64:
162 raise ValueError("Invalid type passed %d" % st_type)
163 self.unpack_fmt = KCSubTypeElement._unpack_formats[self.type_id]
164 self.size = st_size
165 self.totalsize = st_size
166 self.count = 1
167 self.is_array_type = False
168 self.custom_JsonRepr = custom_repr
169 if (st_flag & 0x1) == 0x1:
170 self.is_array_type = True
171 self.size = st_size & 0xffff
172 self.count = (st_size >> 16) & 0xffff
173 self.totalsize = self.size * self.count
174
175 @staticmethod
176 def GetSizeForArray(el_count, el_size):
177 return ((el_count & 0xffff) << 16) | (el_size & 0xffff)
178
179 @staticmethod
180 def FromBinaryTypeData(byte_data):
181 (st_flag, st_type, st_offset, st_size, st_name) = struct.unpack_from('=BBHI32s', byte_data)
182 st_name = st_name.rstrip('\x00')
183 return KCSubTypeElement(st_name, st_type, st_size, st_offset, st_flag)
184
185 @staticmethod
186 def FromBasicCtype(st_name, st_type, st_offset=0):
187 if st_type <= 0 or st_type > KCSUBTYPE_TYPE.KC_ST_UINT64:
188 raise ValueError("Invalid type passed %d" % st_type)
189 st_size = struct.calcsize(KCSubTypeElement._unpack_formats[st_type])
190 st_flag = 0
191 retval = KCSubTypeElement(st_name, st_type, st_size, st_offset, st_flag, KCSubTypeElement._get_naked_element_value)
192 return retval
193
194 @staticmethod
195 def FromKCSubTypeElement(other, name_override=''):
196 _copy = copy.copy(other)
197 if name_override:
198 _copy.name = name_override
199 return copy
200
201 def GetName(self):
202 return self.name
203
204 def GetTotalSize(self):
205 return self.totalsize
206
207 def GetValueAsString(self, base_data, array_pos=0):
208 return str(self.GetValue(base_data, array_pos))
209
210 def GetValue(self, base_data, array_pos=0):
211 return struct.unpack_from(self.unpack_fmt, base_data[self.offset + (array_pos * self.size):])[0]
212
213 @staticmethod
214 def _get_naked_element_value(elementValue, elementName):
215 return json.dumps(elementValue)
216
217 def __str__(self):
218 if self.is_array_type:
219 return '[%d,%d] %s %s[%d];' % (self.offset, self.totalsize, self.GetCTypeDesc(), self.name, self.count)
220 return '[%d,%d] %s %s;' % (self.offset, self.totalsize, self.GetCTypeDesc(), self.name)
221
222 def __repr__(self):
223 return str(self)
224
225 def GetCTypeDesc(self):
226 return KCSubTypeElement._ctypes[self.type_id]
227
228 def GetStringRepr(self, base_data):
229 if not self.is_array_type:
230 return self.GetValueAsString(base_data)
231 if self.type_id == KCSUBTYPE_TYPE.KC_ST_CHAR:
232 str_len = self.count
233 if len(base_data) < str_len:
234 str_len = len(base_data)
235 str_arr = []
236 for i in range(str_len):
237 _v = self.GetValue(base_data, i)
238 if ord(_v) == 0:
239 break
240 str_arr.append(self.GetValueAsString(base_data, i))
241
242 return '"' + ''.join(str_arr) + '"'
243
244 count = self.count
245 if count > len(base_data)/self.size:
246 count = len(base_data)/self.size
247
248 o = '[' + ','.join([self.GetValueAsString(base_data, i) for i in range(count)]) + ']'
249
250 return o
251
252 def GetJsonRepr(self, base_data, flags=0):
253 if (flags & (KCDATA_FLAGS_STRUCT_HAS_PADDING | KCDATA_FLAGS_STRUCT_PADDING_MASK)) != 0:
254 padding = (flags & KCDATA_FLAGS_STRUCT_PADDING_MASK)
255 if padding:
256 base_data = base_data[:-padding]
257 if self.custom_JsonRepr:
258 if self.is_array_type:
259 e_data = [self.GetValue(base_data, i) for i in range(self.count)]
260 else:
261 e_data = self.GetValue(base_data)
262 return self.custom_JsonRepr(e_data, self.name)
263 return self.GetStringRepr(base_data)
264
265 def sizeof(self):
266 return self.totalsize
267
268 def ShouldSkip(self, data):
269 return len(data) < self.offset + self.totalsize
270
271 def ShouldMerge(self):
272 return False
273
274
275 class KCTypeDescription(object):
276 def __init__(self, t_type_id, t_elements=[], t_name='anon', custom_repr=None, legacy_size=None, merge=False):
277 self.type_id = t_type_id
278 self.elements = t_elements
279 self.name = t_name
280 self.totalsize = 0
281 self.custom_JsonRepr = custom_repr
282 self.legacy_size = legacy_size
283 self.merge = merge
284 for e in self.elements:
285 self.totalsize += e.GetTotalSize()
286
287 def ValidateData(self, base_data):
288 if len(base_data) >= self.totalsize:
289 return True
290 return False
291
292 def GetTypeID(self):
293 return self.type_id
294
295 def GetName(self):
296 return self.name
297
298 def __str__(self):
299 o = '%s {\n\t' % self.name + "\n\t".join([str(e) for e in self.elements]) + '\n};'
300 return o
301
302 @staticmethod
303 def FromKCTypeDescription(other, t_type_id, t_name):
304 retval = KCTypeDescription(t_type_id, other.elements, t_name, other.custom_JsonRepr)
305 return retval
306
307 def ShouldMerge(self):
308 return self.merge
309
310 def GetJsonRepr(self, base_data, flags):
311 if (flags & (KCDATA_FLAGS_STRUCT_HAS_PADDING | KCDATA_FLAGS_STRUCT_PADDING_MASK)) != 0:
312 padding = (flags & KCDATA_FLAGS_STRUCT_PADDING_MASK)
313 if padding:
314 base_data = base_data[:-padding]
315 elif self.legacy_size and len(base_data) == self.legacy_size + ((-self.legacy_size) & 0xf):
316 base_data = base_data[:self.legacy_size]
317 if self.custom_JsonRepr:
318 return self.custom_JsonRepr([e.GetValue(base_data) for e in self.elements])
319 o = ", ".join(['"%s": %s' % (e.GetName(), e.GetJsonRepr(base_data)) for e in self.elements if not e.ShouldSkip(base_data)])
320 if not self.merge:
321 o = '{' + o + '}'
322 return o
323
324 def sizeof(self):
325 return max(st.totalsize + st.offset for st in self.elements)
326
327
328 def GetTypeNameForKey(k):
329 retval = "0x%x" % k
330 if k in KNOWN_TYPES_COLLECTION:
331 retval = KNOWN_TYPES_COLLECTION[k].GetName()
332 elif k in kcdata_type_def_rev:
333 retval = kcdata_type_def_rev[k]
334 return retval
335
336
337 def GetTypeForName(n):
338 ret = 0
339 if n in kcdata_type_def:
340 ret = kcdata_type_def[n]
341 return ret
342
343
344 LEGAL_OLD_STYLE_ARRAY_TYPES = map(GetTypeForName, LEGAL_OLD_STYLE_ARRAY_TYPE_NAMES)
345
346 kcdata_type_def_rev[GetTypeForName('KCDATA_BUFFER_BEGIN_STACKSHOT')] = 'kcdata_stackshot'
347 kcdata_type_def_rev[GetTypeForName('KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT')] = 'kcdata_delta_stackshot'
348 kcdata_type_def_rev[GetTypeForName('KCDATA_BUFFER_BEGIN_CRASHINFO')] = 'kcdata_crashinfo'
349 kcdata_type_def_rev[GetTypeForName('KCDATA_BUFFER_BEGIN_OS_REASON')] = 'kcdata_reason'
350 kcdata_type_def_rev[GetTypeForName('STACKSHOT_KCCONTAINER_TASK')] = 'task_snapshots'
351 kcdata_type_def_rev[GetTypeForName('STACKSHOT_KCCONTAINER_THREAD')] = 'thread_snapshots'
352 kcdata_type_def_rev[GetTypeForName('KCDATA_BUFFER_BEGIN_XNUPOST_CONFIG')] = 'xnupost_testconfig'
353
354 class Indent(object):
355 def __init__(self):
356 self.n = 0
357 def __call__(self, end=False):
358 if end:
359 return " " * (self.n-4)
360 else:
361 return " " * self.n
362 @contextlib.contextmanager
363 def indent(self):
364 self.n += 4
365 try:
366 yield
367 finally:
368 self.n -= 4
369
370 INDENT = Indent()
371
372 class KCObject(object):
373
374 def __init__(self, type_code, data, offset, flags=0):
375
376 self.i_type = type_code
377 self.i_data = data
378 self.offset = offset
379 self.i_size = len(data)
380 self.i_flags = flags
381 self.obj_collection = []
382 self.obj = {}
383 self.is_container_type = False
384 self.is_array_type = False
385 self.is_naked_type = False
386 self.nested_kcdata = None
387 self.i_name = GetTypeNameForKey(type_code)
388
389 self.ParseData()
390
391 if self.i_type == GetTypeForName('KCDATA_TYPE_CONTAINER_BEGIN'):
392 self.__class__ = KCContainerObject
393
394 if self.i_type in KNOWN_TOPLEVEL_CONTAINER_TYPES:
395 self.__class__ = KCBufferObject
396
397 self.InitAfterParse()
398
399 def InitAfterParse(self):
400 pass
401
402 @staticmethod
403 def FromKCItem(kcitem):
404 return KCObject(kcitem.i_type, kcitem.i_data, kcitem.i_offset, kcitem.i_flags)
405
406 def IsContainerEnd(self):
407 return self.i_type == GetTypeForName('KCDATA_TYPE_CONTIANER_END')
408
409 def IsBufferEnd(self):
410 return self.i_type == GetTypeForName('KCDATA_TYPE_BUFFER_END')
411
412 def IsArray(self):
413 return self.is_array_type
414
415 def ShouldMerge(self):
416 if self.nested_kcdata:
417 return True
418 elif not self.is_array_type and self.i_type in KNOWN_TYPES_COLLECTION:
419 return KNOWN_TYPES_COLLECTION[self.i_type].ShouldMerge()
420 else:
421 return False
422
423 def GetJsonRepr(self):
424 if self.is_array_type:
425 return '[' + ', '.join([i.GetJsonRepr() for i in self.obj_collection]) + ']'
426 if self.i_type in KNOWN_TYPES_COLLECTION:
427 return KNOWN_TYPES_COLLECTION[self.i_type].GetJsonRepr(self.i_data, self.i_flags)
428 if self.is_naked_type:
429 return json.dumps(self.obj)
430 if self.nested_kcdata:
431 return self.nested_kcdata.GetJsonRepr()
432
433 raise NotImplementedError("Broken GetJsonRepr implementation")
434
435 def ParseData(self):
436
437
438 if self.i_type == GetTypeForName('KCDATA_TYPE_CONTAINER_BEGIN'):
439 self.obj['uniqID'] = self.i_flags
440 self.i_name = str(self.obj['uniqID'])
441 self.obj['typeID'] = struct.unpack_from('I', self.i_data)[0]
442 logging.info("0x%08x: %sCONTAINER: %s(%x)" % (self.offset, INDENT(), GetTypeNameForKey(self.obj['typeID']), self.i_flags))
443
444 elif self.i_type in (KNOWN_TOPLEVEL_CONTAINER_TYPES):
445 self.obj['uniqID'] = self.i_name
446 self.obj['typeID'] = self.i_type
447 logging.info("0x%08x: %s%s" % (self.offset, INDENT(), self.i_name))
448
449 elif self.i_type == GetTypeForName('KCDATA_TYPE_CONTIANER_END'):
450 self.obj['uniqID'] = self.i_flags
451 logging.info("0x%08x: %sEND" % (self.offset, INDENT(end=True)))
452
453 elif self.i_type == GetTypeForName('KCDATA_TYPE_BUFFER_END'):
454 self.obj = ''
455 logging.info("0x%08x: %sEND_BUFFER" % (self.offset, INDENT(end=True)))
456
457 elif self.i_type == GetTypeForName('KCDATA_TYPE_UINT32_DESC'):
458 self.is_naked_type = True
459 u_d = struct.unpack_from('32sI', self.i_data)
460 self.i_name = u_d[0].strip(chr(0))
461 self.obj = u_d[1]
462 logging.info("0x%08x: %s%s" % (self.offset, INDENT(), self.i_name))
463
464 elif self.i_type == GetTypeForName('KCDATA_TYPE_UINT64_DESC'):
465 self.is_naked_type = True
466 u_d = struct.unpack_from('32sQ', self.i_data)
467 self.i_name = u_d[0].strip(chr(0))
468 self.obj = u_d[1]
469 logging.info("0x%08x: %s%s" % (self.offset, INDENT(), self.i_name))
470
471 elif self.i_type == GetTypeForName('KCDATA_TYPE_TYPEDEFINTION'):
472 self.is_naked_type = True
473 u_d = struct.unpack_from('II32s', self.i_data)
474 self.obj['name'] = u_d[2].split(chr(0))[0]
475 self.i_name = "typedef[%s]" % self.obj['name']
476 self.obj['typeID'] = u_d[0]
477 self.obj['numOfFields'] = u_d[1]
478 element_arr = []
479 for i in range(u_d[1]):
480 e = KCSubTypeElement.FromBinaryTypeData(self.i_data[40+(i*40):])
481 #print str(e)
482 element_arr.append(e)
483 type_desc = KCTypeDescription(u_d[0], element_arr, self.obj['name'])
484 #print str(type_desc)
485 self.obj['fields'] = [str(e) for e in element_arr]
486 KNOWN_TYPES_COLLECTION[type_desc.GetTypeID()] = type_desc
487 logging.info("0x%08x: %s%s" % (self.offset, INDENT(), self.i_name))
488
489 elif self.i_type == GetTypeForName('KCDATA_TYPE_ARRAY'):
490 self.is_array_type = True
491 e_t = (self.i_flags >> 32) & 0xffffffff
492 if e_t not in LEGAL_OLD_STYLE_ARRAY_TYPES:
493 raise Exception, "illegal old-style array type: %s (0x%x)" % (GetTypeNameForKey(e_t), e_t)
494 e_c = self.i_flags & 0xffffffff
495 e_s = KNOWN_TYPES_COLLECTION[e_t].sizeof()
496 if e_s * e_c > self.i_size:
497 raise Excpetion, "array too small for its count"
498 self.obj['typeID'] = e_t
499 self.i_name = GetTypeNameForKey(e_t)
500 self.i_type = e_t
501 self.obj['numOfElements'] = e_c
502 self.obj['sizeOfElement'] = e_s
503 logging.info("0x%08x: %sARRAY: %s" % (self.offset, INDENT(), self.i_name))
504 #populate the array here by recursive creation of KCObject
505 with INDENT.indent():
506 for _i in range(e_c):
507 _o = KCObject(e_t, self.i_data[(_i * e_s):(_i * e_s) + e_s], self.offset + _i*e_s)
508 self.obj_collection.append(_o)
509
510 elif self.i_type >= GetTypeForName('KCDATA_TYPE_ARRAY_PAD0') and self.i_type <= GetTypeForName('KCDATA_TYPE_ARRAY_PADf'):
511 self.is_array_type = True
512 e_t = (self.i_flags >> 32) & 0xffffffff
513 e_c = self.i_flags & 0xffffffff
514 e_s = (self.i_size - (self.i_type & 0xf)) / e_c if e_c != 0 else None
515 self.obj['typeID'] = e_t
516 self.i_name = GetTypeNameForKey(e_t)
517 self.i_type = e_t
518 self.obj['numOfElements'] = e_c
519 self.obj['sizeOfElement'] = e_s
520 logging.info("0x%08x: %sARRAY: %s" % (self.offset, INDENT(), self.i_name))
521 #populate the array here by recursive creation of KCObject
522 with INDENT.indent():
523 for _i in range(e_c):
524 _o = KCObject(e_t, self.i_data[(_i * e_s):(_i * e_s) + e_s], self.offset + _i*e_s)
525 self.obj_collection.append(_o)
526
527 elif self.i_type == GetTypeForName('KCDATA_TYPE_NESTED_KCDATA'):
528 logging.info("0x%08x: %sNESTED_KCDATA" % (self.offset, INDENT()))
529 with INDENT.indent():
530 nested_iterator = kcdata_item_iterator(self.i_data[:self.i_size])
531 nested_buffer = KCObject.FromKCItem(nested_iterator.next())
532 if not isinstance(nested_buffer, KCBufferObject):
533 raise Exception, "nested buffer isn't a KCBufferObject"
534 nested_buffer.ReadItems(nested_iterator)
535 self.nested_kcdata = nested_buffer
536
537 elif self.i_type in KNOWN_TYPES_COLLECTION:
538 self.i_name = KNOWN_TYPES_COLLECTION[self.i_type].GetName()
539 self.is_naked_type = True
540 logging.info("0x%08x: %s%s" % (self.offset, INDENT(), self.i_name))
541 else:
542 self.is_naked_type = True
543 #self.obj = "data of len %d" % len(self.i_data)
544 #self.obj = ''.join(["%x" % ki for ki in struct.unpack('%dB' % len(self.i_data), self.i_data)])
545 self.obj = map(ord, self.i_data)
546 logging.info("0x%08x: %s%s" % (self.offset, INDENT(), self.i_name))
547
548
549 class KCContainerObject(KCObject):
550 def __init__(self, *args, **kwargs):
551 assert False
552
553 def InitAfterParse(self):
554 self.obj_container_dict = {}
555 self.obj_nested_objs = {}
556
557 def ShouldMerge(self):
558 return True
559
560 def GetJsonRepr(self):
561 # o = '"%s"' % self.obj['uniqID'] + ' : { "typeID" : %d ,' % self.obj['typeID']
562 o = '"%s"' % self.obj['uniqID'] + ' : { '
563 for (k, v) in self.obj_container_dict.items():
564 if v.ShouldMerge():
565 o += v.GetJsonRepr() + ","
566 else:
567 o += ' "%s" : ' % k + v.GetJsonRepr() + ","
568
569 for (k, v) in self.obj_nested_objs.items():
570 o += '"%s" : {' % k + ",".join([vi.GetJsonRepr() for vi in v.values()]) + "} ,"
571
572 o = o.rstrip(',') + "}"
573
574 return o
575
576 def AddObject(self, kco):
577 assert not kco.IsContainerEnd()
578 if isinstance(kco, KCContainerObject):
579 type_name = GetTypeNameForKey(kco.obj['typeID'])
580 if type_name not in self.obj_nested_objs:
581 self.obj_nested_objs[type_name] = {}
582 self.obj_nested_objs[type_name][kco.i_name] = kco
583 return
584 if kco.i_name in self.obj_container_dict:
585 if kco.IsArray() and self.obj_container_dict[kco.i_name].IsArray():
586 self.obj_container_dict[kco.i_name].obj_collection.extend( kco.obj_collection )
587 else:
588 self.obj_container_dict[kco.i_name] = kco
589
590 def IsEndMarker(self, o):
591 if not o.IsContainerEnd():
592 return False
593 if o.i_flags != self.i_flags:
594 raise Exception, "container end marker doesn't match"
595 return True
596
597 no_end_message = "could not find container end marker"
598
599 def ReadItems(self, iterator):
600 found_end = False
601 with INDENT.indent():
602 for i in iterator:
603 o = KCObject.FromKCItem(i)
604 if self.IsEndMarker(o):
605 found_end = True
606 break
607 if isinstance(o, KCContainerObject):
608 o.ReadItems(iterator)
609 self.AddObject(o)
610 if not found_end:
611 raise Exception, self.no_end_message
612
613
614 class KCBufferObject(KCContainerObject):
615
616 def IsEndMarker(self,o):
617 if o.IsContainerEnd():
618 raise Exception, "container end marker at the toplevel"
619 return o.IsBufferEnd()
620
621 no_end_message = "could not find buffer end marker"
622
623
624 class KCData_item:
625 """ a basic kcdata_item type object.
626 """
627 header_size = 16 # (uint32_t + uint32_t + uint64_t)
628
629 def __init__(self, item_type, item_size, item_flags, item_data):
630 self.i_type = item_type
631 self.i_size = item_size
632 self.i_flags = item_flags
633 self.i_data = item_data
634 self.i_offset = None
635
636 def __init__(self, barray, pos=0):
637 """ create an object by parsing data from bytes array
638 returns : obj - if data is readable
639 raises ValueError if something is not ok.
640 """
641 self.i_type = struct.unpack('I', barray[pos:pos+4])[0] # int.from_bytes(barray[pos:pos+4])
642 self.i_size = struct.unpack('I', barray[pos+4:pos+8])[0] # int.from_bytes(barray[pos+4:pos+8])
643 self.i_flags = struct.unpack('Q', barray[pos+8:pos+16])[0] # int.from_bytes(barray[pos+8:pos+16])
644 self.i_data = barray[pos+16: (pos + 16 + self.i_size)]
645 self.i_offset = pos
646
647 def __len__(self):
648 return self.i_size + KCData_item.header_size
649
650 def GetHeaderDescription(self):
651 outs = "type: 0x%x size: 0x%x flags: 0x%x (%s)" % (self.i_type, self.i_size, self.i_flags, GetTypeNameForKey(self.i_type))
652 if not self.i_offset is None:
653 outs = "pos: 0x%x" % self.i_offset + outs
654 return outs
655
656 def __str__(self):
657 return self.GetHeaderDescription()
658
659 def kcdata_item_iterator(data):
660 file_len = len(data)
661 curpos = 0
662 while curpos < file_len:
663 item = KCData_item(data, curpos)
664 yield item
665 curpos += len(item)
666
667 def _get_data_element(elementValues):
668 return json.dumps(elementValues[-1])
669
670 KNOWN_TOPLEVEL_CONTAINER_TYPES = map(GetTypeForName, ('KCDATA_BUFFER_BEGIN_CRASHINFO', 'KCDATA_BUFFER_BEGIN_STACKSHOT', 'KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT', 'KCDATA_BUFFER_BEGIN_OS_REASON','KCDATA_BUFFER_BEGIN_XNUPOST_CONFIG'))
671
672 KNOWN_TYPES_COLLECTION[GetTypeForName('KCDATA_TYPE_UINT32_DESC')] = KCTypeDescription(GetTypeForName('KCDATA_TYPE_UINT32_DESC'), (
673 KCSubTypeElement('desc', KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(32, 1), 0, 1),
674 KCSubTypeElement('data', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 32, 0)
675 ),
676 'KCDATA_TYPE_UINT32_DESC',
677 _get_data_element
678 )
679
680 KNOWN_TYPES_COLLECTION[GetTypeForName('KCDATA_TYPE_UINT64_DESC')] = KCTypeDescription(GetTypeForName('KCDATA_TYPE_UINT64_DESC'), (
681 KCSubTypeElement('desc', KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(32, 1), 0, 1),
682 KCSubTypeElement('data', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 32, 0)
683 ),
684 'KCDATA_TYPE_UINT64_DESC',
685 _get_data_element
686 )
687
688 KNOWN_TYPES_COLLECTION[GetTypeForName('KCDATA_TYPE_TIMEBASE')] = KCTypeDescription(GetTypeForName('KCDATA_TYPE_TIMEBASE'), (
689 KCSubTypeElement('numer', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 0, 0),
690 KCSubTypeElement('denom', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4, 0)
691 ),
692 'mach_timebase_info'
693 )
694
695
696 STACKSHOT_IO_NUM_PRIORITIES = 4
697 KNOWN_TYPES_COLLECTION[0x901] = KCTypeDescription(0x901, (
698 KCSubTypeElement.FromBasicCtype('ss_disk_reads_count', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
699 KCSubTypeElement.FromBasicCtype('ss_disk_reads_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
700 KCSubTypeElement.FromBasicCtype('ss_disk_writes_count', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
701 KCSubTypeElement.FromBasicCtype('ss_disk_writes_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
702 KCSubTypeElement('ss_io_priority_count', KCSUBTYPE_TYPE.KC_ST_UINT64, KCSubTypeElement.GetSizeForArray(STACKSHOT_IO_NUM_PRIORITIES, 8), 32, 1),
703 KCSubTypeElement('ss_io_priority_size', KCSUBTYPE_TYPE.KC_ST_UINT64, KCSubTypeElement.GetSizeForArray(STACKSHOT_IO_NUM_PRIORITIES, 8), 32 + (STACKSHOT_IO_NUM_PRIORITIES * 8), 1),
704 KCSubTypeElement.FromBasicCtype('ss_paging_count', KCSUBTYPE_TYPE.KC_ST_UINT64, 32 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8)),
705 KCSubTypeElement.FromBasicCtype('ss_paging_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 40 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8)),
706 KCSubTypeElement.FromBasicCtype('ss_non_paging_count', KCSUBTYPE_TYPE.KC_ST_UINT64, 48 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8)),
707 KCSubTypeElement.FromBasicCtype('ss_non_paging_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 56 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8)),
708 KCSubTypeElement.FromBasicCtype('ss_data_count', KCSUBTYPE_TYPE.KC_ST_UINT64, 64 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8)),
709 KCSubTypeElement.FromBasicCtype('ss_data_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 72 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8)),
710 KCSubTypeElement.FromBasicCtype('ss_metadata_count', KCSUBTYPE_TYPE.KC_ST_UINT64, 80 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8)),
711 KCSubTypeElement.FromBasicCtype('ss_metadata_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 88 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8))
712 ),
713 'io_statistics'
714 )
715
716 KNOWN_TYPES_COLLECTION[0x902] = KCTypeDescription(0x902, (
717 KCSubTypeElement('snapshot_magic', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 0, 0),
718 KCSubTypeElement('free_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 1, 0),
719 KCSubTypeElement('active_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 2, 0),
720 KCSubTypeElement('inactive_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 3, 0),
721 KCSubTypeElement('purgeable_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 4, 0),
722 KCSubTypeElement('wired_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 5, 0),
723 KCSubTypeElement('speculative_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 6, 0),
724 KCSubTypeElement('throttled_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 7, 0),
725 KCSubTypeElement('filebacked_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 8, 0),
726 KCSubTypeElement('compressions', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 9, 0),
727 KCSubTypeElement('decompressions', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 10, 0),
728 KCSubTypeElement('compressor_size', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 11, 0),
729 KCSubTypeElement('busy_buffer_count', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 4 * 12, 0),
730 KCSubTypeElement('pages_wanted', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 13, 0),
731 KCSubTypeElement('pages_reclaimed', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 14, 0),
732 KCSubTypeElement('pages_wanted_reclaimed_valid', KCSUBTYPE_TYPE.KC_ST_UINT8, 1, 4 * 15, 0)
733 ),
734 'mem_and_io_snapshot'
735 )
736
737
738 KNOWN_TYPES_COLLECTION[0x905] = KCTypeDescription(0x905, (
739 KCSubTypeElement.FromBasicCtype('ts_unique_pid', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
740 KCSubTypeElement.FromBasicCtype('ts_ss_flags', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
741 KCSubTypeElement.FromBasicCtype('ts_user_time_in_terminated_thre', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
742 KCSubTypeElement.FromBasicCtype('ts_system_time_in_terminated_th', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
743 KCSubTypeElement.FromBasicCtype('ts_p_start_sec', KCSUBTYPE_TYPE.KC_ST_UINT64, 32),
744 KCSubTypeElement.FromBasicCtype('ts_task_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 40),
745 KCSubTypeElement.FromBasicCtype('ts_max_resident_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 48),
746 KCSubTypeElement.FromBasicCtype('ts_suspend_count', KCSUBTYPE_TYPE.KC_ST_UINT32, 56),
747 KCSubTypeElement.FromBasicCtype('ts_faults', KCSUBTYPE_TYPE.KC_ST_UINT32, 60),
748 KCSubTypeElement.FromBasicCtype('ts_pageins', KCSUBTYPE_TYPE.KC_ST_UINT32, 64),
749 KCSubTypeElement.FromBasicCtype('ts_cow_faults', KCSUBTYPE_TYPE.KC_ST_UINT32, 68),
750 KCSubTypeElement.FromBasicCtype('ts_was_throttled', KCSUBTYPE_TYPE.KC_ST_UINT32, 72),
751 KCSubTypeElement.FromBasicCtype('ts_did_throttle', KCSUBTYPE_TYPE.KC_ST_UINT32, 76),
752 KCSubTypeElement.FromBasicCtype('ts_latency_qos', KCSUBTYPE_TYPE.KC_ST_UINT32, 80),
753 KCSubTypeElement.FromBasicCtype('ts_pid', KCSUBTYPE_TYPE.KC_ST_INT32, 84),
754 KCSubTypeElement('ts_p_comm', KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(32, 1), 88, 1)
755 ),
756 'task_snapshot'
757 )
758
759 KNOWN_TYPES_COLLECTION[0x906] = KCTypeDescription(0x906, (
760 KCSubTypeElement.FromBasicCtype('ths_thread_id', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
761 KCSubTypeElement.FromBasicCtype('ths_wait_event', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
762 KCSubTypeElement.FromBasicCtype('ths_continuation', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
763 KCSubTypeElement.FromBasicCtype('ths_total_syscalls', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
764 KCSubTypeElement.FromBasicCtype('ths_voucher_identifier', KCSUBTYPE_TYPE.KC_ST_UINT64, 32),
765 KCSubTypeElement.FromBasicCtype('ths_dqserialnum', KCSUBTYPE_TYPE.KC_ST_UINT64, 40),
766 KCSubTypeElement.FromBasicCtype('ths_user_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 48),
767 KCSubTypeElement.FromBasicCtype('ths_sys_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 56),
768 KCSubTypeElement.FromBasicCtype('ths_ss_flags', KCSUBTYPE_TYPE.KC_ST_UINT64, 64),
769 KCSubTypeElement.FromBasicCtype('ths_last_run_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 72),
770 KCSubTypeElement.FromBasicCtype('ths_last_made_runnable_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 80),
771 KCSubTypeElement.FromBasicCtype('ths_state', KCSUBTYPE_TYPE.KC_ST_UINT32, 88),
772 KCSubTypeElement.FromBasicCtype('ths_sched_flags', KCSUBTYPE_TYPE.KC_ST_UINT32, 92),
773 KCSubTypeElement.FromBasicCtype('ths_base_priority', KCSUBTYPE_TYPE.KC_ST_INT16, 96),
774 KCSubTypeElement.FromBasicCtype('ths_sched_priority', KCSUBTYPE_TYPE.KC_ST_INT16, 98),
775 KCSubTypeElement.FromBasicCtype('ths_eqos', KCSUBTYPE_TYPE.KC_ST_UINT8, 100),
776 KCSubTypeElement.FromBasicCtype('ths_rqos', KCSUBTYPE_TYPE.KC_ST_UINT8, 101),
777 KCSubTypeElement.FromBasicCtype('ths_rqos_override', KCSUBTYPE_TYPE.KC_ST_UINT8, 102),
778 KCSubTypeElement.FromBasicCtype('ths_io_tier', KCSUBTYPE_TYPE.KC_ST_UINT8, 103),
779 KCSubTypeElement.FromBasicCtype('ths_thread_t', KCSUBTYPE_TYPE.KC_ST_UINT64, 104),
780 ),
781 'thread_snapshot',
782 legacy_size = 0x68
783 )
784
785 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_THREAD_DELTA_SNAPSHOT')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_THREAD_DELTA_SNAPSHOT'), (
786 KCSubTypeElement.FromBasicCtype('tds_thread_id', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
787 KCSubTypeElement.FromBasicCtype('tds_voucher_identifier', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
788 KCSubTypeElement.FromBasicCtype('tds_ss_flags', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
789 KCSubTypeElement.FromBasicCtype('tds_last_made_runnable_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
790 KCSubTypeElement.FromBasicCtype('tds_state', KCSUBTYPE_TYPE.KC_ST_UINT32, 32),
791 KCSubTypeElement.FromBasicCtype('tds_sched_flags', KCSUBTYPE_TYPE.KC_ST_UINT32, 36),
792 KCSubTypeElement.FromBasicCtype('tds_base_priority', KCSUBTYPE_TYPE.KC_ST_INT16, 40),
793 KCSubTypeElement.FromBasicCtype('tds_sched_priority', KCSUBTYPE_TYPE.KC_ST_INT16, 42),
794 KCSubTypeElement.FromBasicCtype('tds_eqos', KCSUBTYPE_TYPE.KC_ST_UINT8, 44),
795 KCSubTypeElement.FromBasicCtype('tds_rqos', KCSUBTYPE_TYPE.KC_ST_UINT8, 45),
796 KCSubTypeElement.FromBasicCtype('tds_rqos_override', KCSUBTYPE_TYPE.KC_ST_UINT8, 46),
797 KCSubTypeElement.FromBasicCtype('tds_io_tier', KCSUBTYPE_TYPE.KC_ST_UINT8, 47),
798 ),
799 'thread_delta_snapshot'
800 )
801
802 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_TASK_DELTA_SNAPSHOT')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_TASK_DELTA_SNAPSHOT'), (
803 KCSubTypeElement.FromBasicCtype('tds_unique_pid', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
804 KCSubTypeElement.FromBasicCtype('tds_ss_flags', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
805 KCSubTypeElement.FromBasicCtype('tds_user_time_in_terminated_thr', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
806 KCSubTypeElement.FromBasicCtype('tds_system_time_in_terminated_t', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
807 KCSubTypeElement.FromBasicCtype('tds_task_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 32),
808 KCSubTypeElement.FromBasicCtype('tds_max_resident_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 40),
809 KCSubTypeElement.FromBasicCtype('tds_suspend_count', KCSUBTYPE_TYPE.KC_ST_UINT32, 48),
810 KCSubTypeElement.FromBasicCtype('tds_faults', KCSUBTYPE_TYPE.KC_ST_UINT32, 52),
811 KCSubTypeElement.FromBasicCtype('tds_pageins', KCSUBTYPE_TYPE.KC_ST_UINT32, 56),
812 KCSubTypeElement.FromBasicCtype('tds_cow_faults', KCSUBTYPE_TYPE.KC_ST_UINT32, 60),
813 KCSubTypeElement.FromBasicCtype('tds_was_throttled', KCSUBTYPE_TYPE.KC_ST_UINT32, 64),
814 KCSubTypeElement.FromBasicCtype('tds_did_throttle', KCSUBTYPE_TYPE.KC_ST_UINT32, 68),
815 KCSubTypeElement.FromBasicCtype('tds_latency_qos', KCSUBTYPE_TYPE.KC_ST_UINT32, 72),
816 ),
817 'task_delta_snapshot'
818 )
819
820
821 KNOWN_TYPES_COLLECTION[0x909] = KCSubTypeElement('pth_name', KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(64, 1), 0, 1)
822
823
824
825 KNOWN_TYPES_COLLECTION[GetTypeForName('KCDATA_TYPE_LIBRARY_LOADINFO64')] = KCTypeDescription(GetTypeForName('KCDATA_TYPE_LIBRARY_LOADINFO64'), (
826 KCSubTypeElement('imageLoadAddress', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0),
827 KCSubTypeElement('imageUUID', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 8, 1)
828 ),
829 'dyld_load_info'
830 )
831
832 KNOWN_TYPES_COLLECTION[GetTypeForName('KCDATA_TYPE_LIBRARY_LOADINFO')] = KCTypeDescription(GetTypeForName('KCDATA_TYPE_LIBRARY_LOADINFO'), (
833 KCSubTypeElement('imageLoadAddress', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 0, 0),
834 KCSubTypeElement('imageUUID', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 4, 1)
835 ),
836 'dyld_load_info'
837 )
838
839 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_SHAREDCACHE_LOADINFO')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_SHAREDCACHE_LOADINFO'), (
840 KCSubTypeElement('imageLoadAddress', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0),
841 KCSubTypeElement('imageUUID', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 8, 1),
842 KCSubTypeElement('imageSlidBaseAddress', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 24, 0),
843 ),
844 'shared_cache_dyld_load_info',
845 legacy_size = 0x18
846 )
847
848 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERNELCACHE_LOADINFO')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_KERNELCACHE_LOADINFO'), (
849 KCSubTypeElement('imageLoadAddress', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0),
850 KCSubTypeElement('imageUUID', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 8, 1),
851 ),
852 'kernelcache_load_info'
853 )
854
855 KNOWN_TYPES_COLLECTION[0x33] = KCSubTypeElement('mach_absolute_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0, KCSubTypeElement._get_naked_element_value)
856 KNOWN_TYPES_COLLECTION[0x907] = KCSubTypeElement.FromBasicCtype('donating_pids', KCSUBTYPE_TYPE.KC_ST_INT32)
857
858 KNOWN_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)
859
860 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKFRAME')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKFRAME'), (
861 KCSubTypeElement.FromBasicCtype('lr', KCSUBTYPE_TYPE.KC_ST_UINT32),
862 KCSubTypeElement.FromBasicCtype('sp', KCSUBTYPE_TYPE.KC_ST_UINT32, 4)
863 ),
864 'kernel_stack_frames'
865 )
866
867 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKLR')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKLR'), (
868 KCSubTypeElement.FromBasicCtype('lr', KCSUBTYPE_TYPE.KC_ST_UINT32),
869 ),
870 'kernel_stack_frames'
871 )
872
873
874 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_USER_STACKFRAME')] = KCTypeDescription.FromKCTypeDescription(
875 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKFRAME')],
876 GetTypeForName('STACKSHOT_KCTYPE_USER_STACKFRAME'),
877 'user_stack_frames'
878 )
879
880 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_USER_STACKLR')] = KCTypeDescription.FromKCTypeDescription(
881 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKLR')],
882 GetTypeForName('STACKSHOT_KCTYPE_USER_STACKLR'),
883 'user_stack_frames'
884 )
885
886 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKFRAME64')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKFRAME64'), (
887 KCSubTypeElement.FromBasicCtype('lr', KCSUBTYPE_TYPE.KC_ST_UINT64),
888 KCSubTypeElement.FromBasicCtype('sp', KCSUBTYPE_TYPE.KC_ST_UINT64, 8)
889 ),
890 'kernel_stack_frames'
891 )
892
893 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_USER_STACKFRAME64')] = KCTypeDescription.FromKCTypeDescription(
894 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKFRAME64')],
895 GetTypeForName('STACKSHOT_KCTYPE_USER_STACKFRAME64'),
896 'user_stack_frames'
897 )
898
899
900 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKLR64')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKLR64'), (
901 KCSubTypeElement.FromBasicCtype('lr', KCSUBTYPE_TYPE.KC_ST_UINT64),
902 ),
903 'kernel_stack_frames'
904 )
905
906 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_USER_STACKLR64')] = KCTypeDescription.FromKCTypeDescription(
907 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKLR64')],
908 GetTypeForName('STACKSHOT_KCTYPE_USER_STACKLR64'),
909 'user_stack_frames'
910 )
911
912
913 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_NONRUNNABLE_TIDS')] = KCSubTypeElement.FromBasicCtype('nonrunnable_threads', KCSUBTYPE_TYPE.KC_ST_INT64)
914
915 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_NONRUNNABLE_TASKS')] = KCSubTypeElement.FromBasicCtype('nonrunnable_tasks', KCSUBTYPE_TYPE.KC_ST_INT64)
916
917 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_OSVERSION')] = KCSubTypeElement('osversion', KCSUBTYPE_TYPE.KC_ST_CHAR,
918 KCSubTypeElement.GetSizeForArray(256, 1), 0, 1)
919
920 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_BOOTARGS')] = KCSubTypeElement('boot_args', KCSUBTYPE_TYPE.KC_ST_CHAR,
921 KCSubTypeElement.GetSizeForArray(256, 1), 0, 1)
922
923 KNOWN_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)
924
925 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_JETSAM_LEVEL')] = KCSubTypeElement('jetsam_level', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 0, 0, KCSubTypeElement._get_naked_element_value)
926
927 KNOWN_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)
928
929 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_STACKSHOT_FAULT_STATS')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_STACKSHOT_FAULT_STATS'),
930 (
931 KCSubTypeElement.FromBasicCtype('sfs_pages_faulted_in', KCSUBTYPE_TYPE.KC_ST_UINT32, 0),
932 KCSubTypeElement.FromBasicCtype('sfs_time_spent_faulting', KCSUBTYPE_TYPE.KC_ST_UINT64, 4),
933 KCSubTypeElement.FromBasicCtype('sfs_system_max_fault_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 12),
934 KCSubTypeElement.FromBasicCtype('sfs_stopped_faulting', KCSUBTYPE_TYPE.KC_ST_UINT8, 20)
935 ),
936 'stackshot_fault_stats')
937
938 #KNOWN_TYPES_COLLECTION[0x907] = KCSubTypeElement('donating_pids', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 0, 0, KCSubTypeElement._get_naked_element_value)
939 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PID')] = KCSubTypeElement('pid', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
940 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PPID')] = KCSubTypeElement('ppid', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
941 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PROC_NAME')] = KCSubTypeElement('p_comm', KCSUBTYPE_TYPE.KC_ST_CHAR,
942 KCSubTypeElement.GetSizeForArray(32, 1), 0, 1)
943 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_USERSTACK')] = KCSubTypeElement('userstack_ptr', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0)
944 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_ARGSLEN')] = KCSubTypeElement('p_argslen', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
945
946 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PROC_PATH')] = KCSubTypeElement('p_path', KCSUBTYPE_TYPE.KC_ST_CHAR,
947 KCSubTypeElement.GetSizeForArray(1024, 1), 0, 1)
948 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PROC_CSFLAGS')] = KCSubTypeElement('p_csflags', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
949 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_UID')] = KCSubTypeElement('uid', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
950 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_GID')] = KCSubTypeElement('gid', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
951 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PROC_ARGC')] = KCSubTypeElement('argc', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
952 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PROC_FLAGS')] = KCSubTypeElement('p_flags', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
953 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_CPUTYPE')] = KCSubTypeElement('cputype', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
954 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_RESPONSIBLE_PID')] = KCSubTypeElement('responsible_pid', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
955 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_DIRTY_FLAGS')] = KCSubTypeElement('dirty_flags', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
956 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_CRASHED_THREADID')] = KCSubTypeElement('crashed_threadid', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0)
957 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_COALITION_ID')] = KCSubTypeElement('coalition_id', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0)
958
959 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PROC_STATUS')] = KCSubTypeElement('p_status', KCSUBTYPE_TYPE.KC_ST_UINT8, 1, 0, 0)
960
961 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_BSDINFOWITHUNIQID')] = KCTypeDescription(GetTypeForName('TASK_CRASHINFO_BSDINFOWITHUNIQID'),
962 ( KCSubTypeElement('p_uuid', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 0, 1),
963 KCSubTypeElement.FromBasicCtype('p_uniqueid', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
964 KCSubTypeElement.FromBasicCtype('p_puniqueid', KCSUBTYPE_TYPE.KC_ST_UINT64, 24)
965 ),
966 'proc_uniqidentifierinfo')
967
968 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_EXCEPTION_CODES')] = (
969 KCTypeDescription(GetTypeForName('TASK_CRASHINFO_EXCEPTION_CODES'),
970 (KCSubTypeElement.FromBasicCtype('code_0', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
971 KCSubTypeElement.FromBasicCtype('code_1', KCSUBTYPE_TYPE.KC_ST_UINT64, 8)),
972 'mach_exception_data_t'))
973
974
975 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PROC_STARTTIME')] = (
976 KCTypeDescription(GetTypeForName('TASK_CRASHINFO_PROC_STARTTIME'),
977 (KCSubTypeElement.FromBasicCtype('tv_sec', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
978 KCSubTypeElement.FromBasicCtype('tv_usec', KCSUBTYPE_TYPE.KC_ST_UINT64, 8)),
979 'proc_starttime'))
980
981
982 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_RUSAGE_INFO')] = KCTypeDescription(GetTypeForName('TASK_CRASHINFO_RUSAGE_INFO'),
983 (
984 KCSubTypeElement('ri_uuid', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 0, 1),
985 KCSubTypeElement.FromBasicCtype('ri_user_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
986 KCSubTypeElement.FromBasicCtype('ri_system_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
987 KCSubTypeElement.FromBasicCtype('ri_pkg_idle_wkups', KCSUBTYPE_TYPE.KC_ST_UINT64, 32),
988 KCSubTypeElement.FromBasicCtype('ri_interrupt_wkups', KCSUBTYPE_TYPE.KC_ST_UINT64, 40),
989 KCSubTypeElement.FromBasicCtype('ri_pageins', KCSUBTYPE_TYPE.KC_ST_UINT64, 48),
990 KCSubTypeElement.FromBasicCtype('ri_wired_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 56),
991 KCSubTypeElement.FromBasicCtype('ri_resident_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 64),
992 KCSubTypeElement.FromBasicCtype('ri_phys_footprint', KCSUBTYPE_TYPE.KC_ST_UINT64, 72),
993 KCSubTypeElement.FromBasicCtype('ri_proc_start_abstime', KCSUBTYPE_TYPE.KC_ST_UINT64, 80),
994 KCSubTypeElement.FromBasicCtype('ri_proc_exit_abstime', KCSUBTYPE_TYPE.KC_ST_UINT64, 88),
995 KCSubTypeElement.FromBasicCtype('ri_child_user_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 96),
996 KCSubTypeElement.FromBasicCtype('ri_child_system_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 104),
997 KCSubTypeElement.FromBasicCtype('ri_child_pkg_idle_wkups', KCSUBTYPE_TYPE.KC_ST_UINT64, 112),
998 KCSubTypeElement.FromBasicCtype('ri_child_interrupt_wkups', KCSUBTYPE_TYPE.KC_ST_UINT64, 120),
999 KCSubTypeElement.FromBasicCtype('ri_child_pageins', KCSUBTYPE_TYPE.KC_ST_UINT64, 128),
1000 KCSubTypeElement.FromBasicCtype('ri_child_elapsed_abstime', KCSUBTYPE_TYPE.KC_ST_UINT64, 136),
1001 KCSubTypeElement.FromBasicCtype('ri_diskio_bytesread', KCSUBTYPE_TYPE.KC_ST_UINT64, 144),
1002 KCSubTypeElement.FromBasicCtype('ri_diskio_byteswritten', KCSUBTYPE_TYPE.KC_ST_UINT64, 152),
1003 KCSubTypeElement.FromBasicCtype('ri_cpu_time_qos_default', KCSUBTYPE_TYPE.KC_ST_UINT64, 160),
1004 KCSubTypeElement.FromBasicCtype('ri_cpu_time_qos_maintenance', KCSUBTYPE_TYPE.KC_ST_UINT64, 168),
1005 KCSubTypeElement.FromBasicCtype('ri_cpu_time_qos_background', KCSUBTYPE_TYPE.KC_ST_UINT64, 176),
1006 KCSubTypeElement.FromBasicCtype('ri_cpu_time_qos_utility', KCSUBTYPE_TYPE.KC_ST_UINT64, 184),
1007 KCSubTypeElement.FromBasicCtype('ri_cpu_time_qos_legacy', KCSUBTYPE_TYPE.KC_ST_UINT64, 192),
1008 KCSubTypeElement.FromBasicCtype('ri_cpu_time_qos_user_initiated', KCSUBTYPE_TYPE.KC_ST_UINT64, 200),
1009 KCSubTypeElement.FromBasicCtype('ri_cpu_time_qos_user_interactiv', KCSUBTYPE_TYPE.KC_ST_UINT64, 208),
1010 KCSubTypeElement.FromBasicCtype('ri_billed_system_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 216),
1011 KCSubTypeElement.FromBasicCtype('ri_serviced_system_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 224)
1012 ),
1013 'rusage_info')
1014
1015 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_CPU_TIMES')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_CPU_TIMES'),
1016 (
1017 KCSubTypeElement.FromBasicCtype('user_usec', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1018 KCSubTypeElement.FromBasicCtype('system_usec', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1019 ), 'cpu_times')
1020
1021 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_STACKSHOT_DURATION')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_STACKSHOT_DURATION'),
1022 (
1023 KCSubTypeElement.FromBasicCtype('stackshot_duration', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1024 KCSubTypeElement.FromBasicCtype('stackshot_duration_outer', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1025 ), 'stackshot_duration', merge=True
1026 )
1027
1028 KNOWN_TYPES_COLLECTION[GetTypeForName('KCDATA_TYPE_PROCNAME')] = (
1029 KCSubTypeElement("proc_name", KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(-1, 1), 0, 1))
1030
1031 KNOWN_TYPES_COLLECTION[GetTypeForName('KCDATA_TYPE_PID')] = (
1032 KCSubTypeElement('pid', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0))
1033
1034 KNOWN_TYPES_COLLECTION[GetTypeForName('EXIT_REASON_SNAPSHOT')] = KCTypeDescription(GetTypeForName('EXIT_REASON_SNAPSHOT'),
1035 (
1036 KCSubTypeElement.FromBasicCtype('ers_namespace', KCSUBTYPE_TYPE.KC_ST_UINT32, 0),
1037 KCSubTypeElement.FromBasicCtype('ers_code', KCSUBTYPE_TYPE.KC_ST_UINT64, 4),
1038 KCSubTypeElement.FromBasicCtype('ers_flags', KCSUBTYPE_TYPE.KC_ST_UINT64, 12),
1039 ), 'exit_reason_basic_info')
1040
1041 KNOWN_TYPES_COLLECTION[GetTypeForName('EXIT_REASON_USER_DESC')] = (
1042 KCSubTypeElement("exit_reason_user_description", KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(-1, 1), 0, 1))
1043
1044 KNOWN_TYPES_COLLECTION[GetTypeForName('EXIT_REASON_USER_PAYLOAD')] = KCSubTypeElement('exit_reason_user_payload',
1045 KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(-1, 1), 0, 1)
1046
1047 KNOWN_TYPES_COLLECTION[GetTypeForName('EXIT_REASON_CODESIGNING_INFO')] = KCTypeDescription(GetTypeForName('EXIT_REASON_CODESIGNING_INFO'),
1048 (
1049 KCSubTypeElement.FromBasicCtype('ceri_virt_addr', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1050 KCSubTypeElement.FromBasicCtype('ceri_file_offset', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1051 KCSubTypeElement("ceri_pathname", KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(1024, 1), 16, 1),
1052 KCSubTypeElement("ceri_filename", KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(1024, 1), 1040, 1),
1053 KCSubTypeElement.FromBasicCtype('ceri_codesig_modtime_secs', KCSUBTYPE_TYPE.KC_ST_UINT64, 2064),
1054 KCSubTypeElement.FromBasicCtype('ceri_codesig_modtime_nsecs', KCSUBTYPE_TYPE.KC_ST_UINT64, 2072),
1055 KCSubTypeElement.FromBasicCtype('ceri_page_modtime_secs', KCSUBTYPE_TYPE.KC_ST_UINT64, 2080),
1056 KCSubTypeElement.FromBasicCtype('ceri_page_modtime_nsecs', KCSUBTYPE_TYPE.KC_ST_UINT64, 2088),
1057 KCSubTypeElement.FromBasicCtype('ceri_path_truncated', KCSUBTYPE_TYPE.KC_ST_UINT8, 2096),
1058 KCSubTypeElement.FromBasicCtype('ceri_object_codesigned', KCSUBTYPE_TYPE.KC_ST_UINT8, 2097),
1059 KCSubTypeElement.FromBasicCtype('ceri_page_codesig_validated', KCSUBTYPE_TYPE.KC_ST_UINT8, 2098),
1060 KCSubTypeElement.FromBasicCtype('ceri_page_codesig_tainted', KCSUBTYPE_TYPE.KC_ST_UINT8, 2099),
1061 KCSubTypeElement.FromBasicCtype('ceri_page_codesig_nx', KCSUBTYPE_TYPE.KC_ST_UINT8, 2100),
1062 KCSubTypeElement.FromBasicCtype('ceri_page_wpmapped', KCSUBTYPE_TYPE.KC_ST_UINT8, 2101),
1063 KCSubTypeElement.FromBasicCtype('ceri_page_slid', KCSUBTYPE_TYPE.KC_ST_UINT8, 2102),
1064 KCSubTypeElement.FromBasicCtype('ceri_page_dirty', KCSUBTYPE_TYPE.KC_ST_UINT8, 2103),
1065 KCSubTypeElement.FromBasicCtype('ceri_page_shadow_depth', KCSUBTYPE_TYPE.KC_ST_UINT32, 2104),
1066 ), 'exit_reason_codesigning_info')
1067
1068
1069 def GetSecondsFromMATime(mat, tb):
1070 return (float(mat) * tb['numer']) / tb['denom']
1071
1072 def FindLibraryForAddress(liblist, address):
1073 current_lib = None
1074 for l in liblist:
1075 if address >= l[1]:
1076 current_lib = l
1077 return current_lib
1078
1079 def FindIndexOfLibInCatalog(catalog, lib):
1080 index = None
1081 i = 0
1082 for l in catalog:
1083 if l[0] == lib[0] and l[1] == lib[1]:
1084 index = i
1085 break
1086 i += 1
1087
1088 if index is None:
1089 catalog.append(lib)
1090 index = len(catalog) - 1
1091
1092 return index
1093
1094 def GetOffsetOfAddressForLib(lib, address):
1095 return (address - lib[1])
1096
1097 def GetSymbolInfoForFrame(catalog, liblist, address):
1098 lib = FindLibraryForAddress(liblist, address)
1099 if not lib:
1100 lib = ["00000000000000000000000000000000",0,"A"]
1101 offset = GetOffsetOfAddressForLib(lib, address)
1102 index = FindIndexOfLibInCatalog(catalog, lib)
1103 return [index, offset]
1104
1105 def GetStateDescription(s):
1106 retval = []
1107 TH_WAIT = 0x01
1108 TH_SUSP = 0x02
1109 TH_RUN = 0x04
1110 TH_UNINT = 0x08
1111 TH_TERMINATE = 0x10
1112 TH_TERMINATE2 = 0x20
1113 TH_IDLE = 0x80
1114 if (s & TH_WAIT):
1115 retval.append("TH_WAIT")
1116 if (s & TH_SUSP):
1117 retval.append("TH_SUSP")
1118 if (s & TH_RUN):
1119 retval.append("TH_RUN")
1120 if (s & TH_UNINT):
1121 retval.append("TH_UNINT")
1122 if (s & TH_TERMINATE):
1123 retval.append("TH_TERMINATE")
1124 if (s & TH_TERMINATE2):
1125 retval.append("TH_TERMINATE2")
1126 if (s & TH_IDLE):
1127 retval.append("TH_IDLE")
1128 return retval
1129
1130
1131 def format_uuid(elementValues):
1132 return ''.join("%02x" % i for i in elementValues)
1133
1134 def SaveStackshotReport(j, outfile_name, dsc_uuid, dsc_libs_arr):
1135 import time
1136 from operator import itemgetter, attrgetter
1137 ss = j.get('kcdata_stackshot')
1138 if not ss:
1139 print "No KCDATA_BUFFER_BEGIN_STACKSHOT object found. Skipping writing report."
1140 return
1141 timestamp = ss.get('usecs_since_epoch', int(time.time()))
1142 timestamp = time.strftime("%Y-%m-%d %H:%M:%S %z",time.gmtime(timestamp))
1143 os_version = ss.get('osversion', 'Unknown')
1144 timebase = ss.get('mach_timebase_info', {"denom": 1, "numer": 1})
1145 if not dsc_uuid and 'imageSlidBaseAddress' not in ss.get('shared_cache_dyld_load_info'):
1146 print "Stackshot format does not include slid shared cache base address and no UUID provided. Skipping writing report."
1147 return
1148
1149 # If a shared cache UUID is provided, treat the slide as the base address
1150 # for compatibility with existing tools that operate based on this logic
1151 if dsc_uuid:
1152 shared_cache_base_addr = ss.get('shared_cache_dyld_load_info')['imageLoadAddress']
1153 elif 'imageSlidBaseAddress' in ss.get('shared_cache_dyld_load_info'):
1154 shared_cache_base_addr = ss.get('shared_cache_dyld_load_info')['imageSlidBaseAddress']
1155 else:
1156 print "No shared cache UUID provided and data doesn't include imageSlidBaseAddress. Skipping writing report."
1157 return
1158
1159 dsc_common = [format_uuid(ss.get('shared_cache_dyld_load_info')['imageUUID']),
1160 shared_cache_base_addr,
1161 "S"
1162 ]
1163
1164 dsc_libs = []
1165 print "Shared cache UUID found from the binary data is <%s> " % str(dsc_common[0])
1166 if dsc_common[0].replace('-', '').lower() == dsc_uuid:
1167 print "SUCCESS: Found Matching dyld shared cache uuid. Loading library load addresses from layout provided."
1168 _load_addr = dsc_common[1]
1169 #print _load_addr
1170 #print dsc_libs_arr
1171 for i in dsc_libs_arr:
1172 _uuid = i[2].lower().replace('-','').strip()
1173 _addr = int(i[0], 16) + _load_addr
1174 dsc_libs.append([_uuid, _addr, "P"])
1175 #print "adding ", [_uuid, _addr, "C"]
1176 elif dsc_uuid:
1177 print "Provided shared cache UUID does not match. Skipping writing report."
1178 return
1179
1180 AllImageCatalog = []
1181 obj = {}
1182 obj["kernel"] = os_version
1183 obj["date"] = timestamp
1184 obj["reason"] = "kernel panic stackshot"
1185 obj["incident"] = "ABCDEFGH-1234-56IJ-789K-0LMNOPQRSTUV"
1186 obj["crashReporterKey"] = "12ab34cd45aabbccdd6712ab34cd45aabbccdd67"
1187 obj["bootArgs"] = ss.get('boot_args','')
1188 obj["frontmostPids"] = [0]
1189 obj["exception"] = "0xDEADF157"
1190 obj["processByPid"] = {}
1191 processByPid = obj["processByPid"]
1192 ssplist = ss.get('task_snapshots', {})
1193 kern_load_info = []
1194 if "0" in ssplist:
1195 kc_uuid = ssplist["0"].get('kernelcache_load_info', None)
1196 if kc_uuid:
1197 kernelcache_uuid = [format_uuid(kc_uuid['imageUUID']), kc_uuid['imageLoadAddress'], "U" ]
1198 kern_load_info.append(kernelcache_uuid)
1199
1200 kl_infos = ssplist["0"].get("dyld_load_info", [])
1201 for dlinfo in kl_infos:
1202 kern_load_info.append([format_uuid(dlinfo['imageUUID']), dlinfo['imageLoadAddress'], "K"])
1203 for pid,piddata in ssplist.iteritems():
1204 processByPid[str(pid)] = {}
1205 tsnap = processByPid[str(pid)]
1206 pr_lib_dsc = dsc_common
1207 if 'shared_cache_dyld_load_info' in tsnap:
1208 if 'imageSlidBaseAddress' in tsnap.get('shared_cache_dyld_load_info'):
1209 shared_cache_base_addr = tsnap.get('shared_cache_dyld_load_info')['imageSlidBaseAddress']
1210 else:
1211 print "Specific task shared cache format does not include slid shared cache base address. Skipping writing report."
1212 return
1213
1214 pr_lib_dsc = [format_uuid(tsnap.get('shared_cache_dyld_load_info')['imageUUID']),
1215 tsnap.get('shared_cache_dyld_load_info')['imageSlidBaseAddress'],
1216 "S"
1217 ]
1218
1219 pr_libs = []
1220 if len(dsc_libs) == 0:
1221 pr_libs.append(pr_lib_dsc)
1222 _lib_type = "P"
1223 if int(pid) == 0:
1224 _lib_type = "K"
1225 pr_libs = []
1226 else:
1227 for dlinfo in piddata.get('dyld_load_info',[]):
1228 pr_libs.append([format_uuid(dlinfo['imageUUID']), dlinfo['imageLoadAddress'], _lib_type])
1229
1230 pr_libs.extend(kern_load_info)
1231 pr_libs.extend(dsc_libs)
1232
1233 pr_libs.sort(key=itemgetter(1))
1234
1235 tasksnap = piddata['task_snapshot']
1236 tsnap["pid"] = tasksnap["ts_pid"]
1237 tsnap["residentMemoryBytes"] = tasksnap["ts_task_size"]
1238 tsnap["timesDidThrottle"] = tasksnap["ts_did_throttle"]
1239 tsnap["systemTimeTask"] = GetSecondsFromMATime(tasksnap["ts_system_time_in_terminated_th"], timebase)
1240 tsnap["pageIns"] = tasksnap["ts_pageins"]
1241 tsnap["pageFaults"] = tasksnap["ts_faults"]
1242 tsnap["userTimeTask"] = GetSecondsFromMATime(tasksnap[ "ts_user_time_in_terminated_thre"], timebase)
1243 tsnap["procname"] = tasksnap["ts_p_comm"]
1244 tsnap["copyOnWriteFaults"] = tasksnap["ts_cow_faults"]
1245 tsnap["timesThrottled"] = tasksnap["ts_was_throttled"]
1246 tsnap["threadById"] = {}
1247 threadByID = tsnap["threadById"]
1248 thlist = piddata.get('thread_snapshots', {})
1249 for tid,thdata in thlist.iteritems():
1250 threadByID[str(tid)] = {}
1251 thsnap = threadByID[str(tid)]
1252 if "thread_snapshot" not in thdata:
1253 print "Found broken thread state for thread ID: %s." % tid
1254 break
1255 threadsnap = thdata["thread_snapshot"]
1256 thsnap["userTime"] = GetSecondsFromMATime(threadsnap["ths_user_time"], timebase)
1257 thsnap["id"] = threadsnap["ths_thread_id"]
1258 thsnap["basePriority"] = threadsnap["ths_base_priority"]
1259 thsnap["systemTime"] = threadsnap["ths_sys_time"]
1260 thsnap["schedPriority"] = threadsnap["ths_sched_priority"]
1261 thsnap["state"] = GetStateDescription(threadsnap['ths_state'])
1262 thsnap["qosEffective"] = threadsnap["ths_eqos"]
1263 thsnap["qosRequested"] = threadsnap["ths_rqos"]
1264
1265 if threadsnap['ths_continuation']:
1266 thsnap["continuation"] = GetSymbolInfoForFrame(AllImageCatalog, pr_libs, threadsnap['ths_continuation'])
1267 if "kernel_stack_frames" in thdata:
1268 kuserframes = []
1269 for f in thdata["kernel_stack_frames"]:
1270 kuserframes.append(GetSymbolInfoForFrame(AllImageCatalog, pr_libs, f['lr']))
1271 thsnap["kernelFrames"] = kuserframes
1272
1273 if "user_stack_frames" in thdata:
1274 uframes = []
1275 for f in thdata["user_stack_frames"]:
1276 uframes.append(GetSymbolInfoForFrame(AllImageCatalog, pr_libs, f['lr']))
1277 thsnap["userFrames"] = uframes
1278 if threadsnap['ths_wait_event']:
1279 thsnap["waitEvent"] = GetSymbolInfoForFrame(AllImageCatalog, pr_libs, threadsnap['ths_wait_event'])
1280
1281 obj['binaryImages'] = AllImageCatalog
1282 fh = open(outfile_name, "w")
1283 fh.write('{"bug_type":"288", "timestamp":"'+ timestamp +'", "os_version":"'+ os_version +'"}\n')
1284 fh.write(json.dumps(obj, sort_keys=False, indent=2, separators=(',', ': ')))
1285 fh.close()
1286
1287 ## Base utils for interacting with shell ##
1288 def RunCommand(bash_cmd_string, get_stderr = True):
1289 """
1290 returns: (int,str) : exit_code and output_str
1291 """
1292 print "RUNNING: %s" % bash_cmd_string
1293 cmd_args = shlex.split(bash_cmd_string)
1294 output_str = ""
1295 exit_code = 0
1296 try:
1297 if get_stderr:
1298 output_str = subprocess.check_output(cmd_args, stderr=subprocess.STDOUT)
1299 else:
1300 output_str = subprocess.check_output(cmd_args, stderr=None)
1301 except subprocess.CalledProcessError, e:
1302 exit_code = e.returncode
1303 finally:
1304 return (exit_code, output_str)
1305
1306 def ProcessDyldSharedCacheFile(shared_cache_file_path, sdk_str=""):
1307 """ returns (uuid, text_info) output from shared_cache_util.
1308 In case of error None is returned and err message is printed to stdout.
1309 """
1310 if not os.path.exists(shared_cache_file_path):
1311 print "File path: %s does not exists" % shared_cache_file_path
1312 return None
1313 if sdk_str:
1314 sdk_str = ' -sdk "%s" ' % sdk_str
1315 (c, so) = RunCommand("xcrun {} -find dyld_shared_cache_util".format(sdk_str))
1316 if c:
1317 print "Failed to find path to dyld_shared_cache_util. Exit code: %d , message: %s" % (c,so)
1318 return None
1319 dyld_shared_cache_util = so.strip()
1320 (c, so) = RunCommand("{} -info {}".format(dyld_shared_cache_util, shared_cache_file_path))
1321 if c:
1322 print "Failed to get uuid info from %s" % shared_cache_file_path
1323 print so
1324 return None
1325
1326 uuid = so.splitlines()[0].split(": ")[-1].strip().replace("-","").lower()
1327
1328 (c, so) = RunCommand("{} -text_info {}".format(dyld_shared_cache_util, shared_cache_file_path))
1329 if c:
1330 print "Failed to get text_info from %s" % shared_cache_file_path
1331 print so
1332 return None
1333
1334 print "Found %s uuid: %s" % (shared_cache_file_path, uuid)
1335 text_info = so
1336
1337 return (uuid, so)
1338
1339 parser = argparse.ArgumentParser(description="Decode a kcdata binary file.")
1340 parser.add_argument("-l", "--listtypes", action="store_true", required=False, default=False,
1341 help="List all known types",
1342 dest="list_known_types")
1343
1344 parser.add_argument("-s", "--stackshot", required=False, default=False,
1345 help="Generate a stackshot report file",
1346 dest="stackshot_file")
1347
1348 parser.add_argument("--multiple", help="look for multiple stackshots in a single file", action='store_true')
1349
1350 parser.add_argument("-p", "--plist", required=False, default=False,
1351 help="output as plist", action="store_true")
1352
1353 parser.add_argument("-U", "--uuid", required=False, default="", help="UUID of dyld shared cache to be analysed and filled in libs of stackshot report", dest="uuid")
1354 parser.add_argument("-L", "--layout", required=False, type=argparse.FileType("r"), help="Path to layout file for DyldSharedCache. You can generate one by doing \n\tbash$xcrun -sdk <sdk> dyld_shared_cache_util -text_info </path/to/dyld_shared_cache> ", dest="layout")
1355 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")
1356 parser.add_argument("-D", "--dyld_shared_cache", required=False, default="", help="Path to dyld_shared_cache built by B&I", dest="dsc")
1357 parser.add_argument("kcdata_file", type=argparse.FileType('r'), help="Path to a kcdata binary file.")
1358
1359 class VerboseAction(argparse.Action):
1360 def __call__(self, parser, namespace, values, option_string=None):
1361 logging.basicConfig(level=logging.INFO, stream=sys.stderr, format='%(message)s')
1362 parser.add_argument('-v', "--verbose", action=VerboseAction, nargs=0)
1363
1364 @contextlib.contextmanager
1365 def data_from_stream(stream):
1366 try:
1367 fmap = mmap.mmap(stream.fileno(), 0, mmap.MAP_SHARED, mmap.PROT_READ)
1368 except:
1369 yield stream.read()
1370 else:
1371 try:
1372 yield fmap
1373 finally:
1374 fmap.close()
1375
1376 def iterate_kcdatas(kcdata_file):
1377 with data_from_stream(kcdata_file) as data:
1378 iterator = kcdata_item_iterator(data)
1379 kcdata_buffer = KCObject.FromKCItem(iterator.next())
1380 if not isinstance(kcdata_buffer, KCBufferObject):
1381 try:
1382 decoded = base64.b64decode(data)
1383 except:
1384 pass
1385 else:
1386 iterator = kcdata_item_iterator(decoded)
1387 kcdata_buffer = KCObject.FromKCItem(iterator.next())
1388 if not isinstance(kcdata_buffer, KCBufferObject):
1389 import gzip
1390 from io import BytesIO
1391 try:
1392 decompressed = gzip.GzipFile(fileobj=BytesIO(data[:])).read()
1393 except:
1394 pass
1395 else:
1396 iterator = kcdata_item_iterator(decompressed)
1397 kcdata_buffer = KCObject.FromKCItem(iterator.next())
1398 if not isinstance(kcdata_buffer, KCBufferObject):
1399 raise Exception, "unknown file type"
1400
1401 kcdata_buffer.ReadItems(iterator)
1402 yield kcdata_buffer
1403
1404 for magic in iterator:
1405 kcdata_buffer = KCObject.FromKCItem(magic)
1406 if not isinstance(kcdata_buffer, KCBufferObject):
1407 raise Exception, "unknown file type"
1408 kcdata_buffer.ReadItems(iterator)
1409 yield kcdata_buffer
1410
1411
1412
1413 if __name__ == '__main__':
1414 args = parser.parse_args()
1415
1416 if args.multiple and args.stackshot_file:
1417 raise NotImplementedError
1418
1419 if args.list_known_types:
1420 for (n, t) in KNOWN_TYPES_COLLECTION.items():
1421 print "%d : %s " % (n, str(t))
1422 sys.exit(1)
1423
1424 for i,kcdata_buffer in enumerate(iterate_kcdatas(args.kcdata_file)):
1425 if i > 0 and not args.multiple:
1426 break
1427
1428 str_data = "{" + kcdata_buffer.GetJsonRepr() + "}"
1429 str_data = str_data.replace("\t", " ")
1430
1431 try:
1432 json_obj = json.loads(str_data)
1433 except:
1434 print >>sys.stderr, "JSON reparsing failed! Printing string data!\n"
1435 import textwrap
1436 print textwrap.fill(str_data, 100)
1437 raise
1438
1439 dsc_uuid = None
1440 dsc_libs_arr = []
1441 libs_re = re.compile("^\s*(0x[a-fA-F0-9]+)\s->\s(0x[a-fA-F0-9]+)\s+<([a-fA-F0-9\-]+)>\s+.*$", re.MULTILINE)
1442 if args.uuid and args.layout:
1443 dsc_uuid = args.uuid.strip().replace("-",'').lower()
1444 dsc_libs_arr = libs_re.findall(args.layout.read())
1445
1446 if args.dsc:
1447 _ret = ProcessDyldSharedCacheFile(args.dsc, args.sdk)
1448 if _ret:
1449 dsc_uuid = _ret[0]
1450 dsc_libs_arr = libs_re.findall(_ret[1])
1451
1452 if args.stackshot_file:
1453 SaveStackshotReport(json_obj, args.stackshot_file, dsc_uuid, dsc_libs_arr)
1454 elif args.plist:
1455 import Foundation
1456 plist = Foundation.NSPropertyListSerialization.dataWithPropertyList_format_options_error_(
1457 json_obj, Foundation.NSPropertyListXMLFormat_v1_0, 0, None)[0].bytes().tobytes()
1458 #sigh. on some pythons long integers are getting output with L's in the plist.
1459 plist = re.sub(r'^(\s*<integer>\d+)L(</integer>\s*)$', r"\1\2", plist, flags=re.MULTILINE)
1460 print plist,
1461 else:
1462 print json.dumps(json_obj, sort_keys=True, indent=4, separators=(',', ': '))