Skip to content

Commit c9b1b84

Browse files
Respond to PR comments. Minor updates to how GDB scripts would be embedded to the .debug_gdb_scripts section. Updates to specify the #[debugger_visualizer] targets modules and can be used with or without the leading ! for the attribute.
1 parent 27c1664 commit c9b1b84

File tree

1 file changed

+52
-18
lines changed

1 file changed

+52
-18
lines changed

text/0000-debugger-visualizer.md

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ As an example, consider a crate `foo` with this directory structure:
221221
Where `main.rs` contains:
222222

223223
```rust
224-
#[debugger_visualizer(natvis_file = "../Foo.natvis")]
224+
#![debugger_visualizer(natvis_file = "../Foo.natvis")]
225225

226226
/// A rectangle in first quadrant
227227
struct FancyRect {
@@ -286,13 +286,14 @@ Rust developers can add one or more pretty printers to their crate. This is done
286286
in the Rust compiler via `.py` python scripts. Through the use of a new Rust attribute,
287287
`#[debugger_visualizer]`, the compiler will encode the contents of the `.py` file in
288288
the crate metadata if the target is an `rlib`. If the target is an executable, the
289-
`.debug_gdb_scripts` section will include a reference to the pretty printer specified.
289+
`.debug_gdb_scripts` section will include a reference to the pretty printer specified
290+
by embedding the contents of the pretty printer directly into this section.
290291

291292
To provide pretty printers, developers create a file with the `.py` file
292293
extension and reference it via the `#[debugger_visualizer]` attribute as follows:
293294

294295
```rust
295-
#[debugger_visualizer(gdb_script_file = "../foo.py")]
296+
#![debugger_visualizer(gdb_script_file = "../foo.py")]
296297
```
297298

298299
# Reference-level explanation
@@ -301,7 +302,33 @@ extension and reference it via the `#[debugger_visualizer]` attribute as follows
301302
In rustc, a new built-in attribute `#[debugger_visualizer]` will be added which
302303
instructs the compiler to take the specified file path for a debugger visualizer
303304
and add it to the current binary being built. The file path specified must be
304-
relative to the location of the attribute.
305+
relative to the location of the attribute and is resolved in a manner that is
306+
identical to how paths are resolved in the `include_str!` macro. This attribute
307+
will directly target modules which means the syntax `#![debugger_visualizer]` is
308+
also valid when placed at the module level.
309+
310+
For example, the following uses of the attribute are valid:
311+
312+
Where `main.rs` contains:
313+
314+
```rust
315+
#![debugger_visualizer(natvis_file = "../main.natvis")]
316+
317+
#[debugger_visualizer(natvis_file = "../foo.natvis")]
318+
mod foo;
319+
```
320+
321+
and `bar.rs` contains:
322+
323+
```rust
324+
#![debugger_visualizer(natvis_file = "../bar.natvis")]
325+
```
326+
327+
In the first case, the attribute is applied to the crate as a top-level attribute
328+
using the inner attribute syntax and also added to the module foo using the outer
329+
attribute syntax. In the second case, the attribute is applied to the module bar
330+
via a top-level attribute as well which also is valid since it is still targeting
331+
a module.
305332

306333
The `#[debugger_visualizer]` attribute will reserve multiple keys to be able to
307334
specify which type of visualizer is being applied. The following keys will be
@@ -317,17 +344,17 @@ For example, to specify that a `.natvis` file should be included in the binary
317344
being built, the following attribute should be added to the Rust source:
318345

319346
```rust
320-
#[debugger_visualizer(natvis_file = "../foo.natvis")]
347+
#![debugger_visualizer(natvis_file = "../foo.natvis")]
321348
```
322349

323350
The same can be done to specify a GDB python debugger script:
324351

325352
```rust
326-
#[debugger_visualizer(gdb_script_file = "../foo.py")]
353+
#![debugger_visualizer(gdb_script_file = "../foo.py")]
327354
```
328355

329-
Depending on the Rust target, the correct debugger visualizer will be selected and embedded
330-
in the output.
356+
Depending on the Rust target, the correct debugger visualizer will be selected
357+
and embedded in the output.
331358

332359
The Rust compiler will serialize the contents of the file specified via the
333360
`#[debugger_visualizer]` attribute and store it in the crate metadata. This attribute
@@ -346,9 +373,13 @@ would generate a PDB would have all applicable `.natvis` files embedded.
346373
In the case of GDB pretty printer, `#[debugger_visualizer(gdb_script_file = "../foo.py")]`
347374
the compiler will ensure that the set of pretty printers specified will be added to the
348375
`.debug_gdb_scripts` section of the `ELF` generated. The `.debug_gdb_scripts` section
349-
takes a list of null-terminated entries which specify scripts to load within GDB. The
350-
Rust compiler currently embeds a visualizer for some types in the standard library via
351-
the `.debug_gdb_scripts` section.
376+
takes a list of null-terminated entries which specify scripts to load within GDB. This
377+
section supports listing files to load directly or embedding the contents of a script
378+
that will be executed. The Rust compiler currently embeds a visualizer for some types
379+
in the standard library via the `.debug_gdb_scripts` section using the former method.
380+
This attribute will embed the contents of the debugger script so that it will not
381+
need to reference a file in the search path. This has proven to be a more reliable
382+
route than depending on file paths which can be unstable at times.
352383

353384
The `CrateRoot` type would need to be updated to account for debugger visualizer
354385
files for crates within the dependency graph. The `CrateRoot` would contain
@@ -385,12 +416,9 @@ the set of visualizer files that were previously encoded and stored in the
385416
contents written to a new file in the `target` directory. In the case of Natvis,
386417
the path of this new file will be what is passed to the `/NATVIS` linker flag.
387418
For example, in a debug build, the contents of the `.natvis` files that were encoded
388-
in the crate metadata will be written to new files in the directory `target/debug/deps/visualizers`.
389-
Each visualizer file that is written will have a new name to ensure it is unique
390-
across visualizer files for all crates with a naming scheme of `<crate_name>-<hash>.<visualizer_extension>`.
391-
The `<visualizer_extension>` value will be `.natvis` in the case of a Natvis file
392-
and `.py` in the case of a pretty printer. The `<hash>` value will be the hash of the
393-
contents of the visualizer file.
419+
in the crate metadata will be written to new files in a temp directory where they will
420+
be included from. Each visualizer file that is written will have a new name to
421+
ensure it is unique across visualizer files for all crates.
394422

395423
# Drawbacks
396424
[drawbacks]: #drawbacks
@@ -575,7 +603,9 @@ None.
575603

576604
## Inline Natvis XML fragments via an attribute
577605

578-
Debugger visualizer support for Rust could be improved upon by adding support for in-source visualizer definitions via an attribute. Example:
606+
Debugger visualizer support for Rust could be improved upon by adding support
607+
for in-source visualizer definitions via the `#[debugger_visualizer]` attribute
608+
or a new attribute. Example:
579609

580610
```rust
581611
/// A rectangle in first quadrant
@@ -595,6 +625,10 @@ struct FancyRect {
595625
}
596626
```
597627

628+
Currently the `#[debugger_visualizer]` attribute is only allowed to target modules
629+
but can be updated to allow targeting types as well if the same attribute was to be
630+
re-used to support this.
631+
598632
## Inline Natvis XML fragments via a macro
599633

600634
We may want to allow developers to provide Natvis descriptions using a

0 commit comments

Comments
 (0)