【问题标题】:Debugging fail! in library调试失败!在图书馆
【发布时间】:2014-05-24 01:39:01
【问题描述】:

我一直在构建我的代码,这样我就有了一个库,然后我可以在另一个文件中使用它。这一直运行良好,除了今天我遇到fail! 宏引发的错误(索引超出范围)。 具体来说,我不知道它们是从哪里抛出的。报告说当我使用extern crate blah = "blabla" 加载库时它们被抛出,但事实并非如此,因为当我在测试文件中注释掉一些函数调用时一切运行正常。

我的一个想法是手动包含所有文件,根本不使用该库。这看起来很 hacky...Gdb 也没有帮助(尽管有 -g 标志)。

谢谢!

编辑:

抱歉,不清楚。这是一个运行时问题。我已经模拟了一些重现此错误的示例代码。

库文件:

#![crate_id = "that"]
#![crate_type = "rlib"]

pub mod thing {
  fn a() {
    let v = vec!(1f32);
    println!("{}", v.get(3));
  }

  pub fn b() {
    a();
  }
}

Bin 文件:

extern crate that;

fn main() {
  that::thing::b();
}

编译运行时出现如下错误:

task '<main>' failed at 'index out of bounds: the len is 1 but the index is 3', lib.rs:1

虽然此报告确实正确,但行号/位置具有误导性。对于这个简单的问题,很容易看出问题所在。对于较大的文件,情况并非如此。

编辑 2

使用 RUST_BACKTRACE=1 运行的结果:

>>>>>>> RUST_BACKTRACE=1 ./bin 

task '<main>' failed at 'index out of bounds: the len is 1 but the index is 3', lib.rs:1
stack backtrace:
   1:           0x4661d0 - rt::backtrace::imp::write::hfe294d72359867edAZy::v0.11.0.pre
   2:           0x44d850 - rt::unwind::begin_unwind_inner::h0c8451a5a31731e8Hzy::v0.11.0.pre
   3:           0x44cd30 - rt::unwind::begin_unwind_fmt::h2e6bc3145b6b3fb9Ryy::v0.11.0.pre
   4:           0x4660e0 - rust_begin_unwind
   5:           0x4a75d0 - failure::begin_unwind::h3bb6514279e9a504IOv::v0.11.0.pre
   6:           0x4a7610 - failure::fail_bounds_check::he1ed5c80925626c6IMv::v0.11.0.pre
   7:           0x44c810 - vec::Vec<T>::get::h5370221266733407395::v0.0
   8:           0x44b0f0 - thing::a::h3f669f2dfb075f3cfaa::v0.0
   9:           0x44cd00 - thing::b::h8fc1d5bafb3ab544rba::v0.0
  10:           0x4032b0 - main::he270e826329bbcf4faa::v0.0
  11:           0x44af90 - start::closure.7862
  12:           0x4617c0 - rt::task::Task::run::closure.25316
  13:           0x46db00 - rust_try
  14:           0x461710 - rt::task::Task::run::hb455d17a166d559b4ow::v0.11.0.pre
  15:           0x44abd0 - start::h58e604f51e8fd72cipe::v0.11.0.pre
  16:           0x44ab40 - lang_start::h90f473af132f8958Coe::v0.11.0.pre
  17:           0x4032e0 - main
  18:     0x7ff54744df10 - __libc_start_main
  19:           0x4031b0 - <unknown>
  20:                0x0 - <unknown>

【问题讨论】:

  • 您的代码编译失败还是运行失败?请提供完整的控制台输出。
  • 您可能想尝试使用RUST_BACKTRACE=1 运行您的程序以获得完整的堆栈跟踪。
  • 刚刚添加了示例文件和控制台输出。很抱歉我的原始帖子缺乏具体性和延迟。
  • @BurntSushi5 RUST_BACKTRACE=1 是一个很好的技巧。它确实告诉我有问题的功能,但没有行号,而且有点神秘。已编辑结果。
  • @luke:第 7 帧和第 8 帧告诉你这里需要知道的,它是函数 a 中的 .get 调用;我猜您更大示例中的功能可能更大,因此追踪确切原因更复杂?

标签: debugging exception rust


【解决方案1】:

要在发生恐慌时对其进行调试,请在 rust_panic 函数上设置断点。

b rust_panic

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 2019-11-03
    • 2022-06-10
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    • 2022-07-20
    相关资源
    最近更新 更多