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

Run Command refactoring #2295

Merged
merged 6 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Fixes from Review 2
  • Loading branch information
t-moe committed Mar 20, 2024
commit 43c788b66fe587ee5ba227f678bc6bb1327b36f4
24 changes: 16 additions & 8 deletions probe-rs/src/bin/probe-rs/cmd/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,29 @@ impl core::fmt::Display for ProfileMethod {

impl ProfileCmd {
pub fn run(self, lister: &Lister) -> anyhow::Result<()> {
let (mut session, probe_options) = self.run.probe_options.simple_attach(lister)?;

let loader = build_loader(&mut session, &self.run.path, self.run.format_options)?;

let bytes = std::fs::read(&self.run.path)?;
let (mut session, probe_options) = self
.run
.shared_options
.probe_options
.simple_attach(lister)?;

let loader = build_loader(
&mut session,
&self.run.shared_options.path,
self.run.shared_options.format_options,
)?;

let bytes = std::fs::read(&self.run.shared_options.path)?;
let symbols = Symbols::try_from(&bytes)?;

if self.flash {
run_flash_download(
&mut session,
Path::new(&self.run.path),
&self.run.download_options,
Path::new(&self.run.shared_options.path),
&self.run.shared_options.download_options,
&probe_options,
loader,
self.run.chip_erase,
self.run.shared_options.chip_erase,
)?;
}

Expand Down
64 changes: 32 additions & 32 deletions probe-rs/src/bin/probe-rs/cmd/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use normal_run_mode::*;

use std::io::Write;
use std::ops::Range;
use std::path::{Path, PathBuf};
use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
Expand All @@ -28,42 +28,41 @@ const RTT_RETRIES: usize = 10;

#[derive(clap::Parser)]
pub struct Cmd {
/// The path to the ELF file to flash and run
pub(crate) path: String,

/// Options only used when in normal run mode
#[clap(flatten)]
pub(crate) run_options: NormalRunOptions,

// ---- General Options ahead ----
/// Options shared by all run modes
#[clap(flatten)]
pub(crate) common_options: CommonOptions,
pub(crate) shared_options: SharedOptions,
}

#[derive(Debug, clap::Parser)]
pub struct SharedOptions {
#[clap(flatten)]
pub(crate) download_options: BinaryDownloadOptions,
pub(crate) probe_options: ProbeOptions,

#[clap(flatten)]
pub(crate) format_options: FormatOptions,

/// Whether to erase the entire chip before downloading
#[clap(long, help_heading = "DOWNLOAD CONFIGURATION")]
pub(crate) chip_erase: bool,
pub(crate) download_options: BinaryDownloadOptions,

#[clap(flatten)]
pub(crate) probe_options: ProbeOptions,
}
/// The path to the ELF file to flash and run
pub(crate) path: String,

// Options used for all run modes
#[derive(Debug, clap::Parser)]
pub struct CommonOptions {
/// Always print the stacktrace on ctrl + c.
#[clap(long)]
pub(crate) always_print_stacktrace: bool,

/// Whether to erase the entire chip before downloading
#[clap(long, help_heading = "DOWNLOAD CONFIGURATION")]
pub(crate) chip_erase: bool,

/// Suppress filename and line number information from the rtt log
#[clap(long)]
pub(crate) no_location: bool,

#[clap(flatten)]
pub(crate) format_options: FormatOptions,

/// The default format string to use for decoding defmt logs.
#[clap(long)]
pub(crate) log_format: Option<String>,
Expand All @@ -82,18 +81,19 @@ impl Cmd {
) -> Result<()> {
let run_mode = detect_run_mode(&self)?;

let (mut session, probe_options) = self.probe_options.simple_attach(lister)?;
let path = PathBuf::from(&self.path);
let (mut session, probe_options) =
self.shared_options.probe_options.simple_attach(lister)?;

if run_download {
let loader = build_loader(&mut session, &path, self.format_options)?;
let path = Path::new(&self.shared_options.path);
let loader = build_loader(&mut session, path, self.shared_options.format_options)?;
run_flash_download(
&mut session,
&path,
&self.download_options,
path,
&self.shared_options.download_options,
&probe_options,
loader,
self.chip_erase,
self.shared_options.chip_erase,
)?;
// reset the core to leave it in a consistent state after flashing
session
Expand All @@ -102,7 +102,7 @@ impl Cmd {
}

let memory_map = session.target().memory_map.clone();
let rtt_scan_regions = match self.common_options.rtt_scan_memory {
let rtt_scan_regions = match self.shared_options.rtt_scan_memory {
true => session.target().rtt_scan_regions.clone(),
false => Vec::new(),
};
Expand All @@ -112,11 +112,11 @@ impl Cmd {
RunLoop {
memory_map,
rtt_scan_regions,
path,
timestamp_offset,
always_print_stacktrace: self.common_options.always_print_stacktrace,
no_location: self.common_options.no_location,
log_format: self.common_options.log_format,
path: self.shared_options.path,
always_print_stacktrace: self.shared_options.always_print_stacktrace,
no_location: self.shared_options.no_location,
log_format: self.shared_options.log_format,
},
)?;

Expand All @@ -140,7 +140,7 @@ fn detect_run_mode(cmd: &Cmd) -> Result<Box<dyn RunMode>, anyhow::Error> {
struct RunLoop {
memory_map: Vec<MemoryRegion>,
rtt_scan_regions: Vec<Range<u64>>,
path: PathBuf,
path: String,
timestamp_offset: UtcOffset,
always_print_stacktrace: bool,
no_location: bool,
Expand Down Expand Up @@ -213,7 +213,7 @@ impl RunLoop {
core,
self.memory_map.as_slice(),
self.rtt_scan_regions.as_slice(),
self.path.as_path(),
Path::new(&self.path),
&rtt_config,
self.timestamp_offset,
);
Expand Down Expand Up @@ -280,7 +280,7 @@ impl RunLoop {
|| return_reason.is_err()
|| matches!(return_reason, Ok(ReturnReason::Timeout))
{
print_stacktrace(core, self.path.as_path())?;
print_stacktrace(core, Path::new(&self.path))?;
}

signal_hook::low_level::unregister(sig_id);
Expand Down