19

当发生错误时, DoingRUST_BACKTRACE=1 cargo run会给出堆栈跟踪,如下所示。有什么方法可以获取文件和行号而不是十六进制地址?

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
4

2 回答 2

9

有一个合并到主要 rust 存储库中的拉取请求,它将文件名和行号添加到回溯。据我所知,它是 rust 1.0.0 稳定版本的一部分。

您必须启用回溯并使用货物配置文件构建可执行文件,其中包括调试符号到可执行文件中(debug = true在货物清单中带有选项)。AFAIKcargo run现在默认使用调试配置文件。

这是带有文件名和行号的示例跟踪输出:

[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

不幸的是,这在 MacOS X 等一些平台上被破坏了。在 rust github 问题跟踪器中有关于此的未解决问题。

于 2015-12-29T08:13:24.313 回答
1

如果您进入 deps 子目录并从那里运行程序,您现在将在 OSX 中获得行号。

这是由于这个未解决的问题,希望很快得到解决: https ://github.com/rust-lang/cargo/issues/4490

于 2017-10-04T07:14:32.753 回答