c++visual-studio-codenatvis

Extend display range of ArrayItems/IndexListItems using natvis


I am trying to visualize a memory content using natvis which is pointed by a pointer. I have also tried to declare the memory as a vector. But every time the problem I am facing is that, during debugging the visualizer can show only first 50 entry.

I am giving here a very minimal example. Suppose, the pointer_array is a member of Foo class. In the driver file an array of size 5000 is created which is pointed by the array. I would like to observe the value of the array with the variable pointer_array. Also I have tried to understand how natvis reacts with std::vector and that's why as a member variable a vector (foo_vec) is also declared.

foo.h:

#include <iostream>
#include <vector>

class Foo
{
    public:
        Foo(){}

        uint32_t *pointer_array;
        std::vector<uint32_t> foo_vec;
};

main.cpp:

#include "foo.h"
# define ARRAY_SIZE 5000

int main()
{
    Foo obj_1;

    uint32_t foo_array[ARRAY_SIZE];
    for(int i = 0; i < ARRAY_SIZE; i++)
    {
        foo_array[i] = i*2;
    }
    obj_1.pointer_array = foo_array;

    for(uint32_t i = 0; i < ARRAY_SIZE; i++)
    {
        obj_1.foo_vec.push_back(i*3);
    }

    return 0;
}

The following natvis file I have used.

<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
    <Type Name="Foo">
        <DisplayString>Testing_Natvis</DisplayString>
        <Expand>
        <ArrayItems>
            <Size>5000</Size>
            <ValuePointer>pointer_array</ValuePointer>
        </ArrayItems>

        <!-- Tested with IndexListItems but failed to fetch all data, still only first 49 entry -->
        <!-- <IndexListItems>
          <Size>5000</Size>
          <ValueNode>pointer_array[$i]</ValueNode>
        </IndexListItems> -->

          <!-- Same result as like as pointer_array. Only first 49 entry is appeared -->
          <!-- <IndexListItems>
            <Size>foo_vec.size()</Size>
            <ValueNode>foo_vec[$i]</ValueNode>
          </IndexListItems> -->

          <!-- <ArrayItems>
            <Size>foo_vec.size()</Size>
            <ValuePointer>&amp;foo_vec[0]</ValuePointer>
        </ArrayItems> -->
        </Expand>
    </Type>
</AutoVisualizer>

In the launch.json I have added extra only the following two lines:

"visualizerFile": "${workspaceFolder}/natvis_file/file.natvis",
"showDisplayString": true,

For better understanding I am giving here a screenshot of the output where in natvis file I have used IndexListItems and given size 80 to see value from index 0 to 79 but the displayed last value is from index 49.error natvis output

And the following is showing that I have given the size value 6 and natvis perfectly is showing value from index 0 to 5.correct output of natvis from vscode

Any workaround to achieve all entry of the memory using Natvis?


Solution

  • <IndexListItems>
       <Size>5000</Size>
       <ValueNode>pointer_array[$i]</ValueNode>
    </IndexListItems>