koute / cargo-web

A Cargo subcommand for the client-side Web
Apache License 2.0
1.1k stars 80 forks source link

Question: How to debug panic? #197

Open seanvoss opened 5 years ago

seanvoss commented 5 years ago

The error output of

    at __rust_start_panic (wasm-function[18194]:1)
    at rust_panic (wasm-function[18181]:31)
    at _ZN3std9panicking20rust_panic_with_hook17h12b7239ed4348eaeE (wasm-function[18176]:306)
    at _ZN3std9panicking18continue_panic_fmt17hfbe042bfacb6a5d7E (wasm-function[18175]:126)
    at rust_begin_unwind (wasm-function[18174]:3)
    at _ZN4core9panicking9panic_fmt17h0d6d5c8b201e3246E (wasm-function[18234]:70)
    at _ZN4core6result13unwrap_failed17h4ca817aabea69476E (wasm-function[1790]:290)
    at _ZN4core6result19Result$LT$T$C$E$GT$6unwrap17h0580f712ea931b25E (wasm-function[1848]:492)
    at _ZN72_$LT$roguelike..Game$u20$as$u20$quicksilver..lifecycle..state..State$GT$3new17h75062b596fc0cea6E (wasm-function[2126]:375)
    at _ZN11quicksilver9lifecycle3run3run28_$u7b$$u7b$closure$u7d$$u7d$17hb5f759f1b54a8e2cE (wasm-function[2078]:24)
__rust_start_panic @ wasm-01adcf06-18194:2
rust_panic @ wasm-01adcf06-18181:17
_ZN3std9panicking20rust_panic_with_hook17h12b7239ed4348eaeE @ wasm-01adcf06-18176:135
_ZN3std9panicking18continue_panic_fmt17hfbe042bfacb6a5d7E @ wasm-01adcf06-18175:52
rust_begin_unwind @ wasm-01adcf06-18174:3
_ZN4core9panicking9panic_fmt17h0d6d5c8b201e3246E @ wasm-01adcf06-18234:30
_ZN4core6result13unwrap_failed17h4ca817aabea69476E @ wasm-01adcf06-1790:135
_ZN4core6result19Result$LT$T$C$E$GT$6unwrap17h0580f712ea931b25E @ wasm-01adcf06-1848:232
_ZN72_$LT$roguelike..Game$u20$as$u20$quicksilver..lifecycle..state..State$GT$3new17h75062b596fc0cea6E @ wasm-01adcf06-2126:51
_ZN11quicksilver9lifecycle3run3run28_$u7b$$u7b$closure$u7d$$u7d$17hb5f759f1b54a8e2cE @ wasm-01adcf06-2078:14
_ZN11quicksilver9lifecycle11application20Application$LT$T$GT$3new17h8a23d15d354858ecE @ wasm-01adcf06-1786:27
_ZN11quicksilver9lifecycle3run8run_impl17h392103ca095e3eefE @ wasm-01adcf06-2083:448
_ZN11quicksilver9lifecycle3run8run_with17h5b7ddf8616aff7d9E @ wasm-01adcf06-2077:86
_ZN11quicksilver9lifecycle3run3run17h20f2e5c9748df458E @ wasm-01adcf06-2076:57
_ZN9roguelike4main17h69cf4b196d55b8f5E @ wasm-01adcf06-2132:278
_ZN3std2rt10lang_start28_$u7b$$u7b$closure$u7d$$u7d$17h2ea6f737b0651c7bE @ wasm-01adcf06-1935:23
_ZN3std10sys_common9backtrace28__rust_begin_short_backtrace17h1f5c6d3d9bd1f408E @ wasm-01adcf06-18155:5
_ZN3std9panicking3try7do_call17h5c160734b0814692E @ wasm-01adcf06-18172:10
__rust_maybe_catch_panic @ wasm-01adcf06-18193:4
_ZN3std2rt19lang_start_internal17h9c7b1c2b34bc776fE @ wasm-01adcf06-18184:104
_ZN3std2rt10lang_start17h892d11b3941df8e2E @ wasm-01adcf06-1934:50
main @ wasm-01adcf06-2133:13
initialize @ roguelike.js:929
(anonymous) @ roguelike.js:41
Promise.catch (async)
(anonymous) @ roguelike.js:45
(anonymous) @ roguelike.js:50
(anonymous) @ roguelike.js:13
(anonymous) @ roguelike.js:15

Is super difficult to debug. I know there's a panic hook in other implementations of wasm. Is there currently or on the roadmap a plan to add a hook to prettify this?

Pauan commented 5 years ago

I'm not sure if there's a generic solution, but I just write my own:

fn set_panic_hook() {
    std::panic::set_hook(Box::new(|info| {
        stdweb::print_error_panic(&info.to_string());
    }));
}

Now you can call it in main, like this:

fn main() {
    set_panic_hook();

    // Rest of code...
}
cedric-h commented 4 years ago

https://github.com/rustwasm/console_error_panic_hook/blob/master/src/lib.rs

These guys have an implementation that can get you a full stack trace, but it's for wasm-bindgen. You can, however, copy the relevant stack trace code (it's quite small) into the handler Pauan provides.

Pauan commented 4 years ago

@cedric-h That crate is equivalent to my code, since print_error_panic does display the stack trace.

However, console_error_panic_hook does add in some workarounds for browser bugs. It would be quite easy to port that into print_error_panic.

JackMordaunt commented 4 years ago

Am I going crazy, or was print_error_panic removed? I can't find it, or an equivalent, anywhere.

Pauan commented 4 years ago

@JackMordaunt It's defined in stdweb: https://github.com/koute/stdweb/blob/4d337ee9a0a4542ea5803b46b5124d9bc166dcb7/src/webcore/promise_future.rs#L127

Note that you need to enable both the futures-support and experimental_features_which_may_break_on_minor_version_bumps features.

JackMordaunt commented 4 years ago

Thanks a lot!

ghost commented 3 years ago

@Pauan Hi, I've been trying to use the print_error_panic. I can't figure out how to enable the

futures-support and experimental_features_which_may_break_on_minor_version_bumps features.

When I try to just use stdweb::print_error_panic(...), the error is

cannot find function print_error_panic in crate stdweb

How can I enable the features to get the stack trace for panics? Thank you in advance.