1*e3723e1fSApple OSS Distributions""" 2*e3723e1fSApple OSS Distributions Triage Macros for zone related panics 3*e3723e1fSApple OSS Distributions 4*e3723e1fSApple OSS Distributions Supported panic strings from xnu/osfmk/kern/zalloc.c: 5*e3723e1fSApple OSS Distributions "a freed zone element has been modified in zone %s: expected %p but found %p, bits changed %p, at offset %d of %d in element %p, cookies %p %p" and 6*e3723e1fSApple OSS Distributions "zalloc: zone map exhausted while allocating from zone %s, likely due to memory leak in zone %s (%lu total bytes, %d elements allocated)" 7*e3723e1fSApple OSS Distributions These macros are dependant on the above panic strings. If the strings are modified in any way, this script must be updated to reflect the change. 8*e3723e1fSApple OSS Distributions 9*e3723e1fSApple OSS Distributions To support more zone panic strings: 10*e3723e1fSApple OSS Distributions 1. Add the panic string regex to the globals and include in the named capture group 'zone' (the zone to be 11*e3723e1fSApple OSS Distributions logged) as well as any other info necessary to parse out of the panic string. 12*e3723e1fSApple OSS Distributions 2. Add a check for the panic string regex in ZoneTriage(), which then calls into the function you create. 13*e3723e1fSApple OSS Distributions 3. Add a check for the panic string regex in CheckZoneBootArgs() which sets the variable panic_string_regex to your 14*e3723e1fSApple OSS Distributions panic string regex if found. 15*e3723e1fSApple OSS Distributions 4. Create a function that can be called either through the zonetriage macro ZoneTriage() or using its own macro. 16*e3723e1fSApple OSS Distributions This function should handle all lldb commands you want to run for this type of zone panic. 17*e3723e1fSApple OSS Distributions""" 18*e3723e1fSApple OSS Distributionsfrom xnu import * 19*e3723e1fSApple OSS Distributionsimport sys, shlex 20*e3723e1fSApple OSS Distributionsfrom utils import * 21*e3723e1fSApple OSS Distributionsimport xnudefines 22*e3723e1fSApple OSS Distributionsimport re 23*e3723e1fSApple OSS Distributionsimport os.path 24*e3723e1fSApple OSS Distributions 25*e3723e1fSApple OSS Distributions## Globals 26*e3723e1fSApple OSS Distributionspanic_string = None 27*e3723e1fSApple OSS Distributions## If the following panic strings are modified in xnu/osfmk/kern/zalloc.c, they must be updated here to reflect the change. 28*e3723e1fSApple OSS Distributionszone_element_modified = ".*a freed zone element has been modified in zone (?P<zone>.+): expected (0x)?([0-9A-Fa-f]*)? but found (0x)?([0-9A-Fa-f]*)?, bits changed (0x)?([0-9A-Fa-f]*)?, at offset ([0-9]*)? of ([0-9]*)? in element (?P<element>0x[0-9A-Fa-f]*), cookies (0x)?([0-9A-Fa-f]*)? (0x)?([0-9A-Fa-f]*)?.*" 29*e3723e1fSApple OSS Distributionszone_map_exhausted = ".*zalloc: zone map exhausted while allocating from zone .+, likely due to memory leak in zone (?P<zone>.+) \(([0-9]*)? total bytes, ([0-9]*)? elements allocated\).*" 30*e3723e1fSApple OSS Distributions 31*e3723e1fSApple OSS Distributions# Macro: zonetriage, zonetriage_freedelement, zonetriage_memoryleak 32*e3723e1fSApple OSS Distributions@lldb_command('zonetriage') 33*e3723e1fSApple OSS Distributionsdef ZoneTriage(cmd_args=None): 34*e3723e1fSApple OSS Distributions """ Calls function specific to type of zone panic based on the panic string 35*e3723e1fSApple OSS Distributions """ 36*e3723e1fSApple OSS Distributions global panic_string 37*e3723e1fSApple OSS Distributions if panic_string is None: 38*e3723e1fSApple OSS Distributions try: 39*e3723e1fSApple OSS Distributions panic_string = lldb_run_command("paniclog").split('\n', 1)[0] 40*e3723e1fSApple OSS Distributions except: 41*e3723e1fSApple OSS Distributions return 42*e3723e1fSApple OSS Distributions if re.match(zone_element_modified, panic_string) is not None: 43*e3723e1fSApple OSS Distributions ZoneTriageFreedElement() 44*e3723e1fSApple OSS Distributions elif re.match(zone_map_exhausted, panic_string) is not None: 45*e3723e1fSApple OSS Distributions ZoneTriageMemoryLeak() 46*e3723e1fSApple OSS Distributions else: 47*e3723e1fSApple OSS Distributions print("zonetriage does not currently support this panic string.") 48*e3723e1fSApple OSS Distributions 49*e3723e1fSApple OSS Distributions@lldb_command('zonetriage_freedelement') 50*e3723e1fSApple OSS Distributionsdef ZoneTriageFreedElement(cmd_args=None): 51*e3723e1fSApple OSS Distributions """ Runs zstack_findelem on the element and zone being logged based on the panic string regex 52*e3723e1fSApple OSS Distributions """ 53*e3723e1fSApple OSS Distributions global panic_string 54*e3723e1fSApple OSS Distributions if panic_string is None: 55*e3723e1fSApple OSS Distributions try: 56*e3723e1fSApple OSS Distributions panic_string = lldb_run_command("paniclog").split('\n', 1)[0] 57*e3723e1fSApple OSS Distributions except: 58*e3723e1fSApple OSS Distributions return 59*e3723e1fSApple OSS Distributions CheckZoneBootArgs() 60*e3723e1fSApple OSS Distributions ## Run showzonesbeinglogged. 61*e3723e1fSApple OSS Distributions print("(lldb) zstack_showzonesbeinglogged\n%s\n" % lldb_run_command("zstack_showzonesbeinglogged")) 62*e3723e1fSApple OSS Distributions ## Capture zone and element from panic string. 63*e3723e1fSApple OSS Distributions values = re.search(zone_element_modified, panic_string) 64*e3723e1fSApple OSS Distributions if values is None or 'zone' not in values.group() or 'element' not in values.group(): 65*e3723e1fSApple OSS Distributions return 66*e3723e1fSApple OSS Distributions element = values.group('element') 67*e3723e1fSApple OSS Distributions zone = values.group('zone') 68*e3723e1fSApple OSS Distributions btlog = FindZoneBTLog(zone) 69*e3723e1fSApple OSS Distributions if btlog is not None: 70*e3723e1fSApple OSS Distributions print("(lldb) zstack_findelem " + btlog + " " + element) 71*e3723e1fSApple OSS Distributions findelem_output = lldb_run_command("zstack_findelem " + btlog + " " + element) 72*e3723e1fSApple OSS Distributions findelem_output = re.sub('Scanning is ongoing. [0-9]* items scanned since last check.\n', '', findelem_output) 73*e3723e1fSApple OSS Distributions print(findelem_output) 74*e3723e1fSApple OSS Distributions 75*e3723e1fSApple OSS Distributions@lldb_command('zonetriage_memoryleak') 76*e3723e1fSApple OSS Distributionsdef ZoneTriageMemoryLeak(cmd_args=None): 77*e3723e1fSApple OSS Distributions """ Runs zstack_findtop and zstack_findleak on all zones being logged 78*e3723e1fSApple OSS Distributions """ 79*e3723e1fSApple OSS Distributions global kern 80*e3723e1fSApple OSS Distributions CheckZoneBootArgs() 81*e3723e1fSApple OSS Distributions ## Run showzonesbeinglogged. 82*e3723e1fSApple OSS Distributions print("(lldb) zstack_showzonesbeinglogged\n%s\n" % lldb_run_command("zstack_showzonesbeinglogged")) 83*e3723e1fSApple OSS Distributions for zval, _ in kern.zones: 84*e3723e1fSApple OSS Distributions btlog = getattr(zval, 'z_btlog', None) 85*e3723e1fSApple OSS Distributions if btlog: 86*e3723e1fSApple OSS Distributions print('%s:' % zval.z_name) 87*e3723e1fSApple OSS Distributions print("(lldb) zstack_findtop -N 5 0x%lx" % btlog) 88*e3723e1fSApple OSS Distributions print(lldb_run_command("zstack_findtop -N 5 0x%lx" % btlog)) 89*e3723e1fSApple OSS Distributions print("(lldb) zstack_findleak 0x%lx" % btlog) 90*e3723e1fSApple OSS Distributions print(lldb_run_command("zstack_findleak 0x%lx" % btlog)) 91*e3723e1fSApple OSS Distributions 92*e3723e1fSApple OSS Distributionsdef CheckZoneBootArgs(cmd_args=None): 93*e3723e1fSApple OSS Distributions """ Check boot args to see if zone is being logged, if not, suggest new boot args 94*e3723e1fSApple OSS Distributions """ 95*e3723e1fSApple OSS Distributions global panic_string 96*e3723e1fSApple OSS Distributions if panic_string is None: 97*e3723e1fSApple OSS Distributions try: 98*e3723e1fSApple OSS Distributions panic_string = lldb_run_command("paniclog").split('\n', 1)[0] 99*e3723e1fSApple OSS Distributions except: 100*e3723e1fSApple OSS Distributions return 101*e3723e1fSApple OSS Distributions panic_string_regex = "" 102*e3723e1fSApple OSS Distributions if re.match(zone_element_modified, panic_string) is not None: 103*e3723e1fSApple OSS Distributions panic_string_regex = zone_element_modified 104*e3723e1fSApple OSS Distributions if re.match(zone_map_exhausted, panic_string) is not None: 105*e3723e1fSApple OSS Distributions panic_string_regex = zone_map_exhausted 106*e3723e1fSApple OSS Distributions values = re.search(panic_string_regex, panic_string) 107*e3723e1fSApple OSS Distributions if values is None or 'zone' not in values.group(): 108*e3723e1fSApple OSS Distributions return 109*e3723e1fSApple OSS Distributions zone = values.group('zone') 110*e3723e1fSApple OSS Distributions bootargs = lldb_run_command("showbootargs") 111*e3723e1fSApple OSS Distributions correct_boot_args = re.search('zlog([1-9]|10)?=' + re.sub(' ', '.', zone), bootargs) 112*e3723e1fSApple OSS Distributions if correct_boot_args is None: 113*e3723e1fSApple OSS Distributions print("Current boot-args:\n" + bootargs) 114*e3723e1fSApple OSS Distributions print("You may need to include: -zc -zp zlog([1-9]|10)?=" + re.sub(' ', '.', zone)) 115*e3723e1fSApple OSS Distributions 116*e3723e1fSApple OSS Distributionsdef FindZoneBTLog(zone): 117*e3723e1fSApple OSS Distributions """ Returns the btlog address in the format 0x%lx for the zone name passed as a parameter 118*e3723e1fSApple OSS Distributions """ 119*e3723e1fSApple OSS Distributions global kern 120*e3723e1fSApple OSS Distributions for zval, _ in kern.zones: 121*e3723e1fSApple OSS Distributions btlog = getattr(zval, 'z_btlog', None) 122*e3723e1fSApple OSS Distributions if btlog: 123*e3723e1fSApple OSS Distributions if zone == "%s" % zval.z_name: 124*e3723e1fSApple OSS Distributions return "0x%lx" % btlog 125*e3723e1fSApple OSS Distributions return None 126*e3723e1fSApple OSS Distributions# EndMacro: zonetriage, zonetriage_freedelement, zonetriage_memoryleak 127