Skip to content

Commit 39a0be6

Browse files
committed
Updates and copy-edits for debugger_visualizer.
1 parent 4dee364 commit 39a0be6

File tree

2 files changed

+41
-74
lines changed

2 files changed

+41
-74
lines changed

src/attributes.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ The following is an index of all built-in attributes.
272272
- [`non_exhaustive`] — Indicate that a type will have more fields/variants
273273
added in future.
274274
- Debugger
275-
- [`debugger_visualizer`] — Embeds a file that specifies debugger output for a type
275+
- [`debugger_visualizer`] — Embeds a file that specifies debugger output for a type.
276276

277277
[Doc comments]: comments.md#doc-comments
278278
[ECMA-334]: https://www.ecma-international.org/publications/standards/Ecma-334.htm

src/attributes/debugger.md

+40-73
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,36 @@
11
# Debugger attributes
22

3-
The following [attributes] are used for enhancing the debugging experience when using third-party debuggers like GDB or LLDB.
3+
The following [attributes] are used for enhancing the debugging experience when using third-party debuggers like GDB or WinDbg.
44

55
## The `debugger_visualizer` attribute
66

7-
The `debugger_visualizer` attribute can be used to embed a debugger visualizer file into the debug information generated by `rustc`.
8-
This enables an improved debugger experience for types outside of Rust's standard library.
7+
The *`debugger_visualizer` attribute* can be used to embed a debugger visualizer file into the debug information.
8+
This enables an improved debugger experience for displaying values in the debugger.
9+
It uses the [_MetaListNameValueStr_] syntax to specify its inputs, and must be specified as a crate attribute.
910

1011
### Using `debugger_visualizer` with Natvis
1112

1213
Natvis is an XML-based framework for Microsoft debuggers (such as Visual Studio and WinDbg) that uses declarative rules to customize the display of types.
13-
A Natvis file is embedded using the `natvis-file` meta item.
1414
For detailed information on the Natvis format, refer to Microsoft's [Natvis documentation].
1515

16-
<div class="warning">
17-
Currently, this attribute only supports embedding Natvis files on `-windows-msvc` targets.
18-
</div>
16+
This attribute only supports embedding Natvis files on `-windows-msvc` targets.
1917

20-
Consider a crate with this directory structure:
21-
22-
```text
23-
/Cargo.toml
24-
/Rectangle.natvis
25-
+-- src
26-
+-- main.rs
27-
```
28-
29-
Where `main.rs` contains:
18+
The path to the Natvis file is specified with the `natvis_file` key, which is a path relative to the crate source file:
3019

20+
<!-- ignore: requires external files, and msvc -->
3121
```rust ignore
32-
#![debugger_visualizer(natvis_file = "../Rectangle.natvis")]
33-
mod fancy_rect {
34-
pub struct FancyRect {
35-
pub x: f32,
36-
pub y: f32,
37-
pub dx: f32,
38-
pub dy: f32,
39-
}
40-
41-
impl FancyRect {
42-
pub fn new(x: f32, y: f32, dx: f32, dy: f32) -> Self {
43-
FancyRect { x, y, dx, dy }
44-
}
45-
}
46-
}
22+
#![debugger_visualizer(natvis_file = "Rectangle.natvis")]
4723

48-
use fancy_rect::FancyRect;
24+
struct FancyRect {
25+
x: f32,
26+
y: f32,
27+
dx: f32,
28+
dy: f32,
29+
}
4930

5031
fn main() {
51-
let fancy_rect = FancyRect::new(10.0, 10.0, 5.0, 5.0);
52-
()
32+
let fancy_rect = FancyRect { x: 10.0, y: 10.0, dx: 5.0, dy: 5.0 };
33+
println!("set breakpoint here");
5334
}
5435
```
5536

