Skip to content
This repository was archived by the owner on Dec 2, 2020. It is now read-only.

Commit 5e9b283

Browse files
bors[bot]nickray
andauthored
Merge #42
42: Add equivalent of dbg! macro r=adamgreig a=nickray I think this would be quite useful. Thoughts? Maybe it would be better for `dbg!` to be in core with somehow switchable implementation (semihosting, vcom, etc.), but I don't know how to approach that. Added CHANGELOG also for the missing one for #39 Perhaps some more documentation is needed? Co-authored-by: Nicolas Stalder <[email protected]>
2 parents 25cb553 + 23eb7fd commit 5e9b283

File tree

7 files changed

+66
-2
lines changed

7 files changed

+66
-2
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ matrix:
2222
rust: stable
2323
if: (branch = staging OR branch = trying) OR (type = pull_request AND branch = master)
2424

25+
- env: TARGET=thumbv8m.main-none-eabi
26+
rust: stable
27+
if: (branch = staging OR branch = trying) OR (type = pull_request AND branch = master)
28+
2529
- env: TARGET=x86_64-unknown-linux-gnu
2630
rust: nightly
2731
if: (branch = staging OR branch = trying) OR (type = pull_request AND branch = master)

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
55

66
## [Unreleased]
77

8+
- Adds a feature to work around JLink quirks
9+
- Adds a dbg! macro using heprintln
10+
811
## [v0.3.4] - 2019-04-22
912

1013
### Fixed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ name = "cortex-m-semihosting"
1111
readme = "README.md"
1212
repository = "https://github.com/rust-embedded/cortex-m-semihosting"
1313
version = "0.3.4"
14+
edition = "2018"
1415

1516
[features]
1617
inline-asm = []

src/export.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use core::fmt::{self, Write};
44

55
use cortex_m::interrupt;
66

7-
use hio::{self, HStderr, HStdout};
7+
use crate::hio::{self, HStderr, HStdout};
88

99
static mut HSTDOUT: Option<HStdout> = None;
1010

src/hio.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Host I/O
22
33
use core::{fmt, slice};
4-
use nr;
4+
use crate::nr;
55

66
/// Host's standard error
77
pub struct HStderr {

src/lib.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,36 @@
123123
//! ```
124124
//! Output and monitoring proceed as in the above example.
125125
//!
126+
//! ## The `dbg!` macro
127+
//!
128+
//! Analogous to [`std::dbg`](https://doc.rust-lang.org/std/macro.dbg.html) the macro
129+
//! `dbg!` returns a given expression and prints it using `heprintln!` including context
130+
//! for quick and dirty debugging.
131+
//!
132+
//! Example:
133+
//!
134+
//! ```
135+
//! const UUID: *mut u32 = 0x0009_FC70 as *mut u32;
136+
//! dbg!(UUID);
137+
//! let mut uuid: [u32; 4] = [0; 4];
138+
//! for i in 0..4 {
139+
//! dbg!(i);
140+
//! uuid[i] = unsafe { dbg!(UUID.offset(i as isize).read_volatile()) }; }
141+
//! }
142+
//! ```
143+
//! outputs
144+
//! ```
145+
//! [examples/semihosting.rs:37] UUID = 0x0009fc70
146+
//! [examples/semihosting.rs:40] i = 0
147+
//! [examples/semihosting.rs:41] UUID.offset(i as isize).read_volatile() = 3370045464
148+
//! [examples/semihosting.rs:40] i = 1
149+
//! [examples/semihosting.rs:41] UUID.offset(i as isize).read_volatile() = 1426218275
150+
//! [examples/semihosting.rs:40] i = 2
151+
//! [examples/semihosting.rs:41] UUID.offset(i as isize).read_volatile() = 2422621116
152+
//! [examples/semihosting.rs:40] i = 3
153+
//! [examples/semihosting.rs:41] UUID.offset(i as isize).read_volatile() = 1044138593
154+
//! ```
155+
//!
126156
//! # Optional features
127157
//!
128158
//! ## `inline-asm`

src/macros.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,29 @@ macro_rules! heprintln {
8585
$crate::export::hstderr_fmt(format_args!(concat!($s, "\n"), $($tt)*))
8686
};
8787
}
88+
89+
/// Macro that prints and returns the value of a given expression
90+
/// for quick and dirty debugging. Works exactly like `dbg!` in
91+
/// the standard library, replacing `eprintln` with `heprintln`.
92+
#[macro_export]
93+
macro_rules! dbg {
94+
() => {
95+
$crate::hprintln!("[{}:{}]", file!(), line!());
96+
};
97+
($val:expr) => {
98+
// Use of `match` here is intentional because it affects the lifetimes
99+
// of temporaries - https://stackoverflow.com/a/48732525/1063961
100+
match $val {
101+
tmp => {
102+
$crate::hprintln!("[{}:{}] {} = {:#?}",
103+
file!(), line!(), stringify!($val), &tmp);
104+
tmp
105+
}
106+
}
107+
};
108+
// Trailing comma with single argument is ignored
109+
($val:expr,) => { $crate::dbg!($val) };
110+
($($val:expr),+ $(,)?) => {
111+
($($crate::dbg!($val)),+,)
112+
};
113+
}

0 commit comments

Comments
 (0)