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