Home > database >  Visual Studio Debugger: Register a display format for a specific C struct/class
Visual Studio Debugger: Register a display format for a specific C struct/class

Time:11-25

How to let the Visual Studio Debugger know that a specific C /C struct should be displayed in a specific Format?

I've for example a C-struct containing 2 pointers that represent the start and end of an array like the following:

typedef struct
{
    VEC_VALUE_T* __restrict DataBegin_;
    VEC_VALUE_T* __restrict DataEnd_;
    VEC_VALUE_T* __restrict MemEnd_;
    VEC_ALLOC* __restrict Allocator_;
} VEC;

How can I display it in the Debugger as if it were a std::vector.

The same question from another POV: How does the Debugger know how to display a std::vector? Is std::vector using some debugger specific pragmas or something?

CodePudding user response:

As mentioned by @retired-ninja in comment, the natvis framework can be used: https://docs.microsoft.com/en-us/visualstudio/debugger/create-custom-views-of-native-objects?view=vs-2022

Add natvis file in VS

right click on project tab -> Add new item -> Utility -> .natvis

Add a type element for that specific struct/class

Examples on what the syntax looks like can be found in the link above.

In my case the following element definition was sufficient to display it as a std::vector:

<?xml version="1.0" encoding="utf-8"?> 
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">

    <Type Name="::VEC">
        <DisplayString>{{ size={DataEnd_ - DataBegin_}, capacity={MemEnd_ - DataBegin_} }}</DisplayString>
        <Expand>
            <Item Name="[size]" ExcludeView="simple">DataEnd_ - DataBegin_</Item>
            <Item Name="[capacity]" ExcludeView="simple">MemEnd_ - DataBegin_</Item>
            <ArrayItems>
                <Size>DataEnd_ - DataBegin_</Size>
                <ValuePointer>DataBegin_</ValuePointer>
            </ArrayItems>
        </Expand>
    </Type>

</AutoVisualizer>

  • Related