From b2ba0fa9af69b7ff12b68e1c8b1f2c923c455e2e Mon Sep 17 00:00:00 2001 From: Fenrir Date: Tue, 1 Aug 2017 15:24:05 -0600 Subject: [PATCH] Properly set up main thread --- ctr-std/src/panicking.rs | 33 +++++++++++---------------------- ctr-std/src/rt.rs | 19 ++++++++++++++++--- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/ctr-std/src/panicking.rs b/ctr-std/src/panicking.rs index c2e0832..7047334 100644 --- a/ctr-std/src/panicking.rs +++ b/ctr-std/src/panicking.rs @@ -354,25 +354,17 @@ fn default_hook(info: &PanicInfo) { }; // 3DS-specific code begins here - use libctru::{consoleInit, consoleDebugInit, debugDevice, - consoleClear, gfxScreen_t, threadGetCurrent}; + use libctru::{consoleDebugInit, debugDevice}; use sys::stdio::Stderr; let mut err = Stderr::new().ok(); let thread = thread_info::current_thread(); - let name; + let name = thread.as_ref() + .and_then(|t| t.name()) + .unwrap_or(""); unsafe { - // Set up a new console, overwriting whatever was on the bottom screen - // before we started panicking - let _console = consoleInit(gfxScreen_t::GFX_BOTTOM, ptr::null_mut()); - consoleClear(); consoleDebugInit(debugDevice::debugDevice_CONSOLE); - - // Determine thread name - name = thread.as_ref() - .and_then(|t| t.name()) - .unwrap_or(if threadGetCurrent() == ptr::null_mut() {"main"} else {""}); } let write = |err: &mut ::io::Write| { @@ -405,10 +397,6 @@ fn default_hook(info: &PanicInfo) { (None, Some(ref mut err)) => { write(err) } _ => {} } - // Citra will crash and die horribly if we allow it to go further than this, - // So we just trap control flow in this loop instead. You'll still see the panic - // message, and Citra won't die quite as horribly. It's a win-win! Well, mostly. - loop { } } #[cfg(not(feature = "citra"))] @@ -441,17 +429,18 @@ fn default_hook(info: &PanicInfo) { } }; + // 3DS-specific code begins here - use libctru::{errorInit, errorText, errorDisp, threadGetCurrent, + use libctru::{errorInit, errorText, errorDisp, errorConf, errorType, CFG_Language}; use libc; - unsafe { - let thread = thread_info::current_thread(); - let name = thread.as_ref() - .and_then(|t| t.name()) - .unwrap_or(if threadGetCurrent() == ptr::null_mut() {"main"} else {""}); + let thread = thread_info::current_thread(); + let name = thread.as_ref() + .and_then(|t| t.name()) + .unwrap_or(""); + unsafe { // Setup error payload let error_text = format!("thread '{}' panicked at '{}', {}:{}:{}", name, msg, file, line, col); diff --git a/ctr-std/src/rt.rs b/ctr-std/src/rt.rs index ddfb707..735509b 100644 --- a/ctr-std/src/rt.rs +++ b/ctr-std/src/rt.rs @@ -23,6 +23,8 @@ #![doc(hidden)] use panic; +use sys_common::thread_info; +use thread::Thread; use mem; // Reexport some of our utilities which are expected by other crates. @@ -32,8 +34,19 @@ pub use panicking::{begin_panic, begin_panic_fmt}; #[lang = "start"] #[allow(unused_variables)] fn lang_start(main: *const u8, argc: isize, argv: *const *const u8) -> isize { - let _ = unsafe { - panic::catch_unwind(mem::transmute::<_, fn()>(main)) + let failed = unsafe { + let thread = Thread::new(Some("main".to_owned())); + + thread_info::set(None, thread); + + let res = panic::catch_unwind(mem::transmute::<_, fn()>(main)); + + res.is_err() }; - 0 + + if failed { + 101 + } else { + 0 + } }