@@ -58,7 +39,7 @@ and `Rectangle.natvis` contains:
5839
```xml
5940
<?xml version="1.0" encoding="utf-8"?>
6041
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
61-
<Type Name="Rectangle::FancyRect">
42+
<Type Name="foo::FancyRect">
6243
<DisplayString>({x},{y}) + ({dx}, {dy})</DisplayString>
6344
<Expand>
6445
<Synthetic Name="LowerLeft">
@@ -82,55 +63,38 @@ When viewed under WinDbg, the `fancy_rect` variable would be shown as follows:
8263

8364
```text
8465
> Variables:
85-
> fancy_rect: (10, 10) + (5, 5)
86-
> LowerLeft: (10, 10)
87-
> UpperLeft: (10, 15)
88-
> UpperRight: (15, 15)
89-
> LowerRight: (15, 10)
66+
> fancy_rect: (10.0, 10.0) + (5.0, 5.0)
67+
> LowerLeft: (10.0, 10.0)
68+
> UpperLeft: (10.0, 15.0)
69+
> UpperRight: (15.0, 15.0)
70+
> LowerRight: (15.0, 10.0)
9071
```
9172

9273
### Using `debugger_visualizer` with GDB
9374

9475
GDB supports the use of a structured Python script, called a *pretty printer*, that describes how a type should be visualized in the debugger view.
95-
These scripts are embedded using the `gdb_script_file` meta item.
96-
For detailed information on pretty printers, refer to GDB's [pretty print documentation].
76+
For detailed information on pretty printers, refer to GDB's [pretty printing documentation].
9777

9878
Embedded pretty printers are not automatically loaded when debugging a binary under GDB.
9979
There are two ways to enable auto-loading embedded pretty printers:
100-
1. Launch GDB with extra arguments to explicitly add a directory or binary to the auto-load safe path: `gdb -iex "set auto-load safe-path path/to/binary" path/to/binary` (For more information, see GDB's [auto-loading documentation])
80+
1. Launch GDB with extra arguments to explicitly add a directory or binary to the auto-load safe path: `gdb -iex "add-auto-load-safe-path safe-path path/to/binary" path/to/binary`
81+
For more information, see GDB's [auto-loading documentation].
10182
1. Create a file named `gdbinit` under `$HOME/.config/gdb` (you may need to create the directory if it doesn't already exist). Add the following line to that file: `add-auto-load-safe-path path/to/binary`.
10283

103-
Consider a crate called `PersonPrinter` with this directory structure:
104-
105-
```text
106-
/Cargo.toml
107-
/printer.py
108-
+-- src
109-
+-- main.rs
110-
```
111-
112-
Where `main.rs` contains:
84+
These scripts are embedded using the `gdb_script_file` key, which is a path relative to the crate source file.
11385

86+
<!-- ignore: requires external files -->
11487
```rust ignore
115-
#![debugger_visualizer(gdb_script_file = "../printer.py")]
116-
mod person {
117-
pub struct Person {
118-
pub name: String,
119-
pub age: i32,
120-
}
121-
122-
impl Person {
123-
pub fn new(name: String, age: i32) -> Self {
124-
Person { name, age }
125-
}
126-
}
127-
}
88+
#![debugger_visualizer(gdb_script_file = "printer.py")]
12889

129-
use person::Person;
90+
struct Person {
91+
name: String,
92+
age: i32,
93+
}
13094

13195
fn main() {
132-
let bob = Person::new(String::from("Bob"), 10);
133-
()
96+
let bob = Person { name: String::from("Bob"), age: 10 };
97+
println!("set breakpoint here");
13498
}
13599
```
136100

@@ -154,21 +118,24 @@ def lookup(val):
154118
lookup_tag = val.type.tag
155119
if lookup_tag is None:
156120
return None
157-
if "PersonPrinter::person::Person" == lookup_tag:
121+
if "foo::Person" == lookup_tag:
158122
return PersonPrinter(val)
159123

160124
return None
161125

162126
gdb.current_objfile().pretty_printers.append(lookup)
163127
```
164128

165-
When the crate's debug executable is passed into GDB, `print bob` will display:
129+
When the crate's debug executable is passed into GDB[^rust-gdb], `print bob` will display:
166130

167131
```text
168132
"Bob" is 10 years old.
169133
```
170134

135+
[^rust-gdb]: Note: This assumes you are using the `rust-gdb` script which configures pretty-printers for standard library types like `String`.
136+
171137
[auto-loading documentation]: https://sourceware.org/gdb/onlinedocs/gdb/Auto_002dloading-safe-path.html
172138
[attributes]: ../attributes.md
173139
[Natvis documentation]: https://docs.microsoft.com/en-us/visualstudio/debugger/create-custom-views-of-native-objects
174-
[pretty print documentation]: https://sourceware.org/gdb/onlinedocs/gdb/Pretty-Printing.html
140+
[pretty printing documentation]: https://sourceware.org/gdb/onlinedocs/gdb/Pretty-Printing.html
141+
[_MetaListNameValueStr_]: ../attributes.md#meta-item-attribute-syntax

0 commit comments

Comments
 (0)