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