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