Skip to content

Commit

Permalink
Refactor set_breakpoint
Browse files Browse the repository at this point in the history
This commit breakup the set_breakpoint into multiple function.

First, by separating the logic to get the address from the one which put the breakpoint.
And second, by putting the logic for parsing the address and the symbols into their own function.

Those change will help to add line to addr functionality in a later commit.
  • Loading branch information
FlyingCanoe committed Aug 2, 2021
1 parent fb8f8d6 commit 34ac70b
Showing 1 changed file with 32 additions and 24 deletions.
56 changes: 32 additions & 24 deletions examples/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,11 @@ mod example {
context.remote = None;
}
ReplCommand::Kill(()) => println!("{:?}", context.remote()?.kill()?),
ReplCommand::Breakpoint(location) => set_breakpoint(context, &location)?,
ReplCommand::Breakpoint(location) => {
for addr in parse_breakpoint(context, &location)? {
set_breakpoint(context, addr)?
}
}
ReplCommand::Stepi(()) => {
println!("{:?}", context.remote()?.step()?);
return print_source_for_top_of_stack_symbol(context, 3);
Expand Down Expand Up @@ -389,36 +393,40 @@ mod example {
Ok(())
}

fn set_breakpoint(context: &mut Context, location: &str) -> CrabResult<()> {
fn parse_breakpoint(context: &mut Context, location: &str) -> CrabResult<Vec<usize>> {
context.load_debuginfo_if_necessary()?;

if let Ok(addr) = {
usize::from_str_radix(&location, 10)
.map(|addr| addr as usize)
.map_err(|e| Box::new(e))
.or_else(|_e| {
if location.starts_with("0x") {
let raw_num = location.trim_start_matches("0x");
usize::from_str_radix(raw_num, 16)
.map(|addr| addr as usize)
.map_err(|_e| Box::new(format!("Invalid address format.")))
} else {
context
.debuginfo()
.get_symbol_address(&location)
.ok_or(Box::new(format!("No such symbol {}", location)))
}
})
} {
context.mut_remote()?.set_breakpoint(addr)?;
if let Some(addr) = parse_address(location).or(parse_symbol(&location, context)) {
Ok(vec![addr])
} else {
Err(format!(
"Breakpoints must be set on a symbol or at a given address. For example `b main` or `b 0x0000555555559394` or even `b 93824992252820`"
))?
"Breakpoints must be set on a symbol or at a given address. For example `b main` or `b 0x0000555555559394` or even `b 93824992252820`"
))?
}
}

fn set_breakpoint(context: &mut Context, addr: usize) -> CrabResult<()> {
context.mut_remote()?.set_breakpoint(addr)?;
Ok(())
}

fn parse_address(location: &str) -> Option<usize> {
if let Ok(addr) = usize::from_str_radix(&location, 10) {
return Some(addr);
} else {
if location.starts_with("0x") {
let raw_num = location.trim_start_matches("0x");
if let Ok(addr) = usize::from_str_radix(raw_num, 16) {
return Some(addr);
}
}
}
None
}

fn parse_symbol(location: &str, context: &mut Context) -> Option<usize> {
context.debuginfo().get_symbol_address(&location)
}

fn show_backtrace(context: &mut Context, bt_type: &BacktraceType) -> CrabResult<()> {
let call_stack: Vec<_> = get_call_stack(context, bt_type)?;
for func in call_stack {
Expand Down

0 comments on commit 34ac70b

Please sign in to comment.