Skip to content

Commit

Permalink
Rollup merge of rust-lang#86983 - wesleywiser:natvis_std_types, r=mic…
Browse files Browse the repository at this point in the history
…haelwoerister

Add or improve natvis definitions for common standard library types

Natvis definitions are used by Windows debuggers to provide a better experience when inspecting a value for types with natvis definitions. Many of our standard library types and intrinsic Rust types like slices and `str` already have natvis definitions.

This PR adds natvis definitions for missing types (like all of the `Atomic*` types) and improves some of the existing ones (such as showing the ref count on `Arc<T>` and `Rc<T>` and showing the borrow state of `RefCell<T>`). I've also added cdb tests to cover these definitions and updated existing tests with the new visualizations.

With this PR, the following types now visualize in a much more intuitive way:

### Type: `NonZero{I,U}{8,16,32,64,128,size}`, `Atomic{I,U}{8,16,32,64,size}`, `AtomicBool` and `Wrapping<T>`

<details><summary>Example:</summary>

```rust
let a_u32 = AtomicU32::new(32i32);
```

```
0:000> dx a_u32
a_u32            : 32 [Type: core::sync::atomic::AtomicU32]
    [<Raw View>]     [Type: core::sync::atomic::AtomicU32]
```

</details>

### Type: `Cell<T>` and `UnsafeCell<T>`
<details><summary>Example:</summary>

```rust
let cell = Cell::new(123u8);
let unsafecell = UnsafeCell::new((42u16, 30u16));
```

```
0:000> dx cell
cell             : 123 [Type: core::cell::Cell<u8>]
    [<Raw View>]     [Type: core::cell::Cell<u8>]

0:000> dx unsafecell
unsafecell       : (42, 30) [Type: core::cell::UnsafeCell<tuple<u16, u16>>]
    [<Raw View>]     [Type: core::cell::UnsafeCell<tuple<u16, u16>>]
    [0]              : 42 [Type: unsigned short]
    [1]              : 30 [Type: unsigned short]
```

</details>

### Type: `RefCell<T>`

<details><summary>Example:</summary>

```rust
let refcell = RefCell::new((123u16, 456u32));
```

```
0:000> dx refcell
refcell          : (123, 456) [Type: core::cell::RefCell<tuple<u16, u32>>]
    [<Raw View>]     [Type: core::cell::RefCell<tuple<u16, u32>>]
    [Borrow state]   : Unborrowed
    [0]              : 123 [Type: unsigned short]
    [1]              : 456 [Type: unsigned int]
```

</details>

### Type: `NonNull<T>` and `Unique<T>`
<details><summary>Example:</summary>

```rust
let nonnull: NonNull<_> = (&(10, 20)).into();
```

```
0:000> dx nonnull
nonnull          : NonNull(0x7ff6a5d9c390: (10, 20)) [Type: core::ptr::non_null::NonNull<tuple<i32, i32>>]
    [<Raw View>]     [Type: core::ptr::non_null::NonNull<tuple<i32, i32>>]
    [0]              : 10 [Type: int]
    [1]              : 20 [Type: int]
```

</details>

### Type: `Range<T>`, `RangeFrom<T>`, `RangeInclusive<T>`, `RangeTo<T>` and `RangeToInclusive<T>`
<details><summary>Example:</summary>

```rust
let range = (1..12);
let rangefrom = (9..);
let rangeinclusive = (32..=80);
let rangeto = (..42);
let rangetoinclusive = (..=120);
```

```
0:000> dx range
range            : (1..12) [Type: core::ops::range::Range<i32>]
    [<Raw View>]     [Type: core::ops::range::Range<i32>]

0:000> dx rangefrom
rangefrom        : (9..) [Type: core::ops::range::RangeFrom<i32>]
    [<Raw View>]     [Type: core::ops::range::RangeFrom<i32>]

0:000> dx rangeinclusive
rangeinclusive   : (32..=80) [Type: core::ops::range::RangeInclusive<i32>]
    [<Raw View>]     [Type: core::ops::range::RangeInclusive<i32>]

0:000> dx rangeto
rangeto          : (..42) [Type: core::ops::range::RangeTo<i32>]
    [<Raw View>]     [Type: core::ops::range::RangeTo<i32>]

0:000> dx rangetoinclusive
rangetoinclusive : (..=120) [Type: core::ops::range::RangeToInclusive<i32>]
    [<Raw View>]     [Type: core::ops::range::RangeToInclusive<i32>]
```

