mirror of
https://github.com/esp-rs/esp-hal.git
synced 2025-10-03 15:15:01 +00:00
esp-backtrace: Add custom-pre-backtrace feature (#1822)
This commit is contained in:
parent
0eb30c5286
commit
02c99786f9
@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
- add custom-pre-backtrace feature (#1822)
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
|
@ -42,11 +42,12 @@ print-float-registers = [] # TODO support esp32p4
|
|||||||
|
|
||||||
# You may optionally enable one or more of the below features to provide
|
# You may optionally enable one or more of the below features to provide
|
||||||
# additional functionality:
|
# additional functionality:
|
||||||
colors = []
|
colors = []
|
||||||
custom-halt = []
|
custom-halt = []
|
||||||
exception-handler = []
|
custom-pre-backtrace = []
|
||||||
halt-cores = []
|
exception-handler = []
|
||||||
panic-handler = []
|
halt-cores = []
|
||||||
|
panic-handler = []
|
||||||
|
|
||||||
[lints.rust]
|
[lints.rust]
|
||||||
unexpected_cfgs = "allow"
|
unexpected_cfgs = "allow"
|
||||||
|
@ -17,24 +17,25 @@ When using the panic and/or exception handler make sure to include `use esp_back
|
|||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
| Feature | Description |
|
| Feature | Description |
|
||||||
| ----------------- | ------------------------------------------------------------------------------------------------------------------ |
|
|----------------------|--------------------------------------------------------------------------------------------------------------------|
|
||||||
| esp32 | Target ESP32 |
|
| esp32 | Target ESP32 |
|
||||||
| esp32c2 | Target ESP32-C2 |
|
| esp32c2 | Target ESP32-C2 |
|
||||||
| esp32c3 | Target ESP32-C3 |
|
| esp32c3 | Target ESP32-C3 |
|
||||||
| esp32c6 | Target ESP32-C6 |
|
| esp32c6 | Target ESP32-C6 |
|
||||||
| esp32h2 | Target ESP32-H2 |
|
| esp32h2 | Target ESP32-H2 |
|
||||||
| esp32p4 | Target ESP32-P4 |
|
| esp32p4 | Target ESP32-P4 |
|
||||||
| esp32s2 | Target ESP32-S2 |
|
| esp32s2 | Target ESP32-S2 |
|
||||||
| esp32s3 | Target ESP32-S3 |
|
| esp32s3 | Target ESP32-S3 |
|
||||||
| panic-handler | Include a panic handler, will add `esp-println` as a dependency |
|
| 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 |
|
| exception-handler | Include an exception handler, will add `esp-println` as a dependency |
|
||||||
| println | Use `esp-println` to print messages |
|
| println | Use `esp-println` to print messages |
|
||||||
| defmt | Use `defmt` logging to print messages\* (check [example](https://github.com/playfulFence/backtrace-defmt-example)) |
|
| defmt | Use `defmt` logging to print messages\* (check [example](https://github.com/playfulFence/backtrace-defmt-example)) |
|
||||||
| colors | Print messages in red\* |
|
| 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 |
|
| 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. |
|
| 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-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_
|
\* _only used for panic and exception handlers_
|
||||||
|
|
||||||
|
@ -52,6 +52,8 @@ pub mod arch;
|
|||||||
#[cfg(feature = "panic-handler")]
|
#[cfg(feature = "panic-handler")]
|
||||||
#[panic_handler]
|
#[panic_handler]
|
||||||
fn panic_handler(info: &core::panic::PanicInfo) -> ! {
|
fn panic_handler(info: &core::panic::PanicInfo) -> ! {
|
||||||
|
pre_backtrace();
|
||||||
|
|
||||||
#[cfg(feature = "colors")]
|
#[cfg(feature = "colors")]
|
||||||
set_color_code(RED);
|
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 {
|
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)");
|
println!("No backtrace available - make sure to force frame-pointers. (see https://crates.io/crates/esp-backtrace)");
|
||||||
}
|
}
|
||||||
for e in backtrace {
|
for addr in backtrace.into_iter().flatten() {
|
||||||
if let Some(addr) = e {
|
#[cfg(all(feature = "colors", feature = "println"))]
|
||||||
#[cfg(all(feature = "colors", feature = "println"))]
|
println!("{}0x{:x}", RED, addr - crate::arch::RA_OFFSET);
|
||||||
println!("{}0x{:x}", RED, addr - crate::arch::RA_OFFSET);
|
|
||||||
|
|
||||||
#[cfg(not(all(feature = "colors", feature = "println")))]
|
#[cfg(not(all(feature = "colors", feature = "println")))]
|
||||||
println!("0x{:x}", addr - crate::arch::RA_OFFSET);
|
println!("0x{:x}", addr - crate::arch::RA_OFFSET);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "colors")]
|
#[cfg(feature = "colors")]
|
||||||
@ -131,6 +131,8 @@ fn panic_handler(info: &core::panic::PanicInfo) -> ! {
|
|||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
#[link_section = ".rwtext"]
|
#[link_section = ".rwtext"]
|
||||||
unsafe fn __user_exception(cause: arch::ExceptionCause, context: arch::Context) {
|
unsafe fn __user_exception(cause: arch::ExceptionCause, context: arch::Context) {
|
||||||
|
pre_backtrace();
|
||||||
|
|
||||||
#[cfg(feature = "colors")]
|
#[cfg(feature = "colors")]
|
||||||
set_color_code(RED);
|
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"))]
|
#[cfg(all(feature = "exception-handler", target_arch = "riscv32"))]
|
||||||
#[export_name = "ExceptionHandler"]
|
#[export_name = "ExceptionHandler"]
|
||||||
fn exception_handler(context: &arch::TrapFrame) -> ! {
|
fn exception_handler(context: &arch::TrapFrame) -> ! {
|
||||||
|
pre_backtrace();
|
||||||
|
|
||||||
let mepc = context.pc;
|
let mepc = context.pc;
|
||||||
let code = context.mcause & 0xff;
|
let code = context.mcause & 0xff;
|
||||||
let mtval = context.mtval;
|
let mtval = context.mtval;
|
||||||
@ -215,14 +219,12 @@ fn exception_handler(context: &arch::TrapFrame) -> ! {
|
|||||||
if backtrace.iter().filter(|e| e.is_some()).count() == 0 {
|
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)");
|
println!("No backtrace available - make sure to force frame-pointers. (see https://crates.io/crates/esp-backtrace)");
|
||||||
}
|
}
|
||||||
for e in backtrace {
|
for addr in backtrace.into_iter().flatten() {
|
||||||
if let Some(addr) = e {
|
#[cfg(all(feature = "colors", feature = "println"))]
|
||||||
#[cfg(all(feature = "colors", feature = "println"))]
|
println!("{}0x{:x}", RED, addr - crate::arch::RA_OFFSET);
|
||||||
println!("{}0x{:x}", RED, addr - crate::arch::RA_OFFSET);
|
|
||||||
|
|
||||||
#[cfg(not(all(feature = "colors", feature = "println")))]
|
#[cfg(not(all(feature = "colors", feature = "println")))]
|
||||||
println!("0x{:x}", addr - crate::arch::RA_OFFSET);
|
println!("0x{:x}", addr - crate::arch::RA_OFFSET);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -363,3 +365,15 @@ fn halt() -> ! {
|
|||||||
|
|
||||||
loop {}
|
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() }
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user