1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//! Log panic messages using the ITM (Instrumentation Trace Macrocell)
//!
//! This crate contains an implementation of `panic_fmt` that logs panic messages to the ITM
//! stimulus port 0.
//!
//! # Usage
//!
//! ``` ignore
//! #![no_std]
//!
//! extern crate panic_itm;
//!
//! fn main() {
//!     panic!("FOO")
//! }
//! ```
//!
//! ``` text
//! (gdb) monitor tpiu config external uart off 8000000 2000000
//! (gdb) monitor itm port 0 on
//! (gdb) continue
//! (..)
//! ```
//!
//! ``` text
//! $ itmdump -f /dev/ttyUSB0
//! panicked at 'FOO', src/main.rs:6:5
//! ```

#![deny(missing_docs)]
#![deny(warnings)]
#![feature(core_intrinsics)]
#![feature(lang_items)]
#![no_std]

extern crate aligned;
#[macro_use]
extern crate cortex_m;

use core::intrinsics;

use aligned::Aligned;
use cortex_m::peripheral::ITM;
use cortex_m::{interrupt, itm};

#[lang = "panic_fmt"]
unsafe extern "C" fn panic_fmt(
    args: core::fmt::Arguments,
    file: &'static str,
    line: u32,
    col: u32,
) -> ! {
    interrupt::disable();

    let itm = &mut *ITM::ptr();
    let stim = &mut itm.stim[0];

    itm::write_aligned(stim, &Aligned(*b"panicked at '"));
    itm::write_fmt(stim, args);
    itm::write_str(stim, "', ");
    itm::write_str(stim, file);
    iprintln!(stim, ":{}:{}", line, col);

    // XXX What should be the behavior after logging the message?
    intrinsics::abort()
}