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