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