RUST_BACKTRACE应该告诉我什么? [英] What is RUST_BACKTRACE supposed to tell me?

查看:92
本文介绍了RUST_BACKTRACE应该告诉我什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序很恐慌,所以我按照它的建议运行了 RUST_BACKTRACE = 1 ,我明白了(只是一小段).

My program is panicking so I followed its advice to run RUST_BACKTRACE=1 and I get this (just a little snippet).

1: 0x800c05b5 - std::sys::imp::backtrace::tracing::imp::write::hf33ae72d0baa11ed
            at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:42

2: 0x800c22ed - std::panicking::default_hook::{{closure}}::h59672b733cc6a455
            at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libstd/panicking.rs:351

如果程序出现紧急情况,它将停止整个程序,那么我在哪里可以找出发生恐慌的那一行呢?

If the program panics it stops the whole program, so where can I figure out at which line it's panicking on?

此行是否告诉我42行和351行有问题?

Is this line telling me there is a problem at line 42 and line 351?

整个回溯都在这张图片上,我觉得将其复制并粘贴到此处很麻烦.

The whole backtrace is on this image, I felt it would be to messy to copy and paste it here.

我从没听说过堆栈跟踪或反向跟踪.我正在编译警告,但是我不知道什么是调试符号.

I've never heard of a stack trace or a back trace. I'm compiling with warnings, but I don't know what debugging symbols are.

推荐答案

什么是堆栈跟踪?

如果您的程序出现紧急情况,您遇到了一个错误并想修复它;堆栈跟踪希望在这里为您提供帮助.发生紧急情况时,您想知道发生紧急情况的原因(触发紧急情况的功能).但是直接引发恐慌的功能通常不足以真正了解正在发生的事情.因此,我们还打印了调用先前函数的函数...,依此类推.我们回溯导致 main()恐慌的所有函数调用,这是(几乎)被调用的第一个函数.

What is a stack trace?

If your program panics, you encountered a bug and would like to fix it; a stack trace wants to help you here. When the panic happens, you would like to know the cause of the panic (the function in which the panic was triggered). But the function directly triggering the panic is usually not enough to really see what's going on. Therefore we also print the function that called the previous function... and so on. We trace back all function calls leading to the panic up to main() which is (pretty much) the first function being called.

当编译器生成机器代码时,它几乎只需要为CPU发出指令.问题在于,几乎不可能快速查看一组指令来自哪个Rust函数.因此,编译器可以将其他信息插入可执行文件中,该信息会被CPU忽略,但调试工具会使用这些信息.

When the compiler generates the machine code, it pretty much only needs to emit instructions for the CPU. The problem is that it's virtually impossible to quickly see from which Rust-function a set of instructions came. Therefore the compiler can insert additional information into the executable that is ignored by the CPU, but is used by debugging tools.

一个重要的部分是文件位置:编译器注释哪个指令来自哪个文件,位于哪一行.这也意味着我们以后可以看到定义了特定功能的位置.如果没有调试符号,就不会.

One important part are file locations: the compiler annotates which instruction came from which file at which line. This also means that we can later see where a specific function is defined. If we don't have debug symbols, we can't.

在堆栈跟踪中,您可以看到一些文件位置:

In your stack trace you can see a few file locations:

1: 0x800c05b5 - std::sys::imp::backtrace::tracing::imp::write::hf33ae72d0baa11ed
        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:42

Rust标准库附带调试符号.这样,我们可以看到函数的定义位置( gcc_s.rs 第42行).

The Rust standard library is shipped with debug symbols. As such, we can see where the function is defined (gcc_s.rs line 42).

如果在调试模式下编译( rustc cargo build ),则默认情况下会激活调试符号.但是,如果以发布模式( rustc -O cargo build --release )进行编译,则调试符号默认情况下处于禁用状态,因为它们会增加可执行文件的大小.通常对于最终用户而言并不重要.您可以在特定的Cargo.toml 中调整是否要调试符号.rel ="noreferrer"> 配置文件部分,并带有 debug 键.

If you compile in debug mode (rustc or cargo build), debug symbols are activated by default. If you, however, compile in release mode (rustc -O or cargo build --release), debug symbols are disabled by default as they increase the executable size and... usually aren't important for the end user. You can tweak whether or not you want debug symbols in your Cargo.toml in a specific profile section with the debug key.

当您第一次查看堆栈跟踪时,可能会被看到的所有奇怪的函数名称所迷惑.别担心,这很正常!您对您的代码的哪一部分触发了紧急情况很感兴趣,但是堆栈跟踪显示了涉及到的所有功能.在您的示例中,您可以忽略前9个条目:这些只是处理恐慌并生成您所看到的确切消息的函数.

When you first look at a stack trace you might be confused by all the strange function names you're seeing. Don't worry, this is normal! You are interested in what part of your code triggered the panic, but the stack trace shows all functions somehow involved. In your example, you can ignore the first 9 entries: those are just functions handling the panic and generating the exact message you are seeing.

条目10仍然不是您的代码,但也可能很有趣:恐慌是在 Vec< T> index()函数中触发的当您使用 [] 运算符时.最后,条目11显示了您定义的函数.但是您可能已经注意到,该条目缺少文件位置...上一节介绍了解决方法.

Entry 10 is still not your code, but might be interesting as well: the panic was triggered in the index() function of Vec<T> which is called when you use the [] operator. And finally, entry 11 shows a function you defined. But you might have noticed that this entry is missing a file location... the above section describes how to fix that.

  1. 激活调试符号(如果尚未激活)(例如,仅在调试模式下编译).
  2. 忽略堆栈跟踪顶部的 std core 中的所有功能.
  3. 查看您定义的第一个函数,在文件中找到相应的位置并修复错误.
  4. 如果还没有,请将所有 camelCase 函数和方法名称更改为 snake_case 以遵守社区范围的样式指南.
  1. Activate debug symbols if you haven't already (e.g. just compile in debug mode).
  2. Ignore any functions from std and core at the top of the stack trace.
  3. Look at the first function you defined, find the corresponding location in your file and fix the bug.
  4. If you haven't already, change all camelCase function and method names to snake_case to stick to the community wide style guide.

这篇关于RUST_BACKTRACE应该告诉我什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