调试器属性

以下 属性 用于在使用 GDB 或 WinDbg 等第三方调试器时增强调试体验。

debugger_visualizer 属性

debugger_visualizer 属性 可用于将调试器可视化工具文件嵌入到调试信息中。这可以改进在调试器中显示值的调试体验。

它使用 MetaListNameValueStr 语法来指定其输入,并且必须指定为 crate 属性。

debugger_visualizer 与 Natvis 结合使用

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

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

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

#![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 的 美化打印文档

当在 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 键嵌入,该路径是相对于 crate 源文件的路径。

#![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.
1

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

collapse_debuginfo 属性

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

该属性使用 MetaListIdents 语法来指定其输入,并且只能应用于宏定义。

接受的选项

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

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

注意rustc 有一个 -C collapse-macro-debuginfo CLI 选项,用于覆盖默认的折叠行为和 #[collapse_debuginfo] 属性。

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