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 if cmd_args is None or len(cmd_args) == 0: 27 raise ArgumentError() 28 29 refgrp = kern.GetValueFromAddress(cmd_args[0], 'struct os_refgrp *') 30 if not refgrp: 31 raise ArgumentError("Unknown arguments: {:s}".format(cmd_args[0])) 32 33 print(GetOSRefGrpSummary.header) 34 print(GetOSRefGrpSummary(refgrp)) 35# EndMacro: showosrefgrp 36 37 38# Macro: showosrefgrphierarchy 39@lldb_command('showosrefgrphierarchy') 40def ShowOSRefGrpHierarchy(cmd_args=None): 41 """ Display the os reference group hiearchy associated with the specified 42 os reference group 43 Usage: showosrefgrphierarchy <refgrp address> 44 """ 45 if cmd_args is None or len(cmd_args) == 0: 46 raise ArgumentError() 47 48 refgrp = kern.GetValueFromAddress(cmd_args[0], 'struct os_refgrp *') 49 if not refgrp: 50 raise ArgumentError("Unknown arguments: {:s}".format(cmd_args[0])) 51 52 grps = [] 53 54 parent = refgrp 55 while parent != 0: 56 grps.insert(0, parent) 57 parent = parent.grp_parent 58 59 for grp in grps: 60 print(GetOSRefGrpSummary(grp)) 61# EndMacro: showosrefgrphierarchy 62 63# Macro: showglobaltaskrefgrps 64@lldb_command('showglobaltaskrefgrps') 65def ShowGlobalTaskRefGrps(cmd_args=None): 66 """ Display all global task reference count groups 67 Usage: showglobaltaskrefgrps 68 """ 69 70 print(GetOSRefGrpSummary.header) 71 72 # First print global groups 73 task_refgrp = kern.globals.task_refgrp 74 count = sizeof(task_refgrp) // sizeof('struct os_refgrp *') 75 i = 0 76 while i < count: 77 if task_refgrp[i].grp_retain_total != 0: 78 print(GetOSRefGrpSummary(task_refgrp[i])) 79 i += 1 80 81 # Then print kext groups 82 count = kern.globals.sKextAccountsCount 83 kext_accounts_base = addressof(kern.globals.sKextAccounts[0]) 84 for i in range(count): 85 kextaccount = GetObjectAtIndexFromArray(kext_accounts_base, i) 86 if not kextaccount.account: 87 continue 88 task_refgrp = kextaccount.account.task_refgrp 89 90 if task_refgrp.grp_retain_total != 0: 91 print(GetOSRefGrpSummary(addressof(task_refgrp))) 92# EndMacro: showglobaltaskrefgrps 93 94 95# Macro: showtaskrefgrps 96@lldb_command('showtaskrefgrps') 97def ShowTaskRefGrps(cmd_args=None): 98 """ Display per-task reference count groups 99 Usage: showtaskrefgrps <address of task> 100 """ 101 102 if cmd_args is None or len(cmd_args) == 0: 103 raise ArgumentError("Invalid arguments passed.") 104 tval = kern.GetValueFromAddress(cmd_args[0], 'task *') 105 106 print(GetOSRefGrpSummary.header) 107 108 grp = tval.ref_group 109 if kern.globals.task_refgrp_config == 0: 110 count = 2 111 if kern.globals.task_refgrp_config == 1: 112 count = 8 113 if kern.globals.task_refgrp_config == 2: 114 count = 0 115 i = 0 116 while i < count: 117 if grp[i].grp_retain_total != 0: 118 print(GetOSRefGrpSummary(addressof(grp[i]))) 119 i += 1 120 121# EndMacro: showtaskrefgrps 122