]> git.saurik.com Git - apple/xnu.git/blame - tools/lldbmacros/core/kernelcore.py
xnu-4903.221.2.tar.gz
[apple/xnu.git] / tools / lldbmacros / core / kernelcore.py
CommitLineData
39236c6e
A
1
2""" Please make sure you read the README COMPLETELY BEFORE reading anything below.
3 It is very critical that you read coding guidelines in Section E in README file.
4"""
5
6from cvalue import *
7from lazytarget import *
8from configuration import *
d9a64523 9from utils import *
39236c6e
A
10import caching
11import lldb
12
13def IterateTAILQ_HEAD(headval, element_name):
14 """ iterate over a TAILQ_HEAD in kernel. refer to bsd/sys/queue.h
15 params:
16 headval - value : value object representing the head of the list
17 element_name- str : string name of the field which holds the list links.
18 returns:
19 A generator does not return. It is used for iterating.
20 value : an object that is of type as headval->tqh_first. Always a pointer object
21 example usage:
22 list_head = kern.GetGlobalVariable('mountlist')
23 for entryobj in IterateTAILQ_HEAD(list_head, 'mnt_list'):
24 print GetEntrySummary(entryobj)
25 """
26 iter_val = headval.tqh_first
27 while unsigned(iter_val) != 0 :
28 yield iter_val
29 iter_val = iter_val.__getattr__(element_name).tqe_next
30 #end of yield loop
31
32def IterateLinkedList(element, field_name):
33 """ iterate over a linked list.
34 This is equivalent to elt = element; while(elt) { do_work(elt); elt = elt-><field_name>; }
35 params:
36 element - value : value object representing element in the list.
37 field_name - str : name of field that holds pointer to next element
38 returns: Nothing. This is used as iterable
39 example usage:
40 first_zone = kern.GetGlobalVariable('first_zone')
41 for zone in IterateLinkedList(first_zone, 'next_zone'):
42 print GetZoneSummary(zone)
43 """
44 elt = element
45 while unsigned(elt) != 0:
46 yield elt
47 elt = elt.__getattr__(field_name)
48 #end of while loop
49
5ba3f43e 50def IterateListEntry(element, element_type, field_name, list_prefix=''):
39236c6e
A
51 """ iterate over a list as defined with LIST_HEAD in bsd/sys/queue.h
52 params:
53 element - value : Value object for lh_first
54 element_type - str : Type of the next element
55 field_name - str : Name of the field in next element's structure
cc8bc92a 56 list_prefix - str : use 's' here to iterate SLIST_HEAD instead
39236c6e
A
57 returns:
58 A generator does not return. It is used for iterating
59 value : an object thats of type (element_type) head->le_next. Always a pointer object
60 example usage:
61 headp = kern.globals.initproc.p_children
62 for pp in IterateListEntry(headp, 'struct proc *', 'p_sibling'):
63 print GetProcInfo(pp)
64 """
5ba3f43e 65 elt = element.__getattr__(list_prefix + 'lh_first')
39236c6e
A
66 if type(element_type) == str:
67 element_type = gettype(element_type)
68 while unsigned(elt) != 0:
69 yield elt
5ba3f43e 70 next_el = elt.__getattr__(field_name).__getattr__(list_prefix + 'le_next')
39236c6e
A
71 elt = cast(next_el, element_type)
72
3e170ce0
A
73def IterateLinkageChain(queue_head, element_type, field_name, field_ofst=0):
74 """ Iterate over a Linkage Chain queue in kernel of type queue_head_t. (osfmk/kern/queue.h method 1)
75 This is equivalent to the qe_foreach_element() macro
76 params:
77 queue_head - value : Value object for queue_head.
78 element_type - lldb.SBType : pointer type of the element which contains the queue_chain_t. Typically its structs like thread, task etc..
79 - str : OR a string describing the type. ex. 'task *'
80 field_name - str : Name of the field (in element) which holds a queue_chain_t
81 field_ofst - int : offset from the 'field_name' (in element) which holds a queue_chain_t
82 This is mostly useful if a particular element contains an array of queue_chain_t
83 returns:
84 A generator does not return. It is used for iterating.
85 value : An object thats of type (element_type). Always a pointer object
86 example usage:
87 coalq = kern.GetGlobalVariable('coalitions_q')
88 for coal in IterateLinkageChain(coalq, 'struct coalition *', 'coalitions'):
89 print GetCoalitionInfo(coal)
90 """
91 global kern
92 if type(element_type) == str:
93 element_type = gettype(element_type)
94
95 if unsigned(queue_head) == 0:
96 return
97
98 if element_type.IsPointerType():
99 elem_ofst = getfieldoffset(element_type.GetPointeeType(), field_name) + field_ofst
100 else:
101 elem_ofst = getfieldoffset(element_type, field_name) + field_ofst
102
103 link = queue_head.next
104 while (unsigned(link) != unsigned(queue_head)):
105 addr = unsigned(link) - elem_ofst;
106 # I can't use the GetValueFromAddress function of the kernel class
107 # because I have no instance of that class!
108 obj = value(link.GetSBValue().CreateValueFromExpression(None,'(void *)'+str(addr)))
109 obj = cast(obj, element_type)
110 yield obj
111 link = link.next
112
113
39037602 114def IterateQueue(queue_head, element_ptr_type, element_field_name, backwards=False, unpack_ptr_fn=None):
3e170ce0 115 """ Iterate over an Element Chain queue in kernel of type queue_head_t. (osfmk/kern/queue.h method 2)
39236c6e
A
116 params:
117 queue_head - value : Value object for queue_head.
3e170ce0 118 element_ptr_type - lldb.SBType : a pointer type of the element 'next' points to. Typically its structs like thread, task etc..
39236c6e
A
119 - str : OR a string describing the type. ex. 'task *'
120 element_field_name - str : name of the field in target struct.
3e170ce0 121 backwards - backwards : traverse the queue backwards
39037602 122 unpack_ptr_fn - function : a function ptr of signature def unpack_ptr(long v) which returns long.
39236c6e
A
123 returns:
124 A generator does not return. It is used for iterating.
125 value : an object thats of type (element_type) queue_head->next. Always a pointer object
fe8ab488
A
126 example usage:
127 for page_meta in IterateQueue(kern.globals.first_zone.pages.all_free, 'struct zone_page_metadata *', 'pages'):
128 print page_meta
39236c6e
A
129 """
130 if type(element_ptr_type) == str :
131 element_ptr_type = gettype(element_ptr_type)
132
133 queue_head = queue_head.GetSBValue()
134 queue_head_addr = 0x0
135 if queue_head.TypeIsPointerType():
136 queue_head_addr = queue_head.GetValueAsUnsigned()
137 else:
138 queue_head_addr = queue_head.GetAddress().GetLoadAddress(LazyTarget.GetTarget())
39037602
A
139
140 def unpack_ptr_and_recast(v):
141 if unpack_ptr_fn is None:
142 return v
143 v_unpacked = unpack_ptr_fn(v.GetValueAsUnsigned())
144 obj = v.CreateValueFromExpression(None,'(void *)'+str(v_unpacked))
145 obj.Cast(element_ptr_type)
146 return obj
147
3e170ce0 148 if backwards:
39037602 149 cur_elt = unpack_ptr_and_recast(queue_head.GetChildMemberWithName('prev'))
3e170ce0 150 else:
39037602 151 cur_elt = unpack_ptr_and_recast(queue_head.GetChildMemberWithName('next'))
3e170ce0 152
39236c6e
A
153 while True:
154
155 if not cur_elt.IsValid() or cur_elt.GetValueAsUnsigned() == 0 or cur_elt.GetValueAsUnsigned() == queue_head_addr:
156 break
157 elt = cur_elt.Cast(element_ptr_type)
158 yield value(elt)
3e170ce0 159 if backwards:
39037602 160 cur_elt = unpack_ptr_and_recast(elt.GetChildMemberWithName(element_field_name).GetChildMemberWithName('prev'))
3e170ce0 161 else:
39037602
A
162 cur_elt = unpack_ptr_and_recast(elt.GetChildMemberWithName(element_field_name).GetChildMemberWithName('next'))
163
39236c6e 164
cc8bc92a
A
165def IterateRBTreeEntry(element, element_type, field_name):
166 """ iterate over a rbtree as defined with RB_HEAD in libkern/tree.h
167 element - value : Value object for rbh_root
168 element_type - str : Type of the link element
169 field_name - str : Name of the field in link element's structure
170 returns:
171 A generator does not return. It is used for iterating
172 value : an object thats of type (element_type) head->sle_next. Always a pointer object
173 """
174 elt = element.__getattr__('rbh_root')
175 if type(element_type) == str:
176 element_type = gettype(element_type)
177
178 # Walk to find min
179 parent = elt
180 while unsigned(elt) != 0:
181 parent = elt
182 elt = cast(elt.__getattr__(field_name).__getattr__('rbe_left'), element_type)
183 elt = parent
184
185 # Now elt is min
186 while unsigned(elt) != 0:
187 yield elt
188 # implementation cribbed from RB_NEXT in libkern/tree.h
189 right = cast(elt.__getattr__(field_name).__getattr__('rbe_right'), element_type)
190 if unsigned(right) != 0:
191 elt = right
192 left = cast(elt.__getattr__(field_name).__getattr__('rbe_left'), element_type)
193 while unsigned(left) != 0:
194 elt = left
195 left = cast(elt.__getattr__(field_name).__getattr__('rbe_left'), element_type)
196 else:
197
198 # avoid using GetValueFromAddress
199 addr = elt.__getattr__(field_name).__getattr__('rbe_parent')&~1
200 parent = value(elt.GetSBValue().CreateValueFromExpression(None,'(void *)'+str(addr)))
201 parent = cast(parent, element_type)
202
203 if unsigned(parent) != 0:
204 left = cast(parent.__getattr__(field_name).__getattr__('rbe_left'), element_type)
205 if (unsigned(parent) != 0) and (unsigned(elt) == unsigned(left)):
206 elt = parent
207 else:
208 if unsigned(parent) != 0:
209 right = cast(parent.__getattr__(field_name).__getattr__('rbe_right'), element_type)
210 while unsigned(parent) != 0 and (unsigned(elt) == unsigned(right)):
211 elt = parent
212
213 # avoid using GetValueFromAddress
214 addr = elt.__getattr__(field_name).__getattr__('rbe_parent')&~1
215 parent = value(elt.GetSBValue().CreateValueFromExpression(None,'(void *)'+str(addr)))
216 parent = cast(parent, element_type)
217
218 right = cast(parent.__getattr__(field_name).__getattr__('rbe_right'), element_type)
219
220 # avoid using GetValueFromAddress
221 addr = elt.__getattr__(field_name).__getattr__('rbe_parent')&~1
222 elt = value(elt.GetSBValue().CreateValueFromExpression(None,'(void *)'+str(addr)))
223 elt = cast(elt, element_type)
224
225
d9a64523
A
226def IteratePriorityQueueEntry(root, element_type, field_name):
227 """ iterate over a priority queue as defined with struct priority_queue from osfmk/kern/priority_queue.h
228 root - value : Value object for the priority queue
229 element_type - str : Type of the link element
230 field_name - str : Name of the field in link element's structure
231 returns:
232 A generator does not return. It is used for iterating
233 value : an object thats of type (element_type). Always a pointer object
234 """
235 def _make_pqe(addr):
236 return value(root.GetSBValue().CreateValueFromExpression(None,'(struct priority_queue_entry *)'+str(addr)))
237
238 queue = [unsigned(root.pq_root_packed) & ~3]
239
240 while len(queue):
241 elt = _make_pqe(queue.pop())
242
243 while elt:
244 yield containerof(elt, element_type, field_name)
245 addr = unsigned(elt.child)
246 if addr: queue.append(addr)
247 elt = elt.next
248
249
39236c6e
A
250class KernelTarget(object):
251 """ A common kernel object that provides access to kernel objects and information.
252 The class holds global lists for task, terminated_tasks, procs, zones, zombroc etc.
253 It also provides a way to symbolicate an address or create a value from an address.
254 """
255 def __init__(self, debugger):
256 """ Initialize the kernel debugging environment.
257 Target properties like architecture and connectedness are lazy-evaluted.
258 """
259 self._debugger = debugger # This holds an lldb.SBDebugger object for debugger state
260 self._threads_list = []
261 self._tasks_list = []
fe8ab488 262 self._coalitions_list = []
5ba3f43e 263 self._thread_groups = []
39236c6e
A
264 self._allproc = []
265 self._terminated_tasks_list = []
266 self._zones_list = []
267 self._zombproc_list = []
268 self._kernel_types_cache = {} #this will cache the Type objects as and when requested.
269 self._version = None
270 self._arch = None
271 self._ptrsize = None # pointer size of kernel, not userspace
272 self.symbolicator = None
273 class _GlobalVariableFind(object):
274 def __init__(self, kern):
275 self._xnu_kernobj_12obscure12 = kern
276 def __getattr__(self, name):
277 v = self._xnu_kernobj_12obscure12.GetGlobalVariable(name)
278 if not v.GetSBValue().IsValid():
3e170ce0 279 raise ValueError('No such global variable by name: %s '%str(name))
39236c6e
A
280 return v
281 self.globals = _GlobalVariableFind(self)
282 LazyTarget.Initialize(debugger)
283
284 def _GetSymbolicator(self):
285 """ Internal function: To initialize the symbolication from lldb.utils
286 """
287 if not self.symbolicator is None:
288 return self.symbolicator
289
290 from lldb.utils import symbolication
291 symbolicator = symbolication.Symbolicator()
292 symbolicator.target = LazyTarget.GetTarget()
293 self.symbolicator = symbolicator
294 return self.symbolicator
295
296 def Symbolicate(self, addr):
297 """ simple method to get name of function/variable from an address. this is equivalent of gdb 'output /a 0xaddress'
298 params:
299 addr - int : typically hex value like 0xffffff80002c0df0
300 returns:
301 str - '' if no symbol found else the symbol name.
302 Note: this function only finds the first symbol. If you expect multiple symbol conflict please use SymbolicateFromAddress()
303 """
304 ret_str = ''
305 syms = self.SymbolicateFromAddress(addr)
306 if len(syms) > 0:
307 ret_str +=syms[0].GetName()
308 return ret_str
309
310 def SymbolicateFromAddress(self, addr):
311 """ symbolicates any given address based on modules loaded in the target.
312 params:
313 addr - int : typically hex value like 0xffffff80002c0df0
314 returns:
315 [] of SBSymbol: In case we don't find anything than empty array is returned.
316 Note: a type of symbol can be figured out by gettype() function of SBSymbol.
317 example usage:
318 syms = kern.Symbolicate(0xffffff80002c0df0)
319 for s in syms:
320 if s.GetType() == lldb.eSymbolTypeCode:
321 print "Function", s.GetName()
322 if s.GetType() == lldb.eSymbolTypeData:
323 print "Variable", s.GetName()
324 """
325 if type(int(1)) != type(addr):
326 if str(addr).strip().find("0x") == 0 :
327 addr = int(addr, 16)
328 else:
329 addr = int(addr)
330 ret_array = []
331 symbolicator = self._GetSymbolicator()
332 syms = symbolicator.symbolicate(addr)
333 if not syms:
334 return ret_array
335 for s in syms:
336 ret_array.append(s.get_symbol_context().symbol)
337 return ret_array
338
339 def IsDebuggerConnected(self):
340 proc_state = LazyTarget.GetProcess().state
341 if proc_state == lldb.eStateInvalid : return False
342 if proc_state in [lldb.eStateStopped, lldb.eStateSuspended] : return True
343
344 def GetGlobalVariable(self, name):
345 """ Get the value object representation for a kernel global variable
346 params:
347 name : str - name of the variable. ex. version
348 returns: value - python object representing global variable.
349 raises : Exception in case the variable is not found.
350 """
3e170ce0
A
351 self._globals_cache_dict = caching.GetDynamicCacheData("kern._globals_cache_dict", {})
352 if name not in self._globals_cache_dict:
353 self._globals_cache_dict[name] = value(LazyTarget.GetTarget().FindGlobalVariables(name, 1).GetValueAtIndex(0))
354 return self._globals_cache_dict[name]
39236c6e
A
355
356 def GetLoadAddressForSymbol(self, name):
357 """ Get the load address of a symbol in the kernel.
358 params:
359 name : str - name of the symbol to lookup
360 returns: int - the load address as an integer. Use GetValueFromAddress to cast to a value.
361 raises : LookupError - if the symbol is not found.
362 """
363 name = str(name)
364 target = LazyTarget.GetTarget()
365 syms_arr = target.FindSymbols(name)
366 if syms_arr.IsValid() and len(syms_arr) > 0:
367 symbol = syms_arr[0].GetSymbol()
368 if symbol.IsValid():
369 return int(symbol.GetStartAddress().GetLoadAddress(target))
370
371 raise LookupError("Symbol not found: " + name)
372
373 def GetValueFromAddress(self, addr, type_str = 'void *'):
374 """ convert a address to value
375 params:
376 addr - int : typically hex value like 0xffffff80008dc390
377 type_str - str: type to cast to. Default type will be void *
378 returns:
379 value : a value object which has address as addr and type is type_str
380 """
381 obj = value(self.globals.version.GetSBValue().CreateValueFromExpression(None,'(void *)'+str(addr)))
382 obj = cast(obj, type_str)
383 return obj
384
385 def GetValueAsType(self, v, t):
386 """ Retrieves a global variable 'v' of type 't' wrapped in a vue object.
387 If 'v' is an address, creates a vue object of the appropriate type.
388 If 'v' is a name, looks for the global variable and asserts its type.
389 Throws:
390 NameError - If 'v' cannot be found
391 TypeError - If 'v' is of the wrong type
392 """
393 if islong(v):
394 return self.GetValueFromAddress(v, t)
395 else:
396 var = LazyTarget.GetTarget().FindGlobalVariables(v, 1)[0]
397 if not var:
398 raise NameError("Failed to find global variable '{0}'".format(v))
399 if var.GetTypeName() != t:
400 raise TypeError("{0} must be of type '{1}', not '{2}'".format(v, t, var.GetTypeName()))
401 return value(var)
402
403 def _GetIterator(self, iter_head_name, next_element_name='next', iter_head_type=None):
404 """ returns an iterator for a collection in kernel memory.
405 params:
406 iter_head_name - str : name of queue_head or list head variable.
407 next_element_name - str : name of the element that leads to next element.
408 for ex. in struct zone list 'next_zone' is the linking element.
409 returns:
410 iterable : typically used in conjunction with "for varname in iterable:"
411 """
412 head_element = self.GetGlobalVariable(iter_head_name)
413 return head_element.GetSBValue().linked_list_iter(next_element_name)
414
415 def TruncPage(self, addr):
416 return (addr & ~(unsigned(self.GetGlobalVariable("page_size")) - 1))
417
418 def RoundPage(self, addr):
419 return trunc_page(addr + unsigned(self.GetGlobalVariable("page_size")) - 1)
420
421 def StraddlesPage(self, addr, size):
422 if size > unsigned(self.GetGlobalVariable("page_size")):
423 return True
fe8ab488
A
424 val = ((addr + size) & (unsigned(self.GetGlobalVariable("page_size"))-1))
425 return (val < size and val > 0)
39236c6e 426
d9a64523
A
427
428 def PhysToKVARM64(self, addr):
429 ptov_table = self.GetGlobalVariable('ptov_table')
430 for i in range(0, self.GetGlobalVariable('ptov_index')):
431 if (addr >= long(unsigned(ptov_table[i].pa))) and (addr < (long(unsigned(ptov_table[i].pa)) + long(unsigned(ptov_table[i].len)))):
432 return (addr - long(unsigned(ptov_table[i].pa)) + long(unsigned(ptov_table[i].va)))
433 return (addr - unsigned(self.GetGlobalVariable("gPhysBase")) + unsigned(self.GetGlobalVariable("gVirtBase")))
434
39236c6e
A
435 def PhysToKernelVirt(self, addr):
436 if self.arch == 'x86_64':
437 return (addr + unsigned(self.GetGlobalVariable('physmap_base')))
d9a64523
A
438 elif self.arch.startswith('arm64'):
439 return self.PhysToKVARM64(addr)
5ba3f43e 440 elif self.arch.startswith('arm'):
39236c6e
A
441 return (addr - unsigned(self.GetGlobalVariable("gPhysBase")) + unsigned(self.GetGlobalVariable("gVirtBase")))
442 else:
5ba3f43e 443 raise ValueError("PhysToVirt does not support {0}".format(self.arch))
39236c6e 444
3e170ce0
A
445 def GetNanotimeFromAbstime(self, abstime):
446 """ convert absolute time (which is in MATUs) to nano seconds.
447 Since based on architecture the conversion may differ.
448 params:
449 abstime - int absolute time as shown by mach_absolute_time
450 returns:
451 int - nanosecs of time
452 """
453 usec_divisor = caching.GetStaticCacheData("kern.rtc_usec_divisor", None)
454 if not usec_divisor:
455 if self.arch == 'x86_64':
456 usec_divisor = 1000
457 else:
458 rtclockdata_addr = self.GetLoadAddressForSymbol('RTClockData')
459 rtc = self.GetValueFromAddress(rtclockdata_addr, 'struct _rtclock_data_ *')
460 usec_divisor = unsigned(rtc.rtc_usec_divisor)
461 usec_divisor = int(usec_divisor)
462 caching.SaveStaticCacheData('kern.rtc_usec_divisor', usec_divisor)
463 nsecs = (abstime * 1000)/usec_divisor
464 return nsecs
465
39236c6e
A
466 def __getattribute__(self, name):
467 if name == 'zones' :
468 self._zones_list = caching.GetDynamicCacheData("kern._zones_list", [])
469 if len(self._zones_list) > 0: return self._zones_list
39037602
A
470 zone_array = self.GetGlobalVariable('zone_array')
471 for i in range(0, self.GetGlobalVariable('num_zones')):
472 self._zones_list.append(addressof(zone_array[i]))
39236c6e
A
473 caching.SaveDynamicCacheData("kern._zones_list", self._zones_list)
474 return self._zones_list
475
476 if name == 'threads' :
477 self._threads_list = caching.GetDynamicCacheData("kern._threads_list", [])
478 if len(self._threads_list) > 0 : return self._threads_list
479 thread_queue_head = self.GetGlobalVariable('threads')
480 thread_type = LazyTarget.GetTarget().FindFirstType('thread')
481 thread_ptr_type = thread_type.GetPointerType()
482 for th in IterateQueue(thread_queue_head, thread_ptr_type, 'threads'):
483 self._threads_list.append(th)
484 caching.SaveDynamicCacheData("kern._threads_list", self._threads_list)
485 return self._threads_list
486
487 if name == 'tasks' :
488 self._tasks_list = caching.GetDynamicCacheData("kern._tasks_list", [])
489 if len(self._tasks_list) > 0 : return self._tasks_list
490 task_queue_head = self.GetGlobalVariable('tasks')
491 task_type = LazyTarget.GetTarget().FindFirstType('task')
492 task_ptr_type = task_type.GetPointerType()
493 for tsk in IterateQueue(task_queue_head, task_ptr_type, 'tasks'):
494 self._tasks_list.append(tsk)
495 caching.SaveDynamicCacheData("kern._tasks_list", self._tasks_list)
496 return self._tasks_list
497
fe8ab488
A
498 if name == 'coalitions' :
499 self._coalitions_list = caching.GetDynamicCacheData("kern._coalitions_list", [])
500 if len(self._coalitions_list) > 0 : return self._coalitions_list
3e170ce0 501 coalition_queue_head = self.GetGlobalVariable('coalitions_q')
fe8ab488
A
502 coalition_type = LazyTarget.GetTarget().FindFirstType('coalition')
503 coalition_ptr_type = coalition_type.GetPointerType()
3e170ce0
A
504 for coal in IterateLinkageChain(addressof(coalition_queue_head), coalition_ptr_type, 'coalitions'):
505 self._coalitions_list.append(coal)
fe8ab488
A
506 caching.SaveDynamicCacheData("kern._coalitions_list", self._coalitions_list)
507 return self._coalitions_list
508
5ba3f43e
A
509 if name == 'thread_groups' :
510 self._thread_groups_list = caching.GetDynamicCacheData("kern._thread_groups_list", [])
511 if len(self._thread_groups_list) > 0 : return self._thread_groups_list
512 thread_groups_queue_head = self.GetGlobalVariable('tg_queue')
513 thread_group_type = LazyTarget.GetTarget().FindFirstType('thread_group')
514 thread_groups_ptr_type = thread_group_type.GetPointerType()
515 for coal in IterateLinkageChain(addressof(thread_groups_queue_head), thread_groups_ptr_type, 'tg_queue_chain'):
516 self._thread_groups_list.append(coal)
517 caching.SaveDynamicCacheData("kern._thread_groups_list", self._thread_groups_list)
518 return self._thread_groups_list
519
39236c6e
A
520 if name == 'terminated_tasks' :
521 self._terminated_tasks_list = caching.GetDynamicCacheData("kern._terminated_tasks_list", [])
522 if len(self._terminated_tasks_list) > 0 : return self._terminated_tasks_list
523 task_queue_head = self.GetGlobalVariable('terminated_tasks')
524 task_type = LazyTarget.GetTarget().FindFirstType('task')
525 task_ptr_type = task_type.GetPointerType()
526 for tsk in IterateQueue(task_queue_head, task_ptr_type, 'tasks'):
527 self._terminated_tasks_list.append(tsk)
528 caching.SaveDynamicCacheData("kern._terminated_tasks_list", self._terminated_tasks_list)
529 return self._terminated_tasks_list
530
531 if name == 'procs' :
532 self._allproc = caching.GetDynamicCacheData("kern._allproc", [])
533 if len(self._allproc) > 0 : return self._allproc
534 all_proc_head = self.GetGlobalVariable('allproc')
535 proc_val = cast(all_proc_head.lh_first, 'proc *')
536 while proc_val != 0:
537 self._allproc.append(proc_val)
538 proc_val = cast(proc_val.p_list.le_next, 'proc *')
539 caching.SaveDynamicCacheData("kern._allproc", self._allproc)
540 return self._allproc
541
fe8ab488
A
542 if name == 'interrupt_stats' :
543 self._interrupt_stats_list = caching.GetDynamicCacheData("kern._interrupt_stats_list", [])
544 if len(self._interrupt_stats_list) > 0 : return self._interrupt_stats_list
545 interrupt_stats_head = self.GetGlobalVariable('gInterruptAccountingDataList')
546 interrupt_stats_type = LazyTarget.GetTarget().FindFirstType('IOInterruptAccountingData')
547 interrupt_stats_ptr_type = interrupt_stats_type.GetPointerType()
548 for interrupt_stats_obj in IterateQueue(interrupt_stats_head, interrupt_stats_ptr_type, 'chain'):
549 self._interrupt_stats_list.append(interrupt_stats_obj)
550 caching.SaveDynamicCacheData("kern._interrupt_stats", self._interrupt_stats_list)
551 return self._interrupt_stats_list
552
39236c6e
A
553 if name == 'zombprocs' :
554 self._zombproc_list = caching.GetDynamicCacheData("kern._zombproc_list", [])
555 if len(self._zombproc_list) > 0 : return self._zombproc_list
556 zproc_head = self.GetGlobalVariable('zombproc')
557 proc_val = cast(zproc_head.lh_first, 'proc *')
558 while proc_val != 0:
559 self._zombproc_list.append(proc_val)
560 proc_val = cast(proc_val.p_list.le_next, 'proc *')
561 caching.SaveDynamicCacheData("kern._zombproc_list", self._zombproc_list)
562 return self._zombproc_list
563
564 if name == 'version' :
565 self._version = caching.GetStaticCacheData("kern.version", None)
566 if self._version != None : return self._version
567 self._version = str(self.GetGlobalVariable('version'))
568 caching.SaveStaticCacheData("kern.version", self._version)
569 return self._version
570
571 if name == 'arch' :
572 self._arch = caching.GetStaticCacheData("kern.arch", None)
573 if self._arch != None : return self._arch
574 arch = LazyTarget.GetTarget().triple.split('-')[0]
fe8ab488 575 if arch in ('armv7', 'armv7s', 'armv7k'):
39236c6e
A
576 self._arch = 'arm'
577 else:
578 self._arch = arch
579 caching.SaveStaticCacheData("kern.arch", self._arch)
580 return self._arch
581
582 if name == 'ptrsize' :
583 self._ptrsize = caching.GetStaticCacheData("kern.ptrsize", None)
584 if self._ptrsize != None : return self._ptrsize
585 arch = LazyTarget.GetTarget().triple.split('-')[0]
d9a64523 586 if arch == 'x86_64' or arch.startswith('arm64'):
39236c6e
A
587 self._ptrsize = 8
588 else:
589 self._ptrsize = 4
590 caching.SaveStaticCacheData("kern.ptrsize", self._ptrsize)
591 return self._ptrsize
592
5ba3f43e
A
593 if name == 'VM_MIN_KERNEL_ADDRESS':
594 if self.arch == 'x86_64':
595 return unsigned(0xFFFFFF8000000000)
d9a64523 596 elif self.arch.startswith('arm64'):
5ba3f43e
A
597 return unsigned(0xffffffe000000000)
598 else:
599 return unsigned(0x80000000)
600
601 if name == 'VM_MIN_KERNEL_AND_KEXT_ADDRESS':
602 if self.arch == 'x86_64':
603 return self.VM_MIN_KERNEL_ADDRESS - 0x80000000
604 else:
605 return self.VM_MIN_KERNEL_ADDRESS
606
39236c6e 607 return object.__getattribute__(self, name)