Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Enable backtraces for agent errors #2437

Merged
merged 3 commits into from
Sep 30, 2022
Merged

Conversation

ranweiler
Copy link
Member

Enable the backtrace feature of anyhow, our dynamic catch-all error library. This lets us include backtraces (on stable rustc) in our ?-propagated errors (as long as RUST_BACKTRACE is set, which we do in runtime-tools).

Example error without the backtrace feature:

Error: loading path: i-dont-exist.txt

Caused by:
    The system cannot find the file specified. (os error 2)
error: process didn't exit successfully: `target\debug\anyhow-backtrace-example.exe i-dont-exist.txt` (exit code: 1)

With:

Error: loading path: i-dont-exist.txt

Caused by:
    The system cannot find the file specified. (os error 2)

Stack backtrace:
   0: std::backtrace::Backtrace::create
             at /rustc/a37499ae66ec5fc52a93d71493b78fb141c32f6b/library\std\src\backtrace.rs:333
   1: std::backtrace::Backtrace::capture
             at /rustc/a37499ae66ec5fc52a93d71493b78fb141c32f6b/library\std\src\backtrace.rs:298
   2: anyhow::context::ext::impl$0::ext_context<std::io::error::Error,alloc::string::String>
             at C:\dev\.cargo\registry\src\github.com-1ecc6299db9ec823\anyhow-1.0.65\src\context.rs:27
   3: anyhow::context::impl$0::with_context::closure$0<alloc::vec::Vec<u8,alloc::alloc::Global>,std::io::error::Error,alloc::string::String,anyhow_backtrace_example::load_utf8::closure_env$0<alloc::string::String> >
             at C:\dev\.cargo\registry\src\github.com-1ecc6299db9ec823\anyhow-1.0.65\src\context.rs:58
   4: enum2$<core::result::Result<alloc::vec::Vec<u8,alloc::alloc::Global>,std::io::error::Error> >::map_err<alloc::vec::Vec<u8,alloc::alloc::Global>,std::io::error::Error,anyhow::Error,anyhow::context::impl$0::with_context::closure_env$0<alloc::vec::Vec<u8,all
             at /rustc/a37499ae66ec5fc52a93d71493b78fb141c32f6b\library\core\src\result.rs:855
   5: anyhow::context::impl$0::with_context<alloc::vec::Vec<u8,alloc::alloc::Global>,std::io::error::Error,alloc::string::String,anyhow_backtrace_example::load_utf8::closure_env$0<alloc::string::String> >
             at C:\dev\.cargo\registry\src\github.com-1ecc6299db9ec823\anyhow-1.0.65\src\context.rs:58
   6: anyhow_backtrace_example::load_utf8<alloc::string::String>
             at .\src\main.rs:17
   7: anyhow_backtrace_example::main
             at .\src\main.rs:8
   8: core::ops::function::FnOnce::call_once<enum2$<core::result::Result<tuple$<>,anyhow::Error> > (*)(),tuple$<> >
             at /rustc/a37499ae66ec5fc52a93d71493b78fb141c32f6b\library\core\src\ops\function.rs:248
   9: std::sys_common::backtrace::__rust_begin_short_backtrace<enum2$<core::result::Result<tuple$<>,anyhow::Error> > (*)(),enum2$<core::result::Result<tuple$<>,anyhow::Error> > >
             at /rustc/a37499ae66ec5fc52a93d71493b78fb141c32f6b\library\std\src\sys_common\backtrace.rs:122
  10: std::rt::lang_start::closure$0<enum2$<core::result::Result<tuple$<>,anyhow::Error> > >
             at /rustc/a37499ae66ec5fc52a93d71493b78fb141c32f6b\library\std\src\rt.rs:166
  11: core::ops::function::impls::impl$2::call_once
             at /rustc/a37499ae66ec5fc52a93d71493b78fb141c32f6b/library\core\src\ops\function.rs:283
  12: std::panicking::try::do_call
             at /rustc/a37499ae66ec5fc52a93d71493b78fb141c32f6b/library\std\src\panicking.rs:492
  13: std::panicking::try
             at /rustc/a37499ae66ec5fc52a93d71493b78fb141c32f6b/library\std\src\panicking.rs:456
  14: std::panic::catch_unwind
             at /rustc/a37499ae66ec5fc52a93d71493b78fb141c32f6b/library\std\src\panic.rs:137
  15: std::rt::lang_start_internal::closure$2
             at /rustc/a37499ae66ec5fc52a93d71493b78fb141c32f6b/library\std\src\rt.rs:148
  16: std::panicking::try::do_call
             at /rustc/a37499ae66ec5fc52a93d71493b78fb141c32f6b/library\std\src\panicking.rs:492
  17: std::panicking::try
             at /rustc/a37499ae66ec5fc52a93d71493b78fb141c32f6b/library\std\src\panicking.rs:456
  18: std::panic::catch_unwind
             at /rustc/a37499ae66ec5fc52a93d71493b78fb141c32f6b/library\std\src\panic.rs:137
  19: std::rt::lang_start_internal
             at /rustc/a37499ae66ec5fc52a93d71493b78fb141c32f6b/library\std\src\rt.rs:148
  20: std::rt::lang_start<enum2$<core::result::Result<tuple$<>,anyhow::Error> > >
             at /rustc/a37499ae66ec5fc52a93d71493b78fb141c32f6b\library\std\src\rt.rs:165
  21: main
  22: invoke_main
             at D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:78
  23: __scrt_common_main_seh
             at D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
  24: BaseThreadInitThunk
  25: RtlUserThreadStart
error: process didn't exit successfully: `target\debug\anyhow-backtrace-example.exe i-dont-exist.txt` (exit code: 1)

There's a lot of stuff in the backtrace, but note how it includes the failure sites in the code in these frames:

   6: anyhow_backtrace_example::load_utf8<alloc::string::String>
             at .\src\main.rs:17
   7: anyhow_backtrace_example::main
             at .\src\main.rs:8

@ranweiler ranweiler merged commit e77a87a into microsoft:main Sep 30, 2022
@ranweiler ranweiler deleted the agent-backtrace branch September 30, 2022 17:13
@ghost ghost locked as resolved and limited conversation to collaborators Oct 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants