]> git.saurik.com Git - apple/xnu.git/blob - tools/lldbmacros/core/xnu_lldb_init.py
xnu-7195.101.1.tar.gz
[apple/xnu.git] / tools / lldbmacros / core / xnu_lldb_init.py
1 from __future__ import absolute_import
2 from __future__ import print_function
3 import os
4 import sys
5 import re
6
7 PY3 = sys.version_info > (3,)
8
9 def GetSettingsValues(debugger, setting_variable_name):
10 """ Queries the lldb internal settings
11 params:
12 debugger : lldb.SBDebugger instance
13 setting_variable_name: str - string name of the setting(eg prompt)
14 returns:
15 [] : Array of strings. Empty array if setting is not found/set
16 """
17 retval = []
18 settings_val_list = debugger.GetInternalVariableValue(setting_variable_name, debugger.GetInstanceName())
19 for s in settings_val_list:
20 retval.append(str(s))
21 return retval
22
23 def GetSymbolsFilePathFromModule(m):
24 """ Get a file path from a module.
25 params: m - lldb.target.module
26 returns:
27 str : path to first file based symbol. Note this might be dir path inside sources.
28 """
29 for s in m.symbols:
30 if s.type == 8:
31 return os.path.dirname(str(s.name))
32 return ""
33
34 def GetSourcePathSettings(binary_path, symbols_path):
35 """ Parse the binary path and symbols_path to find if source-map setting is applicable
36 params:
37 binary_path: str path of the kernel module
38 symbols_path: str path of the symbols stored in binary. Use
39 returns:
40 str : string command to set the source-map setting.
41 """
42 retval = ""
43 train_re = re.compile(r"dsyms/([a-zA-Z]+)/")
44 _t_arr = train_re.findall(binary_path)
45 train = ''
46 if _t_arr:
47 train = _t_arr[0]
48 if not train:
49 return retval
50 new_path = "~rc/Software/{}/Projects/".format(train)
51 new_path = os.path.expanduser(new_path)
52 new_path = os.path.normpath(new_path)
53 common_path_re = re.compile("(^.*?Sources/)(xnu.*?)/.*$")
54 _t_arr = common_path_re.findall(symbols_path)
55 srcpath = ""
56 projpath = "xnu"
57 if _t_arr:
58 srcpath = "".join(_t_arr[0])
59 projpath = _t_arr[0][-1]
60 else:
61 return retval
62
63 new_path = new_path + os.path.sep + projpath
64 cmd = "settings append target.source-map {} {}"
65 retval = cmd.format(srcpath, new_path)
66 return retval
67
68
69 def __lldb_init_module(debugger, internal_dict):
70 debug_session_enabled = False
71 if "DEBUG_XNU_LLDBMACROS" in os.environ and len(os.environ['DEBUG_XNU_LLDBMACROS']) > 0:
72 debug_session_enabled = True
73 prev_os_plugin = "".join(GetSettingsValues(debugger, 'target.process.python-os-plugin-path'))
74 if PY3:
75 print("#" * 30)
76 print("WARNING! Python version 3 is not supported for xnu lldbmacros.")
77 print("Please restart your debugging session with the following workaround")
78 print("\ndefaults write com.apple.dt.lldb DefaultPythonVersion 2\n")
79 print("#" * 30)
80 print("\n")
81 print("Loading kernel debugging from %s" % __file__)
82 print("LLDB version %s" % debugger.GetVersionString())
83 self_path = "{}".format(__file__)
84 base_dir_name = self_path[:self_path.rfind("/")]
85 core_os_plugin = base_dir_name + "/lldbmacros/core/operating_system.py"
86 osplugin_cmd = "settings set target.process.python-os-plugin-path \"%s\"" % core_os_plugin
87 intel_whitelist = ['hndl_allintrs', 'hndl_alltraps', 'trap_from_kernel', 'hndl_double_fault', 'hndl_machine_check']
88 arm_whitelist = ['_fleh_prefabt', '_ExceptionVectorsBase', '_ExceptionVectorsTable', '_fleh_undef', '_fleh_dataabt', '_fleh_irq', '_fleh_decirq', '_fleh_fiq_generic', '_fleh_dec']
89 whitelist_trap_cmd = "settings set target.trap-handler-names %s %s" % (' '.join(intel_whitelist), ' '.join(arm_whitelist))
90 xnu_debug_path = base_dir_name + "/lldbmacros/xnu.py"
91 xnu_load_cmd = "command script import \"%s\"" % xnu_debug_path
92 disable_optimization_warnings_cmd = "settings set target.process.optimization-warnings false"
93
94 source_map_cmd = ""
95 try:
96 source_map_cmd = GetSourcePathSettings(base_dir_name, GetSymbolsFilePathFromModule(debugger.GetTargetAtIndex(0).modules[0]) )
97 except Exception as e:
98 pass
99 if debug_session_enabled :
100 if len(prev_os_plugin) > 0:
101 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)
102 else:
103 print(osplugin_cmd)
104 debugger.HandleCommand(osplugin_cmd)
105 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)
106 else:
107 print(osplugin_cmd)
108 debugger.HandleCommand(osplugin_cmd)
109 print(whitelist_trap_cmd)
110 debugger.HandleCommand(whitelist_trap_cmd)
111 print(xnu_load_cmd)
112 debugger.HandleCommand(xnu_load_cmd)
113 print(disable_optimization_warnings_cmd)
114 debugger.HandleCommand(disable_optimization_warnings_cmd)
115 if source_map_cmd:
116 print(source_map_cmd)
117 debugger.HandleCommand(source_map_cmd)
118
119 load_kexts = True
120 if "XNU_LLDBMACROS_NOBUILTINKEXTS" in os.environ and len(os.environ['XNU_LLDBMACROS_NOBUILTINKEXTS']) > 0:
121 load_kexts = False
122 builtinkexts_path = os.path.join(os.path.dirname(self_path), "lldbmacros", "builtinkexts")
123 if os.access(builtinkexts_path, os.F_OK):
124 kexts = os.listdir(builtinkexts_path)
125 if len(kexts) > 0:
126 print("\nBuiltin kexts: %s\n" % kexts)
127 if load_kexts == False:
128 print("XNU_LLDBMACROS_NOBUILTINKEXTS is set, not loading:\n")
129 for kextdir in kexts:
130 script = os.path.join(builtinkexts_path, kextdir, kextdir.split('.')[-1] + ".py")
131 import_kext_cmd = "command script import \"%s\"" % script
132 print("%s" % import_kext_cmd)
133 if load_kexts:
134 debugger.HandleCommand(import_kext_cmd)
135
136 print("\n")
137