Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

调试器属性

以下属性用于增强使用第三方调试器(如 GDB 或 WinDbg)时的调试体验。

debugger_visualizer 属性

*debugger_visualizer 属性*可用于将调试器可视化文件嵌入调试信息中。这在显示值时改善调试器体验。

Example

#![debugger_visualizer(natvis_file = "Example.natvis")]
#![debugger_visualizer(gdb_script_file = "example.py")]

debugger_visualizer 属性使用 MetaListNameValueStr 语法指定其输入。必须指定以下键之一:

debugger_visualizer 属性只能应用于模块或 crate 根。

debugger_visualizer 属性可以在一个形式上使用任意次数。所有指定的可视化文件都将被加载。

debugger_visualizer 与 Natvis 一起使用

Natvis 是 Microsoft 调试器(如 Visual Studio 和 WinDbg)的基于 XML 的框架,使用声明性规则自定义类型的显示。有关 Natvis 格式的详细信息,请参阅 Microsoft 的 Natvis 文档

此属性仅支持在 -windows-msvc 目标上嵌入 Natvis 文件。

Natvis 文件的路径使用 natvis_file 键指定,该键是相对于源文件的路径。

Example

#![debugger_visualizer(natvis_file = "Rectangle.natvis")]

struct FancyRect {
    x: f32,
    y: f32,
    dx: f32,
    dy: f32,
}

fn main() {
    let fancy_rect = FancyRect { x: 10.0, y: 10.0, dx: 5.0, dy: 5.0 };
    println!("set breakpoint here");
}

Rectangle.natvis 包含:

<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
    <Type Name="foo::FancyRect">
      <DisplayString>({x},{y}) + ({dx}, {dy})</DisplayString>
      <Expand>
        <Synthetic Name="LowerLeft">
          <DisplayString>({x}, {y})</DisplayString>
        </Synthetic>
        <Synthetic Name="UpperLeft">
          <DisplayString>({x}, {y + dy})</DisplayString>
        </Synthetic>
        <Synthetic Name="UpperRight">
          <DisplayString>({x + dx}, {y + dy})</DisplayString>
        </Synthetic>
        <Synthetic Name="LowerRight">
          <DisplayString>({x + dx}, {y})</DisplayString>
        </Synthetic>
      </Expand>
    </Type>
</AutoVisualizer>

在 WinDbg 下查看时,fancy_rect 变量将显示如下:

> Variables:
  > fancy_rect: (10.0, 10.0) + (5.0, 5.0)
    > LowerLeft: (10.0, 10.0)
    > UpperLeft: (10.0, 15.0)
    > UpperRight: (15.0, 15.0)
    > LowerRight: (15.0, 10.0)

debugger_visualizer 与 GDB 一起使用

GDB 支持使用结构化的 Python 脚本,称为漂亮打印机,描述类型在调试器视图中应如何可视化。有关漂亮打印机的详细信息,请参阅 GDB 的漂亮打印文档

Note

在 GDB 下调试二进制文件时,嵌入的漂亮打印机不会自动加载。

有两种启用自动加载嵌入漂亮打印机的方法:

  1. 启动 GDB 时带额外参数以显式将目录或二进制文件添加到自动加载安全路径:gdb -iex "add-auto-load-safe-path safe-path path/to/binary" path/to/binary 有关更多信息,请参阅 GDB 的自动加载文档
  2. $HOME/.config/gdb 下创建名为 gdbinit 的文件(如果目录不存在,您可能需要创建它)。将以下行添加到该文件:add-auto-load-safe-path path/to/binary

这些脚本使用 gdb_script_file 键嵌入,该键是相对于源文件的路径。

Example

#![debugger_visualizer(gdb_script_file = "printer.py")]

struct Person {
    name: String,
    age: i32,
}

fn main() {
    let bob = Person { name: String::from("Bob"), age: 10 };
    println!("set breakpoint here");
}

printer.py 包含:

import gdb

class PersonPrinter:
    "Print a Person"

    def __init__(self, val):
        self.val = val
        self.name = val["name"]
        self.age = int(val["age"])

    def to_string(self):
        return "{} is {} years old.".format(self.name, self.age)

def lookup(val):
    lookup_tag = val.type.tag
    if lookup_tag is None:
        return None
    if "foo::Person" == lookup_tag:
        return PersonPrinter(val)

    return None

gdb.current_objfile().pretty_printers.append(lookup)

当 crate 的调试可执行文件传递给 GDB1 时,print bob 将显示:

"Bob" is 10 years old.

collapse_debuginfo 属性

*collapse_debuginfo [属性]*控制在为调用此宏的代码生成调试信息时,宏定义中的代码位置是否折叠为与宏调用位置关联的单个位置。

Example

#![allow(unused)]
fn main() {
#[collapse_debuginfo(yes)]
macro_rules! example {
    () => {
        println!("hello!");
    };
}
}

使用调试器时,调用 example 宏可能看起来像是在调用函数。也就是说,当您单步执行到调用位置时,它可能显示宏调用而不是展开的代码。

collapse_debuginfo 属性的语法是:

Syntax
CollapseDebuginfoAttributecollapse_debuginfo ( CollapseDebuginfoOption )

CollapseDebuginfoOption
      yes
    | no
    | external

collapse_debuginfo 属性只能应用于 macro_rules 定义

collapse_debuginfo 属性只能在宏上使用一次。

collapse_debuginfo 属性接受这些选项:

  • #[collapse_debuginfo(yes)] — 调试信息中的代码位置被折叠。
  • #[collapse_debuginfo(no)] — 调试信息中的代码位置不被折叠。
  • #[collapse_debuginfo(external)] — 仅当宏来自不同的 crate 时,调试信息中的代码位置才被折叠。

对于没有此属性的宏,external 行为是默认的,除非它们是内置宏。对于内置宏,默认值是 yes

Note

rustc 有一个 -C collapse-macro-debuginfo CLI 选项,可以覆盖默认行为和任何 #[collapse_debuginfo] 属性的值。


  1. 注意:这假设您正在使用 rust-gdb 脚本,该脚本为标准库类型(如 String)配置了漂亮打印机。