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