c++pointersvisual-studio-codenatvis

Use of Natvis framework to observe value pointed by pointer


My goal is to observe a container of value which is pointed by a pointer. I am recommended to use natvis for this purpose. I am using VSCode to develop my project in Linux system. Unfortunately, I am not succeeded to get the desired value. I can only see the first address and value pointed by the pointer.

Sample code I am giving here.

#include <iostream>

class FOO
{
    public:
        FOO(uint32_t a_, uint32_t b_) : a{a_}, b{b_}
                                        {}

        void Print_Value();
        uint32_t *pointer_array;

    protected:
        uint32_t a, b;

};
#include "foo.h"

void FOO :: Print_Value()
{
    std::cout << "a: " << a << std::endl
              << "b: " << b << std::endl;
}
#include "foo.h"

int main()
{
    FOO obj_1(58,9);
    obj_1.Print_Value();

    uint32_t foo_array[5] = {5,15,96,8,77};
    obj_1.pointer_array = foo_array;


    return 0;
}
<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
    <Type Name="FOO">
        <DisplayString>Test</DisplayString>
        <Expand>
            <Item Name="[pointer_array]">pointer_array</Item>
        </Expand>
    </Type>
</AutoVisualizer>
<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
    <Type Name="FOO::pointer_array">
        <DisplayString>Test</DisplayString>
        <Expand>
            <CustomListItems>
                <Variable Name="pointer_array" InitialValue="0" />
                <Size>5</Size>
                <Loop Condition="pointer_array &lt; 5">
                  <Item Name="{pointer_array}"> 1 </Item>
                  <Exec> ++pointer_array </Exec>
                </Loop>
              </CustomListItems>
        </Expand>
    </Type>
</AutoVisualizer>
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/bin/foo_example",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}/bin/",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "visualizerFile": "${workspaceFolder}/natvis_file/file.natvis",
            "showDisplayString": true,
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description":  "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

What I expect

My queries


Solution

  • Finally, found the solution though came lots of other questions which will post in new post. I was wrongly interpreting the syntax.

    <?xml version="1.0" encoding="utf-8"?>
    <AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
        <Type Name="FOO">
            <DisplayString>Test</DisplayString>
            <Expand>
            <Item Name="[a]">a</Item>
            <ArrayItems>
                <Size>5</Size>
                <ValuePointer>pointer_array</ValuePointer>
            </ArrayItems>
            </Expand>
        </Type>
    </AutoVisualizer>