From ed537ec7e1144f75fe6f17756cdc17ec24081ff1 Mon Sep 17 00:00:00 2001 From: Lotte Steenbrink Date: Fri, 19 Jun 2020 09:21:53 +0200 Subject: [PATCH 1/9] dk-run: rename first rtt to rtt_addr for clarity --- tools/dk-run/src/main.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tools/dk-run/src/main.rs b/tools/dk-run/src/main.rs index 6061512..43b7504 100644 --- a/tools/dk-run/src/main.rs +++ b/tools/dk-run/src/main.rs @@ -64,7 +64,7 @@ fn notmain() -> Result { let mut debug_frame = None; let mut range_names = None; - let mut rtt = None; + let mut rtt_addr = None; let mut sections = vec![]; let mut dotdata = None; let mut registers = None; @@ -77,9 +77,9 @@ fn notmain() -> Result { if name == ".symtab" { if let Ok(symtab) = sect.get_data(&elf) { - let (rn, rtt_) = range_names_from(&elf, symtab, text)?; + let (rn, rtt_addr_) = range_names_from(&elf, symtab, text)?; range_names = Some(rn); - rtt = rtt_; + rtt_addr = rtt_addr_; } } @@ -195,8 +195,9 @@ fn notmain() -> Result { let mut rtt = Rtt::attach_region( core.clone(), &sess, - &ScanRegion::Exact(rtt.ok_or_else(|| anyhow!("RTT control block not found"))?), + &ScanRegion::Exact(rtt_addr.ok_or_else(|| anyhow!("RTT control block not available"))?), )?; + let channel = rtt .up_channels() .take(0) From 482ddc59d049ab13c8978dc80f9460e05a971736 Mon Sep 17 00:00:00 2001 From: Lotte Steenbrink Date: Fri, 19 Jun 2020 10:54:20 +0200 Subject: [PATCH 2/9] bup: it works! --- beginner/apps/Cargo.toml | 2 +- tools/Cargo.lock | 1 + tools/dk-run/Cargo.toml | 1 + tools/dk-run/src/main.rs | 36 +++++++++++++++++++++++++++++++----- 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/beginner/apps/Cargo.toml b/beginner/apps/Cargo.toml index a5483c0..6c75a25 100644 --- a/beginner/apps/Cargo.toml +++ b/beginner/apps/Cargo.toml @@ -18,7 +18,7 @@ debug = 1 debug-assertions = true # ! incremental = false lto = "fat" -opt-level = 'z' # ! +opt-level = 0 overflow-checks = false [profile.release] diff --git a/tools/Cargo.lock b/tools/Cargo.lock index 9d6fbe9..c2b3b84 100644 --- a/tools/Cargo.lock +++ b/tools/Cargo.lock @@ -169,6 +169,7 @@ dependencies = [ "probe-rs", "probe-rs-rtt", "rustc-demangle", + "thiserror", "xmas-elf", ] diff --git a/tools/dk-run/Cargo.toml b/tools/dk-run/Cargo.toml index fdac90b..1dbdd3d 100644 --- a/tools/dk-run/Cargo.toml +++ b/tools/dk-run/Cargo.toml @@ -15,3 +15,4 @@ probe-rs = "0.6.2" probe-rs-rtt = "0.1.0" rustc-demangle = "0.1.16" xmas-elf = "0.7.0" +thiserror = "1.0.11" diff --git a/tools/dk-run/src/main.rs b/tools/dk-run/src/main.rs index 43b7504..2ac912c 100644 --- a/tools/dk-run/src/main.rs +++ b/tools/dk-run/src/main.rs @@ -13,6 +13,7 @@ use std::{ process, rc::Rc, }; +use core::fmt::Error; use anyhow::{anyhow, bail}; use arrayref::array_ref; @@ -192,17 +193,42 @@ fn notmain() -> Result { // run let core = Rc::new(core); - let mut rtt = Rtt::attach_region( - core.clone(), - &sess, - &ScanRegion::Exact(rtt_addr.ok_or_else(|| anyhow!("RTT control block not available"))?), - )?; + let mut num_retries = 5; + + let mut rtt: Result; + + loop { + // TODO how do i best to this without allocating two rtts + rtt = Rtt::attach_region( + core.clone(), + &sess, + &ScanRegion::Exact(rtt_addr.ok_or_else(|| anyhow!("RTT control block not available"))?), // todo only do this once? + ); + + // todo clean up control flow + if rtt.is_ok() { + log::info!("successfully attached rtt"); + break; + } + + num_retries -= 1; + if num_retries == 0 { + // todo return last err instead + break; + } + log::info!("Rtt::attach_region failed. retrying."); + } + + log::info!("go rtt"); let channel = rtt + .unwrap() .up_channels() .take(0) .ok_or_else(|| anyhow!("RTT up channel 0 not found"))?; + log::info!("chan"); + static CONTINUE: AtomicBool = AtomicBool::new(true); ctrlc::set_handler(|| { From 3fac13606efb7ba923d4121108847c3a419547c6 Mon Sep 17 00:00:00 2001 From: Lotte Steenbrink Date: Fri, 19 Jun 2020 11:02:18 +0200 Subject: [PATCH 3/9] cleanup unused deps, debug output --- tools/Cargo.lock | 1 - tools/dk-run/Cargo.toml | 3 +-- tools/dk-run/src/main.rs | 13 +++---------- 3 files changed, 4 insertions(+), 13 deletions(-) diff --git a/tools/Cargo.lock b/tools/Cargo.lock index c2b3b84..9d6fbe9 100644 --- a/tools/Cargo.lock +++ b/tools/Cargo.lock @@ -169,7 +169,6 @@ dependencies = [ "probe-rs", "probe-rs-rtt", "rustc-demangle", - "thiserror", "xmas-elf", ] diff --git a/tools/dk-run/Cargo.toml b/tools/dk-run/Cargo.toml index 1dbdd3d..b74ed75 100644 --- a/tools/dk-run/Cargo.toml +++ b/tools/dk-run/Cargo.toml @@ -14,5 +14,4 @@ log = "0.4.8" probe-rs = "0.6.2" probe-rs-rtt = "0.1.0" rustc-demangle = "0.1.16" -xmas-elf = "0.7.0" -thiserror = "1.0.11" +xmas-elf = "0.7.0" \ No newline at end of file diff --git a/tools/dk-run/src/main.rs b/tools/dk-run/src/main.rs index 2ac912c..50ec002 100644 --- a/tools/dk-run/src/main.rs +++ b/tools/dk-run/src/main.rs @@ -13,7 +13,6 @@ use std::{ process, rc::Rc, }; -use core::fmt::Error; use anyhow::{anyhow, bail}; use arrayref::array_ref; @@ -198,7 +197,6 @@ fn notmain() -> Result { let mut rtt: Result; loop { - // TODO how do i best to this without allocating two rtts rtt = Rtt::attach_region( core.clone(), &sess, @@ -207,28 +205,23 @@ fn notmain() -> Result { // todo clean up control flow if rtt.is_ok() { - log::info!("successfully attached rtt"); + log::info!("Successfully attached RTT"); break; } num_retries -= 1; if num_retries == 0 { - // todo return last err instead break; } - log::info!("Rtt::attach_region failed. retrying."); + log::info!("Attaching RTT failed. retrying"); } - log::info!("go rtt"); - let channel = rtt - .unwrap() + .unwrap() // using ? instead wouldn't show the user any helpful error message .up_channels() .take(0) .ok_or_else(|| anyhow!("RTT up channel 0 not found"))?; - log::info!("chan"); - static CONTINUE: AtomicBool = AtomicBool::new(true); ctrlc::set_handler(|| { From b65b432762ad9d7953d2a1e130e0f1388d9b1711 Mon Sep 17 00:00:00 2001 From: Lotte Steenbrink Date: Fri, 19 Jun 2020 11:22:56 +0200 Subject: [PATCH 4/9] control flow cleanup --- tools/dk-run/src/main.rs | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/tools/dk-run/src/main.rs b/tools/dk-run/src/main.rs index 50ec002..63da642 100644 --- a/tools/dk-run/src/main.rs +++ b/tools/dk-run/src/main.rs @@ -192,29 +192,30 @@ fn notmain() -> Result { // run let core = Rc::new(core); - let mut num_retries = 5; - + let rtt_addr_result = rtt_addr.ok_or_else(|| anyhow!("RTT control block not available"))?; let mut rtt: Result; + let mut num_retries = 3; // picked at random, increase if necessary loop { rtt = Rtt::attach_region( core.clone(), &sess, - &ScanRegion::Exact(rtt_addr.ok_or_else(|| anyhow!("RTT control block not available"))?), // todo only do this once? + &ScanRegion::Exact(rtt_addr_result), ); - // todo clean up control flow - if rtt.is_ok() { - log::info!("Successfully attached RTT"); - break; + match rtt { + Ok(_) => { break; } + Err(res) => { + num_retries -= 1; + if num_retries == 0 { + log::info!("Max number of retries exceeded. Giving up."); + return Err(anyhow!(res)); + } + log::info!("Attaching RTT failed. retrying"); + } } - - num_retries -= 1; - if num_retries == 0 { - break; - } - log::info!("Attaching RTT failed. retrying"); } + log::info!("Successfully attached RTT"); let channel = rtt .unwrap() // using ? instead wouldn't show the user any helpful error message From 07cdaf5212dcc206b9f425aeed3d41a67108daa3 Mon Sep 17 00:00:00 2001 From: Lotte Steenbrink Date: Fri, 19 Jun 2020 11:35:43 +0200 Subject: [PATCH 5/9] reset debug level --- beginner/apps/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beginner/apps/Cargo.toml b/beginner/apps/Cargo.toml index 6c75a25..a5483c0 100644 --- a/beginner/apps/Cargo.toml +++ b/beginner/apps/Cargo.toml @@ -18,7 +18,7 @@ debug = 1 debug-assertions = true # ! incremental = false lto = "fat" -opt-level = 0 +opt-level = 'z' # ! overflow-checks = false [profile.release] From 71579aaa9f983de94eedd95882a6589fb7c89cdf Mon Sep 17 00:00:00 2001 From: Lotte Steenbrink Date: Fri, 19 Jun 2020 15:53:28 +0200 Subject: [PATCH 6/9] sketch --- tools/dk-run/src/main.rs | 41 ++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/tools/dk-run/src/main.rs b/tools/dk-run/src/main.rs index 63da642..0e2e5b0 100644 --- a/tools/dk-run/src/main.rs +++ b/tools/dk-run/src/main.rs @@ -192,32 +192,33 @@ fn notmain() -> Result { // run let core = Rc::new(core); - let rtt_addr_result = rtt_addr.ok_or_else(|| anyhow!("RTT control block not available"))?; - let mut rtt: Result; - let mut num_retries = 3; // picked at random, increase if necessary + let rtt_addr_res = rtt_addr.ok_or_else(|| anyhow!("RTT control block not available"))?; + let mut rtt_res: Result = + Err(probe_rs_rtt::Error::ControlBlockNotFound); + const NUM_RETRIES: usize = 3; // picked at random, increase if necessary - loop { - rtt = Rtt::attach_region( - core.clone(), - &sess, - &ScanRegion::Exact(rtt_addr_result), - ); - - match rtt { - Ok(_) => { break; } - Err(res) => { - num_retries -= 1; - if num_retries == 0 { - log::info!("Max number of retries exceeded. Giving up."); - return Err(anyhow!(res)); + for try_index in 0..NUM_RETRIES { + rtt_res = Rtt::attach_region(core.clone(), &sess, &ScanRegion::Exact(rtt_addr_res)); + match rtt_res { + Ok(_) => { + log::info!("Successfully attached RTT"); + break; + } + Err(probe_rs_rtt::Error::ControlBlockNotFound) => { + if try_index < NUM_RETRIES { + log::info!("Attaching RTT failed. retrying"); + } else { + log::info!("Max number of retries exceeded. Giving up"); + return Err(anyhow!(probe_rs_rtt::Error::ControlBlockNotFound)); } - log::info!("Attaching RTT failed. retrying"); + } + Err(e) => { + return Err(anyhow!(e)); } } } - log::info!("Successfully attached RTT"); - let channel = rtt + let channel = rtt_res .unwrap() // using ? instead wouldn't show the user any helpful error message .up_channels() .take(0) From 35a78540c0354fb1ae76be67e9b80267d61d9d81 Mon Sep 17 00:00:00 2001 From: Lotte Steenbrink Date: Mon, 22 Jun 2020 22:44:11 +0200 Subject: [PATCH 7/9] =?UTF-8?q?respond=20to=20jorges=20reviw=E2=80=93=20ma?= =?UTF-8?q?ke=20error=20messages=20more=20useful?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/dk-run/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/dk-run/src/main.rs b/tools/dk-run/src/main.rs index 0e2e5b0..a53e38b 100644 --- a/tools/dk-run/src/main.rs +++ b/tools/dk-run/src/main.rs @@ -206,9 +206,9 @@ fn notmain() -> Result { } Err(probe_rs_rtt::Error::ControlBlockNotFound) => { if try_index < NUM_RETRIES { - log::info!("Attaching RTT failed. retrying"); + log::info!("Could not attach because the target's RTT control block isn't initialized (yet). retrying"); } else { - log::info!("Max number of retries exceeded. Giving up"); + log::info!("Max number RTT attach of retries exceeded. Did you call dk::init() first thing in your program?"); return Err(anyhow!(probe_rs_rtt::Error::ControlBlockNotFound)); } } From f502132139213e9f79ad45c802dfaf48602c9dfd Mon Sep 17 00:00:00 2001 From: Lotte Steenbrink Date: Mon, 22 Jun 2020 22:54:47 +0200 Subject: [PATCH 8/9] =?UTF-8?q?respond=20to=20jorges=20reviw=E2=80=93=20fi?= =?UTF-8?q?x=20off=20by=20one=20err?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/dk-run/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/dk-run/src/main.rs b/tools/dk-run/src/main.rs index a53e38b..005809a 100644 --- a/tools/dk-run/src/main.rs +++ b/tools/dk-run/src/main.rs @@ -197,7 +197,7 @@ fn notmain() -> Result { Err(probe_rs_rtt::Error::ControlBlockNotFound); const NUM_RETRIES: usize = 3; // picked at random, increase if necessary - for try_index in 0..NUM_RETRIES { + for try_index in 0..=NUM_RETRIES { rtt_res = Rtt::attach_region(core.clone(), &sess, &ScanRegion::Exact(rtt_addr_res)); match rtt_res { Ok(_) => { From 080e96d290a045917f27579cf41230fc2dddffbf Mon Sep 17 00:00:00 2001 From: Lotte Steenbrink Date: Mon, 22 Jun 2020 22:55:27 +0200 Subject: [PATCH 9/9] =?UTF-8?q?respond=20to=20jorges=20reviw=E2=80=93=20us?= =?UTF-8?q?e=20expect(unreachable)=20//=20reason?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/dk-run/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/dk-run/src/main.rs b/tools/dk-run/src/main.rs index 005809a..32d46da 100644 --- a/tools/dk-run/src/main.rs +++ b/tools/dk-run/src/main.rs @@ -219,7 +219,7 @@ fn notmain() -> Result { } let channel = rtt_res - .unwrap() // using ? instead wouldn't show the user any helpful error message + .expect("unreachable") // this block is only executed when rtt was successfully attached before .up_channels() .take(0) .ok_or_else(|| anyhow!("RTT up channel 0 not found"))?;