19 class Globals(object):
22 G
.accept_incomplete_data
= False
23 G
.data_was_incomplete
= False
26 'KCDATA_TYPE_INVALID': 0x0,
27 'KCDATA_TYPE_STRING_DESC': 0x1,
28 'KCDATA_TYPE_UINT32_DESC': 0x2,
29 'KCDATA_TYPE_UINT64_DESC': 0x3,
30 'KCDATA_TYPE_INT32_DESC': 0x4,
31 'KCDATA_TYPE_INT64_DESC': 0x5,
32 'KCDATA_TYPE_BINDATA_DESC': 0x6,
33 'KCDATA_TYPE_ARRAY': 0x11,
34 'KCDATA_TYPE_TYPEDEFINITION': 0x12,
35 'KCDATA_TYPE_CONTAINER_BEGIN': 0x13,
36 'KCDATA_TYPE_CONTAINER_END': 0x14,
38 'KCDATA_TYPE_ARRAY_PAD0': 0x20,
39 'KCDATA_TYPE_ARRAY_PAD1': 0x21,
40 'KCDATA_TYPE_ARRAY_PAD2': 0x22,
41 'KCDATA_TYPE_ARRAY_PAD3': 0x23,
42 'KCDATA_TYPE_ARRAY_PAD4': 0x24,
43 'KCDATA_TYPE_ARRAY_PAD5': 0x25,
44 'KCDATA_TYPE_ARRAY_PAD6': 0x26,
45 'KCDATA_TYPE_ARRAY_PAD7': 0x27,
46 'KCDATA_TYPE_ARRAY_PAD8': 0x28,
47 'KCDATA_TYPE_ARRAY_PAD9': 0x29,
48 'KCDATA_TYPE_ARRAY_PADa': 0x2a,
49 'KCDATA_TYPE_ARRAY_PADb': 0x2b,
50 'KCDATA_TYPE_ARRAY_PADc': 0x2c,
51 'KCDATA_TYPE_ARRAY_PADd': 0x2d,
52 'KCDATA_TYPE_ARRAY_PADe': 0x2e,
53 'KCDATA_TYPE_ARRAY_PADf': 0x2f,
55 'KCDATA_TYPE_LIBRARY_LOADINFO': 0x30,
56 'KCDATA_TYPE_LIBRARY_LOADINFO64': 0x31,
57 'KCDATA_TYPE_TIMEBASE': 0x32,
58 'KCDATA_TYPE_MACH_ABSOLUTE_TIME': 0x33,
59 'KCDATA_TYPE_TIMEVAL': 0x34,
60 'KCDATA_TYPE_USECS_SINCE_EPOCH': 0x35,
61 'KCDATA_TYPE_PID': 0x36,
62 'KCDATA_TYPE_PROCNAME': 0x37,
63 'KCDATA_TYPE_NESTED_KCDATA': 0x38,
65 'STACKSHOT_KCCONTAINER_TASK': 0x903,
66 'STACKSHOT_KCCONTAINER_THREAD': 0x904,
67 'STACKSHOT_KCTYPE_DONATING_PIDS': 0x907,
68 'STACKSHOT_KCTYPE_SHAREDCACHE_LOADINFO': 0x908,
69 'STACKSHOT_KCTYPE_THREAD_NAME': 0x909,
70 'STACKSHOT_KCTYPE_KERN_STACKFRAME': 0x90A,
71 'STACKSHOT_KCTYPE_KERN_STACKFRAME64': 0x90B,
72 'STACKSHOT_KCTYPE_USER_STACKFRAME': 0x90C,
73 'STACKSHOT_KCTYPE_USER_STACKFRAME64': 0x90D,
74 'STACKSHOT_KCTYPE_BOOTARGS': 0x90E,
75 'STACKSHOT_KCTYPE_OSVERSION': 0x90F,
76 'STACKSHOT_KCTYPE_KERN_PAGE_SIZE': 0x910,
77 'STACKSHOT_KCTYPE_JETSAM_LEVEL': 0x911,
78 'STACKSHOT_KCTYPE_DELTA_SINCE_TIMESTAMP': 0x912,
79 'STACKSHOT_KCTYPE_KERN_STACKLR': 0x913,
80 'STACKSHOT_KCTYPE_KERN_STACKLR64': 0x914,
81 'STACKSHOT_KCTYPE_USER_STACKLR': 0x915,
82 'STACKSHOT_KCTYPE_USER_STACKLR64': 0x916,
83 'STACKSHOT_KCTYPE_NONRUNNABLE_TIDS': 0x917,
84 'STACKSHOT_KCTYPE_NONRUNNABLE_TASKS': 0x918,
85 'STACKSHOT_KCTYPE_CPU_TIMES': 0x919,
86 'STACKSHOT_KCTYPE_STACKSHOT_DURATION': 0x91a,
87 'STACKSHOT_KCTYPE_STACKSHOT_FAULT_STATS': 0x91b,
88 'STACKSHOT_KCTYPE_KERNELCACHE_LOADINFO': 0x91c,
89 'STACKSHOT_KCTYPE_THREAD_WAITINFO' : 0x91d,
90 'STACKSHOT_KCTYPE_THREAD_GROUP_SNAPSHOT' : 0x91e,
91 'STACKSHOT_KCTYPE_THREAD_GROUP' : 0x91f,
92 'STACKSHOT_KCTYPE_JETSAM_COALITION_SNAPSHOT' : 0x920,
93 'STACKSHOT_KCTYPE_JETSAM_COALITION' : 0x921,
94 'STACKSHOT_KCTYPE_THREAD_POLICY_VERSION': 0x922,
95 'STACKSHOT_KCTYPE_INSTRS_CYCLES' : 0x923,
96 'STACKSHOT_KCTYPE_USER_STACKTOP' : 0x924,
97 'STACKSHOT_KCTYPE_ASID' : 0x925,
98 'STACKSHOT_KCTYPE_PAGE_TABLES' : 0x926,
99 'STACKSHOT_KCTYPE_SYS_SHAREDCACHE_LAYOUT' : 0x927,
100 'STACKSHOT_KCTYPE_THREAD_DISPATCH_QUEUE_LABEL' : 0x928,
101 'STACKSHOT_KCTYPE_THREAD_TURNSTILEINFO' : 0x929,
102 'STACKSHOT_KCTYPE_TASK_CPU_ARCHITECTURE' : 0x92a,
103 'STACKSHOT_KCTYPE_LATENCY_INFO' : 0x92b,
104 'STACKSHOT_KCTYPE_LATENCY_INFO_TASK' : 0x92c,
105 'STACKSHOT_KCTYPE_LATENCY_INFO_THREAD' : 0x92d,
106 'STACKSHOT_KCTYPE_LOADINFO64_TEXT_EXEC' : 0x92e,
108 'STACKSHOT_KCTYPE_TASK_DELTA_SNAPSHOT': 0x940,
109 'STACKSHOT_KCTYPE_THREAD_DELTA_SNAPSHOT': 0x941,
113 'KCDATA_TYPE_BUFFER_END': 0xF19158ED,
116 'TASK_CRASHINFO_EXTMODINFO': 0x801,
117 'TASK_CRASHINFO_BSDINFOWITHUNIQID': 0x802,
118 'TASK_CRASHINFO_TASKDYLD_INFO': 0x803,
119 'TASK_CRASHINFO_UUID': 0x804,
120 'TASK_CRASHINFO_PID': 0x805,
121 'TASK_CRASHINFO_PPID': 0x806,
123 # Don't want anyone using this. It's struct rusage from whatever machine generated the data
124 #'TASK_CRASHINFO_RUSAGE': 0x807,
127 'TASK_CRASHINFO_RUSAGE_INFO': 0x808,
128 'TASK_CRASHINFO_PROC_NAME': 0x809,
129 'TASK_CRASHINFO_PROC_STARTTIME': 0x80B,
130 'TASK_CRASHINFO_USERSTACK': 0x80C,
131 'TASK_CRASHINFO_ARGSLEN': 0x80D,
132 'TASK_CRASHINFO_EXCEPTION_CODES': 0x80E,
133 'TASK_CRASHINFO_PROC_PATH': 0x80F,
134 'TASK_CRASHINFO_PROC_CSFLAGS': 0x810,
135 'TASK_CRASHINFO_PROC_STATUS': 0x811,
136 'TASK_CRASHINFO_UID': 0x812,
137 'TASK_CRASHINFO_GID': 0x813,
138 'TASK_CRASHINFO_PROC_ARGC': 0x814,
139 'TASK_CRASHINFO_PROC_FLAGS': 0x815,
140 'TASK_CRASHINFO_CPUTYPE': 0x816,
141 'TASK_CRASHINFO_WORKQUEUEINFO': 0x817,
142 'TASK_CRASHINFO_RESPONSIBLE_PID': 0x818,
143 'TASK_CRASHINFO_DIRTY_FLAGS': 0x819,
144 'TASK_CRASHINFO_CRASHED_THREADID': 0x81A,
145 'TASK_CRASHINFO_COALITION_ID': 0x81B,
146 'EXIT_REASON_SNAPSHOT': 0x1001,
147 'EXIT_REASON_USER_DESC': 0x1002,
148 'EXIT_REASON_USER_PAYLOAD': 0x1003,
149 'EXIT_REASON_CODESIGNING_INFO': 0x1004,
150 'EXIT_REASON_WORKLOOP_ID': 0x1005,
151 'EXIT_REASON_DISPATCH_QUEUE_NO': 0x1006,
152 'KCDATA_BUFFER_BEGIN_CRASHINFO': 0xDEADF157,
153 'KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT': 0xDE17A59A,
154 'KCDATA_BUFFER_BEGIN_STACKSHOT': 0x59a25807,
155 'KCDATA_BUFFER_BEGIN_COMPRESSED': 0x434f4d50,
156 'KCDATA_BUFFER_BEGIN_OS_REASON': 0x53A20900,
157 'KCDATA_BUFFER_BEGIN_XNUPOST_CONFIG': 0x1E21C09F
159 kcdata_type_def_rev
= dict((v
, k
) for k
, v
in kcdata_type_def
.iteritems())
161 KNOWN_TYPES_COLLECTION
= {}
163 KNOWN_TOPLEVEL_CONTAINER_TYPES
= ()
166 return type('enum', (), args
)
168 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)
171 LEGAL_OLD_STYLE_ARRAY_TYPE_NAMES
= ['KCDATA_TYPE_LIBRARY_LOADINFO',
172 'KCDATA_TYPE_LIBRARY_LOADINFO64',
173 'STACKSHOT_KCTYPE_KERN_STACKFRAME',
174 'STACKSHOT_KCTYPE_USER_STACKFRAME',
175 'STACKSHOT_KCTYPE_KERN_STACKFRAME64',
176 'STACKSHOT_KCTYPE_USER_STACKFRAME64',
177 'STACKSHOT_KCTYPE_DONATING_PIDS',
178 'STACKSHOT_KCTYPE_THREAD_DELTA_SNAPSHOT']
180 KCDATA_FLAGS_STRUCT_PADDING_MASK
= 0xf
181 KCDATA_FLAGS_STRUCT_HAS_PADDING
= 0x80
183 class KCSubTypeElement(object):
184 """convert kcdata_subtype_descriptor to """
185 _unpack_formats
= (None, 'c', 'b', 'B', 'h', 'H', 'i', 'I', 'q', 'Q')
186 _ctypes
= ('Unknown', 'char', 'int8_t', 'uint8_t', 'int16_t', 'uint16_t', 'int32_t', 'uint32_t', 'int64_t', 'uint64_t')
188 def __init__(self
, st_name
, st_type
, st_size
, st_offset
=0, st_flag
=0, custom_repr
=None):
190 self
.offset
= st_offset
191 self
.type_id
= st_type
192 if st_type
<= 0 or st_type
> KCSUBTYPE_TYPE
.KC_ST_UINT64
:
193 raise ValueError("Invalid type passed %d" % st_type
)
194 self
.unpack_fmt
= KCSubTypeElement
._unpack
_formats
[self
.type_id
]
196 self
.totalsize
= st_size
198 self
.is_array_type
= False
199 self
.custom_JsonRepr
= custom_repr
200 if (st_flag
& 0x1) == 0x1:
201 self
.is_array_type
= True
202 self
.size
= st_size
& 0xffff
203 self
.count
= (st_size
>> 16) & 0xffff
204 self
.totalsize
= self
.size
* self
.count
207 def GetSizeForArray(el_count
, el_size
):
208 return ((el_count
& 0xffff) << 16) |
(el_size
& 0xffff)
211 def FromBinaryTypeData(byte_data
):
212 (st_flag
, st_type
, st_offset
, st_size
, st_name
) = struct
.unpack_from('=BBHI32s', byte_data
)
213 st_name
= st_name
.rstrip('\x00')
214 return KCSubTypeElement(st_name
, st_type
, st_size
, st_offset
, st_flag
)
217 def FromBasicCtype(st_name
, st_type
, st_offset
=0, legacy_size
=None):
218 if st_type
<= 0 or st_type
> KCSUBTYPE_TYPE
.KC_ST_UINT64
:
219 raise ValueError("Invalid type passed %d" % st_type
)
220 st_size
= struct
.calcsize(KCSubTypeElement
._unpack
_formats
[st_type
])
222 retval
= KCSubTypeElement(st_name
, st_type
, st_size
, st_offset
, st_flag
, KCSubTypeElement
._get
_naked
_element
_value
)
224 retval
.legacy_size
= legacy_size
228 def FromKCSubTypeElement(other
, name_override
=''):
229 _copy
= copy
.copy(other
)
231 _copy
.name
= name_override
237 def GetTotalSize(self
):
238 return self
.totalsize
240 def GetValueAsString(self
, base_data
, array_pos
=0):
241 return str(self
.GetValue(base_data
, array_pos
))
243 def GetValue(self
, base_data
, array_pos
=0):
244 return struct
.unpack_from(self
.unpack_fmt
, base_data
[self
.offset
+ (array_pos
* self
.size
):])[0]
247 def _get_naked_element_value(elementValue
, elementName
):
248 return json
.dumps(elementValue
)
251 if self
.is_array_type
:
252 return '[%d,%d] %s %s[%d];' % (self
.offset
, self
.totalsize
, self
.GetCTypeDesc(), self
.name
, self
.count
)
253 return '[%d,%d] %s %s;' % (self
.offset
, self
.totalsize
, self
.GetCTypeDesc(), self
.name
)
258 def GetCTypeDesc(self
):
259 return KCSubTypeElement
._ctypes
[self
.type_id
]
261 def GetStringRepr(self
, base_data
):
262 if not self
.is_array_type
:
263 return self
.GetValueAsString(base_data
)
264 if self
.type_id
== KCSUBTYPE_TYPE
.KC_ST_CHAR
:
266 if len(base_data
) < str_len
:
267 str_len
= len(base_data
)
269 for i
in range(str_len
):
270 _v
= self
.GetValue(base_data
, i
)
273 str_arr
.append(self
.GetValueAsString(base_data
, i
))
274 return json
.dumps(''.join(str_arr
))
277 if count
> len(base_data
)/self
.size
:
278 count
= len(base_data
)/self
.size
280 o
= '[' + ','.join([self
.GetValueAsString(base_data
, i
) for i
in range(count
)]) + ']'
284 def GetJsonRepr(self
, base_data
, flags
=0):
285 if (flags
& (KCDATA_FLAGS_STRUCT_HAS_PADDING | KCDATA_FLAGS_STRUCT_PADDING_MASK
)) != 0:
286 padding
= (flags
& KCDATA_FLAGS_STRUCT_PADDING_MASK
)
288 base_data
= base_data
[:-padding
]
289 if self
.custom_JsonRepr
:
290 if self
.is_array_type
:
291 e_data
= [self
.GetValue(base_data
, i
) for i
in range(self
.count
)]
293 e_data
= self
.GetValue(base_data
)
294 return self
.custom_JsonRepr(e_data
, self
.name
)
295 return self
.GetStringRepr(base_data
)
298 return self
.totalsize
300 def ShouldSkip(self
, data
):
301 return len(data
) < self
.offset
+ self
.totalsize
303 def ShouldMerge(self
):
307 class KCTypeDescription(object):
308 def __init__(self
, t_type_id
, t_elements
=[], t_name
='anon', custom_repr
=None, legacy_size
=None, merge
=False, naked
=False):
309 self
.type_id
= t_type_id
310 self
.elements
= t_elements
313 self
.custom_JsonRepr
= custom_repr
315 self
.legacy_size
= legacy_size
318 for e
in self
.elements
:
319 self
.totalsize
+= e
.GetTotalSize()
321 def ValidateData(self
, base_data
):
322 if len(base_data
) >= self
.totalsize
:
333 o
= '%s {\n\t' % self.name + "\n\t".join([str(e) for e in self.elements]) + '\n};'
337 def FromKCTypeDescription(other
, t_type_id
, t_name
):
338 retval
= KCTypeDescription(t_type_id
, other
.elements
, t_name
, other
.custom_JsonRepr
,
339 legacy_size
=getattr(other
, 'legacy_size', None))
342 def ShouldMerge(self
):
345 def GetJsonRepr(self
, base_data
, flags
):
346 if (flags
& (KCDATA_FLAGS_STRUCT_HAS_PADDING | KCDATA_FLAGS_STRUCT_PADDING_MASK
)) != 0:
347 padding
= (flags
& KCDATA_FLAGS_STRUCT_PADDING_MASK
)
349 base_data
= base_data
[:-padding
]
350 elif hasattr(self
, 'legacy_size') and len(base_data
) == self
.legacy_size
+ ((-self
.legacy_size
) & 0xf):
351 base_data
= base_data
[:self
.legacy_size
]
352 if self
.custom_JsonRepr
:
353 return self
.custom_JsonRepr([e
.GetValue(base_data
) for e
in self
.elements
])
355 o
= ", ".join([e
.GetJsonRepr(base_data
) for e
in self
.elements
if not e
.ShouldSkip(base_data
)])
357 o
= ", ".join(['"%s": %s' % (e
.GetName(), e
.GetJsonRepr(base_data
)) for e
in self
.elements
if not e
.ShouldSkip(base_data
)])
363 return max(st
.totalsize
+ st
.offset
for st
in self
.elements
)
366 def GetTypeNameForKey(k
):
368 if k
in KNOWN_TYPES_COLLECTION
:
369 retval
= KNOWN_TYPES_COLLECTION
[k
].GetName()
370 elif k
in kcdata_type_def_rev
:
371 retval
= kcdata_type_def_rev
[k
]
375 def GetTypeForName(n
):
377 if n
in kcdata_type_def
:
378 ret
= kcdata_type_def
[n
]
382 LEGAL_OLD_STYLE_ARRAY_TYPES
= map(GetTypeForName
, LEGAL_OLD_STYLE_ARRAY_TYPE_NAMES
)
384 kcdata_type_def_rev
[GetTypeForName('KCDATA_BUFFER_BEGIN_STACKSHOT')] = 'kcdata_stackshot'
385 kcdata_type_def_rev
[GetTypeForName('KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT')] = 'kcdata_delta_stackshot'
386 kcdata_type_def_rev
[GetTypeForName('KCDATA_BUFFER_BEGIN_CRASHINFO')] = 'kcdata_crashinfo'
387 kcdata_type_def_rev
[GetTypeForName('KCDATA_BUFFER_BEGIN_OS_REASON')] = 'kcdata_reason'
388 kcdata_type_def_rev
[GetTypeForName('STACKSHOT_KCCONTAINER_TASK')] = 'task_snapshots'
389 kcdata_type_def_rev
[GetTypeForName('STACKSHOT_KCCONTAINER_THREAD')] = 'thread_snapshots'
390 kcdata_type_def_rev
[GetTypeForName('KCDATA_BUFFER_BEGIN_XNUPOST_CONFIG')] = 'xnupost_testconfig'
392 class Indent(object):
395 def __call__(self
, end
=False):
397 return " " * (self
.n
-4)
400 @contextlib.contextmanager
410 class KCObject(object):
412 def __init__(self
, type_code
, data
, offset
, flags
=0):
414 self
.i_type
= type_code
417 self
.i_size
= len(data
)
419 self
.obj_collection
= []
421 self
.is_container_type
= False
422 self
.is_array_type
= False
423 self
.is_naked_type
= False
424 self
.nested_kcdata
= None
425 self
.i_name
= GetTypeNameForKey(type_code
)
429 if self
.i_type
== GetTypeForName('KCDATA_TYPE_CONTAINER_BEGIN'):
430 self
.__class
__ = KCContainerObject
431 elif self
.i_type
== GetTypeForName('KCDATA_BUFFER_BEGIN_COMPRESSED'):
432 self
.__class
__ = KCCompressedBufferObject
433 elif self
.i_type
in KNOWN_TOPLEVEL_CONTAINER_TYPES
:
434 self
.__class
__ = KCBufferObject
436 self
.InitAfterParse()
439 return "<KCObject at 0x%x>" % self
.offset
441 def InitAfterParse(self
):
445 def FromKCItem(kcitem
):
446 return KCObject(kcitem
.i_type
, kcitem
.i_data
, kcitem
.i_offset
, kcitem
.i_flags
)
448 def IsContainerEnd(self
):
449 return self
.i_type
== GetTypeForName('KCDATA_TYPE_CONTAINER_END')
451 def IsBufferEnd(self
):
452 return self
.i_type
== GetTypeForName('KCDATA_TYPE_BUFFER_END')
455 return self
.is_array_type
457 def ShouldMerge(self
):
458 if self
.nested_kcdata
:
460 elif not self
.is_array_type
and self
.i_type
in KNOWN_TYPES_COLLECTION
:
461 return KNOWN_TYPES_COLLECTION
[self
.i_type
].ShouldMerge()
465 def GetJsonRepr(self
):
466 if self
.is_array_type
:
467 return '[' + ', '.join([i
.GetJsonRepr() for i
in self
.obj_collection
]) + ']'
468 if self
.i_type
in KNOWN_TYPES_COLLECTION
:
469 return KNOWN_TYPES_COLLECTION
[self
.i_type
].GetJsonRepr(self
.i_data
, self
.i_flags
)
470 if self
.is_naked_type
:
471 return json
.dumps(self
.obj
)
472 if self
.nested_kcdata
:
473 return self
.nested_kcdata
.GetJsonRepr()
475 raise NotImplementedError("Broken GetJsonRepr implementation")
480 if self
.i_type
== GetTypeForName('KCDATA_TYPE_CONTAINER_BEGIN'):
481 self
.obj
['uniqID'] = self
.i_flags
482 self
.i_name
= str(self
.obj
['uniqID'])
483 self
.obj
['typeID'] = struct
.unpack_from('I', self
.i_data
)[0]
484 logging
.info("0x%08x: %sCONTAINER: %s(%x)" % (self
.offset
, INDENT(), GetTypeNameForKey(self
.obj
['typeID']), self
.i_flags
))
486 elif self
.i_type
in (KNOWN_TOPLEVEL_CONTAINER_TYPES
):
487 self
.obj
['uniqID'] = self
.i_name
488 self
.obj
['typeID'] = self
.i_type
489 logging
.info("0x%08x: %s%s" % (self
.offset
, INDENT(), self
.i_name
))
491 elif self
.i_type
== GetTypeForName('KCDATA_TYPE_CONTAINER_END'):
492 self
.obj
['uniqID'] = self
.i_flags
493 logging
.info("0x%08x: %sEND" % (self
.offset
, INDENT(end
=True)))
495 elif self
.i_type
== GetTypeForName('KCDATA_TYPE_BUFFER_END'):
497 logging
.info("0x%08x: %sEND_BUFFER" % (self
.offset
, INDENT(end
=True)))
499 elif self
.i_type
== GetTypeForName('KCDATA_TYPE_UINT32_DESC'):
500 self
.is_naked_type
= True
501 u_d
= struct
.unpack_from('32sI', self
.i_data
)
502 self
.i_name
= u_d
[0].strip(chr(0))
504 logging
.info("0x%08x: %s%s" % (self
.offset
, INDENT(), self
.i_name
))
506 elif self
.i_type
== GetTypeForName('KCDATA_TYPE_UINT64_DESC'):
507 self
.is_naked_type
= True
508 u_d
= struct
.unpack_from('32sQ', self
.i_data
)
509 self
.i_name
= u_d
[0].strip(chr(0))
511 logging
.info("0x%08x: %s%s" % (self
.offset
, INDENT(), self
.i_name
))
513 elif self
.i_type
== GetTypeForName('KCDATA_TYPE_TYPEDEFINITION'):
514 self
.is_naked_type
= True
515 u_d
= struct
.unpack_from('II32s', self
.i_data
)
516 self
.obj
['name'] = u_d
[2].split(chr(0))[0]
517 self
.i_name
= "typedef[%s]" % self
.obj
['name']
518 self
.obj
['typeID'] = u_d
[0]
519 self
.obj
['numOfFields'] = u_d
[1]
521 for i
in range(u_d
[1]):
522 e
= KCSubTypeElement
.FromBinaryTypeData(self
.i_data
[40+(i
*40):])
523 element_arr
.append(e
)
524 type_desc
= KCTypeDescription(u_d
[0], element_arr
, self
.obj
['name'])
525 self
.obj
['fields'] = [str(e
) for e
in element_arr
]
526 KNOWN_TYPES_COLLECTION
[type_desc
.GetTypeID()] = type_desc
527 logging
.info("0x%08x: %s%s" % (self
.offset
, INDENT(), self
.i_name
))
529 elif self
.i_type
== GetTypeForName('KCDATA_TYPE_ARRAY'):
530 self
.is_array_type
= True
531 e_t
= (self
.i_flags
>> 32) & 0xffffffff
532 if e_t
not in LEGAL_OLD_STYLE_ARRAY_TYPES
:
533 raise Exception, "illegal old-style array type: %s (0x%x)" % (GetTypeNameForKey(e_t
), e_t
)
534 e_c
= self
.i_flags
& 0xffffffff
535 e_s
= KNOWN_TYPES_COLLECTION
[e_t
].legacy_size
536 if e_s
* e_c
> self
.i_size
:
537 raise Exception("array too small for its count")
538 self
.obj
['typeID'] = e_t
539 self
.i_name
= GetTypeNameForKey(e_t
)
541 self
.obj
['numOfElements'] = e_c
542 self
.obj
['sizeOfElement'] = e_s
543 logging
.info("0x%08x: %sARRAY: %s" % (self
.offset
, INDENT(), self
.i_name
))
544 #populate the array here by recursive creation of KCObject
545 with INDENT
.indent():
546 for _i
in range(e_c
):
547 _o
= KCObject(e_t
, self
.i_data
[(_i
* e_s
):(_i
* e_s
) + e_s
], self
.offset
+ _i
*e_s
)
548 self
.obj_collection
.append(_o
)
550 elif self
.i_type
>= GetTypeForName('KCDATA_TYPE_ARRAY_PAD0') and self
.i_type
<= GetTypeForName('KCDATA_TYPE_ARRAY_PADf'):
551 self
.is_array_type
= True
552 e_t
= (self
.i_flags
>> 32) & 0xffffffff
553 e_c
= self
.i_flags
& 0xffffffff
554 e_s
= (self
.i_size
- (self
.i_type
& 0xf)) / e_c
if e_c
!= 0 else None
555 self
.obj
['typeID'] = e_t
556 self
.i_name
= GetTypeNameForKey(e_t
)
558 self
.obj
['numOfElements'] = e_c
559 self
.obj
['sizeOfElement'] = e_s
560 logging
.info("0x%08x: %sARRAY: %s" % (self
.offset
, INDENT(), self
.i_name
))
561 #populate the array here by recursive creation of KCObject
562 with INDENT
.indent():
563 for _i
in range(e_c
):
564 _o
= KCObject(e_t
, self
.i_data
[(_i
* e_s
):(_i
* e_s
) + e_s
], self
.offset
+ _i
*e_s
)
565 self
.obj_collection
.append(_o
)
567 elif self
.i_type
== GetTypeForName('KCDATA_TYPE_NESTED_KCDATA'):
568 logging
.info("0x%08x: %sNESTED_KCDATA" % (self
.offset
, INDENT()))
569 with INDENT
.indent():
570 nested_iterator
= kcdata_item_iterator(self
.i_data
[:self
.i_size
])
571 nested_buffer
= KCObject
.FromKCItem(nested_iterator
.next())
572 if not isinstance(nested_buffer
, KCBufferObject
):
573 raise Exception, "nested buffer isn't a KCBufferObject"
574 nested_buffer
.ReadItems(nested_iterator
)
575 self
.nested_kcdata
= nested_buffer
577 elif self
.i_type
in KNOWN_TYPES_COLLECTION
:
578 self
.i_name
= KNOWN_TYPES_COLLECTION
[self
.i_type
].GetName()
579 self
.is_naked_type
= True
580 logging
.info("0x%08x: %s%s" % (self
.offset
, INDENT(), self
.i_name
))
582 self
.is_naked_type
= True
583 #self.obj = "data of len %d" % len(self.i_data)
584 #self.obj = ''.join(["%x" % ki for ki in struct.unpack('%dB' % len(self.i_data), self.i_data)])
585 self
.obj
= map(ord, self
.i_data
)
586 logging
.info("0x%08x: %s%s" % (self
.offset
, INDENT(), self
.i_name
))
589 class KCContainerObject(KCObject
):
590 def __init__(self
, *args
, **kwargs
):
593 def InitAfterParse(self
):
594 self
.obj_container_dict
= {}
595 self
.obj_nested_objs
= {}
597 def ShouldMerge(self
):
600 def GetJsonRepr(self
):
601 # o = '"%s"' % self.obj['uniqID'] + ' : { "typeID" : %d ,' % self.obj['typeID']
602 o
= '"%s"' % self
.obj
['uniqID'] + ' : { '
603 for (k
, v
) in self
.obj_container_dict
.items():
605 o
+= v
.GetJsonRepr() + ","
607 o
+= ' "%s" : ' % k
+ v
.GetJsonRepr() + ","
609 for (k
, v
) in self
.obj_nested_objs
.items():
610 o
+= '"%s" : {' % k + ",".join([vi.GetJsonRepr() for vi in v.values()]) + "} ,"
612 o = o.rstrip(',') + "}"
616 def AddObject(self, kco):
617 assert not kco.IsContainerEnd()
618 if isinstance(kco, KCContainerObject):
619 type_name = GetTypeNameForKey(kco.obj['typeID
'])
620 if type_name not in self.obj_nested_objs:
621 self.obj_nested_objs[type_name] = {}
622 self.obj_nested_objs[type_name][kco.i_name] = kco
624 if kco.i_name in self.obj_container_dict:
625 if kco.IsArray() and self.obj_container_dict[kco.i_name].IsArray():
626 self.obj_container_dict[kco.i_name].obj_collection.extend( kco.obj_collection )
628 self.obj_container_dict[kco.i_name] = kco
630 def IsEndMarker(self, o):
631 if not o.IsContainerEnd():
633 if o.i_flags != self.i_flags:
634 raise Exception, "container end marker doesn't match
"
637 no_end_message = "could
not find container end marker
"
639 def ReadItems(self, iterator):
641 with INDENT.indent():
643 o = KCObject.FromKCItem(i)
644 if self.IsEndMarker(o):
649 if isinstance(o, KCContainerObject):
650 o.ReadItems(iterator)
653 if G.accept_incomplete_data:
654 if not G.data_was_incomplete:
655 print >>sys.stderr, "kcdata
.py WARNING
: data
is incomplete
!"
656 G.data_was_incomplete = True
658 raise Exception, self.no_end_message
662 class KCBufferObject(KCContainerObject):
664 def IsEndMarker(self,o):
665 if o.IsContainerEnd():
666 raise Exception, "container end marker at the toplevel
"
667 return o.IsBufferEnd()
669 no_end_message = "could
not find
buffer end marker
"
671 class KCCompressedBufferObject(KCContainerObject):
673 def ReadItems(self, iterator):
675 with INDENT.indent():
677 o = KCObject.FromKCItem(i)
678 if self.IsEndMarker(o):
679 self.compressed_type = o.i_type
680 self.blob_start = o.offset + 16
683 self.header[o.i_name] = o.obj
685 def IsEndMarker(self, o):
686 return o.i_type in KNOWN_TOPLEVEL_CONTAINER_TYPES
688 def GetCompressedBlob(self, data):
689 if self.header['kcd_c_type'] != 1:
690 raise NotImplementedError
691 blob = data[self.blob_start:self.blob_start+self.header['kcd_c_totalout']]
692 if len(blob) != self.header['kcd_c_totalout']:
696 def Decompress(self, data):
697 start_marker = struct.pack('<IIII', self.compressed_type, 0, 0, 0)
698 end_marker = struct.pack('<IIII', GetTypeForName('KCDATA_TYPE_BUFFER_END'), 0, 0, 0)
699 decompressed = zlib.decompress(self.GetCompressedBlob(data))
700 if len(decompressed) != self.header['kcd_c_totalin']:
701 raise ValueError, "length of decompressed
: %d vs expected
%d" % (len(decompressed), self.header['kcd_c_totalin'])
702 alignbytes = b'\x00' * (-len(decompressed) % 16)
703 return start_marker + decompressed + alignbytes + end_marker
707 """ a basic kcdata_item type object.
709 header_size = 16 # (uint32_t + uint32_t + uint64_t)
711 def __init__(self, item_type, item_size, item_flags, item_data):
712 self.i_type = item_type
713 self.i_size = item_size
714 self.i_flags = item_flags
715 self.i_data = item_data
718 def __init__(self, barray, pos=0):
719 """ create an object by parsing data from bytes array
720 returns : obj - if data is readable
721 raises ValueError if something is not ok.
723 self.i_type = struct.unpack('I', barray[pos:pos+4])[0] # int.from_bytes(barray[pos:pos+4])
724 self.i_size = struct.unpack('I', barray[pos+4:pos+8])[0] # int.from_bytes(barray[pos+4:pos+8])
725 self.i_flags = struct.unpack('Q', barray[pos+8:pos+16])[0] # int.from_bytes(barray[pos+8:pos+16])
726 self.i_data = barray[pos+16: (pos + 16 + self.i_size)]
730 return self.i_size + KCData_item.header_size
732 def GetHeaderDescription(self):
733 outs = "type: 0x
%x size
: 0x
%x flags
: 0x
%x (%s)" % (self.i_type, self.i_size, self.i_flags, GetTypeNameForKey(self.i_type))
734 if not self.i_offset is None:
735 outs = "pos
: 0x
%x" % self.i_offset + outs
739 return self.GetHeaderDescription()
741 def kcdata_item_iterator(data):
744 while curpos < file_len:
745 item = KCData_item(data, curpos)
749 def _get_data_element(elementValues):
750 return json.dumps(elementValues[-1])
752 KNOWN_TOPLEVEL_CONTAINER_TYPES = map(GetTypeForName, ('KCDATA_BUFFER_BEGIN_COMPRESSED', 'KCDATA_BUFFER_BEGIN_CRASHINFO', 'KCDATA_BUFFER_BEGIN_STACKSHOT', 'KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT', 'KCDATA_BUFFER_BEGIN_OS_REASON','KCDATA_BUFFER_BEGIN_XNUPOST_CONFIG'))
754 KNOWN_TYPES_COLLECTION[GetTypeForName('KCDATA_TYPE_UINT32_DESC')] = KCTypeDescription(GetTypeForName('KCDATA_TYPE_UINT32_DESC'), (
755 KCSubTypeElement('desc', KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(32, 1), 0, 1),
756 KCSubTypeElement('data', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 32, 0)
758 'KCDATA_TYPE_UINT32_DESC',
762 KNOWN_TYPES_COLLECTION[GetTypeForName('KCDATA_TYPE_UINT64_DESC')] = KCTypeDescription(GetTypeForName('KCDATA_TYPE_UINT64_DESC'), (
763 KCSubTypeElement('desc', KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(32, 1), 0, 1),
764 KCSubTypeElement('data', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 32, 0)
766 'KCDATA_TYPE_UINT64_DESC',
770 KNOWN_TYPES_COLLECTION[GetTypeForName('KCDATA_TYPE_TIMEBASE')] = KCTypeDescription(GetTypeForName('KCDATA_TYPE_TIMEBASE'), (
771 KCSubTypeElement('numer', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 0, 0),
772 KCSubTypeElement('denom', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4, 0)
778 STACKSHOT_IO_NUM_PRIORITIES = 4
779 KNOWN_TYPES_COLLECTION[0x901] = KCTypeDescription(0x901, (
780 KCSubTypeElement.FromBasicCtype('ss_disk_reads_count', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
781 KCSubTypeElement.FromBasicCtype('ss_disk_reads_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
782 KCSubTypeElement.FromBasicCtype('ss_disk_writes_count', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
783 KCSubTypeElement.FromBasicCtype('ss_disk_writes_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
784 KCSubTypeElement('ss_io_priority_count', KCSUBTYPE_TYPE.KC_ST_UINT64, KCSubTypeElement.GetSizeForArray(STACKSHOT_IO_NUM_PRIORITIES, 8), 32, 1),
785 KCSubTypeElement('ss_io_priority_size', KCSUBTYPE_TYPE.KC_ST_UINT64, KCSubTypeElement.GetSizeForArray(STACKSHOT_IO_NUM_PRIORITIES, 8), 32 + (STACKSHOT_IO_NUM_PRIORITIES * 8), 1),
786 KCSubTypeElement.FromBasicCtype('ss_paging_count', KCSUBTYPE_TYPE.KC_ST_UINT64, 32 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8)),
787 KCSubTypeElement.FromBasicCtype('ss_paging_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 40 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8)),
788 KCSubTypeElement.FromBasicCtype('ss_non_paging_count', KCSUBTYPE_TYPE.KC_ST_UINT64, 48 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8)),
789 KCSubTypeElement.FromBasicCtype('ss_non_paging_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 56 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8)),
790 KCSubTypeElement.FromBasicCtype('ss_data_count', KCSUBTYPE_TYPE.KC_ST_UINT64, 64 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8)),
791 KCSubTypeElement.FromBasicCtype('ss_data_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 72 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8)),
792 KCSubTypeElement.FromBasicCtype('ss_metadata_count', KCSUBTYPE_TYPE.KC_ST_UINT64, 80 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8)),
793 KCSubTypeElement.FromBasicCtype('ss_metadata_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 88 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8))
798 KNOWN_TYPES_COLLECTION[0x902] = KCTypeDescription(0x902, (
799 KCSubTypeElement('snapshot_magic', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 0, 0),
800 KCSubTypeElement('free_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 1, 0),
801 KCSubTypeElement('active_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 2, 0),
802 KCSubTypeElement('inactive_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 3, 0),
803 KCSubTypeElement('purgeable_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 4, 0),
804 KCSubTypeElement('wired_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 5, 0),
805 KCSubTypeElement('speculative_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 6, 0),
806 KCSubTypeElement('throttled_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 7, 0),
807 KCSubTypeElement('filebacked_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 8, 0),
808 KCSubTypeElement('compressions', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 9, 0),
809 KCSubTypeElement('decompressions', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 10, 0),
810 KCSubTypeElement('compressor_size', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 11, 0),
811 KCSubTypeElement('busy_buffer_count', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 4 * 12, 0),
812 KCSubTypeElement('pages_wanted', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 13, 0),
813 KCSubTypeElement('pages_reclaimed', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 14, 0),
814 KCSubTypeElement('pages_wanted_reclaimed_valid', KCSUBTYPE_TYPE.KC_ST_UINT8, 1, 4 * 15, 0)
816 'mem_and_io_snapshot'
820 KNOWN_TYPES_COLLECTION[0x905] = KCTypeDescription(0x905, (
821 KCSubTypeElement.FromBasicCtype('ts_unique_pid', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
822 KCSubTypeElement.FromBasicCtype('ts_ss_flags', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
823 KCSubTypeElement.FromBasicCtype('ts_user_time_in_terminated_thre', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
824 KCSubTypeElement.FromBasicCtype('ts_system_time_in_terminated_th', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
825 KCSubTypeElement.FromBasicCtype('ts_p_start_sec', KCSUBTYPE_TYPE.KC_ST_UINT64, 32),
826 KCSubTypeElement.FromBasicCtype('ts_task_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 40),
827 KCSubTypeElement.FromBasicCtype('ts_max_resident_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 48),
828 KCSubTypeElement.FromBasicCtype('ts_suspend_count', KCSUBTYPE_TYPE.KC_ST_UINT32, 56),
829 KCSubTypeElement.FromBasicCtype('ts_faults', KCSUBTYPE_TYPE.KC_ST_UINT32, 60),
830 KCSubTypeElement.FromBasicCtype('ts_pageins', KCSUBTYPE_TYPE.KC_ST_UINT32, 64),
831 KCSubTypeElement.FromBasicCtype('ts_cow_faults', KCSUBTYPE_TYPE.KC_ST_UINT32, 68),
832 KCSubTypeElement.FromBasicCtype('ts_was_throttled', KCSUBTYPE_TYPE.KC_ST_UINT32, 72),
833 KCSubTypeElement.FromBasicCtype('ts_did_throttle', KCSUBTYPE_TYPE.KC_ST_UINT32, 76),
834 KCSubTypeElement.FromBasicCtype('ts_latency_qos', KCSUBTYPE_TYPE.KC_ST_UINT32, 80),
835 KCSubTypeElement.FromBasicCtype('ts_pid', KCSUBTYPE_TYPE.KC_ST_INT32, 84),
836 KCSubTypeElement('ts_p_comm', KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(32, 1), 88, 1)
841 KNOWN_TYPES_COLLECTION[0x906] = KCTypeDescription(0x906, (
842 KCSubTypeElement.FromBasicCtype('ths_thread_id', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
843 KCSubTypeElement.FromBasicCtype('ths_wait_event', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
844 KCSubTypeElement.FromBasicCtype('ths_continuation', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
845 KCSubTypeElement.FromBasicCtype('ths_total_syscalls', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
846 KCSubTypeElement.FromBasicCtype('ths_voucher_identifier', KCSUBTYPE_TYPE.KC_ST_UINT64, 32),
847 KCSubTypeElement.FromBasicCtype('ths_dqserialnum', KCSUBTYPE_TYPE.KC_ST_UINT64, 40),
848 KCSubTypeElement.FromBasicCtype('ths_user_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 48),
849 KCSubTypeElement.FromBasicCtype('ths_sys_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 56),
850 KCSubTypeElement.FromBasicCtype('ths_ss_flags', KCSUBTYPE_TYPE.KC_ST_UINT64, 64),
851 KCSubTypeElement.FromBasicCtype('ths_last_run_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 72),
852 KCSubTypeElement.FromBasicCtype('ths_last_made_runnable_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 80),
853 KCSubTypeElement.FromBasicCtype('ths_state', KCSUBTYPE_TYPE.KC_ST_UINT32, 88),
854 KCSubTypeElement.FromBasicCtype('ths_sched_flags', KCSUBTYPE_TYPE.KC_ST_UINT32, 92),
855 KCSubTypeElement.FromBasicCtype('ths_base_priority', KCSUBTYPE_TYPE.KC_ST_INT16, 96),
856 KCSubTypeElement.FromBasicCtype('ths_sched_priority', KCSUBTYPE_TYPE.KC_ST_INT16, 98),
857 KCSubTypeElement.FromBasicCtype('ths_eqos', KCSUBTYPE_TYPE.KC_ST_UINT8, 100),
858 KCSubTypeElement.FromBasicCtype('ths_rqos', KCSUBTYPE_TYPE.KC_ST_UINT8, 101),
859 KCSubTypeElement.FromBasicCtype('ths_rqos_override', KCSUBTYPE_TYPE.KC_ST_UINT8, 102),
860 KCSubTypeElement.FromBasicCtype('ths_io_tier', KCSUBTYPE_TYPE.KC_ST_UINT8, 103),
861 KCSubTypeElement.FromBasicCtype('ths_thread_t', KCSUBTYPE_TYPE.KC_ST_UINT64, 104),
862 KCSubTypeElement.FromBasicCtype('ths_requested_policy', KCSUBTYPE_TYPE.KC_ST_UINT64, 112),
863 KCSubTypeElement.FromBasicCtype('ths_effective_policy', KCSUBTYPE_TYPE.KC_ST_UINT64, 120),
869 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_THREAD_DISPATCH_QUEUE_LABEL')] = KCSubTypeElement('dispatch_queue_label', KCSUBTYPE_TYPE.KC_ST_CHAR,
870 KCSubTypeElement.GetSizeForArray(64, 1), 0, 1)
872 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_THREAD_DELTA_SNAPSHOT')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_THREAD_DELTA_SNAPSHOT'), (
873 KCSubTypeElement.FromBasicCtype('tds_thread_id', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
874 KCSubTypeElement.FromBasicCtype('tds_voucher_identifier', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
875 KCSubTypeElement.FromBasicCtype('tds_ss_flags', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
876 KCSubTypeElement.FromBasicCtype('tds_last_made_runnable_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
877 KCSubTypeElement.FromBasicCtype('tds_state', KCSUBTYPE_TYPE.KC_ST_UINT32, 32),
878 KCSubTypeElement.FromBasicCtype('tds_sched_flags', KCSUBTYPE_TYPE.KC_ST_UINT32, 36),
879 KCSubTypeElement.FromBasicCtype('tds_base_priority', KCSUBTYPE_TYPE.KC_ST_INT16, 40),
880 KCSubTypeElement.FromBasicCtype('tds_sched_priority', KCSUBTYPE_TYPE.KC_ST_INT16, 42),
881 KCSubTypeElement.FromBasicCtype('tds_eqos', KCSUBTYPE_TYPE.KC_ST_UINT8, 44),
882 KCSubTypeElement.FromBasicCtype('tds_rqos', KCSUBTYPE_TYPE.KC_ST_UINT8, 45),
883 KCSubTypeElement.FromBasicCtype('tds_rqos_override', KCSUBTYPE_TYPE.KC_ST_UINT8, 46),
884 KCSubTypeElement.FromBasicCtype('tds_io_tier', KCSUBTYPE_TYPE.KC_ST_UINT8, 47),
885 KCSubTypeElement.FromBasicCtype('tds_requested_policy', KCSUBTYPE_TYPE.KC_ST_UINT64, 48),
886 KCSubTypeElement.FromBasicCtype('tds_effective_policy', KCSUBTYPE_TYPE.KC_ST_UINT64, 56),
888 'thread_delta_snapshot',
892 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_TASK_DELTA_SNAPSHOT')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_TASK_DELTA_SNAPSHOT'), (
893 KCSubTypeElement.FromBasicCtype('tds_unique_pid', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
894 KCSubTypeElement.FromBasicCtype('tds_ss_flags', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
895 KCSubTypeElement.FromBasicCtype('tds_user_time_in_terminated_thr', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
896 KCSubTypeElement.FromBasicCtype('tds_system_time_in_terminated_t', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
897 KCSubTypeElement.FromBasicCtype('tds_task_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 32),
898 KCSubTypeElement.FromBasicCtype('tds_max_resident_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 40),
899 KCSubTypeElement.FromBasicCtype('tds_suspend_count', KCSUBTYPE_TYPE.KC_ST_UINT32, 48),
900 KCSubTypeElement.FromBasicCtype('tds_faults', KCSUBTYPE_TYPE.KC_ST_UINT32, 52),
901 KCSubTypeElement.FromBasicCtype('tds_pageins', KCSUBTYPE_TYPE.KC_ST_UINT32, 56),
902 KCSubTypeElement.FromBasicCtype('tds_cow_faults', KCSUBTYPE_TYPE.KC_ST_UINT32, 60),
903 KCSubTypeElement.FromBasicCtype('tds_was_throttled', KCSUBTYPE_TYPE.KC_ST_UINT32, 64),
904 KCSubTypeElement.FromBasicCtype('tds_did_throttle', KCSUBTYPE_TYPE.KC_ST_UINT32, 68),
905 KCSubTypeElement.FromBasicCtype('tds_latency_qos', KCSUBTYPE_TYPE.KC_ST_UINT32, 72),
907 'task_delta_snapshot'
911 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_THREAD_NAME')] = KCSubTypeElement('pth_name', KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(64, 1), 0, 1)
913 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_SYS_SHAREDCACHE_LAYOUT')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_SYS_SHAREDCACHE_LAYOUT'), (
914 KCSubTypeElement('imageLoadAddress', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0),
915 KCSubTypeElement('imageUUID', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 8, 1)
917 'system_shared_cache_layout'
920 KNOWN_TYPES_COLLECTION[GetTypeForName('KCDATA_TYPE_LIBRARY_LOADINFO64')] = KCTypeDescription(GetTypeForName('KCDATA_TYPE_LIBRARY_LOADINFO64'), (
921 KCSubTypeElement('imageLoadAddress', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0),
922 KCSubTypeElement('imageUUID', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 8, 1)
928 KNOWN_TYPES_COLLECTION[GetTypeForName('KCDATA_TYPE_LIBRARY_LOADINFO')] = KCTypeDescription(GetTypeForName('KCDATA_TYPE_LIBRARY_LOADINFO'), (
929 KCSubTypeElement('imageLoadAddress', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 0, 0),
930 KCSubTypeElement('imageUUID', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 4, 1)
936 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_LOADINFO64_TEXT_EXEC')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_LOADINFO64_TEXT_EXEC'), (
937 KCSubTypeElement('imageLoadAddress', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0),
938 KCSubTypeElement('imageUUID', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 8, 1),
940 'dyld_load_info_text_exec'
943 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_SHAREDCACHE_LOADINFO')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_SHAREDCACHE_LOADINFO'), (
944 KCSubTypeElement('imageLoadAddress', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0),
945 KCSubTypeElement('imageUUID', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 8, 1),
946 KCSubTypeElement('imageSlidBaseAddress', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 24, 0),
948 'shared_cache_dyld_load_info',
952 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERNELCACHE_LOADINFO')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_KERNELCACHE_LOADINFO'), (
953 KCSubTypeElement('imageLoadAddress', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0),
954 KCSubTypeElement('imageUUID', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 8, 1),
956 'kernelcache_load_info'
959 KNOWN_TYPES_COLLECTION[0x33] = KCSubTypeElement('mach_absolute_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0, KCSubTypeElement._get_naked_element_value)
960 KNOWN_TYPES_COLLECTION[0x907] = KCSubTypeElement.FromBasicCtype('donating_pids', KCSUBTYPE_TYPE.KC_ST_INT32, legacy_size=4)
962 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)
964 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKFRAME')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKFRAME'), (
965 KCSubTypeElement.FromBasicCtype('lr', KCSUBTYPE_TYPE.KC_ST_UINT32),
966 KCSubTypeElement.FromBasicCtype('sp', KCSUBTYPE_TYPE.KC_ST_UINT32, 4)
968 'kernel_stack_frames',
972 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKLR')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKLR'), (
973 KCSubTypeElement.FromBasicCtype('lr', KCSUBTYPE_TYPE.KC_ST_UINT32),
975 'kernel_stack_frames'
979 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_USER_STACKFRAME')] = KCTypeDescription.FromKCTypeDescription(
980 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKFRAME')],
981 GetTypeForName('STACKSHOT_KCTYPE_USER_STACKFRAME'),
985 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_USER_STACKLR')] = KCTypeDescription.FromKCTypeDescription(
986 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKLR')],
987 GetTypeForName('STACKSHOT_KCTYPE_USER_STACKLR'),
991 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKFRAME64')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKFRAME64'), (
992 KCSubTypeElement.FromBasicCtype('lr', KCSUBTYPE_TYPE.KC_ST_UINT64),
993 KCSubTypeElement.FromBasicCtype('sp', KCSUBTYPE_TYPE.KC_ST_UINT64, 8)
995 'kernel_stack_frames',
999 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_USER_STACKFRAME64')] = KCTypeDescription.FromKCTypeDescription(
1000 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKFRAME64')],
1001 GetTypeForName('STACKSHOT_KCTYPE_USER_STACKFRAME64'),
1006 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKLR64')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKLR64'), (
1007 KCSubTypeElement.FromBasicCtype('lr', KCSUBTYPE_TYPE.KC_ST_UINT64),
1009 'kernel_stack_frames'
1012 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_USER_STACKLR64')] = KCTypeDescription.FromKCTypeDescription(
1013 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKLR64')],
1014 GetTypeForName('STACKSHOT_KCTYPE_USER_STACKLR64'),
1019 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_NONRUNNABLE_TIDS')] = KCSubTypeElement.FromBasicCtype('nonrunnable_threads', KCSUBTYPE_TYPE.KC_ST_INT64)
1021 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_NONRUNNABLE_TASKS')] = KCSubTypeElement.FromBasicCtype('nonrunnable_tasks', KCSUBTYPE_TYPE.KC_ST_INT64)
1023 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_OSVERSION')] = KCSubTypeElement('osversion', KCSUBTYPE_TYPE.KC_ST_CHAR,
1024 KCSubTypeElement.GetSizeForArray(256, 1), 0, 1)
1026 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_BOOTARGS')] = KCSubTypeElement('boot_args', KCSUBTYPE_TYPE.KC_ST_CHAR,
1027 KCSubTypeElement.GetSizeForArray(256, 1), 0, 1)
1029 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)
1031 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)
1033 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_JETSAM_LEVEL')] = KCSubTypeElement('jetsam_level', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 0, 0, KCSubTypeElement._get_naked_element_value)
1035 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)
1037 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_STACKSHOT_FAULT_STATS')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_STACKSHOT_FAULT_STATS'),
1039 KCSubTypeElement.FromBasicCtype('sfs_pages_faulted_in', KCSUBTYPE_TYPE.KC_ST_UINT32, 0),
1040 KCSubTypeElement.FromBasicCtype('sfs_time_spent_faulting', KCSUBTYPE_TYPE.KC_ST_UINT64, 4),
1041 KCSubTypeElement.FromBasicCtype('sfs_system_max_fault_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 12),
1042 KCSubTypeElement.FromBasicCtype('sfs_stopped_faulting', KCSUBTYPE_TYPE.KC_ST_UINT8, 20)
1044 'stackshot_fault_stats')
1046 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_THREAD_WAITINFO')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_THREAD_WAITINFO'),
1048 KCSubTypeElement.FromBasicCtype('owner', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1049 KCSubTypeElement.FromBasicCtype('waiter', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1050 KCSubTypeElement.FromBasicCtype('context', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
1051 KCSubTypeElement.FromBasicCtype('wait_type', KCSUBTYPE_TYPE.KC_ST_UINT8, 24)
1055 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_THREAD_TURNSTILEINFO')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_THREAD_TURNSTILEINFO'),
1057 KCSubTypeElement.FromBasicCtype('waiter', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1058 KCSubTypeElement.FromBasicCtype('turnstile_context', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1059 KCSubTypeElement.FromBasicCtype('turnstile_priority', KCSUBTYPE_TYPE.KC_ST_UINT8, 16),
1060 KCSubTypeElement.FromBasicCtype('number_of_hops', KCSUBTYPE_TYPE.KC_ST_UINT8, 17),
1061 KCSubTypeElement.FromBasicCtype('turnstile_flags', KCSUBTYPE_TYPE.KC_ST_UINT64, 18),
1063 'thread_turnstileinfo')
1065 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_THREAD_GROUP_SNAPSHOT')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_THREAD_GROUP'),
1067 KCSubTypeElement.FromBasicCtype('tgs_id', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1068 KCSubTypeElement('tgs_name', KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(16, 1),
1070 KCSubTypeElement.FromBasicCtype('tgs_flags', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
1072 'thread_group_snapshot')
1075 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_THREAD_GROUP')] = KCSubTypeElement('thread_group', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0, KCSubTypeElement._get_naked_element_value)
1077 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_JETSAM_COALITION_SNAPSHOT')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_JETSAM_COALITION_SNAPSHOT'),
1079 KCSubTypeElement.FromBasicCtype('jcs_id', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1080 KCSubTypeElement.FromBasicCtype('jcs_flags', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1081 KCSubTypeElement.FromBasicCtype('jcs_thread_group', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
1082 KCSubTypeElement.FromBasicCtype('jcs_leader_task_uniqueid', KCSUBTYPE_TYPE.KC_ST_UINT64, 24)
1084 'jetsam_coalition_snapshot')
1086 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_JETSAM_COALITION')] = KCSubTypeElement('jetsam_coalition', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0, KCSubTypeElement._get_naked_element_value)
1088 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_INSTRS_CYCLES')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_INSTRS_CYCLES'),
1090 KCSubTypeElement.FromBasicCtype('ics_instructions', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1091 KCSubTypeElement.FromBasicCtype('ics_cycles', KCSUBTYPE_TYPE.KC_ST_UINT64, 8)
1093 'instrs_cycles_snapshot')
1095 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_TASK_CPU_ARCHITECTURE')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_TASK_CPU_ARCHITECTURE'),
1097 KCSubTypeElement.FromBasicCtype('cputype', KCSUBTYPE_TYPE.KC_ST_INT32, 0),
1098 KCSubTypeElement.FromBasicCtype('cpusubtype', KCSUBTYPE_TYPE.KC_ST_INT32, 4)
1100 'task_cpu_architecture')
1102 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_LATENCY_INFO')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_LATENCY_INFO'),
1104 KCSubTypeElement.FromBasicCtype('latency_version', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1105 KCSubTypeElement.FromBasicCtype('setup_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1106 KCSubTypeElement.FromBasicCtype('total_task_iteration_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
1107 KCSubTypeElement.FromBasicCtype('total_terminated_task_iteration_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 24)
1109 'stackshot_latency_collection')
1111 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_LATENCY_INFO_TASK')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_LATENCY_INFO_TASK'),
1113 KCSubTypeElement.FromBasicCtype('task_uniqueid', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1114 KCSubTypeElement.FromBasicCtype('setup_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1115 KCSubTypeElement.FromBasicCtype('task_thread_count_loop_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
1116 KCSubTypeElement.FromBasicCtype('task_thread_data_loop_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
1117 KCSubTypeElement.FromBasicCtype('cur_tsnap_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 32),
1118 KCSubTypeElement.FromBasicCtype('pmap_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 40),
1119 KCSubTypeElement.FromBasicCtype('bsd_proc_ids_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 48),
1120 KCSubTypeElement.FromBasicCtype('misc_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 56),
1121 KCSubTypeElement.FromBasicCtype('misc2_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 64),
1122 KCSubTypeElement.FromBasicCtype('end_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 72)
1124 'stackshot_latency_task')
1126 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_LATENCY_INFO_THREAD')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_LATENCY_INFO_THREAD'),
1128 KCSubTypeElement.FromBasicCtype('thread_id', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1129 KCSubTypeElement.FromBasicCtype('cur_thsnap1_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1130 KCSubTypeElement.FromBasicCtype('dispatch_serial_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
1131 KCSubTypeElement.FromBasicCtype('dispatch_label_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
1132 KCSubTypeElement.FromBasicCtype('cur_thsnap2_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 32),
1133 KCSubTypeElement.FromBasicCtype('thread_name_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 40),
1134 KCSubTypeElement.FromBasicCtype('sur_times_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 48),
1135 KCSubTypeElement.FromBasicCtype('user_stack_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 56),
1136 KCSubTypeElement.FromBasicCtype('kernel_stack_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 64),
1137 KCSubTypeElement.FromBasicCtype('misc_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 72),
1139 'stackshot_latency_thread')
1141 def set_type(name, *args):
1142 typ = GetTypeForName(name)
1143 KNOWN_TYPES_COLLECTION[typ] = KCTypeDescription(GetTypeForName(typ), *args)
1146 set_type('STACKSHOT_KCTYPE_USER_STACKTOP',
1148 KCSubTypeElement.FromBasicCtype('sp', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1149 KCSubTypeElement('stack_contents', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(8, 1), 8, 1),
1153 #KNOWN_TYPES_COLLECTION[0x907] = KCSubTypeElement('donating_pids', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 0, 0, KCSubTypeElement._get_naked_element_value)
1154 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PID')] = KCSubTypeElement('pid', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1155 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PPID')] = KCSubTypeElement('ppid', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1156 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PROC_NAME')] = KCSubTypeElement('p_comm', KCSUBTYPE_TYPE.KC_ST_CHAR,
1157 KCSubTypeElement.GetSizeForArray(32, 1), 0, 1)
1158 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_USERSTACK')] = KCSubTypeElement('userstack_ptr', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0)
1159 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_ARGSLEN')] = KCSubTypeElement('p_argslen', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1161 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PROC_PATH')] = KCSubTypeElement('p_path', KCSUBTYPE_TYPE.KC_ST_CHAR,
1162 KCSubTypeElement.GetSizeForArray(1024, 1), 0, 1)
1163 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PROC_CSFLAGS')] = KCSubTypeElement('p_csflags', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1164 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_UID')] = KCSubTypeElement('uid', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1165 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_GID')] = KCSubTypeElement('gid', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1166 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PROC_ARGC')] = KCSubTypeElement('argc', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1167 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PROC_FLAGS')] = KCSubTypeElement('p_flags', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1168 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_CPUTYPE')] = KCSubTypeElement('cputype', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1169 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_RESPONSIBLE_PID')] = KCSubTypeElement('responsible_pid', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1170 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_DIRTY_FLAGS')] = KCSubTypeElement('dirty_flags', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1171 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_CRASHED_THREADID')] = KCSubTypeElement('crashed_threadid', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0)
1172 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_COALITION_ID')] = KCSubTypeElement('coalition_id', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0)
1174 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PROC_STATUS')] = KCSubTypeElement('p_status', KCSUBTYPE_TYPE.KC_ST_UINT8, 1, 0, 0)
1176 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_BSDINFOWITHUNIQID')] = KCTypeDescription(GetTypeForName('TASK_CRASHINFO_BSDINFOWITHUNIQID'),
1177 ( KCSubTypeElement('p_uuid', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 0, 1),
1178 KCSubTypeElement.FromBasicCtype('p_uniqueid', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
1179 KCSubTypeElement.FromBasicCtype('p_puniqueid', KCSUBTYPE_TYPE.KC_ST_UINT64, 24)
1181 'proc_uniqidentifierinfo')
1183 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_EXCEPTION_CODES')] = (
1184 KCTypeDescription(GetTypeForName('TASK_CRASHINFO_EXCEPTION_CODES'),
1185 (KCSubTypeElement.FromBasicCtype('code_0', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1186 KCSubTypeElement.FromBasicCtype('code_1', KCSUBTYPE_TYPE.KC_ST_UINT64, 8)),
1187 'mach_exception_data_t'))
1190 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PROC_STARTTIME')] = (
1191 KCTypeDescription(GetTypeForName('TASK_CRASHINFO_PROC_STARTTIME'),
1192 (KCSubTypeElement.FromBasicCtype('tv_sec', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1193 KCSubTypeElement.FromBasicCtype('tv_usec', KCSUBTYPE_TYPE.KC_ST_UINT64, 8)),
1197 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_RUSAGE_INFO')] = KCTypeDescription(GetTypeForName('TASK_CRASHINFO_RUSAGE_INFO'),
1199 KCSubTypeElement('ri_uuid', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 0, 1),
1200 KCSubTypeElement.FromBasicCtype('ri_user_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
1201 KCSubTypeElement.FromBasicCtype('ri_system_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
1202 KCSubTypeElement.FromBasicCtype('ri_pkg_idle_wkups', KCSUBTYPE_TYPE.KC_ST_UINT64, 32),
1203 KCSubTypeElement.FromBasicCtype('ri_interrupt_wkups', KCSUBTYPE_TYPE.KC_ST_UINT64, 40),
1204 KCSubTypeElement.FromBasicCtype('ri_pageins', KCSUBTYPE_TYPE.KC_ST_UINT64, 48),
1205 KCSubTypeElement.FromBasicCtype('ri_wired_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 56),
1206 KCSubTypeElement.FromBasicCtype('ri_resident_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 64),
1207 KCSubTypeElement.FromBasicCtype('ri_phys_footprint', KCSUBTYPE_TYPE.KC_ST_UINT64, 72),
1208 KCSubTypeElement.FromBasicCtype('ri_proc_start_abstime', KCSUBTYPE_TYPE.KC_ST_UINT64, 80),
1209 KCSubTypeElement.FromBasicCtype('ri_proc_exit_abstime', KCSUBTYPE_TYPE.KC_ST_UINT64, 88),
1210 KCSubTypeElement.FromBasicCtype('ri_child_user_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 96),
1211 KCSubTypeElement.FromBasicCtype('ri_child_system_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 104),
1212 KCSubTypeElement.FromBasicCtype('ri_child_pkg_idle_wkups', KCSUBTYPE_TYPE.KC_ST_UINT64, 112),
1213 KCSubTypeElement.FromBasicCtype('ri_child_interrupt_wkups', KCSUBTYPE_TYPE.KC_ST_UINT64, 120),
1214 KCSubTypeElement.FromBasicCtype('ri_child_pageins', KCSUBTYPE_TYPE.KC_ST_UINT64, 128),
1215 KCSubTypeElement.FromBasicCtype('ri_child_elapsed_abstime', KCSUBTYPE_TYPE.KC_ST_UINT64, 136),
1216 KCSubTypeElement.FromBasicCtype('ri_diskio_bytesread', KCSUBTYPE_TYPE.KC_ST_UINT64, 144),
1217 KCSubTypeElement.FromBasicCtype('ri_diskio_byteswritten', KCSUBTYPE_TYPE.KC_ST_UINT64, 152),
1218 KCSubTypeElement.FromBasicCtype('ri_cpu_time_qos_default', KCSUBTYPE_TYPE.KC_ST_UINT64, 160),
1219 KCSubTypeElement.FromBasicCtype('ri_cpu_time_qos_maintenance', KCSUBTYPE_TYPE.KC_ST_UINT64, 168),
1220 KCSubTypeElement.FromBasicCtype('ri_cpu_time_qos_background', KCSUBTYPE_TYPE.KC_ST_UINT64, 176),
1221 KCSubTypeElement.FromBasicCtype('ri_cpu_time_qos_utility', KCSUBTYPE_TYPE.KC_ST_UINT64, 184),
1222 KCSubTypeElement.FromBasicCtype('ri_cpu_time_qos_legacy', KCSUBTYPE_TYPE.KC_ST_UINT64, 192),
1223 KCSubTypeElement.FromBasicCtype('ri_cpu_time_qos_user_initiated', KCSUBTYPE_TYPE.KC_ST_UINT64, 200),
1224 KCSubTypeElement.FromBasicCtype('ri_cpu_time_qos_user_interactiv', KCSUBTYPE_TYPE.KC_ST_UINT64, 208),
1225 KCSubTypeElement.FromBasicCtype('ri_billed_system_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 216),
1226 KCSubTypeElement.FromBasicCtype('ri_serviced_system_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 224)
1230 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_CPU_TIMES')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_CPU_TIMES'),
1232 KCSubTypeElement.FromBasicCtype('user_usec', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1233 KCSubTypeElement.FromBasicCtype('system_usec', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1234 KCSubTypeElement.FromBasicCtype('runnable_usec', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
1237 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_STACKSHOT_DURATION')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_STACKSHOT_DURATION'),
1239 KCSubTypeElement.FromBasicCtype('stackshot_duration', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1240 KCSubTypeElement.FromBasicCtype('stackshot_duration_outer', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1241 ), 'stackshot_duration', merge=True
1244 KNOWN_TYPES_COLLECTION[GetTypeForName('KCDATA_TYPE_PROCNAME')] = (
1245 KCSubTypeElement("proc_name
", KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(-1, 1), 0, 1))
1247 KNOWN_TYPES_COLLECTION[GetTypeForName('KCDATA_TYPE_PID')] = (
1248 KCSubTypeElement('pid', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0))
1250 KNOWN_TYPES_COLLECTION[GetTypeForName('EXIT_REASON_SNAPSHOT')] = KCTypeDescription(GetTypeForName('EXIT_REASON_SNAPSHOT'),
1252 KCSubTypeElement.FromBasicCtype('ers_namespace', KCSUBTYPE_TYPE.KC_ST_UINT32, 0),
1253 KCSubTypeElement.FromBasicCtype('ers_code', KCSUBTYPE_TYPE.KC_ST_UINT64, 4),
1254 KCSubTypeElement.FromBasicCtype('ers_flags', KCSUBTYPE_TYPE.KC_ST_UINT64, 12),
1255 ), 'exit_reason_basic_info')
1257 KNOWN_TYPES_COLLECTION[GetTypeForName('EXIT_REASON_USER_DESC')] = (
1258 KCSubTypeElement("exit_reason_user_description
", KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(-1, 1), 0, 1))
1260 KNOWN_TYPES_COLLECTION[GetTypeForName('EXIT_REASON_USER_PAYLOAD')] = KCSubTypeElement('exit_reason_user_payload',
1261 KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(-1, 1), 0, 1)
1263 KNOWN_TYPES_COLLECTION[GetTypeForName('EXIT_REASON_CODESIGNING_INFO')] = KCTypeDescription(GetTypeForName('EXIT_REASON_CODESIGNING_INFO'),
1265 KCSubTypeElement.FromBasicCtype('ceri_virt_addr', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1266 KCSubTypeElement.FromBasicCtype('ceri_file_offset', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1267 KCSubTypeElement("ceri_pathname
", KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(1024, 1), 16, 1),
1268 KCSubTypeElement("ceri_filename
", KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(1024, 1), 1040, 1),
1269 KCSubTypeElement.FromBasicCtype('ceri_codesig_modtime_secs', KCSUBTYPE_TYPE.KC_ST_UINT64, 2064),
1270 KCSubTypeElement.FromBasicCtype('ceri_codesig_modtime_nsecs', KCSUBTYPE_TYPE.KC_ST_UINT64, 2072),
1271 KCSubTypeElement.FromBasicCtype('ceri_page_modtime_secs', KCSUBTYPE_TYPE.KC_ST_UINT64, 2080),
1272 KCSubTypeElement.FromBasicCtype('ceri_page_modtime_nsecs', KCSUBTYPE_TYPE.KC_ST_UINT64, 2088),
1273 KCSubTypeElement.FromBasicCtype('ceri_path_truncated', KCSUBTYPE_TYPE.KC_ST_UINT8, 2096),
1274 KCSubTypeElement.FromBasicCtype('ceri_object_codesigned', KCSUBTYPE_TYPE.KC_ST_UINT8, 2097),
1275 KCSubTypeElement.FromBasicCtype('ceri_page_codesig_validated', KCSUBTYPE_TYPE.KC_ST_UINT8, 2098),
1276 KCSubTypeElement.FromBasicCtype('ceri_page_codesig_tainted', KCSUBTYPE_TYPE.KC_ST_UINT8, 2099),
1277 KCSubTypeElement.FromBasicCtype('ceri_page_codesig_nx', KCSUBTYPE_TYPE.KC_ST_UINT8, 2100),
1278 KCSubTypeElement.FromBasicCtype('ceri_page_wpmapped', KCSUBTYPE_TYPE.KC_ST_UINT8, 2101),
1279 KCSubTypeElement.FromBasicCtype('ceri_page_slid', KCSUBTYPE_TYPE.KC_ST_UINT8, 2102),
1280 KCSubTypeElement.FromBasicCtype('ceri_page_dirty', KCSUBTYPE_TYPE.KC_ST_UINT8, 2103),
1281 KCSubTypeElement.FromBasicCtype('ceri_page_shadow_depth', KCSUBTYPE_TYPE.KC_ST_UINT32, 2104),
1282 ), 'exit_reason_codesigning_info')
1284 KNOWN_TYPES_COLLECTION[GetTypeForName('EXIT_REASON_WORKLOOP_ID')] = (
1285 KCSubTypeElement('exit_reason_workloop_id', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0, KCSubTypeElement._get_naked_element_value))
1287 KNOWN_TYPES_COLLECTION[GetTypeForName('EXIT_REASON_DISPATCH_QUEUE_NO')] = (
1288 KCSubTypeElement('exit_reason_dispatch_queue_no', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0, KCSubTypeElement._get_naked_element_value))
1290 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_ASID')] = (
1291 KCSubTypeElement('ts_asid', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 0, 0))
1293 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_PAGE_TABLES')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_PAGE_TABLES'), (
1294 KCSubTypeElement(None, KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0, KCSubTypeElement._get_naked_element_value), ),
1300 def GetSecondsFromMATime(mat, tb):
1301 return (float(long(mat) * tb['numer']) / tb['denom']) / 1e9
1303 def FindLibraryForAddress(liblist, address):
1310 def FindIndexOfLibInCatalog(catalog, lib):
1314 if l[0] == lib[0] and l[1] == lib[1]:
1321 index = len(catalog) - 1
1325 def GetOffsetOfAddressForLib(lib, address):
1326 return (address - lib[1])
1328 def GetSymbolInfoForFrame(catalog, liblist, address):
1329 lib = FindLibraryForAddress(liblist, address)
1331 lib = ["00000000000000000000000000000000",0,"A
"]
1332 offset = GetOffsetOfAddressForLib(lib, address)
1333 index = FindIndexOfLibInCatalog(catalog, lib)
1334 return [index, offset]
1336 def GetStateDescription(s):
1343 TH_TERMINATE2 = 0x20
1344 TH_WAIT_REPORT = 0x40
1347 retval.append("TH_WAIT
")
1349 retval.append("TH_SUSP
")
1351 retval.append("TH_RUN
")
1353 retval.append("TH_UNINT
")
1354 if (s & TH_TERMINATE):
1355 retval.append("TH_TERMINATE
")
1356 if (s & TH_TERMINATE2):
1357 retval.append("TH_TERMINATE2
")
1358 if (s & TH_WAIT_REPORT):
1359 retval.append("TH_WAIT_REPORT
")
1361 retval.append("TH_IDLE
")
1365 def format_uuid(elementValues):
1366 return ''.join("%02x
" % i for i in elementValues)
1368 kThreadWaitNone = 0x00
1369 kThreadWaitKernelMutex = 0x01
1370 kThreadWaitPortReceive = 0x02
1371 kThreadWaitPortSetReceive = 0x03
1372 kThreadWaitPortSend = 0x04
1373 kThreadWaitPortSendInTransit = 0x05
1374 kThreadWaitSemaphore = 0x06
1375 kThreadWaitKernelRWLockRead = 0x07
1376 kThreadWaitKernelRWLockWrite = 0x08
1377 kThreadWaitKernelRWLockUpgrade = 0x09
1378 kThreadWaitUserLock = 0x0a
1379 kThreadWaitPThreadMutex = 0x0b
1380 kThreadWaitPThreadRWLockRead = 0x0c
1381 kThreadWaitPThreadRWLockWrite = 0x0d
1382 kThreadWaitPThreadCondVar = 0x0e
1383 kThreadWaitParkedWorkQueue = 0x0f
1384 kThreadWaitWorkloopSyncWait = 0x10
1385 kThreadWaitOnProcess = 0x11
1386 kThreadWaitSleepWithInheritor = 0x12
1387 kThreadWaitEventlink = 0x13
1388 kThreadWaitCompressor = 0x14
1391 UINT64_MAX = 0xffffffffffffffff
1392 STACKSHOT_WAITOWNER_KERNEL = (UINT64_MAX - 1)
1393 STACKSHOT_WAITOWNER_PORT_LOCKED = (UINT64_MAX - 2)
1394 STACKSHOT_WAITOWNER_PSET_LOCKED = (UINT64_MAX - 3)
1395 STACKSHOT_WAITOWNER_INTRANSIT = (UINT64_MAX - 4)
1396 STACKSHOT_WAITOWNER_MTXSPIN = (UINT64_MAX - 5)
1397 STACKSHOT_WAITOWNER_THREQUESTED = (UINT64_MAX - 6)
1398 STACKSHOT_WAITOWNER_SUSPENDED = (UINT64_MAX - 7)
1400 STACKSHOT_TURNSTILE_STATUS_UNKNOWN = 0x01
1401 STACKSHOT_TURNSTILE_STATUS_LOCKED_WAITQ = 0x02
1402 STACKSHOT_TURNSTILE_STATUS_WORKQUEUE = 0x04
1403 STACKSHOT_TURNSTILE_STATUS_THREAD = 0x08
1404 STACKSHOT_TURNSTILE_STATUS_BLOCKED_ON_TASK = 0x10
1405 STACKSHOT_TURNSTILE_STATUS_HELD_IPLOCK = 0x20
1407 def formatWaitInfo(info):
1408 s = 'thread %d: ' % info['waiter'];
1409 type = info['wait_type']
1410 context = info['context']
1411 owner = info['owner']
1412 if type == kThreadWaitKernelMutex:
1413 s += 'kernel mutex %x' % context
1414 if owner == STACKSHOT_WAITOWNER_MTXSPIN:
1415 s += " in spin mode
"
1417 s += " owned by thread
%u" % owner
1419 s += "with unknown owner
"
1420 elif type == kThreadWaitPortReceive:
1421 s += "mach_msg receive on
"
1422 if owner == STACKSHOT_WAITOWNER_PORT_LOCKED:
1423 s += "locked port
%x" % context
1424 elif owner == STACKSHOT_WAITOWNER_INTRANSIT:
1425 s += "intransit port
%x" % context
1427 s += "port
%x name
%x" % (context, owner)
1429 s += "port
%x" % context
1430 elif type == kThreadWaitPortSetReceive:
1431 if owner == STACKSHOT_WAITOWNER_PSET_LOCKED:
1432 s += "mach_msg receive on locked port
set %x" % context
1434 s += "mach_msg receive on port
set %x" % context
1435 elif type == kThreadWaitPortSend:
1436 s += "mach_msg send on
"
1437 if owner == STACKSHOT_WAITOWNER_PORT_LOCKED:
1438 s += "locked port
%x" % context
1439 elif owner == STACKSHOT_WAITOWNER_INTRANSIT:
1440 s += "intransit port
%x" % context
1441 elif owner == STACKSHOT_WAITOWNER_KERNEL:
1442 s += "port
%x owned by kernel
" % context
1444 s += "port
%x owned by pid
%d" % (context, owner)
1446 s += "port
%x with unknown owner
" % context
1447 elif type == kThreadWaitPortSendInTransit:
1448 s += "mach_msg send on port
%x in transit to
" % context
1450 s += "port
%x" % owner
1453 elif type == kThreadWaitSemaphore:
1454 s += "semaphore port
%x " % context
1456 s += "owned by pid
%d" % owner
1458 s += "with unknown owner
"
1459 elif type == kThreadWaitKernelRWLockRead:
1460 s += "krwlock
%x for reading
" % context
1461 elif type == kThreadWaitKernelRWLockWrite:
1462 s += "krwlock
%x for writing
" % context
1463 elif type == kThreadWaitKernelRWLockUpgrade:
1464 s += "krwlock
%x for upgrading
" % context
1465 elif type == kThreadWaitUserLock:
1467 s += "unfair lock
%x owned by thread
%d" % (context, owner)
1469 s += "spin lock
%x" % context
1470 elif type == kThreadWaitPThreadMutex:
1471 s += "pthread mutex
%x" % context
1473 s += " owned by thread
%d" % owner
1475 s += " with unknown owner
"
1476 elif type == kThreadWaitPThreadRWLockRead:
1477 s += "pthread rwlock
%x for reading
" % context
1478 elif type == kThreadWaitPThreadRWLockWrite:
1479 s += "pthread rwlock
%x for writing
" % context
1480 elif type == kThreadWaitPThreadCondVar:
1481 s += "pthread condvar
%x" % context
1482 elif type == kThreadWaitWorkloopSyncWait:
1483 s += "workloop sync wait
"
1484 if owner == STACKSHOT_WAITOWNER_SUSPENDED:
1486 elif owner == STACKSHOT_WAITOWNER_THREQUESTED:
1487 s += ", thread requested
"
1489 s += ", owned by thread
%u" % owner
1491 s += ", unknown owner
"
1492 s += ", workloop
id %x" % context
1493 elif type == kThreadWaitOnProcess:
1494 if owner == 2**64-1:
1495 s += "waitpid
, for any children
"
1496 elif 2**32 <= owner and owner < 2**64-1:
1497 s += "waitpid
, for process group
%d" % abs(owner - 2**64)
1499 s += "waitpid
, for pid
%d" % owner
1500 elif type == kThreadWaitSleepWithInheritor:
1502 s += "turnstile
, held waitq
"
1504 s += "turnstile
, pushing thread
%d" % owner
1505 elif type == kThreadWaitEventlink:
1507 s += "eventlink
, held waitq
"
1509 s += "eventlink
, signaled by thread
%d" % owner
1510 elif type == kThreadWaitCompressor:
1511 s += "in compressor segment
%x, busy
for thread
%d" % (context, owner)
1514 s += "unknown
type %d (owner
%d, context
%x)" % (type, owner, context)
1518 def formatTurnstileInfo(ti):
1520 return " [no turnstile
]"
1522 ts_flags = int(ti['turnstile_flags'])
1523 ctx = int(ti['turnstile_context'])
1524 hop = int(ti['number_of_hops'])
1525 prio = int(ti['turnstile_priority'])
1526 if ts_flags & STACKSHOT_TURNSTILE_STATUS_HELD_IPLOCK:
1527 return " [turnstile blocked on task
, but ip_lock was held
]"
1528 if ts_flags & STACKSHOT_TURNSTILE_STATUS_BLOCKED_ON_TASK:
1529 return " [turnstile blocked on task pid
%d, hops
: %d, priority
: %d]" % (ctx, hop, prio)
1530 if ts_flags & STACKSHOT_TURNSTILE_STATUS_LOCKED_WAITQ:
1531 return " [turnstile was
in process of being updated
]"
1532 if ts_flags & STACKSHOT_TURNSTILE_STATUS_WORKQUEUE:
1533 return " [blocked on workqueue
: 0x
%x, hops
: %x, priority
: %d]" % (ctx, hop, prio)
1534 if ts_flags & STACKSHOT_TURNSTILE_STATUS_THREAD:
1535 return " [blocked on
: %d, hops
: %x, priority
: %d]" % (ctx, hop, prio)
1536 if ts_flags & STACKSHOT_TURNSTILE_STATUS_UNKNOWN:
1537 return " [turnstile
with unknown inheritor
]"
1539 return " [unknown turnstile status
!]"
1541 def formatWaitInfoWithTurnstiles(waitinfos, tsinfos):
1546 if int(w['waiter']) == int(t['waiter']):
1547 wis_tis.append((w, t))
1551 wis_tis.append((w, None))
1553 return map(lambda (wi, ti): formatWaitInfo(wi) + formatTurnstileInfo(ti), wis_tis)
1555 def SaveStackshotReport(j, outfile_name, incomplete):
1557 from operator import itemgetter, attrgetter
1558 ss = j.get('kcdata_stackshot')
1560 print "No KCDATA_BUFFER_BEGIN_STACKSHOT
object found
. Skipping writing report
."
1563 timestamp = ss.get('usecs_since_epoch')
1565 timestamp = time.strftime("%Y
-%m
-%d %H
:%M
:%S
+0000",time.gmtime(timestamp / 1000000 if timestamp else None))
1566 except ValueError, e:
1567 print "couldn
't convert timestamp:", str(e)
1570 os_version = ss.get('osversion
', 'Unknown
')
1571 timebase = ss.get('mach_timebase_info
', {"denom": 1, "numer": 1})
1574 shared_cache_info = ss.get('shared_cache_dyld_load_info
')
1575 if shared_cache_info:
1576 shared_cache_base_addr = shared_cache_info['imageSlidBaseAddress
']
1577 dsc_common = [format_uuid(shared_cache_info['imageUUID
']), shared_cache_info['imageSlidBaseAddress
'], "S" ]
1578 print "Shared cache UUID found from the binary data is <%s> " % str(dsc_common[0])
1580 dsc_layout = ss.get('system_shared_cache_layout
')
1584 print "Found in memory system shared cache layout with {} images".format(len(dsc_layout))
1585 slide = ss.get('shared_cache_dyld_load_info
')['imageLoadAddress
']
1587 for image in dsc_layout:
1588 dsc_libs.append([format_uuid(image['imageUUID
']), image['imageLoadAddress
'] + slide, "C"])
1590 AllImageCatalog = []
1592 obj["kernel"] = os_version
1593 if timestamp is not None:
1594 obj["date"] = timestamp
1595 obj["reason"] = "kernel panic stackshot"
1596 obj["incident"] = "ABCDEFGH-1234-56IJ-789K-0LMNOPQRSTUV"
1597 obj["crashReporterKey"] = "12ab34cd45aabbccdd6712ab34cd45aabbccdd67"
1598 obj["bootArgs"] = ss.get('boot_args
','')
1599 obj["frontmostPids"] = [0]
1600 obj["exception"] = "0xDEADF157"
1601 obj["processByPid"] = {}
1604 obj["reason"] = "!!!INCOMPLETE!!! kernel panic stackshot"
1605 obj["notes"] = "This stackshot report generated from incomplete data! Some information is missing! "
1607 processByPid = obj["processByPid"]
1608 ssplist = ss.get('task_snapshots
', {})
1611 kc_uuid = ssplist["0"].get('kernelcache_load_info
', None)
1613 kernelcache_uuid = [format_uuid(kc_uuid['imageUUID
']), kc_uuid['imageLoadAddress
'], "U" ]
1614 kern_load_info.append(kernelcache_uuid)
1616 kl_infos = ssplist["0"].get("dyld_load_info", [])
1617 for dlinfo in kl_infos:
1618 kern_load_info.append([format_uuid(dlinfo['imageUUID
']), dlinfo['imageLoadAddress
'], "K"])
1620 kl_infos_text_exec = ssplist["0"].get("dyld_load_info_text_exec", [])
1621 for dlinfo in kl_infos_text_exec:
1622 kern_load_info.append([format_uuid(dlinfo['imageUUID
']), dlinfo['imageLoadAddress
'], "T"])
1624 for pid,piddata in ssplist.iteritems():
1625 processByPid[str(pid)] = {}
1626 tsnap = processByPid[str(pid)]
1627 pr_lib_dsc = dsc_common
1628 if 'shared_cache_dyld_load_info
' in tsnap:
1629 if 'imageSlidBaseAddress
' in tsnap.get('shared_cache_dyld_load_info
'):
1630 shared_cache_base_addr = tsnap.get('shared_cache_dyld_load_info
')['imageSlidBaseAddress
']
1632 print "Specific task shared cache format does not include slid shared cache base address. Skipping writing report."
1635 pr_lib_dsc = [format_uuid(tsnap.get('shared_cache_dyld_load_info
')['imageUUID
']),
1636 tsnap.get('shared_cache_dyld_load_info
')['imageSlidBaseAddress
'],
1640 if len(dsc_libs) == 0 and pr_lib_dsc:
1641 pr_libs.append(pr_lib_dsc)
1647 for dlinfo in piddata.get('dyld_load_info
',[]):
1648 pr_libs.append([format_uuid(dlinfo['imageUUID
']), dlinfo['imageLoadAddress
'], _lib_type])
1650 pr_libs.extend(kern_load_info)
1651 pr_libs.extend(dsc_libs)
1653 pr_libs.sort(key=itemgetter(1))
1655 if 'task_snapshot
' not in piddata:
1657 tasksnap = piddata['task_snapshot
']
1658 tsnap["pid"] = tasksnap["ts_pid"]
1659 if 'ts_asid
' in piddata:
1660 tsnap["asid"] = piddata["ts_asid"]
1662 if 'ts_pagetable
' in piddata:
1664 for tte in piddata["ts_pagetable"]:
1665 pagetables.append(tte)
1666 tsnap["pageTables"] = pagetables
1668 tsnap["residentMemoryBytes"] = tasksnap["ts_task_size"]
1669 tsnap["timesDidThrottle"] = tasksnap["ts_did_throttle"]
1670 tsnap["systemTimeTask"] = GetSecondsFromMATime(tasksnap["ts_system_time_in_terminated_th"], timebase)
1671 tsnap["pageIns"] = tasksnap["ts_pageins"]
1672 tsnap["pageFaults"] = tasksnap["ts_faults"]
1673 tsnap["userTimeTask"] = GetSecondsFromMATime(tasksnap[ "ts_user_time_in_terminated_thre"], timebase)
1674 tsnap["procname"] = tasksnap["ts_p_comm"]
1675 tsnap["copyOnWriteFaults"] = tasksnap["ts_cow_faults"]
1676 tsnap["timesThrottled"] = tasksnap["ts_was_throttled"]
1677 tsnap["threadById"] = {}
1678 threadByID = tsnap["threadById"]
1679 thlist = piddata.get('thread_snapshots
', {})
1680 for tid,thdata in thlist.iteritems():
1681 threadByID[str(tid)] = {}
1682 thsnap = threadByID[str(tid)]
1683 if "thread_snapshot" not in thdata:
1684 print "Found broken thread state for thread ID: %s." % tid
1686 threadsnap = thdata["thread_snapshot"]
1687 thsnap["userTime"] = GetSecondsFromMATime(threadsnap["ths_user_time"], timebase)
1688 thsnap["id"] = threadsnap["ths_thread_id"]
1689 thsnap["basePriority"] = threadsnap["ths_base_priority"]
1690 thsnap["systemTime"] = threadsnap["ths_sys_time"]
1691 thsnap["schedPriority"] = threadsnap["ths_sched_priority"]
1692 thsnap["state"] = GetStateDescription(threadsnap['ths_state
'])
1693 thsnap["qosEffective"] = threadsnap["ths_eqos"]
1694 thsnap["qosRequested"] = threadsnap["ths_rqos"]
1696 if "pth_name" in thdata:
1697 thsnap["name"] = thdata["pth_name"];
1699 if threadsnap['ths_continuation
']:
1700 thsnap["continuation"] = GetSymbolInfoForFrame(AllImageCatalog, pr_libs, threadsnap['ths_continuation
'])
1701 if "kernel_stack_frames" in thdata:
1703 for f in thdata["kernel_stack_frames"]:
1704 kuserframes.append(GetSymbolInfoForFrame(AllImageCatalog, pr_libs, f['lr
']))
1705 thsnap["kernelFrames"] = kuserframes
1707 if "user_stack_frames" in thdata:
1709 for f in thdata["user_stack_frames"]:
1710 uframes.append(GetSymbolInfoForFrame(AllImageCatalog, pr_libs, f['lr
']))
1711 thsnap["userFrames"] = uframes
1713 if "user_stacktop" in thdata:
1714 (address,) = struct.unpack("<Q", struct.pack("B"*8, *thdata["user_stacktop"]["stack_contents"]))
1715 thsnap["userStacktop"] = GetSymbolInfoForFrame(AllImageCatalog, pr_libs, address)
1717 if threadsnap['ths_wait_event
']:
1718 thsnap["waitEvent"] = GetSymbolInfoForFrame(AllImageCatalog, pr_libs, threadsnap['ths_wait_event
'])
1720 if 'thread_waitinfo
' in piddata and 'thread_turnstileinfo
' in piddata:
1721 tsnap['waitInfo
'] = formatWaitInfoWithTurnstiles(piddata['thread_waitinfo
'] , piddata['thread_turnstileinfo
'])
1722 elif 'thread_waitinfo
' in piddata:
1723 tsnap['waitInfo
'] = map(formatWaitInfo, piddata['thread_waitinfo
'])
1725 obj['binaryImages
'] = AllImageCatalog
1726 if outfile_name == '-':
1729 fh = open(outfile_name, "w")
1732 header['bug_type
'] = 288
1733 if timestamp is not None:
1734 header['timestamp
'] = timestamp
1735 header['os_version
'] = os_version
1736 fh.write(json.dumps(header))
1739 fh.write(json.dumps(obj, sort_keys=False, indent=2, separators=(',', ': ')))
1742 ## Base utils for interacting with shell ##
1743 def RunCommand(bash_cmd_string, get_stderr = True):
1745 returns: (int,str) : exit_code and output_str
1747 print "RUNNING: %s" % bash_cmd_string
1748 cmd_args = shlex.split(bash_cmd_string)
1753 output_str = subprocess.check_output(cmd_args, stderr=subprocess.STDOUT)
1755 output_str = subprocess.check_output(cmd_args, stderr=None)
1756 except subprocess.CalledProcessError, e:
1757 exit_code = e.returncode
1759 return (exit_code, output_str)
1762 parser = argparse.ArgumentParser(description="Decode a kcdata binary file.")
1763 parser.add_argument("-l", "--listtypes", action="store_true", required=False, default=False,
1764 help="List all known types",
1765 dest="list_known_types")
1767 parser.add_argument("-s", "--stackshot", required=False, default=False,
1768 help="Generate a stackshot report file",
1769 dest="stackshot_file")
1771 parser.add_argument("--multiple", help="look for multiple stackshots in a single file", action='store_true
')
1773 parser.add_argument("-p", "--plist", required=False, default=False,
1774 help="output as plist", action="store_true")
1776 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")
1777 parser.add_argument("--pretty", default=False, action='store_true
', help="make the output a little more human readable")
1778 parser.add_argument("--incomplete", action='store_true
', help="accept incomplete data")
1779 parser.add_argument("kcdata_file", type=argparse.FileType('r
'), help="Path to a kcdata binary file.")
1781 class VerboseAction(argparse.Action):
1782 def __call__(self, parser, namespace, values, option_string=None):
1783 logging.basicConfig(level=logging.INFO, stream=sys.stderr, format='%(message)s')
1784 parser.add_argument('-v
', "--verbose", action=VerboseAction, nargs=0)
1786 @contextlib.contextmanager
1787 def data_from_stream(stream):
1789 fmap = mmap.mmap(stream.fileno(), 0, mmap.MAP_SHARED, mmap.PROT_READ)
1798 def iterate_kcdatas(kcdata_file):
1799 with data_from_stream(kcdata_file) as data:
1800 iterator = kcdata_item_iterator(data)
1801 kcdata_buffer = KCObject.FromKCItem(iterator.next())
1803 if isinstance(kcdata_buffer, KCCompressedBufferObject):
1804 kcdata_buffer.ReadItems(iterator)
1805 decompressed = kcdata_buffer.Decompress(data)
1806 iterator = kcdata_item_iterator(decompressed)
1807 kcdata_buffer = KCObject.FromKCItem(iterator.next())
1809 if not isinstance(kcdata_buffer, KCBufferObject):
1810 # ktrace stackshot chunk
1811 iterator = kcdata_item_iterator(data[16:])
1812 kcdata_buffer = KCObject.FromKCItem(iterator.next())
1814 if not isinstance(kcdata_buffer, KCBufferObject):
1816 decoded = base64.b64decode(data)
1820 iterator = kcdata_item_iterator(decoded)
1821 kcdata_buffer = KCObject.FromKCItem(iterator.next())
1822 if not isinstance(kcdata_buffer, KCBufferObject):
1824 from io import BytesIO
1826 decompressed = gzip.GzipFile(fileobj=BytesIO(data[:])).read()
1830 iterator = kcdata_item_iterator(decompressed)
1831 kcdata_buffer = KCObject.FromKCItem(iterator.next())
1833 if not isinstance(kcdata_buffer, KCBufferObject):
1834 raise Exception, "unknown file type"
1837 kcdata_buffer.ReadItems(iterator)
1840 for magic in iterator:
1841 kcdata_buffer = KCObject.FromKCItem(magic)
1842 if kcdata_buffer.i_type == 0:
1844 if not isinstance(kcdata_buffer, KCBufferObject):
1845 raise Exception, "unknown file type"
1846 kcdata_buffer.ReadItems(iterator)
1851 if isinstance(data, list):
1852 return map(prettify, data);
1854 elif isinstance(data, dict):
1856 for key, value in data.items():
1857 if 'uuid
' in key.lower() and isinstance(value, list) and len(value) == 16:
1858 value = '%02X
%02X
%02X
%02X
-%02X
%02X
-%02X
%02X
-%02X
%02X
-%02X
%02X
%02X
%02X
%02X
%02X
' % tuple(value)
1859 elif 'address
' in key.lower() and isinstance(value, (int, long)):
1860 value = '0x
%X' % value
1862 value = '0x
%X' % value
1863 elif key == 'thread_waitinfo
':
1864 value = map(formatWaitInfo, value)
1865 elif key == 'stack_contents
':
1866 (address,) = struct.unpack("<Q", struct.pack("B"*8, *value))
1867 value = '0x
%X' % address
1869 value = prettify(value);
1870 newdata[key] = value
1878 if __name__ == '__main__
':
1879 args = parser.parse_args()
1881 if args.multiple and args.stackshot_file:
1882 raise NotImplementedError
1884 if args.list_known_types:
1885 for (n, t) in KNOWN_TYPES_COLLECTION.items():
1886 print "%d : %s " % (n, str(t))
1889 if args.incomplete or args.stackshot_file:
1890 G.accept_incomplete_data = True
1892 for i,kcdata_buffer in enumerate(iterate_kcdatas(args.kcdata_file)):
1893 if i > 0 and not args.multiple:
1896 str_data = "{" + kcdata_buffer.GetJsonRepr() + "}"
1897 str_data = str_data.replace("\t", " ")
1900 json_obj = json.loads(str_data)
1902 print >>sys.stderr, "JSON reparsing failed! Printing string data!\n"
1904 print textwrap.fill(str_data, 100)
1908 json_obj = prettify(json_obj)
1910 if args.stackshot_file:
1911 SaveStackshotReport(json_obj, args.stackshot_file, G.data_was_incomplete)
1914 plist = Foundation.NSPropertyListSerialization.dataWithPropertyList_format_options_error_(
1915 json_obj, Foundation.NSPropertyListXMLFormat_v1_0, 0, None)[0].bytes().tobytes()
1916 #sigh. on some pythons long integers are getting output with L's
in the plist
.
1917 plist
= re
.sub(r
'^(\s*<integer>\d+)L(</integer>\s*)$', r
"\1\2", plist
, flags
=re
.MULTILINE
)
1920 print json
.dumps(json_obj
, sort_keys
=True, indent
=4, separators
=(',', ': '))