1 from __future__
import absolute_import
2 from __future__
import print_function
7 PY3
= sys
.version_info
> (3,)
9 def GetSettingsValues(debugger
, setting_variable_name
):
10 """ Queries the lldb internal settings
12 debugger : lldb.SBDebugger instance
13 setting_variable_name: str - string name of the setting(eg prompt)
15 [] : Array of strings. Empty array if setting is not found/set
18 settings_val_list
= debugger
.GetInternalVariableValue(setting_variable_name
, debugger
.GetInstanceName())
19 for s
in settings_val_list
:
23 def GetSymbolsFilePathFromModule(m
):
24 """ Get a file path from a module.
25 params: m - lldb.target.module
27 str : path to first file based symbol. Note this might be dir path inside sources.
31 return os
.path
.dirname(str(s
.name
))
34 def GetSourcePathSettings(binary_path
, symbols_path
):
35 """ Parse the binary path and symbols_path to find if source-map setting is applicable
37 binary_path: str path of the kernel module
38 symbols_path: str path of the symbols stored in binary. Use
40 str : string command to set the source-map setting.
43 train_re
= re
.compile(r
"dsyms/([a-zA-Z]+)/")
44 _t_arr
= train_re
.findall(binary_path
)
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
)
58 srcpath
= "".join(_t_arr
[0])
59 projpath
= _t_arr
[0][-1]
63 new_path
= new_path
+ os
.path
.sep
+ projpath
64 cmd
= "settings append target.source-map {} {}"
65 retval
= cmd
.format(srcpath
, new_path
)
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'))
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")
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"
96 source_map_cmd
= GetSourcePathSettings(base_dir_name
, GetSymbolsFilePathFromModule(debugger
.GetTargetAtIndex(0).modules
[0]) )
97 except Exception as e
:
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
)
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
)
108 debugger
.HandleCommand(osplugin_cmd
)
109 print(whitelist_trap_cmd
)
110 debugger
.HandleCommand(whitelist_trap_cmd
)
112 debugger
.HandleCommand(xnu_load_cmd
)
113 print(disable_optimization_warnings_cmd
)
114 debugger
.HandleCommand(disable_optimization_warnings_cmd
)
116 print(source_map_cmd
)
117 debugger
.HandleCommand(source_map_cmd
)
120 if "XNU_LLDBMACROS_NOBUILTINKEXTS" in os
.environ
and len(os
.environ
['XNU_LLDBMACROS_NOBUILTINKEXTS']) > 0:
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
)
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
)
134 debugger
.HandleCommand(import_kext_cmd
)