embedded-trainings-2020/embedded-workshop-book/src/panicking.md

44 lines
1.6 KiB
Markdown
Raw Normal View History

2020-07-08 13:06:49 +00:00
# Panicking
2020-07-14 11:51:11 +00:00
✅ Open the `src/bin/panic.rs` file and click the "Run" button.
2020-07-08 13:06:49 +00:00
This program attempts to index an array beyond its length and this results in a panic.
``` console
(HOST) INFO flashing program (34.79 KiB)
(HOST) INFO success!
────────────────────────────────────────────────────────────────────────────────
2020-07-08 13:06:49 +00:00
ERROR:panic_log -- panicked at 'index out of bounds: the len is 3 but the index is 3', src/bin/panic.rs:29:13
────────────────────────────────────────────────────────────────────────────────
2020-07-08 13:06:49 +00:00
stack backtrace:
0: HardFaultTrampoline
<exception entry>
[...]
7: panic::bar
at src/bin/panic.rs:29:13
8: panic::foo
at src/bin/panic.rs:22:5
9: panic::__cortex_m_rt_main
at src/bin/panic.rs:12:5
10: main
at src/bin/panic.rs:8:1
[...]
(HOST) ERROR the program panicked
2020-07-08 13:06:49 +00:00
```
In `no_std` programs the behavior of panic is defined using the `#[panic_handler]` attribute. In the example, the *panic handler* is defined in the `panic_log` crate but we can also implement it manually:
2020-07-14 11:51:11 +00:00
✅ Comment out the `panic_log` import and add the following function to the example:
2020-07-08 13:06:49 +00:00
``` rust
#[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! {
log::error!("{}", info);
loop {
asm::bkpt()
}
}
```
Now run the program again. Try changing the format string of the `error!` macro.