rust-lang / backtrace-rs

Backtraces in Rust
https://docs.rs/backtrace
Other
535 stars 245 forks source link

`Symbol` struct is not `Clone` #387

Open Diggsey opened 3 years ago

Diggsey commented 3 years ago

It does not appear possible to use the raw backtrace functionality effectively, because the Symbol struct does not implement Clone, and the resolve_frame callback is passed a &Symbol reference which is not 'static. As a result, there's no way to store that symbol for later use.

bjorn3 commented 3 years ago

This is deliberate. The Symbol may borrow for example the name field from the mmapped object file. This is the case for the gimli backend. The gimli backend uses an LRU cache for those mmaps, so the Symbol may become invalid later on: https://github.com/rust-lang/backtrace-rs/blob/e7f61cc6b52791e0299bbd6ecfaa25e8ae02034a/src/symbolize/gimli.rs#L582

Diggsey commented 3 years ago

The Backtrace type can be resolved before it is formatted, but as a result of Symbol not being Clone, there's no way to do this with the raw API?

philipc commented 3 years ago

Changing Symbol to be Clone will require adding a lifetime parameter to Symbol (because the 'static is a lie, the real lifetime is shorter than that). Doing that isn't going to solve the problem you have because you still won't be able to store the symbol for later use.

alexcrichton commented 3 years ago

Yes unfortunately Symbol can't easily be Clone unless it has some sort of cow-like representation. What might make sense though is to do something like impl From<&Symbol> for BacktraceSymbol which already does the cow-like thing, and that should give you an owned struct to persist.

Diggsey commented 3 years ago

Yep 👍 I think I would also need a similar conversion for Frame in order to use the formatter?

alexcrichton commented 3 years ago

Yes I believe a similar conversion from &Frame to BacktraceFrame should be possible.