diff options
| author | Josh Ferrell <josh@vector35.com> | 2024-08-15 22:48:59 -0400 |
|---|---|---|
| committer | Josh Ferrell <josh@vector35.com> | 2024-08-16 16:29:37 -0400 |
| commit | de9584d8710144e352dbf09c5c2d1d46751d2f20 (patch) | |
| tree | 0173e78f0884bbe962c3da4f3f3c4be7a7755588 /rust/examples | |
| parent | 086342aedfa97ff207d0c8e1993bf0ebff8a519a (diff) | |
Support for automatic loading of <filename>.debug and <filename>.dSYM
Diffstat (limited to 'rust/examples')
| -rw-r--r-- | rust/examples/dwarf/dwarf_import/src/helpers.rs | 60 | ||||
| -rw-r--r-- | rust/examples/dwarf/dwarf_import/src/lib.rs | 40 |
2 files changed, 93 insertions, 7 deletions
diff --git a/rust/examples/dwarf/dwarf_import/src/helpers.rs b/rust/examples/dwarf/dwarf_import/src/helpers.rs index 7127b2b8..691c5751 100644 --- a/rust/examples/dwarf/dwarf_import/src/helpers.rs +++ b/rust/examples/dwarf/dwarf_import/src/helpers.rs @@ -478,8 +478,14 @@ pub(crate) fn download_debug_info(build_id: &String, view: &BinaryView) -> Resul } -pub(crate) fn find_local_debug_file(build_id: &String, view: &BinaryView) -> Option<String> { +pub(crate) fn find_local_debug_file_for_build_id(build_id: &String, view: &BinaryView) -> Option<String> { let settings = Settings::new(""); + let debug_dirs_enabled = settings.get_bool("analysis.debugInfo.enableDebugDirectories", Some(view), None); + + if !debug_dirs_enabled { + return None; + } + let debug_info_paths = settings.get_string_list("analysis.debugInfo.debugDirectories", Some(view), None); if debug_info_paths.is_empty() { @@ -518,7 +524,7 @@ pub(crate) fn find_local_debug_file(build_id: &String, view: &BinaryView) -> Opt pub(crate) fn load_debug_info_for_build_id(build_id: &String, view: &BinaryView) -> (Option<Ref<BinaryView>>, bool) { - if let Some(debug_file_path) = find_local_debug_file(build_id, view) { + if let Some(debug_file_path) = find_local_debug_file_for_build_id(build_id, view) { return ( binaryninja::load_with_options( @@ -537,3 +543,53 @@ pub(crate) fn load_debug_info_for_build_id(build_id: &String, view: &BinaryView) } (None, false) } + + +pub(crate) fn find_sibling_debug_file(view: &BinaryView) -> Option<String> { + let settings = Settings::new(""); + let load_sibling_debug = settings.get_bool("analysis.debugInfo.loadSiblingDebugFiles", Some(view), None); + + if !load_sibling_debug { + return None; + } + + let filename = view.file().filename().to_string(); + + let debug_file = PathBuf::from(format!("{}.debug", filename)); + let dsym_folder = PathBuf::from(format!("{}.dSYM/", filename)); + if debug_file.exists() && debug_file.is_file() { + return Some(debug_file.to_string_lossy().to_string()); + } + + if dsym_folder.exists() && dsym_folder.is_dir() { + let dsym_file = dsym_folder + .join("Contents/Resources/DWARF/") + .join(filename); // TODO: should this just pull any file out? Can there be multiple files? + if dsym_file.exists() { + return Some(dsym_file.to_string_lossy().to_string()); + } + } + + None +} + + +pub(crate) fn load_sibling_debug_file(view: &BinaryView) -> (Option<Ref<BinaryView>>, bool) { + let Some(debug_file) = find_sibling_debug_file(view) else { + return (None, false); + }; + + let load_settings = match view.default_platform() { + Some(plat) => format!("{{\"analysis.debugInfo.internal\": false, \"loader.platform\": \"{}\"}}", plat.name()), + None => "{\"analysis.debugInfo.internal\": false}".to_string() + }; + + ( + binaryninja::load_with_options( + debug_file, + false, + Some(load_settings) + ), + false + ) +} diff --git a/rust/examples/dwarf/dwarf_import/src/lib.rs b/rust/examples/dwarf/dwarf_import/src/lib.rs index 78c99123..98153398 100644 --- a/rust/examples/dwarf/dwarf_import/src/lib.rs +++ b/rust/examples/dwarf/dwarf_import/src/lib.rs @@ -459,9 +459,14 @@ impl CustomDebugInfoParser for DWARFParser { } if dwarfreader::has_build_id_section(view) { if let Ok(build_id) = get_build_id(view) { - return helpers::find_local_debug_file(&build_id, view).is_some(); + if helpers::find_local_debug_file_for_build_id(&build_id, view).is_some() { + return true; + } } } + if helpers::find_sibling_debug_file(view).is_some() { + return true; + } false } @@ -473,7 +478,10 @@ impl CustomDebugInfoParser for DWARFParser { progress: Box<dyn Fn(usize, usize) -> Result<(), ()>>, ) -> bool { let (external_file, close_external) = if !dwarfreader::is_valid(bv) { - if let Ok(build_id) = get_build_id(bv) { + if let (Some(debug_view), x) = helpers::load_sibling_debug_file(bv) { + (Some(debug_view), x) + } + else if let Ok(build_id) = get_build_id(bv) { helpers::load_debug_info_for_build_id(&build_id, bv) } else { @@ -529,7 +537,7 @@ pub extern "C" fn CorePluginInit() -> bool { "title" : "Enable Debuginfod Support", "type" : "boolean", "default" : false, - "description" : "Enable using debuginfod servers to fetch debug info for files with a .note.gnu.build-id section.", + "description" : "Enable using Debuginfod servers to fetch DWARF debug info for files with a .note.gnu.build-id section.", "ignore" : [] }"#, ); @@ -541,7 +549,18 @@ pub extern "C" fn CorePluginInit() -> bool { "type" : "array", "elementType" : "string", "default" : [], - "description" : "Servers to use for fetching debug info for files with a .note.gnu.build-id section.", + "description" : "Servers to use for fetching DWARF debug info for files with a .note.gnu.build-id section.", + "ignore" : [] + }"#, + ); + + settings.register_setting_json( + "analysis.debugInfo.enableDebugDirectories", + r#"{ + "title" : "Enable Debug File Directories", + "type" : "boolean", + "default" : true, + "description" : "Enable searching local debug directories for DWARF debug info.", "ignore" : [] }"#, ); @@ -553,7 +572,18 @@ pub extern "C" fn CorePluginInit() -> bool { "type" : "array", "elementType" : "string", "default" : [], - "description" : "Paths to folder containing debug info stored by build id.", + "description" : "Paths to folder containing DWARF debug info stored by build id.", + "ignore" : [] + }"#, + ); + + settings.register_setting_json( + "analysis.debugInfo.loadSiblingDebugFiles", + r#"{ + "title" : "Enable Loading of Sibling Debug Files", + "type" : "boolean", + "default" : true, + "description" : "Enable automatic loading of X.debug and X.dSYM files next to a file named X.", "ignore" : [] }"#, ); |
