Semihosting

    The crate provides an API to do semihosting operations on Cortex-M devices. The program below is the semihosting version of “Hello, world!”:

    If you run this program on hardware you’ll see the “Hello, world!” message within the OpenOCD logs.

    1. (..)
    2. Hello, world!
    3. (..)
    1. (gdb) monitor arm semihosting enable
    2. semihosting is enabled

    QEMU understands semihosting operations so the above program will also work with qemu-system-arm without having to start a debug session. Note that you’ll need to pass the -semihosting-config flag to QEMU to enable semihosting support; these flags are already included in the .cargo/config.toml file of the template.

    There’s also an exit semihosting operation that can be used to terminate the QEMU process. Important: do not use debug::exit on hardware; this function can corrupt your OpenOCD session and you will not be able to debug more programs until you restart it.

    1. #![no_main]
    2. #![no_std]
    3. use panic_halt as _;
    4. use cortex_m_rt::entry;
    5. #[entry]
    6. fn main() -> ! {
    7. let roses = "blue";
    8. if roses == "red" {
    9. debug::exit(debug::EXIT_SUCCESS);
    10. } else {
    11. debug::exit(debug::EXIT_FAILURE);
    12. }
    13. loop {}
    1. $ cargo run
    2. Running `qemu-system-arm (..)
    3. 1

    For convenience, the panic-semihosting crate has an “exit” feature that when enabled invokes exit(EXIT_FAILURE) after logging the panic message to the host stderr.

    1. $ cargo run
    2. Running `qemu-system-arm (..)
    3. panicked at 'assertion failed: `(left == right)`
    4. left: `"blue"`,
    5. right: `"red"`', examples/hello.rs:15:5
    6. $ echo $?

    NOTE: To enable this feature on panic-semihosting, edit your Cargo.toml dependencies section where panic-semihosting is specified with: