简体   繁体   中英

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)

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在此处输入图像描述

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.

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;$T0,$T1&gt *)this
      </ExpandedItem>
    </Expand>
  </Type>

But beware of recursion and side effects connecteed with reinterpret_cast

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM