Skip to content

Conversation

@nanguye2496
Copy link
Owner

@nanguye2496 nanguye2496 commented Feb 27, 2021

The natvis description of Rust HashMap contains this line:

<Item Name="{((tuple&lt;$T1, $T2&gt;*)base.table.ctrl.pointer)[-(i + 1)].__0}">((tuple&lt;$T1, $T2&gt;*)base.table.ctrl.pointer)[-(i + 1)].__1</Item>

WinDbg's natvis executor fails to cast base.table.ctrl.pointer to tuple&lt;$T1, $T2&gt;*, leading to this issue rust-lang#82674. The cause of this failure comes from WinDbg's syntax parser, which adds a whitespace between $T1 and $T2 after substituting them by the corresponding parametric types. While waiting for a fix from WinDbg side, I propose this work-around.

For this program:

use std::fmt;

#[derive(PartialEq, Eq, Hash)]
struct Student {
    name: String,
    country: String,
}

impl fmt::Display for Student {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "({}, {})", self.name, self.country)
    }
}

fn main() {
    use std::collections::HashMap;
    let mut map = HashMap::new();
    let s1 = Student { name: String::from("Nam"), country: String::from("UK") };
    let s2 = Student { name: String::from("Alex"), country: String::from("US")};
    map.insert(s1, 10);
    map.insert(s2, 8);
    for (k,v) in map.iter() {
        println!("{}, {}", k, v);
    }
}

Here is the HashMap visualization before the fix:
image

Here is the HashMap visualization after the fix:
image

@nanguye2496 nanguye2496 changed the title Rewrite .natvis description for HashMap to improve the visualization of key-value pairs in WinDbg Rewrite natvis description for HashMap to improve the visualization of key-value pairs in WinDbg Mar 1, 2021
<!-- Bucket is populated -->
<Exec>n--</Exec>
<Item Name="{((tuple&lt;$T1, $T2&gt;*)base.table.ctrl.pointer)[-(i + 1)].__0}">((tuple&lt;$T1, $T2&gt;*)base.table.ctrl.pointer)[-(i + 1)].__1</Item>
<Item>((tuple&lt;$T1,$T2&gt;*)base.table.ctrl.pointer)[-(i + 1)]</Item>
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should display each key-value pair as a tuple instead of having the key as the item name to allow developers to examine HashMap keys of user-defined types in WinDbg.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants