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