Skip to content

Commit

Permalink
optimize argument
Browse files Browse the repository at this point in the history
  • Loading branch information
everettjf committed Jan 5, 2022
1 parent 22b87d8 commit 170176b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "atosl"
version = "0.1.8"
version = "0.1.9"
edition = "2021"
description = "🦀️atos for linux by rust - A partial replacement for Apple's atos tool for converting addresses within a binary file to symbols."
license = "MIT"
Expand Down
46 changes: 23 additions & 23 deletions src/atosl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn print_addresses(
object_path: &str,
load_address: u64,
addresses: Vec<u64>,
debug_mode: bool,
verbose: bool,
file_offset_type: bool,
) -> Result<(), anyhow::Error> {
let file = fs::File::open(&object_path)?;
Expand All @@ -27,27 +27,27 @@ pub fn print_addresses(
.ok_or(anyhow!("file name error(to_str)"))?;

if is_object_dwarf(&object) {
if debug_mode {
if verbose {
println!("dwarf");
}
dwarf_symbolize_addresses(
&object,
object_filename,
load_address,
addresses,
debug_mode,
verbose,
file_offset_type,
)
} else {
if debug_mode {
if verbose {
println!("symbols");
}
symbol_symbolize_addresses(
&object,
object_filename,
load_address,
addresses,
debug_mode,
verbose,
file_offset_type,
)
}
Expand All @@ -66,7 +66,7 @@ fn symbol_symbolize_addresses(
object_filename: &str,
load_address: u64,
addresses: Vec<u64>,
debug_mode: bool,
verbose: bool,
file_offset_type: bool,
) -> Result<(), anyhow::Error> {
// find vmaddr for __TEXT segment
Expand All @@ -82,7 +82,7 @@ fn symbol_symbolize_addresses(
}

for address in addresses {
if debug_mode {
if verbose {
println!("---------------------------------------------");
println!("BEGIN ADDRESS {} | {:016x}", address, address);
}
Expand All @@ -93,18 +93,18 @@ fn symbol_symbolize_addresses(
load_address,
address,
text_vmaddr,
debug_mode,
verbose,
file_offset_type,
);
if debug_mode {
if verbose {
println!("RESULT:")
}
match symbol_result {
Ok(symbol) => println!("{}", symbol),
Err(err) => println!("N/A - {}", err),
};

if debug_mode {
if verbose {
println!("END ADDRESS {} | {:016x}", address, address);
}
}
Expand All @@ -117,7 +117,7 @@ fn symbol_symbolize_address(
load_address: u64,
address: u64,
text_vmaddr: u64,
_debug_mode: bool,
_verbose: bool,
file_offset_type: bool,
) -> Result<String, anyhow::Error> {
let search_address: u64 = if file_offset_type {
Expand Down Expand Up @@ -146,7 +146,7 @@ fn dwarf_symbolize_addresses(
object_filename: &str,
load_address: u64,
addresses: Vec<u64>,
debug_mode: bool,
verbose: bool,
file_offset_type: bool,
) -> Result<(), anyhow::Error> {
let endian = if object.is_little_endian() {
Expand Down Expand Up @@ -178,7 +178,7 @@ fn dwarf_symbolize_addresses(
}

for address in addresses {
if debug_mode {
if verbose {
println!("---------------------------------------------");
println!("BEGIN ADDRESS {} | {:016x}", address, address);
}
Expand All @@ -189,10 +189,10 @@ fn dwarf_symbolize_addresses(
load_address,
address,
text_vmaddr,
debug_mode,
verbose,
file_offset_type,
);
if debug_mode {
if verbose {
println!("RESULT:")
}
match symbol_result {
Expand All @@ -205,7 +205,7 @@ fn dwarf_symbolize_addresses(
load_address,
address,
text_vmaddr,
debug_mode,
verbose,
file_offset_type,
);
match symbol_result {
Expand All @@ -215,7 +215,7 @@ fn dwarf_symbolize_addresses(
}
};

if debug_mode {
if verbose {
println!("END ADDRESS {} | {:016x}", address, address);
}
}
Expand All @@ -228,7 +228,7 @@ fn dwarf_symbolize_address(
load_address: u64,
address: u64,
text_vmaddr: u64,
debug_mode: bool,
verbose: bool,
file_offset_type: bool,
) -> Result<String, anyhow::Error> {
let search_address: u64 = if file_offset_type {
Expand Down Expand Up @@ -268,7 +268,7 @@ fn dwarf_symbolize_address(
// catch header
let debug_info_header = dwarf.debug_info.header_from_offset(debug_info_offset)?;

if debug_mode {
if verbose {
println!("got debug info header 0x{:016x}", debug_info_offset.0);
};

Expand All @@ -287,7 +287,7 @@ fn dwarf_symbolize_address(
low_pc = Some(lowpc_val);
let high_pc_value = entry.attr_value(gimli::DW_AT_high_pc);

if debug_mode {
if verbose {
println!("high pc value : {:?}", high_pc_value);
}

Expand All @@ -300,7 +300,7 @@ fn dwarf_symbolize_address(
}
}

if debug_mode {
if verbose {
println!("low pc = {:?}, high pc = {:?}", low_pc, high_pc)
}

Expand All @@ -317,7 +317,7 @@ fn dwarf_symbolize_address(
}
}
}
if debug_mode {
if verbose {
println!("found_symbol_name = {:?}", found_symbol_name);
};

Expand Down Expand Up @@ -358,7 +358,7 @@ fn dwarf_symbolize_address(
}
}

if debug_mode {
if verbose {
println!(
"found_file_name = {:?} found_line = {:?}",
found_file_name, found_line
Expand Down
16 changes: 7 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ struct Args {
#[clap(parse(try_from_str = parse_address_string))]
addresses: Vec<u64>,

/// Enable debug mode with extra output
#[clap(short, default_value_t = 0)]
debug: u8,
/// Enable verbose mode with extra output
#[clap(short)]
verbose: bool,

/// Addresses are file offsets (ignore vmaddr in __TEXT or other executable segment)
#[clap(short, default_value_t = 0)]
file_offset_type: u8,
#[clap(short)]
file_offset_type: bool,
}

fn parse_address_string(address: &str) -> Result<u64, anyhow::Error> {
Expand All @@ -48,15 +48,13 @@ fn parse_address_string(address: &str) -> Result<u64, anyhow::Error> {
fn main() {
let args = Args::parse();
let object_path = args.object_path.into_os_string().into_string().unwrap();
let debug_mode = args.debug != 0;
let file_offset_type = args.file_offset_type != 0;

let result = atosl::print_addresses(
&object_path,
args.load_address,
args.addresses,
debug_mode,
file_offset_type,
args.verbose,
args.file_offset_type,
);
match result {
Ok(..) => {}
Expand Down

0 comments on commit 170176b

Please sign in to comment.