Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stack trace with line numbers when running cargo run

Doing RUST_BACKTRACE=1 cargo run gives a stack trace when an error occurs as shown below. Is there any way to get the file & line number instead of the hex address?

thread '<main>' panicked at 'called `Result::unwrap()` on an `Err` value: Parsing: invalid encoding byte', /Users/rustbuild/src/rust-buildbot/slave/stable-dist-rustc-mac/build/src/libcore/result.rs:729
stack backtrace:
   1:        0x104c9403f - sys::backtrace::write::h7807ec07859fb503t1r
   2:        0x104c980e4 - panicking::on_panic::ha0ed2b9b562a7f9ctZv
   3:        0x104c7c4d5 - rt::unwind::begin_unwind_inner::hbfb8d99cb52be7a1cHv
   4:        0x104c7cd66 - rt::unwind::begin_unwind_fmt::hac7eda7c3f3b8498QFv
   5:        0x104c979bc - rust_begin_unwind
   6:        0x104cb75f5 - panicking::panic_fmt::h051633da0da2e362wwy
   7:        0x104be0877 - result::Result<T, E>::unwrap::h15040486031244389916
   8:        0x104bdc6f1 - main::h393644ca2d1fdb82uLa
   9:        0x104c99e18 - rust_try_inner
  10:        0x104c99e05 - rust_try
  11:        0x104c988e8 - rt::lang_start::h5324dae87dacdac8YTv
  12:        0x104be500e - main
An unknown error occurred
like image 604
Bilal Syed Hussain Avatar asked May 28 '15 20:05

Bilal Syed Hussain


People also ask

What causes stack trace?

Usually, a stack trace is shown when an Exception is not handled correctly in code. (An exception is what a runtime environment uses to tell you that there's an error in your code.) This may be one of the built-in Exception types, or a custom Exception created by a program or a library.

How do you analyze stack trace?

Analyze external stack tracesFrom the main menu, select Code | Analyze Stack Trace or Thread Dump. In the Analyze Stack Trace dialog that opens, paste the external stack trace or thread dump into the Put a stacktrace here: text area. Click OK. The stack trace is displayed in the Run tool window.

What is the difference between call stack and stack trace?

A call stack is typically "the current stack of operations" - i.e. while it's running. A stack trace is typically a copy of the call stack which is logged at some sort of failure, e.g. an exception.


1 Answers

There is a pull request merged into main rust repo which adds file names and line numbers to backtrace. As far as I can see it was a part of rust 1.0.0 stable release.

You have to enable backtraces and build executable using cargo profile which includes debug symbols into executable (with debug = true option in cargo manifest). AFAIK cargo run is using debug profile by default now.

Here is example trace output with file names and line numbers:

[user@salikhov ~/workspace/mqtt-rust $ RUST_BACKTRACE=1 cargo run
   Compiling mqtt v0.1.0 (file:///home/user/workspace/mqtt-rust)

     Running `target/debug/mqtt`
thread '<main>' panicked at 'I want line numbers!', src/proto/client.rs:33
stack backtrace:
   1:     0x7ff049fa47d9 - sys::backtrace::tracing::imp::write::he18882fa84e6b00ePnt
   2:     0x7ff049fa39b8 - panicking::on_panic::h495226a97f084523enx
   3:     0x7ff049f9dcce - sys_common::unwind::begin_unwind_inner::h7a4ee06c0d57e26affs
   4:     0x7ff049f95f47 - sys_common::unwind::begin_unwind::h13029855766851973181
                        at ../src/libstd/sys/common/unwind/mod.rs:232
   5:     0x7ff049f95e8a - proto::client::MqttConnection::connect::h633d3d42c15a3dedgYa
                        at /home/user/workspace/mqtt-rust/<std macros>:3
   6:     0x7ff049f80416 - main::h1d77c75265710f92gaa
                        at src/main.rs:5
   7:     0x7ff049fa6084 - sys_common::unwind::try::try_fn::h4848098439110500489
   8:     0x7ff049fa3098 - __rust_try
   9:     0x7ff049fa5cf8 - rt::lang_start::hcf64c98c1a7c0031Zkx
  10:     0x7ff049f834f6 - main
  11:     0x7ff049170ec4 - __libc_start_main
  12:     0x7ff049f802a8 - <unknown>
  13:                0x0 - <unknown>
An unknown error occurred

Unfortunately, this is broken on some platforms like MacOS X. There is open issue about this in rust github issue tracker.

like image 186
Konstantin V. Salikhov Avatar answered Oct 03 '22 12:10

Konstantin V. Salikhov