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