19 # can be removed once we move to Python3.1+
20 from future
.utils
.surrogateescape
import register_surrogateescape
21 register_surrogateescape()
23 class Globals(object):
26 G
.accept_incomplete_data
= False
27 G
.data_was_incomplete
= False
30 'KCDATA_TYPE_INVALID': 0x0,
31 'KCDATA_TYPE_STRING_DESC': 0x1,
32 'KCDATA_TYPE_UINT32_DESC': 0x2,
33 'KCDATA_TYPE_UINT64_DESC': 0x3,
34 'KCDATA_TYPE_INT32_DESC': 0x4,
35 'KCDATA_TYPE_INT64_DESC': 0x5,
36 'KCDATA_TYPE_BINDATA_DESC': 0x6,
37 'KCDATA_TYPE_ARRAY': 0x11,
38 'KCDATA_TYPE_TYPEDEFINITION': 0x12,
39 'KCDATA_TYPE_CONTAINER_BEGIN': 0x13,
40 'KCDATA_TYPE_CONTAINER_END': 0x14,
42 'KCDATA_TYPE_ARRAY_PAD0': 0x20,
43 'KCDATA_TYPE_ARRAY_PAD1': 0x21,
44 'KCDATA_TYPE_ARRAY_PAD2': 0x22,
45 'KCDATA_TYPE_ARRAY_PAD3': 0x23,
46 'KCDATA_TYPE_ARRAY_PAD4': 0x24,
47 'KCDATA_TYPE_ARRAY_PAD5': 0x25,
48 'KCDATA_TYPE_ARRAY_PAD6': 0x26,
49 'KCDATA_TYPE_ARRAY_PAD7': 0x27,
50 'KCDATA_TYPE_ARRAY_PAD8': 0x28,
51 'KCDATA_TYPE_ARRAY_PAD9': 0x29,
52 'KCDATA_TYPE_ARRAY_PADa': 0x2a,
53 'KCDATA_TYPE_ARRAY_PADb': 0x2b,
54 'KCDATA_TYPE_ARRAY_PADc': 0x2c,
55 'KCDATA_TYPE_ARRAY_PADd': 0x2d,
56 'KCDATA_TYPE_ARRAY_PADe': 0x2e,
57 'KCDATA_TYPE_ARRAY_PADf': 0x2f,
59 'KCDATA_TYPE_LIBRARY_LOADINFO': 0x30,
60 'KCDATA_TYPE_LIBRARY_LOADINFO64': 0x31,
61 'KCDATA_TYPE_TIMEBASE': 0x32,
62 'KCDATA_TYPE_MACH_ABSOLUTE_TIME': 0x33,
63 'KCDATA_TYPE_TIMEVAL': 0x34,
64 'KCDATA_TYPE_USECS_SINCE_EPOCH': 0x35,
65 'KCDATA_TYPE_PID': 0x36,
66 'KCDATA_TYPE_PROCNAME': 0x37,
67 'KCDATA_TYPE_NESTED_KCDATA': 0x38,
69 'STACKSHOT_KCCONTAINER_TASK': 0x903,
70 'STACKSHOT_KCCONTAINER_THREAD': 0x904,
71 'STACKSHOT_KCTYPE_DONATING_PIDS': 0x907,
72 'STACKSHOT_KCTYPE_SHAREDCACHE_LOADINFO': 0x908,
73 'STACKSHOT_KCTYPE_THREAD_NAME': 0x909,
74 'STACKSHOT_KCTYPE_KERN_STACKFRAME': 0x90A,
75 'STACKSHOT_KCTYPE_KERN_STACKFRAME64': 0x90B,
76 'STACKSHOT_KCTYPE_USER_STACKFRAME': 0x90C,
77 'STACKSHOT_KCTYPE_USER_STACKFRAME64': 0x90D,
78 'STACKSHOT_KCTYPE_BOOTARGS': 0x90E,
79 'STACKSHOT_KCTYPE_OSVERSION': 0x90F,
80 'STACKSHOT_KCTYPE_KERN_PAGE_SIZE': 0x910,
81 'STACKSHOT_KCTYPE_JETSAM_LEVEL': 0x911,
82 'STACKSHOT_KCTYPE_DELTA_SINCE_TIMESTAMP': 0x912,
83 'STACKSHOT_KCTYPE_KERN_STACKLR': 0x913,
84 'STACKSHOT_KCTYPE_KERN_STACKLR64': 0x914,
85 'STACKSHOT_KCTYPE_USER_STACKLR': 0x915,
86 'STACKSHOT_KCTYPE_USER_STACKLR64': 0x916,
87 'STACKSHOT_KCTYPE_NONRUNNABLE_TIDS': 0x917,
88 'STACKSHOT_KCTYPE_NONRUNNABLE_TASKS': 0x918,
89 'STACKSHOT_KCTYPE_CPU_TIMES': 0x919,
90 'STACKSHOT_KCTYPE_STACKSHOT_DURATION': 0x91a,
91 'STACKSHOT_KCTYPE_STACKSHOT_FAULT_STATS': 0x91b,
92 'STACKSHOT_KCTYPE_KERNELCACHE_LOADINFO': 0x91c,
93 'STACKSHOT_KCTYPE_THREAD_WAITINFO' : 0x91d,
94 'STACKSHOT_KCTYPE_THREAD_GROUP_SNAPSHOT' : 0x91e,
95 'STACKSHOT_KCTYPE_THREAD_GROUP' : 0x91f,
96 'STACKSHOT_KCTYPE_JETSAM_COALITION_SNAPSHOT' : 0x920,
97 'STACKSHOT_KCTYPE_JETSAM_COALITION' : 0x921,
98 'STACKSHOT_KCTYPE_THREAD_POLICY_VERSION': 0x922,
99 'STACKSHOT_KCTYPE_INSTRS_CYCLES' : 0x923,
100 'STACKSHOT_KCTYPE_USER_STACKTOP' : 0x924,
101 'STACKSHOT_KCTYPE_ASID' : 0x925,
102 'STACKSHOT_KCTYPE_PAGE_TABLES' : 0x926,
103 'STACKSHOT_KCTYPE_SYS_SHAREDCACHE_LAYOUT' : 0x927,
104 'STACKSHOT_KCTYPE_THREAD_DISPATCH_QUEUE_LABEL' : 0x928,
105 'STACKSHOT_KCTYPE_THREAD_TURNSTILEINFO' : 0x929,
106 'STACKSHOT_KCTYPE_TASK_CPU_ARCHITECTURE' : 0x92a,
107 'STACKSHOT_KCTYPE_LATENCY_INFO' : 0x92b,
108 'STACKSHOT_KCTYPE_LATENCY_INFO_TASK' : 0x92c,
109 'STACKSHOT_KCTYPE_LATENCY_INFO_THREAD' : 0x92d,
110 'STACKSHOT_KCTYPE_LOADINFO64_TEXT_EXEC' : 0x92e,
112 'STACKSHOT_KCTYPE_TASK_DELTA_SNAPSHOT': 0x940,
113 'STACKSHOT_KCTYPE_THREAD_DELTA_SNAPSHOT': 0x941,
117 'KCDATA_TYPE_BUFFER_END': 0xF19158ED,
120 'TASK_CRASHINFO_EXTMODINFO': 0x801,
121 'TASK_CRASHINFO_BSDINFOWITHUNIQID': 0x802,
122 'TASK_CRASHINFO_TASKDYLD_INFO': 0x803,
123 'TASK_CRASHINFO_UUID': 0x804,
124 'TASK_CRASHINFO_PID': 0x805,
125 'TASK_CRASHINFO_PPID': 0x806,
127 # Don't want anyone using this. It's struct rusage from whatever machine generated the data
128 #'TASK_CRASHINFO_RUSAGE': 0x807,
131 'TASK_CRASHINFO_RUSAGE_INFO': 0x808,
132 'TASK_CRASHINFO_PROC_NAME': 0x809,
133 'TASK_CRASHINFO_PROC_STARTTIME': 0x80B,
134 'TASK_CRASHINFO_USERSTACK': 0x80C,
135 'TASK_CRASHINFO_ARGSLEN': 0x80D,
136 'TASK_CRASHINFO_EXCEPTION_CODES': 0x80E,
137 'TASK_CRASHINFO_PROC_PATH': 0x80F,
138 'TASK_CRASHINFO_PROC_CSFLAGS': 0x810,
139 'TASK_CRASHINFO_PROC_STATUS': 0x811,
140 'TASK_CRASHINFO_UID': 0x812,
141 'TASK_CRASHINFO_GID': 0x813,
142 'TASK_CRASHINFO_PROC_ARGC': 0x814,
143 'TASK_CRASHINFO_PROC_FLAGS': 0x815,
144 'TASK_CRASHINFO_CPUTYPE': 0x816,
145 'TASK_CRASHINFO_WORKQUEUEINFO': 0x817,
146 'TASK_CRASHINFO_RESPONSIBLE_PID': 0x818,
147 'TASK_CRASHINFO_DIRTY_FLAGS': 0x819,
148 'TASK_CRASHINFO_CRASHED_THREADID': 0x81A,
149 'TASK_CRASHINFO_COALITION_ID': 0x81B,
150 'EXIT_REASON_SNAPSHOT': 0x1001,
151 'EXIT_REASON_USER_DESC': 0x1002,
152 'EXIT_REASON_USER_PAYLOAD': 0x1003,
153 'EXIT_REASON_CODESIGNING_INFO': 0x1004,
154 'EXIT_REASON_WORKLOOP_ID': 0x1005,
155 'EXIT_REASON_DISPATCH_QUEUE_NO': 0x1006,
156 'KCDATA_BUFFER_BEGIN_CRASHINFO': 0xDEADF157,
157 'KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT': 0xDE17A59A,
158 'KCDATA_BUFFER_BEGIN_STACKSHOT': 0x59a25807,
159 'KCDATA_BUFFER_BEGIN_COMPRESSED': 0x434f4d50,
160 'KCDATA_BUFFER_BEGIN_OS_REASON': 0x53A20900,
161 'KCDATA_BUFFER_BEGIN_XNUPOST_CONFIG': 0x1E21C09F
163 kcdata_type_def_rev
= dict((v
, k
) for k
, v
in kcdata_type_def
.iteritems())
165 KNOWN_TYPES_COLLECTION
= {}
167 KNOWN_TOPLEVEL_CONTAINER_TYPES
= ()
170 return type('enum', (), args
)
173 # Decode bytes as UTF-8, using surrogateescape if there are invalid UTF-8
174 # sequences; see PEP-383
176 def BytesToString(b
):
177 return b
.decode('utf-8', errors
="surrogateescape")
179 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)
182 LEGAL_OLD_STYLE_ARRAY_TYPE_NAMES
= ['KCDATA_TYPE_LIBRARY_LOADINFO',
183 'KCDATA_TYPE_LIBRARY_LOADINFO64',
184 'STACKSHOT_KCTYPE_KERN_STACKFRAME',
185 'STACKSHOT_KCTYPE_USER_STACKFRAME',
186 'STACKSHOT_KCTYPE_KERN_STACKFRAME64',
187 'STACKSHOT_KCTYPE_USER_STACKFRAME64',
188 'STACKSHOT_KCTYPE_DONATING_PIDS',
189 'STACKSHOT_KCTYPE_THREAD_DELTA_SNAPSHOT']
191 KCDATA_FLAGS_STRUCT_PADDING_MASK
= 0xf
192 KCDATA_FLAGS_STRUCT_HAS_PADDING
= 0x80
194 class KCSubTypeElement(object):
195 """convert kcdata_subtype_descriptor to """
196 _unpack_formats
= (None, 'c', 'b', 'B', 'h', 'H', 'i', 'I', 'q', 'Q')
197 _ctypes
= ('Unknown', 'char', 'int8_t', 'uint8_t', 'int16_t', 'uint16_t', 'int32_t', 'uint32_t', 'int64_t', 'uint64_t')
199 def __init__(self
, st_name
, st_type
, st_size
, st_offset
=0, st_flag
=0, custom_repr
=None):
201 self
.offset
= st_offset
202 self
.type_id
= st_type
203 if st_type
<= 0 or st_type
> KCSUBTYPE_TYPE
.KC_ST_UINT64
:
204 raise ValueError("Invalid type passed %d" % st_type
)
205 self
.unpack_fmt
= KCSubTypeElement
._unpack
_formats
[self
.type_id
]
207 self
.totalsize
= st_size
209 self
.is_array_type
= False
210 self
.custom_JsonRepr
= custom_repr
211 if (st_flag
& 0x1) == 0x1:
212 self
.is_array_type
= True
213 self
.size
= st_size
& 0xffff
214 self
.count
= (st_size
>> 16) & 0xffff
215 self
.totalsize
= self
.size
* self
.count
218 def GetSizeForArray(el_count
, el_size
):
219 return ((el_count
& 0xffff) << 16) |
(el_size
& 0xffff)
222 def FromBinaryTypeData(byte_data
):
223 (st_flag
, st_type
, st_offset
, st_size
, st_name
) = struct
.unpack_from('=BBHI32s', byte_data
)
224 st_name
= BytesToString(st_name
.rstrip('\0'))
225 return KCSubTypeElement(st_name
, st_type
, st_size
, st_offset
, st_flag
)
228 def FromBasicCtype(st_name
, st_type
, st_offset
=0, legacy_size
=None):
229 if st_type
<= 0 or st_type
> KCSUBTYPE_TYPE
.KC_ST_UINT64
:
230 raise ValueError("Invalid type passed %d" % st_type
)
231 st_size
= struct
.calcsize(KCSubTypeElement
._unpack
_formats
[st_type
])
233 retval
= KCSubTypeElement(st_name
, st_type
, st_size
, st_offset
, st_flag
, KCSubTypeElement
._get
_naked
_element
_value
)
235 retval
.legacy_size
= legacy_size
239 def FromKCSubTypeElement(other
, name_override
=''):
240 _copy
= copy
.copy(other
)
242 _copy
.name
= name_override
248 def GetTotalSize(self
):
249 return self
.totalsize
251 def GetValueAsString(self
, base_data
, array_pos
=0):
252 v
= self
.GetValue(base_data
, array_pos
)
253 if isinstance(v
, bytes):
254 return BytesToString(v
)
257 def GetValue(self
, base_data
, array_pos
=0):
258 return struct
.unpack_from(self
.unpack_fmt
, base_data
[self
.offset
+ (array_pos
* self
.size
):])[0]
261 def _get_naked_element_value(elementValue
, elementName
):
262 return json
.dumps(elementValue
)
265 if self
.is_array_type
:
266 return '[%d,%d] %s %s[%d];' % (self
.offset
, self
.totalsize
, self
.GetCTypeDesc(), self
.name
, self
.count
)
267 return '[%d,%d] %s %s;' % (self
.offset
, self
.totalsize
, self
.GetCTypeDesc(), self
.name
)
272 def GetCTypeDesc(self
):
273 return KCSubTypeElement
._ctypes
[self
.type_id
]
275 def GetStringRepr(self
, base_data
):
276 if not self
.is_array_type
:
277 return self
.GetValueAsString(base_data
)
278 if self
.type_id
== KCSUBTYPE_TYPE
.KC_ST_CHAR
:
280 if len(base_data
) < str_len
:
281 str_len
= len(base_data
)
283 for i
in range(str_len
):
284 _v
= self
.GetValue(base_data
, i
)
287 str_arr
.append(self
.GetValueAsString(base_data
, i
))
288 return json
.dumps(''.join(str_arr
))
291 if count
> len(base_data
)/self
.size
:
292 count
= len(base_data
)/self
.size
294 o
= '[' + ','.join([self
.GetValueAsString(base_data
, i
) for i
in range(count
)]) + ']'
298 def GetJsonRepr(self
, base_data
, flags
=0):
299 if (flags
& (KCDATA_FLAGS_STRUCT_HAS_PADDING | KCDATA_FLAGS_STRUCT_PADDING_MASK
)) != 0:
300 padding
= (flags
& KCDATA_FLAGS_STRUCT_PADDING_MASK
)
302 base_data
= base_data
[:-padding
]
303 if self
.custom_JsonRepr
:
304 if self
.is_array_type
:
305 e_data
= [self
.GetValue(base_data
, i
) for i
in range(self
.count
)]
307 e_data
= self
.GetValue(base_data
)
308 return self
.custom_JsonRepr(e_data
, self
.name
)
309 return self
.GetStringRepr(base_data
)
312 return self
.totalsize
314 def ShouldSkip(self
, data
):
315 return len(data
) < self
.offset
+ self
.totalsize
317 def ShouldMerge(self
):
321 class KCTypeDescription(object):
322 def __init__(self
, t_type_id
, t_elements
=[], t_name
='anon', custom_repr
=None, legacy_size
=None, merge
=False, naked
=False):
323 self
.type_id
= t_type_id
324 self
.elements
= t_elements
327 self
.custom_JsonRepr
= custom_repr
329 self
.legacy_size
= legacy_size
332 for e
in self
.elements
:
333 self
.totalsize
+= e
.GetTotalSize()
335 def ValidateData(self
, base_data
):
336 if len(base_data
) >= self
.totalsize
:
347 o
= '%s {\n\t' % self.name + "\n\t".join([str(e) for e in self.elements]) + '\n};'
351 def FromKCTypeDescription(other
, t_type_id
, t_name
):
352 retval
= KCTypeDescription(t_type_id
, other
.elements
, t_name
, other
.custom_JsonRepr
,
353 legacy_size
=getattr(other
, 'legacy_size', None))
356 def ShouldMerge(self
):
359 def GetJsonRepr(self
, base_data
, flags
):
360 if (flags
& (KCDATA_FLAGS_STRUCT_HAS_PADDING | KCDATA_FLAGS_STRUCT_PADDING_MASK
)) != 0:
361 padding
= (flags
& KCDATA_FLAGS_STRUCT_PADDING_MASK
)
363 base_data
= base_data
[:-padding
]
364 elif hasattr(self
, 'legacy_size') and len(base_data
) == self
.legacy_size
+ ((-self
.legacy_size
) & 0xf):
365 base_data
= base_data
[:self
.legacy_size
]
366 if self
.custom_JsonRepr
:
367 return self
.custom_JsonRepr([e
.GetValue(base_data
) for e
in self
.elements
])
369 o
= ", ".join([e
.GetJsonRepr(base_data
) for e
in self
.elements
if not e
.ShouldSkip(base_data
)])
371 o
= ", ".join(['"%s": %s' % (e
.GetName(), e
.GetJsonRepr(base_data
)) for e
in self
.elements
if not e
.ShouldSkip(base_data
)])
377 return max(st
.totalsize
+ st
.offset
for st
in self
.elements
)
380 def GetTypeNameForKey(k
):
382 if k
in KNOWN_TYPES_COLLECTION
:
383 retval
= KNOWN_TYPES_COLLECTION
[k
].GetName()
384 elif k
in kcdata_type_def_rev
:
385 retval
= kcdata_type_def_rev
[k
]
389 def GetTypeForName(n
):
391 if n
in kcdata_type_def
:
392 ret
= kcdata_type_def
[n
]
396 LEGAL_OLD_STYLE_ARRAY_TYPES
= map(GetTypeForName
, LEGAL_OLD_STYLE_ARRAY_TYPE_NAMES
)
398 kcdata_type_def_rev
[GetTypeForName('KCDATA_BUFFER_BEGIN_STACKSHOT')] = 'kcdata_stackshot'
399 kcdata_type_def_rev
[GetTypeForName('KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT')] = 'kcdata_delta_stackshot'
400 kcdata_type_def_rev
[GetTypeForName('KCDATA_BUFFER_BEGIN_CRASHINFO')] = 'kcdata_crashinfo'
401 kcdata_type_def_rev
[GetTypeForName('KCDATA_BUFFER_BEGIN_OS_REASON')] = 'kcdata_reason'
402 kcdata_type_def_rev
[GetTypeForName('STACKSHOT_KCCONTAINER_TASK')] = 'task_snapshots'
403 kcdata_type_def_rev
[GetTypeForName('STACKSHOT_KCCONTAINER_THREAD')] = 'thread_snapshots'
404 kcdata_type_def_rev
[GetTypeForName('KCDATA_BUFFER_BEGIN_XNUPOST_CONFIG')] = 'xnupost_testconfig'
406 class Indent(object):
409 def __call__(self
, end
=False):
411 return " " * (self
.n
-4)
414 @contextlib.contextmanager
424 class KCObject(object):
426 def __init__(self
, type_code
, data
, offset
, flags
=0):
428 self
.i_type
= type_code
431 self
.i_size
= len(data
)
433 self
.obj_collection
= []
435 self
.is_container_type
= False
436 self
.is_array_type
= False
437 self
.is_naked_type
= False
438 self
.nested_kcdata
= None
439 self
.i_name
= GetTypeNameForKey(type_code
)
443 if self
.i_type
== GetTypeForName('KCDATA_TYPE_CONTAINER_BEGIN'):
444 self
.__class
__ = KCContainerObject
445 elif self
.i_type
== GetTypeForName('KCDATA_BUFFER_BEGIN_COMPRESSED'):
446 self
.__class
__ = KCCompressedBufferObject
447 elif self
.i_type
in KNOWN_TOPLEVEL_CONTAINER_TYPES
:
448 self
.__class
__ = KCBufferObject
450 self
.InitAfterParse()
453 return "<KCObject at 0x%x>" % self
.offset
455 def InitAfterParse(self
):
459 def FromKCItem(kcitem
):
460 return KCObject(kcitem
.i_type
, kcitem
.i_data
, kcitem
.i_offset
, kcitem
.i_flags
)
462 def IsContainerEnd(self
):
463 return self
.i_type
== GetTypeForName('KCDATA_TYPE_CONTAINER_END')
465 def IsBufferEnd(self
):
466 return self
.i_type
== GetTypeForName('KCDATA_TYPE_BUFFER_END')
469 return self
.is_array_type
471 def ShouldMerge(self
):
472 if self
.nested_kcdata
:
474 elif not self
.is_array_type
and self
.i_type
in KNOWN_TYPES_COLLECTION
:
475 return KNOWN_TYPES_COLLECTION
[self
.i_type
].ShouldMerge()
479 def GetJsonRepr(self
):
480 if self
.is_array_type
:
481 return '[' + ', '.join([i
.GetJsonRepr() for i
in self
.obj_collection
]) + ']'
482 if self
.i_type
in KNOWN_TYPES_COLLECTION
:
483 return KNOWN_TYPES_COLLECTION
[self
.i_type
].GetJsonRepr(self
.i_data
, self
.i_flags
)
484 if self
.is_naked_type
:
485 return json
.dumps(self
.obj
)
486 if self
.nested_kcdata
:
487 return self
.nested_kcdata
.GetJsonRepr()
489 raise NotImplementedError("Broken GetJsonRepr implementation")
494 if self
.i_type
== GetTypeForName('KCDATA_TYPE_CONTAINER_BEGIN'):
495 self
.obj
['uniqID'] = self
.i_flags
496 self
.i_name
= str(self
.obj
['uniqID'])
497 self
.obj
['typeID'] = struct
.unpack_from('I', self
.i_data
)[0]
498 logging
.info("0x%08x: %sCONTAINER: %s(%x)" % (self
.offset
, INDENT(), GetTypeNameForKey(self
.obj
['typeID']), self
.i_flags
))
500 elif self
.i_type
in (KNOWN_TOPLEVEL_CONTAINER_TYPES
):
501 self
.obj
['uniqID'] = self
.i_name
502 self
.obj
['typeID'] = self
.i_type
503 logging
.info("0x%08x: %s%s" % (self
.offset
, INDENT(), self
.i_name
))
505 elif self
.i_type
== GetTypeForName('KCDATA_TYPE_CONTAINER_END'):
506 self
.obj
['uniqID'] = self
.i_flags
507 logging
.info("0x%08x: %sEND" % (self
.offset
, INDENT(end
=True)))
509 elif self
.i_type
== GetTypeForName('KCDATA_TYPE_BUFFER_END'):
511 logging
.info("0x%08x: %sEND_BUFFER" % (self
.offset
, INDENT(end
=True)))
513 elif self
.i_type
== GetTypeForName('KCDATA_TYPE_UINT32_DESC'):
514 self
.is_naked_type
= True
515 u_d
= struct
.unpack_from('32sI', self
.i_data
)
516 self
.i_name
= BytesToString(u_d
[0].rstrip('\0'))
518 logging
.info("0x%08x: %s%s" % (self
.offset
, INDENT(), self
.i_name
))
520 elif self
.i_type
== GetTypeForName('KCDATA_TYPE_UINT64_DESC'):
521 self
.is_naked_type
= True
522 u_d
= struct
.unpack_from('32sQ', self
.i_data
)
523 self
.i_name
= BytesToString(u_d
[0].rstrip('\0'))
525 logging
.info("0x%08x: %s%s" % (self
.offset
, INDENT(), self
.i_name
))
527 elif self
.i_type
== GetTypeForName('KCDATA_TYPE_TYPEDEFINITION'):
528 self
.is_naked_type
= True
529 u_d
= struct
.unpack_from('II32s', self
.i_data
)
530 self
.obj
['name'] = u_d
[2].split(chr(0))[0]
531 self
.i_name
= "typedef[%s]" % self
.obj
['name']
532 self
.obj
['typeID'] = u_d
[0]
533 self
.obj
['numOfFields'] = u_d
[1]
535 for i
in range(u_d
[1]):
536 e
= KCSubTypeElement
.FromBinaryTypeData(self
.i_data
[40+(i
*40):])
537 element_arr
.append(e
)
538 type_desc
= KCTypeDescription(u_d
[0], element_arr
, self
.obj
['name'])
539 self
.obj
['fields'] = [str(e
) for e
in element_arr
]
540 KNOWN_TYPES_COLLECTION
[type_desc
.GetTypeID()] = type_desc
541 logging
.info("0x%08x: %s%s" % (self
.offset
, INDENT(), self
.i_name
))
543 elif self
.i_type
== GetTypeForName('KCDATA_TYPE_ARRAY'):
544 self
.is_array_type
= True
545 e_t
= (self
.i_flags
>> 32) & 0xffffffff
546 if e_t
not in LEGAL_OLD_STYLE_ARRAY_TYPES
:
547 raise Exception, "illegal old-style array type: %s (0x%x)" % (GetTypeNameForKey(e_t
), e_t
)
548 e_c
= self
.i_flags
& 0xffffffff
549 e_s
= KNOWN_TYPES_COLLECTION
[e_t
].legacy_size
550 if e_s
* e_c
> self
.i_size
:
551 raise Exception("array too small for its count")
552 self
.obj
['typeID'] = e_t
553 self
.i_name
= GetTypeNameForKey(e_t
)
555 self
.obj
['numOfElements'] = e_c
556 self
.obj
['sizeOfElement'] = e_s
557 logging
.info("0x%08x: %sARRAY: %s" % (self
.offset
, INDENT(), self
.i_name
))
558 #populate the array here by recursive creation of KCObject
559 with INDENT
.indent():
560 for _i
in range(e_c
):
561 _o
= KCObject(e_t
, self
.i_data
[(_i
* e_s
):(_i
* e_s
) + e_s
], self
.offset
+ _i
*e_s
)
562 self
.obj_collection
.append(_o
)
564 elif self
.i_type
>= GetTypeForName('KCDATA_TYPE_ARRAY_PAD0') and self
.i_type
<= GetTypeForName('KCDATA_TYPE_ARRAY_PADf'):
565 self
.is_array_type
= True
566 e_t
= (self
.i_flags
>> 32) & 0xffffffff
567 e_c
= self
.i_flags
& 0xffffffff
568 e_s
= (self
.i_size
- (self
.i_type
& 0xf)) / e_c
if e_c
!= 0 else None
569 self
.obj
['typeID'] = e_t
570 self
.i_name
= GetTypeNameForKey(e_t
)
572 self
.obj
['numOfElements'] = e_c
573 self
.obj
['sizeOfElement'] = e_s
574 logging
.info("0x%08x: %sARRAY: %s" % (self
.offset
, INDENT(), self
.i_name
))
575 #populate the array here by recursive creation of KCObject
576 with INDENT
.indent():
577 for _i
in range(e_c
):
578 _o
= KCObject(e_t
, self
.i_data
[(_i
* e_s
):(_i
* e_s
) + e_s
], self
.offset
+ _i
*e_s
)
579 self
.obj_collection
.append(_o
)
581 elif self
.i_type
== GetTypeForName('KCDATA_TYPE_NESTED_KCDATA'):
582 logging
.info("0x%08x: %sNESTED_KCDATA" % (self
.offset
, INDENT()))
583 with INDENT
.indent():
584 nested_iterator
= kcdata_item_iterator(self
.i_data
[:self
.i_size
])
585 nested_buffer
= KCObject
.FromKCItem(nested_iterator
.next())
586 if not isinstance(nested_buffer
, KCBufferObject
):
587 raise Exception, "nested buffer isn't a KCBufferObject"
588 nested_buffer
.ReadItems(nested_iterator
)
589 self
.nested_kcdata
= nested_buffer
591 elif self
.i_type
in KNOWN_TYPES_COLLECTION
:
592 self
.i_name
= KNOWN_TYPES_COLLECTION
[self
.i_type
].GetName()
593 self
.is_naked_type
= True
594 logging
.info("0x%08x: %s%s" % (self
.offset
, INDENT(), self
.i_name
))
596 self
.is_naked_type
= True
597 #self.obj = "data of len %d" % len(self.i_data)
598 #self.obj = ''.join(["%x" % ki for ki in struct.unpack('%dB' % len(self.i_data), self.i_data)])
599 self
.obj
= map(ord, self
.i_data
)
600 logging
.info("0x%08x: %s%s" % (self
.offset
, INDENT(), self
.i_name
))
603 class KCContainerObject(KCObject
):
604 def __init__(self
, *args
, **kwargs
):
607 def InitAfterParse(self
):
608 self
.obj_container_dict
= {}
609 self
.obj_nested_objs
= {}
611 def ShouldMerge(self
):
614 def GetJsonRepr(self
):
615 # o = '"%s"' % self.obj['uniqID'] + ' : { "typeID" : %d ,' % self.obj['typeID']
616 o
= '"%s"' % self
.obj
['uniqID'] + ' : { '
617 for (k
, v
) in self
.obj_container_dict
.items():
619 o
+= v
.GetJsonRepr() + ","
621 o
+= ' "%s" : ' % k
+ v
.GetJsonRepr() + ","
623 for (k
, v
) in self
.obj_nested_objs
.items():
624 o
+= '"%s" : {' % k + ",".join([vi.GetJsonRepr() for vi in v.values()]) + "} ,"
626 o = o.rstrip(',') + "}"
630 def AddObject(self, kco):
631 assert not kco.IsContainerEnd()
632 if isinstance(kco, KCContainerObject):
633 type_name = GetTypeNameForKey(kco.obj['typeID
'])
634 if type_name not in self.obj_nested_objs:
635 self.obj_nested_objs[type_name] = {}
636 self.obj_nested_objs[type_name][kco.i_name] = kco
638 if kco.i_name in self.obj_container_dict:
639 if kco.IsArray() and self.obj_container_dict[kco.i_name].IsArray():
640 self.obj_container_dict[kco.i_name].obj_collection.extend( kco.obj_collection )
642 self.obj_container_dict[kco.i_name] = kco
644 def IsEndMarker(self, o):
645 if not o.IsContainerEnd():
647 if o.i_flags != self.i_flags:
648 raise Exception, "container end marker doesn't match
"
651 no_end_message = "could
not find container end marker
"
653 def ReadItems(self, iterator):
655 with INDENT.indent():
657 o = KCObject.FromKCItem(i)
658 if self.IsEndMarker(o):
663 if isinstance(o, KCContainerObject):
664 o.ReadItems(iterator)
667 if G.accept_incomplete_data:
668 if not G.data_was_incomplete:
669 print >>sys.stderr, "kcdata
.py WARNING
: data
is incomplete
!"
670 G.data_was_incomplete = True
672 raise Exception, self.no_end_message
676 class KCBufferObject(KCContainerObject):
678 def IsEndMarker(self,o):
679 if o.IsContainerEnd():
680 raise Exception, "container end marker at the toplevel
"
681 return o.IsBufferEnd()
683 no_end_message = "could
not find
buffer end marker
"
685 class KCCompressedBufferObject(KCContainerObject):
687 def ReadItems(self, iterator):
689 with INDENT.indent():
691 o = KCObject.FromKCItem(i)
692 if self.IsEndMarker(o):
693 self.compressed_type = o.i_type
694 self.blob_start = o.offset + 16
697 self.header[o.i_name] = o.obj
699 def IsEndMarker(self, o):
700 return o.i_type in KNOWN_TOPLEVEL_CONTAINER_TYPES
702 def GetCompressedBlob(self, data):
703 if self.header['kcd_c_type'] != 1:
704 raise NotImplementedError
705 blob = data[self.blob_start:self.blob_start+self.header['kcd_c_totalout']]
706 if len(blob) != self.header['kcd_c_totalout']:
710 def Decompress(self, data):
711 start_marker = struct.pack('<IIII', self.compressed_type, 0, 0, 0)
712 end_marker = struct.pack('<IIII', GetTypeForName('KCDATA_TYPE_BUFFER_END'), 0, 0, 0)
713 decompressed = zlib.decompress(self.GetCompressedBlob(data))
714 if len(decompressed) != self.header['kcd_c_totalin']:
715 raise ValueError, "length of decompressed
: %d vs expected
%d" % (len(decompressed), self.header['kcd_c_totalin'])
716 alignbytes = b'\x00' * (-len(decompressed) % 16)
717 return start_marker + decompressed + alignbytes + end_marker
721 """ a basic kcdata_item type object.
723 header_size = 16 # (uint32_t + uint32_t + uint64_t)
725 def __init__(self, item_type, item_size, item_flags, item_data):
726 self.i_type = item_type
727 self.i_size = item_size
728 self.i_flags = item_flags
729 self.i_data = item_data
732 def __init__(self, barray, pos=0):
733 """ create an object by parsing data from bytes array
734 returns : obj - if data is readable
735 raises ValueError if something is not ok.
737 self.i_type = struct.unpack('I', barray[pos:pos+4])[0] # int.from_bytes(barray[pos:pos+4])
738 self.i_size = struct.unpack('I', barray[pos+4:pos+8])[0] # int.from_bytes(barray[pos+4:pos+8])
739 self.i_flags = struct.unpack('Q', barray[pos+8:pos+16])[0] # int.from_bytes(barray[pos+8:pos+16])
740 self.i_data = barray[pos+16: (pos + 16 + self.i_size)]
744 return self.i_size + KCData_item.header_size
746 def GetHeaderDescription(self):
747 outs = "type: 0x
%x size
: 0x
%x flags
: 0x
%x (%s)" % (self.i_type, self.i_size, self.i_flags, GetTypeNameForKey(self.i_type))
748 if not self.i_offset is None:
749 outs = "pos
: 0x
%x" % self.i_offset + outs
753 return self.GetHeaderDescription()
755 def kcdata_item_iterator(data):
758 while curpos < file_len:
759 item = KCData_item(data, curpos)
763 def _get_data_element(elementValues):
764 return json.dumps(elementValues[-1])
766 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'))
768 KNOWN_TYPES_COLLECTION[GetTypeForName('KCDATA_TYPE_UINT32_DESC')] = KCTypeDescription(GetTypeForName('KCDATA_TYPE_UINT32_DESC'), (
769 KCSubTypeElement('desc', KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(32, 1), 0, 1),
770 KCSubTypeElement('data', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 32, 0)
772 'KCDATA_TYPE_UINT32_DESC',
776 KNOWN_TYPES_COLLECTION[GetTypeForName('KCDATA_TYPE_UINT64_DESC')] = KCTypeDescription(GetTypeForName('KCDATA_TYPE_UINT64_DESC'), (
777 KCSubTypeElement('desc', KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(32, 1), 0, 1),
778 KCSubTypeElement('data', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 32, 0)
780 'KCDATA_TYPE_UINT64_DESC',
784 KNOWN_TYPES_COLLECTION[GetTypeForName('KCDATA_TYPE_TIMEBASE')] = KCTypeDescription(GetTypeForName('KCDATA_TYPE_TIMEBASE'), (
785 KCSubTypeElement('numer', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 0, 0),
786 KCSubTypeElement('denom', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4, 0)
792 STACKSHOT_IO_NUM_PRIORITIES = 4
793 KNOWN_TYPES_COLLECTION[0x901] = KCTypeDescription(0x901, (
794 KCSubTypeElement.FromBasicCtype('ss_disk_reads_count', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
795 KCSubTypeElement.FromBasicCtype('ss_disk_reads_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
796 KCSubTypeElement.FromBasicCtype('ss_disk_writes_count', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
797 KCSubTypeElement.FromBasicCtype('ss_disk_writes_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
798 KCSubTypeElement('ss_io_priority_count', KCSUBTYPE_TYPE.KC_ST_UINT64, KCSubTypeElement.GetSizeForArray(STACKSHOT_IO_NUM_PRIORITIES, 8), 32, 1),
799 KCSubTypeElement('ss_io_priority_size', KCSUBTYPE_TYPE.KC_ST_UINT64, KCSubTypeElement.GetSizeForArray(STACKSHOT_IO_NUM_PRIORITIES, 8), 32 + (STACKSHOT_IO_NUM_PRIORITIES * 8), 1),
800 KCSubTypeElement.FromBasicCtype('ss_paging_count', KCSUBTYPE_TYPE.KC_ST_UINT64, 32 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8)),
801 KCSubTypeElement.FromBasicCtype('ss_paging_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 40 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8)),
802 KCSubTypeElement.FromBasicCtype('ss_non_paging_count', KCSUBTYPE_TYPE.KC_ST_UINT64, 48 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8)),
803 KCSubTypeElement.FromBasicCtype('ss_non_paging_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 56 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8)),
804 KCSubTypeElement.FromBasicCtype('ss_data_count', KCSUBTYPE_TYPE.KC_ST_UINT64, 64 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8)),
805 KCSubTypeElement.FromBasicCtype('ss_data_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 72 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8)),
806 KCSubTypeElement.FromBasicCtype('ss_metadata_count', KCSUBTYPE_TYPE.KC_ST_UINT64, 80 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8)),
807 KCSubTypeElement.FromBasicCtype('ss_metadata_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 88 + 2 * (STACKSHOT_IO_NUM_PRIORITIES * 8))
812 KNOWN_TYPES_COLLECTION[0x902] = KCTypeDescription(0x902, (
813 KCSubTypeElement('snapshot_magic', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 0, 0),
814 KCSubTypeElement('free_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 1, 0),
815 KCSubTypeElement('active_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 2, 0),
816 KCSubTypeElement('inactive_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 3, 0),
817 KCSubTypeElement('purgeable_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 4, 0),
818 KCSubTypeElement('wired_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 5, 0),
819 KCSubTypeElement('speculative_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 6, 0),
820 KCSubTypeElement('throttled_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 7, 0),
821 KCSubTypeElement('filebacked_pages', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 8, 0),
822 KCSubTypeElement('compressions', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 9, 0),
823 KCSubTypeElement('decompressions', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 10, 0),
824 KCSubTypeElement('compressor_size', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 11, 0),
825 KCSubTypeElement('busy_buffer_count', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 4 * 12, 0),
826 KCSubTypeElement('pages_wanted', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 13, 0),
827 KCSubTypeElement('pages_reclaimed', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 4 * 14, 0),
828 KCSubTypeElement('pages_wanted_reclaimed_valid', KCSUBTYPE_TYPE.KC_ST_UINT8, 1, 4 * 15, 0)
830 'mem_and_io_snapshot'
834 KNOWN_TYPES_COLLECTION[0x905] = KCTypeDescription(0x905, (
835 KCSubTypeElement.FromBasicCtype('ts_unique_pid', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
836 KCSubTypeElement.FromBasicCtype('ts_ss_flags', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
837 KCSubTypeElement.FromBasicCtype('ts_user_time_in_terminated_thre', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
838 KCSubTypeElement.FromBasicCtype('ts_system_time_in_terminated_th', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
839 KCSubTypeElement.FromBasicCtype('ts_p_start_sec', KCSUBTYPE_TYPE.KC_ST_UINT64, 32),
840 KCSubTypeElement.FromBasicCtype('ts_task_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 40),
841 KCSubTypeElement.FromBasicCtype('ts_max_resident_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 48),
842 KCSubTypeElement.FromBasicCtype('ts_suspend_count', KCSUBTYPE_TYPE.KC_ST_UINT32, 56),
843 KCSubTypeElement.FromBasicCtype('ts_faults', KCSUBTYPE_TYPE.KC_ST_UINT32, 60),
844 KCSubTypeElement.FromBasicCtype('ts_pageins', KCSUBTYPE_TYPE.KC_ST_UINT32, 64),
845 KCSubTypeElement.FromBasicCtype('ts_cow_faults', KCSUBTYPE_TYPE.KC_ST_UINT32, 68),
846 KCSubTypeElement.FromBasicCtype('ts_was_throttled', KCSUBTYPE_TYPE.KC_ST_UINT32, 72),
847 KCSubTypeElement.FromBasicCtype('ts_did_throttle', KCSUBTYPE_TYPE.KC_ST_UINT32, 76),
848 KCSubTypeElement.FromBasicCtype('ts_latency_qos', KCSUBTYPE_TYPE.KC_ST_UINT32, 80),
849 KCSubTypeElement.FromBasicCtype('ts_pid', KCSUBTYPE_TYPE.KC_ST_INT32, 84),
850 KCSubTypeElement('ts_p_comm', KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(32, 1), 88, 1)
855 KNOWN_TYPES_COLLECTION[0x906] = KCTypeDescription(0x906, (
856 KCSubTypeElement.FromBasicCtype('ths_thread_id', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
857 KCSubTypeElement.FromBasicCtype('ths_wait_event', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
858 KCSubTypeElement.FromBasicCtype('ths_continuation', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
859 KCSubTypeElement.FromBasicCtype('ths_total_syscalls', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
860 KCSubTypeElement.FromBasicCtype('ths_voucher_identifier', KCSUBTYPE_TYPE.KC_ST_UINT64, 32),
861 KCSubTypeElement.FromBasicCtype('ths_dqserialnum', KCSUBTYPE_TYPE.KC_ST_UINT64, 40),
862 KCSubTypeElement.FromBasicCtype('ths_user_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 48),
863 KCSubTypeElement.FromBasicCtype('ths_sys_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 56),
864 KCSubTypeElement.FromBasicCtype('ths_ss_flags', KCSUBTYPE_TYPE.KC_ST_UINT64, 64),
865 KCSubTypeElement.FromBasicCtype('ths_last_run_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 72),
866 KCSubTypeElement.FromBasicCtype('ths_last_made_runnable_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 80),
867 KCSubTypeElement.FromBasicCtype('ths_state', KCSUBTYPE_TYPE.KC_ST_UINT32, 88),
868 KCSubTypeElement.FromBasicCtype('ths_sched_flags', KCSUBTYPE_TYPE.KC_ST_UINT32, 92),
869 KCSubTypeElement.FromBasicCtype('ths_base_priority', KCSUBTYPE_TYPE.KC_ST_INT16, 96),
870 KCSubTypeElement.FromBasicCtype('ths_sched_priority', KCSUBTYPE_TYPE.KC_ST_INT16, 98),
871 KCSubTypeElement.FromBasicCtype('ths_eqos', KCSUBTYPE_TYPE.KC_ST_UINT8, 100),
872 KCSubTypeElement.FromBasicCtype('ths_rqos', KCSUBTYPE_TYPE.KC_ST_UINT8, 101),
873 KCSubTypeElement.FromBasicCtype('ths_rqos_override', KCSUBTYPE_TYPE.KC_ST_UINT8, 102),
874 KCSubTypeElement.FromBasicCtype('ths_io_tier', KCSUBTYPE_TYPE.KC_ST_UINT8, 103),
875 KCSubTypeElement.FromBasicCtype('ths_thread_t', KCSUBTYPE_TYPE.KC_ST_UINT64, 104),
876 KCSubTypeElement.FromBasicCtype('ths_requested_policy', KCSUBTYPE_TYPE.KC_ST_UINT64, 112),
877 KCSubTypeElement.FromBasicCtype('ths_effective_policy', KCSUBTYPE_TYPE.KC_ST_UINT64, 120),
883 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_THREAD_DISPATCH_QUEUE_LABEL')] = KCSubTypeElement('dispatch_queue_label', KCSUBTYPE_TYPE.KC_ST_CHAR,
884 KCSubTypeElement.GetSizeForArray(64, 1), 0, 1)
886 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_THREAD_DELTA_SNAPSHOT')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_THREAD_DELTA_SNAPSHOT'), (
887 KCSubTypeElement.FromBasicCtype('tds_thread_id', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
888 KCSubTypeElement.FromBasicCtype('tds_voucher_identifier', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
889 KCSubTypeElement.FromBasicCtype('tds_ss_flags', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
890 KCSubTypeElement.FromBasicCtype('tds_last_made_runnable_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
891 KCSubTypeElement.FromBasicCtype('tds_state', KCSUBTYPE_TYPE.KC_ST_UINT32, 32),
892 KCSubTypeElement.FromBasicCtype('tds_sched_flags', KCSUBTYPE_TYPE.KC_ST_UINT32, 36),
893 KCSubTypeElement.FromBasicCtype('tds_base_priority', KCSUBTYPE_TYPE.KC_ST_INT16, 40),
894 KCSubTypeElement.FromBasicCtype('tds_sched_priority', KCSUBTYPE_TYPE.KC_ST_INT16, 42),
895 KCSubTypeElement.FromBasicCtype('tds_eqos', KCSUBTYPE_TYPE.KC_ST_UINT8, 44),
896 KCSubTypeElement.FromBasicCtype('tds_rqos', KCSUBTYPE_TYPE.KC_ST_UINT8, 45),
897 KCSubTypeElement.FromBasicCtype('tds_rqos_override', KCSUBTYPE_TYPE.KC_ST_UINT8, 46),
898 KCSubTypeElement.FromBasicCtype('tds_io_tier', KCSUBTYPE_TYPE.KC_ST_UINT8, 47),
899 KCSubTypeElement.FromBasicCtype('tds_requested_policy', KCSUBTYPE_TYPE.KC_ST_UINT64, 48),
900 KCSubTypeElement.FromBasicCtype('tds_effective_policy', KCSUBTYPE_TYPE.KC_ST_UINT64, 56),
902 'thread_delta_snapshot',
906 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_TASK_DELTA_SNAPSHOT')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_TASK_DELTA_SNAPSHOT'), (
907 KCSubTypeElement.FromBasicCtype('tds_unique_pid', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
908 KCSubTypeElement.FromBasicCtype('tds_ss_flags', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
909 KCSubTypeElement.FromBasicCtype('tds_user_time_in_terminated_thr', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
910 KCSubTypeElement.FromBasicCtype('tds_system_time_in_terminated_t', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
911 KCSubTypeElement.FromBasicCtype('tds_task_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 32),
912 KCSubTypeElement.FromBasicCtype('tds_max_resident_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 40),
913 KCSubTypeElement.FromBasicCtype('tds_suspend_count', KCSUBTYPE_TYPE.KC_ST_UINT32, 48),
914 KCSubTypeElement.FromBasicCtype('tds_faults', KCSUBTYPE_TYPE.KC_ST_UINT32, 52),
915 KCSubTypeElement.FromBasicCtype('tds_pageins', KCSUBTYPE_TYPE.KC_ST_UINT32, 56),
916 KCSubTypeElement.FromBasicCtype('tds_cow_faults', KCSUBTYPE_TYPE.KC_ST_UINT32, 60),
917 KCSubTypeElement.FromBasicCtype('tds_was_throttled', KCSUBTYPE_TYPE.KC_ST_UINT32, 64),
918 KCSubTypeElement.FromBasicCtype('tds_did_throttle', KCSUBTYPE_TYPE.KC_ST_UINT32, 68),
919 KCSubTypeElement.FromBasicCtype('tds_latency_qos', KCSUBTYPE_TYPE.KC_ST_UINT32, 72),
921 'task_delta_snapshot'
925 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_THREAD_NAME')] = KCSubTypeElement('pth_name', KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(64, 1), 0, 1)
927 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_SYS_SHAREDCACHE_LAYOUT')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_SYS_SHAREDCACHE_LAYOUT'), (
928 KCSubTypeElement('imageLoadAddress', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0),
929 KCSubTypeElement('imageUUID', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 8, 1)
931 'system_shared_cache_layout'
934 KNOWN_TYPES_COLLECTION[GetTypeForName('KCDATA_TYPE_LIBRARY_LOADINFO64')] = KCTypeDescription(GetTypeForName('KCDATA_TYPE_LIBRARY_LOADINFO64'), (
935 KCSubTypeElement('imageLoadAddress', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0),
936 KCSubTypeElement('imageUUID', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 8, 1)
942 KNOWN_TYPES_COLLECTION[GetTypeForName('KCDATA_TYPE_LIBRARY_LOADINFO')] = KCTypeDescription(GetTypeForName('KCDATA_TYPE_LIBRARY_LOADINFO'), (
943 KCSubTypeElement('imageLoadAddress', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 0, 0),
944 KCSubTypeElement('imageUUID', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 4, 1)
950 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_LOADINFO64_TEXT_EXEC')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_LOADINFO64_TEXT_EXEC'), (
951 KCSubTypeElement('imageLoadAddress', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0),
952 KCSubTypeElement('imageUUID', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 8, 1),
954 'dyld_load_info_text_exec'
957 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_SHAREDCACHE_LOADINFO')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_SHAREDCACHE_LOADINFO'), (
958 KCSubTypeElement('imageLoadAddress', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0),
959 KCSubTypeElement('imageUUID', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 8, 1),
960 KCSubTypeElement('imageSlidBaseAddress', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 24, 0),
961 KCSubTypeElement('sharedCacheSlidFirstMapping', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 32, 0),
963 'shared_cache_dyld_load_info',
967 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERNELCACHE_LOADINFO')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_KERNELCACHE_LOADINFO'), (
968 KCSubTypeElement('imageLoadAddress', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0),
969 KCSubTypeElement('imageUUID', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 8, 1),
971 'kernelcache_load_info'
974 KNOWN_TYPES_COLLECTION[0x33] = KCSubTypeElement('mach_absolute_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0, KCSubTypeElement._get_naked_element_value)
975 KNOWN_TYPES_COLLECTION[0x907] = KCSubTypeElement.FromBasicCtype('donating_pids', KCSUBTYPE_TYPE.KC_ST_INT32, legacy_size=4)
977 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)
979 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKFRAME')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKFRAME'), (
980 KCSubTypeElement.FromBasicCtype('lr', KCSUBTYPE_TYPE.KC_ST_UINT32),
981 KCSubTypeElement.FromBasicCtype('sp', KCSUBTYPE_TYPE.KC_ST_UINT32, 4)
983 'kernel_stack_frames',
987 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKLR')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKLR'), (
988 KCSubTypeElement.FromBasicCtype('lr', KCSUBTYPE_TYPE.KC_ST_UINT32),
990 'kernel_stack_frames'
994 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_USER_STACKFRAME')] = KCTypeDescription.FromKCTypeDescription(
995 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKFRAME')],
996 GetTypeForName('STACKSHOT_KCTYPE_USER_STACKFRAME'),
1000 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_USER_STACKLR')] = KCTypeDescription.FromKCTypeDescription(
1001 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKLR')],
1002 GetTypeForName('STACKSHOT_KCTYPE_USER_STACKLR'),
1006 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKFRAME64')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKFRAME64'), (
1007 KCSubTypeElement.FromBasicCtype('lr', KCSUBTYPE_TYPE.KC_ST_UINT64),
1008 KCSubTypeElement.FromBasicCtype('sp', KCSUBTYPE_TYPE.KC_ST_UINT64, 8)
1010 'kernel_stack_frames',
1014 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_USER_STACKFRAME64')] = KCTypeDescription.FromKCTypeDescription(
1015 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKFRAME64')],
1016 GetTypeForName('STACKSHOT_KCTYPE_USER_STACKFRAME64'),
1021 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKLR64')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKLR64'), (
1022 KCSubTypeElement.FromBasicCtype('lr', KCSUBTYPE_TYPE.KC_ST_UINT64),
1024 'kernel_stack_frames'
1027 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_USER_STACKLR64')] = KCTypeDescription.FromKCTypeDescription(
1028 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_KERN_STACKLR64')],
1029 GetTypeForName('STACKSHOT_KCTYPE_USER_STACKLR64'),
1034 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_NONRUNNABLE_TIDS')] = KCSubTypeElement.FromBasicCtype('nonrunnable_threads', KCSUBTYPE_TYPE.KC_ST_INT64)
1036 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_NONRUNNABLE_TASKS')] = KCSubTypeElement.FromBasicCtype('nonrunnable_tasks', KCSUBTYPE_TYPE.KC_ST_INT64)
1038 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_OSVERSION')] = KCSubTypeElement('osversion', KCSUBTYPE_TYPE.KC_ST_CHAR,
1039 KCSubTypeElement.GetSizeForArray(256, 1), 0, 1)
1041 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_BOOTARGS')] = KCSubTypeElement('boot_args', KCSUBTYPE_TYPE.KC_ST_CHAR,
1042 KCSubTypeElement.GetSizeForArray(256, 1), 0, 1)
1044 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)
1046 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)
1048 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_JETSAM_LEVEL')] = KCSubTypeElement('jetsam_level', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 0, 0, KCSubTypeElement._get_naked_element_value)
1050 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)
1052 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_STACKSHOT_FAULT_STATS')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_STACKSHOT_FAULT_STATS'),
1054 KCSubTypeElement.FromBasicCtype('sfs_pages_faulted_in', KCSUBTYPE_TYPE.KC_ST_UINT32, 0),
1055 KCSubTypeElement.FromBasicCtype('sfs_time_spent_faulting', KCSUBTYPE_TYPE.KC_ST_UINT64, 4),
1056 KCSubTypeElement.FromBasicCtype('sfs_system_max_fault_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 12),
1057 KCSubTypeElement.FromBasicCtype('sfs_stopped_faulting', KCSUBTYPE_TYPE.KC_ST_UINT8, 20)
1059 'stackshot_fault_stats')
1061 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_THREAD_WAITINFO')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_THREAD_WAITINFO'),
1063 KCSubTypeElement.FromBasicCtype('owner', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1064 KCSubTypeElement.FromBasicCtype('waiter', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1065 KCSubTypeElement.FromBasicCtype('context', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
1066 KCSubTypeElement.FromBasicCtype('wait_type', KCSUBTYPE_TYPE.KC_ST_UINT8, 24)
1070 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_THREAD_TURNSTILEINFO')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_THREAD_TURNSTILEINFO'),
1072 KCSubTypeElement.FromBasicCtype('waiter', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1073 KCSubTypeElement.FromBasicCtype('turnstile_context', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1074 KCSubTypeElement.FromBasicCtype('turnstile_priority', KCSUBTYPE_TYPE.KC_ST_UINT8, 16),
1075 KCSubTypeElement.FromBasicCtype('number_of_hops', KCSUBTYPE_TYPE.KC_ST_UINT8, 17),
1076 KCSubTypeElement.FromBasicCtype('turnstile_flags', KCSUBTYPE_TYPE.KC_ST_UINT64, 18),
1078 'thread_turnstileinfo')
1080 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_THREAD_GROUP_SNAPSHOT')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_THREAD_GROUP'),
1082 KCSubTypeElement.FromBasicCtype('tgs_id', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1083 KCSubTypeElement('tgs_name', KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(16, 1),
1085 KCSubTypeElement.FromBasicCtype('tgs_flags', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
1087 'thread_group_snapshot')
1090 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_THREAD_GROUP')] = KCSubTypeElement('thread_group', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0, KCSubTypeElement._get_naked_element_value)
1092 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_JETSAM_COALITION_SNAPSHOT')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_JETSAM_COALITION_SNAPSHOT'),
1094 KCSubTypeElement.FromBasicCtype('jcs_id', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1095 KCSubTypeElement.FromBasicCtype('jcs_flags', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1096 KCSubTypeElement.FromBasicCtype('jcs_thread_group', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
1097 KCSubTypeElement.FromBasicCtype('jcs_leader_task_uniqueid', KCSUBTYPE_TYPE.KC_ST_UINT64, 24)
1099 'jetsam_coalition_snapshot')
1101 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_JETSAM_COALITION')] = KCSubTypeElement('jetsam_coalition', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0, KCSubTypeElement._get_naked_element_value)
1103 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_INSTRS_CYCLES')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_INSTRS_CYCLES'),
1105 KCSubTypeElement.FromBasicCtype('ics_instructions', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1106 KCSubTypeElement.FromBasicCtype('ics_cycles', KCSUBTYPE_TYPE.KC_ST_UINT64, 8)
1108 'instrs_cycles_snapshot')
1110 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_TASK_CPU_ARCHITECTURE')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_TASK_CPU_ARCHITECTURE'),
1112 KCSubTypeElement.FromBasicCtype('cputype', KCSUBTYPE_TYPE.KC_ST_INT32, 0),
1113 KCSubTypeElement.FromBasicCtype('cpusubtype', KCSUBTYPE_TYPE.KC_ST_INT32, 4)
1115 'task_cpu_architecture')
1117 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_LATENCY_INFO')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_LATENCY_INFO'),
1119 KCSubTypeElement.FromBasicCtype('latency_version', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1120 KCSubTypeElement.FromBasicCtype('setup_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1121 KCSubTypeElement.FromBasicCtype('total_task_iteration_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
1122 KCSubTypeElement.FromBasicCtype('total_terminated_task_iteration_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 24)
1124 'stackshot_latency_collection')
1126 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_LATENCY_INFO_TASK')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_LATENCY_INFO_TASK'),
1128 KCSubTypeElement.FromBasicCtype('task_uniqueid', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1129 KCSubTypeElement.FromBasicCtype('setup_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1130 KCSubTypeElement.FromBasicCtype('task_thread_count_loop_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
1131 KCSubTypeElement.FromBasicCtype('task_thread_data_loop_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
1132 KCSubTypeElement.FromBasicCtype('cur_tsnap_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 32),
1133 KCSubTypeElement.FromBasicCtype('pmap_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 40),
1134 KCSubTypeElement.FromBasicCtype('bsd_proc_ids_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 48),
1135 KCSubTypeElement.FromBasicCtype('misc_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 56),
1136 KCSubTypeElement.FromBasicCtype('misc2_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 64),
1137 KCSubTypeElement.FromBasicCtype('end_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 72)
1139 'stackshot_latency_task')
1141 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_LATENCY_INFO_THREAD')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_LATENCY_INFO_THREAD'),
1143 KCSubTypeElement.FromBasicCtype('thread_id', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1144 KCSubTypeElement.FromBasicCtype('cur_thsnap1_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1145 KCSubTypeElement.FromBasicCtype('dispatch_serial_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
1146 KCSubTypeElement.FromBasicCtype('dispatch_label_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
1147 KCSubTypeElement.FromBasicCtype('cur_thsnap2_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 32),
1148 KCSubTypeElement.FromBasicCtype('thread_name_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 40),
1149 KCSubTypeElement.FromBasicCtype('sur_times_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 48),
1150 KCSubTypeElement.FromBasicCtype('user_stack_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 56),
1151 KCSubTypeElement.FromBasicCtype('kernel_stack_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 64),
1152 KCSubTypeElement.FromBasicCtype('misc_latency', KCSUBTYPE_TYPE.KC_ST_UINT64, 72),
1154 'stackshot_latency_thread')
1156 def set_type(name, *args):
1157 typ = GetTypeForName(name)
1158 KNOWN_TYPES_COLLECTION[typ] = KCTypeDescription(GetTypeForName(typ), *args)
1161 set_type('STACKSHOT_KCTYPE_USER_STACKTOP',
1163 KCSubTypeElement.FromBasicCtype('sp', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1164 KCSubTypeElement('stack_contents', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(8, 1), 8, 1),
1168 #KNOWN_TYPES_COLLECTION[0x907] = KCSubTypeElement('donating_pids', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 0, 0, KCSubTypeElement._get_naked_element_value)
1169 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PID')] = KCSubTypeElement('pid', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1170 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PPID')] = KCSubTypeElement('ppid', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1171 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PROC_NAME')] = KCSubTypeElement('p_comm', KCSUBTYPE_TYPE.KC_ST_CHAR,
1172 KCSubTypeElement.GetSizeForArray(32, 1), 0, 1)
1173 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_USERSTACK')] = KCSubTypeElement('userstack_ptr', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0)
1174 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_ARGSLEN')] = KCSubTypeElement('p_argslen', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1176 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PROC_PATH')] = KCSubTypeElement('p_path', KCSUBTYPE_TYPE.KC_ST_CHAR,
1177 KCSubTypeElement.GetSizeForArray(1024, 1), 0, 1)
1178 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PROC_CSFLAGS')] = KCSubTypeElement('p_csflags', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1179 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_UID')] = KCSubTypeElement('uid', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1180 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_GID')] = KCSubTypeElement('gid', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1181 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PROC_ARGC')] = KCSubTypeElement('argc', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1182 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PROC_FLAGS')] = KCSubTypeElement('p_flags', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1183 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_CPUTYPE')] = KCSubTypeElement('cputype', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1184 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_RESPONSIBLE_PID')] = KCSubTypeElement('responsible_pid', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1185 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_DIRTY_FLAGS')] = KCSubTypeElement('dirty_flags', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0)
1186 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_CRASHED_THREADID')] = KCSubTypeElement('crashed_threadid', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0)
1187 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_COALITION_ID')] = KCSubTypeElement('coalition_id', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0)
1189 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PROC_STATUS')] = KCSubTypeElement('p_status', KCSUBTYPE_TYPE.KC_ST_UINT8, 1, 0, 0)
1191 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_BSDINFOWITHUNIQID')] = KCTypeDescription(GetTypeForName('TASK_CRASHINFO_BSDINFOWITHUNIQID'),
1192 ( KCSubTypeElement('p_uuid', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 0, 1),
1193 KCSubTypeElement.FromBasicCtype('p_uniqueid', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
1194 KCSubTypeElement.FromBasicCtype('p_puniqueid', KCSUBTYPE_TYPE.KC_ST_UINT64, 24)
1196 'proc_uniqidentifierinfo')
1198 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_EXCEPTION_CODES')] = (
1199 KCTypeDescription(GetTypeForName('TASK_CRASHINFO_EXCEPTION_CODES'),
1200 (KCSubTypeElement.FromBasicCtype('code_0', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1201 KCSubTypeElement.FromBasicCtype('code_1', KCSUBTYPE_TYPE.KC_ST_UINT64, 8)),
1202 'mach_exception_data_t'))
1205 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_PROC_STARTTIME')] = (
1206 KCTypeDescription(GetTypeForName('TASK_CRASHINFO_PROC_STARTTIME'),
1207 (KCSubTypeElement.FromBasicCtype('tv_sec', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1208 KCSubTypeElement.FromBasicCtype('tv_usec', KCSUBTYPE_TYPE.KC_ST_UINT64, 8)),
1212 KNOWN_TYPES_COLLECTION[GetTypeForName('TASK_CRASHINFO_RUSAGE_INFO')] = KCTypeDescription(GetTypeForName('TASK_CRASHINFO_RUSAGE_INFO'),
1214 KCSubTypeElement('ri_uuid', KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(16, 1), 0, 1),
1215 KCSubTypeElement.FromBasicCtype('ri_user_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
1216 KCSubTypeElement.FromBasicCtype('ri_system_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 24),
1217 KCSubTypeElement.FromBasicCtype('ri_pkg_idle_wkups', KCSUBTYPE_TYPE.KC_ST_UINT64, 32),
1218 KCSubTypeElement.FromBasicCtype('ri_interrupt_wkups', KCSUBTYPE_TYPE.KC_ST_UINT64, 40),
1219 KCSubTypeElement.FromBasicCtype('ri_pageins', KCSUBTYPE_TYPE.KC_ST_UINT64, 48),
1220 KCSubTypeElement.FromBasicCtype('ri_wired_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 56),
1221 KCSubTypeElement.FromBasicCtype('ri_resident_size', KCSUBTYPE_TYPE.KC_ST_UINT64, 64),
1222 KCSubTypeElement.FromBasicCtype('ri_phys_footprint', KCSUBTYPE_TYPE.KC_ST_UINT64, 72),
1223 KCSubTypeElement.FromBasicCtype('ri_proc_start_abstime', KCSUBTYPE_TYPE.KC_ST_UINT64, 80),
1224 KCSubTypeElement.FromBasicCtype('ri_proc_exit_abstime', KCSUBTYPE_TYPE.KC_ST_UINT64, 88),
1225 KCSubTypeElement.FromBasicCtype('ri_child_user_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 96),
1226 KCSubTypeElement.FromBasicCtype('ri_child_system_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 104),
1227 KCSubTypeElement.FromBasicCtype('ri_child_pkg_idle_wkups', KCSUBTYPE_TYPE.KC_ST_UINT64, 112),
1228 KCSubTypeElement.FromBasicCtype('ri_child_interrupt_wkups', KCSUBTYPE_TYPE.KC_ST_UINT64, 120),
1229 KCSubTypeElement.FromBasicCtype('ri_child_pageins', KCSUBTYPE_TYPE.KC_ST_UINT64, 128),
1230 KCSubTypeElement.FromBasicCtype('ri_child_elapsed_abstime', KCSUBTYPE_TYPE.KC_ST_UINT64, 136),
1231 KCSubTypeElement.FromBasicCtype('ri_diskio_bytesread', KCSUBTYPE_TYPE.KC_ST_UINT64, 144),
1232 KCSubTypeElement.FromBasicCtype('ri_diskio_byteswritten', KCSUBTYPE_TYPE.KC_ST_UINT64, 152),
1233 KCSubTypeElement.FromBasicCtype('ri_cpu_time_qos_default', KCSUBTYPE_TYPE.KC_ST_UINT64, 160),
1234 KCSubTypeElement.FromBasicCtype('ri_cpu_time_qos_maintenance', KCSUBTYPE_TYPE.KC_ST_UINT64, 168),
1235 KCSubTypeElement.FromBasicCtype('ri_cpu_time_qos_background', KCSUBTYPE_TYPE.KC_ST_UINT64, 176),
1236 KCSubTypeElement.FromBasicCtype('ri_cpu_time_qos_utility', KCSUBTYPE_TYPE.KC_ST_UINT64, 184),
1237 KCSubTypeElement.FromBasicCtype('ri_cpu_time_qos_legacy', KCSUBTYPE_TYPE.KC_ST_UINT64, 192),
1238 KCSubTypeElement.FromBasicCtype('ri_cpu_time_qos_user_initiated', KCSUBTYPE_TYPE.KC_ST_UINT64, 200),
1239 KCSubTypeElement.FromBasicCtype('ri_cpu_time_qos_user_interactiv', KCSUBTYPE_TYPE.KC_ST_UINT64, 208),
1240 KCSubTypeElement.FromBasicCtype('ri_billed_system_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 216),
1241 KCSubTypeElement.FromBasicCtype('ri_serviced_system_time', KCSUBTYPE_TYPE.KC_ST_UINT64, 224)
1245 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_CPU_TIMES')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_CPU_TIMES'),
1247 KCSubTypeElement.FromBasicCtype('user_usec', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1248 KCSubTypeElement.FromBasicCtype('system_usec', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1249 KCSubTypeElement.FromBasicCtype('runnable_usec', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
1252 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_STACKSHOT_DURATION')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_STACKSHOT_DURATION'),
1254 KCSubTypeElement.FromBasicCtype('stackshot_duration', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1255 KCSubTypeElement.FromBasicCtype('stackshot_duration_outer', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1256 KCSubTypeElement.FromBasicCtype('stackshot_duration_prior', KCSUBTYPE_TYPE.KC_ST_UINT64, 16),
1257 ), 'stackshot_duration', merge=True
1260 KNOWN_TYPES_COLLECTION[GetTypeForName('KCDATA_TYPE_PROCNAME')] = (
1261 KCSubTypeElement("proc_name
", KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(-1, 1), 0, 1))
1263 KNOWN_TYPES_COLLECTION[GetTypeForName('KCDATA_TYPE_PID')] = (
1264 KCSubTypeElement('pid', KCSUBTYPE_TYPE.KC_ST_INT32, 4, 0, 0))
1266 KNOWN_TYPES_COLLECTION[GetTypeForName('EXIT_REASON_SNAPSHOT')] = KCTypeDescription(GetTypeForName('EXIT_REASON_SNAPSHOT'),
1268 KCSubTypeElement.FromBasicCtype('ers_namespace', KCSUBTYPE_TYPE.KC_ST_UINT32, 0),
1269 KCSubTypeElement.FromBasicCtype('ers_code', KCSUBTYPE_TYPE.KC_ST_UINT64, 4),
1270 KCSubTypeElement.FromBasicCtype('ers_flags', KCSUBTYPE_TYPE.KC_ST_UINT64, 12),
1271 ), 'exit_reason_basic_info')
1273 KNOWN_TYPES_COLLECTION[GetTypeForName('EXIT_REASON_USER_DESC')] = (
1274 KCSubTypeElement("exit_reason_user_description
", KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(-1, 1), 0, 1))
1276 KNOWN_TYPES_COLLECTION[GetTypeForName('EXIT_REASON_USER_PAYLOAD')] = KCSubTypeElement('exit_reason_user_payload',
1277 KCSUBTYPE_TYPE.KC_ST_UINT8, KCSubTypeElement.GetSizeForArray(-1, 1), 0, 1)
1279 KNOWN_TYPES_COLLECTION[GetTypeForName('EXIT_REASON_CODESIGNING_INFO')] = KCTypeDescription(GetTypeForName('EXIT_REASON_CODESIGNING_INFO'),
1281 KCSubTypeElement.FromBasicCtype('ceri_virt_addr', KCSUBTYPE_TYPE.KC_ST_UINT64, 0),
1282 KCSubTypeElement.FromBasicCtype('ceri_file_offset', KCSUBTYPE_TYPE.KC_ST_UINT64, 8),
1283 KCSubTypeElement("ceri_pathname
", KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(1024, 1), 16, 1),
1284 KCSubTypeElement("ceri_filename
", KCSUBTYPE_TYPE.KC_ST_CHAR, KCSubTypeElement.GetSizeForArray(1024, 1), 1040, 1),
1285 KCSubTypeElement.FromBasicCtype('ceri_codesig_modtime_secs', KCSUBTYPE_TYPE.KC_ST_UINT64, 2064),
1286 KCSubTypeElement.FromBasicCtype('ceri_codesig_modtime_nsecs', KCSUBTYPE_TYPE.KC_ST_UINT64, 2072),
1287 KCSubTypeElement.FromBasicCtype('ceri_page_modtime_secs', KCSUBTYPE_TYPE.KC_ST_UINT64, 2080),
1288 KCSubTypeElement.FromBasicCtype('ceri_page_modtime_nsecs', KCSUBTYPE_TYPE.KC_ST_UINT64, 2088),
1289 KCSubTypeElement.FromBasicCtype('ceri_path_truncated', KCSUBTYPE_TYPE.KC_ST_UINT8, 2096),
1290 KCSubTypeElement.FromBasicCtype('ceri_object_codesigned', KCSUBTYPE_TYPE.KC_ST_UINT8, 2097),
1291 KCSubTypeElement.FromBasicCtype('ceri_page_codesig_validated', KCSUBTYPE_TYPE.KC_ST_UINT8, 2098),
1292 KCSubTypeElement.FromBasicCtype('ceri_page_codesig_tainted', KCSUBTYPE_TYPE.KC_ST_UINT8, 2099),
1293 KCSubTypeElement.FromBasicCtype('ceri_page_codesig_nx', KCSUBTYPE_TYPE.KC_ST_UINT8, 2100),
1294 KCSubTypeElement.FromBasicCtype('ceri_page_wpmapped', KCSUBTYPE_TYPE.KC_ST_UINT8, 2101),
1295 KCSubTypeElement.FromBasicCtype('ceri_page_slid', KCSUBTYPE_TYPE.KC_ST_UINT8, 2102),
1296 KCSubTypeElement.FromBasicCtype('ceri_page_dirty', KCSUBTYPE_TYPE.KC_ST_UINT8, 2103),
1297 KCSubTypeElement.FromBasicCtype('ceri_page_shadow_depth', KCSUBTYPE_TYPE.KC_ST_UINT32, 2104),
1298 ), 'exit_reason_codesigning_info')
1300 KNOWN_TYPES_COLLECTION[GetTypeForName('EXIT_REASON_WORKLOOP_ID')] = (
1301 KCSubTypeElement('exit_reason_workloop_id', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0, KCSubTypeElement._get_naked_element_value))
1303 KNOWN_TYPES_COLLECTION[GetTypeForName('EXIT_REASON_DISPATCH_QUEUE_NO')] = (
1304 KCSubTypeElement('exit_reason_dispatch_queue_no', KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0, KCSubTypeElement._get_naked_element_value))
1306 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_ASID')] = (
1307 KCSubTypeElement('ts_asid', KCSUBTYPE_TYPE.KC_ST_UINT32, 4, 0, 0))
1309 KNOWN_TYPES_COLLECTION[GetTypeForName('STACKSHOT_KCTYPE_PAGE_TABLES')] = KCTypeDescription(GetTypeForName('STACKSHOT_KCTYPE_PAGE_TABLES'), (
1310 KCSubTypeElement(None, KCSUBTYPE_TYPE.KC_ST_UINT64, 8, 0, 0, KCSubTypeElement._get_naked_element_value), ),
1316 def GetSecondsFromMATime(mat, tb):
1317 return (float(long(mat) * tb['numer']) / tb['denom']) / 1e9
1319 def FindLibraryForAddress(liblist, address):
1326 def FindIndexOfLibInCatalog(catalog, lib):
1330 if l[0] == lib[0] and l[1] == lib[1]:
1337 index = len(catalog) - 1
1341 def GetOffsetOfAddressForLib(lib, address):
1342 return (address - lib[1])
1344 def GetSymbolInfoForFrame(catalog, liblist, address):
1345 lib = FindLibraryForAddress(liblist, address)
1347 lib = ["00000000000000000000000000000000",0,"A
"]
1348 offset = GetOffsetOfAddressForLib(lib, address)
1349 index = FindIndexOfLibInCatalog(catalog, lib)
1350 return [index, offset]
1352 def GetStateDescription(s):
1359 TH_TERMINATE2 = 0x20
1360 TH_WAIT_REPORT = 0x40
1363 retval.append("TH_WAIT
")
1365 retval.append("TH_SUSP
")
1367 retval.append("TH_RUN
")
1369 retval.append("TH_UNINT
")
1370 if (s & TH_TERMINATE):
1371 retval.append("TH_TERMINATE
")
1372 if (s & TH_TERMINATE2):
1373 retval.append("TH_TERMINATE2
")
1374 if (s & TH_WAIT_REPORT):
1375 retval.append("TH_WAIT_REPORT
")
1377 retval.append("TH_IDLE
")
1381 def format_uuid(elementValues):
1382 return ''.join("%02x
" % i for i in elementValues)
1384 kThreadWaitNone = 0x00
1385 kThreadWaitKernelMutex = 0x01
1386 kThreadWaitPortReceive = 0x02
1387 kThreadWaitPortSetReceive = 0x03
1388 kThreadWaitPortSend = 0x04
1389 kThreadWaitPortSendInTransit = 0x05
1390 kThreadWaitSemaphore = 0x06
1391 kThreadWaitKernelRWLockRead = 0x07
1392 kThreadWaitKernelRWLockWrite = 0x08
1393 kThreadWaitKernelRWLockUpgrade = 0x09
1394 kThreadWaitUserLock = 0x0a
1395 kThreadWaitPThreadMutex = 0x0b
1396 kThreadWaitPThreadRWLockRead = 0x0c
1397 kThreadWaitPThreadRWLockWrite = 0x0d
1398 kThreadWaitPThreadCondVar = 0x0e
1399 kThreadWaitParkedWorkQueue = 0x0f
1400 kThreadWaitWorkloopSyncWait = 0x10
1401 kThreadWaitOnProcess = 0x11
1402 kThreadWaitSleepWithInheritor = 0x12
1403 kThreadWaitEventlink = 0x13
1404 kThreadWaitCompressor = 0x14
1407 UINT64_MAX = 0xffffffffffffffff
1408 STACKSHOT_WAITOWNER_KERNEL = (UINT64_MAX - 1)
1409 STACKSHOT_WAITOWNER_PORT_LOCKED = (UINT64_MAX - 2)
1410 STACKSHOT_WAITOWNER_PSET_LOCKED = (UINT64_MAX - 3)
1411 STACKSHOT_WAITOWNER_INTRANSIT = (UINT64_MAX - 4)
1412 STACKSHOT_WAITOWNER_MTXSPIN = (UINT64_MAX - 5)
1413 STACKSHOT_WAITOWNER_THREQUESTED = (UINT64_MAX - 6)
1414 STACKSHOT_WAITOWNER_SUSPENDED = (UINT64_MAX - 7)
1416 STACKSHOT_TURNSTILE_STATUS_UNKNOWN = 0x01
1417 STACKSHOT_TURNSTILE_STATUS_LOCKED_WAITQ = 0x02
1418 STACKSHOT_TURNSTILE_STATUS_WORKQUEUE = 0x04
1419 STACKSHOT_TURNSTILE_STATUS_THREAD = 0x08
1420 STACKSHOT_TURNSTILE_STATUS_BLOCKED_ON_TASK = 0x10
1421 STACKSHOT_TURNSTILE_STATUS_HELD_IPLOCK = 0x20
1423 def formatWaitInfo(info):
1424 s = 'thread %d: ' % info['waiter'];
1425 type = info['wait_type']
1426 context = info['context']
1427 owner = info['owner']
1428 if type == kThreadWaitKernelMutex:
1429 s += 'kernel mutex %x' % context
1430 if owner == STACKSHOT_WAITOWNER_MTXSPIN:
1431 s += " in spin mode
"
1433 s += " owned by thread
%u" % owner
1435 s += "with unknown owner
"
1436 elif type == kThreadWaitPortReceive:
1437 s += "mach_msg receive on
"
1438 if owner == STACKSHOT_WAITOWNER_PORT_LOCKED:
1439 s += "locked port
%x" % context
1440 elif owner == STACKSHOT_WAITOWNER_INTRANSIT:
1441 s += "intransit port
%x" % context
1443 s += "port
%x name
%x" % (context, owner)
1445 s += "port
%x" % context
1446 elif type == kThreadWaitPortSetReceive:
1447 if owner == STACKSHOT_WAITOWNER_PSET_LOCKED:
1448 s += "mach_msg receive on locked port
set %x" % context
1450 s += "mach_msg receive on port
set %x" % context
1451 elif type == kThreadWaitPortSend:
1452 s += "mach_msg send on
"
1453 if owner == STACKSHOT_WAITOWNER_PORT_LOCKED:
1454 s += "locked port
%x" % context
1455 elif owner == STACKSHOT_WAITOWNER_INTRANSIT:
1456 s += "intransit port
%x" % context
1457 elif owner == STACKSHOT_WAITOWNER_KERNEL:
1458 s += "port
%x owned by kernel
" % context
1460 s += "port
%x owned by pid
%d" % (context, owner)
1462 s += "port
%x with unknown owner
" % context
1463 elif type == kThreadWaitPortSendInTransit:
1464 s += "mach_msg send on port
%x in transit to
" % context
1466 s += "port
%x" % owner
1469 elif type == kThreadWaitSemaphore:
1470 s += "semaphore port
%x " % context
1472 s += "owned by pid
%d" % owner
1474 s += "with unknown owner
"
1475 elif type == kThreadWaitKernelRWLockRead:
1476 s += "krwlock
%x for reading
" % context
1477 elif type == kThreadWaitKernelRWLockWrite:
1478 s += "krwlock
%x for writing
" % context
1479 elif type == kThreadWaitKernelRWLockUpgrade:
1480 s += "krwlock
%x for upgrading
" % context
1481 elif type == kThreadWaitUserLock:
1483 s += "unfair lock
%x owned by thread
%d" % (context, owner)
1485 s += "spin lock
%x" % context
1486 elif type == kThreadWaitPThreadMutex:
1487 s += "pthread mutex
%x" % context
1489 s += " owned by thread
%d" % owner
1491 s += " with unknown owner
"
1492 elif type == kThreadWaitPThreadRWLockRead:
1493 s += "pthread rwlock
%x for reading
" % context
1494 elif type == kThreadWaitPThreadRWLockWrite:
1495 s += "pthread rwlock
%x for writing
" % context
1496 elif type == kThreadWaitPThreadCondVar:
1497 s += "pthread condvar
%x" % context
1498 elif type == kThreadWaitWorkloopSyncWait:
1499 s += "workloop sync wait
"
1500 if owner == STACKSHOT_WAITOWNER_SUSPENDED:
1502 elif owner == STACKSHOT_WAITOWNER_THREQUESTED:
1503 s += ", thread requested
"
1505 s += ", owned by thread
%u" % owner
1507 s += ", unknown owner
"
1508 s += ", workloop
id %x" % context
1509 elif type == kThreadWaitOnProcess:
1510 if owner == 2**64-1:
1511 s += "waitpid
, for any children
"
1512 elif 2**32 <= owner and owner < 2**64-1:
1513 s += "waitpid
, for process group
%d" % abs(owner - 2**64)
1515 s += "waitpid
, for pid
%d" % owner
1516 elif type == kThreadWaitSleepWithInheritor:
1518 s += "turnstile
, held waitq
"
1520 s += "turnstile
, pushing thread
%d" % owner
1521 elif type == kThreadWaitEventlink:
1523 s += "eventlink
, held waitq
"
1525 s += "eventlink
, signaled by thread
%d" % owner
1526 elif type == kThreadWaitCompressor:
1527 s += "in compressor segment
%x, busy
for thread
%d" % (context, owner)
1530 s += "unknown
type %d (owner
%d, context
%x)" % (type, owner, context)
1534 def formatTurnstileInfo(ti):
1536 return " [no turnstile
]"
1538 ts_flags = int(ti['turnstile_flags'])
1539 ctx = int(ti['turnstile_context'])
1540 hop = int(ti['number_of_hops'])
1541 prio = int(ti['turnstile_priority'])
1542 if ts_flags & STACKSHOT_TURNSTILE_STATUS_HELD_IPLOCK:
1543 return " [turnstile blocked on task
, but ip_lock was held
]"
1544 if ts_flags & STACKSHOT_TURNSTILE_STATUS_BLOCKED_ON_TASK:
1545 return " [turnstile blocked on task pid
%d, hops
: %d, priority
: %d]" % (ctx, hop, prio)
1546 if ts_flags & STACKSHOT_TURNSTILE_STATUS_LOCKED_WAITQ:
1547 return " [turnstile was
in process of being updated
]"
1548 if ts_flags & STACKSHOT_TURNSTILE_STATUS_WORKQUEUE:
1549 return " [blocked on workqueue
: 0x
%x, hops
: %x, priority
: %d]" % (ctx, hop, prio)
1550 if ts_flags & STACKSHOT_TURNSTILE_STATUS_THREAD:
1551 return " [blocked on
: %d, hops
: %x, priority
: %d]" % (ctx, hop, prio)
1552 if ts_flags & STACKSHOT_TURNSTILE_STATUS_UNKNOWN:
1553 return " [turnstile
with unknown inheritor
]"
1555 return " [unknown turnstile status
!]"
1557 def formatWaitInfoWithTurnstiles(waitinfos, tsinfos):
1562 if int(w['waiter']) == int(t['waiter']):
1563 wis_tis.append((w, t))
1567 wis_tis.append((w, None))
1569 return map(lambda (wi, ti): formatWaitInfo(wi) + formatTurnstileInfo(ti), wis_tis)
1571 def SaveStackshotReport(j, outfile_name, incomplete):
1573 from operator import itemgetter, attrgetter
1574 ss = j.get('kcdata_stackshot')
1576 print "No KCDATA_BUFFER_BEGIN_STACKSHOT
object found
. Skipping writing report
."
1579 timestamp = ss.get('usecs_since_epoch')
1581 timestamp = time.strftime("%Y
-%m
-%d %H
:%M
:%S
+0000",time.gmtime(timestamp / 1000000 if timestamp else None))
1582 except ValueError, e:
1583 print "couldn
't convert timestamp:", str(e)
1586 os_version = ss.get('osversion
', 'Unknown
')
1587 timebase = ss.get('mach_timebase_info
', {"denom": 1, "numer": 1})
1590 shared_cache_info = ss.get('shared_cache_dyld_load_info
')
1591 if shared_cache_info:
1592 shared_cache_base_addr = shared_cache_info['imageSlidBaseAddress
']
1593 dsc_common = [format_uuid(shared_cache_info['imageUUID
']), shared_cache_info['imageSlidBaseAddress
'], "S" ]
1594 print "Shared cache UUID found from the binary data is <%s> " % str(dsc_common[0])
1596 dsc_layout = ss.get('system_shared_cache_layout
')
1600 print "Found in memory system shared cache layout with {} images".format(len(dsc_layout))
1601 slide = ss.get('shared_cache_dyld_load_info
')['imageLoadAddress
']
1603 for image in dsc_layout:
1604 dsc_libs.append([format_uuid(image['imageUUID
']), image['imageLoadAddress
'] + slide, "C"])
1606 AllImageCatalog = []
1608 obj["kernel"] = os_version
1609 if timestamp is not None:
1610 obj["date"] = timestamp
1611 obj["reason"] = "kernel panic stackshot"
1612 obj["incident"] = "ABCDEFGH-1234-56IJ-789K-0LMNOPQRSTUV"
1613 obj["crashReporterKey"] = "12ab34cd45aabbccdd6712ab34cd45aabbccdd67"
1614 obj["bootArgs"] = ss.get('boot_args
','')
1615 obj["frontmostPids"] = [0]
1616 obj["exception"] = "0xDEADF157"
1617 obj["processByPid"] = {}
1620 obj["reason"] = "!!!INCOMPLETE!!! kernel panic stackshot"
1621 obj["notes"] = "This stackshot report generated from incomplete data! Some information is missing! "
1623 processByPid = obj["processByPid"]
1624 ssplist = ss.get('task_snapshots
', {})
1627 kc_uuid = ssplist["0"].get('kernelcache_load_info
', None)
1629 kernelcache_uuid = [format_uuid(kc_uuid['imageUUID
']), kc_uuid['imageLoadAddress
'], "U" ]
1630 kern_load_info.append(kernelcache_uuid)
1632 kl_infos = ssplist["0"].get("dyld_load_info", [])
1633 for dlinfo in kl_infos:
1634 kern_load_info.append([format_uuid(dlinfo['imageUUID
']), dlinfo['imageLoadAddress
'], "K"])
1636 kl_infos_text_exec = ssplist["0"].get("dyld_load_info_text_exec", [])
1637 for dlinfo in kl_infos_text_exec:
1638 kern_load_info.append([format_uuid(dlinfo['imageUUID
']), dlinfo['imageLoadAddress
'], "T"])
1640 for pid,piddata in ssplist.iteritems():
1641 processByPid[str(pid)] = {}
1642 tsnap = processByPid[str(pid)]
1643 pr_lib_dsc = dsc_common
1644 if 'shared_cache_dyld_load_info
' in tsnap:
1645 if 'imageSlidBaseAddress
' in tsnap.get('shared_cache_dyld_load_info
'):
1646 shared_cache_base_addr = tsnap.get('shared_cache_dyld_load_info
')['imageSlidBaseAddress
']
1648 print "Specific task shared cache format does not include slid shared cache base address. Skipping writing report."
1651 pr_lib_dsc = [format_uuid(tsnap.get('shared_cache_dyld_load_info
')['imageUUID
']),
1652 tsnap.get('shared_cache_dyld_load_info
')['imageSlidBaseAddress
'],
1656 if len(dsc_libs) == 0 and pr_lib_dsc:
1657 pr_libs.append(pr_lib_dsc)
1663 for dlinfo in piddata.get('dyld_load_info
',[]):
1664 pr_libs.append([format_uuid(dlinfo['imageUUID
']), dlinfo['imageLoadAddress
'], _lib_type])
1666 pr_libs.extend(kern_load_info)
1667 pr_libs.extend(dsc_libs)
1669 pr_libs.sort(key=itemgetter(1))
1671 if 'task_snapshot
' not in piddata:
1673 tasksnap = piddata['task_snapshot
']
1674 tsnap["pid"] = tasksnap["ts_pid"]
1675 if 'ts_asid
' in piddata:
1676 tsnap["asid"] = piddata["ts_asid"]
1678 if 'ts_pagetable
' in piddata:
1680 for tte in piddata["ts_pagetable"]:
1681 pagetables.append(tte)
1682 tsnap["pageTables"] = pagetables
1684 tsnap["residentMemoryBytes"] = tasksnap["ts_task_size"]
1685 tsnap["timesDidThrottle"] = tasksnap["ts_did_throttle"]
1686 tsnap["systemTimeTask"] = GetSecondsFromMATime(tasksnap["ts_system_time_in_terminated_th"], timebase)
1687 tsnap["pageIns"] = tasksnap["ts_pageins"]
1688 tsnap["pageFaults"] = tasksnap["ts_faults"]
1689 tsnap["userTimeTask"] = GetSecondsFromMATime(tasksnap[ "ts_user_time_in_terminated_thre"], timebase)
1690 tsnap["procname"] = tasksnap["ts_p_comm"]
1691 tsnap["copyOnWriteFaults"] = tasksnap["ts_cow_faults"]
1692 tsnap["timesThrottled"] = tasksnap["ts_was_throttled"]
1693 tsnap["threadById"] = {}
1694 threadByID = tsnap["threadById"]
1695 thlist = piddata.get('thread_snapshots
', {})
1696 for tid,thdata in thlist.iteritems():
1697 threadByID[str(tid)] = {}
1698 thsnap = threadByID[str(tid)]
1699 if "thread_snapshot" not in thdata:
1700 print "Found broken thread state for thread ID: %s." % tid
1702 threadsnap = thdata["thread_snapshot"]
1703 thsnap["userTime"] = GetSecondsFromMATime(threadsnap["ths_user_time"], timebase)
1704 thsnap["id"] = threadsnap["ths_thread_id"]
1705 thsnap["basePriority"] = threadsnap["ths_base_priority"]
1706 thsnap["systemTime"] = threadsnap["ths_sys_time"]
1707 thsnap["schedPriority"] = threadsnap["ths_sched_priority"]
1708 thsnap["state"] = GetStateDescription(threadsnap['ths_state
'])
1709 thsnap["qosEffective"] = threadsnap["ths_eqos"]
1710 thsnap["qosRequested"] = threadsnap["ths_rqos"]
1712 if "pth_name" in thdata:
1713 thsnap["name"] = thdata["pth_name"];
1715 if threadsnap['ths_continuation
']:
1716 thsnap["continuation"] = GetSymbolInfoForFrame(AllImageCatalog, pr_libs, threadsnap['ths_continuation
'])
1717 if "kernel_stack_frames" in thdata:
1719 for f in thdata["kernel_stack_frames"]:
1720 kuserframes.append(GetSymbolInfoForFrame(AllImageCatalog, pr_libs, f['lr
']))
1721 thsnap["kernelFrames"] = kuserframes
1723 if "user_stack_frames" in thdata:
1725 for f in thdata["user_stack_frames"]:
1726 uframes.append(GetSymbolInfoForFrame(AllImageCatalog, pr_libs, f['lr
']))
1727 thsnap["userFrames"] = uframes
1729 if "user_stacktop" in thdata:
1730 (address,) = struct.unpack("<Q", struct.pack("B"*8, *thdata["user_stacktop"]["stack_contents"]))
1731 thsnap["userStacktop"] = GetSymbolInfoForFrame(AllImageCatalog, pr_libs, address)
1733 if threadsnap['ths_wait_event
']:
1734 thsnap["waitEvent"] = GetSymbolInfoForFrame(AllImageCatalog, pr_libs, threadsnap['ths_wait_event
'])
1736 if 'thread_waitinfo
' in piddata and 'thread_turnstileinfo
' in piddata:
1737 tsnap['waitInfo
'] = formatWaitInfoWithTurnstiles(piddata['thread_waitinfo
'] , piddata['thread_turnstileinfo
'])
1738 elif 'thread_waitinfo
' in piddata:
1739 tsnap['waitInfo
'] = map(formatWaitInfo, piddata['thread_waitinfo
'])
1741 obj['binaryImages
'] = AllImageCatalog
1742 if outfile_name == '-':
1745 fh = open(outfile_name, "w")
1748 header['bug_type
'] = 288
1749 if timestamp is not None:
1750 header['timestamp
'] = timestamp
1751 header['os_version
'] = os_version
1752 fh.write(json.dumps(header))
1755 fh.write(json.dumps(obj, sort_keys=False, indent=2, separators=(',', ': ')))
1758 ## Base utils for interacting with shell ##
1759 def RunCommand(bash_cmd_string, get_stderr = True):
1761 returns: (int,str) : exit_code and output_str
1763 print "RUNNING: %s" % bash_cmd_string
1764 cmd_args = shlex.split(bash_cmd_string)
1769 output_str = subprocess.check_output(cmd_args, stderr=subprocess.STDOUT)
1771 output_str = subprocess.check_output(cmd_args, stderr=None)
1772 except subprocess.CalledProcessError, e:
1773 exit_code = e.returncode
1775 return (exit_code, output_str)
1778 @contextlib.contextmanager
1779 def data_from_stream(stream):
1781 fmap = mmap.mmap(stream.fileno(), 0, mmap.MAP_SHARED, mmap.PROT_READ)
1790 def iterate_kcdatas(kcdata_file):
1791 with data_from_stream(kcdata_file) as data:
1792 iterator = kcdata_item_iterator(data)
1793 kcdata_buffer = KCObject.FromKCItem(iterator.next())
1795 if isinstance(kcdata_buffer, KCCompressedBufferObject):
1796 kcdata_buffer.ReadItems(iterator)
1797 decompressed = kcdata_buffer.Decompress(data)
1798 iterator = kcdata_item_iterator(decompressed)
1799 kcdata_buffer = KCObject.FromKCItem(iterator.next())
1801 if not isinstance(kcdata_buffer, KCBufferObject):
1802 # ktrace stackshot chunk
1803 iterator = kcdata_item_iterator(data[16:])
1804 kcdata_buffer = KCObject.FromKCItem(iterator.next())
1806 if not isinstance(kcdata_buffer, KCBufferObject):
1808 decoded = base64.b64decode(data)
1812 iterator = kcdata_item_iterator(decoded)
1813 kcdata_buffer = KCObject.FromKCItem(iterator.next())
1814 if not isinstance(kcdata_buffer, KCBufferObject):
1816 from io import BytesIO
1818 decompressed = gzip.GzipFile(fileobj=BytesIO(data[:])).read()
1822 iterator = kcdata_item_iterator(decompressed)
1823 kcdata_buffer = KCObject.FromKCItem(iterator.next())
1825 if not isinstance(kcdata_buffer, KCBufferObject):
1826 raise Exception, "unknown file type"
1829 kcdata_buffer.ReadItems(iterator)
1832 for magic in iterator:
1833 kcdata_buffer = KCObject.FromKCItem(magic)
1834 if kcdata_buffer.i_type == 0:
1836 if not isinstance(kcdata_buffer, KCBufferObject):
1837 raise Exception, "unknown file type"
1838 kcdata_buffer.ReadItems(iterator)
1843 if isinstance(data, list):
1844 return map(prettify, data);
1846 elif isinstance(data, dict):
1848 for key, value in data.items():
1849 if 'uuid
' in key.lower() and isinstance(value, list) and len(value) == 16:
1850 value = '%02X
%02X
%02X
%02X
-%02X
%02X
-%02X
%02X
-%02X
%02X
-%02X
%02X
%02X
%02X
%02X
%02X
' % tuple(value)
1851 elif 'address
' in key.lower() and isinstance(value, (int, long)):
1852 value = '0x
%X' % value
1853 elif key == 'lr
' or key == 'sharedCacheSlidFirstMapping
':
1854 value = '0x
%X' % value
1855 elif key == 'thread_waitinfo
':
1856 value = map(formatWaitInfo, value)
1857 elif key == 'stack_contents
':
1858 (address,) = struct.unpack("<Q", struct.pack("B"*8, *value))
1859 value = '0x
%X' % address
1861 value = prettify(value);
1862 newdata[key] = value
1870 if __name__ == '__main__
':
1871 parser = argparse.ArgumentParser(description="Decode a kcdata binary file.")
1872 parser.add_argument("-l", "--listtypes", action="store_true", required=False, default=False,
1873 help="List all known types",
1874 dest="list_known_types")
1876 parser.add_argument("-s", "--stackshot", required=False, default=False,
1877 help="Generate a stackshot report file",
1878 dest="stackshot_file")
1880 parser.add_argument("--multiple", help="look for multiple stackshots in a single file", action='store_true
')
1882 parser.add_argument("-p", "--plist", required=False, default=False,
1883 help="output as plist", action="store_true")
1885 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")
1886 parser.add_argument("--pretty", default=False, action='store_true
', help="make the output a little more human readable")
1887 parser.add_argument("--incomplete", action='store_true
', help="accept incomplete data")
1888 parser.add_argument("kcdata_file", type=argparse.FileType('r
'), help="Path to a kcdata binary file.")
1890 class VerboseAction(argparse.Action):
1891 def __call__(self, parser, namespace, values, option_string=None):
1892 logging.basicConfig(level=logging.INFO, stream=sys.stderr, format='%(message)s')
1893 parser.add_argument('-v
', "--verbose", action=VerboseAction, nargs=0)
1895 args = parser.parse_args()
1897 if args.multiple and args.stackshot_file:
1898 raise NotImplementedError
1900 if args.list_known_types:
1901 for (n, t) in KNOWN_TYPES_COLLECTION.items():
1902 print "%d : %s " % (n, str(t))
1905 if args.incomplete or args.stackshot_file:
1906 G.accept_incomplete_data = True
1908 for i,kcdata_buffer in enumerate(iterate_kcdatas(args.kcdata_file)):
1909 if i > 0 and not args.multiple:
1912 str_data = "{" + kcdata_buffer.GetJsonRepr() + "}"
1913 str_data = str_data.replace("\t", " ")
1916 json_obj = json.loads(str_data)
1918 print >>sys.stderr, "JSON reparsing failed! Printing string data!\n"
1920 print textwrap.fill(str_data, 100)
1924 json_obj = prettify(json_obj)
1926 if args.stackshot_file:
1927 SaveStackshotReport(json_obj, args.stackshot_file, G.data_was_incomplete)
1930 plist = Foundation.NSPropertyListSerialization.dataWithPropertyList_format_options_error_(
1931 json_obj, Foundation.NSPropertyListXMLFormat_v1_0, 0, None)[0].bytes().tobytes()
1932 #sigh. on some pythons long integers are getting output with L's
in the plist
.
1933 plist
= re
.sub(r
'^(\s*<integer>\d+)L(</integer>\s*)$', r
"\1\2", plist
, flags
=re
.MULTILINE
)
1936 print json
.dumps(json_obj
, sort_keys
=True, indent
=4, separators
=(',', ': '))