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