|
12 | 12 | // See the License for the specific language governing permissions and
|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
| 15 | +use std::path::PathBuf; |
15 | 16 | use std::{
|
16 | 17 | collections::HashMap,
|
17 | 18 | ops::Deref,
|
@@ -303,9 +304,7 @@ pub(crate) fn get_expr_value<R: Reader<Offset = usize>>(
|
303 | 304 | }
|
304 | 305 |
|
305 | 306 |
|
306 |
| -pub(crate) fn download_debug_info(view: &BinaryView) -> Result<Ref<BinaryView>, String> { |
307 |
| - let settings = Settings::new(""); |
308 |
| - |
| 307 | +pub(crate) fn get_build_id(view: &BinaryView) -> Result<String, String> { |
309 | 308 | let mut build_id: Option<String> = None;
|
310 | 309 |
|
311 | 310 | if let Ok(raw_view) = view.raw_view() {
|
@@ -351,14 +350,24 @@ pub(crate) fn download_debug_info(view: &BinaryView) -> Result<Ref<BinaryView>,
|
351 | 350 | }
|
352 | 351 | }
|
353 | 352 |
|
354 |
| - if build_id.is_none() { |
355 |
| - return Err("Failed to get build id".to_string()); |
| 353 | + if let Some(x) = build_id { |
| 354 | + Ok(x) |
356 | 355 | }
|
| 356 | + else { |
| 357 | + Err("Failed to get build id".to_string()) |
| 358 | + } |
| 359 | +} |
| 360 | + |
| 361 | + |
| 362 | +pub(crate) fn download_debug_info(view: &BinaryView) -> Result<Ref<BinaryView>, String> { |
| 363 | + let settings = Settings::new(""); |
| 364 | + |
| 365 | + let build_id = get_build_id(view)?; |
357 | 366 |
|
358 | 367 | let debug_server_urls = settings.get_string_list("network.debuginfodServers", Some(view), None);
|
359 | 368 |
|
360 | 369 | for debug_server_url in debug_server_urls.iter() {
|
361 |
| - let artifact_url = format!("{}/buildid/{}/debuginfo", debug_server_url, build_id.as_ref().unwrap()); |
| 370 | + let artifact_url = format!("{}/buildid/{}/debuginfo", debug_server_url, build_id); |
362 | 371 |
|
363 | 372 | // Download from remote
|
364 | 373 | let (tx, rx) = mpsc::channel();
|
@@ -421,3 +430,47 @@ pub(crate) fn download_debug_info(view: &BinaryView) -> Result<Ref<BinaryView>,
|
421 | 430 | }
|
422 | 431 | return Err("Could not find a server with debug info for this file".to_string());
|
423 | 432 | }
|
| 433 | + |
| 434 | + |
| 435 | +pub(crate) fn load_debug_info_for_build_id(view: &BinaryView) -> Result<Option<Ref<BinaryView>>, String> { |
| 436 | + let settings = Settings::new(""); |
| 437 | + let debug_info_paths = settings.get_string_list("analysis.debugInfo.debugDirectories", Some(view), None); |
| 438 | + if debug_info_paths.is_empty() { |
| 439 | + return Ok(None) |
| 440 | + } |
| 441 | + |
| 442 | + for debug_info_path in debug_info_paths.into_iter() { |
| 443 | + if let Ok(path) = PathBuf::from_str(&debug_info_path.to_string()) |
| 444 | + { |
| 445 | + let build_id = get_build_id(view)?; |
| 446 | + let elf_path = path |
| 447 | + .join(&build_id[..2]) |
| 448 | + .join(&build_id[2..]) |
| 449 | + .join("elf"); |
| 450 | + |
| 451 | + let debug_ext_path = path |
| 452 | + .join(&build_id[..2]) |
| 453 | + .join(format!("{}.debug", &build_id[2..])); |
| 454 | + |
| 455 | + let options = "{\"analysis.debugInfo.internal\": false}"; |
| 456 | + let final_path = if debug_ext_path.exists() { |
| 457 | + debug_ext_path |
| 458 | + } |
| 459 | + else if elf_path.exists() { |
| 460 | + elf_path |
| 461 | + } |
| 462 | + else { |
| 463 | + // No paths exist |
| 464 | + return Ok(None) |
| 465 | + }; |
| 466 | + return Ok( |
| 467 | + binaryninja::load_with_options( |
| 468 | + final_path.to_string_lossy().to_string(), |
| 469 | + false, |
| 470 | + Some(options) |
| 471 | + ) |
| 472 | + ); |
| 473 | + } |
| 474 | + } |
| 475 | + Ok(None) |
| 476 | +} |
0 commit comments