Debugging Embedded Rust With Probe rs and Defmt Logging
Debug embedded Rust with probe-rs: defmt logging over RTT, panic-probe, breakpoints in VS Code, reading hard faults and what to check when nothing prints.

Embedded programs fail silently. There is no console, no stack trace and often no operating system to tell you what happened. A logging channel and a debugger turn that around. This post uses defmt and probe-rs, the common pairing for Cortex-M chips; crate versions change quickly, so copy dependency lines from your HAL's examples.
Wiring defmt
[dependencies]
defmt = "0.3"
defmt-rtt = "0.4"
panic-probe = { version = "0.3", features = ["print-defmt"] }[target.thumbv7em-none-eabihf]
runner = "probe-rs run --chip STM32F411RETx"
rustflags = ["-C", "link-arg=-Tlink.x", "-C", "link-arg=-Tdefmt.x"]
[env]
DEFMT_LOG = "debug"use defmt::{debug, error, info, warn};
use {defmt_rtt as _, panic_probe as _};
fn report(temp_c: i32, ok: bool) {
info!("temperature {} C", temp_c);
debug!("raw ok={}", ok);
if temp_c > 80 {
warn!("hot: {=i32}", temp_c);
}
if !ok {
error!("sensor read failed");
}
}The two use lines link the RTT transport and the panic handler; without the first, nothing is ever logged. defmt formats on your computer: the chip sends an index into a table and the values, which is why one info! costs a handful of bytes.
Reading the output
INFO temperature 24 C
DEBUG raw ok=trueprobe-rs run decodes it using the ELF file, so it must be the same binary that is flashed. Levels below DEFMT_LOG are compiled out entirely, so debug! in a release build with DEFMT_LOG=info costs nothing.
Custom types derive defmt::Format:
#[derive(defmt::Format)]
struct Reading {
id: u8,
value: i32,
}Panics
panic-probe with print-defmt logs the message and location, then triggers a breakpoint instruction so the debugger stops. A bare unwrap() on None reports where:
ERROR panicked at src/main.rs:42:20:
called `Option::unwrap()` on a `None` valuePrefer expect with a reason, as the same rule holds on the desktop (unwrap on None).
Breakpoints in VS Code
Install the probe-rs extension and add a launch configuration:
{
"version": "0.2.0",
"configurations": [
{
"type": "probe-rs-debug",
"request": "launch",
"name": "Debug",
"chip": "STM32F411RETx",
"coreConfigs": [
{
"programBinary": "target/thumbv7em-none-eabihf/debug/app",
"rttEnabled": true
}
]
}
]
}You can then step, inspect variables and view RTT in the debug console. Optimized builds hide variables; use debug = 2 and opt-level = 1 for sessions that need to see values.
HardFaults
A HardFault with no message means the CPU executed something illegal. Common causes in Rust:
- **Stack overflow:** a large array on the stack. Move it to a
staticor shrink it.flip-linkputs the stack below RAM so overflow faults instead of corrupting data. - **Unaligned or invalid access** through
unsafepointers. - **Using a peripheral before enabling its clock.**
- **A panic with
panic-halt** that never prints anything.
Catch it with a handler:
use cortex_m_rt::{exception, ExceptionFrame};
#[exception]
unsafe fn HardFault(ef: &ExceptionFrame) -> ! {
defmt::error!("hard fault at pc={:#x}", ef.pc());
loop {}
}The program counter in the frame tells you the instruction; addr2line or the debugger maps it to a line. Beyond the logging itself, see getting started with embedded Rust and no_std.


