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 # Convert LLDB version string to version tuple. 69 # A version string may be of form: lldb_host-1403.2.6.11 (iPhoneOS) 70 # Code below only matches 1403.2.6.11 and ignores rest of the string. 71 ver_str = debugger.GetVersionString() 72 lldb_ver = re.search("^lldb.*-([0-9.]+)", ver_str, re.MULTILINE).group(1) 73 ver = tuple(map(int, lldb_ver.split('.'))) 74 75 # Display correct command to install missing packages. 76 if ver[1] == 2: 77 cmd_fmt = "Please install {mod:s}: xcrun --sdk <sdk> python3 -m pip install --user --ignore-installed {mod:s}" 78 else: 79 cmd_fmt = "Please install {mod:s}: xcrun pip3 install --user --ignore-installed {mod:s}" 80 81 try: 82 import macholib 83 except: 84 print(cmd_fmt.format(mod="macholib")) 85 return False 86 87 try: 88 from future import standard_library 89 except: 90 print(cmd_fmt.format(mod="future")) 91 return False 92 93 return True 94 95def __lldb_init_module(debugger, internal_dict): 96 97 if not CheckMissingLibs(debugger): 98 print("Can't load LLDB macros. Please install dependencies first.") 99 return 100 101 debug_session_enabled = False 102 if "DEBUG_XNU_LLDBMACROS" in os.environ and len(os.environ['DEBUG_XNU_LLDBMACROS']) > 0: 103 debug_session_enabled = True 104 prev_os_plugin = "".join(GetSettingsValues(debugger, 'target.process.python-os-plugin-path')) 105 print("Loading kernel debugging from %s" % __file__) 106 print("LLDB version %s" % debugger.GetVersionString()) 107 self_path = "{}".format(__file__) 108 base_dir_name = self_path[:self_path.rfind("/")] 109 core_os_plugin = base_dir_name + "/lldbmacros/core/operating_system.py" 110 osplugin_cmd = "settings set target.process.python-os-plugin-path \"%s\"" % core_os_plugin 111 intel_whitelist = ['hndl_allintrs', 'hndl_alltraps', 'trap_from_kernel', 'hndl_double_fault', 'hndl_machine_check'] 112 arm_whitelist = ['_fleh_prefabt', '_ExceptionVectorsBase', '_ExceptionVectorsTable', '_fleh_undef', '_fleh_dataabt', '_fleh_irq', '_fleh_decirq', '_fleh_fiq_generic', '_fleh_dec'] 113 whitelist_trap_cmd = "settings set target.trap-handler-names %s %s" % (' '.join(intel_whitelist), ' '.join(arm_whitelist)) 114 xnu_debug_path = base_dir_name + "/lldbmacros/xnu.py" 115 xnu_load_cmd = "command script import \"%s\"" % xnu_debug_path 116 disable_optimization_warnings_cmd = "settings set target.process.optimization-warnings false" 117 118 # Single stepping support 119 report_all_threads_cmd = "settings set target.process.experimental.os-plugin-reports-all-threads false" 120 step_mode_cmd = "settings set target.process.run-all-threads true" 121 122 source_map_cmd = "" 123 try: 124 source_map_cmd = GetSourcePathSettings(base_dir_name, GetSymbolsFilePathFromModule(debugger.GetTargetAtIndex(0).modules[0]) ) 125 except Exception as e: 126 pass 127 if debug_session_enabled : 128 if len(prev_os_plugin) > 0: 129 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) 130 else: 131 print(osplugin_cmd) 132 debugger.HandleCommand(osplugin_cmd) 133 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) 134 else: 135 print(osplugin_cmd) 136 debugger.HandleCommand(osplugin_cmd) 137 print(whitelist_trap_cmd) 138 debugger.HandleCommand(whitelist_trap_cmd) 139 print(xnu_load_cmd) 140 debugger.HandleCommand(xnu_load_cmd) 141 print(disable_optimization_warnings_cmd) 142 debugger.HandleCommand(disable_optimization_warnings_cmd) 143 print(report_all_threads_cmd) 144 debugger.HandleCommand(report_all_threads_cmd) 145 print(step_mode_cmd) 146 debugger.HandleCommand(step_mode_cmd) 147 if source_map_cmd: 148 print(source_map_cmd) 149 debugger.HandleCommand(source_map_cmd) 150 151 load_kexts = True 152 if "XNU_LLDBMACROS_NOBUILTINKEXTS" in os.environ and len(os.environ['XNU_LLDBMACROS_NOBUILTINKEXTS']) > 0: 153 load_kexts = False 154 builtinkexts_path = os.path.join(os.path.dirname(self_path), "lldbmacros", "builtinkexts") 155 if os.access(builtinkexts_path, os.F_OK): 156 kexts = os.listdir(builtinkexts_path) 157 if len(kexts) > 0: 158 print("\nBuiltin kexts: %s\n" % kexts) 159 if load_kexts == False: 160 print("XNU_LLDBMACROS_NOBUILTINKEXTS is set, not loading:\n") 161 for kextdir in kexts: 162 # Python does not handle well modules that contain '-' in their names. 163 # Remap such scripts to use '_' instead. 164 script_name = kextdir.split('.')[-1].replace('-', '_') + ".py" 165 script = os.path.join(builtinkexts_path, kextdir, script_name) 166 167 import_kext_cmd = "command script import \"%s\"" % script 168 print("%s" % import_kext_cmd) 169 if load_kexts: 170 debugger.HandleCommand(import_kext_cmd) 171 172 print("\n") 173 174