rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
95.68k stars 12.33k forks source link

Demangle C++ functions in backtraces #66416

Open leo60228 opened 4 years ago

leo60228 commented 4 years ago

I'm modding a C++ game (which contains debug symbols, thanks flibitijibibo!) using Rust. If I panic in a Rust function from my mod, this is what the backtrace looks like:

thread '<unnamed>' panicked at 'panic test', src/lib.rs:37:5
stack backtrace:
   0: backtrace::backtrace::libunwind::trace
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.40/src/backtrace/libunwind.rs:88
   1: backtrace::backtrace::trace_unsynchronized
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.40/src/backtrace/mod.rs:66
   2: std::sys_common::backtrace::_print_fmt
             at src/libstd/sys_common/backtrace.rs:77                                                                                   
   3: <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt
             at src/libstd/sys_common/backtrace.rs:61
   4: core::fmt::write
             at src/libcore/fmt/mod.rs:1030
   5: std::io::Write::write_fmt
             at src/libstd/io/mod.rs:1412
   6: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:65
   7: std::sys_common::backtrace::print
             at src/libstd/sys_common/backtrace.rs:50
   8: std::panicking::default_hook::{{closure}}
             at src/libstd/panicking.rs:188
   9: std::panicking::default_hook
             at src/libstd/panicking.rs:205
  10: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:464
  11: std::panicking::begin_panic
             at /rustc/bc0e288ad02ef362b5a6c42aaf61f2901c9b46db/src/libstd/panicking.rs:400
  12: vloader::hook_physfs_init
             at src/lib.rs:37
  13: core::ops::function::Fn::call
             at /rustc/bc0e288ad02ef362b5a6c42aaf61f2901c9b46db/src/libcore/ops/function.rs:69
  14: <alloc::boxed::Box<F> as core::ops::function::Fn<A>>::call
             at /rustc/bc0e288ad02ef362b5a6c42aaf61f2901c9b46db/src/liballoc/boxed.rs:956
  15: vloader::PHYSFS_INIT::__ffi_detour
             at /home/leo60228/vloader/<::detour::macros::static_detour macros>:31
  16: _Z15FILESYSTEM_initPc
             at /home/flibitijibibo/Programming/cppProjects/Contracts/VVVVVV/Src/FileSystemUtils.cpp:39
  17: main
             at /home/flibitijibibo/Programming/cppProjects/Contracts/VVVVVV/Src/main.cpp:39
  18: __libc_start_main
  19: <unknown>

This backtrace is almost perfect, except for the ugly mangled C++ symbol. Now that #65646 allows unwinding through C++ code, it would be nice if Rust demangled them. backtrace-rs already supports this behind a feature, and I tried enabling it through xargo, but it didn't work. I think this might be because of the way std prints backtraces.

jonas-schievink commented 4 years ago

Unwinding through an FFI boundary is still undefined behavior, but that's unrelated to how backtraces look.

leo60228 commented 4 years ago

What did #65646 do then?

leo60228 commented 4 years ago

Oh, so it's still UB, but it works now.