</details>

### Type: `Duration`
<details><summary>Example:</summary>

```rust
let duration = Duration::new(5, 12);
```

```
0:000> dx duration
duration         : 5s 12ns [Type: core::time::Duration]
    [<Raw View>]     [Type: core::time::Duration]
    seconds          : 5 [Type: unsigned __int64]
    nanoseconds      : 12 [Type: unsigned int]
```

</details>

### Type: `ManuallyDrop<T>`
<details><summary>Example:</summary>

```rust
let manuallydrop = ManuallyDrop::new((123, 456));
```

```
0:000> dx manuallydrop
manuallydrop     : (123, 456) [Type: core::mem::manually_drop::ManuallyDrop<tuple<i32, i32>>]
    [<Raw View>]     [Type: core::mem::manually_drop::ManuallyDrop<tuple<i32, i32>>]
    [0]              : 123 [Type: int]
    [1]              : 456 [Type: int]
```

</details>

### Type: `Pin<T>`
<details><summary>Example:</summary>

```rust
let mut s = "this".to_string();
let pin = Pin::new(&mut s);
```

```
0:000> dx pin
pin              : Pin(0x11a0ff6f0: "this") [Type: core::pin::Pin<mut alloc::string::String*>]
    [<Raw View>]     [Type: core::pin::Pin<mut alloc::string::String*>]
    [len]            : 4 [Type: unsigned __int64]
    [capacity]       : 4 [Type: unsigned __int64]
    [chars]
```

</details>

### Type: `Rc<T>` and `Arc<T>`
<details><summary>Example:</summary>

```rust
let rc = Rc::new(42i8);
let rc_weak = Rc::downgrade(&rc);
```

```
0:000> dx rc
rc               : 42 [Type: alloc::rc::Rc<i8>]
    [<Raw View>]     [Type: alloc::rc::Rc<i8>]
    [Reference count] : 1 [Type: core::cell::Cell<usize>]

0:000> dx rc_weak
rc_weak          : 42 [Type: alloc::rc::Weak<i8>]
    [<Raw View>]     [Type: alloc::rc::Weak<i8>]
```

</details>

r? ``@michaelwoerister``
cc ``@nanguye2496``
  • Loading branch information
GuillaumeGomez authored Jul 13, 2021
2 parents ca99e3e + 14fdf8a commit 323d07f
Show file tree
Hide file tree
Showing 12 changed files with 596 additions and 49 deletions.
4 changes: 2 additions & 2 deletions src/etc/natvis/intrinsic.natvis
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
<Type Name="str">
<DisplayString>{data_ptr,[length]s8}</DisplayString>
<StringView>data_ptr,[length]s8</StringView>
<DisplayString>{(char*)data_ptr,[length]s8}</DisplayString>
<StringView>(char*)data_ptr,[length]s8</StringView>
<Expand>
<Item Name="[len]" ExcludeView="simple">length</Item>
<Synthetic Name="[chars]">
Expand Down
17 changes: 17 additions & 0 deletions src/etc/natvis/liballoc.natvis
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<Item Name="[len]" ExcludeView="simple">vec.len</Item>
<Item Name="[capacity]" ExcludeView="simple">vec.buf.cap</Item>
<Synthetic Name="[chars]">
<DisplayString>{(char*)vec.buf.ptr.pointer,[vec.len]s8}</DisplayString>
<Expand>
<ArrayItems>
<Size>vec.len</Size>
Expand All @@ -57,22 +58,38 @@
</Synthetic>
</Expand>
</Type>

