Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

defmt-print: Log if malformed frame gets skipped #610

Merged
merged 6 commits into from
Oct 21, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 29 additions & 20 deletions print/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,36 @@ struct Opts {
#[structopt(short, parse(from_os_str), required_unless_one(&["version"]))]
elf: Option<PathBuf>,

#[structopt(long)]
show_skipped_frames: bool,

#[structopt(short, long)]
verbose: bool,
justahero marked this conversation as resolved.
Show resolved Hide resolved

#[structopt(short = "V", long)]
version: bool,
// may want to add this later
// #[structopt(short, long)]
// verbose: bool,
// TODO add file path argument; always use stdin for now
}

const READ_BUFFER_SIZE: usize = 1024;

fn main() -> anyhow::Result<()> {
let opts: Opts = Opts::from_args();

if opts.version {
let Opts {
elf,
show_skipped_frames,
verbose,
version,
} = Opts::from_args();

if version {
return print_version();
}

let verbose = false;
defmt_decoder::log::init_logger(verbose, |metadata| {
// We display *all* defmt frames, but nothing else.
defmt_decoder::log::is_defmt_frame(metadata)
defmt_decoder::log::init_logger(verbose, move |metadata| match verbose {
false => defmt_decoder::log::is_defmt_frame(metadata), // We display *all* defmt frames, but nothing else.
true => true, // We display *all* frames.
});

let bytes = fs::read(&opts.elf.unwrap())?;
let bytes = fs::read(&elf.unwrap())?;

let table = Table::parse(&bytes)?.ok_or_else(|| anyhow!(".defmt data not found"))?;
let locs = table.get_locations(&bytes)?;
Expand Down Expand Up @@ -71,7 +77,13 @@ fn main() -> anyhow::Result<()> {
// if recovery is impossible, abort
false => return Err(DecodeError::Malformed.into()),
// if recovery is possible, skip the current frame and continue with new data
true => continue,
true => {
justahero marked this conversation as resolved.
Show resolved Hide resolved
if show_skipped_frames || verbose {
println!("(HOST) malformed frame skipped");
println!("└─ {} @ {}:{}", env!("CARGO_PKG_NAME"), file!(), line!());
}
continue;
}
},
}
}
Expand All @@ -92,13 +104,10 @@ fn location_info(locs: &Option<Locations>, frame: &Frame, current_dir: &Path) ->
let loc = locs.as_ref().map(|locs| &locs[&frame.index()]);

if let Some(loc) = loc {
let relpath = if let Ok(relpath) = loc.file.strip_prefix(&current_dir) {
relpath
} else {
// not relative; use full path
&loc.file
};
file = Some(relpath.display().to_string());
// try to get the relative path, else the full one
let path = loc.file.strip_prefix(&current_dir).unwrap_or(&loc.file);

file = Some(path.display().to_string());
line = Some(loc.line as u32);
mod_path = Some(loc.module.clone());
}
Expand Down