rust-lang / backtrace-rs

Backtraces in Rust

Home Page:https://docs.rs/backtrace

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Backtrace and line numbers in Docker

AxelJunker opened this issue · comments

I'm using anyhow = { version = "1.0.53", features = ["backtrace"] } which uses this library to provide backtraces on errors. When running with RUST_BACKTRACE=1 cargo run I get nice backtraces pointing me to line numbers where the error originated from:

...
 6: my_project::my_function::{{closure}}
             at ./src/main.rs:84:16
...

However, when running in Docker I get a different (useless) backtrace:

Stack backtrace:
   0: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
   1: tokio::park::thread::CachedParkThread::block_on
   2: tokio::runtime::thread_pool::ThreadPool::block_on
   3: tokio::runtime::Runtime::block_on
   4: my_project::main
   5: std::sys_common::backtrace::__rust_begin_short_backtrace
   6: std::rt::lang_start::{{closure}}
   7: core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once
             at ./rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/core/src/ops/function.rs:259:13
      std::panicking::try::do_call
             at ./rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/panicking.rs:406:40
      std::panicking::try
             at ./rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/panicking.rs:370:19
      std::panic::catch_unwind
             at ./rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/panic.rs:133:14
      std::rt::lang_start_internal::{{closure}}
             at ./rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/rt.rs:128:48
      std::panicking::try::do_call
             at ./rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/panicking.rs:406:40
      std::panicking::try
             at ./rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/panicking.rs:370:19
      std::panic::catch_unwind
             at ./rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/panic.rs:133:14
      std::rt::lang_start_internal
             at ./rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/rt.rs:128:20
   8: main
   9: __libc_start_main
  10: _start

If I put a regular panic!("panik") anywhere in my code, I get the line number of the panic:

thread 'main' panicked at 'panik', src/main.rs:82:5
...

Any ideas on how to get better backtraces with line numbers in Docker?

How do you build it in docker? Do you use --release? That disables debuginfo generation which means that backtrace-rs can't find the location of every call frame anymore.

Ah, yes I do use --release. Thanks!

You can add

[profile.release]
debug = 1

to Cargo.toml to enable enough debuginfo for showing line numbers in backtraces. You can set debug to 2 or "true" if you want to inspect variables in a debugger.