Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NATVIS reinterpret type or alias type

Is there a way in natvis to reinterpret a type to an already natvis-defined type ? or alias it ?

For example, I'd like to do this kind of "trick" (really necessary in my context even if that does sound really weird to you, it's a question of JIT symbol generation)

<Type Name="std::vector&lt;*,*&gt;">
<DisplayString>{*(stl1.dll!std::vector&lt;$T0,$T1&gt *)this}</DisplayString>
</Type>

But it doesn't work to display the expand items, it just displays a string as value (which seems logical considering the 'DisplayString' role).

I've also tried with a SmartPointer trick, it does better but it turns out it doesn't work when there is base classes involved (it only displays the SmartPointer type and ignore completely inheritance)

like image 318
Juicebox Avatar asked Oct 19 '25 13:10

Juicebox


2 Answers

I think you try to represent one type as another (transparently)? In your case you should use ExpandedItem. This should work:

  <Type Name="std::vector&lt;*,*&gt;">
    <Expand>
      <ExpandedItem>
        *(stl1.dll!std::vector&lt;$T1,$T2&gt; *)this
      </ExpandedItem>
    </Expand>
  </Type>

But beware of recursion and side effects connecteed with reinterpret_cast

like image 70
mr NAE Avatar answered Oct 21 '25 08:10

mr NAE


As you do not show your classes I can just show my own example code.

struct A { int x, y; };
struct B { int x, y; };
struct C { int v, w; };

int main()
{
    A a{ 1,2 };
    B b{ 3,4 };
    C c{ 5,6 };
    return 0;
}

And the natvis:

<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
  <Type Name="A">
    <AlternativeType Name="B"/>
    <AlternativeType Name="C"/>
    <DisplayString>A {x}, {y}</DisplayString>
  </Type>
</AutoVisualizer>

This display as enter image description here

As you can see the AlternativeName does the trick, but it is required on the natvis for the original class. And it requires that both classes have the same members/member names.

Please also note that when playing around with natvis it is a good idea to enable natvis debugging. Go to menu Tools/Options/Debugging/"Output Windows"/"General Output Settings" and set "Natvis diagnostic messages (C++ only)" to a useful value.

like image 35
Werner Henze Avatar answered Oct 21 '25 08:10

Werner Henze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!