<Type Name="alloc::rc::Rc&lt;*&gt;">
<DisplayString>{ptr.pointer->value}</DisplayString>
<Expand>
<ExpandedItem>ptr.pointer->value</ExpandedItem>
<Item Name="[Reference count]">ptr.pointer->strong</Item>
<Item Name="[Weak reference count]">ptr.pointer->weak</Item>
</Expand>
</Type>
<Type Name="alloc::rc::Weak&lt;*&gt;">
<DisplayString>{ptr.pointer->value}</DisplayString>
<Expand>
<ExpandedItem>ptr.pointer->value</ExpandedItem>
<Item Name="[Reference count]">ptr.pointer->strong</Item>
<Item Name="[Weak reference count]">ptr.pointer->weak</Item>
</Expand>
</Type>

<Type Name="alloc::sync::Arc&lt;*&gt;">
<DisplayString>{ptr.pointer->data}</DisplayString>
<Expand>
<ExpandedItem>ptr.pointer->data</ExpandedItem>
<Item Name="[Reference count]">ptr.pointer->strong</Item>
<Item Name="[Weak reference count]">ptr.pointer->weak</Item>
</Expand>
</Type>
<Type Name="alloc::sync::Weak&lt;*&gt;">
<DisplayString>{ptr.pointer->data}</DisplayString>
<Expand>
<ExpandedItem>ptr.pointer->data</ExpandedItem>
<Item Name="[Reference count]">ptr.pointer->strong</Item>
<Item Name="[Weak reference count]">ptr.pointer->weak</Item>
</Expand>
</Type>
<Type Name="alloc::borrow::Cow&lt;*&gt;">
Expand Down
158 changes: 149 additions & 9 deletions src/etc/natvis/libcore.natvis
Original file line number Diff line number Diff line change
@@ -1,23 +1,163 @@
<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
<Type Name="core::ptr::Unique&lt;*&gt;">
<DisplayString>{{ Unique {pointer} }}</DisplayString>
<Type Name="core::cell::Cell&lt;*&gt;">
<DisplayString>{value.value}</DisplayString>
<Expand>
<Item Name="[ptr]">pointer</Item>
<ExpandedItem>value.value</ExpandedItem>
</Expand>
</Type>
<Type Name="core::cell::Ref&lt;*&gt;">
<DisplayString>{value}</DisplayString>
<Expand>
<ExpandedItem>value</ExpandedItem>
</Expand>
</Type>
<Type Name="core::cell::RefMut&lt;*&gt;">
<DisplayString>{value}</DisplayString>
<Expand>
<ExpandedItem>value</ExpandedItem>
</Expand>
</Type>
<Type Name="core::cell::RefCell&lt;*&gt;">
<DisplayString>{value.value}</DisplayString>
<Expand>
<Item Name="[Borrow state]" Condition="borrow.value.value == 0">"Unborrowed",sb</Item>
<Item Name="[Borrow state]" Condition="borrow.value.value &gt; 0">"Immutably borrowed",sb</Item>
<Item Name="[Borrow state]" Condition="borrow.value.value &lt; 0">"Mutably borrowed",sb</Item>
<ExpandedItem>value.value</ExpandedItem>
</Expand>
</Type>
<Type Name="core::cell::UnsafeCell&lt;*&gt;">
<DisplayString>{value}</DisplayString>
<Expand>
<ExpandedItem>value</ExpandedItem>
</Expand>
</Type>

<Type Name="core::ptr::Shared&lt;*&gt;">
<DisplayString>{{ Shared {pointer} }}</DisplayString>
<Type Name="core::mem::manually_drop::ManuallyDrop&lt;*&gt;">
<DisplayString>{value}</DisplayString>
<Expand>
<Item Name="[ptr]">pointer</Item>
<ExpandedItem>value</ExpandedItem>
</Expand>
</Type>

