diff --git a/Cargo.toml b/Cargo.toml index 6ad469f..6f064a3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ authors = ["Gary Guo "] edition = "2018" [workspace] -members = ["cdylib"] +members = ["cdylib", "example"] [dependencies] gimli = { version = "0.25.0", default-features = false, features = ["read"] } diff --git a/example/Cargo.toml b/example/Cargo.toml new file mode 100644 index 0000000..7fbac5b --- /dev/null +++ b/example/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "example" +version = "0.1.0" +edition = "2018" + +[dependencies] +unwind = { path = "../", features = ["system-alloc", "personality", "panic-handler"] } +libc = "0.2" diff --git a/example/src/main.rs b/example/src/main.rs new file mode 100644 index 0000000..77cbb4b --- /dev/null +++ b/example/src/main.rs @@ -0,0 +1,52 @@ +#![no_std] +#![feature(start)] +#![feature(default_alloc_error_handler)] + +extern crate alloc; +extern crate unwind; + +use alloc::{borrow::ToOwned, string::String}; +use unwind::print::*; + +#[link(name = "c")] +extern "C" {} + +struct PrintOnDrop(String); + +impl Drop for PrintOnDrop { + fn drop(&mut self) { + println!("dropped: {:?}", self.0); + } +} + +struct PanicOnDrop; + +impl Drop for PanicOnDrop { + fn drop(&mut self) { + panic!("panic on drop"); + } +} + +fn foo() { + panic!("panic"); +} + +fn bar() { + let _p = PrintOnDrop("string".to_owned()); + foo() +} + +#[start] +fn main(_argc: isize, _argv: *const *const u8) -> isize { + unwind::panic::catch_unwind(|| { + let _ = unwind::panic::catch_unwind(|| { + bar(); + println!("done"); + }); + println!("caught"); + let _p = PanicOnDrop; + foo(); + 0 + }) + .unwrap_or(101) +}