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