<Type Name="core::num::nonzero::NonZeroI8">
<DisplayString>{__0}</DisplayString>
</Type>
<Type Name="core::num::nonzero::NonZeroI16">
<DisplayString>{__0}</DisplayString>
</Type>
<Type Name="core::num::nonzero::NonZeroI32">
<DisplayString>{__0}</DisplayString>
</Type>
<Type Name="core::num::nonzero::NonZeroI64">
<DisplayString>{__0}</DisplayString>
</Type>
<Type Name="core::num::nonzero::NonZeroI128">
<DisplayString>{__0}</DisplayString>
</Type>
<Type Name="core::num::nonzero::NonZeroIsize">
<DisplayString>{__0}</DisplayString>
</Type>
<Type Name="core::num::nonzero::NonZeroU8">
<DisplayString>{__0}</DisplayString>
</Type>
<Type Name="core::num::nonzero::NonZeroU16">
<DisplayString>{__0}</DisplayString>
</Type>
<Type Name="core::num::nonzero::NonZeroU32">
<DisplayString>{__0}</DisplayString>
</Type>
<Type Name="core::num::nonzero::NonZeroU64">
<DisplayString>{__0}</DisplayString>
</Type>
<Type Name="core::num::nonzero::NonZeroU128">
<DisplayString>{__0}</DisplayString>
</Type>
<Type Name="core::num::nonzero::NonZeroUsize">
<DisplayString>{__0}</DisplayString>
</Type>

<Type Name="core::num::wrapping::Wrapping&lt;*&gt;">
<DisplayString>{__0}</DisplayString>
</Type>

<Type Name="core::ops::range::Range&lt;*&gt;">
<DisplayString>({start}..{end})</DisplayString>
</Type>
<Type Name="core::ops::range::RangeFrom&lt;*&gt;">
<DisplayString>({start}..)</DisplayString>
</Type>
<Type Name="core::ops::range::RangeInclusive&lt;*&gt;">
<DisplayString>({start}..={end})</DisplayString>
</Type>
<Type Name="core::ops::range::RangeTo&lt;*&gt;">
<DisplayString>(..{end})</DisplayString>
</Type>
<Type Name="core::ops::range::RangeToInclusive&lt;*&gt;">
<DisplayString>(..={end})</DisplayString>
</Type>

<Type Name="core::pin::Pin&lt;*&gt;">
<DisplayString>Pin({(void*)pointer}: {pointer})</DisplayString>
<Expand>
<ExpandedItem>pointer</ExpandedItem>
</Expand>
</Type>

<Type Name="core::ptr::non_null::NonNull&lt;*&gt;">
<DisplayString>{(void*) pointer}</DisplayString>
<DisplayString>NonNull({(void*) pointer}: {pointer})</DisplayString>
<Expand>
<ExpandedItem>pointer</ExpandedItem>
</Expand>
</Type>

<Type Name="core::ptr::unique::Unique&lt;*&gt;">
<DisplayString>Unique({(void*)pointer}: {pointer})</DisplayString>
<Expand>
<ExpandedItem>pointer</ExpandedItem>
</Expand>
</Type>

<Type Name="core::sync::atomic::AtomicBool">
<DisplayString>{(bool)v.value}</DisplayString>
</Type>
<Type Name="core::sync::atomic::AtomicI8">
<DisplayString>{v.value}</DisplayString>
</Type>
<Type Name="core::sync::atomic::AtomicI16">
<DisplayString>{v.value}</DisplayString>
</Type>
<Type Name="core::sync::atomic::AtomicI32">
<DisplayString>{v.value}</DisplayString>
</Type>
<Type Name="core::sync::atomic::AtomicI64">
<DisplayString>{v.value}</DisplayString>
</Type>
<Type Name="core::sync::atomic::AtomicIsize">
<DisplayString>{v.value}</DisplayString>
</Type>
<Type Name="core::sync::atomic::AtomicU8">
<DisplayString>{v.value}</DisplayString>
</Type>
<Type Name="core::sync::atomic::AtomicU16">
<DisplayString>{v.value}</DisplayString>
</Type>
<Type Name="core::sync::atomic::AtomicU32">
<DisplayString>{v.value}</DisplayString>
</Type>
<Type Name="core::sync::atomic::AtomicU64">
<DisplayString>{v.value}</DisplayString>
</Type>
<Type Name="core::sync::atomic::AtomicUsize">
<DisplayString>{v.value}</DisplayString>
</Type>

<Type Name="core::time::Duration">
<DisplayString>{secs,d}s {nanos,d}ns</DisplayString>
<Expand>
<Item Name="[value]">*pointer</Item>
<Item Name="seconds">secs,d</Item>
<Item Name="nanoseconds">nanos,d</Item>
</Expand>
</Type>
</AutoVisualizer>
</AutoVisualizer>
18 changes: 17 additions & 1 deletion src/etc/natvis/libstd.natvis
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@
</Type>

