esp-backtrace: Add custom-pre-backtrace feature (#1822)

This commit is contained in:
dimpolo 2024-07-18 09:38:40 +02:00 committed by GitHub
parent 0eb30c5286
commit 02c99786f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 54 additions and 37 deletions

View File

@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased
### Added
- add custom-pre-backtrace feature (#1822)
### Changed

View File

@ -42,11 +42,12 @@ print-float-registers = [] # TODO support esp32p4
# You may optionally enable one or more of the below features to provide
# additional functionality:
colors = []
custom-halt = []
exception-handler = []
halt-cores = []
panic-handler = []
colors = []
custom-halt = []
custom-pre-backtrace = []
exception-handler = []
halt-cores = []
panic-handler = []
[lints.rust]
unexpected_cfgs = "allow"

View File

@ -17,24 +17,25 @@ When using the panic and/or exception handler make sure to include `use esp_back
## Features
| Feature | Description |
| ----------------- | ------------------------------------------------------------------------------------------------------------------ |
| esp32 | Target ESP32 |
| esp32c2 | Target ESP32-C2 |
| esp32c3 | Target ESP32-C3 |
| esp32c6 | Target ESP32-C6 |
| esp32h2 | Target ESP32-H2 |
| esp32p4 | Target ESP32-P4 |
| esp32s2 | Target ESP32-S2 |
| esp32s3 | Target ESP32-S3 |
| panic-handler | Include a panic handler, will add `esp-println` as a dependency |
| exception-handler | Include an exception handler, will add `esp-println` as a dependency |
| println | Use `esp-println` to print messages |
| defmt | Use `defmt` logging to print messages\* (check [example](https://github.com/playfulFence/backtrace-defmt-example)) |
| colors | Print messages in red\* |
| halt-cores | Halt both CPUs on ESP32 / ESP32-S3 instead of doing a `loop {}` in case of a panic or exception |
| semihosting | Call `semihosting::process::abort()` on panic. |
| custom-halt | Invoke the extern function `custom_halt()` instead of doing a `loop {}` in case of a panic or exception |
| Feature | Description |
|----------------------|--------------------------------------------------------------------------------------------------------------------|
| esp32 | Target ESP32 |
| esp32c2 | Target ESP32-C2 |
| esp32c3 | Target ESP32-C3 |
| esp32c6 | Target ESP32-C6 |
| esp32h2 | Target ESP32-H2 |
| esp32p4 | Target ESP32-P4 |
| esp32s2 | Target ESP32-S2 |
| esp32s3 | Target ESP32-S3 |
| panic-handler | Include a panic handler, will add `esp-println` as a dependency |
| exception-handler | Include an exception handler, will add `esp-println` as a dependency |
| println | Use `esp-println` to print messages |
| defmt | Use `defmt` logging to print messages\* (check [example](https://github.com/playfulFence/backtrace-defmt-example)) |
| colors | Print messages in red\* |
| halt-cores | Halt both CPUs on ESP32 / ESP32-S3 instead of doing a `loop {}` in case of a panic or exception |
| semihosting | Call `semihosting::process::abort()` on panic. |
| custom-halt | Invoke the extern function `custom_halt()` instead of doing a `loop {}` in case of a panic or exception |
| custom-pre-backtrace | Invoke the extern function `custom_pre_backtrace()` before handling a panic or exception |
\* _only used for panic and exception handlers_

View File

@ -52,6 +52,8 @@ pub mod arch;
#[cfg(feature = "panic-handler")]
#[panic_handler]
fn panic_handler(info: &core::panic::PanicInfo) -> ! {
pre_backtrace();
#[cfg(feature = "colors")]
set_color_code(RED);
@ -107,14 +109,12 @@ fn panic_handler(info: &core::panic::PanicInfo) -> ! {
if backtrace.iter().filter(|e| e.is_some()).count() == 0 {
println!("No backtrace available - make sure to force frame-pointers. (see https://crates.io/crates/esp-backtrace)");
}
for e in backtrace {
if let Some(addr) = e {
#[cfg(all(feature = "colors", feature = "println"))]
println!("{}0x{:x}", RED, addr - crate::arch::RA_OFFSET);
for addr in backtrace.into_iter().flatten() {
#[cfg(all(feature = "colors", feature = "println"))]
println!("{}0x{:x}", RED, addr - crate::arch::RA_OFFSET);
#[cfg(not(all(feature = "colors", feature = "println")))]
println!("0x{:x}", addr - crate::arch::RA_OFFSET);
}
#[cfg(not(all(feature = "colors", feature = "println")))]
println!("0x{:x}", addr - crate::arch::RA_OFFSET);
}
#[cfg(feature = "colors")]
@ -131,6 +131,8 @@ fn panic_handler(info: &core::panic::PanicInfo) -> ! {
#[no_mangle]
#[link_section = ".rwtext"]
unsafe fn __user_exception(cause: arch::ExceptionCause, context: arch::Context) {
pre_backtrace();
#[cfg(feature = "colors")]
set_color_code(RED);
@ -166,6 +168,8 @@ unsafe fn __user_exception(cause: arch::ExceptionCause, context: arch::Context)
#[cfg(all(feature = "exception-handler", target_arch = "riscv32"))]
#[export_name = "ExceptionHandler"]
fn exception_handler(context: &arch::TrapFrame) -> ! {
pre_backtrace();
let mepc = context.pc;
let code = context.mcause & 0xff;
let mtval = context.mtval;
@ -215,14 +219,12 @@ fn exception_handler(context: &arch::TrapFrame) -> ! {
if backtrace.iter().filter(|e| e.is_some()).count() == 0 {
println!("No backtrace available - make sure to force frame-pointers. (see https://crates.io/crates/esp-backtrace)");
}
for e in backtrace {
if let Some(addr) = e {
#[cfg(all(feature = "colors", feature = "println"))]
println!("{}0x{:x}", RED, addr - crate::arch::RA_OFFSET);
for addr in backtrace.into_iter().flatten() {
#[cfg(all(feature = "colors", feature = "println"))]
println!("{}0x{:x}", RED, addr - crate::arch::RA_OFFSET);
#[cfg(not(all(feature = "colors", feature = "println")))]
println!("0x{:x}", addr - crate::arch::RA_OFFSET);
}
#[cfg(not(all(feature = "colors", feature = "println")))]
println!("0x{:x}", addr - crate::arch::RA_OFFSET);
}
}
@ -363,3 +365,15 @@ fn halt() -> ! {
loop {}
}
#[cfg(not(feature = "custom-pre-backtrace"))]
#[allow(unused)]
fn pre_backtrace() {}
#[cfg(feature = "custom-pre-backtrace")]
fn pre_backtrace() {
extern "Rust" {
fn custom_pre_backtrace();
}
unsafe { custom_pre_backtrace() }
}