Skip to content

Commit 88f46f9

Browse files
committed
Panic in exception handler
1 parent 3271ec2 commit 88f46f9

File tree

4 files changed

+12
-44
lines changed

4 files changed

+12
-44
lines changed

esp-backtrace/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212

1313
### Changed
1414

15+
- `exception-handler` now panics. (#3838)
1516

1617
### Fixed
1718

esp-backtrace/Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ test = false
2020

2121
[dependencies]
2222
cfg-if = "1.0.0"
23-
defmt = { version = "1.0.1", optional = true }
23+
defmt = { version = "1", optional = true }
2424
esp-config = { version = "0.5.0", path = "../esp-config" }
2525
esp-println = { version = "0.15.0", optional = true, default-features = false, path = "../esp-println" }
2626
heapless = "0.8"
@@ -51,17 +51,17 @@ print-float-registers = [] # TODO support esp32p4
5151

5252
# You may optionally enable one or more of the below features to provide
5353
# additional functionality:
54-
## Print messages in red (only used for panic and exception handlers)
54+
## Print messages in red
5555
colors = []
56-
## Invoke the extern function custom_halt() instead of doing a loop {} in case of a panic or exception
56+
## Invoke the extern function `custom_halt()` instead of doing a loop {} in case of a panic or exception
5757
custom-halt = []
5858
## Invoke the extern function `custom_pre_backtrace()` before handling a panic or exception
5959
custom-pre-backtrace = []
60-
## Include an exception handler, will add `esp-println` as a dependency
60+
## Turn unhandled exceptions into panics
6161
exception-handler = []
6262
## Halt both CPUs on ESP32 / ESP32-S3 instead of doing a `loop {}` in case of a panic or exception
6363
halt-cores = []
64-
## Include a panic handler, will add `esp-println` as a dependency
64+
## Include a panic handler
6565
panic-handler = []
6666

6767
[lints.rust]

esp-backtrace/src/lib.rs

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! ## Feature Flags
2+
#![doc = document_features::document_features!()]
13
#![allow(rustdoc::bare_urls, unused_macros)]
24
#![cfg_attr(target_arch = "xtensa", feature(asm_experimental_arch))]
35
//! This is a lightweight crate for obtaining backtraces during panics, exceptions, and hard faults
@@ -41,12 +43,6 @@ impl Backtrace {
4143
arch::backtrace()
4244
}
4345

44-
#[inline]
45-
#[cfg(feature = "exception-handler")]
46-
fn from_sp(sp: u32) -> Self {
47-
arch::backtrace_internal(sp, 0)
48-
}
49-
5046
/// Returns the backtrace frames as a slice.
5147
#[inline]
5248
pub fn frames(&self) -> &[BacktraceFrame] {
@@ -125,35 +121,21 @@ fn panic_handler(info: &core::panic::PanicInfo) -> ! {
125121
#[unsafe(no_mangle)]
126122
#[unsafe(link_section = ".rwtext")]
127123
unsafe fn __user_exception(cause: arch::ExceptionCause, context: arch::Context) {
128-
pre_backtrace();
129-
130-
println!("\n\nException occurred '{}'", cause);
131-
println!("{:?}", context);
132-
133-
let backtrace = Backtrace::from_sp(context.A1);
134-
for frame in backtrace.frames() {
135-
println!("0x{:x}", frame.program_counter());
136-
}
137-
138-
abort();
124+
panic!("\n\nException occurred '{}'\n{:?}", cause, context);
139125
}
140126

141127
#[cfg(all(feature = "exception-handler", target_arch = "riscv32"))]
142128
#[unsafe(export_name = "ExceptionHandler")]
143129
fn exception_handler(context: &arch::TrapFrame) -> ! {
144-
pre_backtrace();
145-
146130
let mepc = context.pc;
147131
let code = context.mcause & 0xff;
148132
let mtval = context.mtval;
149133

150134
if code == 14 {
151-
println!("");
152-
println!(
135+
panic!(
153136
"Stack overflow detected at 0x{:x} called by 0x{:x}",
154137
mepc, context.ra
155138
);
156-
println!("");
157139
} else {
158140
let code = match code {
159141
0 => "Instruction address misaligned",
@@ -175,26 +157,11 @@ fn exception_handler(context: &arch::TrapFrame) -> ! {
175157
_ => "UNKNOWN",
176158
};
177159

178-
println!(
160+
panic!(
179161
"Exception '{}' mepc=0x{:08x}, mtval=0x{:08x}",
180162
code, mepc, mtval
181163
);
182-
183-
println!("{:?}", context);
184-
185-
let backtrace = Backtrace::from_sp(context.s0 as u32);
186-
let frames = backtrace.frames();
187-
if frames.is_empty() {
188-
println!(
189-
"No backtrace available - make sure to force frame-pointers. (see https://crates.io/crates/esp-backtrace)"
190-
);
191-
}
192-
for frame in backtrace.frames() {
193-
println!("0x{:x}", frame.program_counter());
194-
}
195164
}
196-
197-
abort();
198165
}
199166

200167
// Ensure that the address is in DRAM and that it is 16-byte aligned.

hil-test/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ embedded-hal-nb = "1.0.0"
247247
esp-alloc = { path = "../esp-alloc", optional = true }
248248
esp-backtrace = { path = "../esp-backtrace", default-features = false, features = ["exception-handler", "defmt", "semihosting"] }
249249
esp-bootloader-esp-idf = { path = "../esp-bootloader-esp-idf" }
250-
esp-hal = { path = "../esp-hal", optional = true }
250+
esp-hal = { path = "../esp-hal" }
251251
esp-hal-embassy = { path = "../esp-hal-embassy", optional = true }
252252
esp-storage = { path = "../esp-storage", optional = true }
253253
esp-wifi = { path = "../esp-wifi", optional = true }

0 commit comments

Comments
 (0)