<Type Name="std::ffi::c_str::CString">
<DisplayString>{inner.data_ptr,s}</DisplayString>
<DisplayString>{(char*)inner.data_ptr}</DisplayString>
<Expand>
<Synthetic Name="[chars]">
<DisplayString>{(char*)inner.data_ptr}</DisplayString>
<Expand>
<ArrayItems>
<Size>inner.length</Size>
Expand All @@ -101,4 +102,19 @@
</Synthetic>
</Expand>
</Type>

<Type Name="std::ffi::os_str::OsString">
<DisplayString>{(char*)inner.inner.bytes.buf.ptr.pointer,[inner.inner.bytes.len]}</DisplayString>
<Expand>
<Synthetic Name="[chars]">
<DisplayString>{(char*)inner.inner.bytes.buf.ptr.pointer,[inner.inner.bytes.len]}</DisplayString>
<Expand>
<ArrayItems>
<Size>inner.inner.bytes.len</Size>
<ValuePointer>(char*)inner.inner.bytes.buf.ptr.pointer</ValuePointer>
</ArrayItems>
</Expand>
</Synthetic>
</Expand>
</Type>
</AutoVisualizer>
22 changes: 22 additions & 0 deletions src/test/debuginfo/duration-type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// only-cdb
// compile-flags:-g

// === CDB TESTS ==================================================================================

// cdb-command: g

// cdb-command: dx duration
// cdb-check:duration : 5s 12ns [Type: core::time::Duration]
// cdb-check: [<Raw View>] [Type: core::time::Duration]
// cdb-check: seconds : 5 [Type: unsigned __int64]
// cdb-check: nanoseconds : 12 [Type: unsigned int]

use std::time::Duration;

fn main() {
let duration = Duration::new(5, 12);

zzz(); // #break
}

fn zzz() { }
49 changes: 49 additions & 0 deletions src/test/debuginfo/marker-types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// only-cdb
// compile-flags:-g

// === CDB TESTS ==================================================================================

// cdb-command: g

// cdb-command: dx nonnull
// cdb-check:nonnull : NonNull(0x[...]: 0xc) [Type: core::ptr::non_null::NonNull<u32>]
// cdb-check: [<Raw View>] [Type: core::ptr::non_null::NonNull<u32>]
// cdb-check: 0xc [Type: unsigned int]

// cdb-command: dx manuallydrop
// cdb-check:manuallydrop : 12345 [Type: core::mem::manually_drop::ManuallyDrop<i32>]
// cdb-check: [<Raw View>] [Type: core::mem::manually_drop::ManuallyDrop<i32>]

// cdb-command: dx pin
// cdb-check:pin : Pin(0x[...]: "this") [Type: core::pin::Pin<ref_mut$<alloc::string::String> >]
// cdb-check: [<Raw View>] [Type: core::pin::Pin<ref_mut$<alloc::string::String> >]
// cdb-check: [len] : 0x4 [Type: unsigned __int64]
// cdb-check: [capacity] : 0x4 [Type: unsigned __int64]
// cdb-check: [chars] : "this"

// cdb-command: dx unique
// cdb-check:unique : Unique(0x[...]: (0x2a, 4321)) [Type: core::ptr::unique::Unique<tuple$<u64,i32> >]
// cdb-check: [<Raw View>] [Type: core::ptr::unique::Unique<tuple$<u64,i32> >]
// cdb-check: [0] : 0x2a [Type: unsigned __int64]
// cdb-check: [1] : 4321 [Type: int]

#![feature(ptr_internals)]

use std::mem::ManuallyDrop;
use std::pin::Pin;
use std::ptr::{NonNull, Unique};

fn main() {
let nonnull: NonNull<_> = (&12u32).into();

let manuallydrop = ManuallyDrop::new(12345i32);

let mut s = "this".to_string();
let pin = Pin::new(&mut s);

let unique: Unique<_> = (&mut (42u64, 4321i32)).into();

zzz(); // #break
}

fn zzz() { }
Loading

0 comments on commit 323d07f

Please sign in to comment.