1from xnu import * 2 3 4@lldb_type_summary(['struct os_refgrp *']) 5@header("{0: <18s} {1: <46s} {2: <9s} {3: <9s} {4: <9s} {5: <18s}" 6 .format("os_refgrp", "name", "count", "retain", "release", "log")) 7def GetOSRefGrpSummary(refgrp): 8 """ Summarizes os_refgrp structure. 9 params: refgrp: value - value object representing an os_refgrp in 10 kernel 11 returns: str - summary of the os reference group 12 """ 13 14 format_string = "{0: <#18x} {1: <46s} {2: <9d} {3: <9d} {4: <9d} {5: <#18x}" 15 16 return format_string.format(refgrp, str(refgrp.grp_name), 17 refgrp.grp_count, refgrp.grp_retain_total, 18 refgrp.grp_release_total, refgrp.grp_log) 19 20# Macro: showosrefgrp 21@lldb_command('showosrefgrp') 22def ShowOSRefGrpHelper(cmd_args=None): 23 """ Display a summary of the specified os reference group 24 Usage: showosrefgrp <refgrp address> 25 """ 26 27 refgrp = kern.GetValueFromAddress(cmd_args[0], 'struct os_refgrp *') 28 if not refgrp: 29 raise ArgumentError("Unknown arguments: {:s}".format(cmd_args[0])) 30 31 print(GetOSRefGrpSummary.header) 32 print(GetOSRefGrpSummary(refgrp)) 33# EndMacro: showosrefgrp 34 35 36# Macro: showosrefgrphierarchy 37@lldb_command('showosrefgrphierarchy') 38def ShowOSRefGrpHierarchy(cmd_args=None): 39 """ Display the os reference group hiearchy associated with the specified 40 os reference group 41 Usage: showosrefgrphierarchy <refgrp address> 42 """ 43 44 refgrp = kern.GetValueFromAddress(cmd_args[0], 'struct os_refgrp *') 45 if not refgrp: 46 raise ArgumentError("Unknown arguments: {:s}".format(cmd_args[0])) 47 48 grps = [] 49 50 parent = refgrp 51 while parent != 0: 52 grps.insert(0, parent) 53 parent = parent.grp_parent 54 55 for grp in grps: 56 print(GetOSRefGrpSummary(grp)) 57# EndMacro: showosrefgrphierarchy 58 59# Macro: showglobaltaskrefgrps 60@lldb_command('showglobaltaskrefgrps') 61def ShowGlobalTaskRefGrps(cmd_args=None): 62 """ Display all global task reference count groups 63 Usage: showglobaltaskrefgrps 64 """ 65 66 print(GetOSRefGrpSummary.header) 67 68 # First print global groups 69 task_refgrp = kern.globals.task_refgrp 70 count = sizeof(task_refgrp) // sizeof('struct os_refgrp *') 71 i = 0 72 while i < count: 73 if task_refgrp[i].grp_retain_total != 0: 74 print(GetOSRefGrpSummary(task_refgrp[i])) 75 i += 1 76 77 # Then print kext groups 78 count = kern.globals.sKextAccountsCount 79 i = 0 80 while i < count: 81 a = GetObjectAtIndexFromArray(addressof(kern.globals.sKextAccounts[0]), i) 82 g = a.account.task_refgrp 83 84 if g.grp_retain_total != 0: 85 print(GetOSRefGrpSummary(addressof(g))) 86 i += 1 87# EndMacro: showglobaltaskrefgrps 88 89 90# Macro: showtaskrefgrps 91@lldb_command('showtaskrefgrps') 92def ShowTaskRefGrps(cmd_args=None): 93 """ Display per-task reference count groups 94 Usage: showtaskrefgrps <address of task> 95 """ 96 97 if not cmd_args: 98 raise ArgumentError("Invalid arguments passed.") 99 tval = kern.GetValueFromAddress(cmd_args[0], 'task *') 100 101 print(GetOSRefGrpSummary.header) 102 103 grp = tval.ref_group 104 if kern.globals.task_refgrp_config == 0: 105 count = 2 106 if kern.globals.task_refgrp_config == 1: 107 count = 8 108 if kern.globals.task_refgrp_config == 2: 109 count = 0 110 i = 0 111 while i < count: 112 if grp[i].grp_retain_total != 0: 113 print(GetOSRefGrpSummary(addressof(grp[i]))) 114 i += 1 115 116# EndMacro: showtaskrefgrps 117