xref: /xnu-8792.41.9/tools/lldbmacros/core/xnu_lldb_init.py (revision 5c2921b07a2480ab43ec66f5b9e41cb872bc554f)
1*5c2921b0SApple OSS Distributionsfrom __future__ import absolute_import, print_function
2*5c2921b0SApple OSS Distributions
3*5c2921b0SApple OSS Distributionsimport os
4*5c2921b0SApple OSS Distributionsimport re
5*5c2921b0SApple OSS Distributions
6*5c2921b0SApple OSS Distributionsdef GetSettingsValues(debugger, setting_variable_name):
7*5c2921b0SApple OSS Distributions    """ Queries the lldb internal settings
8*5c2921b0SApple OSS Distributions        params:
9*5c2921b0SApple OSS Distributions            debugger : lldb.SBDebugger instance
10*5c2921b0SApple OSS Distributions            setting_variable_name: str - string name of the setting(eg prompt)
11*5c2921b0SApple OSS Distributions        returns:
12*5c2921b0SApple OSS Distributions            [] : Array of strings. Empty array if setting is not found/set
13*5c2921b0SApple OSS Distributions    """
14*5c2921b0SApple OSS Distributions    retval = []
15*5c2921b0SApple OSS Distributions    settings_val_list = debugger.GetInternalVariableValue(setting_variable_name, debugger.GetInstanceName())
16*5c2921b0SApple OSS Distributions    for s in settings_val_list:
17*5c2921b0SApple OSS Distributions        retval.append(str(s))
18*5c2921b0SApple OSS Distributions    return retval
19*5c2921b0SApple OSS Distributions
20*5c2921b0SApple OSS Distributionsdef GetSymbolsFilePathFromModule(m):
21*5c2921b0SApple OSS Distributions    """ Get a file path from a module.
22*5c2921b0SApple OSS Distributions        params: m - lldb.target.module
23*5c2921b0SApple OSS Distributions        returns:
24*5c2921b0SApple OSS Distributions            str : path to first file based symbol. Note this might be dir path inside sources.
25*5c2921b0SApple OSS Distributions    """
26*5c2921b0SApple OSS Distributions    for s in m.symbols:
27*5c2921b0SApple OSS Distributions        if s.type == 8:
28*5c2921b0SApple OSS Distributions            return os.path.dirname(str(s.name))
29*5c2921b0SApple OSS Distributions    return ""
30*5c2921b0SApple OSS Distributions
31*5c2921b0SApple OSS Distributionsdef GetSourcePathSettings(binary_path, symbols_path):
32*5c2921b0SApple OSS Distributions    """ Parse the binary path and symbols_path to find if source-map setting is applicable
33*5c2921b0SApple OSS Distributions        params:
34*5c2921b0SApple OSS Distributions            binary_path: str path of the kernel module
35*5c2921b0SApple OSS Distributions            symbols_path: str path of the symbols stored in binary. Use
36*5c2921b0SApple OSS Distributions        returns:
37*5c2921b0SApple OSS Distributions            str : string command to set the source-map setting.
38*5c2921b0SApple OSS Distributions    """
39*5c2921b0SApple OSS Distributions    retval = ""
40*5c2921b0SApple OSS Distributions    train_re = re.compile(r"dsyms/([a-zA-Z]+)/")
41*5c2921b0SApple OSS Distributions    _t_arr = train_re.findall(binary_path)
42*5c2921b0SApple OSS Distributions    train = ''
43*5c2921b0SApple OSS Distributions    if _t_arr:
44*5c2921b0SApple OSS Distributions        train = _t_arr[0]
45*5c2921b0SApple OSS Distributions    if not train:
46*5c2921b0SApple OSS Distributions        return retval
47*5c2921b0SApple OSS Distributions    new_path = "~rc/Software/{}/Projects/".format(train)
48*5c2921b0SApple OSS Distributions    new_path = os.path.expanduser(new_path)
49*5c2921b0SApple OSS Distributions    new_path = os.path.normpath(new_path)
50*5c2921b0SApple OSS Distributions    common_path_re = re.compile("(^.*?Sources/)(xnu.*?)/.*$")
51*5c2921b0SApple OSS Distributions    _t_arr = common_path_re.findall(symbols_path)
52*5c2921b0SApple OSS Distributions    srcpath = ""
53*5c2921b0SApple OSS Distributions    projpath = "xnu"
54*5c2921b0SApple OSS Distributions    if _t_arr:
55*5c2921b0SApple OSS Distributions        srcpath = "".join(_t_arr[0])
56*5c2921b0SApple OSS Distributions        projpath = _t_arr[0][-1]
57*5c2921b0SApple OSS Distributions    else:
58*5c2921b0SApple OSS Distributions        return retval
59*5c2921b0SApple OSS Distributions
60*5c2921b0SApple OSS Distributions    new_path = new_path + os.path.sep +  projpath
61*5c2921b0SApple OSS Distributions    cmd = "settings append target.source-map {} {}"
62*5c2921b0SApple OSS Distributions    retval =  cmd.format(srcpath, new_path)
63*5c2921b0SApple OSS Distributions    return retval
64*5c2921b0SApple OSS Distributions
65*5c2921b0SApple OSS Distributionsdef CheckMissingLibs(debugger):
66*5c2921b0SApple OSS Distributions    """ Check that required modules are installed. """
67*5c2921b0SApple OSS Distributions
68*5c2921b0SApple OSS Distributions    # Collect lldb_host version
69*5c2921b0SApple OSS Distributions    ver_str = debugger.GetVersionString()
70*5c2921b0SApple OSS Distributions    lldb_ver = re.search("^lldb.*-(.*)$", ver_str, re.MULTILINE).group(1)
71*5c2921b0SApple OSS Distributions    ver = tuple(map(int, lldb_ver.split('.')))
72*5c2921b0SApple OSS Distributions
73*5c2921b0SApple OSS Distributions    # Display correct command to install missing packages.
74*5c2921b0SApple OSS Distributions    if ver[1] == 2:
75*5c2921b0SApple OSS Distributions        cmd_fmt = "Please install {mod:s}: xcrun --sdk <sdk> python3 -m pip install --user --ignore-installed {mod:s}"
76*5c2921b0SApple OSS Distributions    else:
77*5c2921b0SApple OSS Distributions        cmd_fmt = "Please install {mod:s}: xcrun pip3 install --user --ignore-installed {mod:s}"
78*5c2921b0SApple OSS Distributions
79*5c2921b0SApple OSS Distributions    try:
80*5c2921b0SApple OSS Distributions        import macholib
81*5c2921b0SApple OSS Distributions    except:
82*5c2921b0SApple OSS Distributions        print(cmd_fmt.format(mod="macholib"))
83*5c2921b0SApple OSS Distributions        return False
84*5c2921b0SApple OSS Distributions
85*5c2921b0SApple OSS Distributions    try:
86*5c2921b0SApple OSS Distributions        from future import standard_library
87*5c2921b0SApple OSS Distributions    except:
88*5c2921b0SApple OSS Distributions        print(cmd_fmt.format(mod="future"))
89*5c2921b0SApple OSS Distributions        return False
90*5c2921b0SApple OSS Distributions
91*5c2921b0SApple OSS Distributions    return True
92*5c2921b0SApple OSS Distributions
93*5c2921b0SApple OSS Distributionsdef __lldb_init_module(debugger, internal_dict):
94*5c2921b0SApple OSS Distributions
95*5c2921b0SApple OSS Distributions    if not CheckMissingLibs(debugger):
96*5c2921b0SApple OSS Distributions        print("Can't load LLDB macros. Please install dependencies first.")
97*5c2921b0SApple OSS Distributions        return
98*5c2921b0SApple OSS Distributions
99*5c2921b0SApple OSS Distributions    debug_session_enabled = False
100*5c2921b0SApple OSS Distributions    if "DEBUG_XNU_LLDBMACROS" in os.environ and len(os.environ['DEBUG_XNU_LLDBMACROS']) > 0:
101*5c2921b0SApple OSS Distributions        debug_session_enabled = True
102*5c2921b0SApple OSS Distributions    prev_os_plugin = "".join(GetSettingsValues(debugger, 'target.process.python-os-plugin-path'))
103*5c2921b0SApple OSS Distributions    print("Loading kernel debugging from %s" % __file__)
104*5c2921b0SApple OSS Distributions    print("LLDB version %s" % debugger.GetVersionString())
105*5c2921b0SApple OSS Distributions    self_path = "{}".format(__file__)
106*5c2921b0SApple OSS Distributions    base_dir_name = self_path[:self_path.rfind("/")]
107*5c2921b0SApple OSS Distributions    core_os_plugin = base_dir_name + "/lldbmacros/core/operating_system.py"
108*5c2921b0SApple OSS Distributions    osplugin_cmd = "settings set target.process.python-os-plugin-path \"%s\"" % core_os_plugin
109*5c2921b0SApple OSS Distributions    intel_whitelist = ['hndl_allintrs', 'hndl_alltraps', 'trap_from_kernel', 'hndl_double_fault', 'hndl_machine_check']
110*5c2921b0SApple OSS Distributions    arm_whitelist = ['_fleh_prefabt', '_ExceptionVectorsBase', '_ExceptionVectorsTable', '_fleh_undef', '_fleh_dataabt', '_fleh_irq', '_fleh_decirq', '_fleh_fiq_generic', '_fleh_dec']
111*5c2921b0SApple OSS Distributions    whitelist_trap_cmd = "settings set target.trap-handler-names %s %s" % (' '.join(intel_whitelist), ' '.join(arm_whitelist))
112*5c2921b0SApple OSS Distributions    xnu_debug_path = base_dir_name + "/lldbmacros/xnu.py"
113*5c2921b0SApple OSS Distributions    xnu_load_cmd = "command script import \"%s\"" % xnu_debug_path
114*5c2921b0SApple OSS Distributions    disable_optimization_warnings_cmd = "settings set target.process.optimization-warnings false"
115*5c2921b0SApple OSS Distributions
116*5c2921b0SApple OSS Distributions    # Single stepping support
117*5c2921b0SApple OSS Distributions    report_all_threads_cmd = "settings set target.process.experimental.os-plugin-reports-all-threads false"
118*5c2921b0SApple OSS Distributions    step_mode_cmd = "settings set target.process.run-all-threads true"
119*5c2921b0SApple OSS Distributions
120*5c2921b0SApple OSS Distributions    source_map_cmd = ""
121*5c2921b0SApple OSS Distributions    try:
122*5c2921b0SApple OSS Distributions        source_map_cmd = GetSourcePathSettings(base_dir_name, GetSymbolsFilePathFromModule(debugger.GetTargetAtIndex(0).modules[0]) )
123*5c2921b0SApple OSS Distributions    except Exception as e:
124*5c2921b0SApple OSS Distributions        pass
125*5c2921b0SApple OSS Distributions    if debug_session_enabled :
126*5c2921b0SApple OSS Distributions        if len(prev_os_plugin) > 0:
127*5c2921b0SApple OSS Distributions            print("\nDEBUG_XNU_LLDBMACROS is set. Skipping the setting of OS plugin from dSYM.\nYou can manually set the OS plugin by running\n" + osplugin_cmd)
128*5c2921b0SApple OSS Distributions        else:
129*5c2921b0SApple OSS Distributions            print(osplugin_cmd)
130*5c2921b0SApple OSS Distributions            debugger.HandleCommand(osplugin_cmd)
131*5c2921b0SApple OSS Distributions        print("\nDEBUG_XNU_LLDBMACROS is set. Skipping the load of xnu debug framework.\nYou can manually load the framework by running\n" + xnu_load_cmd)
132*5c2921b0SApple OSS Distributions    else:
133*5c2921b0SApple OSS Distributions        print(osplugin_cmd)
134*5c2921b0SApple OSS Distributions        debugger.HandleCommand(osplugin_cmd)
135*5c2921b0SApple OSS Distributions        print(whitelist_trap_cmd)
136*5c2921b0SApple OSS Distributions        debugger.HandleCommand(whitelist_trap_cmd)
137*5c2921b0SApple OSS Distributions        print(xnu_load_cmd)
138*5c2921b0SApple OSS Distributions        debugger.HandleCommand(xnu_load_cmd)
139*5c2921b0SApple OSS Distributions        print(disable_optimization_warnings_cmd)
140*5c2921b0SApple OSS Distributions        debugger.HandleCommand(disable_optimization_warnings_cmd)
141*5c2921b0SApple OSS Distributions        print(report_all_threads_cmd)
142*5c2921b0SApple OSS Distributions        debugger.HandleCommand(report_all_threads_cmd)
143*5c2921b0SApple OSS Distributions        print(step_mode_cmd)
144*5c2921b0SApple OSS Distributions        debugger.HandleCommand(step_mode_cmd)
145*5c2921b0SApple OSS Distributions        if source_map_cmd:
146*5c2921b0SApple OSS Distributions            print(source_map_cmd)
147*5c2921b0SApple OSS Distributions            debugger.HandleCommand(source_map_cmd)
148*5c2921b0SApple OSS Distributions
149*5c2921b0SApple OSS Distributions        load_kexts = True
150*5c2921b0SApple OSS Distributions        if "XNU_LLDBMACROS_NOBUILTINKEXTS" in os.environ and len(os.environ['XNU_LLDBMACROS_NOBUILTINKEXTS']) > 0:
151*5c2921b0SApple OSS Distributions            load_kexts = False
152*5c2921b0SApple OSS Distributions        builtinkexts_path = os.path.join(os.path.dirname(self_path), "lldbmacros", "builtinkexts")
153*5c2921b0SApple OSS Distributions        if os.access(builtinkexts_path, os.F_OK):
154*5c2921b0SApple OSS Distributions            kexts = os.listdir(builtinkexts_path)
155*5c2921b0SApple OSS Distributions            if len(kexts) > 0:
156*5c2921b0SApple OSS Distributions                print("\nBuiltin kexts: %s\n" % kexts)
157*5c2921b0SApple OSS Distributions                if load_kexts == False:
158*5c2921b0SApple OSS Distributions                    print("XNU_LLDBMACROS_NOBUILTINKEXTS is set, not loading:\n")
159*5c2921b0SApple OSS Distributions                for kextdir in kexts:
160*5c2921b0SApple OSS Distributions                    # Python does not handle well modules that contain '-' in their names.
161*5c2921b0SApple OSS Distributions                    # Remap such scripts to use '_' instead.
162*5c2921b0SApple OSS Distributions                    script_name = kextdir.split('.')[-1].replace('-', '_') + ".py"
163*5c2921b0SApple OSS Distributions                    script = os.path.join(builtinkexts_path, kextdir, script_name)
164*5c2921b0SApple OSS Distributions
165*5c2921b0SApple OSS Distributions                    import_kext_cmd = "command script import \"%s\"" % script
166*5c2921b0SApple OSS Distributions                    print("%s" % import_kext_cmd)
167*5c2921b0SApple OSS Distributions                    if load_kexts:
168*5c2921b0SApple OSS Distributions                        debugger.HandleCommand(import_kext_cmd)
169*5c2921b0SApple OSS Distributions
170*5c2921b0SApple OSS Distributions    print("\n")
171*5c2921b0SApple OSS Distributions
172