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

How to use backtrace after stabilisation? #262

Closed
Morganamilo opened this issue Aug 16, 2022 · 8 comments
Closed

How to use backtrace after stabilisation? #262

Morganamilo opened this issue Aug 16, 2022 · 8 comments

Comments

@Morganamilo
Copy link

I can't quite figure out how to use backtrace now that it's stable.

The backtrace method is gone from the error type, however looking at the docs it says:

If using the nightly channel, or stable with features = ["backtrace"], a backtrace is captured and printed with the error if the underlying error type does not already provide its own. In order to see backtraces, they must be enabled through the environment variables described in std::backtrace:

This seems to imply it prints automatically now but it does not. Searching the docs for backtrace doesn't give any results.

@ArekPiekarz
Copy link
Contributor

Which compiler version do you use? Backtrace is stabilized, but still only on nightly - it will arrive on the stable branch in Rust 1.65.

@Morganamilo
Copy link
Author

I'm using nightly, where it's currently stable.

@ArekPiekarz
Copy link
Contributor

I guess one would need to use the Provider API.

@Morganamilo
Copy link
Author

I don't get what this has to do with me printing a backtrace.

@dtolnay
Copy link
Owner

dtolnay commented Sep 5, 2022

It should still work as documented:

fn main() {
    let error = anyhow::Error::msg("huh");
    println!("{:?}", error);
}
$ RUST_LIB_BACKTRACE=1 cargo run

huh

Stack backtrace:
   0: anyhow::error::<impl anyhow::Error>::msg
             at anyhow-1.0.64/src/error.rs:83:36
   1: repro::main
             at ./src/main.rs:2:17
   2: core::ops::function::FnOnce::call_once
             at /rustc/289279de116707f28cf9c18e4bbb8c6ec84ad75b/library/core/src/ops/function.rs:248:5

  13: std::rt::lang_start_internal
             at /rustc/289279de116707f28cf9c18e4bbb8c6ec84ad75b/library/std/src/rt.rs:148:20
  14: std::rt::lang_start
             at /rustc/289279de116707f28cf9c18e4bbb8c6ec84ad75b/library/std/src/rt.rs:165:17

  18: _start

or using println!("{:#?}", error.backtrace()):

Backtrace [
    { fn: "anyhow::error::<impl anyhow::Error>::msg", file: "anyhow-1.0.64/src/error.rs", line: 83 },
    { fn: "testing::main", file: "./src/main.rs", line: 2 },
    { fn: "core::ops::function::FnOnce::call_once", file: "/rustc/289279de116707f28cf9c18e4bbb8c6ec84ad75b/library/core/src/ops/function.rs", line: 248 },

    { fn: "std::rt::lang_start_internal", file: "/rustc/289279de116707f28cf9c18e4bbb8c6ec84ad75b/library/std/src/rt.rs", line: 148 },
    { fn: "std::rt::lang_start", file: "/rustc/289279de116707f28cf9c18e4bbb8c6ec84ad75b/library/std/src/rt.rs", line: 165 },

    { fn: "_start" },
]

@dtolnay
Copy link
Owner

dtolnay commented Sep 5, 2022

Be mindful that a specific anyhow version is specific to one particular iteration of the unstable backtrace API, so for the backtrace API that's used by rustc nightly-2022-08-13 or newer, you would need to be using anyhow 1.0.63 or newer.

@dtolnay dtolnay closed this as completed Sep 5, 2022
@Morganamilo
Copy link
Author

Hey so I'm not home right now so I can't quite test this. But my code doesn't just println the error. I manually unwind the context to print it how I want. I would like some way to also print the backtrace manually.

@dtolnay
Copy link
Owner

dtolnay commented Sep 5, 2022

Then #262 (comment) is the answer to that. For example using https://doc.rust-lang.org/nightly/core/any/fn.request_ref.html:

#![feature(provide_any)]

use std::any;
use std::backtrace::Backtrace;

fn main() {
    let error = anyhow::Error::msg("huh");
    println!("{:#?}", error.backtrace());
    for cause in error.chain().skip(1) {
        if let Some(backtrace) = any::request_ref::<Backtrace>(cause) {
            println!("{:#?}", backtrace);
        }
    }
}

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

No branches or pull requests

3 participants