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

Add equivalent of dbg! macro #42

Merged
merged 4 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ matrix:
rust: stable
if: (branch = staging OR branch = trying) OR (type = pull_request AND branch = master)

- env: TARGET=thumbv8m.main-none-eabi
rust: stable
if: (branch = staging OR branch = trying) OR (type = pull_request AND branch = master)

- env: TARGET=x86_64-unknown-linux-gnu
rust: nightly
if: (branch = staging OR branch = trying) OR (type = pull_request AND branch = master)
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

- Adds a feature to work around JLink quirks
- Adds a dbg! macro using heprintln

## [v0.3.4] - 2019-04-22

### Fixed
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ name = "cortex-m-semihosting"
readme = "README.md"
repository = "https://github.com/rust-embedded/cortex-m-semihosting"
version = "0.3.4"
edition = "2018"

[features]
inline-asm = []
Expand Down
2 changes: 1 addition & 1 deletion src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::fmt::{self, Write};

use cortex_m::interrupt;

use hio::{self, HStderr, HStdout};
use crate::hio::{self, HStderr, HStdout};

static mut HSTDOUT: Option<HStdout> = None;

Expand Down
2 changes: 1 addition & 1 deletion src/hio.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Host I/O

use core::{fmt, slice};
use nr;
use crate::nr;

/// Host's standard error
pub struct HStderr {
Expand Down
30 changes: 30 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,36 @@
//! ```
//! Output and monitoring proceed as in the above example.
//!
//! ## The `dbg!` macro
//!
//! Analogous to [`std::dbg`](https://doc.rust-lang.org/std/macro.dbg.html) the macro
//! `dbg!` returns a given expression and prints it using `heprintln!` including context
//! for quick and dirty debugging.
//!
//! Example:
//!
//! ```
//! const UUID: *mut u32 = 0x0009_FC70 as *mut u32;
//! dbg!(UUID);
//! let mut uuid: [u32; 4] = [0; 4];
//! for i in 0..4 {
//! dbg!(i);
//! uuid[i] = unsafe { dbg!(UUID.offset(i as isize).read_volatile()) }; }
//! }
//! ```
//! outputs
//! ```
//! [examples/semihosting.rs:37] UUID = 0x0009fc70
//! [examples/semihosting.rs:40] i = 0
//! [examples/semihosting.rs:41] UUID.offset(i as isize).read_volatile() = 3370045464
//! [examples/semihosting.rs:40] i = 1
//! [examples/semihosting.rs:41] UUID.offset(i as isize).read_volatile() = 1426218275
//! [examples/semihosting.rs:40] i = 2
//! [examples/semihosting.rs:41] UUID.offset(i as isize).read_volatile() = 2422621116
//! [examples/semihosting.rs:40] i = 3
//! [examples/semihosting.rs:41] UUID.offset(i as isize).read_volatile() = 1044138593
//! ```
//!
//! # Optional features
//!
//! ## `inline-asm`
Expand Down
26 changes: 26 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,29 @@ macro_rules! heprintln {
$crate::export::hstderr_fmt(format_args!(concat!($s, "\n"), $($tt)*))
};
}

/// Macro that prints and returns the value of a given expression
/// for quick and dirty debugging. Works exactly like `dbg!` in
/// the standard library, replacing `eprintln` with `heprintln`.
#[macro_export]
macro_rules! dbg {
() => {
$crate::hprintln!("[{}:{}]", file!(), line!());
};
($val:expr) => {
// Use of `match` here is intentional because it affects the lifetimes
// of temporaries - https://stackoverflow.com/a/48732525/1063961
match $val {
tmp => {
$crate::hprintln!("[{}:{}] {} = {:#?}",
file!(), line!(), stringify!($val), &tmp);
tmp
}
}
};
// Trailing comma with single argument is ignored
($val:expr,) => { $crate::dbg!($val) };
($($val:expr),+ $(,)?) => {
($($crate::dbg!($val)),+,)
};
}