-
Notifications
You must be signed in to change notification settings - Fork 52
Add Natvis visualizations and tests for ArrayVec
and SliceVec
types.
#167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
## Debugger Visualizers | ||
|
||
Many languages and debuggers enable developers to control how a type is | ||
displayed in a debugger. These are called "debugger visualizations" or "debugger | ||
views". | ||
|
||
The Windows debuggers (WinDbg\CDB) support defining custom debugger visualizations using | ||
the `Natvis` framework. To use Natvis, developers write XML documents using the natvis | ||
schema that describe how debugger types should be displayed with the `.natvis` extension. | ||
(See: https://docs.microsoft.com/en-us/visualstudio/debugger/create-custom-views-of-native-objects?view=vs-2019) | ||
The Natvis files provide patterns which match type names a description of how to display | ||
those types. | ||
|
||
The Natvis schema can be found either online (See: https://code.visualstudio.com/docs/cpp/natvis#_schema) | ||
or locally at `<VS Installation Folder>\Xml\Schemas\1033\natvis.xsd`. | ||
|
||
The GNU debugger (GDB) supports defining custom debugger views using Pretty Printers. | ||
Pretty printers are written as python scripts that describe how a type should be displayed | ||
when loaded up in GDB/LLDB. (See: https://sourceware.org/gdb/onlinedocs/gdb/Pretty-Printing.html#Pretty-Printing) | ||
The pretty printers provide patterns, which match type names, and for matching | ||
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). | ||
|
||
### Embedding Visualizers | ||
|
||
Through the use of the currently unstable `#[debugger_visualizer]` attribute, the `tinyvec` | ||
crate can embed debugger visualizers into the crate metadata. | ||
|
||
Currently the two types of visualizers supported are Natvis and Pretty printers. | ||
|
||
For Natvis files, when linking an executable with a crate that includes Natvis files, | ||
the MSVC linker will embed the contents of all Natvis files into the generated `PDB`. | ||
|
||
For pretty printers, the compiler will encode the contents of the pretty printer | ||
in the `.debug_gdb_scripts` section of the `ELF` generated. | ||
|
||
### Testing Visualizers | ||
|
||
The `tinyvec` crate supports testing debugger visualizers defined for this crate. The entry point for | ||
these tests are `tests/debugger_visualizer.rs`. These tests are defined using the `debugger_test` and | ||
`debugger_test_parser` crates. The `debugger_test` crate is a proc macro crate which defines a | ||
single proc macro attribute, `#[debugger_test]`. For more detailed information about this crate, | ||
see https://crates.io/crates/debugger_test. The CI pipeline for the `tinyvec` crate has been updated | ||
to run the debugger visualizer tests to ensure debugger visualizers do not become broken/stale. | ||
|
||
The `#[debugger_test]` proc macro attribute may only be used on test functions and will run the | ||
function under the debugger specified by the `debugger` meta item. | ||
|
||
This proc macro attribute has 3 required values: | ||
|
||
1. The first required meta item, `debugger`, takes a string value which specifies the debugger to launch. | ||
2. The second required meta item, `commands`, takes a string of new line (`\n`) separated list of debugger | ||
commands to run. | ||
3. The third required meta item, `expected_statements`, takes a string of new line (`\n`) separated list of | ||
statements that must exist in the debugger output. Pattern matching through regular expressions is also | ||
supported by using the `pattern:` prefix for each expected statement. | ||
|
||
#### Example: | ||
|
||
```rust | ||
#[debugger_test( | ||
debugger = "cdb", | ||
commands = "command1\ncommand2\ncommand3", | ||
expected_statements = "statement1\nstatement2\nstatement3")] | ||
fn test() { | ||
|
||
} | ||
``` | ||
|
||
Using a multiline string is also supported, with a single debugger command/expected statement per line: | ||
|
||
```rust | ||
#[debugger_test( | ||
debugger = "cdb", | ||
commands = " | ||
command1 | ||
command2 | ||
command3", | ||
expected_statements = " | ||
statement1 | ||
pattern:statement[0-9]+ | ||
statement3")] | ||
fn test() { | ||
|
||
} | ||
``` | ||
|
||
In the example above, the second expected statement uses pattern matching through a regular expression | ||
by using the `pattern:` prefix. | ||
|
||
#### Testing Locally | ||
|
||
Currently, only Natvis visualizations have been defined for the `tinyvec` crate via `debug_metadata/tinyvec.natvis`, | ||
which means the `tests/debugger_visualizer.rs` tests need to be run on Windows using the `*-pc-windows-msvc` targets. | ||
To run these tests locally, first ensure the debugging tools for Windows are installed or install them following | ||
the steps listed here, [Debugging Tools for Windows](https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/). | ||
Once the debugging tools have been installed, the tests can be run in the same manner as they are in the CI | ||
pipeline. | ||
|
||
#### Note | ||
|
||
When running the debugger visualizer tests, `tests/debugger_visualizer.rs`, they need to be run consecutively | ||
and not in parallel. This can be achieved by passing the flag `--test-threads=1` to rustc. This is due to | ||
how the debugger tests are run. Each test marked with the `#[debugger_test]` attribute launches a debugger | ||
and attaches it to the current test process. If tests are running in parallel, the test will try to attach | ||
a debugger to the current process which may already have a debugger attached causing the test to fail. | ||
|
||
For example: | ||
|
||
``` | ||
cargo test --test debugger_visualizer --features debugger_visualizer -- --test-threads=1 | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010"> | ||
<Type Name="tinyvec::arrayvec::ArrayVec<array$<*,*>>"> | ||
<DisplayString>{{ len={len} }}</DisplayString> | ||
<Expand> | ||
<Item Name="[len]">len</Item> | ||
<Item Name="[capacity]">$T2</Item> | ||
<ArrayItems> | ||
<Size>len</Size> | ||
<ValuePointer>($T1*)data</ValuePointer> | ||
</ArrayItems> | ||
</Expand> | ||
</Type> | ||
|
||
<Type Name="tinyvec::slicevec::SliceVec<*>"> | ||
<Expand> | ||
<ExpandedItem>data</ExpandedItem> | ||
</Expand> | ||
</Type> | ||
</AutoVisualizer> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use debugger_test::debugger_test; | ||
use tinyvec::*; | ||
|
||
#[inline(never)] | ||
fn __break() { | ||
println!("breakpoint hit"); | ||
} | ||
|
||
#[debugger_test( | ||
debugger = "cdb", | ||
commands = r#" | ||
dx strings | ||
dx inline_tv | ||
dx inline_tv.__0 | ||
g | ||
dx slice_vec | ||
g | ||
dx strings | ||
"#, | ||
expected_statements = r#" | ||
strings : { len=0x3 } [Type: tinyvec::arrayvec::ArrayVec<array$<str,7> >] | ||
[<Raw View>] [Type: tinyvec::arrayvec::ArrayVec<array$<str,7> >] | ||
[len] : 0x3 [Type: unsigned short] | ||
[capacity] : 7 | ||
[0] : "a" [Type: str] | ||
[1] : "b" [Type: str] | ||
[2] : "c" [Type: str] | ||
|
||
inline_tv : Inline [Type: enum2$<tinyvec::tinyvec::TinyVec<array$<i32,4> > >] | ||
[<Raw View>] [Type: enum2$<tinyvec::tinyvec::TinyVec<array$<i32,4> > >] | ||
[+0x004] __0 : { len=0x4 } [Type: tinyvec::arrayvec::ArrayVec<array$<i32,4> >] | ||
|
||
inline_tv.__0 : { len=0x4 } [Type: tinyvec::arrayvec::ArrayVec<array$<i32,4> >] | ||
[<Raw View>] [Type: tinyvec::arrayvec::ArrayVec<array$<i32,4> >] | ||
[len] : 0x4 [Type: unsigned short] | ||
[capacity] : 4 | ||
[0] : 1 [Type: i32] | ||
[1] : 2 [Type: i32] | ||
[2] : 3 [Type: i32] | ||
[3] : 4 [Type: i32] | ||
|
||
strings : { len=0x6 } [Type: tinyvec::arrayvec::ArrayVec<array$<str,7> >] | ||
[<Raw View>] [Type: tinyvec::arrayvec::ArrayVec<array$<str,7> >] | ||
[len] : 0x6 [Type: unsigned short] | ||
[capacity] : 7 | ||
[0] : "a" [Type: str] | ||
[1] : "b" [Type: str] | ||
[2] : "d" [Type: str] | ||
[3] : "e" [Type: str] | ||
[4] : "f" [Type: str] | ||
[5] : "g" [Type: str] | ||
"# | ||
)] | ||
#[inline(never)] | ||
fn test_debugger_visualizer() { | ||
let mut strings = ArrayVec::<[&str; 7]>::default(); | ||
strings.push("a"); | ||
strings.push("b"); | ||
strings.push("c"); | ||
assert_eq!(["a", "b", "c"], &strings[..]); | ||
|
||
let mut inline_tv = tiny_vec!([i32; 4] => 1, 2, 3); | ||
assert!(inline_tv.is_inline()); | ||
|
||
inline_tv.push(4); | ||
__break(); | ||
|
||
{ | ||
let mut slice_vec = SliceVec::from(strings.as_mut_slice()); | ||
assert_eq!(3, slice_vec.capacity()); | ||
assert_eq!("c", slice_vec.remove(2)); | ||
slice_vec.push("d"); | ||
println!("{:?}", slice_vec); | ||
__break(); | ||
|
||
assert_eq!(["a", "b", "d"], &slice_vec[..]); | ||
} | ||
|
||
strings.push("e"); | ||
strings.push("f"); | ||
strings.push("g"); | ||
assert_eq!(["a", "b", "d", "e", "f", "g"], &strings[..]); | ||
__break(); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.