Skip to content

Commit 1540fe5

Browse files
Add documentation for embedding a debugger visualizer in the regex crate. Move the location of the Natvis file in the regex crate out of the project root.
1 parent 1e61cba commit 1540fe5

File tree

4 files changed

+110
-1
lines changed

4 files changed

+110
-1
lines changed

HACKING.md

+7
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,13 @@ invoking `cargo test`. Note that this variable is inspected at compile
271271
time, so if the tests don't seem to be running, you may need to run
272272
`cargo clean`.
273273

274+
This crate also supports defining and testing custom debugger visualizers.
275+
The `#[debugger_visualizer]` attribute is currently unstable and behind a
276+
`debugger_visualizer` feature gate. To test these visualizers, enable the
277+
`debugger_visualizer` feature for this crate and run the `tests/test_visualizer.rs`
278+
tests using the nightly toolchain. For more information on debugger visualizers,
279+
see `debug_metadata/README.md`.
280+
274281
## Benchmarking
275282

276283
The benchmarking in this crate is made up of many micro-benchmarks. Currently,

debug_metadata/README.md

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
## Debugger Visualizers
2+
3+
Many languages and debuggers enable developers to control how a type is
4+
displayed in a debugger. These are called "debugger visualizations" or "debugger
5+
views".
6+
7+
The Windows debuggers (WinDbg\CDB) support defining custom debugger visualizations using
8+
the `Natvis` framework. To use Natvis, developers write XML documents using the natvis
9+
schema that describe how debugger types should be displayed with the `.natvis` extension.
10+
(See: https://docs.microsoft.com/en-us/visualstudio/debugger/create-custom-views-of-native-objects?view=vs-2019)
11+
The Natvis files provide patterns which match type names a description of how to display
12+
those types.
13+
14+
The Natvis schema can be found either online (See: https://code.visualstudio.com/docs/cpp/natvis#_schema)
15+
or locally at `<VS Installation Folder>\Xml\Schemas\1033\natvis.xsd`.
16+
17+
The GNU debugger (GDB) supports defining custom debugger views using Pretty Printers.
18+
Pretty printers are written as python scripts that describe how a type should be displayed
19+
when loaded up in GDB/LLDB. (See: https://sourceware.org/gdb/onlinedocs/gdb/Pretty-Printing.html#Pretty-Printing)
20+
The pretty printers provide patterns, which match type names, and for matching
21+
types, descibe how to display those types. (For writing a pretty printer, see: https://sourceware.org/gdb/onlinedocs/gdb/Writing-a-Pretty_002dPrinter.html#Writing-a-Pretty_002dPrinter).
22+
23+
### Embedding Visualizers
24+
25+
Through the use of the currently unstable `#[debugger_visualizer]` attribute, the `regex`
26+
crate can embed debugger visualizers into the crate metadata.
27+
28+
Currently the two types of visualizers supported are Natvis and Pretty printers.
29+
30+
For Natvis files, when linking an executable with a crate that includes Natvis files,
31+
the MSVC linker will embed the contents of all Natvis files into the generated `PDB`.
32+
33+
For pretty printers, the compiler will encode the contents of the pretty printer
34+
in the `.debug_gdb_scripts` section of the `ELF` generated.
35+
36+
### Testing Visualizers
37+
38+
The `regex` crate supports testing debugger visualizers defined for this crate. The entry point for
39+
these tests are `tests/test_visualizer.rs`. These tests are defined using the `debugger_test` and
40+
`debugger_test_parser` crates. The `debugger_test` crate is a proc macro crate which defines a
41+
single proc macro attribute, `#[debugger_test]`. For more detailed information about this crate,
42+
see https://crates.io/crates/debugger_test. The CI pipeline for the `regex` crate has been updated
43+
to run the debugger visualizer tests to ensure debugger visualizers do not become broken/stale.
44+
45+
The `#[debugger_test]` proc macro attribute may only be used on test functions and will run the
46+
function under the debugger specified by the `debugger` meta item.
47+
48+
This proc macro attribute has 3 required values:
49+
50+
1. The first required meta item, `debugger`, takes a string value which specifies the debugger to launch.
51+
2. The second required meta item, `commands`, takes a string of new line (`\n`) separated list of debugger
52+
commands to run.
53+
3. The third required meta item, `expected_statements`, takes a string of new line (`\n`) separated list of
54+
statements that must exist in the debugger output. Pattern matching through regular expressions is also
55+
supported by using the `pattern:` prefix for each expected statement.
56+
57+
#### Example:
58+
59+
```rust
60+
#[debugger_test(
61+
debugger = "cdb",
62+
commands = "command1\ncommand2\ncommand3",
63+
expected_statements = "statement1\nstatement2\nstatement3")]
64+
fn test() {
65+
66+
}
67+
```
68+
69+
Using a multiline string is also supported, with a single debugger command/expected statement per line:
70+
71+
```rust
72+
#[debugger_test(
73+
debugger = "cdb",
74+
commands = "
75+
command1
76+
command2
77+
command3",
78+
expected_statements = "
79+
statement1
80+
pattern:statement[0-9]+
81+
statement3")]
82+
fn test() {
83+
84+
}
85+
```
86+
87+
In the example above, the second expected statement uses pattern matching through a regular expression
88+
by using the `pattern:` prefix.
89+
90+
#### Note
91+
92+
When running the debugger visualizer tests, `tests/test_visualizer.rs`, they need to be run consecutively
93+
and not in parallel. This can be achieved by passing the flag `--test-threads=1` to rustc. This is due to
94+
how the debugger tests are run. Each test marked with the `#[debugger_test]` attribute launches a debugger
95+
and attaches it to the current test process. If tests are running in parallel, the test will try to attach
96+
a debugger to the current process which may already have a debugger attached causing the test to fail.
97+
98+
For example:
99+
100+
```
101+
cargo test --test visualizers --features debugger_visualizer -- --test-threads=1
102+
```
File renamed without changes.

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ another matching engine with fixed memory requirements.
610610
#![cfg_attr(feature = "debugger_visualizer", feature(debugger_visualizer))]
611611
#![cfg_attr(
612612
feature = "debugger_visualizer",
613-
debugger_visualizer(natvis_file = "../regex.natvis")
613+
debugger_visualizer(natvis_file = "../debug_metadata/regex.natvis")
614614
)]
615615
#![warn(missing_debug_implementations)]
616616

0 commit comments

Comments
 (0)