From b767498152122c78f22c3b3fc6860fbed28319bb Mon Sep 17 00:00:00 2001 From: 0rphon <59403052+0rphon@users.noreply.github.com> Date: Wed, 20 Apr 2022 15:46:41 -0700 Subject: [PATCH] added custom panic hook (#1762) --- Cargo.lock | 12 ++++++++++++ Cargo.toml | 6 ++++++ leo/main.rs | 32 +++++++++++++++++++++++++++++++- 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 7d8eae5221..3f03c2658d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1262,6 +1262,7 @@ version = "1.5.3" dependencies = [ "ansi_term", "assert_cmd", + "backtrace", "clap 3.1.9", "color-backtrace", "colored", @@ -1282,6 +1283,7 @@ dependencies = [ "serde_json", "snarkvm-utilities 0.7.5 (git+https://github.com/AleoHQ/snarkVM.git?rev=51633e2)", "structopt", + "sys-info", "test_dir", "toml", "tracing", @@ -2589,6 +2591,16 @@ dependencies = [ "unicode-xid 0.2.2", ] +[[package]] +name = "sys-info" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b3a0d0aba8bf96a0e1ddfdc352fc53b3df7f39318c71854910c3c4b024ae52c" +dependencies = [ + "cc", + "libc", +] + [[package]] name = "tempfile" version = "3.3.0" diff --git a/Cargo.toml b/Cargo.toml index 1807713169..1cdbce37f1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -59,6 +59,12 @@ version = "0.5.1" [dependencies.colored] version = "2.0" +[dependencies.backtrace] +version = "0.3.65" + +[dependencies.sys-info] +version = "0.9.1" + [dependencies.dirs] version = "4.0.0" diff --git a/leo/main.rs b/leo/main.rs index baf29ad926..34a3d66a71 100644 --- a/leo/main.rs +++ b/leo/main.rs @@ -175,8 +175,38 @@ enum CommandOpts { // }, } +fn set_panic_hook() { + #[cfg(not(debug_assertions))] + std::panic::set_hook({ + Box::new(move |e| { + println!( + "thread `{}` {}", + std::thread::current().name().unwrap_or(""), + e + ); + println!("stack backtrace: \n{:?}", backtrace::Backtrace::new()); + println!("error: internal compiler error: unexpected panic\n"); + println!("note: the compiler unexpectedly panicked. this is a bug.\n"); + println!("note: we would appreciate a bug report: https://github.com/AleoHQ/leo/issues/new?labels=bug,panic&template=bug.md&title=[Bug]\n"); + println!( + "note: {} {} running on {} {}\n", + env!("CARGO_PKG_NAME"), + env!("CARGO_PKG_VERSION"), + sys_info::os_type().unwrap_or_else(|e| e.to_string()), + sys_info::os_release().unwrap_or_else(|e| e.to_string()), + ); + println!( + "note: compiler args: {}\n", + std::env::args().collect::>().join(" ") + ); + println!("note: compiler flags: {:?}\n", Opt::from_args()); + }) + }); +} + fn main() { - handle_error(run_with_args(Opt::from_args())) + set_panic_hook(); + handle_error(run_with_args(Opt::from_args())); } /// Run command with custom build arguments.