4 def GetSettingsValues(debugger
, setting_variable_name
):
5 """ Queries the lldb internal settings
7 debugger : lldb.SBDebugger instance
8 setting_variable_name: str - string name of the setting(eg prompt)
10 [] : Array of strings. Empty array if setting is not found/set
13 settings_val_list
= debugger
.GetInternalVariableValue(setting_variable_name
, debugger
.GetInstanceName())
14 for s
in settings_val_list
:
18 def GetSymbolsFilePathFromModule(m
):
19 """ Get a file path from a module.
20 params: m - lldb.target.module
22 str : path to first file based symbol. Note this might be dir path inside sources.
26 return os
.path
.dirname(str(s
.name
))
29 def GetSourcePathSettings(binary_path
, symbols_path
):
30 """ Parse the binary path and symbols_path to find if source-map setting is applicable
32 binary_path: str path of the kernel module
33 symbols_path: str path of the symbols stored in binary. Use
35 str : string command to set the source-map setting.
38 train_re
= re
.compile(r
"dsyms/([a-zA-Z]+)/")
39 _t_arr
= train_re
.findall(binary_path
)
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
)
53 srcpath
= "".join(_t_arr
[0])
54 projpath
= _t_arr
[0][-1]
58 new_path
= new_path
+ os
.path
.sep
+ projpath
59 cmd
= "settings append target.source-map {} {}"
60 retval
= cmd
.format(srcpath
, new_path
)
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"
84 source_map_cmd
= GetSourcePathSettings(base_dir_name
, GetSymbolsFilePathFromModule(debugger
.GetTargetAtIndex(0).modules
[0]) )
85 except Exception as e
:
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
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
96 debugger
.HandleCommand(osplugin_cmd
)
97 print whitelist_trap_cmd
98 debugger
.HandleCommand(whitelist_trap_cmd
)
100 debugger
.HandleCommand(xnu_load_cmd
)
101 print disable_optimization_warnings_cmd
102 debugger
.HandleCommand(disable_optimization_warnings_cmd
)
105 debugger
.HandleCommand(source_map_cmd
)
108 if "XNU_LLDBMACROS_NOBUILTINKEXTS" in os
.environ
and len(os
.environ
['XNU_LLDBMACROS_NOBUILTINKEXTS']) > 0:
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
)
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
122 debugger
.HandleCommand(import_kext_cmd
)