Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
benediktwerner committed Nov 25, 2019
1 parent 4eb65d1 commit 37fb123
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 214 deletions.
41 changes: 7 additions & 34 deletions wasmdbg-cli/src/cmds/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,13 @@ fn cmd_nearpc(dbg: &mut Debugger, args: &[CmdArg]) -> CmdResult {
None => (DISASSEMBLY_DEFAULT_MAX_LINES, 2),
};
let ip = dbg.get_vm()?.ip();
let code = dbg
.get_file()?
.module()
.get_func(ip.func_index)
.unwrap()
.instructions();
let code = dbg.get_file()?.module().get_func(ip.func_index).unwrap().instructions();
if forward + back >= code.len() as u32 {
print_disassembly(dbg, CodePosition::new(ip.func_index, 0), None)
} else {
let start = ip.instr_index - back.min(ip.instr_index);
let end = (ip.instr_index + forward).min(code.len() as u32);
print_disassembly(
dbg,
CodePosition::new(ip.func_index, start),
Some(end - start),
)
print_disassembly(dbg, CodePosition::new(ip.func_index, start), Some(end - start))
}
}

Expand Down Expand Up @@ -183,18 +174,8 @@ fn cmd_globals(dbg: &mut Debugger, _args: &[CmdArg]) -> CmdResult {
println!("<no locals>");
} else {
let max_index_len = globals.len().to_string().len();
for (i, (val, global)) in globals
.iter()
.zip(dbg.get_file()?.module().globals())
.enumerate()
{
println!(
"Global {:>3$}: {:15} : {}",
i,
global.name(),
val,
max_index_len
);
for (i, (val, global)) in globals.iter().zip(dbg.get_file()?.module().globals()).enumerate() {
println!("Global {:>3$}: {:15} : {}", i, global.name(), val, max_index_len);
}
}
Ok(())
Expand All @@ -214,10 +195,7 @@ fn print_disassembly(dbg: &Debugger, start: CodePosition, len: Option<u32>) -> C
});
let code = match dbg.get_file()?.module().get_func(start.func_index) {
Some(func) => {
ensure!(
!func.is_imported(),
"Cannot show disassembly of imported function"
);
ensure!(!func.is_imported(), "Cannot show disassembly of imported function");
let start = start.instr_index as usize;
if let Some(len) = len {
let end = start + len as usize;
Expand All @@ -235,9 +213,7 @@ fn print_disassembly(dbg: &Debugger, start: CodePosition, len: Option<u32>) -> C
let instr_index = start.instr_index + i as u32;
let addr_str = format!("{}:{:>02$}", start.func_index, instr_index, max_index_len);
let breakpoint = match breakpoints {
Some(ref breakpoints) => {
breakpoints.find_code(CodePosition::new(start.func_index, instr_index))
}
Some(ref breakpoints) => breakpoints.find_code(CodePosition::new(start.func_index, instr_index)),
None => None,
};
let breakpoint_str = match breakpoint {
Expand All @@ -261,10 +237,7 @@ fn print_disassembly(dbg: &Debugger, start: CodePosition, len: Option<u32>) -> C
indent
);
} else {
println!(
" {}{} {: >4$}{}",
breakpoint_str, addr_str, "", instr_str, indent
);
println!(" {}{} {: >4$}{}", breakpoint_str, addr_str, "", instr_str, indent);
}
match instr {
Instruction::Block(_) => indent += 1,
Expand Down
26 changes: 16 additions & 10 deletions wasmdbg-cli/src/cmds/execution.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use wasmdbg::{Value,Breakpoint, BreakpointTrigger};
use wasmdbg::vm::{CodePosition, Trap};
use wasmdbg::Debugger;
use wasmdbg::{Breakpoint, BreakpointTrigger, Value};

use super::context;
use super::{CmdArg, CmdArgOptionExt, CmdResult, Command, Commands};
Expand Down Expand Up @@ -33,12 +33,22 @@ pub fn add_cmds(commands: &mut Commands) {
.requires_file()
);
commands.add(
Command::new_subcommand("watch")
Command::new_subcommand("watch")
.description("Set a watchpoint")
.requires_file()
.add_subcommand(Command::new("memory", cmd_watch_memory).takes_args("ADDR:addr [read|write]").description("Watch a memory location").help("Watch the memory at address ADDR and pause execution when it's value is read/written."))
.add_subcommand(Command::new("global", cmd_watch_global).takes_args("INDEX:u32 [read|write]").description("Watch a global").help("Watch the global with index INDEX and pause execution when it's value is read/written."))
);
.add_subcommand(
Command::new("memory", cmd_watch_memory)
.takes_args("ADDR:addr [read|write]")
.description("Watch a memory location")
.help("Watch the memory at address ADDR and pause execution when it's value is read/written."),
)
.add_subcommand(
Command::new("global", cmd_watch_global)
.takes_args("INDEX:u32 [read|write]")
.description("Watch a global")
.help("Watch the global with index INDEX and pause execution when it's value is read/written."),
),
);
commands.add(
Command::new("delete", cmd_delete)
.description("Delete a breakpoint")
Expand Down Expand Up @@ -115,11 +125,7 @@ fn cmd_call(dbg: &mut Debugger, args: &[CmdArg]) -> CmdResult {
if let Some(arg_parsed) = Value::from_str(&arg.as_string(), *value_type) {
args_parsed.push(arg_parsed);
} else {
bail!(
"Failed to parse argument \"{}\" as {}",
arg.as_string(),
value_type
);
bail!("Failed to parse argument \"{}\" as {}", arg.as_string(), value_type);
}
}

Expand Down
28 changes: 6 additions & 22 deletions wasmdbg-cli/src/cmds/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,40 +41,28 @@ impl CmdArg {
match self {
CmdArg::Str(val) => val.to_string(),
CmdArg::Const(val) => val.to_string(),
_ => panic!(
"Parsed arg has wrong type. Expected str, found {}",
self.type_str()
),
_ => panic!("Parsed arg has wrong type. Expected str, found {}", self.type_str()),
}
}

fn as_const(&self) -> &'static str {
match self {
CmdArg::Const(val) => val,
_ => panic!(
"Parsed arg has wrong type. Expected const, found {}",
self.type_str()
),
_ => panic!("Parsed arg has wrong type. Expected const, found {}", self.type_str()),
}
}

fn as_u32(&self) -> u32 {
match self {
CmdArg::U32(val) => *val,
_ => panic!(
"Parsed arg has wrong type. Expected u32, found {}",
self.type_str()
),
_ => panic!("Parsed arg has wrong type. Expected u32, found {}", self.type_str()),
}
}

fn as_usize(&self) -> usize {
match self {
CmdArg::Usize(val) => *val,
_ => panic!(
"Parsed arg has wrong type. Expected usize, found {}",
self.type_str()
),
_ => panic!("Parsed arg has wrong type. Expected usize, found {}", self.type_str()),
}
}
}
Expand Down Expand Up @@ -266,9 +254,7 @@ pub struct Commands {

impl Commands {
const fn new() -> Commands {
Commands {
commands: Vec::new(),
}
Commands { commands: Vec::new() }
}

pub fn all() -> Commands {
Expand Down Expand Up @@ -416,9 +402,7 @@ impl CommandHandler {
}
println!(
"\n{}",
cmd.help
.or(cmd.description)
.unwrap_or("No help for this command")
cmd.help.or(cmd.description).unwrap_or("No help for this command")
);
}
None => println!("Unknown command: \"{}\". Try \"help\".", cmd_name),
Expand Down
21 changes: 5 additions & 16 deletions wasmdbg-cli/src/cmds/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,9 @@ pub trait ParseCmdArg {
}

impl ParseCmdArg for CmdArgType {
fn parse<'a>(&self, line: &'a str) -> Result<(&'a str, Vec<CmdArg>), Error> {
fn parse<'a>(&self, line: &'a str) -> anyhow::Result<(&'a str, Vec<CmdArg>)> {
match self {
CmdArgType::Str(_) | CmdArgType::Path(_) => {
wrap(next_arg(line), |a| Ok(CmdArg::Str(a.to_string())))
}
CmdArgType::Str(_) | CmdArgType::Path(_) => wrap(next_arg(line), |a| Ok(CmdArg::Str(a.to_string()))),
CmdArgType::Line(_) => Ok(("", vec![CmdArg::Str(line.to_string())])),
CmdArgType::Fmt(_) => {
if let Some('/') = line.trim_start().chars().next() {
Expand All @@ -128,9 +126,7 @@ impl ParseCmdArg for CmdArgType {
}
CmdArgType::Usize(_) => wrap(next_arg(line), |a| Ok(CmdArg::Usize(a.parse()?))),
CmdArgType::U32(_) => wrap(next_arg(line), |a| Ok(CmdArg::U32(a.parse()?))),
CmdArgType::Addr(_) => wrap(next_arg(line), |a| {
Ok(CmdArg::U32(u32::from_str_with_radix(a)?))
}),
CmdArgType::Addr(_) => wrap(next_arg(line), |a| Ok(CmdArg::U32(u32::from_str_with_radix(a)?))),
CmdArgType::Const(val) => {
if line.trim_start().starts_with(*val) {
Ok((&line[val.len()..], vec![CmdArg::Const(val)]))
Expand Down Expand Up @@ -259,15 +255,8 @@ fn next_arg(line: &str) -> anyhow::Result<(&str, &str)> {
// }

fn parse_format(fmt_str: &str) -> anyhow::Result<(u32, u32, Format)> {
let count_str = fmt_str
.chars()
.take_while(|c| c.is_numeric())
.collect::<String>();
let count = if count_str.is_empty() {
1
} else {
count_str.parse()?
};
let count_str = fmt_str.chars().take_while(|c| c.is_numeric()).collect::<String>();
let count = if count_str.is_empty() { 1 } else { count_str.parse()? };
let mut size = 4;
let mut format = Format::Hex;
for c in fmt_str.chars().skip_while(|c| c.is_numeric()) {
Expand Down
2 changes: 0 additions & 2 deletions wasmdbg-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate anyhow;

use std::sync::Arc;
Expand Down
15 changes: 3 additions & 12 deletions wasmdbg-cli/src/readline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ fn get_history_file() -> String {

fn find_cmds<'a>(cmds: &'a Commands, prefix: &str) -> Vec<&'a Command> {
cmds.iter()
.filter(|cmd| {
cmd.name.starts_with(prefix)
|| cmd.aliases.iter().any(|&alias| alias.starts_with(prefix))
})
.filter(|cmd| cmd.name.starts_with(prefix) || cmd.aliases.iter().any(|&alias| alias.starts_with(prefix)))
.collect()
}

Expand All @@ -40,11 +37,7 @@ impl<Term: Terminal> Completer<Term> for MyCompleter {
}
}

fn complete<'a, I>(
cmds: &Commands,
curr_word: &'a str,
other_words: &mut I,
) -> Option<Vec<Completion>>
fn complete<'a, I>(cmds: &Commands, curr_word: &'a str, other_words: &mut I) -> Option<Vec<Completion>>
where
I: Iterator<Item = &'a str> + Clone,
{
Expand Down Expand Up @@ -175,9 +168,7 @@ impl Readline {
pub fn new(cmds: Arc<Commands>) -> Self {
let interface = Interface::new("wasmdbg").unwrap();
interface.set_completer(Arc::new(MyCompleter { cmds }));
interface
.set_prompt(&"wasmdbg> ".red().to_string())
.unwrap();
interface.set_prompt(&"wasmdbg> ".red().to_string()).unwrap();

if let Err(error) = interface.load_history(get_history_file()) {
if error.kind() != io::ErrorKind::NotFound {
Expand Down
5 changes: 1 addition & 4 deletions wasmdbg-cli/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ pub fn terminal_width() -> usize {

pub fn print_header(text: &str) {
let line_length = terminal_width() - text.len() - 8;
println!(
"{}",
format!("──[ {} ]──{:─<2$}", text, "", line_length).blue()
)
println!("{}", format!("──[ {} ]──{:─<2$}", text, "", line_length).blue())
}

pub fn print_line() {
Expand Down
6 changes: 1 addition & 5 deletions wasmdbg/src/breakpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,7 @@ impl Breakpoints {
}

pub fn find_memory(&self, start: u32, len: u32, write: bool) -> Option<u32> {
let watchpoints = if write {
&self.memory_write
} else {
&self.memory_read
};
let watchpoints = if write { &self.memory_write } else { &self.memory_read };
for &addr in watchpoints {
if start <= addr && addr < start + len {
for (index, breakpoint) in self {
Expand Down
11 changes: 2 additions & 9 deletions wasmdbg/src/debugger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ pub struct Debugger {

impl Debugger {
pub const fn new() -> Self {
Debugger {
file: None,
vm: None,
}
Debugger { file: None, vm: None }
}

pub fn file(&self) -> Option<&File> {
Expand Down Expand Up @@ -107,11 +104,7 @@ impl Debugger {
}

pub fn delete_breakpoint(&mut self, index: u32) -> DebuggerResult<bool> {
Ok(self
.get_file()?
.breakpoints()
.borrow_mut()
.delete_breakpoint(index))
Ok(self.get_file()?.breakpoints().borrow_mut().delete_breakpoint(index))
}

pub fn clear_breakpoints(&mut self) -> DebuggerResult<()> {
Expand Down
Loading

0 comments on commit 37fb123

Please sign in to comment.