|
| 1 | +//! The `llvm-dwarfdump` utility helper library. |
| 2 | +
|
| 3 | +use std::{ |
| 4 | + path::{Path, PathBuf}, |
| 5 | + process::{Command, Stdio}, |
| 6 | +}; |
| 7 | + |
| 8 | +pub static EXECUTABLE: &str = "llvm-dwarfdump"; |
| 9 | +pub static DEBUG_LINES_ARGUMENTS: [&str; 1] = ["--debug-line"]; |
| 10 | +pub static SOURCE_FILE_ARGUMENTS: [&str; 1] = ["--show-sources"]; |
| 11 | + |
| 12 | +/// Calls the `llvm-dwarfdump` tool to extract debug line information |
| 13 | +/// from the shared object at `path`. Returns the output. |
| 14 | +/// |
| 15 | +/// Provide `Some(dwarfdump_exectuable)` to override the default executable. |
| 16 | +pub fn debug_lines( |
| 17 | + shared_object: &Path, |
| 18 | + dwarfdump_executable: &Option<PathBuf>, |
| 19 | +) -> anyhow::Result<String> { |
| 20 | + dwarfdump(shared_object, dwarfdump_executable, &DEBUG_LINES_ARGUMENTS) |
| 21 | +} |
| 22 | + |
| 23 | +/// Calls the `llvm-dwarfdump` tool to extract the source file name. |
| 24 | +/// Returns the source file path. |
| 25 | +/// |
| 26 | +/// Provide `Some(dwarfdump_exectuable)` to override the default executable. |
| 27 | +pub fn source_file( |
| 28 | + shared_object: &Path, |
| 29 | + dwarfdump_executable: &Option<PathBuf>, |
| 30 | +) -> anyhow::Result<PathBuf> { |
| 31 | + let output = dwarfdump(shared_object, dwarfdump_executable, &SOURCE_FILE_ARGUMENTS)?; |
| 32 | + Ok(output.trim().into()) |
| 33 | +} |
| 34 | + |
| 35 | +/// The internal `llvm-dwarfdump` helper function. |
| 36 | +fn dwarfdump( |
| 37 | + shared_object: &Path, |
| 38 | + dwarfdump_executable: &Option<PathBuf>, |
| 39 | + arguments: &[&str], |
| 40 | +) -> anyhow::Result<String> { |
| 41 | + let executable = dwarfdump_executable |
| 42 | + .to_owned() |
| 43 | + .unwrap_or_else(|| PathBuf::from(EXECUTABLE)); |
| 44 | + |
| 45 | + let output = Command::new(executable) |
| 46 | + .args(arguments) |
| 47 | + .arg(shared_object) |
| 48 | + .stdin(Stdio::null()) |
| 49 | + .stdout(Stdio::piped()) |
| 50 | + .stderr(Stdio::piped()) |
| 51 | + .spawn()? |
| 52 | + .wait_with_output()?; |
| 53 | + |
| 54 | + if !output.status.success() { |
| 55 | + anyhow::bail!(String::from_utf8_lossy(&output.stderr).to_string()); |
| 56 | + } |
| 57 | + |
| 58 | + Ok(String::from_utf8_lossy(&output.stdout).to_string()) |
| 59 | +} |
0 commit comments