Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Rust] Handling of panics by passing them to trace() #42

Closed
Matt-Is-Confused opened this issue Aug 31, 2021 · 5 comments · Fixed by #111
Closed

[Rust] Handling of panics by passing them to trace() #42

Matt-Is-Confused opened this issue Aug 31, 2021 · 5 comments · Fixed by #111
Labels
enhancement New feature or request

Comments

@Matt-Is-Confused
Copy link

Matt-Is-Confused commented Aug 31, 2021

Adding this to start() will hook all panic messages (probably), unless it panics during a panic in which case it just crashes. If memory problems are present it will crash on alloc so we can't rely on .to_string().

#[no_mangle]
fn start() {
    #[cfg(debug_assertions)]
    panic::set_hook(Box::new(|info| {
        if let Some(s) = info.payload().downcast_ref::<&str>() {
            trace(s);
        } else {
            trace("A panic occurred somewhere...")
        }

        /* // I am unsure of what wizardry wasmbindgen uses but backtraces don't seem to work independently.
        trace("Stack:\n");
        let backtrace = Backtrace::force_capture();
        match backtrace.status() {
            std::backtrace::BacktraceStatus::Unsupported => trace("No backtrace support"),
            std::backtrace::BacktraceStatus::Disabled => {
                trace("Something disabled the backtracing")
            }
            std::backtrace::BacktraceStatus::Captured => {
                for frame in backtrace.frames() {
                    trace(&format!("{:?}", frame));
                }
            }
            _ => trace("The compiler demanded I add this. If you see this assume everything has gone wrong."),
        };
        */

        //trace(&info.to_string())
        // .to_string() will just crash if allocations are not working
    }));
}

An error(&str) function could be added so that stack logs could be passed to the console as errors.

@aduros aduros added the enhancement New feature or request label Aug 31, 2021
@aduros
Copy link
Owner

aduros commented Aug 31, 2021

This looks amazing, thanks! Would it makes sense to include this in wasm4.rs somehow?

@Matt-Is-Confused
Copy link
Author

It could be included. It needs to be called once to be used. Having it be in an init function that is called from start() or simply being in the default start function would work.

@FaberVitale
Copy link
Contributor

FaberVitale commented Sep 8, 2021

I'm going to leave here for visibility how to set a panic hook handler in a wasm-4 cartridge written in rust with no_std.

// `src/lib.rs`
#![no_std]

// mod declarations
// other imports

use core::arch::wasm32;
use wasm4;

fn start() {}

fn update() {}

#[panic_handler]
fn panic_handler(panic_info: &core::panic::PanicInfo<'_>) -> ! {
     wasm4::trace("panic error");

    #[cfg(debug_assertions)]
    if let Some(cause) = panic_info.payload().downcast_ref::<&str>() {
        wasm4::trace(cause);
    }

    unsafe { wasm32::unreachable() }
}

#[cfg(debug_assertions)] inside the panic removes that if branch in a build --release:

Disabling in --release saves a lot of KB, with pong.wasm goes from 40KB to 12KB without running wasm-opt.

@aduros
Copy link
Owner

aduros commented Sep 9, 2021

@FaberVitale That looks great! Should we add it to wasm4.rs? Does it work everywhere or only in no_std?

@FaberVitale
Copy link
Contributor

@FaberVitale Does it work everywhere or only in no_std?

You have to config a panic_handler if you are in no_std mode.
If you're not in std mode a default one is already provided.

wasm32::unreachable() is the only way I've found to not block the event loop with a loop {} expression.

That looks great! Should we add it to wasm4.rs?

Yes if the template is going to be a no_std.

I've left the comment here in case someone else wants to make a no_std cart.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants