X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/3e170ce000f1506b7b5d2c5c7faec85ceabb573d..b226f5e54a60dc81db17b1260381d7dbfea3cdf1:/tools/lldbmacros/core/xnu_lldb_init.py diff --git a/tools/lldbmacros/core/xnu_lldb_init.py b/tools/lldbmacros/core/xnu_lldb_init.py old mode 100644 new mode 100755 index b51824644..e7f494b96 --- a/tools/lldbmacros/core/xnu_lldb_init.py +++ b/tools/lldbmacros/core/xnu_lldb_init.py @@ -1,4 +1,5 @@ import os +import re def GetSettingsValues(debugger, setting_variable_name): """ Queries the lldb internal settings @@ -14,6 +15,52 @@ def GetSettingsValues(debugger, setting_variable_name): retval.append(str(s)) return retval +def GetSymbolsFilePathFromModule(m): + """ Get a file path from a module. + params: m - lldb.target.module + returns: + str : path to first file based symbol. Note this might be dir path inside sources. + """ + for s in m.symbols: + if s.type == 8: + return os.path.dirname(str(s.name)) + return "" + +def GetSourcePathSettings(binary_path, symbols_path): + """ Parse the binary path and symbols_path to find if source-map setting is applicable + params: + binary_path: str path of the kernel module + symbols_path: str path of the symbols stored in binary. Use + returns: + str : string command to set the source-map setting. + """ + retval = "" + train_re = re.compile(r"dsyms/([a-zA-Z]+)/") + _t_arr = train_re.findall(binary_path) + train = '' + if _t_arr: + train = _t_arr[0] + if not train: + return retval + new_path = "~rc/Software/{}/Projects/".format(train) + new_path = os.path.expanduser(new_path) + new_path = os.path.normpath(new_path) + common_path_re = re.compile("(^.*?Sources/)(xnu.*?)/.*$") + _t_arr = common_path_re.findall(symbols_path) + srcpath = "" + projpath = "xnu" + if _t_arr: + srcpath = "".join(_t_arr[0]) + projpath = _t_arr[0][-1] + else: + return retval + + new_path = new_path + os.path.sep + projpath + cmd = "settings append target.source-map {} {}" + retval = cmd.format(srcpath, new_path) + return retval + + def __lldb_init_module(debugger, internal_dict): debug_session_enabled = False if "DEBUG_XNU_LLDBMACROS" in os.environ and len(os.environ['DEBUG_XNU_LLDBMACROS']) > 0: @@ -30,6 +77,13 @@ def __lldb_init_module(debugger, internal_dict): whitelist_trap_cmd = "settings set target.trap-handler-names %s %s" % (' '.join(intel_whitelist), ' '.join(arm_whitelist)) xnu_debug_path = base_dir_name + "/lldbmacros/xnu.py" xnu_load_cmd = "command script import \"%s\"" % xnu_debug_path + disable_optimization_warnings_cmd = "settings set target.process.optimization-warnings false" + + source_map_cmd = "" + try: + source_map_cmd = GetSourcePathSettings(base_dir_name, GetSymbolsFilePathFromModule(debugger.GetTargetAtIndex(0).modules[0]) ) + except Exception as e: + pass if debug_session_enabled : if len(prev_os_plugin) > 0: 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 @@ -44,5 +98,28 @@ def __lldb_init_module(debugger, internal_dict): debugger.HandleCommand(whitelist_trap_cmd) print xnu_load_cmd debugger.HandleCommand(xnu_load_cmd) + print disable_optimization_warnings_cmd + debugger.HandleCommand(disable_optimization_warnings_cmd) + if source_map_cmd: + print source_map_cmd + debugger.HandleCommand(source_map_cmd) + + load_kexts = True + if "XNU_LLDBMACROS_NOBUILTINKEXTS" in os.environ and len(os.environ['XNU_LLDBMACROS_NOBUILTINKEXTS']) > 0: + load_kexts = False + builtinkexts_path = os.path.join(os.path.dirname(self_path), "lldbmacros", "builtinkexts") + if os.access(builtinkexts_path, os.F_OK): + kexts = os.listdir(builtinkexts_path) + if len(kexts) > 0: + print "\nBuiltin kexts: %s\n" % kexts + if load_kexts == False: + print "XNU_LLDBMACROS_NOBUILTINKEXTS is set, not loading:\n" + for kextdir in kexts: + script = os.path.join(builtinkexts_path, kextdir, kextdir.split('.')[-1] + ".py") + import_kext_cmd = "command script import \"%s\"" % script + print "%s" % import_kext_cmd + if load_kexts: + debugger.HandleCommand(import_kext_cmd) + print "\n"